unable to insert tuple object

1,032 views
Skip to first unread message

vkuznet

unread,
Aug 26, 2009, 11:38:14 AM8/26/09
to mongodb-user
Hi,
working with pymongo, I'm unable to insert a tuple object. Here is a
session:

In [443]: monitor={'foo':12.12, 'boo' : (1,1)}

In [444]: cache.insert(monitor)
---------------------------------------------------------------------------
InvalidDocument Traceback (most recent call
last)

/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/
python2.6/site-packages/pymongo/collection.pyc in insert(self,
doc_or_docs, manipulate, safe, check_keys)
180 docs = [self.__database._fix_incoming(doc, self)
for doc in docs]
181
--> 182 data = [bson.BSON.from_dict(doc, check_keys) for doc
in docs]
183 self._send_message(2002, "".join(data))
184

/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/
python2.6/site-packages/pymongo/bson.pyc in from_dict(cls, dict,
check_keys)
517 raising `pymongo.errors.InvalidName` in either
case
518 """
--> 519 return cls(_dict_to_bson(dict, check_keys))
520 from_dict = classmethod(from_dict)
521

InvalidDocument: Cannot encode object: (1, 1)

But if I convert tuple to list, it works just fine:

In [445]: monitor={'foo':12.12, 'boo' : [1,1]}

In [446]: cache.insert(monitor)
Out[446]: ObjectId('\xc1U\x95J\xe2\x19N\xdc\x04\t\x00\x00')

Is there any reason why we cannot insert tuple objects?
Thank you,
Valentin.

Mike

unread,
Aug 26, 2009, 12:04:26 PM8/26/09
to mongodb-user
There is no technical reason why we cannot support saving tuples. The
issue is that the only database type they would map to is an array,
which is what Python lists map to as well. Therefore if you inserted a
tuple it would end up being decoded as a list. I have been shying away
from implicit conversions like this wherever possible, preferring the
approach of forcing users to do conversions explicitly to avoid
surprises.

That being said, tuples and lists might be interchangeable enough for
this change to make sense. What do people think about this?

vkuznet

unread,
Aug 26, 2009, 12:50:56 PM8/26/09
to mongodb-user
Thanks, it clears things up. Apart from internal decision, for end-
users will be useful to get
such message saying we don't support tuple, please convert to list.
Another option is to have a
flag in API allowing internal conversion.

Michael Dirolf

unread,
Aug 26, 2009, 3:26:13 PM8/26/09
to mongod...@googlegroups.com
Actually, I think it makes sense for tuples to be converted to lists.
Adding now.

Nicolas Clairon

unread,
Aug 26, 2009, 4:51:17 PM8/26/09
to mongod...@googlegroups.com
If tuple are converted to lists, then it make sense that every object
wich implement __iter__ method can be insert into database.

From my point of view, I prefere to be explicite than implicite as
converting tuple to list is not so difficulte :

my_list = list(my_tuple)

Michael Dirolf

unread,
Aug 26, 2009, 5:46:24 PM8/26/09
to mongod...@googlegroups.com
Any other thoughts on this? I'm pretty flexible on this - could be
strict and say lists only, or could allow any iterable (which would be
converted to a list on a database roundtrip). As Nicolas points out -
allowing just lists and tuples probably doesn't make as much sense as
either of the other options.

Wondering what the community thinks?

vkuznet

unread,
Aug 26, 2009, 9:01:04 PM8/26/09
to mongodb-user
As I wrote originally, as a user I don't see a problem ONLY if
appropriate exception message will be shown. If I'll be told that
tuples are not allowed and I need to convert it to list it's fine with
me as a user. Otherwise it's unclear if I got error or tuple object is
not supported. Here is exception message:

InvalidDocument: Cannot encode object: (1, 1)

so from user point of view it seems like driver problem, rather
unsupported object type.

On Aug 26, 5:46 pm, Michael Dirolf <m...@10gen.com> wrote:
> Any other thoughts on this? I'm pretty flexible on this - could be  
> strict and say lists only, or could allow any iterable (which would be  
> converted to a list on a database roundtrip). As Nicolas points out -  
> allowing just lists and tuples probably doesn't make as much sense as  
> either of the other options.
>
> Wondering what the community thinks?
>
> On Aug 26, 2009, at 4:51 PM, Nicolas Clairon wrote:
>
>
>
> > If tuple are converted to lists, then it make sense that every object
> > wich implement __iter__ method can be insert into database.
>
> > From my point of view, I prefere to be explicite than implicite as
> > converting tuple to list is not so difficulte :
>
> > my_list = list(my_tuple)
>

Phillip B Oldham

unread,
Aug 27, 2009, 4:04:44 AM8/27/09
to mongodb-user
On Aug 27, 2:01 am, vkuznet <vkuz...@gmail.com> wrote:
> As I wrote originally, as a user I don't see a problem ONLY if
> appropriate exception message will be shown. If I'll be told that
> tuples are not allowed and I need to convert it to list it's fine with
> me as a user. Otherwise it's unclear if I got error or tuple object is
> not supported. Here is exception message:
>
> InvalidDocument: Cannot encode object: (1, 1)
>
> so from user point of view it seems like driver problem, rather
> unsupported object type.

I agree with this, especially since tuples are ordered in python and
lists aren't.

Michael Dirolf

unread,
Aug 27, 2009, 12:30:32 PM8/27/09
to mongod...@googlegroups.com

Lists are ordered, they are just mutable. Either way, if we decide to
revert back to having tuples un-encodable I will change the error that
gets raised for a tuple. Any more thoughts from anyone on whether we
should keep this change or whether tuples should be unencodable?

I'm leaning towards keeping the change right now...


Phillip B Oldham

unread,
Sep 14, 2009, 4:39:50 AM9/14/09
to mongodb-user
On Aug 27, 5:30 pm, Michael Dirolf <m...@10gen.com> wrote:
> I'm leaning towards keeping the change right now...

Would it be possible for this change to be "hookable" in some way? For
instance, one could write a hook script to translate from/to a set or
Decimal (or another custom "type").

Michael Dirolf

unread,
Sep 14, 2009, 9:29:36 AM9/14/09
to mongod...@googlegroups.com

Please check out the example "custom_type.py" in the examples
directory of the PyMongo distribution. Think this has some approaches
that could work for you.

- Mike

Reply all
Reply to author
Forward
0 new messages