Myashka/SO-Python_QA-filtered-2023-tanh_score-after_2023_02 huggingface dataset:
Title: Python Pathfinding Optimization
Tags: python,algorithm,optimization,path-finding

Question: So the task is to find the safest path for the player within a given maze r, where the amount of encountered monsters is as low as possible.
Every maze is represented as an n by n array, where '.' represents a floor tile, '#' a wall tile and '@' a monster tile.
Start is at the upper left corner and exit at the bottom right corner. This is an example maze:
r = ["....@",
     "@##.#",
     ".##@#",
     ".@..#",
     "###@."]

Player can only move right and down.
I'm able to create a working algorithm, but it slows down significantly as the maze size increases (e.g. a 20 by 20 maze). I'd appreciate any kind of help with optimizing this algorithm:
def count(r):
    n = len(r)
    visited = [[False for j in range(n)] for i in range(n)]
    paths = []
 
    def pathfind(i, j, path, monster_count):
        if i == n - 1 and j == n - 1:
            paths.append((path, monster_count))
            return
        
        visited[i][j] = True
 
        if i + 1 < n and not visited[i + 1][j] and r[i + 1][j] != '#':
            if r[i + 1][j] == '@':
                pathfind(i + 1, j, path + [(i + 1, j)], monster_count + 1)
 
            else:
                pathfind(i + 1, j, path + [(i + 1, j)], monster_count)
 
        if j + 1 < n and not visited[i][j + 1] and r[i][j + 1] != '#':
            if r[i][j + 1] == '@':
                pathfind(i, j + 1, path + [(i, j + 1)], monster_count + 1)
 
            else:
                pathfind(i, j + 1, path + [(i, j + 1)], monster_count)
 
        visited[i][j] = False
 
    if r[0][0] == '@':
        pathfind(0, 0, [(0, 0)], 1)
 
    elif r[0][0] == '#':
        return -1
    
    else:
        pathfind(0, 0, [(0, 0)], 0)

    if len(paths) == 0:
        return -1

    return paths

Answer: The reason why you're slow is that you're exploring paths going through (i, j)  every time you find a path to that spopt, regardless of whether some previous path got to there and figured out what can be done.
Instead of visited, construct 2 data structures.  came_from and monsters.  In came_from you record what path you found that gets here.  In monsters you record how many monsters there were on that path.  If you get to a spot with a came_from you only continue exploring if you got there with fewer monsters than it already had.
And now when you're done you'll be able to start at the bottom right corner and follow came_from back to the start like a cookie trail.
There are improvements on this idea, but this should still find a path in reasonable time even with a 200 by 200 maze.
Title: matplotlib filenotfounderror site-packages
Tags: python,matplotlib,filenotfounderror

Question: matplotlib has completely broken my python env.
When i run:
import matplotlib as plt
I received:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\SamCurtis.AzureAD\AppData\Roaming\Python\Python38\site-packages\matplotlib.libs\.load-order-matplotlib-3.7.1'
I receive the same error if i try to pip install OR pip uninstall matplotlib
Infact all my pip functionality is broken (i cannot pip freeze, uninstall / install) anything.

Answer: I bumped into a similar problem just now after attempting to downgrade back to my old matplotlib version from 3.7.1. pip was throwing up this matplotlib.libs error even when I wasn't trying to do anything involving matplotlib.
The solution was to delete the matplotlib and mpl_toolkits directories from site-packages. Then I was able to reinstall my old matplotlib version and use pip as usual.
Title: Initial pagerank precomputed values with networkx
Tags: python,networkx,pagerank

Question: I'm trying to run an experiment where I have PageRank values and a directed graph built. I have a graph in the shape of a star (many surrounding nodes that point to a central node).
All those surrounding nodes have already a PageRank value precomputed and I want to check how the central node PageRank value is affected by the surrounding ones.
Is there a way to perform this with networkx? I've tried building the graph with weighs (using the weights to store the precomputed PageRank values) but at the end, it look does not look like the central node value changes much.

Answer: I will answer myself the question. In the PageRank method for NetworX you have the parameter nstart, which specifically is the starting pagerank point for the nodes.

nstart : dictionary, optional
Starting value of PageRank iteration for each node.

Still, I'm afraid the graph structure is the limiting factor when doing the random walk and obtaining a relevant result.
Title: ModuleNotFoundError: No module named 'whisper' when trying install in sagemaker
Tags: python,amazon-sagemaker,openai-whisper

Question: I'm trying to install openai-whisper on AWS Sagemaker. I've tried creating virtual env, upgrading to python 3.9 and found out the installation get "Killed" before it even finishes. I need help solving this, been struggling for a couple for days. Thanks in advance.
pip install openai-whisper

Traceback:
Keyring is skipped due to an exception: 'keyring.backends'
Collecting openai-whisper
  Using cached openai-whisper-20230306.tar.gz (1.2 MB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Collecting torch
Killed

import whisper


---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
/tmp/ipykernel_13713/3212043240.py in <module>
----> 1 import whisper

ModuleNotFoundError: No module named 'whisper'

Answer: If you have a look at the last line of the installation logs, this is happening because the installation gets killed due to running out of memory.
Use the following for your installation when you encounter the issue:
pip install openai-whisper --no-cache-dir
Title: ModuleNotFoundError: No module named 'whisper' when trying install in sagemaker
Tags: python,amazon-sagemaker,openai-whisper

Question: I'm trying to install openai-whisper on AWS Sagemaker. I've tried creating virtual env, upgrading to python 3.9 and found out the installation get "Killed" before it even finishes. I need help solving this, been struggling for a couple for days. Thanks in advance.
pip install openai-whisper

Traceback:
Keyring is skipped due to an exception: 'keyring.backends'
Collecting openai-whisper
  Using cached openai-whisper-20230306.tar.gz (1.2 MB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Collecting torch
Killed

import whisper


---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
/tmp/ipykernel_13713/3212043240.py in <module>
----> 1 import whisper

ModuleNotFoundError: No module named 'whisper'

Answer: Can you try to implement this with no virtual environment and let us know what happens? Are you doing this in Studio or Classic Notebook Instances?
Title: Cannot access page on this server (python and selenium)
Tags: python,selenium-webdriver

Question: I'm trying to access a product page from this site, but it shows that I cannot access it on this server, I added an user agent and used a vpn but those didn't work... Any help is appreciated
from selenium.webdriver import Chrome, ChromeOptions
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup

import chromedriver_autoinstaller

chromedriver_autoinstaller.install()

def get_product(driver: Chrome, id: int):
    driver.get(MAIN_URL.format(id))

    link = driver.find_elements(By.CSS_SELECTOR, '[data-clicktype="product_tile_click"]')[0].get_attribute('href')
    driver.get(link) # Cannot access this link

    soup = BeautifulSoup(driver.page_source, 'lxml')
    # ...

MAIN_URL = 'https://www.lowes.com/search?searchTerm={}&refinement=1'


options = ChromeOptions()
options.add_argument("--incognito")
options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36")

driver = Chrome(options=options)

products = []
product_ids = [333346]
for product_id in product_ids:
    products.append(get_product(driver, product_id))

print(products[0])

Answer: There is multiple things you can try: like trying with a browser then a different browser, using a proxy or TOR, add a delay like time.sleep() in case you are trying to access the page too quickly ?
Title: import ibm_db_sa Fails with: AttributeError: type object 'String' has no attribute 'RETURNS_CONDITIONAL'
Tags: python,python-3.x,windows,sqlalchemy

Question: OS: Win 10 22H2
Python: 3.9.13
Install location: C:\Program Files\Python39
ibm-db
Version: 3.1.4
ibm-db-sa
Version: 0.3.9
SQLAlchemy
Version: 2.0.7
When running python -i and import ibm_db_sa I get the below.  Any help appreciated. I searched for dependencies but did not find anything useful.
PS C:\windows\system32> python -i
Python 3.9.13 (tags/v3.9.13:6de2ca5, May 17 2022, 16:36:42) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ibm_db
>>> import ibm_db_sa
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Program Files\Python39\lib\site-packages\ibm_db_sa\__init__.py", line 22, in <module>
    from . import ibm_db, pyodbc, base
  File "C:\Program Files\Python39\lib\site-packages\ibm_db_sa\ibm_db.py", line 20, in <module>
    from .base import DB2ExecutionContext, DB2Dialect
  File "C:\Program Files\Python39\lib\site-packages\ibm_db_sa\base.py", line 662, in <module>
    class DB2Dialect(default.DefaultDialect):
  File "C:\Program Files\Python39\lib\site-packages\ibm_db_sa\base.py", line 675, in DB2Dialect
    returns_unicode_strings = sa_types.String.RETURNS_CONDITIONAL
AttributeError: type object 'String' has no attribute 'RETURNS_CONDITIONAL'
>>>

Answer: I just ran into this issue and discovered the solution after a contact at IBM gave me a lead.
It looks like the ibm_db_sa package was just updated earlier in the month. If you look at the prerequisites on the product description page you will see it requires "SQLAlchemy version between 0.7.3 - 1.4.x". The latest version of SQLAlchemy is 2.0, which you have installed like I did.
I uninstalled SQLAlchemy, and reinstalled a down-leveled version.

pip3 uninstall sqlalchemy
pip3 install sqlalchemy==1.4.47

Once I did this, I was able to run the import cleanly.
Update:
I did discover backleveling SQLAlchemy caused a dependency issue with ipython-sql. and had to run the additional commands.

pip3 uninstall ipython-sql
pip3 install ipython-sql==0.4.1
Title: Speech recognition error can't find Microphone as source
Tags: python,python-3.x,windows,speech-recognition,pyaudio

Question: I'm working on the listening part of a speech recognition project. Currently, I'm stuck with this error message when I run the code.
import speech_recognition as sr

robot_ear = sr.Recognizer()
with sr.Microphone(device_index=1) as mic:
    #robot_ear.adjust_for_ambient_noise(mic)
    print("Robot: I'm listening")
    audio = robot_ear.listen(mic)

try:
    you = robot_ear.recognize_google(audio)
except:
    you = ""

print(you)

This is the error:
Robot: I'm listening
Traceback (most recent call last):
  File "C:\Users\trung\Documents\Python 3\nghe.py", line 7, in <module>
    audio = robot_ear.listen(mic)
            ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\trung\AppData\Local\Programs\Python\Python311\Lib\site-packages\speech_recognition\__init__.py", line 465, in listen
    assert source.stream is not None, "Audio source must be entered before listening, see documentation for ``AudioSource``; are you using ``source`` outside of a ``with`` statement?"
           ^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: Audio source must be entered before listening, see documentation for ``AudioSource``; are you using ``source`` outside of a ``with`` statement?

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\trung\Documents\Python 3\nghe.py", line 4, in <module>
    with sr.Microphone(device_index=1) as mic:
  File "C:\Users\trung\AppData\Local\Programs\Python\Python311\Lib\site-packages\speech_recognition\__init__.py", line 189, in __exit__
    self.stream.close()
    ^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'close'

I tried setting the Microphone manually by looking for its index, which appears to be index 1, but doesn't seem to fix the problem.

Answer: It appears that my microphone access setting in Windows 11 is off. I just needed to go to Start > Settings > Privacy & security > Microphone and make sure Microphone access is turned on, then it's working now.
Title: Does the attribute name length of Python objects has effect on performance?
Tags: python,python-3.x,performance,dictionary,interpreter

Question: Python objects keep attributes in the __dict__ dictionaries (unless you are using __slots__), and you can assign an arbitrary string as an attribute of an object at runtime using setattr() for example. This means that every time an attribute is accessed or assigned, the interpreter must perform the string hashing of the attribute name to get it's value from the dictionary, which is more expensive the longer is the string. Could this affect performance in small programs where the cost of attribute name's hashing is comparable to everything else the program is doing?
Also, if I understand correctly, this doesn't apply to regular variable names since in the bytecode they are converted into some kind of integer indices, right?

Answer: So the answer seems to be that CPython interpreter doesn't actually compute the attribute name's hash on every attribute access. Instead, it applies string interning, meaning that it keeps a single copy of most compile-time string constants (including variable/function/attribute/etc names, and even dictionary keys), and compares their references whenever they appear in code, which are simple integer comparisons. This probably doesn't apply to identifiers created at runtime, such as using setattr().
Thanks to @jasonharper for the pointer.
Title: Pandas Groupby multiple columns with cumcount
Tags: python,pandas

Question: I am new to python
I have a dataset where the same customer can apply for a product multiple times in a day and have fields for cust_number and date
when I apply
df['g']=dfc.groupby('CustNo','DATE').cumcount()

python errors
ValueError: No axis named DATE for object type DataFrame

is there an easy solution? I think an assignment of axis'?
help please

Answer: check the column name in your data frame, whether "DATE" column is exist. if the date column is exist in ur data frame, first you need to change the date format, follow the below step.
import datetime
#use below syntax to convert your date column into date format
df['DATE']=pd.to_datetime(df['DATE'])
Title: np.logical_or() in Numpy - Python
Tags: python,numpy

Question: Please anyone can explain me this resulting code
x = np.array([9, 5])
y = np.array([16, 12])

np.logical_or(x < 5, y > 15)

Result -> array([ True, False])
Since:

np.logical_or(x < 5, y > 15)

x < 5 = [False, False], y > 15 = [True, False]


I think it should be:
[False or False, False or True] = [False, True]

In fact Python giving the result like this:
[False or True, False or False] = [True, False]

Why the result is not [False, True], but in fact python giving the result [True, False]?
It is not match with the order of operation np.logical_or(x < 5, y > 15)
Even I tried ChatGPT explanation I still don't have clear understanding of this concept.
Please I would be appreciate, if anyone can explain the background of python process in detail step by step.

Answer: You are interpreting the result wrong. Numpy's logical_or can have a few arguments as filters. In your case, the input arguments are x < 5 and y > 15. It then combines them together with an or operator.

The first argument x < 5 would lead to: [False, False]
The second argument y > 15 would lead to: [True, False]
The final result = [False or True, False or False] = [True, False]
Title: Issue with implementing inverse FFT for polynoms
Tags: python,algorithm,fft

Question: I am studying the FFT algorithm for fast polynomial multiplication. We went over the algorithm and I decided to try and implement it in Python.
from typing import List
import numpy as np


def fft(p: List[int]) -> List[int]:
    n = len(p)
    if n == 1:
        return p
    
    unity_root = np.exp(2j * np.pi / n)
    
    p_even = p[::2]
    p_odd = p[1::2]
    
    y_even = fft(p_even)
    y_odd = fft(p_odd)
    
    y = [0] * n
    for j in range(n // 2):
        omega = np.power(unity_root, j)
        y[j] = y_even[j] + omega * y_odd[j]
        y[n // 2 + j] = y_even[j] - omega * y_odd[j]
    
    return y


def ifft(p: List[int]) -> List[int]:
    n = len(p)
    if n == 1:
        return p
    
    unity_root = (1 / n) * np.exp(-2j * np.pi / n)
    
    p_even = p[::2]
    p_odd = p[1::2]
    
    y_even = ifft(p_even)
    y_odd = ifft(p_odd)
    
    y = [0] * n
    for j in range(n // 2):
        omega = np.power(unity_root, j)
        y[j] = y_even[j] + omega * y_odd[j]
        y[n // 2 + j] = y_even[j] - omega * y_odd[j]
    
    return y

I tried running the following code to make sure it works
print(ifft(fft([1, 2, 3, 4])))

I expected the output to be the original list I started with as that list represents the coefficients, yet I am getting (ignoring precision issues with floating point arithmetic):
[(4+0j), (11-0j), (12+0j), (13+0j)]

My question is:
Shouldn't I be getting the original list? If I should be getting the original list, where is the mistake in the code? as I went over the code several times and I am having issues finding it. If I shouldn't be getting the original list back and my code is correct, what am I actually getting?

Answer: The problem is that, in ifft, you're dividing the root of unity by n. You need to divide the final result instead.
Title: How does argmax works for Three Dimensional array?
Tags: python,tensorflow,tensorflow2.0,tensor

Question: x=tf.constant ([
      [[1,2,3],
       [4,5,6]],
       [[7,8,9],
       [10,11,12]],
       [[13,14,15],
       [16,17,18]]])
    #print(x)
    print(tf.math.argmax(x,axis=0))

Result:
tf.Tensor(
         [[2 2 2]
          [2 2 2]], shape=(2, 3), dtype=int64)

How does argmax() works for 3D-Arrays??
please someone help!!!

Answer: The result at (0,0) gives you the index (along axis=0) with the highest number. 
So here you compare 1, 7 and 13. Since 13 is the largest number, the result at (0,0) = 2
For the result at (0,1) you compare 2, 8 and 14 and so on...
Title: Display a dataframe larger in python
Tags: python,pandas,dataframe,display

Question: I create a dataframe by a csv file.
df.to_csv('C:\\file.csv', index = False, header = True, encoding='latin1', sep = ';')

My dataframe is like :




Id
Text




1
Pedro-Sanchez                                                                                                                                                                                              /n  Gomez




It displays the first row on two row. When it is a long text with space, the dataframe display it to the line.
I want to display my dataframe like :




Id
Text




1
Pedro-Sanchez Gomez




I use pd.set_option('display.max_colwidth',None) but it doesn't work.
Can you please help me?

Answer: you can use replace to remove this
df['Text'] = df['Text'].str.replace('\n', '')
Title: Display a dataframe larger in python
Tags: python,pandas,dataframe,display

Question: I create a dataframe by a csv file.
df.to_csv('C:\\file.csv', index = False, header = True, encoding='latin1', sep = ';')

My dataframe is like :




Id
Text




1
Pedro-Sanchez                                                                                                                                                                                              /n  Gomez




It displays the first row on two row. When it is a long text with space, the dataframe display it to the line.
I want to display my dataframe like :




Id
Text




1
Pedro-Sanchez Gomez




I use pd.set_option('display.max_colwidth',None) but it doesn't work.
Can you please help me?

Answer: If I don't misunderstand your problem, you are trying to delete the /ns, you should use the regex for this.For example, you can customize it like this
res = []
for sub in test_list:
res.append(sub.replace("\n", ""))
Title: logging basicConfig not having effect in __main__
Tags: python-3.x,logging

Question: I am writing a package that has a __main__.py with something like:
def main():
    logging.basicConfig(level=logging.INFO)
    print(logging.getLogger(__name__))

if __name__ == "__main__":
    main()

And I am running it like so:
python3 -m my_package

And seeing:
<Logger __main__ (ERROR)>

instead of INFO. And so none of my log messages are showing up on the screen.
Why is basicConfig not having effect?

Answer: Another package I was importing was setting logging upfront, and my basicConfig settings were not overriding. I added force=True to my basicConfig call and it works now.
Title: Location of VPython library
Tags: python,vpython

Question: Where is vpython installed?
When I enter in the terminal the command: pip install vpython,There are some processes in the log, but in the end I don’t see any files of this library in the root folder of the project, but at the same time I can import this library in the code

Answer: Like most Python modules, you'll find it in Lib/site-packages/vpython within your Python installation.
Title: In Tkinter is there any way to Zoom in/out on the whole of the GUI content?
Tags: python,tkinter

Question: Looking for similar functionality that a browser has? (ctrl:shift:+ for example)
I tried:
root.tk.call('tk', 'scaling', new_scale) but it didn't change anything.
Any suggestions greatly welcomed...

Answer: In Tkinter is there any way to Zoom in/out on the whole of the GUI content?

No, there is no way to do what you want. There is a way to scale items on a canvas, and you can scale fonts (and thus, widgets that depend on the size of a font) but you can't the UI as a whole.
Title: Python - Is there a built-in publisher/consumer pattern?
Tags: python,python-3.x,python-3.9

Question: I was looking for a very simple, inline, publisher/consumer, or an event pattern, builtin in Python, is there such thing?
For example:
db/user.py
def create(**kwargs):
  user = db.put('User', **kwargs)
  publish('user.created', user)

admin/listeners.py
@consume('user.created')
def send_email_on_signup(user):
  send_admin_mail(f'New user signup {user.name}')

Answer: For in-process event queue, the asyncio event loop can act as a very simple in-process event handling pattern.
Many web frameworks like Django and FastAPI comes with their own event handling patterns though. Django has signals and FastAPI has background task. But it's also pretty common to use something like Celery with both of these.
Title: Python - Is there a built-in publisher/consumer pattern?
Tags: python,python-3.x,python-3.9

Question: I was looking for a very simple, inline, publisher/consumer, or an event pattern, builtin in Python, is there such thing?
For example:
db/user.py
def create(**kwargs):
  user = db.put('User', **kwargs)
  publish('user.created', user)

admin/listeners.py
@consume('user.created')
def send_email_on_signup(user):
  send_admin_mail(f'New user signup {user.name}')

Answer: Python has a built-in module called queue which provides a thread-safe way to implement the producer-consumer pattern.
The queue module provides the Queue class, which implements a thread-safe FIFO queue. The Queue class has methods for adding items to the queue (put()), removing items from the queue (get()), and checking whether the queue is empty or full (empty(), full()).
In the producer-consumer pattern, producers add items to the queue, and consumers remove items from the queue. The queue module provides a convenient way to synchronize access to the queue so that producers and consumers don't interfere with each other.
Title: How to optimize memory usage of itertools.combinations?
Tags: multithreading,memory,memory-management,out-of-memory,python-itertools

Question: I want to generate all possible combinations for my missing letters of password.
I choose itertools.combinations over itertools.products because it produces about 4% of the later and it's fast but after running my code gets killed because of out of memory.
def genb(lst, word_length):
    with concurrent.futures.ThreadPoolExecutor(max_workers=multiprocessing.cpu_count()) as executor:
        comb = ("".join(combination) for combination in itertools.combinations(lst, word_length))
        for future in concurrent.futures.as_completed(executor.submit(combine, comb)):
            result = future.result()
            if result:
                return result
    return False

Is there a way to reduce memory usage of the above code?
I have 256GB of RAM and 64 threads running.

Answer: This memory issue is not related to the combinations() call which uses only a tiny, fixed amount of memory.
Instead, the issue is caused by having too many instances of concurrent futures.
A possible solution is to create only a handful of futures (each running a separate core) and to submit batches of password attempts to evaluate.
Title: SystemExit: 2 : error when calling parse_args()
Tags: python,import,jupyter-notebook,argparse

Question: I am getting the following error. As I am new to python I don't understand how to solve this error. Thank you in advance.
" usage: ipykernel_launcher.py [-h] -i INPUT [-f F]
ipykernel_launcher.py: error: the following arguments are required: -i/--input
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2"
This is my code.
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input", required = True,
                help = 'path to the input data')
parser.add_argument("-f", required=False)

args = vars(parser.parse_args())

Answer: -i or --input are required arguments but you don't pass any argument with

args = vars(parser.parse_args())

Try to add the argument with the folder of the required input data maybe

args = vars(parser.parse_args("-i /folder"))
Title: what does shapely_object.bounds do?
Tags: python,geopandas,shapely

Question: I am working with folium and shapely, and just came across a keyword named as bounds. Can anyone explain what does it exactly do.
I found an explanation but i dont understand the point behind it : circle_geom.bounds is the bounding box of the circle geometry, which is a tuple of (minx, miny, maxx, maxy) coordinates.

Answer: One application could be if you wanted to place a smaller object within a larger object in the direct center.
For example, if you know the max and min x and y of your larger shape, and the same for another shape you wish to place in the direct center of your larger shape, simple calculations can place the smaller object within the larger object in that perfect center.
Does that help?
Title: Slash command gives incorrect value for member.status
Tags: python,discord.py

Question: The following hybrid command gives varying results depending on if it is invoked as a commands.command or as a slash command:
@commands.hybrid_command()
async def test(self, ctx: commands.Context):
    await ctx.send(f"Your status: {ctx.author.status}")

When invoked as !test, it correctly gives the member.status. However, when the command is invoked as a slash command, the result is always "offline".
Any slash command that I create cannot correctly determine member.status, and any commands.command() I create works correctly.
I have my intents set to discord.Intents.all(). I've tried resetting my permissions through the Discord Developer Portal, but I am mostly baffled as to why a normal command works perfectly and a slash command fails in the same scenario.
Edit
Using the object that returns from ctx.guild.get_member(ctx.author) the member.status value works as intended from within a slash command. For whatever reason, it just seems that the member object passed from ctx.author in a slash command does not have the same functionality as it normally does.

Answer: It seems this may just be a bug, as there is nothing inherently wrong with the code (according to the docs). A work-around would be to access the desired member through using guild.get_member() which always returns a functioning discord.Member object.
Title: AzureML Cannot create a deployment in endpoint p2b-sample-endpoint because it is in Creating provisioning state
Tags: azure-devops,azure-machine-learning-service,azureml-python-sdk

Question: online_endpoint_name = "p2b-sample-endpoint"

# create an online endpoint
endpoint = ManagedOnlineEndpoint(
    name=online_endpoint_name,
    description="this is a sample online endpoint",
    auth_mode="key",
    tags={"foo": "bar"},

blue_deployment = ManagedOnlineDeployment(
    name="blue",
    endpoint_name=online_endpoint_name,
    model=model,
    environment=env,
    code_configuration=CodeConfiguration(
        code="./", scoring_script="score.py"
    ),
    instance_type="Standard_DS2_v2",
    instance_count=1,
)
)

ml_client.online_deployments.begin_create_or_update(blue_deployment)



HttpResponseError: (UserError) Cannot create a deployment in endpoint p2b-sample-endpoint because it is in Creating provisioning state.

Code: UserError
Message: Cannot create a deployment in endpoint p2b-sample-endpoint because it is in Creating provisioning state. 
Additional Information:Type: ComponentName
Info: {
    "value": "managementfrontend"
}Type: Correlation
Info: {
    "value": {
        "operation": "295e1dadc1e11a2db8a470788ec6494f",
        "request": "adb9c5f973b580b9"
    }
}Type: Environment
Info: {
    "value": "westeurope"
}Type: Location
Info: {
    "value": "westeurope"
}Type: Time
Info: {
    "value": "2023-03-27T20:16:26.6786058+00:00"
}Type: InnerError
Info: {
    "value": {
        "code": "BadArgument",
        "innerError": {
            "code": "EndpointNotReady",
            "innerError": null
        }
    }
}Type: MessageFormat
Info: {
    "value": "Cannot create a deployment in endpoint {endpointName} because it is in {state} provisioning state."
}Type: MessageParameters
Info: {
    "value": {
        "endpointName": "p2b-sample-endpoint",
        "state": "Creating"
    }
}

Also, no deployment logs found. folder is empty

Answer: Somewhere burried deepdown in the logs was gunicorn not found, I just had to make sure gunicorn was part part of requirements.txt and it worked
Title: big_modeling.py not finding the offload_dir
Tags: python,peft

Question: I'm trying to load a large model on my local machine and trying to offload some of the compute to my CPU since my GPU isn't great (Macbook Air M2). Here's my code:
from peft import PeftModel
from transformers import AutoTokenizer, GPTJForCausalLM, GenerationConfig
from transformers import BitsAndBytesConfig

quantization_config = BitsAndBytesConfig(llm_int8_enable_fp32_cpu_offload=True)

tokenizer = AutoTokenizer.from_pretrained("EleutherAI/gpt-j-6B")

tokenizer.pad_token = tokenizer.eos_token
tokenizer.pad_token_id = tokenizer.eos_token_id

offload_folder="/Users/matthewberman/Desktop/offload"

model = GPTJForCausalLM.from_pretrained(
    "EleutherAI/gpt-j-6B",
    device_map="auto",
    offload_folder=offload_folder,
    quantization_config=quantization_config
)

model = PeftModel.from_pretrained(model, "samwit/dolly-lora", offload_dir=offload_folder)

However, I get this error:
ValueError: We need an `offload_dir` to dispatch this model according to this `device_map`, the following submodules need to be offloaded: base_model.model.transformer.h.10, base_model.model.transformer.h.11, base_model.model.transformer.h.12, base_model.model.transformer.h.13, base_model.model.transformer.h.14, base_model.model.transformer.h.15, base_model.model.transformer.h.16, base_model.model.transformer.h.17, base_model.model.transformer.h.18, base_model.model.transformer.h.19, base_model.model.transformer.h.20, base_model.model.transformer.h.21, base_model.model.transformer.h.22, base_model.model.transformer.h.23, base_model.model.transformer.h.24, base_model.model.transformer.h.25, base_model.model.transformer.h.26, base_model.model.transformer.h.27, base_model.model.transformer.ln_f, base_model.model.lm_head.

I am definitely pointing to a valid offload directory as the previous method uses offload_folder successfully (I see things being put in there).
What am I doing wrong?

Answer: Try adding " offload_folder='./', " to your peftModel.from_pretrained(...) argument.
Title: An error occurred: module 'openai' has no attribute 'ChatCompletion'
Tags: python,openai-api,gpt-4

Question: I'm trying to build a discord bot that uses the GPT-4 API to function as a chatbot on discord. I have the most recent version of the OpenAI library but when I run my code it tells me "An error occurred: module 'openai' has no attribute 'ChatCompletion'"
I tried uninstalling and reinstalling the OpenAI library, I tried using the completions endpoint and got the error "This is a chat model and not supported in the v1/completions endpoint. Did you mean to use v1/chat/completions?"
This is the snippet of code thats giving me issues:
async def get_gpt_response(prompt, history):
    history_strings = [f"{message['role']}: {message['content']}" for message in history] # update history format
    chat_prompt = '\n'.join(history_strings + [f"user: {prompt}"])
    
    completions = openai.ChatCompletion.create(
        engine=config["model"],
        prompt=chat_prompt,
        max_tokens=config["max_tokens"],
        n=1,
        temperature=config["temperature"],
    )
    return completions.choices[0].text.strip().split('assistant:', 1)[-1].strip()

Answer: Make sure you have the latest OpenAI library. I have the same issue and resolved it by upgrade openai 26.5 to 27.2 version.
Title: An error occurred: module 'openai' has no attribute 'ChatCompletion'
Tags: python,openai-api,gpt-4

Question: I'm trying to build a discord bot that uses the GPT-4 API to function as a chatbot on discord. I have the most recent version of the OpenAI library but when I run my code it tells me "An error occurred: module 'openai' has no attribute 'ChatCompletion'"
I tried uninstalling and reinstalling the OpenAI library, I tried using the completions endpoint and got the error "This is a chat model and not supported in the v1/completions endpoint. Did you mean to use v1/chat/completions?"
This is the snippet of code thats giving me issues:
async def get_gpt_response(prompt, history):
    history_strings = [f"{message['role']}: {message['content']}" for message in history] # update history format
    chat_prompt = '\n'.join(history_strings + [f"user: {prompt}"])
    
    completions = openai.ChatCompletion.create(
        engine=config["model"],
        prompt=chat_prompt,
        max_tokens=config["max_tokens"],
        n=1,
        temperature=config["temperature"],
    )
    return completions.choices[0].text.strip().split('assistant:', 1)[-1].strip()

Answer: Make sure you don’t have a file called “openai.py”
Title: An error occurred: module 'openai' has no attribute 'ChatCompletion'
Tags: python,openai-api,gpt-4

Question: I'm trying to build a discord bot that uses the GPT-4 API to function as a chatbot on discord. I have the most recent version of the OpenAI library but when I run my code it tells me "An error occurred: module 'openai' has no attribute 'ChatCompletion'"
I tried uninstalling and reinstalling the OpenAI library, I tried using the completions endpoint and got the error "This is a chat model and not supported in the v1/completions endpoint. Did you mean to use v1/chat/completions?"
This is the snippet of code thats giving me issues:
async def get_gpt_response(prompt, history):
    history_strings = [f"{message['role']}: {message['content']}" for message in history] # update history format
    chat_prompt = '\n'.join(history_strings + [f"user: {prompt}"])
    
    completions = openai.ChatCompletion.create(
        engine=config["model"],
        prompt=chat_prompt,
        max_tokens=config["max_tokens"],
        n=1,
        temperature=config["temperature"],
    )
    return completions.choices[0].text.strip().split('assistant:', 1)[-1].strip()

Answer: Yes i have the file nameed openai.py after changes the name it runs
Title: An error occurred: module 'openai' has no attribute 'ChatCompletion'
Tags: python,openai-api,gpt-4

Question: I'm trying to build a discord bot that uses the GPT-4 API to function as a chatbot on discord. I have the most recent version of the OpenAI library but when I run my code it tells me "An error occurred: module 'openai' has no attribute 'ChatCompletion'"
I tried uninstalling and reinstalling the OpenAI library, I tried using the completions endpoint and got the error "This is a chat model and not supported in the v1/completions endpoint. Did you mean to use v1/chat/completions?"
This is the snippet of code thats giving me issues:
async def get_gpt_response(prompt, history):
    history_strings = [f"{message['role']}: {message['content']}" for message in history] # update history format
    chat_prompt = '\n'.join(history_strings + [f"user: {prompt}"])
    
    completions = openai.ChatCompletion.create(
        engine=config["model"],
        prompt=chat_prompt,
        max_tokens=config["max_tokens"],
        n=1,
        temperature=config["temperature"],
    )
    return completions.choices[0].text.strip().split('assistant:', 1)[-1].strip()

Answer: Be sure your python version is 3.8 or 3.9. I used 3.6 and had the same issue, it was not until I upgraded that it worked correctly
Title: An error occurred: module 'openai' has no attribute 'ChatCompletion'
Tags: python,openai-api,gpt-4

Question: I'm trying to build a discord bot that uses the GPT-4 API to function as a chatbot on discord. I have the most recent version of the OpenAI library but when I run my code it tells me "An error occurred: module 'openai' has no attribute 'ChatCompletion'"
I tried uninstalling and reinstalling the OpenAI library, I tried using the completions endpoint and got the error "This is a chat model and not supported in the v1/completions endpoint. Did you mean to use v1/chat/completions?"
This is the snippet of code thats giving me issues:
async def get_gpt_response(prompt, history):
    history_strings = [f"{message['role']}: {message['content']}" for message in history] # update history format
    chat_prompt = '\n'.join(history_strings + [f"user: {prompt}"])
    
    completions = openai.ChatCompletion.create(
        engine=config["model"],
        prompt=chat_prompt,
        max_tokens=config["max_tokens"],
        n=1,
        temperature=config["temperature"],
    )
    return completions.choices[0].text.strip().split('assistant:', 1)[-1].strip()

Answer: I experienced exactly the same error, even after just installation the OpenAi library.  I ran the script below:
pip install --upgrade openai
Which gave me that latest version with ChatCompletion as a method.
Title: Flow vehicle problems: collision avoidance behavior
Tags: python,reinforcement-learning,sumo,traffic-simulation

Question: In main_UrbanRoadway.py, I generate 3 types of vehicle flows with the help of vehicle.add() defined in flow.core.params.py
Requirements
My requirements for vehicle flows are shown as follows:

Follow the specified acceleration and speed range even under emergency situations which may lead to collisions;

Allow for possible collisions to take place.


Problems occurred

If I set the speed mode as "aggressive"(e.g. SumoCarFollowingParams(speed_mode='aggressive')), will the generated flow follow the acceleration & speed limit set by me in additional_params?

According to the docs, the "obey_safe_speed" mode prevents vehicles from colliding longitudinally, but can fail in cases where vehicles are allowed to lane change. If I set the speed mode as "obey_safe_speed", will the generated flow goes beyond the limit in avoidance with possible longitudinal collisions?


One of my vehicle types is shown below:
vehicles = VehicleParams()

vehicles.add(veh_id="hdv",
             lane_change_params=SumoLaneChangeParams('only_strategic_safe'),
             car_following_params=SumoCarFollowingParams(speed_mode='aggressive', min_gap=5, tau=1, max_speed=MAX_HDV_SPEED),
             acceleration_controller=(IDMController, {})
             )

Answer: "aggressive" will not limit sumo in accelerations, but speed limit yes.

Yes.
Title: Error calling ImageDataGenerator.flow in Keras
Tags: python,machine-learning,keras,image-processing

Question: I am working on image augmentation with images having numeric target value for regression problem, and got the error "ValueError: Unsupported channel number: 150". The code and full stack trace is below.
x_dataset=[]
for file in glob.glob(img):
  a=cv2.imread(file, 0)
  a=cv2.resize(a, (150, 150))
  a=a/255.
  x_dataset.append(a)

datagen = ImageDataGenerator(
    rotation_range=90,  # rotate images up to 20 degrees
    horizontal_flip=True,  # flip images horizontally
    vertical_flip=True  # flip images vertically
)


X.shape

(1, 30, 150, 150)


y.shape

(1, 30)

i=0
for batch in datagen.flow (X, y, batch_size=32, 
                           save_to_dir='aug', 
                           save_format='png'):
  i +=1
  if i> 20:
   break 

ValueError                                Traceback (most recent call last)
<ipython-input-57-7ca22a93f1b2> in <module>
      1 i=0
----> 2 for batch in datagen.flow (X, y, batch_size=32, 
      3                            save_to_dir='aug',
      4                            save_format='png'):
      5   i +=1

3 frames
/usr/local/lib/python3.9/dist-packages/keras/preprocessing/image.py in __next__(self, *args, **kwargs)
    154 
    155     def __next__(self, *args, **kwargs):
--> 156         return self.next(*args, **kwargs)
    157 
    158     def next(self):

/usr/local/lib/python3.9/dist-packages/keras/preprocessing/image.py in next(self)
    166         # The transformation of images is not under thread lock
    167         # so it can be done in parallel
--> 168         return self._get_batches_of_transformed_samples(index_array)
    169 
    170     def _get_batches_of_transformed_samples(self, index_array):

/usr/local/lib/python3.9/dist-packages/keras/preprocessing/image.py in _get_batches_of_transformed_samples(self, index_array)
    807         if self.save_to_dir:
    808             for i, j in enumerate(index_array):
--> 809                 img = image_utils.array_to_img(
    810                     batch_x[i], self.data_format, scale=True
    811                 )

/usr/local/lib/python3.9/dist-packages/keras/utils/image_utils.py in array_to_img(x, data_format, scale, dtype)
    277         return pil_image.fromarray(x[:, :, 0].astype("uint8"), "L")
    278     else:
--> 279         raise ValueError(f"Unsupported channel number: {x.shape[2]}")
    280 
    281 

ValueError: Unsupported channel number: 150

Please help me to come out of this error

Answer: I see, the problem is in your shape:
In X.shape you have (1, 30, 150, 150), but in keras it expects this: (30, 150, 150, 1) which is (batch_size, height, width, channel).
I guess your images are in gray scale so it has only 1 channel.
And for the Y shape you just need (30,).
Title: How to add another dictionary entry in a nested python dictionary
Tags: python,django

Question: I would like to make a dictionary in the dictionary.
I have this code
 dictionary = {}
    for g in genre:
        total = 0
        products = Product.objects.filter(genre=g)
        for product in products:
            total += product.popularity
        dictionary[g.category] = {g.name: total}

I would like it to look like this, for example
{'book': {'Horror':0, 'Comedy:0}, 'cd': {'Disco': 0, 'Rap': 0}}

Answer: for g in genre:
total = 0
products = Product.objects.filter(genre=g)
for product in products:
total += product.popularity
if g.category not in dictionary:
dictionary[g.category] = {}
dictionary[g.category][g.name] = total
Title: Deleted conda environments still appears in jupyter-lab. How can I remove them?
Tags: python,jupyter-notebook,jupyter,conda,environment

Question: Formerly, I add a conda installation of Python in ~/opt/anaconda3 with 3 environments : DEV (python 3.9) , PYT (python 3.11) and PROD (python 3.10)
I removed it (all ~/opt deleted), an now I have a miniconda installation in ~/miniconda3, with one environment : PROD (python 3.10)
When I start jupyter-lab, it still offers me to create notebook with DEV, PYT and PROD. It evidently fails if I try to use one of the two firsts, saying that  ~/opt/anaconda3/envs/DEV does not exists.
How can I get rid of the two inexistent environments ? Where is the memory of that ?
Computer is Mac under OSX 13.2.1. jupyter-lab is 3.6.3.
Thanks,
Olivier

Answer: Did you look at your ~/.bashrc file, which might still export the path of those environments in your python path?
Using the command jupyter kernelspec list, you can look at the different envs you created. If you see unwanted kernels, you can do jupyter kernelspec uninstall unwanted-kernel.
Title: Deleted conda environments still appears in jupyter-lab. How can I remove them?
Tags: python,jupyter-notebook,jupyter,conda,environment

Question: Formerly, I add a conda installation of Python in ~/opt/anaconda3 with 3 environments : DEV (python 3.9) , PYT (python 3.11) and PROD (python 3.10)
I removed it (all ~/opt deleted), an now I have a miniconda installation in ~/miniconda3, with one environment : PROD (python 3.10)
When I start jupyter-lab, it still offers me to create notebook with DEV, PYT and PROD. It evidently fails if I try to use one of the two firsts, saying that  ~/opt/anaconda3/envs/DEV does not exists.
How can I get rid of the two inexistent environments ? Where is the memory of that ?
Computer is Mac under OSX 13.2.1. jupyter-lab is 3.6.3.
Thanks,
Olivier

Answer: I found the envs in ~/Library/Jupyter/kernels
@CamB04 : I found my solution without kernelspec. But yes, I see now that jupyter kernelspec ... is the right tool. Thank :-)
Title: Why is NiceGUI displaying a notification before the function with the notification is called?
Tags: python,nicegui

Question: When I run this code, I'm expecting the "running action" notification to not be shown until the action function is called, similar to how the "handling upload" notification is not shown until the handle_upload function is called. I'm not sure why it works as expected with handle_upload, but not with action. When I upload a file, I see both notifications, "running action" and "handling upload". When I click the "Action the Data" button, I don't see any notification. Appreciate any advice/suggestions for getting the expected behavior (and better understanding how NiceGUI works).
@ui.page("/submit")
def submit():
    def action_it(data: bytes) -> None:
        ui.notify("running action")

    def handle_upload(file: events.UploadEventArguments) -> None:
        ui.notify("handling upload")
        data = file.content.read()
        lines = [line for line in data.splitlines() if line.strip()]
        output.clear()
        with output:
            ui.label('Lines uploaded:')
            for line in lines:
                ui.label(line.decode())
    
    ui.button("Action the Data", on_click=action_it(data))
    ui.button("Cancel")

    ui.upload(on_upload=handle_upload, auto_upload=True)
    output = ui.column()

Answer: @markalex Pointed out that I wasn't passing a function delegate to on_click but was actually sending it the results of the function execution, and that is why I wasn't getting the behaviour I expected.
To get the behavior I wanted, I just needed to use a lambda function to call my function and pass it the argument:
ui.button("Action the Data", on_click=lambda: action_it(data))