rot13以更多Pythonic风格? [英] rot13 in a more Pythonic style?

查看:60
本文介绍了rot13以更多Pythonic风格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试写rot13,但要用更好的和更多的Pythonic

风格来做,而不是我正在使用。你会认为

是多么丑陋的事情?你会如何改进它?特别是

,我不喜欢

嵌套两个二元选择的三向选择方式。另外我不喜欢两次说明相同的

算法,但是看不出如何整齐地对它们进行参数化。


是的,我知道.encode()和.translate()。

不,我实际上并不需要rot13本身,它只是一个方便的

替代真正的工作特定任务的例子。

不,我不必用lambdas做,但如果

最终函数是lambda,那就太好了。

#!/ bin / python

导入字符串


lc_rot13 = lambda c:(chr((ord(c) - ord(''a' ')+ 13)%26 + ord(''a'')))


uc_rot13 = lambda c:(chr((ord(c) - ord(''A' ')+ 13)%26 + ord(''A'')))


c_rot13 = lambda c :(((c,uc_rot13(c))[c in

''ABCDEFGHIJKLMNOPQRSTUVWXYZ'']),lc_rot13(c))[c in

''abcdefghijklmnopqrstuvwxyz'']


rot13 = lambda s:string.join([s中的c为c_rot13(c)],'''')

prin t rot13(''Sybevk Tenohaqnr,Fcyhaqvt ihe guevtt'')

I''m trying to write rot13, but to do it in a better and more Pythonic
style than I''m currrently using. What would you reckon to the
following pretty ugly thing? How would you improve it? In
particular, I don''t like the way a three-way selection is done by
nesting two binary selections. Also I dislike stating the same
algorithm twice, but can''t see how to parameterise them neatly.

Yes, I know of .encode() and .translate().
No, I don''t actually need rot13 itself, it''s just a convenient
substitute example for the real job-specific task.
No, I don''t have to do it with lambdas, but it would be nice if the
final function was a lambda.
#!/bin/python
import string

lc_rot13 = lambda c : (chr((ord(c) - ord(''a'') + 13) % 26 + ord(''a'')))

uc_rot13 = lambda c : (chr((ord(c) - ord(''A'') + 13) % 26 + ord(''A'')))

c_rot13 = lambda c : (((c, uc_rot13(c)) [c in
''ABCDEFGHIJKLMNOPQRSTUVWXYZ'']), lc_rot13(c) )[c in
''abcdefghijklmnopqrstuvwxyz'']

rot13 = lambda s : string.join([ c_rot13(c) for c in s ],'''')
print rot13( ''Sybevk Tenohaqnr, Fcyhaqvt ihe guevtt'' )

推荐答案

你可以试试'some_string" .encode(' 'rot_13'')

You could try "some_string".encode(''rot_13'')


2007-02-14,Andy Dingley< di ***** @ codesmiths.comwrote:
On 2007-02-14, Andy Dingley <di*****@codesmiths.comwrote:

我正在尝试编写rot13,但要用比我正在使用的更好更多的
Pythonic风格来做。你会对以下非常丑陋的东西估计是什么?你怎么会b
改善它?特别是,我不喜欢通过嵌套两个二进制选择来完成三向

选择的方式。另外我不赞成两次说同样的算法,但是看不清楚如何将它们整齐地参数化。


是,我知道.encode()和.translate()。
I''m trying to write rot13, but to do it in a better and more
Pythonic style than I''m currrently using. What would you
reckon to the following pretty ugly thing? How would you
improve it? In particular, I don''t like the way a three-way
selection is done by nesting two binary selections. Also I
dislike stating the same algorithm twice, but can''t see how to
parameterise them neatly.

Yes, I know of .encode() and .translate().



str.translate就是我要做的。


导入字符串

rot13table = string.maketrans(

''abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ'',

''nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJ KLM'')


print'' Sybevk Tenohaqnr,Fcyhaqvt ihe guevtt''。translate(rot13table)

str.translate is what I''d do.

import string
rot13table = string.maketrans(
''abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ'',
''nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJ KLM'')

print ''Sybevk Tenohaqnr, Fcyhaqvt ihe guevtt''.translate(rot13table)


不,我实际上并不需要rot13本身,它只是一个方便的

替换实际作业特定任务的示例。不,我不会使用lambdas来实现
,但如果最终的

函数是lambda,那就太好了。
No, I don''t actually need rot13 itself, it''s just a convenient
substitute example for the real job-specific task. No, I don''t
have to do it with lambdas, but it would be nice if the final
function was a lambda.



如何成为一个lambda帮助你?


-

Neil Cerutti

How would it being a lambda help you?

--
Neil Cerutti


Andy Dingley写道:
Andy Dingley wrote:

我正在尝试写rot13,但要做得更好还有更多Pythonic

风格,而不是我正在使用的。你会认为

是多么丑陋的事情?你会如何改进它?特别是

,我不喜欢

嵌套两个二元选择的三向选择方式。另外我不喜欢两次说明相同的

算法,但是看不出如何整齐地对它们进行参数化。


是的,我知道.encode()和.translate()。

不,我实际上并不需要rot13本身,它只是一个方便的

替代真正的工作特定任务的例子。

不,我不必用lambdas做,但如果

最终函数是lambda,那就太好了。


#!/ bin / python

导入字符串


lc_rot13 = lambda c:(chr((ord(c) - ord( ''a'')+ 13)%26 + ord(''a'')))


uc_rot13 = lambda c:(chr((ord(c) - ord( ''A'')+ 13)%26 + ord(''A'')))


c_rot13 = lambda c:(((c,uc_rot13(c))[ c in

''ABCDEFGHIJKLMNOPQRSTUVWXYZ'']),lc_rot13(c))[c in

''abcdefghijklmnopqrstuvwxyz'']


rot13 = lambda s:string.join(s中的c为[c_rot13(c)],'''')


print rot13(''Sybevk Tenohaqnr,Fcyhaqvt ihe guevtt'')
I''m trying to write rot13, but to do it in a better and more Pythonic
style than I''m currrently using. What would you reckon to the
following pretty ugly thing? How would you improve it? In
particular, I don''t like the way a three-way selection is done by
nesting two binary selections. Also I dislike stating the same
algorithm twice, but can''t see how to parameterise them neatly.

Yes, I know of .encode() and .translate().
No, I don''t actually need rot13 itself, it''s just a convenient
substitute example for the real job-specific task.
No, I don''t have to do it with lambdas, but it would be nice if the
final function was a lambda.
#!/bin/python
import string

lc_rot13 = lambda c : (chr((ord(c) - ord(''a'') + 13) % 26 + ord(''a'')))

uc_rot13 = lambda c : (chr((ord(c) - ord(''A'') + 13) % 26 + ord(''A'')))

c_rot13 = lambda c : (((c, uc_rot13(c)) [c in
''ABCDEFGHIJKLMNOPQRSTUVWXYZ'']), lc_rot13(c) )[c in
''abcdefghijklmnopqrstuvwxyz'']

rot13 = lambda s : string.join([ c_rot13(c) for c in s ],'''')
print rot13( ''Sybevk Tenohaqnr, Fcyhaqvt ihe guevtt'' )



首先,对我来说(个人)是Pythonic意味着我应该

分隔逻辑和变量,在这种情况下有旋转

机制和变量及其应该旋转的数量。

然后当然字母大小写是我认为是字母本身的状态,字母的意思并没有改变。

作为字典的傻瓜我使用它们很多


所以考虑到这一点,我会写一个这样的课:

###

class Rot(object ):

def __init __(自我,金额= 13):

self .__ alpha =''abcdefghijklmnopqrstuvwxyz''

self .__金额=金额

self .__ index_string = dict()

self .__ crypt_index_string = dict()

self .__ string_index = dict()

self .__ crypt_string_index = dict()

self .__ positi on = 0


self .__ create_dicts()

def __cypher(self,number):

alpha_len = len(self。 __alpha)

rotation_overflow = alpha_len - self .__金额

new_number =无


如果编号rotation_overflow:

new_number = number - self .__金额


其他:

new_number = self .__ position + self .__金额


return(new_number)

def __create_dicts(self):

for self .__ alpha:

self .__ position + = 1


self .__ index_string [self .__ position] = letter

self .__ crypt_index_string [self .__ cypher(self .__ position)] =

letter


self .__ string_index [letter] = self .__ position

self .__ crypt_string_index [letter] =

self .__ cypher (自我.__位置)

def加密(自我,文本):

text_list = list()

letter_capital =无
<文字中的字母:


letter_capital = letter.isupper()

letter = letter.lower()


如果信件不在自我.__ alpha:

text_list。追加(信件)


其他:

position_plain = self .__ string_index [letter]

letter_crypt = self .__ crypt_index_string [position_plain]


如果letter_capital:

letter_crypt = letter_crypt.upper()


text_list.append(letter_crypt)


return("" .join(text_list))

def decrypt(self,text):

text_list = list( )

letter_capital =无


的文字信:

letter_capital = letter.isupper()

letter = letter.lower()


如果信件不在自我.__ alpha:

text_list.append(letter)

else:

position_crypt = self .__ crypt_string_index [letter]

letter_plain = self .__ index_string [position_crypt]


如果letter_capital:

letter_plain = letter_plain.u pper()


text_list.append(letter_plain)


return("" .join(text_list))

###


测试是否有效:

Well first of all, for me (personal) being Pythonic means that I should
separate the logic and variables, in this case there is the rotation
mechanism and the variable with the amount it should rotate.
Then of course the letters case is something I consider as a state of
the letter itself, the meaning of the letter doesn''t change.
And being a sucker for dictionaries I use them a lot

So with that in mind I would write a class like this:
###
class Rot(object):
def __init__(self,amount = 13):
self.__alpha = ''abcdefghijklmnopqrstuvwxyz''
self.__amount = amount
self.__index_string = dict()
self.__crypt_index_string = dict()
self.__string_index = dict()
self.__crypt_string_index = dict()
self.__position = 0

self.__create_dicts()
def __cypher(self,number):
alpha_len = len(self.__alpha)
rotation_overflow = alpha_len - self.__amount
new_number = None

if number rotation_overflow:
new_number = number - self.__amount

else:
new_number = self.__position + self.__amount

return(new_number)
def __create_dicts(self):
for letter in self.__alpha:
self.__position += 1

self.__index_string[self.__position] = letter
self.__crypt_index_string[self.__cypher(self.__position)] =
letter

self.__string_index[letter] = self.__position
self.__crypt_string_index[letter] =
self.__cypher(self.__position)
def encrypt(self,text):
text_list = list()
letter_capital = None

for letter in text:
letter_capital = letter.isupper()
letter = letter.lower()

if letter not in self.__alpha:
text_list.append(letter)

else:
position_plain = self.__string_index[letter]
letter_crypt = self.__crypt_index_string[position_plain]

if letter_capital:
letter_crypt = letter_crypt.upper()

text_list.append(letter_crypt)

return("".join(text_list))
def decrypt(self,text):
text_list = list()
letter_capital = None

for letter in text:
letter_capital = letter.isupper()
letter = letter.lower()

if letter not in self.__alpha:
text_list.append(letter)

else:
position_crypt = self.__crypt_string_index[letter]
letter_plain = self.__index_string[position_crypt]

if letter_capital:
letter_plain = letter_plain.upper()

text_list.append(letter_plain)

return("".join(text_list))
###

Testing if it works:


>> rot13.decrypt(rot13.encrypt(" This is a TEST"))
>>rot13.decrypt(rot13.encrypt("This is a TEST"))



''这是测试''


-

mph

''This is a TEST''

--
mph


这篇关于rot13以更多Pythonic风格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆