oopt-gnpy icon indicating copy to clipboard operation
oopt-gnpy copied to clipboard

Replace `collections.namedtuple` with `dataclasses` or `attrs`

Open dutc opened this issue 7 years ago • 2 comments

Referencing #100, we need to improve our Elements base class to better handle optional fields while still validating required fields.

There are two major options we can choose for this:

  • dataclasses (built into Python >3.7, available via pip install dataclasses prior to that) https://www.python.org/dev/peps/pep-0557/
  • attrs (user-supported library) https://github.com/python-attrs/attrs

We want to preserve the immutable behavior of network elements for ease-of-reasoning and optimisation purposes. We want to improve the user interface when creating new fields and improve the robustness of our code in validating user input on required fields.

dutc avatar Oct 23 '18 17:10 dutc

I have a couple of questions related to the "immutable behavior of network elements":

  1. Why is not the Edfa element class using an immutable namedtuple for params like all the other element classes? This looks a bit inconsistent.

  2. What exactly is the importance of this immutable behavior? Currently, for other elements than Edfa, the immutable params is anyway not used after initialization, where it is just used for assigning values to other (mutable) variables. For example in class Roadm you have self.loss = self.params.loss. Then this roadm.loss variable is updated by build_network in network.py. Same for Fiber class variables con_in and con_out for example.

ojnas avatar Nov 06 '18 09:11 ojnas

I have check the data class: I like it a lot. Only drawback it needs python 3.7, which is a bit of a pain to update.

jeanluc-auge avatar Apr 03 '19 08:04 jeanluc-auge