def rule(self): if self.subNodes is not None: extension = new.instance(pmml.Extension) extension.tag = "Extension" extension.attrib = {"name": "gain", "value": self.bestGain} extension.children = [] output = new.instance(pmml.CompoundRule) output.tag = "CompoundRule" output.attrib = {} output.children = [extension, self.pmmlTrue] output.predicateIndex = 1 for subNode, var, cat, val in zip(self.subNodes, self.cutVar, self.cutCat, self.cutVal): node = subNode.rule() predicate = new.instance(pmml.SimplePredicate) predicate.tag = "SimplePredicate" if val is None: predicate.attrib = {"field": var, "operator": "equal", "value": cat} else: predicate.attrib = {"field": var, "operator": "greaterThan" if cat else "lessOrEqual", "value": val} predicate.children = [] node[node.predicateIndex] = predicate output.children.append(node) else: output = new.instance(pmml.SimpleRule) output.tag = "SimpleRule" output.attrib = {"score": self.score} output.children = [self.pmmlTrue] output.predicateIndex = 0 return output
def unjelly(self, obj): if type(obj) is not types.ListType: return obj self.references.append(_theNullRef) jelType = obj[0] if not self.taster.isTypeAllowed(jelType): raise InsecureJelly(jelType) regClass = unjellyableRegistry.get(jelType) if regClass is not None: if isinstance(regClass, ClassType): inst = _Dummy() # XXX chomp, chomp self.resolveReference(inst) inst.__class__ = regClass val = inst.unjellyFor(self,obj) else: refid = self.getRefId() self.resolveReference(NotKnown()) val = regClass(self, obj) # this is how it ought to be done self.resolveReference(val, refid) if hasattr(val, 'postUnjelly'): self.postCallbacks.append(inst.postUnjelly) return val regFactory = unjellyableFactoryRegistry.get(jelType) if regFactory is not None: refid = self.getRefId() self.resolveReference(NotKnown()) state = self.unjelly(obj[1]) inst = regFactory(state) if hasattr(inst, 'postUnjelly'): self.postCallbacks.append(inst.postUnjelly) return self.resolveReference(inst, refid) thunk = getattr(self, '_unjelly_%s'%jelType, None) if thunk is not None: ret = thunk(obj[1:]) else: nameSplit = string.split(jelType, '.') modName = string.join(nameSplit[:-1], '.') if not self.taster.isModuleAllowed(modName): raise InsecureJelly("Module %s not allowed." % modName) clz = namedObject(jelType) if not self.taster.isClassAllowed(clz): raise InsecureJelly("Class %s not allowed." % jelType) if hasattr(clz, "__setstate__"): ret = instance(clz, {}) self.resolveReference(ret) state = self.unjelly(obj[1]) ret.__setstate__(state) else: ret = instance(clz, {}) self.resolveReference(ret) state = self.unjelly(obj[1]) ret.__dict__ = state if hasattr(clz, 'postUnjelly'): self.postCallbacks.append(ret.postUnjelly) return ret
def _unjelly_instance(self, rest): clz = self.unjelly(rest[0]) if type(clz) is not types.ClassType: raise InsecureJelly("Instance found with non-class class.") if hasattr(clz, "__setstate__"): inst = instance(clz, {}) state = self.unjelly(rest[1]) inst.__setstate__(state) else: state = self.unjelly(rest[1]) inst = instance(clz, state) if hasattr(clz, 'postUnjelly'): self.postCallbacks.append(inst.postUnjelly) return inst
def __copy__(self): obj = new.instance(self.__class__, self.__dict__) obj.distutils_vars = obj.distutils_vars.clone(obj._environment_hook) obj.command_vars = obj.command_vars.clone(obj._environment_hook) obj.flag_vars = obj.flag_vars.clone(obj._environment_hook) obj.executables = obj.executables.copy() return obj
def __new(cls, C): try: import new return new.instance(C) except: pass return Exception.__new__(C)
def _simplePredicate(self, field, value, operator): p = new.instance(pmml.SimplePredicate) p.tag = "SimplePredicate" p.children = [] p.attrib = dict(field=field, value=value, operator=operator) p.post_validate() return p
def getNewInstance(fullClassName, searchPath=['./']): """@return: an instance of the fullClassName class WITHOUT the C{__init__} method having been called""" # Was original, then modified with parts of # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/223972 parts = fullClassName.split('.') className = parts.pop() moduleName = parts.pop() package = '.'.join(parts) try: module = sys.modules[package + '.' + moduleName] except KeyError: if len(parts) > 0: module = __import__(package + "." + moduleName, globals(), locals(), ['']) else: module = __import__(moduleName, globals(), locals()) function = getattr(module, className) if not callable(function): raise ImportError(fullFuncError) return new.instance(function)
def load(cls, filename): f = open(filename) data = cPickle.load(f) f.close() obj = new.instance(cls, data) obj.verbose = False return obj
def get_class(kls, attrs): parts = __name__.split('.') module = "%s.%s" % (".".join(parts[:-1]), kls) m = importlib.import_module(module) m = getattr(m, kls.title()) m = new.instance(m, attrs) return m
def evaluate(self): classe = _RpcUtils.class_for_name(self.serviceIntfName) if self.serviceMethodName not in classe.__dict__: raise _RpcException(__err_msg__["met.nf"] % (self.serviceIntfName, self.serviceMethodName)) serialization = {} methods = [] _RpcUtils.multi_inherit_serialization(classe, serialization, methods) if len(serialization) > 0: if self.serviceMethodName in serialization: self.returnType = serialization[self.serviceMethodName] else: raise _RpcException(__err_msg__["ser.met.nf"] % (self.serviceIntfName, self.serviceMethodName)) else: raise _RpcException(__err_msg__["ser.nf"] % (self.serviceIntfName)) instance = new.instance(classe) method = getattr(instance, self.serviceMethodName) RpcHandler.ctx.serviceInstance = instance RpcHandler.ctx.methodInstance = method RpcHandler._callInterceptors("beforeEvaluate") val = method.__call__(*self.parameterValues) RpcHandler.ctx.responseObject = val RpcHandler._callInterceptors("afterEvaluate") return val
def _new(S, name, args, kwargs): # can only call _new if we have a matching class to instantiate if not S._class: raise RuntimeError, "instances of this type not available" # check that it isn't already there! try: getinst(S._path, name) raise RuntimeError, "tried to create an instance that already exists" except NoSuchObject: pass # we *expect* this # construct the new object. Do *not* call __init__ yet! newobj = new.instance(S._class, {}) # Call Object.__setup__ to do required object setup, eg. set object # name. fullname = S._path + '%' + name Object.__setup__(newobj, fullname, name) # Call the real __init__ if it's there if hasattr(newobj, '__init__'): apply(newobj.__init__, args, kwargs) # Remember to cache the new instance! ocache[fullname] = newobj return newobj
def falseSimplePredicate(self): output = new.instance(pmml.SimplePredicate) output.tag = "SimplePredicate" output.attrib = {"field": self.name, "operator": "lessOrEqual", "value": self.value} output.children = [] output.needsValue = False return output
def __deepcopy__(self, memo={}): output = new.instance(self.__class__) output.__dict__ = copy.deepcopy(self.__dict__, memo) if "repr" in output.__dict__: del output.__dict__["repr"] memo[id(self)] = output return output
def recv_packet(self, block=True): buf = self.recv_buf packet = self.recv_pkt while True: if self.recv_ready(): break if packet.parse(buf) == -1: return -1 # TODO: callbacks if packet.event == packet.EVENT_NONE: if block: self.net_recv() else: return None elif packet.event == packet.EVENT_READY: #print 'body ready' pass elif packet.event == packet.EVENT_CHUNK: #print 'chunk', repr(packet.chunk_body) packet.body += packet.chunk_body elif packet.event == packet.EVENT_HEAD: #print 'head ready' pass if packet.get_header('Connection').lower() == 'keep-alive': #print 'keep-alive' self.keep_alive = True else: #print 'not keep-alive' self.keep_alive = False self.recv_pkt = new.instance(self.recv_pkt.__class__) self.recv_pkt.__init__() return packet
def getRandomStruct(typeCode, compRef): ''' Helper function ''' structDef = getDefinition(typeCode.id()) try: return getKnownBaciType(structDef._get_id()) except: pass #determine which namespace the struct is in... #changes 'IDL:alma/someMod/.../struct:1.0" to # [ 'someMod', ...,'struct' ] nameHierarchy = structDef._get_id().split(':')[1].split('/')[1:] #Just the 'struct' part... structName = nameHierarchy.pop() moduleName = nameHierarchy.pop(0) LOGGER.logTrace("module=" + moduleName + "; hierarchy=" + str(nameHierarchy) + "; struct=" + structName) #import the top module tGlobals = {} tLocals = {} #module object where the struct is contained container = __import__(moduleName, tGlobals, tLocals, []) if container == None: msg = "import of module \'" + moduleName + "\' failed" LOGGER.logCritical(msg) raise CORBA.NO_IMPLEMENT(msg) # Now navigate down the nested hierarchy of objects for h in nameHierarchy: previousContainer = container container = container.__dict__.get(h) if container == None: msg = "Name \'" + h + "\' not found in " + str( previousContainer) LOGGER.logCritical(msg) raise CORBA.NO_IMPLEMENT(msg) #class object for the struct tClass = container.__dict__.get(structName) if tClass == None: msg = "Could not get structure \'" + structName + "\' from " \ + str(container) LOGGER.logCritical(msg) raise CORBA.NO_IMPLEMENT(msg) #create an instance of the struct using a kooky Python mechanism. retVal = instance(tClass) #populate the fields of the struct using the IFR for member in structDef._get_members(): LOGGER.logTrace("Adding a member variable for: " + str(member.name)) retVal.__dict__[member.name] = getRandomValue(member.type_def._get_type(), compRef) return retVal
def receiveClose(self): klass = reflect.namedObject(self.classname) assert type(klass) == types.ClassType # TODO: new-style classes obj = instance(klass, {}) setInstanceState(obj, self.d) self.protocol.setObject(self.count, obj) self.deferred.callback(obj) return obj, None
def makeClass(module, classname, dict): exec('import %s' % (module)) klass = eval('%s.%s' % (module, classname)) obj = new.instance(klass) if hasMethod(obj, 'from_yaml'): return obj.from_yaml(dict) obj.__dict__ = dict return obj
def accept(self): sock, addr = self.sock.accept() link = new.instance(self.__class__) link.__init__(sock) link.role = LINK_ROLE_ACCEPT link.parent = self link.remote_addr = "%s:%d" % sock.getpeername() return link
def unjelly(self, obj): if type(obj) is not types.ListType: return obj jelType = obj[0] if not self.taster.isTypeAllowed(jelType): raise InsecureJelly(jelType) regClass = unjellyableRegistry.get(jelType) if regClass is not None: if isinstance(regClass, ClassType): inst = _Dummy() # XXX chomp, chomp inst.__class__ = regClass method = inst.unjellyFor else: method = regClass # this is how it ought to be done val = method(self, obj) if hasattr(val, 'postUnjelly'): self.postCallbacks.append(inst.postUnjelly) return val regFactory = unjellyableFactoryRegistry.get(jelType) if regFactory is not None: state = self.unjelly(obj[1]) inst = regFactory(state) if hasattr(inst, 'postUnjelly'): self.postCallbacks.append(inst.postUnjelly) return inst thunk = getattr(self, '_unjelly_%s'%jelType, None) if thunk is not None: ret = thunk(obj[1:]) else: nameSplit = string.split(jelType, '.') modName = string.join(nameSplit[:-1], '.') if not self.taster.isModuleAllowed(modName): raise InsecureJelly("Module %s not allowed (in type %s)." % (modName, jelType)) clz = namedObject(jelType) if not self.taster.isClassAllowed(clz): raise InsecureJelly("Class %s not allowed." % jelType) if hasattr(clz, "__setstate__"): ret = instance(clz, {}) state = self.unjelly(obj[1]) ret.__setstate__(state) else: state = self.unjelly(obj[1]) ret = instance(clz, state) if hasattr(clz, 'postUnjelly'): self.postCallbacks.append(ret.postUnjelly) return ret
def bestTree(self, parent, bestClassification, producer): if self.split is None: pmmlTrue = new.instance(pmml.pmmlTrue) pmmlTrue.tag = "True" pmmlTrue.attrib = {} pmmlTrue.children = [] trueNode = new.instance(pmml.Node) trueNode.tag = "Node" trueNode.attrib = {"score": bestClassification} trueNode.children = [pmmlTrue] trueNode.test = pmmlTrue.createTest() parent[producer.nodeIndex] = trueNode if len(self.true_outworlds) > 0: # find the single best world using max (not sorting!) max(self.true_outworlds.values(), key=lambda x: x.split.gainCache).bestTree( trueNode, bestClassification, producer ) else: trueNode = new.instance(pmml.Node) trueNode.tag = "Node" trueNode.attrib = {"score": self.split.score(True)} trueNode.children = [self.split.trueSimplePredicate()] trueNode.test = trueNode.children[0].createTest() falseNode = new.instance(pmml.Node) falseNode.tag = "Node" falseNode.attrib = {"score": self.split.score(False)} falseNode.children = [self.split.falseSimplePredicate()] falseNode.test = falseNode.children[0].createTest() parent.children.append(trueNode) parent.children.append(falseNode) if len(self.true_outworlds) > 0 and len(self.false_outworlds) > 0: # find the single best world using max (not sorting!) max(self.true_outworlds.values(), key=lambda x: x.split.gainCache).bestTree( trueNode, bestClassification, producer ) max(self.false_outworlds.values(), key=lambda x: x.split.gainCache).bestTree( falseNode, bestClassification, producer )
def cloneNode(self, deep): import new clone = new.instance(self.__class__, self.__dict__) clone.attributes = self.attributes.copy() if not deep: clone.childNodes = [] else: clone.childNodes = map(lambda x: x.cloneNode, self.childNodes) return clone
def instance(klass, d): if isinstance(klass, types.ClassType): return new.instance(klass, d) elif isinstance(klass, type): o = object.__new__(klass) o.__dict__ = d return o else: raise TypeError, "%s is not a class" % klass
def createLogFilter(self, filterNode): type = filterNode.getAttribute(u"type") # $NON-NLS-1$ on = filterNode.getAttribute(u"on") # $NON-NLS-1$ pattern = filterNode.getText() inst = instance(self.filterMap[type]) inst.__init__(on, pattern) return inst
def svg(self): self._svg = new.instance(svg.SVG) self._svg.__dict__["tag"] = "g" self._svg.__dict__["attrib"] = self.attrib self._svg.__dict__["_svg"] = self._svg self._svg.__dict__["children"] = [] for child in self.children: self._svg.__dict__["children"].append(transform(self.trans, child))
def __deepcopy__(self, memo={}): mostdict = copy.copy(self.__dict__) del mostdict["trans"] if "repr" in mostdict: del mostdict["repr"] output = new.instance(self.__class__) output.__dict__ = copy.deepcopy(mostdict, memo) output.__dict__["trans"] = copy.copy(self.trans) memo[id(self)] = output return output
def getReduced(self): """ Returns an object of my own class, containing reduced versions of myself and the auxiliary criterion parts. """ rdatapart=self.datapart.getReduced() rauxobjects=[o.getReduced(datapart=rdatapart) for o in self.auxobjects ] r=new.instance(self.__class__,{}) r.datapart=rdatapart r.auxobjects=rauxobjects return r
def cloneNode(self, deep): import new clone = new.instance(self.__class__, self.__dict__.copy()) if self._makeParentNodes: clone.parentNode = None clone.childNodes = [] if deep: for child in self.childNodes: clone.appendChild(child.cloneNode(1)) return clone
def getCommands(slack, trello): import inspect, new, command from command import Command commands = [] for name, obj in inspect.getmembers(command): if inspect.isclass(obj) and issubclass(obj, Command) and obj != Command: commands.append(new.instance(obj, {'slack': slack, 'trello': trello})) logging.info('Found and instantiated command ' + name) return sorted(commands, lambda x, y: cmp(y.importance, x.importance))
def getExplorer(self, object, identifier): oid = id(object) if self.data.has_key(oid): return self.data[oid] else: klass = typeTable.get(type(object), ExplorerGeneric) e = new.instance(klass, {}) self.data[oid] = e klass.__init__(e, object, identifier) return e
def __deserializeObject(self, name): classe = _RpcUtils.class_for_name(name) instance = new.instance(classe) serialization = dict() fields = [] _RpcUtils.multi_inherit_serialization(classe, serialization, fields) for fname in fields: val = self.__deserializeValue(serialization[fname]) instance.__dict__[fname] = val return instance
def _newInstance(cls, state): """ Make a new instance of a class without calling its __init__ method. 'state' will be used to update inst.__dict__ . Supports both new- and old-style classes. """ if not isinstance(cls, types.ClassType): # new-style inst = cls.__new__(cls) inst.__dict__.update(state) # Copy 'instance' behaviour else: inst = instance(cls, state) return inst
def _newInstance(cls, state=_NO_STATE): """ Make a new instance of a class without calling its __init__ method. Supports both new- and old-style classes. @param state: A C{dict} used to update C{inst.__dict__} or C{_NO_STATE} to skip this part of initialization. @return: A new instance of C{cls}. """ if not isinstance(cls, types.ClassType): # new-style inst = cls.__new__(cls) if state is not _NO_STATE: inst.__dict__.update(state) # Copy 'instance' behaviour else: if state is not _NO_STATE: inst = instance(cls, state) else: inst = instance(cls) return inst
def initialize(self, existingSegment=False, customProcessing=None, setModelMaturity=False): """Initialize the consumer, the producer, and start the maturity count.""" self.updator = self.engine.producerUpdateScheme.updator( COUNT) # use the producer's UpdateScheme if not existingSegment: self.lock = False self.pmmlModelMaturity.attrib["locked"] = False else: if setModelMaturity or ( "updateExisting" in self.producerParameters and self.producerParameters["updateExisting"] is True): self.updator.initialize( {COUNT: self.pmmlModelMaturity.attrib["numUpdates"]}) self.lock = self.pmmlModelMaturity.attrib["locked"] self.consumerAlgorithm.initialize() self.producerAlgorithm.initialize(**self.producerParameters) self.constants = self.pmmlModel.child(pmml.Extension, exception=False) if self.constants is None: self.constants = NameSpaceReadOnly() else: self.constants = self.constants.child( pmml.X_ODG_CustomProcessingConstants, exception=False) if self.constants is None: self.constants = NameSpaceReadOnly() else: self.constants = self.constants.nameSpace self.userFriendly = getattr(self, "userFriendly", new.instance(Segment)) self.userFriendly.name = self.name() self.userFriendly.pmmlPredicate = self.pmmlPredicate self.userFriendly.expression = self.expressionTree self.userFriendly.evaluate = self.predicateMatches self.userFriendly.pmmlModel = self.pmmlModel self.userFriendly.consumer = self.consumerAlgorithm self.userFriendly.producer = self.producerAlgorithm self.userFriendly.const = self.constants self.userFriendly.state = self.state if customProcessing is not None: db = customProcessing.persistentStorage.db if self.userFriendly.name not in db: db[self.userFriendly.name] = NameSpace() self.userFriendly.db = db[self.userFriendly.name]
def receiveClose(self): # you could attempt to do some value-checking here, but there would # probably still be holes #obj = Dummy() klass = reflect.namedObject(self.classname) assert type(klass) == types.ClassType # TODO: new-style classes obj = instance(klass, {}) setInstanceState(obj, self.d) self.protocol.setObject(self.count, obj) self.deferred.callback(obj) return obj, None
def callFun(func,var):#反射调用 # os.chdir("./AutoMan/addtion/parse/") modName = cla.get(func) relPath = "AutoMan.addtion.parse."#web工程的相对路径 # funcMod = __import__(modName+"."+modName,{},{},[modName]) funcMod = __import__(relPath+modName+"."+modName,{},{},[modName]) print "++++++++++++++moudle:"+str(funcMod) funcClass = getattr(funcMod,cla.get(func)) instance = new.instance(funcClass) funName = getattr(funcClass,fun.get(func)) # print type(var) data = apply(funName,[instance,var]) return data
def setProvenance(self, model, algoName, algorithm, userParameters): model.attrib["algorithmName"] = algoName parameters = dict(algorithm.defaultParams) parameters.update(userParameters) extension = model.child(pmml.Extension, exception=False) if extension is None: extension = new.instance(pmml.Extension) extension.tag = "Extension" extension.attrib = {} extension.children = [] else: extension.children = [c for c in extension.children if not isinstance(c, pmml.X_ODG_AlgorithmParameter)] keys = parameters.keys() keys.sort() for key in keys: ap = new.instance(pmml.X_ODG_AlgorithmParameter) ap.tag = "X-ODG-AlgorithmParameter" ap.attrib = {"name": key, "value": parameters[key]} ap.children = [] extension.children.append(ap)
def _compoundAnd(self, *predicates): if len(predicates) == 0: raise Exception, " ".join([ "Encountered a list of zero predicates in", "SegmentationScheme's _compoundAnd; this", "should not ever be possible." ]) elif len(predicates) == 1: return predicates[0] else: c = new.instance(pmml.CompoundPredicate) c.tag = "CompoundPredicate" c.children = predicates c.attrib = dict(booleanOperator="and") return c
def new(T, state=None): """ Build objects. :param T: The type of object to build. :type T: type|class :param state: Optional object state. :type state: dict :return: The new object. """ if issubclass(T, object): inst = T.__new__(T) inst.__dict__.update(state or {}) else: inst = _new.instance(T, state) return inst
def _compoundRange(self, rangeTuple): f, low, high, closure = rangeTuple opL = "greaterOrEqual" if closure.startswith('c') else "greaterThan" opH = "lessOrEqual" if closure.endswith('d') else "lessThan" if high is None: return self._simplePredicate(field=f, operator=opL, value=low) elif low is None: return self._simplePredicate(field=f, operator=opH, value=high) else: p1 = self._simplePredicate(field=f, operator=opL, value=low) p2 = self._simplePredicate(field=f, operator=opH, value=high) c = new.instance(pmml.CompoundPredicate) c.tag = "CompoundPredicate" c.children = [p1, p2] c.attrib = dict(booleanOperator="and") return c
def run_task_job(queue_key): q = Q(queue_key) while True: msg = q.pop() try: print msg task_data = json.loads(msg[1]) task = task_data['task'] task = 'syncTask.task.task.testTask' task_list = task.split(".") module = importlib.import_module(".".join(task_list[:-1])) reload(module) TaskClass = getattr(module, task_list[-1]) obj = new.instance(TaskClass) obj.run(**json.loads(task_data['params'])) except Exception, ex: print str(ex)
def parse_string(self, data, source='<string>', debug=None, tracking=0): if not isinstance(data, basestring): raise AttributeError, \ "argument must be a string, was '%s'" % type(f) import new lexer = self.lex.clone() lexer.input(data) self.lexers.append((lexer, source)) dict = { 'productions' : self.yacc.productions, 'action' : self.yacc.action, 'goto' : self.yacc.goto, 'errorfunc' : self.yacc.errorfunc, } parser = new.instance(ply.yacc.LRParser, dict) result = parser.parse(lexer=lexer, debug=debug, tracking=tracking) self.lexers.pop() return result
def svg(self): obj = new.instance(svg.SVG) obj.__dict__["tag"] = "path" obj.__dict__["attrib"] = self.attrib obj.__dict__["children"] = self.children obj.__dict__["_svg"] = obj obj.attrib["d"] = self.d() if self.marks == []: output = obj else: output = svg.SVG("g", obj) lowX, lowY = self(self.low) highX, highY = self(self.high) for item in self.marks: if isinstance(item, (int, long, float)): t, mark = item, glyphs.tick else: t, mark = item # marks should be (pos, mark) pairs or just pos X, Y = self(t) if (self.low <= t <= self.high or math.sqrt((X - lowX)**2 + (Y - lowY)**2) < trans.epsilon or math.sqrt((X - highX)**2 + (Y - highY)**2) < trans.epsilon): angle = self.angle(t) if isinstance(mark, basestring): mark = self._render_text(X, Y, angle, mark) else: mark = trans.transform( lambda x, y: (X + math.cos(angle) * x - math.sin(angle) * y, Y + math.sin(angle) * x + math.cos(angle) * y), mark) output.append(mark) self._svg = output
def InitBuildInfo(nickname, para): buildConfFile = './BuildSwitch/BuildStep.xml' buildStep = [] try: dom = xml.dom.minidom.parse(buildConfFile) root = dom.documentElement for buildNode in root.childNodes: if buildNode.nodeType != buildNode.ELEMENT_NODE: continue if nickname.lower() != buildNode.getAttribute('nickname'): continue product = buildNode.getAttribute('product') buildstep.BuildStep.g_t_weight = 0 buildstep.BuildStep.g_c_weight = 0 for stepNode in buildNode.childNodes: if stepNode.nodeType != stepNode.ELEMENT_NODE: continue name = stepNode.getAttribute('name') value = stepNode.getAttribute('value') order = stepNode.getAttribute('order') weight = stepNode.getAttribute('weight') buildstep.BuildStep.g_t_weight += int(weight) try: clsName = '' if product == 'bdm': clsName = getattr(buildstep, buildstep.build_step_creator[name]) elif product == 'bdkv': clsName = getattr(buildstep, buildstep.kvbuild_step_creator[name]) step = new.instance(clsName) step.__init__(name, int(value), int(order), int(weight), para) buildStep.append(step) except Exception, e: logging.error("error occers when creating buildsteps") logging.error(e) return (buildStep, False) return (buildStep, True)
def load_parsers(config): ''' 根据配置文件加载所有的parsers ''' import sys import new parsers = {} strs = config.get("slave", "parsers").strip().split(" ") for name in strs: name = name.strip() if len(name) == 0: continue file_path = config.get(name, "file_path") class_name = config.get(name, "class_name") sys.path.append(file_path) mod = __import__(name) clazz = getattr(mod, class_name) parser = new.instance(clazz) source = config.get(name, "source") source = source.strip() parsers[source] = parser return parsers
def instance_noinit(C): """Create an instance of class C without calling __init__ [Note: This function was greatly simplified in gnosis-1.0.7, but I'll leave these notes here for future reference.] We go to some lengths here to avoid calling __init__. It gets complicated because we cannot always do the same thing; it depends on whether there are __slots__, and even whether __init__ is defined in a class or in its parent. Even still, there are ways to construct *highly artificial* cases where the wrong thing happens. If you change this, make sure ALL test cases still work ... easy to break things """ if isOldStyleInstance(C): import new # 2nd arg is required for Python 2.0 (optional for 2.1+) return new.instance(C, {}) elif isNewStyleInstance(C): return C.__new__(C) else: raise TypeError("You must specify a class to create instance of.")
def svg(self): if (self.xmin is not None and self.xmax is not None and self.ymin is not None and self.ymax is not None): self.trans = trans.window( self.xmin, self.xmax, self.ymin, self.ymax, x=self.x, y=self.y, width=self.width, height=self.height, xlogbase=self.xlogbase, ylogbase=self.ylogbase, minusInfinityX=(self.x - 10. * self.width), minusInfinityY=(self.y - 10. * self.height), flipx=self.flipx, flipy=self.flipy) else: self.fit() self._svg = new.instance(svg.SVG) self._svg.__dict__["tag"] = "g" self._svg.__dict__["attrib"] = self.attrib self._svg.__dict__["_svg"] = self._svg self._svg.__dict__["children"] = [] for child in self.children: self._svg.__dict__["children"].append( trans.transform(self.trans, child)) if self.clip: clipPath = svg.SVG("clipPath", id=svg.randomid("clip-"))(svg.SVG( "rect", self.x, self.y, self.width, self.height)) self._svg["clip-path"] = "url(#%s)" % clipPath["id"] self._svg = svg.SVG("g", clipPath, self._svg)
def FFIInstance(classdef, this = 0, userManagesMemory = 0): answer = instance(classdef) answer.this = this answer.userManagesMemory = userManagesMemory return answer
def clone(self): return new.instance(self.__class__,self.__dict__.copy())
def unjellyAO(self, ao): """Unjelly an Abstract Object and everything it contains. I return the real object. """ self.stack.append(ao) t = type(ao) if t is types.InstanceType: #Abstract Objects c = ao.__class__ if c is Module: return reflect.namedModule(ao.name) elif c in [Class, Function] or issubclass(c, type): return reflect.namedObject(ao.name) elif c is InstanceMethod: im_name = ao.name im_class = reflect.namedObject(ao.klass) im_self = self.unjellyAO(ao.instance) if im_name in im_class.__dict__: if im_self is None: return getattr(im_class, im_name) elif isinstance(im_self, crefutil.NotKnown): return crefutil._InstanceMethod( im_name, im_self, im_class) else: return new.instancemethod(im_class.__dict__[im_name], im_self, im_class) else: raise TypeError("instance method changed") elif c is Instance: klass = reflect.namedObject(ao.klass) state = self.unjellyAO(ao.state) if hasattr(klass, "__setstate__"): inst = new.instance(klass, {}) self.callAfter(inst.__setstate__, state) else: inst = new.instance(klass, state) return inst elif c is Ref: o = self.unjellyAO(ao.obj) #THIS IS CHANGING THE REF OMG refkey = ao.refnum ref = self.references.get(refkey) if ref is None: self.references[refkey] = o elif isinstance(ref, crefutil.NotKnown): ref.resolveDependants(o) self.references[refkey] = o elif refkey is None: # This happens when you're unjellying from an AOT not read from source pass else: raise ValueError( "Multiple references with the same ID: %s, %s, %s!" % (ref, refkey, ao)) return o elif c is Deref: num = ao.refnum ref = self.references.get(num) if ref is None: der = crefutil._Dereference(num) self.references[num] = der return der return ref elif c is Copyreg: loadfunc = reflect.namedObject(ao.loadfunc) d = self.unjellyLater(ao.state).addCallback( lambda result, _l: apply(_l, result), loadfunc) return d #Types elif t in _SIMPLE_BUILTINS: return ao elif t is types.ListType: l = [] for x in ao: l.append(None) self.unjellyInto(l, len(l) - 1, x) return l elif t is types.TupleType: l = [] tuple_ = tuple for x in ao: l.append(None) if isinstance(self.unjellyInto(l, len(l) - 1, x), crefutil.NotKnown): tuple_ = crefutil._Tuple return tuple_(l) elif t is types.DictType: d = {} for k, v in ao.items(): kvd = crefutil._DictKeyAndValue(d) self.unjellyInto(kvd, 0, k) self.unjellyInto(kvd, 1, v) return d else: raise TypeError("Unsupported AOT type: %s" % t) del self.stack[-1]
# must be old style classes class B: pass class C: pass print type(B) print type(C) # create a class from a string (determined at runtime) and 2 objects A = new.classobj("A", (B, C), {'i': 10, 'j': 20, 'k': 30}) a1 = new.instance(A, {'x': 101, 'y': 201, 'z': 301}) a2 = new.instance(A, {'x': 102, 'y': 202, 'z': 302}) # add 3 methods to class A.f1 = new.instancemethod(lambda self: "f1()", None, A) A.f2 = new.instancemethod(lambda self: "f2()", None, A) A.f3 = new.instancemethod(lambda self: "f3()", None, A) # print info on class print "base classes of A:", A.__bases__ print "class dictionary of A:", A.__dict__ # print info on objects print "a1's class =", a1.__class__ print "a1's dictionary =", a1.__dict__ print "a2's class =", a1.__class__
def failure2Copyable(fail, unsafeTracebacks=0): f = new.instance(CopyableFailure, fail.__dict__) f.unsafeTracebacks = unsafeTracebacks return f
if not _dict.has_key('name'): # Doesn't look like a marshalled exception raise name = _dict['name'] args = _dict.get('args') # Look up the exception if not hasattr(__builtins__, name): # Unknown exception name raise # Instantiate the exception object import new _dict = {'args': args} raise new.instance(getattr(__builtins__, name), _dict), None, sys.exc_info()[2] return params[0] def __getattr__(self, name): log_debug(6, name) return _Method(self.__request, name) def __str__(self): return "<Remote-Shelf instance at %s>" % id(self) #------------------------------------------------------------------------------- # test code # pylint: disable=E0012, C0411, C0413, E1136 if __name__ == '__main__':
print m m.Eggs = Eggs sys.modules['Spam'] = m import Spam def get_more_yolks(self): return self.yolks + 3 print 'new.classobj()' C = new.classobj('Spam', (Spam.Eggs, ), {'get_more_yolks': get_more_yolks}) if verbose: print C print 'new.instance()' c = new.instance(C, {'yolks': 3}) if verbose: print c o = new.instance(C) verify(o.__dict__ == {}, "new __dict__ should be empty") del o o = new.instance(C, None) verify(o.__dict__ == {}, "new __dict__ should be empty") del o def break_yolks(self): self.yolks = self.yolks - 2 print 'new.instancemethod()'
def svg(self): self._svg = new.instance(svg.SVG) self._svg.__dict__["tag"] = "g" self._svg.__dict__["attrib"] = self.attrib self._svg.__dict__["children"] = self.children self._svg.__dict__["_svg"] = self._svg
def _doClustering(self, tbl): """Give a UniTable, get a clustering model.""" # initialize random number service randomService = random.Random() randomService.seed(self.numberSeed) # initialize clusters if self.seed == "explicit": pass # they're already initialized elif self.seed == "randomPoints" and len(tbl) >= self.numberOfClusters: index = set() while len(index) < self.numberOfClusters: index.add(randomService.randint(0, len(tbl)-1)) index = list(index) randomService.shuffle(index) self.clusters = [] for i in index: self.clusters.append([tbl[i][j] for j in range(len(self.fields))]) else: for i in xrange(len(tbl.keys())): for cluster in self.clusters: cluster[i] = randomService.gauss(tbl.field(i).mean(), tbl.field(i).std()) consumer = self._model.getModelInstance() self.__tbl = tbl iteration = 0 oldclusterdata = None oldclusters = None while True: # update clusters in scoring engine for i, clusterobj in enumerate(self._model.getChildrenOfType(pmmlCluster)): clusterobj.getChildrenOfType(pmmlArray)[0].array_values = self.clusters[i] consumer.initialize(self.consumerget, None) # compute scores from the current state clusterdata = dict([(name, []) for name in self.clusterNames]) for i in xrange(len(tbl)): self.__i = i score = consumer.score()[0][0] clusterdata[score].append(i) # assign new clusters to the means for cluster, name in zip(self.clusters, self.clusterNames): for f in range(len(cluster)): coordinates = tbl.field(f)[clusterdata[name]] if len(coordinates) > 0: cluster[f] = coordinates.mean() # determine convergence converged = (oldclusterdata == clusterdata) oldclusterdata = clusterdata # determine if the step size has become smaller than a threshold if oldclusters is None: stepsize = None else: stepsize = 0. for oldcluster, newcluster in zip(oldclusters, self.clusters): for f in range(len(oldcluster)): step = abs(oldcluster[f] - newcluster[f]) if step > stepsize: stepsize = step # COPY the clusters oldclusters = [] for cluster in self.clusters: oldclusters.append(cluster[:]) # if any conditions are satisfied, break from the loop iteration += 1 if self.halt_convergence and converged: break if self.halt_smallStep is not None and stepsize is not None and stepsize < self.halt_smallStep: break if self.halt_maxIterations is not None and iteration >= self.halt_maxIterations: break numMembers = dict([(name, len(value)) for name, value in clusterdata.items()]) # print "TODO (candidate for future metadata): number of iterations = %d" % iteration # print "TODO (candidate for future metadata): number of data points in each cluster = %s" % str(numMembers) # print "TODO (candidate for future metadata): biggest step size in the last iteration = %g" % stepsize # print "" # put the results into PMML for i, clusterobj in enumerate(self._model.getChildrenOfType(pmmlCluster)): clusterobj.getChildrenOfType(pmmlArray)[0].array_values = self.clusters[i] children = [] for child in self._model.getChildren(): if isinstance(child, pmmlCluster): oldarray = child.getChildrenOfType(pmmlArray)[0] newarray = new.instance(pmmlArray) newarray.array_values = list(oldarray.array_values) newarray.array_type = str(oldarray.array_type) newarray.array_n = len(oldarray.array_values) existing_attributes = dict([(name, value) for name, value in child.getAttributes().items() if value is not None]) newcluster = pmmlCluster(children=[newarray], attributes=existing_attributes) children.append(newcluster) else: children.append(child) return pmmlClusteringModel(children=children, attributes=self._model.getAttributes())
def bestRule(self, parent, bestClassification, producer): if self.split is None: pmmlTrue = new.instance(pmml.pmmlTrue) pmmlTrue.tag = "True" pmmlTrue.attrib = {} pmmlTrue.children = [] if len(self.true_outworlds) > 0: trueRule = new.instance(pmml.CompoundRule) trueRule.tag = "CompoundRule" trueRule.attrib = {} else: trueRule = new.instance(pmml.SimpleRule) trueRule.tag = "SimpleRule" trueRule.attrib = {"score": bestClassification} trueRule.children = [pmmlTrue] trueRule.test = pmmlTrue.createTest() parent[producer.nodeIndex] = trueRule if len(self.true_outworlds) > 0: # find the single best world using max (not sorting!) max(self.true_outworlds.values(), key=lambda x: x.split.gainCache).bestRule( trueRule, bestClassification, producer) else: if len(self.true_outworlds) > 0 and len(self.false_outworlds) > 0: trueRule = new.instance(pmml.CompoundRule) trueRule.tag = "CompoundRule" trueRule.attrib = {} falseRule = new.instance(pmml.CompoundRule) falseRule.tag = "CompoundRule" falseRule.attrib = {} else: trueRule = new.instance(pmml.SimpleRule) trueRule.tag = "SimpleRule" trueRule.attrib = {"score": self.split.score(True)} falseRule = new.instance(pmml.SimpleRule) falseRule.tag = "SimpleRule" falseRule.attrib = {"score": self.split.score(False)} trueRule.children = [self.split.trueSimplePredicate()] trueRule.test = trueRule.children[0].createTest() falseRule.children = [self.split.falseSimplePredicate()] falseRule.test = falseRule.children[0].createTest() parent.children.append(trueRule) parent.children.append(falseRule) if len(self.true_outworlds) > 0 and len(self.false_outworlds) > 0: # find the single best world using max (not sorting!) max(self.true_outworlds.values(), key=lambda x: x.split.gainCache).bestRule( trueRule, bestClassification, producer) max(self.false_outworlds.values(), key=lambda x: x.split.gainCache).bestRule( falseRule, bestClassification, producer)
def createLogOutput(self, type, data, outputDirectory): inst = instance(self.outputMap[type]) inst.__init__(data, outputDirectory) return inst
def __request(self, methodname, params): # pylint: disable=R0915 log_debug(6, methodname, params) # Init the socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: sock.connect(self.serverAddr) except socket.error as e: sock.close() methodname = None log_error("Error connecting to the auth cache: %s" % str(e)) Traceback("Shelf.__request", extra=""" Error connecting to the the authentication cache daemon. Make sure it is started on %s""" % str(self.serverAddr)) # FIXME: PROBLEM: this rhnFault will never reach the client raise_with_tb( rhnFault( 1000, _("Spacewalk Proxy error (issues connecting to auth cache). " "Please contact your system administrator")), sys.exc_info()[2]) wfile = sock.makefile("w") try: send(wfile, methodname, None, *params) except CommunicationError: wfile.close() sock.close() Traceback("Shelf.__request", extra="Encountered a CommunicationError") raise except socket.error: wfile.close() sock.close() log_error("Error communicating to the auth cache: %s" % str(e)) Traceback("Shelf.__request", extra="""\ Error sending to the authentication cache daemon. Make sure the authentication cache daemon is started""") # FIXME: PROBLEM: this rhnFault will never reach the client raise_with_tb( rhnFault( 1000, _("Spacewalk Proxy error (issues connecting to auth cache). " "Please contact your system administrator")), sys.exc_info()[2]) wfile.close() rfile = sock.makefile("r") try: params, methodname = recv(rfile) except CommunicationError as e: log_error(e.faultString) rfile.close() sock.close() log_error("Error communicating to the auth cache: %s" % str(e)) Traceback("Shelf.__request", extra="""\ Error receiving from the authentication cache daemon. Make sure the authentication cache daemon is started""") # FIXME: PROBLEM: this rhnFault will never reach the client raise_with_tb( rhnFault( 1000, _("Spacewalk Proxy error (issues communicating to auth cache). " "Please contact your system administrator")), sys.exc_info()[2]) except Fault as e: rfile.close() sock.close() # If e.faultCode is 0, it's another exception if e.faultCode != 0: # Treat is as a regular xmlrpc fault raise _dict = e.faultString if not isinstance(_dict, type({})): # Not the expected type raise if 'name' not in _dict: # Doesn't look like a marshalled exception raise name = _dict['name'] args = _dict.get('args') # Look up the exception if not hasattr(__builtins__, name): # Unknown exception name raise # Instantiate the exception object import new _dict = {'args': args} # pylint: disable=bad-option-value,nonstandard-exception raise_with_tb(new.instance(getattr(__builtins__, name), _dict), sys.exc_info()[2]) return params[0]
def newobject(cls,init,*args): tmp = new.instance(cls) init(tmp,*args) return tmp