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

# :license       LASR_UC3M v1.0, ver LICENCIA.txt

# Este programa es software libre: puede redistribuirlo y/o modificarlo
# bajo los terminos de la Licencia Academica Social Robotics Lab - UC3M
# publicada por la Universidad Carlos III de Madrid, tanto en su version 1.0
# como en una version posterior.

# Este programa se distribuye con la intencion de que sea util,
# pero SIN NINGUNA GARANTIA. Para mas detalles, consulte la
# Licencia Academica Social Robotics Lab - UC3M version 1.0 o posterior.

# Usted ha recibido una copia de la Licencia Academica Social
# Robotics Lab - UC3M en el fichero LICENCIA.txt, que tambien se encuentra
# disponible en <URL a la LASR_UC3Mv1.0>.
"""
Averages ouput from ocular's learner_recognizer_node.

:author: <NAME>
"""
# from functools import partial
from itertools import chain, islice
# from toolz import (compose, frequencies, take)
from toolz import frequencies
import pandas as pd

import roslib
roslib.load_manifest('ocular')
import rospy
from rospy import (Publisher, Subscriber)
from rospy import loginfo, logfatal
from ocular.msg import (RecognizedObject, SystemOutput)
from rospy_utils.param_utils import load_params
from rospy_utils.func_utils import error_handler as eh

__RATE = 30

# reverselist = compose(reversed, list)


class Accumulator(object):

    """
    A List Container with max length set up in maxlen parameter.

    If new element added and list is full, then the prev. elems are flushed
    """

    def __init__(self, maxlen, seq=None):
        """
        Constructor.

        Args:
            :maxlen: Max num of elements of the accumulator.
            :seq: sequence to initialize the Accumulator. Default=None

        """
        self.maxlen = maxlen
        self.l = []     # Need no initialize
        self.insert(seq)

    def append(self, item):
        """
        Add a new item to the list.

        If len(list) >= maxlen, it flushes the list and then adds the item

        Example:

            >>> acc = Accumulator(3)
            >>> acc.append(1)   # Also acc(1)
            [1]
            >>> acc.append(2)
            [1, 2]
            >>> acc.append(3)
            [1, 2, 3]
            >>> acc.get()       # Also acc()
            [1, 2, 3]
            >>> acc.append(4)
            [4]
            >>> acc.get()
            [4]

        :see: Accumulator.__call__
        """
        if len(self.l) == self.maxlen:
            self.l = []
        self.l.append(item)
        return self
        # return self.l

    # def insert(self, seq):
    #     self.l = self.b_insert(self.l, seq)
    #     return self

    def insert(self, seq):
        """
        Insert a sequence to the list.

        Note:
            if len(self.l+seq) >= maxlen only keeps the last maxlen items
        """
        if not seq:
            return self
        total_len = len(self.l) + len(seq)
        start = (total_len // self.maxlen) * self.maxlen
        self.l = list(islice(chain(self.l, seq), start, total_len))
        return self
        # self.l = __combine(self.l, seq)
        # return self

    def get(self):
        """Return the list. Equivalent to acc."""
        return self.l

    def isfull(self):
        """Return True if Accumulator is full. Otherwise return False."""
        return len(self.l) >= self.maxlen

    def isempty(self):
        """Return True if Accumulator is empty. Otherwise return False."""<commit_msg>(m) removing old commented code<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

# :license       LASR_UC3M v1.0, ver LICENCIA.txt

# Este programa es software libre: puede redistribuirlo y/o modificarlo
# bajo los terminos de la Licencia Academica Social Robotics Lab - UC3M
# publicada por la Universidad Carlos III de Madrid, tanto en su version 1.0
# como en una version posterior.

# Este programa se distribuye con la intencion de que sea util,
# pero SIN NINGUNA GARANTIA. Para mas detalles, consulte la
# Licencia Academica Social Robotics Lab - UC3M version 1.0 o posterior.

# Usted ha recibido una copia de la Licencia Academica Social
# Robotics Lab - UC3M en el fichero LICENCIA.txt, que tambien se encuentra
# disponible en <URL a la LASR_UC3Mv1.0>.
"""
Averages ouput from ocular's learner_recognizer_node.

:author: <NAME>
"""
# from functools import partial
from itertools import chain, islice
# from toolz import (compose, frequencies, take)
from toolz import frequencies
import pandas as pd

import roslib
roslib.load_manifest('ocular')
import rospy
from rospy import (Publisher, Subscriber)
from rospy import loginfo, logfatal
from ocular.msg import (RecognizedObject, SystemOutput)
from rospy_utils.param_utils import load_params
from rospy_utils.func_utils import error_handler as eh

__RATE = 30

# reverselist = compose(reversed, list)


class Accumulator(object):

    """
    A List Container with max length set up in maxlen parameter.

    If new element added and list is full, then the prev. elems are flushed
    """

    def __init__(self, maxlen, seq=None):
        """
        Constructor.

        Args:
            :maxlen: Max num of elements of the accumulator.
            :seq: sequence to initialize the Accumulator. Default=None

        """
        self.maxlen = maxlen
        self.l = []     # Need no initialize
        self.insert(seq)

    def append(self, item):
        """
        Add a new item to the list.

        If len(list) >= maxlen, it flushes the list and then adds the item

        Example:

            >>> acc = Accumulator(3)
            >>> acc.append(1)   # Also acc(1)
            [1]
            >>> acc.append(2)
            [1, 2]
            >>> acc.append(3)
            [1, 2, 3]
            >>> acc.get()       # Also acc()
            [1, 2, 3]
            >>> acc.append(4)
            [4]
            >>> acc.get()
            [4]

        :see: Accumulator.__call__
        """
        if len(self.l) == self.maxlen:
            self.l = []
        self.l.append(item)
        return self

    def insert(self, seq):
        """
        Insert a sequence to the list.

        Note:
            if len(self.l+seq) >= maxlen only keeps the last maxlen items
        """
        if not seq:
            return self
        total_len = len(self.l) + len(seq)
        start = (total_len // self.maxlen) * self.maxlen
        self.l = list(islice(chain(self.l, seq), start, total_len))
        return self
        # self.l = __combine(self.l, seq)
        # return self

    def get(self):
        """Return the list. Equivalent to acc."""
        return self.l

    def isfull(self):
        """Return True if Accumulator is full. Otherwise return False."""
        return len(self.l) >= self.maxlen

    def isempty(self):
        """Return True if Accumulator is empty. Otherwise return False."""
{'fraction_non_alphanumeric': 0.0980506255455339, 'fraction_numerical': 0.009601396566773349, 'mean_word_length': 2.223159868729489, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 55, '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': '3075126', 'n_tokens_mistral': 2176, 'n_tokens_neox': 2092, 'n_words': 727}