我在使用re.sub. 下面的工作正常。
dt1 = "2026-12-02"
pattern = re.compile(r'(?P<year>\d{4})-(?P<month>\d{1,2})-(?P<day>\d{1,2})')
m = pattern.match(dt1)
print(m.group('year'))
print(m.group('month'))
print(m.group('day'))
repl = '\\3-\\2-\\1'
print(re.sub(pattern, repl, dt1))
Run Code Online (Sandbox Code Playgroud)
输出是
02-12-2026
我的查询不是使用组号,我们可以使用组名作为:\day-\month-\year
import _winreg as registry
key=registry.OpenKey(registry.HKEY_CURRENT_USER,"Software\Microsoft\Windows\CurrentVersion\Internet Settings",0,registry.KEY_ALL_ACCESS)
proxy=proxy_server+":"+proxy_port
registry.SetValue(key, 'ProxyEnable', registry.REG_SZ, 'dword:00000001')
registry.SetValue(key, 'ProxyServer', registry.REG_SZ, proxy)
Run Code Online (Sandbox Code Playgroud)
我正在使用上面的代码来设置代理。但它没有在 Internet 设置中添加新密钥。而是创建一个新文件夹,并使用提供的值放入一个名为 default 的密钥。

任何人都可以帮我解决这个问题。真的卡住了
看看这段代码吧:
import re
ti = "abcd"
tq = "abcdef"
check_abcd = re.compile('^abcd')
print id(check_abcd.search(ti))
print id(check_abcd.search(tq))
print check_abcd.search(ti)
print check_abcd.search(tq)
if check_abcd.search(ti) is check_abcd.search(tq):
print "Matching"
else:
print "not matching"
Run Code Online (Sandbox Code Playgroud)
输出:
41696976
41696976
<_sre.SRE_Match object at 0x00000000027C3ED0>
<_sre.SRE_Match object at 0x00000000027C3ED0>
not matching
Run Code Online (Sandbox Code Playgroud)
定义is:
`is` is identity testing, == is equality testing.
is will return True if two variables point to the same object
Run Code Online (Sandbox Code Playgroud)
1)现在为什么当id和对象引用相同时is不返回True.
2)当is被替换时==它仍然返回false.是比较对象使用时的预期行为==.
我有一个函数让我们说def temp.我正在以下面的方式嘲笑它:
msg = "Mocked!!!!!!!"
@mock.patch.object(Someothermodule.Someclass,'Somefunction',create=True,side_effect=Error(Error.something,msg))
def temp(self,mock_A):
Run Code Online (Sandbox Code Playgroud)
它是一种巨大的项目,所以斜面发布细节here.But发生了什么是功能temp确实得到嘲笑,我也得到正确的消息,但我后来得到stop called on unstarted patcher和程序在这里fails.Is有什么解决办法,这在某种程度上禁止_exit的mock或任何其他方法.我猜测内容在某种程度上不足以创建整个场景,但这是我能做的最好的.
此外,如果我不提供mock_A功能会发生什么def temp.在这种情况下补丁工作的是什么?
编辑:
我有一个解决方法,其中我已定义我的补丁如下:
@mock.patch.object(Someothermodule.Someclass,'Somefunction',create=True,some_function)
def temp(self):
Run Code Online (Sandbox Code Playgroud)
现在的问题是
1)当我使用side_effect或return_value我必须提供一个mock_object装饰后面的功能.
2)当我刚使用函数代替时side_effect,我不需要在装饰器后面的函数中提供mock_object.
所以,
When we dont pass mock_obj,do all the function know about the patch?how exactly is this working?What is the difference between the scenario wherein we have to explicitly pass mock_object and wherein we dont have …
我最近开始在python中使用多处理,我有以下代码来更新多个进程的列表项.但它正在给出空列表.
from multiprocessing import Pool
import time
global_list = list()
def testfun(n):
print('started ', n)
time.sleep(1)
global_list.append(n)
print('completed ', n)
def call_multiprocessing_function():
mytasks = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n']
with Pool() as pool:
pool.map(testfun, mytasks)
if __name__ == "__main__":
print('starting the script')
print(global_list)
call_multiprocessing_function()
print(global_list)
print('completed the script')
Run Code Online (Sandbox Code Playgroud)
我得到以下输出
starting the script
[]
started a
started b
started c
started d
completed a
started e
completed b
started f
completed c …Run Code Online (Sandbox Code Playgroud) 根据我的理解,
对于像列表这样的可变对象,如果底层对象支持像列表这样的变异,则 +=(增强赋值)会导致对象发生变异。当然,这不适用于不支持突变的不可变对象,因为__iadd__它不存在并且__add__是被调用的对象。
考虑以下代码:
class TestClass:
bar = []
def __init__(self, x):
self.bar += [x]
f = TestClass(1)
g = TestClass(2)
h = TestClass(3)
print(f.bar)
print(g.bar)
print(TestClass.bar)
Run Code Online (Sandbox Code Playgroud)
现在,我明白为什么所有三个 g,f,h 都会打印出来,[1, 2, 3]因为我们正在“改变”类属性。
但是当我尝试通过执行来查看对象的命名空间时 print(f.__dict__)查看,我也看到了对象内部的 bar 属性。
问题 1:如果发生变异的实际属性是类属性,为什么在对象命名空间中创建实例?
其次,“如果”在对象中创建了该实例变量(如 f),那么f.bar += 4应该只影响对象 f,因为 f 中的新实例变量 bar 应该遮蔽了类属性。但是我看到f.bar+=4反过来这样做也会更新 class 属性,这让我感到非常困惑。
有人可以帮我解决这个问题吗?
我不相信我设置正确...因为无论我为foo()填写什么数字,它似乎总是返回"True".我究竟做错了什么??
# Complete the following function.
# Returns True if x * y / z is odd, False otherwise.
def foo(x, y, z):
answer = True
product = (x * y) / z
if (product%2) == 0:
answer = False
return answer
print(foo(1,2,3))
Run Code Online (Sandbox Code Playgroud) 我有str格式的输出
Port Name Intvl In Mbps % In Kpps Out Mbps % Out Kpps
Et7/1 5:00 338.2 3.4% 31 0.0 12.3% 121
Run Code Online (Sandbox Code Playgroud)
我需要确定所有百分比,即3.4和12.3.做同样事情的最pythonic方法是什么
find_all with % just给出了索引,有没有更好的方法来实现呢?
假设我们有以下结构:
class A():
class __A():
def __to_be_mocked(self):
#something here
def __init__(self):
with A.lock:
if not A.instance:
A.instance = A.__A()
def __getattr__(self,name):
return getattr(self.instance,name)
Run Code Online (Sandbox Code Playgroud)
现在我们要模拟函数。__to_be_mocked我们如何模拟它作为接受的目标mock.patch.object是package.module.ClassName。我已经尝试了所有方法,例如
target = A.__A
target = A.___A
Run Code Online (Sandbox Code Playgroud)
还有很多。
编辑:
我解决了它使用
target=A._A__A and attribute as '_A__to_be_mocked`
Run Code Online (Sandbox Code Playgroud)
现在的问题是__to_be_mocked在里面,__A所以不应该是___A__to_be_mocked。
是因为setattributeinA还是__init__in A?
使用portalocker我们可以通过以下方式锁定文件以进行访问:
f=open("M99","r+")
portalocker.lock(f,portalocker.LOCK_EX)
Run Code Online (Sandbox Code Playgroud)
将lock在该文件可以使用删除
f.close() #or
portalocker.unlock(file) #needs `file` ie reference to file it locked ..pretty obvious too
Run Code Online (Sandbox Code Playgroud)
可以通过python中的任何其他方式完成同样的事情
我们可以锁定文件以进行访问
重新启动Python(因此不再具有原始Python文件对象或文件编号).
解锁文件以便在新进程中进行访问.
我无法保存f或file object不能使用pickle或其他任何东西.有没有办法使用Python标准库或一些win32api调用?
任何Windows实用程序也会执行...来自Windows的任何命令行?
python ×10
python-3.x ×4
mocking ×2
python-2.7 ×2
regex ×2
unit-testing ×2
patch ×1
python-2.x ×1
registry ×1
windows ×1
winreg ×1