我正在创建一个python脚本,我希望有一个参数来操作你输出的搜索结果数量.我现在已经命名了这个论点--head.这是我希望它拥有的功能:
如果--head没有在命令行传递,我希望它默认为一个值.在这种情况下,一个相当大的,像80
当--head没有任何值传递时,我希望它默认为另一个值.在这种情况下,有限的东西,如10
当--head传递一个值时,我希望它存储它传递的值.
以下是一些描述问题的代码:
>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('-h',
'--head',
dest='size',
const=80,
default=10,
action="I don't know",
help='Only print the head of the output')
>>> # OFC, that last line will fail because the action is uknown,
... # but here is how I'd like it to work
... parser.parse_args(''.split())
Namespace(size=80)
>>> parser.parse_args('--head'.split())
Namespace(size=10)
>>> parser.parse_args('--head 15'.split())
Namespace(size=15)
Run Code Online (Sandbox Code Playgroud)
我知道我可能会为此编写自定义操作,但我首先想看看是否有任何默认行为.
我正在创建一个提供动态图像和文本的网络应用程序。绘制的每个字符串可能有多种颜色。
到目前为止,我已经创建了一个 parse 方法和一个 render 方法。parse 方法只获取字符串,并从中解析颜色,它们的格式如下:“§aThis 是绿色§rthis 是白色”(是的,它是 Minecraft)。所以这就是我的字体模块的样子:
# Imports from pillow
from PIL import Image, ImageDraw, ImageFont
# Load the fonts
font_regular = ImageFont.truetype("static/font/regular.ttf", 24)
font_bold = ImageFont.truetype("static/font/bold.ttf", 24)
font_italics = ImageFont.truetype("static/font/italics.ttf", 24)
font_bold_italics = ImageFont.truetype("static/font/bold-italics.ttf", 24)
max_height = 21 # 9, from FONT_HEIGHT in FontRederer in MC source, multiplied by
# 3, because each virtual pixel in the font is 3 real pixels
# This number is also returned by:
# font_regular.getsize("ABCDEFGHIJKLMNOPQRSTUVWXYZ")[1]
# Create the color …Run Code Online (Sandbox Code Playgroud) 我正在用Java做一个小游戏。对于这个游戏,我只是添加了声音。所以我想把我所有的图像和音频文件放在罐子里。对于图片,这很简单:
new ImageIcon(Main.class.getResource("images/machgd2.png")).getImage()
Run Code Online (Sandbox Code Playgroud)
但是对于音频,仅当我在Eclipse中运行程序时才起作用,而不能从jar中运行。我用:
File soundFile = new File(Main.class.getResource(filename).getFile());
Run Code Online (Sandbox Code Playgroud)
那么如何从.jar文件中获取此文件?
更新:
好的,感谢Andrew!为了播放声音,我使用了一个在网上找到的类,然后发现该类仅使用File来获取AudioInputStream,因此我删除了File。
python ×2
argparse ×1
file ×1
fonts ×1
getresource ×1
jar ×1
java ×1
javasound ×1
python-3.x ×1