2011-04-16 36 views
-1

我試圖將我的加密程序併入聊天程序。但我不能通過與Mockets的MutableString。那麼如何正確地將我的MutableString轉換爲適當的字符串呢?如何正確地轉換字符串而不是MutableString

import sys 
from UserString import MutableString 

def GetMode(): 

while True: 
    print '\n(E)ncrypt Or (D)ecrypt?' 
    Mode = raw_input().lower() 
    if Mode in 'e d encrypt decrypt'.split(): 
     return Mode 
    else: 
     print '\nEnter Proper Choice!' 

def GetInput(): 
    while True: 
     print '\n(T)ype Message Or (L)oad File?' 
     Input = raw_input().lower() 
     if Input in 't type'.split(): 
      Input = raw_input('>') 
      return Input 
     elif Input in 'l load'.split(): 
      MsgLoc = raw_input() 
      MsgLoc = open(MsgLoc, 'r') 
      try: 
       Input = MsgLoc.read() 
       MsgLoc.close() 
       return Input 
      except: 
       print '\nCould Not Open' , MsgLoc 
     else: 
      print '\nEnter Proper Choice!' 

def GetKey(): 
    while True: 
     Key = 0 
     print '\nPlease Enter A 20 Digit Number...\n** Do NOT use zeros!!!! EX-NAY ERO-ZAY! **' 
     try: 
      Key = int(input()) 
      Key = str(Key) 
      if (len(Key) == 20): 
       return Key 
      else: 
       print('\nPlease Enter A Valid Number!') 
     except: 
      print('\nPlease Enter A Valid Number!') 

def Translate(Mode, Input, Key): 
    if Mode[0] == 'e': 
     print('\nEncrypting....') 
     Encrypt(Input, Key) 
    else: 
     print('\nDecrypting....') 
     Decrypt(Input, Key) 

def Encrypt(Input, Key): 
    Msg = MutableString() 
    NonMutMsg = Input 
    Msg += NonMutMsg 
    MsgLen = len(Msg) 
    CypherKey = Key 
    a = 0 
    b = 19 

    #Loop For Proccessing Key 
    for z in range(10): 
     KeySkip = int(CypherKey[a]) 
     KeyIncrement = int(CypherKey[b]) 
     c = MsgLen/KeySkip 
     d = -1 

     #Loop To Skip Then Increment 
     for y in range(c): 
      d = d+KeySkip 
      LtrNum = ord(NonMutMsg[d]) 
      LtrNum = LtrNum + KeyIncrement 
      Msg[d] = chr(LtrNum) 
+2

你爲什麼要使用反正MutableString:

獲取數據時,使用data參數?它被棄用,狗慢,「這個班的主要目的是作爲一個教育的例子」(見文檔)。另外,通過在*每行之後添加一個不需要的空白行來加倍LOC是有害的 - 而且大量的代碼也無濟於事。 – delnan 2011-04-16 17:42:33

回答

2

首先,作爲MutableString documentation狀態:

應當指出的是,這些類相比,實際字符串或Unicode對象效率非常低; MutableString尤其如此。

,最重要的是:

這個類的主要目的是作爲繼承

換句話說教育例如,不要使用這個類在現實世界中碼。

pythonstring = mutablestring.data 
相關問題