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