所以我有一些使用Jakarta HttpClient的Java代码:
URI aURI = new URI( "http://host/index.php?title=" + title + "&action=edit" );
GetMethod aRequest = new GetMethod( aURI.getEscapedPathQuery());
Run Code Online (Sandbox Code Playgroud)
问题是如果title包含任何&符号(&),它们被认为是参数分隔符,请求变得棘手......如果我用URL转义的等价物替换它们%26,那么getEscapedPathQuery()会将其双重转义%2526.
我目前正在通过基本修复损坏来解决这个问题:
URI aURI = new URI( "http://host/index.php?title=" + title.replace("&", "%26") + "&action=edit" );
GetMethod aRequest = new GetMethod( aURI.getEscapedPathQuery().replace("%2526", "%26"));
Run Code Online (Sandbox Code Playgroud)
但是必须有一个更好的方法来做到这一点,对吧?请注意,标题可以包含任意数量的不可预测的UTF-8字符等,因此必须转义其他所有字符.
我有一个由一堆图标,我想呈现为单个位图(GIF,PNG,等等)在Web上使用其自定义TrueType字体(TTF).你认为这是一项简单的任务,但显然不是吗?这里有大量与TTF相关的软件:
http://cg.scs.carleton.ca/~luc/ttsoftware.html
但它是所有不同级别的"不是我想要的",断开链接和/或很难在现代Ubuntu盒子上编译 - 例如.dumpglyphs(C++)和ttfgif(C)由于隐藏的缺失依赖性而无法编译.有任何想法吗?
所以我有一些代码,大致简化,看起来像这样:
class B
def initialize opts
@opts = opts
end
end
class A
def initialize opts
# defaults etc applied to opts
@b = B.new opts
end
end
Run Code Online (Sandbox Code Playgroud)
换句话说,当我使用选项初始化A时,它会创建一个B并将一组修改过的选项传递给它.
我想测试B.new得到正确的论点.现在,我正在这样做,使用RSpec/RR:
@b = Object.new
# stub methods on @b here
stub(B).new { |options|
options[:foo].should == 'whatever'
@b
}
A.new({:foo => 'whatever'})
Run Code Online (Sandbox Code Playgroud)
但这有两个问题.
首先,我无法B使用实际选项实例化实际副本.如果我在块中调用B.new,它会调用存根版本并循环直到堆栈弹出.我可以@b = B.new在存根之前设置,但我不知道将要传递的选项,从而击败测试点.
(并且在有人打电话给我之前:是的,在严格的单元测试教条中,A的测试应该在B中删除任何方法,并且需要大量存根意味着你的代码首先是坏的.)
其次,should在测试设置中,而不是在it ... do ... end之后的单独块中,感觉是错误的.但由于我不能创造一个实际的B(见上文),我也无法真正询问它的建设后状态.
有任何想法吗?
我们将一大堆ArcGIS shapefile导入到PostGIS中,随时随地转换shp2pgsql.问题是,如果shapefile有任何环自交叉,则导入扼流圈:
NOTICE: Ring Self-intersection at or near point -80.1338 25.8102
ERROR: new row for relation "place_shapes" violates
check constraint "shape_is_valid"
Run Code Online (Sandbox Code Playgroud)
我们该如何解决这个问题?
这应该很简单,但是Python的日常时间过于复杂,使得简单的事情变得复杂......
所以我有一个HH:MM格式的时间字符串(例如'09:30'),我想把它变成今天日期的日期时间.不幸的是,默认日期是1900年1月1日:
>>> datetime.datetime.strptime(time_str, "%H:%M")
datetime.datetime(1900, 1, 1, 9, 50)
Run Code Online (Sandbox Code Playgroud)
datetime.combine看起来就像是这个意思,但如果我能弄清楚如何解析时间以便接受它,我会很高兴:
now = datetime.datetime.now()
>>> datetime.datetime.combine(now, time.strptime('09:30', '%H:%M'))
TypeError: combine() argument 2 must be datetime.time, not time.struct_time
>>> datetime.datetime.combine(now, datetime.datetime.strptime('09:30', '%H:%M'))
TypeError: combine() argument 2 must be datetime.time, not datetime.datetime
>>> datetime.datetime.combine(now, datetime.time.strptime('09:30', '%H:%M'))
AttributeError: type object 'datetime.time' has no attribute 'strptime'
Run Code Online (Sandbox Code Playgroud)
这种怪物有效......
>>> datetime.datetime.combine(now,
datetime.time(*(time.strptime('09:30', '%H:%M')[3:6])))
datetime.datetime(2014, 9, 23, 9, 30)
Run Code Online (Sandbox Code Playgroud)
......但必须有更好的方法来做到这一点......!?
所以我们有一个遗留系统跟踪ID为"欧洲/法国/巴黎"的地方,我正在构建一个Rails外观,将其转换为http:// foobar/places/Europe/France/Paris等URL.这个要求是不可协商的,可能的级别数量无限制,而且我们无法逃避斜线.
为http:// foobar/places/Europe设置routes.rb 是微不足道的:
map.resources :places
Run Code Online (Sandbox Code Playgroud)
...但是http:// foobar/places/Europe/France抱怨"没有行动回应欧洲".我试过了:
map.connect '/places/:id', :controller => 'places', :action => 'show'
Run Code Online (Sandbox Code Playgroud)
...但是这给出了相同的结果,显然:id结束于第一个'/'.如何使ID覆盖"地点"之后的任何内容和所有内容?
我正在尝试将Ruby 1.8应用程序升级到1.9并在此处遇到一些障碍.在Ruby 1.8.7中,我可以将一个块传递给Builder 3.0.0并按预期调用它:
1.8.7 :003 > @builder = Builder::XmlMarkup.new
1.8.7 :004 > block = lambda { puts "foo" }
1.8.7 :005 > @builder.tag(&block)
foo
Run Code Online (Sandbox Code Playgroud)
但在1.9中,我收到此错误:
1.9.3p0 :002 > @builder = Builder::XmlMarkup.new
1.9.3p0 :003 > block = lambda { puts "foo" }
1.9.3p0 :004 > @builder.content(&block)
ArgumentError: wrong number of arguments (1 for 0)
from (irb):3:in `block in irb_binding'
from /Users/dev/.bundle/ruby/1.9.1/gems/builder-3.0.0/lib/builder/xmlbase.rb:155:in `call'
...
Run Code Online (Sandbox Code Playgroud)
并重写为一个stabby lambda(这只是语法糖,对吗?)没有帮助:
1.9.3p0 :006 > block = -> { puts "foo" }
1.9.3p0 :007 > @builder.content(&block)
ArgumentError: …Run Code Online (Sandbox Code Playgroud) 如果我有一个Nokogiri :: XML :: Element,我如何计算其子索引与其父级相关?那是:
<foo> <-- parent
<bar/> <-- 1st child
<bar/> <-- 2nd child
</foo>
Run Code Online (Sandbox Code Playgroud)
在Javascriptland中,jQuery有index(),但Nokogiri没有.引入nokogiri确实提供了一个路径的方法,但返回的XPath字符串像"/foo/bar[2]"和截断bar[1],以bar引导,使转弯,早成若干有点毛毛:
element.path.split('/').last.slice(/[0-9]+/) || '1' # quick'n'dirty
element.path.split('/').last.slice(/\[([0-9]+)\]/, 1) || '1' # a bit safer
Run Code Online (Sandbox Code Playgroud) 所以我有一个带有专栏的网页.该列顶部有一个固定的标题,还有一个大的滚动主体部分,下面的HTML/CSS可以正常工作.
问题:我想在主体的底部添加填充,所以如果你一直向下滚动,你会得到一些空白,而不是让所有东西都堵塞到底部边缘.添加padding-bottom: 50px到.bodyChrome中完美运行; 但是,在Firefox中,似乎使用该bottom属性意味着padding-bottom被忽略.如果删除bottom,则会显示填充,但滚动条会消失.
的test.html
<link type="text/css" rel="stylesheet" media="all" href="/test.css">
<div class="column">
<div class="header">
Fixed header
</div>
<div class="body">
<h1>Body</h1>
Where's the bottom padding?
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
test.css
.column {
position: absolute;
height: 100px;
width: 100%;
background-color: green;
}
.header {
position: absolute;
height: 30px;
background-color: red;
}
.body {
position: absolute;
top: 30px;
bottom: 0;
overflow-y: auto;
background-color: blue;
padding-bottom: 50px; /* Broken in Firefox */
}
Run Code Online (Sandbox Code Playgroud)
JSFiddle上面的内容:http …
我想修剪XML中p标签内的前导空格,所以这个:
<p> Hey, <em>italics</em> and <em>italics</em>!</p>
Run Code Online (Sandbox Code Playgroud)
变成这样:
<p>Hey, <em>italics</em> and <em>italics</em>!</p>
Run Code Online (Sandbox Code Playgroud)
(修剪尾随空格不会受到伤害,但并非强制性.)
现在,我知道normalize-whitespace()应该这样做,但如果我尝试将它应用于文本节点..
<xsl:template match="text()">
<xsl:text>[</xsl:text>
<xsl:value-of select="normalize-space(.)"/>
<xsl:text>]</xsl:text>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
...它分别应用于每个文本节点(在括号中)并将它们吸干:
[Hey,]<em>[italics]</em>[and]<em>[italics]</em>[!]
Run Code Online (Sandbox Code Playgroud)
我的XSLT看起来基本上是这样的:
<xsl:template match="p">
<xsl:apply-templates/>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
那么有什么方法可以让apply-templates完成,然后在输出上运行normalize-space,这应该做对了吗?
我有两个包含工作日"位图"的字符串:例如,1------仅周一,--3-5--周三和周五,你明白了.
我想将这些与逻辑OR的等价物合并在一起,例如.1------|| --3-5--= 1-3-5--.最恐怖的方法是什么?我现在正在这样做,但感觉不是特别优雅:
week1, week2, merged = '1------', '--3-5--', ''
for i in range(0, len(week1)):
merged += week1[i] if week1[i] != '-' else week2[i]
print merged
Run Code Online (Sandbox Code Playgroud)
请注意,我使用的是Python 2.7,因此没有可变字符串,并且可以安全地假设输入字符串始终是正确格式化的(=始终-用于false并且1..7在正确的位置用于true).
ruby ×3
bitmap ×2
python ×2
builder ×1
constructor ×1
css ×1
datetime ×1
escaping ×1
firefox ×1
gis ×1
hierarchy ×1
http ×1
import ×1
java ×1
lambda ×1
layout ×1
nokogiri ×1
padding ×1
parent-child ×1
postgis ×1
routing ×1
rr ×1
rspec ×1
ruby-1.9 ×1
shapefile ×1
string ×1
strptime ×1
stubbing ×1
time ×1
trim ×1
truetype ×1
url ×1
whitespace ×1
xml ×1
xslt ×1