Variables and types

declare a var

In [5]:
a = 2
a
Out[5]:
2
In [6]:
print(a)
2

do some work with vars

In [7]:
a = 5
print(a)
5
In [8]:
b = 3
b
Out[8]:
3
In [9]:
c = a*b
print(c)
15

Show env

In [10]:
who
VariableInspectorWindow	 a	 b	 c	 inspector	 

Show types

In [11]:
type(a)
Out[11]:
int
In [12]:
type(b)
Out[12]:
int
In [13]:
type(c)
Out[13]:
int
In [14]:
d = a / b
In [15]:
d
Out[15]:
1.6666666666666667
In [16]:
type(d)
Out[16]:
float

Use a custom inspector

In [30]:
# coding: utf-8

# # Variable Inspector Widget

# ## A short example implementation

# This notebook demonstrates how one can use the widgets already built in to IPython to create a working variable inspector much like the ones seen in popular commercial scientific computing environments.

import ipywidgets as widgets # Loads the Widget framework.
from IPython.core.magics.namespace import NamespaceMagics # Used to query namespace.

# For this example, hide these names, just to avoid polluting the namespace further
get_ipython().user_ns_hidden['widgets'] = widgets
get_ipython().user_ns_hidden['NamespaceMagics'] = NamespaceMagics

class VariableInspectorWindow(object):
    instance = None
    
    def __init__(self, ipython):
        """Public constructor."""
        if VariableInspectorWindow.instance is not None:
            raise Exception("""Only one instance of the Variable Inspector can exist at a 
                time.  Call close() on the active instance before creating a new instance.
                If you have lost the handle to the active instance, you can re-obtain it
                via `VariableInspectorWindow.instance`.""")
        
        VariableInspectorWindow.instance = self
        self.closed = False
        self.namespace = NamespaceMagics()
        self.namespace.shell = ipython.kernel.shell
        
        self._box = widgets.Box()
        self._box.layout.overflow_y = 'scroll'
        self._table = widgets.HTML(value = 'Not hooked')
        self._box.children = [self._table]
        
        self._ipython = ipython
        self._ipython.events.register('post_run_cell', self._fill)
        
    def close(self):
        """Close and remove hooks."""
        if not self.closed:
            self._ipython.events.unregister('post_run_cell', self._fill)
            self._box.close()
            self.closed = True
            VariableInspectorWindow.instance = None

    def _fill(self):
        """Fill self with variable information."""
        values = self.namespace.who_ls()
        self._table.value = '<div class="rendered_html jp-RenderedHTMLCommon"><table><thead><tr><th>Name</th><th>Type</th><th>Value</th></tr></thead><tr><td>' +             '</td></tr><tr><td>'.join(['{0}</td><td>{1}</td><td>{2}'.format(v, type(eval(v)).__name__, str(eval(v))) for v in values]) +             '</td></tr></table></div>'

    def _ipython_display_(self):
        """Called when display() or pyout is used to display the Variable 
        Inspector."""
        self._box._ipython_display_()

inspector = VariableInspectorWindow(get_ipython())
inspector

Do some big calculus

In [18]:
14444444666565656566*9869868968968868698
Out[18]:
142564776188524251172737355696615571068
In [19]:
2**1000
Out[19]:
10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376
In [20]:
0.1+0.1+0.1-0.3
Out[20]:
5.551115123125783e-17

Work with strings

In [21]:
first_name = 'Sheldon'
In [22]:
last_name = 'Cooper'
In [23]:
type(first_name)
Out[23]:
str
In [24]:
type(last_name)
Out[24]:
str
In [25]:
name = first_name + ' ' + last_name
name
Out[25]:
'Sheldon Cooper'

Show env again

In [26]:
who
VariableInspectorWindow	 a	 b	 c	 d	 first_name	 inspector	 last_name	 name	 

Import a module

In [27]:
import sys
sys.version
Out[27]:
'3.6.8 (default, Dec 30 2018, 13:01:27) \n[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.11.45.5)]'
In [28]:
import os
In [29]:
os.getcwd()
Out[29]:
'/Users/roza/Desktop/diu/TDD/Python-init/notebooks'
In [ ]: