python

get contents of <a> tags using python

Assuming I have html read into my program like this: <p><a href="http://vancouver.en.craigslist.ca/nvn/ret/1817849271.html"&gt;F/T &amp; P/T Sales Associate - Caliente Fashions</a> - <font size="-1"> (North Vancouver)</font></p> <p><a href="http://vancouver.en.craigslist.ca/van/ret/1817804151.html"&gt;IMMEDIATE EMPLOYMENT WANTED!</a> - ...

Imagemagick or similar for scripted image enhancement

I found the following algorithm to work fairly well for enhancement of various images of whiteboards, etc: duplicate the layer, make sure the top layer is active (gaussian)blur the new layer. You shouldn't be able to read the text anymore. set the layer mode to dodge invert the layer I tried it out in gimp and it looks promising. I'...

Python and Unicode Blocks for regex

Coming from the land of Perl, I can do something like the following to test the membership of a string in a particular unicode block: # test if string has any katakana script characters my $japanese = "カタカナ"; if ($japanese =~ /\p{InKatakana}/) { print "string has katakana" } I've read that Python does not support unicode blocks (tr...

How can I group objects by their date in Django?

I'm trying to select all objects in the articles table, and have them grouped by their date. I'm thinking it would look similar to this: articles = Article.objects.filter(pub_date__lte=datetime.date.today()).group_by(pub_date.day) articles = {'2010-01-01': (articleA, articleB, articleC...), '2010-01-02': (article1, article2,...

making a python object unsable after a finalize-type call

I have a python object which wraps a sensitive and important resource on the system. I have a cleanup() function which safely releases various locks used by the object. I want to make sure that after a call to cleanup() the object becomes unusable. Ideally, any call to any member function of the object would raises an exception. Is the...

Python code simplification? One line, add all in list.

I'm making my way through project Euler and I'm trying to write the most concise code I can. I know it's possible, so how could I simplify the following code. Preferably, I would want it to be one line and not use the int->string->int conversion. Question: What is the sum of the digits of the number 21000? My answer: >>> i=0 >>> for...

Which openid / oauth library to connect a django project to Google Apps Accounts?

I'm working on an intranet django project (not using GAE) for a company that uses Google Apps for login. So I'd like my users to be able to log in to my django project using their google accounts login. OpenID seems appropriate, although maybe Oauth might work too? I see a lot of similarly named libraries out there to connect django's...

Python: get number of items in generator without storing the items

I have a generator for a large set of items. I want to iterate through them once, outputting them to a file. However, with the file format I currently have, I first have to output the number of items I have. I don't want to build a list of the items in memory, as there are too many of them and that would take a lot of time and memory. Is...

Mocking urllib2.urlopen and lxml.etree.parse using pymox

I'm trying to test some python code that uses urllib2 and lxml. I've seen several blog posts and stack overflow posts where people want to test exceptions being thrown, with urllib2. I haven't seen examples testing successful calls. Am I going down the correct path? Does anyone have a suggestion for getting this to work? Here is what...

Possible to return instantiator in Python?

class Parent(): def __init__(self): self.child = Child() class Child(): def __init__(self): # get Parent instance self.parent = self.Instantiator() I know this isn't proper encapsulation but for interest's sake... Given a "Parent" class that instantiates a "Child" object, is it possible from within Child to return the...

How to accumulate state across tests in py.test

I currently have a project and tests similar to these. class mylib: @classmethod def get_a(cls): return 'a' @classmethod def convert_a_to_b(cls, a): return 'b' @classmethod def works_with(cls, a, b): return True class TestMyStuff(object): def test_first(self): self.a = mylib...

Running pyflakes remotely with flymake and tramp in emacs?

I'm trying to use flymake to run pyflakes, as suggested here: http://www.emacswiki.org/cgi-bin/wiki/PythonMode#toc9 This works fine for local files, and almost works with remote files with a bit of tweaking, but I'm left with a problem where flymake/pyflakes 'modifies' the buffer when it runs (although nothing actually seems to change),...

Which is faster to learn: Django (Python) or Ruby on Rails (Ruby)?

I have done the front-end of my design, but I have no experience in web programming. Would like to pick up a language asap so that I can deploy the product. Which is faster to pick up between the two? I know there is always debate in which is better. I think either one serves well for me, but I want to know which one is easier to learn, ...

How are closures implemented in Python?

"Learning Python, 4th Ed." mentions that "the enclosing scope variable is looked up when the nested functions are later called.." However, I thought that when a function exits, all of its local references disappear. def makeActions(): acts = [] for i in range(5): # Tries to remember each i acts.append(lambda x: i ** x) #...

Why does this simple Python script reveal the incorrect answer?

I'm again working on Project Euler, this time problem #4. The point of this script is to find the largest palindromic product of two three digit numbers. I thought it was fairly straightforward to solve, but I'm getting an answer that is too low. More specifically, I am getting 580085, and the answer is 906609. Could someone tell me wh...

Python distutils builds extensions differently on different machines.

I have been working on a Python extension module with lots of files. While building on one machine, python setup.py build will happily detect changed files, build just those files, and link the whole thing together, just like make. On another machine, however, a single change to any file triggers a recompile of all sources. Just to be c...

HTML generation in Python

What's the easiest way to quickly create some simple HTML in Python? All I've found so far are complex templating systems or classes for HTML generation with APIs that seem much heavier than what I need. I could just do it myself by sticking strings together but I thought there might be a library that could save me a little time. edit-...

Reisze wx.Dialog horizontally only

Is there a way to allow a custom wx.Dialog to be resized in the horizontal direction only? I've tried using GetSize() and then setting the min and max height of the window using SetSizeHints(), but for some reason it always allows the window to be resized just a little, and it looks rather tacky. The only other alternative I have come up...

Python scipy.weave and STANN C++ Library

I'm trying out scipy.weave to build a fast minimal spanning tree program in Python. Unfortunately, using scipy.weave with a C++ library that I found, STANN, is more difficult that I had assumed. Here's the link to the STANN library: http://sites.google.com/a/compgeom.com/stann/ Below is the Python with scipy.weave script that I wrote. ...

Is there a limit on the number of asynchronous urlfetch calls I can run simultaneously?

I noticed what appears to be a limit on simultaneous asynchronous calls of urlfetch in the Java implementation (as noted here: http://code.google.com/appengine/docs/java/urlfetch/overview.html) but not in the python documentation: http://code.google.com/appengine/docs/python/urlfetch/asynchronousrequests.html So is it the case that th...