convexengineering / gplibrary

Useful subsystem models
MIT License
10 stars 11 forks source link

Wanted: an elegant way of creating model instances with fewer subs #68

Closed pgkirsch closed 8 years ago

pgkirsch commented 8 years ago

The current module structure for something like the vertical tail model is:


class VerticalTail(CostedConstraintSet):

    def __init__(self)
        # cost
        # constraints

    @classmethod
    def standalone_737(cls):
        substitutions = {...}

    @classmethod
    def aircraft_737(cls):
        substitutions = {...} # almost identical but just with fewer entries

This involves a lot of duplication in the substitutions dict. Notionally, what I would like is for the aircraft_737 to "inherit" (for want of more correct terminology) from the standalone_737 class method and just punch out a few of the entries in the dict.

Any suggestions for an elegant pythonic way of doing this @bqpd @whoburg? Ideally the proposed fix would involve changing as little code as possible.

bqpd commented 8 years ago

You could have another private method or attribute with the common substitutions?


  _subs = {...}

    @classmethod
    def standalone_737(cls):
        substitutions = {...}
        substitutions.update(self._subs)
pgkirsch commented 8 years ago

I could but this doesn't capture the "intent" where the standalone subs dict has the maximum number of substitutions needed for a model to work, and that the coupled (or aircraft) version just allows using fewer of these substitutions. I'm leaning towards something like pop or del. @whoburg suggested creating a third @classmethod that just returns the standalone substitutions dict and both standalone and coupled call this method, and the latter modifies the dictionary as necessary.

bqpd commented 8 years ago

Sounds good!

pgkirsch commented 8 years ago

Seems like a decent consensus then - closing for now