Skip to content

Instantly share code, notes, and snippets.

@rcoup
Created July 26, 2021 06:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rcoup/1a943b120c778f9c8aeb50ca0261bea3 to your computer and use it in GitHub Desktop.
Save rcoup/1a943b120c778f9c8aeb50ca0261bea3 to your computer and use it in GitHub Desktop.
Python Sequence Join Format String Wrapper
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