meta-interface


Namemeta-interface JSON
Version 0.0.1.1.1 PyPI version JSON
download
home_pageNone
SummaryA tiny project that introduces the interface concept to Python.
upload_time2024-08-31 04:31:18
maintainerNone
docs_urlNone
authorNone
requires_python>=3.7
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Python Metaclass Based Interface

This is a tiny project that introduces the interface concept to Python.

This documentation describes the use of Python metaclasses to create and manage interfaces in Python. The code defines two primary metaclasses, `Interface` and `Interfaces`, which can be used to enforce interface implementation and manage multiple interfaces respectively.

### Usage

To use this package, it is necessary to import the package firstly. You can import partly, but it will import all here for presentation.

```python
from interface import *
```

## Metaclass `Interface`

The `Interface` metaclass is used to define an interface, which is a way to ensure that certain methods are implemented by any class that claims to implement the interface. This is akin to creating a contract for class behavior.

### Usage

To define an interface using `Interface`, simply declare a class with methods decorated by `@abstractmethod`. Here's a basic example:

```python
class IC(Interface):
    @abstractmethod
    def my_abstract_method(self, ...):
        pass
```

Classes that want to implement this interface should use the interface as a metaclass and provide concrete implementations of all abstract methods:

```python
class C(metaclass=IC):
    def my_abstract_method(self, ...):
        print("Implementation of abstract method")
```

## Restricted Interface

If you want the interface to be specifically implemented by certain classes or their subclasses, the interface can be parameterized by passing a class reference to `Interface`. For example:

### Usage

```python
class IBase(Interface[Base]):
    @abstractmethod
    def my_abstract_method(self, ...):
        pass

class C(Base, metaclass=IBase):
    def my_abstract_method(self, ...):
        print("Restricted implementation")
```

## Metaclass `Interfaces`

The `Interfaces` metaclass allows a class to observe multiple interfaces. Specially, if multiple interfaces define a method with the same name, the implementation in the class observing these interfaces will apply the method from the last interface specified.

### Usage

Here’s how you can use `Interfaces` to make a class observe multiple interfaces:

```python
class IC(Interface):
    @abstractmethod
    def my_abstract_methodA(self, ...): pass

class IBase(Interface[Base]):
    @abstractmethod
    def my_abstract_methodB(self, ...): pass

class C(Base, metaclass=Interfaces(IC, IBase)):
    def my_abstract_methodA(self, ...):
        print('A')
    
    def my_abstract_methodB(self, ...):
        print('B')
```

# Decorators for Interfaces

This documentation provides details on the usage of decorators `abstractmethod`, `defaultmethod`, and `declaredclass` in Python, designed to work with metaclasses derived from `Interface`. These decorators help in defining interface constraints and behaviors for classes.

## Decorator `abstractmethod`

The `abstractmethod` decorator is used to mark methods as abstract within an interface. This indicates that any concrete class implementing this interface must provide an implementation for these methods. 

### Usage

To use `abstractmethod`, include it in a class that uses an `Interface` as its metaclass or is derived from such an interface:

```python
class IBase(Interface[Base]):
    @abstractmethod
    def my_abstract_method(self, ...):
        pass

class C(metaclass=IBase):
    def my_abstract_method(self, ...):
        print("Implemented method")
```

## Decorator `defaultmethod`

The `defaultmethod` decorator indicates that a method has a default implementation within an interface. Classes using this interface can override these methods, but it's not mandatory for instantiation.

### Usage

Here's how to apply `defaultmethod` in your class definitions:

```python
class IBase(Interface[Base]):
    @defaultmethod
    def my_method(self, ...):
        print("Default implementation")

class C(Base, metaclass=IBase):
    def my_method(self, ...):
        IBase.my_method(self, ...)  # Call the default implementation
        print("Overridden implementation")
```

## Decorator `declaredclass`

The `declaredclass` decorator is used to indicate a base class for which an interface wil be specifically restricted. This decorator is essential when an interface is tightly coupled with a specific class.

### Usage

Use `declaredclass` to mark a class as the base class for an interface:

```python
@declaredclass
class Base: pass

class IBase(Interface[Base]):
    @abstractmethod
    def my_method(self, ...):
        pass

class Base(metaclass=IBase):
    def my_method(self, ...):
        print("Method implementation")
```

# Additional Notes

- The `Interface` and `Interfaces` metaclasses include mechanisms to ensure that classes properly implement required methods, either through direct implementation or through class hierarchy constraints.
- The use of these metaclasses makes it clear at the class definition level which interfaces a class intends to implement, promoting better structure and organization in complex systems.
- These decorators require that the class definition includes an appropriate metaclass, either `Interface` or a derivation.
- `abstractmethod` and `defaultmethod` provide a clear indication of method requirements and implementations at the interface level, promoting more robust object-oriented design.
- `declaredclass` ensures the interface is adhered to by a specific class, facilitating controlled implementations and dependencies.

This setup in Python facilitates clear, modular, and maintainable code, especially useful in large projects where enforcing design patterns and consistency is crucial.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "meta-interface",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": null,
    "keywords": null,
    "author": null,
    "author_email": "Caewinix <Caewinix@yeah.net>",
    "download_url": "https://files.pythonhosted.org/packages/96/3e/879d6b71b0c59dee264bae0e15432a8ba8eba913dd9d15e5c4fe87f70af7/meta_interface-0.0.1.1.1.tar.gz",
    "platform": null,
    "description": "# Python Metaclass Based Interface\n\nThis is a tiny project that introduces the interface concept to Python.\n\nThis documentation describes the use of Python metaclasses to create and manage interfaces in Python. The code defines two primary metaclasses, `Interface` and `Interfaces`, which can be used to enforce interface implementation and manage multiple interfaces respectively.\n\n### Usage\n\nTo use this package, it is necessary to import the package firstly. You can import partly, but it will import all here for presentation.\n\n```python\nfrom interface import *\n```\n\n## Metaclass `Interface`\n\nThe `Interface` metaclass is used to define an interface, which is a way to ensure that certain methods are implemented by any class that claims to implement the interface. This is akin to creating a contract for class behavior.\n\n### Usage\n\nTo define an interface using `Interface`, simply declare a class with methods decorated by `@abstractmethod`. Here's a basic example:\n\n```python\nclass IC(Interface):\n    @abstractmethod\n    def my_abstract_method(self, ...):\n        pass\n```\n\nClasses that want to implement this interface should use the interface as a metaclass and provide concrete implementations of all abstract methods:\n\n```python\nclass C(metaclass=IC):\n    def my_abstract_method(self, ...):\n        print(\"Implementation of abstract method\")\n```\n\n## Restricted Interface\n\nIf you want the interface to be specifically implemented by certain classes or their subclasses, the interface can be parameterized by passing a class reference to `Interface`. For example:\n\n### Usage\n\n```python\nclass IBase(Interface[Base]):\n    @abstractmethod\n    def my_abstract_method(self, ...):\n        pass\n\nclass C(Base, metaclass=IBase):\n    def my_abstract_method(self, ...):\n        print(\"Restricted implementation\")\n```\n\n## Metaclass `Interfaces`\n\nThe `Interfaces` metaclass allows a class to observe multiple interfaces. Specially, if multiple interfaces define a method with the same name, the implementation in the class observing these interfaces will apply the method from the last interface specified.\n\n### Usage\n\nHere\u2019s how you can use `Interfaces` to make a class observe multiple interfaces:\n\n```python\nclass IC(Interface):\n    @abstractmethod\n    def my_abstract_methodA(self, ...): pass\n\nclass IBase(Interface[Base]):\n    @abstractmethod\n    def my_abstract_methodB(self, ...): pass\n\nclass C(Base, metaclass=Interfaces(IC, IBase)):\n    def my_abstract_methodA(self, ...):\n        print('A')\n    \n    def my_abstract_methodB(self, ...):\n        print('B')\n```\n\n# Decorators for Interfaces\n\nThis documentation provides details on the usage of decorators `abstractmethod`, `defaultmethod`, and `declaredclass` in Python, designed to work with metaclasses derived from `Interface`. These decorators help in defining interface constraints and behaviors for classes.\n\n## Decorator `abstractmethod`\n\nThe `abstractmethod` decorator is used to mark methods as abstract within an interface. This indicates that any concrete class implementing this interface must provide an implementation for these methods. \n\n### Usage\n\nTo use `abstractmethod`, include it in a class that uses an `Interface` as its metaclass or is derived from such an interface:\n\n```python\nclass IBase(Interface[Base]):\n    @abstractmethod\n    def my_abstract_method(self, ...):\n        pass\n\nclass C(metaclass=IBase):\n    def my_abstract_method(self, ...):\n        print(\"Implemented method\")\n```\n\n## Decorator `defaultmethod`\n\nThe `defaultmethod` decorator indicates that a method has a default implementation within an interface. Classes using this interface can override these methods, but it's not mandatory for instantiation.\n\n### Usage\n\nHere's how to apply `defaultmethod` in your class definitions:\n\n```python\nclass IBase(Interface[Base]):\n    @defaultmethod\n    def my_method(self, ...):\n        print(\"Default implementation\")\n\nclass C(Base, metaclass=IBase):\n    def my_method(self, ...):\n        IBase.my_method(self, ...)  # Call the default implementation\n        print(\"Overridden implementation\")\n```\n\n## Decorator `declaredclass`\n\nThe `declaredclass` decorator is used to indicate a base class for which an interface wil be specifically restricted. This decorator is essential when an interface is tightly coupled with a specific class.\n\n### Usage\n\nUse `declaredclass` to mark a class as the base class for an interface:\n\n```python\n@declaredclass\nclass Base: pass\n\nclass IBase(Interface[Base]):\n    @abstractmethod\n    def my_method(self, ...):\n        pass\n\nclass Base(metaclass=IBase):\n    def my_method(self, ...):\n        print(\"Method implementation\")\n```\n\n# Additional Notes\n\n- The `Interface` and `Interfaces` metaclasses include mechanisms to ensure that classes properly implement required methods, either through direct implementation or through class hierarchy constraints.\n- The use of these metaclasses makes it clear at the class definition level which interfaces a class intends to implement, promoting better structure and organization in complex systems.\n- These decorators require that the class definition includes an appropriate metaclass, either `Interface` or a derivation.\n- `abstractmethod` and `defaultmethod` provide a clear indication of method requirements and implementations at the interface level, promoting more robust object-oriented design.\n- `declaredclass` ensures the interface is adhered to by a specific class, facilitating controlled implementations and dependencies.\n\nThis setup in Python facilitates clear, modular, and maintainable code, especially useful in large projects where enforcing design patterns and consistency is crucial.\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A tiny project that introduces the interface concept to Python.",
    "version": "0.0.1.1.1",
    "project_urls": {
        "Bug Tracker": "https://github.com/Caewinix/meta-interface/issues",
        "Homepage": "https://github.com/Caewinix/meta-interface"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7d6a735f31b54af850fb87a7f0bd6bf46c60b045e52258cb27c7986aab127096",
                "md5": "0e0a72c4caf4d135bbee6e5a35deb281",
                "sha256": "01fdb50ea808675ebcd8e2b1f81beb9374ef5896eb484580ee57277e2a98e999"
            },
            "downloads": -1,
            "filename": "meta_interface-0.0.1.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "0e0a72c4caf4d135bbee6e5a35deb281",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 6499,
            "upload_time": "2024-08-31T04:31:16",
            "upload_time_iso_8601": "2024-08-31T04:31:16.183042Z",
            "url": "https://files.pythonhosted.org/packages/7d/6a/735f31b54af850fb87a7f0bd6bf46c60b045e52258cb27c7986aab127096/meta_interface-0.0.1.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "963e879d6b71b0c59dee264bae0e15432a8ba8eba913dd9d15e5c4fe87f70af7",
                "md5": "6157cf92b721dc9469080df12690fc80",
                "sha256": "592ea4286e63933f084a0891d521bc6e32fe45b9d156bd810a87bbb8b64333f1"
            },
            "downloads": -1,
            "filename": "meta_interface-0.0.1.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "6157cf92b721dc9469080df12690fc80",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 8462,
            "upload_time": "2024-08-31T04:31:18",
            "upload_time_iso_8601": "2024-08-31T04:31:18.198379Z",
            "url": "https://files.pythonhosted.org/packages/96/3e/879d6b71b0c59dee264bae0e15432a8ba8eba913dd9d15e5c4fe87f70af7/meta_interface-0.0.1.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-08-31 04:31:18",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Caewinix",
    "github_project": "meta-interface",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "meta-interface"
}
        
Elapsed time: 0.57152s