andy-z / ged4py

GEDCOM tools for Python
MIT License
18 stars 7 forks source link

Enhancement allow sub_tag to find alternate occurences of tags #39

Closed ukgenes closed 3 years ago

ukgenes commented 3 years ago

Description

Can have multiple BIRT, CENS etc records but no way to access the alternates using a path Desireable: include an occurence number on Record so that tags could be sorted and switched between preferred (i.e. 0) and alternates (1 ...n) with corresponding methods to manipulate the subrecords with a Record

Comment: very nice piece of software ... thx. I would make these enhancements but I haven't programmed for years and don't know github ... so it's too risky!

What I Did

def subtag(self, path, follow=True, alt=0): # <<<<< .... head, , tail = path.partition('/') occurence = 0 for rec in record.sub_records: if rec.tag != head: continue

    if alt != occurence: # <<<<< skips first alt occurences
        occurence += 1
        continue

... supports print('CENS/DATE-0', str(sub_tag(gm.Record.pointer['@I293@'], "CENS/DATE"))) print('CENS/DATE-1', str(sub_tag(gm.Record.pointer['@I293@'], "CENS/DATE", alt=1))) print('CENS/DATE-2', str(sub_tag(gm.Record.pointer['@I293@'], "CENS/DATE", alt=2)))

Paste the command(s) you ran and the output.
If there was a crash, please include the traceback here.
andy-z commented 3 years ago

@ukgenes, thanks for suggestion!

Would it work for you if instead of altering sub_tag I add support for hierarchical path names to sub_tags, like in sub_tag. You could do then:

cens_dates = record.sub_tags("CENS/DATE")
for date in cens_dates:
    print("CENS/DATE", date)

BTW, I do not quite understand your code above:

print('CENS/DATE-0', str(sub_tag(gm.Record.pointer['@I293@'], "CENS/DATE")))

Is sub_tag your own standalone method? It does not look like this is a method that you call on Record class.

andy-z commented 3 years ago

@ukgenes, instead of modifying sub_tag I have extended sub_tags() method to accept hierarchical tag names. You can now say record.sub_tags("CENS/DATE") and it will return a list of records.

Version with this change is 0.4.3, it was pushed to PyPI few minutes ago.