Skip to content

Commit

Permalink
Added pop_named(name) method (#320), updated README and CHANGES
Browse files Browse the repository at this point in the history
  • Loading branch information
mmontel-wgs committed Jul 6, 2020
2 parents ca1fe3c + d6121f9 commit abc1b31
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Version 0.10.3
Released ????? ??, ????

- Added get_named(substring) method to retrieve holidays by name (dr-p)
- Added pop_named(substring) method to pop specific holiday/s by name (samtregar, dr-p)
- Support for Burundi (bmwachajr)
- Support for Latvia (rolandinsh)
- Support for Romania (dorianm)
Expand Down
11 changes: 9 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,17 @@ get_list(key)
Same as `get` except returns a `list` of holiday names instead of a comma
separated string

get_named(name)
Returns a `list` of holidays matching (even partially) the provided name
(case insensitive check)

pop(key, default=None)
Same as `get` except the key is removed from the holiday object

pop_named(name)
Same as `pop` but takes the name of the holiday (or part of it) rather than
the date

update/append
Accepts dictionary of {date: name} pairs, a list of dates, or even singular
date/string/timestamp objects and adds them to the list of holidays
Expand Down Expand Up @@ -385,13 +393,12 @@ More Examples
# "Ninja Turtle Day" on July 13th. We can create a new class that inherits
# the UnitedStates class and the only method we need to override is _populate()
>>> from dateutil.relativedelta import relativedelta
>>> class CorporateHolidays(holidays.UnitedStates):
>>> def _populate(self, year):
>>> # Populate the holiday list with the default US holidays
>>> holidays.UnitedStates._populate(self, year)
>>> # Remove Columbus Day
>>> self.pop(date(year, 10, 1) + relativedelta(weekday=MO(+2)), None)
>>> self.pop_named("Columbus Day")
>>> # Add Ninja Turtle Day
>>> self[date(year, 7, 13)] = "Ninja Turtle Day"
>>> date(2014, 10, 14) in Holidays(country="US")
Expand Down
16 changes: 12 additions & 4 deletions holidays/holiday_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,23 @@ def get(self, key, default=None):
def get_list(self, key):
return [h for h in self.get(key, "").split(", ") if h]

def get_named(self, name):
# find all dates matching provided name (accepting partial
# strings too, case insensitive), returning them in a list
return [key for key in self if name.lower() in self[key].lower()]

def pop(self, key, default=None):
if default is None:
return dict.pop(self, self.__keytransform__(key))
return dict.pop(self, self.__keytransform__(key), default)

def get_named(self, name):
# find all dates matching provided name (accepting partial
# strings too, case insensitive), returning them in a list
return [key for key in self if name.lower() in self[key].lower()]
def pop_named(self, name):
to_pop = self.get_named(name)
if not to_pop:
raise KeyError(name)
for key in to_pop:
self.pop(key)
return to_pop

def __eq__(self, other):
return dict.__eq__(self, other) and self.__dict__ == other.__dict__
Expand Down
7 changes: 7 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ def test_pop(self):
self.assertNotIn(date(2014, 1, 1), self.holidays)
self.assertIn(date(2014, 7, 4), self.holidays)

def test_pop_named(self):
self.assertIn(date(2014, 1, 1), self.holidays)
self.holidays.pop_named("New Year's Day")
self.assertNotIn(date(2014, 1, 1), self.holidays)
self.assertRaises(KeyError,
lambda: self.holidays.pop_named("New Year's Dayz"))

def test_setitem(self):
self.holidays = holidays.US(years=[2014])
self.assertEqual(len(self.holidays), 10)
Expand Down

0 comments on commit abc1b31

Please sign in to comment.