Skip to content

Instantly share code, notes, and snippets.

View kylelambert101's full-sized avatar

Kyle kylelambert101

View GitHub Profile
@kylelambert101
kylelambert101 / RegularExpressions.py
Last active August 18, 2020 17:03
Python -> Regular Expressions
import re
'''=== TL;DR ==='''
# if only expecting one match per string, use re.search
pattern = r"(.+)\s+(.+)"
myString = "Two words"
m = re.search(pattern, myString)
if m:
fullmatch = m.group(0)
match1 = m.group(1)
@kylelambert101
kylelambert101 / LambdaSort.py
Created August 18, 2020 17:07
Python -> Sort a list of objects by field
# To sort a list of objects by the field `foo`:
sortedList = sorted(myList, key = lambda x: x.foo, reverse=True)
@kylelambert101
kylelambert101 / ReadCSV.py
Created August 18, 2020 17:10
Python -> Read a CSV File
import pandas as pd
# I used to use a csv_reader to read in csv files, but pandas is much better.
music_data = pd.read_csv('music_collection.csv')
# music_data can now be accessed as a dict e.g:
all_days = music_data['Day'].unique()
@kylelambert101
kylelambert101 / TraceProps.tsx
Created August 18, 2020 17:21
React -> Trace Prop Changes
// Use this custom hook when you are trying to trace why a component is re-rendering
const useTraceUpdate = (componentName: string, props: any): void => {
// Capture previous prop state
const prev = React.useRef(props);
React.useEffect(() => {
// Check all field/value pairs of the new props for changes
const changedProps = Object.entries(props).reduce(
(changeSummary, [fieldName, currentValue]) => {
const previousValue = Reflect.get(prev.current, fieldName);
return previousValue === currentValue
@kylelambert101
kylelambert101 / Dates.py
Created August 18, 2020 17:23
Python -> Date Formatting and Parsing
# See https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes
from datetime import datetime
dateString = "2020-05-31"
# parse a string into a date
myDateTime = datetime.strptime(dateString, '%Y-%m-%d')
# if you want to strip out time
myDate = myDateTime.date()
@kylelambert101
kylelambert101 / DictIteration.py
Created August 18, 2020 17:24
Python -> Iterate through a dict
for key, value in myDict.items():
print(f'Key is {key}, value is {value}')
@kylelambert101
kylelambert101 / Arguments.py
Created August 18, 2020 17:26
Python -> Capture command line arguments
# Capture all arguments
args = sys.argv
# Capture only first argument
arg = sys.argv[0]
# Capture all arguments after the first
other_args = sys.argv[1:]
@kylelambert101
kylelambert101 / FileExists.py
Created August 18, 2020 17:27
Python -> Check if file or directory exists
from os import path
result = path.exists(myPath)
# Check whether it is specifically a file
result = path.isfile(myPath)
@kylelambert101
kylelambert101 / FlattenArray.js
Created August 18, 2020 17:29
JavaScript -> Flatten an array
// To flatten a JS array of arrays (a.k.a get a master array with all values from children)...
// flat is newer - not available in IE
const foo = myArray.flat();
// reduce is another option
const bar = myArray.reduce((allData,currentArray) => allData.concat(currentArray));
@kylelambert101
kylelambert101 / JSON.js
Created August 18, 2020 17:30
JavaScript -> JSON Strings
// Parse a string into a JSON object
const obj = JSON.parse(myString);
// Stringify an object
// Straight up, no formatting
const str1 = JSON.stringify(obj);
// Pretty-print the JSON with indent of 4
const str2 = JSON.stringify(obj, undefined, 4);