This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*- To test without capturing matches -*/ | |
const isUpperCaseLetter = /[A-Z]+/.test(myString); | |
/*- Named capture groups -*/ | |
const result = 'John Doe'.match(/^(?<first>\w+)\s+(?<last>\w+)/)?.groups; | |
/* result: | |
{ | |
first: "John", | |
last: "Doe" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# To sort a list of objects by the field `foo`: | |
sortedList = sorted(myList, key = lambda x: x.foo, reverse=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Simple if/else block | |
MYVAL=$(some_command) | |
if [[ $MYVAL -eq 1 ]] | |
then | |
echo "It was one" | |
elif [[ $MYVAL -eq 2 ]] | |
then | |
echo "It was two" | |
else | |
echo "It was not one" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for key, value in myDict.items(): | |
print(f'Key is {key}, value is {value}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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:] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from os import path | |
result = path.exists(myPath) | |
# Check whether it is specifically a file | |
result = path.isfile(myPath) |
OlderNewer