Skip to content

Instantly share code, notes, and snippets.

View hwshim0810's full-sized avatar

Hyunwoo Shim hwshim0810

View GitHub Profile
@hwshim0810
hwshim0810 / python_overloading.py
Created March 6, 2018 04:08
Overloading in python
registry = {}
class MultiMethod(object):
def __init__(self, name):
self.name = name
self.typemap = {}
def __call__(self, *args):
# a generator expression
@hwshim0810
hwshim0810 / model_utils.py
Created March 6, 2018 04:11
Generate unique key with django model
import string
from random import SystemRandom
def generate_key(field_name, model, size=8):
charset = string.ascii_lowercase + string.digits
key = ''.join(SystemRandom().choice(charset) for _ in range(size))
is_exist = model._default_manager.filter(**{field_name: key}).exists()
while is_exist:
key = generate_key(size, model)
@hwshim0810
hwshim0810 / queryset_util.py
Last active March 15, 2018 07:01
Django queryset filter by builderpattern
import operator
from functools import reduce
from django.db.models import Q
class QueryBuilder:
"""
Continuous queryset filtering
"""
@hwshim0810
hwshim0810 / views.py
Created March 15, 2018 07:07
Django queryset filter : group by max limit 1
"""
class SampleCategoryModel
id = primary
class SamplePostModel
category = foreignkey(SampleCategoryModel)
created_at = datetimefield
"""
from django.db.models import Max
@hwshim0810
hwshim0810 / function.py
Last active March 26, 2018 07:51
get ip in django
def get_client_ip(request):
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
if x_forwarded_for:
return x_forwarded_for.split(',')[0]
else:
return request.META.get('REMOTE_ADDR')
@hwshim0810
hwshim0810 / git.sh
Created April 4, 2018 14:28
Copy git repository to new repository with reservation history
git clone --mirror [OriginRepo.git]
cd [OriginRepo.git]
git remote set-url --push origin [New Repo.git]
git push --mirror
@hwshim0810
hwshim0810 / modal.html
Created April 24, 2018 15:41
Bootstrap modal example
<a href="#modalBox" data-toggle="modal">OpenButton</a>
<div class="modal fade" id="modalBox" tabindex="-1" role="dialog" aria-labelledby="modal-label" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="modal-label">
Title Area
</h4>
@hwshim0810
hwshim0810 / onback.kt
Created May 14, 2018 14:26
onBackPressed with rxjava :: in presenter
private val backButtonSubject: Subject<Long> = BehaviorSubject.createDefault(0L).toSerialized()
private val backButtonSubjectDisposable =
compositeDisposable.add(backButtonSubject.toFlowable(BackpressureStrategy.BUFFER)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) // need compose
.buffer(2, 1)
.map { it[0] to it[1] }
.subscribe({ value ->
if (value.second - value.first < 2000) getView()?.finishView()
@hwshim0810
hwshim0810 / function.py
Created May 17, 2018 11:08
Get if int or rounded float
def get_if_int_or_float(value, under_num=1):
return int(value) if value.is_integer() else round(value, under_num)
@hwshim0810
hwshim0810 / mixin.scss
Created May 20, 2018 14:12
Simple responsive mixin
@mixin breakpoint($point) {
@if $point == desktop {
@media (min-width: 70em) {
@content;
}
}
@else if $point == laptop {
@media (min-width: 64em) {
@content;
}