【问题标题】:How can I dynamically detect when an integer changes如何动态检测整数何时更改
【发布时间】:2022-10-13 23:14:12
【问题描述】:
基于 this answer,我可以使用 Python API UserManager 类获取我的 Hub 社区帐户中的用户总数。但是,这个数字一定会在某个时候发生变化。我正在寻找一种动态检测变化的方法。
这可以获取您组织中的用户总数。
from arcgis.gis import GIS
gis = GIS("https://yourhub.or.agol.account", "adminUserName", "password")
from itertools import count
import arcgis
users = arcgis.gis.UserManager(gis)
# get the total number of users in your AGOL account provided you have administrative priveleges
totalUsers = users.counts(type='user_type', as_df=False)[0]['count']
print(totalUsers)
#prints
539
以下是我必须检测到的变化(静态)。问题是,因为这个脚本是通过任务调度程序运行的,所以当totalUsers 发生变化时它会持续运行——直到我手动输入新的用户数。
if totalUsers == 538: #<--How can I make this integer dynamic?
print(f'Total number of Hub users is {totalUsers}')
elif totalUsers < 538:
#send a notification email to GIS manager
elif totalUsers > 538:
#send a notification email to GIS manager
我认为这更像是一个 python 而不是 GIS 问题,所以我在这里发布。
【问题讨论】:
标签:
python
dynamic
arcgis
【解决方案1】:
你是对的,这是一个一般的 Python 编程问题,不是专门针对 ArcGIS 的。
最简单的选择是将当前用户写入本地文件,然后读取记录的数字并将其与新计数进行比较。
from arcgis.gis import GIS
gis = GIS("https://www.arcgis.com", "adminUserName", "password")
from itertools import count
import arcgis
def saveUsers(userCount):
with open('usercount.csv', 'w') as f:
f.write(str(userCount))
def getCurrentUsers():
users = arcgis.gis.UserManager(gis)
# get the total number of users in your AGOL account provided you have administrative priveleges
totalUsers = users.counts(type='user_type', as_df=False)[0]['count']
return totalUsers
def loadPreviousUsers():
with open('usercount.csv') as f:
lines = f.readlines()
userCount = int(lines[0])
return userCount
currentUsers = getCurrentUsers()
previousUsers = loadPreviousUsers()
if totalUsers == previousUsers: #<--How can I make this integer dynamic?
print(f'Total number of Hub users is {totalUsers}')
elif totalUsers < previousUsers:
print(f'send a notification email to GIS manager: {previousUsers} -> {totalUsers}')
saveUsers(currentUsers)
elif totalUsers > previousUsers:
print(f'send a notification email to GIS manager: {previousUsers} -> {totalUsers}')
saveUsers(currentUsers)