Created
January 21, 2017 12:24
-
-
Save badbye/e9d35281462e4c1b981d471a7ec95d97 to your computer and use it in GitHub Desktop.
Elegent magic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # encoding: utf8 | |
| """ | |
| Created on 2017.01.21 | |
| @author: yalei | |
| """ | |
| import inspect | |
| import re | |
| def valid_exp(s): | |
| bracket = [] | |
| res = '' | |
| for i in s: | |
| s = s.lstrip(i) | |
| if i in [',', ')'] and bracket == []: | |
| return res, s | |
| if i in ['(', '[', '{']: | |
| bracket.append(i) | |
| elif i in [')', ']', '}']: | |
| bracket.pop() | |
| res += i | |
| return res, s | |
| def name(*args, **kwargs): | |
| exec_line = inspect.stack()[-1][-2][0].strip() # - A - wtf | |
| # print exec_line | |
| arg_strings = re.search('name\((.*)', exec_line).groups()[0] | |
| args = [] | |
| while arg_strings: | |
| arg, arg_strings = valid_exp(arg_strings) | |
| args.append(arg.strip()) | |
| return args | |
| if __name__ == '__main__': | |
| a = 1 | |
| b = 2 | |
| print 'arguments: ', name(a+1-4, 8-92+123) | |
| print 'arguments: ', name(b-a, 'hhhh, success') |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does not work in console.