本文整理汇总了Python中pendulum.DateTime方法的典型用法代码示例。如果您正苦于以下问题:Python pendulum.DateTime方法的具体用法?Python pendulum.DateTime怎么用?Python pendulum.DateTime使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pendulum
的用法示例。
在下文中一共展示了pendulum.DateTime方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: configure_adapters
# 需要导入模块: import pendulum [as 别名]
# 或者: from pendulum import DateTime [as 别名]
def configure_adapters():
""" Register Adapters and DB Converters """
from pendulum import DateTime as Pendulum
try:
from sqlite3 import register_adapter
register_adapter(Pendulum, lambda val: val.isoformat(' '))
except ImportError:
pass
try:
import MySQLdb.converters
MySQLdb.converters.conversions[Pendulum] = MySQLdb.converters.DateTime2literal
except ImportError:
pass
try:
import pymysql.converters
pymysql.converters.conversions[Pendulum] = pymysql.converters.escape_datetime
except ImportError:
pass
示例2: ensure_finished_tasks
# 需要导入模块: import pendulum [as 别名]
# 或者: from pendulum import DateTime [as 别名]
def ensure_finished_tasks(self, dag, execution_date: pendulum.DateTime, session: Session):
"""
This method makes sure finished_tasks is populated if it's currently None.
This is for the strange feature of running tasks without dag_run.
:param dag: The DAG for which to find finished tasks
:type dag: airflow.models.DAG
:param execution_date: The execution_date to look for
:param session: Database session to use
:return: A list of all the finished tasks of this DAG and execution_date
:rtype: list[airflow.models.TaskInstance]
"""
if self.finished_tasks is None:
self.finished_tasks = dag.get_task_instances(
start_date=execution_date,
end_date=execution_date,
state=State.finished() + [State.UPSTREAM_FAILED],
session=session,
)
return self.finished_tasks
示例3: test_accuracy
# 需要导入模块: import pendulum [as 别名]
# 或者: from pendulum import DateTime [as 别名]
def test_accuracy():
dt1 = pendulum.DateTime(2000, 11, 20)
dt2 = pendulum.DateTime(2000, 11, 25)
dt3 = pendulum.DateTime(2016, 11, 5)
p1 = pendulum.period(dt1, dt3)
p2 = pendulum.period(dt2, dt3)
assert p1.years == 15
assert p1.in_years() == 15
assert p1.months == 11
assert p1.in_months() == 191
assert p1.days == 5829
assert p1.remaining_days == 2
assert p1.in_days() == 5829
assert p2.years == 15
assert p2.in_years() == 15
assert p2.months == 11
assert p2.in_months() == 191
assert p2.days == 5824
assert p2.remaining_days == 4
assert p2.in_days() == 5824
示例4: test_timedelta_behavior
# 需要导入模块: import pendulum [as 别名]
# 或者: from pendulum import DateTime [as 别名]
def test_timedelta_behavior():
dt1 = pendulum.DateTime(2000, 11, 20, 1)
dt2 = pendulum.DateTime(2000, 11, 25, 2)
dt3 = pendulum.DateTime(2016, 11, 5, 3)
p1 = pendulum.period(dt1, dt3)
p2 = pendulum.period(dt2, dt3)
it1 = p1.as_timedelta()
it2 = p2.as_timedelta()
assert it1.total_seconds() == p1.total_seconds()
assert it2.total_seconds() == p2.total_seconds()
assert it1.days == p1.days
assert it2.days == p2.days
assert it1.seconds == p1.seconds
assert it2.seconds == p2.seconds
assert it1.microseconds == p1.microseconds
assert it2.microseconds == p2.microseconds
示例5: test_should_works_with_datetime_formatter
# 需要导入模块: import pendulum [as 别名]
# 或者: from pendulum import DateTime [as 别名]
def test_should_works_with_datetime_formatter(self):
decorator = format_parameters({"param_a": format_datetime})
endpoint = mock.MagicMock()
decorated_endpoint = decorator(endpoint)
decorated_endpoint(param_a='2020-01-01T0:0:00+00:00')
endpoint.assert_called_once_with(param_a=DateTime(2020, 1, 1, 0, tzinfo=Timezone('UTC')))
示例6: _test_previous_dates_setup
# 需要导入模块: import pendulum [as 别名]
# 或者: from pendulum import DateTime [as 别名]
def _test_previous_dates_setup(schedule_interval: Union[str, datetime.timedelta, None],
catchup: bool, scenario: List[str]) -> list:
dag_id = 'test_previous_dates'
dag = models.DAG(dag_id=dag_id, schedule_interval=schedule_interval, catchup=catchup)
task = DummyOperator(task_id='task', dag=dag, start_date=DEFAULT_DATE)
def get_test_ti(session, execution_date: pendulum.DateTime, state: str) -> TI:
dag.create_dagrun(
run_type=DagRunType.SCHEDULED,
state=state,
execution_date=execution_date,
start_date=pendulum.now('UTC'),
session=session
)
ti = TI(task=task, execution_date=execution_date)
ti.set_state(state=State.SUCCESS, session=session)
return ti
with create_session() as session: # type: Session
date = cast(pendulum.DateTime, pendulum.parse('2019-01-01T00:00:00+00:00'))
ret = []
for idx, state in enumerate(scenario):
new_date = date.add(days=idx)
ti = get_test_ti(session, new_date, state)
ret.append(ti)
return ret
示例7: test_pendulum_template_dates
# 需要导入模块: import pendulum [as 别名]
# 或者: from pendulum import DateTime [as 别名]
def test_pendulum_template_dates(self):
dag = models.DAG(
dag_id='test_pendulum_template_dates', schedule_interval='0 12 * * *',
start_date=timezone.datetime(2016, 6, 1, 0, 0, 0))
task = DummyOperator(task_id='test_pendulum_template_dates_task', dag=dag)
ti = TI(task=task, execution_date=timezone.utcnow())
template_context = ti.get_template_context()
self.assertIsInstance(template_context["execution_date"], pendulum.DateTime)
self.assertIsInstance(template_context["next_execution_date"], pendulum.DateTime)
self.assertIsInstance(template_context["prev_execution_date"], pendulum.DateTime)
示例8: get_one
# 需要导入模块: import pendulum [as 别名]
# 或者: from pendulum import DateTime [as 别名]
def get_one(cls,
execution_date: pendulum.DateTime,
key: Optional[str] = None,
task_id: Optional[Union[str, Iterable[str]]] = None,
dag_id: Optional[Union[str, Iterable[str]]] = None,
include_prior_dates: bool = False,
session: Session = None) -> Optional[Any]:
"""
Retrieve an XCom value, optionally meeting certain criteria. Returns None
of there are no results.
:param execution_date: Execution date for the task
:type execution_date: pendulum.datetime
:param key: A key for the XCom. If provided, only XComs with matching
keys will be returned. To remove the filter, pass key=None.
:type key: str
:param task_id: Only XComs from task with matching id will be
pulled. Can pass None to remove the filter.
:type task_id: str
:param dag_id: If provided, only pulls XCom from this DAG.
If None (default), the DAG of the calling task is used.
:type dag_id: str
:param include_prior_dates: If False, only XCom from the current
execution_date are returned. If True, XCom from previous dates
are returned as well.
:type include_prior_dates: bool
:param session: database session
:type session: sqlalchemy.orm.session.Session
"""
result = cls.get_many(execution_date=execution_date,
key=key,
task_ids=task_id,
dag_ids=dag_id,
include_prior_dates=include_prior_dates,
session=session).first()
if result:
return result.value
return None
示例9: get_previous_start_date
# 需要导入模块: import pendulum [as 别名]
# 或者: from pendulum import DateTime [as 别名]
def get_previous_start_date(
self,
state: Optional[str] = None,
session: Session = None
) -> Optional[pendulum.DateTime]:
"""
The start date from property previous_ti_success.
:param state: If passed, it only take into account instances of a specific state.
"""
self.log.debug("previous_start_date was called")
prev_ti = self.get_previous_ti(state=state, session=session)
return prev_ti and prev_ti.start_date
示例10: previous_start_date_success
# 需要导入模块: import pendulum [as 别名]
# 或者: from pendulum import DateTime [as 别名]
def previous_start_date_success(self) -> Optional[pendulum.DateTime]:
"""
This attribute is deprecated.
Please use `airflow.models.taskinstance.TaskInstance.get_previous_start_date` method.
"""
warnings.warn(
"""
This attribute is deprecated.
Please use `airflow.models.taskinstance.TaskInstance.get_previous_start_date` method.
""",
DeprecationWarning,
stacklevel=2,
)
return self.get_previous_start_date(state=State.SUCCESS)
示例11: _convert
# 需要导入模块: import pendulum [as 别名]
# 或者: from pendulum import DateTime [as 别名]
def _convert(self, dt): # type: (_D) -> _D
if dt.tzinfo is self:
return self._normalize(dt, dst_rule=POST_TRANSITION)
if not isinstance(dt.tzinfo, Timezone):
return dt.astimezone(self)
stamp = timestamp(dt)
if isinstance(dt.tzinfo, FixedTimezone):
offset = dt.tzinfo.offset
else:
transition = dt.tzinfo._lookup_transition(stamp)
offset = transition.ttype.offset
if stamp < transition.local and transition.previous is not None:
if (
transition.previous.is_ambiguous(stamp)
and getattr(dt, "fold", 1) == 0
):
pass
else:
offset = transition.previous.ttype.offset
stamp -= offset
transition = self._lookup_transition(stamp, is_utc=True)
if stamp < transition.at and transition.previous is not None:
transition = transition.previous
offset = transition.ttype.offset
stamp += offset
fold = int(not transition.ttype.is_dst())
kwargs = {"tzinfo": self}
if _HAS_FOLD or isinstance(dt, pendulum.DateTime):
kwargs["fold"] = fold
return dt.__class__(*local_time(stamp, 0, dt.microsecond), **kwargs)
示例12: test
# 需要导入模块: import pendulum [as 别名]
# 或者: from pendulum import DateTime [as 别名]
def test(mock): # type: (pendulum.DateTime) -> Iterator[None]
set_test_now(mock)
try:
yield
finally:
set_test_now()
示例13: set_test_now
# 需要导入模块: import pendulum [as 别名]
# 或者: from pendulum import DateTime [as 别名]
def set_test_now(test_now=None): # type: (Optional[pendulum.DateTime]) -> None
pendulum._TEST_NOW = test_now
示例14: get_test_now
# 需要导入模块: import pendulum [as 别名]
# 或者: from pendulum import DateTime [as 别名]
def get_test_now(): # type: () -> Optional[pendulum.DateTime]
return pendulum._TEST_NOW
示例15: format
# 需要导入模块: import pendulum [as 别名]
# 或者: from pendulum import DateTime [as 别名]
def format(
self, dt, fmt, locale=None
): # type: (pendulum.DateTime, str, typing.Optional[typing.Union[str, Locale]]) -> str
"""
Formats a DateTime instance with a given format and locale.
:param dt: The instance to format
:type dt: pendulum.DateTime
:param fmt: The format to use
:type fmt: str
:param locale: The locale to use
:type locale: str or Locale or None
:rtype: str
"""
if not locale:
locale = pendulum.get_locale()
locale = Locale.load(locale)
result = self._FORMAT_RE.sub(
lambda m: m.group(1)
if m.group(1)
else m.group(2)
if m.group(2)
else self._format_token(dt, m.group(3), locale),
fmt,
)
return decode(result)