从文件中摘录标记样式注释
excerpts的Python项目详细描述
简介
读过或写过包含分区注释的源文件吗? 尤其是在科学和/或数据分析脚本中,我很喜欢
### Collect Data
或
###### Remove Outliers###
甚至
### 2.1 Descriptive Statistics
(RStudio,用于 programming language R有 甚至想出他们自己的 code sectioning and folding feature 需要像
# Hypothesis Testing ----
)
如果这些注释是标记样式的节注释,我们可以将其摘录并 设置目录。
第一个示例
假设您有以下代码:
#!/usr/bin/env python3 # #% A Tutorial Introduction # ##% Getting Started # no need to import anything print('hello, world') # ###% The First Python Function def main(): print('hello, world') main() # ##% Variables and Arithmetic Expressions print('some code') # a comment print('more code')
我们可以摘录用“%”标记的注释:
# A Tutorial Introduction ## Getting Started ### The First Python Function ## Variables and Arithmetic Expressions
再详细一点
假设您有一个文件“tests/files/some_file.txt”正在读取:
#######% % All About Me #######% % Me ####### The above defines a pandoc markdown header. ####### This is more text that will not be extracted. #######% **This** is an example of a markdown paragraph: markdown #######% recognizes only six levels of heading, so we use seven or #######% more levels to mark "normal" text. #######% Here you can use the full markdown #######% [syntax](http://daringfireball.net/projects/markdown/syntax). #######% *Note* the trailing line: markdown needs an empty line to end #######% a paragraph. #######% #% A section ##% A subsection ### Not a subsubsection but a plain comment. ############% Another markdown paragraph. ############% ####### More text that will not be extracted.
然后通过
importexcerptsfile_name='tests/files/some_file.txt'withopen(file_name)asinfile:lines=infile.readlines()excerpted=excerpts.excerpt(lines=lines,comment_character="#",magic_character="%")
print(''.join(str(p)forpinexcerpted))
给出
% All About Me % Me **This** is an example of a markdown paragraph: markdown recognizes only six levels of heading, so we use seven or more levels to mark "normal" text. Here you can use the full markdown [syntax](http://daringfireball.net/projects/markdown/syntax). *Note* the trailing line: markdown needs an empty line to end a paragraph. # A section ## A subsection Another markdown paragraph.
使用文件
如果要从文件中摘录并对结果运行pandoc,可以使用
excerpts.excerpts(file_name=file_name,comment_character="#",magic_character="%",output_path="output",run_pandoc=True,pandoc_formats="html")
产生 this file.
命令行界面
摘要有一个命令行接口,您可以从 操作系统的命令行,而不是python3:
usage: excerpts [-h] [-O OUTPUT_PATH] [-o POSTFIX] [-e PREFIX] [-c COMMENT_CHARACTER] [-m MAGIC_CHARACTER] [-v] [-x] [-p] [-n] [-l] [--no-latex] [--formats PANDOC_FORMATS] [--no-pep8] file excerpt markdown-style comments from a file to markdown and
PEP8
PEP8要求“块注释的每一行以一个和一个空格开头”。 摘要通过删除一个注释字符来满足此要求 后面是一个空格和一系列注释字符。 如果需要禁用此行为,可以将allow_pep8设置为false。
要求
节选需要Python3。
安装
- 尝试
- pip3安装git+git://gitlab.com/fvafrcu/execpts–升级–用户