473,414 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Restricting the alphabet of a string

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
4 1847
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Stefan Krah | last post by:
Hello, I am currently writing code where it is convenient to convert char to int . The conversion function relies on a character set with contiguous alphabets. int set_mesg(Key *key, char...
12
by: one | last post by:
greetings i am just wondering if some expert here can either show me how to do this or point me to the right direction (url... i want to use c# to generate a list of alphabet e.g A B C ... AA...
2
by: steve smith | last post by:
Hi I am new to this language, and still don't understand most of the concepts, I am trying to sort an arraylist by alphabet, I belive i need to use the IComparer interface, but have no idea how...
8
by: Jack Addington | last post by:
I want to scroll through the alphabet in order to scroll some data to the closest name that starts with a letter. If the user hits the H button then it should scroll to the letter closest to H. ...
9
by: booksnore | last post by:
I am writing some code to search for strings that contain every letter of the alphabet. At the moment I am using the method below to check to see if a string contains every letter of the alphabet....
31
by: Joe Smith | last post by:
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789" " " "!#%^&*()-_" "+=~\|;:\'" "\"{},.<>/\?" "\a\b\f\n\r\t\v\\" Do the above string literals comprise an alphabet for C?...
2
by: gyap88 | last post by:
Hello everyone, i m using visual studio 2005(vb) to code my project. I have a string derived from a textbox. I want to ensure that every characters within this string is either an alphabet, number...
6
by: emrodge | last post by:
I'm using Access 2007 on Windows Vista and am having some problems trying to configure a simple button. There are two tables called Clients and Contacts which are linked on clients.id and...
7
by: shashi shekhar singh | last post by:
Respected Sir, I am really tired in solving of this issue that have been arises when i would like to restrict files to access only on my Test page , here i am retriving my files in iframe in Test...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.