我想使用一些有用的函数作为命令.为此我正在测试click
库.我定义了我的三个原始函数,然后装饰为click.command
:
import click
import os, sys
@click.command()
@click.argument('content', required=False)
@click.option('--to_stdout', default=True)
def add_name(content, to_stdout=False):
if not content:
content = ''.join(sys.stdin.readlines())
result = content + "\n\tadded name"
if to_stdout is True:
sys.stdout.writelines(result)
return result
@click.command()
@click.argument('content', required=False)
@click.option('--to_stdout', default=True)
def add_surname(content, to_stdout=False):
if not content:
content = ''.join(sys.stdin.readlines())
result = content + "\n\tadded surname"
if to_stdout is True:
sys.stdout.writelines(result)
return result
@click.command()
@click.argument('content', required=False)
@click.option('--to_stdout', default=False)
def add_name_and_surname(content, to_stdout=False):
result = add_surname(add_name(content))
if to_stdout is True:
sys.stdout.writelines(result) …
Run Code Online (Sandbox Code Playgroud) 你好我正在尝试编译一个来自这个网站的c ++文件:http://opencv-code.com/tutorials/eye-detection-and-tracking/ 用于眼睛跟踪.但我对此有点新鲜,我并不真正理解库是如何链接的.我知道include头的绝对路径位于/ user/include/opencv2中.如何在gcc命令行(ubuntu)中链接它?我试过这个命令:
$ g++ -Wall eye-tracking.cpp -o eyeTracking
Run Code Online (Sandbox Code Playgroud)
看来我还需要链接其他库.我尝试将使用此命令找到的链接:
$ pkg-config --libs opencv
Run Code Online (Sandbox Code Playgroud)
但在这里我再也不知道如何将输出链接到我的命令.我通过输入以下命令尝试了我的逻辑:
$g++ -Wall eye-tracking.cpp -I `pkg-config --libs opencv` -o eyeTracking
Run Code Online (Sandbox Code Playgroud)
当然它不起作用,我真的不明白我在做什么:P
有人能解释一下怎么样?
在这里您可以找到整个文件代码:
/**
* eye-tracking.cpp:
* Eye detection and tracking with OpenCV
*
* This program tries to detect and tracking the user's eye with webcam.
* At startup, the program performs face detection followed by eye detection
* using OpenCV's built-in Haar cascade classifier. If the user's eye detected
* …
Run Code Online (Sandbox Code Playgroud) 我有这段代码:
import click
@click.option('--delete_thing', help="Delete some things columns.", default=False)
def cmd_do_this(delete_thing=False):
print "I deleted the thing."
Run Code Online (Sandbox Code Playgroud)
我想重命名 中的选项变量--delete-thing
。但是Python不允许变量名中出现破折号。有没有办法编写这种代码?
import click
@click.option('--delete-thing', help="Delete some things columns.", default=False, store_variable=delete_thing)
def cmd_do_this(delete_thing=False):
print "I deleted the thing."
Run Code Online (Sandbox Code Playgroud)
所以delete_thing
将被设置为值delete-thing
我有大的gzip压缩文件.我编写了一段代码将这些文件拆分成较小的文件.我可以指定每个文件的行数.事情是我最近将每次分割的行数增加到16,000,000,当我处理更大的文件时,分裂不会发生.有时会成功生成较小的文件,有时会产生一个文件,但重量只有40B或50B,这是一个失败.我试着通过查看gzip
代码中提到的那些来捕获异常.所以我的代码看起来像这样:
def writeGzipFile(file_name, content):
import gzip
with gzip.open(file_name, 'wb') as f:
if not content == '':
try:
f.write(content)
except IOError as ioe:
print "I/O ERROR wb", ioe.message
except ValueError as ve:
print "VALUE ERROR wb: ", ve.message
except EOFError as eofe:
print "EOF ERROR wb: ", eofe.message
except:
print "UNEXPECTED ERROR wb"
Run Code Online (Sandbox Code Playgroud)
问题是,当内容太高,与行数相关时,我经常收到"意外错误"消息.所以我不知道这里会抛出哪种错误.
我终于发现线的数量是问题所在,并且看起来python gzip
一次在一个文件中写入这么大量的数据失败了.将每个拆分的行数减少到4,000,000个.但是,我想分割内容并按顺序写入文件,以确保即使是高数据内容也可以写入.
所以我想知道如何在文件中一次性找出可以写入的最大字符数gzip
,而不会出现任何故障.
编辑1
所以我要注意所有剩下的例外情况(我不知道有可能只是抓住Exception
对不起):
def writeGzipFile(file_name, content, file_permission=None):
import gzip, traceback
with gzip.open(file_name, 'wb') as f:
if not content …
Run Code Online (Sandbox Code Playgroud) 我的bitbucket帐户上有一个团队myteam
,其中包含一个名为的项目mainproject
.每当我想在里面创建一个存储库时,我只需要执行这个命令行:
$ curl -X POST -v -u myaccount:passwd "https://api.bitbucket.org/2.0/repositories/myteam/repo1" -d '{"scm": "git", "is_private": "true", "fork_policy": "no_public_forks"}'
Run Code Online (Sandbox Code Playgroud)
这有效.但是,当我创建第二个项目时会出现问题secondproject
.我该如何告诉API存储库应该属于哪个项目?
我尝试在数据(-d)中指定项目信息:
$ curl -X POST -v -u myaccount:passwd "https://api.bitbucket.org/2.0/repositories/myteam/repo2" -d '{"scm": "git", "is_private": "true", "fork_policy": "no_public_forks", "project": {"name": "secondproject"} }'
Run Code Online (Sandbox Code Playgroud)
或者使用关键参数:
$ curl -X POST -v -u myaccount:passwd "https://api.bitbucket.org/2.0/repositories/myteam/repo2" -d '{"scm": "git", "is_private": "true", "fork_policy": "no_public_forks", "project": {"key": "SEC"} }'
Run Code Online (Sandbox Code Playgroud)
这将在项目中创建存储库repo2 mainproject
.
我尝试使用uuid,但同样的情况发生,repo在mainproject中创建.
我尝试将项目名称放在链接中:
$ curl -X POST -v -u myaccount:passwd "https://api.bitbucket.org/2.0/repositories/myteam/secondproject/repo1" -d '{"scm": "git", "is_private": …
Run Code Online (Sandbox Code Playgroud) 我有一个存储库 ,myBigRep
其中包含一个子模块mySmallRep
。该子模块是一个 python 项目,其中有一个setup.py
安装一些命令的脚本。为了设置它,我运行:
$ pip install --editable myBigRep/local/src/mySmallRep/
Run Code Online (Sandbox Code Playgroud)
就是这样。
现在我想克隆到mySmallRep
其他地方,设置其子模块并安装 Python 命令。所以我首先冻结 Python 包:
$ cd myBigRep
$ pip freeze
-e git+git@bitbucket.org:me/mySmallRep.git@d702f237b4c5449dffe5f224a8c361bf983566fc#egg=mySmallRep&subdirectory=../../../../local/src/mySmallRep
$ pip freeze > requirements.txt
$ git add requirements.txt
$ git commit -m "added requirements."
$ git push origin master
Run Code Online (Sandbox Code Playgroud)
现在我克隆并设置所有内容:
$ cd
$ git clone git@bitbucket.org:me/myBigRep.git myBigRepClone
$ cd myBigRepClone
$ git submodule init && git submodule update
$ git submodule foreach git pull origin master
$ pip …
Run Code Online (Sandbox Code Playgroud) 我有一个 jar 文件:“CallMeMaybe.jar”。
主类 callmemaybe.CallMeMaybe 中有一个静态方法 callMe() 。就像可以通过运行从命令行调用 main() 方法:
java -cp CallMeMaybe.jar callmemaybe.CallMeMaybe
Run Code Online (Sandbox Code Playgroud)
有没有办法直接调用另一个静态方法而不是 main() ?
我想这样做 :
java -cp CallMeMaybe.jar callmemaybe.CallMeMaybe.callMe()
Run Code Online (Sandbox Code Playgroud) 我想检测鼠标在QPushButton
. 为此,我在按钮上安装了一个事件过滤器。但是,MouseMove
当鼠标悬停在按钮上时,事件不会完全触发。当我单击与前一个位置不同的位置上的按钮时,似乎有时会触发它。简单地说:我将鼠标移到按钮上:没有任何反应。我点击:MouseButtonPressed
事件被触发。我将鼠标移动到按钮上的另一个位置:没有任何反应。我再次点击:也MouseButtonPressed
被触发MouseMove
了。
我想在每次鼠标悬停按钮时触发 MouseMove。我该怎么办?
这是我的代码:
import sys
from PyQt4 import QtCore
from PyQt4.QtGui import *
class eventFilterWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
widget = QWidget()
button = QPushButton("Trigger event!")
button.installEventFilter(self)
hbox = QHBoxLayout()
hbox.addWidget(button)
widget.setLayout(hbox)
self.setCentralWidget(widget)
self.show()
def eventFilter(self, object, event):
if event.type() == QtCore.QEvent.MouseButtonPress:
print "You pressed the button"
return True
elif event.type() == QtCore.QEvent.MouseMove:
print "C'mon! CLick-meeee!!!"
return True
return False
def main():
app = QApplication(sys.argv)
#myWindow = EventsWindow()
window …
Run Code Online (Sandbox Code Playgroud) 我有一个包含大量文本文件的文件夹。每一个都被压缩并重几个千兆字节。我写了一段代码来拆分每个 gzip 文件的内容:每个 gzip 文件都用 打开gzip
,然后读取每个指定的行块并将其写入一个新的 gzip 文件。
这是文件中的代码file_compression.py
:
import sys, os, file_manipulation as fm
import gzip
def splitGzipFile(fileName, dest=None, chunkPerSplit=100, linePerChunk=4, file_field_separator="_", zfill=3
, verbose=False, file_permission=None, execute=True):
"""
Splits a gz file into chunk files.
:param fileName:
:param chunkPerSplit:
:param linePerChunk:
:return:
"""
absPath = os.path.abspath(fileName)
baseName = os.path.basename(absPath)
dirName = os.path.dirname(absPath)
destFolder = dirName if dest is None else dest
## Compute file fields
rawBaseName, extensions = baseName.split(os.extsep, 1)
if not str(extensions).startswith("."):
extensions = …
Run Code Online (Sandbox Code Playgroud) Pandoc 不能将 HTML 表格很好地呈现为 docx 文档。我获取请求的内容,然后使用模板文件呈现它。然后我像这样使用 pypandoc:
response = render(
request,
'template.html',
{
"field1": f1,
"field1": f2,
}
)
import pypandoc
pypandoc.convert(source=response.content, format='html', to='docx', outputfile='output.docx')
Run Code Online (Sandbox Code Playgroud)
template.html 包含一个表格。在 docx 文件中,我得到一个表格,其内容在下面分开。是否有额外的参数需要考虑来解决这个问题?或者 pandoc 转换还不支持井表?是否有任何功能示例?也许有更简单的方法来做到这一点?
编辑 1
我提供了更简洁的例子。这是一个测试 python 片段:
$ cat test-table.py
#!/usr/bin/env python
test_table = """
<p>Table with colgroup and col</p>
<table border="1">
<colgroup>
<col style="background-color: #0f0">
<col span="2">
</colgroup>
<tr>
<th>Lime</th>
<th>Lemon</th>
<th>Orange</th>
</tr>
<tr>
<td>Green</td>
<td>Yellow</td>
<td>Orange</td>
</tr>
<tr>
<td>Fruit</td>
<td>Fruit</td>
<td>Fruit</td>
</tr>
</table>
"""
print("[test_table]")
print(test_table)
import pypandoc
pypandoc.convert(source=test_table, …
Run Code Online (Sandbox Code Playgroud)