Skip to content

Commit

Permalink
docs: add more system package examples
Browse files Browse the repository at this point in the history
Signed-off-by: Peefy <[email protected]>
  • Loading branch information
Peefy committed Nov 7, 2024
1 parent f9d28a0 commit 076aab7
Show file tree
Hide file tree
Showing 64 changed files with 2,458 additions and 36 deletions.
2 changes: 1 addition & 1 deletion blog/2023-02-27-kcl-0.4.5-release-blog/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ config: Config {

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:

```python3
```python
config: Config {
resource: r
resource: Resource {
Expand Down
12 changes: 12 additions & 0 deletions docs/reference/model/base64.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,20 @@ weight: 100

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

```python
import base64

abcd_base64 = base64.encode("abcd")
```

## decode

`decode(value: str) -> str`

Decode the string `value` using the codec registered to the utf8 string for encoding.

```python
import base64

decode = base64.decode("MC4zLjA=")
```
54 changes: 54 additions & 0 deletions docs/reference/model/crypto.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,104 @@ weight: 100

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

```python
import crypto

md5 = crypto.md5("ABCDEF")
```

## sha1

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

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

```python
import crypto

sha = crypto.sha1("ABCDEF")
```

## sha224

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

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

```python
import crypto

sha = crypto.sha224("ABCDEF")
```

## sha256

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

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

```python
import crypto

sha = crypto.sha256("ABCDEF")
```

## sha384

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

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

```python
import crypto

sha = crypto.sha384("ABCDEF")
```

## sha512

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

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

```python
import crypto

sha = crypto.sha512("ABCDEF")
```

## blake3

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

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

```python
import crypto

blake3 = crypto.blake3("ABCDEF")
```

## uuid

`uuid() -> str`

Generate a random UUID string.

```python
import crypto

a = crypto.uuid()
```

## filesha256

`filesha256(filepath: str) -> str`

Calculate the SHA256 hash of the file `filepath`.

```python
import crypto

sha = crypto.filesha256("test.txt")
```
30 changes: 30 additions & 0 deletions docs/reference/model/datetime.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,56 @@ weight: 100

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

```python
import datetime

ticks = datetime.ticks()
```

## date

`date() -> str`

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

```python
import datetime

date = datetime.date()
```

## now

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

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`.

```python
import datetime

date = datetime.now()
```

## today

`today() -> str`

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

```python
import datetime

date = datetime.today()
```

## validate

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

Validate whether the provided date string matches the specified format.

```python
import datetime

result = datetime.validate("2024-08-26", "%Y-%m-%d") # Valid date
```
96 changes: 96 additions & 0 deletions docs/reference/model/file.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,68 +12,164 @@ weight: 100

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

```python
import file

a = file.read("test.txt")
```

## glob

`glob(pattern: str) -> str`

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

```python
import file

json_files = file.glob("./*.json")
```

## modpath

`modpath() -> str`

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

```python
import file

modpath = file.modpath()
```

## workdir

`workdir() -> str`

Return the path of the current working directory.

```python
import file

workdir = file.workdir()
```

## exists

`exists(filepath: str) -> bool`

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.

```python
import file

file_exists = file.exists("test.txt")
```

## abs

`abs(filepath: str) -> str`

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

```python
import file

abs_file_path = file.abs("test.txt")
```

## mkdir

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

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

```python
import file

file.mkdir("path")
```

## delete

`delete(directory: str)`

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

```python
import file

file.delete("test.txt")
```

## cp

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

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

```python
import file

file.cp("src", "dest")
```

## mv

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

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

```python
import file

file.mv("src", "dest")
```

## size

`size(filepath: str)`

Get the size of a file at the specified path.

```python
import file

size = file.size("test.txt")
```

## write

`write(filepath: str, content: str)`

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.

```python
import file

file.size("test.txt", "content")
```

## read_env

`read_env(key: str) -> str`

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

```python
import file

value = file.read_env("ENV_VAR")
```

## current

`current() -> str`

Read the path of the current script or module that is being executed.

```python
import file

value = file.current()
```
Loading

0 comments on commit 076aab7

Please sign in to comment.