我们在Somenergia使用且可能有用的工具
项目描述
somenergia-utils
此模块包括在SomEnergia合作组织脚本中普遍使用的不同Python模块和脚本,但没有实体本身拥有自己的存储库。
venv
:在 Python 虚拟环境中运行命令的脚本sql2csv.py
:运行参数化 SQL 查询并将结果作为(制表符分隔的)csv 获取的脚本。tsv
:用于快速序列化 yamlns.namespace 和类似字典对象的模块(csv,使用制表符作为分隔符)tsvread
:提供 TSV 行作为 yamlns.namespaces(字典特殊化)tsvwrite
:将字典序列写入 TSV
dbutils.py
:包含 db 相关功能的模块runsql
:运行包含 SQL 的文件并将对象作为 yamlns.namespaces 返回runsql_cached
:类似于 runsql,但将结果缓存到 tsv 文件中以避免重复运行fetchNs
:一个生成器,它封装 db 游标以获取具有属性的对象,而不是 psycopg 数组nsList
:使用前面的功能构建此类对象的列表(较慢,但可能更方便)csvTable
:[已弃用,使用 runsql] 将查询结果转换为具有适当标题名称的制表符分隔的表格pgconfig_from_environ
:从 PG 环境变量构建 db 连接配置字典
isodates
:将字符串转换为时间/日期/datetime 对象,具有无知的/时区处理sheetfetcher.py
:从 gdrive 电子表格检索数据的便利类trace
:通过使用@trace
装饰器快速启用和禁用函数调用跟踪testutils
:包含常见测试工具的模块testutils.enterContext
:(Py<3.11 polyfill)从 setUp() 中进入上下文处理器,确保在拆卸时退出testutils.destructiveTest
:装饰器,用于防止在生产中运行破坏性测试testutils.temp_path
:上下文管理器,为测试提供自我销毁的临时目录testutils.working_dir
:上下文管理器,更改当前工作目录并在之后恢复它testutils.sandbox_dir
:上下文管理器,结合temp_path
和working_dir
erptree
:从 erp(erppeek,odoo)中提取对象及其子对象,具有受控递归
venv
脚本
简化在给定虚拟环境中运行 Python 脚本。这在从 crontab 行运行 Python 脚本时特别有用。
usage: venv /PATH/TO/PYTHON/VIRTUALENV COMMAND [PARAM1 [PARAM2...]]
sql2csv.py
脚本
运行 SQL 文件并将查询结果输出为制表符分隔的 csv 文件。
需要本地 config.py
文件,或者您可以使用 -C file.py
提供。它应包含一个名为 pyscopg
的字典,其中包含 psycopg2.connect 的关键字参数。
您可以通过 yamlfile 或命令行选项提供查询参数。
sql2csv.py <sqlfile> [<yamlfile>] [--<var1> <value1> [--<var2> <value2> ..] ]
dbutils
Python 模块
方便的数据库访问
有一个包含查询的文件。
params = dict(
param1='value',
param2=[1,3,4,5], # Useful for the IN clause
)
for item in runsql('myquery.sql', **params):
print(item.id, item.description) # id and description should be columns in the query
像 sql2csv
一样,必须存在一个 config.py 文件,或者您可以使用 config
关键字参数提供它
参数将按照 psycopg2 文档 中指定的方式插入
TODO:db 和 cursor 参数以重用现有参数。
如果您知道结果总是相同的,则可以使用 runsql_cached
。它将生成与 sql 文件类似的 tsv 文件名,并使用它而不是查询进行后续执行。
runsql_cached
的用法与 runsql
非常相似,可以互换。 runsql_cached
只是添加了一些关键字参数。
cachefile
:用于覆盖默认缓存文件(名称、路径或文件对象)force
:强制执行查询,即使缓存存在
游标包装器
方便的游标包装器,使数据库访问代码更易于阅读。
示例
import psycopg2, dbutils
db = psycopg2.connect(**dbconfiguration)
with db.cursor() as cursor :
cursor.execute("SELECT name, age FROM people")
for person as dbutils.fetchNs(cursor):
if person.age < 21: continue
print("{name} is {age} years old".format(person))
sheetfetcher
Python 模块
gdrive 的便利包装器。
from sheetfetcher import SheetFetcher
fetcher = SheetFetcher(
documentName='My Document',
credentialFilename='drive-certificate.json',
)
table = fetcher.get_range("My Sheet", "A2:F12")
fulltable = fetcher.get_fullsheet("My Sheet")
- 文档选择器可以是 uri 或标题
- 工作表选择器可以是索引、名称或 id。
- 范围选择器可以是命名范围、索引元组或“ A2:F5”坐标。
- 您应该创建一个证书并授权其访问文档
跟踪
这个装饰器是一个用于跟踪函数和方法调用的快速助手。它将显示函数的名称、参数值和返回值。
from trace import trace
@trace
def factorial(n):
if n<1: return 1
return n*factorial(n-1)
factorial(6)
('> factorial', (6,))
('> factorial', (5,))
('> factorial', (4,))
('> factorial', (3,))
('> factorial', (2,))
('> factorial', (1,))
('> factorial', (0,))
('< factorial', (0,), '->', 1)
('< factorial', (1,), '->', 1)
('< factorial', (2,), '->', 2)
('< factorial', (3,), '->', 6)
('< factorial', (4,), '->', 24)
('< factorial', (5,), '->', 120)
('< factorial', (6,), '->', 720)
自定义断言 testutils.assertNsEqual
及其相关
这些断言已移动到 yamlns.testutils
和 yamlns.pytestutils
。为了保持兼容性,这些导入也可以在 somutils.testutils
中找到。
assertNsEqual
允许断言类似于 json/yaml 的结构(结合字典、列表、数字、字符串、日期等)之间的相等性。
在规范化结构(排序字典键)之后,比较是在 YAML 输出上进行的,以便将差异作为文本差异发现。yamlns.testutils
还提供了 assertNsContains
,它可以忽略第二个参数中的额外键。
yamlns.pytestutils
为 pytest 提供了等效功能
assert_ns_equal
assert_ns_contains
还包括这些 pytest 固定值
test_name
固定值text_snapshot
固定值yaml_snapshot
固定值
testutils.temp_path
上下文处理程序,它创建一个临时目录并确保在退出上下文后删除它。
with testutils.temp_path() as tmp:
# The folder will exist while you are in the context
# You can use tmp as path
with (tmp/"myfile").open() as f:
f.write("yep")
# No trace of the folder after the context
testutils.enterContext
允许在 setUp
中打开上下文处理程序,在拆卸时正确关闭。这允许使用上下文处理程序作为快速自我销毁的固定值。
这是 Python 3.11 中引入的同名方法的 polyfill。
用法
from somutils.testutils import enterContext
class MyTest(unittest.TestCase):
if not hasattr(unittest.TestCase, 'enterContext'):
enterContext = enterContext
def setUp(self):
self.file = self.enterContext(open('myfile'))
# on teardown, the file is closed
testutils.working_dir
在上下文活动期间更改工作目录,并在之后恢复以前的工作目录。
with testutils.working_dir("this/other/folder"):
print(os.get_cwd()) # here we are in this/other/folder
print(os.get_cwd()) # here we are back to our previous working dir
testutils.sandbox_dir
结合前两个上下文处理程序以创建临时目录,将其设置为当前工作目录,并在上下文结束时恢复工作目录并清理临时文件夹。
with testutils.sandbox_dir() as sandbox:
# There you use tmp folder as path
with Path("myfile").open() as f:
f.write("yep")
# No trace of the folder after the context
testutils.destructiveTest
这是一个实用工具,用于避免在生产 OpenERP 中运行破坏性测试。它是一个装饰器,它会检查 dbconfig
中配置的 erp 是否有测试标志,如果没有,则跳过测试。
还提供了脚本 enable_destructive_test.py
,用于设置/取消设置该测试标志,该标志默认未定义。
isodates
这是一个用于简化 isodate 解析和时区处理的模块。
sequence
解释字符串,就像标准打印对话框中用于指定要打印的页面所使用的字符串一样。例如,“2,4,6-9,13”表示“2, 4, 从 6 到 9 和 13”。
>>> [x for x in sequence("2,4,6-9,13")]
[2, 4, 6, 7, 8, 9, 13]
erptree
从一个 ERP 对象创建结构,您可以使用它进行导航或将其作为 yaml 输出。您可以选择要展开的字段,仅使用名称或 id,匿名化或删除。您可以使用点表示法指定相关对象中的字段。对于多个关系,子字段指的是所有相关对象中的子字段。
from somutils.erptree import erptree
O = Client(....)
partner = erptree(
partnerAddress_id,
O.ResPartnerAddress,
expand = {
# Will expand those attributes
'partner_id': O.ResPartner,
'partner_id.company': O.ResPartner,
},
pickName = [
# For those fk pick the name instead of the default id,name tuple
# pickId parameter does the analog with id
'partner_id.state_id',
'partner_id.country_id',
],
anonymize = [
# Will tamper the value
'email',
],
remove = [
# Means clear this field
'superfluous_field',
],
only = [
# Means: just retrieve name and code from partner_id.company
'partner_id.company.name',
'partner_id.company.code',
],
)
项目详情
下载文件
下载适用于您平台的文件。如果您不确定要选择哪个,请了解更多关于 安装软件包 的信息。
源分发
构建分发
somutils-1.9.0.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 8cd1151b71ed62236a82cce2c6bbcc4c2ad85ecfc34deabca086e11622ed04e9 |
|
MD5 | 1a0bf4b0af77bf0e0cb7efb698e50210 |
|
BLAKE2b-256 | 1df9b0ba63b613f2a1d05c84a14691672170892cbd25608b917ea4b8a7311b74 |
somutils-1.9.0-py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 334ead258e09efe13dc6f80a0221df5e33af5e565ad05fb7b2ff933f0086e5b3 |
|
MD5 | 66c642fe752e6a9c9025a83f88499cea |
|
BLAKE2b-256 | 60639ed98cfebff9f6c81a83214e3bb149dbf99f028136dae20762964d679dbb |