سؤال

Here is the code (it runs inside class):

if profile['profile_picture']:
    profile['profile_picture_raw_path'], # Line 26
    profile['profile_picture_thumbnail'],
    profile['profile_picture'] = self.picture_path(profile['profile_picture'])

Keys don't exist. The typical result returned by picture_path is

('//domain.com/218971848/21924823397471f5e4e41a803da17f7c.jpg:large', '//domain.com/profile-images/218971848/21924823397471f5e4e41a803da17f7c.jpg:thumb-100', '//domain.com/profile-images/218971848/21924823397471f5e4e41a803da17f7c.jpg:exact')

As you can see the result is a tuple of 3 elements.

And I got an error:

 File "/srv/rwt/production/application/rwt/profile.py", line 26, in load_profile
    profile['profile_picture_raw_path'],
KeyError: 'profile_picture_raw_path'

Why does this error appear? Searched Stack Overflow for similar questions, but they seem to be asked about accessing dictionary values by not-existing keys.

هل كانت مفيدة؟

المحلول

profile['profile_picture_raw_path'], on a line by itself is parsed as a standalone expression (a rather useless one, though).

You need to tell the Python interpreter that the next lines are part of the expression:

profile['profile_picture_raw_path'], \
profile['profile_picture_thumbnail'], \
profile['profile_picture'] = self.picture_path(profile['profile_picture'])

نصائح أخرى

use a bracket

if profile['profile_picture']:
    (profile['profile_picture_raw_path'], # Line 26
    profile['profile_picture_thumbnail'],
    profile['profile_picture']) = self.picture_path(profile['profile_picture'])
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top