python

python, django: copy image

I created this function, to copy an image from a django-model to another django-model. The image has to be saved redundantly: def __copy_file__(from_object,to_object,field): attr = field.attname try: newpath = getattr(from_object,attr).path dot_at = newpath.rfind(".") while os.path.exists(newpath): ...

Creating views in django (string indentation problems)

I am new to both Python (and django) - but not to programming. I am having no end of problems with identation in my view. I am trying to generate my html dynamically, so that means a lot of string manipulation. Obviously - I cant have my entire HTML page in one line - so what is required in order to be able to dynamically build an html ...

Replace the built-in min functions in python

I should write a function min_in_list(munbers), which takes a list of numbers and returns the smallest one. NOTE: built-in function min is NOT allowed! def min_in_list(numbers): the_smallest = [n for n in numbers if n < n+1] return the_smallest What's wrong? ...

extending an irc bot with modules

I've created an IRC bot in Python from scratch just for the fun of it. I've got a way to load modules into it, but you have to manually type out the code to load each module as below: if data.find('PRIVMSG %s :add_mod foo\r\n' % channel) enabled_mods.append(foo) if data.find('PRIVMSG %s :rm_mod foo\r\n' % channel) enabled_mods.r...

pytz and Etc/GMT-5

I'm having trouble understanding the conversion between the "Etc/GMT-5" timezone and UTC in pytz. >>> dt = datetime(2009, 9, 9, 10, 0) # September 9 2009, 10:00 >>> gmt_5 = pytz.timezone("Etc/GMT-5") >>> gmt_5.localize(dt) datetime.datetime(2009, 9, 9, 10, 0, tzinfo=<StaticTzInfo 'Etc/GMT-5'>) Everything is fine so far, but then I try...

Fast max-flow min-cut library for Python

Is there a reliable and well-documented Python library with a fast implementation of an algorithm that finds maximum flows and minimum cuts in directed graphs? pygraph.algorithms.minmax.maximum_flow from python-graph solves the problem but it is painfully slow: finding max-flows and min-cuts in a directed graph with something like 4000 ...

process closing while saving a file - Python - Windows XP

Hello, I'm working on a project for school where e-mails will be pulled from an inbox and downloaded to different locations depending on how things are parsed. The language I'm writing in is Python, and the environment it will be run on is Windows XP. The idea is that the program will run in the background with no interaction from the us...

An algorithm to create HTML table from this row data?

I have come unstuck on a relatively (almost) trivial problem. I have a row of data that I want to display in tabular form (HTML). For some reason (possibly long day [again] behind the computer), I am not coming up with any elegant solutions (algorithms) to do this. I have presented some sample data, and how such data would be displayed ...

Python - Most efficient way to compare # of words sequenced in "right" order across two strings/lists

Hi experts, I was wondering what the most computationally efficient Python way of cracking this problem would be. Say you have two strings (or lists from splitting those strings--doesn't matter), "this is the right string" vs. "this is right the string." We're assuming that the first string is always right, and a score will be assigne...

Read dynamic xml using element tree

Hi, Environment: Windows,Python,wxpython and Element tree as xml parser. I am developing a stand alone where it reads the xml and creates a tree. My application reads the xml and creates tree but when xml changes next time(when DEPTH of xml increases- i mean when two child elements are added).Application fails to read(Logic fails :( ) ...

Removing duplicates members from a list of tuples

Hi, this question might have similars in SO but my case is a bit different. and I tried to adapt those answers to my problem but couldn't. so here is the thing: I have this list : [(['c', 'a', 'b'], 10), (['c', 'a', 'b'], 9),(['h','b'],2)] for example. I want to remove the duplicates in this list by keeping tuple that has the larger nu...

Use URLLIB without system default proxy Python

I have a small script that needs to communicate with me, it is part of my proxy. The script needs to run before the proxy starts, but the system is set to use the proxy, so it does not go through. How would I use urllib, but not the default proxy? ...

Dynamic Image Creation Using Python over a Web Page

Hello everyone, im new to learning python and i've been trying to implement a text to image converter that runs on a web page. 1.I have succeeded in making the functional code that converts the text into image ,in python using the PIL module. (i.e user enters input text at run time and that gets converted into an image and is stored in...

PyGtk Program is not responding on Windows

Hello everybody! I just managed to get py2exe work on a Windows Virtual Machine but stumbled on another problem which I didn't have right after I installed GTK, Pango, Gobject etc. on that machine: When I launch a Python Script the window appears but it immediately stops responding. This happens too if I open a python interpreter and t...

Python trailing comma after print executes next instruction

If a trailing comma is added to the end of a print statement, the next statement is executed first. Why is this? For example, this executes 10000 ** 10000 before it prints "Hi ": print "Hi", print 10000 ** 10000 And this takes a while before printing "Hi Hello": def sayHello(): for i in [0] * 100000000: pass print "Hello" pri...

django eyed3 script to add mp3 to database

Hello, I'm writing a script to add a genre artist and title of song to django project. Here are parts from my models class GenreManager(models.Manager): def isGenreInDb(self,genre): try: genre=self.get(name__iexact=genre) return genre.id except Genre.DoesNotExist: return False ...

Data persistence for python when a lot of lookups but few writes?

Hi, I am working on a project that basically monitors a set remote directories (FTP, networked paths, and another), if the file is considered new and meets criteria we download it and process it. However i am stuck on what the best way is to keep track of the files we already downloaded. I don't want to download any duplicate files, so ...

How is a python egg different from a regular package?

It seems like just adding a package path to your PYTHONPATH gives you access to all of its modules and functions, similar to installing an egg. Is the difference just that the egg is zip compressed? ...

python - undefined variable?

Does python have some undefined variable / debug mode which will output a notice/warning? PHP allows you to modify the error_reporting to turn on notice warnings which mean doing <?php echo $foo; will throw an "Undefined variable foo on line 2....... does python have something similar? I had a bug where I was doing db.connect in...

Python - default arguments in function

Looking at the python doc http://docs.python.org/library/urllib2.html urllib2.urlopen(url[, data][, timeout]) So, I pass in a url, then optional data and timeout variables (from how I read it). So if I want to pass a timeout, but not the data... whats the default variable for data? Do you just do, urlopen('http://www.example.com/', ,...