Exemplo n.º 1
0
def recordtree(table, start='start', stop='stop'):
    """
    Construct an interval tree for the given table, where each node in the tree is a row of the table represented
    as a hybrid tuple/dictionary-style record object.

    """

    try:
        import bx.intervals
    except ImportError as e:
        raise UnsatisfiedDependency(e, dep_message)

    getstart = attrgetter(start)
    getstop = attrgetter(stop)

    tree = bx.intervals.intersection.IntervalTree()
    for rec in records(table):
        tree.add(getstart(rec), getstop(rec), rec)
Exemplo n.º 2
0
def recordtree(table, start='start', stop='stop'):
    """
    Construct an interval tree for the given table, where each node in the tree is a row of the table represented
    as a hybrid tuple/dictionary-style record object.

    """

    try:
        import bx.intervals
    except ImportError as e:
        raise UnsatisfiedDependency(e, dep_message)

    getstart = attrgetter(start)
    getstop = attrgetter(stop)

    tree = bx.intervals.intersection.IntervalTree()
    for rec in records(table):
        tree.add(getstart(rec), getstop(rec), rec)
Exemplo n.º 3
0
def test_records_unevenrows():
    table = (('foo', 'bar'), ('a', 1, True), ('b',))
    actual = records(table)
    # access items
    it = iter(actual)
    o = it.next()
    eq_('a', o['foo'])
    eq_(1, o['bar'])
    o = it.next()
    eq_('b', o['foo'])
    eq_(None, o['bar'])
    # access attributes
    it = iter(actual)
    o = it.next()
    eq_('a', o.foo)
    eq_(1, o.bar)
    o = it.next()
    eq_('b', o.foo)
    eq_(None, o.bar)
Exemplo n.º 4
0
def test_records():
    table = (('foo', 'bar'), ('a', 1), ('b', 2))
    actual = records(table)
    # access items
    it = iter(actual)
    o = it.next()
    eq_('a', o['foo'])
    eq_(1, o['bar'])
    o = it.next()
    eq_('b', o['foo'])
    eq_(2, o['bar'])
    # access attributes
    it = iter(actual)
    o = it.next()
    eq_('a', o.foo)
    eq_(1, o.bar)
    o = it.next()
    eq_('b', o.foo)
    eq_(2, o.bar)
Exemplo n.º 5
0
def recordtrees(table, facet, start='start', stop='stop'):
    """
    Construct faceted interval trees for the given table, where each node in the tree is a row of the table represented
    as a hybrid tuple/dictionary-style record object.

    """

    try:
        import bx.intervals
    except ImportError as e:
        raise UnsatisfiedDependency(e, dep_message)

    getstart = attrgetter(start)
    getstop = attrgetter(stop)
    getkey = attrgetter(facet)

    trees = dict()
    for rec in records(table):
        k = getkey(rec)
        if k not in trees:
            trees[k] = bx.intervals.intersection.IntervalTree()
        trees[k].add(getstart(rec), getstop(rec), rec)
    return trees
Exemplo n.º 6
0
def recordtrees(table, facet, start='start', stop='stop'):
    """
    Construct faceted interval trees for the given table, where each node in the tree is a row of the table represented
    as a hybrid tuple/dictionary-style record object.

    """

    try:
        import bx.intervals
    except ImportError as e:
        raise UnsatisfiedDependency(e, dep_message)

    getstart = attrgetter(start)
    getstop = attrgetter(stop)
    getkey = attrgetter(facet)

    trees = dict()
    for rec in records(table):
        k = getkey(rec)
        if k not in trees:
            trees[k] = bx.intervals.intersection.IntervalTree()
        trees[k].add(getstart(rec), getstop(rec), rec)
    return trees
Exemplo n.º 7
0
Arquivo: io.py Projeto: deytao/petl
def tojson(table, source=None, *args, **kwargs):
    """
    Write a table in JSON format. E.g.::

        >>> from petl import tojson, look    
        >>> look(table)
        +-------+-------+
        | 'foo' | 'bar' |
        +=======+=======+
        | 'a'   | 1     |
        +-------+-------+
        | 'b'   | 2     |
        +-------+-------+
        | 'c'   | 2     |
        +-------+-------+
        
        >>> tojson(table, 'example.json')
        >>> # check what it did
        ... import json
        >>> with open('example.json') as f:
        ...     json.load(f)
        ... 
        [{u'foo': u'a', u'bar': 1}, {u'foo': u'b', u'bar': 2}, {u'foo': u'c', u'bar': 2}]
    
    Note that this is currently not streaming, all data is loaded into memory
    before being written to the file.
    
    .. versionadded:: 0.5
    
    """
    
    encoder = JSONEncoder(*args, **kwargs)
    source = _write_source_from_arg(source)
    with source.open_('wb') as f:
        for chunk in encoder.iterencode(list(records(table))):
            f.write(chunk)