summaryrefslogtreecommitdiff
path: root/graphics/asymptote/GUI/xasyUtils.py
blob: 04e327bb44329668a65bbc3fa989830eda8841b0 (plain)
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
#!/usr/bin/env python3

import re
import typing as typing
import math
import itertools

def tuple2StrWOspaces(val: tuple) -> str:
    newStr = ','.join(['{:.6g}'.format(value) for value in val])
    return '({0})'.format(newStr)

def tryParse(val, typ=float):
    try:
        return typ(val)
    except ValueError:
        return None

def funcOnList(list1: typing.Union[typing.List, typing.Tuple], list2: typing.Union[typing.List, typing.Tuple], func: typing.Callable) -> tuple:
    """Returns [f(x[i], y[i]) : i in 1, ..., n - 1] in order with f as func
    and x and y as list1 and 2. """

    assert len(list1) == len(list2)
    return tuple([func(list1[i], list2[i]) for i in range(len(list1))])


def listize(str, typ, delim='()') -> list:
    str = str.strip(delim)
    raw_elem = str.split(',')
    final_list = []
    if isinstance(typ, (list, tuple)):
        for i in range(len(raw_elem)):
            if i < len(typ):
                curr_typ = typ[i]
            else:
                curr_typ = typ[-1]
            final_list.append(curr_typ(raw_elem[i].strip()))
    else:
        for elem in raw_elem:
            final_list.append(typ(elem.strip()))
    return final_list

def twonorm(vec: typing.Iterable[typing.Union[float, int]]) -> float:
    rawSquared = sum(map(lambda x: x*x, vec))
    return math.sqrt(rawSquared)

def tryParseKey(raw_key):
    """Returns None if raw key is not in #.# format"""
    # See https://regex101.com/r/6G9MZD/1/
    # for the regex data
    return re.fullmatch(r'^(\d+)\.(\d+)$', raw_key)