-
Notifications
You must be signed in to change notification settings - Fork 8
/
util.py
167 lines (132 loc) · 4.27 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
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
# Copyright (C) 2014-2015 Oleh Prypin <[email protected]>
#
# This file is part of SixCells.
#
# SixCells is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# SixCells is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with SixCells. If not, see <http://www.gnu.org/licenses/>.
import sys as _sys
import os.path as _path
import math as _math
import collections as _collections
def minmax(*args, **kwargs):
return min(*args, **kwargs), max(*args, **kwargs)
def all_grouped(items, key):
"""Are all the items in one group or not?
`key` should be a function that says whether 2 items are connected."""
try:
grouped = {next(iter(items))}
except StopIteration:
return True
anything_to_add = True
while anything_to_add:
anything_to_add = False
for a in items - grouped:
if any(key(a, b) for b in grouped):
anything_to_add = True
grouped.add(a)
return len(grouped) == len(items)
def distance(a, b, squared=False):
"Distance between two items"
try:
ax, ay = a
except TypeError:
ax, ay = a.x(), a.y()
try:
bx, by = b
except TypeError:
bx, by = b.x(), b.y()
r = (ax-bx)**2 + (ay-by)**2
if not squared:
r = _math.sqrt(r)
return r
def angle(a, b=None):
"""Angle between two items: 0 if b is above a, tau/4 if b is to the right of a...
If b is not supplied, this becomes the angle between (0, 0) and a."""
try:
ax, ay = a
except TypeError:
ax, ay = a.x(), a.y()
if b is None:
return _math.atan2(ax, -ay)
try:
bx, by = b
except TypeError:
bx, by = b.x(), b.y()
return _math.atan2(bx-ax, ay-by)
Point = _collections.namedtuple('Point', 'x, y')
class Entity(object):
def __init__(self, name):
self.name = name
def __repr__(self):
return self.name
try:
_script_name = __FILE__
except NameError:
_script_name = _sys.argv[0]
_script_path = _path.dirname(_path.realpath(_path.abspath(_script_name)))
def here(*args):
return _path.join(_script_path, *args)
class cached_property(object):
"Attribute that is calculated and stored upon first access."
def __init__(self, fget):
self.__doc__ = fget.__doc__
self.fget = fget
self.attr = fget.__name__
def __get__(self, obj, objtype=None):
if obj is None:
return self
value = self.fget(obj)
obj.__dict__[self.attr] = value
return value
class setter_property(object):
"Attribute that is based only on a setter function; the getter just returns the value"
def __init__(self, fset):
self.__doc__ = fset.__doc__
self.fset = fset
self.attr = '_' + fset.__name__
def __get__(self, obj, objtype=None):
if obj is None:
return self
return getattr(obj, self.attr)
def __set__(self, obj, value):
it = self.fset(obj, value)
try:
it = iter(it)
except TypeError: pass
else:
for value in it:
setattr(obj, self.attr, value)
class event_property(setter_property):
"""An ordinary attribute that can you can get and set,
but a function without arguments is called when setting it."""
def __set__(self, obj, value):
setattr(obj, self.attr, value)
self.fset(obj)
# Python 2.7 + 3.x compatibility
try:
unicode
except NameError:
unicode = str
try:
basestring
except NameError:
basestring = (str, bytes)
if isinstance(round(0), float):
_round = round
def round(number, ndigits=None):
if ndigits is None:
return int(_round(number))
else:
return _round(number, ndigits)
def exec_(expression, globals=None, locals=None):
eval(compile(expression, '<string>', 'exec'), globals, locals)