Skip to content

Instantly share code, notes, and snippets.

View KaoruNishikawa's full-sized avatar

Kaoru Nishikawa KaoruNishikawa

View GitHub Profile
@KaoruNishikawa
KaoruNishikawa / diff.py
Last active June 22, 2023 13:14
Print Git-like diff of arbitrary 2 strings in Python
import difflib
def diff(a: str, b: str) -> None:
line_color = {"+": 32, "-": 31}
diffs = difflib.ndiff(a.splitlines(keepends=True), b.splitlines(keepends=True))
diff_list = list(diffs)
styled: list[str] = []
for prev, next in zip(diff_list, diff_list[1:] + [""]):
@KaoruNishikawa
KaoruNishikawa / install.sh
Last active August 25, 2022 10:43
NECST installation script for non-docker environment (never tested)
function get_ros2_workspace() {
# Look up environment variables.
# If the value isn't actually a directory, this will create it.
candidate=($ROS2_WS $ROS_WS)
for __value in $candidate; do
if [ ! -z $__value ]; then
echo $__value
[ ! -d $__value ] && mkdir -p $__value
return 0
fi
@KaoruNishikawa
KaoruNishikawa / collect
Last active August 11, 2022 03:54
Collect directories in one place
#!/usr/bin/env python3
import shutil
import subprocess
import sys
import time
from pathlib import Path
save_dir = Path.home() / "Downloads" / "to_upload"
@KaoruNishikawa
KaoruNishikawa / imshow_skewed.py
Created July 29, 2022 10:39
Draw skewed image on skewed axis.
__all__ = ["imshow_skewed"]
from typing import Tuple
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.transforms import Affine2D
from mpl_toolkits.axisartist import floating_axes
from mpl_toolkits.axisartist.grid_finder import DictFormatter, FixedLocator
@KaoruNishikawa
KaoruNishikawa / neclib_pyproject.toml
Created March 28, 2022 05:33
Poetry: SolveProblemError when a package with multiple constraints in project dependency and its sub-dependency
[tool.poetry]
name = "neclib"
version = "0.3.1"
description = "My package"
authors = ["me"]
[tool.poetry.dependencies]
python = ">=3.6.1, <3.10"
astropy = [
{ version = "^3.0", python = "<3.8" },
import functools
import types
import xarray as xr
@xr.register_dataarray_accessor("accessor_name")
class Something(xr.DataArray):
__slots__ = (
"_cache",
@KaoruNishikawa
KaoruNishikawa / padded_array.py
Last active June 24, 2021 15:21
Create np.ndarray from ragged nested lists.
from typing import Any
import numpy as np
def padded_array(list_: list, padval: Any = np.nan) -> np.ndarray:
"""Make np.array from ragged nested lists.
Convert list of any dimension into an np.array with no ragged parts.
[tool.poetry]
name = "test"
version = "0.1.0"
description = ""
[tool.poetry.dependencies]
python = "^3.7"
matplotlib = "^3.3.2"
astropy = "^4.1"
numpy = "^1.19.4"
#!/usr/bin/env python3
"""
Get board temperature
via UDP in IPv4 (SOCK_DGRAM)
from "localhost:16210"
send it to "xffts_board##" and "xffts_power_board##" topics
Get spectral data
via TCP in IPv4 (SOCK_STREAM)
from "localhost:25144"
from astropy.coordinates import SkyCoord, EarthLocation, Angle
from astropy.time import Time
from datetime import datetime
def get_lst(location, hemisphere='n'):
"""Not precise.
Parameters
----------
location: astropy.coordinates.EarthLocation
Location of observer.