Skip to content

Instantly share code, notes, and snippets.

@GaryLee
GaryLee / make_reg_svg.py
Created August 31, 2024 10:07
A example to generate register definition with drawsvg module.
#!python
# -*- coding: utf-8 -*-
import drawsvg as draw
# Reference: https://github.com/cduck/drawsvg/blob/master/docs/index.md
reg = {
'31': 'EN',
@GaryLee
GaryLee / coding_style_format.sh
Created August 29, 2024 01:41
My setting of coding style of verilog. Use verible-verilog-format to re-format the code.
#!/bin/sh
verible-verilog-format \
--inplace \
--column_limit=200 \
--indentation_spaces=4 \
--line_break_penalty=4 \
--assignment_statement_alignment=align \
--case_items_alignment=align \
--class_member_variable_alignment=align \
--formal_parameters_alignment=align \
@GaryLee
GaryLee / tasks.py
Created July 30, 2024 00:02
An example showing how to use invoke as standalone program.
#!python
# coding: utf-8
from invoke import task, Program
@task
def task1(c):
c.run("Put your first task command here.")
@task
@GaryLee
GaryLee / toml2tmpl.py
Last active July 31, 2024 06:42
Generate document with TOML and MAKO template.
#!python3
# coding: utf-8
import sys
from os import path
import tomli as toml
from mako.template import Template
def main():
if len(sys.argv) != 3:
@GaryLee
GaryLee / pip_proxy_install.sh
Last active June 26, 2024 03:26
How to use proxy to do PIP/YUM/DNF installation.
# Refer to https://stackoverflow.com/questions/48343669/how-to-install-python-packages-over-ssh-port-forwarding.
# Login to server via following ssh command. Your computer must have permission to connect to PIP server directly.
ssh -R 9999 username@server.host.name
# After login to your server.host.name. Use following command to install proxy package.
pip install package_name --proxy socks5h:127.0.0.1:9999
# The apt and yum can work with this tunnel by setting proxy.
# Add following line in /etc/yum.conf
@GaryLee
GaryLee / least_sqrt_root.py
Created June 24, 2024 00:35
Simple linear regression example using least square root for solution.
import numpy as np
def linear_regression(x, y):
"""The input are x and y for 2D"""
assert isinstance(x, np.ndarray)
assert isinstance(y, np.ndarray)
w = np.sum((y - np.average(y)) * x) / np.sum((x - np.average(x))**2)
b = np.average(y) - w * np.average(x)
return w, b
@GaryLee
GaryLee / parse_verilog_number.py
Last active June 17, 2024 02:41
Parse verilog number literal.
def parse_verilog_number(s):
"""Parse a verilog number literal to the tuple (sign, bits, base, value).
sign: 1 for postive, -1 for negative.
bits: The bits token in the number literal.
base: The base of number(2, 10 or 16).
value: The value token.
"""
base_token = {'b': 2, 'd': 10, 'h': 16}
pattern = re.compile(r"""(?P<sign>[\-\+])?(?P<define>\`)?(?P<bits>[\w]+)?\'((?P<base2>[bB])(?P<value2>[01]+)|(?P<base10>[dD])(?P<value10>\d+)|(?P<base16>[hH])(?P<value16>[0-9a-fA-F]+)|(?P<value>\d+))""")
m = pattern.match(s)
@GaryLee
GaryLee / sync_desktop.sh
Created June 2, 2024 01:44
A shell script what utilize rsync to backup import folders of desktop.
#!/bin/sh
# -delete flag will delete unmatched files and folder on target.
#DELETE_FLAG=-delete
DELETE_FLAG=
# The rsync flags
# -a, --archive
# This is equivalent to -rlptgoD. It is a quick way of saying you want recursion and want to preserve almost
# everything (with -H being a notable omission).
@GaryLee
GaryLee / flatten.py
Last active June 21, 2024 02:29
Flatten nested Python sequence object.
from collections.abc import Sequence
def flatten(x):
"""Flatten given parameter recursively."""
if isinstance(x, Sequence):
for v in x:
yield from flatten(v)
else:
yield x
@GaryLee
GaryLee / get_graph_model.py
Created May 22, 2024 04:22
An example to get the root of mxGraphModel from draw io file.
#!python
# coding: utf-8
import sys
import zlib
import base64
import xml.etree.ElementTree as ET
from urllib.parse import unquote
def decode_drawio(filename):