python

Python urllib2.open is slow, need a better way to read several urls

Hey guys. As the title suggests, I'm working on a site written in python and it makes several calls to the urllib2 module to read websites. I then parse them with BeautifulSoup. As I have to read 5-10 sites, the page takes a while to load. I'm just wondering if there's a way to read the sites all at once? Or anytricks to make it fast...

wxPython SetBackgroundColour not working on OS X

I haven't had to do any GUI programming in a long time, so I might be being obtuse here, so please bear with me if this is a stupid question. I decided to use wxPython for a small hobby project, and I'm having trouble changing the background colour of the main window. I'm using Python 2.6.2 and wxPython 2.8.11.0 on Snow Leopard. Can anyo...

How to create a floor function with a "step" argument

Hello, I would like to create a function floor(number, step), which acts like : floor(0, 1) = 0 floor(1, 1) = 1 floor(1, 2) = 0 floor(5, 2) = 4 floor(.8, .25) = .75 What is the better way to do something like that ? Thanks. ...

Good learner book for a 12 year old?

Hey Guys, My 12 year old brother has recently expressed an interest in learning to program. I of course think this is a great idea, why not start him early? I'm wondering what you guys think with regards a book? I was thinking I should start him off on Java but I'm unsure what book would be best? Any suggestions with regards a book or e...

How to get stdout into a string (Python)

I need to capture the stdout of a process I execute via subprocess into a string to then put it inside a TextCtrl of a wx application I'm creating. How do I do that? EDIT: I'd also like to know how to determine when a process terminates ...

Resuable model members in django

I have a django model like this: class Something(models.Model): title = models.CharField(max_length=200, default=u'') text = models.CharField(max_length=250, default=u'', blank=True) photo = models.ImageField(upload_to=u'something') def photo_thumb(self): if self.photo: return u'<img src="%s" />'...

python sub class constructor

In python OOP, lets say, Person is a parent class with its own constructor; then Student is a sub class of Person, before I use Student, must Person.__init__(self) be called first in the constructor of Student? Plus, can I define a new constructor in Student class? class Person(): def __init__(self): Above is class Person ...

Popen gives "File not found" Error (windows/python)

I'm trying to run console commands via subprocess.Popen, and whenever I run it I get the windows "File not found" error, even when running the echo command. I am also using Popen inside a thread made with the thread module. Is that the problem? ...

How to parse data in a variable length delimited file?

I have a text file which does not confirm to standards. So I know the (end,start) positions of each column value. Sample text file : # # # # Techy Inn Val NJ Found the position of # using this code : 1 f = open('sample.txt', 'r') 2 i = 0 3 positions = [] 4 for line in f: 5 if line.find('#') > 0: 6 pr...

first process of python popen pipe can't be killed

I am using this code p1 = Popen(['rtmpdump'] + cmd_args.split(' '), stdout=PIPE) p2 = Popen(player_cmd.split(' '), stdin=p1.stdout, stderr=PIPE) p2.wait() # try to kill rtmpdump # FIXME: why is this not working ? try: p2.stdin.close() p1.stdout.close() p1.kill() except AttributeError: # if we use python 2.5 from sign...

How can I display native accents to languages in console in windows?

print "Español\nPortuguês\nItaliano".encode('utf-8') Errors: Traceback (most recent call last): File "", line 1, in print "Español\nPortuguês\nItaliano".encode('utf-8') UnicodeDecodeError: 'ascii' codec can't decode byte 0xf1 in position 4: ordinal not in range(128) I'm trying to make a multilingual console program i...

Python - Sum of numbers

I am trying to sum all the numbers up to a range, with all the numbers up to the same range. I am using python: limit = 10 sums = [] for x in range(1,limit+1): for y in range(1,limit+1): sums.append(x+y) This works just fine, however, because of the nested loops, if the limit is too big it will take a lot of time to compu...

python: httplib error: can not send headers

conn = httplib.HTTPConnection('thesite') conn.request("GET","myurl") conn.putheader('Connection','Keep-Alive') #conn.putheader('User-Agent','Mozilla/5.0(Windows; u; windows NT 6.1;en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome//5.0.375.126 Safari//5.33.4') #conn.putheader('Accept-Encoding','gzip,deflate,sdch') #conn.putheader('Accep...

exception for notifying that subclass should implement a method in python

Hi! suppose I want to create an abstract class in python with some methods to be implemented by subclasses. (in Python) for example: class Base(): def f(self): print "Hello." self.g() print "Bye!" class A(Base): def g(self): print "I am A" class B(Base): def g(self): print "I am B...

Python design question (Can/should decorators be used in this case?)

I have a problem that can be simplified as follows: I have a particular set of objects that I want to modify in a particular way. So, it's possible for me to write a function that modifies a single object and then create a decorator that applies that function to all of the objects in the set. So, let's suppose I have something like thi...

Intelligent date range parsing of human input?

Has anyone come across a script / cl app written in any language that handles the parsing of human-entered dates well? I'd love to be able to parse, for example: "3 to 4 weeks" "2 - 3 days" "3 weeks to 2 months" ...

How do I cache a list/dictionary in Pylons?

On a website I'm making, there's a section that hits the database pretty hard. Harder than I want. The data that's being retrieved is all very static. It will rarely change. So I want to cache it. I came across http://wiki.pylonshq.com/display/pylonsdocs/Caching+in+Templates+and+Controllers and had a good read have been making use of te...

Problems with my BaseHTTPServer

I am trying to create my own functions in the subclass of BaseHTTPRequestHandler as such class Weblog(BaseHTTPServer.BaseHTTPRequestHandler): def do_HEAD(self): self.send_response(200) self.send_header("Content-type", "text/html") self.end_headers() def do_GET(self): """Respond to a GET request.""" if self.pat...

How to optimize PyQt QSortFilterProxyModel filter reimplementation?

I have a reimplemented QSortFilterProxyModel acceptRows to achieve custom behavior, i want it to not filter out items which have a valid child. class KSortFilterProxyModel(QSortFilterProxyModel): #FIXME: Funciona pero es endemoniadamente lento def __init__(self, parent=None): super(KSortFilterProxyModel, self).__init__(parent) s...

Modifying a namedtuple's constructor arguments via subclassing?

I want to create a namedtuple which represents the individual flags in a short bitfield. I'm trying to subclass it so that I can unpack the bitfield before the tuple is created. However, my current attempt isn't working: class Status(collections.namedtuple("Status", "started checking start_after_check checked error paused queued loade...