Ticket #7010: trac_7010-code-clean-up.patch

File trac_7010-code-clean-up.patch, 9.6 KB (added by mvngu, 12 years ago)

clean up sage/crypto/classical.py so it conforms to coding conventions

  • sage/crypto/classical.py

    # HG changeset patch
    # User Minh Van Nguyen <nguyenminh2@gmail.com>
    # Date 1254671883 25200
    # Node ID c518b1d9f5f047fb07eb87ba91ffc44734268aa9
    # Parent  e334f15125a3faecdecfefc6079a7ba4536955e8
    trac 7010: clean up sage/crypto/classical.py so it conforms to coding conventions
    
    diff -r e334f15125a3 -r c518b1d9f5f0 sage/crypto/classical.py
    a b  
    4545# - methods to cryptanalyze the Hill, substitution, transposition, and
    4646#   Vigenere ciphers
    4747
    48 from sage.monoids.string_monoid import StringMonoid_class, AlphabeticStringMonoid
     48from sage.monoids.string_monoid import (
     49    StringMonoid_class,
     50    AlphabeticStringMonoid)
    4951from sage.monoids.string_monoid_element import StringMonoidElement
    5052from sage.monoids.string_ops import strip_encoding
    5153from sage.groups.perm_gps.permgroup_named import SymmetricGroup
     
    136138            Hill cryptosystem on Free alphabetic string monoid on A-Z of block length 3
    137139        """
    138140        if not isinstance(S, StringMonoid_class):
    139             raise TypeError, "S (= %s) must be a string monoid."%S
     141            raise TypeError("S (= %s) must be a string monoid." % S)
    140142        R = IntegerModRing(S.ngens())
    141         M = MatrixSpace(R,m,m)
    142         SymmetricKeyCryptosystem.__init__(self, S, S, M, block_length = m)
     143        M = MatrixSpace(R, m, m)
     144        SymmetricKeyCryptosystem.__init__(self, S, S, M, block_length=m)
    143145
    144146    def __call__(self, A):
    145147        """
     
    177179            try:
    178180                A = M(A)
    179181            except:
    180                 raise TypeError, "A (= %s) must specify a square matrix of degree %s." % (A, m)
     182                raise TypeError("A (= %s) must specify a square matrix of degree %s." % (A, m))
    181183        return HillCipher(self, A)
    182184
    183185    def _repr_(self):
     
    250252        m = M.nrows()
    251253        N = Integer(self.cipher_domain().ngens())
    252254        while True:
    253             A = M([ randint(0,N-1) for i in range(m**2) ])
     255            A = M([ randint(0, N-1) for i in range(m**2) ])
    254256            if N.gcd(A.det()) == 1:
    255257                break
    256258        return A
     
    282284        S = self.plaintext_space()
    283285        M = self.key_space()
    284286        if not A in M:
    285             raise TypeError, "A (= %s) must be a matrix in the key space of %s." % (A, self)
     287            raise TypeError("A (= %s) must be a matrix in the key space of %s." % (A, self))
    286288        m = self.block_length()
    287         MatZZ = MatrixSpace(ZZ,m)
    288         AZ = MatZZ([ [ A[i,j].lift() for j in range(m) ] for i in range(m) ])
     289        MatZZ = MatrixSpace(ZZ, m)
     290        AZ = MatZZ([ [ A[i, j].lift() for j in range(m) ] for i in range(m) ])
    289291        AZ_adj = AZ.adjoint()
    290         u, r, s = xgcd(A.det().lift(),S.ngens())
     292        u, r, s = xgcd(A.det().lift(), S.ngens())
    291293        if u != 1:
    292             raise ValueError, "Argument:\n\n%s\n\nis not invertible."%(A)
     294            raise ValueError("Argument:\n\n%s\n\nis not invertible." % (A))
    293295        return r * A.parent()(AZ_adj)
    294296
    295297    def encoding(self, M):
     
    317319            True
    318320        """
    319321        S = self.cipher_domain()
    320         if isinstance(S,AlphabeticStringMonoid):
     322        if isinstance(S, AlphabeticStringMonoid):
    321323            return S(strip_encoding(M))
    322324        try:
    323325            return S.encoding(M)
    324326        except:
    325             raise TypeError, "Argument M = %s does not encode in the cipher domain" % M
     327            raise TypeError("Argument M = %s does not encode in the cipher domain" % M)
    326328
    327329    def deciphering(self, A, C):
    328330        """
     
    616618            True
    617619        """
    618620        # sanity check
    619         from sage.monoids.string_monoid import ( AlphabeticStringMonoid,
    620                                                  BinaryStringMonoid,
    621                                                  HexadecimalStringMonoid )
     621        from sage.monoids.string_monoid import (
     622            AlphabeticStringMonoid,
     623            BinaryStringMonoid,
     624            HexadecimalStringMonoid)
    622625        if not isinstance(A, ( AlphabeticStringMonoid,
    623626                               BinaryStringMonoid,
    624627                               HexadecimalStringMonoid )):
     
    10671070        sage: E == loads(dumps(E))
    10681071        True
    10691072    """
     1073
    10701074    def __init__(self, S):
    10711075        """
    10721076        See ``SubstitutionCryptosystem`` for full documentation.
     
    10791083            Substitution cryptosystem on Free alphabetic string monoid on A-Z
    10801084        """
    10811085        if not isinstance(S, StringMonoid_class):
    1082             raise TypeError, "S (= %s) must be a string monoid."%S
     1086            raise TypeError("S (= %s) must be a string monoid." % S)
    10831087        SymmetricKeyCryptosystem.__init__(self, S, S, S)
    10841088
    10851089    def __call__(self, K):
     
    11051109            GSVXZGRMGSVSZG
    11061110        """
    11071111        if not isinstance(K, StringMonoidElement):
    1108             raise TypeError, "K (= %s) must be a string."%K
     1112            raise TypeError("K (= %s) must be a string." % K)
    11091113        if K.parent() != self.key_space():
    1110             raise TypeError, "K (= %s) must be a string in the key space."%K
     1114            raise TypeError("K (= %s) must be a string in the key space." % K)
    11111115        return SubstitutionCipher(self, K)
    11121116
    11131117    def _repr_(self):
     
    12081212            True
    12091213        """
    12101214        S = self.cipher_domain()
    1211         if isinstance(S,AlphabeticStringMonoid):
     1215        if isinstance(S, AlphabeticStringMonoid):
    12121216            return S(strip_encoding(M))
    12131217        try:
    12141218            return S.encoding(M)
    12151219        except:
    1216             raise TypeError, "Argument M = %s does not encode in the cipher domain" % M
     1220            raise TypeError("Argument M = %s does not encode in the cipher domain" % M)
    12171221
    12181222    def deciphering(self, K, C):
    12191223        """
     
    13021306        sage: E == loads(dumps(E))
    13031307        True
    13041308    """
     1309
    13051310    def __init__(self, S, n):
    13061311        """
    13071312        See ``TranspositionCryptosystem`` for full documentation.
     
    13141319            Transposition cryptosystem on Free alphabetic string monoid on A-Z of block length 14
    13151320        """
    13161321        if not isinstance(S, StringMonoid_class):
    1317             raise TypeError, "S (= %s) must be a string monoid."%S
     1322            raise TypeError("S (= %s) must be a string monoid." % S)
    13181323        key_space = SymmetricGroup(n)
    1319         SymmetricKeyCryptosystem.__init__(self, S, S, key_space, block_length = n)
     1324        SymmetricKeyCryptosystem.__init__(self, S, S, key_space, block_length=n)
    13201325
    13211326    def __call__(self, K):
    13221327        """
     
    13451350            try:
    13461351                K = G(K)
    13471352            except:
    1348                 raise TypeError, "K (= %s) must specify a permutation."%K
     1353                raise TypeError("K (= %s) must specify a permutation." % K)
    13491354        if not isinstance(K, PermutationGroupElement) and K.parent() == G:
    1350             raise TypeError, "K (= %s) must be a permutation or list specifying a permutation."%K
     1355            raise TypeError("K (= %s) must be a permutation or list specifying a permutation." % K)
    13511356        return TranspositionCipher(self, K)
    13521357
    13531358    def _repr_(self):
     
    14231428        """
    14241429        if check:
    14251430            if not K in self.key_space():
    1426                 raise TypeError, "Argument K (= %s) is not in the key space." % K
     1431                raise TypeError("Argument K (= %s) is not in the key space." % K)
    14271432        return K**-1
    14281433
    14291434    def encoding(self, M):
     
    14511456            True
    14521457        """
    14531458        S = self.cipher_domain()
    1454         if isinstance(S,AlphabeticStringMonoid):
     1459        if isinstance(S, AlphabeticStringMonoid):
    14551460            return S(strip_encoding(M))
    14561461        try:
    14571462            return S.encoding(M)
    14581463        except:
    1459             raise TypeError, "Argument M = %s does not encode in the cipher domain" % M
     1464            raise TypeError("Argument M = %s does not encode in the cipher domain" % M)
    14601465
    14611466    def deciphering(self, K, C):
    14621467        """
     
    15621567            Vigenere cryptosystem on Free alphabetic string monoid on A-Z of period 14
    15631568        """
    15641569        if not isinstance(S, StringMonoid_class):
    1565             raise TypeError, "S (= %s) must be a string monoid."%S
    1566         SymmetricKeyCryptosystem.__init__(self, S, S, S, block_length = 1, period = n)
     1570            raise TypeError("S (= %s) must be a string monoid." % S)
     1571        SymmetricKeyCryptosystem.__init__(self, S, S, S, block_length=1, period=n)
    15671572
    15681573    def __call__(self, K):
    15691574        """
     
    15921597            try:
    15931598                K = S(K)
    15941599            except:
    1595                 raise TypeError, "K (= %s) must specify a string of length %s." % (K, m)
     1600                raise TypeError("K (= %s) must specify a string of length %s." % (K, m))
    15961601        if not len(K) == m:
    1597             raise TypeError, "K (= %s) must specify a string of length %s." % (K, m)
     1602            raise TypeError("K (= %s) must specify a string of length %s." % (K, m))
    15981603        return VigenereCipher(self, K)
    15991604
    16001605    def _repr_(self):
     
    16391644        S = self.key_space()
    16401645        n = S.ngens()
    16411646        m = self.period()
    1642         return S([ randint(0,n-1) for i in range(m) ])
     1647        return S([ randint(0, n-1) for i in range(m) ])
    16431648
    16441649    def inverse_key(self, K):
    16451650        """
     
    16941699            True
    16951700        """
    16961701        S = self.cipher_domain()
    1697         if isinstance(S,AlphabeticStringMonoid):
     1702        if isinstance(S, AlphabeticStringMonoid):
    16981703            return S(strip_encoding(M))
    16991704        try:
    17001705            return S.encoding(M)
    17011706        except:
    1702             raise TypeError, "Argument M = %s does not encode in the cipher domain" % M
     1707            raise TypeError("Argument M = %s does not encode in the cipher domain" % M)
    17031708
    17041709    def deciphering(self, K, C):
    17051710        """