小编Dav*_*ull的帖子

如何链接可能在Python中返回None的属性查找?

我的问题是一般的问题,当一个中间问题可能会返回时如何链接一系列属性查找None,但是由于我遇到了试图使用Beautiful Soup的问题,我将在那个环境中问它.

Beautiful Soup解析HTML文档并返回一个对象,该对象可用于访问该文档的结构化内容.例如,如果解析的文档在变量中soup,我可以获得其标题:

title = soup.head.title.string
Run Code Online (Sandbox Code Playgroud)

我的问题是,如果文档没有标题,则soup.head.title返回None并且后续string查找会引发异常.我可以打破链条:

x = soup.head
x = x.title if x else None
title = x.string if x else None
Run Code Online (Sandbox Code Playgroud)

但在我看来,这是冗长而难以阅读的.

我可以写:

title = soup.head and soup.head.title and soup.title.head.string
Run Code Online (Sandbox Code Playgroud)

但这是冗长而低效的.

如果想到的话,我认为可能的一个解决方案是创建一个nil可以返回None任何属性查找的对象(调用它).这将允许我写:

title = ((soup.head or nil).title or nil).string
Run Code Online (Sandbox Code Playgroud)

但这很难看.有没有更好的办法?

python

18
推荐指数
2
解决办法
4060
查看次数

git:如何使用另一个分支的内容重置分支?

我无法弄清楚如何将一个分支更新为与另一个分支相同。下面是一个例子:

git init test; cd test
echo apple >a; echo banana >b; git add a b; git commit -m 'A1/a:apple;b:banana'
echo carrot >c; git add c; git commit -m 'A2/c:carrot'
git checkout -b second HEAD^1
echo beets >b; echo dandelion >d; git add b d; git commit -m 'B1/b:beets;d:dandelion'
Run Code Online (Sandbox Code Playgroud)

在这一点上,我的历史看起来像这样:

A1-----A2     (master, contains a:apple, b:banana, c:carrot)
 \
  \----B1     (second, contains a:apple, b:beets, d:dandelion)
Run Code Online (Sandbox Code Playgroud)

现在我想向“第二个”分支添加一个提交,使其内容与“主”分支匹配,即:

A1-----A2
 \
  \----B1--?--B2     (desired contents a:apple, b:banana, c:carrot)
Run Code Online (Sandbox Code Playgroud)

我的问题是,我运行哪些 git 命令来执行此操作?我想出的最接近的是:

git checkout master .
git …
Run Code Online (Sandbox Code Playgroud)

git

5
推荐指数
1
解决办法
2288
查看次数

在UITableView更新块中组合moveSection:toSection,insertSections:和deleteSections :.

UITableView文件说,这是确定的呼叫结合moveSection:toSection:,insertSections:withRowAnimation:以及deleteSections:withRowAnimation:beginUpdates- endUpdates块.一节所谓的批量插入,删除和行和部分重装表视图编程指南还解释说,混合插入和删除更新区块内时,表视图不会缺失确实在插入之前,无论什么样的顺序方法打电话.

我的问题是,当调用moveSection:toSection:与调用相结合insertSections:deleteSections:,在什么样的顺序不表视图做动作?或者,fromSectiontoSection是指删除之前,删除和插入之间,还是插入之后的部分索引?

uitableview ios

0
推荐指数
1
解决办法
1142
查看次数

标签 统计

git ×1

ios ×1

python ×1

uitableview ×1