File size: 17,243 Bytes
b104a01 | 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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | [
{
"topic": "python_functions",
"tags": [
"python",
"functions",
"def",
"arguments",
"parameters",
"return"
],
"content": "Python Functions:\n- Define with `def name(params):`, body indented 4 spaces\n- Parameters: positional args, keyword args (name=value), *args (variadic), **kwargs (keyword variadic)\n- Default values: `def f(x=10)`. NEVER use mutable defaults like `def f(x=[])`.\n- Return values: `return value`. No return = returns None.\n- Type hints: `def add(a: int, b: int) -> int:`. Optional.\n- Docstrings: triple-quoted string right after def line.\n- Lambda: `lambda x: x*2` for inline anonymous functions."
},
{
"topic": "python_classes",
"tags": [
"python",
"classes",
"oop",
"self",
"inheritance",
"methods"
],
"content": "Python Classes:\n- Define with `class Name:`. Methods take `self` as first param.\n- `__init__(self, ...)` is constructor.\n- Instance variables: `self.var = value` inside __init__.\n- Inheritance: `class Child(Parent):`. Call `super().__init__()` in child __init__.\n- `@property` decorator for computed attributes.\n- `@staticmethod` for methods that don't need self.\n- `@classmethod` with `cls` as first param for factory methods.\n- `__str__` for string representation, `__repr__` for debugging representation."
},
{
"topic": "python_imports",
"tags": [
"python",
"import",
"modules",
"packages",
"from"
],
"content": "Python Imports:\n- `import module` imports the whole module. Access with `module.name`.\n- `from module import name` imports specific name into current namespace.\n- `from module import *` imports all public names (avoid this).\n- `import module as alias` for shorter names.\n- `from module import name as alias` for renaming.\n- Packages: directory with `__init__.py` file. Example: `from package.module import name`.\n- Common imports: `import os`, `import sys`, `import json`, `import re`, `import math`."
},
{
"topic": "python_exceptions",
"tags": [
"python",
"exceptions",
"error",
"try",
"except",
"finally",
"raise"
],
"content": "Python Error Handling:\n- `try:` block contains code that might raise an exception.\n- `except ExceptionType as e:` catches specific exception types.\n- `except Exception as e:` catches (almost) all exceptions.\n- `else:` runs if no exception occurred.\n- `finally:` ALWAYS runs (cleanup code).\n- `raise ValueError(\"message\")` to raise an exception.\n- Common exceptions: ValueError, TypeError, KeyError, IndexError, FileNotFoundError, ZeroDivisionError.\n- Context managers: `with open(path) as f:` auto-closes file even on error."
},
{
"topic": "python_strings",
"tags": [
"python",
"strings",
"format",
"f-strings",
"methods",
"split",
"join"
],
"content": "Python String Operations:\n- f-strings: `f\"Hello {name}\"` for string formatting.\n- `.format()`: `\"Hello {}\".format(name)`.\n- `.split(sep)`: split into list by separator.\n- `.join(iterable)`: join list into string with separator.\n- `.strip()`, `.lstrip()`, `.rstrip()`: remove whitespace.\n- `.lower()`, `.upper()`, `.title()`, `.capitalize()`: case changes.\n- `.replace(old, new)`: replace substrings.\n- `.startswith(prefix)`, `.endswith(suffix)`: check prefixes/suffixes.\n- `.find(sub)`, `.index(sub)`: find substring position.\n- Slice: `s[start:end:step]`. Reverse: `s[::-1]`."
},
{
"topic": "python_lists",
"tags": [
"python",
"lists",
"list",
"array",
"comprehension",
"append",
"slice"
],
"content": "Python Lists:\n- Create: `[1, 2, 3]` or `list(range(5))`.\n- `.append(item)`: add single item to end.\n- `.extend(iterable)`: add multiple items.\n- `.insert(index, item)`: insert at position.\n- `.pop(index)`: remove and return item at index (last if no index).\n- `.remove(value)`: remove first occurrence of value.\n- `.sort()`: sort in place. `.sort(reverse=True)` for descending.\n- `sorted(list)`: returns new sorted list.\n- List comprehension: `[x*2 for x in range(10) if x % 2 == 0]`.\n- Slice: `lst[start:end:step]`. Copy: `lst[:]`.\n- `len(lst)`, `min(lst)`, `max(lst)`, `sum(lst)`."
},
{
"topic": "python_dicts",
"tags": [
"python",
"dictionary",
"dict",
"hashmap",
"keys",
"values",
"items"
],
"content": "Python Dictionaries:\n- Create: `{\"key\": \"value\"}` or `dict(a=1, b=2)`.\n- Access: `d[\"key\"]`. Use `d.get(\"key\", default)` to avoid KeyError.\n- Set: `d[\"key\"] = value`.\n- `.keys()`, `.values()`, `.items()` for views.\n- `for k, v in d.items():` for iteration.\n- `key in d` to check existence.\n- `.pop(key)`, `.popitem()` to remove.\n- `.update(other_dict)` to merge.\n- Dict comprehension: `{x: x**2 for x in range(5)}`.\n- `defaultdict` from collections for default values.\n- `Counter` from collections for counting."
},
{
"topic": "algorithm_sorting",
"tags": [
"algorithm",
"sort",
"sorting",
"bubble",
"merge",
"quick",
"binary search"
],
"content": "Common Sorting Algorithms:\n- Built-in: `sorted(list)` or `list.sort()`. Uses Timsort (O(n log n)).\n- `sorted(list, key=lambda x: x[1])` for custom sort key.\n- `sorted(list, reverse=True)` for descending.\n- Binary search: `bisect_left(list, x)` from bisect module.\n- Sort by multiple criteria: `sorted(list, key=lambda x: (x.age, x.name))`.\n- Partial sort: `heapq.nsmallest(k, list)` or `heapq.nlargest(k, list)`.\n- Finding min/max with conditions: `min(list, key=func)`."
},
{
"topic": "algorithm_recursion",
"tags": [
"algorithm",
"recursion",
"recursive",
"base case",
"stack",
"memoization"
],
"content": "Recursion:\n- A function that calls itself with smaller inputs.\n- Must have a BASE CASE to stop recursion (e.g., n <= 1).\n- Must make PROGRESS toward base case each call.\n- Stack limit: Python has ~1000 recursion depth limit.\n- Memoization: cache results with `@functools.lru_cache(maxsize=None)`.\n- Examples: factorial, fibonacci, tree traversal, divide-and-conquer.\n- Iterative alternative: use a stack (list) to simulate recursion.\n- Tail recursion: Python does NOT optimize tail recursion."
},
{
"topic": "algorithm_search",
"tags": [
"algorithm",
"search",
"linear",
"binary",
"bfs",
"dfs",
"graph"
],
"content": "Search Algorithms:\n- Linear search: check each element O(n). Works on unsorted data.\n- Binary search: divide-and-conquer on sorted data O(log n). Use bisect module.\n- BFS (Breadth-First Search): uses queue, finds shortest path in unweighted graph.\n- DFS (Depth-First Search): uses stack or recursion, good for exhaustive search.\n- Dijkstra: shortest path in weighted graph. Use `heapq` for priority queue.\n- A*: heuristic-guided search for pathfinding.\n- Graph representation: adjacency list `{node: [neighbors]}` or matrix."
},
{
"topic": "testing_basics",
"tags": [
"testing",
"pytest",
"unittest",
"assert",
"test",
"tdd"
],
"content": "Python Testing:\n- Use pytest: `pip install pytest`. Run with `pytest` command.\n- Test files named `test_*.py` or `*_test.py`.\n- Test functions named `test_*`.\n- Use `assert` for checks: `assert add(2, 3) == 5`.\n- pytest discovers tests automatically.\n- Fixtures: use `@pytest.fixture` for setup/teardown.\n- `pytest.raises(ExceptionType)` for testing exceptions.\n- `pytest.mark.parametrize` for running same test with multiple inputs.\n- Coverage: `pytest --cov=module` to check code coverage.\n- Test categories: normal cases, edge cases, error cases, boundary values."
},
{
"topic": "testing_patterns",
"tags": [
"testing",
"mock",
"patch",
"fixture",
"integration",
"unit"
],
"content": "Testing Patterns:\n- Unit test: tests a single function/method in isolation.\n- Integration test: tests multiple components together.\n- Mock: `unittest.mock.patch` or `pytest-mock` to replace dependencies.\n- Arrange-Act-Assert: set up data, execute code, verify result.\n- Given-When-Then: BDD style of describing tests.\n- Test doubles: Dummy, Stub, Spy, Mock, Fake.\n- Coverage metrics: line coverage, branch coverage, path coverage.\n- Property-based testing: `hypothesis` library generates random inputs.\n- Snapshot testing: compare output to saved reference."
},
{
"topic": "code_quality_linting",
"tags": [
"code quality",
"lint",
"linter",
"pylint",
"flake8",
"formatting"
],
"content": "Code Quality:\n- Linting: automated checking for errors and style issues.\n- flake8: checks PEP 8 style, syntax errors, complexity.\n- pylint: more thorough checking, generates quality score.\n- black: opinionated code formatter (auto-fixes formatting).\n- isort: sorts imports alphabetically.\n- mypy: static type checker for type hints.\n- ruff: fast Rust-based linter and formatter (combines flake8+isort+more).\n- pre-commit: runs checks before git commit.\n- PEP 8: 79-char lines, 4-space indent, snake_case functions, CamelCase classes."
},
{
"topic": "git_basics",
"tags": [
"git",
"version control",
"commit",
"branch",
"merge",
"clone"
],
"content": "Git Basics:\n- `git init`: initialize new repository.\n- `git clone <url>`: copy remote repository.\n- `git add <file>`: stage changes. `git add .` for all changes.\n- `git commit -m \"message\"`: save staged changes.\n- `git status`: see current state of working directory.\n- `git log`: view commit history. `git log --oneline` for compact view.\n- `git diff`: see unstaged changes. `git diff --staged` for staged changes.\n- `git branch <name>`: create branch. `git checkout <name>`: switch branch.\n- `git merge <branch>`: merge branch into current branch.\n- `git push`: upload commits to remote. `git pull`: download and merge."
},
{
"topic": "file_io",
"tags": [
"python",
"file",
"io",
"read",
"write",
"csv",
"json",
"open"
],
"content": "Python File I/O:\n- Read: `with open(path) as f: content = f.read()`.\n- Read lines: `with open(path) as f: lines = f.readlines()`.\n- Write: `with open(path, 'w') as f: f.write(content)`.\n- Append: `with open(path, 'a') as f: f.write(content)`.\n- CSV: `import csv`. `csv.reader(f)` to read. `csv.writer(f)` to write.\n- JSON: `json.load(f)` to read. `json.dump(data, f)` to write. `json.dumps(obj)` to string.\n- Binary: `open(path, 'rb')` / `open(path, 'wb')`.\n- Path: use `os.path.join(dir, file)` for cross-platform paths.\n- Directory: `os.makedirs(path, exist_ok=True)`. `os.listdir(path)`.\n- Check exists: `os.path.exists(path)`. `os.path.isfile(path)`."
},
{
"topic": "security_basics",
"tags": [
"security",
"injection",
"validation",
"xss",
"sql",
"sanitize"
],
"content": "Security Best Practices:\n- Input validation: NEVER trust user input. Validate type, length, format.\n- SQL injection: use parameterized queries, never string concatenation.\n- XSS (Cross-Site Scripting): escape output in HTML context.\n- eval() is DANGEROUS: use `ast.literal_eval()` for safe evaluation.\n- exec() is DANGEROUS: avoid entirely. Use restricted environments if necessary.\n- Password hashing: use bcrypt, argon2, or hashlib with salt.\n- HTTPS: always use for network communication.\n- Secret management: use environment variables, never hardcode secrets.\n- Dependency scanning: check for known vulnerabilities (Dependabot).\n- Principle of least privilege: give minimum necessary permissions."
},
{
"topic": "security_sql_injection",
"tags": [
"security",
"sql",
"injection",
"parameterized",
"prepared"
],
"content": "SQL Injection Prevention:\n- BAD: `f\"SELECT * FROM users WHERE name = '{name}'\"` \u2014 allows injection.\n- GOOD: use parameterized queries: `cursor.execute(\"SELECT * FROM users WHERE name = ?\", (name,))`.\n- ORMs (SQLAlchemy, Django) auto-parameterize queries.\n- Escape special characters if you must build strings.\n- Use stored procedures for complex operations.\n- Validate input: reject unexpected characters.\n- Limit database permissions per application user.\n- Monitor for injection attempts in logs."
},
{
"topic": "design_patterns_creational",
"tags": [
"design patterns",
"singleton",
"factory",
"builder",
"prototype"
],
"content": "Creational Design Patterns:\n- Singleton: ensure class has exactly one instance. Module-level variable in Python.\n- Factory: factory function returns objects without specifying exact class.\n- Factory Method: subclass decides which class to instantiate.\n- Abstract Factory: create families of related objects.\n- Builder: separate construction from representation. Build step-by-step.\n- Prototype: clone existing objects instead of creating new ones.\n- Python-specific: use `__new__` for singleton. Use classmethods as factories."
},
{
"topic": "python_concurrency",
"tags": [
"python",
"concurrent",
"threading",
"async",
"multiprocessing",
"asyncio"
],
"content": "Python Concurrency:\n- Threading: `import threading`. Thread(target=func, args=(...)). GIL limits CPU parallelism.\n- Multiprocessing: `import multiprocessing`. Process(target=func). True parallelism.\n- asyncio: `async def`, `await`, `asyncio.run()`. Cooperative multitasking for I/O.\n- concurrent.futures: ThreadPoolExecutor for threads, ProcessPoolExecutor for processes.\n- Lock: prevent race conditions. `threading.Lock()`. Use `with lock:`.\n- Queue: thread-safe queue. `queue.Queue()`. For producer-consumer patterns.\n- Use asyncio for: web servers, API calls, file I/O operations.\n- Use multiprocessing for: CPU-heavy computations, data processing."
},
{
"topic": "web_requests",
"tags": [
"web",
"http",
"requests",
"api",
"rest",
"get",
"post"
],
"content": "HTTP Requests in Python:\n- `import requests`. Install: `pip install requests`.\n- GET: `r = requests.get(url, params={\"key\": \"value\"})`.\n- POST: `r = requests.post(url, json={\"key\": \"value\"})`.\n- Response: `r.status_code`, `r.json()`, `r.text`, `r.headers`.\n- Timeout: `requests.get(url, timeout=5)` to avoid hanging.\n- Error handling: `r.raise_for_status()` or check `r.status_code`.\n- Headers: `requests.get(url, headers={\"Authorization\": \"Bearer token\"})`.\n- Session: `s = requests.Session()` for persistent connections and cookies.\n- Authentication: `requests.get(url, auth=(\"user\", \"pass\"))`."
},
{
"topic": "data_structures_advanced",
"tags": [
"data structures",
"heap",
"queue",
"stack",
"tree",
"graph",
"hash"
],
"content": "Advanced Python Data Structures:\n- Heap (min): `import heapq`. `heapq.heappush(heap, item)`. `heapq.heappop(heap)`.\n- Heap (max): push negative values, or use custom key with wrapper.\n- Queue (FIFO): `from collections import deque`. `d.append()` / `d.popleft()`.\n- Stack (LIFO): use list. `.append()` / `.pop()`.\n- Priority Queue: `from queue import PriorityQueue` (thread-safe) or heapq.\n- Default dict: `from collections import defaultdict(lambda: 0)`.\n- Ordered dict: `from collections import OrderedDict` (Python 3.7+ dicts are ordered).\n- Named tuple: `from collections import namedtuple`. `Point = namedtuple('Point', ['x', 'y'])`.\n- ChainMap: combine multiple dicts.\n- Counter: `from collections import Counter`. `Counter(list).most_common(3)`."
},
{
"topic": "tool_call_format",
"tags": [
"tool",
"json",
"format",
"nanobot",
"api",
"function calling"
],
"content": "Tool Call JSON Format:\n- Write file: {\"tool\": \"fs\", \"action\": \"write\", \"path\": \"filename.py\", \"content\": \"code here\"}\n- Read file: {\"tool\": \"fs\", \"action\": \"read\", \"path\": \"filename.py\"}\n- List dir: {\"tool\": \"fs\", \"action\": \"list\", \"path\": \"directory\"}\n- Check exists: {\"tool\": \"fs\", \"action\": \"exists\", \"path\": \"filename.py\"}\n- Delete: {\"tool\": \"fs\", \"action\": \"delete\", \"path\": \"filename.py\"}\n- Create dir: {\"tool\": \"fs\", \"action\": \"mkdir\", \"path\": \"directory\"}\n- Compile check: {\"tool\": \"compile\", \"path\": \"filename.py\"}\n- Run tests: {\"tool\": \"test\", \"path\": \"directory\"}\n- Search code: {\"tool\": \"search\", \"query\": \"pattern\", \"path\": \".\", \"ext\": \".py\"}\n- Run command: {\"tool\": \"shell\", \"cmd\": \"command string\"}\n- Git init: {\"tool\": \"git\", \"action\": \"init\", \"path\": \".\"}\n- Git commit: {\"tool\": \"git\", \"action\": \"commit\", \"message\": \"commit msg\"}\n- Done: {\"tool\": \"done\"}\n\nAlways emit JSON on a single line. After the tool call, include THOUGHT: and RESULT: on new lines."
}
] |