Skip to content

Instantly share code, notes, and snippets.

View singularitti's full-sized avatar
:octocat:
WFH

Qi Zhang singularitti

:octocat:
WFH
View GitHub Profile
@singularitti
singularitti / cn_en.py
Created March 15, 2024 20:19
If I want to match spaces between Chinese characters and English characters (in either order), as well as spaces between Chinese characters themselves, what regex should I use? #regex
import re
def remove_spaces(text):
pattern = re.compile(r'(?<=\p{Script=Han})\s+(?=\p{Script=Han}|\p{Script=Latin})|(?<=\p{Script=Latin})\s+(?=\p{Script=Han})')
return pattern.sub('', text)
@singularitti
singularitti / check.jl
Created February 4, 2024 01:35
Checking for digits in an integer in Julia #Julia #numbers #algorithm
# See https://youtu.be/ZYzlhp-W0a8
function digitsin(digits::Integer, number)
# Decimal representation of `digits` has N digits
base = 10
while (digits ÷ base > 0)
# `digits ÷ base` is same as `floor(Int, digits/base)`
base *= 10
end
# `base` is now the first integer power of 10 above `digits`, used to pick last N digits from `number`
while number > 0
@singularitti
singularitti / merge.sh
Created January 12, 2024 01:48
Merge Git repos #Git
set -eux
rm -rf rubygems bundler
git clone https://github.com/rubygems/rubygems
git clone https://github.com/rubygems/bundler
(cd bundler
time git filter-repo --to-subdirectory-filter bundler # --tag-rename :bundler-
)
(cd rubygems
git co f553da065 # switch to the commit before the merge
git remote add bundler ../bundler
@singularitti
singularitti / snow.jl
Created December 28, 2023 00:52
Print snowing scene in REPL #Julia
using REPL
# See https://discourse.julialang.org/t/let-it-snow/72950/15
function snow(io = stdout)
h, w = displaysize(io)
iob = IOBuffer()
ioc = IOContext(IOContext(iob, io), :displaysize=>(h,w))
print(io, repeat("\n", h), "\e[", h, "A\e[1G") # new lines and move back up
air = ones(Int, w, h)
flakes = [" ", "*", "❄︎", "❅", "❆"]
scsin(t) = ((sin(t) / 2) + 0.5) * (0.1 / 3)
@singularitti
singularitti / jldoc.md
Created October 15, 2023 01:17
Julia Documenter GPT prompt #GPT #prompt

You are a documentation generator for Julia code that automatically generates documentation for any provided code using the official Julia documentation style.

  • Do not explain code.
  • Do not add Fields or Constructors, Fields should be replaced by the Arguments section.
  • Do not print my given code, just print the docs.

Here are two examples (enclosed by 4-backticks) for you to learn how to document:

  1. For code
@singularitti
singularitti / rmpdf.sh
Created September 4, 2023 22:30
Delete PDF files under a directory and its subdirectories #Shell #file
find <path> -type f -name "*.pdf" -exec trash {} \;
@singularitti
singularitti / ExifTool.sh
Created August 21, 2023 22:39
Use ExifTool #Shell #images
exiftool -overwrite_original -All= *.jp*g *.JPG *.png
@singularitti
singularitti / tlmgr.sh
Created August 21, 2023 22:38
Mange tlmgr #Shell #LaTeX
# Remove all backups
tlmgr backup --all --clean=0
# Remove LuaLaTeX caches
luaotfload-tool --cache=erase
@singularitti
singularitti / dock.sh
Created August 21, 2023 22:37
Manage macOS Dock #macOS #Shell
# Reset Dock to default layout
defaults delete com.apple.dock; killall Dock
# Dim hidden apps
defaults write com.apple.Dock showhidden -boolean yes; killall Dock
# Scroll gestures
defaults write com.apple.dock scroll-to-open -bool true && killall Dock
# Show "Quit Finder" menu item
@singularitti
singularitti / images.sh
Created August 21, 2023 22:35
Resize images #Shell
# Resize the specified image, ignoring the previous aspect ratio
sips -z <height> <width> <image>
# Resize the largest side of the specified image, preserving the aspect ratio
sips -Z <size> <image>
# Crop the specified image to the given dimensions (relative to the center of the original image)
sips -c <height> <width> <image>