Python Modules
Definition
In Python, a .py file is referred to as a module.
Categories
There are four main categories of modules that can be imported:
- Built-in standard modules (also known as standard library). Execute
help('modules')to see all built-in modules. - Third-party open-source modules, which can be installed via
pip install <module-name>. - Custom modules.
Benefits
The primary advantage of using modules is improved code maintainability. Additionally, it eliminates the need to start from scratch when writing code. Once a module is created, it can be reused elsewhere, avoiding conflicts between function and variable names. Even with identical names, functions and variables can coexist in different modules. However, avoid naming conflicts with built-in functions.
To prevent naming conflicts, Python uses packages. If module names conflict with others, organize them under a top-level package name, such as mycompany. For example:
mycompany/
__init__.py
abc.py
xyz.py
Each package directory must contain an __init__.py file. This file can be empty or contain Python code. It serves as a module itself, named according to its parent package.
Avoid naming custom modules the same as built-in modules like
sys.py, which would prevent importing system modules.
Module Importing
import module
from module import something
from module.submodule import something as alias
from module.submodule import *
Note: When a module is imported, its code executes asif running another
.pyfile.
Creating Custom Modules
Creating a custom module is straightforward: simply create a .py file. This file becomes a module that can be imported into other programs.
Module Search Path
import sys
print(sys.path)
Python searches through the directories in order. As soon as a matching module is found, it's imported and searching stops. The first element in the list is the current directory, meaning custom modules are prioritized.
Installing and Using Third-Party Packages
Visit https://pypi.python.org/pypi for available packages.
Packages
Not covered yet.
Cross-Module Imports
Not covered yet.
Absolute vs Relative Imports
Not covered yet.
Serialization (JSON & Pickle)
Serialization refers to converting objects in memory into a storable or transmittable format.
In Python, this process is called pickling. The goal is to save the serialized data to disk or transmit it over networks. The reverse process is called unpickling.
Purpose of Serialization
- Persist custom objects in storage.
- Transfer objects between locations.
- Enhance program maintainability.
Pickle Module
Python provides the pickle module for serialization.
import pickle
obj = {'name': 'Alice', 'age': 25, 'hobby': 'reading'}
serialized = pickle.dumps(obj)
restored = pickle.loads(serialized)
print(restored)
JSON Module
JSON converts Python data types to strings and vice versa. It's language-agnostic and more compact than pickle.
import json
data = {'key': 'value'}
json_string = json.dumps(data)
restored = json.loads(json_string)
print(restored)
Differences Between JSON and Pickle
- JSON supports only basic data types (int, str, list, tuple, dict).
- Pickle supports all Python data types.
- JSON is cross-language compatible.
- Pickle is Python-specific and consumes more space.
Working with Files
When storing multiple serialized objects, use seperate lines:
import json
items = [{'name': 'item1'}, {'name': 'item2'}]
with open('data.json', 'w') as f:
for item in items:
f.write(json.dumps(item) + '\n')
with open('data.json', 'r') as f:
for line in f:
item = json.loads(line.strip())
print(item)
Additional JSON Options
import json
data = {'name': 'Bob', 'age': 30}
json_string = json.dumps(data, sort_keys=True, indent=2, ensure_ascii=False)
print(json_string)
Shelve Module
The shelve module provides persistent storage for Python objects using a dictionary-like interface.
import shelve
shelf = shelve.open('my_data')
shelf['key'] = {'data': 'stored'}
shelf.close()
shelf = shelve.open('my_data')
retrieved = shelf['key']
shelf.close()
print(retrieved)
ConfigParser Module
Used for reading and writing configuration files.
import configparser
config = configparser.ConfigParser()
config['DEFAULT'] = {'ServerAliveInterval': '45'}
config['server.com'] = {'Host': '192.168.0.1'}
with open('config.ini', 'w') as configfile:
config.write(configfile)
Collections Module
Extends built-in data structures with additional types.
NamedTuple
from collections import namedtuple
Point = namedtuple('Point', ['x', 'y'])
p = Point(1, 2)
print(p.x, p.y)
Deque
from collections import deque
queue = deque([1, 2, 3])
queue.appendleft(0)
queue.append(4)
print(queue)
OrderedDict
from collections import OrderedDict
ordered = OrderedDict([('a', 1), ('b', 2)])
print(ordered)
DefaultDict
from collections import defaultdict
dd = defaultdict(list)
dd['key'].append('value')
print(dd)
Counter
from collections import Counter
counter = Counter('abracadabra')
print(counter)
Random Module
import random
print(random.random()) # Float between 0 and 1
print(random.randint(1, 10)) # Integer between 1 and 10
print(random.choice(['a', 'b'])) # Random element
Time Module
Timestamps
import time
timestamp = time.time()
print(timestamp)
Formatted Strings
formatted = time.strftime('%Y-%m-%d %H:%M:%S')
print(formatted)
Structured Time
import time
struct = time.localtime()
print(struct)
OS Module
Interface for interacting with the operating system.
import os
print(os.getcwd()) # Current working directory
os.mkdir('new_dir') # Create directory
os.listdir('.') # List contents
Sys Module
Interface for interacting with the Python interpreter.
import sys
print(sys.argv) # Command-line arguments
print(sys.version) # Python version