From 931e55c35e356a84bd5472d188252557379c1bda Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Sun, 13 Oct 2019 21:01:31 +0200 Subject: [PATCH] Fix: Black does its job --- mapytex/__init__.py | 4 +-- mapytex/calculus/API/expression.py | 11 +++++- mapytex/calculus/API/tokens/token.py | 3 +- mapytex/calculus/core/MO/atoms.py | 1 + mapytex/calculus/core/MO/mo.py | 1 - mapytex/calculus/core/MO/monomial.py | 7 ++-- mapytex/calculus/core/typing/multiply.py | 4 ++- mapytex/geometry/__init__.py | 2 +- mapytex/geometry/pythagore.py | 5 +-- mapytex/stat/dataset.py | 44 ++++++++++++++---------- mapytex/stat/number_tools.py | 4 ++- mapytex/stat/random_generator.py | 23 ++++++++----- mapytex/stat/weightedDataset.py | 33 ++++++++++-------- setup.py | 23 ++++++------- 14 files changed, 98 insertions(+), 67 deletions(-) diff --git a/mapytex/__init__.py b/mapytex/__init__.py index 4cd3175..263836c 100644 --- a/mapytex/__init__.py +++ b/mapytex/__init__.py @@ -1,9 +1,9 @@ #!/usr/bin/env python # encoding: utf-8 -from .calculus import Expression#, Polynom, Fraction, random_str, txt, Equation +from .calculus import Expression # , Polynom, Fraction, random_str, txt, Equation -#Expression.set_render('tex') +# Expression.set_render('tex') from .stat import Dataset, WeightedDataset from .geometry import random_pythagore diff --git a/mapytex/calculus/API/expression.py b/mapytex/calculus/API/expression.py index 0b2d833..7fae25a 100644 --- a/mapytex/calculus/API/expression.py +++ b/mapytex/calculus/API/expression.py @@ -12,7 +12,13 @@ Expression """ from functools import partial from ..core import AssocialTree, Tree, compute, typing, TypingError -from ..core.random import extract_rdleaf, extract_rv, random_generator, compute_leafs, replace_rdleaf +from ..core.random import ( + extract_rdleaf, + extract_rv, + random_generator, + compute_leafs, + replace_rdleaf, +) from ..core.MO import moify from .tokens import factory from .renders import renders @@ -75,6 +81,7 @@ class Expression(object): >>> Expression.set_render('txt') """ from .tokens.token import Token + Token.set_render(render) cls.RENDER = render @@ -485,6 +492,7 @@ def extract_variable(leaf): except AttributeError: return None + def replace(leaf, origin, dest): """ Recursively replace origin to dest in leaf """ try: @@ -497,6 +505,7 @@ def replace(leaf, origin, dest): replace_var = partial(replace, origin=origin, dest=dest) return leaf.tree.map_on_leaf(replace_var) + # ----------------------------- # Reglages pour 'vim' # vim:set autoindent expandtab tabstop=4 shiftwidth=4: diff --git a/mapytex/calculus/API/tokens/token.py b/mapytex/calculus/API/tokens/token.py index 924ac5b..3d3d3fa 100644 --- a/mapytex/calculus/API/tokens/token.py +++ b/mapytex/calculus/API/tokens/token.py @@ -69,7 +69,7 @@ class Token(object): else: raise ValueError(f"Unknow render {self.RENDER}") - #return renders[self.RENDER](self._mo) + # return renders[self.RENDER](self._mo) @property def __txt__(self): @@ -203,7 +203,6 @@ class Token(object): return self._get_soul() <= self._get_soul(other) - # ----------------------------- # Reglages pour 'vim' # vim:set autoindent expandtab tabstop=4 shiftwidth=4: diff --git a/mapytex/calculus/core/MO/atoms.py b/mapytex/calculus/core/MO/atoms.py index 897cfac..1f4270b 100644 --- a/mapytex/calculus/core/MO/atoms.py +++ b/mapytex/calculus/core/MO/atoms.py @@ -26,6 +26,7 @@ def moify(token): except MOError: return token + @coroutine def moify_cor(target): """ Coroutine which try to convert a parsed token into an MO diff --git a/mapytex/calculus/core/MO/mo.py b/mapytex/calculus/core/MO/mo.py index 1729498..74149cc 100644 --- a/mapytex/calculus/core/MO/mo.py +++ b/mapytex/calculus/core/MO/mo.py @@ -101,7 +101,6 @@ class MO(ABC): raise NotImplementedError - class Atom(MO): """ Base Math Object with only one component. diff --git a/mapytex/calculus/core/MO/monomial.py b/mapytex/calculus/core/MO/monomial.py index 41eaeed..b02412b 100644 --- a/mapytex/calculus/core/MO/monomial.py +++ b/mapytex/calculus/core/MO/monomial.py @@ -110,6 +110,7 @@ class MOstrPower(Molecule): 'monome2' """ return f"monome{self.power}" + def differentiate(self): """ differentiate a MOstrPower and get a tree @@ -121,8 +122,10 @@ class MOstrPower(Molecule): > x^2 """ if self._power > 2: - return Tree('*', self.power, MOstrPower(self.variable, self._power._value-1)) - return Tree('*', self.power, MOstr(self.variable)) + return Tree( + "*", self.power, MOstrPower(self.variable, self._power._value - 1) + ) + return Tree("*", self.power, MOstr(self.variable)) class MOMonomial(Molecule): diff --git a/mapytex/calculus/core/typing/multiply.py b/mapytex/calculus/core/typing/multiply.py index fd0d074..883c0ab 100644 --- a/mapytex/calculus/core/typing/multiply.py +++ b/mapytex/calculus/core/typing/multiply.py @@ -27,6 +27,7 @@ multiply_doc = """ Multiply MOs multiply = Dispatcher("multiply", doc=multiply_doc) + def multiply_filter(left, right): """ Automatic multiply on MO @@ -56,6 +57,7 @@ def multiply_filter(left, right): except TypeError: pass + @multiply.register((MOnumber, MOFraction), MOstr) @special_case(multiply_filter) def moscalar_mostr(left, right): @@ -102,7 +104,7 @@ def moscalar_mostrpower(left, right): >>> multiply(a, x) """ - #if left == 1: + # if left == 1: # return right return MOMonomial(left, right) diff --git a/mapytex/geometry/__init__.py b/mapytex/geometry/__init__.py index c078b0c..89880e1 100644 --- a/mapytex/geometry/__init__.py +++ b/mapytex/geometry/__init__.py @@ -6,4 +6,4 @@ from .pythagore import random_pythagore # ----------------------------- # Reglages pour 'vim' # vim:set autoindent expandtab tabstop=4 shiftwidth=4: -# cursor: 16 del +# cursor: 16 del diff --git a/mapytex/geometry/pythagore.py b/mapytex/geometry/pythagore.py index 47294a5..861601c 100644 --- a/mapytex/geometry/pythagore.py +++ b/mapytex/geometry/pythagore.py @@ -5,7 +5,7 @@ from random import randint -def random_pythagore(v_min = 1, v_max = 10, nbr_format = lambda x : x) : +def random_pythagore(v_min=1, v_max=10, nbr_format=lambda x: x): """ Generate a pythagore triplet :returns: (a,b,c) such that a^2 = b^2 + c^2 @@ -14,10 +14,11 @@ def random_pythagore(v_min = 1, v_max = 10, nbr_format = lambda x : x) : while u == v: u, v = randint(v_min, v_max), randint(v_min, v_max) u, v = max(u, v), min(u, v) - triplet = (u**2+v**2, 2*u*v, u**2-v**2) + triplet = (u ** 2 + v ** 2, 2 * u * v, u ** 2 - v ** 2) formated_triplet = [nbr_format(i) for i in triplet] return formated_triplet + # ----------------------------- # Reglages pour 'vim' # vim:set autoindent expandtab tabstop=4 shiftwidth=4: diff --git a/mapytex/stat/dataset.py b/mapytex/stat/dataset.py index 59de98e..7c48188 100644 --- a/mapytex/stat/dataset.py +++ b/mapytex/stat/dataset.py @@ -1,4 +1,4 @@ -#/usr/bin/env python +# /usr/bin/env python # -*- coding:Utf-8 -*- # @@ -32,11 +32,17 @@ class Dataset(list): """ @classmethod - def random(cls, length, data_name="Valeurs", - distrib="gauss", rd_args=(0, 1), - nbr_format=lambda x: round(x, 2), - v_min=None, v_max=None, - exact_mean=None): + def random( + cls, + length, + data_name="Valeurs", + distrib="gauss", + rd_args=(0, 1), + nbr_format=lambda x: round(x, 2), + v_min=None, + v_max=None, + exact_mean=None, + ): """ Generate a random list of value :param length: length of the dataset @@ -47,11 +53,9 @@ class Dataset(list): :param v_max: maximum accepted value :param exact_mean: if set, the last generated number will be create in order that the computed mean is exacly equal to "exact_mean" """ - data = random_generator(length, - distrib, rd_args, - nbr_format, - v_min, v_max, - exact_mean) + data = random_generator( + length, distrib, rd_args, nbr_format, v_min, v_max, exact_mean + ) return cls(data, data_name=data_name) @@ -94,7 +98,7 @@ class Dataset(list): def deviation(self): """ Compute the deviation (not normalized) """ mean = self.mean() - return sum([(x - mean)**2 for x in self]) + return sum([(x - mean) ** 2 for x in self]) @number_factory def variance(self): @@ -120,7 +124,8 @@ class Dataset(list): self.quartile(1), self.quartile(2), self.quartile(3), - max(self)) + max(self), + ) @number_factory def quartile(self, quartile=1): @@ -173,18 +178,21 @@ class Dataset(list): """ Latex code to display dataset as a tabular """ d_per_line = self.effectif_total() // nbr_lines d_last_line = self.effectif_total() % d_per_line - splited_data = [self[x:x + d_per_line] - for x in range(0, self.effectif_total(), d_per_line)] + splited_data = [ + self[x : x + d_per_line] + for x in range(0, self.effectif_total(), d_per_line) + ] # On ajoute les éléments manquant pour la dernière line if d_last_line: - splited_data[-1] += [' '] * (d_per_line - d_last_line) + splited_data[-1] += [" "] * (d_per_line - d_last_line) # Construction du tableau latex = "\\begin{{tabular}}{{|c|*{{{nbr_col}}}{{c|}}}} \n".format( - nbr_col=d_per_line) + nbr_col=d_per_line + ) latex += "\t\t \hline \n" - d_lines = [' & '.join(map(str, l)) for l in splited_data] + d_lines = [" & ".join(map(str, l)) for l in splited_data] latex += " \\\\ \n \\hline \n".join(d_lines) latex += " \\\\ \n \\hline \n" diff --git a/mapytex/stat/number_tools.py b/mapytex/stat/number_tools.py index 3c40b81..fad8860 100644 --- a/mapytex/stat/number_tools.py +++ b/mapytex/stat/number_tools.py @@ -1,4 +1,4 @@ -#/usr/bin/env python +# /usr/bin/env python # -*- coding:Utf-8 -*- from functools import wraps @@ -6,6 +6,7 @@ from functools import wraps def number_factory(fun): """ Decorator which format returned value """ + @wraps(fun) def wrapper(*args, **kwargs): ans = fun(*args, **kwargs) @@ -16,6 +17,7 @@ def number_factory(fun): return round(ans, 2) except AttributeError: return ans + return wrapper diff --git a/mapytex/stat/random_generator.py b/mapytex/stat/random_generator.py index 10aaae3..0a73ceb 100644 --- a/mapytex/stat/random_generator.py +++ b/mapytex/stat/random_generator.py @@ -1,14 +1,18 @@ -#/usr/bin/env python +# /usr/bin/env python # -*- coding:Utf-8 -*- from random import randint, uniform, gauss, choice -def random_generator(length, - distrib=gauss, rd_args=(0, 1), - nbr_format=lambda x: round(x, 2), - v_min=None, v_max=None, - exact_mean=None): +def random_generator( + length, + distrib=gauss, + rd_args=(0, 1), + nbr_format=lambda x: round(x, 2), + v_min=None, + v_max=None, + exact_mean=None, +): """ Generate a random list of value :param length: length of the dataset @@ -47,7 +51,8 @@ def random_generator(length, "gauss": gauss, "uniform": uniform, "randint": randint, - "choice": choice} + "choice": choice, + } try: distrib(*rd_args) except TypeError: @@ -67,11 +72,13 @@ def random_generator(length, last_v = nbr_format((length + 1) * exact_mean - sum(data)) if not validate(last_v): raise ValueError( - "Can't build the last value. Conflict between v_min/v_max and exact_mean") + "Can't build the last value. Conflict between v_min/v_max and exact_mean" + ) data.append(last_v) return data + # ----------------------------- # Reglages pour 'vim' # vim:set autoindent expandtab tabstop=4 shiftwidth=4: diff --git a/mapytex/stat/weightedDataset.py b/mapytex/stat/weightedDataset.py index ca1f75c..4f8614b 100644 --- a/mapytex/stat/weightedDataset.py +++ b/mapytex/stat/weightedDataset.py @@ -1,4 +1,4 @@ -#/usr/bin/env python +# /usr/bin/env python # -*- coding:Utf-8 -*- """ @@ -12,9 +12,11 @@ from .dataset import Dataset from itertools import chain from .number_tools import number_factory + def flatten_list(l): return list(chain(*l)) + class WeightedDataset(dict): """ A weighted dataset with statistics and latex rendering methods @@ -37,11 +39,8 @@ class WeightedDataset(dict): """ def __init__( - self, - datas=[], - data_name="Valeurs", - weights=[], - weight_name="Effectifs"): + self, datas=[], data_name="Valeurs", weights=[], weight_name="Effectifs" + ): """ Initiate the WeightedDataset """ @@ -84,7 +83,7 @@ class WeightedDataset(dict): def deviation(self): """ Compute the deviation (not normalized) """ mean = self.mean() - return sum([v * (k - mean)**2 for (k, v) in self.items()]) + return sum([v * (k - mean) ** 2 for (k, v) in self.items()]) @number_factory def variance(self): @@ -109,11 +108,13 @@ class WeightedDataset(dict): (1, 3, 4, 5, 5) """ - return (min(self.keys()), - self.quartile(1), - self.quartile(2), - self.quartile(3), - max(self.keys())) + return ( + min(self.keys()), + self.quartile(1), + self.quartile(2), + self.quartile(3), + max(self.keys()), + ) @number_factory def quartile(self, quartile=1): @@ -146,8 +147,9 @@ class WeightedDataset(dict): position = self.posi_quartile(quartile) - 1 expanded_values = flatten_list([v * [k] for (k, v) in self.items()]) if position.is_integer(): - return (expanded_values[int(position)] + - expanded_values[int(position) + 1]) / 2 + return ( + expanded_values[int(position)] + expanded_values[int(position) + 1] + ) / 2 else: return expanded_values[ceil(position)] @@ -167,7 +169,8 @@ class WeightedDataset(dict): def tabular_latex(self): """ Latex code to display dataset as a tabular """ latex = "\\begin{{tabular}}{{|c|*{{{nbr_col}}}{{c|}}}} \n".format( - nbr_col=len(self.keys())) + nbr_col=len(self.keys()) + ) latex += "\t \hline \n" data_line = "\t {data_name} ".format(data_name=self.data_name) weight_line = "\t {weight_name} ".format(weight_name=self.weight_name) diff --git a/setup.py b/setup.py index 568582e..9c8d5d9 100644 --- a/setup.py +++ b/setup.py @@ -6,17 +6,14 @@ except ImportError: from distutils.core import setup setup( - name='mapytex', - version='2.1', - description='Computing like a student', - author='Benjamin Bertrand', - author_email='programming@opytex.org', - url='http://git.opytex.org/lafrite/Mapytex', - #packages=['mapytex'], + name="mapytex", + version="2.1", + description="Computing like a student", + author="Benjamin Bertrand", + author_email="programming@opytex.org", + url="http://git.opytex.org/lafrite/Mapytex", + # packages=['mapytex'], packages=find_packages(), - include_package_data = True, - install_requires=[ - 'multipledispatch', - 'tabulate', - ], - ) + include_package_data=True, + install_requires=["multipledispatch", "tabulate"], +)