Skip to main content

Ralsina.Me — Roberto Alsina's website

Posts about python (old posts, page 66)

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 ter­mi­nal, sup­ports re­siz­ing and moves as you it­er­ate from 0 to 79.

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

Is­n'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 pro­gram­mer, how could I make PyQt have some­thing as right as that?

Here'show the out­put looks like:

progress

You can do this with ev­ery toolk­it, and you prob­a­bly should!. It has one ex­tra fea­ture: you can in­ter­rupt the it­er­a­tion. 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!

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://re­vista.python.org.ar

Some ar­ti­cles:

  • PyAr, The His­­to­ry

  • from gc im­­port com­­mon­sense - Fin­ish Him!

  • Pain­­less Con­cur­ren­­cy: The mul­ti­pro­cess­ing Mod­­ule

  • In­­tro­­duc­­tion to Unit Test­ing with Python

  • Taint Mode in Python

  • Ap­­plied Dy­­namism

  • Dec­o­rat­ing code (Part 1)

  • We­b2Py for Ev­ery­­body

It's avail­able in pret­ty much ev­ery for­mat any­one can read, and if your favourite is not there, we will make it for you or may I be smote by the fly­ing spaghet­ti mon­ster's nood­ly ap­pendage!

AFAIK there is no oth­er Python mag­a­zine be­ing pub­lished (feel free to cor­rect me), so it's kind of a big thing for us in PyAr (the Ar­genti­na Python com­mu­ni­ty) that we are do­ing one, and in two lan­guages.

But why stop here? Want it to be avail­able in your lan­guage? Con­tact us at re­vistap­yar@net­man­ager­s.­com.ar it may be doable!

And of course, very soon there will be a call for ar­ti­cles for Is­sue 2, and trust me: that one's go­ing to be epic: this one was just a warmup.

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 think­ing: how hard could it be to make PyQt app de­ploy­ment ab­so­lute­ly easy? Well, I am guess­ing: not very hard.

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

Ques­tion: what has de­ployed bil­lions of apps and has its users hap­py? An­swer: phones app stores.

Ques­tion: how do they work? An­swer: well, that's not that short, so let's start ex­plain­ing.

As I see it, a rea­son­able app store has the fol­low­ing com­po­nents:

A Stable Deployment Target

You can't de­ploy from the store if you don't know what you are de­ploy­ing in­to. If the tar­get plat­form is shaky, you just can't know how to de­ploy with­out us­er as­sis­tance, and we are try­ing to make this easy for the user, which means that's not ac­cept­able.

So, what's a sta­ble de­ploy­ment tar­get we can provide?

  • PyQt (so we can de­­ploy GUIs to all ma­jor desk­­top plat­­for­m­s)

  • Python stan­­dard li­brary

  • Se­lec­t­ed mod­­ules

What can be (and should be) bun­dled with the ap­p?

  • Pure python mod­­ules

  • Art­­work and oth­­er re­­sources

What may be bun­dled:

  • Python mod­­ules writ­ten in C/C++, but you then have to re­­do the app for each plat­­for­m, and that kin­­da suck­­s.

Deployment Services

  • Apps should be able to check if there is a new ver­­sion of them in the store, to ask for up­­­grades.

  • Apps should be added by the de­­ploy­­ment plat­­form nice­­ly in­­­to the host sys­tem's menus, desk­­top, etc.

Monetization Services

  • Some way to charge for ap­p­s. Even for open source ap­p­s, you could ask for U$S0.99 if you in­­stall them through the store. Op­­tion­al, of course, and up to the app own­er.

  • Ad plat­­for­m? There must be a good one for desk­­top apps some­where?

The Store Itself

  • A we­b­site that down­loads a "pack­­age" as­­so­­ci­at­ed with a lo­­cal de­­ploy­­ment ap­­pli­­ca­­tion.

  • A app store ap­p. In­­stall things not via we­b, but via a desk­­top ap­­pli­­ca­­tion.

I don't ex­pect a func­tion­al ver­sion of this would take me more than a week work­ing full­time to im­ple­men­t. Of course then there are all sorts of us­abil­i­ty, look­s, etc. things to con­sid­er.

And... I am go­ing to do some­thing I very rarely do. I am go­ing to ask for mon­ey.

As an ex­per­i­men­t, I have set­up a project at http://www.in­diegogo.­com/Qt-Shop and set a fund­ing goal of U$S 600.

There you can fund me. I prom­ise that if the project is to­tal­ly fund­ed, I will de­liv­er. If it is­n't, I may de­liv­er any­way. I would pre­fer to have the mon­ey though.

The plat­form would be re­leased un­der GPLv2 or lat­er.

Why we are here.

Warn­ing: rant ahead.

Yes­ter­day the gov­ern­ment of Ar­genti­na an­nounced that they are giv­ing way 3 mil­lion net­books to stu­dents. They al­so an­nounced that they are giv­ing them the op­tion of Ubun­tu or Win­dows 7.

There was, of course, the typ­i­cal re­ac­tion from the FLOSS side: why are they giv­ing Win­dows to the stu­dents when Lin­ux is bet­ter? It's un­fair that the gov­ern­ment pays for Win­dows!

I am here to tell you to grow up and stop be­ing a ba­by. I am here to tell you to stop treat­ing oth­ers like ba­bies.

I think I can do this be­cause I am im­mune to crit­i­cism from the FLOSS crowd: I am a mem­ber of that crowd. I have an awe­some FLOSS pedi­gree, I have used noth­ing but Lin­ux for over 15 years. And I have a thick skin and I don't care much what oth­er peo­ple say, in prin­ci­ple, un­less they give me good rea­sons to care. And I am telling you to stop com­plain­ing.

I am telling you that if the on­ly rea­son to use a spe­cif­ic piece of soft­ware is be­cause it's cheap­er, you are ac­cept­ing that piece of soft­ware suck­s.

I want peo­ple not to just use Lin­ux, I want them to want to use Lin­ux. I want them to wait anx­ious­ly for the next re­lease of Ubun­tu or Fire­fox or what­ev­er.

And the first step to­wards ex­cel­lence is want­ing to be ex­cel­len­t. If hav­ing to pay noth­ing for Win­dows or Ubun­tu there is a cer­tain­ty that Win­dows will win, then Ubun­tu freak­ing sucks and needs to im­prove. Peo­ple are not adopt­ing it even if it's free? Then some­thing is wrong, and fig­ur­ing out what is im­por­tan­t.

But even more im­por­tant than find­ing the miss­ing piece is know­ing a piece is miss­ing. Open source has grown com­pla­cen­t. It's grown self right­eous. It's be­come ado­les­cen­t, sure of its awe­some­ness and im­mor­tal­i­ty.

I don't be­lieve in many things, but I do be­lieve in free will. I be­lieve that peo­ple are not mo­ron­s, I be­lieve that if they pre­fer Win­dows, it's be­cause it does some­thing bet­ter, and I be­lieve that what­ev­er that is (and I don't re­al­ly know what it may be), it can be found, and can be im­proved, and can be re­placed, and oth­er things can be added, and peo­ple will want to use the bet­ter prod­uc­t.

And if they don't... well, at least we fought an hon­est fight, and we did our best, and we (hope­ful­ly) had fun in the pro­cess, and pushed the en­velope, and cre­at­ed nice things, and the users are bet­ter off in the end even if our ba­bies are not the cho­sen ones, be­cause we raised the lev­el of ev­ery­thing.

For ex­am­ple, be­fore Lin­ux, Win­dows sucked much, much more than it does now, and I think many of those im­prove­ments were be­cause of Lin­ux, and I am hap­py that to­day Win­dows users have a OS that does­n't stink.

I want free and open source soft­ware to be used be­cause it's awe­some, not be­cause it's cheap. Awe­some and cheap I can live with. Just cheap? That suck­s.

And the con­stant "they use win­dows be­cause they don't know bet­ter"? That's pa­tron­iz­ing and con­de­scend­ing, and very, very an­noy­ing. And if it an­noys me, who is not the tar­get of the lame con­de­scen­sion, trust me, it an­noys the crap out of Win­dows user­s.

Grow a spine, get your ass­es in­to gear, start mak­ing awe­some stuff, kick ass with qual­i­ty. That's why we are here. Not to be the cheap­est date in town.

Goodreads+webcam+python+zbar == hackfun!

I am a big fan of GoodReads a so­cial net­work for peo­ple who read book­s.

I read a lot, and I like that I can see what oth­er peo­ple think be­fore start­ing a book, and I can put my short re­views, and I can see what I have been read­ing, and lots more.

In fac­t, goodreads is go­ing to be a big part of a project I am start­ing with some PyAr guys.

One thing I have been lazy about is adding my book list to goodread­s, be­cause it's a bit of a chore.

Well, chore no more!

Here's how to do it, the hack­er way...

  1. Get zbar

  2. Get a cheap we­b­­cam

  3. Get a book

  4. Get a 7-­­line python pro­­gram (in­­clud­ed be­low)

Now watch the video...

Cute, is­n'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)

Contents © 2000-2020 Roberto Alsina