Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add better __repr__ methods for QgsDateTimeRange, QgsDateRange
  • Loading branch information
nyalldawson committed Mar 24, 2021
1 parent 4be88aa commit 4fe99d8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions python/core/__init__.py.in
Expand Up @@ -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)
Expand All @@ -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
Expand Down
30 changes: 30 additions & 0 deletions python/core/additions/ranges.py
@@ -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.