Skip to content

Instantly share code, notes, and snippets.

@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
@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 / detect-platform.sh
Last active January 29, 2020 14:27
How to Detect Platform
# 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
#!/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 / 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
@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 / Anaconda.sublime-settings
Last active August 29, 2015 14:20
Settings for Anaconda plugin
{
"anaconda_linter_underlines": false,
"pep8_max_line_length": 72,
"zzz": 1
}
@htv2012
htv2012 / skeleton.py
Last active May 9, 2023 16:06
Python Skeleton
#!/usr/bin/env python3
def main():
pass
if __name__ == "__main__":
main()
@htv2012
htv2012 / closed_object.py
Last active August 29, 2015 14:19
Demo: a object that, after close, is in an invalid state. At this point, any method invocation will result in an exception. Output: http://codepad.org/34k4e7wB
class Foo(object):
def __invalid_action(self, *args, **kwargs):
raise IOError('Sorry, we are closed')
def __init__(self):
print 'create'
def read(self):
print 'read'
@htv2012
htv2012 / return_mutable_attributes_is_dangerous.py
Created February 20, 2015 15:45
Why return a mutable attribute might be dangerous
#!/usr/bin/env python
""" Why return a mutable attribute might be dangerous """
class Car(object):
def __init__(self, owners):
self._owners = owners
def get_owners(self):
""" Return a list of owners. Getter/setters are evil, but that