python

Create decorator that can see current class method

Can you create a decorator inside a class that will see the classes methods and variables? The decorator here doesnt see: self.longcondition() class Foo: def __init__(self, name): self.name = name # decorator that will see the self.longcondition ??? class canRun(object): def __init__(self, f): ...

Using sessions in Django

I'm using sessions in Django to store login user information as well as some other information. I've been reading through the Django session website and still have a few questions. From the Django website: By default, Django stores sessions in your database (using the model django.contrib.sessions.models.Session). Though this ...

Modifying Python code to use SSL for a REST call

I have Python code to call a REST service that is something like this: import urllib import urllib2 username = 'foo' password = 'bar' passwordManager = urllib2.HTTPPasswordMgrWithDefaultRealm() passwordManager .add_password(None, MY_APP_PATH, username, password) authHandler = urllib2.HTTPBasicAuthHandler(passwordManager) opener...

"Matrix decomposition" of a matrix with holonic sub structure

Before I start, I must say that for those with a background of linear algebra, this is NOT matrix decomposition as you know it. Please read the following paragraphs to get a clearer understanding of the problem I am trying to solve. Here are the salient properties/definitions of the matrix and its submatrices: I have an SxP matrix whi...

Tkinter button command activates upon running program?

I'm trying to make a build retrieval form, and seem to have issues with the buttons... I'm a novice at Python/tkinter GUI programming (and GUI programming in general) and borrowed the skeleton of a Hello World app, and sorta built off that. In the code below, I've set the "command" option of my Browse button to call my class's internal ...

In Python small floats tending to zero

Hey! I couldn't find an answer to this problem so I'm asking it here: I have a Bayesian Classifier programmed in Python, the problem is that when I multiply the features probabilities I get VERY small float values like 2.5e-320 or something like that, and suddenly it turns into 0.0. The 0.0 is obviously of no use to me since I must find...

Use py2app with Matplotlib and its Tex formatting? Dvipng not found.

I have an application put together in py2app on OS X 10.6 which uses Matplotlib to generate graphs. (Using py2app version 0.5.3 and matplotlib version 0.99.3, if it matters.) I have the Tex formatting option enabled: ... from matplotlib import rc rc('text', usetex=True) ... The script works fine when executed in the command line, incl...

Why can't I use Cocoa classes from my Python script?

Today is the first time I've used Python, so I'm sure this'll be an easy question. I need to convert this Python script from a command line application: webkit2png. The end result will be a URL that returns an image of the webpage passed into it as a querystring param. I've achieved this on Windows with .NET and IE, Gecko and WebKit, bu...

Need to remove duplicates from a list of dictionaries and alter data for the remaining duplicate (python)

Consider this short python list of dictionaries (first dictionary item is a string, second item is a Widget object): raw_results = [{'src': 'tag', 'widget': <Widget: to complete a form today>}, # dupe 1a {'src': 'tag', 'widget': <Widget: a newspaper>}, # dupe 2a {'src': 'zip', 'widget': <Widget: to co...

Django, Python, trying to change field values / attributes in object retrieved from DB objects.all call, not working, advice appreciated

I'm trying to change a specific field from a field in an object that I retrieved from a django db call. class Dbobject () def __init__(self): dbobject = Modelname.objects.all() def test (self): self.dbobject[0].fieldname = 'some new value' then I am able to access a specific attribute like so: objclass = Dbobject(...

How can I use bulkuploader to populate class with a db.SelfReferenceProperty?

I've got a class that is using db.SelfReferenceProperty to create a tree-like structure. When trying to populate the database using appcfg.py upload_data -- config_file=bulkloader.yaml --kind=Group --filename=group.csv (...) , I'm getting an exception saying BadValueError: name must not be empty. (Full stack below) I tried ordering th...

Replace non-ascii chars from a unicode string in Python

How can I replace non-ascii chars from a unicode string in Python? This are the output I spect for the given inputs: música -> musica cartón -> carton caño -> cano Myaybe with a dict where 'á' is a key and 'a' a value? ...

Python ctypes - Accessing data string in Structure .value fails

I am able to get a Structure populated as a result of a dll-function (as it seems looking into it using x=buffer(MyData) and then repr(str(buffer(x))) ) But an error is raised if I try to access the elements of the Structure using .value I have a VarDefs.h that requires a struct like this: typedef struct { char Var1[8+1]; char...

Beautifulsoup, Python and HTML automatic page truncating?

Hello, I'm using Python and BeautifulSoup to parse HTML pages. Unfortunately, for some pages (> 400K) BeatifulSoup is truncating the HTML content. I use the following code to get the set of "div"s: findSet = SoupStrainer('div') set = BeautifulSoup(htmlSource, parseOnlyThese=findSet) for it in set: print it At a certain point, th...

Python - Way to restart a for loop, similar to "continue" for while loops?

Basically, I need a way to return control to the beginning of a for loop and actually restart the entire iteration process after taking an action if a certain condition is met. What I'm trying to do is this: for index, item in enumerate(list2): if item == '||' and list2[index-1] == '||': del list2[index] *<some ...

How does django one-to-one relationships map the name to the child object?

Hi Apart from one example in the docs, I can't find any documentation on how exactly django chooses the name with which one can access the child object from the parent object. In their example, they do the following: class Place(models.Model): name = models.CharField(max_length=50) address = models.CharField(max_len...

How to clear cookies in WebKit?

Hello ;) i'm currently working with PyWebKitGtk in python (http://live.gnome.org/PyWebKitGtk). I would like to clear all cookies in my own little browser. I found interesting method webkit.HTTPResponse.clearCookies() but I have no idea how to lay my hands on instance of HTTPResponse object :/ I wouldn't like to use java script for that...

Create console in python

Hi, I'm looking to have the same functionality (history, ...) as when you simply type python in your terminal. The script I have goes through a bunch of setup code, and when ready, the user should have a command prompt. What would be the best way to achieve this? ...

uploading empty file to ftp with psuedo-file

Hey all, As far as i know it is impossible to create an empty file with ftp, you have to create an empty file on the local drive, upload it, then delete it when you are done. I was wondering if it is possible to do something like: class FakeFile: def read(self): return '\x04' ftpinstance.storbinary('stor fe', FakeFile()) ...

Best way to create a "reversed" list in Python?

In Python, what is the best way to create a new list whose items are the same as those of some other list, but in reverse order? (I don't want to modify the existing list in place.) Here is one solution that has occurred to me: new_list = list(reversed(old_list)) It's also possible to duplicate old_list then reverse the duplicate in ...