公用事业私人收藏
shinyutils的Python项目详细描述
Shinyutils
用于常见任务的各种实用程序。:火花::火花::火花:
设置
用pip
安装。
pip install shinyutils
matwrap
包装在matplotlib
和seaborn
周围。
使用量
fromshinyutilsimportMatWrapasmw# do not import `matplotlib`, `seaborn`mw.configure()# this should be called before importing any packages that import matplotlibfig=mw.plt().figure()ax=fig.add_subplot(111)# `ax` can be used normally now# Use class methods in `MatWrap` to access `matplotlib`/`seaborn` functions.mw.mpl()# returns `matplotlib` modulemw.plt()# returns `matplotlib.pyplot` modulemw.sns()# returns `seaborn` module
配置
使用mw.configure
配置绘图。参数(默认值用[]表示)是:
context:seaborncontext([paper]/poster/talk/notebook)style:seabornstyle(white/whitegrid/dark/darkgrid/[ticks])font:anyfontavailabletofontspec(default[LatinModernRoman])latex_pkgs:additionallatexpackagestobeincludedbeforedefaults**rc_extra:matplotlibrcparameterstooverridedefaults
subcls
处理子类的实用函数。
功能
get_subclasses(cls)
:返回cls
的所有子类的列表。get_subclass_names(cls)
:返回cls
的所有子类的名称列表。get_subclass_from_name(base_cls, cls_name)
:返回名为cls_name
的base_cls
的子类。build_subclass_object(base_cls, cls_name, kwargs)
:返回使用kwargs
初始化的get_subclass_from_name
的实例。
argp
用于参数分析的实用程序。
LazyHelpFormatter
HelpFormatter
具有正常的默认值和颜色(由crayons
提供)!要使用,只需在创建ArgumentParser
实例时传递formatter_class=LazyHelpFormatter
。
arg_parser=ArgumentParser(formatter_class=LazyHelpFormatter)sub_parsers=arg_parser.add_subparsers(dest="cmd")sub_parsers.required=True# `formatter_class` needs to be set for sub parsers as well.cmd1_parser=sub_parsers.add_parser("cmd1",formatter_class=LazyHelpFormatter)
comma_separated_ints
ArgumentParser
表示以逗号分隔的int列表的类型(例如1,2,3,4
)。
arg_parser.add_argument("--csi",type=comma_separated_ints)
OutputFileType
ArgumentParser
表示输出文件的类型。如果需要,将创建文件的基本目录。返回的值是一个文件对象。
OutputDirectoryType
ArgumentParser
表示输出目录的类型。如果不存在,则创建目录。
ClassType
ArgumentParser
表示给定基类的子类的类型。返回的值是一个类。
classBase:passclassA(Base):passclassB(Base):passarg_parser.add_argument("--cls",type=ClassType(Base),default=A)
shiny_arg_parser
ArgumentParser
带有lazyhelpformatter和logging参数的对象。
logng
日志工具。
build_log_argp
创建带有日志参数的参数组。
>>> arg_parser = ArgumentParser()
>>> build_log_argp(arg_parser)
>>> arg_parser.print_help()
usage: test.py [-h] [--log-level {DEBUG,INFO,WARNING,ERROR,CRITICAL}]
optional arguments:
-h, --help show this help message and exit
--log-level {DEBUG,INFO,WARNING,ERROR,CRITICAL}
conf_logging
配置全局日志记录(并添加颜色!)使用由ArgumentParser.parse_args
返回的参数。log_level
可以用关键字参数覆盖。如果输出不是tty,则不启用颜色。
args=arg_parser.parse_args()conf_logging(args)conf_logging(args,log_level="INFO")# override `log_level`
默认情况下,包在信息级别配置日志记录。