你说,你“对可能设立的分支机构数量之多感到过重”,并询问如何“确保它彻底检查所有可能的分支”。
这完全是提出休养解决办法的。 它有助于通过使用相对简短和简单编码的非常庞大的收集系统进行搜索,如果你写好的话,最终将涵盖所有这些收集。
也有 down,这在一定程度上解释了为什么你想要在这里进行深入的首例搜查,以避免首先下载许多组织的信息,而是尽快检查解决办法,因为你们都只想知道父母是否拥有美国人。 作为父母,而不是所有父母,或者人数。
在你的具体情况下,另一个缩小的方面可能是,许多组织可能有相同的母子,但你的做法将每次下载数据。 因此,你可能想记住你所下载的任何东西,如果你第二次进入,那就会出现这种情况(它获得了美国电力公司)。 否则,你就已经做过。
这也有助于避免在你试图改进的法典中出现错误。 你的法典有一字:
# needs ==, not =
if parsed_response[ relationships ][i][ label ] == "Government of the United States of America":
你们的法典秩序意味着,在界定你的职能之前,你要重新指定你的职能。
最后,你期望实现以下目标尚不清楚:
1 # not a parent, do nothing
无,使用<条码>。
这些问题是:
import requests
url = https://api.ror.org/organizations/010jszw77
api_response = requests.get(url)
parsed_response = api_response.json()
num_relationships = len(parsed_response[ relationships ]) # 2 relationships in this example, both parents
print(f"Found {num_relationships} relationships")
def run_up_chain_to_USGovt(rorid):
local_url = https://api.ror.org/organizations/ + rorid
local_api_response = requests.get(local_url)
local_parsed_response = local_api_response.json()
return
for i in range(0, num_relationships):
if (parsed_response[ relationships ][i][ type ]) == "Parent":
print("parent")
if parsed_response[ relationships ][i][ label ] == "Government of the United States of America":
USFF = True
else:
run_up_chain_to_USGovt(parsed_response[ relationships ][i][ id ])
else:
pass
还要指出的另一个问题是:parsed_response[ relations ][i][id]
实际上回归了URL。 转而来的是,A/62/L.A.A.A.C.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.R.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A.A. 但是,在我看来,<编码>https://api.ror.org/Organizations/027ka1x80。 打算这样做。
您真的希望你发挥 rec的作用:
- load the data for an organisation
- for each of its relationships
- check if it is a parent
- if so, check if it is the US govt.
- if so, we re done - the organisation has the US govt. as a parent, return
True
- if not, call the recursive function to first check all of its parents
- if the result is
True
, this organisation has it as a grandparent, return True
- if we didn t return
True
already, none were the US govt. or had it as a (grand)parent, return False
我们应牢记,不要“召回性功能”,而是“如果我们事先对该组织进行核查,我们应“召回性功能”,它就没有美国议会。 a 祖父母。 只有当我们从任何地方收回本组织在返回<代码>False之前的身份证时,我们才会这样做,记住不要再次检查。
在你最初的法典中,你把一个组织的细节放在职能内外两倍,但你只能把休养职能称作原始组织身份证并核对结果。
所有这一切:
import requests
from requests import Response
def has_US_govt_parent(org_id: str, visited: list[str] = None) -> bool:
"""
Returns True if the organization with the given ROR ID has a parent organization
that is the Government of the United States of America, False otherwise.
"""
# start with an empty cache
if visited is None:
visited = []
# - load the data for an organisation
print(f Loading data for {org_id} )
url = f https://api.ror.org/organizations/{org_id}
api_response: Response = requests.get(url)
parsed_response = api_response.json()
# - for each of its relationships
# you don t need the number of relationships and index it, you can just loop over the members
for relationship in parsed_response[ relationships ]:
# - check if it is a parent
if relationship[ type ] == "Parent":
# -if so, check if it is the US government
if relationship[ label ] == "Government of the United States of America":
return True
else:
# assuming that the last part without slashes is the actual ID, there s many ways to do this
parent_id = relationship[ id ].split( / )[-1]
# - call the recursive function if we haven t checked this organisation before, otherwise no
if parent_id in visited:
grandparent_US_govt = False
else:
print(f Need to check parents of {relationship["label"]} )
grandparent_US_govt = has_US_govt_parent(parent_id, visited)
# - if the result is True, this organisation has it as a grandparent, return True
if grandparent_US_govt:
return True
else:
pass
# remember we already visited this one, it will always be False
visited.append(org_id)
# - if we didn t return True already, none were the US govt. or had it as a (grand)parent, return False
return False
if has_US_govt_parent( 010jszw77 ):
print( 010jszw77 has a US government parent )
else:
print( 010jszw77 does not have a US government parent )
产出:
Loading data for 010jszw77
Need to check parents of National Aeronautics and Space Administration
Loading data for 027ka1x80
010jszw77 has a US government parent
请注意,如果你要经常进行这种搜查,你可能希望建立一个快速的盘问表,并定期更新,而不是每次都发出所有这种预报。 或者,如果你从单一方案进行许多此类搜索,则至少要保留<代码>>>,以便随后的搜索速度大大加快。
在职能之外保持<条码>查询条码>的轨道,并更确切地加以利用,以检查本组织是否已经访问:
import requests
from requests import Response
def has_US_govt_parent(org_id: str, visited: dict[str, bool] = None) -> bool:
"""
Returns True if the organization with the given ROR ID has a parent organization
that is the Government of the United States of America, False otherwise.
"""
# start with an empty cache
if visited is None:
visited = {}
# just check it here, and we re done
if org_id in visited:
return visited[org_id]
# - load the data for an organisation
print(f Loading data for {org_id} )
url = f https://api.ror.org/organizations/{org_id}
api_response: Response = requests.get(url)
parsed_response = api_response.json()
# assume we won t find the US govt.
result = False
# - for each of its relationships
# you don t need the number of relationships and index it, you can just loop over the members
for relationship in parsed_response[ relationships ]:
# - check if it is a parent
if relationship[ type ] == "Parent":
# -if so, check if it is the US government
if relationship[ label ] == "Government of the United States of America":
result = True
break
else:
# assuming that the last part without slashes is the actual ID, there s many ways to do this
parent_id = relationship[ id ].split( / )[-1]
# - call the recursive function
print(f Need to check parents of {relationship["label"]} )
if has_US_govt_parent(parent_id, visited):
result = True
break
else:
pass
# remember we already visited this one, it will always be False
visited[org_id] = result
# - if we didn t return True already, none were the US govt. or had it as a (grand)parent, return False
return result
visited = {}
if has_US_govt_parent( 010jszw77 , visited):
print( 010jszw77 has a US government parent )
else:
print( 010jszw77 does not have a US government parent )
print( Data for these organisations were retrieved: , visited)
这不仅有助于跟踪已经返回的各组织:<代码>False,而且只是记住在字典中产生的所有结果,并且将这种藏匿点用于连续电话。
现在,你还可以:
visited = {}
orgs = [ 01s3dpm97 , 010jszw77 , 028b18z22 ]
for org in orgs:
print(f Checking {org} )
if has_US_govt_parent(org, visited):
print(f {org} has a US government parent )
else:
print(f {org} does not have a US government parent )
print( Visited: , visited)
产出:
Checking 01s3dpm97
Loading data for 01s3dpm97
Need to check parents of University of Alabama in Huntsville
Loading data for 02zsxwr40
Need to check parents of University of Alabama System
Loading data for 051fvmk98
01s3dpm97 does not have a US government parent
Checking 010jszw77
Loading data for 010jszw77
Need to check parents of National Aeronautics and Space Administration
Loading data for 027ka1x80
010jszw77 has a US government parent
Checking 028b18z22
Loading data for 028b18z22
Need to check parents of National Aeronautics and Space Administration
028b18z22 has a US government parent
Visited: { 051fvmk98 : False, 02zsxwr40 : False, 01s3dpm97 : False, 027ka1x80 : True, 010jszw77 : True, 028b18z22 : True}