Lambda Function in Python

Sort in non-standard way

In python2

>>> a = ['floor', 'house', 'room', 'roof']
>>> a.sort(lambda x,y: cmp(len(x), len(y)))
>>> a
['room', 'roof', 'floor', 'house']
>>>

In Python3

>>> a = ['floor', 'house', 'room', 'roof']
>>> a.sort(key=lambda x: (len(x)))
>>> a
['room', 'roof', 'floor', 'house']
>>>

Closures - functions evaluated in an environment containing bound variables

In python2

>>> def comp(threshold):
...     return lambda x: x < threshold
...
>>> func_a = comp(10)
>>> func_b = comp(20)
>>> print func_a(5), func_a(9), func_a(15), func_a(25)
True True False False
>>> print func_b(5), func_b(9), func_b(15), func_b(25)
True True True False

In python3

>>> def comp(threshold):
...     return lambda x: x < threshold
...
>>> func_a = comp(10)
>>> func_b = comp(20)
>>> print(func_a(5), func_a(9), func_a(15), func_a(25))
True True False False
>>> print(func_b(5), func_b(9), func_b(15), func_b(25))
True True True False

Mapping - performs a function call on each element of a list.

In python2

>>> a = [1, 2, 3, 4, 5, 6]
>>> print map(lambda x: x*x, a)
[1, 4, 9, 16, 25, 36]

In python3

>>> a = [1, 2, 3, 4, 5, 6]
>>> [item for item in map(lambda x: x*x, a)]
[1, 4, 9, 16, 25, 36]

Filter - returns all elements from a list that evaluate True when passed to a certain function.

In python2

>>> a = [1, 2, 3, 4, 5, 6]
>>> filter(lambda x: x % 2 == 0, a)
[2, 4, 6]

In python3

>>> a = [1, 2, 3, 4, 5, 6]
>>> list(filter(lambda x: x % 2 == 0, a))
[2, 4, 6]

Reduce - The fold/reduce function runs over all elements in a list (usually left-to-right), accumulating a value as it goes.

>>> from functools import reduce
>>> a = [1, 2, 3, 4, 5, 6]
>>> reduce(lambda x, y: x+y, a)
21
>>>

This performs (((((1 + 2) + 3) + 4) + 5) + 6) = 21

Read More

Python OS Functions

The OS module in Python provides functions to using operating system dependent functionality.
To use it, import the OS module “import os

Read More

ZFS Dataset is Busy Solution

Solution on Ubuntu:
Using the following command and find with process is holding the dataset.

grep zroot/2013-10-15T065955229209 /proc/*/mounts

note: replace zroot/2013-10-15T065955229209 as your mount point.

Read More

Install Linux On A Chromebook

Create OS Recovery Media

  1. Insert a USB stick 4GB or greater
  2. Install Chromebook Recovery Utility extension for chrome browser
  3. Launch Chromebook Recovery Utility and follow the guide to create OS Recovery Media
Read More