Skip to content

Commit

Permalink
Code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
KarthikUdyawar committed Aug 16, 2023
1 parent d810183 commit 51550ad
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 35 deletions.
8 changes: 3 additions & 5 deletions src/api/components/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,21 @@
from fastapi import HTTPException

from src.api.schema import (
PredictionRequest,
PredictionResponse,
GenerateRequest,
GenerateResponse,
PredictionRequest,
PredictionResponse,
)
from src.api.utils import (
generate_password,
calc_class_strength,
calc_entropy,
calc_strength,
display_time,
entropy_to_crack_time,
generate_password,
)

from src.middleware.exception import CustomException
from src.middleware.logger import logger

from src.utils.data_validation import is_valid_password


Expand Down
9 changes: 3 additions & 6 deletions src/api/routers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@

from fastapi import APIRouter

from src.api.components import (
password_strength_component,
generate_strong_password,
)
from src.api.components import generate_strong_password, password_strength_component
from src.api.schema import (
PredictionRequest,
PredictionResponse,
GenerateRequest,
GenerateResponse,
PredictionRequest,
PredictionResponse,
)

router = APIRouter()
Expand Down
2 changes: 1 addition & 1 deletion src/api/schema/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""This module defines classes for password strength prediction and generation
"""This module defines classes for password strength prediction and generation
requests and responses using Pydantic models."""
from pydantic import BaseModel, Field

Expand Down
45 changes: 22 additions & 23 deletions src/api/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Utility module for password strength calculation and related calculations."""
import math
import secrets
from typing import Any

from src.interface.config import CustomData
from src.pipe.pipeline import Pipeline

Expand Down Expand Up @@ -28,7 +30,7 @@ def generate_password(length: int) -> str:
return "".join(secrets.choice(CHARS_SET) for _ in range(length))


def calc_strength(password: str) -> float:
def calc_strength(password: str) -> float | Any:
"""Calculate the strength of a given password.
Args:
Expand Down Expand Up @@ -124,25 +126,22 @@ def display_time(seconds: float) -> str:
Returns:
str: The human-readable time representation.
"""
time_string = "instant"
if seconds >= 1 and seconds < SECONDS_IN_MINUTE:
time_string = f"{seconds} seconds"
if seconds < SECONDS_IN_HOUR:
time_string = (
f"{str(1 + math.ceil(seconds / SECONDS_IN_MINUTE))} minutes"
)
if seconds < SECONDS_IN_DAY:
time_string = f"{str(1 + math.ceil(seconds / SECONDS_IN_HOUR))} hours"
if seconds < SECONDS_IN_MONTH:
time_string = f"{str(1 + math.ceil(seconds / SECONDS_IN_DAY))} days"
if seconds < SECONDS_IN_YEAR:
time_string = (
f"{str(1 + math.ceil(seconds / SECONDS_IN_MONTH))} months"
)
if seconds < SECONDS_IN_CENTURY:
time_string = f"{str(1 + math.ceil(seconds / SECONDS_IN_YEAR))} years"
else:
time_string = (
f"{str(1 + math.ceil(seconds / SECONDS_IN_CENTURY))} centuries"
)
return time_string
return (
"instant"
if seconds < 1
else f"{seconds:.2f} seconds"
if seconds < SECONDS_IN_MINUTE
else f"{1 + math.ceil(seconds / SECONDS_IN_MINUTE):.2f} minutes"
if seconds < SECONDS_IN_HOUR
else f"{1 + math.ceil(seconds / SECONDS_IN_HOUR):.2f} hours"
if seconds < SECONDS_IN_DAY
else f"{1 + math.ceil(seconds / SECONDS_IN_DAY):.2f} days"
if seconds < SECONDS_IN_MONTH
else f"{1 + math.ceil(seconds / SECONDS_IN_MONTH):.2f} months"
if seconds < SECONDS_IN_YEAR
else f"{1 + math.ceil(seconds / SECONDS_IN_YEAR):.2f} years"
if seconds < SECONDS_IN_CENTURY
else f"{1 + math.ceil(seconds / SECONDS_IN_CENTURY):.2f} centuries"
if seconds < 1_00_00_000 * SECONDS_IN_CENTURY
else "Eternity"
)

0 comments on commit 51550ad

Please sign in to comment.