Overview

HTTPS SSH

Requirements

  • Python 2.6 or later.

Install

$ pip install tesdat

Usage

Basic Example

>>> import tesdat
>>> model = tesdat.Model({
...     'id': tesdat.IncrementPattern(),
...     'x': tesdat.CyclePattern(['a', 'b', 'c']),
...     # Blank will be omit.
...     'option': tesdat.ChoicePattern(['ok', 'ng', tesdat.Blank]),
... })
>>> # render 5times.
>>> container = tesdat.ListContainer(model, 5)
>>> container.render()
[
 {'id': 1, 'x': 'a', 'option': 'ng'},
 {'id': 2, 'x': 'b', 'option': 'ok'},
 {'id': 3, 'x': 'c'},
 {'id': 4, 'x': 'a'},
 {'id': 5, 'x': 'b', 'option': 'ng'}
]
>>> container.output('test.json')  # output as json(default)

TSV Example

>>> import tesdat
>>> model = tesdat.Model([
...     tesdat.IncrementPattern(start=10, step=5, callback=str),
...     tesdat.HashPattern(2, 'md5'),  # hashing value of the third column.
...     tesdat.ChoicePattern(['foo', 'bar', 'baz']),
...     tesdat.CyclePattern(range(0, 30, 10), callback=str),
... ])

>>> # render in the order that specified in the arguments.
>>> model.ordering(2)
>>> container = tesdat.ListContainer(model)
>>> # repeat count can be specified as an argument to the render-method.
>>> container.render(10)  # repeat 10 times.
[[somedata...]...]
>>> def add_label(rows):
...     rows.insert(0, ['id', 'hash-of-name', 'name', 'value'])
...     return rows

>>> def convert_tsv(rows):
...     return '\n'.join([
...         '\t'.join(cols) for cols in rows
...     ])

>>> container.set_shape(add_label)
>>> container.set_stringify(convert_tsv)
>>> container.output('test.tsv', rewrite=True)  # rewrite if test.tsv exists.

Custom Example

if object is callable, store execution result.

Model

>>> import tesdat
>>> def square(i):
...    return i ** 2

>>> container = tesdat.DictContainer(square)
>>> container.render(['a', 'b', 'c', 'd', 'e'])
{'a': 0, 'b': 1, 'c': 4, 'd': 9, 'e': 16}

Pattern

>>> import tesdat
>>> model = tesdat.Model({
...    'col1': (lambda r, i: i),
...    'col2': (lambda r: r['col1'] + 1),
...    'col3': (lambda r: r['col2'] * 2),
...    'col4': 100,  # fixed value
... }).ordering('col1', 'col2', 'col3')

>>> container = tesdat.ListContainer(model)
>>> container.render(3)
[
 {'col1': 0, 'col2': 1, 'col3': 2, 'col4': 100},
 {'col1': 1, 'col2': 2, 'col3': 4, 'col4': 100},
 {'col1': 2, 'col2': 3, 'col3': 6, 'col4': 100},
]

Limited number of element Example

>>> import tesdat
>>> model = tesdat.Model({
...     # x: a is 1times limited. / b is 2times limited. / c is 3times limited.
...     'x': tesdat.PickoutPattern({'a': 1, 'b': 2, 'c': 3}, missing=None),
...     # y: a is 2times limited. / b and c is 1times limited.
...     'y': tesdat.PickoutPattern(['a', 'a', 'b', 'c'], missing='*'),
...     # z: a and b can't be selected. / c is 5times limited.
...     'z': tesdat.PickoutPattern(['c']*5, missing=None),
... })

>>> container = tesdat.ListContainer(model)
>>> # render the missing-argument value if choices does not exist.
>>> container.render(6)
[
 {'x': 'c', 'y': 'b', 'z': 'c'},
 {'x': 'b', 'y': 'c', 'z': 'c'},
 {'x': 'c', 'y': 'a', 'z': 'c'},
 {'x': 'a', 'y': 'a', 'z': 'c'},
 {'x': 'c', 'y': '*', 'z': 'c'},
 {'x': 'b', 'y': '*', 'z': None},
]

Combination Example

To generate the testdata that combines multiple elements can be achieved by using the repeat-argument of CyclePattern and SequencePattern.

>>> import tesdat
>>> l0 = ['a', 'b']
>>> l1 = ['a', 'b', 'c']
>>> l2 = ['a', 'b', 'c', 'd']
>>> model = tesdat.Model([
...     tesdat.SequencePattern(l0, repeat=len(l1)*len(l2), missing=tesdat.Break),
...     tesdat.CyclePattern(l1, repeat=len(l2)),
...     tesdat.CyclePattern(l2),
... ])

>>> # Container is an alias of ListContainer
>>> container = tesdat.Container(model)
>>> # by specifying the Break to missing-argument
>>> # automatically detect the end of elements and escape before reaching the 10000.
>>> container.render(10000)
[
 ['a', 'a', 'a'],
 ['a', 'a', 'b'],
 ['a', 'a', 'c'],
 ['a', 'a', 'd'],
 ['a', 'b', 'a'],
 ['a', 'b', 'b'],
 ['a', 'b', 'c'],
 ['a', 'b', 'd'],
 ['a', 'c', 'a'],
 ['a', 'c', 'b'],
 ['a', 'c', 'c'],
 ['a', 'c', 'd'],
 ['b', 'a', 'a'],
 ['b', 'a', 'b'],
 ['b', 'a', 'c'],
 ['b', 'a', 'd'],
 ['b', 'b', 'a'],
 ['b', 'b', 'b'],
 ['b', 'b', 'c'],
 ['b', 'b', 'd'],
 ['b', 'c', 'a'],
 ['b', 'c', 'b'],
 ['b', 'c', 'c'],
 ['b', 'c', 'd'],
]

History

2.0.x

  • Add Ordered

  • Some of the features have been removed or modified.

    • interval-argument renamed to repeat-argument.(CyclePattern, SequencePattern)

    • HashPattern renamed to HashOfPattern.

    • *Container.repeat was removed. please use the following way instead of that.

      • second argument of *Container.__init__
      • first argument of *Container.render
    • OrderedDictContainer named ODictContainer as alias.

    • charset-argument renamed to encoding-argument.

1.6.x

  • Added Blank constant(class) for omitting.

1.5.x

  • When caught StopIteration, escape the rendering normally.

  • Added Break exception. (inherit StopIteration)

    • If missing-argument received Break, detect end the rendering and throw Break exception.
  • If specified iterator object as custom pattern(or model), automatically next() method is called.

  • Support Python 3.x.

  • Added charset argument to container.output.

  • Added deepcopy-flag argument to container.output, container.stringify and container.shape.

1.4.x

  • Support negative index in model.ordering(ListModel only).

  • Added __repr__ to all patterns.

  • Updated pattern-name.

    • Define SeqPattern as an alias of SequencePattern.

    • Define IncPattern as an alias of IncrementPattern.

    • Change LimitedChoicePattern to PickoutPattern.

      • LimitedChoicePattern became the alias of PickoutPattern.
  • Does not guarantee the operation of python2.5.

1.3.x

  • Added interval-argument to SequencePattern and CyclePattern.

    • interval-argument is intervals up proceeds to the next element.(interval > 0, default:1)
  • Added Example to take advantage of the interval-argument.

1.2.x

  • Added SequencePattern.(fixed that it can be accessed from the top level module)
  • Added OrderedDictContainer.(python >= 2.7)
  • ListContainer defined Container as alias.
  • Fixed the problem that LimitedChoicePattern.missing-argument is affected from callback.

1.1.x

  • Added LimitedChoicePattern.
  • Added Examples.

1.0.x

  • First release.