<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def set_preferences(self, user=None, **kwargs):
 """Set preferences from keyword arguments."""

        if user is None:
            user = current_user

        d = {pref.key: pref for pref in user.preferences}
        for k, v in kwargs.items():
            if k in d:
                d[k].value = v
            else:
                d[k] = UserPreference(user=user, key=k, value=v)
                db.session.add(d[k])