Skip to content

Commit

Permalink
Created the recursive_get_representation to allow up to n=2 recursive…
Browse files Browse the repository at this point in the history
… calls
  • Loading branch information
evandrocoan committed Aug 21, 2019
1 parent 7528961 commit 20f5e0b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
30 changes: 24 additions & 6 deletions all/debug_tools/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import random

import threading
import traceback
import textwrap

from collections import OrderedDict
Expand Down Expand Up @@ -635,11 +636,24 @@ def wrap_text(text, wrap=0, trim_tabs=None, trim_spaces=None, trim_lines=None,
return dedent_lines


def get_representation(objectname, ignore=[], emquote=False, repr=repr):
def recursive_get_representation(*args, recursive_depth=2, **kwargs):
""" It attempt to detect the `get_representation` function was called recursively
It can happen when one attribute contains the other and vice-versa.
"""
Given a object, iterating through all its public attributes and return then as a string
representation.
# https://stackoverflow.com/questions/7900345/can-a-python-method-check-if-it-has-been-called-from-within-itself
is_recursive = len(
[ stack[-3]
for stack in traceback.extract_stack()
if stack[2] == 'recursive_get_representation'
]
) > recursive_depth

kwargs['is_recursive'] = is_recursive
return get_representation(*args, **kwargs)


def get_representation(objectname, ignore=[], emquote=False, repr=repr, is_recursive=False):
""" Iterating through all its public attributes and return then as a string representation
`ignore` a list of attributes to be ignored
`emquote` if True, puts the attributes values inside single or double quotes accordingly.
`repr` is the callback to call recursively on nested objects, can be either `repr` or `str`.
Expand All @@ -659,10 +673,14 @@ def pack_attribute(string):
if hasattr( objectname, '__dict__' ):
valid_attributes = objectname.__dict__.keys()

for attribute in valid_attributes:
if is_recursive:
clean_attributes.append( "{}".format( '<recursive>' ) )

else:
for attribute in valid_attributes:

if not attribute.startswith( '_' ) and attribute not in ignore:
clean_attributes.append( "{}: {}".format( attribute, pack_attribute( objectname.__dict__[attribute] ) ) )
if not attribute.startswith( '_' ) and attribute not in ignore:
clean_attributes.append( "{}: {}".format( attribute, pack_attribute( objectname.__dict__[attribute] ) ) )

return "%s %s;" % ( objectname.__class__.__name__, ", ".join( clean_attributes ) )

Expand Down
2 changes: 1 addition & 1 deletion all/debug_tools/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
__version__ = "2.6.13"
__version__ = "2.6.14"

0 comments on commit 20f5e0b

Please sign in to comment.