Skip to content

Instantly share code, notes, and snippets.

@IvanaGyro
IvanaGyro / photo_datetime.py
Last active March 18, 2023 12:15
Draw datetime on the photos and the videos.
import os
import io
from pathlib import Path
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
from PIL import ExifTags
import exiftool
@IvanaGyro
IvanaGyro / schema.js
Created June 22, 2020 09:54
Helper for exploring MongoDB's schema.
function signature(e){switch(typeof e){case"string":return"s";case"number":return Number.isInteger(e)?"i":"f";case"undefined":return"u";default:return null===e?"n":e.constructor===Array?"a":`{${Object.keys(e).sort().map(r=>`${r}:${signature(e[r])}`).join(",")}}`}}
function deserilize(sign){return eval(`i='i';s='s';n='n';a='a';f='f';var o=${sign};o;`)}
function _diff([e,t]){return Object.keys(e).forEach(f=>{"object"==typeof e[f]&&"object"==typeof t[f]?([e[f],t[f]]=_diff([e[f],t[f]]),Object.keys(e[f]).length||Object.keys(t[f]).length||(delete e[f],delete t[f])):e[f]===t[f]&&(delete e[f],delete t[f])}),[e,t]}
function diff(f,i){return _diff([f,i].map(deserilize))}
// > use mydb
db.mycollection.mapReduce(function() { emit(signature(this), 1)}, function(k, vs) { return Array.sum(vs) }, { out: {replace: 'mycollection_', db: 'mydbschema'}, scope: { signature })
// > use mydbschema
@IvanaGyro
IvanaGyro / hanlpWrapper.js
Created February 12, 2020 03:19
Wrap java objects to get the properties with the dot operator.
const java = require('java');
const path = require('path');
const JAVA_DIR = path.resolve(__dirname, '../libs/java/');
java.options = [ '-Xms3072m' , '-Xmx3072m', '-Xmn1152m' ];
java.asyncOptions = {
asyncSuffix: 'Async',
syncSuffix: '', // For readability, remove the convetional suffix of sync
// functions.
ifReadOnlySuffix: '_alt',
@IvanaGyro
IvanaGyro / wrap_klepto.py
Last active August 8, 2019 08:51
A module wrapping the python module `klepto`. Function names are considered by the keygen and the decorators of caches can be applied to classes.
import functools
import importlib
import inspect
import sys
from copy import copy
from pathlib import Path
from types import ModuleType
import klepto
@IvanaGyro
IvanaGyro / fix_mongodump.py
Created July 20, 2019 08:28
Fix MongoDB's dumped JSONs which save indices information.
#!python3
import argparse
import json
import os
import shutil
from pathlib import Path
def convert(obj):
if type(obj) == list:
return [convert(item) for item in obj]
@IvanaGyro
IvanaGyro / ukkonen_suffix_tree.py
Created June 8, 2019 09:30
Implement of Ukkonen’s algorithm of building suffix trees with Python
from collections import defaultdict
class Node: # for the effectivity reason, do not inherit from ABCs
__slots__ = ('beg', 'end', 'link', 's', 'node')
def __init__(self, s):
self.s = s
self.beg = self.end = self.link = None
self.node = defaultdict(lambda: Node(s))
@IvanaGyro
IvanaGyro / nlargest.py
Last active May 21, 2019 16:29
The Python implementation of getting n largest elements in unsorted array.
'''
The implementation of getting n largest elements in the array.
The algorithms follows the page:
https://www.geeksforgeeks.org/k-largestor-smallest-elements-in-an-array/
'''
import operator
from math import inf
def nlargest_bubble(n, items):
@IvanaGyro
IvanaGyro / mystring.cpp
Created May 8, 2019 06:13
Demonstrate how to build a class with move semantics and copy-and-swap idioms.
#include <cstring>
#include <iostream>
#include <algorithm>
#include <utility>
using namespace std;
class MyString {
public:
MyString() {
@IvanaGyro
IvanaGyro / diverse_subarray.py
Created April 27, 2019 07:14
The TLE solution of the third problem of Kick Start 2019 Round B.
from collections import defaultdict, deque
def max_trinket(s, a):
'''
Attributes:
s: The maximum number of trinkets allowed of a single type
a: Array of the types of the trinkets
'''
if not s:
@IvanaGyro
IvanaGyro / energy_stone.py
Created April 25, 2019 12:30
The wrong answer to the second problem of 2019 Kick Start round B
from operator import itemgetter
def eat(stones):
max_e = {}
stones.sort(key=itemgetter(2), reverse=True)
def dp(time, i):
if i == len(stones):
return 0
if (time, i) not in max_e:
max_e[(time, i)] = max(