Python Forum
Use function, retry until valid - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Use function, retry until valid (/thread-39123.html)



Use function, retry until valid - Ashcora - Jan-05-2023

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?


RE: Use function, retry until valid - deanhystad - Jan-05-2023

When is the loop supposed to end? Currently it only loops once. Describe how it should work.


RE: Use function, retry until valid - Ashcora - Jan-05-2023

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)


RE: Use function, retry until valid - jefsummers - Jan-05-2023

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..


RE: Use function, retry until valid - Ashcora - Jan-05-2023

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"



RE: Use function, retry until valid - deanhystad - Jan-05-2023

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.


RE: Use function, retry until valid - Ashcora - Jan-05-2023

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.


RE: Use function, retry until valid - deanhystad - Jan-05-2023

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.


RE: Use function, retry until valid - Ashcora - Jan-06-2023

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 :)