class Person(Structure): _required = ['ssid'] name = String(pattern='[A-Za-z]+$', maxLength=8) ssid = String(minLength=3, pattern='[A-Za-z]+$') num = Integer(maximum=30, minimum=10, multiplesOf="dd", exclusiveMaximum=False) foo = StructureReference(a=String(), b=StructureReference(c=Number(minimum=10), d=Number(maximum=10)))
class Example(Structure): _additionalProperties = True _required = [] # array support, similar to json schema a = Array(uniqueItems=True, minItems=3, items=[String(), Number(maximum=10)]) b = Array(minItems=3, maxItems=5, items=Number(maximum=10)) c = Array(items=[String(), String(), Number(maximum=10)]) d = Array(minItems=2, items='') e = Array(minItems=2) f = Array[Integer] g = Array[Foo] h = Array[Array[Integer]]
class Example(Structure): _additionalProperties = True _required = [] # deque support, similar to json schema a = Deque(uniqueItems=True, minItems=3, items=[String(), Number(maximum=10)]) b = Deque(minItems=3, maxItems=5, items=Number(maximum=10)) c = Deque(items=[String(), String(), Number(maximum=10)]) d = Deque(minItems=2, items='') e = Deque(minItems=2) f = Deque[Integer] g = Deque[Foo] h = Deque[Deque[Integer]] i = ImmutableDeque[String]
def test_field_declaration_bad_usage(): Name = String() class Foo(Structure): i = Integer first_name_foo = Name last_name_foo = Name _additionalProperties = False # Note that here Names points to a single instance of field,so when Bar uses it, it "hijacks" it from Foo. class Bar(Structure): first_name_bar = Name last_name_bar = Name i = Integer _additionalProperties = False with raises(KeyError) as ex: Foo.first_name_foo assert "last_name_bar" in str(ex.value) # this can cause a lot of weirdness.... bar = Bar(i=1, first_name_bar="jack", last_name_bar="smith") assert bar.first_name_bar == bar.last_name_bar # and even: with raises(KeyError) as ex: Foo(i=1, first_name_foo="Tom", last_name_foo="Jones") assert "last_name_bar" in str(ex.value)
class Example(Structure): _required = [] #set support, similar to Array b = Set(minItems=3, maxItems=5, items=Number(maximum=10)) d = Set(minItems=2, items=String) e = Set(minItems=2) f = Set[Integer] g = Set[AnyOf(fields=[String(minLength=3), Number(minimum=10)])]
class Example(Structure): _required = [] a = Integer b = String c = Array[String(minLength=3, pattern='[A-Za-z]+$')] d = Array[Integer] e = Array[Integer, String] f = Array(items=[String, Integer])
class Example(Structure): i = Integer(maximum=10) s = String(maxLength=5) array = Array[Integer(multiplesOf=5), Number] embedded = StructureReference(a1=Integer(), a2=Float()) simplestruct = SimpleStruct all = AllOf[Number, Integer] enum = Enum(values=[1, 2, 3])
class Trade(Structure): notional: DecimalNumber(maximum=10000, minimum=0) quantity: PositiveInt(maximum=100000, multiplesOf=5) symbol: String(pattern='[A-Z]+$', maxLength=6) buyer: Trader seller: Trader venue: Enum[Venue] comment: String _optional = ["comment", "venue"]
class Example(Structure): i = Integer(maximum=10) s = String(maxLength=5) a = Array[Integer(multiplesOf=5), Number] foo = StructureReference(a1=Integer(), a2=Float()) ss = SimpleStruct all = AllOf[Number, Integer] any = AnyOf[Number(minimum=1), Integer] one = OneOf[Number(minimum=1), Integer] no = NotField(fields=[String]) enum = Enum(values=[1, 2, 3])
class Example(Structure): _required = [] # standard definition: limits on size. key is a number<=10, value is a string a = Map(minItems=3, maxItems=5, items=[Number(maximum=10), String()]) # a slightly simplified representation - not that String has no '()' b = Map(items=(Number(maximum=10), String)) # Limits on size. Key and value can be anything c = Map(minItems=3, maxItems=5) # terse, Java-generics like: Key is String of size>=3, value is a number d = Map[String(minLength=3), Number] # terse, Java-generics like, representation: Key is string, value is a number e = Map[String, Number] #Key is string, value can be anything f = Map[String, Field]
class ImmutableExample(ImmutableStructure): i = Integer s = String anything = Anything arr = Array[String(maxLength=10)] arr2 = Array[Foo] map1 = Map[String, Array[Integer]] map2 = Map[String, Foo] bar = Set enum_arr = Array[Enum[Values]] date = DateField(date_format="%y%m%d") _required = []
class Example(Structure): i = Integer s = String anything = Anything arr = Array[String(maxLength=10)] arr2 = Array[Foo] map1 = Map[String, Array[Integer]] map2 = Map[String, Foo] bar = Set enum_arr = Array[Enum[Values]] date = DateField(date_format="%y%m%d") points = Array[Point] deq = Deque[Integer] _required = []
class Example(Structure): anything = Anything i = Integer(maximum=10) s = String(maxLength=5) any = AnyOf[Array[Person], Person] complex_allof = AllOf[ AnyOf[Integer, Person], BigPerson] # this is stupid, but we do it for testing people = Array[Person] array_of_one_of = Array[OneOf[ Float, Integer, Person, StructureReference(a1=Integer(), a2=Float())]] array = Array[Integer(multiplesOf=5), OneOf[Array[Person], Number]] embedded = StructureReference(a1=Integer(), a2=Float()) simplestruct = SimpleStruct all = AllOf[Number, Integer] enum = Enum(values=[1, 2, 3]) _required = []
def test_bad_items_definition_err(): with raises(TypeError) as excinfo: Tuple(items=String()) assert "Expected a list/tuple of Fields" in str(excinfo.value)
class Trade(Structure): _additionalProperties = True _required = ['tradable'] tradable = String() # class referece: to another Structure person = Person
def TableName(): return String(minLength=3)
class Foo(Structure): a = Integer b = Array[NotField[String(maxLength=3), Integer(minimum=4)]]
class Foo(Structure): a = Integer b = Array[NotField[String(minLength=3), String(maxLength=5), Integer]]
class Foo(Structure): a = Integer b = Array[OneOf[String(minLength=3), String(maxLength=5), Integer]]
def MyField(i) -> Field: return String(minLength=1)
def TableName() -> Field: return String(minLength=5)
def Names(): return Array(items=String(minLength=2))
class MixedTypesExample(Structure): i: Integer(maximum=10) s: String(maxLength=5) s1: str a: dict simple: SimpleStruct
class B(Structure): s = String(maxLength=5, minLength=2) a = ImmutableString() _required = []
class Person(Structure): name = String ssid = String(minLength=3)
class SimpleStruct(Structure): name = String(pattern='[A-Za-z]+$', maxLength=8)
def MyField() -> Field: return String()
class B(Structure): s = String(maxLength=5, minLength=2) a = ImmutableString(default='xyz') _required = []
class Trader(Structure): lei: String(pattern='[0-9A-Z]{18}[0-9]{2}$') alias: String(maxLength=32)