In my original notes I said:
In this workshop we will use Anaconda, a (free) pre-packaged version of Python which can be easily installed on your laptop in a user's own directory, eliminating the need for computer root access for the installation.
but I've run into difficulties using Anaconda python with the prebuilt csound, so I suggest if you've installed Anaconda python, you uninstall it and work with the default python on your system for this workshop. (I build csound from sources so can direct where the libraries are located on the system).
The default csound installer works with the Mac. If anyone is using a windows machine and can report progress, please email me at the address on the LHS of the page.
Learning Python
For a completely cheezy adolescent Pydrama some thoughts of professional programmers–including Python's inventor, Guido van Rossum.
There are numerous sites offering free on-line courses and PDFs such as How to Think Like a Computer Scientist
The 1st edition is available in paper, pdf and html formats from http://greenteapress.com/thinkpython/
Code examples are at http://www.greenteapress.com/thinkpython/code/
The 2nd edition is available from: http://openbookproject.net/thinkcs/python/english2e/
Which has also developed into an online resource repository: http://www.openbookproject.net/pybiblio/
It also references another very popular introduction to python:
A Byte of Python. Location of the latest site: http://www.swaroopch.com/notes/python/
Dive Into Python Moves more quickly and deals with things in more depth. Not recommended as a first read, unless you are an experienced programmer. http://www.diveintopython.net/
The Stack Overflow site is good for on-line questions/solutions and discussions of common problems, a browser with the search string “python: how do I …” will usually return a discussion at http://stackoverflow.com/
python.org Do not forget these pages: https://docs.python.org/2/
They contain an excellent Tutorial by Guido van Rossum (Python’s inventor), Style Guide, Introduction to the Standard Library etc etc
Google Python Style Guide Python is the main scripting language used at Google. This style guide is a list of dos and don'ts for Python programs.
https://google-styleguide.googlecode.com/svn/trunk/pyguide.html
Learning Overview
Python Workshop (for IIS)
Sequence of Material to Cover
(reference: https://docs.python.org/2/tutorial/)
=== Invoking python in Interactive mode ===
- CLI
- IDE (eg. Spyder, emacs, vi, Textwrangler)
(- python Notebook - ipython)
2. Objects, Names, Modules and Namespaces
Objects: Everything in Python (numbers, strings, functions, classes…)
We often (mostly) get to the Object through a Name.
=== Names and Assignment ===
----------------------------
> a = 12
> b = 'B'
> c = True
> d = [1, 2, 3]
> e = a * b
> def func ():
print 'This is a function'
> f = func
> f()
This is a function
{ reuse of names )
=== Modules ===
---------------
A module is a file containing Python code
Each module has it's own global Namespace, so two modules can have the same names within them and remain distinct
e.g. Both an integer module and a floatingpoint module can have a function named mult()
which don't conflict when use use them by pre-fixing the module name:
integer.mult() and floatingpoint.mult()
=== Using modules: Import ===
-----------------------------
Models are included by Importing them, to any depth needed. i.e. an imported module can import other modules.
Three forms
1. > import my_module (maintains its own namespace, so using in the form my_module.myFunction()
2. > from my_module import myFunction (reads myFunction into the current (importing) namespace. loses original namespace context.
3. > from my_module import * as for 2. for all names in my_module. Generally not recommended, except perhaps as a way of breaking up a file into multiple files for ease of editing etc.
=== Special module names ===
------------------------------
__builtin__ : generic functions available without importing any other modules
__main__ : when running a python script, the python interpreter treats it as a module called __main__
This module gets its own namespace.
Used in the form:
def main():
...
if __name__ == '__main__':
main()
Other considerations when using import:
Source Code Encoding: encoding other than ASCII (e.g UTF-8) are possible.
This is usually done by putting a special comment line after the #! line. E.g.
#!/usr/bin/env
# _*_ coding: iso-8859-15 _*_
which would permit
> currency = u"€"
> print ord(currency)
Speaking of !# :::::
In computing, a shebang (also called a hashbang, hashpling, pound bang, or crunchbang) refers to the characters "#!" when they are the first two characters in an interpreter directive as the first line of a text file. In a Unix-like operating system, the program loader takes the presence of these two characters as an indication that the file is a script, and tries to execute that script using the interpreter specified by the rest of the first line in the file.
#!/usr/bin/env tells the shell to use the python specified in /usr/bin/env
=== Scoping: ===
----------------
The order in which names are searched for
Scoping is inside out, so
- names defined within a function is unique to it
- names defined within a module are unique to it
Except:
global names, e.g.:
global TRACE
TRACE = True
globals used within functions need to be declared as global. E.g.
myFunction():
global TRACE
if TRACE:
print "..."
If you find you need to use many GLOBALS, you might be better off using classes and objects (q.v).
=== Classes ===
---------------
A class' methods can only access the class' variable and functions by using a reference to itself.
Unlike some other OO languages, multiple classes can be defined in the same module (namespace) and can share global data.