Commit 611653211a467c567f7615d22f2f015550fdee93
1 parent
98621b46
Exists in
dev
nouveau my_package1.my_module1
Showing
4 changed files
with
278 additions
and
239 deletions
Show diff stats
... | ... | @@ -0,0 +1,273 @@ |
1 | +#!/usr/bin/env python3 | |
2 | + | |
3 | +from __future__ import annotations | |
4 | + | |
5 | +# TODO : | |
6 | +# - classmethod vs staticmethod | |
7 | +# - doctest | |
8 | +# - exception (custom) | |
9 | +# - dataclass | |
10 | +# - generic types | |
11 | +# TODO: return type de plusieurs params : | |
12 | +# def response(query: str) -> Response[str]: | |
13 | +# -> Any: | |
14 | +# -> Generic | |
15 | +# -> None: | |
16 | +# -> Sequence[T]: | |
17 | +# -> Dict[str, int]: | |
18 | +# -> [int,float]: ??? | |
19 | +# -> List[T]: | |
20 | +# -> tuple[int, str]: | |
21 | +# ... | |
22 | + | |
23 | + | |
24 | +''' | |
25 | +================================================================= | |
26 | + MODULE Comment | |
27 | +================================================================= | |
28 | +''' | |
29 | + | |
30 | + | |
31 | +''' | |
32 | +================================================================= | |
33 | + PACKAGES IMPORT | |
34 | +================================================================= | |
35 | +''' | |
36 | + | |
37 | +# --- GENERAL PURPOSE IMPORT --- | |
38 | + | |
39 | +from typing import Dict, List, Tuple | |
40 | +import platform | |
41 | +from datetime import date | |
42 | + | |
43 | +''' | |
44 | +import pykwalify.core | |
45 | +import sys | |
46 | +import yaml, logging, os, pickle, time | |
47 | +from datetime import datetime | |
48 | +from pykwalify.errors import PyKwalifyException,SchemaError | |
49 | +from pathlib import Path | |
50 | + | |
51 | +# --- PROJECT SPECIFIC IMPORT --- | |
52 | + | |
53 | +from django.conf import settings as djangosettings | |
54 | +from common.models import AgentSurvey, AgentCmd, AgentLogs | |
55 | +from src.core.pyros_django.obsconfig.configpyros import ConfigPyros | |
56 | +from device_controller.abstract_component.device_controller import ( | |
57 | + DCCNotFoundException, UnknownGenericCmdException, UnimplementedGenericCmdException, UnknownNativeCmdException | |
58 | +) | |
59 | +''' | |
60 | + | |
61 | + | |
62 | +''' | |
63 | +================================================================= | |
64 | + GENERAL MODULE CONSTANTS & FUNCTIONS DEFINITIONS | |
65 | +================================================================= | |
66 | +''' | |
67 | + | |
68 | +# - General constants | |
69 | + | |
70 | +DEBUG = False | |
71 | + | |
72 | +IS_WINDOWS = platform.system() == "Windows" | |
73 | + | |
74 | + | |
75 | +# - General Functions | |
76 | + | |
77 | + | |
78 | +def general_function_that_returns_a_float(arg_a: int, arg_b: str, arg_c: float=1.2, arg_d: bool=True) -> float: | |
79 | + ''' | |
80 | + This function is used for ... blabla ... | |
81 | + | |
82 | + Args: | |
83 | + arg_a: the path of the file to wrap | |
84 | + arg_b: instance to wrap | |
85 | + arg_c: toto | |
86 | + arg_d: whether or not to delete the file when the File instance is destructed | |
87 | + | |
88 | + Returns: | |
89 | + A buffered writable file descriptor | |
90 | + | |
91 | + Raises: | |
92 | + AttributeError: The ``Raises`` section is a list of all exceptions | |
93 | + that are relevant to the interface. | |
94 | + ValueError: If `arg_a` is equal to `arg_b`. | |
95 | + ''' | |
96 | + | |
97 | + # comment on a | |
98 | + a = 1 | |
99 | + | |
100 | + # comment on b | |
101 | + b = 2 | |
102 | + | |
103 | + return 3.5 | |
104 | + | |
105 | + | |
106 | +def general_function_that_returns_a_tuple_of_3_elem(a: int, b: int=2, c: str='titi') -> Tuple[str, float, str]: | |
107 | + ''' Commentaire général sur la fonction | |
108 | + | |
109 | + Args: | |
110 | + a: the path of the file to wrap | |
111 | + b: instance to wrap | |
112 | + c: toto | |
113 | + ''' | |
114 | + | |
115 | + return (a, b, c+' toto') | |
116 | + | |
117 | + | |
118 | +class MySuperClass1: | |
119 | + pass | |
120 | + | |
121 | + | |
122 | +class MySuperClass2: | |
123 | + pass | |
124 | + | |
125 | + | |
126 | +# ''' | |
127 | +# ================================================================= | |
128 | +# CLASS MyFirstClass | |
129 | +# ================================================================= | |
130 | +# ''' | |
131 | +class MySimpleClass(MySuperClass1, MySuperClass2): | |
132 | + ''' a Class with multi-inheritance | |
133 | + | |
134 | + blabla | |
135 | + | |
136 | + blabla | |
137 | + ''' | |
138 | + | |
139 | + # | |
140 | + # The class attributes | |
141 | + # | |
142 | + | |
143 | + names: List[str] = ["Guido", "Jukka", "Ivan"] | |
144 | + ''' List is mutable''' | |
145 | + | |
146 | + version: Tuple[int, int, int] = (3, 7, 1) | |
147 | + ''' Tuple is UNmutable''' | |
148 | + | |
149 | + options: Dict[str, bool] = {"centered": False, "capitalize": True} | |
150 | + ''' Dict (is mutable) ''' | |
151 | + | |
152 | + my_attr1: dict = {} | |
153 | + current_file = None | |
154 | + pickle_file = "obsconfig.p" | |
155 | + | |
156 | + # | |
157 | + # The class methods | |
158 | + # | |
159 | + | |
160 | + def __init__(self, a: int, b: float) -> None: | |
161 | + ''' | |
162 | + La methode __init__ doit toujours retourner "None" | |
163 | + | |
164 | + Args: | |
165 | + a: blabla | |
166 | + ''' | |
167 | + | |
168 | + c = 1 | |
169 | + d = 2 | |
170 | + | |
171 | + return False | |
172 | + | |
173 | + def __str__(self) -> str: | |
174 | + ''' | |
175 | + La methode __str__ doit toujours retourner "str" | |
176 | + ''' | |
177 | + return "toto" | |
178 | + | |
179 | + def my_method2(self, a: int, b: float) -> None: | |
180 | + a = 1 | |
181 | + b = 2 | |
182 | + | |
183 | + | |
184 | +# ''' | |
185 | +# ================================================================= | |
186 | +# CLASS Person | |
187 | +# ================================================================= | |
188 | +# ''' | |
189 | + | |
190 | +class Person: | |
191 | + """ Class to create a person, in several ways (several Factory methods) | |
192 | + | |
193 | + => Illustrate difference btw static and class methods | |
194 | + | |
195 | + Usage: | |
196 | + | |
197 | + 1) Classic Constructor : | |
198 | + | |
199 | + >>> person1 = Person('Alfredo', 21) | |
200 | + | |
201 | + 2) Class method (Factory) : | |
202 | + | |
203 | + >>> person2 = Person.fromBirthYear('Peter', 2000) | |
204 | + >>> person2.age | |
205 | + 22 | |
206 | + | |
207 | + 3) Another class method (Factory) : | |
208 | + | |
209 | + >>> person3 = Person.twin('John', person2) | |
210 | + >>> person3.age == person2.age | |
211 | + True | |
212 | + >>> person3.name == person2.name | |
213 | + False | |
214 | + | |
215 | + 4) Static method (does not need access to the class attributes or methods) : | |
216 | + | |
217 | + >>> Person.isAdult(22) | |
218 | + True | |
219 | + | |
220 | + """ | |
221 | + | |
222 | + def __init__(self, name: str, age: int) -> None: | |
223 | + self.name = name | |
224 | + self.age = age | |
225 | + | |
226 | + # a class method to create a Person object by birth year | |
227 | + @classmethod | |
228 | + def fromBirthYear(cls, name: str, year: int) -> Person: | |
229 | + return cls(name, date.today().year - year) | |
230 | + | |
231 | + # a class method to create a Person object from another | |
232 | + @classmethod | |
233 | + def twin(cls, name: str, p: Person) -> Person: | |
234 | + return cls(name, p.age) | |
235 | + | |
236 | + # a static method to check if a Person is adult or not | |
237 | + @staticmethod | |
238 | + def isAdult(age: int): | |
239 | + return age > 18 | |
240 | + | |
241 | + | |
242 | +# ''' | |
243 | +# ================================================================= | |
244 | +# Main function (definition) | |
245 | +# ================================================================= | |
246 | +# ''' | |
247 | + | |
248 | + | |
249 | +def main() -> None: | |
250 | + ''' | |
251 | + Comment on Main function definition | |
252 | + ''' | |
253 | + a = 1 | |
254 | + b = 2 | |
255 | + c = a+b | |
256 | + | |
257 | + a = general_function_that_returns_a_tuple_of_3_elem(1, 2) | |
258 | + print(a) | |
259 | + | |
260 | + import doctest | |
261 | + doctest.testmod() | |
262 | + | |
263 | + | |
264 | +''' | |
265 | +================================================================= | |
266 | + Main function (execution) | |
267 | +================================================================= | |
268 | +''' | |
269 | +if __name__ == "__main__": | |
270 | + ''' | |
271 | + Comment on Main function execution | |
272 | + ''' | |
273 | + main() | ... | ... |
doc/code_style/package1_name/__init__.py deleted
No preview for this file type
doc/code_style/package1_name/module1.py deleted
... | ... | @@ -1,238 +0,0 @@ |
1 | -#!/usr/bin/env python3 | |
2 | - | |
3 | - | |
4 | -''' | |
5 | -================================================================= | |
6 | - PACKAGES IMPORT | |
7 | -================================================================= | |
8 | -''' | |
9 | - | |
10 | -# --- GENERAL PURPOSE IMPORT --- | |
11 | - | |
12 | -import pykwalify.core | |
13 | -import sys | |
14 | -import yaml, logging, os, pickle, time | |
15 | -from datetime import datetime | |
16 | -from pykwalify.errors import PyKwalifyException,SchemaError | |
17 | -from pathlib import Path | |
18 | - | |
19 | -# --- PROJECT SPECIFIC IMPORT --- | |
20 | - | |
21 | -from django.conf import settings as djangosettings | |
22 | -from common.models import AgentSurvey, AgentCmd, AgentLogs | |
23 | -from src.core.pyros_django.obsconfig.configpyros import ConfigPyros | |
24 | -from device_controller.abstract_component.device_controller import ( | |
25 | - DCCNotFoundException, UnknownGenericCmdException, UnimplementedGenericCmdException, UnknownNativeCmdException | |
26 | -) | |
27 | - | |
28 | - | |
29 | - | |
30 | -''' | |
31 | -================================================================= | |
32 | - GENERAL MODULE CONSTANTS & FUNCTIONS DEFINITIONS | |
33 | -================================================================= | |
34 | -''' | |
35 | - | |
36 | -# - General constants | |
37 | - | |
38 | -DEBUG = False | |
39 | - | |
40 | -IS_WINDOWS = platform.system() == "Windows" | |
41 | - | |
42 | - | |
43 | -# - General Functions | |
44 | - | |
45 | -def general_function1(arg_a: int=1, arg_b: str='toto', arg_c: float, arg_d: bool) -> float: | |
46 | - ''' | |
47 | - This function is used for ... blabla ... | |
48 | - | |
49 | - Args: | |
50 | - arg_a: the path of the file to wrap | |
51 | - arg_b: instance to wrap | |
52 | - arg_c: toto | |
53 | - arg_d: whether or not to delete the file when the File instance is destructed | |
54 | - | |
55 | - Returns: | |
56 | - A buffered writable file descriptor | |
57 | - | |
58 | - Raises: | |
59 | - AttributeError: The ``Raises`` section is a list of all exceptions | |
60 | - that are relevant to the interface. | |
61 | - ValueError: If `arg_a` is equal to `arg_b`. | |
62 | - ''' | |
63 | - | |
64 | - # comment on a | |
65 | - a = 1 | |
66 | - | |
67 | - # comment on b | |
68 | - b = 2 | |
69 | - | |
70 | - return 3.5 | |
71 | - | |
72 | - | |
73 | -def general_function2(a: int, b:int): | |
74 | - ''' | |
75 | - Commentaires | |
76 | - ''' | |
77 | - a = 1 | |
78 | - b = 2 | |
79 | - | |
80 | - return 3 | |
81 | - | |
82 | - | |
83 | - | |
84 | -''' | |
85 | -================================================================= | |
86 | - CLASS MyFirstClass | |
87 | -================================================================= | |
88 | -''' | |
89 | - | |
90 | -class MyFirstClass: | |
91 | - ''' | |
92 | - General Comment on the class | |
93 | - bla | |
94 | - bla | |
95 | - ''' | |
96 | - | |
97 | - # The class attributes | |
98 | - | |
99 | - # comment on my_attr1 | |
100 | - my_attr1 = {} | |
101 | - current_file = None | |
102 | - pickle_file = "obsconfig.p" | |
103 | - obs_config = None | |
104 | - devices = None | |
105 | - computers = None | |
106 | - agents = None | |
107 | - obs_config_file_content = None | |
108 | - errors = None | |
109 | - | |
110 | - | |
111 | - # The class methods | |
112 | - | |
113 | - def __init__(self, a: int, b: float) -> int: | |
114 | - ''' | |
115 | - Commentaire sur my_method1" | |
116 | - | |
117 | - Args: | |
118 | - a: blabla | |
119 | - | |
120 | - Returns: | |
121 | - bool: [description] | |
122 | - ''' | |
123 | - | |
124 | - c = 1 | |
125 | - d = 2 | |
126 | - | |
127 | - return False | |
128 | - | |
129 | - | |
130 | - def my_method2(self, a: int, b: float): | |
131 | - a = 1 | |
132 | - b = 2 | |
133 | - | |
134 | - | |
135 | -#TODO: *args et **all_kwargs | |
136 | - | |
137 | - def check_and_return_config(self, yaml_file: str, schema_file: str) -> dict: | |
138 | - ''' | |
139 | - Check if yaml_file is valid for the schema_file and return an dictionary of the config file | |
140 | - | |
141 | - Args: | |
142 | - yaml_file: Path to the config_file to be validated | |
143 | - schema_file: Path to the schema file | |
144 | - | |
145 | - Returns: | |
146 | - dict: dictionary of the config file (with values) | |
147 | - ''' | |
148 | - # disable pykwalify error to clean the output | |
149 | - #####logging.disable(logging.ERROR) | |
150 | - try: | |
151 | - can_yaml_file_be_read = False | |
152 | - while can_yaml_file_be_read != True: | |
153 | - if os.access(yaml_file, os.R_OK): | |
154 | - can_yaml_file_be_read = True | |
155 | - else: | |
156 | - print(f"{yaml_file} can't be accessed, waiting for availability") | |
157 | - time.sleep(0.5) | |
158 | - | |
159 | - c = pykwalify.core.Core(source_file=yaml_file, schema_files=[self.SCHEMA_PATH+schema_file]) | |
160 | - return c.validate(raise_exception=True) | |
161 | - except SchemaError: | |
162 | - for error in c.errors: | |
163 | - print("Error :",str(error).split(". Path")[0]) | |
164 | - print("Path to error :",error.path) | |
165 | - self.errors = c.errors | |
166 | - return None | |
167 | - except IOError: | |
168 | - print("Error when reading the observatory config file") | |
169 | - | |
170 | - | |
171 | - #TODO: @classmethod (avec cls) | |
172 | - @staticmethod | |
173 | - def check_config(yaml_file: str, schema_file: str) -> any: | |
174 | - """ | |
175 | - Check if yaml_file is valid for the schema_file and return a boolean or list of errors according the schema | |
176 | - | |
177 | - Args: | |
178 | - yaml_file (str): Path to the config_file to be validated | |
179 | - schema_file (str): Path to the schema file | |
180 | - | |
181 | - Returns: | |
182 | - any: boolean (True) if the configuration is valid according the schema or a list of error otherwise | |
183 | - """ | |
184 | - # disable pykwalify error to clean the output | |
185 | - ####logging.disable(logging.ERROR) | |
186 | - try: | |
187 | - can_yaml_file_be_read = False | |
188 | - while can_yaml_file_be_read != True: | |
189 | - if os.access(yaml_file, os.R_OK): | |
190 | - can_yaml_file_be_read = True | |
191 | - else: | |
192 | - print(f"{yaml_file} can't be accessed, waiting for availability") | |
193 | - time.sleep(0.5) | |
194 | - | |
195 | - c = pykwalify.core.Core(source_file=yaml_file, schema_files=[schema_file]) | |
196 | - c.validate(raise_exception=True) | |
197 | - return True | |
198 | - except SchemaError: | |
199 | - for error in c.errors: | |
200 | - print("Error :",str(error).split(". Path")[0]) | |
201 | - print("Path to error :",error.path) | |
202 | - | |
203 | - return c.errors | |
204 | - except IOError: | |
205 | - print("Error when reading the observatory config file") | |
206 | - | |
207 | - | |
208 | - | |
209 | -class MySecondClass: | |
210 | - ''' General Comment on the class ''' | |
211 | - pass | |
212 | - | |
213 | - | |
214 | -#TODO: Exceptions custom | |
215 | -#TODO: générer doc exemple avec Sphinx | |
216 | - | |
217 | - | |
218 | - | |
219 | -''' | |
220 | -================================================================= | |
221 | - Main function | |
222 | -================================================================= | |
223 | -''' | |
224 | -def main(): | |
225 | - a = 1 | |
226 | - b = 2 | |
227 | - c = 3 | |
228 | - | |
229 | - | |
230 | - | |
231 | -''' | |
232 | -================================================================= | |
233 | - Exec of main function | |
234 | -================================================================= | |
235 | -''' | |
236 | -if __name__ == "__main__": | |
237 | - | |
238 | - main() |