Skip to content

Commit f647834

Browse files
committed
PyQgis: Move NULL and edit to qgis.core
1 parent 35fc290 commit f647834

File tree

2 files changed

+52
-58
lines changed

2 files changed

+52
-58
lines changed

python/__init__.py

-58
Original file line numberDiff line numberDiff line change
@@ -35,64 +35,6 @@
3535

3636
from qgis.core import QgsFeature, QgsGeometry
3737

38-
try:
39-
# Add a __nonzero__ method onto QPyNullVariant so we can check for null values easier.
40-
# >>> value = QPyNullVariant("int")
41-
# >>> if value:
42-
# >>> print "Not a null value"
43-
from types import MethodType
44-
from PyQt4.QtCore import QPyNullVariant
45-
46-
def __nonzero__(self):
47-
return False
48-
49-
def __repr__(self):
50-
return 'NULL'
51-
52-
def __eq__(self, other):
53-
return isinstance(other, QPyNullVariant) or other is None
54-
55-
def __ne__(self, other):
56-
return not isinstance(other, QPyNullVariant) and other is not None
57-
58-
def __hash__(self):
59-
return 2178309
60-
61-
QPyNullVariant.__nonzero__ = MethodType(__nonzero__, None, QPyNullVariant)
62-
QPyNullVariant.__repr__ = MethodType(__repr__, None, QPyNullVariant)
63-
QPyNullVariant.__eq__ = MethodType(__eq__, None, QPyNullVariant)
64-
QPyNullVariant.__ne__ = MethodType(__ne__, None, QPyNullVariant)
65-
QPyNullVariant.__hash__ = MethodType(__hash__, None, QPyNullVariant)
66-
67-
# define a dummy QPyNullVariant instance NULL in qgis.core
68-
# this is mainly used to compare against
69-
# so one can write if feat['attr'] == NULL:
70-
from qgis import core
71-
core.NULL = QPyNullVariant( int )
72-
except ImportError:
73-
pass
74-
75-
# Define a `with edit(layer)` statement
76-
77-
class edit:
78-
def __init__(self,layer):
79-
self.layer = layer
80-
81-
def __enter__(self):
82-
assert self.layer.startEditing()
83-
return self.layer
84-
85-
def __exit__(self, ex_type, ex_value, traceback):
86-
if ex_type is None:
87-
assert self.layer.commitChanges()
88-
return True
89-
else:
90-
self.layer.rollBack()
91-
return False
92-
93-
from qgis import core
94-
core.edit = edit
95-
9638

9739
def mapping_feature(feature):
9840
geom = feature.geometry()

python/core/__init__.py

+52
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,55 @@ def add(values, *args):
100100
def wrapper(func):
101101
return register_function(func, args, group, **kwargs)
102102
return wrapper
103+
104+
try:
105+
# Add a __nonzero__ method onto QPyNullVariant so we can check for null values easier.
106+
# >>> value = QPyNullVariant("int")
107+
# >>> if value:
108+
# >>> print "Not a null value"
109+
from types import MethodType
110+
from PyQt4.QtCore import QPyNullVariant
111+
112+
def __nonzero__(self):
113+
return False
114+
115+
def __repr__(self):
116+
return 'NULL'
117+
118+
def __eq__(self, other):
119+
return isinstance(other, QPyNullVariant) or other is None
120+
121+
def __ne__(self, other):
122+
return not isinstance(other, QPyNullVariant) and other is not None
123+
124+
def __hash__(self):
125+
return 2178309
126+
127+
QPyNullVariant.__nonzero__ = MethodType(__nonzero__, None, QPyNullVariant)
128+
QPyNullVariant.__repr__ = MethodType(__repr__, None, QPyNullVariant)
129+
QPyNullVariant.__eq__ = MethodType(__eq__, None, QPyNullVariant)
130+
QPyNullVariant.__ne__ = MethodType(__ne__, None, QPyNullVariant)
131+
QPyNullVariant.__hash__ = MethodType(__hash__, None, QPyNullVariant)
132+
133+
NULL = QPyNullVariant( int )
134+
135+
except ImportError:
136+
pass
137+
138+
# Define a `with edit(layer)` statement
139+
140+
class edit:
141+
def __init__(self,layer):
142+
self.layer = layer
143+
144+
def __enter__(self):
145+
assert self.layer.startEditing()
146+
return self.layer
147+
148+
def __exit__(self, ex_type, ex_value, traceback):
149+
if ex_type is None:
150+
assert self.layer.commitChanges()
151+
return True
152+
else:
153+
self.layer.rollBack()
154+
return False

0 commit comments

Comments
 (0)