The Ugliest Hack I’ve Written So Far

Posted on by Chris Warburton

‘raise’+‘,’.join([t[0] for t in [[e] for n,e in enumerate([a.expr3,a.expr2,a.expr1]) if e is not None or any([a.expr1,a.expr2,a.expr3][-(n+1):])] if (t[0] is not None and t.__setitem__(0,t[0].rec(i))) or (t[0] is None and t.__setitem__(0, ‘None’)) or True][::-1])

This is a 1-line PyMeta rule which means the following:

Define a rule named “raise” with an amount of indentation “i”, which applies to anything, which we’ll call “a”, as long as “a” is a type of ‘Raise’. Upon finding such a thing we should output a string ‘raise’ followed by the first item of every list in the set of singleton lists of “a”‘s attributes ’expr3’, ‘expr2’ and ‘expr1’ when reversed which is either not equal to “None” or else comes after a non-None attribute, if the element of these lists is not None and swapping the first element for its contents recursively at the same indentation level, or if it is None then replacing it with the string “None”.

If I were writing this normally it would be something much cleaner like:

def raise(i, a):
  if a.__class__ == Raise:
    to_return = ‘raise’
    attribs = [a.expr3, a.expr2, a.expr1]
    to_keep = []
    not_end = False
    for att in attribs:
      if att is None or not_end:
      elif (not att is None) and (not not_end):
        not_end = True
    to_return = to_return+‘,’.join(to_keep)
    return to_return

but the default PyMeta grammar only allows a single line of Python in the output. Whilst the point of OMeta is that I can subclass and rewrite it to work in whatever way I want, I can’t get the hang of subclassing grammars yet, and hence this mind-bending, yet at least partially elegant, functional approach.