# Metamorphism
Implements metamorphism for Python classes using decorators
## Aim
Metamorphic classes are similar to polymorphic classes except that the sub-class is applied to an already existing object.
## Example usage
```python
from metamorph import morph, Metamorphic
class MetamorphBase(Metamorphic) :
def greet(self) :
return "Hello, World!"
class MetamorphChild(MetamorphBase) :
def greet(self) :
return "Hello, Earth!"
greeter = MetamorphBase()
print(greeter.greet()) # prints Hello, World!
morph(greeter, MetamorphChild)
print(greeter.greet()) # prints Hello, Earth!
```
## Constraints
By default the module enforces a number of constraints on metamorph children. It throws exception when the class is created. This is designed to prevent unexpected behavior during runtime. These can be partially or wholly ignored using `CustomMetamorphc`. (See below)
To avoid unpredictable side effects by default child classes are only allowed to change the behavior of an object. They cannot maintain their own state. Thus they can only implement methods that already exist in the metamorphic class and method signatures must match. Implementing `__init__` or `__new__` or any attributes also throws an exception. In addition child classes can only inherit directly from the base class and multiple inheritance is forbidden. The base class is still allowed to use multiple inheritance.
## Configuration
The module can be configured by using the metaclass `CustomMetamorphic` followed by keyword/option pairs. For example
```python
class UnsafeMetamorphic(Metamorphism, metaclass=CustomMetamorphism, strict=False) :
pass
class UnsafeMetamorph(UnsafeMetamorphic) :
def can_define_anything(self) :
pass
```
If you want to use the same settings in several metamorphic classes then create a class which derives from the `MetamorphismBase` class. For Example
```python
class UnsafeMetamophism(MetamorphismBase, metaclass=CustomMetamorphism, strict=False) :
pass
class UnsafeMetamorphic(UnsafeMetamophism) :
pass
class AnotherMetamorphic(UnsafeMetamophism) :
pass
```
The following options are available
| Option | Default | Notes |
|:-------|:--------|:------|
| `strict` | True | Setting this to False bypasses all checks on the sanity of the code. Note: Due to the potential for unexpected side effects any bugs reported from using this option may not addressed.
| `allow_mixed_typing` | False | Allows child metamorph to use type hinting if not applied by the base. However changing or removing the hint still raises an error.
| `allow_init` | False | Allows `__init__` or `__new__` to be defined in the child classes
| `allow_metamorph_subclassing` | False | Allow classes to inherit from metamorphs.
| `private_members` | False | Normally members starting with __ are shared between the base and its children. Setting this option to true makes these variables only accessible from the base class.
## API
* `Metamorphic` - The base class for metamorphic classes.
* ~~`metamorphism_config(strict, allow_mixed_types, allow_init, private_members)` - Used to configure the module.~~
* `morph(obj, morphclass)` - Morphs obj to the given morphclass.
* `ismetamorphic(obj_or_cls)` - Returns if class or object are metamorphic.
* `ismetamorphicbase(obj_or_cls)` - Returns if class or object is a metamorphic base class.
* `ismetamorphicchild(obj_or_cls)` - Returns if class or object is a metamorphic child class.
* `ismetamorphicconfig(obj_or_cls)` - Returns if class or object is use to configure metamorphism
* `MetamorphismError` - Raised if there is a problem with the definition of the classes.
* `MetamorphismException` - Raised if `morph` is used incorrectly.
* `MetamorphicType` - The metaclass for metamorphic classes. You should not need to use it directly. Inherit from the Metamorphic class instead.
* `CustomMetamorphic` - Allow customisation of metamorphism constraints.
* `MetamorphismBase` - The base class used for all metamorphism.
Raw data
{
"_id": null,
"home_page": "https://github.com/joshurtree/metamorphism",
"name": "metamorphism",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": null,
"keywords": null,
"author": "Josh Andrews",
"author_email": "joshurtree@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/fe/36/2a9eb62a2e4b3c152407ebc79c5c0c63c9fc3d842a108519ef9ad8921866/metamorphism-0.1.0.tar.gz",
"platform": null,
"description": "# Metamorphism\nImplements metamorphism for Python classes using decorators\n\n## Aim\nMetamorphic classes are similar to polymorphic classes except that the sub-class is applied to an already existing object. \n\n## Example usage\n```python\nfrom metamorph import morph, Metamorphic\n\nclass MetamorphBase(Metamorphic) :\n def greet(self) :\n return \"Hello, World!\"\n\nclass MetamorphChild(MetamorphBase) :\n def greet(self) :\n return \"Hello, Earth!\"\n\ngreeter = MetamorphBase()\nprint(greeter.greet()) # prints Hello, World!\nmorph(greeter, MetamorphChild)\nprint(greeter.greet()) # prints Hello, Earth!\n```\n\n## Constraints\nBy default the module enforces a number of constraints on metamorph children. It throws exception when the class is created. This is designed to prevent unexpected behavior during runtime. These can be partially or wholly ignored using `CustomMetamorphc`. (See below)\n\nTo avoid unpredictable side effects by default child classes are only allowed to change the behavior of an object. They cannot maintain their own state. Thus they can only implement methods that already exist in the metamorphic class and method signatures must match. Implementing `__init__` or `__new__` or any attributes also throws an exception. In addition child classes can only inherit directly from the base class and multiple inheritance is forbidden. The base class is still allowed to use multiple inheritance.\n\n## Configuration\nThe module can be configured by using the metaclass `CustomMetamorphic` followed by keyword/option pairs. For example\n\n```python\nclass UnsafeMetamorphic(Metamorphism, metaclass=CustomMetamorphism, strict=False) :\n pass\n\nclass UnsafeMetamorph(UnsafeMetamorphic) :\n def can_define_anything(self) :\n pass\n```\nIf you want to use the same settings in several metamorphic classes then create a class which derives from the `MetamorphismBase` class. For Example\n```python\nclass UnsafeMetamophism(MetamorphismBase, metaclass=CustomMetamorphism, strict=False) :\n pass\n\nclass UnsafeMetamorphic(UnsafeMetamophism) :\n pass\n\nclass AnotherMetamorphic(UnsafeMetamophism) :\n pass\n```\n\nThe following options are available\n| Option | Default | Notes |\n|:-------|:--------|:------|\n| `strict` | True | Setting this to False bypasses all checks on the sanity of the code. Note: Due to the potential for unexpected side effects any bugs reported from using this option may not addressed.\n| `allow_mixed_typing` | False | Allows child metamorph to use type hinting if not applied by the base. However changing or removing the hint still raises an error.\n| `allow_init` | False | Allows `__init__` or `__new__` to be defined in the child classes\n| `allow_metamorph_subclassing` | False | Allow classes to inherit from metamorphs.\n| `private_members` | False | Normally members starting with __ are shared between the base and its children. Setting this option to true makes these variables only accessible from the base class.\n\n## API\n\n* `Metamorphic` - The base class for metamorphic classes.\n* ~~`metamorphism_config(strict, allow_mixed_types, allow_init, private_members)` - Used to configure the module.~~ \n* `morph(obj, morphclass)` - Morphs obj to the given morphclass.\n* `ismetamorphic(obj_or_cls)` - Returns if class or object are metamorphic.\n* `ismetamorphicbase(obj_or_cls)` - Returns if class or object is a metamorphic base class.\n* `ismetamorphicchild(obj_or_cls)` - Returns if class or object is a metamorphic child class.\n* `ismetamorphicconfig(obj_or_cls)` - Returns if class or object is use to configure metamorphism \n* `MetamorphismError` - Raised if there is a problem with the definition of the classes.\n* `MetamorphismException` - Raised if `morph` is used incorrectly.\n* `MetamorphicType` - The metaclass for metamorphic classes. You should not need to use it directly. Inherit from the Metamorphic class instead.\n* `CustomMetamorphic` - Allow customisation of metamorphism constraints.\n* `MetamorphismBase` - The base class used for all metamorphism.\n",
"bugtrack_url": null,
"license": "Apache",
"summary": "Implements metamorphic classes for Python",
"version": "0.1.0",
"project_urls": {
"Homepage": "https://github.com/joshurtree/metamorphism"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "b1f2bffa6233a11c790507609e53c90c8fc023af7624ec1b780166f4a6fd30bd",
"md5": "092338844c6fe45e2b6309be293db0ef",
"sha256": "7abfce09e13aac9932d1091bc672fa2449199598c2d17fa37abe4ed12f9b66e1"
},
"downloads": -1,
"filename": "metamorphism-0.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "092338844c6fe45e2b6309be293db0ef",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 6853,
"upload_time": "2024-08-30T21:02:38",
"upload_time_iso_8601": "2024-08-30T21:02:38.731635Z",
"url": "https://files.pythonhosted.org/packages/b1/f2/bffa6233a11c790507609e53c90c8fc023af7624ec1b780166f4a6fd30bd/metamorphism-0.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "fe362a9eb62a2e4b3c152407ebc79c5c0c63c9fc3d842a108519ef9ad8921866",
"md5": "59a98af52f72a8facd494123ebf7653c",
"sha256": "1b70ee80e63f28d2ebaa48869d59a2a2e5a1f2b6108f98d8f2b57bec07847614"
},
"downloads": -1,
"filename": "metamorphism-0.1.0.tar.gz",
"has_sig": false,
"md5_digest": "59a98af52f72a8facd494123ebf7653c",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 6744,
"upload_time": "2024-08-30T21:02:40",
"upload_time_iso_8601": "2024-08-30T21:02:40.169820Z",
"url": "https://files.pythonhosted.org/packages/fe/36/2a9eb62a2e4b3c152407ebc79c5c0c63c9fc3d842a108519ef9ad8921866/metamorphism-0.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-30 21:02:40",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "joshurtree",
"github_project": "metamorphism",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "metamorphism"
}