简体   繁体   English

解析复杂的基于字符串的配置选项

[英]Parsing complicated string-based configuration options

I'm trying to build a Python program that will parse the conversion codes from a Universe Database.我正在尝试构建一个 Python 程序,它将解析来自 Universe 数据库的转换代码。 These conversion codes are highly dense strings that encode a ton of information, and I'm struggling to get started.这些转换代码是高度密集的字符串,编码了大量信息,我正在努力入门。 The different permutations of the conversion codes likely number in the hundreds of thousands (though I haven't done the math).转换代码的不同排列可能有数十万种(尽管我没有计算过)。

I'm familiar with argparse , however I can't come up with a way of handling this kind of parsing with argparse , and my Google-fu hasn't come up with any other solution.我熟悉argparse ,但是我想不出一种方法来处理这种用argparse进行的解析,而且我的 Google-fu 也没有想出任何其他解决方案。

Initially, my lazy work around was to just do a dictionary lookup for the most common conversion codes, but now that we're using this Python program for more data, it's becoming a huge chore to maintain each individual conversion code.最初,我懒惰的工作是只对最常见的转换代码进行字典查找,但现在我们正在使用这个 Python 程序来获取更多数据,维护每个单独的转换代码变得非常繁琐。

For example, date conversion codes may take forms like:例如,日期转换代码可能采用 forms,如:

  • Date: D[n][*m][s][fmt[[f1, f2, f3, f4, f5]]][E][L] , eg D2/ or D4-2/RM日期: D[n][*m][s][fmt[[f1, f2, f3, f4, f5]]][E][L] ,例如D2/D4-2/RM
  • Datetime: DT[4|D|4D|T|TS|Z][;timezone] , eg DTZ or DT4;America/Denver日期时间: DT[4|D|4D|T|TS|Z][;timezone] ,例如DTZDT4;America/Denver
  • Datetime ISO: DTI[B][R|W][S][Z][2|1|0][;[timezone|offset]] , eg DTIBZ2 or DTIR;America/Denver日期时间 ISO: DTI[B][R|W][S][Z][2|1|0][;[timezone|offset]] ,例如DTIBZ2DTIR;America/Denver

And there are a bunch of other conversion codes, with equally complicated parameters.还有一堆其他的转换代码,参数同样复杂。

My end goal is to be able to convert Universe's string data into the appropriate Python object and back again, and in order to do that, I need to understand these conversion codes.我的最终目标是能够将 Universe 的字符串数据转换为适当的 Python object 并再次转换回来,为此,我需要了解这些转换代码。

If it helps, I do not need to validate these conversion codes.如果有帮助,我不需要验证这些转换代码。 Once they are set in the database, they are validated there.一旦将它们设置在数据库中,就会在那里进行验证。

I recommend that you use the readnamedfields/writenamedfields methods, this will return OCONV data, and when you write back it handles the ICONV.我建议您使用 readnamedfields/writenamedfields 方法,这将返回 OCONV 数据,当您写回时它会处理 ICONV。

import u2py help( u2py.File.readnamedfields) Help on function readnamedfields in module u2py: import u2py help(u2py.File.readnamedfields) u2py 模块中关于 function readnamedfields 的帮助:

readnamedfields(self, *args) F.readnamedfields(recordid, fieldnnames, [lockflag]) -> new DynArray object -- read the specified fields by name of a record in the file readnamedfields(self, *args) F.readnamedfields(recordid, fieldnnames, [lockflag]) -> new DynArray object -- 通过文件中记录的名称读取指定字段

fieldnames is a u2py.DynArray object with each of its fields being a name defined in the dictionary file
lockflag is either 0 (default), or [LOCK_EXCLUSIVE or LOCK_SHARED] [ + LOCK_WAIT]

note: if fieldnames contains names that are not defined in the dictionary, these names are replaced by @ID and no exception is raised

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM