Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import argparse
- import regex as re
- from bitstring import BitArray
- def decode(string):
- sym = {'ПУК': '0', 'СРЕНЬК': '1'}
- return BitArray(''.join(['0b'] + [sym[s]*int(c and c or 1) for (c, s) in re.findall(r'(\d*)(\w+)-?', string)])).bytes.decode()
- def encode(string):
- sym = {'0': 'ПУК', '1': 'СРЕНЬК'}
- 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)])
- if __name__ == '__main__':
- parser = argparse.ArgumentParser(description='Encode and decode 2ch.hk fart code.')
- parser.add_argument('string', metavar='string', type=str,
- help='string for process')
- function = parser.add_mutually_exclusive_group(required=True)
- function.add_argument('-d', '--decode', dest='function', action='store_const',
- const=decode, help='decode string')
- function.add_argument('-e', '--encode', dest='function', action='store_const',
- const=encode, help='encode string')
- args = parser.parse_args()
- print(args.function(args.string))
Add Comment
Please, Sign In to add comment