{ "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 and docstring are already given below, \n", "as well as a few commands to test your function. \n", "What you need to do is write the body of the `create_empty_file()` function." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "ename": "FileNotFoundError", "evalue": "[Errno 2] No such file or directory: 'tmp_dir'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 20\u001b[0m \u001b[0;31m# Directory \"dir_name\" should contain 'file_1.txt' and 'file_2.txt', but\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 21\u001b[0m \u001b[0;31m# not 'file_3.txt' since the later was created in test mode.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 22\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Content of directory\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdir_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\":\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlistdir\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdir_name\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'tmp_dir'" ] } ], "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", " # Write the body of the function here...\n", "\n", " \n", " \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", "## 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.5" } }, "nbformat": 4, "nbformat_minor": 4 }