STEM 隨筆︰古典力學︰模擬術【小工具】六

金代铜钞版

生︰何謂東西,不謂南北
師︰東方木西方金,金木所以成物;北方水南方火,水火二用,故不成。
生︰無土耶?
師︰土也,四方之中,東南西北之宰,是為中之用,胡物不中用?況土者堵也,阿堵是也 ,奈何?
生︰此說有之?
師︰無此一說,故說此一說;有此一說,焉用說

昔時賢文》君子愛財取之有道。

─── 《孔方之阿堵物

 

生︰今有

ipywidgets/docs/source/examples/

Variable Inspector Widget

 

反背之物怎麼講呢?

師︰遙想

1878 年大發明家愛迪生寫下

It has been just so in all of my inventions. The first step is an intuition, and comes with a burst, then difficulties arise — this thing gives out and [it is] then that “Bugs” — as such little faults and difficulties are called — show themselves and months of intense watching, study and labor are requisite before commercial success or failure is certainly reached.

上文中 bugs 是用小昆蟲來指小瑕疵!為什麼呢?不得而知。至於『除蟲』debugging 的由來,就像『黏在繼電器上的飛蛾』一說,也許是故事裡的事,但又何必在意那麼多,相信能有什麼不好呢 ?難道說人不是因夢想而偉大,是一根會思考的蘆葦。這是 1946 的 伊尼亞克 ENIAC 世界上第一台通用型計算機,完全的圖靈機

Eniac

 

為什麼從『錯誤』開始談起呢?對寫程式的人來說,『除錯』是常態。不要害怕『犯錯』,把錯誤看成『敵友』── 唱反調的朋友,它能糾正不清的思慮,改進不明的理解。如果能以『不二過』為圭臬,你將能學的更快更好。

中國古代沒有標點符號,卻有句讀之學,把文本斷句加上圈點,以此確定文意。舉例來說︰

『下雨天留客天天留我不留』這個文本,可以有多種句讀,文意各不相同。

下雨天,留客天;天、留我不留。
下雨,天留客,天天留;我不留。
下雨天,留客;天天留我!不留。

同樣的,人使用電腦,也需要一種清楚的辦法來確定指令(命令 ),一點不能含混不清。技術點的說,人是透過『人機介面』操作電腦。這個『介』或 『界』字一般泛指『兩者之間』的意思,『介面』或譯『界面』interface 一詞在電腦術語的用法上,通常表達物與物的交接處,功能操作的地方。比如說「應用軟體界面 API」指的是電腦程式如何使用軟體系統所提供的「功能程式庫 library」;USB 介面是指電腦主機和 USB 裝置間的硬體介面;而所謂的終端機則提供了人與電腦作業系統的『文字界面』。

─── 摘自《除蟲!除錯?終端機。

 

幾曾超越『塵歸塵,土歸土』也。既有『蠱』

 蠱卦 (山風蠱)

蠱,元亨,利涉大川。先甲三日,後甲三日。初六,幹父之蠱,有子,考无咎,厲終吉。九二,幹母之蠱,不可貞。九三,幹父之蠱,小有悔,无大咎。六四,裕父之蠱,往見吝。六五,幹父之蠱,用譽。上九,不事王侯,高尚其事。

 

『蟲』自生矣!焉用問?餘者,且是『除不除』哩?!

中不中用,但看學生呦!?

events.py

"""Infrastructure for registering and firing callbacks on application events.
Unlike :mod:`IPython.core.hooks`, which lets end users set single functions to
be called at specific times, or a collection of alternative methods to try,
callbacks are designed to be used by extension authors. A number of callbacks
can be registered for the same event without needing to be aware of one another.
The functions defined in this module are no-ops indicating the names of available
events and the arguments which will be passed to them.
.. note::
   This API is experimental in IPython 2.0, and may be revised in future versions.
"""

from backcall import callback_prototype

……

    def register(self, event, function):
        """Register a new event callback.
        
        Parameters
        ----------
        event : str
          The event for which to register this callback.
        function : callable
          A function to be called on the given event. It should take the same
          parameters as the appropriate callback prototype.
        
        Raises
        ------
        TypeError
          If ``function`` is not callable.
        KeyError
          If ``event`` is not one of the known events.
        """
        if not callable(function):
            raise TypeError('Need a callable, got %r' % function)
        callback_proto = available_events.get(event)
        self.callbacks[event].append(callback_proto.adapt(function))
    
    def unregister(self, event, function):
        """Remove a callback from the given event."""
        if function in self.callbacks[event]:
            return self.callbacks[event].remove(function)

        # Remove callback in case ``function`` was adapted by `backcall`.
        for callback in self.callbacks[event]:
            try:
                if callback.__wrapped__ is function:
                    return self.callbacks[event].remove(callback)
            except AttributeError:
                pass

        raise ValueError('Function {!r} is not registered as a {} callback'.format(function, event))
………

@_define_event
def post_run_cell(result):
    """Fires after user-entered code runs.
    Parameters
    ----------
    result : :class:`~IPython.core.interactiveshell.ExecutionResult`
      The object which will be returned as the execution result.
    """
    pass

 

/backcall

Backwards compatible callback APIs

backcall

https://travis-ci.org/takluyver/backcall.png?branch=master

Specifications for callback functions passed in to an API

If your code lets other people supply callback functions, it’s important to specify the function signature you expect, and check that functions support that. Adding extra parameters later would break other peoples code unless you’re careful.

backcall provides a way of specifying the callback signature using a prototype function:

from backcall import callback_prototype

@callback_prototype
def handle_ping(sender, delay=None):
    # Specify positional parameters without a default, and keyword
    # parameters with a default.
    pass

def register_ping_handler(callback):
    # This checks and adapts the function passed in:
    callback = handle_ping.adapt(callback)
    ping_callbacks.append(callback)