def __init__(self, db, begin_period=None, end_period=None, weekdays_rule=None, hours_per_day=8): self.db = db self.begin_period = begin_period self.end_period = end_period self.weekdays_rule = weekdays_rule self.hours_per_day = hours_per_day if not self.begin_period: self.begin_period = datetime.now() - relativedelta.relativedelta( day=31, months=1, hour=0, minute=0, second=0, weekday=rrule.FR(-2) ) + timedelta(days=1) if not self.end_period: self.end_period = datetime.now() self.real_end_period = datetime.now( ) + relativedelta.relativedelta( day=31, weekday=rrule.FR(-2) ) if(self.end_period > self.real_end_period): self.begin_period = datetime.now( ) - relativedelta.relativedelta( day=31, months=0, hour=0, minute=0, second=0, weekday=rrule.FR(-2) ) + timedelta(days=1) self.real_end_period = datetime.now( ) + relativedelta.relativedelta( day=31, months=1, weekday=rrule.FR(-2) ) if not self.weekdays_rule: self.weekdays_rule = rrule.rrule( rrule.DAILY, byweekday=( rrule.MO, rrule.TU, rrule.WE, rrule.TH, rrule.FR, ), dtstart=self.begin_period )
def lastfridays(): """Return 5 last Fridays of the month.""" return list(x.date() for x in rrule.rrule(rrule.MONTHLY, interval=1, count=10, byweekday=(rrule.FR(-1)), dtstart=datetime.datetime.now()))
def begin_period(self): return self.now - relativedelta.relativedelta( day=31, months=1, hour=0, minute=0, second=0, microsecond=0, weekday=rrule.FR(-2)) + timedelta(days=1)
def testMultipleByWeek(self): self.failUnlessParseMatches( "FREQ=MONTHLY;BYDAY=-2FR,1SU", False, # we only do a single "on the nth Xday" rule in the UI { 'freq': rrule.MONTHLY, 'interval': 1, 'byweekday': (rrule.FR(-2), rrule.SU(1)) }, {})
def testNextToLast(self): self.failUnlessParseMatches( "FREQ=MONTHLY;BYDAY=-2FR", False, # we don't do "next to last" in the UI { 'freq': rrule.MONTHLY, 'interval': 1, 'byweekday': (rrule.FR(-2), ) }, {})
def get_opex_friday(start, end): rules = [] opex_day = rrule.rrule(rrule.MONTHLY, byweekday=(rrule.FR(+3)), cache=True, dtstart=start, until=end) rules.append(opex_day) ruleset = rrule.rruleset() for rule in rules: ruleset.rrule(rule) days = ruleset.between(start, end, inc=True) return days
def __init__(self, date): self.rrule = rrule.rrule(rrule.MONTHLY, dtstart=date, byweekday=rrule.FR(+1))
def workingdays(startdate, enddate): # Business days (working days) between the start date and end date days = list(rrule.rrule(rrule.DAILY, byweekday = range(0,5), dtstart = startdate, until = enddate)) # List of company holidays, Memorial Day, Independence Day, Labor Day, Thanksgiving, Company Shutdown, MLK Birthday holidays = list(rrule.rrule(rrule.YEARLY, dtstart = startdate, until = enddate, bymonth = 5, byweekday = rrule.MO(-1))) +\ list(rrule.rrule(rrule.YEARLY, dtstart = startdate, until = enddate, bymonth = 7, bymonthday = 4)) +\ list(rrule.rrule(rrule.YEARLY, dtstart = startdate, until = enddate, bymonth = 9, byweekday = rrule.MO(1))) +\ list(rrule.rrule(rrule.YEARLY, dtstart = startdate, until = enddate, bymonth = 11, byweekday = rrule.TH(4))) +\ list(rrule.rrule(rrule.YEARLY, dtstart = startdate, until = enddate, bymonth = 11, byweekday = rrule.FR(4))) +\ list(rrule.rrule(rrule.YEARLY, dtstart = startdate, until = enddate, bymonth = 12, bymonthday = 25)) +\ list(rrule.rrule(rrule.YEARLY, dtstart = startdate, until = enddate, bymonth = 12, bymonthday = 26)) +\ list(rrule.rrule(rrule.YEARLY, dtstart = startdate, until = enddate, bymonth = 12, bymonthday = 27)) +\ list(rrule.rrule(rrule.YEARLY, dtstart = startdate, until = enddate, bymonth = 12, bymonthday = 28)) +\ list(rrule.rrule(rrule.YEARLY, dtstart = startdate, until = enddate, bymonth = 12, bymonthday = 29)) +\ list(rrule.rrule(rrule.YEARLY, dtstart = startdate, until = enddate, bymonth = 12, bymonthday = 30)) +\ list(rrule.rrule(rrule.YEARLY, dtstart = startdate, until = enddate, bymonth = 12, bymonthday = 31)) +\ list(rrule.rrule(rrule.YEARLY, dtstart = startdate, until = enddate, bymonth = 1, bymonthday = 1)) +\ list(rrule.rrule(rrule.YEARLY, dtstart = startdate, until = enddate, bymonth = 1, byweekday = rrule.MO(3))) # Total number of working days totaldays = len(set(days) - set(holidays)) return totaldays
import datetime from dateutil import rrule from random import randint import pandas as pd import numpy as np base_url = "http://www.kayak.com/flights/" city_from = "SCE" city_to = "ATL" today = datetime.date.today() dates = list(rrule.rrule(rrule.WEEKLY, count=4, byweekday=rrule.FR(1),dtstart=today)) date_format = "%Y-%m-%d" URLs = [] for date in dates: range = date.strftime(date_format) + "/" + (date + datetime.timedelta(days=2)).strftime(date_format) URLs.append(base_url + city_from + '-' + city_to + '/' + range) # Another way: # full_URL = ('https://www.kayak.com/flights/' + city_from + '-' + city_to + # '/' + date_start + '-flexible/' + date_end + '-flexible?sort=bestflight_a') for url in URLs: driver = webdriver.PhantomJS("phantomjs") # use phantomjs headless browser to fetch webpage