NiceLabel.Blog Everything you wanted to know about NiceLabel

20Jan/11Off

Working with Modules in Python Scripts in NiceForm and NiceLabel Portal

A module is a Python source file (a text file) whose name ends in .py. Objects (names) defined in a module can be imported and used elsewhere.

The import statement has several different forms:

import module
from module import name1, name2
from module import name as anotherName
from module import *

 

If you use the first form, you receive a reference to the module object. If a module defines a class SomeClass then you can access it using module.SomeClass. If you need access to only a few objects from the module, you can use the second form. It imports only the names you have specified from the module. If a name you wish to import would clash with a name in your current namespace, you can use the third form. The fourth form imports all the names from the module into your namespace. In Python this is something you would not normally do.

Exploring Modules

To find out what a module contains you can use the dir() function, which lists all the attributes of an object and therefore all functions, classes, variables of a module. Several of these names begin with an underscore which means that they are not meant to be used outside the module.

For example:

import math
print dir(math)

 

would return the following:

['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']

Very useful command that can help you while exploring modules, classes, variables and methods is help(). help() is a built-in function that provides information like what method does and what arguments that method takes.

import math
help(math.sin)

 

would return:

sin(...)
    sin(x)

    Return the sine of x (measured in radians).

Finding modules

To import a module, the Python interpreter needs to find it. With a module, the Python interpreter first looks for a file named module.py, where module is the name of the module you pass to the import statement. The Python interpreter looks for modules in the directories that are part of the module search path. These directories are listed in the sys.path variable from the sys module.

import sys
print sys.path

 

There are some differences between NiceForm and NiceLabel Portal here. In NiceForm you would get something like this:

['C:\Windows\system32\python27.zip','C:\Ln\Python27\Lib', 'C:\Ln\Python27\DLLs', 'C:\Ln\Python27\Lib\lib-tk','C:\PROGRA~2\EuroPlus\NICELA~1\bin','C:\Ln\Python27', 'C:\Ln\Python27\lib\site-packages', 'C:\Ln\Python27\lib\site-packages\win32', 'C:\Ln\Python27\lib\site-packages\win32\lib', 'C:\Ln\Python27\lib\site-packages\Pythonwin']

while in NiceLabel Portal sys.path would return just ‘.’. The reason for this is that NiceLabel Portal runs in the web browser and does not have access to the local files and directories. As a consequence in NiceLabel Portal you can use only built-in modules. You have on other hand have access to a rich functionality of .NET Framework Class Library for Silverlight.

Comments (0) Trackbacks (0)

Sorry, the comment form is closed at this time.

Trackbacks are disabled.