Tips and Tricks¶
The following section lists a series of tips and tricks I found useful in my experience.
This includes:
- Solutions to language problems
- Tips for using the
wsadmin
NameError
with Boolean values¶
Being Python 2.7 the earliest Python version i was used to work on, i was shocked to find that simple scripts running on Python 2.1 failed with NameError: True
and NameError: False
.
Upon investigation I found that:
> 3.0
:True
andFalse
are reserved keywords (and so cannot be defined) and in Python;>= 2.3
:True
andFalse
are defined simply as constants, so they could even be assigned arbitrary values;< 2.3
:True
andFalse
are NOT defined at all, hence theNameError
exception.
Info
More informations on this subject and other interesting considerations can be found in this article.
Solutions¶
Since boolean values were introduced in Python 2.3 (with PEP 285), if the wsadmin
is running an older Jython version, you won't be able to use True
or False
(nor the bool(...)
function) in your scripts.
To overcome this problem you have 2 options:
Let's explore these options in detail...
1. Define True
/False
¶
To use True
and False
in the scripts add the following line at the top of your code, then you'll be able to write your scripts as usual:
exec("try: (True, False)\nexcept NameError: exec('True = 1==1; False = 1==0')")
Info
The code is wrapped in an exec(...)
function for two primary reasons:
- To provide a oneliner fix, easier to copy-paste, thus reducing errors;
- To elude linters and static type checkers like
pylint
ormypy
which consider this an error (while it is only a false positive, since we're dealing with old legacy Python versions). - The inner
exec
call serves the sole purpose of safe guarding in case the script is being executed (while testing) on Python 3.x (theSyntaxError
raised when trying to assign a keyword cannot be catched).
Source
This workaround is inspired by this SO answer.
Example
The following example shows how the False
value gets correctly evaluated in a condition:
>>> exec("try: (True, False)\nexcept NameError: exec('True = 1==1; False = 1==0')")
>>> exists = True
>>> if exists:
... print("File exists.")
... else:
... print("File does not exists.")
File exists.
2. Use 0
/1
¶
Python interprets 0
and 1
respectively as falsy and truthy values.
This means that we can use 0
instead of False
and 1
instead of True
.
Example
We can rewrite the previous example like this:
>>> exists = 1
>>> if exists:
... print("File exists.")
... else:
... print("File does not exists.")
File exists.
The __future__
module¶
Just like Python, Jython also provides the __future__
module.
This built-in module was added in Python 2.1 and allows the developer to force the Python (and so, Jython) interpreters into using newer features of the language.
Warning
The list of features the module exposes depends on the version of Jython your DMGR is running, so always make sure that the feature you're trying to import is actually present in the __future__
module.
A full list of features available, with all their descriptions and version prerequisites can be found here.
-
To discover the features available in your Jython environment use the following commands:
>>> import __future__ >>> print([feature for feature in dir(__future__) if not feature.startswith("_")]) ['nested_scopes']
-
Then, if you intend to use them, just
import
them like a regular module:>>> from __future__ import nested_scopes
Info
In this case I'm using the
nested_scopes
feature, which backports PEP 227 (introduced in Python 2.2) down to Python 2.1.