Created
November 9, 2021 03:29
-
-
Save scidam/535b0509c5c697bcb13be3ae0e07eac0 to your computer and use it in GitHub Desktop.
My solution to linkenin_checker challenge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from collections import namedtuple | |
| import re | |
| with open('specifications.txt', 'rt') as file: | |
| specifications = file.read() | |
| specs = namedtuple('specs', 'range regex') | |
| #specs range builtin module | |
| #specs regex from re.compile | |
| def get_linkedin_dict(): | |
| '''Convert specifications into a dict where: | |
| keys: feature | |
| values: specs namedtuple''' | |
| parsing = False | |
| result = dict() | |
| range_val = None | |
| regex_val = None | |
| for line in specifications.splitlines(): | |
| print(line, result) | |
| if line.strip().startswith('feature') and not parsing: | |
| feature = line.split(':')[-1].strip() | |
| parsing = True | |
| print(line, 'here') | |
| continue | |
| if line.strip().startswith('feature') and parsing: | |
| print("Storing,", range_val, regex_val, line) | |
| if range_val is not None and regex_val is not None: | |
| result[feature] = specs(range_val, regex_val) | |
| range_val = None | |
| regex_val = None | |
| feature = line.split(':')[-1].strip() | |
| elif parsing: | |
| if line.strip().startswith('requirements'): | |
| val1, val2 = re.findall(r'\d+', line) | |
| range_val = range(int(val1), int(val2) + 1) | |
| continue | |
| elif line.strip().startswith('login'): | |
| rg = line.split(':')[-1].replace(' ', '') | |
| regex_val = re.compile(r'^[{}]+@[{}]+\.com|net|org$'.format(rg, rg)) | |
| elif line.strip().startswith('permitted'): | |
| rg = line.split(':')[-1].replace(' ', '') | |
| regex_val = re.compile(r'^[{}]+$'.format(rg)) | |
| continue | |
| print(feature) | |
| if range_val is not None and regex_val is not None: | |
| result[feature] = specs(range_val, regex_val) | |
| return result | |
| def check_linkedin_feature(feature_text, url_or_login): | |
| '''Raise a ValueError if the url_or_login isn't login or custom_url | |
| If feature_text is valid, return True otherwise return False''' | |
| data = get_linkedin_dict() | |
| result = data.get(url_or_login, None) | |
| if result is None: | |
| raise ValueError('Feature needs to be either login or custom_url') | |
| else: | |
| length = len(feature_text) in data[url_or_login].range | |
| regex = bool(data[url_or_login].regex.search(feature_text)) | |
| return length & regex |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment