Default-Dict

Sat 17 May 2025

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')
user
defaultdict(<function __main__.<lambda>>, {})
type(user)
collections.defaultdict
user['abc']
'Kevin'
user['country']
'Kevin'
user['name'] = 'Peter'
user['age'] = 21 …

Category: basics

Read More

Dns-Lookup

Sat 17 May 2025

title: "DNS Lookup" author: "Rj" date: 2019-04-20 description: "-" type: technical_note draft: false


import socket
addr1 = socket.gethostbyname('google.com')
addr2 = socket.gethostbyname('yahoo.com')
print(addr1)
172.217.0.238
print(addr2)
98.137.246.8


Score: 5

Category: basics

Read More

Domain-Finder

Sat 17 May 2025

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 …

Category: basics

Read More

Enumerate-List

Sat 17 May 2025

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

Category: basics

Read More

Enumerate-Sample

Sat 17 May 2025

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'])
for i, x in …

Category: basics

Read More

Euclidean-Compare

Sat 17 May 2025

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 …

Category: basics

Read More

Euclidean-Distance

Sat 17 May 2025

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 …

Category: basics

Read More

Euclidean-Distance-Vanilla

Sat 17 May 2025

title: "Euclidean Distance Vanilla" author: "Rj" date: 2019-04-20 description: "-" type: technical_note draft: false


import numpy
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)
print(dist_a_b)
5.196152422706632


Score: 5

Category: basics

Read More

Euclidean-Simple

Sat 17 May 2025

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 …

Category: basics

Read More

Execution-Time-Calculator

Sat 17 May 2025

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 …

Category: basics

Read More
Page 4 of 17

« Prev Next »