cltl python course: object oriented programming (3/3)

12
CLTL Python Course Object Oriented Programming in Python 3 rd session May 15 th 2013

Upload: ruben-izquierdo-bevia

Post on 12-Jul-2015

92 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CLTL python course: Object Oriented Programming (3/3)

CLTL Python Course

Object Oriented Programming in Python 3rd session

May 15th 2013

Page 2: CLTL python course: Object Oriented Programming (3/3)

Reminders

● Session I

– What is Object Oriented Programming

– Classes, objects, methods, properties...

– How to define classes, with methods and attributes

– How to create objects● Session II

– Create modules

– Create packages

– Reuse and import

Page 3: CLTL python course: Object Oriented Programming (3/3)

Session 3

● Eclipse and PyDev

– Use python within eclipse

– Create projects

– Create packages

– Create modules

– ...

Page 4: CLTL python course: Object Oriented Programming (3/3)

Eclipse and PyDev

1) Create a PyDev project

Will contain all the structure, folders and information of the project

2) Create a source folder in the project

Required for creating modules and packages

3) Create packages

You already know python packages

4) Create modules

You also know python modules

Page 5: CLTL python course: Object Oriented Programming (3/3)

Creating a PyDev project

● File > new > project > PyDev > PyDev Project

● Project name: the name of the project

● Project contents: the folder to locate the project

● Project type: usually Python

● Grammar version: the python grammar version (not important)

● Interpreter: which python you want use to run the project

● Create default 'src' folder

● Referenced projects: their source folders are added to the PYTHONPATH for the new project

Page 6: CLTL python course: Object Oriented Programming (3/3)

Creating a source folder

● Required in PyDev for creating packages and modules

– For every new package/module, an existing source folder must be selected

● Two ways of creating (if you didn't create it with the PyDev project)

– File -> new -> source folder Browse and select the project (the name will be always 'src')

– Right click on the project -> new -> Source folder (you can select the name of the folder)

Page 7: CLTL python course: Object Oriented Programming (3/3)

Creating a Python package

● Python package:

– A special folder where group python modules

– There is a special module __init__.py

– It will be seen as a whole package

● How to create packages:

– File -> new -> python package. Select source folder and name

– Right click on the project / source folder -> new python package

● How to nest packages:

– Drag/drop on the pydev package explorer

– By the “dotted” name --> root.nested

Page 8: CLTL python course: Object Oriented Programming (3/3)

Creating a Python module

● Python module: a python file that can contain functions, classes...

● In PyDev a python module can be associated to:

– A source folder

– A python package

● How to create modules:

– File -> new -> python module. Select source folder (leave package empty) or package, module name and template

– Right click on source folder/package on explorer -> new python module

● Pydev modules templates:

– Empty: just an empty file

– Class: contains the basic definition for a class

– Main: contains the basic definition of a main python script (you can run it)

Page 9: CLTL python course: Object Oriented Programming (3/3)

How to run a module in PyDev

● Option 1

– Select the module and make it active

– Run --> Run As --> Python run● Option 2

– Right click on the module on the explorer

– Run --> Run as --> python run

● Advanced:

– Breakpoints, debugging...

Page 10: CLTL python course: Object Oriented Programming (3/3)

Assignment

We want to develop some python packages for different projects of the CLTL group, so we should create a nice structure of packages and modules.

● We have only a main group (CLTL)

● There are several projects going on: newsreader, opener, semantics_of_history, each one could be a package

● In each project we will developed different tools...

● Each tool will process different data, different files...

● ....

Page 11: CLTL python course: Object Oriented Programming (3/3)

Assignment

● Define a project called my_cltl_project with a source folder

● This package structure

● Cltl (pkg)

– Newsreader (pkg)

– Opener (pkg)

– semantics_of_history (pkg)

● coreference_tool (pkg)– metrics: B3.py, MUC.py (mods)– data: coref_set.py (mods)– io: coref_set_reader.py, coref_set_writer.py,

csv_reader.py (mods)– Common (pkg)

● time_functions (mod)

Page 12: CLTL python course: Object Oriented Programming (3/3)

Assignment

● In time_functions.py implement a function called get_time() that returns a string with the current time (check python module time)

● In csv_reader.py implement a function read_csv(path) that takes as input the path of a file, which is CSV comma separated, and prints each line reversed

a,b,c,d,e --> e,d,c,b,a

1,ruben,2,canito --> canito,2,ruben,1

(check split and list slicing and indexing)● Created another project

– with a main python module at least

– Import the required stuff from the project cltl and call to the functions get_time() and read_csv(path)