python是否有任何方法可以轻松快速地创建CLI实用程序,而无需大量的参数解析样板?
在Perl 6中,MAIN子的签名自动解析命令行参数.
有没有办法在没有大量样板的情况下在Python中做类似的事情?如果没有,最好的方法是什么?我正在考虑一个函数装饰器,它将执行一些内省并做正确的事情.如果没有什么比这更好的了,我会想到的东西就像我下面的东西.这是一个好主意吗?
@MagicMain
def main(one, two=None, *args, **kwargs):
print one # Either --one or first non-dash argument
print two # Optional --arg with default value (None)
print args # Any other non-dash arguments
print kwargs # Any other --arguments
if __name__ == '__main__':
main(sys.argv)
Run Code Online (Sandbox Code Playgroud) python method-signature perl6 command-line-arguments magic-methods
我有一个方法,它将返回一个对象或None查找失败.以下哪种风格更好?
def get_foo(needle):
haystack = object_dict()
if needle not in haystack: return None
return haystack[needle]
Run Code Online (Sandbox Code Playgroud)
要么,
def get_foo(needle):
haystack = object_dict()
try:
return haystack[needle]
except KeyError:
# Needle not found
return None
Run Code Online (Sandbox Code Playgroud)
我还没有决定哪个更适合自己.另一个选择是return haystack[needle] if needle in haystack else None,但我不确定那更好.
如何在select语句[Oracle DB]中执行此操作:
Value in column: 12 ----> Output value: 012
Value in column: 112 ----> Output value: 112
Value in column: 2 ----> Output value: 002
Run Code Online (Sandbox Code Playgroud)
我可以用select to_char吗?如何应用此案例?
我发现了一个有趣的生成器模式的scala实现,但是我无法理解几行的含义:
case class Built(a:Int, b:String){}
trait Status
trait Done extends Status
trait Need extends Status
class Builder[A<:Status,B<:Status] private(){
private var built = Built(0,"")
def setA(a0:Int)={
built = built.copy(a=a0)
this.asInstanceOf[Builder[Done,B]]
}
def setB(b0: String) = {
built = built.copy(b = b0)
this.asInstanceOf[Builder[A,Done]]
}
def result(implicit ev: Builder[A,B] <:< Builder[Done,Done]) = built
}
object Builder{
def apply() = new Builder[Need,Need]
}
Run Code Online (Sandbox Code Playgroud)
1)课堂宣言private()意味着什么class Builder[A<:Status,B<:Status] private()?
2)implicit ev: Builder[A,B] <:< Builder[Done,Done]结果函数的含义是什么?
所以我有这个haskell代码,我理解其中的一半,但我无法理解\x ->这里:
testDB :: Catalogue
testDB = fromList [
("0265090316581", ("The Macannihav'nmor Highland Single Malt", "75ml bottle")),
("0903900739533", ("Bagpipes of Glory", "6-CD Box")),
("9780201342758", ("Thompson - \"Haskell: The Craft of Functional Programming\"", "Book")),
("0042400212509", ("Universal deep-frying pan", "pc"))
]
-- Exercise 1
longestProductLen :: [(Barcode, Item)] -> Int
longestProductLen = maximum . map (\(x, y) -> length $ fst y)
formatLine :: Int -> (Barcode, Item) -> String
formatLine k (x, (y1, y2)) = x ++ "..." ++ …Run Code Online (Sandbox Code Playgroud) 我最好被描述为 C#/F# + 一些业余 Haskell 程序员。
我对 Scala 中的类型签名有点困惑。
例如
恒等函数有类型
Nothing => Nothing
Run Code Online (Sandbox Code Playgroud)
(根据我的 intellij 中的 Scala 控制台)
但对我来说这毫无意义。
身份类型类似于..
all x . x => x
Run Code Online (Sandbox Code Playgroud)
.....
所以
identity 1
=> x ~ Int
=> 1 : Int
Nothing => Nothing
Run Code Online (Sandbox Code Playgroud)
对我来说毫无意义......我希望我在将任何值传递给一个不期望什么的函数时输入异常!
显然我错过了一些东西。
而不是我必须为我决定添加的每种颜色创建一个新案例,是否可以使用带有来自数组的字符串的switch语句作为案例?这是我的代码!
public static String[][] colours = { { "red", "933" },
{ "lblue", "359770" }, { "blue", "428770" },
{ "orange", "461770" }, { "pink", "123770" } };
public static void changeColour(String col, Player player) { // changeColour("red", player);
switch (col) {
case "red":
player.setColour(Integer.parseInt(colours[0][1]));
break;
case "lblue":
player.setColour(Integer.parseInt(colours[1][1]));
break;
case "blue":
player.setColour(Integer.parseInt(colours[2][1]));
break;
case "orange":
player.setColour(Integer.parseInt(colours[3][1]));
break;
case "pink":
player.setColour(Integer.parseInt(colours[4][1]));
break;
}
}
Run Code Online (Sandbox Code Playgroud)