INF1204-FUNDAMENTALS OF PROGRAMMING II

Download Report

Transcript INF1204-FUNDAMENTALS OF PROGRAMMING II

INF1204-FUNDAMENTALS OF
PROGRAMMING II
- 15 Credits
- 15 weeks
- 4 hours a week
Course Description
• This course is an introduction to computer
programming using the Python programming
language.
• This course covers basic procedural
techniques such as variables, data types,
selection, iteration, and functions.
Learning Outcomes
• write computer programs using the Python
programming language
• manipulate different types of data types found in
Python
• write and understand basic computer algorithms
• mentally execute and trace the execution of
computer programs
• find and correct simple computer program bugs
• create functions and use them in computer
programs
Introduction
• Python is a high-level general purpose programming
language.
• Because code is automatically compiled to byte code and e
xecuted, Python is suitable for use as a scripting language,
Web application implementation language, etc.
• Because Python can be extended in C and C++, Python can
provide the speed needed for even compute intensive task
s.
• Because of its strong structuring constructs (nested code bl
ocks, functions, classes, modules, and packages) and its co
nsistent use of objects and objectoriented programming, Py
thon enables us to write clear, logical applications for small
and large tasks.
Important Features of Python
•
•
•
•
•
•
Builtin high level data types: strings, lists, dictionaries, etc.
The usual control structures: if, ifelse, ifelifelse, while, plus a powerful collection iterator (for).
Multiple levels of organizational structure: functions, classes, modules, and packa
ges. These assist in organizing code. An excellent and large example is the Python
standard library.
Compile on the fly to byte code Source code is compiled to byte code without a separate compile step. Source co
de modules can also be "precompiled" to byte code files.
Objectoriented Python provides a consistent way to use objects: everything is an object. And, in P
ython it is easy to implement new object types (called classes in objectoriented programming).
Extensions in C and C++ Extension modules and extension types can be written by hand. There are also to
ols that help with this, for example, SWIG, sip, Pyrex.
Jython is a version of Python that "plays well with" Java.
Some things you will need to know:
•
•
•
•
•
•
•
•
•
•
•
•
•
Python uses indentation to show block structure. Indent one level to show the beginning of a block
. Outdent one level to show the end of a block. As an example, the following Cstyle code:
if (x) {
if (y)
{
f1()
}
f2()
}
in Python would be:
if x:
if y:
f1()
f2()
And, the convention is to use four spaces (and no hard tabs) for each level of indentation. Actually,
it's more than a convention; it's practically a requirement. Following that "convention" will make it
so much easier to merge your Python code with code from other sources.
Week 2-3: MODULES and PACKAGES
o MODULES
o What is a module in Python?
o You can use a module to organize a number of
Python definitions in a single file.
o A definition can be a function, a class, or a var
iable containing any Python object.
o Here is an example: python_101_module_si
mple.py
Modules
• Comments:
The string definitions at the beginning of each
of the module, class definitions, and function
definitions serve as documentation for these it
ems. You can show this documentation with t
he following from the commandline:
• $ pydoc python_101_module_simple
Modules
• Or this, from the Python interactive prompt:
• >>> import python_101_module_simple
>>> help(python_101_module_simple)
• It is common and it is a good practice to includ
e a test harness for the module at the end of t
he source file. Note that the test:
• if __name__ == '__main__':
Modules
• will be true only when the file is run (e.g. from
the commandline with something like:
• "$ python python_101_module_simple.py
• but not when the module is imported.
• Remember that the code in a module is only evaluated the first time it is imported in a program.
Modules
• So, for example, change the value of a global v
ariable in a module might cause behavior that
users of the module might not expect.
• Constants, on the other hand, are safe. A cons
tant, in Python, is a variable whose value is ini
tialized but not changed. An example is LABEL,
above.
Packages
• A package is a way to organize a number of m
odules together as a unit.
• Python packages can also contain other packa
ges.
• To give us an example to talk about, consider t
he follow package structure:
Packages
• package_example/
package_example/__init__.py
package_example/module1.py
package_example/module2.py
package_example/A.py
package_example/B.py
Packages
• And, here are the contents:
• __init__.py:
• # __init__.py
# Expose definitions from modules in this packag
e. from module1 import class1
from module2 import class2
Packages
• module1.py:
#module1.py:
class class1:
def __init__(self):
self.description = 'class #1‘
def show(self):
print self.description
Packages
• module2.py:
# module2.py
class class2:
def __init__(self):
self.description = 'class #2‘
def show(self):
print self.description
Packages
• A.py:
# A.py
import B
• B.py:
# B.py
def function_b():
print 'Hello from function_b'
Packages
• In order to be used as a Python package (e.g. so t
hat modules can be imported from it) a directory
must contain a file whose name is __init__.py.
• The code in this module is evaluated the first tim
e a module is imported from the package.
• In order to import modules from a package, you
may either add the package directory to sys.path
or, if the parent directory is on sys.path, use dotnotation to explicitly specify the path.
• In our example, you might use: "import package_
example.module1".
Packages
• A module in a package can import another mo
dule from the same package directly without
using the path to the package. For example, th
e module A in our sample package package_e
xample can import module B in the same pack
age with "import B". Module A does not need
to use "import package_example.B".
Packages
• In the __init__.py file, import and make availa
ble objects defined in modules in the package
. Our sample package package_example does t
his. Then, you can use from package_example
import * to import the package and its conten
ts. For example:
Packages
• >>> from package_example import *
• >>> dir() ['__builtins__', '__doc__', '__file__', '__name__',
'atexit', 'class1', 'class2', 'module1', 'module2',
'readline', 'rlcompleter', 'sl', 'sys']
• >>>
• >>> c1 = class1()
• >>> c2 = class2()
• >>> c1.show()
• class #1
• >>> c2.show()
• class #2
Packages
• With Python 2.3, you can collect the modules i
n a package into a Zip file by using PyZipFile fr
om the Python standard library. See http://w
ww.python.org/doc/current/lib/pyzipfileobjects.html.
Packages
• >>> import zipfile
>>> a = zipfile.PyZipFile('mypackage.zip', 'w', z
ipfile.ZIP_DEFLATED)
>>> a.writepy('Examples')
>>> a.close()
Packages
• Then you can import and use this archive by in
serting its path in sys.path. In the following ex
ample, class_basic_1 is a module within packa
ge mypackage:
Packages
• Then you can import and use this archive by in
serting its path in sys.path. In the following ex
ample, class_basic_1 is a module within packa
ge mypackage:
Packages
• >>> import sys
>>> sys.path.insert(0, '/w2/Txt/Training/mypa
ckage.zip')
>>> import class_basic_1 Basic name: Apricot
>>> obj = class_basic_1.Basic('Wilma')
>>> obj.show() Basic name: Wilma
Implementing Python Packages
• In order to be able to import individual modul
es from a directory, the directory must contai
n a file named __init__.py.
• (Note that requirement does not apply to dire
ctories that are listed in PYTHONPATH.)
• The __init__.py serves several purposes:
Cont
• The presence of the file __init__.py in a directory
marks the directory as a Python package, which
enables importing modules from the directory.
• The first time an application imports any module
from the directory/package, the code in the mod
ule __init__ is evaluated.
• If the package itself is imported (as opposed to an
individual module within the directory/package)
, then it is the __init__ that is imported (and eval
uated).
Using Packages
• One simple way to enable the user to import and
use a package is to instruct the use to import indi
vidual modules from the package.
A second, slightly more advanced way to enable t
he user to import the package is to expose those
features of the package in the __init__ module. S
uppose that module mod1 contains functions fu
n1a and fun1b and suppose that module mod2 co
ntains functions fun2a and fun2b. Then file __init
__.py might contain the following:
cont
• from mod1 import fun1a, fun1b
from mod2 import fun2a, fun2b
• Then, if the following is evaluated in the user's
code:
import testpackages
cont
• Then testpackages will contain fun1a, fun1b, fun2
a, and fun2b.
For example, here is an interactive session that de
mostrates importing the package:
• >>> import testpackages
>>> print dir(testpackages)
[`__builtins__', `__doc__', `__file__', `__name__',
`__path__',
`fun1a', `fun1b', `fun2a', `fun2b', `mod1', `mod2']
Distributing and Installing Packages
• Distutils (Python Distribution Utilities) has spe
cial support for distrubuting and installing pac
kages. Learn more here: Distributing Python M
odules http://docs.python.org/distutils/index.html.
As our example, imagine that we have a direct
ory containing the following:
cont
Testpackages
Testpackages/README
Testpackages/MANIFEST.in
Testpackages/setup.py
Testpackages/testpackages/__init__.py
Testpackages/testpackages/mod1.py
Testpackages/testpackages/mod2.py
cont
• Notice the subdirectory Testpackages/testpackages containin
g the file __init__.py. This is the Python packa
ge that we will install.
• We'll describe how to configure the above file
s so that they can be packaged as a single dist
ribution file and so that the Python package th
ey contain can be installed as a package by Di
stutils.
cont
• The MANIFEST.in file lists the files that we wan
t included in our distribution. Here is the cont
ents of our MANIFEST.in file:
• include README MANIFEST MANIFEST.in
include setup.py
include testpackages/*.py
cont
• The setup.py file describes to Distutils (1) how
to package the distribution file and (2) how to
install the distribution.
• Here is the contents of our sample setup.py:
cont
#!/usr/bin/env python
from distutils.core import setup
# [1]
long_description = 'Tests for installing and distributing Python package
s'
setup(name = 'testpackages',
# [2]
version = '1.0a',
description = 'Tests for Python packages',
maintainer = 'Dave Kuhlman',
maintainer_email = '[email protected]',
url = 'http://www.rexx.com/~dkuhlman',
long_description = long_description,
packages = ['testpackages']
# [3]
)
cont
• Explanation: 1.
We import the necessary component from Distuti
ls. 2.
We describe the package and its developer/maint
ainer. 3.
We specify the directory that is to be installed as
a package. When the user installs our distributio
n, this directory and all the modules in it will be in
stalled as a package.
Now, to create a distribution file, we run the follo
wing:
cont
• python setup.py sdist formats=gztar
• which will create a file testpackages1.0a.tar.gz under the directory dist.
Then, you can give this distribution file to a po
tential user, who can install it by doing the foll
owing:
cont
• $ tar xvzf testpackages1.0a.tar.gz
$ cd testpackages1.0a $ python setup.py build
$ python setup.py install
# as root
Overview of Python Standard Library
• The “Python library” contains several different
kinds of components.
• It contains data types that would normally be
considered part of the “core” of a language, such
as numbers and lists. For these types, the Python
language core defines the form of literals and
places some constraints on their semantics, but
does not fully define the semantics.
• On the other hand, the language core does define
syntactic properties like the spelling and priorities
of operators.
Cont
• The library also contains built-in functions and
exceptions — objects that can be used by all
Python code without the need of an import
statement. Some of these are defined by the
core language, but many are not essential for
the core semantics and are only described
here.
Cont
• The bulk of the library, however, consists of a
collection of modules. There are many ways to
dissect this collection. Some modules are written
in C and built in to the Python interpreter; others
are written in Python and imported in source
form. Some modules provide interfaces that are
highly specific to Python, like printing a stack
trace; some provide interfaces that are specific to
particular operating systems, such as access to
specific hardware; others provide interfaces that
are specific to a particular application domain,
like the World Wide Web.
cont
• Some modules are available in all versions and
ports of Python; others are only available
when the underlying system supports or
requires them; yet others are available only
when a particular configuration option was
chosen at the time when Python was
compiled and installed.
String Handling
• What are strings?
• Strings in programming are simply text, either
individual characters, words, phrases, or
complete sentences.
• They are one of the most common elements
to use when programming, at least when it
comes to interacting with the user.
String
• Because they are so common, they are a
native data type within Python, meaning they
have many powerful capabilities built-in.
• In Python, strings are immutable sequences of
characters.
• They are immutable in that in order to
modify a string, you must produce a new strin
g.
When to use strings
•
•
•
•
•
•
Any text information.
How to use strings
Create a new string from a constant:
s1 = 'abce‘
s2 = "xyz“
s3 = ""“A
multiline
string.
"""
Cont
• Use any of the string methods, for example:
• >>> 'The happy cat ran home.'.upper()
'THE HAPPY CAT RAN HOME.'
>>> 'The happy cat ran home.'.find('cat')
10
>>> 'The happy cat ran home.'.find('kitten')
-1
>>> 'The happy cat ran home.'.replace('cat', 'dog')
'The happy dog ran home.'
Cont
• Type "help(str)" or see http://www.python.org
/doc/current/lib/stringmethods.html for more information on string
methods.
Cont
• You can also use the equivalent functions from
the string module. For example:
• >>> import string
>>> s1 = 'The happy cat ran home.'
>>> string.find(s1, 'happy')
4
Cont
• There is also a string formatting operator: "%". For example:
• >>> state = 'California‘
• >>> 'It never rains in sunny %s.' % state
'It never rains in sunny California.‘
>>>
>>> width = 24
>>> height = 32
>>> depth = 8
>>> print 'The box is %d by %d by %d.' % (width, height, depth, )
The box is 24 by 32 by 8.
Cont
• Things to know:
• Format specifiers consist of a percent sign foll
owed by flags, length, and a type character.
• The number of format specifiers in the target s
tring (to the left of the "%" operator) must be
the same as the number of values on the right
.
• When there are more than one value (on the r
ight), they must be provided in a tuple.
Cont
• You can also write strings to a file and read the
m from a file. Here are some examples.
Writing For example:
• >>> outfile = open('tmp.txt', 'w')
>>> outfile.write('This is line #1\n')
>>> outfile.write('This is line #2\n')
>>> outfile.write('This is line #3\n')
>>> outfile.close()
Cont
• Notes:
• Note the endofline character at the end of each string.
• The open() builtin function creates a file object. It takes as arg
uments (1) the file name and (2) a mode. Co
mmonly used modes are "r" (read), "w" (write
), and "a" (append).
Cont
• Reading an entire file example:
• >>> infile = file('tmp.txt', 'r')
>>> content = infile.read()
• >>> print content
• This is line #1
• This is line #2
• This is line #3
• >>> infile.close()
Cont
• Notes:
Also consider using something like content.spli
tlines(), if you want to divide content in lines (
split on newline characters).
Reading a file one line at a time example:
Cont
• >>> infile = file('tmp.txt', 'r')
>>> for line in infile:
• ... print 'Line:', line
• ...
• Line: This is line #1
• Line: This is line #2
• Line: This is line #3
• >>> infile.close()
Cont
• Notes:
• Learn more about the for: statement in section fo
r: statement.
• "infile.readlines()" returns a list of lines in the file.
For large files use the file object itself or "infile.x
readlines()", both of which are iterators for the lin
es in the file.
• In older versions of Python, a file object is not itse
lf an iterator. In those older versions of Python, y
ou may need to use infile.readlines() or a while lo
op containing infile.readline() For example:
Cont
• >>> infile = file('tmp.txt', 'r')
>>> for line in infile.readlines():
... print 'Line:', line ...
• A few additional comments about strings:
• A string is a special kind of sequence. So, you c
an index into the characters of a string and yo
u can iterate over the characters in a string. Fo
r example:
Cont
• >>> s1 = 'abcd‘
• >>> s1[1]
• 'b‘
• >>> s1[2]
• 'c‘
• >>> for ch in s1:
• ... print ch
• ...
• a
• b
• c
• d
Cont
• An interesting feature of string formatting is th
e ability to use dictionaries to supply the valu
es that are inserted. Here is an example:
• names = {'tree': 'sycamore', 'flower': 'poppy', '
herb': 'arugula'}
• print 'The tree is %(tree)s' % names
print 'The flower is %(flower)s' % names
print 'The herb is %(herb)s' % names
Basic String Operations
• The "+" and "*" operators are overloaded in
Python, letting you concatenate and repeat
string objects, respectively.
• Overloading is just using the same operator to
do multiple things, based on the situation
where it’s used.
• For example, the “+” symbol can mean
addition when two numbers are involved or,
as in this case, combining strings.
Cont
• Concatenation combines two (or more) strings
into a new string object whereas repeat
simply repeats a given string a given number
of times.
• Here are some examples:
• >>> len( ’abc ’) #length : number items
• 3
Cont
• >>> ’abc ’ + ’def ’ #concatenation : a new string
’abcdef ’
• >>> ’Ni! ’ ∗ 4 #multiple concatentation : "Ni!"
+ "Ni!" + ...
’Ni!Ni!Ni!Ni! ’
Cont
• You need to be aware that Python doesn’t
automatically change a number to a string, so
writing “spam” + 3 will give you an error.
• To explicitly tell Python that a number should
be a string, simply tell it.
• >>>str (3) #converts number to string
• Just remember that you can no longer
perform mathematical functions with it; it’s
strictly text.
Cont
• Iteration in strings is a little different than in
other languages. Rather than creating a loop
to continually go through the string and print
out each character, Python has a built-in type
for iteration. Here’s an example followed by an
explanation:
Cont
• >>> myjob = "lumberjack“
• >>> for c in myjob: print c , #step through
items ...
• lumberjack
• >>> "k" in myjob #1 means true
1
Cont
• Essentially what is happening is that Python is
sequentially going through the variable
“myjob” and printing each character that
exists in the string.
Combining and Separating Strings
• Strings can be combined (joined) and
separated (split) quite easily.
• Tokenization is the process of splitting
something up into individual tokens; in this
case, a sentence is split into individual words.
Cont
• The Python interpreter tokenizes the source
code and identifies the parts that are part of
the actual programming language and the
parts that are data.
• The individual tokens are separated by
delimiters, characters that actually separate
one token from another.
Cont
• In strings, the main delimiter is a whitespace
character, such as a tab, a newline, or an
actual space.
• These delimiters mark off indi- vidual
characters or words, sentences, and
paragraphs.
Cont
• Joining strings combines the separate strings
into one string.
• Because string operations always create a new
string, you don’t have to worry about the
original strings being overwritten.
• The catch is that it doesn’t concatenate the
strings, i.e. joining doesn’t combine them like
you would expect.
• Here’s an example:
Cont
•
•
•
•
•
>>>string1 = "1 2 3"
>>>string2= "A B C"
>>>string3 = string2 . join ( string1 )
>>>print string3
1A B C A B C2A B C A B C3
Times and Dates
• A Python program can handle date & time in
several ways.
• Python's time and calendar modules help
track dates and times.
How to Measure Time
• Time intervals are floating-point numbers in
units of seconds.
• Particular instants in time are expressed in
seconds since 12:00am, January 1,
1970(epoch).
Tick
• There is a popular time module available in
Python which provides functions for working
with times, and for converting between
representations.
• The function time.time() returns the current
system time in ticks since 12:00am, January 1,
1970(epoch).
Cont
• >>> import time; # This is required to include
time module.
• >>> ticks = time.time()
• >>> print "Number of ticks since 12:00am,
January 1, 1970:", ticks
Number of ticks since 12:00am, January 1, 1970:
xxxxxxx.xxx
Date Arithmetic
• Date arithmetic is easy to do with ticks.
However, dates before the epoch cannot be
represented in this form. Dates in the far
future also cannot be represented this way the cutoff point is sometime in 2038 for UNIX
and Windows.
Time Tuple
• What is TimeTuple?
• Many of Python's time functions handle time
as a tuple of 9 numbers, as shown below:
Time Tuple
Index
Field
Values
0
4-digit year
2014
1
Month
1 to 12
2
Day
1 to 31
3
Hour
0 to 23
4
Minute
0 to 59
5
Second
0 to 61 (60 or 61 is a leap
second)
6
Day of the week
0 to 6
7
Day of the year
1 to 366
8
Daylight savings
-1, 0, 1, -1 means library
determined DST
Struct_time
• The above tuple is equivalent to struct_time
structure. This structure has following
attributes:
struct-time
Index
Attributes
Values
0
tm_year
2014
1
tm_mon
1 to 12
2
tm_mday
1 to 31
3
tm_hour
0 to 23
4
tm_min
0 to 59
5
tm_sec
0 to 61 (60 and 61 are the
leap second)
6
tm_wday
0 to 6
7
tm_yday
1 to 366 (Julian Day)
8
tm_isdst
-1,0,1, -1 means library
determined DST
Getting current time
• To translate a time instant from a seconds
since the epoch floating-point value into a
time-tuple, pass the floating- point value to a
function (e.g., localtime) that returns a timetuple with all nine items valid.
cont
• >>> import time;
• >>> localtime = time.localtime(time.time())
>>> print "Local current time :", localtime
Getting formatted time
• You can format any time as per your
requirement, but simple method to get time
in readable format is asctime():
• >>> import time;
• >>> localtime = time.asctime(
time.localtime(time.time()) )
• >>> print "Local current time :", localtime
Getting calendar for a month
• The calendar module gives a wide range of
methods to play with yearly and monthly
calendars. Here, we print a calendar for a
given month ( Jan 2008 ):
• >>> import calendar
• >>> cal = calendar.month(2008, 1)
• >>> print "Here is the calendar:"
• >>> print cal
time module
• There is a popular time module available in
Python which provides functions for working
with times and for converting between
representations.
calendar module
• The calendar module supplies calendarrelated functions, including functions to print
a text calendar for a given month or year.
Mathematics and Numbers
• Number data types store numeric values. They
are immutable data types, which means that
changing the value of a number data type
results in a newly allocated object.
• Number objects are created when you assign
a value to them. For example:
• var1 = 1
• var2 = 10
Delete a number object
• You can also delete the reference to a number
object by using the del statement. The syntax
of the del statement is:
• del var1[,var2[,var3[....,varN]]]]
• You can delete a single object or multiple
objects by using the del statement. For
example:
• del var
• del var_a, var_b
Numerical Types
• Python supports four different numerical
types:
• int (signed integers): often called just integers
or ints, are positive or negative whole
numbers with no decimal point.
• long (long integers ): or longs, are integers of
unlimited size, written like integers and
followed by an uppercase or lowercase L.
cont
• float (floating point real values) : or floats,
represent real numbers and are written with a
decimal point dividing the integer and
fractional parts. Floats may also be in scientific
notation, with E or e indicating the power of
10 (2.5e2 = 2.5 x 102 = 250).
cont
• complex (complex numbers) : are of the form
a + bJ, where a and b are floats and J (or j)
represents the square root of -1 (which is an
imaginary number). a is the real part of the
number, and b is the imaginary part. Complex
numbers are not used much in Python
programming.
Examples
int
long
float
complex
10
51924361L
0.0
3.14j
100
-0x19323L
15.20
45.j
Number Type Conversion:
• Python converts numbers internally in an
expression containing mixed types to a
common type for evaluation.
• But sometimes, you'll need to coerce a
number explicitly from one type to another to
satisfy the requirements of an operator or
function parameter.
cont
• Type int(x)to convert x to a plain integer.
• Type long(x) to convert x to a long integer.
• Type float(x) to convert x to a floating-point
number.
• Type complex(x) to convert x to a complex
number with real part x and imaginary part zero.
• Type complex(x, y) to convert x and y to a
complex number with real part x and imaginary
part y. x and y are numeric expressions
Mathematical Functions:
• Python includes following functions that
perform mathematical calculations.
cont
Function
Returns ( description )
abs(x)
The absolute value of x: the (positive) distance between x and
zero.
ceil(x)
The ceiling of x: the smallest integer not less than x
cmp(x,y)
-1 if x < y, 0 if x == y, or 1 if x > y
exp(x)
The exponential of x: ex
fabs(x)
The absolute value of x.
floor(x)
The floor of x: the largest integer not greater than x
log(x)
The natural logarithm of x, for x> 0
log10(x)
The base-10 logarithm of x for x> 0 .
max(x1,x2,…)
The largest of its arguments: the value closest to positive
infinity
cont
Function
Returns (description)
min(x1,x2,….)
The smallest of its arguments: the value closest to negative
infinity
modf(x)
The fractional and integer parts of x in a two-item tuple.
Both parts have the same sign as x. The integer part is
returned as a float.
pow(x,y)
The value of x**y.
round(x,[.n])
x rounded to n digits from the decimal point. Python
rounds away from zero as a tie-breaker: round(0.5) is 1.0
and round(-0.5) is -1.0.
sqrt(x)
The square root of x for x > 0
Trigonometric Functions
Function
Description
acos(x)
Return the arc cosine of x, in radians.
asin(x)
Return the arc sine of x, in radians.
atan(x)
Return the arc tangent of x, in radians.
atan2(x,y)
Return atan(y / x), in radians.
cos(x)
Return the cosine of x radians.
hypot(x,y)
Return the Euclidean norm, sqrt(x*x + y*y).
sin(x)
Return the sine of x radians.
tan(x)
Return the tangent of x radians.
degrees(x)
Converts angle x from radians to degrees.
radians(x)
Converts angle x from degrees to radians.
Mathematical Constants
Constant
Description
pi
The mathematical constant pi.
e
The mathematical constant e.
Command-Line Programming
•
•
•
•
•
•
•
A command is used to invocate a script
One liners -> shell scripts -> applications
A lot of interfaces:
Files
Pipes
User’s input/output
Networking
cont
• The Unix model:
• Lots of small tools that can be combined in
lots of useful ways.
Why Python for command-line
programming
•
•
•
•
•
It is available in a wide range of platforms
Readable, consistent syntax
Easy to write and maintain
Scale well with large apps and libraries
Lots of modules (operating system functions,
networking, file systems)
if _name_==‘__main__’
•
•
•
•
•
•
For any Python script break it up into:
Functions
A main function, called from command line
Make it easy to:
Test functionality
Reuse the functions
Anatomy of a command line
Files
•
•
•
•
Reading, writing, appending to files
Text or binary format
Example:file1.py
Example:file2.py
Pipes
•
•
•
•
•
•
•
Instead of a filename, pipe input/output
Create chains of tools
Standard pipes:
Input: stdin
Output: stdout & stderr
The sys module has support for these
The fileinput module support reading from
stdin and files
Passing of an argument/arguments
• Allow user to specify argument
• (edit script, modify a file)
• Need to handle (flags, strings, pipes, invalid
number of commands, type checking, range
checking)
Thoughts
• Always provide help at the command line
• Be consistent (short/long flag, dangerous flag,
sensible abbreviation for flags)
Calling Command
• Python can execute other applications
• Example: subprocess module is the best of
STD library modules.
• The envoy module is a whole lot easier and
more Pythonic.