Python

From Frack - Hackerspace Friesland
Jump to navigationJump to search
Onderwerp: Python
Python logo.png
Deskundigen
Gebruiker Elmer.jpg Elmer
Gebruiker Elmer.jpgElmer de Looff (Elmer) Rol: niet-deelnemer Deskundig met: Arduino, ENC28J60, Elektronica, GnuCash, HTML, LPD8806, Linux, MediaWiki, Mercurial, Netwerken, OpenVPN, Programmeren, Python, SSH, Software, Solderen Beginnend met: Dm-crypt, Javascript Werkt aan: Geen projecten :(
,
Afbeelding Anoniem.png Failbaitr
Failbaitr Rol: deelnemer Deskundig met: Arduino, CNC, CNC-Frezen, Canvas, Digitale Fabricatie, ENC28J60, Glasvezel lassen, HTML, Hydroponics, Javascript, LPD8806, Linux, Mercurial, Programmeren, Python, Robotica, Software, Stappenmotor Beginnend met: Elektronica Werkt aan: Geen projecten :(
,
Afbeelding Anoniem.png Fludizz
Fludizz Rol: deelnemer Deskundig met: GL-Inet, Glasvezel lassen, Iptables, Linux, Netwerken, Nftables, OpenVPN, Python, RaspberryPi, Software Beginnend met: ENC28J60 Werkt aan: Geen projecten :(
,
Gebruiker CodeHunger.jpg CodeHunger
Gebruiker CodeHunger.jpgCodeHunger (CodeHunger) Rol: niet-deelnemer Deskundig met: Arduino, HTML, Javascript, Mustache, Programmeren, Python, Sammy Werkt aan: Geen projecten :(
Beginners
Gebruiker Zeerover.jpg Zeerover
Gebruiker Zeerover.jpgNathalie (Zeerover) Rol: niet-deelnemer Deskundig met: HTML Beginnend met: Programmeren, Python Werkt aan: Geen projecten :(
,
Pan.png Panter
Pan.pngPanter Rol: deelnemer Deskundig met: Programmeren Beginnend met: Arduino, Python, RaspberryPi, Robotica, Solderen Werkt aan: Geen projecten :(
Onderdeel van Programmeren
Deelonderwerpen Mustache
Python wordt o.a. gebruikt in
10 watt led flood light + esp8266, Beamercontrol, Binaire klok, Conrad BEL-8006 radiothermostaat, DMX Homelight, Deurbel, FlatPackers, Frack Kassa, FrackBot, LPD8806-Lan, Ledwall, Lightbox, Localchat, Mac2user, Paintball sentry, Pixelvloed, PlainTLV, Quote database, RadioCurses, RobotArm, SpaceAnnounce, SpaceSlurper, Spacestate Switch, Tranquillity, Twitterboom, Vloerplan gcode, Voedselbank

Wekelijkse workshops in een toegankelijke en veelgebruikte programmeertaal om mensen te laten zien dat programmeren wel leuk, en helemaal niet zo moeilijk is.

Summary of Workshops

W17: Hackathon IRC bot / Christmas Tree demo

  • 2011-12-08 @ 20:00 CEST (this event has passed)
  • Goals for further development of the Bot:
    • Move nick changes, channel joins etc to a builtin plugin suite
    • Allw plugins to register a "command", and make the bot respond to certain prefixes ("!space" => prefix: "!", command: "space")
    • Do not allow two plugins to register the same command
    • Have plugins for separate classes of messages (query / channel / server) to allow reconnect/rejoin-like plugins (also PONG)
    • Identify owner(s) of the bot and only let those clients make control changes


W15 & W16: Hackathon IRC bot

  • 2011-11-17/24 @ 20:00 CEST (this event has passed)
  • We didn't complete any of the pending design goals, but we made a good chunk of progress:
    • Fixed the mainloop to only push out complete messages (ending in newline) for handling;
    • Moved all message parsing to a separate module. This now handles all messages and returns proper message objects
    • Moved plugin related code to a separate module, and plugins into their own directory with autoloading. Leading numbers on the module names indicate the order in which they will be imported (and asked to handle messages);
    • The process for connecting and identifying to the server has been streamlined and made significantly less hacky. Server, port and ident information need to be provided to instantiate a connection, instead of Connecting later on. This still needs to be wrapper into a higher layer of control, instantiating multiple server connections from a central client.


W14: Hackathon IRC bot

  • 2011-11-03 @ 20:00 CEST (this event has passed)
  • Fludizz suggested we do an evening of hacking together an IRC bot. We have some sources hanging around in the repository, and we have a vague idea of what it should do. Targets include:
    • Proper message parsing: nothing should trigger exceptions any more, and should result in a Message object of sorts
    • Have a means of sending messages back to a channel/query/server
    • Plugin system: we should be able to add plugins to the system, that do things with messages or as timed events
    • Triggers for events: messages are passed through something that checks whether there's a plugin that can do something with the received message
  • What we achieved:
    • IRC Connection in a thread that processes a continuous stream of messages
    • Separate methods to join a channel, change nick, set modes etc.
    • Practice-compliant (as opposed to necessarily RFC compliant) parsing of PRIVMSG class lines. This means we can track things happening in channels and queries.
    • A very rudimentary plugin system.
  • //frack/home/elmer/irc_client.py -- The current version of the IRC client with basic annotation of what happens.

W13: Hackathon PlainTLV

  • 2011-10-20 @ 20:00 CEST (this event has passed)
  • We introduced Jeroen and GoTr00t to Python (and Mercurial on Windows, oh the joy)
  • We covered Test-driven development using the unittest suite for the PlainTLV spec.
  • Live refactoring of existing reference implementation at the end of the evening, reducing a massive if/elif tree to a dictionary for dispatching (the closest to a Switch statement that Python has.
  • The reference implementation of PlainTLV and the test suite have moved in the repository:
    • //frack/libs/plaintlv.py -- The reference implementation ;
    • //frack/libs/test_plaintlv.py -- The conformance test suite.

W12: Advancing on GTK GUI development

  • 2011-09-15 @ 20:00 CEST (this event has passed)
  • We expanded the GUI introduction from last week, and combined the messaging web service from #W09 with the basic GUI we developed last time. We have an application that is able to retrieve messages from a JSON web service, and post back to it. Both author name and server to connect to are configurable through a popup menu, and various GTK tricks are used and explained.
  • Code in the repository:
    • //frack/W12/message_server.py -- The server application that serves the messages. It has a simple RESTful interface with two main options: Returning all messages, or those from a certain number onwards. The latter is used in the client exclusively;
    • //frack/W12/herald.py -- The client application, whose GUI is defined by //frack/W12/herald.glade. This application showcases a number of GTK GUI features, namely:
      • A Menu Bar that triggers a popup window with application settings (and bindings that prevent the window from being destroyed instead of hidden);
      • The use of Text Entry fields, both for reading and writing;
      • Adding items to existing containers (specifically a Label to a VBox);
      • Automatic scrolling of content using a Viewport inside a Scrolled Window.

W11: Introduction to GTK GUI and Glade

  • 2011-09-08 @ 20:00 CEST (this event has passed)
  • An introduction to GUI programming with GTK using Glade. Glade is a frontend that allows rapid development of GTK applications, and Python has binding that allow reading of its XML output to construct a GUI. We covered the basics of creating a simple application and developed two:
  • Code in the repository:
    • A demo application that prints a random quote when a button is pressed. This application is relatively simple, but explains a good number of problems that you may run into. It is heavily documented and can be found at //frack/W11/gui_demo.py;
    • A client/server application where the serving of quotes is moved to a simple web server (based upon BaseHTTPServer) and the GUI client requests information from this web server. (//frack/W11/quotes_client.py and //frack/W11/quotes_server.py)

W10: Base HTTP Server (repeat)

  • 2011-09-01 @ 20:00 CEST (this event has passed)
  • We repeated most of the Base HTTP Server presentation from last week, but developed more code on top of it
  • Code in the repository:
    • A more advanced BaseHTTPServer module that does delegation to separate response methods based on the requested path. This also handles serving local files in a safe way (not allowing the client to browse files in directories above the specified path) and serving up content from another web resource. (//frack/W09/basehttp_advanced.py);
    • A simple non-transparent proxy based on BaseHTTPServer. This allows for a single domain to be proxied through localhost (or wherever this server runs). HTTP POST actions are intercepted and logged to a text file. (//frack/W09/basehttp_proxy.py);

W09: Base HTTP Server

  • 2011-08-25 @ 20:00 CEST (this event has passed)
  • We covered the BaseHTTPServer module which allows us to handle arbitrary HTTP requests. In the CodeJam we created a simple web service where people could leave messages, much like the guestbooks of old.
  • Presentation: Python: BaseHTTPServer
  • Code in the repository:
    • Last week's sqlite module in the libs directory;
    • Example module for BaseHTTPServer usage (//frack/W09/basehttp.py);
    • SpaceMessgenger CodeJam example based on BaseHTTP and SQLite (//frack/W09/messages.py).

W08: Subclasing SQLite and more classes

  • 2011-08-18 @ 20:00 CEST (this event has passed)
  • This workshop was another without a strict plan or presentation. We covered subclassing and redefining methods. The end product was a subclass of the SQLite connection object, which returns a list of "ordered dictionaries", as opposed to the regular result object.
  • Both the SQLite subclass and the OrderedDictionary can be found in the repository (//frack/W08).

New format for related activities (WIP)

Naam Datum en tijd Omschrijving
W01 – Python introduction 23 juni 2011 om 20:00 First introduction to Python for everyone who wants to join
W02 – Iteration in Python 30 juni 2011 om 20:00 We explored most ways of Python iteration and did a FizzBuzz code exercise.
W03 – Files, contexts and CSV 7 juli 2011 om 20:00 We discussed how to read and write files, use contexts (for files that is) and reading Comma Separated Values (good 'ole spreadsheet format).
W04 – Recap of container types, functions and modules 14 juli 2011 om 20:00 We recapped basic collection types (with the addition of the set, covered function definition and docstring usage, and saving your Python code in your own modules.
W05 – SQLite databases 21 juli 2011 om 20:00 We covered creation of databases and tables, inserts, transactions, escaping, handling database errors and capturing of general user input.
W06 – Classes 28 juli 2011 om 20:00 We covered basic classes, method and attribute creation, as well as class hierarchy and basics on the garbage collector.
W07 – Unit testing 4 augustus 2011 om 20:00 We covered the basics of unit testing in a presentation-free format. After this several demonstrations of 'complicated and roundabout solutions for FizzBuzz' were given.

Format

Each workshop will start off with a presentation, focusing on one or two aspects of the Python programming language. These presentations will be created such that hopefully they will be useful for later reference.

After the presentation, there will be a code session where we put the discussed techniques into practice. We can either work based on an audience suggestions, or an exercise I have prepared. In the former case, the code exercise can be used as a homework assignment, for those that appreciate the experience.

Software repository

All the material (code and presentations) made/presented during the workshops will be available for download later. I've set up a DVCS in the form of Mercurial. Please download/install this (for debian/ubuntu: apt-get install mercurial) and provide me with your SSH public key (you can leave this key in the discussion page, or via irc/email/sneakernet. If you need my email address, talk to me).

Once you've set up Mercurial, you can get a copy of the repository by running hg clone ssh://hg@shell.underdark.nl:45022/frack (linux). This will create a directory 'frack' under your current working directory. For windows, a walkthrough is provided on the Mercurial website.

Useful Mercurial commands

  • hg in -- checks the remote repository for changesets that you lack. If the server has newer changes, it's recommended you apply these before committing your own work; this avoids manual merges;
  • hg pull --update -- This downloads the latest changes from the remote repository and updates your working directory;
  • hg add -- Adds a file to the repository (starts tracking/versioning it);
  • hg diff -- Shows the difference between the current tip revision and your working directory;
  • hg commit -m "$message" -- the most basic way of committing files. You can optionally specify the files/directories to commit by specifying them after the message;
  • hg push -- Push your changes out to the server. This will fail if you write in places that you lack permission to, or when you commit files with syntax errors.

Permissions

Space members (and others with a repository account) have write permissions in the //projects directory, and in the //home/$username directory. Attempting to commit changes outside these directories will cause your changeset to be rejected by the repository server. If you need broader access privileges, talk to Elmer.

N.B. The username for your home directory is usually your first name or nickname, if you want to alter this, talk to Elmer.

Errors when committing

abort: no username supplied (see "hg help config")

If you encounter this error, this is because Mercurial doesn't know of any username for you. Fix this by pasting the following content into ~/.hgrc (where ~ is your home directory if not auto-expanded)

[ui]
# Change The following to your proper email address
username = Anonymous coward <anon@example.com>
verbose = True

[extensions]
color = 

[color]
status.modified = magenta bold
status.added = green bold
status.removed = red bold
status.deleted = cyan bold
status.unknown = blue bold
status.ignored = black bold

This also enables colors for various Mercurial commands.

Install Python 2.7 on Windows 10

Install

1. Python installer (version 2.7.x)
https://www.python.org/downloads/
Check checkbox "Add python.exe to path"
2. Install libs
pip install numpy
pip install pyserial
pip install pygame
pip install matplotlib
pip install imutils
3. Install OpenCV 2.4.x
https://sourceforge.net/projects/opencvlibrary/files/opencv-win/
Click 2.4.x folder
Download opencv-2.4.x.exe (Self extracting exe)
copy opencv\build\python\2.7\x86\cv2.pyd to c:\Python27\Lib\site-packages

Versions used

Look at code online

Code hosting is down for now. This might get fixed once I've set up hgweb on the new repository host. As of now, the Frack mercurial repository can be viewed online. This allows viewing of changesets, diffs that have been made to files, and whole file views and downloads (through 'raw' on a file view). Also available is the Frack repository is LintMe, an application by Underdark that analyses code and highlights mistakes, style errors and other potential code smells.