-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc69deb
commit 150f394
Showing
4 changed files
with
91 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
|
27 changes: 27 additions & 0 deletions
27
additional_algorithms/class_description_template.tex.jinja
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters