comparison sat/tools/common/utils.py @ 4037:524856bd7b19

massive refactoring to switch from camelCase to snake_case: historically, Libervia (SàT before) was using camelCase as allowed by PEP8 when using a pre-PEP8 code, to use the same coding style as in Twisted. However, snake_case is more readable and it's better to follow PEP8 best practices, so it has been decided to move on full snake_case. Because Libervia has a huge codebase, this ended with a ugly mix of camelCase and snake_case. To fix that, this patch does a big refactoring by renaming every function and method (including bridge) that are not coming from Twisted or Wokkel, to use fully snake_case. This is a massive change, and may result in some bugs.
author Goffi <goffi@goffi.org>
date Sat, 08 Apr 2023 13:54:42 +0200
parents 584379473925
children
comparison
equal deleted inserted replaced
4036:c4464d7ae97b 4037:524856bd7b19
109 109
110 def update(self, items): 110 def update(self, items):
111 self._dict.update({i: None for i in items}) 111 self._dict.update({i: None for i in items})
112 112
113 113
114 def parseSize(size): 114 def parse_size(size):
115 """Parse a file size with optional multiple symbole""" 115 """Parse a file size with optional multiple symbole"""
116 try: 116 try:
117 return int(size) 117 return int(size)
118 except ValueError: 118 except ValueError:
119 number = [] 119 number = []
141 return number 141 return number
142 except Exception as e: 142 except Exception as e:
143 raise ValueError(f"invalid size: {e}") 143 raise ValueError(f"invalid size: {e}")
144 144
145 145
146 def getSizeMultiplier(size, suffix="o"): 146 def get_size_multiplier(size, suffix="o"):
147 """Get multiplier of a file size""" 147 """Get multiplier of a file size"""
148 size = int(size) 148 size = int(size)
149 #  cf. https://stackoverflow.com/a/1094933 (thanks) 149 #  cf. https://stackoverflow.com/a/1094933 (thanks)
150 for unit in ["", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"]: 150 for unit in ["", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"]:
151 if abs(size) < 1024.0: 151 if abs(size) < 1024.0:
152 return size, f"{unit}{suffix}" 152 return size, f"{unit}{suffix}"
153 size /= 1024.0 153 size /= 1024.0
154 return size, f"Yi{suffix}" 154 return size, f"Yi{suffix}"
155 155
156 156
157 def getHumanSize(size, suffix="o", sep=" "): 157 def get_human_size(size, suffix="o", sep=" "):
158 size, symbol = getSizeMultiplier(size, suffix) 158 size, symbol = get_size_multiplier(size, suffix)
159 return f"{size:.2f}{sep}{symbol}" 159 return f"{size:.2f}{sep}{symbol}"