Myashka/SO-Python_QA-filtered-2023-tanh_score-after_2023_02 huggingface dataset:
Title: How can I use pandas.query() to check if a string exists in a list within the dataframe?
Tags: python,pandas,dataframe

Question: Let's say I have a dataframe that looks like:
    col1
0   ['str1', 'str2']
1   ['str3', 'str4']
2   []
3   ['str2', 'str4']
4   ['str1', 'str3']
5   []

I'm trying to craft a df.query() string that would be the equivalent of saying "'str3' in col1".
So it would return:
    col1
1   ['str3', 'str4']
4   ['str1', 'str3']

I've tried df.query("col1.str.contains('str3')") but that results in
"None of [Float64Index([nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,\n              ...\n              nan, nan, nan, nan, nan, nan, nan, nan, nan, nan],\n             dtype='float64', length=652)] are in the [index]"
I'm guessing because many of the lists in this column may be empty and convert to nan floats instead of strings?
It's highly preferable that I use query strings for this since rather than list constructors, since I want this to be a script where other users can filter a dataframe using these query strings that they may craft.

Answer: Is it possible in your set up to use lambda functions? Because it seems that data could be filtered with mask:
df['col'].apply(labmda x: 'str3' in x)
Title: Pip installs packages in the wrong directory
Tags: python,pip,python-module,python-packaging,pythonpath

Question: So I want to install the opencv-python package. I typed in pip install opencv-python and got this:
Collecting opencv-python
  Downloading opencv_python-4.7.0.72-cp37-abi3-win_amd64.whl (38.2 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 38.2/38.2 MB 3.1 MB/s eta 0:00:00
Requirement already satisfied: numpy>=1.17.0 in c:\users\leo westerburg burr\appdata\local\packages\pythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0\localcache\local-packages\python39\site-packages (from opencv-python) (1.24.2)
Installing collected packages: opencv-python
Successfully installed opencv-python-4.7.0.72

When I try and import the package (on IDLE) I get
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    import cv2
ModuleNotFoundError: No module named 'cv2'

This is the same for all the packages I have installed recently, like numpy. The thing is when I type sys.path into the IDLE I get
C:\Users\Leo Westerburg Burr\AppData\Local\Programs\Python\Python311\Lib\idlelib
C:\Users\Leo Westerburg Burr\AppData\Local\Programs\Python\Python311\python311.zip
C:\Users\Leo Westerburg Burr\AppData\Local\Programs\Python\Python311\DLLs
C:\Users\Leo Westerburg Burr\AppData\Local\Programs\Python\Python311\Lib
C:\Users\Leo Westerburg Burr\AppData\Local\Programs\Python\Python311
C:\Users\Leo Westerburg Burr\AppData\Local\Programs\Python\Python311\Lib\site-packages

Which are all in the AppData/Local/Programs directory, however the packages are stored in appdata\local\packages\pythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0\... as you can see when I installed opencv-python - which I find weird; why are they installed there and not in programs\python ?
I have tried reinstalling pip, and also downloading a newer version of python. What is weird is that I have Python311 and Python38 in my Python folder, but this weird folder that has the packages is python39?
So my question is: how do I get pip to install packages in Programs\Python\Python311\..., rather than Packages\... ?
Do I have to add something to my PATH?

Answer: It seems that you have both python3.9 and 3.11 installed. When just typing in pip install ... you probably install your package in python 3.9 whereas you run python 3.11 in IDLE.
Try python -V in your command prompt, it will probably answer python3.11
Title: ValueError: source code string cannot contain null bytes
Tags: python,python-3.x,windows,virtual-machine

Question: I'm originally an Ubuntu user, but I have to use a Windows Virtual Machine for some reason.
I was trying to pip-install a package using the CMD, however, I'm getting the following error:
from pip._vendor.packaging.utils import canonicalize_name
   ValueError: source code string cannot contain null bytes

I used pip install numpy and pip3 install numpy along with other commands I found while tying to fix the problem.
I checked that pip is available and reinstalled Python to make sure the path is added. I've also made sure that I'm running everything as an administrator. Everything seems to be installed properly, but I keep getting that error.
I've also checked almost all other StackOverflow questions related to this error message.
How can I solve this?

Answer: The error that occurred was while using "Python 3.10.11 (64-bit)". Though I reinstalled it the issue continued.
When I downgraded to "Python 3.9.0 (64-bit)", the issue was solved.
Title: conda error: zstandard could not be imported
Tags: python,conda

Question: My Conda (on Ubuntu 18.04) has the following error message but all functions are running correctly:
/home/td7920/miniconda3/lib/python3.8/site-packages/conda_package_streaming/package_streaming.py:19: UserWarning: zstandard could not be imported. Running without .conda support.
  warnings.warn("zstandard could not be imported. Running without .conda support.")
/home/td7920/miniconda3/lib/python3.8/site-packages/conda_package_handling/api.py:29: UserWarning: Install zstandard Python bindings for .conda support
  _warnings.warn("Install zstandard Python bindings for .conda support")
Collecting package metadata (repodata.json): | /home/td7920/miniconda3/lib/python3.8/site-packages/conda_package_streaming/package_streaming.py:19: UserWarning: zstandard could not be imported. Running without .conda support.
  warnings.warn("zstandard could not be imported. Running without .conda support.")
/home/td7920/miniconda3/lib/python3.8/site-packages/conda_package_handling/api.py:29: UserWarning: Install zstandard Python bindings for .conda support
  _warnings.warn("Install zstandard Python bindings for .conda support")

I did pip install zstandard and conda install zstandard. When I do conda list, both are found,
zstandard                 0.19.0           py38h5945529_1    conda-forge
zstd                      1.5.5.1                  pypi_0    pypi

But still get the error.

Answer: I figured out a way to fix this issue. We need to import zstandard in the following two .py files and everything will be smooth as wind.

/home/ubuntu/anaconda3/lib/python3.9/site-packages/conda_package_handling/api.py


/home/ubuntu/anaconda3/lib/python3.9/site-packages/conda_package_streaming/package_streaming.py
Title: YOLOv5: does best.pt control for overfitting?
Tags: python,machine-learning,artificial-intelligence,yolov5,fitness

Question: After each YOLOv5 training, two model files are saved: last.pt and best.pt. I'm aware that:

last.pt is the latest saved checkpoint of the model. This will be updated after each epoch.
best.pt is the checkpoint that has the best validation loss so far. It is updated whenever the model fitness improves.

Fitness is defined as a weighted combination of mAP@0.5 and mAP@0.5:0.95 metrics:
 def fitness(x): 
     # Returns fitness (for use with results.txt or evolve.txt) 
     w = [0.0, 0.0, 0.1, 0.9]  # weights for [P, R, mAP@0.5, mAP@0.5:0.95] 
     return (x[:, :4] * w).sum(1) 

My question is, if the training continued for too many epochs (and last.pt is thus overfitted), is best.pt then a checkpoint from when the training was not yet overfit? In other words, does best.pt control for overfitting?

Answer: We can assume that the best.pt has a good performance for non-training data when the model has a regularization. However, I saw some researchers who choose a model which doesn't have the best result in terms of the limitation of validation loss.
If you don't have a lot of costs to train your model, you could consider the option that I mentioned, otherwise, you could just pick up the best.pt.
In addition, we choose some of the models to ensemble them. This can collaborate with the first, second, etc., Although the validation loss of models has a slight difference, they can produce better performance than just using the best model.
Title: AttributeError: module 'jmespath' has no attribute 'search'
Tags: python,attributeerror,jmespath

Question: I encountered that error:

import jmespath
AttributeError: Module jmespath has no attribute search

I used the version 1.0.1 of this module.
I wanted to run the following code:
import jmespath

person = {
    "person": [
        {'id': 1, 'name': 'ali', 'age': 42, 'children': [
            {'name': 'sara', 'age': 7},
            {'name': 'sima', 'age': 15},
            {'name': 'sina', 'age': 2}
        ]},
        {'id': 2, 'name': 'reza', 'age': 65, 'children': []}
    ]
}

print(jmespath.search('person[*].children[?age>`10`].name', person))

Answer: Which version of Python are you using? If it's an older version, you can try running the code with a higher version of Python.
Title: How do I change the value of a variable inside a string?
Tags: python,python-3.x,string

Question: I'm trying to simplify the error message since on my project it's expected to print a lot of errors.
I tried this simple example but nothing seems to work.
How can I make it so 'n' iterates and actually prints the position where there is an error?
It always prints as if 'n=1'.
n = 1
error = f"Error word {n}"

def test(word):
    if len(word)>5:
        print(error)

ar = ['pizza', 'toasts', 'ham', 'mayo', 'houses']
for word in ar:
    test(word)
    n+=1

I've tried
error = f"Error word {n}"

error = 'Error word '+ str(n)

error = "Error word {n}".format(n)

But maybe I'm not looking at the issue the correct way?

Answer: because n is outside the loop. the code once executed is not updating the value on n in line number 2. it is something like error = 'Error word 1'
Title: mlrun cann't show the traning graph by AttributeError(\"'K8sHelper' object has no attribute 'v1api'\")
Tags: python,nginx,show,http-status-code-500,mlrun

Question: I have run mlrun by docker-compose. But when I finished the sample training, I couldn't see the results graph on the management pages. I get 500 error and the details likes:
{
 "data": {
   "detail": "AttributeError(\"'K8sHelper' object has no attribute 'v1api'\")"
 },
 "status": 500,
 "statusText": "Internal Server Error",
 "headers": {
   "connection": "keep-alive",
   "content-length": "76",
   "content-type": "application/json",
   "date": "Fri, 07 Apr 2023 09:45:51 GMT",
   "server": "nginx/1.21.6"
 },
 "config": {
   "url": "projects/quick-tutorial-root/files",
   "method": "get",
   "headers": {
     "Accept": "application/json, text/plain, */*"
   },
   "params": {
     "path": "/auto-trainer-train/0/confusion-matrix.html"
   },
   "baseURL": "/mlrun/api/v1",
   "transformRequest": [
     null
   ],
   "transformResponse": [
     null
   ],
   "timeout": 0,
   "xsrfCookieName": "XSRF-TOKEN",
   "xsrfHeaderName": "X-XSRF-TOKEN",
   "maxContentLength": -1,
   "maxBodyLength": -1,
   "transitional": {
     "silentJSONParsing": true,
     "forcedJSONParsing": true,
     "clarifyTimeoutError": false
   }
 },
 "request": {}
}

I do not find any info for this question, please help me, thanks!

Answer: Yes, I have found a method to solve this problem by myself. This problem is due to k8s config conflict. So I can change the code in the container of mlrun-api and then commit a new image.
Now I add after the 66-line code in mlrun/api/api/endpoints/files.py with "use_secrets= False" temporarily. The show is on UI normally.
Title: pptx issues with fonts size and color from procedure
Tags: python,procedure,python-pptx

Question: ive created a code to create PPT presentation from a dataframe. defined a procedure "addtext" to add text passing some parameters. somehow, procedure is ignoring text size and color .. why ?  thanks!
from pptx import Presentation
from pptx.util import Inches, Pt
prs = Presentation()
blank_slide_layout = prs.slide_layouts[6]
import pandas as pd

data = {
    'titolo': ['Pizza Margherita', 'Bolognese', 'Fish and Chips', 'Tacos', 'Sushi', 'Paella', 'Spaghetti Carbonara', 'Couscous', 'Falafel', 'Goulash'],
    'commenti': ['pomodoro, mozzarella, basilico', 'pomodoro, carne macinata, carote, sedano, cipolla', 'pesce fritto, patatine fritte', 'carne, cipolle, peperoni, pomodoro, avocado', 'riso, pesce crudo, alga nori', 'riso, pollo, gamberi, verdure', 'uova, pancetta, pecorino romano', 'couscous, agnello, verdure', 'fagioli, cipolle, spezie', 'carne, paprika, patate'],
    'numero': [8, 12, 10, 9, 15, 20, 14, 18, 6, 16],
    'paese': ['Italia', 'Italia', 'Regno Unito', 'Messico', 'Giappone', 'Spagna', 'Italia', 'Marocco', 'Medio Oriente', 'Ungheria']
}

df = pd.DataFrame(data)


def addtext(titlee,L,T,SS,RR,GG,BB):
    txBox = slide.shapes.add_textbox(Inches(L),Inches(T),Inches(1),Inches(1))
    tf = txBox.text_frame
    p = tf.paragraphs[0]
    run = p.add_run()
    run.text = titlee
    font.name = 'Calibri'
    font.size = Pt(SS)
    font.color.rgb = RGBColor(RR,GG,BB)

for index, row in df.iterrows():
    slide = prs.slides.add_slide(blank_slide_layout)
    
    addtext(row['titolo'],.2,1,42,0, 0, 255)
    addtext(row['commenti'],.2,2,22,102, 255, 102)
    addtext('Costo medio: ' + str(row['numero']) + ' €',.2,5,18,32, 32, 255)

prs.save('test.pptx')

Answer: You haven't associated the font variable with the run (variable run).
Title: How to specify constant inputs for Gradio click handler?
Tags: python,gradio

Question: If I have the following code:
submit = gr.Button(...)
submit.click(
  fn=my_func,
  inputs=[
    some_slider_1, # gr.Slider
    some_slider_2, # gr.Slider
    some_slider_3, # gr.Slider
  ],
  outputs=[ some_text_field ]
)

How can I substitute let's say some_slider_2 for a constant value like 2?
If I simply write 2 in there, I get an error:
AttributeError: 'int' object has no attribute '_id'

Answer: If you want a constant number input which is not visible on the UI you can pass gr.Number(value=2, visible=False)
Title: Datalogic 8500xt misses the last digit while scanning barcodes using pyserial
Tags: python,barcode-scanner,pyserial,serial-communication

Question: I'm trying to scan barcodes using a Datalogic 8500xt scanner with pyserial in my Python program. However, I'm facing an issue where the scanner misses the last digit of the barcode. For instance, if I scan a barcode that has seven digits, such as "1234567", the scanner only returns "123456". The same issue occurs when I scan a barcode that has five digits, such as "09876", and the scanner returns "0987".
I have tried to adjust the scanner's settings to fix the issue, but with no success. I also tried to increase the sleep time between scans, but it didn't help either. I'm using pyserial to communicate with the scanner, and I'm wondering if there is something I'm missing in my code.
Here is a code snippet that I'm using to read data from the scanner:
 try:
        # device_port= serial_ports()[0]
        device_port= 'COM3'
        print("device_port : ",device_port)
        print("Connecting device")
        ser = serial.Serial(
            port = device_port,
            timeout = 1,
            baudrate=9600,
            parity=serial.PARITY_EVEN,
            stopbits=serial.STOPBITS_ONE,
            bytesize=serial.SEVENBITS,
        )
        i = True

        while i:
            # if wt == 'S14\x0D':
                ser.write('S01\x0D'.encode('utf-8'))
                print("connected")
                # ser.write('\x05'.encode('utf-8'))
                # time.sleep(1)
                barcode=None
                read_val = ser.read(size=128)
                read_val = str(read_val, 'UTF-8')  
                res = list(read_val)
                print(res)
                res = res[4:-1]
                if len(res)>0:
                    i=False
                    barcode=res
        ser.close()
        return {"barcode": barcode}
                
    except:           
            return {"message": "No Barcode Device Connected"}

Answer: I can see three possible culprits:

Off by one logic error: try res = res[4:-1] -> res = res[4:]
Insufficient timeout time, try timeout=1 -> timeout=5,  # Increase timeout to allow more time for data transfer
Insufficient read buffer size: try ser.read(size=128) -> ser.read(size=256)

Also remove the bare except and replace it with a more specific one, to avoid hiding errors while debugging.
Title: Find the most similar subsequence in another sequence when they both numeric and huge
Tags: python,list,numpy

Question: I have two numeric and huge np.arrays (let's call them S1 and S2, such that len(S1)>>len(S2)>>N where N is a very large number). I wish to find the most likely candidate part of S1 to be equal to S2.
The naive approach would be to compute a running difference between S2 and parts of S1. This would take too long (about 170 hours for a single comparison).
Another approach I thought about was to manually create a matrix of windows, M, where each row i of M is S1[i:(i+len(S2)]. Then, under this approach, we can broadcast a difference operation. It is also infeasible because it takes a long time (less than the most naive, but still), and it uses all the RAM I have.
Can we parallelize it using a convolution? Can we use torch/keras to do something similar? Bear in mind I am looking for the best candidate, so the values of some convolution just have to preserve order, so the most likely candidate will have the smallest value.

Answer: I am assuming you are doing this as a stepping stone to find the perfect match
My reason for assuming this is that you say:

I wish to find the most likely candidate part of S1 to be equal to S2.


Start with the first value in the small array.

Make a list of all indices of the big array, that match that first value of the small array. That should be very fast? Let's call that array indices, and it may have values [2,123,457,513, ...]

Now look at the second value in the small array. Search through all positions indices+1 of the big array, and test for matches to that second value. This may be faster, as there are relatively few comparisons to make. Write those successful hits into a new, smaller, indices array.

Now look at the third value in the small array, and so on. Eventually the indices array will have shrunk to size 1, when you have found the single matched position.


If the individual numerical values in each array are 0-255, you might want to "clump" them into, say, 4 values at a time, to speed things up. But if they are  floats, you won't be able to.
Typically the first few steps of this approach will be slow, because it will be inspecting many positions. But (assuming the numbers are fairly random), each successive step becomes much faster. Therefore the determining factor in how long it will take, will be the first few steps through the small array.
This would demand memory size as large as the largest plausible length of indices. (You could overwrite each indices list with the next version, so you would only need one copy.)
You could parallelise this:
You could give each parallel process a chunk of the big array (s1). You could make the chunks overlap by len(s2)-1, but you only need to search the first len(s1) elements of each chunk on the first iteration: the last few elements are just there to allow you to detect sequences that end there (but not start there).
Proviso
As @Kelly Bundy points out below, this won't help you if you are not on a journey that ultimately ends in finding a perfect match.
Title: Using Scapy and a wireless network card to scan network - getting Network is down Error
Tags: python,networking,wifi,scapy,packet-sniffers

Question: Disclaimer:  I can't use aircrack
I am using Ubuntu (if that helps)
I have a wireless network adapter USB device that I am using (tenda N150) to scan networks.
I have used a tutorial and in the tutorial they asked to put the device in monitor mode
sudo ifconfig wlan0 down
sudo iwconfig wlan0 mode monitor

The code that I am running is:
from scapy.all import *
from threading import Thread
import pandas
import time
import os

# initialize the networks dataframe that will contain all access points nearby
networks = pandas.DataFrame(columns=["BSSID", "SSID", "dBm_Signal", "Channel", "Crypto"])
# set the index BSSID (MAC address of the AP)
networks.set_index("BSSID", inplace=True)

def callback(packet):
    if packet.haslayer(Dot11Beacon):
        # extract the MAC address of the network
        bssid = packet[Dot11].addr2
        # get the name of it
        ssid = packet[Dot11Elt].info.decode()
        try:
            dbm_signal = packet.dBm_AntSignal
        except:
            dbm_signal = "N/A"
        # extract network stats
        stats = packet[Dot11Beacon].network_stats()
        # get the channel of the AP
        channel = stats.get("channel")
        # get the crypto
        crypto = stats.get("crypto")
        networks.loc[bssid] = (ssid, dbm_signal, channel, crypto)


def print_all():
    while True:
        os.system("clear")
        print(networks)
        time.sleep(0.5)


def change_channel():
    ch = 1
    while True:
        os.system(f"iwconfig {interface} channel {ch}")
        # switch channel from 1 to 14 each 0.5s
        ch = ch % 14 + 1
        time.sleep(0.5)


if __name__ == "__main__":
    # interface name, check using iwconfig
    interface = "wlxc83a35c2e0bb"
    # start the thread that prints all the networks
    printer = Thread(target=print_all)
    printer.daemon = True
    printer.start()
    # start the channel changer
    channel_changer = Thread(target=change_channel)
    channel_changer.daemon = True
    channel_changer.start()
    # start sniffing
    sniff(prn=callback, iface=interface, monitor=True)

The error that I am getting is:
OSError: [Errno 100] Network is down
This is the output of the iwconfig
wlxc83a35c2e0bb  IEEE 802.11  Mode:Monitor  Tx-Power=20 dBm   
          Retry short  long limit:2   RTS thr:off   Fragment thr:off
          Power Management:off


Any Ideas why this might happen ?
the code that I tried it written above

Answer: So, the problem was that i forgot:
sudo iwconfig wlan0 up
and because of that the interface stayed down.
Title: I can't install Anaconda on a MacBook Pro M1 with Ventura 13.3.1
Tags: python,installation,anaconda,macos-ventura

Question: this is my first question here :)
When i try to install Anaconda on my MacBook (M1) with Ventura 13.3.1 i receive the following error:
"This package is incompatible with this version of macOS."
I tried the arm64 installer and the x86 installer, both lead to the same error message.
I used Anaconda on the same MacBook just a few days ago, but after the update from Ventura 13.2.1 to 13.3 I couldn't open Jupyter Notebooks from within the Anaconda Navigator. First I thought that the problem might be caused by Anaconda, so I uninstalled it. However, now here I am, unable to install it again. I also did a complete reset of my MacBook, nothing changed.
Does anyone have the same issue or know how I can fix this?
Thanks a lot

Answer: if you have homebrew installed you should be able to run "brew install anaconda"
Title: I can't install Anaconda on a MacBook Pro M1 with Ventura 13.3.1
Tags: python,installation,anaconda,macos-ventura

Question: this is my first question here :)
When i try to install Anaconda on my MacBook (M1) with Ventura 13.3.1 i receive the following error:
"This package is incompatible with this version of macOS."
I tried the arm64 installer and the x86 installer, both lead to the same error message.
I used Anaconda on the same MacBook just a few days ago, but after the update from Ventura 13.2.1 to 13.3 I couldn't open Jupyter Notebooks from within the Anaconda Navigator. First I thought that the problem might be caused by Anaconda, so I uninstalled it. However, now here I am, unable to install it again. I also did a complete reset of my MacBook, nothing changed.
Does anyone have the same issue or know how I can fix this?
Thanks a lot

Answer: thanks  "brew install anaconda".
Title: I can't install Anaconda on a MacBook Pro M1 with Ventura 13.3.1
Tags: python,installation,anaconda,macos-ventura

Question: this is my first question here :)
When i try to install Anaconda on my MacBook (M1) with Ventura 13.3.1 i receive the following error:
"This package is incompatible with this version of macOS."
I tried the arm64 installer and the x86 installer, both lead to the same error message.
I used Anaconda on the same MacBook just a few days ago, but after the update from Ventura 13.2.1 to 13.3 I couldn't open Jupyter Notebooks from within the Anaconda Navigator. First I thought that the problem might be caused by Anaconda, so I uninstalled it. However, now here I am, unable to install it again. I also did a complete reset of my MacBook, nothing changed.
Does anyone have the same issue or know how I can fix this?
Thanks a lot

Answer: I had the same problem as you, I installed on my account only instead of Macintosh HD and it worked like a breeze.
Title: I can't install Anaconda on a MacBook Pro M1 with Ventura 13.3.1
Tags: python,installation,anaconda,macos-ventura

Question: this is my first question here :)
When i try to install Anaconda on my MacBook (M1) with Ventura 13.3.1 i receive the following error:
"This package is incompatible with this version of macOS."
I tried the arm64 installer and the x86 installer, both lead to the same error message.
I used Anaconda on the same MacBook just a few days ago, but after the update from Ventura 13.2.1 to 13.3 I couldn't open Jupyter Notebooks from within the Anaconda Navigator. First I thought that the problem might be caused by Anaconda, so I uninstalled it. However, now here I am, unable to install it again. I also did a complete reset of my MacBook, nothing changed.
Does anyone have the same issue or know how I can fix this?
Thanks a lot

Answer: Try selecting a different partition/folder for the installation.
Title: Is there any possibility to run all test suites and generate report?
Tags: python,squish

Question: Is there any command with squish runner or any option available in squish to run all the test suites in single run and generate report of complete project?

Answer: Nothing that is built into squishrunner, but if you specify the same "html" report folder for each execution, the suite reports will be stored in it and linked in the index.html page, to view them conveniently.
Executing one after the other via a shell script or batch file is simple, just add each squishrunner call into such a file.
As for reporting, please look into Squish Test Center, for which you should have 2 licenses per each Squish GUI Tester Tester license. This is because the HTML report format is deprecated and lacks features.
Title: Return true if a graph is a disjoint union of paths
Tags: python,networkx,graph-theory

Question: I am trying to make an if statement with the condidion that a graph is a disjoint union of paths using networkx but I'm not sure how to go about it
I tried deleting an edge (u,v) in each node iteration then checking if the first node in the graph to the first node without the edge (node1 to u) is a path then check the same thing from the second node to the last node (v to node 2).
            for i in G1.nodes:
                node_count = node_count+1
                nodes.append(i)
                p = nx.neighbors(G1, i)
                for o in p:
                    neigh.append(o)
                    
                for j in neigh:
                    G1.remove_edge(i, j)
            no = len(nodes)
            for k in range(0, node_count-1):
                path.append(nodes[k])
            if nx.is_path(G1, path):
                for l in range(node_count-1, no-1):
                    path2.append(nodes[l])
                if nx.is_path(G1, path2):
                    return True

Answer: A finite graph G is the disjoint union of paths iff every vertex has degree <= 2 and there are no cycles.
Observations:

|E| < |V|
There can be any number of isolated points (degree zero)

Procedure:

return true if all nodes have degree 0 or 1
return false if any node has degree > 2
return false if any node has degree 2 and there are no nodes of degree 1
return false if the graph has any cycles.
return true

Cycle detection is linear in this graph because |E| < |V|. One of several fast ways to find cycles is repeatedly using BFS (once per connected component).
Title: Is __init__ a class attribute?
Tags: python

Question: Let's consider the example below:
class Student:
    school = "stackoverflow"
    
    def __init__(self, name, course):
        self.name = name
        self.course = course

I understand that __init__ is the constructor. I understand that it is a method (since it's a function within a class). Or, to be more precise, as I've come to understand from the numerous tutorials - an attribute that holds a method. But is __init__ considered a class attribute in this example? Would that be correct to say? Or is it only school that is considered a class attribute?
Edit: I do understand that considering the essence of my question, any method may replace __init __(). But I would like to get an understanding using __init __() as the example at hand.

Answer: Yes, init is an instance method of the Student class, not a class attribute. In your example, the class attribute is school, which is a static property that is shared by all instances of the class.
The init method is a special method that is called when an instance of the class is created. It is used to initialize the attributes of the instance with the values passed to it as arguments. Since it is a method that operates on an instance of the class, it is not considered a class attribute.
To clarify, a class attribute is a property of the class itself, not its instances. It is shared by all instances of the class and can be accessed using the class name. For example, Student.school would return the value of the school class attribute.
In summary, init is an instance method used to initialize the attributes of instances of the class, while school is a class attribute that is shared by all instances of the class.
edit
I understand your confusion. Although the init method is called for each instance of the class, it is not considered a class attribute because it belongs to each instance of the class, not to the class itself.
In other words, the init method is defined in the class definition, but it is not a class attribute. Instead, it is a special method that is called on each instance of the class to initialize its attributes. The self parameter in the init method refers to the specific instance of the class that is being initialized.
To summarize, a class attribute is a property or method that belongs to the class itself and is shared by all instances of the class. The init method, on the other hand, is a special method that is called on each instance of the class to initialize its attributes, and therefore it is not a class attribute.
Title: How do I write sound files in python using wave?
Tags: python,wave,winsound

Question: I am trying to make a text-to-sound-file converter with python, it reads the binary data of an input and writes a sound file that matches the data.
1 is a higher note (440 hz) and 0 is a lower note(330 hz). I have tried so many different things and my code is all over the place. Can someone please help me fix it?
import os
import numpy as np
import winsound
import wave
import struct
import math
import random
# Parameters
sampleRate = 44100    # samples per second
duration = 1           # sample duration (seconds)
frequency = 440.0       # sound frequency (Hz)
print(os.getcwd())
obj = wave.open('sound.wav','w')
obj.setnchannels(1) # mono
obj.setsampwidth(2)
obj.setframerate(sampleRate)
string=input("Enter text to turn into sound file")
binary=' '.join(format(ord(i), 'b') for i in string)
print(binary)
for i in binary:
    if i=="1":
        winsound.Beep(440, 500)
        data=struct.pack('<h', 440)
        obj.writeframesraw( data )
    elif i=="0":
        winsound.Beep(330, 500)
        data=struct.pack('<h', 330)
        obj.writeframesraw( data )
obj.close()

Answer: You may use the 'wb' mode when you try to write
(in line 13)
Title: Continue iteration to the next json record after encountering ijson.common.IncompleteJSONError
Tags: python,json,npm,ijson

Question: I have a large json file (about 11,600 records) and I am trying to parse it using ijson. However, the for loop breaks because of one faulty json record. Is there a way to continue the iteration by skipping that record and moving on using ijson or any other python library? Here's the code snippet.
try:
      for row in ijson.items(json_file, 'rows.item'):
        data = row
        try:
            super_df=dependency_latest_version(data, version)
        except Exception as e:
            print(e)
except ijson.common.IncompleteJSONError:
    traceback.print_exc()

This generates the following error:
for row in ijson.items(json_file, 'rows.item'):
ijson.common.IncompleteJSONError: parse error: after array element, I expect ',' or ']'
          dmeFilename":"README.md"}}":{"integrity":"sha512-204Fg2wwe1Q
                     (right here) ------^

I tried iterating through the json file line by line and then using json.loads(line) but it didn't help since the entire json file was being read as a single line. Are there any other alternatives? Thank you.

Answer: I faced a similar situation once. Later I had to fix the document before parsing as @Rodrigo mentioned. It is pointless to try to fix the library since the library is doing exactly what it is supposed to do and it should not be able to parse the document unless it is a proper json document.
The way I fixed it for my case is that I tried to look for a pattern where the supposed json document has improper formatting and wrote a script to fix those. After doing this preprocessing, it becomes a proper json document and at this point it can be parsed with ijson or any similar library.
Title: Spotify API Only Returning Some of Users playlists
Tags: python,python-requests,request,spotify,endpoint

Question: I have some Python code that is meant to return the user's playlists. However, it only returns some of my playlists. The documentation says it should return all playlists in my library (all the ones I've created and the ones I've liked). Also, when I use the 'try it' feature on Spotify's website, it works perfectly fine. I've made sure I have the right scopes, the user ID is correct and even made sure my playlists are public (not that it should matter). Here is my code calling the function:
def getUsersPlaylists(self, user_id, offset): 
        headers = {
            "Authorization" : f"Bearer {self.access_token}"
        }
        endpoint = 'https://api.spotify.com/v1/users/' + user_id + '/playlists?offset=' + str(offset) + '&limit=50'
        return requests.get(endpoint, headers=headers).json()

and I've also tried this similar function that just uses the user that logged in with OAuth 2.0 rather than providing the user ID:
def getUsersPlaylists(self, offset): 
        headers = {
            "Authorization" : f"Bearer {self.access_token}"
        }
        endpoint = 'https://api.spotify.com/v1/me/playlists?offset=' + str(offset) + '&limit=50'
        return requests.get(endpoint, headers=headers).json()

What would be causing this? As I said before, it shows some playlists but not others and I cant see a pattern in which playlists it does and doesn't show. Thanks to anyone who can help :)

Answer: I just found my problem. I was using the user-read-private and playlist-read-collaborative scopes, but I also needed the playlist-read-private scope. Using this scope fixed my problem.
Title: moov atom not found (Extracting unique faces from youtube video)
Tags: python,numpy,opencv,ffmpeg,urllib

Question: I got the error below
Saved 0 unique faces
[mov,mp4,m4a,3gp,3g2,mj2 @ 0000024f505224c0] moov atom not found

Trying to extract unique faces from a YouTube video with the code below which is designed to download the YouTube video and extract unique faces into a folder named faces. I got an empty video and folder. Please do check the Python code below
import os
import urllib.request
import cv2
import face_recognition
import numpy as np

# Step 1: Download the YouTube video
video_url = "https://www.youtube.com/watch?v=JriaiYZZhbY&t=4s"
urllib.request.urlretrieve(video_url, "video.mp4")

# Step 2: Extract frames from the video
cap = cv2.VideoCapture("video.mp4")
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
frames = []
for i in range(frame_count):
    cap.set(cv2.CAP_PROP_POS_FRAMES, i)
    ret, frame = cap.read()
    if ret:
        frames.append(frame)
cap.release()

# Step 3: Detect faces in the frames
detected_faces = []
for i, frame in enumerate(frames):
    face_locations = face_recognition.face_locations(frame)
    for j, location in enumerate(face_locations):
        top, right, bottom, left = location
        face_image = frame[top:bottom, left:right]
        cv2.imwrite(f"detected_{i}_{j}.jpg", face_image)
        detected_faces.append(face_image)

# Step 4: Save the faces as separate images
if not os.path.exists("faces"):
    os.makedirs("faces")
known_faces = []
for i in range(len(detected_faces)):
    face_image = detected_faces[i]
    face_encoding = face_recognition.face_encodings(face_image)[0]
    known_faces.append(face_encoding)
    cv2.imwrite(f"faces/face_{i}.jpg", face_image)
print("Saved", len(known_faces), "unique faces")

Answer: Your file is not an mp4/mov file.
It's a web page, not a video file.
You can't just download a YouTube web page like that and expect to receive a video file.
Title: Failed to initialize conda directories when trying to install Anaconda 3
Tags: python,anaconda3

Question: I'm trying to install Anaconda 3 2023.03 and can't get anywhere with it. I get an error that says "Failed to initialize conda directories". I tried installing as an administrator. If I do this the install goes through but the system doesn't recognise the install anywhere. If I type "where conda" into the terminal it says it doesn't exist. I had a previous installation of python 3.11 which I deleted every trace I could find (I'm not competent to go fishing around the registry without clear instructions here) but still no joy.
I've only just started learning Python and need to install Anaconda to follow along with my Udemy course, so please dumb it down for me.

Answer: When you install Anaconda, choose to install it for "All Users" instead of "Just for Me"
This should solve the issue.
Title: How can I change the Main Window with a button and run the events from another window Pyside6
Tags: python,pyqt,pyside6

Question: I want to change the Main Window through a button click, but when the main window change´s the event´s dont run.
The both window´s are already been created in qt Designer
here is the code:
from PySide6.QtWidgets import *
from PySide6.QtWidgets import QPushButton
from PySide6.QtCore import  QObject
from PySide6.QtCore import  QCoreApplication
from PySide6.QtCore import *
from PySide6.QtGui import *

from PySide6 import QtWidgets, QtCore, QtGui
from ui_app import Ui_MainWindow
from ui_menu import Ui_MainWindow1
import sys

class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setupUi(self)
        self.setWindowTitle("Login")

        self.pushButton.clicked.connect(self.trocarpag)

    def trocarpag(self):
        if self.textuser.toPlainText() == "" :
            msg = QMessageBox()
            msg.setIcon(QMessageBox.Warning)
            msg.setText("Preencha o Username!!!")
            msg.exec()
        elif self.textpass.toPlainText() == "" :
            msg = QMessageBox()
            msg.setIcon(QMessageBox.Warning)
            msg.setText("Preencha a Password!!!")
            msg.exec()
        else:
            ###Change the Main Window####`
             self.ui = Ui_MainWindow1()
             self.ui.setupUi(self)
            
if __name__ =="__main__":
    app = QApplication()
    window = MainWindow()
    window.show()
    app.exec()

Answer: A line of code self.ui = Ui_MainWindow1() creates just a member variable in your object window of class MainWindow. There are no code that would replace widgets in current window or show new window instead.
Title: spacy python package no longer runs
Tags: python,spacy

Question: Running python 3.11.3 on macos, Intel.
I had spacy working fine. I then decided to try adding gpu support with: pip install -U 'spacy[cuda113]' but started getting errors.
I uninstalled with pip uninstall 'spacy[cuda113]' and then reinstalled spacy with just pip install spacy.
However, I'm still getting the same errors when running a simple script with just
import spacy in it:
Traceback (most recent call last):
  File "/Users/steve/workshop/python/blah.py", line 4, in <module>
    import spacy
  File "/usr/local/lib/python3.11/site-packages/spacy/__init__.py", line 14, in <module>
    from . import pipeline  # noqa: F401
    ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/spacy/pipeline/__init__.py", line 1, in <module>
    from .attributeruler import AttributeRuler
  File "/usr/local/lib/python3.11/site-packages/spacy/pipeline/attributeruler.py", line 6, in <module>
    from .pipe import Pipe
  File "spacy/pipeline/pipe.pyx", line 1, in init spacy.pipeline.pipe
  File "spacy/vocab.pyx", line 1, in init spacy.vocab
  File "/usr/local/lib/python3.11/site-packages/spacy/tokens/__init__.py", line 1, in <module>
    from .doc import Doc
  File "spacy/tokens/doc.pyx", line 36, in init spacy.tokens.doc
  File "/usr/local/lib/python3.11/site-packages/spacy/schemas.py", line 158, in <module>
    class TokenPatternString(BaseModel):
  File "/usr/local/lib/python3.11/site-packages/pydantic/main.py", line 369, in __new__
    cls.__signature__ = ClassAttribute('__signature__', generate_model_signature(cls.__init__, fields, config))
                                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pydantic/utils.py", line 231, in generate_model_signature
    merged_params[param_name] = Parameter(
                                ^^^^^^^^^^
  File "/usr/local/Cellar/python@3.11/3.11.3/Frameworks/Python.framework/Versions/3.11/lib/python3.11/inspect.py", line 2722, in __init__
    raise ValueError('{!r} is not a valid parameter name'.format(name))
ValueError: 'in' is not a valid parameter name

Answer: I uninstalled pydantic then installed it again.I got it fixed in pydantic 1.10.
Title: pdf2image fails in docker container
Tags: python,docker,docker-compose,poppler,pdf2image

Question: I have a Python project running in a docker container, but I can't get convert_from_path to work (from pdf2image library). It works locally on my Windows PC, but not in the linux-based docker container.
The error I get each time is Unable to get page count. Is poppler installed and in PATH?
Relevant parts of my code look like this
from pdf2image import convert_from_path
import os
from sys import exit

def my_function(file_source_path):
    try:
        pages = convert_from_path(file_source_path, 600, poppler_path=os.environ.get('POPPLER_PATH'))
    except Exception as e:
        print('Fail 1')
        print(e)
    try:
        pages = convert_from_path(file_source_path, 600)
    except Exception as e:
        print('Fail 2')
        print(e)
    try:
        pages = convert_from_path(file_source_path, 600, poppler_path=r'\usr\local\bin')
    except Exception as e:
        print('Fail 3')
        print(e)
        print(os.environ)
        exit('Exiting script')

In attempt 1 I try to reference the original file saved on windows. Basically the path refers to '/code/poppler' which is a binded mount referring to
[snippet from docker-compose.yml]
- type: bind
  source: "C:/Program Files/poppler-0.68.0/bin"
  target: /code/poppler

In attempt 2 I just try to leave the path empty. In attempt 3 I tried something I found that worked from some other users locally.
Relevant parts of my Dockerfile look like this
FROM python:3.10

WORKDIR /code

# install poppler
RUN apt-get update
RUN apt-get install poppler-utils -y

COPY ./requirements.txt ./
RUN pip install --upgrade pip
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

CMD ["python", "./app.py"]

Answer: So the issue was that my Docker image was not refreshing correctly and after nuking the build-cache and trying again the middle option worked combined with the above Dockerfile.
So a combination of RUN apt-get install poppler-utils -y in the Dockerfile + not referencing the path in the code pages = convert_from_path(file_source_path, 600) will work, as it will find the PATH automatically when installing poppler-utils.
The binded mount can also be removed from docker-compose.yml and from the .env file.
Title: python --version shows old version
Tags: python,python-3.x,installation,path,version

Question: I'm using Windows, and in my path I have:
C:\Users\Ant_P\AppData\Local\Programs\Python\Python311\
C:\Users\Ant_P\AppData\Local\Programs\Python\Python311\Scripts\

When I type python3 --version, it still shows me 3.10.8.
I deleted the old python folder, but it still exists. I've tried looking online, but I'm not seeing how to fix this. I thought I just had to set my path, but apparently there is something else going on. Any clues?

Answer: If you are on Windows and you have installed Python 3.11, you should type python --version instead of python3 --version.
Alternatively, you can try running py --version to see which version of Python is currently being used by your system.
If you have set your PATH correctly and it still shows the wrong version of Python, you can try restarting your command prompt or shell to see if the changes take effect.
Title: How to create db migrations for local tortoise project?
Tags: python,fastapi,tortoise-orm

Question: I have a FastAPI + tortose projects and I want to run the project locally with database postgres://lom:lom@localhost:5432/lom (database is created)
My code
# lom/app.py
class App:
    storage: S3Storage

    def __init__(self):
        self.config = Config(_env_file=".env", _env_file_encoding="utf-8")
        self.__setup_sentry()
        ...
    def create_app(self, loop: asyncio.AbstractEventLoop) -> FastAPI:
        app = FastAPI()
        register_tortoise(
            app,
            modules={
                "models": [
                    "lom.core",
                    "aerich.models",
                ]
            }

I want to apply current migrations and create new migrations
I am trying
aerich init -t <Don't understand path>

What aerich command should I run and which parameters should I use?
├── lom
│   ├── app.py
│   ├── config.py
│   ├── core
│   │   ├── city.py
│   │   ├── company.py
├── 
├── migrations
│   ├── 001_main.sql
│   ├── 002_cities.sql
│   ├── 003_cities_declination.sql

Answer: Initialize aerich by running the following command in the root directory of your project:
aerich init -t tortoise.Tortoise -p lom.models

This will create an aerich.ini configuration file and a migrations directory in the root directory of your project.

Configure the aerich.ini file to match your database connection settings. The default settings are for a SQLite database, so you will need to modify the [tortoise] section to use PostgreSQL instead.

[tortoise]
# database_url = sqlite://db.sqlite3
database = lom
host = localhost
port = 5432
user = lom
password = lom
modules = ['lom.models']

Run the following command to create a new migration:
aerich migrate

This will create a new migration file in the migrations directory.

Run the following command to apply the migrations:
aerich upgrade

This will apply all pending migrations to your database and the problem you're facing will be solved. Let me know if this helps