Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #36443 > unrolled thread

ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes

Started byDave Cinege <dave@cinege.com>
First post2013-01-08 12:02 -0500
Last post2013-01-10 05:04 -0800
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes Dave Cinege <dave@cinege.com> - 2013-01-08 12:02 -0500
    Re: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes Duncan Booth <duncan.booth@invalid.invalid> - 2013-01-10 11:34 +0000
      Re: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes 88888 Dihedral <dihedral88888@googlemail.com> - 2013-01-10 05:04 -0800

#36443 — ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes

FromDave Cinege <dave@cinege.com>
Date2013-01-08 12:02 -0500
SubjectANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes
Message-ID<mailman.284.1357665177.2939.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

Thesaurus: A different way to call a dictionary.

Thesaurus is a new a dictionary subclass which allows calling keys as
if they are class attributes and will search through nested objects
recursively when __getitem__ is called.

You will notice that the code is disgusting simple. However I have found that
this has completely changed the way I program in python. I've re-written some
exiting programs using Thesaurus, and often realized 15-30% code reduction.
Additionally I find the new code much easier to read.

If you find yourself programing with nested dictionaries often, fighting to 
generate output or command lines for external programs, or wish you had 
a dictionary that could act (sort of) like a class, Thesaurus may be for you.

[toc] | [next] | [standalone]


#36568

FromDuncan Booth <duncan.booth@invalid.invalid>
Date2013-01-10 11:34 +0000
Message-ID<XnsA14475B2A400Fduncanbooth@127.0.0.1>
In reply to#36443
Dave Cinege <dave@cinege.com> wrote:

> You will notice that the code is disgusting simple. However I have
> found that this has completely changed the way I program in python.
> I've re-written some exiting programs using Thesaurus, and often
> relized 15-30% code reduction. Additionally I find the new code much
> easier to read. 

And here's the same code written without your class but maintaining as 
far as possible the same structure. I find my version far easier to read 
then your's with all your spurious 'g.' 'L.' prefixes.


-----------------------------------------------------

#!python2.7
from textwrap import dedent

class Blob(object): pass

prog = Blob()
prog.VERSION = '1.0'		# But isn't this so much cleaner?
prog.NAME = 'Thesaurus'

class TestClass:
	no = 'Class'
	way = 'this'

def main ():
	tc = TestClass()
	l = ['Some', 'objects']

	# Here's how you should create output without a fight.
	print dedent('''\
		When programing python without {prog.NAME}, it is very
		easy to access your {l[1]}.
		
		{l[0]} people might say {prog.NAME} has no {tc.no}.''').format(prog=prog, l=l, tc=tc)

	if hasattr(prog, 'VERSION'):
		print 'But I challenge them to write code {tc.way} clean without it!'.format(**locals())

if __name__ == '__main__':
	main()
-----------------------------------------------------


-- 
Duncan Booth http://kupuguy.blogspot.com

[toc] | [prev] | [next] | [standalone]


#36569

From88888 Dihedral <dihedral88888@googlemail.com>
Date2013-01-10 05:04 -0800
Message-ID<bcaaeea7-90a1-4f4e-943a-8e5350b5150e@googlegroups.com>
In reply to#36568
在 2013年1月10日星期四UTC+8下午7时34分23秒,Duncan Booth写道:
> Dave Cinege <dave@cinege.com> wrote:
> 
> 
> 
> > You will notice that the code is disgusting simple. However I have
> 
> > found that this has completely changed the way I program in python.
> 
> > I've re-written some exiting programs using Thesaurus, and often
> 
> > relized 15-30% code reduction. Additionally I find the new code much
> 
> > easier to read. 
> 
> 
> 
> And here's the same code written without your class but maintaining as 
> 
> far as possible the same structure. I find my version far easier to read 
> 
> then your's with all your spurious 'g.' 'L.' prefixes.
> 
> 
> 
> 
> 
> -----------------------------------------------------
> 
> 
> 
> #!python2.7
> 
> from textwrap import dedent
> 
> 
> 
> class Blob(object): pass
> 
> 
> 
> prog = Blob()
> 
> prog.VERSION = '1.0'		# But isn't this so much cleaner?
> 
> prog.NAME = 'Thesaurus'
> 
> 
> 
> class TestClass:
> 
> 	no = 'Class'
> 
> 	way = 'this'
> 
> 
> 
> def main ():
> 
> 	tc = TestClass()
> 
> 	l = ['Some', 'objects']
> 
> 
> 
> 	# Here's how you should create output without a fight.
> 
> 	print dedent('''\
> 
> 		When programing python without {prog.NAME}, it is very
> 
> 		easy to access your {l[1]}.
> 
> 		
> 
> 		{l[0]} people might say {prog.NAME} has no {tc.no}.''').format(prog=prog, l=l, tc=tc)
> 
> 
> 
> 	if hasattr(prog, 'VERSION'):
> 
> 		print 'But I challenge them to write code {tc.way} clean without it!'.format(**locals())
> 
> 
> 
> if __name__ == '__main__':
> 
> 	main()
> 
> -----------------------------------------------------
> 
> 
> 
> 
> 
> -- 
> 
> Duncan Booth http://kupuguy.blogspot.com

An object can accquire new properties and methods
in the run time without the limitations from 
the class definition of the object which belongs to.

This is a true OOP language.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web