python

How to pack and unpack using ctypes (Structure <-> str)

This might be a silly question but I couldn't find a good answer in the docs or anywhere. If I use struct to define a binary structure, the struct has 2 symmetrical methods for serialization and deserialization (pack and unpack) but it seems ctypes doesn't have a straightforward way to do this. Here's my solution, which feels wrong: fr...

django admin permissions - can edit user but can't edit his permissons - how to do it?

I gave the editors such permissions: auth | user | can add/change user - ON auth | permissions | can add/change permissions - OFF Still, when editing, they can change their permissions (and allow themselves actions they shouldn't do). I've found a ticket from 2yrs ago: http://code.djangoproject.com/ticket/6519 and it still works this...

How much of NumPy and SciPy is in C?

Python newbie here. Are parts of NumPy and/or SciPy programmed in C/C++? And how does the overhead of calling C from Python compare to the overhead of calling C from Java and/or C#? I'm just wondering if Python is a better option than Java or C# for scientific apps. If I look at the shootouts, Python loses by a huge margin. But I gue...

Is SOAPpy the same thing as SOAPy?

I would normally read the documentation to figure that out, but the links from both websites are on sourceforge and both are 404ing. ...

Suppress linebreak on file.write

When writing to a text file, some of the file.write instances are followed by a linebreak in the output file and others aren't. I don't want linebreaks except where I tell them to occur. Code: for doc,wc in wordcounts.items(): out.write(doc) #this works fine, no linebreak for word in wordlist: if word ...

Passing a list to eval()

Is there a way to pass a list as a function argument to eval() Or do I have to convert it to a string and then parse it as a list in the function? My simple example looks like: eval("func1(\'" + fArgs + "\')") I'm just not sure if there is a better way of taking fArgs as a list instead of a string Note: The list is provided from a ...

The best way to join two dissimilar mySQL tables -- planning for django from python.

Hi all, table a (t_a): id name last first email state country 0 sklass klass steve [email protected] in uk 1 jabid abid john [email protected] ny us 2 jcolle colle john [email protected] wi us table b (t_b): id sn given nick email l c 0 steven klass ...

2 Axes Reportlab Graph

Hi, I have managed to create a 2 axes graph in ReportLab, by overlapping a barchart and linepot. Here is the code for anyone interested in something similar: from reportlab.graphics.shapes import Drawing,colors from reportlab.graphics.widgets.markers import makeMarker from reportlab.graphics.charts.barcharts import VerticalBarChart fro...

os.kill not raising an OSError, however I do not see the given pid running

On my ubuntu server I run the following command: python -c 'import os; os.kill(5555, 0)' This is done so that I can see if pid 5555 is running. From my understanding this should raise an OSError if the pid is not running. This is not raising an OSError for me which means it should be a running process. However when I run: ps aux |...

Outgoing load balancer

I have a big threaded feed retrieval script in python. My question is, how can I load balance outgoing requests so that I don't hit any one host too often? This is a big problem for feedburner, since a large percentage of sites proxy their RSS through feedburner and to further complicate matters many sites will alias a subdomain on the...

Numpy meshgrid in 3D

Numpy's meshgrid is very useful for converting two vectors to a coordinate grid. What is the easiest way to extend this to three dimensions? So given three vectors x, y, and z, construct 3x3D arrays (instead of 2x2D arrays) which can be used as coordinates. ...

Django creating a form field that's read only using widgets

My form field looks something like the following: class FooForm(ModelForm): somefield = models.CharField( widget=forms.TextInput(attrs={'readonly':'readonly'}) ) class Meta: model = Foo Geting an error like the following with the code above: init() got an unexpected keyword argument 'widget' I thought this is a...

how to find list of modules which depend upon a specific module in python

In order to reduce development time of my Python based web application, I am trying to use reload() for the modules I have recently modified. The reload() happens through a dedicated web page (part of the development version of the web app) which lists the modules which have been recently modified (and the modified time stamp of py file ...

In python how can I check to see if an object has a value?

Base Account class BaseAccount(models.Model): user = models.ForeignKey(User, unique=True) def __unicode__(self): """ Return the unicode representation of this customer, which is the user's full name, if set, otherwise, the user's username """ fn = self.user.get_full_name() if fn: return fn return sel...

reading mails using python

how do i read mails from my mail box using python?? import getpass, imaplib M = imaplib.IMAP4('IMAP4.gmail.com:993') M.login(getpass.getuser(), getpass.getpass()) M.select() typ, data = M.search(None, 'ALL') for num in data[0].split(): typ, data = M.fetch(num, '(RFC822)') print 'Message %s\n%s\n' % (num, data[0][1]) M.close() M....

Python syntax highlighting / Intellisense?

I've started messing around with Google App Engine, writing Java. I love Visual Studio for many reasons, but currently my .py files just look like text. I've searched the web looking for a way to get it to highlight python files (intellisense would be a nice bonus, but not required) but turned up nothing. There are other questions on ...

how to reference python package when filename contains a period

I am using django and I have a file named models.admin.py and I want to do the following idea in models.py: from "models.admin" import * however, I get a syntax error for having double quotes. But if I just do from models.admin import * then I get "ImportError: No module named admin" Is there any way to import from a python file ...

Optimized dot product in Python

The dot product of two n-dimensional vectors u=[u1,u2,...un] and v=[v1,v2,...,vn] is is given by u1*v1 + u2*v2 + ... + un*vn. A question posted yesterday encouraged me to find the fastest way to compute dot products in Python using only the standard library, no third-party modules or C/Fortran/C++ calls. I timed four different approach...

Any way to make nice antialiased round corners for images in python?

Is there any way to make nice round corners with python? Currently PIL and GD2 are used in my project. Both of them have an arc() method, that allows you to draw a quater-circle, but the quater-circle is not antialiased, so the image looks crispy. Is there any neat way to make antialiased/smooth round corners? ...

Why do I see "cannot import name descriptor_pb2" error when using Google Protocol Buffers?

When using the generated Python code from our protobuf classes, we get this error: cannot import name descriptor_pb2 The equivalent C++ generated code works just fine, so it would appear that there is no problem with our actual proto definitions. This error occurs when I try and import our class, like so: import sys sys.path.append(...