Skip to main content
edited tags
Link
Reinderien
  • 71.2k
  • 5
  • 76
  • 257
Tweeted twitter.com/StackCodeReview/status/1446037584656969735
Became Hot Network Question
deleted 3 characters in body; edited title
Source Link
riskypenguin
  • 3.5k
  • 1
  • 10
  • 28

Python password generator | Do you consider this a "clean code"?class

I am a self taught python developer and I trying to write clean code as mush as possible. Since I do not have any environment that can give me constructive criticism, I would like to hear your opinions, so, here it is a python password generator, I know that something like that can be made with single function, but this is just "project as a placeholder" if that make sense..

So does this consider "good practice"? -andAnd what can I do better?

import sys
import yaml
import string
import random
from typing import Union


class GeneratePassword:
    """
    Object that generate password. Password size is taken from yaml config file,
    it can be week(64), medium(128) or strong(256). Need to pass that as
    string argument to cls.generate_password function.
    """
    @classmethod
    def generate_password(cls, password_level: str):
        conf = cls.check_config_file()
        if conf is not None:
            security_level = conf.get(password_level)
            new_password = cls.generate_gibberish(security_level)
            return new_password

    @staticmethod
    def check_config_file() -> Union[dict, None]:
        try:
            with open('./config/security_level_conf.yml') as pass_conf:
                current_data = yaml.safe_load(pass_conf)
                return current_data
        except FileNotFoundError:
            sys.exit('Config file not found.')

    @staticmethod
    def generate_gibberish(security_level: int) -> str:
        choices = string.punctuation + string.ascii_letters + string.digits
        password = []
        for i in range(security_level):
            character = random.choice(choices)
            password.append(character)
        return "".join(password)


if __name__ == '__main__':
     print(GeneratePassword.generate_password('week'))

Python password generator | Do you consider this a "clean code"?

I am a self taught python developer and I trying to write clean code as mush as possible. Since I do not have any environment that can give me constructive criticism, I would like to hear your opinions, so, here it is a python password generator, I know that something like that can be made with single function, but this is just "project as a placeholder" if that make sense..

So does this consider "good practice"? -and what can I do better?

import sys
import yaml
import string
import random
from typing import Union


class GeneratePassword:
    """
    Object that generate password. Password size is taken from yaml config file,
    it can be week(64), medium(128) or strong(256). Need to pass that as
    string argument to cls.generate_password function.
    """
    @classmethod
    def generate_password(cls, password_level: str):
        conf = cls.check_config_file()
        if conf is not None:
            security_level = conf.get(password_level)
            new_password = cls.generate_gibberish(security_level)
            return new_password

    @staticmethod
    def check_config_file() -> Union[dict, None]:
        try:
            with open('./config/security_level_conf.yml') as pass_conf:
                current_data = yaml.safe_load(pass_conf)
                return current_data
        except FileNotFoundError:
            sys.exit('Config file not found.')

    @staticmethod
    def generate_gibberish(security_level: int) -> str:
        choices = string.punctuation + string.ascii_letters + string.digits
        password = []
        for i in range(security_level):
            character = random.choice(choices)
            password.append(character)
        return "".join(password)


if __name__ == '__main__':
     print(GeneratePassword.generate_password('week'))

Python password generator class

I am a self taught python developer and I trying to write clean code as mush as possible. Since I do not have any environment that can give me constructive criticism, I would like to hear your opinions, so, here it is a python password generator, I know that something like that can be made with single function, but this is just "project as a placeholder" if that make sense.

So does this consider "good practice"? And what can I do better?

import sys
import yaml
import string
import random
from typing import Union


class GeneratePassword:
    """
    Object that generate password. Password size is taken from yaml config file,
    it can be week(64), medium(128) or strong(256). Need to pass that as
    string argument to cls.generate_password function.
    """
    @classmethod
    def generate_password(cls, password_level: str):
        conf = cls.check_config_file()
        if conf is not None:
            security_level = conf.get(password_level)
            new_password = cls.generate_gibberish(security_level)
            return new_password

    @staticmethod
    def check_config_file() -> Union[dict, None]:
        try:
            with open('./config/security_level_conf.yml') as pass_conf:
                current_data = yaml.safe_load(pass_conf)
                return current_data
        except FileNotFoundError:
            sys.exit('Config file not found.')

    @staticmethod
    def generate_gibberish(security_level: int) -> str:
        choices = string.punctuation + string.ascii_letters + string.digits
        password = []
        for i in range(security_level):
            character = random.choice(choices)
            password.append(character)
        return "".join(password)


if __name__ == '__main__':
    print(GeneratePassword.generate_password('week'))
Source Link

Python password generator | Do you consider this a "clean code"?

I am a self taught python developer and I trying to write clean code as mush as possible. Since I do not have any environment that can give me constructive criticism, I would like to hear your opinions, so, here it is a python password generator, I know that something like that can be made with single function, but this is just "project as a placeholder" if that make sense..

So does this consider "good practice"? -and what can I do better?

import sys
import yaml
import string
import random
from typing import Union


class GeneratePassword:
    """
    Object that generate password. Password size is taken from yaml config file,
    it can be week(64), medium(128) or strong(256). Need to pass that as
    string argument to cls.generate_password function.
    """
    @classmethod
    def generate_password(cls, password_level: str):
        conf = cls.check_config_file()
        if conf is not None:
            security_level = conf.get(password_level)
            new_password = cls.generate_gibberish(security_level)
            return new_password

    @staticmethod
    def check_config_file() -> Union[dict, None]:
        try:
            with open('./config/security_level_conf.yml') as pass_conf:
                current_data = yaml.safe_load(pass_conf)
                return current_data
        except FileNotFoundError:
            sys.exit('Config file not found.')

    @staticmethod
    def generate_gibberish(security_level: int) -> str:
        choices = string.punctuation + string.ascii_letters + string.digits
        password = []
        for i in range(security_level):
            character = random.choice(choices)
            password.append(character)
        return "".join(password)


if __name__ == '__main__':
     print(GeneratePassword.generate_password('week'))