Skip to content

Instantly share code, notes, and snippets.

@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
@htv2012
htv2012 / listdlls_tryout.py
Last active April 13, 2018 22:36
Find out which process is using a specific DLL
#!/usr/bin/env python
from __future__ import print_function, unicode_literals
import os
import re
import subprocess
"""
Sample listdlls output:
@htv2012
htv2012 / json_logging.py
Last active September 1, 2017 07:18
How to log to JSON
#!/usr/bin/env python
import json
import logging
class JSONFormatter:
"""
A simple formatter class which takes a record and return the fields
in JSON format
"""
@htv2012
htv2012 / App.java
Created August 22, 2017 14:54
Java Skeleton
public class App {
public void start() {
}
public static void main(String[] args) {
App app = new App();
app.start();
}
}
@htv2012
htv2012 / accessor.java
Created June 2, 2017 15:11
Java property with accessor
T m_var;
public T getVar() { return this.m_var; }
public Class setVar(T newValue) { this.m_var = newValue; return this; }
@htv2012
htv2012 / CustomMatchersTest.java
Created May 18, 2017 17:13
# Custom Matcher to Compare Maps
package net.southeastwind.test;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static net.southeastwind.test.SameMapAs.sameMapAs;
@htv2012
htv2012 / lines_range_in_a_file.py
Created August 7, 2015 20:59
Given a text file, provide a quick and easy way to return a range of lines
"""
Given a text file, provide a quick and easy way to return a range of
lines
"""
from itertools import islice
def select_lines(iterable, start_line, last_line):
# text lines usually 1-based, whereas islice is 0-base ==> -1
@htv2012
htv2012 / compare_list_of_objects.py
Last active August 29, 2015 14:26
In unittest, assertItemsEqual() does not work for lists of objects, even if these objects implement the __eq__ method for comparison. This module provide assertUnorderedListsEqual() which does the job.
"""
In unittest, assertItemsEqual() does not work for lists of objects,
even if these objects implement the __eq__ method for comparison.
This module provide assertUnorderedListsEqual() which does the job.
"""
import unittest
def list_diff(a, b):
@htv2012
htv2012 / compare_objects.py
Created August 4, 2015 22:02
How to compare objects
"""
In unittest, when we need to compare two objects via assertEqual(), we
have two choices: implement the class' __eq__() or write a comparison
function and register it via addTypeEqualityFunc(). Each of these
approaches has its pros and cons.
The __eq__ Method
=================
Pros