是否有一些查询"魔法"可以执行我正在寻找的内容,或者必须使用返回的结果以编程方式完成?
我有桌子"骨头":
Color Amount Length
-----------------------
yellow 3 15
brown 2 16
white 4 10
yellow 2 11
black 2 15
white 1 15
brown 2 16
white 4 10
yellow 1 11
black 2 15
Run Code Online (Sandbox Code Playgroud)
我实际上想要得到一个观点,我看到它按每种颜色的骨骼总量排序,并查看数量.应该是这样的:
Color Amount
-----------------------
white 9
yellow 6
brown 4
black 4
Run Code Online (Sandbox Code Playgroud)
但到目前为止,我只想到了以下查询:
SELECT Color,COUNT(*) as count FROM tablename GROUP BY Color ORDER BY count DESC;
Run Code Online (Sandbox Code Playgroud)
但这给了我:
Color Amount
-----------------------
white 3
yellow 3
brown 2
black 2
Run Code Online (Sandbox Code Playgroud)
哪个是具有不同值的事件计数,但不是我真正想要的.是否可以在一个查询中?如果这样的魔法可能,我该如何扩展呢?例如,我的数量相同,但是对于长度超过11厘米的骨头,我们可以这样说吗?
我正在尝试为使用简单验证函数的函数编写测试。我的函数需要验证所有传入的参数。验证函数AttributeError在出现问题时抛出一个,否则返回 True。但是,当我映射时validate它不起作用,除非我做一些额外的事情,比如使用all():
def my_function(a, b, c=None):
map(validate, (a, b, c)) # This doesn't fail validation (incorrect behavior)
all(map(validate, (a, b, c)) # This DOES fail validation (correct behavior)
# Some other stuff
Run Code Online (Sandbox Code Playgroud)
我的单元测试是这样做的:
def test_my_function(self):
bad_id = 0
self.assertRaises(AttributeError, add_favorite, 10, bad_id)
Run Code Online (Sandbox Code Playgroud)
这是我的验证功能:
def validate(identifier):
if identifier is None:
return True
elif not isinstance(identifier, int):
raise AttributeError("Identifier={0} not of type integer.".format(identifier))
elif not (identifier > 0):
raise AttributeError("Identifier={0} not an integer greater than zero.".format(identifier)) …Run Code Online (Sandbox Code Playgroud) 我为我的ID制作了一个自定义类型:
type ID uint
func (id ID) MarshalJSON() ([]byte, error) {
e, _ := HashIDs.Encode([]int{int(id)})
fmt.Println(e) /// 34gj
return []byte(e), nil
}
func (id *ID) Scan(value interface{}) error {
*id = ID(value.(int64))
return nil
}
Run Code Online (Sandbox Code Playgroud)
我使用HashIDs包对我的id进行编码,以便用户无法在客户端读取它们.但是我收到了这个错误:
json:为类型types.ID调用MarshalJSON时出错:顶级值后无效字符'g'
我试图根据工资来计算用户应该支付多少税.例如,在第一个if循环中计算20%,这将保存在generaltax:
int generalTax = 0;
int userGrossPay = 50000;
if (userGrossPay <= 10600) {generalTax += 0;}
else if (((userGrossPay >= 10600) && (userGrossPay <= 31785))) { generalTax = ((20/100) * userGrossPay); }
else if (((userGrossPay >= 31786) && (userGrossPay <= 150000))) { generalTax = ((40/100) * userGrossPay); System.out.println(generalTax);}
else if (userGrossPay > 150001) {generalTax = ((45/100) * userGrossPay); }
else{System.out.println("error");};
userGrossPay -= generalTax;
System.out.println(userGrossPay);
Run Code Online (Sandbox Code Playgroud)
但是generalTax,由于某种原因,付费始终被卡住,0并且在每次迭代时都没有正确更新.
我正在尝试生成一些代码,它首先会modulos p在函数中找到我所有完美的正方形perfectsq(p).
有了完美的正方形列表.我想找到方程的所有解y^2=x^3+Ax+B.我这样做是通过使用列表perfectsq(p)来检查m=x^3+Ax+B该列表中的内容.有人能告诉我为什么这段代码没有编译?
def perfectsq(p):
x=[]
for i in range(1,p):
m=(i**2)%p
x.extend(m)
i+=1
def ellipticpt(a, b, p):
x=perfectsq(p)
if 4*(a**3)+27*(b**2) != 0:
for i in range(0,p):
m=(i**3+a*i+b)%p
if m in x:
i=x.index(m)+1
print (m,i)
i+=1
else:
i+=1
else:
print "Error"
Run Code Online (Sandbox Code Playgroud) 我试图编写一个程序,但遇到了return语句的问题.
以下代码行引发了一条错误消息,指出该变量names未定义.
但是,我确实使用return语句返回names并将其传递给main函数:
def main(names):
names_in()
print(names)
# import the record, put it into a list, pass that list to main().
def names_in():
infile = open('names.txt','r')
names = infile.readlines()
infile.close()
index = 0
while index < len(names):
names[index] = names[index].rstrip('\n')
index += 1
return names
main(names)
Run Code Online (Sandbox Code Playgroud)
我之前写过另一个程序做同样的事情,一切正常,所以我不确定这里有什么问题?
我下面有这个示例RDD(rdd以下称为)。数据集是一个元组(String, Int):
(some | random | value, 10)
(some | random | value, 11)
(some | random | value, 12)
Run Code Online (Sandbox Code Playgroud)
我想得到以下输出:
(some, 10)
(random, 10)
(value, 10)
(some, 11)
(random, 11)
(value, 11)
(some, 12)
(random, 12)
(value, 12)
Run Code Online (Sandbox Code Playgroud)
我有这个Scala代码来尝试上述转换:
rdd.map(tuple => tuple._1.split("|").foreach(elemInArray => (elemInArray, tuple._2)))
Run Code Online (Sandbox Code Playgroud)
在这段代码中,我遍历整个数据集,并将元组的第一部分拆分为|。然后,我遍历返回的数组中的每个元素,并使用每个元素和获得的计数split创建一个元组。elementtuple._1
由于某些原因,我一直得到以下结果:
()
()
()
()
()
()
()
()
()
Run Code Online (Sandbox Code Playgroud)
有人知道这个问题吗?我似乎找不到错误的地方。
我正在尝试在 DigitalOcean 上使用 LetsEncrypt 部署带有 SSL 证书的 Kubernetes 集群。我遵循了这些说明,一切正常,直到 ClusterIssuer 创建挑战订单。然后我收到这个错误:
cert-manager/controller/orders "msg"="Failed to determine the list of Challenge resources needed for the Order" "error"="no configured challenge solvers can be used for this challenge" "resource_kind"="Order" "resource_name"="letsencrypt-prod-cert-458163912-1173127706"
Run Code Online (Sandbox Code Playgroud)
我已经尝试过使用 http 并尝试配置 DigitalOcean 的dns01解析器,但都不起作用,并且出现了类似的错误。该站点通过 ip 和 dns 名称上线(尽管我收到了 no-ssl 证书警告)。这是 ClusterIssuer 描述:
Name: letsencrypt-issuer
Namespace:
Labels: app/instance=webapp
app/managed-by=Tiller
app/name=webapp
app/version=0.1.0
helm.sh/chart=webapp-0.1.0
Annotations: cert-manager.io/cluster-issuer: letsencrypt-issuer
kubernetes.io/ingress.class: nginx
kubernetes.io/tls-acme: true
API Version: cert-manager.io/v1alpha2
Kind: ClusterIssuer
Metadata:
Creation Timestamp: 2019-10-16T23:24:47Z
Generation: 2 …Run Code Online (Sandbox Code Playgroud) 我正在学习 Rust(来自 Javascript),并且在 Rust 中我尝试创建一个基于组件的 UI 模板。这是我可以在 Rust 游乐场中重现的最小示例。
我有一个枚举向量。我想添加将返回一组新向量的组件。该组件从不是引用的成员函数返回一个向量。
let _new_children = match new_view.unwrap() {
View::View(children) => children, // reference &Vec<View>
View::Render(ref component) => component.render(), // struct Vec<View>
};
let _new_children = match new_view.unwrap() {
View::View(children) => children,
View::Render(ref component) => &component.render(), // temporary value dropped while borrowed
};
Run Code Online (Sandbox Code Playgroud)
我怎么解决这个问题?我是否需要重写函数检查两个向量之间差异的方式(itertools有一个 zip_longest 方法,我也使用该方法)。
如果您使用的是Java或JavaScript,那么有一种很好的方法可以执行String减法操作,以便给出两个字符串:
org.company.project.component
org.company.project.component.sub_component
Run Code Online (Sandbox Code Playgroud)
你得到:
sub_component
Run Code Online (Sandbox Code Playgroud)
我知道我可以编写代码来比较字符的字符串,但我希望有一种方法可以以非常紧凑的方式完成.
另一个用例是找到字符串之间的差异:
org.company.project.component.diff
org.company.project.component.sub_component
Run Code Online (Sandbox Code Playgroud)
我实际上只想删除相同的部分.
java ×2
python ×2
apache-spark ×1
components ×1
go ×1
hashids ×1
javascript ×1
json ×1
kubernetes ×1
lets-encrypt ×1
list ×1
marshalling ×1
mysql ×1
python-3.x ×1
regex ×1
return ×1
rust ×1
scala ×1
sql ×1