I know there is support for "encoding" datetime objects using the
gluon.contrib flavor of simplejson.
(at least in the old version of web2py I am using)
But I opted for using 'pip install simplejson' as it compiles the latest
version for me... So I use that.
I don't have issues in encoding, as I use it only in the cache (which is
more-readable AND faster then cPickle), and so I don't even try to store
complex-objects - not even 'Rows' objects - I always do ',as_list()' on all
the queries in my lambdas...
What I am interested in doing, is have it turned back into a datetime
object when 'decoding' - and THAT is not supported even in the web2py
flavor - and I haven't found a single simple solution for it on the web for
the standard simplejson either.
I tried to hack it in the redis_cache.py (since I use redis for caching). I
added a function 'as_datetime' at the beginning of the file:
def as_datetime(dct):
if type(dct) is dict:
Created = dct.get('Created', None)
Modified = dct.get('Modified', None)
if Created: dct['Created'] = datetime.datetime.strptime(Created,
'%Y-%m-%d
%H:%M:%S')
if Modified: dct['Modified'] = datetime.datetime.strptime(Modified,
'%Y-%m-%d
%H:%M:%S')
return dct
Then, in the '__call__' function of the 'RedisClient' class, I replaced:
value = pickle.loads(obj)
with:
value = pickle.loads(obj, object_hook=as_datetime)
I tried some other variations of the, like going over the values of the
dictionary, and looking for a non-empty string-type value that
startswith('20') and/or matches a reg-ex pattern of isodatetime, and/or
using 'dateutil.parse()' instead of 'datetime.strptime()' etc...
The problem I'm having is that this is all very 'taxing' on my
caching-system - I am getting a slowdown of 40% on the entire
cache-retrieval on each request, compared to leaving it as a string...
I am wondering, is that more a consequence of the way I am interjecting
into the simplejson parser (the 'object_hoock' approach...)
I wonder, is there a better (more performant) way of doing this? Am I
missing something?
I've seen there is a 'default=' something somewhere in the simplejson
stuff, but there is very poor documentation on these things in simplejson...
It just seems such a trivial thing, I was sure I am missing something
obvious for some reason...
If I do, I haven't ought it yet... Any thoughts?
Is there a way to interject the parsing process at the leaf-most value, and
not the dictionary-level? Would that be faster?
* I know I should probably be using Massimo's implementations/wrapper-over
simplejson, I just don't fully understand the whole _simplejson
speed-improvement stuff, as I am using python 2.6 and not 2.7 - so I'm not
clear on where I stand in that respect for using Massimo's code... I think
I would still get better performance just using the 'pip install
simplejson' approach and use that one directly...
I would consider using it, if it would solve my datetime problem, but it
doesn't - all it solves, is *encoding *'Rows' objects with datetime
objects, which I don't use anyway...
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.