python

What is the Python equivalent of Unix 'touch'?

I'm writing a function and I want it to touch a file so that I can write to that file. If the file doesn't exist, I will get an error. How can I say that? ...

splitting the bill algorithmically & fair, afterwards :)

I'm trying to solve the following real-life problem you might have encountered yourselves: You had dinner with some friends and you all agreed to split the bill evenly. Except that when the bill finally arrives, you find out not everyone has enough cash on them (if any, cheap bastards). So, some of you pays more than others... Afterwa...

what do you think about my django search app?

hi, I've got a new pet project: a pure django search app. I know there are some apps for searching (django-solr, solango, django-hackstack) but they all use a backend for high-end searching. I've got a little experience in information retrieval and natural language processing in general so I want to try to write another search app which...

What is the difference between .py and .pyc files?

I have noticed .pyc files spontaneously being generated when some .py file of the same name gets run. What is the difference between .py and .pyc files? Also, I find that having .pyc files lying around clutters up space. Should one delete .pyc files? Or is there a benefit and/or necessity to having them around? UPDATE: Here are 2 answ...

mod_wsgi: multiple application invokations per request?

I'm using apache with mod_wsgi, and when I start apache and make a request, I see it make one invokation of the application. After a few minutes (the application gets reloaded), and now I see it make two invokations of the application per request. Why's that? Also: is there any easy way to initialize resources (database pools, for examp...

Now to convert this strings to date time object in Python or django?

Now to convert this strings to date time object in Python or django? 2010-08-17T19:00:00Z 2010-08-17T18:30:00Z 2010-08-17T17:05:00Z 2010-08-17T14:30:00Z 2010-08-10T22:20:00Z 2010-08-10T21:20:00Z 2010-08-10T20:25:00Z 2010-08-10T19:30:00Z 2010-08-10T19:00:00Z 2010-08-10T18:30:00Z 2010-08-10T17:30:00Z 2010-08-10T17:05:00Z 2010-08-10T17:05:...

Object vs. class variable

This is a completely theoretical question. Suppose the following code: >>> class C: ... a = 10 ... def f(self): self.a = 999 ... >>> >>> C.a 10 >>> c = C() >>> c.a 10 >>> c.f() >>> c.a 999 At this point, is class variable C.a still accessible through the object c? ...

Parse a map of int -> list from a string

Hi, This should be a fairly straight forward python question, but I'm getting stuck getting the syntax right. Let's say I have a string: "1:a,b,c::2:e,f,g::3:h,i,j" and I want to convert this to a map like so: {'1': ['a', 'b', 'c'], '2': ['e', 'f', 'g'], '3': ['h', 'i', 'j']} How would this be done? I can figure out how to do it...

How can I put only value after decimal

I have got output and I want to use only three values after decimal. How can i do that in Python? ...

interactive matplolib through eclipse

is it possible to have an interactive session through pydev for using matplotlib? The problem is that I cannot get shell access to drive the charts as the interpretor halts after the show() command. I would like to have an interactive session through pydev. Interactive sessions are well described by the matplotlib manual here: http://ma...

SWIG + SCIPY = Import Error on PyArray_TYPE

Hey gang, so I've written a swig wrapper for some C code. I'm trying to bridge the gap between scipy arrays and C arrays, which I know is messy. After a clean compilation (well...not including some warnings...) I'm getting this issue when I load the python-swig-c module: undefined symbol: PyArray_TYPE I've added my swig interface file...

app engine DeadlineExceededError for cron jobs and task queue for wikipedia crawler

Hello, I am trying to build a wikipedia link crawler on google app engine. I wanted to store an index in the datastore. But I run into the DeadlineExceededError for both cron jobs and task queue. for the cron job I have this code: def buildTree(self): start=time.time() self.log.info(" Start Time: %f" % start) nobranches...

Writing empty string to textfile in Python

Quite embarassing issue, though i come from web development and rarely have to deal with files i/o. I wrote a simple config updater for use on my shared hosting. It scans the directory for subdirectories, and then writes config lines to a file - one line for each subdirectory. The problem is, when it detects there are config lines but n...

How to make a class that acts like a string?

I have a context manager that captures output to a string for a block of code indented under a with statement. This context manager yields a custom result object which will, when the block has finished executing, contain the captured output. from contextlib import contextmanager @contextmanager def capturing(): "Captures output wit...

How to continuously monitor rhythmbox for track change using python

Hi, I want to monitor the change of track in Rhythmbox using python. I want to continuously check for change of track and execute a set of functions if the track is changed. I have written a piece of code which gets hold of the Rhythmbox interfaces from the dbus and gets the current track details. But this program has to be run manually...

Picking out symbols from a code base with Python

Hi all Given a code base (say for example a large C or Objective-C project) I would like to analyze the sourcecode files and pick out symbols of interest. They might be class declarations, variable names or types, or method names. Is there a Python module that could help me with this? The only approach I can see going forward is to u...

Creating functools.partial *args & *kwargs from parsed strings

I am being passed lists of strings that I would like to convert to method calls using functools.partial: [ 'object1.method1(arg1, arg2, 4, key=value)', 'object2.method2(6, arg1)' ...] It's easy to use a regexp to generate the necessary 'func' argument to functools.partial, but I'm having a devil of a time easily converting the string ...

how to add python to system dependency on win7?

Hi, i've got a problem when i'm doing dev I managed to use python manage.py runserver in a CMD shell but the system cant find python How could I add python to the system dependency to make the commandline work? ...

python number guessing question

import sys print 'Content-Type: text/html' print '' print '<pre>' # Read the form input which is a single line guess = -1 data = sys.stdin.read() # print data if data == []: print "Welcome to Josh's number game" try: guess = int(data[data.find('=')+1:]) except: guess = -1 print 'Your guess is', guess answer = 42 if g...

set key with new bulkloader

I am converting a script to use the new bulkloader. (What was wrong with the original bulkloader? - I prefer writing Python to editing configuration files...) Anyway, I want to prevent duplicates by assigning a combination of properties to the key. The docs say: If you want to use or calculate a key from the import data, specify a...