Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Dec 2, 2021. It is now read-only.

format Vector: use ... to indicate long Vector(use '*' to showall)#18

Open
miziaiba wants to merge1 commit intofluentpython:master
base:master
Choose a base branch
Loading
frommiziaiba:master
Open
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion10-seq-hacking/vector_v5.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -268,7 +268,13 @@ def angle(self, n): # <2>
def angles(self): # <3>
return (self.angle(n) for n in range(1, len(self)))

max_format_size = 30
def __format__(self, fmt_spec=''):
showall = False
if fmt_spec.endswith('*'):
fmt_spec = fmt_spec[:-1]
showall = True

if fmt_spec.endswith('h'): # hyperspherical coordinates
fmt_spec = fmt_spec[:-1]
coords = itertools.chain([abs(self)],
Expand All@@ -277,7 +283,14 @@ def __format__(self, fmt_spec=''):
else:
coords = self
outer_fmt = '({})' # <6>
components = (format(c, fmt_spec) for c in coords) # <7>

# if truncate is needed
if (not showall) and len(self) > self.max_format_size:
components = itertools.chain((format(x, fmt_spec) for index, x in enumerate(coords)
if index < self.max_format_size), ['...'])
else:
components = (format(x, fmt_spec) for x in coords) # <7>

return outer_fmt.format(', '.join(components)) # <8>

@classmethod
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp