英文:
What purpose did the IMAP4.Literal attribute serve and how was it used?
问题
Literal属性在imaplib库中用于处理IMAP协议中的文字字面量(literal)。文字字面量是一种用于发送邮件数据的方法,通常用于发送邮件的内容。在源代码中,Literal属性是一个正则表达式对象,用于匹配文字字面量的标识符和大小。正则表达式re.compile(b'.*{(?P<size>\\d+)}$', re.ASCII)
用于匹配这些文字字面量。
在imaplib库中,Literal属性主要用于IMAP协议的底层实现,用于解析和处理接收到的邮件数据。当你使用IMAP协议从邮件服务器检索邮件时,这个属性可能用于解析邮件的文本内容。
尽管Literal属性在源代码中定义了,但根据注释,它已经不再使用,并且只是为了向后兼容性而保留。因此,通常情况下,你不需要直接操作或使用Literal属性。在imaplib库的后续版本中,可能已经有其他方式或工具取代了它,以更有效地处理邮件数据。
如果你想了解关于Literal属性的更多信息以及它是否被其他内容取代了,你可以查看imaplib库的更新文档或相关的资源,以获取更详细的信息。
英文:
I am handling connections to an IMAP server using the Python standard imaplib library.
I create an IMAP4 object like this:
import imaplib
M = imaplib.IMAP4_SSL('imap.gmail.com') # Open imaplib connection
The resulting object has these methods and attributes:
>>> dir(M)
['Literal',
'PROTOCOL_VERSION',
'Untagged_status',
'_CRAM_MD5_AUTH',
'__class__',
'__delattr__',
'__dict__',
'__dir__',
'__doc__',
'__enter__',
'__eq__',
'__exit__',
'__format__',
'__ge__',
'__getattr__',
'__getattribute__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__le__',
'__lt__',
'__module__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'__weakref__',
'_append_untagged',
'_check_bye',
'_cmd_log',
'_cmd_log_idx',
'_cmd_log_len',
'_command',
'_command_complete',
'_connect',
'_create_socket',
'_dump_ur',
'_encoding',
'_get_capabilities',
'_get_line',
'_get_response',
'_get_tagged_response',
'_log',
'_match',
'_mesg',
'_mode_ascii',
'_mode_utf8',
'_new_tag',
'_quote',
'_simple_command',
'_tls_established',
'_untagged_response',
'abort',
'append',
'authenticate',
'capabilities',
'capability',
'certfile',
'check',
'close',
'continuation_response',
'copy',
'create',
'debug',
'delete',
'deleteacl',
'enable',
'error',
'expunge',
'fetch',
'file',
'getacl',
'getannotation',
'getquota',
'getquotaroot',
'host',
'is_readonly',
'keyfile',
'list',
'literal',
'login',
'login_cram_md5',
'logout',
'lsub',
'mo',
'myrights',
'namespace',
'noop',
'open',
'partial',
'port',
'print_log',
'proxyauth',
'read',
'readline',
'readonly',
'recent',
'rename',
'response',
'search',
'select',
'send',
'setacl',
'setannotation',
'setquota',
'shutdown',
'sock',
'socket',
'sort',
'ssl_context',
'starttls',
'state',
'status',
'store',
'subscribe',
'tagged_commands',
'tagnum',
'tagpre',
'tagre',
'thread',
'uid',
'unselect',
'unsubscribe',
'untagged_responses',
'utf8_enabled',
'welcome',
'xatom']
By default, the Literal attribute contains a re (regular expression) object:
>>> type(M.Literal)
re.Pattern
>>> print(M.Literal)
re.compile(b'.*{(?P<size>\\d+)}$', re.ASCII)
In the source code for imaplib.py, the attribute is defined on lines 113-114:
# Literal is no longer used; kept for backward compatibility.
Literal = re.compile(br'.*{(?P<size>\d+)}$', re.ASCII)
What was it used for and how, and what, if anything, replaced it?
答案1
得分: 1
这个注释是在提交a6429db4b837dc49eb1bee42617798aebd7b43d4
中添加的,该提交定义了一个类似的常量_Literal
,用于定义一个名为Literal
的实例属性,现在该属性用于替代原始的全局Literal
。
因此,模式仍然在使用,只是不再通过模块全局变量。
之前:
$ git checkout a6429db4b837dc49eb1bee42617798aebd7b43d4^
$ grep Literal Lib/imaplib.py
Literal = re.compile(br'.*{(?P<size>\d+)}$', re.ASCII)
while self._match(Literal, dat):
之后:
$ git checkout a6429db4b837dc49eb1bee42617798aebd7b43d4
$ grep Literal Lib/imaplib.py
# Literal is no longer used; kept for backward compatibility.
Literal = re.compile(br'.*{(?P<size>\d+)}$', re.ASCII)
_Literal = br'.*{(?P<size>\d+)}$'
self.Literal = re.compile(_Literal, re.ASCII)
self.Literal = re.compile(_Literal)
while self._match(self.Literal, dat):
英文:
That comment was added in commit a6429db4b837dc49eb1bee42617798aebd7b43d4, which defined a similar constant _Literal
that was used to define an instance attribute named Literal
that is now used in place of the original global Literal
.
So the pattern is still used, just not via a module global.
Before:
$ git checkout a6429db4b837dc49eb1bee42617798aebd7b43d4\^
$ grep Literal Lib/imaplib.py
Literal = re.compile(br'.*{(?P<size>\d+)}$', re.ASCII)
while self._match(Literal, dat):
After:
$ git checkout a6429db4b837dc49eb1bee42617798aebd7b43d4
$ grep Literal Lib/imaplib.py
# Literal is no longer used; kept for backward compatibility.
Literal = re.compile(br'.*{(?P<size>\d+)}$', re.ASCII)
_Literal = br'.*{(?P<size>\d+)}$'
self.Literal = re.compile(_Literal, re.ASCII)
self.Literal = re.compile(_Literal)
while self._match(self.Literal, dat):
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论