Skip to content

Instantly share code, notes, and snippets.

View alvinwan's full-sized avatar
🦉
🧀🍰

Alvin Wan alvinwan

🦉
🧀🍰
View GitHub Profile
@alvinwan
alvinwan / argparse_to_dict.py
Created February 26, 2024 07:28
Extract argparse information
import argparse
import json
def get_argparse_spec_as_dict(parser):
"""
Gets the argparse spec as a dictionary.
This makes transformations on the argument parser a little
easier.
"""
@alvinwan
alvinwan / MyLibrary.py
Created February 19, 2024 08:47
How to mock submodules from just a single file
"""
"Hello world" example of a single-file 'library'.
After importing this file, you can then use this file to mock submodules for
a library. For example,
import main # this file
import torch.nn.functional # 'imports' the mock module below instead
This helps with debugging, logging, and more.
@alvinwan
alvinwan / MyMetalKernel.py
Last active October 15, 2023 06:58
How to write a minimal, standalone Python script to run Metal (GPU) kernels on Mac
"""
"Hello world" example of using Metal from Python.
This script can be run from the command line just like any other Python file. No
need for Xcode or any other IDE. Just make sure you have the latest version of
Python 3 installed, along with the PyObjC and pyobjc-framework-Metal packages.
"""
import Metal
import ctypes
@alvinwan
alvinwan / MyApplication.py
Last active May 21, 2023 04:43
How to build a Python Mac app with deeplink support (e.g., custom URL protocol handlers)
"""File that runs your application. This file will be called by the deeplink"""
import os
import sys
with open(os.path.expanduser('~/Downloads/myapp.txt'), 'w') as f: # write to a myapp.txt in your Downloads folder
f.write(str(sys.argv)) # deeplink will be passed as sys.argv[1]
@alvinwan
alvinwan / .bash_profile
Last active October 22, 2021 06:10
.bash_profile
# Use this ~/.bash_profile script on your Glitch instance.
#
# Hello! This file isn't necessary to look at. All you need to know
# is that:
#
# 1. You can create Python files on the left "New File" and use
# `numpy` or `opencv-python` freely.
# 2. On the bottom left, click on "Tools > Terminal" to open terminal
# and run `python <script>` just as you would normally on your
# own computer.
@alvinwan
alvinwan / README.md
Last active September 14, 2021 02:49
Why all Python class constructor should call their parent constructors
@alvinwan
alvinwan / readme.md
Created September 14, 2021 02:09
Enhanced Weather Report

Running the above report.py gets us the temperature for any city.

> python report.py
City: Oakland
State: California
Country: United States
+73°F
@alvinwan
alvinwan / script0.py
Last active September 11, 2021 08:07
c = input("enter amount and currency with the sign RMB/EUR:")
if c[-3:-1i] in ['rmb','RMB','Rmb']:
E=eval(c[0:-3])/7.8942
print("your converted currency is {:.2f}EUR".format(E))
elif c[-3:-1]in['EUR','Eur','eur']:
R=eval(c[0:-3])*7.8942
print("your converted currency is {:.2f}RMB".format(R))
else:
print("Format Error")
@alvinwan
alvinwan / interactive.txt
Created August 31, 2021 19:02
Get key by Python dictionary value
>>> list_to_dict=[{"a":1},{"b":2}]
>>> for key, value in list_to_dict.items():
... if value == 1:
... break
...
>>> key
'a'
@alvinwan
alvinwan / einstein_riddle.py
Last active August 27, 2021 07:51
Einstein riddle solver in Python, for Skillshare student question
finalized = [{}, {}, {}, {}, {}] # 5 houses, with no properties set yet
# 1. express all constraints
constraints = [
[{'nationality': 'Brit', 'color': 'red'}], # the Brit lives in the red house
[{'drink': 'coffee', 'color': 'green', 'offset': 0}, {'color': white', 'offset': 1}],
...
]
# 2. iteratively...