On Wednesday, May 30, 2018 at 1:14:13 PM UTC-5, Edward K. Ream wrote:
> There are some aspects of the code the I didn't understand at first, and
I'll explain those fully in later posts.
There is a subtle feature of the code that I promised to explain. At
present, all of the ivars returned by the ivars() *method *are lists or
dicts. *The code never assigns to these lists or dicts, it only changes
their contents.*
This means that the code never has to return these ivars to callers. The
changes happen in place. As a result, the code is surprisingly robust, and
the ivars method does not, in fact, create any problems.
*Style and pyflakes*
Having said that, the ivars() method is dubious, and imo should be replaced
by explicit assignments. Like this:
# ( positions, nodes, attrs, levels, gnx2pos, parPos,
# expanded, marked, selPos) = self.ivars()
attrs = self.attrs
gnx2pos = self.gnx2pos
levels = self.levels
nodes = self.nodes
parPos = self.parPos
positions = self.position
As you can see, not all the tuple values are unpacked. That's correct:
pyflakes will tell us exactly which ivars are, and aren't used.
*Eliminating the ivars method*
The ivars() method can be replaced by a list of ivars to be pickled by the
tobytes method. Like this:
def __init__(self):
self.pickled_vars = (
'positions', 'nodes', 'attrs', 'levels', 'gnx2pos', 'parPos',
'expanded', 'marked', 'selectedPosition',
)
...
def tobytes(self):
'''Returns pickled data of this model'''
# data = self.ivars()
data = (getattr(self, var) for var in self.pickled_vars)
return pickle.dumps(data)
*Summary*
The ivars returned by the ivars() method are all lists or dicts. The code
only changes their contents, never the references. This makes the code
robust, and fast.
Replacing calls to the ivars method allows pyflakes to check the code. At
present, replacing ivars by local vars results in no performance gain at
all. Otoh, using vars instead of ivars reduces visual clutter, so it's not
a big deal, imo.
The ivars() method can be replaced by a list of ivars to be pickled by the
tobytes method.
Edward
--
You received this message because you are subscribed to the Google Groups
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.