Skip to content

Commit

Permalink
0.9.9 (#170)
Browse files Browse the repository at this point in the history
* ⚡️ `utils.mark` and `get_marked` now work with ProcGroup and other classes

* 🔖 0.9.9
  • Loading branch information
pwwang authored Jun 3, 2023
1 parent d358f42 commit 34deb54
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 137 deletions.
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## 0.9.9

- ⚡️ `utils.mark` and `get_marked` now work with `ProcGroup` and other classes

## 0.9.8

- 🐛 Fix priority of core plugin
Expand Down
7 changes: 6 additions & 1 deletion pipen/procgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

from os import PathLike
from functools import wraps, cached_property
from typing import Callable, Type, List
from typing import Any, Callable, Mapping, Type, List
from abc import ABC, ABCMeta
from diot import Diot

Expand Down Expand Up @@ -72,6 +72,7 @@ class ProcGroup(ABC, metaclass=ProcGropuMeta):
"""

name: str | None = None
__meta__: Mapping[str, Any] = {}
DEFAULTS = Diot()
PRESERVED = {
"opts",
Expand All @@ -85,6 +86,10 @@ class ProcGroup(ABC, metaclass=ProcGropuMeta):
"_INST",
}

def __init_subclass__(cls) -> None:
# Clear the meta
cls.__meta__ = {}

def __init__(self, **opts) -> None:
self.opts = Diot(self.__class__.DEFAULTS or {}) | (opts or {})
self.name = self.__class__.name or self.__class__.__name__
Expand Down
24 changes: 15 additions & 9 deletions pipen/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,37 +538,43 @@ def get_base(
return get_base(bases[0], abc_base, value, value_getter)


def mark(**kwargs) -> Callable[[Type[Proc]], Type[Proc]]:
"""Mark a proc with given kwargs as metadata
def mark(**kwargs) -> Callable[[type], type]:
"""Mark a class (e.g. Proc) with given kwargs as metadata
These marks will not be inherited by the subclasses.
These marks will not be inherited by the subclasses if the class is
a subclass of `Proc` or `ProcGroup`.
Args:
**kwargs: The kwargs to mark the proc
Returns:
The decorator
"""
def decorator(cls: type) -> type:
if not hasattr(cls, "__meta__"):
cls.__meta__ = {}

def decorator(proc: Type[Proc]) -> Type[Proc]:
proc.__meta__.update(kwargs)
return proc
cls.__meta__.update(kwargs)
return cls

return decorator


def get_marked(proc: Type[Proc], mark_name: str, default: Any = None) -> Any:
def get_marked(cls: type, mark_name: str, default: Any = None) -> Any:
"""Get the marked value from a proc
Args:
proc: The proc
cls: The proc
mark_name: The mark name
default: The default value if the mark is not found
Returns:
The marked value
"""
return proc.__meta__.get(mark_name, default)
if not hasattr(cls, "__meta__"):
return default

return cls.__meta__.get(mark_name, default)


def is_valid_name(name: str) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion pipen/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Provide version of pipen"""

__version__ = "0.9.8"
__version__ = "0.9.9"
260 changes: 135 additions & 125 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.masonry.api"

[tool.poetry]
name = "pipen"
version = "0.9.8"
version = "0.9.9"
description = "A pipeline framework for python"
authors = [ "pwwang <[email protected]>",]
license = "MIT"
Expand Down
17 changes: 17 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,20 @@ class P2(P1):

P3 = pipen.Proc.from_proc(P1)
assert get_marked(P3, "a") is None

class X:
...

assert get_marked(X, "a", None) is None

@mark(a=1)
class Y:
...

assert get_marked(Y, "a") == 1

class Z(Y):
...

# Marks inherited, as Y/Z are not Proc nor ProcGroup
assert get_marked(Z, "a", None) == 1

0 comments on commit 34deb54

Please sign in to comment.