Re: [Tutor] Using xml.etree

2011-09-19 Thread lists
Hello again.

So, any xml.etree experts out there who might have missed this over the weekend?

Thanks in advance!

Chris
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using xml.etree

2011-09-19 Thread Tim Golden

On 19/09/2011 10:46, lists wrote:

Hello again.

So, any xml.etree experts out there who might have missed this over the weekend?


Not me, I'm afraid, but might I suggest that you ask on the mail
Python list:

  http://mail.python.org/mailman/listinfo/python-list

There's nothing wrong with asking here, but since this question is
more specific to a particular library than to Python-the-language
you're more likely to find people familiar with the package (including
its maintainer in fact...)

TJG
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using xml.etree

2011-09-19 Thread Tim Golden

On 19/09/2011 11:01, Tim Golden wrote:

you're more likely to find people familiar with the package (including
its maintainer in fact...)


Sorry, I misread your post and thought you were referring lxml.etree
(which is a 3rd-party lib). My basic point still stands, though:
you'll get more library-specific help on python-list.

TJG
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using xml.etree

2011-09-19 Thread lists
Thanks Tim,

Will do.

Chris

On Mon, Sep 19, 2011 at 11:05 AM, Tim Golden m...@timgolden.me.uk wrote:
 On 19/09/2011 11:01, Tim Golden wrote:

 you're more likely to find people familiar with the package (including
 its maintainer in fact...)

 Sorry, I misread your post and thought you were referring lxml.etree
 (which is a 3rd-party lib). My basic point still stands, though:
 you'll get more library-specific help on python-list.

 TJG
 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] How it is better than java

2011-09-19 Thread Ashish Gaonker
My obvious thinking is : Java being compiled language , must be faster then
a interpreted   language.

I know couple of points : Development time is less + easy to learn + python
is expressive.

Can you share some more especially as compared to Java / .net (two primarily
used languages in enterprise language  web based applications)

-- 
Thanks  Regards
Ashish Gaonker
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How it is better than java

2011-09-19 Thread Christian Witts

On 2011/09/19 03:27 PM, Ashish Gaonker wrote:
My obvious thinking is : Java being compiled language , must be faster 
then a interpreted   language.


I know couple of points : Development time is less + easy to learn + 
python is expressive.


Can you share some more especially as compared to Java / .net (two 
primarily used languages in enterprise language  web based applications)


--
Thanks  Regards
Ashish Gaonker


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
I would suggest reading `Python is not Java` [1] and `Java is not 
Python, either` [2], old but good reads.  And then ask what is your 
focus for development.  Better is extremely subjective and can be liable 
to induce flame-wars, although this list is quite friendly.


To me, I started using Python as a glue language, controlling process 
flows and the like, with still heavy uses of C and PL/SQL for what I 
do.  Over time Python has taken center-stage for my projects due to 
ease-of-use and rapid application development and only moving time 
critical work that needs to be faster to C but in most cases that is 
not needed for me anymore.


Add to that the great work on PyPy [3] which is extremely efficient, I 
hardly ever have to write in another language if I don't wish.


[1] http://dirtsimple.org/2004/12/python-is-not-java.html
[2] http://dirtsimple.org/2004/12/java-is-not-python-either.html
[3] http://pypy.org/
--

Christian Witts
Python Developer

//
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How it is better than java

2011-09-19 Thread Walter Prins
Hi Ashish,

On 19 September 2011 14:27, Ashish Gaonker ashish@gmail.com wrote:

 My obvious thinking is : Java being compiled language , must be faster then
 a interpreted   language.

 I know couple of points : Development time is less + easy to learn + python
 is expressive.

 Can you share some more especially as compared to Java / .net (two
 primarily used languages in enterprise language  web based applications)


There's many pages on the internet comparing Python, Java and other
languages.  I suggest you check them out.

Having said that, I'll point out that Python has several implementations,
including one called Jython, which actually targets the Java runtime and
so should have comparable performance to Java on the same runtime.
Additionally there's projects like PyPy which in some cases is faster even
than C/C++.

This page (from my bookmarks) has some interesting points of comparison for
consideration:
http://pythonconquerstheuniverse.wordpress.com/category/java-and-python/

Walter
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] string formatting

2011-09-19 Thread Pirritano, Matthew

Pythonistas,

This is the resolution of a question I asked over the weekend.

The method I was thinking of was in a program I wrote on my work
computer but couldn't remember.

Now I'm at work and I see. It is not including the tuple at the end of
the string nor using a dictionary. There is another way using locals().

I was trying to remember this method:

You some variables say:

X = sky
Y = blue

Print the %(x)s is %(y)s % locals()

the sky is blue

That works! And in cases where I'm replacing over 20 strings it's much
easier than having to include a tuple at the end. Especially when
there's only two or three variables I'm replacing repeatedly, in which
case a dictionary seems like overkill.

Thanks
Matt


Matthew Pirritano, Ph.D.
Research Analyst IV
Medical Services Initiative (MSI)
Orange County Health Care Agency
(714) 568-5648
-Original Message-
From: tutor-bounces+mpirritano=ochca@python.org
[mailto:tutor-bounces+mpirritano=ochca@python.org] On Behalf Of
Steven D'Aprano
Sent: Saturday, September 17, 2011 6:58 PM
To: tutor@python.org
Subject: Re: [Tutor] string formatting

Matthew Pirritano wrote:

 But I have very large blocks of text and I thought there was another
way
 like
 
 X = sky
 Y = blue
 the %(X)s is %(Y)s

Unless you use the string formatting operator %, strings containing % 
are just strings. Large or small, the way you do string formatting is 
with the % operator. Python will never do string formatting without an 
explicit command to do so:


text % value  # Single non-tuple argument
text % (value, value, ...)  # Multiple arguments


They don't have to be string literals, they can be variables:

text = Hello, I'd like to have an %s
value = argument
print text % value


You can also use named arguments by using a dictionary:

text = Hello, I'd like to have an %(X)s
values = {X: argument}
print text % values

More details in the Fine Manual:
http://docs.python.org/library/stdtypes.html#string-formatting



Alternatives include the new advanced formatting method:

text.format()

http://docs.python.org/library/string.html#formatstrings


and $ substitutions with the string module:

import string
string.Template

http://docs.python.org/library/string.html#template-strings





-- 
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] string formatting

2011-09-19 Thread Wayne Werner
On Mon, Sep 19, 2011 at 10:46 AM, Pirritano, Matthew
mpirrit...@ochca.comwrote:

 snip

You some variables say:

 X = sky
 Y = blue

 Print the %(x)s is %(y)s % locals()

 the sky is blue

 That works! And in cases where I'm replacing over 20 strings it's much
 easier than having to include a tuple at the end. Especially when
 there's only two or three variables I'm replacing repeatedly, in which
 case a dictionary seems like overkill.


I suspect your email program auto-capitalized the initial X and Y, and P for
you, as that code would actually create a syntax and KeyError (or two).

Technically speaking, locals() is already dictionary:

 type(locals())
class 'dict'

and just for the sake of completeness, in newer (2.6(?) or greater) versions
of Python, you can use the format() method:

x = 'sky'
y = 'blue'

print('The {x} is {y}.'.format(locals()))

HTH,
-Wayne
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] string formatting

2011-09-19 Thread bodsda
Is there any additional overhead of using the locals() or format(locals()) 
instead of a tuple? - the format option is a double function call so I would 
expect that to be considerably slower

Thanks,
Bodsda
-- my phone won't let me bottom post, sorry 
Sent from my BlackBerry® wireless device

-Original Message-
From: Wayne Werner waynejwer...@gmail.com
Sender: tutor-bounces+bodsda=googlemail@python.org
Date: Mon, 19 Sep 2011 12:47:01 
To: Pirritano, Matthewmpirrit...@ochca.com
Cc: tutor@python.org
Subject: Re: [Tutor] string formatting

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] string formatting

2011-09-19 Thread Wayne Werner
On Mon, Sep 19, 2011 at 1:11 PM, bod...@googlemail.com wrote:

 Is there any additional overhead of using the locals() or format(locals())
 instead of a tuple? - the format option is a double function call so I would
 expect that to be considerably slower


Using the following code and timeit, it appears that there is a difference,
but unless you call 0.3-6 ns considerable (assuming I got my math correct:
The difference was ~1.2 or ~1.3 seconds for the classic, ~1.9 for the %
locals version, and timeit runs 1,000,000 times with the default settings),
then the difference isn't terrible.

-Wayne

 from __future__ import print_function
import timeit

def classicwithlocals():
x = 'hello'
from __future__ import print_function
import timeit

def classicwithlocals():
x = 'hello'
y = 'goodbye'
combined = 'You say %(x)s, and I say %(y)s' % locals()

def classicwithtuple():
x = 'hello'
y = 'goodbye'
combined = 'You say %s, and I say %s' % (x, y)

def withargs():
x = 'hello'
y = 'goodbye'
combined = 'You say {0}, and I say {1}'.format(x, y)


for f in [withargs, classicwithtuple, classicwithlocals]:
t = timeit.Timer(f)
print(t.timeit())
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] OptionParser

2011-09-19 Thread Mina Nozar

Hello,

I am trying to use OptionParser (my first time) to set a variable (cvs_output).  i.e. if --csv is given in the list of 
options, then cvs_output = True.

Then I check,
if cvs_output == True:
[...]


I have the following so far but something is missing.
from optparse import OptionParser
usage = usage: %prog [options]
parser = OptionParser(usage=usage)
parser.add_option(-cvs, dest=output, default=True, help=outputs the csv file 
for plotting activites)


python dUCx_ActivityPlots.py

python dUCx_ActivityPlots.2.py -h
Usage: dUCx_ActivityPlots.2.py [options]

Options:
  -h, --help show this help message and exit
  --cvs=OUTPUT_FLAG  outputs the csv file for plotting activities


I don't really understand what dest and action in the arguments to 
parser.add_option mean.
Any help is appreciated.
Mina
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] OptionParser

2011-09-19 Thread Wayne Werner
On Mon, Sep 19, 2011 at 1:22 PM, Mina Nozar noz...@triumf.ca wrote:

 **
 I don't really understand what dest and action in the arguments to
 parser.add_option mean.
 Any help is appreciated.


Have you read the fine manual, specifically the sections here:
http://docs.python.org/library/optparse.html#optparse-standard-option-actions

and here:
http://docs.python.org/library/optparse.html#option-attributes

Additionaly, I'm not sure you copied your code correctly, because after a
copy and paste I get this:

$ python localtest.py
Traceback (most recent call last):
  File localtest.py, line 5, in module
parser.add_option(-cvs, dest=output, default=True, help=outputs the
csv file for plotting activites)
  File /usr/lib/python2.6/optparse.py, line 1012, in add_option
option = self.option_class(*args, **kwargs)
  File /usr/lib/python2.6/optparse.py, line 566, in __init__
self._set_opt_strings(opts)
  File /usr/lib/python2.6/optparse.py, line 606, in _set_opt_strings
self)
optparse.OptionError: invalid long option string '-cvs': must start with --,
followed by non-dash

and when I correct that:

$ python localtest.py -h

uwwerne@WWERNER /c
$

I get no output.

HTH,
Wayne
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using xml.etree

2011-09-19 Thread Sander Sweers

On 17/09/11 13:08, lists wrote:

I have been trying to learn how to parse XML with Python and learn how
to use xml.etree. Lots of the tutorials seem to be very long winded.

I'm trying to access a UK postcode API at www.uk-postcodes.com to take
a UK postcode and return the lat/lng of the postcode. This is what the
XML looks like: http://www.uk-postcodes.com/postcode/HU11AA.xml

The function below returns a dict with the xml tag as a key and the
text as a value. Is this a correct way to use xml.etree?


Define correct, does it give the desired result? Then I would say yes it 
is correct. There may be alternative ways to get to the same result though.



def ukpostcodesapi(postcode):
import urllib


Why do the import here, for speed? You are reading an xml file from the 
internet, guess where most of the time is spend in your function ;-).



import xml.etree.ElementTree as etree

baseURL='http://www.uk-postcodes.com/'
 geocodeRequest='postcode/'+postcode+'.xml'


You could use string formatting here.
  url = 'http://www.uk-postcodes.com/postcode/%s.xml' % postcode

Also what would happen if postcode includes a space?



#grab the xml
tree=etree.parse(urllib.urlopen(baseURL+geocodeRequest))


What happens if you get an error (a 404 error perhaps)? You might want 
to add a try/except block around reading the xml from the internet.



root=tree.getroot()
results={}
for child in root[1]: #here's the geo tag
results.update({child.tag:child.text}) #build a dict containing 
the
geocode data
return results


As you only get 1 set of long/lat tags in the xml you could use find(). 
See below an example.


from xml.etree import ElementTree as ET
import urllib2

url = 'http://www.uk-postcodes.com/postcode/HU11AA.xml'
xml = urllib2.urlopen(url).read()
tree = ET.XML(xml)

geo = {}

for leaf in tree.find('geo'):
geo[leaf.tag] = leaf.text

Greets
Sander
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using xml.etree

2011-09-19 Thread lists
On Mon, Sep 19, 2011 at 9:20 PM, Sander Sweers sander.swe...@gmail.com wrote:
 On 17/09/11 13:08, lists wrote:

 I have been trying to learn how to parse XML with Python and learn how
 to use xml.etree. Lots of the tutorials seem to be very long winded.

 I'm trying to access a UK postcode API at www.uk-postcodes.com to take
 a UK postcode and return the lat/lng of the postcode. This is what the
 XML looks like: http://www.uk-postcodes.com/postcode/HU11AA.xml

 The function below returns a dict with the xml tag as a key and the
 text as a value. Is this a correct way to use xml.etree?

 Define correct, does it give the desired result? Then I would say yes it is
 correct. There may be alternative ways to get to the same result though.

 def ukpostcodesapi(postcode):
        import urllib

 Why do the import here, for speed? You are reading an xml file from the
 internet, guess where most of the time is spend in your function ;-).

        import xml.etree.ElementTree as etree

        baseURL='http://www.uk-postcodes.com/'
         geocodeRequest='postcode/'+postcode+'.xml'

 You could use string formatting here.
  url = 'http://www.uk-postcodes.com/postcode/%s.xml' % postcode

 Also what would happen if postcode includes a space?


        #grab the xml
        tree=etree.parse(urllib.urlopen(baseURL+geocodeRequest))

 What happens if you get an error (a 404 error perhaps)? You might want to
 add a try/except block around reading the xml from the internet.

        root=tree.getroot()
        results={}
        for child in root[1]: #here's the geo tag
                results.update({child.tag:child.text}) #build a dict
 containing the
 geocode data
        return results

 As you only get 1 set of long/lat tags in the xml you could use find(). See
 below an example.

 from xml.etree import ElementTree as ET
 import urllib2

 url = 'http://www.uk-postcodes.com/postcode/HU11AA.xml'
 xml = urllib2.urlopen(url).read()
 tree = ET.XML(xml)

 geo = {}

 for leaf in tree.find('geo'):
    geo[leaf.tag] = leaf.text

 Greets
 Sander

Thank you

I've been working on this and ended up with http://pastebin.com/Y9keC9tB

Chris
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] iretator.send

2011-09-19 Thread Christopher King
Dear tutor dudes,
I know that a for loop uses a an iterators next method in this way

for variable in iterator:
execute_code(variable)

is equivalent to

while True:
try:
variable = iterator.next()
except StopIteration:
break
else:
execute_code(variable)

Is there any syntax that uses the send method of an iterator?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] iretator.send

2011-09-19 Thread Steven D'Aprano

Christopher King wrote:


Is there any syntax that uses the send method of an iterator?


No.


--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How it is better than java

2011-09-19 Thread Steven D'Aprano

Ashish Gaonker wrote:

My obvious thinking is : Java being compiled language , must be faster then
a interpreted   language.



There are three misunderstandings with that statement.

Firstly:

Languages are neither compiled or interpreted. Languages are syntax 
and grammar. Implementations are either compiled, or interpreted, or 
both: for example, there are C interpreters and C compilers. And the 
quality of both can vary significantly.


Asking which language is faster is like asking which is faster, Ford or 
Toyota? That depends on the particular model, and the conditions, and 
the skill of the driver.


It is ironic that you contrast Java as compiled and Python as 
interpreted, because that is *marketing*. When Java first came out, 
Sun didn't want people describing it as interpreted, which it was, so 
they popularized the term byte-code compiler just so that they could 
talk about Java being compiled. Java would compile your Java source code 
to byte-code which was then interpreted by a virtual machine.


That is *exactly* what Python does: it compiles Python source code to 
byte-code which is interpreted by a virtual machine, just like Java. 
What do you think the .pyc files contain, and what the compile() 
function does? And yet, even back in the 1980s, everybody swallowed 
Sun's marketing and called Java a compiled language and Python an 
interpreted language. This is a testament to Sun spending millions in 
advertising.


Byte-code compilation is a lot older than Java. Pascal used something 
similar in the early 1970s, called a p-machine. Even Apple's Hypertalk 
did the same thing, only they called it tokenized code instead of 
compiled. Java's big innovation was to convince people to use the term 
compiler for what was functionally identical to an interpreter.


Of course, Sun (now owned by Oracle) put in a lot of money into Java. 
Millions. Today, Java does have implementations which compile source 
code to machine code. But there are Python implementations that do the 
same, such as Nuitka and Compyler. (I don't know how successful or good 
they are.)


Secondly, what do you mean by faster? Faster to write? Faster to 
compile? Faster to run? Faster for the engine to start up? Even today, 
after Sun has spent tens of millions on Java development, the Java 
Runtime Environment is a big, heavyweight machine that takes a long time 
to start up: cold starts can easily take 30 seconds. That makes Java 
completely unsuitable for small, lightweight tasks: in the time it takes 
for a *single* Java program just to start up, you could possibly run a 
dozen Python programs or a hundred interpreted bash scripts.


But again, that's an *implementation*, not a hard rule about Java. There 
is at least one third-party JRE which claims to have startup times twice 
as fast as the Sun/Oracle JRE.


Either way, once you take startup time into account, sometimes Python 
scripts are not only faster to write and faster to maintain, but faster 
to run as well.


Thirdly, there is no rule of nature that a compiled program to do a job 
must be faster than an interpreted program to do the same thing. This 
depends entirely on the quality of implementation of both: a poor 
compiler may easily generate bad, slow code that takes longer to run 
than a wickedly fast and efficient interpreter. E.g. a compiled version 
of bubblesort will still be slower than an interpreted version of quicksort.


Nothing drives this home more than PyPy, a Just In Time optimizing 
version of Python. PyPy uses a JIT compiler to run code sometimes FASTER 
than the equivalent program in optimized C.


Yes. Faster than C. You read that right.

http://morepypy.blogspot.com/2008/01/rpython-can-be-faster-than-c.html
http://morepypy.blogspot.com/2011/02/pypy-faster-than-c-on-carefully-crafted.html
http://morepypy.blogspot.com/2011/08/pypy-is-faster-than-c-again-string.html


Of course, benchmarks are notoriously flawed, especially micro-
benchmarks. What they *really* answer is not which language is faster? 
(a nonsense question, as I have tried to explain) but which 
implementation is faster with these particular settings on this 
particular task?, a much more practical question.


As exciting as it is to see Python code run faster than C code, it 
shouldn't really surprise anyone that a JIT dynamic compiler with cross 
module optimization beats a static compiler without it. What *is* 
surprising is that a small group of open-source developers have been 
able to build an optimizing JIT compiler for Python of such quality.


Or at least, it is surprising to people who think that quality code can 
only come from big proprietary companies with huge budgets.


What's really exciting though is that now PyPy can be fast enough for 
large programs that traditionally needed to be written in C can now be 
(sometimes!) written in Python:


http://morepypy.blogspot.com/2011/07/realtime-image-processing-in-python.html


Who cares whether Java, or C, is faster 

Re: [Tutor] string formatting

2011-09-19 Thread Steven D'Aprano

Wayne Werner wrote:

On Mon, Sep 19, 2011 at 1:11 PM, bod...@googlemail.com wrote:


Is there any additional overhead of using the locals() or format(locals())
instead of a tuple? - the format option is a double function call so I would
expect that to be considerably slower



Using the following code and timeit, it appears that there is a difference,
but unless you call 0.3-6 ns considerable (assuming I got my math correct:
The difference was ~1.2 or ~1.3 seconds for the classic, ~1.9 for the %
locals version, and timeit runs 1,000,000 times with the default settings),
then the difference isn't terrible.


I get similar speeds with Python 2.7:

 for f in [withargs, classicwithtuple, classicwithlocals]:
... t = timeit.Timer(f)
... print(t.timeit())
...
1.72170519829
1.18233394623
1.96000385284


However, a single reading with timeit is subject to a lot of noise. 
Trying again:



 for f in [withargs, classicwithtuple, classicwithlocals]:
... t = timeit.Timer(f)
... print(min(t.repeat(repeat=5)))  # Best of five runs.
...
1.19279980659
1.01878404617
1.56821203232

So, roughly speaking, it looks like the format method is about 20% 
slower than % with a tuple, and using locals is about 60% slower.




--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor