# Welcome to fastcore
> Python goodies to make your coding faster, easier, and more maintainable
Python is a powerful, dynamic language. Rather than bake everything into the language, it lets the programmer customize it to make it work for them. `fastcore` uses this flexibility to add to Python features inspired by other languages we've loved, 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.
## Installing
To install fastcore run: `conda install fastcore` (if you use Anaconda, which we strongly recommend) or `pip install fastcore`. For an [editable install](https://stackoverflow.com/questions/35064426/when-would-the-e-editable-option-be-useful-with-pip-install), clone this repo and run: `pip install -e ".[dev]"`.
## A tour
`fastcore` contains many features. See the [docs](https://fastcore.fast.ai) for all the details, which cover the modules provided:
- `test`: Simple testing functions
- `foundation`: Mixins, delegation, composition, and more
- `utils`: Utility functions to help with functional-style programming, parallel processing, and more
- `dispatch`: Multiple dispatch methods
- `transform`: Pipelines of composed partially reversible transformations
Here's a (somewhat) quick tour of a few higlights, showing examples from each of these modules.
### Documentation
All fast.ai projects, including this one, are built with [nbdev](https://nbdev.fast.ai), which is a full literate programming environment built on Jupyter Notebooks. That means that every piece of documentation, including the page you're reading now, can be accessed as interactive Jupyter notebooks. In fact, you can even grab a link directly to a notebook running interactively on Google Colab - if you want to follow along with this tour, click the link below, or click the badge at the top of the page:
```
colab_link('index')
```
[Open `index` in Colab](https://colab.research.google.com/github/fastai/fastcore/blob/master/nbs/index.ipynb)
The full docs are available at [fastcore.fast.ai](https://fastcore.fast.ai). The code in the examples and in all fast.ai libraries follow the [fast.ai style guide](https://docs.fast.ai/dev/style.html). In order to support interactive programming, all fast.ai libraries are designed to allow for `import *` to be used safely, particular by ensuring that [`__all__`](https://riptutorial.com/python/example/2894/the---all---special-variable) is defined in all packages. In order to see where a function is from, just type it:
```
coll_repr
```
<function fastcore.foundation.coll_repr(c, max_n=10)>
For more details, including a link to the full documentation and source code, use `doc`, which pops up a window with this information:
```python
doc(coll_repr)
```
<img width="499" src="nbs/images/att_00000.png" align="left">
The documentation also contains links to any related functions or classes, which appear like this: `coll_repr` (in the notebook itself you will just see a word with back-ticks around it; the links are auto-generated in the documentation site). The documentation will generally show one or more examples of use, along with any background context necessary to understand them. As you'll see, the examples for each function and method are shown as tests, rather than example outputs, so let's start by explaining that.
### Testing
fastcore's testing module is designed to work well with [nbdev](https://nbdev.fast.ai), which is a full literate programming environment built on Jupyter Notebooks. That means that your tests, docs, and code all live together in the same notebook. fastcore and nbdev's approach to testing starts with the premise that all your tests should pass. If one fails, no more tests in a notebook are run.
Tests look like this:
```
test_eq(coll_repr(range(1000), 5), '(#1000) [0,1,2,3,4...]')
```
That's an example from the docs for `coll_repr`. As you see, it's not showing you the output directly. Here's what that would look like:
```
coll_repr(range(1000), 5)
```
'(#1000) [0,1,2,3,4...]'
So, the test is actually showing you what the output looks like, because if the function call didn't return `'(#1000) [0,1,2,3,4...]'`, then the test would have failed.
So every test shown in the docs is also showing you the behavior of the library --- and vice versa!
Test functions always start with `test_`, and then follow with the operation being tested. So `test_eq` tests for equality (as you saw in the example above). This includes tests for equality of arrays and tensors, lists and generators, and many more:
```
test_eq([0,1,2,3], np.arange(4))
```
When a test fails, it prints out information about what was expected:
```python
test_eq([0,1,2,3], np.arange(3))
```
```
----
AssertionError: ==:
[0, 1, 2, 3]
[0 1 2]
```
If you want to check that objects are the same type, rather than the just contain the same collection, use `test_eq_type`.
You can test with any comparison function using `test`, e.g test whether an object is less than:
```
test(2, 3, operator.lt)
```
You can even test that exceptions are raised:
```
def divide_zero(): return 1/0
test_fail(divide_zero)
```
...and test that things are printed to stdout:
```
test_stdout(lambda: print('hi'), 'hi')
```
### Foundations
fast.ai is unusual in that we often use [mixins](https://en.wikipedia.org/wiki/Mixin) in our code. Mixins are widely used in many programming languages, such as Ruby, but not so much in Python. We use mixins to attach new behavior to existing libraries, or to allow modules to add new behavior to our own classes, such as in extension modules. One useful example of a mixin we define is `Path.ls`, which lists a directory and returns an `L` (an extended list class which we'll discuss shortly):
```
p = Path('images')
p.ls()
```
(#6) [Path('images/att_00007.png'),Path('images/att_00000.png'),Path('images/puppy.jpg'),Path('images/att_00006.png'),Path('images/mnist3.png'),Path('images/att_00005.png')]
You can easily add you own mixins with the `patch` [decorator](https://realpython.com/primer-on-python-decorators/), which takes advantage of Python 3 [function annotations](https://www.python.org/dev/peps/pep-3107/#parameters) to say what class to patch:
```
@patch
def num_items(self:Path): return len(self.ls())
p.num_items()
```
6
We also use `**kwargs` frequently. In python `**kwargs` in a parameter like means "*put any additional keyword arguments into a dict called `kwargs`*". Normally, using `kwargs` makes an API quite difficult to work with, because it breaks things like tab-completion and popup lists of signatures. `utils` provides `use_kwargs` and `delegates` to avoid this problem. See our [detailed article on delegation](https://www.fast.ai/2019/08/06/delegation/) on this topic.
`GetAttr` solves a similar problem (and is also discussed in the article linked above): it's allows you to use Python's exceptionally useful `__getattr__` magic method, but avoids the problem that normally in Python tab-completion and docs break when using this. For instance, you can see here that Python's `dir` function, which is used to find the attributes of a python object, finds everything inside the `self.default` attribute here:
```
class Author:
def __init__(self, name): self.name = name
class ProductPage(GetAttr):
_default = 'author'
def __init__(self,author,price,cost): self.author,self.price,self.cost = author,price,cost
p = ProductPage(Author("Jeremy"), 1.50, 0.50)
[o for o in dir(p) if not o.startswith('_')]
```
['author', 'cost', 'name', 'price']
Looking at that `ProductPage` example, it's rather verbose and duplicates a lot of attribute names, which can lead to bugs later if you change them only in one place. `fastcore` provides `sto
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
共30个文件
py:16个
txt:5个
md:2个
资源分类:Python库 所属语言:Python 资源全名:fastcore-1.2.5.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源推荐
资源详情
资源评论
收起资源包目录
fastcore-1.2.5.tar.gz (30个子文件)
fastcore-1.2.5
MANIFEST.in 111B
PKG-INFO 19KB
fastcore.egg-info
PKG-INFO 19KB
requires.txt 72B
not-zip-safe 1B
SOURCES.txt 585B
entry_points.txt 20B
top_level.txt 9B
dependency_links.txt 1B
settings.ini 639B
LICENSE 11KB
CONTRIBUTING.md 2KB
setup.cfg 38B
setup.py 3KB
fastcore
imports.py 3KB
logargs.py 3KB
utils.py 87B
test.py 3KB
xtras.py 14KB
basics.py 21KB
meta.py 5KB
dispatch.py 8KB
__init__.py 22B
_nbdev.py 9KB
all.py 227B
foundation.py 15KB
transform.py 9KB
script.py 3KB
nb_imports.py 197B
README.md 15KB
共 30 条
- 1
资源评论
挣扎的蓝藻
- 粉丝: 13w+
- 资源: 15万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功