fastcore 2.2.26__tar.gz → 2.2.28__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {fastcore-2.2.26/fastcore.egg-info → fastcore-2.2.28}/PKG-INFO +3 -1
- {fastcore-2.2.26 → fastcore-2.2.28}/README.md +2 -0
- {fastcore-2.2.26 → fastcore-2.2.28}/fastcore/__init__.py +2 -2
- {fastcore-2.2.26 → fastcore-2.2.28}/fastcore/aio.py +15 -3
- {fastcore-2.2.26 → fastcore-2.2.28}/fastcore/apisurface.py +10 -3
- {fastcore-2.2.26 → fastcore-2.2.28}/fastcore/docments.py +2 -2
- fastcore-2.2.28/fastcore/editskill.py +85 -0
- {fastcore-2.2.26 → fastcore-2.2.28}/fastcore/foundation.py +10 -4
- {fastcore-2.2.26 → fastcore-2.2.28}/fastcore/meta.py +11 -7
- {fastcore-2.2.26 → fastcore-2.2.28}/fastcore/nbio.py +2 -4
- {fastcore-2.2.26 → fastcore-2.2.28}/fastcore/parallel.py +3 -1
- {fastcore-2.2.26 → fastcore-2.2.28}/fastcore/script.py +30 -14
- {fastcore-2.2.26 → fastcore-2.2.28}/fastcore/tools.py +8 -4
- {fastcore-2.2.26 → fastcore-2.2.28}/fastcore/xml.py +14 -2
- {fastcore-2.2.26 → fastcore-2.2.28}/fastcore/xtras.py +33 -7
- {fastcore-2.2.26 → fastcore-2.2.28/fastcore.egg-info}/PKG-INFO +3 -1
- fastcore-2.2.26/fastcore/editskill.py +0 -67
- {fastcore-2.2.26 → fastcore-2.2.28}/CONTRIBUTING.md +0 -0
- {fastcore-2.2.26 → fastcore-2.2.28}/LICENSE +0 -0
- {fastcore-2.2.26 → fastcore-2.2.28}/fastcore/_modidx.py +0 -0
- {fastcore-2.2.26 → fastcore-2.2.28}/fastcore/all.py +0 -0
- {fastcore-2.2.26 → fastcore-2.2.28}/fastcore/ansi.py +0 -0
- {fastcore-2.2.26 → fastcore-2.2.28}/fastcore/basics.py +0 -0
- {fastcore-2.2.26 → fastcore-2.2.28}/fastcore/dispatch.py +0 -0
- {fastcore-2.2.26 → fastcore-2.2.28}/fastcore/docscrape.py +0 -0
- {fastcore-2.2.26 → fastcore-2.2.28}/fastcore/funccall.py +0 -0
- {fastcore-2.2.26 → fastcore-2.2.28}/fastcore/imghdr.py +0 -0
- {fastcore-2.2.26 → fastcore-2.2.28}/fastcore/imports.py +0 -0
- {fastcore-2.2.26 → fastcore-2.2.28}/fastcore/nb_imports.py +0 -0
- {fastcore-2.2.26 → fastcore-2.2.28}/fastcore/net.py +0 -0
- {fastcore-2.2.26 → fastcore-2.2.28}/fastcore/py2pyi.py +0 -0
- {fastcore-2.2.26 → fastcore-2.2.28}/fastcore/shutil.py +0 -0
- {fastcore-2.2.26 → fastcore-2.2.28}/fastcore/style.py +0 -0
- {fastcore-2.2.26 → fastcore-2.2.28}/fastcore/test.py +0 -0
- {fastcore-2.2.26 → fastcore-2.2.28}/fastcore/transform.py +0 -0
- {fastcore-2.2.26 → fastcore-2.2.28}/fastcore/utils.py +0 -0
- {fastcore-2.2.26 → fastcore-2.2.28}/fastcore/xdg.py +0 -0
- {fastcore-2.2.26 → fastcore-2.2.28}/fastcore.egg-info/SOURCES.txt +0 -0
- {fastcore-2.2.26 → fastcore-2.2.28}/fastcore.egg-info/dependency_links.txt +0 -0
- {fastcore-2.2.26 → fastcore-2.2.28}/fastcore.egg-info/entry_points.txt +0 -0
- {fastcore-2.2.26 → fastcore-2.2.28}/fastcore.egg-info/requires.txt +0 -0
- {fastcore-2.2.26 → fastcore-2.2.28}/fastcore.egg-info/top_level.txt +0 -0
- {fastcore-2.2.26 → fastcore-2.2.28}/pyproject.toml +0 -0
- {fastcore-2.2.26 → fastcore-2.2.28}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: fastcore
|
|
3
|
-
Version: 2.2.
|
|
3
|
+
Version: 2.2.28
|
|
4
4
|
Summary: Python supercharged for fastai development
|
|
5
5
|
Author-email: Jeremy Howard and Sylvain Gugger <infos@fast.ai>
|
|
6
6
|
License: Apache-2.0
|
|
@@ -88,6 +88,8 @@ To install fastcore run: `conda install fastcore -c fastai` (if you use Anaconda
|
|
|
88
88
|
|
|
89
89
|
To get started, we recommend you read through [the fastcore tour](https://fastcore.fast.ai/tour.html).
|
|
90
90
|
|
|
91
|
+
[`globtastic`](https://fastcore.fast.ai/xtras.html#globtastic) supports filename filters (`file_glob`, `file_re`) and full-path filters (`path_glob`, `path_re`). Glob filters use `fnmatch`: `*` matches directory separators and `**` has no special meaning.
|
|
92
|
+
|
|
91
93
|
## Contributing
|
|
92
94
|
|
|
93
95
|
After you clone this repository, please run `nbdev_install_hooks` in your terminal. This sets up git hooks, which clean up the notebooks to remove the extraneous stuff stored in the notebooks (e.g. which cells you ran) which causes unnecessary merge conflicts.
|
|
@@ -57,6 +57,8 @@ To install fastcore run: `conda install fastcore -c fastai` (if you use Anaconda
|
|
|
57
57
|
|
|
58
58
|
To get started, we recommend you read through [the fastcore tour](https://fastcore.fast.ai/tour.html).
|
|
59
59
|
|
|
60
|
+
[`globtastic`](https://fastcore.fast.ai/xtras.html#globtastic) supports filename filters (`file_glob`, `file_re`) and full-path filters (`path_glob`, `path_re`). Glob filters use `fnmatch`: `*` matches directory separators and `**` has no special meaning.
|
|
61
|
+
|
|
60
62
|
## Contributing
|
|
61
63
|
|
|
62
64
|
After you clone this repository, please run `nbdev_install_hooks` in your terminal. This sets up git hooks, which clean up the notebooks to remove the extraneous stuff stored in the notebooks (e.g. which cells you ran) which causes unnecessary merge conflicts.
|
|
@@ -38,7 +38,7 @@ test_eq(x.unique(), [1,2,3,4,5])
|
|
|
38
38
|
Modules:
|
|
39
39
|
|
|
40
40
|
- `fastcore.aio`: Bridging async and sync code: `run_sync`, `iter_sync`, `ctx_sync`, `athreaded`, `maybe_await`, and `then`, plus `Debounce` for coalescing bursts of calls
|
|
41
|
-
- `fastcore.apisurface`:
|
|
41
|
+
- `fastcore.apisurface`: Signatures, documentation and grouped namespaces for generated API clients
|
|
42
42
|
- `fastcore.basics`: Basic functionality used in the fastai library
|
|
43
43
|
- `fastcore.docments`: Document parameters using comments.
|
|
44
44
|
- `fastcore.editskill`: Text, file, cell, and notebook editing from `fastcore.tools` and `fastcore.nbio`, plus the conventions the whole fastai editing toolkit follows. Read this before working with the editing tools in any package that shares them.
|
|
@@ -55,4 +55,4 @@ Modules:
|
|
|
55
55
|
- `fastcore.xml`: Concise HTML generation and namespace-aware XML construction.
|
|
56
56
|
- `fastcore.xtras`: Utility functions used in the fastai library"""
|
|
57
57
|
|
|
58
|
-
__version__ = "2.2.
|
|
58
|
+
__version__ = "2.2.28"
|
|
@@ -2,13 +2,25 @@
|
|
|
2
2
|
|
|
3
3
|
## Calling async code from sync code
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Use these helpers to call async libraries without making your own code async:
|
|
6
|
+
|
|
7
|
+
- `run_sync` runs a coroutine and returns its result.
|
|
8
|
+
- `iter_sync` iterates an async generator.
|
|
9
|
+
- `ctx_sync` uses an async context manager in a `with` block.
|
|
10
|
+
|
|
11
|
+
They share an event loop on a background daemon thread, started on first use.
|
|
6
12
|
|
|
7
13
|
## Calling sync code from async code
|
|
8
14
|
|
|
9
|
-
`athreaded`
|
|
15
|
+
`athreaded` turns a blocking function into an async function that runs its body in a worker thread. This lets async code use blocking libraries without stalling the event loop.
|
|
16
|
+
|
|
17
|
+
`await maybe_await(o)` awaits `o` if it is awaitable and returns it unchanged otherwise.
|
|
18
|
+
|
|
19
|
+
`then(x, *fs)` applies each function to the preceding result. It returns a plain value when all steps are synchronous, or an awaitable if `x` or any step is awaitable.
|
|
20
|
+
|
|
21
|
+
This is useful for clients with both sync and async modes. A method can use `return then(self.gists.get(gid), ~Self.files.values(), first)` in either mode, without duplicating its body.
|
|
10
22
|
|
|
11
|
-
`then`
|
|
23
|
+
`then` awaits every awaitable returned by a step. Steps cannot pass awaitables through as data.
|
|
12
24
|
|
|
13
25
|
Docs: https://fastcore.fast.ai/aio.html.md"""
|
|
14
26
|
|
|
@@ -1,8 +1,15 @@
|
|
|
1
|
-
"""
|
|
1
|
+
"""Signatures, documentation and grouped namespaces for generated API clients
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
`apisurface` builds signatures, docstrings and grouped namespaces from API operation metadata. Generated clients can use it to support tab completion and documentation through `?` and `doc()`. `fastspec` uses it for HTTP clients generated from specifications.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
An operation record can be any object with these attributes:
|
|
6
|
+
|
|
7
|
+
- `name`, `summary` and `docs_url` describe the operation.
|
|
8
|
+
- `group` gives its namespace as a string, or a list for nested groups.
|
|
9
|
+
- `params` lists parameter names in order. `required_params` identifies required parameters.
|
|
10
|
+
- `param_types`, `param_defaults` and `param_docs` map parameter names to their metadata.
|
|
11
|
+
|
|
12
|
+
`mk_sig` creates an `inspect.Signature` with Python parameter names. `mk_doc` creates a docstring. `OpGroup` and `mk_groups` organize operations into namespaces with attribute access. `full_docs` combines their overviews into Markdown.
|
|
6
13
|
|
|
7
14
|
Docs: https://fastcore.fast.ai/apisurface.html.md"""
|
|
8
15
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"""Document parameters using comments.
|
|
2
2
|
|
|
3
|
-
A
|
|
3
|
+
A docment is a comment beside a parameter in a function signature. It keeps the parameter's description next to its name, type and default. `docments(f)` reads these comments for documentation tools and CLIs such as `fastcore.script`:
|
|
4
4
|
|
|
5
5
|
```python
|
|
6
6
|
def add(
|
|
@@ -12,7 +12,7 @@ def add(
|
|
|
12
12
|
test_eq(docments(add), {'a': 'The first operand', 'b': 'The second operand', 'return': 'The sum'})
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
For longer descriptions, put comment lines above the parameter. `docments` also reads `typing.Annotated` string metadata and NumPy-style docstrings. Pass `full=True` to retrieve each parameter's default, type and docment together.
|
|
16
16
|
|
|
17
17
|
Docs: https://fastcore.fast.ai/docments.html.md"""
|
|
18
18
|
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"""Text, file, cell, and notebook editing from `fastcore.tools` and `fastcore.nbio`, plus the conventions the whole fastai editing toolkit follows. Read this before working with the editing tools in any package that shares them.
|
|
2
|
+
|
|
3
|
+
`from fastcore.editskill import *` loads the fastcore editing layer: the text primitives and file tools of `fastcore.tools`, and the notebook I/O and cell editors of `fastcore.nbio`. When installed, prefer `exhash` for hash-verified editing, `rgapi` for search, and `aidialog` or `dialoghelper` for dialogs.
|
|
4
|
+
|
|
5
|
+
## Places
|
|
6
|
+
|
|
7
|
+
The tools edit strings in memory, files, notebook cells, and notebooks. File tools take a path. Cell tools take `path, cell_id` and edit that cell's source. `Notebook` and `NbCell` hold notebooks and cells in memory.
|
|
8
|
+
|
|
9
|
+
A Solveit dialog is an `.ipynb` whose cells represent notes, runnable code, and prompt/reply pairs. Cell tools work with notebook structure, regardless of which application produced the file. `aidialog.dlgskill` and `dialoghelper` provide tools for reading and editing dialog messages.
|
|
10
|
+
|
|
11
|
+
`open_doc` (in exhash) parses a file (`fname=`, or a `Path` as `src`), URL (an `https?://` str), or text (any other str) into a `Section` tree, with sections taken from Markdown headings, tree-sitter definitions in code, or md-heading cells in a notebook. A section points at a span of lines in a file, or a run of cells in a notebook. To change what a section contains, edit those lines or cells with the file and cell tools.
|
|
12
|
+
|
|
13
|
+
## Naming
|
|
14
|
+
|
|
15
|
+
Function names follow these conventions:
|
|
16
|
+
|
|
17
|
+
- Operations on files, notebooks, cells, and messages use `verb_target`: `view_file`, `create_file`, `read_nb`, `write_nb`, `view_cell`, `validate_nb`, `view_msg`, and `view_dlg`. This includes `lnhashview_cell`. Names such as `find_msgs`, `add_msg`, `del_msgs`, and `find_cells` need no extra dialog or notebook prefix. `msg` identifies a dialog message and `cell` identifies a notebook cell. `summary_nb` returns rows for notebook cells.
|
|
18
|
+
- Text-editing primitives include `insert_line`, `del_lines`, `replace_lines`, and `str_replace`. File, cell, and message versions add a prefix, as in `file_del_lines`, `cell_del_lines`, and `msg_del_lines`. After their address arguments, these versions take the same arguments as the text primitive.
|
|
19
|
+
|
|
20
|
+
`str_replace` keeps the name and argument order established by Anthropic's text editor tool. `ast_replace` uses AST patterns to find edit targets. `exhash` takes commands containing hash-verified line addresses. These operations use the same file, cell, and message prefixes: `file_ast_replace`, `msg_ast_replace`, `file_exhash`, and `cell_exhash`.
|
|
21
|
+
|
|
22
|
+
Converters use `x2y` names, such as `nb2dict` and `cell2xml`. In aidialog, exactly one side is `dlg`. Converter methods use `to_y`, as in `nb.to_dict()`.
|
|
23
|
+
|
|
24
|
+
Plural names take multiple items: `view_cell` takes one cell, `lnhashview_cells` several, and `del_msgs` many.
|
|
25
|
+
|
|
26
|
+
## Parameters
|
|
27
|
+
|
|
28
|
+
One vocabulary, identical wherever it appears:
|
|
29
|
+
|
|
30
|
+
- The place's address comes first (`text`; `path`; `path, cell_id`; a message `id`), the new text next, and ambient context last as keyword-only (message tools name their dialog that way).
|
|
31
|
+
- `start_line`/`end_line`: 1-based, inclusive, `None` for first/last, negative counting from the end. `del_lines` accepts no defaults: state the range.
|
|
32
|
+
- `re_filter`/`invert_filter`: restrict an edit to lines matching (or not matching) a regex, like ex's `g//` and `g!//`; combines with the range.
|
|
33
|
+
- Searches read patterns as regex by default. Editors read them as literal text unless `use_regex=True`.
|
|
34
|
+
- `nums` and `lnhashs` on any view: line numbers, or `lineno|hash|` addresses. `maxlen` caps characters per summary line; `trunc_out`/`trunc_in` truncate outputs and sources in dialog views.
|
|
35
|
+
- Search tools share one filter vocabulary: `pattern` first, `root='.'`, and the same include/exclude/ext/hidden/ignore block across `fd`, `ls`, `rg`, and `nbrg`. Variants differ by defaults, not API: `ls` is `fd` with listing defaults. Boolean filters narrow as `only_*` and widen as `include_*`.
|
|
36
|
+
- `context=` counts whatever units the place has: lines (or blocks in summary mode) for files, cells for notebooks, messages for dialogs. Dialog search defaults to context 1 because the neighbouring note usually explains the match.
|
|
37
|
+
- Every editor returns a diff ("none: No changes." when nothing changed). The diff is the verification: read it instead of re-viewing the target.
|
|
38
|
+
|
|
39
|
+
## Functions and methods
|
|
40
|
+
|
|
41
|
+
Editing functions take a file path, save their changes, and return a diff. Editing methods change an object in memory. Save it explicitly to write the changes.
|
|
42
|
+
|
|
43
|
+
Methods omit the function's address arguments and the part of its name that identifies the object. For example, `cell_str_replace(path, cell_id, ...)` becomes `c.str_replace(...)` on an `NbCell`. `find_cells(path, pat)` becomes `nb.find_cells(pat)` on a `Notebook`.
|
|
44
|
+
|
|
45
|
+
Read functions return snapshot rows containing addresses, source, and metadata. Read methods return the objects themselves. Use one style at a time per file. Save before switching to file functions, then reopen the object before returning to methods.
|
|
46
|
+
|
|
47
|
+
## Addresses
|
|
48
|
+
|
|
49
|
+
Edits accept line numbers, lnhash addresses, or section tokens. Get addresses while reading the target. Views accept `nums=True` or `lnhashs=True`. Searches can include addresses with `rg(lnhashs=True)`.
|
|
50
|
+
|
|
51
|
+
Prefer lnhash addresses when `exhash` is installed. The editor checks the hash against current content and rejects stale addresses. Plain line numbers are unverified and can shift after an edit. Re-view after each plain-line-number edit and apply multiple edits from bottom to top. Read `exhash.skill` for address formats and verified editing.
|
|
52
|
+
|
|
53
|
+
Section tokens identify sections in document outlines. In `1.6.|12|a3f2|,45|b1c3|`, `1.6.` is the section number. The rest is the lnhash range for its first and last lines. `d.at(token)` returns the section after checking its hash. `d.view(tok1, tok2)` prints the rendered text of both sections. Use the boundary range to edit the section: `file_exhash(path, ('12|a3f2|,45|b1c3|', 'c', new_text))`.
|
|
54
|
+
|
|
55
|
+
Read `exhash.skill` before working through large Markdown, code, or notebook files, or documentation from a URL. It describes `open_doc` tree searches and link following, including the llms.txt workflow.
|
|
56
|
+
|
|
57
|
+
When you don't know where to edit, start with a summary. `rgapi`'s `rg(summary=True)` and `nbrg`, aidialog's `summary_dlg`, and exhash's `open_doc` outlines show rows for blocks, cells, messages, or sections. Each row includes its address.
|
|
58
|
+
|
|
59
|
+
For prose, config, and other text organized into paragraphs, block summaries show the whole matched paragraph and its boundary addresses. Line-mode results show fragments that can require another view for context.
|
|
60
|
+
|
|
61
|
+
## What's where
|
|
62
|
+
|
|
63
|
+
- `fastcore.tools`: text primitives, file tools, and `line_hash`/`lnhash`/`lnhash_at` for creating addresses without exhash installed.
|
|
64
|
+
- `fastcore.nbio`: notebook read/write/validate/repair, cell construction, cell editors, and the `Notebook`/`NbCell` session objects with their snapshot queries (`find_cells`, `summary_nb`).
|
|
65
|
+
- `exhash.skill`: hash-verified editing for files and cells, plus `open_doc` section outlines for Markdown, code, and notebooks; prefer it for edits where installed.
|
|
66
|
+
- `rgapi.skill`: `rg`/`fd`/`ls`/`nbrg` search with lnhash output, and `rgstr` to search text already in hand.
|
|
67
|
+
- `remold`: structural search and rewrite for Python source (declarative ast-grep rules, LibCST matcher transforms, symbol queries); the engine behind `ast_replace`.
|
|
68
|
+
- `aidialog.dlgskill`, `dialoghelper`: the dialog layer, including its own theory of dialogs and projections.
|
|
69
|
+
|
|
70
|
+
Docs: https://fastcore.fast.ai/tools.html.md and https://fastcore.fast.ai/nbio.html.md
|
|
71
|
+
"""
|
|
72
|
+
|
|
73
|
+
from fastcore.tools import (insert_line, str_replace, strs_replace, replace_lines, del_lines, ast_replace,
|
|
74
|
+
file_insert_line, file_str_replace, file_strs_replace, file_replace_lines, file_del_lines, file_ast_replace,
|
|
75
|
+
view_file, create_file, line_hash, lnhash, lnhash_at)
|
|
76
|
+
from fastcore.nbio import (read_nb, write_nb, new_nb, mk_cell, validate_nb, validate_cell, repair_nb, repair_cell,
|
|
77
|
+
view_cell, cell_insert_line, cell_str_replace, cell_strs_replace, cell_replace_lines, cell_del_lines, cell_ast_replace, Notebook, NbCell, find_cells, summary_nb)
|
|
78
|
+
|
|
79
|
+
__all__ = ['insert_line', 'str_replace', 'strs_replace', 'replace_lines', 'del_lines', 'ast_replace',
|
|
80
|
+
'file_insert_line', 'file_str_replace', 'file_strs_replace', 'file_replace_lines', 'file_del_lines', 'file_ast_replace',
|
|
81
|
+
'view_file', 'create_file', 'line_hash', 'lnhash', 'lnhash_at',
|
|
82
|
+
'read_nb', 'write_nb', 'new_nb', 'mk_cell', 'validate_nb', 'validate_cell', 'repair_nb', 'repair_cell',
|
|
83
|
+
'view_cell', 'cell_insert_line', 'cell_str_replace', 'cell_strs_replace', 'cell_replace_lines', 'cell_del_lines', 'cell_ast_replace', 'Notebook', 'NbCell', 'find_cells', 'summary_nb']
|
|
84
|
+
|
|
85
|
+
__pyskill_params__ = {'replace_params': ('start_line', 'end_line', 'n_matches', 're_filter', 'invert_filter', 'use_regex')}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
"""The `L` class and helpers for it
|
|
2
2
|
|
|
3
|
-
`add_docs`
|
|
3
|
+
`add_docs` attaches docstrings to a class and its methods from a dictionary. `docs` is its decorator form. Both check that every public method has documentation.
|
|
4
4
|
|
|
5
|
-
`
|
|
5
|
+
`coll_repr` shows a collection's length and a preview of its items. `flatmap(f, xs)` applies `f` to each item and concatenates the results.
|
|
6
|
+
|
|
7
|
+
`L` extends Python's list operations with indexing inspired by [NumPy](http://www.numpy.org/). You can index with an integer, slice, collection of integers or boolean mask. Methods such as `map`, `filter`, `sorted`, `unique`, `itemgot` and `attrgot` return an `L`:
|
|
6
8
|
|
|
7
9
|
```python
|
|
8
10
|
t = L(1,2,3)
|
|
@@ -12,9 +14,13 @@ test_eq(t[[False,True,False,True]], [2,4])
|
|
|
12
14
|
test_eq(t.map(lambda o:o*2), [2,4,6,8])
|
|
13
15
|
```
|
|
14
16
|
|
|
15
|
-
|
|
17
|
+
Call `L.map(int)` on the class to get a function that converts an iterable's items to integers. This curried form works with `map`, `filter`, `groupby`, `argwhere`, `argfirst`, `first`, `last`, `sorted`, `reduce`, `partition`, `takewhile`, `dropwhile` and `accumulate`.
|
|
18
|
+
|
|
19
|
+
`star(f)` unpacks the last argument before calling `f`. In `map`, that argument is the item. In `reduce`, it is the item after the accumulator. `rstar` reverses the unpacked arguments.
|
|
20
|
+
|
|
21
|
+
`L` has methods for `itertools` operations including `cycle`, `takewhile`, `dropwhile`, `accumulate`, `pairwise`, `batched`, `compress`, `permutations` and `combinations`.
|
|
16
22
|
|
|
17
|
-
`
|
|
23
|
+
`partition` returns two collections according to a predicate. `flatten` recursively flattens nested iterables, leaving strings intact.
|
|
18
24
|
|
|
19
25
|
Docs: https://fastcore.fast.ai/foundation.html.md"""
|
|
20
26
|
|
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
"""Metaclasses
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
These metaclasses customize object construction:
|
|
4
4
|
|
|
5
|
-
- `FixSigMeta` preserves signature introspection
|
|
6
|
-
- `PrePostInitMeta`
|
|
7
|
-
- `AutoInit` is a `PrePostInitMeta` mixin that calls `super().__init__
|
|
8
|
-
- `NewChkMeta` makes `C(x)` return `x`
|
|
9
|
-
- `BypassNewMeta` casts an object of
|
|
5
|
+
- `FixSigMeta` preserves the constructor signature for introspection and tab completion. The other metaclasses inherit from it.
|
|
6
|
+
- `PrePostInitMeta` calls `__pre_init__` before `__init__` and `__post_init__` afterward.
|
|
7
|
+
- `AutoInit` is a `PrePostInitMeta` mixin that calls `super().__init__`.
|
|
8
|
+
- `NewChkMeta` makes `C(x)` return `x` when `x` is already a `C` and there are no other arguments.
|
|
9
|
+
- `BypassNewMeta` casts an object of `_bypass_type` to the new class without copying it.
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
See this [introduction to metaclasses](https://realpython.com/python-metaclasses/) for background.
|
|
12
|
+
|
|
13
|
+
`use_kwargs` and `use_kwargs_dict` replace `**kwargs` in a signature with parameters from a list or dictionary. `funcs_kwargs` lets a class accept method overrides as constructor arguments.
|
|
14
|
+
|
|
15
|
+
`@delegates(base)` adds `base`'s optional parameters to a wrapper's displayed signature for introspection and tab completion. Use `keep=True` to retain `**kwargs` and `but` to exclude parameters. On a class, `@delegates()` uses the superclass constructor:
|
|
12
16
|
|
|
13
17
|
```python
|
|
14
18
|
def basefn(a, b=2, c=3): ...
|
|
@@ -761,11 +761,9 @@ MAXLEN = 180 # Most characters shown per displayed line
|
|
|
761
761
|
|
|
762
762
|
# %% ../nbs/13_nbio.ipynb #80541be8
|
|
763
763
|
def prev_line(txt, maxlen=MAXLEN, pre='', sep=''):
|
|
764
|
-
"One-line preview
|
|
765
|
-
n = len(txt)
|
|
764
|
+
"One-line preview capped at `maxlen`, with blank-line runs shown as `¶` and a trailing count of omitted characters"
|
|
766
765
|
txt = re.sub(r'\n(?:\s*\n)*', '¶', txt)
|
|
767
|
-
|
|
768
|
-
return truncstr(f'{pre}[{humanize(n)}]{sep}{txt}', maxlen, suf=lambda n: f'…[{humanize(n)}]')
|
|
766
|
+
return truncstr(f'{pre}{sep}{txt}', maxlen, suf=lambda n: f'…[{humanize(n)}]')
|
|
769
767
|
|
|
770
768
|
# %% ../nbs/13_nbio.ipynb #804670bc
|
|
771
769
|
class CellRow:
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"""Threading and multiprocessing functions
|
|
2
2
|
|
|
3
|
-
`parallel(f, items)`
|
|
3
|
+
`parallel(f, items)` applies `f` to each item using a process pool. Set `threadpool=True` to use threads. Set `n_workers` to control concurrency, or to 0 for serial execution when debugging. Optional arguments include a `progress` bar, a `pause` between starts, and `return_exceptions` to collect errors. `parallel_async` runs work concurrently with asyncio.
|
|
4
|
+
|
|
5
|
+
`@threaded` runs a function in a background thread and returns the thread. Read its `result` attribute after it finishes. Set `process=True` to run in a process. `startthread` and `startproc` start their decorated functions immediately.
|
|
4
6
|
|
|
5
7
|
Docs: https://fastcore.fast.ai/parallel.html.md"""
|
|
6
8
|
|
|
@@ -15,7 +15,7 @@ def main(
|
|
|
15
15
|
print(msg.upper() if upper else msg)
|
|
16
16
|
```
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
`call_parse` provides argument parsing, help, defaults and error handling. Copy the example into a file and run it without adding an `if __name__ == "__main__"` block:
|
|
19
19
|
|
|
20
20
|
```
|
|
21
21
|
$ examples/test_fastcore.py --help
|
|
@@ -31,11 +31,11 @@ options:
|
|
|
31
31
|
--upper Convert to uppercase? (default: False)
|
|
32
32
|
```
|
|
33
33
|
|
|
34
|
-
|
|
34
|
+
You can also call the function normally from Python, including in a Jupyter notebook.
|
|
35
35
|
|
|
36
36
|
## Annotated params
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
Use `typing.Annotated` for `argparse` options that docments can't express:
|
|
39
39
|
|
|
40
40
|
```python
|
|
41
41
|
from fastcore.script import *
|
|
@@ -47,45 +47,61 @@ def main(msg:Annotated[str, "The message"],
|
|
|
47
47
|
print(msg.upper() if upper else msg)
|
|
48
48
|
```
|
|
49
49
|
|
|
50
|
-
The first element
|
|
50
|
+
The first element is the parameter's type. The first string in its metadata provides help text. Add a metadata dictionary to set `action`, `nargs`, `const`, `choices`, `required` or `version` arguments for `argparse.add_argument`.
|
|
51
|
+
|
|
52
|
+
The dictionary also accepts `opt`. Set it to `True` for a flag or `False` for a positional parameter. Without `opt`, parameters with defaults become flags.
|
|
51
53
|
|
|
52
54
|
## Short flags
|
|
53
55
|
|
|
54
|
-
|
|
56
|
+
The first capital letter in an optional parameter's name declares a short flag:
|
|
57
|
+
|
|
58
|
+
- `Resume:int=None` gives `-r` and `--resume`.
|
|
59
|
+
- `sUggest:str=None` gives `-u` and `--suggest`.
|
|
60
|
+
- Names without capitals have only a long flag.
|
|
61
|
+
|
|
62
|
+
Long flags use lowercase and hyphens, such as `--cache-dir` for `cache_dir`. Python calls keep the parameter's spelling, such as `main(Resume=3)`. Positional parameters have no flags and keep their names unchanged.
|
|
55
63
|
|
|
56
64
|
## Positional params
|
|
57
65
|
|
|
58
|
-
Parameters without
|
|
66
|
+
Parameters without defaults are positional. Other parameters become flags. `*args` accepts zero or more positional values. Parameters after it are flags. CLI calls ignore `**kwargs`.
|
|
67
|
+
|
|
68
|
+
Pass parameter names in `pos` to `call_parse` or `anno_parser` to keep them positional even with defaults. Omitted values use those defaults. Command-line order follows the signature, regardless of the order in `pos`. A boolean flag cannot appear in `pos` because it takes no value.
|
|
59
69
|
|
|
60
70
|
## Param types
|
|
61
71
|
|
|
62
72
|
A `bool` parameter is a `store_true` flag defaulting to `False`. If its default is `True` it becomes a `--no-` prefixed `store_false` flag instead, so passing the flag turns it off. Use `bool_arg` as the type when you want an explicit `--flag true|false` argument that honors its default.
|
|
63
73
|
|
|
64
|
-
|
|
74
|
+
`argparse` normally repeats an option's name as its value placeholder, such as `--path PATH`. Here help shows the type instead, such as `--path (str)`.
|
|
75
|
+
|
|
76
|
+
`anno2str` formats the type name. It displays `Path` as `path`. Unions list each type once and omit `str` when `path` is present. For example, `Path|str` displays as `path`.
|
|
77
|
+
|
|
78
|
+
Enums show their choices as `{choice,...}`. Positional parameters show their names.
|
|
65
79
|
|
|
66
80
|
Union types such as `int|str` try each type in turn, and `enum` types such as those from `str_enum` become argparse choices.
|
|
67
81
|
|
|
68
82
|
## The CLI entry point
|
|
69
83
|
|
|
70
|
-
|
|
84
|
+
`call_parse` parses `sys.argv` when you run the function's file directly with `python foo.py`, `python -m foo` or `%run foo.py`. It also parses arguments on a zero-argument call from the top-level body of a directly run file. Console-script wrappers use this form.
|
|
85
|
+
|
|
86
|
+
Calls from a notebook, REPL or another function are ordinary Python calls. This includes calls from another `call_parse` function.
|
|
71
87
|
|
|
72
|
-
CLI calls return
|
|
88
|
+
CLI calls return integers, including booleans, as exit codes. They discard other return values. Python calls preserve return values. Raise `CliError` to report an error message to CLI users.
|
|
73
89
|
|
|
74
|
-
|
|
90
|
+
Set `nested=True` when one CLI launches another. The outer parser removes the arguments it recognizes from `sys.argv`, leaving the rest for the inner CLI:
|
|
75
91
|
|
|
76
92
|
```sh
|
|
77
93
|
myrunner --keyword 1 script.py -- <script.py args>
|
|
78
94
|
```
|
|
79
95
|
|
|
80
|
-
|
|
96
|
+
`--` is optional in some invocations. Use it to separate the applications' arguments and avoid cases such as:
|
|
81
97
|
|
|
82
98
|
```sh
|
|
83
99
|
myrunner script.py -h
|
|
84
100
|
```
|
|
85
101
|
|
|
86
|
-
|
|
102
|
+
`myrunner` handles `-h` here instead of passing it to `script.py`.
|
|
87
103
|
|
|
88
|
-
|
|
104
|
+
Use `is_cli` to distinguish CLI execution from Python calls. Here the CLI prints a result that Python callers receive as a return value:
|
|
89
105
|
|
|
90
106
|
```python
|
|
91
107
|
@call_parse
|
|
@@ -97,7 +113,7 @@ def sum_args(a:int=0, b:int=0):
|
|
|
97
113
|
test_eq(sum_args(1,2), 3) # Python call: returns the value, prints nothing
|
|
98
114
|
```
|
|
99
115
|
|
|
100
|
-
|
|
116
|
+
Raise `CliError` when a command cannot continue. CLI execution prints its message to stderr and exits with status 1. Python callers can catch the exception:
|
|
101
117
|
|
|
102
118
|
```python
|
|
103
119
|
@call_parse
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
"""Text and file editing primitives shared by the fastai editing tools
|
|
2
2
|
|
|
3
|
-
The editors
|
|
3
|
+
The text editors take a string and return the edited text. Invalid edits raise `ValueError`. File editors read and write a path and return a diff. Aidialog provides corresponding message editors.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Read `fastcore.editskill` for the editing toolkit's naming, parameter and workflow conventions. It exposes these tools alongside the notebook editors from `fastcore.nbio`.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
`line_hash`, `lnhash` and `lnhash_at` create [exhash](https://answerdotai.github.io/exhash) addresses without depending on the exhash package. Addresses use `lineno|hash|`. The hash is the low 16 bits of CRC32, formatted as four hexadecimal characters.
|
|
8
|
+
|
|
9
|
+
File editors take the path first and return a unified diff. An unchanged result returns `none: No changes.` Invalid edits return `error: ...`.
|
|
8
10
|
|
|
9
11
|
view_file('~/a/b.py', 3)
|
|
10
12
|
create_file('~/a/b/c.py', 'content here')
|
|
@@ -12,7 +14,9 @@ File tools wrap the primitives with path I/O, returning unified diffs of what ch
|
|
|
12
14
|
file_del_lines('myfile.py', 2, 4)
|
|
13
15
|
file_replace_lines('myfile.py', new_content=src) # no line numbers: replace the entire contents
|
|
14
16
|
|
|
15
|
-
`file_str_replace`, `file_strs_replace
|
|
17
|
+
`file_str_replace`, `file_strs_replace` and `file_del_lines` support line ranges, `re_filter` and `invert_filter`. These work like ex's `g//` and `g!//` filters.
|
|
18
|
+
|
|
19
|
+
For Python syntax-tree replacements, use `ast_replace` or `file_ast_replace`. They accept ast-grep `(pattern, replacement)` rules with `$VAR` metavariables and require the optional `remold` package.
|
|
16
20
|
|
|
17
21
|
Docs: https://fastcore.fast.ai/tools.html.md"""
|
|
18
22
|
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
"""Concise HTML generation and namespace-aware XML construction.
|
|
2
2
|
|
|
3
|
-
`ft(tag, *children, **attrs)` builds an `FT` tree
|
|
3
|
+
`ft(tag, *children, **attrs)` builds an `FT` tree for HTML generation. `to_xml` renders the tree. [FastHTML](https://fastht.ml) uses these functions to generate HTML.
|
|
4
|
+
|
|
5
|
+
Common tags have constructors such as `Div`, `P` and `A`. Pass attributes as keywords, using `klass` for `class` and `fr` for `for`. Underscores in attribute names become hyphens. You can also get and set attributes on the element.
|
|
6
|
+
|
|
7
|
+
To supply children after attributes, call the element: `Div(id='x')(child1, child2)`.
|
|
4
8
|
|
|
5
9
|
```python
|
|
6
10
|
t = Div(P('hi', klass='a'), hx_get='/get')
|
|
@@ -8,7 +12,15 @@ test_eq(to_xml(t), '<div hx-get="/get">\n <p class="a">hi</p>\n</div>\n')
|
|
|
8
12
|
test_eq(to_xml(Div(id='x')('child')), '<div id="x">child</div>\n')
|
|
9
13
|
```
|
|
10
14
|
|
|
11
|
-
`E
|
|
15
|
+
`E` creates XML elements from Python calls. Its `ns` argument maps namespace prefixes to URIs. The `prefix` and `attr_ns` arguments set the default prefixes for element and attribute names.
|
|
16
|
+
|
|
17
|
+
For example, `e = E('w', attr_ns='w', ns={'w': uri})` uses `w` for both defaults. Calling `e.tcW(type='dxa', w=2400)` creates a `w:tcW` element with `w:type` and `w:w` attributes.
|
|
18
|
+
|
|
19
|
+
`E` preserves the case of tag and attribute names. Use `prefix__name` for an explicitly prefixed attribute, or `attrs_` for literal attribute names.
|
|
20
|
+
|
|
21
|
+
Nesting an element under a different parent does not change its namespace bindings.
|
|
22
|
+
|
|
23
|
+
XML serialization escapes text and writes booleans as `true` or `false`. Override `attr_value` for other conventions. `.bytes()` returns UTF-8. `ft` and `to_xml` retain their HTML behavior.
|
|
12
24
|
|
|
13
25
|
Docs: https://fastcore.fast.ai/xml.html.md"""
|
|
14
26
|
|
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
"""Utility functions used in the fastai library
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
IO utilities include:
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
- `maybe_open`: accept a path or an open file, closing only files it opens.
|
|
6
|
+
- `run`: run a command and return stdout; raise `IOError` on failure.
|
|
7
|
+
- `atomic_save`: write a temporary file, then rename it over the destination.
|
|
8
|
+
- `untar_dir`: extract an archive, creating a containing directory when needed.
|
|
9
|
+
- `globtastic`: recursively find files with include and exclude filters.
|
|
6
10
|
|
|
7
|
-
`
|
|
11
|
+
`Config` reads and writes an ini file with one `DEFAULT` section. Access keys as attributes, items, or with `.get(key, default)`. Use `create=` to supply initial contents for a missing file.
|
|
12
|
+
|
|
13
|
+
`types=` converts values on read. `Path` values resolve relative to the config file's directory. `Config.find(name)` searches a directory and its parents for the file.
|
|
14
|
+
|
|
15
|
+
`dict2obj` converts nested dicts to `AttrDict` and lists to `L`, allowing attribute access such as `d.b.c`. `obj2dict` reverses the conversion:
|
|
8
16
|
|
|
9
17
|
```python
|
|
10
18
|
d = dict2obj({'a':1, 'b':{'c':2, 'd':[3,4]}})
|
|
@@ -12,9 +20,22 @@ test_eq(d.b.c, 2)
|
|
|
12
20
|
test_eq(d.b.d[1], 4)
|
|
13
21
|
```
|
|
14
22
|
|
|
15
|
-
|
|
23
|
+
These methods extend [Pathlib.Path](https://docs.python.org/3/library/pathlib.html#basic-use):
|
|
24
|
+
|
|
25
|
+
- `ls()` lists directory contents as an `L`, optionally filtered by MIME prefix or extension.
|
|
26
|
+
- `read_json`, `write_json`, and `read_jsonl` handle JSON files.
|
|
27
|
+
- `readlines` reads lines; `mk_write` creates parent directories before writing.
|
|
28
|
+
- `delete`, `relpath`, and `normpath` provide file deletion and path conversions.
|
|
29
|
+
|
|
30
|
+
Set `Path.BASE_PATH` to display paths relative to that directory.
|
|
31
|
+
|
|
32
|
+
Other utilities include:
|
|
16
33
|
|
|
17
|
-
|
|
34
|
+
- `exec_eval`: run a code string and return its last expression, like a notebook cell.
|
|
35
|
+
- `fenced`: wrap text in a Markdown fence longer than any fence character run in the body.
|
|
36
|
+
- `str_diff`: return a unified diff, or `''` for identical text.
|
|
37
|
+
- `unqid` and `friendly_name`: generate random Python identifiers or memorable word-based names.
|
|
38
|
+
- `flexicache`: cache results with configurable expiration policies.
|
|
18
39
|
|
|
19
40
|
Docs: https://fastcore.fast.ai/xtras.html.md"""
|
|
20
41
|
|
|
@@ -117,6 +138,8 @@ def globtastic(
|
|
|
117
138
|
symlinks:bool=True, # follow symlinks?
|
|
118
139
|
file_glob:str=None, # Only include files matching glob
|
|
119
140
|
file_re:str=None, # Only include files matching regex
|
|
141
|
+
path_glob:str=None, # Only include files whose full paths match glob (wildcards match separators)
|
|
142
|
+
path_re:str=None, # Only include files whose full paths match regex
|
|
120
143
|
folder_re:str=None, # Only enter folders matching regex
|
|
121
144
|
skip_file_glob:str=None, # Skip files matching glob
|
|
122
145
|
skip_file_re:str=None, # Skip files matching regex
|
|
@@ -137,10 +160,13 @@ def globtastic(
|
|
|
137
160
|
if exts:
|
|
138
161
|
exts = [e if e.startswith('.') else f'.{e}' for e in listify(exts)]
|
|
139
162
|
file_re = f"({'|'.join(re.escape(e) for e in exts)})$"
|
|
140
|
-
file_re,folder_re = compile_re(file_re),compile_re(folder_re)
|
|
163
|
+
file_re,folder_re,path_re = compile_re(file_re),compile_re(folder_re),compile_re(path_re)
|
|
141
164
|
skip_file_re,skip_folder_re = compile_re(skip_file_re),compile_re(skip_folder_re)
|
|
142
165
|
def _keep_file(root, name):
|
|
143
|
-
|
|
166
|
+
fname = os.path.join(root,name)
|
|
167
|
+
return (not path_glob or fnmatch(fname, path_glob)) and (
|
|
168
|
+
not path_re or path_re.search(fname)) and (
|
|
169
|
+
not file_glob or fnmatch(name, file_glob)) and (
|
|
144
170
|
not file_re or file_re.search(name)) and (
|
|
145
171
|
not skip_file_glob or not fnmatch(name, skip_file_glob)) and (
|
|
146
172
|
not skip_file_re or not skip_file_re.search(name))
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: fastcore
|
|
3
|
-
Version: 2.2.
|
|
3
|
+
Version: 2.2.28
|
|
4
4
|
Summary: Python supercharged for fastai development
|
|
5
5
|
Author-email: Jeremy Howard and Sylvain Gugger <infos@fast.ai>
|
|
6
6
|
License: Apache-2.0
|
|
@@ -88,6 +88,8 @@ To install fastcore run: `conda install fastcore -c fastai` (if you use Anaconda
|
|
|
88
88
|
|
|
89
89
|
To get started, we recommend you read through [the fastcore tour](https://fastcore.fast.ai/tour.html).
|
|
90
90
|
|
|
91
|
+
[`globtastic`](https://fastcore.fast.ai/xtras.html#globtastic) supports filename filters (`file_glob`, `file_re`) and full-path filters (`path_glob`, `path_re`). Glob filters use `fnmatch`: `*` matches directory separators and `**` has no special meaning.
|
|
92
|
+
|
|
91
93
|
## Contributing
|
|
92
94
|
|
|
93
95
|
After you clone this repository, please run `nbdev_install_hooks` in your terminal. This sets up git hooks, which clean up the notebooks to remove the extraneous stuff stored in the notebooks (e.g. which cells you ran) which causes unnecessary merge conflicts.
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
"""Text, file, cell, and notebook editing from `fastcore.tools` and `fastcore.nbio`, plus the conventions the whole fastai editing toolkit follows. Read this before working with the editing tools in any package that shares them.
|
|
2
|
-
|
|
3
|
-
`from fastcore.editskill import *` loads the fastcore editing layer: the text primitives and file tools of `fastcore.tools`, and the notebook I/O and cell editors of `fastcore.nbio`. Sibling packages extend the same toolkit, and where they are installed prefer them as noted: `exhash` for hash-verified editing, `rgapi` for search, `aidialog`/`dialoghelper` for dialogs.
|
|
4
|
-
|
|
5
|
-
## Places
|
|
6
|
-
|
|
7
|
-
Each editing function works on one place: text (a str in memory), a file (a path on disk), a cell (one cell's source in an .ipynb file, addressed by `path, cell_id`), or a notebook (the parsed .ipynb; `Notebook` and `NbCell` are its held-object forms). These are the representation layer: an .ipynb is a file of cells, whatever produced it. The dialog layer above adds the msg and dlg places: a Solveit dialog is an .ipynb whose cells are messages (notes, runnable code, prompt/reply pairs), and `aidialog.dlgskill` and `dialoghelper` provide the message tools, following the conventions here with their own nouns. The word picks the layer: cell tools answer representation questions ("why does Jupyter reject this file?"), msg tools answer content questions ("what does this message say?").
|
|
8
|
-
|
|
9
|
-
The section layer sits above both. `open_doc` (in exhash) parses a file (`fname=`, or a `Path` as `src`), URL (an `https?://` str), or text (any other str) into a `Section` tree, with sections taken from Markdown headings, tree-sitter definitions in code, or md-heading cells in a notebook. A section points at a span of lines in a file, or a run of cells in a notebook. To change what a section contains, edit those lines or cells with the file and cell tools.
|
|
10
|
-
|
|
11
|
-
## Naming
|
|
12
|
-
|
|
13
|
-
Two name shapes cover the toolkit, and the pivot is the verb's direct object:
|
|
14
|
-
|
|
15
|
-
- An operation on a whole place takes the place as its noun: verb_place. `view_file`, `create_file`, `read_nb`, `write_nb`, `view_cell`, `validate_nb`; in the dialog layer `view_msg` and `view_dlg`. Coined verbs follow the same shape: `lnhashview_cell` is "lnhashview this cell". When the verb's object is instead the unit inside the place, and that unit names its place uniquely, no prefix is needed - the unit noun is the place signal: `find_msgs`, `add_msg`, `del_msgs` (msgs live only in dialogs), `find_cells`, `summary_nb`'s rows (cells live only in notebooks).
|
|
16
|
-
- An operation within a place already owns its noun (`insert_line`, `del_lines`, `replace_lines`, `str_replace`), so the place prefixes as a namespace and the op name survives intact: place_op, as in `file_del_lines`, `cell_del_lines`, `msg_del_lines`. The bare op names are the text-level primitives, and every place version keeps the identical signature after its address arguments, so each family is learned once and recognized everywhere.
|
|
17
|
-
|
|
18
|
-
The exceptions are deliberate and closed. `str_replace` keeps the name and argument order established by Anthropic's text editor tool. Instrument-named ops put the instrument first and elide their unit: `ast_replace` (the AST pattern is how the edit finds its target) and `exhash` (hash-verified line addresses travel inside its commands), place-prefixed like any other line-level op: `file_ast_replace`, `msg_ast_replace`, `file_exhash`, `cell_exhash`. Converters are named x2y (`nb2dict`, `cell2xml`; in aidialog, `dlg` on exactly one side of every converter), and on a held object the converter is a `to_y` method (`nb.to_dict()`). Plural marks arity: `view_cell` takes one cell, `lnhashview_cells` several, `del_msgs` many.
|
|
19
|
-
|
|
20
|
-
## Parameters
|
|
21
|
-
|
|
22
|
-
One vocabulary, identical wherever it appears:
|
|
23
|
-
|
|
24
|
-
- The place's address comes first (`text`; `path`; `path, cell_id`; a message `id`), the new text next, and ambient context last as keyword-only (message tools name their dialog that way).
|
|
25
|
-
- `start_line`/`end_line`: 1-based, inclusive, `None` for first/last, negative counting from the end. `del_lines` accepts no defaults: state the range.
|
|
26
|
-
- `re_filter`/`invert_filter`: restrict an edit to lines matching (or not matching) a regex, like ex's `g//` and `g!//`; combines with the range.
|
|
27
|
-
- Searches read patterns as regex by default; editors read them as literal text until `use_regex=True`. Searching is read-only, so its default favors power; editing favors caution.
|
|
28
|
-
- `nums` and `lnhashs` on any view: line numbers, or `lineno|hash|` addresses. `maxlen` caps characters per summary line; `trunc_out`/`trunc_in` truncate outputs and sources in dialog views.
|
|
29
|
-
- Search tools share one filter vocabulary: `pattern` first, `root='.'`, and the same include/exclude/ext/hidden/ignore block across `fd`, `ls`, `rg`, and `nbrg`. Variants differ by defaults, not API: `ls` is `fd` with listing defaults. Boolean filters narrow as `only_*` and widen as `include_*`.
|
|
30
|
-
- `context=` counts whatever units the place has: lines (or blocks in summary mode) for files, cells for notebooks, messages for dialogs. Dialog search defaults to context 1 because the neighbouring note usually explains the match: the why lives next to the what.
|
|
31
|
-
- Every editor returns a diff ("none: No changes." when nothing changed). The diff is the verification: read it instead of re-viewing the target.
|
|
32
|
-
|
|
33
|
-
## Functions and methods
|
|
34
|
-
|
|
35
|
-
Every operation has two shapes with one contract each. The function is a transaction: it addresses a file by path, applies the edit, writes, and returns a diff. The method is a session: it changes the object in hand, and nothing reaches disk until an explicit save. The correspondence is mechanical - the method is the function minus its address arguments, keeping its name except that a place word which became `self` drops: `cell_str_replace(path, cell_id, ...)` is `c.str_replace(...)` on a held `NbCell`, `find_cells(path, pat)` is `nb.find_cells(pat)` on a held `Notebook`. Function-side reads return plain snapshot rows carrying addresses, source, and meta; session-side reads return the objects themselves. Use one shape at a time per file: save before switching to functions, reopen after.
|
|
36
|
-
|
|
37
|
-
## Addresses
|
|
38
|
-
|
|
39
|
-
Edits say where with line numbers, lnhash addresses, or section tokens. Take addresses from the read you were already doing instead of with a second look: views accept `nums=True` or `lnhashs=True`, and searches return addresses directly (`rg(lnhashs=True)`). Prefer lnhash addresses whenever `exhash` is installed: they are verified against current content at edit time, so a stale address fails loudly instead of editing nearby text, which is exactly why taking addresses early works. Plain line numbers are unverified and shift as edits apply, so re-view after each edit and apply multi-edits bottom-to-top. `exhash.skill` owns the address format and the verified editor. Section tokens make a long document navigable and editable a piece at a time. An outline row shows one, as in `1.6.|12|a3f2|,45|b1c3|`: the section number, then the lnhash addresses of its first and last lines. `d.at(token)` returns that section with its hash checked, `d.view(tok1, tok2)` prints the rendered text of each, and the same boundary pair drops straight into a range edit: `file_exhash(path, ('12|a3f2|,45|b1c3|', 'c', new_text))`. Read `exhash.skill` before working through a large Markdown, code, or notebook file, or before reading documentation from a URL: `open_doc` also searches within a tree and follows links, which is how the llms.txt docs workflow runs.
|
|
40
|
-
|
|
41
|
-
When you don't yet know where to edit, locate with a summary first: `rgapi`'s `rg(summary=True)` and `nbrg`, aidialog's `summary_dlg`, and exhash's `open_doc` outlines each show one row per natural unit (block, cell, message, section), carrying the unit's address. For prose, config, and other paragraph-shaped text, blocks are usually the right unit for reading too: one summary row shows the whole matched paragraph with its boundary addresses, where a line-mode hit shows a fragment that then needs a context view. Summaries locate, views read, addresses edit, diffs confirm.
|
|
42
|
-
|
|
43
|
-
## What's where
|
|
44
|
-
|
|
45
|
-
- `fastcore.tools`: text primitives, file tools, and `line_hash`/`lnhash`/`lnhash_at` for creating addresses without exhash installed.
|
|
46
|
-
- `fastcore.nbio`: notebook read/write/validate/repair, cell construction, cell editors, and the `Notebook`/`NbCell` session objects with their snapshot queries (`find_cells`, `summary_nb`).
|
|
47
|
-
- `exhash.skill`: hash-verified editing for files and cells, plus `open_doc` section outlines for Markdown, code, and notebooks; prefer it for edits where installed.
|
|
48
|
-
- `rgapi.skill`: `rg`/`fd`/`ls`/`nbrg` search with lnhash output, and `rgstr` to search text already in hand.
|
|
49
|
-
- `remold`: structural search and rewrite for Python source (declarative ast-grep rules, LibCST matcher transforms, symbol queries); the engine behind `ast_replace`.
|
|
50
|
-
- `aidialog.dlgskill`, `dialoghelper`: the dialog layer, including its own theory of dialogs and projections.
|
|
51
|
-
|
|
52
|
-
Docs: https://fastcore.fast.ai/tools.html.md and https://fastcore.fast.ai/nbio.html.md
|
|
53
|
-
"""
|
|
54
|
-
|
|
55
|
-
from fastcore.tools import (insert_line, str_replace, strs_replace, replace_lines, del_lines, ast_replace,
|
|
56
|
-
file_insert_line, file_str_replace, file_strs_replace, file_replace_lines, file_del_lines, file_ast_replace,
|
|
57
|
-
view_file, create_file, line_hash, lnhash, lnhash_at)
|
|
58
|
-
from fastcore.nbio import (read_nb, write_nb, new_nb, mk_cell, validate_nb, validate_cell, repair_nb, repair_cell,
|
|
59
|
-
view_cell, cell_insert_line, cell_str_replace, cell_strs_replace, cell_replace_lines, cell_del_lines, cell_ast_replace, Notebook, NbCell, find_cells, summary_nb)
|
|
60
|
-
|
|
61
|
-
__all__ = ['insert_line', 'str_replace', 'strs_replace', 'replace_lines', 'del_lines', 'ast_replace',
|
|
62
|
-
'file_insert_line', 'file_str_replace', 'file_strs_replace', 'file_replace_lines', 'file_del_lines', 'file_ast_replace',
|
|
63
|
-
'view_file', 'create_file', 'line_hash', 'lnhash', 'lnhash_at',
|
|
64
|
-
'read_nb', 'write_nb', 'new_nb', 'mk_cell', 'validate_nb', 'validate_cell', 'repair_nb', 'repair_cell',
|
|
65
|
-
'view_cell', 'cell_insert_line', 'cell_str_replace', 'cell_strs_replace', 'cell_replace_lines', 'cell_del_lines', 'cell_ast_replace', 'Notebook', 'NbCell', 'find_cells', 'summary_nb']
|
|
66
|
-
|
|
67
|
-
__pyskill_params__ = {'replace_params': ('start_line', 'end_line', 'n_matches', 're_filter', 'invert_filter', 'use_regex')}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|