Skip to content

Commit ac4f508

Browse files
committed
create Python __repr__ methods for QgsPoint and QgsPointXY
1 parent 97726d6 commit ac4f508

File tree

9 files changed

+37
-8
lines changed

9 files changed

+37
-8
lines changed
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# The following has been generated automatically from src/core/geometry/qgspoint.h
2+
QgsPoint.__repr__ = lambda self: '<QgsPoint {}>'.format(self.asWkt())
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# The following has been generated automatically from src/core/qgspointxy.h
2+
QgsPointXY.__repr__ = lambda self: '<QgsPointXY {}>'.format(self.toString())

python/core/auto_generated/geometry/qgspoint.sip.in

+1
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ Angle undefined. Always returns 0.0
426426
virtual QgsPoint *createEmptyWithSameType() const /Factory/;
427427

428428

429+
429430
protected:
430431

431432
virtual int childCount() const;

python/core/auto_generated/qgspointxy.sip.in

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ Returns a string representation of the point (x, y) with a preset ``precision``.
122122
If ``precision`` is -1, then a default precision will be used.
123123
%End
124124

125+
125126
QString asWkt() const;
126127
%Docstring
127128
Returns the well known text representation for the point (e.g. "POINT(x y)").

scripts/sipify.pl

+13-4
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ sub detect_non_method_member{
694694
}
695695

696696
# SIP_SKIP
697-
if ( $LINE =~ m/SIP_SKIP|SIP_PYTHON_OPERATOR_/ ){
697+
if ( $LINE =~ m/SIP_SKIP|SIP_PYTHON_SPECIAL_/ ){
698698
dbg_info('SIP SKIP!');
699699
# if multiline definition, remove previous lines
700700
if ( $MULTILINE_DEFINITION != MULTILINE_NO){
@@ -711,9 +711,18 @@ sub detect_non_method_member{
711711
detect_and_remove_following_body_or_initializerlist();
712712
# line skipped, go to next iteration
713713

714-
if ($LINE =~ m/SIP_PYTHON_OPERATOR_(\w+)\(\s*(\w+)\s*\)/ ){
715-
my $pyop = "${ACTUAL_CLASS}.__" . lc($1) . "__ = lambda self: self.$2()";
716-
dbg_info("PYTHON OPERATOR $pyop");
714+
if ($LINE =~ m/SIP_PYTHON_SPECIAL_(\w+)\(\s*(".*"|\w+)\s*\)/ ){
715+
my $method_or_code = $2;
716+
dbg_info("PYTHON SPECIAL method or code: $method_or_code");
717+
my $pyop = "${ACTUAL_CLASS}.__" . lc($1) . "__ = lambda self: ";
718+
if ( $method_or_code =~ m/^"(.*)"$/ ){
719+
$pyop .= $1;
720+
}
721+
else
722+
{
723+
$pyop .= "self.${method_or_code}()";
724+
}
725+
dbg_info("PYTHON SPECIAL $pyop");
717726
if ($python_output ne ''){
718727
push @OUTPUT_PYTHON, "$pyop\n";
719728
}

src/core/geometry/qgspoint.h

+2
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,8 @@ class CORE_EXPORT QgsPoint: public QgsAbstractGeometry
495495

496496
QgsPoint *createEmptyWithSameType() const override SIP_FACTORY;
497497

498+
SIP_PYTHON_SPECIAL_REPR( "'<QgsPoint {}>'.format(self.asWkt())" )
499+
498500
protected:
499501

500502
int childCount() const override;

src/core/qgis_sip.h

+13-3
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,19 @@
198198
#define SIP_DOC_TEMPLATE
199199

200200
/*
201-
* Define the __bool__ operator using the given method
202-
* sipify.pl will take care of creating the injection in qgis/{module}/__init__.py
201+
* Define Python special method (bool, repr, etc.) using the given method or code
202+
* sipify.pl will create a dedicated python file named according to the class
203+
* and located in python/{module}/auto_additions/{classname}.py
204+
* a simple method name can be provided (e.g. isValid) and sipify will create the proper code
205+
* or some Python code can be provided:
206+
*
207+
* SIP_PYTHON_SPECIAL_BOOL( isValid )
208+
* => sipify => MyClass.__bool__ = lambda self: self.isValid()
209+
*
210+
* SIP_PYTHON_SPECIAL_REPR( "'<MyClass {}>'format(self.toString())'" )
211+
* => sipify => MyClass.__repr__ = lambda self: '<MyClass {}>'format(self.toString())'
203212
*/
204-
#define SIP_PYTHON_OPERATOR_BOOL(method)
213+
#define SIP_PYTHON_SPECIAL_BOOL(method_or_code)
214+
#define SIP_PYTHON_SPECIAL_REPR(method_or_code)
205215

206216
#endif // QGIS_SIP_H

src/core/qgsdefaultvalue.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class CORE_EXPORT QgsDefaultValue
9595
* Checks if a default value is set. Alias for isValid().
9696
* \returns false if the expression is a null string.
9797
*/
98-
operator bool() const SIP_PYTHON_OPERATOR_BOOL( isValid );
98+
operator bool() const SIP_PYTHON_SPECIAL_BOOL( isValid );
9999

100100
private:
101101
QString mExpression;

src/core/qgspointxy.h

+2
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ class CORE_EXPORT QgsPointXY
156156
*/
157157
QString toString( int precision = -1 ) const;
158158

159+
SIP_PYTHON_SPECIAL_REPR( "'<QgsPointXY {}>'.format(self.toString())" )
160+
159161
/**
160162
* Returns the well known text representation for the point (e.g. "POINT(x y)").
161163
* The wkt is created without an SRID.

0 commit comments

Comments
 (0)