Python Forum
Newbie question to find the seconds between now and midnight
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbie question to find the seconds between now and midnight
#3
Here is a quick OOP example of your market opening hours checking:

from datetime import datetime, time
import time as time_time


class MarketOpeningHours:
    """Representation of market opening hours (e.g. 13:00 - 14:00)"""

    def __init__(self, opening_time: time, closing_time: time):
        if closing_time <= opening_time:
            raise Exception("Closing time must be later than opening time!")
        self.opening_time = opening_time
        self.closing_time = closing_time

    def is_open(self) -> bool:
        return self.opening_time <= datetime.now().time() <= self.closing_time


class Market:
    """Representation of market"""

    def __init__(self):
        self.opening_hours_list = []

    def add_opening_hours(self, opening_hours: MarketOpeningHours):
        self.opening_hours_list.append(opening_hours)

    def is_open(self) -> bool:
        for opening_hours in self.opening_hours_list:
            if opening_hours.is_open():
                return True
        return False


class MarketsList:
    """Representation of markets list"""

    def __init__(self):
        self.markets = []

    def add_market(self, market: Market):
        self.markets.append(market)

    def is_any_market_open(self) -> bool:
        for market in self.markets:
            if market.is_open():
                return True
        return False


market_a = Market()
market_a.add_opening_hours(MarketOpeningHours(time(9, 0), time(11, 0)))
market_a.add_opening_hours(MarketOpeningHours(time(13, 0), time(15, 0)))

market_b = Market()
market_b.add_opening_hours(MarketOpeningHours(time(9, 0), time(11, 0)))
market_b.add_opening_hours(MarketOpeningHours(time(13, 0), time(15, 15)))

market_c = Market()
market_c.add_opening_hours(MarketOpeningHours(time(9, 0), time(10, 30)))
market_c.add_opening_hours(MarketOpeningHours(time(11, 0), time(13, 0)))
market_c.add_opening_hours(MarketOpeningHours(time(14, 0), time(15, 0)))

markets_list = MarketsList()
markets_list.add_market(market_a)
markets_list.add_market(market_b)
markets_list.add_market(market_c)

while markets_list.is_any_market_open():
    print(str(datetime.now().time()) + ": some market is still open - waiting")
    time_time.sleep(1)
Reply


Messages In This Thread
RE: Newbie question to find the seconds between now and midnight - by ODIS - Dec-12-2017, 04:14 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How is pandas modifying all rows in an assignment - python-newbie question markm74 1 767 Nov-28-2023, 10:36 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 750 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Newbie question about switching between files - Python/Pycharm Busby222 3 677 Oct-15-2023, 03:16 PM
Last Post: deanhystad
  Newbie.... run for cover. OpenCV question Stevolution2023 2 1,032 Apr-12-2023, 12:57 PM
Last Post: Stevolution2023
  numpy newbie question bcwilly_ca 4 1,247 Feb-10-2023, 05:55 PM
Last Post: jefsummers
  Problem with module time and leap seconds Pedroski55 3 1,302 Oct-07-2022, 11:27 PM
Last Post: Pedroski55
  Store variable data and display sum after 60 seconds the_dude 11 3,583 Dec-16-2021, 07:07 PM
Last Post: deanhystad
  Question from complete python's newbie Davicom 3 2,453 Jun-09-2021, 06:09 PM
Last Post: bowlofred
  Newbie question about running Python via GUI on OSX ejwjohn 8 3,649 Feb-05-2021, 03:20 PM
Last Post: Larz60+
  super newbie question: escape character tsavoSG 3 2,539 Jan-13-2021, 04:31 AM
Last Post: tsavoSG

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020