Skip to content

Instantly share code, notes, and snippets.

View hirokiky's full-sized avatar

Hiroki Kiyohara hirokiky

View GitHub Profile
@hirokiky
hirokiky / autoBottom.js
Created June 17, 2016 05:46
Vue.js directive to scroll bottom when the applied value updated.
module.exports = function(Vue) {
Vue.directive('auto-bottom', {
update: function() {
this.el.scrollTop = this.el.scrollHeight;
}
})
};
@hirokiky
hirokiky / dockerutil.js
Created July 13, 2016 03:04
Excluding the header docker exec from buffer.
const EXEC_HEADER_LEN = 8;
const EXEC_HEADER_STREAM_LEN = 4;
const EXEC_HEADER_SIZE_LEN = 4;
function excludeDockerExecHeader(buffer) {
if (buffer.length <= EXEC_HEADER_LEN) {
return "";
}
let bodyLength = buffer.readUInt32BE(EXEC_HEADER_STREAM_LEN, EXEC_HEADER_SIZE_LEN);
@hirokiky
hirokiky / memoize_generator.py
Created September 14, 2016 05:34
Decotator to memoize results of generator. This will scan until end of the generator at the first of iterating.
from functools import wraps
class MemGen:
def __init__(self, generator):
self.generator = generator
self.mem = []
self.__doc__ = generator.__doc__
def __iter__(self):
@hirokiky
hirokiky / reservednames.py
Created October 11, 2016 03:18
Reserved names
reserved_names = {
'about',
'abuse',
'account',
'accounts',
'activity',
'admin',
'administrator',
'administrators',
'admins',
@hirokiky
hirokiky / choices_enum.py
Last active October 26, 2016 04:51
A Base Enum class which can be used for Django's choices field.
class ChoicesEnum(Enum):
def __str__(self):
return self.value
@classmethod
def choices(cls):
return tuple(
(item.value, item.name.replace('_', ' ').title()) for item in cls
)
@hirokiky
hirokiky / cmindent.js
Last active April 19, 2019 13:39
A CodeMirror command to delete 4-space indent or charactor before.
/**
* > if foo:
* > | <= The cursor is here and hit delCharOrIndent command, it willbe
* > | <= here
*
* This command will delete at most 4 spaces before, if text before charactor is all spaces.
* If not on the case, this command will delete charactor as usual.
*/
(function(mod) {
@hirokiky
hirokiky / customadmin.md
Created December 3, 2016 02:11
TokyoDjangoMeetup

models.py

class MyUser(AbstractUser):
    idstaff = models.CharField(max_length=255)

    class Meta:
        db_table = 'myuser'
        swappable = 'AUTH_USER_MODEL'
@hirokiky
hirokiky / jsonify.js
Created February 25, 2017 11:21
Jsonify tree objects
toJson() {
return JSON.stringify(this, (key, value) => {
if (key == 'parent') {
return value.id;
} else {
return value;
}
});
}
@hirokiky
hirokiky / backorbeginning.el
Last active March 17, 2017 02:58
back-or-beginning: C-a back to indentation, and then back to begining of lines.
;; Moving start line and start indend by C-a
(defun back-to-indentation-or-beginning () (interactive)
(if (= (point) (progn (back-to-indentation) (point)))
(beginning-of-line)))
(global-set-key "\C-a" 'back-to-indentation-or-beginning)
@hirokiky
hirokiky / agg.py
Created April 28, 2017 05:13
List of dict => dict of dict aggregator.
import statistics
class Field:
def __init__(self, field, agg_func=lambda l: l[0]):
self.field = field
self.agg_func = agg_func
def aggregate(self, l):
return self.agg_func(l)