Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

revert changes to __pretty_str__; closes #14

  • Loading branch information...
commit 393126944ad6dba5cfa157c9bc9afc7adbb24298 1 parent 287a1e5
Tomer Filiba tomerfiliba authored

Showing 1 changed file with 63 additions and 9 deletions. Show diff stats Hide diff stats

  1. +63 9 construct/lib/container.py
72 construct/lib/container.py
@@ -3,7 +3,7 @@
3 3 """
4 4
5 5 from collections import MutableMapping
6   -from pprint import pformat
  6 +
7 7
8 8 def recursion_lock(retval, lock_name = "__recursion_lock__"):
9 9 def decorator(func):
@@ -82,8 +82,29 @@ def __iter__(self):
82 82 def __repr__(self):
83 83 return "%s(%s)" % (self.__class__.__name__, repr(self.__dict__))
84 84
  85 + #def __str__(self):
  86 + # return "%s(%s)" % (self.__class__.__name__, str(self.__dict__))
  87 +
85 88 def __str__(self):
86   - return "%s(%s)" % (self.__class__.__name__, str(self.__dict__))
  89 + return self.__pretty_str__()
  90 +
  91 + @recursion_lock("<...>")
  92 + def __pretty_str__(self, nesting = 1, indentation = " "):
  93 + attrs = []
  94 + ind = indentation * nesting
  95 + for k, v in self.items():
  96 + if not k.startswith("_"):
  97 + text = [ind, k, " = "]
  98 + if hasattr(v, "__pretty_str__"):
  99 + text.append(v.__pretty_str__(nesting + 1, indentation))
  100 + else:
  101 + text.append(repr(v))
  102 + attrs.append("".join(text))
  103 + if not attrs:
  104 + return "%s()" % (self.__class__.__name__,)
  105 + attrs.insert(0, self.__class__.__name__ + ":")
  106 + return "\n".join(attrs)
  107 +
87 108
88 109 class FlagsContainer(Container):
89 110 """
@@ -93,21 +114,46 @@ class FlagsContainer(Container):
93 114 """
94 115
95 116 @recursion_lock("<...>")
96   - def __str__(self):
97   - d = dict((k, self[k]) for k in self
98   - if self[k] and not k.startswith("_"))
99   - return "%s(%s)" % (self.__class__.__name__, pformat(d))
  117 + def __pretty_str__(self, nesting = 1, indentation = " "):
  118 + attrs = []
  119 + ind = indentation * nesting
  120 + for k in self.keys():
  121 + v = self.__dict__[k]
  122 + if not k.startswith("_") and v:
  123 + attrs.append(ind + k)
  124 + if not attrs:
  125 + return "%s()" % (self.__class__.__name__,)
  126 + attrs.insert(0, self.__class__.__name__+ ":")
  127 + return "\n".join(attrs)
  128 +
100 129
101 130 class ListContainer(list):
102 131 """
103 132 A container for lists.
104 133 """
105   -
106 134 __slots__ = ["__recursion_lock__"]
107 135
108   - @recursion_lock("[...]")
109 136 def __str__(self):
110   - return pformat(self)
  137 + return self.__pretty_str__()
  138 +
  139 + @recursion_lock("[...]")
  140 + def __pretty_str__(self, nesting = 1, indentation = " "):
  141 + if not self:
  142 + return "[]"
  143 + ind = indentation * nesting
  144 + lines = ["["]
  145 + for elem in self:
  146 + lines.append("\n")
  147 + lines.append(ind)
  148 + if hasattr(elem, "__pretty_str__"):
  149 + lines.append(elem.__pretty_str__(nesting + 1, indentation))
  150 + else:
  151 + lines.append(repr(elem))
  152 + lines.append("\n")
  153 + lines.append(indentation * (nesting - 1))
  154 + lines.append("]")
  155 + return "".join(lines)
  156 +
111 157
112 158 class LazyContainer(object):
113 159
@@ -159,3 +205,11 @@ def _get_value(self):
159 205 value = property(_get_value)
160 206
161 207 has_value = property(lambda self: self._value is not NotImplemented)
  208 +
  209 +
  210 +
  211 +if __name__ == "__main__":
  212 + c = Container(a = 5, b = 6)
  213 + print c
  214 +
  215 +

0 comments on commit 3931269

Please sign in to comment.
Something went wrong with that request. Please try again.