python

Comprehensive guide to Operator Overloading in Python

Is there a comprehensive guide to operator overloading anywhere? Preferably online, but a book would be fine too. The description of the operator module leaves a lot out, such as including operators that can't be overloaded and missing the r operators or providing sensible defaults. (Writing these operators is good practice, but still be...

Is there a memory efficient and fast way to load big json files in python?

I have some json files with 500MB. If I use the "trivial" json.load to load its content all at once, it will consume a lot of memory. Is there a way to read partially the file? If it was a text, line delimited file, I would be able to iterate over the lines. I am looking for analogy to it. Any suggestions? Thanks ...

Python: How do I create sequential file names?

I want my program to be able to write files in a sequential format, ie: file1.txt, file2.txt, file3.txt. It is only meant to write a single file upon execution of the code. It can't overwrite any existing files, and it MUST be created. I'm stumped. ...

Python - Differences between elements of a list

Given a list of numbers how to find differences between every (i)-th and (i+1)-th of its elements? Should one better use lambda or maybe lists comprehension? Example: Given a list t=[1,3,6,...] it is to find a list v=[2,3,...] because 3-1=2, 6-3=3, etc. ...

Why subprocess.Popen doesn't work when args is sequence?

I'm having a problem with subprocess.Popen when args parameter is given as sequence. For example: import subprocess maildir = "/home/support/Maildir" This works (it prints the correct size of /home/support/Maildir dir): size = subprocess.Popen(["du -s -b " + maildir], shell=True, stdout=subprocess.PIPE).commu...

Why does this python queue code process items multiple times?

The following is a testcase I created. Why does every process print the number 1 to 5 and are the numbers not divided over the processes? code: #!/usr/bin/python from subprocess import * from Queue import Queue from Queue import Empty import multiprocessing from multiprocessing import Process def main(): r = Runner() r.run()...

Python Vector Class

I'm coming from a C# background where this stuff is super easy—trying to translate into Python for Maya. There's gotta' be a better way to do this. Basically, I'm looking to create a Vector class that will simply have x, y and z coordinates, but it would be ideal if this class returned a tuple with all 3 coordinates and if you could edi...

Python: Help with counters and writing files

I was suggested to use a separate file as a counter to give my files sequential file names, but I don't understand how I would do that. I need my file names to have sequential numbers, like file1.txt, file2.txt, file3.txt. Any help is appreciated! Edit: My mistake, I forgot to say that the code makes 1 file when it's executed, and needs...

Importing Python module from the Bash

I am launching a Python script from the command line (Bash) under Linux. I need to open Python, import a module, and then have lines of code interpreted. The console must then remain in Python (not quit it). How do I do that? I have tried an alias like this one: alias program="cd /home/myname/programs/; python; import module; line_of_c...

How to digitally sign a message with M2Crypto using the keys within a DER format certificate

Hi everyone. I am working on a project to implement digital signatures of outgoing messages and decided to use M2Crypto for that. I have a certificate (in DER format) from which I extract the keys to sign the message. For some reason I keep getting an ugly segmentation fault error when I call the "sign_update" method. Given the previo...

Python recursive function error: "maximum recursion depth exceeded"

I solved Problem 10 of Project Euler with the following code, which works through brute force: def isPrime(n): for x in range(2, int(n**0.5)+1): if n % x == 0: return False return True def primeList(n): primes = [] for i in range(2,n): if isPrime(i): primes.append(i) retu...

What would be the most efficient way to do this search (mysql or text)?

Suppose I have 500 rows of data, each with a paragraph of text (like this paragraph). That's it.I want to do a search that matches part of words. (%LIKE%, not FULL_TEXT) What would be faster? SELECT * FROM ...WHERE LIKE "%query%"; This would put load on the database server. Select all. Then, go through each one and do .find >= 0 This ...

Game cross-compiling and packaging

Hi, Some friends and I wanted to develop a game. Any language will do. I've been programming in C for years, but never wrote a game before. One of us knows SDL a little bit. It would also be a nice excuse to learn Python+pygame. We wish our game to be 'standalone'. By standalone, I mean most users (at least Linux, Mac and Windows) will...

How to run python scripts on your server?

Hi, I have mod_python installed on my server, but if I want to acceses a python script - let's say httü://site.com/something.py the script doesn't run, the download box "pops up" Any solutions? ...

open file in "w" mode: IOError: [Errno 2] No such file or directory

When i try to open a file in write mode with the following code: packetFile = open("%s/%s/%s/%s.mol2" % ("dir", "dir2", "dir3", "some_file"), "w") Gives me the following error: IOError: [Errno 2] No such file or directory: 'dir/dir2/dir3/some_file.mol2' Note that the "dir/dir2/dir3/" does exist! Also the "w" mode should create the fi...

In Python, can I single line a for loop over iterator with an IF filter?

Silly question: I have a simple for loop followed by a simple if statement: for airport in airports: if airport.is_important: and I was wondering if I can write this as a single line somehow. So, yes, I can do this: for airport in (airport for airport in airports if airport.is_important): but it reads so silly and redundant...

Unittesting aspect-oriented features.

Hi, I'd like to know what would you propose as the best way to unit test aspect-oriented application features (well, perhaps that's not the best name, but it's the best I was able to come up with :-) ) such as logging or security? These things are sort of omni-present in the application, so how to test them properly? E.g. say that I'm...

How to get item count from list in python?

['a','a','b','c','c','c'] to [2, 2, 1, 3, 3, 3] and {'a': 2, 'c': 3, 'b': 1} ...

Do you know a Python module to read Universal File Format (UFF)?

Google wasn't my friend this time. This is the closest to a doc I can find : http://go-ci.com/note/new/note_401.html. The guys who created that should have something at http://sdrl.uc.edu/UFF2/58.asc but it's a 404. UFF is apparently a format used by scientific softwares. This is not widely used but some old handcrafted programs requi...

Google App Engine UI Widgets

Are there any UI widgets available to the python side of Google App Engine? I'd like something like the collapsed/expanded views of Google Groups threads. Are these type things limited to the GWT side? ...