Re: Writing Python Code More Concisely Than Perl!?

Liste des GroupesRevenir à l misc 
Sujet : Re: Writing Python Code More Concisely Than Perl!?
De : c186282 (at) *nospam* nnada.net (c186282)
Groupes : comp.lang.misc comp.programming
Date : 14. Jun 2025, 04:31:59
Autres entêtes
Message-ID : <2x2dnWlc86XdcNH1nZ2dnZfqnPidnZ2d@giganews.com>
References : 1
User-Agent : Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0
On 5/19/24 1:17 AM, Lawrence D'Oliveiro wrote:
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.
   PERL *can* be concise. It's also closer to a 'shell-script'
   language, which makes it more challenging to write AND
   understand six months later.
   Frankly, Python is just generally 'better' these days.
   Maybe not AS 'concise' but more 'readable' AND easier
   to understand six months from now. The speed is now
   'adequate' and Python-4 is supposed to be even faster.
   There is some computer stuff that really should be done
   in 'C' (I remember when it was the cool NEW lang !) ...
   but now I'm far more likely to write in Python for the
   abovementioned reasons.
   And hey, BASIC still exists ... though not as nicely
   structured it can STILL get the job done. Also consider
   one of the 'C-shells'.
   Since the 60s, seems like EVERYBODY had their "better
   idea" about programming languages and styles. However
   only a very FEW have stood the test of time. I can
   still write some COBOL and FORTRAN ... occasionally
   do so Just For Fun ... but their overall utility has
   greatly diminished compared to later langs.
   (I *do* still often write in PASCAL though - see
   it as a kind of 'poetry' :-)

Date Sujet#  Auteur
14 Jun 25 * Re: Writing Python Code More Concisely Than Perl!?16c186282
14 Jun 25 `* Re: Writing Python Code More Concisely Than Perl!?15Keith Thompson
15 Jun 25  `* Re: Writing Python Code More Concisely Than Perl!?14c186282
15 Jun 25   `* Re: Writing Python Code More Concisely Than Perl!?13Keith Thompson
15 Jun 25    +* Re: Writing Python Code More Concisely Than Perl!?7Brian Morrison
15 Jun 25    i+* Re: Writing Python Code More Concisely Than Perl!?3Keith Thompson
16 Jun 25    ii`* Re: Writing Python Code More Concisely Than Perl!?2Brian Morrison
16 Jun 25    ii `- Re: Writing Python Code More Concisely Than Perl!?1Keith Thompson
15 Jun 25    i`* Re: Writing Python Code More Concisely Than Perl!?3Lawrence D'Oliveiro
16 Jun 25    i `* Re: Writing Python Code More Concisely Than Perl!?2Brian Morrison
17 Jun 25    i  `- Re: Writing Python Code More Concisely Than Perl!?1Lawrence D'Oliveiro
18 Jun 25    `* Re: Writing Python Code More Concisely Than Perl!?5c186282
18 Jun 25     `* Re: Writing Python Code More Concisely Than Perl!?4Keith Thompson
19 Jun 25      `* Re: Writing Python Code More Concisely Than Perl!?3Keith Thompson
20 Jun 25       `* Re: Writing Python Code More Concisely Than Perl!?2c186282
20 Jun 25        `- Re: Writing Python Code More Concisely Than Perl!?1Keith Thompson

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal