2010-09-14 12:56

Very pythonic progress dialogs.

Sometimes, you see a piece of code and it just feels right. Here's an example I found when doing my "Import Antigravity" session for PyDay Buenos Aires: the progressbar module.

Here's an example that will teach you enough to use progressbar effectively:

progress = ProgressBar()
for i in progress(range(80)):
    time.sleep(0.01)

Yes, that's it, you will get a nice ASCII progress bar that goes across the terminal, supports resizing and moves as you iterate from 0 to 79.

The progressbar module even lets you do fancier things like ETA or fie transfer speeds, all just as nicely.

Isn't that code just right? You want a progress bar for that loop? Wrap it and you have one! And of course since I am a PyQt programmer, how could I make PyQt have something as right as that?

Here'show the output looks like:

progress

You can do this with every toolkit, and you probably should!. It has one extra feature: you can interrupt the iteration. Here's the (short) code:

# -*- coding: utf-8 -*-
import sys, time
from PyQt4 import QtCore, QtGui

def progress(data, *args):
    it=iter(data)
    widget = QtGui.QProgressDialog(*args+(0,it.__length_hint__()))
    c=0
    for v in it:
        QtCore.QCoreApplication.instance().processEvents()
        if widget.wasCanceled():
            raise StopIteration
        c+=1
        widget.setValue(c)
        yield(v)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)

    # Do something slow
    for x in progress(xrange(50),"Show Progress", "Stop the madness!"):
        time.sleep(.2)

Have fun!

2010-09-10 11:52

The first english Issue of PET (our Python Magazine) is out!

Hell yeah! It has been a lot of work but it's out at http://revista.python.org.ar

Some articles:

  • PyAr, The History
  • from gc import commonsense - Finish Him!
  • Painless Concurrency: The multiprocessing Module
  • Introduction to Unit Testing with Python
  • Taint Mode in Python
  • Applied Dynamism
  • Decorating code (Part 1)
  • Web2Py for Everybody

It's available in pretty much every format anyone can read, and if your favourite is not there, we will make it for you or may I be smote by the flying spaghetti monster's noodly appendage!

AFAIK there is no other Python magazine being published (feel free to correct me), so it's kind of a big thing for us in PyAr (the Argentina Python community) that we are doing one, and in two languages.

But why stop here? Want it to be available in your language? Contact us at [email protected] it may be doable!

And of course, very soon there will be a call for articles for Issue 2, and trust me: that one's going to be epic: this one was just a warmup.

2010-09-09 13:50

Making deployment of desktop Python apps trivial: an idea

Proprietor and printer in front of Schwartz Print Shop in Minneapolis

Here's what I'm thinking: how hard could it be to make PyQt app deployment absolutely easy? Well, I am guessing: not very hard.

Here's the trick: see what works in the real world, and adopt it.

Question: what has deployed billions of apps and has its users happy? Answer: phones app stores.

Question: how do they work? Answer: well, that's not that short, so let's start explaining.

As I see it, a reasonable app store has the following components:

A Stable Deployment Target

You can't deploy from the store if you don't know what you are deploying into. If the target platform is shaky, you just can't know how to deploy without user assistance, and we are trying to make this easy for the user, which means that's not acceptable.

So, what's a stable deployment target we can provide?

  • PyQt (so we can deploy GUIs to all major desktop platforms)
  • Python standard library
  • Selected modules

What can be (and should be) bundled with the app?

  • Pure python modules
  • Artwork and other resources

What may be bundled:

  • Python modules written in C/C++, but you then have to redo the app for each platform, and that kinda sucks.

Deployment Services

  • Apps should be able to check if there is a new version of them in the store, to ask for upgrades.
  • Apps should be added by the deployment platform nicely into the host system's menus, desktop, etc.

Monetization Services

  • Some way to charge for apps. Even for open source apps, you could ask for U$S0.99 if you install them through the store. Optional, of course, and up to the app owner.
  • Ad platform? There must be a good one for desktop apps somewhere?

The Store Itself

  • A website that downloads a "package" associated with a local deployment application.
  • A app store app. Install things not via web, but via a desktop application.

I don't expect a functional version of this would take me more than a week working fulltime to implement. Of course then there are all sorts of usability, looks, etc. things to consider.

And... I am going to do something I very rarely do. I am going to ask for money.

As an experiment, I have setup a project at http://www.indiegogo.com/Qt-Shop and set a funding goal of U$S 600.

There you can fund me. I promise that if the project is totally funded, I will deliver. If it isn't, I may deliver anyway. I would prefer to have the money though.

The platform would be released under GPLv2 or later.

2010-09-08 13:28

Why we are here.

Warning: rant ahead.

Yesterday the government of Argentina announced that they are giving way 3 million netbooks to students. They also announced that they are giving them the option of Ubuntu or Windows 7.

There was, of course, the typical reaction from the FLOSS side: why are they giving Windows to the students when Linux is better? It's unfair that the government pays for Windows!

I am here to tell you to grow up and stop being a baby. I am here to tell you to stop treating others like babies.

I think I can do this because I am immune to criticism from the FLOSS crowd: I am a member of that crowd. I have an awesome FLOSS pedigree, I have used nothing but Linux for over 15 years. And I have a thick skin and I don't care much what other people say, in principle, unless they give me good reasons to care. And I am telling you to stop complaining.

I am telling you that if the only reason to use a specific piece of software is because it's cheaper, you are accepting that piece of software sucks.

I want people not to just use Linux, I want them to want to use Linux. I want them to wait anxiously for the next release of Ubuntu or Firefox or whatever.

And the first step towards excellence is wanting to be excellent. If having to pay nothing for Windows or Ubuntu there is a certainty that Windows will win, then Ubuntu freaking sucks and needs to improve. People are not adopting it even if it's free? Then something is wrong, and figuring out what is important.

But even more important than finding the missing piece is knowing a piece is missing. Open source has grown complacent. It's grown self righteous. It's become adolescent, sure of its awesomeness and immortality.

I don't believe in many things, but I do believe in free will. I believe that people are not morons, I believe that if they prefer Windows, it's because it does something better, and I believe that whatever that is (and I don't really know what it may be), it can be found, and can be improved, and can be replaced, and other things can be added, and people will want to use the better product.

And if they don't... well, at least we fought an honest fight, and we did our best, and we (hopefully) had fun in the process, and pushed the envelope, and created nice things, and the users are better off in the end even if our babies are not the chosen ones, because we raised the level of everything.

For example, before Linux, Windows sucked much, much more than it does now, and I think many of those improvements were because of Linux, and I am happy that today Windows users have a OS that doesn't stink.

I want free and open source software to be used because it's awesome, not because it's cheap. Awesome and cheap I can live with. Just cheap? That sucks.

And the constant "they use windows because they don't know better"? That's patronizing and condescending, and very, very annoying. And if it annoys me, who is not the target of the lame condescension, trust me, it annoys the crap out of Windows users.

Grow a spine, get your asses into gear, start making awesome stuff, kick ass with quality. That's why we are here. Not to be the cheapest date in town.

2010-09-01 16:36

Goodreads+webcam+python+zbar == hackfun!

I am a big fan of GoodReads a social network for people who read books.

I read a lot, and I like that I can see what other people think before starting a book, and I can put my short reviews, and I can see what I have been reading, and lots more.

In fact, goodreads is going to be a big part of a project I am starting with some PyAr guys.

One thing I have been lazy about is adding my book list to goodreads, because it's a bit of a chore.

Well, chore no more!

Here's how to do it, the hacker way...

  1. Get zbar
  2. Get a cheap webcam
  3. Get a book
  4. Get a 7-line python program (included below)

Now watch the video...

Cute, isn't it?

Here's the code:

import os

p=os.popen('/usr/bin/zbarcam','r')
while True:
    code = p.readline()
    print 'Got barcode:', code
    isbn = code.split(':')[1]
    os.system('chromium http://www.goodreads.com/search/search?q=%s'%isbn)

2010-08-29 16:58

PET: English Translation Issue 1 has a date

Because it worked once, let's do it again. I have just set a completely arbitrary, and probably too early date for the release of the first english Issue of "PET: Python Entre Todos" magazine.

The english version is called PET, which means "Python Entre Todos: English Translation".

It will have the same contents as the first spanish Issue and... it will be the last Issue done like this.

From now on, both versions will be published at the same time, if we can.

So, there will be a very short gap between the english first Issue and the second one (less than a month, we hope).

So, stay tuned

2010-08-23 18:50

I'm a nerd, but I Have a Cheap Phone

I just got a new phone because my old one disappeared. I got a Samsung Star, which is not a smartphone, but what they call a featurephone, which seems to mean "it does a lot of things but is cheap, can only be coded in Java and doesn't run android".

Here's some of the features of this baby:

  • Touchscreen, 400x240
  • Fake GPS (gives you a 400 meters radius of your location. Good enough for me in the city)
  • Webkit-based browser that works surprisingly well (it comes with the LGPL as a document and you can't delete it :-)
  • Accelerometer
  • FM radio
  • A 2GB micro-SD card
  • 3.2MP camera (takes video at 320x240).

The camera can take decent photos in good conditions:

Foto0005.jpg

And it even has some "advanced" features (by which I mean: things my cheap dedicated camera doesn't do), like smile shutter and panoramic shots:

Foto0018.jpg

Other that that... well, it's a phone, what can I say. It was "cheap", which means "it costs more than I want to pay for it, but less than the alternatives".

So, how am I taking advantage of it...

First: I have not had a decent pocket-sized ebook reader since my last Clie died. So, I looked for software to do that.

It turns out that the world of Midlets, as feature phone apps are often called, is aweird place, where things are quite hard to find for the uninitiated.

There is a sort of "app shop" at http://getjar.com but it's by no means comprehensive, and often things are quite hard to find.

After lots of looking, I found a good (I may even say very good) program called Foliant. Here's their home page, russian only ... it's weird, my favourite Palm ebook reader, Palm Fiction also has a russian-only site!

Of course, the fonts sucked (way too large) but it turns out you can convert TTF fonts using this tool so I am now back to the lovely Droid Sans I am used to.

Another nice thing about the new phone is that it can actually play media (yes, my previous phone was so crappy it didn't even play mp3). But... not every kind of media. For audio, just use mp3. For video... it's a bit more complicated.

Here's the short version:

  1. Convert using HandBrake, ffmpeg MP4 video, AAC audio.
  2. Always use the .mp4 extension, the phone is not smart enough to know what a .m4v is.
  3. Don't make the video larger than 320x240.

Number 3 is a problem. For example, you may have a video in a wide screen format, like 640x272, which is a 2.35:1 ratio.

The obvious thing is to cap the width to 320, and that would give you a 320x136 video.

Well, that's wrong. What you should do is find the right height, keeping aspect ratio, for a 400px width. In this case, that would be a 400x170.

But you can't use a 400x170 video! which is why you will use 320x170, and on playback tell the phone to stretch it and ignore aspect ratio. And voila, 400x170 and the correct aspect ratio.

The difference? 320x130 has only 41600 pixels, while 320*170 has 54400, which means you get a 30% better picture.

Yes, it's tiny (3 inches) but it looks pretty sharp, and depending on the kind of material you are watching, it works.

UPDATE: Foliant is better than I thought, once you get the Samsung-specific version. It's fullscreen (no silly soft buttons) and the screen rotates automatically using the accelerometer. It's a pleasure to use, and the UI is very nice.

2010-08-19 23:02

Spain is much bigger than you think!

On the news today (the newspaper is Tiempo Argentino, sorry about the unreadable photo):

Foto0012.jpg

It says "In Spain it amounts to less than 340 thousand customers a month, or 0.006% of the total".

This is talking about cellphone users. Now, how many cell phone users are there in Spain?

According to this newspaper, 340000 are 0.006% of the total, so... 5 666 666 667 (aprox.), therefore, Spain has roughly one cell phone for each man, woman and child in the world.

2010-08-13 18:46

Things I learned publishing a magazine

Today at 00:00:00 GMT-3 PET: Python entre todos was indeed launched, in time (arbitrary but forced) and in budget ($0).

So, what did I learn? I learned a lot!

  • The only thing you need to publish an e-mag is time and content.
  • Time can be converted into content, but if you write everything yourself it's a blog, not a magazine. Luckily, PET found great contributors.
  • If you want utilitarian design, rst2pdf can do the job
  • In fact, it can do it better than other tools in some ways
    • I can push a fixed version of the PDFs in 5 minutes for all layouts. How much would it take me using Scribus or other DTP? In a magazine where correctness matters, that's a big deal.
    • TOCs are better than in most amateur PDF magazines I've seen. The in-content-TOC is clickable, and the PDF TOC is perfect.
    • Page numbers in the PDF TOC make sense (no, the cover is not page 1)
    • I am producing 6 PDF versions: A4(bw, colour), A5(bw,colour), Booklet(bw, colour) and I could add any other I want in a few minutes.
  • I learned about PDF imposition!

Let's explain the last one:

Suppose you want to print a small booklet, and you have 32 pages of content. How do you do that?

The easiest way is to print it 2-up double-sided in A4 paper so that you can stack the pages, fold them down the middle, staple them, and get a nice A5 booklet.

The problem is that the page ordering is hard to get right. For example, for a 4-page booklet, you need to print one A4 page with pages 4-1 on one side and 2-3 on the other. For an 8 page booklet it's 8-1,2-7,3-6,4-5.

Lucklily there's a way to get this done automatically:

1. Install podofo 3. Get booklet-A4.plan (see below) 2. Run this:

podofoimpose my-A5-pages.pdf my-booklet.pdf booklet-A4.plan lua

booklet-A4.plan is this:

---Generic Booklet (A4)
---
---It is said generic as it will try to determine
---automatically how to fit the booklet onto A4
---paper sheets, scaling pages if necessary.
---it is well suited for office documents for
---which you do not care too much about resulting
---imposition artefacts since it manages to save
---paper!
---
-- print("Booklet")
-- We output an A4 booklet
PageWidth = 595.27559
PageHeight = 841.88976

print("PageCount",PageCount)

-- We assume that H > W
-- Argh, we now can do better since we have "if" ;-)
-- Scale = PageHeight / (2*SourceWidth)
if(SourceWidth <= SourceHeight)
then
--  If you A5 pages are not really A5, uncomment the next line
--  Scale = PageHeight / (2*SourceWidth)
    Scale = 1
    rot = 90
        xof = SourceHeight
        yofRA = 0
        yofRB = SourceWidth
        yofVA = 0
        yofVB = SourceWidth
else
--  If you A5 pages are not really A5, uncomment the next line
--  Scale = PageHeight / (2*SourceHeight)
    Scale = 1
    rot = 0
        xof = 0;
        yofRA = 0
        yofRB = SourceHeight
        yofVA = SourceHeight
        yofVB = 0
end

do
    rest = PageCount % 4
    totp = PageCount
    if rest ~= 0
        then
        totp = totp + ( 4 - rest)
        end
    inc = 0
    count = 0
    imax = totp/4
    while count < imax
        do
--          We assume that podofoimpose will discard invalid records
--          such as those with source page greater than PageCount
--          print(totp, inc, rot, xof,yofRA, yofRA, yofVA, yofVB)
-- Recto
        PushRecord(totp - inc , inc + 1 , rot, xof , yofRA)
        PushRecord(inc + 1 , inc + 1 , rot, xof , yofRB)
-- Verso
        PushRecord(inc + 2 , inc + 2 , rot, xof , yofVA)
        PushRecord(totp-(inc + 1) , inc + 2 , rot, xof, yofVB)

        count = count + 1
        inc = inc + 2
        end
end

That code is taken from here: http://www.oep-h.com/impose/

And voilá, you get a scrambled PDF with the pages in exactly the right order (and empty pages added as needed).

2010-08-10 16:57

Come see me in Bahía Blanca next weekend!

I will be speaking at the Jornadas del Sur in Bahía Blanca this weekend (August 14/15 and 16).

For a change, I will not be giving my old tired talks, but a brand new one, called "The Amateur", and probably a lightning talk or something else.

The usual offer of free beer will not be possible this year, so: If you mention this blog in the QA session, you get... free candy!

After that, I will be speaking at FM La Tribu on saturday August 21st in the Charlas Abiertas 2010 where I will be speaking about a lot of things between virtualenv and nose and tox and other testing-related topics.

Contents © 2000-2019 Roberto Alsina