Docsity

Python Programming: Understanding the Basics and Overcoming Its Weaknesses - Prof. Derek H, Lab Reports of Computer Systems Networking and Telecommunications

Partial preview of the text

Download Python Programming: Understanding the Basics and Overcoming Its Weaknesses - Prof. Derek H and more Lab Reports Computer Systems Networking and Telecommunications in PDF only on Docsity!

CSCI 553: Networking III

Unix Network Programming

Spring 2007

Basic Scripting

Today's Agenda

 Thursday's (2/14) Task  Untar & uncompress unp code  build, test basic datetime client  (^) Thursday (2/21) Task  (^) Project version 0.  definitive project.txt description  Add some design/features/issue discussions  A prototype or at least mock up of main function, some functionality  (^) Lab 03 Discussion  Introduction to Scripting using Python

Lab 03

 Make Variables & Relative Directories install: cp -a libpalindrome.a /home/csci553/harry/harryrepo/lab03/lib cp -a *.h /home/csci553/harry/harryrepo/lab03/include

Lab 03

 Make Variables & Relative Directories OUT_DIR=/home/csci553/harry/harryrepo/lab install: cp -a libpalindrome.a $(OUT_DIR)/lib cp -a *.h $(OUT_DIR)/include

Introduction

Two things determine time to solution:

 (^) How long it takes to write a program (human time)  How long it takes that program to run (machine time) 

Different languages make different tradeoffs

between these [Prechelt 2000 ]

 (^) High-level languages let programmers express their thoughts more quickly…  …but the more abstract the language, the more slowly it runs

Introduction

 This series of lectures introduces a versatile high-level language called Python  (^) A good way to build tools and crunch data

Python’s Weaknesses

 Nimble languages like Python are slower than sturdy languages like Fortran, C/C++, or Java  (^) Factor of 20 is common  But it's relatively easy to call libraries in those other languages from Python 

Doesn't have as many numerical or statistical

tools as MATLAB

 But its Numeric package isn't bad 

Not as widely used as Perl

 But it's catching up

Why Another Language

This course isn't really about Python

 (^) It's about solving software engineering problems 

But we have to write the examples in

something

 Might as well choose something useful  And it puts everyone on an equal footing 

For more information:

 [Lutz & Ascher 2003 ] is the standard introduction  [Martelli 2005 ] is a collection of useful tips and tricks  See Python Cookbook for the on-line version

Execution Cycle

Fig 6.2: Sturdy vs. Nimble Execution

Execution Cycle

This is why sturdy programs run faster,

but nimble programs are faster to write

Compiling gives the computer a chance to

optimize

Load-and-go makes human turnaround

faster

 And as we'll see later, it permits some powerful high-level programming tricks

Execution Shortcuts

 (^) On Unix, make #!/usr/bin/python the first line of the program  (^) This tells Unix to execute /usr/bin/python with the rest of the file as its input  (^) Of course, this doesn't work if Python is installed somewhere else  (^) Use which python to find out  (^) Better: use #!/usr/bin/env python as the first line  (^) This tells Unix to use /usr/bin/env to find Python, then run the script with it $ cat hashbang.py #!/usr/bin/env python print 124/ print 124.0/28. $ hashbang.py 4

 (^) On Windows, associate .py files with Python  (^) Happens automatically when you run the Python Windows installer  (^) Double-clicking on anything ending in .py will then run it

Variables

 (^) Variables are names for values  (^) No declarations: variables are created when something is assigned to them planet = "Pluto" moon = "Charon" p = planet Fig 6.3: Variables Refer to Values

Possible Mistakes

 (^) Must give a variable a value before using it planet = "Sedna" print plant # note the misspelling Traceback (most recent call last): File "lec/inc/py01/undefined_var.py", line 2, in? print plant # note the misspelling NameError: name 'plant' is not defined  (^) Unlike some languages, Python doesn't provide a default value  (^) Because doing so can hide a lot of errors  (^) Note: anything from "#" to the end of the line is a comment  (^) Variables don't have types, but values do  (^) Python complains if you try to operate on incompatible values x = "two" # "two" is a string y = 2 # 2 is an integer print x * y # multiplying a string concatenates it repeatedly twotwo print x + y # but you can't add an integer and a string Traceback (most recent call last): File "lec/inc/py01/add_int_str.py", line 4, in? print x + y # but you can't add an integer and a string TypeError: cannot concatenate 'str' and 'int' objects

Printing

 The print statement prints zero or more values to standard output  (^) Separated by spaces  (^) Automatically puts a newline at the end  (^) So print on its own just prints a blank line  (^) Putting a comma at the end of the line suppresses the newline planet = "Pluto" num_moons = 1 moon = "Charon" print planet, "has", num_moons, "satellite", print "and its name is", moon Pluto has 1 satellite and its name is Charon