小编10S*_*Tom的帖子

Graphviz:使所有节点的大小与最大节点相同

我正在尝试配置我的Digraph调用,以便每个节点使用所需的最大宽度和高度.给定下面的图像,每个节点将是第一个节点的宽度和第二个节点的高度.我查看了fixedsize属性,但它似乎不合适.如果fixedsize设置为true,则必须指定限制.如果可能的话,我宁愿自动确定.所需的最大值取决于给定图形的节点标签,并不总是相同.

在此输入图像描述

样本设置:

dot = Digraph(comment="Example 1",
          format='png',
          edge_attr={'color':'black', 
                     'style':'filled'},
          graph_attr={'fixedsize':'false', 
                      'bgcolor':'transparent'},
          node_attr={'fontname':'bold',
                     'fontsize':'11', 
                     'shape':'sqaure', 
                     'color':'black', 
                     'style':'filled', 
                     'fillcolor':'lightsteelblue3'})
Run Code Online (Sandbox Code Playgroud)

fixedsize:如果为false,则节点的大小由包含其标签和图像所需的最小宽度和高度(如果有)确定,并具有margin属性指定的边距.宽度和高度也必须至少与width和height属性指定的大小一样大,这些属性指定这些参数的最小值.如果为true,则节点大小仅由width和height属性的值指定,并且不会展开以包含文本标签.如果标签(带边距)不符合这些限制,则会发出警告.如果fixedsize属性设置为shape,则width和height属性也会确定节点形状的大小,但标签可能会大得多.避免节点重叠时使用标签和形状大小,但节点的所有边都忽略标签并仅接触节点形状.如果标签太大,则不会发出警告.

编辑:一个凌乱的例子,但是一个例子.我用'gv'格式构建了图形,处理了高度和宽度,并重建了图形.

from graphviz import Digraph

dot = Digraph(comment="Example 1",
              format='gv',
              edge_attr={'color':'brown', 
                         'style':'filled'},
              graph_attr={'rankdir':'LR', 
                          'bgcolor':'transparent'},
              node_attr={'fontsize':'11', 
                         'shape':'sqaure', 
                         'color':'black',
                        'style':'filled',
                        'fillcolor':'antiquewhite'})
# nodes and edges
dot.node('1', 'This is the longest width')
dot.node('2', 'This\nis\nthe\nlargest\nheight')
dot.node('3', 'Small')
dot.edges(['12','23'])

def get_node_max(digraph):
    import re
    heights = [height.split('=')[1] for height in re.findall('height=[0-9.]+', str(digraph))]
    widths = [width.split('=')[1] for width in re.findall('width=[0-9.]+', str(digraph))]
    heights.sort(key=float)
    widths.sort(key=float)  
    return heights[len(heights)-1], widths[len(widths)-1]

params = {'format':'png', …
Run Code Online (Sandbox Code Playgroud)

python graphviz

8
推荐指数
1
解决办法
2639
查看次数

Doxygen多个粗体字

我正在尝试使整行文本加粗。Doxygen文档指出:

\ b使用粗体显示参数。等同于<b> word </ b>。要以粗体显示多个单词,请使用<b>多个单词</ b>。

我已经尝试过(在阅读HTML命令部分之前):

  • \\!\ b很多单词\ b
  • \\!<b>许多单词</ b>
  • \\!\ b很多单词/ b
  • \\!\ b <很多单词> \ b

我查看了HTML标签<B>和</ B>,但是我不想将格式仅绑定到HTML输出。除此之外,当我尝试'\\!<B>很多单词</ B>'时,它不起作用。

注意:

  • 文本在段落(\ par)中。
  • 在<b>和</ b>中添加了空格,以阻止此网页将其作为文字粗体标记插入。

doxygen

5
推荐指数
2
解决办法
2506
查看次数

python re.split() 空字符串

下面的例子取自python re文档

re.split(r'\b', 'Words, words, words.')
['', 'Words', ', ', 'words', ', ', 'words', '.']
Run Code Online (Sandbox Code Playgroud)

'\b' 匹配单词开头或结尾的空字符串。这意味着如果您运行此代码,它会产生错误。

(jupyter笔记本python 3.6)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-128-f4d2d57a2022> in <module>
      1 reg = re.compile(r"\b")
----> 2 re.split(reg, "Words, word, word.")

/usr/lib/python3.6/re.py in split(pattern, string, maxsplit, flags)
    210     and the remainder of the string is returned as the final element
    211     of the list."""
--> 212     return _compile(pattern, flags).split(string, maxsplit)
    213 
    214 def findall(pattern, string, flags=0):

ValueError: split() requires a non-empty …
Run Code Online (Sandbox Code Playgroud)

regex python-3.x jupyter-notebook

5
推荐指数
1
解决办法
2118
查看次数

Python:将文件向上移动一个目录

下面是一个小的测试例程,它获取文件的路径并将文件向上移动一个目录。我正在使用 os 和 shutil 模块,是否有一个模块可以执行此任务?有没有更pythonic的方式来实现这个功能?

下面的代码在 Windows 上运行,但最好的跨平台解决方案将不胜感激。

def up_one_directory(path):
    """Move file in path up one directory"""
    head, tail = os.path.split(path)
    try:
        shutil.move(path, os.path.join(os.path.split(head)[0], tail))
    except Exception as ex:
        # report
        pass
Run Code Online (Sandbox Code Playgroud)

directory shutil python-3.x pathlib

4
推荐指数
2
解决办法
7271
查看次数

熊猫0.19.2 read_excel IndexError:列表索引超出范围

我想解析一个Excel电子表格。我决定使用熊猫,但立即被错误发现。

在此处输入图片说明

我从下面的代码开始,但是使用了完整路径并尝试设置工作表名称。

import pandas as pd

table = pd.read_excel('ss_12.xlsx')

if __name__ == '__main__':
    pass
Run Code Online (Sandbox Code Playgroud)

excel电子表格与我的脚本文件位于同一目录中。从这个意义上讲,我教过它与open()相同,只是在同一个目录中需要一个名称。我在网上看了一些例子,按他们的说法应该可以。

在此处输入图片说明

我正在尝试删除上图中的第一列。完整错误(不确定如何格式化,抱歉)

C:\xx\Playpen\ConfigList_V1_0.xlsx
Traceback (most recent call last):
  File "C:\xx\Playpen\getConVars.py", line 12, in <module>
    pd.read_excel(excelFile)
  File "C:\xx\Programs\Python\Python35\lib\site-packages\pandas\io\excel.py", line 200, in read_excel
    **kwds)
  File "C:\xx\Programs\Python\Python35\lib\site-packages\pandas\io\excel.py", line 432, in _parse_excel
    sheet = self.book.sheet_by_index(asheetname)
  File "C:\xx\Programs\Python\Python35\lib\site-packages\xlrd\book.py", line 432, in sheet_by_index
    return self._sheet_list[sheetx] or self.get_sheet(sheetx)
IndexError: list index out of range
Run Code Online (Sandbox Code Playgroud)

python excel pandas

2
推荐指数
1
解决办法
4709
查看次数

Python正则表达式匹配多行

我正在尝试跨多行匹配正则表达式模式。该模式以一个子字符串开始和结束,这两个子字符串都必须在一行的开头。我可以跨行匹配,但我似乎无法指定结束模式也必须在行的开头。

示例字符串:

Example=N      ; Comment Line One error=

; Comment Line Two.

Desired=
Run Code Online (Sandbox Code Playgroud)

我正在尝试匹配从Example=Desired=. 如果error=不在字符串中,这将起作用。但是,当它出现时,我匹配Example=N ; Comment Line One error=

config_value = 'Example'
pattern = '^{}=(.*?)([A-Za-z]=)'.format(config_value)
match = re.search(pattern, string, re.M | re.DOTALL)
Run Code Online (Sandbox Code Playgroud)

我也试过:

config_value = 'Example'
pattern = '^{}=(.*?)(^[A-Za-z]=)'.format(config_value)
match = re.search(pattern, string, re.M | re.DOTALL)
Run Code Online (Sandbox Code Playgroud)

regex multiline python-3.x

2
推荐指数
1
解决办法
6766
查看次数