Skip to content

Instantly share code, notes, and snippets.

View patarapolw's full-sized avatar

Pacharapol Withayasakpunt patarapolw

View GitHub Profile
@patarapolw
patarapolw / test.js
Created May 24, 2018 12:30
Getting asynchronous POST to the top level Return (jQuery contextMenu Asynchronous)
$('#vocab').contextMenu({
selector: ".entry",
build: function($trigger, e) {
e.preventDefault();
function buildMenu(buildMenuCallback){
$.post('/vocabInLearning', { vocab: $('a', this).text() }, function(data) {
var isLearnt = (data === '1');
buildMenuCallback({
@patarapolw
patarapolw / image_server.py
Created October 16, 2018 16:49
Simple image server in Python, for use in Jupyter Notebook, markdown, local website. Can also be adapted for viewing PDF with pdf.js
import subprocess
from pathlib import Path
from datetime import datetime
from PIL import ImageGrab
import atexit
from urllib.parse import quote
class Server:
def __init__(self, root, port=14074):
@patarapolw
patarapolw / searchbox.py
Last active November 13, 2018 17:43
Converting searchbox query string into computer-understandable list
import shlex
import json
import re
ADDITIONAL_CHAR = ''.join(re.findall(r'\w', re.escape(''.join(chr(u) for u in range(0x10FFFF)))))
def parse_query(q: str, operators=(':', '=', '>', '<')):
"""
:param str q:
import ast
import re
class SearchBox:
ast_table = {
ast.Eq: ':',
ast.Is: '=',
ast.Or: 'or',
ast.And: 'and',
import lark
parser = lark.Lark(r'''
?atom : atom or atom
| atom and atom
| "(" atom ")"
| compare
| value
compare : value ":" value
@patarapolw
patarapolw / myJSsort.ts
Created April 3, 2019 10:09
Javascript sort
a.sort((a, b) => {
function convert(x: any) {
return x[sortBy];
}
function compare() {
const m = convert(a) || -Infinity;
const n = convert(b) || -Infinity;
if (typeof m === "string" && typeof n === "string") {
return m.localeCompare(n);
@patarapolw
patarapolw / mongoFilter.ts
Created April 8, 2019 14:47
convert mongo query to filter function
export function mongoFilter(x: any, cond: any): boolean {
return Object.keys(cond).every((k) => {
if (k === "$or") {
return (cond[k] as any[]).some((c0) => mongoFilter(x, c0));
} else if (k === "$and") {
return (cond[k] as any[]).every((c0) => mongoFilter(x, c0));
} else {
let dotIndex = k.indexOf(".");
while (dotIndex !== -1) {
x = x[k.slice(0, dotIndex)];
************* Module src.test.test_search
src/test/test_search.py:1:0: C0111: Missing module docstring (missing-docstring)
src/test/test_search.py:4:0: C0111: Missing function docstring (missing-docstring)
src/test/test_search.py:8:0: C0111: Missing function docstring (missing-docstring)
************* Module src.python.server
src/python/server.py:1:0: C0111: Missing module docstring (missing-docstring)
src/python/server.py:17:0: C0103: Constant name "app" doesn't conform to UPPER_CASE naming style (invalid-name)
src/python/server.py:18:0: C0103: Constant name "socketio" doesn't conform to UPPER_CASE naming style (invalid-name)
src/python/server.py:28:0: C0111: Missing function docstring (missing-docstring)
src/python/server.py:33:0: C0111: Missing function docstring (missing-docstring)
@patarapolw
patarapolw / type-check.js
Last active August 23, 2019 14:58
JavaScript instance type checking.
function isSameType(a, b) {
const tA = typeof a;
const tB = typeof b;
if (tA !== "object") {
return tA === typeof b;
} else if (tB === "object") {
return a.constructor === b.constructor;
}
return tA === tB; // always false???
}