Skip to main content

Ralsina.Me — Roberto Alsina's website

Python Trick: Save anything in config files

The Python con­fig ob­jects are con­ve­nient and sim­ple, but they have a prob­lem: you can on­ly save strings. That means you need to store num­bers as strings and re­mem­ber to use the get­int()/get­float() meth­ods (or co­erce by hand!), which is er­ror prone and an­ti-python­ic. Stor­ing a list is even ugli­er.

You could store ascii pick­les, but those are pret­ty un­pleas­ant to read in some cas­es.

Here's my so­lu­tion: En­code it us­ing a JSON en­coder first! (I am us­ing demj­son)

Sil­ly ob­vi­ous code frag­men­t:

def getValue(section,key,default=None):
    try:
        return JSON().decode(conf.get (section,key))
    except:
        return default

def setValue(section,key,value):
    value=JSON().encode(value)
    try:
        r=conf.set(section,key,value)
    except ConfigParser.NoSectionError:
        conf.add_section(section)
        r=conf.set(section,key,value)
    f=open(os.path.expanduser('~/.bartleblog/config'),'w')
    conf.write(f)
    return r

With just a lit­tle ef­fort you can have a read­able ascii typed python con­fig file.

Today's first hour of hacking...

... has been all about UI.

I have al­ways had a prob­lem when writ­ing PyQt app­s: stock icon­s.

Which ones should I use? Where are they?

I usu­al­ly fished through the crys­talsvg icon set un­til I found one that seemed to be what I need­ed, and then copied it to my ap­p.

Sad­ly, that's an­noy­ing in sev­er­al ways:

  1. Since those are PNG icon­s, you need to find the right size.

  2. Not all icons are there for all sizes!

  3. Be­­cause of 2, I need to check three or four fold­ers to see all the icon­s.

So, I de­cid­ed to cut my loss­es, and see what else could be done. And here it is:

bartleblog11.png

I am now us­ing all SVG icon­s, from the rein­hardt set that will look equal­ly out of place in all OS­s, but which I like (and I think look awe­some with this re­laxed Domi­no the­me). And be­cause they are all SVG, I don't care about sizes, and they are all in the same place, and all is good.

And when­ev­er Oxy­gen is re­leased, all I need to do is switch the files around and that's that. Which is nice, too.

Of course there is a catch... it does look out of place, and I ex­pect many to find it ug­ly. So what, since I am the on­ly us­er of this ap­p! ;-)

Today's two hours of hacking

  • Done with the main blog con­­fig di­a­log.

  • Fixed a dozen bugs

  • Gen­er­ate the blog in a rea­­son­able place

  • Fixed a lot of UI bugs (tab or­der­s, sizes)

Still lots and lots of things to be done, tho.

Making your QTextBrowser show remote images

It's re­mark­ably easy to turn your QTextBrows­er in­to a lim­it­ed web browser, at least good enough to show im­ages from the we­b.

Here's all the code:

from PyQt4 import QtCore,QtGui
import urllib, os, md5

class PBrowser(QtGui.QTextBrowser):
    def loadResource(self, type, name):
        url=unicode(name.toString())
        ret=QtCore.QVariant()
        if url.startswith('http://'):
            dn=os.path.expanduser('~/.bartleblog/cache/')
            if not os.path.isdir(dn):
                os.mkdir(dn)
            m=md5.new()
            m.update(url)
            fn=os.path.join(dn,m.hexdigest())
            if not os.path.isfile(fn):
                urllib.urlretrieve(url, fn)
            ret=QtGui.QTextBrowser.loadResource(self, type, QtCore.QUrl(fn))
        else:
            ret=QtGui.QTextBrowser.loadResource(self, type, name)
        return ret

And here's bartle­blog tak­ing ad­van­tage of it:

bartleblog10.png

It even has a prim­i­tive cache and ev­ery­thing ;-)

PHP is for Lucre (II)

I prob­a­bly should have charged more.

Things I learned in this pro­jec­t:

  • If they tell you the de­sign is im­­por­­tan­t, it does­n't mat­ter that said de­sign in­­­cludes:

    • Fixed pix­el-height text in­­­puts (so when you re­­­size fonts you can't see what you write)

    • Mis­­­aligned fields and la­­­bels

    So, I need­ed to use Smar­­ty to gen­er­ate the ex­act same HT­M­L, work­ing just as bad as the orig­i­­nal. And it is thrice the work of do­ing it right.

  • If de­­ploy­­ment is on Win­­dows, re­­gard­­less of hav­ing the same ver­­sion of PH­P, you are go­ing to get in trou­ble one way or an­oth­er.

    In or­der to do it quick & cheap & good, I used PEAR mod­­ules ex­ten­­sive­­ly. And now I have to make sure all of them are in the serv­er... where there is no re­­mote ad­min so­lu­­tion us­able. So I have to be on­site to de­­ploy. I can't even re­mem­ber the last time I had to do such a thing :-(

So, I was cheap­er than the oth­er pro­pos­al­s, and maybe I was a bit too cheap. Live and learn :-)


Contents © 2000-2020 Roberto Alsina