Software Tool Seminar WS1516 - Taming the Snake



Similar documents
Objects and classes. Objects and classes. Jarkko Toivonen (CS Department) Programming in Python 1

ESCI 386 Scientific Programming, Analysis and Visualization with Python. Lesson 5 Program Control

CIS 192: Lecture 10 Web Development with Flask

Introduction to Python

CS177 MIDTERM 2 PRACTICE EXAM SOLUTION. Name: Student ID:

Exercise 4 Learning Python language fundamentals

Instruction Set Architecture of Mamba, a New Virtual Machine for Python

Introduction to Python

Moving from CS 61A Scheme to CS 61B Java

Obfuscation: know your enemy

Chapter 2 Writing Simple Programs

Static vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer

Crash Dive into Python

Python for Rookies. Example Examination Paper

Introduction to Python for Text Analysis

CSCE 110 Programming I Basics of Python: Variables, Expressions, and Input/Output

Python Programming: An Introduction To Computer Science

Today s Topics... Intro to Computer Science (cont.) Python exercise. Reading Assignment. Ch 1: 1.5 (through 1.5.2) Lecture Notes CPSC 121 (Fall 2011)

[1] Learned how to set up our computer for scripting with python et al. [3] Solved a simple data logistics problem using natural language/pseudocode.

Chapter 3 Writing Simple Programs. What Is Programming? Internet. Witin the web server we set lots and lots of requests which we need to respond to

Introduction to Python

Building and breaking a Python sandbox

Computer Science for San Francisco Youth

Self-review 9.3 What is PyUnit? PyUnit is the unit testing framework that comes as standard issue with the Python system.

Python Loops and String Manipulation

Optimizing and interfacing with Cython. Konrad HINSEN Centre de Biophysique Moléculaire (Orléans) and Synchrotron Soleil (St Aubin)

Profiling, debugging and testing with Python. Jonathan Bollback, Georg Rieckh and Jose Guzman

flask-mail Documentation

Crash Dive into Python

Exercise 1: Python Language Basics

Slides from INF3331 lectures - web programming in Python

Integrating Fax Sending Services

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Intro to scientific programming (with Python) Pietro Berkes, Brandeis University

Introduction to Big Data with Apache Spark UC BERKELEY

Python Classes and Objects

Chapter 7: Functional Programming Languages

Sources: On the Web: Slides will be available on:

PHP Magic Tricks: Type Juggling. PHP Magic Tricks: Type Juggling

Python Evaluation Rules

Computers. An Introduction to Programming with Python. Programming Languages. Programs and Programming. CCHSG Visit June Dr.-Ing.

The PHP 5.4 Features You Will Actually Use

requests_toolbelt Documentation

A skip list container class in Python

CIS 192: Lecture 13 Scientific Computing and Unit Testing

Python Objects. Charles Severance

2! Multimedia Programming with! Python and SDL

Exercise 0. Although Python(x,y) comes already with a great variety of scientic Python packages, we might have to install additional dependencies:

6.170 Tutorial 3 - Ruby Basics

CIS 192: Lecture 10 Web Development with Flask

Tutorial. Reference for more thorough Mininet walkthrough if desired

Unit testing with mock code EuroPython 2004 Stefan Schwarzer p.1/25

Introduction to Programming Languages and Techniques. xkcd.com FULL PYTHON TUTORIAL

Sorting. Lists have a sort method Strings are sorted alphabetically, except... Uppercase is sorted before lowercase (yes, strange)

Server-side Development using Python and SQL

Programming Exercises

University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python

CPSC Network Programming. , FTP, and NAT.

7 Why Use Perl for CGI?

Deterministic Discrete Modeling

Things you didn't know about Python

COMP 112 Assignment 1: HTTP Servers

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

CS106A, Stanford Handout #38. Strings and Chars

Tools and Techniques for Developing Atmospheric Python Software: Insight from the Python ARM Radar Toolkit

latest Release 0.2.6

Name Spaces. Introduction into Python Python 5: Classes, Exceptions, Generators and more. Classes: Example. Classes: Briefest Introduction

Writing Simple Programs

Informatica e Sistemi in Tempo Reale

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

ft6 Motivation next step: perform the tests usually tedious, error prone work aided by a tool easily repeatable enter ft6 ft6

Electronic Mail

Using EDA Databases: Milkyway & OpenAccess

Overview. Elements of Programming Languages. Advanced constructs. Motivating inner class example

Time Clock Import Setup & Use

COMS Programming Languages Python: Lecture 1. Kangkook Jee

An extension to DBAPI 2.0 for easier SQL queries

The Clean programming language. Group 25, Jingui Li, Daren Tuzi


Lecture 8. IP Fundamentals

PostgreSQL Functions By Example

Python 2 and 3 compatibility testing via optional run-time type checking

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Analog Documentation. Release Fabian Büchler

>

Homework 1. Comp 140 Fall 2008

Introduction to Computer Science I Spring 2014 Mid-term exam Solutions

LEARNING TO PROGRAM WITH PYTHON. Richard L. Halterman

MATH10040 Chapter 2: Prime and relatively prime numbers

socketio Documentation

Programming in Python. Basic information. Teaching. Administration Organisation Contents of the Course. Jarkko Toivonen. Overview of Python

pyownet Documentation

Flask-SSO Documentation

CRASH COURSE PYTHON. Het begint met een idee

What s Up with These Short Sale Buy Backs?

Transcription:

Software Tool Seminar WS1516 - Taming the Snake November 4, 2015 1 Taming the Snake 1.1 Understanding how Python works in N simple steps (with N still growing) 1.2 Step 0. What this talk is about (and what it isn t) Four basic things you need to learn to become a good programmer: 1. Learn how to express algorithms in a computer programming language 2. Learn how to design programs 3. Learn how your tool (the programming language) works and what it can offer you 4. Learn what others already have built for you I will only teach you how Python works. (And this is only the first part of it.) 1.3 Step 1. Everything is an object In Python, everything is an object. Examples: 42, 4.3, Hello world, True, False, None, [0, 1, 2, 3], { key : value, other key : other value } ( This, is, a, tuple ), np.eye(10) Really everything! math.sin, lambda x: x, class C, math, func. code To understand Python, we should first understand what objects are! 1.4 Step 2. The three properties of an object Objects have: 1. Identity 2. State 3. Methods 1

1.5 Step 3. The Identity of an object In [1]: a = [0, 1, 2] b = [0, 1, 2] a == b Out[1]: True In [2]: a is b Out[2]: False In [3]: id(a) Out[3]: 140008350782240 In [4]: id(b) Out[4]: 140008350781880 In [5]: (a is b) == (id(a) == id(b)) Out[5]: True In [6]: x = 6 y = 6 x is y Out[6]: True In [7]: x = 666 y = 666 x is y Out[7]: False In [8]: id(a) is id(a) Out[8]: False Do not use is unless you have a good reason! Reasonable exceptions: x is True x is False x is None This works because True, False, None are signletons in Python, i.e. there is only one object True, etc., in the whole Python universe. 2

1.6 Step 4. Understand assignment In [9]: import numpy as np a = np.ones(10) b = np.zeros(10) print(a) print(b) [ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] In [10]: a is b Out[10]: False In [11]: a = b print(a) print(b) [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] In [12]: a[0] = 1 print(a) print(b) [ 1. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [ 1. 0. 0. 0. 0. 0. 0. 0. 0. 0.] In [13]: a is b Out[13]: True 1.6.1 Definition of assignment a = b means assign the name a to the object with name b 1.6.2 Let s repeat! 1.6.3 Definition of assignment a = b means assign the name a to the object with name b 3

1.7 Step 5. The state of an object Objects have state (data) associated to them, which can change over the lifetime of an object. The state is stored in the objects attributes. In [14]: from email.mime.text import MIMEText m = MIMEText( Hi, this is an email! ) m Out[14]: <email.mime.text.mimetext instance at 0x7f563c04cf38> In [15]: print(m.as_string()) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Hi, this is an email! In [16]: m._payload m._headers Out[16]: [( Content-Type, text/plain; charset="us-ascii" ), ( MIME-Version, 1.0 ), ( Content-Transfer-Encoding, 7bit )] The underscore means, payload and headers are private attributes. You, the user, should not mess around with them. Methods can change attributes: In [17]: m.add_header( From, stephan.rave@uni-muenster.de ) m.add_header( To, evil.overlord@the.hell.com ) m.add_header( Subject, World domination ) print(m.as_string()) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: stephan.rave@uni-muenster.de To: evil.overlord@the.hell.com Subject: World domination Hi, this is an email! In [18]: m._headers Out[18]: [( Content-Type, text/plain; charset="us-ascii" ), ( MIME-Version, 1.0 ), ( Content-Transfer-Encoding, 7bit ), ( From, stephan.rave@uni-muenster.de ), ( To, evil.overlord@the.hell.com ), ( Subject, World domination )] We can also change attributes, even private ones: (Do not try this at home!) In [19]: m._payload = We need to talk! print(m.as_string()) 4

Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: stephan.rave@uni-muenster.de To: evil.overlord@the.hell.com Subject: World domination We need to talk! In [20]: m.my_attribute = 666 m.my_attribute Out[20]: 666 1.7.1 Step 6. Understand the basics of attribute lookup So where do all these attributes come from? In [21]: m. dict Out[21]: { charset : us-ascii, default type : text/plain, headers : [( Content-Type, text/plain; charset="us-ascii" ), ( MIME-Version, 1.0 ), ( Content-Transfer-Encoding, 7bit ), ( From, stephan.rave@uni-muenster.de ), ( To, evil.overlord@the.hell.com ), ( Subject, World domination )], payload : We need to talk!, unixfrom : None, defects : [], epilogue : None, my attribute : 666, preamble : None} In [22]: m. dict [ favourite_song ] = Hotel california m. dict [ _payload ] = WE NEED TO TALK!!! In [23]: m.favourite_song Out[23]: Hotel california In [24]: print(m.as_string()) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: stephan.rave@uni-muenster.de To: evil.overlord@the.hell.com Subject: World domination WE NEED TO TALK!!! 5

1.7.2 Attribute lookup a.b means a. dict [ b ] In [25]: a = np.eye(10) a.secret_answer = 42 --------------------------------------------------------------------------- AttributeError <ipython-input-25-9d909bc47556> in <module>() 1 a = np.eye(10) ----> 2 a.secret answer = 42 AttributeError: numpy.ndarray object has no attribute secret answer In [26]: a. dict --------------------------------------------------------------------------- AttributeError <ipython-input-26-87da7e691200> in <module>() ----> 1 a. dict AttributeError: numpy.ndarray object has no attribute dict In [27]: (42). dict --------------------------------------------------------------------------- AttributeError <ipython-input-27-f22e1a648ac9> in <module>() ----> 1 (42). dict AttributeError: int object has no attribute dict 6

In [28]: [0, 1, 2]. dict --------------------------------------------------------------------------- AttributeError <ipython-input-28-cb92ad523bf5> in <module>() ----> 1 [0, 1, 2]. dict AttributeError: list object has no attribute dict 1.7.3 Attribute lookup If a has a dict, a.b means a. dict [ b ] This is not the case for builtin types or C extension types. In [29]: class C(object): pass Out[29]: {} c = C() c. dict In [30]: c.secret_answer = 42 c. dict Out[30]: { secret answer : 42} In [31]: class C(object): secret_answer = 42 Out[31]: 42 c = C() c.secret_answer In [32]: c. dict Out[32]: {} In [33]: c. class Out[33]: main.c In [34]: c. class. dict Out[34]: <dictproxy { dict : <attribute dict of C objects>, doc : None, module : main, weakref : <attribute weakref of C objects>, secret answer : 42}> 7

1.7.4 Attribute lookup (simplified) If a has a dict, a.b means if b in a. dict : return a. dict [ b ] elif b in a. class. dict : return a. class. dict [ b ] elif b in base class dicts : return base_class. dict [ b ] else: raise AttributeError This is not the case for builtin types or C extension types. 1.7.5 Step 7. Be careful with class attributes In [35]: class C(object): great_list_of_awesomeness = [] c1 = C() c2 = C() In [36]: c1.great_list_of_awesomeness.append(42) c1.great_list_of_awesomeness Out[36]: [42] In [37]: c2.great_list_of_awesomeness Out[37]: [42] This might not be what you want! 1.7.6 Step 8. Understand methods Methods are functions defined in class definitions as follows: In [38]: class C(object): Out[38]: 42 def init (self, x): self.x = x def f(self, y): return self.x + y c = C(11) c.f(31) 8

c.f(31) translates to C.f(c, 31) i.e. calling a method on an object c magically inserts c as first argument of the method. This also explains this strange error: In [39]: c.f() --------------------------------------------------------------------------- TypeError <ipython-input-39-69c69864e152> in <module>() ----> 1 c.f() TypeError: f() takes exactly 2 arguments (1 given) init is one of Python s many special methods. It is called, after a new object has been created. Thus c = C(11) translates to c = newly_created_c_instance c. init (c, 11) We can add new methods to the class at any time: In [40]: def g(ego, y): return ego.x * y Out[40]: 22 C.mult = g c.mult(2) Note that self as first argument name is pure convention. You should really stick to it! Adding functions directly to objects does not work: In [41]: def h(self, y): return self.x - y c.h = h c.h(-31) 9

--------------------------------------------------------------------------- TypeError <ipython-input-41-f498a20a0d9d> in <module>() 3 4 c.h = h ----> 5 c.h(-31) TypeError: h() takes exactly 2 arguments (1 given) Oh no, the magic is not working! In [42]: import types c.h = types.methodtype(h, c) c.h(-31) Out[42]: 42 1.7.7 Step 9. Understand immutable objects Numbers, strings and tuples are immutable in Python: In [43]: t = (0, 1, 2) t[1] = 2 --------------------------------------------------------------------------- TypeError <ipython-input-43-4ca911cddf58> in <module>() 1 t = (0, 1, 2) ----> 2 t[1] = 2 TypeError: tuple object does not support item assignment There is absolutely no way to modify them! So what about here: In [44]: x = 41 y = x x += 1 In [45]: y Out[45]: 41 10

In [46]: x is y Out[46]: False In [47]: x Out[47]: 42 So, x refers to a new int object with value 42. Think what would have happend, if the object had stayed the same! This is, how inplace addition works: In [48]: class MyNumber(object): def init (self, value): self.value = value def iadd (self, other): return MyNumber(self.value + other) In [49]: n = MyNumber(12) print(n.value) print(id(n)) n += 7 print(n.value) print(id(n)) 12 140008016975120 19 140008149531472 n += 7 is really the same as n = n. iadd (7) What happens here? In [50]: class C(object): value = 42 c1 = C() c2 = C() c1.value = 666 In [51]: c1.value Out[51]: 666 In [52]: c2.value Out[52]: 42 In [53]: print(c1. dict ) print(c2. dict ) print(c. dict ) { value : 666} {} { dict : <attribute dict of C objects>, module : main, weakref : <attribute wea 11

1.7.8 Step 10. Be careful with default arguments: Default arguments are attributes of the function object: In [54]: def f(x, more_values=[]): more_values.append(x) print(more_values) In [55]: f. defaults Out[55]: ([],) In [56]: print(dir(f)) [ call, class, closure, code, defaults, delattr, dict, doc, for In [57]: def f(x, more_values=[]): more_values.append(x) print(more_values) In [58]: f(1) [1] In [59]: f(42, [4, 8, 15, 16, 23, 42]) [4, 8, 15, 16, 23, 42, 42] In [60]: f(1) f(1) [1, 1] [1, 1, 1] In [61]: f. defaults Out[61]: ([1, 1, 1],) In [62]: f. defaults = ([],) f(1) [1] 12