[issue32820] Add __format__ method to ipaddress

2019-09-12 Thread Zachary Ware


Zachary Ware  added the comment:


New changeset f9c95a4ba24c52eb1c052e3052d677e90a429a9a by Zachary Ware 
(ewosborne) in branch 'master':
bpo-32820: __format__ method for ipaddress (#5627)
https://github.com/python/cpython/commit/f9c95a4ba24c52eb1c052e3052d677e90a429a9a


--
nosy: +zach.ware

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue32820] Add __format__ method to ipaddress

2018-03-04 Thread Eric Osborne

Eric Osborne  added the comment:

I have pushed out new diffs.

* moved __format__ to _BaseAddress, where it should have been in the first
place
* redid format parser as regexp, as it was getting awfully complicated
* added support for 'X'
* added support for 's' by falling through to regular format()
* added IPv6 to the test suite

This was a decent-sized change, but all the tests pass so it looks OK to me.
As per request, I defer importing re and compiling the necessary regexp
until it's absolutely necessary. This is significantly slower than
importing re and compiling the regexp when ipaddress is imported.

localized compile and import
In [4]: %timeit f'{a:#_b}'
7.05 µs ± 34.2 ns per loop (mean ± std. dev. of 7 runs, 10 loops each)

toplevel compile and import
In [2]: %timeit f'{a:#_b}'
5.34 µs ± 17 ns per loop (mean ± std. dev. of 7 runs, 10 loops each)

Is this worth trying to get clever about? It doesn't matter in my use case,
and I could make a reasonable argument either way. I'm tempted to leave it
localized, as I can't imagine a case where formatting eleventy billion IP
addresses as padded binary is all that time-sensitive.  On the other hand,
I'm also not sure how Pythonic it is to stick an import statement 20 lines
deep in a dunder method, so I'm open to suggestions.

eric

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue32820] Add __format__ method to ipaddress

2018-02-23 Thread Nick Coghlan

Nick Coghlan  added the comment:

I think supporting "s" makes sense, as we're going to need to treat the empty 
format string as implying "s" for backwards compatibility reasons:

>>> f"{v4}"
'1.2.3.4'

Right now, attempting to format that field without `!s` will give you a 
TypeError, but as Eric O notes, once IP addresses support __format__, we're 
going to have to choose between:

- raise an exception if formatting directions are given without a numeric 
typecode
- ignore the formatting in that case (which is what the current PR does)
- handle it the same way "!s" would (and also allow "s" as an explicit type 
code)

The last option sounds the most desirable to me, as that way we can just say 
"'s' is the default IP addressing formatting typecode" (similar to the way 'd' 
is the default for integers, and 'g' is the default for floating point numbers).

I *don't* think we should allow combining the numeric typecode with the string 
type code, though. The numeric types don't allow that, and f-strings already 
support nesting if you want to combine string formatting with numeric 
formatting:

>>> f"{f'{42**100:g}':>20s}"
'2.11314e+162'

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue32820] Add __format__ method to ipaddress

2018-02-23 Thread Eric Osborne

Eric Osborne  added the comment:

I dunno, I think this might be useful.  A binary representation is itself a
string, and takes formatting as such (ditto for hex, as hex(int()) returns
a string:

In [20]: a
Out[20]: IPv4Address('1.2.3.4')

In [92]: f'{a}'
Out[92]: '1.2.3.4'

In [21]: int(a)
Out[21]: 16909060

In [22]: f'{bin(int(a)):42s}'
Out[22]: '0b1001000110100   '

In [24]: f'{a:b}'
Out[24]: '0001001000110100'

In [25]: f'{a:b42s}'
Out[25]: '1.2.3.4'

That last one should return '1001000110100'.  I was
worried about going down a really deep rabbit hole trying to support a lot
of string format stuff with no use case, but there's not much more which
could be done which makes any sense.  's' seems reasonable.

My current code supports [b, x, n] integer presentation types.  I need to
add [X], that's just an oversight.  Supporting [b, x, X, n] means that an
IP address is considered an integer, and should get the subset of integer
presentations which make sense. Not the full set - neither octal nor
character are good fits.  But support for some sort of alignment padding
seems reasonable.  Consider:

In [61]: f'{42:30}'
Out[61]: '42'

In [62]: f'{int(a):30}'
Out[62]: '  16909060'

In [63]: f'{a:30}'
Out[63]: '1.2.3.4'

In [66]: f'{a:42b}'
Out[66]: '0001001000110100'

Those last two seem odd.  I think f'{a:30}' should return the same thing as
this:

In [69]: f'{str(a):30}'
Out[69]: '1.2.3.4   '

and f'{a:42b'} should return the same thing as this:

In [77]: f'{bin(int(a)):42}'
Out[77]: '0b1001000110100   '

This also means supporting [>,<,^] alignment.  And, of course, ignoring any
length spec too short, as is done with regular integer formatting:

In [86]: b
Out[86]: 16909060

In [87]: f'{b:6}'
Out[87]: '16909060'

Thoughts?

eric

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue32820] Add __format__ method to ipaddress

2018-02-23 Thread Eric V. Smith

Eric V. Smith  added the comment:

True enough. You'd think that I (of all people!) would know that. Nevermind.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue32820] Add __format__ method to ipaddress

2018-02-23 Thread Serhiy Storchaka

Serhiy Storchaka  added the comment:

You always can use f'{addr!s:20}'.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue32820] Add __format__ method to ipaddress

2018-02-23 Thread Eric V. Smith

Eric V. Smith  added the comment:

I'd like 's' to be supported, and just be passed to format(str(self), fmt) (or 
some alternate spelling for the same thing).

My use case is to format tables including addresses:

print(f'{addr:20s} {hostname}')

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue32820] Add __format__ method to ipaddress

2018-02-20 Thread Nick Coghlan

Nick Coghlan  added the comment:

I've updated the issue title to reflect the revised proposal (i.e. using 
__format__ rather than a new IP address specific method).

--
title: Add bits method to ipaddress -> Add __format__ method to ipaddress

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com