[TurboGears] Re: Dictionaries and kid

2006-11-14 Thread Karl Guertin

On 11/14/06, Diez B. Roggisch [EMAIL PROTECTED] wrote:
 Not from my side of course - if I didn't wanted such explanations to be
 publically readable, I'd tell them my stuffed animals ;)

I didn't think you would, but technically you have the copyright on
emails and the wiki is public domain, so I can't just transfer stuff
without asking.

--~--~-~--~~~---~--~~
 You received this message because you are subscribed to the Google Groups 
TurboGears group.
To post to this group, send email to turbogears@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~--~~~~--~~--~--~---



[TurboGears] Re: Dictionaries and kid

2006-11-14 Thread Diez B. Roggisch

On Tuesday 14 November 2006 04:26, Karl Guertin wrote:
 On 11/12/06, Diez B. Roggisch [EMAIL PROTECTED] wrote:
  The general problem you see here is known as marshalling.
  [snip]
  Diez

 Any complaints if I add this to the official docs with light editing?

Not from my side of course - if I didn't wanted such explanations to be 
publically readable, I'd tell them my stuffed animals ;)

-- 
 Diez B. Roggisch
 Developer

T  +49 (30) 443 50 99 - 27
F  +49 (30) 443 50 99 - 99
M  +49 (179) 11 75 303
E  [EMAIL PROTECTED]


 artnology GmbH

A  Milastraße 4 / D-10437 Berlin
T  +49 (30) 443 50 99 - 0
F  +49 (30) 443 50 99 - 99
E  [EMAIL PROTECTED]
I  http://www.artnology.com

--~--~-~--~~~---~--~~
 You received this message because you are subscribed to the Google Groups 
TurboGears group.
To post to this group, send email to turbogears@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~--~~~~--~~--~--~---



[TurboGears] Re: Dictionaries and kid

2006-11-13 Thread Karl Guertin

On 11/12/06, Diez B. Roggisch [EMAIL PROTECTED] wrote:
 The general problem you see here is known as marshalling.
 [snip]
 Diez

Any complaints if I add this to the official docs with light editing?

--~--~-~--~~~---~--~~
 You received this message because you are subscribed to the Google Groups 
TurboGears group.
To post to this group, send email to turbogears@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~--~~~~--~~--~--~---



[TurboGears] Re: Dictionaries and kid

2006-11-13 Thread iain duncan

On Mon, 2006-13-11 at 22:26 -0500, Karl Guertin wrote:
 On 11/12/06, Diez B. Roggisch [EMAIL PROTECTED] wrote:
  The general problem you see here is known as marshalling.
  [snip]
  Diez
 
 Any complaints if I add this to the official docs with light editing?

I think it's a great idea. If anyone knows offsite links to further
resources on the same topic, those would be nice to have at the bottom.

Iain



--~--~-~--~~~---~--~~
 You received this message because you are subscribed to the Google Groups 
TurboGears group.
To post to this group, send email to turbogears@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~--~~~~--~~--~--~---



[TurboGears] Re: Dictionaries and kid

2006-11-12 Thread Diez B. Roggisch

iain duncan schrieb:
 
 Most solid doc on this topic is:

 http://docs.turbogears.org/1.0/GettingStarted/JSON

 You can explicitly control how objects are converted to json using the
 jsonify decorator. There's an example in the json.py file created by
 quickstart. I have not fully explored jsonify (it was added during one
 of my periodic breaks from TG), so I haven't written a reference
 jsonify doc.
 
 What about docs or books ( non-gears too I mean ) on what the best way
 to do this kind of thing is, as far as the architecture and object
 implementation?
 
 I finally got the stuff working ok with an implementation of the cart as
 a session object that sub classes list. This *seems* to make it's json
 behaviour the closest to how I am using it in python code and js code,
 but I'm sure it's not the most elegant approach.

The general problem you see here is known as marshalling. It means to 
define ways how data is to be represented as a stream of bytes to move 
it between two processes - regardless of where these processes live, on 
one machine, or two connected via a network, or even separaded by time - 
  then its about persistence.

The process of taking in-process data and producing a stream of bytes is 
marshalling, the inverse is called unmarshalling.

All inter process communication (IPC) standards like CORBA, COM, XMLRPC, 
RMI, SOAP and so on minimally define a marshalling standard.

JSON is such a standard too, albeit _not_ a IPC mechanism. And it lacks 
quite a few things, like encoding specification, for which it relies on 
the transport protocol HTTP. But it is simple and powerful, especially 
in the context of javascript-to-server communication.

Bob's SimpleJson knows how to produce correct JSON-formatted data from 
python's built in primitives and standard collection types. Now this 
leaves us with the need to convert our self-defined classes to a bunch 
of objects that are understood by SimpleJson.

But to do so, it is not necessary to subclass from the builtins, as you 
seem to assume!

There are two ways to accomplish this:

1) make an object perform the work itself. This is done in TG via the 
means of the __json__-method. If an object has such a method defined, 
and is returned from a TG controller, the method will be called to 
obtain a jsonable representation. So you could accomplish what you need 
like this:

class ShoppingCart(object):

 def __json__(self):
 return {
 number_of_items : len(self.items),
 items : self.items
 }


This is certainly way cleaner than tayloring the implementation so that 
it is implicitly representable.

2) Use a external method/code that gets an object and inspects it to 
create a json-representable object. This is called a decortor or visitor 
pattern - I'm actually not sure about these pattern stuff, it's all kind 
of blurry and trivial to me, but that might well be my ignorance.

In TG, this is accomplished using TurboJson. You should investigate into 
the jsonify-decorator and related code. It is brought to use for your 
projject in a file called json.py in your projects primary package 
directory. If you use identity support, it will be prepopulated with 
code like this:

@jsonify.when('isinstance(obj, User)')
def jsonify_user(obj):
 result = jsonify_sqlobject( obj )
 del result['password']
 result[groups] = [g.group_name for g in obj.groups]
 result[permissions] = [p.permission_name for p in obj.permissions]
 return result


As you can see, there is a test given as a string that will be evaluated 
against an unknown object to see if the decorated method is to be 
invoked to return the json-compatible representation of the object. If 
so, it is invoked with the object in question.

With these two approaches, you can fine-grained control, what and how to 
  serialize using JSON. I personally prefer the second approach, as the 
first method makes an object know something about its usage that I 
consider to specific. A e.g. model object could be used in a context 
that is totally unrelated to web-programming - and thus it shouldn't 
know how to represent itself in JSON.

Diez

--~--~-~--~~~---~--~~
 You received this message because you are subscribed to the Google Groups 
TurboGears group.
To post to this group, send email to turbogears@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~--~~~~--~~--~--~---



[TurboGears] Re: Dictionaries and kid

2006-11-12 Thread iain duncan

Diez, thank you for taking the time to write that. I think it would be
useful to have some of what you've written added to the json doc. =)

Iain

  Most solid doc on this topic is:
 
  http://docs.turbogears.org/1.0/GettingStarted/JSON
 
  You can explicitly control how objects are converted to json using the
  jsonify decorator. There's an example in the json.py file created by
  quickstart. I have not fully explored jsonify (it was added during one
  of my periodic breaks from TG), so I haven't written a reference
  jsonify doc.
  
  What about docs or books ( non-gears too I mean ) on what the best way
  to do this kind of thing is, as far as the architecture and object
  implementation?
  
  I finally got the stuff working ok with an implementation of the cart as
  a session object that sub classes list. This *seems* to make it's json
  behaviour the closest to how I am using it in python code and js code,
  but I'm sure it's not the most elegant approach.
 
 The general problem you see here is known as marshalling. It means to 
 define ways how data is to be represented as a stream of bytes to move 
 it between two processes - regardless of where these processes live, on 
 one machine, or two connected via a network, or even separaded by time - 
   then its about persistence.
 
 The process of taking in-process data and producing a stream of bytes is 
 marshalling, the inverse is called unmarshalling.
 
 All inter process communication (IPC) standards like CORBA, COM, XMLRPC, 
 RMI, SOAP and so on minimally define a marshalling standard.
 
 JSON is such a standard too, albeit _not_ a IPC mechanism. And it lacks 
 quite a few things, like encoding specification, for which it relies on 
 the transport protocol HTTP. But it is simple and powerful, especially 
 in the context of javascript-to-server communication.
 
 Bob's SimpleJson knows how to produce correct JSON-formatted data from 
 python's built in primitives and standard collection types. Now this 
 leaves us with the need to convert our self-defined classes to a bunch 
 of objects that are understood by SimpleJson.
 
 But to do so, it is not necessary to subclass from the builtins, as you 
 seem to assume!
 
 There are two ways to accomplish this:
 
 1) make an object perform the work itself. This is done in TG via the 
 means of the __json__-method. If an object has such a method defined, 
 and is returned from a TG controller, the method will be called to 
 obtain a jsonable representation. So you could accomplish what you need 
 like this:
 
 class ShoppingCart(object):
 
  def __json__(self):
  return {
  number_of_items : len(self.items),
  items : self.items
  }
 
 
 This is certainly way cleaner than tayloring the implementation so that 
 it is implicitly representable.
 
 2) Use a external method/code that gets an object and inspects it to 
 create a json-representable object. This is called a decortor or visitor 
 pattern - I'm actually not sure about these pattern stuff, it's all kind 
 of blurry and trivial to me, but that might well be my ignorance.
 
 In TG, this is accomplished using TurboJson. You should investigate into 
 the jsonify-decorator and related code. It is brought to use for your 
 projject in a file called json.py in your projects primary package 
 directory. If you use identity support, it will be prepopulated with 
 code like this:
 
 @jsonify.when('isinstance(obj, User)')
 def jsonify_user(obj):
  result = jsonify_sqlobject( obj )
  del result['password']
  result[groups] = [g.group_name for g in obj.groups]
  result[permissions] = [p.permission_name for p in obj.permissions]
  return result
 
 
 As you can see, there is a test given as a string that will be evaluated 
 against an unknown object to see if the decorated method is to be 
 invoked to return the json-compatible representation of the object. If 
 so, it is invoked with the object in question.
 
 With these two approaches, you can fine-grained control, what and how to 
   serialize using JSON. I personally prefer the second approach, as the 
 first method makes an object know something about its usage that I 
 consider to specific. A e.g. model object could be used in a context 
 that is totally unrelated to web-programming - and thus it shouldn't 
 know how to represent itself in JSON.
 
 Diez
 
  


--~--~-~--~~~---~--~~
 You received this message because you are subscribed to the Google Groups 
TurboGears group.
To post to this group, send email to turbogears@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~--~~~~--~~--~--~---



[TurboGears] Re: Dictionaries and kid

2006-11-11 Thread Bob Ippolito

On 11/11/06, iain duncan [EMAIL PROTECTED] wrote:

 When we send stuff to kid, it gets them as a dictionary. What if we want
 to send a dictionary in the dictionary? I am able to get kid to
 dereferrence the first level ok,

 ie, the items in the cart are themselves dictionaries, with the name as
 the key.

 for item in cart.item
 ${item}   -- key name of my sub dictionary,
 ${item['quant']}   -- error
 ${item.quant}   -- error

 Is there a way to have nested dictionaries passed to kid or will I need
 to redesign the container?

That doesn't have anything to do with kid, but how dictionaries work.
Iterating over a dictionary gives you its keys. If you want the (key,
value) pairs then use for key, value in cart.item.iteritems().

-bob

--~--~-~--~~~---~--~~
 You received this message because you are subscribed to the Google Groups 
TurboGears group.
To post to this group, send email to turbogears@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~--~~~~--~~--~--~---



[TurboGears] Re: Dictionaries and kid

2006-11-11 Thread iain duncan

On Sat, 2006-11-11 at 16:38 -0800, Bob Ippolito wrote:
 On 11/11/06, iain duncan [EMAIL PROTECTED] wrote:
 
  When we send stuff to kid, it gets them as a dictionary. What if we want
  to send a dictionary in the dictionary? I am able to get kid to
  dereferrence the first level ok,
 
  ie, the items in the cart are themselves dictionaries, with the name as
  the key.
 
  for item in cart.item
  ${item}   -- key name of my sub dictionary,
  ${item['quant']}   -- error
  ${item.quant}   -- error
 
  Is there a way to have nested dictionaries passed to kid or will I need
  to redesign the container?
 
 That doesn't have anything to do with kid, but how dictionaries work.
 Iterating over a dictionary gives you its keys. If you want the (key,
 value) pairs then use for key, value in cart.item.iteritems().

Ah, because in kid, I'm iterating in Python ( which I should have
known!) I will confess my head has been spinning when switching between
js and python and js turned pythony. :/

Iain


--~--~-~--~~~---~--~~
 You received this message because you are subscribed to the Google Groups 
TurboGears group.
To post to this group, send email to turbogears@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~--~~~~--~~--~--~---



[TurboGears] Re: Dictionaries and kid

2006-11-11 Thread Bob Ippolito

On 11/11/06, iain duncan [EMAIL PROTECTED] wrote:

 On Sat, 2006-11-11 at 16:38 -0800, Bob Ippolito wrote:
  On 11/11/06, iain duncan [EMAIL PROTECTED] wrote:
  
   When we send stuff to kid, it gets them as a dictionary. What if we want
   to send a dictionary in the dictionary? I am able to get kid to
   dereferrence the first level ok,
  
   ie, the items in the cart are themselves dictionaries, with the name as
   the key.
  
   for item in cart.item
   ${item}   -- key name of my sub dictionary,
   ${item['quant']}   -- error
   ${item.quant}   -- error
  
   Is there a way to have nested dictionaries passed to kid or will I need
   to redesign the container?
 
  That doesn't have anything to do with kid, but how dictionaries work.
  Iterating over a dictionary gives you its keys. If you want the (key,
  value) pairs then use for key, value in cart.item.iteritems().

 Ah, because in kid, I'm iterating in Python ( which I should have
 known!) I will confess my head has been spinning when switching between
 js and python and js turned pythony. :/

Well, JavaScript iterates over objects the exact same way that Python
iterates over dicts ;)

-bob

--~--~-~--~~~---~--~~
 You received this message because you are subscribed to the Google Groups 
TurboGears group.
To post to this group, send email to turbogears@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~--~~~~--~~--~--~---



[TurboGears] Re: Dictionaries and kid

2006-11-11 Thread iain duncan


 
 Well, JavaScript iterates over objects the exact same way that Python
 iterates over dicts ;)

Does that mean that if you want to use ajax to keep essentially mirrors
of your object on the server and the client end, a python dictionary is
the best way to go? Sub class a dictionary?

Thanks for all the help Bob,
Iain



--~--~-~--~~~---~--~~
 You received this message because you are subscribed to the Google Groups 
TurboGears group.
To post to this group, send email to turbogears@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~--~~~~--~~--~--~---



[TurboGears] Re: Dictionaries and kid

2006-11-11 Thread Bob Ippolito

On 11/11/06, iain duncan [EMAIL PROTECTED] wrote:


 
  Well, JavaScript iterates over objects the exact same way that Python
  iterates over dicts ;)

 Does that mean that if you want to use ajax to keep essentially mirrors
 of your object on the server and the client end, a python dictionary is
 the best way to go? Sub class a dictionary?

That's pretty much irrelevant. Subclassing dict is generally a bad idea though.

-bob

--~--~-~--~~~---~--~~
 You received this message because you are subscribed to the Google Groups 
TurboGears group.
To post to this group, send email to turbogears@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~--~~~~--~~--~--~---



[TurboGears] Re: Dictionaries and kid

2006-11-11 Thread iain duncan

   Well, JavaScript iterates over objects the exact same way that Python
   iterates over dicts ;)
 
  Does that mean that if you want to use ajax to keep essentially mirrors
  of your object on the server and the client end, a python dictionary is
  the best way to go? Sub class a dictionary?
 
 That's pretty much irrelevant. Subclassing dict is generally a bad idea 
 though.

I guess my confusion is with how we return things from the controller,
how they get to become json, and how we should take them apart. Any
pointers on good docs on this topic?

Thanks again
Iain



--~--~-~--~~~---~--~~
 You received this message because you are subscribed to the Google Groups 
TurboGears group.
To post to this group, send email to turbogears@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~--~~~~--~~--~--~---



[TurboGears] Re: Dictionaries and kid

2006-11-11 Thread Karl Guertin

On 11/11/06, iain duncan [EMAIL PROTECTED] wrote:

Well, JavaScript iterates over objects the exact same way that Python
iterates over dicts ;)
  
   Does that mean that if you want to use ajax to keep essentially mirrors
   of your object on the server and the client end, a python dictionary is
   the best way to go? Sub class a dictionary?
 
  That's pretty much irrelevant. Subclassing dict is generally a bad idea 
  though.

 I guess my confusion is with how we return things from the controller,
 how they get to become json, and how we should take them apart. Any
 pointers on good docs on this topic?

Most solid doc on this topic is:

http://docs.turbogears.org/1.0/GettingStarted/JSON

You can explicitly control how objects are converted to json using the
jsonify decorator. There's an example in the json.py file created by
quickstart. I have not fully explored jsonify (it was added during one
of my periodic breaks from TG), so I haven't written a reference
jsonify doc.

--~--~-~--~~~---~--~~
 You received this message because you are subscribed to the Google Groups 
TurboGears group.
To post to this group, send email to turbogears@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~--~~~~--~~--~--~---



[TurboGears] Re: Dictionaries and kid

2006-11-11 Thread iain duncan


 Most solid doc on this topic is:
 
 http://docs.turbogears.org/1.0/GettingStarted/JSON
 
 You can explicitly control how objects are converted to json using the
 jsonify decorator. There's an example in the json.py file created by
 quickstart. I have not fully explored jsonify (it was added during one
 of my periodic breaks from TG), so I haven't written a reference
 jsonify doc.

What about docs or books ( non-gears too I mean ) on what the best way
to do this kind of thing is, as far as the architecture and object
implementation?

I finally got the stuff working ok with an implementation of the cart as
a session object that sub classes list. This *seems* to make it's json
behaviour the closest to how I am using it in python code and js code,
but I'm sure it's not the most elegant approach.

Each item type is an entry in the list that maps to a product in my
producst table, and each entry in the list holds a dictionary with

[ {name:product_name, quant:quant, subtotal:subtotal} ]

Then the cart class additionally has three extra fields for total number
of objects, total cost, and cart message, and adding, clearing, removing
map from js functions to python cleanly. ( js function add hits
controller json method add, which pulls the cart out of a session hat
and calls cart.add )

Anyone know if this kind of stuff is covered in the book? ( Not that the
local stores have copies yet of course! Gotta love the book biz. )

Unfortunately, I need to get this working soon, so I'm going to run with
what I have now, but I think the project might make for a nice tutorial
down the road if anyone is interested in looking over the code later and
telling me how to do it better. Writing it all up would be a nice way
for me to contribute back after all my pestering, and it makes for a
pretty snazzy cart that should degrade well too.

Iain



--~--~-~--~~~---~--~~
 You received this message because you are subscribed to the Google Groups 
TurboGears group.
To post to this group, send email to turbogears@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~--~~~~--~~--~--~---