Skip to content

Instantly share code, notes, and snippets.

@SCP002
SCP002 / filter.go
Last active July 3, 2022 19:35
Golang 1.18 (optionally lower): concurrent Filter funtion example. Keeps empty elements and original order. Using Worker Pool.
// Golang >= 1.18 (uses generics).
// Golang < 1.18 without generics (replace T with explicit type in PoolFilter function signature).
package main
import (
"sort"
"github.com/alitto/pond"
)
@SCP002
SCP002 / spawn.js
Last active April 21, 2021 06:50
Node.js: Spawn process. Keep StdOut and StdErr in the original order. Display output real time. Capture output on exit.
// Based on answer from https://stackoverflow.com/questions/57046930/node-js-spawn-keep-stdout-and-stderr-in-the-original-order
const { spawn } = require('child_process');
const options = {
shell: true, // Keep shell: true to redirect StdErr to StdOut.
stdio: [
'inherit', // StdIn. Fixes "ERROR: Input redirection is not supported, exiting the process immediately" on Windows.
'pipe', // StdOut.
'pipe', // StdErr.
@SCP002
SCP002 / json.go
Last active March 15, 2022 22:04
Golang: Marshal & Unmarshal partially known / dynamic JSON. Using structs (type safe).
// To stop json.Marshal from using HTMLEscape and adding trailing newline, see:
// https://github.com/SCP002/jsonexraw
package main
import (
"fmt"
json "github.com/yaegashi/jsonex.go/v1"
)
@SCP002
SCP002 / json.py
Last active April 26, 2021 13:19
Python: Dump & Load partially known / dynamic JSON. Using dataclasses (type safe).
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Requirements:
# Python 3.7+
# pip install dataclasses-json
import dataclasses as dc
import os
@SCP002
SCP002 / fix-powershell-context-menu.reg
Last active October 4, 2022 22:33
Windows Registry: Run PowerShell scripts with double click. Various tweaks of context menu for .ps1 and .psm1 files. See coments for description.
Windows Registry Editor Version 5.00
; WARNING:
; Uses Visual Studio Code as a standard editor.
; You might change it to your own with default file path, like:
;
; [HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\Edit\Command]
; @="\"C:\\Windows\\System32\\notepad.exe\" \"%1\""
;
@SCP002
SCP002 / json.ts
Last active April 7, 2021 20:25
TypeScript: Parse & Stringify partially known / dynamic JSON. Using classes (type safe).
// npm install --save-dev class-transformer
import 'reflect-metadata';
import { classToPlain, deserialize, Exclude, Type } from 'class-transformer';
const inpJsonStr: string = `
{
"topKnownKey": 1,
"topUnknownKey": 2,
@SCP002
SCP002 / ProcessExample.java
Last active April 14, 2021 20:15
Java 8: Start process. Keep StdOut and StdErr in the original order. Display output real time character by character. Capture output on exit.
package org.example;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
public class ProcessExample {
public static void main(String[] args) {
ProcessBuilder pb = new ProcessBuilder();
@SCP002
SCP002 / JSONExample.java
Last active April 11, 2021 03:47
Java 13 (optionally 8): Decode & Encode partially known / dynamic JSON into classes. Require Jackson.
/*
* Java 13+ (Uses multi-line string).
* Java 8+ without multi-line string (line 87).
* Require Jackson dependency (Maven example; Add to <dependencies> in your pom.xml):
* <dependency>
* <groupId>com.fasterxml.jackson.core</groupId>
* <artifactId>jackson-databind</artifactId>
* <version>2.12.2</version>
* </dependency>
*/
@SCP002
SCP002 / FilterExample.java
Last active April 22, 2021 09:14
Java 8: concurrent Filter funtion example. Using Worker Pool.
package org.example;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.*;
abstract class ConcurrentFilter {
@SCP002
SCP002 / popen.py
Last active June 25, 2022 11:00
Python: Start process. Keep StdOut and StdErr in the original order. Display output real time character by character. Capture output on exit.
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Requirements:
# Python 3.7+ (uses data classes, lines 16 and 25).
import dataclasses as dc
import os
import subprocess
import sys