Skip to content

Instantly share code, notes, and snippets.

View sodastsai's full-sized avatar

Tien-Che Tsai sodastsai

View GitHub Profile
@sodastsai
sodastsai / Test_override_storage.py
Last active October 15, 2018 10:04
Django's storage class which would override files
from io import StringIO
from django.core.files.storage import Storage
from django.test import SimpleTestCase as TestCase
from override_storages import OverrideStorageMixin
# noinspection PyAbstractClass
@sodastsai
sodastsai / md-toc.py
Created July 23, 2016 16:42
Markdown TOC
#!/usr/bin/env python
from __future__ import unicode_literals, print_function, absolute_import, division
import os
import re
import sys
from io import StringIO, open
//: Playground - noun: a place where people can play
struct VirtualPrinter: CustomStringConvertible {
var sortingIndex: Int
let name: String
init(name: String, sortingIndex: Int) {
self.name = name
self.sortingIndex = sortingIndex
}
@sodastsai
sodastsai / Iterable, Iterator, and Generator
Last active June 7, 2016 16:59
Iterable, Iterator, and Generator
For Python and ES6:
Iterables:
- 有辦法被套在 `for ... in ...` (Python) or `for ... of ...` (ES6)
- 可以一直重複被列舉內容
- For Python: 有個 method 叫 `__iter__` 會回傳 iterator
- For ES6: 有個 attribute 叫 `Symbol.iterator` 會回傳 iterator
Iterators:
- 可以有效率(效能跟記憶體使用上)的列舉出 collection 中的內容
@sodastsai
sodastsai / singleton.py
Last active September 23, 2019 22:53
Python singleton metaclass (thread-safe)
from __future__ import absolute_import, division, print_function, unicode_literals
from multiprocessing.dummy import Pool as ThreadPool
from multiprocessing import Lock
from threading import get_ident
class SingletonType(type):
def __new__(mcs, name, bases, attrs):
# Assume the target class is created (i.e. this method to be called) in the main thread.
#!/usr/bin/env python
from __future__ import absolute_import, division, print_function, unicode_literals
import pip
def reverse_dependency(package_name):
rev_dep = [
pkg.project_name for pkg in pip.get_installed_distributions()
if package_name in [requirement.project_name for requirement in pkg.requires()]
]
@sodastsai
sodastsai / guard-declare.h
Last active August 25, 2017 03:09
Swift `guard` in Objective-C
#define guard(CONDITION) if (CONDITION) {}
import datetime
from itertools import permutations
def test_body(a, b, c, d, e, f, g, h, i):
mul_result = (a*10 + b) * c
add_result = mul_result + (f*10 + g)
return mul_result == d*10 + e and add_result == h*10 + i
now = datetime.datetime.now()
@interface SomeOtherClass : NSObject
@end
@implementation SomeOtherClass
- (void)performAction1:(id)param {
if ([param isKindOfClass:[NSNumber class]] || [param isKindOfClass:[NSString class]]) {
NSLog(@"%lf", [param doubleValue]);
} else {
class E(Exception):
def __init__(self, *args, **kwargs):
super(E, self).__init__(*args, **kwargs)
print('Create exception')
def __del__(self):
print('Discard exception')
def __str__(self):
return 'my exception'
def f():