快乐冲浪与生活

多体验、多体会、多体悟

0%

局部变量优化,是指将全局变量 / 函数的地址赋值给局部变量,从而减少内存寻址的开销。

我自己的一个观点,大多数应用的瓶颈在于数据库、磁盘 I/O,所以一般不会用这一优化技术。但我今天在项目代码中看到类似的代码, 又问了豆包,豆包说有场景有必要,所以写一个测试验证一下。

本文用于记录在使用 Python 过程中不知道的类装饰器的用法与功能。

@dataclass

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
"""Add dunder methods based on the fields defined in the class.

Examines PEP 526 __annotations__ to determine fields.

If init is true, an __init__() method is added to the class. If repr
is true, a __repr__() method is added. If order is true, rich
comparison dunder methods are added. If unsafe_hash is true, a
__hash__() method is added. If frozen is true, fields may not be
assigned to after instance creation. If match_args is true, the
__match_args__ tuple is added. If kw_only is true, then by default
all fields are keyword-only. If slots is true, a new class with a
__slots__ attribute is returned.
"""

根据类中字段的定义,给类添加魔法方法:

在数据库中,查询按某一字段降序和升序的关键字,分别是 DESCASC,不指定则默认按升序,也就是 ASC,如下面三个查询:

不指定

1
SELECT created_at, id FROM llm_models ORDER BY created_at;

指定 ASC

1
SELECT created_at, id FROM llm_models ORDER BY created_at ASC;

最近在写一个产品,在认证鉴权模块用到了 JWT,为探究其本质,所以打算花一些时间学习一下,并记录成文。

首先,回答两个问答:

  1. 什么是 JWT?
  2. JWT 解决了什么样的问题?

什么是 JWT?

看了那么多天的 Playwright,准备做一个文献信息爬取,设定的需求如下:

  • 爬取 PubMed 上,查询词为 maize(玉米)的 100 篇文献
  • 要求近 5 年内摘要可访问的文献
  • 单篇文献应该具有以下字段:
    • title(标题)
    • authors(作者),以英文逗号和空格间隔
    • journal(期刊)
    • first affiliation(第一单位)
    • abstract(摘要)
    • pmid(PubMed 文献编号)
    • doi(文献数字对象标识)
    • publish date(发表日期)
    • pubmed_url(详情地址)
  • 按 publish date 降序排列

思路

  • 打开列表页,每页 10 个文献,爬取文献的标题和详情页地址(10 次)
  • 遍历获得的 100 个文献及详情页地址
  • 从详情页获取其它信息
  • 全部信息获取完毕后,保存至 CSV 文件

编码

编写用于存储数据的模型和将模式数据写入 CSV 的方法: