quantity_asker.py 981 B

123456789101112131415161718192021222324252627282930313233
  1. from PyInquirer import prompt, Validator, ValidationError
  2. from prompt_toolkit import document
  3. import regex
  4. # Ask for a quantity
  5. class QuantityValidator(Validator):
  6. def validate(self, document):
  7. ok = regex.match('^[1-9][0-9]*00$', document.text)
  8. if not ok:
  9. raise ValidationError(message = 'Quantity must be a positive integer multiple of 100')
  10. class QuantityAsker:
  11. """Ask quantity for name entries"""
  12. def __init__(self):
  13. self.widget = [
  14. {
  15. 'type':'input',
  16. 'name':'number',
  17. 'message':'Enter a quantity',
  18. 'validate':QuantityValidator
  19. }
  20. ]
  21. def askForOne(self, name):
  22. self.widget[0]['message'] = 'Quantity for ' + name + ' (g)'
  23. return int(prompt(self.widget)['number'])
  24. def askForEachOf(self, nameList):
  25. ans = []
  26. for name in nameList:
  27. ans.append(self.askForOne(name))
  28. return ans