# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # 好东西[Python标准库](https://docs.python.org/3/library/#the-python-standard-library)
# Python标准库是Python安装的一部分，它包含各种各样的软件包，这些软件包在构建Python杰作时可能会有所帮助，本笔记本列出了一些常用的包装及其主要功能。

# ## [`datetime`](https://docs.python.org/3/library/datetime.html#module-datetime) 用于处理日期和时间

# +
import datetime as dt

local_now = dt.datetime.now()
print('local now: {}'.format(local_now))

utc_now = dt.datetime.utcnow()
print('utc now: {}'.format(utc_now))

# 你可以单独访问任何值:
print('{} {} {} {} {} {}'.format(local_now.year, local_now.month,
                                 local_now.day, local_now.hour,
                                 local_now.minute, local_now.second))

print('date: {}'.format(local_now.date()))
print('time: {}'.format(local_now.time()))
# -

# ### `strftime()`
# 对于字符串格式 `datetime`

# +
formatted1 = local_now.strftime('%Y/%m/%d-%H:%M:%S')
print(formatted1)

formatted2 = local_now.strftime('date: %Y-%m-%d time:%H:%M:%S')
print(formatted2)
# -

# ### `strptime()`
# 用于将日期时间字符串转换为日期对象

my_dt = dt.datetime.strptime('2000-01-01 10:00:00', '%Y-%m-%d %H:%M:%S')
print('my_dt: {}'.format(my_dt))

# ### [`timedelta`](https://docs.python.org/3/library/datetime.html#timedelta-objects)
# 适用于时差

# +
tomorrow = local_now + dt.timedelta(days=1)
print('tomorrow this time: {}'.format(tomorrow))

delta = tomorrow - local_now
print('tomorrow - now = {}'.format(delta))
print('days: {}, seconds: {}'.format(delta.days, delta.seconds))
print('total seconds: {}'.format(delta.total_seconds()))
# -

# ### 使用时区
# 首先确保 [`pytz`](http://pytz.sourceforge.net/) 已经安装.

import sys
# !{sys.executable} -m pip install pytz

# +
import datetime as dt
import pytz

naive_utc_now = dt.datetime.utcnow()
print('naive utc now: {}, tzinfo: {}'.format(naive_utc_now, naive_utc_now.tzinfo))

# 本地化原始日期时间
UTC_TZ = pytz.timezone('UTC')
utc_now = UTC_TZ.localize(naive_utc_now)
print('utc now: {}, tzinfo: {}'.format(utc_now, utc_now.tzinfo))

# 将本地化日期时间转换为不同的时区
PARIS_TZ = pytz.timezone('Europe/Paris')
paris_now = PARIS_TZ.normalize(utc_now)
print('Paris: {}, tzinfo: {}'.format(paris_now, paris_now.tzinfo))

NEW_YORK_TZ = pytz.timezone('America/New_York')
ny_now = NEW_YORK_TZ.normalize(utc_now)
print('New York: {}, tzinfo: {}'.format(ny_now, ny_now.tzinfo))
# -

# **注意**: 如果你的项目大量使用日期时间, 你可能需要看看外部的库，例如： [Pendulum](https://pendulum.eustace.io/docs/) 和 [Maya](https://github.com/kennethreitz/maya), 对于某些用例，这使得使用日期时间更加容易

# ## [`logging`](https://docs.python.org/3/library/logging.html#module-logging)

# +
import logging

# 为每个模块分别购买专用记录仪的便捷方法
logger = logging.getLogger(__name__)
logger.setLevel(logging.WARNING)

logger.debug('This is debug')
logger.info('This is info')
logger.warning('This is warning')
logger.error('This is error')
logger.critical('This is critical')
# -

# ### 记录异常
# 有一个整洁的 `exception` 方法在 `logging` 除用户定义的日志条目外，该模块该将自动记录堆栈跟踪. 

try:
    path_calculation = 1 / 0
except ZeroDivisionError:
    logging.exception('All went south in my calculation')

# ### 格式化日志条目

# +
import logging

# 仅Jupyter笔记本环境需要
from importlib import reload
reload(logging)

my_format = '%(asctime)s | %(name)-12s | %(levelname)-10s | %(message)s'
logging.basicConfig(format=my_format)

logger = logging.getLogger('MyLogger')

logger.warning('Something bad is going to happen')
logger.error('Uups, it already happened')
# -

# ### Logging to a file

# +
import os
import logging

# 仅Jupyter环境需要
from importlib import reload
reload(logging)

logger = logging.getLogger('MyFileLogger')

# 让我们为记录器定义一个file_handler
log_path = os.path.join(os.getcwd(), 'my_log.txt')
file_handler = logging.FileHandler(log_path)

#  而且定义好格式
formatter = logging.Formatter('%(asctime)s | %(name)-12s | %(levelname)-10s | %(message)s')
file_handler.setFormatter(formatter)

logger.addHandler(file_handler)


# logger.addHandler(logging.StreamHandler())

logger.warning('Oops something is going to happen')
logger.error('<NAME> visits our place')
# -

# ## [`random`](https://docs.python.org/3/library/random.html) 用于生成随机数

# +
import random

rand_int = random.randint(1, 100)
print('random integer between 1-100: {}'.format(rand_int))

rand = random.random()
print('random float between 0-1: {}'.format(rand))
# -

# 如果需要伪随机数，可以将`seed`设置为随机。这将重现输出（尝试多次运行单元）

# +
import random

random.seed(5)  # Setting the seed

# Let's print 10 random numbers
for _ in range(10):
    print(random.random())
# -

# ## [`re`](https://docs.python.org/3/library/re.html#module-re) 用于正则表达式

# ### 搜索事件

# +
import re

secret_code = 'qwret 8sfg12f5 fd09f_df'
# "r" 开头表示原始格式，将其与正则表达式模式一起使用
search_pattern = r'(g12)' 

match = re.search(search_pattern, secret_code)
print('match: {}'.format(match))
print('match.group(): {}'.format(match.group()))

numbers_pattern = r'[0-9]'
numbers_match = re.findall(numbers_pattern, secret_code)
print('numbers: {}'.format(numbers_match))
# -

# ### 验证变量

# +
import re

def validate_only_lower_case_letters(to_validate):
    pattern = r'^[a-z]+$'
    return bool(re.match(pattern, to_validate))

print(validate_only_lower_case_letters('thisshouldbeok'))
print(validate_only_lower_case_letters('thisshould notbeok'))
print(validate_only_lower_case_letters('Thisshouldnotbeok'))
print(validate_only_lower_case_letters('thisshouldnotbeok1'))
print(validate_only_lower_case_letters(''))
# -



StarcoderdataJupyterScriptsDedupFiltered