Faker Events
Generates Events with formatted fake data for streams. The intention is for development and testing purposes without relying on real data.
Usage
Faker Events is a package that doesn't come with a CLI. This is in part due to the Events you create being written in Python as Dictionaries, and processed using a function referred to as the profiler.
The Faker package is utilised to generate the data on the profiles. Understanding how Faker works is recommended and you can find the documentation for it here.
Beyond the profiles though for the custom event types any python data generation software can be used.
Installation
By default faker-events simply prints to standard out. To use a stream, give the option when installing.
Kafka
pip install faker-events[kafka]
Kinesis
pip install faker-events[kinesis]
Starting a Stream
Create an Event Generator and start using Live Stream. By default only 10 profiles are created. Giving large numbers can take sometime to build.
Set the "Events Per Minute" on the live_stream method to change the maximum allowed, but subject to system performance also. The default is ~60 per minute, but they are random so expect potentially lower rates.
Standard Output
import faker_events
eg = faker_events.EventGenerator(num_profiles=100)
eg.live_stream(epm=120)
Output
{"type": "example", "event_time": "2021-01-14T19:10:02.678866", "event_id": 1, "first_name": "John", "last_name": "Harris"}
{"type": "example", "event_time": "2021-01-14T19:10:03.468144", "event_id": 2, "first_name": "Robert", "last_name": "Lane"}
{"type": "example", "event_time": "2021-01-14T19:10:04.270969", "event_id": 3, "first_name": "Michelle", "last_name": "Clayton"}
{"type": "example", "event_time": "2021-01-14T19:10:04.888072", "event_id": 4, "first_name": "Robert", "last_name": "Lane"}
{"type": "example", "event_time": "2021-01-14T19:10:05.446477", "event_id": 5, "first_name": "Andrew", "last_name": "Oconnor"}
^C
Stopping Event Stream
If you want to see a demo of this without writing code, run faker_events as a module from the command line.
python -m faker_events
Using Stream Handlers
Once you have installed Faker Events with the Stream type you want you can now use a stream handler to send the JSON messages to Kakfa, or Kinesis.
Kafka
import faker_events
example = faker_events.Stream(stype='kafka', host='kafka:9092', name='example')
eg = faker_events.EventGenerator(stream=example)
eg.live_stream()
Kinesis
import faker_events
example = faker_events.Stream(stype='kinesis', name='example', key='key')
eg = faker_events.EventGenerator(stream=example)
eg.live_stream()
Starting a Batch
Create an Event Generator and use the batch method, with a start and finish datetime object, and the frequncy like on the live stream.
from datetime import datetime, timedelta
import faker_events
eg = faker_events.EventGenerator(num_profiles=1)
start = datetime(2019, 1, 1) # No one wants to relive 2020...
finish = start + timedelta(seconds=10)
eg.batch(start, finish, epm=10)
Data Points
Event Data Points
The Event Type has some basic data points about the event that can be used within the profiled method. (Access the Attribute using the event within the profiler)
- event_id - The id of the particular event
- event_time - The time the event occured (ISO Format)
Profile Data Points
When you create the Event Generator, the profiles you will use in the events are created with a number of data points. Below is a list of attributes that can be used on the 'profile' within the Event Profiler function.
- id
- uuid
- username
- gender
- first_name
- last_name
- prefix_name
- suffix_name
- birthdate
- blood_group
- employer
- job
- full_address1
- building_number1
- street_name1
- street_suffix1
- state1
- postcode1
- city1
- phone1
- full_address2
- building_number2
- street_name2
- street_suffix2
- state2
- postcode2
- city2
- phone2
- driver_license
- license_plate
Profiling Events
Creating an Event is as easy as just creating a dictionary that is passed into the Event Class. The Event Instance is then just set on the Event Generator, and you can then use the 'create_events' method which will return a generator, or us the 'live_stream' or 'batch' methods that will handle the generator.
If you want event values to be dynamic, create a profiler functions. The function should take two arguments; event and profile. These carry the attributes listed above into the function for updating event values, or even creating new key value pairs.
Update the event yourself by using 'event.data', which contains the dictionary passed into the Event Class. The other option is to return a dictionary with the key value pairs you want to update. The Event instance will handle updating the values.
The profile is a randomly selected profile from the profiles created by the Event Generator. You can use details from the profile to build our events that simulate customers, or entities.
from faker import Faker
from faker_events import Event, EventGenerator
fake = Faker()
event = {
'Fixed': 'Doesnt Change',
'Once': fake.color(),
'Always': '',
'Profiled': '',
}
def profiler(event, profile):
return {
'Always': fake.boolean(),
'Profiled': profile.email,
}
eg = EventGenerator(num_profiles=2)
eg.events = Event(event, profiler)
eg.live_stream()
Event Sequences
You can sequence the events by setting the next event to occur, and occurence on how many times it will happen. If no limit is set, the next Event Type will never be used.
Either the 'next' attribute can be set with a statement, or the bitwise operator can be use to set the next event.
from faker_events import Event, EventGenerator
eg = EventGenerator(num_profiles=1)
a = Event({'Name': 'A'}, limit=1)
b = Event({'Name': 'B'}, limit=2)
c = Event({'Name': 'C'}, limit=1)
eg.events = a
a.next = b
b.next = c
# Short form:
# eg.events = a >> b >> c
eg.live_stream()
Output
{"Name": "A"}
{"Name": "B"}
{"Name": "B"}
{"Name": "C"}
Event limited reached. 4 in total generated
Persistant State
If you need to update the details of the profile, or add persistant data from the events you can do so within the Profiled method of the Event instance. When using sequenced events, the profile can be used to retrieve the data from previous events.
from faker_events import EventGenerator, Event
eg = EventGenerator(num_profiles=1)
event_a = {'Name': 'A', 'LastEvent': 'none'}
def profiler_a(event, profile):
profile.LastEvent = 'EventA'
event_b = {'Name': 'B', 'LastEvent': 'none'}
def profiler_b(event, profile):
event.data['LastEvent'] = profile.LastEvent
profile.LastEvent = 'EventB'
event_c = {'Name': 'C', 'LastEvent': 'none'}
def profiler_c(event, profile):
event.data['LastEvent'] = profile.LastEvent
a = Event(event_a, profiler_a, 1)
b = Event(event_b, profiler_b, 1)
c = Event(event_c, profiler_c, 1)
eg.events = a >> b >> c
eg.live_stream()
Output
{"Name": "A", "LastEvent": "none"}
{"Name": "B", "LastEvent": "EventA"}
{"Name": "C", "LastEvent": "EventB"}
Event limit reached. 3 in total generated
Multiple Event Flows
By grouping the events in lists, the Event Generator is able to work through multiple Event Flows for each profile created, creating complex event streams.
from faker_events import Event, EventGenerator
eg = EventGenerator(1)
flow_a1 = Event({"Name": "A1"}, limit=1)
flow_aa1 = Event({"Name": "AA1"}, limit=1)
flow_aa2 = Event({"Name": "AA2"}, limit=1)
flow_b1 = Event({"Name": "B1"}, limit=1)
flow_bb1 = Event({"Name": "BB1"}, limit=1)
flow_bb2 = Event({"Name": "BB2"}, limit=1)
eg.events = [flow_a1, flow_b1]
flow_a1 >> [flow_aa1, flow_aa2]
flow_b1 >> [flow_bb1, flow_bb2]
eg.live_stream()
Output
{"Name": "B1"}
{"Name": "BB2"}
{"Name": "A1"}
{"Name": "AA1"}
{"Name": "AA2"}
{"Name": "BB1"}
Event limit reached. 6 in total generated
License
Faker-Events is released under the MIT License. See the bundled LICENSE file for details.
Credits
Metadata
Release files for faker-events 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| Faker-Events-1.1.0.tar.gz | 9.8 kB | Without provenance | Details for Faker-Events-1.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| Faker_Events-1.1.0-py3-none-any.whl | Python 3 | none | any | Details for Faker_Events-1.1.0-py3-none-any.whl |
Total release size: 20.8 kB
Release files / Faker-Events-1.1.0.tar.gz
| Download URL | Faker-Events-1.1.0.tar.gz |
|---|---|
| Size | 9.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
08cf6c023ad9a67be7f21529b958b982027e5e5865896690085a1c2fa3ffec67
|
|
BLAKE2b-256 checksum How to use checksums |
a9487a1395b19d1c1dfcbcd6f3cf4fbee249120215a32a4b321674843fd6f36e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
|
Release files / Faker_Events-1.1.0-py3-none-any.whl
| Download URL | Faker_Events-1.1.0-py3-none-any.whl |
|---|---|
| Size | 10.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
28c80e0b7358ceb15161c797ae073c964e7a21e1e0d7f75be73d117349611676
|
|
BLAKE2b-256 checksum How to use checksums |
7314b13e8bbcd0dc8a17e9a7d249fa55cdcfc3f7880647fe983b39522b668bf0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
|
Security for faker-events 1.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.