python

Django url.py without method names

In my Django project, my url.py module looks something like this: urlpatterns = patterns('', (r'^$', 'web.views.home.index'), (r'^home/index', 'web.views.home.index'), (r'^home/login', 'web.views.home.login'), (r'^home/logout', 'web.views.home.logout'), (r'^home/register', 'web.views.home.register'), ) Is there a w...

Algorithm to calculate percent difference betweem two blobs of text.

I've been researching on finding an efficient solution to this. I've looked into diffing engines (google's diff-match-patch, python's diff) and some some longest common chain algorithms. I was hoping on getting you guys suggestions on how to solve this issue. Any algorithm or library in particular you would like to recommend? Thanks. ...

How do I install M2Crypto on Ubuntu?

I'm trying to build and install M2Crypto on Ubuntu 10.04 LTS. I downloaded and untarred M2Crypto-0.20.2.tar, and from the M2Crypto-0.20.2 directory I tried python setup.py build. I got an error because I don't have swig. So I ran sudo apt-get install swig. Then I tried python setup.py build again and got: /usr/lib/python2.6/distutil...

Scipy sparse triangular matrix?

Hi, I am using Scipy to construct a large, sparse (250k X 250k) co-occurrence matrix using scipy.sparse.lil_matrix. Co-occurrence matrices are triangular; that is, M[i,j] == M[j,i]. Since it would be highly inefficient (and in my case, impossible) to store all the data twice, I'm currently storing data at the coordinate (i,j) where i i...

currying functions in python in a loop

So here is some code that simplifies what I've been working on: vars = { 'a':'alice', 'b':'bob', } cnames = ['charlie', 'cindy'] commands = [] for c in cnames: kwargs = dict(vars) kwargs['c'] = c print kwargs commands.append(lambda:a_function(**kwargs)) print commands def a_function(a=None, b=None, c=None): ...

Python string format character for __unicode__?

Firstly, is there one? If not, is there a nice way to force something like print '%s' % obj to call obj.__unicode__ instead of obj.__str__? ...

In-place substitution of PyGTK widgets

The code below creates a column of three labels. I would like to take the middle label, and replace it with another widget using the text from the label after the initial creation of the UI. My actual use case is to take a GTKBuilder populated UI, and replace any particular named label with a dynamically wrapped label at run time. (I us...

Creating multiple sites with django

Hi all, I have to create a project in django where the admin can create the sites dynamically and assign the administrators for the same, which would manage that particular site. Can someone please suggest with some hint on how it can be done? Thanks in advance. ...

Capture the last occurrence of a tag

My text is of the form: <Story> <Sentence id="1"> some text </Sentence> <Sentence id="2"> some text </Sentence> <Sentence id="3"> some text </Sentence> My task is to insert a closing tag </Story> after the last </Sentence>. In the text, every </Sentence> is followed by 3 spaces. I tried capturing the last </Sentence> using...

Stack Overflow's User authentication style

I have to say, stack overflow's user authentication system is probably the best out there. I truly admire using cookies to store anonymous's identity and was planning to do something like that for my sites too (since it is so convenient). I am fluent in django and python, is that enough to build an authentication system which is based ...

Export PYTHONPATH - syntax error.

I need to connect MySQLdb - module. I download MySQLdb - module and install it. But when i write (in python interactive shell): import MySQLdb - i get no module named MySQLdb. Then i decided to include MySQLdb directory in PYTHONPATH variable. I write (in python interactive shell): export PYTHONPATH=${PYTHONPATH}:/where/module/live...

Python script reading from a csv file

"Type","Name","Description","Designation","First-term assessment","Second-term assessment","Total" "Subject","Nick","D1234","F4321",10,19,29 "Unit","HTML","D1234-1","F4321",18,, "Topic","Tags","First Term","F4321",18,, "Subtopic","Review of representation of HTML",,,,, All the abov...

How to read from the serial port in python without using external APIs?

Hi. I have to read a stream which is sent from a homemade device over the serial port. The problem is that it should be deployed on a machine where I don't have access to install anything new, which means I have to use the python standard libraries to do this. Is this possible, and if so, how can I manage this. If it turns out to be a...

a indexing question of ndarray

for example, there is a matrix: import numpy as np A = np.array([[ 8. , -6. , 2. ], [-0.5, 8. , -6. ], [ 0.5, -0.5, 2. ]]) It's a LU Decomposition (Doolittle’s decomposition) result.(A = [L\U]) I want to get L and U from A. U should be: U = np.array([[ 8., -6., 2.], [ 0., 8., -6.], ...

Shortcut to testing PHP code

Having a python interpreter is really useful as it lets you quickly type in few commands and verify output; making it easier to understand syntax. However, in PHP, every time I want to try something out I have to - create a PHP script, save it, run it in my browser. Is there a shortcut that I am missing out on for PHP that would make ...

Get max key in dictionary

Hi I have a dictionary that looks like this MyCount= {u'10': 1, u'1': 2, u'3': 2, u'2': 2, u'5': 2, u'4': 2, u'7': 2, u'6': 2, u'9': 2, u'8': 2} I need highest key which is 10 but i if try max(MyCount.keys()) it gives 9 as highest. Same for max(MyCount). The dictionary is created dynamically. ...

Python httplib.HTTPSConnection and password

I use httplib.HTTPSConnection with private key: h = httplib.HTTPSConnection(url, key_file='../cert/priv.pem', cert_file='../cert/srv_test.crt') Then I am asked to enter the password to that private key. Is there any option to enter such password not from user input (console) but from other source (code, environment)? Maybe something l...

MySQLdb install problem.

I need to install MySQLdb. I write: $ tar xfz MySQL-python-1.2.1.tar.gz $ cd MySQL-python-1.2.1 $ python setup.py build #it is ok $ su root setup.py install #return list of errors error list: setup.py: line 3: import: command not found setup.py: line 4: import: command not found- setup.py: line 5: from: command not found-...

Django Admin - Displaying Intermediary Fields for M2M Models

Hi, We have a Django app which contains a list of newspaper articles. Each article has a m2m relationship with both a "spokesperson", as well as a "firm" (company mentioned in the article). At the moment, the Add Article page for creating new Articles is quite close to what we want - it's just the stock Django Admin, and we're using fi...

Generate "fuzzy" difference of two files in Python.

Hello all, I have an issue for comparing two files. Basically, what I want to do is a UNIX-like diff between two files, for example: $ diff -u left-file right-file However my two files contain floats; and because these files were generated on distinct architectures (but computing the same things), the floating values are not exactly th...