Issue with python terminal

Hi all,

I am back with an old problem of mine (in fact, I haven’t had
the time to look into it lately, but still cannot solve it anyhow).

If I open a molecule in avogadro and execute the following python
statements in the Python Terminal:

import Avogadro
mol=Avogadro.Molecule
print “num atoms = %d” % mol.numAtoms

I get the following error message:

Traceback (most recent call last):
File “”, line 1, in
TypeError: int argument required

This is due to the fact that:

print mol.numAtoms
<property object at 0xb10d2464>

Is this the normal behavior? I was expecting “mol.numAtoms” to be an
(int) in fact but instead it seems to be an instance of a (undocumented)
“property” class. How do I deal with these objects? Or is this exposure
of a “property” (instead of a integer (or other data types)) a bug in
the python interface? Do I go ahead and report a bug?

Thank you in advance for your care.
Best regards,
Alfredo Carvalho

Hi,

On Wed, May 20, 2009 at 8:33 PM, Alfredo Palace Carvalho
ajpalace@uevora.pt wrote:

Hi all,

I am back with an old problem of mine (in fact, I haven’t had
the time to look into it lately, but still cannot solve it anyhow).

If I open a molecule in avogadro and execute the following python
statements in the Python Terminal:

import Avogadro
mol=Avogadro.Molecule

If you want to get the current molecule, use Avogadro.molecule (with a
small ‘m’). If you use Molecule, you get the class definition and not
the instance of the molecule. As a rule of thumb, classes start with
capital letters instances with lower case letters.

There is also Avogadro.molecules, which is an instance of the
MoleculeList class (python only). Use this class like:

newMol = Avogadro.molecules.addMolecule()

to create a new molecule if needed. (It makes sure the molecule
doesn’t get deleted when you assigned it to a GLWidget for example.)

Tim

print “num atoms = %d” % mol.numAtoms

I get the following error message:

Traceback (most recent call last):
File “”, line 1, in
TypeError: int argument required

This is due to the fact that:

print mol.numAtoms
<property object at 0xb10d2464>

Is this the normal behavior? I was expecting “mol.numAtoms” to be an
(int) in fact but instead it seems to be an instance of a (undocumented)
“property” class. How do I deal with these objects? Or is this exposure
of a “property” (instead of a integer (or other data types)) a bug in
the python interface? Do I go ahead and report a bug?

Thank you in advance for your care.
Best regards,
Alfredo Carvalho


Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables
unlimited royalty-free distribution of the report engine
for externally facing server and web deployment.
http://p.sf.net/sfu/businessobjects


Avogadro-devel mailing list
Avogadro-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/avogadro-devel

Hi,

Thanks for the prompt reply.
This is a problem I’ve been struggling with for quite a long time
(maybe from the switch from 0.8 versions to 0.9 versions) and
has been stoping me from running my python scripts, because, basically
I’ve been unable to do anything useful with python…

Now I am a bit confused and I realize I do not know the basics
of python scripting in Avogadro.
At first, when I retried to run those statements (in the Python
Terminal) following your instructions I was very glad to see that it worked.
I could not believe it was such a basic thing. I have reminiscences
that in the past (i.e. avogadro 0.8.x) I used Avogadro.molecule
to refer to the current molecule in scripts , but after a certain
upgrade of Avogadro my python scripts stoped working (due to severe
changes in the programming interface, i.e. the introduction
of the Extension class, etc.) and following the documentation
online and examples scripts I could never get them to work
anymore. Probably then I made this change (Avogadro.molecule
to Avogadro.Molecule) and saw fewer error messages and assumed
I was on the right path.

Anyway, now I made this change in one of my test python extension
scripts (which I include in attachment) and I am still unable
to run it. I now realize that what I still can’t (don’t know how)
to do is to refer to the current molecule inside the performAction
method. “Avogadro.molecule” works inside the Python Terminal
but not inside the “Extension” class’s performAction method.
Running my code gives the following error:

AttributeError: ‘module’ object has no attribute ‘molecule’

Could you please take a look at my script? It is fairly simple.
If I can refer to the current molecule in this script I think
I can work out the rest by myself, but I could not find any
instructions on how to do it in the documentation/examples.
In the examples I have seen, either they work by creating a new
molecule or the python code in the examples is meant for the
python Terminal but do not work (apparently) inside an extension
script. Maybe I have overlooked something in the documentation but
I feel stuck just in the beginning of my scripts!!!

Thank you very much.

Cheers,
Alfredo

On Wed, 20 May 2009, Tim Vandermeersch wrote:

Hi,

On Wed, May 20, 2009 at 8:33 PM, Alfredo Palace Carvalho
ajpalace@uevora.pt wrote:

Hi all,

I am back with an old problem of mine (in fact, I haven’t had
the time to look into it lately, but still cannot solve it anyhow).

If I open a molecule in avogadro and execute the following python
statements in the Python Terminal:

import Avogadro
mol=Avogadro.Molecule

If you want to get the current molecule, use Avogadro.molecule (with a
small ‘m’). If you use Molecule, you get the class definition and not
the instance of the molecule. As a rule of thumb, classes start with
capital letters instances with lower case letters.

There is also Avogadro.molecules, which is an instance of the
MoleculeList class (python only). Use this class like:

newMol = Avogadro.molecules.addMolecule()

to create a new molecule if needed. (It makes sure the molecule
doesn’t get deleted when you assigned it to a GLWidget for example.)

Tim

print “num atoms = %d” % mol.numAtoms

I get the following error message:

Traceback (most recent call last):
File “”, line 1, in
TypeError: int argument required

This is due to the fact that:

print mol.numAtoms
<property object at 0xb10d2464>

Is this the normal behavior? I was expecting “mol.numAtoms” to be an
(int) in fact but instead it seems to be an instance of a (undocumented)
“property” class. How do I deal with these objects? Or is this exposure
of a “property” (instead of a integer (or other data types)) a bug in
the python interface? Do I go ahead and report a bug?

Thank you in advance for your care.
Best regards,
Alfredo Carvalho


Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables
unlimited royalty-free distribution of the report engine
for externally facing server and web deployment.
http://p.sf.net/sfu/businessobjects


Avogadro-devel mailing list
Avogadro-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/avogadro-devel

On Thu, May 21, 2009 at 5:00 PM, Alfredo Palace Carvalho
ajpalace@uevora.pt wrote:

Hi,

Thanks for the prompt reply.
This is a problem I’ve been struggling with for quite a long time
(maybe from the switch from 0.8 versions to 0.9 versions) and
has been stoping me from running my python scripts, because, basically
I’ve been unable to do anything useful with python…

The Avogadro.molecule variable is only set when using the python
terminal at the

Now I am a bit confused and I realize I do not know the basics
of python scripting in Avogadro.
At first, when I retried to run those statements (in the Python
Terminal) following your instructions I was very glad to see that it worked.
I could not believe it was such a basic thing. I have reminiscences
that in the past (i.e. avogadro 0.8.x) I used Avogadro.molecule
to refer to the current molecule in scripts , but after a certain
upgrade of Avogadro my python scripts stoped working (due to severe
changes in the programming interface, i.e. the introduction
of the Extension class, etc.) and following the documentation
online and examples scripts I could never get them to work
anymore. Probably then I made this change (Avogadro.molecule
to Avogadro.Molecule) and saw fewer error messages and assumed
I was on the right path.

Yes, there have been some drastic changes but these where needed to
allow python scripts to be used as Tools, Engines (Display types) and
Extensions. Apart from some minor changes (user suggestions), the
python API will remain unchanged for 1.0.

Anyway, now I made this change in one of my test python extension
scripts (which I include in attachment) and I am still unable
to run it. I now realize that what I still can’t (don’t know how)
to do is to refer to the current molecule inside the performAction
method. “Avogadro.molecule” works inside the Python Terminal
but not inside the “Extension” class’s performAction method.
Running my code gives the following error:

AttributeError: ‘module’ object has no attribute ‘molecule’

The Avogadro.molecule variable is only set when using the python
terminal. It is provided for convenience. When performing an action in
performAction, you can get the molecule like this:

def performAction(self, action, glwidget):
mol = glwidget.molecule

However, since we have a static method to get the current GLWidget, I
could add the Avogadro.molecule directly to the python module. This
way Avogadro.molecule would also be available in standalone scripts.

Could you please take a look at my script? It is fairly simple.
If I can refer to the current molecule in this script I think
I can work out the rest by myself, but I could not find any
instructions on how to do it in the documentation/examples.
In the examples I have seen, either they work by creating a new
molecule or the python code in the examples is meant for the
python Terminal but do not work (apparently) inside an extension
script. Maybe I have overlooked something in the documentation but
I feel stuck just in the beginning of my scripts!!!

There are some examples on the wiki page
(http://avogadro.openmolecules.net/wiki/Python_Extensions). I will try
to find time to add more examples but if you have some examples you
can contribute, it would be greatly appreciated.

Thanks,
Tim

Thank you very much.

Cheers,
Alfredo

On Wed, 20 May 2009, Tim Vandermeersch wrote:

Hi,

On Wed, May 20, 2009 at 8:33 PM, Alfredo Palace Carvalho
ajpalace@uevora.pt wrote:

Hi all,

I am back with an old problem of mine (in fact, I haven’t had
the time to look into it lately, but still cannot solve it anyhow).

If I open a molecule in avogadro and execute the following python
statements in the Python Terminal:

import Avogadro
mol=Avogadro.Molecule

If you want to get the current molecule, use Avogadro.molecule (with a
small ‘m’). If you use Molecule, you get the class definition and not
the instance of the molecule. As a rule of thumb, classes start with
capital letters instances with lower case letters.

There is also Avogadro.molecules, which is an instance of the
MoleculeList class (python only). Use this class like:

newMol = Avogadro.molecules.addMolecule()

to create a new molecule if needed. (It makes sure the molecule
doesn’t get deleted when you assigned it to a GLWidget for example.)

Tim

print “num atoms = %d” % mol.numAtoms

I get the following error message:

Traceback (most recent call last):
File “”, line 1, in
TypeError: int argument required

This is due to the fact that:

print mol.numAtoms

<property object at 0xb10d2464>

Is this the normal behavior? I was expecting “mol.numAtoms” to be an
(int) in fact but instead it seems to be an instance of a (undocumented)
“property” class. How do I deal with these objects? Or is this exposure
of a “property” (instead of a integer (or other data types)) a bug in
the python interface? Do I go ahead and report a bug?

Thank you in advance for your care.
Best regards,
Alfredo Carvalho


Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables
unlimited royalty-free distribution of the report engine
for externally facing server and web deployment.
http://p.sf.net/sfu/businessobjects


Avogadro-devel mailing list
Avogadro-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/avogadro-devel

Hi,

I had to reply just to say many thanks!
I was really stuck! I am a relatively newbie working with avogadro
and am still trying to get acquainted with its “guts”. But, you
realize that without dealing with this problem there wasn’t much
I could do.

Sure, now as I browse the documentation, I see references to this
glwidget object. But, before, I never paid much attention to it
(its name sounded like something quite internal to avogadro, it
didn’t sound anything like what I was looking for). It is always
easier to look after things once you have found them … :slight_smile:
Even that protein.py example in the page you pointed at
(http://avogadro.openmolecules.net/wiki/Python_Extensions)
it was just what I was looking for! Was it always there???
Sorry for wasting your time by not looking properly at the avogadro’s
website.

Anyway, if you allow me a suggestion: it would be useful for anyone
reading the docs about python scripting to have a reference to this
glwidget right at the beginning of introductory docs. Just a single
sentence would suffice calling attention to it and explaining its role.
I assume the glwidget is essential, as it is the entry point to
any objects that you have on the screen, right? So if you are
getting started with python scripting, that should be a place
where you get started at.

I think that the introductory text on python scripting is oriented
towards the Python Terminal. It refers to the Avogadro.molecule
and not to glwidget.molecule.

As you explained, we have the Avogadro.molecule variable as a
convenience in Python Terminal but not in the python standalone
scrits. In my experience, this can be a source of confusion as
one would expect (at least I did) a uniform behavior inside the
Python Terminal and inside python scripts. It doesn’t matter
so much if you refer to something named “Avogadro.molecule” or
"glwidget.molecule" but I would expect it to be present both in
the terminal and inside a script.

On Thu, 21 May 2009, Tim Vandermeersch wrote:

Yes, there have been some drastic changes but these where needed to
allow python scripts to be used as Tools, Engines (Display types) and
Extensions. Apart from some minor changes (user suggestions), the
python API will remain unchanged for 1.0.

I didn’t mean this as a criticism, I was just pointing out the fact.
I know that these changes, although they do require some extra work
in adapting the scripts, they are meant for a greater good ultimately!

Anyway, I reiterate my thanks for your help (and sorry for the length
of this message!) and extend it to the developers of Avogadro, which
is becoming a nice software. Maybe when I get more familiar with it
I can give my contribution too.

Cheers,
Alfredo