diff --git a/additional_algorithms/class_description.py b/additional_algorithms/class_description.py new file mode 100644 index 0000000..71334d7 --- /dev/null +++ b/additional_algorithms/class_description.py @@ -0,0 +1,57 @@ +''' Aim of this file : + - Use json export from word_challenge to plot performances charts/plots +''' +from jinja2 import Template + +EXPORT_PATH = "documentation/images/visualizations/" +TEMPLATE_PATH = "additional_algorithms/class_description_template.tex.jinja" + +printable_methods = [ + "__init__", + "__repr__", + "__eq__", + "__lt__", + "__hash__", +] + +class ObjectClassToTikz(): + '''Draw TiKz images from class to show graphically the class construction''' + def __init__(self, object, template_path: str = TEMPLATE_PATH, hidden_methods: bool = False) -> None: + self.object = object + self.classname = self.object.__class__.__name__ + self.attributes = self.object.__dict__.keys() + self.methods = [method for method in dir(self.object) + if callable(getattr(self.object, method)) + and (not method.startswith('__') if hidden_methods==False else (method in printable_methods or not method.startswith('__')))] + self.template_path = template_path + self.hidden_methods = hidden_methods + + + def build_elements(self, elements) -> str: + '''Parse attributes/methods from a class''' + attributes_code = "" + for i, key in enumerate(list(elements)): + # Check if hidden method + attributes_code += r"\small{%d}: \verb|%s|" % (i, key) + if i != len(list(elements)) - 1: + attributes_code += "\\\\\n" + return attributes_code + + def save_drawing(self) -> None: + '''Use classes attributes/methods and write in a TiKz template''' + filename = f"class_%s.tex" % self.classname + + context = { + 'title': self.classname, + 'attributes': self.build_elements(self.attributes), + 'methods': self.build_elements(self.methods) + } + + with open(self.template_path, 'r') as f: + template_content = f.read() + + template = Template(template_content) + + with open(EXPORT_PATH + filename, 'w') as f: + f.write(template.render(context)) + diff --git a/additional_algorithms/class_description_template.tex.jinja b/additional_algorithms/class_description_template.tex.jinja new file mode 100644 index 0000000..8b7fc71 --- /dev/null +++ b/additional_algorithms/class_description_template.tex.jinja @@ -0,0 +1,27 @@ +\begin{figure}[h] + \centering + \begin{tikzpicture} + \node[draw, rounded corners=2mm, inner sep = 0.2cm, fill=orange!0] { + \begin{tikzpicture} + \node[inner sep = 1mm] (title) { \Large{\bfseries {{ title }} }}; + % \draw (title.south west) -- (title.south east); + \node[at=(title.south), anchor=north, inner sep=3mm, align=left, fill=green!30!black!10, yshift=-0.2cm] (attributes) { + \begin{minipage}{60mm} + \textbf{Attributes}\\ + {{ attributes }} + \end{minipage} + }; + % \draw (attributes.north west) -- (attributes.north east); + \node[at=(attributes.south), anchor=north, inner sep=3mm, align=left, fill=blue!10, yshift=-0.2cm] (methods) { + \begin{minipage}{60mm} + \textbf{Methods}\\ + {{ methods }} + \end{minipage} + }; + % \draw (methods.north west) -- (methods.north east); + \end{tikzpicture} + }; + \end{tikzpicture} + \caption{Class description - {{ title }} } + \label{class:{{ title }}} +\end{figure} \ No newline at end of file diff --git a/additional_algorithms/template_2D_intersections.tex.jinja b/additional_algorithms/template_2D_intersections.tex.jinja index 878956a..e59f9c2 100644 --- a/additional_algorithms/template_2D_intersections.tex.jinja +++ b/additional_algorithms/template_2D_intersections.tex.jinja @@ -1,12 +1,12 @@ \begin{tikzpicture} - \begin{axis} + \begin{axis}[title={{ title }}] % Segments {% for segment in segments %} {{ segment }} {% endfor %} % Intersections {% for intersection in intersections %} - \node[cross out, text=blue, fill=blue] at {{ intersection }} {$\times$}; + \node[cross out, text=blue, fill=blue] at {{ intersection }} {$\mathbf{\times}$}; {% endfor %} \end{axis} \end{tikzpicture} \ No newline at end of file diff --git a/additional_algorithms/template_performances.tex.jinja b/additional_algorithms/template_performances.tex.jinja index ed1a071..987a61c 100644 --- a/additional_algorithms/template_performances.tex.jinja +++ b/additional_algorithms/template_performances.tex.jinja @@ -1,5 +1,9 @@ \begin{tikzpicture} -\begin{axis}[] +\begin{axis}[ + title={ {{ title }}}, + xlabel={ {{ xlabel }} }, + ylabel={ {{ ylabel }} } +] {% for metric in metrics %} {{ metric }}; {% endfor %}