views:

33

answers:

2

I'm new to Python, and I can say off the bat my programming experience is nominal compared to many of you. Brace yourselves :)

I have 2 files. A GEDCOM parser written in Python that I found from a user on this site (gedcom.py - http://ilab.cs.byu.edu/cs460/2006w/assignments/program1.html) and a simple GEDCOM file that I pulled from heiner-eichmann.de/gedcom/gedcom.htm. Guess who's having trouble putting 2 and 2 together? This guy...

Here is a code snippet followed by what I've done thus far.

class Gedcom:
""" Gedcom parser

This parser is for the Gedcom 5.5 format.  For documentation of
this format, see

http://homepages.rootsweb.com/~pmcbride/gedcom/55gctoc.htm

This parser reads a GEDCOM file and parses it into a set of
elements.  These elements can be accessed via a list (the order of
the list is the same as the order of the elements in the GEDCOM
file), or a dictionary (the key to the dictionary is a unique
identifier that one element can use to point to another element).

"""

def __init__(self,file):
    """ Initialize a Gedcom parser. You must supply a Gedcom file.
    """
    self.__element_list = []
    self.__element_dict = {}
    self.__element_top = Element(-1,"","TOP","",self.__element_dict)
    self.__current_level = -1
    self.__current_element = self.__element_top
    self.__individuals = 0
    self.__parse(file)

def element_list(self):
    """ Return a list of all the elements in the Gedcom file.  The
    elements are in the same order as they appeared in the file.
    """
    return self.__element_list

def element_dict(self):
    """ Return a dictionary of elements from the Gedcom file.  Only
    elements identified by a pointer are listed in the dictionary.  The
    key for the dictionary is the pointer.
    """
    return self.__element_dict

my little script

import gedcom
g = Gedcom('C:\tmp\test.ged') //I'm on Windows
print g.element_list()

From here, I receive a bunch of output "gedcom.Element instance at 0x00..."

I'm not sure why I'm receiving this output. I thought according to the element_list method a formatted list would be returned. I've Googled and search this site. The answer is probably staring me in the face but I was hoping someone could point out the obvious.

Much Appreciated.

A: 

someclass instance at 0xdeadbeef is the result of the the standard __repr__ method for classes that don't define one, as apparently class gedcom.Element doesn't, so the problem is only with you printing a list of such instances. If such class defines __str__, you could

for x in g.element_list():
    print x

but if it doesn't, that will also give similar output (as __str__ "defaults to" __repr__). What do you want to do with those elements, e.g. a method that their class does offer?

Alex Martelli
Ok I see what's happening. The class does define __str__ so that took care of it. Thank you!
Jay
@Jay, you're welcome!
Alex Martelli
A: 

There's nothing wrong or unusual about that output. Because gedcom.Element hasn't defined a __repr__, printing the list will show the default __repr__. If you wanted to access a particular attribute on each element, you could try:

print [element.some_attribute for element in g.element_list()]

edit: Aha, I looked over the source you provided. It does indeed define a __str__, but no __repr__. Here's what you want, most likely:

for element in g.element_list()
    print element
Aaron Gallagher
Aaron, that was it. You were apparently 10 seconds behind Alex on this one or I'd mark yours as the accepted answer. Thank you though, I understand what's happening.
Jay