Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
PyQgis: Move NULL and edit to qgis.core
  • Loading branch information
m-kuhn committed Aug 12, 2015
1 parent 35fc290 commit f647834
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 58 deletions.
58 changes: 0 additions & 58 deletions python/__init__.py
Expand Up @@ -35,64 +35,6 @@

from qgis.core import QgsFeature, QgsGeometry

try:
# Add a __nonzero__ method onto QPyNullVariant so we can check for null values easier.
# >>> value = QPyNullVariant("int")
# >>> if value:
# >>> print "Not a null value"
from types import MethodType
from PyQt4.QtCore import QPyNullVariant

def __nonzero__(self):
return False

def __repr__(self):
return 'NULL'

def __eq__(self, other):
return isinstance(other, QPyNullVariant) or other is None

def __ne__(self, other):
return not isinstance(other, QPyNullVariant) and other is not None

def __hash__(self):
return 2178309

QPyNullVariant.__nonzero__ = MethodType(__nonzero__, None, QPyNullVariant)
QPyNullVariant.__repr__ = MethodType(__repr__, None, QPyNullVariant)
QPyNullVariant.__eq__ = MethodType(__eq__, None, QPyNullVariant)
QPyNullVariant.__ne__ = MethodType(__ne__, None, QPyNullVariant)
QPyNullVariant.__hash__ = MethodType(__hash__, None, QPyNullVariant)

# define a dummy QPyNullVariant instance NULL in qgis.core
# this is mainly used to compare against
# so one can write if feat['attr'] == NULL:
from qgis import core
core.NULL = QPyNullVariant( int )
except ImportError:
pass

# Define a `with edit(layer)` statement

class edit:
def __init__(self,layer):
self.layer = layer

def __enter__(self):
assert self.layer.startEditing()
return self.layer

def __exit__(self, ex_type, ex_value, traceback):
if ex_type is None:
assert self.layer.commitChanges()
return True
else:
self.layer.rollBack()
return False

from qgis import core
core.edit = edit


def mapping_feature(feature):
geom = feature.geometry()
Expand Down
52 changes: 52 additions & 0 deletions python/core/__init__.py
Expand Up @@ -100,3 +100,55 @@ def add(values, *args):
def wrapper(func):
return register_function(func, args, group, **kwargs)
return wrapper

try:
# Add a __nonzero__ method onto QPyNullVariant so we can check for null values easier.
# >>> value = QPyNullVariant("int")
# >>> if value:
# >>> print "Not a null value"
from types import MethodType
from PyQt4.QtCore import QPyNullVariant

def __nonzero__(self):
return False

def __repr__(self):
return 'NULL'

def __eq__(self, other):
return isinstance(other, QPyNullVariant) or other is None

def __ne__(self, other):
return not isinstance(other, QPyNullVariant) and other is not None

def __hash__(self):
return 2178309

QPyNullVariant.__nonzero__ = MethodType(__nonzero__, None, QPyNullVariant)
QPyNullVariant.__repr__ = MethodType(__repr__, None, QPyNullVariant)
QPyNullVariant.__eq__ = MethodType(__eq__, None, QPyNullVariant)
QPyNullVariant.__ne__ = MethodType(__ne__, None, QPyNullVariant)
QPyNullVariant.__hash__ = MethodType(__hash__, None, QPyNullVariant)

NULL = QPyNullVariant( int )

except ImportError:
pass

# Define a `with edit(layer)` statement

class edit:
def __init__(self,layer):
self.layer = layer

def __enter__(self):
assert self.layer.startEditing()
return self.layer

def __exit__(self, ex_type, ex_value, traceback):
if ex_type is None:
assert self.layer.commitChanges()
return True
else:
self.layer.rollBack()
return False

0 comments on commit f647834

Please sign in to comment.