所以我一直在做一些挖掘,我一直在努力拼凑一个在PHP中生成有效的v4 UUID的函数.这是我能够来的最接近的.我在十六进制,十进制,二进制,PHP的按位运算符等方面的知识几乎不存在.此函数生成有效的v4 UUID直到一个区域.v4 UUID应采用以下形式:
xxxxxxxx-xxxx- 4 xxx- y xxx-xxxxxxxxxxxx
其中y 是8,9,A或B.这是函数失败的地方,因为它不符合它.
我希望在这个领域有比我更多知识的人可以帮助我解决这个问题并帮助我解决这个问题.
功能如下:
<?php
function gen_uuid() {
$uuid = array(
'time_low' => 0,
'time_mid' => 0,
'time_hi' => 0,
'clock_seq_hi' => 0,
'clock_seq_low' => 0,
'node' => array()
);
$uuid['time_low'] = mt_rand(0, 0xffff) + (mt_rand(0, 0xffff) << 16);
$uuid['time_mid'] = mt_rand(0, 0xffff);
$uuid['time_hi'] = (4 << 12) | (mt_rand(0, 0x1000));
$uuid['clock_seq_hi'] = (1 << 7) | (mt_rand(0, 128));
$uuid['clock_seq_low'] = mt_rand(0, 255);
for ($i = 0; $i < 6; …Run Code Online (Sandbox Code Playgroud) 我怎样才能使@functools.lru_cache装饰器忽略一些关于缓存键的函数参数?
例如,我有一个如下所示的函数:
def find_object(db_handle, query):
# (omitted code)
return result
Run Code Online (Sandbox Code Playgroud)
如果我lru_cache像这样应用装饰器,db_handle将包含在缓存键中.因此,如果我尝试使用相同query但不同的函数调用该函数db_handle,它将再次执行,我想避免.我只想lru_cache考虑query参数.
当您实例化生成器函数时,它将不会执行任何代码,直到您调用next它.
这意味着如果生成器函数包含某种初始化代码,则在迭代之前不会执行它.
考虑这个例子:
def generator(filename):
with open(filename) as f:
data = f.read()
while True:
yield data
gen = generator('/tmp/some_file')
# at this point, no generator code is executed
# initialization code is executed only at the first iteration
for x in gen:
pass
Run Code Online (Sandbox Code Playgroud)
如果该文件不存在,则会在for循环中引发异常.我想yield在生成器迭代之前执行第一个代码之前的代码,因此初始化期间的任何异常都将在生成器实例化时引发.
有一种干净的pythonic方式吗?
在Linux上,除非我弄错了,否则应用程序可以使用套接字调用系列在数据报传输上一次发送或接收一个数据包.
想知道Linux是否为应用程序提供了一种在数据报传输的单个调用中发送和接收多个数据包的方法.
由于某种原因,该程序打印以下警告:
Task exception was never retrieved
future: <Task finished coro=<coro() done, defined at /usr/lib64/python3.4/asyncio/coroutines.py:139> exception=SystemExit(2,)>
Run Code Online (Sandbox Code Playgroud)
即使异常被清楚地检索和传播,如caught SystemExit!打印到终端,并且进程状态代码变为2.
Python 2和trollius也是如此.
我错过了什么吗?
#!/usr/bin/env python3
import asyncio
@asyncio.coroutine
def comain():
raise SystemExit(2)
def main():
loop = asyncio.get_event_loop()
task = loop.create_task(comain())
try:
loop.run_until_complete(task)
except SystemExit:
print("caught SystemExit!")
raise
finally:
loop.close()
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud) 假设我有两个像这样工作的函数:
@tornado.gen.coroutine
def f():
for i in range(4):
print("f", i)
yield tornado.gen.sleep(0.5)
@tornado.gen.coroutine
def g():
yield tornado.gen.sleep(1)
print("Let's raise RuntimeError")
raise RuntimeError
Run Code Online (Sandbox Code Playgroud)
通常,函数f可能包含无限循环并且永不返回(例如,它可以处理某个队列)。
我想要做的是能够在它产生的任何时候中断它。
最明显的方法不起作用。异常仅在函数f退出后引发(如果它是无穷无尽的,它显然永远不会发生)。
@tornado.gen.coroutine
def main():
try:
yield [f(), g()]
except Exception as e:
print("Caught", repr(e))
while True:
yield tornado.gen.sleep(10)
if __name__ == "__main__":
tornado.ioloop.IOLoop.instance().run_sync(main)
Run Code Online (Sandbox Code Playgroud)
输出:
f 0
f 1
Let's raise RuntimeError
f 2
f 3
Traceback (most recent call last):
File "/tmp/test/lib/python3.4/site-packages/tornado/gen.py", line 812, in run
yielded = self.gen.send(value)
StopIteration
During handling …Run Code Online (Sandbox Code Playgroud)