Deutsch English Français Italiano |
<v2c21f$37ibf$2@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!news.nobody.at!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Lawrence D'Oliveiro <ldo@nz.invalid> Newsgroups: comp.lang.misc,comp.programming Subject: Writing Python Code More Concisely Than Perl!? Date: Sun, 19 May 2024 05:17:35 -0000 (UTC) Organization: A noiseless patient Spider Lines: 76 Message-ID: <v2c21f$37ibf$2@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Date: Sun, 19 May 2024 07:17:36 +0200 (CEST) Injection-Info: dont-email.me; posting-host="b84845553f5d38789493b2957a94d439"; logging-data="3393903"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/fniI/DR7gIh1B+4FXkLiL" User-Agent: Pan/0.155 (Kherson; fc5a80b8) Cancel-Lock: sha1:eBFLCEJyGTVmqURaBRNSNgVnSkE= Bytes: 3475 Been doing some LDAP stuff lately, and I came across the “migrationtools” package <https://gitlab.com/future-ad-laboratory/migrationtools> for converting the contents of /etc/passwd and family to LDAP records. This is a bunch of Perl code, full of lines like these: if ($shell) { print $HANDLE "loginShell: $shell\n"; } if ($uid ne "") { print $HANDLE "uidNumber: $uid\n"; } else { print $HANDLE "uidNumber:\n"; } if ($gid ne "") { print $HANDLE "gidNumber: $gid\n"; } else { print $HANDLE "gidNumber:\n"; } if ($homedir) { print $HANDLE "homeDirectory: $homedir\n"; } else { print $HANDLE "homeDirectory:\n"; } Perl is supposed to be famous, even notorious, for the conciseness of its code, but I think whoever created this originally didn’t get that memo. I created an alternative tool <https://bitbucket.org/ldo17/passwd_to_ldap>, focusing just on the passwd, shadow and group files, and leaving out the macOS compatibility. My code for writing out a single LDIF record is basically this: write_attr \ ( out, "dn", "%s=%s,%s" % (table.dn_field, escape_dn(entry[table.keyfield]), tabledn) ) for objclass in table.object_classes : write_attr(out, "objectClass", objclass) #end for write_attr(out, "objectClass", "top") for field, key in table.ldap_mapping : if key in entry : value = entry[key] if isinstance(value, (list, tuple)) : for item in value : write_attr(out, field, item) #end for else : write_attr(out, field, value) #end if #end if #end for out.write("\n") If you total the sizes of migrate_passwd.pl and migrate_group.pl, you get 496 lines (not including migrate_common.ph). My entire script is just 341 lines. Of course, what I didn’t show you above is the table of rules that drives that common LDIF-writing code, to steer the different processing of the different files and their fields. But that complete table is just 63 lines. This is quite common with table-driven aka data-driven programming: you might think that factoring out common code into a more generic form, with the different cases defined in a data structure, just moves the complexity from one place to another, but in fact it is usually the case that you end up with less code overall.