Should I learn Python instead?

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

    #1

    Should I learn Python instead?

    Hi guys,

    I'm a student/hobbyist programmer interested in creating a web project.
    It's nothing too complicated, I would like to get data from an RSS
    feed and store that into a database. I want my website to get the
    information from the database and display parts of it depending on the
    criteria I set.

    I just finished an OO programming class in Java and I thought it would
    be a good idea to do this in C# since ASP.NET makes web applications
    easier than using Java (that's what I've heard anyway). I thought it
    would be easy to pick up since the language syntax is very similar but
    I'm getting overwhelmed by the massive class library. MSDN docs are
    very good and thorough but the language just seems a little unwieldy
    and too verbose.

    This is how to access an RSS feed and create an XML document to
    manipulate it.

    System.Net.WebR equest myRequest = System.Net.WebR equest.Create("//feed
    url here");
    System.Net.WebR esponse myResponse = myRequest.GetRe sponse();
    System.IO.Strea m rssStream = myResponse.GetR esponseStream() ;
    System.Xml.XmlD ocument rssDoc = new System.Xml.XmlD ocument();

    rssDoc.Load(rss Stream);

    Here's PHP.

    $rss_feed = file_get_conten ts($rss_url);

    I realize that learning the library is part of the process, but as a
    beginner I appreciate simplicity. Is Python easier than C#? Can
    someone show how to access an XML document on the web and have it ready
    to be manipulated for comparison? Any other advice for a newbie?

    Thanks.

  • Luis M. González

    #2
    Re: Should I learn Python instead?

    First of all, let me tell you that you can now write apps for .NET with
    Python.
    There's a python implementation for the .NET framework called
    Ironpython (in beta 5 now).

    Regarding XML, I can't tell you much but in general, python is much
    easier, cleaner, concise and intuitive than all the other alternatives
    you mention.
    Being a dynamic language, you can achieve more with less lines of code.

    Easy tasks are easier with python, and complex tasks can look simple
    with too.

    I also think that Python is an excellent first language to learn.
    Learn it, get used to think as a programmer, learn the basics
    (functions classes, oop.) and then, when you're reasonably comfortable,
    you'll be ready to pick any other language (or you'll be happy enough
    to know that you don't need to learn any other one).

    Comment

    • ej

      #3
      Re: Should I learn Python instead?


      "fyleow" <fyleow@gmail.c om> wrote :

      <snip>
      [color=blue]
      > I realize that learning the library is part of the process, but as a
      > beginner I appreciate simplicity.
      > Is Python easier than C#?[/color]
      Absolutely.
      [color=blue]
      > Can someone show how to access an XML document on the web and have it[/color]
      ready[color=blue]
      > to be manipulated for comparison?[/color]
      Yes. (see below)
      [color=blue]
      > Any other advice for a newbie?[/color]
      Learn Python. It's good to know other languages too, but when it comes to
      getting stuff done fast & cleanly, you will find Python an invaluable tool.





      # here's a simple use of the urllib module to fetch a document from the web
      (output from an interactive python interpreter session):[color=blue]
      > python[/color]
      Python 2.3.4 (#1, Feb 7 2005, 15:50:45)
      [GCC 3.3.4 (pre 3.3.5 20040809)] on linux2
      Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
      >>> import urllib
      >>> url = 'http://www.google.com'
      >>> fd = urllib.urlopen( url)
      >>> html = fd.read()
      >>> print html[/color][/color][/color]
      <html><head><me ta http-equiv="content-type" content="text/html;
      charset=ISO-8859-1"><title>Googl e</title><style>
      ....
      <snip>


      I don't use RSS myself, and don't happen to know a url that gets me to an
      RSS document, but here's an RSS document I found at:
      www.XML.com,Textuality Services,Mark Pilgrim,Programming, Publishing,What Is RSS


      Here's just the document itself:

      <rdf:RDF
      xmlns:rdf="http ://www.w3.org/1999/02/22-rdf-syntax-ns#"
      xmlns="http://purl.org/rss/1.0/"
      xmlns:dc="http://purl.org/dc/elements/1.1/"[color=blue]
      >[/color]
      <channel rdf:about="http ://www.xml.com/cs/xml/query/q/19">
      <title>XML.co m</title>
      <link>http://www.xml.com/</link>
      <description>XM L.com features a rich mix of information and services for
      the XML community.</description>
      <language>en-us</language>
      <items>
      <rdf:Seq>
      <rdf:li
      rdf:resource="h ttp://www.xml.com/pub/a/2002/12/04/normalizing.htm l"/>
      <rdf:li
      rdf:resource="h ttp://www.xml.com/pub/a/2002/12/04/som.html"/>
      <rdf:li
      rdf:resource="h ttp://www.xml.com/pub/a/2002/12/04/svg.html"/>
      </rdf:Seq>
      </items>
      </channel>
      <item rdf:about="http ://www.xml.com/pub/a/2002/12/04/normalizing.htm l">
      <title>Normaliz ing XML, Part 2</title>
      <link>http://www.xml.com/pub/a/2002/12/04/normalizing.htm l</link>
      <description> In this second and final look at applying relational
      normalization techniques to W3C XML Schema data modeling, Will Provost
      discusses when not to normalize, the scope of uniqueness and the fourth and
      fifth normal forms.</description>
      <dc:creator>Wil l Provost</dc:creator>
      <dc:date>2002-12-04</dc:date>
      </item>
      <item rdf:about="http ://www.xml.com/pub/a/2002/12/04/som.html">
      <title>The .NET Schema Object Model</title>
      <link>http://www.xml.com/pub/a/2002/12/04/som.html</link>
      <description>Pr iya Lakshminarayana n describes in detail the use of the
      ..NET Schema Object Model for programmatic manipulation of W3C XML
      Schemas.</description>
      <dc:creator>Pri ya Lakshminarayana n</dc:creator>
      <dc:date>2002-12-04</dc:date>
      </item>
      <item rdf:about="http ://www.xml.com/pub/a/2002/12/04/svg.html">
      <title>SVG's Past and Promising Future</title>
      <link>http://www.xml.com/pub/a/2002/12/04/svg.html</link>
      <description> In this month's SVG column, Antoine Quint looks back at
      SVG's journey through 2002 and looks forward to 2003.</description>
      <dc:creator>Ant oine Quint</dc:creator>
      <dc:date>2002-12-04</dc:date>
      </item>
      </rdf:RDF>



      Here's a separate interpreter session where I have put the document above
      into a string called xml:

      This is the important, functional part:
      [color=blue][color=green][color=darkred]
      >>> from xml.dom import minidom
      >>> doc = minidom.parseSt ring(xml)[/color][/color][/color]

      The rest of what follows is just me poking around at the document structure.
      You can write whatever code you need to get what you want:
      [color=blue][color=green][color=darkred]
      >>> doc.firstChild[/color][/color][/color]
      <DOM Element: rdf:RDF at 0x404a56ac>[color=blue][color=green][color=darkred]
      >>> c1 = doc.firstChild
      >>> c1.getElementsB yTagName('dc:da te')[/color][/color][/color]
      [<DOM Element: dc:date at 0x404acc6c>, <DOM Element: dc:date at 0x404acfec>,
      <DOM Element: dc:date at 0x404db38c>][color=blue][color=green][color=darkred]
      >>> c1.getElementsB yTagName('dc:da te')[0].toxml()[/color][/color][/color]
      u'<dc:date>2002-12-04</dc:date>'[color=blue][color=green][color=darkred]
      >>> date = c1.getElementsB yTagName('dc:da te')[0]
      >>> date.nodeValue
      >>> date.hasChildNo des[/color][/color][/color]
      <bound method Element.hasChil dNodes of <DOM Element: dc:date at 0x404acc6c>>[color=blue][color=green][color=darkred]
      >>> date.hasChildNo des()[/color][/color][/color]
      True[color=blue][color=green][color=darkred]
      >>> date.firstChild[/color][/color][/color]
      <DOM Text node "2002-12-04">[color=blue][color=green][color=darkred]
      >>> dir(date.firstC hild)[/color][/color][/color]
      ['ATTRIBUTE_NODE ', 'CDATA_SECTION_ NODE', 'COMMENT_NODE',
      'DOCUMENT_FRAGM ENT_NODE', 'DOCUMENT_NODE' , 'DOCUMENT_TYPE_ NODE',
      'ELEMENT_NODE', 'ENTITY_NODE', 'ENTITY_REFEREN CE_NODE', 'NOTATION_NODE' ,
      'PROCESSING_INS TRUCTION_NODE', 'TEXT_NODE', 'TREE_POSITION_ ANCESTOR',
      'TREE_POSITION_ DESCENDENT', 'TREE_POSITION_ DISCONNECTED',
      'TREE_POSITION_ EQUIVALENT', 'TREE_POSITION_ FOLLOWING',
      'TREE_POSITION_ PRECEDING', 'TREE_POSITION_ SAME_NODE', '__doc__', '__len__',
      '__module__', '__nonzero__', '__repr__', '__setattr__',
      '_call_user_dat a_handler', '_get_childNode s', '_get_data',
      '_get_firstChil d', '_get_isWhitesp aceInElementCon tent', '_get_lastChild ',
      '_get_length', '_get_localName ', '_get_nodeValue ', '_get_wholeText ',
      '_set_data', '_set_nodeValue ', 'appendChild', 'appendData', 'attributes',
      'childNodes', 'cloneNode', 'data', 'deleteData', 'firstChild',
      'getInterface', 'getUserData', 'hasAttributes' , 'hasChildNodes' ,
      'insertBefore', 'insertData', 'isSameNode', 'isSupported',
      'isWhitespaceIn ElementContent' , 'lastChild', 'length', 'localName',
      'namespaceURI', 'nextSibling', 'nodeName', 'nodeType', 'nodeValue',
      'normalize', 'ownerDocument' , 'parentNode', 'prefix', 'previousSiblin g',
      'removeChild', 'replaceChild', 'replaceData', 'replaceWholeTe xt',
      'setUserData', 'splitText', 'substringData' , 'toprettyxml', 'toxml',
      'unlink', 'wholeText', 'writexml'][color=blue][color=green][color=darkred]
      >>> date.firstChild .wholeText[/color][/color][/color]
      u'2002-12-04'[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      # call dir() and help() on your doc and various other things to see what is
      available.
      # for example
      [color=blue][color=green][color=darkred]
      >>> dir(c1)[/color][/color][/color]
      ['ATTRIBUTE_NODE ', 'CDATA_SECTION_ NODE', 'COMMENT_NODE',
      'DOCUMENT_FRAGM ENT_NODE', 'DOCUMENT_NODE' , 'DOCUMENT_TYPE_ NODE',
      'ELEMENT_NODE', 'ENTITY_NODE', 'ENTITY_REFEREN CE_NODE', 'NOTATION_NODE' ,
      'PROCESSING_INS TRUCTION_NODE', 'TEXT_NODE', 'TREE_POSITION_ ANCESTOR',
      'TREE_POSITION_ DESCENDENT', 'TREE_POSITION_ DISCONNECTED',
      'TREE_POSITION_ EQUIVALENT', 'TREE_POSITION_ FOLLOWING',
      'TREE_POSITION_ PRECEDING', 'TREE_POSITION_ SAME_NODE', '__doc__', '__init__',
      '__module__', '__nonzero__', '__repr__', '_attrs', '_attrsNS',
      '_call_user_dat a_handler', '_child_node_ty pes', '_get_attribute s',
      '_get_childNode s', '_get_firstChil d', '_get_lastChild ', '_get_localName ',
      '_get_tagName', '_magic_id_node s', 'appendChild', 'attributes',
      'childNodes', 'cloneNode', 'firstChild', 'getAttribute', 'getAttributeNS ',
      'getAttributeNo de', 'getAttributeNo deNS', 'getElementsByT agName',
      'getElementsByT agNameNS', 'getInterface', 'getUserData', 'hasAttribute',
      'hasAttributeNS ', 'hasAttributes' , 'hasChildNodes' , 'insertBefore',
      'isSameNode', 'isSupported', 'lastChild', 'localName', 'namespaceURI',
      'nextSibling', 'nodeName', 'nodeType', 'nodeValue', 'normalize',
      'ownerDocument' , 'parentNode', 'prefix', 'previousSiblin g',
      'removeAttribut e', 'removeAttribut eNS', 'removeAttribut eNode',
      'removeAttribut eNodeNS', 'removeChild', 'replaceChild', 'schemaType',
      'setAttribute', 'setAttributeNS ', 'setAttributeNo de', 'setAttributeNo deNS',
      'setIdAttribute ', 'setIdAttribute NS', 'setIdAttribute Node', 'setUserData',
      'tagName', 'toprettyxml', 'toxml', 'unlink', 'writexml']


      # See also: http://docs.python.org/lib/module-xml.dom.minidom.html

      There may well be other, more specialized Python modules just for doing
      RSS - check around and at the Python Cheese Shop: http://www.python.org/pypi

      HTH,
      -ej






      Comment

      • Serge Orlov

        #4
        Re: Should I learn Python instead?

        fyleow wrote:[color=blue]
        > Hi guys,
        >
        > I'm a student/hobbyist programmer interested in creating a web project.
        > It's nothing too complicated, I would like to get data from an RSS
        > feed and store that into a database. I want my website to get the
        > information from the database and display parts of it depending on the
        > criteria I set.
        >
        > I just finished an OO programming class in Java and I thought it would
        > be a good idea to do this in C# since ASP.NET makes web applications
        > easier than using Java (that's what I've heard anyway). I thought it
        > would be easy to pick up since the language syntax is very similar but
        > I'm getting overwhelmed by the massive class library. MSDN docs are
        > very good and thorough but the language just seems a little unwieldy
        > and too verbose.
        >
        > This is how to access an RSS feed and create an XML document to
        > manipulate it.
        >
        > System.Net.WebR equest myRequest = System.Net.WebR equest.Create("//feed
        > url here");
        > System.Net.WebR esponse myResponse = myRequest.GetRe sponse();
        > System.IO.Strea m rssStream = myResponse.GetR esponseStream() ;
        > System.Xml.XmlD ocument rssDoc = new System.Xml.XmlD ocument();
        >
        > rssDoc.Load(rss Stream);
        >
        > Here's PHP.
        >
        > $rss_feed = file_get_conten ts($rss_url);
        >[/color]

        Here is Python (including printing all titles in the feed):

        import urllib
        import elementtree.Ele mentTree as ET

        feed = ET.parse(urllib .urlopen("http://www.python.org/channews.rdf"))
        for item_title in feed.findall("c hannel/item/title"):
        print item_title.text

        elementtree library is available here:

        [color=blue]
        > I realize that learning the library is part of the process, but as a
        > beginner I appreciate simplicity. Is Python easier than C#?[/color]

        Developers of Python try to make it simple and powerful, so in general
        Python is easier to use, but you may hit corner cases.

        [color=blue]
        > Any other advice for a newbie?[/color]

        Sometimes open source documentation is harder to understand than
        documentation for commercial libraries. In such cases I usually walk
        over all functions in the library trying them, getting familiar with
        the library's jargon. So the learning curve is more steep but when
        you're on the top things are much easier.

        Serge

        Comment

        • fyleow

          #5
          Re: Should I learn Python instead?

          Thanks guys. I picked up the Apress book on Python and downloaded the
          Komodo IDE trial to get me started.

          I'm going to try using Django to build my website and since my brother
          already has hosting I'll just borrow some space.
          He does PHP for a living, so it would probably be better for me to use
          that instead.

          Unfortunately, I'm allergic to dollar signs ;)

          Comment

          • Gerard Flanagan

            #6
            Re: Should I learn Python instead?

            fyleow wrote:[color=blue]
            > Hi guys,
            >
            > I'm a student/hobbyist programmer interested in creating a web project.
            > It's nothing too complicated, I would like to get data from an RSS
            > feed and store that into a database. I want my website to get the
            > information from the database and display parts of it depending on the
            > criteria I set.
            >[/color]
            [...][color=blue]
            >
            > This is how to access an RSS feed and create an XML document to
            > manipulate it.
            >
            > System.Net.WebR equest myRequest = System.Net.WebR equest.Create("//feed
            > url here");
            > System.Net.WebR esponse myResponse = myRequest.GetRe sponse();
            > System.IO.Strea m rssStream = myResponse.GetR esponseStream() ;
            > System.Xml.XmlD ocument rssDoc = new System.Xml.XmlD ocument();
            >
            > rssDoc.Load(rss Stream);
            >
            > Here's PHP.
            >
            > $rss_feed = file_get_conten ts($rss_url);
            >[/color]

            I've never used it myself but maybe this would be useful:


            [color=blue]
            >From the Homepage:[/color]
            [color=blue][color=green][color=darkred]
            >>> import feedparser
            >>> d = feedparser.pars e("http://feedparser.org/docs/examples/atom10.xml")
            >>> d.feed.title[/color][/color][/color]
            u'Sample Feed'[color=blue][color=green][color=darkred]
            >>> d.channel.title[/color][/color][/color]
            u'Sample Feed'[color=blue][color=green][color=darkred]
            >>> d.feed.link[/color][/color][/color]
            u'http://example.org/'[color=blue][color=green][color=darkred]
            >>> d.feed.subtitle # parses escaped HTML[/color][/color][/color]
            u'For documentation <em>only</em>'[color=blue][color=green][color=darkred]
            >>> d.channel.descr iption[/color][/color][/color]
            u'For documentation <em>only</em>'

            Gerard

            Comment

            • Steve Bergman

              #7
              Re: Should I learn Python instead?

              There is also TurboGears, which (IMO) is greatness in the making.
              Though unfortunately, the documentation is still catching up to the
              greatness. I'm using it and loving it, though.



              DJango is great for content management. TurboGears is (IMO) a more
              generalized framework. It all depends on what kind of site you are
              wanting to create.

              Comment

              • Lawrence Oluyede

                #8
                Re: Should I learn Python instead?

                "fyleow" <fyleow@gmail.c om> writes:[color=blue]
                > I'm a student/hobbyist programmer interested in creating a web project.[/color]

                I'm a student too and I've done a little Python web related stuff long ago.
                [color=blue]
                > It's nothing too complicated, I would like to get data from an RSS
                > feed and store that into a database. I want my website to get the
                > information from the database and display parts of it depending on the
                > criteria I set.[/color]

                That's a really easy thing to do. You're lucky because thanks to Mark Pilgrim
                we have one of the best RSS/Atom parsing libraries out there:


                It's quite simple:

                1 - you parse the feed
                2 - you take the data
                3 - you display them in your html page with one of the python frameworks
                available.
                [color=blue]
                > I just finished an OO programming class in Java and I thought it would
                > be a good idea to do this in C# since ASP.NET makes web applications
                > easier than using Java (that's what I've heard anyway).[/color]

                It's quite right. ASP.NET is easier than JSP and J2EE stuff but Python is
                better to me :)
                [color=blue]
                > I thought it
                > would be easy to pick up since the language syntax is very similar but
                > I'm getting overwhelmed by the massive class library. MSDN docs are
                > very good and thorough but the language just seems a little unwieldy
                > and too verbose.[/color]

                Yeah they like in that way. A 40000+ class library and gigatons of
                documents. All is verbose in their static world: documents, books, languages :(
                [color=blue]
                > This is how to access an RSS feed and create an XML document to
                > manipulate it.[/color]

                I know the author of the RSS.NET library and I used it in the past, it can save
                you some machinery. But why get a bad language with a good library instead of a
                wonderful library and a very good language :) ?
                [color=blue]
                > Is Python easier than C#?[/color]

                IMHO yes.
                [color=blue]
                > Can
                > someone show how to access an XML document on the web and have it ready
                > to be manipulated for comparison? Any other advice for a newbie?[/color]

                Start with the examples on the feedparser homepage. Then choose a framework
                (Turbogears? CherryPy?) and then it's a matter of _minutes_ to have a HTML page
                filled with your data.

                --
                Lawrence - http://www.oluyede.org/blog
                "Nothing is more dangerous than an idea
                if it's the only one you have" - E. A. Chartier

                Comment

                Working...