Commit b43e4421 authored by ajil.k's avatar ajil.k

updated

parent fb23ae7e
prototype
\ No newline at end of file
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" /> <excludeFolder url="file://$MODULE_DIR$/venv" />
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="jdk" jdkName="Python 3.9 (venv)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>
</module> </module>
\ No newline at end of file
# importing libraries
import uvicorn
from scripts.logging.logger import logger
if __name__ == "__main__":
try:
uvicorn.run("main:app", port=8000)
except Exception as e:
logger.error(e)
print("Error-", e)
import copy import uvicorn
from fastapi import FastAPI
from scripts.services.creational_design_structure import prototype
class Character:
def __init__(self, name, health, speed, damage, skills): app = FastAPI()
self.name = name app.include_router(prototype)
self.health = health if __name__ == "__main__":
self.speed = speed try:
self.damage = damage uvicorn.run(app, port=8000)
self.skills = skills except Exception as e:
print("Error-", e)
def __str__(self):
# Returns
return f"{self.name} (health: {self.health}, speed: {self.speed}, damage: {self.damage}, skills: {self.skills})"
class CharacterPrototype:
def __init__(self):
self.character = {}
# Method for creating new character
def create_character(self, name, character):
self.character[name] = character
# Method for cloning the base character and update its attributes
def clone(self, name, **attrs):
character = copy.deepcopy(self.character.get(name))
character.__dict__.update(attrs)
return character
# Creates a prototype for a base character
base_character = Character("Character", health=100, speed=5, damage=10, skills=12)
# Creates a prototype object and register the base character
prototype = CharacterPrototype()
base_character_name = "normal_character"
prototype.create_character(base_character_name, base_character)
# Creates 2 new characters by cloning the base character and updating its attributes
four_star_character = prototype.clone(base_character_name, speed=20, damage=25, skills=15)
five_star_character = prototype.clone(base_character_name, health=125, speed=30, damage=50, skills=20)
# Printing the characters
print("Base Character attribute:\n", base_character)
print("4 STAR Character attribute:\n", four_star_character)
print("5 STAR Character attribute:\n", five_star_character)
class EndPoints:
root = "/"
create_base_character = "/create_base_character/"
create_prototypes = "/create_prototypes/"
create_characters = "/create_characters/"
\ No newline at end of file
from typing import Optional
from pydantic import BaseModel
class PrototypeCharacter(BaseModel):
health: Optional[int]
speed: Optional[int]
damage: Optional[int]
skills: Optional[int]
from scripts.core.handlers.prototyping import Character, CharacterPrototype
from scripts.logging.logger import logger
def create_new_character(prototypes):
try:
# Creates a prototype for a base character
base_character = Character("Character", health=100, speed=5, damage=10, skills=12)
# Creates a prototype object and register the base character
prototype = CharacterPrototype()
base_character_name = "normal_character"
prototype.create_character(base_character_name, base_character)
print(prototypes)
# Creates a new character by cloning the base character and updating its attributes
new_character = prototype.clone(base_character_name,
health=prototypes.health,
speed=prototypes.speed,
damage=prototypes.damage,
skills=prototypes.skills)
print(new_character)
return base_character, new_character
except Exception as e:
logger.error(e)
import copy
# Base Class
class Character:
def __init__(self, name, health, speed, damage, skills):
self.name = name
self.health = health
self.speed = speed
self.damage = damage
self.skills = skills
def __str__(self):
# Returns
return f"{self.name} (health: {self.health}, speed: {self.speed}, damage: {self.damage}, skills: {self.skills})"
class CharacterPrototype:
def __init__(self):
self.character = {}
# Method for creating new character
def create_character(self, name, character):
self.character[name] = character
# Method for cloning the base character and update its attributes
def clone(self, name, **attrs):
character = copy.deepcopy(self.character.get(name))
character.__dict__.update(attrs)
return character
import logging
import os
from logging.handlers import RotatingFileHandler
def get_logger():
"""
Creates a rotating log
"""
__logger__ = logging.getLogger('')
__logger__.setLevel("ERROR")
log_formatter = '%(asctime)s - %(levelname)-6s - [%(threadName)5s:%(funcName)5s():%(lineno)s] - %(message)s'
time_format = "%Y-%m-%d %H:%M:%S"
formatter = logging.Formatter(log_formatter, time_format)
log_files = os.path.join("temp/ERROR.log")
temp_handler = RotatingFileHandler(log_files, maxBytes=100000000, backupCount=5)
temp_handler.setFormatter(formatter)
__logger__.addHandler(temp_handler)
return __logger__
logger = get_logger()
# importing libraries
from fastapi import APIRouter
from scripts.constants.endpoints import EndPoints
from scripts.constants.models import PrototypeCharacter
from scripts.core.handlers.api_functions import create_new_character
from scripts.logging.logger import logger
# Create FastAPI instance
prototype = APIRouter()
# Root
@prototype.get(EndPoints.root)
async def root():
return {"Message": "It's Working!"}
# Create Characters
@prototype.post(EndPoints.create_characters)
def create_characters(prototypes: PrototypeCharacter):
try:
print(prototypes)
base_character, new_character = create_new_character(prototypes)
return {"Base Character": base_character, "New Character": new_character}
except Exception as e:
logger.error(e)
2023-02-20 12:08:36 - ERROR - [AnyIO worker thread:create_characters():32] - __init__() missing 1 required positional argument: 'base_characters'
2023-02-20 12:23:36 - ERROR - [AnyIO worker thread:__init__():14] - __init__() missing 5 required positional arguments: 'name', 'health', 'speed', 'damage', and 'skills'
2023-02-20 12:32:35 - ERROR - [AnyIO worker thread:__init__():14] - __init__() missing 5 required positional arguments: 'name', 'health', 'speed', 'damage', and 'skills'
2023-02-20 12:33:45 - ERROR - [AnyIO worker thread:__init__():14] - __init__() missing 5 required positional arguments: 'name', 'health', 'speed', 'damage', and 'skills'
2023-02-20 12:33:53 - ERROR - [AnyIO worker thread:__init__():14] - __init__() missing 5 required positional arguments: 'name', 'health', 'speed', 'damage', and 'skills'
2023-02-20 12:36:33 - ERROR - [AnyIO worker thread:__init__():14] - __init__() missing 5 required positional arguments: 'name', 'health', 'speed', 'damage', and 'skills'
2023-02-20 12:36:42 - ERROR - [AnyIO worker thread:__init__():14] - __init__() missing 5 required positional arguments: 'name', 'health', 'speed', 'damage', and 'skills'
2023-02-20 12:37:48 - ERROR - [AnyIO worker thread:__init__():14] - __init__() missing 5 required positional arguments: 'name', 'health', 'speed', 'damage', and 'skills'
2023-02-20 12:37:56 - ERROR - [AnyIO worker thread:__init__():14] - __init__() missing 5 required positional arguments: 'name', 'health', 'speed', 'damage', and 'skills'
2023-02-20 12:46:08 - ERROR - [AnyIO worker thread:__init__():13] - __init__() missing 5 required positional arguments: 'name', 'health', 'speed', 'damage', and 'skills'
2023-02-20 12:46:08 - ERROR - [AnyIO worker thread:create_base_character():28] - 'Prototyping' object has no attribute 'prototypes'
2023-02-20 12:57:50 - ERROR - [AnyIO worker thread:__init__():13] - __init__() missing 5 required positional arguments: 'name', 'health', 'speed', 'damage', and 'skills'
2023-02-20 12:57:50 - ERROR - [AnyIO worker thread:create_base_character():28] - 'Prototyping' object has no attribute 'prototypes'
2023-02-20 14:26:10 - ERROR - [AnyIO worker thread:create_characters():25] - 'CharacterPrototype' object has no attribute 'speed'
2023-02-20 14:28:58 - ERROR - [AnyIO worker thread:create_characters():25] - 'CharacterPrototype' object has no attribute 'speed'
2023-02-20 14:29:29 - ERROR - [AnyIO worker thread:create_characters():25] - 'CharacterPrototype' object has no attribute 'speed'
2023-02-20 14:30:50 - ERROR - [AnyIO worker thread:create_new_charcter():64] - 'CharacterPrototype' object has no attribute 'speed'
2023-02-20 14:34:05 - ERROR - [AnyIO worker thread:create_new_charcter():19] - 'CharacterPrototype' object has no attribute 'speed'
2023-02-20 14:34:47 - ERROR - [AnyIO worker thread:create_new_charcter():20] - 'CharacterPrototype' object has no attribute 'speed'
2023-02-20 14:35:19 - ERROR - [AnyIO worker thread:create_new_charcter():20] - 'CharacterPrototype' object has no attribute 'speed'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment