Пример #1
0
 def shift_range(self, start_date: pendulum.Pendulum, end_date: pendulum.Pendulum) -> Tuple[pendulum.Pendulum, pendulum.Pendulum]:
     # HACK: Records are made available one business day *after* their "date".
     # Accounting for holidays, they might be delayed over a 4-day weekend.
     # When harvesting yesterday's data, actually reach back farther...
     if end_date.is_today():
         start_date = start_date.subtract(days=5)
     return start_date, end_date
Пример #2
0
    def test_parse_creates_an_instance_default_to_utcnow(self):
        p = Pendulum.parse()
        now = Pendulum.utcnow()
        self.assertIsInstanceOfPendulum(p)
        self.assertEqual(p.timezone_name, p.timezone_name)

        self.assertPendulum(p, now.year, now.month, now.day, now.hour, now.minute, now.second)
Пример #3
0
    def get_seasonal_maxima(self, start_year: int, end_year: int, season_to_months: dict, varname_internal: str):

        """
        returns a dictionary {season:{year: field of maxima}}
        :param start_year:
        :param end_year:
        :param season_to_months:

        (order of months in the list of months is important, i.e. for DJF the order should be [12, 1, 2])
        """
        result = defaultdict(dict)

        for season, months in season_to_months.items():

            for y in range(start_year, end_year + 1):
                d1 = Pendulum(y, months[0], 1)
                d2 = d1.add(months=len(months)).subtract(seconds=1)

                if d2.year > end_year:
                    continue

                current_period = Period(d1, d2)
                print("calculating mean for [{}, {}]".format(current_period.start, current_period.end))
                data = self.read_data_for_period(current_period, varname_internal)

                if varname_internal == LAKE_ICE_FRACTION:
                    result[season][y] = np.ma.masked_where(data.values > 1, data.values).max(axis=0)
                else:
                    result[season][y] = data.max(dim="t").values

        return result
Пример #4
0
    def test_not_equal_with_timezone_true(self):
        d1 = Pendulum(2000, 1, 1, tzinfo='America/Toronto')
        d2 = Pendulum(2000, 1, 1, tzinfo='America/Vancouver')
        d3 = datetime(2000, 1, 1)

        self.assertTrue(d1.ne(d2))
        self.assertFalse(d1.ne(d3))
Пример #5
0
    def get_year_month_to_period_map(self, start_year, end_year):

        """
        generate mapping (year, month) --> period
        :param start_year: 
        :param end_year: 
        :return: 
        """
        res = {}

        for y in range(start_year, end_year + 1):
            start = Pendulum(y, self.start_month, 1)
            end = start.add(months=self.nmonths).subtract(microseconds=1)


            if end.year > end_year:
                continue

            print(start, end)
            p = Period(start, end)

            for s in p.range("months"):
                res[(s.year, s.month)] = p

        return res
Пример #6
0
    def get_mean_number_of_hles_days(self, start_year: int, end_year: int, season_to_months: dict, hles_vname: str):
        result = defaultdict(dict)

        cache_dir = Path(self.base_folder) / "cache"
        cache_dir.mkdir(exist_ok=True)
        seasons_str = "-".join(season_to_months)
        cache_file = cache_dir / f"get_mean_number_of_hles_days_{start_year}-{end_year}_m{seasons_str}_{hles_vname}.bin"

        if cache_file.exists():
            return pickle.load(cache_file.open("rb"))

        for season, months in season_to_months.items():

            for y in range(start_year, end_year + 1):
                d1 = Pendulum(y, months[0], 1)
                d2 = d1.add(months=len(months)).subtract(seconds=1)

                if d2.year > end_year:
                    continue

                current_period = Period(d1, d2)
                print("calculating mean for [{}, {}]".format(current_period.start, current_period.end))
                data = self.read_data_for_period(current_period, hles_vname)

                # calculate number of hles days

                data_daily = data.resample(t="1D", keep_attrs=True).mean(dim="t")

                result[season][y] = (data_daily.values >= 0.1).sum(axis=0)


        pickle.dump(result, cache_file.open("wb"))
        return result
Пример #7
0
    def do_harvest(self, start_date: pendulum.Pendulum, end_date: pendulum.Pendulum) -> Iterator[Tuple[str, Union[str, dict, bytes]]]:
        url = furl(self.config.base_url)

        url.args['dateStart'] = start_date.date().strftime('%m/%d/%Y')
        url.args['dateEnd'] = end_date.date().strftime('%m/%d/%Y')
        url.args['offset'] = 0

        return self.fetch_records(url)
Пример #8
0
    def wrap_with_test_now(self, dt=None):
        if dt is None:
            dt = Pendulum.create(2012, 1, 1, 1, 2, 3)

        Pendulum.set_test_now(dt)

        yield

        Pendulum.set_test_now()
Пример #9
0
    def test_between_equal_false(self):
        d1 = Pendulum(1999, 12, 31)
        d2 = Pendulum(2000, 1, 1)
        d3 = Pendulum(2000, 1, 31)
        d4 = datetime(2000, 1, 1)
        d5 = datetime(2000, 1, 31)

        self.assertFalse(d1.between(d2, d3))
        self.assertFalse(d1.between(d4, d5))
Пример #10
0
    def test_between_not_equal_true(self):
        d1 = Pendulum(2000, 1, 15)
        d2 = Pendulum(2000, 1, 1)
        d3 = Pendulum(2000, 1, 31)
        d4 = datetime(2000, 1, 1)
        d5 = datetime(2000, 1, 31)

        self.assertTrue(d1.between(d2, d3, False))
        self.assertTrue(d1.between(d4, d5, False))
Пример #11
0
    def test_between_not_equal_switch_false(self):
        d1 = Pendulum(2000, 1, 1)
        d2 = Pendulum(2000, 1, 1)
        d3 = Pendulum(2000, 1, 31)
        d4 = datetime(2000, 1, 1)
        d5 = datetime(2000, 1, 31)

        self.assertFalse(d1.between(d3, d2, False))
        self.assertFalse(d1.between(d5, d4, False))
Пример #12
0
    def test_between_equal_switch_true(self):
        d1 = Pendulum(2000, 1, 15)
        d2 = Pendulum(2000, 1, 1)
        d3 = Pendulum(2000, 1, 31)
        d4 = datetime(2000, 1, 1)
        d5 = datetime(2000, 1, 31)

        self.assertTrue(d1.between(d3, d2))
        self.assertTrue(d1.between(d5, d4))
Пример #13
0
    def test_equal_with_timezone_true(self):
        d1 = Pendulum(2000, 1, 1, 12, 0, 0, tzinfo='America/Toronto')
        d2 = Pendulum(2000, 1, 1, 9, 0, 0, tzinfo='America/Vancouver')
        d3 = datetime(2000, 1, 1, 12, 0, 0)

        self.assertTrue(d1.eq(d2))
        self.assertTrue(d1.eq(d3))
        self.assertEqual(d1, d2)
        self.assertEqual(d1, d3)
Пример #14
0
    def test_in_timezone(self):
        d = Pendulum(2015, 1, 15, 18, 15, 34)
        now = Pendulum(2015, 1, 15, 18, 15, 34)
        self.assertEqual('UTC', d.timezone_name)
        self.assertPendulum(d, now.year, now.month, now.day, now.hour, now.minute)

        d = d.in_timezone('Europe/Paris')
        self.assertEqual('Europe/Paris', d.timezone_name)
        self.assertPendulum(d, now.year, now.month, now.day, now.hour + 1, now.minute)
Пример #15
0
    def test_not_equal_to_false(self):
        d1 = Pendulum(2000, 1, 1, 1, 2, 3)
        d2 = Pendulum(2000, 1, 1, 1, 2, 3)
        d3 = datetime(2000, 1, 1, 1, 2, 3)

        self.assertFalse(d1.ne(d2))
        self.assertFalse(d1.ne(d3))
        self.assertEqual(d1, d2)
        self.assertEqual(d1, d3)
Пример #16
0
    def test_equal_to_true(self):
        d1 = Pendulum(2000, 1, 1, 1, 2, 3)
        d2 = Pendulum(2000, 1, 1, 1, 2, 3)
        d3 = datetime(2000, 1, 1, 1, 2, 3)

        self.assertTrue(d1.eq(d2))
        self.assertTrue(d1.eq(d3))
        self.assertEqual(d1, d2)
        self.assertEqual(d1, d3)
Пример #17
0
    def test_greater_than_false(self):
        d1 = Pendulum(2000, 1, 1)
        d2 = Pendulum(2000, 1, 2)
        d3 = datetime(2000, 1, 2)

        self.assertFalse(d1.gt(d2))
        self.assertFalse(d1.gt(d3))

        self.assertFalse(d1 > d2)
        self.assertFalse(d1 > d3)
Пример #18
0
    def test_greater_than_true(self):
        d1 = Pendulum(2000, 1, 1)
        d2 = Pendulum(1999, 12, 31)
        d3 = datetime(1999, 12, 31)

        self.assertTrue(d1.gt(d2))
        self.assertTrue(d1.gt(d3))

        self.assertTrue(d1 > d2)
        self.assertTrue(d1 > d3)
Пример #19
0
    def test_greater_than_or_equal_with_timezone_true(self):
        d1 = Pendulum(2000, 1, 1, 12, 0, 0, tzinfo='America/Toronto')
        d2 = Pendulum(2000, 1, 1, 8, 59, 59, tzinfo='America/Vancouver')
        d3 = pytz.timezone('America/Vancouver').localize(datetime(2000, 1, 1, 8, 59, 59))

        self.assertTrue(d1.gte(d2))
        self.assertTrue(d1.gte(d3))

        self.assertTrue(d1 >= d2)
        self.assertTrue(d1 >= d3)
Пример #20
0
    def test_less_than_or_equal_with_timezone_false(self):
        d1 = Pendulum(2000, 1, 1, 9, 0, 1, tzinfo='America/Vancouver')
        d2 = Pendulum(2000, 1, 1, 12, 0, 0, tzinfo='America/Toronto')
        d3 = pytz.timezone('America/Toronto').localize(datetime(2000, 1, 1, 12, 0, 0))

        self.assertFalse(d1.lte(d2))
        self.assertFalse(d1.lte(d3))

        self.assertFalse(d1 <= d2)
        self.assertFalse(d1 <= d3)
Пример #21
0
    def test_less_than_or_equal_false(self):
        d1 = Pendulum(2000, 1, 2)
        d2 = Pendulum(2000, 1, 1)
        d3 = datetime(2000, 1, 1)

        self.assertFalse(d1.lte(d2))
        self.assertFalse(d1.lte(d3))

        self.assertFalse(d1 <= d2)
        self.assertFalse(d1 <= d3)
Пример #22
0
    def test_less_than_or_equal_true_equal(self):
        d1 = Pendulum(2000, 1, 1)
        d2 = Pendulum(2000, 1, 1)
        d3 = datetime(2000, 1, 1)

        self.assertTrue(d1.lte(d2))
        self.assertTrue(d1.lte(d3))

        self.assertTrue(d1 <= d2)
        self.assertTrue(d1 <= d3)
Пример #23
0
    def _do_fetch(self, start_date: pendulum.Pendulum, end_date: pendulum.Pendulum) -> Iterator[Tuple[str, Union[str, dict, bytes]]]:
        url = furl(self.config.base_url)

        url.args['dateStart'] = start_date.date().strftime('%m/%d/%Y')
        url.args['dateEnd'] = end_date.date().strftime('%m/%d/%Y')
        url.args['offset'] = 0
        url.args['printFields'] = ','.join(NSF_FIELDS)
        url.args['rpp'] = PAGE_SIZE

        return self.fetch_records(url)
Пример #24
0
    def test_less_than_true(self):
        d1 = Pendulum(2000, 1, 1)
        d2 = Pendulum(2000, 1, 2)
        d3 = datetime(2000, 1, 2)

        self.assertTrue(d1.lt(d2))
        self.assertTrue(d1.lt(d3))

        self.assertTrue(d1 < d2)
        self.assertTrue(d1 < d3)
Пример #25
0
    def test_less_than_with_timezone_true(self):
        d1 = Pendulum(2000, 1, 1, 8, 59, 59, tzinfo='America/Vancouver')
        d2 = Pendulum(2000, 1, 1, 12, 0, 0, tzinfo='America/Toronto')
        d3 = pytz.timezone('America/Toronto').localize(datetime(2000, 1, 1, 12, 0, 0))

        self.assertTrue(d1.lt(d2))
        self.assertTrue(d1.lt(d3))

        self.assertTrue(d1 < d2)
        self.assertTrue(d1 < d3)
Пример #26
0
    def test_greater_than_with_timezone_false(self):
        d1 = Pendulum(2000, 1, 1, 12, 0, 0, tzinfo='America/Toronto')
        d2 = Pendulum(2000, 1, 1, 9, 0, 1, tzinfo='America/Vancouver')
        d3 = pytz.timezone('America/Vancouver').localize(datetime(2000, 1, 1, 9, 0, 1))

        self.assertFalse(d1.gt(d2))
        self.assertFalse(d1.gt(d3))

        self.assertFalse(d1 > d2)
        self.assertFalse(d1 > d3)
Пример #27
0
    def test_greater_than_or_equal_true_equal(self):
        d1 = Pendulum(2000, 1, 1)
        d2 = Pendulum(2000, 1, 1)
        d3 = datetime(2000, 1, 1)

        self.assertTrue(d1.gte(d2))
        self.assertTrue(d1.gte(d3))

        self.assertTrue(d1 >= d2)
        self.assertTrue(d1 >= d3)
Пример #28
0
    def test_is_birthday(self):
        d = Pendulum.now()
        a_birthday = d.sub_year()
        self.assertTrue(a_birthday.is_birthday())
        not_a_birthday = d.sub_day()
        self.assertFalse(not_a_birthday.is_birthday())
        also_not_a_birthday = d.add_days(2)
        self.assertFalse(also_not_a_birthday.is_birthday())

        d1 = Pendulum(1987, 4, 23)
        d2 = Pendulum(2014, 9, 26)
        d3 = Pendulum(2014, 4, 23)
        self.assertFalse(d2.is_birthday(d1))
        self.assertTrue(d3.is_birthday(d1))
Пример #29
0
    def do_harvest(self, start_date: pendulum.Pendulum, end_date: pendulum.Pendulum, set_spec=None) -> list:
        url = furl(self.url)

        if set_spec:
            url.args['set'] = set_spec
        url.args['verb'] = 'ListRecords'
        url.args['metadataPrefix'] = self.metadata_prefix

        if self.time_granularity:
            url.args[self.from_param] = start_date.format('YYYY-MM-DDT00:00:00', formatter='alternative') + 'Z'
            url.args[self.until_param] = end_date.format('YYYY-MM-DDT00:00:00', formatter='alternative') + 'Z'
        else:
            url.args[self.from_param] = start_date.date().isoformat()
            url.args[self.until_param] = end_date.date().isoformat()

        return self.fetch_records(url)
Пример #30
0
 def test_farthest_with_datetime(self):
     instance = Pendulum.create(2015, 5, 28, 12, 0, 0)
     dt1 = datetime(2015, 5, 28, 11, 0, 0)
     dt2 = datetime(2015, 5, 28, 14, 0, 0)
     closest = instance.farthest(dt1, dt2)
     self.assertIsInstanceOfPendulum(closest)
     self.assertPendulum(closest, 2015, 5, 28, 14, 0, 0)
Пример #31
0
 def test_diff_in_minutes_negative_no_sign(self):
     dt = Pendulum.create(2000, 1, 1)
     self.assertEqual(
         58,
         dt.diff(dt.copy().subtract(hours=1).add(minutes=2)).in_minutes())
Пример #32
0
 def test_diff_in_minutes_positive_big(self):
     dt = Pendulum(2000, 1, 1)
     self.assertEqual(
         1502,
         dt.diff(dt.copy().add(hours=25).add(minutes=2)).in_minutes())
Пример #33
0
 def test_unlocalizable_directive(self):
     d = Pendulum(1975, 12, 25, 14, 15, 16, tzinfo='local')
     f = ClassicFormatter()
     self.assertRaises(ValueError, f._localize_directive, d, '%8', 'en')
Пример #34
0
 def test_add_weeks_negative(self):
     self.assertEqual(14, Pendulum(1975, 5, 21).add(weeks=-1).day)
Пример #35
0
 def test_add_hours_zero(self):
     self.assertEqual(0, Pendulum(1975, 5, 21, 0, 0, 0).add(hours=0).hour)
Пример #36
0
 def test_diff_in_seconds_positive_big(self):
     dt = Pendulum.create(2000, 1, 1)
     self.assertEqual(
         7202,
         dt.diff(dt.copy().add(hours=2).add(seconds=2)).in_seconds())
Пример #37
0
 def test_diff_in_minutes_ensure_is_truncated(self):
     dt = Pendulum.create(2000, 1, 1)
     self.assertEqual(
         1,
         dt.diff(dt.copy().add(minutes=1).add(seconds=59)).in_minutes())
Пример #38
0
 def test_diff_in_weeks_negative_no_sign(self):
     dt = Pendulum.create(2000, 1, 1)
     self.assertEqual(52, dt.diff(dt.copy().subtract(years=1)).in_weeks())
Пример #39
0
 def test_diff_in_weekend_days_negative_with_sign(self):
     dt = Pendulum.create(2000, 1, 31)
     self.assertEqual(
         -10,
         dt.diff(dt.start_of('month'), False).in_weekend_days())
Пример #40
0
 def test_diff_in_weekend_days_positive(self):
     dt = Pendulum.create(2000, 1, 1)
     self.assertEqual(10, dt.diff(dt.end_of('month')).in_weekend_days())
Пример #41
0
 def test_add_seconds_zero(self):
     self.assertEqual(0,
                      Pendulum(1975, 5, 21, 0, 0, 0).add(seconds=0).second)
Пример #42
0
ANGLE_TARGET = 3103
ANGLE_AVG_LENGTH = 32
ANGLE_SMOOTHING = 1.0  # 1.0 turns off smoothing
ANGLE_KP = 400
ANGLE_KD = 400

POSITION_TARGET = 0
POSITION_CTRL_PERIOD_MS = 25  # Must be a multiple of CONTROL_PERIOD_MS
POSITION_SMOOTHING = 0.5  # 1.0 turns off smoothing
POSITION_KP = 0  #1
POSITION_KD = 0  #5

################################################################################
# OPEN SERIAL PORT
################################################################################
p = Pendulum()
p.open(SERIAL_PORT, SERIAL_BAUD)
p.control_mode(False)
p.stream_output(False)

if CALIBRATE:
    if not p.calibrate():
        print("Failed to connect to device")
        p.close()
        exit()

time.sleep(1)

################################################################################
# SET PARAMETERS
################################################################################
Пример #43
0
 def test_add_minutes_positive(self):
     self.assertEqual(1,
                      Pendulum(1975, 5, 21, 0, 0, 0).add(minutes=1).minute)
Пример #44
0
 def test_diff_in_minutes_vs_default_now(self):
     with self.wrap_with_test_now():
         self.assertEqual(
             60,
             Pendulum.now().subtract(hours=1).diff().in_minutes())
Пример #45
0
 def test_diff_in_years_positive(self):
     dt = Pendulum.create(2000, 1, 1)
     self.assertEqual(1, dt.diff(dt.copy().add(years=1)).in_years())
Пример #46
0
 def test_diff_in_weeks_vs_default_now(self):
     with self.wrap_with_test_now():
         self.assertEqual(
             1,
             Pendulum.now().subtract(weeks=1).diff().in_weeks())
Пример #47
0
 def test_diff_in_seconds_positive(self):
     dt = Pendulum.create(2000, 1, 1)
     self.assertEqual(
         62,
         dt.diff(dt.copy().add(minutes=1).add(seconds=2)).in_seconds())
Пример #48
0
 def test_diff_in_weeks_ensure_is_truncated(self):
     dt = Pendulum.create(2000, 1, 1)
     self.assertEqual(
         0,
         dt.diff(dt.copy().add(weeks=1).subtract(days=1)).in_weeks())
Пример #49
0
 def test_add_hours_negative(self):
     self.assertEqual(23, Pendulum(1975, 5, 21, 0, 0, 0).add(hours=-1).hour)
Пример #50
0
 def test_add_seconds_positive(self):
     self.assertEqual(1,
                      Pendulum(1975, 5, 21, 0, 0, 0).add(seconds=1).second)
Пример #51
0
 def test_add_hours_positive(self):
     self.assertEqual(1, Pendulum(1975, 5, 21, 0, 0, 0).add(hours=1).hour)
Пример #52
0
 def test_add_minutes_negative(self):
     self.assertEqual(59,
                      Pendulum(1975, 5, 21, 0, 0, 0).add(minutes=-1).minute)
Пример #53
0
 def test_diff_in_hours_positive(self):
     dt = Pendulum.create(2000, 1, 1)
     self.assertEqual(
         26,
         dt.diff(dt.copy().add(days=1).add(hours=2)).in_hours())
Пример #54
0
 def test_add_seconds_negative(self):
     self.assertEqual(59,
                      Pendulum(1975, 5, 21, 0, 0, 0).add(seconds=-1).second)
Пример #55
0
 def test_diff_in_hours_negative_with_sign(self):
     dt = Pendulum.create(2000, 1, 1)
     self.assertEqual(
         -22,
         dt.diff(dt.copy().subtract(days=1).add(hours=2), False).in_hours())
Пример #56
0
 def test_diff_in_hours_vs_default_now(self):
     with self.wrap_with_test_now(Pendulum.create(2012, 1, 15)):
         self.assertEqual(48,
                          Pendulum.now().subtract(days=2).diff().in_hours())
Пример #57
0
 def test_diff_in_weekdays_negative_no_sign(self):
     dt = Pendulum.create(2000, 1, 31)
     self.assertEqual(21, dt.diff(dt.start_of('month')).in_weekdays())
Пример #58
0
 def test_add_minutes_zero(self):
     self.assertEqual(0,
                      Pendulum(1975, 5, 21, 0, 0, 0).add(minutes=0).minute)
Пример #59
0
    def test_strftime(self):
        f = ClassicFormatter()
        d = Pendulum(2016, 8, 28)
        m = re.match('(.*)', '%_TTT')

        self.assertRaises(ValueError, f._strftime, d, m, 'fr')
Пример #60
0
 def test_custom_formatters(self):
     d = Pendulum(1975, 12, 25, 14, 15, 16, tzinfo='local')
     f = ClassicFormatter()
     self.assertEqual('Thursday 25th of December 1975 02:15:16 PM -05:00',
                      f.format(d, '%A %-d%_t of %B %Y %I:%M:%S %p %_z'))