Newbie coding question - format error

2014-06-29 Thread Martin S
Hi again,

Still working leisurely through the tutorial here:
http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/io.html

An excise at the end of second 1.10 says to
Write a version of the quotient problem in *Exercise for Quotients*
,
quotientformat.py, that uses the string format method to construct the same
final string as before. Again be sure to give a full sentence stating both
the integer quotient and the remainder.


So I did

x=int(input('Enter an integer '))
y=int(input('Enter another integer '))
z=int(input('Enter a third integer '))
formatStr='Integer {0}, {1}, {2}, and the sum is {3}.'
equations=formatStr.format(x,y,z,x+y+z)
print(equations)
formatStr2='{0} divided by {1} is {2} with a reminder of {3}'
equations2=formatStr2.format(x,y,x//y,x%y)
print(equations2)

And obviously this works.
But the question is: if I want to keep the results of {2} and {3} between
the first instance (formatStr) and the second (formatStr2)  how would I go
about it? Apprently using {4} and {5}  instead results in a index sequence
error as in

IndexError: tuple index out of range

/Martin S
--
Regards,

Martin S
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: print statements and profiling a function slowed performance

2014-06-29 Thread Chris Angelico
On Sun, Jun 29, 2014 at 4:14 PM, Dennis Lee Bieber
 wrote:
> On Sat, 28 Jun 2014 23:50:26 +1000, Chris Angelico 
> declaimed the following:
>
>>No, they were measured; but the exact difference will depend on Python
>>version, console, OS, etc, etc, etc, etc, etc, which is why I said
>>"maybe 100ms" - on one run it was about that (rounded aggressively to
>>make for convenient figures), but if I ran the test again, it might
>>come out at 50ms or 200ms. For what it's worth, I did the test on
>>Python 3.5(ish) on Linux; since you tested it on Python 2, a closer
>>comparison would be to use xrange rather than range, to avoid the RAM
>>overhead.
>>
>
> I chose range() in order to reuse the list (generated outside the
> timing), rather than include the time of the iterator on both loops.
>
> Ran it three times and only had differences in the noise.
>
> The main incentive for my "subjective" comment is that I was getting a
> do-nothing loop that was ~half the time you reported, while the print #
> loop was ~twice your reported times.

My original point (printing to console can be quite slow) is supported
by your figures just as much as it is by mine; but the difference
between them is unsurprising, given the number of differences in the
test (list vs iterable range, Linux vs Windows, Py2 vs Py3). So, not
subjective, but still open to huge variation.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: why i can't install ez_setup

2014-06-29 Thread Peter Tomcsanyi


"Mark Lawrence"  wrote in message 
news:[email protected]...
I don't consider that particularly sound adice.  easy_install is hardly an 
"obscure Python installation tool".  If you get pip it will look after the 
dependancies for you.  The site you're referenced is unofficial.  FWIW 
I've used it and never had a problem, but it's certainly a case of "buyer 
beware".


Well, I suggested to the original poster using the only way of installing 
Python things for Windows which worked for me.
After trying all the ez_installs and pips (which require some C compilers 
installed and PATH variable set properly and ...) and whatever suggested on 
many sites, which never worked for me, I felt a big relief when I found that 
page full of prepared proper Windows installers.
You are right, one must be bewared when downloading anything, but from my 
(and your) experience it seems to be safe.


Peter


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


Writing Multiple files at a times

2014-06-29 Thread subhabangalore
Dear Group,

I am trying to crawl multiple URLs. As they are coming I want to write them as 
string, as they are coming, preferably in a queue. 

If any one of the esteemed members of the group may kindly help.

Regards,
Subhabrata Banerjee. 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Writing Multiple files at a times

2014-06-29 Thread Mark Lawrence

On 29/06/2014 11:49, [email protected] wrote:

Dear Group,

I am trying to crawl multiple URLs. As they are coming I want to write them as 
string, as they are coming, preferably in a queue.

If any one of the esteemed members of the group may kindly help.

Regards,
Subhabrata Banerjee.



https://docs.python.org/3/library/urllib.html#module-urllib
https://pypi.python.org/pypi/requests/2.3.0

https://docs.python.org/3/library/queue.html

--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com


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


Re:Writing Multiple files at a times

2014-06-29 Thread Dave Angel
[email protected] Wrote in message:
> Dear Group,
> 
> I am trying to crawl multiple URLs. As they are coming I want to write them 
> as string, as they are coming, preferably in a queue. 
> 
> If any one of the esteemed members of the group may kindly help.
> 

>From your subject line,  it appears you want to keep multiple files open, and 
>write to each in an arbitrary order.  That's no problem,  up to the operating 
>system limits.  Define a class that holds the URL information and for each 
>instance,  add an attribute for an output file handle. 

Don't forget to close each file when you're done with the corresponding URL. 

-- 
DaveA

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


Re: Writing Multiple files at a times

2014-06-29 Thread Roy Smith
In article ,
 Dave Angel  wrote:

> [email protected] Wrote in message:
> > Dear Group,
> > 
> > I am trying to crawl multiple URLs. As they are coming I want to write them 
> > as string, as they are coming, preferably in a queue. 
> > 
> > If any one of the esteemed members of the group may kindly help.
> > 
> 
> >From your subject line,  it appears you want to keep multiple files open, 
> >and write to each in an arbitrary order.  That's no problem,  up to the 
> >operating system limits.  Define a class that holds the URL information and 
> >for each instance,  add an attribute for an output file handle. 
> 
> Don't forget to close each file when you're done with the corresponding URL.

One other thing to mention is that if you're doing anything with 
fetching URLs from Python, you almost certainly want to be using Kenneth 
Reitz's excellent requests module (http://docs.python-requests.org/).  
The built-in urllib support in Python works, but requests is so much 
simpler to use.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Newbie coding question - format error

2014-06-29 Thread Sibylle Koczian

Am 29.06.2014 09:06, schrieb Martin S:


x=int(input('Enter an integer '))
y=int(input('Enter another integer '))
z=int(input('Enter a third integer '))
formatStr='Integer {0}, {1}, {2}, and the sum is {3}.'
equations=formatStr.format(x,y,z,x+y+z)
print(equations)
formatStr2='{0} divided by {1} is {2} with a reminder of {3}'
equations2=formatStr2.format(x,y,x//y,x%y)
print(equations2)

And obviously this works.
But the question is: if I want to keep the results of {2} and {3}
between the first instance (formatStr) and the second (formatStr2)  how
would I go about it? Apprently using {4} and {5}  instead results in a
index sequence error as in

IndexError: tuple index out of range



{0} ... {3} are just placeholders in your format strings, they can't 
exist outside of them. And you can't put more placeholders into the 
format string than you've got values to put into them. That's what the 
IndexError is about.


But nothing forces you to put your calculations into the call to 
format(). Instead, give names to the results you want to keep:


mysum = x + y + z
equation_sentence_1 = formatStr.format(x, y, z, mysum)
...
myquotient = x // y
myremainder = x % y
# or, nicer: (myquotient, myremainder) = divmod(x, y)
equation_sentence_2 = formatStr2.format(x, y, myquotient, myremainder)
...

Now you still can do all you want with mysum, myquotient, myremainder.

HTH
Sibylle

BTW: it's better to post here using text, not HTML. Not all newsreaders 
and mail clients used for this list cope well with HTML. And it's just 
luck that your code doesn't contain indentations, they might not 
survive. Moreover code is much more readable with a fixed font which you 
get as a by-product using text.





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


Re: Newbie coding question - format error

2014-06-29 Thread Roy Smith
In article ,
 Sibylle Koczian  wrote:

> Am 29.06.2014 09:06, schrieb Martin S:

> > IndexError: tuple index out of range
> >
> 
> {0} ... {3} are just placeholders in your format strings, they can't 
> exist outside of them. And you can't put more placeholders into the 
> format string than you've got values to put into them. That's what the 
> IndexError is about.

This seems like a confusing error message to me.  I've opened 
http://bugs.python.org/issue21879 on this.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Newbie coding question - format error

2014-06-29 Thread Terry Reedy

On 6/29/2014 3:06 AM, Martin S wrote:

A couple of additional notes:


x=int(input('Enter an integer '))
y=int(input('Enter another integer '))
z=int(input('Enter a third integer '))
formatStr='Integer {0}, {1}, {2}, and the sum is {3}.'


When the replacement fields and arguments are in the same order, the 
indexes are optional. The following works, and might be less confusing.


formatStr = 'Integer {}, {}, {}, and the sum is {}.'


equations=formatStr.format(x,y,z,x+y+z)


We no longer have a space shortage ;-). The following is easier to read.

equations = formatStr.format(x, y, z, x+y+z)


print(equations)


Compute quotient and remainder with the divmod function.

q, r = divmod(x, y)

Both are computed at once and x // y and x % y just toss the other 
answer. x // y == divmod(x, y)[0], x % y == divmod(x, y)[1]


--
Terry Jan Reedy

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


Re: Writing Multiple files at a times

2014-06-29 Thread subhabangalore
On Sunday, June 29, 2014 7:31:37 PM UTC+5:30, Roy Smith wrote:
> In article ,
> 
>  Dave Angel  wrote:
> 
> 
> 
> > [email protected] Wrote in message:
> 
> > > Dear Group,
> 
> > > 
> 
> > > I am trying to crawl multiple URLs. As they are coming I want to write 
> > > them 
> 
> > > as string, as they are coming, preferably in a queue. 
> 
> > > 
> 
> > > If any one of the esteemed members of the group may kindly help.
> 
> > > 
> 
> > 
> 
> > >From your subject line,  it appears you want to keep multiple files open, 
> 
> > >and write to each in an arbitrary order.  That's no problem,  up to the 
> 
> > >operating system limits.  Define a class that holds the URL information 
> > >and 
> 
> > >for each instance,  add an attribute for an output file handle. 
> 
> > 
> 
> > Don't forget to close each file when you're done with the corresponding URL.
> 
> 
> 
> One other thing to mention is that if you're doing anything with 
> 
> fetching URLs from Python, you almost certainly want to be using Kenneth 
> 
> Reitz's excellent requests module (http://docs.python-requests.org/).  
> 
> The built-in urllib support in Python works, but requests is so much 
> 
> simpler to use.

Dear Group,

Sorry if I miscommunicated. 

I am opening multiple URLs with urllib.open, now one Url has huge html source 
files, like that each one has. As these files are read I am trying to 
concatenate them and put in one txt file as string. 
>From this big txt file I am trying to take out each html file body of each URL 
>and trying to write and store them with attempts like,

for i, line in enumerate(file1):
f = open("/python27/newfile_%i.txt" %i,'w')
f.write(line)
f.close()

Generally not much of an issue, but was thinking of some better options.

Regards,
Subhabrata Banerjee. 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Newbie coding question - format error

2014-06-29 Thread Martin S
Thanks for the input. The main thing was that 

On 29 Jun 2014, Terry Reedy  wrote:
>On 6/29/2014 3:06 AM, Martin S wrote:
>
>A couple of additional notes:
>
>> x=int(input('Enter an integer '))
>> y=int(input('Enter another integer '))
>> z=int(input('Enter a third integer '))
>> formatStr='Integer {0}, {1}, {2}, and the sum is {3}.'
>
>When the replacement fields and arguments are in the same order, the 
>indexes are optional. The following works, and might be less confusing.
>
>formatStr = 'Integer {}, {}, {}, and the sum is {}.'
>
>> equations=formatStr.format(x,y,z,x+y+z)
>
>We no longer have a space shortage ;-). The following is easier to
>read.
>
>equations = formatStr.format(x, y, z, x+y+z)
>
>> print(equations)
>
>Compute quotient and remainder with the divmod function.
>
>q, r = divmod(x, y)
>
>Both are computed at once and x // y and x % y just toss the other 
>answer. x // y == divmod(x, y)[0], x % y == divmod(x, y)[1]

-- Sent with K-@ Mail - the evolution of emailing.-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Newbie coding question - format error

2014-06-29 Thread Martin S
Thanks for the input. The main thing was that the replacement fields were only 
valid for their local environment. 

/martin 

On 29 Jun 2014, Terry Reedy  wrote:
>On 6/29/2014 3:06 AM, Martin S wrote:
>
>A couple of additional notes:
>
>> x=int(input('Enter an integer '))
>> y=int(input('Enter another integer '))
>> z=int(input('Enter a third integer '))
>> formatStr='Integer {0}, {1}, {2}, and the sum is {3}.'
>
>When the replacement fields and arguments are in the same order, the 
>indexes are optional. The following works, and might be less confusing.
>
>formatStr = 'Integer {}, {}, {}, and the sum is {}.'
>
>> equations=formatStr.format(x,y,z,x+y+z)
>
>We no longer have a space shortage ;-). The following is easier to
>read.
>
>equations = formatStr.format(x, y, z, x+y+z)
>
>> print(equations)
>
>Compute quotient and remainder with the divmod function.
>
>q, r = divmod(x, y)
>
>Both are computed at once and x // y and x % y just toss the other 
>answer. x // y == divmod(x, y)[0], x % y == divmod(x, y)[1]

-- Sent with K-@ Mail - the evolution of emailing.-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Writing Multiple files at a times

2014-06-29 Thread Denis McMahon
On Sun, 29 Jun 2014 10:32:00 -0700, subhabangalore wrote:

> I am opening multiple URLs with urllib.open, now one Url has huge html
> source files, like that each one has. As these files are read I am
> trying to concatenate them and put in one txt file as string.
> From this big txt file I am trying to take out each html file body of
> each URL and trying to write and store them

OK, let me clarify what I think you said.

First you concatenate all the web pages into a single file.
Then you extract all the page bodies from the single file and save them 
as separate files.

This seems a silly way to do things, why don't you just save each html 
body section as you receive it?

This sounds like it should be something as simple as:

from BeautifulSoup import BeautifulSoup
import requests

urlList = [ 
"http://something/";, 
"http://something/";, 
"http://something/";, 
... ]

n = 0
for url in urlList:
r = requests.get( url )
soup = BeautifulSoup( r.content )
body = soup.find( "body" )
fp = open( "scraped/body{:0>5d}.htm".format( n ), "w" )
fp.write( body.prettify() )
fp.close
n += 1

will give you:

scraped/body0.htm
scraped/body1.htm
scraped/body2.htm


for as many urls as you have in your url list. (make sure the target 
directory exists!)

-- 
Denis McMahon, [email protected]
-- 
https://mail.python.org/mailman/listinfo/python-list