MCPcopy Index your code
hub / github.com/RustPython/RustPython / _format

Function _format

Lib/ast.py:127–184  ·  view source on GitHub ↗
(node, level=0)

Source from the content-addressed store, hash-verified

125 will be omitted from the output for better readability.
126 """
127 def _format(node, level=0):
128 if indent is not None:
129 level += 1
130 prefix = '\n' + indent * level
131 sep = ',\n' + indent * level
132 else:
133 prefix = ''
134 sep = ', '
135 if isinstance(node, AST):
136 cls = type(node)
137 args = []
138 args_buffer = []
139 allsimple = True
140 keywords = annotate_fields
141 for name in node._fields:
142 try:
143 value = getattr(node, name)
144 except AttributeError:
145 keywords = True
146 continue
147 if value is None and getattr(cls, name, ...) is None:
148 keywords = True
149 continue
150 if not show_empty:
151 if value == []:
152 field_type = cls._field_types.get(name, object)
153 if getattr(field_type, '__origin__', ...) is list:
154 if not keywords:
155 args_buffer.append(repr(value))
156 continue
157 if not keywords:
158 args.extend(args_buffer)
159 args_buffer = []
160 value, simple = _format(value, level)
161 allsimple = allsimple and simple
162 if keywords:
163 args.append('%s=%s' % (name, value))
164 else:
165 args.append(value)
166 if include_attributes and node._attributes:
167 for name in node._attributes:
168 try:
169 value = getattr(node, name)
170 except AttributeError:
171 continue
172 if value is None and getattr(cls, name, ...) is None:
173 continue
174 value, simple = _format(value, level)
175 allsimple = allsimple and simple
176 args.append('%s=%s' % (name, value))
177 if allsimple and len(args) <= 3:
178 return '%s(%s)' % (node.__class__.__name__, ', '.join(args)), not args
179 return '%s(%s%s)' % (node.__class__.__name__, prefix, sep.join(args)), False
180 elif isinstance(node, list):
181 if not node:
182 return '[]', True
183 return '[%s%s]' % (prefix, sep.join(_format(x, level)[0] for x in node)), False
184 return repr(node), True

Callers 1

dumpFunction · 0.70

Calls 8

isinstanceFunction · 0.85
getattrFunction · 0.85
reprFunction · 0.85
lenFunction · 0.85
getMethod · 0.45
appendMethod · 0.45
extendMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected