From 1c366d7c52cd1a322bb94c03d6b3fb4cc7b8300d Mon Sep 17 00:00:00 2001 From: Johannes Raggam Date: Wed, 25 Dec 2013 21:48:00 +0100 Subject: [PATCH] backport some of regebros modern python idiom changes from his regebro-refactor branch --- CHANGES.rst | 3 +++ src/icalendar/cal.py | 2 +- src/icalendar/prop.py | 8 ++++---- src/icalendar/tests/test_icalendar.py | 4 ++-- src/icalendar/tests/test_unit_cal.py | 2 +- src/icalendar/tests/test_unit_caselessdict.py | 3 +-- 6 files changed, 12 insertions(+), 10 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index beded26..f768b46 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -5,6 +5,9 @@ Changelog 4.0.dev (unreleased) -------------------- +- Backport some of Regebro's changes from his regebro-refactor branch. + [thet] + - Raise explicit error on another malformed content line case. [hajdbo] diff --git a/src/icalendar/cal.py b/src/icalendar/cal.py index 39f2ba8..dfdc2bf 100644 --- a/src/icalendar/cal.py +++ b/src/icalendar/cal.py @@ -97,7 +97,7 @@ class Component(CaselessDict): """ if not cond: return value - if type(value) in types_factory.all_types: + if isinstance(value, types_factory.all_types): # Don't encode already encoded values. return value klass = types_factory.for_property(name) diff --git a/src/icalendar/prop.py b/src/icalendar/prop.py index 5cf3967..82c9df8 100644 --- a/src/icalendar/prop.py +++ b/src/icalendar/prop.py @@ -267,7 +267,7 @@ class vDDDTypes(object): So this is practical. """ def __init__(self, dt): - if type(dt) not in (datetime, date, timedelta, time): + if not isinstance(dt, (datetime, date, timedelta, time)): raise ValueError('You must use datetime, date, timedelta or time') if isinstance(dt, datetime): self.params = Parameters(dict(value='DATE-TIME')) @@ -845,7 +845,7 @@ class TypesFactory(CaselessDict): def __init__(self, *args, **kwargs): "Set keys to upper for initial dict" CaselessDict.__init__(self, *args, **kwargs) - self.all_types = [ + self.all_types = ( vBinary, vBoolean, vCalAddress, @@ -865,8 +865,8 @@ class TypesFactory(CaselessDict): vTime, vUTCOffset, vUri, - vWeekday, - ] + vWeekday + ) self['binary'] = vBinary self['boolean'] = vBoolean self['cal-address'] = vCalAddress diff --git a/src/icalendar/tests/test_icalendar.py b/src/icalendar/tests/test_icalendar.py index 95155f9..22ebd80 100644 --- a/src/icalendar/tests/test_icalendar.py +++ b/src/icalendar/tests/test_icalendar.py @@ -154,8 +154,8 @@ class IcalendarTestCase (unittest.TestCase): self.assertEqual(name, 'ATTENDEE') self.assertEqual(vals, 'MAILTO:maxm@example.com') self.assertEqual( - list(params.items()).sort(), - [('ROLE', 'REQ-PARTICIPANT'), ('CN', 'Max Rasmussen')].sort() + sorted(params.items()), + sorted([('ROLE', 'REQ-PARTICIPANT'), ('CN', 'Max Rasmussen')]) ) # And the traditional failure diff --git a/src/icalendar/tests/test_unit_cal.py b/src/icalendar/tests/test_unit_cal.py index 70be4ee..f684b98 100644 --- a/src/icalendar/tests/test_unit_cal.py +++ b/src/icalendar/tests/test_unit_cal.py @@ -55,7 +55,7 @@ class TestCalComponent(unittest.TestCase): self.assertEqual(c.decoded('version', 'No Version'), 'No Version') c.add('rdate', [datetime(2013, 3, 28), datetime(2013, 3, 27)]) - self.assertEqual(type(c.decoded('rdate')), prop.vDDDLists) + self.assertTrue(isinstance(c.decoded('rdate'), prop.vDDDLists)) # The component can render itself in the RFC 2445 format. c = Component() diff --git a/src/icalendar/tests/test_unit_caselessdict.py b/src/icalendar/tests/test_unit_caselessdict.py index 9ca7d0c..929fa6c 100644 --- a/src/icalendar/tests/test_unit_caselessdict.py +++ b/src/icalendar/tests/test_unit_caselessdict.py @@ -86,6 +86,5 @@ class TestCaselessdict(unittest.TestCase): ncd.update({'key5': 'val5', 'KEY6': 'val6', 'KEY5': 'val7'}) self.assertEqual(ncd['key6'], 'val6') - keys = list(ncd.keys()) - keys.sort() + keys = sorted(ncd.keys()) self.assertEqual(keys, ['KEY1', 'KEY2', 'KEY3', 'KEY5', 'KEY6'])