-1

我们已经将我们的 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 3functools似乎不起作用。

以下是在 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 环境下不再工作,只能开始工作,在循环functools.partial()中拼写出调用:for

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>
4

1 回答 1

1

在 Python 3 中, map 函数返回一个迭代器而不是一个 list

这意味着如果你调用map一个集合,调用的效果直到你迭代生成的迭代器时才会实现。

考虑这个类:

>>> 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 中,正确的方法是遍历可迭代对象并在每个元素上调用函数:

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

正如链接的文档所说:

map()对于函数的副作用调用特别棘手;正确的转换是使用常规的 for 循环(因为创建列表只是浪费)。

于 2018-10-13T18:00:57.117 回答