Importing All Classes Implemented in a python package

1,194 views
Skip to first unread message

afrotypa

unread,
Oct 11, 2011, 2:02:53 PM10/11/11
to robotframework-users
In order to simplify importing libraries implemented as modules each
containing multiple classes in a package, I want to be able to have
something like this :-

1. A Package P1 in the PYTHONPATH (i.e. a directory named P1 in the
python path)
2. P1 contains modules m1.py, m2.py etc.
3. Each of the modules contains classes M1C1, M1C2,etc M2C1, M2C2
where MxCy denotes the yth class in the xth module.
4. Each of the classes implement public methods that are all keywords
i.e. each class is a RF library.

Here are the paths of the modules m1.py, m2.py and __init__.py which
makes P1 a package ..

<python_path>\P1\m1.py
<python_path>\P1\m2.py
<python_path>\P1\__init__.py

I want to be able to import all the public methods defined in all the
classes by simply importing the P1 package as a robot library.

Simply defining an __init__.py in P1 that contains the following lines
does not seem to accomplish this as I thought (none of the keywords
defined in m1 or m2 are found):-

from m1 import *
from m2 import *

Then in the test suite, to import the P1 package I used the Library
setting

Library P1

Does anyone know how this can be accomplished.?

Please get back.

Thanks,

Pekka Klärck

unread,
Oct 11, 2011, 4:30:04 PM10/11/11
to ovua...@gmail.com, robotframework-users
2011/10/11 afrotypa <ovua...@gmail.com>:

> In order to simplify importing libraries implemented as modules each
> containing multiple classes in a package, I want to be able to have
> something like this :-
>
> 1. A Package P1 in the PYTHONPATH (i.e. a directory named P1 in the
> python path)
> 2. P1 contains modules m1.py, m2.py etc.
> 3. Each of the modules contains classes M1C1, M1C2,etc M2C1, M2C2
> where MxCy denotes the yth class in the xth module.
> 4. Each of the classes implement public methods that are all keywords
> i.e. each class is a RF library.
>
> Here are the paths of the modules m1.py, m2.py and __init__.py which
> makes P1 a package ..
>
> <python_path>\P1\m1.py
> <python_path>\P1\m2.py
> <python_path>\P1\__init__.py
>
> I want to be able to import all the public methods defined in all the
> classes by simply importing the P1 package as a robot library.
>
> Simply defining an __init__.py in P1 that contains the following lines
> does not seem to accomplish this as I thought (none of the keywords
> defined in m1 or m2 are found):-
>
> from m1 import *
> from m2 import *

This imports classes M1C1, M1C2, etc.

> Then in the test suite, to import the P1 package I used the Library
> setting
>
> Library  P1

This makes module P1 a library so that all the functions in the module
are keywords. Now your keywords are in the imported classes and thus
they aren't considered keywords.

> Does anyone know how this can be accomplished.?

Simplest possibility that could work is creating class P1 that
inherits all your sub test libraries:

class P1(M1C1, M1C2, ...):
pass

Since the library has same name as the module, using `| Library | P1
|` now imports class P1 from module P1. Alternatively you could build
something more fancy using either the hybrid or dynamic library APIs
explained in the User Guide.

Cheers,
.peke
--
Agile Tester/Developer/Consultant :: http://eliga.fi
Lead Developer of Robot Framework :: http://robotframework.org

afrotypa

unread,
Oct 12, 2011, 6:58:16 AM10/12/11
to robotframework-users
Thanks for the ideas. I will look closer at the hybrid and dynamic
library apis.

On Oct 11, 4:30 pm, Pekka Klärck <p...@iki.fi> wrote:
> 2011/10/11 afrotypa <ovuaia...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages