<commit_before>"""
Utility functions.

"""


from itertools import islice, groupby, chain
from collections import defaultdict, Counter, namedtuple
from operator import itemgetter
import re
from string import maketrans
import random
import time
import datetime
from collections import OrderedDict
from functools import partial
from itertools import izip_longest
import heapq
import sys
from petl.base import IterContainer
import operator


class RowContainer(IterContainer):
    
    def __getitem__(self, item):
        if isinstance(item, basestring):
            return ValuesContainer(self, item)
        else:
            return super(RowContainer, self).__getitem__(item)


def header(table):
    """
    Return the header row for the given table. E.g.::
    
        >>> from petl import header
        >>> table = [['foo', 'bar'], ['a', 1], ['b', 2]]
        >>> header(table)
        ['foo', 'bar']
    
    """
    
    it = iter(table)
    return it.next()


def fieldnames(table):
    """
    Return the string values of all fields for the given table. If the fields
    are strings, then this function is equivalent to :func:`header`, i.e.::
    
        >>> from petl import header, fieldnames
        >>> table = [['foo', 'bar'], ['a', 1], ['b', 2]]
        >>> header(table)
        ['foo', 'bar']
        >>> fieldnames(table)
        ['foo', 'bar']
        >>> header(table) == fieldnames(table)
        True
    
    Allows for custom field objects, e.g.::

        >>> class CustomField(object):
        ...     def __init__(self, id, description):
        ...         self.id = id
        ...         self.description = description
        ...     def __str__(self):
        ...         return self.id
        ...     def __repr__(self):
        ...         return 'CustomField(%r, %r)' % (self.id, self.description)
        ... 
        >>> table = [[CustomField('foo', 'Get some foo.'), CustomField('bar', 'A lot of bar.')], 
        ...          ['a', 1], 
        ...          ['b', 2]]
        >>> header(table)
        [CustomField('foo', 'Get some foo.'), CustomField('bar', 'A lot of bar.')]
        >>> fieldnames(table)    
        ['foo', 'bar']

    """
    
    return [str(f) for f in header(table)]

    
def iterdata(table, *sliceargs):
    """
    Return an iterator over the data rows for the given table. E.g.::
    
        >>> from petl import data
        >>> table = [['foo', 'bar'], ['a', 1], ['b', 2]]
        >>> it = data(table)
        >>> it.next()
        ['a', 1]
        >>> it.next()
        ['b', 2]
        
    .. versionchanged:: 0.3
    
    Positional arguments can be used to slice the data rows. The `sliceargs` are 
    passed to :func:`itertools.islice`.
    
    .. versionchanged:: 0.10
    
    Renamed from "data".
    
    """

    it = islice(table, 1, None) # skip header row
    if sliceargs:
        it = islice(it, *sliceargs)
    return it
    

def data(table, *sliceargs):
    """
    Return a container supporting iteration over data rows in a given 
    table. I.e., like :func:`iterdata` only a container is returned so you 
    can iterate over it multiple times.
    
    .. versionchanged:: 0.10 

    Now returns a container, previously returned an iterator. See also 
    :func:`iterdata`.
    """
    
    return DataContainer(table, *sliceargs)


class DataContainer(IterContainer):
    
    def __init__(self, table, *sliceargs):
        self.table = table
        self.sliceargs = sliceargs
        
    def __iter__(self):
        return iterdata(self.table, *self.sliceargs) 
        
           
def dataslice(table, *args):
    """
    .. deprecated:: 0.3
    
    Use :func:`data` instead, it supports slice arguments.
    
    """
    
    return islice(data(table), *args)

    
def records(table, missing=None):
    """
    Return an iterator over the data in the table, yielding each row as a 
    dictionary of values indexed by field name. E.g.::
    
        >>> from petl import records
        >>> table = [['foo', 'bar'], ['a', 1], ['b', 2]]
        >>> it = records(table)
        >>> it.next()<commit_msg>data returns row container - better for fluent<commit_after>"""
Utility functions.

"""


from itertools import islice, groupby, chain
from collections import defaultdict, Counter, namedtuple
from operator import itemgetter
import re
from string import maketrans
import random
import time
import datetime
from collections import OrderedDict
from functools import partial
from itertools import izip_longest
import heapq
import sys
from petl.base import IterContainer
import operator


class RowContainer(IterContainer):
    
    def __getitem__(self, item):
        if isinstance(item, basestring):
            return ValuesContainer(self, item)
        else:
            return super(RowContainer, self).__getitem__(item)


def header(table):
    """
    Return the header row for the given table. E.g.::
    
        >>> from petl import header
        >>> table = [['foo', 'bar'], ['a', 1], ['b', 2]]
        >>> header(table)
        ['foo', 'bar']
    
    """
    
    it = iter(table)
    return it.next()


def fieldnames(table):
    """
    Return the string values of all fields for the given table. If the fields
    are strings, then this function is equivalent to :func:`header`, i.e.::
    
        >>> from petl import header, fieldnames
        >>> table = [['foo', 'bar'], ['a', 1], ['b', 2]]
        >>> header(table)
        ['foo', 'bar']
        >>> fieldnames(table)
        ['foo', 'bar']
        >>> header(table) == fieldnames(table)
        True
    
    Allows for custom field objects, e.g.::

        >>> class CustomField(object):
        ...     def __init__(self, id, description):
        ...         self.id = id
        ...         self.description = description
        ...     def __str__(self):
        ...         return self.id
        ...     def __repr__(self):
        ...         return 'CustomField(%r, %r)' % (self.id, self.description)
        ... 
        >>> table = [[CustomField('foo', 'Get some foo.'), CustomField('bar', 'A lot of bar.')], 
        ...          ['a', 1], 
        ...          ['b', 2]]
        >>> header(table)
        [CustomField('foo', 'Get some foo.'), CustomField('bar', 'A lot of bar.')]
        >>> fieldnames(table)    
        ['foo', 'bar']

    """
    
    return [str(f) for f in header(table)]

    
def iterdata(table, *sliceargs):
    """
    Return an iterator over the data rows for the given table. E.g.::
    
        >>> from petl import data
        >>> table = [['foo', 'bar'], ['a', 1], ['b', 2]]
        >>> it = data(table)
        >>> it.next()
        ['a', 1]
        >>> it.next()
        ['b', 2]
        
    .. versionchanged:: 0.3
    
    Positional arguments can be used to slice the data rows. The `sliceargs` are 
    passed to :func:`itertools.islice`.
    
    .. versionchanged:: 0.10
    
    Renamed from "data".
    
    """

    it = islice(table, 1, None) # skip header row
    if sliceargs:
        it = islice(it, *sliceargs)
    return it
    

def data(table, *sliceargs):
    """
    Return a container supporting iteration over data rows in a given 
    table. I.e., like :func:`iterdata` only a container is returned so you 
    can iterate over it multiple times.
    
    .. versionchanged:: 0.10 

    Now returns a container, previously returned an iterator. See also 
    :func:`iterdata`.
    """
    
    return DataContainer(table, *sliceargs)


class DataContainer(RowContainer):
    
    def __init__(self, table, *sliceargs):
        self.table = table
        self.sliceargs = sliceargs
        
    def __iter__(self):
        return iterdata(self.table, *self.sliceargs) 
        
           
def dataslice(table, *args):
    """
    .. deprecated:: 0.3
    
    Use :func:`data` instead, it supports slice arguments.
    
    """
    
    return islice(data(table), *args)

    
def records(table, missing=None):
    """
    Return an iterator over the data in the table, yielding each row as a 
    dictionary of values indexed by field name. E.g.::
    
        >>> from petl import records
        >>> table = [['foo', 'bar'], ['a', 1], ['b', 2]]
        >>> it = records(table)
        >>> it.next()
{'fraction_non_alphanumeric': 0.13899660688317983, 'fraction_numerical': 0.005574406204556471, 'mean_word_length': 2.0783289817232378, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 129, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5476525', 'n_tokens_mistral': 2475, 'n_tokens_neox': 2382, 'n_words': 787}