◐ Shell
reader mode source ↗
Skip to content
This repository was archived by the owner on Dec 2, 2021. It is now read-only.
Open
Changes from all commits
File filter
Conversations
Jump to
Diff view
Apply and reload
Show whitespace
Diff view
Apply and reload
83 changes: 78 additions & 5 deletions 10-seq-hacking/vector_v5.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,65 @@
'<4.000e+00, 1.047e+00, 9.553e-01, 7.854e-01>'
>>> format(Vector([0, 1, 0, 0]), '0.5fh')
'<1.00000, 1.57080, 0.00000, 0.00000>'
"""

from array import array
Expand Down Expand Up @@ -228,10 +287,10 @@ def __hash__(self):
return functools.reduce(operator.xor, hashes, 0)

def __abs__(self):
return math.sqrt(sum(x * x for x in self))

def __bool__(self):
return bool(abs(self))

def __len__(self):
return len(self._components)
Expand Down @@ -268,16 +327,30 @@ def angle(self, n): # <2>
def angles(self): # <3>
return (self.angle(n) for n in range(1, len(self)))

def __format__(self, fmt_spec=''):
if fmt_spec.endswith('h'): # hyperspherical coordinates
fmt_spec = fmt_spec[:-1]
coords = itertools.chain([abs(self)],
self.angles()) # <4>
outer_fmt = '<{}>' # <5>
else:
coords = self
outer_fmt = '({})' # <6>
components = (format(c, fmt_spec) for c in coords) # <7>
return outer_fmt.format(', '.join(components)) # <8>

@classmethod
Expand Down
Toggle all file notes Toggle all file annotations