def create_ds(owner, data): ''' ' Creates a new Datastream given a key and some data. The keys owner is used as the Datastream's owner. ' The data needs to contain the name for the Datastream as a minimum in order to create it. ' ' Keyword Arguments: ' key - Valid key object. ' data - Dictionary of key value pairs for the data to create the Datastream with ' ' Returns: New Datastream object or dictionary containing any exceptions and errors that occured ''' timingMark = time.time() ds = DataStream(owner=owner) for attr, val in data.iteritems(): if attr == "scaling_function": try: sc = ScalingFunction.objects.get(name__iexact=val) except ScalingFunction.DoesNotExist as e: error = "ScalingFunction with the name '%s' does not exist." % str(val) return {"error": error, "exception": str(e)} setattr(ds, attr, sc) else: try: setattr(ds, attr, val) except Exception as e: error = "There was a problem setting '%s' with the value '%s'." % (str(attr), str(val)) return {"error": error, "exception": str(e)} print 'Assign attr time: %f' % (time.time() - timingMark) timingMark = time.time() try: ds.full_clean() ds.save() except ValidationError as e: unique_error = u'Data stream with this Owner and Name already exists.' if '__all__' in e.message_dict and unique_error in e.message_dict['__all__']: raise e error = "There were one or more problems setting DataStream attributes." return {"error": error, "exception": str(e)} print 'Clean and save time: %f' % (time.time() - timingMark) print return ds
try: scaleF = ScalingFunction.objects.get(name="Identity") print "Scaling Function good." except ObjectDoesNotExist: print "I am creating a scaling function" scaleF = ScalingFunction(name="Identity", definition="return x;") scaleF.save() try: ds1 = DataStream.objects.get(id=1) except ObjectDoesNotExist: ds1 = DataStream( id=1, name="Test data 1", description="Simple Sign curve", color="purple", min_value=-50.0, max_value=50.0, scaling_function=scaleF, owner=AuthUser.objects.get(username="******"), ) ds1.save() try: ds2 = DataStream.objects.get(id=2) except ObjectDoesNotExist: ds2 = DataStream( id=2, name="Test data 2", description="Amplitude modulated sin curve.", color="red ", min_value=-50.0,