Myashka/SO-Python_QA-filtered-2023-tanh_score-after_2023_02 huggingface dataset:
Title: Python script running in pycharm but not in cmd
Tags: python,cmd,pycharm

Question: I use Pycharm with python version 3.9 and scripts run just fine,
but when I write python in cmd, it opens Microsoft Store on the page of Python3.10.
Do I need to give cmd some kind of premission to python? How do I do that?
I searched online, but I couldn't find a way to make cmd run python without downloading it again.
edit: I want to use cmd as a terminal and to be able to run scripts through it.
In order to use cmd as python terminal I write python but cmd can't find it.
This is the location/path of python:
C:\Users\ofira\AppData\Local\Programs\Python\Python39\python.exe

Answer: The solution is easy. When the microsoft store open's type python3.9 in the search tab. Download it and everything will work like a charm. Downloading it form the store will automatically add the path in the system environment variable.
Title: How to access Flask app context from test_client using pytest?
Tags: python,flask,testing,pytest

Question: I feel like I don't fully understand app_context and its usage. When I turn my app into a test_client, I can't seem to access the fields that are within the apps context, which means I can't test in my unit tests whether the element was successfully added to the state of the app. How do I access the app context from a test_client, as shown below?
Here are minimal sections of the relevant code:
App:
def make_service():
    service = Flask("my_service")
    my_content: Dict[str, str] = {}
    with service.app_context():
        service.my_content = my_content
    return service

app = make_service()


@app.route("/test/")
def example_func():
    app.my_content["test"] = "testing entry"

Pytest fixture:
@pytest.fixture(name="test_app")
def test_client():
    with app.test_client() as testing_client:
        with app.app_context():
            yield testing_client

Pytest test function:
def test_example_func(test_app):
    response = test_app.get(
        "/test/",
    )
    assert test_app.my_content["test"] == "testing_entry"

Error:
AttributeError: 'FlaskClient' object has no attribute 'my_content'

Answer: The solution was simpler than expected. I had completely overlooked the fact that test_client wraps the app and stores it as a variable names application.
The solution is to change test_app.my_content["test"] == "testing_entry" to test_app.application.my_content["test"] == "testing_entry"
Title: django.db.utils.NotSupportedError: MySQL 8 or later is required (found 5.7.33). in Django
Tags: python,django

Question: I have this error when performing migrations to my file command
I tried this command
python manage.py makemigrations

but Error insist
django.db.utils.NotSupportedError: MySQL 8 or later is required (found 5.7.33).

Is there anyway to perform migration? or should I downgrade my python version to v.9?
python version
python v.3.10

Answer: You can install an older version of django with
pip install Django==3.2.19 command
Title: Displaying when a button is pressed
Tags: python,raspberry-pi

Question: I have a RasberryPi3b and i am trying to simulate button presses on this device i have.
It is a START/STOP button system where STOP is always LOW, and START is LOW when pressed, as i understand.
I have some code that Displays when the start button is pressed, but stop button code doesn't print.
Below is the setup i have;
START & STOP - Connected to Ground;
START - Connected to GPIO14;
STOP - Connected to GPIO15;
And here is the code


import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
start_pins = [14]
stop_pins = [15]

for pin in start_pins:
  GPIO.setup(pin,GPIO.IN,pull_up_down=GPIO.PUD_UP)
  
for pin in stop_pins:
  GPIO.setup(pin,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
  
while True:
  for pin in start_pins:
    if GPIO.input(pin) == GPIO.LOW:
      print("Pin", pin," is low")
      
   for pin in stop_pins:
    if GPIO.input(pin) == GPIO.HIGH:
      print("Pin", pin," is high")
  



Once START is pressed it will also print "Pin 14 is low" multiple times, slightly confused why it prints so much.

Answer: Ahh nevermind i changed GPIO.PUD_DOWN back to UP for stop pins.
And changed it to != for stop pins not ==
Title: Using WebDAV to list files on NextCloud server results in method not supported
Tags: python,webdav,nextcloud

Question: I'm trying to list files using webdab but I'm having issues.  I can create directories and put files just fine but not list a directory or pull a file. I'm seeing the error, "Method not supported".
from webdav3.client import Client
options = {
  'webdav_hostname': "https://___________.com/remote.php/dav/files/",
  'webdav_login': "user_name",
  'webdav_password': "password"
}
client = Client(options)
print(client.list('/'))

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/<user>/.local/lib/python3.10/site-packages/webdav3/client.py", line 67, in _wrapper
    res = fn(self, *args, **kw)
  File "/home/<user>/.local/lib/python3.10/site-packages/webdav3/client.py", line 264, in list
    response = self.execute_request(action='list', path=directory_urn.quote())
  File "/home/<user>/.local/lib/python3.10/site-packages/webdav3/client.py", line 228, in execute_request
    raise MethodNotSupported(name=action, server=self.webdav.hostname)
webdav3.exceptions.MethodNotSupported: Method 'list' not supported for https://________.com/remote.php/dav/files

Answer: Another alternative is to try os.scandir(), which is sometimes a sufficient method for listing files on the Nextcloud server, and is available in Python.
Title: Using WebDAV to list files on NextCloud server results in method not supported
Tags: python,webdav,nextcloud

Question: I'm trying to list files using webdab but I'm having issues.  I can create directories and put files just fine but not list a directory or pull a file. I'm seeing the error, "Method not supported".
from webdav3.client import Client
options = {
  'webdav_hostname': "https://___________.com/remote.php/dav/files/",
  'webdav_login': "user_name",
  'webdav_password': "password"
}
client = Client(options)
print(client.list('/'))

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/<user>/.local/lib/python3.10/site-packages/webdav3/client.py", line 67, in _wrapper
    res = fn(self, *args, **kw)
  File "/home/<user>/.local/lib/python3.10/site-packages/webdav3/client.py", line 264, in list
    response = self.execute_request(action='list', path=directory_urn.quote())
  File "/home/<user>/.local/lib/python3.10/site-packages/webdav3/client.py", line 228, in execute_request
    raise MethodNotSupported(name=action, server=self.webdav.hostname)
webdav3.exceptions.MethodNotSupported: Method 'list' not supported for https://________.com/remote.php/dav/files

Answer: Despite already providing a username in the webdav_login, a username is still needed in the call to listdir. For example, to get a list of files in the root directory of the user "user_name", client.list('/user_name/').
Title: Google Colab ImportError: numpy.core.multiarray failed to import
Tags: python,google-colaboratory,pandas-ta

Question: I have the following colab notebook that has always worked for me, however now I am receiving the following error:
"ImportError: numpy.core.multiarray failed to import"
How can I fix this problem? I have already tried re-installing pands to no avail.
Here is my code:
pip install pandas_ta

!wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz
!tar -xzvf ta-lib-0.4.0-src.tar.gz
%cd ta-lib
!./configure --prefix=/usr
!make
!make install
!pip install Ta-lib

from google.colab import files
import os
import pandas as pd
import pandas_ta as ta


def TAfunction(data):
  stock_df = pd.read_csv(data)
  CustomStrategy = ta.Strategy(
    name="RSI Strat",
    description = "RSI",
    ta=[
        {"kind":"rsi"},
        {"kind":"bbands", "length": 5},
        {"kind":"macd", "fast": 8, "slow":21},
        {"kind":"sma", "length": 20},
        {"kind":"sma", "length": 50},]
)
  stock_df.ta.strategy(CustomStrategy)
  stock_df.to_csv(data, encoding = 'utf-8-sig', index=False)

test_dir = '/content/drive/MyDrive/Colab Notebooks/Stocks CSVs/IndCSVs/'

for file in os.listdir(test_dir):
  if file.endswith(".csv"):
    TAfunction(test_dir + file)

Answer: I've been having this exact same error while running a so-vits-svc 4.0 model in Colab. All I had to do was:

Click on "Runtime" in the top menu
Click on "Restart runtime"
Run the cell again
Title: Efficient Look-up for permutations of the start of lists (Python)
Tags: python,data-structures,permutation,lookup

Question: Problem:
I need to retrieve integer lists from a container while having only a permutation of the lists' start.
Thus, I'm looking for a data-structure/ algorithm that gets an arbitrary length input and returns the corresponding list or an empty one if no matches are found. This needs to be very quick as its called hundreds of times a second on a very large container.
Example case:
{
[1, 15, 30, 20, 45],
[10, 1, 301, 45],
[15, 2, 255, 586, 950, 10124]
}

Input 1:. The Data-structure/algorithm input is [30, 1]
My expected Return: []
→ Empty List, since no matching len(Input)=2 permutation of any contained list is found.
All possible length 2 permutations of the contained list starts are [1, 15], [15, 1], [10, 1], [1, 10], [15, 2], [2, 15] thus not [30, 1].
Input 2: The Data-structure/algorithm input is [30, 1, 15]
Expected Return: [1, 15, 30, 20, 45]
→ [30, 1, 15] can be permuted into [1, 15, 30] which corresponds to the start of the returned list
Supplementary Problem information:
→ Indexes in lists [...] are unique integers (list don't contain the same number twice)
→ Lists have arbitrary but different lengths > 1, up to a few hundred integers.
→ The input length is variable and not guaranteed to exist in the container
→ The container is expected to contain up to several millions of lists
→ The Integers in the lists are actually unbounded, but are expected to be < 1 million
Naive, working but way too slow solution:
Input = sort( Input )
for element in container:
  if ( sort( element[:len(Input)] ) ) == Input ): return ( element )
# cut first Input-length elements and compare sorted
return ( [] )

→ This is infeasible, since the container contains millions of elements and the search is supposed to be called a few hundred times per seconds.

Answer: Let the "set hash" of a list be a hash of the list elements that doesn't depend on the order.  For example set_hash(L) = hash(sort(L)) works, as does set_hash(L) = sum([hash(x) for x in L]).
Now, for every list in your data, replace each element with the set hash of the prefix ending at that element.  For example [10, 1, 301, 45] would become [set_hash([10]), set_hash([10, 1]), set_hash([10, 1, 301]), set_hash([10, 1, 301, 45])].
Then, for each distinct set_hash make a list of the indexes at which it occurs.  You would map set_hash([10,1]) -> [1], for example, because that hash only occurs in the 2nd data item (and we're counting from 0).
Now, to query for data items matching L, you just look up the data indexes for set_hash(L), and check each of the corresponding data items to see if it really does match.
Title: Good Patterns to pass down data in deep OOP composition?
Tags: python,oop,design-patterns

Question: This is a general open question about best practices and scalability in Python using OOP.
Overtime, I have been using in several projects Class inheritance and composition. This pattern has helped me abstracting and encapsulating a lot of the code. However, as the codebase increases, I have been trapped in a recurrent pattern where I have a "master" Class which "orchestrates" another classes by composition, which in turn also have another composed classes to orchestrate and so on. This creates a "deep" tree structure of dependant classes.
The problem is that if I want to pass down arguments to many of the Class components from the master class, I have to pass it down on everytime I init the class again and again, writing redundant code. For example (a simple example just to illustrate the kindof composition but imagine a much deeper structure than this):
 class UploaderExecutor:
     def __init__(self, mediatype: Literal['video', 'image'], channel: Literal['online', 'offline'], ...):
     self.uploader = Uploader(mediatype, channel, ...)
     self.validator = Validator(mediatype, channel, ...)
     self.reporter = Reporter(mediatype, ...)
     ...

 class Uploader():
     def __init__(self, mediatype, channel, ...):
         self.configurator = Configurator(mediatype, channel...)
         self.file_parser = FileParser(mediatype, channel...)
         self.Notificator = Notificator(mediaype, channel...)
         ...

 class Validator():
     def __init__(self, mediatype, channel, ...):
         self.file_validator = FileValidator(mediatype, channel...)
         self.name_validator = NameValidator(channel...)
         ...

 class Reporter():
     def __init__(self, mediatype, channel, ...):
         self.slack_reporter = SlackReporter(mediatype, channel...)
         self.telegram_reporter = TelegramReporter(mediatype, channel...)
         ...

   #etc etc

I guess this pattern of passing down arguments to all the composed classes is not escalable as more and more cases are added. Also I do not want to rely in solutions like global variables, because I want to decide which data passing down to which composed classes (maybe not all of them would need for example mediatype). So my question is which kind of abstractions or design patterns are recommended in these cases?

Answer: I believe that following the O.O.P. principles, you can design this example starting from a class - let's say MediaFile. This can (ideally) encapsulate and completely hide the mediatype and channel literals and only expose methods such as upload(), validate() and report().
For different types of MediaFile objects, you can use polymorphism and implement these methods differently. If you have many combinations of, let's say mediatype + channel for creating the appropriate  MediaFile object, you can maybe use the factory pattern.
As a result, when adding a new type, you'll have to implement various methods for it and not touch code that is used by the other existing implementations. O.O.P. can help you decouple the code in this way.
Title: Python: chmod is NOT preventing file from being deleted
Tags: python,chmod

Question: I'm setting the mode on a file to try to prevent it being deletable, but nothing seems to work.
Example:
import os
from stat import S_IRUSR, S_IRGRP, S_IROTH

with tempfile.TemporaryDirectory() as local_dir:
    local_file = os.path.join(local_dir, 'a.txt')
    with open(local_file, 'wt') as f:
        f.writelines('some stuff')
    os.chmod(local_file, S_IRUSR|S_IRGRP|S_IROTH)
    print(oct(os.stat(local_file).st_mode)[-3:])  # prints '444' as expected
    os.remove(local_file)  # no exception
    print(os.path.isfile(local_file)) # prints False, the file has been deleted

Answer: Running chmod on a file cannot render a file non-deletable. There is no "deletable/not deletable" mode bit.
A user's permissions for a file have no effect on whether they can delete that file. To delete a file, a user needs write and execute permissions on the containing directory.
Title: cv2 destroyWindow() does not work in ros callback function
Tags: python,opencv,ros

Question: I am getting data from a 3d simulation, specifically the lidar and camera of a robot. I want to show the camera images with cv2 whenever the robot detects an object closer than a set threshold around it. However the images are displayed fine when an object is within the specified distance but when the robot stops detecting an object within the threshold the window is not destroyed and it is stuck displaying the last shown image. If i get in range of an object again the window still does not update with the new images.
#!/usr/bin/env python

import rospy
import cv2
from cv_bridge import CvBridge, CvBridgeError
from sensor_msgs.msg import LaserScan
from sensor_msgs.msg import Image

bridge = CvBridge()
threshold = 0.5
sub_camera = None
window_name = "Camera preview"

def scan_callback(scan):
    global sub_camera
    if(min(scan.ranges) < threshold):
        rospy.loginfo("Object detected")
        if(not sub_camera):
            sub_camera = rospy.Subscriber("/camera/rgb/image_raw", Image, image_callback)
    else:
        rospy.loginfo("No object detected")
        if(sub_camera):
            cv2.destroyWindow(window_name)
            sub_camera.unregister()
            sub_camera = None

def image_callback(img):
    try:
        cv_image = bridge.imgmsg_to_cv2(img, "bgr8") 
    except CvBridgeError as e:
        rospy.logerr("CvBridge Error: {0}".format(e))

    cv2.namedWindow(window_name)
    cv2.imshow(window_name, cv_image)
    cv2.waitKey(1)

rospy.init_node('camera_lidar_node', anonymous=False)
sub_lidar = rospy.Subscriber("/scan", LaserScan, scan_callback)

while not rospy.is_shutdown():
    rospy.spin()

Answer: waitKey() does GUI event processing.
You have to be in that call continuously or frequently. If you are not, the GUI freezes.
You should use the GUI facilities of ROS itself. Those are made for ROS.
Title: HTML pages are not rendering
Tags: python,html,forms,flask,wtforms

Question: I'm currently working on a 100 days of code project however I'm stuck. I'm unable to render the "Success.html" and "Denied.html" templates in my login route code. I'm including the python and html portions of the code. Any insight is appreciated. I think the problem is inside the login route code however I've hit a wall and don't know what to try at the moment.
import email_validator
from flask import Flask, render_template, request
from flask_wtf import FlaskForm
from flask_wtf.csrf import CSRFProtect
from wtforms.validators import Email, Length, DataRequired, ValidationError
from wtforms import StringField, PasswordField, SubmitField
from email_validator import validate_email

# create a flask instance
app = Flask(__name__)

# CSRF protection
app.secret_key = "123456"
#disabling CSRF Protection
# app.config['WTF_CSRF_ENABLED'] = False


# create a form class
class LoginForm(FlaskForm):
    def validate_email(self, field):
        # Custom email validation using email_validator
        try:
            validate_email(field.data)
        except Exception as e:
            raise ValidationError(str(e))

    email = StringField(label="Email", validators=[DataRequired(), Email()], render_kw={"size": 30})
    password = PasswordField(label="Password", validators=[DataRequired(), Length(min=8)], render_kw={"size": 30})
    submit = SubmitField(label="Log In")


# create a route decorator
@app.route('/', methods=["GET", "POST"])
def login():
    form = LoginForm()
    form.validate_on_submit()
    print(request.method)
    print(form.email.data)
    print(form.password.data)

    return render_template('login.html', form=form)


@app.route('/login', methods=["GET", "POST"])
def login_info():
    form = LoginForm()
    if form.validate_on_submit():
        if form.email.data == "admin@email.com" and form.password.data == "12345678":
            print(request.method)
            print(type(form.email.data))
            print(type(form.password.data))
            return render_template('success.html')
        else:
            return render_template('denied.html')
    else:
        return render_template('login.html', form=form)



if __name__ == "__main__":
    app.run(debug=True)

The HTML code for the login page is this
<!DOCTYPE HTML>

<html>
    <head>
        <title>Login</title>
    </head>
    <body>
        <div class="container">
        <h1>Login Page</h1>
        <form method="post" action="{{ url_for('login')}}" novalidate>
    {{form.csrf_token}}
    <p>
        {{form.email.label}} <br> {{form.email}}
        {% for err in form.email.errors %}
        <span style="color:red">{{ err }}</span>
        {% endfor %}
    </p>
    <p>
        {{form.password.label}} <br> {{form.password}}
        {% for err in form.password.errors %}
        <span style="color:red">{{ err  }}</span>
        {% endfor %}
    </p>
    {{form.submit}}
        </form>
        </div>
    </body>
</html>

Answer: You're submitting your form to the wrong endpoint. The form's action attribute references login, not login_info.
Title: Trying to visualize topics using pyldavis but it is giving drop error
Tags: python,topic-modeling,pyldavis

Question: I am trying to visualize topics using PyLDAVis but the following code is giving error. Not sure what the issue is.
import pyLDAvis.gensim_models

pyLDAvis.enable_notebook()
vis = pyLDAvis.gensim_models.prepare(lda_model, corpus, id2word)
pyLDAvis.show(vis)

File ~/PycharmProjects/KeyWordExtractor/venv/lib/python3.9/site-packages/pyLDAvis/_prepare.py:228, in _topic_info(topic_term_dists, topic_proportion, term_frequency, term_topic_freq, vocab, lambda_step, R, n_jobs)
    225 saliency = term_proportion * distinctiveness
    227 # Order the terms for the "default" view by decreasing saliency:
--> 228 default_term_info  = pd.DataFrame({'saliency': saliency, 'Term': vocab, \
    229                                    'Freq': term_frequency, 'Total': term_frequency, \
    230                                    'Category': 'Default'}). \
    231    sort_values(by='saliency', ascending=False). \
    232    head(R).drop('saliency', 1)
    233 # Rounding Freq and Total to integer values to match LDAvis code:
    234 default_term_info['Freq'] = np.floor(default_term_info['Freq'])

TypeError: drop() takes from 1 to 2 positional arguments but 3 were given

Answer: I'm getting this error too with pyLDAvis 3.4.0, pandas 2.0.0, python 3.11.3. I only started getting this error when I updated to python 3.11.
Title: Pyspark how to add row number in dataframe without changing the order?
Tags: python,dataframe,apache-spark,pyspark,apache-spark-sql

Question: I want to add a column with row number for the below dataframe, but keep the original order.
The existing dataframe:
+-—-+
|val|
+-—-+
|1.0|
+-—-+
|0.0|
+-—-+
|0.0|
+-—-+
|1.0|
+-—-+
|0.0|
+-—-+

My expected output:
+-—-+-—-+
|idx|val|
+-—-+-—-+
|  1|1.0|
+-—-+-—-+
|  2|0.0|
+-—-+-—-+
|  3|0.0|
+-—-+-—-+
|  4|1.0|
+-—-+-—-+
|  5|0.0|
+-—-+-—-+

I had tried many codes like the below:
from pyspark.sql.functions import row_number,lit
from pyspark.sql.window import Window
w = Window().orderBy(lit('A'))
df = df.withColumn("row_num", row_number().over(w))

Window.partitionBy("xxx").orderBy("yyy")

But the above code just only groupby the value and set index, which will make my df not in order.
Can we just add one column without changing the order?

Answer: There's no such thing as order in Apache Spark, it is a distributed system where data is divided into smaller chunks called partitions, each operation will be applied to these partitions, the creation of partitions is random, so you will not be able to preserve order unless you specified in your orderBy() clause, so if you need to keep order you need to specify which column will be used to keep order.
Title: Gradient using PIL is just one solid color
Tags: python,image,python-imaging-library,gradient

Question: I made a little script to create a gradient using python and save it as an image. Here is the code:
from PIL import Image, ImageDraw

# Define the image size and color gradient
width, height = 500, 500
color_start = (255, 0, 0)  # red
color_end = (0, 0, 255)  # blue

# Create a new image with the given size
image = Image.new("RGB", (width, height))

# Draw a gradient background on the image
draw = ImageDraw.Draw(image)
for y in range(height):
    color = tuple(int(color_start[j] + (color_end[j] - color_start[j]) * y) for j in range(3))
    draw.line((0, y, width, y), fill=color)

# Save the image to a PNG file
image.save("gradient.png")

However, instead of having a gradient I have a solid color (blue) and one tiny line of red at the top. Is there a way to fix that?

Answer: That's because you need to devide by height : color = tuple(int(color_start[j] + (color_end[j] - color_start[j]) * y / height) for j in range(3)). Hope that helps
Title: What is the difference between manager.Pool and Pool in python multiprocessing?
Tags: python,multiprocessing

Question: Say I want to share a dictionary between processes. If I have defined a manager, what is the difference between instantiating a pool using manager.Pool() and multiprocessing.Pool()?
Ex: What is the difference between the two with statements in main_1 and main_2?
import multiprocessing as mp
import time
from random import random


def func(d):
    pid = mp.current_process().pid
    time.sleep(3 * random())
    d[pid] = 'I added this'


def main_1():
    print('Start of main 1')
    with mp.Manager() as manager:
        d = manager.dict()
        with manager.Pool() as pool:  # using manager
            pool.map(func, [d] * 4)

        print(d)


def main_2():
    print('Start of main 2')
    manager = mp.Manager()
    try:
        d = manager.dict()
        with mp.Pool() as pool:  # using multiprocessing
            pool.map(func, [d] * 4)

        print(d)
    finally:
        manager.shutdown()


if __name__ == '__main__':
    main_1()
    main_2()

More generally
Are all processes started after a Manager exists in the scope automatically served by it?

Answer: Both manager.Pool and multiprocessing.Pool in Python are used for creating a pool of worker processes to execute tasks in parallel. However, there are some differences between them.
multiprocessing.Pool is a built-in class provided by the Python multiprocessing module. It creates a pool of worker processes, where each process executes tasks independently. The tasks can be submitted to the pool as functions or methods to be executed in parallel.
On the other hand, manager.Pool is a class provided by the Python multiprocessing.managers module. It also creates a pool of worker processes like multiprocessing.Pool. However, manager.Pool is used for creating a pool of processes that can be shared among multiple Python processes. This means that the processes in the pool can be accessed and used by multiple Python processes, making it a good choice for distributed computing.
Title: How do you add an "or" in a python list?
Tags: python,list,boolean-logic

Question: Fairly new to python programming.
I'm writing a program for learning about US Presidents, the code works, but it is very rigid, I don't know how to add exceptions if the answer is wrong, but not technically wrong.
i.e. If the question is Who is the 32nd President?  The user should be able to answer Franklin D. Roosevelt, or, Franklin Roosevelt, or FDR or just Roosevelt etc...  The way my code is setup only allows for one answer, is there a way to add exceptions to where the user can get the answer right without having to answer exactly?
import random
def main():
    presidents = {"first":"George Washington", "second":"John Adams","third":"Thomas Jefferson",
                  "fourth":"James Madison", "fifth":"James Monroe", "sixth":"John Quincy Adams",
                  "seventh":"Andrew Jackson", "eighth":"Martin Van Buren", "ninth":"William Henry Harrison",
                  "tenth":"John Tyler","eleventh":"James K. Polk", "twelth":"Zachary Taylor",}
    wrong = []
    print("President Test \n")
    incorrect_answers = False

    while len(presidents)>0:
        pick=random.choice(list(presidents.keys()))
        correct_answer=presidents.get(pick)
        print("Who was the",pick,"President of the United States?"
        answer=input("Your answer?: ")

        if answer.lower()==correct_answer.lower():
            print("That's Correct!\n")
            del presidents[pick]
        else:
            print("That's Incorrect.")
            print("The correct answer is", correct_answer)
            wrong.append(pick)
            incorrect_answers = True

    Print("You missed", len(wrong), "Presidents.\n")
    
    if incorrect_answers:
        print("Here are the ones that you may want to brush up on:/n")
        for each in wrong:
            print(each)
    else:
        print("Perfect!")

main()
   

I was hoping that if I simply added an "or" statement in the list code that it would magically work, but I see that it doesn't.
"eleventh":"James K. Polk" or "James Polk"
Also hoping that it would be able to use more than 2 options,
"thirty fifth":"John F. Kennedy" or "John Kennedy" or "JFK"
Is there a way to do this with a list?
Thanks

Answer: You can't add "or" to a list because a list is data and "or" is logic. You can solve your problem by changing the data structure, though, and with a small change to the logic of the code.
One way to do this is to store a list of correct answers for each key in the dictionary rather than just a single string. Then you can use the in operator to check if the entered answer is correct rather than == for strict equality.
If you want to allow case-insensitive comparison, then you will need to write a manual loop rather than using the in operator.
Title: How do I pass in a DependencyTicket for an output port? Can't figure out what type it's expecting. (pydrake)
Tags: python,drake

Question: I'm working through some basic tutorials and I've come across an issue. I have a simple LeafSystem connected to a PidController, and I want to make my own output port that sends the state to the controller (I know I can use a DeclareStateOutputPort to get a state output automatically, but I'd like to learn more about how things work and maybe change the outputs in customized ways). When I try to wire things up like this, I get an algebraic loop warning, which makes sense since I haven't told it that my output doesn't depend on the input from the controller yet. Searching the docs, I found that I need to specify the prerequisites_to_calc parameter by giving, in this case, a ticket indicating that I only depend on the states.
However, I can't figure out how to pass that ticket in. I assumed I would just use the LeafSystem.all_state_ticket() dependency ticket for the prerequisites_to_calc parameter, but that doesn't work. The errors are mostly referencing the C++ code, which I don't feel as confident parsing.
class MassSpringDamper(LeafSystem):
    def __init__(self, m, k1, k2, b1, b2, mu_d, mu_s):
        super().__init__()
        state_index = self.DeclareContinuousState(2)
        self._F_port = self.DeclareVectorInputPort(name='F', size=1)
        self.DeclareStateOutputPort("x", state_index)
        # This is the problem spot: what does the prerequisites_to_calc parameter expect?
        self.DeclareVectorOutputPort("y", size=2, calc=self.get_custom_output, prerequisites_to_calc=self.all_state_ticket())
        self.m = m
        self.k1 = k1
        self.k2 = k2
        self.b1 = b1
        self.b2 = b2
        self.mu_d = mu_d
        self.mu_s = mu_s

Any help is appreciated!

Answer: The prerequisites_of_calc is a set of tickets, not just one ticket. Either prerequisites_of_calc={self.all_state_ticket()} or prerequisites_of_calc=set([self.all_state_ticket()]) should work.
Also note its spelled prerequisites_of_calc not prerequisites_to_calc.
Title: How do I install idlelib?
Tags: python,installation,pip,module,python-idle

Question: How do I install idlelib in a windows computer?
Is there a way for me to install it with pip?
I am using

Windows 7 (64-bit)
Windows embeddable package (64-bit)
Python 3.8.9

Thanks.
Because I am using Windows embedded version, it comes with nothing of that fancy stuff.
D:\Installed\python-3.8.9>python -m idlelib
D:\Installed\python-3.8.9\python.exe: No module named idlelib

D:\Installed\python-3.8.9>Lib\idlelib\idle.bat
The system cannot find the path specified.

D:\Installed\python-3.8.9>pip install idle3
ERROR: Could not find a version that satisfies the requirement idle3 (from versions: none)
ERROR: No matching distribution found for idle3

D:\Installed\python-3.8.9>pip install python-idlelib
ERROR: Could not find a version that satisfies the requirement python-idlelib (from versions: none)
ERROR: No matching distribution found for python-idlelib

D:\Installed\python-3.8.9>pip install idlelib
ERROR: Could not find a version that satisfies the requirement idlelib (from versions: none)
ERROR: No matching distribution found for idlelib

D:\Installed\python-3.8.9>python -m idlelib
D:\Installed\python-3.8.9\python.exe: No module named idlelib

D:\Installed\python-3.8.9>python3 -m idlelib
'python3' is not recognized as an internal or external command,
operable program or batch file.

D:\Installed\python-3.8.9>py -m idlelib
'py' is not recognized as an internal or external command,
operable program or batch file.

D:\Installed\python-3.8.9>py3 -m idlelib
'py3' is not recognized as an internal or external command,
operable program or batch file.

D:\Installed\python-3.8.9>

D:\Installed\python-3.8.9>echo %PATH%
D:\Installed\python-3.8.9\;D:\Installed\python-3.8.9\Scripts\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;D:\Installed\Execs\;D:\Installed\R-42-editor\bin\x64\;D:\Installed\poppler-0.68.0\bin\;D:\Installed\Java17\bin\

Answer: Q. How do I install idlelib in a windows computer?
A. Get the standard 3.10 or 3.11 Windows installer from python.org, not the embeddable version. Run it.  Include the py launcher and tkinter/IDLE components.
Start IDLE from a command line with py -m idlelib, or from the Start menu IDLE entry on the python submenu, or from File Explorer right click on .py file, Edit with IDLE.
Q. Is there a way for me to install it with pip?
A. There may be alternate 3-party versions of idlelib on pypi.org, but IDLE requires tkinter, which requires _tkinter, which requires installation of the tcl/tk version that _tkinter was compiled against.  These are all left out of embedable python.  In other words, any variation of IDLE that you can install with pip requires that you already have tk/tkinter/IDLE installed with the standard installer.
I cannot say anything about Microsoft Store Python.
Title: My `collate_fn` function got empty data when pass it to collate_fn parameter of Trainer function
Tags: python,python-3.x,pytorch,huggingface-transformers

Question: I am trying to do fine-tuning an existing hugging face model.
The below code is what I collected from some documents
from transformers import AutoTokenizer, AutoModelForQuestionAnswering, TrainingArguments, Trainer
import torch

# Load the Vietnamese model and tokenizer
model_name = "vinai/phobert-base"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForQuestionAnswering.from_pretrained(model_name)

# Define the training data
train_dataset = [
    {
        "question": "What is your name ?",
        "context": "My name is Peter",
        "answer": {
            "text": "Peter",
            "start": 7,
            "end": 11
        }
    }
]

# Define the validation data
val_dataset = [
    {
        "question": "What is your name ?",
        "context": "My name is Peter",
        "answer": {
            "text": "Peter",
            "start": 7,
            "end": 11
        }
    }
]

# Define the training arguments
training_args = TrainingArguments(
    output_dir='./results',
    evaluation_strategy='epoch',
    learning_rate=2e-5,
    per_device_train_batch_size=16,
    per_device_eval_batch_size=16,
    num_train_epochs=3,
    weight_decay=0.01,
)


# Define the data collator
def collate_fn(data):
    input_ids = torch.stack([item.get('input_ids', None) for item in data if 'input_ids' in item])
    attention_mask = torch.stack([item.get('attention_mask', None) for item in data if 'attention_mask' in item])
    start_positions = torch.stack([item.get('start_positions', None) for item in data if 'start_positions' in item])
    end_positions = torch.stack([item.get('end_positions', None) for item in data if 'end_positions' in item])
    return {
        'input_ids': input_ids,
        'attention_mask': attention_mask,
        'start_positions': start_positions,
        'end_positions': end_positions
    }

# Define the trainer
trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=train_dataset,
    eval_dataset=val_dataset,
    data_collator=collate_fn
)

# Fine-tune the model
trainer.train()


I keep receiving the error of
    input_ids = torch.stack([item.get('input_ids', None) for item in data if 'input_ids' in item])
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: stack expects a non-empty TensorList

I try to do
def collate_fn(data):
    print(data)

but I got []

Answer: There is only one example on the train_dataset, so try with batch size equal to 1.
Title: How to disable debugger warnings about frozen modules when using nbconvert.ExecutePreprocessor in python script?
Tags: python,jupyter-notebook,nbconvert

Question: I am trying to run a python script to run all cells in all notebooks found a directory. It runs fine and I am getting the desired results in the notebook files. However, I want to disable the warnings that are printed to the VSCode cmd terminal when running the script. My code below:
import nbformat
from glob import glob
from nbconvert.preprocessors import ExecutePreprocessor

if __name__ == "__main__":

    nb_list = glob("./*.ipynb")

    ep = ExecutePreprocessor()

    for nb in nb_list:
        with open(nb) as f:
            nb_r = nbformat.read(f, as_version=4)
            ep.preprocess(nb_r)

The console output:

0.00s - Debugger warning: It seems that frozen modules are being used, which may
0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off
0.00s - to python to disable frozen modules.
0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.


Tried setting "env": {"PYDEVD_DISABLE_FILE_VALIDATION":"1"} in the launch.json file. Didn't change anything.

Tried setting "pythonArgs": ["-Xfrozen_modules=off"] in the launch.json file. Didn't change anything.

Tried setting warnings.filterwarnings('ignore', module='ExecutePreprocessor'). Didn't change anything.

Tried setting os.environ['PYTHONWARNINGS'] = ''. Didn't change anything.

Tried setting os.environ['PYDEVD_USE_CYTHON'] = '1'. Didn't change anything.

What I haven't tried is setting PYDEVD_DISABLE_FILE_VALIDATION=1. I don't know where to set this, how to set it, and the implications.

Answer: Please pass -Xfrozen_modules=off 0.00s - to python to disable frozen modules. 0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.
Title: Why is the value checked by python's unittest different from the value when printed in driver code?
Tags: python,unit-testing,replit

Question: I made a bunch of unit tests for my students assignments so they can check if their code is working correctly and I ran into an issue I don't know how approach.
The unit test code is this:
hero = Hero("Test Hero")

for item in items:
    hero.pickUp(item)
    hero.equip(item)
    hero.unequip(item)
    self.assertTrue(item.name in hero.backpack.__str__())
    self.assertFalse(item.name in hero.__str__())

self.assertEqual(hero.damage, 0)
self.assertEqual(hero.block, 0)
self.assertEqual(hero.armor, 0)

The last 3 tests are the ones the student isn't passing, stating that the values are -10,-10 and -30 respectively, the negative of the value expected had the unequip method not been called. So I try to figure out what is going on by replacing their driver code with the test and printing out the values of the variables I'm checking but when printed to the console I get 0,0,0; the values I'm expecting. How can the value printed to the console be different from the value when checked by the unit test? I don't get this issue when running the unit tests against my own solution to the assignment.
Note: This is all being done on Replit so that might be part of the issue.
Tried to find some sort of reason why this is happening but my searches have come up empty handed, so here I am.

Answer: Found the issue, student was inconsistent with how they were referencing the dictionary holding what items were currently equipped. Not sure how it was resulting in a difference between the printed value and the unit test but after correcting the issue was resolved.
Title: Question and answer over multiple csv files in langchain
Tags: python,python-3.x,langchain

Question: I've a folder with multiple csv files, I'm trying to figure out a way to load them all into langchain and ask questions over all of them.
Here's what I have so far.
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import Chroma
from langchain.text_splitter import CharacterTextSplitter
from langchain import OpenAI, VectorDBQA
from langchain.document_loaders import DirectoryLoader
from langchain.document_loaders.csv_loader import CSVLoader
import magic
import os
import nltk

os.environ['OPENAI_API_KEY'] = '...'

loader = DirectoryLoader('../data/', glob='**/*.csv', loader_cls=CSVLoader)

documents = loader.load()

text_splitter = CharacterTextSplitter(chunk_size=400, chunk_overlap=0)

texts = text_splitter.split_documents(documents)

embeddings = OpenAIEmbeddings(openai_api_key=os.environ['OPENAI_API_KEY'])

docsearch = Chroma.from_documents(texts, embeddings)

qa = VectorDBQA.from_chain_type(llm=OpenAI(), chain_type="stuff", vectorstore=docsearch)

query = "how many females are present?"
qa.run(query)

Answer: You should load them all into a vectorstore such as Pinecone or Metal. Then use a RetrievalQAChain or ConversationalRetrievalChain depending on if you want memory or not.
Title: How can I calculate SSIM between 2 images using scikit-image?
Tags: python,python-imaging-library,scikit-image

Question: I am trying to find the SSIM between two images that I'm storing using the Image data structure from PIL. the structural_similarity function requires both images to be stored as numpy arrays of the same shape, but despite me supplying such arrays, I keep getting an error suggesting they are not in fact the same shape.
The error I keep getting comes from the structural_similarity function, with the error being:
operands could not be broadcast together with shapes (3,) (2,) 

My code looks as follows:
import numpy as np
from PIL import Image
from skimage.metrics import structural_similarity

def readAndResize(image_path, width=60, height=30):
    try:
        image = Image.open(image_path)
        image = image.resize((width, height))
        return image
    except:
        print("Error: Could not read the image at", image_path)
        return []
    
def computeMeasure1(image1, image2):
        img1 = np.asarray(image1)
        img2 = np.asarray(image2)
        print(img1.shape)  # prints (30, 60, 3)
        print(img2.shape)  # prints (30, 60, 3)
        measure_value = structural_similarity(img1, img2, win_size=(3,3), multichannel=True)
        return measure_value

def main():
    
    path = "Images/Student_Test/generated.photos_v3_0228313.jpg"
    path2 = "Images/Student_Train/6_img_1553496386570.jpg"

    image = (readAndResize(path))
    image2 = (readAndResize(path2))

    print(computeMeasure1(image,image2))

main()

I have tried to store the images using imread() from opencv-python, but the exact same problem persisted, with the same error popping up despite both arrays being the same shape.

Answer: Problem solved, win_size=(3,3) should be a single value, not a tuple
Title: Polars groupby aggregating by sum, is returning a list of all unique values instead of actual sum
Tags: python,dataframe,group-by,python-polars,rust-polars

Question: I'm trying to do a aggregation from a polars DataFrame. But I'm not getting what I'm expecting.
This is a minimal replication of the issue:

import polars as pl

# Create a DataFrame
df = pl.DataFrame({"category": ["A", "A", "B", "B", "B"],
"value": [1., 2., 3., 4., 5.]})

# Group by 'category' and sum 'value'
result = df.groupby("category").agg({"value": pl.sum})

# Print the result
print(result)

And I'm getting:
┌──────────┬─────────────────┐
│ category ┆ value           │
│ ---      ┆ ---             │
│ str      ┆ list[f64]       │
╞══════════╪═════════════════╡
│ A        ┆ [1.0, 2.0]      │
│ B        ┆ [3.0, 4.0, 5.0] │
└──────────┴─────────────────┘

and I'd like to get:
┌──────────┬─────────────────┐
│ category ┆ value           │
│ ---      ┆ ---             │
│ str      ┆ list[f64]       │
╞══════════╪═════════════════╡
│ A        ┆ 3.0             │
│ B        ┆ 12.0            │
└──────────┴─────────────────┘


Any ideas on where the issue? thanks in advance.

Answer: You just pass in expressions into the agg function, not a dict:
result = df.groupby("category").agg(pl.col('value').sum())
Title: Cannot install Ultralytics using pip in PythonAnywhere
Tags: python,pip,pythonanywhere

Question: I was trying to install the module ultralytics (for YOLOv8) in the bash of hosting service PythonAnywhere
I tried:
pip  install ultralytics
But get:

ERROR: Could not find a version that satisfies the requirement ultralytics (from versions: none)
ERROR: No matching distribution found for ultralytics

I can install using the same method in my local machine , but having trouble in PythonAnywhere
I upgraded pip, but no difference
Why is this happening?

Answer: Try to install a newer version of Python with version >= 3.7
Then run python -m pip install --upgrade pip to upgrade your pip and install ultralytics again.
Title: Why can assignment in a 'for' loop change an inner list when iterating over 2-dimensional lists, but not change elements in a flat list?
Tags: python,list,for-loop

Question: For the following two codes, please explain why one works and the other does not.
Code 1:
y = [['1', '2'], ['3', '4']]
for x in y:
    x[0] = x[0] + '9'

print(y)

Result: [['19', '2'], ['39', '4']]
Code 2:
y = ['1', '2', '3', '4']
for x in y:
    x = x + '9'

print(y)

Result: ['1', '2', '3', '4']
Why does the result of code 1 have a 9 at the end, and the result of code 2 does not have a 9 at the end?

Answer: In the first code snippet, you have a list of list's and in the second code snippet you gave, you have a list of strings. Python's built in list is mutable. This means that mutating the list will change it's original entries as in the first code snippet. Hence, the nines are appended in the result of the first snippet. In the, second snippet, you tried modifying each element in the array, however since strings in python are immutable changing the string won't change the original entries in the array. But rather creates a new string, which is unused until the end of the loop. As a result, the array in the second example is left untouched.
Title: pycharm :No module named 'tvm'
Tags: python,pycharm,tvm

Question: I want to use tvm in my project,but got "pycharm :No module named 'tvm'". And I have configurated tvm.If I use the terminal environment to run the project, I can successful run program. The main problem is I need the hint in the pycharm.I try to add the system environment in pycharm, but I don't know how to do.

Answer: In PyCharm, in Project Settings, you can choose Python Interpreter.
You need to set this to point to the same python or python.exe command you run in a terminal. Otherwise PyCharm will use some (other) Python interpreter installed on your computer.
In UNIX shell you can find out which python command is run by your terminal by typing which python.