Python & Perl              Modules & Packages                     Vladimir Kulyukinwww.youtube.com/vkedco           www.vk...
Outline   ●   Traversing & Filtering Directories   ●   Python Modules & Packages   ●   Perl Modules & Packageswww.youtube....
OS.WALK()   ●   os.walk() is the workhorse of processing local file       systems   ●   This function uses generators to w...
Sample Problem       Write a PY script that filters all files in a       given directory by timestamps saving       only t...
PY Moduleswww.youtube.com/vkedco         www.vkedco.blogspot.com
Modules   ●   A PY module is a .py file   ●   A module may contain any syntactically       valid Python code   ●   Some PY...
Module Import Patterns      ●   import module      ●   import module1, module2, …, moduleN      ●   import module as anoth...
Telling Python Where to Look: Method 01   ●   Put your modules into one of the paths in       sys.path   ●   To find out t...
Telling Python Where to Look: Method 03   ●   There are standard directories specifically created for third-       party m...
Telling Python Where to Look: Method 02   ●   This method should be used when you have to put your modules into       a di...
Telling Python Where to Look: Method 03   ●   This method should be used when you have to put your       modules into a di...
What Python Loads   ●   When Python needs a modules byte code, it generates it       automatically   ●   Python looks for ...
PY Packageswww.youtube.com/vkedco          www.vkedco.blogspot.com
Package   ●   A package is a directory that contains a       set of modules (set can be empty)   ●   What makes a director...
MY_MATH Application   ●   Suppose I want to implement           a   package       my_math (this is a toy package)   ●   Su...
MY_MATH Applicationwww.youtube.com/vkedco   www.vkedco.blogspot.com
MY_Math Application Source Code                         source is herewww.youtube.com/vkedco           www.vkedco.blogspot...
PL Packages & Moduleswww.youtube.com/vkedco     www.vkedco.blogspot.com
PL Modules   ●   A PL package is a namespace   ●   A PL module is a package within a file   ●   Large collection of PL mod...
Module Use   ●   Modules are used with the use pragma   ●   When Perl sees the use pragam anywhere in       a program, it ...
Modifying @INC        unshift @INC, “/home/vladimir/code/”;        use lib “/home/vladimir/code/”;www.youtube.com/vkedco  ...
Reading & References  ●   http://docs.python.org/py3k/tutorial/modules.html  ●   www.cpan.orgwww.youtube.com/vkedco
Upcoming SlideShare
Loading in …5
×

Python & Perl: Traversing & Filtering Directories with OS.WALK(), Python Modules & Packages

927 views
841 views

Published on

Python & Perl: Traversing & Filtering Directories with OS.WALK(), Python Modules & Packages

Published in: Software
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
927
On SlideShare
0
From Embeds
0
Number of Embeds
294
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Python & Perl: Traversing & Filtering Directories with OS.WALK(), Python Modules & Packages

  1. 1. Python & Perl Modules & Packages Vladimir Kulyukinwww.youtube.com/vkedco www.vkedco.blogspot.com
  2. 2. Outline ● Traversing & Filtering Directories ● Python Modules & Packages ● Perl Modules & Packageswww.youtube.com/vkedco www.vkedco.blogspot.com
  3. 3. OS.WALK() ● os.walk() is the workhorse of processing local file systems ● This function uses generators to walk through a local file system import os ## given some directory, startdir, os.walk ## returns a generator object that yields triples ## of the form (path, dirlist, filelist) for path, dirlist, filelist in os.walk(startdir): ## path is current directory ## dirlist is list of subdirectories of path ## filelist is list of files in pathwww.youtube.com/vkedco www.vkedco.blogspot.com
  4. 4. Sample Problem Write a PY script that filters all files in a given directory by timestamps saving only those files that have been saved on or before a given time stamp. source is herewww.youtube.com/vkedco www.vkedco.blogspot.com
  5. 5. PY Moduleswww.youtube.com/vkedco www.vkedco.blogspot.com
  6. 6. Modules ● A PY module is a .py file ● A module may contain any syntactically valid Python code ● Some PY modules are not associated with .py files: – The sys module is not associated with any .py file – Modules written in other languageswww.youtube.com/vkedco www.vkedco.blogspot.com
  7. 7. Module Import Patterns ● import module ● import module1, module2, …, moduleN ● import module as another_name ● from module import name ● from module import name1, name2, …, nameN ● from module import name as another name ● from module import *www.youtube.com/vkedco www.vkedco.blogspot.com
  8. 8. Telling Python Where to Look: Method 01 ● Put your modules into one of the paths in sys.path ● To find out the current value of sys.path: >>> import sys >>> print sys.path >>> [/home/vladimir, /usr/bin, /usr/lib/python2.7, /usr/lib/python2.7/plat-linux2, /usr/lib/python2.7/lib- tk, /usr/lib/python2.7/lib-old, /usr/lib/python2.7/lib- dynload, /usr/local/lib/python2.7/dist-packages, /usr/lib/python2.7/dist-packages]www.youtube.com/vkedco www.vkedco.blogspot.com
  9. 9. Telling Python Where to Look: Method 03 ● There are standard directories specifically created for third- party modules: – C:Pyhon2.7site-packages (Windows) – /usr/local/lib/python2.7/dist-packages (Linux) ● Problems with Method 01: – You may not have permission to the standard directories – You may not want to clutter these directories – You want/have to put your modules in a different directorywww.youtube.com/vkedco www.vkedco.blogspot.com
  10. 10. Telling Python Where to Look: Method 02 ● This method should be used when you have to put your modules into a directory, not available in sys.path ● Set the name of the environment variable PYTHONPATH – Windows: Right click on Computer, Properties, Advanced System Settings, Environment Variables – Linux/Unix: Add this line to /etc/bash.bashrc (or similar file); on some Linux flavors, you may have to do this as superuser export PYTHONPATH=$PYTHONPATH:~/my_python_dirwww.youtube.com/vkedco www.vkedco.blogspot.com
  11. 11. Telling Python Where to Look: Method 03 ● This method should be used when you have to put your modules into a directory, not available in sys.path ● Create a custom loader file that adds a specific directory to sys.path import sys my_math_path = /home/vladimir/programming/python/code/my_math if __name__ == __main__: if not my_math_path in sys.path: sys.path.append(my_math_path)www.youtube.com/vkedco www.vkedco.blogspot.com
  12. 12. What Python Loads ● When Python needs a modules byte code, it generates it automatically ● Python looks for a file with the same name as the modules .py file that has the extension .pyo ● If there is no .pyo file or if it is older than the .py file, Python looks for a file with the extension .pyc ● If an up-to-date .pyc file is found, Python loads it ● If there is no up-to-date .pyc file, Python compiles and loads the .py filewww.youtube.com/vkedco www.vkedco.blogspot.com
  13. 13. PY Packageswww.youtube.com/vkedco www.vkedco.blogspot.com
  14. 14. Package ● A package is a directory that contains a set of modules (set can be empty) ● What makes a directory a package is the presence of the file __init__.py ● __init__.py defines the structure of a package ● __init__.py can be emptywww.youtube.com/vkedco www.vkedco.blogspot.com
  15. 15. MY_MATH Application ● Suppose I want to implement a package my_math (this is a toy package) ● Suppose the package will be implemented in /home/vladimir/programming/python/code/my_math ● The package has three sub-packages: – pack_add, pack_mult, & pack_subt ● For clarity, all module names start with pack and all modules start with modwww.youtube.com/vkedco www.vkedco.blogspot.com
  16. 16. MY_MATH Applicationwww.youtube.com/vkedco www.vkedco.blogspot.com
  17. 17. MY_Math Application Source Code source is herewww.youtube.com/vkedco www.vkedco.blogspot.com
  18. 18. PL Packages & Moduleswww.youtube.com/vkedco www.vkedco.blogspot.com
  19. 19. PL Modules ● A PL package is a namespace ● A PL module is a package within a file ● Large collection of PL modules at CPAN – Data::Dumper – File::Find – Getopt::Std – Win32::TieRegistrywww.youtube.com/vkedco www.vkedco.blogspot.com
  20. 20. Module Use ● Modules are used with the use pragma ● When Perl sees the use pragam anywhere in a program, it includes the module specified in the pragma before doing anything else in the program ● @INC is a list of search directories Perl uses to include moduleswww.youtube.com/vkedco www.vkedco.blogspot.com
  21. 21. Modifying @INC unshift @INC, “/home/vladimir/code/”; use lib “/home/vladimir/code/”;www.youtube.com/vkedco www.vkedco.blogspot.com
  22. 22. Reading & References ● http://docs.python.org/py3k/tutorial/modules.html ● www.cpan.orgwww.youtube.com/vkedco

×