opath.objchain

Chaining methods for chaining nested objects together

Functions

chainable_list(fxn) Decorator that turns a returned value from a list to a ChainList (if possible)
copy(x) Shallow copy operation on arbitrary Python objects.
wraps(wrapped[, assigned, updated]) Decorator factory to apply update_wrapper() to a wrapper function

Classes

ChainList List-like class that collects attributes and applies functions but functions like a list in every other regard.
ObjChain([push_up, check_attr]) A tree-like class for chaining commands and attributes together with special root/head handling.
class opath.objchain.ChainList[source]

Bases: list

List-like class that collects attributes and applies functions but functions like a list in every other regard.

m1 = ChainList(["string1   ", "   string2"])
m1.strip().upper() # ["STRING1", "STRING2"]
append(object) → None -- append object to end
clear() → None -- remove all items from L
copy() → list -- a shallow copy of L
count(value) → integer -- return number of occurrences of value
extend(iterable) → None -- extend list by appending elements from the iterable
index(value[, start[, stop]]) → integer -- return first index of value.

Raises ValueError if the value is not present.

insert()

L.insert(index, object) – insert object before index

pop([index]) → item -- remove and return item at index (default last).

Raises IndexError if list is empty or index is out of range.

remove(value) → None -- remove first occurrence of value.

Raises ValueError if the value is not present.

reverse()

L.reverse() – reverse IN PLACE

sort(key=None, reverse=False) → None -- stable sort *IN PLACE*
class opath.objchain.ObjChain(push_up=False, check_attr=True)[source]

Bases: object

A tree-like class for chaining commands and attributes together with special root/head handling.

Each attribute creates a new ObjChain instance (a ‘node’) which acts like a node in a linked list.

root.child1.child2.child3 # etc.

Chainer constructor

Parameters:
  • parent (ObjChain) – parent node that called this object
  • push_up (boolean) – whether to push up node to the root node by default
_add(attr, child, push_up=None, check_attr=None)[source]

Adds child node to this node.

Parameters:
  • attr (str) – name to use to reference the child
  • child (ObjChain) – child node to add
  • push_up (boolean) – whether to add the child node to the root node. If True, the child will be able to be accessed from the root node.
  • check_attr (boolean or None) – if True, will raise exception if attr is not a valid attribute. If None, value will default to defaults defined on initialization
Returns:

the child node

Return type:

ObjChain

_add_grandchild(child)[source]

Adds a node to the roots grandchildren

_create_and_add_child(attr, with_attributes=None, push_up=None, check_attr=None)[source]

Copy this node and adds the node as a child

Parameters:
  • attr (str) – name of the new node
  • with_attributes (dict) – attribute to apply to the new node
  • push_up (boolean) – whether to push the new node to root.
  • check_attr (boolean or None) – if True, will raise exception if attr is not a valid attribute. If None, value will default to defaults defined on initialization
Returns:

the newly added child node

Return type:

ObjChain

_create_child(with_attributes=None)[source]

Create a new copy node with with a set of attributes

Parameters:with_attributes (dict) – list of attributes to apply to child
Returns:child node
Return type:ObjChain
_opts(**opts)[source]

Returns the options dictionary. If passed opts has a None value, default option is used.

_remove_child(attr)[source]

Removes a child from this node

Parameters:attr (str) – the attribute name of the node
Returns:the removed child, else return None
Return type:ObjChain or None
_sanitize_identifier(iden)[source]

Validates the identifier to ensure it is not a reserve keyword used in python (‘which’, ‘in’, ‘class’, ‘else’, etc.). Other strings such as ‘something.else’ that cannot be translated into an attribute are also disallowed.

_update_grandchildren(push_up)[source]

Updates accessible children

_validate_attr(attr, push_up=None)[source]

Validates the attribute name for a node and checks if that attribute (i) already exists or (ii) already exists in the root attributes (if push_up=True)

param attr: attribute name for potential node :type attr: str :param push_up: whether to validate if the attribute exists in the list of root attributes :type push_up: boolean :return: None :rtype: None

ancestors(include_self=False)[source]

All ancestral nodes

Parameters:include_self (boolean) – Whether to include this node in the return list
Returns:list of ancestor nodes (list of ObjChain instances)
Return type:list
attr

The name for this node from the parent’s reference.

children

This nodes descendent notes

descendents(include_self=False)[source]

All descendent nodes

Parameters:include_self (boolean) – Whether to include this node in the return list
Returns:list of descendent nodes (list of ObjChain instances)
Return type:list
get(attr)[source]

Short for getattr(self, attr)

has(attr)[source]

Short for hasattr(self, attr)

is_root()[source]

Whether this node is the root/head node

parent

This nodes parent

remove_parent(push_up=None)[source]

Remove this node’s parent, effectively breaking the chain

root

The root/head node

opath.objchain.chainable_list(fxn)[source]

Decorator that turns a returned value from a list to a ChainList (if possible)