Python Forum
Use function, retry until valid
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use function, retry until valid
#1
Question 
Hello community,

I am currently stuck with the following use case: my goal is to find user names within a directory as normally the display name is provided. If the input if wrong or cannot be found, the user should be able to loop until the entry is found. If the input has been found, the username should be returned and saved instead of the variable that has been provided initially.

Currently I am trying with "while TRUE" in combination with "try, expect, finally" but somehow this does not work properly. Can you help me find the tree in the forest?

def searchUser(userstring):
    while True:
        try:
            userid = JIRA._get_user_id(userstring)
        except:
            userid = input("Please enter a valid user: ")

        else:
            return userid


teamlead = "Teamleader_DisplayName"
teamlead = searchUser(teamlead)

departmentlead = "Departmentlead_DisplayName"
departmentlead = searchUser(departmentlead)
Where is my mental knot? Can you support me?
Reply
#2
When is the loop supposed to end? Currently it only loops once. Describe how it should work.
Reply
#3
Thanks for your support. The loop ends when the display Name respectively the name behind the provided string can be found in the directory. If it can be found then the username shall be used as variable instead of the search string variable

Example:
In a document there is „Peter Pan“ named as responsible (this will be taken over als string to the function)
Now the loop shall take a look for „Peter Pan“.

If it is found, the „Peter Pan“ (which is provided for the function) should be replaced with „peter.pan“ (which is the username not the display name, in the sense of a dictionary lookup“

If it is not found (e.g. due to a typo „Petr Pan“), the user can correct the name within this loop. If then the name is found, see above.

The input variable will be replaced with the variable from the dictionary string to proceed the next code lines with this username (e.g. to automatically assign tasks to this user name as it is provided and verified by the dictionary lookup)
Reply
#4
I'm not familiar with JIRA, but the first question is whether or not the JIRA._get_user_id() raises an exception if the user is not found. Is that the expected behavior? If it does not raise an exception it will always pass the try..
Reply
#5
Luckily, there is an error reported in the log

Error:
(...) in _get_user_id raise JIRAError(f"No matching user found for: '{user}'") jira.exceptions.JIRAError: JiraError HTTP None text: No maching user found for "Petr Pan"
Reply
#6
finally will run every time the try/except block is evaluated. You only want to return a value if JIRA._get_user_id() is successful.
def searchUser(userstring):
    while True:
        try:
            return JIRA._get_user_id(userstring)
        except:
            userstring = input("Please enter a valid user: ")
 
 
teamlead = "Teamleader_DisplayName"
teamlead = searchUser(teamlead)
 
departmentlead = "Departmentlead_DisplayName"
departmentlead = searchUser(departmentlead)
Methods that start with an underscore indicate that the method is not meant for public use. The package authors are warning you thatJIRA._get_user_id() can change at any time and it is unwise for you to use this method in your code.
Ashcora likes this post
Reply
#7
To sum it up: You are my today‘s hero. With your help it works now :) that’s so awesome as I spent so many thoughts and tries on it.

So all in all I thought too complicated to not just take care on the return...

The underscore topic is okay for me, as I will use it as long as I can, and the Active Directory connection using Python is already working (as kind of successor solution :) ).

But I didn’t know it what the underscored methods mean. So this is really helpful to know.

All in all: THANK YOU so much for your help! This is really appreciated.
Reply
#8
If JIRA._get_user_id(userstring) raises an exception, the return is not executed, and control jumps directly to the except. If it is successful, it returns the userid.

You should know Python coding conventions. I will help you understand other people's code and it will help you write code that other people can read. The document that outlines Python coding conventions is PEP 8. This is the section that talks about naming conventions.

https://peps.python.org/pep-0008/#naming-conventions

And it has this to say about a leading underscore in a variable name:
Quote:In addition, the following special forms using leading or trailing underscores are recognized (these can generally be combined with any case convention):

_single_leading_underscore: weak “internal use” indicator. E.g. from M import * does not import objects whose names start with an underscore.
I didn't know the part about the wildcard import, but I never use wildcard imports.

So _get_user_id() is an internal use only function. Or it should be if the JIRA package authors are following PEP8 conventions. When I looked at the JIRA python package documentation it does not mention _get_user_id(). Another thing to be concerned about.
Ashcora likes this post
Reply
#9
Thanks for sharing the naming conventions among others.

I was taught Java in school. That was 15 years ago, but the language still influences my programming. Therefore I take every tip I can get to get rid of the "old learned". Like also for example the "wildcard import".

I saved the link as bookmark to get reminded every time I open my browser :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Script getting reindexing only valid error cubangt 1 944 Dec-07-2023, 04:06 PM
Last Post: cubangt
  How to ignore "Retrying (Retry(total=2, connect=2, read=5, redirect=5, status=None))" const 3 2,729 Mar-26-2022, 08:55 AM
Last Post: ndc85430
  checking for valid hexadecimal digits Skaperen 3 6,419 Sep-02-2021, 07:22 AM
Last Post: buran
  requests_futures.sessions retry list of HTTP 429 Johanoosterwaal 0 1,571 Nov-20-2020, 09:23 PM
Last Post: Johanoosterwaal
  Retry After Exception Harshil 2 2,266 Aug-09-2020, 05:32 PM
Last Post: Harshil
  Limiting valid values for a variable in a class WJSwan 5 3,667 Jul-06-2020, 07:17 AM
Last Post: Gribouillis
  How to verify the give number is valid Mekala 3 2,413 May-16-2020, 02:40 PM
Last Post: anbu23
  C:\Windows\System32\appwiz.cpl is not a valid Win32 application Marceepoo 8 5,198 Mar-15-2020, 04:46 AM
Last Post: buran
  is a str object a valid iterator? Skaperen 6 5,659 Jan-27-2020, 08:44 PM
Last Post: Skaperen
  How to get valid error message while invoking REST API through Python gollupraveen 0 2,078 Oct-04-2019, 07:15 PM
Last Post: gollupraveen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020