Custom Dictionary and JSON Module in Python -


i have custom python class extends 'dict' built-in type.

######################################################################## class serviceparametersnew(dict):     """"""     _allowed = ['maxrecordcount', 'tables',                 'allowgeometryupdates', 'supportsdisconnectedediting',                 'description', 'name', 'xsspreventioninfo',                 'hasstaticdata', 'capabilities', 'copyrighttext',                 'currentversion', 'size', 'hasversioneddata',                 'units', 'syncenabled', 'supportedqueryformats',                 'editortrackinginfo', 'servicedescription']     _editortrackinginfo = {         "enableeditortracking": true,         "enableownershipaccesscontrol": false,         "allowotherstoupdate": true,         "allowotherstodelete": true}     _xsspreventioninfo = {         "xsspreventionenabled": true,         "xsspreventionrule": "inputonly",         "xssinputrule": "rejectinvalid"}     _tables =  []     #----------------------------------------------------------------------     def __str__(self):         """returns object string"""         val = {}         in self._allowed:             if in self.__dict__:                 val[a] = self.__getitem__(a)         val['table'] = self._tables         val['xsspreventioninfo'] = self._xsspreventioninfo         val['editortrackinginfo'] = self._editortrackinginfo         return json.dumps(val)     #----------------------------------------------------------------------     def __getitem__(self, key):         if key.lower() in [a.lower() in self._allowed]:             return self.__dict__[key]         raise exception("invalid parameter requested")     #----------------------------------------------------------------------     def __setitem__(self, index, value):         self.__dict__[index] = value     #----------------------------------------------------------------------     @property     def xsspreventioninfo(self):         """gets xsspreventioninfo"""         return self._xsspreventioninfo     #----------------------------------------------------------------------     @property     def editortrackinginfo(self):         """gets editortrackinginfo settings"""         return self._editortrackinginfo     #----------------------------------------------------------------------     def updatexsspreventioninfo(self, xsspreventionenabled=true,                                 xsspreventionrule="inputonly",                                 xssinputrule="rejectinvalid"):         """updates xss prevention information"""         self._xsspreventioninfo = {             "xsspreventionenabled": true,             "xsspreventionrule": "inputonly",             "xssinputrule": "rejectinvalid"         }     #----------------------------------------------------------------------     def updateeditortrackinginfo(self,                                  enableeditortracking=true,                                  enableownershipaccesscontrol=false,                                  allowotherstoupdate=true,                                  allowotherstodelete=true                                  ):         """updates editor tracking information"""         self._editortrackinginfo = {             "enableeditortracking": enableeditortracking,             "enableownershipaccesscontrol": enableownershipaccesscontrol,             "allowotherstoupdate": allowotherstoupdate,             "allowotherstodelete": allowotherstodelete         } 

the class going check if value in allowed dictionary if is, entered value.

i figured since inherited dict, use json.dumps(<object>)to dump class string value.

do have override function working?

you misunderstand __dict__ - it's not place data of dict, it's place attributes stored classes. example:

>>> class a(dict): ...   def __init__(self): ...     dict.__init__(self) ...     self.x = 123 ...  >>> a=a() >>> a.__dict__ {'x': 123} 

so, a.__dict__['x'] (but not always) same a.x.

as general rule: do not use __dict__ unless love dark forbidden magicks.

to make class work , happily serialize json, use dict.__getitem__ ordinary superclass method call.

class extendeddict(dict):     _allowed = ['a', 'b']     def __getitem__(self, key):         if key.lower() in [a.lower() in self._allowed]:             # call superclass method             return dict.__getitem__(self, key)         raise exception("invalid parameter requested")  d = extendeddict() d['a'] = 1 d['b'] = 2 import json, sys sys.stdout.write(json.dumps(d)) 

this print:

{"a": 1, "b": 2} 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -