Skip to content

Commit

Permalink
Monkey patch __nonzero__ onto QPyNullVariant for easier null check.
Browse files Browse the repository at this point in the history
Who doesn't love monkeys :)
  • Loading branch information
NathanW2 committed Jun 11, 2013
1 parent 3115c30 commit 8392c93
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions python/__init__.py
Expand Up @@ -22,3 +22,15 @@
__copyright__ = '(C) 2007, Martin Dobias'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

from PyQt4.QtCore import QPyNullVariant
from types import MethodType

# Add a __nonzero__ method onto QPyNullVariant so we can check for null values easier.
# >>> value = QPyNullVariant("int")
# >>> if value:
# >>> print "Not a null value"
def __nonzero__(self):
return False

QPyNullVariant.__nonzero__ = MethodType(__nonzero__, None, QPyNullVariant)

1 comment on commit 8392c93

@slarosa
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Nathan,
after this commit I am unable to start up my standalone application or python script neither from IDE application nor from Python Console. Therefore even the 'Run Script' command in Python Console does not work. I always get:

Traceback (most recent call last):
  File "/home/sam/clipper.py", line 2, in 
    from qgis.core import *
  File "/usr/local/share/qgis/python/qgis/__init__.py", line 26, in 
    from PyQt4.QtCore import QPyNullVariant
ImportError: cannot import name QPyNullVariant

Do you have any hint ?

Please sign in to comment.