官术网_书友最值得收藏!

  • Python Data Analysis
  • Ivan Idris
  • 506字
  • 2021-08-05 17:31:49

Using IPython as a shell

Scientists, data analysts, and engineers are used to experimenting. IPython was created by scientists with experimentation in mind. The interactive environment that IPython provides is viewed by many as a direct answer to MATLAB, Mathematica, and Maple.

The following is a list of features of the IPython shell:

  • Tab completion, which helps you find a command
  • History mechanism
  • Inline editing
  • Ability to call external Python scripts with %run
  • Access to system commands
  • The pylab switch
  • Access to the Python debugger and profiler

The following list describes how to use the IPython shell:

  • The pylab switch: The pylab switch automatically imports all the Scipy, NumPy, and matplotlib packages. Without this switch, we would have to import these packages ourselves.

    All we need to do is enter the following instruction on the command line:

    $ ipython -pylab
    Type "copyright", "credits" or "license" for more information.
    
    IPython 2.0.0-dev -- An enhanced Interactive Python.
    ? -> Introduction and overview of IPython's features.
    %quickref -> Quick reference.
    help -> Python's own help system.
    object? -> Details about 'object', use 'object??' for extra details.
    
    Welcome to pylab, a matplotlib-based Python environment [backend: MacOSX].
    For more information, type 'help(pylab)'.
    
    In [1]: quit()
    

    Tip

    The quit() function or Ctrl + D quits the IPython shell.

  • Saving a session: We might want to be able to go back to our experiments. In IPython, it is easy to save a session for later use, with the following command:
    In [1]: %logstart
    Activating auto-logging. Current session state plus future input saved.
    Filename : ipython_log.py
    Mode : rotate
    Output logging : False
    Raw input log : False
    Timestamping : False
    State : active
    

    Logging can be switched off as follows:

    In [9]: %logoff
    Switching logging OFF
    
  • Executing system shell command: Execute a system shell command in the default IPython profile by prefixing the command with the ! symbol. For instance, the following input will get the current date:
    In [1]: !date
    

    In fact, any line prefixed with ! is sent to the system shell. Also, we can store the command output as shown here:

    In [2]: thedate = !date
    In [3]: thedate
    
  • Displaying history: We can show the history of commands with the %hist command, for example:
    In [1]: a = 2 + 2
    
    In [2]: a
    Out[2]: 4
    
    In [3]: %hist
    a = 2 + 2
    a
    %hist
    

    This is a common feature in Command Line Interface (CLI) environments. We can also search through the history with the -g switch as follows:

    In [5]: %hist -g a = 2
     1: a = 2 + 2
    

    Tip

    Downloading the example code

    You can download the example code files for all the Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

We saw a number of so-called magic functions in action. These functions start with the % character. If the magic function is used on a line by itself, the % prefix is optional.

主站蜘蛛池模板: 鞍山市| 松桃| 四会市| 灵丘县| 杭锦后旗| 东至县| 且末县| 申扎县| 平阳县| 西青区| 临颍县| 册亨县| 若羌县| 鸡西市| 兴文县| 台中市| 芜湖市| 上蔡县| 海安县| 甘德县| 林周县| 台北市| 旬邑县| 毕节市| 平昌县| 苍山县| 上饶县| 杭锦旗| 衡东县| 呼图壁县| 营山县| 庆元县| 桐梓县| 通城县| 温宿县| 靖宇县| 车险| 汤阴县| 永登县| 扶余县| 辽阳市|