From the course: Level Up: Advanced Python

Implement a LinkedIn checker - Python Tutorial

From the course: Level Up: Advanced Python

Implement a LinkedIn checker

(video game beeps and chimes) - [Narrator] In this challenge, we're going to create a LinkedIn checker for two LinkedIn features, creating a custom URL, and logging in. So let's look at each of these in turn. (bright beeping) LinkedIn allows you to create a custom URL for your profile. Now this is my LinkedIn profile, and you can see that in the custom URL section, it states that it must contain between 3 and 100 letters or numbers, and we can't use spaces, symbols, or special characters. When logging in, LinkedIn allows you to log in using either your email address or your phone number. For this challenge, we will check only for an email address, and not a phone number. Now even then, checking if a string is an email address is incredibly complicated, so we'll simplify things. We'll say that it only needs to have a set of characters before and after the at symbol, followed by the dot, and then the domain name can only be com, .org, or .net. We won't do checks for things like does the email address contain consecutive dots, or does it start with a non-alphanumeric character? We're going to create a LinkedIn checker that checks if the features conform to the conditions. So that's the custom URL, or logging in. Now, the requirements for each of these features is defined in specifications.txt, which is available in your exercise folder. So let's take a quick look at it. You can see that we have different requirements and permitted characters for the custom URL feature and the login feature. In the linkedin_checker.py file, you need to take these specifications and pass them into a dictionary of specs namedtuples. Specs, of course, being short for specifications. Now these specs namedtuples includes two components, range and regex. Range is to help with verifying that the length of the features meet the requirements. So for the custom URL as an example, that's between 3 and 100 characters. And regex is a re.compile object from the regular expression module, that helps you confirm the features contains only permitted characters. Again, using custom URL as an example, this can only be lower and uppercase alphabetical characters, and the digits zero to nine. So here's the challenge. Pass the specifications into a dictionary of specs namedtuples. Update the function check_linkedin_feature. This takes in as input the feature text and the URL or login, which is whether the feature text is a custom URL login. Now, if the feature text is valid, then return true, or else return false. And if the URL or login input isn't a custom URL or a login, then raise a ValueError. You can test your solution using pie test. So go ahead and pause the video here, and I'll show you the solution that I came up with. As always, you're welcome to come up with your own solution as long as it passes all the tests. (video game chimes and beeps) Let's head over to the get_linkedin_dict function. We need to convert the specification into a dict with keys being the feature, and the values of the dictionary being the specs namedtuple. So I initialize the dictionary that I'm going to use, and call it data. Next, we need to work our way through each line of specifications to determine the requirements and the permitted characters. If the line is in blank and contains the requirements, we want to pass the line and capture the minimum and maximum number of characters. If the line instead contains the string permitted characters, we need to gather them up, and put them into square brackets so that it can be used by the irregular expression compile object. This checks to see that each of the characters in any feature text only contains the accepted values for that feature. If the line from the specification contains the string login characters instead, then like before, we want to gather them up. Now, because a dot and a dash have a special meaning inside of square brackets of a regular expression, we would normally prepend them with a backslash. If we look at the specifications.txt file, for the line with login characters we have a couple of dashes for the login characters, A to Z, capital A to capital Z, and then finally, the dash by itself. Now, we only want to prepend a back slash for the dash by itself, and leave the other ones alone. So let's head back to the solution file. Now, one way we could do this is to use the replace function, but by default, it would replace all occurrences. Replace has an optional count positional argument that we would probably want to use, but it replaces one, two, or three occurrences, and so on, starting from the left to the right. Now, we only want to replace the last occurrence of the dash. One workaround is to reverse the string and then use replace with the positional count argument of one to replace the dash we want. But because we're going to reverse the string again to get our original regex, we need to add the back slash after the dot and the dash. This means that when the string is reversed, they prepend the dash. And finally, we reverse our regex back to its original form. We're now in a position to create our regular expression compile object, and as I said, the rules for an email address would be simplified, so we allow a set of one or more characters the at symbol, followed by the host name, which is a set of regex characters we've determined, followed by a dot, and then finally followed by the domain name. Let's go ahead and confirm this passes all our tests. Pytest test, and test linkedin_checker.py. And as you can see, it's passed all our tests, so we've completed the final challenge. Now my solution is just one way of solving this problem. Go ahead and share your solution in the Q and A section. Now, just so you know, if you just post your code in the Q and A section as plain text, it won't be formatted, and so it'll be difficult to read. You might want to post the link to your code snippet using something like GitHub's gist instead. I'd love to see your answer to this code challenge.

Contents