Skip to main content

12 posts tagged with "Tips & Tricks"

Introduce some tips and tricks

View All Tags

Avoid Negative Division Pitfalls in Python

· 4 min read

While working on automated accounting transactions with Beancount, I ran into an unexpected issue involving floating-point number conversions. In financial calculations, it's common practice to convert floating-point values to integers (for example, converting 1.55 to 155) by multiplying by 100, perform the necessary calculations, and then convert the result back by dividing by 100. This method helps maintain precision when dealing with currency values.

Sounds straightforward, doesn't it? That's what I thought until negative numbers entered the equation.

To my surprise, when I attempted to convert -155 back to -1.55, Python returned -2.45 instead. Let's dive into why this happens.

使用 Python 处理时间:time.time() vs time.monotonic()

· 9 min read

在写 TiDB Cloud 的 Python SDK 时,我需要实现一个等待集群创建成功的函数 wait_for_available() ,以便用户等待集群变为可用状态。这个函数需要计算集群创建所花费的时间,并在超过用户设置的超时时间时抛出异常。这是一个常见的计算时间差的场景。可以通过记录集群创建的开始时间、获取当前时间,然后计算与开始时间开始时间的差值来实现该功能。伪代码如下:

start_time = GetCurrentTime()
while True:
current_time = GetCurrentTime()
duration = current_time - start_time
if duration > timeout:
raise TimeoutError
if cluster.status == "AVAILABLE":
break

How to Identify Outdated Files in a Git Repository

· 9 min read

Scenario

When maintaining a Git repository, particularly one for documentation, it is common to have files that haven't been updated in a while. To address this issue, you can link a section or sentence in the document to the corresponding code or an existing issue, so that the document can be updated when the code changes or the issue is resolved. This is useful when starting a new project, but it can be difficult to maintain these links for an existing large project.

To identify potential outdated files, you can use the git log command to retrieve the last commit log of each file and find long-term inactive files. The following sections describe how to generate a last commit report for a repository and how to write the script step by step.

Code-Driven Documentation: How to Eliminate Errors in Configuration Documentation

· 11 min read

This document outlines how to utilize code to enhance the accuracy of configuration documentation, particularly for technical writers who might not be well-versed in coding but are responsible for creating or reviewing configuration files. By following the examples in this document, writers can ensure that their configuration files are correct, efficient, and easy to understand, while also reducing the risk of errors or inconsistencies.

How to Bundle Your JavaScript Projects Using webpack

· 7 min read

Scenario

I use webpack to bundle some JS scripts today, see openapi-scripts/pull/16. The following lists details of the project and reasons for bundling:

How to Deploy an OpenAPI Documentation on Netlify

· 4 min read

Scenario

  1. There is an API definition file open-api-swagger.json in a private repository production/api and I have no write access to it.
  2. I want to deploy a preview website of the open-api-swagger.json:
    • test some Redoc configuration in my repository Oreoxmt/preview-api
    • build the open-api-swagger.json specification file into an HTML file index.html using redoc-cli build
    • deploy it on Netlify or other services

RL 01 | I Don't Need to Know How It Works

· 6 min read

My Reading List from July 16, 2022, to July 31, 2022.

Lists

Reading ListInspirationTags
Things I wish everyone knew about Git (Part I)The opposite of git-push is not git-pull.Git
Things I wish everyone knew about Git (Part II)Good advice is commit early and oftenGit
Shell productivity tips and tricksWe will cover some shell features you can leverage to make your shell do more of the work for you.Tips & Tricks
6 deprecated Linux commands and the tools you should be using insteadSwap your old Linux commands for new and improved alternatives that provide the same functionality, if not more.Tips & Tricks

生活小技巧 | Barrier:低成本的键鼠共享

· 8 min read

新一期的生活小技巧分享终于更新啦,这次是社畜奥奥的打工小技巧分享。

如果你需要同时使用多个终端,又不希望两个键盘、两个鼠标来回切换,或者说你只有一个键盘、一个鼠标却想要控制多个终端的话,那这个超级低成本的小技巧就非常适合你啦!

生活小技巧 | 一键下载+批量改名

· 6 min read

最近在学习 Hadoop 的相关内容,厦门大学数据库实验室提供了相关内容的讲义,但是下载的时候要点击 17 次,文件默认的名称也较为繁琐,改名又需要操作 17 次。所以懒懒的奥利奥同学就开始研究有没有一些不太手动的方法,虽然这个过程花费的时间好像会更多(

如果你感兴趣的话,那就继续看下去吧。

应用场景:实现这个页面所有 PPT 的一键下载,以及后续文件名的修改

代码获取:github

轮子再造 | 使用 GitHub Actions 自动部署 Hexo 博客 - 上篇

· 15 min read

Oreo 同学最近开始记笔记和写心情了,但是就是不走寻常路偏偏想整一个自己的网站来写博客。正巧之前有一个 oreo.life 的域名,很快就定下来了 Hexo + GitHub Pages + Cloudflare CDN 的方案,Oreo 自己折腾了一段时间,成功部署了现在你看到的博客并在坚持更新,可喜可贺可喜可贺。

但是有个问题,GitHub Pages 对应的 Repo 存的是渲染过后的 HTML 文件,我们总得有个地方来存原始的 Markdown 文档。于是更新博客首先需要在 _source 目录写 Markdown,然后在根目录执行 hexo generatehexo deploy 更新网站。命令行操作就先不说了,还要研究什么 Node.js、npm 是不是太过分了,在 Windows 配置环境超麻烦的好吗。世界如此美好,我却还要存 node_modules,这样不好,不好。

成,好歹咱也是做过 CI 的,GitHub Actions 也出来一段时间了看起来反响不错,那肯定也可以用 Actions 完成从博客源码到 Pages 部署的完整流程。Google 一搜发现已经有很多很多人做了类似的事情了,但是总有一些小地方不够好看或者是不满足需求,那就只能自己重新造一遍轮子了。