切换到 Python 3 时 functools 停止工作

functools stops working when switch to Python 3

我们已经将我们的 Django 项目代码库从 Python 2.7 迁移到 3.6,突然间,过去的工作停止了。具体来说,这个:

map(functools.partial(self._assocUser, user=user), persistedGroupIds)

需要替换为:

 for group_id in persistedGroupIds:
      self._assocUser(group_id, user)

还有这个:

    persistedGroupIds = map(functools.partial(self._persistGroup, grp_mappings=attrAll.entitlements), saml_authorization_attributes)  

需要去:

     persistedGroupIds = []
     for idp_group_name in saml_authorization_attributes:
         persistedGroupIds.append(self._persistGroup(idp_group_name, attrAll.entitlements))

在旧功能重新出现之前。 Python 3 的 functools 似乎不起作用。

这是在 Python 2:

下运行良好的完整代码清单
    from django.contrib.auth.models import User
from django.contrib.auth.models import Group
import functools
from mappings import SAMLAttributesConfig
from django.conf import settings
import logging

log = logging.getLogger(__name__)

class SAMLServiceProviderBackend(object):

    empty_entitlements_message="IdP supplied incorrect authorization entitlements.  Please contact their support."

    def _assocUser(self, group_id, user):

        group = Group.objects.get(id=group_id)
        group.user_set.add(user)

        return None


    def _persistGroup(self,idp_group_name, grp_mappings):

        group_name = grp_mappings[idp_group_name]

        try:
            group = Group.objects.get(name=group_name)
        except Group.DoesNotExist:
            group = Group(name=group_name)
            group.save()

        return group.id

    def _extract_grp_entitlements(self,saml_authentication_attributes,groups):
        result = []
        input_length = len(saml_authentication_attributes[groups])
        if input_length == 0:
            log.error(self.empty_entitlements_message)
            raise RuntimeError(self.empty_entitlements_message)
        if input_length == 1:
            result = [t.strip() for t in saml_authentication_attributes[groups][0].split(',')] 
        elif input_length:
            result = saml_authentication_attributes[groups]
        return result
#         return [t.strip() for t in saml_authentication_attributes[groups][0].split(',')] \
#             if len(saml_authentication_attributes[groups]) == 1\
#             else saml_authentication_attributes[groups]


    def authenticate(self, saml_authentication=None):
        if not saml_authentication:  # Using another authentication method
            return None

        attrAll = SAMLAttributesConfig(mappings_file_name=settings.AUTH_MAPPINGS_FILE).get_config()
        groups = attrAll.entitlements.containerName

        if saml_authentication.is_authenticated():

            saml_authentication_attributes = saml_authentication.get_attributes()
            saml_authorization_attributes = self._extract_grp_entitlements(saml_authentication_attributes,groups)          
            persistedGroupIds = map(functools.partial(self._persistGroup, grp_mappings=attrAll.entitlements), saml_authorization_attributes)  

            try:
                user = User.objects.get(username=saml_authentication.get_nameid())
            except User.DoesNotExist:

                user = User(username=saml_authentication.get_nameid())
                user.set_unusable_password()
                try:
                    user.first_name = saml_authentication_attributes['samlNameId'][0]
                except KeyError:
                    pass
                try:
                    setattr(user, "first_name", saml_authentication_attributes[attrAll.subject.first_name][0])

                except KeyError:
                    pass 

                #user.last_name = attributes['Last name'][0]
                user.save()
                map(functools.partial(self._assocUser, user=user), persistedGroupIds)
                user.save()
            return user
        return None

    def get_user(self, user_id):
        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None

上面的代码在 Python 3 环境下不再工作,只能开始像这样工作,在 for 循环中拼写出 functools.partial() 调用:

from django.contrib.auth.models import User
from django.contrib.auth.models import Group
import functools
from .mappings import SAMLAttributesConfig
from django.conf import settings
import logging

log = logging.getLogger(__name__)

class SAMLServiceProviderBackend(object):

    empty_entitlements_message="IdP supplied incorrect authorization entitlements.  Please contact their support."

    def _assocUser(self, group_id, user):

        group = Group.objects.get(id=group_id)
        group.user_set.add(user)

        return None


    def _persistGroup(self,idp_group_name, grp_mappings):

        group_name = grp_mappings[idp_group_name]

        try:
            group = Group.objects.get(name=group_name)
        except Group.DoesNotExist:
            group = Group(name=group_name)
            group.save()

        return group.id

    def _extract_grp_entitlements(self,saml_authentication_attributes,groups):
        result = []
        input_length = len(saml_authentication_attributes[groups])
        if input_length == 0:
            log.error(self.empty_entitlements_message)
            raise RuntimeError(self.empty_entitlements_message)
        if input_length == 1:
            result = [t.strip() for t in saml_authentication_attributes[groups][0].split(',')] 
        elif input_length:
            result = saml_authentication_attributes[groups]
        return result
#         return [t.strip() for t in saml_authentication_attributes[groups][0].split(',')] \
#             if len(saml_authentication_attributes[groups]) == 1\
#             else saml_authentication_attributes[groups]


    def authenticate(self, saml_authentication=None):
        if not saml_authentication:  # Using another authentication method
            return None

        attrAll = SAMLAttributesConfig(mappings_file_name=settings.AUTH_MAPPINGS_FILE).get_config()
        groups = attrAll.entitlements.containerName

        if saml_authentication.is_authenticated():

            saml_authentication_attributes = saml_authentication.get_attributes()
            saml_authorization_attributes = self._extract_grp_entitlements(saml_authentication_attributes,groups)          
            persistedGroupIds = map(functools.partial(self._persistGroup, grp_mappings=attrAll.entitlements), saml_authorization_attributes)  

            try:
                user = User.objects.get(username=saml_authentication.get_nameid())
            except User.DoesNotExist:

                user = User(username=saml_authentication.get_nameid())
                user.set_unusable_password()
                try:
                    user.first_name = saml_authentication_attributes['samlNameId'][0]
                except KeyError:
                    pass
                try:
                    setattr(user, "first_name", saml_authentication_attributes[attrAll.subject.first_name][0])

                except KeyError:
                    pass 

                #user.last_name = attributes['Last name'][0]
                user.save()
                for group_id in persistedGroupIds:
                    self._assocUser(user = user, group_id = group_id)
                # map(functools.partial(self._assocUser, user=user), persistedGroupIds)
                user.save()
            return user
        return None

    def get_user(self, user_id):
        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None

有什么问题吗?

我在 Eclipse 中使用 PyDev 插件。下面是我的 Python 解释器的配置方式:



这是 Eclipse 的 .pydevproject 文件:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?eclipse-pydev version="1.0"?><pydev_project>



    <pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">venv3.6</pydev_property>



    <pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python interpreter</pydev_property>



    <pydev_variables_property name="org.python.pydev.PROJECT_VARIABLE_SUBSTITUTION">

        <key>DJANGO_SETTINGS_MODULE</key>

        <value>reporting.settings</value>

        <key>DJANGO_MANAGE_LOCATION</key>

        <value>./manage.py</value>

        <key>SAML_PLUGIN</key>

        <value>/Users/sl/abc/venv3.6/lib/python3.6/site-packages/onelogin/saml2</value>

        <key>PY</key>

        <value>36</value>

    </pydev_variables_property>



    <pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">



        <path>/${PROJECT_DIR_NAME}</path>



    </pydev_pathproperty>



    <pydev_pathproperty name="org.python.pydev.PROJECT_EXTERNAL_SOURCE_PATH">

        <path>${SAML_PLUGIN}</path>

    </pydev_pathproperty>


</pydev_project>

中Python 3、map函数returns an iterator instead of a list.

这意味着如果您在集合上调用 map,调用的效果在您遍历生成的迭代器之前不会具体化。

考虑这个 class:

>>> class C:
...     def __init__(self, x):
...         self.x = x
...     def double(self):
...         self.x *= 2
...     def __repr__(self):                                                                                             
...         return '<C:{}>'.format(self.x)
... 

让我们列一个实例列表:

>>> cs = [C(x) for x in range(1, 4)]
>>> cs
[<C:1>, <C:2>, <C:3>]

现在使用map调用每个实例的double方法:

>>> res = map(C.double, cs)

注意结果不是列表:

>>> res
<map object at 0x7ff276350470>

并且实例没有改变:

>>> cs
[<C:1>, <C:2>, <C:3>]

如果我们在迭代器上调用 next,实例将依次更新。

>>> next(res)
>>> cs
[<C:2>, <C:2>, <C:3>]
>>> next(res)
>>> cs
[<C:2>, <C:4>, <C:3>]
>>> next(res)
>>> cs
[<C:2>, <C:4>, <C:6>]

在您提供的代码示例中,调用 map 的结果未分配给变量,因此 map 用于其副作用而不是其输出。在 Python 3 中,正确的做法是遍历 iterable 并在每个元素上调用函数:

>>> for c in cs:
        c.double()

正如链接文档所说:

Particularly tricky is map() invoked for the side effects of the function; the correct transformation is to use a regular for loop (since creating a list would just be wasteful).