Skip to content

Commit

Permalink
feat(faq): add calculation between numeric unit values and numbers. (K…
Browse files Browse the repository at this point in the history
  • Loading branch information
Peefy authored Nov 9, 2022
1 parent 0cd7364 commit d1c8b0c
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
35 changes: 34 additions & 1 deletion docs/user_docs/support/faq-kcl.md
Original file line number Diff line number Diff line change
Expand Up @@ -2192,7 +2192,40 @@ a = 1 # immutable exported variable
_b = 2 # mutable non-export variable
```

## 48. How to develop a KCL plugin?
## 48. How to calculate the literal value of a numeric unit with a number

In KCL, numeric units exist as a special type, which does not allow arithmetic operations, because `str(1024Mi) == "1024Mi"` can be written directly, but when they are calculated, KCL compiler doesn't know what unit to take. For example, `1Ki + 1Mi` cannot determine whether the calculated result uses the unit of `Ki` or `Mi`. There is ambiguity here, so numerical units are directly prohibited from performing operations in KCL.

However, we can convert the numeric unit literal value into an integer through the `int()` function, and then call the functions in the units package or spliced them into the corresponding unit string.

For Example:

```python
import units
import math

val: units.NumberMultiplier = 2048Mi
ratio: float = 0.3
cpu: int | str = 1

res = {
cpu = str(int(int(cpu) * ratio * 1000)) + "m" # Convert int value to value with the unit 'm'
memory = units.to_Mi(int(int(val) * ratio)) # `memory = val * 0.3` with the unit 'Mi'
}
```

The output YAML is

```yaml
val: 2147483648.0
ratio: 0.3
cpu: 1
res:
cpu: 300m
memory: 614Mi
```
## 49. How to develop a KCL plugin?
KCL plugins are installed in the plugins subdirectory of KCLVM (usually installed in the `$HOME/.kusion/kclvm/plugins` directory), or set through the `$KCL_PLUGINS_ROOT` environment variable. For plugin developers, plugins are managed in the [Git repository](https://github.com/KusionStack/kcl-plugin), and the plugin repository can be cloned to this directory for development.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2206,7 +2206,42 @@ a = 1 # 不可变导出变量
_b = 2 # 可变非导出变量
```

## 48. 如何通过编写 KCL 插件进行扩展?
## 48. 如何将数字单位字面值与数字进行运算

在 KCL 中,数字单位作为一种特殊类型存在,它不允许算术运算,因为可以直接使用 `str(1024Mi)== "1024Mi"` 方式进行单位转换并直接输出,但是当它们进行计算后,KCL 无法为这个数字取一定的单位标准,比如 `1Ki + 1Mi` 无法确定计算后的结果使用单位 `Ki` 还是 `Mi`, 这里存在二义性,因此在 KCL 中直接禁止了数字单位进行运算。

但是,我们可以通过 `int()` 函数将数字单位字面值转换为整数并进行计算,然后使用 `units` 系统模块中的函数将它们转换为相应的数字单位字符串。

比如

```kcl
import units
import math
val: units.NumberMultiplier = 2048Mi
ratio: float = 0.3
cpu: int | str = 1
res = {
cpu = str(int(int(cpu) * ratio * 1000)) + "m" # Convert int value to value with the unit 'm' and calculate `cpu = cpu * radio`
memory = units.to_Mi(int(int(val) * ratio)) # `memory = val * radio` with the unit 'Mi'
}
```

输出为:

```yaml
val: 2147483648.0
ratio: 0.3
cpu: 1
res:
cpu: 300m
memory: 614Mi
```
## 49. 如何通过编写 KCL 插件进行扩展?
KCL 插件在 KCLVM 的 plugins 子目录(通常安装在 `$HOME/.kusion/kclvm/plugins` 目录),或者通过 `$KCL_PLUGINS_ROOT` 环境变量设置(环境变量优先级更高)。对于插件开发人员,插件都在 [Git 仓库](https://github.com/KusionStack/kcl-plugin)管理,可以将插件仓库克隆到该目录进行开发。

Expand Down

0 comments on commit d1c8b0c

Please sign in to comment.