listData — Phatch v0.2 documentation

### Navigation

- index
- modules |
- next |
- previous |
- Phatch v0.2 documentation »
- lib »

# listData

class `lib.listData.` `DataDict`(data=None, headers=None, id='id')

  Bases: `lib.listData.DataTuple`

  `get`(row, col, default='')

    Returns the cell value specified by the row and column index.

    Parameters: | row (int) – index of row col (int) – index of column default – value to return if column header does not exist
    Returns: | cell value

        >>> data = DataDict([{'id': 0, 'hello':'world'},
        ... {'id': 1, 'foo':'bar'}])
        >>> data.get_headers()
        ['foo', 'hello', 'id']
        >>> data.get(0,0)
        ''
        >>> data.get(0,1)
        'world'
        >>> data.get(1,0)
        'bar'
        >>> data.get(1,1)
        ''
        >>> data.get(1,2)
        1

  `get_by_header`(row, header, default='')

    Returns the cell value specified by the row and column header.

    Parameters: | row (int) – index of row col (string) – column header default – value to return if column header does not exist
    Returns: | cell value

        >>> data = DataDict([{'id': 0, 'hello':'world'},
        ... {'id': 1, 'foo':'bar'}], headers=['foo','hello'])
        >>> data.get_by_header(0,'foo')
        ''
        >>> data.get_by_header(0,'hello')
        'world'
        >>> data.get_by_header(1,'foo')
        'bar'

  `get_headers`()

    Get the headers of the columns.

    Returns: | headers of the columns
    Return type: | list of strings

        >>> data = DataDict([{'id': 0, 'hello':'world'},
        ... {'id': 1, 'foo':'bar', 'hello':'planet'}])
        >>> data.get_headers()
        ['foo', 'hello', 'id']

  `sort`(data)

    Sorts the data in place.

        >>> DataDict(id='path').sort([{'path': 'id1', 'name': 'f1'},
        ... {'path': 'id0', 'name': 'f2'}])
        [{'path': 'id0', 'name': 'f2'}, {'path': 'id1', 'name': 'f1'}]

  `update_headers`(headers=None, all=False)

    Change sequence and find all headers (dict keys) in the data.

        >>> data = DataDict([{'id': 0, 'hello':'world'},
        ... {'id': 1, 'foo':'bar'}])
        >>> data.get_headers()
        ['foo', 'hello', 'id']
        >>> data.update_headers(['hello'])
        >>> data.get_headers()
        ['hello']
        >>> data.update_headers(['hello'], all=True)
        >>> data._headers
        ['hello', 'foo', 'id']

class `lib.listData.` `DataTuple`(data=None)

  Bases: `object`

  `get`(row, col)

    Returns the cell value specified by the row and col.

    Parameters: | row (int) – index of row col (int) – index of column
    Returns: | cell value

        >>> data = DataTuple([(0, 6)])
        >>> data.get(0,0)
        6

  `get_headers`()

    Get the headers of the columns.

    Returns: | headers of the columns
    Return type: | list of strings

        >>> data = DataTuple([('id1','f1'),('id0','f2')])
        >>> data.get_headers()
        ['0']

  `set_data`(data, amount=None, sort=True)

    The data is organised as tuple/list of tuples. Amount is how much is visible and not necessarily the length of the data tuple!

    Parameters: | data (tuple/list) – data organized in rows amount (int) – amount of rows visible
    Returns: | whether the underlying data has really changed
    Return type: | bool

        >>> data = DataTuple()
        >>> data.set_data([(6, ),(5, )], amount=1)
        True
        >>> data.set_data([(6, ),(5, )], amount=1)
        False
        >>> len(data) == data.amount
        False

  `set_filter`(filter)

    Filters the data which is visible. It puts the visible data rows to the front and limits the amount. The visible data is automatically sorted.

    Parameter: | filter (string) – substring which should appear in the data rows
    Returns: | whether the filter has really changed
    Return type: | bool

        >>> data = DataTuple([(6, ),(5, )])
        >>> data.set_filter('6')
        True
        >>> data.set_filter('6')
        False
        >>> data.amount
        1
        >>> len(data)
        2

  `sort`(data)

    Sorts the data in place.

        >>> DataTuple().sort([('id1','f1'),('id0','f2')])
        [('id0', 'f2'), ('id1', 'f1')]

`lib.listData.` `files_data_dict`(files)

  Turns a flat file list into a hierarchical one , of which the data values can be fed to the DataDict Class (not the whole hierarchy).

  Parameter: | files (list) – rows which consists of tuples, each tuples contains the full filename as the first element before other data
  Returns: | folder hierarchy
  Return type: | dict

      >>> import pprint
      >>> files = [{'path': 'f0/i00', 'size': '5kb'},
      ... {'path': 'f0/i01', 'size': '1kb'},
      ... {'path': 'f1/i10', 'size': '2kb'},
      ... {'path': 'f1/f2/i120', 'size': '3kb'}]
      >>> pprint.pprint(files_data_dict(files),width=60)
      {'f0/': {'children': {},
               'data': [{'path': 'f0/i00', 'size': '5kb'},
                        {'path': 'f0/i01', 'size': '1kb'}]},
       'f1/': {'children': {},
               'data': [{'path': 'f1/i10', 'size': '2kb'}]},
       'f1/f2/': {'children': {},
                  'data': [{'path': 'f1/f2/i120',
                            'size': '3kb'}]}}

`lib.listData.` `files_data_tuple`(files)

  Turns a flat file list into a hierarchical one, of which the data values can be fed to the DataTuple Class (not the whole hierarchy).

  Parameter: | files (list) – rows which consists of tuples, each tuples contains the full filename as the first element before other data
  Returns: | folder hierarchy
  Return type: | dict

      >>> import pprint
      >>> files = [('f0/i00', 0), ('f0/i01', 1),
      ... ('f1/i10', 2), ('f1/f2/i120', 3),]
      >>> pprint.pprint(files_data_tuple(files),width=60)
      {'f0/': {'children': {},
               'data': [('f0/i00', 0), ('f0/i01', 1)]},
       'f1/': {'children': {}, 'data': [('f1/i10', 2)]},
       'f1/f2/': {'children': {}, 'data': [('f1/f2/i120', 3)]}}

### Navigation

- index
- modules |
- next |
- previous |
- Phatch v0.2 documentation »
- lib »