473,698 Members | 2,393 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1659
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.co m>
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__(se lf, key):
.... return key.lower() in self._dict
.... def __getitem__(sel f, key):
.... return self._dict[key.lower()]
.... def __setitem__(sel f, 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_re ad' 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.comescri bió:
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__(sel f, 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__(sel f, 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.comescri bió:
>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
occurrenc e.
>... def __setitem__(sel f, 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__(sel f, 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.comescri bió:
>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
occurrenc e.
>... def __setitem__(sel f, 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__(sel f, 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3586
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 respectively In the sample code you see that I have class Item and class Dict class Dict contains a dictionary called items. The items dictionary will contain instances of Item that are keyed off of the Item name. In __main__ I create two...
4
2870
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 http://starship.skyport.net/crew/amk/regex/regex-to-re.html http://www.python.org/doc/howto/regex-to-re/ http://www.amk.ca/python/howto/regex-to-re/ Thanks in advance.
5
2353
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. Or how can I replace the html &entities; in a string "blablabla&amp;blablabal&amp;balbalbal" with the chars they mean using re.sub? I found out they are stored in an dict . I though about this functionality:
4
2516
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 want to # init a class from that dictionary. # However, I don't want to specify the # dictionary gets by hand # since it is error prone.
1
9250
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 DataBinding accepts as a data source either an IList or an IListSource at System.Windows.Forms.ListControl.set_DataSource(Object value)
3
7547
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 to do so. Could anyone provide me with source code for the same? Thanks in Advance. Regards, --Jaffar
3
1421
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 UserDict class CaseInsensitiveDict(dict, UserDict.DictMixin): def __init__(self, *args, **kwargs): self.orig = {}
1
2274
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) Dictionary<string, Dictionary<string, string>> d = new Dictionary<string, Dictionary<string, string>>(); Now I am using GetDictionaryType Function where I an sending a dictionary object. In GetDictionaryType() I want to find out which dictionary...
0
8676
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9161
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9029
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8897
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7732
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6522
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.