Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import *
- from DataBaseConnection import DataBaseConnection
- import json
- import os, sys
- class VariationAlgorithm:
- def __init__(self, nameOfTask, idPattern):
- directoryPath = r'/home/luka/Desktop/Tasks'
- self.nameOfTask = nameOfTask
- self.idPattern = idPattern
- self.DirectoryPath = directoryPath + '/' + self.nameOfTask
- self.DictionariesPath = self.DirectoryPath + '/Dictionaries'
- self.PatternPath = self.DirectoryPath + '/Pattern'
- self.GenerationPath = self.DirectoryPath + '/Generation'
- def prepareDirectory(self): ##Called first to setup enviroment for generating new generations
- if not os.path.exists(self.DirectoryPath):
- os.makedirs(self.DirectoryPath)
- os.makedirs(self.DictionariesPath)
- os.makedirs(self.PatternPath)
- os.makedirs(self.GenerationPath)
- else:
- print("Directory already exists!")
- newCountFilePath = self.DirectoryPath + '/CountOfTasks.txt' ##count of generations
- newAnswerCountPath = self.DirectoryPath + '/AnswerCount.txt'##count of answers (1,4)
- writeCount = open(newCountFilePath, 'w+')
- writeCount.write('0')
- writeCount.close()
- writeAnswerCount = open(newAnswerCountPath, 'w+')
- writeAnswerCount.write('')
- writeAnswerCount.close()
- def createNewDictionary(self, variables): ##Creating new dict (pairs of generated variables and math variables) - .json
- orderNumber = self.orderNumber()
- DictionaryPath = self.DictionariesPath + '/Dictionary' + str(orderNumber) + '.json'
- with open(DictionaryPath, 'w') as writeFile:
- json.dump(variables, writeFile)
- countPath = self.DirectoryPath + '/CountOfTasks.txt'
- countWrite = open(countPath, 'w')
- countWrite.write(str(orderNumber + 1))
- countWrite.close()
- def checkDictionary(self, variables): ##Checking if same generation exists
- countOfTasks = self.orderNumber()
- for dictNumber in range(countOfTasks):
- dictPath = self.DictionariesPath + '/Dictionary' + str(dictNumber) + '.json'
- with open(dictPath) as readFile:
- dictionary = json.load(readFile)
- intersection = set(variables.items() & dictionary.items())
- return not(len(intersection) == len(variables))
- def matchLatexPattern(self, variables, typeOfMatching): ##Matching latex pattern with generated variables
- latexPath = self.PatternPath + '/LatexPattern' + typeOfMatching + '.txt'
- latexPattern = open(latexPath,'r+')
- rows = latexPattern.readlines()
- writeRow = ""
- for row in rows:
- if row[0] == '/':
- writeRow += '<p>' + row.rstrip() + '<p><br>' + '\n'
- continue
- temporaryRow=""
- splitedRow = row.split(" ")
- for i in range(len(splitedRow)):
- if splitedRow[i] in variables:
- temporaryRow += str(variables[splitedRow[i]])
- else:
- temporaryRow += splitedRow[i]
- writeRow += '<p>' + temporaryRow.rstrip() + '<p><br>' + '\n'
- taskText = self.filterVariation(writeRow)
- self.saveVariation(taskText, typeOfMatching)
- latexPattern.close()
- def filterVariation(self, stringTask): ## Filtering needless expressions
- tempString = ""
- for i in range(len(stringTask)):
- if stringTask[i] == '1' and stringTask[i+1] == 'x':
- continue
- if stringTask[i] == '/':
- continue
- else:
- tempString += stringTask[i]
- return tempString
- def orderNumber(self): ##Returning number of generated tasks
- countPath = self.DirectoryPath + '/CountOfTasks.txt'
- countRead = open(countPath, 'r')
- orderNumber = int(countRead.read())
- countRead.close()
- return orderNumber
- def saveVariation(self, variation, typeOfSaving): ##Saving new generation - .txt
- orderNumber = self.orderNumber()
- newVariationDirectoryPath = self.GenerationPath + '/' + 'Variation' + str(orderNumber)
- os.makedir(newVariationDirectoryPath)
- newVariationPath = self.newVariationDirectoryPath + '/' + typeOfSaving + '.txt'
- writeVariation = open(newVariationPath, 'w')
- writeVariation.write(variation)
- writeVariation.close()
- def divide(self, dividend, divisor): ##Preventing dividing with zero
- return dividend == 0 or divisor == 0
- def deleteVariations(self):
- connection = DataBaseConnection()
- #removing variations and answers from DB
- cursorDelete = connection.getCursor()
- queryDelete = "DELETE FROM variation WHERE id_pattern_fk = %s"
- cursorDelete.execute(queryDelete, (self.idPattern))
- cursorDelete.close()
- cursorDeleteAnswers = conection.getCursor()
- queryDeleteAnswers = "DELETE FROM variation_answer WHERE id_pattern_fk = %s"
- cursorDeleteAnswers.execute(queryDeleteAnswers, (self.idPattern))
- cursorDelete.close()
- ##removing Variation
- os.remove(self.GenerationPath)
- os.remove(self.DictionariesPath)
- os.remove(self.PatternPath)
- ##reseting countOfTasks to 0
- countPath = self.DirectoryPath + '/CountOfTasks.txt'
- countReset = open(countPath, 'w')
- countReset.write('0')
- countReset.close()
- def commitVariations(self):
- connection = DataBaseConnection()
- cursorSetImplemented = connection.getCursor()
- querySetImplemented = "UPDATE pattern SET is_implemented_pattern = %s WHERE id_pattern = %s"
- cursorSetImplemented.execute(querySetImplemented, (1, self.idPattern))
- numberOfVariations = self.orderNumber()
- for i in range(numberOfVariations):
- stepByStepPath = self.GenerationPath + '/' + 'Variation' + str(i) + '/' + 'StepByStep.txt'
- taskPath = self.GenerationPath + '/' + 'Variation' + str(i) + '/' + 'Task.txt'
- stepByStepRead = open(stepByStepPath, 'r')
- stepByStepVariation = stepByStepRead.read()
- stepByStepRead.close()
- taskRead = open(taskPath, 'r')
- taskVariation = taskRead.read()
- taskRead.close()
- commitCursor = connection.getCursor()
- queryCommit = "INSERT INTO variation (id_pattern_fk, task_variation, stepbystep_variation) VALUES(%s, %s, %s)"
- commitCursor.execute(queryCommit, (self.idPattern, taskVariation, stepByStepVariation))
- commitCursor.close()
- ##dodaj za rjesenja, sub/super, weight
- def pullPattern(self): ##getting pattern's info
- connection = DataBaseConnection()
- cursorPull = connection.getDictCursor()
- query = "SELECT * FROM pattern WHERE id_pattern = %s"
- cursorPull.execute(query, (self.idPattern))
- result = cursorPull.fetchone()
- stepByStepPattern = result['stepbystep_pattern']
- taskPattern = result['task_pattern']
- variablesAndConditions = result['variables_and_conditions_pattern']
- subCategory = result['subcategory_pattern']
- superCategory = result['supercategory_pattern']
- weight = result['weight_pattern']
- remark = reuslt['remark_pattern']
- newStepByStepPatternPath = self.PatternPath + '/LatexPatternStepByStep.txt'
- newTaskPatternPath = self.PatternPath + '/LatexPatternTask.txt'
- newVariablesAndConditionsPath = self.PatternPath + '/VariablesAndConditions.txt'
- newSubCategoryPatternPath = self.PatternPath + '/SubCategory.txt'
- newSuperCategoryPatternPath = self.PatternPath + '/SuperCategory.txt'
- newWeightPatternPath = self.PatternPath + '/Weight.txt'
- newRemarkPatternPath = self.PatternPath + '/Remark.txt'
- writeStepByStepPattern = open(newStepByStepPatternPath, 'w+')
- writeStepByStepPattern.write(stepByStepPattern)
- writeStepByStepPattern.close()
- writeTaskPattern = open(newTaskPatternPath, 'w+')
- writeTaskPattern.write(taskPattern)
- writeTaskPattern.close()
- writeVariablesAndConditions = open(newVariablesAndConditionsPatternPath, 'w+')
- writeVariablesAndConditions.write(variablesAndConditions)
- writeVariablesAndConditions.close()
- writeSubCategory = open(newSubCategorypatternPath, 'w+')
- writeSubCategory.write(subCategory)
- writeSubCategory.close()
- writeSuperCategory = open(newSubCategoryPatternPath, 'w+')
- writeSubCategory.write(subCategory)
- writeSubCategory.close()
- writeWeight = open(newWeightPatternPath, 'w+')
- writeWeight.write(weight)
- writeWeight.close()
- writeRemark = open(newRemarkPatternPath, 'w+')
- writeRemark.write(remark)
- writeRemar.close()
- cursorGetCountOfAnswers = connection.getDictCursor()
- queryGetCountOfAnswers = "SELECT COUNT(*) as count FROM pattern_answer WHERE id_pattern_fk = %id"
- cursorGetCountOfAnswers.execute(queryGetCountOfAnswers,(self.idPattern))
- resultCountOfAnswers = cursorGetCountOfAnswers.fetch()
- answerCount = resultCountOfAnswers['count']
- answerCountPath = self.DirectoryPath + '/AnswerCount.txt'
- writeAnswerCount = open(nswerCountPath, 'w')
- writeAnswerCount.write(answerCount)
- writeAnswerCount.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement