On 2026-06-13, Paul Rubin <[email protected]> wrote:
> Jon Ribbens <[email protected]> writes:
>
>> Oh, and doesn't have ints as the dictionary values, which is rather
>> more fatal to the use-case.
>
>> print("Used memory: %d Kb" % (info["MemTotal"] - info["MemFree"] -
>> info["Buffers"] - info["Cached"] + info["SwapTotal"] - info["SwapFree"] -
>> info["SwapCached"]))
>
> It really should look at the units in the meminfo file too, but anyway,
Yes, although that's not entirely possible since proc_meminfo(5) doesn't
document what units are possible (although in actual fact it appears
that every line is hard-coded to either say 'kB' or no unit, no other
options are available and which lines have 'kB' or not never changes).
> alloced = ['MemTotal', 'SwapTotal']
> free = ['MemFree, 'Buffers', 'Cached', 'SwapFree', 'SwapCached']
>
> def total(xs): return sum(int(info[f'{x}:']) for x in xs)
> print(f''Used memory: {total(alloced) - total(free)} Kb')
Yes, obviously it is physically possible to write the code to cope with
the altered data structure. Just, there's a reason that neither my nor
Lawrence's version used your neat dict(line.split()[:2] ...) trick.
--
https://mail.python.org/mailman3//lists/python-list.python.org