<commit_before>from .exceptions import CardValidityError

import re
import string

CARD_WHITE=0
CARD_BLACK=1

CARD_PLACEHOLDER_LENGTH=3

class Card(object):
  def __init__(self, id=-1, text='', type=CARD_WHITE):
    self.id=id
    self.__text=text
    self.type=type

  def isValid(self, text=None):
    # implementing some safety
    # if the card is white, no wildcards are allowed
    # if the card is black, wildcards are allowed
    # there must even be at least one present
    
    # if text is None, use the current internal text
    if text == None:
      text = self.getInternalText()

    format_iterator = string.Formatter().parse(text)
    placeholders = map([f[2] for f in format_iterator], lambda x: x != None)
    if self.type==CARD_WHITE and len(placeholders)>0:
      raise CardValidityError({'id':self.id, 'text': 'White cards may not have any placeholders ( {...} ).'})
    elif self.type==CARD_BLACK and len(placeholders)==0:
      raise CardValidityError({'id': self.id, 'text': 'Black cards must contain at least one placeholder'})      
    return True

  # sets the internal text (usually just neede internally)
  def setInternalText(self, text):
    # will run through the isValid check
    if self.isValid(text):
      self.__text = text

  def getInternalText(self):
    return self.__text

  # retrieves the properly formatted card text
  def getCardText(self):
    return re.sub("{}", "_"*CARD_PLACEHOLDER_LENGTH, self.__text)

  # parses the text and will set it internally too
  def setCardText(self, text):
    internal_text = re.sub(" __+ ", " {} ",text)
    self.setInternalText(internal_text)<commit_msg>using helper function instead of the variable itself<commit_after>from .exceptions import CardValidityError

import re
import string

CARD_WHITE=0
CARD_BLACK=1

CARD_PLACEHOLDER_LENGTH=3

class Card(object):
  def __init__(self, id=-1, text='', type=CARD_WHITE):
    self.id=id
    self.__text=text
    self.type=type

  def isValid(self, text=None):
    # implementing some safety
    # if the card is white, no wildcards are allowed
    # if the card is black, wildcards are allowed
    # there must even be at least one present
    
    # if text is None, use the current internal text
    if text == None:
      text = self.getInternalText()

    format_iterator = string.Formatter().parse(text)
    placeholders = map([f[2] for f in format_iterator], lambda x: x != None)
    if self.type==CARD_WHITE and len(placeholders)>0:
      raise CardValidityError({'id':self.id, 'text': 'White cards may not have any placeholders ( {...} ).'})
    elif self.type==CARD_BLACK and len(placeholders)==0:
      raise CardValidityError({'id': self.id, 'text': 'Black cards must contain at least one placeholder'})      
    return True

  # sets the internal text (usually just neede internally)
  def setInternalText(self, text):
    # will run through the isValid check
    if self.isValid(text):
      self.__text = text

  def getInternalText(self):
    return self.__text

  # retrieves the properly formatted card text
  def getCardText(self):
    return re.sub("{}", "_"*CARD_PLACEHOLDER_LENGTH, self.getInternalText())

  # parses the text and will set it internally too
  def setCardText(self, text):
    internal_text = re.sub(" __+ ", " {} ",text)
    self.setInternalText(internal_text)
{'fraction_non_alphanumeric': 0.1045045045045045, 'fraction_numerical': 0.004204204204204204, 'mean_word_length': 3.691549295774648, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4773704', 'n_tokens_mistral': 1046, 'n_tokens_neox': 1026, 'n_words': 332}