Skip to content

Instantly share code, notes, and snippets.

View VehpuS's full-sized avatar
🦔

Moshe Jonathan Gordon Radian VehpuS

🦔
View GitHub Profile
@VehpuS
VehpuS / sql-select-by-value-in-array.sql
Last active April 25, 2024 07:56
SQL select by value in a array
SELECT id, column FROM table
where id::VARCHAR = ANY (ARRAY['b329a9b1-54fb-4d5e-853e-4c58555ab0de', 'fc666e0a-0dd1-4d02-990c-4ae52324da36'])
@VehpuS
VehpuS / lazy_dict.py
Last active April 21, 2024 11:08
An implementation for a dictionary with values that are evaluated only on their first access (or when accessing their values using .values).
import inspect
import types
class lazy_dict(dict):
'''
@summary:
An object that allows to store in a key the return results of functions when they are accessed for the first
time (AKA lazy evaluation).
You may use this storage mechanism by passing functions or lambdas without parameters.
def partition (count, total, steps=1, minimum=0, maximum=None, sum_total=True):
'''
@summary:
Given a number "total" and number of parts to split it to "count", return a count-length tuple made of numbers that sum up to "total".
Steps defines in what increments to increase each "bin", starting with 0 or minimum sized bins to bins of size total or maximum.
If sum total is True - only partitions summing to total will return, otherwise, partitions with lower sums will be returned as well.
@param count:
How many bins to split total into.
@param total:
The total size to split into bins.
@VehpuS
VehpuS / range_from_slice.py
Last active April 21, 2024 11:07
Given a python slice object and a given length, return a range representing the indices that should be returned for that slice for a collection of that length (useful for implementing __getitem__ for slice as index)
def range_from_slice_overcomplicated(slc: slice, length: int) -> range:
step = 1 if slc.step is None else slc.step
assert step != 0, "slice step cannot be zero"
default_start = 0 if step > 0 else length - 1
default_end = length if step > 0 else 0
return range(
default_start if slc.start is None else (length + slc.start) if slc.start < 0 else slc.start,
default_end if slc.stop is None else max(-1, (length + slc.stop)) if slc.stop < 0 else min(slc.stop, length),
step
)
@VehpuS
VehpuS / run_os_cmd.py
Last active April 21, 2024 11:07
Run bash command from python
import subprocess
import sys
def run_os_cmd(
cmd: str, # Command to run
interactive_write: bool=True, # Should the command print as it's running
):
print(f"Running '{cmd}'")
cmd_proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True )
@VehpuS
VehpuS / xml_node.py
Last active April 21, 2024 11:07
A helper class to make navigating xml files in python nicer (for someone unused to the python parser :P). My way of becoming more "fluent" with XML in python, and may be useful for other people / provide a good demo for safe recursion in Python, recursive types...
from __future__ import annotations
from typing import Any, Dict, List, NamedTuple, Union
import xml.etree.ElementTree as ET
class XmlNode(NamedTuple):
'''A helper class to make navigating xml files in python nicer and to create virtual XML nodes that can be saved'''
tag: str
attrib: Dict[str, Any]
@VehpuS
VehpuS / touchmove-mouseover-simulation.md
Last active February 20, 2024 02:12
Simulating Javascript mouseover events on touch screens using touchmove attributes and document.elementFromPoint

I made an HTML port of game of life made with a table of checkboxes based on Pluralsight's Play by Play: HTML, CSS, and JavaScript with Lea Verou (http://www.pluralsight.com/courses/play-by-play-lea-verou). I was able to create a cool mousedown interface that allowed the "player" to draw on the board by dragging the mouse while it was down, and wanted to do the same for touch devices.

Unfortunately, touch events only hold references to the DOM element where they began (i.e. where "touchstart" fired) and do not recognize the DOM elements that exist under them (a detailed explanation for this can be found here: http://stackoverflow.com/questions/4550427/prefered-alternative-to-onmouseover-for-touch).

Instead, touch events maintain several lists of Touch objects - which represent interactions with the touch screen. Among these objects' properties are clientX and clientY, which indicate the X and Y coordinates of the events relative to the viewport (go to http://www.javascriptkit.com/javatutors/touchevents.shtm

@VehpuS
VehpuS / template-context.tsx
Created December 14, 2020 09:08
A template for a React context with a safe hook accessor
import React from "react";
export interface TemplateContextProps {
templateValue: any;
}
export const TemplateContext = React.createContext<TemplateContextProps | undefined>(undefined);
export const TemplateContextProvider: React.FunctionComponent<{
initialTemplateValue: any
@VehpuS
VehpuS / testOpenApi.ts
Last active October 22, 2023 13:00
Test openapi operations schema before calling an endpoint
import { compact, every, includes, isEmpty, map, some } from 'lodash';
interface OpenAPIOperation {
path: string;
action: 'get' | 'post' | 'put' | 'delete';
parameters: string[];
}
function checkOpenApiForOps(
openApiJson: any,
@VehpuS
VehpuS / 0-startup-overview.md
Created September 16, 2020 15:28 — forked from dideler/0-startup-overview.md
Startup Engineering notes