New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ModelChainResult.__repr__ #1236
Changes from all commits
edf3b53
0551447
2b52088
068c72b
df268d0
db68853
3b005a2
0963ca9
db1a33d
7f66f43
ce3b77b
dde8221
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -254,6 +254,33 @@ | |||||
return surface_tilt, surface_azimuth | ||||||
|
||||||
|
||||||
def _getmcattr(self, attr): | ||||||
""" | ||||||
Helper for __repr__ methods, needed to avoid recursion in property | ||||||
lookups | ||||||
""" | ||||||
out = getattr(self, attr) | ||||||
try: | ||||||
out = out.__name__ | ||||||
except AttributeError: | ||||||
pass | ||||||
return out | ||||||
|
||||||
|
||||||
def _mcr_repr(obj): | ||||||
''' | ||||||
Helper for ModelChainResult.__repr__ | ||||||
''' | ||||||
if isinstance(obj, tuple): | ||||||
return "Tuple (" + ", ".join([_mcr_repr(o) for o in obj]) + ")" | ||||||
if isinstance(obj, pd.DataFrame): | ||||||
return "DataFrame ({} rows x {} columns)".format(*obj.shape) | ||||||
if isinstance(obj, pd.Series): | ||||||
return "Series (length {})".format(len(obj)) | ||||||
# scalar, None, other? | ||||||
return repr(obj) | ||||||
|
||||||
|
||||||
# Type for fields that vary between arrays | ||||||
T = TypeVar('T') | ||||||
|
||||||
|
@@ -385,6 +412,33 @@ | |||||
value = self._result_type(value) | ||||||
super().__setattr__(key, value) | ||||||
|
||||||
def __repr__(self): | ||||||
mc_attrs = dir(self) | ||||||
|
||||||
def _head(obj): | ||||||
try: | ||||||
return obj[:3] | ||||||
except: | ||||||
return obj | ||||||
|
||||||
if type(self.dc) is tuple: | ||||||
num_arrays = len(self.dc) | ||||||
else: | ||||||
num_arrays = 1 | ||||||
kandersolar marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
desc1 = ('=== ModelChainResult === \n') | ||||||
desc2 = (f'Number of Arrays: {num_arrays} \n') | ||||||
attr = 'times' | ||||||
desc3 = ('times (first 3)\n' + | ||||||
f'{_head(_getmcattr(self, attr))}' + | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Seems like this should be fine as long as it's safe to assume There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Won't work if |
||||||
'\n') | ||||||
lines = [] | ||||||
for attr in mc_attrs: | ||||||
if not (attr.startswith('_') or attr=='times'): | ||||||
lines.append(f' {attr}: ' + _mcr_repr(getattr(self, attr))) | ||||||
desc4 = '\n'.join(lines) | ||||||
return (desc1 + desc2 + desc3 + desc4) | ||||||
|
||||||
|
||||||
class ModelChain: | ||||||
""" | ||||||
|
@@ -678,18 +732,8 @@ | |||||
'airmass_model', 'dc_model', 'ac_model', 'aoi_model', | ||||||
'spectral_model', 'temperature_model', 'losses_model' | ||||||
] | ||||||
|
||||||
def getmcattr(self, attr): | ||||||
"""needed to avoid recursion in property lookups""" | ||||||
out = getattr(self, attr) | ||||||
try: | ||||||
out = out.__name__ | ||||||
except AttributeError: | ||||||
pass | ||||||
return out | ||||||
|
||||||
return ('ModelChain: \n ' + '\n '.join( | ||||||
f'{attr}: {getmcattr(self, attr)}' for attr in attrs)) | ||||||
f'{attr}: {_getmcattr(self, attr)}' for attr in attrs)) | ||||||
|
||||||
@property | ||||||
def dc_model(self): | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
dir
sorts the list of attributes alphabetically instead of conceptually. Not necessarily a downside.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, once I saw the alphabetized list my thought was "that's easier to use"