Skip to content

Instantly share code, notes, and snippets.

View grimen's full-sized avatar

Jonas Grimfelt grimen

View GitHub Profile
@grimen
grimen / sqlalchemy-truncate_db.py
Created November 19, 2022 05:51 — forked from absent1706/sqlalchemy-truncate_db.py
Sqlalchemy: Truncate all tables
def truncate_db(engine):
# delete all table data (but keep tables)
# we do cleanup before test 'cause if previous test errored,
# DB can contain dust
meta = MetaData(bind=engine, reflect=True)
con = engine.connect()
trans = con.begin()
con.execute('SET FOREIGN_KEY_CHECKS = 0;')
for table in meta.sorted_tables:
con.execute(table.delete())
# @source: https://medium.com/@DorIndivo/how-we-migrated-from-python-multithreading-to-asyncio-128b0c8e4ec5
async def run_func_async(func:Callable, func_args:List[Any], executor:ThreadPoolExecutor):
if asyncio.iscoroutinefunction(func):
return await func(*func_args)
else:
return await asyncio.get_event_loop().run_in_executor(
func=lambda:func(*func_args), executor=executor)
@grimen
grimen / index.js
Created May 5, 2021 04:13 — forked from stalniy/index.js
CASL + Objection
const { defineAbility } = require('@casl/ability');
const { rulesToQuery } = require('@casl/ability/extra');
const Knex = require('knex');
const { Model } = require('objection');
const { interpret } = require('@ucast/objection')
const { CompoundCondition } = require('@ucast/core')
const knex = Knex({
client: 'sqlite3',
connection: ':memory:'
/* =========================================
IMPORTS
-------------------------------------- */
const fs = require('fs')
const path = require('path')
const _yaml = require('js-yaml')
sudo rmmod nvidia_uvm
sudo rmmod nvidia
sudo modprobe nvidia
sudo modprobe nvidia_uvm
const createFor = (exported = {}) => {
return (defaults = {}) => {
const functions = {}
exported['defaults'] = {...defaults}
for (const [key, value] of Object.entries(exported)) {
if (typeof value === 'function') {
functions[key] = exported[key]
class CombinedStream extends stream.PassThrough {
constructor (...streams) {
super()
this._streams = streams
this._transformStream = undefined
this.on('pipe', this.onPipe)
}
@grimen
grimen / streams-test.js
Last active January 23, 2019 09:41
Experiments with Node.js Stream API.
/* =============================================
Dependencies
------------------------------------------ */
const debug = require('debug')
const chalk = require('chalk')
const { Readable, Writable, Transform, pipeline } = require('stream')
@grimen
grimen / generators-test.js
Last active January 23, 2019 09:41
Experiments with Node.js Generator API - partially requires Node 10.
/* =========================================
IMPORTS
-------------------------------------- */
const fs = require('fs')
const { promisify } = require('util')
sleep = promisify(setTimeout)
import coloredlogs, logging
# TODO: find library similar to `debug` in Node/NPM
def debug(name):
logger = logging.getLogger(name)
coloredlogs.install(level='DEBUG', logger=logger)