Package: id-schema
ID SchemaHow to install it?You can installID Schemafrom this Github repository withpython3 setup.py install,
or just install it directly from pypi withpip3 install id-schema.What is it?ID Schemaallows you to define your custom Schema, how an identifier string should look like. Each identifier string consists of one or morecomponents, which are then concatenated into one large identifier string.
Currently there are three components.ComponentsRandomGroupTheRandomGroupcomponent allows to generate a random sequence of a predefined alphabet
with a predefined length. A length can either be aLengthRangeorLengthSequence.
ALengthRangehas a minimum length and a maximum length. ValidLengthRanges are for example:LengthRange(1)# always maps to 1LengthRange(1,max_length=1)# always maps to 1LengthRange(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 > maxALengthSequencehas a sequence of lengths that are allowed. ValidLengthSequences are for example:LengthSequence(1)# always maps to 1LengthSequence(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 argumentunique=True. The default isunique=False.Here are some examples on how to use theRandomGroupcomponent.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'ExactLiteralTheExactLiteralcomponent allows to generate an exact literal sequence. The usage is very simple.ExactLiteral("-")# will always generate '-'ExactLiteral("abcd")# will always generate 'abcd'OneOfTheOneOfcomponent allows to generate exactly one other component based on a component subset. Specifying a pure string automatically transforms it to anExactLiteralfor 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 RandomGroupID SchemaTo actually use these components, you have to create anIDSchemasubclass. Just define your components under theComponentsclass attribute and you are done.fromid_schemaimportOneOf,IDSchema,RandomGroup,ExactLiteralimportstringclassEmailSchema(IDSchema):Components=(RandomGroup(alphabet=string.ascii_letter,length=LengthRange(1,20)),ExactLiteral("@"),OneOf(components=("gmail.com","yahoo.com","hotmail.com")))classSerialNumberSchema(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),)IDSchemaexposes various functions:generate_one(): generate one string from the Schemagenerate_n(n): generate n strings from the same Schemagenerate_n_unique(n, max_retry=20): generate n unique strings from the same Schema. Fail aftermax_retryattempts.validate(string, max_reductions=5000): validate if a given string matches the Schema (note thatweightsare not validated). DoS Protection implemented bymax_reductions. RaisesRuntimeErrorifmax_reductionsrestriction hit.