Initial commit

This commit is contained in:
Xavier Claude 2014-12-16 21:26:00 +01:00
commit 589e7cebdc
4 changed files with 143 additions and 0 deletions

19
README.md Normal file
View file

@ -0,0 +1,19 @@
#Nato converter
Convert list of character to the equivalent in NATO alphabet. And it takes care
of the capitals:
$ ./nato_converter.py abc
aBc: alpha BRAVO charlie
This is useful to give password orally and avoid confusion:
$ ./nato_converter.py $( pwgen 8 1)
Mahngi8i: MIKE alpha hotel november golf india 8 india
If you don't specify an argument, it reads stdint:
$ pwgen 3 2 | ./nato_converter.py
Ym2 0Ge : YANKEE mike 2 0 GOLF echo

81
nato_converter.py Executable file
View file

@ -0,0 +1,81 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
table = {
"a": "alpha",
"b": "bravo",
"c": "charlie",
"d": "delta",
"e": "echo",
"f": "foxtrot",
"g": "golf",
"h": "hotel",
"i": "india",
"j": "juliett",
"k": "kilo",
"l": "lima",
"m": "mike",
"n": "november",
"o": "oscar",
"p": "papa",
"q": "quebec",
"r": "romeo",
"s": "sierra",
"t": "tango",
"u": "uniform",
"v": "victor",
"w": "whiskey",
"x": "x-ray",
"y": "yankee",
"z": "zulu"
}
class IllegalStringSize(Exception):
def __init__(self, msg):
super(Exception, self).__init__(msg)
def find_in_table(letter):
if len(letter) != 1:
raise IllegalStringSize(
"The letter parameter should only contains one character")
if letter in table:
res = table[letter]
elif letter.lower() in table:
res = table[letter.lower()].upper()
elif not letter or letter.isspace():
res = ""
else:
res = letter
return res
def convert(orig):
result = ""
for letter in orig:
res = find_in_table(letter)
if result:
result = "%s %s" % (result, res)
else:
result = res
return result
if __name__ == '__main__':
orig = ""
if len(sys.argv) > 1:
for arg in sys.argv[1:]:
orig = orig + arg
elif not sys.stdin.isatty():
for arg in sys.stdin.readlines():
orig = orig + arg
if orig:
orig = orig.replace("\n", " ")
result = convert(orig)
print "%s: %s" % (orig, result)
else:
print "usage"
# vim: set ts=4 sw=4 expandtab:

15
setup.py Normal file
View file

@ -0,0 +1,15 @@
#!/usr/bin/env python
from distutils.core import setup
if __name__ == '__main__':
setup(name="Nato_converter",
version="0.1",
description="Convert list of character to the equivalent in Nato"+
"alphabet",
author="Xavier Claude",
author_email="contact+python@xavierclaude.be",
url="todo",
license="GPL2+",
scripts=["nato_converter.py"],
)

28
test.py Executable file
View file

@ -0,0 +1,28 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import nato_converter
import unittest
class TestFindInTable(unittest.TestCase):
def test_find_lower(self):
self.assertEqual("alpha", nato_converter.find_in_table("a"))
def test_find_upper(self):
self.assertEqual("ALPHA", nato_converter.find_in_table("A"))
def test_find_space(self):
self.assertEqual("", nato_converter.find_in_table(" "))
self.assertEqual("", nato_converter.find_in_table("\t"))
self.assertEqual("", nato_converter.find_in_table("\n"))
def test_find_multiple(self):
self.assertRaises(nato_converter.IllegalStringSize,
nato_converter.find_in_table, "aa")
def test_find_special_char(self):
self.assertEqual("1", nato_converter.find_in_table("1"))
self.assertEqual(u"é", nato_converter.find_in_table(u"é"))
if __name__ == '__main__':
unittest.main()