
Python
Design enterprise-grade packages with sophisticated organization and clear interfaces.
Design packages for scalability and maintainability.
enterprise_app/
āāā __init__.py
āāā _version.py # Version management
āāā _config.py # Configuration
āāā api/ # Public API layer
ā āāā __init__.py
ā āāā client.py
āāā core/ # Core functionality (private)
ā āāā __init__.py
ā āāā models/
ā ā āāā __init__.py
ā ā āāā base.py
ā ā āāā user.py
ā ā āāā product.py
ā āāā services/
ā ā āāā __init__.py
ā ā āāā authentication.py
ā ā āāā database.py
ā āāā utils/
ā āāā __init__.py
ā āāā decorators.py
ā āāā validators.py
āāā exceptions/ # Custom exceptions
āāā __init__.py
āāā errors.pyenterprise_app/__init__.py:
"""Enterprise application package."""
from ._version import __version__
from .api.client import Client
from .exceptions import AppError
__all__ = ["Client", "AppError", "__version__"]
# Prevent import of internal modules
__private__ = ["core", "_config"]enterprise_app/api/__init__.py:
"""Public API."""
from .client import Client
__all__ = ["Client"]enterprise_app/api/client.py:
"""Public client API."""
from ..core.services.authentication import AuthService
from ..core.models.user import User
class Client:
"""Public-facing client."""
def __init__(self, api_key):
self.auth = AuthService(api_key)
def get_user(self, user_id):
return self.auth.authenticate()
# Use private coreapp/
āāā __init__.py
āāā core.py
āāā plugins/
ā āāā __init__.py
ā āāā base.py
ā āāā registry.py
āāā examples/
āāā plugin_a.py
āāā plugin_b.pyapp/plugins/base.py:
"""Base class for all plugins."""
from abc import ABC, abstractmethod
from typing import Any, Dict
class Plugin(ABC):
"""Base plugin interface."""
name: str = "Plugin"
version: str = "1.0.0"
@abstractmethod
def initialize(self, config: Dict[str, Any]) -> None:
"""Initialize plugin."""
pass
@abstractmethod
def execute(self) -> Any:
"""Execute plugin logic."""
pass
def cleanup(self) -> None:
"""Cleanup (optional)."""
passapp/plugins/registry.py:
"""Plugin registry and loader."""
import importlib
import sys
from pathlib import Path
from typing import Dict, Type
from .base import Plugin
class PluginRegistry:
"""Manage plugin lifecycle."""
def __init__(self):
self._plugins: Dict[str, Plugin] = {}
self._registry: Dict[str, Type[Plugin]] = {}
def register(self, name: str, plugin_class: Type[Plugin]) -> None:
"""Register plugin class."""
self._registry[name] = plugin_class
def load(self, name: str, config: Dict) -> Plugin:
"""Load and initialize plugin."""
if name not in self._registry:
raise ValueError(f"Plugin {name} not registered")
plugin_class = self._registry[name]
plugin = plugin_class()
plugin.initialize(config)
self._plugins[name] = plugin
return plugin
def execute(self, name: str) -> None:
"""Execute loaded plugin."""
if name not in self._plugins:
raise ValueError(f"Plugin {name} not loaded")
self._plugins[name].execute()
def discover_plugins(self, plugin_dir: str) -> None:
"""Auto-discover plugins."""
plugin_path = Path(plugin_dir)
sys.path.insert(0, str(plugin_path.parent))
for py_file in plugin_path.glob("*.py"):
if py_file.name.startswith("plugin_"):
module_name = py_file.stem
module = importlib.import_module(module_name)
# Find Plugin subclasses
for item_name in dir(module):
item = getattr(module, item_name)
if (isinstance(item, type) and
issubclass(item, Plugin) and
item is not Plugin):
self.register(module_name, item)# Company-wide namespace package structure
# /team_a/company/service_a/__init__.py
# /team_b/company/service_b/__init__.py
# /shared/company/core/__init__.py
# All contribute to "company" namespace!
# company/__init__.py is NOT created (namespace package)
# configuration.py - Setup script
import sys
from pathlib import Path
def setup_namespace_paths():
"""Configure namespace packages."""
paths = [
Path("/team_a"),
Path("/team_b"),
Path("/shared")
]
for path in paths:
if str(path) not in sys.path:
sys.path.insert(0, str(path))
# Usage
setup_namespace_paths()
# Now can import from all locations
from company.service_a import ServiceA
from company.service_b import ServiceB
from company.core import CoreUtilities# _version.py
__version__ = "2.1.0"
__version_info__ = (2, 1, 0)
VERSION = __version__
# compatibility.py
import sys
from . import _version
def check_python_version():
"""Ensure compatible Python version."""
if sys.version_info < (3, 8):
raise RuntimeError(
f"Package requires Python 3.8+, "
f"got {sys.version_info.major}.{sys.version_info.minor}"
)
def check_dependencies():
"""Verify required dependencies."""
required = {
"requests": "2.25.0",
"numpy": "1.20.0"
}
import importlib
missing = []
for package, min_version in required.items():
try:
module = importlib.import_module(package)
if not hasattr(module, "__version__"):
continue
# Simple version check
installed = module.__version__
if installed < min_version:
missing.append(f"{package}>={min_version}")
except ImportError:
missing.append(package)
if missing:
raise ImportError(
f"Missing required packages: {', '.join(missing)}"
)
# Run checks on import
check_python_version()
check_dependencies()# __init__.py - Carefully curate public API
from . import _version
from ._exceptions import (
APIError,
AuthenticationError,
ValidationError
)
from .api.client import Client
from .api.models import User, Product
# Version info
__version__ = _version.__version__
__version_info__ = _version.__version_info__
# Public API - what users should import
__all__ = [
"Client",
"User",
"Product",
"APIError",
"AuthenticationError",
"ValidationError",
]
# Hide internals
__private__ = ["core", "api", "_version"]
# Optional: freeze module
def __getattr__(name):
if name in __private__:
raise AttributeError(
f"'{name}' is internal, not part of public API"
)
raise AttributeError(f"module has no attribute '{name}'")# Deprecation with migration path
import warnings
from functools import wraps
def deprecated(alternative=None, removed_in=None):
"""Deprecation decorator with migration help."""
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
msg = f"{func.__name__} is deprecated"
if alternative:
msg += f", use {alternative} instead"
if removed_in:
msg += f", will be removed in {removed_in}"
warnings.warn(msg, DeprecationWarning, stacklevel=2)
return func(*args, **kwargs)
wrapper.__deprecated__ = True
wrapper.__alternative__ = alternative
return wrapper
return decorator
# Old API (kept for compatibility)
@deprecated(alternative="process_v2()", removed_in="3.0.0")
def process(data):
"""Deprecated: Use process_v2() instead."""
return process_v2(data)
# New API
def process_v2(data):
"""New processing function."""
return data
# Aliasing for compatibility
get_user = get_user_v2 # Old name points to new functionmypackage/
āāā __init__.py
āāā core.py
āāā README.md # Overview
āāā docs/
ā āāā index.rst
ā āāā guide/
ā ā āāā installation.rst
ā ā āāā quickstart.rst
ā ā āāā advanced.rst
ā āāā api/
ā āāā reference.rst
āāā CHANGELOG.md # Version historyInclude in __init__.py:
"""
MyPackage - Description of package.
Quick Start:
>>> from mypackage import Client
>>> client = Client()
>>> result = client.process(data)
Documentation:
Full documentation at: https://mypackage.readthedocs.io
Issues:
Report issues at: https://github.com/user/mypackage/issues
"""Ready to practice? Challenges | Quiz
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep ⢠Build Real ⢠Verify Skills ⢠Launch Forward