Hello everybody! I'm stuck in the effort to access some data fields in a LibreOffice Base form from a Python macro. I did only find a way to access the current document, but that's all. Armed with that, and hoping to find some field names, I wrote a small routine to inspect the types of all elements in that document, and I stumbled in a strange behaviour in the exec() function: it seems that exec() only works if called from the interactive interpreter, while it doesn't if called inside a program. Why? (by the way, if somebody knows how to read currently displayed data from fields in a LibreOffice Base form and put it in some other field on the same form, using a Python macro, I would be very happy...)
Francesco Here's my code: (Python 3.5.3 on Linux) import inspect, collections def esplora(xModel): i = 0 questi = [] for x in dir(xModel): qqq = "Non Modif" what = "Nothing" try: what = "Attr?" exec("qqq = inspect.getmembers(xModel.%s)" % x) what = "Attr!!!" if qqq == "Non Modif": try: what = "Type?" exec("qqq = type(xModel.%s)" % x) what = "Type!!!" except: what = "Except2" except: what = "Except1" questi.append(str((x, qqq, what))) i += 1 questi.append(("Totale elementi: %s" % i, )) print("\n".join(questi)) Example data: >>> class prova(object): ... def __init__(self): ... self.abc = "ABC" ... self.num = 123 ... self.tup = ("abc", 321) ... >>> lui = prova() Applied the function to the sample data: incorrect behaviour >>> esplora(lui) ('__class__', 'Non Modif', 'Type!!!') ('__delattr__', 'Non Modif', 'Type!!!') ('__dict__', 'Non Modif', 'Type!!!') ('__dir__', 'Non Modif', 'Type!!!') ('__doc__', 'Non Modif', 'Type!!!') ('__eq__', 'Non Modif', 'Type!!!') ('__format__', 'Non Modif', 'Type!!!') ('__ge__', 'Non Modif', 'Type!!!') ('__getattribute__', 'Non Modif', 'Type!!!') ('__gt__', 'Non Modif', 'Type!!!') ('__hash__', 'Non Modif', 'Type!!!') ('__init__', 'Non Modif', 'Type!!!') ('__le__', 'Non Modif', 'Type!!!') ('__lt__', 'Non Modif', 'Type!!!') ('__module__', 'Non Modif', 'Type!!!') ('__ne__', 'Non Modif', 'Type!!!') ('__new__', 'Non Modif', 'Type!!!') ('__reduce__', 'Non Modif', 'Type!!!') ('__reduce_ex__', 'Non Modif', 'Type!!!') ('__repr__', 'Non Modif', 'Type!!!') ('__setattr__', 'Non Modif', 'Type!!!') ('__sizeof__', 'Non Modif', 'Type!!!') ('__str__', 'Non Modif', 'Type!!!') ('__subclasshook__', 'Non Modif', 'Type!!!') ('__weakref__', 'Non Modif', 'Type!!!') ('abc', 'Non Modif', 'Type!!!') ('num', 'Non Modif', 'Type!!!') ('tup', 'Non Modif', 'Type!!!') Totale elementi: 28 >>> type(lui.tup) <class 'tuple'> >>> dir(lui) ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'abc', 'num', 'tup'] >>> Same thing called directly: correct behaviour >>> for x in dir(lui): ... exec("y = type(lui.%s)" % x) ... print((x, y)) ... ('__class__', <class 'type'>) ('__delattr__', <class 'method-wrapper'>) ('__dict__', <class 'dict'>) ('__dir__', <class 'builtin_function_or_method'>) ('__doc__', <class 'NoneType'>) ('__eq__', <class 'method-wrapper'>) ('__format__', <class 'builtin_function_or_method'>) ('__ge__', <class 'method-wrapper'>) ('__getattribute__', <class 'method-wrapper'>) ('__gt__', <class 'method-wrapper'>) ('__hash__', <class 'method-wrapper'>) ('__init__', <class 'method'>) ('__le__', <class 'method-wrapper'>) ('__lt__', <class 'method-wrapper'>) ('__module__', <class 'str'>) ('__ne__', <class 'method-wrapper'>) ('__new__', <class 'builtin_function_or_method'>) ('__reduce__', <class 'builtin_function_or_method'>) ('__reduce_ex__', <class 'builtin_function_or_method'>) ('__repr__', <class 'method-wrapper'>) ('__setattr__', <class 'method-wrapper'>) ('__sizeof__', <class 'builtin_function_or_method'>) ('__str__', <class 'method-wrapper'>) ('__subclasshook__', <class 'builtin_function_or_method'>) ('__weakref__', <class 'NoneType'>) ('abc', <class 'str'>) ('num', <class 'int'>) ('tup', <class 'tuple'>) >>> _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor