Posts about Python

Time the performance by timeit and cell magic

Goal

This post aims to introduce how to measure the running time of your code or function by using timeit module and cell magic command %%time and %%timeit.

Reference *Time a Python Function

Libraries

In [1]:
import pandas as pd
import timeit

Measure a literal code

In [2]:
timeit.timeit('2**10')
Out[2]:
0.01853936500265263

Measure the execution time for a function

In [4]:
def execute_abc(x=10):
    return 2 ** 10
In [7]:
timeit.timeit(execute_abc)
Out[7]:
0.0984989230055362

Measure the time by using Cell Magic in Jupyter Notebook

In [3]:
%%time
2**10
CPU times: user 4 µs, sys: 1e+03 ns, total: 5 µs
Wall time: 11 µs
Out[3]:
1024
In [9]:
%timeit 2**10
17.9 ns ± 0.597 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)

Create A Temporary File

Goal

This post aims to create a temporary file using tempfile

Reference

Libraries

In [1]:
import tempfile

Create a temporary file

In [9]:
temp_file = tempfile.NamedTemporaryFile()
temp_file.name
Out[9]:
'/var/folders/9_/tl0k78wd62xchzsh_4x4bz0h0000gn/T/tmp75145s92'
In [23]:
!ls -a /var/folders/9_/tl0k78wd62xchzsh_4x4bz0h0000gn/T/ | grep tmp7
tmp75145s92

Createa a temporary directory

In [8]:
temp_dir = tempfile.TemporaryDirectory()
temp_dir.name
Out[8]:
'/var/folders/9_/tl0k78wd62xchzsh_4x4bz0h0000gn/T/tmpje1mz_wj'
In [19]:
!ls /var/folders/9_/tl0k78wd62xchzsh_4x4bz0h0000gn/T/ | grep tmpje
tmpje1mz_wj

Data Exploration Tool - Lantern Part 1

Overview

Lantern is a python module for a toolkit collection for data exploration from a variety of dataset to visualization.

In this post, I will walk through the followings:

  • How to set up lantern
  • What lantern can do
    • dataset
    • plot (visualization)
    • grid (interactive table view)
    • widget