Skip to content

Instantly share code, notes, and snippets.

@JuniorPolegato
Last active July 6, 2017 20:43
Show Gist options
  • Save JuniorPolegato/d0c81ca7432fd05f49378cb887edb684 to your computer and use it in GitHub Desktop.
Save JuniorPolegato/d0c81ca7432fd05f49378cb887edb684 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import math
import random
import locale
# Locale para utilizar agrupamento e ponto decimal da localização configurada
locale.setlocale(locale.LC_ALL, '')
# Constantes
BASE_LETRA = ('', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y', 'B') # k minúsculo
SUFIXO_LETRA = 'iB' # pode ser 'B' e abaixo 1000
BASE_LOG = 1024 # pode ser 1000 e acima B
CASAS = 2
def hum(B):
i = int(math.log(B, BASE_LOG))
c = 0 if B < BASE_LOG else CASAS
v = locale.format('%%.%if' % c, float(B) / BASE_LOG ** i, grouping=True)
return '%s %s%s' % (v, BASE_LETRA[i], SUFIXO_LETRA)
if __name__ == '__main__':
for i in range(1, len(BASE_LETRA)):
B = random.randint(0, BASE_LOG ** (i + 1))
print('%12s <=> %i bytes' % (hum(B), B))
@tenflavio
Copy link

Bom dia.
Show de bola.

Ao testar com o tamanho 1737776592, resulta em 1,74 GiB <=> 1737776592 bytes

Não deveria ser 1,74 TB ?

du -sh /backup/backups/

1,7T /backup/backups/

du -s /backup/backups/

1737776592 /backup/backups/

@JuniorPolegato
Copy link
Author

Olá!

Se ver na documentação do "du", na parte "GNU", está escrito:

DETALHES GNU
A saída padrão é em unidades de 1024 bytes (quando nenhuma unidade é especificada por opções), a menos que a variável de ambiente POSIXLY_CORRECT seja selecionada, no caso em que o POSIX é seguido.

Portanto esse número está em kiB e não em bytes como você relata, dessa forma 1737776592 é realmente 1,74 GiB e não 1,74 TiB. Nesse caso deve multiplicar o número obtido com "du" por 1024 e depois usar a função "hum".

Abraços.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment