Difference between revisions of "Addon:Python Shell Gramplet"

From Gramps
Jump to: navigation, search
m (Developers: typo)
m
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:Plugins]]
+
[[Image:Pythongadget.png|thumb|right|300px|Fig. 4.8 Python Shell]]
[[Category:Developers/General]]
 
 
 
[[Image:Pythongadget.png|thumb|right|200px|Fig. 4.8 Python Shell]]
 
  
 
The Python gramplet brings up a Python Shell for interpreting python expressions.  
 
The Python gramplet brings up a Python Shell for interpreting python expressions.  
Line 8: Line 5:
 
You can type most any (single line) Python expression.  
 
You can type most any (single line) Python expression.  
  
In addition, the environment has been populated with some useful variables, including '''self''' (this Python gramplet), '''Date''', '''uistate''' and '''dbstate'''.
+
In addition, the environment has been populated with some useful variables, including '''self''' (this Python gramplet), '''Date''', '''db''', '''uistate''' and '''dbstate'''.  
 
 
Note that the name '''Date''' will be translated into your language, if a translations is available.
 
  
 
The '''Date''' entry is a date object constructor, and can be used for date arithmetic. For example, you might be interested in questions like:
 
The '''Date''' entry is a date object constructor, and can be used for date arithmetic. For example, you might be interested in questions like:
Line 34: Line 29:
 
  1980-01-26
 
  1980-01-26
  
You can also use the same formats as you use in data entry:
+
You can convert one date into another calendar, which returns a new date object:
 
 
> Date("Jan 15, 1962")
 
1962-15-01
 
> Date("15 Jan, 1962")
 
1962-15-01
 
> Date("1962-15-01")
 
1962-15-01
 
 
 
There are two ways to use different calendars:
 
 
 
> Date("Jan 15, 1532 (Julian)")
 
1532-15-01 (Julian)
 
> Date(1671, 12, 31, calendar="julian")
 
1671-12-31
 
 
 
and a method to convert one date into another calendar, which returns a new date object:
 
  
 
  > Date(1703, 6, 1).to_calendar("hebrew")
 
  > Date(1703, 6, 1).to_calendar("hebrew")
Line 76: Line 55:
 
You can use the Python Shell to interact with people from your database and test GRAMPS functions:
 
You can use the Python Shell to interact with people from your database and test GRAMPS functions:
  
  > person = dbstate.db.get_person_from_gramps_id("I01284")
+
  > person = db.get_person_from_gramps_id("I01284")
 
  > from Utils import probably_alive
 
  > from Utils import probably_alive
  > probably_alive(person, dbstate.db, Date("July 4, 1776"))
+
  > probably_alive(person, db, Date(1776, 7, 4))
  
 
You can also interact with the GUI:
 
You can also interact with the GUI:
Line 103: Line 82:
 
You can download this plugin by following the directions at [[Third-party Plugins]].
 
You can download this plugin by following the directions at [[Third-party Plugins]].
  
<br clear="all"/>
+
[[Category:Plugins]]
 +
[[Category:Developers/General]]
 +
[[Category:Gramplets]]

Revision as of 10:07, 3 March 2012

Fig. 4.8 Python Shell

The Python gramplet brings up a Python Shell for interpreting python expressions.

You can type most any (single line) Python expression.

In addition, the environment has been populated with some useful variables, including self (this Python gramplet), Date, db, uistate and dbstate.

The Date entry is a date object constructor, and can be used for date arithmetic. For example, you might be interested in questions like:

What was the date 56 years before a given date:

> Date(2007, 12, 31) - 56
1951-12-31

How old was someone on Sept 3, 1955 who was born on June 7, 1922:

> Date(1955, 9, 3) - Date(1922, 5, 7)
(33, 3, 27)

(About 33 years, 3 months, and 27 days). When did they turn 21 years old?

> Date(1922, 5, 7) + 21
1943-05-07

You can also add years, months, and days:

> Date(1980) + (0, 0, 25)
1980-01-26

You can convert one date into another calendar, which returns a new date object:

> Date(1703, 6, 1).to_calendar("hebrew")
5463-10-17 (Hebrew)

Developers

Another use for this Gramplet is for debugging. This gramplet makes a nice interface to the running GRAMPS system. You can inspect, and alter the system by entering Python commands. As a simple example, you can:

> self.clear_text()     # clear the text in this window
> self.set_wrap(False)  # turn word wrap off
> self.set_wrap(True)   # turn word wrap on

The Python Gramplet also has the Python "garbage collector" preloaded with the same flags as the Debug Tool. To use:

> gc.collect()
23
> gc.garbage[0]
<cell at 0x9f9089c: function object at 0x9f89dbc>
> gc.get_referents(self)
[...]
> gc.get_referrers(self)
[...]

You can use the Python Shell to interact with people from your database and test GRAMPS functions:

> person = db.get_person_from_gramps_id("I01284")
> from Utils import probably_alive
> probably_alive(person, db, Date(1776, 7, 4))

You can also interact with the GUI:

> uistate.viewmanager.pages
[<DataViews.GrampletView.GrampletView instance at 0xa0bd0ac>, 
 <DataViews.PersonView.PersonView instance at 0xa8f542c>, 
 <DataViews.RelationView.RelationshipView instance at 0xa8f562c>, 
 <DataViews.FamilyList.FamilyListView instance at 0xa8f5f8c>, 
 <DataViews.PedigreeView.PedigreeView instance at 0xa8fc5cc>, 
 <DataViews.EventView.EventView instance at 0xa8fc88c>, 
 <DataViews.SourceView.SourceView instance at 0xa8fcdcc>, 
 <DataViews.PlaceView.PlaceView instance at 0xa9070ec>, 
 <DataViews.MediaView.MediaView instance at 0xa9074ac>, 
 <DataViews.RepositoryView.RepositoryView instance at 0xa9077ac>, 
 <DataViews.NoteView.NoteView instance at 0xa907d8c>, 
 <DataViews.GeoView.GeoView instance at 0xa90d0cc>]
> uistate.viewmanager.pages[0]
<DataViews.GrampletView.GrampletView instance at 0xa0bd0ac>
> uistate.viewmanager.pages[0].columns[0].get_children()[0].get_children()[0]

This returns the Gtk Frame of the first Gramplet in the first column.

You can download this plugin by following the directions at Third-party Plugins.