You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 4184fa6
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: README.md
+13-15Lines changed: 13 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,19 +13,19 @@ Python is a powerful, dynamic language. Rather than bake everything into the lan
13
13
14
14
Here are some tips on using fastcore:
15
15
16
-
-**Liberal imports**: Use `from fastcore.module import *` freely. The library is designed for safe wildcard imports.
17
-
-**Enhanced list operations**: Substitute `list` with [`L`](https://fastcore.fast.ai/foundation.html#l). This provides advanced indexing, method chaining, and additional functionality while maintaining list-like behavior.
18
-
-**Extend existing classes**: Apply the `@patch` decorator to add methods to classes, including built-ins, without subclassing.
19
-
-**Streamline class initialization**: In `__init__` methods, use[`store_attr()`](https://fastcore.fast.ai/basics.html#store_attr) to efficiently set multiple attributes, reducing repetitive assignment code.
20
-
-**Explicit keyword arguments**: Apply the [`delegates`](https://fastcore.fast.ai/meta.html#delegates) decorator to functions to replace `**kwargs` with specific parameters, enhancing IDE support and documentation.
21
-
-**Optimize parallel execution**: Use fastcore’s enhanced [`ThreadPoolExecutor`](https://fastcore.fast.ai/parallel.html#threadpoolexecutor) and [`ProcessPoolExecutor`](https://fastcore.fast.ai/parallel.html#processpoolexecutor) for simplified concurrent processing.
22
-
-**Expressive testing**: Prefer fastcore’s testing functions like [`test_eq`](https://fastcore.fast.ai/test.html#test_eq), [`test_ne`](https://fastcore.fast.ai/test.html#test_ne), [`test_close`](https://fastcore.fast.ai/test.html#test_close) for more readable and informative test assertions.
23
-
-**Advanced file operations**: Use the extended `Path` class, which adds methods like `ls()`, `read_json()`, and others to [`pathlib.Path`](https://docs.python.org/3/library/pathlib.html#pathlib.Path).
24
-
-**Flexible data structures**: Convert between dictionaries and attribute-access objects using [`dict2obj`](https://fastcore.fast.ai/xtras.html#dict2obj) and [`obj2dict`](https://fastcore.fast.ai/xtras.html#obj2dict) for more intuitive data handling.
25
-
-**Functional programming paradigms**: Use tools like [`compose`](https://fastcore.fast.ai/basics.html#compose), [`maps`](https://fastcore.fast.ai/basics.html#maps), and [`filter_ex`](https://fastcore.fast.ai/basics.html#filter_ex) to write more functional-style Python code.
26
-
-**Documentation**: Document parameters and return values with [`docments`](https://fastcore.fast.ai/docments.html#docments), using source comments or `Annotated` metadata for generated APIs. [`MarkdownRenderer`](https://fastcore.fast.ai/docments.html#markdownrenderer) shows the effective signature and retains usage sections such as Notes, Raises, and Examples.
27
-
-**Time-aware caching**: Apply the [`timed_cache`](https://fastcore.fast.ai/xtras.html#timed_cache) decorator to add time-based expiration to the standard `lru_cache` functionality.
28
-
-**Simplified CLI creation**: Use `fastcore.script` to easily transform Python functions into command-line interfaces.
16
+
- Use `from fastcore.module import *` freely. fastcore’s modules are built to be safe for wildcard imports.
17
+
-Use [`L`](https://fastcore.fast.ai/foundation.html#l) in place of `list`. [`L`](https://fastcore.fast.ai/foundation.html#l) behaves like a list, with extra indexing options, method chaining and more methods.
18
+
-Add methods to existing classes, including built-ins, with the `@patch` decorator instead of subclassing.
19
+
- In `__init__` methods, call[`store_attr()`](https://fastcore.fast.ai/basics.html#store_attr) to set multiple attributes at once instead of assigning each one.
20
+
- Apply the [`delegates`](https://fastcore.fast.ai/meta.html#delegates) decorator to show a function’s real parameters in place of `**kwargs`, for IDEs and generated documentation.
21
+
- Use fastcore’s versions of [`ThreadPoolExecutor`](https://fastcore.fast.ai/parallel.html#threadpoolexecutor) and [`ProcessPoolExecutor`](https://fastcore.fast.ai/parallel.html#processpoolexecutor) for simpler concurrent processing.
22
+
- Prefer fastcore’s test functions, such as [`test_eq`](https://fastcore.fast.ai/test.html#test_eq), [`test_ne`](https://fastcore.fast.ai/test.html#test_ne) and [`test_close`](https://fastcore.fast.ai/test.html#test_close), for assertions that read clearly and give informative failures.
23
+
-fastcore extends [`pathlib.Path`](https://docs.python.org/3/library/pathlib.html#pathlib.Path) with methods such as `ls()` and `read_json()`.
24
+
- Convert between dictionaries and objects with attributeaccess using [`dict2obj`](https://fastcore.fast.ai/xtras.html#dict2obj) and [`obj2dict`](https://fastcore.fast.ai/xtras.html#obj2dict).
25
+
-Write in a functional style with tools such as [`compose`](https://fastcore.fast.ai/basics.html#compose), [`maps`](https://fastcore.fast.ai/basics.html#maps) and [`filter_ex`](https://fastcore.fast.ai/basics.html#filter_ex).
26
+
- Document parameters and return values with [`docments`](https://fastcore.fast.ai/docments.html#docments), using source comments or `Annotated` metadata for generated APIs. [`MarkdownRenderer`](https://fastcore.fast.ai/docments.html#markdownrenderer) shows the effective signature and keeps usage sections such as Notes, Raises and Examples.
27
+
- Apply the [`timed_cache`](https://fastcore.fast.ai/xtras.html#timed_cache) decorator to add time-based expiry to the standard `lru_cache`.
28
+
-Turn Python functions into command-line interfaces with `fastcore.script`.
29
29
30
30
For example, [`L`](https://fastcore.fast.ai/foundation.html#l) is a drop-in replacement for `list` with extra superpowers:
31
31
@@ -57,8 +57,6 @@ 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
-
62
60
## Contributing
63
61
64
62
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.
Copy file name to clipboardExpand all lines: fastcore/__init__.py
+13-13Lines changed: 13 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -4,19 +4,19 @@
4
4
5
5
Here are some tips on using fastcore:
6
6
7
-
- **Liberal imports**: Use `from fastcore.module import *` freely. The library is designed for safe wildcard imports.
8
-
- **Enhanced list operations**: Substitute `list` with `L`. This provides advanced indexing, method chaining, and additional functionality while maintaining list-like behavior.
9
-
- **Extend existing classes**: Apply the `@patch` decorator to add methods to classes, including built-ins, without subclassing.
10
-
- **Streamline class initialization**: In `__init__` methods, use `store_attr()` to efficiently set multiple attributes, reducing repetitive assignment code.
11
-
- **Explicit keyword arguments**: Apply the `delegates` decorator to functions to replace `**kwargs` with specific parameters, enhancing IDE support and documentation.
12
-
- **Optimize parallel execution**: Use fastcore's enhanced `ThreadPoolExecutor` and `ProcessPoolExecutor` for simplified concurrent processing.
13
-
- **Expressive testing**: Prefer fastcore's testing functions like `test_eq`, `test_ne`, `test_close` for more readable and informative test assertions.
14
-
- **Advanced file operations**: Use the extended `Path` class, which adds methods like `ls()`, `read_json()`, and others to `pathlib.Path`.
15
-
- **Flexible data structures**: Convert between dictionaries and attribute-access objects using `dict2obj` and `obj2dict` for more intuitive data handling.
16
-
- **Functional programming paradigms**: Use tools like `compose`, `maps`, and `filter_ex` to write more functional-style Python code.
17
-
- **Documentation**: Document parameters and return values with `docments`, using source comments or `Annotated` metadata for generated APIs. `MarkdownRenderer` shows the effective signature and retains usage sections such as Notes, Raises, and Examples.
18
-
- **Time-aware caching**: Apply the `timed_cache` decorator to add time-based expiration to the standard `lru_cache` functionality.
19
-
- **Simplified CLI creation**: Use `fastcore.script` to easily transform Python functions into command-line interfaces.
7
+
- Use `from fastcore.module import *` freely. fastcore's modules are built to be safe for wildcard imports.
8
+
- Use `L` in place of `list`. `L` behaves like a list, with extra indexing options, method chaining and more methods.
9
+
- Add methods to existing classes, including built-ins, with the `@patch` decorator instead of subclassing.
10
+
- In `__init__` methods, call `store_attr()` to set multiple attributes at once instead of assigning each one.
11
+
- Apply the `delegates` decorator to show a function's real parameters in place of `**kwargs`, for IDEs and generated documentation.
12
+
- Use fastcore's versions of `ThreadPoolExecutor` and `ProcessPoolExecutor` for simpler concurrent processing.
13
+
- Prefer fastcore's test functions, such as `test_eq`, `test_ne` and `test_close`, for assertions that read clearly and give informative failures.
14
+
- fastcore extends `pathlib.Path` with methods such as `ls()` and `read_json()`.
15
+
- Convert between dictionaries and objects with attributeaccess using `dict2obj` and `obj2dict`.
16
+
- Write in a functional style with tools such as `compose`, `maps` and `filter_ex`.
17
+
- Document parameters and return values with `docments`, using source comments or `Annotated` metadata for generated APIs. `MarkdownRenderer` shows the effective signature and keeps usage sections such as Notes, Raises and Examples.
18
+
- Apply the `timed_cache` decorator to add time-based expiry to the standard `lru_cache`.
19
+
- Turn Python functions into command-line interfaces with `fastcore.script`.
20
20
21
21
For example, `L` is a drop-in replacement for `list` with extra superpowers:
Copy file name to clipboardExpand all lines: fastcore/basics.py
+4-4Lines changed: 4 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,10 @@
1
1
"""Basic functionality used in the fastai library
2
2
3
-
The staples used throughout fastai code, each replacing a common multi-line pattern with a one-liner: `ifnone(a,b)` is `b if a is None else a` (though both args are always evaluated); `listify` and `tuplify` convert anything to a list or tuple the way you'd mean it (`None` becomes `[]`, a `str` or `dict` stays a single item, a generator is consumed); and `basic_repr('a,b')` gives a class a deterministic `key=value` repr with no memory address, so notebook and git diffs stay clean.
3
+
The staples used throughout fastai code, each replacing a common multi-line pattern with a one-liner.
4
4
5
5
Curried versions of the comparison and arithmetic functions in Python's `operator` module: `lt gt le ge eq ne add sub mul truediv is_ is_not in_ mod`. With two args they work like `operator`'s versions; with one arg they return a partial that binds it as the *second* argument, so `lt(3)` means "is less than 3" and `in_(vals)` means "is contained in `vals`". They read especially well as `cmp` arguments to `fastcore.test.test`, e.g. `test(x, valid, in_)`.
6
6
7
-
`AttrDict` is a `dict` whose keys are also attributes, so `d.foo` reads and writes `d['foo']` (to convert a whole nested structure at once, see `dict2obj` in `fastcore.xtras`); `NS` is the same idea built on `SimpleNamespace`, adding indexing and iteration. `store_attr()`, called inside `__init__`, stores the function's arguments as attributes of `self` in one line:
7
+
`store_attr()`, called inside `__init__`, stores the function's arguments as attributes of `self` in one line:
8
8
9
9
```python
10
10
class Point:
@@ -13,7 +13,7 @@ def __init__(self, x, y, scale=1): store_attr()
13
13
test_eq((p.x, p.y, p.scale), (1, 2, 1))
14
14
```
15
15
16
-
Tools for making and transforming functions: `compose(f,g,...)` chains functions left to right; `bind` is `partial` extended with `arg0`,`arg1`,... placeholders for reordering positional arguments; and `fail_clean` re-raises exceptions with the library's own traceback frames stripped, for errors that are part of a function's contract rather than bugs.
16
+
Utilities for functional programming or for defining, modifying, or debugging functions.
17
17
18
18
`~Self` is a concise alternative to `lambda` for a function that operates on a single object (note the capitalization!). Write the chain of attribute accesses, method calls, and indexing just as you would after a variable name, with `~Self` in its place, and the result is a plain function that runs the chain on its argument:
19
19
@@ -25,7 +25,7 @@ def __init__(self, x, y, scale=1): store_attr()
25
25
26
26
Since `.`, `()`, and `[]` bind tighter than `~`, the whole chain builds first and `~` then converts it to a function, so the chain never needs its own parentheses: `map(~Self.imag, nums)` works as written.
27
27
28
-
`@patch` adds a function to an existing class as a method, using the function's `self:` type annotation to pick the class (a union annotation patches several classes at once); `@patch_to(Cls)` is the same with the class passed explicitly. Both take `as_prop`, `set_prop`, and `cls_method`. fastai code uses this to build classes incrementally across a notebook, so expect to find a class's methods defined far from the class itself:
28
+
fastai code builds classes incrementally across a notebook. A class's methods often appear far from the class itself. `@patch` adds a function to an existing class as a method, and picks the classfrom the function's `self:` annotation:
Copy file name to clipboardExpand all lines: fastcore/nbio.py
+3-3Lines changed: 3 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,10 @@
1
1
"""Reading, writing, and running Jupyter notebooks
2
2
3
-
Cell tools apply `fastcore.tools`' string editing primitives to one notebook cell's source, addressed by path and cell id, mirroring that module's file tools: the same operations and parameters, with `path, cell_id` in place of `path`. Each editor (including the structural `cell_ast_replace`) returns a diff of the change, and `view_cell` shows a cell's source with optional line numbers or exhash addresses.
3
+
The cell tools apply `fastcore.tools`' string editing primitives to one notebook cell's source. They match that module's file tools, with the same operations and parameters, but take `path, cell_id` where the file tools take `path`. Every editor, including the structural `cell_ast_replace`, returns a diff of its change. `view_cell` shows a cell's source with optional line numbers or exhash addresses.
4
4
5
-
Naming and parameter conventions shared across the editing toolkit are documented in `fastcore.editskill`, which also re-exports this module's editing tools.
5
+
`fastcore.editskill` documents the naming and parameter conventions shared across the editing toolkit, and re-exports this module's editing tools.
6
6
7
-
`diff_cells` compares two versions of a cell sequence -- an open notebook against its file on disk, a dialog against an edited copy -- and yields the edits that turn one into the other. Items are aligned by `id` (any objects with `.id` work, not just cells), and each edit is a *block*: a contiguous run of deletions, insertions, or in-place changes, so an applier can handle a whole run at once and inserted items keep their order. Identity questions ride the ids; whether an aligned pair *changed* is a content question, answered by comparing `key(item)` on each side.
7
+
`diff_cells` compares two versions of a cell sequence, such as an open notebook and its file on disk, or a dialog and an edited copy. It yields the edits that turn one into the other. It aligns items by `id`, and works with any objects that have an `.id`, not just cells. Each edit is a *block* covering a contiguous run of deletions, insertions or in-place changes. An applier can handle each block in one step, keeping inserted items in order. An aligned pair counts as changed when `key(item)` differs between the two sides.
0 commit comments