The Wayback Machine - https://web.archive.org/web/20151026182537/http://renesd.blogspot.com/search/label/python
Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Friday, July 18, 2014

Berlin guide for intellectuals, part 1.


Berlin: "Capital city of Germany.  Poor but sexy."  Intellectual: "A person who places a high value on or pursues things of interest to the intellect or the more complex forms and fields of knowledge, as aesthetic or philosophical matters, especially on an abstract and general level."

Part 1) A mini guide to Berlin for geeks, nerds, makers, and other thinking misfits.  I'm trying to list things here unique to Berlin, and Germany which may pique the interest of the intellectually curious.
I'm mainly writing this for my many friends who are visiting over the summer... but if you have any tips, or URLs to interesting things please email me ( rene@f0o.com ) or leave a note in the comments. Thanks! I have a *lot* more to add, but this is a good start I think. Hope someone finds it useful.  Sorry for the pretentious name ;)

Club Mate

“Do you have any more of that disgusting energy drink? It’s gonna be a long night.” — Julian Assange

Move over coffee!  Each serious intellectual movement has its drug of choice, and that is no different in Berlin.  Where the drink of choice amongst makers is Club Mate.

Image
CLUB-MATE
“It tastes like cold tea and cigarettes.  Like when someone puts their cigarette in your cup of tea the night before, and you drink it in the morning by mistake.” — Anonymous friend.

Berlin Philharmonic

The Berlin Philharmonic was voted the worlds number two orchestra in 2008, and ranked as top 3 in Europe by mumble mumble... The exact awards they won escape me, but they are FUCKING GOOD.  That is the main point, and you can go to see them.

The schedule, and tickets can be found at the The Berlin Philharmonic website.

Image
 Berlin Philharmonic Orchestra in 2006. Photo by Monika Rittershaus.
(Philharmonie on summer break until 30th August)

Deutsches Technikmuseum Berlin

“But didn't the USA invent computers?  And aeroplanes, and the wheel?” — Me.

Image
Section of the Z1 replica
“In fact, the information age can be said to have begun in the 1930s, within walking distance of the German Museum of Technology. In Berlin-Kreuzberg, the inventor Konrad Zuse built the world’s first computers. On the occasion of his hundredth birthday, the museum’s Department of Computing and Automation is presenting a newly conceived permanent exhibition: in the space of roughly 300 square meters (over 3000 square feet), the life and work of Konrad Zuse are presented in six thematic units and placed in the context of the worldwide history of technology.” -- Mathematics and Computer Science exhibition.

The Z3 computer (made in Berlin) was "the world's first working programmable, fully automatic digital computer".  The Deutsches Technikmuseum Berlin has a display devoted to its maker, Konrad Zuse.


Computer play museum

“We were walking down around here earlier, and damn!  It's like New York in the 80's.  So fresh.” — They Might Be Giants.
 
A museum dedicated to play and computer games?  Why not?


“Our new permanent exhibition “Computerspiele. Evolution eines Mediums” ("Computer Games. Evolution of a Medium") opened on the 21st of January, 2011. Over 300 exhibits, including rare originals, playable classics and art works from the game-art sector and interaction performances, conspire to turn you into “Homo Ludens Digitalis", so that you can create your own image of the game players of the 21st century. Experience a historical journey with highlights for every generation.”

Computerspielemuseum Karl-Marx-Allee 93a, 10243 Berlin
http://www.computerspielemuseum.de/1210_Home.htm


Chaos computer club.

A hacker club with its origins in the 80s named after Chaos theory.  You may want to disable your wireless and mobile phone before visiting any of their events ;)
Image
Forkbomb tattoo
“The Chaos Computer Club e. V. (CCC) is Europe's largest association of hackers. For more than thirty years we are providing information about technical and societal issues, such as surveillance, privacy, freedom of information, hacktivism, data security and many other interesting things around technology and hacking issues. As the most influential hacker collective in Europe we organize campaigns, events, lobbying and publications as well as anonymizing services and communication infrastructure. There are many hackerspaces in and around Germany which belong to or share a common bond to the CCC as stated in our hacker ethics.”

https://en.wikipedia.org/wiki/Chaos_Computer_Club

Tischtennis (Ping Pong)

Tisch means table in Deutsch(German), and Tennis means... tennis.  Add them together and you get Tischtennis.  Which means Ping Pong.  Got it?

Image
Tischtennis is everywhere
Rumour has it, in the GDR Stasi times, East Berlin people are said to have played ping pong to stop the spies from recording their conversations.  These days, there are still some bars which have Tischtennis inside, sometimes in the basement through a side door.  Outside, Berlin has a LOT of Tischtennis tables all around the city which you can use for free.

Check out http://www.pingpongmap.net/ (bookmark it on your phone), so you can see where the tables are around you.

One type of game which is fairly popular in groups is called Around the world.  Each person has a bat, and the people run around the table taking turns at a hit.  If you lose the point, you are out of the game and you stand aside.  As many as 30 people play at once, and it's great fun.

Whilst you are playing, you can pretend to be hiding secrets from spies too ;)


Co-working, and hacker spaces, meetups

You want to meet some people, hear their ideas or make something with others?  Berlin is a freelancers dream.  Plenty of places to hang out in and do some making.  When visiting a city, going to the various meetups can be a great way to meet people.  Interested in bee keeping?  Or want to know how to make a synthesiser out of toothpicks?  There's probably a meetup or workshop for you.  Check out meetup.com, lanyrd, or just search for "berlin X usergroup" on a Search Engine and you'll probably find a few options to your liking.

If you need to do some serious work, perhaps a coworking space will be better than sitting in a café.  There's plenty around, and they mostly allow drop ins just for the day.  Most of them charge around 12 euros per day.

Co.Up.Adalbertstr. 8, 10999 Berlinhttp://co-up.de/
AhoyWindscheidstrasse 18, 10627 Berlinhttp://www.ahoyberlin.com/en
ESDIPGrünbergerstr 48, 10245 Berlinhttp://esdipberlin.com/
Note:I'm just listing Coworking spaces that have day rates.

Drinking, Berlin style.

“How do you say Beer in German? Bier.  Oh.” — Not you.

Feierabend, means home time, or Beer O'clock.  You'll see many Berlin people with a beer open walking home from work.

The locals love beer in Berlin, and you can get a nice bottle from about a euro from a corner shop called a "Spätkauf" or "Späti".  You are allowed to drink whilst walking home from work, and you are allowed to drink in many parks.

Image
Spätkauf
Many bars have table service, and you pay at the end of your drinking session.  Although, not for all bars.  Whilst Berliners mostly drink beer you can get plenty of other booze.  You can get a Gin, Wodka, and many fine wines made either in Berlin or Germany.

There's not so much of a "rounds" culture.  If you offer to pay for someones drink they may look at you funny.  I regularly encounter people who are completely baffled by the concept.

There's not much wine made in Berlin, but there is in the south of Germany.  To get the low down on German wine, the wikipedia page has a good enough article. https://en.wikipedia.org/wiki/German_wine  If you want dry wine, look for "trocken".  I think the "Grauburgunder" wines are sort of good, and quite underrated if you want to try something different.  Whereas German wine is best known for its Riesling.  "Ice" wines are another interesting style.  If you're from outside of Europe, then you have a massive task ahead of you coming to grips with endless wine varieties :) Bubbles (sparkling wine) is called Sekt, and you can get a drinkable bottle from a few euros.


Words on paper, Book shops

“Mmmm, book smell.” — Readers.

Does hanging out in bookshops eating bagels and reading books sound appealing?  Once you have your book, your adventure begins.  Take your book to a park (or a lake) and drink in the day with book smell and sun.

http://www.yelp.com/search?find_desc=Book+Stores&find_loc=Berlin
http://www.slowtravelberlin.com/berlins-best-bookshops/

Music to take home, Record stores.

You know those black spinning things which have tiny little groves in them?  It turns out a lot of people still make them.

Berlin is not only a centre of classical music within Europe, but is also a hub of many other types of more modern music.  Berlin is the capital city of the underground if you are into electronic music.  It's where soundcloud is, and it seems every third person in the street is a DJ.  Germans also love their metal, and punk too.  So you can often pick up some great discs if you're into that sort of thing.


Unsurprisingly there are a *lot* of record shops you can hang out in.  You could spend hours, days, and even months flicking through crates and listening to lost sounds.  Many of the flee markets also have record sellers.

You can pick up local genres of music like Neue Deutsche Welle (https://en.wikipedia.org/wiki/Neue_Deutsche_Welle) or music from the GDR. I've personally started collecting German sea shanties (yes, the record store owners usually laugh at me when I make my purchases).

http://record-shops.org/berlin/
http://www.recordjunkie.com/stores/directory/germany/berlin
http://www.factmag.com/2013/11/27/record-shopping-in-berlin/
http://www.yelp.com/search?find_desc=Record+Stores&find_loc=Berlin
https://foursquare.com/matas/list/vinyl-in-berlin



Be nice to Berlin, and Berlin will be nice to you.

Be a considerate little tourist will ya?  You don't like it when someone spews up on your doorstep yeah?
Image
Berlin will ♥ U if U ♥ Berlin.
  • Do say hallo and smile at people.
  • Do put your recycling in the correct bins.
  • Don't take photos of people without asking. Privacy is important to Germans.
  • Do tip.  It's not expected but people love it!
  • Don't use AirBNB or similar.  It drives up the rents for people trying to live in the city.
  • Don't say the N word.
  • If you are going to put something on a wall, please make it good.

Where else to look? Blogs, Websites, Facebook.

What's up this week Berlin?  There's lots of blogs, websites, and facebook groups in English.  Many of the blogs below have lists of other blogs to check out too.

    10 things to do in a Berlin summer
 
Long form articles about Berlin in the "slow travel" tradition.

A visual magazine about Berlin.
    http://www.findingberlin.com/

Most big clubs are on resident adviser of course :)
    http://www.residentadvisor.net/events.aspx?ai=34

A daily email magazine with things to do.
    http://www.sugarhigh.de/

Lots of the open air/underground music/dance events:
    https://www.facebook.com/pages/Open-Airs-Berlin/355481677855564

Unlike is filled with hipstery type things.
    http://unlike.net/berlin/

LGBT clubbing, and such.
    http://www.discodamaged.net/

Food, design, and life in Berlin.
    http://reified.typepad.com/

Berlin design blog is about... well.
    http://www.berlindesignblog.de/

I heart Berlin is a blog about Berlin, also with a weekly events guide.
    http://www.iheartberlin.de/

A blog about ... [guess... go on! ...] Berlin!
    http://www.uberlin.co.uk/

Safety.

“Mind the gap!” There, I've warned you to mind the gap.  Try and remember that, since it won't be repeated at you every three minutes.

Keep off the bike paths!  You might get hit by a bike rider.  The bike paths are often separated from the roads, and next to pedestrian ones.  People new to Berlin are sometimes not used to bike paths, and hopefully just get a bell rung at them a few times rather than have a collision with a bike.

Riding your bike in Berlin is waaaaay safer than many cities in the world.  There's bike paths, and many car drivers are used to bikes.  Most people don't wear helmets, since you are not required to by law.

Going to a gallery opening held inside an abandoned office block might sound cool, until you fall through the floor and realise the walls are made of asbestos.

Berlin is pretty safe compared to many big cities.  But there are areas a bit more wild than others.  I know many women who go around Berlin by themselves and feel safe.  Neuköln in particular seems to be a place which gets more reports of trouble.  Having said that, Neuköln is a really fun place with many interesting things. Just be aware at night, and maybe don't go alone.  If you walk through a park at night, you'll probably get offered illegal drugs.

This is not a complete safety guide, but covers some basics.  You should probably look up a more comprehensive guide.

More to come...?  Corrections, feedback?

If you liked this, and want more please leave a comment :)  Or if you have a suggest, please comment or email me ( rene@f0o.com ).

Friday, June 06, 2014

Dear Apple, please make Swift more fun

Dear Apple,

it was with great pleasure that I see you enjoy 3 letter key words.  Your use of 'let' and 'var' were inspired!  These allow people to line things up nicely in a 4 space world.  With 3 letters for the keyword, and 1 for the space.  Brilliant!

However python has more elegant type setting than Swift in this case.  It uses "def" rather than "func".

It is not too late to make Swift more fun!

For your consideration:

func sayHello(personName: String) -> String {
    let greeting = "Hello, " + personName + "!"
    return greeting
} 

... now let's remove that c at the end of func...

fun sayHello(personName: String) -> String {
    let greeting = "Hello, " + personName + "!"
    return greeting
} 


See how the function name nicely lines up with the variables below!  That is type setting gorgeousity right there that is!

Pretty please, think hard about renaming func.  To either "fun" or to "def".


Thank you,
René.

Thursday, May 08, 2014

Statically checking python

With python, type checking is usually done at runtime.  Python will tell you of type problems when you run the code.  At runtime Python will tell you if there is a problem with the program 1 + 'a'. Since the types int and string can not be added together with the + operator.  This is why Python is often considered only a dynamically typed language, not a statically typed language.

However there are now tools to type check your python programs before you run them. Unfortunately it is not a widely known that you can type check python programs before you run them!

But what exactly are the benefits of static type checking anyway?  If you have 100% unit test coverage of your code, shouldn't that catch all the same bugs? Yes, it probably would. However, not all teams unit test all of their code.  Also, sometimes it might be dangerous to run the code to check for errors, or maybe the code takes a long time to run before it encounters errors.

You can check that:
  • interface methods are implemented
  • compatible operators are used on variables, eg "asdf" + 2
  • the correct number of arguments are passed into function calls
  • there are no unused variables, or modules
  • that variables aren't assigned to and never used
 Type inference also allows you to:
  • lookup the type(s) of a variable
  • find usages of functions
  • refactor, and type check the changes
There's hundreds more things you can check for with tools like pylint and some of the IDEs.  There are also field list markers in doc strings, where you can specify argument and return types. These can be used by tools like IDEs (eg pycharm) and other static checkers. Full program type inference is now doable for large parts of python. See the shedskin, and RPython restricted subsets for two implementations.

Now I will go over some problems you can check before runtime. I will show which tools work, and which tools do not work for these problems.

Want to download the code? You can follow along at this github link: https://github.com/illume/static_checking_python

The main tools I cover are:

Implements an interface

An interface is a way to specify what public interface implementations should present.

Can we check if an implementation of an interface in python has all of the required methods?  Below we have a Birds interface. We also have the Duck and Pidgeon implementations of the Birds interface.

implementsinterface1.py


import abc

class Birds(object):
    """docstring for Birds"""

    __metaclass__ = abc.ABCMeta

    def __init__(self, arg):
        self.arg = arg

    @abc.abstractmethod
    def noise(self):
        """docstring for noise"""
        pass

    @abc.abstractmethod
    def move(self):
        """docstring for move"""
        pass

class Duck(Birds):
    """docstring for Duck"""
    __implements__ = (Birds, )
    def __init__(self, arg):
        super(Duck, self).__init__(arg)

    def noises(self):
        """docstring for noise"""
        print self.arg

    def moves(self):
        """docstring for move"""
        print self.arg

class Pidgeon(Birds):
    """docstring for Pidgeon"""
    __implements__ = (Birds, )
    def __init__(self, arg):
        super(Pidgeon, self).__init__(arg)

    def noises(self):
        """docstring for noise"""
        print self.arg


    def moves(self):
        """docstring for move"""
        print self.arg 
 
 
 
 
(anenv)shit-robot:staticchecking rene$ pylint implementsinterface1.py 
No config file found, using default configuration
************* Module implementsinterface1
C:  1, 0: Missing module docstring (missing-docstring)
W: 23, 0: Method 'move' is abstract in class 'Birds' but is not overridden (abstract-method)
W: 23, 0: Method 'noise' is abstract in class 'Birds' but is not overridden (abstract-method)
W: 38, 0: Method 'move' is abstract in class 'Birds' but is not overridden (abstract-method)
W: 38, 0: Method 'noise' is abstract in class 'Birds' but is not overridden (abstract-method)

Here we see that pylint detects that we have not implemented the noise and move methods... because we made a typo.

implementsinterface2.py

In this file I correct the typo, and rerun pylint.

Here pylint can find that we are correctly implementing the required methods.

"""docstring for the module
"""
import abc

class Birds(object):
    """docstring for Birds"""

    __metaclass__ = abc.ABCMeta

    def __init__(self, arg):
        self.arg = arg

    @abc.abstractmethod
    def noise(self):
        """docstring for noise"""
        raise NotImplementedError

    @abc.abstractmethod
    def move(self):
        """docstring for move"""
        raise NotImplementedError

class Duck(Birds):
    """docstring for Duck"""
    __implements__ = (Birds, )
    def __init__(self, arg):
        super(Duck, self).__init__(arg)

    def noise(self):
        """docstring for noise"""
        # This will give a TypeError: cannot concatenate 'str' and 'int' objects
        # Pylint does not find this.
        print "a duck quacks this many times:" + 2

    def move(self):
        """docstring for move"""
        print self.arg

class Pidgeon(Birds):
    """docstring for Pidgeon"""
    __implements__ = (Birds, )
    def __init__(self, arg):
        super(Pidgeon, self).__init__(arg)

    def noise(self):
        """docstring for noise"""
        print self.arg


    def move(self):
        """docstring for move"""
        print self.arg
 

Detecting TypeErrors

Can you check for type errors? Let us test it out.

typeerror1.py


def doduck():
    # This will give a TypeError: cannot concatenate 'str' and 'int' objects
    # Pylint does not find this, However PyCharm does find it.
    return "The duck quacked this many times:" + 2

doduck()

First we try something simple... adding a string to a number.

This will give a TypeError: cannot concatenate 'str' and 'int' objects
Pylint does not find this error.

However, PyCharm shows the error. It says "Expected type 'str | unicode', got 'int' instead."
Note that although pylint did not find this error, it found these problems with the code:

(anenv)shit-robot:staticchecking rene$ pylint typeerror2.py 
No config file found, using default configuration
************* Module typeerror2
C:  7, 0: Final newline missing (missing-final-newline)
C:  1, 0: Missing module docstring (missing-docstring)
C:  1, 0: Invalid constant name "a" (invalid-name)
C:  3, 0: Missing function docstring (missing-docstring)
 

typeerror2.py

a = 2

def doduck():
    return "The duck quacked this many times:" + a

doduck()
 
We make it a bit more complicated for PyCharm, by putting the number into a variable.

Luckily, PyCharm still finds the error.

pylint still can not find this error.

typeerror3.py

import typeerrorsupport

def doduck():
    # This will give a TypeError: cannot concatenate 'str' and 'int' objects
    # Neither PyCharm, or pylint find this.
    return "The duck quacked this many times:" + typeerrorsupport.a

doduck()

Now we move the variable into a separate module.

This is where PyCharm does not find the error.

However pysonar2 can find the return type of the doduck() function is either a string or an int.
pysonar2 is not actually a tool for checking types, but only does the type inference in a library. It is meant for integrating into IDEs and such. It does advanced type inference in python.

Here it guesses that it could either return a string or an int type.

The command I used to generate some html output from all the files. I had installed and compiled
  java -jar pysonar2/target/pysonar-2.0-SNAPSHOT.jar . ./html

Follow the install instructions at the pysonar2 github page: https://github.com/yinwang0/pysonar2

typeerror4.py

import typeerrorsupport

def doduck():
    # This will give a TypeError: cannot concatenate 'str' and 'int' objects
    # Neither PyCharm, or pylint find this.
    return "The duck quacked this many times:{}".format(typeerrorsupport.a)

doduck()

This is correctly using the format method of string to put the int into the string.

Here pysonar2 correctly sees that an int is returned by the doduck() function.

typeerror5.py

import typeerrorsupport

def doduck():
    # This will give a TypeError: cannot concatenate 'str' and 'int' objects
    # Neither PyCharm, or pylint find this.
    return "The duck quacked this many times:{}" + typeerrorsupport.number_of_quacks()

doduck()

By adding the return type into the doc string of the typeerrorsupport.number_of_quacks() function we see that PyCharm can detect the TypeError.

If you follow the reST field lists, PyCharm (and other tools) can use that type information.

typeerrorsupport.py

a = 2

def number_of_quacks():
    """
    :rtype : int
    """
    return 2

The docstring tells it, that it would return an int type, and PyCharm detected this across module boundaries.

Note that pylint does not currently detect the TypeError even though we have told it the return type is int via the doctstring field list.

Conclusion

Much static type checking can be done in a dynamically typed language like python with modern tools.

Whilst none of the tools are perfect by themselves, we can see that by using a combination of the tools we can detect all of the problems we set out to detect.

You can use either the Abstract Base Class in the abc package which comes with python, or zope.interfaces to implement interfaces which can check that interfaces are implemented for you. I haven't gone into much detail here, just showed some basic interface checking with pylint.

PyCharm combined with appropriately written and typed doc strings can detect many problems. PyCharm also has many refactoring tools built in, and things like code completion for a dynamically typed language. Note, that this is not the only IDE or tool for python that can do this, but just the one I'm using for illustration purposes.

pysonar2 shows that types can be inferred without even specifying the types in docstrings. This has already been shown with tools like shedskin, and RPython(by pypy) which are statically inferring types from a subset of python. This style of type inference could be used for Ahead of Time (AOT) compilation, and within IDEs for better type inference. Better type inference allows better type checking, refactoring, and type inspection.

Monday, February 03, 2014

Welcome to your new NSA partner network. Tech art in Berlin.


Whilst walking around the Afterglow art hack day opening in Berlin, my phone buzzed as I had received a text message.  Opening up my phone I saw this:

Image
"Welcome to your new NSA partner network."

I immediately had a scary thought.  "Had someone hacked into my phone?"

My next thought was "oh, someone is hacking the mobile networks".  "Damn, I forgot to turn my phone off like I usually do before coming to hacker events".

My phone didn't have service to either of the networks it usually connects to, so someone was definitely jamming the phone systems.

This message was a very impressive piece of tech art indeed.  It made me think, and feel something.




We are starting an EU/UK tech online magazine. Want to know when it is released?
Email



Image
Rest your head against the speaker.
Part of the art hack day.

"The revolution is over. Welcome to the afterglow." reads the opening line to the transmedial festival in Berlin.

The transmedial festival brought back Cyber punk type thoughts from the last 15-20 years or so.  Now that the digital revolution is over, and "The future is now", and that 2020 is just around the corner... what next?

The afterglow.

"What is next" is a question lots of people in tech are always pontificating about, but perhaps not in the context of the time known historically as the information revolution.

Despite all that lofty talk, and a long speech on this topic, the event was a lot of fun.  Especially the art hack day parts.  Lots of people were there interacting with various art works people had made.


Image
Some sort of eletronic alchemy going on here
"Art Hack Day as a grassroots event/exhibit format/community for artists whose medium is tech and hackers whose medium is art."

Image

Over 80 artist hackers were invited to create the exhibit from scratch in 48 hours.  What they came up with was quite often fun and impressive.

Image
Stamps combine to make prints.


Knowing the design brief for these pieces can give you a better appreciation of what went into the thought process.  Here is the brief the creators were given:
As coders we fear the ‘legacy’ system, a piece of old junk we haven’t yet figured out how to throw away. As artists, we’re tempted by prolific outbursts of freshness and novelty; more art of less value. Businesses and government crave more data, more connections, more context. By embracing these impulses without contemplation we perpetuate the technological hype cycle and unintentionally shorten the half-life of our artefacts. Technology has become akin to a natural resource, generating physical and immaterial waste that is appropriated in such diverse contexts as e-garbage dumps, big data businesses and mass surveillance schemes. As such, trash is no longer what is just left behind but is central to our post-digital lives. When digital detritus piles up it decomposes, giving rise to a post-digital afterglow with the potential for new expression and new enterprise. Can we make peace with our excessive data flows and their inevitable obsolescence? Can we find nourishment in waste, overflow and excess? Can the afterglow of perpetual decay illuminate us?

This honey pot exhibit was one of my favourites.  If you look closely you can see a phone in there tempting passers by...

Image
Honey pot.  With actual honey. With phones in the honey.

Below are a selection of short video clips showing some of the other exhibits in motion.






The tower of beer was one of my favourites.  Not just because it had beer cans in it, but because of the great video effect it had when filmed.




The augmented reality of this tablet swinging from a rope was popular.  Point it at the picture of a rubbish bin painted on the wall, and the video will show rubbish flowing out of the wall.






Not only is the Afterglow happening for the digital revolution, but also for the revolution in Berlin.  After the Berlin wall fell, many artists entered the city from around the world.  It is currently undergoing another revolution of sorts, with many tech companies growing from within Berlin, and also moving to Berlin.  However, tech in Berlin has been around for a long time.  From groups like the Chaos Computer Club (europe's largest association of hackers), to the Z3 (world's first working programmable, fully automatic digital computer), Berlin has a very old historical involvement with technology.  The other revolution in tech that is going on is citizens fighting back against surveillance of the state and corporations.  So maybe we are not finished with the digital revolution, and perhaps it will cycle, one more time, towards a future.  But which future?



We are starting an EU/UK tech online magazine. Want to know when it is released?
Email

Thursday, April 11, 2013

Serial and pixels for breakfast. Arduino + pygame.

Taking a byte of bits of Serial, along with Green screen with Sam, pygame, and an Arduino hooked up to a light sensor and a motor thing.

Coffee too?  Naturally.

Where do we begin?  At the end of course.  A video of the result...



The arduino code

#include <Servo.h>  
Servo myservo;
int potpin = 0;
int val;

void setup() { 
  Serial.begin(9600);
  myservo.attach(9);
} 
 
void loop() { 
  val = analogRead(potpin);
  Serial.println(val);
  val = map(val, 62, 540, 0, 179);  
  myservo.write(val);
  delay(15);
}

python pygame code

Extended the "green eggs and ham" code I made recently to read serial data from the Arduino, and paint the screen different shades of green, whilst also making some silly sounds depending on the value read from the light sensor.



import os
import pygame
import serial

# guessing serial ports names. Linux or macosx.
#'/dev/tty.usbserial'
ser = serial.Serial('/dev/tty.usbmodem1411',
                    9600,
                    timeout = 0)
serial_buffer = ""

# an event number for the SERIAL
SERIAL = pygame.USEREVENT + 1


from pygame.locals import *
Event = pygame.Event
split = os.path.split


pygame.mixer.pre_init(44100, 8, 2, 1024)
pygame.init()
screen = pygame.display.set_mode((640, 480))
message = "Press q to quit, b for blue, r for red."
pygame.display.set_caption(message)

import pygame.examples
example_path = split(pygame.examples.__file__)[0]
example_data = os.path.join(example_path, "data")
sounds = [pygame.mixer.Sound("wiff.wav"), 
          pygame.mixer.Sound("punch.wav"), 
         ]

going = True
while going:
    serial_data = ser.read()
    #print (repr(serial_data))
    while serial_data:
        serial_buffer += serial_data
        # if there is a new line in it, then 
        if "\r\n" in serial_buffer:
            #print (repr(serial_buffer))

            evt = Event(SERIAL, 
                        {'line': serial_buffer})
            pygame.event.post(evt)
            serial_buffer = ""
        serial_data = ser.read()

    events = pygame.event.get()

    for event in events:
        print (event)
        if event.type == KEYDOWN and event.key == K_q:
            going = False
        if event.type == KEYDOWN and event.key == K_b:
            screen.fill(Color("blue"))
        if event.type == KEYDOWN and event.key == K_r:
            screen.fill(Color("red"))
        if event.type == SERIAL:
            # read the line from the serial event.
            print (repr(event.line))

            # clean up, and do sanity checking.
            # It could be corrupt or garbage.
            line = event.line.replace("\r\n", "")
            line = line.replace("\n", "")
            line = line.replace("\r", "")
            # it could be empty.
            if line:
                val = int(line)
                if val < 1024 and val > 0:
                    print (repr(val))
                    # map 0 and 1023 of analog read to
                    #  0-255 colour colour range.
                    green = int((val/float(1023))*255 )
                    screen.fill((0, green, 0))
                    if val < 100:
                        sounds[0].play()
                    if val > 300:
                        sounds[1].play()
                    
    pygame.display.flip()

ser.close()

Saturday, April 06, 2013

Reducing the number of concepts. To make teaching easier.

I am Sam.  Sam I am*.  Well at least my middle name is Sam.  So of course, one fun book I've always been drawn to is Green Eggs and Ham. The famous beginners book by "Dr. Seuss".
 
It contains just 50 different words: a, am, and, anywhere, are, be, boat, box, car, could, dark, do, eat, eggs, fox, goat, good, green, ham, here, house, I, if, in, let, like, may, me, mouse, not, on, or, rain, Sam, say, see, so, thank, that, the, them, there, they, train, tree, try, will, with, would, you.

A great beginners book, because it's a full story teaching a number of other concepts using a limited amount of words.  It's a clever bootstrapping hack to language learning.  Apart from all its cleverness, it's a fun & entertaining read too! :)

So you want to teach some newbies how to be l33t python pygame pr0grAmm3rs?  Maybe Sam I am, and Green Eggs and Ham have something to teach us, the teachers?

Let's reduce the number of concepts!

Classes, packages, and even functions are hard to get right away.  As in the first 10 minutes of teaching something to someone it's hard anyway.  Some people may get your car metaphors, and your nonsense about Ducks quacking like ducks... but others will look at you blankly.  Then that snotty nosed brat at the back of the class will start texting rude words about how you are a poo poo head, and it's GAME OVER.

However, there are two parts to these concepts.  Creating them, and just using them.

Here is an example pygame program using functions, packages, and classes... but not creating them.  This requires less knowledge to start doing something.

Image
Press r for red.

import pygame
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((640, 480))
message = "Press q to quit, b for blue, r for red."
pygame.display.set_caption(message)

going = True
while going:
    events = pygame.event.get()
    for event in events:
        print (event)
        if event.type == KEYDOWN and event.key == K_q:
            going = False
        if event.type == KEYDOWN and event.key == K_b:
            screen.fill(Color("blue"))
        if event.type == KEYDOWN and event.key == K_r:
            screen.fill(Color("red"))
      
    pygame.display.flip()


This displays quite a number of perhaps confusing concepts, but removes a few of the complex concepts.

Image
Watch ALL the events printed out!

Notice how there are no functions declared, no classes created, and no packages declared.  There's also no "Reactor", or callbacks required for the events.  It's just importing (which is boiler plate).

Ask yourself, "How would I see all of the events coming into a browser with JavaScript?".  This example prints out all of the events happening to the program.  Including Keyboard events, and Mouse events.  You don't need to know in advance what type of events will be appearing.  You can discover that there is a key up event, that there are mouse motion events.

By reducing the set of concepts needed to understand the code you will have an easier job teaching it.

Image
Press b for blue!


(* I don't like Green Eggs and Ham. I don't like them here or there. I don't like them anywhere.)

q for quit.

Friday, April 05, 2013

Raspberry pygame

The Arduino adventure took a little pause today...

I finally managed to buy a raspberry pi.

Things have improved greatly with the software since I messed around with an alpha board 1 year and 7 months ago.  Pygame is even on the desktop!  Pretty cool :)  I just wish they'd been in contact with the project so we could have made the experience better.  Tried to reach out a few times with no luck.
 
But anyway... managed to get one finally.  *happy dance*
 
Image
raspberry pygame

Improvements to the sound, OpenGL ES, camera, movie, python 3 version, are amongst 
some of the things I see that can be improved so far.

Tuesday, April 02, 2013

pygame logo colour swatches

Been messing around with some colour swatches for the neu pygame website...
Image
colors/colours for pygame logo

Friday, July 20, 2012

KEEP PYTHON WEIRD

Image

Friday, July 13, 2012

pretty print json

Pretty Print JSON

How to pretty print json from the command line?

$ echo '{"foo": "lorem", "bar": "ipsum"}' | python -mjson.tool
{
    "bar": "ipsum", 
    "foo": "lorem"
}

$ python -mjson.tool < /tmp/input.json 
{
    "bar": "ipsum", 
    "foo": "lorem"
}

$ curl http://rene.f0o.com/~rene/json_example.json | python -mjson.tool
{
    "bar": "ipsum", 
    "foo": "lorem"
}


Sunday, June 10, 2012

Pygame Google Summer of Code, 2012.

Thanks to the students, and mentors donating their time, along with the PSF, and Google, the following pygame related projects are ongoing over the summer.

"Pygame: GUI toolkit" by Sam Bull (sambull), mentored by Mike Fletcher(mcfletch) and with backup mentor Robert Deaton (masquerade). (Proposal | Blog)

"Pygame: Improved Sprite and Scene system" by Sagie Maoz (n0nick), co-mentored by Robert Deaton (masquerade) and Katie Cunningham (kcunning). (Proposal | Blog)

"Easy networking in PyGame" by Szymon Wróblewski (bluex), mentored by Rene Dudfield (illume) and with backup mentor Mike Fletcher (mcfletch). (Proposal | Blog)


Some project updates:

Sagie wrote a tutorial on pygame.sprite.Dirty and pygame.sprite.LayeredDirty, as well as a blog update on his progress.
    http://dotfile.n0nick.net/quick-dirty-using-pygames-dirtysprite-layered
    http://dotfile.n0nick.net/gsoc-journal-weeks-1-2

Szymon wrote a blog post on the networking project:
    http://pygame-networking.blogspot.com/2012/06/gsoc-journal-introduction.html

Sam wrote a post on the "SCG" beta release of the GUI project:
    http://blog.sambull.org/sgc-0-1-4-beta-release

Sam has also written to the mailing list asking for feedback on his work.
    https://groups.google.com/forum/?fromgroups#!topic/pygame-mirror-on-google-groups/AQiDPF_rINo

Sagie has also been getting into some conversations on the mailing list, and in the irc channel about the sprites project.
    https://groups.google.com/forum/?fromgroups#!topic/pygame-mirror-on-google-groups/KhaV8V6o4Js

Sam, Szymon, and Rene started on a little game for pyweek to be used as an experimental sandbox for testing out gsoc project ideas.
    https://bitbucket.org/pygame/pygamegsoc12


Wednesday, June 06, 2012

Internet protocol 6, 6, 6.

June 6th, 2012 is ipv6 launch day. 
http://www.worldipv6launch.org/


You can now go to http://www.pygame.org/ with version 6 of the internet protocol.

Sunday, June 03, 2012

SQL Injection via field names, and table names.

About a year ago I had to implement a system where the table name could be configured for an application.  Why would you want to do that?  Lots of reasons really, like if you have a table generator via a web interface.  In this case, the system integrates with other systems, and users need a way to specify where the data would go in an existing database.  So I merrily went ahead to try and put the table name in a prepared statement... but ERROR!  Unfortunately the sqlite database does not do table names or column names via prepared statements (like postgresql does for example).  The documented solution by sqlite is to escape them correctly with the provided functions.  Unfortunately python does not expose these SQL escaping functions - since most people should be using prepared statements.

Here are some WONTFIX, closed, invalid bugs also mentioning table names, and column names not being a problem.  One of them is six years old.

(closed wontfix) http://bugs.python.org/issue11685
(closed invalid) https://code.djangoproject.com/ticket/1474
(closed invalid) https://code.djangoproject.com/ticket/14991

Python and Django don't really consider this a vulnerability in themselves, but a problem in users code.

However, I still think it is something people should still be aware of.  Especially if you are taking user input to generate your table names, or your field names.  For those people, it could be a problem unless you are really careful.


SQL Injection with Django (a case study)

I'm going to use the python framework Django as a case study of what to be careful of.  However this advice applies to all python, php, [insert your favourite language or framework] and other SQL using libraries.

This code is used to quote table names, and field names in Django via the mysql db backend.

    def quote_name(self, name):         if name.startswith('"') and name.endswith('"'):             return name # Quoting once is enough.         return '"%s"' % name

Note how there is no database provided escaping of the input?  To protect against SQL injection it should be using prepared statements or an escaping function.

Because Django mostly does things like validate field names, and table names... this turns out to not be a problem in a lot of cases.  Other layers of security in Django protect it here.  This isn't an "OMG free Django p0wny!" bug, but a fairly niche bug if you do something wrong in your own code.

An example of how you can inject SQL because of quote_name is here, where the command takes a table name:
django-admin.py createcachetable 'myinjectedtable` (id INT) select id from users;create table `thecachetablename'

This SQL injection could be a problem if you are getting the cache table name from user input somewhere else, and not validating it first.

quote_name (as one of the places that does it wrong) is used in public 3rd party code, and internally within Django.  It is not a private method, since it does not begin with '_' or '__'.  There are places where a string can be supplied from outside of django (either through command line, or through other APIs used, or from a file).  It says that the db backends are responsible for quoting the field names (eg, the Q.__str__ method).

The Django book says:
"Thus, if you need to, say, dynamically construct a list of tables from a POST variable, you’ll need to escape that name in your code. Django provides a function, django.db.connection.ops.quote_name, which will escape the identifier according to the current database’s quoting scheme."

Another example where you can inject SQL...
MyObject.objects.extra(where=['"name"="SomeName";CREATE TABLE qwer (id int) select id from users'])

The Django documentation warns to be careful about this function.  However there is no documentation on the extra method about escaping field names, or table names.  Should probably add a warning there: https://docs.djangoproject.com/en/1.4/ref/models/querysets/#extra

You should be careful using input from untrusted places where you'll use it for table names, or field names.  Luckily this is not done very often by lots of people.

What can we do to fix table name SQL injections?

  • Use prepared statements if db backend allows,
  • ... or escape it correctly(as documented by each DB engine) when using prepared statements are not possible.
  • Disable multiple statements in the db connect, so even if something gets through the damage is limited.
  • validate the input where possible (for example, why would a table name need to include ;  or DROP DATABASE?)
  • document dangerous calls to explain how to escape values correctly in your database abstraction.
Some more expensive to do fixes are:
  • have automated SQL injection tests.
  • use the database provided permission systems. So front end code only has permissions to do what is required, and admin code can only do what is required of it.

Thursday, February 02, 2012

Chaos Python

Add this into your functional tests and smoke it.
import sys, random
def chaos_trace(frame, event, arg): 
    if event == 'line' and random.random() < 0.000001: 
        raise MemoryError()
    return chaos_trace
sys.settrace(chaos_trace)
You will get some lovely random failures injected into your code. A great way to find bugs, and make sure your reasoning is sound in the face of CHAOS!

Monday, December 19, 2011

CherryPy - I love it because it's pyhon.

CherryPy moved to bitbucket a while ago, and also got a website refresh.

I love cherrypy, since I can use it with python 3 - not legacy python.

I love cherrypy, because I can just use python objects.

I love cherrypy because it has a clever name.

I love cherrypy, because it just works.

I love cherrypy, it is minimal.

I love cherrypy.
Image

Wednesday, December 07, 2011

Thoughts on Python 3

It's awesome.

Go get some!

Sunday, November 20, 2011

PyGameZine issue0

Today we are very excited to announce the launch of PyGameZine!


Image


There is more information about it on the http://www.pygame.org/ website.

Friday, November 11, 2011

Vintage in Paris

Image

Our Vintage in Paris app has been published on the Nokia app store!

Image

The process was pretty simple, since it was a very technically basic but content heavy app.

The main difficulty in this app was walking around the streets of Paris taking pictures, and researching really good places to find Vintage.

Why a Vintage in Paris app?

Finding cool second hand and retro clothes is a hobby of mine, and my partners.   It's always fun finding interesting old styles, rummaging through clothes racks.  Especially in other countries, and especially when you find a bargain.

Image


There's something special about wandering down some back alley, and finding an old shop stuffed to the rafters with old stuff.  It's like taking a journey back in time to some fantastical version of Paris you may have in your head.  If you like Art Deco stuff, then Paris is the place to get it.

What was the signup to the Nokia store like?

I have been signed up as a Nokia developer for quite a while already, so there hasn't been too much paper work, or forms to fill out.  I can't remember what I needed to do originally, so there might be more bureaucracy than I remember.  I'm sure there will be more forms for me to fill out in the future too.

Technical parts.

The app is available as a J2ME Java app, a Meego app, and a WRT app.  Nokia has so many different platforms now!  They are adding a couple more platforms too.  Luckily HTMLish apps "work" on all their platforms.

Our Content Management System(CMS) is written in Python/JavaScript/C/SQLite and uses pygame for image processing.  The CMS part is still in development, but is slowly getting there.

We use a form based system to define schemas.  I wrote about this in a "Validation through html forms" blog post, and mainly in "Using a html form as the model".  Basically this allows us to create a html form, and then that's it for the content type.  No need to mess around with schemas, or python models, just create a html form, and that is it.  Since I usually follow a design led approach, the html forms are usually done before anything else.

The data is stored in a mini database system I built which I detailed in the blog post "sqlitepickle SQLite Vs pickle".  It lets you use a dict like interface to save python objects.  It's basically a very simple key value store built on python stdlib modules.

We're using JavaScript on the server side as I detailed in a blog post "server side jQuery" to generate the html.  The nice thing is I can use jQuery plugins on either the client side or server side depending on the need.  Including form validation, and other goodies.  I see it as a replacement for a templating language.

This app as it stands doesn't need much of a CMS, but future apps we have in development require a beast of a system in order to process the vast amount of data - and squeeze it down onto various different formats ready for distribution on multiple channels.

Image

Well it's out there now.  So hopefully it'll help some fellow vintage treasure hunters find some good stuff in Paris.

This is part of a series of blog posts on "my journey getting published on various app stores".

Monday, October 17, 2011

beta testing intel appup apps

So, I've been going around in circles the last few days with the validation our app on the intel appup store.

The support people escalated my request, and it looks like they have fixed a problem with the validation program.  It looks like their automated validation program had a bug.

In the mean time I've gone through the beta testing process.  This is where you can send your app to some beta testers and ask them to test for you.  If they already have the intel appup installed on their computer, your app appears in their "My Apps" section.  Then you can click on it, to try and download+install.

Just like the Mac OSX Application stores, if the download fails halfway through the system fails.  This is 2011 mind you, so you'd hope that people had figured out that internet downloads could possibly fail, and might need resuming.  Especially on mobile netbooks which are using unreliable 3g wireless internet connections.

As far as I can tell, there is no automated feedback collecting system in place.  Which means you will need to manage that feedback yourself.  Either by emailing your beta testers or using your own in game beta feedback forms.  If you're going to be releasing on multiple platforms, you'll want to create your own beta testing process anyway.  Since not all platforms make feedback easy, or possible at all.

It took me quite a while to actually download anything on their store.  Even small programs took a while.  I guess they are having connectivity issues with their download servers.  A 0.42MB app took over 10 minutes and multiple tries to download successfully.

The apps the html5 encapsulator by intel creates all include their own web browser.  Which means that every app is at least 10MB big, not even including your app data.  This is compared to other platforms, you mostly only need to include your actual data, or have a thin wrapper - like phonegap - which doesn't take up too much space.

Anyway... fingers crossed it passes their validation process soon.

Friday, October 14, 2011

'Halloween Juggling Pumpkin' published to the chrome web store... I think.

Well, I clicked the publish button, and it seems the 'Halloween Juggling Pumpkin' app has been published to the chrome web store.  I'm quite happy that it is up!

Here is a screen shot of the menu screen.
Image

Well, that wasn't too hard to do after all.


Here is a screenshot of some game play.
Image

Here is the lovely store URL it gave me for the store: https://chrome.google.com/webstore/detail/ndnpdjgfifincamlnbidkagoljbiagcb

This was way less work compared to the intel appup store, which I'm still having issues publishing it. To be fair, their store is still in beta for html5 apps.