On 2026-06-12, Lawrence D’Oliveiro <[email protected]> wrote:
> So I looked at the example Gambas program at
><https://gambaswiki.org/wiki/doc/whatisgambas> and marvelled at its
> unnecessary complexity (UUOC, even). It’s like PHP with variable
> declarations!
>
> Here’s my version: excluding header comments and blank lines and
> shebang line, it’s about half the size.
>
> info = dict \
>   (
>     (entry[0][:-1], int(entry[-2]))
>     for line in open("/proc/meminfo", "rt")
>     for entry in (tuple(s.strip() for s in line.split(" ")),)
>     if entry[-2] != ""
>   )
> print("Used memory: %d Kb" % (info["MemTotal"] - info["MemFree"] - 
> info["Buffers"] - info["Cached"] + info["SwapTotal"] - info["SwapFree"] - 
> info["SwapCached"]))

Better:

  with open('/proc/meminfo') as meminfo:
      info = {
          entry[0][:-1]: int(entry[1])
          for line in meminfo
          if (entry := line.split())
      }
  print('Used memory: %d kB' % (
      info['MemTotal'] - info['MemFree'] - info['Buffers'] - info['Cached'] +
      info['SwapTotal'] - info['SwapFree'] - info['SwapCached']
  ))
-- 
https://mail.python.org/mailman3//lists/python-list.python.org

Reply via email to