473,402 Members | 2,046 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

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|Independent Sys Consultant | http://www.computer-surgery.co.uk/
So what are the eigenvalues and eigenvectors of 'The Matrix'? --anon
May 12 '07 #1
3 1011
In <sl******************@julia.computer-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(object):
def iter_entries(self):
return iter(sorted(self._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
May 12 '07 #2
En Sat, 12 May 2007 14:09:06 -0300, Roger Gammans
<ro***@computer-surgery.co.ukescribió:
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
http://wiki.python.org/moin/PythonStyle

--
Gabriel Genellina

May 13 '07 #3
On May 13, 3:09 am, Roger Gammans <r...@computer-surgery.co.ukwrote:
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

May 14 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Brian Kelley | last post by:
def res(): try: a = 1 return finally: print "do I get here?" res() outputs "do I get here?"
10
by: Carl Banks | last post by:
Here's a summary of the final decision Guido made concerning how Python will indent code in the future, from python-dev: Guido had been planning to deprecate tabs in favor of spaces for awhile,...
9
by: David Stockwell | last post by:
In referring to my copy of the python bible, it tells me I can't use all three items 'try' except and finally. I can use the t/f or t/e combinations though What combination can i use if i want...
26
by: djw | last post by:
Hi, Folks- I have a question regarding the "proper" use of try: finally:... Consider some code like this: d = Device.open() try: d.someMethodThatCanRaiseError(...) if SomeCondition: raise...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.