Skip to content

Instantly share code, notes, and snippets.

View hirokiky's full-sized avatar

Hiroki Kiyohara hirokiky

View GitHub Profile
@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 / 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 / reservednames.py
Created October 11, 2016 03:18
Reserved names
reserved_names = {
'about',
'abuse',
'account',
'accounts',
'activity',
'admin',
'administrator',
'administrators',
'admins',
@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 / 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 / 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 / write_file_to_docker.js
Created May 19, 2016 01:36
Writing files by dockerode
container.exec({Cmd: ['/bin/sh', '-c', 'cat > /path/to/test.txt], AttachStdin: true}, (err, exec) => {
exec.start({hijack: true, stdin: true}, function(err, stream) {
stream.write("Hi there.\n")
});
});
@hirokiky
hirokiky / tsl_client.py
Created April 28, 2016 08:25
TSL supported WebSocket for docker-py
import docker
class WSFixedClient(docker.Client):
def _create_websocket_connection(self, url):
sslopt = {}
if self.cert:
sslopt["certfile"] = self.cert[0]
sslopt["keyfile"] = self.cert[1]
return websocket.create_connection(
@hirokiky
hirokiky / forcecachesession.py
Last active February 1, 2016 08:05
requests.Session to memoize response forcedly, depeding on cachetools library.
import cachetools
import requests
class ForceCacheSession(requests.Session):
""" Force caching requests.Session
* Apply `cachetools.Cache` by `mount_cache()` methoud
* It will cache if the method was GET and status code was 200
* It will apply `res.from_cache` bool value
@hirokiky
hirokiky / mandnull.js
Created January 20, 2016 15:00
moment.js and null
> var moment = require('moment')
undefined
> null < moment('1970-01-01')
false
> null < moment('1970-01-02')
true
>