Smart way to store a function during execution - python-3.x

I have a function that I modify frequently. I want to be able to store the exact version of the function used during execution and then use that to recreate the result. Is there a smart way to do that?

Related

Automatically convert uuids to url safe strings using node-pg

I use uuid for just about every ID in my REST backend powered by node and postgres. I also plan to use validate.js to make sure the queries are formatted correctly.
In order to shorten the URLS for my application, I would like to convert all UUIDS used by my backend into URL safe strings when exposed to the REST consumer.
The problem is that, as far as I can tell there is no such setting within node-pg. And node-pg usually returns the query results as JSON objects using either strings or numbers. That makes it hard to autmatically convert them.
I could of course just go through every single rest endpoint and add code that automatically converts all the types where I know a UUID would be. But that would violate DRY and also be a hotbed for bugs.
I also could try to automatically detect strings that look like UUIDs and then just convert them, but that also seams like it may introduce lots of bugs.
One ideal solution would be some sort of custom code injection into node-pg that automatically converts uuids. Or maybe just some pg function I could use to automatically convert the uuids within the pg-queries themselves (although that would be a bit tedious).
Another Ideal solution might be some way to use validate.js to convert the outputs and inputs during the validation. But I don't know how I could do this.
So basically, what would be a good way to autmatically convert uuids in node-pg to url safe (shorter) strings without having to add a bit of code to every single endpoint?
I think this is what I want: https://github.com/brianc/node-pg-types
It lets me set custom converters for each datatype. The input will probably still have to be converted manually

When do I use a callback function in JavaScript programming

I have a very simple app, that mainly collects data from the native ‘os’ nodejs module and then write to the DOM. I don’t really care which function updates the page first or last, and where my functions do not rely on one another, do I need to use callback functions?
The underlying noob question I have is, “when exactly do I need to use callback, vs when I don’t?”
Is there a millisecond time benchmark, where if a function is going to take longer than n seconds, then use a callback?

Mongodb pass code to be executed on the server during query

I am using mongodb and what I am trying to do is query a collection, and receive well filtered and ordered and projected result. Sadly the logic I want to implement is complex and even if we assume that it is possible to use db.collection.aggregate it will result in long, complex, hard to read aggregate descriptor, which I believe in most cases is unwanted.
So I was thinking - Mongodb understands javascript, therefor most likely I can pass a javascript function during the query itself, expecting that my mongo server will make the query, run the provided function passing the query result to it, then return the final result to me. Something like:
db.collection.find(myQuery, serverCallback).toArray(function(err, db) { ... });
Sadly it seems this is impossible. Investigating further I reached stored javascript and understood that I can define that serverCallback on the server instead of passing it. Which is good, but seems messy and wrong to me. So basically this is the reason, why i decided to ask here if someone with better mongodb experience can argument this approach.
[My understandings]
I beleive that not every case of filtering, aggregating, etc. can be achieved with db.collection.aggregate, which is pretty normal. For all the cases that need special way of filtering the query result, we have two options - to define stored javascript that we execute on the query result on the mongo server, or to fetch the information from the server and do the processing/filtering/etc. in the client.
If we choose to define stored javascript, it is very likely that we will define some project specific logic into the mongo server. I think that the project specifics should always belong to the project code instead of the database. That way we can version them with git and easily access them if we want to change them.
If we choose to apply the aggregation logic after the query we loose the capability to choose who will make the calculations - the server or the client. Which may be an opinion that we want to have.
[My question]
What is the reasoning behind not allowing serverCallback to be provided during the query? I believe that there must be reasons that I do not understand here.
[Edits]
First I want to say that I have resolve my problem and as it was way too complex to explain it easily. I will prefer to stick to something easier to explain and understand. I believe this example of MongoDB stored javascript provides great example so lets use it. Basically what i tried to ask above was is there a way to pass this sum function during db.collection.find (and why there isn't any). Something like this:
function sum(queryResultAsArray) {
//Do whatever we want with queryResultAsArray
//For the example we filter result rows with x + y == 6;
return queryResultAsArray.filter(function(row) {
return row.x + row.y == 6
});
}
db.test.find({}, queryResultAsArray);
And this to be equal to the examples:
db.test.find({$where: "sum(this.x, this.y) == 6"});
For reasoning on why would one can prefer passing function rather than stored javascript see the original post.

Skip function invocation and return value with custom function filter

I am wanting to share some logic between multiple functions which now seems to be partially achievable by using Function Filters within azure functions however in certain cases I don't want to continue execution of the function and be able to return a result back to the binding provider (same as returning a result from the function it's self).
Taking a look at the code the only way to short circuit the calling of the inner IFunctionInvoker within FunctionInvocationFilterInvoker is to throw an exception inside the custom invocation filter. The only problem with this approach is that the exception will bubble up to the host and cause a failure/retries etc.. somewhere else.
Also taking this approach doesn't allow me to set another return value back binding provider. I did see that we could wrap this in our own IFunctionInvoker but I was wondering if there was a better way to achieve this?
Code executing filters
Not possible yet, but this is something we'll definitely enable. We're tracking this capability in our repo here.

Scope of allowed functions/macros in Clojure

I want users to be able to submit a code to a server where it would be executed. In order to secure it, I want to specify a list of functions and macros that are approved - a user execution scope. So I am wondering whether something like this is possible in clojure. Is there any easy way how to do it or are there any libraries that help with it?
My first idea was to iterate over the submitted code snippet and check that all the symbols in there are actually allowed. But then I realized that one can easily turn a string or anything into a symbol using a code. So this approach isn't the best in general.
You should probably look at stuff like Clojail.

Resources