Skip to content

Instantly share code, notes, and snippets.

View sksoumik's full-sized avatar

Sadman Kabir Soumik sksoumik

View GitHub Profile
import cv2
# read an image from disk in RGB format
image = cv2.imread("your/image/path", 1)
# display the image in a specified window
cv2.imshow("Window Name", image)
# set the time 5 seconds for displaying the window
cv2.waitKey(5000)
import functools
sequence = [10, 20, 30, 15]
"""
-----------------------------------------------------
Example 1: sum up all the items in the sequence list.
-----------------------------------------------------
"""
# Reading json files
import json
def read_json_content(file_path):
with open(file_path, 'r', encoding='utf-8') as fp:
data = json.loads(fp.read())
if data is None:
raise Exception('Invalid JSON')
return data
# Read multiple json files from a directory and convert them to pandas dataframe
# Add file names as a new column in DataFrame
import pandas as pd
import json
import os
path_to_json = 'path/to/json/files'
data_list = []
import cv2
import matplotlib.pyplot as plt
sample_image = cv2.imread("path/of/image")
plt.axis("off")
plt.imshow(cv2.cvtColor(sample_image, cv2.COLOR_BGR2RGB))
plt.show()
seq = [1, 2, 3, 4]
# result contains odd numbers of the list
result = filter(lambda x: x + 1, seq)
print(list(result)) # Output: [1, 2, 3, 4]
# Does nothing on seq as the lambda function does not hold any condition
# filter only works on condition, so we need to give some conditional statement with lambda
result = filter(lambda x: x % 2 == 0,
seq) # returns the elements which are only divisible by two
print(list(result)) # output: [2, 4]
def add_t(x):
return x + 1
tuple_1 = (1, 2, 3, 4)
map_sample_1 = tuple(map(add_t, tuple_1))
print(map_sample_1) # output: (2, 3, 4, 5)
# ---------------------------------------------------------------
# A lambda function that multiplies two numbers
multiply = lambda x, y: x * y
print(multiply(2, 2)) # output: 4
# A lambda function that multiplies three numbers
multiply = lambda x, y, z: x * y * z
print(multiply(2, 2, 2)) # output: 8
# A lambda function that adds 10 to the number passed in as an argument, and print the resul
a = lambda a: a + 10
class Person(models.Model):
name = models.CharField(max_length=255) # Mandatory
bio = models.TextField(max_length=500, blank=True) # Optional (don't put null=True)
birth_date = models.DateField(null=True, blank=True) # Optional (here you may add null=True)
# Merge Sort | Time Complexity O(NlogN)
def mergeSort(alist):
print("Splitting ", alist)
if len(alist) > 1:
mid = len(alist) // 2
lefthalf = alist[:mid]
righthalf = alist[mid:]
mergeSort(lefthalf)
print("LeftHalf ", lefthalf)