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 / docker-run.sh
Last active January 5, 2024 02:30
Docker container running tool: use the host's user and configuration within the container and cache the software packages to the host.
#!/usr/bin/env bash
set -e
set -u
set -o pipefail
# usage: bash docker-run.sh [OPTIONS] IMAGE [COMMAND] [ARG...]
function get_valid_port() {
local random_port
@duruyao
duruyao / custom_routing_table.md
Last active September 19, 2023 01:42
Custom 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 / protobuf_rw_file.cpp
Last active March 15, 2023 08:27
Save files to and load files from Protocol Buffers Messages (implemented in 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 / py_project_coding_style_example.py
Last active November 2, 2022 09:59
Python3 project 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 October 18, 2022 08:51
Check path permissions before starting the docker image.
#!/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 March 15, 2023 08:27
Use Cython to compile the Python3 sources to C/C++ shared libraries and executables.
#!/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
@duruyao
duruyao / graphics_cli.py
Last active October 8, 2022 08:29
Print rectangles, squares and tables to the command line interface.
#!/usr/bin/env python3.9
r_w3_h2 = """\
┌─┐
└─┘"""
r_w5_h3 = """\
┌───┐
│ │
└───┘"""
r_w7_h4 = """\
@duruyao
duruyao / qwer.py
Last active July 11, 2022 07:51
Command line game for typing practice.
#!/usr/bin/env python3
# date: 2022-07-08
# author: duruyao@gmail.com
# desc: command line game for typing practice
import os
import sys
import time
import random
import string