Matthew Francis Pyu No

Embed Size (px)

Citation preview

  • 7/26/2019 Matthew Francis Pyu No

    1/31

    Upcoming PyUNO improvements in LibreOffice 5.1

    Matthew Francis

  • 7/26/2019 Matthew Francis Pyu No

    2/31

    Introdction

    !he state of PyUNO as of LibreOffice 5."Imp#emented near the dawn of Python$ bare#y changed since

    %ery #itt#e syntactic sgar

    &enera##y a #ot #i'e writing (ava

    )#ower than it sho#d be * especia##y from a remote process

    +hat I hope to achieve with the new changes for 5.1

    Ma'e wor'ing with UNO in Python fee# more Pythonic

    , Less verbose * ma'e se of avai#ab#e Python synta-

    Ma'e it faster than before

    Persona# goa# * ma'e PyUNO more appropriate as a base tobi#d atomated UI tests on

  • 7/26/2019 Matthew Francis Pyu No

    3/31

    LibreOffice /arhs 0"15 onference Presentation

    New featres in PyUNO for 5.1

  • 7/26/2019 Matthew Francis Pyu No

    4/31

    o##ection interfaces

    Inde-ed array interfacescom22sn22star22container223Inde-/ccess

    com22sn22star22container223Inde-4ep#ace

    com22sn22star22container223Inde-ontainer

    +hats changed6

    UNO ob7ects imp#ementing these interfaces now behave#i'e Python #ists

  • 7/26/2019 Matthew Francis Pyu No

    5/31

    o##ection interfaces

    +henever yo see method ca##s #i'e the fo##owing$ there is asimp#er way to do it2

    count = obj.getCount() count = len(obj)

    Value = obj.getByIndex(0) value = obj[0]

    obj.replaceByIndex(0, value) obj[0]= value

    obj.insertByIndex(0, value)

    obj[0:0]= valueobj.reoveByIndex(0) delobj[0]

    Iteration and testing va#e presence a#so wor's2forvalue inobj: !

    ifvalue inobj: !

    ...bt the if" insynta- is probab#y sef# on#y rare#y for inde-ed co##ections

    8and not efficient9

  • 7/26/2019 Matthew Francis Pyu No

    6/31

    o##ection interfaces

    :-amp#e2 iterating over docment footnotes * the o#d waydoc = ! # load a text docuent

    count = doc.$ootnotes.getCount()

    %or i in range(count):

    %ootnote = doc.$ootnotes.getByIndex(i)

    print(%ootnote.&tring)

  • 7/26/2019 Matthew Francis Pyu No

    7/31

    :-amp#e2 iterating over docment footnotes * the new waydoc = ! # load a text docuent

    count = len(doc.$ootnotes)

    %or i in range(len):

    print(doc.$ootnotes[i].&tring)

    Or even better$ if the inde- isnt important2doc = ! # load a text docuent

    %or %ootnote indoc.$ootnotes:

    print(%ootnote.&tring)

  • 7/26/2019 Matthew Francis Pyu No

    8/31

    Other e-amp#es of 3Inde-;!e-t docment

    4ed#ines

    :ndnotes

    )preadsheetharts

    Named4anges

  • 7/26/2019 Matthew Francis Pyu No

    9/31

    o##ection interfaces

    /ssociative array interfacescom22sn22star22container223Name/ccess

    com22sn22star22container223Name4ep#ace

    com22sn22star22container223Nameontainer

    +hats changed6

    UNO ob7ects imp#ementing these interfaces now behave#i'e Python dicts

  • 7/26/2019 Matthew Francis Pyu No

    10/31

    o##ection interfaces

    +henever yo see method ca##s #i'e the fo##owing$ there is asimp#er way to do it2

    Value = obj.getBy'ae(ey) value = obj[ey]

    i% obj.asBy'ae(ey): ! i% ey inobj: !

    obj.replaceBy'ae(ey, value) obj[ey]= value

    obj.insertBy'ae(ey, value) obj[ey]= value

    obj.reoveBy'ae(ey) delobj[ey]

    Iteration and testing va#e presence a#so wor's * for 'eys2forey inobj: !

    ifey inobj: !

    =ifferent from inde-ed co##ections * the forand inoperators tests for 'eys$ not va#es

  • 7/26/2019 Matthew Francis Pyu No

    11/31

    o##ection interfaces

    :-amp#e2 Navigate e#ements of a spreadsheet * the o#d wayspr = ! # load a spreadseet

    seet = spr.&eets.getBy'ae(*&eet+*)

    range = seet.'aedanges.getBy'ae(*-yange*)

  • 7/26/2019 Matthew Francis Pyu No

    12/31

    o##ection interfaces

    :-amp#e2 Navigate e#ements of a spreadsheet * the new wayspr = ! # load a spreadseet

    seet = spr.&eets[*&eet+*]

    range = seet.'aedanges[*-yange*]

  • 7/26/2019 Matthew Francis Pyu No

    13/31

    o##ection interfaces

    +hat if an ob7ect spports both 3Inde-; and 3Name; 6>o can access it sing both obj0/and obj*'ae*/

    ?owever$ iterating yie#ds 'eys rather than va#es

    Li'e a Python dict

    :-amp#es2

    !e-t docment

    !e-t!ab#es

    :mbeddedOb7ects&raphicOb7ects

  • 7/26/2019 Matthew Francis Pyu No

    14/31

    o##ection interfaces

    :nmerationscom22sn22star22container223:nmeration/ccess

    com22sn22star22container223:nmeration

    +hats changed>o can iterate over UNO enmerations the Python way

  • 7/26/2019 Matthew Francis Pyu No

    15/31

    o##ection interfaces

    +henever yo see method ca##s #i'e the fo##owing$ there is a@ic'er way to do it2en = obj.createnueration()

    1ile en.as-oreleents():

    value = en.nextleent()

    ...

    Instead$ do2forvalue inobj:

    ...

  • 7/26/2019 Matthew Francis Pyu No

    16/31

    o##ection interfaces

    :-amp#e2 iterating over docment paragraphs * the o#d waydoc = ! # 2oad a text docuent

    en = doc.3ext.createnueration()

    1ile en.as-oreleents():

    paragrap = en.nextleent()

    print(paragrap.&tring)

  • 7/26/2019 Matthew Francis Pyu No

    17/31

    o##ection interfaces

    :-amp#e2 iterating over docment paragraphs * the new waydoc = ! # 2oad a text docuent

    forparagrap indoc.3ext:

    print(paragrap.&tring)

    Or se a Python sty#e e-p#icit iterator2doc = ! # 2oad a text docuent

    itr = iter(doc.3ext)

    paragrap = next(itr)

    print(paragrap.&tring)

    Or f#atten the te-t so it can be accessed by inde-2doc = ! # 2oad a text docuentparagraps = list(doc.3ext)

    print(paragraps0/.&tring)

    Obvios#y this can be inefficient for a #arge docment * bt e-treme#y convenient in theconte-t of e.g. a short test when there are on#y a few paragraphs

  • 7/26/2019 Matthew Francis Pyu No

    18/31

    :#imination of e-p#icit /ny

    ertain method ca##s need to be passed an /ny with a se@ence of aspecific type

    Most common#y this occrs with co##ection interfaces

    !he synta- to dea# with this in PyUNO was obscre and annoying

    :-amp#e2 creating a docment inde-doc = ! # 2oad a text docuent

    index = doc.createInstance(4co.sun.star.text.ContentIndex4)5

    uno.invoe(index.2evel6aragrap&tyles, 7

    4replaceByIndex4, (0, uno.8ny(4/string4, (*Caption*,))))

    PyUNO can now infer the type re@ired by the co##ection atomatica##yindex.2evel6aragrap&tyles0/ = (*Caption*,)

  • 7/26/2019 Matthew Francis Pyu No

    19/31

    List and iterator argments

    +herever a UNO /PI e-pects a se@ence$ a Python #ist or iterator cannow be passed.

    !his enab#es the se of #ist comprehensions and generator e-pressionsfor method ca##s and property assignments.

    :-amp#e2 Pop#ate a te-t tab#edoc = ! # 2oad a text docuent

    tbl = doc.createInstance(*co.sun.star.text.3ext3able*)

    tbl.initiali9e(+0,+0)

    doc.3ext.insert3extContent(doc.CurrentController.Vie1Cursor, tbl, $alse)

    # 8ssign nubers 0.. to te cells using a generator expression

    tbl.;ata = ((y for y in range(10*x,10*x + 10)) for x in range(10))

  • 7/26/2019 Matthew Francis Pyu No

    20/31

    List and iterator argments

  • 7/26/2019 Matthew Francis Pyu No

    21/31

    !o#erant strct initia#isation

    Initia#ising a UNO strct previos#y re@ired a## members to be set$ ornone

    :-amp#e2 Property%a#e * fre@ent#y$ on#y name and va#e are needed%ro co.sun.star.beans iport 6ropertyValue

    prop+ = 6ropertyValue()

    prop+.'ae = *%oo*

    prop+.Value = *bar*

    prop< = 6ropertyValue(*%oo*, 0, *bar*, 0)

    prop = 6ropertyValue('ae=*%oo*, >andle=0, Value=*bar*, &tate=0)

    !his re@irement is now re#a-ed when a## argments are named

    prop? = 6ropertyValue('ae=*%oo*, Value=*bar*)

  • 7/26/2019 Matthew Francis Pyu No

    22/31

    e## ranges

    / cstom behavior is app#ied to ce## range ob7ectscom22sn22star22tab#e223e##4ange

    !his is different to the other changes * the co##ection

    interfaces are generic$ this is a higher #eve# /PI?owever$ its one that is wide#y sed and co#d benefit fromsome syntactic sgar

    /pp#ies to2

    )heets in a#c spreadsheets

    +riter te-t tab#es

    )bset ce## ranges created on these

  • 7/26/2019 Matthew Francis Pyu No

    23/31

    e## ranges

    :-isting synta-cell = cellrange.getCellBy6osition(col, ro1)

    rng = cellrange.getCellangeBy6osition(le%t, top, rigt, botto)

    rng = cellrange.getCellangeBy'ae(nae)

    New synta- * access #i'e a two dimensiona# arraycell = cellrange0,0/ # 8ccess cell by indices

    rng = cellrange0,+:

  • 7/26/2019 Matthew Francis Pyu No

    24/31

    e## ranges

    Note that the indices sed are in Python/C order!hese pairs are e@iva#ent2

    # ro1 r, colun c

    cell = cellranger,c/

    cell = cellrange.getCellBy6osition(c,r)

    # ro1s t to b, coluns l to r

    rng = cellranget:b,l:r/

    rng = cellrange.getCellangeBy6osition(l,t,r@+,b@+).

  • 7/26/2019 Matthew Francis Pyu No

    25/31

    e## ranges

    Ob7ects which a#so imp#ement com22sn22star22tab#e223o#mn4ow4angespport negative indices 8fromAend inde-ing9 and the be#ow synta- forreferencing who#e rows and co#mns

    a#c spreadsheet sheets and ce## ranges created pon these spportthis interface

    +riter te-t tab#es nfortnate#y dont

    rng = cellrange0/ # 8ccess cell range by ro1 index

    rng = cellrange0,:/ # 8ccess cell range by ro1 index

    rng = cellrange:,0/ # 8ccess cell range by colun index

  • 7/26/2019 Matthew Francis Pyu No

    26/31

    Import constants by grop name

    Previos#y$ UNO constants had to be imported individa##y:-amp#e%ro co.sun.star.accessibility.8ccessibleole iport -'AB8

    %ro co.sun.star.accessibility.8ccessibleole iport ;I82D

    %ro co.sun.star.accessibility.8ccessibleole iport 6A&>BA33'

    onstant grops can now be imported as a who#e%ro co.sun.star.accessibility iport 8ccessibleole

    # 'o1 you can re%erence 8ccessibleole.-'AB8 etc.

    b7 h h bi#i

  • 7/26/2019 Matthew Francis Pyu No

    27/31

    Ob7ect hashabi#ity

    UNO ob7ects sho#d now have stab#e hash va#es

    !his a##ows them to be safe#y sed as 'eys for co##ectionss = set()

    sobj/ = +

    ...# 2ater, 1e get te sae object %ro A' again

    # 3is only 1ors i% te object as a stable as

    del sobj/

    +hats that Bsho#dC doing there6?and#e with care$ dont re#y on this if possib#e

    ases where this is sef# sho#d be rare

    P f i

  • 7/26/2019 Matthew Francis Pyu No

    28/31

    Performance improvements

    :very time a UNO ob7ect is passed to PyUNO$ we have to performintrospection on it to find ot information abot its methods andproperties

    In the case of remote 8ot of process9 PyUNO$ this means ma'ing interAprocess ca##s

    InterAprocess ca##s are s#ow$ so the fewer the better

    +e cant avoid ma'ing at #east a few ca##s

    Up to LibreOffice 5." there was a bg which meant there were p to 5"interAprocess ca##s for each ob7ect

    Predictab#y this wasnt very fast

    Frther optimisations made to e#iminate nnecessary ca##s and ma'esome others #aDy 8on#y when acta##y needed$ not for every ob7ect9

    Now its mch faster remote#y and a #itt#e faster #oca##y

    F ## t

  • 7/26/2019 Matthew Francis Pyu No

    29/31

    Fa##ot

    / ma7or aim of these changes was not to brea' e-isting code)ccessf#6 /#most

    ased an isse with LibreLogo *commit 1E1ab0acf0Ga00Ebe5a"ebHGHGHbcdfda

    Now that some PyUNO ob7ects behave #i'e proper Python co##ections$

    they have trth va#es that depend on whether or not theyre empty!he LibreLogo code sed a variab#e that was either " or a PyUNOob7ect$ and e-pected the two choices to be a#ways $alseor 3rue

    respective#y

    Mea c#pa * didnt e-pect that

    Unfortnate#y no easy way to wor' arond

  • 7/26/2019 Matthew Francis Pyu No

    30/31

    estions6

  • 7/26/2019 Matthew Francis Pyu No

    31/31

    /## te-t and image content in this docment is #icensed nder the reative ommons /ttribtionA)hare /#i'e ." License8n#ess otherwise specified9. JLibreOfficeJ and J!he =ocment FondationJ are registered trademar's. !heir respective #ogosand icons are sb7ect to internationa# copyright #aws. !he se of these therefore is sb7ect to the trademar' po#icy.

    !han' yo

    http://creativecommons.org/licenses/by-sa/3.0/http://wiki.documentfoundation.org/TradeMark_Policyhttp://wiki.documentfoundation.org/TradeMark_Policyhttp://creativecommons.org/licenses/by-sa/3.0/