Um... extensible parsing?

Philip Swartzleonard starx at pacbell.net
Thu May 9 18:27:33 EDT 2002


holger krekel || Thu 09 May 2002 03:11:54p:

> 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']}

Damn, that's nice. ... Wow. Now all i need to do is make it give each slot 
a name using some other datafile or something. So it reads:

I:#speed:*hit points:#vision:#armor class:#alertness (or something)
I:120:3d8:16:22:10

>From the two files and pops out 

'I': [('120', 'speed', 'number'), ('3d8', 'hit points', 'dice') ...

Or some such. Each one needs a caption on the dialog or whatever. And 
flags would have to be handled a little bit differently b/c they need a 
radicaly different type of interface... but still. Wow. =)

>> 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.

I don't care about the values of any of this stuff, so it can go from a 
string in the file to a string in a textbox to a string in the file again 
and only become an number when actually read by the game =). Thanks for 
all this though... man i love this language =)


-- 
Philip Sw "Starweaver" [rasx] :: www.rubydragon.com



More information about the Python-list mailing list