Skip to content

Commit

Permalink
Display information about HModel objects (Ref issue: EconForge#22)
Browse files Browse the repository at this point in the history
Added methods __str__, __repr__ and _repr_html_ to the HModel class
  • Loading branch information
NormannR committed Jul 20, 2021
1 parent 31bf276 commit 477e318
Showing 1 changed file with 65 additions and 1 deletion.
66 changes: 65 additions & 1 deletion dolark/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,18 @@ def features(self):

@property
def name(self):
return self.data.get("name", "unnamed")
try:
self.data["name"].value
except Exception as e:
return "Anonymous"

@property
def infos(self):
infos = {
"name": self.name,
"type": "dtcc",
}
return infos

def check(self):

Expand Down Expand Up @@ -451,3 +462,56 @@ def get_starting_rule(self, method="improved_time_iteration", **kwargs):
dr0 = sol.dr

return dr0

def __str__(self):
from dolo.misc.termcolor import colored
from numpy import zeros

s = "Model:\n------\nname: \"{name}\"\ntype: \"{type}\"\n".format(**self.infos)

s += "\nFeatures:\n----------\n"
features = self.features
for f in features.keys():
ind = features[f]
if ind:
s += "{feature} {tick}".format(feature=f, tick=colored("✓", "green"))
else:
s += "{feature} {tick}".format(feature=f, tick=colored("✗", "red"))
s += "\n"

return s

def __repr__(self):
return self.__str__()

def _repr_html_(self):

# general informations
table_infos = """
<table>
<td><b>Model</b></td>
<tr>
<td>name</td>
<td>{name}</td>
</tr>
<tr>
<td>type</td>
<td>{type}</td>
</tr>
</table>""".format(**self.infos)
table = '<tr><td style="text-align:center"><b>Feature</b></td><td style="text-align:center"><b>Value</b></td></tr>\n'

features = self.features
lines = []
for f in features.keys():
ind = features[f]
if ind:
lines.append("<tr><td style=\"text-align:left\">{feature}</td><td style=\"text-align:center\">{tick}</td></tr>".format(feature=f, tick="<span style=\"color: green;\">✓</span>"))
else:
lines.append("<tr><td style=\"text-align:left\">{feature}</td><td style=\"text-align:center\">{tick}</td></tr>".format(feature=f, tick="<span style=\"color: red;\">✗</span>"))
table += str.join("\n", lines)
table = "<table>{}</table>".format(table)

return table_infos + table


0 comments on commit 477e318

Please sign in to comment.