-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
184 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,92 @@ | ||
# saleyo | ||
# Saleyo | ||
|
||
Saleyo is a lightwight Python AOP framework, easy to use and integrate. | ||
|
||
## Getting Start | ||
|
||
```sh | ||
pip install saleyo | ||
``` | ||
|
||
## Basic Tutorial | ||
|
||
### Declear a `Mixin` class | ||
|
||
```python | ||
from saleyo import Mixin | ||
|
||
class Foo:... | ||
|
||
|
||
@Mixin(target = Foo) | ||
class MixinFoo:... | ||
``` | ||
|
||
### Use `MixinOperation` | ||
|
||
```python | ||
from typing import Any | ||
from saleyo import Mixin | ||
from saleyo.operation import Accessor, OverWrite, Post, Pre, Intercept, InvokeEvent | ||
|
||
|
||
class Foo: | ||
__private = "private varible" | ||
|
||
def demo(self): | ||
pass | ||
|
||
|
||
@Mixin(target=Foo) | ||
class MixinFoo: | ||
# Will add a varible named `__private` to Foo and it has the same address with `_Foo__private` | ||
private: Accessor[str] = Accessor("__private") | ||
|
||
# Will Add the `func` to `Foo` | ||
@OverWrite | ||
def func(self): | ||
print("hello saleyo") | ||
|
||
# Will intercept the demo method and redirect to `lambda: print("hello world")` | ||
@Intercept.configure(target_name="demo") | ||
@staticmethod | ||
def intercept_demo(_: InvokeEvent[None]): | ||
return InvokeEvent.from_call(lambda: print("hello world")) | ||
|
||
# Will call before `demo` call | ||
@Pre.configure(target_name="demo") | ||
def pre_demo(*arg): | ||
print("pre hello world") | ||
|
||
# Will call after `demo` call | ||
@Post.configure(target_name="demo") | ||
def post_demo(*arg): | ||
print("post hello world") | ||
|
||
|
||
foo: Any = ( | ||
Foo() | ||
) # Add the typing hint to avoid the error message from some IDE plugins. | ||
|
||
print(foo.__private) # Also `print(MixinFoo.private.value)` | ||
foo.func() | ||
foo.demo() | ||
|
||
>>> private varible | ||
>>> hello saleyo | ||
>>> hello world | ||
>>> post hello world | ||
>>> pre hello world | ||
``` | ||
|
||
### Custom Operation | ||
|
||
```python | ||
from typing import Any, Type | ||
from saleyo.base.template import MixinOperation | ||
from saleyo.base.toolchain import ToolChain | ||
|
||
class MyOperation(MixinOperation[Any]): | ||
def mixin(self, target: Type, toolchain: ToolChain = ...) -> None: | ||
... | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,12 @@ | ||
[project] | ||
name = "saleyo" | ||
version = "0.1.0" | ||
description = "A Package to do mixin in Python" | ||
authors = [ | ||
{name = "H2Sxxa", email = "[email protected]"}, | ||
] | ||
version = "0.1.1" | ||
description = "Saleyo is a lightwight Python AOP framework, easy to use and integrate." | ||
authors = [{ name = "H2Sxxa", email = "[email protected]" }] | ||
dependencies = [] | ||
requires-python = ">=3.11" | ||
requires-python = ">=3.8" | ||
readme = "README.md" | ||
license = {text = "MIT"} | ||
license = { text = "MIT" } | ||
|
||
[build-system] | ||
requires = ["pdm-backend"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
import intercept as intercept | ||
import demo as demo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from typing import Any, Type | ||
from saleyo.base.template import MixinOperation | ||
from saleyo.base.toolchain import ToolChain | ||
|
||
class MyOperation(MixinOperation[Any]): | ||
def mixin(self, target: Type, toolchain: ToolChain = ...) -> None: | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from typing import Any | ||
from saleyo import Mixin | ||
from saleyo.operation import Accessor, OverWrite, Post, Pre, Intercept, InvokeEvent | ||
|
||
|
||
class Foo: | ||
__private = "private varible" | ||
|
||
def demo(self): | ||
pass | ||
|
||
|
||
@Mixin(target=Foo) | ||
class MixinFoo: | ||
# Will add a varible named `__private` to Foo and it has the same address with `_Foo__private` | ||
private: Accessor[str] = Accessor("__private") | ||
|
||
# Will Add the `func` to `Foo` | ||
@OverWrite | ||
def func(self): | ||
print("hello saleyo") | ||
|
||
# Will intercept the demo method and redirect to `lambda: print("hello world")` | ||
@Intercept.configure(target_name="demo") | ||
@staticmethod | ||
def intercept_demo(_: InvokeEvent[None]): | ||
return InvokeEvent.from_call(lambda: print("hello world")) | ||
|
||
# Will call before `demo` call | ||
@Pre.configure(target_name="demo") | ||
def pre_demo(*arg): | ||
print("pre hello world") | ||
|
||
# Will call after `demo` call | ||
@Post.configure(target_name="demo") | ||
def post_demo(*arg): | ||
print("post hello world") | ||
|
||
|
||
foo: Any = ( | ||
Foo() | ||
) # Add the typing hint to avoid the error message from some IDE plugins. | ||
|
||
print(foo.__private) # Also `print(MixinFoo.private.value)` | ||
foo.func() | ||
foo.demo() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters