GNU/Linux xterm-256color zsh 309 views

I wrote a snippet for Ultisnips that helps with some of the more monotonous parts of writing a new class in Python.

Here’s the snippet, and relevant global:

global !p
def get_args(line: str):
  args = []
  for arg in map(lambda x: x.lstrip(), line.split(',')):
    temp = arg.split(':')
    attr, anno = (temp[0], temp[1].lstrip()) if len(temp) > 1 else (temp[0], None)
    args.append((attr, anno))
  return args
endglobal

snippet cl "class ..." b
class $1:
    """$2`!p
if "\n" in t[2]:
  snip >> 1
  snip += '"""'
else:
  snip.rv = '"""'`

    def __init__(self, $3):`!p
snip >> 2
for arg in get_args(t[3]):
  snip += f"self.{arg[0]} = {arg[0]}"`$0
endsnippet