<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

__author__: '<NAME>'
__licence__: 'BSD3-clause'

"""
Scrip to programatically generate notebook for exploratory analysis
Use the code from: https://gist.github.com/fperez/9716279
"""

import nbformat as nbf
from nbconvert.preprocessors import ExecutePreprocessor


from config import NotebookConfig
from plotting import plot_choose
from counting import count_unique_value


class GenerateNotebook(NotebookConfig):
    """
    """
    def __init__(self, notebook_filename):
        """
        """
        self.outfilename = notebook_filename
        # Generate an empty notebook
        self.nb = nbf.v4.new_notebook()
        self._import()
        self._load_dataset()
        # Processor to run the notebook
        self.processor = ExecutePreprocessor(timeout=600, kernel_name='python3')

    def _import(self):
        """
        Import all the needed library
        """
        self.add_section('Importing modules')
        import_code = '\n'.join(self.to_import)
        self._add_code(import_code)

    def _load_dataset(self):
        """
        """
        self._add_text('# Loading dataset')
        loading = """df =  pd.read_csv('{}')""".format(self.cleaned_df_location)
        self._add_code(loading)

    def add_section(self, text):
        return self._add_text('# Section: {}'.format(text))

    def add_question_title(self, text):
        return self._add_text('## {}'.format(text))

    def add_freq_table(self, to_freq):
        """
        """
        code_to_freq = """count_unique_value_single(df, "{}")""".format(to_freq)
        return self._add_code(code_to_freq)

    def add_plot(self, to_plot):
        """
        """
        pass

    def _add_text(self, text_to_add):
        """
        """
        formatting_text = nbf.v4.new_markdown_cell(text_to_add)
        self._append_notebook(formatting_text)

    def _add_code(self, code_to_add):
        """<commit_msg>Add add_count<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

__author__: '<NAME>'
__licence__: 'BSD3-clause'

"""
Scrip to programatically generate notebook for exploratory analysis
Use the code from: https://gist.github.com/fperez/9716279
"""

import nbformat as nbf
from nbconvert.preprocessors import ExecutePreprocessor


from config import NotebookConfig


class GenerateNotebook(NotebookConfig):
    """
    """
    def __init__(self, notebook_filename):
        """
        """
        self.outfilename = notebook_filename
        # Generate an empty notebook
        self.nb = nbf.v4.new_notebook()
        self._import()
        self._load_dataset()
        # Processor to run the notebook
        self.processor = ExecutePreprocessor(timeout=600, kernel_name='python3')

    def _import(self):
        """
        Import all the needed library
        """
        self.add_section('Importing modules')
        import_code = '\n'.join(self.to_import)
        self._add_code(import_code)

    def _load_dataset(self):
        """
        """
        self._add_text('# Loading dataset')
        loading = """df =  pd.read_csv('{}')""".format(self.cleaned_df_location)
        self._add_code(loading)

    def add_section(self, text):
        """
        """
        return self._add_text('# Section: {}'.format(text))

    def add_question_title(self, text):
        """
        """
        return self._add_text('## {}'.format(text))

    def add_count(self, *args):
        """
        """
        count_count = """get_count(df, {}, "{}")""".format(*args)
        return self._add_code(count_count)

    def add_freq_table(self, to_freq):
        """
        """
        code_to_freq = """count_unique_value_single(df, "{}")""".format(to_freq)
        return self._add_code(code_to_freq)

    def add_plot(self, to_plot):
        """
        """
        pass

    def _add_text(self, text_to_add):
        """
        """
        formatting_text = nbf.v4.new_markdown_cell(text_to_add)
        self._append_notebook(formatting_text)

    def _add_code(self, code_to_add):
        """
{'fraction_non_alphanumeric': 0.13148193659375768, 'fraction_numerical': 0.007372818874416319, 'mean_word_length': 2.617777777777778, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '4548973', 'n_tokens_mistral': 1301, 'n_tokens_neox': 1251, 'n_words': 239}