Posts about python (old posts, page 8)

2006-10-17 21:06

No, I don't get a dime from them

For a few months I have been using an unmanaged virtual private server from Tektonic, and I love it.

What's that? Let's take it one word at a time, and then some more.

  1. It's a server: which means it's a full-ish linux installation. So it is capable of doing lots of things. I can run all sorts of weird python thingies in it if I want. IMAPS and SSMTP? No problemo.
  2. It's private: which means I am root on it. I have the shell. I choose what to install.
  3. It's virtual: it's a Virtuozzo partition in a real server. That means no custom kernel modules, and that since almost everything is shared with other instances, 5GB of disk and 128MB of RAM go a long way.
  4. It's unmanaged: which means I manage it. Which is just the way I prefer it, since that's my job.
  5. It's cheap. I started on a 8 dollars a month plan (which doesn't seem to be there anymore, the current cheapest is a 15 dollars plan).
  6. It's a throwaway. I want to host some client as a favour? I just put it there. I could even rent another of these servers for a while, use it, then close it. Backups? Clicking on a webpage saves the image! Other than that... I back it.
  7. Fixed IPs. All you want (for extra coins).
  8. A home away from home. All my stuff is there. I need it, I get it. Without bothering about having my own server at home via no-ip or somesuch (which of course I still have too ;-)
  9. It works. It hardly ever breaks. And having survived expensive, managed servers, this baby is working just as well.
  10. It's a nice gift. Suppose you have a connection to a free software project/LUG/family/whatever, and they need a place on the internet. Why not sponsor them with something like this? I offered one to PyAr (which didn't take it, but it's the thought that counts ;-)
  11. The ultimate learning experience: you can restore the system in 2 minutes. Want to play/learn sysadmining? Do it on the real virtual thing! Much cheaper than hosing your own box ;-)
  12. They offer a good service. So, people should know about it. And of course... if you know a similar, but even better deal... I'm all ears!

2006-10-05 13:38

A different UNIX Part II: A better shell language

One of the things people study when they "learn unix" is shell scripting and usage. Because every system has a shell, and if you learn to use it interactively, you are half way there to automating system tasks!

Let's consider that for a moment... what are the odds that the same language can be good for interactive use and for programming? I say slim.

Not to mention that learning shell as a way to learn unix is like going to a school that teaches TV production, and studying the remote. While useful, not really the important tool (ok, that analogy doesn't work at all. But it sounds neat, doesn't it?).

The first thing is that today's Linux domination of the unixsphere has caused a serious monoculture in shell scripting: everyone uses bash. The more enlightened ones may check that their scripts work on some other Bourne-style shell.

There are no important distributions (or proprietary unixes) that use a csh or anything like it. Debian has a policy that things should work without bashisms. That's about as good as it gets.

Writing a dozen pages on how shell sucks would be trivial. But uninteresting.

So, let's think it over, and start from the top.

What should a shell scripting language be like?

What doesn't matter?

Let's tackle these things. I invite anyone to add extra ideas in the comments section.

What should a shell scripting language be like?

  • Interpreted (obvious)

  • Dynamic typing (you will be switching ints to strs and viceversa all the time).

  • Easy incorporation of other programs as functions/methods/whatever.

    That pretty much is what makes it a shell. ls should be indistinguishable from something written using the shell itself.

  • Pipes. This is a must. Unix has a bazillion tools meant to be used in command pipelines. You can implement a RDBMS using that kind of thing (look for nosql). Leverage that.

    But even here, on its strength, the shell is not perfect. Why can't I easily pipe stderr and stdout to different processes? Why can't I pipe the same thing to two processes at the same time (yes, I know how to do it with a neat trick ;-)

  • Globbing. *.txt should give you a list of files. This is one of the obvious things where sh is broken. *.txt may be a string or a list, depending on context... and a list is just a series of strings with blanks. That is one of the bazillion things that makes writing shell scripts (at least good ones) hard:

    [[email protected] ralsina]\$ echo *out
    [[email protected] ralsina]\$ echo *outa
  • A list data type. No, writing strings separated with spaces is not ok. Maybe a python-style dictionary as well?

  • Functions (obvious)

  • Libraries (and ok, the shell source mechanism seems good enough)

  • Standalone. It shouldn't spawn sh for any reason ;-)

What doesn't matter?

  • Performance. Ok, it matters that a five-liner doesn't take 50 minutes unless it has to. But 1 seconds or two seconds? not that important.
  • Object orientation. I don't see it being too useful. Shell scripts are old-fashioned :-)
  • Compatibility to current shells. Come on. Why be like something that sucks? ;-)

Now, the example

Let's consider a typical piece of shell script and a rewrite in a more reasonable syntax.

This is bash (no it doesn't work on any other shell, I think):

DAEMONS=( syslog network cron )

# Start daemons
for daemon in "\${DAEMONS[@]}"; do
      if [ "\$daemon" = "\${daemon#!}" ]; then
              if [ "\$daemon" = "\${daemon#@}" ]; then
                      /etc/rc.d/\$daemon start
                      stat_bkgd "Starting \${daemon:1}"
                      (/etc/rc.d/\${daemon:1} start) &>/dev/null &

And since DAEMONS is something the admin writes, this script lets you shoot in the foot in half a dozen ways, too.

How about this:


# Start daemons
for daemon in DAEMONS {
      if ( daemon[0] != "!" ) {
              if ( daemon[0] == "@" ) {
                      stat_bkgd ("Starting "+daemon[1:])
                      /etc/rc.d/+daemon[1:] ("start") &> /dev/null &
              } else {
                      /etc/rc.d/+daemon ("start")

Of couse the syntax is something I just made up as I was writing, but isn't it nicer already?

2006-06-07 11:41

Eating like a Python, for Python, and other stuff

I am now almost recovered from the trip to Santa Fe for the Jornada Python and I have reached a few conclussions.

  1. I do kinda miss my old city.
  2. It does take slightly longer than 15 minutes to show it ;-)
  3. This kind of events is cool
  4. I am old, and can no longer take a 6-hour bus ride and then go to work.

On more detail:

The city is much prettier than it was when I left 5 years ago. Lots of construction going on, everything is cleaner, nicer, and more organized, it seems.

Man, I had forgotten how great you can eat there. And how cheaply. Remember I mentioned that fish-eating place? Well, it's only $22. That's pesos. So, it's about 7 dollars for all the delicious fish you can eat, and all the cold beer you can drink.

Which in my case is quite a lot. Although I am getting slow on the beer, too.

The only bad thing about it is that the guys that went on Sunday got Pacú and we didn't. Dammit!

Then there is the Sociedad Alemana. The ugliest place ever. The best picada [1] ever. It's a clubhouse founded by a former Graf Spee crewman. If they eat like that on the german navy, I would consider joining. And I get seasick.

Oh, and the lebanese restaurant. I was forgetting that.

Of course all this eating and imbibing also meant I missed half the event, but the morning conferences were introductory, so I got the good bits anyway.

Specially: I now understand metaclasses! and decorators! Yipee!

We had a good time with Rosario, she got to know better some of my friends, specially Javier and César [aka Ned Flanders] (and Cesar's family, Norma, Florencia and Julian).

We took a ride over the costanera (a riverwalk), peppered with comments like this (all dates are wrong):

Here was the Piedras Blancas beach, until the flood of 87.

That's where the old train bridge was until the flood of 69, then there was a liftchair, until the flood of 82.

That suspension bridge is not the original, which was washed off in 83 (and then stolen)

This piece of the costanera is new, the old one was gone in 78

And so on. You see, this place is... kinda floody. Architecture is not a permanent thing.

On yet another angle, I am going back to school next year. I don't intend to finish my Maths degree, but will go for System Engineering instead. We'll see what happens.

[1] We had the german style picada. That means plates of potato salad, smoked ham, three different sausages, leverwurst, pickles, three cheeses, and a sausage cazuela. The classic Santa Fe style picada may include any of those plus meatballs, milanesa (schnitzel-like thing), chips, olives, lupines (a kind of pickled bean), small sandwiches, and a hundred other things. All that in small pieces, ready to eat with fingers and/or toothpicks. All salty. So you get extra beer.

2006-05-23 19:28

A silly entry

Today's entry has no pieces of neat python code, no questions, no critic of anything, no interesting link, no hint of anything I wrote/(would/will)write, no funny piece, no unfunny piece, no nothing.

I am tired today.

So, have a nice day :-)

2006-05-20 13:00

Python-v4l: neat!

Thinking about CherryTV and ways to turn it into a real application, I thought the worse piece of it was the reliance on v4lctl, and how really you just don't know if it works or not, and how you can't finetune, and whatever, and run into Python-v4l.

It has remained apparently untouched by two years, but I managed to build it with one edit and to make it work by switching a line to an alternate version (they are both there), and it's nice.

Here's the example TV viewing application using it:

#!/usr/bin/env python
# Sample TV viewing application for pyv4l >= 0.3 - by Michael Dove
#<[email protected]>
# Note: this does the imaging in grab mode. Performance is limited.
# I average 35 fps @ 320x240. Disabling the writes to the tk window yeilds 90+ fps.

import v4l
import Image
import ImageChops
WIDTH = 320
HEIGHT = 240
vid ='/dev/video')
cap = vid.getCapabilities()
print "Device Name: %s" % cap[0]
print "Type: %d" % cap[1]
print "Channels: %d" % cap[2]
print "Audios: %d" % cap[3]
print "Maximum Width: %d" % cap[4]
print "Maximum Height: %d" % cap[5]
print "Minimum Width: %d" % cap[6]
print "Minimum Height: %d" % cap[7]
print vid.getChannel(0) # TV
vid.setChannel(0) # set to TV

import Tkinter
import ImageTk
photo = ImageTk.PhotoImage("RGB",(WIDTH,HEIGHT))
label= Tkinter.Label(tk,text="mini TV",image=photo,width=WIDTH,height=HEIGHT)

nextFrame = 0;

    while 1:
        output = vid.getImage(nextFrame)
        im = Image.fromstring("RGB", (WIDTH, HEIGHT), output)
        # update Tk label
        nextFrame = vid.queueFrame()

except Tkinter.TclError:
    print "something"

If you have seen the equivalent C app... well... nice job here!

2006-05-17 08:29

Flickr Banners

If you are one of the three persons who actually see this blog on its site instead of reading it through some sort of aggregator, you may have noticed I have a banner.

This is what it looks like:


Nice, ah?

It's not very original (look for "spell with flickr" to see where I stole the idea), but I wrote a script to do it. So here it is, feel free to steal it too, it's your turn.

You will need this, ImageMagick, and a Flickr API key.

To use it simply call it like this:

python something

And you will have a something.jpg with something in it.

#!/usr/bin/env python

import flickr,sys,random
from urllib import urlopen
import os

g=flickr.Group(id="[email protected]")

urls = []
for l in sys.argv[1]:
  if l=="a":
  if l=="i":
  photos = g.getPhotos(tags=[l], per_page=50)
              size='Square', urlType='source'))

for i in range(0,len(urls)):

os.system ("montage -tile %dx1 -geometry +0+0 %s %s"%(
            len(urls),' '.join([ '%d.jpg'%x for x in range(0,
            len(urls)) ]), sys.argv[1]+'.jpg'))

2006-05-16 00:20

Using runit is even simpler

I have posted in the past about runit.

One of the problems people migrating to runit have is that all your services are SysV scripts.

The runit author has a collection of scripts you can use, but usually they require some adjustment to work on a specific version of Linux.

So, I wrote a lame python script that takes the SysV scripts you are currently using and turns them into runit services, including dependencies.

Suppose you usually start on runlevel 3. Then you save this script and run it like this:

mkdir services
python 3

And you should end with a bunch of runit services inside services/

Those services will start in roughly the same order as if you were using SysV init. That's probably way too much dependencies.

The main difference is that kdm will start earlier and the ttys will start way earlier than you are used to.

I have found that my notebook boots faster using this, but I can't provide a bootchart because it simply doesn't seem to work in my computer.

If anyone is willing and able to run the tests and quantify the difference, I am all ears.

For some reason kudzu, iptables and arptables_jf don't work with this approach, so just stick them at the bottom of /etc/runit/1

Also, please understand that these are not correct runit services. They are not managed, so if your service crashes it stays crashed.

So, you should still eventually migrate to correct scripts. This is just a way to make that simpler.

2006-05-12 12:17


A simple (yeah, sure!) solution so you can watch TV on all the computers in your home. CherryTV!

It's small, it's cherryPowered, and it does work.

2006-05-11 08:37

If you see a russian, it's my doing

In this russian site, you can see how they got my post about the Santa Fe python event. And apparently what impressed them is the thing about the fish and the beer.

См. вас там!

2006-05-08 23:40

I Jornada Python en Santa Fe, Argentina

Bilingual entry here:

Yo nací en Santa Fe. Me gusta Python. Hay un evento de Python en Santa Fe.

Pienso asistir solamente como espectador, quedarme callado y escuchar cosas interesantes que digan los demas.

Sin embargo, si quieren experimentar un lugar donde se pueden comer 30 platos de pescado por 20 pesos (y te podes comer los 30, aunque yo conozco solamente una persona con la suficiente fortaleza estomacal), mas toda la cerveza que puedas tomar... bueno, nos vemos en Santa Fe el 3 de junio.

Mas informacion aca

I come from Santa Fe. I like Python. There is a Python event on Santa Fe.

I intend to attend in a strictly spectator fashion. I will listen to other guys telling interesting stuff, and keep quiet.

However, if you want to experience a place where you can eat 30 different fish dishes for about 7 dollars (and I mean you can eat all 30, although I know only one person of such superhuman fortitude), plus all the beer you can drink... well, I will be in Santa Fe on June 3rd.

More information here

Contents © 2000-2018 Roberto Alsina