<commit_before># -*- coding: utf-8 -*-

import csv

class Exporter(object):

    def __init__(self, name):
        self.name = name

    def do_export(self, f, rows):
    
        for row in rows:
            f.write("{}\n".format(row))
            
            
            
class JsonExporter(object):
    def do_export(self, f, rows):
        json.dump(rows, f, indent = 2)
        
 
class CsvExporter(object):
    def do_export(self, f, rows):
    
        fieldnames = rows[0].keys()
        
        writer = csv.DictWriter(f, fieldnames = fieldnames, delimiter = ";")

        writer.writeheader()
        
        for row in rows:
            writer.writerow(row)<commit_msg>Some hints..<commit_after># -*- coding: utf-8 -*-

import csv
import json

class BaseExporter(object):
    def __init__(self, f, debug = False):
        self.f = f
        self.debug = debug
        
    def do_export(self, rows):
        raise NotImplementedError("To be implemented in subclasses!")
    

class FileExporter(BaseExporter):

    def do_export(self, rows):
    
        for row in rows:
            output = "{}\n".format(row)
            if self.debug:
                print(output)
            else:
                self.f.write("{}\n".format(row))
            
            
            
class JsonExporter(BaseExporter):
    def do_export(self, rows):
        
        if self.debug:
            print(json.dumps(rows, indent=2))
        else:
            json.dump(rows, self.f, indent = 2)
        
 
class CsvExporter(BaseExporter):
    def do_export(self, rows):
        if self.debug:
            raise NotImplementedError("TODO")
    
        fieldnames = rows[0].keys()
        
        writer = csv.DictWriter(self.f, fieldnames = fieldnames, delimiter = ";")

        writer.writeheader()
        
        for row in rows:
            writer.writerow(row)
            
"""   
Un piccolo esempio..
         
BACKENDS_MAP = {
    "son" : JsonExporter,
    "csv" : CsvExporter
}

def apply_exortation_from_file(fname, data):
    ext = fname[-3:] #Leggi gli ultimi 3 caratteri!!
    xp_klass = BACKENDS_MAP.get(ext)
    
    xp = xp_klass(f)
    xp.do_export(data)
    
"""
{'fraction_non_alphanumeric': 0.11737734983952315, 'fraction_numerical': 0.0041265474552957355, 'mean_word_length': 1.7343358395989974, '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': '527037', 'n_tokens_mistral': 705, 'n_tokens_neox': 682, 'n_words': 152}