Exemple #1
0
def extract_attrs(data) -> dict:
    """
    Peak = 08:00 to 20:00
    Off peak 1 = 00:00 to 08:00
    off peak 2 = 20:00 to 00:00
    """
    items = []
    d = defaultdict(list)
    tzn = pendulum.now().timezone_name
    for item in data:
        curr = pendulum.instance(item.get("start")).in_timezone(tzn)
        peak = pendulum.period(curr.at(8), curr.at(19).end_of("hour"))
        offpeek1 = pendulum.period(curr.start_of("day"), curr.at(8))
        offpeek2 = pendulum.period(curr.at(20), curr.at(23).end_of("hour"))

        if curr in peak:
            d["peak"].append(item.get("value"))
        elif curr in offpeek1:
            d["offpeek1"].append(item.get("value"))
        elif curr in offpeek2:
            d["offpeek2"].append(item.get("value"))

        items.append(item.get("value"))

    d["Peak"] = mean(d["peak"])
    d["Off-peak 1"] = mean(d["offpeek1"])
    d["Off-peak 2"] = mean(d["offpeek2"])
    d["Average"] = mean(items)
    d["Min"] = min(items)
    d["Max"] = max(items)

    return dict(d)
Exemple #2
0
def test_pickle():
    dt1 = pendulum.datetime(2016, 11, 18)
    dt2 = pendulum.datetime(2016, 11, 20)

    p = pendulum.period(dt1, dt2)
    s = pickle.dumps(p)
    p2 = pickle.loads(s)

    assert p.start == p2.start
    assert p.end == p2.end
    assert p.invert == p2.invert

    p = pendulum.period(dt2, dt1)
    s = pickle.dumps(p)
    p2 = pickle.loads(s)

    assert p.start == p2.start
    assert p.end == p2.end
    assert p.invert == p2.invert

    p = pendulum.period(dt2, dt1, True)
    s = pickle.dumps(p)
    p2 = pickle.loads(s)

    assert p.start == p2.start
    assert p.end == p2.end
    assert p.invert == p2.invert
Exemple #3
0
    def test_pickle(self):
        dt1 = pendulum.create(2016, 11, 18)
        dt2 = pendulum.create(2016, 11, 20)

        p = pendulum.period(dt1, dt2)
        s = pickle.dumps(p)
        p2 = pickle.loads(s)

        self.assertEqual(p.start, p2.start)
        self.assertEqual(p.end, p2.end)
        self.assertEqual(p.invert, p2.invert)

        p = pendulum.period(dt2, dt1)
        s = pickle.dumps(p)
        p2 = pickle.loads(s)

        self.assertEqual(p.start, p2.start)
        self.assertEqual(p.end, p2.end)
        self.assertEqual(p.invert, p2.invert)

        p = pendulum.period(dt2, dt1, True)
        s = pickle.dumps(p)
        p2 = pickle.loads(s)

        self.assertEqual(p.start, p2.start)
        self.assertEqual(p.end, p2.end)
        self.assertEqual(p.invert, p2.invert)
Exemple #4
0
def test_intersect_included():
    start = pendulum.datetime(2016, 8, 7)
    end = start.add(weeks=1)
    p1 = pendulum.period(start, end)
    intersection = p1.intersect(pendulum.period(start.add(days=2), start.add(days=4)))

    assert_datetime(intersection.start, 2016, 8, 9)
    assert_datetime(intersection.end, 2016, 8, 11)
Exemple #5
0
def test_intersect_excluded():
    start = pendulum.datetime(2016, 8, 7)
    end = start.add(weeks=1)
    p1 = pendulum.period(start, end)
    intersection = p1.intersect(
        pendulum.period(start.add(days=-2), start.add(days=-1))
    )

    assert intersection is None
    def test_string_input(self):
        '''
        Test two valid number inputs
        '''
        with self.assertRaises(TypeError):
            pen.period('hi', 3)

        with self.assertRaises(TypeError):
            pen.period(3, 'hi')
Exemple #7
0
def test_intersect_same():
    start = pendulum.datetime(2016, 8, 7)
    end = start.add(weeks=1)
    p1 = pendulum.period(start, end)
    intersection = p1.intersect(
        pendulum.period(start, end)
    )

    assert_datetime(intersection.start, 2016, 8, 7)
    assert_datetime(intersection.end, 2016, 8, 14)
Exemple #8
0
def test_intersect_multiple():
    start = pendulum.datetime(2016, 8, 7)
    end = start.add(weeks=1)
    p1 = pendulum.period(start, end)
    intersection = p1.intersect(
        pendulum.period(start.add(days=-2), start.add(days=2)),
        pendulum.period(start.add(days=1), start.add(days=2))
    )

    assert_datetime(intersection.start, 2016, 8, 8)
    assert_datetime(intersection.end, 2016, 8, 9)
Exemple #9
0
def class_date_range(
    start_date: 'Date', end_date: 'Date', off_days: Iterable['Date'],
    class_weekdays: Tuple[bool, bool, bool, bool, bool, bool, bool]
) -> Tuple['Date', 'Date']:
    """
    Calculates the start and end dates for a class.

    :param start_date: The inclusive start date of the class period.
    :param end_date: The inclusive end date of the class period.
    :param off_days: The dates of all holidays in the class period.
        May contain holidays outside of the class period.
    :param class_weekdays: What days will the class meet?
        Monday is at index 0 and Sunday is at 6.
    :return: The start and end date for a class that meets the given weekdays in the period.

    >>> mlk = date(2020, 1, 20)
    >>> spring_break = period(date(2020, 3, 9), date(2020, 3, 15))
    >>> spring_holidays = period(date(2020, 4, 10), date(2020, 4, 12))
    >>> off_days = {mlk, *spring_break, *spring_holidays}
    >>> start_and_end = class_date_range(
    ...     start_date=date(2020, 1, 13),
    ...     end_date=date(2020, 5, 12),
    ...     off_days=off_days,
    ...     class_weekdays=(True, False, True, False, False, False, False))
    >>> print(start_and_end)
    (Date(2020, 1, 13), Date(2020, 5, 11))
    """

    class_start_date = None
    class_end_date = None
    off_days = set(off_days)

    for day in period(start_date, end_date):

        if day in off_days:
            continue

        if class_weekdays[day.weekday()]:
            class_start_date = day
            break

    for day in reversed([*period(start_date, end_date)]):

        if day in off_days:
            continue

        if class_weekdays[day.weekday()]:
            class_end_date = day
            break

    return class_start_date, class_end_date
Exemple #10
0
def test_multiply():
    dt1 = pendulum.DateTime(2016, 8, 7, 12, 34, 56)
    dt2 = dt1.add(days=6, seconds=34)
    it = pendulum.period(dt1, dt2)
    mul = it * 2
    assert isinstance(mul, pendulum.Duration)
    assert_duration(mul, 0, 0, 1, 5, 0, 1, 8)

    dt1 = pendulum.DateTime(2016, 8, 7, 12, 34, 56)
    dt2 = dt1.add(days=6, seconds=34)
    it = pendulum.period(dt1, dt2)
    mul = it * 2
    assert isinstance(mul, pendulum.Duration)
    assert_duration(mul, 0, 0, 1, 5, 0, 1, 8)
Exemple #11
0
def test_floor_divide():
    dt1 = pendulum.DateTime(2016, 8, 7, 12, 34, 56)
    dt2 = dt1.add(days=2, seconds=34)
    it = pendulum.period(dt1, dt2)
    mul = it // 2
    assert isinstance(mul, pendulum.Duration)
    assert_duration(mul, 0, 0, 0, 1, 0, 0, 17)

    dt1 = pendulum.DateTime(2016, 8, 7, 12, 34, 56)
    dt2 = dt1.add(days=2, seconds=35)
    it = pendulum.period(dt1, dt2)
    mul = it // 3
    assert isinstance(mul, pendulum.Duration)
    assert_duration(mul, 0, 0, 0, 0, 16, 0, 11)
Exemple #12
0
def test_filter_dates_by_earliest_date(test_logger):
    """
    Test that the filter_dates task removes dates before workflow_config.earliest_date.
    """
    dates = list(
        pendulum.period(pendulum.date(2016, 1, 1),
                        pendulum.date(2016, 1, 7)).range("days"))
    workflow_config = WorkflowConfig(workflow_name="DUMMY_FLOW",
                                     earliest_date=pendulum.date(2016, 1, 4))
    with prefect.context(logger=test_logger):
        filtered_dates = filter_dates.run(available_dates=dates,
                                          workflow_config=workflow_config)
    assert filtered_dates == list(
        pendulum.period(pendulum.date(2016, 1, 4),
                        pendulum.date(2016, 1, 7)).range("days"))
Exemple #13
0
def get_available_time_slots(time_from, time_to, duration_hour):
    get_all_calendar_schedules.cache_clear()

    period = pendulum.period(pendulum.parse(time_from),
                             pendulum.parse(time_to))
    result = []

    for start in period.range('hours'):
        end = start.add(hours=duration_hour)
        time_slot_period = pendulum.period(start, end)
        overlapped_schedules = get_schedules_that_overlaps(time_slot_period)
        if len(overlapped_schedules) == 0:
            result.append(start.format('LLL'))

    return result
def get_application_historical_metrics():
    metrics = defaultdict(list)
    period = pendulum.period(pendulum.Pendulum(2016, 11, 1), pendulum.tomorrow())
    for dt in period.range('days'):
        date = dt.to_date_string()
        timestamp = dt.to_iso8601_string()
        query = '''
  WITH app_metrics AS (SELECT type, count(*) total_count
                FROM
                  (SELECT DISTINCT ON (object_id) date_trunc('day', created_at) AS day, type, object_id FROM audit_event
                  WHERE ((object_type = 'Application'
                          AND object_id NOT IN (SELECT id FROM application WHERE status = 'deleted') )
                          OR object_type = 'SupplierDomain')
                  AND created_at < :date
                  ORDER BY object_id, created_at DESC ) a
                GROUP BY type)
    SELECT * FROM app_metrics
    UNION SELECT 'started_application', sum(total_count) FROM app_metrics
      WHERE type IN ('submit_application','approve_application','create_application','revert_application')
    UNION SELECT 'completed_application', sum(total_count) FROM app_metrics
      WHERE type IN ('submit_application','approve_application','revert_application')
                '''
        for row in db.session.execute(query, {'date': date}).fetchall():
            metrics[row['type'] + "_count"].append({"value": row["total_count"], "ts": timestamp})

    return jsonify(metrics)
Exemple #15
0
def create_start_url(year1, month1, day1, year2, month2, day2, query):

    url_list = []
    #query = 'diabetes'
    #query = 'machine+learning'
    start = pendulum.datetime(year1, month1, day1)
    end = pendulum.datetime(year2, month2, day2)
    period = pendulum.period(start, end)

    weeks_list = []
    for dt in period.range('weeks'):
        start_week = dt.start_of('week').to_date_string()
        end_week = dt.end_of('week').to_date_string()
        #print("START WEEK: " + start_week)
        #print("END WEEK: " + end_week)
        week_tuple = (start_week, end_week)
        weeks_list.append(week_tuple)
        # print(date)

    print(weeks_list)

    for date in range(len(weeks_list)):
        start_date = weeks_list[date][0]
        end_date = weeks_list[date][1]

        #for i in range(len(start_date)):
        start_url = "https://github.com/search?utf8=%E2%9C%93&q={}+created%3A{}..{}&type=Repositories" \
                .format(query, start_date, end_date)
        #print(start_url)
        url_list.append(start_url)

    return url_list
Exemple #16
0
    def get_date_range_partitions(current_time=None):
        check.opt_inst_param(current_time, "current_time", datetime.datetime)
        tz = timezone if timezone else "UTC"
        _start = (to_timezone(start, tz) if isinstance(start, PendulumDateTime)
                  else pendulum.instance(start, tz=tz))

        if end:
            _end = end
        elif current_time:
            _end = current_time
        else:
            _end = pendulum.now(tz)

        # coerce to the definition timezone
        if isinstance(_end, PendulumDateTime):
            _end = to_timezone(_end, tz)
        else:
            _end = pendulum.instance(_end, tz=tz)

        period = pendulum.period(_start, _end)
        date_names = [
            Partition(value=current, name=current.strftime(fmt))
            for current in period.range(delta_range, delta_amount)
        ]

        # We don't include the last element here by default since we only want
        # fully completed intervals, and the _end time is in the middle of the interval
        # represented by the last element of date_names
        if inclusive:
            return date_names

        return date_names[:-1]
Exemple #17
0
def test_not_contains():
    dt1 = pendulum.datetime(2000, 1, 1, 12, 45, 37)
    dt2 = pendulum.datetime(2000, 1, 31, 12, 45, 37)

    p = pendulum.period(dt1, dt2)
    dt = pendulum.datetime(2000, 1, 1, 11, 45, 37)
    assert dt not in p
Exemple #18
0
 def get_status(self, learnprogress):
     today = pendulum.today().date()
     period = pendulum.period(today, learnprogress.plan.start_time)
     if learnprogress.status == 'assigned' and period.remaining_days > 0:
         return '未开始'
     else:
         return learnprogress.get_status_display()
Exemple #19
0
    def minutes_in_each_tarrif(self):
        minutes_to_charge = []

        call_started_at = pendulum.instance(self.call_started_at)
        call_ended_at = pendulum.instance(self.call_ended_at)

        call_start_day = pendulum.instance(call_started_at)
        call_end_day = pendulum.instance(call_ended_at)

        total_duration = pendulum.period(call_start_day, call_end_day)
        for day in total_duration.range("days"):

            start_standard_charge = pendulum.instance(
                datetime.combine(day, self.standard_starts_at))
            stop_standard_charge = pendulum.instance(
                datetime.combine(day, self.standard_ends_at))

            start_charging_at = max(call_started_at, start_standard_charge)
            stop_charging_at = min(call_ended_at, stop_standard_charge)

            minutes_to_charge_day = (stop_charging_at -
                                     start_charging_at).in_minutes()
            minutes_to_charge.append(minutes_to_charge_day)

        standard_minutes = sum(minutes_to_charge)
        reduced_minutes = total_duration.in_minutes() - standard_minutes

        return {
            "reduced": max(0, reduced_minutes),
            "standard": max(0, standard_minutes),
        }
Exemple #20
0
def test_contains_with_datetime():
    dt1 = pendulum.datetime(2000, 1, 1, 12, 45, 37)
    dt2 = pendulum.datetime(2000, 1, 31, 12, 45, 37)

    p = pendulum.period(dt1, dt2)
    dt = pendulum.datetime(2000, 1, 7)
    assert dt in p
Exemple #21
0
 def _table_header(self):
     """Generate header for COUNTER table for report, as list of cells."""
     header_cells = list(HEADER_FIELDS[self.report_type])
     for d_obj in pendulum.period(self.period[0],
                                  self.period[1]).range("months"):
         header_cells.append(d_obj.strftime("%b-%Y"))
     return header_cells
Exemple #22
0
def add_observations(cpi: str, end: str, ini: Optional[str] = None) -> None:
    """
    cpi is the kind of indicator [IPCA, IPCA15], 
    end is the last time observation to be updated
    and ini is the first
    """
    import pendulum
    cpi = cpi.upper()
    end = pendulum.parse(end)
    init = pendulum.datetime(
        2012, 1, 1) if cpi == "IPCA" else pendulum.datetime(2012, 2, 1)
    if ini is not None:
        ini = pendulum.parse(ini) if pendulum.parse(ini) >= init else init
    else:
        ini = end
    period = pendulum.period(ini, end)
    months = [p.format("YYYYMM") for p in period.range('months')]

    for month in months:
        if cpi == "IPCA":
            if (pendulum.from_format(month, "YYYYMM") <= pendulum.datetime(
                    2019, 12, 1)):
                df = fetch(cpi, limit=month, new=False)
            else:
                df = fetch(cpi, limit=month, new=True)
        else:
            if (pendulum.from_format(month, "YYYYMM") <= pendulum.datetime(
                    2020, 1, 1)):
                df = fetch(cpi, limit=month, new=False)
            else:
                df = fetch(cpi, limit=month, new=True)
        _add_data_frame(df)
        print(f"Added for {cpi} and {month}")
Exemple #23
0
    async def sync_bills(self, schema, period: pendulum.period = None):
        """Output the `bills` in the period."""
        stream = "bills"
        loop = asyncio.get_event_loop()

        if not period:
            # build a default period from the last bookmark
            bookmark = get_bookmark(self.state, stream, "start_time")
            start = pendulum.parse(bookmark)
            end = pendulum.now()
            period = pendulum.period(start, end)

        singer.write_schema(stream, schema, ["invoice_id"])

        for at in period.range("months"):
            result = await loop.run_in_executor(None, self.client.bill, at)
            if result:
                singer.write_record(stream, result)
                try:
                    end = datetime.datetime.strptime(
                        result["end_time"], "%Y-%m-%dT%H:%M:%SZ").isoformat()
                    self.state = write_bookmark(self.state, stream,
                                                "start_time", end)
                except:
                    # print("what fails is:" + result['end_time'])
                    sys.stderr.write("what fails is:" + result['end_time'] +
                                     "\n")
Exemple #24
0
def 时间迭代(start, end):
    """
    为支持单位range()有:years,months,weeks, days,hours,minutes和seconds
    for i in list.range('months'):
        print(i.to_datetime_string())
    """
    return pendulum.period(pendulum.parse(start), pendulum.parse(end))
Exemple #25
0
def _get_shanbay_streak(end_date=pendulum.now("Asia/Shanghai"), streak=0):
    start_date = end_date.start_of("month")
    r = requests.get(
        SHANBAY_CALENDAR_API.format(
            user_name=MY_SHANBAY_USER_NAME,
            start_date=start_date.to_date_string(),
            end_date=end_date.to_date_string(),
        )
    )
    if not r.ok:
        raise Exception("Can not get days from shanbay API")

    data = r.json()
    logs = data["logs"]
    if not logs:
        return streak
    periods = list(pendulum.period(start_date, end_date.subtract(days=1)))
    periods.sort(reverse=True)

    log_dates = [i["date"] for i in logs]
    # if today id done
    if end_date.to_date_string() in log_dates:
        streak += 1

    # for else if not break not else
    for p in periods:
        if p.to_date_string() not in log_dates:
            break
        streak += 1
    else:
        streak = _get_shanbay_streak(
            start_date.subtract(months=1).end_of("month"), streak=streak
        )
    return streak
Exemple #26
0
    def enqueue(self, start_date, end_date):
        """Schedule logfile processing.

        This is the start of the indexing process. The time period
        described by start_date and end_date determines how many days
        of logs will be processed.

        Other than scheduling work to occur in the future, not much
        happens here. The main goal is to prevent unnecessary work
        by allowing the same time period to be submitted multiple times.

        """

        period = pendulum.period(start_date, end_date)

        if self.queue.count(period) > 0:
            cherrypy.engine.publish(
                "applog:add",
                "logindex",
                "enqueue",
                "Ignoring a request to queue an already-queued range"
            )
            return False

        self.queue.append(period)
        cherrypy.engine.publish("scheduler:add", 5, "logindex:process_queue")

        cherrypy.engine.publish(
            "applog:add",
            "logindex",
            "enqueue",
            "Queueing complete, processing scheduled"
        )

        return True
Exemple #27
0
def test_inverted_and_absolute():
    dt1 = pendulum.DateTime(2000, 1, 1)
    dt2 = pendulum.DateTime(2000, 1, 31)
    p = pendulum.period(dt2, dt1, True)

    assert_datetime(p.start, 2000, 1, 1)
    assert_datetime(p.end, 2000, 1, 31)
Exemple #28
0
def get_date_intervals(start_date, end_date, terms):
    start_day = start_date.day
    start_month = start_date.month
    start_year = start_date.year

    end_day = end_date.day
    end_month = end_date.month
    end_year = end_date.year

    start = pendulum.Pendulum(start_year, start_month, start_day)
    end = pendulum.Pendulum(end_year, end_month, end_day)
    period = pendulum.period(start, end)

    if terms == 'Monthly':
        return [dt.format('%B %d, %Y') for dt in period.range('months')][1:]

    elif terms == 'Quarterly':
        return [dt.format('%B %d, %Y') for dt in period.range('months', 3)][1:]

    elif terms == 'Semi-Annually':
        return [dt.format('%B %d, %Y') for dt in period.range('months', 6)][1:]

    elif terms == 'Annually':
        return [dt.format('%B %d, %Y')
                for dt in period.range('months', 12)][1:]
def main(args):
    if len(args) != 2:
        print('usage: daily-weather src dst')
        sys.exit(-2)

    src = args[0]
    dst = args[1]

    df = pd.read_excel(src)
    df.set_index(pd.to_datetime(df.date_time), inplace=True)
    df.drop('date_time', axis='columns', inplace=True)

    df_out = pd.DataFrame()

    start = pendulum.parse('2017-11-17')
    end = pendulum.parse(str(df.index.max().date()))

    for day in pendulum.period(start, end).range('days'):
        day_str = day.to_date_string()
        hourly_wx = df[day_str].between_time('9:00', '18:00')
        df_out.loc[day_str, 'min_temp'] = hourly_wx.temperature.min()
        df_out.loc[day_str, 'max_temp'] = hourly_wx.temperature.max()
        df_out.loc[day_str, 'is_precip'] = is_precip_day(hourly_wx.precip_type)

    df_out.reset_index(inplace=True)
    df_out.columns = ['date', 'min_temp', 'max_temp', 'is_precip']
    df_out.dropna(inplace=True)
    df_out.to_excel(dst)
Exemple #30
0
def test_inverted():
    dt1 = pendulum.DateTime(2000, 1, 1)
    dt2 = pendulum.DateTime(2000, 1, 31)
    p = pendulum.period(dt2, dt1)

    assert_datetime(p.start, 2000, 1, 31)
    assert_datetime(p.end, 2000, 1, 1)
Exemple #31
0
def test_with_pendulum():
    dt1 = pendulum.DateTime(2000, 1, 1)
    dt2 = pendulum.DateTime(2000, 1, 31)
    p = pendulum.period(dt1, dt2)

    assert_datetime(p.start, 2000, 1, 1)
    assert_datetime(p.end, 2000, 1, 31)
Exemple #32
0
    def get_date_range_partitions():
        tz = timezone if timezone else pendulum.now().timezone.name
        _start = (start.in_tz(tz) if isinstance(start, pendulum.Pendulum) else
                  pendulum.instance(start, tz=tz))

        if not end:
            _end = pendulum.now(tz)
        elif isinstance(end, pendulum.Pendulum):
            _end = end.in_tz(tz)
        else:
            _end = pendulum.instance(end, tz=tz)

        period = pendulum.period(_start, _end)
        date_names = [
            Partition(value=current, name=current.strftime(fmt))
            for current in period.range(delta_range, delta_amount)
        ]

        # We don't include the last element here by default since we only want
        # fully completed intervals, and the _end time is in the middle of the interval
        # represented by the last element of date_names
        if inclusive:
            return date_names

        return date_names[:-1]
Exemple #33
0
    def stream_slices(
        self,
        sync_mode: SyncMode,
        cursor_field: List[str] = None,
        stream_state: Mapping[str, Any] = None
    ) -> Iterable[Optional[Mapping[str, Any]]]:
        """Extend default slicing based on accounts with slices based on date intervals"""
        stream_state = stream_state or {}
        stream_slices = super().stream_slices(sync_mode=sync_mode,
                                              cursor_field=cursor_field,
                                              stream_state=stream_state)
        for stream_slice in stream_slices:
            account = stream_slice["account"]
            account_id = account["instagram_business_account"]["id"]

            state_value = stream_state.get(account_id,
                                           {}).get(self.cursor_field)
            start_date = pendulum.parse(
                state_value) if state_value else self._start_date
            start_date = max(start_date, self._start_date,
                             pendulum.now().subtract(days=self.buffer_days))
            for since in pendulum.period(start_date, self._end_date).range(
                    "days", self.days_increment):
                until = since.add(days=self.days_increment)
                self.logger.info(
                    f"Reading insights between {since.date()} and {until.date()}"
                )
                yield {
                    **stream_slice,
                    "since": since.to_datetime_string(),
                    "until": until.to_datetime_string(),  # excluding
                }
Exemple #34
0
    def get_next_trading_day(self):
        current_time = pendulum.now(tz=self.timezone)

        start_time = pendulum.parse(self.begin_time, exact=True)
        start_date = pendulum.datetime(
            current_time.year,
            current_time.month,
            current_time.day,
            start_time.hour,
            start_time.minute,
            tz=self.timezone,
        )

        if current_time < start_date:
            if current_time.date() == start_date.date():
                if current_time.day_of_week not in self.weekends:
                    if start_date.date() not in self.get_holidays():
                        return start_date

        for day in pendulum.period(start_date.add(days=1),
                                   start_date.add(days=7)):
            if day.day_of_week in self.weekends:
                continue
            if day.date() in self.get_holidays():
                continue
            return day
Exemple #35
0
def test_range_months_overflow():
    dt1 = pendulum.datetime(2016, 1, 30, tz="America/Sao_Paulo")
    dt2 = dt1.add(months=4)

    p = pendulum.period(dt1, dt2)
    r = list(p.range("months"))

    assert_datetime(r[0], 2016, 1, 30, 0, 0, 0)
    assert_datetime(r[-1], 2016, 5, 30, 0, 0, 0)
Exemple #36
0
def test_range_with_dst():
    dt1 = pendulum.datetime(2016, 10, 14, tz="America/Sao_Paulo")
    dt2 = dt1.add(weeks=1)

    p = pendulum.period(dt1, dt2)
    r = list(p.range("days"))

    assert_datetime(r[0], 2016, 10, 14, 0, 0, 0)
    assert_datetime(r[2], 2016, 10, 16, 1, 0, 0)
    assert_datetime(r[-1], 2016, 10, 21, 0, 0, 0)
Exemple #37
0
 def _fill_months(self):
     """Ensure each month in period represented and zero fill if not."""
     start, end = self.period[0], self.period[1]
     try:
         for d_obj in pendulum.period(start, end).range("months"):
             if d_obj not in (x[0] for x in self._full_data):
                 self._full_data.append((d_obj, 0))
     except IndexError:
         pass
     else:
         self._full_data.sort()
Exemple #38
0
def test_range_amount():
    dt1 = pendulum.datetime(2016, 10, 14, tz="America/Sao_Paulo")
    dt2 = dt1.add(weeks=1)

    p = pendulum.period(dt1, dt2)
    r = list(p.range("days", 2))

    assert len(r) == 4
    assert_datetime(r[0], 2016, 10, 14, 0, 0, 0)
    assert_datetime(r[1], 2016, 10, 16, 1, 0, 0)
    assert_datetime(r[2], 2016, 10, 18, 0, 0, 0)
    assert_datetime(r[3], 2016, 10, 20, 0, 0, 0)
Exemple #39
0
    def _totals_line(self, metric):
        """Generate Totals for a given metric."""
        total_cells = [TOTAL_TEXT[self.report_type]]
        publishers = set(resource.publisher for resource in self.pubs)
        if len(publishers) == 1:
            total_cells.append(publishers.pop())
        else:
            total_cells.append(u"")
        platforms = set(resource.platform for resource in self.pubs)
        if len(platforms) == 1:
            total_cells.append(platforms.pop())
        else:
            total_cells.append(u"")
        if self.report_type in ("JR1", "BR1", "BR2"):
            total_cells.extend([u""] * 4)
        elif self.report_type == "DB2":
            total_cells.append(metric)
        total_usage = 0
        pdf_usage = 0
        html_usage = 0

        number_of_months = len(
            list(pendulum.period(self.period[0], self.period[1]).range("months"))
        )
        month_data = [0] * number_of_months
        for pub in self.pubs:
            if pub.metric != metric:
                continue
            if self.report_type == "JR1":
                pdf_usage += pub.pdf_total
                html_usage += pub.html_total
            for month, data in enumerate(pub):
                total_usage += data[2]
                month_data[month] += data[2]
        total_cells.append(six.text_type(total_usage))
        if self.report_type == "JR1":
            total_cells.append(six.text_type(html_usage))
            total_cells.append(six.text_type(pdf_usage))
        total_cells.extend(six.text_type(d) for d in month_data)
        return total_cells
Exemple #40
0
    def GET(self, *_args, **kwargs):
        """Display the list of available grids, or the current grid"""

        name = kwargs.get('name', '')
        start = kwargs.get('start')

        grids = cherrypy.engine.publish(
            "registry:search",
            "grids:*",
            as_dict=True
        ).pop()

        options = defaultdict(lambda: None)

        try:
            config = next(
                value.split("\n")
                for key, value in grids.items() if key.endswith(":" + name)
            )

            headers = [value.strip() for value in config[0].split(",")]

            options.update([value.split('=') for value in config[1:]])

        except StopIteration:
            headers = []

        rows = []
        if options["layout"] == "month":
            today = pendulum.today()

            try:
                start = pendulum.from_format(start, "YYYY-MM")
            except (TypeError, ValueError):
                start = today.start_of("month")

            headers = ["Date", "Day"] + headers
            options["last_month"] = start.subtract(months=1)
            options["next_month"] = start.add(months=1)
            options["this_month"] = today

            period = pendulum.period(start, start.end_of("month"))

            for day in period.range("days"):
                row = [''] * len(headers)
                row[0] = day.format("MMM D, YYYY")
                row[1] = day.format("dddd")
                rows.append(row)
        elif headers:
            row = [''] * len(headers)
            rows = [row for x in range(1, 30)]

        return {
            "html": ("grids.jinja.html", {
                "headers": headers,
                "name": name,
                "names": [key.split(":")[1] for key in grids.keys()],
                "options": options,
                "rows": rows,
            })
        }
Exemple #41
0
def _parse(text, **options):
    """
    Parses a string with the given options.

    :param text: The string to parse.
    :type text: str

    :rtype: mixed
    """
    # Handling special cases
    if text == "now":
        return pendulum.now()

    parsed = base_parse(text, **options)

    if isinstance(parsed, datetime.datetime):
        return pendulum.datetime(
            parsed.year,
            parsed.month,
            parsed.day,
            parsed.hour,
            parsed.minute,
            parsed.second,
            parsed.microsecond,
            tz=parsed.tzinfo or options.get("tz", UTC),
        )

    if isinstance(parsed, datetime.date):
        return pendulum.date(parsed.year, parsed.month, parsed.day)

    if isinstance(parsed, datetime.time):
        return pendulum.time(
            parsed.hour, parsed.minute, parsed.second, parsed.microsecond
        )

    if isinstance(parsed, _Interval):
        if parsed.duration is not None:
            duration = parsed.duration

            if parsed.start is not None:
                dt = pendulum.instance(parsed.start, tz=options.get("tz", UTC))

                return pendulum.period(
                    dt,
                    dt.add(
                        years=duration.years,
                        months=duration.months,
                        weeks=duration.weeks,
                        days=duration.remaining_days,
                        hours=duration.hours,
                        minutes=duration.minutes,
                        seconds=duration.remaining_seconds,
                        microseconds=duration.microseconds,
                    ),
                )

            dt = pendulum.instance(parsed.end, tz=options.get("tz", UTC))

            return pendulum.period(
                dt.subtract(
                    years=duration.years,
                    months=duration.months,
                    weeks=duration.weeks,
                    days=duration.remaining_days,
                    hours=duration.hours,
                    minutes=duration.minutes,
                    seconds=duration.remaining_seconds,
                    microseconds=duration.microseconds,
                ),
                dt,
            )

        return pendulum.period(
            pendulum.instance(parsed.start, tz=options.get("tz", UTC)),
            pendulum.instance(parsed.end, tz=options.get("tz", UTC)),
        )

    if CDuration and isinstance(parsed, CDuration):
        return pendulum.duration(
            years=parsed.years,
            months=parsed.months,
            weeks=parsed.weeks,
            days=parsed.days,
            hours=parsed.hours,
            minutes=parsed.minutes,
            seconds=parsed.seconds,
            microseconds=parsed.microseconds,
        )

    return parsed
Exemple #42
0
 def _table_header(self):
     """Generate header for COUNTER table for report, as list of cells."""
     header_cells = list(HEADER_FIELDS[self.report_type])
     for d_obj in pendulum.period(self.period[0], self.period[1]).range("months"):
         header_cells.append(d_obj.strftime("%b-%Y"))
     return header_cells