python- range()

发布于 2025-02-07 10:43:20 字数 591 浏览 5 评论 0原文

我是Python的新手,我试图创建一个简单的循环,以便我列出具有Value Integer用户的用户。但是,DB上的用户ID似乎很奇怪(用户1-72的显示正确,距离72是712),因此我需要跳过那些没有任何用户IDS 73-711的用户。必须做什么例外,或者我需要创建一个简单的例外情况?

for iD in range(1, 720):
        loginID = iD
        for users in tls.getUserByID(loginID):
            fname = users.get('firstName')
            lname = users.get('lastName')
            print(loginID, fname +" "+ lname)

我得到的错误是..

raise testlinkerrors.TLResponseError(
testlink.testlinkerrors.TLResponseError: NO_USER_BY_ID_LOGIN: (getUserByID) - Cannot Find User with DB ID (73)

I'm new to Python, I have tried to create a simple loop in order for me to list users that has userID of value integer. However the userID on the DB seems weird (Users 1-72 are displayed properly, next to 72 is 712 already), so I need to skip those users who does not have any userID 73-711. What exception must be done or do I need to create a simple if else?

for iD in range(1, 720):
        loginID = iD
        for users in tls.getUserByID(loginID):
            fname = users.get('firstName')
            lname = users.get('lastName')
            print(loginID, fname +" "+ lname)

The error I'm getting is..

raise testlinkerrors.TLResponseError(
testlink.testlinkerrors.TLResponseError: NO_USER_BY_ID_LOGIN: (getUserByID) - Cannot Find User with DB ID (73)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

你是我的挚爱i 2025-02-14 10:43:21

呼叫tls.getuserbyid(loginID)如果没有DB ID的用户,则您将获得异常语句。

from testlink.testlinkerrors import TLResponseError

for iD in range(1, 720):
    loginID = iD
    try:
        users = tls.getUserByID(loginID)
    except TLResponseError:
        print(loginID, "not found !")
        continue
    for user in users:
        fname = user.get('firstName')
        lname = user.get('lastName')
        print(loginID, fname +" "+ lname)

You will get exception when call tls.getUserByID(loginID) if no user with DB ID, so you need to handle the exception testlinkerrors.TLResponseError by try/except statement.

from testlink.testlinkerrors import TLResponseError

for iD in range(1, 720):
    loginID = iD
    try:
        users = tls.getUserByID(loginID)
    except TLResponseError:
        print(loginID, "not found !")
        continue
    for user in users:
        fname = user.get('firstName')
        lname = user.get('lastName')
        print(loginID, fname +" "+ lname)
许仙没带伞 2025-02-14 10:43:21

您可以尝试此@CKMB

     for iD in range(60, 720):
        if iD not in [* range(73, 712)]:
            loginID = iD
            try:
                api_getUser = tls.getUserByID(loginID)
                for user in api_getUser:
                  fname = user.get('firstName')
                  lname = user.get('lastName')
                  print(loginID, fname + " " + lname)
            except testlinkerrors.TLResponseError:
                print(loginID, "not found")
                continue
          

You can try this @ckmb

     for iD in range(60, 720):
        if iD not in [* range(73, 712)]:
            loginID = iD
            try:
                api_getUser = tls.getUserByID(loginID)
                for user in api_getUser:
                  fname = user.get('firstName')
                  lname = user.get('lastName')
                  print(loginID, fname + " " + lname)
            except testlinkerrors.TLResponseError:
                print(loginID, "not found")
                continue
          
忘年祭陌 2025-02-14 10:43:21

您可以通过简单的尝试/除外跳过故障。

for iD in range(1, 720):
        loginID = iD
        try:
            for users in tls.getUserByID(loginID):
                fname = users.get('firstName')
                lname = users.get('lastName')
                print(loginID, fname +" "+ lname)
        except testlinkerrors.TLResponseError:
            print("Skipping iD")
            continue

You can skip the failures with a simple try/except block.

for iD in range(1, 720):
        loginID = iD
        try:
            for users in tls.getUserByID(loginID):
                fname = users.get('firstName')
                lname = users.get('lastName')
                print(loginID, fname +" "+ lname)
        except testlinkerrors.TLResponseError:
            print("Skipping iD")
            continue
计㈡愣 2025-02-14 10:43:21

您可以编写一个简单的功能,该功能需要一个值得一提的数字来跳过并返回发电机。

def skip(iter, start, end):
    return (i for i in iter if not (i <= end and i >= start))

现在:

list(skip(range(1, 720), 73, 711))

产量:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 
 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 
 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 
 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 
 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 
 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 712, 713, 
 714, 715, 716, 717, 718, 719]
>>>

You could write a simple function that takes an iterable and a range of numbers to skip and returns a generator.

def skip(iter, start, end):
    return (i for i in iter if not (i <= end and i >= start))

And now:

list(skip(range(1, 720), 73, 711))

Yields:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 
 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 
 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 
 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 
 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 
 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 712, 713, 
 714, 715, 716, 717, 718, 719]
>>>
扭转时空 2025-02-14 10:43:21

我已经使用了@Jason Yang的答案 - 看来这是我期望的,但是我相信跳过73-711会增加运行时,如何自动跳过它们?就像这些int不存在于该范围内,然后跳过

for iD in range(60, 720):
        loginID = iD
        try:
            api_getUser = tls.getUserByID(loginID)
        except testlinkerrors.TLResponseError:
            print (loginID, "not found")
            continue
        for user in api_getUser:
            fname = user.get('firstName')
            lname = user.get('lastName')
            print(loginID, fname + " " + lname)

I have used the answer of @Jason Yang - and it seems this is what I'm expecting to have, however I believe skipping 73-711 will increase runtime, are there any ways on how to automatically skip them? Like if these int are not present in the range, then skip

for iD in range(60, 720):
        loginID = iD
        try:
            api_getUser = tls.getUserByID(loginID)
        except testlinkerrors.TLResponseError:
            print (loginID, "not found")
            continue
        for user in api_getUser:
            fname = user.get('firstName')
            lname = user.get('lastName')
            print(loginID, fname + " " + lname)
动听の歌 2025-02-14 10:43:21

有多种方法可以获取某些数字范围的子集。

如果您知道可以使用

>>> import itertools
>>> list(itertools.chain(range(10),range(100,110),range(210,222)))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221]
>>> 

rel 知道不会工作。

您可以像克里斯(Chris)答案一样使用过滤,

您可以使用try-except,而只是处理成功的滤波,

您可以通过记住有问题的滤镜,将其保存在某个地方并将其用于过滤,从而结合上面的两个。对于保存部分,如果我们想在关闭程序后要记住,我们可以使用 json < /a>模块将其保存到

例如“例如

from testlink.testlinkerrors import TLResponseError
import json

ignore_file = "ignore_id.json" 
#to_ignore = set()

def fun(users, to_ignore=()):
    to_ignore = set(to_ignore)
    for iD in users:
        loginID = iD
        if iD in to_ignore:
            print(loginID, "ignored !")
            continue 
        try:
            users = tls.getUserByID(loginID)
        except TLResponseError:
            print(loginID, "not found !")
            to_ignore.add(iD)
            continue
        for user in users:
            fname = user.get('firstName')
            lname = user.get('lastName')
            print(loginID, fname +" "+ lname)
    return to_ignore


def load_json(file_name):
    try:
        with open(file_name) as file: 
            return json.load(file)
    except FileNotFoundError:
        return []

def save_json(file_name,data):
    with open(file_name,"w") as file:
        json.dump(data,file)




to_ignore = fun(range(1,720), load_json(ignore_file))
save_json(ignore_file,list(to_ignore))

编辑”的文件中:

您还可以将功能概括为将用户和列表忽略以及其他可能需要的内容,并且可以返回沿着它找到的有问题的用户集如何处理适当的信息。

现在,随着函数现在概括,您可以进行不同类型的呼叫,例如

fun(range(1,720))
fun(itertools.chain(range(73),range(712,720))
fun(range(1,113)) #for the other database
etc...

There are a number of way to get some subset of some range of number

If you know the boundaries you can use chain

>>> import itertools
>>> list(itertools.chain(range(10),range(100,110),range(210,222)))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221]
>>> 

for your example is itertools.chain(range(73),range(712,720)), that way you don't waste time with something you alredy know is not going to work.

You can use filtering like in Chris answer

You can use try-except and just processing the successful ones

You can combine the two above by remembering the problematic ones, saving it somewhere, and use that to filter. For the saving part, if we want to remember after closing the program, we can use json module to save it into a file

Like for example

from testlink.testlinkerrors import TLResponseError
import json

ignore_file = "ignore_id.json" 
#to_ignore = set()

def fun(users, to_ignore=()):
    to_ignore = set(to_ignore)
    for iD in users:
        loginID = iD
        if iD in to_ignore:
            print(loginID, "ignored !")
            continue 
        try:
            users = tls.getUserByID(loginID)
        except TLResponseError:
            print(loginID, "not found !")
            to_ignore.add(iD)
            continue
        for user in users:
            fname = user.get('firstName')
            lname = user.get('lastName')
            print(loginID, fname +" "+ lname)
    return to_ignore


def load_json(file_name):
    try:
        with open(file_name) as file: 
            return json.load(file)
    except FileNotFoundError:
        return []

def save_json(file_name,data):
    with open(file_name,"w") as file:
        json.dump(data,file)




to_ignore = fun(range(1,720), load_json(ignore_file))
save_json(ignore_file,list(to_ignore))

EDIT:

You can also generalize the function to take the user and the list to ignore and whatever else it might need, and it can return the set of problematic user it found along the way and do something appropriate with that info.

Now with the function now generalize like that, you can do different types of call such as

fun(range(1,720))
fun(itertools.chain(range(73),range(712,720))
fun(range(1,113)) #for the other database
etc...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文