random

- collection of un-sorted bollocks
git clone git://git.acid.vegas/random.git
Log | Files | Refs | Archive

uniblocks.py (1357B)

      1 import unicodedata
      2 import requests
      3 
      4 def fetch_unicode_blocks(url):
      5     response = requests.get(url)
      6     response.raise_for_status()  # Raise an error if the request failed
      7     lines = response.text.split('\n')
      8     blocks = []
      9     for line in lines:
     10         if line.startswith('#') or line.strip() == '':
     11             continue  # Skip comments and empty lines
     12         range_part, block_name = line.strip().split('; ')
     13         start, end = range_part.split('..')
     14         blocks.append((int(start, 16), int(end, 16), block_name))
     15     return blocks
     16 
     17 def is_printable(char):
     18     category = unicodedata.category(char)
     19     return not category.startswith('C') and not category in ['Zl', 'Zp']
     20 
     21 def print_block_characters(start, end, block_name):
     22     print(f"Printing block: {block_name} (U+{start:04X} to U+{end:04X})")
     23     for code_point in range(start, end + 1):
     24         char = chr(code_point)
     25         if is_printable(char):
     26             try:
     27                 print(char, end=' ')
     28             except UnicodeEncodeError:
     29                 pass  # Skip characters that cannot be encoded by the terminal
     30     input()
     31 
     32 def main(url):
     33     blocks = fetch_unicode_blocks(url)
     34     for start, end, block_name in blocks:
     35         print_block_characters(start, end, block_name)
     36 
     37 if __name__ == "__main__":
     38     url = 'http://www.unicode.org/Public/UNIDATA/Blocks.txt'
     39     main(url)