QuercusCompiledScript.eval output not working - quercus

I cannot figure out what the problem is with the QuercusCompiledScript.eval. Running code:
QuercusScriptEngine quercusScriptEngine = new QuercusScriptEngine();
quercusScriptEngine.eval("<?php echo 'hello uncompiled!\n'; ?>");
CompiledScript script = quercusScriptEngine.compile("<?php echo 'hello compiled!\n'; ?>");
script.eval();
System.out.println("that's all");
produces:
hello uncompiled!
that's all
Debugging this stuff I could not figure out what's wrong, as it does execute the statement, buffers are OK, but the output itself is not performed.
What is wrong?

I found the cause of the problem. The QuercusScriptEngine.eval() explicitly does writer.flush() in the end referring to the http://bugs.caucho.com/view.php?id=1914. But the QuercusCompiledScript.eval() does not, at least in the quercus-4.0.39 (and in the quercus-4.0.45 as well). The workaround is to flush explicitly providing a Writer:
CompiledScript script = quercusScriptEngine.compile("<?php echo 'hello compiled!\n'; ?>");
ScriptContext ctx = quercusScriptEngine.getContext();
Writer writer = new OutputStreamWriter(System.out);
ctx.setWriter(writer);
script.eval(ctx);
writer.flush();

Related

Array.include? myVariable not working as expected

I am coding a Ruby 1.9 script and I'm running into some issues using the .include? method with an array.
This is my whole code block:
planTypes = ['C','R','S'];
invalidPlan = true;
myPlan = '';
while invalidPlan do
print "Enter the plan type (C-Commercial, R-Residential, S-Student): ";
myPlan = gets().upcase;
if planTypes.include? myPlan
invalidPlan = false;
end
end
For troubleshooting purposes I added print statements:
while invalidPlan do
print "Enter the plan type (C-Commercial, R-Residential, S-Student): ";
myPlan = gets().upcase;
puts myPlan; # What is my input value? S
puts planTypes.include? myPlan # What is the boolean return? False
puts planTypes.include? "S" # What happens when hard coded? True
if planTypes.include? myPlan
puts "My plan is found!"; # Do I make it inside the if clause? Nope
invalidPlan = false;
end
end
Since I was getting the correct result with a hard-coded string, I tried "#{myPlan}" and myPlan.to_s. However I still get a false result.
I'm new to Ruby scripting, so I'm guessing I'm missing something obvious, but after reviewing similar question here and here, as well as checking the Ruby Doc, I'm at a loss as to way it's not acting correctly.
The result of gets includes a newline (\n), which you can see if you print myPlan.inspect:
Enter the plan type (C-Commercial, R-Residential, S-Student): C
"C\n"
Add strip to clean out the unwanted whitespace:
myPlan = gets().upcase.strip;
Enter the plan type (C-Commercial, R-Residential, S-Student): C
"C"

Python Simple PiggyBank Program

This is my Python Program that I have been having some issues with:
-- coding: cp1252 --
from time import gmtime, strftime
print("Welcome to the PiggyBank version 1.")
num_write = int(input("How much money would you like to store in your PiggyBank?"))
f = open("PiggyBanks_Records.txt", "w")
current_time = strftime("%Y-%m-%d %H:%M:%S", gmtime())
convert_1 = str(current_time)
convert_2 = str(int(num_write))
add_1 = ("\n" + convert_1 + " £" + convert_2)
add_2 = ("\n" + add_1) #Tried to make it so new line is added every time the program is run
final_record = str(add_2)
print("Final file written to the PiggyBank: " + final_record)
#Write to File
f.write(final_record)
f.close()
Right now whenever the program writes to the file it over-writes. I would preferably would like to keep, like a history of the amounts added. If anyone can help so the string that needs to be written to the .txt file goes down by one line and essentially keeps going for ever. I am also open to any suggestion on how I can shorten this code.
You need to open your file with append mode :
f = open("PiggyBanks_Records.txt", "a")
Using the 'w' write option with open automatically looks for the specified file, and deletes its contents if it already exists (which you can read about here) or creates it if it doesn't. Use 'a' instead to add / append to the file.

Change a target line of a file

I'm running a process, which uses a set of parameters defined in a file parameters.py
Now, I'd like to swept the value of one of the parameters over certain range. Thus, I wrote a batch file to automatise the procedure.
Problem
My problem is that I need to modify the 20th line of the file parameter, from value = 200 to value = $VAR, where $VAR sets the new value of the parameter.
Question
Is there a bash command that allows to change a specific target line of the file?
Thank you and cheers.
Try doing this with sed :
sed -i "20s/value = 200/value = $VAR/" file.txt
That will change value = 200 to value = $VAR on line 20. s/// is a skeleton for sed substitutions : s/before/after/
The -i switch edit the file in place
While it's certainly possible, I wouldn't recommend it. One day, someone will add a comment before this line or an empty line and your script will break.
Use sed instead:
sed -e 's/value = 200/value = $VAR/' < parameters.py
An even better solution would be to move everything in parameters.py into parameterdefaults.py like so:
parameterdefaults.py:
options = {
"value": 200,
}
parameters.py:
import parameterdefaults
options = dict( **parameterdefaults.options ) # copy defaults
options['value'] = 10 # Override the few values you need to change
or even better:
codethatneedsparameters.py:
import parameterdefaults
import parameters
options = dict( **parameterdefaults.options ) # copy defaults
options.update( parameters.options ) # merge with changes
Now you can write:
parameters.py:
options = {
"value": 10,
}
The easiest way (but not the best) to do this instead of hacking the script is to change it from
value = 200
to
import os # somewhere at the top if need be.
value = int(os.environ.get('VAR', 200))
That way it can be modified from the calling process what ever that may be.

Batch file programming: problem with variable

SET SS_SOURCE_PROJECT = sausages
#echo SS_SOURCE_PROJECT = %SS_SOURCE_PROJECT%
This isn't working, it just outputs:
SS_SOURCE_PROJECT =
But I am expecting
SS_SOURCE_PROJECT = sausages
This is on WinXP if it matters. What obvious dumb thing am I doing wrong?
remove spaces:
SET SS_SOURCE_PROJECT=sausages
Yes, batch syntax is terrible.
To expand on #Stefan's answer, the original code works like this: (note the spaces)
C:\>SET SS_SOURCE_PROJECT = sausages
C:\>echo "%SS_SOURCE_PROJECT %"
" sausages"

Compact C Folding in Vim

I'm trying to make a simple Vim script that would create very compact top-level folds for c files. Ideally, if it was run on this code:
static void funca(...)
{
...
}
/* Example comment */
static void funcb(...)
{
...
}
Then it would create folds which would look like this when closed:
+-- x Lines: static void funca(...)----------------------
+-- x Lines: static void funcb(...)----------------------
So basically it would be like foldmethod=syntax with foldlevel=1, except that each fold would start one line further up, and would extend further down to include all following blank lines.
I know how to make one of these folds (assuming foldmethod=manual):
/^{<cr>kVnn?^$<cr>zf
But I'm not sure how to put it into a function. This is my effort:
function Cfold()
set foldmethod=manual " Manual folds
ggzE " Delete all folds
while (/^{<cr>) " Somehow loop through each match
kVnn?^$<cr>zf " This would work fine except for the last function
endwhile
endfunction
map <Leader>f :call Cfold()<cr>
But it isn't valid, I'm not entirely sure how functions work. Also, it won't work for the last function in the file, since it won't find '^{' again. If someone could help me get this working, and somehow add a case for the last function in a file, I would be extremely grateful.
Thanks in advance :)
You can create folds programmatically using the foldexpr and foldtext. Try this, though you may have to tweak CFoldLevel so it doesn't swallow non-function parts of the code:
function! CFoldLevel(lnum)
let line = getline(a:lnum)
if line =~ '^/\*'
return '>1' " A new fold of level 1 starts here.
else
return '1' " This line has a foldlevel of 1.
endif
endfunction
function! CFoldText()
" Look through all of the folded text for the function signature.
let signature = ''
let i = v:foldstart
while signature == '' && i < v:foldend
let line = getline(i)
if line =~ '\w\+(.*)$'
let signature = line
endif
let i = i + 1
endwhile
" Return what the fold should show when folded.
return '+-- ' . (v:foldend - v:foldstart) . ' Lines: ' . signature . ' '
endfunction
function! CFold()
set foldenable
set foldlevel=0
set foldmethod=expr
set foldexpr=CFoldLevel(v:lnum)
set foldtext=CFoldText()
set foldnestmax=1
endfunction
See :help 'foldexpr' for more details.

Resources