python - reload subclass of frozenset -


i have class want have functions of frozenset don't want him configurable (by init, frozenset gets iterable).

additionally, want him have function 'reload' - loading static list server user can not change (so don't want user think can change it). list on server can changed admin need reload option.

that's hoped for:

class a(frozenset):     def __init__(self, list_id):         super().__init__()         self.list_id = list_id         self.reload()      def reload(self):         #loading staff self.list_id...         pass 

but didn't find way 'add' new staff class (i tried re-init it).

may using wrong staff if have anther way fine (i need option compare difference between difference objects):

a = a(1) b = a(2) len(a) iter(a) a.difference(b) 

may overloading add , update of set don't want (it looks bad in code because there more update-like functions).

you cannot update frozenset contents, no; remains immutable when subclassed.

you can subclass collections.abc.set() abstract base class instead; models immutable set too; need implement methods listed in abstract methods column , rest taken care of you:

from collections.abc import set  class a(set):     def __init__(self, list_id):         self.list_id = list_id         self.reload()      def reload(self):         values = get_values(self.list_id)         self._values = frozenset(values)      def __contains__(self, item):         return item in self._values      def __iter__(self):         return iter(self._values)      def __len__(self):         return len(self._values) 

not all methods of built-in frozenset type implemented; can supply missing ones these aliases of operator methods:

def issubset(self, other):     return self <= frozenset(other)  def issuperset(self, other):     return self >= frozenset(other)  def union(self, *others):     res = self     o in others:         res |= frozenset(o)     return res  def intersection(self, *others):     res = self     o in others:         res &= frozenset(o)     return res  def difference(self, *others):     res = self     o in others:         res -= frozenset(o)     return res  def symmetric_difference(self, other):     return self ^ frozenset(other) 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -