Python源码示例:arrow.now()
示例1
def jinja2_filter(app):
def format_datetime(value):
dt = arrow.get(value)
return dt.humanize()
app.jinja_env.filters["dt"] = format_datetime
@app.context_processor
def inject_stage_and_region():
return dict(
YEAR=arrow.now().year,
URL=URL,
SENTRY_DSN=SENTRY_FRONT_END_DSN,
VERSION=SHA1,
FIRST_ALIAS_DOMAIN=FIRST_ALIAS_DOMAIN,
)
示例2
def notify_premium_end():
"""sent to user who has canceled their subscription and who has their subscription ending soon"""
for sub in Subscription.query.filter(Subscription.cancelled == True).all():
if (
arrow.now().shift(days=3).date()
> sub.next_bill_date
>= arrow.now().shift(days=2).date()
):
user = sub.user
LOG.d(f"Send subscription ending soon email to user {user}")
send_email(
user.email,
f"Your subscription will end soon {user.name}",
render(
"transactional/subscription-end.txt",
user=user,
next_bill_date=sub.next_bill_date.strftime("%Y-%m-%d"),
),
render(
"transactional/subscription-end.html",
user=user,
next_bill_date=sub.next_bill_date.strftime("%Y-%m-%d"),
),
)
示例3
def handle_sender_email(envelope: Envelope):
filename = (
arrow.now().format("YYYY-MM-DD_HH-mm-ss") + "_" + random_string(10) + ".eml"
)
filepath = os.path.join(SENDER_DIR, filename)
with open(filepath, "wb") as f:
f.write(envelope.original_content)
LOG.d("Write email to sender at %s", filepath)
msg = email.message_from_bytes(envelope.original_content)
orig = get_orig_message_from_bounce(msg)
if orig:
LOG.warning(
"Original message %s -> %s saved at %s", orig["From"], orig["To"], filepath
)
return "250 email to sender accepted"
示例4
def require_api_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
if current_user.is_authenticated:
g.user = current_user
else:
api_code = request.headers.get("Authentication")
api_key = ApiKey.get_by(code=api_code)
if not api_key:
return jsonify(error="Wrong api key"), 401
# Update api key stats
api_key.last_used = arrow.now()
api_key.times += 1
db.session.commit()
g.user = api_key.user
return f(*args, **kwargs)
return decorated
示例5
def _lifetime_or_active_subscription(self) -> bool:
"""True if user has lifetime licence or active subscription"""
if self.lifetime:
return True
sub: Subscription = self.get_subscription()
if sub:
return True
apple_sub: AppleSubscription = AppleSubscription.get_by(user_id=self.id)
if apple_sub and apple_sub.is_valid():
return True
manual_sub: ManualSubscription = ManualSubscription.get_by(user_id=self.id)
if manual_sub and manual_sub.end_at > arrow.now():
return True
return False
示例6
def can_upgrade(self):
"""User who has lifetime licence or giveaway manual subscriptions can decide to upgrade to a paid plan"""
sub: Subscription = self.get_subscription()
# user who has canceled can also re-subscribe
if sub and not sub.cancelled:
return False
apple_sub: AppleSubscription = AppleSubscription.get_by(user_id=self.id)
if apple_sub and apple_sub.is_valid():
return False
manual_sub: ManualSubscription = ManualSubscription.get_by(user_id=self.id)
# user who has giveaway premium can decide to upgrade
if (
manual_sub
and manual_sub.end_at > arrow.now()
and not manual_sub.is_giveaway
):
return False
return True
示例7
def get_subscription(self) -> "Subscription":
"""return *active* subscription
TODO: support user unsubscribe and re-subscribe
"""
sub = Subscription.get_by(user_id=self.id)
# TODO: sub is active only if sub.next_bill_date > now
# due to a bug on next_bill_date, wait until next month (May 8)
# when all next_bill_date are correctly updated to add this check
if sub and sub.cancelled:
# sub is active until the next billing_date + 1
if sub.next_bill_date >= arrow.now().shift(days=-1).date():
return sub
# past subscription, user is considered not having a subscription = free plan
else:
return None
else:
return sub
示例8
def greylisting_needed_for_alias(alias: Alias) -> bool:
min_time = arrow.now().shift(minutes=-1)
# get the nb of activity on this alias
nb_activity = (
db.session.query(EmailLog)
.join(Contact, EmailLog.contact_id == Contact.id)
.filter(Contact.alias_id == alias.id, EmailLog.created_at > min_time,)
.group_by(EmailLog.id)
.count()
)
if nb_activity > MAX_ACTIVITY_DURING_MINUTE_PER_ALIAS:
LOG.d(
"Too much forward on alias %s. Nb Activity %s", alias, nb_activity,
)
return True
return False
示例9
def greylisting_needed_for_mailbox(alias: Alias) -> bool:
min_time = arrow.now().shift(minutes=-1)
# get nb of activity on this mailbox
nb_activity = (
db.session.query(EmailLog)
.join(Contact, EmailLog.contact_id == Contact.id)
.join(Alias, Contact.alias_id == Alias.id)
.filter(Alias.mailbox_id == alias.mailbox_id, EmailLog.created_at > min_time,)
.group_by(EmailLog.id)
.count()
)
if nb_activity > MAX_ACTIVITY_DURING_MINUTE_PER_MAILBOX:
LOG.d(
"Too much forward on mailbox %s, alias %s. Nb Activity %s",
alias.mailbox,
alias,
nb_activity,
)
return True
return False
示例10
def current(self, value):
if not value or 'project' not in value:
self._current = {}
if self._old_state is None:
self._old_state = {}
return
start = value.get('start', arrow.now())
if not isinstance(start, arrow.Arrow):
start = self._parse_date(start)
self._current = {
'project': value['project'],
'start': start,
'tags': value.get('tags') or []
}
if self._old_state is None:
self._old_state = self._current
示例11
def stop(self, stop_at=None):
if not self.is_started:
raise WatsonError("No project started.")
old = self.current
if stop_at is None:
# One cannot use `arrow.now()` as default argument. Default
# arguments are evaluated when a function is defined, not when its
# called. Since there might be huge delays between defining this
# stop function and calling it, the value of `stop_at` could be
# outdated if defined using a default argument.
stop_at = arrow.now()
if old['start'] > stop_at:
raise WatsonError('Task cannot end before it starts.')
if stop_at > arrow.now():
raise WatsonError('Task cannot end in the future.')
frame = self.frames.add(
old['project'], old['start'], stop_at, tags=old['tags']
)
self.current = None
return frame
示例12
def _parse_multiformat(self, value):
date = None
for fmt in (None, 'HH:mm:ss', 'HH:mm'):
try:
if fmt is None:
date = arrow.get(value)
else:
date = arrow.get(value, fmt)
date = arrow.now().replace(
hour=date.hour,
minute=date.minute,
second=date.second
)
break
except (ValueError, TypeError):
pass
return date
示例13
def test_start_project_at(watson):
now = arrow.now()
watson.start('foo', start_at=now)
watson.stop()
# Task can't start before the previous task ends
with pytest.raises(WatsonError):
time_str = '1970-01-01T00:00'
time_obj = arrow.get(time_str)
watson.start('foo', start_at=time_obj)
# Task can't start in the future
with pytest.raises(WatsonError):
time_str = '2999-12-31T23:59'
time_obj = arrow.get(time_str)
watson.start('foo', start_at=time_obj)
assert watson.frames[-1].start == now
# stop
示例14
def test_stop_started_project_at(watson):
watson.start('foo')
now = arrow.now()
# Task can't end before it starts
with pytest.raises(WatsonError):
time_str = '1970-01-01T00:00'
time_obj = arrow.get(time_str)
watson.stop(stop_at=time_obj)
# Task can't end in the future
with pytest.raises(WatsonError):
time_str = '2999-12-31T23:59'
time_obj = arrow.get(time_str)
watson.stop(stop_at=time_obj)
watson.stop(stop_at=now)
assert watson.frames[-1].stop == now
# cancel
示例15
def parse_date(date):
"""
Parses a date and returns it as YYYYMMDD
If not parsable, returns False
Args:
date
"""
try:
parsed_date = parse(str(date))
except ValueError:
return False
if (arrow.now().date() - parsed_date.date()).days < 0:
raise ValueError("Date cannot be set in the future")
return parsed_date.strftime("%Y%m%d")
#============#
# Validators #
#============#
示例16
def twitter_sentiment(symbol, max_tweets):
""""""
now = arrow.now()
dates = now.date()
print(dates)
time = now.time()
print(time)
sentiment_variables = dates_to_sentiment(dates, symbol, max_tweets)
return sentiment_variables
######################################### Split Trading Pair into 'to' and 'from' ###########
示例17
def humanize_dates(p_due=None, p_start=None, p_creation=None):
"""
Returns string with humanized versions of p_due, p_start and p_creation.
Examples:
- all dates: "16 days ago, due in a month, started 2 days ago"
- p_due and p_start: "due in a month, started 2 days ago"
- p_creation and p_due: "16 days ago, due in a month"
"""
dates_list = []
if p_creation:
dates_list.append(humanize_date(p_creation))
if p_due:
dates_list.append('due ' + humanize_date(p_due))
if p_start:
now = arrow.now().date()
dates_list.append('{} {}'.format(
'started' if p_start <= now else 'starts',
humanize_date(p_start)
))
return ', '.join(dates_list)
示例18
def process_item(self, item, spider):
if not self.check(item['ip'], item['port']):
session = Session()
try:
session.add(
Ip(ip=item['ip'],
port=item['port'],
http_type=item['http_type'],
country=item['country'],
create_time=arrow.now().datetime,
update_time=arrow.now().datetime))
session.commit()
except Exception as e:
logging.exception(e)
session.rollback()
finally:
session.close()
示例19
def read_record(self, days=0, date_string=None, redownload=False):
date = arrow.now().shift(days=int(days))
if date_string is not None:
date = arrow.get(date_string)
basename = date.format("YYYY-MM-DD") + ".json"
file_path = self.record_path + basename
record = self.read_file(file_path)
if record is None or redownload is True:
year_str = date.format("YYYY")
try:
self.s3_client.download_file("kino-records", f"{year_str}/{basename}", file_path)
except BaseException:
return {}
return self.read_file(file_path)
示例20
def is_between(start_time: tuple, end_time: tuple, now=None) -> bool:
if start_time is None and end_time is None:
return True
if now is None:
now = datetime.datetime.now()
start_h, start_m = start_time
end_h, end_m = end_time
if end_h == 24 and end_m == 0:
end_h = 23
end_m = 59
start = now.replace(hour=start_h, minute=start_m, second=0, microsecond=0)
end = now.replace(hour=end_h, minute=end_m, second=0, microsecond=0)
if start <= now <= end:
return True
else:
return False
示例21
def is_today_day_of_week(day_of_week: list) -> bool:
day_of_week = list(map(lambda x: int(x), day_of_week))
if day_of_week == [0]:
return True
now = arrow.now()
today_day_of_week = now.weekday() + 1
if today_day_of_week in day_of_week:
return True
elif len(day_of_week) == 1:
value = day_of_week[0]
if value == 8 and ArrowUtil.is_weekday():
return True
elif value == 9 and not ArrowUtil.is_weekday():
return True
else:
return False
else:
return False
示例22
def check_sleep_time(self):
self.slackbot.send_message(text=MsgResource.GOOD_MORNING)
record = self.data_handler.read_record()
activity = record.get("activity", {})
go_to_bed_time = arrow.get(activity.get("go_to_bed", None))
wake_up_time = arrow.now()
self.data_handler.edit_record_with_category(
"activity", ("wake_up", str(wake_up_time))
)
sleep_time = (wake_up_time - go_to_bed_time).seconds / 60 / 60
sleep_time = round(sleep_time * 100) / 100
self.data_handler.edit_record(("Sleep", str(sleep_time)))
self.slackbot.send_message(
text=MsgResource.SLEEP_TIME(
bed_time=go_to_bed_time.format("HH:mm"),
wakeup_time=wake_up_time.format("HH:mm"),
diff_h=str(sleep_time),
)
)
示例23
def check_go_to_bed(self):
go_to_bed_time = arrow.now()
self.data_handler.edit_record_with_category(
"activity", ("go_to_bed", str(go_to_bed_time))
)
self.slackbot.send_message(text=MsgResource.GOOD_NIGHT)
# slack presence issue
# state = State()
# state.check()
# presence_log = state.current[state.SLEEP]
# if presence_log['presence'] == 'away':
# go_to_bed_time = arrow.get(presence_log['time'])
# self.data_handler.edit_record_with_category(
# 'activity', ('go_to_bed', str(go_to_bed_time)))
示例24
def send_message(username):
data = request.form
author = data["author"]
message = data["message"]
date_sent = arrow.now().timestamp
conversation_db_path = get_conversation_db_path_for_users({"user_one": author, "user_two": username})
conversation = Conversation(conversation_db_path)
conversation.add_message(author, message, date_sent)
return jsonify({
"success": True
})
示例25
def __init__(self, master, user_one, user_two):
super().__init__()
self.master = master
self.user_one = user_one
self.user_two = user_two
self.requester = Requester()
self.running = True
self.last_checked_time = arrow.now().timestamp
示例26
def run(self):
while self.running:
new_messages = self.requester.get_new_messages(self.last_checked_time, self.user_one, self.user_two)
self.last_checked_time = arrow.now().timestamp
for message in new_messages['messages']:
self.master.receive_message(message["author"], message["message"])
time.sleep(3)
del self.master.listening_thread
return
示例27
def send_message(username):
data = request.form
author = data["author"]
message = data["message"]
date_sent = arrow.now().timestamp
conversation_db_path = get_conversation_db_path_for_users({"user_one": author, "user_two": username})
conversation = Conversation(conversation_db_path)
conversation.add_message(author, message, date_sent)
return jsonify({
"success": True
})
示例28
def notify_trial_end():
for user in User.query.filter(
User.activated == True, User.trial_end.isnot(None), User.lifetime == False
).all():
if user.in_trial() and arrow.now().shift(
days=3
) > user.trial_end >= arrow.now().shift(days=2):
LOG.d("Send trial end email to user %s", user)
send_trial_end_soon_email(user)
示例29
def delete_refused_emails():
for refused_email in RefusedEmail.query.filter(RefusedEmail.deleted == False).all():
if arrow.now().shift(days=1) > refused_email.delete_at >= arrow.now():
LOG.d("Delete refused email %s", refused_email)
if refused_email.path:
s3.delete(refused_email.path)
s3.delete(refused_email.full_report_path)
# do not set path and full_report_path to null
# so we can check later that the files are indeed deleted
refused_email.deleted = True
db.session.commit()
LOG.d("Finish delete_refused_emails")
示例30
def notify_manual_sub_end():
for manual_sub in ManualSubscription.query.all():
need_reminder = False
if arrow.now().shift(days=14) > manual_sub.end_at > arrow.now().shift(days=13):
need_reminder = True
elif arrow.now().shift(days=4) > manual_sub.end_at > arrow.now().shift(days=3):
need_reminder = True
if need_reminder:
user = manual_sub.user
LOG.debug("Remind user %s that their manual sub is ending soon", user)
send_email(
user.email,
f"Your trial will end soon {user.name}",
render(
"transactional/manual-subscription-end.txt",
name=user.name,
user=user,
manual_sub=manual_sub,
),
render(
"transactional/manual-subscription-end.html",
name=user.name,
user=user,
manual_sub=manual_sub,
),
)