As the number of classes increases, it becomes essential to organize them effectively for accessibility and maintainability. Creating a well-structured codebase is crucial for good code.
Modular programming involves dividing a large programming task into smaller, manageable subtasks or modules. This approach offers several advantages:
In Python, modules are simple files. If two files are in the same folder, you can load a class from one module into another for use.
Packages are folders that contain modules. They help to organize projects by grouping related modules together, preventing a cluttered structure. To create a package, include a file named __init__.py
in each folder that should be treated as a package. This file can be empty but must exist.
Consider a Point
class defined in a file named point_docstrings.py
. To use this class in another file, you can import it:
import point_docstrings
p1 = point_docstrings.Point()
When you import point_docstrings
into another program, point_docstrings
becomes part of the calling program’s namespace. The namespace includes all methods and classes available to the current program.
To import only a specific class from a module, use the from
keyword:
from point_docstrings import Point
p1 = Point()
If a class with the same name already exists in the current namespace, you can use the as
clause to rename the imported class:
from Pointdocstrings import Point as Pt
p1 = Pt()
To import multiple classes from a module, list them after the import
keyword:
from Pointdocstrings import Point, Line
To import all classes from a module, use the *
wildcard:
from Pointdocstrings import *
parent_directory/
├── main.py
├── Drawing/
│ ├── __init__.py
│ ├── point_call.py
│ └── point_docstrings.py
└── Maths/
├── __init__.py
└── Theorem.py
# Method 1
import Drawing.point_call
P1 = Drawing.point_call.Point()
# Method 2
from Drawing.point_call import Point
P1 = Point()
# Method 3
from Drawing import point_call
P1 = point_call.Point()
.
refers to the current directory (package)...
refers to the parent directory (package).If Point-call.py
wants to call Point-docstrings.py
within the same Drawing
package, you can use a relative import:
from .Point_docstrings import Point
If Theorem.py
in the Maths
package wants to call Point-docstrings.py
in the Drawing
package, you can use:
from ..Drawing.Point_docstrings import Point
Python has a vast collection of libraries available on the Python Package Index (PyPI): https://pypi.org/
Use the matplotlib
library (https://pypi.org/project/matplotlib/) to create a line chart visualizing monthly sales data.
Instructions:
matplotlib.pyplot
module.['January', 'February', 'March', 'April', 'May', 'June']
[2500, 2700, 3000, 2800, 3200, 3500]
plt.plot()
to draw the line chart.plt.show()
.Use the faker
library (https://pypi.org/project/Faker/) to generate synthetic customer data.
Instructions:
Faker
library.customers.csv
.Find a library that helps you read the contents of a PDF file and extract its text.
Instructions: