Skip to content

Python 3 alternatives for __getinitargs__

Is there a short enough way to call the __init__ constructor of a class during unpickling in Python 3? The usual way to do this was using __getinitargs__ like so However, the __getinitargs__ will be ignored in new style classes and in Python 3+, all classes can only be new style classes. There is the __getnew…

How to count the number of letters in a word?

I’m trying to create a program where if you input a word, it will print out each letter of the word and how many times the letter appears in that word. Eg; when I input “aaaarggh”, the output should be “a 4 r 1 g 2 h 1”. So far it just prints out each letter and position in the

Using pythons open function for .png Image

Im getting an error when using pythons inbuild function “open” and don’t know how to get it to work for png files. Examplecode: img =open(‘here.png’).read() Error: UnicodeDecodeError: ‘charmap’ codec can’t decode byte 0x90 in position 101: character maps to <…

Using Python decorators to retry request

I have multiple functions in my script which does a REST API api requests.As i need to handle the error scenarios i have put a retry mechanism as below. I have several different methods which does similar operation. How can we do it better way to avoid duplication may be using decorators. Answer Instead of us…

Python Recursive Knight Tour

I’m trying to solve the knight tour algorithms with recursive backtracking method in python. The solution should be a matrix with 24 documented steps, but it only count up-to 5 steps. It doesn’t enter the recursive if statement. Answer The problem I see is that you set yNeu = x + pos[1] when you p…

Pandas DataFrame column numerical integration

Currently I have a DataFrame as shown below: I would like to do the numerical integration of Current with TimeSec (∫Idt) for different Devices and collect the data into a new DataFrame as below: The problem is that the time interval is not even and the number of data for each device is not even as well. Answe…

The tasks from asyncio.gather does not work concurrently

I want to scrape data from a website concurrently, but I found that the following program is NOT executed concurrently. However, this program starts to download the second content only after the first one finishes. If my understanding is correct, the await keyword on the await return_soup(url) awaits for the …