AllenDowney / ModSimPy

Text and supporting code for Modeling and Simulation in Python
https://allendowney.github.io/ModSimPy/
MIT License
823 stars 1.76k forks source link

Cannot run dir() on ModSimSeries objects #50

Closed syclops closed 3 years ago

syclops commented 4 years ago

The following code results in a Pandas KeyError:

import modsim
foo = modsim.modsim.ModSimSeries([])
dir(foo)  # KeyError: 'dt'

This seems to be the result of the definition of ModSimSeries, in which the property definitions of dt and T call pandas.Series.loc. As a result, when calling dir(foo), the standard Python library (accessor.py) seems to try to get a nonexistent row.

Oddly, defining only dt as a row index in the series seems to fix this:

import modsim
bar = modsim.modsim.ModSimSeries({'dt': 2})
dir(bar)  # Works as expected

I suspect that this can be handled by overriding the default __getattribute__ or __getattr__ in ModSimSeries.

AllenDowney commented 3 years ago

Thanks for letting me know about this. I am currently working on Version 4 of the book, which is based on a greatly simplified version of modsim.py. In this version, the results from TimeSeries and SweepSeries are just Pandas Series objects. Since there is no more subclassing, problems like this should be resolved.