Presentation is loading. Please wait.

Presentation is loading. Please wait.

Writing modules and packages

Similar presentations


Presentation on theme: "Writing modules and packages"— Presentation transcript:

1 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 fib, 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 fib directory.

2 Writing modules and packages
To use the module fibo, we can import it in two ways: 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: import fib.fibo from fib import fibo __init__.py: __all__ = [”fibo"]

3 Writing modules and packages
Example 1: Create a file fibo.py # Fibonacci numbers module def fib(n): # write Fibonacci series up to n a, b = 0, 1 while b < n: print b, a, b = b, a+b def fib2(n): # return Fibonacci series up to n result = [] result.append(b) return result If you want to account for the methods in fibo.py: import fibo and call the functions through fibo.fib(n) and fibo.fib2(n)

4 Writing modules and packages
Example 2: Create a package fib Create a directory named fib. In directory fib create the file fibo.py. In directory create an empty __init__.py file The __init__.py files are required to make Python treat the directories as containing packages If you want to account for the methods in fibo.py: import fib.fibo and call the functions through fib.fibo.fib(n) and fib.fibo.fib2(n) or from fib.fibo import fib2 and call the functions through fib2(n)

5 Writing modules and packages
Example 3: Create a package fib Create a directory named fib. In directory fib create the file fibo.py. In directory create a __init__.py file an add __all__ = ["fibo"] The __init__.py files are required to make Python treat the directories as containing packages If you want to account for ALL the methods in package fib: from fib import * and call the functions through fibo.fib(n) and fibo.fib2(n)

6 Multiple function arguments
Every function in Python receives a predefined number of arguments, if declared normally, like this: It is possible to declare functions which receive a variable number of arguments, using the following syntax: def myfunction(first, second, third): # do something with the 3 variables ... 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)

7 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

8 Sets Sets are lists with no duplicate entries.
Sets are a powerful tool in Python since they have the ability to calculate differences and intersections between other sets. print set("my name is Eric and Eric is my name".split()) returns set [‘my’, name, ‘is’, ‘Eric’, ‘and’] a = set(["Jake", "John", "Eric"]) b = set(["John", "Jill"]) a.intersection(b) b.intersection(a)

9 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)

10 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)

11 more information

12 BioPython http://biopython.org/wiki/Biopython Download
& Installation Documentation


Download ppt "Writing modules and packages"

Similar presentations


Ads by Google