agronholm / agronholm/cbor2

Add cbor2.tool option to generate bytes from python literal

未關閉
#103 1 則留言 1 個 reaction 已指派 0 人 在 GitHub 檢視
enhancement
主要語言
Rust
星號
305
分支
79
平均合併
21 小時 59 分鐘
30 天內合併 PR
2

描述

I use cbor2 to parse and generate test sequences for my application. When developing, it is sometimes useful to create one-off cbor sequences to test an application. Therefore, it would be great if the cbor2.tool was extended to convert python literals to cbor data.

Here is a potential example of how it could be implemented:

```python
from cbor2 import dumps
import ast
import sys

# parses a string as a python literal, and then converts to cbor and prints the bytes
def dumps_literal(literal):
cbor_val = ast.literal_eval(literal)
sys.stdout.buffer.write(dumps(cbor_val))

if __name__ == "__main__":
dumps_literal(sys.argv[1])

# Example output:
#
# $ python3 -m write_cbor '{"Hello" : ["w", "o", "r", "l","d"]}' | xxd -p
# a16548656c6c6f856177616f6172616c6164
#
# $ python3 -m write_cbor '("abc", 1,2,3)' | xxd -p
# 8463616263010203
```

Thoughts?

貢獻指南

這個儲存庫沒有索引到貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。