python

List of Lists in python?

I need a good function to do this in python. def foo(n): # do somthing return list_of_lists >> foo(6) [[1], [2,3], [4,5,6]] >> foot(10) [[1], [2,3], [4,5,6] [7,8,9,10]] NOTE:Seems like my brain has stopped functioning today. ...

Why MySQLdb for Mac has to have MySQL installed to install?

Hi, I been working on finding out how to install MySQLdb module for Python on Mac. And all pathes finally come cross to have MySQL installed since there is a mysql_config needed for the module. But I don't understand why it has to be needed? MySQLdb suppose to be a client module for the client who wants to connect to the server. But n...

Auto create next Key in python dictionary

Hi Is there an easy way to create dictionary like associative array in php? in php i can do: > $x='a'; > while($x<d){ > $arr[]['Letter']=$x; > $x++ > } The interpreter adds automatically a new number into empty brackets "[]" so i can access letter b from $arr[1]['Letter'], etc. Is there a way to do same with python? ...

decypher with me that obfuscated MultiplierFactory

This week on comp.lang.python, an "interesting" piece of code was posted by Steven D'Aprano as a joke answer to an homework question. Here it is: class MultiplierFactory(object): def __init__(self, factor=1): self.__factor = factor @property def factor(self): return getattr(self, '_%s__factor' % self.__class_...

regex for character appearing at most once

I want to check a string that contains the period, ".", at most once in python. ...

How to split data into equal size packets having variable header size.. .

I am building peer to peer application in python. Its going to work over UDP. I have function called getHeader(packetNo,totalPackets) which returns me the header for that packet.Depending on size of header I am chopping data, attaching data to header and getting same packet size. Header size is not fixed because length consumed by diffe...

How to draw a class's metaclass in UML?

If class A is created by its __metaclass M, how does the arrow look in UML? The stereotype syntax seems to be related. I didn't look in Python UML tools yet. ...

How to except SyntaxError?

I would like to except the error the following code produces, but I don't know how. from datetime import datetime try: date = datetime(2009, 12a, 31) except: print "error" The code above is not printing "error". That's what I would like to be able to do. edit: The reason I would like to check for syntax errors, is because 12...

Why can't I crop this image in Python PIL? (simple syntax problem?)

from PIL import Image im = Image.open(f) #the size is 500x350 box = (0,0,100,100) kay = im.crop(box) It seems like there's nothing wrong with this, right? That last line will result in an error and won't continue, but I don't know what the error is because it's AJAX and I can't debug ATM. ...

How to print error of python - simple question

Hi, is this possible: try: something here except: print 'the whatever error occurred.' Is it possible to do this in 1 line? Edit...I want to print the 1 line AFTER "except:" I simply just want to print the error, whatever that is. ...

django model Form. Include fields from related models

Hi. I have a model, called Student, which has some fields, and a OneToOne relationship with user (django.contrib.auth.User). class Student(models.Model): phone = models.CharField(max_length = 25 ) birthdate = models.DateField(null=True) gender = models.CharField(max_length=1,choices = GENDER_CHOICES) city = models.C...

python _+ django, is it compiled code?

Just looking into python from a .net background. Is python compiled like .net? If yes, can it be obfuscated and is it more or less secure than .net compiled code that is obfuscated? does pretty much every web host (unix) support django and python? ...

Get offset of current buffer in vim (in particular, via python scripting)

Hi, i want to get the offset of the current cursor position the current selection range in vim, beginning from the start of the file. I do this in python, so hints how to do it with vim's python scripting would be very helpful. I have used vim.current.. before for doing scripting, but it uses lines and columns rather than a gener...

Python: Path to current file depends on how I execute the program

This is my Python program: #!/usr/bin/env python import os BASE_PATH = os.path.dirname(__file__) print BASE_PATH If I run this using python myfile.py it prints an empty string. If I run it using myfile.py, it prints the correct path. Why is this? I'm using Windows Vista and Python 2.6.2. ...

Python Script to find instances of a set of strings in a set of files

Hi all, I have a file which I use to centralize all strings used in my application. Lets call it Strings.txt; TITLE="Title" T_AND_C="Accept my terms and conditions please" START_BUTTON="Start" BACK_BUTTON="Back" ... This helps me with I18n, the issue is that my application is now a lot larger and has evolved. As such a lot of these s...

Djapian - filtering results

I use Djapian to search for object by keywords, but I want to be able to filter results. It would be nice to use Django's QuerySet API for this, for example: if query.strip(): results = Model.indexer.search(query).prefetch() else: results = Model.objects.all() results = results.filter(somefield__lt=somevalue) return results Bu...

python PIL - background displayed opaque instead of transparent

I want to generate 32x32 sized thumbnails from uploaded images (actually avatars). To prevent a thumbnail from being smaller than that size, I want to create a transparent 32x32 background and paste the thumbnail on it. The code below tries to do so. However, the avatar is displayed on a black and opaque background; I lose transparen...

replace/delete field using sqlalchemy

Two part question. Using postgres in python. 1) How do I replace all fields from the same column that match a specified value? For example, let's say I want to replace any fields that match "green" with "red" in the "Color" column. 2) How to delete all fields from the same column that match a specified value? For example, I'm trying to...

How do I use PyMock and Nose with Django models?

I'm trying to do TDD with PyMock, but I keep getting error when I use Nose and execute core.py from command line: "ERROR: Failure: ImportError (Settings cannot be imported, because environment variable DJA NGO_SETTINGS_MODULE is undefined.)" If I remove "from cms.models import Entry" from the unit test module I created, everything work...

Python process pool and scope

I am trying to run autogenerated code (which might potentially not terminate) in a loop, for genetic programming. I'm trying to use multiprocessing pool for this, since I don't want the big performance overhead of creating a new process each time, and I can terminate the pool process if it runs too long (which i cant do with threads). E...