The purpose of this post is to outline with a practical example the use in tandem of what to me were two obscure methods in Python. These methods belong to the string module. They are string.translate() and string.maketrans(). More on these methods later.
First some back background; a quick introduction to substitution ciphers. A cipher is the method by which you jumble up a plain text message into something that hopefully keeps it secret. Upon getting the ciphered message this jumbling up process in reversed to get back the original message. That is it, in essence.
Let us assume the English alphabet contains only 5 letters instead of the usual 26. These letters are ‘aeguv’ and with them you made the word ‘vague‘. You want to send this special word to someone as a secret. With a substitution cipher we would replace each letter with another character so that it loses it human meaning. With our example we could use the following substitutions or substitution table:
‘a’ becomes ‘1’,
‘g’ becomes ‘2’,
‘u’ becomes ‘3’,
‘v’ becomes ‘4’,
‘e’ becomes ‘5’
Using this our secret word becomes
The receiver of this word would need to know how the substitutions works and then apply apply this knowledge in reverse to discover the message.
The above is known as a simple substitution cipher as it only operates on single letters. Using what we learnt here I wrote a script which takes any text string containing characters from the English alphabet and Roman numerals and to output a ciphered message.
So for example, taking out secret message to be “the quick brown fox jumps over the lazy dog” we get “4(% 15)#+ “2/7. &/8 *5-03 /6%2 4(% ,!:9 $/’”
1 # coding=UTF-8 2 # http://www.python.org/peps/pep-0263.html 3 """ 4 This module implements a simple substitute cipher 5 using the maketrans() and translate functions of the 6 string module 7 """ 8 9 10 # imports 11 import sys 12 import string 13 # constants 14 # exception classes 15 # interface functions 16 # classes 17 # internal functions & classes 18 19 class Simplecipher(object): 20 """Simplecipher is a implementation of a simple substution cipher""" 21 def __init__(self): 22 self._from = self.make_from() 23 self._to = self.make_to() 24 self.encode_table = self.encoding_table() 25 self.decode_table = self.decoding_table() 26 27 def make_from(self): 28 """ Create the full character set that our message uses """ 29 # 'abcdefghijklmnopqrstuvwxyz0123456789' 30 return string.ascii_lowercase + string.digits 31 32 def make_to(self): 33 """ Return a string of 36 character used to substitute with """ 34 # 'chr' converts ascii code to an ascii character 35 result = "".join([chr(i+32) for i in range(1,37)]) 36 return result 37 38 def encoding_table(self): 39 if len(self._from) != len(self._to): 40 raise "The inputs to maketans() are not of the same length" 41 else: 42 # from > to 43 return string.maketrans(self._from, self._to) 44 45 def decoding_table(self): 46 if len(self._from) != len(self._to): 47 raise "The input to maketans() are not of the same length" 48 else: 49 # to > from 50 return string.maketrans(self._to, self._from) 51 52 def make_secret(self,message=None): 53 """ cipher an input string """ 54 result = string.translate(message, self.encode_table) 55 return result 56 57 def find_message(self,secret_message=None): 58 """ decode an inout string """ 59 result = string.translate(secret_message, self.decode_table) 60 return result 61 62 63 def main(): 64 # create a new Simplecipher object 65 simp_ciph = Simplecipher() 66 # the message we want to cipher 67 message = 'the quick brown fox jumps over the lazy dog' 68 # cipher the message 69 secret = simp_ciph.make_secret(message) 70 71 print message 72 print secret 73 74 # decode the secret message 75 print simp_ciph.find_message(secret) 76 77 if __name__ == '__main__': 78 status = main() 79 sys.exit(status)
I have been attending the council meetings for the ward I live in. They seem more to be more a paper exercise than democracy in action. In some cases local residents have been outnumbered by council employees. One of the handouts we received was the crime statistics for local areas. The data itself is a table of numbers. No graph, charts or anything to make it easier to digest. So I decided to chart the data myself.
The stacked bars below show the total levels of recorded crime each month. Each bar is broken down into incident type. You can see that March 2012 experienced a spike in crime.
The chart below shows the types of recorded crime each month as a proportion of the total crime recorded in that month. This is essentially a percentage. It is as though there were 100 reported crimes each month. Each bar then breaks up this 100 into the different types of crime recorded. Each horizontal band represents a different crime. The wider the band gets the more crime of that type, and the narrower the band, the less it is.
Come, bring your loved ones. Go tell the majestic sun to preserve its precious rays. We’ll teach the core of our own beings to glow with its light..
For the first few weeks I didn’t really notice the police here except for the occasional check point. There are two forces operating here, one, the police force I’ve seen depicted on the telly; paunch men wearing black uniforms and carrying short canes, the other, the traffic police who wear a grey uniform and tend to be younger. The latter is more visible on the streets of towns, with the former manning check points, making arrests, extracting confessions, securing official events and operating the police station. The traffic police are more like the bobby on the beat in England, they issue fines, enforce traffic restrictions, intervene in minor incidents and radio ahead to the regular police if needed.I have been told that there is no real rule of law in Pakistan/Kashmir but having spent time here and speaking to people this varies from city to city and between Kashmir and Pakistan. In Karachi you can pay your way out of murdering someone, in Kashmir you will go to jail for the crime. In the past the police here were not provided with any form of transport, instead they would commandeer a passing car in order to make a pursuit or a visit. The driver would have to oblige and there was no hope of being compensated for petrol or time. People feared the police. Now, with with the relative affluence that has arrived in these parts, they drive well maintained Toyota pick ups and motor cycles but are not feared as much and people will often argue with them if they are not happy with something. They are also paid well, for example the take home pay of a traffic policeman is 25000Rps per month. This was intended to wipe out any taking of bribes. It has mostly worked, but as they don’t always get fuel expenses for their vehicles this could provide an avenue for those who are willing to accept a bribe. Apart from the excellent salary, the vehicles and the smart uniforms they lack the backup teams needed to investigate crimes or follow up leads. For example there is nothing equivalent to the crime scene investigation teams, or CCTV camera network the regular police in the UK use. In the absence of such devices the police will often use force to get people to talk and prove guilt. This mostly takes the form of slapping and caning of the suspect. Officially torture is forbidden and if evidence of this bought before a judge it can jeopardize a case. This has led the police to become inventive when it comes to extracting confessions. For example one traffic policeman told me of a man who was questioned for the theft of 14 Jeeps in Muzafarabad, Azad Kashmir. He wouldn’t talk; so the traffic policeman was told by his superior to purchase some chillies and a piece of plastic piping. The chillies were then boiled in water and allowed to cool. The pipe was tapered on one end and inserted into the rear end of the suspect and the chillies poured in. After this the suspect spoke up. I get the impression that everyone who is brought in for questioning is roughed up in some way, but they are always men. However, for lesser crimes, or those of a higher station in life or with the money, you can escape all this. Justice and money seem to be interchangeable entities for lesser crimes such as theft of property possession of alcohol.
Went to a funeral in Mirpur. Read Jannazah prayers in a larger hall across the road from the cemetery. I watched the coffin being lowered into its resting place and helped replace the earth over it. The man whose funeral it was used to live in Chicago. In death he has returned to the soil of his birth.