Skip to content

Instantly share code, notes, and snippets.

@yeiichi
Last active April 24, 2025 03:32
Show Gist options
  • Save yeiichi/9690a0675a3d0551cd2d1d49ee904006 to your computer and use it in GitHub Desktop.
Save yeiichi/9690a0675a3d0551cd2d1d49ee904006 to your computer and use it in GitHub Desktop.
Display Unicode information for input string.
#!/usr/bin/env python3
"""Display Unicode information for input string."""
import sys
import unicodedata
from argparse import ArgumentParser
from dataclasses import dataclass
from typing import List, TextIO
@dataclass
class CharacterInfo:
"""Stores information about a single character."""
character: str
name: str
code: str
def __str__(self) -> str:
"""Returns string representation of character information."""
return f'{self.character}: {self.name}: {self.code}'
class DisplayFormatter:
"""Handles the display formatting and output of character analysis."""
def __init__(self, output_stream: TextIO = sys.stdout):
"""Initialize with an output stream.
Args:
output_stream: Stream to write output to (defaults to stdout)
"""
self.output_stream = output_stream
def display_results(self, char_infos: List[CharacterInfo]) -> None:
"""Display the analysis results for all characters.
Args:
char_infos: List of character information to display
"""
for char_info in char_infos:
print(str(char_info), file=self.output_stream)
class CharacterAnalyzer:
"""Analyzes characters in a string and provides their Unicode information."""
def __init__(self, text: str):
"""Initialize with input text.
Args:
text: Input string to analyze
"""
self.text = text
def analyze_characters(self) -> List[CharacterInfo]:
"""Analyze each character in the input string.
Returns:
List of CharacterInfo objects containing character details
"""
return [
CharacterInfo(
character=char,
name=unicodedata.name(char),
code=hex(ord(char))
)
for char in self.text
]
def parse_arguments() -> str:
"""Parse command line arguments.
Returns:
Input string to analyze
Raises:
SystemExit: If no input string is provided
"""
parser = ArgumentParser(description='Display Unicode information for input string')
parser.add_argument('input_string', nargs='*', help='Input string to analyze')
args = parser.parse_args()
input_text = ' '.join(args.input_string)
if not input_text:
parser.error("No input string provided. Please provide at least one character to analyze.")
return input_text
def main() -> None:
"""Main entry point of the script."""
input_text = parse_arguments()
analyzer = CharacterAnalyzer(input_text)
formatter = DisplayFormatter()
formatter.display_results(analyzer.analyze_characters())
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment