Archive for the ‘python’ Category

check html templates for spelling errors

Tuesday, November 17th, 2009
my spelling is horribly. it’s quite annoying to update a webpage and getting complaints about some spelling errors. usually all the human language strings are nicely stored in a separate textfile but for smaller projects i often put them directly into html templates. if you have a proper ide it will do the spell checking for you. but if you use some not so sophisticated ide you have to do it yourself or you can use one of the opensource spellers. there are ispell, aspell, myspell and some more. at first i tried to write a shellscript using one of them. then i remembered using one once from python. there is this nice library, pyEnchant. it makes spellchecking really easy. here is a little python script which checks all the html templates. the advantage of this is that it can be implemented as a unittest or something similar, so you will be warned if there is a new spelling error in your project.

the script is quick and dirty and for german. but you should get the idea. from enchant.checker import SpellChecker import os, re, codecs, sys chkr = SpellChecker("de_DE") # patterns remove in this case html and jinja2/ django # code and some special words rmPatterns = [r'<.*?>', r'{%.*?%}', r'{{.*?}}', r'me@norep\.com', u'projectName', u'FooName'] # get a list of directories and subdirectories def listdirs(dirname): dirs = [os.path.join(dirname, f) for f in os.listdir(dirname) / if os.path.isdir(os.path.join(dirname, f))] for d in dirs[:]: dirs += listdirs(d) return dirs for d in listdirs(’templates’): for f in [os.path.join(d, f) for f in os.listdir(d) if re.search(r'\.html$', f)]: # read filedata… as unicode, i always use unicode fd = codecs.open(f, ‘r’, ‘utf-8′) data = fd.read() fd.close() # remove the tags and codes defined in rmPatterns for p in rmPatterns: data = re.sub(p, ”, data) # get error words found = [] chkr.set_text(data) for err in chkr: found.append(err.word) # if errors found, print them if len(found) > 0: print “%s: %s” % (len(found), f) for w in found: print ” : %s -> %s” % (w, ‘, ‘.join(chkr.dict.suggest(w))) the output for one of my projects is: 1: templates/pages/about.html : stösst -> stößt, störst 2: templates/pages/help.html : Registrier -> Registrier-, Registriere, Registriert, Registrieren : Bestätigungs -> Bestätigung, Bestätigungs- 3: templates/pages/legalNotice.html : St -> Set, St., Et, Kt, Sh, SV, Ist, Äst, Ast, Ost, Gst, Sät, So : mail -> Mail, mal, -mail, mail-, mai- : tel -> teil, Gel, Tel., Telex, Teller, Telekom

zine, wordpress killer

Sunday, May 10th, 2009
i think about a new blog. a nontechnical trashblog or something. for the kerbtier blog i’m using wordpress and i’m quite happy with it. lots of useful plugins and quite stable. but the code is somewhat funny and it’s not so easy to adapt/extend it to ones needs. i often had to change code instead of reconfigure or add code and this makes it time consuming to keep it up to date.

i looked for another blog software and found zine. it’s quite similar to wordpress but written in python. i did a test installation and till now everything went perfect. i didn’t test it properly but only the fact that it’s written in python makes me belive in it much more than in wordpress.

image manipulation with python

Wednesday, March 11th, 2009
in webapps you often need to manipulate images. create thumbnails, add shadows or borders, create captchas and many other things. usually i use imagemagick. it’s a very powerfull image manipulation tool with apis for many languages. i often experienced some difficulties when trying to use it with a specific language. there i usually almost no documentation for the apis and it looks like it’s not a lot used. many tend to call the imagemackig executable trough a os call.

how is it with python

i found a few image magick python modules. most were completely undocumented, unfinished or more or less uninstallable (at least on a 64 bit debian). the one who works properly is PythonMagick. i first tried to install the lattest version but due to some dependencies problems it wasn’t possible on my ubuntu system. there is a deb package available which was easyli installed with: sudo apt-get install python-pythonmagick it’s not the latest version but it worked ok, at least for my needs.

how do you use it

i found no documentation for it but it was quite simple to figure out the basic things. there is documentation for Magick++ which is a object oriented wrapper around imagemagick. all the image methods have short descriptions. to add a red 2 pixel border to an image for example you need following code: from PythonMagick import Image i = Image('example.jpg') # reades image and creates an image instance i.borderColor("#ff0000") # sets border paint color to red i.border("2x2") # paints a 2 pixel border i.write("out.jpg") # writes the image to a file the parameter “2×2″ of the border method is a geometry string used by imagemagick as input for many methods. another example who crops the image, flips it, adds an oil paint look and finally save it as a png. i = Image('example.jpg') i.crop("100x100+25+25") i.flip() i.oilPaint(2) i.save('out.png') if i find some spare time i will add a few more complex examples of how to use imagemagick from python.

python, i like the snake

Monday, March 2nd, 2009
90% of my development work during the last few years was done in java. i had to create a little project for the OLPC about a year ago. OLPC is using python very heavily and so i developed the tool in python. before that, i did only a few very simple things in python. a few weeks ago i started to develop webapplications in python. here are a few ideas and a list of packages/ software i used.

python versus java

compared with java, python is IMHO not as clean. it isn’t as “designed” as java is. but compared with, for example PHP, it’s a hyper clean and intuitive language. java is static typed and although also bytecode compiled like python, it needs an explicit compilation step. this makes developing python programs a bit more agile. for java and python there are lots of libraries/ packages available. as much as i have seen the python ones seems to be usually higher quality, but this can just be the selection i made and doesn’t need to be a general thing. the most important point in using python for webapps instead of java is that the python programs are usually fewer lines of code compared to the java programs. as much as i have seen till now i think this is also possible while having at least the same readability of the code like java programs. on the other hand you can write more easyli horrible code in python than in java. so python needs a bit more discipline. i will use python mainly for little not so critical projects, at least until i am fluent with it.

what did i use for webapps

with java i usually used an apache with mod_jk to connect to tomcat. i looked for a similar setup in python and found that there are a lot of possible ways but the most promising thing was the WSGI standard. there was a module for apache2 and so i was happy. WSGI is short of WebServer Gateway Interface and it sits somewhere between the webserver and python. compared with java where a application server is used who shares a common context for all servlets/ requests, WSGI-python scripts are loaded in a context for each process (apache processes i think), so caching and communication with other requests is not possible. but, if you are used to develop your applications with a cluster in mind it doesn’t hurt. you can use memcached for a cache.

because WSGI is a very low level interface it’s best to use a proper webframework. there are lots of them. the most are full blown frameworks and i am a bit allergic to them, i like it when i can put together the tools i want and doesn’t have to be pushed to use a certain way (forced, you are never. with full blown frameworks it’s the same as with certain girlfriends. they tend to give subtle hints about something but don’t force you, they are open to everyting but you get afraid what awaits you if you really go with something else then her preferred way). the only one i kind of liked was web.py. i’ts not the most beautiful but well built and it lets you do whatever you want if you like to. i use it mainly for mapping urls to code and webstuff like redirecting, send errors and stuff.

to access a database there are different packages available. i checked for orm mappers and found quite a lot of different implementations. the two best (as much or less as i can judge it) are Storm and SQLAlchemy. both are flexible, easy to use and have lots of functionality who doesn’t get in your way if not used. compared with hibernate it’s a dream.

to render html or whatever, i was looking for a good template language. there are many of them and i decided to use Jinja2. its the one i liked most but, hey it’s a template language, they are almost all usefull, properly built and support about the same featureset.

if i remember properly, thats all i needed, at least for the web aspect of the applications. python also has many very useful modules included, therefore you don’t need to include lots of modules. no crappy apache commons jars who give some functionality they missed to implement in the standard library.