Groups | Search | Server Info | Login | Register
Groups > comp.programming > #16760
| From | Lawrence D'Oliveiro <ldo@nz.invalid> |
|---|---|
| Newsgroups | comp.lang.misc, comp.programming |
| Subject | Writing Python Code More Concisely Than Perl!? |
| Date | 2024-05-19 05:17 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <v2c21f$37ibf$2@dont-email.me> (permalink) |
Cross-posted to 2 groups.
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.
Back to comp.programming | Previous | Next — Next in thread | Find similar
Writing Python Code More Concisely Than Perl!? Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-05-19 05:17 +0000
Re: Writing Python Code More Concisely Than Perl!? c186282 <c186282@nnada.net> - 2025-06-13 23:31 -0400
Re: Writing Python Code More Concisely Than Perl!? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-06-14 02:25 -0700
Re: Writing Python Code More Concisely Than Perl!? c186282 <c186282@nnada.net> - 2025-06-15 00:58 -0400
Re: Writing Python Code More Concisely Than Perl!? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-06-14 23:04 -0700
Re: Writing Python Code More Concisely Than Perl!? Brian Morrison <news@fenrir.org.uk> - 2025-06-15 14:46 +0100
Re: Writing Python Code More Concisely Than Perl!? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-06-15 13:44 -0700
Re: Writing Python Code More Concisely Than Perl!? Brian Morrison <news@fenrir.org.uk> - 2025-06-16 18:32 +0100
Re: Writing Python Code More Concisely Than Perl!? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-06-16 11:38 -0700
Re: Writing Python Code More Concisely Than Perl!? Lawrence D'Oliveiro <ldo@nz.invalid> - 2025-06-15 21:02 +0000
Re: Writing Python Code More Concisely Than Perl!? Brian Morrison <news@fenrir.org.uk> - 2025-06-16 18:34 +0100
Re: Writing Python Code More Concisely Than Perl!? Lawrence D'Oliveiro <ldo@nz.invalid> - 2025-06-16 23:07 +0000
Re: Writing Python Code More Concisely Than Perl!? c186282 <c186282@nnada.net> - 2025-06-17 23:38 -0400
Re: Writing Python Code More Concisely Than Perl!? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-06-18 09:27 -0700
Re: Writing Python Code More Concisely Than Perl!? c186282 <c186282@nnada.net> - 2025-06-19 01:30 -0400
Re: Writing Python Code More Concisely Than Perl!? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-06-19 02:23 -0700
Re: Writing Python Code More Concisely Than Perl!? c186282 <c186282@nnada.net> - 2025-06-20 01:27 -0400
Re: Writing Python Code More Concisely Than Perl!? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-06-20 12:32 -0700
Re: Writing Python Code More Concisely Than Perl!? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-08-06 16:30 +0200
Re: Writing Python Code More Concisely Than Perl!? John Ames <commodorejohn@gmail.com> - 2025-08-06 08:17 -0700
Re: Writing Python Code More Concisely Than Perl!? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-08-07 03:49 +0200
csiph-web