Skip to content

Commit

Permalink
create Python __repr__ methods for QgsPoint and QgsPointXY
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Jun 19, 2018
1 parent 97726d6 commit ac4f508
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 8 deletions.
2 changes: 2 additions & 0 deletions python/core/auto_additions/qgspoint.py
@@ -0,0 +1,2 @@
# The following has been generated automatically from src/core/geometry/qgspoint.h
QgsPoint.__repr__ = lambda self: '<QgsPoint {}>'.format(self.asWkt())
2 changes: 2 additions & 0 deletions python/core/auto_additions/qgspointxy.py
@@ -0,0 +1,2 @@
# The following has been generated automatically from src/core/qgspointxy.h
QgsPointXY.__repr__ = lambda self: '<QgsPointXY {}>'.format(self.toString())
1 change: 1 addition & 0 deletions python/core/auto_generated/geometry/qgspoint.sip.in
Expand Up @@ -426,6 +426,7 @@ Angle undefined. Always returns 0.0
virtual QgsPoint *createEmptyWithSameType() const /Factory/; virtual QgsPoint *createEmptyWithSameType() const /Factory/;





protected: protected:


virtual int childCount() const; virtual int childCount() const;
Expand Down
1 change: 1 addition & 0 deletions python/core/auto_generated/qgspointxy.sip.in
Expand Up @@ -122,6 +122,7 @@ Returns a string representation of the point (x, y) with a preset ``precision``.
If ``precision`` is -1, then a default precision will be used. If ``precision`` is -1, then a default precision will be used.
%End %End



QString asWkt() const; QString asWkt() const;
%Docstring %Docstring
Returns the well known text representation for the point (e.g. "POINT(x y)"). Returns the well known text representation for the point (e.g. "POINT(x y)").
Expand Down
17 changes: 13 additions & 4 deletions scripts/sipify.pl
Expand Up @@ -694,7 +694,7 @@ sub detect_non_method_member{
} }


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


if ($LINE =~ m/SIP_PYTHON_OPERATOR_(\w+)\(\s*(\w+)\s*\)/ ){ if ($LINE =~ m/SIP_PYTHON_SPECIAL_(\w+)\(\s*(".*"|\w+)\s*\)/ ){
my $pyop = "${ACTUAL_CLASS}.__" . lc($1) . "__ = lambda self: self.$2()"; my $method_or_code = $2;
dbg_info("PYTHON OPERATOR $pyop"); dbg_info("PYTHON SPECIAL method or code: $method_or_code");
my $pyop = "${ACTUAL_CLASS}.__" . lc($1) . "__ = lambda self: ";
if ( $method_or_code =~ m/^"(.*)"$/ ){
$pyop .= $1;
}
else
{
$pyop .= "self.${method_or_code}()";
}
dbg_info("PYTHON SPECIAL $pyop");
if ($python_output ne ''){ if ($python_output ne ''){
push @OUTPUT_PYTHON, "$pyop\n"; push @OUTPUT_PYTHON, "$pyop\n";
} }
Expand Down
2 changes: 2 additions & 0 deletions src/core/geometry/qgspoint.h
Expand Up @@ -495,6 +495,8 @@ class CORE_EXPORT QgsPoint: public QgsAbstractGeometry


QgsPoint *createEmptyWithSameType() const override SIP_FACTORY; QgsPoint *createEmptyWithSameType() const override SIP_FACTORY;


SIP_PYTHON_SPECIAL_REPR( "'<QgsPoint {}>'.format(self.asWkt())" )

protected: protected:


int childCount() const override; int childCount() const override;
Expand Down
16 changes: 13 additions & 3 deletions src/core/qgis_sip.h
Expand Up @@ -198,9 +198,19 @@
#define SIP_DOC_TEMPLATE #define SIP_DOC_TEMPLATE


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


#endif // QGIS_SIP_H #endif // QGIS_SIP_H
2 changes: 1 addition & 1 deletion src/core/qgsdefaultvalue.h
Expand Up @@ -95,7 +95,7 @@ class CORE_EXPORT QgsDefaultValue
* Checks if a default value is set. Alias for isValid(). * Checks if a default value is set. Alias for isValid().
* \returns false if the expression is a null string. * \returns false if the expression is a null string.
*/ */
operator bool() const SIP_PYTHON_OPERATOR_BOOL( isValid ); operator bool() const SIP_PYTHON_SPECIAL_BOOL( isValid );


private: private:
QString mExpression; QString mExpression;
Expand Down
2 changes: 2 additions & 0 deletions src/core/qgspointxy.h
Expand Up @@ -156,6 +156,8 @@ class CORE_EXPORT QgsPointXY
*/ */
QString toString( int precision = -1 ) const; QString toString( int precision = -1 ) const;


SIP_PYTHON_SPECIAL_REPR( "'<QgsPointXY {}>'.format(self.toString())" )

/** /**
* Returns the well known text representation for the point (e.g. "POINT(x y)"). * Returns the well known text representation for the point (e.g. "POINT(x y)").
* The wkt is created without an SRID. * The wkt is created without an SRID.
Expand Down

0 comments on commit ac4f508

Please sign in to comment.