<commit_before>__author__ = 'calvin'

import datetime
import sqlite3
import logging

from .table import Table
from ..tools import insert_row


logger = logging.getLogger('AnonymousUsage')

NO_STATE = type('NO_STATE', (object, ), {})


class State(Table):
    """
    Tracks the state of a certain attribute over time.

    Usage:
        tracker.track_state(state_name)
        tracker[state_name] = 'ON'
        tracker[state_name] = 'OFF'
    """
    table_args = ("UUID", "INT"), ("Count", "INT"), ("State", "TEXT"), ("Time", "TEXT")
    IPC_COMMANDS = {'GET': ('count', 'state'),
                    'SET': ('count', 'state'),
                    'ACT': ()}

    def __init__(self, name, tracker, initial_state=NO_STATE, keep_redundant=False):
        super(State, self).__init__(name, tracker)
        self.keep_redundant = keep_redundant

        if self.count == 0:
            # This is a new table
            self._state = initial_state
            if initial_state is not NO_STATE:
                # If the initial state was not NO_STATE, then add it to the database
                self.insert(self._state)
        else:
            self._state = self.get_last(1)[0]['State']

    @property
    def state(self):
        return self._state

    @state.setter
    def state(self, value):
        self.insert(value)

    def insert(self, value):
        if not self.keep_redundant and value == self._state:
            # Don't add redundant information, ie if the state value is the same as the previous do not insert a new row
            return

        dt = datetime.datetime.now().strftime(self.time_fmt)

        try:
            with Table.lock:
                insert_row(self.tracker.dbcon, self.name, self.tracker.uuid, self.count + 1, str(value), dt)
        except sqlite3.Error as e:
            logger.error(e)
        else:
            self._state = value
            self.count += 1
            logger.debug("{name} state set to {value}".format(name=self.name, value=value))
        return self

    def __repr__(self):
        state = self._state if self._state is not NO_STATE else 'No State'
        return "State ({s.name}): {state}".format(s=self, state=state)
<commit_msg>Added change action type to the state trackable to make it more consistent with the other types<commit_after>__author__ = 'calvin'

import datetime
import sqlite3
import logging

from .table import Table
from ..tools import insert_row


logger = logging.getLogger('AnonymousUsage')

NO_STATE = type('NO_STATE', (object, ), {})


class State(Table):
    """
    Tracks the state of a certain attribute over time.

    Usage:
        tracker.track_state(state_name)
        tracker[state_name] = 'ON'
        tracker[state_name] = 'OFF'
    """
    table_args = ("UUID", "INT"), ("Count", "INT"), ("State", "TEXT"), ("Time", "TEXT")
    IPC_COMMANDS = {'GET': ('count', 'state'),
                    'SET': ('count', 'state'),
                    'ACT': ('change')}

    def __init__(self, name, tracker, initial_state=NO_STATE, keep_redundant=False):
        super(State, self).__init__(name, tracker)
        self.keep_redundant = keep_redundant

        if self.count == 0:
            # This is a new table
            self._state = initial_state
            if initial_state is not NO_STATE:
                # If the initial state was not NO_STATE, then add it to the database
                self.insert(self._state)
        else:
            self._state = self.get_last(1)[0]['State']

    @property
    def state(self):
        return self._state

    @state.setter
    def state(self, value):
        self.insert(value)

    def change(self, value):
        self.state = value
        return self.state

    def insert(self, value):
        if not self.keep_redundant and value == self._state:
            # Don't add redundant information, ie if the state value is the same as the previous do not insert a new row
            return

        dt = datetime.datetime.now().strftime(self.time_fmt)

        try:
            with Table.lock:
                insert_row(self.tracker.dbcon, self.name, self.tracker.uuid, self.count + 1, str(value), dt)
        except sqlite3.Error as e:
            logger.error(e)
        else:
            self._state = value
            self.count += 1
            logger.debug("{name} state set to {value}".format(name=self.name, value=value))
        return self

    def __repr__(self):
        state = self._state if self._state is not NO_STATE else 'No State'
        return "State ({s.name}): {state}".format(s=self, state=state)

{'fraction_non_alphanumeric': 0.11645182433908674, 'fraction_numerical': 0.00305877212147695, 'mean_word_length': 2.27, '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': '4528556', 'n_tokens_mistral': 1381, 'n_tokens_neox': 1327, 'n_words': 396}