Skip to content

Instantly share code, notes, and snippets.

@badbye
Created January 21, 2017 12:24
Show Gist options
  • Select an option

  • Save badbye/e9d35281462e4c1b981d471a7ec95d97 to your computer and use it in GitHub Desktop.

Select an option

Save badbye/e9d35281462e4c1b981d471a7ec95d97 to your computer and use it in GitHub Desktop.
Elegent magic
# 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')
@badbye

badbye commented Jan 21, 2017

Copy link
Copy Markdown
Author

Does not work in console.

from test import *
a = 1
print 'arguments: ', name(a+1-4, 8-92+123)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment