is it possible to use a piece of code in a GAMS? - gams-math

It is necessary to use the following piece of code in a GAMS code repeatedly (I copied this piece of code approximately 10 times or more, in different lines of the program),And has made the program very unreadable.
loop(k,s,
if( AG(s,k)=TG(s),
TG(s)= (TGT(s,k)+2*TGT(s,k-1)+3*TGT(s,k-2))/3 ; );
);
Is there a command or a way to use this piece of code more easily?
Many Thanks!

Set up the repeated code with $onecho $offecho:
$onecho > %gams.scrdir%myfile.rsp
loop(k,
loop(s$flag,
if( AG(s,k)=TG(s),
TG(s)= (TGT(s,k)+2*TGT(s,k-1)+3*TGT(s,k-2))/3 ;
flag=0; );
);
);
$offecho
The %gams.scrdir% just saves the file to the scratch directory. (It is then deleted after the run and avoids cluttering up your main directories. You could change this to any path you want.)
To use it in your code:
$include %gams.scrdir%myfile.rsp
Note that $onecho only stores the code in a file; GAMS won’t use the code until you call it with $include.

Related

Variable does not save event.response from network.request -Lua

I am trying to create a Lua program using Sublime and Corona. I want to fetch a webpage, use a pattern to extract certain text from the page, and then save the extracted text into a table. I am using the network.request method provided by Corona
The problem: The extracted text is not saving into the global variable I created. Whenever I try to reference it or print it outside of the function it returns nil. Any ideas why this is happening?
I have attached a screen shot of my event.response output. This is what I want to be saved into my Lua table
Event.response Output
Here is my code:
local restaurants = {}
yelpString = ""
--this method tells the program what to do once the website is retrieved
local function networkListener( event )
if ( event.isError ) then
print( "Network error: ", event.response )
else
yelpString = event.response
--loops through the website to find the pattern that extracts
restaurant names and prints it out
for i in string.gmatch(yelpString, "<span >(.-)<") do
table.insert(restaurants, i)
print(i)
end
end
end
-- retrieves the website
network.request( "https://www.yelp.com/search?
cflt=restaurants&find_loc=Cleveland%2C+OH%2C+US", "GET", networkListener )
This sounds like a scoping problem. From the output you give, it looks like networkListener is being called, and you are successfully adding the text into the restaurants table. Moreover, since you define restaurants as a table, it should be a table when you reference it, not nil. So by deduction, the issue must be that you are trying to access the restaurants table from somewhere where it is not in scope.
If you declare restaurants as "local" in the top level of a file (i.e. not within a function or a block), it will be accessible to the whole file, but it won't be accessible to anything outside the file. So the table.insert(restaurants, i) in your code will work, but if you try to reference restaurants from somewhere outside the file, it will be nil. I'm guessing this is the cause of the problems that you are running into.
For more details on scope, have a look at the Programming in Lua book. The book is for Lua 5.0, but the scoping rules for local variables have not changed in later versions of Lua (as of this writing, the latest is Lua 5.3).

Accessing custom jbake confing properties in asciidoc

After some time I spent staring at the jbake code, I figured out that if I declare my own property in jbake.properties :
...
foo=bar
...
I can reuse that in files that go through a template engine by referencing it as ${config.foo}. I'd like to have this substitution working also on the content lvl, i.e. for files written in asciidoc, living inside the content directory.
Is there any non-trivial way to achieve it? How can I make the templating engine to proccess the result of asciidoc parses engine, or make it running it before the asciidoctor?
I found the answer myself.
To use the property substitution in asciidoc files, add following to the jbake.properties:
...
asciidoctor.attributes.export=true
foo=world
...
and reference the variable in aFile.adoc this way:
Hello {foo}!

PleaseWaitButton in perl? loading gif etc. during long sql query

I have a web app that is all run via one perl file that works with a database. At one point the user can execute an action that takes a lot of time (it adds a bunch of rows from one table to another). Is there a way I can have a wait .gif or message show while the sql is executing, and then have it disappear once it's over? I'm pretty new to perl, saw that this was possible through Javascript and the PleaseWaitButton though. Any help would be much appreciate though. My code for the lengthy update part is below, so I image whatever thing would need to be inserted somewhere in there:
if((#inTable[0])==0){
my $update = `perl /stockhistory.pl
my #updatearray = split(" ", $update);
my $val;
for(my $i = 0; $i < scalar #updatearray; $i+=6){
$val = eval{ExecSQL($dbuser, $dbpasswd, "insert into PORT_ModernData (SYMBOL, TIME, OPEN, HIGH, LOW, CLOSE, VOLUME) VALUES (?,?,?,?,?,?,?)",undef, $stocksy,, $updatearray[$i], $updatearray[$i+1], $updatearray[$i+2], $updatearray[$i+3], $updatearray[$i+4], $updatearray[$i+5]);};
}
}
Thanks for any and all help!
The way I understand your problem, I would suggest Javascript, though I do like avoiding it myself.
This way, the user can click the button, triggering the sql get, and the message comes up, while the server does what it does, when done, Javascript can tell the user so and provide the link, data or whatever the result is.
With perl, this would involve a more complicated procedure including further server/client communication.
I would gladly be corrected, though, if anyone knows better.

Maximo - Adding elements to a CustomMboSet using scripting

Is it possible to add to a CustomMboSet in Maximo using scripting? I am writing a custom application using a custom object called TIMESHEET. As part of the application I am writing a (Jython) script that needs to dynamically build up an MboSet (a set of TIMESHEETs). The code retrieves an existing CustomMboSet and attempts to add elements to it. It works when using an out of box MboSet, but when I try to run the same code on a custom MboSet it does not seem to work. No error is thrown, but code below the offending line is not run.
In other words, this works (LABTRANS is an out of box MBO):
myMboSet = mbo.getMboSet("LABTRANS")
newMbo = myMboSet.add()
# Set attributes on newMbo, everything is happy
But this does not (TIMESHEET is a custom MBO):
myMboSet = mbo.getMboSet("TIMESHEET")
newMbo = myMboSet.add()
# Code does not execute after the above line
Anyone have any insight as to why I am seeing this behavior? Does the Maximo scripting framework simply not support the dynamic building up of CustomMboSets? Any help is appreciated. Thanks.
You need to make sure that the relationship exists between the Current MBO and the Custom MBO in the database configuration otherwise it will not work.
Alternatively you can use the following code to create an new mboSet on the fly:
timeSheetMboSet = mxServer.getMboSet("TIMESHEET", userInfo)
mbo.getMboSet(RELATIONSHIPNAME).
LABTRANS and TIMESHEET must be the relationship names to the object in auto script.
If you want to get/add records in any object, use
mxServer.getMboSet(OBJECTNAME, userInfo)
A bit more explanation. You can create your own custom relationship from within your automation script. The trick is to make sure it's not already existing. That's why I use a dollar sign for mine.
variable = mbo.getMboSet(tempRelationshipName,Object,where clause)
previousPhaseSet = mbo.getMboSet("$wophasetranstemp1", "exitdate is null")

phpexcel pdf rendering library has not been defined

After trying and failing to generate PDFs with PHPExcel 1.7.6 (out of memory errors), I upgraded to 1.7.8. I can't for the life of me figure out how to get it working. I've tried tcPDF and mPDF, and it's the same for both.
Putting it back to Excel output, I can see I'm setting the path correctly. All I can get is "PDF Rendering library has not been defined", and I can't figure out what it wants - I've tried 'mPDF5.4', 'MPDF54' (the actual name of the folder itself), 'mpdf', 'mpdf.php'...same each time.
I've been using PHPExcel for over a year, so I'm not entirely new to it. I've lost way more time than I care to admit on this problem, and I haven't found this problem described anywhere, so I'm feeling more than a little stupid that I appear to be the only one that can't figure this out.
The actual code I'm using is the following:
ini_set('include_path', ini_get('include_path').'\\Classes\\');
$rendererName = PHPExcel_Settings::PDF_RENDERER_MPDF;
$rendererLibrary = 'mPDF5.4';
$rendererLibraryPath = ini_get('include_path') . $rendererLibrary;
(That is, pretty well a copy of the example code.)
In the interest of completeness, the headers I'm using are
echo header("Content-Type: application/pdf");
echo header("Content-Disposition: attachment; filename=".$filename.".pdf" );
echo header('Cache-Control: max-age=0');
These near the top of the file, naturally.
Near the end of the file, the output code is
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objWriter->save('php://output');
I got it working. Much as I'd like to say I had a breakthrough moment and understand it perfectly, I have no idea how I got it to work. However, in hopes that it might help someone, let me lay out what I did.
I'm running XAMPP on Windows. My file structure has the folder for PHPExcel itself in xampp\php\PEAR\Classes. domPDF is in the same folder, and I renamed it 'dompdf'.
For reasons I no longer recall, I set the include path like so:
ini_set('include_path', ini_get('include_path').'\\Classes\\');
To set the rendering path, I used the following:
$rendererName = PHPExcel_Settings::PDF_RENDERER_DOMPDF;
$rendererLibrary = 'dompdf';
$rendererLibraryPath = ini_get('include_path') . $rendererLibrary;
For the actual writer creation, I'm using the following:
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objWriter->save($path.$fullFileName);
One thing I noticed that may have made things different was doing this:
// include 'PHPExcel/Writer/Excel2007.php';
That is, unlike everything I've done in PHPExcel, I'm not including anything from the Writer folder at all. Best I can remember, that's all that's different this time versus a week ago when I asked the question. Once I'd grabbed the 01simple-download-pdf.php file from the Tests folder in 1.7.8, it was mostly a matter of copying the code from it and tweaking it to my paths.
To summarize, leave $rendererName alone. The $rendererLibrary is the name of the folder that contains the library, 'dompdf' in my case. The $rendererLibraryPath is literally setting the path to that folder, so it ends with the path that contains the pdf library folder.
It should be obvious that I'm no uber-leet hax0r, but SO has answered many, many programming questions for me. I'm hoping this helps someone else, so they're not wasting hours like I did.
The PHPOffice contains also PHPWord. I have had the same error message with PHPWord. This is for LINUX. A replacement of 'PhpWord' by 'PhpExcel' should do the job for this case. You must modify the path $rendererLibraryPath to your needs.
$rendererName = \PhpOffice\PhpWord\Settings::PDF_RENDERER_DOMPDF;
$rendererLibraryPath = realpath(__DIR__ . '/../../../../../dompdf-0.6.1');
\PhpOffice\PhpWord\Settings::setPdfRenderer($rendererName, $rendererLibraryPath);
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'PDF');
$objWriter->save('helloWorld.pdf');
If you are getting this error and you have set the correct path to the TCPDF or DOMPDF folder (you do not have to write the full path), then also make sure you have these lines:
if (!PHPExcel_Settings::setPdfRenderer(
$rendererName,
$rendererLibraryPath
)) {
die(
'NOTICE: Please set the $rendererName and $rendererLibraryPath values' .
EOL .
'at the top of this script as appropriate for your directory structure'
);
}