# Traitlets
[![Build Status](https://travis-ci.org/ipython/traitlets.svg?branch=master)](https://travis-ci.org/ipython/traitlets)
[![Documentation Status](https://readthedocs.org/projects/traitlets/badge/?version=latest)](https://traitlets.readthedocs.io/en/latest/?badge=latest)
| | |
|---------------|----------------------------------------|
| **home** | https://github.com/ipython/traitlets |
| **pypi-repo** | https://pypi.org/project/traitlets/ |
| **docs** | https://traitlets.readthedocs.io/ |
| **license** | Modified BSD License |
Traitlets is a pure Python library enabling:
- the enforcement of strong typing for attributes of Python objects
(typed attributes are called *"traits"*);
- dynamically calculated default values;
- automatic validation and coercion of trait attributes when attempting a
change;
- registering for receiving notifications when trait values change;
- reading configuring values from files or from command line
arguments - a distinct layer on top of traitlets, so you may use
traitlets without the configuration machinery.
Its implementation relies on the [descriptor](https://docs.python.org/howto/descriptor.html)
pattern, and it is a lightweight pure-python alternative of the
[*traits* library](http://code.enthought.com/pages/traits.html).
Traitlets powers the configuration system of IPython and Jupyter
and the declarative API of IPython interactive widgets.
## Installation
For a local installation, make sure you have
[pip installed](https://pip.pypa.io/en/stable/installing/) and run:
```bash
pip install traitlets
```
For a **development installation**, clone this repository, change into the
`traitlets` root directory, and run pip:
```bash
git clone https://github.com/ipython/traitlets.git
cd traitlets
pip install -e .
```
## Running the tests
```bash
pip install "traitlets[test]"
py.test traitlets
```
## Usage
Any class with trait attributes must inherit from `HasTraits`.
For the list of available trait types and their properties, see the
[Trait Types](https://traitlets.readthedocs.io/en/latest/trait_types.html)
section of the documentation.
### Dynamic default values
To calculate a default value dynamically, decorate a method of your class with
`@default({traitname})`. This method will be called on the instance, and
should return the default value. In this example, the `_username_default`
method is decorated with `@default('username')`:
```Python
import getpass
from traitlets import HasTraits, Unicode, default
class Identity(HasTraits):
username = Unicode()
@default('username')
def _username_default(self):
return getpass.getuser()
```
### Callbacks when a trait attribute changes
When a trait changes, an application can follow this trait change with
additional actions.
To do something when a trait attribute is changed, decorate a method with
[`traitlets.observe()`](https://traitlets.readthedocs.io/en/latest/api.html?highlight=observe#traitlets.observe).
The method will be called with a single argument, a dictionary which contains
an owner, new value, old value, name of the changed trait, and the event type.
In this example, the `_num_changed` method is decorated with ``@observe(`num`)``:
```Python
from traitlets import HasTraits, Integer, observe
class TraitletsExample(HasTraits):
num = Integer(5, help="a number").tag(config=True)
@observe('num')
def _num_changed(self, change):
print("{name} changed from {old} to {new}".format(**change))
```
and is passed the following dictionary when called:
```Python
{
'owner': object, # The HasTraits instance
'new': 6, # The new value
'old': 5, # The old value
'name': "foo", # The name of the changed trait
'type': 'change', # The event type of the notification, usually 'change'
}
```
### Validation and coercion
Each trait type (`Int`, `Unicode`, `Dict` etc.) may have its own validation or
coercion logic. In addition, we can register custom cross-validators
that may depend on the state of other attributes. For example:
```Python
from traitlets import HasTraits, TraitError, Int, Bool, validate
class Parity(HasTraits):
value = Int()
parity = Int()
@validate('value')
def _valid_value(self, proposal):
if proposal['value'] % 2 != self.parity:
raise TraitError('value and parity should be consistent')
return proposal['value']
@validate('parity')
def _valid_parity(self, proposal):
parity = proposal['value']
if parity not in [0, 1]:
raise TraitError('parity should be 0 or 1')
if self.value % 2 != parity:
raise TraitError('value and parity should be consistent')
return proposal['value']
parity_check = Parity(value=2)
# Changing required parity and value together while holding cross validation
with parity_check.hold_trait_notifications():
parity_check.value = 1
parity_check.parity = 1
```
However, we **recommend** that custom cross-validators don't modify the state
of the HasTraits instance.
没有合适的资源?快使用搜索试试~ 我知道了~
Jetbotmini依赖包
共142个文件
py:68个
md:13个
sample:11个
需积分: 2 0 下载量 32 浏览量
2023-03-10
11:44:59
上传
评论
收藏 300KB ZIP 举报
温馨提示
Adafruit-GPIO Adafruit-MotorHAT Adafruit-SSD1306 traitlets
资源推荐
资源详情
资源评论
收起资源包目录
Jetbotmini依赖包 (142个子文件)
make.bat 7KB
setup.cfg 102B
config 342B
description 73B
Adafruit_SSD1306-1.6.2-py3.6.egg 10KB
exclude 240B
.gitignore 695B
.gitignore 225B
.gitignore 48B
.gitignore 40B
HEAD 221B
HEAD 221B
HEAD 32B
HEAD 23B
pack-32f735d8016c986fb0611b1a22858b3d7acb77d3.idx 5KB
MANIFEST.in 374B
index 1KB
LICENSE 1KB
LICENSE 1KB
.mailmap 11KB
Makefile 7KB
master 221B
master 41B
README.md 5KB
COPYING.md 3KB
ISSUE_TEMPLATE.md 3KB
ISSUE_TEMPLATE.md 3KB
ISSUE_TEMPLATE.md 3KB
README.md 2KB
readme-docs.md 2KB
PULL_REQUEST_TEMPLATE.md 1KB
PULL_REQUEST_TEMPLATE.md 1KB
PULL_REQUEST_TEMPLATE.md 1KB
README.md 938B
README.md 639B
CONTRIBUTING.md 124B
pack-32f735d8016c986fb0611b1a22858b3d7acb77d3.pack 42KB
packed-refs 293B
PKG-INFO 2KB
happycat_oled_64.ppm 24KB
happycat_oled_32.ppm 12KB
traitlets.py 105KB
test_traitlets.py 70KB
FT232H.py 40KB
loader.py 34KB
application.py 32KB
test_application.py 24KB
test_configurable.py 22KB
configurable.py 19KB
GPIO.py 17KB
test_loader.py 16KB
SSD1306.py 15KB
SSD1306.py 15KB
SPI.py 14KB
test_traitlets_enum.py 13KB
conf.py 11KB
ez_setup.py 10KB
ez_setup.py 10KB
Adafruit_MotorHAT_Motors.py 9KB
I2C.py 9KB
test_GPIO.py 9KB
test_SPI.py 8KB
test_I2C.py 7KB
MCP230xx.py 6KB
buttons.py 6KB
PWM.py 6KB
github.py 5KB
descriptions.py 5KB
sphinxdoc.py 5KB
stats.py 5KB
test_decorators.py 5KB
PCA95xx.py 5KB
Platform.py 4KB
animate.py 4KB
shapes.py 4KB
_warnings.py 4KB
test_PWM.py 4KB
Adafruit_PWM_Servo_Driver.py 4KB
PCF8574.py 3KB
myapp.py 3KB
test_Platform.py 3KB
setup.py 3KB
decorators.py 3KB
getargspec.py 3KB
image.py 3KB
manager.py 2KB
MockGPIO.py 2KB
setup.py 1KB
setup.py 1KB
importstring.py 1KB
setup.py 1KB
utils.py 1KB
test_importstring.py 883B
log.py 779B
bunch.py 652B
__init__.py 639B
sentinel.py 524B
test_bunch.py 272B
__init__.py 203B
__init__.py 114B
共 142 条
- 1
- 2
资源评论
寅恪光潜
- 粉丝: 2w+
- 资源: 51
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- linux shell 脚本示例 升级用
- web开发课程设计/毕设-在线商城系统一个完整的在线商城网站,包括用户注册、商品展示、购物车、支付和订单管理
- 数据库课程设计/毕设-在线商城数据库系统支持在线购物的数据库系统
- Python 包管理器、程序打包工具、pip 镜像源设置工具、模块安装包下载器
- R语言数据分析-分析学生考试成绩计算每个学生的平均成绩,并将结果存储在新的列
- latex专业的排版系统生成高质量的科技和数学文档基于TeX排版系统增加了许多易用的功能和宏包
- Tomcat部署及优化
- github使用Git进行版本控制提供远程仓库存储代码支持多人协作
- matlab信号处理与分析控制系统设计PID控制器设计
- python宠物医院管理系统功能-添加新医生添加新主人预约就诊
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功