Add better __repr__ methods for QgsDateTimeRange, QgsDateRange

This commit is contained in:
Nyall Dawson 2021-03-23 14:12:06 +10:00
parent 4be88aac5d
commit 4fe99d8d6c
2 changed files with 33 additions and 0 deletions

View File

@ -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

View File

@ -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 ')'}>"