Perl to Python

Carel Fellinger cfelling at iae.nl
Mon Nov 26 22:54:40 EST 2001


Paul Malinowski <paul.malinowski at wulfsberg.com> wrote:
> Howdy,

> #!/usr/bin/perl

  #!/usr/bin/python

this first line is so simple that even I can cope with it, other lines
I've to second guess Perl's semantics.
  
  from os import environ as env

use env as shorthand for os.environ, done here solely to resemble Perl.

> #Setting some environment variables.
> $ENV{UDBCINI}="~openlinksw/bin/udbc.ini";
> $ENV{ODBCINI}="~openlinksw/bin/odbc.ini";
> $ENV{ODBCINSTINI}="~openlinksw/bin/odbcinst.ini";

  env["UDBCINI"] = "~openlinksw/bin/udbc.ini"
  env["ODBCINI"] = "~openlinksw/bin/odbc.ini"
  env["ODBCINSTINI"] = "~openlinksw/bin/odbcinst.ini"

see, the environment is just a dict inside python, and statements
don't need no trailing semi-collin


> $ENV{LD_LIBRARY_PATH}="$ENV{LD_LIBRARY_PATH}:~openlinksw/lib";
> $ENV{SHLIB_PATH}="$ENV{SHLIB_PATH}:~openlinksw/lib";
> $ENV{PATH}="$ENV{PATH}:~openlinksw/bin:~openlinksw/samples/ODBC";

  env["LD_LIBRARY_PATH"] =  env["LD_LIBRARY_PATH"] + "~openlinksw/lib"
  env["SHLIB_PATH"] = env["SHLIB_PATH"] + ":~openlinksw/lib";

here we have to retrieve some info from the environment in the right
hand side and concatenate it to get the new value.

  env["PATH"] = "%s:~openlinksw/bin:~openlinksw/samples/ODBC" % env["PATH"]

but we could as well use %-string substitution.  We could even use some
fancy environment wrapper class that would allow us to substitute special
values in case the environment variable was undefined, somthing Perlers
seem very fond of.

> #SQL query that runs qryModelSNforWeb in the warranty Access database.

never used sql myself, so here I start talking double dutch:)

> $sql = "echo \"select *  from <Query in MS Access Database>;\" |
> odbctest DSN=<OpenLink Definition to MS Access DataBase>";

  sql = 'echo "select *  from %s;" | odbctest DSN=%s' % (query, link)

[[I take it this should be one long string, no return embedded.]]
Python has two kind of string quotes, simply use the one most
convenient.  Here ' allows us to use " unbackslased inside the string.
I took the freedom to suggest using the %-string substitution operator
here to fill in the actual query and database link.

> if (!open(SQL,"$sql |")) {
>     print "Unable to open connect to database<br>";
>     exit 0;
> }
> 
> if (open(SQL, "$sql |")) {
>     chomp(@rawdata = <SQL>);
> }

I'm confused, twice the same query?
Anyway, python is more verbose here, we'll have to be more explicit here.

  import commands, sys

  status, result = commands.getstatusoutput(sql)
  if status:
      print "Unable to open connect to database<br>"
      sys.exit(status)
  else:
      output = output.split('\n')

[[guessing that the chomp works on an array of lines.]]
getstatusoutput already strips any trailing linefeed, so we have to deal
with embedded newlines only. Simply splitting on newline suffices.

I think it's better to pass the status info on with sys.exit instead
of using the misleading zero wich implies all's swell were it isn't.
-- 
groetjes, carel



More information about the Python-list mailing list