Ruby block comment and profile questions - ruby

I have written a Ruby version of Erik Demaine's (MIT) docdist8.py. This is available on github as docdist-v3.rb. I faced two weird kind of situations:
1) In the function inner_product there is a block comment:
Inner product between two vectors, where vectors
are repeated as dictionaries of (word, freq) pairs.
Example : inner_product({"and":3, "of":2, "the":5},
{"and":4, "in":1, "of":1, "this":2}) = 14.0
If I wrap this with =begin and =end there is no problem, but if I wrap it with triple double-quotes """, I get errors as follows:
./docdist-v3.rb:71: syntax error, unexpected tIDENTIFIER, expecting kEND
Example : inner_product({"and":3, "of":2, "the":5},
^
./docdist-v3.rb:71: syntax error, unexpected tIDENTIFIER, expecting kEND
Example : inner_product({"and":3, "of":2, "the":5},
^
./docdist-v3.rb:72: syntax error, unexpected kIN, expecting kEND
... {"and":4, "in":1, "of":1, "this":2}) = 14.0
^
./docdist-v3.rb:72: syntax error, unexpected tIDENTIFIER, expecting kEND
... {"and":4, "in":1, "of":1, "this":2}) = 14.0
^
./docdist-v3.rb:72: syntax error, unexpected tIDENTIFIER, expecting kEND
..."and":4, "in":1, "of":1, "this":2}) = 14.0
^
Are there rules / allowed entries for """ that are different from =begin and =end?
2) When I run my program with the time command it executes in about 0.3 seconds. However, if I put require 'profile' the time it takes becomes very high in comparison - 30 seconds. Hence I don't get the correct output at all. This doesn't seem to be the case with the original Python version, where it takes only a marginal extra time to profile. How do I get the same profile run in Ruby?
Note: The two files I used to run the Ruby program are t2.bobsey.txt and t3.lewis.txt. They are available at http://ocw.mit.edu/ans7870/6/6.006/s08/lecturenotes/dd_data.htm

1) Block comments always have the form:
=begin
Comment
=end
You are actually creating a string that is never used:
"""
Not a comment
"""
# => "\nNot a comment\n"
That's why you get an error when adding another quote and that's why the syntax highlighting renders them as strings.
2) It's slower with profiler but I get identical results:
ruby docdist-v3.rb t2.bobsey.txt t3.lewis.txt
File t2.bobsey.txt:262111 lines,49785 words,3354 distinct words
File t3.lewis.txt:1015474 lines,182355 words,8530 distinct words
The distance between the documents is: 0.574160 (radians)

Related

Typing multiline expressions in nix repl

This PR seems to indicate that it was at least a feature in nix repl's predecessor, nix-repl.
When I try to replicate the example in the PR, I get errors.
nix-repl> rec {
error: syntax error, unexpected end of file, at (string):1:6
nix-repl> a = b.foo;
error: syntax error, unexpected ';', expecting end of file, at (string):1:7
nix-repl> b.foo.bar = 12;
error: syntax error, unexpected '=', expecting end of file, at (string):1:23
nix-repl> }
error: syntax error, unexpected '}', at (string):1:11
nix-repl>

IndentationError: unexpected unindent in time series

The code i wrote in time series forecasting is as follows.
It keeps getting the error.
for param in pdq:
for param_seasonal in seasonal_pdq:
try:
mod = sm.tsa.statespace.SARIMAX(indexedDataset,order=param,seasonal_order=param_seasonal,enforce_stationarity=False,enforce_invertibility=False)
results = mod.fit()
print('ARIMA{}x{}12 - AIC:{}'.format(param,param_seasonal,results.aic))
except: continue
File "<ipython-input-211-c755485ed830>", line 7
except: continue
^
IndentationError: unexpected unindent
for param in pdq:
for param_seasonal in seasonal_pdq:
try:
mod = sm.tsa.statespace.SARIMAX(indexedDataset,order=param,seasonal_order=param_seasonal,enforce_stationarity=False,enforce_invertibility=False)
results = mod.fit()
print('ARIMA{}x{}12 - AIC:{}'.format(param,param_seasonal,results.aic))
except:
continue
except needs to be at the same level of indent with try.
Please be more careful with indentation when you write python scripts.

mocha/chai test Unexpected token =>

I'm running a test, and I'm getting an unexpected error.
I'm sorting the results:
docs.sort((a, b) => m_ids.findIndex(id => a._id.equals(id)) -
m_ids.findIndex(id => b._id.equals(id)));
The error I'm getting is definitely related to that line
mbp:test testlab$ mocha .
/Users/testlab/Documents/workspace/KBase/controller/KBase.js:112
docs.sort((a, b) => m_ids.findIndex(id => a._id.equals(id)) -
^^
SyntaxError: Unexpected token =>
I was thinking about skipping the code by putting an if statement around it, but that doesn't seem to work either.
I rewrote the sort to not use =>, so it is working on the webpage and it is working on chai/mocha now.

How to find the line causing error in Julia?

Suppose there is a script A that calls function B, both in Julia.
There are some errors in function B, which cause the script to be stopped at runtime.
Is there a neat way to find out which line is causing the error?
It does not make any sense, to have to put messages like println manually in each line to find out upto which line the code survives, and in which line error happens.
Edit: I am using Linux Red Hat 4.1.2 and Julia version 0.3.6. directly. With no IDE.
Reading the backtrace:
juser#juliabox:~$ cat foo.jl
# line 1 empty comment
foo() = error("This is line 2")
foo() # line 3
juser#juliabox:~$ julia foo.jl
ERROR: This is line 2
in foo at /home/juser/foo.jl:2
in include at ./boot.jl:245
in include_from_node1 at loading.jl:128
in process_options at ./client.jl:285
in _start at ./client.jl:354
while loading /home/juser/foo.jl, in expression starting on line 3
This lines in foo at /home/juser/foo.jl:2 ... while loading /home/juser/foo.jl, in expression starting on line 3 reads as: "there was an error at line 2 in /home/juser/foo.jl file ... while loading /home/juser/foo.jl, in expression starting on line 3"
Looks pretty clear to me!
Edit: /home/juser/foo.jl:2 means; file: /home/juser/foo.jl, line number: 2.
Also you could use #show macro instead of println function for debugging purposes:
julia> println(1 < 5 < 10)
true
julia> #show 1 < 5 < 10
(1<5<10) => true
true

Problems catching unidecoder exceptions

I'm trying out the unidecoder gem and it's giving me problems with some strings:
require 'unidecoder'
str = "\u00A3"
str.to_ascii
#: (C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder/data/x00.yml):
found unknown escape character while parsing a quote d scalar at line
2 column 3
from C:/Ruby193/lib/ruby/1.9.1/psych.rb:203:in parse'
from C:/Ruby193/lib/ruby/1.9.1/psych.rb:203:inparse_stream'
from C:/Ruby193/lib/ruby/1.9.1/psych.rb:151:in parse'
from C:/Ruby193/lib/ruby/1.9.1/psych.rb:127:inload'
from C:/Ruby193/lib/ruby/1.9.1/psych.rb:297:in block in load_file'
from C:/Ruby193/lib/ruby/1.9.1/psych.rb:297:inopen'
from C:/Ruby193/lib/ruby/1.9.1/psych.rb:297:in load_file'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder.rb:8:in
block in '
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder.rb:78:in
yield'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder.rb:78:in
default'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder.rb:78:in
decode_char'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder.rb:39:in
block in decode'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder.rb:37:in
gsub'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder.rb:37:in
decode'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder.rb:16:in
to_ascii'
from (irb):21
from C:/Ruby193/bin/irb:12:in'>>
What's worse, I can't catch the error by doing:
foo = str.to_ascii rescue 'x'
Does anyone know what's happening here?
rescue clause with no parameter list, the parameter defaults to StandardError; it looks like unidecoder raises kinda other exception, but the stacktrace seems to be incomplete (it should show the exception type.)
Take a look at "C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder/data/x00.yml". Line 2 is an YAML entry - "\z",which is not a valid escape sequence in Ruby(but a Regexp anchor to mark the end of string). This might be a bug. You can edit this line to - "\x00".
However, "\u00A3"(£) is not a valid ASCII character, I didn't find the point of encoding it to ASCII.
The exception raised is Psych::SyntaxError, you can catch that specific exception, as #mudasobwa commented.

Resources