<commit_before>import csv

def commandsToCSV(fName, cmds):
    with open(fName, 'wb') as f:
        writer = csv.writer(f, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
        writer.writerow(['Start Time', 'End Time', 'LBA', 'FUA', 'length'])

        prev = None
        for cmd in cmds:
            isFUA = 'Y' if hasattr(cmd[0], "fua") and cmd[0].fua() else 'N'
            length = cmd[0].sectorCount() if hasattr(cmd[0], "sectorCount") else 0
            writer.writerow([cmd[0].sTime(), cmd[len(cmd) - 1].eTime(), cmd[0].lba(),
                isFUA, length])
<commit_msg>Use Field objects to generate CSV<commit_after>import csv
import copy

class Field(object):
    def __init__(self, name, valueFn):
        self._name = name
        self._valueFn = valueFn

    def name(self):
        return self._name

    def value(self, prev, cur, next):
        return self._valueFn(self, prev, cur, next)

class Time(Field):
    def __init__(self, isStart):
        name = 'Start Time' if isStart else 'End Time'
        def valueFn(self, prev, cur, next):
            return cur[0].sTime() if isStart else cur[len(cur) - 1].eTime() 
        super(Time, self).__init__(name, valueFn)

class ID(Field):
    def __init__(self, isStart):
        name = 'ID' if isStart else 'End ID'
        def valueFn(self, prev, cur, next):
            return cur[0].id() if isStart else cur[len(cur) - 1].id() 
        super(ID, self).__init__(name, valueFn)

class LBA(Field):
    def __init__(self):
        super(LBA, self).__init__('LBA', lambda self, prev, cur, next: cur[0].lba())

class FUA(Field):
    def __init__(self):
        super(FUA, self).__init__('FUA',
                lambda self, prev, cur, next:
                    'Y' if hasattr(cur[0], "fua") and cur[0].fua() else 'N'
            )

class Length(Field):
    def __init__(self):
        super(Length, self).__init__('length',
                lambda self, prev, cur, next:
                    cur[0].sectorCount() if hasattr(cur[0], "sectorCount") else 0
            )

class InterCmdTime(Field):
    def __init__(self):
        def valueFn(self, prev, cur, next):
            return 0 if prev is None else cur[0].sTime() - prev[0].sTime()
        super(InterCmdTime, self).__init__('InterCmdTime', valueFn)


ALL_FIELDS = [
        Time(True), Time(False), ID(True), ID(False), InterCmdTime(), LBA(),
        FUA(), Length()
    ]

def commandsToCSV(fName, cmds, fields = ALL_FIELDS):
    fields = map(lambda f: copy.deepcopy(f), fields)

    with open(fName, 'wb') as f:
        writer = csv.writer(f, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
        writer.writerow(map(lambda f: f.name(), fields))

        def cmdIter(cmds):
            prev = None
            cur = None
            next = None
            for cmd in cmds:
                prev = cur
                cur = next
                next = cmd
                if cur is not None:
                    yield [prev, cur, next]
            prev = cur
            cur = next
            next = None
            yield [prev, cur, next]

        for t in cmdIter(cmds):
            writer.writerow(map(lambda f: f.value(t[0], t[1], t[2]), fields))

{'fraction_non_alphanumeric': 0.14182842830306824, 'fraction_numerical': 0.007514088916718848, 'mean_word_length': 2.185443668993021, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3676082', 'n_tokens_mistral': 1063, 'n_tokens_neox': 1024, 'n_words': 287}