Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Terry Reedy Newsgroups: comp.lang.python Subject: Re: Curious Omission In New-Style Formats Date: Mon, 11 Jul 2016 16:52:00 -0400 Lines: 39 Message-ID: References: <834b1cce-38dd-474c-8915-4ff1cd6b27ec@googlegroups.com> <7fcc8c21-106f-41d4-a5ba-409f3b54a56d@googlegroups.com> <5783c91e$0$1622$c3e8da3$5496439d@news.astraweb.com> <5783D63F.5040307@stoneleaf.us> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de 7zwwuwiJbRiYGA7iDFK6vgw2s1l0n6yrgzOnTHS64xBQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'width': 0.07; 'alternatives': 0.09; 'formatted': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'jan': 0.11; 'skip:# 20': 0.13; '2016': 0.16; '3:27': 0.16; 'expands': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'reedy': 0.16; 'wrote:': 0.16; 'skip:{ 20': 0.18; '>>>': 0.20; 'meant': 0.22; 'occurs': 0.22; 'text,': 0.22; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'header:User- Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'not.': 0.27; 'specify': 0.27; 'table': 0.32; 'int': 0.33; 'case,': 0.34; "skip:' 20": 0.34; 'running': 0.34; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'say': 0.37; 'received:org': 0.37; 'format': 0.39; 'does': 0.39; 'subject:-': 0.39; 'rather': 0.39; 'to:addr:python.org': 0.40; 'field': 0.60; 'challenge': 0.61; 'received:96': 0.63; '>>>>>': 0.66; 'jul': 0.72; 'received:fios.verizon.net': 0.91; 'improvement': 0.93 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: pool-96-227-207-81.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <834b1cce-38dd-474c-8915-4ff1cd6b27ec@googlegroups.com> <7fcc8c21-106f-41d4-a5ba-409f3b54a56d@googlegroups.com> <5783c91e$0$1622$c3e8da3$5496439d@news.astraweb.com> <5783D63F.5040307@stoneleaf.us> Xref: csiph.com comp.lang.python:111293 On 7/11/2016 3:27 PM, Ian Kelly wrote: > On Mon, Jul 11, 2016 at 12:54 PM, Terry Reedy wrote: >> In any case, I think it an improvement to say that '0x00123' has a field >> width of 7 rather than a 'precision' of 5. >> >>>>> '{:#07x}'.format(0x123) # specifiy field width >> '0x00123' >>>>> "%#0.5x" % 0x123 # specify int precision >> '0x00123' > > It occurs to me now that this does create a challenge if the format is > meant to support negative numbers as well: > >>>> '%#0.5x' % -0x123 > '-0x00123' This expands the field from 7 to 8 chars. In running text, this is alright. In formatted table columns, it is not. >>>> '{:#07x}'.format(-0x123) > '-0x0123' Multiple alternatives >>> '{: #08x} {: #08x}'.format(0x123, -0x123) ' 0x00123 -0x00123' >>> '{:+#08x} {:+#08x}'.format(0x123, -0x123) '+0x00123 -0x00123' >>> '{0:#0{1}x} {2:+#0{3}x}'.format(0x123, 7, -0x123, 8) '0x00123 -0x00123' >>> n1, n2, w = 0x123, -0x123, 7 >>> '{0:#0{1}x} {2:+#0{3}x}'.format(n1, w+(n1<0), n2, w+(n2<0)) '0x00123 -0x00123' In running text, I ight go with '+','-' prefix. -- Terry Jan Reedy