我的代码中有一股难闻的气味.也许我只需要让它消失一段时间,但现在它让我烦恼.
我需要创建三个不同的输入文件来运行三个辐射传输建模(RTM)应用程序,以便我可以比较它们的输出.这个过程将重复数千组输入,所以我用python脚本自动化它.
我想将输入参数存储为一个通用的python对象,我可以将其传递给另外三个函数,每个函数都会将该通用对象转换为运行他们负责的RTM软件所需的特定参数.我认为这是有道理的,但随意批评我的方法.
每个RTM软件都有许多可能的输入参数.他们中的许多人都在重叠.他们中的大多数都保持合理的默认值,但应该很容易改变.
我开始时很简单 dict
config = {
day_of_year: 138,
time_of_day: 36000, #seconds
solar_azimuth_angle: 73, #degrees
solar_zenith_angle: 17, #degrees
...
}
Run Code Online (Sandbox Code Playgroud)
有很多参数,它们可以干净地分类成组,所以我想在以下内容中使用dicts dict:
config = {
day_of_year: 138,
time_of_day: 36000, #seconds
solar: {
azimuth_angle: 73, #degrees
zenith_angle: 17, #degrees
...
},
...
}
Run Code Online (Sandbox Code Playgroud)
我喜欢.但是有很多冗余属性.例如,如果另一个是已知的,那么可以找到太阳方位角和天顶角,那么为什么要对两者进行硬编码呢?所以我开始研究python的内置property.如果我将它存储为对象属性,这可以让我对数据做一些漂亮的事情:
class Configuration(object):
day_of_year = 138,
time_of_day = 36000, #seconds
solar_azimuth_angle = 73, #degrees
@property
def solar_zenith_angle(self):
return 90 - self.solar_azimuth_angle
...
config = Configuration()
Run Code Online (Sandbox Code Playgroud)
但是现在我已经失去了第二个dict例子中的结构.
请注意,一些属性是比我少琐碎的solar_zenith_angle例子,可能需要访问其他属性的属性组之外这是一部分.例如,我可以计算 …
我有这个HTML:
<div class="speaker-list">
<div class="view-content">
<div class="views-row views-row-1 views-row-odd views-row-first">
<div class="views-field views-field-title">
<span class="field-content">
<a href="/speaker/keith-anderson">Keith Anderson</a>
</span>
</div>
<div class="views-field views-field-field-job-title">
<div class="field-content">VP, Digital Advisory</div>
</div>
<div class="views-field views-field-field-company">
<div class="field-content">RetailNet Group</div>
</div>
<div class="views-field views-field-title-1">
<span class="field-content">
<a href="/session/store">Store of the Future</a>
</span>
</div>
<div class="views-field views-field-field-headshot">
<div class="field-content">
<div id="file-53" class="file file-image file-image-jpeg contextual-links-region">
<div class="content">
<img typeof="foaf:Image" src="/kanderson.jpg" width="180" height="180" alt="" />
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
它是由Drupal视图动态生成的,因此我根本无法更改输出html.我需要在这里工作.这是期望的结果:

爆头没有任何造型,这就是它的样子:

我试图设置图像样式以强制它浮动到文本的左侧:
.view-speaker-list div.view-content div.views-row …Run Code Online (Sandbox Code Playgroud) 我需要一个函数,它将python的运算符符号或关键字之一作为字符串,连同其操作数,对其进行求值,并返回结果.像这样:
>>> string_op('<=', 3, 3)
True
>>> string_op('|', 3, 5)
7
>>> string_op('and', 3, 5)
True
>>> string_op('+', 5, 7)
12
>>> string_op('-', -4)
4
Run Code Online (Sandbox Code Playgroud)
不能认为该字符串是安全的.我只对映射二元运算符感到满意,但如果能得到所有这些运算符,我会非常高兴.
import operator
def string_op(op, *args, **kwargs):
"""http://docs.python.org/2/library/operator.html"""
symbol_name_map = {
'<': 'lt',
'<=': 'le',
'==': 'eq',
'!=': 'ne',
'>=': 'ge',
'>': 'gt',
'not': 'not_',
'is': 'is_',
'is not': 'is_not',
'+': 'add', # conflict with concat
'&': 'and_', # (bitwise)
'/': 'div',
'//': 'floordiv',
'~': 'invert',
'%': 'mod',
'*': …Run Code Online (Sandbox Code Playgroud) 我是作者 - 目前几乎绝对是唯一的用户,如果在多个项目中 - 一个简单的数据库层(灵感来自minimongo)为MongoDB调用kale.我目前__getattr__在基类中使用的模型导致了一些难以跟踪的错误.
去年6月David Halter在这个网站上简明扼要地阐述了我遇到的问题.讨论很有意思,但没有提供解决方案.
简而言之:
>>> class A(object):
... @property
... def a(self):
... print "We're here -> attribute lookup found 'a' in one of the usual places!"
... raise AttributeError
... return "a"
...
... def __getattr__(self, name):
... print "We're here -> attribute lookup has not found the attribute in the usual places!"
... print('attr: ', name)
... return "not a"
...
>>> print(A().a)
We're here …Run Code Online (Sandbox Code Playgroud)