Um... extensible parsing?
holger krekel
pyth at devel.trillke.net
Thu May 9 18:11:54 EDT 2002
Philip Swartzleonard wrote:
>
> I am interested in making a program in python that helps people edit the
> gameworld definition files used in the Angband and it's many and various
> varients. They are a fairly simple text file format, and each record
> follow a mostly set pattern. For exapmle, here's the format for one of
> the files from ToME:
ask any questions you need to about the following code
which should help you get started.
# paste this to your interpreter and
# have a /tmp/datafile ready
specs=[]
for line in open('/tmp/datafile').readlines():
if not line or line.startswith('N'):
specs.append({})
name,rest=line[:1],line[2:]
if name:
sep = '|' in rest and '|' or ':'
vals=map(lambda x: x.strip(), rest.split(sep))
specs[-1].setdefault(name, []).extend(vals)
# then enter e.g.
>>> specs[0]
{'B': ['CLAW', 'HURT', '1d2', 'CLAW', 'HURT', '1d2', 'BITE', 'HURT',
'1d3'],
'D': ["A woman's face on the body of a vicious black bird."],
'E': ['0', '1', '1', '0', '1', '0'],
'F': ['FEMALE',
'CAN_FLY',
'WILD_TOO',
'WILD_MOUNTAIN',
'DROP_CORPSE',
'',
'RAND_25',
'ANIMAL',
'EVIL',
'MORTAL',
'BASEANGBAND'],
'G': ['H', 'D'],
'I': ['120', '3d8', '16', '22', '10'],
'N': ['157', 'Black harpy'],
'O': ['0', '0', '0', '0'],
'W': ['9', '1', '600', '19']}
>>>
> Anyway, I want to make a generic editor program that reads some datafile
> to determine the structure of the 'info' file it needs to edit, and
> constructs it's interface from there. I'd assume i'd have some kind of
> list interface to deal with the records (using the N line information
> that all of thie files that I want to edit use), and that I'd have
> customized controls for editing the different types of data (e.g.
> flag_checkboxes, string_field, dice_field, number_field). What i -don't-
> have a clue how to do is the part that puts the pieces together
> depending on the situation, and the parts that actually parse the
> various files. What i'm asking for is any pointers or insight that could
> help me solve this problem more efficently. Thank you.
- get used to working flexibly with dictionaries
- use a class basically like this:
class niceaccessor:
def __init__(self,specdict):
for attr,value in interpret(specdict):
setattr(attr,value)
so that you can do
print niceaccessor.name
if ANIMAL in niceaccessor.flags:
do this or that
...
- use such a class to make up some frontend
- write a routine which takes any existing
key/value of an niceaccessor-instance
and writes out a record.
- think about whether a string that can
be converted to a number should always
be converted or whether you have
to actually specify this.
hope that helps,
holger
More information about the Python-list
mailing list