Guest User

Untitled

a guest
Dec 22nd, 2021
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | None | 0 0
  1. import argparse
  2. import regex as re
  3.  
  4. from bitstring import BitArray
  5.  
  6.  
  7. def decode(string):
  8.     sym = {'ПУК': '0', 'СРЕНЬК': '1'}
  9.  
  10.     return BitArray(''.join(['0b'] + [sym[s]*int(c and c or 1) for (c, s) in re.findall(r'(\d*)(\w+)-?', string)])).bytes.decode()
  11.  
  12.  
  13. def encode(string):
  14.     sym = {'0': 'ПУК', '1': 'СРЕНЬК'}
  15.  
  16.     return '-'.join([f'{len(g) > 1 and len(g) or ""}{sym[g[0]]}' for (g, *_) in re.findall(r'(?|(0+)|(1+))', BitArray(string.encode()).bin)])
  17.  
  18.  
  19. if __name__ == '__main__':
  20.     parser = argparse.ArgumentParser(description='Encode and decode 2ch.hk fart code.')
  21.  
  22.     parser.add_argument('string', metavar='string', type=str,
  23.                         help='string for process')
  24.  
  25.     function = parser.add_mutually_exclusive_group(required=True)
  26.  
  27.     function.add_argument('-d', '--decode', dest='function', action='store_const',
  28.                           const=decode, help='decode string')
  29.  
  30.     function.add_argument('-e', '--encode', dest='function', action='store_const',
  31.                           const=encode, help='encode string')
  32.  
  33.     args = parser.parse_args()
  34.  
  35.     print(args.function(args.string))
  36.  
Add Comment
Please, Sign In to add comment