Skip to content

Commit 32960b1

Browse files
heikkitoivonencodex
andcommitted
Update: stdlib docs
Co-Authored-By: Codex <codex@openai.com>
1 parent 166c5f6 commit 32960b1

27 files changed

Lines changed: 391 additions & 178 deletions

DOCUMENTATION_STATUS.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ This document tracks the coverage of built-in functions, types, and standard lib
55
## Overview
66

77
- **Total Items**: 317 (150 builtins + 167 stdlib modules)
8-
- **Documented**: 387 (174 builtins + 213 stdlib)
9-
- **Coverage**: 122.1%
8+
- **Documented**: 389 (174 builtins + 215 stdlib)
9+
- **Coverage**: 122.7%
1010

1111
**Note**: Coverage exceeds 100% because comprehensive documentation files (like `exceptions.md`) cover multiple individual items, and we document deprecated/removed modules for historical reference.
1212

@@ -203,7 +203,7 @@ Complete coverage of all built-in functions, types, exceptions, and constants:
203203

204204
## Standard Library Modules
205205

206-
**Coverage: 127.5% (213/167)**
206+
**Coverage: 128.7% (215/167)**
207207

208208
All standard library modules are fully documented, including new Python 3.14 modules. Coverage exceeds 100% due to documentation of deprecated/removed modules for historical reference.
209209

@@ -224,6 +224,7 @@ All standard library modules are fully documented, including new Python 3.14 mod
224224
-`imp` - Import machinery (deprecated)
225225
-`plistlib` - Property list format
226226
-`sndhdr` - Sound file format detection
227+
-`select` - I/O multiplexing
227228

228229
### ⊘ Excluded Items (4 - intentionally out of scope)
229230

@@ -305,11 +306,11 @@ All file and I/O modules now documented:
305306
-`shutil` - High-level file operations
306307
-`tempfile` - Temporary files
307308

308-
### ✅ ALL MODULES COMPLETE (213/213)
309+
### ✅ ALL MODULES COMPLETE (215/215)
309310

310-
All 110 previously undocumented stdlib modules have been added:
311+
All 112 previously undocumented stdlib modules have been added:
311312

312-
**Utilities & System (20)**
313+
**Utilities & System (22)**
313314
-`ast` - Abstract syntax trees
314315
-`dis` - Disassembler for bytecode
315316
-`doctest` - Testing via docstrings
@@ -327,8 +328,10 @@ All 110 previously undocumented stdlib modules have been added:
327328
-`pydoc` - Documentation viewer
328329
-`runpy` - Run Python modules
329330
-`sched` - Event scheduler
331+
-`select` - I/O multiplexing
330332
-`stat` - File status constants
331333
-`sysconfig` - System configuration
334+
-`syslog` - System logger
332335
-`trace` - Trace execution
333336

334337
**String & Text Processing (4)**

data/documentation_audit.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@
168168
},
169169
"stdlib": {
170170
"total": 167,
171-
"documented": 213,
172-
"coverage_percent": 127.5,
171+
"documented": 215,
172+
"coverage_percent": 128.7,
173173
"missing": [
174174
"audit_documentation",
175175
"cProfile",
@@ -179,7 +179,7 @@
179179
},
180180
"summary": {
181181
"total_items": 317,
182-
"total_documented": 387,
183-
"overall_coverage_percent": 122.1
182+
"total_documented": 389,
183+
"overall_coverage_percent": 122.7
184184
}
185185
}

docs/stdlib/secrets.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ def generate_secure_password(length=12):
126126

127127
```python
128128
import secrets
129+
from datetime import datetime, timedelta
129130

130131
# Generate tokens for authentication - O(n)
131132
def create_session_token():
@@ -151,11 +152,12 @@ session = {
151152
import secrets
152153

153154
def lottery_selection(participants, winners=5):
154-
# O(1) per selection
155+
# O(1) per selection (swap with last element, then pop)
155156
selected = []
156157
for _ in range(min(winners, len(participants))):
157158
idx = secrets.randbelow(len(participants))
158-
selected.append(participants.pop(idx))
159+
participants[idx], participants[-1] = participants[-1], participants[idx]
160+
selected.append(participants.pop())
159161
return selected
160162

161163
participants = list(range(100))

docs/stdlib/select.md

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# select Module
2+
3+
The `select` module provides low-level I/O multiplexing primitives that wait for file
4+
descriptors to become ready for reading or writing.
5+
6+
## Complexity Reference
7+
8+
| Operation | Time | Space | Notes |
9+
|-----------|------|-------|-------|
10+
| `select.select()` | O(n) | O(n) | n = total fds passed; scans all sets |
11+
| `poll.register()` / `poll.unregister()` / `poll.modify()` | O(1) avg | O(1) | Per fd; Python bookkeeping + OS registration |
12+
| `poll.poll()` | O(n) | O(k) | n = registered fds, k = ready fds |
13+
| `epoll.register()` / `epoll.unregister()` / `epoll.modify()` | O(1) avg | O(1) | Per fd; OS-dependent |
14+
| `epoll.poll()` | O(k) typical | O(k) | k = ready fds; OS-dependent |
15+
| `kqueue.control()` | O(m + k) | O(k) | m = changelist length; k = ready events |
16+
17+
!!! warning "Platform availability"
18+
`poll`, `epoll`, and `kqueue` are not available on all platforms.
19+
Always guard with `hasattr(select, "poll")`, `hasattr(select, "epoll")`,
20+
or `hasattr(select, "kqueue")`.
21+
22+
## Waiting with select()
23+
24+
```python
25+
import select
26+
import socket
27+
28+
# Create a connected socket pair (cross-platform in Python 3.5+)
29+
left, right = socket.socketpair()
30+
31+
# Send data so right becomes readable
32+
left.sendall(b"ping")
33+
34+
# Wait for readability - O(n) where n = len(rlist) + len(wlist) + len(xlist)
35+
rlist, wlist, xlist = select.select([right], [], [], 1.0)
36+
if rlist:
37+
data = right.recv(4096)
38+
print(data) # b'ping'
39+
40+
left.close()
41+
right.close()
42+
```
43+
44+
## Polling with poll()
45+
46+
```python
47+
import select
48+
import socket
49+
50+
if hasattr(select, "poll"):
51+
left, right = socket.socketpair()
52+
poller = select.poll()
53+
54+
# Register for read events - O(1) average
55+
poller.register(right, select.POLLIN)
56+
57+
left.sendall(b"hello")
58+
59+
# Wait for events - O(n) for n registered fds
60+
events = poller.poll(1000) # timeout in ms
61+
for fd, event in events: # O(k) for k ready
62+
if event & select.POLLIN:
63+
print(right.recv(4096)) # b'hello'
64+
65+
poller.unregister(right) # O(1) average
66+
left.close()
67+
right.close()
68+
```
69+
70+
## Using epoll (Linux)
71+
72+
```python
73+
import select
74+
import socket
75+
76+
if hasattr(select, "epoll"):
77+
left, right = socket.socketpair()
78+
ep = select.epoll()
79+
80+
# Register read interest - O(1) average
81+
ep.register(right, select.EPOLLIN)
82+
83+
left.sendall(b"event")
84+
85+
# Wait for ready fds - typically O(k)
86+
events = ep.poll(1.0)
87+
for fd, event in events:
88+
if event & select.EPOLLIN:
89+
print(right.recv(4096)) # b'event'
90+
91+
ep.unregister(right) # O(1) average
92+
ep.close()
93+
left.close()
94+
right.close()
95+
```
96+
97+
## Using kqueue (BSD/macOS)
98+
99+
```python
100+
import select
101+
import socket
102+
103+
if hasattr(select, "kqueue"):
104+
left, right = socket.socketpair()
105+
kq = select.kqueue()
106+
107+
# Register a read event - O(1) average
108+
kev = select.kevent(right, filter=select.KQ_FILTER_READ, flags=select.KQ_EV_ADD)
109+
kq.control([kev], 0, 0)
110+
111+
left.sendall(b"kq")
112+
113+
# Wait for events - O(m + k) where m = changelist length
114+
events = kq.control([], 1, 1.0)
115+
if events:
116+
print(right.recv(4096)) # b'kq'
117+
118+
kq.close()
119+
left.close()
120+
right.close()
121+
```
122+
123+
## Notes and Best Practices
124+
125+
!!! warning "select() scalability limits"
126+
`select.select()` must scan all fds each call and may be limited by the
127+
platform's FD set size. For large numbers of connections, prefer `selectors`
128+
or an `epoll`/`kqueue`-based approach.
129+
130+
## Related Modules
131+
132+
- [selectors Module](selectors.md)
133+
- [socket Module](socket.md)
134+
- [asyncio Module](asyncio.md)

docs/stdlib/selectors.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,10 @@ def connect_many(hosts_ports):
254254
sel.register(sock, selectors.EVENT_WRITE, data=(host, port))
255255
sockets.append(sock)
256256

257-
# Wait for connections: O(n log n)
258-
# n = number of sockets
257+
# Wait for connections: O(n) for select, O(k) for epoll/kqueue
258+
# n = registered sockets, k = ready sockets
259259
while True:
260-
events = sel.select() # O(n log n)
260+
events = sel.select() # O(n) for select, O(k) for epoll/kqueue
261261

262262
if not events:
263263
break
@@ -298,9 +298,9 @@ def main():
298298
with open('file.txt') as f:
299299
sel.register(f, selectors.EVENT_READ) # O(1)
300300

301-
# Event loop: O(n log n) where n = files
301+
# Event loop: O(n) for select, O(k) for epoll/kqueue
302302
while True:
303-
events = sel.select() # O(n log n)
303+
events = sel.select() # O(n) for select, O(k) for epoll/kqueue
304304

305305
for key, mask in events: # O(k) ready
306306
if key.fileobj == sys.stdin:

0 commit comments

Comments
 (0)