<commit_before>"""
Created on 4 Aug 2016

@author: <NAME> (<EMAIL>)
"""

import csv
import sys

from scs_core.csv.csv_dict import CSVHeader
from scs_core.data.json import JSONify


# TODO: deal with comma-terminated line dialect

# --------------------------------------------------------------------------------------------------------------------

class CSVReader(object):
    """
    classdocs
    """

    # ----------------------------------------------------------------------------------------------------------------

    @staticmethod
    def list_dialects():
        return csv.list_dialects()


    @staticmethod
    def __recast(value):
        try:
            return int(value)
        except ValueError:
            pass

        try:
            return float(value)
        except ValueError:
            pass

        return value


    # ----------------------------------------------------------------------------------------------------------------

    def __init__(self, filename=None):
        """
        Constructor
        """
        self.__filename = filename
        self.__file = sys.stdin if self.__filename is None else open(self.__filename, "r")

        self.__reader = csv.reader(self.__file)

        try:
            paths = next(self.__reader)
        except StopIteration:                   # no input
            paths = []

        self.__header = CSVHeader.construct_from_paths(paths)


    # ----------------------------------------------------------------------------------------------------------------

    def close(self):
        if self.__filename is None:
            return

        self.__file.close()


    # ----------------------------------------------------------------------------------------------------------------

    @property
    def rows(self):
        for row in self.__reader:
            if len(row) == 0:
                continue

            datum = self.__header.as_dict([CSVReader.__recast(cell) for cell in row])

            yield JSONify.dumps(datum)


    @property
    def filename(self):
        return self.__filename


    @property
    def header(self):
        return self.__header


    # ----------------------------------------------------------------------------------------------------------------

    def __str__(self, *args, **kwargs):
        return "CSVReader:{filename:%s, header:%s}" % (self.filename, self.header)
<commit_msg>Fixed __str__(..) method in CSVReader.<commit_after>"""
Created on 4 Aug 2016

@author: <NAME> (<EMAIL>)
"""

import csv
import sys

from scs_core.csv.csv_dict import CSVHeader
from scs_core.data.json import JSONify


# TODO: deal with comma-terminated line dialect

# --------------------------------------------------------------------------------------------------------------------

class CSVReader(object):
    """
    classdocs
    """

    # ----------------------------------------------------------------------------------------------------------------

    @staticmethod
    def list_dialects():
        return csv.list_dialects()


    @staticmethod
    def __recast(value):
        try:
            return int(value)
        except ValueError:
            pass

        try:
            return float(value)
        except ValueError:
            pass

        return value


    # ----------------------------------------------------------------------------------------------------------------

    def __init__(self, filename=None):
        """
        Constructor
        """
        self.__filename = filename
        self.__file = sys.stdin if self.__filename is None else open(self.__filename, "r")

        self.__reader = csv.reader(self.__file)

        try:
            paths = next(self.__reader)
        except StopIteration:                   # no input
            paths = []

        self.__header = CSVHeader.construct_from_paths(paths)


    # ----------------------------------------------------------------------------------------------------------------

    def close(self):
        if self.__filename is None:
            return

        self.__file.close()


    # ----------------------------------------------------------------------------------------------------------------

    @property
    def rows(self):
        for row in self.__reader:
            if len(row) == 0:
                continue

            datum = self.__header.as_dict([CSVReader.__recast(cell) for cell in row])

            yield JSONify.dumps(datum)


    @property
    def filename(self):
        return self.__filename


    @property
    def header(self):
        return self.__header


    # ----------------------------------------------------------------------------------------------------------------

    def __str__(self, *args, **kwargs):
        header = '[' + ', '.join(self.header.paths()) + ']'

        return "CSVReader:{filename:%s, header:%s}" % (self.filename, header)

{'fraction_non_alphanumeric': 0.34795856185252894, 'fraction_numerical': 0.002437538086532602, 'mean_word_length': 0.8743814236771983, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '3285830', 'n_tokens_mistral': 1107, 'n_tokens_neox': 1011, 'n_words': 256}