Dynamic function name definition in Julia.. possible? - dataframe

I have a DataFrame structured as parName|region|year, and access function as getData(parName,reg,year) ( I use access function because I implement my own query logic).
Would it be possible, based on unique(df[:parName]), to dynamically create a set of functions like par1(region,year) "pointing to" getData("par1",region,year) ?
If so, using which approach?
This is a bit the opposite of this question.. there it is explained how to dynamically call a function, while I wander if it possible to dynamically declare/define one..
EDIT:
I am using this approach in order to get the cleanest and most compact syntax possible in writing multi-dimensional equations.
I managed (thanks to the #Liso answer) to implement it as:
for par in unique(dropna(df[:parName]))
#eval ($(Symbol("$(par)_"))) = (r,d1,d2="",y=-1,op=sum) -> gd($par,r,d1,d2,y,op)
#eval ($(Symbol("$(par)!"))) = (v,r,d1,d2="",y=-1) -> sd(v,$par,r,d1,d2,y)
end
i.e., I am using the convention that par!() is a setData-type and par_() is a getData-type equation.
When I'll be able to complete the macro that transforms f(dim1,dim2) = value into f(value,dim1,dim2) I will be able to write my model using a LaTeX-like (and AMPL-like) syntax that is very clear:
#meq price!(tp in secProducts, r in fr2) = sum(price_(r,pp,"",y2)*a_(r,pp,tp,y2) for pp in priProducts) + margin_(r,tp,"",y2)

I am just beginner trying to understand Julia, so I am not sure if it is good idea or not!
See https://docs.julialang.org/en/stable/manual/metaprogramming/#Code-Generation-1 .
I was able to adapt that example to this :
julia> for i in 4:6
#eval ($(Symbol("func$i")))(a) = a^$i
end
julia> func4(2), func5(2), func6(2)
(16, 32, 64)
Maybe it could help you to play and learn :)

Related

undefined method `and' for #<Arel::Attributes::Attribute

I'm having an issue getting a query to work.
I'm essentially trying to write something like the following SQL, with the literal 5 replaced with a variable:
SELECT *
FROM "my_table"
WHERE 5 BETWEEN "my_table"."minimum" AND "my_table"."maximum"
This is what I have at the moment:
MyModel.where(
Arel::Nodes::Between.new(
my_variable, (MyModel.arel_table[:minimum]).and(MyModel.arel_table[:maximum])
)
)
Please ignore the way I am using arel_table, the actual query has multiple joins and is more complex, but this is the most minimum reproducible example I have to demonstrate the problem.
The error, as in the subject of the question is as follows:
undefined method `and' for #<Arel::Attributes::Attribute:0x00007f55e15514f8>
and method is for Arel::Nodes::Node i.e. MyModel.arel_attribute[:name].eq(Arel::Nodes::Quoted.new('engineersmnky')) This is an Arel::Nodes::Equality and you can chain with and.
That being said you can construct an Arel::Nodes::And for yourself via
Arel::Nodes::And.new([left,right])
Then we can pass this to the Between class like so
Arel::Nodes::Between.new(
Arel::Nodes::Quoted.new(my_variable),
Arel::Nodes::And.new([
MyModel.arel_table[:minimum],
MyModel.arel_table[:maximum]])
)
The Arel::Nodes::Quoted (also: Arel::Nodes.build_quoted(arg)) is not needed in your case since your my_variable is an Integer which can be visited and will be treated as an Arel::Nodes::SqlLiteral but I find it best to let arel decide how to handle the quoting in case your my_variable ends up being some other un-visitable Object
There are other ways to create a Between and other ways to create an And depending on what objects you are dealing with.
between is a Arel::Predication and these predications are available to Arel::Nodes::Attribute objects e.g.
MyModel.arel_table[:minimum].between([1,6])
and as mentioned is available to Arel::Nodes::Node and instances of this class provides a convenience method (create_and) for creating an And so we could do the following:
Arel::Nodes::Node.new.create_and([
MyModel.arel_table[:minimum],
MyModel.arel_table[:maximum]])
There are a number of other ways to hack this functionality together by using other Arel classes but this should get you headed in the right direction.

Parsing a SQL spatial column in Python

I am struggling a bit as I am new to programming. I am currently writing a python script and I am a bit stuck. The goal is to parse some spatial information the gets pulled from SQL to a format that is usable for my py script down the line.
I was able to CAST through a SQL query and fetchall using the obdc module. However once I fetch the data that is where it gets trick for me. Here is an example of a print from the fetchall:
[(u'POLYGON ((7014.186279296875 6602.99658203125 1612.5, 7015.984375 6600.416015625 1612.5))',), (u'POLYGON ((6730.962646484375 6715.2490234375 1522.5, 6730.0869140625 6714.13916015625 1522.5))',)]
I am not exactly sure what I am getting here it is like a list of tuples. which I have tried converting to a list of list, but there must be something I am missing.
Here is the usable format I am looking for:
[[7014.186279296875, 6602.99658203125, 1612.5], [7015.984375, 6600.416015625, 1612.5]]
[[6730.962646484375, 6715.2490234375, 1522.5], [6730.0869140625, 6714.13916015625, 1522.5]]
Any ideas of how I can accomplish this? Maybe there is a better way to CAST in SQL or a module in python that would be easier to use instead of just doing a cursor.fetchall() and parsing? Or any any parsing help would be useful. Thanks.
If you want to do parsing, that should be straight forward. For example you've provided next code would do the thing:
result = []
for element in data:
single_elements = element[0][10:-2].split(', ')
for se in single_elements:
row = str(se).split(' ')
result.append([float(a) for a in row])
Result will contain what you need. If parsing is not an option, then paste some of your code so I can see how you're fetching data.

Extending dplyr and use of internal functions

I'm working on a fork of the RSQLServer package and am trying to implement joins. With the current version of the package, joins for any DBI-connected database are implemented using sql_join.DBIConnection. However, that implementation doesn't work well for SQL server. For instance, it makes use of USING which is not supported by SQL server.
I've got a version of this function sql_join.SQLServerConnection working (though not complete yet). I've based my function on sql_join.DBIConnection as much as possible. One issue I've had is that sql_join.DBIConnection calls a number of non-exported functions within dplyr such as common_by. For now, I've worked around this by using dplyr:::common_by, but I'm aware that that's not ideal practice.
Should I:
Ask Hadley Wickham/Romain Francois to export the relevant functions to make life easier for people developing packages that build on dplyr?
Copy the internal functions into the package I'm working on?
Continue to use the ::: operator to call the functions?
Something else?
Clearly with option 3, there's a chance that the interface will change (since they're not exported functions) and that the package would break in the longer term.
Sample code:
sql_join.SQLServerConnection <- function (con, x, y, type = "inner", by = NULL, ...) {
join <- switch(type, left = sql("LEFT"), inner = sql("INNER"),
right = sql("RIGHT"), full = sql("FULL"), stop("Unknown join type:",
type, call. = FALSE))
by <- dplyr:::common_by(by, x, y)
using <- FALSE # all(by$x == by$y)
x_names <- dplyr:::auto_names(x$select)
y_names <- dplyr:::auto_names(y$select)
# more code
}
It looks to me like you may not have to use those functions verbs. Since dplyr now put it's database functionality in dbplyr, the relevant code is here. I don't see the use of auto_names or common_by there.
I strongly recommend following the steps in Creating New Backends after reading SQL Translation.
It may also be worth reviewing some other alternative backends, such as Hrbrmaster's sergeant package for Apache Drill using JDBC.

Mathematica- Solve when given random variables and set equations

I'm trying to figure out if there's a way in mathematica where I can solve for particular variables when given other variables and a set of equations. Essentially there are 6 variables, and I'm given 3 of them and have to calculate the others using these equations-
Variables-
B,Qs,f0,R,c,L
Equations-
f0=1/(2*Pi*Sqrt[L*c])
Qs=(w*L)/R
w=2*Pi*f0
B=f0/Qs
We are given the values of any 3 of those variables and have to figure out the rest using those values.
I was thinking perhaps using Eliminate but I'm not sure exactly how that would be structured as I've only used it previously with set variables that don't change and a single output.
When using the Solve function with Mathematica, you can specify for what variables you want Solve to specify the solutions. Note that Solve may not be able to find expressions in terms of these variables (if the equations you give it are contradictory or insufficient) or for all values as some functions have no inverse or only partial inverses.
Your question looks a lot like homework in Electromagnetics, but here is an example with your original problem. You will have to adapt these ideas to give to Solve the set of variables you are looking for. Also remember to use == to specify equality testing. A simple = is for immediate assignment to a variable.
Solve[{f0 == 1/(2*Pi*Sqrt[L*c]), Qs == (w*L)/R, w == 2*Pi*f0, B == f0/Qs}, {f0, B, c}]
{{f0->w/(2 [Pi]), B->w/(2 [Pi] Qs), c->L/(Qs^2 R^2)}}

Using statement with more than one system resource

I have used the using statement in both C# and VB. I agree with all the critics regarding nesting using statements (C# seems well done, VB not so much)
So with that in mind I was interested in improving my VB using statements by "using" more than one system resource within the same block:
Example:
Using objBitmap As New Bitmap(100,100)
Using objGraphics as Graphics = Graphics.From(objBitmap)
End Using
End Using
Could be written like this:
Using objBitmap As New Bitmap(100,100), objGraphics as Gaphics = Graphics.FromImage(objbitmap)
End Using
So my question is what is the better method?
My gut tells me that if the resources are related/dependent then using more than one resource in a using statement is logical.
My primary language is C#, and there most people prefer "stacked" usings when you have many of them in the same scope:
using (X)
using (Y)
using (Z)
{
// ...
}
The problem with the single using statement that VB.NET has is that it seems cluttered and it would be likely to fall of the edge of the screen. So at least from that perspective, multiple usings look better in VB.NET.
Maybe if you combine the second syntax with line continuations, it would look better:
Using objBitmap As New Bitmap(100,100), _
objGraphics as Graphics = Graphics.FromImage(objbitmap)
' ...
End Using
That gets you closer to what I would consider better readability.
They are both the same, you should choose the one that you find to be the most readable as they are identical at the IL level.
I personally like the way that C# handles this by allowing this syntax:
using (Foo foo = new Foo())
using (Bar bar = new Bar())
{
// ....
}
However I find the VB.NET equivalent of this form (your second example) to be less readable than the nested Using statements from your first example. But this is just my opinion. Choose the style that best suits the readability of the code in question as that is the most important thing considering that the output is identical.