Given the name of a place, how to get the handle for it

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Given the name of a place, how to get the handle for it

dbareis
Hi all,

Seems simple but I can't work it out, I have a name such as "Queensland, Australia":
  1. How can I get a place handle for it?
  2. If it didn't exist will it be created for me, if not how would I go about creating it?
Hopefully those are simple questions for the experts :-)
If not I'm still looking for it :-(

---
Thanks
Dennis


_______________________________________________
Gramps-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gramps-devel
Reply | Threaded
Open this post in threaded view
|

Re: Given the name of a place, how to get the handle for it

dbareis
Hi,

I've made a start but the code causes DB failure later on.

I found someone else's code which just had:

               self.dbstate.db.add_place(place, trans) 

I've put the txn and comment lines either side as "trans" implies I need (blue in code below). All that code works (or at least doesn't raise an exception, but there is one later on elsewhere:

2020-05-12 14:39:46.712: ERROR: grampsapp.py: line 157: Unhandled exception
Traceback (most recent call last):
  File "C:\Users\Dennis\AppData\Roaming\gramps\gramps51\plugins\Form\editform.py", line 494, in save
    self.db.add_event(self.event, trans)
  File "C:\Program Files\GrampsAIO64-5.1.2\gramps\gen\db\generic.py", line 1758, in add_event
    self.commit_event)
  File "C:\Program Files\GrampsAIO64-5.1.2\gramps\gen\db\generic.py", line 1742, in _add_base
    commit_func(obj, trans)
  File "C:\Program Files\GrampsAIO64-5.1.2\gramps\gen\db\generic.py", line 1984, in commit_event
    self._commit_base(event, EVENT_KEY, trans, change_time)
  File "C:\Program Files\GrampsAIO64-5.1.2\gramps\plugins\db\dbapi\dbapi.py", line 617, in _commit_base
    self._update_secondary_values(obj)
  File "C:\Program Files\GrampsAIO64-5.1.2\gramps\plugins\db\dbapi\dbapi.py", line 1018, in _update_secondary_values
    + [obj.handle])
  File "C:\Program Files\GrampsAIO64-5.1.2\gramps\plugins\db\dbapi\sqlite.py", line 127, in execute
    self.__cursor.execute(*args, **kwargs)
sqlite3.InterfaceError: Error binding parameter 3 - probably unsupported type.

Any help appreciated...  Code follows:

    def GetPlaceHandleFromName(self, place_name):
        """ This will create the place if it doesn't already exist """

        # Look through all handles for the wanted place name
        if  place_name == "":
            return None
        place_list = self.dbstate.db.get_place_handles()
        for place_handle in place_list:
            place = self.dbstate.db.get_place_from_handle(place_handle)
            place_title = place_displayer.display(self.dbstate.db, place)
            if place_title.strip() == place_name:
                return place

        # The place doesn't already exist, create it [TODO: Now to set TYPE=FORMS (user can manually edit those for now)
        _LOG.debug('Place not yet in DB, creating: ' + place_name)
        place = Place()
        place.set_title(place_name)
        place.set_type('CreatedByForm')
        place.set_name(PlaceName(value=place_name))


        with DbTxn(_("Form Create New Place (%s)") % place_name, self.dbstate.db) as trans:
             self.dbstate.db.add_place(place, trans)
             self.dbstate.db.commit_place(place, trans)
        return place



---
Thanks
Dennis


Mailtrack Sender notified by
Mailtrack 12/05/20, 02:55:37 pm

On Tue, 12 May 2020 at 13:37, Dennis Bareis <[hidden email]> wrote:
Hi all,

Seems simple but I can't work it out, I have a name such as "Queensland, Australia":
  1. How can I get a place handle for it?
  2. If it didn't exist will it be created for me, if not how would I go about creating it?
Hopefully those are simple questions for the experts :-)
If not I'm still looking for it :-(

---
Thanks
Dennis


_______________________________________________
Gramps-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gramps-devel
Reply | Threaded
Open this post in threaded view
|

Re: Given the name of a place, how to get the handle for it

prculley
You don't have to commit again after adding, the add code just creates a handle and gramps_id and then does the standard commit in a single step.  But I expect that this part of your code is working fine.

The backtrace indicates that there is something wrong with an Event that is being added.  The 'type' of one of the Event class attributes is not correct; you may have used a string where a class was expected, or a class object where a handle to the object was expected etc.  For example, if you were adding the Place to the event, and attached the Place directly, instead of using its handle, this would create that sort of error.

The various object classes are defined in gramps.gen.lib. The Event object scheme is defined in the method get_schema.  Since the backtrace thinks parameter 3 is wrong, and the secondary values are only those with schema types in ("string", "integer", "number", "boolean"), I think the Event.place is the likely culprit.

Hope this helps.
Paul C.

On Mon, May 11, 2020 at 11:59 PM Dennis Bareis <[hidden email]> wrote:
Hi,

I've made a start but the code causes DB failure later on.

I found someone else's code which just had:

               self.dbstate.db.add_place(place, trans) 

I've put the txn and comment lines either side as "trans" implies I need (blue in code below). All that code works (or at least doesn't raise an exception, but there is one later on elsewhere:

2020-05-12 14:39:46.712: ERROR: grampsapp.py: line 157: Unhandled exception
Traceback (most recent call last):
  File "C:\Users\Dennis\AppData\Roaming\gramps\gramps51\plugins\Form\editform.py", line 494, in save
    self.db.add_event(self.event, trans)
  File "C:\Program Files\GrampsAIO64-5.1.2\gramps\gen\db\generic.py", line 1758, in add_event
    self.commit_event)
  File "C:\Program Files\GrampsAIO64-5.1.2\gramps\gen\db\generic.py", line 1742, in _add_base
    commit_func(obj, trans)
  File "C:\Program Files\GrampsAIO64-5.1.2\gramps\gen\db\generic.py", line 1984, in commit_event
    self._commit_base(event, EVENT_KEY, trans, change_time)
  File "C:\Program Files\GrampsAIO64-5.1.2\gramps\plugins\db\dbapi\dbapi.py", line 617, in _commit_base
    self._update_secondary_values(obj)
  File "C:\Program Files\GrampsAIO64-5.1.2\gramps\plugins\db\dbapi\dbapi.py", line 1018, in _update_secondary_values
    + [obj.handle])
  File "C:\Program Files\GrampsAIO64-5.1.2\gramps\plugins\db\dbapi\sqlite.py", line 127, in execute
    self.__cursor.execute(*args, **kwargs)
sqlite3.InterfaceError: Error binding parameter 3 - probably unsupported type.

Any help appreciated...  Code follows:

    def GetPlaceHandleFromName(self, place_name):
        """ This will create the place if it doesn't already exist """

        # Look through all handles for the wanted place name
        if  place_name == "":
            return None
        place_list = self.dbstate.db.get_place_handles()
        for place_handle in place_list:
            place = self.dbstate.db.get_place_from_handle(place_handle)
            place_title = place_displayer.display(self.dbstate.db, place)
            if place_title.strip() == place_name:
                return place

        # The place doesn't already exist, create it [TODO: Now to set TYPE=FORMS (user can manually edit those for now)
        _LOG.debug('Place not yet in DB, creating: ' + place_name)
        place = Place()
        place.set_title(place_name)
        place.set_type('CreatedByForm')
        place.set_name(PlaceName(value=place_name))


        with DbTxn(_("Form Create New Place (%s)") % place_name, self.dbstate.db) as trans:
             self.dbstate.db.add_place(place, trans)
             self.dbstate.db.commit_place(place, trans)
        return place



---
Thanks
Dennis


Mailtrack Sender notified by
Mailtrack 12/05/20, 02:55:37 pm

On Tue, 12 May 2020 at 13:37, Dennis Bareis <[hidden email]> wrote:
Hi all,

Seems simple but I can't work it out, I have a name such as "Queensland, Australia":
  1. How can I get a place handle for it?
  2. If it didn't exist will it be created for me, if not how would I go about creating it?
Hopefully those are simple questions for the experts :-)
If not I'm still looking for it :-(

---
Thanks
Dennis
_______________________________________________
Gramps-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gramps-devel


_______________________________________________
Gramps-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gramps-devel
Reply | Threaded
Open this post in threaded view
|

Re: Given the name of a place, how to get the handle for it

dbareis
Hi Paul,

Thanks for the help, now that I know about ".get_schema" I have it a go on an object of unknown (to me) type:

_LOG.debug("Got Schema: " + str(SomeObject.get_schema()))
AttributeError: 'str' object has no attribute 'get_schema'

Can you do that or does it need to be on a class like this code I found:

EventType.get_schema()

As for my code I eventually figured it out, instead of calling it "GetPlaceHandleFromName", I should have called it "GetPlaceFromName".

---
Thanks
Dennis


Mailtrack Sender notified by
Mailtrack 13/05/20, 10:06:43 am

On Wed, 13 May 2020 at 00:25, Paul Culley <[hidden email]> wrote:
You don't have to commit again after adding, the add code just creates a handle and gramps_id and then does the standard commit in a single step.  But I expect that this part of your code is working fine.

The backtrace indicates that there is something wrong with an Event that is being added.  The 'type' of one of the Event class attributes is not correct; you may have used a string where a class was expected, or a class object where a handle to the object was expected etc.  For example, if you were adding the Place to the event, and attached the Place directly, instead of using its handle, this would create that sort of error.

The various object classes are defined in gramps.gen.lib. The Event object scheme is defined in the method get_schema.  Since the backtrace thinks parameter 3 is wrong, and the secondary values are only those with schema types in ("string", "integer", "number", "boolean"), I think the Event.place is the likely culprit.

Hope this helps.
Paul C.

On Mon, May 11, 2020 at 11:59 PM Dennis Bareis <[hidden email]> wrote:
Hi,

I've made a start but the code causes DB failure later on.

I found someone else's code which just had:

               self.dbstate.db.add_place(place, trans) 

I've put the txn and comment lines either side as "trans" implies I need (blue in code below). All that code works (or at least doesn't raise an exception, but there is one later on elsewhere:

2020-05-12 14:39:46.712: ERROR: grampsapp.py: line 157: Unhandled exception
Traceback (most recent call last):
  File "C:\Users\Dennis\AppData\Roaming\gramps\gramps51\plugins\Form\editform.py", line 494, in save
    self.db.add_event(self.event, trans)
  File "C:\Program Files\GrampsAIO64-5.1.2\gramps\gen\db\generic.py", line 1758, in add_event
    self.commit_event)
  File "C:\Program Files\GrampsAIO64-5.1.2\gramps\gen\db\generic.py", line 1742, in _add_base
    commit_func(obj, trans)
  File "C:\Program Files\GrampsAIO64-5.1.2\gramps\gen\db\generic.py", line 1984, in commit_event
    self._commit_base(event, EVENT_KEY, trans, change_time)
  File "C:\Program Files\GrampsAIO64-5.1.2\gramps\plugins\db\dbapi\dbapi.py", line 617, in _commit_base
    self._update_secondary_values(obj)
  File "C:\Program Files\GrampsAIO64-5.1.2\gramps\plugins\db\dbapi\dbapi.py", line 1018, in _update_secondary_values
    + [obj.handle])
  File "C:\Program Files\GrampsAIO64-5.1.2\gramps\plugins\db\dbapi\sqlite.py", line 127, in execute
    self.__cursor.execute(*args, **kwargs)
sqlite3.InterfaceError: Error binding parameter 3 - probably unsupported type.

Any help appreciated...  Code follows:

    def GetPlaceHandleFromName(self, place_name):
        """ This will create the place if it doesn't already exist """

        # Look through all handles for the wanted place name
        if  place_name == "":
            return None
        place_list = self.dbstate.db.get_place_handles()
        for place_handle in place_list:
            place = self.dbstate.db.get_place_from_handle(place_handle)
            place_title = place_displayer.display(self.dbstate.db, place)
            if place_title.strip() == place_name:
                return place

        # The place doesn't already exist, create it [TODO: Now to set TYPE=FORMS (user can manually edit those for now)
        _LOG.debug('Place not yet in DB, creating: ' + place_name)
        place = Place()
        place.set_title(place_name)
        place.set_type('CreatedByForm')
        place.set_name(PlaceName(value=place_name))


        with DbTxn(_("Form Create New Place (%s)") % place_name, self.dbstate.db) as trans:
             self.dbstate.db.add_place(place, trans)
             self.dbstate.db.commit_place(place, trans)
        return place



---
Thanks
Dennis


Mailtrack Sender notified by
Mailtrack 12/05/20, 02:55:37 pm

On Tue, 12 May 2020 at 13:37, Dennis Bareis <[hidden email]> wrote:
Hi all,

Seems simple but I can't work it out, I have a name such as "Queensland, Australia":
  1. How can I get a place handle for it?
  2. If it didn't exist will it be created for me, if not how would I go about creating it?
Hopefully those are simple questions for the experts :-)
If not I'm still looking for it :-(

---
Thanks
Dennis
<img alt="" style="display: flex;" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" width="0" height="0">
_______________________________________________
Gramps-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gramps-devel


_______________________________________________
Gramps-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gramps-devel
Reply | Threaded
Open this post in threaded view
|

Re: Given the name of a place, how to get the handle for it

dbareis
Hi,

I'm now getting a similar issue with dates, I'm using "def typename(x): return type(x).__name__" to determine the "class" of the objects involved and originally it is "Date" and what I get is "date", I assume "Date" is a gramps class and "date" is python?  I can't find the "Date" class in gramps code.

---
Thanks
Dennis


Mailtrack Sender notified by
Mailtrack 13/05/20, 10:28:42 am

On Wed, 13 May 2020 at 10:07, Dennis Bareis <[hidden email]> wrote:
Hi Paul,

Thanks for the help, now that I know about ".get_schema" I have it a go on an object of unknown (to me) type:

_LOG.debug("Got Schema: " + str(SomeObject.get_schema()))
AttributeError: 'str' object has no attribute 'get_schema'

Can you do that or does it need to be on a class like this code I found:

EventType.get_schema()

As for my code I eventually figured it out, instead of calling it "GetPlaceHandleFromName", I should have called it "GetPlaceFromName".

---
Thanks
Dennis


Mailtrack Sender notified by
Mailtrack 13/05/20, 10:06:43 am

On Wed, 13 May 2020 at 00:25, Paul Culley <[hidden email]> wrote:
You don't have to commit again after adding, the add code just creates a handle and gramps_id and then does the standard commit in a single step.  But I expect that this part of your code is working fine.

The backtrace indicates that there is something wrong with an Event that is being added.  The 'type' of one of the Event class attributes is not correct; you may have used a string where a class was expected, or a class object where a handle to the object was expected etc.  For example, if you were adding the Place to the event, and attached the Place directly, instead of using its handle, this would create that sort of error.

The various object classes are defined in gramps.gen.lib. The Event object scheme is defined in the method get_schema.  Since the backtrace thinks parameter 3 is wrong, and the secondary values are only those with schema types in ("string", "integer", "number", "boolean"), I think the Event.place is the likely culprit.

Hope this helps.
Paul C.

On Mon, May 11, 2020 at 11:59 PM Dennis Bareis <[hidden email]> wrote:
Hi,

I've made a start but the code causes DB failure later on.

I found someone else's code which just had:

               self.dbstate.db.add_place(place, trans) 

I've put the txn and comment lines either side as "trans" implies I need (blue in code below). All that code works (or at least doesn't raise an exception, but there is one later on elsewhere:

2020-05-12 14:39:46.712: ERROR: grampsapp.py: line 157: Unhandled exception
Traceback (most recent call last):
  File "C:\Users\Dennis\AppData\Roaming\gramps\gramps51\plugins\Form\editform.py", line 494, in save
    self.db.add_event(self.event, trans)
  File "C:\Program Files\GrampsAIO64-5.1.2\gramps\gen\db\generic.py", line 1758, in add_event
    self.commit_event)
  File "C:\Program Files\GrampsAIO64-5.1.2\gramps\gen\db\generic.py", line 1742, in _add_base
    commit_func(obj, trans)
  File "C:\Program Files\GrampsAIO64-5.1.2\gramps\gen\db\generic.py", line 1984, in commit_event
    self._commit_base(event, EVENT_KEY, trans, change_time)
  File "C:\Program Files\GrampsAIO64-5.1.2\gramps\plugins\db\dbapi\dbapi.py", line 617, in _commit_base
    self._update_secondary_values(obj)
  File "C:\Program Files\GrampsAIO64-5.1.2\gramps\plugins\db\dbapi\dbapi.py", line 1018, in _update_secondary_values
    + [obj.handle])
  File "C:\Program Files\GrampsAIO64-5.1.2\gramps\plugins\db\dbapi\sqlite.py", line 127, in execute
    self.__cursor.execute(*args, **kwargs)
sqlite3.InterfaceError: Error binding parameter 3 - probably unsupported type.

Any help appreciated...  Code follows:

    def GetPlaceHandleFromName(self, place_name):
        """ This will create the place if it doesn't already exist """

        # Look through all handles for the wanted place name
        if  place_name == "":
            return None
        place_list = self.dbstate.db.get_place_handles()
        for place_handle in place_list:
            place = self.dbstate.db.get_place_from_handle(place_handle)
            place_title = place_displayer.display(self.dbstate.db, place)
            if place_title.strip() == place_name:
                return place

        # The place doesn't already exist, create it [TODO: Now to set TYPE=FORMS (user can manually edit those for now)
        _LOG.debug('Place not yet in DB, creating: ' + place_name)
        place = Place()
        place.set_title(place_name)
        place.set_type('CreatedByForm')
        place.set_name(PlaceName(value=place_name))


        with DbTxn(_("Form Create New Place (%s)") % place_name, self.dbstate.db) as trans:
             self.dbstate.db.add_place(place, trans)
             self.dbstate.db.commit_place(place, trans)
        return place



---
Thanks
Dennis


Mailtrack Sender notified by
Mailtrack 12/05/20, 02:55:37 pm

On Tue, 12 May 2020 at 13:37, Dennis Bareis <[hidden email]> wrote:
Hi all,

Seems simple but I can't work it out, I have a name such as "Queensland, Australia":
  1. How can I get a place handle for it?
  2. If it didn't exist will it be created for me, if not how would I go about creating it?
Hopefully those are simple questions for the experts :-)
If not I'm still looking for it :-(

---
Thanks
Dennis
_______________________________________________
Gramps-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gramps-devel
<img width="0" height="0" alt="" style="display: flex;" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7">


_______________________________________________
Gramps-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gramps-devel