更新:我正在改写这个问题,因为对我来说重点是识别对象文字:
如何区分对象文字和任何其他Javascript对象(例如DOM节点,Date对象等)?我该怎么写这个函数:
function f(x) {
if (typeof x === 'object literal')
console.log('Object literal!');
else
console.log('Something else!');
}
Run Code Online (Sandbox Code Playgroud)
因此它只会Object literal!在下面的第一个调用中打印出来:
f({name: 'Tom'});
f(function() {});
f(new String('howdy'));
f('hello');
f(document);
Run Code Online (Sandbox Code Playgroud)
原始问题
我正在编写一个Javascript函数,用于接受对象文字,字符串或DOM节点作为其参数.它需要稍微不同地处理每个参数,但目前我无法弄清楚如何区分DOM节点和普通的旧对象文字.
这是我的函数的一个大大简化的版本,以及我需要处理的每种参数的测试:
function f(x) {
if (typeof x == 'string')
console.log('Got a string!');
else if (typeof x == 'object')
console.log('Got an object literal!');
else
console.log('Got a DOM node!');
}
f('hello');
f({name: 'Tom'});
f(document);
Run Code Online (Sandbox Code Playgroud)
此代码将为后两个调用记录相同的消息.我无法弄清楚该else if条款中包含哪些内容.我尝试过其他类似的变化x instanceof Object也有同样的效果.
据我所知,这可能是我的API /代码设计不好.即使是这样,我仍然想知道如何做到这一点.
我想在网格中显示图像缩略图的集合.图像将有各种尺寸,但我想将缩略图限制为特定尺寸(让我们说200px宽和150px高).
我想要找到的是一些神奇的HTML标记和CSS规则
<img>元素中我不确定这是否可行.我可以使用以下标记对我想要的(坏)近似:
<div class="thumb">
<img src="360x450.jpeg">
</div>
Run Code Online (Sandbox Code Playgroud)
和CSS:
.thumb {
width: 200px;
height: 150px;
overflow: hidden;
}
.thumb img {
min-width: 200px;
min-height: 150px;
width: 200px;
}
Run Code Online (Sandbox Code Playgroud)
这种尝试以各种方式打破:
纵向方向的图像尺寸正确,但会从容器底部溢出,导致垂直偏心裁剪.
由于硬编码width和min-height规则,宽和短的图像在水平维度上会失真.
但如果没有硬编码width,大于最小高度和宽度的图像将根本不会调整大小.
如果它有用,我会举一个例子来(希望)说明我想要做的事情,在这里:
我知道我可以通过<img>完全省略元素来解决这个问题,而是将缩略图拉成包含元素的居中背景图像,但是,如果可能的话,我想将<img>元素保留在页面中.
感谢您提供的任何帮助或指示!
编辑:我想我应该注意到理想的解决方案适用于IE 6+和现代浏览器,但任何适用于IE 9+和其他现代浏览器(最近的WebKit,Gecko等)的解决方案都将很乐意接受.
我目前的项目需要广泛使用位字段.我找到了一个简单的,功能性的配方,用于一个字段类,但它缺少我需要的一些功能,所以我决定扩展它.我刚刚实施__str__,__repr__我想确保我遵循惯例.
__str__应该是非正式和简洁的,所以我让它返回位字段的十进制值(str(bit field 11) 即将是"3".
__repr__应该是对象的官方表示,所以我让它返回实际的位串(repr(bit field 11)即将是"11").
在您看来,这个实现是否符合str和repr?
另外,我已经使用该bin()函数来获取存储在类中的值的位串.这与Python <2.6不兼容,有替代方法吗?
干杯,
皮特
鉴于功能
def f():
x, y = 1, 2
def get():
print 'get'
def post():
print 'post'
Run Code Online (Sandbox Code Playgroud)
有没有办法让我以我可以调用它们的方式访问它的本地get()和post()函数?我正在寻找一个可以使用上面定义的函数f()的函数:
>>> get, post = get_local_functions(f)
>>> get()
'get'
Run Code Online (Sandbox Code Playgroud)
我可以访问那些本地函数的代码对象
import inspect
for c in f.func_code.co_consts:
if inspect.iscode(c):
print c.co_name, c
Run Code Online (Sandbox Code Playgroud)
结果
get <code object get at 0x26e78 ...>
post <code object post at 0x269f8 ...>
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚如何获得实际可调用的函数对象.这甚至可能吗?
谢谢你的帮助,
将.
我刚刚开始深入研究XProc(使用Calabash).我有一系列XSLT转换我想应用于单个输入文档以生成单个输出文档.我以前使用一个简单的Python脚本来驱动转换,但似乎XProc可能是一个很好的选择.
下面的管道似乎对我有用.它基本上只是需要以正确顺序应用的XSLT转换列表.问题是,这似乎是多余的.我希望有一些方法可以减少它,但(到目前为止)我无法自己解决这个问题.
<?xml version="1.0"?>
<p:pipeline version="1.0" xmlns:p="http://www.w3.org/ns/xproc">
<p:xslt name="remove-locations">
<p:input port="stylesheet">
<p:document href="preprocessors/remove-locations.xsl"/>
</p:input>
</p:xslt>
<p:xslt name="divisions-1">
<p:input port="stylesheet">
<p:document href="preprocessors/divisions-1.xsl"/>
</p:input>
</p:xslt>
<p:xslt name="divisions-2">
<p:input port="stylesheet">
<p:document href="preprocessors/divisions-2.xsl"/>
</p:input>
</p:xslt>
<p:xslt name="subjects-1">
<p:input port="stylesheet">
<p:document href="preprocessors/subjects-1.xsl"/>
</p:input>
</p:xslt>
<p:xslt name="subjects-2">
<p:input port="stylesheet">
<p:document href="preprocessors/subjects-2.xsl"/>
</p:input>
</p:xslt>
<p:xslt name="types-1">
<p:input port="stylesheet">
<p:document href="preprocessors/types-1.xsl"/>
</p:input>
</p:xslt>
<p:xslt name="types-2">
<p:input port="stylesheet">
<p:document href="preprocessors/types-2.xsl"/>
</p:input>
</p:xslt>
<p:xslt name="core">
<p:input port="stylesheet">
<p:document href="preprocessors/core.xsl"/>
</p:input>
</p:xslt>
<p:xslt name="consolidate-descriptions">
<p:input port="stylesheet">
<p:document href="preprocessors/consolidate-descriptions.xsl"/>
</p:input> …Run Code Online (Sandbox Code Playgroud) 我正在使用GAE构建我的第一个应用程序,以允许用户进行选举,并为每次选举创建一个选举实体.
为了避免存储太多数据,我想在一段时间后自动删除选举实体 - 比如选举结束后三个月.是否可以在GAE中自动执行此操作?或者我需要手动执行此操作吗?
如果重要,我正在使用Python界面.
我正在为Emacs 23尝试新的Python模式,但我不知道如何判断我是使用新模式(源代码位于~/.elisp/python.el)还是捆绑python.el模式.
有没有办法找出当前(或任何活动)模式的加载位置?C-h m似乎没有提供这些信息,我不知道在哪里可以看.
我遇到了这个问题,我制作了一个HTML数组,但我无法用Python读出来.甚至可以在App Engine中执行此操作吗?我在PHP中读到它是可能的.
这是html代码:
<label for="hashtags">Hashtags: </label><br/>
{% for hashtag in stream.hashtags %}
<input type="text" value="{{hashtag}}" name="hashtags[]" id="hashtags" class="text ui-widget-content ui-corner-all" />
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
这就是我目前正在尝试阅读HTML数组的方法:
newHashTags = self.request.get('hashtags[]')
for newHashTag in newHashTags:
stream.hashtags.append(newHashTag)
Run Code Online (Sandbox Code Playgroud)
当我调试时,这是在post变量中.
MultiDict: MultiDict([('streamid', '84'), ('name', 'Akteurs'), ('description', '#stream'), ('hashtags[]', '#andretest'), ('hashtags[]', '#saab')])
Run Code Online (Sandbox Code Playgroud) python ×4
html ×2
comparison ×1
conventions ×1
css ×1
emacs ×1
function ×1
inspect ×1
javascript ×1
mode ×1
path ×1
pipeline ×1
python-mode ×1
saxon ×1
thumbnails ×1
types ×1
xml ×1
xproc ×1