微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

具有多个条件时删除如果不

如何解决具有多个条件时删除如果不

我想查找所有以 .jpg、.png 或 .jpeg 结尾的文件。 我写的东西是这样的:


class CreatePost(graphene.Mutation):
      class Arguments:
            description = graphene.String(required=True)
      post = graphene.Field(schema.Posts)
      @classmethod
      def mutate(cls,root,info,description):
            post = models.Post(description=description,added_by=info.context.user)
            post.save()
            return CreatePost(post=post)
class UpdatePost(graphene.Mutation):
      # owner(added_by)+admin can update and delete
      class Arguments:
            id = graphene.Int(required=True)
            description = graphene.String()
      post = graphene.Field(schema.Posts)
      @classmethod
      def mutate(cls,id,description):
            post = models.Post.objects.get(id=id)
            if (info.context.user == post.added_by):
                  post.description = description
                  post.save()
                  return CreatePost(post=post)
            else:
                  "Only the owner of the post and the admin(website's wonr) can edit and delet this post"
                  
                  

class DeletePost(graphene.Mutation):
       # owner(added_by)+admin can update and delete
      class Arguments:
            id = graphene.Int(required=True)
      post = graphene.Field(schema.Posts)
      @classmethod
      def mutate(cls,id):
            post = models.Post.objects.get(id=id)
            if (info.context.user == post.added_by):
                  post.delete()
                  return f"{'ID':ID}"
            else:
                  return {'partial': True}




但是我觉得不好看。例如,当您想搜索更多时。所以我写了这个:

(defun get-picture (dir)
  (remove-if-not (lambda (item)
           (or (string= ".jpg" (pathname-type item))
               (string= ".png" (pathname-type item))
               (string= ".jpeg" (pathname-type item))))
         (uiop:directory-files dir)))

但显然,mapc 在这里不正确。 所以我想知道有没有更好的方法(除了 dolist)?

解决方法

(defun get-picture-files (d &key
                            (extensions '("jpg" "png" "jpeg"))
                            (test #'string-equal))
  (remove-if (lambda (p)
               (not (member (pathname-type p)
                            extensions
                            :test test)))
             (uiop:directory-files d)))

这个

  • 允许您指定扩展名;
  • 和测试(所以“GOO.JPG”);
  • 只调用一次 pathname-type
  • 不使用已弃用的 remove-if-not

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。