python

Whats the difference between list[-1:][0] and list[len(list)-1]?

Lest say you want the last element of a python list: what is the difference between myList[-1:][0] and myList[len(myList)-1] I thought there was no difference but then I tried this >>> list = [0] >>> list[-1:][0] 0 >>> list[-1:][0] += 1 >>> list [0] >>> list[len(list)-1] += 1 >>> list [1] I was a little surprised... ...

Game map from Code

It's a long one so you might want to get that cup of tea/coffee you've been holding off on ;) I run a game called World of Arl, it's a turn based strategy game akin to Risk or Diplomacy. Each player has a set of cities, armies and whatnot. The question revolves around the display of these things. Currently the map is created using a bac...

Nullable ForeignKeys and deleting a referenced model instance

I have a ForeignKey which can be null in my model to model a loose coupling between the models. It looks somewhat like that: class Message(models.Model): sender = models.ForeignKey(User, null=True, blank=True) sender_name = models.CharField(max_length=255) On save the senders name is written to the sender_name attribute. Now, I wa...

How do you deploy django applications for windows?

I'm working on a web application using isapi_wsgi and django-pyodbc. I'd like to have a way to install all dependencies and push the files out to the webserver. Unfortunately, some of these are easier said than done. In particular, handling dependencies is a pain as some of them won't install correctly even under setuptools (pywin32 i...

How is string.find implemented in CPython?

I was wondering if the 'find' method on strings was implemented with a linear search, or if python did something more sophisticated. The Python documentation doesn't discuss implementation details, so http://docs.python.org/library/stdtypes.html is of no help. Could someone please point me to the relevant source code? ...

Any alternatives to IronPython, Python for .NET for accessing CLR from python?

Are there any alternatives to Python for .NET or IronPython for accessing .NET CLR? Both of these seem to have downsides in that Python for .NET is not under active development (as far as I can tell) and you lose some features available in CPython if you use IronPython. So are there any alternatives? ...

Python Class Decorator

In Python 2.5, is there a way to create a decorator that decorates a class? Specifically, I want to use a decorator to add a member to a class and change the constructor to take a value for that member. Looking for something like the following (which has a syntax error on 'class Foo:': def getId(self): return self.__id class addID(or...

Which version of python added the else clause for for loops?

Which was the first version of python to include the else clause for for loops? I find that the python docs usually does a good job of documenting when features were added, but I can't seem to find the info on this feature. (It doesn't help that 'for' and 'else' are particularly difficult terms to google for on a programming website) ...

Good Python modules for fuzzy string comparison?

I'm looking for a Python module that can do simple fuzzy string comparisons. Specifically, I'd like a percentage of how similar the strings are. I know this is potentially subjective so I was hoping to find a library that can do positional comparisons as well as longest similar string matches, among other things. Basically, I'm hoping...

Splitting out the output of ps using Python

On Linux, the command ps aux outputs a list of processes with multiple columns for each stat. e.g. USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND ... postfix 22611 0.0 0.2 54136 2544 ? S 15:26 0:00 pickup -l -t fifo -u apache 22920 0.0 1.5 198340 16588 ? S 09:58 0:05 /usr/sbin/h...

What is a clean, pythonic way to have multiple constructors in Python?

I can't find a definitive answer for this. AFAIK, you can't have multiple __init__ functions in a Python class. So what is a good way to solve this problem? Suppose I have an class called Cheese with the number_of_holes property. How can I have two ways of creating cheese-objects... one that takes a number of holes like this: parmesa...

Python Virtual Machines architecture diagrams/references

Someone could point out sites/books where I can find introductory documentation about the architecture of the Python VM? I'm interested in the C version, but if there are easy-to-follow references about other implementations it could be helpful too. I'm trying to find any kind of resources of higher level than plain source code (howev...

Python Desktop Integration - Drag and drop

I have a pygame window that I want to know when a file has been dragged and dropped onto it. I only need to be able to fetch the name of the file. How can this be accomplished? ...

What to do with "The input line is too long" error message?

I am trying to use os.system() to call another program that takes an input and an output file. The command I use is ~250 characters due to the long folder names. When I try to call the command, I'm getting an error: The input line is too long. I'm guessing there's a 255 character limit (its built using a C system call, but I couldn't ...

Redirecting function definitions in python

This is a very contrived example as it's not easy to explain the context in which I have ended up implementing this solution. However, if anyone can answer why this particular peculiarity happens, I'd be grateful. The example: class A(dict): def __init__(self): self['a'] = 'success' def __getitem__(self, name): pri...

Dynamically change the choices in a wx.ComboBox()

I didn't find a better way to change the different choices in a wx.ComboBox() than swap the old ComboBox with a new one. Is there a better way? Oerjan Pettersen #!/usr/bin/python #20_combobox.py import wx import wx.lib.inspection class MyFrame(wx.Frame): def __init__(self, *args, **kwargs): wx.Frame.__init__(self, *args, **...

Data Synchronization framework / algorithm for server<->device ?

I'm looking to implement data synchronization between servers and distributed clients. The data source on the server is mysql with django on top. The client can vary. Updates can take place on either client or server, and the connection between server and client is not reliable (eg. changes can be made on a disconnected cell phone, shoul...

Advice for C# programmer writing Python

I've mainly been doing C# development for the past few years but recently started to do a bit of Python (not Iron Python). But I'm not sure if I've made the mental leap to Python...I kind of feel I'm trying to do things as I would in C#. Any advice on how I can fully take advantage of Python? Or any tips\tricks, things to learn more a...

Can pysvn 1.6.3 be made to work with Subversion 1.6 under linux?

I see no reference on their website for this. I get pysvn to configure and build, but then it fails all the test. Has anyone had any luck getting this to work under linux? ...

Best way to integrate Python and JavaScript?

Is it possible to integrate Python and JavaScript? For example, imagine you wanted to be able to define classes in JavaScript and use them from Python (or vice versa). If so, what's the best way? I'm interested not only if this is possible but if anyone has done it within a "serious" project or product. I'm guessing it would be possi...