Skip to main content

ID Schema

How to install it?

You can install ID Schema from this Github repository with python3 setup.py install, or just install it directly from pypi with pip3 install id-schema.

What is it?

ID Schema allows you to define your custom Schema, how an identifier string should look like. Each identifier string consists of one or more components, which are then concatenated into one large identifier string. Currently there are three components.

Components

RandomGroup

The RandomGroup component allows to generate a random sequence of a predefined alphabet with a predefined length. A length can either be a LengthRange or LengthSequence. A LengthRange has a minimum length and a maximum length. Valid LengthRanges are for example:

LengthRange(1) # always maps to 1
LengthRange(1, max_length=1) # always maps to 1
LengthRange(1, 3) # length range between 1 and 3, interval [1,3]
LengthRange(5,10) # interval [5,10]
LengthRange(10, max_length=1) # INVALID RANGE! , because min > max

A LengthSequence has a sequence of lengths that are allowed. Valid LengthSequences are for example:

LengthSequence(1) # always maps to 1
LengthSequence(1,2,3) # maps to lengths (1,2,3)
LengthSequence(1, 10, 20) # maps to lengths (1,10,20)
LengthSequence((1,10,20)) # also maps to lengths (1,10,20)

If you want the resulting random string sequence to contain only unique elements, you can use the keyword argument unique=True. The default is unique=False.

Here are some examples on how to use the RandomGroup component.

RandomGroup(alphabet="abcd", length=1)
RandomGroup(alphabet="abcd", length=LengthSequence(1,3))
RandomGroup(alphabet="abcd", length=LengthRange(1,5))
RandomGroup(alphabet="abcd", length=LengthSequence(1,3,5), unique=True)
RandomGroup(alphabet="ab", length=LengthRange(1,3), weights=[0.75,0.25]) # 'a' will occur 3/4 as much as 'b'

ExactLiteral

The ExactLiteral component allows to generate an exact literal sequence. The usage is very simple.

ExactLiteral("-") # will always generate '-'
ExactLiteral("abcd") # will always generate 'abcd'

OneOf

The OneOf component allows to generate exactly one other component based on a component subset. Specifying a pure string automatically transforms it to an ExactLiteral for you.

OneOf(components=(
	"gmail.com",
	"yahoo.com",
	"hotmail.com"
))
OneOf(components=(
	RandomGroup(alphabet="abcd", length=LengthRange(1,3)),
	RandomGroup(alphabet="efgh", length=LengthRange(1,3)),
), weights=(0.75, 0.25)) # first RandomGroup will occur 3/4 as much as second RandomGroup

ID Schema

To actually use these components, you have to create an IDSchema subclass. Just define your components under the Components class attribute and you are done.

from id_schema import OneOf, IDSchema, RandomGroup, ExactLiteral
import string

class EmailSchema(IDSchema):
	Components = (
		RandomGroup(alphabet=string.ascii_letter, length=LengthRange(1,20)),
		ExactLiteral("@"),
		OneOf(components=(
			"gmail.com",
			"yahoo.com",
			"hotmail.com"
		))
	)

class SerialNumberSchema(IDSchema):
	Components = (
		RandomGroup(alphabet=string.ascii_letter, length=5),
		ExactLiteral("-"),
		RandomGroup(alphabet=string.ascii_letter, length=LengthRange(5,10)),
		ExactLiteral("-"),
		RandomGroup(alphabet=string.ascii_letter, length=5),
	)

IDSchema exposes various functions:

  • generate_one() : generate one string from the Schema
  • generate_n(n): generate n strings from the same Schema
  • generate_n_unique(n, max_retry=20) : generate n unique strings from the same Schema. Fail after max_retry attempts.
  • validate(string, max_reductions=5000): validate if a given string matches the Schema (note that weights are not validated). DoS Protection implemented by max_reductions. Raises RuntimeError if max_reductions restriction hit.

Metadata

Release files for id-schema 1.0.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for id-schema 1.0.9
File Size Uploaded
id_schema-1.0.9.tar.gz 6.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for id-schema 1.0.9
File Interpreter ABI Platform
id_schema-1.0.9-py3-none-any.whl Python 3 none any Details

Total release size: 13.6 kB

Release files / id_schema-1.0.9.tar.gz

Download URL id_schema-1.0.9.tar.gz
Size 6.0 kB
Tags Source
SHA-256 checksum
How to use checksums
2641c02e65c5a1c7dbde858efc0172b336783bf946c30b1321b652429d90024d
BLAKE2b-256 checksum
How to use checksums
f4d51a1c07cda5640f6f9cb583c3792a54b99326a7657c0b78cbe7e5789699b6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.4

Release files / id_schema-1.0.9-py3-none-any.whl

Download URL id_schema-1.0.9-py3-none-any.whl
Size 7.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8442d29cfc487e8eca15a893e783051e8237d85846f7f08683144062f576ab40
BLAKE2b-256 checksum
How to use checksums
0dfd405201912d1e04924a944e93f2c69b1a4a0dc017374124335734ad0ec146
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.4

Release history Release notifications | RSS feed

This release

1.0.9 This release

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page