python

Python scoping and threading question

I have one thread that inserts into the queueStream (not shown here) and FlowController which is another thread that pops from the queue if the queue is not empty. I verified that the data is inserted into the queue correctly with the debug code in addToQueue() Problem is, the 'if queueStream' statement in FlowController always sees th...

Adding model-wide help text to a django model's admin form

In my django app, I would like to be able to add customized help text to the admin change form for some of my models. Note I'm not talking about the field specific help_text attribute that I can set on individual fields. For example, at the top of the change form for My_Model in My_App I'd like to be able to add some HTML that says "Fo...

Python Titlecase a String with exceptions

Is there a standard way in Python to titlecase a string (i.e. words start with uppercase characters, all remaining cased characters have lowercase) but leaving articles and like lowercased? ...

Uses of self referencing lists

Hello, I know it is possible to create a self referencing list in languages like Python: >>> my_list = [1,2] >>> my_list.append(my_list) >>> print my_list [1,2,[...]] >>> print my_list[0] 1 >>> print my_list[2] [1,2,[...]] What algorithms benefit from self referencing lists? I cannot think of one. Thanks. ...

Python: Execute a command in a subshell without cmd interface or hidden or in background

Hello all, I would like to know how I could execute a command whitout appears the cmd window. My code is in Python and the O.S. is Windows7. The problematic line is: os.system(pathandarguments) The program works fine, execute the given path with the arguments but I loose the control of my program because my program window minimizes, I ...

Custom standard input for python subprocess.

Hello, I'm running an SSH process like this: sshproc = subprocess.Popen([command], shell=True) exit = os.waitpid(sshproc.pid, 0)[1] This works and opens an interactive terminal. Based on the documentation for subprocess, sshproc is using the script's sys.stdin. The question is: how can I print to stderr or a file what input is bein...

How can I programmatically change the argspec of a function in a python decorator?

Given a function: def func(f1, kw='default'): pass bare_argspec = inspect.getargspec(func) @decorator def func2(f1, kw='default'): pass decorated_argspec = inspect.getargspec(func2) How can I create a decorator such that bare_argspec == decorated_argspec? (As to why, the framework that calls the decorated function does argsp...

Conditional Class Creation (Python)

From the tutorial: "A class definition is an executable statement." Is the following recommended in a script? my_switch = False if my_switch: class Hello: def __init__(self): self.greeting = "Hello!" else: class Hello: def __init__(self): self.greeting = "Salut!" ...

a list of pygame sprites loses its first element, and gains a duplicate of the last

I have a function that loads a sprite sheet, finds a block of sprites, and then puts each individual sprite into a list. Before it appends a sprite into the list, it will blit it onto the screen. Once it's done loading sprites, it will then iterate through the list, blitting each sprite as it goes. The two sets of blits should be iden...

Issue with Django admin registering an inline user profile admin

I'm currently working on a django project. I'm attempting to add a UserProfile model inline to my User model. In my models.py I have: class UserProfile(models.Model): ''' Extension to the User model in django admin. ''' user = models.ForeignKey(User) site_role = models.CharField(max_length=128, choices=SITE_ROLE) ...

How to update $PATH

I am writing a python/pygtk application that is adding some custom scripts (bash) in a certain folder in $HOME (eg. ~/.custom_scripts). I want to make that folder available in $PATH. So every time the python app is adding the script, that script could be instantly available when the user is opening a terminal (eg. gnome-terminal). Wh...

QSortFilterProxyModel returning artificial row

I'm using a QSortFilterProxyModel to filter results from a QAbstractListModel. However, I'd like to return a first entry which is not present in the original model, that is, it's somehow artificial. This is what I have so far: class ActivedAccountModel(QSortFilterProxyModel): ...

need Advice on ID3 implementation and Datatype to be used

Need advice: I am implementing ID3 algorithm in Machine Learning. I am using dictionary to read the training file and store into. But as I am going forward I am understanding that in dictionary v dont have fixed places for each key,value pair as in list or array. Now I might have problem in getting the position of the final attribute and...

How can I disable a third party API when executing Django unit tests?

I'm trying to build some unit tests against some code I have in Django that runs operations against a third party API. Specifically, I'm synchronizing some user data with MailChimp and using a library that implements the MailChimp API. I have a custom class MailChimpAPI that essentially acts as a thin wrapper around the Python library I...

Why can't I "save as" an Excel file from my Python code?

I have an Python ExcelDocument class that provides basic convenience methods for reading/writing/formatting Excel files, and I'm getting a strange error in seemingly simple Python code. I have a save and saveAs method: def save(self): ''' Save the file ''' self.workbook.Save() def saveAs(self, newFileName): ''' Save the file a...

Using a loop to generate unique bitmap buttons with separate events when clicked.

I'm pretty new to Python, so I'll hope you forgive me for such amateurish code. I've tried pouring over examples that do similar things but I'm having trouble figuring out what they're doing that is different. In examples I've seen each button generated with the loop had a different action, for mine only the last button in the loop is af...

Appengine - how to get an entity and display values

I'm having trouble with my project. I have 2 models class UserPrefs(db.Model): user = db.UserProperty() name = db.StringProperty() class Person(db.Model): name = db.StringProperty() phone = db.PhoneNumberProperty() userPrefs = db.ReferenceProperty(UserPrefs) class PersonHandler(webapp.RequestHandler): def get...

Error Importing Graphics Python

I'm typing at the console from graphics import * and I get this error Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> from graphics import * ImportError: No module named graphics ...

Trying to group similar text rows in a column of a million row table - Open to Non-MySQL approaches

I have a large number (about 40 million) of VARCHAR entries in a MySQL table. The length of the string can be anywhere between 5-80 characters. I am trying to group similar text together and thought of a possible approach: Take a row and calculate a similarity measure (like Edit Distance) with every other row and decide (I am not sure h...

Equivalent of objects.latest() in App Engine

Hi, What would be the best way to get the latest inserted object using AppEngine ? I know in Django this can be done using MyObject.objects.latest() in AppEngine I'd like to be able to do this class MyObject(db.Model): time = db.DateTimeProperty(auto_now_add=True) # Return latest entry from MyObject. MyObject.all().latest() An...