Archive for the 'Python' Category
If you want to be able to encrypt and decrypt files with Python, you might check out the following sites:
http://www.dlitz.net/software/pycrypto/
http://www.amk.ca/python/code/crypto.html
However, if you know you’re going to only be using Linux, I recommend doing file encryption as explained in this post. You can invoke commands at the command line by following the explanation here. Read more »
December 20th, 2008 | Posted in Linux, Python, Tip | No Comments
When you are installing modules in Python that need to be available to Python scripts stored in various locations on the server, you can use PYTHONPATH to specify where those resources are on the server. This environment variable tells Python where to find those modules/libraries.
This post explains how to set environment variables in Linux. You [...] Read more »
December 12th, 2008 | Posted in Linux, Python, Tip | 3 Comments
Let’s say you want to generate a sequence of numbers from 1 to 10 in Python. Here’s how:
x = range(1, 11) // it goes up to one less than the second parameter
Let’s say you want to generate a sequence from 0 to 10:
x = range(11) // default is to start at 0 Read more »
December 5th, 2008 | Posted in Python, Tip | No Comments
After installing Python and creating a script, all you have to do is open the command prompt and type the name of the script file (and pass any parameters to the script after that).
For example, in Windows XP, you would go to the Start menu, click on Run…, and enter cmd. Then you would need [...] Read more »
December 2nd, 2008 | Posted in Python, Tip | No Comments
Let’s say you have two lists in Python that have a lot of overlap between them.
a = ["abc","def","ghi"]
b = ["def","ghi","jkl"]
Now let’s say you want to determine what is in b that is not in a. In set theory, you would refer to this as the set-theoretic difference of b in a. The following simple code [...] Read more »
November 30th, 2008 | Posted in Collections, Python | 1 Comment
This is the first in a series of posts about how to publish to a WordPress blog using an interface called XmlRpc that is supported by WordPress as well as other blogging platforms.
My motivation for writing these posts is that I have a hobby project that is written in the C# language, and I wanted [...] Read more »
November 14th, 2008 | Posted in C#, Java, Python, XmlRpc | 5 Comments
x = "abcdefg"
print "abc" in x // True
print "xyz" in x //False Read more »
October 4th, 2008 | Posted in Python | 3 Comments
Python has a function to help you delete directories and the files in the them, but it’s important to be very careful with this, because it will delete everything recursively without a warning.
import shutil
shutil.rmtree("C:\\Temp\\DeleteMe") Read more »
September 27th, 2008 | Posted in Python | No Comments
In Windows, Linux, MacOS, etc. you can either start up programs using the user interface (pointing and clicking with your mouse) or from a command line. When you run something from the command line, it does basically the same thing behind the scenes, but you type commands instead.
For example, in Windows, you can open Internet [...] Read more »
September 1st, 2008 | Posted in Python | No Comments
Let’s say you have a string with values that are delimited by a comma. This would be common when parsing text files.
So suppose you have a delimited string:
x = "abc,def,ghi"
You can convert that into a Python list using the split function, passing the character that is delimiting the items:
y = x.split(",")
print y // [’abc’, ‘def’, [...] Read more »
September 1st, 2008 | Posted in Python | No Comments
It is very simple to do a log transformation in Python. Log transformations are sometimes performed on a set of numbers to smooth out the data (make it look more “Normal”), which can help in performing certain statistical analyses.
The default in Python is the natural log. So for demonstration purposes, I will first find e^10. [...] Read more »
August 27th, 2008 | Posted in Python, Statistics | No Comments
import shutil
shutil.copytree(src, dst)
See also Copying Files in Python. Read more »
August 25th, 2008 | Posted in Python | No Comments
To copy a file in Python, you would use the following syntax where src is the path to the file you are copying and dst is the path to which you want to copy the file.
import shutil
shutil.copy(src, dst)
If you want to copy files in a directory structure, you can use similar syntax where src is [...] Read more »
August 25th, 2008 | Posted in Python | No Comments
This is easy to do with the join command, though the syntax may not be immediately obvious.
So if you create a list:
x = ["abc","def","ghi","jkl"]
And wanted to convert that to a comma-separated list (for example, to store in a file), you would do this:
“,”.join(x)
And it would result in:
‘abc,def,ghi,jkl’
Or if you wanted it to be a tab-delimited [...] Read more »
August 23rd, 2008 | Posted in Python | No Comments
It is pretty easy to determine whether a String object is an integer in Python, but the functionality is a bit obscured by a misnomer. This can be done using a function called isdigit.
x = “123″
x.isdigit() // True
However, this has some limitations. It only works with deciphering positive integers. For example:
x = “1.23″
x.isdigit() // False
y [...] Read more »
August 23rd, 2008 | Posted in Python | 7 Comments