Re: [Tutor] http question

2014-11-09 Thread Alan Gauld

On 09/11/14 05:53, Clayton Kirkwood wrote:


After reading various documentation, it seems that the urllib is limited.
Perhaps I am wrong and you will clarify that point.


Every library is limited. The issue is not whether it is limited, but 
whether it has enough functionality to do what you need to do.


So far you've given us no clue about what you plan on other than the 
statement that you want to "do some http posts".


The standard library solution for that in Python land is the
urllib package.

A popular third party alternative is requests.

httplib is normally only needed if you are doing non-standard
things. Accessing a server which is not a web server for example.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

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


Re: [Tutor] http question

2014-11-09 Thread Steven D'Aprano
On Sat, Nov 08, 2014 at 09:53:33PM -0800, Clayton Kirkwood wrote:

> >> but I also am aware of httplib2, but it still seems to be in eternal
> >> alpha.
> >
> >What leads you to that conclusion? If you're talking about this:
> >
> >https://github.com/jcgregorio/httplib2
> >
> >I don't see any sign that it is alpha version software. According to the
> >readme file, it is at version 0.8.
> >
> >I don't see any signs that the author publicly releases any alpha or
> >beta versions, they all appear to be ready for production. But if you
> >have seen something that suggests otherwise, please point it out,
> >because I'm happy to be corrected.
> 
> Well, I work from the premise that 0.anything is still a work in progress

All software is always a work in progress, until such time it is 
abandoned.

> and hasn't gotten to a point where the author is comfortable with general
> use. I am sure that you disagree.

In the FOSS (Free and Open Source Software) community, version 0.x does 
not always carry connotations of being unready for use. It may, or it 
may not. But normally "alpha" software will have an "a" in the version 
number, e.g. 0.7a, 0.7b for beta, 0.7rc1 (release candidate 1), 0.7 is 
ready for production.

What matters is not my opinion, or yours, but that of the author of the 
software, and I don't know what that is.


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


[Tutor] Test to check if values of dictionary are all equal (which happen to be dictionaries)

2014-11-09 Thread Jignesh Sutar
I needed to test if the values of all entries in a dictionary were equal
but since the values themselves were dictionaries I couldn't simply take a
set of the values and test if this equated to one. So I ended up taking all
combination of the keys and testing pairs of sub dictionaries. I just want
to check that there isn't a more direct way of doing this that testing all
combinations?


import itertools

dictmain={"K1": {1:"SD_V1",2:"SD_V2"},
  "K2": {1:"SD_V1",2:"SD_V2"},
  "K3": {1:"SD_V1",2:"SD_V2"}}

for compare in list(itertools.combinations(dictmain,2)):
print "Comparing dictionaries:", compare

if dictmain[compare[0]]==dictmain[compare[1]]:
print "comb dict are equal"
else:
print "comb dict are NOT equal"
break


Many thanks in advance,
Jignesh
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Test to check if values of dictionary are all equal (which happen to be dictionaries)

2014-11-09 Thread Peter Otten
Jignesh Sutar wrote:

> I needed to test if the values of all entries in a dictionary were equal
> but since the values themselves were dictionaries I couldn't simply take a
> set of the values and test if this equated to one. So I ended up taking
> all combination of the keys and testing pairs of sub dictionaries. I just
> want to check that there isn't a more direct way of doing this that
> testing all combinations?
> 
> 
> import itertools
> 
> dictmain={"K1": {1:"SD_V1",2:"SD_V2"},
>   "K2": {1:"SD_V1",2:"SD_V2"},
>   "K3": {1:"SD_V1",2:"SD_V2"}}
> 
> for compare in list(itertools.combinations(dictmain,2)):
> print "Comparing dictionaries:", compare
> 
> if dictmain[compare[0]]==dictmain[compare[1]]:
> print "comb dict are equal"
> else:
> print "comb dict are NOT equal"
> break
> 
> 
> Many thanks in advance,
> Jignesh


If you don't have exotic data in your dicts equality should be transitive, 
i. e. from

a == b and a == c

follows

b == c

so that you don't have to test the latter explicitly. 
This reduces the number of tests significantly.

values = dictmain.itervalues() # Python 3: iter(dictmain.values())
first = next(values) # pick an arbitrary value to compare against all others
if all(first == item for item in values):
print("all dicts are equal")
else:
print("not all dicts are equal")

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


[Tutor] don't understand iteration

2014-11-09 Thread Clayton Kirkwood
I have the following code:

 

import urllib.request,re,string

months = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May.', 'Jun.', 'Jul.', 'Aug.',
'Sep.', 'Oct.', 'Nov.', 'Dec.']

from urllib.request import urlopen

for line in urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl'):

line = line.decode('utf-8')  # Decoding the binary data to text.

if 'EST' in line or 'EDT' in line:  # look for Eastern Time

 blah =
re.search(r'<\w\w>(\w{3}\.)\s+(\d{2}),\s+(\d{2}).+([AP]M)\s+(E[SD]T)', line)



 (month, day, time, ap, offset) = blah.group(1,2,3,4,5)

 

 print(blah,'\n',ap,month, offset,day, time )
#which gives:

 

<_sre.SRE_Match object; span=(0, 28), match='Nov. 09, 07:15:46 PM EST'> 

 PM Nov. EST 09 07

 

This works fine, but in the (month... line, I have blah.group(1,2,3,4,5),
but this is problematic for me. I shouldn't have to use that 1,2,3,4,5
sequence. I tried to use many alternatives using:  range(5) which doesn't
work, list(range(5)) which actually lists the numbers in a list, and several
others. As I read it, the search puts out a tuple. I was hoping to just
assign the re.search to month, day, time, ap, offset directly. Why wouldn't
that work? Why won't a range(5) work? I couldn't find a way to get the len
of blah.

 

What am I missing?

 

Clayton

 

You can tell the caliber of a man by his gun--c. kirkwood

 

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


Re: [Tutor] don't understand iteration

2014-11-09 Thread Dave Angel
You forgot to state your Python version. I'll assume 3.4

"Clayton Kirkwood"  Wrote in message:
> I have the following code: import urllib.request,re,stringmonths = 
> ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May.', 'Jun.', 'Jul.', 'Aug.', 'Sep.', 
> 'Oct.', 'Nov.', 'Dec.']from urllib.request import urlopenfor line in 
> urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl'):line = 
> line.decode('utf-8')  # Decoding the binary data to text.if 'EST' in line 
> or 'EDT' in line:  # look for Eastern Time blah = 
> re.search(r'<\w\w>(\w{3}\.)\s+(\d{2}),\s+(\d{2}).+([AP]M)\s+(E[SD]T)', line)  
>(month, day, time, ap, offset) = blah.group(1,2,3,4,5) 

You apparently know you want exactly 5 items, and you want them in
 order, so you need 5 arguments,  in order. If you don't like what
 you have, you can pass a list, if you precede it with an
 asterisk. 

(month, day, time, ap, offset) = blah.group( *list (range (1, 6)))

Should do it.


-- 
DaveA

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


Re: [Tutor] don't understand iteration

2014-11-09 Thread Peter Otten
Clayton Kirkwood wrote:

> I have the following code:

>  blah =
> re.search(r'<\w\w>(\w{3}\.)\s+(\d{2}),\s+(\d{2}).+([AP]M)\s+(E[SD]T)',
> line)

>  (month, day, time, ap, offset) = blah.group(1,2,3,4,5)
> This works fine, but in the (month... line, I have blah.group(1,2,3,4,5),
> but this is problematic for me. I shouldn't have to use that 1,2,3,4,5
> sequence. I tried to use many alternatives using:  range(5) which doesn't
> work, list(range(5)) which actually lists the numbers in a list, and
> several others. As I read it, the search puts out a tuple. I was hoping to
> just assign the re.search to month, day, time, ap, offset directly. Why
> wouldn't that work? Why won't a range(5) work? I couldn't find a way to
> get the len of blah.

> What am I missing?



While the direct answer would be

month, day, time, ap, offset = blah.group(*range(1,6))

there is also the groups() method

month, day, time, ap, offset = blah.groups()

which is appropriate when you want to unpack all capturing groups.



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


Re: [Tutor] don't understand iteration

2014-11-09 Thread Clayton Kirkwood


>-Original Message-
>From: Tutor [mailto:tutor-bounces+crk=godblessthe...@python.org] On
>Behalf Of Dave Angel
>Sent: Sunday, November 09, 2014 5:10 PM
>To: tutor@python.org
>Subject: Re: [Tutor] don't understand iteration
>
>You forgot to state your Python version. I'll assume 3.4
>
>"Clayton Kirkwood"  Wrote in message:
>> I have the following code: import urllib.request,re,stringmonths =
>['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May.', 'Jun.', 'Jul.', 'Aug.', 'Sep.',
>'Oct.', 'Nov.', 'Dec.']from urllib.request import urlopenfor line in
>urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl'):line =
>line.decode('utf-8')  # Decoding the binary data to text.if 'EST' in
>line or 'EDT' in line:  # look for Eastern Time blah =
>re.search(r'<\w\w>(\w{3}\.)\s+(\d{2}),\s+(\d{2}).+([AP]M)\s+(E[SD]T)',
>line) (month, day, time, ap, offset) =
>blah.group(1,2,3,4,5)
>
>You apparently know you want exactly 5 items, and you want them in
>order, so you need 5 arguments,  in order. If you don't like what  you
>have, you can pass a list, if you precede it with an  asterisk.
>
>(month, day, time, ap, offset) = blah.group( *list (range (1, 6)))
>
>Should do it.
>

You are so smart. I am going to stop listening to all those people in Russia
and what they say about you. Where did you find that one? What does the '*'
do/why? And why the weird range thing?

Clayton
:<)))

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



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


Re: [Tutor] don't understand iteration

2014-11-09 Thread Clayton Kirkwood


>-Original Message-
>From: Tutor [mailto:tutor-bounces+crk=godblessthe...@python.org] On
>Behalf Of Peter Otten
>Sent: Sunday, November 09, 2014 5:47 PM
>To: tutor@python.org
>Subject: Re: [Tutor] don't understand iteration
>
>Clayton Kirkwood wrote:
>
>> I have the following code:
>
>>  blah =
>> re.search(r'<\w\w>(\w{3}\.)\s+(\d{2}),\s+(\d{2}).+([AP]M)\s+(E[SD]T)',
>> line)
>
>>  (month, day, time, ap, offset) = blah.group(1,2,3,4,5) This
>> works fine, but in the (month... line, I have blah.group(1,2,3,4,5),
>> but this is problematic for me. I shouldn't have to use that 1,2,3,4,5
>> sequence. I tried to use many alternatives using:  range(5) which
>> doesn't work, list(range(5)) which actually lists the numbers in a
>> list, and several others. As I read it, the search puts out a tuple. I
>> was hoping to just assign the re.search to month, day, time, ap,
>> offset directly. Why wouldn't that work? Why won't a range(5) work? I
>> couldn't find a way to get the len of blah.
>
>> What am I missing?
>
>
>
>While the direct answer would be
>
>month, day, time, ap, offset = blah.group(*range(1,6))
>
>there is also the groups() method
>
>month, day, time, ap, offset = blah.groups()
>
>which is appropriate when you want to unpack all capturing groups.
>

Still seems odd to me. Blah is a tuple, and would think that there should be
a natural way to pull it apart. One can iterate across a string or list
easily, why not a tuple. I also would have thought that a range(5) would
have worked. Isn't it an iterable?

Thanks for the insight...

Clayton


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



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


Re: [Tutor] don't understand iteration

2014-11-09 Thread Ben Finney
"Clayton Kirkwood"  writes:

> >-Original Message-
> >From: Tutor [mailto:tutor-bounces+crk=godblessthe...@python.org] On
> >Behalf Of Dave Angel

(Clayton, does your mail client not present messages written by their
authors? The messages should not come to you “From:” the tutor list
itself. It's awkward to follow whom you're quoting.)

> >(month, day, time, ap, offset) = blah.group( *list (range (1, 6)))
> >
> >Should do it.

> Where did you find that one?

Working through the Python Tutorial (actually doing the examples, and
working to understand them before moving on) teaches these and other
Python concepts https://docs.python.org/3/tutorial/>.

> What does the '*' do/why?

Learn about sequence unpacking in function parameters at the Tutorial
https://docs.python.org/3/tutorial/controlflow.html#tut-unpacking-arguments>.

> And why the weird range thing?

Learn about the ‘range’ built-in at the Python library reference
https://docs.python.org/3/library/functions.html#func-range>.

-- 
 \ “I went to the museum where they had all the heads and arms |
  `\  from the statues that are in all the other museums.” —Steven |
_o__)   Wright |
Ben Finney

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