我在使用python setUpClass时遇到了一些麻烦.
例如,考虑以下情况
class MyTest(unittest.case.TestCase):
@classmethod
def setUpClass(cls):
print "Test setup"
try:
1/0
except:
raise
@classmethod
def tearDownClass(cls):
print "Test teardown"
Run Code Online (Sandbox Code Playgroud)
几个问题
上面的代码是处理测试setUpClass异常的正确方法(通过提高它以便python unittest可以处理它),有fail(),skip()方法,但那些只能由测试实例使用而不是测试类.
当存在setUpClass异常时,我们如何确保tearDownClass运行(如果我们手动调用它,则unittest不运行它).
我想通过python API获取当前的Elasticsearch版本.我可以轻松地通过http调用来获取它
import requests
requests.get(http://endpoint:9200)
Run Code Online (Sandbox Code Playgroud)
但我想知道有没有办法让版本通过API调用而不是http请求到端点.喜欢
from elasticsearch import Elasticsearch
es = Elasticsearch()
Run Code Online (Sandbox Code Playgroud)
我浏览了Elasticsearch python客户端文档,但找不到可以获取当前ES版本的调用(https://elasticsearch-py.readthedocs.org/en/master/api.html)
我试图用自己的直觉在python中打印所有有效的括号组合.它几乎可以工作,但只是它不会打印几个组合.代码看起来像这样
solution = ""
def parentheses(n):
global solution
if n == 0:
print solution
return
for i in range(1, n+1):
start_index = len(solution)
solution = solution + ( "(" * i + ")" * i )
parentheses(n - i)
solution = solution[:start_index]
if __name__ == "__main__":
n = int(raw_input("Enter the number of parentheses:"))
print "The possible ways to print these parentheses are ...."
parentheses(n)
Run Code Online (Sandbox Code Playgroud)
对于n = 3,它打印
()()()
()(())
(())()
((()))
它的工作原理如下.
在第一次迭代中
()()()将被打印,当调用返回到直接父级时,它将首先开始附加的列表的切片现在将是()并运行循环的下一次迭代以打印()( ( ) ) 等等
问题是我在某种程度上无法使用此逻辑捕获此组合
(()()) …
我看到以下API将在Elasticsearch中通过查询进行删除 - http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-delete-by-query.html
但我想对弹性搜索批量API做同样的事情,即使我可以使用批量上传文档
es.bulk(body=json_batch)
Run Code Online (Sandbox Code Playgroud)
我不知道如何使用python批量API进行弹性搜索来调用查询删除.
我试图通过提供从这样的方法中获取的参数来格式化 python 中的 SQL 查询
query = "select * from employees where employee_name like '%s%'" % (name)
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,出现以下错误(python 2.6)
ValueError: unsupported format character ''' (0x27) at index 886
Run Code Online (Sandbox Code Playgroud)
我也在命令行中尝试了这个来找出问题
>>> print "hello '%s'" % ('world')
hello 'world'
>>> print "hello '%s\%'" % ('world')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string
>>> print "hello '%s%'" % ('world')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: not …Run Code Online (Sandbox Code Playgroud) 我想了解为什么下面的代码会抛出错误,我试图删除字典中的项目,如果存在特定的键.
>>>
>>> a = {1:1, 2:2}
>>> type(a)
<type 'dict'>
>>> a.has_key(1) and del a[1]
File "<stdin>", line 1
a.has_key(1) and del a[1]
^
SyntaxError: invalid syntax
>>>
Run Code Online (Sandbox Code Playgroud)
使上述代码工作的唯一方法是使用
if a.has_key(1): del a[1]
Run Code Online (Sandbox Code Playgroud) 我试图在 Swift 中用数字 1 到 10 初始化一个整数数组。在 python 中,这将是
array = range(1, 11)
Run Code Online (Sandbox Code Playgroud)
很快,我想出了以下内容
var array = [Int]()
array = (1..<11).sort()
Run Code Online (Sandbox Code Playgroud)
虽然这有效,但我不喜欢 sort() 类方法来生成它。有没有其他有效的方法可以在 python 中像范围一样快速生成数字列表。
我正在尝试快速执行以下操作-尝试反向解码数组中的地址列表并打印其纬度/经度坐标。我的代码如下。
let addressArray = ["Address 1", "Address 2"]
var coordinatesArray = [CLLocationCoordinate2D]()
override func viewDidLoad() {
super.viewDidLoad()
createAddressList()
printAddressList()
}
func printAddressList() {
for i in 0 ..< addressArray.count {
print("Address = \(addressArray[i]) Coordinates = \(coordinatesArray[i].latitude),\(coordinatesArray[i].latitude)")
}
func createAddressList() {
for i in 0 ..< addressArray.count {
let address = addressArray[i]
geocoder.geocodeAddressString(address, completionHandler: {(placemarks, error) -> Void in
print("Address = \(address)");
if let placemark = placemarks?.first {
let coordinate = placemark.location?.coordinate
self.coordinatesArray.append(coordinate!)
}
})
}
}
}
Run Code Online (Sandbox Code Playgroud)
该代码仅显示已解码的第一个地址,然后没有任何反应。
我确实有如下修复方法,如下所示,它是将printAddressList调用从viewDidLoad方法中移出,如下所示
func …Run Code Online (Sandbox Code Playgroud)