Re: [Python-Dev] PEP 3144 review.

2009-09-16 Thread python-3000
On Tue, Sep 15, 2009 at 09:35:13PM +0200, Sebastian Rittau wrote:
> On Tue, Sep 15, 2009 at 01:16:06PM -0400, Scott Dial wrote:
> 
> > I have to concur with the opinions above. I was very confused by the
> > following error:
> > 
> > >>> addr = ipaddr.IPAddress("10.1.2.3/255.255.240.0")
> > ...
> > ipaddr.IPAddressIPValidationError: '98.223.189.24/255.255.240.0' is not
> > a valid address (hint, it's probably a network)
> > 
> > Because, it *is* a address of a host on a network.
> 
> To me, 10.1.2.3/255.255.240.0 is not a host address, but specifies a
> network. I.e., 10.1.2.3/255.255.240.0 == 10.1.0.0/255.255.240.0 ==
> 10.1.35.200/20.
We shouldn't invent another bicycle here. It is a host address, that ALSO
specifies a network mask, that is not /32. Mask /32 just may be omitted when
written, but assumed, when stored by computer.

What you talk about is "address with mask". It is just IP address plus mask
information. IP address and mask just written together, one after another.
You MAY use mask to extract network part from an address, and have a "set" of
IP addresses, or range of IP addresses, or "network" as you say.
But you are not forced to do that with "MUST" statement, because you may also
use mask and IP address to compute widely used default value for a "broadcast"
address, or you may extract "host" part from the whole IP address.

There is no semantical difference between IPAddress for hosts and IPAddress for
networks, look at 'inet' type of PostgreSQL. Python shouldn't have separate
types for that - it is stupid, people will say 'python programmers are lamers'.
If you need only one IP address - you write AA.BB.CC.DD[/32] or /255.255.255.255
and the mask value may be omitted here. If you need to specify a larger range of
IP addresses, you specify another mask value, that is simple.


> 
> > >>> net = ipaddr.IPNetwork("10.1.2.3/255.255.240.0")
> > 
> > But then, I was dumbfounded as to how I could get the gateway IP from
> > this IPNetwork object.
> 
> Well, you can't. There is no way to determine a gateway, without querying
> the network topology. This is clearly outside the scope of this module.
> The only two known host addresses of a network are the network address
> (10.1.0.0 in the example you gave) and the broadcast address (10.1.15.255).
> 
> > It took me a while to figure out that you can
> > iterate over IPNetwork instances:
> > 
> > >>> gateway = net[1]
> > 
> > I was then confused, because:
> > 
> > >>> print(type(gateway))
> > 
> > 
> > Which sorta blew my mind.. I fully expected to receive an IPNetwork back
> > from that operation. It is unclear to me why the network information
> > gets chucked by that operation.
> 
> This makes perfect sense to me. An IP network consists of a list of IP
> addresses. Returning /32 networks seems kind of pointless to me.
Why? What differ IP Host address from IP Network address with mask /32?
Don't say "they have different python classes".
Well, I know, that an element is not a set containing only that element.
But that is about mathematical objects. What difference between /32 IP Network
and IP Host for Python?

Why IPNetwork can't be used where IPAddress is used?
When you only need IP address, you only use address part and ignore mask.
Why IPAddress can't be used where IPNetwork is accepted?
It is just IPNetwork with /32 mask.

It is counterintuitive for me, network administrator.

> 
> > I foresee having to work around that in
> > real applications by doing something obnoxious like:
> > 
> > >>> actual_gateway = ipaddr.IPNetwork("%s/%s" % (gateway, addr.netmask))
> 
> But a gateway is not an IP address plus hostmask. A gateway is just a single
> IP address. What is the use of adding a hostmask to the gateway IP address
> or some other IP address inside the network?
Just to specify an address together with a mask, for short. It is widespread.
People are used to it.
When a gateway address written with a mask, it fully specifies you IP stack
configuration, it specifies (when computed as usual):
   1. netmask
   2. your subnetwork address
   3. host address
   4. address of default route
   5. subnetwork broadcast address
   6. I don't know, maybe something else?
   7. Oh, yes, it shows you the range of IP addresses, available for your use,
  if it was your subnetwork.
So, what is better to write on network diagrams or other documentation, those
five data pieces or just one gateway-with-a-mask?


> 
>  - Sebastian
> ___
> Python-Dev mailing list
> Python-Dev@python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: 
> http://mail.python.org/mailman/options/python-dev/python-3000%40udmvt.ru

-- 
Alexey S.
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] PEP 3142: Add a "while" clause to generator expressions

2009-01-20 Thread python-3000
On Mon, Jan 19, 2009 at 10:10:00AM -0500, Gerald Britton wrote:
> Please find below PEP 3142: Add a "while" clause to generator
> expressions.  I'm looking for feedback and discussion.
> 
...
>   g = (n for n in range(100) while n*n < 50)

May I suggest you this variant?

def raiseStopIteration():
raise StopIteration

g = (n for n in range(100) if n*n < 50 or raiseStopIteration())

Well, there are more characters...

But it is not using any syntax changes and does not require any approval
to be functional. Yet it is as fast as the proposed variant, does not require
modules and, I hope, will not confuse you or anyone else.


-- 
Alexey G. Shpagin
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] teaching the new urllib

2009-02-04 Thread python-3000
On Tue, Feb 03, 2009 at 06:50:44PM -0500, Tres Seaver wrote:
> -BEGIN PGP SIGNED MESSAGE-
> Hash: SHA1
> 
> 
> The encoding information *is* available in the response headers, e.g.:
> 
> - -- %< -
> $ wget -S --spider http://knuth.luther.edu/test.html
> - --18:46:24--  http://knuth.luther.edu/test.html
>=> `test.html'
> Resolving knuth.luther.edu... 192.203.196.71
> Connecting to knuth.luther.edu|192.203.196.71|:80... connected.
> HTTP request sent, awaiting response...
>   HTTP/1.1 200 OK
>   Date: Tue, 03 Feb 2009 23:46:28 GMT
>   Server: Apache/2.0.50 (Linux/SUSE)
>   Last-Modified: Mon, 17 Sep 2007 23:35:49 GMT
>   ETag: "2fcd8-1d8-43b2bf40"
>   Accept-Ranges: bytes
>   Content-Length: 472
>   Keep-Alive: timeout=15, max=100
>   Connection: Keep-Alive
>   Content-Type: text/html; charset=ISO-8859-1
> Length: 472 [text/html]
> 200 OK
> - -- %< -
> 
> So, the OP's use case *could* be satisfied, assuming that the Py3K
> version of urllib sprouted a means of leveraging that header.  In this
> sense, fetching the resource over HTTP is *better* than loading it from
> a file:  information about the character set is explicit, and highly
> likely to be correct, at least for any resource people expect to render
> cleanly in a browser.

First of all, as it was noted, Content-Type may have no charset parameter,
or be omitted at all.
But the most important and the worst is that charset in Content-Type may
have no relation to charset in document. And even worse - charset specified
in document may have no relation to charset used to encode the document. :(

Remember, that headers are supplied by HTTP server and it have to read document
from just a file, so there is no difference, since there is no magic in being a
HTTP server. Ofcourse it will be correct to provide web-server with some hints
about charset of byte-encoded text documents, but web-server will not stop
working without charset specified or with incorrect charset.

This use case is really important for those international segments of Internet,
which have two or more conflicting character sets for their (single) alphabet.
As an example - every Russian Internet user can tell you that a browser, that
have no menu option to select explicitly what encoding to use for current
document, is completely unusable.

-- 
Alexey Shpagin
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com