<commit_before># coding: utf-8

import os
import sys
import pickle
import json
import logging
logger = logging.getLogger("PCounter")

from . import util


USBIO_BIT = util.enum('COUNT', 'BONUS', 'CHANCE', 'SBONUS', 
                      'RESERVED1', 'RESERVED2', 'LAST')
COUNT_INDEX = util.enum('COUNT', 'BONUS', 'CHANCE', 'SBONUS', 
                        'TOTALCOUNT', 'CHAIN', 'USER',
                        LAST=20 )

N_BITS = USBIO_BIT.LAST
N_COUNTS = COUNT_INDEX.LAST
BITMASK = (1 << USBIO_BIT.LAST) - 1
BITSHIFT = USBIO_BIT.LAST

class PCounterError(Exception): pass
class PCounterInitFailed(PCounterError): pass

class ICounter(object):
  def __init__(self, name=None, func_to_on=None, func_to_off=None, func_output=None):
    self.name = name
    self.func_to_on = func_to_on
    self.func_to_off = func_to_off
    self.func_output = func_output


class PCounter(object):
  def __init__(self, rcfile=None, output=None, 
              counterif=None, isreset=False, isaddnull=True,
              outputcharset=None):
    self.rcfile = rcfile
    self.couterif = counterif
    self.isaddnull = isaddnull
    self.isreset = isreset
    self.output = output or sys.stdout
    self.outputcharset = outputcharset or 'utf-8'

    self.counts = [0] * N_COUNTS 
    self.history = []
    self._switch = [ False ] * N_BITS 
    self._prev_outputstrs = "" 


  def save(self):
    try:
      with open(self.rcfile, "wb") as f:
        pickle.dump(self.counts, f, -1)
        pickle.dump(self.history, f, -1)
    except IOError, e:
      logger.error(u"カウンタ値が保存できませんでした。原因：{0}".format(e.message))

  def load(self):
    try:
      with open(self.rcfile, "rb") as f:
        self.counts = pickle.load(f)
        self.history = pickle.load(f)
      return True
    except IOError, e:
      logger.error(u"カウンタ値を読み込めませんでした。原因：{0}".format(e.message))
      return False

  def countup(self, port):
    for bit in (USBIO_BIT.COUNT, USBIO_BIT.BONUS, USBIO_BIT.CHANCE, USBIO_BIT.SBONUS):
      checkbit = 1 << bit
      if port & checkbit:
        # 状態がOff→Onになるとき
        if not self._switch[bit]:
          self._switch[bit] = True
          if self.couterif and callable(self.couterif.func_to_on):
            self.couterif.func_to_on(bit, iostatus, self.counts, self.history)
      else:
        # 状態がOn→Offになるとき
        if self._switch[bit]:
          self._switch[bit] = False
          if self.icounter and callable(self.icounter.func_to_off):
            self.icounter.func_to_off(bit, iostatus, self.counts, self.history)

  def display(self):
    if self.couterif and callable(self.couterif.func_output):
      countstr = self.couterif.func_output(self.counts, self.history)
      if countstr != self._prev_outputstrs:
        self._prev_outputstrs = countstr
        self.output.write(countstr.encode(self.outputcharset))
        if self.isaddnull:
          self.output.write("\x00")
        self.output.flush()

  def reset_counter(self):
    for i in len(self.counts):
      self.counts[i] = 0

  def do(self, port):
    self.countup(port)
    self.display()


def to_on_default(cbittype, iostatus, counts, history):
  if cbittype == USBIO_BIT.COUNT:
    counts[COUNT_INDEX.COUNT] += 1
    counts[COUNT_INDEX.TOTALCOUNT] += 1
  if cbittype == USBIO_BIT.BONUS:
    counts[COUNT_INDEX.BONUS] += 1
    if iostatus & (1 << USBIO_BIT.CHANCE):   # チャンス中なら
      counts[COUNT_INDEX.CHAIN] += 1
  if cbittype == USBIO_BIT.CHANCE:
    counts[COUNT_INDEX.CHANCE] += 1
  if cbittype == USBIO_BIT.SBONUS:
    counts[COUNT_INDEX.SBONUS] += 1


def to_off_default(cbittype, iostatus, counts, history):
  if cbittype == USBIO_BIT.BONUS:
    counts[COUNT_INDEX.COUNT] = 0
  if cbittype == USBIO_BIT.CHANCE:
    counts[COUNT_INDEX.CHAIN] = 0


<commit_msg>Adds output_default function.<commit_after># coding: utf-8

import os
import sys
import pickle
import json
import logging
logger = logging.getLogger("PCounter")

from . import util


USBIO_BIT = util.enum('COUNT', 'BONUS', 'CHANCE', 'SBONUS', 
                      'RESERVED1', 'RESERVED2', 'LAST')
COUNT_INDEX = util.enum('COUNT', 'BONUS', 'CHANCE', 'SBONUS', 
                        'TOTALCOUNT', 'CHAIN', 'USER',
                        LAST=20 )

N_BITS = USBIO_BIT.LAST
N_COUNTS = COUNT_INDEX.LAST
BITMASK = (1 << USBIO_BIT.LAST) - 1
BITSHIFT = USBIO_BIT.LAST

class PCounterError(Exception): pass
class PCounterInitFailed(PCounterError): pass

class ICounter(object):
  def __init__(self, name=None, func_to_on=None, func_to_off=None, func_output=None):
    self.name = name
    self.func_to_on = func_to_on
    self.func_to_off = func_to_off
    self.func_output = func_output


class PCounter(object):
  def __init__(self, rcfile=None, output=None, 
              counterif=None, isreset=False, isaddnull=True,
              outputcharset=None):
    self.rcfile = rcfile
    self.couterif = counterif
    self.isaddnull = isaddnull
    self.isreset = isreset
    self.output = output or sys.stdout
    self.outputcharset = outputcharset or 'utf-8'

    self.counts = [0] * N_COUNTS 
    self.history = []
    self._switch = [ False ] * N_BITS 
    self._prev_outputstrs = "" 


  def save(self):
    try:
      with open(self.rcfile, "wb") as f:
        pickle.dump(self.counts, f, -1)
        pickle.dump(self.history, f, -1)
    except IOError, e:
      logger.error(u"カウンタ値が保存できませんでした。原因：{0}".format(e.message))

  def load(self):
    try:
      with open(self.rcfile, "rb") as f:
        self.counts = pickle.load(f)
        self.history = pickle.load(f)
      return True
    except IOError, e:
      logger.error(u"カウンタ値を読み込めませんでした。原因：{0}".format(e.message))
      return False

  def countup(self, port):
    for bit in (USBIO_BIT.COUNT, USBIO_BIT.BONUS, USBIO_BIT.CHANCE, USBIO_BIT.SBONUS):
      checkbit = 1 << bit
      if port & checkbit:
        # 状態がOff→Onになるとき
        if not self._switch[bit]:
          self._switch[bit] = True
          if self.couterif and callable(self.couterif.func_to_on):
            self.couterif.func_to_on(bit, iostatus, self.counts, self.history)
      else:
        # 状態がOn→Offになるとき
        if self._switch[bit]:
          self._switch[bit] = False
          if self.icounter and callable(self.icounter.func_to_off):
            self.icounter.func_to_off(bit, iostatus, self.counts, self.history)

  def display(self):
    if self.couterif and callable(self.couterif.func_output):
      countstr = self.couterif.func_output(self.counts, self.history)
      if countstr != self._prev_outputstrs:
        self._prev_outputstrs = countstr
        self.output.write(countstr.encode(self.outputcharset))
        if self.isaddnull:
          self.output.write("\x00")
        self.output.flush()

  def reset_counter(self):
    for i in len(self.counts):
      self.counts[i] = 0

  def do(self, port):
    self.countup(port)
    self.display()


def to_on_default(cbittype, iostatus, counts, history):
  if cbittype == USBIO_BIT.COUNT:
    counts[COUNT_INDEX.COUNT] += 1
    counts[COUNT_INDEX.TOTALCOUNT] += 1
  if cbittype == USBIO_BIT.BONUS:
    counts[COUNT_INDEX.BONUS] += 1
    if iostatus & (1 << USBIO_BIT.CHANCE):   # チャンス中なら
      counts[COUNT_INDEX.CHAIN] += 1
  if cbittype == USBIO_BIT.CHANCE:
    counts[COUNT_INDEX.CHANCE] += 1
  if cbittype == USBIO_BIT.SBONUS:
    counts[COUNT_INDEX.SBONUS] += 1


def to_off_default(cbittype, iostatus, counts, history):
  if cbittype == USBIO_BIT.BONUS:
    counts[COUNT_INDEX.COUNT] = 0
  if cbittype == USBIO_BIT.CHANCE:
    counts[COUNT_INDEX.CHAIN] = 0


def output_default(counts, history):
  pass



{'fraction_non_alphanumeric': 0.11953931691818905, 'fraction_numerical': 0.006883770187979878, 'mean_word_length': 2.9185684647302903, 'pattern_counts': {'":': 0, '<': 15, '<?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': '5070167', 'n_tokens_mistral': 2840, 'n_tokens_neox': 2755, 'n_words': 534}