Tuesday, November 19, 2013

Project Night: A red Raspberry Pi, 126 LEDs and a job

Project night


Every month, through PYPTUG, we set aside 2 nights for the Pythonistas and Pythonistas to be of the Piedmont Triad community. Tomorrow (wednesday the 20th of November) is one such night.

So what do we do there? The meetup page says:

"Do you have a project you want to show off? Or do you need a second set of eyes on your code? Would you like to hack on an open source project? Perhaps you need help getting a module on a specific platform? Don't have a project but would like to help others or to learn more Python?
Whatever the case may be, project nights are your opportunity to get some help or to help others, so bring your laptop (or Raspberry Pi or whatever device that can run Python) and let's get things going."

Red Raspberry Pi


So, I always show up at these project nights with a few potential projects. For example, hacking on Brython, or writing a good music collection manager in Python, or playing with a red Raspberry Pi. Red? Yes, this kind:

http://www.raspberrypi.org/archives/3195 

Pi Lite


But that wouldn't be a project in itself. I'll bring over a Pi Lite. What is that?

Ciseco Pi-Lite
126 ways to have fun.

Spectrum analyzer? network traffic display? game of life? cool scroller? Who knows what i'll end up doing, but it'll be fun, I'm sure.

A job?


Oh yes, almost forgot. So this project night is in Winston Salem:
Inmar 2650 Pilgrim Court, Winston Salem, nc

And it just happen to be that there are some openings for Python developpers, doing web services and focusing on Python. If that sounds like fun, get in touch.

Winston Salem is a great place to live and work (and that's me saying this, coming from cosmopolitan Montreal). Plus Inmar is migrating downtown pretty soon, in the Wake Forest Innovation Quarter. So come on, take the first step and possibly come and join me and other enthusiastic Pythonistas at Inmar and drop me a note, or even better, email me your resume.

Requirements:
Experience with Python (web services a plus) and with Postgresql (or even mysql).

François
@f_dion


Monday, October 28, 2013

toolz and curry

Lightning talk


Doing a quick lightning talk tonight for PYPTUG @WFU and it wont be about spanners and food as the title might imply, but about...

Functional programming


Yes, about functional programming in an imperative by design language (Python). And it's a lightning talk, so it'll be very superficial. But hopefully interesting nonetheless.

My first experience with pure functional programming was in the 80s with the Miranda programming language. If one counts impure functional such as LISP, scheme etc, then that would be Logo as my first.

Back to Python

One of the most appealing Pythonic technique I use on a regular basis, is functional. But more on that later. Here in this talk, I'll focus on a new module called Toolz (the result of a merge of functoolz and itertoolz - with a z, not the standard library functools and itertools).


François
@f_dion

Friday, October 11, 2013

IEEE 754/854, when it is needed

Linkedin

There was a post on linkedin IEEE Computer Society group, lamenting the division by zero exception raised by Python, instead of returning infinity (or -infinity). I replied, but it is not that readable. So I'm putting the properly formatted reply on my blog. [edit: no comments followed on linkedin, but many appeared on the original source after that]

IEEE decimal

If you want IEEE 754/854 decimal behaviour, what you want to use is the decimal module:

>>> import decimal
>>> help(decimal)

The above typed in the python interpreter will provide all the help you need. The examples are a little verbose, however.


ExtendedContext


If you want to be shorthand, you can copy/paste the following at the top of your program:

from decimal import setcontext,ExtendedContext, Decimal as ieee
setcontext(ExtendedContext)


Then whenever you want to use ieee behaviour use ieee(value):

>>> 1/ieee(0)
Infinity
>>> -1/ieee(0)
-Infinity

Python does it right



It is good that it has to be explicitly stated that you want this. A division by zero should raise an exception when dealing with financial data[*]. I would recommend numpy if dealing with scientific data.


François
@f_dion

* Unless you are calculating my bonus...

Tuesday, October 8, 2013

$1 in Python

Interesting question


"Why cant I use $1 in a Python script to get the first argument to a script? What is the easiest way to do something like that in Python?"

$1 in a shell script gives us the first argument to the script, BTW.

I get a lot of questions through email, and in person. This one was part of an interesting exchange. The reasoning went like this, since the shell is processing the script, I should be able to use $1.

The problem is that it doesn't work like that. Dennis Ritchie introduced the #! (pound bang or shebang line) concept at Bell Labs and announced it publicly in 1980. In the exece function of sys1.c could be found the following:

 else if (u.u_exdata.S[0]=='#' && u.u_exdata.S[1]=='!' && indir==0) {
  cp = &u.u_exdata.S[2];
  while (*cp==' ' && cp<&u.u_exdata.S[SHSIZ])
   cp++;
  u.u_dirp = cp;
  while (cp < &u.u_exdata.S[SHSIZ-1]  && *cp != '\n')
   cp++;
  *cp = '\0';
  indir++;
  iput(ip);
  ip = namei(schar, 0);
  if (ip==NULL)
   return;
  goto again;
 
 
So, once it was established that a script started with #! and a variable number of spaces, what followed was the path to an executable that would process this script. No search, so it has to be a full path.

In that regard, if the line starts with #!/usr/bin/python or #!/usr/bin/env python, the shell is out of the loop and no $1. Python will process the script. There is an sh module that makes it appear like we are mixing Python and shell in one script, but it is still all Python code.

Simplest shell script


#!/usr/bin/bash

echo $1

Simplest Python


#!/usr/bin/env python
from sys import argv as s


print(s[1])

Fairly similar except we had to specify an import statement, and I aliased argv to s so it looked like $ in the shell script.

The "problem" (or difference) with the Python script is that it will error out if no arguments are given when run:

$ ./arg.py
Traceback (most recent call last):
  File "./arg.py", line 5, in <module>
    print(s[1])
IndexError: list index out of range

A closer equivalent



#!/usr/bin/env python
from sys import argv as s

print(s[1] if len(s)>1 else '')

Here we are simply testing to see how many items are in the argv list. The first item is the script name, so if we have >1 items, we have at a minimum one argument, so it is safe to get [1]. If not, we will return an empty string, just like a shell script would do. A try / except IndexError would have also worked in this context.

See also

I would also investigate argparse, opster and docopt. argv is really bare metal and there is no point in reinventing the wheel.


François
@f_dion

Wednesday, September 25, 2013

The Raspberry Pi @ForsythTechCC IEEE club


Last week the ForsythTech IEEE club got a Raspberry Pi. Glad to see it being put to good use, Carroll Perkins just sent me the following:

"This Thursday, Sept 26th, at 3PM in Hauser 332, the ForsythTech IEEE Students meeting is a demo by Bobby Garriott of the Raspberry Pi computer hook up.  LCD screen, keyboard and mouse hook ups will be demonstrated.


The Raspberry Pi is a $35 computer board.  Ideal for embedded applications of all types including robotics, CNC and 3D printer applications. "


Friday, September 20, 2013

RPi MOOC en espanol

Donde

http://telescopio.galileo.edu/curso/raspberry-pi/

Cuando

El 14 de octubre 2013.

Información

Indican en la pagina que van a abarcar todo eso:

  • Instalación, Configuración, Accesorios y demás aspectos a considerar
  • Demos de instalación Wheezy y otras distribuciones
  • Introducción a Python
  • Introducción a la programación en Python para Raspberry Pi
  • Ejemplo completo Python
  • Modulo GPIO del Raspberry Pi para conexiones externas
  • Elementos básicos de hardware y como controlarlos con el GPIO
  • Próximos Pasos: Proyectos y Comunidad

Me parece muy genial. Es algo similar a los taller PyHack que hacemos en Carolina del Norte, pero en la red.

François
@f_dion

Monday, September 16, 2013

The lost art of digital entomology

Slides


I had forgotten to push the slides and code to bitbucket. This is from my PYPTUG talk from june. You can get the files here:

https://bitbucket.org/fdion/pyptug/src/a64e0c283150/debug?at=default


François
@f_dion