光的世界︰派生科學計算一

學習『工具』最簡單的方法,就是『使用』它︰

pi@raspberrypi:~ $ ipython3
Python 3.4.2 (default, Oct 19 2014, 13:31:11) 
Type "copyright", "credits" or "license" for more information.

IPython 2.3.0 -- 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.

In [1]: 

 

不過對此『工具』是什麼□○都尚且不知,又將如何『使用』呢?通常『工具』的『使用』始於閱讀『概觀』︰

Introduction

Overview

One of Python’s most useful features is its interactive interpreter. It allows for very fast testing of ideas without the overhead of creating test files as is typical in most programming languages. However, the interpreter supplied with the standard Python distribution is somewhat limited for extended interactive use.

The goal of IPython is to create a comprehensive environment for interactive and exploratory computing. To support this goal, IPython has three main components:

  • An enhanced interactive Python shell.
  • A decoupled two-process communication model, which allows for multiple clients to connect to a computation kernel, most notably the web-based notebook
  • An architecture for interactive parallel computing.

All of IPython is open source (released under the revised BSD license).

………

 

從『簡介』尋找,發現『上手』之處︰

Introducing IPython

You don’t need to know anything beyond Python to start using IPython – just type commands as you would at the standard Python prompt. But IPython can do much more than the standard prompt. Some key features are described here. For more information, check the tips page, or look at examples in the IPython cookbook.

If you’ve never used Python before, you might want to look at the official tutorial or an alternative, Dive into Python.

The four most helpful commands

The four most helpful commands, as well as their brief description, is shown to you in a banner, every time you start IPython:

command description
? 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.

 

Tab completion

Tab completion, especially for attributes, is a convenient way to explore the structure of any object you’re dealing with. Simply type object_name.<TAB> to view the object’s attributes (see the readline section for more). Besides Python objects and keywords, tab completion also works on file and directory names.

………

 

要是還能知道『參考文件』在哪裡固然很好︰

───

 

往往根本什麼說明也沒有哩!?此時亦無礙徒手上陣,嘗試錯誤,將之 TRY 出耶?!

依稀記得作者當初下的第一個指令就是『?』︰

? -> Introduction and overview of IPython’s features.

 

匆匆瀏覽後,隨即上路︰

IPython — An enhanced Interactive Python
=========================================

IPython offers a combination of convenient shell features, special commands and a history mechanism for both input (command history) and output (results caching, similar to Mathematica). It is intended to be a fully compatible replacement for the standard Python interpreter, while offering vastly improved functionality and flexibility.

At your system command line, type ‘ipython -h’ to see the command line options available. This document only describes interactive features.

MAIN FEATURES
————-

* Access to the standard Python help. As of Python 2.1, a help system is available with access to object docstrings and the Python manuals. Simply type ‘help’ (no quotes) to access it.

* Magic commands: type %magic for information on the magic subsystem.

* System command aliases, via the %alias command or the configuration file(s).

* Dynamic object information:

Typing ?word or word? prints detailed information about an object. If
certain strings in the object are too long (docstrings, code, etc.) they get snipped in the center for brevity.

Typing ??word or word?? gives access to the full information without
snipping long strings. Long strings are sent to the screen through the less pager if longer than the screen, printed otherwise.

The ?/?? system gives access to the full source code for any object (if
available), shows function prototypes and other useful information.

If you just want to see an object’s docstring, type ‘%pdoc object’ (without quotes, and without % if you have automagic on).

Both %pdoc and ?/?? give you access to documentation even on things which are not explicitely defined. Try for example typing {}.get? or after import os, type os.path.abspath??. The magic functions %pdef, %source and %file operate similarly.

* Completion in the local namespace, by typing TAB at the prompt.

At any time, hitting tab will complete any available python commands or variable names, and show you a list of the possible completions if there’s no unambiguous one. It will also complete filenames in the current directory.

This feature requires the readline and rlcomplete modules, so it won’t work if your Python lacks readline support (such as under Windows).
* Search previous command history in two ways (also requires readline):

– Start typing, and then use Ctrl-p (previous,up) and Ctrl-n (next,down) to search through only the history items that match what you’ve typed so far. If you use Ctrl-p/Ctrl-n at a blank prompt, they just behave like normal arrow keys.

– Hit Ctrl-r: opens a search prompt. Begin typing and the system searches your history for lines that match what you’ve typed so far, completing as much as it can.

– %hist: search history by index (this does *not* require readline).

* Persistent command history across sessions.

* Logging of input with the ability to save and restore a working session.

* System escape with !. Typing !ls will run ‘ls’ in the current directory.

* The reload command does a ‘deep’ reload of a module: changes made to the module since you imported will actually be available without having to exit.

* Verbose and colored exception traceback printouts. See the magic xmode and xcolor functions for details (just type %magic).

* Input caching system:

IPython offers numbered prompts (In/Out) with input and output caching. All input is saved and can be retrieved as variables (besides the usual arrow key recall).

The following GLOBAL variables always exist (so don’t overwrite them!):
_i: stores previous input.
_ii: next previous.
_iii: next-next previous.
_ih : a list of all input _ih[n] is the input from line n.

Additionally, global variables named _i<n> are dynamically created (<n> being the prompt counter), such that _i<n> == _ih[<n>]

For example, what you typed at prompt 14 is available as _i14 and _ih[14].

You can create macros which contain multiple input lines from this history, for later re-execution, with the %macro function.

The history function %hist allows you to see any part of your input history by printing a range of the _i variables. Note that inputs which contain magic functions (%) appear in the history with a prepended comment. This is because they aren’t really valid Python code, so you can’t exec them.

* Output caching system:
For output that is returned from actions, a system similar to the input
cache exists but using _ instead of _i. Only actions that produce a result (NOT assignments, for example) are cached. If you are familiar with Mathematica, IPython’s _ variables behave exactly like Mathematica’s % variables.

The following GLOBAL variables always exist (so don’t overwrite them!):
_ (one underscore): previous output.
__ (two underscores): next previous.
___ (three underscores): next-next previous.

Global variables named _<n> are dynamically created (<n> being the prompt counter), such that the result of output <n> is always available as _<n>.

Finally, a global dictionary named _oh exists with entries for all lines
which generated output.

* Directory history:

Your history of visited directories is kept in the global list _dh, and the
magic %cd command can be used to go to any entry in that list.

* Auto-parentheses and auto-quotes (adapted from Nathan Gray’s LazyPython)

1. Auto-parentheses

Callable objects (i.e. functions, methods, etc) can be invoked like
this (notice the commas between the arguments)::

In [1]: callable_ob arg1, arg2, arg3

and the input will be translated to this::

callable_ob(arg1, arg2, arg3)

This feature is off by default (in rare cases it can produce
undesirable side-effects), but you can activate it at the command-line
by starting IPython with `–autocall 1`, set it permanently in your
configuration file, or turn on at runtime with `%autocall 1`.

You can force auto-parentheses by using ‘/’ as the first character
of a line. For example::

In [1]: /globals # becomes ‘globals()’

Note that the ‘/’ MUST be the first character on the line! This
won’t work::

In [2]: print /globals # syntax error

In most cases the automatic algorithm should work, so you should
rarely need to explicitly invoke /. One notable exception is if you
are trying to call a function with a list of tuples as arguments (the
callable_ob(arg1, arg2, arg3)

This feature is off by default (in rare cases it can produce
undesirable side-effects), but you can activate it at the command-line
by starting IPython with `–autocall 1`, set it permanently in your
configuration file, or turn on at runtime with `%autocall 1`.

You can force auto-parentheses by using ‘/’ as the first character
of a line. For example::

In [1]: /globals # becomes ‘globals()’

Note that the ‘/’ MUST be the first character on the line! This
won’t work::

In [2]: print /globals # syntax error

In most cases the automatic algorithm should work, so you should
rarely need to explicitly invoke /. One notable exception is if you
are trying to call a function with a list of tuples as arguments (the
parenthesis will confuse IPython)::

In [1]: zip (1,2,3),(4,5,6) # won’t work

but this will work::

In [2]: /zip (1,2,3),(4,5,6)
——> zip ((1,2,3),(4,5,6))
Out[2]= [(1, 4), (2, 5), (3, 6)]

IPython tells you that it has altered your command line by
displaying the new command line preceded by –>. e.g.::

In [18]: callable list
——-> callable (list)

2. Auto-Quoting

You can force auto-quoting of a function’s arguments by using ‘,’ as
the first character of a line. For example::

In [1]: ,my_function /home/me # becomes my_function(“/home/me”)

If you use ‘;’ instead, the whole argument is quoted as a single
string (while ‘,’ splits on whitespace)::

In [2]: ,my_function a b c # becomes my_function(“a”,”b”,”c”)
In [3]: ;my_function a b c # becomes my_function(“a b c”)

Note that the ‘,’ MUST be the first character on the line! This
won’t work::

In [4]: x = ,my_function /home/me # syntax error

 

一旦需要什麼 □□ ,或想要怎樣 ○○ ,再查詢、翻閱、谷歌……文件,仔細精讀文本一番乎!!??果然常用之故,有時大腦恐已忘記 ,誰知指頭竟能自書,當真『熟能生巧』隨心所欲哩??!!

因此『派生互動筆記』界面,作者也作如是觀矣!!!

The IPython Notebook

Introduction

The notebook extends the console-based approach to interactive computing in a qualitatively new direction, providing a web-based application suitable for capturing the whole computation process: developing, documenting, and executing code, as well as communicating the results. The IPython notebook combines two components:

A web application: a browser-based tool for interactive authoring of documents which combine explanatory text, mathematics, computations and their rich media output.

Notebook documents: a representation of all content visible in the web application, including inputs and outputs of the computations, explanatory text, mathematics, images, and rich media representations of objects.

See also

See the installation documentation for directions on how to install the notebook and its dependencies.

Main features of the web application

  • In-browser editing for code, with automatic syntax highlighting, indentation, and tab completion/introspection.
  • The ability to execute code from the browser, with the results of computations attached to the code which generated them.
  • Displaying the result of computation using rich media representations, such as HTML, LaTeX, PNG, SVG, etc. For example, publication-quality figures rendered by the matplotlib library, can be included inline.
  • In-browser editing for rich text using the Markdown markup language, which can provide commentary for the code, is not limited to plain text.
  • The ability to easily include mathematical notation within markdown cells using LaTeX, and rendered natively by MathJax.

Notebook documents

Notebook documents contains the inputs and outputs of a interactive session as well as additional text that accompanies the code but is not meant for execution. In this way, notebook files can serve as a complete computational record of a session, interleaving executable code with explanatory text, mathematics, and rich representations of resulting objects. These documents are internally JSON files and are saved with the .ipynb extension. Since JSON is a plain text format, they can be version-controlled and shared with colleagues.

Notebooks may be exported to a range of static formats, including HTML (for example, for blog posts), reStructuredText, LaTeX, PDF, and slide shows, via the new nbconvert command.

Furthermore, any .ipynb notebook document available from a public URL can be shared via the IPython Notebook Viewer (nbviewer). This service loads the notebook document from the URL and renders it as a static web page. The results may thus be shared with a colleague, or as a public blog post, without other users needing to install IPython themselves. In effect, nbviewer is simply nbconvert as a web service, so you can do your own static conversions with nbconvert, without relying on nbviewer.

………