Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Module 7 - Modules and File handling

Uploaded by

merryfil.adolfo
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Module 7 - Modules and File handling

Uploaded by

merryfil.adolfo
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Modules and File

Handling

PPE - Module 7
In this module, you will learn about:

● importing and using Python modules;


● using some of the most useful Python standard library modules;
● constructing and using Python packages;
● PIP (Python Installation Package) and how to use it to install and uninstall
ready-to-use packages from PyPI.
● Working with file-system, directory tree and files;
● Selected Python Standard Library modules (math, and random)
What is a module?

a file containing Python definitions and statements, which can be later imported
and used when necessary.

Scenario:
We develop a program that uses the same functions throughout the
whole program.
Solution: module, so the program can use the functions defined in the module
once instead of defining the same function multiple times throughout the program.
Importing and using Python modules

To make a module usable, you must import it


Importing a module is done by a keyword ‘import’ followed by name of the module

Try to make use of dir() to know the functions of the module


Simulation [20]
‘math’ module functions

Trigonometry

● sin(x) → the sine of x;


● cos(x) → the cosine of x;
● tan(x) → the tangent of x.
● asin(x) → the arcsine of x;
● acos(x) → the arccosine of x;
● atan(x) → the arctangent of x.
● sinh(x) → the hyperbolic sine;
● cosh(x) → the hyperbolic cosine;
● tanh(x) → the hyperbolic tangent;
● asinh(x) → the hyperbolic arcsine;
● acosh(x) → the hyperbolic arccosine;
● atanh(x) → the hyperbolic arctangent.
‘math’ module functions (cont.)

exponentiation

● e → a constant with a value that is an approximation of Euler's number (e)


● exp(x) → finding the value of ex;
● log(x) → the natural logarithm of x
● log(x, b) → the logarithm of x to base b
● log10(x) → the decimal logarithm of x (more precise than log(x, 10))
● log2(x) → the binary logarithm of x (more precise than log(x, 2))
‘math’ module functions (cont.)

general-purpose functions

● ceil(x) → the ceiling of x (the smallest integer greater than or equal to x)


● floor(x) → the floor of x (the largest integer less than or equal to x)
● trunc(x) → the value of x truncated to an integer (be careful - it's not an
equivalent either of ceil or floor)
● factorial(x) → returns x! (x has to be an integral and not a negative)
● hypot(x, y) → returns the length of the hypotenuse of a right-angle triangle
with the leg lengths equal to x and y (the same as sqrt(pow(x, 2) + pow(y,
2)) but more precise)

Note: be aware of the differences between ceil(), floor(), and trunc()


‘random’ module functions

delivers some mechanisms allowing you to operate with pseudorandom numbers.

Note the prefix pseudo - the numbers generated by the modules may look random
in the sense that you cannot predict their subsequent values, but don't forget that
they all are calculated using very refined algorithms.

● seed()
● random()
● randrange()
● shuffle()
● choice()
● choices()
● sample()
More on Python modules

● https://docs.python.org/3/py-modindex.html
Simulation [21]
Problem Set [18]
Python Modules and Package

Writing your own modules doesn't differ much from writing ordinary scripts.

There are some specific aspects you must be aware of, but it definitely isn't rocket science. You'll see this soon
enough.

Let's summarize some important issues:

● a module is a kind of container filled with functions - you can pack as many functions as you want into one
module and distribute it across the world;
● of course, it's generally a good idea not to mix functions with different application areas within one module
(just like in a library - nobody expects scientific works to be put among comic books), so group your functions
carefully and name the module containing them in a clear and intuitive way (e.g., don't give the name
arcade_games to a module containing functions intended to partition and format hard disks)
● making many modules may cause a little mess - sooner or later you'll want to group your modules exactly in
the same way as you've previously grouped functions - is there a more general container than a module?
● yes, there is - it's a package; in the world of modules, a package plays a similar role to a folder/directory in
the world of files.
Simulation [22]
Python Modules and Package

Using packages made by other developers

PIP = pip installs package

Pypi.org

Pip install pygame


Simulation [23]
File Handling

stream = open(file, mode = 'r', encoding = None)

Modes :

r open mode: read

the file associated with the stream must exist and has to be readable, otherwise the
open() function raises an exception.

w open mode: write

the file associated with the stream doesn't need to exist; if it doesn't exist it will be
created; if it exists, it will be truncated to the length of zero (erased); if the creation
isn't possible (e.g., due to system permissions) the open() function raises an exception.
File Handling(cont.)

a open mode: append

the file associated with the stream doesn't need to exist; if it doesn't exist, it will be created; if it exists the
virtual recording head will be set at the end of the file (the previous content of the file remains untouched.)

r+ open mode: read and update

the file associated with the stream must exist and has to be writable, otherwise the open() function raises
an exception;

both read and write operations are allowed for the stream.

w+ open mode: write and update

the file associated with the stream doesn't need to exist; if it doesn't exist, it will be created; the previous
content of the file remains untouched;

both read and write operations are allowed for the stream.
File Handling(cont.)

t open mode: text

The file will be opened in normal text format

b open mode: binary

The file will be opened in binary format


Simulation [30]
Problem Set [19]

You might also like