前段时间我学会了rsync删除文件比许多其他工具快得多.
几天前,我在Serverfault上遇到了这个精彩的答案,这解释了为什么rsync删除文件非常好.
从那个答案的报价:
我今天重访了这个,因为大多数文件系统以btree格式存储它们的目录结构,删除文件的顺序也很重要.当您执行取消链接时,需要避免重新平衡btree.因此我在删除之前添加了一个排序.
你能解释一下如何按顺序删除文件来阻止或减少btree重新计算的数量?
我期望答案显示如何删除以增加删除速度,详细说明btree级别发生的事情.写作的rsync人和另一个程序(参见问题中的链接)使用这些知识来创建更好的程序.我认为让其他程序员有这种理解能够写出更好的软件是很重要的.
要进行JDBC查询,我需要将日期传递给它.日期保存在PostgreSql数据库的Date字段类型中,表示特定日期没有任何时间.
因为我只需要约会,所以我决定使用特定的对象,它只代表没有时间的日期,这是LocalDate来自Joda-Time包.我认为这很重要,因为如果我使用DateTime对象,它将携带冗余时间数据,并且当时钟向后放置一小时时可能导致夏令时结束时的错误(尽管情况空前罕见,但不是不可能).
但是当我开始尝试LocalDate使用preparedStatement.setDate方法的公认参数来对象时,我找不到合适的方法来做到这一点.
setDate接受java.sql.Date作为参数.构造java.sql.Date对象的唯一选择是以毫秒为单位传递时间.
但是这违背了使用LocalDateJoda-Time软件包的所有目的,因为在这个转换中我们会回到毫秒,并且在这些转换发生时,时钟可能会被放回一小时并将日期更改为上一个日期.
所以,现在我的代码中有这一行:
preparedStatement.setDate(1, new java.sql.Date(localDate.toDate().getTime()));
Run Code Online (Sandbox Code Playgroud)
但这是转换LocalDate为setDate格式接受的最佳方式吗?
我的担忧是否与夏令时和相应的时钟转换相关?
有没有更好的方法将日期(并且只有没有时间的日期)传递给JDBC preparedStatement?
有没有什么办法可以将const关键字添加到作为参数传递的数组中来运行:
void foo(char arr_arg[])
Run Code Online (Sandbox Code Playgroud)
如果我放在()const之前或之后(),那将意味着它不是常数,而不是.charvoid foo(const char arr_arg[])charvoid foo(char const arr_arg[])chararr_arg
我刚刚读到,在引擎盖下,作为参数发送的数组被表示为指针,因此void foo(char arr_arg[])也是如此void foo(char* ptr_arg).考虑到这一点,我可能会重写函数,void foo(char * const ptr_arg)因为它正是我想要实现的.
但是我想知道是否有一种方法可以const在此声明中添加关键字,void foo(char arr_arg[])使其与void foo(char * const ptr_arg)(而不是 void foo(char const * ptr_arg)或void foo(const char * ptr_arg))相同?
我只是想了解是否存在arr_arg使用数组表示法使常量变为常量的语法[].
I'm using python 3.2.3 IDLE and this is my code:
originalList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, …Run Code Online (Sandbox Code Playgroud) 阅读文档我遇到了以下段落:
范围定义块内名称的可见性.如果在块中定义了局部变量,则其范围包括该块.如果定义发生在功能块中,则作用域将扩展到定义块中包含的任何块,除非包含的块为名称引入了不同的绑定.类块中定义的名称范围仅限于类块; 它没有扩展到方法的代码块 - 这包括了解和生成器表达式,因为它们是使用函数作用域实现的.
我决定尝试自己从方法中访问类变量:
>>> class A():
i = 1
def f(self):
print(i)
>>> a = A()
>>> a.i
1
>>> a.f()
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
a.f()
File "<pyshell#4>", line 4, in f
print(i)
NameError: global name 'i' is not defined
Run Code Online (Sandbox Code Playgroud)
我知道i可以通过显式指向类名来访问该变量A.i:
>>> a = A()
>>> class A():
i = 1
def f(self):
print(A.i)
>>> a = A()
>>> a.f()
1
Run Code Online (Sandbox Code Playgroud)
问题是为什么该语言的开发人员使方法中的类变量不可见?它背后的理由是什么?
在阅读Python re模块的文档时,我决定查看re.py源代码.
当我打开它时,我发现了这个:
_cache = {}
_MAXCACHE = 100
def _compile(*key):
cachekey = (type(key[0]),) + key
p = _cache.get(cachekey)
if p is not None:
return p
#...Here I skip some part of irrelevant to the question code...
if len(_cache) >= _MAXCACHE:
_cache.clear()
_cache[cachekey] = p
return p
Run Code Online (Sandbox Code Playgroud)
为什么_cache.clear()在达到_MAXCACHE条目时使用缓存清除?
完全清除缓存并从头开始是否常见?
为什么只是没用过最久前的兑现值被删除?
回答这个问题我遇到了一个有趣的情况2类似的代码片段表现完全不同.我在这里只是要了解其原因,并提高我对此类案例的直觉.
我将改编Python 2.7的代码片段(在Python 3中,性能差异是相同的).
from collections import OrderedDict
from operator import itemgetter
from itertools import izip
items = OrderedDict([('a', 10), ('b', 9), ('c', 4), ('d', 7), ('e', 3), ('f', 0), ('g', -5), ('h', 9)])
def f1():
return min(items, key=items.get)
def f2():
return min(items.iteritems(), key=itemgetter(1))[0]
from timeit import Timer
N = 100000
print(Timer(stmt='f1()', setup='from __main__ import f1').timeit(number = N))
print(Timer(stmt='f2()', setup='from __main__ import f2').timeit(number = N))
Run Code Online (Sandbox Code Playgroud)
输出:
0.603327797248
1.21580172899
Run Code Online (Sandbox Code Playgroud)
第一个解决方案必须进行查找OrderedDictionary以获取value每个key.第二种解决方案只是遍历OrderedDictionary键值对,它们必须打包成元组.
第二种解决方案慢2倍. …
有几种方法可以打破几个嵌套循环
他们是:
1)使用break-continue
for x in xrange(10):
for y in xrange(10):
print x*y
if x*y > 50:
break
else:
continue # only executed if break was not used
break
Run Code Online (Sandbox Code Playgroud)
2)使用退货
def foo():
for x in range(10):
for y in range(10):
print x*y
if x*y > 50:
return
foo()
Run Code Online (Sandbox Code Playgroud)
3)使用特殊例外
class BreakIt(Exception): pass
try:
for x in range(10):
for y in range(10):
print x*y
if x*y > 50:
raise BreakIt
except BreakIt:
pass
Run Code Online (Sandbox Code Playgroud)
我有一些想法,可能还有其他方法可以做到这一点.它是通过使用StopIteration异常直接发送到外部循环的.我写了这段代码
it = iter(range(10))
for i in it: …Run Code Online (Sandbox Code Playgroud) 在Python 2.x中,我能够这样做:
>>> '4f6c6567'.decode('hex_codec')
'Oleg'
Run Code Online (Sandbox Code Playgroud)
但是在Python 3.2中我遇到了这个错误:
>>> b'4f6c6567'.decode('hex_codec')
Traceback (most recent call last):
File "<pyshell#25>", line 1, in <module>
b'4f6c6567'.decode('hex_codec')
TypeError: decoder did not return a str object (type=bytes)
Run Code Online (Sandbox Code Playgroud)
根据文档 hex_codec应提供"字节到字节的映射".所以这里正确使用了字节串的对象.
我怎样才能摆脱这个错误,以避免从十六进制编码的文本转换笨拙的变通方法?
在这里,我问了一个关于不工作查询的问题.
无意中(在一个答案的帮助下)我找到了如何使解决方案正确.问题是我不明白他们为什么会产生不同的结果.
所以,数据库有这个架构:

而我在寻找一切从模型PC,Printer并Laptop与最高价格.所有这些表可能具有非唯一model列,因为具有不同的项code可能具有相同的模型.
我最初的解决方案是:
with model_price(model,price) as (
select model,price
from PC
union
select model,price
from Laptop
union
select model,price
from Printer
)
select model
from model_price
where price >= all(select price from model_price)
Run Code Online (Sandbox Code Playgroud)
它给出了错误的结果 - 系统返回* Wrong number of records (less by 2).
有效的纠正解决方案是这样的:
with model_price(model,price) as (
select model,price
from PC
union
select model,price
from Laptop
union
select model,price
from Printer
) …Run Code Online (Sandbox Code Playgroud)