Python as replacement for PHP?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Erik Johnson

    Python as replacement for PHP?

    This is somewhat a NEWBIE question...

    My company maintains a small RDBS driven website. We currently generate
    HTML using PHP. I've hacked a bit in Python, and generally think it is a
    rather cool language. I've done Perl and like it, there are a few features
    of PHP I like but overall am not too excited about it. I have found PHP's
    strtotime() function to be quite flexible and handy and we make liberal use
    of it.

    I have not yet really "dug-in" to Python - I have dabbled and hacked a
    bit. I am advocating considering switching to Python for a number of
    reasons:

    1) I think Python is cool.
    2) We can do system administration type scripts (currently
    implemented in Perl), web page generation (PHP), and (potentially)
    client-side applications (including GUI's that make socket and/or external
    HTTP requests) in one language.
    3) Python's interactive interpreter makes it easy to try things out.
    4) PyUnit - we would like to develop a robust set of tests and be
    able to do regression testing. I'm not aware of a JUnit/PyUnit analog in
    PHP. Are you?
    5) Python has better code support for complex native data types
    (e.g., tuples, dictionaries, sequences, etc. and being able to write these
    directly in a hierarchical structure rather than building them up piecewise
    with function calls and assignments as in PHP).
    6) All the other standard evangeslistic points about why Python is
    better than <your favorite language here>, some of which may be valid to us,
    some probably not. To those that have used PHP: what am I potentially losing
    that Python really can't replace?

    So, I'm hoping there are some people out there that actually have some
    expereience with both Python & PHP and can give me some solid, informed
    advice about PHP vs. Python, in general and particularly on the following
    points: (NOT Python evangelism please: I've already heard most of it, I've
    espoused a pretty good dose myself - frankly, I'm willing to buy most of it,
    but I've got a serious business decision to make here).

    1) PHP seems to have a pretty good integration/driver with PostgreSQL.
    If we were to switch to Python, what modules/drivers are available to talk
    to PosgreSQL and is it as functional as PHP's support?

    2) Is there a way to have our cake and eat it too? That is, rather than
    being forced to ride one horse or another, can I ride both horses at once:
    is there a way to integrate PHP and Python such that I could call PHP
    functions from a Python script? I know you can always make system calls, and
    it probably wouldn't be that difficult to call an external PHP script and
    slurp up it's output from STDOUT, but my impression is that this is a pretty
    expensive thing to do (relatively) compared to generating HTML from a single
    script invocation. I'm asking if there exists some sort of single-process
    integration (e.g., something akin to Java's "native" interface where a
    program can schlep data back and forth between a Java environment and
    "native" C)?

    3) Does Python have an analog to strtotime()? (For those not familiar
    with that function, it converts a wide range of date formats as string into
    time_t value. Can handle things like "now", "+24 hours", "-3 days",
    "yesterday 06:00PM MST", "January 23, 2004", "2004-02-26 18:00:00 -07",
    etc.)

    4) I am vaguely aware of Apache's modpython. We are currently running
    apache, but I'm real green when it comes to configuring/running/managing
    Apache. I don't really understand what all modpython does for me. Generally
    allows Apache to foist off an HTTP request on a Python script to handle I
    assume, but maybe some kind soul can better inform me about what all
    modpython really does for me?

    5) A PHP script can freely jump in and out of static HTML and script
    code with <?php ?> tags. This is sometimes handy. Can you do the same thing
    with Python?

    6) Debugging: I'm aware there is a debugger for Python - I haven't
    really used it. Maybe there is a better way to do this using PHP, but right
    now when a PHP script isn't working right, I'm reduced to print statements
    and just re-running the script. Do you know a better way to do this in PHP?
    If I were doing things in Python, is there an easy way to generate an HTTP
    request from my browser, but stop the generating script and analyze it in a
    Python debugger?

    7) This goes back to #2, but we are looking at purchasing a PHP graphing
    package: http://www.aditus.nu/jpgraph/ If we could easily call PHP from
    within Python, then I guess that is a no brainer: we can use the PHP
    package. If not, then we are left answering the quesiton, can we call it as
    a forked process, and if so, is that too expensive? Do you know of
    comparable packages for Python?


    I think that's it for now. Thank you for taking the time to read my post
    and many thanks in advance to anyone who would be so kind as to reply.
    (direct response to ej at wellkeeper dot com greatly appreciated).

    -ej


  • Bart Nessux

    #2
    Re: Python as replacement for PHP?

    Erik Johnson wrote:[color=blue]
    > This is somewhat a NEWBIE question...
    >
    > My company maintains a small RDBS driven website. We currently generate
    > HTML using PHP. I've hacked a bit in Python, and generally think it is a
    > rather cool language. I've done Perl and like it, there are a few features
    > of PHP I like but overall am not too excited about it. I have found PHP's
    > strtotime() function to be quite flexible and handy and we make liberal use
    > of it.
    > 6) All the other standard evangeslistic points about why Python is
    > better than <your favorite language here>, some of which may be valid to us,
    > some probably not. To those that have used PHP: what am I potentially losing
    > that Python really can't replace?
    >
    > So, I'm hoping there are some people out there that actually have some
    > expereience with both Python & PHP and can give me some solid, informed
    > advice about PHP vs. Python, in general and particularly on the following
    > points: (NOT Python evangelism please: I've already heard most of it, I've
    > espoused a pretty good dose myself - frankly, I'm willing to buy most of it,
    > but I've got a serious business decision to make here).[/color]

    When it comes to dynamic, DB driven sites, PHP is the only way to go.
    Python is not even close to being suited for this task. PHP claims to be
    a general-purpose language, but I do not know anyone who uses it for
    anything other than dynamic Web programming.

    A serious business decision? PHP would be the only winner here. You
    should not even be considering another language for this, it borders on
    stupidity. We use Python & C for backend processing, systems
    administration and other general programming tasks, but the front-end
    (the Websites) are pure PHP.


    Comment

    • Phil Roberts

      #3
      Re: Python as replacement for PHP?

      With total disregard for any kind of safety measures "Erik
      Johnson" <ej.at.wellkeep er@dot.com> leapt forth and uttered:
      [color=blue]
      > 4) PyUnit - we would like to develop a robust set of tests and
      > be able to do regression testing. I'm not aware of a
      > JUnit/PyUnit analog in PHP. Are you?
      >[/color]

      SimpleTest: http://www.lastcraft.com/simple_test.php
      PHPUnit (1):http://www.students.cs.uu.nl/people/voostind/index.php?
      page=software
      PHPUnit (2):http://pear.php.net/package/PHPUnit
      PHPUnit (3):http://phpunit.sourceforge.net/

      Having three test suites all called PHPUnit is a tad confusing...


      --
      Phil Roberts | Dork Pretending To Be Hard | http://www.flatnet.net/

      Comment

      • CountScubula

        #4
        Re: Python as replacement for PHP?

        "Erik Johnson" <ej.at.wellkeep er@dot.com> wrote in message
        news:403fa54b@n ews.zianet.com. ..[color=blue]
        > This is somewhat a NEWBIE question...
        > (snip)
        > I have not yet really "dug-in" to Python - I have dabbled and hacked a
        > bit. I am advocating considering switching to Python for a number of
        > reasons:
        >
        > 1) I think Python is cool.
        > 2) We can do system administration type scripts (currently
        > implemented in Perl), web page generation (PHP), and (potentially)
        > client-side applications (including GUI's that make socket and/or external
        > HTTP requests) in one language.
        > 3) Python's interactive interpreter makes it easy to try things[/color]
        out.[color=blue]
        > 4) PyUnit - we would like to develop a robust set of tests and be
        > able to do regression testing. I'm not aware of a JUnit/PyUnit analog in
        > PHP. Are you?
        > 5) Python has better code support for complex native data types
        > (e.g., tuples, dictionaries, sequences, etc. and being able to write these
        > directly in a hierarchical structure rather than building them up[/color]
        piecewise[color=blue]
        > with function calls and assignments as in PHP).
        > 6) All the other standard evangeslistic points about why Python is
        > better than <your favorite language here>, some of which may be valid to[/color]
        us,[color=blue]
        > some probably not. To those that have used PHP: what am I potentially[/color]
        losing[color=blue]
        > that Python really can't replace?
        >
        > (snip)
        >
        > -ej
        >
        >[/color]


        You should use what is best for you, but trying to settle on one laguage for
        everthing is not a good idea, you will end uplocking yourself into more code
        than you need sometimes.

        Python is gread for server stuff, and GUI stuff, but for dynamic
        intergration of html/script, PHP wins hands down.

        Now I also use PHP for everything else (except GUI stuff) on servers, almost
        all of my shell scripts are in php, along with autoresponders, mail filters,
        data backup servers, etc..

        But again, use what is quick/fast/simple to implment for you. I use to use
        perl for everthing under the sun, then slowly moved to php, not becouse it
        was cool, but rather easier to set up, and with added functions designed for
        dynamic site ingration.

        You also need to look at what each language was desgined for, sure I can
        write a dynamic web page in bash or korn, but why? Perl was written to do
        large extraction and reporting of text files, Python was written as a server
        language, PHP was design specificly for dynamic/integration of web pages.

        Ok, I'm done now.

        --
        Mike Bradley
        http://www.gzentools.com -- free online php tools


        Comment

        • Skip Montanaro

          #5
          Re: Python as replacement for PHP?


          Erik> 1) PHP seems to have a pretty good integration/driver with
          Erik> PostgreSQL. If we were to switch to Python, what
          Erik> modules/drivers are available to talk to PosgreSQL and is it as
          Erik> functional as PHP's support?

          I use psycopg and like it. There are a couple others as well.

          Erik> 3) Does Python have an analog to strtotime()? (For those not
          Erik> familiar with that function, it converts a wide range of date
          Erik> formats as string into time_t value. Can handle things like
          Erik> "now", "+24 hours", "-3 days", "yesterday 06:00PM MST",
          Erik> "January 23, 2004", "2004-02-26 18:00:00 -07", etc.)

          Marc-Andre Lemburg's mxDateTime might be the closest you'll get to such wide
          ranging formats.

          Erik> 4) I am vaguely aware of Apache's modpython. We are currently
          Erik> running apache, but I'm real green when it comes to
          Erik> configuring/running/managing Apache. I don't really understand
          Erik> what all modpython does for me. Generally allows Apache to
          Erik> foist off an HTTP request on a Python script to handle I
          Erik> assume, but maybe some kind soul can better inform me about
          Erik> what all modpython really does for me?

          It essentially avoids process creation and Python startup overhead. Both
          can be significant performance barriers on heavily loaded systems.

          Erik> 5) A PHP script can freely jump in and out of static HTML and
          Erik> script code with <?php ?> tags. This is sometimes handy. Can
          Erik> you do the same thing with Python?

          There are lots of different Python-based templating systems. I like
          Quixote. It's roughly the inverse of the way most systems do the
          HTML/<language> mind-meld. Instead of Python-in-HTML it's HTML-in-Python.
          There are other systems with which I am not aware. Cheetah and PSP come to
          mind.

          Erik> 6) Debugging: I'm aware there is a debugger for Python - I haven't
          Erik> really used it. Maybe there is a better way to do this using
          Erik> PHP, but right now when a PHP script isn't working right, I'm
          Erik> reduced to print statements and just re-running the script. Do
          Erik> you know a better way to do this in PHP? If I were doing
          Erik> things in Python, is there an easy way to generate an HTTP
          Erik> request from my browser, but stop the generating script and
          Erik> analyze it in a Python debugger?

          Inserting print statements is a pretty common debugging technique in Python
          as well. Also, take a look at the cgitb module.

          Skip

          Comment

          • Irmen de Jong

            #6
            Re: Python as replacement for PHP?

            Bart Nessux wrote:
            [color=blue]
            > When it comes to dynamic, DB driven sites, PHP is the only way to go.
            > Python is not even close to being suited for this task.[/color]

            WTF??

            --Irmen

            Comment

            • A.M. Kuchling

              #7
              Re: Python as replacement for PHP?

              On Fri, 27 Feb 2004 13:50:09 -0700,
              Erik Johnson <ej.at.wellkeep er@dot.com> wrote:[color=blue]
              > 1) PHP seems to have a pretty good integration/driver with PostgreSQL.
              > If we were to switch to Python, what modules/drivers are available to talk
              > to PosgreSQL and is it as functional as PHP's support?[/color]

              There are several; for some reason I don't really understand, there are
              multiple Python/PostgreSQL modules. Most of them will support the standard
              DB-API described in PEP 249. Personally I use PygreSQL with Quixote as the
              web framework.

              Note that I'm one of the developers on a Python web framework called Quixote
              (www.quixote.ca), so my answers are slanted in that direction. If you're in
              the Washington DC area, I'll be giving a Quixote tutorial next weekend at a
              Linux user group meeting; see novalug.tux.org for meeting details.
              [color=blue]
              > 3) Does Python have an analog to strtotime()? (For those not familiar
              > with that function, it converts a wide range of date formats as string into
              > time_t value. Can handle things like "now", "+24 hours", "-3 days",
              > "yesterday 06:00PM MST", "January 23, 2004", "2004-02-26 18:00:00 -07",
              > etc.)[/color]

              mxDateTime has an extensive parsing module. There's a PEP for adding date
              parsing to core Python, but it may not get done for 2.4.
              [color=blue]
              > 4) I am vaguely aware of Apache's modpython. We are currently running
              > apache, but I'm real green when it comes to configuring/running/managing
              > Apache. I don't really understand what all modpython does for me. Generally[/color]

              It embeds a Python interpreter in the Apache daemon, so you can intersperse
              Python processing in Apache's request processing. There's going to be a
              mod_python tutorial at the upcoming PyCon conference; again, if you're in
              the DC area you might consider going in order to talk to existing Python/web
              developers.

              Note that embedding the interpreter in Apache isn't a requirement; you can
              also run applications using SCGI, or FastCGI instead. You'd have to carry
              out benchmarks to see which one ends up running fastest for your
              application.
              [color=blue]
              > 5) A PHP script can freely jump in and out of static HTML and script
              > code with <?php ?> tags. This is sometimes handy. Can you do the same thing
              > with Python?[/color]

              There are various templating packages available for Python, such as Cheetah
              (vaguely PHPish), ZPT (XML-based), or PTL (part of Quixote). Personally I
              don't find embedding Python code in HTML to be a good idea; it's too
              difficult to refactor, and PTL has various convenient features for
              automatically quoting HTML.
              [color=blue]
              > a forked process, and if so, is that too expensive? Do you know of
              > comparable packages for Python?[/color]

              One of the upsides of using Python is that many more non-Web-related
              packages have been written for it. Scientific programming is a significant
              application domain for Python, so several graphing packages have been
              written. You could use Reportlab for PDF generation, Chaco, Gnuplot, or Gist
              for graphing, or the Python Imaging Library for generating PNGs/JPGs.

              --amk

              Comment

              • simo

                #8
                Re: Python as replacement for PHP?

                Here's how I see it (I'm a Web Developer by trade):

                PHP - excellent database integration, if you don't want to have to use
                a Java application server for database work, go with PHP. Also nice
                templating system which even the Dreamweaver morons can work with. It
                is a bit slow at times (XML-RPC/SOAP implementations stink) and has
                limited data types.

                Python - excellent GUI support (PyQt/TKinter/wxPython are sooo easy),
                best suited to rapid application development - we use it for
                prototyping cross-platform C++ apps at work, also for wrapping GUI's
                around sysadmin scripts. Plus it's easy to distribute using distutils.

                Perl - bloody fast, if you're doing lots of text processing/regex
                (e.g. XML parsing) then Perl is it, probably best for sysadmin tasks
                too. We use this for large reports at work. I love its hash handling
                and wide range of modules.

                The moral of the story is don't get locked into one technology, use
                the best tools for the job - especially if you happen to know them
                all! ;o)

                Comment

                • Roger Binns

                  #9
                  Re: Python as replacement for PHP?

                  > So, I'm hoping there are some people out there that actually have some[color=blue]
                  > expereience with both Python & PHP and can give me some solid, informed
                  > advice about PHP vs. Python,[/color]

                  My approach is to use PHP with the Smarty templating system for the
                  web front end. All program logic and "heavy" lifting (and even
                  the light lifting) is done by a backend Python program accessed
                  via XML-RPC. That lets me use each language for what it is best
                  and most productive at.

                  Testing is easier since you can do it via XML-RPC.

                  Roger


                  Comment

                  • Cameron Laird

                    #10
                    XML best practices (was: Python as replacement for PHP?)

                    In article <30260531.04022 71901.1bbdda99@ posting.google. com>,
                    simo <simoninusa2001 @yahoo.co.uk> wrote:

                    Comment

                    • Cameron Laird

                      #11
                      Re: Python as replacement for PHP?

                      In article <403fa54b@news. zianet.com>,
                      Erik Johnson <ej.at.wellkeep er@dot.com> wrote:

                      Comment

                      • Ville Vainio

                        #12
                        Re: Python as replacement for PHP?

                        >>>>> "Simon" == simo <simoninusa2001 @yahoo.co.uk> writes:

                        Simon> Here's how I see it (I'm a Web Developer by trade): PHP -
                        Simon> excellent database integration, if you don't want to have
                        Simon> to use a Java application server for database work, go with
                        Simon> PHP. Also nice templating system which even the Dreamweaver

                        So, how do Python's DB modules fall short of those of PHP? If they
                        indeed do, what should module developers do to fix this gap?

                        I'm so very tired of the argument that because language X specializes
                        in doing Q, it must be better than language Y (which is not
                        specialized for any particular task) for doing Q:

                        "Perl is only good for doing regexps, therefore it must be better than
                        Python for doing regexps"

                        "PHP is only good for doing DB connectivity /Web templating, therefore
                        it must be better than Python for doing them"

                        That just isn't logical.

                        Simon> Perl - bloody fast, if you're doing lots of text
                        Simon> processing/regex (e.g. XML parsing) then Perl is it,
                        Simon> probably best for sysadmin tasks too. We use this for large

                        This is another argument I can't understand. If perl crunches your log
                        file in 3 minutes and Python takes 4 (but the script remains readable
                        and maintainable), Python would still be my choice. And regexps for
                        XML parsing don't always work anyway.

                        Simon> The moral of the story is don't get locked into one
                        Simon> technology, use the best tools for the job - especially if
                        Simon> you happen to know them all! ;o)

                        Often Python is the best tool for all the jobs there is to do. People
                        just fail to see that because, driven by prejudice, they opt for the
                        tool that can *only* do whatever the job is at the moment. That might
                        have been sensible 15 years ago, but the software landscape has
                        changed since.

                        --
                        Ville Vainio http://tinyurl.com/2prnb

                        Comment

                        • Bruno Desthuilliers

                          #13
                          Re: Python as replacement for PHP?

                          Bart Nessux wrote:[color=blue]
                          > Erik Johnson wrote:
                          >[color=green]
                          >> This is somewhat a NEWBIE question...
                          >>
                          >> My company maintains a small RDBS driven website. We currently
                          >> generate
                          >> HTML using PHP. I've hacked a bit in Python, and generally think it is a
                          >> rather cool language. I've done Perl and like it, there are a few
                          >> features
                          >> of PHP I like but overall am not too excited about it.[/color][/color]
                          (snip)
                          [color=blue][color=green]
                          >> To those that have used PHP: what am I potentially
                          >> losing
                          >> that Python really can't replace?
                          >>
                          >> So, I'm hoping there are some people out there that actually have
                          >> some
                          >> expereience with both Python & PHP and can give me some solid, informed
                          >> advice about PHP vs. Python, in general and particularly on the following
                          >> points:[/color][/color]

                          (snip)[color=blue]
                          >
                          > When it comes to dynamic, DB driven sites, PHP is the only way to go.
                          > Python is not even close to being suited for this task.[/color]

                          (snip)
                          [color=blue]
                          > A serious business decision? PHP would be the only winner here. You
                          > should not even be considering another language for this, it borders on
                          > stupidity.[/color]

                          (snip)

                          Bart, could you please back your (strong) assertions with at least one
                          argument ?

                          AFAIK, there is a whole lot of python tools for web developpement, and I
                          don't really see any evidence that one of the two languages is more or
                          less suited to this kind of task.

                          The only points I see in favor of PHP are :
                          - it's a de facto standard, widely used, widely available, and well
                          established
                          - there may be some specific libs not (currently) available in Python.

                          Bruno

                          Comment

                          • Bart Nessux

                            #14
                            Re: Python as replacement for PHP?

                            > Bart, could you please back your (strong) assertions with at least one[color=blue]
                            > argument ?
                            >
                            > AFAIK, there is a whole lot of python tools for web developpement[/color]

                            The OP was comparing Python to PHP, not ZOPE to PHP. PHP by itself is a
                            natural Web programming language, Python is not. Google for dynamic DB
                            driven programming tools... how may PHP returns vs Python returns are
                            there?
                            [color=blue]
                            > The only points I see in favor of PHP are :
                            > - it's a de facto standard, widely used, widely available, and well
                            > established[/color]

                            That's exactly right. That's my point. I meant nothing more.

                            Comment

                            • Keith Bowes

                              #15
                              Re: Python as replacement for PHP?

                              Erik Johnson wrote:
                              [color=blue]
                              >[/color]

                              CAUTION: I'm replying as a non-Python programmer.
                              [color=blue]
                              > 1) I think Python is cool.
                              > 2) We can do system administration type scripts (currently
                              > implemented in Perl), web page generation (PHP), and (potentially)
                              > client-side applications (including GUI's that make socket and/or external
                              > HTTP requests) in one language.[/color]

                              You can do client-side apps in PHP too. Pass the -q option to the CGI
                              version or just use the CLI version.
                              GUI - PHP-GTK
                              Sockets - The socket extension or fsockopen() to treat it as a regular file.
                              [color=blue]
                              > 3) Python's interactive interpreter makes it easy to try things out.[/color]

                              ? Like the -r option?
                              [color=blue]
                              > 4) PyUnit - we would like to develop a robust set of tests and be
                              > able to do regression testing. I'm not aware of a JUnit/PyUnit analog in
                              > PHP. Are you?
                              > 5) Python has better code support for complex native data types
                              > (e.g., tuples, dictionaries, sequences, etc. and being able to write these
                              > directly in a hierarchical structure rather than building them up piecewise
                              > with function calls and assignments as in PHP).[/color]

                              In PHP, all these things are essentially arrays and that works fine.
                              Even the PHP developers say that classes are just syntactic sugar for
                              associative arrays (that'll change in PHP 5, though). So PHP is
                              procedural; there's nothing wrong with that.
                              [color=blue]
                              >[/color]

                              Not to sound like I hate everything which isn't PHP. It's just that PHP
                              does have a wide range of functionality and is moving toward
                              command-line/administration capabilities, and is quite easy to learn if
                              you know other C-like languages. But if you think that Python better
                              serves your purpose, then you should use it. Surely, it would be
                              possible to write a strtotime() implementation in Python; someone may
                              have already done so.

                              Comment

                              Working...