Skip to content

Instantly share code, notes, and snippets.

@guykisel
Last active August 29, 2015 14:07

Revisions

  1. guykisel revised this gist Oct 15, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions autocast.py
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@
    # 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
    @@ -17,6 +18,7 @@ def __getattr__(self, 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():
  2. guykisel revised this gist Oct 15, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion autocast.py
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@
    def __getattr__(self, name):
    func = None
    if name in self._fake.__dict__.keys():
    return gfake.__dict__[name].im_func)
    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))
  3. guykisel created this gist Oct 15, 2014.
    42 changes: 42 additions & 0 deletions autocast.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    # No autocast:
    # https://github.com/guykisel/robotframework-faker/issues/3
    # Does not work: ${WordsList} = Words nb=10
    # Works: ${WordsList} = Words nb=${10}

    def __getattr__(self, name):
    func = None
    if name in self._fake.__dict__.keys():
    return gfake.__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}

    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