Chapter_03



In [1]:
a = 5
a


Out[1]:
5

In [3]:
import numpy as np
data = {i : np.random.randn() for i in range(7)}
data


Out[3]:
{0: 0.6657944924383111,
 1: -1.7185705089628915,
 2: 1.5112591467921834,
 3: 1.7656798596463126,
 4: -2.4009542788859055,
 5: -0.21360746973968364,
 6: -1.8716910085557108}

In [4]:
print(data)


{0: 0.6657944924383111, 1: -1.7185705089628915, 2: 1.5112591467921834, 3: 1.7656798596463126, 4: -2.4009542788859055, 5: -0.21360746973968364, 6: -1.8716910085557108}

In [5]:
an_apple = 27
an_example = 42
an_apple


Out[5]:
27

In [6]:
b = [1, 2, 3]

In [7]:
b.append(10)

In [8]:
import datetime

In [9]:
datetime.time()


Out[9]:
datetime.time(0, 0)

In [10]:
b?

In [11]:
def add_numbers(a, b):
    """
    Add two numbers
    
    Returns
    ----------
    the_sum : type of argument
    """
    return a + b

In [12]:
add_numbers?

In [13]:
add_numbers??

In [14]:
np.*load*?

In [15]:
%run ipython_script_test.py

In [16]:
c


Out[16]:
7.5

In [17]:
result


Out[17]:
1.4666666666666666

In [1]:
!pwd


"pwd" ­Ґ пў«пҐвбп ў­гв७­Ґ© Ё«Ё ў­Ґи­Ґ©
Є®¬ ­¤®©, ЁбЇ®«­пҐ¬®© Їа®Ја ¬¬®© Ё«Ё Ї ЄҐв­л¬ д ©«®¬.

In [2]:
foo = 'test*'
!ls $foo


"ls" ­Ґ пў«пҐвбп ў­гв७­Ґ© Ё«Ё ў­Ґи­Ґ©
Є®¬ ­¤®©, ЁбЇ®«­пҐ¬®© Їа®Ја ¬¬®© Ё«Ё Ї ЄҐв­л¬ д ©«®¬.

In [3]:
%run ipython_bug.py


---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
D:\C++\DataScience\Python_for_data_analysis\Chapter_02\ipython_bug.py in <module>()
     15         throws_an_exception()
     16 
---> 17 calling_things()

D:\C++\DataScience\Python_for_data_analysis\Chapter_02\ipython_bug.py in calling_things()
     13 def calling_things():
     14         works_fine()
---> 15         throws_an_exception()
     16 
     17 calling_things()

D:\C++\DataScience\Python_for_data_analysis\Chapter_02\ipython_bug.py in throws_an_exception()
      8         a = 5
      9         b = 6
---> 10         assert (a + b == 10)
     11         return None
     12 

AssertionError: 

In [4]:
%debug


> d:\c++\datascience\python_for_data_analysis\chapter_02\ipython_bug.py(10)throws_an_exception()
      8         a = 5
      9         b = 6
---> 10         assert (a + b == 10)
     11         return None
     12 

ipdb> %whos
*** SyntaxError: invalid syntax
ipdb> %who
*** SyntaxError: invalid syntax
ipdb> print(a, b)
5 6
ipdb> u
> d:\c++\datascience\python_for_data_analysis\chapter_02\ipython_bug.py(15)calling_things()
     13 def calling_things():
     14         works_fine()
---> 15         throws_an_exception()
     16 
     17 calling_things()

ipdb> u
> d:\c++\datascience\python_for_data_analysis\chapter_02\ipython_bug.py(17)<module>()
     13 def calling_things():
     14         works_fine()
     15         throws_an_exception()
     16 
---> 17 calling_things()

ipdb> u
> c:\programdata\anaconda3\lib\site-packages\ipython\utils\py3compat.py(186)execfile()
    184         with open(fname, 'rb') as f:
    185             compiler = compiler or compile
--> 186             exec(compiler(f.read(), fname, 'exec'), glob, loc)
    187 
    188     # Refactor print statements in doctests.

ipdb> d
> d:\c++\datascience\python_for_data_analysis\chapter_02\ipython_bug.py(17)<module>()
     13 def calling_things():
     14         works_fine()
     15         throws_an_exception()
     16 
---> 17 calling_things()

ipdb> d
> d:\c++\datascience\python_for_data_analysis\chapter_02\ipython_bug.py(15)calling_things()
     13 def calling_things():
     14         works_fine()
---> 15         throws_an_exception()
     16 
     17 calling_things()

ipdb> d
> d:\c++\datascience\python_for_data_analysis\chapter_02\ipython_bug.py(10)throws_an_exception()
      8         a = 5
      9         b = 6
---> 10         assert (a + b == 10)
     11         return None
     12 

ipdb> !a
5
ipdb> w
  c:\programdata\anaconda3\lib\site-packages\ipython\utils\py3compat.py(186)execfile()
    184         with open(fname, 'rb') as f:
    185             compiler = compiler or compile
--> 186             exec(compiler(f.read(), fname, 'exec'), glob, loc)
    187 
    188     # Refactor print statements in doctests.

  d:\c++\datascience\python_for_data_analysis\chapter_02\ipython_bug.py(17)<module>()
     13 def calling_things():
     14         works_fine()
     15         throws_an_exception()
     16 
---> 17 calling_things()

  d:\c++\datascience\python_for_data_analysis\chapter_02\ipython_bug.py(15)calling_things()
     13 def calling_things():
     14         works_fine()
---> 15         throws_an_exception()
     16 
     17 calling_things()

> d:\c++\datascience\python_for_data_analysis\chapter_02\ipython_bug.py(10)throws_an_exception()
      8         a = 5
      9         b = 6
---> 10         assert (a + b == 10)
     11         return None
     12 

ipdb> q

In [15]:
%run -d ipython_bug.py


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-15-18dbdfa279da> in <module>()
----> 1 get_ipython().magic('run -d ipython_bug.py')

C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py in magic(self, arg_s)
   2156         magic_name, _, magic_arg_s = arg_s.partition(' ')
   2157         magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2158         return self.run_line_magic(magic_name, magic_arg_s)
   2159 
   2160     #-------------------------------------------------------------------------

C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py in run_line_magic(self, magic_name, line)
   2077                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2078             with self.builtin_trap:
-> 2079                 result = fn(*args,**kwargs)
   2080             return result
   2081 

<decorator-gen-58> in run(self, parameter_s, runner, file_finder)

C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\magic.py in <lambda>(f, *a, **k)
    186     # but it's overkill for just that one bit of state.
    187     def magic_deco(arg):
--> 188         call = lambda f, *a, **k: f(*a, **k)
    189 
    190         if callable(arg):

C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\magics\execution.py in run(self, parameter_s, runner, file_finder)
    713                         opts.get('b', ['1'])[0], filename)
    714                     self._run_with_debugger(
--> 715                         code, code_ns, filename, bp_line, bp_file)
    716                 else:
    717                     if 'm' in opts:

C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\magics\execution.py in _run_with_debugger(self, code, code_ns, filename, bp_line, bp_file)
    816             maxtries = 10
    817             bp_file = bp_file or filename
--> 818             checkline = deb.checkline(bp_file, bp_line)
    819             if not checkline:
    820                 for bp in range(bp_line + 1, bp_line + maxtries + 1):

C:\ProgramData\Anaconda3\lib\pdb.py in checkline(self, filename, lineno)
    749         # this method should be callable before starting debugging, so default
    750         # to "no globals" if there is no current frame
--> 751         globs = self.curframe.f_globals if hasattr(self, 'curframe') else None
    752         line = linecache.getline(filename, lineno, globs)
    753         if not line:

AttributeError: 'NoneType' object has no attribute 'f_globals'

In [10]:
import cprof_example
%prun -l 7 -s cumulative cprof_example.run_experiment()


11.5648809202
 

In [11]:
%magic

In [16]:
%lprun -f cprof_example.run_experiment


ERROR:root:Line magic function `%lprun` not found.

In [ ]: