本文整理匯總了Python中humanize.naturalsize方法的典型用法代碼示例。如果您正苦於以下問題:Python humanize.naturalsize方法的具體用法?Python humanize.naturalsize怎麽用?Python humanize.naturalsize使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類humanize
的用法示例。
在下文中一共展示了humanize.naturalsize方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: import humanize [as 別名]
# 或者: from humanize import naturalsize [as 別名]
def __init__(self, args, config):
self.args = args
self.config = config
self.cache_dir = utils.get_cache_dir(config)
self.model_dir = utils.get_model_dir(config)
self.category = utils.get_category(config, self.cache_dir if os.path.exists(self.cache_dir) else None)
self.draw_bbox = utils.visualize.DrawBBox(self.category, colors=args.colors, thickness=args.thickness)
self.anchors = torch.from_numpy(utils.get_anchors(config)).contiguous()
self.height, self.width = tuple(map(int, config.get('image', 'size').split()))
self.path, self.step, self.epoch = utils.train.load_model(self.model_dir)
state_dict = torch.load(self.path, map_location=lambda storage, loc: storage)
self.dnn = utils.parse_attr(config.get('model', 'dnn'))(model.ConfigChannels(config, state_dict), self.anchors, len(self.category))
self.dnn.load_state_dict(state_dict)
self.inference = model.Inference(config, self.dnn, self.anchors)
self.inference.eval()
if torch.cuda.is_available():
self.inference.cuda()
logging.info(humanize.naturalsize(sum(var.cpu().numpy().nbytes for var in self.inference.state_dict().values())))
self.cap = self.create_cap()
self.keys = set(args.keys)
self.resize = transform.parse_transform(config, config.get('transform', 'resize_test'))
self.transform_image = transform.get_transform(config, config.get('transform', 'image_test').split())
self.transform_tensor = transform.get_transform(config, config.get('transform', 'tensor').split())
示例2: main
# 需要導入模塊: import humanize [as 別名]
# 或者: from humanize import naturalsize [as 別名]
def main():
args = make_args()
config = configparser.ConfigParser()
utils.load_config(config, args.config)
for cmd in args.modify:
utils.modify_config(config, cmd)
with open(os.path.expanduser(os.path.expandvars(args.logging)), 'r') as f:
logging.config.dictConfig(yaml.load(f))
height, width = tuple(map(int, config.get('image', 'size').split()))
cache_dir = utils.get_cache_dir(config)
model_dir = utils.get_model_dir(config)
category = utils.get_category(config, cache_dir if os.path.exists(cache_dir) else None)
anchors = utils.get_anchors(config)
anchors = torch.from_numpy(anchors).contiguous()
path, step, epoch = utils.train.load_model(model_dir)
state_dict = torch.load(path, map_location=lambda storage, loc: storage)
dnn = utils.parse_attr(config.get('model', 'dnn'))(model.ConfigChannels(config, state_dict), anchors, len(category))
inference = model.Inference(config, dnn, anchors)
inference.eval()
logging.info(humanize.naturalsize(sum(var.cpu().numpy().nbytes for var in inference.state_dict().values())))
dnn.load_state_dict(state_dict)
image = torch.autograd.Variable(torch.randn(args.batch_size, 3, height, width), volatile=True)
path = model_dir + '.onnx'
logging.info('save ' + path)
torch.onnx.export(dnn, image, path, export_params=True, verbose=args.verbose) # PyTorch's bug
示例3: __init__
# 需要導入模塊: import humanize [as 別名]
# 或者: from humanize import naturalsize [as 別名]
def __init__(self, args, config):
self.args = args
self.config = config
self.model_dir = utils.get_model_dir(config)
self.category = utils.get_category(config)
self.anchors = torch.from_numpy(utils.get_anchors(config)).contiguous()
self.dnn = utils.parse_attr(config.get('model', 'dnn'))(model.ConfigChannels(config), self.anchors, len(self.category))
self.dnn.eval()
logging.info(humanize.naturalsize(sum(var.cpu().numpy().nbytes for var in self.dnn.state_dict().values())))
if torch.cuda.is_available():
self.dnn.cuda()
self.height, self.width = tuple(map(int, config.get('image', 'size').split()))
output = self.dnn(torch.autograd.Variable(utils.ensure_device(torch.zeros(1, 3, self.height, self.width)), volatile=True))
_, _, self.rows, self.cols = output.size()
self.i, self.j = self.rows // 2, self.cols // 2
self.output = output[:, :, self.i, self.j]
dataset = Dataset(self.height, self.width)
try:
workers = self.config.getint('data', 'workers')
except configparser.NoOptionError:
workers = multiprocessing.cpu_count()
self.loader = torch.utils.data.DataLoader(dataset, batch_size=self.args.batch_size, num_workers=workers)
示例4: __str__
# 需要導入模塊: import humanize [as 別名]
# 或者: from humanize import naturalsize [as 別名]
def __str__(self):
"""Format model description as a string."""
try:
dump = self.dump()
except NotImplementedError:
dump = ""
except AttributeError:
return repr(self)
if dump:
dump = "\n" + dump
meta = deepcopy(self.meta)
meta["created_at"] = format_datetime(meta["created_at"])
meta["size"] = humanize.naturalsize(self.size)
try:
meta["environment"]["packages"] = \
" ".join("%s==%s" % tuple(p) for p in self.environment["packages"])
except KeyError:
pass
return "%s%s" % (pformat(meta, width=1024), dump)
示例5: extract_model_meta
# 需要導入模塊: import humanize [as 別名]
# 或者: from humanize import naturalsize [as 別名]
def extract_model_meta(base_meta: dict, extra_meta: dict, model_url: str) -> dict:
"""
Merge the metadata from the backend and the extra metadata into a dict which is suitable for \
`index.json`.
:param base_meta: tree["meta"] :class:`dict` containing data from the backend.
:param extra_meta: dict containing data from the user, similar to `meta.json`.
:param model_url: public URL of the model.
:return: converted dict.
"""
meta = {"default": {"default": base_meta["uuid"],
"description": base_meta["description"],
"code": extra_meta["code"]}}
del base_meta["model"]
del base_meta["uuid"]
meta["model"] = base_meta
meta["model"].update({k: extra_meta[k] for k in ("code", "datasets", "references", "tags",
"extra")})
response = requests.get(model_url, stream=True)
meta["model"]["size"] = humanize.naturalsize(int(response.headers["content-length"]))
meta["model"]["url"] = model_url
meta["model"]["created_at"] = format_datetime(meta["model"]["created_at"])
return meta
示例6: summarise_usage
# 需要導入模塊: import humanize [as 別名]
# 或者: from humanize import naturalsize [as 別名]
def summarise_usage():
wall_time = humanize.naturaldelta(time.time() - __before)
user_time = humanize.naturaldelta(os.times().user)
sys_time = os.times().system
if resource is None:
# Don't report max memory on Windows. We could do this using the psutil lib, via
# psutil.Process(os.getpid()).get_ext_memory_info().peak_wset if demand exists
maxmem_str = ""
else:
max_mem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
if sys.platform != "darwin":
max_mem *= 1024 # Linux and other OSs (e.g. freeBSD) report maxrss in kb
maxmem_str = "; max memory={}".format(
humanize.naturalsize(max_mem, binary=True)
)
logger.info("wall time = {}".format(wall_time))
logger.info("rusage: user={}; sys={:.2f}s".format(user_time, sys_time) + maxmem_str)
示例7: add_rom
# 需要導入模塊: import humanize [as 別名]
# 或者: from humanize import naturalsize [as 別名]
def add_rom(codename, link, info):
update = {}
file_size = naturalsize(int(get(link, stream=True).headers['Content-Length']))
file = link.split('/')[-1]
version = link.split('/')[3]
android = link.split('_')[-2]
update.update({"android": android})
update.update({"codename": codename})
update.update({"device": info['name']})
update.update({"download": link})
update.update({"filename": file})
update.update({"size": file_size})
update.update({"md5": "null"})
update.update({"version": version})
DATA.append(update)
with open(f'stable_fastboot/{codename}.yml', 'w', newline='\n') as output:
yaml.dump(update, output, Dumper=yaml.CDumper)
示例8: _handle_file
# 需要導入模塊: import humanize [as 別名]
# 或者: from humanize import naturalsize [as 別名]
def _handle_file(self, them_d):
file_data = them_d["file"]
self.abs_destname = self._decide_destname("file",
file_data["filename"])
self.xfersize = file_data["filesize"]
free = estimate_free_space(self.abs_destname)
if free is not None and free < self.xfersize:
self._msg(u"Error: insufficient free space (%sB) for file (%sB)" %
(free, self.xfersize))
raise TransferRejectedError()
self._msg(u"Receiving file (%s) into: %s" %
(naturalsize(self.xfersize),
os.path.basename(self.abs_destname)))
self._ask_permission()
tmp_destname = self.abs_destname + ".tmp"
return open(tmp_destname, "wb")
示例9: about
# 需要導入模塊: import humanize [as 別名]
# 或者: from humanize import naturalsize [as 別名]
def about(self, context):
"""Tells you information about the bot itself."""
# this command is based off of code provided by Rapptz under the MIT license
# https://github.com/Rapptz/RoboDanny/blob/f6638d520ea0f559cb2ae28b862c733e1f165970/cogs/stats.py
# Copyright © 2015 Rapptz
embed = discord.Embed(description=self.bot.config['description'])
embed.add_field(name='Latest changes', value=await self._latest_changes(), inline=False)
owner = self.bot.get_user(self.bot.config.get('primary_owner', self.bot.owner_id))
embed.set_author(name=str(owner), icon_url=owner.avatar_url)
embed.add_field(name='Servers', value=await self.bot.cogs['Stats'].guild_count())
cpu_usage = self.process.cpu_percent() / psutil.cpu_count()
mem_usage = humanize.naturalsize(self.process.memory_full_info().uss)
embed.add_field(name='Process', value=f'{mem_usage}\n{cpu_usage:.2f}% CPU')
embed.add_field(name='Uptime', value=self.bot.cogs['BotBinMisc'].uptime(brief=True))
embed.set_footer(text='Made with discord.py', icon_url='https://i.imgur.com/5BFecvA.png')
await context.send(embed=embed)
示例10: __init__
# 需要導入模塊: import humanize [as 別名]
# 或者: from humanize import naturalsize [as 別名]
def __init__(self, api, add_time, file_id, info_hash, last_update,
left_time, move, name, peers, percent_done, rate_download,
size, status, cid, pid, url, *args, **kwargs):
self.api = api
self.cid = cid
self.name = name
self.add_time = add_time
self.file_id = file_id
self.info_hash = info_hash
self.last_update = last_update
self.left_time = left_time
self.move = move
self.peers = peers
self.percent_done = percent_done
self.rate_download = rate_download
self.size = size
self.size_human = humanize.naturalsize(size, binary=True)
self.status = status
self.url = url
self._directory = None
self._deleted = False
self._count = -1
示例11: mega_dl
# 需要導入模塊: import humanize [as 別名]
# 或者: from humanize import naturalsize [as 別名]
def mega_dl(url: str) -> str:
""" MEGA.nz direct links generator
Using https://github.com/tonikelope/megadown"""
reply = ''
try:
link = re.findall(r'\bhttps?://.*mega.*\.nz\S+', url)[0]
except IndexError:
reply = "`No MEGA.nz links found`\n"
return reply
command = f'megadown -q -m {link}'
result = popen(command).read()
try:
data = json.loads(result)
print(data)
except json.JSONDecodeError:
reply += "`Error: Can't extract the link`\n"
return reply
dl_url = data['url']
name = data['name']
size = naturalsize(int(data['file_size']))
reply += f'[{name} ({size})]({dl_url})\n'
return reply
示例12: cm_ru
# 需要導入模塊: import humanize [as 別名]
# 或者: from humanize import naturalsize [as 別名]
def cm_ru(url: str) -> str:
""" cloud.mail.ru direct links generator
Using https://github.com/JrMasterModelBuilder/cmrudl.py"""
reply = ''
try:
link = re.findall(r'\bhttps?://.*cloud\.mail\.ru\S+', url)[0]
except IndexError:
reply = "`No cloud.mail.ru links found`\n"
return reply
command = f'cmrudl -s {link}'
result = popen(command).read()
result = result.splitlines()[-1]
try:
data = json.loads(result)
except json.decoder.JSONDecodeError:
reply += "`Error: Can't extract the link`\n"
return reply
dl_url = data['download']
name = data['file_name']
size = naturalsize(int(data['file_size']))
reply += f'[{name} ({size})]({dl_url})\n'
return reply
示例13: info
# 需要導入模塊: import humanize [as 別名]
# 或者: from humanize import naturalsize [as 別名]
def info(self, ctx):
"""Retrieve various Node/Server/Player information."""
player = self.bot.wavelink.get_player(ctx.guild.id)
node = player.node
used = humanize.naturalsize(node.stats.memory_used)
total = humanize.naturalsize(node.stats.memory_allocated)
free = humanize.naturalsize(node.stats.memory_free)
cpu = node.stats.cpu_cores
fmt = f'**WaveLink:** `{wavelink.__version__}`\n\n' \
f'Connected to `{len(self.bot.wavelink.nodes)}` nodes.\n' \
f'Best available Node `{self.bot.wavelink.get_best_node().__repr__()}`\n' \
f'`{len(self.bot.wavelink.players)}` players are distributed on nodes.\n' \
f'`{node.stats.players}` players are distributed on server.\n' \
f'`{node.stats.playing_players}` players are playing on server.\n\n' \
f'Server Memory: `{used}/{total}` | `({free} free)`\n' \
f'Server CPU: `{cpu}`\n\n' \
f'Server Uptime: `{datetime.timedelta(milliseconds=node.stats.uptime)}`'
await ctx.send(fmt)
示例14: stream_closed
# 需要導入模塊: import humanize [as 別名]
# 或者: from humanize import naturalsize [as 別名]
def stream_closed(self, stream, **kw):
# print("closed", stream, self._active)
if stream.id not in self._active:
print(
"Previously unknown stream to {stream.target_host} died".format(
stream=stream,
)
)
else:
bw = self._active[stream.id]
print(
"Stream {stream.id} to {stream.target_host}: {read} read, {written} written in {duration:.1f}s ({read_rate})".format(
stream=stream,
read=util.colors.green(humanize.naturalsize(bw.bytes_read())),
written=util.colors.red(humanize.naturalsize(bw.bytes_written())),
read_rate=humanize.naturalsize(sum(bw.rate())) + '/s',
duration=bw.duration(),
)
)
示例15: _draw_node
# 需要導入模塊: import humanize [as 別名]
# 或者: from humanize import naturalsize [as 別名]
def _draw_node(self, node, edge):
if hasattr(node, 'variable'):
name = self.var_name[node.variable.data._cdata]
tensor = self.state_dict[name]
label = '\n'.join(map(str, filter(lambda x: x is not None, [
'%d: %s' % (self.index, name),
type(self)._pretty_size(tensor.size(), edge),
humanize.naturalsize(tensor.numpy().nbytes),
])))
self.dot.node(str(id(node)), label, shape='note')
else:
name = type(node).__name__
label = '%d: %s' % (self.index, name)
self.dot.node(str(id(node)), label, fillcolor='white')