473,241 Members | 1,761 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,241 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 1637
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: none | last post by:
or is it just me? I am having a problem with using a dictionary as an attribute of a class. This happens in python 1.5.2 and 2.2.2 which I am accessing through pythonwin builds 150 and 148...
4
by: Josef Sachs | last post by:
Is Andrew Kuchling's regex-to-re HOWTO available anywhere? I've found the following (dead) links on various Web pages: http://py-howto.sourceforge.net/regex-to-re/regex-to-re.html...
5
by: Lukas Holcik | last post by:
Hi everyone! How can I simply search text for regexps (lets say <a href="(.*?)">(.*?)</a>) and save all URLs(1) and link contents(2) in a dictionary { name : URL}? In a single pass if it could....
4
by: brianobush | last post by:
# # My problem is that I want to create a # class, but the variables aren't known # all at once. So, I use a dictionary to # store the values in temporarily. # Then when I have a complete set, I...
1
by: john wright | last post by:
I have a dictionary oject I created and I want to bind a listbox to it. I am including the code for the dictionary object. Here is the error I am getting: "System.Exception: Complex...
3
by: jaffar.kazi | last post by:
Hi All, I have a VB COM component that has an object of type Scripting.Dictionary. Each element of this again has Scripting.Dictionary type members. I need to access this in C#, and am not able...
3
by: Phoe6 | last post by:
I have a requirement for using caseless dict. I searched the web for many different implementations and found one snippet which was implemented in minimal and useful way. ############# import...
1
by: sachin2 | last post by:
I am using 3 types of dictionaries. 1) Dictionary<string, string > d = new Dictionary<string, string>(); 2) Dictionary<string, List<string>> d = new Dictionary<string, List<string>>(); 3)...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.