Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python for Introduction to Complex NetworksStudents

Similar presentations


Presentation on theme: "Python for Introduction to Complex NetworksStudents"— Presentation transcript:

1 Python for Introduction to Complex NetworksStudents
Guy Rapaport, ISE Tech Team

2 Warning: this is a 3-hours Hands-On Crash-Course
You will finish this lecture with code examples and tips. It is your responsibility to learn the rest on your own. Read the online resources, use Google a lot, and don't give up. While this guide is presented in the form of a presentation, note that it is designed to be used as a learning materials. Some slides have comments (like this one) denoting extracurricular information. I have attempted to cover all Python topics in not-so-many slides. This can get you started. You can acknowledge all the different things included in Python and in Python source code; Google the rest of the information on your own. It would be best if you would read these slides carefully, executing all of the code examples. However, if you haven't got the time or patience, at least skim through this presentation. Note which features Python contains. Some you'd might never use; I don't use variadic functions, for instance. But knowing they exist will help you identify them when you read other people's source code; and remembering they exist might enable you to look up Python code examples of these features and incorporate them in your own code.

3 Contents Overview Basics: variables, statements, expressions
Defining Functions Python as an Object Oriented Language Namespaces Collections and Iterations Conditionals File Handling Exception Handling Classes + Appendices

4 Python is... general-purpose - originally designed for Amoeba OS. Used in many contexts. interpreted - it's a scripting language. high-level - Object Oriented, Garbage Collected... readability - and means it (unlike other languages) comprehensive STL - networking, XML- RPC... functional programming - first-order functions dynamic typing - i.e. duck typing. named after Monty Python. Python is a general-purpose, interpreted high-level programming language[12] whose design philosophy emphasizes code readability. Its syntax is said to be clear[13] and expressive.[14] Python has a large and comprehensive standard library.[15] Python supports multiple programming paradigms, primarily but not limited to object-oriented, imperative and, to a lesser extent,functional programming styles. It features a fully dynamic type system and automatic memory management, similar to that of Scheme,Ruby, Perl, and Tcl. Like other dynamic languages, Python is often used as a scripting language, but is also used in a wide range of non-scripting contexts. Using third-party tools, Python code can be packaged into standalone executable programs. Python interpreters are available for many operating systems. ( anguage)) Python is a programming language that lets you work more quickly and integrate your systems more effectively. You can learn to use Python and see almost immediate gains in productivity and lower maintenance costs. (

5 Online Resources Official Documentation http://docs.python.org/
Google Python Class python-class/ Code examples for this fine presentation: CrashCourse_CodeExamples.zip More text and code in my course's website.

6 Executing from CLI Normal installation requires you to enter the full path to python.exe Add c:\python27 to your PATH for convenience.

7 Executing from IDLE Write Code, Save (CTRL+S), Run (F5)
Note that IDLE's default indentation for code is 4 whitespaces and not TAB - more on that later!

8 Hello, Python! print "Hello, Python!" #OR print 'Hello, Python!' #SO
print '"Hello, Python!", I screamed in joy!' #AND print '''He entered the room and said, "Hello. Python!". Nobody uttered a word.''' #multi-line strings In Python 3K (codename for all Python 3.xx versions), print is no longer a statement but a function, i.e. print("Hello, Python!")

9 Hello, Python! print "Hello, Python!" No "main" function.
A Python module is a .py file. There are statements, and there are expressions. print is a statement - it does not return values and the expression syntax does not apply to it.

10 Hello, Python! Built in types: "Hello, Python!" - Strings
True, False - Booleans 1 , 2 , 3.0 , complex(4,5) None =~ null These are all Objects - unlike Java, like C#. These are all constants - meaning you can check whether "(6 < 7) is True" etc.

11 Hello, Python! #More basic, useful examples s = "Hello, Python!"
print len(s) print s*2 print 'I said: "' + s + '"' print 5/2 print 5/2.0 #also 5.0/2 . This is different in Py3K. #String Formatting: print "%s, %s %f!"%("Hello","Python",2.7)

12 Defining Functions def foo(arg1,arg2,arg3): return arg1 + arg2 + arg3
def keyword - for functions/methods. No return type. return keyword - for returning. If not included, None is returned by default.

13 Variadic or Keyword Arguments
>>> def foo(*args): print args ... >>> foo("Shalom","Python") ("Shalom","Python")#args is tuple >>> def bar(**kwargs): print kwargs >>> bar(shalom="Python",monty=1) {'shalom': 'Python', 'monty': 1}#args is dictionary

14 First Order Functions def createAdder(x): def adder(y): return x+y
return adder map , all , any (func,collection) map(lambda x:x**2,[1,2,3,4,5]) List Comprehension: x = [i**2 for i in xrange(8) if (i % 3 != 0)] x = (i**2 for i in xrange(8) if (i % 3 != 0))

15 Shalom, Python! def foo(): print "Hello, Python!"
print "Shalom, Python!" foo() #prints the latter one; that's the consequence of interpretation.

16 Shalom, Python! def foo(): print "Hello, Python!" Mind the gap:
Python does not care about semicolons ; or curly-brackets {} only about indentation of code blocks. Indentation must be consistent within a file (=module) - either 2, 4, 8 whitespaces, or a tab. This is usually the most frustrating and hard-to-get-used-to feature for new Python programmers. Code blocks are used for looping constructs, conditionals, definitions of classes and functions/methods. Picture of Johnny (Tommy Wiseau) is from the film "The Room".

17 Everything's an Object =~ Dictionary
def managedFoo(): managedFoo.invocations += 1 print "managedFoo has been invoked",managedFoo.invocations,"times so far." managedFoo.invocations = 0 managedFoo()

18 Everything's an Object => Syntax is translated to Method Invocations
managedFoo.invocations = 0 is really managedFoo.__setattr__("invocations",0) __setattr__(managedFoo,"invocations",0) And it's not *always* possible: >>> "inovcations".managedFoo = 0 Traceback (most recent call last): File "<pyshell#14>", line 1, in <module> "inovcations".managedFoo = 0 AttributeError: 'str' object has no attribute 'managedFoo'

19 Namespaces Namespaces are dictionaries/hashes/mappings.
Python semantics are all about accessing namespaces, enhancing namespaces, importing from one namespace to the other. The default namespace is not empty - the following slide contains its contents. Use dir() and help() to study the environment.

20 >>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BufferError', 'BytesWarning', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'None', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError', '_', '__debug__', '__doc__', '__import__', '__name__', '__package__', 'abs', 'all', 'any', 'apply', 'basestring', 'bin', 'bool', 'buffer', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'cmp', 'coerce', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'execfile', 'exit', 'file', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'intern', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'long', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'raw_input', 'reduce', 'reload', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'unichr', 'unicode', 'vars', 'xrange', 'zip'] Using the dir() builtin function. Notice how dir itself is mentioned (marked in bold) in this list. help() and range() are also mentioned; in the next slide we'll use help to learn more about range.

21 Using the help() builtin function:
>>> help(range) Help on built-in function range in module __builtin__: range(...) range([start,] stop[, step]) -> list of integers Return a list containing an arithmetic progression of integers. range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0. When step is given, it specifies the increment (or decrement). For example, range(4) returns [0, 1, 2, 3]. The end point is omitted! These are exactly the valid indices for a list of 4 elements. >>> range(5) [0, 1, 2, 3, 4] help displays an object's docstring - which appears, in Python, right after the definition (unlike Java's Javadoc). For instance, the range function probably looks a bit like this: def range(start=0,stop,step=1): """range([start,] stop[, step]) -> list of integers ...

22 Namespaces - Unexpected Behavior
>>> def foo(): ... print x ... >>> def goo(): ... x += 1 >>> foo() 3 >>> goo() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 2, in goo UnboundLocalError: local variable 'x' referenced before assignment This might look surprising, unless you understand Python scoping rules. from inside functions, the global x is read-only. When goo() tries to increment it, the interpreter thinks it must be local and fails to find its definition! stions/146359/python- scope The "global" keyword (and later in Py3K the "nonlocal" keyword) can allow updating global variables.

23 The Key to Understanding Python
Understand everything is an object. Understand all the code you read, with all the oddly-looking syntax, is just methods invocations. Understand namespaces, and use dir() and help() to navigate and study your environment! And use proper indentation!!! There are no crazy environmental issues a-la Java, C#, Perl... This is very straightforward and unambiguous! You just have to get use to it.

24 Collections and Iteration
All collections are heterogenous (no typing!) list tuple (frozen list) set dictionary Only hashable objects can be put into sets and dictionaries. There are frozen sets and dictionaries as well.

25 Collections and Iteration
All collections and some Python types are iterable. This is achieved by conforming to the Python iterator protocol. for i in iterable: #The basic Python loop print i print "something" in iterable print "something" not in iterable Understanding Python's "for" statement

26 Collections and Iteration
Many other objects conform to the iteration protocol: >>> for foo in "strings": print foo, #comma cancels newline after print s t r i n g s >>> for goo in open("filename.txt"): print goo line by

27 Collections and Iteration
Lists and Strings can be accessed in a variety of ways. Subscriptions pick a certain item at a certain index; Slices follow a [start:stop:step] pattern. >>> "the quick"[1] 'h' >>> "brown fox"[2:5] 'own' >>> "jumped over"[-1] 'r' >>> "the lazy dog"[:-2] 'the lazy d' >>> "i've seen it"[::2] 'iv eni'

28 Collections: Hashable Types
Some types are not hashable - i.e. lists. Python's default hash value is id() - memory address. If we define our own classes with special semantics, in order to make hashing work as we would like we need to implement two methods: __hash__ __eq__ Non-hashables cannot be dict. keys or inside sets. lst = [1,2,3] s = set() s.add(1) #works s.add(lst) => TypeError: unhashable type: 'list'

29 Collections: Hashable Types
Hash Collision Solved By Chaining: Initial Hashing - __hash__ Chaining - __eq__ Source:

30 ADTs API: List lst = list() lst.append(1) lst.extend([2,3,4,5,1])
lst.reverse() #in place! lst.sort() #in place! lst.count(1) lst.index(2) lst.pop() lst.remove(3) lst = [] lst += [1] lst += [2,3,4,5,1] lst = lst[::-1] lst = sorted(lst) 6 in lst # True iff 6 is in list lst - takes O(n) time.

31 ADTs API: Set s1 = set() s1.add(3) s2 = set([2,3,4]) s2 - s1
s1 & s2 # s1 or s2 s1 | s2 # s1 and s2 s1 ^ s2 s1 >= s2 s2 > s1 2 in s2 # In o(1) time. The set API includes operator overloading for most mathematical-set operations, resulting in very readable code.

32 ADTs API: Dictionary d = {} d["dog"] = "cat" print d["dog"]
dict.fromkeys([1,2,3]) dict.fromkeys([1,2],3) d.keys(); d.items(); for k in d.iterkeys(): for k,v in d.iteritems(): for v in d.itervalues(): d = dict() 'key' in d d.has_key('key') d.get("dog","default when key is missing")

33 Conditionals - if , elif , else
No switch-case statement. if-elif-else if cond =="this": doThis() elif cond == "that": doThat() elif conf ==... else: doNothing()

34 Conditionals - comparisons
is , is not - reserved keywords for object identity! >>> "dog" is "dog" True #misleading: might change >>> ["dog"] is ["dog"] False >>> ["dog"] == ["dog"] True #equality is not identity! >>> id("dog") #default identity: memory address It is a common misconception that strings and numbers, being immutable, are stored as constants by the compiler. This is not true. The following code demonstrates this. The explanation for this phenomena is beyond the scope of this tutorial. >>> a = 1 >>> b = 1 >>> b is a True >>> a = 6000 >>> b = 6000 False

35 Conditionals - boolean operators
Everything you need to know is here: (x.__and__(y)) (x.__le__(y))

36 File Objects and File Handling
Default way is quick! (Unlike Java!!!) f = open("input.txt")#'r' is the default flag - read! contents = f.read() f.close() #other flags: a , w , rb, wb f = open("output.txt",'wb') f.write(contents)

37 Exception Handling try: 5.0 / 0 ; raise Exception("Oh no!!!")
except ZeroDivisionError: print "Go back to ~intro!" except: print "For any other reason" else: print "Everything went OK" finally: print "Always printed."

38 Exception Handling try =~ try except =~ catch. Can come in 3 flavours:
except ValueError: except ValueError(e):#handling e else,finally =~ else,finally in other languages raise =~ throw: raise Exception("reason") Note that Python functions do not declare which exceptions they might throw.

39 Classes: Definition and Inheritance
class A(): def __init__(self,value = 5): self.value = value def displayValue(self,more = None): print self.value if more: print more a = A(7) a.displayValue() #self=~this: explicitly mentioned, implicitly passed. No method overloading.

40 Classes: Definition and Inheritance
Instantiation syntax = classname() : a = A() Single constructor = __init__ toString() = __str__ and __repr__: __str__ - when printing __repr__ - when printed within collection/console Visibility for methods is not encouraged (public, private, protected, etc.)

41 Classes: Definition and Inheritance
//Java public class Foo(){ int a = 3; String b = "Goo"; } #Python class Foo(): def __init__(self): self.a = 3 self.b = "Goo" classVar = "spam" #instance members are explicitly set!

42 Classes: Definition and Inheritance
Python supports Inheritance: class MyException(Exception): ... class YourException(MyException,TheirException): You can read more about Inheritance here: BUT...

43 Classes: Definition and Inheritance
Due to duck-typing... class Foo(): def do(self): print "Hey!" class Goo(): def do(self): print "Hai!" ref = Foo() ref.do() #prints "Hey!" ref = Goo() ref.do() #prints "Hai!"

44 Classes: Operator Overloading
Operator overloading is very easy in Python. Allows making better use of the syntax, resulting in more readable code. Based on keyword methods names such as: __add__ , __mul__ , __pow__ , __iter__ ... Full list: names

45 Classes vs. Collections: Implementing a Hashable Type
>>> class Payload: def __init__(self,payload): self.payload = payload def __repr__(self): return "Payload(%d)"%(self.payload) >>> print set([Payload(1),Payload(1)]) set([Payload(1), Payload(1)]) >>> class Payload: def __init__(self,payload): self.payload = payload def __repr__(self): return "Payload(%d)"%(self.payload) def __eq__(self,other): return self.payload == other.payload def __hash__(self): return hash(self.payload) >>> print set([Payload(1),Payload(1)]) set([Payload(1)])

46 Questions? Online Documentation dir and help builtins
WRITING AND EXECUTING CODE! Code examples for this crash-course are available online.

47 What do you think is the most important skill every programmer should posses?
Guido Van Rossum: Your questions are rather general and hard to answer. :-) I guess being able to cook an egg for breakfast is invaluable.

48 Appendix A: Configuring PyDev in an Eclipse Workspace
A Guide in Pictures

49

50

51

52

53

54

55 Appendix B: Advanced Topics
Not covered here, but worth reading!

56 The Many Flavours of Python
Python is compiled interactive (scripting) language. It has a very enhanced compiler that produces bytecode which is then interpreted in a VM (like Java). Due to the interactive nature of Python many do see it as a purely interpreted language. Python's official version is CPython - where low-level libraries are implemented in C. (Python can generally integrate with C code - like Java's JNI.) Other flavours include: Jython - Python running on the JVM, can use Java classes; IronPython - Python running on the .NET CLR, can use C# classes; Stackless Python - experimental flavour of Python. Python 3 (aka "Python 3K") - improved speed, still not commonly used. Python code can be made faster using JIT compilers (such as Pypy). Python has an extensive web framework called Django ("the D is silent").

57 StackOverflow's Series about Advanced Topics in Python
Iterators (and the iterator protocol): Decorators: decorators# Metaprogramming (you'd probably never use it, but in case you do): python/ #

58 Notable libraries in Python STL
re - Regular Expressions csv - Parsing CSV/TSV files quickly datetime - with a sane API... pickle, shelve - object {de}serialization timeit - code profiling dis - {dis}assembler for the Python bytecod import dis; dis.dis(dis) More online:

59 Notable Libraries: numpy and scipy
Scientific and Numeric libraries for Python. The de-facto standard for Python mathematical libraries, gradually replacing Matlab et al. Below is a good example of using operator overloading to craft classes with beautiful API, resulting in a very readable code! >>> from numpy import *>>> a = array([ [ 0, 1, 2, 3, 4],[10,11,12,13,14],[20,21,22,23,24],[30,31,32,33,34] ])>>>>>> a[0,0] # indices start by zero0>>> a[-1] # last row array([30, 31, 32, 33, 34])>>> a[1:3,1:4] # subarray array([[11, 12, 13], [21, 22, 23]]) *

60 Notable Libraries: networkx
Graph/Networking library for Python. We will use this library during the course.

61 Notable Libraries: BeautifulSoup
The best library for parsing HTML and scraping web pages. "You didn't write that awful page. You're just trying to get some data out of it. Beautiful Soup is here to help. Since 2004, it's been saving programmers hours or days of work on quick-turnaround screen scraping projects." See also: Scraping Web Databases using Python

62 Notable Libraries: Twisted
"Twisted is an event-driven network programming framework written in Python and licensed under the MIT License." (Wikipedia) Used in Mellanox's UFM. Twisted Homepage:

63 Appendix C: The Zen of Python
import this import antigravity

64 The Zen of Python by Tim Peters
Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one -- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those! Type "import this" into the Python shell.

65 Type "import antigravity" into the Python shell.

66 Improve your style! http://docs.python.org/faq/programming.html
styleguide.googlecode.com/svn/trunk/pyguide .html

67 Python Keywords This list was taken from here:
Test yourself - what does each of these keywords mean? and as assert break class continue def del elif else except exec finally for from global if import in is lambda not or pass print raise return try while with yield Solution: and - and operator (val > 3 and val <= 9) as - aliasing. used when importing (import networkx as nx) or in the with statement (with open("document.docx") as f) assert - a simple mechanism for writing assertions which apply only when code is executed in debug mode. See: break - used to completely break out of current innermost loop only. class - used to define classes. continue - used to skip current iteration of current innermost loop. def - used to define functions (or methods if included in a class code block). del - used to explicitly delete an object. elif - used in conditional statements (if ... elif ... else) else - used in conditional statements (if ... elif ... else) or in exception handling to mark the result of no exceptions (try...except...else...finally) except - used to catch raised exceptions. Can include a specific exception to catch; otherwise all exceptions are caught. exec - given a string, this statement executes this code (exec "print 'dog'"). finally - used in exception handling to mark the final actions, regardless of whether an exception occured or not. for - iteration construct. Also used in list comprehension ([x**2 for x in range(10)]). from - used to import elements from another namespace. (from itertools import cycle) global - used to declare a variable to be a reference to the global variable of the same name. if - the base of the conditional statements, import - expand the namespace using external modules. in - Used to test if object is in collection (or a key in a dictionary); also used in for loop statements and list comprehensions. (for x in lst: ...) is - identity operator. Used to test object identity (usually memory address). lambda - an expensive, yet one-liner way, to define anonymous functions. map(lambda x:x**2,[1,2,3,4,5]) not - negation operator; also used in the expressions "x is not y" and "z not in lst" . or - boolean or. Invokes __or__ method in lhs operand. pass - Python's NOP. Can be used in order to mark a code block which has no real content. print - print statement. Replaced by the print function in Py3K. raise - Like Java's throw. Deliberately throw an exception. return - used in functions and methods for, well, returning values. If not included, code will return None. try - Like Java's try. Wrapping code blocks which might throw exceptions. while - while loop operator. with - Like Scheme's let. Used to define temporary variables, usually along with the "as" keyword. with open("file.txt") as f:read f yield - Used to define a generator. For more about generators, read here:


Download ppt "Python for Introduction to Complex NetworksStudents"

Similar presentations


Ads by Google