Skip to main content

Ralsina.Me — Roberto Alsina's website

Writer's block

In­ter­est­ing thing about writ­ing a post ev­ery day: I did­n't get blocked yet. Sure, this is on­ly the 9th post, but con­sid­er­ing my pre­vi­ous post-a-­month ca­dence, and that I would just have noth­ing to write about, it's clear to me that writ­ing leads to writ­ing.

That is per­haps the most ob­vi­ous thing that not ev­ery­one no­tices: the way to do things is to do things. The way to write free soft­ware is to write soft­ware that is free. To go to Alas­ka you have to go. To Alas­ka. To write, you have to write. To do mu­sic, you have to do mu­sic. To make bread, you have to make bread.

Will ev­ery piece of bread you make be good? Will ev­ery­thing you write be good? Will you get to Alaska? No. You will fail.

But if you don't do failed crap first, then there is no way to do any­thing de­cent lat­er. I think it was Amadeo Car­ri­zo (fa­mous, old, goal­keep­er) who said about a (not so fa­mous, young, goal­keep­er) "He's not bad, but he needs to be scored on a few hun­dred times more be­fore he's good".

I have a long his­to­ry of fail­ure. I have a short sto­ry of suc­cess­es. I am work­ing on it.

Abandonment issues: rst2pdf

Of all the corpses of my pro­ject­s, there is one thatI feel worse about, which is rst2pdf. I feel bad about aband­non­ing sev­er­al, but rst2pdf was ac­tu­al­ly a use­ful tool, used by a bunch of peo­ple, and that it has nev­er gath­ered enough mo­men­tum with oth­er de­vel­op­ers is sad.

So, I will pick it up. I will spend about 4 hours a week on it. The plan is to:

  1. Gath­­er some patch­es that are lin­ger­ing on the is­­sue track­­er

  2. Fix some sim­­ple-ish bugs

  3. Make an­oth­er re­lease with 1) and 2)

And of course:

  1. Not let it fall in dis­­re­­pair again

In the meantime, here is a nice thing I just heard about. Dimitri Christodoulou has hacked rst2pdf so that it can handle the raw:: html directive.

This, dear friends is com­plete­ly nut­s, ab­so­lute­ly out of scope for any giv­en do­cu­tils tool, and just too cool :-)

I will try to hi­jack his code (prop­er cred­it and so on), and in­cor­po­rate it in­to rst2pdf.

And Dim­itri, or any­one else who wants to do cool stuff with rst2pdf: let me know! I will give you com­mit rights im­me­di­ate­ly!

Python context managers: they are easy!

This comes from this thread in the Python Ar­genti­na mail­ing list (which I strong­ly rec­om­mend if you read span­ish).

I was the oth­er day try­ing to do shel­l-scrip­t-­like-things on python (as part of a mon­ster set­up.py) and I was an­noyed that in shell it's easy to do this:

cd foo
bar -baz
cd -

Or this:

pushd foo
bar -baz
popd

Or this:

(cd foo && bar -baz)

And on Python I had to do this, which is ver­bose and ug­ly:

cwd = os.getcwd()
try:
    os.chdir('foo')
    os.system('bar -baz')
finally:
    os.chdir(cwd)

This is what I want­ed to have:

with os.chdir('foo'):
    os.system('bar -baz')

And of course, you can't do that. So, I asked, how do you im­ple­ment that con­text man­ager? I got sev­er­al an­swer­s.

  1. That's avail­able in Fab­ric:

    with cd("­foo"):
        run("bar")
  2. It's not hard to do:

    class DirCon­textM(ob­jec­t):
        def __init__(­self, new_dir):
            self­.new_dir = new_dir
            self­.old_dir = None
    
        def __en­ter__(­self):
            self­.old_dir = os­.getcwd()
            os­.chdir(­self.new_dir)
    
        def __ex­it__(­self, *_):
            os­.chdir(­self.old_dir)
  3. It's even eas­i­er to do:

    from con­textlib im­port con­textman­ag­er
    
    @con­textman­ag­er
    def cd(­path):
        old_dir = os­.getcwd()
        os­.chdir(­path)
        yield
        os­.chdir(old_dir)
  4. That's cool, so let's add it to path.py

  5. Maybe check for ex­­cep­­tions

    @con­textman­ag­er
    def cd(­path):
        old_dir = os­.getcwd()
        os­.chdir(­path)
        try:
            yield
        fi­nal­ly:
            os­.chdir(old_dir)

All in al­l, I learned how to do con­text man­ager­s, about con­textlib, about fab­ric and about path.py. Which is not bad for 15 min­utes :-)


Contents © 2000-2023 Roberto Alsina