File-like objects are objects in Python that behave like a real file, e.g. have a read() and a write method(), but have a different implementation from file. It is realization of the Duck Typing concept. It is considered a good practice to allow a file-like object everywhere where a file is expected so that e…
Tag: python
Including non-Python files with setup.py
How do I make setup.py include a file that isn’t part of the code? (Specifically, it’s a license file, but it could be any other thing.) I want to be able to control the location of the file. In the original source folder, the file is in the root of the package. (i.e. on the same level as the topm…
Check if a given key already exists in a dictionary
This question’s answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions. I wanted to test if a key exists in a dictionary before updating the value for the key. I wrote the following code: I think this is not the best way to a…
Atomic increment of a counter in django
I’m trying to atomically increment a simple counter in Django. My code looks like this: If I understand Django correctly, this should wrap the function in a transaction and make the increment atomic. But it doesn’t work and there is a race condition in the counter update. How can this code be made…
Read two variables in a single line with Python [duplicate]
This question already has answers here: Get a list of numbers as input from the user (11 answers) Closed 12 days ago. I am familiar with the input() function, to read a single variable from user input. Is there a similar easy way to read two variables? I’m looking for the equivalent of: One way I am abl…
User-friendly time format in Python?
Python: I need to show file modification times in the “1 day ago”, “two hours ago”, format. Is there something ready to do that? It should be in English. Answer The code was originally published on a blog post “Python Pretty Date function” (http://evaisse.com/post/93417709/…
Knight’s Tour using a Neural Network
I was looking at the knights tour problem and decided to have a go at implementing it in python using a neural network to find solutions. The general explanation of the method can be found on Wikipedia While I think I have implemented it correctly (I can’t see anything else that is wrong), it doesn̵…
How can I get the name of an object?
Suppose I have code like: How can I get the name of each object in Python? That is to say: what could I write instead of name in this code, so that the loop will show handling object x and then handling object y and handling object z? In my actual code, I have a dict of functions that I
How to handle exceptions in a list comprehensions?
I have some a list comprehension in Python in which each iteration can throw an exception. For instance, if I have: I’ll get a ZeroDivisionError exception in the 3rd element. How can I handle this exception and continue execution of the list comprehension? The only way I can think of is to use a helper …
Python Implementation of the Object Pool Design Pattern
I need an Object Pool, and rather than implement it myself, I thought I would look around for a ready-made and tested Python library. What I found was plenty of other people looking, but not getting many straight answers, so I have brought it over here to Stack Overflow. In my case, I have a large number of t…