Skip to main content

Ralsina.Me — Roberto Alsina's website

Python is Not a Configuration File Format

There is a large thread in red­dit about us­ing Python as con­fig­u­ra­tion file for­mat. I want to be clear about that:

DON'T DO THAT, UN­LESS YOU HAVE A VERY GOOD REA­SON.

If you need to ask if it's a good idea, then you don't have a good rea­son. If you are sure that you have a good rea­son, then maybe you have a good rea­son.

There are many rea­sons for it, but I will ex­plore just two, then of­fer a sug­ges­tion.

Python is read­-on­ly, and con­fig­ur­ing is not pro­gram­ming.

Sure, it's easy to use python as a con­fig file. You just im­port the thing, and there you go, all the da­ta is there. But now your con­fig­u­ra­tion syn­tax is a gen­er­al pur­pose lan­guage that can do things like pop up di­alogs when you parse it.

Your con­fig can now de­pend on the whole in­ter­net, the hard­ware, the weath­er, and in­ter­ac­tive in­put. Pow­er­ful? Sure. Good idea? May­be, some­times. But your ap­pli­ca­tion is now not able to con­fig­ure it­self.

If your ap­pli­ca­tion wants to store any kind of set­ting, it won't be able to. So most in­ter­ac­tive, desk­top app­s, just should not use python for this, ev­er.

But what about non-in­ter­ac­tive tool­s? Well, us­ing python means that oth­er tools can't write to the con­fig file, ei­ther, which makes the tool less pow­er­ful. The pow­er to have tools use tools is one of the cor­ner­stones of mod­ern com­put­ing, and you just cut your app off that ecosys­tem. De­pend­ing on what lan­guage the tool us­es it may not even be able to parse your con­fig file.

And what hap­pens when some­one is told "use this con­fig frag­ment to achieve X"? Well, odds are, if the re­cip­i­ent has done any­thing that takes ad­van­tage of us­ing python as a con­fig for­mat, then the frag­ment will not work. It would be like do­ing copy­/­paste from ran­dom code in github in­to your own pro­gram and ex­pect­ing it to work.

So, you can't write to it from the ap­p, you can't get con­fig­u­ra­tion tips from the in­ter­net, you can't use oth­er tools to mod­i­fy con­fig files, and oth­er tools have a hard time pars­ing your files.

Al­so, it means that to han­dle the gen­er­al case of con­fig­ur­ing your ap­p, you need a pro­gram­mer. That is al­most cer­tain­ly overkil­l. Very few apps need that kind of thing. If your app can on­ly be con­fig­ured by pro­gram­mer­s, you may have failed at mak­ing a good app (ex­cep­tions ex­ist).

And what's the ad­vice? Well, the ad­vice is "don't do that" and the corol­lary is "con­fig­ure us­ing data, not code". use IN­Is, or XM­L, or YAM­L, or JSON, or plain text files, or what­ev­er. But not code.

PS: My lat­est pro­jec­t, Niko­la us­es python as a con­fig­u­ra­tion lan­guage. I thought I had a good rea­son. I did­n't.

The Future of PyQt by Example

Three years ago, I start­ed a se­ries of long posts called "PyQt by Ex­am­ple". It reached five posts be­fore I aban­doned for a se­ries of rea­sons that don't mat­ter any­more. That se­ries is com­ing back start­ing next week, rewrit­ten, im­proved and ex­tend­ed.

It will do so in a new site, and the "old" posts will be re­tired to an ar­chive page. Why? Well, the tech­nolo­gies used in some of them are ob­so­lete or don't quite work nowa­days. So, the new ver­sions will be the pre­ferred ones.

And while I am not promis­ing any­thing, I have enough writ­ten to make this some­thing quite longer, more nice­ly lay­out­ed, more in­ter­est­ing and make it cov­er more ground. BUT, while do­ing some checks on the traf­fic sta­tis­tics for the old post­s, some things popped out.

This was very popular

About 60% of my site's traf­fic goes to those five post­s. Out of about 1200 posts over 12 years, 60% of the view­ers go to the 0.4% of the pages. That is a lot.

It's a long tail

The traf­fic has not de­creased in three years. If any­thing, it has in­creased

https://p.twimg.com/Aw0MHhoCAAAXmro.png:large

A long and tall tail.

So, all this means there is a de­sire for PyQt doc­u­men­ta­tion that is not sat­is­fied. I am not sur­prised: PyQt is great, and the rec­om­mend­ed book is not free, so there is bound to be a lot of de­mand.

And, here's the not-­so-rosy bit: I had un­ob­tru­sive, rel­e­van­t, out­-of-the-way-but-vis­i­ble ads in those pages for more than two years. Of the 70000 unique vis­i­tors, not even one clicked on an ad. Don't wor­ry, I was not ex­pect­ing to get mon­ey out of them (although I would love to some day col­lect a $100 check in­stead of hav­ing google hold my mon­ey for me ad eter­num).

But re­al­ly? Not even one ad click? In more than two years, thou­sands of peo­ple? I have to won­der if I just at­tract cheap peo­ple ;-)

PyCamp Starts This Week

This fri­day marks the be­gin­ning of the 2012 Py­Cam­p. What's Py­Cam­p? It's a lot of python pro­gram­mers (this year, about 50) gath­er­ing in an iso­lat­ed place for a long week­end, with noth­ing to do ex­cept code. We will have our meals catered, there are no TVs, there is hard­ly any­thing with­in walk­ing dis­tance, and it's go­ing to be very cold.

So, it's go­ing to be awe­some. It's a rare chance for me to spend a few days hack­ing at my own per­son­al pro­ject­s, un­in­ter­rupt­ed by more im­por­tant things like fam­i­ly, work, cook­ing, or so­cial­iz­ing ex­cept be­tween nerd­s.

Sure, there is the oc­ca­sion­al mono­cy­cle rid­ing, or jug­gling lesson, or shoot­ing prac­tice, but re­al­ly, three or four sol­id days of hack­ing.

I in­tend to work on projects re­lat­ed to Niko­la my stat­ic site gen­er­a­tor, so if you are in­ter­est­ed in that and go­ing to py­cam­p, I want to talk with you.

And if you are in­ter­est­ed but not go­ing to py­cam­p, there is no rea­son not to join in a vir­tu­al sprint. We'll have in­ter­net. There is IR­C. I will have time. It's a week­end! Please share any in­ter­est­ing ideas you have about stat­ic site gen­er­a­tors in the niko­la-dis­cuss group and we'll see how much can get im­ple­ment­ed or at least start­ed.

A Simple Nikola Link Checker

One of the most im­por­tant things when you are build­ing a stat­ic site gen­er­a­tor like Niko­la is that your site should not be bro­ken. So, I re­al­ly should have done this ear­li­er ;-)

This is a very sim­ple link check­er that en­sures the pages Niko­la gen­er­ates have no bro­ken links. I will make it part of Niko­la prop­er once it's more pol­ished and doit sup­ports get­ting a list of tar­gets

To try it, get it and run it from the same place where you have your conf.py, right after you run doit.

import os
import urllib
from urlparse import urlparse

import lxml.html

def analyze(filename):
    try:
        # Use LXML to parse the HTML
        d = lxml.html.fromstring(open(filename).read())
        for l in d.iterlinks():
            # Get the target link
            target = l[0].attrib[l[1]]
            if target == "#":  # These are always valid
                continue
            parsed = urlparse(target)
            # We only handle relative links.
            # TODO: check if the URL points to inside the generated
            # site and check it anyway
            if parsed.scheme:
                continue
            # Ignore the fragment, since the link will still work
            # TODO: check that the fragment is valid
            if parsed.fragment:
                target = target.split('#')[0]
            # Calculate what file or folder this points to
            target_filename = os.path.abspath(
                os.path.join(os.path.dirname(filename), urllib.unquote(target)))
            # Check if it exists, or report it
            if not os.path.exists(target_filename):
                print "In %s broken link: " % filename, target
    except Exception as exc:
        # Something bad happened, report
        print "Error with:", filename, exc

# This is hackish: we use doit to get a list of all
# generated files. Minor modifications would let you check
# the non-generated files as well.

for task in os.popen('doit list --all', 'r').readlines():
    task = task.strip()
    if task.split(':')[0] in (
        'render_tags',
        'render_archive',
        'render_galleries',
        'render_indexes',
        'render_pages',
        'render_site') and '.html' in task:
            # It looks like a generated HTML file
            analyze(task.split(":")[-1])

Confessions of a Troll

And then he told me "Rober­to's a known trol­l" and I was like "re­al­ly?" he seems like a nice guy!

—Not say­ing who said it

So, that hap­pened a while ago in a din­ner. And I am that trol­l. I must con­fess it, so here it is. I have a long, long, long his­to­ry of trolling. Per­haps I could ra­tio­nal­ize it in­to just be­ing ar­gu­men­ta­tive, or just en­joy­ing a good ar­gu­men­t, but no, re­al­ly, I de­rive plea­sure from see­ing peo­ple re­act to what I say.

That could maybe be in some way con­struc­tive, I could just do it to make peo­ple see things from a dif­fer­ent an­gle, and in my good mo­ments I do that. But in oth­ers I just find some­one I dis­like, or some­one who says some­thing that rubs me the wrong way, and I ... well, it's not pret­ty.

What I usu­al­ly do is up the ante. I es­ca­late. I say out­ra­geous things. Usu­al­ly just slight­ly more out­ra­geous than what the oth­er guy says, so I can still ra­tio­nal­ize it as "he start­ed it", or "he de­serves it" but hon­est­ly, deep in­sid­e, I know it's not true. I know I am be­ing nasty be­cause I like it. And that suck­s. It sucks that I have this mean streak in me, and I can't get rid of it. It sucks that I have fun in­sult­ing the oth­er poor bas­tard.

I could even say that most of the peo­ple I troll are trolls them­selves. But that's not re­al­ly an ex­cuse. If they de­served con­tempt for their trolling, then I am just as de­serv­ing for mine. I am sure they, al­so, have their rea­sons and those rea­sons sound per­fect­ly ra­tio­nal to them­selves.

I could say that I of­ten troll those who spread poi­sonous speech. That I troll those who are spread­ing mis­in­for­ma­tion and evil ideas. And yes, that is true, but again, I could re­ply with­out trolling. I could re­ply ra­tio­nal­ly, and just point out where they are say­ing non­sense, where they are spread­ing stu­pid­i­ty, and avoid the flair, avoid the gra­tu­itous in­sult.

I could say that I troll those who both­er me per­son­al­ly out of their own free will. So yes, telling a Je­ho­vah's Wit­ness that I will be at the door as soon as I wash my hands of the blood sac­ri­fice I make to Sa­tan is fun­ny, but why is it any bet­ter than just telling them I am not in­ter­est­ed.

Oh, sure, I could say that telling some­one who in­sult­ed me "opin­ions are like ass­holes, and so are you" is some­what clev­er, but I could just say "y­ou are wrong, rude, and un­de­serv­ing of my at­ten­tion".

I sure could say that trolling some­one who is rude, in­sult­ing, ob­nox­ious and dis­rup­tive may teach him (it's al­ways a he, is­n't he?) a lesson, but it's just not true. They just see those re­spons­es as chal­lenges.

I could say that feed­ing the oth­er troll is just harm­less fun, but it's not true be­cause I know it hurts peo­ple, and cre­ates an ob­nox­ious en­vi­ron­men­t. I could say that trolling those who de­serve to be trolled is fair. But it's not true be­cause I am noone's judge.

So, if I have trolled you, ac­cept this post as an apol­o­gy. Not a "sor­ry if I of­fend­ed you" apol­o­gy, but a straight apol­o­gy. I am sor­ry I trolled you. I will try to stop.

I will not stop say­ing and writ­ing what I think, and I will not ab­stain from re­ply­ing to oth­er peo­ple's opin­ion­s, but I will do a hon­est try to do it in a not-trolling man­ner, of of­fer­ing my opin­ion as it is, in­stead of as a per­for­ma­ce to make the oth­er one feel hu­mil­i­at­ed or hurt or un­de­serv­ing.

Be­cause, and here is my on­ly piece of ex­cuse: I hate bul­lies. I was sent to school two years too ear­ly. So that you have an idea how that can af­fect some­one, I am of av­er­age height, but un­til I was 13 I was con­vinced I was very short. I man­aged to be­come a de­cent swim­mer, but un­til I was 12 or so, I was con­vinced I sucked at all sports and games. I was in­ceas­ing­ly bul­lied since I first ev­er met my "peer­s" un­til I was able to kick the crap out of them. I be­came a tough kid. I could take a beat­ing like you can't imag­ine, be­cause I was too small to hit back, and the on­ly way not to be a to­tal los­er was to keep get­ting up and go­ing to be beat­en again, while mak­ing sar­cas­tic com­ments.

And it turned out that those sar­cas­tic com­ments worked. I got my ass kicked, but the next day that kid re­mem­bered how I was get­ting back on my feet and jok­ing about his moth­er. And that, of course, lead to fur­ther bul­ly­ing. When I was about 12, I was so ex­treme, I scared ev­ery­one so much, they stopped. I was scary. I talked like a freak­ing psy­cho. I prob­a­bly was close to be­ing one.

And the les­son I learned was that if you make fun of peo­ple, they got hurt enough to want to avoid it lat­er. So, I was, in fac­t, a bul­ly my­self. And thus, I be­came that which I hat­ed. I have had bet­ter times and worst times, I have been more in con­trol and not so much. And it has tak­en me a long time to fig­ure this out. I don't like what I be­came. So I will stop. And I am sor­ry.

Just gen­tle rib­bing and self­-dep­re­ca­tion from now on. Prom­ise.


Contents © 2000-2023 Roberto Alsina