Skip to content

Commit 8151b7a

Browse files
committed
add module utfyaml
1 parent e6d7062 commit 8151b7a

File tree

6 files changed

+831
-0
lines changed

6 files changed

+831
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ There is a `README.md` for each module.
7272
| [threadutil](threadutil) | Utility functions for better management of threads |
7373
| [timeutil](timeutil) | Support specify time format output and get current ts, ms, us api etc |
7474
| [utfjson](utfjson) | Force `json.dump` and `json.load` in `utf-8` encoding |
75+
| [utfyaml](utfyaml) | Force `yaml.dump` and `yaml.load` in `utf-8` encoding |
7576
| [wsjobd](wsjobd) | Job daemon based on websocket protocol |
7677
| [zkutil](zkutil) | Utility functions for zookeeper |
7778

utfyaml/README.md

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
2+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
3+
# Table of Content
4+
5+
- [Name](#name)
6+
- [Status](#status)
7+
- [Synopsis](#synopsis)
8+
- [Description](#description)
9+
- [Methods](#methods)
10+
- [utfyaml.dump](#utfyamldump)
11+
- [utfyaml.load](#utfyamlload)
12+
- [Author](#author)
13+
- [Copyright and License](#copyright-and-license)
14+
15+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
16+
17+
# Name
18+
19+
utfyaml: force `yaml.dump` and `yaml.load` in `utf-8` encoding.
20+
21+
# Status
22+
23+
This library is considered production ready.
24+
25+
# Synopsis
26+
27+
```
28+
from pykit import utfyaml
29+
30+
utfyaml.load(
31+
'''
32+
key: value
33+
number: 1
34+
float: 3.14
35+
boolean: True
36+
汉字: 我
37+
'''
38+
)
39+
40+
#{
41+
# 'key': 'value',
42+
# 'number': 1,
43+
# 'float': 3.14,
44+
# 'boolean': True,
45+
# '汉字': '我',
46+
#}
47+
48+
utfyaml.dump(
49+
{
50+
'key': 'value',
51+
'number': 1,
52+
'float': 3.14,
53+
'boolean': True,
54+
'汉字': '我',
55+
})
56+
57+
#'''
58+
#key: value
59+
#number: 1
60+
#float: 3.14
61+
#boolean': True
62+
#汉字: 我
63+
#'''
64+
65+
utfyaml.dump(
66+
{
67+
'key': 'value',
68+
'number': 1,
69+
'float': 3.14,
70+
'boolean': True,
71+
'汉字': '我',
72+
u'unicode': 'hello, 中国',
73+
}, encoding='GBK', save_unicode=True)
74+
75+
#"""
76+
#key: value
77+
#number: 1
78+
#float: 3.14
79+
#boolean: True
80+
#\xba\xba\xd7\xd6: \xce\xd2
81+
#!!python/unicode 'unicode': !!python/unicode 'hello, \xd6\xd0\xb9\xfa'
82+
#"""
83+
```
84+
85+
# Description
86+
87+
Load a string with yaml format to a python instance in `utf-8` encoding
88+
and dump a python instance to yaml format string in `utf-8` encoding.
89+
90+
# Methods
91+
92+
## utfyaml.dump
93+
94+
**syntax**:
95+
`utfyaml.dump(py_instance, encoding='utf-8', save_unicode=False)`
96+
97+
Dump a python instance to a string with yaml format.
98+
99+
**arguments**:
100+
101+
- `py_instance`:
102+
a python instance to be dumped to a yaml format string.
103+
104+
- `encoding`:
105+
specifies in which encoding the result string is to be.
106+
By default, it is `'utf-8'`.
107+
If it is splecified and is `None`, means no need to encode
108+
and result string will be in `unicode`.
109+
110+
- `save_unicode`:
111+
specifies if to dump `unicode` with tag `!!python/unicode`.
112+
By default, it is `False`.
113+
114+
**return**:
115+
a string as the dump result of `py_instance` with yaml format.
116+
117+
## utfyaml.load
118+
119+
**syntax**:
120+
`utfyaml.load(yaml_string, encoding='utf-8')`
121+
122+
Load a string with yaml format to a python instance.
123+
124+
**arguments**:
125+
126+
- `yaml_string`:
127+
a string with yaml format to be loaded to a python instance.
128+
129+
- `encoding`:
130+
specifies in which encoding the strings in the result is to be.
131+
By default, it is `'utf-8'`.
132+
If it is specified and is `None`, means that no need to encode
133+
strings in the result, which will be in `unicode`.
134+
135+
**rerutn**:
136+
the python instance `yaml_stirng` specified.
137+
138+
# Author
139+
140+
Li Wenbo (李文博) <[email protected]>
141+
142+
143+
# Copyright and License
144+
145+
The MIT License (MIT)
146+
147+
Copyright (c) 2018 Li Wenbo (李文博) <[email protected]>

utfyaml/__init__.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from .utfyaml import (
2+
dump,
3+
load,
4+
)
5+
6+
__all__ = [
7+
'dump',
8+
'load',
9+
]

utfyaml/test/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)