简体   繁体   English

如何比较存储大小的字符串

[英]How to compare strings that are storage sizes

I am trying to write a wrapper script in python to extend logical volumes.我正在尝试用 python 编写一个包装脚本来扩展逻辑卷。 I am giving it a desired size, and it's able to get the exiting size.我给它一个所需的大小,它能够获得现有的大小。

existing_dict{} = {'root': '5.00g', 'opt': '10.00g', 'var': '20.00g'}
desired_dict{} = {'root': '10g', 'opt': '20g', 'var': '100%'}

How exactly do I compare the existing size value to the desired size value so I can take some action on it?我如何准确地将现有大小值与所需大小值进行比较,以便我可以对其采取一些措施? So say the desired is same as existing then do nothing.所以说想要的与现有的相同,然后什么都不做。 If existing size is bigger than desired size then throw an error.如果现有大小大于所需大小,则抛出错误。 If desired size is bigger than existing size, try to lvextend.如果所需大小大于现有大小,请尝试 lvextend。 Or, lastly, if the desired size is a percentage, then perform the lvextend with the percentage value.或者,最后,如果所需大小是百分比,则使用百分比值执行 lvextend。

I think you can start with breaking down the string to kb or bytes, and then compare them eg 5.0 G = 5x1024x1024 bytes我认为您可以先将字符串分解为 kb 或字节,然后比较它们,例如 5.0 G = 5x1024x1024 字节

def convert_to_bytes(size):
    return size*1024*1024
existing_size = convert_to_bytes(size1)
required_size = convert_to_bytes(size2)
if existing_size < required_size:
    #resize
else:
   raise Exception()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM