该功能是找出密码有多强.如果:
有没有办法减少函数中的代码量?请帮我制作短于200个字符的函数代码(尽量解决而不给变量赋值)
import re
def golf(password):
if len(password) >= 10 \
and re.search("^[a-zA-Z0-9]+", password) \
and re.search("[a-z]+", password) \
and re.search("[A-Z]+", password) \
and re.search("[0-9]+", password):
print(password, True)
return True
else:
print(password, False)
return False
if __name__ == '__main__':
golf('A1213pokl') == False
golf('bAse730onE') == True
golf('asasasasasasasaas') == False
golf('QWERTYqwerty') == False
golf('123456123456') == False
golf('QwErTy911poqqqq') == True
golf('..........') == False
Run Code Online (Sandbox Code Playgroud) PowerShell能够提取1492条记录的列表。当我将ldap3模块与Python配合使用时,我的记录数已达到1000条。请帮助我将Python代码更改为超出限制。
PowerShell输入: get-aduser -filter * -SearchBase "OU=SMZ USERS,OU=SMZ,OU=EUR,DC=my_dc,DC=COM" | Measure-Object
输出:计数:1492平均:总和:最大值:最小值:属性:
import json
from ldap3 import Server, \
Connection, \
AUTO_BIND_NO_TLS, \
SUBTREE, \
ALL_ATTRIBUTES
def get_ldap_info(u):
with Connection(Server('my_server', port=636, use_ssl=True),
auto_bind=AUTO_BIND_NO_TLS,
read_only=True,
check_names=True,
user='my_login', password='my_password') as c:
c.search(search_base='OU=SMZ Users,OU=SMZ,OU=EUR,DC=my_dc,DC=com',
search_filter='(&(samAccountName=' + u + '))',
search_scope=SUBTREE,
attributes=ALL_ATTRIBUTES,
size_limit = 0,
paged_criticality = True,
paged_size = None,
#attributes = ['cn'],
get_operational_attributes=True)
content = c.response_to_json()
result = json.loads(content)
i = 0
for item in result["entries"]:
i += 1
print(i)
get_ldap_info('*')
Run Code Online (Sandbox Code Playgroud) 此代码 ping 各种机器。您能否帮我更改此代码,以便如果 ping 进程挂起超过 7 秒,它会关闭并返回一些标志?
(我想从使用 WMI 的机器中提取各种数据。为此,我将 ping 功能更改为其他功能。问题是在某些机器上 WMI 已损坏并且提取数据的过程无限期挂起。需要超时。)
import multiprocessing.dummy
import subprocess
import numpy as np
import time
start_time = time.time()
def ping(ipadd):
try:
response = subprocess.check_output(['ping', ipadd])
return True
except subprocess.CalledProcessError as e:
return False
#print(ping('10.25.59.20'))
machine_names = \
'''
ya.ru
microsoft.com
www.google.com
www.amazon.com
www.nasa.com
'''.split()
np_machine_names = np.array(machine_names)
p = multiprocessing.dummy.Pool(7)
ping_status = p.map(ping, machine_names)
np_ping_status = np.fromiter(ping_status, dtype=bool)
print(*np_machine_names[np_ping_status], sep = '\n')
run_time = time.time() - start_time
print(f'Runtime: {run_time:.0f}')
Run Code Online (Sandbox Code Playgroud)
更新:虽然我很欣赏关于添加超时到子进程的提示,但问题仍然存在。如何关闭挂起的功能?假设我已将 …
我想要打印出现不止一次的任何数字.如何将for循环更改为列表推导?
from collections import Counter
cnt=Counter()
in1="4 8 0 3 4 2 0 3".split(" ")
for elt in in1:
cnt[elt]+=1
more_than_one=[]
for value, amount in cnt.items():
if amount > 1: more_than_one.append(value)
print(*more_than_one)
Run Code Online (Sandbox Code Playgroud)
理想输出:4 0 3
我想使用 Get-ChildItem 查询目录,并创建一个包含 Path、Size(以 GB 为单位)、MinimumCreationTime、MaximumCreationTime 等列的表。在 foreach 循环中我写了 3 个 Measure 命令。是否可以用一个命令测量多个属性?
$pathes = @'
C:\open
C:\games
'@.Split([System.Environment]::NewLine, [System.StringSplitOptions]::RemoveEmptyEntries)
foreach ($path in $pathes){
Get-ChildItem $path -Recurse | Measure Length -Sum
Get-ChildItem $path -Recurse | Measure CreationTime -Minimum
Get-ChildItem $path -Recurse | Measure CreationTime -Maximum
}
Run Code Online (Sandbox Code Playgroud)