Pymacs, exporting python objects to elisp.

François Pinard pinard at iro.umontreal.ca
Sat Dec 14 12:54:07 EST 2002


[Syver Enstad]

> I've been using Pymacs for sometime but always used python functions from
> elisp not instances (class objects).  I need to export python instances to
> elisp with pymacs.  How do I do that?

Hello, Syver, and other Pythoneers.

One way is to have a Python function returning them.  On the Emacs side, the
function value would be some opaque object.  But you already know this:

> An example:

> class Organisation:
>    def __init__(self):
>       self._departments = []
>    def departments(self):
>        return self._departments
>    def addDeparmtent(self, aDepartment):
>        self._departments.append(aDepartment)

> class Department:
>    def __init__(self, aString):
>        self._name = aString
>    def name(self):
>        self._name

> def for_calling_from_emacs_lisp():
>     org = Organisation()    
>     dept = Department('Sales')
>     org.addDepartment(dept)
>     return org

Now, the problem:

> Let's say I want to find the department named Sales from emacs lisp.  How
> would it look:

> (setq org (pymodname-for-calling-from-emacs-lisp))
> ...Then what..?
> Python pseudo code:
> for each in org.departments():
>     if each.name() == 'Sales':
>        # do stuff

I would try something along the following lines (untested code ahead!):

   (let ((departments ((pymacs-call "getattr" org "departments"))))
     (while departments
        (let ((each (pop departments)))
           (when (string-equal ((pymacs-call "getattr" each "name")) "Sales")
              ;; do stuff
              ))))

I do not remember if in Emacs Lisp, ((...)) is usable to call a function
returned by the inner parentheses.  If not, then one may have to resort to
explicitly using Emacs Lisp `apply'.  It would be nicer if not necessary.

Note that `pymacs-call' is fairly recent.  If you run some older Pymacs, use
`pymacs-apply' instead, then using a single list for all arguments.

I once thought of adding `pymacs-*' on the Lisp side for `*' in `getattr',
`getitem', `setattr', `setitem' and maybe a few other Python primitives, but
as I do not often need these, and users never requested such features, my
feeling is that we can go without them, and without having to document them!
But such things should be fairly easy to add if they appear to be needed.

> I also thought about the possibility of maybe returning Python instances
> as instances of EIEIO classes.

Just curious, what is EIEIO?

-- 
François Pinard   http://www.iro.umontreal.ca/~pinard




More information about the Python-list mailing list