<commit_before>class TerminalColorMapException(Exception):
  pass

def _rgb(color):
  return ((color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff)

def _diff(color1, color2):
  (r1, g1, b1) = _rgb(color1)
  (r2, g2, b2) = _rgb(color2)
  return abs(r1-r2) + abs(g1-g2) + abs(b1-b2)
    
class TerminalColorMap:
  def getColors(self, order='rgb'):
    return self.colors

  def convert(self, hexcolor):
    diffs = {}
    for xterm, rgb in self.colors.items():
      diffs[_diff(rgb, hexcolor)] = xterm
    minDiffAnsi = diffs[min(diffs.keys())]
    return (minDiffAnsi, self.colors[minDiffAnsi])

  def colorize(self, string, rgb=None, ansi=None,bg=None,ansi_bg=None):
    if rgb is None and ansi is None:
      raise TerminalColorMapException('colorize: must specify one named parameter: rgb or ansi')
    if rgb is not None and ansi is not None:
      raise TerminalColorMapException('colorize: must specify only one named parameter: rgb or ansi')
    if bg is not None and ansi_bg is not None:
      raise TerminalColorMapException('colorize: must specify only one named parameter: bg or ansi_bg')
    
    if rgb != None:
      (closestAnsi, closestRgb) = self.convert(rgb)
    elif ansi != None:
      (closestAnsi, closestRgb) = (ansi, self.colors[ansi])
    
    if bg == None and ansi_bg == None:
        return "\033[38;5;{ansiCode:d}m{string:s}\033[0m".format(ansiCode=closestAnsi, string=string)
    
    if bg != None:
        (closestBgAnsi,unused) = self.convert(bg)
    elif ansi_bg != None:
        (closestBgAnsi,unused) = (ansi_bg, self.colors[ansi_bg])

    return u"\033[38;5;{ansiCode:d}m\033[48;5;{bf:d}m{string:s}\033[0m".format(ansiCode=closestAnsi,bf=closestBgAnsi, string=string)

class VT100ColorMap(TerminalColorMap):
  primary = [
    0x000000, 0x800000, 0x008000, 0x808000, 0x000080, 0x800080, 0x008080, 0xc0c0c0
  ]

  bright = [
    0x808080, 0xff0000, 0x00ff00, 0xffff00, 0x0000ff, 0xff00ff, 0x00ffff, 0xffffff
  ]

  def __init__(self):
    self.colors = dict()
    self._compute()

  def _compute(self):<commit_msg>Python3 fix<commit_after>class TerminalColorMapException(Exception):
  pass

def _rgb(color):
  return ((color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff)

def _diff(color1, color2):
  (r1, g1, b1) = _rgb(color1)
  (r2, g2, b2) = _rgb(color2)
  return abs(r1-r2) + abs(g1-g2) + abs(b1-b2)
    
class TerminalColorMap:
  def getColors(self, order='rgb'):
    return self.colors

  def convert(self, hexcolor):
    diffs = {}
    for xterm, rgb in self.colors.items():
      diffs[_diff(rgb, hexcolor)] = xterm
    minDiffAnsi = diffs[min(diffs.keys())]
    return (minDiffAnsi, self.colors[minDiffAnsi])

  def colorize(self, string, rgb=None, ansi=None,bg=None,ansi_bg=None):
    if rgb is None and ansi is None:
      raise TerminalColorMapException('colorize: must specify one named parameter: rgb or ansi')
    if rgb is not None and ansi is not None:
      raise TerminalColorMapException('colorize: must specify only one named parameter: rgb or ansi')
    if bg is not None and ansi_bg is not None:
      raise TerminalColorMapException('colorize: must specify only one named parameter: bg or ansi_bg')
    
    if rgb != None:
      (closestAnsi, closestRgb) = self.convert(rgb)
    elif ansi != None:
      (closestAnsi, closestRgb) = (ansi, self.colors[ansi])
    
    if bg == None and ansi_bg == None:
        return "\033[38;5;{ansiCode:d}m{string:s}\033[0m".format(ansiCode=closestAnsi, string=string)
    
    if bg != None:
        (closestBgAnsi,unused) = self.convert(bg)
    elif ansi_bg != None:
        (closestBgAnsi,unused) = (ansi_bg, self.colors[ansi_bg])

    return "\033[38;5;{ansiCode:d}m\033[48;5;{bf:d}m{string:s}\033[0m".format(ansiCode=closestAnsi,bf=closestBgAnsi, string=string)

class VT100ColorMap(TerminalColorMap):
  primary = [
    0x000000, 0x800000, 0x008000, 0x808000, 0x000080, 0x800080, 0x008080, 0xc0c0c0
  ]

  bright = [
    0x808080, 0xff0000, 0x00ff00, 0xffff00, 0x0000ff, 0xff00ff, 0x00ffff, 0xffffff
  ]

  def __init__(self):
    self.colors = dict()
    self._compute()

  def _compute(self):
{'fraction_non_alphanumeric': 0.128686327077748, 'fraction_numerical': 0.06653668047769924, 'mean_word_length': 3.874109263657957, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '624647', 'n_tokens_mistral': 1743, 'n_tokens_neox': 1547, 'n_words': 362}