-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
50 lines (41 loc) · 1.51 KB
/
utils.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import numpy
PIXELS_IN_BIG_FILE = 50 * 1000 * 1000 # 50 megapixels
def to_hsv_matrix(matrix, hues):
"""
The matrix is a 2D array of uint8's. The hues are either None or another 2D
array of the same shape.
We return a 3D array representing an HSV image of the matrix, optionally
colored by the hues.
"""
result = numpy.zeros([*matrix.shape, 3], numpy.uint8)
result[:, :, 2] = matrix * 255
if hues is not None:
result[:, :, 0] = hues
result[:, :, 1] = 255 # Saturation
return result
def make_matrix(tokens_a, tokens_b):
matrix = numpy.zeros([len(tokens_a), len(tokens_b)], dtype=numpy.uint8)
for i, value in enumerate(tokens_a):
matrix[i, :] = (tokens_b == value)
return matrix
def guess_language(filename):
file_type = filename.split(".")[-1]
known_types = { # Sorted by language (sorted by value, not key!)
"c": "c",
"h": "cpp", # Might be C or C++, err on the side of caution
"cc": "cpp",
"hh": "cpp",
"cpp": "cpp",
"hpp": "cpp",
"go": "go",
"hs": "haskell",
"js": "javascript",
"py": "python",
"svelte": "svelte",
"ts": "typescript",
}
expected_language = known_types.get(file_type)
if expected_language is not None:
return expected_language
raise ValueError(f"Cannot infer language for unknown file extension "
f"'.{file_type}'. Set language explicitly")