CSC598BIL675-2016
Download
Report
Transcript CSC598BIL675-2016
Classes and objects
• You can define many objects of the same class:
myobjecty = MyClass()
myobjecty.variable = "works!”
• and print out both values:
print myobjectx.variable
print myobjecty.variable
• To access a function inside of an object you use notation similar to accessing a
variable:
myobjectx.function()
Modules and packages
• Modules in Python are simply Python files with the .py extension, which
implement a set of functions. Modules are imported from other modules
using the import command.
# import the library
Import sys, string
# use it
sys.stdout.write(“…”)
• Two very important functions come in handy when exploring modules in
Python - the dir and help functions.
help(sys.stdout)
dir (sys)
Parsing a file
import sys
import sys module
opens a file to read
file = open(sys.argv[1], ‘r’)
content = file.readlines()
file.close()
filename is the 1st command line argument
returns content as list
closes a file
listA = []
listB = []
for i in content:
line = string.split(i)
listA.append(string.atof(line[0]))
listB.append(string.atof(line[1]))
file = open(sys.argv[2], ‘w’)
for i in listA:
file.write(i+”\n”)
file.close()
opens a file to write
Iterating through list
Writing modules and packages
• To create a module of your own, simply create a new .py file with the module
name, and then import it using the Python file name (without the .py
extension) using the import command.
• Packages are namespaces which contain multiple packages and modules
themselves. They are simply directories, but with a twist.
• Each package in Python is a directory which MUST contain a special file called
__init__.py. This file can be empty, and it indicates that the directory it
contains is a Python package, so it can be imported the same way a module
can be imported.
• If we create a directory called foo, which marks the package name, we can
then create a module inside that package called bar. We also must not forget
to add the __init__.py file inside the foo directory.
Writing modules and packages
• To use the module bar, we can import it in two ways:
import foo.bar
from foo import bar
• The __init__.py file can also decide which modules the package exports as
the API, while keeping other modules internal, by overriding the __all__
variable, like so:
__init__.py:
__all__ = ["bar"]
Multiple function arguments
• Every function in Python receives a predefined number of arguments, if
declared normally, like this:
def myfunction(first, second, third):
# do something with the 3 variables
...
• It is possible to declare functions which receive a variable number of
arguments, using the following syntax:
def foo(first, second, third, *therest):
print "First: %s" % first
print "Second: %s" % second
print "Third: %s" % third
print "And all the rest... %s" % list(therest)
Multiple function arguments
• It is also possible to send functions arguments by keyword, so that the
order of the argument does not matter, using the following syntax:
def bar(first, second, third, **options):
if options.get("action") == "sum":
print "The sum is: %d" % (first + second + third)
if options.get("number") == "first":
return first
result = bar(1, 2, 3, action = "sum", number = "first")
print "Result: %d" % result
Sets
• Sets are lists with no duplicate entries.
print set("my name is Eric and Eric is my name".split())
returns set [‘my’, name, ‘is’, ‘Eric’, ‘and’]
• Sets are a powerful tool in Python since they have the ability
to calculate differences and intersections between other sets.
a = set(["Jake", "John", "Eric"])
b = set(["John", "Jill"])
a.intersection(b)
b.intersection(a)
Sets contd.
a = set(["Jake", "John", "Eric"])
b = set(["John", "Jill"])
print a.symmetric_difference(b)
print b.symmetric_difference(a)
print a.difference(b)
print b.difference(a)
print a.union(b)
Exception handling
• Python's solution to errors are exceptions. You might have seen an
exception before.
def do_stuff_with_number(n):
print n
the_list = (1, 2, 3, 4, 5)
for i in range(20):
try:
do_stuff_with_number(the_list[i])
except IndexError: # Raised when accessing a non-existing index of a list
do_stuff_with_number(0)
more information
www.python.org