Thursday 19 April 2018

loop through a date range in shell


d=2015-12-31
while [ "$d" != 2017-01-01 ]; do
  echo $d
  d=$(date -I -d "$d + 1 day")
done

Friday 23 February 2018

No matching distribution found for requirements.txt

If the  error "No matching distribution found for requirements.txt" occurs while installing pyhton packages in a virtualenv, try this:



pip install --upgrade -r requirements.txt

Monday 24 July 2017

How to get the TLS version of request being made by python code?


Python TLS request version

 
# pyhton 2
python -c "import json, urllib2;\
 print json.load(urllib2.urlopen('https://www.howsmyssl.com/a/check'))['tls_version']"


# pyhton 3
python3 -c "import json, urllib.request;\
print(json.loads(urllib.request.urlopen('https://www.howsmyssl.com/a/check').read().decode('UTF-8'))['tls_version'])"

How to get openssl version in python

Python get openSSL version



>>> from OpenSSL import SSL
print(SSL.OPENSSL_VERSION_NUMBER)

Friday 10 March 2017

Variables / Arguments evaluation rules in Scala


Evaluation Rules in Scala

Call by Value: Evaluates the function arguments before calling the function

Call by Name: Evaluates the function first and then evaluates the arguments



def example = 2 // evaluated when called
val example = 2 //evaluated immediately
lazy val example = 2 //evaluated once when needed
def square(x: Double) // call by value def square(x: => Double) // call by name def myFct(bindings: Int*) = { ... } // bindings is a sequence of int,
// containing a varying # of arguments

Scala fold diffrence foldLeft FoldRight , fold v/s foldLeft

What is the difference between fold, folRight, foldLeft in Scala?


Scala provides provides 3 methods to iterate the collection, namely fold, foldLeft and foldRight.

The differences between the three methods is very subtle, and is very simple to understand when we start with analyzing the signature of these 3 functions. 

The signature has 2 things in common:
1) Seed value or the initial value is required for the methods
2) An function/operation is needed, which accepts 2 arguments and returns the same type as the seed value.

Now to understand the differences between these methods lets look at individual signatures.

fold

def fold[A1 >: A](z: A1)(op: (A1, A1) ⇒ A1): A1

The input arguments to the op function is of same type, so fold method always requires
initial value to be of the same type as the collection content.

As we are aware the 3 methods are used to iterate the collections, fold is capable of
arbitrary tree decompositions on the sequential collection. In simple words the operation
specified for fold can execute in Parallel.


val list: List[Int] = List(1, 2, 3, 4, 5)
list.fold(0)(_ + _) // op function takes 2 Int and returns sum(Int)
// possible orders of parallel execution for fold
0 + 1 = 1 0 + 2 = 2 0 + 4 = 4 // 1st parallel operation
1 2 + 3 = 5 4 + 5 = 9 // 2nd parallel operation
1 5 + 9 = 14
1 + 14 = 15



foldLeft

def foldLeft[B](z: B)(op: (B, A) ⇒ B): B

The input arguments to the operation could possibly be of different types,
and one argument must be of the collection content type.

foldLeft sequentially access the collection elements one-by-one from Left to Right.



val list: List[Int] = List(1, 2, 3, 4, 5)
list.foldLeft(0)(_ + _)
// Only possible iteration sequence
0 + 1 = 1
1 + 2 = 3
3 + 3 = 6
6 + 4 = 10
10 + 5 = 15

foldRight


def foldRight[B](z: B)(op: (A, B) ⇒ B): B

foldRight is same as foldLeft except for the access order.

foldRight sequentially access the collection elements one-by-one from Right to Left.


val list: List[Int] = List(1, 2, 3, 4, 5)
list.foldRight(0)(_ + _)
// Only possible iteration sequence, Reverse access to elements
0 + 5 = 5
5 + 4 = 9
9 + 3 = 12
12 + 2 = 14
14 + 1 = 15


Tuesday 30 August 2016

ieee754.c.src:7:29: fatal error: npy_math_common.h: No such file or directory


To fix this:

If you are trying to setup numpy in python virtual env then do the following

sudo apt-get remove python-virtualenv
sudo pip install virtualenv

virtualenv from apt-get doesn't come with it setup tools.


numpy/core/src/npymath/ieee754.c.src:7:29: fatal error: npy_math_common.h: No such file or directory
compilation terminated.
numpy/core/src/npymath/ieee754.c.src:7:29: fatal error: npy_math_common.h: No such file or directory
compilation terminated.

error: Setup script exited with error: Command "gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -Inumpy/core/include -Ibuild/src.linux-x86_64-2.7/numpy/core/include/numpy -I/usr/include/python2.7 -Ibuild/src.linux-x86_64-2.7/numpy/core/src/private -Ibuild/src.linux-x86_64-2.7/numpy/core/src/private -Ibuild/src.linux-x86_64-2.7/numpy/core/src/private -c build/src.linux-x86_64-2.7/numpy/core/src/npymath/ieee754.c -o build/temp.linux-x86_64-2.7/build/src.linux-x86_64-2.7/numpy/core/src/npymath/ieee754.o" failed with exit status 1