Date: prev next · Thread: first prev next last
2015 Archives by date, by thread · List index


Hi,
I wrote some code some time ago. The code also checks the differences between two states of an object. The first part is similar to yours, it's also a recursive function, but it creates a dict and you can use a level to stop searching, so you won't get an infinite cycle.


The second part - I think, I used some code from stackoverflow - compares two nested dicts with each other and prints out the results.

It works best inside of an extension, where you can keep objects alive.

so it would be:
level = 2
self.res1 = get_attribs(object,level )

#do something
# and start the function again with:

self.res2 = get_attribs(object,level )
findDiff(self.res1, self.res2)

Regards,
Xaver


Here's the code:



ctx = uno.getComponentContext()
smgr = ctx.ServiceManager
desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
doc = desktop.getCurrentComponent()
current_Contr = doc.CurrentController
viewcursor = current_Contr.ViewCursor

object = doc
max_lvl = 3

def get_attribs(obj,lvl):
    results = {}
    for key in dir(obj):

        try:
            value = getattr(obj, key)
            if 'callable' in str(type(value)):
                continue
        except :
            #print(key)
            continue

        if key not in results:
            if type(value) in (
                               type(None),
                               type(True),
                               type(1),
                               type(.1),
                               type('string'),
                               type(()),
                               type([]),
                               type(b''),
                               type(r''),
                               type(u'')
                               ):
                results.update({key: value})

            elif lvl < max_lvl:
                try:
                    results.update({key: get_attribs(value,lvl+1)})
                except:
                    pass

    return results


diff = []

def findDiff(d1, d2, path=""):
    for k in d1.keys():
        if not d2.has_key(k):
            print path, ":"
            print k + " as key not in d2", "\n"
        else:
            if type(d1[k]) is dict:
                if path == "":
                    path = k
                else:
                    path = path + "->" + k
                findDiff(d1[k],d2[k], path)
            else:
                if d1[k] != d2[k]:
                    diff.append((path,k,d1[k],d2[k]))
                    path = ''


res1 = get_attribs(object,1)

viewcursor.gotoEnd(False)
viewcursor.setString('Test ')

res2 = get_attribs(object,1)

findDiff(res1, res2)

for d in diff:
    print(d)
    time.sleep(.4)







Am 22.08.2015 um 20:24 schrieb Piet van Oostrum:
Hi-Angel wrote:

  > Okay, I wrote a python code that explores every property, and doesn't
  > fall into an infinite cycle. It is
  > [...]
  > However it produces too much output, e.g. from a test document it
  > produced 1gb of output. I think the problem is that most elements
  > still appears in output many times — the check that in backtrace
  > wasn't the current element is ensures only that it wouldn't fall in an
  > infinite cycle.

You can get an infinite cycle at any level, so only checking at the top level is not good enough. 
You will have to check at each node.


--
To unsubscribe e-mail to: users+unsubscribe@global.libreoffice.org
Problems? http://www.libreoffice.org/get-help/mailing-lists/how-to-unsubscribe/
Posting guidelines + more: http://wiki.documentfoundation.org/Netiquette
List archive: http://listarchives.libreoffice.org/global/users/
All messages sent to this list will be publicly archived and cannot be deleted

Context


Privacy Policy | Impressum (Legal Info) | Copyright information: Unless otherwise specified, all text and images on this website are licensed under the Creative Commons Attribution-Share Alike 3.0 License. This does not include the source code of LibreOffice, which is licensed under the Mozilla Public License (MPLv2). "LibreOffice" and "The Document Foundation" are registered trademarks of their corresponding registered owners or are in actual use as trademarks in one or more countries. Their respective logos and icons are also subject to international copyright laws. Use thereof is explained in our trademark policy.