DolphinDB error when calling udf: “The object date is neither a XDB connection nor a function definition.” - user-defined-functions

server version: 2.00.8 2022.09.28
I define a function myFunc in DolphinDB as follows. The definition part and each line of code in the function body can be executed successfully. But error occurs when the script is run:
Server response: 'myFunc("20230118", t) => myFunc: tmp = select * from t where date(dt) == date => The object date is neither a XDB connection nor a function definition.'
dt = 2023.01.18T04:01:51.100 2023.01.19T04:01:51.000 2023.01.19T04:01:51.900
sym = ["IBM", "MSFTN", "GOOGS"]
value = 1..3
t=table(dt, sym, value)
def myFunc(day, t){
date = temporalParse(day, "yyyyMMdd")
tmp = select * from t where date(dt)=date
return tmp
}
myFunc("20230118", t)

It works after just replaced the variable date to dateParsed:
def myFunc(day, t){
dateParsed = temporalParse(day, "yyyyMMdd")
tmp = select * from t where date(dt)=dateParsed
return tmp
}

The error is reported because a variable named “date” is defined in the function body, and the built-in function date is called later. When the function is parsed, date(dt) cannot be recognized as a function as the variable is parsed first. Thus it is not recommended to define variables with the same name as built-in functions or other keywords.
The script can be corrected to:
def myFunc(day, t){
myDate = temporalParse(day, "yyyyMMdd")
tmp = select * from t where date(dt)=myDate
return tmp
}
myFunc("20230118", t)

Related

Pandoc: Separate table of contents for each section

I am converting Markdown to HTML with Pandoc. With --toc, Pandoc generates a table of contents and inserts it under the first H1 heading (of which there is only one).
I would like it to have a separate, additional table of contents for each subheading. More specifically, I would like a small local table of contents under each H3.
Can Pandoc do that, and if yes, how?
I received an answer on the pandoc-discuss mailing list in the form of a Lua filter.
Quoting from the author of the solution:
Warning: Assumes that all chapters are heading level 2 — change the chapter_level and toc_level variables to match!
Warning: Assumes that each section/chapter has a unique identifier!
local chapter_level = 2
local toc_level = 3
local headings = {}
local current_chapter = nil
local function collect_headings (head)
if head.level == chapter_level then
local id = head.identifier
current_chapter = {
chapter = id,
toc = {},
}
headings[id] = current_chapter
elseif head.level == toc_level then
if current_chapter then
local toc = current_chapter.toc
toc[#toc+1] = head
end
end
return nil
end
local function build_toc (heads)
local toc = {}
for _,head in ipairs(heads) do
local entry = {
pandoc.Plain{
pandoc.Link(
head.content:clone(), -- text
'#' .. head.identifier, -- target
"", -- empty title
pandoc.Attr(
"", -- empty identifier
{'local-toc-link'} -- class
)
)
}
}
toc[#toc+1] = entry
end
return pandoc.Div(
{ pandoc.BulletList(toc) },
pandoc.Attr( "", {'local-toc'} )
)
end
local function insert_toc (head)
if head.level == chapter_level then
local id = head.identifier
if headings[id] then
local toc = build_toc(
headings[id].toc
)
return {head,toc}
end
end
return nil
end
return {
{ Header = collect_headings },
{ Header = insert_toc },
}

How to create autoincrement variable Odoo POS

I'm working on these files to add a custom autoincrement value to each pos ticket generated in odoo v10:
Point_of_sale es el modulo
.. \addons\point_of_sale\static\src\js\ models.js
.. \addons\pos_ticket\static\src\xml\pos_ticket_view.xml
odoo code:
Model.js
order_id_ : function (){
var x = 1;
if(this.pos.order.id)
{
x = this.pos.order.id++;
}
else
{
x = x++;
}
function sequense(num)
{
var s = ""+ num;
while (s.length < 8)
{
s = "0" + s;
}
return s;
}
return sequense(x);
},
pos_ticket_view.xml
<t t-esc="order.order_id_()" / >
But when I run it like this I get this error because the variable isn't yet created:
Your if statement can't be processed if one of the earlier variables don't exist.
Instead of:
if(this.pos.order.id)
You should ensure the early variables exist, like so:
if(this.pos && this.pos.order && this.pos.order.id)
If pos or pos.order is not set, it should stop evaluating the if statement and you should no longer receive an error.

Memory allocation of string literal in c

I am having a strange issue with memory allocation in c, the file is fairly complicated so I cannot include it all here but perhaps you can point me in the right direction as to why this may be happening.
I am trying to create a string literal as such:
char * p = "root"
But when i look at the value of this variable at runtime (at the line directly after the declaration) i get this:
$1 = 0x7001260c "me"
and when I look at the contents of the memory at 0x7001260c it indeed holds the string "me".
EDIT:
To give more context when I run the following code the value of p on the last line is "root".
create_directory("root/home");
char * p = "root";
char * q = "foo";
And when I run the following code the value of p is "io"
create_directory("io/home");
char * p = "root";
char * q = "foo";
The create_directory function:
void create_directory(char * path) {
directory d;
directory * dir = &d;
//Browse to closest directory
path = find_directory(path, dir);
//Create remaining directories
char component[20];
path = next_component(path, component);
while (strlen(component) > 0) {
add_dir_entry(dir, component, inode_next);
write_dir_entry(dir, inode_to_loc(dir->inode));
directory new;
new.type = DIRECTORY;
new.inode = inode_next;
write_dir_entry(&new, inode_to_loc(inode_next));
inode_next++;
dir = &new;
path = next_component(path, component);
}
}
Almost certainly, there's a bug somewhere in your program that causes a constant to be modified which is, of course, illegal. Perhaps you're doing something like this:
void to_lower(char *j)
{
while (*j != 0) { *j = tolower(*j); j++; }
}
...
bool is_yes(char *k)
{
to_lower(k);
return strcmp(k, "yes") == 0;
}
void someFunc(char *k)
{
if (is_yes(k)) // ...
...
}
someFunc("testing");
See what this does? We pass a pointer to a constant to sumeFunc, but it flows down to to_lower which modifies the thing it points to -- modifying a constant.
Somehow, your code probably does something like that.
Start by changing code like char * p = "root" to code like char const* p = "root". That will give you a better chance of catching this kind of problem at compile time.

Why this function return an (owned) value?

the code
from: Genie howto repeat a string N times as an string arrayGenie howto repeat a string N times as an string array
def repeatwithsep (e: string, n: int, separator: string): string
var elen = e.length;
var slen = separator.length;
var a = new StringBuilder.sized ((elen * n) + (slen * (n - 1)) + 1);
for var i = 0 to (n - 1)
if i != 0
a.append_len (separator, slen)
a.append_len (e, elen)
return (owned) a.str
var a is a local variable, when a goes out of scope, it will be destroyed.
why this function
return (owned) a.str
what is the difference between
return a.str
return (owned) a.str
what is the benefit of (owned)
return a.str will make a copy of the string using g_strdup, because by default the function result and the StringBuilder will both own a separate copy of the string after the (implicit) assignment.
Since the StringBuilder stored in a will go out of scope and it's copy will thus never be used again this is not desireable / efficient in this case.
Hence the solution is to pass ownership of the string from a.str to the result of the function using the (owned) directive.
BTW: You can easily find this out by compiling both versions with valac -C and comparing the generated C code:
- _tmp21_->str = NULL;
- result = _tmp22_;
+ _tmp23_ = g_strdup (_tmp22_);
+ result = _tmp23_;
(In this comparison the left side was return (owned) a.str and the right side was return a.str)
PS: This is documented in the ownership section of the Vala tutorial and also the corresponding part of the Genie tutorial.
I would also recommend the Reference Handling article.

Slow Scala assert

We've been profiling our code recently and we've come across a few annoying hotspots. They're in the form
assert(a == b, a + " is not equal to " + b)
Because some of these asserts can be in code called a huge amount of times the string concat starts to add up. assert is defined as:
def assert(assumption : Boolean, message : Any) = ....
why isn't it defined as:
def assert(assumption : Boolean, message : => Any) = ....
That way it would evaluate lazily. Given that it's not defined that way is there an inline way of calling assert with a message param that is evaluated lazily?
Thanks
Lazy evaluation has also some overhead for the function object created. If your message object is already fully constructed (a static message) this overhead is unnecessary.
The appropriate method for your use case would be sprintf-style:
assert(a == b, "%s is not equal to %s", a, b)
As long as there is a speciaized function
assert(Boolean, String, Any, Any)
this implementation has no overhead or the cost of the var args array
assert(Boolean, String, Any*)
for the general case.
Implementing toString would be evaluated lazily, but is not readable:
assert(a == b, new { override def toString = a + " is not equal to " + b })
It is by-name, I changed it over a year ago.
http://www.scala-lang.org/node/825
Current Predef:
#elidable(ASSERTION)
def assert(assertion: Boolean, message: => Any) {
if (!assertion)
throw new java.lang.AssertionError("assertion failed: "+ message)
}
Thomas' answer is great, but just in case you like the idea of the last answer but dislike the unreadability, you can get around it:
object LazyS {
def apply(f: => String): AnyRef = new {
override def toString = f
}
}
Example:
object KnightSpeak {
override def toString = { println("Turned into a string") ; "Ni" }
}
scala> assert(true != false , LazyS("I say " + KnightSpeak))
scala> println( LazyS("I say " + KnightSpeak) )
Turned into a string
I say Ni
Try: assert( a==b, "%s is not equals to %s".format(a,b))
The format should only be called when the assert needs the string. Format is added to RichString via implicit.

Resources