471,624 Members | 1,794 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

caseless dictionary howto ?

hello,

I need to search a piece of text and make all words that are equal
(except their case) also equal in their case, based on the first occurrence.
So I'm using a dictionary to store names and attributes of objects.
As as I need to search on the caseless name (so I've choosen lowercase),
My dictionairy looks like this:

self.procs [ "serial_hw_read" ] = ( "Serial_HW_Read", "F", "++", T)

Is this really a good solution,
or are there better ways ?

thanks,
Stef Mientki
Jun 19 '07 #1
12 1576
On 6/19/07, Stef Mientki <S.**************@mailbox.kun.nlwrote:
hello,

I need to search a piece of text and make all words that are equal
(except their case) also equal in their case, based on the first occurrence.
So I'm using a dictionary to store names and attributes of objects.
As as I need to search on the caseless name (so I've choosen lowercase),
My dictionairy looks like this:

self.procs [ "serial_hw_read" ] = ( "Serial_HW_Read", "F", "++", T)

Is this really a good solution,
or are there better ways ?
Since you want an almost dictionary, you should create a new class
that inherits from dict and overloads the methods that you want to
change.

--
Evan Klitzke <ev**@yelp.com>
Jun 19 '07 #2
Evan Klitzke wrote:
On 6/19/07, Stef Mientki <S.**************@mailbox.kun.nlwrote:
>hello,

I need to search a piece of text and make all words that are equal
(except their case) also equal in their case, based on the first
occurrence.
So I'm using a dictionary to store names and attributes of objects.
As as I need to search on the caseless name (so I've choosen lowercase),
My dictionairy looks like this:

self.procs [ "serial_hw_read" ] = ( "Serial_HW_Read", "F", "++", T)

Is this really a good solution,
or are there better ways ?

Since you want an almost dictionary, you should create a new class
that inherits from dict and overloads the methods that you want to
change.
thanks,
but still one question.

Suppose I succeed in creating my own type, derived from dictionary,
I would be able to add/set a key like this:

self.procs [ "Serial_HW_Read" ] = ( "F", "++", T)

and now I can query the dictionary by

self.procs.has_key ( "serial_hw_read")

but how do I get the original string "Serial_HW_Read" back ?

thanks,
Stef Mientki
Jun 19 '07 #3
Stef Mientki wrote:
Evan Klitzke wrote:
>On 6/19/07, Stef Mientki <S.**************@mailbox.kun.nlwrote:
>>hello,

I need to search a piece of text and make all words that are equal
(except their case) also equal in their case, based on the first
occurrence.
So I'm using a dictionary to store names and attributes of objects.
As as I need to search on the caseless name (so I've choosen lowercase),
My dictionairy looks like this:

self.procs [ "serial_hw_read" ] = ( "Serial_HW_Read", "F",
"++", T)

Is this really a good solution,
or are there better ways ?

Since you want an almost dictionary, you should create a new class
that inherits from dict and overloads the methods that you want to
change.

Suppose I succeed in creating my own type, derived from dictionary,
I would be able to add/set a key like this:

self.procs [ "Serial_HW_Read" ] = ( "F", "++", T)

and now I can query the dictionary by

self.procs.has_key ( "serial_hw_read")

but how do I get the original string "Serial_HW_Read" back ?
I probably wouldn't inherit from dict -- you're going to need to
redefine most of the methods anyway. I'd do something like::
>>class D(object):
.... def __init__(self):
.... self._dict = {}
.... self._original_keys = {}
.... def __iter__(self):
.... for key in self._dict:
.... yield self._original_keys[key]
.... def __contains__(self, key):
.... return key.lower() in self._dict
.... def __getitem__(self, key):
.... return self._dict[key.lower()]
.... def __setitem__(self, key, value):
.... self._dict[key.lower()] = value
.... if key not in self._original_keys:
.... self._original_keys[key.lower()] = key
....
>>procs = D()
procs['Serial_HW_Read'] = 'F', '++', 'T'
'serial_hw_read' in procs
True
>>procs['SErial_Hw_reAD']
('F', '++', 'T')
>>list(procs)
['Serial_HW_Read']

Note that because I store the first form encountered for every key, I
can always use the lower-case version to retrieve the "original string".

STeVe
Jun 19 '07 #4
En Tue, 19 Jun 2007 19:40:10 -0300, Steven Bethard
<st************@gmail.comescribió:
Stef Mientki wrote:
>Evan Klitzke wrote:
>>On 6/19/07, Stef Mientki <S.**************@mailbox.kun.nlwrote:

I need to search a piece of text and make all words that are equal
(except their case) also equal in their case, based on the first
occurrence.
... def __setitem__(self, key, value):
... self._dict[key.lower()] = value
... if key not in self._original_keys:
... self._original_keys[key.lower()] = key
...

Note that because I store the first form encountered for every key, I
can always use the lower-case version to retrieve the "original string".
As written, it stores the last used spelling; a small change is required
to get the intended behavior:
... def __setitem__(self, key, value):
... self._dict[key.lower()] = value
... if key.lower() not in self._original_keys:
... self._original_keys[key.lower()] = key
--
Gabriel Genellina

Jun 20 '07 #5
Gabriel Genellina wrote:
En Tue, 19 Jun 2007 19:40:10 -0300, Steven Bethard
<st************@gmail.comescribió:
>Stef Mientki wrote:
>>Evan Klitzke wrote:
On 6/19/07, Stef Mientki <S.**************@mailbox.kun.nlwrote:
>
I need to search a piece of text and make all words that are equal
(except their case) also equal in their case, based on the first
occurrence.
>... def __setitem__(self, key, value):
... self._dict[key.lower()] = value
... if key not in self._original_keys:
... self._original_keys[key.lower()] = key
...

Note that because I store the first form encountered for every key, I
can always use the lower-case version to retrieve the "original string".

As written, it stores the last used spelling; a small change is required
to get the intended behavior:
>... def __setitem__(self, key, value):
... self._dict[key.lower()] = value
... if key.lower() not in self._original_keys:
... self._original_keys[key.lower()] = key
Good catch. Thanks!

Steve
Jun 20 '07 #6
Gabriel Genellina wrote:
En Tue, 19 Jun 2007 19:40:10 -0300, Steven Bethard
<st************@gmail.comescribió:
>Stef Mientki wrote:
>>Evan Klitzke wrote:
On 6/19/07, Stef Mientki <S.**************@mailbox.kun.nlwrote:
>
I need to search a piece of text and make all words that are equal
(except their case) also equal in their case, based on the first
occurrence.
>... def __setitem__(self, key, value):
... self._dict[key.lower()] = value
... if key not in self._original_keys:
... self._original_keys[key.lower()] = key
...

Note that because I store the first form encountered for every key, I
can always use the lower-case version to retrieve the "original string".

As written, it stores the last used spelling; a small change is
required to get the intended behavior:
>... def __setitem__(self, key, value):
... self._dict[key.lower()] = value
... if key.lower() not in self._original_keys:
... self._original_keys[key.lower()] = key

--Gabriel Genellina
thanks guys,
I'll think a while which solution I'll choose.

cheers,
Stef Mientki
Jun 20 '07 #7
Stef Mientki wrote:
I need to search a piece of text and make all words that are equal
(except their case) also equal in their case, based on the first
occurrence.
So I'm using a dictionary to store names and attributes of objects.
As as I need to search on the caseless name (so I've choosen lowercase),
My dictionairy looks like this:

self.procs [ "serial_hw_read" ] = ( "Serial_HW_Read", "F", "++", T)
I have no idea what the "F", "++", "T" means at the end (if it's some kind of
flags or attributes, maybe a class to hold them would look better), but that's
a good solution to the problem IMHO.

Stefan
Jun 20 '07 #8
Stefan Behnel wrote:
Stef Mientki wrote:
>I need to search a piece of text and make all words that are equal
(except their case) also equal in their case, based on the first
occurrence.
So I'm using a dictionary to store names and attributes of objects.
As as I need to search on the caseless name (so I've choosen lowercase),
My dictionairy looks like this:

self.procs [ "serial_hw_read" ] = ( "Serial_HW_Read", "F", "++", T)

I have no idea what the "F", "++", "T" means at the end (if it's some kind of
flags or attributes,
This makes part of a simulation of a JAL (a Pascal like language for
micros),
and this dictionary builds up the namespace,
used in translation JAL to pure Python.

Serial_HW_Read = the name of a function
"F" = the type of that function (procedure / function / pseudo variable
/ interrupt /..)
"++" = the direction of each parameters, (input / output / input+output )
T = a tupple, containing the type of each of the parameters (bit / byte/
sbyte / word / ...)
maybe a class to hold them would look better),
maybe, but no human is looking at it ;-)
but that's
a good solution to the problem IMHO.
Seeing the other solution,
I indeed tend to stick to this solution,
because it's the most simple one
(certainly if you realize that the dictionary is generated by the
program itself)

cheers,
Stef Mientki
Stefan
Jun 20 '07 #9
On Wed, 2007-06-20 at 11:14 +0200, stef wrote:
Stefan Behnel wrote:
Serial_HW_Read = the name of a function
"F" = the type of that function (procedure / function / pseudo variable
/ interrupt /..)
"++" = the direction of each parameters, (input / output / input+output )
T = a tupple, containing the type of each of the parameters (bit / byte/
sbyte / word / ...)
maybe a class to hold them would look better),
maybe, but no human is looking at it ;-)
But humans might look at the code that looks at the thing.
thing.func_type is self-documenting, thing[0] is not.

--
Carsten Haese
http://informixdb.sourceforge.net
Jun 20 '07 #10
Carsten Haese wrote:
On Wed, 2007-06-20 at 11:14 +0200, stef wrote:
>Stefan Behnel wrote:
Serial_HW_Read = the name of a function
"F" = the type of that function (procedure / function / pseudo variable
/ interrupt /..)
"++" = the direction of each parameters, (input / output / input+output )
T = a tupple, containing the type of each of the parameters (bit / byte/
sbyte / word / ...)
>> maybe a class to hold them would look better),
maybe, but no human is looking at it ;-)

But humans might look at the code that looks at the thing.
thing.func_type is self-documenting, thing[0] is not.
I think you might be right,
but for a one-time/one-programmer program,
I think the documentation will be good enough.

cheers,
Stef Mientki
Jun 20 '07 #11
stef wrote:
I think you might be right,
but for a one-time/one-programmer program,
I think the documentation will be good enough.
Famous last words.

Stefan :)
Jun 20 '07 #12
En Wed, 20 Jun 2007 11:11:52 -0300, stef <s.*******@id.umcn.nlescribió:
Carsten Haese wrote:
>On Wed, 2007-06-20 at 11:14 +0200, stef wrote:
>>Stefan Behnel wrote:
>>Serial_HW_Read = the name of a function
"F" = the type of that function (procedure / function / pseudo variable
/ interrupt /..)
"++" = the direction of each parameters, (input / output /
input+output )
T = a tupple, containing the type of each of the parameters (bit /
byte/
sbyte / word / ...)

maybe a class to hold them would look better),

maybe, but no human is looking at it ;-)

But humans might look at the code that looks at the thing.
thing.func_type is self-documenting, thing[0] is not.
I think you might be right,
but for a one-time/one-programmer program,
I think the documentation will be good enough.
The best way would be to mix both things, using a NamedTuple (available on
Python 2.6 or from
http://aspn.activestate.com/ASPN/Coo.../Recipe/500261)

--
Gabriel Genellina

Jun 20 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by none | last post: by
4 posts views Thread by Josef Sachs | last post: by
5 posts views Thread by Lukas Holcik | last post: by
4 posts views Thread by brianobush | last post: by
1 post views Thread by john wright | last post: by
3 posts views Thread by jaffar.kazi | last post: by
3 posts views Thread by Phoe6 | last post: by
reply views Thread by leo001 | last post: by
1 post views Thread by ZEDKYRIE | last post: by

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.