我收到了这个错误
TypeError:super()至少需要1个参数(给定0)
在python2.7.11上使用此代码:
class Foo(object):
def __init__(self):
pass
class Bar(Foo):
def __init__(self):
super().__init__()
Bar()
Run Code Online (Sandbox Code Playgroud)
使其工作的解决方法是:
class Foo(object):
def __init__(self):
pass
class Bar(Foo):
def __init__(self):
super(Bar, self).__init__()
Bar()
Run Code Online (Sandbox Code Playgroud)
似乎语法是特定于python 3.那么,在2.x和3.x之间提供兼容代码并避免发生此错误的最佳方法是什么?
我在项目的根目录中获得了下一个.clang格式的文件:
---
AlignTrailingComments: true
AllowShortFunctionsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
BreakBeforeBinaryOperators: false
IndentWidth: 4
SortIncludes: false
NamespaceIndentation: All
...
Run Code Online (Sandbox Code Playgroud)
当我在我的c ++标题上运行clang-format时出现问题,类会自动缩进,如下所示:
如您所见,标签public和private仅缩进2个空格.但我想要实现的是下面的输出(缩进是手动调整):
这样,代码崩溃变得非常愉快.
我怎么能调整我的.clang格式才能达到这个效果?如果不可能,你将如何修补clang格式的源代码来实现这种期望的行为?
编辑:
我尝试过使用AccessModifierOffset失败,我使用了以下值{-2,0,2,4}:
如您所见,公共块内的语句将不会正确缩进.
EDIT2:
我已经尝试了@Henrique Jung解决方案,这绝对不是我要求的,如果使用这种组合,结果将是这样的:
正如您所看到的,函数内部缩进了8个空格而不是4个,这并不好.
EDIT3:
几个月前我给了赏金,所以我要再试一次,因为这个肯定很有趣.如果我对俚语格式的源代码有足够的了解,我会试一试,不幸的是我没有.
概述
我试图找出如何使用某种DFS迭代算法遍历有向循环图.这是我目前实现的一个小版本(它不涉及周期):
class Node(object):
def __init__(self, name):
self.name = name
def start(self):
print '{}_start'.format(self)
def middle(self):
print '{}_middle'.format(self)
def end(self):
print '{}_end'.format(self)
def __str__(self):
return "{0}".format(self.name)
class NodeRepeat(Node):
def __init__(self, name, num_repeats=1):
super(NodeRepeat, self).__init__(name)
self.num_repeats = num_repeats
def dfs(graph, start):
"""Traverse graph from start node using DFS with reversed childs"""
visited = {}
stack = [(start, "")]
while stack:
# To convert dfs -> bfs
# a) rename stack to queue
# b) pop becomes pop(0)
node, parent …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用networkx 1.11绘制一些DAG,但我遇到了一些错误,这是测试:
import networkx as nx
print nx.__version__
G = nx.DiGraph()
G.add_node(1,level=1)
G.add_node(2,level=2)
G.add_node(3,level=2)
G.add_node(4,level=3)
G.add_edge(1,2)
G.add_edge(1,3)
G.add_edge(2,4)
import pylab as plt
nx.draw_graphviz(G, node_size=1600, cmap=plt.cm.Blues,
node_color=range(len(G)),
prog='dot')
plt.show()
Run Code Online (Sandbox Code Playgroud)
这是追溯:
Traceback (most recent call last):
File "D:\sources\personal\python\framework\stackoverflow\test_dfs.py", line 69, in <module>
prog='dot')
File "d:\virtual_envs\py2711\lib\site-packages\networkx\drawing\nx_pylab.py", line 984, in draw_graphviz
pos = nx.drawing.graphviz_layout(G, prog)
AttributeError: 'module' object has no attribute 'graphviz_layout'
Run Code Online (Sandbox Code Playgroud)
我正在使用python 2.7.11 x64,networkx 1.11,我已经安装了在PATH 中可用的graphviz-2.38dot.我错过了什么?
一旦它工作,我怎么能用以下节点绘制图形:
类似于下图的东西
正如您在该图像中看到的那样,节点非常好地对齐
我试图弄清楚如何使用PyInstaller加载动态/隐藏导入,到目前为止我得到了这个简单的结构:
首先,我在我的PYTHONPATH中添加了一个框架包 d:\Sources\personal\python\framework
我的许多python项目都使用了这个包,特别是它与我想要打包的下面简单项目一起使用
Main project
????data <- Pure static data
????plugins <- Dynamic modules which uses framework's modules
????resources <- Static data+embedded (generated by pyqt), used by plugins
? ????css
? ????images
| resources.py
| resources.qrc
main.py <- Uses framework's modules to load plugins dynamically
Run Code Online (Sandbox Code Playgroud)
我的spec文件看起来像这样:
# -*- mode: python -*-
block_cipher = None
a = Analysis(['main.py'],
pathex=['d:\\sources\\personal\\python\\pyqt\\pyshaders'],
binaries=None,
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
##### include mydir in distribution #######
def extra_datas(mydir):
def rec_glob(p, files):
import …Run Code Online (Sandbox Code Playgroud) 从这里提取我们得到了一个最小的迭代dfs例程,我把它称为最小,因为你很难进一步简化代码:
def iterative_dfs(graph, start, path=[]):
q = [start]
while q:
v = q.pop(0)
if v not in path:
path = path + [v]
q = graph[v] + q
return path
graph = {
'a': ['b', 'c'],
'b': ['d'],
'c': ['d'],
'd': ['e'],
'e': []
}
print(iterative_dfs(graph, 'a'))
Run Code Online (Sandbox Code Playgroud)
这是我的问题,您如何将此例程转换为拓扑排序方法,其中例程也变为"最小"?我看过这个视频,这个想法非常聪明,所以我想知道是否可以在上面的代码中应用相同的技巧,所以topological_sort的最终结果也变得"最小".
不要求拓扑排序的版本,这不是对上述例程的微小修改,我已经看过很少.问题不是"如何在python中实现拓扑排序",而是找到上述代码的最小可能调整集成为topological_sort.
附加评论
在原文中,作者说:
不久之前,我读了Guido van Rossen的图表实现,看似简单.现在,我坚持使用复杂性最低的纯python最小系统.我们的想法是能够探索算法.稍后,您可以优化和优化代码,但您可能希望以编译语言执行此操作.
这个问题的目标不是优化iterative_dfs,而是提出一个从它派生的topology_sort的最小版本(只是为了更多地了解图论算法).其实,我想一个更一般的问题可以像给一组最小的算法,{ iterative_dfs,recursive_dfs,iterative_bfs,recursive_dfs},这将是他们的topological_sort推导?虽然这会使问题变得更长/更复杂,但是从iterative_dfs中找出topology_sort就足够了.
python algorithm graph-theory depth-first-search topological-sort
Consider the below mcve:
import sys
import textwrap
from PyQt5.Qsci import QsciScintilla
from PyQt5.Qt import *
if __name__ == '__main__':
app = QApplication(sys.argv)
view = QsciScintilla()
view.SendScintilla(view.SCI_SETMULTIPLESELECTION, True)
view.SendScintilla(view.SCI_SETMULTIPASTE, 1)
view.SendScintilla(view.SCI_SETADDITIONALSELECTIONTYPING, True)
view.setAutoIndent(True)
view.setTabWidth(4)
view.setIndentationGuides(True)
view.setIndentationsUseTabs(False)
view.setBackspaceUnindents(True)
view.setText(textwrap.dedent("""\
def foo(a,b):
print('hello')
"""))
view.show()
app.exec_()
Run Code Online (Sandbox Code Playgroud)
The behaviour of the auto-indent of the above snippet is really bad when comparing it with editors such as SublimeText or CodeMirror. First let's see how nice will behave the autoindent feature in SublimeText with …
概述
我想有可靠的django部署,我想我不遵循这里的最佳实践.到目前为止,我一直在使用fabric作为配置管理工具来部署我的django网站,但我不确定这是最好的方法.
在高性能的django书中有一个警告说:
Fabric不是配置管理工具.试图将它作为一个使用将最终导致你的心痛和痛苦.Fabric是在一个或多个远程系统中执行脚本的绝佳选择,但这只是这个难题的一小部分.不要通过在结构上构建自己的配置管理系统来重新发明轮子
所以,我决定要学习ansible.
质询
这里的最终目标是在QScintilla中实现基于缩进的代码折叠,类似于SublimeText3所做的方式。
首先,这是一个使用QScintilla机制手动提供折叠的小例子:
import sys
from PyQt5.Qsci import QsciScintilla
from PyQt5.Qt import *
if __name__ == '__main__':
app = QApplication(sys.argv)
view = QsciScintilla()
# http://www.scintilla.org/ScintillaDoc.html#Folding
view.setFolding(QsciScintilla.BoxedTreeFoldStyle)
lines = [
(0, "def foo():"),
(1, " x = 10"),
(1, " y = 20"),
(1, " return x+y"),
(-1, ""),
(0, "def bar(x):"),
(1, " if x > 0:"),
(2, " print('this is')"),
(2, " print('branch1')"),
(1, " else:"),
(2, " print('and this')"),
(2, " print('is branch2')"),
(-1, ""),
(-1, ""),
(-1, ""), …Run Code Online (Sandbox Code Playgroud) 让我们考虑一下这个片段:
from subprocess import Popen, PIPE, CalledProcessError
def execute(cmd):
with Popen(cmd, shell=True, stdout=PIPE, bufsize=1, universal_newlines=True) as p:
for line in p.stdout:
print(line, end='')
if p.returncode != 0:
raise CalledProcessError(p.returncode, p.args)
base_cmd = [
"cmd", "/c", "d:\\virtual_envs\\py362_32\\Scripts\\activate",
"&&"
]
cmd1 = " ".join(base_cmd + ['python -c "import sys; print(sys.version)"'])
cmd2 = " ".join(base_cmd + ["python -m http.server"])
Run Code Online (Sandbox Code Playgroud)
如果我运行execute(cmd1)输出将打印没有任何问题.
但是,如果我运行execute(cmd2)而不会打印任何内容,为什么会这样,我如何修复它,以便我可以实时看到http.server的输出.
另外,如何for line in p.stdout在内部进行评估?是什么样的无限循环,直到达到stoout eof或什么?
这个主题在SO中已经解决了几次,但我还没有找到一个Windows解决方案.上面的片段实际上是来自这个答案的代码并尝试从virtualenv运行http.server(在win7上运行python3.6.2-32bits)
python ×8
python-2.7 ×3
algorithm ×2
c++ ×2
qscintilla ×2
sublimetext3 ×2
ansible ×1
auto-indent ×1
centos ×1
clang-format ×1
demoscene ×1
deployment ×1
django ×1
fabric ×1
graph-theory ×1
indentation ×1
networkx ×1
popen ×1
pyinstaller ×1
pyqt ×1
pyqt5 ×1
python-2.x ×1
python-3.x ×1
sublimetext ×1
subprocess ×1
windows ×1