2009/1/30 Rob Healey <robhealey1@gmail.com>
Devs:

I am having problems with the sorting ability...

as you give code, we need to execute the code to know what the problem is, it would be easier if you give a patch and/or the code with the error message you get
 

Here is my code, could someone help me figure it out... ?  It needs to be a locale.sort, so this is what was given to me already...

I am also trying to get the alphabet navigation for places as well as individuals and surnames.  These are the SurnameListPage, IndividualListPage, and PlaceListPage...


--------------------

Some comments on coding style:
 

    def alphabet_navigation(self, of, ind_list, page):

of is not a good variable name to understand this. Use short names like that only if it is needed a lot and the long variable name would therefore be annoying to type

        """
        Will create the alphabetical navigation bar...
 
in the doc string of the method, add what each input variable is. Write GOOD doc strings

        """

        def get_alpha_list(ind_list, page):
            """ Will produce the active letters in the alphabet """

submethods in methods should be avoided. Use global methods outsize of the class or private class methods like __get_alpha_list(self)


            namedict = {}

            if page is not "Places":

use enumerators for different types instead of string types. So  not "Places" but _PLACES. The calling method can then import the _PLACES var and use that to call this method. Well, I prefer it that way at least

                for person_handle in ind_list:
                    person = self.report.database.get_person_from_handle(person_handle)
                    primary_name = person.get_primary_name()

                    alpha_name = primary_name.surname

                    if alpha_name:
                        firstletter = alpha_name[0]
                        if firstletter in namedict:
                            namedict[firstletter] += 1
                        else:
                            namedict[firstletter] = 1
            else:
                for handle in handle_list:
                    place = self.report.database.get_place_from_handle(handle)
                    n = ReportUtils.place_name(self.report.database, handle)
n is too short for a variable


                    if n:
                        firstletter = normalize('NFC', n)[0].upper()
if you need normalize for places, you will need it also for persons in the above if
it is important here that the value of n is really a unicode object, and not an str or things will not work
I believe the database is unicode so it should be ok

                        if firstletter in namedict:
                            namedict[firstletter] += 1
                        else:
                            namedict[firstletter] = 1

            keyssorted = namedict.keys
Above needs to be a function, so .keys() to work

            keyssorted = keyssorted.sort(locale.strcoll)
            return keyssorted

        namedict = get_alpha_list(ind_list, page)

        of.write('\t<div id="navigation">\n')
        of.write('\t\t<ul>\n')
        for ltr in namedict:
            of.write('\t\t\t<li><a href="#%s">%s</a> |</li>' % (ltr, ltr))
        of.write('\t\t</ul>\n')
        of.write('\t</div>\n')  


I would prefer a setup with reusable methods. Without testing anything, my idea (needs polishing and debugging and doc strings):

_PLACE = 0
_PERSON = 1
_KEYNAME_METHOD = [self.get_person_keyname, self.get_place_keyname]

def get_person_keyname(self, handle):
    """ .... """ 
      person = self.report.database.get_person_from_handle(person_handle)
      return person.get_primary_name().surname

def get_place_keyname(self, handle):
     """ ... """
   place = self.report.database.get_place_from_handle(handle)
   return ReportUtils.place_name(self.report.database, handle)

def get_first_letter_dict(self, handle_list, key):
   """ key is _PLACE or _PERSON ...."""
   namedict = {}
   for handle in handle_list:
        keyname = _KEYNAME_METHOD[key](handle)
        if keyname:
                        firstletter = normalize('NFC', keyname)[0].upper()
                        if firstletter in namedict:
                            namedict[firstletter] += 1
                        else:
                            namedict[firstletter] = 1
   return namedict

def alphabet_navigation(self, of, ind_list, key):
         """
         Will create the alphabetical navigation bar...
         """
        first_letter_dict = self.get_first_letter_dict(ind_list, key)
        sorted_first_letter = first_letter_dict.keys().sort(locale.strcoll)

        of.write('\t<div id="navigation">\n')
        of.write('\t\t<ul>\n')
        for ltr in  sorted_first_letter:
            of.write('\t\t\t<li><a href="#%s">%s</a> |</li>' % (ltr, ltr))
        of.write('\t\t</ul>\n')
        of.write('\t</div>\n')  



--------------------

Sincerely Yours,
Rob G. Healey


------------------------------------------------------------------------------
This SF.net email is sponsored by:
SourcForge Community
SourceForge wants to tell your story.
http://p.sf.net/sfu/sf-spreadtheword
_______________________________________________
Gramps-devel mailing list
Gramps-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/gramps-devel