-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy path_sample_module.py
More file actions
107 lines (84 loc) · 2.86 KB
/
_sample_module.py
File metadata and controls
107 lines (84 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"""
Sample module for demonstrating autodoc functionality.
This module provides a simple database connection class and utility functions
for demonstrating how API documentation is generated with sphinx.ext.autodoc.
"""
class DatabaseConnection:
"""
Manages connections to a database.
This class provides methods for connecting to, querying, and disconnecting
from a database. It's designed to demonstrate autodoc's ability to generate
documentation from Python docstrings.
:param host: The database host address
:type host: str
:param port: The port number (default: 9042)
:type port: int
:param username: Optional username for authentication
:type username: str or None
"""
def __init__(self, host, port=9042, username=None):
"""Initialize a new database connection."""
self.host = host
self.port = port
self.username = username
self._connected = False
def connect(self):
"""
Establish a connection to the database.
:returns: True if connection was successful
:rtype: bool
:raises ConnectionError: If connection fails
"""
self._connected = True
return True
def execute(self, query):
"""
Execute a query on the database.
:param query: The query string to execute
:type query: str
:returns: Query results
:rtype: list
:raises RuntimeError: If not connected to database
"""
if not self._connected:
raise RuntimeError("Not connected to database")
return []
def disconnect(self):
"""Close the database connection."""
self._connected = False
def format_query(query, indent=4):
"""
Format a database query for better readability.
This function takes a query string and formats it with proper indentation
and line breaks for improved readability in logs or documentation.
:param query: The query string to format
:type query: str
:param indent: Number of spaces for indentation (default: 4)
:type indent: int
:returns: The formatted query string
:rtype: str
Example:
>>> format_query("SELECT * FROM users")
'SELECT * FROM users'
"""
return query.strip()
def validate_connection_params(host, port):
"""
Validate database connection parameters.
:param host: The database host address
:type host: str
:param port: The port number
:type port: int
:returns: True if parameters are valid, False otherwise
:rtype: bool
"""
if not host or not isinstance(host, str):
return False
if not isinstance(port, int) or port < 1 or port > 65535:
return False
return True
# Module-level constants
DEFAULT_TIMEOUT = 30
"""Default connection timeout in seconds"""
MAX_RETRIES = 3
"""Maximum number of connection retry attempts"""