In julia, how do I assign the output of an expression to a new variable? - julia

Stupid example, I would like to do something like
X=println("hi"),
and get
X="hi".

The general solution is to use IOBuffer and takebuf_string as described by #ARM above. If it's enough to capture the output of print, then
s = string(args...)
gives the string that would have been printed by print(args...). Also,
s = repr(X)
gives the string that would have been printed by showall(X). Both are implemented using IOBuffer and takebuf_string internally.

I think the poster wants to access the nice summary format that you can get from println. One way to access that as a string is to write to a buffer using print and then read it back as a string. There's probably also an easier way.
using DataFrames
data = DataFrame()
data[:turtle] = ["Suzy", "Suzy", "Bob", "Batman", "Batman", "Bob", "Adam"]
data[:mealType] = ["bug", "worm", "worm", "bug", "worm", "worm", "stick"]
stream = IOBuffer()
println(data)
print(stream, data)
yourString = takebuf_string(stream)
returns
"7x2 DataFrame\n| Row | turtle | mealType |\n|-----|----------|----------|\n| 1 | \"Suzy\" | \"bug\" |\n| 2 | \"Suzy\" | \"worm\" |\n| 3 | \"Bob\" | \"worm\" |\n| 4 | \"Batman\" | \"bug\" |\n| 5 | \"Batman\" | \"worm\" |\n| 6 | \"Bob\" | \"worm\" |\n| 7 | \"Adam\" | \"stick\" |"

If you are after formatted strings you can use #sprintf.
julia> x = #sprintf("%s", "hi")
"hi"
julia> x
"hi"
julia> x = #sprintf("%d/%d", 3, 4)
"3/4"
It's a macro though so be careful

Related

How to separate out letters in a sentence using R

I have a character vector that is a string of letters and punctuation. I want to create a data frame where each column is made up of a letter/character from this string.
e.g.
Character string = I WENT TO THE FAIR
Dataframe = | I | | W | E | N | T | | T | O | | T | H | E | | F | A | I | R |
I thought I could do this using a loop with substr, but I can't work out how to get R to write into separate columns, rather than just writing over the previous letter. I'm new to writing loops etc so struggling a bit to get my head around the way in which to compose what I need.
Thanks for any help and advice that you can offer.
Best wishes,
Natalie
This should get that result
string <- "I WENT TO THE FAIR"
df <- as.data.frame(t(as.data.frame(strsplit(string,""))), row.names = "1")

How to eliminate nothing elements in a array (1D) in Julia?

I would like to know how I could eliminate nothing elements in a Julia array (1D) like the one below. It was built from reading a text file with lines with no relevant information mixed with lines with relevant information. "nothing" is type Void and I would like to clean the array of all of it.
nothing
nothing
nothing
nothing
nothing
" -16.3651\t 0.1678\t -4.6997\t -14.0152\t -2.6855\t -16.0294\t -7.8049\t -27.1912\t -5.0354\t -14.5187\t\r\n"
" -16.4490\t -1.0910\t -3.6087\t -12.6724\t -1.5945\t -14.7705\t -7.2174\t -25.2609\t -3.7766\t -14.3509\t\r\n"
" -16.4490\t -2.2659\t -2.4338\t -10.9100\t -0.5875\t -13.6795\t -6.7139\t -22.9950\t -2.9373\t -14.0991\t\r\n"
testvector[testvector.!=nothing] is also a very readable option.
benchmarking can help choose the most efficient code.
How are you reading that file?
You can filter out nothings from an array:
filter(x -> !is(nothing, x), [nothing, 42]) # => Any[42]
But you may want to clean your data first, with a tsv (tab separated values) file like this:
-16.3651 0.1678 -4.6997 -14.0152 -2.6855 -16.0294 -7.8049 -27.1912 -5.0354 -14.5187
-16.4490 -1.0910 -3.6087 -12.6724 -1.5945 -14.7705 -7.2174 -25.2609 -3.7766 -14.3509
-16.4490 -2.2659 -2.4338 -10.9100 -0.5875 -13.6795 -6.7139 -22.9950 -2.9373 -14.0991
Using readdlm:
julia> readdlm("data.tsv")
3x10 Array{Float64,2}:
-16.3651 0.1678 -4.6997 -14.0152 … -27.1912 -5.0354 -14.5187
-16.449 -1.091 -3.6087 -12.6724 -25.2609 -3.7766 -14.3509
-16.449 -2.2659 -2.4338 -10.91 -22.995 -2.9373 -14.0991
Using DataFrmaes.readtable:
julia> df = readtable("data.tsv");
julia> names!(df, [symbol(x) for x in 'A':'J'])
2x10 DataFrames.DataFrame
| Row | A | B | C | D | E | F | G |
|-----|---------|---------|---------|----------|---------|----------|---------|
| 1 | -16.449 | -1.091 | -3.6087 | -12.6724 | -1.5945 | -14.7705 | -7.2174 |
| 2 | -16.449 | -2.2659 | -2.4338 | -10.91 | -0.5875 | -13.6795 | -6.7139 |
| Row | H | I | J |
|-----|----------|---------|----------|
| 1 | -25.2609 | -3.7766 | -14.3509 |
| 2 | -22.995 | -2.9373 | -14.0991 |
one simple way is using filter! function to update your vector like this:
testvector=[fill(nothing,10) ; [1,2,3]];
# =>13-element Array{Any,1}:
# nothing
# nothing
# nothing
# nothing
# nothing
# nothing
# nothing
# nothing
# nothing
# nothing
# 1
# 2
# 3
filter!(x->x!=nothing, testvector)
# => 3-element Array{Any,1}:
# 1
# 2
# 3
thanks #Daniel Arndt
EDIT, Refer to this paragraph from Julia doc:
nothing is a special value that does not print anything at the
interactive prompt. Other than not printing, it is a completely normal
value and you can test for it programmatically.
I think all of the conditions below, reach us to the same result
x!=nothing
x!==nothing
!is(x,nothing)
!isa(x,Void)
typeof(x)!=Void
To add to the answers above, it appears:
filter(!isnothing, [nothing, 42])
is a working shorthand for filter(x -> !isnothing(x), [nothing, 42]), and will correctly return 42.
Dear All,
At the end, the code became this:
tmpFile=open(fileName)
tmp=readdlm(tmpFile);
ind=pmap(typeof,tmp[:,1]).!=SubString{ASCIIString}; # if the first column typeof is string, than pmap will return false, else, it return true. This will provide an index of valid/not valid rows.
tmpClean=tmp[ind,:]; # only valid rows will be used
If you may have any suggestion to improve it, I would appreciate it. Thank you for your help.

Compare stored values in Selenium IDE

I am new to test automation and to Selenium IDE. With Selenium IDE, I want to store two values(integer) and compare them. Test passes if the compared result is greater than or equal to zero. So far, I only found an option to store the values and wondering if there is any option to compare the stored values.
Any suggestion would be helpful.
Thanks
Okay, assuming you're always subtracting A (constant value) from B(variable value), you can use some javascript to perform the test.
store | 2 | A
store | 4 | B
storeEval | var s = false; s = eval((storedVars['B'] - storedVars['A']) >=0); | s
verifyExpression | ${s}
replace the two store steps above with whatever you use to get your variables A and B.
The verifyExpression line will pass(return true) if result is greater than or equal to zero, will fail(stay false) if not.
store |2| A
store |4| B
storeEval |var s = false; s = eval((storedVars['B'] - storedVars['A']) >=0);| s
echo |${s}|
Executing: |store | 2 | A |
Executing: |store | 4 | B |
Executing: |storeEval | var s = false; s = eval((storedVars['B'] - storedVars['A']) >=0); | s |
script is: var s = false; s = eval((storedVars['B'] - storedVars['A']) >=0);
Executing: |echo | ${s} | |
echo: true
Test case passed

Replacement and non-matches with 'sub'

Months ago I ended up with a sub statement that originally worked with my input data. It has since stopped working causing me to re-examine my ugly process. I hate to share it but it accomplished several things at once:
active$id[grep("CIR",active$description)] <- sub(".*CIR0*(\\d+).*","\\1",active$description[grep("CIR",active$description)],perl=TRUE)
This statement created a new id column by finding rows that had an id embedded in the description column. The sub statement would find the number following a "CIR0" and populate the id column iff there was an id within a row's description. I recognize it is inefficient with the embedded grep subsetting either side of the assignment.
Is there a way to have a 'sub' replacement be NA or empty if the pattern does not match? I feel like I'm missing something very simple but ask for the community's assistance. Thank you.
Example with the results of creating an id column:
| name | id | description |
|------+-----+-------------------|
| a | 343 | Here is CIR00343 |
| b | | Didn't have it |
| c | 123 | What is CIR0123 |
| d | | CIR lacks a digit |
| e | 452 | CIR452 is next |
I was struggling with the same issue a few weeks ago. I ended up using the str_match function from the stringr package. It returns NA if the target string is not found. Just make sure you subset the result correctly. An example:
library(stringr)
str = "Little_Red_Riding_Hood"
sub(".*(Little).*","\\1",str) # Returns 'Little'
sub(".*(Big).*","\\1",str) # Returns 'Little_Red_Riding_Hood'
str_match(str,".*(Little).*")[1,2] #Returns 'Little'
str_match(str,".*(Big).*")[1,2] # Returns NA
I think in this case you could try using ifelse(), i.e.,
active$id[grep("CIR",active$description)] <- ifelse(match, replacement, "")
where match should evaluate to true if there's a match, and replacement is what that element would be replaced with in that case. Likewise, if match evaluates to false, that element's replaced with an empty string (or NA if you prefer).

AdvancedDatagrid Iterating Through Each Row of the Open Leaf/Tree

I need to get the data for each row in an advanceddatagrid where the nodes are open.
For example, my ADG looks like this:
+ Science
- Math
- Passed
John Doe | A+ | Section C
Amy Rourke | B- | Section B
- Failed
Jane Doe | F | Section D
Mike Cones | F | Section D
- English
+ Passed
+ Failed
- History
+ Passed
- Failed
Lori Pea | F | Section C
I tried using the following code to get the open nodes:
var o:Object = new Object();
o = IHierarchicalCollectionView(myADG.dataProvider).openNodes;
But doing the following code to inspect the object:
Alert.show(ObjectUtil.toString(o), 'object inpsection');
Gives me:
(Object)#0
Math (2)
children = (mx.collections::ArrayCollection)#2
filterFunction = (null)
length = 2
list = (mx.collections::ArrayList)#3
length = 2
source = (Array)#4
[0] (Object)#5
children = (mx.collections::ArrayCollection)#6
filterFunction = (null)
length = 2
list = (mx.collections::ArrayList)#7
length = 2
source = (Array)#8
[0] <Table>
<Name>John Doe</Name>
<Grade>A+</Grade>
<Section>Section C</Section>
</Table>
[1] <Table>
<Name>Amy Rourke</Name>
<Grade>B-</Grade>
<Section>Section B</Section>
....
...
..
Basically, I just need to create an object or array or xmllist that would give me:
Math | Passed | John Doe | A+ | Section C
Math | Passed | Amy Rourke | B- | Section B
Math | Failed | Jane Doe | F | Section D
Math | Failed | Mike Cones | F | Section D
History | Failed | Lori Pea | F | Section C
Any suggestion would be highly appreciated. Thanks
You should be able to iterate across the openNodes object's properties and for each one grab the collection and concat the values onto a new array then use that as the source of another type of collection if necessary. Something like this:
var newArray:Array = [];
for(var property:String in o)
{
newArray = newArray.concat(o[property][0].source); //Passed, property is subject as in Math
newArray = newArray.concat(o[property][1].source); //Failed property is subject as in Math
}
The only real problem with this is you're trying to also keep the Math and passed or failed in the objects, otherwise the above should work. To get this other part working I think you need to break each of the statements above into it's own loop that iterates across the source of the openNodes object and puts the right values into a new Value Object you make up that has the subject and the pass or fail set on it. Then you could store these values as well, also notice I'm assuming the pass fail is always organized this way in the original data structure where in each subject you'll have two arrays and the first will be pass followed by fail.

Resources