-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_application.py
More file actions
110 lines (84 loc) · 2.54 KB
/
example_application.py
File metadata and controls
110 lines (84 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import random
import string
from datetime import datetime
from pygating.gates.date_gate import DateGate
from src.pygating import PyGating
from src.pygating.gates import PercentageGate
from src.pygating.gating_configurations import GatingConfigurationAll
def generate_random_id():
return "".join(random.choices(string.ascii_letters + string.digits, k=16))
def random_string(length):
return "".join(random.choices(string.ascii_letters, k=length))
class Name:
def __init__(self, first, last):
self._first = first
self._last = last
self.details = {"length": str(len(first + last))}
@property
def first(self):
return self._first
def last_name(self):
return self._last
class Shop:
def __init__(self, name):
self._name = name
def get_name(self):
return self._name
class Entity:
def __init__(self, id: str, shop):
self.id = id
self.shop = shop
# Get the created at time to 2024-01-01
self.created_at = datetime(2024, 1, 1)
def get_id(self):
return self.id
# Initialize PyGating
PyGating.init()
# Configure the gate
gate_config = GatingConfigurationAll(
fail_closed=True,
gates=[
PercentageGate(
percentage=10, allow=True, entity_property="shop.get_name.details.length"
),
],
)
# gate_config = {
# "type": "GatingConfigurationAll",
# "fail_closed": True,
# "gates": [
# {
# "type": "PercentageGate",
# "percentage": 10.0,
# "allow": True,
# "entity_property": "id"
# }
# ]
# }
# Generate entities and check gate
num_entities = 1000
passed_count = 0
for _ in range(num_entities):
first_name = random_string(random.randint(10, 20))
last_name = random_string(random.randint(10, 20))
name = Name(first=first_name, last=last_name)
shop = Shop(name=name)
entity = Entity(id=generate_random_id(), shop=shop)
if PyGating.check_gating(gate_config, entity=entity):
passed_count += 1
# Print out the percentage of entities that passed the gate
percentage_passed = (passed_count / num_entities) * 100
print(
f"Percentage of entities that passed the gate: {percentage_passed}%"
) # Should be roughly 5%
gate_config = GatingConfigurationAll(
fail_closed=True,
gates=[
DateGate(
start_date=datetime(2024, 1, 1),
end_date=datetime(2024, 12, 31),
entity_property="created_at",
),
],
)
print(PyGating.check_gating(gate_config, entity=entity))