python

Python: No csv.close()??

I'm using the CSV module to read a tab delimited file. Code below: z = csv.reader(open('/home/rv/ncbi-blast-2.2.23+/db/output.blast'), delimiter='\t') But when I add Z.close() to end of my script i get and error stating "csv.reader' object has no attribute 'close'" z.close() So how do i close "Z"? ...

How do you pass a Queue reference to a function managed by pool.map_async()?

I want a long-running process to return its progress over a Queue (or something similar) which I will feed to a progress bar dialog. I also need the result when the process is completed. A test example here fails with a RuntimeError: Queue objects should only be shared between processes through inheritance. import multiprocessing, time ...

Shared pointers and building in SIP4 (was: Dynamic casting in SWIG/python?)

So I'm playing about with Python, C++0x, and SWIG 2.0. I've got a header that looks like this: #include <string> #include <iostream> #include <memory> using namespace std; struct Base { virtual string name(); int foo; shared_ptr<Base> mine; Base(int); virtual ~Base() {} virtual void doit(shared_ptr<Base> b) { cout << ...

Why am I getting this error when I try writing a client for soaplib?

Traceback (most recent call last): File "", line 1, in NameError: name 'HelloWorldService' is not defined I am following the example at http://github.com/jkp/soaplib by writing the following code: from soaplib.client import make_service_client client = make_service_client('http://localhost:7789/',HelloWorldService()) ...

Beginner Python Practice?

Well just getting into the flow of thing with Python. Reading a few books, finding it fairly easy as I already have some experience with C++/Java from school and Python is definetly my favorite thus far. Anyway, I am getting a whole bunch of information on python, but haven't been putting it to much use. Thus, what I was wondering was i...

Python: Searching/reading binary data

I'm reading in a binary file (a jpg in this case), and need to find some values in that file. For those interested, the binary file is a jpg and I'm attempting to pick out its dimensions by looking for the binary structure as detailed here. I need to find FFC0 in the binary data, skip ahead some number of bytes, and then read 4 bytes (...

How to get root premissions for my app?

My app needs to do some privileged work. I've been looking everywhere, but I can't find anything useful. I know I want to use Policykit1 and dbus because all the other alternatives I've found aren't used anymore. This is the code I got so far: import dbus import os bus = dbus.SystemBus() proxy = bus.get_object('org.freedesktop.PolicyK...

Scapy SYN send on our own IP address

Hello, I tried to send SYN packets on my local network and monitoring them with Wireshark and everything works just fine, except when i try to send a packet to my own ip address it "seems" to work because it says Sent 1 packet, but it is not really sent, i can't see the packet in Wireshark nor any answers to the packet. My setup is a co...

How to get all the info in XML into dictionary with Python

Let's say I have an XML file as follows. <A> <B> <C>"blah"</C> <C>"blah"</C> </B> <B> <C>"blah"</C> <C>"blah"</C> </B> </A> I need to read this file into a dictionary something like this. dict["A.B1.C1"] = "blah" dict["A.B1.C2"] = "blah" dict["A.B2.C1"] = "blah" dict["A.B2.C2"] = "blah" But the format of the dict doesn...

Still wondering about directed graphs drawn from AppEngine

With reference to another case of pretty much the same question I have, Brightside asked: http://stackoverflow.com/questions/2264157/library-to-render-directed-graphs-similar-to-graphviz-on-google-app-engine The accepted answer was "canvis", which looks very cool from a rendering perspective, but canvis just does the drawing. It still n...

Filter foreignkey field from the selection of another foreignkey in django-admin?

hi, i have the next models class Region(models.Model): nombre = models.CharField(max_length=25) class Departamento(models.Model): nombre = models.CharField(max_length=25) region = models.ForeignKey(Region) class Municipio(models.Model): nombre = models.CharField(max_length=35) departamento = models.ForeignKey(Depar...

Why use argparse rather than optparse?

I noticed that the Python 2.7 documentation includes yet another command-line parsing module. In addition to getopt and optparse we now have argparse. Why has yet another command-line parsing module been created? Why should I use it instead of optparse? Are their new features I should know about? ...

Checking validity of email in django/python

I have written a function for adding emails to newsletter base. Until I've added checking validity of sent email it was working flawlessly. Now each time I'm getting "Wrong email" in return. Can anybody see any errors here ? The regex used is : \b[\w\.-]+@[\w\.-]+\.\w{2,4}\b and it is 100% valid (http://gskinner.com/RegExr/), but I may ...

Matplotlib: move graph to the right

I have two graphs with in one image, each with 5 points. Their value on the X axis is not important, all that I require is that they're all equally distributed on it. import matplotlib.pyplot as plt data = [43,51,44,73,60] data2 = [34,25,42,53,61] fig = plt.figure(1) ax = fig.add_subplot(111) ax.plot(data, '-o', color='#000000', lw=...

How to add __iter__ to dynamic type?

Source def flags(*opts): keys = [t[0] for t in opts] words = [t[1] for t in opts] nums = [2**i for i in range(len(opts))] attrs = dict(zip(keys,nums)) choices = iter(zip(nums,words)) return type('Flags', (), dict(attrs)) Abilities = flags( ('FLY', 'Can fly'), ('FIREBALL', 'Can shoot fireballs'), ('IN...

What is the easiest way to access a a computers microphone in Python?

I need to get some numbers so I can generate random numbers using ambient sound. I want something on this level pseudo code: import microphone p = pitch.get() print p Edit: This is in Windows 7, BTW ...

Error in python: UnicodeEncodeError: 'gbk' codec can't encode character: illegal multibyte sequence

Hi, I want to get html content from a url and parse the html content with regular expression. But the html content has some multibyte characters. So I met the error described in title. Could somebody tell me how to resolve this problem? Thanks ...

Python list initialization (by ref problem)

I have some simple code that represents a graph using a square boolean matrix where rows/columns are nodes and true represents an undirected link between two nodes. I am initializing this matrix with False values and then setting the value to True where a link exists. I believe the way I am initializing the list is causing a single boo...

Import large chunk of data into Google App Engine Data Store at one go...

Hi I have a large CSV file, approx 10 MB in size, which contains all the data which need to be imported in the Google App Engine DataStore. I tried following approaches to perform import but all the times it failed in half way. Import using mapping a command to url and then executing url, failed because of request time out... Import...

How to pass a variable to a re.sub callback?

I am using a re.sub callback to replace substrings with random values, but I would like the random values to be the same across different strings. Since the re.sub callback does not allow arguments, I am not sure how to do this. Here is a simplified version of what I'm doing: def evaluate(match): mappings = {'A': 1, 'B': 2} re...