blahblah
Fake data generator for district42 schema
(!) Work in progress, breaking changes are possible until v2.0 is released
Installation
pip3 install blahblah
Usage
from blahblah import fake
from district42 import schema
UserSchema = schema.dict({
'id': schema.int.min(1),
'name': schema.str.regex(r"[a-z0-9_]+"),
'is_deleted': schema.bool,
})
print(fake(UserSchema))
Documentation
None
schema.none
sch = schema.none
assert fake(sch) is None
Bool
schema.bool
sch = schema.bool
assert fake(sch) in (True, False)
schema.bool(value)
sch = schema.bool(True)
assert fake(sch) is True
Int
schema.int
INT_MIN = -(2 ** 63)
INT_MAX = 2 ** 63 - 1
sch = schema.int
assert INT_MIN <= fake(sch) <= INT_MAX
schema.int(value)
sch = schema.int(42)
assert fake(sch) == 42
schema.int.min(value)
sch = schema.int.min(0)
assert 0 <= fake(sch) <= INT_MAX
schema.int.max(value)
sch = schema.int.max(0)
assert INT_MIN <= fake(sch) <= 0
Float
schema.float
sch = schema.float
assert isinstance(fake(sch), float)
schema.float(value)
sch = schema.float(3.14)
assert fake(sch) == 3.14
schema.float.min(value)
sch = schema.float.min(0.0)
assert fake(sch) >= 0.0
schema.float.max(value)
sch = schema.float.max(0.0)
assert fake(sch) <= 0.0
Str
schema.str
sch = schema.str
generated = fake(sch)
assert isinstance(generated, str)
schema.str.len(length)
sch = schema.str.len(10)
generated = fake(sch)
assert len(generated) == 10
schema.str.len(min_length, max_length)
sch = schema.str.len(1, ...)
generated = fake(sch)
assert len(generated) >= 1
sch = schema.str.len(..., 32)
generated = fake(sch)
assert len(generated) <= 32
sch = schema.str.len(1, 32)
generated = fake(sch)
assert 1 <= len(generated) <= 32
schema.str.alphabet(letters)
digits = "01234567890"
sch = schema.str.alphabet(digits)
generated = fake(sch)
assert all(x in digits for x in generated)
schema.str.contains(substr)
sch = schema.str.contains("@")
generated = fake(sch)
assert "@" in generated
schema.str.regex(pattern)
import re
sch = schema.str.regex(r"[a-z]+")
generated = fake(sch)
assert re.match(r"[a-z]+", generated)
List
schema.list
sch = schema.list
generated = fake(sch)
assert isinstance(generated, list)
schema.list(elements)
sch = schema.list([schema.int(1), schema.int(2)])
generated = fake(sch)
assert generated = [1, 2]
schema.list(type)
sch = schema.list(schema.int)
generated = fake(sch)
assert all(isinstance(x) for x in generated)
schema.list(type).len(length)
sch = schema.list(schema.int).len(3)
generated = fake(sch)
assert len(generated) == 3
schema.list(type).len(min_length, max_length)
sch = schema.list(schema.int).len(1, ...)
generated = fake(sch)
assert len(generated) >= 1
sch = schema.list(schema.int).len(..., 10)
generated = fake(sch)
assert len(generated) <= 10
sch = schema.list(schema.int).len(1, 10)
generated = fake(sch)
assert 1 <= len(generated) <= 10
Dict
schema.dict
sch = schema.dict
generated = fake(sch)
assert isinstance(generated, dict)
schema.dict(keys)
sch = schema.dict({
"id": schema.int,
"name": schema.str | schema.none,
optional("platform"): schema.str,
})
generated = fake(sch)
assert isinstance(generated["id"], int)
assert isinstance(generated["name"], (str, type(None))
assert generated.keys() == {"id", "name"}
Any
schema.any
sch = schema.any
generated = fake(sch)
assert isinstance(generated, object)
schema.any(*types)
sch = schema.any(schema.str, schema.int)
generated = fake(sch)
assert isinstance(generated, (str, int))
Custom Types
1. Declare Schema
from typing import Any
from uuid import UUID
from district42 import Props, SchemaVisitor, SchemaVisitorReturnType as ReturnType
from district42.types import Schema
from niltype import Nilable
class UUIDProps(Props):
@property
def value(self) -> Nilable[UUID]:
return self.get("value")
class UUIDSchema(Schema[UUIDProps]):
def __accept__(self, visitor: SchemaVisitor[ReturnType], **kwargs: Any) -> ReturnType:
return visitor.visit_uuid(self, **kwargs)
def __call__(self, /, value: UUID) -> "UUIDSchema":
return self.__class__(self.props.update(value=value))
2. Register Generator
from typing import Any
from uuid import UUID, uuid4
from blahblah import Generator
from niltype import Nil
class UUIDGenerator(Generator, extend=True):
def visit_uuid(self, schema: UUIDSchema, **kwargs: Any) -> UUID:
if schema.props.value is not Nil:
return schema.props.value
return uuid4()
3. Use
from blahblah import fake
from district42 import register_type, schema
register_type("uuid", UUIDSchema)
print(fake(schema.uuid))
# 0d9d188a-4f1f-4bce-ba6e-51ca3732900e
Full code available here: district42_exp_types/uuid
Metadata
Release files for blahblah 1.5.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 | |
|---|---|---|---|---|
| blahblah-1.5.0.tar.gz | 15.9 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| blahblah-1.5.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 35.7 kB
Release files / blahblah-1.5.0.tar.gz
| Download URL | blahblah-1.5.0.tar.gz |
|---|---|
| Size | 15.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b497219e17b29c63a22fc3a0d747e46b70c690f9ac7ce3a9fb28ef0506de2cad
|
|
BLAKE2b-256 checksum How to use checksums |
25e72f0e9841b87cc804746250f8c32eddf1c03568ee93c01712f28007246f3e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12
|
Release files / blahblah-1.5.0-py3-none-any.whl
| Download URL | blahblah-1.5.0-py3-none-any.whl |
|---|---|
| Size | 19.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9ecabcd18a855a0f1ab4fe76877813f21ad45b755982202af8ddf11a1911c3eb
|
|
BLAKE2b-256 checksum How to use checksums |
4afb44113feadc63ee42bbc373b1f872f11d08b90a115ea71894d5bd76520d5e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12
|
Security for blahblah 1.5.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.