Skip to content

Instantly share code, notes, and snippets.

@graingert
Last active April 11, 2022 09:15
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 graingert/500e0c9ec7fe45f91993fe3b73877df3 to your computer and use it in GitHub Desktop.
Save graingert/500e0c9ec7fe45f91993fe3b73877df3 to your computer and use it in GitHub Desktop.
import dataclasses
import operator
@dataclasses.dataclass
class WarningFilter:
command: str
msg: str
cls: str
module: str | None = None
def format(self):
prefix = f"{self.command}:{self.msg}:{self.cls}"
return prefix if self.module is None else f"{prefix}:{self.module}"
def sort_key(self):
return (self.module is not None, self.module, self.cls, self.msg)
@classmethod
def from_format(cls, fmt):
return cls(*fmt.strip().split(":"))
def warn_sort(v):
return "\n".join(
h.format()
for h in sorted(
(WarningFilter.from_format(x) for x in v.split("\n") if x.strip()),
key=operator.methodcaller("sort_key"),
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment