python

Efficiently carry out multiple string replacements in Python

If I would like to carry out multiple string replacements, what is the most efficient way to carry this out? An example of the kind of situation I have encountered in my travels is as follows: >>> strings = ['a', 'list', 'of', 'strings'] >>> [s.replace('a', '')...replace('u', '') for s in strings if len(s) > 2] ['a', 'lst', 'of', 'st...

How to replace a non-static substring case-insensitively

This question is similar to this except that the substring to be replaced is only known at the runtime. I want to write the definition of ireplace, that behaves like this: >>> ireplace(r'c:\Python26\lib\site.py', r'C:\python26', r'image\python26') r'image\python26\lib\site.py' >>> ...

Print syntax error

After installing python 3.1, I cannot print anything. Here is an example: >>> print "Hello World" File "<stdin>", line 1 print "Hello World" ^ SyntaxError: invalid syntax >>> How do I fix this error? ...

tornado server not returning response with self.write

Hi, I have a simple tornado server running like this: import json import suds from suds.client import Client import tornado.httpserver import tornado.ioloop import tornado.web class MainHandler(tornado.web.RequestHandler): def get(self): url = "http://xx.xxx.xx.xxx/Service.asmx?WSDL" client = Client(url) res...

Hide/Delete a StaticText in wxPython

Hey eveybody, I was wondering how to hide/delete a StaticText in wxPython? thanks, soule ...

How can Django's AuthenticationMiddlewear not return a user object in the request

My settings has the following MIDDLEWARE_CLASSES defined: MIDDLEWARE_CLASSES = ( 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django_cas.middleware.CASMiddleware', 'django.contr...

Is there a JavaScript or jQuery equivalent to Python's "sum" built-in function?

Say I have an array-ish container of decimal numbers. I want the sum. In Python I would do this: x = [1.2, 3.4, 5.6] sum(x) Is there a similarly concise way to do this in JavaScript? ...

BeautifulSoup(html) not working, saying can't call module?

import urllib2 import urllib from BeautifulSoup import BeautifulSoup # html from BeautifulSoup import BeautifulStoneSoup # xml import BeautifulSoup # everything import re f = o.open( 'http://www.google.com', p) html = f.read() f.close() soup = BeautifulSoup(html) Getting an error saying the line with soup ...

In a python script, how can I save the contents of a string to file, and the file is in current directory?

In a python script, how can I save the contents of a string to file, and the file is in current directory? i.e the same directory as the test.py script file ...

Where to host a periodically running Python or Java service?

I'm going to build a little service which monitors an IMAP email account and acts on the read messages. For this it just has to run every say 10 min, no external trigger required, but I want to host this service externally (so that I don't need to worry about up times.) To be machine independent I could write the service in Java or Pyth...

Best way to test for existence of a variable in Python

I am reading a million plus files to scrape out some data. The files are generally pretty uniform but there are occasional problems where something that I expected to find is not present. For example, I expect some sgml code to identify a value I need for data_line in temp #temp is a list of lines from a file if <VARIABLENAME> in...

Dynamic Django Application Creation Based On a SQL / CSV file

Is there any django application that can create other apps merely based on a sql / csv file provided that a default template is found for the new applications. ...

How do I access the properties of a many-to-many "through" table from a django template?

From the Django documentation... When you're only dealing with simple many-to-many relationships such as mixing and matching pizzas and toppings, a standard ManyToManyField is all you need. However, sometimes you may need to associate data with the relationship between two models. For example, consider the case of an application...

How do I setup python to always include my directory of utility files

I have been programming in Python for a while now, and have created some utilities that I use a lot. Whenever I start a new project, I start writing, and as I need these utilities I copy them from where ever I think the latest version of the particular utility is. I have enough projects now that I am losing track of where the latest ve...

python function fails to return unless the last statement is slow

I'm working on a subclass of threading.Thread which allows its methods to be called and run in the thread represented by the object that they are called on as opposed to the usual behavior. I do this by using decorators on the target method that place the call to the method in a collections.deque and using the run method to process the d...

How to make float values in Python display .00 instead of .0 ?

Simple question, sorry I can;t figure this out. I have some numbers that are made by float(STRING) and they are displayed as xxx.0, but I want them to end in .00 if it is indeed a whole number. How would I do this? Thanks! EDIT: Python saiys that float doesn't have a cal 'format()' ...

Remove a given element from the other set in a dict of two sets

I have a dict, { "foo": set(["a", "b"]), "bar": set(["c", "d"]) }, and I'm given an element of one of the two sets and the name of the other set. I need to remove that element. How would I go about doing this? My best attempt so far is this: keys = dict.keys() if Element in dict[keys[0]].union(dict[keys[1]]): dict[keys[abs(keys.index(...

Using beautifulsoup, how to I reference table rows in html page

I have a html page that looks like: <html> .. <form post="/products.hmlt" ..> .. <table ...> <tr>...</tr> <tr> <td>part info</td> .. </tr> </table> .. </form> .. </html> I tried: form = soup.findAll('form') table = form.findAll('table') # table inside form But I get an e...

Zooming With Python Image Library

Hi, I'm writing a simple application in Python which displays images.I need to implement Zoom In and Zoom Out by scaling the image. I think the Image.transform method will be able to do this, but I'm not sure how to use it, since it's asking for an affine matrix or something like that :P Here's the quote from the docs: im.transform...

form -> table -> tr using successive findAll calls

Ok so I can reference my table correctly in a html page like this: form = soup.findAll('form')[1] table = form.findAll('table', width="79%") # returns 1 table, doing a print shows table with rows tr = table.findAll('tr') I get an error: ResultSet object has no attribute findAll. Why doesn't this work? I used the output of form.f...