Skip to content

Instantly share code, notes, and snippets.

View duruyao's full-sized avatar
🏢
Coding in office

Ryan Du duruyao

🏢
Coding in office
View GitHub Profile
@duruyao
duruyao / gdscript_naming_conventions.md
Created September 2, 2024 08:57
Naming conventions for Godot projects programmed with GDScript.
@duruyao
duruyao / pre-commit.sh
Last active September 2, 2024 04:41
A Git hook example.
#!/usr/bin/env bash
set -euo pipefail
function error_ln() {
printf "\033[1;32;31m%s\n\033[m" "${1}"
}
while IFS='' read -r line; do files+=("${line}"); done < <(git diff-index --cached --name-only HEAD --diff-filter=ACM)
for file in "${files[@]}"; do
@duruyao
duruyao / docker-run.sh
Last active September 7, 2024 04:09
A third-party Docker container running tool: Use the host's users and configurations in the container and cache the software package to the host.
#!/usr/bin/env bash
set -euo pipefail
# usage: bash docker-run.sh [OPTIONS] IMAGE [COMMAND] [ARG...]
function get_valid_port() {
local random_port
random_port="$((RANDOM % (49151 - 1024 + 1) + 1024))"
if [ -n "$(command -v netstat)" ]; then
@duruyao
duruyao / customizing_routing_table.md
Last active September 2, 2024 03:43
Customizing routing table.

I have an Ubuntu system that uses two wired networks simultaneously (PCI Ethernet, USB Ethernet). I would like it to use the USB wired network for internet access and the PCI wired network for accessing the internal network (e.g., 10.0.13.120, 10.0.13.134, 10.0.12.1, 10.0.10.16, 10.0.10.17, etc).

1.

$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.100.1   0.0.0.0         UG    101    0        0 usb0
0.0.0.0         192.168.31.1    0.0.0.0         UG    102    0        0 enp4s0
@duruyao
duruyao / rw_pb_example.cpp
Last active September 2, 2024 03:58
Examples of reading and writing proto files using C++.
/*
* @author duruyao
* @date 2023-03-14
* @desc save files to and load files from protobuf messages
* @usage EXECUTABLE <SRC_FILENAME> <DST_FILENAME>
*/
#include <cstdio>
#include <string>
@duruyao
duruyao / get_ball.py
Created January 5, 2023 04:08
The game of getting balls.
import sys
def have_wining_strategy(*bags: int) -> bool:
if 0 == sum(bags):
return False
for i in range(len(bags)):
for n in range(bags[i]):
if not have_wining_strategy(*bags[:i], n, *bags[i + 1:]):
return True
@duruyao
duruyao / py3_static_coding_style_example.py
Last active September 2, 2024 04:42
A Python3 static coding style example.
#!/usr/bin/env python3
from typing import Union, Optional, Dict
def multiply(arg1: Union[int, float, str, None] = 0.0,
arg2: Union[int, float, str, None] = 0.0) -> Optional[Dict[str, Union[int, float, str]]]:
"""
Calculate the product of two numbers.
:param arg1: a factor (default: 0.0)
@duruyao
duruyao / docker-command-checker.sh
Last active September 2, 2024 04:16
A third-party Docker container directory permissions pre-checker.
#!/usr/bin/env bash
## date: 2022-10-13
## author: duruyao@gmail.com
## file: docker-checker.sh
## desc: check path permissions before starting the docker image
set -euo pipefail
function error_ln() {
@duruyao
duruyao / setup.py
Last active September 2, 2024 04:18
Compiling the Python3 sources to C/C++ shared libraries and executables via Cython.
#!/usr/bin/env python3.9
import os
import re
import sys
import glob
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext