Skip to content

Instantly share code, notes, and snippets.

@htv2012
htv2012 / Preferences.sublime-settings
Last active August 29, 2015 14:20
Sensible preferences for Sublime Text
{
"always_show_minimap_viewport": true,
"ensure_newline_at_eof_on_save": true,
"font_size": 12,
"highlight_line": true,
"remember_full_screen": true,
"rulers": [72, 80, 120],
"show_encoding": true,
"show_line_endings": true,
"translate_tabs_to_spaces": true,
@htv2012
htv2012 / closed_object_with_decorator.py
Last active August 29, 2015 14:20
Demo: an object that after closed, is in invalid state. At this point, any method invocation will result in an exception unless that method is decorated. Output: http://codepad.org/BUssgQR3
import functools
def accessible_while_closed(func):
"""
A decorator to mark a function as available, even after the object
closed.
"""
func.accessible_while_closed = True
return func
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This script takes in a single parameter representing a directory
and traverse that directory to list the exceptions that are ignored.
"""
from __future__ import print_function
import ast
import os
import sys
@htv2012
htv2012 / detect-platform.sh
Last active February 4, 2025 17:33
How to Detect Platform and Distro
# Detect platform in bash
# https://stackoverflow.com/a/3466183/459745
case $(uname -s) in
Linux*) platform=Linux;;
Darwin*) platform=Mac;;
CYGWIN*) platform=Cygwin;;
MINGW*) platform=MinGw;;
*) platform=Unknown;;
esac
@htv2012
htv2012 / strip_accents.py
Created January 29, 2020 14:23
Strip the accents from a unicode (e.g. Vietnamese) text
def strip_accents(text):
"""
Strips the accents, replace the dd, and lower case
"""
text = text.replace("đ", "d").replace("Đ", "d")
text = unicodedata.normalize("NFD", text)
text = text.encode("ascii", "ignore")
text = text.decode("utf-8")
text = text.lower()
return text
@htv2012
htv2012 / repr.py
Created September 3, 2024 16:34
Create a __repr__ for a class
#!/usr/bin/env python3
"""
Generate the repr for a class
"""
import argparse
import io
import subprocess
import platform