Archive for the ‘python’ Category

Greenwich Tweets

Sunday, May 10th, 2009

I’m slowly getting the ball rolling with an idea I’ve had for a while called Greenwich Tweets. The idea behind this is to aggregrate information about Greenwich (London) from a number of Web resources, principally Twitter.

So far I haven’t done much more than register a Twitter account (@greenwichtweets) and domain name (greenwichtweets.co.uk) and browse some of the API documentation. Updates on progress will be available on the Greenwich Tweets site (which at the moment is just redirecting to a page on this site!). I’m hoping to start work in earnest around July.

At the moment I’m looking at Python and Zope (possibly Plone) to manage the back-end stuff, although I’m hoping to keep it all reasonably lightweight.

All themed up and ready to go

Saturday, April 12th, 2008

After a short period of inactivity, I had a concentrated burst of work on my theme which is very nearly finished. Doing the CSS work wasn’t too hard; after all I chose a simple theme to start on as I knew the majority of my time would be spent getting my head around Plone 3.

So, plonetheme.simplicity has now been checked into the Collective SVN repository and, barring a couple of fixes which I’ll hopefully make this weekend, is almost ready for testing. I’ve installed a example of it on my Plone 3.0.3 site at littlewebcreations.co.uk. This highlights one of the problems I’m currently experiencing: the calendar expands outside its container box as in Plone 3.0.3 (and presumably some other earlier versions), the days of the week render as three characters as opposed to two in Plone 3.0.6 (the version which I developed the site in). At the moment I’m not sure if there’s a configuration setting I can make to change this, or whether I’ll need to customise the calendar portlet or use a bit of Javascript to sort the problem out.

Installing my theme product into a different Plone instance threw me for a while. I managed to install it using “setup.py install” from the appropriate Python path but couldn’t see it listed in my available products within the ZMI. I then realised I had to create a plontheme.simplicity-configure.zcml in my Zope instance’s etc/package-includes directory. Or, I could have copied the one that was generated for me in the root directory of my theme. Of course I could have read the generated install.txt file which explained all, but that would have been too easy.

Variables in Python regular expressions: an example

Monday, February 11th, 2008

This is something very useful which I picked up from a mailing list, so I’m certainly not claiming to have come up with it.

The problem: basically, you need to replace some text or code using a regular expression in Python, but you need that regular expression to contain a variable reference so it can be run within a loop etc. How do you do this?

The example below is the actual one I was working on — I needed to replace a <div> tag with a changing id with the actual contents of a file within Zope, in order to insert HTML snippets (YouTube video, Google code etc.) into a page. The page editing tool we use (EditonPro) sensibly strips out this kind of stuff, but this was a special case where the site editor really needed to do this. The inserted tags looked something like this:

 <div id="snippet-123456">
   [snippet: YouTube video of something]
 </div>

Then I defined a method (getSnippet.py) with the following code:

import re

def getSnippet(self, content):

   content = content.replace('\n','')
   pattern = re.compile(r"""(<div id="snippet

-)([^"]*)(">[^<]*)(</div>)""", re.MULTILINE)

   reg = re.findall(pattern, content)

   if reg:

      count = len(reg)

      i=0

      while i < count:

         snpid = str(reg[i][1])

         sr =  r"""<div id="snippet-%s"[^<]*</div>"""

         src = re.compile(sr % snpid)

         srch = src.findall(content)

         srch = srch[0]

         try:

           repl = self.aq_parent[snpid].data

         except:

           repl = ''

      content = content.replace(srch, repl)

      i=i+1

   return content

Briefly, this is how this works: in the document_view page template a call is made to getSnippet thus:

<div tal:define="content here/CookedBody"
     tal:replace="structure python: here.getSnippet(content)"/>

The page content is passed to the getSnippet External Method which first does a regex to find all the relevant bits of code. If anything is returned in the variable “reg”, a while loop is initiated.

The div ids, which map to file names of the Zope files, are extracted from “reg” while this loop runs. We use them to construct a regular expression which will match the relevant snippet code. The key lines are these:

sr =  r"""<div id="snippet-%s"[^<]*</div>"""

src = re.compile(sr % snpid)

We use the %s syntax when defining our regular expression and pass the variable to it when compiling it.

It works, great stuff.