Finally started on python..

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

    #1

    Finally started on python..


    Hi,

    Having known about python since around the turn of the century ,
    I finally found a (actually two) reason to learn it.

    Python seems to have moved on a little since the 1.5.2 release
    covered in the reference book (Essential Python) I bought way back when
    so I could learn it when the time came but it seems to be mainly backward
    compatible - is there anything that likely to catch me out -
    I use linux, so heavy use of an upto date pydoc has filled the gaps
    so far.

    I do however have a couple of questions:-

    1) A nice simple language query :
    I found myself using this sort of code a bit in one of my recent
    scripts
    class Something:
    def Entries(self):
    sort=self._data .keys()
    sort.sort()
    for i in sort:
    yield i

    IS this preferable to just returning the sort array from the function
    or not? Which is more runtime efficent.

    Does it change if the last line was yield self._data[i] instead as
    that would require a map() in the function ?

    2)
    I've ended up coding a new wrapper for reading in data structures
    from XML files (it wraps xml.sax) so that ctor are call on each end
    tag with the XML Objects contents.

    Does the python communitity have something like Perl's CPAN and
    is there already something there taht does this or would it
    be something that I could give back? Is there a naming
    convention for such modules in python - I couldn't easly detect
    one looking at the library which whip with python in Debian.

    Sorry, for asking so much in a first post but I thought both
    queries were link with by relative newby status.

    TTFN
    --
    Roger. Home| http://www.sandman.uklinux.net/
    Master of Peng Shui. (Ancient oriental art of Penguin Arranging)
    Work|Independen t Sys Consultant | http://www.computer-surgery.co.uk/
    So what are the eigenvalues and eigenvectors of 'The Matrix'? --anon
  • Marc 'BlackJack' Rintsch

    #2
    Re: Finally started on python..

    In <slrnf4bt4d.64d .roger@julia.co mputer-surgery.co.uk>, Roger Gammans
    wrote:
    I found myself using this sort of code a bit in one of my recent
    scripts
    class Something:
    def Entries(self):
    sort=self._data .keys()
    sort.sort()
    for i in sort:
    yield i
    >
    IS this preferable to just returning the sort array from the function
    or not? Which is more runtime efficent.
    I see no benefit for a generator here as the whole list mus be build for
    sorting anyway. If you still want return an iterable here it could be
    written this way:

    class Something(objec t):
    def iter_entries(se lf):
    return iter(sorted(sel f._data.keys()) )

    Just leave out the `iter()` call to return a sorted list.

    If you want to make `Something` objects iterable over the sorted keys,
    change the method name to `__iter__()`. Then you can use `Something`
    objects like this:

    something = Something()
    # Puts some entries into `something`.
    for entry in something:
    print entry

    Ciao,
    Marc 'BlackJack' Rintsch

    Comment

    • Gabriel Genellina

      #3
      Re: Finally started on python..

      En Sat, 12 May 2007 14:09:06 -0300, Roger Gammans
      <roger@comput er-surgery.co.ukes cribió:
      Having known about python since around the turn of the century ,
      I finally found a (actually two) reason to learn it.
      Welcome!
      Does the python communitity have something like Perl's CPAN and
      is there already something there taht does this or would it
      be something that I could give back? Is there a naming
      convention for such modules in python - I couldn't easly detect
      one looking at the library which whip with python in Debian.
      See the Python wiki pages, you can get the answer to these and many more
      questions:
      http://wiki.python.org/moin/PublishingPythonModules and


      --
      Gabriel Genellina

      Comment

      • John Machin

        #4
        Re: Finally started on python..

        On May 13, 3:09 am, Roger Gammans <r...@compute r-surgery.co.ukwr ote:
        2)
        I've ended up coding a new wrapper for reading in data structures
        from XML files (it wraps xml.sax) so that ctor are call on each end
        tag with the XML Objects contents.
        >
        is there already something there taht does this
        Check out ElementTree at http://www.effbot.org/ ... it may be similar

        Comment

        Working...