{ "metadata": { "name": "9932_02_02" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Chapter 2, example 2\n", "====================\n", "\n", "Here we continue with the previous example: we write some Python commands to list all files in the downloaded data. We also show how to execute external Python scripts in IPython using the magic command `%run`.\n", "\n", "The following code should be stored in `egos.py`:\n", "\n", " import sys\n", " import os\n", " # we retrieve the folder as the first positional argument\n", " # to the command-line call\n", " if len(sys.argv) > 1:\n", " folder = sys.argv[1]\n", " # we list all files in the specified folder\n", " files = os.listdir(folder)\n", " # ids contains the sorted list of all unique idenfitiers\n", " ids = sorted(set(map(lambda file: int(file.split('.')[0]), files)))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `egos.py` script accepts the facebook folder's relative path as an argument, and extracts all file identifiers in an `ids` variable. Each file in the dataset has the form `ID.extension`." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%run egos.py data/facebook" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "After using the `%run` command to execute this script, all variables defined in the script are available in the current user namespace. In particular, we can retrieve the `ids` variable here." ] }, { "cell_type": "code", "collapsed": false, "input": [ "ids" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 2, "text": [ "[0, 107, 348, 414, 686, 698, 1684, 1912, 3437, 3980]" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Whereas output variables are available in the namespace *after* the script's execution, variables available in IPython *before* its execution are not available within the script by default. For example, here we define the `folder` variable in the current namespace, and we call `%run egos.py` without an argument." ] }, { "cell_type": "code", "collapsed": false, "input": [ "folder = 'data/facebook'" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "A `NameError` exception is expected here, because the `folder` variable is not defined in the script, although it is defined in the current namespace." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%run egos.py" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'folder' is not defined", "output_type": "pyerr", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[1;32mIPython\\utils\\py3compat.pyc\u001b[0m in \u001b[0;36mexecfile\u001b[1;34m(fname, glob, loc)\u001b[0m\n\u001b[0;32m 169\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 170\u001b[0m \u001b[0mfilename\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mfname\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 171\u001b[1;33m \u001b[1;32mexec\u001b[0m \u001b[0mcompile\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mscripttext\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfilename\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'exec'\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mglob\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mloc\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 172\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 173\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mexecfile\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfname\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m*\u001b[0m\u001b[0mwhere\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32mchapter2\\egos.py\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[0mfolder\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0msys\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0margv\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[1;31m# we list all files in the specified folder\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m \u001b[0mfiles\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlistdir\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfolder\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 9\u001b[0m \u001b[1;31m# ids contains the sorted list of all unique idenfitiers\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 10\u001b[0m \u001b[0mids\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0msorted\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mset\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmap\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;32mlambda\u001b[0m \u001b[0mfile\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfile\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msplit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'.'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfiles\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mNameError\u001b[0m: name 'folder' is not defined" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "However, we can tell IPython *explicitely* to use the current user namespace for the external script's execution with the `-i` option." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%run -i egos.py" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now the script's execution works as expected, and the `ids` variable is available." ] }, { "cell_type": "code", "collapsed": false, "input": [ "ids" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 6, "text": [ "[0, 107, 348, 414, 686, 698, 1684, 1912, 3437, 3980]" ] } ], "prompt_number": 6 } ], "metadata": {} } ] }