python-3.9

local variable 'counter' referenced before assignment

local variable 'counter' referenced before assignment Question: I am running the below code and getting this error. I am used to c# syntax but this doesn’t make any sense The rest of the code is working i have tested. Only counter is the problem at the moment import logging from transformers import pipeline counter = …

Total answers: 1

Upgrading Python version now gives variable not defined error

Upgrading Python version to 3.9 on macOS now gives “variable not defined” error for some file-reading code Question: I just upgraded from Python 3.7 to 3.9.14 and it now gives a variable not defined error. The same code works fine locally and remotely where Python 3.9.2 is installed but now locally it gives an error …

Total answers: 1

FastAPI is returning AttributeError: 'dict' object has no attribute 'encode'

FastAPI is returning AttributeError: 'dict' object has no attribute 'encode' Question: I am having a very simple FastAPI service, when I try to hit the /spell_checking endpoint, I get this error `AttributeError: ‘dict’ object has no attribute ‘encode’. I am hitting the endpoint using postman, with Post request, and this is the url: http://127.0.0.1:8080/spell_checking, the …

Total answers: 1

How to print Arabic and English text together using Python's fpdf library

How to print Arabic and English text together using Python's fpdf library Question: pdf.multi_cell(w=150, h=5, txt="تمانتمانتمانتمانتم English Letters") "English Letters" is rendered as a row of rectangles in the output: I also tried using arabic_reshaper Example code snippet: import arabic_reshaper from bidi.algorithm import get_display reshaped_text = arabic_reshaper.reshape(text) bidi_text = get_display(reshaped_text) pdf.set_xy(9, 132) pdf.multi_cell(w=150, h=5, txt=bidi_text) …

Total answers: 1

Python f-strings: how to escape curly brackets with numbers in it

Python f-strings: how to escape curly brackets with numbers in it Question: If you have to escape curly brackets in f-strings you can double them, so {{hello}} results in {hello}. The problem is, that the algorithm just concatenates the string and afterwards interprets this part as every other part. So if there is any structure …

Total answers: 1

How to save ndarray or show anim?

How to save ndarray or show anim? Question: I am using TIGRE (an open source toolbox for reconstruction provided by CERN). The original code was written for python 3.7 and is struggling to run correctly on 3.9. This is the original code and the error it produces. #%% Initialize import tigre import numpy as np …

Total answers: 1

Python get the name of all fields in a dataclass

Python get the name of all fields in a dataclass Question: I am trying to write a function to log dataclasses I would like to get the name of all fields in the dataclass and print the value to each (similar to how you might write a function to print a dictionary) i.e. @dataclasses.dataclass class …

Total answers: 2

"unresolved reference" in ternary if else one liner

"unresolved reference" in ternary if else one liner Question: I am trying to work out why the following line doesn’t work: i = 0 if i // 3 == 0 else i += 1 I am getting an "unresolved reference ‘i’ " error despite the fact that i is defined before this line. Apologies if …

Total answers: 1

Why PyCharm does not recognize user environment variables configuration?

Why PyCharm does not recognize user environment variables configuration? Question: I tried to use the PyCharm user environment variable configuration, however, it throws KeyError. If I try to set the variables via commands it works, but via configuration it does not. I will appreciate any help. import os print(os.environ[‘BLA’]) (venv) (base) mikam@Mikas-MacBook-Pro Scripts % python …

Total answers: 3

Python dictionary with fixed __dict__ contents and variable arguments

Python dictionary with fixed __dict__ contents and variable arguments Question: I’m trying to read in a bunch of data from text files to a dictionary, and I’ve made a couple of small typos which end up creating new key/value pairs and breaking the program (which can be annoying and a pain to debug). This got …

Total answers: 1