Myashka/SO-Python_QA-filtered-2023-tanh_score-after_2023_02 huggingface dataset:
Title: Why I'm getting this error when displaying an image in customtkinter?
Tags: python,customtkinter

Question: I'm trying to display an image from a specific folder, but the error "_tkinter.TclError: image "pyimage2" doesn't exist" is showing. I have verified the image path, and it's correct.
This is a project where many nested functions are called. I suppose this may be the cause of the error, since I have tried this isolated function in another app and it's working properly.
def display_image(csv_file_name, df, frame):
    # Create image path
    image_path = current_folder_path + '/' + csv_file_name + '.png'
    # Create image widget
    my_image = customtkinter.CTkImage(Image.open(image_path), size=(25, 25))
    # Create label to display image
    image_label = customtkinter.CTkLabel(frame, text="", image=my_image)
    image_label.pack()

_tkinter.TclError: image "pyimage2" doesn't exist

Answer: Possibly you've created more than one instance of Tk()/CTk(). Each image object is tied to one instance, and won't work with widgets that are part of a different instance. If this is the problem, use Toplevel() (or whatever the customtkinter equivalent is) to create additional windows. – jasonharper
*This was the reason of the error. I tried to use a class designed for displaying toplevel windows in customtkinter and it worked. Thanks to jasonharper.
Title: VS Code is unable to recognize Python module
Tags: python,visual-studio-code,jupyter,sympy

Question: I'm experiencing an issue with installing and using the 'sympy' module in my Python environment. I created the environment using MiniConda and I'm using VS Code as my code editor. I have already tried installing 'sympy' using both Conda and pip commands, and they both indicate successful installation. However, when I try to import 'sympy' in my Python code, I get a 'ModuleNotFoundError: No module named 'sympy'' error.
I have double-checked that the module is indeed installed in my environment (everywhere sympy is listed as installed), but it seems that VS Code is unable to recognize it. I have restarted my computer and reactivated my environment, but the issue persists.
Could someone please help me troubleshoot this problem? Any suggestions or insights would be greatly appreciated!
import math

from sympy import*

x= Symbol('x')
l = Symbol('l')
t = Symbol('t')
e = Symbol('e')

This is the code I tried to make working.
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[1], line 3
      1 import math
----> 3 from sympy import*
      5 x= Symbol('x')
      6 l = Symbol('l')

ModuleNotFoundError: No module named 'sympy'

And this is the Error in detail.
Thank you in advance.

Answer: Thanks so much for all of your suggestions.
I ended up updating Python to 3.11 in both Anaconda and VS Code and created a new environment using Python 3.11. Now it works just fine.
Title: Determining Volumes for Poetry and Sphinx files in Python from Dockerfile
Tags: python,docker,dockerfile,python-sphinx,python-poetry

Question: I'm working on containerizing my project using Docker, and I want to create volumes for certain files to ensure they are persistent and can be easily accessed. Specifically, I need to determine which files should be created as volumes in my Dockerfile.
For my project managed by Poetry, I want to create volumes for the following files: poetry.lock, pyproject.toml, the /tests directory (used for pytest), and the /src directory (containing my Python files).
Additionally, I'm using Sphinx for documentation, and I would like to create volumes for the following Sphinx files: conf.py, index.rst, and index.html.
My directory structure looks like this:
project
├── poetry.lock
├── pyproject.toml
├── tests/
│   ├── test_file1.py
│   ├── test_file2.py
│   └── ...
├── src/
│   ├── main.py
│   ├── module1.py
│   ├── module2.py
│   └── ...
└── docs/
    ├── conf.py
    ├── index.rst
    └── index.html

What would be the best approach to identify these files and include them as volumes in my Dockerfile? Any guidance or example syntax would be greatly appreciated. Thank you!
This question is aimed at seeking guidance on identifying the files that should be created as volumes in the Dockerfile, specifically for Poetry and Sphinx projects, and requesting examples or advice on how to include them as volumes in the Dockerfile.

Answer: Don't use the Dockerfile VOLUME directive at all.  It mostly has confusing side effects; the easiest way to read it is that no future changes to the referenced directories should be persisted in the image.  At a technical level, it causes Docker to mount an anonymous volume on those directories if nothing else is mounted there.  You rarely if ever want this.  Even if you're using Compose volumes: or docker run -v to mount content into a container, you can mount files over any directory whether it's declared as a VOLUME or not.
The files you mention are all either part of your application source, or developer-only artifacts.  None of these should be in volumes or bind mounts either.  The code should be in your Docker image, and the documentation and test output should be in your host development environment.
Given the directory layout, I might include tests and docs in your .dockerignore file so that they don't get included in the Docker image.  You can run pytest and sphinx in a non-Docker virtual environment to run tests and generate documentation, even if you're deploying the application using Docker.
Title: App gets a lot of requests from same IP in one second, looks like atack
Tags: security,heroku,python-requests

Question: My app MERN in hosted on Heruku.
I see a lot of requests running in the console (heroku logs --tail)in the same second, looks like they are automated.
Requests have weird things inside.
Example is path="/get-spot-by-type-key?key=ifIJnFkezs2v4X**.php**&type=business"
"if" and "php" is something we don't have usually.
All requests are from the same fwd.
How to block them?

Answer: Looks like you are being scanned by some attack proxy. There is no easy solution around it I believe. Or better to say - the solutions may have side effects. You can:

Blacklist the IP address accessing your resources. PRO: easy CONS: it is easy for an attacker to switch to some other IP via proxy/tor and you can exclude some valid users this way.
Blacklist the payload or parts of it. Since this is a scan the payload will change all the time so this will be lots of futile work.

If your website offers resources to authenticated users only - blacklist the user. (ok, this might be actually quite simple but works under certain conditions)
There are other solutions you may want to use like showing users some captcha when you recognize unusual traffic, but they require a bit of implementation effort.
Also you may want to consider hiding your server behind cloudflare or similar and let someone else handle the security response.
Title: How to update SQLAlchemy Postgres array at a single element?
Tags: python,postgresql,sqlalchemy

Question: There is no docs for ARRAY field update in sqlalchemy. Idk, how to update position by index in sqlachemy orm. There is only possible to update the whole array, but I need to update single element in entry.
I found example in postgres docs:
SELECT * FROM sal_emp;
 name  |      pay_by_quarter       |                 schedule
-------+---------------------------+-------------------------------------------
 Bill  | {10000,10000,10000,10000} | {{meeting,lunch},{training,presentation}}
 Carol | {20000,25000,25000,25000} | {{breakfast,consulting},{meeting,lunch}}

UPDATE sal_emp SET pay_by_quarter[4] = 15000
WHERE name = 'Bill';

But I didn't find how to slice or index in .values method, there is only keyword arguments supported.
# sqlalchemy

update(SalEmp).values(pay_by_quarter=???).where(SalEmp.name=='Bill')

# Can't do like this:

update(SalEmp).values(SalEmp.pay_by_quarter[4]=15000).where(SalEmp.name=='Bill')

Answer: You can do this by passing a dict such as {SalEmp.pay_by_quarter[4]: 15000} to values.
The ORM query looks like
update(SalEmp).values({SalEmp.pay_by_quarter[4]: 15000}).where(SalEmp.name=='Bill')
Title: Would it be faster to store data for a transposition table in a text file or an SQL database?
Tags: python,mysql,python-3.x

Question: I'm making a Connect 4 AI, and in order to increase its speed, I have saved previously evaluated positions in an SQLite3 database. (MySQL can be considered.) Take an example item that I would save:
['       br     rbb    brbrb         rb          ',0.786] <-- the string at the start is the board state, and the real is the evaluation for that position.
For my AI, it turns out to be slightly slower to fetch data rather than re-evaluate if I commit the saved data at the end (only if the evaluation function stays the same; if it changes and becomes more time-consuming to run, the speed of the data fetching may outweigh the evaluation). otherwise, the speed is perfectly fine. I want to be able to fetch the evaluation in the shortest time possible.
The only problem that I might have is with fetching the evaluation and only the evaluation. I am not very good with fetching and writing data to text files, and I'm not sure if isolating the evaluation will end up slowing down the process. What's the best way forward?

Answer: Fast is relative, and here you are talking about Input/Outputs (IOs)
IO speed depends on several factors, if you use a naive 'file' (CSV) then you are bound to the speed of your local file system. If you use a DB, you are bound to the connection to the DB.
The fastest you'll get is if you keep the data in memory rather than save it somewhere. You don't say how many rows you need to access, but a decent laptop computer has 16GB of memory these days, even with raw (uncompressed) data, that's 100's millions of rows of your type of data.
If you want speed, just keep the data in memory.
Now, your key to finding the 'evaluation' is a string. If you create a dictionary of the key and the value, as opposed to the array representation you have now, the lookup will be much faster as you don't need to scan the array to find the value; just lookup the key in the dict.
If you really have more rows than can fit into memory, then you might want to consider an in-memory key value store (sharded) rather than a DB. Something like a Redis cluster would be ideal for this use case. A key value store is like a dictionary you access over a connection like a DB. It can be sharded across many servers if size is an issue. If you know the key, retrieval is faster than a DB engine. (Although most DB engines will have cached data in memory too, it will need to go fetch data on disk at first)
Redis can be backed up to disk too; it just does so upon change of a number of keys (or time) that is configurable, so it is not spending its time trying to save to disk.
Title: How to save the extract from multiple urls as csv?
Tags: python

Question: I'm very new to python so please excuse my noobish question/approach.
I systematically want to scrape the website from a QSR brand to get the locations of their restaurants + some more restaurant related information.
Inspecting the website I found out how the url needs to be to access this information.
Therefore I created a csv sheet containing the unique identifier of each restaurant.
I created the following python code to

import the csv with the unique identifier
combine the two parts of the Url with the unique identifier from the csv
request the content from each individual url
write/save it to a csv file

Using the print command I can see that the script is successfully parsing all the url's to the command line.
The requests.get is also accessing the urls. In the end, the csv is only containing the extract from the last url.
What am I missing?
There should be somewhere around 1,500 entries.
I'm kind of assuming that I would have to define that for each requested url the extract should be saved to a new column?
import csv
import requests


> with open(r'C:\Users\xxx\Desktop\Brand.csv') as f:
    reader = csv.reader(f)
    for row in reader:
            urls = "https://locator.uberall.com/api/storefinders/04AxJXeSBFk4qtBbhQ9JCK1987mtnF/locations/" + row[0] + "?v=20230110&language=de&fieldMask=businessId&fieldMask=callToActions&fieldMask=city&fieldMask=country&fieldMask=descriptionLong&fieldMask=descriptionShort&fieldMask=distance&fieldMask=email&fieldMask=fax&fieldMask=googlePlaceId&fieldMask=id&fieldMask=identifier&fieldMask=keywords&fieldMask=lat&fieldMask=lng&fieldMask=name&fieldMask=openingHours&fieldMask=openingHoursNotes&fieldMask=phone&fieldMask=photos&fieldMask=province&fieldMask=socialPost&fieldMask=specialOpeningHours&fieldMask=streetAndNumber&fieldMask=timezone&fieldMask=zip&fieldMask=brands&fieldMask=customItems&fieldMask=events&fieldMask=languages&fieldMask=menus&fieldMask=paymentOptions&fieldMask=people&fieldMask=products&fieldMask=services&fieldMask=socialProfiles&identifier=true"
            print (urls)
            r = requests.get(urls)
            
            with open("results.csv", "wb") as f:
                f.write(r.content)

Answer: Usually with open("results.csv", "wb") as f: is used to write in binary mode. If you want to append each new line instead of overwriting the existing ones you should consider using with open("results.csv", "a") as f: or if you really want to write in binary mode you could use with open("results.csv", "ab") as f:
Title: unrecognized configuration parameter "standard_conforming_strings" while querying from Redshift
Tags: python,linux,sqlalchemy,amazon-redshift,psycopg2

Question: I am trying to connect to my redshift cluster using Sqlalchemy in a linux environment, but i am facing the following issue.

from sqlalchemy import create_engine
import pandas as pd
conn = create_engine('postgresql://connection string')
data_frame = pd.read_sql_query("SELECT * FROM schema.table", conn)



sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedObject)
unrecognized configuration parameter "standard_conforming_strings"

I really dont understand what the issue is. It works perfectly fine in Windows.
PS: Not sure if this makes any difference but i have installed psycopg2-binary on the linux machine in contrast to psycopg2 on windows.
EDIT
1 .The version of pyscopg2 is windows is 2.9.3 while the version of pyscopg2-binary in linux is 2.9.6

The version of Sqlalchemy in windows is 1.4.39 while in linux is 2.0.16

Answer: I have figured out the answer based on the comment by @Adrian . Just changed the version of Sql alchemy on the linux env to what i have on Windows and it works now.
Title: NumPy implementation slower than the for-loop version
Tags: python,numpy

Question: I have two functions, np_prod and slice_prod, that perform the same element-wise operations on two NumPy arrays, mat1 and mat2, which have the same shape. However, I noticed that the np_prod function is slower than the slice_prod function. I used the following code to measure the performance:
import numpy as np
import timeit

shape = (10, 600, 600)
mat1 = np.random.rand(*shape).astype(np.float32)
mat2 = np.random.rand(*shape).astype(np.float32)

def np_prod(mat1, mat2):
    res = mat1.copy()
    res *= -1
    res += 1
    res *= (1 - mat2)
    res *= -1
    res += 1
    return res

def slice_prod(mat1, mat2):
    res = mat1.copy()
    for i in range(res.shape[0]):
        res[i] *= -1
        res[i] += 1
        res[i] *= (1 - mat2[i])
        res[i] *= -1
        res[i] += 1
    return res

N = 400
print("For loop product: ", timeit.timeit("slice_prod(mat1, mat2)", setup="from __main__ import slice_prod, mat1, mat2", number=N))
print("Numpy product: ", timeit.timeit("np_prod(mat1, mat2)", setup="from __main__ import np_prod, mat1, mat2", number=N))

The obtained result is
For loop product:  15.605191999999999
Numpy product:  18.899588700000002

My NumPy version is 1.24.2. I expected the NumPy implementation to be faster due to its optimized array operations, but it seems like the for-loop version is faster in this case. Can anyone explain why this is happening and how I can optimize np_prod to be faster?
(The example I provided is a simplified version of my use case. In my own code the performance diffrence is more notable, like 2x, measured using time.perf_counter)

Answer: It's a matter of cache usage: The original code handles 14MB of data, whereas the second version handles 10 blocks of 1.4MB each.
On my computer for example, the CPU's L3 cache is 8 MB. So for the first version, the data being worked on can't fit in the cache and has to be read from memory on each operation. In the second version, after reading each block to the cache, all the operations can act on the data in the cache.
Changing the code so that the blocks being worked on can fit in the L2 or L1 caches should yield even better performance.
Title: Regular Expression to return filename and extension if there is a realtive path or not
Tags: python,regex

Question: Orginally we were parsing xml files that had the following href attribute values:
href="file.txt"
Now the href attributes can have a relative path:
href=".../docs/file.txt"
In either case I need to return just the file name. We had the following python code calling the following regular expression:
href_pattern = r'href="([^"]*)
While this worked only the filename was there, with a relative path, it will return that as well.
I know that a regular expression of the form:
([^\/]*$)
Will pull the filename only if a relative path is in the value.
My question, as I'm not a regular expression expert, is what is a regular expression that would work in both cases?
Thanks for any help or pointers!
Russ

Answer: It doesn't exactly answer the question but I suggest you to extract the field as you did in the previous version and then you can extract the filename from that result.
Title: Different no of columns in partitionBy output file csvs
Tags: python,apache-spark,pyspark,apache-spark-sql

Question: I am new to pyspark, and an trying to write a dataframe to csv by using partitionBy on a particular column. The column has values from 0-1030. While the first few files are created correctly with the correct no of columns and the partitionBy column is dropped.
However some of the files are created along with the partitionBy column in the csv file  + another column that I drop before i write the dataframe.
Can someone help on this
Data.coalesce(1).write.mode(“overwrite”).partitionBy(“col”).csv(path)

Answer: The code you provided seems to be correct for writing a DataFrame to CSV using partitioning in PySpark. However, it's difficult to determine the exact cause of the issue without more information or the specific error message you encountered.
Here are a few suggestions to investigate and potentially resolve the problem:

Check the column names and data types: Make sure that the column names and data types in your DataFrame are correct. Ensure that there are no duplicate column names and that the column you are partitioning on ("col") exists in the DataFrame.

Verify the data in the DataFrame: Check if there are any unexpected values or inconsistencies in the data. It's possible that some data points might be causing issues during the write process. Use DataFrame transformations and actions to examine the data and filter out any problematic rows.

Confirm the dropping of columns: Double-check the portion of your code where you drop a column before writing the DataFrame to CSV. Ensure that you are dropping the correct column and that it is being dropped before calling the write operation.

Test with a smaller dataset: If the issue persists, try creating a smaller sample dataset with a limited number of rows and test the write operation again. This can help isolate the problem and provide more specific error messages, if any.

Update PySpark version: Ensure that you are using an up-to-date version of PySpark. It's possible that the issue you're encountering is a bug that has been fixed in a later version.


If you can provide more specific information, such as the error message or a sample of the problematic data, I can assist you further in troubleshooting the issue.
Title: Can't spawn background processes after the app was running for some time
Tags: python,multiprocessing

Question: In our Python application we spawn some background processes when the program starts up. Then in some cases we need to shutdown one of them and some time later we want to re-start (= spawn) another background process again.
However, it seems that sometimes, when the application was running for some days already (it's a server application), then it cannot spawn another process. In this case we get the following error log at the call of new_proc.start():
Jun 23 14:23:46 srv gunicorn[2717986]: Traceback (most recent call last):
Jun 23 14:23:46 srv gunicorn[2717986]:   File "<string>", line 1, in <module>
Jun 23 14:23:46 srv gunicorn[2717986]:   File "/usr/lib/python3.8/multiprocessing/spawn.py", line 116, in spawn_main
Jun 23 14:23:46 srv gunicorn[2717986]:     exitcode = _main(fd, parent_sentinel)
Jun 23 14:23:46 srv gunicorn[2717986]:   File "/usr/lib/python3.8/multiprocessing/spawn.py", line 126, in _main
Jun 23 14:23:46 srv gunicorn[2717986]:     self = reduction.pickle.load(from_parent)
Jun 23 14:23:46 srv gunicorn[2717986]:   File "/usr/lib/python3.8/multiprocessing/synchronize.py", line 110, in __setstate__
Jun 23 14:23:46 srv gunicorn[2717986]:     self._semlock = _multiprocessing.SemLock._rebuild(*state)
Jun 23 14:23:46 srv gunicorn[2717986]: FileNotFoundError: [Errno 2] No such file or directory

So apparently the Python multiprocessing module cannot create the file required for a semaphore (or something similar). When I then shutdown the whole application and restart it, then again all subprocesses can be spawned again. So it really seems to be related to the fact that the app was running for some days already ...
Any ideas what might be the issue here?

Answer: If your application has been running for several days, it's possible that some resources, such as file descriptors, have been exhausted. This can happen if the application is not properly releasing resources after they are no longer needed. Make sure you are closing files and releasing other resources correctly in your code. You can use context managers (with statements) or explicitly close resources using the appropriate methods.
Also, some operating systems impose limits on the number of semaphores or the maximum number of open files a process can have. You can check the system limits and adjust them if necessary.
Title: How can I get the location of a device in python?
Tags: python,geolocation,gps

Question: so i'm trying to make a program that can give the user there current gps location, I didn't want to use any sort of API's, what are some libraries I can use to go about this?

Answer: Yoy have many options:
Geopy is a Python library that offers services for geocoding and also has modules for gaining access to GPS data from various sources. The geopy library can be used to determine a user's location based on their IP address or Wi-Fi network.
GPSd is a service daemon that keeps an eye on one or more GPS units and gives access to the GPS data via a number of protocols. The gpsd Python library can be used to interact with the GPSd daemon and retrieve the GPS coordinates from a connected GPS device.
PySerial : If your computer has a serial port and your GPS device is attached to it, you can read data from the GPS device using the pyserial library. You may speak with the device and other objects using the library.
Title: django csrf gives forbidden 403 error for POST
Tags: python,django,csrf

Question: I'm trying to learning Django and as part of a tutorial, i was trying to pass some data through forms. It was good with GET, but POST gives me a forbidden 403. I googled and applied many of those tricks but nothing helped.
Here is my code:
view
@csrf_protect
def counter(request):
    template = loader.get_template("counter.html")
    ss = request.POST["cnt"]
    context = {
        "words" : len(ss.split(" "))
    }
    return HttpResponse(template.render(context, request))

form
<form method="post" action="counter">
{% csrf_token %}
    <textarea name="cnt" rows="10" cols="50"></textarea><br>
    <input type="submit">
</form>

The 403 error at first was:

Forbidden (403) CSRF verification failed. Request aborted. Help Reason given for failure: CSRF token missing.

But after removing cookies (I read that it might be an issue with the cookie), it changed to:

Forbidden (403)
CSRF verification failed. Request aborted.
You are seeing this message because this site requires a CSRF cookie when submitting forms. This cookie is required for security reasons, to ensure that your browser is not being hijacked by third parties.
If you have configured your browser to disable cookies, please re-enable them, at least for this site, or for “same-origin” requests.


This issue only solves when i comment out 'django.middleware.csrf.CsrfViewMiddleware' in the settings, and remove {% csrf_token %} out of html form.

Answer: Based on what you provided you wanted to get the length after splitting the value of what is entered in the textarea. In order to do this, try using request.POST.get('cnt'). This will get the value for you.
Title: pytube throwing 'get_throttling_function_name: could not find match for multiple' error while trying to download YouTube videos
Tags: python,youtube,pytube

Question: I'm using the pytube library in my Python project to download YouTube videos. Everything was working fine until recently, when I started getting an error from pytube. I'm sure my pytube is up-to-date.
Here's the function where I use pytube:
`
# from pytube import YouTube
# import base64

# Get the video
video = YouTube(audio_link)

# Create a buffer
buffer = io.BytesIO()

# Download audio stream into memory
audio_stream = video.streams.get_audio_only()
audio_stream.stream_to_buffer(buffer)

# Base64 encode audio bytes
b64 = base64.b64encode(buffer.getvalue()).decode()

if download_link:
    # Create download link and write to Streamlit
    href = f'<a href="data:audio/mp3;base64,{b64}" download="{meeting_name}.mp3">Download audio file</a>'
    st.markdown(href, unsafe_allow_html=True)

meeting_audio = buffer.getvalue()


# Close the buffer
buffer.close()

`
When the function tries to run video = YouTube(audio_link), it throws the following error:
get_throttling_function_name: could not find match for multiple
I've tried using different YouTube links, and the error is the same. Updating pytube to the latest version didn't solve the issue either.
Does anyone have any idea why this might be happening and how to fix it? Any help would be greatly appreciated.
Thank you.

Answer: Looks like Youtube made more changes, and now the expression pytube had does not work.
I played around with it and got this one working:
r'a.[a-zA-Z]\s*&&\s*([a-z]\s*=\sa.get("n"))\s&&\s*([a-z]\s*=.|([a-zA-Z0-9$])'
replace lines 272, 273 with this and it should work.
god bless regex
Title: Python: How do I import all exception from some module?
Tags: python,exception

Question: I have the following scenario:
Program A uses library L. Library L defines some exceptions. If an exception defined in L is raised in A, I want to serialize it into a json and send it via some channel (e.g. redis). This exception is then received by some program B where I want to deserialize the exception and do something with it. In order to be able to deserialize the exception, B needs to know all the exceptions defined in L. How would I get all exceptions from L? Would it make sen to loop through all members of L and check if they are subclasses of Exception?

Answer: In order to de-serialize the exceptions in B, L must be importable from B, with the same full name that it is importable in A.
If both A, B and L are part of the same Python package, it would just work. If A and B are in different packages, you'd better refactor L to live in its own stand-alone package (not depending from A), and make it a dependency of B.
Title: Callback function for MDFloatingActionButtonSpeedDial in kivymd
Tags: python,kivy,kivymd

Question: Am trying to figure out Kivy in Python and cannot find what I am doing wrong here. I have tried to resolve this bug multiple times but cannot do so. Please help.
P.S. This is not the main MDApp (it is Records(MDApp))class but  a Screen class from ScreenManager. don't know if that's the problem.
python code:
class Hall_of_Fame(Screen):
    def call(self, instance):
        if instance.icon=='pen-plus':
            current_win= 'Add_Record'
        elif instance.icon== 'archive-edit':
            current_win= 'Edit_Record'

        self.manager.current = current_win

kivy file snippet:
<Hall_of_Fame>:
    name:'Hall_of_Fame'
    MDlabel:
        text: 'landed'
        size: 34 

<Match>:
    id: Match
    name: 'Match'

    MDBoxLayout:
        orientation: "vertical"

        MDScreen:        
            MDFloatingActionButtonSpeedDial:
                data: root.data_dic
                root_button_anim: True

                callback: app.call

what am I doing wrong?
(this is my first time asking a question here so sorry for any missing info I should have added)
terminal output:


 Traceback (most recent call last):
   File "c:\Users\Downloads\RECO\Main.py", line 90, in <module>
     Records().run()
   File "C:\Users\AppData\Local\Programs\Python\Python311\Lib\site-packages\kivy\app.py", line 955, in run
     self._run_prepare()
   File "C:\Users\AppData\Local\Programs\Python\Python311\Lib\site-packages\kivy\app.py", line 925, in _run_prepare
     root = self.build()
            ^^^^^^^^^^^^
   File "c:\Users\Downloads\RECO\Main.py", line 84, in build
     kiv =  Builder.load_file('layout.kv')
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "C:\Users\AppData\Local\Programs\Python\Python311\Lib\site-packages\kivy\lang\builder.py", line 305, in load_file
     return self.load_string(data, **kwargs)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "C:\Users\AppData\Local\Programs\Python\Python311\Lib\site-packages\kivy\lang\builder.py", line 407, in load_string
     self._apply_rule(
   File "C:\Users\AppData\Local\Programs\Python\Python311\Lib\site-packages\kivy\lang\builder.py", line 660, in _apply_rule
     child.apply_class_lang_rules(
   File "C:\Users\AppData\Local\Programs\Python\Python311\Lib\site-packages\kivy\uix\widget.py", line 470, in apply_class_lang_rules
     Builder.apply(
   File "C:\Users\AppData\Local\Programs\Python\Python311\Lib\site-packages\kivy\lang\builder.py", line 540, in apply
     self._apply_rule(
   File "C:\Users\AppData\Local\Programs\Python\Python311\Lib\site-packages\kivy\lang\builder.py", line 620, in _apply_rule
     cls = Factory_get(cname)
           ^^^^^^^^^^^^^^^^^^
   File "C:\Users\AppData\Local\Programs\Python\Python311\Lib\site-packages\kivy\factory.py", line 147, in __getattr__
     raise FactoryException('Unknown class <%s>' % name)
 kivy.factory.FactoryException: Unknown class <MDlabel>

Answer: In your kv, MDLabel is spelled wrong. Note the uppercase l.
Title: Can we add values greater than 255 in 8 bit system? If yes then how?
Tags: python

Question: Can we add  values greater than 255 in 8 bit system? If yes then how?
Just not understood! I have seen some websites they introduce overflow concept but it really works? They said it is possible to add values greater than 255 by overflow concept

Answer: If you have an 8 bits adder with an overflow bit, then you have 9 bits of result, which allows to represent numbers up to 511.
Title: python3 and numba, wrong calculation of adler32 checksum
Tags: python-3.x,numba,adler32

Question: I have written the same algorithm in 3 flavours:

plain Python
Python optimized with Numba
Micropython optimized with Viper

I am sure about the correctness of the routine in plain Python and Micropython: the checksum on some megabytes of random data is equal the checksum computed by zlib.adler32
This is the routine written for Numba:
@numba.jit(nopython = True)
def adler_32(data: bytes, length: np.int32, init: np.uint32) -> np.uint32 :
# note: on the first call, init should be 1
    ADLER = np.uint16(65521)
    NMAX = np.int32(5552)                               # max iterations to keep s1, s2 as uint16

    s1 = np.uint16(init & 0xffff)
    s2 = np.uint16(init >> 16)
    i = 0
    while length > 0 :
        k = min(length, NMAX)

        length -= k
        while k :
            s1 += np.uint8(data[i])
            k -= 1
            s2 += s1
            i += 1

        if s1 >= ADLER :
            s1 -= ADLER
        if s2 >= ADLER :
            s2 -= ADLER

    return  (np.uint32(s2) << 16) | s1

Problem: s1 and s2 assumes values greater then 0xffff, so the results are incorrect.
Why s1 and s2, declared as uint16, get bigger values?
Are there any caveats or side effects I don't see?
ERRATA CORRIGE
This is obviously wrong, in a hurry I copy & paste it from a microcontroller routine where the data are small and s1, s2 stay in uint16.
        if s1 >= ADLER :
            s1 -= ADLER
        if s2 >= ADLER :
            s2 -= ADLER

The correct, general algorithm, is
    s1 %= ADLER
    s2 %= ADLER

Answer: You need to understand the magic number in there: 5552. One more than that, 5553, is the number of iterations in the worst case that will overflow s2 if it is a 32-bit unsiged integer. Both s1 and s2 need to be declared as 32 bits. Not 16 bits.
Worst case is s1 and s2 starting at their highest values, 65520, and all of the bytes being 255. Then s2 becomes (1 + n) (65520 + 255 n / 2), where n is the number of bytes, all with value 255. Setting that equal to 232 and solving for n, you get 5552.19.
Another error is that if sx > ADLER: sx -= ADLER falls well short of assuring that sx is less than ADLER, which it needs to be at that point in the loop. Again, for 5552 to have any meaning. Those both need to be sx %= ADLER.
Title: python3 and numba, wrong calculation of adler32 checksum
Tags: python-3.x,numba,adler32

Question: I have written the same algorithm in 3 flavours:

plain Python
Python optimized with Numba
Micropython optimized with Viper

I am sure about the correctness of the routine in plain Python and Micropython: the checksum on some megabytes of random data is equal the checksum computed by zlib.adler32
This is the routine written for Numba:
@numba.jit(nopython = True)
def adler_32(data: bytes, length: np.int32, init: np.uint32) -> np.uint32 :
# note: on the first call, init should be 1
    ADLER = np.uint16(65521)
    NMAX = np.int32(5552)                               # max iterations to keep s1, s2 as uint16

    s1 = np.uint16(init & 0xffff)
    s2 = np.uint16(init >> 16)
    i = 0
    while length > 0 :
        k = min(length, NMAX)

        length -= k
        while k :
            s1 += np.uint8(data[i])
            k -= 1
            s2 += s1
            i += 1

        if s1 >= ADLER :
            s1 -= ADLER
        if s2 >= ADLER :
            s2 -= ADLER

    return  (np.uint32(s2) << 16) | s1

Problem: s1 and s2 assumes values greater then 0xffff, so the results are incorrect.
Why s1 and s2, declared as uint16, get bigger values?
Are there any caveats or side effects I don't see?
ERRATA CORRIGE
This is obviously wrong, in a hurry I copy & paste it from a microcontroller routine where the data are small and s1, s2 stay in uint16.
        if s1 >= ADLER :
            s1 -= ADLER
        if s2 >= ADLER :
            s2 -= ADLER

The correct, general algorithm, is
    s1 %= ADLER
    s2 %= ADLER

Answer: (I wrote this as a comment to Mark post, but SO refuses it is too long.)
I discovered the issue.
Today I tested up to 8 Gbytes of payload, confronting every 256 Mb the partial checksum of zlib.adler32 with my routines. One payload is random data, the other payload all 0xff.
Both the routines returns the same checksums, till the end.
And my routine still uses np.uint16 for s1 and s2 !
It seems to me the numpy variable types are only a "hint" to the JIT, they aren't real types (like  C declarations).
At a certain point,  the variables don't overflow, probably there is an hidden "unboxing" from 16 to 32 bits.
Check: if before the modulus I force 16 bits with 'sx &= 0xffff", the results aren't correct, like @Mark Adler wrote.
I got sidetracked by this "unboxing" and also because I was working on the same routine on three different Python implementations.
Sometimes I hate Python: in C I have discovered the problem in 5 minutes.
I'm an old programmer, used to compiled and typed languages ​​;-)
Title: Is a python venv portable to other machines with same architecture and OS type?
Tags: python,virtualenv,python-packaging,python-venv

Question: I'm building an app based on python3 that uses tkinter and several other packages and I would like to create a an appImage or pyinstaller onefile I can deliver to my remote team members. After spending 4 days trying to get pyinstaller working without success I decided to just create a venv with the required python packages minimal bash scripting and distribute a tar file instead. I thought that would be a quick & straightforward way to go, but even that is proving not to be as easy as I thought it would be. I'm open to suggestions.
I started by creating a folder with a python3 venv (python3 -m venv .) and added all my app files. I activate the venv and use pip to install the python dependencies. I test my app and it works as expected, then I create a tar image of the folder.
When I extract the tar file on a new VM and activate the venv to test, it fails b/c the packages aren't found. Why? the VM is the same OS and machine architecture I used to create the app. I do a pip install of one of the packages that should already be in the venv and sure enough none of them are showing up.
Going back to the dev system I dbl checked if the packages were in the folder I tared up and they were "already satisfied". So what is happening?
Moreover, I discovered that tcl/tk that tkinter relies on isn't installed by default, so that is an external dependency the venv can't resolve, so my choices seem to be narrowing.
I'm just puzzled why the venv didn't preserve the packages my app requires.
Next I'll look into what it will take to create an appImage.

Answer: Yes but not the way you are doing it.  venv/virtualenv ARE MADE TO SOLVE THIS (mostly). the problem is that you aren't using venv properly.
You are trying to distribute the virtual environment itself along with your code which is not how it works.  You need to create a requirements file with pip freeze > requirements.txt and include the requirements.txt file in your tar along with your project code, that's it. Then the recipient after untarring has to recreate the venv on their host or shell and then install the requirements ( pip install -r requirements.txt)
As for external dependencies that's a different issue. But a combination of Docker and VENV could solve that. Or you could denote what libraries the user needs to install on their system in a README.
The main key is that you DO NOT put your project files to be distributed into the venv/virtualenv folder itself.  That folder is just used by pip for managing package installations for your virtual environment.  That folder has to be recreated by the other users manually using the requirements file you produce by freezing your requirements (packages that have been installed into your virtual environment)
Title: Is a python venv portable to other machines with same architecture and OS type?
Tags: python,virtualenv,python-packaging,python-venv

Question: I'm building an app based on python3 that uses tkinter and several other packages and I would like to create a an appImage or pyinstaller onefile I can deliver to my remote team members. After spending 4 days trying to get pyinstaller working without success I decided to just create a venv with the required python packages minimal bash scripting and distribute a tar file instead. I thought that would be a quick & straightforward way to go, but even that is proving not to be as easy as I thought it would be. I'm open to suggestions.
I started by creating a folder with a python3 venv (python3 -m venv .) and added all my app files. I activate the venv and use pip to install the python dependencies. I test my app and it works as expected, then I create a tar image of the folder.
When I extract the tar file on a new VM and activate the venv to test, it fails b/c the packages aren't found. Why? the VM is the same OS and machine architecture I used to create the app. I do a pip install of one of the packages that should already be in the venv and sure enough none of them are showing up.
Going back to the dev system I dbl checked if the packages were in the folder I tared up and they were "already satisfied". So what is happening?
Moreover, I discovered that tcl/tk that tkinter relies on isn't installed by default, so that is an external dependency the venv can't resolve, so my choices seem to be narrowing.
I'm just puzzled why the venv didn't preserve the packages my app requires.
Next I'll look into what it will take to create an appImage.

Answer: A virtual environment (venv) serves as a separate environment for your project and its dependencies. Please ensure that you verify whether your venv is activated or not within the virtual machine (VM). It is important to note that the venv can only be activated through the command prompt.
When it comes to porting the project to a different machine, you can utilize Docker.
Title: VSCode Debugger not working with FastAPI app in Docker container
Tags: python,docker,visual-studio-code,debugging,fastapi

Question: I'm having trouble getting the debugger in VSCode to work with my FastAPI Python app that I run in a Docker container. I've packaged the app into a wheel file and installed it in the container by copying the wheel file over and then installing it.
My setup involves using debugpy to listen to 0.0.0.0:5678, and I have set the VSCode debugger to listen to localhost port 5678. However, while the Docker container starts as expected, the FastAPI app doesn't seem to run.
Here is my launch.json configuration:
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Remote Attach",
            "type": "python",
            "request": "attach",
            "preLaunchTask": "docker-run: debug",
            "connect": {
                "host": "localhost",
                "port": 5678
            },
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}/src/fast_api_app",
                    "remoteRoot": "."
                }
            ],
            "justMyCode": true
        }
    ]
}

And here is my task.json:
{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "docker-build",
            "label": "docker-build development",
            "dockerBuild": {
                "context": "${workspaceFolder}",
                "file": "${workspaceFolder}/Dockerfile",
                "tag": "fastapiapp:debug",
                "remove": true
            }
        },
        {
            "type": "docker-run",
            "label": "docker-run: debug",
            "dependsOn": ["docker-build development"],
            "dockerRun": {
                "image": "fastapiapp:debug",
                "containerName": "fastapiapp-debug",
                "remove": true,
                "envFiles": [
                    "${workspaceFolder}/.env"
                ],
                "ports": [
                    { "hostPort": 8000, "containerPort": 8000 , "protocol": "tcp"},
                    { "hostPort": 5678, "containerPort": 5678 , "protocol": "tcp"}
                ],
                "command": "python -m debugpy --listen 0.0.0.0:5678 --wait-for-client -m fast_api_app.main"
            }
        }
    ]
}

I have followed many tutorials and suggestions but nothing seems to work. Any suggestions or pointers would be greatly appreciated.

Answer: I managed to figure out the issue and resolve it myself. The problem was originating from the Dockerfile; it was still running the command specified in the CMD [...] directive.
Despite specifying a different command to run in the task.json file for the debug session, Docker was defaulting to the command from the Dockerfile, causing the application not to run as expected.
To solve the problem, I removed the CMD [...] directive from the Dockerfile. This allowed the Docker container to use the command specified in the task.json file when starting, enabling it to attach correctly to the VSCode debugger.
I hope this helps anyone else encountering a similar issue!
Title: Image won't go on Tkinter button
Tags: python,image,tkinter,png

Question: For some reason the image I am trying to have on this button will not show up, although I think the Image does exist when the code runs since the button resizes but does not show the image. I have thought this was due to the image properties but have tried many other images that do not work!
Have I missed something quite obvious and am being stupid? PS the button will not call its function when the 'goast image' is there.
settingimage = PhotoImage(file="icons8-settings-50 (1).png")

settingsbut = Button(window, command=settings, image=settingimage)
settingsbut.place(x=10, y=10)

I Have tried resizing the button many times, tried using "compound", using different images and I have tried using a label as a dummy for the image to sit on while the button does not. None of these have resulted in the image being shown on the button. Although no errors occur and the button resizes for the image leading me to think its visual.

Answer: I have 2 concerns:

why is settingsbut lines indented?
is your image, i.e. icons8-settings-50 (1).png in the same file as your Python script?

if these are not the cause of the problem, I suggest that you share a larger section of your code along with an SS of your output/error.
Title: npydantic.error_wrappers.ValidationError: 1 validation error for XXX field required (type=value_error.missing)
Tags: python,django,py-langchain

Question: I've edited the question as going round in circles trying to fix the original error. Here's the latest:
Current error
pydantic.main.BaseModel.__init__\npydantic.error_wrappers.ValidationError: 1 validation error for ConversationBufferMemory2\nchat\n  field required (type=value_error.missing)\n', 'args': '[]', 'kwargs':

llm.py
    chat = Chat.objects.get(id=chat_id)
    memory = ConversationBufferMemory2()
    memory.set_chat(chat)

models2.py
from langchain.memory.buffer import ConversationBufferMemory
class ConversationBufferMemory2(ConversationBufferMemory):
    """Buffer for storing conversation memory."""

    chat:Chat

    #def __init__(self,*args, **kwargs):
    #    super().__init__(*args, **kwargs)
        
    def set_chat(self,chat:Chat):
        self.chat=chat
        
    @property
    def buffer(self) -> Any:
        """String buffer of memory. Each message is separated by a newline."""
        if self.return_messages:
            return self.chat.messages  # Think this is the right return type ? https://python.langchain.com/docs/modules/memory/
        else:
            return get_buffer_string(
                self.chat.messages,
                human_prefix=self.human_prefix,
                ai_prefix=self.ai_prefix,
            )

ValidationError: 

File "/code/apps/llm_module/llm.py", line 183, in get_response\n    memory = ConversationBufferMemory2()\n             ^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File "/usr/local/lib/python3.11/site-packages/langchain/load/serializable.py", line 74, in __init__\n    super().__init__(**kwargs)\n  File "pydantic/main.py", line 341, in pydantic.main.BaseModel.__init__\npydantic.error_wrappers.ValidationError: 1 validation error for ConversationBufferMemory2\nchat\n  field required (type=value_error.missing)\n', 'args': '[]', 'kwargs':

Answer: Ok it looks like I got past this error and moved on to another error. I do not believe the two are related so I will write this one up to help others.
I replaced chat:Chat with chat:Chat=[] so that there was a default value assigned to the chat variable and Pydantic would stop complaining.
I also couldn't just pass in the chat variable during initialisation as it seemed to cause untold amount of grief in relation to the inherticance of ConversationBufferMemory that is happening.