我在Python3.8中有一个名为“stores.py”的文件。这个文件有一个名为“scan_transactions”的方法,它接受两个位置参数:“store”和“checkpoint”。该方法基本上通过使用正则表达式模式扫描PostgreSQL表中的存储事务。当代码到达事务表中该特定存储的最后一个事务id时,将使用并更新另一个表(检查点表),以指示任何给定存储的最新最大事务id
目前,我正在从一个预定义的字典(类似于下面的字典)传递这两个参数:
dict_stores = {'store1': 'checkpoint_store1', 'store2': 'checkpoint_store2','store3': 'checkpoint_store3'}
当前代码如下所示:
def store_transactions(store: str, checkpoint_name: str)
.
.
.
.
.
if __name__ == '__main__':
for store, checkpoint in shops.dict_stores.items():
LOG.debug(f'Processing store : {store}, checkpoint: {checkpoint}')
store_transactions(store, checkpoint)
我现在希望使其更具动态性,并允许用户在执行之前将要处理事务的存储作为批处理作业传递。这将使用下面的命令行:
"stores.py" --stores -store1 -store2 -store3...etc.
然后,上面的命令将替换这个预先固定的字典并动态创建一个字典。有人知道我如何使用“arg parser”以编程方式将参数“-shop1”、““-shop2”转换为类似于上面的字典(它们各自的检查点作为值),并使用当前运行的相同循环处理所有存储吗
我发现使用一个在
,
上拆分的结构来读取可能性列表是很方便的这就像
注意,我认为您需要使用位置argparse参数来重复它们(即,您没有存储选项名称)。或者,也许我对OptPass感到困惑,因为我现在主要使用Click
文档的nargs部分涵盖了这一点,因此看起来您也可以使用
store
。没有非常清楚的例子。也就是说,对于用户来说,这需要更多的输入,所以我会选择位置输出:
替代解决方案:只需读取一个JSON配置文件,可能将参数设置为
stdin
的文件名或触发器相关问题 更多 >
编程相关推荐