string.split returns empty when unaccessed

Greg Jorgensen gregj at pobox.com
Sat Jan 20 22:58:22 EST 2001


I can't tell for sure from the code you posted, but the "data" variable
of the D104 class is empty until the __repr__ method is invoked. I'm
assunming the getWaypoints() method just returns self.data. So if you
call getWaypoints before __repr__ is invoked the reference you get back
refers to the still-empty data variable. Once the __repr__ method is
invoked by print the data variable contains the string you want to pass
to split().

Why not just have getWaypoints return a dictionary instead?

--
Greg Jorgensen
Portland, Oregon, USA


gregj at pobox.comIn article <mailman.980047224.17889.python-
list at python.org>,
  Jonathan Pennington <john at coastalgeology.org> wrote:
> I'm having a problem spliting a string that is not accessed in some
> way before the split. I am using the pygarmin module (modified) and
> wxPython to build a GUI mapping program on a FreeBSD system with the
> Python port v2.0. The code that retrieves the list is very long and
> spans 3 modules, so it is not included here. I'm hoping that this is a
> known problem or at least understandable from this limited info, and
> the code is not needed, but I can provide it offlist if necessary.
>
> Given a string foo returned from a class instance, string.split(foo,
> 'sep') returns [''] instead of a list of string items. However, if I
> print foo, then try string.split(foo, 'sep') I *will* get a good
> list. Copying as foo = foo[:] before splitting causes the same
> behavior.
>
> What happens is:
>
> > gps = garmin(UnixSerialLink('/dev/cuaa0'))
> > w = gps.getWaypoints()
> > t = string.split(w, '|R|')
> > print t
> ['']
> > w = gps.getWaypoints()
> > w = w[:]
> > t = string.split(w,'|R|')
> > print t
> ['']
> > w = gps.getWaypoints()
> > print 'w'
> [... waypoints ...]
> > t = string.split(w,'|R|')
> > print t
> ['name:V:stuff','cmnt:V:more stuff','lat:V:number','lon:V:number'...]
>
> I can't figure out why that would happen, has anyone seen this before?
> I can't seem to find a reference to the behavior anywhere. I'm still
> new to the Python object model, but referencing it before splitting
> doesn't seem like it should change it at all.
>
> I've changed quite a bit in PyGarmin and there's a lot of code
> accessed, but the instance that returns the string is below. Thanks.
>
> --------------class follows---------------------
>
> import UserString
> class D104(Waypoint, UserString.UserString):
>    parts = Waypoint.parts + ("dst", "smbl", "dspl")
>    fmt = "<6s l l L 40s f h b"
>
>    def __init__(self, ident="", slat=0L, slon=0L, cmnt="", dst=0L,
smbl=0L, dspl=0L):
>       self.ident = ident         # text identidier (upper case)
>       self.slat = slat           # lat & long in semicircle terms
>       self.slon = slon
>       self.cmnt = cmnt           # comment (must be upper case)
>       self.unused = 0L
>       self.dst = dst                  # proximity distance (m)
>       self.smbl = smbl                   # symbol_type id
>       self.dspl = dspl                   # D104 display option
>       self.data = ""
>
>    def __repr__(self):
>       self.data = "name:V:%s|R|cmnt:V:%s|R|lat:V:%i|R|lon:V:%
i|R|dst:V:%i|R|smbl:V:%i|R|dspl:V:%i" % \
>              (self.ident, self.cmnt, self.slat, self.slon, self.dst, \
>               self.smbl, self.dspl)
>       return self.data
>
> --
> Jonathan Pennington		| http://coastalgeology.org
> Site Manager			| Protection and stewardship
> CoastalGeology.Org (CGO)	| through public education.
> john at coastalgeology.org		| Join CGO, make a difference.
>
>


Sent via Deja.com
http://www.deja.com/



More information about the Python-list mailing list