1 Python Training for HP OSO Guido van Rossum CNRI 7/23/1999 9am - 1pm.

Slides:



Advertisements
Similar presentations
Slide 1 Insert your own content. Slide 2 Insert your own content.
Advertisements

Copyright © 2003 Pearson Education, Inc. Slide 1-1 The Web Wizards Guide to PHP by David A. Lash.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Chapter 11 Separate Compilation and Namespaces. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Separate Compilation.
Chapter 14 Inheritance. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Inheritance Basics Derived classes, with.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Introduction to Ruby.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Introduction to ASP.NET.
Copyright © 2002 Pearson Education, Inc. Slide 1.
"The State of the Python Union" Python10 - Alexandria, VA - February 7, 2002 Guido van Rossum Director, PythonLabs at Zope Corporation
Why I Invented Python EuroPython – June 27, 2005 Guido van Rossum Elemental Security, Inc.
10/09/1999© 1999 CNRI, Guido van Rossum 1 Computer Programming for Everybody Guido van Rossum CNRI (Corporation for National Research Initiatives, Reston,
Python - an Open Source Project Guido van Rossum CNRI
0 - 0.
Lecture 10 Flow of Control: Loops (Part 2) COMP1681 / SE15 Introduction to Programming.
Website Design What is Involved?. Web Design ConsiderationsSlide 2Bsc Web Design Stage 1 Website Design Involves Interface Design Site Design –Organising.
Lilian Blot Announcements Teaching Evaluation Form week 9 practical session Formative Assessment week 10 during usual practical sessions group 1 Friday.
Configuration management
Modern Programming Languages, 2nd ed.
Section 6.1 CS 106, Fall The Big Q What do we get by being able to define a class?! Or Do we really need this?!
August 2012 This work is licensed under the Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit
Code Correctness, Readability, Maintainability Svetlin Nakov Telerik Corporation
1 Uniform memory access (UMA) Each processor has uniform access time to memory - also known as symmetric multiprocessors (SMPs) (example: SUN ES1000) Non-uniform.
Lilian Blot PART V: FUNCTIONS Core elements Autumn 2013 TPOP 1.
Business Planning using Spreasheets-2 1 BP-2: Good Spreadsheet Practice  There is always the temptation to rush in and start entering data.  However.
CS 240 Computer Programming 1
Bottoms Up Factoring. Start with the X-box 3-9 Product Sum
Lilian Blot CORE ELEMENTS PART V: FUNCTIONS PARAMETERS & VARIABLES SCOPE Lecture 5 Autumn 2014 TPOP 1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Chapter Modules CSC1310 Fall Modules Modules Modules are the highest level program organization unit, usually correspond to source files and.
Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes.
Coding Standard: General Rules 1.Always be consistent with existing code. 2.Adopt naming conventions consistent with selected framework. 3.Use the same.
API Design CPSC 315 – Programming Studio Fall 2008 Follows Kernighan and Pike, The Practice of Programming and Joshua Bloch’s Library-Centric Software.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
An Introduction to Python – Part IV Dr. Nancy Warter-Perez June 23, 2005.
ASP.NET Programming with C# and SQL Server First Edition
TEACHING PROGRAMMING BY ITERATIVE DEEPENING Dr. Mark Lee | School of Computer Science, University of Birmingham
Epydoc API Documentation Extraction in Python Edward Loper.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Modules. A module is a file containing Python definitions and statements intended for use in other Python programs. There are many modules as part of.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
High-Quality Programming Code Code Correctness, Readability, Maintainability, Testability, Etc. SoftUni Team Technical Trainers Software University
Creating your first C++ program
1 Documenting with Javadoc. 2 Motivation  Why document programs? To make it easy to understand, e.g., for reuse and maintenance  What to document? Interface:
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
SE: CHAPTER 7 Writing The Program
1 Life Cycle of Software Specification Design –Risk Analysis –Verification Coding Testing –Refining –Production Maintenance.
Cohesion and Coupling CS 4311
Code Correctness, Readability, Maintainability Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Slide 12.1 Chapter 12 Implementation. Slide 12.2 Learning outcomes Produce a plan to minimize the risks involved with the launch phase of an e-business.
How the Session Works Outline Practical on arrival Talk 1 Reflect on practical Clarify concepts Practical exercises at your own pace Talk 2: Further concepts.
Program Style Chapter 22 IB103 Week 12 (part 2). Modularity: the ability to reuse code Encapsulation: hide data access directly but may use methods (the.
CMSC 1041 Introduction to C Creating your first C program.
GoodOO Programming Practice in Java © Allan C. Milne v
High-Quality Programming Code Code Correctness, Readability, Maintainability Svetlin Nakov Technical Trainer Software University
A Python Tour: Just a Brief Introduction "The only way to learn a new programming language is by writing programs in it." -- B. Kernighan and D. Ritchie.
Individual Testing, Big-O, C++ Bryce Boe 2013/07/16 CS24, Summer 2013 C.
12. MODULES Rocky K. C. Chang November 6, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and William F. Punch and.
Introduction. MIS 5450 Behavioral Layer JavaScript and DOM Structural Layer XHTML Presentation Layer CSS Design Development Process.
Coupling and Cohesion Schach, S, R. Object-Oriented and Classical Software Engineering. McGraw-Hill, 2002.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
High-Quality Programming Code Code Correctness, Readability, Maintainability, Testability, Etc. SoftUni Team Technical Trainers Software University
A Python Tour: Just a Brief Introduction
© 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Python Classes By Craig Pennell.
Software Quality Engineering
Rocky K. C. Chang 15 November 2018 (Based on Dierbach)
Widely used guidance for writing better code.
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Chapter 2. Problem Solving and Software Engineering
Perl Programming Dr Claire Lambert
Presentation transcript:

1 Python Training for HP OSO Guido van Rossum CNRI 7/23/1999 9am - 1pm

2 Plug The Practice of Programming Brian W. Kernighan and Rob Pike Addison-Wesley, 1999 Mostly about C, but very useful!

3 CODE STRUCTURE

4 The importance of readability Most time is spent on maintenance Think about the human reader Can you still read your own code... –next month? –next year?

5 Writing readable code Be consistent (but not too consistent!) Use whitespace judicously Write appropriate comments Write helpful doc strings –not novels Indicate unfinished business

6 Modifying existing code Conform to the existing style –even if its not your favorite style! –local consistency overrides global Update the comments!! –and the doc strings!!!

7 Organizing code clearly Top-down or bottom-up? Pick one style, stick to it Alternative: group by functionality –eg: constructor, destructor housekeeping low level methods high level methods

8 When to use classes (...and when not!) Use a class: –when multiple copies of state needed e.g.: client connections; drawing objects Use a module: –when on copy of state always suffices e.g.: logger; cache Use functions: –when no state needed; e.g. sin()

9 Class hierarchies Avoid deep class hierarchies –inefficient multi-level lookup –hard to read find method definitions –easy to make mistakes name clashes between attribute

10 Modules and packages Modules collect classes, functions Packages collect modules For group of related modules: –consider using a package minimizes chance of namespace clashes

11 Naming conventions (my preferred style) Modules, packages: lowercase except when 1 module ~ 1 class Classes: CapitalizedWords also for exceptions Methods, attrs: lowercase_words Local variables: i, j, sum, x0, etc. Globals: long_descriptive_names

12 The main program In script or program: def main():... if __name__ == __main__: main() In module: def _test():... if __name__ == __main__: _test() Always define a function!

13 DOCUMENTATION

14 Writing comments Explain salient points (only) n = n+1 # include end point Note dependencies, refs, bugs # Assume reader() handles I/O errors # See Knuth, vol.3, page 410 # XXX doesnt handle x<0 yet

15 Writing doc strings """Brief one-line description. Longer description, documenting argument values, defaults, return values, and exceptions. """

16 When NOT to use comments Dont comment whats obvious n = n+1 # increment n Dont put a comment on every line Dont draw boxes, lines, etc. # def remove_bias(self): # self.bias = 0

17 One more thing... UPDATE THE COMMENTS WHEN UPDATING THE CODE! (dammit!)

18 THE LIBRARY

19 The library is your friend! Know what's there Study the library manual –especially the early chapters: Python, string, misc, os services Notice platform dependencies Avoid obsolete modules

20 Stupid os.path tricks os.path.exists(p), isdir(p), islink(p) os.path.isabs(p) os.path.join(p, q,...), split(p) os.path.basename(p), dirname(p) os.path.splitdrive(p), splitext(p) os.path.normcase(p), normpath(p) os.path.expanduser(p)

21 PORTING YOUR BRAIN (from Java to Python)

22 Class or module? Stateless operatons, factory funcs Java: static methods Python: functions in module Singleton state Java: static members, methods Python module globals, functions

23 Private, protected, public? Java: private, protected, public –enforced by compiler (and JVM?) Python: __private –enforced by compiler –loophole: _Class__private _private, _protected, public –used by convention

24 Method/constr. overloading Java: class C { int f() {... } int f(int i) {... } int f(int i, int arg) {... } } Python: class C: def f(i=0, arg=None):...

25 Java interfaces In Python, interfaces often implied class File: def read(self, n):... class CompressedFile: def read(self, n):...

26 Abstract classes Not used much in Python Possible: class GraphicalObject: def draw(self, display): raise NotImplementedError def move(self, dx, dy): raise NotImplementedError....

27 ERROR HANDLING

28 When to catch exceptions When there's an alternative option try: f = open(".startup") except IOError: f = None # No startup file; use defaults To exit with nice error message try: f = open("data") except IOError, msg: print "I/O Error:", msg; sys.exit(1)

29 When NOT to catch them When the cause is likely a bug need the traceback to find the cause! When the caller can catch it keep exception handling in outer layers When you don't know what to do try: receive_message() except: print "An error occurred!"

30 Exception handling style Bad: try: parse_args() f = open(file) read_input() make_report() except IOError: print file, "not found" # (what if read_input() # raises IOError?) Good: parse_args() try: f = open(file) except IOError, msg: print file, msg sys.exit(1) read_input() make_report()

31 Error reporting/logging Decide where errors should go: –sys.stdout - okay for small scripts –sys.stderr - for larger programs –raise exception - in library modules let caller decide how to report! –log function - not recommended better redirect sys.stderr to log object!

32 The danger of except: What's wrong with this code: try: return self.children[O] # first child except: return None # no children Solution: except IndexError:

33 PYTHON PITFALLS

34 Sharing mutable objects through variables a = [1,2]; b = a; a.append(3); print b as default arguments def add(a, list=[]): list.append(a); return list as class attributes class TreeNode: children = []...

35 Lurking bugs bugs in exception handlers try: f = open(file) except IOError, err: print "I/O Error:", file, msg misspelled names in assignments self.done = 0 while not done: if self.did_it(): self.Done = 1

36 Global variables # logging module log = [] def addlog(x): log.append(x) def resetlog(): log = [] # doesnt work! # logging module # corrected version log = [] def addlog(x): log.append(x) def resetlog(): global log log = []

37 kjpylint Detects many lurking bugs –

38 PERFORMANCE

39 When to worry about speed Only worry about speed when... –your code works (!) –and its overall speed is too slow –and it must run many times –and you can't buy faster hardware

40 Using the profile module >>> import profile >>> import xmlini >>> data = open("test.xml").read() >>> profile.run("xmlini.fromxml(data)") >>> profile.run("for i in range(100): xmlini.fromxml(data)") function calls in CPU seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) :1(?) profile:0(for i in range(100): xmlini.fromxml(data)) profile:0(profiler) xmlini.py:105(end_group) xmlini.py:109(start_item) xmlini.py:115(end_item) xmlini.py:125(start_val) xmlini.py:129(end_val) xmlini.py:134(finish_starttag) xmlini.py:143(finish_endtag) xmlini.py:152(handle_proc) xmlini.py:162(handle_charref) xmlini.py:167(handle_entityref) xmlini.py:172(handle_data) xmlini.py:182(handle_comment) xmlini.py:60(fromxml) xmlini.py:70(__init__) xmlini.py:80(getdict) xmlini.py:86(start_top) xmlini.py:92(end_top) xmlini.py:99(start_group)

41 Measuring raw speed # Here's one way import time def timing(func, arg, ncalls=100): r = range(ncalls) t0 = time.clock() for i in r: func(arg) t1 = time.clock() dt = t1-t0 print "%s: %.3f ms/call (%.3f seconds / %d calls)" % ( func.__name__, 1000*dt/ncalls, dt, ncalls)

42 How to hand-optimize code import string, types def dictser(dict, ListType=types.ListType, isinstance=isinstance): L = [] group = dict.get("main") if group: for key in group.keys(): value = group[key] if isinstance(value, ListType): for item in value: L.extend([" ", key, " = ", item, "\n"]) else: L.extend([" ", key, " = ", value, "\n"])... return string.join(L, "")

43 When NOT to optimize code Usually When it's not yet working If you care about maintainability! Premature optimization is the root of all evil (well, almost :)

44 THREAD PROGRAMMING

45 Which API? thread - traditional Python API import thread thread.start_new(doit, (5,)) # (can't easily wait for its completion) threading - resembles Java API from threading import Thread # and much more... t = Thread(target=doit, args=(5,)) t.start() t.join()

46 Atomic operations Atomic: i = None a.extend([x, y, z]) x = a.pop() v = dict[k] Not atomic: i = i+1 if not dict.has_key(k): dict[k] = 0

47 Python lock objects Not reentrant: lock.acquire(); lock.acquire() # i.e. twice! –blocks another thread calls lock.release() No "lock owner" Solution: –threading.RLock class (more expensive)

48 Critical sections lock.acquire() try: "this is the critical section" "it may raise an exception..." finally: lock.release()

49 "Synchronized" methods class MyObject: def __init__(self): self._lock = threading.RLock() # or threading.Lock(), if no reentrancy needed def some_method(self): self._lock.acquire() try: "go about your business" finally: self._lock.release()

50 Worker threads Setup: def consumer():... def producer():... for i in range(NCONSUMERS): thread.start_new(consumer, ()) for i in range(NPRODUCERS): thread.start_new(producer, ()) "now wait until all threads done"

51 Shared work queue Producers: while 1: job = make_job() Q.put(job) Consumers: while 1: job = Q.get() finish_job(job) Shared: import Queue Q = Queue.Queue(0) # or maxQsize

52 Using a list as a queue Shared: Q = [] Producers: while 1: job = make_job() Q.append(job) Consumers: while 1: try: job = Q.pop() except IndexError: time.sleep(...) continue finish_job(job)

53 Using a condition variable Shared: Q = [] cv = Condition() Producers: while 1: job = make_job() cv.acquire() Q.append(job) cv.notify() cv.release() Consumers: while 1: cv.acquire() while not Q: cv.wait() job = Q.pop() cv.release() finish_job(job)

54 TIME FOR DISCUSSION