-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_r.py
31 lines (29 loc) · 1.11 KB
/
print_r.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import sys
def print_r(var, indent=0):
"""Equivalent of PHP's print_r for Python 2 and 3."""
spacing = ' ' * indent
if isinstance(var, dict):
print('{}{{'.format(spacing))
for key, value in var.items():
print('{} [{}] => '.format(spacing, repr(key)))
print_r(value, indent + 4)
print('{}}}'.format(spacing))
elif isinstance(var, (list, tuple, set)):
print('{}{}'.format(spacing, type(var).__name__))
print('{}['.format(spacing))
for item in var:
print_r(item, indent + 4)
print('{}]'.format(spacing))
elif isinstance(var, str):
if sys.version_info[0] < 3: # Python 2
print('{}"{}"'.format(spacing, var.encode('utf-8')))
else: # Python 3
print('{}"{}"'.format(spacing, var))
elif isinstance(var, (int, float, bool)):
print('{}{}'.format(spacing, var))
elif var is None:
print('{}None'.format(spacing))
else:
print('{}{}'.format(spacing, repr(var)))
# Example usage:
print_r({"name": "John", "age": 30, "scores": [10, 20, 30], "is_active": True})