I decided to step out of my comfort zone (Java) and try out Python. I wrote a very simple reverser.py and want it to be reviewed.
"""
This python script basically takes a string and reverses it.
This python script can be used as a command line script as well as a separate
module.
"""
import sys
def reverse(message):
"""
This function takes a string and reverses it, so as to somewhat obfuscate
it.
:param message: The unencrypted text message to be reversed.
"""
text = ''
for character in message:
text = character + text
return text
if __name__ == '__main__':
numberOfArgs = len(sys.argv)
message = ''
if numberOfArgs == 1: # i.e. the program name only
message = input('Please enter the text to be encrypted : ')
elif numberOfArgs == 2:
message = sys.argv[1]
else:
print('Incorrect number of arguments : %d\n' % numberOfArgs)
print('Usage: python3 reverseCipher [unencrypted string]')
exit(2) # '2' stands for command-line syntax errors in unix
print(reverse(message))
Examples in the terminal:
$ python3 reverser.py
Please enter the text to be encrypted : I understand that text manipulation in python is quite easy.
.ysae etiuq si nohtyp ni noitalupinam txet taht dnatsrednu I
$ python3 reverser.py "I am just starting out with python"
nohtyp htiw tuo gnitrats tsuj ma I
It would be great if I would be advised about:
- Code style and idioms
- The required mindset (for I have one for Java)
- Anything that can help me learn the language
-
1\$\begingroup\$ See codereview.stackexchange.com/questions/102052/… \$\endgroup\$Janne Karila– Janne Karila2015年11月25日 14:47:27 +00:00Commented Nov 25, 2015 at 14:47
2 Answers 2
This is going to be a short answer, but did you know Python has such a feature in-built?
text = "I'm a stringy!"
print(text[::-1])
Above would replace your reverse
method and is the most Pythonic way to solve this.
This is called extended slicing and has been added since Python 2.3. Slicing is a very easy to use method when handling array-like data.
The following illustrates how slicing and extended slicing can be used, executed in the interactive Python IDE:
In [1]: text = "I'm a stringy!"
In [2]: print(text)
I'm a stringy!
In [3]: print(text[:-1])
I'm a stringy
In [4]: print(text[::-1])
!ygnirts a m'I
In [5]: print(text[:4:-1])
In [6]: print((text:4:-1] + text[:3:5])[::-1])
I stringy!
In [7]: print(text[::-1][::-1][::-1])
!ygnirts a m'I
-
1\$\begingroup\$ also useful is
''.join(reversed(text))
, but in this case reverse-stepping is great. \$\endgroup\$Adam Smith– Adam Smith2015年11月25日 21:09:26 +00:00Commented Nov 25, 2015 at 21:09
I guess you might have heard of PEP8, which is the style guideline for Python. Contrary to a lot of other programming languages, this style guideline is actually used a lot, and is the main standard for coding in Python.
Your code looks clean, and I like that you've commented using docstrings moth the module/file and the function. Mast does have a point that you can accomplish the same using message[::-1]
, but part of going into a new language is learning what the language can offer in standard libraries.
Do look into using slice'ing, which is extremely powerful and useful. A challenge for you is to resolve Project Euler #13: Large sum using Python and sliceing. I think you'll get some pleasant surprises related to string vs number handling, and handling of large numbers as well.
Two other comments related to style issues:
- Use
snake_case
for variable and function naming – That is usenumber_of_arguments
instead ofnumberOfArgs
- Look into using format for string formatting – The
%
is scheduled for depreceation, and you should rather useprint('Incorrect number of arguments: {}'.format(number_of_arguments))
. For more on this see Format specification mini language, on that page there is also useful examples and syntax for how to pretty print using format.