The inevitable ‘Hello, World!’ sample. Three lines, fully explained.

The sample comes with the PyFM Tools as onegasoft/__init__.py.

Function specifications#Back to top

The function accepts a single parameter and returns back the ‘hello’ string with this parameter.

PyRun( "onegasoft"; "hello"; name )

"onegasoft"Text constant

Module name (as defined in PyFM Tools).

"hello"Text constant

Function name, case-sensitive.

nameanything, except Container

Name to insert into the ‘hello’ string; usually Text, but can be any other type as well.

Result and side effects

The function return string Hello, <name>!. The function has no side effects.

Code#Back to top

The code is straighforward:

def hello(name):
    """Return string 'Hello, <name>!'."""
    return u'Hello, %s!' % unicode(name)

If you're not familiar with Python:

  • The def file defines a function called hello that takes a single parameter: name.

  • The next string that is enclosed into triple quotes is a so-called ‘docstring,’ a tiny documentation stirng that can be attached to a module, a class, a method, or a function. The string is used in interactive help; for example, you can use it in with the console in PyFM Tools:

    >>> import onegasoft
    >>> help(onegasoft.hello)
    Help on function hello in module onegasoft:
        Return string 'Hello, <name>!'.
  • The final string that starts with return actually does the job. The u'Hellp, %s!' is a Unicode string. The %s symbol in it is a placeholder for the % operator that follows the string. The operator checks the string and replaces all placeholders with the corresponding values. In our case it will replace %s with a unicode value of the passed parameter.

Installation#Back to top

The code has already been copied into the PyFM Tools file. Open the file, go to files and find the onegasoft/__init__.py file.

To install the file from PyFM Tools press the ‘Install’ button. If everything is OK, the Status field will change to ‘Installed.’