【问题标题】:Returning a value that is not excluded in python返回一个不在 python 中排除的值
【发布时间】:2023-01-19 16:04:15
【问题描述】:

我想创建一个 function 来检查给定帐户是否不是技术帐户。如果帐户是技术性的,我们会跳过它,如果不是,我们会显示帐户详细信息。

技术账户保存在addc_accounts_excluded.json文件中:

{
    "sAMAccountName": [
        "spiderman",
        "ironman"
    ]
}

所有帐户都保存在encoded_users_from_LDAP.json 文件中:

{
    "entries": [
        {
            "attributes": {
                "cn": "Bruce Wayne",
                "primaryGroupID": 513,
                "sAMAccountName": "batman",
                "userAccountControl": 514,
                "whenCreated": "2016-04-19 10:06:25+00:00"
            },
            "dn": "CN=Bruce Wayne,OU=Users,OU=DC-COMICS,DC=universum,DC=local"
        },
        {
            "attributes": {
                "cn": "Clark Kent",
                "primaryGroupID": 513,
                "sAMAccountName": "superman",
                "userAccountControl": 514,
                "whenCreated": "2016-04-19 10:06:25+00:00"
            },
            "dn": "CN=Clark Kent,OU=Users,OU=DC-COMICS,DC=universum,DC=local"
        },
        {
            "attributes": {
                "cn": "Peter Parker",
                "primaryGroupID": 513,
                "sAMAccountName": "spiderman",
                "userAccountControl": 514,
                "whenCreated": "2016-04-19 10:06:25+00:00"
            },
            "dn": "CN=Peter Parker,OU=Users,OU=DC-COMICS,DC=universum,DC=local"
        }
    ]
}

我的蟒蛇代码:

### load data from JSON files
# addc_accounts_excluded.json
accounts_excluded = root_path + json_dir + accounts_excluded_file
with open(accounts_excluded, 'r', encoding="UTF-8") as file:
    data = json.load(file)
    users_excluded = data['sAMAccountName']

# encoded_users_from_LDAP.json
encoded_retrived_users = root_path + json_dir + "test.json"
with open(encoded_retrived_users, 'r', encoding="UTF-8") as file:
    data = json.load(file)
    retrived_users = data['entries']


def user_validation(): # this what I need to validate accounts


for user in retrived_users:
    attributes = user['attributes']
    sAMAccountName = attributes['sAMAccountName']
    if ('is the user excluded? if not do it'):
        print(sAMAccountName)
        print(attributes['cn'])    

我在 powershell 中有这样的东西:

function userValidation ($suspect) {
    $account = $true
    foreach ($user in $csvDnsHostName) {
        if ($user -eq $suspect) {$account = $false}
    }
    return $account
}

foreach ($cmptr in $allCmptrsFromjsonBaseWorkFile){
        $cmptrIdData = $cmptr.cmptr_id_data
        $dnsHostName = $cmptrIdData.dnsHostName
        if (userValidation($dnsHostName) -eq $true) { # to wyklucza konta techniczne
            $cmptrStatusInf = $cmptr.cmptr_status_inf
            $accountStatus = $cmptrStatusInf.accountStatus

【问题讨论】:

    标签: python python-3.x function compare definition


    【解决方案1】:

    如果我对您的问题的理解是正确的,那么这应该有效:

    def user_validation(user: str, excluded_users: str):
      return user not in excluded_users
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-13
      • 1970-01-01
      • 2015-12-03
      • 2011-01-30
      相关资源
      最近更新 更多