我使用PyCharm IDE来协助制作符合PEP0257标准的文档字符串.它提供了两个属性,我不完全理解它们之间的区别/用法:
:raise Exception: exception explanation here:raises Exception: exception explanation here我什么时候会在我的文档中使用raise反对raises?具体来说,如果一个类需要一个未提供的参数并引发一个TypeError,那应该用来记录它?
我有一个奇怪的问题.为了有一个简短的最小工作示例(MWE),让我们假设Connect()返回一个urllib3.connection.HTTPConnection对象.我们还假设我还有其他一些异常,如果'magicword'在错误消息中找到我想忽略的话(不是实际的单词,但是,嘿,这是一个MWE).
MWE:
try:
conn_obj = Connect()
except Exception as e: # there are some other exceptions I want to ignore
if 'magicword' not in e.message:
print 'fatal error: {}'.format(e.message)
Run Code Online (Sandbox Code Playgroud)
这在我的机器上正常工作并在遇到时打印"致命错误"并忽略其他异常(在这种情况下应该如此).
但是,在同事的计算机上,不会处理错误,而是会崩溃并创建回溯.这是我的机器上完全相同的错误,只有他不会打印和崩溃而不是处理.我们都使用完全相同的操作系统(Windows 7).
显然不处理特定的异常并不理想,所以我尝试了这条路线:
from urllib3.exceptions import NewConnectionError
try:
conn_obj = Connect()
except NewConnectionError as nce:
print 'fatal error: {}'.format(e.message)
except Exception as e: # there are some other exceptions I want to ignore
if 'magicword' not in e.message:
print 'fatal error: {}'.format(e.message)
Run Code Online (Sandbox Code Playgroud)
那也行不通.出于某种原因,它不会在他的盒子上捕获例外.为什么可以在我的机器上处理异常但不在他的机器上处理?
更新: …
我希望我的项目有这个结构:
requirements.txt
README.md
.gitignore
project/
__init__.py
project.py
core/
__init__.py
base.py
engines/
__init__.py
engine1.py
engine2.py
utils/
__init__.py
refine_data.py
whatever.py
Run Code Online (Sandbox Code Playgroud)
该应用程序是从project/project.py.但是,在使用相对或绝对导入时,我经常会遇到导入错误.
两个引擎都需要从中导入project.core.base,utils也需要从中导入project.core.base,并且project.py(运行的主文件)需要能够从中导入engines.
绝对导入不起作用:
# engines/engine1.py
from project.core.base import MyBaseClass
Run Code Online (Sandbox Code Playgroud)
这给出了错误:
ImportError: No module named project.core.base
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试相对导入而不是
# engines/engine1.py
from ..core.base import MyBaseClass
Run Code Online (Sandbox Code Playgroud)
我明白了:
ValueError: Attempted relative import beyond toplevel package
Run Code Online (Sandbox Code Playgroud)
我已经看到Github上的其他项目结构相似,但这似乎会引起各种各样的问题.我如何让它工作?
我在pythex上使用了以下正则表达式来测试它:
(\d|t)(_\d+){1}\.
Run Code Online (Sandbox Code Playgroud)
它工作正常,我主要对组2感兴趣.它成功运作如下所示:
但是,我无法让Python真正向我展示正确的结果.这是一个MWE:
fn_list = ['IMG_0064.png',
'IMG_0064.JPG',
'IMG_0064_1.JPG',
'IMG_0064_2.JPG',
'IMG_0064_2.PNG',
'IMG_0064_2.BMP',
'IMG_0064_3.JPEG',
'IMG_0065.JPG',
'IMG_0065.JPEG',
'IMG-20150623-00176-preview-left.jpg',
'IMG-20150623-00176-preview-left_2.jpg',
'thumb_2595.bmp',
'thumb_2595_1.bmp',
'thumb_2595_15.bmp']
pattern = re.compile(r'(\d|t)(_\d+){1}\.', re.IGNORECASE)
for line in fn_list:
search_obj = re.match(pattern, line)
if search_obj:
matching_group = search_obj.groups()
print matching_group
Run Code Online (Sandbox Code Playgroud)
输出什么都没有.
但是,上面的pythex清楚地显示了两个返回的组,第二个应该存在并且击中了更多的文件.我究竟做错了什么?
我已经逐步完成了这个并用pdb跟踪检查了每个值,但无法弄清楚为什么我得到一个KeyError,一切都有它的预期值.这是功能:
def get_formatted_timestamp(date_field, time_field):
# expects date_field = yyyy-M-d
# expects time_field = H:m:s
# outputs yyyy-MM-ddTHH:mm:ss:SSSZ ('T' and 'Z' are literals)
dt = date_field.strip().split('/')
tm = time_field.strip().split(':')
if len(dt) != 3 or len(tm) != 3 or len(dt[0]) != 4:
print 'invalid date or time: {} {}'.format(date_field, time_field)
return '1900-01-01T00:00:00.000Z' # error date value
y = dt[0]
M = dt[1] if len(dt[1]) == 2 else '0'+dt[1]
d = dt[2] if len(dt[2]) == 2 else '0'+dt[2]
H = tm[0] if len(tm[0]) …Run Code Online (Sandbox Code Playgroud)