Skip to content

Instantly share code, notes, and snippets.

@yuliji
yuliji / process_pool.py
Created July 4, 2022 04:02
[python process pool]
import multiprocessing
import time
def cube(x):
return x**3
if __name__ == "__main__":
pool = multiprocessing.Pool(3)
start_time = time.perf_counter()
processes = [pool.apply_async(cube, args=(x,)) for x in range(1,1000)]
@yuliji
yuliji / child_process_execSync.js
Last active May 3, 2022 00:02
[child_process_execSync.js] child_process.js print stdout
import { execSync } from 'child_process';
export async function runTest(args: TestArgs) {
execSync(`echo "run e2e for ${args.page} in ${args.browser}"`, {encoding: 'utf-8', stdio: ['pipline', 'inherit', 'inherit']});
execSync(`echo "run e2e for ${args.page} in ${args.browser}"`, {encoding: 'utf-8', stdio: 'inherit'}); // use parents config of stdio
}
@yuliji
yuliji / shell.js
Created September 29, 2021 04:07
spawn bash in node.js
const child_process = require('child_process');
const ls = child_process.spawn('bash', [], { stdio: 'inherit' });
ls.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
@yuliji
yuliji / str2hex.py
Last active June 29, 2021 00:17
[string to hex] Convert string to hex format 0xab123 #python
>>> b = '区块链是金融系统的未来'.encode('utf-8')
>>> b
b'\xe5\x8c\xba\xe5\x9d\x97\xe9\x93\xbe\xe6\x98\xaf\xe9\x87\x91\xe8\x9e\x8d\xe7\xb3\xbb\xe7\xbb\x9f\xe7\x9a\x84\xe6\x9c\xaa\xe6\x9d\xa5'
>>> int.from_bytes(b, byteorder='big', signed=False)
26580042452449738111737693352426092008374866632327585440351958788959384868658597
>>> hex(int.from_bytes(b, byteorder='big', signed=False))
'0xe58cbae59d97e993bee698afe98791e89e8de7b3bbe7bb9fe79a84e69caae69da5'
@yuliji
yuliji / kill_current.sh
Last active February 17, 2021 23:24
[kill current script when quit]
trap 'kill -- $$' INT TERM QUIT;
# -- is a conventional marker recognised by most argument parsers which means "consider everything beyond this to be positional arguments (not flags)".
@yuliji
yuliji / nginx_no_cache.conf
Created February 12, 2021 00:10
[nginx_no_cache.conf] disable cache for nginx
add_header Last-Modified $date_gmt;
add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
if_modified_since off;
expires off;
etag off;
@yuliji
yuliji / greyscale.py
Created December 30, 2020 05:27
convert image to grey scale
from PIL import Image
img = Image.open('image.png').convert('LA')
img.save('greyscale.png')
@yuliji
yuliji / yargs_command.js
Created September 29, 2020 01:39
[yargs multi subcommand]
//https://github.com/yargs/yargs/issues/225
var yargs = require('yargs')
var argv = yargs
.usage('usage: $0 <command>')
.command('create', 'create a new [project|module]', function (yargs) {
argv = yargs
.usage('usage: $0 create <item> [options]')
.command('project', 'create a new project', function (yargs) {
console.log('creating project :)')
@yuliji
yuliji / plot_math.py
Last active October 2, 2020 11:12
[python plot math equation]
# Import our modules that we are using
import matplotlib.pyplot as plt
import numpy as np
# Create the vectors X and Y
x = np.array(range(10))
y = x ** 2
# Create the plot
plt.plot(x,y,label='y = x**2')
@yuliji
yuliji / time_iso.py
Last active October 4, 2020 05:23
[python time iso]python time iso format
from datetime import datetime, timezone
datetime.now(tz=timezone.utc).isoformat()
#'2020-09-18T11:19:55.651168+00:00'
def get_timestamp():
now = datetime.datetime.utcnow()
t = now.isoformat("T", "milliseconds")
return t + "Z"