Skip to content

Instantly share code, notes, and snippets.

@guykisel
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guykisel/479b7e39bce966257dbd to your computer and use it in GitHub Desktop.
Save guykisel/479b7e39bce966257dbd to your computer and use it in GitHub Desktop.
RF autocast
# No autocast:
# https://github.com/guykisel/robotframework-faker/issues/3
# Does not work: ${WordsList} = Words nb=10
# Works: ${WordsList} = Words nb=${10}
# Libdoc introspects accurate args/kwargs
def __getattr__(self, name):
func = None
if name in self._fake.__dict__.keys():
return self._fake.__dict__[name].im_func)
elif name in faker.generator.Generator.__dict__.keys():
return faker.generator.Generator.__dict__[name]
raise AttributeError('Non-existing keyword "{0}"'.format(name))
# with autocast
# Works: ${WordsList} = Words nb=10
# Works: ${WordsList} = Words nb=${10}
# Libdoc shows *args, **kwargs for all keywords
def __getattr__(self, name):
if name in _fake.__dict__.keys():
return _str_vars_to_data(_fake.__dict__[name].im_func)
elif name in faker.generator.Generator.__dict__.keys():
return _str_vars_to_data(faker.generator.Generator.__dict__[name])
raise AttributeError('Non-existing keyword "{0}"'.format(name))
def _str_to_data(string):
try:
return ast.literal_eval(str(string).strip())
except Exception:
return string
@decorator.decorator
def _str_vars_to_data(f, *args, **kwargs):
args = [_str_to_data(arg) for arg in args]
kwargs = dict((arg_name, _str_to_data(arg)) for arg_name, arg in kwargs.items())
result = f(*args, **kwargs)
logger.debug(result)
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment