plotext-plus 1.0.9__py3-none-any.whl → 1.0.11__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,285 +1,319 @@
1
1
  # this file contains all tools necessary to build the docstrings in _doc.py
2
2
 
3
- from plotext_plus._utility import pad_string, colorize, uncolorize
3
+ import copy
4
4
  from inspect import getfullargspec as args
5
5
  from re import sub
6
- import copy
7
6
 
7
+ from plotext_plus._utility import colorize
8
+ from plotext_plus._utility import uncolorize
8
9
 
10
+ method_name_color = "blue+"
11
+ method_name_style = "bold"
12
+ alias_style = "italic"
9
13
 
10
- method_name_color = 'blue+'
11
- method_name_style = 'bold'
12
- alias_style = 'italic'
14
+ parameters_title_color = "none"
15
+ parameters_title_style = "none"
13
16
 
14
- parameters_title_color = 'none'
15
- parameters_title_style = 'none'
17
+ parameter_name_color = "red+"
18
+ parameter_name_style = "bold"
16
19
 
17
- parameter_name_color = 'red+'
18
- parameter_name_style = 'bold'
20
+ parameter_specs_color = "orange+"
21
+ parameter_specs_style = "dim"
19
22
 
20
- parameter_specs_color = 'orange+'
21
- parameter_specs_style = 'dim'
23
+ parameter_doc_style = "italic"
22
24
 
23
- parameter_doc_style = 'italic'
25
+ return_color = "orange+"
26
+ return_style = "bold"
24
27
 
25
- return_color = 'orange+'
26
- return_style = 'bold'
28
+ warning = colorize("Warning", "orange", "bold")
27
29
 
28
- warning = colorize('Warning', 'orange', 'bold')
30
+ nl = "\n"
31
+ sp = " "
32
+ cm = ", "
33
+ sc = "; "
29
34
 
30
- nl = '\n'
31
- sp = ' '
32
- cm = ', '
33
- sc = '; '
34
35
 
35
36
  def correct_doc(doc):
36
- doc = doc.strip()
37
- doc = doc[:1].upper() + doc[1:]
38
- doc = doc if len(doc) == 0 or doc[-1] == '.' else doc + '.'
39
- doc = sub(' ', ' ', doc)
40
- return doc
41
-
42
- class parameter_class(): # parameter doc
43
- def __init__(self, name, doc = '', type = '', default = ''):
44
- self.name = name.lower()
45
- self.doc = correct_doc(doc)
46
- self.type = None if type is None else str(type)
47
- self.set_default(default)
48
-
49
- def set_default(self, default = ''):
50
- if default == '':
51
- self.default = ''
52
- elif isinstance(default, str):
53
- self.default = "'" + default + "'"
54
- elif isinstance(default, float):
55
- self.default = str(round(default, 3))
56
- else:
57
- self.default = str(default)
58
-
59
- def get_doc(self):
60
- name = colorize(self.name, parameter_name_color, parameter_name_style)
61
- type = '' if self.type == '' else 'type: ' + str(self.type)
62
- default = '' if self.default == '' else 'default: ' + self.default
63
- specs = sc.join([spec for spec in [type, default] if spec != ''])
64
- specs = nl + colorize(specs, parameter_specs_color, parameter_specs_style) if specs != '' else ''
65
- doc = colorize(self.doc, style = parameter_doc_style)
66
- return nl + name + sp + doc + specs
67
-
68
- def copy(self, default = ''):
69
- par = copy.copy(self)
70
- par.set_default(default)
71
- return par
72
-
73
-
74
- class parameters_class():
75
- def __init__(self):
76
- self.list = []
77
-
78
- def append(self, parameter):
79
- self.list.append(parameter)
80
-
81
- def add(self, name, doc = '', type = '', default = ''):
82
- self.append(parameter_class(name, doc, type, default))
83
-
84
- def get_title(self):
85
- lp = len(self.list)
86
- title = 'This is its parameter:' if lp == 1 else 'These are its parameters:'
87
- return colorize(title, parameters_title_color, parameters_title_style)
88
-
89
- def get_doc(self):
90
- docs = [el.get_doc() for el in self.list]
91
- return nl + self.get_title() + nl + nl.join(docs) if len(self.list) != 0 else ''
92
-
93
- def get_parameter(self, name):
94
- names = [el.name for el in self.list]
95
- if name not in names:
96
- print(warning, 'no parameter', name, 'found')
97
- index = names.index(name) if name in names else None
98
- return self.list[index] if name in names else None
99
-
100
-
101
-
102
- class output_class():
103
- def __init__(self, doc = '', type = None):
104
- self.type = type
105
- self.doc = correct_doc(doc)
106
-
107
- def get_doc(self):
108
- title = colorize('Returns', return_color, return_style)
109
- type = '' if self.type is None else 'type: ' + str(self.type)
110
- type = colorize(type , parameter_specs_color, parameter_specs_style) if type != '' else ''
111
- doc = colorize(self.doc, style = parameter_doc_style)
112
- return nl + title + sp + doc + nl + type if self.doc != '' else ''
113
-
114
-
115
- class method_class():
116
- def __init__(self, name, alias = None):
117
- self.name = name.lower()
118
- self.set_doc()
119
- self.alias = alias
120
-
121
- self.parameters = parameters_class()
122
- self.set_output()
123
- self.status = False
124
-
125
- def set_doc(self, doc = ''):
126
- self.doc = correct_doc(doc)
127
-
128
- def set_output(self, doc = '', type = ''):
129
- self.output = output_class(doc, type)
130
-
131
- def append_parameter(self, parameter_object):
132
- self.parameters.append(parameter_object)
133
-
134
- def add_parameter(self, name, doc = '', type = '', default = ''):
135
- self.parameters.add(name, doc, type, default)
136
-
137
- def get_title(self):
138
- return colorize(self.name, method_name_color, method_name_style)
139
-
140
- def get_doc(self):
141
- alias = (nl + "The methods " + colorize(self.name + '()', style = alias_style) + ' and ' + colorize(self.alias + '()', style = alias_style) + ' are equivalent.') if self.alias != '' else ''
142
- pars = self.parameters.get_doc()
143
- out = self.output.get_doc()
144
- return nl.join([el for el in [self.doc, alias, pars, out] if el != ''])
145
-
146
- def get_parameters(self):
147
- return [el.name for el in self.parameters.list]
148
-
149
- def get_parameter(self, name):
150
- return self.parameters.get_parameter(name)
151
-
152
- def show(self):
153
- print(self.get_doc())
154
-
155
-
156
- def get_parameters(method):
157
- spec = args(method)
158
- parameters = ([spec.varargs] if spec.varargs is not None else []) + spec.args + spec.kwonlyargs
159
- parameters = [el for el in parameters if el != 'self']
160
- #defaults = spec.defaults if spec.defaults is not None else spec.kwonlydefaults.values() if spec.kwonlydefaults is not None else []
161
- #lp, ld = len(parameters), len(defaults)
162
- #defaults = [None] * (lp - ld) + list(defaults)
163
- #return [(parameters[i], defaults[i]) for i in range(lp)]
164
- return parameters#, defaults
37
+ doc = doc.strip()
38
+ doc = doc[:1].upper() + doc[1:]
39
+ doc = doc if len(doc) == 0 or doc[-1] == "." else doc + "."
40
+ doc = sub(" ", " ", doc)
41
+ return doc
42
+
43
+
44
+ class ParameterClass: # parameter doc
45
+ def __init__(self, name, doc="", type="", default=""):
46
+ self.name = name.lower()
47
+ self.doc = correct_doc(doc)
48
+ self.type = None if type is None else str(type)
49
+ self.set_default(default)
50
+
51
+ def set_default(self, default=""):
52
+ if default == "":
53
+ self.default = ""
54
+ elif isinstance(default, str):
55
+ self.default = "'" + default + "'"
56
+ elif isinstance(default, float):
57
+ self.default = str(round(default, 3))
58
+ else:
59
+ self.default = str(default)
60
+
61
+ def get_doc(self):
62
+ name = colorize(self.name, parameter_name_color, parameter_name_style)
63
+ type = "" if self.type == "" else "type: " + str(self.type)
64
+ default = "" if self.default == "" else "default: " + self.default
65
+ specs = sc.join([spec for spec in [type, default] if spec != ""])
66
+ specs = (
67
+ nl + colorize(specs, parameter_specs_color, parameter_specs_style)
68
+ if specs != ""
69
+ else ""
70
+ )
71
+ doc = colorize(self.doc, style=parameter_doc_style)
72
+ return nl + name + sp + doc + specs
73
+
74
+ def copy(self, default=""):
75
+ par = copy.copy(self)
76
+ par.set_default(default)
77
+ return par
78
+
79
+
80
+ class ParametersClass:
81
+ def __init__(self):
82
+ self.list = []
83
+
84
+ def append(self, parameter):
85
+ self.list.append(parameter)
86
+
87
+ def add(self, name, doc="", type="", default=""):
88
+ self.append(ParameterClass(name, doc, type, default))
89
+
90
+ def get_title(self):
91
+ lp = len(self.list)
92
+ title = "This is its parameter:" if lp == 1 else "These are its parameters:"
93
+ return colorize(title, parameters_title_color, parameters_title_style)
94
+
95
+ def get_doc(self):
96
+ docs = [el.get_doc() for el in self.list]
97
+ return nl + self.get_title() + nl + nl.join(docs) if len(self.list) != 0 else ""
98
+
99
+ def get_parameter(self, name):
100
+ names = [el.name for el in self.list]
101
+ if name not in names:
102
+ print(warning, "no parameter", name, "found")
103
+ index = names.index(name) if name in names else None
104
+ return self.list[index] if name in names else None
105
+
106
+
107
+ class OutputClass:
108
+ def __init__(self, doc="", type=None):
109
+ self.type = type
110
+ self.doc = correct_doc(doc)
111
+
112
+ def get_doc(self):
113
+ title = colorize("Returns", return_color, return_style)
114
+ type = "" if self.type is None else "type: " + str(self.type)
115
+ type = (
116
+ colorize(type, parameter_specs_color, parameter_specs_style)
117
+ if type != ""
118
+ else ""
119
+ )
120
+ doc = colorize(self.doc, style=parameter_doc_style)
121
+ return nl + title + sp + doc + nl + type if self.doc != "" else ""
122
+
123
+
124
+ class MethodClass:
125
+ def __init__(self, name, alias=None):
126
+ self.name = name.lower()
127
+ self.set_doc()
128
+ self.alias = alias
129
+
130
+ self.parameters = ParametersClass()
131
+ self.set_output()
132
+ self.status = False
133
+
134
+ def set_doc(self, doc=""):
135
+ self.doc = correct_doc(doc)
136
+
137
+ def set_output(self, doc="", type=""):
138
+ self.output = OutputClass(doc, type)
139
+
140
+ def append_parameter(self, parameter_object):
141
+ self.parameters.append(parameter_object)
142
+
143
+ def add_parameter(self, name, doc="", type="", default=""):
144
+ self.parameters.add(name, doc, type, default)
145
+
146
+ def get_title(self):
147
+ return colorize(self.name, method_name_color, method_name_style)
148
+
149
+ def get_doc(self):
150
+ alias = (
151
+ (
152
+ nl
153
+ + "The methods "
154
+ + colorize(self.name + "()", style=alias_style)
155
+ + " and "
156
+ + colorize(self.alias + "()", style=alias_style)
157
+ + " are equivalent."
158
+ )
159
+ if self.alias != ""
160
+ else ""
161
+ )
162
+ pars = self.parameters.get_doc()
163
+ out = self.output.get_doc()
164
+ return nl.join([el for el in [self.doc, alias, pars, out] if el != ""])
165
+
166
+ def get_parameters(self):
167
+ return [el.name for el in self.parameters.list]
168
+
169
+ def get_parameter(self, name):
170
+ return self.parameters.get_parameter(name)
171
+
172
+ def show(self):
173
+ print(self.get_doc())
165
174
 
166
175
 
167
- class documentation_class(): # a list of method_class objects
176
+ def get_parameters(method):
177
+ spec = args(method)
178
+ parameters = (
179
+ ([spec.varargs] if spec.varargs is not None else [])
180
+ + spec.args
181
+ + spec.kwonlyargs
182
+ )
183
+ parameters = [el for el in parameters if el != "self"]
184
+ # defaults = spec.defaults if spec.defaults is not None else spec.kwonlydefaults.values() if spec.kwonlydefaults is not None else []
185
+ # lp, ld = len(parameters), len(defaults)
186
+ # defaults = [None] * (lp - ld) + list(defaults)
187
+ # return [(parameters[i], defaults[i]) for i in range(lp)]
188
+ return parameters # , defaults
189
+
190
+
191
+ class DocumentationClass: # a list of MethodClass objects
168
192
  "It contains the doc-strings of all the main plotext functions."
169
193
 
170
194
  def __init__(self):
171
- self._methods = []
195
+ self._methods = []
172
196
 
173
- def _add_method(self, name, alias = ''):
174
- method = method_class(name, alias)
175
- self._methods.append(method)
176
- setattr(self, name, method.show)
197
+ def _add_method(self, name, alias=""):
198
+ method = MethodClass(name, alias)
199
+ self._methods.append(method)
200
+ setattr(self, name, method.show)
177
201
 
178
202
  def _last(self):
179
- return self._methods[-1]
203
+ return self._methods[-1]
180
204
 
181
205
  def _set_doc(self, doc):
182
- self._last().set_doc(doc)
183
-
184
- def _add_parameter(self, name, doc = '', type = '', default = ''):
185
- self._last().add_parameter(name, doc, type, default)
206
+ self._last().set_doc(doc)
186
207
 
187
- def _set_output(self, doc = '', type = ''):
188
- self._last().set_output(doc, type)
208
+ def _add_parameter(self, name, doc="", type="", default=""):
209
+ self._last().add_parameter(name, doc, type, default)
210
+
211
+ def _set_output(self, doc="", type=""):
212
+ self._last().set_output(doc, type)
189
213
 
190
214
  def _get_method(self, name):
191
- names = [el.name for el in self._methods]
192
- if name not in names:
193
- print(warning, 'no method', name + '() found')
194
- return self._methods[names.index(name)] if name in names else None
215
+ names = [el.name for el in self._methods]
216
+ if name not in names:
217
+ print(warning, "no method", name + "() found")
218
+ return self._methods[names.index(name)] if name in names else None
195
219
 
196
220
  def _get_parameters(self, parameter, method):
197
- method = self.get_method(method)
198
- return method.get_parameters(parameter) if method is not None else None
221
+ method = self.get_method(method)
222
+ return method.get_parameters(parameter) if method is not None else None
199
223
 
200
- def _add_past_parameter(self, name, method, default = None):
201
- method = self._get_method(method)
202
- parameter = method.get_parameter(name) if method is not None else None
203
- parameter = parameter if default is None else parameter.copy(default)
204
- self._last().append_parameter(parameter) if parameter is not None else None
224
+ def _add_past_parameter(self, name, method, default=None):
225
+ method = self._get_method(method)
226
+ parameter = method.get_parameter(name) if method is not None else None
227
+ parameter = parameter if default is None else parameter.copy(default)
228
+ self._last().append_parameter(parameter) if parameter is not None else None
205
229
 
206
230
  def _set_past_output(self, method):
207
- method = self.get_method(method)
208
- output = method.output
209
- self._set_output(output.type, output.doc)
231
+ method = self.get_method(method)
232
+ output = method.output
233
+ self._set_output(output.type, output.doc)
210
234
 
211
235
  def all(self):
212
- docs = (nl * 3).join([el.get_title() + nl + el.get_doc() for el in self._methods if el.status in [0, 1]])
213
- print(docs)
236
+ docs = (nl * 3).join(
237
+ [
238
+ el.get_title() + nl + el.get_doc()
239
+ for el in self._methods
240
+ if el.status in [0, 1]
241
+ ]
242
+ )
243
+ print(docs)
214
244
 
215
245
  def _add_function(self, function):
216
- name = function.__name__
217
- method = self._get_method(name)
218
- name += '()'
219
- if method is None:
220
- print(warning, name, "doc not present")
221
- return
222
- doc = method.get_doc()
223
- function.__doc__ = uncolorize(doc)
224
- function.doc = lambda: print(doc)
225
- parameters_actual = get_parameters(function)
226
- parameters_found = method.get_parameters()
227
- if parameters_actual != parameters_found:
228
- actual = colorize(cm.join(parameters_actual), style = 'italic')
229
- found = colorize(cm.join(parameters_found), style = 'italic')
230
- print(warning, "the parameters of", name, "are", actual, "not", found + '.')
231
-
232
-
233
- class parameter_types():
234
- def __init__(self):
235
- self.int = 'an integer'
236
- self.float = 'a float'
237
- self.num = 'a number'
238
- self.str = 'a string'
239
- self.bool = 'a Boolean'
240
-
241
-
242
- self.ints = 'integers'
243
- self.floats = 'floats'
244
- self.nums = 'numbers'
245
- self.strs = 'strings'
246
- self.bools = 'Booleans'
247
-
248
- self.list_int = lambda n = 'many': self.plural(self.ints, n)
249
- self.list_float = lambda n = 'many': self.plural(self.floats, n)
250
- self.list_num = lambda n = 'many': self.plural(self.nums, n)
251
- self.list_str = lambda n = 'many': self.plural(self.strs, n)
252
- self.list_bool = lambda n = 'many': self.plural(self.bools, n)
253
-
254
- self.fig = 'a plotext figure'
255
- self.xy = 'one or two lists of numbers or string dates'
256
- self.multiple_xy = 'an optional list of numbers or date strings and a mandatory matrix of numbers'
257
- self.x = 'a list of numbers or string dates'
258
- self.marker = 'a string or a list of strings'
259
- self.color = 'a string or an integer (from 0 to 255) or a tuple of 3 integers (from 0 to 255)'
260
- self.colors = 'strings or integers (from 0 to 255) or tuples of 3 integers (from 0 to 255)'
261
- self.list_color = lambda n = 'many': self.plural(self.colors, n)
262
- self.color_list = self.color + ' or a list of those'
263
- self.str_list = self.mix(self.str, self.list_str())
264
-
265
- self.str_int = self.mix(self.str, self.int)
266
- self.str_num = self.mix(self.str, self.num)
267
- self.list_str_num = lambda n = 'many': self.plural(self.mix(self.strs, self.nums), n)
268
- self.list_num_bool = lambda n = 'many': self.plural(self.mix(self.nums, self.bools), n)
269
- self.bool_num_str = self.mix(self.bool, self.num, self.str)
270
- self.dic = "a dictionary with mandatory keys: 'Open', 'Close', 'High', 'Low'; each value should be a list of numbers."
271
- self.matrix = 'a list of numbers or a list of tuples 3 integers (from 0 to 255)'
272
- self.datetime = 'a datetime object'
273
- self.list_datetime = self.plural(self.datetime)
274
- self.data = 'a 2 dimensional matrix of numbers or strings'
275
-
276
- def plural(self, type, n = 'many'):
277
- return 'a list of ' + (str(n) + sp if not isinstance(n, str) else '') + type
278
-
279
- def mix(self, *types):
280
- return ' or '.join(types)
281
-
282
- documentation = documentation_class()
246
+ name = function.__name__
247
+ method = self._get_method(name)
248
+ name += "()"
249
+ if method is None:
250
+ print(warning, name, "doc not present")
251
+ return
252
+ doc = method.get_doc()
253
+ function.__doc__ = uncolorize(doc)
254
+ function.doc = lambda: print(doc)
255
+ parameters_actual = get_parameters(function)
256
+ parameters_found = method.get_parameters()
257
+ if parameters_actual != parameters_found:
258
+ actual = colorize(cm.join(parameters_actual), style="italic")
259
+ found = colorize(cm.join(parameters_found), style="italic")
260
+ print(warning, "the parameters of", name, "are", actual, "not", found + ".")
261
+
262
+
263
+ class ParameterTypes:
264
+ def __init__(self):
265
+ self.int = "an integer"
266
+ self.float = "a float"
267
+ self.num = "a number"
268
+ self.str = "a string"
269
+ self.bool = "a Boolean"
270
+
271
+ self.ints = "integers"
272
+ self.floats = "floats"
273
+ self.nums = "numbers"
274
+ self.strs = "strings"
275
+ self.bools = "Booleans"
276
+
277
+ self.list_int = lambda n="many": self.plural(self.ints, n)
278
+ self.list_float = lambda n="many": self.plural(self.floats, n)
279
+ self.list_num = lambda n="many": self.plural(self.nums, n)
280
+ self.list_str = lambda n="many": self.plural(self.strs, n)
281
+ self.list_bool = lambda n="many": self.plural(self.bools, n)
282
+
283
+ self.fig = "a plotext figure"
284
+ self.xy = "one or two lists of numbers or string dates"
285
+ self.multiple_xy = "an optional list of numbers or date strings and a mandatory matrix of numbers"
286
+ self.x = "a list of numbers or string dates"
287
+ self.marker = "a string or a list of strings"
288
+ self.color = "a string or an integer (from 0 to 255) or a tuple of 3 integers (from 0 to 255)"
289
+ self.colors = "strings or integers (from 0 to 255) or tuples of 3 integers (from 0 to 255)"
290
+ self.list_color = lambda n="many": self.plural(self.colors, n)
291
+ self.color_list = self.color + " or a list of those"
292
+ self.str_list = self.mix(self.str, self.list_str())
293
+
294
+ self.str_int = self.mix(self.str, self.int)
295
+ self.str_num = self.mix(self.str, self.num)
296
+ self.list_str_num = lambda n="many": self.plural(
297
+ self.mix(self.strs, self.nums), n
298
+ )
299
+ self.list_num_bool = lambda n="many": self.plural(
300
+ self.mix(self.nums, self.bools), n
301
+ )
302
+ self.bool_num_str = self.mix(self.bool, self.num, self.str)
303
+ self.dic = "a dictionary with mandatory keys: 'Open', 'Close', 'High', 'Low'; each value should be a list of numbers."
304
+ self.matrix = "a list of numbers or a list of tuples 3 integers (from 0 to 255)"
305
+ self.datetime = "a datetime object"
306
+ self.list_datetime = self.plural(self.datetime)
307
+ self.data = "a 2 dimensional matrix of numbers or strings"
308
+
309
+ def plural(self, type, n="many"):
310
+ return "a list of " + (str(n) + sp if not isinstance(n, str) else "") + type
311
+
312
+ def mix(self, *types):
313
+ return " or ".join(types)
314
+
315
+
316
+ documentation = DocumentationClass()
283
317
  method = documentation._add_method
284
318
  doc = documentation._set_doc
285
319
  par = documentation._add_parameter
@@ -288,4 +322,4 @@ out = documentation._set_output
288
322
  past_out = documentation._set_past_output
289
323
  add = documentation._add_function
290
324
 
291
- t = parameter_types()
325
+ t = ParameterTypes()