python

ElementTree XPath - Select Element based on attribute.

I am having trouble using the attribute XPath Selector in ElementTree, which I should be able to do according to the Documentation Here's some sample code XML <root> <target name="1"> <a></a> <b></b> </target> <target name="2"> <a></a> <b></b> </target> </root> Python def parse(document): root = et.parse(doc...

How to script Visual Studio 2008 from Python?

I'd like to write Python scripts that drive Visual Studio 2008 and Visual C++ 2008. All the examples I've found so far use win32com.client.Dispatch. This works fine for Excel 2007 and Word 2007 but fails for Visual Studio 2008: import win32com.client app1 = win32com.client.Dispatch( 'Excel.Application' ) # ok app2 = win32com.client.Di...

What is the meaning of '(?i)password' in python regular expression?

Pexpect can be used to automate tasks in python (does not need TCL to be installed). One of the simplest routines of this class is the 'run()' routine. It accepts a dictionary of expected question patterns as keys and the responses as values. For example pexpect.run ('scp foo [email protected]:.', events={'(?i)password': mypasswor...

Python script for minifying CSS?

I'm looking for a simple Python script that can minify CSS as part of a web-site deployment process. (Python is the only scripting language supported on the server and full-blown parsers like CSS Utils are overkill for this project). Basically I'd like jsmin.py for CSS. A single script with no dependencies. Any ideas? ...

cherrypy not closing the sockets

Hi Guys, I am using cherrypy as a webserver. It gives good performance for my application but there is a very big problem with it. cherrypy crashes after couple of hours stating that it could not create a socket as there are too many files open: [21/Oct/2008:12:44:25] ENGINE HTTP Server cherrypy._cpwsgi_server.CPWSGIServer(('0.0.0.0',...

Sorting a tuple that contains tuples

I have the following tuple, which contains tuples: MY_TUPLE = ( ('A','Apple'), ('C','Carrot'), ('B','Banana'), ) I'd like to sort this tuple based upon the second value contained in inner-tuples (i.e., sort Apple, Carrot, Banana rather than A, B, C). Any thoughts? ...

how to use 'super' ?

Hi, I was wondering if anyone could explain to me the difference between doing class Child(SomeBaseClass): def __init__(self): super(Child, self).__init__() and this class Child(SomeBaseClass): def __init__(self): SomeBaseClass.__init__(self) I've seen 'super' being used quite alot in classes with only sing...

How do you get the text from an HTML 'datacell' using BeautifulSoup

I have been trying to strip out some data from HTML files. I have the logic coded to get the right cells. Now I am struggling to get the actual contents of the 'cell': here is my htm snip headerRows[0][10].contents [<font size="+0"><font face="serif" size="1"><b>Apples Produced</b><font size="3"> </font></font></font>] ...

When does urllib2 actually download a file from a url?

url = "http://example.com/file.xml" data = urllib2.urlopen(url) data.read() The question is, when exactly will the file be downloaded from the internet? When i do urlopen or .read()? On my network interface I see high traffic both times. ...

Using Variables for Class Names in Python?

I want to know how to use variables for objects and function names in Python. In PHP, you can do this: $className = "MyClass"; $newObject = new $className(); How do you do this sort of thing in Python? Or, am I totally not appreciating some fundamental difference with Python, and if so, what is it? ...

Elegant structured text file parsing

I need to parse a transcript of a live chat conversation. My first thought on seeing the file was to throw regular expressions at the problem but I was wondering what other approaches people have used. I put elegant in the title as i've previously found that this type of task has a danger of getting hard to maintain just relying on reg...

Javascript style dot notation for dictionary keys unpythonic?

I've started to use constructs like these: class DictObj(object): def __init__(self): self.d = {} def __getattr__(self, m): return self.d.get(m, None) def __setattr__(self, m, v): super.__setattr__(self, m, v) Update: based on this thread, I've revised the DictObj implementation to: class dotdict(d...

What would be a better implementation of all combinations in lexicographic order of a jagged list?

I was put in a position today in which I needed to enumerate all possible combinations of jagged list. For instance, a naive approach would be: for a in [1,2,3]: for b in [4,5,6,7,8,9]: for c in [1,2]: yield (a,b,c) This is functional, but not general in terms of the number of lists that can be used. Here is a ...

Alternatives to a wizard

I'm making a program that fits the wizard concept ideally; the user is walked through the steps to create a character for a game. However, I'm realizing that the limitations of the wizard are making it difficult to design "elegant" logic flow. For example, because all pages of the wizard are initalized at the same time, I can't have the...

Incoming poplib refactoring using windows python 2.3

Hi Guys could you please help me refactor this so that it is sensibly pythonic. import sys import poplib import string import StringIO, rfc822 import datetime import logging def _dump_pop_emails(self): self.logger.info("open pop account %s with username: %s" % (self.account[0], self.account[1])) self.popinstance = poplib.POP3(s...

Open Source Profiling Frameworks?

Have you ever wanted to test and quantitatively show whether your application would perform better as a static build or shared build, stripped or non-stripped, upx or no upx, gcc -O2 or gcc -O3, hash or btree, etc etc. If so this is the thread for you. There are hundreds of ways to tune an application, but how do we collect, organize, pr...

how to use cursor for reading multiple files in database in python

In python how to read multiple files from mysql database using cursor or loop one by one and store in separate table ...

How can I generate a report file (ODF, PDF) from a django view

I would like to generate a report file from a view&template in django. Preferred file formats would be OpenOffice/ODF or PDF. What is the best way to do this? I do want to reuse the page layout defined in the template, possibly by redefining some blocks in a derived template. Ideally, the report should be inserted into an existing tem...

RFC 1123 Date Representation in Python?

Is there a fairly easy way to convert a datetime object into an RFC 1123 (HTTP/1.1) date/time string, i.e. a string with the format Sun, 06 Nov 1994 08:49:37 GMT Using strftime does not work, since the strings are locale-dependant. Do I have to build the string by hand? ...

Parsing different date formats from feedparser in python?

I'm trying to get the dates from entries in two different RSS feeds through feedparser. Here is what I'm doing: import feedparser as fp reddit = fp.parse("http://www.reddit.com/.rss") cc = fp.parse("http://contentconsumer.com/feed") print reddit.entries[0].date print cc.entries[0].date And here's how they come out: 2008-10-21T22:23:...