python

In Python, are single character strings guaranteed to be identical?

The title really says it all. I read somewhere (an SO post, I think, and probably somewhere else, too), that Python automatically references single character strings, so not only does 'a' == 'a', but 'a' is 'a'. However, I can't remember reading if this is guaranteed behavior in Python, or is it just implementation specific? Bonus poi...

Check for presence of a sublist in Python

I want to write a function that determines if a sublist exists in a larger list. list1 = [1,0,1,1,1,0,0] list2 = [1,0,1,0,1,0,1] #Should return true sublistExists(list1, [1,1,1]) #Should return false sublistExists(list2, [1,1,1]) Is there a Python function that can do this? ...

Why does my setup.py script give this error?

So I have a C++ class that I made python wrappers for, and I made a setup.py file to compile it in order to use it in python. When I try to run python setup.py install I get the following error: lipo: can't create output file: build/temp.macosx-10.5-fat3-2.7/../tools/transport-stream/TransportStreamPacket_py.o (No such file or directory...

twisted.protocols.ftp.FTPClient and Deferreds

Like most it's taking me a while to get used to using Deferreds but I'm slowly getting there. However, it's not clear to me how I can process a response and then call another FTP command using the processed response when using Twisted's FTP module. I'm using the the example FTP code as my jumping off point. I want to connect to a FTP se...

Python package for Microsoft Active Accessibility library?

Is there a package for Microsoft Active Accessibility library other than http://pypi.python.org/pypi/pyAA/2.0 which seems to have been abandoned (I can't seem to get the source code from sourceforge )and does not support Python 2.6. Thanks. ...

Class design: Last Modified

My class: class ManagementReview: """Class describing ManagementReview Object. """ # Class attributes id = 0 Title = 'New Management Review Object' fiscal_year = '' region = '' review_date = '' date_completed = '' prepared_by = '' __goals = [] # List of <ManagementReviewGoals>. __objecti...

How to call a static methods on a django model class during a south migration

I'm writing a data migration in south to fix some denormalized data I screwed up in earlier code. The way to figure out the right value for the incorrect field is to call a static method on the django model class. The code looks like this: class Account(models.Model): name = models.CharField() @staticmethod def lookup_by_...

Dump Contents of Python Module loaded in memory

I ran the Python REPL tool and imported a Python Module. Can I can dump the contents of that Module into a file? Is this possible? Thanks in advance. ...

How do I do this replace regex in python?

Given a string of text, in Python: s = "(((((hi abc )))))))" s = "***(((((hi abc ***&&&&" How do I replace all non-alphabetic symbols that occur more than 3 times...as blank string For all the above, the result should be: hi abc ...

Creating an instance of a class, before class is defined?

Here's an example of my problem : class bar() : spam = foo() def test(self) : print self.spam.eggs pass class foo() : eggs = 50 The issue (I assume) is that I'm trying to create an instance of a class before the class is defined. The obvious solution is to re-order my classes, but I like to keep my classes...

Django form don't show specific inputs

Say, I've got a model like this: class Fleet(models.Model): user = models.ForeignKey(User) [...] ship1 = models.IntegerField(default=0) ship2 = models.IntegerField(default=0) ship3 = models.IntegerField(default=0) ship4 = models.IntegerField(default=0) And a form: class sendFleet(forms.Form): [...] shi...

GIMP get layer position relative to the image

Hi, i'm coding in python-fu and i need to get the layer position relative to the image (eg. the layer starts at x=35, y=50) Is this possible? I haven't found anything in the gimp pdb docs ...

Python get subclass name

Is it possible to get the name of a subclass? For example: class Foo: def bar(self): print type(self) class SubFoo(Foo): pass SubFoo().bar() will print: < type 'instance' > I'm looking for a way to get "SubFoo". I know you can do isinstance, but I don't know the name of the class a priori, so that doesn't work for...

Distributing PyQt4 application - licence/legal question

Disclaimer: I know there aren't any lawyers here, but I'd like to hear some opinions. I'm making a Python application using PyQt4 for the GUI. It will be open source, maybe GPL, but rather MIT/BSD licence since I don't see the value of copyleft for it. Since I cannot rely on users to have PyQt installed (in fact, I'm safe to assume that...

Consecutive, conflicting regex substitutions

I've tried this for the italics: r = re.compile(r"(\*[^ ]+\*)") r.sub(r'<i>"\1"</i>', foo) but it doesn't work, as I am sure anyone in the regex know will see right away. ...

Class not refreshing on 2nd url call

Hi there, I have a web page with a link to a url eg./customer/showitem?id=7, which displays details of a specific customer in a child-window using method showitem() in class customer. The method may set the value of a customer class attribute that controls an alert which is displayed when the page is loaded (eg. self.onloadalert="Warni...

Remove Last Path Component In a String

I have a path: myPath = "C:\Users\myFile.txt" I would like to remove the end path so that the string only contains: "C:\Users" So far I am using split, but it just gives me a list, and im stuck at this point. myPath = myPath.split(os.sep) Thanks ...

sharing a :memory: database between different threads in python using sqlite3 package

I would like to create a :memory: database in python and access it from different threads. Essentially something like: class T(threading.Thread): def run(self): self.conn = sqlite3.connect(':memory:') # do stuff with the database for i in xrange(N): T().start() and have all the connections referring to the sam...

How do I create a unix timestamp that doesn't adjust for localtime?

So I have datetime objects in UTC time and I want to convert them to UTC timestamps. The problem is, time.mktime makes adjustments for localtime. So here is some code: import os import pytz import time import datetime epoch = pytz.utc.localize(datetime.datetime(1970, 1, 1)) print time.mktime(epoch.timetuple()) os.environ['TZ'] = '...

Problem encoding accented characters with python

I'm having trouble encoding accented characters in a URL using the python command line. Reducing my problem to the essential, this code: >>> import urllib >>> print urllib.urlencode({'foo' : raw_input('> ')}) > áéíóúñ prints this in a mac command line: foo=%C3%A1%C3%A9%C3%AD%C3%B3%C3%BA%C3%B1 but the same code prints this in window...