This repository has been archived by the owner on Jul 27, 2022. It is now read-only.
forked from rosejn/lua-pprint
-
Notifications
You must be signed in to change notification settings - Fork 17
/
init.lua
367 lines (292 loc) · 8.01 KB
/
init.lua
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
--[[ Pretty Printing for Torch and Lua, main module.
Code adapted from inspect.lua, here (https://github.com/kikito/inspect.lua).
* changed some formatting, removed numbering of elements
* added support for tensors
* changed various function names
]]
require 'torch'
-- Wraps quoted strings in apostraphes
local function smart_quote(str)
if string.match( string.gsub(str, "[^'\"]", ""), '^"+$' ) then
return "'" .. str .. "'"
end
return string.format("%q", str)
end
local control_chars_translation = {
["\a"] = "\\a", ["\b"] = "\\b", ["\f"] = "\\f", ["\n"] = "\\n",
["\r"] = "\\r", ["\t"] = "\\t", ["\v"] = "\\v", ["\\"] = "\\\\"
}
local function unescape_char(c)
return control_chars_translation[c]
end
local function unescape(str)
local result, _ = string.gsub( str, "(%c)", unescape_char )
return result
end
local function is_identifier(str)
return string.match( str, "^[_%a][_%a%d]*$" )
end
local function is_array_key(k, length)
return type(k)=='number' and 1 <= k and k <= length
end
local function is_dictionary_key(k, length)
return not is_array_key(k, length)
end
local sort_orders_by_type = {
['number'] = 1, ['boolean'] = 2, ['string'] = 3, ['table'] = 4,
['tensor'] = 5, ['function'] = 6, ['userdata'] = 7, ['thread'] = 8
}
local function sort_keys(a,b)
local ta, tb = type(a), type(b)
if ta ~= tb then return sort_orders_by_type[ta] < sort_orders_by_type[tb] end
if ta == 'string' or ta == 'number' then return a < b end
return false
end
local function get_dictionary_keys(t)
local length = #t
local keys = {}
for k, _ in pairs(t) do
if is_dictionary_key(k, length) then table.insert(keys, k) end
end
table.sort(keys, sort_keys)
return keys
end
local function get_to_string_result_safely(t, mt)
local __tostring = type(mt) == 'table' and mt.__tostring
local string, status
if type(__tostring) == 'function' then
status, string = pcall(__tostring, t)
string = status and string or 'error: ' .. tostring(string)
end
return string
end
local Printer = {}
function Printer:init(depth, inline)
local pprintor = {
buffer = {},
depth = depth or 0,
inline = inline or false,
level = 0,
counters = {
['function'] = 0,
['userdata'] = 0,
['thread'] = 0,
['table'] = 0
},
pools = {
['function'] = setmetatable({}, {__mode = "kv"}),
['userdata'] = setmetatable({}, {__mode = "kv"}),
['thread'] = setmetatable({}, {__mode = "kv"}),
['table'] = setmetatable({}, {__mode = "kv"})
}
}
setmetatable(pprintor, {
__index = Printer,
__tostring = function(instance) return table.concat(instance.buffer) end
} )
return pprintor
end
function Printer:new(v, depth, inline)
local p = self:init(depth, inline)
return p:put_value(v)
end
function Printer:puts(...)
local args = {...}
for i = 1, #args do
table.insert(self.buffer, tostring(args[i]))
end
return self
end
function Printer:tabify()
if self.inline then
self:puts(" ")
else
self:puts("\n", string.rep(" ", self.level))
end
return self
end
function Printer:up()
self.level = self.level - 1
end
function Printer:down()
self.level = self.level + 1
end
function Printer:put_comma(comma)
if comma then self:puts(',') end
return true
end
function Printer:put_table(t)
if self:already_seen(t) then
self:puts('<table>')
elseif self.level >= self.depth then
self:puts('{...}')
else
self:down()
local length = #t
local mt = getmetatable(t)
self:puts('{')
local string = get_to_string_result_safely(t, mt)
if type(string) == 'string' and #string > 0 then
self:puts(' -- ', unescape(string))
if length >= 1 then self:tabify() end -- tabify the array values
end
local comma = false
for i = 1, length do
comma = self:put_comma(comma)
self:puts(' '):put_value(t[i])
end
local dict_keys = get_dictionary_keys(t)
for _, k in ipairs(dict_keys) do
comma = self:put_comma(comma)
self:tabify():put_key(k):puts(' = '):put_value(t[k])
end
if mt then
comma = self:put_comma(comma)
self:tabify():puts('<metatable> = '):put_value(mt)
end
self:up()
if #dict_keys > 0 or mt then -- dictionary table. Justify closing }
self:tabify()
elseif length > 0 then -- array tables have one extra space before closing }
self:puts(' ')
end
self:puts('}')
end
return self
end
function Printer:put_tensor(t)
local size = t:nElement()
if size <= 20 then
self:puts(tostring(t))
else
self:puts('[' .. torch.typename(t) .. ' of dimension ')
self:put_tensor_dims(t)
self:puts(']')
end
end
function Printer:put_storage(t)
local size = t:size()
if size <= 20 then
self:puts(tostring(t))
else
self:puts('[' .. torch.typename(t) .. ' of size ')
self:puts(size)
self:puts(']')
end
end
function Printer:put_tensor_dims(t)
local n_dims = t:dim()
local dim_sizes = t:size()
if n_dims == 0 then
self:puts('[0-dimensional tensor]')
else
for i = 1, n_dims-1 do
self:puts(dim_sizes[i]):puts('x')
end
self:puts(dim_sizes[n_dims])
end
end
function Printer:already_seen(v)
local tv = type(v)
return self.pools[tv][v] ~= nil
end
function Printer:get_or_create_counter(v)
local tv = type(v)
local current = self.pools[tv][v]
if not current then
current = self.counters[tv] + 1
self.counters[tv] = current
self.pools[tv][v] = current
end
return current
end
function Printer:put_value(v)
local tv = type(v)
if tv == 'string' then
self:puts(smart_quote(unescape(v)))
elseif tv == 'number' or tv == 'boolean' or tv == 'nil' then
if v == math.huge then
self:puts('math.huge')
elseif v == -math.huge then
self:puts('-math.huge')
else
self:puts(tostring(v))
end
elseif tv == 'table' then
self:put_table(v)
elseif torch.isTensor(v) then
self:put_tensor(v)
elseif torch.isStorage(v) then
self:put_storage(v)
else
self:puts('<',tv,'>')
end
return self
end
function Printer:put_key(k)
if type(k) == "string" and is_identifier(k) then
return self:puts(k)
end
return self:puts( "[" ):put_value(k):puts("]")
end
local function print_tensor_dimensions(t)
local p = Printer.init()
p:put_tensor_dims(t)
return tostring(p)
end
local function print_tensor_info(t)
local p = Printer.init()
-- if we find a table, recursively call info on its elements
if type(t) == 'table' then
local newline = ' '
if #t > 1 then newline = '\n' end
p:puts('{' .. newline)
for i = 1, #t do
p:puts(print_tensor_info(t[i]))
p:puts(newline)
end
p:puts('}')
else
if t == nil then p:puts("[nil]") end
p:puts('['):put_tensor_dims(t)
if t.min then p:puts(', min: '):put_value(t:min()) end
if t.mean then p:puts(', mean: '):put_value(t:mean()) end
if t.max then p:puts(', max: '):put_value(t:max()) end
if t.type then p:puts(', type: '):put_value(t:type()) end
p:puts(']')
end
return tostring(p)
end
local function pretty_string(t, depth, inline)
depth = depth or 4
inline = inline or false
return tostring(Printer:new(t, depth, inline))
end
-- Returns an inline string.
local function string(t, depth)
return pretty_string(t, depth, true)
end
local function pprint_pprint(self, data, depth)
print(pretty_string(data, depth))
end
pprint = {
pretty_string = pretty_string, -- multi-line string
string = string, -- inline string
dims = print_tensor_dimensions,
info = print_tensor_info,
__call = pprint_pprint,
printer = function(depth)
depth = depth or 4
local function pretty_string(...)
local str_table = {}
for i = 1, select('#', ...) do
local obj = select(i, ...)
table.insert(str_table, tostring(Printer:new(obj, depth)))
end
return table.concat(str_table, ' ')
end
return pretty_string
end
}
setmetatable(pprint, pprint)
return pprint