def build(self) -> Function: function = Function(Id=self._id, Name=self._name) function.save() for property in self._properties: property.save() function.Properties.add(property) return function
def handle(self, *args, **options): source = options['source'] if not source or not os.path.exists(os.path.expanduser(source)): raise Exception('{0} is not a valid CSV path'.format(source)) function = None duplicates = list() functions = set() with open(source) as file_: reader = csv.reader(file_) for row in reader: if 'Function' in row[0]: name = row[1] file = row[2] sloc = int(row[3]) function = Function.objects.filter( name=name, file=file ) if not function.exists(): function = Function() function.name = name function.file = file function.sloc = sloc if function not in functions: functions.add(function) else: duplicates.append(function) function = [ f for f in functions if f == function ][0] duplicates.append(function) functions.remove(function) if len(functions) > 0: logger.debug('Adding {0} functions.'.format(len(functions))) Function.objects.bulk_create(functions) if len(duplicates) > 0: for function in duplicates: logger.debug( 'Duplicate {0} in {1} with {2} SLOC'.format( function.name, function.file, function.sloc ) ) logger.info('Appended {0} functions.'.format(len(functions)))
def handle(self, *args, **options): source = options['source'] if not source or not os.path.exists(os.path.expanduser(source)): raise Exception('{0} is not a valid CSV path'.format(source)) functions = set() files = set() with open(source) as file_: reader = csv.reader(file_) for row in reader: if 'Function' in row[0]: name = row[1] file = row[2] sloc = int(row[3]) function = Function() function.name = name function.file = file function.sloc = sloc if function not in functions: functions.add(function) else: functions = self._update(function, functions) elif 'File' in row[0]: name = row[2] sloc = int(row[3]) file_ = File() file_.name = name file_.sloc = sloc if file_ not in files: files.add(file_) else: files = self._update(file_, files) if len(functions) > 0: logger.debug('Adding {0} functions.'.format(len(functions))) Function.objects.bulk_create(functions) logger.info('Loaded {0} functions.'.format(len(functions))) if len(files) > 0: logger.debug('Adding {0} files.'.format(len(files))) File.objects.bulk_create(files) logger.info('Loaded {0} files.'.format(len(files)))
def _createGroupDeviceFunctions(self, properties): categories = set(p.Category for p in properties.all()) functions = [] for category in categories: categoryProperties = properties.filter(Category=category) categoryGroupProperties = self._createGroupDeviceProperties( categoryProperties) function = Function() function.Name = category.Name function = self.__functionRepository.Save(function) for categoryGroupProperty in categoryGroupProperties: categoryGroupProperty = self.__propertyRepository.Save( categoryGroupProperty) function.Properties.add(categoryGroupProperty) self.__functionRepository.Save(function) functions.append(function) return functions
def handle(self, *args, **options): primary = options['primary'] secondary = options['secondary'] if not primary or not os.path.exists(os.path.expanduser(primary)): raise Exception('{0} is not a valid CSV path'.format(primary)) if not secondary or not os.path.exists(os.path.expanduser(secondary)): raise Exception('{0} is not a valid CSV path'.format(secondary)) primary_dataset = dict() secondary_dataset = dict() function = None with open(primary) as file_: reader = csv.reader(file_) for row in reader: if 'Function' in row[0]: function = Function() function.name = row[1] function.file = row[2] if function not in primary_dataset: primary_dataset[function] = int(row[3]) else: logger.debug( '{0} duplicate in {1}'.format(function, primary) ) with open(secondary) as file_: reader = csv.reader(file_) for row in reader: if 'Function' in row[0]: function = Function() function.name = row[1] function.file = row[2] if function not in secondary_dataset: secondary_dataset[function] = int(row[3]) else: logger.debug( '{0} duplicate in {1}'.format(function, secondary) ) match = 0 for item in secondary_dataset: if item in primary_dataset: if secondary_dataset[item] == primary_dataset[item]: match += 1 logger.info( '{0}/{1} having matching SLOC'.format( match, len(secondary_dataset) ) )
def functions_log(): form = FunctionForm() if form.validate_on_submit(): try: motors = form.motor.data motor = Motors.query.filter_by(serial_number=motors).first() function = Function(number=form.number.data, name=form.name.data, rating=form.rating.data, motor=motor, location=form.location.data, starter=form.starter.data) db.session.add(function) db.session.commit() flash(f'Motor usage has been created in the database', 'Success') return redirect(url_for('functions_log')) except : flash(f'There was an Error, Please try again', 'Warning') return redirect(url_for('functions_log')) return render_template('functions.html', title='Function', form=form)
def Save(self, data): if type(data) is Function: model = data else: model = Function() model.Id = data.get("Id", "") model.Name = data.get("Name", "") function = self.Get(model.Id) status = self.Status(model, function) if status is ModelStatus.New: model.Id = None model.save() elif status is ModelStatus.Modified: model.save() if type(data) is not Function: Properties = data['Properties'] for propertyDict in Properties: property = self.__propertyRepo.Save(propertyDict) model.Properties.add(property.Id) return model
"""Initialize db. """ from datetime import datetime import sys sys.path.insert(0, '../') from app.models import Function from app.models import db # read json data into db import json from datetime import datetime import os infile = os.path.join(os.path.dirname(__file__), 'app.json') data = json.load(open(infile, 'r')) for rec in data: kws = {} for k, v in rec.items(): if k == 'timestamp': kws[k] = datetime.strptime(v, '%Y-%m-%d %H:%M:%S.%f') elif k != 'id': kws[k] = v f = Function(**kws) db.session.add(f) db.session.commit()
from sr import app from app import db from app.models import User, Store, Ecommerce, Function with app.app_context(): Ecommerce.insert_all() Function.insert_all()