class Rhash proposal

P

paolo veronelli

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
 
O

Oliver Fromme

paolo veronelli said:
> 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
 
P

paolo veronelli

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
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top