Skip to content

Commit 7150387

Browse files
committed
Improve code organization: docstrings, type hints, and imports
- Add module docstrings to cli.py, admin.py, logging.py, and hash.py - Add `from __future__ import annotations` for PEP 563 deferred evaluation - Modernize type hints using Python 3.10+ syntax (X | None instead of Optional[X]) - Use TYPE_CHECKING imports to avoid circular dependencies - Improve function docstrings to Google style with Args/Returns sections - Add type annotations to key public APIs in: - connection.py: Connection class and conn() function - fetch.py: Fetch and Fetch1 classes - expression.py: QueryExpression class properties and methods - utils.py: utility functions - hash.py: hashing functions - admin.py: kill() and kill_quick() functions
1 parent 8441793 commit 7150387

File tree

8 files changed

+677
-280
lines changed

8 files changed

+677
-280
lines changed

src/datajoint/admin.py

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,42 @@
1+
"""
2+
Administrative utilities for managing database connections.
3+
4+
This module provides functions for viewing and terminating database connections
5+
through the MySQL processlist interface.
6+
"""
7+
8+
from __future__ import annotations
9+
110
import logging
211

312
import pymysql
413

5-
from .connection import conn
14+
from .connection import Connection, conn
615

716
logger = logging.getLogger(__name__.split(".")[0])
817

918

10-
def kill(restriction=None, connection=None, order_by=None):
19+
def kill(
20+
restriction: str | None = None,
21+
connection: Connection | None = None,
22+
order_by: str | list[str] | None = None,
23+
) -> None:
1124
"""
12-
view and kill database connections.
25+
View and interactively kill database connections.
1326
14-
:param restriction: restriction to be applied to processlist
15-
:param connection: a datajoint.Connection object. Default calls datajoint.conn()
16-
:param order_by: order by a single attribute or the list of attributes. defaults to 'id'.
27+
Displays active database connections matching the optional restriction and
28+
prompts the user to select connections to terminate.
1729
18-
Restrictions are specified as strings and can involve any of the attributes of
19-
information_schema.processlist: ID, USER, HOST, DB, COMMAND, TIME, STATE, INFO.
30+
Args:
31+
restriction: SQL WHERE clause condition to filter the processlist.
32+
Can reference any column from information_schema.processlist:
33+
ID, USER, HOST, DB, COMMAND, TIME, STATE, INFO.
34+
connection: A datajoint.Connection object. If None, uses datajoint.conn().
35+
order_by: Column name(s) to sort results by. Defaults to 'id'.
2036
2137
Examples:
22-
dj.kill('HOST LIKE "%compute%"') lists only connections from hosts containing "compute".
23-
dj.kill('TIME > 600') lists only connections in their current state for more than 10 minutes
38+
>>> dj.kill('HOST LIKE "%compute%"') # connections from hosts containing "compute"
39+
>>> dj.kill('TIME > 600') # connections idle for more than 10 minutes
2440
"""
2541

2642
if connection is None:
@@ -59,18 +75,28 @@ def kill(restriction=None, connection=None, order_by=None):
5975
logger.warn("Process not found")
6076

6177

62-
def kill_quick(restriction=None, connection=None):
78+
def kill_quick(
79+
restriction: str | None = None,
80+
connection: Connection | None = None,
81+
) -> int:
6382
"""
64-
Kill database connections without prompting. Returns number of terminated connections.
83+
Kill database connections without prompting.
84+
85+
Terminates all database connections matching the optional restriction
86+
without user confirmation.
6587
66-
:param restriction: restriction to be applied to processlist
67-
:param connection: a datajoint.Connection object. Default calls datajoint.conn()
88+
Args:
89+
restriction: SQL WHERE clause condition to filter the processlist.
90+
Can reference any column from information_schema.processlist:
91+
ID, USER, HOST, DB, COMMAND, TIME, STATE, INFO.
92+
connection: A datajoint.Connection object. If None, uses datajoint.conn().
6893
69-
Restrictions are specified as strings and can involve any of the attributes of
70-
information_schema.processlist: ID, USER, HOST, DB, COMMAND, TIME, STATE, INFO.
94+
Returns:
95+
Number of connections terminated.
7196
7297
Examples:
73-
dj.kill('HOST LIKE "%compute%"') terminates connections from hosts containing "compute".
98+
>>> dj.kill_quick('HOST LIKE "%compute%"') # kill connections from "compute" hosts
99+
>>> dj.kill_quick('TIME > 600') # kill connections idle for more than 10 minutes
74100
"""
75101
if connection is None:
76102
connection = conn()

src/datajoint/cli.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,41 @@
1+
"""
2+
Command-line interface for DataJoint Python.
3+
4+
This module provides a console interface for interacting with DataJoint databases,
5+
allowing users to connect to servers and work with virtual modules from the command line.
6+
7+
Usage:
8+
datajoint [-u USER] [-p PASSWORD] [-h HOST] [-s SCHEMA:MODULE ...]
9+
10+
Example:
11+
datajoint -u root -h localhost -s mydb:experiment mydb:subject
12+
"""
13+
14+
from __future__ import annotations
15+
116
import argparse
217
from code import interact
318
from collections import ChainMap
19+
from typing import TYPE_CHECKING
20+
21+
if TYPE_CHECKING:
22+
from collections.abc import Sequence
423

524
import datajoint as dj
625

726

8-
def cli(args: list = None):
27+
def cli(args: Sequence[str] | None = None) -> None:
928
"""
10-
Console interface for DataJoint Python
29+
Console interface for DataJoint Python.
30+
31+
Launches an interactive Python shell with DataJoint configured and optional
32+
virtual modules loaded for database schemas.
33+
34+
Args:
35+
args: List of command-line arguments. If None, reads from sys.argv.
1136
12-
:param args: List of arguments to be passed in, defaults to reading stdin
13-
:type args: list, optional
37+
Raises:
38+
SystemExit: Always raised when the interactive session ends.
1439
"""
1540
parser = argparse.ArgumentParser(
1641
prog="datajoint",

0 commit comments

Comments
 (0)