-
Notifications
You must be signed in to change notification settings - Fork 46
Creating unique names for new nets #334
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
base: main
Are you sure you want to change the base?
Changes from all commits
9c8f20d
7c3900d
18023d5
a87c75a
2f268f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,7 @@ | ||
| from os import urandom | ||
| import random | ||
| import json | ||
| from datetime import datetime | ||
|
|
||
| from flask_login import UserMixin | ||
| from flask_sqlalchemy import SQLAlchemy | ||
|
|
@@ -10,6 +13,25 @@ | |
| import psycopg2 | ||
|
|
||
|
|
||
| def generate_cosmic_name(): | ||
| """Generates unique cosmic object name with timestamp""" | ||
| try: | ||
| with open("static/cosmic_names.json", "r", encoding="utf-8") as f: | ||
| config = json.load(f) | ||
| except (FileNotFoundError, json.JSONDecodeError, KeyError): | ||
| config = { | ||
| "cosmic_objects": ["Звезда", "Галактика", "Планета"], | ||
| "prefixes": ["", "XB-", "NGC-"], | ||
| } | ||
|
|
||
| timestamp = datetime.now().strftime("%d%m%y") | ||
| random_num = random.randint(100, 999) | ||
| cosmic_type = random.choice(config["cosmic_objects"]) | ||
| prefix = random.choice(config["prefixes"]) | ||
|
|
||
| return f"{cosmic_type} {prefix}{timestamp}{random_num}" | ||
|
|
||
|
|
||
| convention = { | ||
| "ix": "ix_%(column_0_label)s", | ||
| "uq": "uq_%(table_name)s_%(column_0_name)s", | ||
|
|
@@ -44,7 +66,7 @@ class Network(db.Model): # type:ignore[name-defined] | |
| author_id = db.Column(BigInteger, ForeignKey("user.id"), nullable=False) | ||
|
|
||
| guid = db.Column(Text, nullable=False, unique=True) | ||
| title = db.Column(Text, default="Новая сеть", nullable=False) | ||
| title = db.Column(Text, default=lambda: generate_cosmic_name(), nullable=False) | ||
|
Contributor
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. Тут точно лямбда нужна? Нельзя просто
Collaborator
Author
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. Да, иначе результат функции он кеширует и при создании сети каждый раз будет одно и то же название. Лямбда же позволяет при каждом создании сети вызывать функцию
Contributor
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. Так если default= generate_cosmic_name использовать, мы тоже чисто функцию передаём, нет? |
||
|
|
||
| description = db.Column(Text, default="", nullable=True) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| { | ||
| "cosmic_objects": [ | ||
| "Комета", | ||
| "Звезда", | ||
| "Галактика", | ||
| "Туманность", | ||
| "Спутник", | ||
| "Вселенная", | ||
| "Астероид", | ||
| "Планета", | ||
| "Метеорит", | ||
| "Квазар", | ||
| "Пульсар", | ||
| "Чёрная дыра", | ||
| "Суперновая", | ||
| "Созвездие", | ||
| "Космодром", | ||
| "Телескоп", | ||
| "Ракета", | ||
| "Орбита", | ||
| "Экзопланета", | ||
| "Космический корабль", | ||
| "Межзвёздный объект", | ||
| "Космическая станция", | ||
| "Темная материя", | ||
| "Космический зонд" | ||
| ], | ||
| "prefixes": ["", "XB-", "NGC-", "HD-", "M-", "IC-", "PG-", "PSR-"] | ||
| } |
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.
Может лучше из файла читать их какого-то, а то функция на 40 строчек почти