How to convert a number to binary?

2007-05-17 Thread Lyosha
Converting binary to base 10 is easy:
>>> int('', 2)
255

Converting base 10 number to hex or octal is easy:
>>> oct(100)
'0144'
>>> hex(100)
'0x64'

Is there an *easy* way to convert a number to binary?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to convert a number to binary?

2007-05-17 Thread Lyosha
On May 17, 4:40 pm, Michael Bentley <[EMAIL PROTECTED]> wrote:
> On May 17, 2007, at 6:33 PM, Lyosha wrote:
>
> > Converting binary to base 10 is easy:
> >>>> int('', 2)
> > 255
>
> > Converting base 10 number to hex or octal is easy:
> >>>> oct(100)
> > '0144'
> >>>> hex(100)
> > '0x64'
>
> > Is there an *easy* way to convert a number to binary?
>
> def to_base(number, base):
> 'converts base 10 integer to another base'
>
> number = int(number)
> base = int(base)
> if base < 2 or base > 36:
> raise ValueError, "Base must be between 2 and 36"
> if not number:
> return 0
>
> symbols = string.digits + string.lowercase[:26]
> answer = []
> while number:
> number, remainder = divmod(number, base)
> answer.append(symbols[remainder])
> return ''.join(reversed(answer))
>
> Hope this helps,
> Michael

That's way too complicated...  Is there any way to convert it to a one-
liner so that I can remember it?  Mine is quite ugly:
"".join(str((n/base**i) % base) for i in range(20) if n>=base**i)
[::-1].zfill(1)

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to convert a number to binary?

2007-05-18 Thread Lyosha
On May 17, 11:04 pm, Stargaming <[EMAIL PROTECTED]> wrote:
[...]
> >>>Is there an *easy* way to convert a number to binary?
[...]
>
> Wrote this a few moons ago::
>
>dec2bin = lambda x: (dec2bin(x/2) + str(x%2)) if x else ''

This is awesome.  Exactly what I was looking for.  Works for other
bases too.

I guess the reason I couldn't come up with something like this was
being brainwashed that lambda is a no-no.

And python2.5 funky ?: expression comes in handy!

Thanks a lot!

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to convert a number to binary?

2007-05-18 Thread Lyosha
On May 17, 11:10 pm, Ben Finney <[EMAIL PROTECTED]>
wrote:
[...]
> > That's way too complicated...  Is there any way to convert it to a
> > one- liner so that I can remember it?
>
> You put in a module so you don't *have* to remember it.
>
> Then, you use it in this one-liner:
>
> foo = to_base(15, 2)
>
> Carrying a whole lot of one-liners around in your head is a waste of
> neurons. Neurons are far more valuable than disk space, screen lines,
> or CPU cycles.

While I agree with this general statement, I think remembering a
particular one-liner to convert a number to a binary is more valuable
to my brain than remembering where I placed the module that contains
this function.

I needed the one-liner not to save disk space or screen lines.  It's
to save time, should I need to convert to binary when doing silly
little experiments.  I would spend more time getting the module
wherever it is I stored it (and rewriting it if it got lost).

It's fun, too.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Inheriting from Python list object(type?)

2007-05-23 Thread Lyosha
On May 23, 12:07 pm, Mangabasi <[EMAIL PROTECTED]> wrote:
> On May 23, 1:43 pm, "Jerry Hill" <[EMAIL PROTECTED]> wrote:
>
>
>
> > On 23 May 2007 11:31:56 -0700, Mangabasi <[EMAIL PROTECTED]> wrote:
>
> > > When I modified this to:
>
> > > class Point(list):
> > > def __init__(self,x,y):
> > > super(Point, self).__init__([x, y])
> > > self.x = x
> > > self.y = y
>
> > > It worked.
>
> > Are you sure?
>
> > >>> p = Point(10, 20)
> > >>> p
> > [10, 20]
> > >>> p.x
> > 10
> > >>> p.x = 15
> > >>> p
> > [10, 20]
> > >>> p[0]
> > 10
> > >>> p.x
> > 15
>
> > That doesn't look like what you were asking for in the original post.
> > I'm afraid I don't know anything about numpy arrays or what special
> > attributes an object may need to be put into a numpy array though.
>
> > --
> > Jerry
>
> This is the winner:
>
> class Point(list):
> def __init__(self, x, y, z = 1):
> super(Point, self).__init__([x, y, z])
> self.x = x
> self.y = y
> self.z = z
[...]

http://docs.python.org/dev/whatsnew/node3.html announces named tuples
in python2.6.  This is not what you want since tuples are immutable,
but you might get some inspiration from their implementation.  Or
maybe not.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Inheriting from Python list object(type?)

2007-05-23 Thread Lyosha
On May 23, 12:07 pm, Mangabasi <[EMAIL PROTECTED]> wrote:
> On May 23, 1:43 pm, "Jerry Hill" <[EMAIL PROTECTED]> wrote:
>
>
>
> > On 23 May 2007 11:31:56 -0700, Mangabasi <[EMAIL PROTECTED]> wrote:
>
> > > When I modified this to:
>
> > > class Point(list):
> > > def __init__(self,x,y):
> > > super(Point, self).__init__([x, y])
> > > self.x = x
> > > self.y = y
>
> > > It worked.
>
> > Are you sure?
>
> > >>> p = Point(10, 20)
> > >>> p
> > [10, 20]
> > >>> p.x
> > 10
> > >>> p.x = 15
> > >>> p
> > [10, 20]
> > >>> p[0]
> > 10
> > >>> p.x
> > 15
>
> > That doesn't look like what you were asking for in the original post.
> > I'm afraid I don't know anything about numpy arrays or what special
> > attributes an object may need to be put into a numpy array though.
>
> > --
> > Jerry
>
> This is the winner:
>
> class Point(list):
> def __init__(self, x, y, z = 1):
> super(Point, self).__init__([x, y, z])
> self.x = x
> self.y = y
> self.z = z
[...]

http://docs.python.org/dev/whatsnew/node3.html announces named tuples
in python2.6.  This is not what you want since tuples are immutable,
but you might get some inspiration from their implementation.  Or
maybe not.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Inheriting from Python list object(type?)

2007-05-23 Thread Lyosha
On May 23, 12:07 pm, Mangabasi <[EMAIL PROTECTED]> wrote:
> On May 23, 1:43 pm, "Jerry Hill" <[EMAIL PROTECTED]> wrote:
>
>
>
> > On 23 May 2007 11:31:56 -0700, Mangabasi <[EMAIL PROTECTED]> wrote:
>
> > > When I modified this to:
>
> > > class Point(list):
> > > def __init__(self,x,y):
> > > super(Point, self).__init__([x, y])
> > > self.x = x
> > > self.y = y
>
> > > It worked.
>
> > Are you sure?
>
> > >>> p = Point(10, 20)
> > >>> p
> > [10, 20]
> > >>> p.x
> > 10
> > >>> p.x = 15
> > >>> p
> > [10, 20]
> > >>> p[0]
> > 10
> > >>> p.x
> > 15
>
> > That doesn't look like what you were asking for in the original post.
> > I'm afraid I don't know anything about numpy arrays or what special
> > attributes an object may need to be put into a numpy array though.
>
> > --
> > Jerry
>
> This is the winner:
>
> class Point(list):
> def __init__(self, x, y, z = 1):
> super(Point, self).__init__([x, y, z])
> self.x = x
> self.y = y
> self.z = z
[...]

http://docs.python.org/dev/whatsnew/node3.html announces named tuples
in python2.6.  This is not what you want since tuples are immutable,
but you might get some inspiration from their implementation.  Or
maybe not.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Inheriting from Python list object(type?)

2007-05-23 Thread Lyosha
On May 23, 12:19 pm, Lyosha <[EMAIL PROTECTED]> wrote:
> On May 23, 12:07 pm, Mangabasi <[EMAIL PROTECTED]> wrote:
>
> > On May 23, 1:43 pm, "Jerry Hill" <[EMAIL PROTECTED]> wrote:
>
> > > On 23 May 2007 11:31:56 -0700, Mangabasi <[EMAIL PROTECTED]> wrote:
>
> > > > When I modified this to:
>
> > > > class Point(list):
> > > > def __init__(self,x,y):
> > > > super(Point, self).__init__([x, y])
> > > > self.x = x
> > > > self.y = y
>
> > > > It worked.
>
> > > Are you sure?
>
> > > >>> p = Point(10, 20)
> > > >>> p
> > > [10, 20]
> > > >>> p.x
> > > 10
> > > >>> p.x = 15
> > > >>> p
> > > [10, 20]
> > > >>> p[0]
> > > 10
> > > >>> p.x
> > > 15
>
> > > That doesn't look like what you were asking for in the original post.
> > > I'm afraid I don't know anything about numpy arrays or what special
> > > attributes an object may need to be put into a numpy array though.
>
> > > --
> > > Jerry
>
> > This is the winner:
>
> > class Point(list):
> > def __init__(self, x, y, z = 1):
> > super(Point, self).__init__([x, y, z])
> > self.x = x
> > self.y = y
> > self.z = z
>
> [...]
>
> http://docs.python.org/dev/whatsnew/node3.htmlannounces named tuples
> in python2.6.  This is not what you want since tuples are immutable,
> but you might get some inspiration from their implementation.  Or
> maybe not.


Dude, google groups suck!  They say "an error has occurred" and the
message is happily posted.

-- 
http://mail.python.org/mailman/listinfo/python-list