how to add new print %b format to python?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rusty Shackleford

    how to add new print %b format to python?

    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:
    [color=blue][color=green][color=darkred]
    >>> '%b' % 3[/color][/color][/color]
    11[color=blue][color=green][color=darkred]
    >>> '%b' % 5[/color][/color][/color]
    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
  • P@draigBrady.com

    #2
    Re: how to add new print %b format to python?

    Rusty Shackleford wrote:[color=blue]
    > 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:
    >
    >[color=green][color=darkred]
    >>>>'%b' % 3[/color][/color]
    >
    > 11
    >[color=green][color=darkred]
    >>>>'%b' % 5[/color][/color]
    >
    > 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.[/color]

    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(bin ary(4096))

    Pádraig.

    Comment

    • John Roth

      #3
      Re: how to add new print %b format to python?


      <P@draigBrady.c om> wrote in message news:408D4480.8 090104@draigBra dy.com...[color=blue]
      > Rusty Shackleford wrote:[color=green]
      > > 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:
      > >
      > >[color=darkred]
      > >>>>'%b' % 3[/color]
      > >
      > > 11
      > >[color=darkred]
      > >>>>'%b' % 5[/color]
      > >
      > > 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.[/color]
      >
      > 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).[/color]

      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[color=blue]
      >
      > Pádraig.[/color]


      Comment

      • John Roth

        #4
        Re: how to add new print %b format to python?

        "Rusty Shackleford" <rs@overlook.ho melinux.net> wrote in message
        news:slrnc8q9qp .htc.rs@frank.o verlook.homelin ux.net...[color=blue]
        > 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:
        >[color=green][color=darkred]
        > >>> '%b' % 3[/color][/color]
        > 11[color=green][color=darkred]
        > >>> '%b' % 5[/color][/color]
        > 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.[/color]

        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



        Comment

        • Brian Kelley

          #5
          Re: how to add new print %b format to python?

          John Roth wrote:
          [color=blue]
          > "Rusty Shackleford" <rs@overlook.ho melinux.net> wrote in message
          > news:slrnc8q9qp .htc.rs@frank.o verlook.homelin ux.net...
          >[color=green]
          >>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:
          >>
          >>[color=darkred]
          >>>>>'%b' % 3[/color]
          >>
          >>11
          >>[color=darkred]
          >>>>>'%b' % 5[/color]
          >>
          >>101[/color][/color]

          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[color=blue][color=green]
          >> '%b' % (-1)[/color][/color]

          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
          [color=blue][color=green]
          >> '%32b' % (-1L)[/color][/color]
          111111111111111 111111111111111 11

          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:

          Download Python for free. The Python programming language, an object-oriented scripting and rapid application development language. You can download it from http://www.python.org/download


          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(String Type):
          def __mod__(self, *k, **kw):
          return StringType.__mo d__(self, *k, **kw)

          Good Luck





          Comment

          Working...