-
-
Notifications
You must be signed in to change notification settings - Fork 115
feat: adiciona utilitários para lidar com passaportes brasileiros #699
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
25921a6
Adicionando utilitário de passaporte
erickcpassos 036b771
Corrige table of contents do README
erickcpassos 0f1f0b5
Corrige table of contents do README_EN
erickcpassos 7cbe015
Adiciona novos utilitários no changelog
erickcpassos c5e353f
Corrige espaçamento do README
erickcpassos ee5d8b8
Corrige espaçamento no README_EN
erickcpassos 1e506e3
Formata READMEs para o padrão
erickcpassos 695674f
Retoma READMEs anteriores
erickcpassos 3843805
Corrige título utilitários do README
erickcpassos ffbec55
Merge branch 'main' into 579
niltonpimentel02 0251c99
Corrige atualização do changelog
erickcpassos 01b61f2
Merge branch '580' of https://github.com/erickcpassos/python into 579
erickcpassos ab85df4
Merge branch 'main' into 579
niltonpimentel02 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import random | ||
| import re | ||
| import string | ||
|
|
||
|
|
||
| def is_valid(passport: str) -> bool: | ||
| """ | ||
| Checks if a Brazilian passport number is valid. | ||
|
|
||
| To be considered valid, the input must be a string containing exactly two alphabetical characters followed by exactly six numerical digits. | ||
|
|
||
| This function does not verify is the input is a real passport number, as there are no checksums for the Brazilian passport. | ||
|
|
||
| Args: | ||
| passport (str): The string containing the passport number to be checked. | ||
|
|
||
| Returns: | ||
| bool: True if the passport number is valid (2 letters followed by 6 digits). False otherwise. | ||
|
|
||
| Example: | ||
| >>> is_valid("Ab123456") | ||
| True | ||
| >>> is_valid("12345678") | ||
| False | ||
| >>> is_valid("DC-221345") | ||
| False | ||
| """ | ||
|
|
||
| if not isinstance(passport, str): | ||
| return False | ||
|
|
||
| pattern = re.compile("^[A-Z]{2}[0-9]{6}$") | ||
| match = re.match(pattern, passport) | ||
| return match is not None | ||
|
|
||
|
|
||
| def remove_symbols(passport: str) -> str: | ||
| """ | ||
| Removes symbols ('-', '.', and whitespaces) from a passport number. | ||
|
|
||
| This function takes a passport number string as input and removes all occurrences of | ||
| the '.', '-', and whitespace characters from it. | ||
|
|
||
| Args: | ||
| passport (str): The string containing a passport number | ||
|
|
||
| Returns: | ||
| str: The passport numbers with dashes (-), dots (.), and whitespaces ( ) removed. | ||
|
|
||
| Example: | ||
| >>> remove_symbols("Ab123456") | ||
| Ab123456 | ||
| >>> remove_symbols("Ab-123456") | ||
| Ab123456 | ||
| >>> remove_symbols("Ab -. 123456") | ||
| Ab123456 | ||
| """ | ||
|
|
||
| return "".join(filter(lambda c: c not in ".- ", passport)) | ||
|
|
||
|
|
||
| def format_passport(passport: str) -> str | None: | ||
| """ | ||
| Formats a Brazilian passport number for display. | ||
|
|
||
| This function takes a string representing a valid passport number and returns it formatted (uppercase, without symbols). | ||
|
|
||
| Args: | ||
| passport (str | None): A Brazilian passport number (lower or uppercase, possibly including symbols) | ||
|
|
||
| Returns: | ||
| str: The formatted passport number (uppercase, without symbols) or None if the input is invalid | ||
|
|
||
| Example: | ||
| >>> format_passport("Ab123456") | ||
| AB123456 | ||
| >>> format_passport("Ab-123456") | ||
| AB123456 | ||
| >>> format_passport("111111") | ||
| None | ||
| """ | ||
|
|
||
| passport = remove_symbols(passport.upper()) | ||
|
|
||
| return passport if is_valid(passport) else None | ||
|
|
||
|
|
||
| def generate() -> str: | ||
| """ | ||
| Generate a random valid Brazilian passport number string. | ||
|
|
||
| This function generates a random Brazilian passport number string. | ||
|
|
||
| Returns: | ||
| str: A random valid passport number string. | ||
|
|
||
| Example: | ||
| >>> generate() | ||
| "RY393097" | ||
| >>> generate() | ||
| "ZS840088" | ||
| """ | ||
|
|
||
| letters = "".join(random.choices(string.ascii_uppercase, k=2)) | ||
| digits = "".join(random.choices(string.digits, k=6)) | ||
|
|
||
| return f"{letters}{digits}" |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.