-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
61 lines (46 loc) · 1.39 KB
/
util.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
51
52
53
54
55
56
57
58
59
60
61
import math
import re
from enum import Enum
from fractions import Fraction
variable_re = re.compile(r'([-|+]?\d*[/\\]*\d*[A-ZА-ЯІЇЄЙҐ]+\d+)', re.UNICODE | re.IGNORECASE)
dismantled_variable_re = re.compile(r'([-|+]?\d*[/\\]*\d*)([A-ZА-ЯІЇЄЙҐ]+)(\d+)', re.UNICODE | re.IGNORECASE)
sign_re = re.compile(r'<=|<|=|>=|>', re.UNICODE)
class Sign(Enum):
LE = '<='
EQ = '='
GE = '>='
def compare(self, left, right):
if self is Sign.LE:
return left <= right
elif self is Sign.EQ:
return left == right
elif self is Sign.GE:
return left >= right
else:
raise ValueError(f'Unknown sign {self}')
def flipped(self):
if self is self.LE:
return Sign.GE
elif self is self.GE:
return Sign.LE
return self.EQ
def __str__(self) -> str:
return self.value
class LpType(Enum):
MIN = 'min'
MAX = 'max'
def __str__(self):
return self.value
class Status(Enum):
UNSOLVED = 'unsolved'
OPTIMAL = 'optimal'
INFEASIBLE = 'infeasible'
UNDEFINED = 'undefined'
def __str__(self) -> str:
return self.value
def get_fractional_part(number: int | float | Fraction) -> int | float | Fraction:
if number < 0:
return number + math.ceil(abs(number))
elif number == 1:
return 1
return number - int(number)