{"id":19720849,"url":"https://github.com/quantopian/metautils","last_synced_at":"2025-07-23T06:04:26.734Z","repository":{"id":27264887,"uuid":"30737518","full_name":"quantopian/metautils","owner":"quantopian","description":"Utilities for writing metaclasses.","archived":false,"fork":false,"pushed_at":"2023-04-06T22:25:22.000Z","size":31,"stargazers_count":8,"open_issues_count":0,"forks_count":9,"subscribers_count":15,"default_branch":"master","last_synced_at":"2025-04-29T21:39:22.087Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/quantopian.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-02-13T02:57:06.000Z","updated_at":"2023-01-22T23:21:14.000Z","dependencies_parsed_at":"2024-11-11T23:12:50.002Z","dependency_job_id":"b5cdc9ec-1a54-48a3-924e-f04318dc2854","html_url":"https://github.com/quantopian/metautils","commit_stats":{"total_commits":18,"total_committers":2,"mean_commits":9.0,"dds":0.05555555555555558,"last_synced_commit":"10e11c5bd8bd7ded52b97261f61c3186607bd617"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/quantopian/metautils","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quantopian%2Fmetautils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quantopian%2Fmetautils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quantopian%2Fmetautils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quantopian%2Fmetautils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/quantopian","download_url":"https://codeload.github.com/quantopian/metautils/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quantopian%2Fmetautils/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266626115,"owners_count":23958344,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-23T02:00:09.312Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-11T23:12:42.680Z","updated_at":"2025-07-23T06:04:26.708Z","avatar_url":"https://github.com/quantopian.png","language":"Python","readme":"metautils\n=========\n\nUtilities for writing and composing metaclasses.\n\nTemplate Model\n-----------------------\n\nWhy do we need or want to write class templates.\n\nConsider the two metaclasses.\n\n.. code:: python\n\n    class AllLower(type):\n        def __new__(mcls, name, bases, dict_):\n            dict_ = {k.lower(): v for k, v in dict_.items()}\n            return super().__new__(mcls, name, bases, dict_)\n\n\n    class MethodCatcher(type):\n        def __new__(mcls, name, bases, dict_):\n            dict_['methods'] = [v for v in dict_.values() if callable(v)]\n            return super().__new__(mcls, name, bases, dict_)\n\nWhat would we do if we wanted to make a class that used *BOTH* of these\nmetaclasses? Using a class that subclasses both ``AllLower`` and\n``MethodCatcher`` does not work, what we want is a way to chain them.\n\nWith the class template model, we could have written our metaclasses\nas:\n\n.. code:: python\n\n    from metautils import T, templated\n\n    class AllLower(T):\n        @templated\n        def __new__(mcls, name, bases, dict_, T_):\n            dict_ = {k.lower(): v for k, v in dict_.items()}\n            return T_.__new__(mcls, name, bases, dict_)\n\n\n    class MethodCatcher(T):\n        @templated\n        def __new__(mcls, name, bases, dict_, T_):\n            dict_['methods'] = [v for v in dict_.values() if callable(v)];\n            return T_.__new__(mcls, name, bases, dict_)\n\nNow we can define classes that use *BOTH* of these metaclasses like so:\n\n.. code:: python\n\n    class C(object, metaclass=MethodCatcher(AllLower())):\n        def F():\n            pass\n\n        def g():\n            pass\n\n        a = 'a'\n        B = 'b'\n\nWe can see that this applied the composition of the metaclasses.\n\n.. code:: python\n\n    \u003e\u003e\u003e C.f\n    \u003cfunction __main__.C.F\u003e\n    \u003e\u003e\u003e C.g\n    \u003cfunction __main__.C.g\u003e\n    \u003e\u003e\u003e C.b\n    'b'\n    \u003e\u003e\u003e C.a\n    'a'\n    \u003e\u003e\u003e C.methods\n    [\u003cfunction __main__.C.g\u003e, \u003cfunction __main__.C.F\u003e]\n\nThe order that the metaclasses are composed is explicit as they act as\ntransformers over each other.\n\n\n``Template``\n--------------------\n\nWhile the previous example only showed metaclasses, you can use this for any\nclass; however, it is most useful for metaclasses where having a compatible\nmetaclass hierarchy is important.\n\nA ``Template`` is a callable that takes a ``type`` object and\nreturns a new ``type`` object. It takes the following arguments:\n\n-  ``base``: A type object. ``default``: ``type``.\n-  ``adjust_name``: Should we prepend the name of the ``base`` to the\n   new type object. ``default``: ``True``.\n\nThese can be chained together with any concrete metaclass at the end,\ne.g.:\n\n.. code:: python\n\n    new_class = m(n,p(q(...z(type)...)))\n\nYou can also use the compose function to do this:\n\n.. code:: python\n\n    from metautils import compose\n\n    new_class_template = compose(m, n, p, q, ..., z)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fquantopian%2Fmetautils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fquantopian%2Fmetautils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fquantopian%2Fmetautils/lists"}