python

Why is this recursive statement wrong?

This is a bank simulation that takes into account 20 different serving lines with a single queue, customers arrive following an exponential rate and they are served during a time that follows a normal probability distribution with mean 40 and standard deviation 20. Things were working just fine till I decided to exclude the negative va...

Yaml merge in Python

So I'm toying around with the idea of making myself (and anyone who cares to use it of course) a little boilerplate library in Python for Pygame. I would like a system where settings for the application are provided with a yaml file. So I was thinking it would be useful if the library provided a default yaml tree and merged it with a u...

What does += mean in Python?

I see code like this for example in Python: if cnt > 0 and len(aStr) > 1: while cnt > 0: aStr = aStr[1:]+aStr[0] cnt += 1 What does the += mean? ...

App dock icon in wxPython

In wxPython on Mac OS X is it possible to display a custom icon when an application is launched. Right now when I launch the app I wrote, a blank white icon is displayed in the dock. How would I go about changing it? ...

Initialisation of keyword args in Python

Why does the following: class A(object): def __init__(self, var=[]): self._var = var print 'var = %s %s' % (var, id(var)) a1 = A() a1._var.append('one') a2 = A() result in: var = [] 182897439952 var = ['one'] 182897439952 I don't understand why it is not using a new instance of a list when using optional keyword arg...

Why in the world does Python's Tkinter break using canvas.create_image?

I've got a python GUI app in the workings, which I intend to use on both Windows and Mac. The documentation on Tkinter isn't the greatest, and google-fu has failed me. In short, I'm doing: c = Canvas( master=frame, width=settings.WINDOW_SIZE[0], height=settings.WINDOW_SIZE[1], background=settings.CANVAS_COLOUR ) file = ...

How do you make the Python Msqldb module use ? in stead of %s for query parameters?

MySqlDb is a fantastic Python module -- but one part is incredibly annoying. Query parameters look like this cursor.execute("select * from Books where isbn=%s", (isbn,)) whereas everywhere else in the known universe (oracle, sqlserver, access, sybase...) they look like this cursor.execute("select * from Books where isbn=?", (isbn,)) ...

Change/adapt date widget on admin interface

Hi! I'm configuring the admin site for my new app, and I found a little problem with my setup. I have a 'birth date' field on my database editable via the admin site, but, the date widget isn't very handy for that, because it makes that, if I have to enter i.e. 01-04-1956 in the widget, i would have to page through a lot of years. Also...

Extract domain name from a host name

Is there a programatic way to find the domain name from a given hostname? given -> www.yahoo.co.jp return -> yahoo.co.jp The approach that works but is very slow is: split on "." and remove 1 group from the left, join and query an SOA record using dnspython when a valid SOA record is returned, consider that a domain Is there a clean...

Catch only some runtime errors in Python

I'm importing a module which raises the following error in some conditions: RuntimeError: pyparted requires root access I know that I can just check for root access before the import, but I'd like to know how to catch this spesific kind of error via a try/except statement for future reference. Is there any way to differentiate betwe...

Abstract class + mixin + multiple inheritance in python

So, I think the code probably explains what I'm trying to do better than I can in words, so here goes: import abc class foo(object): __metaclass__ = abc.ABCMeta @abc.abstractmethod def bar(self): pass class bar_for_foo_mixin(object): def bar(self): print "This should satisfy the abstract method require...

Changing case (upper/lower) on adding data through Django admin site

I'm configuring the admin site of my new project, and I have a little doubt on how should I do for, on hitting 'Save' when adding data through the admin site, everything is converted to upper case... Edit: Ok I know the .upper property, and I I did a view, I would know how to do it, but I'm wondering if there is any property available f...

Python: finding uid/gid for a given username/groupname (for os.chown)

What's a good way to find the uid/gid for a given username or groupname using Python? I need to set file ownership with os.chown and need the integer ids instead of the alphabetic. [Quick note]: getpwnam works great but is not available on windows, so here's some code that creates stubs to allow you to run the same code on windows and u...

How do I convert a string 2 bytes long to an integer in python

I have a python program I've inherited and am trying to extend. I have extracted a two byte long string into a string called pS. pS 1st byte is 0x01, the second is 0x20, decimal value == 288 I've been trying to get its value as an integer, I've used lines of the form x = int(pS[0:2], 16) # this was fat fingered a while back and read...

List Comprehensions and Conditions?

I am trying to see if i can make thsi code more interesting/exciting using list comprehensions. Lets say i have the following lists defined: a_list = [ 'HELLO', 'FOO', 'FO1BAR', 'ROOBAR', 'SHOEBAR'] regex_list = [lambda x: re.search(r'FOO', x, re.IGNORECASE), lambda x: re.search(r'RO',...

Python: Mixing files and loops

I'm writing a script that logs errors from another program and restarts the program where it left off when it encounters an error. For whatever reasons, the developers of this program didn't feel it necessary to put this functionality into their program by default. Anyways, the program takes an input file, parses it, and creates an outp...

Controlling VirtualBox via COM from Python?

Hello. I'm trying to control latest Sun VirtualBox via it's COM interface from Python. But, unfortunately, the following code don't work: import win32com.client VBOX_GUID = "{B1A7A4F2-47B9-4A1E-82B2-07CCD5323C3F}" try : oVbox = win32com.client.Dispatch( VBOX_GUID ) oVbox.FindMachine( "kubuntu" ) except Exception as oEx: print str...

how to get the n-th record of a datastore query

Suppose that I have the model Foo in GAE and this query: query = Foo.all().order('-key') I want to get the n-th record. What is the most efficient way to achieve that? Will the solution break if the ordering property is not unique, such as the one below: query = Foo.all().order('-color') edit: n > 1000 edit 2: I want to develop a ...

Combining 2 .csv files by common column.

So I have two .csv files where the first line in file 1 is: MPID,Title,Description,Model,Category ID,Category Description,Subcategory ID,Subcategory Description,Manufacturer ID,Manufacturer Description,URL,Manufacturer (Brand) URL,Image URL,AR Price,Price,Ship Price,Stock,Condition The first line from file 2: Regular Price,Sale Price...

crontab in python

I'm writing code in python for some sort of daemon that has to execute a specific action at a certain instance in time defined by a crontab string. Is there a module I can use? If not, can someone paste/link an algorithm I can use to check whether the instance of time defined by the crontab has occured in the time from when the previous ...