Example #1
0
def _print_instance(obj, depth=0, name=''):
    # Handle the first element of the list/dict.
    r = []
    if isinstance(obj, (list, dict)):
        if not obj:
            r = []
            return r
        if isinstance(obj, list):
            sobj = obj[0]
            key = '0'
        elif isinstance(obj, dict):
            key, sobj = next(iteritems(obj))
        if isinstance(
                sobj,
            (list, dict, int, long, string_types, np.ndarray, float)):
            r = []
        else:
            r = [(depth + 1, str(key))] + _print_instance(sobj, depth + 1)
    # Arrays do not have children.
    elif isinstance(obj, (np.ndarray, tb.EArray)):
        r = []
    # Handle class instances.
    elif hasattr(obj, '__dict__'):
        vs = vars(obj)
        if hasattr(obj, '__dir__'):
            vs.update({
                name: getattr(obj, name)
                for name in dir(obj)
                if name not in ('CLASS', 'TITLE', 'VERSION')
            })
        fields = {k: v for k, v in iteritems(vs) if not k.startswith('_')}
        r = list(
            chain(*[
                _print_instance(fields[n], depth=depth + 1, name=str(n))
                for n in sorted(iterkeys(fields))
            ]))
    else:
        r = []
    # Add the current object's display string.
    if name:
        if isinstance(obj, tb.EArray):
            s = name + ' [{dtype} {shape}]'.format(dtype=obj.dtype,
                                                   shape=obj.shape)
        elif isinstance(
                obj, (string_types, int, long, float, tuple)) or obj is None:
            s = name + ' = ' + str(obj)
        else:
            s = name
        r = [(depth, s)] + r
    return r
Example #2
0
def _print_instance(obj, depth=0, name=''):
    # Handle the first element of the list/dict.
    r = []
    if isinstance(obj, (list, dict)):
        if not obj:
            r = []
            return r
        if isinstance(obj, list):
            sobj = obj[0]
            key = '0'
        elif isinstance(obj, dict):
            key, sobj = next(iteritems(obj))
        if isinstance(sobj, (list, dict, int, long, string_types, np.ndarray, 
                      float)):
            r = []
        else:
            r = [(depth+1, str(key))] + _print_instance(sobj, depth+1)
    # Arrays do not have children.
    elif isinstance(obj, (np.ndarray, tb.EArray)):
        r = []
    # Handle class instances.
    elif hasattr(obj, '__dict__'):
        vs = vars(obj)
        if hasattr(obj, '__dir__'):
            vs.update({name: getattr(obj, name) 
                        for name in dir(obj)
                            if name not in ('CLASS', 'TITLE', 'VERSION')})
        fields = {k: v 
            for k, v in iteritems(vs) 
                if not k.startswith('_')}
        r = list(chain(*[_print_instance(fields[n], depth=depth+1, name=str(n)) 
                for n in sorted(iterkeys(fields))]))
    else:
        r = []
    # Add the current object's display string.
    if name:
        if isinstance(obj, tb.EArray):
            s = name + ' [{dtype} {shape}]'.format(dtype=obj.dtype, 
                shape=obj.shape)
        elif isinstance(obj, (string_types, int, long, float, tuple)) or obj is None:
            s = name + ' = ' + str(obj)
        else:
            s = name
        r = [(depth, s)] + r
    return r
def _get_geometry_arr(geometry):
    k = sorted(iterkeys(geometry))
    return np.array([geometry[_] for _ in k])
Example #4
0
def _get_geometry_arr(geometry):
    k = sorted(iterkeys(geometry))
    return np.array([geometry[_] for _ in k])