Writing Python Code More Concisely Than Perl!?

Liste des GroupesRevenir à cl misc 
Sujet : Writing Python Code More Concisely Than Perl!?
De : ldo (at) *nospam* nz.invalid (Lawrence D'Oliveiro)
Groupes : comp.lang.misc comp.programming
Date : 19. May 2024, 06:17:35
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v2c21f$37ibf$2@dont-email.me>
User-Agent : Pan/0.155 (Kherson; fc5a80b8)
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.

Date Sujet#  Auteur
19 May 24 o Writing Python Code More Concisely Than Perl!?1Lawrence D'Oliveiro

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal