Find probability distribution given constraints?

Question: I am looking at the following problem. I have a function $f(x)$ with support on $[0, \infty)$. Furthermore, $f(x)$ is bounded between 0 and 1, monotonically increasing and concave everywhere.
Separately, there is a r.v. $X$ with some unknown density $p(x)$ which is also defined on $[0, \infty)$. We know that $E[X]=m$.
I would like to maximize $E[f(X)]$. Clearly it is bounded by $f(m)$ regardless of the distribution of $X$ because of Jensen's inequality. But can I do better (obtain a lower upper bound) by finding a specific pdf for $X$ which maximizes $E[f(X)]$ subject to the constraints on $X$? Something along the lines of how the exponential distribution has maximum entropy for the class of distributions with support on the positive real line and known mean, except instead of entropy I am maximizing the expectation of $f(x)$. Any ideas or direction would be much appreciated. Thanks!
Answer: Wouldn't the distribution that just puts all the mass at $m$ give you $E[f(X)] = f(m)$?  (Or if you don't want to have a mass point, you could come arbitrarily close to this by putting almost all the mass arbitrarily close to $m$.)


How do I calculate the accuracy of answers to my number based quiz?

Question: I'm making a number based quiz. All answers are integers, and participant are given a score between 0 and 100 based on accuracy. If you're far off the mark, you should get zero points, if you are correct, you get 100 points.
(Sidenote: I tried a version where the goal is to get the least amount of negative points, and the score was simply the absolute difference between the correct and given answer. According to my family, this is "not fun" and "weird".)
So, I'm looking for a function that takes to arguments, the correct answer and the guessed value.
Consider the question: what is the temperature in Celsius on the surface of the Sun? The correct answer is 5505C. If the user guesses 5505, they get 100 points, and the further away from 5505 their answer is, the less points the should get.
So far I have tried the following function f, where g is the guessed answer, and c is the correct answer.
<code> f(g,c) = (1-(|g-c|)/c)*100
</code>
This has a number of problems. Consider the cases where the correct answer is negative (like the absolute zero temperature, -273), or when the answer is zero (which year was Jesus born?).
(And it might be out of scope, but consider the case where the correct answer is a year. I think this should affect scoring somehow, you would expect better accuracy the closer the correct answer is to the current year. E.g. most people could be expected to know within a decade when the Soviet Union fell, but not so for when the siege of Carthage occured.)
(Also, I'm not sure which fields of math this touches on. But I suppose it can be considered a geometry problem as I'm trying to figure out the "distance" between two points.)
Comment: Also consider this, if the question is: How many planets are there in the universe? I'm pretty sure everyone would get a $0~\%$ score on this, because we're looking at a logarithmic scale and it's a challenge to know how many digits there even are in the number ... So instead, maybe it would be better to just give three or four options to choose from?
Comment: This is still a matter of design. If you have questions where even the magnitude of the answer is difficult to determine, you may want a logarithmic scale. Since you said you want to reward educated guesses, I'm guessing you want to reward consistently accurate estimates, so you want a bell curve type shape, where the peak of the function is wider. There need to be more specifics for us to help you more meaningfully. What are the rules of this quiz? Who *should* win the game? What kinds of questions will you ask?
Comment: This is a soft question because there are many functions that can be justified depending on what properties are desired. Can you elaborate on the purpose of this quiz? Is this to test the number sense of a class of students, to create a game out of trivia questions? Are you expecting a single winner, or do you want everyone who approximated well to have relatively equal point values? How strongly would you like to reward someone for getting highly accurate answers either by trivia recall or dumb luck?
Comment: Yeah, my purpose here is actually to learn the web application framework ASP.NET Core, and I've had this idea for a more fun quiz for some time. The problem with traditional quiz questions is that the answers are trivia that you usually can't reason about. I want to make a number based quiz where people can make educated guesses, and be rewarded for it.
Comment: Jesus cannot be born in year $0$, because year $0$ never existed...
Answer: Without more specifications, I cannot give you a particularly good answer for your exact needs, but I will at least outline my interpretation of a scoring function, and how my own specifications dictated my choice, as an example that you can either take or adapt yourself.
What I am personally looking for:

the scoring rewards educated guesses, not happening to know the exact answer by trivia recall
the scoring can handle all kinds of questions with numerical answers, as long as particular parameters are given
after many rounds, the person with the most consistent educated estimates should win

Base Function
Let's say the answer to a question is $100$. I want the person who guessed $96$ to have roughly the same score as the person who guessed exactly $100$ because if they managed to get $96$ by an educated guess, they got pretty close and should be rewarded. If we draw out this kind of function, it should look something like this:

For me, this screams "bell curve", so I will be using this as the basis of my scoring function.
Our base scoring function is
$$
f(d) = 100e^{-s^2d^2}
$$
where $d$ is the difference between the guessed answer and correct answer. The $s$ variable is a strictness factor. The higher $s$ is, the closer you have to be to the actual answer to get the same amount of points. We square $s$ because this makes it proportional to the reciprocal of the standard deviation, which makes the parameter feel a bit more intuitive. For example, the jump from $s=4$ to $s=7$ should not be the same "strictness" jump from $s=7$ to $s=50$.
This leads into our first question case:

Uniform Distribution
Let's say that the possible answers to a particular question range from $0$ to $100$, and there is a perception that any of those answers are equally likely. For example, "How old is Mary Jane?" Without knowing any specifics and randomly guessing, any integer is equally likely to be the answer. These are questions with answers we will describe as "uniformly distributed".
Our strictness depends on how big the range of values is. If our answers are from $0$ and $100$, then it is reasonable to guess within $10$ of the correct answer. If our answers are from $0$ and $1,000,000$, then it is unreasonable to guess within $10$ of the correct answer. Therefore, we need to scale our strictness depending on how big our answer range is. We will call this range $r$.
Uniform score function:
$$
f(x) = 100e^{\left(-\left(\frac{40s^2}{r^{2}}\right)\left(x-c\right)^{2}\right)}
$$
where $x$ is the guess, $c$ is the correct answer, and $r$ is the range of answers (or at least a decent approximation of the range if it is unbounded on both ends).
The $(x-c)$ is just a translation that puts it on top of the correct answer. Dividing the strictness by a factor of $r^2$ makes sure that the scaling is properly adjusted to the size of the range. The $40$ factor is very arbitrary, and is used here solely to put the strictness value in line with the next scoring function I am about to use.

Logarithmic Distribution
What if the question was such that even the magnitude of the answer is difficult to determine? For example, "How many atoms are there in the universe?" Essentially, we have a starting point, a minimum amount for an answer, but the sky's the limit when guessing the proper answer here.
We want to say that being under by $5$ magnitudes should be given the same score as being over by $5$ magnitudes. If the answer is $10^{40}$, then $10^{35}$ and $10^{45}$ should be given the same score. We update our scoring function by taking the difference of magnitudes rather than the difference of the pure values. This is why we will call the answers to these kinds of questions "logarithmically distributed".
We get something along the lines of:
$$
f(x) = 100e^{\left(-s^2\left(\ln\left(c-m\right)-\ln\left(x-m\right)\right)^{2}\right)}
$$
where $x$ is the guess, $c$ is the correct answer, and $m$ is the minimum possible answer.
However, there's a major problem with this function. If we are talking about an answer on the scale of $10^6$, it may be reasonable to expect the answer to be off by one magnitude (between $10^5$ and $10^7$). However, if our answer is in the range of $10^{100}$, then it may be unreasonable to expect the answer to be only off by one magnitude (between $10^{99}$ and $10^{101}$). We expect that if the magnitude of the answer is $p$, then the amount of magnitudes the answer is off by to get the same score should be proportional to $p$.
Logarithmic score function:
$$
f(x) = 100e^{\left(-\left(\frac{10s^{2}}{\left(c-m\right)^{0.1}}\right)\left(\ln\left(c-m\right)-\ln\left(x-m\right)\right)^{2}\right)}
$$
where $x$ is the guess, $c$ is the correct answer, and $m$ is the minimum possible answer.
We divide by $(c-m)^{\frac{1}{10}}$ to get the proper variance in magnitude we expect from our answer. The factor of $10$ is once again arbitrary, but now we can use the same strictness factor $s$ on both of our scoring functions and expect roughly the same score for the same strength of guesses.
If the answer does not have a minimum, but rather only has a maximum, it is rather trivial to just mirror flip the function.

Summary
We have achieved our goals with our scoring functions, while being able to tune the strictness of the score, and being able to apply our function to any question with a numerical answer, regardless of whether it is a uniformly distributed answer or a logarithmically distributed answer, and we are also able to translate the function regardless of where the answer range begins, even if it is negative.
Uniformly distributed answer:
$$
f(x) = 100e^{\left(-\left(\frac{40s^2}{r^{2}}\right)\left(x-c\right)^{2}\right)}
$$
where $x$ is the guess, $c$ is the correct answer, and $r$ is the range of answers (or at least a decent approximation of the range if it is unbounded on both ends).
Logarithmically distributed answer:
$$
f(x) = 100e^{\left(-\left(\frac{10s^{2}}{\left(c-m\right)^{0.1}}\right)\left(\ln\left(c-m\right)-\ln\left(x-m\right)\right)^{2}\right)}
$$
where $x$ is the guess, $c$ is the correct answer, and $m$ is the minimum possible answer.
A specific range of $s$ I would recommend is from $1$ to $5$. If this is more of a family game and you would like everyone to get a decent chunk of points with each question, lean towards $1$. If you want the answers to be more strict and the game to be more competitive, I would lean towards $5$.
If you don't like decimals, you could always just treat each of these with a floor/ceiling function. You can play around with these functions here.
Comment: This is fantastic, thank you! :)
Answer: The fact that you use a function, makes it seem a computer program is in use. Therefor you could use something like an absolute hyperbolic tangent to bound your points needly, without worrying about negative or zero values for c. This way, you could also easily make an accuracy component, which adresses your latter point. It could look something like this:
$$ score = 100\,(1-|\tanh((g-c)\cdot a)|) $$
For you to work out what the correct values of the accuracy a has to be to make the scores make sense.
In the case that you don't use a computer, then you are going far beyond what regular people would do with pen and paper when playing a quiz game.
Answer: I would suggest that having one function for all questions is a mistake.  If you ask the temperature of the sun, nobody should be expected to get the value you have in mind, as there are many ways to define it.  If you ask what Fahrenheit temperature water freezes at, any answer other than $32$ is badly wrong.  If you ask the population of the world, it is changing with time.  You need to think about what is close on each question.


What are terms and functions in the first order logic?

Question: I am trying to learn the syntax of the first order logic. On Wikipedia I see these rules:

Any variable is a term.
Any expression $f(t_1,...,t_n)$ of $n$ arguments (where each argument $t_i$ is a term and $f$ is a function symbol of valence $n$) is a term.

I understand variables as references to specific objects (for example Berlin, USA, John, Marry, Jupiter). I also understand functions as something that get variable as inputs and generate set of variables. For example: ChildrenOf(John, Marry) will returns a set of children of John and Marry and this set, according to the rule 2, is a term by itself. So, it can be used as an argument of a function. But how then the following function should be interpreted:
ChildrenOf(ChildrenOf(John, Marry), ChildrenOf(Bill, Anna))

In other words, how should I evaluate functions if we use sets as their arguments? I can assume that in the above example the function should return all the people for whom the one of the parents is offspring of John and Marry and another parent is offspring of Bill and Anna but I am not sure that this is correct since it is not stated explicitly on Wikipedia.
Comment: Where are you taking this example from? is this some introduction to set theory?
Answer:
I also understand functions as something that get variable as inputs and generate set of variables.

That's incorrect: semantically, a function takes in a tuple of values and outputs a single value. This is present in the definition of a structure, where each function symbol is interpreted as a literal single-valued function on that structure. So we don't run into the problem you're worried about at all, and composition of functions is perfectly benign.
It's also not accurate to say that functions output variables: variables are specific types of symbols, and a string like "$f(x_1,x_2)$" is simply not a symbol of that type. But this is really a side issue.

Separately, a bit of intuition which might be helpful: you should think of a term as a noun, possibly a vague noun in case variables are involved (until we pick a variable assignment such a term doesn't pick out a specific element of a given structure) but a noun nonetheless. A function symbol by contrast is part of a noun phrase which begins with the word "the" to reflect its single-valuedness (e.g. "the smallest prime number greater than ---").


Differentiability of $\Psi(x) = \frac{1}{\phi(x)}$, $\phi: G \rightarrow \mathbb{R}$, $G \subset \mathbb{R}^n$?

Question: How to infer the differentiability of
$$\Psi(x) = \frac{1}{\phi(x)}$$
where $\phi: G \rightarrow \mathbb{R}$, $G \subset \mathbb{R}^n$.
Can this be done with the one-dimensional definition of derivative, i.e.
$$\lim_{h \rightarrow 0} \frac{f(a+h)-f(a)}{h}$$
since $\phi (x) \in \mathbb{R}$?
Answer: Let $\theta: t \in \Bbb R \setminus \{0\} \mapsto \frac1t$. Then $\psi = \theta \circ \phi$, and provided $\phi$ is never zero, i.e. that $\phi(G) \subset \Bbb R \setminus \{0\}$, we get that $\theta$ is differentiable on $\phi(G)$, hence $\psi$ is differentiable on $G$ and by chain rule,
$$D \psi(x) = \theta'(\phi(x)) D \phi(x) = -\frac1{(\phi(x))^2} D \phi(x)$$
Comment: Could you elaborate on why you get that $\theta$ is differentiable?
Comment: @mavavilj well $\theta$ is differentiable on $\Bbb R \setminus \{0\}$, so it is differentiable on any set contained in $\Bbb R \setminus \{0\}$ (in particular on $\phi(G)$)
Comment: My notes don't use the gradient. Is $\nabla \Psi(x) = D \Psi(x)$? Or is it equal to $D \Psi(x) \cdot u$?
Comment: @mavavilj rather, it is the transpose. I'll edit the answer.


Nested command substitutions in a makefile

Question: I have a pretty basic makefile where I want to use the name of the current project / folder, but I keep having problems.  I've tried:
example:
  docker build -t `basename `pwd`` .
  docker build -t `basename $(pwd)` # From what I know $() relates to a variable in makefiles.

Any ideas?
Answer: One approach:
docker build -t `basename $(PWD)`

This, like many environment variables, is all uppercase.
As was pointed out in the comments, this:
docker build -t $$(basename $$(pwd))

will also work.  This is more akin to your first attempt, where the pwd command is used rather than the PWD environment variable.
And, this will also work:
docker build -t `basename \`pwd\``

Comment: This doesn't answer the (apparent) question it the title, though.If you actually wanted to nest command substitutions, you need to double the dollar signs in order to pass them through to the shell; `$$(basename $$(pwd))`
Comment: Wow... I tried this before but the make output shows up as:
```
docker build -t `basename /home/aosmith/Development/project` .
```

turns out this actually works, I just wasnt paying attention thanks!
Comment: $$ solved my issue for this, I guess it's like a Makefile escape char for command substitution.


How can I derive the integral like this one?

Question: I have a problem with integral like this one:
$$\int_{0}^{2\pi} \frac{\text{d}x}{\cos x+a}$$
when $a$ is from $1$ to $+\infty$.
Could anyone help me please?
Comment: The image is itself from Integral Calculator. I doubt if it's not solved there.
Comment: Tried [Wolfram Alpha](https://www.wolframalpha.com/input/?i=Integrate%5B1%2F%28a%2BCos%5Bx%5D%29%5D)?
Comment: @gt6989b, well yes.  You can also give "Alpha" a definite integral.
Comment: @gt6989b, In *Mathematica* it is `Assuming[a>1,Integrate[1/(a+Cos[x]),{x,0,2*Pi}]]`.  Doesn't seem that WolframAlpha can handle this syntax.  Any ideas how to get it to evaluate this?
Comment: Doesn't cost much to say that the integral is asymptotic to $\frac{2\pi}a$.
Comment: Do not deface your posts.
Answer: The constant $a$ must be strictly greater than $1.$
In that case, let $z=e^{i x}$ and $\cos x = \frac{1}{2}( e^{i x}+e^{-i x}) = \frac{1}{2} (z+1/z)$, etc.
After evaluating the integral by computing the residue at the pole inside the contour, you should obtain:
$$\int_0^{2\pi} \frac{dx}{a+\cos x} = \frac{2\pi}{\sqrt{a^2-1}}$$
Answer: Another approach writes $2\int_0^\pi\frac{dx}{\cos x+a}$ with $t=\tan\tfrac{x}{2}=\sqrt{\tfrac{a+1}{a-1}}\tan y$ as$$\int_0^\infty\frac{4dt}{a+1+(a-1)t^2}=\int_0^{\pi/2}\frac{4dy}{\sqrt{a^2-1}}=\frac{2\pi}{\sqrt{a^2-1}}.$$
Answer: A common method for integrals of this form is to use the Weierstrass substitution which transforms the integral as so:
$$t=\tan(x/2)$$
$$\cos x=\frac{1-t^2}{1+t^2}$$
$$dx=\frac{2}{1+t^2}dt$$
you may also want to consider changing your domain
Answer:
$$\int_0^{2\pi} \frac{\text{d}x}{a + \cos(x)}$$

Stepping into the complex plane:
$$z = e^{ix} ~~~~~~~ \cos(x) \to \frac{1}{2}\left(z + \frac{1}{z}\right) ~~~~~~~ \text{d}x = \frac{\text{d}z}{iz}$$
The integral becomes
$$\int_{|z|=1} \frac{\text{d}z}{iz\left(a + \frac{1}{2}\left(z + \frac{1}{z}\right)\right)}$$
Now we arrange it into
$$ \frac{2}{i} \int_{|z|=1} \frac{dz}{z^2 + 2az + 1}
= \frac{2}{i} \int_{|z|=1} \frac{dz}{(z-z_1)(z-z_2)}$$
where the circle $|z|=1$ is parametrized counterclockwise and
\begin{align*} z_1 = -a - \sqrt{a^2-1}  && z_2 = -a + \sqrt{a^2-1}.
\end{align*}
are the poles of the integrand function.
Clearly $z_1 < -a < -1$, so $z_1$ is outside of the unit circle. As for $z_2$, it is convenient to notice that
\begin{align*}
z_1 z_2 = 1.
\end{align*}
Thus, since $z_1$ is outside the unit circle, its inverse $z_2$ is inside the unit circle. So, applying the residue theorem, we have
\begin{align*}
\frac{2}{i} \int_{|z|=1} \frac{dz}{(z-z_1)(z-z_2)}
&= \frac{2}{i} \ 2 \pi i \ \mathrm{Res}\left( \frac{1}{(z-z_1)(z-z_2)}, z_2\right) \\
&= 4 \pi \frac{1}{z_2 - z_1} \\
&= 4 \pi \frac{1}{2 \sqrt{a^2 -1}} \\
&= \boxed{\frac{2 \pi}{\sqrt{a^2-1}}}
\end{align*}


Tightness of a finite measure on a complete and separable metric space

Question:
Let $(X, \rho)$ be a metric spac and let $\mu$ be a finite Borel measure on it. Assume thay $(X, \rho)$ is complete and separable. Prove that for every $\epsilon > 0$ there is a complact subset $K \subset X$ such that $\mu(K^c) < \epsilon$.

I am not sure what to do here. We are to use that a subset of a complete metric space is compact iff it is closed and totally bounded. I managed to prove that for any $\delta > 0$ there is a finite subset $\{x_1,...,x_n\}$ such that $\mu((B(x_1,\delta) \cup... \cup B(x_n,\delta))^c) < \epsilon$. It seems like I have to take something like closure, but I am not sure how.
Answer: For each $n$ take $\delta =\frac 1 n$ and replace $\epsilon$ by $\epsilon /2^{n}$ in your argument to get a finite set $\{x_{n1},x_{n2},...,x_{nk_n}\}$. Consider $H=\cap_n [B(x_{n1},\frac 1 n)\cup B(x_{n2},\frac 1 n),...,\cup B(x_{nk_n},\frac 1 n)]$ This set is totally bounded and hence its closure $K$ is compact. The measure of $H^{c}$ is less than $\epsilon$. Hence $\mu(K^{c}) <\epsilon$.


Answer improvement comments below answer -1

Question: Why user who down-votes the answer and adds comment bellow the answer, how he/she thinks the answer can be improved, would also give away one of his/her points?
Main question:
What do to when the user answers to his/her post himself, but the accepted answer is not an answer. Instead the answer is just a general speculation what is wrong and does not give anything to other forum members? Can anyone explain me please if the answer should be down voted or flagged? example
(Before down-voting current question please answer, what did you lear from the example by comment?)
Comment: give  example then we can surely tell you what is the issue
Answer: For this problem I think you can do down voting if you want to do so ,
you can not force someone to provide full detail answer because here question also asked by that person.
in this kind of case you can leave comment to OP that provide full answer but if he/she don't want to answer you can not do more except flag that post as  low quality Post, if community (Reviewer) feels the same answer would be deleted


Loading a Roslyn compiled assembly into a sandbox AppDomain

Question: I've got a code-snippet that compiles a script with the script engine and I retreiv the assembly as a byte array.
Now I want to load this Assembly in a Sandbox, this is what I've got:
Assembly _dynamicAssembly;
ScriptEngine _engine;
Session _session;

public string Execute(string code)
{
    // Setup sandbox
    var e = new Evidence();
    e.AddHostEvidence(new Zone(SecurityZone.Internet));
    var ps = SecurityManager.GetStandardSandbox(e);
    var setup = new AppDomainSetup
                         { ApplicationBase = Environment.CurrentDirectory };
    var domain =
        AppDomain.CreateDomain("Sandbox",
                               AppDomain.CurrentDomain.Evidence, setup, ps);
    AppDomain.CurrentDomain.AssemblyResolve += DomainAssemblyResolve;

    // Process code
    var submission = _engine.CompileSubmission<object>(code, _session);
    submission.Compilation.Emit(memoryStream);
    var assembly = memoryStream.ToArray();

    _dynamicAssembly = Assembly.Load(assembly);

    var loaded = domain.Load(assembly);

    // Rest of the code...
}

This is the event handler for AssemblyResolve:
Assembly DomainAssemblyResolve(object sender, ResolveEventArgs args)
{
    return _dynamicAssembly;
}

This means that when I do domain.Load(assembly) I will get the _dynamicAssembly, if I don't subscribe to that event and return that Assembly, I get a FileNotFoundException.
The above compiles and runs, but the problem is that code that is executed in the domain-assembly is not actually executed in the sandbox. When I get the submission-method and invoke the factory in it and return this AppDomain.CurrentDomain.FriendlyName the result is: MyRoslynApplication.vshost.exe which is not the sandbox AppDomain
Am I loading my byte[]-assembly wrong?
Answer: If you want to load a type that runs in the sandbox and access it from your main AppDomain, you'll need to use a method like CreateInstanceFromAndUnwrap.  The type will need to be a MarshalByRefObject so that it can create a transparent proxy in the calling AppDomain for access.
If the main AppDomain resolves the assembly, it will be loaded into the main AppDomain (as well as the sandbox AppDomain) so that you end up with two copies loaded.  Your main AppDomain must always remain insulated from the sandbox via proxies, so that only MarshalByRefObject and serializable objects can be accessed.  Note that the type you're referencing also cannot be defined in the assembly you want to load into the sandbox; you'll want to define interfaces and possibly serializable types in a third common assembly, which will then be loaded into both the main and sandbox AppDomains.

I've done some additional digging and it looks like all the methods for loading an assembly into another AppDomain and generating a proxy require an assembly name to resolve.  I'm not sure if it's possible to load via a byte[] in this case; you might need to save the assembly to disk and load that.  I'll dig a bit more.

I think you can do this (this is untested, but it seems plausible).
These would need to be in an 'interface' assembly accessible to both your main app and the sandbox (I'll refer to it as Services.dll):
public interface IMyService
{
    //.... service-specific methods you'll be using
}

public interface IStubLoader
{
    Object CreateInstanceFromAndUnwrap(byte[] assemblyBytes, string typeName);
}

Next is a class in a StubLoader.dll.  You won't reference this assembly directly; this is where you'll call the first AppDomain.CreateInstanceFromAndUnwrap, providing this as the assembly name and StubLoader as the type name.
public sealed class StubLoader: MarshalByRefObject, IStubLoader
    {
        public object CreateInstanceFromAndUnwrap(byte[] assemblyBytes, string typeName)
        {
            var assembly = Assembly.Load(assemblyBytes);
            return assembly.CreateInstance(typeName);
        }
    }

Now, to use it from your main AppDomain, you do this:
//Create transparent proxy for the stub loader, which will live in the sandbox
var stubLoader = (IStubLoader)sandboxDomain.CreateInstanceFromAndUnwrap("Stubloader.dll", "StubLoader");

//Have the stub loader marshal a proxy to a dynamically loaded assembly (via byte[]) where MyService is the type name implementing MarshalByRefObject and IMyService
var myService = (IMyService)stubLoader.CreateInstanceFromAndUnwrap(assemblyBytes, "MyService");

Unfortunately, AppDomains are not simple to use.  This is because they provide a high degree of insulation and therefore require proxying to allow for usage across AppDomain boundaries.

In response to how you could marshal a non-serializable and non-MarshalByRefObject class, here is a rough example of what might be in the shared interface DLL:
public interface ISessionWrapper
{
    void DoSomethingWithSession();
}

public sealed class SessionWrapper : MarshalByRefObject, ISessionWrapper
{
    private readonly Session _session;

    public SessionWrapper(Session session)
    {
        _session = session;
    }

    public void DoSomethingWithSession()
    {
        //Do something with the wrapped session...
        //This executes inside the sandbox, even though it can be called (via proxy) from outside the sandbox
    }
}

Now everywhere your original service needed to work with Session, it can instead pass ISessionWrapper, whose calls will be marshalled behind the scenes so that all of the actual code executes in the sandbox on a real Session instance living in the sandbox.
Comment: @Filip, the only idea coming to me at this moment would be to use some roundabout mechanism whereby the byte[] gets communicated via some cross-process or cross-domain mechanism (such as storing it in Isolated Storage or communicating it over a port), then have a 'stub loader' assembly that you can reference by name.  It would then start up, access the byte[] through this outside medium, then return the wrapped proxy.
Comment: @Filip, I think there is a slightly easier way, as byte[] is serializable, so you won't need to use an outside data transport mechanism.  I've proposed a generic mechanism to work around the fact that the BCL doesn't include a byte[] overload for AppDomain.CreateInstanceFromAndUnwrap.  If you can get this to work and find any issues with what I posted, please go ahead and edit the post.
Comment: @Filip, I think you may need to do some more research on AppDomains and MarshalByRefObject.  Aside from the awkwardness of loading from a `byte[]`, you'll still need to do some design work to figure out how to proxy an interface that adapts your 'script' for use by your application.  At the very least, you need a MarshalByRefObject and some form of interface for your service that's independent of the dynamically loaded assembly.
Comment: @Filip, one thought is that you could create a wrapper MarshalByRefObject that encapsulates the operations you need to perform with the Session, then pass that SessionWrapper to your script.  Now you can construct the SessionWrapper in your main AppDomain, pass it to the method (which marshals it as a proxy to the script), which can then operate with the proxy (where its methods get marshalled back to your main AppDomain for execution.)
Comment: How would that work when the Assembly is not on disk? `CreateInstanceFromAndUnwrap` requires a physical file, no? **Edit** in response to your edit, I can't load from disk since that would require IO Permissions.
Comment: @Filip, would you be able to use some form of isolated storage?  I'm guessing not, since that doesn't allow a file name.
Comment: Wow... really? That seems Way to "complicated" for what I want to do, specially with all this nice roslyn things out there. I really just want to run a "script" in the Script Engine with limited priviligies.
Comment: Well I've come a bit further here, however, I can't create an instance of the type inside the `CreateInstanceFromAndUnwrap` method because there is no public constructor defined. Since this is just a code line that I am executing like this: `var x = 10; x == 10` see outputted IL here: http://privatepaste.com/4cfe2db1ad
Comment: Running this now, got a bit further: `return assembly.CreateInstance("Submission#1", true, BindingFlags.InvokeMethod | BindingFlags.Public, null, new [] {Session.Create()}, new CultureInfo("neutral"), null);` however, this complains about it being security transparent: "In order to access security critical code, this assembly must be fully trusted.". Wouldn't making it fully trusted defeat the purpose?
Comment: @Filip, I hadn't considered how the security permissions fit in; I recommend posting that as a new question.  It's unfortunately outside my expertise.
Comment: Well the above question isn't really solved and I think the permission stuff fits in here since it's most likely what I need to accomplish to get it working.
The problem is that the generated code ( in the byte[] assembly ) uses types that are not serializable, thus, I can't create an instance of it..
Thanks for your help!
Comment: Thanks for the help Dan, your code would work in any other normal circumstance. But when it comes to the way that I did it with roslyn, I can't pass for instance a `Session` down to the other app domain, because it is not serializable and requires that compilation and execution is made in the same app domain.
Comment: Do you think you could write up an example of how that would work?
Comment: Brilliant answer.  You clarified a lot of confusion for me.  The `StubLoader` technique is perfect.
Answer: probably this could also help:
https://learn.microsoft.com/en-us/dotnet/framework/misc/how-to-run-partially-trusted-code-in-a-sandbox
Also I had to persist the created assembly file stream onto the disk to get the Sandbox working. I'm still struggling with Strong naming and unit testing of the solution (there are over 16 projects), however, I'll get back to you with the working copy.


How do I ensure Rails ActiveRecord sort order is applied when using Jquery .append?

Question: I have a controller that returns a queryset like so;
def show
  @docs = Docs.where("id > ?", params[:id]).order(:id).reverse_order
  respond to do |format|
    format.js
  end
end

In the rails console this returns my records in the correct order.
show.js.erb looks like this;
<% unless @docs.empty? %>
  $("#published").append("<%=raw escape_javascript(render(@docs)) %>");
<% end %>

EDIT: I'm using a partial like so and I believe the order is ignored here;
my index.html calls;
<%= render @outline.outline_docs %>

which renders **outline\_outline_doc.html.erb **;
<div class="well" data-doc="<%= outline_doc.id %>">

<%= link_to outline_doc.id, "" %>

  <%= Time.at(outline_doc.created_at.to_i + 1) %>

But instead of the items being displayed in the order specified in the controller (27,28,29,30) they are always displayed as (28,29,27,30). Any ordering specified is always ignored.
How can I ensure that the records retrieved are appended in the desired manner?
Answer: Try this
def show
  @docs = Docs.where("id >", params[:id]).all
  @docs.sort! { |a,b| a.id <=> b.id }
  respond to do |format|
    format.js
  end
end

The primary problem MAY be that the adapter that you use to connect to your DB does not respect ordering
EDIT: Responding to your comment:
Lets try moving the ordering to a view:
<% unless @docs.empty? %>
  $("#published").append("<%=raw escape_javascript(render(@docs.sort! { |a,b| a.id <=> b.id })) %>");
<% end %>

EDIT2: PARTIALS
try rendering your partial like this
<%= render :partial => @outline.outline_docs, :collection => @docs.sort! { |a,b| a.id <=> b.id } %>

Alternatively (if you are sure the order is passed correctly in your existing code) just:
<%= render :partial => @outline.outline_docs, :collection => @docs %>

I didn't see how your outline is directly connected with @docs so you may try using :collections with @docs (if the second render call is the culprit):
$("#published").append("<%=raw escape_javascript(render(:partial => "PARTIAL NAME", :collection => @docs)) %>");
<% end %>

Don't forget to replace PARTIAL NAME with whatever the partial you want to render @docs in or use @docs instead of PARTIAL NAME if you have corresponsing to_partial_path
Comment: This one did it...  `$("#published").append("<%=raw escape_javascript(render(:partial => "PARTIAL NAME", :collection => @docs)) %>");`  ... thanks for your help.
Comment: I've tried that but the order is always the same. I'm sure it's not the adapter because I get the desired results in the console.
Comment: @ObregonCornsnook Edited with additional response, try it
Comment: Thanks. I tried it and noticed that the order is still ignored. Digging deeper I saw that just outputting the results simply gave me the correct order but because I'm using a partial (see edit above) the order is ignored. How do I keep the needed order while using the partial?
Comment: @ObregonCornsnook check EDIT2, maybe this will help in resolving the error, I am not able to test right now so sorry if I relegate it to you :)


Django Pyinstaller .EXE gives me ModuleNotFoundError: No module named 'app.urls'

Question: I am trying to run a Django project using an EXE file compiled using Pyinstaller. But when I run the compiled .EXE file using this command I get ModuleNotFoundError: No module named 'app.urls' and another OsError: [WinError 123]. I will explain how I setup this stuff later.
Here is the full error when I run the .EXE file:
C:\Users\omen\Desktop\dist\dj>dj.exe runserver
Watching for file changes with StatReloader
Performing system checks...

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "threading.py", line 917, in _bootstrap_inner
  File "threading.py", line 865, in run
  File "site-packages\django\utils\autoreload.py", line 54, in wrapper
  File "C:\Users\omen\Desktop\dist\dj\django\core\management\commands\runserver.py", line 117, in inner_run
    self.check(display_num_errors=True)
  File "site-packages\django\core\management\base.py", line 390, in check
  File "site-packages\django\core\management\base.py", line 377, in _run_checks
  File "site-packages\django\core\checks\registry.py", line 72, in run_checks
  File "site-packages\django\core\checks\urls.py", line 40, in check_url_namespaces_unique
  File "site-packages\django\core\checks\urls.py", line 57, in _load_all_namespaces
  File "site-packages\django\utils\functional.py", line 80, in __get__
  File "site-packages\django\urls\resolvers.py", line 584, in url_patterns
  File "site-packages\django\utils\functional.py", line 80, in __get__
  File "site-packages\django\urls\resolvers.py", line 577, in urlconf_module
  File "importlib\__init__.py", line 127, in import_module
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "C:\Users\omen\Anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
    exec(bytecode, module.__dict__)
  File "dj\dj\urls.py", line 22, in <module>
  File "site-packages\django\urls\conf.py", line 34, in include
  File "importlib\__init__.py", line 127, in import_module
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'app.urls'

Traceback (most recent call last):
  File "dj\manage.py", line 24, in <module>
  File "dj\manage.py", line 20, in main
  File "site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
  File "site-packages\django\core\management\__init__.py", line 375, in execute
  File "site-packages\django\core\management\base.py", line 323, in run_from_argv
  File "C:\Users\omen\Desktop\dist\dj\django\core\management\commands\runserver.py", line 60, in execute
    super().execute(*args, **options)
  File "site-packages\django\core\management\base.py", line 364, in execute
  File "C:\Users\omen\Desktop\dist\dj\django\core\management\commands\runserver.py", line 95, in handle
    self.run(**options)
  File "C:\Users\omen\Desktop\dist\dj\django\core\management\commands\runserver.py", line 102, in run
    autoreload.run_with_reloader(self.inner_run, **options)
  File "site-packages\django\utils\autoreload.py", line 598, in run_with_reloader
  File "site-packages\django\utils\autoreload.py", line 583, in start_django
  File "site-packages\django\utils\autoreload.py", line 301, in run
  File "site-packages\django\utils\autoreload.py", line 307, in run_loop
  File "site-packages\django\utils\autoreload.py", line 347, in tick
  File "site-packages\django\utils\autoreload.py", line 363, in snapshot_files
  File "site-packages\django\utils\autoreload.py", line 262, in watched_files
  File "site-packages\django\utils\autoreload.py", line 103, in iter_all_python_module_files
  File "site-packages\django\utils\autoreload.py", line 139, in iter_modules_and_files
  File "pathlib.py", line 1339, in exists
  File "pathlib.py", line 1161, in stat
OSError: [WinError 123] La syntaxe du nom de fichier, de répertoire ou de volume est incorrecte: '<frozen importlib._bootstrap>'
[11168] Failed to execute script manage

First I installed Pyinstaller and other dependencies using the commands:
conda install -c conda-forge pyinstaller
conda install django
conda install -c conda-forge gdal
conda install -c anaconda psycopg2

Then I installed OSGeo4W64 on my Windows 10 machine for GDAL.
I added this in settings.py:

import os

if os.name == 'nt':
    import platform
    OSGEO4W = r"C:\OSGeo4W"
    if '64' in platform.architecture()[0]:
        OSGEO4W += "64"
    assert os.path.isdir(OSGEO4W), "Directory does not exist: " + OSGEO4W
    os.environ['OSGEO4W_ROOT'] = OSGEO4W
    os.environ['GDAL_DATA'] = OSGEO4W + r"\share\gdal"
    os.environ['PROJ_LIB'] = OSGEO4W + r"\share\proj"
    os.environ['PATH'] = OSGEO4W + r"\bin;" + os.environ['PATH']

os.environ['DJANGO_SETTINGS_MODULE'] = 'dj.settings'

SETTINGS_PATH = os.path.dirname(os.path.dirname(__file__))

TEMPLATE_DIRS = (
    os.path.join(SETTINGS_PATH, 'templates'),
)

GEOS_LIBRARY_PATH = r'C:\OSGeo4W64\bin\geos_c.dll'
GDAL_LIBRARY_PATH = r'C:\OSGeo4W64\bin\gdal204.dll'

I also generated a spec file using this command:
pyi-makespec dj/manage.py

And this is the generated manage.spec:
(I added in datas the templates folder but it didn't work...)
# -*- mode: python ; coding: utf-8 -*-

block_cipher = None

a = Analysis(['dj\\manage.py'],
             pathex=['C:\\Users\\omen\\Desktop'],
             binaries=[],
             datas=[('app/templates','app/templates')],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          [],
          exclude_binaries=True,
          name='manage',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               upx_exclude=[],
               name='manage')

Then I generated the project using this command:
pyinstaller --name=dj dj/manage.py

Also I edited libgdal.py to have the following:
# Custom library path set?
try:
    from django.conf import settings
    lib_path = settings.GDAL_LIBRARY_PATH
except (AttributeError, ImportError, ImproperlyConfigured, OSError):
    lib_path = None

Here is my main urls.py:
from django.contrib import admin
from django.urls import path
from django.urls import include, path

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('app.urls')),
]

And here is INSTALLED_APPS:
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app'
]

Please help!!!
Comment: what is your main `urls.py`
Comment: @Moha369 I added it in the end
Comment: and what is your `INSTALLED_APPS` ?
Comment: @Moha369 check question
Comment: 1- is the app you created called `app`2- is there a `urls.py` in the app ?
Comment: @Moha369 YES & OF COURSE YES
Answer: I able to fix this problem...
Here is what I have done:

Generate spec file using pyi-makespec dj/manage.py
Add app.urls in hiddenimports such as hiddenimports=['app.urls',],
Use this command instead pyinstaller manage.spec

Finally!!!
Answer: This might not solve for Django, but it solved my issues with Plotly maps. I put this code into my .spec file and was able to run Plotly charts after going through PyInstaller. Could not get my maps to show and was getting error messages.
File "<frozen importlib._bootstrap>"
ModuleNotFoundError: No module named 'plotly.validators.choropleth' pyinstaller

Now it works using the below:
# -*- mode: python ; coding: utf-8 -*-

import sys
sys.setrecursionlimit(5000)

block_cipher = None

a = Analysis(['napdgui.py'],
             pathex=['C:\\Users\\USERNAME\\Desktop\\Python\\NAPD\\napdguiv1'],
             binaries=[],
             datas=[('C:\\Users\\USERNAME\\Anaconda3\\Lib\\site-packages\\plotly\\', 'plotly'),
                ],
             hiddenimports=['pkg_resources.py2_warn', 'app.urls'],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          [],
          exclude_binaries=True,
          name='Tool v08122020',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               upx_exclude=[],
               name='Analysis Tool v081320')


non-interactive password SSH in JAVA

Question: I have a program that requires cascaded SSH, i.e. it ssh A server and then using same connection ssh B server. Server A is acting as a bridge. I have an instance of shell which is used to ssh first server.
When I am doing ssh user@ipAddress, it asks for password. I tried ssh user@ipAddress\npassword. It doesn't seem to be working.
I cannot use any external tools like ssh-agent or expect. I have no control over the server A.
Is there a way I can provide password as an argument or enter password?
Thanks!!
Answer: You should generate a RSA key on the client
ssh-keygen

and put the public key in the authorized key in the authorized_key folder on the server to be able to connect to the server without a password.
An step by step guide is given here.
Edit:
If you have no access to the server, use a ssh-library for Java as decrived in this question.
Comment: I have no control over server A. I cannot install any certificate. The only thing I can do is, use ssh.
Comment: I updated my answer check the code snippets in this thread you will find your answer
Comment: Thanks lc2817. My problem is not connection to server A. I am able to SSH server A. Your answer provides me the solution to that. I need SSH from server A to server B using the same connection. Thank you!
Answer: Either you can setup the password-less login or you can just feed the password like described here:
ssh -t -t <machine> <<EO_MY_INPUT
<password>
date # (or whichever is the command to get date/time)
exit
EO_MY_INPUT

Answer: Since you cannot use expect or ssh-keygen
The following gem can be used to script a password ssh session
http://www.debian-administration.org/articles/587
#!/bin/bash
# Copyright (C) 2008 John S. Skogtvedt <jss at bzz.no>
# Licence: GNU GPL v3 or later at your option

if [ -n "$SSH_ASKPASS_FD" ]
then
        read password <&$SSH_ASKPASS_FD
        echo "$password"
        exit 0
elif [ $# -lt 1 ]
then
        echo "Usage: echo password | $0 <ssh command line>" >&2
        exit 1
fi

export SSH_ASKPASS=$0
export SSH_ASKPASS_FD=4
[ "$DISPLAY" ] || export DISPLAY=dummy:0
read password

exec 3<&0
# write password 100 times to make repeated ssh connections work
for x in $(seq 100)
do
  echo "$password"
done | exec setsid "$@" 4<&0 0<&3

Save the above as asksshpass.sh can chmod +x
echo "yourpassword" | ./sshaskpass.sh ssh firstname.lastname@example.com date

non-interactive password SSH in JAVA


An equivalent condition of the intersection of convex hulls of two compact sets being empty

Question: I came across the following problem where $A$ and $B$ are two compact sets in $\mathbb{R}^n$ ('compact' here means 'closed and bounded'). We need to show that there exists a nonzero vector $\mathbf{a}\in \mathbb{R}^n$ and a scalar $b\in \mathbb{R}$ such that:
$$\mathbf{a}^{\top}\mathbf{x}-b \leq -1 ~ \forall \mathbf{x} \in A \text{ and } \mathbf{a}^{\top}\mathbf{x}-b \geq 1 ~ \forall \mathbf{x} \in B,$$
if and only if the intersection of the convex hull of $A$ and the convex hull of $B$ is empty.
I think it is similar to the Separating hyperplane theorem of convex sets. However, I cannot figure out where the $-1$ and $1$ comes from. Maybe the proof of this statement is far from the Separating hyperplane theorem? Any useful suggestions are appreciated.
Comment: It is just the usual separation theorem you can find in Rudin's FA. A simple transformation can be used to get $-1$ and $+1$.
Comment: Thanks. Could you explain more about the 'simple transformation' to get -1 and +1, which I'm still confused about?
Answer: To prove this from standard version of a separation theorem you can do the following:  If $a^{T}x-b \leq \alpha$ on $A$ and $a^{T}x-b \geq \beta$ on $B$ with $\beta >\alpha$ apply the map $f(t)=\frac 2 {\beta-\alpha} t-1-\frac {2\alpha}  {\beta-\alpha}$ to these inequalities. Since $f$ is increasing the inequalities remain valid after aplying $f$ to both sides.
Comment: Thanks a lot. But I have two more questions. First, when applying the map to both sides, $a^{T}x-b$ is also changed, so that it is not the original inequality. Second, the standard version of separation theorem is about two convex sets, while  here $A$, $B$ are arbitrary compact sets and we are talking about their convex hulls. Sorry to bother but I'm fresh in this area. It would be appreciated if you could reply.
Comment: When you apply $f$ to $a^{T}x-b$ you get $a'^{T}x-b'$ for some $a',b'$. Can you figure out what $a'$ and $b'$ are ?  I am using the fact that convex hulls of compact sets in $\mathbb R^{n}$ are themselves compact. This is proved in Rudin's book. Once you know this you can apply the separation theorem to the convex hulls of $A$ and $B$. @Arthur556
Comment: Thank you very much.


Gradient Descent and Back Propagation in Neural Networks: derivation in vectorised form

Question: I was learning deep learning from Andrew Ng's course on Coursera and in one of the programming assignments to code out Neural Networks from scratch, the formula for the derivative of cost function wrt W is given as:
$$ \frac {dJ} {dW_{i}} = \frac {1} {m} \frac {dJ} {dZ_{i}}  A_{i - 1}^T  $$
Where,
$J$ is the cost function
$m$ is the number of training examples,
$W_{i}$ are the parameters from layer $i - 1$ to layer $i$
$Z_i = W_i^TA_{i - 1} + B_i$.
$A_0$ is $X^T$, i.e., $ dim(A_{0}) = n * m$ and $A_i = ActivationFn(Z_i)$.
However, this seems inconsistent dimensionally, assuming the product of $ \frac {dJ} {dZ_{i}}$ and $A_{i - 1}^T $ is dot product. If we denote the number of nodes in layer $i$ as $n_i$, where layer $0$ is the input $X$ and $n_0 = number of features(n)$,
$$ dim(A_{i - 1}) = (n_{i - 1}, m) $$
$$ dim(Z_i) = (n_{i}, m) $$
$$ dim(\frac{dJ}{dZ_i}) = dim(Z) $$
$$ dim(\frac {dJ} {dZ_{i}}  A_{i - 1}^T ) = (n_{i}, n_{i - 1}) $$
$$ dim(\frac {dJ} {dW_{i}}) = (n_{i - 1}, n_i) $$
However,
$$ dim(W_i) = (n_{i - 1}, n_i) $$
I do not have much knowledge of vector calculus but when I tried t derive these equations myself I got stuck on what kind of product (dot or Hadamard product or something else) will be there between the derivatives on applying chain rule. Can someone please help me figure out where have I gone wrong?
Here is an image of the formulae listed there. Note, the author means $\frac{dJ}{dx}$ when he write $dx$
Answer: The dimensions in the formula were indeed inconsistent, I could fix it by adding a transpose to the formula.
$$ \frac {dJ} {dW_{i}} = \frac {1} {m} (\frac {dJ} {dZ_{i}}  A_{i - 1}^T)^T  $$


Accessing outer block variables from within a nested block

Question: I am trying to access a variable from a parent block from within a nested block using postgresql's plpgsql as follows
DO $outer-block$
DECLARE
  test_variable text DEFAULT "test";
BEGIN

  DO $inner-block$
  BEGIN

    RAISE NOTICE '%',test_variable;

  END;
  $inner-block$;

END;
$outer-block$;

When I try to run this, it tells me that "the column test_variable doesn't exist", so I suppose that this variable is not in scope when the inner block is given control, is there any way to access it from within the nested block?
(I checked up this question: How to access outer scope variables from a function in PostgreSQL?, which was sort of similar, but in that case it's trying to access the outer block variable from whitin a function, which is a persistent database object, and it would in a way be trying to create a closure, that is not what I am doing here, since I want to access the outer block's scope from within a one-time execution inner block, which might make a difference, but I don't know it for certain)
Comment: The inner DO blocks makes absolutely no sense to me. What are you trying to achieve with that? What is the underlying problem you are trying to solve here?
Comment: I'm trying to group specific sub tasks into smaller named blocks with a defined purpose to structure and organize the entire logic, be able to read the whole thing more easily when I go back to it later, creating functions for these one-time tasks wouldn't be worth it. If you have better suggestions for structuring the code, those are welcome, but I would still like to know if what I'm trying to do is possible, regardless of wether it is or not the best approach
Answer: The documentation plpgsql structure shows you how to do this. Using your example as starting point:
DO
$$
<<outerblock>>
DECLARE
  test_variable text DEFAULT 'test';
BEGIN
    RAISE NOTICE '%',test_variable;

    DECLARE
        test_variable text := 'inner test';
    BEGIN
        RAISE NOTICE '%',test_variable;
        RAISE NOTICE '%', outerblock.test_variable;

  END;

END;
$$;

NOTICE:  test
NOTICE:  inner test
NOTICE:  test

Differences from your example:

'test' instead of "test" so the value is not seen as an identifier.

Using the <<outerblock>> label syntax to label the outer block. The $<some_tag>$ does not do this.

Using the label in outerblock.test_variable to fetch the variable value from the outer block.

FYI, in your original example there is no need for the inner block as you don't do anything that changes the value test_variable so it will be the same as the outer block anyway.
UPDATE
Just realized you maybe trying to do nested functions, per the use of DO in your example. That is a different thing then blocks. In order to pass data you would have to add it as a parameter to an argument in the nested function. Since DO functions can't have arguments that is not going to work. You would have to use a regular function.
FURTHER UPDATE
You can have a nested DO function:
DO
$$
<<outerblock>>
DECLARE
  test_variable text DEFAULT 'test';
BEGIN
    RAISE NOTICE '%',test_variable;

    DO
    $i$
    DECLARE
        inner_test_variable text := 'inner test';
    BEGIN
        RAISE NOTICE 'Inner %', inner_test_variable;

    END;
    $i$;
END;
$$;

NOTICE:  test
NOTICE:  Inner inner test
DO

You just can't access the variables in the outer portion. They would have to be passed into the inner function as a parameter to a function argument and DO functions do not have arguments. You could do it with a regular function.
Comment: There is no need for the `DO`.
Comment: I don't see the inner block's "DO", did you forget to type it or are you doing something here that eludes me, I see you did type "DECLARE" and 'BEGIN' twice though
Comment: See my UPDATE for more.
Comment: I am not trying to do nested functions, I really am trying to just have nested blocks, and in fact I am trying to put them inside a function just like in the link you provided, I didn't know the DO for the inner block was unnecessary, thanks this lets me do exactly what I was trying to do
Comment: Glad it worked. Another FYI, per [Identifiers](https://www.postgresql.org/docs/current/sql-syntax-lexical.html) I would stay away from using `-` as in `outer-block` and `inner-block`. Use `_` instead. Using `-` means you have to quote the identifier when you create it and whenever you use it.
Comment: Thanks, will keep it in ming. Also I must point out something I just found out, it's not that "there is no need for the DO" of the inner block, but actually, for some reason it is necessary NOT to have it, otherwise it gives me an error, but if I don't put the inner block's "DO", then the whole thing works fine. I can put a <> for the inner block even if I skip putting in the word "DO" for it either way, so that's cool
Comment: Actually you can have a nested `DO`, see the FURTHER UPDATE to the answer.
Comment: Oh my bad, I explained it poorly, you can have a nested ```DO``` and it will work (that's what I was doing initially), but what I meant is that when you add the ```DO``` to the inner block, for some reason you can't access the variables from the outer block anymore the way you explained, since it throws an error when you try to, but if you don't add the ```DO``` to the inner block, then it works fine
Comment: One more time. `DO` is an anonymous function, it is it's own entity and does not have access to the function it is embedded in. The only way you can pass information from the outer function to the inner one is by passing it in via the inner functions arguments. Since `DO` functions don't have arguments that is not going to happen. If you used a regular function you could do something like `some_func(some_arg varchar)` and then `some_fnc(test_variable)` and use the `test_variable` in `some_fnc()`. Assuming `some_fnc()` had a return then you pass that back to the outer function.


SignalR seems to not be using the root path that I give it when trying to connect

Question: I have my SignalR server listening on the following URL:
http://staging.myserver.com/socket/signalr

I tested locally and it works, but when I deploy, I am running through an NGINX proxy (hence the /socket/)
When it tried to connect to the deployed SignalR server, it is failing and I can see that it is attempting to connect using a URL with the /socket/ ommitted.
I have tried to debug to see if there is a sport where it strips everything but the base url  and appends /signalr but I can't seem to find anything.
I am able to hit the following URL and see my hubs perfectly fine.
http://staging.myserver.com/socket/signalr/hubs

I just don't know why it is omitting the /socket from the url when trying to connect.
Here is more or less an example of what I have
var connection = $.hubConnection('http://staging.myserver.com/socket/signalr');
        var contosoChatHubProxy = connection.createHubProxy('contosoChatHub');
    contosoChatHubProxy.on('addContosoChatMessageToPage', function(name, message) {
        console.log(name + ' ' + message);
    });
    connection.start().done(function() {
            //blah blah
        });

I can see it taking the correct URL when it first initializes the connection, but somewhere along the way, it defaults back to
http://staging.myserver.com/signalr

The only way I have been able to get around it is by adding an addition proxy to / on my NGINX proxy. This is not something that will be able to stick so I need to figure it out.
Answer: Please see GitHub issues #3649 and #3287 which describe the same issue and give some pointers specific to IIS on how to transform the content within the response to the connection negotiation to re-write the URL property. It appears that the URL within the response from the server is used to set a local variable called appRelativeUrl on the client side which is used for subsequent interaction. So another less palatable workaround is to modify/configure the SignalR server using app.MapSignalR("/socket/signalr", hubConfiguration); but I haven't gone that route to see whether it would cause other problems.


Why does my iOS app produce a dyld error on launch?

Question: I have a project running on Xcode 12.3; it's in Objective-C and uses CocoaPods. The 3rd party libraries are built only for devices, not for simulators, so when I run my app on iPhone with iOS 13, the project builds successfully but I get the following error:
dyld: Symbol not found: _$s9SwiftGRPC17ServerSessionBaseC15initialMetadataAA0G0CvM
  Referenced from: /private/var/containers/Bundle/Application/EE50CA2F-F5D6-4DF3-B76F-2E580D604C4B/####.app/Frameworks/Core.framework/Core
  Expected in: /private/var/containers/Bundle/Application/EE50CA2F-F5D6-4DF3-B76F-2E580D604C4B/####.app/Frameworks/SwiftGRPC.framework/SwiftGRPC
 in /private/var/containers/Bundle/Application/EE50CA2F-F5D6-4DF3-B76F-2E580D604C4B/####.app/Frameworks/Core.framework/Core
dyld: launch, loading dependent libraries
DYLD_LIBRARY_PATH=/usr/lib/system/introspection
DYLD_INSERT_LIBRARIES=/Developer/usr/lib/libBacktraceRecording.dylib:/Developer/usr/lib/libMainThreadChecker.dylib:/Developer/Library/PrivateFrameworks/GPUTools.framework/libglInterpose.dylib:/usr/lib/libMTLCapture.dylib:/Developer/Library/PrivateFrameworks/DTDDISupport.framework/libViewDebuggerSupport.dylib
(lldb)

The pod file is as below:
pod 'SwiftGRPC', '~> 0.9.0'
pod 'Firebase/Core'
pod 'Firebase/Messaging'
pod 'CleverTap-iOS-SDK', '~> 3.7.1'

Why am I getting this error and how can I resolve it?
Answer: Have you tried setting BUILD_LIBRARY_FOR_DISTRIBUTION to true for all your frameworks to ensure module stability?
You'll need to set the flag to true in your app (under project settings) as well as in your dependencies by adding a post install script at the end of your Podfile.
post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      config.build_settings['BUILD_LIBRARY_FOR_DISTRIBUTION'] = 'YES'
    end
  end
end

Original solution here


as/400: other way for display graphics?

Question: I'm aware of the existence of DDS files which allow programming of display graphics on the as/400, but is there another way?
Specifically, what I want to do is manipulate the terminal buffer directly to be able to display anything else than just text.
For example, the terminal looks like that:
Let's say, in memory, there would be a two dimensional char array: text[20][80] for the text menu and lower than that, there would be a pixel buffer array of size [200][800].
Is there a way to access either of those arrays directly?
I would like to be able to create a displayable menu entirely in C without the need of a display file and also display other kind of graphics (images) directly in the pixel buffer.
Comment: Are you a developer with command line access? If so, what happens if you enter a STRPTL command and prompt it with F4? Is the command installed?
Comment: @user2338816 Yes I have command line access. The as400 I work with is version V6R1M0. It doesn't have STRPTL command installed.
Comment: It's not clear what you mean by "graphics", especially with C. Commonly nowadays, it might be a browser UI via Java. If C is **required**, you might otherwise want an X-window display, so look into downloading/installing [IBM Tools for Developers (product ID 5799PTL)](https://www-356.ibm.com/partnerworld/wps/servlet/ContentHandler/pw_com_porting_tools_index). But if you can edit your question, you might explain more of what you really need.
Comment: @user2338816 I did made it a lot clearer. Thank you, my question was indeed underdeveloped.
Answer:
Is there a way to access either of those arrays directly?

That's easy enough, though a "display file" that has no formatted fields will still be needed. The 'file' will be the connection between the program and the physical device (or the emulator). You can define a single large area that contains whatever "text" you want your program to put into it. This can even include display field attributes that delimit input areas.
For the most control, the DDS USRDFN keyword is appropriate. But for simple stuff like lists of menu items, almost any large text field can be output to.
Outputting simple text is easy. For detailed stuff like USRDFN formatting, detailed understanding of the 5250 protocol is needed.
One kind of alternative would be to use User Interface Manager (UIM) APIs to update a PANEL's "text area" (:TEXT) via its USREXIT= application program. The UIM handles everything as far as any "display file" definition and actual I/O goes. The UIM can be thought of as a HTML interface for 5250 and uses a very similar markup language to define PANELs.
Another alternative is the Dynamic Screen Manager (DSM) APIs. These give much finer control than the UIM or DDS methods (though DDS USRDFN gets very close). But as with USRDFN, actual device control will require 5250 protocol knowledge.

...and also display other kind of graphics (images) directly in the
  pixel buffer.

There is no "pixel buffer" for 5250 nor even 'pixels'. It's a character-based protocol, like telnet. If you're going for images or 'pixels', you're into browser interfaces, or perhaps Java and NAWT, or X-windows, etc.
Now, granted that with TCP/IP and sockets, you can do essentially anything that you're able to program. Whatever you can figure out how to do, including downloading/installing 3rd-party code libraries, you can do -- within the network restrictions surrounding your server. But it is in fact a server, so GUI kinds of apps generally shouldn't run on it. That's the same as for almost all types of servers. Code the GUI on the client system rather than the server. But you can do it if you really want to.
Comment: OS/400 once had Fax support, and a DSPFAX command. It required the raw G4 TIFF data to be converted into a MO:DCA document prior to viewing with CVTFAX. Probably it's not exactly a conversion but a container. According to the older documentation about Fax support, IBM emulators, or graphics capable terminals are required. My guess is that the modern 3479 or a bit older 3486 terminals — being capable of displaying enhanced user interface widgets instead of char based elements — can display Faxes, and thus pixel data, too. I'm not aware of public documentation about that feature, though.
Answer: I'm not sure why you'd want to do this...
Now-a-days, it'd be much easier to simply generate your output as HTML and serve it up via the integrated apache web server.
But if you really want to do graphics via 5250, it can be done...theoretically at least.  In 20+ years on the platform, I've never seen it.
But way back when (1994?), IBM added support for Graphical Data Display Manager (GDDM) and Presentation Graphics APIs into OS/400.  "GDDM is a means of
displaying, printing, or plotting pictures. Presentation Graphics routines are a
means of displaying, printing, or plotting business charts."
The support is still in the OS.  However, client side support is NOT available in IBM i Access for Windows or the most recently released client, IBM Access Client Solutions (ACS).  It appears that the standalone IBM Personal Communications product may support GDDM.
For complete control of the character buffer, take a look at the Dynamic Screen Manager (DSM) APIs.  The DSM APIs are "a set of screen I/O interfaces that provide a dynamic way to create and manage screens for the Integrated Language Environment® (ILE) high-level languages. Because the DSM interfaces are bindable, they are accessible to ILE programs only."
Comment: I've used GDDM for both print and display. For display, though, you need a graphics-capable 5250 terminal or emulator. The last emulator I'm aware of that did 5250 graphics was the old IBM DOS emulator via SNA. (Maybe color 3279 could work?) Of course, nothing stops someone from writing a modern one. IIRC, GDDM has been native in the OS for a few versions, not requiring installing a separate option or product.
Comment: @user2338816... Yeah, I built a little demo...and it appears that neither ACS nor iAW support GDDM.  But it does look like PCOMM might.
Comment: The IDEAcomm Midrange Client v6.10 (and later) is a very crude 16 bit Windows application, but it supports GDDM when setting the correct terminal type (5292-2). It runs e. g. on Windows 2000.
Answer: There is a way to do it in ILE C/C++. This was very fun to investigate since I haven't tried it myself.
The only documentation on it (page 183+) I could find is from 5.1, but you are able to cross reference the functions used to this 7.3 manual (possibly page vii/7) to see if they're still used the same.
Hope this helped!
Comment: Which function names exactly are you referring to in the ILE C/C++ Programmer's Guide (SC09-2712)? And which functions are you talking about in the C/C++ Runtime Library Functions (SC41-5607)? Your reply leaves more questions dangling than it answers.

as/400: other way for display graphics?


Run function with different parameters depending on different element click?

Question: I'm going through The Odin Project and I am at the 2nd stage of the Rock, Paper, Scissors project. I have already got all of the logic working using prompt to get the player selection and using the console for feedback, etc.
The 2nd part of the project asks me to make a GUI web site to play the game. I have three images that I've added a href="#" to and each image has its own unique id value. I can't seem to figure out how to get the particular value from which image the user clicks and use that id value as the parameter to the play game function when that particular image is clicked. I know I need to add an event listener to catch the click and somehow return the id value to be used in the function. The function simply takes the player selection (clicked image id) and compares it with the randomly generated computer selection and will change the text content of a separate div depending on the result.
Any help would be much appreciated!
EDIT: Forgot to add GitHub repo for code! Also, figured out something that worked before looking at these answers, but I already appreciate the great feedback on the options I have available to accomplish this task. Please feel free to critique my code if it can be improved in any way! I still have some functionality to work on and connect to the html.
GitHub repo: https://github.com/dblinkhorn/rockPaperScissors
Comment: put three image or element, select through `querySelectorAll` or `getElementsByTagName` we do not use element `id` normally, so why `id` - put the minimal version of your project here or jsfiddle
Comment: Added the github repo for code. Thanks!
Answer: Any function you register as an event listener automatically gets passed the event object, when the event you registered the function for occurs.
This event object holds alot of information, amongst which you find which element the event actually occured on: event.target.
If you want to know the id of the element, just ask it:
function clickHandler(event) {
  console.log(event.target.id);
}

Example (I'm using divs here for simplifying the demo):

const divs = document.querySelectorAll('div[id]');

for (const div of divs) div.addEventListener('click', clickHandler);

function clickHandler(event) {
  console.log(event.target.id);
}
div[id] { background-color: #a00; color: #fff; padding: 50px; display: inline-block; }
div[id]::before { content: attr(id); }

Answer: It would be nice to see some of your code to give you better feedback, but a possible way to accomplish what you want is to use event.currentTarget

The currentTarget read-only property of the Event interface identifies the current target for the event, as the event traverses the DOM. It always refers to the element to which the event handler has been attached [...]

function onImgClick(event) {
  console.log(`you clicked the img with id "${event.currentTarget.id}"`)
}

const images = document.querySelectorAll("img")
for (const image of images) {
  image.addEventListener("click", onImgClick);
}


Authenticate user(group) and encrypt port 8078 on a server using SSH2 keys?

Question: I want to encrypt and authenticate port 8078 on a server running Jenkins using SSH keys (I'll pass our the public or private key as needed), and keep 8078 from being accessed by anything but SSH. I have just a few developers using the machine for now.
Questions:
A/ Do I need a firewall to complete this to block 8078 from non SSH access?
B/ Will the decrypted request through port 22, when redirected to port 8078, be seen by a firewall?
B/ What IP/port will the firewall see when the SSH server redirects the unecrypted request on port 22 to port 8078?
C/ Is this a good long term solution to both encryption and authentication to a non web server application?
D/ Would a VPN solution be better for this purpose?
Comment: Just configure the SSH daemon to listen only on port 8078 and don't configure anything else to listen on port 8078.
Answer: A) Once an SSH server listens on 8078 on the server, no other application can, Jenkins included.
B) I get confused: SSH does not redirect unencrypted requests.
C) I assume you want to do the following:

Configure Jenkins to run on Port 8078, unencrypted, but on the localhost IP 127.0.0.1. No firewall needed, as no one from outside can connect that IP. Only if Jenkins cannot be configured to bind to 127.0.0.1, you need a firewall.
Create a special user account with a custom shell that acts as a telnet client to 127.0.0.1:8078. You can do so with a very simple bash script. Authorize all public keys to use that account.
Connect to SSH at port 22 as usual with the special user. The connection will open the special telnet shell.

Script would look like this:
<code>#! /bin/bash
telnet localhost 8078
</code>
Save in /usr/local/bin/jenkins-ssh and
<code>chmod a+x /usr/local/bin/jenkins-ssh
useradd jenkins-ssh
passwd jenkins-ssh (or don't, use pubkey only)
chsh jenkins-ssh -s /usr/local/bin/jenkins-ssh
</code>
D) A VPN solution would be superior in that it can be used for more than one service. But you'd have to bind the service to an internal IP which may become available only after starting the VPN service, thus complicating startup sequence.
Comment: Internal IP? Is that a localhost IP 127.0.0.X, or one on the local network? Otherwise I really like your suggestions, thank you.
Comment: VPNs like openvpn (which is quite good and versatile) work with non-public subnets like 192.168.x.x or 10.x.x.x. You'd configure your VPN server to serve a subnet like 10.11.12.0/24 and have 10.11.12.1 assigned to the server. Jenkins would then bind to 10.11.12.1. When you login with VPN client, you get another IP, like 10.11.12.5, and any connection to 10.11.12.1 would route through the VPN tunnel.
Comment: And then there's IPSec which can indeed encrypt traffic between public IPs, but that's hard to configure and maintain. Plus it doesn't play nice with todays common NAT gateways (read: DSL routers). Don't touch it.


DirectionsRenderer and DirectionsService @react-google-maps/api not getting rendered and also not throwing error

Question: I am trying to user Direction service of @react-google-maps/api.
I am using reactjs and doc link is https://react-google-maps-api-docs.netlify.app/#directionsservice.
 {response !== null && (
                <DirectionsRenderer
                  // required
                  options={{
                    directions: response
                  }}
                />
              )}

What am I doing wrong. I am not getting either.
Answer: Map is getting rendered so many times. As per the documentation they are setting the state during render. render should always remain pure. It's a very bad practice to do side-effecty things in render and in some other react life cycle hook.
I think they have to work on improving the documentation. if we follow the documentation it will give direction but you will get an exceeded google maps quota error.
 const [response, setResponse] = React.useState(null);
   const directionsCallback = res => {
       console.log(' => res', res);
    if (res !== null) {
      if (res.status === 'OK') {
        setResponse(res);
      } else {
        console.log('res: ', res);
      }
    }
  };

         <DirectionsService
            options={{
              destination: destination,
              origin: origin,
              travelMode: 'DRIVING'
            }}
            callback={directionsCallback}
          />

Update:
This solved multiple rendering
let count = React.useRef(0);

const directionsCallback = React.useCallback(res => {
    console.log(count.current);
    if (res !== null) {
      if (res.status === 'OK' && count.current < 2) {
        count.current += 1;
        setResponse(res);
      } else {
        count.current = 0;
        console.log('res: ', res);
      }
    }
  }, []);


systemd: myservice.service vs myservice.target

Question: In a systemd service definition file, what is the difference between
[Unit]
Description=My service...
After=zookeeper.service

and
[Unit]
Description=My service...
After=zookeeper.target

zookeeper is another service defined in another service file, but what is the significance of the extension .service vs .target. Is there a difference?
Answer: A service:

A unit configuration file whose name ends in .service encodes
  information about a process controlled and supervised by systemd.

A target:

A unit configuration file whose name ends in ".target" encodes
  information about a target unit of systemd, which is used for grouping
  units and as well-known synchronization points during start-up.

These both refer to unit configuration files, which are:

A unit configuration file encodes information about a service, a
  socket, a device, a mount point, an automount point, a swap file or
  partition, a start-up target, a watched file system path, a timer
  controlled and supervised by systemd(1), a resource management slice
  or a group of externally created processes.

Reference: man pages for systemd.service, systemd.target, and systemd.unit, respectively.
Also, with respect to the configuration files themselves; services need:

Service files must include a "[Service]" section, which carries
  information about the service and the process it supervises.

No such requirement exists for target configuration files. In fact, targets have no options specific to targets; items are configured in generic [Unit] and [Install] sections.
Answer: Yes. See man systemd.service and man systemd.target for the documentation for each type of systemd unit.
On your systemd, you can find the files and review what systemd directives they contain, assuming both exist:
 locate zookeeper.service
 locate zookeeper.target


How to pass a db_query as an array instead of a string?

Question: I am having a bit of trouble converting my module db_query argument to pass as an array as oppose to a string. The module is D6 and I am trying to port it D7. Here is the snippet:
$result = db_query("SELECT nfi.field_name, nfi.label
  FROM {field_config_instance()} nfi
    INNER JOIN {field_config()} nf ON nfi.field_name = nf.field_name
  WHERE nfi.type_name = '%s' AND nf.active = 1 AND nf.locked = 0", $type_name);

And here the updated version as suggested below, but I am still getting a PDOException error:
$result = db_query("SELECT nfi.field_name, nfi.label
  FROM {field_config_instance()} nfi
    INNER JOIN {field_config()} nf ON nfi.field_name = nf.field_name
  WHERE nfi.type_name = ':type_name'  AND nf.active = 1 AND nf.locked = 0", array(':type_name'=>$type_name));

The full error is as below, line 49 is the line starting "WHERE" or the entire bottom line basically:
PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '() nfi INNER JOIN field_config() nf ON nfi.field_name = nf.field_name ' at line 2: SELECT nfi.field_name, nfi.label FROM {field_config_instance()} nfi INNER JOIN {field_config()} nf ON nfi.field_name = nf.field_name WHERE nfi.type_name = ':type_name' AND nf.active = 1 AND nf.locked = 0; Array ( [:type_name] => page ) in cck_private_fields_get_content_private_fields() (line 49 of C:|... common.inc).
Any suggestions welcome. When I manage to finish this module I'll be submitting it to Drupal.org so everyone can use it.
Answer: There are three errors in that query:

The database tables are not field_config_instance(), and field_config(), but field_config_instance, and field_config. Parentheses have a particular meaning in SQL, and putting them after a table name causes an error message.
The field_config_instance table doesn't contain any "label" field. See the schema definition given in field_schema().
$schema['field_config_instance'] = array(
  'fields' => array(
    'id' => array(
      'type' => 'serial',
      'not null' => TRUE,
      'description' => 'The primary identifier for a field instance',
    ),
    'field_id' => array(
      'type' => 'int',
      'not null' => TRUE,
      'description' => 'The identifier of the field attached by this instance',
    ),
    'field_name' => array(
      'type' => 'varchar',
      'length' => 32,
      'not null' => TRUE,
      'default' => '',
    ),
    'entity_type' => array(
      'type' => 'varchar',
      'length' => 32,
      'not null' => TRUE,
      'default' => '',
    ),
    'bundle' => array(
      'type' => 'varchar',
      'length' => 128,
      'not null' => TRUE,
      'default' => '',
    ),
    'data' => array(
      'type' => 'blob',
      'size' => 'big',
      'not null' => TRUE,
      'serialize' => TRUE,
    ),
    'deleted' => array(
      'type' => 'int',
      'size' => 'tiny',
      'not null' => TRUE,
      'default' => 0,
    ),
  ),
  'primary key' => array('id'),
  'indexes' => array(
    // Used by field_delete_instance().
    'field_name_bundle' => array('field_name', 'entity_type', 'bundle'),
    // Used by field_read_instances().
    'deleted' => array('deleted'),
  ),
);

Drupal 7 placeholders aren't wrapped between quotes, when they are for strings.

The following query should not return any error.
SELECT nfi.field_name FROM {field_config_instance} nfi
  INNER JOIN {field_config} nf ON nfi.field_name = nf.field_name
  WHERE nfi.type_name = :type_name  AND nf.active = 1 AND nf.locked = 0

Answer: If this is for Drupal 7, you should name your array items and placeholders.
$result = db_query("SELECT nfi.field_name, nfi.label
  FROM {field_config_instance()} nfi
    INNER JOIN {field_config()} nf ON nfi.field_name = nf.field_name
  WHERE nfi.type_name = ':type_name' AND nf.active = 1 AND nf.locked = 0", array(':type_name'=>$type_name));

In Drupal 6, db_query() doesn't take an array as second argument.
Comment: The quotes are wrong. Use nfi.type_name = :type_name.
Comment: Thank you for your help Drupac. I am still getting a PDOException error though unfortunately. I have updated the question above to reflect the changes and the error, please have a look :)
Comment: Hi Berdid, thanks for your input. If I take out the quotes from just the line above it still gives the same PDOException unfortunately. If I take them out of the array also i.e array(:type_name=>$type_name));  I get a fatal error on that line instead. Is that what you meant?
Comment: The documentation for [db_query()](http://api.drupal.org/api/drupal/includes%21database.pgsql.inc/function/db_query/6) clearly states that, "Instead of a variable number of query arguments, you may also pass a single array containing the query arguments." It is not true that in Drupal 6 `db_query()` doesn't take an array as second argument. Also, Drupal doesn't use parentheses after table names, such as in {field_config_instance()}; Drupal uses curly brackets.


Inserir um elemento em um array que contem um objeto

Question: Olá, gostaria de saber como faço pra inserir um elemento dentro de um objeto de um array, no caso, quando faço a adição com o código abaixo, o elemento é inserido como outro index:
 this.array = [];
 this.array.push(email);
 this.array.push({ Mensagem: res.data[0].Mensagem });

O retorno que obtenho é este aqui:
[
  {
    "CodigoPostagem": 25,
    "DataMensagem": "2017-06-28T14:35:00",
    "Emitente": "Item",
    "Titulo": "Mensagem número 25",
    "Visualizado": false
  },
  {
    "Mensagem": "Teste"
  }
]

O "email" possui o item acima, queria somente de inserir o retorno de uma api dentro daquele objeto.
Agradeço desde já!
Comment: Queres adicionar esse objeto com a `Mensagem` ao objeto do `email`? é isso?
Answer: Você pode somente atribuir antes de fazer push:
this.array = [];
email.mensagem = res.data[0].Mensagem;
this.array.push(email);


Logically speaking, why can variables be substituted?

Question: Suppose that
$$a^2+a+1=b$$
Suppose also that $a=5/4$. What makes it valid to substitute $5/4$ into the first equation? Is it because equality is transitive?
Comment: It's by definition of equality and because in deductive systems there is a **postulated** rule that allows you to make such a substituion.
Comment: @GitGud, what do you mean by postulated rule?
Comment: I mean it's something that is true axiomatically. It's like that because you define it like that. Why is it that $1=1$ is true? It's by definition of equality, we define it that way.
Answer: It's just a basic principle of first-order logic with equality that if $a = b$ and $P(b)$ for a formula $P$, then $P(a)$. It's probably the most important aspect of equality that usually "goes without saying" in mathematics, except in logic courses.
Comment: @user101939 It is called [Leibniz law](https://en.wikipedia.org/wiki/Identity_of_indiscernibles)
Comment: Yeah, I just call it the substitution property.
Comment: @user101939 If you want to know the name to read about about it, you should look for *first order logic with equality*, *predicate calculus with equality*. Check [this](http://en.wikipedia.org/wiki/First-order_logic#Equality_and_its_axioms) link.
Comment: Does this principle have a name?
Answer: The reason why you can make such a substitution is that $a$ and $\dfrac{5}{4}$ are precisely the same, just written differently. Here's a long-winded way of making the substitution you describe.
\begin{align*}
a &= \frac{5}{4} & \\
a^2 &= \left(\frac{5}{4}\right)^2 &\text{squaring both sides}\\
a^2 + a &= \left(\frac{5}{4}\right)^2 + \frac{5}{4} &\text{adding the first two lines}\\
a^2 + a + 1 &= \left(\frac{5}{4}\right)^2 + \frac{5}{4}+1 &\text{adding $1$ to both sides}\\
b &= \left(\frac{5}{4}\right)^2 + \frac{5}{4}+1 &\text{using the fact that $a$ satisfies the given equation}
\end{align*}
The right hand side is precisely what you obtain when you replace $a$ by $\dfrac{5}{4}$.
Answer: There are at least two ways to understand equality.
One way to treat it is as a primitive logical identity sign: $a=b$ means that '$a$' and '$b$' are signs for the same thing.  Since they represent one and the same object, any property that can be proved of the object represented by $a$ must also hold of the object represented by $b$, because they are the same object. In this case one automatically gets a general substitution principle: $a$ can be replaced by $b$ because they mean the same thing: they are names for the same object.
Another way to proceed is the way that ZF set theory does, and to take equality as a defined property.  For example, ZF says that '$a=b$' is an abbreviation for $$\forall x(x\in a\iff x\in b).$$
Two sets are defined to be equal if they contain the same elements.  If one does this, substitution must be asserted axiomatically, or proved as a theorem.  Without such a proof (trivial if substitution is an axiom) it might or might not hold. Transitivity is similar.
But the usual understanding in algebra is the first type: when we write something like '$x = \frac54$' we mean that the name '$x$' is a synonym for whatever object is denoted by $\frac54$.
Comment: This isn't quite accurate.  Whether a logical system takes equality as a primitive relation or a defined one, one almost always gives the substitutivity property as an axiom or rule of the system.  In principle one certainly could have a system where substitutivity was a theorem, but that's not what's done in most presentations of ZF(C), or any other standard theory I know.


Saída da rede neural retornando Null

Question: Eu estou fazendo uma rede neural com brain.js, para aprender o alfabeto. o código é o seguinte:

const alfabeto = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "T", "U", "V", "W", "X", "Y", "Z"];

function converteLetrasemNumeros(letras) {
 let alfabeto = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 let codigos = [];
        let numeros = [];
 for (let i = 0; i < letras.length; i++) {
  codigos.push(alfabeto.indexOf(letras[i].toUpperCase()) + 1);
 }
   numeros.push(codigos);
 return numeros;
}
function converteNumerosemLetras(numero) {
  let letra;
  console.log(String.fromCharCode(numero+64));
  return letra;
}
let trainingData = [];
trainingData.push(converteLetrasemNumeros(alfabeto));

const neuralNetwork = new brain.recurrent.LSTMTimeStep({hiddenLayers: [2][3]});

console.log(neuralNetwork.train(trainingData, {errorTrash: 0.0001, iterations: 25000}));

console.log(neuralNetwork.run(converteLetrasemNumeros(["A", "B", "C"])));

A saída da rede, como poderão ver na execução do código, retorna Null.
Comment: Utilize o campo de respostas para a solução, em vez de acrescentar na pergunta
Answer: Eu já achei a solução do problema. O Erro estava neste bloco:
for (let i = 0; i < letras.length; i++) {
    codigos.push(alfabeto.indexOf(letras[i].toUpperCase()) + 1);
}
numeros.push(codigos);

Eu estava adicionado valores ao Array numeros, fora do loop, que por sua vez recebia valores de outro Array. Então o código corrigido fica assim:
for (let i in letras) {
    numeros.push(alfabeto.indexOf(letras[i].toUpperCase()) + 1);
}
return numeros

Saída da rede neural retornando Null


Good method/approach in improving advanced Drupal development skills

Question: This might be a bad place to post this, but here it goes.
I'm looking for a good approach in bettering my knowledge/skills to Drupal. I have been using it since D5 and I still feel like I am so 'unskilled' in doing advanced development. I still struggle with how to create a custom module or hook alter methods properly (with 1 try and not 10) through the template.php file. I have seen others create some insane modules or scripts and I just boggles me on how they come up with it.
It's always been trial-and-error for me, and sometimes I get it to work but never can get an answer on 'Why that worked'.
I have been a php developer for over 10+ years, touched a bit of C#, Ruby, and Java but nothing hugely professional. I have always been driven to continue pursuing Drupal development. Just so incredibly overwhelming still at times. I have high hopes to make the next DrupalCon in Austin, Texas.
Answer: I have found the Drupal Examples module to be a great starting point for learning enough to develop my own modules, or to aid in solving a particularly thorny problem I'm having.
There's also the Devel and Dev Themer modules, which help to illuminate some of what's going on behind the scenes.
Then there is the API documentation, which I personally have a harder time understanding until I read some of the comments.
These are the ones I use most; others will probably chime in with others, which I know about but lack enough experience in to form a helpful opinion.
Comment: Never heard of Drupal Examples. I tend to learn best by reverse engineering something. So that is perfect. Thanks for the heads up!

Devel is great when theming node/field/field-collection tpl. But still so confusing on how to theme a form.

I am not one to purchasing books (online tutorials usually are great alone) but have been tempted to buy a few Drupal dev books. Tons out there though, unsure which might be the best to start with for wanting to do advanced developed.
Comment: Have a look at D7 Module development and others from Packt books collection. Were great for me. Maybe not as advanced as you need
Comment: @Drupac Starting to figure out a few things that I just never understood using Drupal Examples and reverse engineering them. :)


What question to ask to find right path

Question: Suppose a certain country is inhabited by truth teller and liars, who only respond to yes or no questions. A tourist comes to a fork in the road where one branch leads to the capital, and the other does not. There is a native standing at the fork.  What yes or no question should the tourist ask to determine which branch to take?
The tourist can ask "If I were to ask you if the left branch leads to the capital, would you say yes?"  By construction, if Yes, then left branch leads to the capital. If No, then it doesn't.
My book however, suggests letting $A$ be "you are a truth teller" and $B$ be "the left branch leads to the capital." Construct a formula from $A$ and $B$, call it $F$, so when asked "Is the statement F true, the native replies yes iff $B$ is true.
How can we construct $F$?
Answer: "If I ask the other kind of person, whether the right road leads to the capital , he will say YES, won't he? "
Explanation: ((( Assuming left road is the correct Path ... )))
                                Liar will say "Yes"
                                Truth teller -"Yes"
Liar knows that truth teller knows the truth and will say no when asked So liar will tell the opposite which is "yes".
Truth teller knows that the liar will lie and will say yes about right road being correct, So truth teller will also say "Yes" indicating that the liar will say yes.
So I will take the other road.
Answer: I think you're supposed to ask: "If you were a truth teller, would you tell me if the left branch leads to the capital?".
If the native is a truth teller, he will confirm that he would say yes if the left branch does indeed lead to the capital.
If the native is a liar (and the left branch does lead to the capital), he would tell you that the left branch doesn't lead to the capital. However, he would ALSO lie to you when you ask him the question above. He would lie about lying, so his answer would have to be "yes" as well.
Comment: This is not the question.
Comment: Edited it... is it better now?
Comment: No, it's not an issue of grammar.  If you scroll up, you'll notice my question is "How can we construct F?" and nothing else.

What question to ask to find right path


Get default selection color?

Question: I can change the CSS selection color, but how can I get the browser/OS default selection so that I can use it elsewhere?
e.g. on Ubuntu+Chrome text is highlighted in orange.

I want to apply this same style to an
 when I hover over it with my mouse.
Comment: Can you describe the problem further - so do you mean that someone on Ubuntu would then have their hover style be orange background + white text and someone on mac might be blue bg + white text?
Comment: This might be what you're looking for: https://stackoverflow.com/questions/30250207/determine-browsers-default-text-highlight-color-using-javascript-or-dart, see the second answer - systems are deprecated, but supposedly still work so you could potentially detect the browser than use them for your styles on the page.
Comment: Also some stock browser default colours listed here if you want to go that route: https://stackoverflow.com/questions/16094837/what-is-the-browser-default-background-color-when-selecting-text
Comment: @NathanielFlick That's exactly right.
Answer: In the CSS2 specs some color keywords are defined but are now deprecated. Yet they should still work just fine:

.selected {
  background: Highlight;
  color: HighlightText;
}

Lorem ipsum with some selected text

Comment: That's pretty cool, but it doesn't actually come out the [right color](https://i.stack.imgur.com/4yqiK.png).
Comment: The color is off on Win10/Chrome FYI https://mpen.xyz/share/2023/01/2023-01-23_21-39-53.png
Comment: Interesting. Windows 10 and macOS work just fine. What browser are you using? Did you try others as well? ... **edit:** okay, I just tested it in Firefox and it is rendered wrong as well.
Comment: That's Chrome 60 on Ubuntu 16.04. [Here's Firefox](https://i.stack.imgur.com/rgDlr.png) -- that one comes out right.
Answer: I think that for what you want is better if you use Javacript combined with CSS.
You could check this post https://stackoverflow.com/a/11752084/8379001 that shows how to find out the os and applies it to CSS.
Comment: The OS isn't enough information. Plus, `navigator.platform` comes out as `"Linux x86_64"` on Ubuntu 16.04. Doesn't even tell me the distro.
Comment: You have to keep in mind that in web pages your platform is a Web Browser, so your direct interaccion is the Web Browser not the OS, all the info you can get from the OS comes from the Browser. Therefore, your best chance is to use JavaScript that runs on the client side. CSS is better just for styling. Also, not all the web browsers have the same interaction with the OS; so, with some you could get more info than others. Anyway, your best approach is through Javascript.
Comment: I realize the information is limited, but I think it's a pointless endeavour. Chrome-Ubuntu uses orange highlight text, Chrome-Fedora uses blue. I won't get anywhere with that approach. Also, I *am* only using this for styling, so CSS would be optimal if it worked reliably.

Get default selection color?


Finding the Last Eigenvalue for a Matrix

Question: $K$ is a $3 \times 3$ real symmetric matrix such that $K = K^3$.  Furthermore, we are given that:
\begin{align*}
K(1, 1, 1) \ \ & = \ \ (0, 0, 0) \\
K(1, 2, -3) \ \ & = \ \ (1, 2, -3)
\end{align*}
So we know that $0, 1$ are two of the eigenvalues of $K$.  What can I do to ascertain that the last eigenvalue is $0, 1$ or something else?
\begin{align*}
\det(K - \lambda I ) \ \ & = \ \ -\lambda(1 - \lambda)(c - \lambda) \\
& = \ \ -\lambda(\lambda - 1)(\lambda - c) \\
& = \ \ -\lambda^3 +(c + 1)\lambda^2 - c \lambda \\ \\
K^3 - K \ \ & = \ \ K(K - 1)(K + 1)
\end{align*}
If I know for sure that $K$ is not idempotent, then I know that $c = -1$:
\begin{align*}
\det (K - \lambda I) - K^3 + K \ \ = \ \ (c+1)K^2 - (c+1)K \ \ = \ \ 0
\end{align*}
But since it was not given explicitly, I don't think this will work.
Answer: Each of $0,1$ and $-1$ is a possible value of the last eigenvalue. In fact, if you complete $u=(1,1,1)^T/\sqrt{3}$ and $v=(1,2,-3)^T/\sqrt{14}\}$ to an orthonormal basis $\{u,v,w\}$ of $\mathbb R^3$, then $K=vv^T+\lambda ww^T$ will satisfy $K^3=K$ for every $\lambda\in\{0,1,-1\}$ and $(0,u),(1,v)$ and $(\lambda,w)$ are three eigenpairs of $K$.
However, as the eigenvalues sum to the trace, one can at least ascertain that the remaining eigenvalue is $\operatorname{tr}(K)-1$.
Answer: Let $w = (1,1,1)\times (1,2,-3) = (-5,4,1)$ and consider
$$Kw = w, \quad Kw = 0, \quad Kw = -w.$$
In all three cases $K$ is symmetric, $K = K^3$ and we get that the last eigenvalue is $1,0$ and $-1$ respectively. The respective matrices of $K$ in basis $\{(1,1,1), (1,2,-3),(-5,4,1)\}$ are
$$\begin{bmatrix} 0 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1\end{bmatrix}, \quad\begin{bmatrix} 0 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 0\end{bmatrix}, \quad\begin{bmatrix} 0 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & -1\end{bmatrix}.$$