Myashka/SO-Python_QA-filtered-2023-tanh_score-after_2023_02 huggingface dataset:
Title: How to create a hypothesis strategy to sample uniformly over a range?
Tags: python-hypothesis

Question: I am doing something roughly like this:
test_a.py
import unittest

import hypothesis
import hypothesis.extra.numpy
import numpy as np
from hypothesis import strategies as st

SHAPE = (10, )

ARRAY_STRATEGY = hypothesis.extra.numpy.arrays(float, SHAPE, elements=st.floats(min_value=-1, max_value=1))
ZERO_ONE_STRATEGY = st.floats(min_value=0, max_value=1)


class TestMyClass(unittest.TestCase):
    @hypothesis.settings(max_examples=10)
    @hypothesis.given(
        value=ZERO_ONE_STRATEGY,
        arr1=ARRAY_STRATEGY,
        arr2=ARRAY_STRATEGY,
        arr3=ARRAY_STRATEGY,
    )
    def test_something(self, value: float, arr1: np.ndarray, arr2: np.ndarray, arr3: np.ndarray) -> None:
        print(value)


And I am often seeing behavior like this:
$ pytest test_a.py --capture=no
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0

I am testing behavior with respect to value, so sampling 0.0 10 times renders this test useless to me. I don't need to randomly sample value, in fact, I would like to sample it evenly over the range of ZERO_ONE_STRATEGY at the granularity of 1 / (max_examples - 1). How can I create a strategy to do that?

Answer: Hypothesis does not offer control over the distribution of examples, because computers are better at this than humans.

In your example, I'd bet that the other arguments are in fact varying between examples, and it's pretty reasonable to check what happens when value=0.0 and the arrays take on a variety of values.
The distribution of inputs which finds bugs fastest-in-expectation is certainly not uniform; nor does it match the distribution of inputs seen in production.
We make no promises about the distribution of examples, so that we can improve it in new versions and based on runtime observations of the behavior of code under test.  Coverage-guided fuzzing with HypoFuzz or SMT-solving with CrossHair are extreme cases but Hypothesis itself has some feedback mechanisms too (as well as the target() function).
We deliberately keep a small amount of redundancy, weighted towards the early examples, to check that your code does the same thing when fed the same inputs.

So my advice overall is to relax, trust the system and research it's based on, and think about the properties to test and possible range of valid inputs rather than the distribution within that range.
Title: How Can I make openAI API respond to requests in specific categories only?
Tags: python,python-3.x,openai-api,gpt-3,chatgpt-api

Question: I have created an openAI API using python, to respond to any type of prompt.
I want to make the API respond to requests that are only related to Ad from product description and greetings requests only and if the user sends a request that's not related to this task, the API should send a message like I'm not suitable for tasks like this.

import os
import openai

openai.api_key = os.getenv("OPENAI_API_KEY")

response = openai.Completion.create(
  model="text-davinci-003",
  prompt="Write a creative ad for the following product to run on Facebook aimed at parents:\n\nProduct: Learning Room is a virtual environment to help students from kindergarten to high school excel in school.",
  temperature=0.5,
  max_tokens=100,
  top_p=1.0,
  frequency_penalty=0.0,
  presence_penalty=0.0
)


I want to update the code to generate a chat like this. make bot understand generating ADs and greetings requests and ignoring the others
EX:-
user:- Hello
api:- Hello, How can I assist you today with your brand?
user:- Write a social media post for the following product to run on Facebook aimed at parents:\n\nProduct: Learning Room is a virtual environment to help students from kindergarten to high school excel in school.
api:- Are you looking for a way to give your child a head start in school? Look no further than Learning Room! Our virtual environment is designed to help students from kindergarten to high school excel in their studies. Our unique platform offers personalized learning plans, interactive activities, and real-time feedback to ensure your child is getting the most out of their education. Give your child the best chance to succeed in school with Learning Room!
user:- where is the united states located?
api:- I'm not suitable for this type of tasks.
So, How can update my code?

Answer: A naive implementation would be to check a user prompt before you send it to GPT3 and make sure it includes words related to advertisments via comparing every word in the prompt to a hashtable full of related words beforehand.
If it has enough related words, let the prompt go through. Else, change the prompt to 'Say "I'm not suitable for this type of tasks."'.
Title: automatic reloading of jupyter notebook after crash
Tags: python,google-cloud-platform,jupyter-notebook,gcp-ai-platform-notebook

Question: is there a way to reload automatically a jupyter notebook, each time it crashes ?
I am actually running a notebook, that trains a Deep learning model (the notebook can reload the last state of model, with state of optimizer and scheduler, after each restart of the kernel ), so that reloading the notebook after a crash enables to get back the last state without a substantial loss of computations.
I was wondering if there was a simple way to do that using the jupyter notebook API, or a signal from the jupyter notebook for example (maybe on logs).
Also, I am running the notebook on google cloud platform (on compute engine), if you know any efficient way to do it, using the GCP troubleshooting services, and the logging agent, it might be interested for me and for others with the same issue.
Thank you again for you time.
I tried to look up for a solution on stack overflow, but I didn't find any similar question.

Answer: From your comment:
"reloading the notebook after a crash enables to get back the last state without a substantial loss of computations."
What do you call a crash?, does it generate logs that can be parsed from /var/log or other location (e.g journalctl -u jupyter.service) ? If so you can manually create a shell script.
With User Managed Notebooks you have the concept of post-startup-script or startup-script
post-startup-script, is path to a Bash script that automatically runs after a notebook instance fully boots up. The path must be a URL or Cloud Storage path. Example: "gs://path-to-file/file-name"
This script can be a loop that monitors the crash you mention
Title: Can Classes Use Assignment Unpacking Similar to Iterable Lists?
Tags: python,python-3.x

Question: I have list myList = [[1, 2], [3, 4], [5, 6]] which I can use assignment unpacking to get list_instance0, list_instance1, list_instance2 = myList
If I have a class:
class myClass():
    def __init__(self, a: int, b: int):
        self.a = a
        self.b = b
    
    def printAB(self):
        return self.a, self.b

Now If I want to assign the list_instance[i] I can do:
classInstance0=myClass(*list_instance0)
classInstance1=myClass(*list_instance1)
classInstance2=myClass(*list_instance2)

If I was to try
subClass0, subClass1, subClass2 = myClass(*list_instance0, *list_instance1, *list_instance2)
# error "myClass" is not iterable: __iter__ method not defined

So I was wondering is there a way to assign classInstance[i] with list_instance[i] to myClass in a more dynamic way instead of having to assign each one individually?

Answer: I think you are misunderstanding inheritance. The items you are calling sublcass are not subclasses of myClass. They are instances of myClass. So the short answer is no. Calling the constructor for myClass returns a single object. Namely, an instance of myClass. So it cannot be assigned to 3 objects or an iterable, because it returns a single object.
Title: I'm trying to convert a fasta document into a dictionary in python using a for loop, but only my last iteration gets captured
Tags: python,for-loop,bioinformatics,fasta

Question: I'm trying to write a code to create a dictionary that reads fasta document for a dna sequence, where names of dna sequences are signified with a ">" at the start of the row containing the name. Until the next time a name is encountered, the dna sequence's bases will keep getting assigned to the dictionary entry. The for loop I've created only creates a dictionary for the last sequence, and I can't understand why this happens.
Here's the code I wrote:
def read_fasta():
    with open('../data/problem_1_question_4_new.fasta', 'r') as fasta:
        for line in fasta:
            rows = line.split()
            sequencedict = {}
            sequence = ''
            if str(rows)[2] == '>':
                sequencename = str(rows)[3:-2]
            else:
                sequence += str(rows)[2:-2]
            sequencedict[sequencename] = sequence
    return(sequencedict)
print(read_fasta())

I'm assuming that I have an error with the indentations, but I don't know where.
Edit: I've solved the error. I moved the line "sequencedict = {}" outside of the for loop.  My new code is:
def read_fasta():
    with open('../data/problem_1_question_4_new.fasta', 'r') as fasta:
        sequencedict = {}
        for line in fasta:
            rows = line.split()
            sequence = ''
            if str(rows)[2] == '>':
                sequencename = str(rows)[3:-2]
            else:
                sequence += str(rows)[2:-2]
            sequencedict[sequencename] = sequence
    return(sequencedict)
print(read_fasta())

Answer: You need to declare your dict outside the for loop. As it stands, your dict is renewed every new iteration.
Title: Cannot import cvzone in VSCode though correctly installed
Tags: python,visual-studio-code,cvzone

Question: I am trying to work with cvzone library in Python using VSCode. I have installed the library using
pip install cvzone. But when trying to import the library, ModuleNotFoundError is being raised.
import cvzone

Output
ModuleNotFoundError: No module named 'cvzone'


I have checked that the correct environment is activated in terminal
Have checked that the interpreter is correctly selected
Ran pip list and ensured that the library is installed
Tried to import using python in terminal, and worked correctly

I tried to run the same in Colab and it was successfully imported. But when trying to run in Jupyter Notebook in VSCode, it is not working.

Using cvzone 1.5.6

Answer: I deleted the virtualenv, created a new one (with another name. First, I created one with the same name, but it showed the same issue), and reinstalled all the packages. Then it worked! I don't know what was wrong, but anyway... Thank you all for answering.
Title: Using is statement for strings
Tags: python,string

Question: Can someone explain why I am getting following outputs? (I know this code has SyntaxWarning.)
>>> a = 'book'
>>> a is 'book'
True

>>> a = 'two book'
>>> a is 'two book'
False

I was expecting same result for both code snippet.

Answer: When comparing any 2 objects with "is" in python, you actually check the storage allocation of the 2 objects.
When creating a string, python tries to optimize the storage by using the same allocation for both. but this behavior isn't always consistent, for it might be different for other strings.
I notices this behavior changes when using a space (" ") as a part of the string.
Anyways, you should compare objects with "==" and not "is". This way, the comparison will use the "eq" method from the class of the object, as it should.
Or you may implement this method by yourself if you'd like to (using inheritance and overriding)
Title: linear regression/ols regression with python
Tags: python,scikit-learn,regression,linear-regression,olsmultiplelinearregression

Question: I want to run multiple linear regression models, and there are 5 independent variables (2 of them are categorical).
Thus, I first applied onehotencoder to change categorical variables into dummies.
These are dependent and independent variables
y = df['price']
x = df[['age', 'totalRooms', 'elevator',
        'floorLevel_bottom', 'floorLevel_high', 
        'floorLevel_low',
        'floorLevel_medium','floorLevel_top',
        'buildingType_bungalow', 'buildingType_plate', 
        'buildingType_plate_tower', 'buildingType_tower']]

Next, I tried the following two methods, but found that their results are different only for the intercept and categorical variables.
from sklearn.linear_model import LinearRegression

mlr = linear_model.LinearRegression()
mlr.fit(x, y)

print('Intercept: \n', mlr_in.intercept_)
print("Coefficients:")
list(zip(x, mlr_in.coef_))

This gives
Intercept:
35228.96453917408
Coefficients:
[('age', 1046.5347118942063),
('totalRooms', -797.7667275033103),
('elevator', 11940.629576736419),
('floorLevel_bottom', 1011.5929167549165),
('floorLevel_high', 157.60625500592502),
('floorLevel_low', 483.89164772666277),
('floorLevel_medium', 630.9547280568961),
('floorLevel_top', -2284.0455475443687),
('buildingType_bungalow', 31610.88176756009),
('buildingType_plate', -9649.087529585862),
('buildingType_plate_tower', -8813.187607409624),
('buildingType_tower', -13148.606630564624)]
import statsmodels.formula.api as smf

x_in = sm.add_constant(x_in)
model = sm.OLS(y, x_in).fit()
print(model.summary())

but this gives

Intercept                  2.43e+04
age                       1046.5347
totalRooms                -797.7667
elevator                  1.194e+04
floorLevel_bottom         5870.7604
floorLevel_high           5016.7738
floorLevel_low            5343.0592
floorLevel_medium         5490.1223
floorLevel_top            2575.1220
buildingType_bungalow     3.768e+04
buildingType_plate       -3575.1281
buildingType_plate_tower -2739.2282
buildingType_tower       -7074.6472
Now I don't understand the difference between them ;(

Answer: Few things to take care of assuming you have done data preprocessing exactly for each iteration. (By the variable names I think there might be something else you might've done)

Set the seed to the same number so that results will pick the same random number, to begin with.
Avoid dummy variable trtap and use pd.get_dummies(x, columns=['floorLevel', 'buildingType'], drop_first=True)
Title: List data print in another for loop in Django template
Tags: python,python-3.x,django,list,django-templates

Question: {% for i in packages %}
{% endfor %}

I used this "for loop" in the Django template, in this loop I need to print another list.
package_main_prices_list=[340, 180, 170, 95, 500]
problem is I need to print one data from the "package_main_prices_list" list in one "Packages" loop in the Django template
like this:
{% for i in packages %}
[340]    #one data from "package_main_prices_list"
{% endfor %}

my views:
package_main_prices_list = []

for i in packages:
    package_price = float(i.startup_payment)
    calculated_price = round(max(package_price, package_price * last_price / 28000))
    package_main_prices_list.append(calculated_price)

context = {
    'packages': packages,
    'package_main_prices_list': package_main_prices_list,
}
return render(request, 'pages/shop.html', context)

The output will be:
package name
340
date
package name
180
date

Answer: Pass the data in query set. It will be much easier
Title: tf.keras.utils.get_file error: TypeError: '<' not supported between instances of 'int' and 'NoneType'
Tags: python,tensorflow,keras,tensorflow2.0

Question: everyone,
Recently I start to program with tensorflow 2.10.0, have the following codes in my ipynb file (Jupyter Notebook file):
if not data_dir.exists():
    tf.keras.utils.get_file('free-spoken-digit-dataset-master.zip',origin="https://codeload.github.com/Jakobovski/free-spoken-digit-dataset/zip/refs/heads/master",extract=True,cache_dir='.',cache_subdir='data')

I want to download the file free-spoken-digit-dataset-master.zip from the URL https://codeload.github.com/Jakobovski/free-spoken-digit-dataset/zip/refs/heads/master, after running the codes the following error message is displayed:
TypeError: '<' not supported between instances of 'int' and 'NoneType'

Has anyone faced this issue or similar issue before?
Also tried the following codes:
tf.keras.utils.get_file(origin="https://github.com/Jakobovski/free-spoken-digit-dataset/archive/v1.0.9.tar.gz",extract=True,cache_dir='.',cache_subdir='data')

The same error message was displayed:
TypeError: '<' not supported between instances of 'int' and 'NoneType'

Answer: Thank you deeply, @TFer2, Stackoverflow is a good forum for software developers, people can almost find each answer here.
I already turned to other projects now. The correct way to loading the spoken_digit dataset is:
dataset,dataset_info=tfds.load("spoken_digit",split=['train'],as_supervised=True,with_info=True)
or
dataset=TFDatasets.builder('spoken_digit').as_dataset()['train']
.
Title: How do I turn a 2-column csv into a json list of the key/value pairs using python?
Tags: python,json,csv

Question: I have a csv like this:




key
spanish




no
no


yes
is


why
porque




I want to make it into a json like this:
{
   "no": "no",
   "yes": "si",
   "why": "porque"
}

All the examples I have found online create JSON's kind of like this:
{
    "no": {
        "key": "no",
        "Spanish": "no"
    }
}

I would like to write a python script that will create the simple key/value pairs in a json file.

Answer: Load you csv to dataframe, then use this:
df.set_index('key')['spanish'].to_json('path/to/jsonfile.json')
Neatly save the dataframe in json format.
Title: Will using time.sleep(1) in a loop block other routes in my FastAPI server?
Tags: python,fastapi,blocking

Question: I have a route that sends a number of emails. To avoid rate limits, I am using time.sleep(1) between emails. If I understand correctly, the route will run in its own thread or coroutine and this will not block other requests, but I thought it would be good to confirm this with the community. Here is a code example (simplified to focus on the issue):
@router.get("/send_a_bunch_of_emails")
def send_a_bunch_of_emails(db: Session = Depends(get_db)):
    users = get_a_bunch_of_users(db)
    for user in users:
        send_email(to=user.email)
        time.sleep(1)  # Maximum of 1 email per second

I am just wanting to confirm, that if hypothetically, this sent 10 emails, it wouldn't block FastAPI for 10 seconds. Based on my testing this doesn't appear to be the case, but I'm wary of gotchas.

Answer: FastAPI runs on ASGI server implementations, where A stands for Asynchronous.  One can't be asynchronous while freezing on simple sleep() call.  This is very much by design; not exactly FastAPI's, but rather the lower-level frameworks it uses.
You can test this directly by implementing /sleep/{int} route, GETting /sleep/666 and trying other endpoints while it "hangs".
Title: why is there tensor.dtype and tensor.type()?
Tags: python,pytorch

Question: Quick question.
Why does a tensor have a type and a dtype attribute?
t = torch.tensor([1,2,3,4])
print(t.dtype, t.type())

is it possible to change one of them without changing the other? if not why is there this redundancy? Does it serve any purpose?

Answer: The Tensor.type(dtype=None) function has the ability to cast the tensor to the given dtype as a parameter. If dtype parameter is not provided, it just returns the dtype of the tensor. So, the difference is the added functionality of casting.
Also, If the casting is performed to a new type, then the function returns a copy of the tensor.
Title: What does a negative return value mean when calling to_sql() in Pandas?
Tags: python,pandas,pandas-to-sql

Question: I'm sending various data frames to Microsoft SQL Server using the Pandas function to_sql() and a mssql+pyodbc:// connection made with sqlalchemy.create_engine.  Sometimes to_sql() returns the number of rows written, which is what I expect from the documentation on Returns:

Number of rows affected by to_sql. None is returned if the callable passed into method does not return an integer number of rows.
The number of returned rows affected is the sum of the rowcount attribute of sqlite3.Cursor or SQLAlchemy connectable which may not reflect the exact number of written rows as stipulated in the sqlite3 or SQLAlchemy.

But in some cases I'm seeing it return negative values like -1, 2, -11, -56.  If I use method="multi" this behavior goes away.  Here I'm writing a table with 325 records:
>>> PLSUBMITTALTYPE.to_sql("PLSubmittalType", con=data_lake, if_exists="replace")
-1

>>> PLSUBMITTALTYPE.to_sql("PLSubmittalType", con=data_lake, if_exists="replace", method="multi", chunksize = 50)
325

What do those negative values mean?  It appears to be succeeding in writing to the database in those cases.

Answer: You have a software stack that is not behaving
in a completely reliable, predicted, documented way.
I recommend changing your approach.
Use some combination of CREATE TABLE / DELETE FROM / TRUNCATE
to obtain a temp table that has zero rows in it.
Often CREATE TABLE LIKE is a convenient approach for this.
Invoke with two args: .to_sql(temp_table, con=con)
This successfully INSERTed and reported number of rows without incident.
Now submit a transaction to transfer those rows to the table
of interest. You have several options:

INSERT all rows (perhaps with errors ignored)
UPDATE all rows
use JOIN to segregate new / existing values, and do separate INSERT / UPDATE
some other vendor-specific UPSERT technique

When choosing an option you now have full flexibility to
use any technique suggested by your DB vendor or the community,
rather than having to funnel things through the narrow
cross-vendor API offered by to_sql.
This puts you back in the driver seat,
so you can implement the more reliable solution you desire.
Title: How to install SciPy and Numpy on Windows on ARM64
Tags: python,numpy,scipy,windows-arm64

Question: I need numpy and scipy to perform some signal analysis. Has anyone succeeded in doing this? (I am interested in running it natively, not via virtualenv). My ultimate goal is to build an exe from the python script that uses numpy and scipy that can be run in WinPE for tests.
I have successfully installed python 3.11.2 and am able to get to numpy installation which also fails at this.
  INFO: unifing config_fc, config, build_clib, build_ext, build commands --fcompiler options
  running build_src
  INFO: build_src
  INFO: building py_modules sources
  creating build
  creating build\src.win-arm64-3.11
  creating build\src.win-arm64-3.11\numpy
  creating build\src.win-arm64-3.11\numpy\distutils
  INFO: building library "npymath" sources
  error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
  [end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: Failed building wheel for numpy
Failed to build numpy

Answer: This will be an interesting exercise. If you have experience in building software go straight ahead. If you're new to building packages, then you might need a different way.
To build windows+ARM native packages of numpy and scipy:

Not only will you need a C/C++ compiler, you will also need a Fortran compiler. I am not sure if gfortran is available for WinARM (that's what we typically use to build scipy wheels). ifort is another compiler to look into. scipy wheels are made with gcc/g++/gfortran.
You will need to download + build a BLAS library from source. The scipy/numpy projects typically use OpenBLAS.
You will need to build + install packages for all the build time dependencies. i.e. Cython, pybind11, (pythran), meson, meson-python, ninja. Some of those may have dependencies of their own. A couple of those have OS/arch independent wheels, but not all.
Once you've built all of those you'll build numpy, then scipy.

This is a large task, and even then some of the packages may have bugs that are exposed when you try and do Windows + ARM.
Can you use an emulated python environment? e.g. does Windows+ARM permit x86_64 python interpreters to run? Alternatively could you use WSL to run a Python interpreter?
Title: How to install SciPy and Numpy on Windows on ARM64
Tags: python,numpy,scipy,windows-arm64

Question: I need numpy and scipy to perform some signal analysis. Has anyone succeeded in doing this? (I am interested in running it natively, not via virtualenv). My ultimate goal is to build an exe from the python script that uses numpy and scipy that can be run in WinPE for tests.
I have successfully installed python 3.11.2 and am able to get to numpy installation which also fails at this.
  INFO: unifing config_fc, config, build_clib, build_ext, build commands --fcompiler options
  running build_src
  INFO: build_src
  INFO: building py_modules sources
  creating build
  creating build\src.win-arm64-3.11
  creating build\src.win-arm64-3.11\numpy
  creating build\src.win-arm64-3.11\numpy\distutils
  INFO: building library "npymath" sources
  error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
  [end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: Failed building wheel for numpy
Failed to build numpy

Answer: Numpy and Scipy don't provide pre-built binary wheels for Windows Arm64 yet and need to be built from the source during installation. You will need to install Visual Studio 2019 with C/C++ toolchains.
Title: Are they equally stored in memory in python3?
Tags: python-3.x,unicode,cpython

Question: As we know that Python3 takes all string characters as Unicode code point.
type('\x0d')
<class 'str'>
type(b'\x0d')
<class 'bytes'>

The ascii of b'\x0d' is 13,stored in memory in the form of 0000 0111,'\x0d' is stored in the same format of 0000 0111 or not?Are they equally stored in memory?
To dig more to make me more confused:
#My python version
python3 --version
Python 3.9.2
#in python cli
len(b'\x0d')
1
import sys
print(sys.getsizeof(b'\x0d'))
34

b\x0d is not stored in the form of 00000111 in memory?
print(sys.getsizeof('\x0d'))
50

From using sys.getsizeof make me understand that:

string and bytes are stored with different objects in python3.
When we say that b\x0d is stored in the form of 00000111 in memory,it is based on some abstract level,in fact b\x0d is stored with 34 bytes in pc's memory for cython3?

Answer: CPython currently encodes (text) strings using the minimum of 8,16, 32 bits per character (in LATIN-1, UTF-16(?), UTF-32) to fit all characters contained in the string.
So, if all of your characters fit in the LATIN-1 encoding - which feature a 1:1 matching with unicode codepoints <256, it will use only one byte per character internally.
I've seem somewhere (sorry, it was a debate in comments in a question here, but I can´t find it now) there are future plans to keep text-strings utf-8 encoded in memory, so to avoid a large overhead in the great majority of text-related workflows, where a utf-8 byte string is decoded to the internal representation to be little used. Pypy currently uses that approach.
Title: Poetry import package in test
Tags: python-packaging,python-poetry

Question: I have the following project structure:
/
  pyproject.toml
  poetry.lock
  tests/
    test.py
  src/
    my_pkg/
      __init__.py
      ...

test.py
import my_pkg
However, I get the error, ModuleNotFoundError: No module named 'my_pkg'. How do I properly import my_pkg into test.py?

Answer: My solution was odd.

Exit the poetry shell using exit.
Create a README.md (touch README.md) on the main package dir 🤷
poetry install (should write: "Installing the current project:")
poetry shell && pytest.
Title: 'Web3' object has no attribute 'isConnected'
Tags: python,jupyter-notebook,ethereum,web3py

Question: I am trying to connect to the Ethereum blockchain with Web3. When I install web3 using jupyter notebook I keep receiving the error that Web3 has no attribute. Can someone please advise on how to get connected to the Ethereum network?
MY CODE:
pip install web3

from web3 import Web3, EthereumTesterProvider
w3 = Web3(EthereumTesterProvider())
w3.isConnected() 

ERROR:
AttributeError Traceback (most recent call last)
Input In [29], in <cell line: 3>()
  1 from web3 import EthereumTesterProvider
  2 w3 = Web3(EthereumTesterProvider())
----> 3 w3.isConnected()

AttributeError: 'Web3' object has no attribute 'isConnected'

I have tried both web3 and capital Web3 and still receive the same error. I have also tried
w3 = Web3(Web3.EthereumTesterProvider()) 

but same issues.

Answer: Please try is_connected() instead of isConnected().
Title: 'Web3' object has no attribute 'isConnected'
Tags: python,jupyter-notebook,ethereum,web3py

Question: I am trying to connect to the Ethereum blockchain with Web3. When I install web3 using jupyter notebook I keep receiving the error that Web3 has no attribute. Can someone please advise on how to get connected to the Ethereum network?
MY CODE:
pip install web3

from web3 import Web3, EthereumTesterProvider
w3 = Web3(EthereumTesterProvider())
w3.isConnected() 

ERROR:
AttributeError Traceback (most recent call last)
Input In [29], in <cell line: 3>()
  1 from web3 import EthereumTesterProvider
  2 w3 = Web3(EthereumTesterProvider())
----> 3 w3.isConnected()

AttributeError: 'Web3' object has no attribute 'isConnected'

I have tried both web3 and capital Web3 and still receive the same error. I have also tried
w3 = Web3(Web3.EthereumTesterProvider()) 

but same issues.

Answer: Web3py has started using snakecase instead of CamelCase, So try is_connected()
Title: How to prevent matplotlib to be shown in popup window?
Tags: python,matplotlib

Question: I use matplotlib in my lib to display legend on a ipyleaflet map. In my CD/CI tests I run several checks on this legend (values displayed, colors etc...). My problem is when it's run on my local computer, matplotlib open a legend popup windows that stops the execution of the tests.
Is it possible to force matplotlib to remain non-interactive when I run my pytest session ?

Answer: You can change the matplotlib backend to a non graphical one by calling matplotlib.use('Agg') at the beginning of your scripts. This will prevent matplotlib from opening windows.
Title: Python: Return tuple value to function and calling a specfic value. TypeError: 'NoneType' object is not subscriptable
Tags: python,yolov5

Question: Hello this is my first Post ever so if I can do anything to improve please tell me.
So I am returning to variables x, y to a function.
When I output the Tuple value in my main function with print(coords()) it works perfectly fine.
It just returns 2 int values (500, 400).
When I try print(coords()[0]) I should get my X value, but I do get a:
TypeError: 'NoneType' object is not subscriptable

The best part is it worked yesterday just like that, when I reopened my Project today it just doesn't work anymore.
Running this code without any functions works perfectly fine as well.
Also interesting is that sometimes trying print(coords()[0]) works for a second and shows the right results, but then crashes with Type Error.
I am using Yolov5, but Yolo is working perfectly fine, and it's only trying to get the coordinates of a single object.
def coords():
rl = detection.xyxy[0].tolist()

        #calculates x and y pos from detected bonding box pixel location
        if len(rl) > 0:
            if rl[0][4] > 0.7:
                if rl[0][5] == 0:             
                    width = float(rl[0][2]) - float(rl[0][0])
                    height = float(rl[0][3]) - float(rl[0][1])

                    x = float(rl[0][2]) - (width / 2)
                    y = float(rl[0][3]) - (height / 2) + 15

                    return int(x), int(y)

                    #yesterday my code looked like this
                    #coords = x, y
                    #return list(coords) # but this results in the same type error
                    
def main():
    while True:
       print(coords()) # does work
       print(coords()[0]) # does not work , but did yesterday

Answer: The answer from John Gordon is right:
If any of the top three if conditions are false, the function will never make it to the return int(x), int(y) line, and ends up returning None by default.
I filtered the None Values now it's working.
Title: (PYTHON, TKINTER) How can I get radiobuttons to delete with my canvases? (School Project Combat Game)
Tags: python,tkinter,canvas,radio-button,ttk

Question: I am trying to figure out how to delete radiobuttons along with my canvas when it switches to the next one.
my code:
(P.S., I am trying to make it so that after the user's turn ends, another canvas with the same properties (but with a fresh set of radiobuttons) opens up, allowing the user to play their next turn. Open to any suggestions/improvements!)
from tkinter import *
import random

root = Tk()

def game_canvas():
    global c, var
    c.destroy()
    c = Canvas(root, height = 720, width = 1280, bg = 'black')
    c.pack()

    #creates the background of the scene
    c.create_image(0, 0, image = arena, anchor = NW)

    #adds the user and griffin's images
    c.create_image(300, 95, image = user_geralt_image2, anchor = NW)
    c.create_image(875, 300, image = griffin_image)

    #creates and places the button to go to the next canva
    confirm_button = Button(c, text = 'Comfirm', command = action, font = 'Calibri 16', borderwidth = 5)
    create_confirm_button = c.create_window(1156, 685, window = confirm_button)

    #creates and places the button to quit the game
    quit_button = Button(c, text = 'Quit', command = exit_confirmation, font = 'Calibri 16', borderwidth = 5)
    create_quit_button = c.create_window(1241, 685, window = quit_button)

    var = IntVar()
    r1 = Radiobutton(root, text = 'Attack', font = 'Calibri 16', variable = var, value = 1)
    r1.pack(anchor = W)

    r2 = Radiobutton(root, text = 'Parry', font = 'Calibri 16', variable = var, value = 2)
    r2.pack(anchor = W)

    r3 = Radiobutton(root, text = 'Dodge', font = 'Calibri 16', variable = var, value = 3)
    r3.pack(anchor = W)

root.mainloop()

I've done a few google searches to find a way to attach the radioheads to the Canvas, but to no avail.
Thanks!!

Answer: There are at least two simple solutions, based on the same principle.
First, you can make the radiobuttons a child of the canvas. When you delete the canvas, all children will automatically be deleted.
Since you didn't do that, I'm guessing you want the radiobuttons to be outside of the canvas. In that case, the simplest thing is to make both the radio buttons and the canvas a frame. When you delete the frame, all of the widgets will be destroyed too.
Title: "ORA-01036 illegal variable name/number" at INSERT INTO query
Tags: python,oracle,ms-access

Question: I'm creating a Python code that fetches data from an MS Access table and inserts it into an Oracle SQL table, but when I run the code, I get the error ORA-01036 illegal variable name/number.
This error is occurring in the INSERT INTO statement. I don't know what it could be.
import pyodbc
import cx_Oracle

# Set up the Microsoft Access connection
access_conn_str = (
    r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};'
    r'DBQ=C:\MyFolder\ACC_BASE.MDB;'
    )
access_conn = pyodbc.connect(access_conn_str)

# Define the Oracle SQL connection string
oracle_conn_str = cx_Oracle.makedsn("MyConnection", "MyPort", "MySID")

# Create a connection to the Oracle SQL database
oracle_conn = cx_Oracle.connect(user="MyUser", password="MyPassword", dsn=oracle_conn_str)

# Create a cursor for each connection
access_cursor = access_conn.cursor()
oracle_cursor = oracle_conn.cursor()

# Execute the select statement to extract data from the Access table
access_cursor.execute('SELECT * FROM ACC_TABLE')

# Loop through the rows of the Access table and insert them into the Oracle SQL table
for row in access_cursor.fetchall():
    oracle_cursor.execute(
        'INSERT INTO ORACLE_TABLE (COD, LEV, AZET, HUES) VALUES (?, ?, ?, ?)',
        [row[0], row[1], row[2], row[3]]
    )

# Commit the changes to the Oracle SQL table
oracle_conn.commit()

# Close the cursors and connections
access_cursor.close()
access_conn.close()
oracle_cursor.close()
oracle_conn.close()

Answer: The message

ORA-01036 illegal variable name/number

indicate that there might be a problem with the variable you are trying to put into the database, try checking the names of columns in the ORACLE_TABLE and check if they match what there is in your INSERT command.
Title: How to map flower port 5556 to an enpoint in FastAPI?
Tags: python-3.x,docker-compose,celery,fastapi,flower

Question: I have a simple FastAPI app. I am using celery for async task processing and flower dashboard for monitoring tasks
My main application is running on port 80
My flower dashboard for task monitoring is running on port 5556
Now I want to map the port to the app endpoint, something like - http://localhost/flower-dashboard
Here is my docker-compose.yml file:
version: '3.8'

services:

  web:
    build: ./project
    ports:
      - 80:80
    command: uvicorn main:app --host 0.0.0.0 --reload
    volumes:
      - ./project:/usr/src/app
    environment:
      - CELERY_BROKER_URL=redis://:password@redis:6379/0
      - CELERY_RESULT_BACKEND=redis://:password@redis:6379/0
    depends_on:
      - redis

  worker:
    build: ./project
    command: celery worker --app=worker.celery --loglevel=info --logfile=logs/celery.log
    volumes:
      - ./project:/usr/src/app
    environment:
      - CELERY_BROKER_URL=redis://:password@redis:6379/0
      - CELERY_RESULT_BACKEND=redis://:password@redis:6379/0
    depends_on:
      - web
      - redis

  redis:
    image: public.ecr.aws/ubuntu/redis:5.0-20.04_edge
    restart: always
    command: /bin/sh -c "redis-server --requirepass $$REDIS_HOST_PASSWORD"
    env_file:
      - redis.env
  dashboard:
    build: ./project
    command:  flower --app=worker.celery --port=5555 --broker=redis://:password@redis:6379/0
    ports:
      - 5556:5555
    environment:
      - CELERY_BROKER_URL=redis://:password@redis:6379/0
      - CELERY_RESULT_BACKEND=redis://:password@redis:6379/0
    depends_on:
      - web
      - redis

Any help would be highly appreciated, thanks!

Answer: This may not be an easy thing to do. To map localhost:5556 to localhost/flower-dashboard you'd need to use a proxy. You could add an Nginx or Apache service to your docker-compose configuration and make it route localhost/flower-dashboard requests to the dashboard service and all other requests localhost/* to the web service. This implies that you do not map web port 80 to the host like you do now, and map the proxy port 80 instead.
Title: File not found while creating file in Python3 in a cron job. (FileNotFoundError: [Errno 2])
Tags: python-3.x,linux,cron,debian,filenotfounderror

Question: After I've updated my system, a weird error appeared:
Traceback (most recent call last):
  File /home/username/bin/my_application.py, line 116, in <module>
    with open(lock_file, 'x'):
FileNotFoundError: [Errno 2] No such file or directory: '/run/user/1000/running_application.lock'

Line 116+117 look like this:
with open(lock_file, 'x'):
    print("Lockfile " + lock_file + " created")

The path of the lockfile is defined like this:
lock_file = '/run/user/' + str(os.getuid()) + '/running_application.lock'

The uid is always 1000 (if triggered by cron or by hand).
I am trying to create a file only to avoid to run this script several times at the same time. It will be triggered in a cron job (crontab -e) on a Debian 11 bullseye server. Before this line, a routine will be triggered to wait until the file doesn't exist anymore or if x minutes are over to exit.
It always worked and if I trigger this script by hand, it still works. But out of the nowhere it doesn't work if it's triggered over the cron job.
And I don't understand the error at all. How can Python expect a file, if the file doesn't exist and have to be created?
I already tried it with the 'w' option, but the same error appears.
And I don't know what to do anymore, because this script works perfectly if it's triggered by hand.
Thanks a lot for your help :)

Answer: You can try specifying the environment variable inside your cronjob
in your cronjob you could add something like */5 * * * * export XDG_RUNTIME_DIR=/run/user/1000 && /usr/bin/python3 /home/username/bin/my_application.py
you can also use an absolut path for your lock file lock_file = '/home/username/locks/running_application.lock'
Also check the permissions, maybe the user running the cron dont have the right permission to create the lock file ?
Title: Best way to build application triggering reminder at certain time
Tags: python,web-applications,architecture

Question: I want to build a python app sending reminder at certain time. It would have several subscribers. Each subscriber sets specific moment when they want to be notified. For example, John wants to have a reminder every days whereas Cassandra wants it every months.
I see several ways to do it :

Use a script running 24/7 with a while loop and checks if it's time to do send the reminder.
Use a cron tab that runs the script every minutes to check if there's reminder to send
Create a simple api (in Flask for example). It checks every minutes or so if there is a reminder to send to subscribers or even make a request to the api.

What is the best way to build such application in Python for few subscribers (10) and for a larger amount(1000)

Answer: For a small amount of subscribers I would do a script that run all the times, it would continuously check the time and send reminders as needed, you can use time and datetime modules for that.
For a large amount of subscribers it would be an API that can manage multiple request simultaneously, you can use Flask or Django or FastAPI for that.
For the API how would it work is that subscribers would make POST request with all the informational notification ( date, time, what message they want to receive for example ) then the server store those informations in a database like if you are familiar with python Celery seem the best one but you have Airflow too, it will schedule a reminder to be executed at a certain time.
Then when the reminder is executed it just retrieve the informations from the database, and send it via email for example
Title: Django and Huey task issue, record in DB doesn't exist when the task is ran
Tags: python,django,redis,python-huey

Question: I am testing Huey with Django and found one issue, tasks can't find the record in DB.

Postgres 14
Redis 7.0.5
Django 4
use with docker

Here is the code:
# settings.py
USE_HUEY = env.bool("USE_HUEY", False)
HUEY = {
    "huey_class": "huey.RedisHuey", 
    "name": "huey",
    "immediate": not USE_HUEY,
    "connection": {"url": REDIS_URL},
}


# app/signals.py
@receiver(post_save, sender=Post)
def post_signal(sender, instance, **kwargs):
    from app.tasks import create_or_update_related_objects
    create_or_update_related_objects(instance.pk)


# app/tasks.py
@db_task()
def create_or_update_related_objects(object_pk):
    post = Post.objects.get(pk=object_pk)
    ...

This is running an async task but I am getting the error:
app.models.Post.DoesNotExist: Post matching query does not exist.

This is not correct, there is a post, and this task is running on a post_save signal.
What is weird, is if I do something like this, it works fine:
@db_task()
def create_or_update_related_objects(object_pk):
    import time
    time.sleep(3)
    post = Post.objects.get(pk=object_pk)
    ...

What am I doing wrong here?

Answer: I'm not sure, but most likely your task will be completed before the database is committed. It seems that the enqueue() method can add your task to the queue so that the database has time to commit.
Title: Print letters with a delay all on the same line
Tags: python

Question: I can't get the characters to all be printed on one line.
list = ["W","e","l","c","o","m","e","","t","o","","w","o","r","d","l","e"]
for item in list:
  sleep(0.25)
  print(item)

i tried using a list and imported the sleep library

Answer: In your print, add this code after item as a second parameter : end=''. By default, every time you printed, the default value \n was added.
Title: Why can't I import config?
Tags: python

Question: I have a config.yml file with api keys in it, further, I have a .py file which greps for it.
I should theoretically be able to import config, and I can do so when I move the config.py file from the main directory into the correct subdirectory, but that doesn't quite work efficiently.
Forgive the poor attempt at file structure formatting.
in directory

servers

backend

file1.py
file2.py
file_i_need_config.py




config.py

I am trying:
from config import Config

print(Config.get_property("api_key_for_thing").unwrap())

and receiving the error:
ModuleNotFoundError: No module named 'config'
Any advice would be appreciated.

Answer: With the kind souls above, I figured it out.
I was executing within the directory of user:backend % python -m file_i_need_config from there.
Needed to execute from the parent directory via user:directory % python -m servers.backend.file_i_need_config
Title: Varying performance of numpy axpy
Tags: python,numpy,performance,benchmarking,apple-m1

Question: I was trying to test the performance of numpy using this very simple script:
import numpy as np
import argparse
from timeit import default_timer as timer

p = argparse.ArgumentParser()
p.add_argument("--N", default=1000, type=int, help="size of matrix A")
p.add_argument(
    "--repeat", default=1000, type=int, help="perform computation x = A*b repeat times"
)
args = p.parse_args()

np.random.seed(0)
A = np.random.rand(args.N, args.N)
b = np.random.rand(args.N)
x = np.zeros(args.N)

ts = timer()
for i in range(args.repeat):
    x[:] = A.dot(b)
te = timer()

gbytes = 8.0 * (args.N**2 + args.N * 2) * 1e-9
print("bandwidth: %.2f GB/s" % (gbytes * args.repeat / (te - ts)))

What it does is it creates a random dense matrix, performs matrix-vector multiplication repeat times, and computes the averaged bandwidth of such operation, which I believe includes memory read, computation and memory write. However when I run this script on my laptop, the results vary quite significantly for each run:
~/toys/python ❯ python numpy_performance.py --N 8000 --repeat 100
bandwidth: 93.64 GB/s
~/toys/python ❯ python numpy_performance.py --N 8000 --repeat 100
bandwidth: 99.15 GB/s
~/toys/python ❯ python numpy_performance.py --N 8000 --repeat 100
bandwidth: 95.08 GB/s
~/toys/python ❯ python numpy_performance.py --N 8000 --repeat 100
bandwidth: 77.28 GB/s
~/toys/python ❯ python numpy_performance.py --N 8000 --repeat 100
bandwidth: 56.90 GB/s
~/toys/python ❯ python numpy_performance.py --N 8000 --repeat 100
bandwidth: 63.87 GB/s
~/toys/python ❯ python numpy_performance.py --N 8000 --repeat 100
bandwidth: 85.43 GB/s
~/toys/python ❯ python numpy_performance.py --N 8000 --repeat 100
bandwidth: 95.69 GB/s
~/toys/python ❯ python numpy_performance.py --N 8000 --repeat 100
bandwidth: 93.91 GB/s
~/toys/python ❯ python numpy_performance.py --N 8000 --repeat 100
bandwidth: 101.99 GB/s

Is this behavior expected? If so, how can it be explained? Thanks!

Answer: There can be multiple reason for the not stable result, CPU can not be stable because not confired for your unique process, you can have other process interfering with your runs, and also thermal throttling that could perturbate the run between cooling
One of the thing you could do is to make multiple run then average the result