I have the method: This just returns the first of the month 2 months ago, which is what I want is there a better approach I could have used using timedeltas? I choose my way because weeks in a month are not always constant. Answer dateutil is an amazing thing. It really should become stdlib someday.
A simple Python HTTP proxy [closed]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations. Closed 4 years ago. The community reviewed whether to reopen this question 10 months ago and left it
Cast base class to derived class python (or more pythonic way of extending classes)
I need to extend the Networkx python package and add a few methods to the Graph class for my particular need The way I thought about doing this is simplying deriving a new class say NewGraph, and adding the required methods. However there are several other functions in networkx which create and return Graph objects (e.g. generate a random graph).
Get difference between two lists with Unique Entries
I have two lists in Python: Assuming the elements in each list are unique, I want to create a third list with items from the first list which are not in the second list: Are there any fast ways without cycles and checking? Answer To get elements which are in temp1 but not in temp2 (assuming uniqueness of the elements in
Generating an MD5 checksum of a file
Is there any simple way of generating (and checking) MD5 checksums of a list of files in Python? (I have a small program I’m working on, and I’d like to confirm the checksums of the files). Answer You can use hashlib.md5() Note that sometimes you won’t be able to fit the whole file in memory. In that case, you’ll have
How to sort a list of strings numerically?
I know that this sounds trivial but I did not realize that the sort() function of Python was weird. I have a list of “numbers” that are actually in string form, so I first convert them to ints, then attempt a sort. Gives me: What I want is I’ve looked around for some of the algorithms associated with sorting numeric
What is the Python ‘buffer’ type for?
There is a buffer type in Python, but how can I use it? In the Python documentation about buffer(), the description is: buffer(object[, offset[, size]]) The object argument must be an object that supports the buffer call interface (such as strings, arrays, and buffers). A new buffer object will be created which references the object argument. The buffer object will
Execute python script inside a python script
I have a scenario where i want to dynamically generate a python script – inside my main python script – store it as a string and then when need be, execute this dynamically generated script from my main script. Is this possible, if so how? thanks Answer Read up on the execfile() function. http://docs.python.org/library/functions.html?highlight=exec#execfile
python: can i set a variable to equal a function of itself?
Can I do this? When I tried to do this I got errors, but perhaps I was doing something wrong. Answer If the variable has been previously defined, you can do that yes. Example: If the variable has not been defined previously, you can’t do that, simply because there is no value that could be passed to the function.
How to round a number to significant figures in Python
I need to round a float to be displayed in a UI. e.g, to one significant figure: Is there a nice way to do this using the Python library, or do I have to write it myself? Answer You can use negative numbers to round integers: Thus if you need only most significant digit: You’ll probably have to take care