python

Embedding Python thread safe

Hi everyone, I'm trying to use Python in a module for an analysis software of vehicle bus systems. For this I have to embed Python in a thread safe manner, since there can be multiple instances of the module witch work independently. I could use a mutex to guard all access to Python and create an unique (python) module for every thread...

Reading and interpreting data from a binary file in Python

Hi, i want to read a file byte by byte and check if the last bit of each byte is set: #!/usr/bin/python def main(): fh = open('/tmp/test.txt', 'rb') try: byte = fh.read(1) while byte != "": if (int(byte,16) & 0x01) is 0x01: print 1 else: print 0 ...

Alternative for PHP GD library in python

I was searching searching for a pure python module which has functionality equalent to PHP GD library. I need to write text on a image file. I know PHP GD library can do that. Does any one know about such module in python too. ...

Python Tkinter Return

Is there a way to return something when a button is pressed? Here is my sample program. a simple file reader. Is the global variable to hold text contents the way to go since I can't return the contents? from Tkinter import * import tkFileDialog textcontents = '' def onopen(): filename = tkFileDialog.askopenfilename() read(...

Does anyone has Python 3 Cheat Sheet

Does anyone has python 3 cheat sheet. You know quick reference kind of thing which has everything on one page. Thank you in advance ...

Buildbot parsing Python Unit test results

I have a test suite that outputs test results in the Python Unit Test format: http://docs.python.org/library/unittest.html Is there an existing Buildbot module/plugin that can parse this form? Example: DigitalReadWrite_02 ... ok DigitalReadWrite_03 ... ok...

Server Upgrade Script

Does anyone have or know of a good template / plan for doing automated server upgrades? In this case I am upgrading a python/django server, but am going to have to apply this update to many machines, and want to be sure that the operation is fully testable and recoverable should anything go wrong. Am picturing something along the lines...

GAE Query fetch()

I am trying to learn simple operations with the datastore and I am having problems. Can someone help why this is not working? class Pet(db.Model): name = db.StringProperty pet = Pet(name="Fluffy") pet.put() query = Pet.all() results = query.fetch(limit=5) print pet.name When I run this I get <class 'google.appengine.ext.db.St...

The dateutil.parser.parse() of distance strings?

Anyone know a python package that's the dateutil of distance strings? It would be great if something was out there that worked something like the following: >>> from awesome_dist_module import Distance >>> d = Distance("100 ft") >>> d.meters 30.48 >>> d = Distance("100 feet") >>> d.meters 30.48 >>> d.miles 0.0189393939 The key thing ...

Python unittests almost never check types

I was going through a few tests written in Java using JUnit and I could'nt help noticing the emphasis which is laid on checking the "type" of objects. This is something I have never seen in Python test-suites. Java being statically-typed and Python being dynamically-typed, should'nt the reverse be the case? ...

Get Text From All Elements Matching a Pattern in Selenium

I have a site with elements of the form: <td id="subject_23432423">content I want to read</td> How do I use Selenium RC (with the Python bindings specifically) to read the content from all these elements? I've gone through all the commands, and while there's a lot of options to find a single element, none of the commands seem to handl...

Psycopg2 using wildcard causes TypeError

Currently I am attempting to search a database to grab certain events. My query is as such SELECT * FROM events WHERE summary ILIKE E'%test%' AND start_time > '2010-10-01' Simply put I need the query to look through a database of calendar events and return anything with a summary with 'test' in it and after the beginning of this month...

how to find out whether website is using cookies or http based authentication

I am trying to automate files download via a webserver. I plan on using wget or curl or python urllib / urllib2. Most solutions use wget and urllib and urllib2. They all talk of HHTP based authentication and cookie based authentication. My problem is I dont know which one is used in the website that stores my data. Here is the interact...

Testing user input against a list in python

Hi, i need to test if the user input is the same as an element of a list, right now i'm doing this cars = ("red","yellow","blue") guess = str(input()) if guess == cars[1] or guess == cars[2]: print ("success!") But i'm working with bigger lists and my if statement is growing a lot with all those checks, is there a way to refe...

List of substrings

I have big string, it can have a few thousands lines. I would like to to get all sub-strings like: [tag] here can be everything [/tag] in a list. How can I do this? My regex is not working (or I'm doing something wrong). ...

Casting an int to a string in Python

I want to be able to generate a number of text files with the names fileX.txt where X is some integer: for i in range(key): filename = "ME" + i + ".txt" //Error here! Can't concat a string and int filenum = filename filenum = open(filename , 'w') Does anyone else know how to do the filename = "ME" + i part so I get a li...

Searching a normal query in an inverted index

I have a full inverted index in form of nested python dictionary. Its structure is : {word : { doc_name : [location_list] } } For example let the dictionary be called index, then for a word " spam ", entry would look like : { spam : { doc1.txt : [102,300,399], doc5.txt : [200,587] } } so that, the documents containing...

Reading and Writing a new line from a file to another in Python

I'm trying to read from a file and write into another. The problem arises when I'm trying to preserve newlines from the original file to the new one. def caesar_encrypt(orig , shift): enctextCC = open("CCencoded.txt" , 'w') for i in range(len(orig)): for j in range(len(orig[i])): curr = orig[i][j] if ord(cu...

How to extract office embedded OLE files under Linux, Nativly (Python,C,Java)?

I am trying to extract Excel Documents which embedded inside word document as OLE but its failing hard. I need to put it in server side script so console or script is necessary. And automating Open Office is very resource hungry .. Is there any tool or libraries to do this ? Please help.. ...

Problems with Tkinter in py2exe

I made a a simple GUI program in python with tkinter and attempted to convert it to an .exe using py2exe. However, I've run into a problem. When I try to run the exe it flashes an error very quickly then disapears. So the best I could do was take a screan shot of the error. How do I go about fixing this? Edit Velociraptors, this is...