Add files via upload

This commit is contained in:
Cutipus 2018-04-25 14:16:16 +03:00 committed by GitHub
parent c2614ffc6f
commit 6535e0cb1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 44 additions and 0 deletions

44
yes.py Normal file
View File

@ -0,0 +1,44 @@
import inspect
from io import StringIO
import sys
import re
did_you_mean = re.compile(r'Did you mean (.*)\?', re.IGNORECASE)
what = 'what?'
class Yes:
def __repr__(self):
try:
last_error = sys.last_value
except:
return what
if not isinstance(last_error, Exception):
return what
error_message = str(last_error)
try:
suggestion = did_you_mean.search(error_message).group(1)
except Exception as e:
return what
if not suggestion.startswith('print('):
suggestion_ = f'print({suggestion})'
else:
suggestion_ = suggestion
frame = inspect.currentframe()
locals = frame.f_back.f_locals
del frame
old_stdout = sys.stdout
eval_output = sys.stdout = StringIO()
eval(suggestion_, globals(), locals)
sys.stdout = old_stdout
return(f'>>> {suggestion} \n{eval_output.getvalue()[:-1]}')
yes = Yes()