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

Restricting the alphabet of a string

P: n/a
Hi,

I ve being thinking about playing around with bit strings but use in
some encoding problems I m considering and was trying to decide how to
implement a bit string class. Is there a library out there for doing
basic things with bit strings already is my first question?

I know that I can extend string to bit string, but is there anyway I
can force the alphabet to be restricted to 1's and 0's (or even 1, 0
and -1, as an extension to from trinary strings).

class Binary_String(String):
pass

Many Thanks

Nathan
Apr 30 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Apr 30, 5:53 am, "Nathan Harmston" <ratchetg...@googlemail.com>
wrote:
Hi,

I ve being thinking about playing around with bit strings but use in
some encoding problems I m considering and was trying to decide how to
implement a bit string class. Is there a library out there for doing
basic things with bit strings already is my first question?

I know that I can extend string to bit string, but is there anyway I
can force the alphabet to be restricted to 1's and 0's (or even 1, 0
and -1, as an extension to from trinary strings).

class Binary_String(String):
pass

Many Thanks

Nathan
You could do something like this:

class Binary_String(str):
def __init__(self, val):
i = 0
for char in val:
if char not in "-101":
raise ValueError("illegal character at index " +
str(i) + "\nOnly -1,0,1 allowed.")

i+=1
self.val = val
b = Binary_String("1a0101")

--output:--
Traceback (most recent call last):
File "test1.py", line 13, in ?
b = Binary_String("1a100")
File "test1.py", line 6, in __init__
raise ValueError("illegal character at index " + str(i)
ValueError: illegal character at index 1
Only -1,0,1 allowed.

Apr 30 '07 #2

P: n/a
On Apr 30, 9:00 am, 7stud <bbxx789_0...@yahoo.comwrote:
On Apr 30, 5:53 am, "Nathan Harmston" <ratchetg...@googlemail.com>
wrote:


Hi,
I ve being thinking about playing around with bit strings but use in
some encoding problems I m considering and was trying to decide how to
implement a bit string class. Is there a library out there for doing
basic things with bit strings already is my first question?
I know that I can extend string to bit string, but is there anyway I
can force the alphabet to be restricted to 1's and 0's (or even 1, 0
and -1, as an extension to from trinary strings).
class Binary_String(String):
pass
Many Thanks
Nathan

You could do something like this:

class Binary_String(str):
def __init__(self, val):
i = 0
for char in val:
if char not in "-101":
raise ValueError("illegal character at index " +
str(i) + "\nOnly -1,0,1 allowed.")

i+=1
self.val = val

b = Binary_String("1a0101")

--output:--
Traceback (most recent call last):
File "test1.py", line 13, in ?
b = Binary_String("1a100")
File "test1.py", line 6, in __init__
raise ValueError("illegal character at index " + str(i)
ValueError: illegal character at index 1
Only -1,0,1 allowed.- Hide quoted text -

- Show quoted text -
Your character test only needs to check if the character is in "-10",
not "-101". In fact, this is a little misleading, since it appears to
check for "-1" but is really only checking for "-"s and "1"s (and "0"s
too, of course).

This code accepts "1-0101" also, but what is -0?

-- Paul
-- Paul

Apr 30 '07 #3

P: n/a
On Apr 30, 9:53 pm, "Nathan Harmston" <ratchetg...@googlemail.com>
wrote:
Hi,

I ve being thinking about playing around with bit strings but use in
some encoding problems I m considering and was trying to decide how to
implement a bit string class. Is there a library out there for doing
basic things with bit strings already is my first question?

I know that I can extend string to bit string, but is there anyway I
can force the alphabet to be restricted to 1's and 0's (or even 1, 0
and -1, as an extension to from trinary strings).

class Binary_String(String):
pass
See if you can pick which line below is impractically different to the
others:

binary: 0, 1
"trinary": -1, 0, 1
octal: 0, 1, 2, 3, 4, 5, 6, 7
decimal: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

HTH,
John

Apr 30 '07 #4

P: n/a
Thanks,

I might just move my trinary string (if I get that far) to be encoded
as 0, 1, 2, thanks for your help.
On 30 Apr 2007 11:14:10 -0700, John Machin <sj******@lexicon.netwrote:
On Apr 30, 9:53 pm, "Nathan Harmston" <ratchetg...@googlemail.com>
wrote:
Hi,

I ve being thinking about playing around with bit strings but use in
some encoding problems I m considering and was trying to decide how to
implement a bit string class. Is there a library out there for doing
basic things with bit strings already is my first question?

I know that I can extend string to bit string, but is there anyway I
can force the alphabet to be restricted to 1's and 0's (or even 1, 0
and -1, as an extension to from trinary strings).

class Binary_String(String):
pass

See if you can pick which line below is impractically different to the
others:

binary: 0, 1
"trinary": -1, 0, 1
octal: 0, 1, 2, 3, 4, 5, 6, 7
decimal: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

HTH,
John

--
http://mail.python.org/mailman/listinfo/python-list
May 1 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.