Re: Web development with Python 3.1

Discussion in 'Python' started by Dotan Cohen, Oct 27, 2009.

  1. Dotan Cohen

    Dotan Cohen Guest

    Why the push to use a framework, and why the resistance from the OP?

    Does the OP need to work with cookies or other http-specific features?
    In fact, other than cookies, what http-specific features exist? Does
    Python have a built-in framework for making available GET and POST
    variables? Database queries should be no different than in other
    Python apps.


    --
    Dotan Cohen

    http://what-is-what.com
    http://gibberish.co.il
     
    Dotan Cohen, Oct 27, 2009
    #1
    1. Advertising

  2. Dotan Cohen wrote:

    > Why the push to use a framework, and why the resistance from the OP?
    >
    > Does the OP need to work with cookies or other http-specific features?
    > In fact, other than cookies, what http-specific features exist?


    declarative mapping of urls to code, of code to templates, abstracting away
    the details of GET and POST, validating and decoding parameters, especially
    if these become larger repetitive structures like several addresses of a
    user, re-rendering invalid form-data, working with HTML or JSON as output,
    managing transactions, providing a error-reporting-infrastructure

    The list continues.


    > Does
    > Python have a built-in framework for making available GET and POST
    > variables? Database queries should be no different than in other
    > Python apps.


    Yes, in the end of the day, it's all python.

    Diez
     
    Diez B. Roggisch, Oct 27, 2009
    #2
    1. Advertising

  3. Dotan Cohen

    Dotan Cohen Guest

    > declarative mapping of urls to code

    Apache does this, unless I am misunderstanding you.


    > of code to templates


    Those who code in HTML don't need this. In any case it's not hard to
    call a function in a class that writes the HTML before the content,
    then write the content, then call another function that writes the
    HTML after the content. This is how my sites are run, though in PHP
    instead of Python. No prepackaged templates.

    > abstracting away
    > the details of GET and POST


    GET is easy, just parse the HTTP request. I don't know how much of a
    problem POST would be.


    > validating and decoding parameters, especially
    > if these become larger repetitive structures like several addresses of a
    > user


    This falls under database, cookies, or HTTP request parsing. Or am I
    misunderstanding something again?


    > re-rendering invalid form-data


    Just add it into the HTML.


    > working with HTML or JSON as output,


    Same as writing to stdout, just output the HTTP headers first.


    > managing transactions, providing a error-reporting-infrastructure
    >


    This does not differ from regular (non-web) Python coding.


    > The list continues.
    >


    I would really like to know what else. So far, I am not convinced that
    a framework offers anything that is not already easily accomplished in
    Python.

    --
    Dotan Cohen

    http://what-is-what.com
    http://gibberish.co.il
     
    Dotan Cohen, Oct 27, 2009
    #3
  4. On Tue, Oct 27, 2009 at 2:36 PM, Dotan Cohen <> wrote:
    >> declarative mapping of urls to code

    >
    > Apache does this, unless I am misunderstanding you.
    >
    >
    >> of code to templates

    >
    > Those who code in HTML don't need this. In any case it's not hard to
    > call a function in a class that writes the HTML before the content,
    > then write the content, then call another function that writes the
    > HTML after the content. This is how my sites are run, though in PHP
    > instead of Python. No prepackaged templates.
    >
    >> abstracting away
    >> the details of GET and POST

    >
    > GET is easy, just parse the HTTP request. I don't know how much of a
    > problem POST would be.
    >
    >
    >> validating and decoding parameters, especially
    >> if these become larger repetitive structures like several addresses of a
    >> user

    >
    > This falls under database, cookies, or HTTP request parsing. Or am I
    > misunderstanding something again?
    >
    >
    >> re-rendering invalid form-data

    >
    > Just add it into the HTML.
    >
    >
    >> working with HTML or JSON as output,

    >
    > Same as writing to stdout, just output the HTTP headers first.
    >
    >
    >> managing transactions, providing a error-reporting-infrastructure
    >>

    >
    > This does not differ from regular (non-web) Python coding.
    >
    >
    >> The list continues.
    >>

    >
    > I would really like to know what else. So far, I am not convinced that
    > a framework offers anything that is not already easily accomplished in
    > Python.


    Using a framework helps to ensure that your code is easy to maintain.
    DRY isn't about saving time now, its about saving time six months
    from now.

    Geremy Condra
     
    geremy condra, Oct 27, 2009
    #4
  5. Dotan Cohen

    Dotan Cohen Guest

    > Using a framework helps to ensure that your code is easy to maintain.

    I see, that is a good point. With someone else (the framework
    maintainers) worrying about maintaining function such as HTTP request
    parsers, a lot of work won't have to be [re]done.


    > DRY isn't about saving time now, its about saving time six months
    > from now.
    >


    I suppose in this case it's DRTW (don't reinvent the wheel) but the
    same principle applies.


    --
    Dotan Cohen

    http://what-is-what.com
    http://gibberish.co.il
     
    Dotan Cohen, Oct 27, 2009
    #5
  6. On Oct 27, 10:26 am, "Diez B. Roggisch" <> wrote:
    ....
    > Yes, in the end of the day, it's all python.


    For me, in the end of the day, it's all java or PHP.
    But I'm working on that. For my purposes the "frameworks"
    don't really help much. That's why I built WHIFF :).

    http://aaron.oirt.rutgers.edu/myapp/docs/W1100_2200.TreeView

    -- Aaron Watters

    ===
    If all you got is lemons, make lemonade.
    -- anon.
     
    Aaron Watters, Oct 27, 2009
    #6
  7. On Tue, Oct 27, 2009 at 3:11 PM, Dotan Cohen <> wrote:
    >> Using a framework helps to ensure that your code is easy to maintain.

    >
    > I see, that is a good point. With someone else (the framework
    > maintainers) worrying about maintaining function such as HTTP request
    > parsers, a lot of work won't have to be [re]done.
    >
    >
    >> DRY isn't about saving time now, its about saving time six months
    >> from now.
    >>

    >
    > I suppose in this case it's DRTW (don't reinvent the wheel) but the
    > same principle applies.


    Well, yes- but it's also DRY, and while DRTW (like the acronym, btw)
    helps to prevent your code from being unreadable to someone else,
    DRY helps to ensure that when you have to change something you
    don't have to worry about changing it in 37 and a half other places
    at the same time. Especially given how notoriously difficult it is to
    do automated testing for web development, that's essential.

    Geremy Condra
     
    geremy condra, Oct 27, 2009
    #7
  8. Dotan Cohen

    Dotan Cohen Guest

    > Well, yes- but it's also DRY, and while DRTW (like the acronym, btw)
    > helps to prevent your code from being unreadable to someone else,
    > DRY helps to ensure that when you have to change something you
    > don't have to worry about changing it in 37 and a half other places
    > at the same time. Especially given how notoriously difficult it is to
    > do automated testing for web development, that's essential.
    >


    That's what classes are for, no? Reuse code. I fail to see how using a
    framework would reduce the need to use classes, provided that the need
    exists.


    --
    Dotan Cohen

    http://what-is-what.com
    http://gibberish.co.il
     
    Dotan Cohen, Oct 27, 2009
    #8
  9. On Tue, Oct 27, 2009 at 4:52 PM, Dotan Cohen <> wrote:
    >> Well, yes- but it's also DRY, and while DRTW (like the acronym, btw)
    >> helps to prevent your code from being unreadable to someone else,
    >> DRY helps to ensure that when you have to change something you
    >> don't have to worry about changing it in 37 and a half other places
    >> at the same time. Especially given how notoriously difficult it is to
    >> do automated testing for web development, that's essential.
    >>

    >
    > That's what classes are for, no? Reuse code. I fail to see how using a
    > framework would reduce the need to use classes, provided that the need
    > exists.


    ....frameworks use classes. They just don't make you write all of them.

    Geremy Condra
     
    geremy condra, Oct 27, 2009
    #9
  10. Dotan Cohen schrieb:
    >> Well, yes- but it's also DRY, and while DRTW (like the acronym, btw)
    >> helps to prevent your code from being unreadable to someone else,
    >> DRY helps to ensure that when you have to change something you
    >> don't have to worry about changing it in 37 and a half other places
    >> at the same time. Especially given how notoriously difficult it is to
    >> do automated testing for web development, that's essential.
    >>

    >
    > That's what classes are for, no? Reuse code. I fail to see how using a
    > framework would reduce the need to use classes, provided that the need
    > exists.
    >


    A webframework is *written* in python. Your whole line of argumentation
    boils down to "I can write things myself in python, why use
    libraries/frameworks". Yes. You can also delete your standard-lib, and
    code everything in there yourself - with the same argument.

    Using a framework is about proven solutions for common problems, letting
    you focus on working on your actual application code.

    Diez
     
    Diez B. Roggisch, Oct 27, 2009
    #10
  11. Dotan Cohen

    Dotan Cohen Guest

    > A webframework is *written* in python. Your whole line of argumentation
    > boils down to "I can write things myself in python, why use
    > libraries/frameworks". Yes. You can also delete your standard-lib, and code
    > everything in there yourself - with the same argument.
    >
    > Using a framework is about proven solutions for common problems, letting you
    > focus on working on your actual application code.
    >


    While I know that to be true in the general sense, from what I've
    looked at Django and other frameworks it seems that the web frameworks
    push the coder to use templates, not letting him near the HTML.

    For instance, I was looking for a class / framework that provided a
    proven method of decoding cookies (setting them is no problem),
    decoding POST and GET variables, escaping variables for safe entry
    into MySQL, and other things. Django and the other frameworks seem to
    force the user to use templates. I just want the functions, and to
    print the HTML as stdout to the browser making the request. I had to
    settle on PHP to do this, which admittedly is what PHP was invented to
    do. However, for obvious reasons, I would have prefered to code in
    Python. In fact, I still would.


    --
    Dotan Cohen

    http://what-is-what.com
    http://gibberish.co.il
     
    Dotan Cohen, Oct 28, 2009
    #11
  12. Dotan Cohen

    Dotan Cohen Guest

    > While I know that to be true in the general sense, from what I've
    > looked at Django and other frameworks it seems that the web frameworks
    > push the coder to use templates, not letting him near the HTML.
    >
    > For instance, I was looking for a class / framework that provided a
    > proven method of decoding cookies (setting them is no problem),
    > decoding POST and GET variables, escaping variables for safe entry
    > into MySQL, and other things. Django and the other frameworks seem to
    > force the user to use templates. I just want the functions, and to
    > print the HTML as stdout to the  browser making the request. I had to
    > settle on PHP to do this, which admittedly is what PHP was invented to
    > do. However, for obvious reasons, I would have prefered to code in
    > Python. In fact, I still would.
    >


    I should probably expand on this:

    How can I get an array with all the GET variables in Python?
    How can I get an array with all the POST variables in Python?
    How can I get an array with all the COOKIE variables in Python?
    How can I get the request URI path (everything after
    http://[www.?]example.com/)?

    That's all I want: no templates and nothing between me and the HTML.
    The HTTP headers I can output to stdout myself as well.

    --
    Dotan Cohen

    http://what-is-what.com
    http://gibberish.co.il
     
    Dotan Cohen, Oct 28, 2009
    #12
  13. Dotan Cohen schrieb:
    >> While I know that to be true in the general sense, from what I've
    >> looked at Django and other frameworks it seems that the web frameworks
    >> push the coder to use templates, not letting him near the HTML.
    >>
    >> For instance, I was looking for a class / framework that provided a
    >> proven method of decoding cookies (setting them is no problem),
    >> decoding POST and GET variables, escaping variables for safe entry
    >> into MySQL, and other things. Django and the other frameworks seem to
    >> force the user to use templates. I just want the functions, and to
    >> print the HTML as stdout to the browser making the request. I had to
    >> settle on PHP to do this, which admittedly is what PHP was invented to
    >> do. However, for obvious reasons, I would have prefered to code in
    >> Python. In fact, I still would.
    >>

    >
    > I should probably expand on this:
    >
    > How can I get an array with all the GET variables in Python?
    > How can I get an array with all the POST variables in Python?
    > How can I get an array with all the COOKIE variables in Python?
    > How can I get the request URI path (everything after
    > http://[www.?]example.com/)?
    >
    > That's all I want: no templates and nothing between me and the HTML.
    > The HTTP headers I can output to stdout myself as well.


    Again: if you insist on doing everything yourself - then of course any
    library or framework isn't for you.


    But then do you deal with headers correctly? Do you respect character
    encodings? Form-encodings? Is your generated HTML valid? Are
    timestamp-formats generated according to RFCs for your cookies? Do you
    parse content negotiation headers?

    I think you underestimate the task it is to make a webapplication good.
    And even if not, what you will do is ... code your own webframework.
    Because there is a lot of boilerplate otherwis. If that's a
    learning-experience your after, fine.

    Besides, yes, you can get all these things nonetheless. You just don't
    need them most of the time.

    And at least pylons/TG2 lets you return whatever you want instead, as a
    string. Not via "print" though - which is simply only for CGI, and no
    other means (e.g. mod_wsgi) of python-web-programming.

    Diez
     
    Diez B. Roggisch, Oct 28, 2009
    #13
  14. Dotan Cohen a écrit :
    >> declarative mapping of urls to code

    >
    > Apache does this, unless I am misunderstanding you.


    Using url rewriting ? Yes, fine. Then tell me how you implement
    "reverse" url generation (like Django or Routes do).

    >
    >> of code to templates

    >
    > Those who code in HTML don't need this.


    ???

    > In any case it's not hard to
    > call a function in a class that writes the HTML before the content,
    > then write the content, then call another function that writes the
    > HTML after the content.


    Hmmm, yummy ! And SO maintainable...

    > This is how my sites are run, though in PHP
    > instead of Python. No prepackaged templates.


    PHP *is* a template language.

    (snip)

    > I would really like to know what else. So far, I am not convinced that
    > a framework offers anything that is not already easily accomplished in
    > Python.


    Given that we're talking about Python frameworks, it seems obvious that
    what they do can be accomplished in Python. Now the question is how much
    you like to write the same boring and error-prone boilerplate code
    project after project...
     
    Bruno Desthuilliers, Oct 28, 2009
    #14
  15. Dotan Cohen a écrit :
    >> Well, yes- but it's also DRY, and while DRTW (like the acronym, btw)
    >> helps to prevent your code from being unreadable to someone else,
    >> DRY helps to ensure that when you have to change something you
    >> don't have to worry about changing it in 37 and a half other places
    >> at the same time. Especially given how notoriously difficult it is to
    >> do automated testing for web development, that's essential.
    >>

    >
    > That's what classes are for, no? Reuse code.


    Nope. "classes" are an artifact of one possible approach of OOP, period.

    > I fail to see how using a
    > framework would reduce the need to use classes,


    Why should it ?
     
    Bruno Desthuilliers, Oct 28, 2009
    #15
  16. Dotan Cohen a écrit :
    >> A webframework is *written* in python. Your whole line of argumentation
    >> boils down to "I can write things myself in python, why use
    >> libraries/frameworks". Yes. You can also delete your standard-lib, and code
    >> everything in there yourself - with the same argument.
    >>
    >> Using a framework is about proven solutions for common problems, letting you
    >> focus on working on your actual application code.
    >>

    >
    > While I know that to be true in the general sense, from what I've
    > looked at Django and other frameworks it seems that the web frameworks
    > push the coder to use templates, not letting him near the HTML.


    Well... there must be a reason, for sure... No ?

    > Django and the other frameworks seem to
    > force the user to use templates.


    Not at all. Nothing prevents you from shooting yourself in the foot and
    generating HTML "by hand" in your view functions (or request handler
    methods etc, depending on the framework). This is just plain stupid wrt/
    readability, maintainability, reuse and whatnot, but hey, if you want
    to waste your time, please do. As far as i'm concerned, I'm very happy
    to let the HTML coder write the HTML part and the application programmer
    write the applicative part - even when I end up wearing both caps.

    > I just want the functions, and to
    > print the HTML as stdout to the browser making the request. I had to
    > settle on PHP to do this,


    Why so ? Almost any programming language can do CGI.
     
    Bruno Desthuilliers, Oct 28, 2009
    #16
  17. Hello, Dotan

    On Wed, 28 Oct 2009 10:26:22 +0200
    Dotan Cohen <> wrote:
    > I should probably expand on this:
    >
    > How can I get an array with all the GET variables in Python?
    > How can I get an array with all the POST variables in Python?
    > How can I get an array with all the COOKIE variables in Python?
    > How can I get the request URI path (everything after
    > http://[www.?]example.com/)?
    >
    > That's all I want: no templates and nothing between me and the HTML.
    > The HTTP headers I can output to stdout myself as well.


    $python
    >>help(cgi)
    >>help(cgitb)
    >>help(Cookie)
    >>help(urlparse)
    >>help(os.environ)


    GET variables:
    -------------------------------------------------------------------------
    import os,cgi
    if os.environ.has_key('QUERY_STRING'):
    getv = cgi.parse_qs(os.environ['QUERY_STRING'])
    print getv.keys()

    Other examples is very simple too...

    --
    Mikhail M.Smagin <>
     
    Mikhail M.Smagin, Oct 28, 2009
    #17
  18. Dotan Cohen

    Dotan Cohen Guest

    >> I should probably expand on this:
    >>
    >> How can I get an array with all the GET variables in Python?
    >> How can I get an array with all the POST variables in Python?
    >> How can I get an array with all the COOKIE variables in Python?
    >> How can I get the request URI path (everything after
    >> http://[www.?]example.com/)?
    >>
    >> That's all I want: no templates and nothing between me and the HTML.
    >> The HTTP headers I can output to stdout myself as well.

    >
    > Again: if you insist on doing everything yourself - then of course any
    > library or framework isn't for you.
    >


    I insist on handling the HTML myself. As for converting the request
    variables into Python variables, if a class/framework makes that
    easier then I would gladly use it. My question was serious. How can I
    do those things?


    > But then do you deal with headers correctly?


    Yes, so far as I know. This is actually simpler than the HTML, just be
    careful not to output two newline characters in sequence (thereby
    ending the header).

    > Do you respect character
    > encodings?


    Yes! UTF-8 from database to scripting language to HTTP request.


    > Form-encodings?


    Yes, UTF-8 the in the other direction. However, as form data can be
    spoofed, I would like a function that checks it. Does Python have such
    a function? What class does?


    > Is your generated HTML valid?


    Naturally, even though this is not a Python issue.


    > Are
    > timestamp-formats generated according to RFCs for your cookies?


    Yes, this is not a problem. Is there some gothcha that I am unaware of?


    > Do you parse
    > content negotiation headers?
    >


    No. I hate sites that do that. If the page is available in another
    language, their is a link in the corner.


    > I think you underestimate the task it is to make a webapplication good.


    Probably, but that would not depend on the scripting language. I make
    bad webapplications in PHP too!


    > And
    > even if not, what you will do is ... code your own webframework.


    That is why I am looking for a class that handles the backend stuff,
    but lets _me_ handle the HTML.


    > Because
    > there is a lot of boilerplate otherwis. If that's a learning-experience your
    > after, fine.
    >
    > Besides, yes, you can get all these things nonetheless. You just don't need
    > them most of the time.
    >
    > And at least pylons/TG2 lets you return whatever you want instead, as a
    > string. Not via "print" though - which is simply only for CGI, and no other
    > means (e.g. mod_wsgi) of python-web-programming.
    >


    I am trying to follow you here. What is "return whatever you want"?
    Return HTML to stdout to be sent to the browser?


    --
    Dotan Cohen

    http://what-is-what.com
    http://gibberish.co.il
     
    Dotan Cohen, Oct 28, 2009
    #18
  19. Dotan Cohen

    Dotan Cohen Guest

    2009/10/28 Bruno Desthuilliers <>:
    > Dotan Cohen a écrit :
    >>>
    >>> declarative mapping of urls to code

    >>
    >> Apache does this, unless I am misunderstanding you.

    >
    > Using url rewriting ? Yes, fine. Then tell me how you implement "reverse"
    > url generation (like Django or Routes do).


    I have no idea what reverse url generation is. I assume that the user
    will call http://example.com/path/to/script.py?var1=hello&var2=world

    script.py would live in /home/user/site-name/public_html/path/to/



    >>> of code to templates

    >>
    >> Those who code in HTML don't need this.

    >
    > ???
    >


    I would prefer to output everything from <html> to </html> with print
    statements. I don't want some framework wrapping my output in tags, I
    want the tags to be part of the output.


    >> In any case it's not hard to
    >> call a function in a class that writes the HTML before the content,
    >> then write the content, then call another function that writes the
    >> HTML after the content.

    >
    > Hmmm, yummy !  And SO maintainable...
    >


    Yes, why not?


    >> This is how my sites are run, though in PHP
    >> instead of Python. No prepackaged templates.

    >
    > PHP *is* a template language.
    >


    This is, in general what I'm doing:

    <?php

    // process GET POST and cookie variables, deal with mysql if needed

    $title="blah";
    include_once"/path/to/headerFile";

    print "$content";

    include_once"/path/to/footerFile";

    exit();
    ?>


    >> I would really like to know what else. So far, I am not convinced that
    >> a framework offers anything that is not already easily accomplished in
    >> Python.

    >
    > Given that we're talking about Python frameworks, it seems obvious that what
    > they do can be accomplished in Python. Now the question is how much you like
    > to write the same boring and error-prone boilerplate code project after
    > project...
    >


    Like I said before, I don't want to have to maintain the functions
    that turn the HTTP environment into Python variables, or the code that
    manages database connections. Functions that escape data to be sent to
    MySQL (to prevent sql injection) would be nice. Other than that, it's
    all on me.

    --
    Dotan Cohen

    http://what-is-what.com
    http://gibberish.co.il
     
    Dotan Cohen, Oct 28, 2009
    #19
  20. Dotan Cohen

    Dotan Cohen Guest

    >> While I know that to be true in the general sense, from what I've
    >> looked at Django and other frameworks it seems that the web frameworks
    >> push the coder to use templates, not letting him near the HTML.

    >
    > Well... there must be a reason, for sure... No ?
    >


    Ö¹Yes, but I don't like it.


    >> Django and the other frameworks seem to
    >> force the user to use templates.

    >
    > Not at all. Nothing prevents you from shooting yourself in the foot and
    > generating HTML "by hand" in your view functions (or request handler methods
    > etc, depending on the framework).


    How is this done in Django, then?


    > This is just plain stupid wrt/
    >  readability, maintainability, reuse and whatnot, but hey, if you want to
    > waste your time, please do. As far as i'm concerned, I'm very happy to let
    > the HTML coder write the HTML part and the application programmer write the
    > applicative part - even when I end up wearing both caps.
    >


    I've done stupider things.


    >> I just want the functions, and to
    >> print the HTML as stdout to the  browser making the request. I had to
    >> settle on PHP to do this,

    >
    > Why so ? Almost any programming language can do CGI.


    And I settled on PHP because it does what I need. However, I would
    prefer to move it to Python for the benefit of using the same language
    in the different places that I code for (such as personal applications
    for the desktop, pyqt).


    --
    Dotan Cohen

    http://what-is-what.com
    http://gibberish.co.il
     
    Dotan Cohen, Oct 28, 2009
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Spencer
    Replies:
    2
    Views:
    487
    John Saunders
    Aug 6, 2003
  2. Akhlaq Khan
    Replies:
    4
    Views:
    858
    =?Utf-8?B?Q293Ym95IChHcmVnb3J5IEEuIEJlYW1lcikgLSBN
    Sep 27, 2004
  3. Replies:
    0
    Views:
    509
  4. Replies:
    2
    Views:
    835
    dar7yl
    Jan 15, 2005
  5. Replies:
    37
    Views:
    964
    Thomas Bartkus
    Jul 11, 2005
Loading...

Share This Page