def __setitem__(self, key, value, sync=True): if not len(key) == 14: raise FreetimeKeyError(key) if not key[0:4].isdigit(): raise FreetimeKeyError(key) if not key[5:7].isdigit(): raise FreetimeKeyError(key) if not key[8:9].isdigit(): raise FreetimeKeyError(key) if not key[10:12].isdigit(): raise FreetimeKeyError(key) if not key[13:14].isdigit(): raise FreetimeKeyError(key) # # Maintain a list of all repeatable entries for a particular year, month # and weekday daykey = key[:9] if not self.has_key( daykey ): DictType.__setitem__( self, daykey, ([],[]) ) daykeys = self.__getitem__( daykey ) keyvals = [0,1] for keyval in keyvals: if value == keyval: if not key in daykeys[keyval]: daykeys[keyval].append( key ) elif sync: if key in daykeys[keyval]: daykeys[keyval].remove( key ) #print daykeys return DictType.__setitem__(self, key, value)
def __getitem__(self, *kargs): value = False name = kargs[0] if type(name) == type(()): name, value = name[:2] if DictType.__contains__(self, name): value = DictType.__getitem__(self, name) devlog('Config::GetItem', "%s = %s" % (name, value)) return value
def __init__(self, argv=None): DictType.clear(self) # first of all we load default CANVAS config (at the top of that file) for line in self.__default_config.split('\n'): self.__parse_configline(line, 'Config::DefaultConf') # then we parse user's config file self.load_configfile() # and at the end we overwrite config with cmdline options self.parse_argv()
def __getitem__(self, key): if self.has_key( key ): return DictType.__getitem__(self, key) elif len(key) == 9: return ([],[]) else: return None
def __new__(meta, name, bases, dict): # @ Metaclass' __new__ serves as a bi-functional slot capable for # initiating the classes as well as alternating the meta. # @ Suggestion is putting majority of the class initialization code # in __init__, as you can directly reference to cls there; saving # here for anything you want to dynamically added to the meta (such # as shared variables or lazily GC'd temps). # @ Any changes here to dict will be visible to the new class and their # future instances, but won't affect the metaclass. While changes # directly through meta will be visible to all (unless you override # it later). dict['new_elem'] = "effective" meta.var = "Change made to %s by metaclass' __new__" % str(meta) meta.count += 1 print "================================================================" print " Metaclass's __new__ (creates class objects)" print "----------------------------------------------------------------" print "Bounded to object: " + str(meta) print "Bounded object's __dict__: " pprint(DictType(meta.__dict__), depth = 1) print "----------------------------------------------------------------" print "Parameter 'name': " + str(name) print "Parameter 'bases': " + str(bases) print "Parameter 'dict': " pprint(dict, depth = 1) print "\n" return super(FactoryMeta, meta).__new__(meta, name, bases, dict)
def __delitem__(self, key, sync=True): daykey = key[:9] daykeys= self.__getitem__( daykey ) if sync: if key in daykeys[0]: daykeys[0].remove( key ) if key in daykeys[1]: daykeys[1].remove( key ) #print daykeys return DictType.__delitem__( self, key )
def pop(self, key, *defaults): if len(defaults) > 1: raise TypeError, "pop expected at most 2 arguments, got %d" % (1 + len(defaults)) try: v = DictType.__getitem__(self, key) self.__delitem__(key) except KeyError, e: if defaults: return defaults[0] else: raise e
def update(self): pps = self.context.restrictedTraverse('@@plone_portal_state') self.portal = pps.portal() self.orders_folder = self.portal.orders self.orders = [] if self.request.has_key('list_orders.form.submitted'): # we have to have *some* filter criteria or the search will take # forever. oc = getToolByName(self.context, 'order_catalog') query = {'portal_type': 'emas.app.order', 'path': '/'.join(self.orders_folder.getPhysicalPath())} # make it a dict, because I want an easy way to get rid of some keys filter_criteria = DictType(self.request.get('filter_criteria', {})) now = DT() yesterday = now - 1 tomorrow = now + 1 start_date = self.request['order_date_start'] end_date = self.request['order_date_end'] if start_date and end_date: start_date = DT(self.request['order_date_start']) end_date = DT(self.request['order_date_end']) date_query = {'query': [start_date, end_date], 'range': 'minmax'} query['order_date'] = date_query for key, value in filter_criteria.items(): query[key] = value self.orders = oc(query) if self.orders: b_size = int(self.request.get('b_size', 50)) b_start = int(self.request.get('b_start', 0)) self.orders = Batch(self.orders, b_size, b_start) else: self.context.plone_utils.addPortalMessage( _('No orders match your search criteria.') )
def __new__(cls, function): # @ Class' __new__ "creates" the instances. # @ This won't affect the metaclass. But it does alter the class' member # as it is bounded to cls. cls.extra = function print "================================================================" print " Class' __new__ (\"creates\" instance objects)" print "----------------------------------------------------------------" print "Bounded to object: " + str(cls) print "Bounded object's __dict__: " pprint(DictType(cls.__dict__), depth = 1) print "----------------------------------------------------------------" print "Parameter 'function': \n" + str(function) print "\n" return super(Factory, cls).__new__(cls)
def __init__(self, function, *args, **kwargs): # @ Class' __init__ initializes the instances. # @ Changes through self here (normally) won't affect the class or the # metaclass; they are only visible locally to the instances. # @ However, here you have another chance to make "static" things # visible to the instances, "locally". self.classFactory = self.__class__.classFactory print "================================================================" print " Class' __init__ (initiates instance objects)" print "----------------------------------------------------------------" print "Bounded to object: " + str(self) print "Bounded object's __dict__: " pprint(DictType(self.__dict__), depth = 1) print "----------------------------------------------------------------" print "Parameter 'function': \n" + str(function) print "\n" return super(Factory, self).__init__(*args, **kwargs)
def __init__(cls, name, bases, dict): # @ Metaclass' __init__ is the standard slot for class initialization. # Classes' common variables should mainly goes in here. # @ Any changes here to dict won't actually affect anything. While # changes directly through cls will be visible to the created class # and its future instances. Metaclass remains untouched. dict['init_elem'] = "defective" cls.var = "Change made to %s by metaclass' __init__" % str(cls) print "================================================================" print " Metaclass's __init__ (initiates class objects)" print "----------------------------------------------------------------" print "Bounded to object: " + str(cls) print "Bounded object's __dict__: " pprint(DictType(cls.__dict__), depth = 1) print "----------------------------------------------------------------" print "Parameter 'name': " + str(name) print "Parameter 'bases': " + str(bases) print "Parameter 'dict': " pprint(dict, depth = 1) print "\n" return super(FactoryMeta, cls).__init__(name, bases, dict)
def __call__(cls, *args): # @ Metaclass' __call__ gets called when a class name is used as a # callable function to create an instance. It is called before the # class' __new__. # @ Instance's initialization code can be put in here, although it # is bounded to "cls" rather than instance's "self". This provides # a slot similar to the class' __new__, where cls' members can be # altered and get copied to the instances. # @ Any changes here through cls will be visible to the class and its # instances. Metaclass remains unchanged. cls.var = "Change made to %s by metaclass' __call__" % str(cls) # @ "Static" methods defined in the meta which cannot be seen through # instances by default can be manually assigned with an access point # here. This is a way to create shared methods between different # instances of the same metaclass. cls.metaVar = FactoryMeta.metaVar print "================================================================" print " Metaclass's __call__ (initiates instance objects)" print "----------------------------------------------------------------" print "Bounded to object: " + str(cls) print "Bounded object's __dict__: " pprint(DictType(cls.__dict__), depth = 1) print "\n" return super(FactoryMeta, cls).__call__(*args)
def __setitem__(self, name, val): if type(val) == type("") and val.lower() in ['no', 'false']: val = False DictType.__setitem__(self, name, val)
def __str__(self): return "<CANVAS Config instance %s>" % DictType.__repr__(self)
def __getitem__(self, key): if self.has_key(key): return DictType.__getitem__(self, key) else: return None