-
-
Notifications
You must be signed in to change notification settings - Fork 72
World's NIF #457
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
Open
carolcardoso080
wants to merge
7
commits into
brazilian-utils:main
Choose a base branch
from
carolcardoso080:456
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
World's NIF #457
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bc0bd8e
Pasta com os 2 primeiros nif's
carolcardoso080 71f6f4a
Reformatted code to comply with pre-push hook
carolcardoso080 385803a
Retirando a pasta worlds_nif da pasta brutils
carolcardoso080 c0aec1f
Ajustes Cumbuca e de 'NIF' para 'TIN'
carolcardoso080 275dc94
Fix code formatting
carolcardoso080 476350c
Ajustes Cumbuca e de 'NIF' para 'TIN'
carolcardoso080 85a3a7c
Atualizando
carolcardoso080 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
from random import randint | ||
|
||
# FORMATTING | ||
############# | ||
|
||
|
||
def remove_symbols(dirty_nif): # type: (str) -> str | ||
""" | ||
Removes spaces, dots, dashes, and other symbols from a NIF. | ||
|
||
Args: | ||
dirty_nif (str): A string containing a NIF with possible non-numeric characters. | ||
|
||
Returns: | ||
str: A cleaned NIF containing only numeric digits. | ||
|
||
Example: | ||
remove_symbols("123-456-7890") -> "1234567890" | ||
""" | ||
return "".join(filter(str.isdigit, dirty_nif)) | ||
|
||
|
||
def format_nif(nif): # type: (str) -> str | ||
""" | ||
Formats a Haitian NIF for display with visual aid symbols. | ||
|
||
This function formats a numbers-only NIF string by adding dashes | ||
for better readability, commonly used in Haitian documents. | ||
|
||
Args: | ||
nif (str): A numbers-only NIF string of exactly 10 digits. | ||
|
||
Returns: | ||
str: A formatted NIF string with dashes, or None if the input is invalid. | ||
|
||
Example: | ||
format_nif("1234567890") -> "123-456-7890" | ||
""" | ||
# Clean the NIF first, in case it contains symbols | ||
nif = remove_symbols(nif) | ||
|
||
# Ensure the NIF is valid for formatting | ||
if len(nif) != 10: | ||
return None | ||
|
||
# Add dashes after the third and sixth digits | ||
return "{}-{}-{}".format(nif[:3], nif[3:6], nif[6:]) | ||
|
||
|
||
# OPERATIONS | ||
############# | ||
|
||
|
||
def validate(nif): # type: (str) -> bool | ||
""" | ||
Validates a Haitian NIF by checking its structure. | ||
|
||
Args: | ||
nif (str): A string containing a NIF to validate. | ||
|
||
Returns: | ||
bool: True if the NIF is valid, otherwise False. | ||
|
||
Example: | ||
validate("1234567890") -> True | ||
""" | ||
# Clean the NIF first, in case it contains symbols | ||
nif = remove_symbols(nif) | ||
|
||
# Ensure the NIF has exactly 10 digits | ||
if len(nif) != 10: | ||
print("The Haitian NIF must have exactly 10 digits.") | ||
return False | ||
|
||
# Ensure the NIF contains only numeric characters | ||
if not nif.isdigit(): | ||
print("The Haitian NIF must contain only numeric characters.") | ||
return False | ||
|
||
print("The Haitian NIF is valid.") | ||
return True | ||
|
||
|
||
def generate(): # type: () -> str | ||
""" | ||
Generates a random valid Haitian NIF. | ||
|
||
Returns: | ||
str: A valid randomly generated NIF. | ||
|
||
Example: | ||
generate() -> "1234567890" | ||
""" | ||
return str(randint(1000000000, 9999999999)) # Generate a 10-digit NIF | ||
|
||
|
||
# USAGE EXAMPLE | ||
|
||
nif = generate() | ||
print(f"Generated NIF: {nif} - Valid? {validate(nif)}") | ||
|
||
# Validate a NIF (input with symbols, such as dots and dashes) | ||
user_nif = input("Enter your NIF: ") | ||
print(validate(user_nif)) | ||
|
||
# Format a NIF (input with symbols, such as dots, dashes, and spaces) | ||
formatted_nif = format_nif(user_nif) | ||
if formatted_nif: | ||
print(f"Formatted NIF: {formatted_nif}") | ||
else: | ||
print("Invalid NIF.") |
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,143 @@ | ||||||
from random import randint | ||||||
|
||||||
# FORMATTING | ||||||
############# | ||||||
|
||||||
|
||||||
def remove_symbols(dirty_nif): # type: (str) -> str | ||||||
""" | ||||||
Removes spaces, dots, and other symbols from a NIF. | ||||||
|
||||||
Args: | ||||||
dirty_nif (str): A string containing a NIF with possible non-numeric characters. | ||||||
|
||||||
Returns: | ||||||
str: A cleaned NIF containing only numeric digits. | ||||||
|
||||||
Example: | ||||||
remove_symbols("123.456.789") -> "123456789" | ||||||
""" | ||||||
return "".join(filter(str.isdigit, dirty_nif)) | ||||||
|
||||||
|
||||||
def format_nif(nif): # type: (str) -> str | ||||||
""" | ||||||
Formats a Portuguese NIF for display with visual aid symbols. | ||||||
|
||||||
This function formats a numbers-only NIF string by adding periods and | ||||||
a dash for better readability, commonly used in Portuguese documents. | ||||||
|
||||||
Args: | ||||||
nif (str): A numbers-only NIF string of exactly 9 digits. | ||||||
|
||||||
Returns: | ||||||
str: A formatted NIF string with periods and a dash, or None | ||||||
if the input is invalid. | ||||||
|
||||||
Example: | ||||||
format_nif("123456789") -> "123.456.789-0" | ||||||
""" | ||||||
# Clean the NIF first, in case it's with any symbols | ||||||
nif = remove_symbols(nif) | ||||||
|
||||||
# Ensure the NIF is valid for formatting | ||||||
if len(nif) != 9: | ||||||
return None | ||||||
|
||||||
# Add periods after the third and sixth digits, and a dash before the last digit | ||||||
return "{}.{}.{}-{}".format(nif[:3], nif[3:6], nif[6:8], nif[8]) | ||||||
|
||||||
|
||||||
# OPERATIONS | ||||||
############# | ||||||
|
||||||
|
||||||
def validate(nif): # type: (str) -> bool | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mesmo esquema aqui, mudar o nome do método para is_valid:
Suggested change
|
||||||
""" | ||||||
Validates a NIF by checking its structure, type, and control digit. | ||||||
|
||||||
Args: | ||||||
nif (str): A string containing a NIF to validate. | ||||||
|
||||||
Returns: | ||||||
bool: True if the NIF is valid, otherwise False. | ||||||
|
||||||
Example: | ||||||
validate("123456789") -> False (control digit mismatch) | ||||||
""" | ||||||
# Clean the NIF first, in case it's with any symbols | ||||||
nif = remove_symbols(nif) | ||||||
|
||||||
# 1. Ensure the NIF has exactly 9 characters and is numeric | ||||||
if len(nif) != 9: | ||||||
print("The NIF must have exactly 9 digits.") | ||||||
return False | ||||||
|
||||||
if not nif.isdigit(): | ||||||
print("The NIF must contain only numbers.") | ||||||
return False | ||||||
|
||||||
# 2. Determine the taxpayer type by the first digit | ||||||
first_digit = int(nif[0]) | ||||||
|
||||||
# List of valid first digits for Portuguese NIFs | ||||||
valid_types = [1, 2, 4, 5, 6, 7, 9] | ||||||
|
||||||
if first_digit not in valid_types: | ||||||
print( | ||||||
f"The first digit {first_digit} is not valid for a Portuguese NIF." | ||||||
) | ||||||
return False | ||||||
|
||||||
base, check_digit = nif[:8], int(nif[8]) | ||||||
return _calculate_digit(base) == check_digit | ||||||
|
||||||
|
||||||
def generate(): # type: () -> str | ||||||
""" | ||||||
Generates a random valid NIF. | ||||||
|
||||||
Returns: | ||||||
str: A valid randomly generated NIF. | ||||||
|
||||||
Example: | ||||||
generate() -> "123456780" | ||||||
""" | ||||||
base = str(randint(10000000, 99999999)) # Generate the first 8 digits | ||||||
return base + str(_calculate_digit(base)) | ||||||
|
||||||
|
||||||
def _calculate_digit(base): # type: (str) -> int | ||||||
""" | ||||||
Calculates the control digit for a given NIF base. | ||||||
|
||||||
Args: | ||||||
base (str): The first 8 digits of a NIF. | ||||||
|
||||||
Returns: | ||||||
int: The control digit calculated using the Portuguese NIF rules. | ||||||
|
||||||
Example: | ||||||
_calculate_digit("12345678") -> 0 | ||||||
""" | ||||||
weights = range(9, 1, -1) # Descending weights from 9 to 2 | ||||||
total = sum(int(digit) * weight for digit, weight in zip(base, weights)) | ||||||
remainder = total % 11 | ||||||
return 0 if remainder < 2 else 11 - remainder | ||||||
|
||||||
|
||||||
# USAGE EXAMPLE | ||||||
|
||||||
nif = generate() | ||||||
print(f"Generated NIF: {nif} - Valid? {validate(nif)}") | ||||||
|
||||||
# Validate a NIF (input with symbols, such as dots and dashes) | ||||||
user_nif = input("Enter your NIF: ") | ||||||
print(validate(user_nif)) | ||||||
|
||||||
# Format a NIF (input with symbols, such as dots and dashes) | ||||||
formatted_nif = format_nif(user_nif) | ||||||
if formatted_nif: | ||||||
print(f"Formatted NIF: {formatted_nif}") | ||||||
else: | ||||||
print("Invalid NIF.") |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aqui, para seguir o padrão dos nomes de métodos, o nome deve ser is_valid. Ficaria assim: