Skip to content

Instantly share code, notes, and snippets.

View unpluggedcoder's full-sized avatar

UnpluggedCoder unpluggedcoder

View GitHub Profile
@unpluggedcoder
unpluggedcoder / pallette_bgr.py
Created October 12, 2023 06:43
Common color pallette format
# Pallette in BGR format
# Preview & reference: https://www.rapidtables.com/web/color/RGB_Color.html
ALICE_BLUE = (255, 248, 240)
ANTIQUE_WHITE = (215, 235, 250)
AQUA = (255, 255, 0)
AQUA_MARINE = (212, 255, 127)
AZURE = (255, 255, 240)
BEIGE = (220, 245, 245)
BISQUE = (196, 228, 255)
@unpluggedcoder
unpluggedcoder / for_jq.sh
Created January 22, 2021 09:53
for loop with jq in Bash
# for object in array
# Thanks to https://www.starkandwayne.com/blog/bash-for-loop-over-json-array-using-jq/
example='[{"name":"foo"},{"name":"bar"}]'
for row in $(echo "${example}" | jq -rc '.[] | @base64'); do
_jq() {
echo ${row} | base64 --decode | jq -r ${1}
}
# usage: $(_jq '.xxx')
echo $(_jq '.name')
done
@unpluggedcoder
unpluggedcoder / Cargo.toml
Last active August 5, 2020 12:34
Json-Rpc in Rust
[package]
name = "jrpc"
version = "0.1.0"
authors = ["unpluggedcoder <unpluggedcoder@outlook.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
log = "0.4"
@unpluggedcoder
unpluggedcoder / spin_lock.rs
Created July 23, 2020 03:37
Simple SpinLock written by Rust
// https://github.com/koute/memory-profiler/blob/master/preload/src/spin_lock.rs
use std::sync::atomic::{AtomicBool, Ordering};
use std::ops::{Deref, DerefMut};
use std::cell::UnsafeCell;
use std::mem::transmute;
pub struct SpinLock< T > {
pub value: UnsafeCell< T >,
pub flag: AtomicBool
}
@unpluggedcoder
unpluggedcoder / prctl.rs
Last active June 18, 2020 12:38
Get current thread name by prctl syscall in Rust
// Given by Alice: https://users.rust-lang.org/t/cstring-into-string-s-behavior-on-android/44470/3
use std::ffi::CStr;
use std::os::raw::c_char;
const PR_GET_NAME: libc::c_int = 16;
unsafe fn call_prctl(allocation: &mut Vec<u8>) -> &CStr {
let ptr = allocation.as_mut_ptr() as *mut c_char;
let ret: libc::c_int = libc::prctl(PR_GET_NAME, ptr);
if ret != 0 {
@unpluggedcoder
unpluggedcoder / dev_setup.sh
Last active March 29, 2020 16:57
Ubuntu Vim development environment setup
#!/usr/bin/env bash
# Ubuntu 18.04
# Rust + Postgresql 10 + redis development environment
# Zsh
sudo apt install zsh
# oh-my-zsh
# using curl
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
# or using wget
@unpluggedcoder
unpluggedcoder / setup_rust.sh
Last active March 29, 2020 16:59
Rust development setup
#!/usr/bin/env bash
##################
# For Chinese ONLY
tee -a ~/.zshrc <<'EOF'
export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup
export RUSTUP_DIST_SERVER=https://mirrors.tuna.tsinghua.edu.cn/rustup
EOF
##################
@unpluggedcoder
unpluggedcoder / r0010_regex_matching.rs
Created February 6, 2020 15:20
Leetcode snippets
#![feature(slice_patterns)]
impl Solution {
pub fn is_match(s: String, p: String) -> bool {
is_match(s.as_bytes(), p.as_bytes())
}
}
fn is_match(s: &[u8], p: &[u8]) -> bool {
match (p, s) {
@unpluggedcoder
unpluggedcoder / cool_argparse_stuff.py
Created November 2, 2018 13:17 — forked from brantfaircloth/cool_argparse_stuff.py
Some cool argparse stuff
class FullPaths(argparse.Action):
"""Expand user- and relative-paths"""
def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, os.path.abspath(os.path.expanduser(values)))
def is_dir(dirname):
"""Checks if a path is an actual directory"""
if not os.path.isdir(dirname):
msg = "{0} is not a directory".format(dirname)
raise argparse.ArgumentTypeError(msg)
@unpluggedcoder
unpluggedcoder / asynclog_using_celery.py
Last active May 2, 2018 17:47
[Python Asynclog Examples] Examples of using asynclog module #python #asynchronous #logging #threading #celery
from celery import shared_task
@shared_task
def write_task(msg):
# Write log in Network IO
print(msg)
celery_handler = AsyncLogDispatcher(write_task, use_thread=False, use_celery=True)
celery_handler.setLevel(logging.INFO)
logger.addHandler(celery_handler)