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"]))
with open('/proc/meminfo') as meminfo:
info = {
entry[0][:-1]: int(entry[1])
for line in meminfo
if (entry := line.split())
}
Jon Ribbens <jon+usenet@unequivocal.eu> writes:
with open('/proc/meminfo') as meminfo:
info = {
entry[0][:-1]: int(entry[1])
for line in meminfo
if (entry := line.split())
}
with open('/proc/meminfo') as meminfo:
info = dict(line.split()[:2] for line in meminfo)
On 2026-06-13, Paul Rubin <no.email@nospam.invalid> wrote:
Jon Ribbens <jon+usenet@unequivocal.eu> writes:
with open('/proc/meminfo') as meminfo:
info = {
entry[0][:-1]: int(entry[1])
for line in meminfo
if (entry := line.split())
}
with open('/proc/meminfo') as meminfo:
info = dict(line.split()[:2] for line in meminfo)
An excellent point, although it does mean the ':' characters remain
in the dictionary keys.
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"]))
Jon Ribbens <jon+usenet@unequivocal.eu> 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,
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')
Lawrence's version used your neat dict(line.split()[:2] ...) trick.
Jon Ribbens <jon+usenet@unequivocal.eu> writes:
Lawrence's version used your neat dict(line.split()[:2] ...) trick.
Maybe this:
dict((a,int(b)) for a,b in (x.split()[:2] for x in xs))
I feel like there should be a way to do this with the := operator
instead of the nested generators, but it doesn't seem to be there.
with open('/proc/meminfo') as meminfo:
info = dict(line.split()[:2] for line in meminfo)
I feel like there should be a way to do this with the := operator
instead of the nested generators, but it doesn't seem to be there.
KeyError: 'MemTotal'
Yeah you have to use 'MemTotal:' as the key. But, you still have to
convert the digit strings to integers.
{e[0][:-1]: int(e[1]) for x in xs if (e := x.split())}
Jon Ribbens <jon+usenet@unequivocal.eu> writes:
{e[0][:-1]: int(e[1]) for x in xs if (e := x.split())}
Does this work?
{ a : int(b)) for x in xs if (a,b := x.split()) }
I think it's ok to keep the ':' in the field name. Otherwise
{ a.rstrip(':') : int(b)) for x in xs if (a,b := x.split()) }
seems a bit more explicit at the expense of a few more chars.
{ a : int(b)) for x in xs if (a,b := x.split()) }No, for three reasons. Firstly, the lines with units result in x.split() having 3 members, so you can't assign it to a 2-tuple.
Secondly, it appears that (a, b := x) means "create a tuple whose
first member is a and whose second member is x, and also assign x to
b", which is not at all what we need.
Jon Ribbens <jon+usenet@unequivocal.eu> writes:
{ a : int(b)) for x in xs if (a,b := x.split()) }No, for three reasons. Firstly, the lines with units result in x.split()
having 3 members, so you can't assign it to a 2-tuple.
Oh yes I had intended to say x.split()[:2] but somehow left that out.
Secondly, it appears that (a, b := x) means "create a tuple whose
first member is a and whose second member is x, and also assign x to
b", which is not at all what we need.
Yuck, I had expected tuple unpacking. Sounds like a pitfall comparable
to "=" vs "==" that kept the := operator out of the language for so
long. Oh well.
| Sysop: | Tetrazocine |
|---|---|
| Location: | Melbourne, VIC, Australia |
| Users: | 11 |
| Nodes: | 8 (0 / 8) |
| Uptime: | 119:02:28 |
| Calls: | 219 |
| Files: | 21,503 |
| Messages: | 82,388 |