-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.py
More file actions
141 lines (110 loc) · 3.54 KB
/
Copy pathexample.py
File metadata and controls
141 lines (110 loc) · 3.54 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from sys import stderr
from crab_dbg import dbg
class Node:
def __init__(self, val=0, next_=None, prev=None):
self.val = val
self.next = next_
self.prev = prev
class DoubleLinkedList:
def __init__(self, head=None, tail=None):
self.head = head
self.tail = tail
@staticmethod
def create(n: int) -> "DoubleLinkedList":
"""
Create a LinkedList of n elements, value ranges from 0 to n - 1.
"""
if n <= 0:
raise ValueError("A double linked list with %d element is meaningless", n)
head = Node(0)
cur = head
for i in range(1, n):
new_node = Node(i)
cur.next = new_node
new_node.prev = cur
cur = cur.next
return DoubleLinkedList(head=head, tail=cur)
class Phone:
def __init__(self, brand, color, price):
self.brand = brand
self.color = color
self.price = price
def __repr__(self):
return "Phone:\n Color: %s\n Brand: %s\n Price: %s\n" % (
self.color,
self.brand,
self.price,
)
class Stack:
def __init__(self):
self.data = []
def push(self, item):
self.data.append(item)
def pop(self):
return self.data.pop()
if __name__ == "__main__":
pi = 3.14
ultimate_answer = 42
flag = True
stock_price = [100, 99, 101, 1]
fruits = {"apple", "peach", "watermelon"}
country_to_capital_cities = {
"China": "Beijing",
"United Kingdom": "London",
"Liyue": "Liyue Harbor",
}
# You can use dbg to inspect a lot of variables.
dbg(
pi,
1 + 1,
sorted(stock_price),
"This string contains (, ' and ,",
ultimate_answer,
flag, # You can leave a comment here as well, dbg() won't show this comment.
stock_price,
fruits,
country_to_capital_cities,
)
# Or, you can use dbg to inspect one. Note that you can pass any keyword arguments originally supported by print()
dbg(country_to_capital_cities, file=stderr)
# You can also use dbg to inspect expressions.
dbg(1 + 1)
# When used with objects, it will show all fields contained by that object.
double_linked_list = DoubleLinkedList.create(2)
dbg(double_linked_list)
# dbg() works with lists, tuples, and dictionaries.
dbg(
[double_linked_list, double_linked_list],
(double_linked_list, double_linked_list),
{"a": 1, "b": [double_linked_list]},
[
1,
2,
3,
4,
],
)
# For even more complex structures, it works as well.
stack = Stack()
stack.push(double_linked_list)
stack.push(double_linked_list)
dbg(stack)
dbg("What if my input is a string?")
# If your type has its own __repr__ or __str__ implementation, no worries, crab_dbg will jut use it.
phone = Phone("Apple", "white", 1099)
dbg(phone)
dbg({"my_phones": [phone]})
# If you are extremely bored.
infinite_list = []
infinite_list.append(infinite_list)
dbg(infinite_list)
# If invoked without arguments, then it will just print the filename and line number.
dbg()
import numpy as np
# This library can also be used with your favorite data science libraries if you enabled our optional features.
ndarray = np.array([[1, 2, 3], [4, 5, 6]])
dbg(ndarray)
# And yes, even deeply nested ndarray is possible.
stack = Stack()
stack.push({"dict_key": ndarray})
dbg(stack)