{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercises: working with modules\n", "\n", "### Exercise 4.1\n", "Write a function that creates an empty file `file_name` in a specified directory `dir_name`. \n", "The function should:\n", "* create the directory if needed (support for recursive directory creation is not needed).\n", "* not overwrite/delete any existing files.\n", "* have a `test_mode` argument that, when set to `True`, cleans-up any newly created file and \n", " deletes the directory `dir_name` if it is empty.\n", "\n", "To help you get started, the function's definition, docstring and some pseudocode are already given below.\n", "What you need to do is to flesh-out the body of the `create_empty_file()` function, by replacing the `...` by actual code that does what is indicated in comments. \n", "If you prefer to write your own function, you can of course also do it.\n", "\n", "There are also some lines of codes at the end of the cell to test your function. These lines are already written for you and no edits needed from your side." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Content of directory tmp_dir : ['file_1.txt', 'file_2.txt']\n" ] } ], "source": [ "\n", "def create_empty_file(file_name, dir_name, test_mode=False):\n", " \"\"\"Create an empty file named 'file_name' in a directory\n", " named 'dir_name'. If test_mode is True the file is\n", " deleted immediatly after it was created.\n", " \"\"\" \n", " # Create a new directory, if it does not already exist.\n", " ...\n", " \n", " # Create a new file, if it does not already exist.\n", " file_path = ... # create full path of file from directory name and file name\n", " if ...\n", " # if file already exist: do nothing and exit function.\n", " \n", " else ...\n", " # otherwise: create an empty file.\n", " \n", " \n", " # If in test mode, do some cleanup: delete the file and directory we just created.\n", " # Note that the directory is only deleted if it is empty.\n", " if test_mode:\n", " ... do the cleanup:\n", " ... delete newly created file\n", " ... delete directory that contained the file, if empty.\n", " \n", "\n", " \n", "# THIS IS TO TEST YOUR FUNCTION. THERE IS NO NEED TO EDIT ANYTHING AFTER THIS POINT.\n", "# *********************************************************************************\n", "# Let's test our function.\n", "# To verify that the \"create_empty_file()\" function works as expected, we run\n", "# it a number of times with test mode on and off, and then look at the content\n", "# of \"dir_name\".\n", "dir_name = 'tmp_dir'\n", "create_empty_file('file_1.txt', dir_name)\n", "create_empty_file('file_2.txt', dir_name)\n", "create_empty_file('file_3.txt', dir_name, test_mode=True)\n", "\n", "# Directory \"dir_name\" should contain 'file_1.txt' and 'file_2.txt', but \n", "# not 'file_3.txt' since the later was created in test mode.\n", "print(\"Content of directory\", dir_name, \":\", os.listdir(dir_name))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "## Exercise 4.2\n", "What doe this line of code do? \n", "`time.sleep(3)`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "# Additional Exercises\n", "\n", "## Exercise 4.3\n", "Import the function `is_part_of_set` of the `exercise4_3_module` module, located in the same folder as this notebook.\n", "1. What does `is_part_of_set` do ?\n", "2. Determine the result of `is_part_of_set` for all values of x and y from -2 to +2 in increment of 0.05.\n", "3. How long does this computation takes ?\n", "4. How would you represent the result of your work ?\n", "\n", "> Remember the `help()` function. You can also open the module file as a normal text file to read its code.\n", ">\n", "> The grid you have to test for contains 6400 points. Try with a smaller number of points first." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.8.6" } }, "nbformat": 4, "nbformat_minor": 4 }