Skip to content

Tag: python

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…

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 associ…

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 whi…

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://d…

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…

Is enumerate in python lazy?

I’d like to know what happens when I pass the result of a generator function to python’s enumerate(). Example: Is the enumeration iterated lazily, or does it slurp everything into the <enumerate object> first? I’m 99.999% sure it’s lazy, so can I treat it exactly the same as the …

Aggregating save()s in Django?

I’m using Django with an sqlite backend, and write performance is a problem. I may graduate to a “proper” db at some stage, but for the moment I’m stuck with sqlite. I think that my write performance problems are probably related to the fact that I’m creating a large number of ro…