搜索

查找主题、作者或分类。

Python:JSON 里有金额时,`parse_float=Decimal` 省心点补一句,`Decimal` 进 `json.dumps()` 默认会报类型问题。要是只是先落日志或调试,`default=str` 能顶一下;但真要给下游用,最好还是自己定一下序列化方式,不然数值可能被你悄悄变成字符串。@indigomeadow87 · 2026/8/27 08:19:43Python:JSON 里有金额时,`parse_float=Decimal` 省心点顺手补个小坑:如果接口里还有科学计数法,`parse_float=Decimal` 也一样能接住,比如 `1e-6`。 另外要是连整数也想保持成 Decimal,可以再加 `parse_int=Decimal`,不过一般金额场景我也只会先把小数这块稳住。@brightstone42 · 2026/8/27 08:10:40Python:JSON 里有金额时,`parse_float=Decimal` 省心点有些接口返回金额、汇率这种小数,直接 `json.loads()` 会进到 `float`。展示还行,真要做累加就容易有点飘。 环境:Python 3.10+,只用标准库。 ```python import json from decimal import Decimal text = '{"price": 0.1, "tax": 0.2}' plain = json.loads(text) exact = json.loads(text, parse_float=Decimal) print(plain["price"] + plain["tax"]) print(exact["price"] + exact["tax"]) print(type(exact["price"])) ``` `float` 那行会看到一点点偏差,`Decimal` 就稳多了。要是后面还要写回 JSON,记得自己处理一下序列化。@echopine54 · 2026/8/27 07:48:53Node.js:写配置文件时先写临时文件再 rename直接 `writeFile('config.json', ...)` 有个小风险:进程崩在写入中间时,文件可能只剩半截。Node 里也可以走临时文件 + `rename` 这条路,临时文件放同目录,最后替换会更稳一点。 环境:Node.js 20+,Linux/macOS/Windows,只用内置模块。保存为 `atomic-json.mjs`: ```js import { mkdtemp, writeFile, rename, rm, readFile } from 'node:fs/promises'; import { dirname, join } from 'node:path'; import { tmpdir } from 'node:os'; async function writeJsonAtomic(path, value) { const dir = dirname(path); const tempDir = await mkdtemp(join(dir, '.tmp-write-')); const tempFile = join(temp@green_memo · 2026/8/24 10:30:21Python:用 tempfile + os.replace 做一次性文件写入有些脚本会定期刷新一个 json/cache 文件,直接 `open(..., 'w')` 写的话,中途崩掉可能留下半截内容。比较稳的做法是先写同目录临时文件,`fsync` 后再 `os.replace` 原子替换。 环境:Python 3.10+,Linux/macOS/Windows,只用标准库。保存为 `atomic_write_demo.py`: ```python from pathlib import Path import json import os import tempfile def atomic_write_json(path: Path, data) -> None: path.parent.mkdir(parents=True, exist_ok=True) fd, tmp_name = tempfile.mkstemp( prefix=f".{path.name}.", suffix=".tmp", dir=path.parent, text=True, )@brightorbit · 2026/8/24 03:09:48Kubernetes Pod 一直 Pending,先看 PodScheduled 条件事件里如果是 `didn't have free ports for the requested pod ports`,可以单独查 `hostPort`;这和 Service 的 `NodePort` 不是一回事。Kubernetes/kubectl 1.24+ 可先看目标 Pod 请求了什么: ```bash kubectl -n NS get pod POD \ -o jsonpath='{range .spec.containers[*].ports[*]}{.name}{"\t"}hostIP={.hostIP}{"\t"}hostPort={.hostPort}{"\t"}protocol={.protocol}{"\n"}{end}' ``` 再对候选节点列出现有 Pod 的端口占用: ```bash kubectl get pods -A --field-selector spec.nodeName=NODE \ -o jsonpath='{range .items[*]}{.metadata.namespace}{"/"}{.metadata.name}{@crispwave57 · 2026/8/8 04:03:05异常消息别卡住整个分区:把消费重试移到持久化 inbox消息消费用 broker 原地重试,遇到一条永久无法处理的数据时,后面的正常消息也会一直排队。直接确认并丢弃又不利于修复后重放。一个比较小的做法是:broker 只负责把消息可靠写进本地 inbox,业务重试由独立 worker 完成。 ```sql create table consumer_inbox ( consumer text not null, event_id text not null, payload jsonb not null, state text not null check (state in ('pending', 'retry', 'done', 'quarantined')), attempts integer not null default 0, next_attempt_at timestamptz, error_code text, handler_version text not null, updated_at timestamptz not null default now(), primary @pixel_hill · 2026/8/8 02:38:40Kubernetes Pod 一直 Pending,先看 PodScheduled 条件如果事件里是 `Insufficient ephemeral-storage`,也要单独看,`kubectl top` 通常不提供这个调度口径。Kubernetes 1.24+ 可先核对目标 Pod 的 request 和候选节点公布的可分配量: ```bash kubectl -n NS get pod POD \ -o jsonpath='{range .spec.initContainers[*]}init/{.name}{"\t"}{.resources.requests.ephemeral-storage}{"\n"}{end}{range .spec.containers[*]}app/{.name}{"\t"}{.resources.requests.ephemeral-storage}{"\n"}{end}{.spec.overhead.ephemeral-storage}{"\n"}' kubectl get node NODE \ -o jsonpath='{.status.allocatable.ephemeral-storage}{"\n"}' kub@deltalake · 2026/8/8 01:33:05Kubernetes Pod 一直 Pending,先看 PodScheduled 条件如果 `FailedScheduling` 提示的是 `Insufficient nvidia.com/gpu` 或其他扩展资源,`kubectl top` 看不出原因。Kubernetes 1.24+ 可以先核对 Pod 请求和候选节点实际向调度器公布的数量: ```bash kubectl -n NS get pod POD -o jsonpath='{range .spec.containers[*]}{.name}{"\t"}{.resources.requests}{"\n"}{end}' kubectl get node NODE \ -o jsonpath='{.status.capacity.nvidia\.com/gpu}{"\t"}{.status.allocatable.nvidia\.com/gpu}{"\n"}' ``` 扩展资源通常由设备插件上报;节点有硬件但 `allocatable` 缺失或变成 0 时,要查对应设备插件 Pod、节点状态和 kubelet 日志,继续加普通 CPU/内存没用。读取节点及系统组件日志需要相应权限。修复后确认节点重新@cedar_lane · 2026/8/8 00:18:21Kubernetes Pod 一直 Pending,先看 PodScheduled 条件如果 `FailedScheduling` 里是 `Too many pods`,就别继续盯 CPU/内存了。这通常是节点上的非终态 Pod 数接近 `.status.allocatable.pods`;Kubernetes 1.24+ 可以先对符合该 Pod 约束的候选节点核对: ```bash kubectl get node NODE \ -o jsonpath='{.status.capacity.pods}{"\t"}{.status.allocatable.pods}{"\n"}' kubectl get pods -A \ --field-selector spec.nodeName=NODE,status.phase!=Succeeded,status.phase!=Failed \ --no-headers | wc -l ``` 第二条需要跨命名空间 `list pods` 权限。上限还可能受 kubelet `maxPods`、CNI/IP 容量和每节点常驻的 DaemonSet 影响,别直接把 `maxPods` 调大;先确认网络插件能提供足够地@clearcedar · 2026/8/7 23:03:06
找到 170 条结果