Skip to content

Commit cd56997

Browse files
Manual review of builtin functions: ascii-eval
1 parent 5734cc3 commit cd56997

2 files changed

Lines changed: 0 additions & 189 deletions

File tree

docs/builtins/ascii.md

Lines changed: 0 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -125,84 +125,6 @@ print(ascii(path))
125125
# "'/home/\\u7528\\u6237/\\u6587\\u4ef6.txt'"
126126
```
127127

128-
## Comparison with Alternatives
129-
130-
### ascii() vs repr()
131-
132-
```python
133-
# Both O(n), but different escaping
134-
135-
text = "Café"
136-
137-
# repr() - shows non-ASCII directly (Python 3)
138-
repr(text) # "'Café'"
139-
140-
# ascii() - escapes all non-ASCII
141-
ascii(text) # "'Caf\\xe9'"
142-
143-
# Use case:
144-
# repr() - for Python evaluation
145-
# ascii() - for ASCII-only output (logs, APIs)
146-
```
147-
148-
### ascii() vs str()
149-
150-
```python
151-
text = "Hello\nWorld"
152-
153-
# str() - shows representation
154-
str(text) # Shows actual newline
155-
156-
# ascii() - escapes everything
157-
ascii(text) # "'Hello\\nWorld'"
158-
159-
# Use case:
160-
# str() - for human display
161-
# ascii() - for ASCII-safe output
162-
```
163-
164-
## Escape Sequence Details
165-
166-
```python
167-
# O(1) - understand escaping rules
168-
169-
# Control characters
170-
ascii("\n") # "'\\n'"
171-
ascii("\t") # "'\\t'"
172-
ascii("\r") # "'\\r'"
173-
ascii("\0") # "'\\x00'"
174-
175-
# Extended ASCII
176-
ascii("\x7f") # "'\\x7f'"
177-
ascii("\xff") # "'\\xff'"
178-
179-
# Unicode BMP (Basic Multilingual Plane)
180-
ascii("α") # "'\\u03b1'"
181-
ascii("") # "'\\u4e2d'"
182-
183-
# Supplementary planes
184-
ascii("𝔸") # "'\\U0001d538'"
185-
```
186-
187-
## Containers with Unicode
188-
189-
```python
190-
# O(n) - escapes all non-ASCII in container
191-
data = {
192-
"name": "Josée",
193-
"city": "Montréal",
194-
"country": "Québec"
195-
}
196-
197-
ascii(data)
198-
# {'name': 'Jos\\xe9e', 'city': 'Montr\\xe9al', 'country': 'Qu\\xe9bec'}
199-
200-
# Each value is escaped separately
201-
lst = ["café", "naïve", "résumé"]
202-
ascii(lst)
203-
# ['caf\\xe9', 'na\\xefve', 'r\\xe9sum\\xe9']
204-
```
205-
206128
## Performance Patterns
207129

208130
### Batch Processing
@@ -224,50 +146,6 @@ safe_version = ascii(large_text) # O(len(large_text))
224146
# Memory: output may be larger (each non-ASCII becomes \xXX, \uXXXX, etc)
225147
```
226148

227-
## Practical Examples
228-
229-
### API Response Logging
230-
231-
```python
232-
# O(n) - log safely without encoding issues
233-
import json
234-
235-
response = {"name": "José", "country": "México"}
236-
response_json = json.dumps(response)
237-
238-
# Log safely in ASCII-only environment
239-
print(ascii(response_json))
240-
# '{"name": "Jos\\u00e9", "country": "M\\u00e9xico"}'
241-
242-
# vs print(response_json) which might have encoding issues
243-
```
244-
245-
### Email Headers
246-
247-
```python
248-
# O(n) - headers must be ASCII
249-
subject = "Meeting: Café ☕ Discussion"
250-
251-
# Make safe for email
252-
safe_subject = ascii(subject)
253-
# "'Meeting: Caf\\xe9 \\u2615 Discussion'"
254-
255-
# Email system can safely handle this
256-
```
257-
258-
### Source Code Comments
259-
260-
```python
261-
# O(n) - include comments with non-ASCII
262-
263-
text = "Author: François"
264-
265-
# Generate comment safely
266-
comment = f"# {ascii(text)}"
267-
# print(comment)
268-
# # 'Author: Fran\\xe7ois'
269-
```
270-
271149
## Special Cases
272150

273151
### Empty String

docs/builtins/eval.md

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -92,33 +92,6 @@ scope = {f"var{i}": i for i in range(1000)}
9292
eval("var0 + var1 + var2", scope) # O(n + 1000)
9393
```
9494

95-
## Security Considerations
96-
97-
### Why eval() is Dangerous
98-
99-
```python
100-
# eval() executes arbitrary code
101-
user_input = "import os; os.system('rm -rf /')"
102-
# eval(user_input) # DANGEROUS - would execute!
103-
104-
# Only use eval() with trusted input
105-
# For untrusted input, use ast.literal_eval()
106-
import ast
107-
safe = ast.literal_eval("[1, 2, 3]") # Safe - only literals
108-
```
109-
110-
### Restricted Execution
111-
112-
```python
113-
# eval() with custom globals/locals
114-
restricted_scope = {"__builtins__": {}}
115-
# eval("import os", restricted_scope) # Still unsafe - can access builtins
116-
117-
# Use ast.literal_eval() for truly safe evaluation
118-
import ast
119-
result = ast.literal_eval("{'key': 'value'}") # Safe
120-
```
121-
12295
## Performance Patterns
12396

12497
### Repeated Evaluation
@@ -207,46 +180,6 @@ for x in range(10):
207180
result = eval(code) # Efficient
208181
```
209182

210-
## Common Patterns
211-
212-
### Dynamic Math Evaluation
213-
214-
```python
215-
# O(n) - parse and evaluate user expression
216-
def calculate(expr, **kwargs):
217-
# WARNING: Only use with trusted input!
218-
return eval(expr, {"__builtins__": {}}, kwargs)
219-
220-
# Safe because no builtins available
221-
result = calculate("x + y", x=10, y=20) # 30
222-
```
223-
224-
### Configuration Values
225-
226-
```python
227-
import ast
228-
229-
# O(n) - parse config safely
230-
config_str = '{"timeout": 30, "retries": 3, "items": [1, 2, 3]}'
231-
config = ast.literal_eval(config_str)
232-
# Safe - only allows data structures
233-
```
234-
235-
### Lambda/Function Creation (Avoid!)
236-
237-
```python
238-
# O(n) - can create functions at runtime
239-
# Generally a bad idea - use proper functions instead
240-
func = eval("lambda x: x ** 2")
241-
result = func(5) # 25
242-
243-
# Better - define function normally
244-
def square(x):
245-
return x ** 2
246-
247-
result = square(5)
248-
```
249-
250183
## Edge Cases
251184

252185
### Empty Expression

0 commit comments

Comments
 (0)