Skip to content

Instantly share code, notes, and snippets.

View cppio's full-sized avatar

Parth Shastri cppio

View GitHub Profile
@cppio
cppio / wrap.py
Last active December 23, 2018 23:05
Simple logging function wrapper in python for easier debugging.
import functools
def wrap(wrapped):
@functools.wraps(wrapped)
def wrapper(*args, **kwds):
print("%s(%s)" % (wrapped.__name__, ", ".join(["%r" % i for i in args] + ["%s=%r" % i for i in kwds.items()])))
try:
ret = wrapped(*args, **kwds)
except BaseException as err:
print("%s >> %r" % (wrapped.__name__, err))
@cppio
cppio / stat.sh
Last active December 23, 2018 23:06
The default stat format string for macOS
stat -f '%d %i %Sp %l %Su %Sg %r %z "%Sa" "%Sm" "%Sc" "%SB" %k %b %#Xf %N'
@cppio
cppio / bash.bat
Created October 18, 2017 01:49
Batch file for WSL to use Mintty
C:\cygwin64\bin\mintty /bin/wslbridge -C ~
@cppio
cppio / findstring.js
Last active November 19, 2017 18:18
A function to find a string in an object.
function findString(object, string, seen, path) {
seen = seen || [object];
path = path || "";
for (var i in object) {
try { object[i]; } catch (e) { continue; }
if (!seen.includes(object[i])) {
seen.push(object[i]);
if (object[i] instanceof Object && !(object[i] instanceof HTMLElement))
findString(object[i], string, seen, path + '["' + i + '"]');
else if (typeof object[i] === "string" && object[i].includes(string))
@cppio
cppio / CollectorIdentityObject.java
Last active February 9, 2019 20:48
Simple wrapper around Java Collectors to use methods
import java.util.EnumSet;
import java.util.Set;
import java.util.stream.Collector;
public interface CollectorIdentityObject<T, A, R> extends CollectorObject<T, A, R> {
@Override
@SuppressWarnings("unchecked")
default R finish(A a) {
return (R) a;
}
@cppio
cppio / Test.cpp
Created February 15, 2019 02:05
Simple C++ class that logs all constructions, destructions, and assignments.
#include <iostream>
class Test {
public:
Test() { std::cout << __PRETTY_FUNCTION__ << std::endl; }
Test(Test const&) { std::cout << __PRETTY_FUNCTION__ << std::endl; }
Test(Test&&) { std::cout << __PRETTY_FUNCTION__ << std::endl; }
~Test() { std::cout << __PRETTY_FUNCTION__ << std::endl; }
@cppio
cppio / type_name.cpp
Created February 15, 2019 02:17
Templated C++ function to get the demangled name of a type with const, volatile, and references.
#include <cxxabi.h>
#include <typeinfo>
#include <type_traits>
#include <string>
#include <memory>
#include <cstdlib>
template<typename T>
std::string type_name() {
@cppio
cppio / forward.cpp
Created February 15, 2019 03:14
An example of C++ perfect forwarding
#include <utility>
template<typename F, typename... Args>
decltype(auto) call(F&& f, Args&&... args) { return f(std::forward<Args>(args)...); }
@cppio
cppio / vectormagic.js
Last active May 13, 2025 18:01
Extract previews from vectormagic.com as SVG files
VecResult.prototype.toSVG = function() {
let paths = [];
this.pieces.forEach(piece => {
let index = 0;
piece.shapes.forEach(shape => {
let commands = [];
shape.lenghts.forEach(length => {
@cppio
cppio / ws.js
Created April 19, 2019 18:19
Log WebSocket calls
((prototype, CONNECTING, OPEN, CLOSING, CLOSED) => {
WebSocket = function() {
const ws = new prototype.constructor(...arguments);
console.trace("new WebSocket", arguments, "=>", ws);
ws.addEventListener("message", event => console.log(event.data));
return ws;
};
WebSocket.prototype = prototype;
WebSocket.CONNECTING = CONNECTING;
WebSocket.OPEN = OPEN;