Skip to content

Commit 4d22198

Browse files
heikkitoivonencodex
andcommitted
Docs: Fix stdlib json-logging
Co-Authored-By: Codex <codex@openai.com>
1 parent 92e2188 commit 4d22198

6 files changed

Lines changed: 61 additions & 69 deletions

File tree

docs/stdlib/json.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ The `json` module provides JSON serialization and deserialization.
77
| Function | Time | Space | Notes |
88
|----------|------|-------|-------|
99
| `json.dumps(obj)` | O(n) | O(n) | Serialize to string |
10-
| `json.dump(obj, fp)` | O(n) | O(k) | Write to file; k = buffer size (streams output) |
10+
| `json.dump(obj, fp)` | O(n) | O(d) | d = nesting depth; writes incrementally |
1111
| `json.loads(s)` | O(n) | O(n) | Parse JSON string |
12-
| `json.load(fp)` | O(n) | O(n) | Read from file |
12+
| `json.load(fp)` | O(n) | O(n) | Reads full file into memory |
1313
| `JSONEncoder.encode()` | O(n) | O(n) | Custom encoder |
1414
| `JSONDecoder.decode()` | O(n) | O(n) | Custom decoder |
15-
| `JSONDecodeError` | - | - | Exception for parse errors (subclass of ValueError) |
1615

1716
## Serialization (dumps/dump)
1817

@@ -89,7 +88,7 @@ data = {'key': 'value', 'items': list(range(1000))}
8988
with open('data.json', 'w') as f:
9089
json.dump(data, f) # O(n) serialization + write
9190

92-
# Streaming: O(1) memory, O(n) time
91+
# Streaming: O(d) extra memory, O(n) time (object itself still in memory)
9392
# Writes to file incrementally
9493
```
9594

@@ -216,10 +215,7 @@ json_str = json.dumps(person, default=custom_serializer) # O(n)
216215

217216
## Version Notes
218217

219-
- **Python 2.6+**: Basic json module
220-
- **Python 3.4+**: Better performance
221-
- **Python 3.6+**: Preserves key order (dicts ordered)
222-
- **Python 3.9+**: Performance improvements
218+
- **Python 3.x**: `json` module is available
223219

224220
## Common Issues
225221

docs/stdlib/keyword.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ The `keyword` module provides access to Python's set of keywords and allows chec
66

77
| Operation | Time | Space | Notes |
88
|-----------|------|-------|-------|
9-
| `keyword.iskeyword()` | O(1) | O(1) | Hash-based lookup |
10-
| Access keyword list | O(1) | O(1) | Pre-computed |
9+
| `keyword.iskeyword()` | O(n) | O(1) | n = string length |
10+
| Access keyword list | O(1) | O(1) | Pre-computed list |
1111

1212
## Checking for Keywords
1313

docs/stdlib/linecache.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ The `linecache` module allows efficient retrieval of individual lines from Pytho
99
| `getline()` first access | O(n) | O(n) | Loads entire file into cache; n = file lines |
1010
| `getline()` cached | O(1) | O(1) | Returns cached line |
1111
| `checkcache()` | O(k) | O(1) | k = cached files; checks mtime for invalidation |
12-
| `clearcache()` | O(1) | O(1) | Clear all cached files |
12+
| `clearcache()` | O(k) | O(1) | k = cached files; clears entries |
1313

1414
## Getting Lines from Files
1515

@@ -34,11 +34,11 @@ for i in range(1, 4):
3434
```python
3535
import linecache
3636

37-
# Clear cache - O(1)
38-
linecache.checkcache()
37+
# Clear entire cache - O(k)
38+
linecache.clearcache()
3939

40-
# Clear specific file - O(1)
41-
linecache.checkcache('myfile.py')
40+
# Invalidate cache entries if files changed
41+
linecache.checkcache()
4242
```
4343

4444
## Related Documentation

docs/stdlib/locale.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ The `locale` module provides access to the POSIX locale database for language, c
77
| Operation | Time | Space | Notes |
88
|-----------|------|-------|-------|
99
| `getlocale()` | O(1) | O(1) | Get current locale |
10-
| `setlocale(category, locale)` | O(1) | O(1) | Set locale |
10+
| `setlocale(category, locale)` | Varies | Varies | Depends on platform/C library |
1111
| `localeconv()` | O(1) | O(1) | Get locale info |
12-
| `format(format, value)` | O(n) | O(n) | n = value length |
12+
| `format_string(format, value)` | O(n) | O(n) | n = formatted output size |
1313
| `strxfrm(string)` | O(n) | O(n) | Transform string |
14-
| `strcoll(s1, s2)` | O(n) | O(n) | Compare locale-aware |
14+
| `strcoll(s1, s2)` | O(n) | O(1) | Compare locale-aware |
1515

1616
## Common Operations
1717

@@ -58,7 +58,7 @@ import locale
5858
# Set German locale - O(1)
5959
locale.setlocale(locale.LC_NUMERIC, 'de_DE.UTF-8')
6060

61-
# O(n) format number - n = value length
61+
# O(n) format number - n = formatted output size
6262
value = 1234567.89
6363
formatted = locale.format_string("%.2f", value)
6464
# Returns: "1.234.567,89"
@@ -173,12 +173,12 @@ formatted = cache.format_number(1234.56, "%.2f") # O(1)
173173
```python
174174
import locale
175175

176-
# Bad: Multiple setlocale calls - O(k) per call
176+
# Bad: Multiple setlocale calls - varies by platform
177177
for i in range(100):
178-
locale.setlocale(locale.LC_NUMERIC, 'de_DE.UTF-8') # O(1)
178+
locale.setlocale(locale.LC_NUMERIC, 'de_DE.UTF-8')
179179
value = locale.format_string("%.2f", i)
180180

181-
# Good: Set once - O(1) setup
181+
# Good: Set once (avoids repeated C library calls)
182182
locale.setlocale(locale.LC_NUMERIC, 'de_DE.UTF-8')
183183
for i in range(100):
184184
value = locale.format_string("%.2f", i) # O(n)
@@ -211,10 +211,8 @@ def efficient_sort(strings):
211211

212212
## Version Notes
213213

214-
- **Python 2.6+**: Basic locale support
215-
- **Python 3.x**: Full locale support
216-
- **Platform-specific**: Available on Unix/Linux and Windows
217-
- **Encoding**: UTF-8 recommended for modern systems
214+
- **Python 3.x**: `locale` module is available
215+
- **Platform-specific**: Behavior depends on the underlying C library and locale data
218216

219217
## Related Documentation
220218

docs/stdlib/logging.md

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ The `logging` module provides functionality for flexible event logging, with dif
66

77
| Operation | Time | Space | Notes |
88
|-----------|------|-------|-------|
9-
| `logging.basicConfig()` | O(1) | O(1) | Configure logging |
10-
| `logger.debug/info/warning/error()` | O(k) | O(1) | k = message length |
9+
| `logging.basicConfig()` | Varies | Varies | Creates handlers/formatters |
10+
| `logger.debug/info/warning/error()` | Varies | Varies | Depends on handlers, filters, and I/O |
1111
| `getLogger()` | O(1) avg | O(1) | Get logger instance |
1212
| Formatting message | O(k) | O(k) | k = formatted string |
1313

@@ -18,13 +18,13 @@ The `logging` module provides functionality for flexible event logging, with dif
1818
```python
1919
import logging
2020

21-
# Configure logging - O(1)
21+
# Configure logging - cost depends on handlers
2222
logging.basicConfig(
2323
level=logging.INFO,
2424
format='%(asctime)s - %(levelname)s - %(message)s'
2525
)
2626

27-
# Log messages - O(k)
27+
# Log messages - cost depends on handlers and I/O
2828
logging.debug('Debug message') # Not shown (level=INFO)
2929
logging.info('Info message') # O(10)
3030
logging.warning('Warning message') # O(15)
@@ -42,7 +42,7 @@ logger = logging.getLogger(__name__)
4242
# Set level - O(1)
4343
logger.setLevel(logging.DEBUG)
4444

45-
# Log messages - O(k)
45+
# Log messages - cost depends on handlers and I/O
4646
logger.debug('Debug: %s', variable) # O(k)
4747
logger.info('Processing: %s', item) # O(k)
4848
logger.warning('Issue: %s', issue) # O(k)
@@ -73,12 +73,12 @@ formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(messag
7373
file_handler.setFormatter(formatter)
7474
console_handler.setFormatter(formatter)
7575

76-
# Add handlers - O(1) each
76+
# Add handlers - O(1) each (list append)
7777
logger.addHandler(file_handler) # O(1)
7878
logger.addHandler(console_handler) # O(1)
7979

80-
# Log - O(k)
81-
logger.info('Application started') # O(k)
80+
# Log - cost depends on handlers and I/O
81+
logger.info('Application started')
8282
```
8383

8484
## Log Levels
@@ -100,7 +100,7 @@ logger.setLevel(logging.WARNING)
100100

101101
# Only WARNING and above are logged
102102
logger.debug('Not logged') # Skipped
103-
logger.warning('Is logged') # O(k)
103+
logger.warning('Is logged')
104104
```
105105

106106
## Common Patterns
@@ -121,16 +121,16 @@ logger = logging.getLogger(__name__)
121121

122122
def process_file(filename):
123123
try:
124-
logger.info(f'Processing {filename}') # O(k)
124+
logger.info(f'Processing {filename}')
125125
with open(filename) as f:
126126
data = f.read() # O(n)
127-
logger.debug(f'Read {len(data)} bytes') # O(k)
127+
logger.debug(f'Read {len(data)} bytes')
128128
return process(data)
129129
except FileNotFoundError:
130-
logger.error(f'File not found: {filename}') # O(k)
130+
logger.error(f'File not found: {filename}')
131131
return None
132132
except Exception as e:
133-
logger.exception(f'Unexpected error: {e}') # O(k)
133+
logger.exception(f'Unexpected error: {e}')
134134
return None
135135
```
136136

@@ -144,9 +144,9 @@ logger = logging.getLogger(__name__)
144144
try:
145145
result = risky_operation() # Might fail
146146
except ValueError as e:
147-
logger.error(f'Invalid value: {e}') # O(k)
147+
logger.error(f'Invalid value: {e}')
148148
except Exception as e:
149-
logger.exception('Unexpected error') # O(k) - includes traceback
149+
logger.exception('Unexpected error') # Includes traceback
150150
```
151151

152152
## Performance Considerations
@@ -172,14 +172,14 @@ logger.debug(f'User {username} logged in') # Always format string
172172
```python
173173
import logging
174174

175-
# File handler - O(k) to write
175+
# File handler - cost depends on I/O and buffering
176176
handler = logging.FileHandler('app.log')
177177

178178
# Buffering affects performance
179179
# Default: buffered (fast)
180180
# Unbuffered: flush each write (slow but safer)
181181

182-
# Rotating file handler - O(k) per write
182+
# Rotating file handler - cost depends on I/O and rollover
183183
rotating = logging.handlers.RotatingFileHandler(
184184
'app.log',
185185
maxBytes=1000000, # 1MB
@@ -189,9 +189,7 @@ rotating = logging.handlers.RotatingFileHandler(
189189

190190
## Version Notes
191191

192-
- **Python 2.x**: logging module available
193-
- **Python 3.x**: Enhanced with better formatting
194-
- **All versions**: O(k) logging complexity
192+
- **Python 3.x**: `logging` module is available
195193

196194
## Related Modules
197195

docs/stdlib/lzma.md

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ The `lzma` module provides XZ compression and decompression functionality.
66

77
| Operation | Time | Space | Notes |
88
|-----------|------|-------|-------|
9-
| `lzma.open(filename)` | O(1) | O(1) | Open file handle |
10-
| `LZMAFile.read()` | O(m) | O(m) | Decompress all, m = uncompressed size |
9+
| `lzma.open(filename)` | Varies | Varies | Depends on file I/O and options |
10+
| `LZMAFile.read()` | O(m) | O(m) | Full read, m = uncompressed size |
1111
| `LZMAFile.write(data)` | O(n) | O(k) | Compress and write, n = input size |
12-
| `compress(data)` | O(n) | O(n) | Compress bytes (higher constant than gzip) |
13-
| `decompress(data)` | O(m) | O(m) | Decompress bytes |
12+
| `compress(data)` | O(n) | O(n) | One-shot compression |
13+
| `decompress(data)` | O(m) | O(m) | One-shot decompression |
1414

1515
## Opening Files
1616

@@ -19,9 +19,8 @@ The `lzma` module provides XZ compression and decompression functionality.
1919
```python
2020
import lzma
2121

22-
# Opening LZMA file: O(1) time
23-
# Just opens file handle
24-
f = lzma.open('file.xz', 'rb') # O(1)
22+
# Opening LZMA file: cost depends on I/O and options
23+
f = lzma.open('file.xz', 'rb')
2524

2625
# With context manager
2726
with lzma.open('file.xz', 'rb') as f:
@@ -34,7 +33,7 @@ with lzma.open('file.xz', 'rb') as f:
3433
import lzma
3534

3635
# Just file handle, minimal memory
37-
f = lzma.open('file.xz', 'rb') # O(1) space
36+
f = lzma.open('file.xz', 'rb')
3837
```
3938

4039
## Reading (Decompression)
@@ -147,13 +146,15 @@ data = lzma.decompress(compressed) # O(m)
147146
```python
148147
import lzma
149148

150-
# Streaming compression: O(n) time (high constant), O(k) space
149+
# Streaming compression: O(n) time, O(k) space
151150
compressor = lzma.LZMACompressor()
152151

153-
result = b''
152+
# Avoid quadratic behavior by collecting chunks
153+
parts = []
154154
for chunk in data_chunks:
155-
result += compressor.compress(chunk) # O(n) total
156-
result += compressor.flush() # Finalize
155+
parts.append(compressor.compress(chunk))
156+
parts.append(compressor.flush()) # Finalize
157+
result = b''.join(parts)
157158

158159
# Memory: large dictionary buffer, not entire data
159160
```
@@ -166,9 +167,11 @@ import lzma
166167
# Streaming decompression: O(m) time, O(k) space
167168
decompressor = lzma.LZMADecompressor()
168169

169-
result = b''
170+
# Avoid quadratic behavior by collecting chunks
171+
parts = []
170172
for chunk in compressed_chunks:
171-
result += decompressor.decompress(chunk) # O(m) total
173+
parts.append(decompressor.decompress(chunk))
174+
result = b''.join(parts)
172175

173176
# Memory: decompression buffer
174177
```
@@ -201,17 +204,17 @@ import lzma
201204

202205
data = large_data
203206

204-
# Fast: preset 0 (still slower than gzip)
207+
# Fast: preset 0 (still slower than gzip in many cases)
205208
compressed = lzma.compress(data, preset=0) # O(n)
206-
# Size reduction: ~30%
209+
# Size reduction depends on input data
207210

208211
# Default: preset 6
209212
compressed = lzma.compress(data, preset=6) # O(n)
210-
# Size reduction: ~50%
213+
# Size reduction depends on input data
211214

212215
# Maximum: preset 9
213216
compressed = lzma.compress(data, preset=9) # O(n) with high constant
214-
# Size reduction: ~55% (best ratio)
217+
# Size reduction depends on input data
215218
```
216219

217220
## Streaming Decompression
@@ -362,7 +365,7 @@ bz2_result = bz2.compress(data)
362365

363366
# LZMA (XZ): Slow, best compression
364367
# Time: O(n) with highest constant
365-
# Ratio: ~55%
368+
# Ratio depends on input data
366369
lzma_result = lzma.compress(data)
367370
```
368371

@@ -415,10 +418,7 @@ with lzma.open('huge.xz', 'rt') as f:
415418

416419
## Version Notes
417420

418-
- **Python 3.3+**: LZMA module introduced
419-
- **Python 3.4+**: Enhanced performance
420-
- **Python 3.6+**: Performance improvements
421-
- **Python 3.9+**: Better compression options
421+
- **Python 3.x**: `lzma` module is available
422422

423423
## Related Documentation
424424

0 commit comments

Comments
 (0)