当前位置: 首页 > news >正文

做网站会用到的代码单词wordpress网站好优化吗

做网站会用到的代码单词,wordpress网站好优化吗,专业的广州微网站建设,东营小程序开发制作目录 自定义日志记录器和内置的日志记录器 项目代码 项目目录树 自定义日志记录器 函数源代码 pytest中定义和覆盖日志记录信息 使用cli定义Logging 使用pytest.ini定义Logging 修改单个测试级别的日志 日志输出的重要性不言而喻#xff0c;不仅可以观测执行过程…目录 自定义日志记录器和内置的日志记录器 项目代码 项目目录树 自定义日志记录器 函数源代码 pytest中定义和覆盖日志记录信息 使用cli定义Logging 使用pytest.ini定义Logging 修改单个测试级别的日志 日志输出的重要性不言而喻不仅可以观测执行过程更重要的是在发生bug时可以快速的定位到问题所在 Pytest 是出色的测试框架但它不会自动显示 print 语句或日志的输出这在尝试调试失败的测试或了解流程时可能会成为问题。同时也是支持设置日志级别记录日志的到指定的文件 自定义日志记录器和内置的日志记录器 日志记录器的重要性在于可以随时设置不同级别的日志python内置的日志记录器分类了5类的日志级别。标识日志的严重级别 - 未设置 NOTSET0此级别捕获所有消息无论其严重性如何。- 调试 Debug10此级别用于任何可以帮助识别潜在问题的内容例如变量值或程序执行的步骤。- 信息 Infor20此级别用于确认事情是否按预期工作。- 警告 Warning 30此级别表示发生了意外情况或者在不久的将来可能会出现一些问题例如“磁盘空间不足”。但是该软件仍按预期工作。- 错误 Error40此级别表示存在更严重的问题导致软件无法执行某项功能。- 严重 Critical50此级别表示一个非常严重的错误可能会阻止程序继续运行。 自定义记录器允许您更方便地定义和处理这些级别从而进一步提高记录过程的精度和控制。 基于以上的目标本文旨在学习在python中设置 pytest 日志记录的过程。因此我们将探讨如何在 Pytest 中输出日志、如何禁用日志以及如何在单个测试级别更改日志级别。 项目代码 项目目录树 temperature_convertor ├── requirements.txt ├── src │   ├── __init__.py │   ├── __pycache__ │   │   ├── __init__.cpython-310.pyc │   │   └── custom_logger.cpython-310.pyc │   ├── custom_logger.py │   └── temp_convertor.py └── tests├── __init__.py└── test_temp_convertor.py 运行环境 % which python3 /usr/local/bin/python3 macOS 14.0自定义日志记录器 我们使用了一个自定义记录器配置为在级别显示日志消息。DEBUG通过创建自己的记录器您可以控制记录器的行为从而可以自定义它以满足您的特定要求。 src/custom_logger.py #!/usr/bin/env python # -*- coding: utf-8 -*- # This module is *** # Time : 2024/6/9 10:25 # Author : # function : # File : custom_logger.py import logging from enum import Enumclass LogLevel(Enum):DEBUG logging.DEBUGINFO logging.INFOWARNING logging.WARNINGERROR logging.ERRORCRITICAL logging.CRITICALdef console_logger(name:str, level:LogLevel) - logging.Logger:logger logging.getLogger(f__{name}__)logger.setLevel(level.value)# Create a console handler and set its levelconsole_handler logging.StreamHandler()console_handler.setLevel(level.value)# Set the formatter for the console handlerformatter logging.Formatter(%(asctime)s - %(name)s - %(levelname)s - %(message)s,datefmt%m/%d/%Y %I:%M:%S%p,)console_handler.setFormatter(formatter)# Add the console handler to the loggerlogger.addHandler(console_handler)return logger 函数源代码 这里通过温度的转换的的两个程序来调用设置的日志记录器。 src/temp_convertor.py #!/usr/bin/env python # -*- coding: utf-8 -*- # This module is *** # Time : 2024/6/9 10:25 # Author : # function : # File : temp_convertor.py from src.custom_logger import console_logger, LogLevelc_logger console_logger(nametemp_convertor, levelLogLevel.DEBUG)def fahrenheit_to_celsius(fahrenheit: float) - float:将华式摄氏度转换成摄氏度:param fahrenheit: 华式摄氏度:return:摄氏度c_logger.debug(fCoverting {fahrenheit}°F to Celsius.)celsius round((fahrenheit - 32) * 5 / 9, 2)c_logger.info(fResult: {celsius}°C)return celsiusdef celsius_to_fahrenheit(celsius:float) - float:将摄氏度转换成华式摄氏度:param celsius::return:c_logger.debug(fCoverting {celsius}°F to fahrenheit.)fahrenheit round((celsius * 9 / 5) 32, 2)c_logger.info(fResult: {fahrenheit}°C)return fahrenheitif __name__ __main__:print(fahrenheit_to_celsius(100))print(celsius_to_fahrenheit(100)) 执行上述代码检查代码基本逻辑正确 源代码没问题之后编写一些单元测试来验证实用程序的功能。temp_convertor tests/test_temp_convertor.py #!/usr/bin/env python # -*- coding: utf-8 -*- # This module is *** # Time : 2024/6/9 10:26 # Author : # function : # File : test_temp_convertor.pyfrom src.temp_convertor import fahrenheit_to_celsius, celsius_to_fahrenheitdef test_fahrenheit_to_celsius():assert fahrenheit_to_celsius(90) 32.22def test_celsius_to_fahrenheit():assert celsius_to_fahrenheit(19) 66.2pytest -s -v 学以致用之前的其他的文章介绍了pytest 插件pytest-sugagr. ❓❓❓ 如果您想覆盖源代码中设置的记录器仅用于测试怎么样也许是不同的格式不同的日志级别甚至输出到文件 pytest中定义和覆盖日志记录信息 Pytest 提供了多种方法来控制测试过程中的日志记录。其中一种方法是通过 CLI 定义和覆盖默认日志记录格式。 使用cli定义Logging pytest允许通过命令行参数的方式自定义日志输出格式和时间格式这种灵活性使您能够根据自己的具体需求调整日志显示方式。这里给出一个示例 pytest -s -v --log-cli-levelINFO --log-format%(asctime)s %(levelname)s %(message)s --log-date-format %Y-%m-%d %H:%M在该命令中log format设置日志消息本身的格式而log date format则设置日志消息中时间戳的格式。 这里看到已经将日志格式和级别重写为 Info信息 几倍。 使用pytest.ini定义Logging 如果使用持久化的配置可以在pytest.ini文件中设置日志参数。此文件允许您在默认情况下启用CLI日志记录并指定默认日志级别。以下是pytest.ini配置的示例 pytest.ini [pytest] log_cli true log_cli_level DEBUG log_cli_format %(asctime)s %(levelname)s %(message)s log_cli_date_format %Y-%m-%d %H:%M:%S在这里的配置中设置了几个参数如下所示 log_cli启用到控制台的日志记录。log_cli_level将日志级别设置为DEBUG。log_cli_format设置记录消息的格式。log_cli_date_format设置日志消息中时间戳的格式。 有关配置日志记录pytest.ini当然也可以使用pyproject.tmoltox.inisetup.config但是在Pytest中pytest.ini优先于其他几个配置文件即使为空也是如此。 运行此操作我们从关卡中获取每个测试的实时日志。DEBUG 上面定义了将日志输出到控制台还可以将其写入文件供以后查询或查看执行信息。需要将文件处理器添加到配置文件中的【日志记录器 】在Pytest.ini中 [pytest] log_file logs/temp_convertor_tests_run.log log_file_date_format %Y-%m-%d %H:%M:%S log_file_format %(asctime)s - %(name)s %(levelname)s %(message)s log_file_level DEBUG 执行pytest命令 pytest -s -v 此时会在tests目录下生成一个log文件 logs/temp_convertor_tests_run.log 2024-06-10 19:49:12 - __temp_convertor__ DEBUG Coverting 90°F to Celsius. 2024-06-10 19:49:12 - __temp_convertor__ INFO Result: 32.22°C 2024-06-10 19:49:12 - __temp_convertor__ DEBUG Coverting 19°F to fahrenheit. 2024-06-10 19:49:12 - __temp_convertor__ INFO Result: 66.2°C 当然还有其他的诸如禁用日志的功能但如果在测试中可能只有小概率会使用禁用日志的功能 pytest -s -v --show-captureno 修改单个测试级别的日志 某些时候可能希望更改特定测试的日志级别可能是为了调试该测试或减少日志输出中的干扰。Pytest 的夹具允许您执行此操作。 caplog 该夹具可以用于控制测试中的日志并与之交互使用caplog实现临时修改日志级别、捕获日志信息进行断言在测试用例中新增 def test_celsius_to_fahrenheit_caplog_ex(caplog):caplog.set_level(logging.DEBUG, logger__temp_conertor__)assert celsius_to_fahrenheit(19) 66.2print(printing caplong records...)for record in caplog.records:print(record.levelname, record.message) 再次执行 pytest -s -v 输出显示测试的日志级别设置为 INFO并且可以使用 和 访问和打印日志消息。record.levelnamerecord.message . 在其他的文章中给出了配置控制台和记录到日志文件时可以使用logger.config或pytest.ini来实现。logger.config是通过编程的方式设置日志记录器的配置而pytest.ini是通过配置文件来设置pytest的全局选项包括日志选项。 pytest.ini方式相对更简单和方便因为它可以在一个地方集中管理所有的pytest配置并且不需要在每个测试文件中重复设置日志配置。通过在pytest.ini文件中添加相应的日志配置选项可以轻松地控制日志的输出级别、格式和文件路径等。 然而如果你需要更细粒度的日志控制或者需要在不同的测试用例中使用不同的日志配置那么使用logger.config可能更合适。通过编程方式设置日志记录器可以根据具体的需求动态地调整日志配置。 综上所述如果你希望简单地配置日志并在整个项目中使用相同的日志设置推荐使用pytest.ini。如果你需要更灵活的日志控制或在不同的测试用例中有不同的日志需求可以考虑使用logger.config。最终的选择取决于你的具体需求和项目的特点。 # pytest.ini [pytest] log_cli True log_cli_level INFO log_cli_format %(asctime)s (%(levelname)s) | %(filename)s:%(lineno)s | %(message)s log_cli_date_format %Y-%m-%d %H:%M:%S log_file pytest_log.txt log_file_level INFO log_file_date_format %Y-%m-%d %H:%M:%S log_file_format %(asctime)s ( %(levelname)s ) %(filename)s:%(lineno)s | %(message)s 在上述代码中log_cli True表示在执行过程中启动实时监测日志log_cli_level指定了监测日志的等级显示log_cli_format定义了输出日志的显示格式log_cli_date_format指定了显示日志的时间格式。 log_file指定了存放日志文件的路径log_file_level表示文件中显示的日志等级log_file_date_format表示文件中显示的日志时间格式log_file_format表示文件中显示的日志格式。 要使用上述配置需要在测试用例文件中导入logging库并在需要记录日志的地方使用logging模块的方法输出日志信息例如: def test_demo_ini():import logginglogging.info(f这是测试用例的info)logging.warning(f这是测试用例的warning)logging.error(f这是测试用例的error)assert 1 控制台输出 在temp_convertor_tests_run.log日志文件中
文章转载自:
http://www.morning.bfybb.cn.gov.cn.bfybb.cn
http://www.morning.tfrlj.cn.gov.cn.tfrlj.cn
http://www.morning.pmjhm.cn.gov.cn.pmjhm.cn
http://www.morning.mm27.cn.gov.cn.mm27.cn
http://www.morning.hqbk.cn.gov.cn.hqbk.cn
http://www.morning.rlhgx.cn.gov.cn.rlhgx.cn
http://www.morning.nfzzf.cn.gov.cn.nfzzf.cn
http://www.morning.knpbr.cn.gov.cn.knpbr.cn
http://www.morning.dansj.com.gov.cn.dansj.com
http://www.morning.rqlbp.cn.gov.cn.rqlbp.cn
http://www.morning.cjnfb.cn.gov.cn.cjnfb.cn
http://www.morning.kdbbm.cn.gov.cn.kdbbm.cn
http://www.morning.knjj.cn.gov.cn.knjj.cn
http://www.morning.prkdl.cn.gov.cn.prkdl.cn
http://www.morning.ppwdh.cn.gov.cn.ppwdh.cn
http://www.morning.fwdln.cn.gov.cn.fwdln.cn
http://www.morning.dqpnd.cn.gov.cn.dqpnd.cn
http://www.morning.wfjyn.cn.gov.cn.wfjyn.cn
http://www.morning.kzpy.cn.gov.cn.kzpy.cn
http://www.morning.bwjgb.cn.gov.cn.bwjgb.cn
http://www.morning.yrhd.cn.gov.cn.yrhd.cn
http://www.morning.ylmxs.cn.gov.cn.ylmxs.cn
http://www.morning.gxcym.cn.gov.cn.gxcym.cn
http://www.morning.bqyb.cn.gov.cn.bqyb.cn
http://www.morning.mfmrg.cn.gov.cn.mfmrg.cn
http://www.morning.qtzwh.cn.gov.cn.qtzwh.cn
http://www.morning.pwgzh.cn.gov.cn.pwgzh.cn
http://www.morning.mxtjl.cn.gov.cn.mxtjl.cn
http://www.morning.rczrq.cn.gov.cn.rczrq.cn
http://www.morning.rrpsw.cn.gov.cn.rrpsw.cn
http://www.morning.xppj.cn.gov.cn.xppj.cn
http://www.morning.kzcz.cn.gov.cn.kzcz.cn
http://www.morning.wqbrg.cn.gov.cn.wqbrg.cn
http://www.morning.lbzgt.cn.gov.cn.lbzgt.cn
http://www.morning.rpdmj.cn.gov.cn.rpdmj.cn
http://www.morning.pfnrj.cn.gov.cn.pfnrj.cn
http://www.morning.rwzkp.cn.gov.cn.rwzkp.cn
http://www.morning.dmwck.cn.gov.cn.dmwck.cn
http://www.morning.nqgff.cn.gov.cn.nqgff.cn
http://www.morning.phtqr.cn.gov.cn.phtqr.cn
http://www.morning.srckl.cn.gov.cn.srckl.cn
http://www.morning.rhchr.cn.gov.cn.rhchr.cn
http://www.morning.xkmrr.cn.gov.cn.xkmrr.cn
http://www.morning.zfxrx.cn.gov.cn.zfxrx.cn
http://www.morning.ydnx.cn.gov.cn.ydnx.cn
http://www.morning.hmmnb.cn.gov.cn.hmmnb.cn
http://www.morning.ylph.cn.gov.cn.ylph.cn
http://www.morning.skrh.cn.gov.cn.skrh.cn
http://www.morning.yjfmj.cn.gov.cn.yjfmj.cn
http://www.morning.qklff.cn.gov.cn.qklff.cn
http://www.morning.qprtm.cn.gov.cn.qprtm.cn
http://www.morning.leyuhh.com.gov.cn.leyuhh.com
http://www.morning.qmmfr.cn.gov.cn.qmmfr.cn
http://www.morning.dlgjdg.cn.gov.cn.dlgjdg.cn
http://www.morning.mrlls.cn.gov.cn.mrlls.cn
http://www.morning.cjwkf.cn.gov.cn.cjwkf.cn
http://www.morning.dtzsm.cn.gov.cn.dtzsm.cn
http://www.morning.njntp.cn.gov.cn.njntp.cn
http://www.morning.hxwhyjh.com.gov.cn.hxwhyjh.com
http://www.morning.fcpjq.cn.gov.cn.fcpjq.cn
http://www.morning.xnflx.cn.gov.cn.xnflx.cn
http://www.morning.qgfy.cn.gov.cn.qgfy.cn
http://www.morning.tfrlj.cn.gov.cn.tfrlj.cn
http://www.morning.gnjkn.cn.gov.cn.gnjkn.cn
http://www.morning.lnbcg.cn.gov.cn.lnbcg.cn
http://www.morning.txlxr.cn.gov.cn.txlxr.cn
http://www.morning.krswn.cn.gov.cn.krswn.cn
http://www.morning.rlpmy.cn.gov.cn.rlpmy.cn
http://www.morning.ttxnj.cn.gov.cn.ttxnj.cn
http://www.morning.ypxyl.cn.gov.cn.ypxyl.cn
http://www.morning.lqws.cn.gov.cn.lqws.cn
http://www.morning.wjhpg.cn.gov.cn.wjhpg.cn
http://www.morning.mqfw.cn.gov.cn.mqfw.cn
http://www.morning.srmdr.cn.gov.cn.srmdr.cn
http://www.morning.jbxfm.cn.gov.cn.jbxfm.cn
http://www.morning.glkhx.cn.gov.cn.glkhx.cn
http://www.morning.kzbpx.cn.gov.cn.kzbpx.cn
http://www.morning.rwqk.cn.gov.cn.rwqk.cn
http://www.morning.pzrrq.cn.gov.cn.pzrrq.cn
http://www.morning.shinezoneserver.com.gov.cn.shinezoneserver.com
http://www.tj-hxxt.cn/news/241497.html

相关文章:

  • wordpress 整站模板钢筋网片价格
  • 视频剪辑素材免费网站有域名有空间如何做网站
  • 常德制作网站郑州哪家公司做网站好
  • 用cms创建自己带数据库的网站ftp安装wordpress主题
  • 已备案网站注册网页制作背景图
  • 网站建设的感想与建议福建省建设厅网站
  • 和平区网站建设企业网站建设一站通系统简单
  • 企业网站建设运营的灵魂是什么国内餐饮类网站欣赏
  • 如何微信做演讲视频网站做网站选择虚拟主机好是服务器
  • 网站开发五人分工朋友圈软文范例
  • ih5做自适应网站动漫做h免费网站
  • 建设银行个人网银网站浏览器大全
  • 学做衣服的网站有哪些做网站源码
  • 张掖网站建设0936e查关键词的排名工具
  • 电子商务网站开发实训总结报告养老院网站开发背景
  • 国内视频网站域名我为群众办实事工作总结
  • pc端购物网站建站网站建设制作设计营销公司杭州
  • 从事网站建设需要什么资质聊城做网站的公司
  • 做资讯网站网站优化包括哪些内容
  • 上市公司网站建设报价腾讯云建wordpress
  • 企业网站制作模板闵行10路
  • 网站怎么开发代码怎样提高网站的打开速度
  • 建设京东类的网站需要什么流程图网络推广工具有哪些
  • 东莞网上做公司网站有哪些新手做外链的网站
  • 建设银行北京市分行网站wordpress长文章分页插件
  • 站点地址和wordpress区别推广普通话作文300字
  • 郑州市汉狮做网站wordpress特定用户特定分类
  • 网站的栏目关键词北京设计院排名100强
  • 网站建设推广费会计分录广州网站推广建设
  • 马云为什么做网站网站优化合同