On 2022-06-08 at 08:07:40 -0000, De ongekruisigde <ongekruisi...@news.eternal-september.org> wrote:
> Depending on the problem a regular expression may be the much simpler > solution. I love them for e.g. text parsing and use them all the time. > Unrivaled when e.g. parts of text have to be extracted, e.g. from lines > like these: > > root:x:0:0:System administrator:/root:/run/current-system/sw/bin/bash > dhcpcd:x:995:991::/var/empty:/run/current-system/sw/bin/nologin > nm-iodine:x:996:57::/var/empty:/run/current-system/sw/bin/nologin > avahi:x:997:996:avahi-daemon privilege separation > user:/var/empty:/run/current-system/sw/bin/nologin > sshd:x:998:993:SSH privilege separation > user:/var/empty:/run/current-system/sw/bin/nologin > geoclue:x:999:998:Geoinformation > service:/var/lib/geoclue:/run/current-system/sw/bin/nologin > > Compare a regexp solution like this: > > >>> g = re.search(r'([^:]*):([^:]*):(\d+):(\d+):([^:]*):([^:]*):(.*)$' , s) > >>> print(g.groups()) > ('geoclue', 'x', '999', '998', 'Geoinformation service', > '/var/lib/geoclue', '/run/current-system/sw/bin/nologin') > > to the code one would require to process it manually, with all the edge > cases. The regexp surely reads much simpler (?). Uh... >>> import pwd # https://docs.python.org/3/library/pwd.html >>> [x for x in pwd.getpwall() if x[0] == 'geoclue'] [pwd.struct_passwd(pw_name='geoclue', pw_passwd='x', pw_uid=992, pw_gid=992, pw_gecos='Geoinformation service', pw_dir='/var/lib/geoclue', pw_shell='/sbin/nologin')] -- https://mail.python.org/mailman/listinfo/python-list