classic-criteria 0.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Sergei Variasov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,74 @@
1
+ Metadata-Version: 2.1
2
+ Name: classic-criteria
3
+ Version: 0.0.1
4
+ Summary: Provides criteria object
5
+ Home-page: https://github.com/variasov/classic-criteria
6
+ Author: Sergei Variasov
7
+ Author-email: variasov@gmail.com
8
+ Project-URL: Bug Tracker, https://github.com/variasov/classic-criteria/issues
9
+ Classifier: Programming Language :: Python :: 3.10
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Operating System :: OS Independent
12
+ Classifier: Development Status :: 4 - Beta
13
+ Requires-Python: >=3.10
14
+ Description-Content-Type: text/markdown
15
+ License-File: LICENSE
16
+ Provides-Extra: dev
17
+ Requires-Dist: pytest~=7.4.4; extra == "dev"
18
+ Requires-Dist: pytest-cov~=4.1; extra == "dev"
19
+ Requires-Dist: twine~=4.0; extra == "dev"
20
+ Requires-Dist: build~=1.0; extra == "dev"
21
+
22
+ # Classic Criteria
23
+
24
+ Дает объект-критерий.
25
+
26
+ Нужен для описания критериев объектов, чтобы затем определять,
27
+ соответствуют ли те или иные объекты критерию, или для формирования запроса,
28
+ к примеру, в SQL-хранилище.
29
+
30
+ Пример:
31
+ ```python
32
+ from datetime import datetime
33
+ from dataclasses import dataclass
34
+ from classic.criteria import Criteria
35
+
36
+
37
+ @dataclass
38
+ class Task:
39
+ created_at: datetime
40
+ finished_at: datetime
41
+
42
+
43
+ @dataclass
44
+ class TaskOlderThan(Criteria[Task]):
45
+ date: datetime
46
+
47
+ def is_satisfied_by(self, candidate: Task) -> bool:
48
+ return candidate.created_at < self.date
49
+
50
+
51
+ @dataclass
52
+ class TaskObsolete(Criteria[Task]):
53
+ days_to_work: int
54
+
55
+ def is_satisfied_by(self, candidate: Task) -> bool:
56
+ days_spent = candidate.finished_at - candidate.created_at
57
+ return days_spent.days > self.days_to_work
58
+
59
+
60
+ some_task = Task(
61
+ created_at=datetime(2024, 1, 1),
62
+ finished_at=datetime(2024, 1, 10),
63
+ )
64
+ criteria = TaskObsolete(3) & TaskOlderThan(datetime(2024, 1, 31))
65
+ assert criteria.is_satisfied_by(some_task)
66
+
67
+
68
+ some_task = Task(
69
+ created_at=datetime(2024, 1, 1),
70
+ finished_at=datetime(2024, 1, 1),
71
+ )
72
+ criteria = TaskObsolete(3) & TaskOlderThan(datetime(2024, 1, 31))
73
+ assert not criteria.is_satisfied_by(some_task)
74
+ ```
@@ -0,0 +1,53 @@
1
+ # Classic Criteria
2
+
3
+ Дает объект-критерий.
4
+
5
+ Нужен для описания критериев объектов, чтобы затем определять,
6
+ соответствуют ли те или иные объекты критерию, или для формирования запроса,
7
+ к примеру, в SQL-хранилище.
8
+
9
+ Пример:
10
+ ```python
11
+ from datetime import datetime
12
+ from dataclasses import dataclass
13
+ from classic.criteria import Criteria
14
+
15
+
16
+ @dataclass
17
+ class Task:
18
+ created_at: datetime
19
+ finished_at: datetime
20
+
21
+
22
+ @dataclass
23
+ class TaskOlderThan(Criteria[Task]):
24
+ date: datetime
25
+
26
+ def is_satisfied_by(self, candidate: Task) -> bool:
27
+ return candidate.created_at < self.date
28
+
29
+
30
+ @dataclass
31
+ class TaskObsolete(Criteria[Task]):
32
+ days_to_work: int
33
+
34
+ def is_satisfied_by(self, candidate: Task) -> bool:
35
+ days_spent = candidate.finished_at - candidate.created_at
36
+ return days_spent.days > self.days_to_work
37
+
38
+
39
+ some_task = Task(
40
+ created_at=datetime(2024, 1, 1),
41
+ finished_at=datetime(2024, 1, 10),
42
+ )
43
+ criteria = TaskObsolete(3) & TaskOlderThan(datetime(2024, 1, 31))
44
+ assert criteria.is_satisfied_by(some_task)
45
+
46
+
47
+ some_task = Task(
48
+ created_at=datetime(2024, 1, 1),
49
+ finished_at=datetime(2024, 1, 1),
50
+ )
51
+ criteria = TaskObsolete(3) & TaskOlderThan(datetime(2024, 1, 31))
52
+ assert not criteria.is_satisfied_by(some_task)
53
+ ```
@@ -0,0 +1,6 @@
1
+ [build-system]
2
+ requires = [
3
+ "setuptools >= 40.6.0",
4
+ "wheel"
5
+ ]
6
+ build-backend = "setuptools.build_meta"
@@ -0,0 +1,37 @@
1
+ [metadata]
2
+ name = classic-criteria
3
+ version = 0.0.1
4
+ description = Provides criteria object
5
+ author = Sergei Variasov
6
+ author_email = variasov@gmail.com
7
+ long_description = file: README.md
8
+ long_description_content_type = text/markdown
9
+ url = https://github.com/variasov/classic-criteria
10
+ project_urls =
11
+ Bug Tracker = https://github.com/variasov/classic-criteria/issues
12
+ classifiers =
13
+ Programming Language :: Python :: 3.10
14
+ License :: OSI Approved :: MIT License
15
+ Operating System :: OS Independent
16
+ Development Status :: 4 - Beta
17
+
18
+ [options]
19
+ packages = find_namespace:
20
+ python_requires = >=3.10
21
+ package_dir =
22
+ = sources
23
+
24
+ [options.packages.find]
25
+ where = sources
26
+
27
+ [options.extras_require]
28
+ dev =
29
+ pytest~=7.4.4
30
+ pytest-cov~=4.1
31
+ twine~=4.0
32
+ build~=1.0
33
+
34
+ [egg_info]
35
+ tag_build =
36
+ tag_date = 0
37
+
@@ -0,0 +1,3 @@
1
+ from setuptools import setup
2
+
3
+ setup()
@@ -0,0 +1 @@
1
+ from .criteria import Criteria
@@ -0,0 +1,187 @@
1
+ from typing import Generic, Optional, Self, TypeVar, get_args, Type
2
+
3
+
4
+ Entity = TypeVar('Entity')
5
+
6
+
7
+ class Criteria(Generic[Entity]):
8
+ """
9
+ Базовый объект-критерий.
10
+
11
+ Нужен для описания критериев объектов, чтобы затем определять,
12
+ соответствуют ли те или иные объекты критерию, или для формирования запроса,
13
+ к примеру, в SQL-хранилище.
14
+
15
+ Взят отсюда и чуть-чуть доработано:
16
+ https://gist.github.com/palankai/f73a18ce06751ab8f245
17
+
18
+ Пример:
19
+ >>> from datetime import datetime
20
+ ... from dataclasses import dataclass
21
+ ... from classic.criteria import Criteria
22
+ ...
23
+ ... @dataclass
24
+ ... class Task:
25
+ ... created_at: datetime
26
+ ... finished_at: datetime
27
+ ...
28
+ ... @dataclass
29
+ ... class TaskOlderThan(Criteria[Task]):
30
+ ... date: datetime
31
+ ...
32
+ ... def is_satisfied_by(self, candidate: Task) -> bool:
33
+ ... return candidate.created_at < self.date
34
+ ...
35
+ ... @dataclass
36
+ ... class TaskObsolete(Criteria[Task]):
37
+ ... days_to_work: int
38
+ ...
39
+ ... def is_satisfied_by(self, candidate: Task) -> bool:
40
+ ... days_spent = candidate.finished_at - candidate.created_at
41
+ ... return days_spent.days > self.days_to_work
42
+ ...
43
+ ... some_task = Task(
44
+ ... created_at=datetime(2024, 1, 1),
45
+ ... finished_at=datetime(2024, 1, 10),
46
+ ... )
47
+ ... criteria = TaskObsolete(3) & TaskOlderThan(datetime(2024, 1, 31))
48
+ ... criteria.is_satisfied_by(some_task)
49
+ True
50
+ >>> some_task = Task(
51
+ ... created_at=datetime(2024, 1, 1),
52
+ ... finished_at=datetime(2024, 1, 1),
53
+ ... )
54
+ ... criteria = TaskObsolete(3) & TaskOlderThan(datetime(2024, 1, 31))
55
+ ... criteria.is_satisfied_by(some_task)
56
+ False
57
+ """
58
+
59
+ @property
60
+ def entity_cls(self) -> Type[object]:
61
+ args = get_args(self)
62
+ if len(args) >= 0:
63
+ return args[0]
64
+
65
+ def __and__(self, other):
66
+ return And(self, other)
67
+
68
+ def __or__(self, other):
69
+ return Or(self, other)
70
+
71
+ def __xor__(self, other):
72
+ return Xor(self, other)
73
+
74
+ def __invert__(self):
75
+ return Invert(self)
76
+
77
+ def is_satisfied_by(self, candidate: Entity) -> bool:
78
+ raise NotImplemented
79
+
80
+ def remainder_unsatisfied_by(self, candidate: Entity) -> Optional[Self]:
81
+ if self.is_satisfied_by(candidate):
82
+ return None
83
+ else:
84
+ return self
85
+
86
+
87
+ class BoundCriteria(Criteria[Entity]):
88
+
89
+ def __init__(self, fn, *args, **kwargs):
90
+ self.fn = fn
91
+ self.args = args
92
+ self.kwargs = kwargs
93
+
94
+ def is_satisfied_by(self, candidate: Entity) -> bool:
95
+ assert isinstance(candidate, self.entity_cls)
96
+ return self.fn(candidate, *self.args, **self.kwargs)
97
+
98
+
99
+ class CompositeCriteria(Criteria[Entity]):
100
+
101
+ def __init__(self, *criteria: Criteria[Entity]):
102
+ self.criteria = criteria
103
+
104
+ def is_satisfied_by(self, candidate: Entity) -> bool:
105
+ raise NotImplementedError
106
+
107
+
108
+ class And(CompositeCriteria):
109
+
110
+ def __and__(self, other: Criteria[Entity]):
111
+ if isinstance(other, And):
112
+ self.criteria += other.criteria
113
+ else:
114
+ self.criteria += (other,)
115
+ return self
116
+
117
+ def is_satisfied_by(self, candidate: Entity):
118
+ satisfied = all([
119
+ criteria.is_satisfied_by(candidate)
120
+ for criteria in self.criteria
121
+ ])
122
+ return satisfied
123
+
124
+ def remainder_unsatisfied_by(self, candidate: Entity):
125
+ non_satisfied = [
126
+ criteria
127
+ for criteria in self.criteria
128
+ if not criteria.is_satisfied_by(candidate)
129
+ ]
130
+ if not non_satisfied:
131
+ return None
132
+ if len(non_satisfied) == 1:
133
+ return non_satisfied[0]
134
+ if len(non_satisfied) == len(self.criteria):
135
+ return self
136
+ return And(*non_satisfied)
137
+
138
+
139
+ class Or(CompositeCriteria):
140
+
141
+ def __or__(self, other):
142
+ if isinstance(other, Or):
143
+ self.criteria += other.criteria
144
+ else:
145
+ self.criteria += (other,)
146
+ return self
147
+
148
+ def is_satisfied_by(self, candidate: Entity):
149
+ satisfied = any([
150
+ criteria.is_satisfied_by(candidate)
151
+ for criteria in self.criteria
152
+ ])
153
+ return satisfied
154
+
155
+
156
+ class NestedCriteria(Criteria[Entity]):
157
+
158
+ def __init__(self, criteria: Criteria[Entity]):
159
+ self.criteria = criteria
160
+
161
+ def is_satisfied_by(self, candidate: Entity) -> bool:
162
+ raise NotImplementedError
163
+
164
+
165
+ class Invert(NestedCriteria):
166
+
167
+ def is_satisfied_by(self, candidate: Entity):
168
+ return not self.criteria.is_satisfied_by(candidate)
169
+
170
+
171
+ class BinaryCriteria(Criteria[Entity]):
172
+
173
+ def __init__(self, left: Criteria[Entity], right: Criteria[Entity]):
174
+ self.left = left
175
+ self.right = right
176
+
177
+ def is_satisfied_by(self, candidate: Entity) -> bool:
178
+ raise NotImplementedError
179
+
180
+
181
+ class Xor(BinaryCriteria[Entity]):
182
+
183
+ def is_satisfied_by(self, candidate: Entity):
184
+ return (
185
+ self.left.is_satisfied_by(candidate) ^
186
+ self.right.is_satisfied_by(candidate)
187
+ )
@@ -0,0 +1,74 @@
1
+ Metadata-Version: 2.1
2
+ Name: classic-criteria
3
+ Version: 0.0.1
4
+ Summary: Provides criteria object
5
+ Home-page: https://github.com/variasov/classic-criteria
6
+ Author: Sergei Variasov
7
+ Author-email: variasov@gmail.com
8
+ Project-URL: Bug Tracker, https://github.com/variasov/classic-criteria/issues
9
+ Classifier: Programming Language :: Python :: 3.10
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Operating System :: OS Independent
12
+ Classifier: Development Status :: 4 - Beta
13
+ Requires-Python: >=3.10
14
+ Description-Content-Type: text/markdown
15
+ License-File: LICENSE
16
+ Provides-Extra: dev
17
+ Requires-Dist: pytest~=7.4.4; extra == "dev"
18
+ Requires-Dist: pytest-cov~=4.1; extra == "dev"
19
+ Requires-Dist: twine~=4.0; extra == "dev"
20
+ Requires-Dist: build~=1.0; extra == "dev"
21
+
22
+ # Classic Criteria
23
+
24
+ Дает объект-критерий.
25
+
26
+ Нужен для описания критериев объектов, чтобы затем определять,
27
+ соответствуют ли те или иные объекты критерию, или для формирования запроса,
28
+ к примеру, в SQL-хранилище.
29
+
30
+ Пример:
31
+ ```python
32
+ from datetime import datetime
33
+ from dataclasses import dataclass
34
+ from classic.criteria import Criteria
35
+
36
+
37
+ @dataclass
38
+ class Task:
39
+ created_at: datetime
40
+ finished_at: datetime
41
+
42
+
43
+ @dataclass
44
+ class TaskOlderThan(Criteria[Task]):
45
+ date: datetime
46
+
47
+ def is_satisfied_by(self, candidate: Task) -> bool:
48
+ return candidate.created_at < self.date
49
+
50
+
51
+ @dataclass
52
+ class TaskObsolete(Criteria[Task]):
53
+ days_to_work: int
54
+
55
+ def is_satisfied_by(self, candidate: Task) -> bool:
56
+ days_spent = candidate.finished_at - candidate.created_at
57
+ return days_spent.days > self.days_to_work
58
+
59
+
60
+ some_task = Task(
61
+ created_at=datetime(2024, 1, 1),
62
+ finished_at=datetime(2024, 1, 10),
63
+ )
64
+ criteria = TaskObsolete(3) & TaskOlderThan(datetime(2024, 1, 31))
65
+ assert criteria.is_satisfied_by(some_task)
66
+
67
+
68
+ some_task = Task(
69
+ created_at=datetime(2024, 1, 1),
70
+ finished_at=datetime(2024, 1, 1),
71
+ )
72
+ criteria = TaskObsolete(3) & TaskOlderThan(datetime(2024, 1, 31))
73
+ assert not criteria.is_satisfied_by(some_task)
74
+ ```
@@ -0,0 +1,12 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ setup.cfg
5
+ setup.py
6
+ sources/classic/criteria/__init__.py
7
+ sources/classic/criteria/criteria.py
8
+ sources/classic_criteria.egg-info/PKG-INFO
9
+ sources/classic_criteria.egg-info/SOURCES.txt
10
+ sources/classic_criteria.egg-info/dependency_links.txt
11
+ sources/classic_criteria.egg-info/requires.txt
12
+ sources/classic_criteria.egg-info/top_level.txt
@@ -0,0 +1,6 @@
1
+
2
+ [dev]
3
+ pytest~=7.4.4
4
+ pytest-cov~=4.1
5
+ twine~=4.0
6
+ build~=1.0