python

Setting the RGB levels of pixels (Python, Jython)

For each pixel in pic: r= random() if r < 0.25: set the red level to randrange(0,256), set the green level to randrange(0,256) set the blue level to randrange(0,256) The rest of the unseen code is correct, I just can't figure out how to phrase this function well enough for it to work. ...

How do I turn a python datetime into a string, with readable format date?

t = e['updated_parsed'] dt = datetime.datetime(t[0],t[1],t[2],t[3],t[4],t[5],t[6] print dt >>>2010-01-28 08:39:49.000003 How do I turn that into a string?: "January 28, 2010" ...

Flatten (an irregular) list of lists in Python

Yes, I know this subject has been covered before (here, here, here, here), but AFAIK, all solutions save one choke on a list like this: L = [[[1, 2, 3], [4, 5]], 6] where the desired output is [1, 2, 3, 4, 5, 6] or perhaps even better, an iterator. The only solution I saw that works for an arbitrary nesting is from @Alabaster Codi...

Easy way to upload files to S3 via HTTP Form

I have written a tiny web appication in python that allows me to browse my S3 buckets. The web appication runs inside the Google App Engine. Now, I want to create a html form for this web appication that allows me to upload a file into the bucket. These information are already inside the form: AWSAccessKeyId and the name of the bucket...

How do I create a script that appends the file create date to filename using python?

I would like to create a python script that appends the file created date to the end of the filename while retaining the oringinal file name ("Report") for a batch of pdf documents. directory = T:\WISAARD_Web Portal Projects\PortalLogging\WebLogExpert filenames = Report.pdf Thank you for your help. ...

Use distribute/setuptools to create symlink (or run script)?

As part of my project's setup process, I need to symlink one of the packages to a specified directory so an init.d script can find it. Is there any way to add this as a post-processing command to setup()? I would even settle for creating another file that creates the link and pass it to setup() as part of some kwarg list of "run these" (...

Strange Python set and hash behaviour - how does this work?

I have a class called GraphEdge which I would like to be uniquely defined within a set (the built-in set type) by its tail and head members, which are set via __init__. If I do not define __hash__, I see the following behaviour: >>> E = GraphEdge('A', 'B') >>> H = GraphEdge('A', 'B') >>> hash(E) 139731804758160 >>> hash(H) 139731804760...

javax.script.ScriptEngine fails at runtime

Any ideas? public class Main { public static void main(String[] args) throws ScriptException { ScriptEngine engine = new ScriptEngineManager().getEngineByName("python"); engine.put("hello_str", ""); engine.eval("for i in range(10):"); engine.eval(" hello_str += str(i)"); Object x = engine.get("hello_str"); Syst...

pygame audio playback speed

quick question. I'm running pygame under linux just to play some audio files. I've got some .wav files and I'm having problems playing them back at the right speed. import pygame.mixer, sys, time #plays too fast pygame.mixer.init(44100) pygame.mixer.music.load(sys.argv[1]) pygame.mixer.music.play() time.sleep(5) pygame.mixer.quit() ...

Running a PHP script inside a Python WSGI enviroment

Hi all, I have a simple PHP script that outputs a dir listing in XML format. I use it to let a flash slideshow know what files are available to show. I've just added the flash to a website that's powered by Django and the PHP file is now served up as it is, not parsed. It's in the directory with the images under my media directory. Th...

lxml has essentially nothing

The lxml package for Python seems to absolutely broken on my system. I am not sure of the problem, as all of the files are in place, it seems. My suspicion is that the problem is in __init__.py, but I don't have enough practice with the system to make an accurate diagnosis or fix the problem. Here is some code that I think will help dia...

installing mechanize with easy_install

I just got easy_install downloaded but i'm having problems installing mechanize, should I be addressing site-packages at any point. In the first try below, i got an error. in the second try below, i got command not found which is wierd since I know for sure that it downloaded. names-computer:~ names$ cd /Users/names/Desktop/ names-compu...

Why Python on Windows can't read an image in binary mode?

Hi, I want to read a image in binary mode so that I could save it into my database, like this: img = open("Last_Dawn.jpg") t = img.read() save_to_db(t) This is working on Mac. But on Windows, what img.read() is incorrect. It's just a little out of the whole set. So my first question is: why code above doesn't work in Windows? And ...

Problem In Running easy_install.exe under Windows

Hello. I am running Python under windows. I face no problem in installing pysqlite package. C:\>c:\Python26\Scripts\easy_install.exe pysqlite Searching for pysqlite Reading http://pypi.python.org/simple/pysqlite/ ........ Download error: [Errno 11001] getaddrinfo failed -- Some packages may not be found! Reading http://initd.org/tracke...

SQLAlchemy sqlalchemy.sql.expression.select vs. sqlalchemy.sql.expression.Select

So I'm brand new to SQLAlchemy, and I'm trying to use the SQL Expression API to create a SELECT statement that specifies the exact columns to return. I found both a class and a function defined in the sqlalchmey.sql.expressions module and I'm not too sure which to use... Why do they have both a class and a function? When would you use on...

wxPython autocomplete

What editors or IDEs offer decent autocompletion for wxPython on Windows or Linux? Are there any? I tried several and support is either non-existant or limited. ...

How do I pass compressed data using the Task Queue Python API in App Engine?

I'm trying to use compressed data with my Tasks in the Task Queue like so: t = taskqueue.Task(url='/tasks/queue', params={'param': zlib.compress(some_string)} However when I try to decompress it in the queue handler like so message = self.request.get('param') message = zlib.decompress(message) I get this error: ...

m2crypto custom certificate verification

I need to build an encrypted connection between two peers, and I need to authenticate both. Both peers already share a fingerprint (SHA256 hash) of the other peer public key. I'm not using X509 or OpenPGP keys/certs as they are too big and bulky for my needs and they don't fit in the security model. I'm trying to build a connection with...

Adding an edge/node with a color attribute

I a using the networkx package of Python. The documentation says we can do H.add_edge(1,2,color='blue') but the output shows an edge with the default(black) color. When I do H.add_node(12,color='green') I get a new node with same default red color. ...

imports while starting an interactive shell

When I start the interactive django shell through manage.py, by executing python -v manage.py shell from the project directory, I see a lot of modules of format django.package.module getting imported in the verbose output but still I have to import them to use it in the shell. The same happens when I just run the Python shell (with...