Default-Dict
title: "Default Dict"
author: "Rj"
date: 2019-04-20
description: "List Test"
type: technical_note
draft: false
source: https://www.accelebrate.com/blog/using-defaultdict-python/
from collections import defaultdict
user = defaultdict(lambda: 'Kevin')
defaultdict(<function __main__.<lambda>>, {})
user['name'] = 'Peter'
user['age'] = 21 …
Read More
Domain-Finder
title: "Find Domain"
author: "Rj"
date: 2019-04-20
description: "List Test"
type: technical_note
draft: false
def get_domain(url):
spltAr = url.split("://")
i = (0,1)[len(spltAr)>1]
dm = spltAr[i].split("?")[0].split('/')[0].split(':')[0].lower()
#print(dm)
return dm
def get_domain_without_prefix(url):
spltAr = url.split("://")
i = (0,1)[len …
Read More
Enumerate-List
title: "Enumerate List"
author: "Raja CSP Raman"
date: 2019-04-20
description: "-"
type: technical_note
draft: false
animals = ['cat', 'dog', 'monkey']
for idx, animal in enumerate(animals):
print ('#%d: %s' % (idx , animal))
#0: cat
#1: dog
#2: monkey
Score: 0
Read More
Enumerate-Sample
title: "Enumerate"
author: "Rj"
date: 2019-04-20
description: "List Test"
type: technical_note
draft: false
data = ['Toronto', 'Montreal', 'Waterloo', 'Chennai', 'Bangalore', 123, ['One', 'Two', 'Three']]
for x in enumerate(data):
print(x)
(0, 'Toronto')
(1, 'Montreal')
(2, 'Waterloo')
(3, 'Chennai')
(4, 'Bangalore')
(5, 123)
(6, ['One', 'Two', 'Three'])
Read More
Euclidean-Compare
title: "Euclidean Compare"
author: "Rj"
date: 2019-04-20
description: "-"
type: technical_note
draft: false
import matplotlib
import numpy
import perfplot
from scipy.spatial import distance
def linalg_norm(data):
a, b = data
return numpy.linalg.norm(a-b, axis=1)
def sqrt_sum(data):
a, b = data
return numpy.sqrt(numpy.sum((a-b …
Read More
Euclidean-Distance
title: "Euclidean Distance"
author: "Raja CSP Raman"
date: 2019-04-20
description: "-"
type: technical_note
draft: false
def euclidean_distance(p, q):
#make sure both array are in the same dimension
assert len(p) == len(q)
return max([abs(x-y) for x,y in zip(p,q)])
acb = euclidean_distance([1,2,3],[1 …
Read More
Euclidean-Distance-Vanilla
title: "Euclidean Distance Vanilla"
author: "Rj"
date: 2019-04-20
description: "-"
type: technical_note
draft: false
def dist(x,y):
return numpy.sqrt(numpy.sum((x-y)**2))
a = numpy.array((1,2,3))
b = numpy.array((4,5,6))
dist_a_b = dist(a,b)
Score: 5
Read More
Euclidean-Simple
title: "Euclidean Simple"
author: "Rj"
date: 2019-04-20
description: "-"
type: technical_note
draft: false
import math
import numpy
from random import uniform
def fastest_calc_dist(p1,p2):
return math.sqrt((p2[0] - p1[0]) ** 2 +
(p2[1] - p1[1]) ** 2 +
(p2[2] - p1[2]) ** 2)
def math_calc_dist(p1,p2):
return math.sqrt(math …
Read More
Execution-Time-Calculator
title: "Execution Time Calculator"
author: "Raja CSP Raman"
date: 2019-04-20
description: "-"
type: technical_note
draft: false
import random
import time
# PythonDecorators/entry_exit_class.py
class execution_time_calculator(object):
def __init__(self, f):
self.f = f
def __call__(self):
start = int(round(time.time() * 1000))
print("Entering", self.f.__name__)
self.f()
end = int …
Read More