def __init__(self, username, password=None, **kwargs): # .. Model.__init__(self, username=username, password=password, **kwargs) # .. if password: self.set_password(password) else: self.password = None
def get_model(user, database, table_name): """Open a cursor to perform database operations Args: user: username; database: name of a database; table_name: name of a table. Return: Cursor to perform database operations """ cur = Model(user, database, table_name) cur.connect() cur.get_cursor() return cur
class Main: def __init__(self, database): self.database = Model(database) self.init_app() def init_app(self): while True: try: data = self.database.read_database() for d in data: print(d) print('____________________________') print(""" What do you want to do? \n 1. Write a new todo \n 2. Update an item in your list \n 3. Delete an item in your list \n 4. Exit """) input_data = input("Choice: ") if input_data == "e": break elif int(input_data) == 1: self.write_data() elif int(input_data) == 2: self.update_data() elif int(input_data) == 3: self.delete_data() elif int(input_data) == 4: print("Goodbye!") sys.exit() except KeyboardInterrupt: print("Goodbye") sys.exit() def write_data(self): user_input = input("Write new todo: ") self.database.write_data(str(user_input)) def update_data(self): try: row_id, text = input( "Input the number in the database and the entry,\nseparated by ':'" ).split(':') except: print("Invalid input") else: self.database.update_data(row_id=row_id, text=text) def delete_data(self): try: row_id = input("Input the ID of you want to delete: ") except: print("Invalid input") else: self.database.delete_data(row_id=row_id)
def filter_models(args_dict): wargs = {} if args_dict["lr"] != None: wargs["learning_rate"] = args_dict["lr"] if args_dict["e"] != None: wargs["epochs"] = args_dict["e"] if args_dict["nw"] != None: wargs["network_width"] = args_dict["nw"] if args_dict["t"] != None: wargs["tag"] = args_dict["t"] if args_dict["t"] == "None": wargs["tag"] = None return Model.objects(**wargs).order_by('-precision_delay')
async def store(self, obj: Model, fields_: Optional[Dict] = None, rewrite=False, **kwargs): collection = self.get_collection(obj) fields = dict(obj.serialize()) if fields_: fields.update(fields_) fields.update(kwargs) # TODO: Use args and kwargs for this! if (id_ := obj._id) is None: await collection.insert_one(fields)
def create_model(car_id): # Function for creating a car model. try: login_session['access_token'] logged_in = True except KeyError: logged_in = False if logged_in: if request.method == 'POST': newItem = Model(name=request.form['name'], car_id=car_id, username=login_session['username']) session.add(newItem) session.commit() return redirect(url_for('cars')) else: return render_template('new_model.html', car_id=car_id) else: return "You have no rights to create models."
class Ticket(db): id = db.fields(db.serial(), db.primary(), db.unique()) date_created = db.fields(db.datetime(auto_add=True)) payment_method = db.fields(db.string(10), db.not_null()) user_id = db.fields(db.integer(), db.not_null(), db.foreignkey('users.id', on_delete_cascade=True)) showtime_id = db.fields(db.integer(), db.not_null(), db.foreignkey( 'showtime.id', on_delete_cascade=True)) seat_id = db.fields(db.integer(), db.foreignkey( 'seat.id', on_delete_cascade=True)) class _Meta_: unique_together = ('showtime_id', 'seat_id')
class Users(db): id = db.fields(db.serial(), db.primary()) date_created = db.fields(db.datetime(auto_add=True)) email = db.fields(db.string(100), db.unique(), db.not_null(True)) password = db.fields(db.string(100), db.not_null(True)) name = db.fields(db.string(100), db.not_null(False)) is_staff = db.fields(db.boolean(default=False), db.not_null()) def find(self, operator, joins, kwargs): if any('report' in item.values() for item in kwargs): dict_={ 'ticket_enddate': datetime.now().strftime("%Y-%m-%d"), 'ticket_startdate': datetime.now().strftime("%Y-%m-%d"), 'total':0 } for item in kwargs: if item.get('field') in ['ticket_enddate', 'ticket_startdate', 'total']: dict_[item.get('field')] = item.get('value') return get_cte_query('user_filtering').format(**dict_) update = [] for item in kwargs: if item.get('field') not in ['ticket_enddate', 'ticket_startdate', 'total', 'report']: update.append(item) return super().find(operator, joins, update)
def __init__(self, database): self.database = Model(database) self.init_app()
class CinemaHall(db): id = db.fields(db.serial(), db.primary(), db.unique()) date_created = db.fields(db.datetime(auto_add=True)) name = db.fields(db.string(100), db.not_null(), db.unique()) description = db.fields(db.string(100), db.not_null())
class Seat(db): id = db.fields(db.serial(), db.primary(), db.unique()) name = db.fields(db.string(100), db.not_null()) number = db.fields(db.integer(), db.not_null()) date_created = db.fields(db.datetime(auto_add=True)) cinema_hall = db.fields( db.integer(), db.foreignkey("cinemahall.id", on_delete_cascade=True)) class _Meta_: unique_together = ("name", "cinema_hall", "number")
class Movie(db): id = db.fields(db.serial(), db.primary(), db.unique()) date_created = db.fields(db.datetime(auto_add=True)) name = db.fields(db.string(100), db.not_null(True)) category = db.fields(db.string(100), db.not_null(True)) date_of_release = db.fields(db.datetime(), db.not_null()) rating = db.fields(db.integer(), db.not_null()) length = db.fields(db.time(), db.not_null()) summary = db.fields(db.text(), db.not_null(False))
class ShowTime(db): id = db.fields(db.serial(), db.primary(), db.unique()) date_created = db.fields(db.datetime(auto_add=True)) show_datetime = db.fields(db.datetime(), db.not_null()) movie_id = db.fields( db.integer(), db.not_null(), db.foreignkey("movie.id", on_delete_cascade=True), ) price = db.fields(db.numeric(), db.not_null()) cinema_hall = db.fields( db.integer(), db.foreignkey("cinemahall.id", on_delete_cascade=True)) class _Meta_: unique_together = ("show_datetime", "movie_id") def insert_query(self, records): query = super().insert_query(records) return get_cte_query("showtime_insert").format(query) def clean(self, query): return get_cte_query("clean_showtime").format(**query) def update(self, id, operator, **kwargs): query = super().update(id, operator, **kwargs) return get_cte_query("showtime_insert").format(query)
from database import ConnectionFactory, Model if __name__ == "__main__": conn = ConnectionFactory("SQLiteConnect").make() conn.connect() model = Model(conn.connection) model.create() model.insert("Candido", "77777777777", "*****@*****.**") model.insert("José", "97777777777", "*****@*****.**") print(model.find_all()) print(model.find("77777777777")) print(model.delete("77777777777")) print(model.find_all()) conn.disconnect()
with open(args["f"], "r") as f: parameters = json.load(f) parameter_combinations = [] for lr in parameters["lr"]: for nw in parameters["nw"]: for ep in parameters["ep"]: parameter_combinations.append([lr, ep, nw]) print("{} parameter combinations, training started...".format( len(parameter_combinations))) bench.start() mongoengine.connect("network_training") for parameter in parameter_combinations: runtime, model_path = train_network.train_network(parameter[0], parameter[1], "cpu", parameter[2]) classification_report = test_network.test_network(parameter[2], model_path) model = Model.save_model(runtime, "cpu", parameter[0], parameter[1], parameter[2], model_path, classification_report, tag=args["t"]) model.save() bench.stop() print("Trained all outcomes [{}]".format(bench.get_time()))
class Users(db): id = db.fields(db.serial(), db.primary()) date_created = db.fields(db.datetime(auto_add=True)) email = db.fields(db.string(100), db.unique(), db.not_null(True)) password = db.fields(db.string(100), db.not_null(True)) name = db.fields(db.string(100), db.not_null(False)) is_staff = db.fields(db.boolean(default=False), db.not_null()) def find(self, operator, joins, kwargs): if any("report" in item.values() for item in kwargs): dict_ = { "ticket_enddate": datetime.now().strftime("%Y-%m-%d"), "ticket_startdate": datetime.now().strftime("%Y-%m-%d"), "total": 0, } for item in kwargs: if item.get("field") in [ "ticket_enddate", "ticket_startdate", "total", ]: dict_[item.get("field")] = item.get("value") return get_cte_query("user_filtering").format(**dict_) update = [] for item in kwargs: if item.get("field") not in [ "ticket_enddate", "ticket_startdate", "total", "report", ]: update.append(item) return super().find(operator, joins, update)