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
Copy file name to clipboardExpand all lines: nbs/llms-ctx-full.txt
+3-267Lines changed: 3 additions & 267 deletions
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
<project title="fastcore">
2
-
fastcore adds to Python features inspired by other languages, like multiple dispatch from Julia, mixins from Ruby, and currying, binding, and more from Haskell. It also adds some “missing features” and clean up some rough edges in the Python standard library, such as simplifying parallel processing, and bringing ideas from NumPy over to Python’s list type.
2
+
fastcore adds to Python features inspired by other languages, like mixins from Ruby, and currying, binding, and more from Haskell. It also adds some “missing features” and clean up some rough edges in the Python standard library, such as simplifying parallel processing, and bringing ideas from NumPy over to Python’s list type.
3
3
4
4
Here are some tips on using fastcore:
5
5
@@ -12,7 +12,6 @@ Here are some tips on using fastcore:
12
12
- **Expressive testing**: Prefer fastcore's testing functions like `test_eq`, `test_ne`, `test_close` for more readable and informative test assertions.
13
13
- **Advanced file operations**: Use the extended `Path` class, which adds methods like `ls()`, `read_json()`, and others to `pathlib.Path`.
14
14
- **Flexible data structures**: Convert between dictionaries and attribute-access objects using `dict2obj` and `obj2dict` for more intuitive data handling.
15
-
- **Data pipeline construction**: Employ `Transform` and `Pipeline` classes to create modular, composable data processing workflows.
16
15
- **Functional programming paradigms**: Utilize tools like `compose`, `maps`, and `filter_ex` to write more functional-style Python code.
17
16
- **Documentation**: Use `docments` where possible to document parameters of functions and methods.
18
17
- **Time-aware caching**: Apply the `timed_cache` decorator to add time-based expiration to the standard `lru_cache` functionality.
@@ -364,77 +363,8 @@ in `list` in the first place, such as making this do what you’d expect
364
363
365
364
(#4) [1,2,3,4]
366
365
367
-
### Transforms
368
-
369
-
A [`Transform`](https://fastcore.fast.ai/transform.html#transform) is
370
-
the main building block of the fastai data pipelines. In the most
371
-
general terms a transform can be any function you want to apply to your
372
-
data, however the
373
-
[`Transform`](https://fastcore.fast.ai/transform.html#transform) class
374
-
provides several mechanisms that make the process of building them easy
375
-
and flexible (see the docs for information about each of these):
Type dispatch, orMultiple dispatch, allows you to change the way a function behaves based upon the input types it receives. This is a prominent feature in some programming languages like Julia. For example, this is a conceptual example of how multiple dispatch works in Julia, returning different values depending on the input types of x and y:
633
-
634
-
```
635
-
collide_with(x::Asteroid, y::Asteroid) = ...
636
-
# deal with asteroid hitting asteroid
637
-
638
-
collide_with(x::Asteroid, y::Spaceship) = ...
639
-
# deal with asteroid hitting spaceship
640
-
641
-
collide_with(x::Spaceship, y::Asteroid) = ...
642
-
# deal with spaceship hitting asteroid
643
-
644
-
collide_with(x::Spaceship, y::Spaceship) = ...
645
-
# deal with spaceship hitting spaceship
646
-
647
-
```
648
-
649
-
Type dispatch can be especially useful in data science, where you might allow different input types (i.e. Numpy arrays and Pandas dataframes) to a function that processes data. Type dispatch allows you to have a common API for functions that do similar tasks.
650
-
651
-
Unfortunately, Python does not support this out-of-the box. Fortunately, there is the @typedispatch decorator to the rescue. This decorator relies upon type hints in order to route inputs the correct version of the function:
652
-
653
-
```
654
-
@typedispatch
655
-
def f(x:str, y:str): return f'{x}{y}'
656
-
657
-
@typedispatch
658
-
def f(x:np.ndarray): return x.sum()
659
-
660
-
@typedispatch
661
-
def f(x:int, y:int): return x+y
662
-
663
-
```
664
-
665
-
Below is a demonstration of type dispatch at work for the function `f`:
666
-
667
-
```
668
-
f('Hello ', 'World!')
669
-
670
-
```
671
-
672
-
```
673
-
'Hello World!'
674
-
```
675
-
676
-
```
677
-
f(2,3)
678
-
679
-
```
680
-
681
-
```
682
-
5
683
-
```
684
-
685
-
```
686
-
f(np.array([5,5,5,5]))
687
-
688
-
```
689
-
690
-
```
691
-
20
692
-
```
693
-
694
-
There are limitations of this feature, as well as other ways of using this functionality that you can read about here. In the process of learning about typed dispatch, I also found a python library called multipledispatch made by Mathhew Rocklin (the creator of Dask).
695
-
696
-
After using this feature, I am now motivated to learn languages like Julia to discover what other paradigms I might be missing.
697
-
698
-
* * *
699
-
700
560
## A better version of functools.partial __
701
561
702
562
`functools.partial` is a great utility that creates functions from other functions that lets you set default values. Lets take this function for example that filters a list to only contain values >= `val`:
@@ -1050,10 +910,6 @@ Thefunctional programming section is my favorite part of this library.
1050
910
* mapped: A more robust `map`
1051
911
* using_attr: compose a function that operates on an attribute
1052
912
1053
-
## Transforms __
1054
-
1055
-
Transforms is a collection of utilities for creating data transformations and associated pipelines. These transformation utilities build upon many of the building blocks discussed in this blog post.
1056
-
1057
913
## Further Reading __
1058
914
1059
915
**It should be noted that you should read themain page of the docs first, followed by the section on tests to fully understand the documentation.**
@@ -1493,60 +1349,6 @@ This blog post was written entirely in a Jupyter Notebook, which GitHub automati
1493
1349
- `def str2bool(s)`
1494
1350
Case-insensitive convert string `s` too a bool (`y`,`yes`,`t`,`true`,`on`,`1`->`True`)
1495
1351
1496
-
## fastcore.dispatch
1497
-
1498
-
> Basic single and dual parameter dispatch
1499
-
1500
-
- `def lenient_issubclass(cls, types)`
1501
-
If possible return whether `cls` is a subclass of `types`, otherwise return False.
1502
-
1503
-
- `def sorted_topologically(iterable)`
1504
-
Return a new list containing all items from the iterable sorted topologically
1505
-
1506
-
- `class TypeDispatch`
1507
-
Dictionary-like object; `__getitem__` matches keys of types using `issubclass`
1508
-
1509
-
- `def __init__(self, funcs, bases)`
1510
-
- `def add(self, f)`
1511
-
Add type `t` and function `f`
1512
-
1513
-
- `def first(self)`
1514
-
Get first function in ordered dict of type:func.
1515
-
1516
-
- `def returns(self, x)`
1517
-
Get the return type of annotation of `x`.
1518
-
1519
-
- `def __repr__(self)`
1520
-
- `def __call__(self, *args, **kwargs)`
1521
-
- `def __get__(self, inst, owner)`
1522
-
- `def __getitem__(self, k)`
1523
-
Find first matching type that is a super-class of `k`
1524
-
1525
-
1526
-
- `class DispatchReg`
1527
-
A global registry for `TypeDispatch` objects keyed by function name
1528
-
1529
-
- `def __init__(self)`
1530
-
- `def __call__(self, f)`
1531
-
1532
-
- `def retain_meta(x, res, as_copy)`
1533
-
Call `res.set_meta(x)`, if it exists
1534
-
1535
-
- `def default_set_meta(self, x, as_copy)`
1536
-
Copy over `_meta` from `x` to `res`, if it's missing
1537
-
1538
-
- `@typedispatch def cast(x, typ)`
1539
-
cast `x` to type `typ` (may also change `x` inplace)
1540
-
1541
-
- `def retain_type(new, old, typ, as_copy)`
1542
-
Cast `new` to type of `old` or `typ` if it's a superclass
1543
-
1544
-
- `def retain_types(new, old, typs)`
1545
-
Cast each item of `new` to type of matching item in `old` if it's a superclass
1546
-
1547
-
- `def explode_types(o)`
1548
-
Return the type of `o`, potentially in nested dictionaries for thing that are listy
1549
-
1550
1352
## fastcore.docments
1551
1353
1552
1354
> Document parameters using comments.
@@ -2056,72 +1858,6 @@ This blog post was written entirely in a Jupyter Notebook, which GitHub automati
2056
1858
- `def __enter__(self)`
2057
1859
- `def __exit__(self, type, value, traceback)`
2058
1860
2059
-
## fastcore.transform
2060
-
2061
-
> Definition of `Transform` and `Pipeline`
2062
-
2063
-
- `class Transform`
2064
-
Delegates (`__call__`,`decode`,`setup`) to (<code>encodes</code>,<code>decodes</code>,<code>setups</code>) if `split_idx` matches
0 commit comments