Skip to content

Instantly share code, notes, and snippets.

@mvandermeulen
Forked from htv2012/repr.py
Created August 17, 2025 03:19
Show Gist options
  • Save mvandermeulen/5b8439f62e0eb3f91c60abca86ec6adc to your computer and use it in GitHub Desktop.
Save mvandermeulen/5b8439f62e0eb3f91c60abca86ec6adc to your computer and use it in GitHub Desktop.
Create a __repr__ for a class
#!/usr/bin/env python3
"""
Generate the repr for a class
"""
import argparse
import io
import subprocess
import platform
def format_attribute(name):
return ' f"%s={self.%s!r}"\n' % (name, name)
def generate_repr(attributes):
sep = ' f", "\n'
buffer = io.StringIO()
buffer.write(" def __repr__(self):\n")
buffer.write(" return (\n")
buffer.write(' f"{self.__class__.__name__}("\n')
buffer.write(sep.join(format_attribute(name) for name in attributes))
buffer.write(' f")"\n')
buffer.write(" )\n")
return buffer.getvalue()
def copy_to_clipboard(text):
system = platform.system()
command = []
if system == "Darwin":
command = ["pbcopy"]
elif system == "Linux":
command = ["xsel", "-b"]
if command:
subprocess.run(command, text=True, input=text)
print("The above is copied to clipboard")
def main():
parser = argparse.ArgumentParser()
parser.add_argument("attributes", nargs="+")
options = parser.parse_args()
text = generate_repr(options.attributes)
print(text)
copy_to_clipboard(text)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment