Advertisement
phjoe

rot13

Oct 18th, 2015
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. """ Python Character Mapping Codec for ROT13.
  3.  
  4.    See http://ucsub.colorado.edu/~kominek/rot13/ for details.
  5.  
  6.    Written by Marc-Andre Lemburg (mal@lemburg.com).
  7.  
  8. """#"
  9.  
  10. import codecs
  11.  
  12. ### Codec APIs
  13.  
  14. class Codec(codecs.Codec):
  15.  
  16.     def encode(self,input,errors='strict'):
  17.  
  18.         return codecs.charmap_encode(input,errors,encoding_map)
  19.  
  20.     def decode(self,input,errors='strict'):
  21.  
  22.         return codecs.charmap_decode(input,errors,decoding_map)
  23.  
  24. class StreamWriter(Codec,codecs.StreamWriter):
  25.     pass
  26.  
  27. class StreamReader(Codec,codecs.StreamReader):
  28.     pass
  29.  
  30. ### encodings module API
  31.  
  32. def getregentry():
  33.  
  34.     return (Codec().encode,Codec().decode,StreamReader,StreamWriter)
  35.  
  36. ### Decoding Map
  37.  
  38. decoding_map = codecs.make_identity_dict(range(256))
  39. decoding_map.update({
  40.    0x0041: 0x004e,
  41.    0x0042: 0x004f,
  42.    0x0043: 0x0050,
  43.    0x0044: 0x0051,
  44.    0x0045: 0x0052,
  45.    0x0046: 0x0053,
  46.    0x0047: 0x0054,
  47.    0x0048: 0x0055,
  48.    0x0049: 0x0056,
  49.    0x004a: 0x0057,
  50.    0x004b: 0x0058,
  51.    0x004c: 0x0059,
  52.    0x004d: 0x005a,
  53.    0x004e: 0x0041,
  54.    0x004f: 0x0042,
  55.    0x0050: 0x0043,
  56.    0x0051: 0x0044,
  57.    0x0052: 0x0045,
  58.    0x0053: 0x0046,
  59.    0x0054: 0x0047,
  60.    0x0055: 0x0048,
  61.    0x0056: 0x0049,
  62.    0x0057: 0x004a,
  63.    0x0058: 0x004b,
  64.    0x0059: 0x004c,
  65.    0x005a: 0x004d,
  66.    0x0061: 0x006e,
  67.    0x0062: 0x006f,
  68.    0x0063: 0x0070,
  69.    0x0064: 0x0071,
  70.    0x0065: 0x0072,
  71.    0x0066: 0x0073,
  72.    0x0067: 0x0074,
  73.    0x0068: 0x0075,
  74.    0x0069: 0x0076,
  75.    0x006a: 0x0077,
  76.    0x006b: 0x0078,
  77.    0x006c: 0x0079,
  78.    0x006d: 0x007a,
  79.    0x006e: 0x0061,
  80.    0x006f: 0x0062,
  81.    0x0070: 0x0063,
  82.    0x0071: 0x0064,
  83.    0x0072: 0x0065,
  84.    0x0073: 0x0066,
  85.    0x0074: 0x0067,
  86.    0x0075: 0x0068,
  87.    0x0076: 0x0069,
  88.    0x0077: 0x006a,
  89.    0x0078: 0x006b,
  90.    0x0079: 0x006c,
  91.    0x007a: 0x006d,
  92. })
  93.  
  94. ### Encoding Map
  95.  
  96. encoding_map = codecs.make_encoding_map(decoding_map)
  97.  
  98. ### Filter API
  99.  
  100. def rot13(infile, outfile):
  101.     outfile.write(infile.read().encode('rot-13'))
  102.  
  103. if __name__ == '__main__':
  104.     import sys
  105.     rot13(sys.stdin, sys.stdout)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement