Skip to content

Commit a4b9569

Browse files
authored
Merge pull request #484 from kcl-lang/docs-more-system-package-examples
docs: add more system package examples
2 parents 8ce7ee6 + 076aab7 commit a4b9569

File tree

64 files changed

+2458
-36
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+2458
-36
lines changed

blog/2023-02-27-kcl-0.4.5-release-blog/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ config: Config {
222222

223223
Before KCL v0.4.5, executing the above code (main.k) will get unexpected configuration values because the KCL compiler incorrectly optimized the following form of equivalent merge configuration blocks:
224224

225-
```python3
225+
```python
226226
config: Config {
227227
resource: r
228228
resource: Resource {

docs/reference/model/base64.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,20 @@ weight: 100
1212

1313
Encode the string `value` using the codec registered for encoding.
1414

15+
```python
16+
import base64
17+
18+
abcd_base64 = base64.encode("abcd")
19+
```
20+
1521
## decode
1622

1723
`decode(value: str) -> str`
1824

1925
Decode the string `value` using the codec registered to the utf8 string for encoding.
26+
27+
```python
28+
import base64
29+
30+
decode = base64.decode("MC4zLjA=")
31+
```

docs/reference/model/crypto.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,50 +12,104 @@ weight: 100
1212

1313
Encrypt the string `value` using `MD5` and the codec registered for encoding.
1414

15+
```python
16+
import crypto
17+
18+
md5 = crypto.md5("ABCDEF")
19+
```
20+
1521
## sha1
1622

1723
`sha1(value: str, encoding: str = "utf-8") -> str`
1824

1925
Encrypt the string `value` using `SHA1` and the codec registered for encoding.
2026

27+
```python
28+
import crypto
29+
30+
sha = crypto.sha1("ABCDEF")
31+
```
32+
2133
## sha224
2234

2335
`sha224(value: str, encoding: str = "utf-8") -> str`
2436

2537
Encrypt the string `value` using `SHA224` and the codec registered for encoding.
2638

39+
```python
40+
import crypto
41+
42+
sha = crypto.sha224("ABCDEF")
43+
```
44+
2745
## sha256
2846

2947
`sha256(value: str, encoding: str = "utf-8") -> str`
3048

3149
Encrypt the string `value` using `SHA256` and the codec registered for encoding.
3250

51+
```python
52+
import crypto
53+
54+
sha = crypto.sha256("ABCDEF")
55+
```
56+
3357
## sha384
3458

3559
`sha384(value: str, encoding: str = "utf-8") -> str`
3660

3761
Encrypt the string `value` using `SHA384` and the codec registered for encoding.
3862

63+
```python
64+
import crypto
65+
66+
sha = crypto.sha384("ABCDEF")
67+
```
68+
3969
## sha512
4070

4171
`sha512(value: str, encoding: str = "utf-8") -> str`
4272

4373
Encrypt the string `value` using `SHA512` and the codec registered for encoding.
4474

75+
```python
76+
import crypto
77+
78+
sha = crypto.sha512("ABCDEF")
79+
```
80+
4581
## blake3
4682

4783
`blake3(value: str, encoding: str = "utf-8") -> str`
4884

4985
Encrypt the string `value` using `BLAKE3` and the codec registered for encoding.
5086

87+
```python
88+
import crypto
89+
90+
blake3 = crypto.blake3("ABCDEF")
91+
```
92+
5193
## uuid
5294

5395
`uuid() -> str`
5496

5597
Generate a random UUID string.
5698

99+
```python
100+
import crypto
101+
102+
a = crypto.uuid()
103+
```
104+
57105
## filesha256
58106

59107
`filesha256(filepath: str) -> str`
60108

61109
Calculate the SHA256 hash of the file `filepath`.
110+
111+
```python
112+
import crypto
113+
114+
sha = crypto.filesha256("test.txt")
115+
```

docs/reference/model/datetime.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,56 @@ weight: 100
1212

1313
Return the current time in seconds since the Epoch. Fractions of a second may be present if the system clock provides them.
1414

15+
```python
16+
import datetime
17+
18+
ticks = datetime.ticks()
19+
```
20+
1521
## date
1622

1723
`date() -> str`
1824

1925
Return the `%Y-%m-%d %H:%M:%S` format date.
2026

27+
```python
28+
import datetime
29+
30+
date = datetime.date()
31+
```
32+
2133
## now
2234

2335
`now(format: str = "%a %b %d %H:%M:%S %Y") -> str`
2436

2537
Return the local time format. e.g. 'Sat Jun 06 16:26:11 1998' or format the combined date and time per the specified format string, and the default date format is `%a %b %d %H:%M:%S %Y`.
2638

39+
```python
40+
import datetime
41+
42+
date = datetime.now()
43+
```
44+
2745
## today
2846

2947
`today() -> str`
3048

3149
Return the `%Y-%m-%d %H:%M:%S.%{ticks}` format date.
3250

51+
```python
52+
import datetime
53+
54+
date = datetime.today()
55+
```
56+
3357
## validate
3458

3559
`validate(date: str, format: str) -> bool`
3660

3761
Validate whether the provided date string matches the specified format.
62+
63+
```python
64+
import datetime
65+
66+
result = datetime.validate("2024-08-26", "%Y-%m-%d") # Valid date
67+
```

docs/reference/model/file.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,68 +12,164 @@ weight: 100
1212

1313
Read the contents of the file `filepath` and return a string instance.
1414

15+
```python
16+
import file
17+
18+
a = file.read("test.txt")
19+
```
20+
1521
## glob
1622

1723
`glob(pattern: str) -> str`
1824

1925
Return a list containing all file names that match `pattern`.
2026

27+
```python
28+
import file
29+
30+
json_files = file.glob("./*.json")
31+
```
32+
2133
## modpath
2234

2335
`modpath() -> str`
2436

2537
Return the root path of the current KCL module (kcl.mod file path or single \*.k file path).
2638

39+
```python
40+
import file
41+
42+
modpath = file.modpath()
43+
```
44+
2745
## workdir
2846

2947
`workdir() -> str`
3048

3149
Return the path of the current working directory.
3250

51+
```python
52+
import file
53+
54+
workdir = file.workdir()
55+
```
56+
3357
## exists
3458

3559
`exists(filepath: str) -> bool`
3660

3761
Whether this file path exists. Returns true if the path points at an existing entity. This function will traverse symbolic links to query information about the destination file.
3862

63+
```python
64+
import file
65+
66+
file_exists = file.exists("test.txt")
67+
```
68+
3969
## abs
4070

4171
`abs(filepath: str) -> str`
4272

4373
Returns the canonical, absolute form of the path with all intermediate components normalized and symbolic links resolved.
4474

75+
```python
76+
import file
77+
78+
abs_file_path = file.abs("test.txt")
79+
```
80+
4581
## mkdir
4682

4783
`mkdir(directory: str, exists: bool=False)`
4884

4985
Create a new directory at the specified path if it doesn't already exist.
5086

87+
```python
88+
import file
89+
90+
file.mkdir("path")
91+
```
92+
5193
## delete
5294

5395
`delete(directory: str)`
5496

5597
Delete a file or an empty directory at the specified path.
5698

99+
```python
100+
import file
101+
102+
file.delete("test.txt")
103+
```
104+
57105
## cp
58106

59107
`cp(src: str, dest: str)`
60108

61109
Copy a file or directory from the source path to the destination path.
62110

111+
```python
112+
import file
113+
114+
file.cp("src", "dest")
115+
```
116+
63117
## mv
64118

65119
`mv(src: str, dest: str)`
66120

67121
Move a file or directory from the source path to the destination path.
68122

123+
```python
124+
import file
125+
126+
file.mv("src", "dest")
127+
```
128+
129+
## size
130+
131+
`size(filepath: str)`
132+
133+
Get the size of a file at the specified path.
134+
135+
```python
136+
import file
137+
138+
size = file.size("test.txt")
139+
```
140+
141+
## write
142+
143+
`write(filepath: str, content: str)`
144+
145+
Write content to a file at the specified path. If the file doesn't exist, it will be created. If it does exist, its content will be replaced.
146+
147+
```python
148+
import file
149+
150+
file.size("test.txt", "content")
151+
```
152+
69153
## read_env
70154

71155
`read_env(key: str) -> str`
72156

73157
Read the environment variable `key` from the current process.
74158

159+
```python
160+
import file
161+
162+
value = file.read_env("ENV_VAR")
163+
```
164+
75165
## current
76166

77167
`current() -> str`
78168

79169
Read the path of the current script or module that is being executed.
170+
171+
```python
172+
import file
173+
174+
value = file.current()
175+
```

0 commit comments

Comments
 (0)