Guest User

Untitled

a guest
Dec 22nd, 2021
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.54 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 = {'ПУК': '00',
  9.            'СРЕНЬК': '01',
  10.            'ХРЮК': '10',
  11.            'УИИИ': '11'}
  12.  
  13.     return BitArray(''.join(['0b'] + [sym[s]*int(c and c or 1) for (c, s) in re.findall(r'(\d*)(\w+)-?', string)])).bytes.decode()
  14.  
  15.  
  16. def encode(string):
  17.     sym = {'00': 'ПУК',
  18.            '01': 'СРЕНЬК',
  19.            '10': 'ХРЮК',
  20.            '11': 'УИИИ'}
  21.  
  22.     def join(l):
  23.         result = [l[0]]
  24.         last = None
  25.  
  26.         for e in l[1:]:
  27.             if last == e:
  28.                 result[-1] += e
  29.             else:
  30.                 result.append(e)
  31.  
  32.             last = e
  33.  
  34.         return result
  35.  
  36.     return '-'.join([f'{len(g)//2 > 1 and len(g)//2 or ""}{sym[g[:2]]}' for g in join(re.findall(r'([01]{2})', BitArray(string.encode()).bin))])
  37.  
  38.  
  39. if __name__ == '__main__':
  40.     parser = argparse.ArgumentParser(description='Encode and decode 2ch.hk fart code.')
  41.  
  42.     parser.add_argument('string', metavar='string', type=str,
  43.                         help='string for process')
  44.  
  45.     function = parser.add_mutually_exclusive_group(required=True)
  46.  
  47.     function.add_argument('-d', '--decode', dest='function', action='store_const',
  48.                           const=decode, help='decode string')
  49.  
  50.     function.add_argument('-e', '--encode', dest='function', action='store_const',
  51.                           const=encode, help='encode string')
  52.  
  53.     args = parser.parse_args()
  54.  
  55.     print(args.function(args.string))
  56.  
Add Comment
Please, Sign In to add comment