Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > how to add new print %b format to python?

Reply
Thread Tools

how to add new print %b format to python?

 
 
Rusty Shackleford
Guest
Posts: n/a
 
      04-26-2004
I have a Summer in front of me without any school, and I'd like to add a
new format for python print strings that will show any number in a
binary representation. For example:

>>> '%b' % 3

11
>>> '%b' % 5

101

You get the idea. I've written functions that return strings, so that
part is done, but where do I go to tinker with the python interpreter to
add this new format?

Please don't argue with me about whether this is an advisable goal in
itself -- I'm using it as a method to learn about the internals of the
python language.


--
My public key:
gpg --recv-keys --keyserver www.mandrakesecure.net 0x8D10BFD5
 
Reply With Quote
 
 
 
 
P@draigBrady.com
Guest
Posts: n/a
 
      04-26-2004
Rusty Shackleford wrote:
> I have a Summer in front of me without any school, and I'd like to add a
> new format for python print strings that will show any number in a
> binary representation. For example:
>
>
>>>>'%b' % 3

>
> 11
>
>>>>'%b' % 5

>
> 101
>
> You get the idea. I've written functions that return strings, so that
> part is done, but where do I go to tinker with the python interpreter to
> add this new format?
>
> Please don't argue with me about whether this is an advisable goal in
> itself -- I'm using it as a method to learn about the internals of the
> python language.


I don't think this will be accepted as the
format args are really a lowest common denominator
across all systems. For e.g. on linux you can use
the ' modifier to print numbers in locale format
(and example for mine is 1,234).

Also %b is already used by userspace utils in linux, from the docs:

In addition to the standard printf(1)
formats, %b causes printf to expand backslash escape
sequences in the corresponding argument, and %q causes
printf to output the corresponding argument in a format
that can be reused as shell input.

Anyway it's easy to get a binary representation
and I can't remember the last time I needed one?
Something like this should work:

binary = lambda n: n>0 and binary(n>>1)+[str(n&1)] or []
''.join(map(binary(4096))

Pádraig.
 
Reply With Quote
 
 
 
 
John Roth
Guest
Posts: n/a
 
      04-26-2004

<(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> Rusty Shackleford wrote:
> > I have a Summer in front of me without any school, and I'd like to add a
> > new format for python print strings that will show any number in a
> > binary representation. For example:
> >
> >
> >>>>'%b' % 3

> >
> > 11
> >
> >>>>'%b' % 5

> >
> > 101
> >
> > You get the idea. I've written functions that return strings, so that
> > part is done, but where do I go to tinker with the python interpreter to
> > add this new format?
> >
> > Please don't argue with me about whether this is an advisable goal in
> > itself -- I'm using it as a method to learn about the internals of the
> > python language.

>
> I don't think this will be accepted as the
> format args are really a lowest common denominator
> across all systems. For e.g. on linux you can use
> the ' modifier to print numbers in locale format
> (and example for mine is 1,234).


I don't believe this is the case. I think the % operator
does not use the C library's sprintf function., but I could
be wrong.

[snip]

The rest of this is equally off base - he explicitly said he
was not interested in ***whether*** he should do it, but
only in *** where to look *** to find out how to do it
as a summer programming exercise in the python core.

John Roth
>
> Pádraig.



 
Reply With Quote
 
John Roth
Guest
Posts: n/a
 
      04-26-2004
"Rusty Shackleford" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) t...
> I have a Summer in front of me without any school, and I'd like to add a
> new format for python print strings that will show any number in a
> binary representation. For example:
>
> >>> '%b' % 3

> 11
> >>> '%b' % 5

> 101
>
> You get the idea. I've written functions that return strings, so that
> part is done, but where do I go to tinker with the python interpreter to
> add this new format?
>
> Please don't argue with me about whether this is an advisable goal in
> itself -- I'm using it as a method to learn about the internals of the
> python language.


Well, if I was familiar with the internals, I'd start with the code
that implements the string object, since it's simply an overload of
the '%' operator. Sorry I can't help you with a module name, though.

To actually get it added to the core, you need to write a PEP and
champion it through the process. Writing a reference implementation
and the documentation is also a necessity. There's a PEP that
explains the documentation standards for Python C language code;
if your patch follows that you've improved your chances by some
significant amount.

John Roth



 
Reply With Quote
 
Brian Kelley
Guest
Posts: n/a
 
      04-27-2004
John Roth wrote:

> "Rusty Shackleford" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) t...
>
>>I have a Summer in front of me without any school, and I'd like to add a
>>new format for python print strings that will show any number in a
>>binary representation. For example:
>>
>>
>>>>>'%b' % 3

>>
>>11
>>
>>>>>'%b' % 5

>>
>>101


For some implementation PEP discussion ideas, I like this format, but
there are additional complexities such as what is

'%b' % -1 ?

This might just be a binary string equivalent to the current machine
bits. But what about long integers?

'%b' % (-1L)

This is technically an infinite string of ones.

Perhaps a useful solution is when printing out negative numbers, the
user must supply the padding.

so
>> '%b' % (-1)


raise a ValueError or some such and

Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: Binary format %b for negative numbers must have bit length
specified

and

>> '%32b' % (-1L)

11111111111111111111111111111111

Now for the source, you want to look at stringobject.c and in particular
the PyString_Format function. You can find the cvs version here:

http://cvs.sourceforge.net/viewcvs.p....c?view=markup

Note that you can make a test case by subclass string and the __mod__
function. This might actually be harder though since you will have to
figure out which argument is supposed to belong to the %b formatter.

from types import StringType

class mystring(StringType):
def __mod__(self, *k, **kw):
return StringType.__mod__(self, *k, **kw)

Good Luck





 
Reply With Quote
 
Nick Vargish
Guest
Posts: n/a
 
      05-04-2004
Rusty Shackleford <(E-Mail Removed)> writes:

> You get the idea. I've written functions that return strings, so that
> part is done, but where do I go to tinker with the python interpreter to
> add this new format?


I would look in Objects/stringobject.c, specifically the function
called PyString_FromFormatV.

Sorry this is a week late, but nobody else seems to have directly
addressed your question.

Nick

--
# sigmask || 0.2 || 20030107 || public domain || feed this to a python
print reduce(lambda x,y+chr(ord(y)-1),' Ojdl!Wbshjti!=obwAcboefstobudi/psh?')
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
print a vs print '%s' % a vs print '%f' a David Cournapeau Python 0 12-30-2008 03:19 AM
Problem - I want to print Current Output of Pdf file and should print once.I get print dialog box but it is not working keto Java 0 05-30-2007 11:27 AM
print dos format file into unix format PengYu.UT@gmail.com Python 5 10-27-2006 01:56 PM
Unlarging the print to print using PDF file to print Bun Mui Computer Support 3 09-13-2004 03:15 AM



Advertisments