> ## Documentation Index
> Fetch the complete documentation index at: https://docs.chaintable.com/llms.txt
> Use this file to discover all available pages before exploring further.

# chaintable-py

> 运行参数、账户变量、常量和通用工具的 Python 接口参考。

`chaintable-py` 提供驱动脚本常用的配置与工具接口。[源码](https://github.com/Chaintable/chaintable-py)中有完整实现。

## 运行参数

```python theme={null}
from chaintable import arguments

args = arguments.get_arguments()
raw_amount = int(args.get('raw_amount', '1500000'))
print(raw_amount / 10 ** 6)
```

### `arguments.get_arguments()`

无参数。读取 `--name=value` 形式的命令行参数，返回按输入顺序组织的字典，键和值均为字符串。没有匹配参数时返回空字典。

Notebook 会把页面配置的运行参数传给脚本。数值、布尔值或 JSON 需要由脚本显式转换，操作见[运行管理](/zh/guides/notebooks/manage-runs)。

## 账户运行变量

```python theme={null}
from chaintable import variables

network = variables.get('DOCS_NETWORK', 'eth')
```

### `variables.get(name, default=None)`

| 参数        | 类型             | 说明                   |
| --------- | -------------- | -------------------- |
| `name`    | `str`          | 账户变量名称               |
| `default` | `str` 或 `None` | 名称不存在时的返回值，默认 `None` |

返回变量的字符串值，或缺失时的默认值。通过执行环境读取当前账户变量，每次调用重新查询。需要 Chaintable 运行代理；本地仅安装包时不能读取账户变量。

变量不存在不抛异常；服务或代理调用失败时可能抛出 `variables.VariableError`，缺少运行代理配置时会报运行环境错误。创建步骤见[读取运行变量](/zh/guides/notebooks/advanced#读取运行变量)。

## 通用工具

```python theme={null}
from chaintable import utils

print(utils.is_address('0xd8da6bf26964af9d7eed9e03e53415d37aa96045'))
print(list(utils.chunked([1, 2, 3], 2)))
```

输出为 `True` 和 `[(1, 2), (3,)]`。

| 接口                                   | 参数                             | 返回值               |
| ------------------------------------ | ------------------------------ | ----------------- |
| `utils.hash_md5(*strings)`           | 多个字符串或 `None`；按顺序拼接，`None` 视为空 | 32 位十六进制 MD5 字符串  |
| `utils.chunked(iterable, size)`      | 可迭代对象与每组大小，`size` 至少为 1        | 逐组返回元组的迭代器，末组可能较短 |
| `utils.is_hexstr(s, bytes_len=None)` | `0x` 开头的十六进制字符串；可指定字节长度        | `bool`            |
| `utils.is_digitstr(s)`               | 检查值是否能转换为 Python 整数            | `bool`            |
| `utils.is_address(value)`            | 检查小写 `0x` 加 40 位十六进制字符         | `bool`            |

`is_address()` 只检查格式，不查询链上账户；混合大小写的校验和地址不符合此工具的小写格式要求。

`hash_md5()` 不在输入之间插入分隔符。例如 `('ab', 'c')` 和 `('a', 'bc')` 会产生相同的拼接输入。

## 常量

`chaintable.consts.chain` 和 `chaintable.consts.address` 提供链与地址相关常量，完整列表见[源码中的 consts](https://github.com/Chaintable/chaintable-py/tree/main/chaintable/consts)。
