caseless dictionary howto ?

S

Stef Mientki

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
 
E

Evan Klitzke

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.
 
S

Stef Mientki

Evan said:
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
 
S

Steven Bethard

Stef said:
Evan said:
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::
.... 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
 
G

Gabriel Genellina

En Tue, 19 Jun 2007 19:40:10 -0300, Steven Bethard
... 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
 
S

Steven Bethard

Gabriel said:
En Tue, 19 Jun 2007 19:40:10 -0300, Steven Bethard
... 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
 
S

stef

Gabriel said:
En Tue, 19 Jun 2007 19:40:10 -0300, Steven Bethard
... 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
 
S

Stefan Behnel

Stef said:
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
 
S

stef

Stefan said:
Stef said:
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
 
C

Carsten Haese

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, 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.
 
S

stef

Carsten said:
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, 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
 
S

Stefan Behnel

stef said:
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 :)
 
G

Gabriel Genellina

Carsten said:
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/Cookbook/Python/Recipe/500261)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top