How to use the pystachio.container.Map function in pystachio

To help you get started, we’ve selected a few pystachio examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github wickman / pystachio / tests / test_container_types.py View on Github external
def test_hashing():
  map = {
    List(Integer)([1,2,3]): 'foo',
    Map(String,Integer)({'a': 1, 'b': 2}): 'bar'
  }
  assert List(Integer)([1,2,3]) in map
  assert Map(String,Integer)({'a': 1, 'b': 2}) in map
  assert List(Integer)([3,2,1]) not in map
  assert Map(String,Integer)({'a': 2, 'b': 1}) not in map
github wickman / pystachio / tests / test_container_types.py View on Github external
def test_basic_maps():
  assert Map(String,Integer)({}).check().ok()
  assert Map(String,Integer)({'a':1}).check().ok()
  assert Map(String,Integer)(('a', 1)).check().ok()
  assert Map(String,Integer)(('a', 1), ('b', 2)).check().ok()
  assert not Map(String,Integer)({'a':'{{foo}}'}).check().ok()
  assert not Map(Integer,String)({'{{foo}}':'a'}).check().ok()
  assert Map(String,Integer)({'a':'{{foo}}'}).bind(foo = 5).check().ok()
  with pytest.raises(TypeError):
    Map(String,Integer)(a = 1)
  with pytest.raises(ValueError):
    Map(String,Integer)({'a': 1}, {'b': 2})
  for value in [None, type, 123, 'a']:
    with pytest.raises(ValueError):
      Map(String,Integer)(value)
  repr(Map(String,Integer)(('a', 1), ('b', 2)))
github wickman / pystachio / tests / test_container_types.py View on Github external
def test_basic_maps():
  assert Map(String,Integer)({}).check().ok()
  assert Map(String,Integer)({'a':1}).check().ok()
  assert Map(String,Integer)(('a', 1)).check().ok()
  assert Map(String,Integer)(('a', 1), ('b', 2)).check().ok()
  assert not Map(String,Integer)({'a':'{{foo}}'}).check().ok()
  assert not Map(Integer,String)({'{{foo}}':'a'}).check().ok()
  assert Map(String,Integer)({'a':'{{foo}}'}).bind(foo = 5).check().ok()
  with pytest.raises(TypeError):
    Map(String,Integer)(a = 1)
  with pytest.raises(ValueError):
    Map(String,Integer)({'a': 1}, {'b': 2})
  for value in [None, type, 123, 'a']:
    with pytest.raises(ValueError):
      Map(String,Integer)(value)
  repr(Map(String,Integer)(('a', 1), ('b', 2)))
github wickman / pystachio / tests / test_composite_types.py View on Github external
def test_find():
  class Resources(Struct):
    cpu = Required(Float)
    ram = Integer
    disks = List(String)

  class Process(Struct):
    name = Required(String)
    resources = Map(String, Resources)

  res0 = Resources(cpu = 0.0, ram = 0)
  res1 = Resources(cpu = 1.0, ram = 1, disks = ['hda3'])
  res2 = Resources(cpu = 2.0, ram = 2, disks = ['hda3', 'hdb3'])
  proc = Process(name = "hello", resources = {
    'res0': res0,
    'res1': res1,
    'res2': res2
  })

  with pytest.raises(Namable.NotFound):
    proc.find(ref('herp'))

  assert proc.find(ref('name')) == String('hello')
  assert proc.find(ref('resources[res0].cpu')) == Float(0.0)
  assert proc.find(ref('resources[res0].ram')) == Integer(0)
github wickman / pystachio / tests / test_container_types.py View on Github external
def test_map_keys_that_should_improve():
  mi = Map(String, Integer)()
  for key in [{2: "hello"}, String, Integer, type]:
    with pytest.raises(KeyError):
      mi[key]
    assert key not in mi
github wickman / pystachio / tests / test_container_types.py View on Github external
def test_map_idioms():
  mi = Map(String,Integer)({'a': 1, 'b': 2})
  for key in ['a', String('a')]:
    assert mi[key] == Integer(1)
    assert key in mi
  for key in ['b', String('b')]:
    assert mi[key] == Integer(2)
    assert key in mi
  for key in [1, 'c', String('c')]:
    with pytest.raises(KeyError):
      mi[key]
    assert key not in mi
github wickman / pystachio / tests / test_container_types.py View on Github external
def test_map_iteration():
  mi = Map(String,Integer)({'a': 1, 'b': 2})
  miter = iter(mi)
  assert next(miter) in (String('a'), String('b'))
  assert next(miter) in (String('a'), String('b'))
  with pytest.raises(StopIteration):
    next(miter)

  mi = Map(String,Integer)({})
  with pytest.raises(StopIteration):
    next(iter(mi))
github wickman / pystachio / tests / test_container_types.py View on Github external
def test_map_find():
  msi = Map(String,Integer)({'a':1})
  assert msi.find(ref('[a]')) == Integer(1)
  with pytest.raises(Namable.NamingError):
    msi.find(ref('.a'))
  with pytest.raises(Namable.NotFound):
    msi.find(ref('[b]'))
  with pytest.raises(Namable.Unnamable):
    msi.find(ref('[a].foo'))

  mii = Map(Integer,String)({3: 'foo', '5': 'bar'})
  assert mii.find(ref('[3]')) == String('foo')
  assert mii.find(ref('[5]')) == String('bar')
github wickman / pystachio / tests / test_composite_types.py View on Github external
def test_composite_interpolation():
  class Resources(Struct):
    cpu = Required(Float)
    ram = Integer
    disk = Integer

  class Process(Struct):
    name = Required(String)
    resources = Map(String, Resources)

  p = Process(name = "hello")
  assert p(resources = {'foo': Resources()}) == \
         p(resources = {'{{whee}}': Resources()}).bind(whee='foo')
  assert p(resources = {'{{whee}}': Resources(cpu='{{whee}}')}).bind(whee=1.0) == \
         p(resources = {'1.0': Resources(cpu=1.0)})
github wickman / pystachio / tests / test_composite_types.py View on Github external
def test_getattr_functions():
  class Resources(Struct):
    cpu = Required(Float)
    ram = Integer
    disk = Integer

  class Process(Struct):
    name = Required(String)
    resources = Map(String, Resources)

  # Basic getattr + hasattr
  assert Process(name = "hello").name() == String('hello')
  assert Process().has_name() is False
  assert Process(name = "hello").has_name() is True


  p = Process(name = "hello")
  p1 = p(resources = {'foo': Resources()})
  p2 = p(resources = {'{{whee}}': Resources()}).bind(whee='foo')

  assert p1.has_resources()
  assert p2.has_resources()
  assert String('foo') in p1.resources()
  assert String('foo') in p2.resources()