By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,172 Members | 762 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,172 IT Pros & Developers. It's quick & easy.

how to add new print %b format to python?

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
P
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.
Jul 18 '05 #2

P: n/a

<P@draigBrady.com> wrote in message news:40**************@draigBrady.com...
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.

Jul 18 '05 #3

P: n/a
"Rusty Shackleford" <rs@overlook.homelinux.net> wrote in message
news:sl***************@frank.overlook.homelinux.ne 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

Jul 18 '05 #4

P: n/a
John Roth wrote:
"Rusty Shackleford" <rs@overlook.homelinux.net> wrote in message
news:sl***************@frank.overlook.homelinux.ne 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

Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.