Skip to content

Commit 2a63c36

Browse files
authored
Merge pull request #136 from Peefy/refactor-python-api-and-docs
refactor: python api code examples
2 parents 86a86e9 + ea95cea commit 2a63c36

File tree

1 file changed

+13
-53
lines changed

1 file changed

+13
-53
lines changed

python/kcl_lib/api/service.py

Lines changed: 13 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,15 @@
5050
class API:
5151
"""KCL APIs
5252
53-
Examples
54-
--------
53+
## Examples
54+
55+
Python Code
56+
5557
```python
5658
import kcl_lib.api as api
57-
# Call the `exec_program` method with appropriate arguments
5859
args = api.ExecProgram_Args(k_filename_list=["a.k"])
59-
# Usage
6060
api = api.API()
61+
6162
result = api.exec_program(args)
6263
print(result.yaml_result)
6364
```
@@ -78,10 +79,9 @@ def parse_program(self, args: ParseProgram_Args) -> ParseProgram_Result:
7879
7980
```python
8081
schema AppConfig:
81-
replicas: int
82-
82+
replicas: int
8383
app: AppConfig {
84-
replicas: 2
84+
replicas: 2
8585
}
8686
```
8787
@@ -109,7 +109,6 @@ def exec_program(self, args: ExecProgram_Args) -> ExecProgram_Result:
109109
```python
110110
schema AppConfig:
111111
replicas: int
112-
113112
app: AppConfig {
114113
replicas: 2
115114
}
@@ -129,7 +128,6 @@ def exec_program(self, args: ExecProgram_Args) -> ExecProgram_Result:
129128
130129
```python
131130
import kcl_lib.api as api
132-
133131
try:
134132
args = api.ExecProgram_Args(k_filename_list=["file_not_found"])
135133
api = api.API()
@@ -141,12 +139,6 @@ def exec_program(self, args: ExecProgram_Args) -> ExecProgram_Result:
141139
"""
142140
return self.call("KclvmService.ExecProgram", args)
143141

144-
def build_program(self, args: BuildProgram_Args) -> BuildProgram_Result:
145-
return self.call("KclvmService.BuildProgram", args)
146-
147-
def exec_artifact(self, args: ExecArtifact_Args) -> ExecProgram_Result:
148-
return self.call("KclvmService.ExecArtifact", args)
149-
150142
def parse_file(self, args: ParseFile_Args) -> ParseFile_Result:
151143
"""Parse KCL single file to Module AST JSON string with import dependencies and parse errors.
152144
@@ -157,7 +149,6 @@ def parse_file(self, args: ParseFile_Args) -> ParseFile_Result:
157149
```python
158150
schema AppConfig:
159151
replicas: int
160-
161152
app: AppConfig {
162153
replicas: 2
163154
}
@@ -187,7 +178,6 @@ def parse_program(self, args: ParseProgram_Args) -> ParseProgram_Result:
187178
```python
188179
schema AppConfig:
189180
replicas: int
190-
191181
app: AppConfig {
192182
replicas: 2
193183
}
@@ -217,7 +207,6 @@ def load_package(self, args: LoadPackage_Args) -> LoadPackage_Result:
217207
```python
218208
schema AppConfig:
219209
replicas: int
220-
221210
app: AppConfig {
222211
replicas: 2
223212
}
@@ -227,7 +216,6 @@ def load_package(self, args: LoadPackage_Args) -> LoadPackage_Result:
227216
228217
```python
229218
import kcl_lib.api as api
230-
231219
args = api.LoadPackage_Args(
232220
parse_args=api.ParseProgram_Args(paths=["schema.k"]), resolve_ast=True
233221
)
@@ -279,7 +267,6 @@ def list_variables(self, args: ListVariables_Args) -> ListVariables_Result:
279267
```python
280268
schema AppConfig:
281269
replicas: int
282-
283270
app: AppConfig {
284271
replicas: 2
285272
}
@@ -300,35 +287,18 @@ def list_variables(self, args: ListVariables_Args) -> ListVariables_Result:
300287
def format_code(self, args: FormatCode_Args) -> FormatCode_Result:
301288
"""Format the code source.
302289
303-
<details><summary>Example</summary>
304-
<p>
290+
## Example
305291
306292
Python Code
307293
308294
```python
309295
import kcl_lib.api as api
310-
311-
source_code = \"\"\"schema Person:
312-
name: str
313-
age: int
314-
315-
check:
316-
0 < age < 120
317-
\"\"\"
296+
source_code = "a = 1"
318297
args = api.FormatCode_Args(source=source_code)
319298
api_instance = api.API()
320-
result = api_instance.format_code(args)
321-
assert (
322-
result.formatted.decode()
323-
== \"\"\"schema Person:
324-
name: str
325-
age: int
326-
327-
check:
328-
0 < age < 120
329299
330-
\"\"\"
331-
)
300+
result = api_instance.format_code(args)
301+
assert result.formatted.decode(), "a = 1"
332302
```
333303
"""
334304
return self.call("KclvmService.FormatCode", args)
@@ -344,7 +314,6 @@ def format_path(self, args: FormatPath_Args) -> FormatPath_Result:
344314
schema Person:
345315
name: str
346316
age: int
347-
348317
check:
349318
0 < age < 120
350319
```
@@ -372,7 +341,6 @@ def lint_path(self, args: LintPath_Args) -> LintPath_Result:
372341
```python
373342
import math
374343
375-
a = 1
376344
```
377345
378346
Python Code
@@ -396,7 +364,6 @@ def override_file(self, args: OverrideFile_Args) -> OverrideFile_Result:
396364
397365
```python
398366
a = 1
399-
400367
b = {
401368
"a": 1
402369
"b": 2
@@ -441,7 +408,6 @@ def get_schema_type_mapping(
441408
```python
442409
schema AppConfig:
443410
replicas: int
444-
445411
app: AppConfig {
446412
replicas: 2
447413
}
@@ -474,7 +440,6 @@ def validate_code(self, args: ValidateCode_Args) -> ValidateCode_Result:
474440
schema Person:
475441
name: str
476442
age: int
477-
478443
check:
479444
0 < age < 120
480445
\"\"\"
@@ -510,7 +475,6 @@ def load_settings_files(
510475
511476
```python
512477
import kcl_lib.api as api
513-
514478
args = api.LoadSettingsFiles_Args(
515479
work_dir=".", files=["kcl.yaml"]
516480
)
@@ -530,9 +494,10 @@ def rename(self, args: Rename_Args) -> Rename_Result:
530494
531495
## Example
532496
533-
The content of `main.k` is
497+
The content of main.k is
534498
535499
```python
500+
536501
a = 1
537502
b = a
538503
```
@@ -541,7 +506,6 @@ def rename(self, args: Rename_Args) -> Rename_Result:
541506
542507
```python
543508
import kcl_lib.api as api
544-
545509
args = api.Rename_Args(
546510
package_root=".",
547511
symbol_path="a",
@@ -563,7 +527,6 @@ def rename_code(self, args: RenameCode_Args) -> RenameCode_Result:
563527
564528
```python
565529
import kcl_lib.api as api
566-
567530
args = api.RenameCode_Args(
568531
package_root="/mock/path",
569532
symbol_path="a",
@@ -619,7 +582,6 @@ def update_dependencies(
619582
620583
```python
621584
import kcl_lib.api as api
622-
623585
args = api.UpdateDependencies_Args(
624586
manifest_path="module"
625587
)
@@ -651,15 +613,13 @@ def update_dependencies(
651613
```python
652614
import helloworld
653615
import flask
654-
655616
a = helloworld.The_first_kcl_program
656617
```
657618
658619
Python Code
659620
660621
```python
661622
import kcl_lib.api as api
662-
663623
args = api.UpdateDependencies_Args(
664624
manifest_path="module"
665625
)

0 commit comments

Comments
 (0)