Created
December 15, 2019 15:09
-
-
Save discrimy/13887122f297efe0bcd0b3964e1edc3a to your computer and use it in GitHub Desktop.
pydi example
Random person generator
This file contains 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 flask import Flask, jsonify | |
from pydi.context import BeanContext | |
from services import * | |
if __name__ == '__main__': | |
ctx = BeanContext() | |
ctx.scan(globals()) | |
ctx.init() | |
person_service = ctx.get_bean(RandomPersonService) | |
app = Flask(__name__) | |
@app.route('/rand_person/<seed>') | |
@app.route('/rand_person') | |
def random_person(seed=None): | |
person = person_service.generate(seed) | |
return jsonify({ | |
'fullname': person.fullname, | |
'tel': person.tel, | |
'login': person.login, | |
'password': person.password, | |
}) | |
app.run() |
This file contains 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
class Person: | |
def __init__(self, | |
fullname: str, | |
tel: str, | |
login: str, | |
password: str): | |
self.password = password | |
self.login = login | |
self.tel = tel | |
self.fullname = fullname |
This file contains 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
import random | |
import string | |
from typing import Optional | |
import slugify | |
from model import Person | |
from pydi.context import bean | |
@bean(bean_id='fullname_service') | |
class RandomFullnameService: | |
firstNames = [ | |
"Агап", | |
"Всеслав", | |
"Виссарион", | |
"Евстигней", | |
"Степан", | |
"Алексей", | |
"Викентий", | |
"Иларион", | |
"Вадим", | |
"Аскольд", | |
] | |
secondNames = [ | |
"Степанишин", | |
"Ялбачев", | |
"Закревский", | |
"Шаталов", | |
"Курушин", | |
"Горяинов", | |
"Коденко", | |
"Барановский", | |
"Хрущёв", | |
"Элинский", | |
] | |
thirdNames = [ | |
"Владимирович", | |
"Федорович", | |
"Вадимович", | |
"Гаврилевич", | |
"Мартьянович", | |
"Куприянович", | |
"Родионович", | |
"Кондратович", | |
"Зиновиевич", | |
"Евстафиевич", | |
] | |
def generate(self, seed: Optional[str]) -> str: | |
random.seed(seed) | |
return f'{random.choice(self.secondNames)} ' \ | |
f'{random.choice(self.firstNames)} ' \ | |
f'{random.choice(self.thirdNames)}' | |
@bean(bean_id='tel_service') | |
class RandomTelService: | |
def generate(self, seed: Optional[str]) -> str: | |
random.seed(seed) | |
return '{} ({:03}) {:03}-{:02}-{:02}'.format(random.randint(1, 1000), | |
random.randint(1, 1000), | |
random.randint(0, 1000), | |
random.randint(0, 100), | |
random.randint(0, 100)) | |
@bean(bean_id='login_service') | |
class RandomLoginService: | |
def generate(self, seed: Optional[str], fullname: str) -> str: | |
random.seed(seed) | |
login = slugify.slugify(fullname.split()[1]) | |
login += str(random.randint(1, 1000)) | |
return login | |
@bean(bean_id='password_service') | |
class RandomPasswordService: | |
def generate(self, seed: Optional[str]) -> str: | |
random.seed(seed) | |
return ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(10)) | |
@bean(bean_id="person_service", | |
fullname_service="fullname_service", | |
tel_service="tel_service", | |
login_service="login_service", | |
password_service="password_service") | |
class RandomPersonService: | |
def __init__(self, | |
fullname_service: RandomFullnameService, | |
tel_service: RandomTelService, | |
login_service: RandomLoginService, | |
password_service: RandomPasswordService): | |
self.fullname_service = fullname_service | |
self.tel_service = tel_service | |
self.login_service = login_service | |
self.password_service = password_service | |
def generate(self, seed: Optional[str]) -> Person: | |
fullname = self.fullname_service.generate(seed) | |
return Person( | |
fullname, | |
self.tel_service.generate(seed), | |
self.login_service.generate(seed, fullname), | |
self.password_service.generate(seed) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment