Skip to content

Instantly share code, notes, and snippets.

@s3rgeym
Last active April 17, 2026 14:02
Show Gist options
  • Select an option

  • Save s3rgeym/acbaf70b0a5152dd6002c91ab018f586 to your computer and use it in GitHub Desktop.

Select an option

Save s3rgeym/acbaf70b0a5152dd6002c91ab018f586 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import re
SIZE_UNITS = {"kB": 1 << 10, "MB": 1 << 20, "GB": 1 << 30}
def parse_size(v):
s, u = v.split()
return float(s) * SIZE_UNITS[u]
def calculate_flatpak(data):
downloaded_size = total_size = 0
unit_pat = r'(\d+(?:\.\d+)? [kMG]?B)'
for a, b in re.findall(f'{unit_pat} / {unit_pat}$', data, flags=re.M):
downloaded_size += parse_size(a)
total_size += parse_size(b)
return downloaded_size, total_size
def human_size(b: float) -> str:
for k, v in SIZE_UNITS.items():
if v > b:
break
return f"{b / v:.2f} {k}"
if __name__ == "__main__":
d, t = calculate_flatpak("""\
1. [✓] com.github.PintaProject.Pinta.Locale stable u flathub 1.6 kB / 715.2 kB
2. [✓] io.github.kolunmi.Bazaar.Locale stable u flathub 5.5 kB / 1.4 MB
3. [✓] io.github.ungoogled_software.ungoogled_chromium.Locale stable u flathub 406.8 kB / 19.0 MB
4. [✓] org.gnome.Platform.Locale 49 u flathub 18.5 kB / 385.7 MB
5. [✓] org.gimp.GIMP stable u flathub 1.3 MB / 99.4 MB
6. [✓] io.github.kolunmi.Bazaar stable u flathub 709.1 kB / 7.8 MB
7. [✓] org.gnome.Platform 49 u flathub 96.5 MB / 408.6 MB
8. [✓] org.telegram.desktop stable u flathub 77.6 MB / 92.7 MB
9. [✓] io.dbeaver.DBeaverCommunity stable u flathub 55.6 MB / 162.2 MB
10. [✓] com.github.PintaProject.Pinta stable u flathub 850.6 kB / 53.6 MB
11. [✓] io.github.ungoogled_software.ungoogled_chromium stable u flathub 121.5 MB / 152.2 MB
12. [✓] com.obsproject.Studio stable u flathub 302.7 kB / 202.8 MB
13. [✓] org.kde.Platform.Locale 6.10 u flathub 18.5 kB / 400.7 MB
14. [✓] org.kde.Platform 6.10 u flathub 19.2 MB / 387.1 MB
15. [✓] org.kde.kdenlive.Locale stable u flathub 4.8 kB / 6.1 MB
16. [✓] org.kde.kdenlive stable u flathub 66.4 MB / 101.9 MB
17. [✓] org.kde.krita.Locale stable u flathub 1.5 kB / 14.2 MB
18. [✓] org.kde.krita stable u flathub 74.6 MB / 262.5 MB\
""")
print("Downloaded Size:", human_size(d))
print("Total Size:", human_size(t))
print(f"Download/Total: {d / t:.2}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment