Skip to content

Commit 00839eb

Browse files
committed
[doc] add code style doc, change english readme
1 parent 33f1072 commit 00839eb

File tree

3 files changed

+212
-65
lines changed

3 files changed

+212
-65
lines changed

CODE_OF_CONDUCT.md

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
2+
# CGraph Code of Conduct
3+
4+
## Coding Guidelines
5+
6+
### C++
7+
Please ensure your code follows the CGraph coding guidelines. Consistent coding style helps maintain the codebase.
8+
9+
Based on Google style guide, with some differences(perhaps):
10+
11+
- Function naming: First letter lowercase, camelCase
12+
```
13+
int getCode() {
14+
return 0;
15+
}
16+
```
17+
18+
- Class and struct naming: First letter uppercase, camelCase
19+
```
20+
class MyClass {
21+
}
22+
23+
struct MyStruct {
24+
}
25+
```
26+
27+
- Variable naming: First letter lowercase, camelCase
28+
```
29+
int myVar = 0;
30+
```
31+
32+
- Member variable naming: First letter lowercase, words connected by underscore, ends with underscore
33+
```
34+
class MyClass {
35+
int my_var_;
36+
}
37+
38+
struct MyStruct {
39+
int my_var_;
40+
}
41+
```
42+
43+
- Macro naming: All uppercase, words connected by underscore
44+
```
45+
#define MY_MACRO (1)
46+
```
47+
48+
- Enum naming: All uppercase, words connected by underscore
49+
```
50+
enum MyEnum {
51+
MY_ENUM_VALUE1,
52+
MY_ENUM_VALUE2,
53+
MY_ENUM_VALUE3,
54+
};
55+
```
56+
57+
### Python
58+
59+
Based on the Google Python Style Guide, the basic requirements are as follows:
60+
61+
1. Files and Encoding
62+
- **File Encoding**: Files should be encoded in UTF-8 and usually do not need an encoding declaration at the top of the file (Python 3 uses UTF-8 by default).
63+
- **File Naming**: First letter uppercase, camelCase, e.g., `MyModule.py`.
64+
65+
2. Comments
66+
- **Module Comments**: Each module should start with a module-level docstring describing the module's purpose, functionality, and main classes or functions. For example:
67+
```python
68+
"""
69+
This module provides utility functions for handling strings.
70+
71+
It includes functions for string formatting, splitting, and joining.
72+
"""
73+
```
74+
- **Function and Method Comments**: Functions and methods also require docstrings explaining their purpose, parameters, return values, and possible exceptions. For example:
75+
```python
76+
def addNumbers(a: int, b: int):
77+
"""
78+
Add two numbers together.
79+
80+
Args:
81+
a (int): The first number.
82+
b (int): The second number.
83+
84+
Returns:
85+
int: The sum of a and b.
86+
"""
87+
return a + b
88+
```
89+
- **Inline Comments**: Use inline comments in the code to explain complex or non-intuitive parts, but don't overuse them. Comments should be concise and clear.
90+
91+
3. Classes and Objects
92+
- **Class Naming**: Class names should use CapWords convention, e.g., `MyClass`.
93+
- **Class Docstrings**: Classes should include a docstring describing their purpose and main functionality.
94+
```python
95+
class MyClass:
96+
"""
97+
A simple class that represents a person.
98+
99+
Attributes:
100+
name (str): The name of the person.
101+
age (int): The age of the person.
102+
"""
103+
def __init__(self, name: str, age: int):
104+
self.name = name
105+
self.age = age
106+
107+
def getInfo(self):
108+
"""
109+
Get the person's information.
110+
111+
Returns:
112+
str: A string containing the person's name and age.
113+
"""
114+
return f"Name: {self.name}, Age: {self.age}"
115+
```
116+
- **Member Variable and Method Naming**: Member variables and method names should use lowercase with words separated by underscores, e.g., `my_variable` and `my_method`.
117+
118+
4. Functions and Methods
119+
- **Function Naming**: Function names should use lowercase first letter, camelCase, e.g., `checkResult()`.
120+
- **Parameter Naming**: Parameter names should also use lowercase first letter, camelCase, and be descriptive, e.g., `myResultValue`.
121+
- **Function Length**: Functions should be kept short and focused on a single task. Avoid excessively long functions.
122+
123+
5. Code Layout
124+
- **Indentation**: Use 4 spaces for indentation, not tabs.
125+
- **Line Length**: Limit each line of code to a maximum of 120 characters. If it exceeds, break the line while maintaining code readability. Usually break after operators. For example:
126+
```python
127+
result = getResult(arg1, arg2,
128+
arg3, arg4)
129+
```
130+
- **Blank Lines**: Use blank lines to separate logical sections, e.g., two blank lines between functions and classes, one blank line between methods within a class.
131+
132+
6. Import Statements
133+
- **Import Order**: Group import statements in the order of standard library, third-party libraries, and local modules, with a blank line between each group. For example:
134+
```python
135+
import os
136+
import sys
137+
138+
import requests
139+
140+
from MyModule import MyClass
141+
```
142+
- **Avoid Wildcard Imports**: Avoid using wildcard imports like `from module import *` as they can lead to naming conflicts and reduced code readability.
143+
144+
7. Exception Handling
145+
- **Specific Exception Types**: When catching exceptions, specify the exact exception type instead of using a generic `except` statement. For example:
146+
```python
147+
try:
148+
result = 1 / 0
149+
except ZeroDivisionError:
150+
print("Division by zero occurred.")
151+
```
152+
153+
8. Testing
154+
- **Write Unit Tests**: Write unit tests for your code to ensure correctness and stability. You can use Python's `unittest` or `pytest` testing frameworks.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,9 @@ if __name__ == '__main__':
392392
* 提供 Python 和 C++ 混合编程功能
393393
* 提供 Python 打包功能,支持 `pip3 install PyCGraph` 安装
394394

395+
[2025.05.05 - v3.1.1 - Chunel]
396+
* 提供 `CODE_OF_CONDUCT.md` 文档
397+
395398
</details>
396399

397400
------------

README_en.md

Lines changed: 55 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818

1919
## 1. Introduction
2020

21-
CGraph, short for <b>C</b>olor <b>Graph</b>, is a cross-platform DAG computing framework without any third-party dependencies. With the scheduling via `GPipeline`, the purpose of sequential and concurrent executing elements is realized.
21+
CGraph, short for <b>C</b>olor <b>Graph</b>, is a cross-platform DAG computing framework. It is written by C++11 without any third-party dependencies, Python APIs are also supported.
2222

23-
You only need to inherit `GNode` class, implement the `run()` method in the subclass, and set the dependencies as needed to achieve the graphical execution of tasks.
23+
With the scheduling via `GPipeline`, the purpose of sequential and concurrent executing elements is realized. You only need to inherit `GNode` class, implement the `run()` method in the subclass, and set the dependencies as needed to achieve the graphical execution of tasks.
2424

2525
At the same time, you can also control the graph conditional judgment, loop or concurrent execution logic by setting various `GGroup`s, which containing multi-node information by themselves.
2626

@@ -29,94 +29,84 @@ You can transfer your params in many scenes. It is also possible to extend the f
2929
![CGraph Skeleton](https://github.com/ChunelFeng/CGraph/blob/main/doc/image/CGraph%20Skeleton.jpg)
3030
<br>
3131

32-
## 2. Compile
33-
* This project supports MacOS, Linux, and Windows systems without any third-party dependencies. C++11 is default and lowest version, C++17 is recommended.
34-
35-
* For developers using `CLion` as IDE within all platform, open the `CMakeLists.txt` file as project to compile.
36-
37-
* Developers on Windows system, using `Visual Studio`(2013 version at least) as IDE, with cmake, enter commands as flowers to build `CGraph.sln` file.
38-
```shell
39-
$ git clone https://github.com/ChunelFeng/CGraph.git
40-
$ cd CGraph
41-
$ cmake . -Bbuild
42-
```
43-
44-
* Developers on MacOS system, using `Xcode` as IDE, with cmake, enter commands as flowers to build `CGraph.xcodeproj` file.
45-
```shell
46-
$ git clone https://github.com/ChunelFeng/CGraph.git
47-
$ cd CGraph
48-
$ mkdir build && cd build
49-
$ cmake .. -G Xcode
50-
```
51-
52-
* Developers on Linux system, enter commands as flowers to compile.
53-
```shell
54-
$ git clone https://github.com/ChunelFeng/CGraph.git
55-
$ cd CGraph
56-
$ cmake . -Bbuild
57-
$ cd build
58-
$ make -j8
59-
```
60-
61-
* Compile online, enter [CGraph env online](https://gitpod.io/#/github.com/ChunelFeng/CGraph), log in with your Github id, enter commands as flowers to compile and run your first tutorial.
62-
```shell
63-
$ sudo apt-get install cmake -y
64-
$ ./CGraph-build.sh
65-
$ ./build/tutorial/T00-HelloCGraph
66-
```
67-
68-
## 3. Demo
69-
70-
#### MyNode.h
32+
## 2. Demo
33+
34+
> C++ version
35+
7136
```cpp
7237
#include "CGraph.h"
7338

74-
class MyNode1 : public CGraph::GNode {
39+
using namespace CGraph;
40+
41+
class MyNode1 : public GNode {
7542
public:
76-
CStatus run () override {
77-
CStatus status;
78-
printf("[%s], Sleep for 1 second ... \n", this->getName().c_str());
43+
CStatus run() override {
44+
printf("[%s], sleep for 1 second ...\n", this->getName().c_str());
7945
CGRAPH_SLEEP_SECOND(1)
80-
return status;
46+
return CStatus();
8147
}
8248
};
8349

84-
85-
class MyNode2 : public CGraph::GNode {
50+
class MyNode2 : public GNode {
8651
public:
87-
CStatus run () override {
88-
CStatus status;
89-
printf("[%s], Sleep for 1 second ... \n", this->getName().c_str());
52+
CStatus run() override {
53+
printf("[%s], sleep for 2 second ...\n", this->getName().c_str());
9054
CGRAPH_SLEEP_SECOND(2)
91-
return status;
55+
return CStatus();
9256
}
9357
};
94-
```
9558

96-
#### main.cpp
97-
```cpp
98-
#include "MyNode.h"
99-
100-
using namespace CGraph;
10159

10260
int main() {
103-
/* build a pipeline */
10461
GPipelinePtr pipeline = GPipelineFactory::create();
10562
GElementPtr a, b, c, d = nullptr;
10663

107-
/* register node with dependency info */
108-
pipeline->registerGElement<MyNode1>(&a, {}, "nodeA"); // register nodeA with no dependency
109-
pipeline->registerGElement<MyNode2>(&b, {a}, "nodeB"); // b depends a
64+
pipeline->registerGElement<MyNode1>(&a, {}, "nodeA");
65+
pipeline->registerGElement<MyNode2>(&b, {a}, "nodeB");
11066
pipeline->registerGElement<MyNode1>(&c, {a}, "nodeC");
111-
pipeline->registerGElement<MyNode2>(&d, {b, c}, "nodeD"); // d depends b and c
67+
pipeline->registerGElement<MyNode2>(&d, {b, c}, "nodeD");
68+
69+
pipeline->process();
11270

113-
/* run dag pipeline */
114-
status = pipeline->process();
11571
GPipelineFactory::remove(pipeline);
72+
11673
return 0;
11774
}
11875
```
11976
12077
![CGraph Demo](https://github.com/ChunelFeng/CGraph/blob/main/doc/image/CGraph%20Demo.jpg)
12178
<br>
12279
As is shown on the picture, run `a` firstly. Then, run `b` and `c` parallelized. Run `d` at last after `b` and `c` finished.
80+
81+
> Python version
82+
```python
83+
import time
84+
from datetime import datetime
85+
86+
from PyCGraph import GNode, GPipeline, CStatus
87+
88+
89+
class MyNode1(GNode):
90+
def run(self):
91+
print("[{0}] {1}, enter MyNode1 run function. Sleep for 1 second ... ".format(datetime.now(), self.getName()))
92+
time.sleep(1)
93+
return CStatus()
94+
95+
class MyNode2(GNode):
96+
def run(self):
97+
print("[{0}] {1}, enter MyNode2 run function. Sleep for 2 second ... ".format(datetime.now(), self.getName()))
98+
time.sleep(2)
99+
return CStatus()
100+
101+
102+
if __name__ == '__main__':
103+
pipeline = GPipeline()
104+
a, b, c, d = MyNode1(), MyNode2(), MyNode1(), MyNode2()
105+
106+
pipeline.registerGElement(a, set(), "nodeA")
107+
pipeline.registerGElement(b, {a}, "nodeB")
108+
pipeline.registerGElement(c, {a}, "nodeC")
109+
pipeline.registerGElement(d, {b, c}, "nodeD")
110+
111+
pipeline.process()
112+
```

0 commit comments

Comments
 (0)