Fix: Black does its job

This commit is contained in:
Bertrand Benjamin 2019-10-13 21:01:31 +02:00
parent aba43c9f19
commit 931e55c35e
14 changed files with 98 additions and 67 deletions

View File

@ -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:

View File

@ -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:

View File

@ -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

View File

@ -101,7 +101,6 @@ class MO(ABC):
raise NotImplementedError
class Atom(MO):
""" Base Math Object with only one component.

View File

@ -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):

View File

@ -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):

View File

@ -18,6 +18,7 @@ def random_pythagore(v_min = 1, v_max = 10, nbr_format = lambda x : x) :
formated_triplet = [nbr_format(i) for i in triplet]
return formated_triplet
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:

View File

@ -32,11 +32,17 @@ class Dataset(list):
"""
@classmethod
def random(cls, length, data_name="Valeurs",
distrib="gauss", rd_args=(0, 1),
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):
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)
@ -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"

View File

@ -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

View File

@ -4,11 +4,15 @@
from random import randint, uniform, gauss, choice
def random_generator(length,
distrib=gauss, rd_args=(0, 1),
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):
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:

View File

@ -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
"""
@ -109,11 +108,13 @@ class WeightedDataset(dict):
(1, 3, 4, 5, 5)
"""
return (min(self.keys()),
return (
min(self.keys()),
self.quartile(1),
self.quartile(2),
self.quartile(3),
max(self.keys()))
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)

View File

@ -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',
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',
],
install_requires=["multipledispatch", "tabulate"],
)