def test_errors(self): class MyMeta(type): pass with self.assertRaises(TypeError): class MyClass(metaclass=MyMeta, otherarg=1): pass with self.assertRaises(TypeError): types.new_class("MyClass", (object,), dict(metaclass=MyMeta, otherarg=1)) types.prepare_class("MyClass", (object,), dict(metaclass=MyMeta, otherarg=1)) class MyMeta(type): def __init__(self, name, bases, namespace, otherarg): super().__init__(name, bases, namespace) with self.assertRaises(TypeError): class MyClass(metaclass=MyMeta, otherarg=1): pass class MyMeta(type): def __new__(cls, name, bases, namespace, otherarg): return super().__new__(cls, name, bases, namespace) def __init__(self, name, bases, namespace, otherarg): super().__init__(name, bases, namespace) self.otherarg = otherarg class MyClass(metaclass=MyMeta, otherarg=1): pass self.assertEqual(MyClass.otherarg, 1)
def _copy_module(module, include=None, exclude=None): copied = types.ModuleType(module.__name__) copied.__package__ = getattr(module, "__package__", None) for name, value in module.__dict__.items(): if (name.startswith("_") or (exclude is not None and name in exclude) or (include is not None and name not in include)): continue type_ = type(value) if value is None or type_ in (bool, bytes, float, int, str): setattr(copied, name, value) elif type_ in (types.FunctionType, types.LambdaType, types.BuiltinFunctionType): def func_proxy(func): """Return a proxy for the given function.""" # pylint: disable=unnecessary-lambda return lambda *args, **kwargs: func(*args, **kwargs) setattr(copied, name, func_proxy(value)) elif type_ is type and value is not type: try: proxy = types.new_class(name, bases=(value,)) proxy.mro = lambda: [] setattr(copied, name, types.new_class(name, bases=(proxy,))) except TypeError: pass return copied
def test_both(test_class, **kwargs): frozen_tests = types.new_class('Frozen_'+test_class.__name__, (test_class, unittest.TestCase)) source_tests = types.new_class('Source_'+test_class.__name__, (test_class, unittest.TestCase)) frozen_tests.__module__ = source_tests.__module__ = test_class.__module__ for attr, (frozen_value, source_value) in kwargs.items(): setattr(frozen_tests, attr, frozen_value) setattr(source_tests, attr, source_value) return frozen_tests, source_tests
def extension_add_subevent(self, code, subcode, evt, name = None): """extension_add_subevent(code, evt, [name]) Add an extension subevent. CODE is the numeric code, subcode is the sub-ID of this event that shares the code ID with other sub-events and EVT is the event class. EVT will be cloned, and the attribute _code of the new event class will be set to CODE. If NAME is omitted, it will be set to the name of EVT. This name is used to insert an entry in the DictWrapper extension_event. """ if hasattr(types, 'ClassType'): newevt = types.ClassType(evt.__name__, evt.__bases__, evt.__dict__.copy()) else: newevt = types.new_class(evt.__name__, evt.__bases__, evt.__dict__.copy()) newevt._code = code self.display.add_extension_event(code, newevt, subcode) if name is None: name = evt.__name__ # store subcodes as a tuple of (event code, subcode) in the # extension dict maintained in the display object setattr(self.extension_event, name, (code,subcode))
def extension_add_event(self, code, evt, name = None): """extension_add_event(code, evt, [name]) Add an extension event. CODE is the numeric code, and EVT is the event class. EVT will be cloned, and the attribute _code of the new event class will be set to CODE. If NAME is omitted, it will be set to the name of EVT. This name is used to insert an entry in the DictWrapper extension_event. """ if hasattr(types, 'ClassType'): newevt = types.ClassType(evt.__name__, evt.__bases__, evt.__dict__.copy()) else: newevt = types.new_class(evt.__name__, evt.__bases__, evt.__dict__.copy()) newevt._code = code self.display.add_extension_event(code, newevt) if name is None: name = evt.__name__ setattr(self.extension_event, name, code)
def data_entry_form(self, row_name='new', data_instance = None, depth = 2): self.form_class = None self.classattr = {} if data_instance is None: data_instance = DataInstance(self.table_name, row_name, depth) if row_name != 'new': data_instance.get_linked_instances() self.instance = data_instance self.get_table(data_instance) self.classattr['form_data_type_0'] = HiddenField('form_data_type_0', default=self.form_type) self.classattr['form_data_table_0'] = HiddenField('form_data_table_0', default=self.table_name) self.classattr['form_data_row_name_0'] = HiddenField('form_data_row_name_0', default=row_name) self.classattr['row_counter'] = HiddenField('row_counter', default=data_instance.instance_counter) form_class = new_class('SingleForm', (Form,), {}, lambda ns: ns.update(self.classattr)) logging.info(self.classattr) logging.info(self.classattr) self.form_class = form_class(None)
def test_metaclass_override_function(self): # Special case: the given metaclass isn't a class, # so there is no metaclass calculation. class A(metaclass=self.Meta): pass marker = object() def func(*args, **kwargs): return marker X = types.new_class("X", (), {"metaclass": func}) Y = types.new_class("Y", (object,), {"metaclass": func}) Z = types.new_class("Z", (A,), {"metaclass": func}) self.assertIs(marker, X) self.assertIs(marker, Y) self.assertIs(marker, Z)
def make_abc_subclasses(base_class): classes = [] for kind, abc in [('Frozen', frozen_abc), ('Source', source_abc)]: name = '_'.join([kind, base_class.__name__]) base_classes = base_class, getattr(abc, base_class.__name__) classes.append(types.new_class(name, base_classes)) return classes
def test_new_class_metaclass_keywords(self): #Test that keywords are passed to the metaclass: def meta_func(name, bases, ns, **kw): return name, bases, ns, kw res = types.new_class("X", (int, object), dict(metaclass=meta_func, x=0)) self.assertEqual(res, ("X", (int, object), {}, {"x": 0}))
def ensure_tomes(self): for number in range(1, 29): class_id = 'Tome_{0:02d}'.format(number) self.tomes[number] = types.new_class( correct_owl_id(class_id), (Tome, ), kwds = { "ontology" : onto } )
def test_prepare_class(self): # Basic test of metaclass derivation expected_ns = {} class A(type): def __new__(*args, **kwargs): return type.__new__(*args, **kwargs) def __prepare__(*args): return expected_ns B = types.new_class("B", (object,)) C = types.new_class("C", (object,), {"metaclass": A}) # The most derived metaclass of D is A rather than type. meta, ns, kwds = types.prepare_class("D", (B, C), {"metaclass": type}) self.assertIs(meta, A) self.assertIs(ns, expected_ns) self.assertEqual(len(kwds), 0)
def test_new_class_meta(self): Meta = self.Meta settings = {"metaclass": Meta, "z": 2} # We do this twice to make sure the passed in dict isn't mutated for i in range(2): C = types.new_class("C" + str(i), (), settings) self.assertIsInstance(C, Meta) self.assertEqual(C.y, 1) self.assertEqual(C.z, 2)
def decorator(cls): assert isinstance(driver, Driver) assert issubclass(cls, Schema) assert not issubclass(cls, DrivedMixin) drived_class = new_class(cls.__name__, (cls, DrivedMixin)) drived_class.driver = driver return drived_class
def test_new_class_exec_body(self): Meta = self.Meta def func(ns): ns["x"] = 0 C = types.new_class("C", (), {"metaclass": Meta, "z": 2}, func) self.assertIsInstance(C, Meta) self.assertEqual(C.x, 0) self.assertEqual(C.y, 1) self.assertEqual(C.z, 2)
def make_return_value_tests(base_class, test_class): frozen_class, source_class = make_abc_subclasses(base_class) tests = [] for prefix, class_in_test in [('Frozen', frozen_class), ('Source', source_class)]: def set_ns(ns): ns['ins'] = class_in_test() tests.append(types.new_class('_'.join([prefix, test_class.__name__]), (test_class, unittest.TestCase), exec_body=set_ns)) return tests
def test_tests_fail_1(self): SimpleTestCase = types.new_class('SimpleTestCase', (BaseSimpleTestCase, tb.TestCase)) suite = unittest.TestSuite() suite.addTest(SimpleTestCase('test_tests_zero_error')) result = unittest.TestResult() suite.run(result) self.assertIn('ZeroDivisionError', result.errors[0][1])
def subclass(cls, name, bases, dict={}): if not isinstance(bases, tuple): bases = (bases,) name = name.encode('utf-8') key = '.'.join((name, str(bases))) subclass = cls.cache.get(key) if subclass is None: subclass = types.new_class(name, bases, dict) #subclass = classobj(name, bases, dict) cls.cache[key] = subclass return subclass
def parametrize(TestCase): '''Parametrize a :class:`unittest.TestCase`. >>> @parametrize ... class TestSomething(unittest.TestCase): ... def test_equality(self): ... self.assertEqual(self.x, self.y) >>> TestSomething(x=1, y=1) >>> TestSomething(x=2, y=2) ''' return types.new_class(TestCase.__name__, (), dict(metaclass=_ParametrizedCollection, base=TestCase))
def Event(name): """ A mutator for events. Still unproven if it works, but *should* dynamically make classes for each unique event. This way, debugging is easier, since we can tell which event is which. """ c = new_class(name, bases=(_Event,))(name) return c
def _new_nonterm(clsname, clsdict={}, clskwds={}, clsbases=(Nonterm,)): mod = sys.modules[__name__] def clsexec(ns): ns['__module__'] = __name__ for k, v in clsdict.items(): ns[k] = v return ns cls = types.new_class(clsname, clsbases, clskwds, clsexec) setattr(mod, clsname, cls) return cls
def create_inheritance_tests(base_class): def set_frozen(ns): ns['abc'] = frozen_abc def set_source(ns): ns['abc'] = source_abc classes = [] for prefix, ns_set in [('Frozen', set_frozen), ('Source', set_source)]: classes.append(types.new_class('_'.join([prefix, base_class.__name__]), (base_class, unittest.TestCase), exec_body=ns_set)) return classes
def test_new_class_meta_with_base(self): Meta = self.Meta def func(ns): ns["x"] = 0 C = types.new_class(name="C", bases=(int,), kwds=dict(metaclass=Meta, z=2), exec_body=func) self.assertTrue(issubclass(C, int)) self.assertIsInstance(C, Meta) self.assertEqual(C.x, 0) self.assertEqual(C.y, 1) self.assertEqual(C.z, 2)
def select(self, cls: type): """ d3.select() method. Args: cls: class of elements to be selected Returns: a object of the selected class, with the new expression in it """ exp = self.method('select', cls.__name__.lower()) selection_class = types.new_class(cls.__name__ + 'Selection', bases=(cls, Selection)) return selection_class(exp)
def __init__(self, errorCode): name = 'SVNERR_' + self.name bases = (SVNError,) cls_dict = {} me = self # Methods def __init__(self, message, returncode, cmd): super(me.errcls, self).__init__(message, returncode, cmd) self.svn_errcode = me cls_dict['__init__'] = __init__ self.errcls = types.new_class(name, bases, {}, lambda ns: ns.update(cls_dict))
def _gen_keyword_tokens(): # Define keyword tokens mod = sys.modules[__name__] def clsexec(ns): ns['__module__'] = __name__ return ns for val, (token, typ) in keywords.graphql_keywords.items(): clsname = f'T_{token}' clskwds = dict(metaclass=parsing.TokenMeta, token=token) cls = types.new_class(clsname, (Token,), clskwds, clsexec) setattr(mod, clsname, cls)
def make_dataclass(cls_name, fields, *, bases=(), namespace=None, init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False): """Return a new dynamically created dataclass. The dataclass name will be 'cls_name'. 'fields' is an iterable of either (name), (name, type) or (name, type, Field) objects. If type is omitted, use the string 'typing.Any'. Field objects are created by the equivalent of calling 'field(name, type [, Field-info])'. C = make_dataclass('C', ['x', ('y', int), ('z', int, field(init=False))], bases=(Base,)) is equivalent to: @dataclass class C(Base): x: 'typing.Any' y: int z: int = field(init=False) For the bases and namespace parameters, see the builtin type() function. The parameters init, repr, eq, order, unsafe_hash, and frozen are passed to dataclass(). """ if namespace is None: namespace = {} else: # Copy namespace since we're going to mutate it. namespace = namespace.copy() anns = {} for item in fields: if isinstance(item, str): name = item tp = 'typing.Any' elif len(item) == 2: name, tp, = item elif len(item) == 3: name, tp, spec = item namespace[name] = spec anns[name] = tp namespace['__annotations__'] = anns # We use `types.new_class()` instead of simply `type()` to allow dynamic creation # of generic dataclassses. cls = types.new_class(cls_name, bases, {}, lambda ns: ns.update(namespace)) return dataclass(cls, init=init, repr=repr, eq=eq, order=order, unsafe_hash=unsafe_hash, frozen=frozen)
def nef_class(cls): '''This function is a decorator that is used to add generated special methods to classes, as described below. The `nef_class()` decorator examines the class to find __annotations__. A field is defined as class variable that has a type annotation. The order of the __annotations__ in all of the generated methods is the order in which they appear in the class definition. Be default the instance decorated by `nef_class` is attributed frozen, which means we prefer it to be immutable. Some basic arithmetic operators are mounted and would apply on `.data` field. ''' base = attr.s(frozen=True, auto_attribs=True, slots=True)(cls) cls_ = types.new_class(base.__name__, (base, NefClass)) return cls_
def userConsent(self, Dx, Uy, Tz, Rw, className, retroactive=True): createConsent = None if retroactive: #is subsumed by (T and U) or access(D) createdConsent = types.new_class(className, (self.onto.rConsent, ), {}) createdConsent.is_a = [(self.getClass(Tz) & self.getClass(Uy)) | self.onto.access.some(self.getClass(Dx))] createdConsent.is_a.append(self.onto.rConsent) else: #non-retroactive Consent: subsumed by (T and U) or access(D and T) createdConsent = types.new_class(className, (self.onto.nrConsent, ), {}) createdConsent.is_a = [ (self.getClass(Tz) & self.getClass(Uy)) | self.onto.access.some(self.getClass(Dx) & self.getClass(Tz)) ] createdConsent.is_a.append(self.onto.nrConsent) #is equivalent to (T and U) createdConsent.equivalent_to = [self.getClass(Tz) & self.getClass(Uy)] return createConsent
def _find_class_for(cls, element_name=None, class_name=None, create_missing=True): """Look in the parent modules for classes matching the element name. One or both of element/class name must be specified. Args: element_name: The name of the element type. class_name: The class name of the element type. create_missing: Whether classes should be auto-created if no existing match is found. Returns: A Resource class. """ if not element_name and not class_name: raise Error('One of element_name,class_name must be specified.') elif not element_name: element_name = util.underscore(class_name) elif not class_name: class_name = util.camelize(element_name) module_path = cls.__module__.split('.') for depth in range(len(module_path), 0, -1): try: __import__('.'.join(module_path[:depth])) module = sys.modules['.'.join(module_path[:depth])] except ImportError: continue try: klass = getattr(module, class_name) return klass except AttributeError: try: __import__('.'.join([module.__name__, element_name])) submodule = sys.modules['.'.join([module.__name__, element_name])] except ImportError: continue try: klass = getattr(submodule, class_name) return klass except AttributeError: continue # If we made it this far, no such class was found if create_missing: return types.new_class(class_name, (cls,), exec_body=lambda ns: ns.update({'__module__': cls.__module__}) or ns)
def builder(namespace): """ Fills the namespace of the parent class with class members that are classes. Each class member has the name of a property, and each class has up to two static methods, a Get method if the property is readable and a Set method if the property is writable. For example, given the spec: <property name="Version" type="s" access="read"> <annotation name="org.freedesktop.DBus.Property.EmitsChangedSignal" value="const"/> </property> A class called "Version" with a single method "Get" will be added to the namespace. :param namespace: the class's namespace """ for prop in properties: try: name = prop.attrib["name"] except KeyError as err: # pragma: no cover fmt_str = ("No name attribute found for property belonging to " 'interface "%s"') raise DPClientGenerationError(fmt_str % interface_name) from err try: access = prop.attrib["access"] except KeyError as err: # pragma: no cover fmt_str = ('No access attribute found for property "%s" ' 'belonging to interface "%s"') raise DPClientGenerationError(fmt_str % (name, interface_name)) from err try: signature = prop.attrib["type"] except KeyError as err: # pragma: no cover fmt_str = ('No type attribute found for property "%s" ' 'belonging to interface "%s"') raise DPClientGenerationError(fmt_str % (name, interface_name)) from err namespace[name] = types.new_class(name, bases=(object, ), exec_body=build_property( access, name, signature))
def __new__(mcs, name, bases, namespace): fieldmethods = {} generic_types = {} for attr_name, obj in list(namespace.items()): if getattr(obj, "__isfieldmethod__", False): # TODO: Ensure callable as cls.somefieldmethod(field). namespace.pop(attr_name) fieldmethods[attr_name] = obj elif isinstance(obj, TypeVar): generic_types[attr_name] = obj namespace = dict(namespace) annotations = namespace.pop("__annotations__", {}) field_base_cls = types.new_class("_FieldBase", (_FieldBase, )) field_base_cls.__module__ = namespace["__module__"] fields = {} # Make the ADT base class. def __new__(cls, *args, **kwargs): raise TypeError(f"Cannot instantiate ADT class {cls.__name__!r}") namespace.update({ "__new__": __new__, "_fields": fields, "_generic_types": generic_types, "_FieldBase": field_base_cls, }) cls = super().__new__(mcs, name, bases, namespace) if generic_types: cls.__qualname__ += "[{}]".format(",".join( t.__name__ for t in generic_types.values())) field_base_cls.__adtbase__ = cls field_base_cls.__qualname__ = cls.__qualname__ + "." + field_base_cls.__name__ # Make the field classes based on the ADT class annotations. for field_name, arg_types in annotations.items(): if type(arg_types) is not tuple: raise TypeError( f"{field_name!r} is a badly declared field - should use a tuple of types" ) f = _make_field(field_name, field_base_cls, arg_types) f.__qualname__ = cls.__qualname__ + "." + f.__name__ for method_name, method in fieldmethods.items(): setattr(f, method_name, _fieldmethod(method, cls, f)) fields[field_name] = f setattr(cls, field_name, f) return cls
def as_names(cls: EnumCls, aliaser: Callable[[str], str] = lambda s: s) -> EnumCls: # Enum requires to call namespace __setitem__ def exec_body(namespace: dict): for elt in cls: # type: ignore namespace[elt.name] = aliaser(elt.name) if not issubclass(cls, Enum): raise TypeError("as_names must be called with Enum subclass") name_cls = type_name(None)( new_class(cls.__name__, (str, Enum), exec_body=exec_body) ) deserializer(Conversion(partial(getattr, cls), source=name_cls, target=cls)) serializer( Conversion(lambda obj: getattr(name_cls, obj.name), source=cls, target=name_cls) ) return cls
def graphene_input_object_from_model_form(cls_name, model_form): import types import graphene from graphene_django.form_converter import convert_form_field form_instance = model_form() cls_dict = { fname: convert_form_field(form_instance.fields[fname]) for fname in form_instance.fields } new_cls = types.new_class( cls_name, (graphene.InputObjectType,), {}, lambda ns: ns.update(cls_dict) ) import sys new_cls.__module__ = sys._getframe(1).f_globals['__name__'] return new_cls
def __call__(self, func: types.FunctionType): args, _, _, defaults, _, _, annotations = inspect.getfullargspec(func) return dataclass(types.new_class( func.__name__, bases=(Query, ), kwds={'rtype': self.rtype}, exec_body=methodcaller( 'update', { '__annotations__': annotations, '__doc__': func.__doc__, '__module__': func.__module__, '__req__': property(compose(partial(apply, func), astuple)), **dict(zip(reversed(args), reversed(defaults or ()))) })), frozen=True)
def named_tuple(classname, fieldnames): #Populate a dictionary of field property accessors cls_dict = {name: property(operator.itemgetter(n))} #Make a __new__ function and add to the class dict def __new__(cls, *args): if len(args) != len(fieldnames) raise TypeError('Expected {} arguments'.format(len(fieldnames))) return tuple.__new__(cls, args) cls_dict['__new__'] = __new__ #Make the class cls = types.new_class(classname, (tuple,), {}, lambda ns: ns.update(cls_dict)) cls.__module__ = sys._getframe(1).f_globals['__name__'] return cls
def generate_classes(rooturl, user, pwd): """ Generates classes for from rest api given structs. After the classes are generated they can be imported like normal classes :param rooturl: URL to rest api :param user: Username for the Rest API :param pwd: Password of the Rest API :return: A List with class names """ cls_names = get_provided_classes(rooturl, user, pwd) for name in cls_names: meta = get_class_meta(rooturl, name, user, pwd) cls = new_class(name, (RestBase, )) setattr(cls, 'meta', meta) globals()[name] = cls return sorted(cls_names)
def subclass_CidSubObj(sub_line_type): """Produce a `CidSubObj` based subclass.""" # see if already exists cls_name = SUB_OBJ_CLASS_DICT[sub_line_type] try: return CidSubObj.getsubcls(cls_name) except ChildRegistryError: pass # resolve 2 of the CidSubObj generic types SubLine = sub_line_type # type of CidLine indicating start of an object in CID file cls = new_class(cls_name, (CidSubObj[CidObj, CidSeq, SubLine], Generic[CidObj, CidSeq])) return cls
def interface_as_type(typ, module): # type: (Interface, str) -> type # we don't add the fields yet -- these types may not exist yet. return new_class( str(typ.name), (types.Namespace, ), kwds={"metaclass": types.Interface}, exec_body=methodcaller( "update", { "__doc__": typ.desc, "__raw__": typ, "__module__": module }, ), )
def patch_config_as_nothrow(instance): if "NoThrow" in [instance.__name__, instance.__class__.__name__]: return instance if type(instance) == type: instance = types.new_class(instance.__name__ + "NoThrow", (instance, ), dict(metaclass=NoThrowMeta)) for (k, v) in inspect.getmembers(instance): if not k.startswith("__") and type(v) == type: type.__setattr__(instance, k, patch_config_as_nothrow(v)) else: for (k, v) in inspect.getmembers(instance.__class__): if not k.startswith("__") and type(v) == type: type.__setattr__(instance.__class__, k, patch_config_as_nothrow(v)) instance.__class__ = type(instance.__class__.__name__ + "NoThrow", (instance.__class__, NoThrowBase), {}) return instance
def _make_skeleton_class(type_constructor, name, bases, type_kwargs, class_tracker_id, extra): """Build dynamic class with an empty __dict__ to be filled once memoized If class_tracker_id is not None, try to lookup an existing class definition matching that id. If none is found, track a newly reconstructed class definition under that id so that other instances stemming from the same class id will also reuse this class definition. The "extra" variable is meant to be a dict (or None) that can be used for forward compatibility shall the need arise. """ skeleton_class = types.new_class(name, bases, {'metaclass': type_constructor}, lambda ns: ns.update(type_kwargs)) return _lookup_class_or_track(class_tracker_id, skeleton_class)
def populate_domains(self): """ Generate domain classes (ex: self.Page) and methods (ex: self.Page.enable) """ for domain in DOMAINS: # Create a new class for each domain with the correct name domain_class = types.new_class(domain["domain"], (DomainProxy, )) new_instance = domain_class(self) for command in domain.get("commands", []): # Create a new method for each domain command method_sig = create_signature(command.get("parameters", [])) new_fn = wrap_factory(command["name"], method_sig) new_method = types.MethodType(new_fn, new_instance) setattr(new_instance, command["name"], new_method) setattr(self, domain["domain"], new_instance)
def _new_nonterm(self, clsname, clsdict={}, clskwds={}, clsbases=(Nonterm, )): mod = sys.modules[self.name] def clsexec(ns): ns['__module__'] = self.name for k, v in clsdict.items(): ns[k] = v return ns cls = types.new_class(clsname, clsbases, clskwds, clsexec) setattr(mod, clsname, cls) return cls
def as_standard_format(self, include_thing=True): propClass = types.new_class(self.uri, (MrDataProperty, )) propClass.uri = "mrdata_prop:" + propClass.__name__.split(":")[0] prop = propClass() prop.uri = self.uri if self.thing: prop.uri = self.thing.uri.split(":")[-1] + ":" + \ prop.uri.split(":")[-1] if include_thing: prop._source = self.thing.as_standard_format() if self.value: prop._target = self.value.as_standard_format() for p in self.properties: prop.add_property(p.as_standard_format(False)) #print(prop.__dict__) return prop
def rounder_mixin(conv: Conv, types: TypeArg = None, names: Any = None) -> Type[Real]: """Mixin class for rounding/modular routines. Defines the operators `%`, `//`, and the functions `divmod`, `round`, `math.floor,ceil,trunc`. Parameters ---------- conv : Callable Function used to convert a tuple of inputs. types : Type or Tuple[Type] or None, optional The types of output that are converted. By default `None -> Number`. names : Any or Tuple[Any, ...], optional Name spaces `(opspace, namespace, mathspace)`. A single namespace is expanded to 3. By default `None -> (operator, builtins, math)`. opspace : default - operator namespace with function attributes: {`floordiv`, `mod`}. defspace : default - builtins namespace with function attributes: {`divmod`, `round`}. mathspace : default - math namespace with function attributes: {`trunc`, `floor`, `ceil`}. Notes ----- You must call `set_objclasses(class)` after defining the `class`. The `__objclass__` attribute is needed to convert the outputs back. """ opsp, dfsp, masp = tuplify(default(names, (operator, builtins, math)), 3) method = one_method_wrapper(conv, types) ops = opr_methods_wrapper(conv, types) def exec_body(nsp: dict) -> None: """Mixin class for rounding/modular routines. """ nsp['__floordiv__'], nsp['__rfloordiv__'] = ops(opsp.floordiv) nsp['__mod__'], nsp['__rmod__'] = ops(opsp.mod) nsp['__divmod__'], nsp['__rdivmod__'] = ops(dfsp.divmod) nsp['__round__'] = method(dfsp.round) nsp['__trunc__'] = method(masp.trunc) nsp['__floor__'] = method(masp.floor) nsp['__ceil__'] = method(masp.ceil) return new_class('RoundableMixin', exec_body=exec_body)
def named_tuple(classname, fieldnames): cls_dict = { name: property(operator.itemgetter(n)) for n, name in enumerate(fieldnames) } def __new__(cls, *args): if len(args) != len(fieldnames): raise TypeError('Expected {} arguments'.format(len(fieldnames))) return tuple.__new__(cls, args) cls_dict['__new__'] = __new__ cls = types.new_class(classname, (tuple, ), {}, lambda ns: ns.update(cls_dict)) cls.__module__ = sys._getframe(1).f_globals['__name__'] return cls
def __rshift__(self, other): from . import datatype if isinstance(self, parametrized): bases = (Generic[self.parameters], ) else: bases = tuple() cls = new_class( self.name, bases, kwds=dict(metaclass=iter_constructors), exec_body=lambda ns: ns.update(__annotations__=other. to_annotations()), ) return datatype(cls)
def create_class(name, attr_defs): """ Return class from *name* and *attr_defs* dictionary. Do the same thing as instruction: class X(metaclass=Class): a1 = Att(int, 1) a2 = float with *name* = 'X' and *attr_defs* = {'a1': Att(int, 1), 'a2': Att(float)} see Class and Att definitions. """ meta = {'metaclass': Class} def update(clsdic): for att, val in attr_defs.items(): clsdic[att] = val return types.new_class(name, kwds=meta, exec_body=update)
def __call__(*args, **params): assert 1 <= len(args) <= 2 cls = args[0] if len(args) == 1 and not params: loader = unittest.defaultTestLoader ts = unittest.TestSuite() for test_case in sorted(cls.__test_cases, key=operator.attrgetter('__name__')): ts.addTest(loader.loadTestsFromTestCase(test_case)) return ts name = args[1] if len(args) == 2 else None if name is None: name = ','.join('{}={}'.format(k, v) for k, v in sorted(params.items())) name = name.replace('%', '%{}'.format(ord('%'))).replace( '.', '%{}'.format(ord('.'))) assert '.' not in name assert not hasattr(cls, name), 'duplicate test name' def setUp(self): for k, v in params.items(): setattr(self, k, v) return cls.__base.setUp(self) def populate(ns): for k, v in vars(cls.__base).items(): enable_if = getattr(v, '_parametrize_enable_if', None) if enable_if and not enable_if(**params): ns[k] = None ns.update(setUp=setUp, __qualname__=cls.__qualname__ + ':' + name, __module__=cls.__module__, __doc__=cls.__doc__) return ns TestCase = types.new_class(name, (cls.__base, ), exec_body=populate) cls.__test_cases.append(TestCase) # Add `TestCase` as `name` to this collection. setattr(cls, name, TestCase) # Trick `unittest.loader.TestLoader.loadTestsFromModule` into finding # this test case. setattr(sys.modules[cls.__module__], cls.__qualname__ + ':' + name, TestCase)
def specialize_class(cls, kind, base=None, **kwargs): # XXX Support passing in submodule names--load (and cache) them? # That would clean up the test modules a bit more. if base is None: base = unittest.TestCase elif not isinstance(base, type): base = base[kind] name = '{}_{}'.format(kind, cls.__name__) bases = (cls, base) specialized = types.new_class(name, bases) specialized.__module__ = cls.__module__ specialized._NAME = cls.__name__ specialized._KIND = kind for attr, values in kwargs.items(): value = values[kind] setattr(specialized, attr, value) return specialized
def create_deferred_base_class(name, fields={}, meta={}, polymorphic=False): metaclass = deferred.ForeignKeyBuilder model_class = models.Model if polymorphic: metaclass = deferred.PolymorphicForeignKeyBuilder model_class = PolymorphicModel meta.setdefault('app_label', 'foo') meta.setdefault('abstract', True) Meta = type(str('Meta'), (), meta) cls_dict = dict(Meta=Meta, __metaclass__=metaclass, __module__=__name__, **fields) return new_class(name, (model_class, ), {'metaclass': metaclass}, lambda attrs: attrs.update(cls_dict))
def object_deserialization( func: Callable[..., T], *input_class_modifiers: Callable[[type], Any], parameters_metadata: Mapping[str, Mapping] = None, ) -> Any: fields = parameters_as_fields(func, parameters_metadata) types = get_type_hints(func, include_extras=True) if "return" not in types: raise TypeError("Object deserialization must be typed") return_type = types["return"] bases = () if getattr(return_type, "__parameters__", ()): bases = (Generic[return_type.__parameters__], ) # type: ignore elif func.__name__ != "<lambda>": input_class_modifiers = ( type_name(to_pascal_case(func.__name__)), *input_class_modifiers, ) def __init__(self, **kwargs): self.kwargs = kwargs input_cls = new_class( to_pascal_case(func.__name__), bases, exec_body=lambda ns: ns.update({"__init__": __init__}), ) for modifier in input_class_modifiers: modifier(input_cls) set_object_fields(input_cls, fields) if any(f.additional_properties for f in fields): kwargs_param = next(f.name for f in fields if f.additional_properties) def wrapper(input): kwargs = input.kwargs.copy() kwargs.update(kwargs.pop(kwargs_param)) return func(**kwargs) else: def wrapper(input): return func(**input.kwargs) wrapper.__annotations__["input"] = input_cls wrapper.__annotations__["return"] = return_type return wrapper
def create_card(data, name, card_type, cls_dict_others=None): """Internal function to create a card, called by other creators.""" assert 'id' in data, 'Data must contain value of key "id".' if name is None: if 'name' in data: name = data['name'] else: name = '{}_{}'.format(card_type.__name__, data['id']) cls_dict = {'data': data} if cls_dict_others is not None: cls_dict.update(cls_dict_others) return new_class(name, (card_type, ), {}, lambda ns: ns.update(cls_dict))
def define(_doc=None, **funcs): ''' Define protocol. :return protocol object with functions defined :param _doc: protocol documentation :param funcs: map from funtion name to its documentation ''' def fill_class(ns): ns['__doc__'] = _doc ns['_protocol_functions'] = funcs ns.update({ f_name: staticmethod(_def_disp_function(f_name, f_doc)) for f_name, f_doc in funcs.items() }) return ns return types.new_class('Protocol', exec_body=fill_class)
def test_metaclass_derivation(self): # issue1294232: correct metaclass calculation new_calls = [] # to check the order of __new__ calls class AMeta(type): def __new__(mcls, name, bases, ns): new_calls.append('AMeta') return super().__new__(mcls, name, bases, ns) @classmethod def __prepare__(mcls, name, bases): return {} class BMeta(AMeta): def __new__(mcls, name, bases, ns): new_calls.append('BMeta') return super().__new__(mcls, name, bases, ns) @classmethod def __prepare__(mcls, name, bases): ns = super().__prepare__(name, bases) ns['BMeta_was_here'] = True return ns A = types.new_class("A", (), {"metaclass": AMeta}) self.assertEqual(new_calls, ['AMeta']) new_calls.clear() B = types.new_class("B", (), {"metaclass": BMeta}) # BMeta.__new__ calls AMeta.__new__ with super: self.assertEqual(new_calls, ['BMeta', 'AMeta']) new_calls.clear() C = types.new_class("C", (A, B)) # The most derived metaclass is BMeta: self.assertEqual(new_calls, ['BMeta', 'AMeta']) new_calls.clear() # BMeta.__prepare__ should've been called: self.assertIn('BMeta_was_here', C.__dict__) # The order of the bases shouldn't matter: C2 = types.new_class("C2", (B, A)) self.assertEqual(new_calls, ['BMeta', 'AMeta']) new_calls.clear() self.assertIn('BMeta_was_here', C2.__dict__) # Check correct metaclass calculation when a metaclass is declared: D = types.new_class("D", (C,), {"metaclass": type}) self.assertEqual(new_calls, ['BMeta', 'AMeta']) new_calls.clear() self.assertIn('BMeta_was_here', D.__dict__) E = types.new_class("E", (C,), {"metaclass": AMeta}) self.assertEqual(new_calls, ['BMeta', 'AMeta']) new_calls.clear() self.assertIn('BMeta_was_here', E.__dict__)
def configure(conf, spec, *, create_properties=True, converters=None, **kwargs): """For an explanation see the :doc:`intro`. :param conf: the configuration :type conf: :term:`path-like object` or :term:`text file` opened for reading or :class:`~configparser.ConfigParser` object :param spec: the specification :type spec: :term:`path-like object` or :term:`text file` opened for reading or ``None`` :param bool create_properties: if ``True`` properties will be created, else only item access with [sec,opt] can be used :param dict converters: same as the ``converters`` argument of :class:`~configparser.ConfigParser` but used directly by this function :param kwargs: arguments for the :class:`~configparser.ConfigParser` (ignored if ``conf`` is a :class:`~configparser.ConfigParser` object) :return: configuration object :rtype: Config :raises SpecError: if there is a problem with the specification :raises ConfigError: if there is a problem with the configuration :raises configparser.Error: from :class:`~configparser.ConfigParser` """ cp = ConfigParser(**kwargs) if spec is None: _read_conf(cp, conf) options = _without_spec(cp, create_properties, kwargs) else: spec_data = Spec(spec, converters if converters else {}) tmp_cp = _check_fixed_opts(conf, spec_data.fixed_opts, kwargs) cp.read_string(spec_data.defaults) _read_conf(cp, tmp_cp) options = _with_spec(cp, create_properties, spec_data, kwargs) def cls_cb(ns): ns['__module__'] = __name__ if create_properties: for key, data in options.items(): ns[data.name] = property(create_getter(key), None if data.ro else create_setter(key), create_deleter(key)) C = types.new_class('Config', (Config,), {}, cls_cb) return C(options, create_properties, kwargs)
def make_cls( name: str, base: Type[proxy_base[T]] = None, include: Optional[List[str]] = None, exclude: Optional[List[str]] = None, ) -> Type: """ Produces a new class that is the same as #proxy but does not inherit from it. Members can be specifically included and excluded with the *include*/*exclude* arguments. If *base* is not specified, it defaults to the #proxy class. """ if base is None: base = cast(Type[proxy[T]], proxy) mro = base.__mro__ if proxy_base not in base.__mro__: raise TypeError('base must be subclass of proxy_base') # We only want to copy the members starting with the #proxy_base class. members = {} for cls in reversed(mro[:mro.index(proxy_base) + 1]): members.update(cls.__dict__) filtered_members = {} for key, value in members.items(): take = False if include is not None and key in include: take = True elif include is None and (key.startswith('__') and key.endswith('__') or key == '_get_current_object'): take = True if exclude is not None and key in exclude: take = False if key in ['__name__', '__weakref__', '__module__', '__doc__'] \ and (not include or key not in include): take = False if take: filtered_members[key] = value return types.new_class( name, (Generic[T], ), exec_body=lambda ns: ns.update(filtered_members)) # type: ignore
def auto_class_factory(name, model_mapping, checkpoint_for_example="bert-base-cased", head_doc=""): # Create a new class with the right name from the base class new_class = types.new_class(name, (_BaseAutoModelClass, )) new_class._model_mapping = model_mapping class_docstring = insert_head_doc(CLASS_DOCSTRING, head_doc=head_doc) new_class.__doc__ = class_docstring.replace("BaseAutoModelClass", name) # Now we need to copy and re-register `from_config` and `from_pretrained` as class methods otherwise we can't # have a specific docstrings for them. from_config = copy_func(_BaseAutoModelClass.from_config) from_config_docstring = insert_head_doc(FROM_CONFIG_DOCSTRING, head_doc=head_doc) from_config_docstring = from_config_docstring.replace( "BaseAutoModelClass", name) from_config_docstring = from_config_docstring.replace( "checkpoint_placeholder", checkpoint_for_example) from_config.__doc__ = from_config_docstring from_config = replace_list_option_in_docstrings( model_mapping, use_model_types=False)(from_config) new_class.from_config = classmethod(from_config) if name.startswith("TF"): from_pretrained_docstring = FROM_PRETRAINED_TF_DOCSTRING elif name.startswith("Flax"): from_pretrained_docstring = FROM_PRETRAINED_FLAX_DOCSTRING else: from_pretrained_docstring = FROM_PRETRAINED_TORCH_DOCSTRING from_pretrained = copy_func(_BaseAutoModelClass.from_pretrained) from_pretrained_docstring = insert_head_doc(from_pretrained_docstring, head_doc=head_doc) from_pretrained_docstring = from_pretrained_docstring.replace( "BaseAutoModelClass", name) from_pretrained_docstring = from_pretrained_docstring.replace( "checkpoint_placeholder", checkpoint_for_example) shortcut = checkpoint_for_example.split("/")[-1].split("-")[0] from_pretrained_docstring = from_pretrained_docstring.replace( "shortcut_placeholder", shortcut) from_pretrained.__doc__ = from_pretrained_docstring from_pretrained = replace_list_option_in_docstrings(model_mapping)( from_pretrained) new_class.from_pretrained = classmethod(from_pretrained) return new_class
def mathops_mixin(conv: Conv, types: TypeArg = None, names: Any = None) -> Type[Complex]: """Mixin class to mimic arithmetic number types. Defines the arithmetic operators `+`, `-`, `*`, `/`, `**`, `==`, `!=` and the functions `pow`, `abs`. Operators `//`, `%` are in `rounder_mixin`, operators `<`, `<=`, `>`, `>=` are in `ordered_mixin` and `<<`, `>>`, `&`, `^`, `|`, `~` are in `bit_twiddle_mixin`. Parameters ---------- conv : Callable Function used to convert a tuple of inputs. types : Type or Tuple[Type] or None, optional The types of output that will be converted. By default `None -> Number` names : Any, optional namespace with function attributes: {'eq', `ne`, `add`, `sub`, `mul`, `truediv`, `pow`, `neg`, `pos`, `abs`}. By default `None -> operator`. Notes ----- You must call `set_objclasses(class)` after defining the `class`. The `__objclass__` attribute is needed to convert the outputs back. """ names = default(names, operator) method_in = in_method_wrapper(conv) method = one_method_wrapper(conv, types) ops = opr_methods_wrapper(conv, types) def exec_body(nsp: dict) -> None: """Mixin class to mimic arithmetic number types. """ nsp['__eq__'] = method_in(names.eq) nsp['__ne__'] = method_in(names.ne) nsp['__add__'], nsp['__radd__'] = ops(names.add) nsp['__sub__'], nsp['__rsub__'] = ops(names.sub) nsp['__mul__'], nsp['__rmul__'] = ops(names.mul) nsp['__truediv__'], nsp['__rtruediv__'] = ops(names.truediv) nsp['__pow__'], nsp['__rpow__'] = ops(names.pow) nsp['__neg__'] = method(names.neg) nsp['__pos__'] = method(names.pos) nsp['__abs__'] = method(names.abs) return new_class('ArithmeticMixin', exec_body=exec_body)
def recursiveCopyUnproblematic(ontologyClass, ontologyParrent): found = False for rel in relations: if rel[0].upper() == ontologyClass.__name__.upper( ) and rel[1] != "includes": found = True break if rel[2].upper() == ontologyClass.__name__.upper( ) and rel[1] != "is_included": found = True break if found == False: with resultOntology: NewClass = types.new_class(ontologyClass.__name__, (ontologyParrent, ), {}) for ontologySubclass in ontologyClass.subclasses(): if not ontologySubclass.__name__ == ontologyClass.__name__: recursiveCopyUnproblematic(ontologySubclass, NewClass)