Created
April 6, 2015 23:30
-
-
Save metasyn/8baa2500d8e2bb6d96b9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"q2 = \"\"\"\n", | |
"Michael\n", | |
"\n", | |
"dawn\n", | |
"\n", | |
"lock hart ln\n", | |
"\n", | |
"Dublin\n", | |
"\n", | |
"--\n", | |
"\n", | |
"kate\n", | |
"\n", | |
"Nan\n", | |
"\n", | |
"webster st\n", | |
"\n", | |
"king city\n", | |
"\n", | |
"--\n", | |
"\n", | |
"raj\n", | |
"\n", | |
"zakjg\n", | |
"\n", | |
"late Road\n", | |
"\n", | |
"Toronto\n", | |
"\n", | |
"--\n", | |
"\n", | |
"dave\n", | |
"\n", | |
"porter\n", | |
"\n", | |
"Rock Ave\n", | |
"\n", | |
"nobleton\n", | |
"\n", | |
"--\n", | |
"\n", | |
"John\n", | |
"\n", | |
"Doe\n", | |
"\n", | |
"round road\n", | |
"\n", | |
"schomberg\n", | |
"\"\"\"\n", | |
"\n", | |
"letter = \"\"\"\n", | |
"[fname] [lname]\n", | |
"[street]\n", | |
"[city]\n", | |
"\n", | |
"Dear [fname]:\n", | |
"\n", | |
" As a fellow citizen of [city], you and all your neighbours \n", | |
"on [street] are invited to a celebration this Saturday at \n", | |
"[city]'s Central Park. Bring beer and food!\n", | |
"\"\"\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"with open('q2.txt', 'w') as f:\n", | |
" f.write(q2)\n", | |
"f.close()\n", | |
"\n", | |
"with open('letter.txt', 'w') as f:\n", | |
" f.write(letter)\n", | |
"f.close()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"letter = ''\n", | |
"q2 = ''\n", | |
"\n", | |
"with open('letter.txt', 'r') as f:\n", | |
" letter = f.read()\n", | |
"f.close()\n", | |
"\n", | |
"with open('q2.txt', 'r') as f:\n", | |
" q2 = f.read()\n", | |
"f.close()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'\\nMichael\\n\\ndawn\\n\\nlock hart ln\\n\\nDublin\\n\\n--\\n\\nkate\\n\\nNan\\n\\nwebster st\\n\\nking city\\n\\n--\\n\\nraj\\n\\nzakjg\\n\\nlate Road\\n\\nToronto\\n\\n--\\n\\ndave\\n\\nporter\\n\\nRock Ave\\n\\nnobleton\\n\\n--\\n\\nJohn\\n\\nDoe\\n\\nround road\\n\\nschomberg\\n'" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"q2" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"\\n[fname] [lname]\\n[street]\\n[city]\\n\\nDear [fname]:\\n\\n As a fellow citizen of [city], you and all your neighbours \\non [street] are invited to a celebration this Saturday at \\n[city]'s Central Park. Bring beer and food!\\n\"" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"letter" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def cleanData(query):\n", | |
" return [item.strip().split('\\n\\n') for item in query.split('--')]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"def writeLetter(template, variables, replacements):\n", | |
" # replace ith variable with ith replacement variable\n", | |
" for i in range(len(variables)):\n", | |
" template = template.replace(variables[i], replacements[i]) \n", | |
" return template" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"data = cleanData(q2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[['Michael', 'dawn', 'lock hart ln', 'Dublin'],\n", | |
" ['kate', 'Nan', 'webster st', 'king city'],\n", | |
" ['raj', 'zakjg', 'late Road', 'Toronto'],\n", | |
" ['dave', 'porter', 'Rock Ave', 'nobleton'],\n", | |
" ['John', 'Doe', 'round road', 'schomberg']]" | |
] | |
}, | |
"execution_count": 11, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"data" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"variables = ['[fname]', '[lname]', '[street]', '[city]']" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"letters = [writeLetter(letter, variables, person) for person in data]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"\n", | |
"Michael dawn\n", | |
"lock hart ln\n", | |
"Dublin\n", | |
"\n", | |
"Dear Michael:\n", | |
"\n", | |
" As a fellow citizen of Dublin, you and all your neighbours \n", | |
"on lock hart ln are invited to a celebration this Saturday at \n", | |
"Dublin's Central Park. Bring beer and food!\n", | |
"\n", | |
"\n", | |
"kate Nan\n", | |
"webster st\n", | |
"king city\n", | |
"\n", | |
"Dear kate:\n", | |
"\n", | |
" As a fellow citizen of king city, you and all your neighbours \n", | |
"on webster st are invited to a celebration this Saturday at \n", | |
"king city's Central Park. Bring beer and food!\n", | |
"\n", | |
"\n", | |
"raj zakjg\n", | |
"late Road\n", | |
"Toronto\n", | |
"\n", | |
"Dear raj:\n", | |
"\n", | |
" As a fellow citizen of Toronto, you and all your neighbours \n", | |
"on late Road are invited to a celebration this Saturday at \n", | |
"Toronto's Central Park. Bring beer and food!\n", | |
"\n", | |
"\n", | |
"dave porter\n", | |
"Rock Ave\n", | |
"nobleton\n", | |
"\n", | |
"Dear dave:\n", | |
"\n", | |
" As a fellow citizen of nobleton, you and all your neighbours \n", | |
"on Rock Ave are invited to a celebration this Saturday at \n", | |
"nobleton's Central Park. Bring beer and food!\n", | |
"\n", | |
"\n", | |
"John Doe\n", | |
"round road\n", | |
"schomberg\n", | |
"\n", | |
"Dear John:\n", | |
"\n", | |
" As a fellow citizen of schomberg, you and all your neighbours \n", | |
"on round road are invited to a celebration this Saturday at \n", | |
"schomberg's Central Park. Bring beer and food!\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"for i in letters:\n", | |
" print i" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment