While trying to import a dat file and create instance in Pyomo, it says "ValueError" as shown the picture below - pyomo

[Error in ][1][instance = model.create_instance('input/pownet_data_camb_2022.dat')][2]
I need to import a dat file and create instance. But, it runs to an error saysing "ValueError: Error retrieving immutable Param value (SimHours):
The Param value is undefined and no default value is specified."
model.SimHours = Param(within=PositiveIntegers)

Based on the error message, I would guess SimHours is not given a value in the data file.

Related

How to pprint to file parameters of an Instance from an AbstractModel in Pyomo?

I´m trying to inspect an Instance of an AbstractModel on Pyomo for checking if parameters reading was OK.
For doing so, I´d like to print the parameters values considered in the instance into a txt file.
for doing so, I´ve tried:
for element in instance.component_objects(Param,descend_into=True):
element.pprint(filename=some_filename)
But what I get is that filename is not a valid argument for pprint. Any hints on what could I do?
pprint expects an output stream to write to. Try:
with open('file.txt', 'w') as output_file:
element.pprint(output_file)

Django Datetime not getting saved

I am trying to create an object of the following model and save it through using code as below.
chk_list_for_batch = ChkListForBatch(batch, chk_point, False, datetime.datetime.now())
chk_list_for_batch.save()
But, I get the following error
django.core.exceptions.ValidationError: ["'2019-05-25 11:20:23.240094'
value must be either True or False."]
I searched but couldn't find any direction. Kindly suggest.
You can create an object and save it to the database like this:
from django.utils import timezone
chk_list_for_batch = ChkListForBatch.objects.create(batch='batch',
chk_point='chk_point', some_field=False, creation_time=timezone.now())
chk_list_for_batch.save()
The docs explain how to create objects in more detail.

Python 'rawpy._rawpy.RawPy' object has no attribute 'imread' after second pass

I try to process a series of DNG raw picture files and it all works well for the first pass (first fils). When I try to read the second DNG file during the second pass through the for-next loop, I receive the error message 'rawpy._rawpy.RawPy' object has no attribute 'imread' when executing the line "with raw.imread(file) as raw:".
import numpy as np
import rawpy as raw
import pyexiv2
from scipy import stats
for file in list:
metadata = pyexiv2.ImageMetadata(file)
metadata.read()
with raw.imread(file) as raw:
rgb16 = raw.postprocess(gamma=(1,1), no_auto_bright=True, output_bps=16)
avgR=stats.describe(np.ravel(rgb16[:,:,0]))[2]
avgG=stats.describe(np.ravel(rgb16[:,:,1]))[2]
avgB=stats.describe(np.ravel(rgb16[:,:,2]))[2]
print i,file,'T=', metadata['Exif.PentaxDng.Temperature'].raw_value,'C',avgR,avgG,avgB
i+=1
I tried already to close the raw object but from googling I understand that is not necessary when a context manager is used.
Help or suggestions are very welcome.
Thanks in advance.
You're overwriting your alias of the rawpy module (raw) with the image you're reading. That means you'll get an error on the second pass through the loop.
import rawpy as raw # here's the first thing named "raw"
#...
for file in list:
#...
with raw.imread(file) as raw: # here's the second
#...
Pick a different name for one of the variables and your code should work.

Returning a more verbose error in production environment (DEBUG=False)

I'm using tastypie, suppose that I have a model named M and a field in it - F - is unique CharField. Suppose that there's already an instance of M that its F value is "test", if I try to create another instance of M that has the same value for F or if I try to update an already created instance of M and change its F value to "test", tastypie returns an error and tells that duplicate key value violates unique constraint "M_F_key"\nDETAIL: Key (F)=(test) already exists.\n but if I set DEBUG=False in settings it doesn't return that error an instead it returns Sorry, this request could not be processed. Please try again later. this way my client can't understand that the problem was a duplicate value for F field and can't show the user appropriate message. How can I solve this?
Well, it can be solved by using a django middleware that implements process_exception

how to assign a value to global variable in runtime in python

While running a code in python I want to update a dictionary value that is in config(global variable) I'm trying to do but i got the following error.
config.py
survey={'url':''}
runnigcode.py
url=config.__getattribute__(config.environment)
url['url']='myurl.com'
TypeError: "'module' object does not support item assignment"
in your runningcode.py, just import the variable as a method:
from config import survey
print survey
if you want to modify the value in the config.py, just use write method, like in this post: Edit configuration file through python