Skip to content

Instantly share code, notes, and snippets.

View aarshtalati's full-sized avatar
🎯
Focusing

Aarsh Talati aarshtalati

🎯
Focusing
View GitHub Profile
@aarshtalati
aarshtalati / compareDicts.py
Created June 8, 2016 04:31
Python Compare two dictionaries
def compareDicts(d1, d2):
differences = []
# present in d1 but not in d2
for key, value in ({k: d1[k] for k in set(d1) - set(d2)}).iteritems():
temp = list[[key, value], None]
differences.append(temp)
# present in d2 but not in d1
diff2 = {k: d2[k] for k in set(d2) - set(d1)}
@aarshtalati
aarshtalati / StringHelpersAndExtensions.cs
Last active June 28, 2016 18:27
Some useful C# helper and extension methods, fresh from the grill
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml;
// The indentation settings in the Gist do not seem to be listening to me. size: 4, mode: tabs, (w)rap: Atlanta hip hop
@aarshtalati
aarshtalati / DictCustomSort.py
Last active July 2, 2016 02:10
Sort dictionary with values closest to given value
>>> chooseFrom
{'1': 10.3438090737, '3': 7.73275047259, '2': 12.9046550095, '5': 10.3438090737, '4': 12.9046550095, '7': 7.88437303088, '6': 5.12169187146, '8': 0.0}
>>> gh
7.73275047259
# reference : http://stackoverflow.com/a/12141207/799593
>>> test = sorted(chooseFrom, key=lambda x:abs(chooseFrom[x]-gh))
>>> test
['3', '7', '1', '5', '6', '2', '4', '8']
@aarshtalati
aarshtalati / PythonNumericCustomSort.py
Created July 7, 2016 12:55
Python : sort int list based on another int list
>>> a=[2, 3, 4, 5, 6, 7, 8]
>>> b=[4, 2, 3, 8, 1, 5, 7, 6]
>>> sorted(a, key = lambda x: b.index(x))
[4, 2, 3, 8, 5, 7, 6]
>>> a=[2, 3, 4, 5, 6, 7, 8, 9]
>>> sorted(a, key = lambda x: b.index(x))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <lambda>
ValueError: 9 is not in list
@aarshtalati
aarshtalati / FindMostFrequents.py
Created July 7, 2016 18:49
Python: find most common element in corresponding two lists
import itertools
a = [7, 3]
b = [3, 1, 2]
c = [4, 3, 5]
def allEqual(t):
same = True
@aarshtalati
aarshtalati / PillowNumpyAddImages.py
Last active July 9, 2016 16:42
Python: PIL ( Pillow ) NumPy add images
from itertools import izip
from PIL import Image
import numpy as np
def getImageDifference(x, y):
diff3 = 1.0
# http://rosettacode.org/wiki/Percentage_difference_between_images#Python
if x.size == y.size:
pairs = izip(x.getdata(), y.getdata())
@aarshtalati
aarshtalati / KendoUiCoreReferences.html
Created December 30, 2016 18:18
Kendo UI Core References
<link href="~/Content/kendo/kendo.common.min.css" rel="stylesheet" />
<link href="~/Content/kendo/kendo.default.min.css" rel="stylesheet" />
<script src="~/Scripts/KendoUI/jquery.min.js"></script>
<script src="~/Scripts/KendoUI/kendo.core.min.js"></script>
<script src="~/Scripts/KendoUI/kendo.ui.core.min.js"></script>
@aarshtalati
aarshtalati / SplitCsvString_Advanced
Created August 21, 2017 14:25
C# Split CSV string
string str = "Tom Cruise, Scott, ,Bob | at";
IEnumerable<string> names = str
.Split(new char[]{',', '|'})
.Where(x=>x!=null && x.Trim().Length > 0)
.Select(x=>x.Trim());
@aarshtalati
aarshtalati / code.py
Created October 7, 2017 02:38
Reverse and splice python tuple
""" About:
img is an OpenCV 2 object. img.shape prints a tuple as (height, width, channels) e.g. (512, 768, 3)
We need a tuple as (width, height) e.g. (768, 512)
"""
import cv2
image_path = './image.jpg'
img = cv2.imread(image_path, flags=cv2.CV_LOAD_IMAGE_COLOR)
print img.shape # (512, 768, 3)
print img.shape[::-1] # (3, 768, 512)
@aarshtalati
aarshtalati / np_count_elements.py
Created October 15, 2017 23:23
Numpy count elements
import numpy as np
x = np.arange(20).reshape(4,5)
y = x % 3 == 0
y = y.astype(int) # optional, if dealing with only 1s and 0s
# simple
np.count_nonzero(y == 1) # 7
np.count_nonzero(y == 0) # 13
(y == 1).sum() # 7
(y == 0).sum() # 13