Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Token class in _tokenizer for pretty-printing. #621

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion packaging/_tokenizer.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
import re
from typing import Dict, Generator, NoReturn, Optional
from typing import Dict, Generator, NoReturn, Optional, Tuple

from .specifiers import Specifier


class Token:
__slots__ = __match_args__ = ("name", "text", "position")

def __init__(self, name: str, text: str, position: int) -> None:
self.name = name
self.text = text
self.position = position

def to_tuple(self) -> Tuple[str, str, int]:
return tuple(getattr(self, k) for k in self.__class__.__match_args__)

def __repr__(self):
return (
self.__class__.__name__
+ "("
+ ", ".join(repr(getattr(self, k)) for k in self.__class__.__match_args__)
+ ")"
)

def __eq__(self, other):
if not isinstance(other, Token):
return NotImplemented
return self.to_tuple == other.to_tuple

def matches(self, name: str = "") -> bool:
if name and self.name != name:
return False
Expand Down