ENH: add " ' " to Function __repr__ #358
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pull request checklist
black rocketpy
) has passed locally and any fixes were madepytest --runslow
) have passed locallyDiscussion
There is a problem in the Dispersion class when it comes to saving Function objects in the
.txt
files. This is a special problem in the.disp_inputs.txt
files, since the values ofpowerOffDrag
,powerOnDrag
and others are received as Functions.What happens in these cases is, when saving in the
.txt
file we simply pass a dictionary, which contains the info of the simulation inputs, into thewrite
function. The interaction between thewrite
function and theFunction
class is so that what gets saved in the file is theFunction
__repr__
. For example:Further on, in the dispersion class, there is an attribute called
inputs_log
that generates, from the input file, a list in which each item is a line of the file. A list of dictionaries with all the inputs necessary to recreate any iteration of the simulation.Now, two problems arise from this:
Function
s are saved is not "redefinable", so we can't recreate thatFunction
object just with its__repr__
information. I believe the only way to do this (from what I know) would be to start usingpickle
. Unfortunately, that is out of the scope of v1.0 since it would take some extra effort to implementinputs_log
is created, it can not interpret each line of the file becauseFunction from R1 to R1 : (Scalar) → (Scalar)
is not a recognizable Python object. It is not a string.The temporary solution I found for this was to just change add a
'
as the first and last character of the__repr__
string. This way, it gets saved in the.txt
as string and can be read by as a python object:I am creating this PR to both ask if there is any better way to do this, or if this is an okay solution to the problem
Does this introduce a breaking change?