Writing modules and packages

Slides:



Advertisements
Similar presentations
More about Ruby Maciej Mensfeld Presented by: Maciej Mensfeld More about Ruby dev.mensfeld.pl github.com/mensfeld.
Advertisements

Chapter 2.2 – More about Ruby Maciej Mensfeld Presented by: Maciej Mensfeld More about Ruby dev.mensfeld.pl github.com/mensfeld senior.
Exception Handling Genome 559. Review - classes 1) Class constructors - class myClass: def __init__(self, arg1, arg2): self.var1 = arg1 self.var2 = arg2.
Python for Science Shane Grigsby. What is python? Why python? Interpreted, object oriented language Free and open source Focus is on readability Fast.
PYTHON FRUITFUL FUNCTIONS CHAPTER 6 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 28 Classes and Methods 6/17/09 Python Mini-Course: Lesson 28 1.
A Crash Course Python. Python? Isn’t that a snake? Yes, but it is also a...
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
Introduction to Python
1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.
Introduction to Python. What is Python? Interpreted object oriented high level programming language – No compiling or linking neccesary Extensible: add.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
Python: Classes By Matt Wufsus. Scopes and Namespaces A namespace is a mapping from names to objects. ◦Examples: the set of built-in names, such as the.
Programming for Engineers in Python Sawa 2015 Lecture 2: Lists and Loops 1.
Chapter Function Basics CSC1310 Fall Function function (subroutine, procedure)a set of statements different inputs (parameters) outputs In.
Python The tutorial
Python Functions.
©John Samuel 2008 Introduction to PHP. ©John Samuel 2008 Objectives At the end of this class the student will be able to; Create and run a simple php.
 Python’s ‘math’.  Modules.  Files.  Python’s ‘time’.  Classes in python.
Python Let’s get started!.
Function Basics. Function In this chapter, we will move on to explore a set of additional statements that create functions of our own function (subroutine,
Exceptions Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
©John Samuel 2008 Introduction to PHP Note: These slides are not included in coverage for the BIF703 final exam...
LECTURE 2 Python Basics. MODULES So, we just put together our first real Python program. Let’s say we store this program in a file called fib.py. We have.
1 Objective Caml (Ocaml) Aaron Bloomfield CS 415 Fall 2005.
Control Statements 1. Conditionals 2 Loops 3 4 Simple Loop Examples def echo(): while echo1(): pass def echo1(): line = input('Say something: ') print('You.
Lecture III Syntax ● Statements ● Output ● Variables ● Conditions ● Loops ● List Comprehension ● Function Calls ● Modules.
User-Written Functions
Python’s Modules Noah Black.
G. Pullaiah College of Engineering and Technology
Ruby Getting started & Ruby.New Presented by Mario Tayah
Lecture 2 Python Basics.
Module 5 Working with Data
Lesson #6 Modular Programming and Functions.
Classes and objects You can define many objects of the same class:
Modules and Packages Damian Gordon.
Python Let’s get started!.
Lesson #6 Modular Programming and Functions.
Topics Introduction to Repetition Structures
Recitation – Week 8 Pranut jain.
Python’s Modules by E. Esin GOKGOZ.
Functions CIS 40 – Introduction to Programming in Python
Functions.
Programming for Engineers in Python
Topics Introduction to Repetition Structures
Repeating code We could repeat code we need more than once: i = 1 print (i) i += 1 print (i) #… stop when i == 9 But each line means an extra line we might.
Lesson #6 Modular Programming and Functions.
CHAPTER FOUR Functions.
Exception Handling Chapter 9.
Python Primer 2: Functions and Control Flow
Topics Introduction to File Input and Output
Introduction to Python
Functions and Procedures
Coding Concepts (Sub- Programs)
Exception Handling Chapter 9 Edited by JJ.
Interfaces.
Functional Programming
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Lesson #6 Modular Programming and Functions.
G. Pullaiah College of Engineering and Technology
Python Syntax Errors and Exceptions
Recursion Taken from notes by Dr. Neil Moore
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Test Automation For Web-Based Applications
Python Modules.
Topics Introduction to File Input and Output
Functions, Procedures, and Abstraction
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
Functions John R. Woodward.
Presentation transcript:

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.

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"]

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)

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)

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)

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)

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

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

BioPython http://biopython.org/wiki/Biopython Download & Installation http://biopython.org/wiki/Download Documentation http://biopython.org/wiki/Category%3AWiki_Documentation