how to add new print %b format to python?

R

Rusty Shackleford

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:
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.
 
P

P

Rusty said:
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:



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.
 
J

John Roth

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
 
J

John Roth

Rusty Shackleford said:
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:

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
 
B

Brian Kelley

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
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
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.py/python/python/dist/src/Objects/stringobject.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
 
N

Nick Vargish

Rusty Shackleford said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,197
Messages
2,571,038
Members
47,633
Latest member
BriannaLyk

Latest Threads

Top