python-openflow icon indicating copy to clipboard operation
python-openflow copied to clipboard

Unpack should return an object

Open cemsbr opened this issue 9 years ago • 8 comments

Refactor unpack methods to return the unpacked object instead of being an inplace method.

Probably the TODO creator meant implementing a class method so we would call FixedTypeList.unpack(buff), for instance.

cemsbr avatar Jul 25 '16 20:07 cemsbr

@beraldoleal I don't think we will be able to reach this feature before kytos/python-openflow#198 and kytos/python-openflow#237.

diraol avatar Apr 25 '17 19:04 diraol

@diraol ok, no problem. b3 is a good target.

beraldoleal avatar May 02 '17 15:05 beraldoleal

~~I was thinking about adding a class method from_bytes like Python int.from_bytes to be more pythonic. What do you think?~~

Edit: Some unpack methods rely on instance attributes (e.g. enum_ref). Thus, a class method won't work for all cases.

cemsbr avatar Jul 25 '17 23:07 cemsbr

@cemsbr Why not use python's method directly to unpack instead of using struct, keeping '.value', etc.. that's what I proposed here: https://github.com/kytos/python-openflow/issues/198#issuecomment-316769379 https://github.com/kytos/python-openflow/compare/master...erickvermot:value_test (line 19 - https://github.com/kytos/python-openflow/compare/master...erickvermot:value_test#diff-83e119f254a95803fa6cdaff8bd5bee3R19)

erickvermot avatar Jul 26 '17 13:07 erickvermot

@erickvermot, I think the pattern pack and _unpack, get_size and _get_size, etc is not the best OOP solution.

cemsbr avatar Jul 27 '17 16:07 cemsbr

@cemsbr why not?

erickvermot avatar Jul 27 '17 16:07 erickvermot

There's no need to create extra private methods. We can have only the public ones and children can override them if necessary.

cemsbr avatar Jul 27 '17 16:07 cemsbr

@cemsbr Yes, you can, and this is the way it is done now. But this makes the derivative classes have to rewrite the docstring, and any other processing that is done prior to the actual packing action (like treating the optional arguments, or updating the length field in GenericMessages derivatives), which leads to massive amounts of redundant code, like in the current implementation. https://github.com/kytos/python-openflow/pull/392/commits/a4c1f0535d9e1a796c85785e525278a0e4490943 commit alone for example leads to a reduction of about 200 lines of code. If we decide not to accept optional value argument the reduction is even greater. The way I propose, (if needed) the user only needs to reimplement the actual packing action, and the user will remain calling pack in the normal way and will always have access to the docstring and other preprocessing without the need to worry about them.

erickvermot avatar Jul 28 '17 18:07 erickvermot