470,594 Members | 1,444 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,594 developers. It's quick & easy.

class Rhash proposal

Writing software in Python for me it means work with dictionaries.
They are next to humans head.They make no assumption on the names of
variables ,mainly they leave the user
to set his program with the names he like.They track me next to the
user.......etc

Not to bore I ask opinions abuot a very simple new class Rhash :
The name to read as ErHash means recursive hash:

Its implementation:

class Rhash(dict):
def __getitem__(rhash,key):
if key not in rhash:
rhash[key]=Rhash()
return dict.__getitem__(key)

Rhash lets us say:

Human=Rhash()
I=Rhash()

Human['isKindOf']['beast']=None

I['like']['mixing'][('smoking','thinking')]=True
and have no Key Error and don't use the unreadable .setdefault(key,{})
which anyway oblige us to
program the depth in advance.

Would it be nice to have it to help directly dict?

Thanks for comments Paolino

--
.....lotta dura per la verdura

Jul 18 '05 #1
2 1235
paolo veronelli <pa*************@yahoo.it> wrote:
class Rhash(dict):
def __getitem__(rhash,key):
if key not in rhash:
rhash[key]=Rhash()
return dict.__getitem__(key)

Rhash lets us say:

Human=Rhash()
I=Rhash()

Human['isKindOf']['beast']=None

I['like']['mixing'][('smoking','thinking')]=True


You could simply write the indices into a sequence (tuple),
instead of stacking multiple dimensions of indices:

Human = {}
I = {}

Human[('isKindOf', 'beast')] = None

I[('like', 'mixing', ('smoking', 'thinking'))] = True

That would be easier and more efficient. Can you provide
a real-world example where your Rhash approach offers a
real advantage?

Best regards
Oliver

--
Oliver Fromme, Konrad-Celtis-Str. 72, 81369 Munich, Germany

``All that we see or seem is just a dream within a dream.''
(E. A. Poe)
Jul 18 '05 #2

The strenght of dictionaries is the key research (obviously)
The possibility of transparent nesting given by Rhashes pays off in
building tree of orederd informations.Ordered so that you can easily find
and store informations in it.

It's nothing more than a simple but useful database which you dedicate to
your views of the world you are trying to simulate.

The focus point in using dictionaries is that we want to deal with complex
informations which cannot be simplified in a record (an instance of a
class).
It looks like every python object is nothing more than a dictionary,which
has constraints on nestings.This paradigma can be broken by using Rhashes
which have no constraints.

The other side of the story,is that keys have no specific meanings and the
code has to take in account some topological propreties of the Rhash
layout to work with it.

Particularly,the meaning of every level of nesting has to be known to the
code.

In my latest job the Rhash 'world' has the resources as first level,the
name of the resources.
The second is the predicate level,so querying it gives all the predicates
applicable to the resource choosen.
The other levels,are polymorphic and parseable only by pieces of code
binded to the predicate level......

Naturally using nested dictionaries is not very useful if the code
performs mere calculations,where the input is primitive.

But if we deal with large and polimorphic informations which want to drive
the code and we are reading __metaclass__ looking for light,they can help.
The RDF reasoner I'm buiding has only 3 classes all derived from Rhash
with some other Rhashes inside.It works and,mostly ,the code is readable
(?).
m=Rhash()

m['Lerat']['isa']['human']=None
m['Lerat']['isa']['walker']=None
m['Lerat']['likes']['music']['blues']=None
m['Lerat']['likes']['music']['ambient']=None
m['Lerat']['sleeps']=None

print m
{'Lerat': {'isa': {'walker': None, 'human': None}, 'likes': {'music':
{'blues': None, 'ambient': None}}, 'sleeps': None}}
This is a better example maybe.
Thanks for comments Paolino


--
.....lotta dura per la verdura

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Carlos Ribeiro | last post: by
166 posts views Thread by Graham | last post: by
38 posts views Thread by JTL | last post: by
10 posts views Thread by =?iso-8859-2?B?SmFuIFJpbmdvuQ==?= | last post: by
49 posts views Thread by Ben Voigt [C++ MVP] | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.