Skip to content

Instantly share code, notes, and snippets.

View quietcricket's full-sized avatar

Shang Liang quietcricket

  • Singapore
View GitHub Profile
@quietcricket
quietcricket / ec2_ami_linux_2_installation.sh
Last active September 25, 2019 04:31
EC2 AMI Linux 2 Installation, with python, uwsgi, supervisor, certbot & nginx
sudo yum update
# call amazon-linux-extras to view what packages are available
sudo amazon-linux-extras install nginx1
sudo systemctl enable nginx.service
sudo systemctl start nginx.service
# gcc and python-devel required for uwsgi
@quietcricket
quietcricket / sudoku_checker.py
Created May 11, 2018 03:07
Algorithm to check if a sudoku solution is valid
from __future__ import print_function, division
def check_sudoku(arr):
# 9 rows, 9 columns and 9 sub squares
# Each of them need to have all the digits
# Not using bit shifting
alignments = [987654321] * 27
for i, row in enumerate(arr):
for j, x in enumerate(row):
@quietcricket
quietcricket / iconutil_icns.sh
Created April 19, 2018 05:56
Create a icns file from a 1024x1024 png file
mkdir MyIcon.iconset
sips -z 16 16 Icon1024.png --out MyIcon.iconset/icon_16x16.png
sips -z 32 32 Icon1024.png --out MyIcon.iconset/icon_16x16@2x.png
sips -z 32 32 Icon1024.png --out MyIcon.iconset/icon_32x32.png
sips -z 64 64 Icon1024.png --out MyIcon.iconset/icon_32x32@2x.png
sips -z 128 128 Icon1024.png --out MyIcon.iconset/icon_128x128.png
sips -z 256 256 Icon1024.png --out MyIcon.iconset/icon_128x128@2x.png
sips -z 256 256 Icon1024.png --out MyIcon.iconset/icon_256x256.png
sips -z 512 512 Icon1024.png --out MyIcon.iconset/icon_256x256@2x.png
sips -z 512 512 Icon1024.png --out MyIcon.iconset/icon_512x512.png
@quietcricket
quietcricket / esp8266_micropython.md
Last active May 31, 2018 06:43
ESP8266 with MicroPython
@quietcricket
quietcricket / text2pdf.py
Created March 13, 2018 10:43
Generate PDF from plain text
import sys
from reportlab.lib.pagesizes import *
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import inch
from reportlab.platypus import Paragraph, SimpleDocTemplate, Spacer, PageBreak
from reportlab.lib.enums import *
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
@quietcricket
quietcricket / esp1866-micropython-upload.sh
Created February 26, 2018 05:31
esp1866 micropython upload
esptool.py --port /dev/tty.wchusbserial1410 --baud 115200 write_flash -fm dio --flash_size=detect 0 esp8266-20171101-v1.9.3.bin
screen /dev/tty.wchusbserial1410 -b115200
@quietcricket
quietcricket / coding_considerations.md
Created November 13, 2017 04:38
Coding with considerations
  • Sacrifice storage for computing time/power: store calculated values and avoid batch job
  • Sacrifice best OOP/DRY practice to simplify complexity: avoid having too many levels of inheritance and compositions. The human brain cannot handle too much info.
  • Avoid having too many helper/factory files: when modifying one function, we shoudn't need to jump back and forth from many other files.
@quietcricket
quietcricket / mongoengine-sphinx-config.py
Created November 10, 2017 03:50
Customize sphinx for projects using mongoengine ORM.
"""
If you try to generate documentations using sphinx's autodoc with models extending mongoengine's Document You are going to have a bad time
1. The code will call Document.objects and list all the entries in your doc!!!
2. Your model's fields will inherit the default docstr from Field class if you don't have any comment. The documentation generated is very long and difficult to read
This snippet helps to solve these 2 problems. Insert them into config.py file.
"""
import re
from mongoengine.base.fields import ObjectIdField
@quietcricket
quietcricket / upload_s3_boto3.py
Last active February 23, 2021 02:23
Upload file to aws s3 with boto3
import mimetypes
def upload_s3(key, fileobj, bucket_name, cache=999999):
"""
Lots of problems with new boto3 library. Many functions are not documented or wrongly documented
1. s3.Object.metadata, s3.Object.cache_control, s3.Object.content_type: are read only, gotcha
2. If you set it with `ExtraArgs={'Metadata':{...}}`, wrong again. Cache-Control will become amz-x-cache-control,
not what you wanted
3. If you set it with `ExtraArgs={'Cache-Control':'max-age=999'}`, wrong again! They need too be CacheControl and ContentType
"""
@quietcricket
quietcricket / download.py
Created November 8, 2017 10:10
Download image using requests and process it with PIL then upload to s3
def download_file(url):
r = requests.get(url)
if r.status_code != 200 or len(r.content) < 100:
raise 'Download Failed'
if re.search('\.png', url):
file_type = 'png'
elif re.search('\.gif', url):