Skip to content

Commit 2476338

Browse files
committed
[python] add multi condition tutorial
1 parent e016ed6 commit 2476338

7 files changed

Lines changed: 98 additions & 45 deletions

File tree

COMPILE.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,13 @@
5050
```shell
5151
$ sudo apt-get install cmake -y # 安装cmake
5252
$ ./CGraph-build.sh # 编译CGraph工程,生成的内容在同级/build/文件夹中
53-
$ ./build/tutorial/T00-HelloCGraph # 运 行T00-HelloCGraph,并且在终端输出 Hello, CGraph.
53+
$ ./build/tutorial/T00-HelloCGraph # 运行T00-HelloCGraph,并且在终端输出 Hello, CGraph.
54+
```
55+
56+
* 安装PyCGraph,使用前需要提前安装`python3` `pybind11``setuptools`
57+
```shell
58+
$ git clone https://github.com/ChunelFeng/CGraph.git
59+
$ cd CGraph/python # 进入对应文件夹
60+
$ python3 setup.py install # 安装 PyCGraph
61+
$ python3 tutorial/T00-HelloCGraph.py # 运行T00-HelloCGraph.py,并且在终端输出 Hello, PyCGraph.
5462
```

python/CMakeLists.txt

Lines changed: 0 additions & 25 deletions
This file was deleted.

python/setup.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
1-
#!/usr/bin/env python
2-
# -*- coding: UTF-8 -*-
1+
"""
2+
@Author: Chunel
3+
@Contact: chunel@foxmail.com
4+
@File: setup
5+
@Time: 2025/3/6 23:25
6+
@Desc:
7+
"""
38

49
import glob
510
from setuptools import setup, Extension
611
import pybind11
712

8-
sources = ['PyCGraph.cpp'] + glob.glob("../src/**/*.cpp", recursive=True)
9-
extra_compile_args = ["-pthread", "-std=c++11", '-fvisibility=hidden']
10-
include_dirs = [pybind11.get_include(), "../src"]
13+
_sources = ['PyCGraph.cpp'] + glob.glob("../src/**/*.cpp", recursive=True)
14+
_extra_compile_args = ["-pthread", "-std=c++11", '-fvisibility=hidden']
15+
_include_dirs = [pybind11.get_include(), "../src"]
1116

1217
ext_modules = [
1318
Extension(
1419
name = "PyCGraph",
15-
sources = sources,
16-
extra_compile_args = extra_compile_args,
17-
include_dirs = include_dirs,
20+
sources = _sources,
21+
extra_compile_args = _extra_compile_args,
22+
include_dirs = _include_dirs,
1823
),
1924
]
2025

2126
setup(
22-
name="PyCGraph",
23-
version="0.0.1",
24-
author="Chunel Feng",
25-
description="CGraph with python API",
26-
ext_modules=ext_modules,
27-
zip_safe=False,
27+
name = "PyCGraph",
28+
version = "1.0.0",
29+
author = "Chunel Feng",
30+
author_email = "chunel@foxmail.com",
31+
description = "CGraph with python api wrapper by pybind11",
32+
url = "https://github.com/ChunelFeng/CGraph",
33+
license = "Apache-2.0",
34+
ext_modules = ext_modules,
35+
zip_safe = False,
2836
)

python/tutorial/MyGNode/HelloCGraphNode.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010

1111
class HelloCGraphNode(GNode):
1212
def run(self):
13-
print("Hello PyCGraph")
13+
print("Hello, PyCGraph.")
1414
return CStatus()

python/tutorial/MyGNode/MyHoldNode.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@
1111
from MyParams.MyParam import MyParam
1212

1313
class MyHoldNode(GNode):
14-
param_key = 'hold-param'
14+
_param_key = 'hold-param'
1515
def init(self):
16-
return self.createGParam(MyParam(), self.param_key)
16+
return self.createGParam(MyParam(), self._param_key)
1717

1818
def run(self):
19-
param: MyParam = self.getGParam(self.param_key)
19+
param: MyParam = self.getGParam(self._param_key)
2020
param.value += 1
2121
print('current value is {0}'.format(param.value))
2222
return CStatus()
2323

2424
def isHold(self):
25-
param: MyParam = self.getGParam(self.param_key)
25+
param: MyParam = self.getGParam(self._param_key)
2626
print('enter hold path, value is {0}'.format(param.value))
2727
return param.value < 5
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
@Author: Chunel
3+
@Contact: chunel@foxmail.com
4+
@File: MyMatchNode
5+
@Time: 2025/3/7 23:54
6+
@Desc:
7+
"""
8+
9+
import time
10+
from datetime import datetime
11+
12+
from PyCGraph import GNode, CStatus
13+
14+
from MyParams.MyParam import MyParam
15+
16+
17+
class MyMatchNode(GNode):
18+
def run(self):
19+
time.sleep(1)
20+
print("[{0}] run MyMatchNode: {1}".format(datetime.now(), self.getName()))
21+
22+
return CStatus()
23+
24+
def isMatch(self):
25+
param: MyParam = self.getGParamWithNoEmpty('param1')
26+
result = 0 != param.count % 2
27+
if result:
28+
print(' match, ready to run {0}'.format(self.getName()))
29+
else:
30+
print(' not match, will not run {0}'.format(self.getName()))
31+
return result
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
@Author: Chunel
3+
@Contact: chunel@foxmail.com
4+
@File: T21-MultiCondition
5+
@Time: 2025/3/7 23:52
6+
@Desc:
7+
"""
8+
9+
from PyCGraph import GPipeline, GSerialMultiCondition, GParallelMultiCondition
10+
11+
from MyGNode.MyMatchNode import MyMatchNode
12+
from MyGNode.MyNode1 import MyNode1
13+
from MyGNode.MyWriteParamNode import MyWriteParamNode
14+
15+
16+
def tutorial_multi_condition():
17+
a, c = MyWriteParamNode(), MyWriteParamNode()
18+
b_multi_condition = GSerialMultiCondition([MyNode1('nodeB1'), MyMatchNode('nodeB2')])
19+
d_multi_condition = GParallelMultiCondition([MyMatchNode('nodeD1'), MyMatchNode('nodeD2')])
20+
21+
pipeline = GPipeline()
22+
pipeline.registerGElement(a, set(), 'nodeA')
23+
pipeline.registerGElement(b_multi_condition, {a}, 'multiConditionB')
24+
pipeline.registerGElement(c, {b_multi_condition}, 'nodeC')
25+
pipeline.registerGElement(d_multi_condition, {c}, 'multiConditionD')
26+
27+
pipeline.process()
28+
29+
30+
if __name__ == '__main__':
31+
tutorial_multi_condition()

0 commit comments

Comments
 (0)