From: Benedict Verheyen on
Hi


i get the following error when trying to set data in the cache of a django
application. The error is however a python error as it involves pickling and i can
reproduce it in a shell.
The error i get is this:
cPickle.PicklingError: Can't pickle <class 'management.views.Stats'>: attribute lookup management.views.Stats failed

Stats is a class i use to hold a couple of values, a string (user) and a number (calls),
that are then used in a template. These are pickable, right?
The data i want to pickle is the return value of this function:

def calc_stats_topcallers():
stats_all = {}
# cPickle doesn't like this class
class Stats(object):
def __init__(self):
self.calls = None
self.user = None
current_year = datetime.datetime.now().year
archive_year = current_year - 4
years = xrange(archive_year, current_year+1)
for year in years:
stats_year = []
counts = {}
# Initialize count dict
# If we don't do this, we get a KeyError
for user in User.objects.all():
counts[user]=0
# Count the times an initiator has been calling
for i in Call.objects.filter(date_created__year=year):
for _init in i.initiator.all():
counts[_init] += 1
# Sort the dictionary
count_sorted = sorted(counts.items(), lambda x, y: cmp(x[1], y[1]), reverse=True)
# Take top 30 callers
for user_stat in count_sorted[0:30]:
if ( user_stat[1] > 0 ):
st = Stats()
st.calls = user_stat[1]
st.user = user_stat[0]
stats_year.append(st)
stats_all[year]=stats_year
stats_sorted = sorted(stats_all.items(), lambda x, y: cmp(x[0], y[0]), reverse=True)
return stats_sorted


When i run the function and pickle the data using a python shell, i get the error.

How do i solve this as i haven't got a clue.


Thanks,
Benedict

From: Peter Otten on
Benedict Verheyen wrote:

> i get the following error when trying to set data in the cache of a django
> application. The error is however a python error as it involves pickling
> and i can reproduce it in a shell.
> The error i get is this:
> cPickle.PicklingError: Can't pickle <class 'management.views.Stats'>:
> attribute lookup management.views.Stats failed
>
> Stats is a class i use to hold a couple of values, a string (user) and a
> number (calls), that are then used in a template. These are pickable,
> right? The data i want to pickle is the return value of this function:
>
> def calc_stats_topcallers():
> stats_all = {}
> # cPickle doesn't like this class
> class Stats(object):
> def __init__(self):
> self.calls = None
> self.user = None

You can only pickle instances of classes that are reachable by the import
system as only the qualified name of the class is stored, not the bytecode
to generate it. Move your class out of the function into the global module
scope and you should be OK.

Peter




From: Benedict Verheyen on
On 3/08/2010 17:01, Peter Otten wrote:
<snip>
> You can only pickle instances of classes that are reachable by the import
> system as only the qualified name of the class is stored, not the bytecode
> to generate it. Move your class out of the function into the global module
> scope and you should be OK.
>
> Peter

Thanks Peter,

that solved the problem !

Regards,
Benedict