The list.index(x) function returns the index in the list of the first item whose value is x. Is there a function, list_func_index(), similar to the index() function that has a function, f(), as a parameter. The function, f() is run on every element, e, of the list until f(e) returns True. Then list_func_index() returns the index of e. Codewise: Is
Combine Pool.map with shared memory Array in Python multiprocessing
I have a very large (read only) array of data that I want to be processed by multiple processes in parallel. I like the Pool.map function and would like to use it to calculate functions on that data in parallel. I saw that one can use the Value or Array class to use shared memory data between processes. But when
Check if object is file-like in Python
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.g. a StringIO or a Socket
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 topmost
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 accomplish this task. Is there
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 thread-safe? Answer Use an F expression: either
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 able to achieve
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/python-pretty-date-function) It is reproduced here as the blog account has been suspended and the page is no
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’t work, it updates a few
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