api 常量大全_使模块常量成为API的一部分是邪恶的

news/2024/7/10 1:59:43 标签: python, java, vue, 设计模式, 编程语言

api 常量大全

One of the initial features which were included in psutil since day one (5 years ago) were system’s boot time, number of CPUs and total physical memory. These metrics have one thing in common: they are (apparently) not supposed to change over time. That is why we (me and Jay) decided that exposing them as module constants calculated at import time was the way to go.

自第一天(5年前)起, psutil中包含的初始功能之一是系统的启动时间,CPU数量和总物理内存。 这些度量标准有一个共同点:(显然)不应随时间变化。 这就是为什么我们(我和周杰伦 )决定将它们暴露为在导入时计算出的模块常量是解决之道。


>>> psutil.NUM_CPUS
>>> psutil.NUM_CPUS
2
2
>>> psutil.BOOT_TIME # as seconds since the epoch
>>> psutil.BOOT_TIME # as seconds since the epoch
1387579049.799092
1387579049.799092
>>> psutil.TOTAL_PHYMEM
>>> psutil.TOTAL_PHYMEM
8374120448
8374120448

5 years later I regret that decision and I’m going to explain you why you don’t want to do the same mistake.

5年后,我对这个决定感到遗憾,并且我将向您解释为什么您不想犯同样的错误。

常数不应该改变 (A constant should not change)

When we think of  ‘constants’, our expectations are that they should not change over time. It may be obvious, but before thinking about introducing a constant be absolutely sure the value it represents is going to remain the same.Now, back then we thought these 3 metrics were not supposed to change, at least until the system is rebooted. Well, we were wrong: it turns out 2 of them actually can.Apparently virtualized systems can change physical installed memory at runtime (see here and here) and system boot time can easily be altered every time you update the system clock.In both of these cases, of course, the constants will not reflect the updated values.

当我们想到“常数”时,我们的期望是它们不应随着时间而改变。 这可能很明显,但是在考虑引入一个常数之前, 必须绝对确保它代表的值将保持不变。 现在,那时我们认为这三个指标至少在重新启动系统之前都不会改变。 好吧,我们错了:事实证明它们实际上是两个。显然,虚拟化系统可以在运行时更改物理安装的内存(请参阅此处和此处 ),并且每次更新系统时钟时都可以轻松更改系统启动时间。当然,在这些情况下,常量将不会反映更新后的值。

在导入时做事很危险 (Doing things at import time is dangerous)

That’s because import time usually means startup time and if something goes wrong the whole application will crash. In general the only reason for a module to crash at import time is because of a missing dependancy or because it’s not supposed to run on that platform in the first place.Now, here’s a couple of bug reports which were collected over time: issue 188, issue 313.The inconvenience was so severe that I had to release different fixed versions ASAP and the fix consisted of a stinky workaround.That’s when I started thinking about getting rid of those constants once and for all and introduce functions instead. But how to do that without breaking everybody’s code?

那是因为导入时间通常意味着启动时间 ,如果出现问题,整个应用程序将崩溃。 总的来说,模块在导入时崩溃的唯一原因是因为缺少依赖关系或因为它最初不应该在该平台上运行。现在,这里有一些随着时间推移而收集的错误报告: 第188期 , 问题313。不便之处非常严重,以至于我不得不尽快发布不同的固定版本,并且此修复程序包含一个臭味十足的变通办法 。从那时起,我开始考虑一劳永逸地摆脱这些常数,而引入功能。 但是如何做到这一点而又不破坏每个人的代码?

向后兼容性很重要 (Backward compatibility matters)

Now here’s the crucial part: every time you deliver a library to someone else you just cannot remove an API all of the sudden, especially if they are 3 and have been around since day one.It should first be deprecated, possibly turned into an alias pointing to a newer API and finally be removed after 1 or 2 major releases. Also, you want the deprecated API to explicitly raise a DeprecationWarning informing the user he’s relying on something which will eventually be removed. With a module constant you cannot do any of that. What you would need is a module property.

现在这是关键部分:每次将库交付给其他人时,您都无法突然删除API,尤其是当它们是3且自第一天就存在时,应该首先弃用它,可能会变成别名指向较新的API,最后在1或2个主要版本中被删除。 此外,您还希望弃用的API显式引发DeprecationWarning,以通知用户他所依赖的东西最终将被删除。 使用模块常量,您将无法执行任何操作。 您需要的是模块属性

模块属性 (Module properties)

One of the greatest things about Python is that it’s so dynamic that it lets you do all sort of nasty things with objects, including injecting names into modules (which are also objects) and make them behave like actual class properties!For this I have to thank Josiah Carlson who came up with this very simple yet effective solution:

Python的最大优点之一就是它的动态性,它使您可以对对象进行各种令人讨厌的事情,包括将名称注入模块(也是对象)并使它们的行为像实际的类属性一样。为此,我必须感谢Josiah Carlson提出了这个非常简单但有效的解决方案:

翻译自: https://www.pybloggers.com/2013/12/making-module-constants-part-of-your-api-is-evil/

api 常量大全


http://www.niftyadmin.cn/n/983155.html

相关文章

OpentTsdb官方文档中文版----查询性能

缓存 此时OpenTSDB没有内置缓存(除了将缓存PNG图像文件60秒的内置GUI)。因此只能依靠底层数据库的缓存。在HBase(最常见的OpenTSDB后端)中,有一个块缓存的概念,它可以在写入 和/或 读取时在内存中存储行和列…

我为什么加入 TDengine

全文约 3,537 字,阅读需要 8 分钟2021 年下半年,在我的好友 TGO 鲲鹏会北京分会委员,马泷医疗集团 CTO 于游的推荐下,受“水滴风马产品公社”的邀请,我为某大厂几十位高 P 同学进行了一次《产品定位与规划》的全天课程…

批量识别保存图片文字的小工具

本次分享一个免费的小工具,可以一次性把成千上万张图片中的文字提取出来。 软件可点击这里下载: https://www.very361.com/zb_users/upload/2022/12/20221202232317166999459777296.zip 本软件的功能,如图: 使用方法应该不用教…

有字体获取 宽度 或者高度

http://www.cnblogs.com/zouzf/archive/2012/11/21/2780040.html转载于:https://www.cnblogs.com/yhj1787354782/p/5102177.html

从 1869 项开源产品中脱颖而出,TDengine 荣登 2021 “科创中国”开源创新榜

在近日中国科协召开的 2022 “科创中国”年度会议上,周济院士发布了 2021 “科创中国”开源创新榜单。本次共有 1869 项开源产品、230 家社区和企业机构参与到榜单评选中,经过来自产学研界的开源专家评审, 最终 TDengine 凭借着强大的产品创新…

在Python中将Firefox扩展与Selenium结合使用

翻译自: https://www.pybloggers.com/2018/09/using-firefox-extensions-with-selenium-in-python/

PHP判断一个请求是Ajax请求还是普通请求

先说前端使用 jQuery 时怎么区分: jQuery 发出 ajax 请求时,会在请求头部添加一个名为 X-Requested-With 的信息,信息内容为:XMLHttpRequest 在后端可以使用 $_SERVER["HTTP_X_REQUESTED_WITH"] 来获取。(注…

OpenTsdb官方文档中文版----聚合器

OpenTSDB旨在在查询执行的过程中有效地组合多个不同的时间序列。原因在于:当用户查看他们的数据,他们通常会从高层的角度开始提问,例如“数据中心的总吞吐量是多少”或“当前地区用电量是多少”。在查看到这些高层次的值之后,可能…