{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "UCL\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Formative Assessment: Scripts" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "*Although we provide access to answers for this exercise, we want you to submit the codes you generate via Moodle, so that we can provide feedback. You should avoid looking at the answers before you submit your work. This submitted work does not count towards your course assessment, it is purely to allow us to provide some rapid feedback to you on how you are doing. You will need to put together a few elements from the notes so far to do all parts of this practical, but you should all be capable of doing it well. Pay attention to writing tidy code, with useful, clear comments and document strings.*" ] }, { "cell_type": "markdown", "metadata": { "solution2": "hidden", "solution2_first": true }, "source": [ "#### Exercise 1\n", "\n", "* Create a Python code in a file called `work/count.py` that does the following:\n", "\n", " - define a function `count(istop)` that prints out numbers from 0 to `istop` **(inclusive)** on the same line. Your function should test that the variable `istop` is an integer, and if not, try to convert it to one (hint: it might well be a string when you pass it from `sys.argv` below).\n", " - define a function `main(vlist)` that loops over each item in the list `vlist` and sends it to `count(...)`\n", " - calls `main(vlist)` if the file is run as a Python script with `vlist` being **all arguments** after `sys.argv[0]` on the script command line\n", " - show a test of the script working\n", " - has plentiful commenting and document strings\n", " \n", " - As a test, when you run the script:\n", "\n", " %run work/count.py 4 5 \n", "\n", " you would expect to get a response of the form:\n", "\n", " 0 1 2 3 4\n", " 0 1 2 3 4 5\n", "\n", " " ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "solution2": "hidden" }, "outputs": [], "source": [ "%%bash\n", "# ANSWER 1\n", "#\n", "\n", "# code between the next line and the \n", "# End Of File (EOF) marker will be saved in \n", "# to the file work/greet.py\n", "cat << EOF > work/count.py\n", "#!/usr/bin/env python\n", "# -*- coding: utf-8 -*- \n", "import sys\n", "\n", "'''\n", "count\n", "\n", "Purpose:\n", "\n", " script to prints out numbers from 0 to istop\n", " (inclusive) on the same line.\n", " \n", "'''\n", "__author__ = \"P Lewis\"\n", "__copyright__ = \"Copyright 2020 P Lewis\"\n", "__license__ = \"GPLv3\"\n", "__email__ = \"p.lewis@ucl.ac.uk\"\n", "\n", "'''\n", "Create a Python code in a file called work/count.py \n", "that does the following:\n", "\n", "- define a function count(istop) that prints out numbers \n", " from 0 to istop (inclusive) on the same line. \n", " Your function should test that the variable istop is an integer, \n", " and if not, try to convert it to one \n", " (hint: it might well be a string when you pass it \n", " from sys.argv below).\n", "- define a function main(vlist) that loops over each item \n", " in the list vlist and sends it to count(...)\n", "- calls main(vlist) if the file is run as a Python \n", " script with vlist being all arguments after \n", " sys.argv[0] on the script command line\n", "- show a test of the script working\n", "- has plentiful commenting and document strings\n", "\n", "- As a test, when you run the script:\n", "\n", " run work/count.py 4 5 \n", "\n", "you would expect to get a response of the form:\n", "\n", " 0 1 2 3 4\n", " 0 1 2 3 4 5\n", "\n", "'''\n", "\n", "# define a function count(istop) that prints out numbers \n", "# from 0 to istop (inclusive) on the same line. \n", "# Your function should test that the variable istop is an integer, \n", "# and if not, try to convert it to one \n", "def count(istop):\n", " '''\n", " print out numbers (integer)\n", " from 0 to istop (inclusive) on the same line.\n", " '''\n", " # force istop to be an integer\n", " # this will fail if you pass something \n", " # that cant be made into an integer, so you could possibly \n", " # be neater about trapping that\n", " istop = int(istop)\n", " \n", " # from 0 to istop (inclusive) on the same line.\n", " # several ways to do this e.g.\n", " print(' '.join([str(i) for i in range(istop+1)]))\n", " \n", " # return from function\n", " return\n", " \n", "# - define a function main(vlist) that loops over each item \n", "# in the list vlist and sends it to count(...)\n", "def main(values):\n", " for istop in values:\n", " count(istop)\n", "\n", "# calls main(vlist) if the file is run as a Python \n", "# script with vlist being all arguments after \n", "# sys.argv[0] on the script command line\n", "if __name__ == \"__main__\":\n", " # execute only if run as a script\n", " # we pass the first command line argument argv[1]\n", " # remembering that argv[0] is the program name\n", " \n", " # we pass the list *after* argument 0, i.e.\n", " # the slice argv[1:]\n", " main(sys.argv[1:])\n", "EOF\n", "\n", "# Chmod 755 to make the file executable\n", "chmod 755 work/count.py" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "solution2": "hidden" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "As a test, when you run the script:\n", "\n", " %run work/count.py 4 5 \n", "you would expect to get a response of the form:\n", "\n", " 0 1 2 3 4\n", " 0 1 2 3 4 5\n", "\n", "work/count.py 4 5 ->\n", "0 1 2 3 4\n", "0 1 2 3 4 5\n" ] } ], "source": [ "msg = '''\n", "As a test, when you run the script:\n", "\n", " %run work/count.py 4 5 \n", "you would expect to get a response of the form:\n", "\n", " 0 1 2 3 4\n", " 0 1 2 3 4 5\n", "'''\n", "print(msg)\n", "\n", "print('work/count.py 4 5 ->')\n", "%run work/count.py 4 5" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "solution2": "hidden" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "some other ways to achieve the printing\n", "\n", "1. 0 1 2 3 4 5 6 7 8 9 10\n", "2. 0 1 2 3 4 5 6 7 8 9 10\n", "3. 0 1 2 3 4 5 6 7 8 9 10 " ] } ], "source": [ "istop = 10\n", "\n", "msg = '''\n", "some other ways to achieve the printing\n", "'''\n", "print(msg)\n", "# as above: this is quite neat and Pythonic\n", "# make a list of strings, then use join\n", "# to make into a single string\n", "print('1.',' '.join([str(i) for i in range(istop+1)]))\n", "\n", "\n", "# use an explicit for loop \n", "# to build the string. Not the most\n", "# Pythonic, but will work.\n", "mystr = ''\n", "# loop over each integer \n", "for i in range(istop+1):\n", " # extend the string in an f-string\n", " mystr = f'{mystr} {i}' \n", "print('2.',mystr)\n", "\n", "# make use of changing end='\\n' in print\n", "# which you may recall from help(print)\n", "print('3.',end=' ')\n", "for i in range(istop+1):\n", " print(i,end=' ') " ] } ], "metadata": { "kernelspec": { "display_name": "Python [conda env:geog0111-geog0111]", "language": "python", "name": "conda-env-geog0111-geog0111-py" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.8" } }, "nbformat": 4, "nbformat_minor": 4 }