I’ve written a command line utility that uses getopt for parsing arguments given on the command line. I would also like to have a filename be an optional argument, such as it is in other utilities …
Tag: python
How do I concatenate two lists in Python?
This question’s answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions. How do I concatenate two lists in Python? Example: Expected outcome: Answer Use the + operator to combine the lists: Output:
Python socket receive – incoming packets always have a different size
I’m using the SocketServer module for a TCP server. I’m experiencing some issue here with the recv() function, because the incoming packets always have a different size, so if I specify recv(1024) (I tried with a bigger value, and smaller), it gets stuck after 2 or 3 requests because the packet length will be smaller (I think), and then the
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 …
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 …
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: from models import Counter from django.db import transaction @transaction.commit_on_success def …
Read two variables in a single line with Python
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: scanf(“%d%d”, &i, &…
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