Created
July 26, 2021 06:47
-
-
Save rcoup/1a943b120c778f9c8aeb50ca0261bea3 to your computer and use it in GitHub Desktop.
Python Sequence Join Format String Wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class J: | |
""" | |
Sequence Join Format String Wrapper | |
>>> seq = ['a', 'b', 1, None] | |
>>> print(f"default: {J(seq)}" | |
default: 1,b,1,None | |
>>> print(f"custom-delimeter: {J(seq):/-}" | |
custom-delimiter: a/-b/-1/-None | |
""" | |
def __init__(self, seq): | |
self.seq = seq | |
def __format__(self, format_spec=','): | |
# format_spec is the delimiter to use | |
if self.seq: | |
return format_spec.join(str(item) for item in self.seq) | |
else: | |
return '' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment