Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Commonmark migration
Source Link

##EDIT

EDIT

##EDIT

EDIT

added 826 characters in body
Source Link
eyquem
  • 27.8k
  • 7
  • 43
  • 46

.

##EDIT

I found a way to simplify:

from sys import excepthook
def condition1(stuff):
 '''
 stuff must be the string of an integer'''
 try:
 int(stuff)
 return True
 except:
 return False
def another2(stuff):
 '''
 stuff is the string of an integer
 but the integer must be in the range(10,30)'''
 return int(stuff) in xrange(10,30)
tup = (condition1,another2)
while True:
 try:
 stuff = raw_input("Please enter foo: ")
 for condition in tup:
 assert(condition(stuff))
 print("Thanks.")
 break
 except AssertionError:
 excepthook('AssertionError', condition.func_doc, None)

.

##EDIT

I found a way to simplify:

from sys import excepthook
def condition1(stuff):
 '''
 stuff must be the string of an integer'''
 try:
 int(stuff)
 return True
 except:
 return False
def another2(stuff):
 '''
 stuff is the string of an integer
 but the integer must be in the range(10,30)'''
 return int(stuff) in xrange(10,30)
tup = (condition1,another2)
while True:
 try:
 stuff = raw_input("Please enter foo: ")
 for condition in tup:
 assert(condition(stuff))
 print("Thanks.")
 break
 except AssertionError:
 excepthook('AssertionError', condition.func_doc, None)
Source Link
eyquem
  • 27.8k
  • 7
  • 43
  • 46

A bit complicated, but may be interesting:

import re
from sys import exc_info,excepthook
from traceback import format_exc
def condition1(stuff):
 '''
 stuff must be the string of an integer'''
 try:
 i = int(stuff)
 return True
 except:
 return False
def condition2(stuff):
 '''
 stuff is the string of an integer
 but the integer must be in the range(10,30)'''
 return int(stuff) in xrange(10,30)
regx = re.compile('assert *\( *([_a-z\d]+)')
 
while True:
 try:
 stuff = raw_input("Please enter foo: ")
 assert(condition1(stuff))
 assert ( condition2(stuff))
 print("Thanks.")
 break
 except AssertionError:
 tbs = format_exc(exc_info()[0])
 funky = globals()[regx.search(tbs).group(1)]
 excepthook(exc_info()[0], funky.func_doc, None)

result

Please enter foo: g
AssertionError: 
 stuff must be the string of an integer
Please enter foo: 170
AssertionError: 
 stuff is the string of an integer
 but the integer must be in the range(10,30)
Please enter foo: 15
Thanks.
lang-py

AltStyle によって変換されたページ (->オリジナル) /