Skip to content

Commit

Permalink
Bug fix and README modify at 2024-07-12T17:31:39+0800
Browse files Browse the repository at this point in the history
  • Loading branch information
miyuki-shirogane committed Jul 12, 2024
1 parent b1d06c4 commit 9dc7717
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 25 deletions.
81 changes: 57 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,29 @@ pip install PistolMagazine

Here’s a quick example to get you started:


You can use the `pistol-gen` command to interactively select a sample code template and specify the path for the generated code within your project directory.

Run the following command:

```bash
pistol-gen
```

The tool will then generate the file with the chosen template at the specified location.

```python
from pistol_magazine import *
from random import choice
from pistol_magazine.hooks.hooks import hook
from pistol_magazine import *


# Create a custom provider
@provider
class MyProvider:
def user_status(self):
def my_provider(self):
return choice(["ACTIVE", "INACTIVE"])


"""
Define hook functions
pre_generate: Executes operations before generating all data. Suitable for tasks like logging or starting external services.
Expand All @@ -59,46 +70,68 @@ def pre_generate_second_hook():
Perform some preprocessing operations, such as starting external services.
"""


@hook('after_generate', order=1, hook_set="SET1")
def after_generate_first_hook(data):
data['user_status'] = 'ACTIVE' if data['user_age'] >= 18 else 'INACTIVE'
data['customized_param'] = 'ACTIVE' if data['int_param'] >= 18 else 'INACTIVE'
return data


@hook('final_generate', order=1, hook_set="SET1")
def final_generate_second_hook(data):
"""
Suppose there is a function send_to_message_queue(data) to send data to the message queue
Or use built-in data exporters to export data, like the code below⬇️.
"""

# Use the custom provider
class UserInfo(DataMocker):
create_time: Timestamp = Timestamp(Timestamp.D_TIMEE10, days=2)
user_name: Str = Str(data_type="name")
user_email: Str = Str(data_type="email")
user_age: Int = Int(byte_nums=6, unsigned=True)
user_status: ProviderField = ProviderField(MyProvider().user_status)
user_marriage: Bool = Bool()
user_dict: Dict = Dict(
json_exporter = JSONExporter() # Also support csv, db, xml export
json_exporter.export(data, 'output.json')


class Temp(DataMocker):
timestamp_param: Timestamp = Timestamp(Timestamp.D_TIMEE10, days=2)
str_param1: Str = Str(data_type="name")
str_param2: Str = Str(data_type="email")
int_param: Int = Int(byte_nums=6, unsigned=True)
customized_param: ProviderField = ProviderField(MyProvider().my_provider)
bool_param: Bool = Bool()
dict_param: Dict = Dict(
{
"a": Float(left=2, right=4, unsigned=True),
"b": Timestamp(Timestamp.D_TIMEE10, days=2)
}
)
user_list: List = List(
list_param: List = List(
[
Datetime(Datetime.D_FORMAT_YMD_T, weeks=2),
StrInt(byte_nums=6, unsigned=True)
]
)
built_in_provider_param1: ProviderField = ProviderField(
CyclicParameterProvider(parameter_list=["x", "y", "z"]).gen
)
built_in_provider_param2: ProviderField = ProviderField(
RandomFloatInRangeProvider(start=0.00, end=4.00, precision=4).gen
)
built_in_provider_param3: ProviderField = ProviderField(
IncrementalValueProvider(start=0, step=-2).gen
)
built_in_provider_param4: ProviderField = ProviderField(
RegexProvider(pattern=r"\d{3}-[a-z]{2}").gen
)
fixed_value_str = "fixed_value"
fixed_value_dict = {"key": "value"}

data = UserInfo().mock(num_entries=2, as_list=False, to_json=True, hook_set='SET1')
"""
e.g.
{"dda7d976-5094-4395-be92-306e7618ec48": {"create_time": 1718601558, "user_name": "Matthew Burke", "user_email": "aaronbrown@example.org", "user_age": 56, "user_status": "ACTIVE", "user_marriage": true, "user_dict": {"a": 5.1988, "b": 1718523595}, "user_list": ["2024-06-08T14:54:16", "44"]}, "c78f7896-f08c-414e-823b-8f173ab8259b": {"create_time": 1718685343, "user_name": "Dennis Collier", "user_email": "amy02@example.com", "user_age": 30, "user_status": "ACTIVE", "user_marriage": true, "user_dict": {"a": 55.2365, "b": 1718577918}, "user_list": ["2024-06-26T16:40:48", "43"]}}
"""
print(data)
def gen_data(self):
return self.mock(
num_entries=3,
as_list=False,
to_json=True,
hook_set='SET1',
key_generator=lambda: RegexProvider(pattern=r"^[A-Z]{4}-\d{3}$").gen()
)


if __name__ == '__main__':
print(Temp().gen_data())

```

Expand Down
9 changes: 8 additions & 1 deletion pistol_magazine/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@
from pistol_magazine import *
# Create a custom provider
@provider
class MyProvider:
def my_provider(self):
return choice(["ACTIVE", "INACTIVE"])
"""
Define hook functions
pre_generate: Executes operations before generating all data. Suitable for tasks like logging or starting external services.
after_generate: Executes operations after generating each data entry but before final processing. Suitable for tasks like data validation or conditional modifications.
final_generate: Executes operations after generating and processing all data entries. Suitable for final data processing, sending data to message queues, or performing statistical analysis.
"""
@hook('pre_generate', order=1, hook_set='SET1')
def pre_generate_first_hook():
print("Start Mocking User Data")
Expand Down Expand Up @@ -84,7 +91,7 @@ def gen_data(self):
)
def test_user_temp():
if __name__ == '__main__':
print(Temp().gen_data())
'''
Expand Down

0 comments on commit 9dc7717

Please sign in to comment.