Skip to content

Instantly share code, notes, and snippets.

@leveled
leveled / check_word_size_macos.sh
Created June 25, 2021 20:04
Check word size on MacOS machine
getconf LONG_BIT
@leveled
leveled / restart_gnome_shell.sh
Created March 6, 2021 00:39
Restart Gnome Shell
busctl --user call org.gnome.Shell /org/gnome/Shell org.gnome.Shell Eval s 'Meta.restart("Restarting…")'
@leveled
leveled / macos_version.sh
Created February 23, 2021 12:52
Return MacOS Version Via Terminal
sw_vers -productVersion
@leveled
leveled / show_listening_tcp_ports.sh
Created February 23, 2021 12:41
Show listening tcp ports on MacOS
netstat -an -ptcp
@leveled
leveled / try_except_finally.py
Created February 22, 2021 01:00
Try except finally example block in python
try:
f = open("demofile.txt")
f.write("Lorum Ipsum")
except:
print("Something went wrong when writing to the file")
finally:
f.close()
@leveled
leveled / asterisk_unpacking.py
Created February 22, 2021 00:49
Asterisk unpacking cheatsheet in Python
#Unpack tuples
>>> fruits = ['lemon', 'pear', 'watermelon', 'tomato']
>>> first, second, *remaining = fruits
>>> remaining
['watermelon', 'tomato']
>>> first, *remaining = fruits
>>> remaining
['pear', 'watermelon', 'tomato']
>>> first, *middle, last = fruits
>>> middle
@leveled
leveled / capture_all_keyword_arguments.py
Created February 22, 2021 00:46
Capture all keyword arguments provided to a function in Python
def tag(tag_name, **attributes):
attribute_list = [
f'{name}="{value}"'
for name, value in attributes.items()
]
return f"<{tag_name} {' '.join(attribute_list)}>"
>>> tag('a', href="http://treyhunner.com")
'<a href="http://treyhunner.com">'
>>> tag('img', height=20, width=40, src="face.jpg")
@leveled
leveled / capture_all_positional_args.py
Created February 22, 2021 00:46
Capture an unlimited number of positional arguments given to a function in Python
from random import randint
def roll(*dice):
return sum(randint(1, die) for die in dice)
>>> roll(20)
18
>>> roll(6, 6)
9
>>> roll(6, 6, 6)
@leveled
leveled / unpacking_iterables.py
Created February 22, 2021 00:41
Unpacking iterables in Python cheatsheet
>>> fruits = ['lemon', 'pear', 'watermelon', 'tomato']
>>> print(fruits[0], fruits[1], fruits[2], fruits[3])
lemon pear watermelon tomato
>>> print(*fruits)
lemon pear watermelon tomato
>>> date_info = {'year': "2020", 'month': "01", 'day': "01"}
>>> filename = "{year}-{month}-{day}.txt".format(**date_info)
>>> filename
'2020-01-01.txt'
@leveled
leveled / zip.py
Last active February 22, 2021 00:38
using the zip function python
fahrenheit = {'t1':-30, 't2':-20, 't3':-10, 't4':0}
#Get the corresponding `celsius` values
celsius = list(map(lambda x: (float(5)/9)*(x-32), fahrenheit.values()))
#Create the `celsius` dictionary
celsius_dict = dict(zip(fahrenheit.keys(), celsius))