python

Atomic file write operations (cross platform)

How do I build up an atomic file write operation? The file is to be written by a Java service and read by python scripts. For the record, reads are far greater than writes. But the write happens in batches and tend to be long. The file size amounts to mega bytes. Right now my approach is: Write file contents to a temp file in same d...

IPC between python app and injected DLL

Hello stack overflow: Sometimes reader, first time poster. Background: Windows box running XP SP3, soon to be upgraded to Windows Seven (MSDNAA <3) I have an injected DLL which gets cycles by hooking a function that is called thousands of times a second. I would like to communicate/control this DLL via a python app. Basically, the ...

python CGI and JQUERY problem

I have a simple python CGI script where I query a MySQL database and then prints the result to the screen/webpage. My problem is that the "cursor.execute()" function returns a list of tuples. I use a simple for loop to iterate through this list and extract each tuple. This was working great until.....I got the bright idea to use jquery t...

How to get a function to execute

Hi all, I have a function and I want it to execute. Does anyone know how to do this? def a(): a = 'print' print a ...

Standard Regex vs python regex discrepancy

I am reading a book and they provide an example of how to match a given string with regular expressions. Here is their example: b*(abb*)*(a|∊) - Strings of a's and b's with no consecutive a's. Now I've tried converting it to python like so: >> p = re.compile(r'b*(abb*)*(a|)') # OR >> p = re.compile(r'b*(abb*)*(a|\b)') # BUT it still...

Why can't I pickle this object?

I have a class (below): class InstrumentChange(object): '''This class acts as the DTO object to send instrument change information from the client to the server. See InstrumentChangeTransport below ''' def __init__(self, **kwargs): self.kwargs = kwargs self._changed = None def _method_name(self, ...

Profile Python CPU Usage By Thread

I've got a multi-threaded Python application, and I'm currently troubleshooting very high (90% or more) CPU usage. I'm going to be trying out the profiler, but I wanted to see if there is a way I can get CPU usage per thread from within the application. I understand that os.times() will get CPU usage overall - is there something I can ...

AppEngine fetch through a free proxy

My (Python) AppEngine program fetches a web page from another site to scrape data from it -- but it seems like the 3rd party site is blocking requests from Google App Engine! -- I can fetch the page from development mode, but not when deployed. Can I get around this by using a free proxy of some sort? Can I use a free proxy to hide the...

Extract the fields of a C struct

I often have to write code in other languages that interact with C structs. Most typically this involves writing Python code with the struct or ctypes modules. So I'll have a .h file full of struct definitions, and I have to manually read through them and duplicate those definitions in my Python code. This is time consuming and error-...

Django: Unexpectedly persistent module variables

I noticed a strange behaviour today: It seems that, in the following example, the config.CLIENT variable stays persistent accross requests – even if the view gets passed an entirely different client_key, the query that gets the client is only executed once (per many requests), and then the config.CLIENT variable stays assigned. It does ...

python to Java checksum calculation

I received this python script that generates a file checksum: import sys,os if __name__=="__main__": #filename=os.path.abspath(sys.argv[1]) #filename=r"H:\Javier Ortiz\559-7 From Pump.bin" cksum=0 offset=0 pfi=open(filename,'rb') while 1: icks=0 chunk=pfi.read(256) if not chunk: break #if EOF exit loop for iter in chunk: ...

Bash or Python for extracting blocks from text files

Hi, I have a huge text file, which is structured as: SEPARATOR STRING1 (arbitrary number of lines) SEPARATOR ... SEPARATOR STRING2 (arbitrary number of lines) SEPARATOR SEPARATOR STRING3 (arbitrary number of lines) SEPARATOR .... What only changes between the different "blocks" of the file is the STRING and the content between the se...

Django/Python - Try/except problem

Hello, i have code like this: try: var = request.POST['var'] except NameError: var = '' Why always code after "except" is executing? Even if request.POST['var'] exist. ...

SQL queries through PYODBC fail silently on one machine, works on another

I am working on a program to automate parsing data from XML files and storing it into several databases. (Specifically the USGS realtime water quality service, if anyone's interested, at http://waterservices.usgs.gov/rest/WaterML-Interim-REST-Service.html) It's written in Python 2.5.1 using LXML and PYODBC. The databases are in Microso...

Appending the same string to a list of strings in Python

I am trying to take one string, and append it to every string contained in a list, and then have a new list with the completed strings. Example: list = ['foo', 'fob', 'faz', 'funk'] string = 'bar' *magic* list2 = ['foobar', 'fobbar', 'fazbar', 'funkbar'] I tried for loops, and an attempt at list comprehension, but it was garbage. As...

Python: fill out a form and confirm with a button click

Hello! Which do you think is the best method to fill out a form and confirm with clicking a button with Python? Do I have to use django? I want to do it in a simple way. Is there a library? Thanks in advance! ...

Change dynamically the contents of a matplotlib plot.

I while ago, I was comparing the output of two functions using python and matplotlib. The result was as good as simple, since plotting with matplotlib is quite easy: I just plotted two arrays with different markers. Piece of cake. Now I find myself with the same problem, but now I have a lot of pair of curves to compare. I initially tri...

Sending stdout as response from CGI spawned program

I'm trying to compose a .zip file in a CGI program and send that as the content response. I'm getting stuck in that whenever I spawn a program that prints to stdout, that somehow doesn't get accepted by Apache. It seems to be something to do with spawning a program that writes to stdout. The snippet below reproduces this problem. I ...

why is this an infinite loop in python?

I can't seem to figure out why this is an infinite loop in python?? for i in range(n): j=1 while((i*j)<n): j+=1 shouldn't the outer loop go n times. incrementing j until its equal to n div i each time? ...

Using Python struct.unpack with 1-byte variables

How can I use struct.unpack() or some other function available in Python to easily convert one byte variable to a Python integer? Right now, it is done in a rather lame way: file = open("telemetry.dat", "rb").read() magic = file[0] int(binascii.hexlify(magic), 16) Is there another? ...