pattern.py (1060B)
1 from plint import error 2 3 4 class Pattern: 5 def __init__(self, metric, my_id="", feminine_id="", constraint=None, hemistiches=None): 6 self.metric = metric 7 self.length = None 8 self.parse_metric() 9 self.my_id = my_id 10 self.feminine_id = feminine_id 11 self.constraint = constraint 12 if hemistiches: 13 self.hemistiches = hemistiches 14 15 def parse_metric(self): 16 """Parse from a metric description""" 17 try: 18 verse = [int(x) for x in self.metric.split('/')] 19 for i in verse: 20 if i < 1: 21 raise ValueError 22 except ValueError: 23 raise error.TemplateLoadError("Metric description should only contain positive integers") 24 if sum(verse) > 16: 25 raise error.TemplateLoadError("Metric length limit exceeded") 26 self.hemistiches = [] 27 self.length = 0 28 for v in verse: 29 self.length += v 30 self.hemistiches.append(self.length) 31 self.length = self.hemistiches.pop()