Skip to content
Permalink
Browse files

Add better __repr__ methods for QgsDateTimeRange, QgsDateRange

  • Loading branch information
nyalldawson committed Mar 24, 2021
1 parent 4be88aa commit 4fe99d8d6ca8a7ab9e8a7c791d2a2f05241d4ea2
Showing with 33 additions and 0 deletions.
  1. +3 −0 python/core/__init__.py.in
  2. +30 −0 python/core/additions/ranges.py
@@ -38,6 +38,7 @@ from .additions.qgstaskwrapper import QgsTaskWrapper
from .additions.readwritecontextentercategory import ReadWriteContextEnterCategory
from .additions.runtimeprofiler import ScopedRuntimeProfileContextManager
from .additions.validitycheck import check
from .additions.ranges import datetime_range_repr, date_range_repr

# Injections into classes
QgsFeature.__geo_interface__ = property(mapping_feature)
@@ -53,6 +54,8 @@ QgsSettings.enumValue = _qgssettings_enum_value
QgsSettings.setEnumValue = _qgssettings_set_enum_value
QgsSettings.flagValue = _qgssettings_flag_value
QgsTask.fromFunction = fromFunction
QgsDateTimeRange.__repr__ = datetime_range_repr
QgsDateRange.__repr__ = date_range_repr

# Classes patched using a derived class
QgsProviderMetadata = PyProviderMetadata
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
ranges.py
---------------------
Date : Mar 2021
Copyright : (C) 2021 by Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
from qgis.PyQt.QtCore import Qt


# add some __repr__ methods to QGIS range classes. We can't do this via sip because they are template based classes


def datetime_range_repr(self):
return f"<QgsDateTimeRange:{'[' if self.includeBeginning() else '('}{self.begin().toString(Qt.ISODate)}, {self.end().toString(Qt.ISODate)}{']' if self.includeEnd() else ')'}>"


def date_range_repr(self):
return f"<QgsDateTimeRange:{'[' if self.includeBeginning() else '('}{self.begin().toString(Qt.ISODate)}, {self.end().toString(Qt.ISODate)}{']' if self.includeEnd() else ')'}>"

0 comments on commit 4fe99d8

Please sign in to comment.