python

How to avoid gcc warning in Python C extension when using Py_BEGIN_ALLOW_THREADS

The simplest way to manipulate the GIL in Python C extensions is to use the macros provided: my_awesome_C_function() { blah; Py_BEGIN_ALLOW_THREADS // do stuff that doesn't need the GIL if (should_i_call_back) { Py_BLOCK_THREADS // do stuff that needs the GIL Py_UNBLOCK_THREADS } Py_E...

twisted http client

I am after an example describing the usage of Twisted's HTTP Client. After reading the excellent blog post on the internals of Twisted, I understand how the "Factory" and "Protocol" components play their role but I am unclear on how to introduce "Request" in the overall Client flow. More specifically, I need to be able to perform HTTP...

Should a class or method which processes a file close the file as a side effect?

I'm wondering which is the more 'Pythonic' / better way to write methods which process files. Should the method which processes the file close that file as a side effect? Should the concept of the data being a 'file' be completely abstracted from the method which is processing the data, meaning it should expect some 'stream' but not nece...

Reading XML DOCTYPE info with Python

I need to parse a version of an XML file as follows. <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE twReport [ <!ELEMENT twReport (twHead?, (twWarn | twDebug | twInfo)*, twBody, twSum?, twDebug*, twFoot?, twClientInfo?)> <!ATTLIST twReport version CDATA "10,4"> <----- VERSION INFO HERE I use xml.dom.minidom for pa...

Connecting to a Multicast Server in Python

Hello This is my code for connecting to a multicast server, is this the best way of handling the exception. What I would like to do is to retry to connect if an exception occurs def initialiseMulticastTrackerComms(): try: sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.setsockopt(socket.IPPROTO_IP, socket.IP_MUL...

Using getcwd when running in Eclipse

Hi, When running a project in eclipse the eclipse saves a copy of the .py file inside the workspace defined in project creation. But the file that imported to project can be in other location. If using os.getcwd() and running the script from the command line, the return value would be the real path of the script file, but when running ...

How do I change which version of python mod_python uses

I'm doing some introductory work with django which seems really easy (and fun) so far but I have been doing all this from Python 2.6 which I installed in /opt/local (RedHat 5.3) because the python that came with redhat was 2.4. I set up a symlink: /usr/bin/python2.6 -> /opt/local/bin/python and I have been using that for all the djang...

Django: serializing list to json

I am sending information between client and django server and I would like to use json to this. I am sending simple information - list of strings. I tried using django.core.serializers, but when I did, I got AttributeError: 'str' object has no attribute '_meta' It seems, this can be used only for django objects. How can I serialize si...

Using KWallet in PyQt4.

It would be great if anyone could show me how to use KWallet with pyqt4 ...

Dll loaded twice with Dll redirection through manifest

Hi there, I'm including python.h in my VC++ dll project which causes an implicit linking with python25.dll. However I want to load a specific python25.dll (several can be present on the computer) so I created a very simple manifest file named test.manifest: <?xml version='1.0' encoding='UTF-8' standalone='yes'?> <assembly xmlns='urn:sc...

Emptying the datastore in GAE

I know what you're thinking, 'O not that again!', but here we are since Google have not yet provided a simpler method. I have been using a queue based solution which worked fine: import datetime from models import * DELETABLE_MODELS = [Alpha, Beta, AlphaBeta] def initiate_purge(): for e in config.DELETABLE_MODELS: deferre...

How do I represent and work with n-bit vectors in Python?

In an assignment I am currently working on we need to work with bit vectors, but I am very unsure of how to do this in Python. They should be able to be from 4 bits to 20 bits. I have never worked with bit vector before, but I guess that one would one create arrays of unsigned bytes that you manipulated using the usual AND/OR/XOR operati...

How to retrieve the values for only one table field in Django

If I have following database fields: id, name, emp_id. How do I make a query in Django to get the values of column name only with a where clause. Thanks... ...

Is there a library for python that will generate hierarchy/tree diagrams?

The title kind of says it all really. Matplotlib is fantastic for most graphing applications, but I can't seem to find anything that will make hierarchy or tree diagrams. Does anyone have experience with a package they would wish to recommend? Thanks in advance for your time, Dan ...

Looking for the fastest way to find the exact overlap between two arrays of equal length in numpy

I am looking for the optimal (fastest) way to find the exact overlap between two arrays in numpy. Given two arrays x and y x = array([1,0,3,0,5,0,7,4],dtype=int) y = array([1,4,0,0,5,0,6,4],dtype=int) What I want to get is, an array of the same length that contains only the numbers from both vectors that are equal: array([1,0,0,0,5,0...

how to convert a xml string to a dictionary in python

Hello guys, I've a program that reads a xml document from a socket, so I've the xml document stored in a string which I would like to convert directly to a python dictionary, the same way it is done in django's simplejson library. Take as an example: str ="<?xml version="1.0" ?><person><name>john</name><age>20</age></person" dic_xml = ...

Virtualenv problem

I have created a new environement in virtualenv with --no-site-packages and executed activate file. So, shouldn't my current Django app show any error? Environement doesn't have Django installed. I think, my site is using my old python with Django. How can I change it? Maybe it's because my .htaccess file, here it is: SetHandler mod_pyt...

Errors with python-openid and Google Apps Federated Login

UPDATE I managed to get it working although I'm not quite sure why ;) It seems like python-openid uses a POST-request to issue the openid mode=associate and for some reason Google doesn't like that. When I patched python-openid to use a GET-request instead everything worked fine. I'll continue my investigation and update this post when ...

Python: Embed Chaco in PyQt4 Mystery

How do i go about adding Chaco to an existing PyQt4 application? Hours of searches yielded little (search for yourself). So far i've figured i need the following lines: import os os.environ['ETS_TOOLKIT']='qt4' i could not find PyQt4-Chaco code anywhere on the internets i would be very grateful to anyone filling in the blanks to sho...

Python: Rar Brute Forcer

Hello, I am trying to brute force a RAR archive which is protected by a password with 3 characters: import os Alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" for a in range(0,26): for b in range(0,26): for c in range(0,26): Brute = Alphabets[a] + Alphabets[b] + Alphabets[c] os.popen4("Rar.exe x -p" + Brute + " Protected.rar") # raw...