Groups | Search | Server Info | Keyboard shortcuts | Login | Register


Groups > comp.programming > #16789

Re: Writing Python Code More Concisely Than Perl!?

Subject Re: Writing Python Code More Concisely Than Perl!?
Newsgroups comp.lang.misc, comp.programming
References <v2c21f$37ibf$2@dont-email.me>
From c186282 <c186282@nnada.net>
Date 2025-06-13 23:31 -0400
Message-ID <2x2dnWlc86XdcNH1nZ2dnZfqnPidnZ2d@giganews.com> (permalink)

Cross-posted to 2 groups.

Show all headers | View raw


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' :-)

Back to comp.programming | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

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