Routing Multiple External IP Addresses

Question: My employer has business-level cable modem service, and we have a block of five static IP addresses. Until this morning, we've been using IPCop running on an old box in order to facilitate NAT'ing and port forwarding from the five external IP's to various servers within the internal network. Unfortunately the IPCop box died a sudden death this morning, and we're left limping along on a temporary Linksys wireless router. We've been able to restore email by using that single static IP as the external address it's using, but we need to service all five in order to run the business.
I'm looking to purchase as inexpensive of a router as possible that can handle port forwarding across multiple external IP addresses. While particular product recommendations are welcome from my point of view, I'm more interested in what I need to be looking for to be able to determine if a particular model can support this configuration.
TL;DR Version: What is the term (assuming that there is one) that is used to describe the a router/firewall that supports forwarding ports from multiple external addresses to various internal addresses?
Comment: Why not rebuild the IPCop box?
Comment: @Dan: That's a possibility, but we'd like to move to something a little more reliable if possible. We've had issues with IPCop going down in the past, so we'd like to try to move to a dedicated networking device if one is available.
Answer: I suggest grabbing an old desktop/server and run PFsense. multiple IPs are called Virtual IPs and you want to use ARP verses CARP
Answer: You are looking for "Port forwarding" or DNAT.
I know Juniper calls it VIP for Virtual IP...
Comment: Honestly, I never encountered a device that had port forwarding and that couldn't handle what you are describing. If you want to be sure, I know Watchguard are pretty complete devices http://www.watchguard.com/products/xtm-2/overview.asp
Comment: Thanks. I'm familiar with port forwarding, but my specific requirement here is to be able to handle multiple external IP addresses and forward specific ports from specific addresses.
Comment: @Alex: Good call. I recently installed an XTM23 at a client site and it works flawlessly. I also run a couple old Firebox X1000 units in my own offices.
Comment: @Alex: Our experience (other than with IPCop) is with consumer-grade equipment like the Linksys router we're limping along with. I took a quick look at the Watchguard systems, but it seems like a bit more than we really need (especially in the support department). Is there anything more in the couple hundred $$ range that might do what we're after?
Comment: @Adam I would say that the downtime your are experiencing at the moment is well worth the extra 200$ you will put on the device :) . But for cheaper devices you'll have to go with Linksys, D-Link or Netgear.
Comment: @Alex: What I'm currently considering is a Cisco ASA5505. $359.
Comment: @Adam The ASA5505 are great devices. I am currently managing a couple of hundred of those. But honestly, the 200$ more for the Watchguard will give you more flexibility and probably some ease of use that you might not find in the Cisco device. Your call :)
Comment: @Alex: I'm glad to hear that the ASA is a good choice. While the watchguard might have been better, the order has already been placed to Newegg ;)
Answer: We are a small office and we use Juniper SSG5s that cost in the $400 -$700 range.
IMO, juniper calls what youre asking for MIPs - you Map an external IP to an internal IP and then create a policy to allow certain types of traffic/ports etc.
Juniper support techs will actually remote in and show you how to do this - their support is worth the money.

Routing Multiple External IP Addresses


Problem similar to folland chapter 2 problem 51.

Question:
Let $(X\mathcal M,\mu)$ and $(Y,\mathcal N,v)$ be $\sigma$-finite measure spaces.

If $f:X\to\Bbb R$ is $\mathcal M$-measurable, $g:Y\to\Bbb R$ is $\mathcal N$-measurable, and $h(x,y)=f(x)g(y)$, prove that $h$ is $\mathcal M\otimes \mathcal N$-measurable.
If $f\in L^1(\mu)$ and $g\in L^1(v)$, prove that $h\in L^1(\mu\times v)$ and $$\int\, h\,d(\mu\times v)=\left[\int\,f\,\mathrm d\mu\right]\left[\int\,g\,\mathrm dv\right].$$

Could someone show me how to prove this. I have been staring at it, and cannot even think where to start. I am a bit desperate here. I will highly rate for your help. Thank you.
Answer:
The problem is easy if $f$ and $g$ are characteristic functions of measurable sets. Then we use the fact that we can approximate pointwise a measurable functions by linear combinations of characteristic functions of measurable sets.
We assume first that $f$ and $g$ are non-negative. We use the definition of the product measure when $f=\chi_A$ and $g=\chi_B$. Then we extend to simple functions and use monotone convergence theorem.
Answer: I think I solved the problem. If anyone has comments, I would greatly appreciate them.
$\text{a)}$ Define $\phi:X\times Y\to\Bbb R\times\Bbb R$ by $\phi(x,y)=\left(f(x),g(y)\right)$ and define $\psi:\Bbb R\times\Bbb R\to\Bbb R$ by $\psi(m,n)=mn$. Notice that $h$ is the composition of these two functions, i.e., $$h(x,y)=f(x)g(y)=\left(\psi\circ\phi\right)(x,y).$$
Since a continuous function of a measurable function is measurable, it suffices to show that $\phi(x,y)$ is a $(\mathcal M\otimes\mathcal N)$-measurable function from $X\times Y$ into $\Bbb R\times \Bbb R$. Let $R$ be an open rectangle in $\Bbb R\times\Bbb R$ such that $R=A\times B$ for some open sets $A$ and $B$ in $\Bbb R$. Then $$\begin{align}
\phi^{-1}(R)=\phi^{-1}(A\times B)&=\{(x,y):f(x)\in A,g(y)\in B\}\\\,\\
&=\{(x,y):f(x)\in A\}\cap\{(x,y):g(y)\in B\}\\\,\\
&=\left(f^{-1}(A)\times Y\right)\cap\left(X\times g^{-1}(B)\right)\\\,\\
&=f^{-1}(A)\times g^{-1}(B).
\end{align}$$Since $f^{-1}(A)\in\mathcal M$ and $g^{-1}(B)\in\mathcal N$, and $f$ and $g$ are $\mathcal M$ and $\mathcal N$ measurable, respectively. Thus, $\phi^{-1}(R)\in\mathcal M\otimes\mathcal N$, and thus, $h(x,y)$ is $(\mathcal M\otimes\mathcal N)$-measurable. $\,\blacksquare$
$\text{b)}$ Let us suppose $f$ and $g$ are non-negative measurable functions. Let $\{\phi_n\}$ and $\{\psi_n\}$ be sequences of increasing functions such that $\phi_n\to f$ and $\psi_n\to g$ converge pointwise on $X$ and $Y$, respectively. Then $\{\phi_n\psi_n\}$ is an increasing sequence such that $\phi_n\psi_n\to h$. By the monotone convergence theorem, we have $$\begin{align}
\int\,fgd(\mu\times v)=\lim_{n\to\infty}\int\,\phi_n\psi_nd(\mu\times v)&=\lim_{n\to\infty}\left(\int\phi_nd\mu\int\psi_ndv\right)\\
&=\left(\lim_{n\to\infty}\int\phi_nd\mu\right)\left(\lim_{n\to\infty}\int\psi_ndv\right)\\
&=\int\,fd\mu\int\,gdv.
\end{align}$$$\quad$Since $|fg|=|f||g|$ and $f\in L^{1}(\mu)$ and $g\in L^{1}(v)$, then $fg=h\in L^{1}(\mu\times v)$. Also, since $f$ and $g$ are real valued, $$\begin{align}
\int\,fgd(\mu\times v)&=\int(fg)^+d(\mu\times v)-\int(fg)^-d(\mu\times v)\\
&=\int f^+g^+d(\mu\times v)+\int f^{-}g^-d(\mu\times v)-\int f^+g^-d(\mu\times v)-\int f^-g^+d(\mu\times v)\\
&=\left(\int f^+d\mu-\int f^-d\mu\right)\int g^+ dv-\left(\int f^+d\mu-\int f^-d\mu\right)\int g^-dv\\
&= \left(\int f^+d\mu -\int f^- d\mu\right)\left(\int g^+d\nu -\int g^- d\nu\right)\\
&=\int fd\mu\int gdv. \qquad\blacksquare
\end{align}$$
Comment: Could you use paste the latex code here instead of using an image?
Comment: @Abramo Did you give me the -1? How rude! The reason why I did not paste the latex code is because I have various commands set up on my latex documents, and I would have to go through the code and change various parts so that it will show up on stackexchange. Why do you need the code? The math is sufficient, no?
Comment: I think it would be ridiculous to give someone -1 just because it's posted as an image rather than latex code… +1 to cancel the downvote!
Comment: @Abramo I LaTeXified his image, now someone must check if there are any errors. +1 by the way to the answer by Sarah.
Comment: $f(x)g(y)\in\mathbb{R}$ not in $\mathbb{R}\times\mathbb{R}$!
Comment: I'm confuse in part that $(fg)^+-(fg)^-$ becomes $f^+g^++f^-g^--f^+g^--f^-g^+$. I was searching for properties of positive and negative parts of functions and find nothing. To me is really confuse...
Comment: @Mathecm $fg$ is positive when either both $f$, $g$ are positive or when they're both negative.
Answer: @Sarah, here is my proof..... which I think is a little pretty because I don't use sequences.
Let $F(x,y):= f(x) \forall y \in Y$, $G(x,y):=g(y) \forall x\in X$.
Then $$F^{-1}([-\infty, a))=\{(x,y) \in X\times Y : F(x,y)\in [-\infty,a)\}$$
$$=\{(x,y) \in X\times Y : f(x)\in [-\infty,a), \forall y\in Y\}$$
$$=\{x \in X : f(x)\in [-\infty,a) \}\times Y\in \mathcal{M}\times\mathcal{N}$$ as $Y\in \mathcal{N}$ and $\{x \in X : f(x)\in [-\infty,a) \}\in \mathcal{M}$ as $f$ is $\mathcal{M}$-measurable; similarly, we can do this for $g$.
By Tonelli's theorem we have $|F(x,y)G(x,y)|=|f(x)g(y)|=|h|:X\times Y \to [0,\infty]$ that $|h|$ is integrable because it is measurable (product of measurable functions is measurable and absolute value of measurable function is measurable) and nonnegative.  On the other hand, $|h|$ is integralbe if and only if $h$ is integrable (hence $h$ is integrable).
now by Fubini's theorem,because $h$ is integrable and $f$ and $g$ are measurable we have
$$\int_{X\times Y} h d(\mu \times \nu) = \int_X (\int_Y f(x) g(y) d\nu (y) ) d\mu (x) = (\int_X f d\mu )(\int_Y g d\nu )$$


Production server (cluster) configuration

Question: I have a problem, that i'd like to address to you. We are creating an webapp that shows infos and statistics. And we have a problem about how to 'do right' our server configuration
The thing is that our apps use crons to do few things, and crons sometimes blocks our database, and when that happens, whole application is frozen, which means no user input, no pages loading etc.
What should we do to prevent this from happening? is there a way to mirror the database and hold two of them one for backend and one for frontend, or we should limit connections to the database, or maybe we should use some other method.
Any help would be appreciated
Comment: Consider: Make the developers fix their crappy code.
Comment: It's simple: do not lock database. There are plenty of ways, starting from simple read-only replica…
Comment: @AlexeyTen Thanks for your input. I'd like to know one more thing, It's a good solution, but wouldn't copying bigger databases (like for ex. 50gigs+, be time consuming?)

TomTom taken into consideration :D
Comment: You only have to create the base for the replica once, the deltas are much much smaller
Answer: You should make your programmers to fix cron queries, that they do not block mysql. That is the only one right way. If a programmer needs to process a lot data it must run it in a loop in small and fast chunks.


Yii2 - how to refer to main model in a gii(ant) generated view, under a relations tab?

Question: I have a standard giiant generated view where the main model $model is present. First part of the View is a Detailview widget:
<?=
DetailView::widget([
    'model' => $model,

So far clear. Then comes relations tabs (or blocks):
<?php $this->beginBlock('relatedmodels');
echo GridView::widget(['dataProvider' => new \yii\data\ActiveDataProvider(['query' => $model->getRelatedmodels()]),
...
'columns' => [
    [
        'attribute' => 'calculated',
        'value' => function ($relatedmodel) {return $relatedmodel->getCalculated($model->id);},
    ],

This gives of course an error

Undefined variable: model

Controller is also standard giiant generated:
public function actionView($id) {
    \Yii::$app->session['__crudReturnUrl'] = Url::previous();
    Url::remember();
    Tabs::rememberActiveState();

    return $this->render('view', [
        'model' => $this->findModel($id),
    ]);
}

How can I pass the main model id to the function getCalculated() ? Or how to initialise $model into the anonymous function (is it possible at all) ? Can you please point me to the right direction ?
Comment: add controller action  code also.
Comment: `'value' => function ($relatedmodel)  use ($model) {return $relatedmodel->getCalculated($model->id);},`
Comment: Wow, thank you, it's working flawlessly. Is there some kind of documentation about this?
Answer: You need to use Anonymous function - use() and what is a closure and why does it use the "use" identifier?
$this->beginBlock('relatedmodels');
<?= GridView::widget(['dataProvider' => new \yii\data\ActiveDataProvider(['query' => $model->getRelatedmodels()]),
...
'columns' => [
    [
        'attribute' => 'calculated',
        'value' => function ($relatedmodel) use ($model) {return $relatedmodel->getCalculated($model->id);},
    ],

Comment: Thank you! I have tried several different variants but cannot use 2 variables at the same time, e.g. $model and $relatedmodel. Is it a Yii specific thing, or what am I missing?
Comment: I have no idea how I have figured it out, and why it is like this, but this is working: 'value' => function ($mdl) use ($model, $mainmodel) {return $mdl->attr . $model->attr}


auto updating chef client

Question: I've been exploring chef and one of its features is to auto update a client. I'm hesitant to use this feature in production because I don't want to upgrade a node while it is in use. For major changes I would want to route traffic to a different node and then upgrade (eg I wouldn't want to upgrade my app server while it is in use).
I know I can script the updates of my nodes and update on demand but I'm wondering if people are just letting their nodes auto update in production and how they're dealing with upgrading while the sever is in use.
Answer: It Depends™ on how your environment works really.
For example: I was just talking with one of the former sysadmins from Heroku. They have a highly automated system; when an update or something is needed on a node it goes into "maintenance mode". All the traffic gets offloaded to other servers; once that is done it's "Maintenance ready", the updates are applied and it reboots. When it comes back up the other systems recognize it as "ready" and it starts getting traffic again.
The key to how that works, is that each layer in the application "knows" what servers are available and routes traffic intelligently. Unfortunately most vendor supplied applications have extremely little fail-over ability, and it ain't pretty. So you'll probably be stuck with manual processes top to bottom.
Comment: Thanks, the idea of maintenance mode was something that sounds like what I was thinking. Appreciate the input.

auto updating chef client


Simple Linear Regression, Serially Correlated Residuals, and Interpretation

Question: Question
Can I draw any proper conclusions about the linearity and strength of a relationship between two non-stationary time series (I'm considering two series of interest rates, series $A$ and series $B$) using simple linear regression when the residuals are serially  correlated and heteroskedastic?
Background
I see that computing a naive Pearson correlation coefficient (PCC) doesn't help since this calculation yields a random variable regardless of sample size. Since the $\beta$ in simple linear regression is related to the PCC, I think this would also yield a random variable for the coefficient in the model.
Interpretation
If I run a simple linear regression, and despite non-IID residuals, have an $R^2$ of $0.75$, can I say that "series $A$ explains $75%$ of the variability in series $B$"? Or do I have to qualify that statement by saying "series $A$ explains $75%$ of the variability in series $B$ in this particular sample"?  The model is still consistent (p.19), but not unbiased?
Answer: Without stationarity (and therefore ergodicity) you can't assume that the linear regression is a consistent estimator. Basically your observations are not iid.
A linear regression at that point is just a geometric construct, the line that minimizes squared distance, but it doesn't really have statistical meaning.
Answer: I think you need to say "series A explains 75 of the variabilitiy in series B in this particular sample" and if you want to generalize this you need to do cross validation.


Which probability distribution fits my data?

Question: I have generated a dataset (available here) for which I try to find out the best fitting probability distribution.
I first generated uniformly distributed random directions and then calculated the angle between two succeeding directions. The dataset are these angles. You can interpret the random directions as random points sampled on the surface of the unit sphere.
The dataset of random directions is available here: random_dirs.txt
I generated the random directions through polar coordinates $\theta = 2*\pi*u$, $\phi=acos(2v-1)$ (where  $u,v$ are uniformly distributed from (0,1)); this method is also listed on this site.
I then generate normalized direction vectors $\overrightarrow{n_i}=\left(\begin{array}{c}
\sin(\phi)cos(\theta)\\
\sin(\phi)\\
\cos(\phi))
\end{array}
\right)$
The angle between two suceeding directions is then calculated using
random_dirs = load('random_dirs.txt');
rand_dirs_1 = random_dirs(1:end-1,:);
rand_dirs_2= random_dirs(2:end,:);
angles = acos(dot(rand_dirs_1, rand_dirs_2,2));

I tried the normal distribution:
Density (PDF)

Cumulative Probability (CDF)

The CDF matches pretty good, but the PDF could be fitting "better".
Is the data distributed according the normal distribution? How do I verify that? or is there a better matching distribution?
The reason I need to identify a distribution is that I want to compare with other datasets using mean and standard deviation, so I guess first finding out a matching distribution which calculates these measures is necessary.
e.g. this is the same original data, but I applied a spatial sorting algorithm to the random directions and then calculated the angles between neighbouring directions. The angles between the sorted directions are available here.
The sorting uses a space-filling curve (e.g. Hilbert curve) to map the 3D data into 1D space and sort the directions accordingly.

Now I want to compare how much "improvement" in terms of mean and sigma the sorting introduced.
Or are there other measures I could use to express the "improvement"?

How many bins should I use for fitting the data? I used 61 according to "Scott's method" (Scott, David W. (1979). "On optimal and data-based histograms".).
According to "Sturges' method" (Sturges, H. A. (1926). "The choice of a class interval") I should use 18 bins which changes the appearance of the data quite a bit, it looks more like a normal distribution:
Comment: If you're interested in improvement in mean and standard deviation (as you say in the question) ... you could just directly measure the change in mean and in standard deviation.
Comment: Why do you need to identify a distribution?
Comment: How did you generate it?
Comment: @mark999 I updated my question with a description of the generation
Comment: @Glen_b I want to compare other datasets using mean and standard deviation, so I guess first finding out a matching distribution which calculates these measures is necessary
Comment: @Glen_b I added a description how the angles are calculated and a dataset which I want to compare against. I hope this clears things up.
Comment: It would seem that your distribution has thicker tails than a normal distribution so perhaps try a t-distribution at varying degrees of freedom and asess its fit.
Comment: @JohnK I am using MATLAB, but I am unsure which t-distribution you are referring to. Is this the right one? http://mathworks.com/help/stats/t-location-scale-distribution.html
Comment: Yes. Give that a try.
Comment: @JohnK I tried it, but it does not look that much better (see my edit)
Comment: @Johnk those tails are thinner than normal, not thicker. A t-distribution is worse than a normal.
Comment: Please clarify exactly how you calculated the angle; when I calculate the distribution of the angle between two successive uniforms I don't get this, which suggests that there's something unexplained here.
Comment: The distribution of gaps between ordered uniform observations is a problem that has been studied; there are posts here about it; however, there are still things about your circumstances that you need to clarify. If you're just trying to calculate how much improvement there has been, why would you need to identify the distribution?
Comment: @Glen_b Am I missing something? The t-distribution is famous for having thicker tails than the normal.
Comment: @John The empirical distribution of the *data* is thinner tailed than normal. As a result, the heavier-tailed t-distribution is worse.
Comment: @Glen_b yes, you can interpret the random directions as random points sampled on the surface of the unit sphere; I don't necessarily need to express the improvement introduced through the sorting using a probability distribution, I just thought it would a useful measure here. Do you have any other measure I could use?
Comment: @Glen_b this might just be the simplest solution, I did not think about this
Answer: Edit: It now looks like these are random directions on the sphere, in which case the pdf of the distance between random vectors should be $\frac12\sin(\theta);\quad 0<\theta<\pi$. The remainder of this answer is not relevant in that case; I'll probably delete some of it eventually.
I'll need to come back to look at the ordered case to see if I can get anywhere with that.

Looking at your data, I'm a little concerned by some lack-of-fit in your data to the theoretical density:

Given your large sample size, this suggests you may not actually be getting quite a random distribution for some reason. There's slightly too many angles around $\pi/2$ and slightly too few in the region around $\pi/3$ and $2\pi/3$
While the deviation is small, it's large enough to lead to rejection by a Kolmogorov-Smirnov test ($p$ about $1.35\times 10^{-5}$). I'm not sure why that's happening but your simulated s.d. will be slightly too small as well.

(my old answer)

Is the data distributed according the normal distribution?

Your data are not drawn from a normal distribution.

How do I verify that?

You can't. You can sometimes rule it out, as here, but you can't conclude that the data were drawn from a normal distribution on the basis of the appearance of the data.

or is there a better matching distribution?

Oh, an infinite number of them. If you would seek a simple model for data like that, a scaled (i.e. to be on $(0,\pi)$) beta distribution might be adequate, but if you know something about what the values consist of it's likely you can come up with better options.

I try to find out the best fitting probability distribution.

"Best" depends on your criteria. What are we to try to do best at? (e.g. what are you using this to do?)
Comment: It's pretty straightforward. It follows immediately from the distribution of polar angles [here](http://mathworld.wolfram.com/SpherePointPicking.html) (you can just call one of the vectors the pole); there's also a derivation [here](http://math.stackexchange.com/questions/1246748/distribution-of-an-angle-between-a-random-and-fixed-unit-length-n-vectors) for example.
Comment: I think it more like a beta distribution
Comment: how do you know the PDF of the random directions is $\frac12\sin(\theta);\quad 0<\theta<\pi$? can you point me to any source please? I did not think of a sine/cosine distribution, but now that you mention it: should I go with the [Raised cosine distribution](https://en.wikipedia.org/wiki/Raised_cosine_distribution)?
Comment: thanks for the links, they really cleared things up. which additional explanation do you need? I will gladly update my question with that necessary information. The sorting uses a space-filling curve (e.g. Hilbert curve) to map the 3D data into 1D space and sort the directions accordingly.
Comment: I added this detail also to my question; what do you mean by "simulate the other one"?
Comment: Just what you already have done (but if you need additional accuracy, replicated as many more times as you need).
Comment: @Glen_b regarding your edit about the lack-of-fit: the random directions are generated through the polar coordinates $\theta = 2*\pi*u$, $\phi=acos(2v-1)$ (where  $u,v$ are uniformly distributed from (0,1)); this method is also listed on the [page you linked to](http://mathworld.wolfram.com/SpherePointPicking.html). (I added this detail to the question as well); do you see any problem with my method?
Comment: I hadn't noticed it there at the link, but I derived it myself before I posted that update; when I generated my own values that way, I got a very close match to the sin distribution. Is it possible your original random directions in the file aren't quite random? Incidentally, look at the cos of your original data. If those data are angles between random 3-vectors, that $\cos(\theta)$ should look uniform, but it's not.
Comment: I generated another dataset (available [here](http://s000.tinyupload.com/index.php?file_id=57533877486997760908)) and it fits the theoretical pdf better: http://i.imgur.com/O0gwoj7.png; I don't know if I executed the kstest correctly, it reports `p = 0.0124`; can you please give an example how you generated your data and executed the test? it does not have to be in MATLAB, I'd just like to see how it's done
Comment: I went by the $\text{acos}(2v-1)$ approach. That's already directly from the sin distribution as long as you have uniforms, since it's actually the inverse cdf method for generating the sin distribution.
Comment: Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/27070/discussion-between-m-s-and-glen-b).
Answer: For a one-dimensional distribution of angles, you might look into the von Mises distribution. One thing the von Mises distribution does is account for the equivalence between the endpoints of your domain, e.g. an angle of 0 is the same as an angle of $2\pi$.
There are other 1-dimensional distributions that have this quality - for example you could "wrap" the density of pretty much any continuous distribution onto the range 0 to $2\pi$ - see "wrapped Normal distribution", or wrapped t, or wrapped Cauchy.
Comment: Being angles between 3D vectors, these angles are on the half-circle.


Proper use of DBNull

Question: Can someone inform me of the proper use of DBNull?  I'm developing an MVC3 project which has this query in the controller.
I have a linq query that returns a field that I know has no value on the first cycle:
var qryGetBirthdate = (from c in db.Customer
                              select new
                                   {
                                      c.Birthdate
                                   }
                              ).FirstOrDefault();

In the database, Birthdate is a Datetime type which is null.
The check I do for this is wrong:
 if ( !DBNull.Value.Equals(qryGetBirthdate.Birthdate))
           {
               DateTime? dob = qryGetBirthdate.Birthdate;
           }

I've tried variations from other blogs but can't find the answer.  Basically what I want is to catch the fact that 'qryGetBirthdate.Birthdate' doesn't exist yet.  All I'm getting is the exception that the object isn't set to a reference which I know.
What am I doing wrong?
Answer: FirstOrDefault returns null for a reference type if the sequence is empty. That's the reason for the NullRefernceException at qryGetBirthdate.Birthdate. So check if it's null:
if(qryGetBirthdate != null)
{
    // now you can access it safely
}

However, you don't need to create the anonymous type.
DateTime? firstBirthdate = (from c in db.Customer
                             select c.Birthdate).FirstOrDefault();

Comment: That's it!  Thanks Tim.  I realise now I shouldn't be using DBNull with L2S either.   I posted the last comment before you added' "However..."  I like the cleaner version, I'll do it this way.
Answer: If BirthDate is a nullable Datetime SQL type then c.Birthdate should be a DateTime? in which case you would use:
DateTime? dob = qryGetBirthdate.Birthdate;

Comment: My point was you can't compare it to DBNull - you can remove the `if` clause.
Answer: Have you tried the following.  The variable is probably null and not dbnull.
 if (qryGetBirthdate != null && qryGetBirthdate.BirthDate != null && !DBNull.Value.Equals(qryGetBirthdate.Birthdate))
            {
                DateTime? dob = qryGetBirthdate.Birthdate;
            }

Proper use of DBNull


Cannot import barba.js in sveltekit

Question: This is my +layout.svelte file:

<svelte:body data-barba="wrapper" />

  <slot />

The code results in a 500 internal error:
TypeError: Cannot read properties of undefined (reading 'default')
    at +layout.svelte:4:2

Demo : Bug Demo
Answer: Try adding barba.init({}); inside onMount.

        <slot />

I made some modifications to your demo sandbox

Cannot import barba.js in sveltekit


How do I append the occurrence of a field using awk?

Question: I am trying to append a number to a record that indicates how many times a particular value has occurred in a particular column within a file.  For example, let's say my file looks like this:
John,Guitar
John,Vocals
John,Piano
Paul,Bass
Paul,Vocals
George,Guitar
George,Vocals
Ringo,Drums

What I would like is to output a file that looks like this:
John,Guitar,1
John,Vocals,2
John,Piano,3
Paul,Bass,1
Paul,Vocals,2
George,Guitar,1
George,Vocals,2
Ringo,Drums,1

How do I achieve this in awk?  I would also be open to an answer using a shell script, or sed, as I'm just guessing that awk might be a good way of handling this.
Answer: Awk solution:
awk 'BEGIN{ FS=OFS="," }{ $0=$0 OFS (++a[$1]) }1' file

The output:
John,Guitar,1
John,Vocals,2
John,Piano,3
Paul,Bass,1
Paul,Vocals,2
George,Guitar,1
George,Vocals,2
Ringo,Drums,1

The same with print operator:
awk 'BEGIN{ FS=OFS="," }{ print $0,(++a[$1]) }' file

Comment: @jthill, *the BEGIN construction is usually unnecessary* - that's pretty subjective opinion
Comment: more of the same :) `awk -F"," '$0=$0","++a[$1]' file`
Comment: By the way, the `BEGIN` construction is usually unnecessary.  `awk {$++NF=++a[$1];print}' FS=, OFS=, file` is all you need here.
Answer: With awk:
awk 'BEGIN{FS=OFS=","} {print $1,$2,++counter[$1]}' file

Output:

John,Guitar,1
John,Vocals,2
John,Piano,3
Paul,Bass,1
Paul,Vocals,2
George,Guitar,1
George,Vocals,2
Ringo,Drums,1
Answer: Another way with awk
awk '{b=$0;sub(",.*","");$0=b","++c[$0]}1' infile


Are different p-values for chi-squared and z test expected for testing difference in proportions?

Question: I'm trying to test the difference in proportions using the z test method and chi-squared method, but am getting very different answers. Is that normal?
My data:
<code>        CI     CII
Male    205    102
Female  83     39
</code>
Calculating the z score I get 0.25 which should correlate to a p-value of 0.4013.
Calculating the chi-squared score I get 0.0626 correlating to a p-value of 0.8025.
I read that the z-score requires some assumptions (probability of success is ~0.5 and n is high). Is this violating those? Or is it just the nature of these different approaches that gives very different answers with the same meaning (no evidence of difference).
I'm certainly open to miscalculations, but I've re-checked. If this behaviour isn't normal I'll recheck again!
Here are my calculations in R.
<code>> r1 <- 205
> r2 <- 102
> n1 <- 288
> n2 <- 141
> (p1 <- r1/n1)
[1] 0.7118056
> (p2 <- r2/n2)
[1] 0.7234043
> (common.proportion <- (r1+r2)/(n1+n2))
[1] 0.7156177
> (se.pooled <- sqrt(common.proportion*(1-common.proportion)*(1/n1+1/n2)))
[1] 0.0463676
> (zscore <- (p1-p2)/se.pooled)
[1] -0.2501466
>
> # chi-squared
> prop.test(c(205,102), c(288,141), correct = FALSE)

    2-sample test for equality of proportions without continuity
    correction

data:  c(205, 102) out of c(288, 141)
X-squared = 0.0626, df = 1, p-value = 0.8025
alternative hypothesis: two.sided
95 percent confidence interval:
 -0.10208385  0.07888645
sample estimates:
   prop 1    prop 2
0.7118056 0.7234043
</code>
Comment: Tom, can you show your math? These two tests should give very similar results for your sample sizes (especially if making the same choice about continuity corrections).
Comment: A related issue. If I'm presenting confidence intervals around each proportion calculated using the binomial distribution, but then comparing them and presenting a p-value using chi-squared, that seems a bit wrong. I might have overlapping CIs, but then a p-value that's less than 0.05. Am I thinking correctly?
Answer: Very simple: both the z test and the contingency table $\chi^{2}$ test are two tailed tests, but you have got the one-sided $p$-value for your z test statistic. That is for $H_{0}: p_{1} - p_{2} = 0$, the $p$-value = $P(|Z| \ge |z|)$, but your reported $p$-value is only $P(Z \le z)$.
Notice that $0.4013 \times 2 \approx 0.8025$. Easy!
Comment: And the square of the *Z*-score is (−0.2501466)² = 0.06257, which equals the test statistic `X-squared` from the `prop.test()` output.
Comment: @Tom The reason that it's called `X-squared` in the R output, is that the X is really an ASCII interpretation of the greek capital letter chi (Χ) (the lowercase version of this letter looks like this: χ). And it's a *chi-squared* test, which is used in lots of other situations. That said, the test(s) should never be used for comparing binomial proportions, as they have terrible statistical properties. See https://stats.stackexchange.com/questions/82720/confidence-interval-around-binomial-estimate-of-0-or-1/82724#82724
Comment: Thank you for that "easy" answer! (Which prompted a good schooling on one-tailed tests. And now @KarlOveHufthammer you're sending me down another schooling. If `x-squared` is simply the z score squared, why do we even have it? And why isn't it called `z-squared`? (Obviously, not to be answered here. I have a lot to learn!)


What is the best way in python to get a denormalized array from this ordered array?

Question: I have this array:
>>> print raw_data
['LEVEL 1',
'SUBJECT A',
'GROUP X',
'COMMENT i',
'COMMENT ii',
'COMMENT iii',
'GROUP Y',
'COMMENT iv',
'COMMENT v',
'COMMENT vi',
'LEVEL 2',
'SUBJECT B',
'GROUP Z',
'COMMENT vii',
'COMMENT viii',
'COMMENT ix',
'SUBJECT C',
'GROUP X2',
'COMMENT x',
'COMMENT xi',
'COMMENT xii',
'COMMENT xiii',
'GROUP Y2',
'COMMENT xiv',
'COMMENT xv',
'COMMENT xvi']

Where the obvious hierarchy is:

Level

Subject

Group

Comments

My objective is to get the array as a denormalized array to be store on a database:
>>> print result
[
    ['LEVEL 1', 'SUBJECT A', 'GROUP X', 'COMMENT i'],
    ['LEVEL 1', 'SUBJECT A', 'GROUP X', 'COMMENT ii'],
    ['LEVEL 1', 'SUBJECT A', 'GROUP X', 'COMMENT iii'],
    ['LEVEL 1', 'SUBJECT A', 'GROUP Y', 'COMMENT iv'],
    ['LEVEL 1', 'SUBJECT A', 'GROUP Y', 'COMMENT v'],
    ['LEVEL 1', 'SUBJECT A', 'GROUP Y', 'COMMENT vi'],
    ['LEVEL 2', 'SUBJECT B', 'GROUP Z', 'COMMENT vi'],
    ['LEVEL 2', 'SUBJECT B', 'GROUP Z', 'COMMENT vii'],
    ['LEVEL 2', 'SUBJECT B', 'GROUP Z', 'COMMENT viii'],
    ['LEVEL 2', 'SUBJECT B', 'GROUP Z', 'COMMENT ix'],
    ['LEVEL 2', 'SUBJECT C', 'GROUP X1', 'COMMENT x'],
    ['LEVEL 2', 'SUBJECT C', 'GROUP X1', 'COMMENT xi'],
    ['LEVEL 2', 'SUBJECT C', 'GROUP X1', 'COMMENT xii'],
    ['LEVEL 2', 'SUBJECT C', 'GROUP X1', 'COMMENT xiii],'
    ['LEVEL 2', 'SUBJECT C', 'GROUP Y2', 'COMMENT xiv'],
    ['LEVEL 2', 'SUBJECT C', 'GROUP Y2', 'COMMENT xv'],
    ['LEVEL 2', 'SUBJECT C', 'GROUP Y2', 'COMMENT xi']
]

I was trying to solve this, but I am quite lost, I think this problem has to be usual, so I would like to know if someone has a efficient approach, this seems to be something like nested sets, but I don't know a lot of this on python, getting the level is easy, but I am getting " headaches" getting this further.
>>> def addlevel(a):
    if a.startswith('LEVEL'):
        return [1, a]
    elif a.startswith('SUBJECT'):
        return [2, a]
    elif a.startswith('GROUP'):
        return [3, a]
    elif a.startswith('COMMENT'):
        return [4, a]
>>> map(addlevel, raw_data)
[[1, 'LEVEL 1'],
 [2, 'SUBJECT A'],
 [3, 'GROUP X'],
 [4, 'COMMENT i'],
 [4, 'COMMENT ii'],
 [4, 'COMMENT iii'],
 [3, 'GROUP Y'],
 [4, 'COMMENT iv'],
 [4, 'COMMENT v'],
 [4, 'COMMENT vi'],
 [1, 'LEVEL 2'],
 [2, 'SUBJECT B'],
 [3, 'GROUP Z'],
 [4, 'COMMENT vii'],
 [4, 'COMMENT viii'],
 [4, 'COMMENT ix'],
 [2, 'SUBJECT C'],
 [3, 'GROUP X2'],
 [4, 'COMMENT x'],
 [4, 'COMMENT xi'],
 [4, 'COMMENT xii'],
 [4, 'COMMENT xiii'],
 [3, 'GROUP Y2'],
 [4, 'COMMENT xiv'],
 [4, 'COMMENT xv'],
 [4, 'COMMENT xvi']]

I would appreciate any clues !
Answer: Pseudocode, don't have a handy python interpreter right now:
Set LEVEL, SUBJECT, GROUP to None, results to []

Loop over the list
  if its a 'LEVEL', set LEVEL to it
  if its a 'SUBJECT', set SUBJECT to it
  if its a 'GROUP', set GROUP to it
  if its a "COMMENT", append [LEVEL SUBJECT GROUP and COMMENT] to results
Ta-da.

It just relies on the ordering...
Comment: There should be a `Ta-da` command in Python. :) Anyway, good snippet.
Answer: You could try something like this:
raw_data = [ 'LEVEL 1', 'SUBJECT A', 'GROUP X', 'COMMENT i', 'COMMENT ii',
'COMMENT iii', 'GROUP Y', 'COMMENT iv', 'COMMENT v', 'COMMENT vi', 'LEVEL 2',
'SUBJECT B', 'GROUP Z', 'COMMENT vii', 'COMMENT viii', 'COMMENT ix',
'SUBJECT C', 'GROUP X2', 'COMMENT x', 'COMMENT xi', 'COMMENT xii',
'COMMENT xiii', 'GROUP Y2', 'COMMENT xiv', 'COMMENT xv', 'COMMENT xvi' ]

level, subject, group, comment = '', '', '', ''

result = []

for item in raw_data:

    if item.startswith('COMMENT'):
        comment = item
    elif item.startswith('GROUP'):
        group = item
        comment = ''
    elif item.startswith('SUBJECT'):
        subject = item
        group = ''
    elif item.startswith('LEVEL'):
        level = item
        subject = ''

    if level and subject and group and comment:
        result.append([level, subject, group, comment])

import pprint
pprint.pprint(result)

Which would yield:
[['LEVEL 1', 'SUBJECT A', 'GROUP X', 'COMMENT i'],
 ['LEVEL 1', 'SUBJECT A', 'GROUP X', 'COMMENT ii'],
 ['LEVEL 1', 'SUBJECT A', 'GROUP X', 'COMMENT iii'],
 ['LEVEL 1', 'SUBJECT A', 'GROUP Y', 'COMMENT iv'],
 ['LEVEL 1', 'SUBJECT A', 'GROUP Y', 'COMMENT v'],
 ['LEVEL 1', 'SUBJECT A', 'GROUP Y', 'COMMENT vi'],
 ['LEVEL 2', 'SUBJECT B', 'GROUP Z', 'COMMENT vii'],
 ['LEVEL 2', 'SUBJECT B', 'GROUP Z', 'COMMENT viii'],
 ['LEVEL 2', 'SUBJECT B', 'GROUP Z', 'COMMENT ix'],
 ['LEVEL 2', 'SUBJECT C', 'GROUP X2', 'COMMENT x'],
 ['LEVEL 2', 'SUBJECT C', 'GROUP X2', 'COMMENT xi'],
 ['LEVEL 2', 'SUBJECT C', 'GROUP X2', 'COMMENT xii'],
 ['LEVEL 2', 'SUBJECT C', 'GROUP X2', 'COMMENT xiii'],
 ['LEVEL 2', 'SUBJECT C', 'GROUP Y2', 'COMMENT xiv'],
 ['LEVEL 2', 'SUBJECT C', 'GROUP Y2', 'COMMENT xv'],
 ['LEVEL 2', 'SUBJECT C', 'GROUP Y2', 'COMMENT xvi']]

Comment: simple ! I was trying to do something related with nested sets, but this seem more simple
Comment: Bug!  ` ['LEVEL 2', 'SUBJECT A', 'GROUP Y', 'COMMENT vi'], ` should not be in the results.  For each level found, you need to erase all the sublevels.
Comment: @Paul Thanks for spotting the bug (and for pyparsing, btw ;) - corrected my post.


When does $\mathrm{End}(X)\cong X$?

Question: I noticed in lecture that for any field $\mathbb F$, take the one-dimensional vector space $V$ over $\mathbb F$ and write $\mathbb F\cong V\cong V^\ast=\mathrm{Hom}(V,\mathbb F)\cong\mathrm{Hom}(\mathbb F,\mathbb F)=\mathrm{End}(\mathbb F)$. This is a very basic case, so I'm just wondering, more generally, for what kinds of spaces $X$ is $\mathrm{End}(X)\cong X$?
Comment: This is true for all commutative rings: $\operatorname{Hom}_R(R,R)\cong R^{op}$ for all rings, where the Hom is as $R$-modules. Commutativity gives $R\cong R^{op}$, so there you go.
Comment: In that case, your first problem is that your endomorphism object is rarely the same kind of object as $X$.
Comment: Are you restricting your question to vector spaces?
Comment: Definitely not! I was trying to show this for certain types of rings, but totally failing.
Comment: I'm pretty embarrassed I missed such an immediate proof. My thought was that, since all fields are rings, I should start with, say, integral domains and try to use its properties.
Comment: @user10486601  This isn't something I'd be embarrassed over.
Answer: Let $R$ be any ring (commutative, with 1). Then $\text{End}_{R-\text{modules}}(R) = R$: any endomorphism $\phi$ is uniquely determined by $\phi(1)$ since $\phi(r) = r\phi(1)$, and $\phi(1)$ can be set to anything in $R$.
Answer: I don't know what you mean by "spaces," but here's a very general setup where you have an isomorphism of this sort. Consider a closed monoidal category, namely a category $M$ with a monoidal structure $\otimes$ and an internal hom $[-, -]$ fitting into an adjunction
$$\text{Hom}(a, [b, c]) \cong \text{Hom}(a \otimes b, c).$$
$\text{Vect}$ is such a category, with $\otimes$ the tensor product and $[-, -]$ the vector space of linear maps between two vector spaces. More generally, for $R$ a commutative ring, $\text{Mod}(R)$ is such a category, with $\otimes$ the tensor product over $R$ and $[-, -]$ the $R$-module of $R$-module homomorphisms between two $R$-modules.

Proposition: The unit object $1$ always satisfies $1 \cong [1, 1]$. More generally, every object $b$ satisfies $b \cong [1, b]$.

Proof. Follows immediately from the adjunction $\text{Hom}(a, [1, b]) \cong \text{Hom}(a \otimes 1, b) \cong \text{Hom}(a, b)$ together with the Yoneda lemma. $\Box$
Comment: A slightly stronger statement is that the unit object carries a canonical monoidal structure induced by the unit isomorphism $1 \otimes 1 \cong 1$, and we even have $1 \cong [1, 1]$ as monoids.

When does $\mathrm{End}(X)\cong X$?


is it possible to have Web server and NAS on the same hardware microsever?

Question: I have a HP microserver proliant gen 8, and i want to know if there's a way to make this hardware work as NAS with freeNAS ... etc, and in the same time make it work as a web server with Centos Nginx ? please describe in steps.
Thanks!
Answer: Yes. Install virtualization. Try Hyper-V or ESXi.
Comment: Chris thank you very much for you answer, don't know why all the people down voted me for, however can you please explain more the idea, because i'm a bit new in sysadmin stuff, also i prefer linux solution than Microsoft :)
Comment: I wouldn't recommend ESXi for a ProLiant Microserver. Pretty poor hardware support for the storage controller.
Answer: Sure, but rather than attempting to virtualize on such limited hardware, I'd suggest just running CentOS to handle both functions. There's probably no reason to use FreeNAS here, as CentOS can serve as a NAS and is entirely capable of ZFS.
This would be the best use of the resources available to your ProLiant Microserver.
Comment: Thank you very much for you answer, and on Centos can i make auto-sync and functions of NAS? i would like to link all my device, is this a possibility ?
Comment: Autosync to what?
Comment: phones or pc to the NAS, like a cloud for example ...
Comment: I'm not sure that this solution will become a "cloud", but it *can* provide storage to your local network.


Confidence intervals for predictors in multivariate logistic regression

Question: I've got a question.
I am dealing with medical data which contain 5 predictors and 1 binary outcome. When I try to classify the data using all 5 predictors I get 0.84 area under roc-curve which is quite good for me. However when I consider confidence intervals for these 5 parameters, I see that just 1 of them is significantly different from 0, its confidence interval doesn't include value "1". For all other predictors confidence interval include value 1 (from 0.78 to 1.2, for example).
When I try to classify the dataset based on just this 1 predictor (the one with good confidence interval), I still have good confidence interval for it but the area under roc-curve is 0.74 and combinations of specificity and sensitivity are really worse than in first example.
The question is: how should I treat the situation when not all of the predictors are significantly different from 0? Can I consider such deciding rule as a good one and use it in further analysis?
Thanks a lot!
Answer: Much has been written about your issues on this site.  Briefly, statistical significance testing is not a good way to develop a multivariable regression model (note that multivariate would refer to more than one dependent variable).  A possible exception is using a very liberal $\alpha$ level such as 0.5 in a backwards step-down approach.  The higher $\alpha$ is the less damage to statistical inference and prediction.  But often better is to use penalization or data reduction.  The latter is easiest to implement, using tools such as variable clustering and redundancy analysis, all blinded to $Y$ to avoid bias/overfitting.
You might also use bootstrap validation for your 5-variable model to quantify how much of the $c$-index (area under ROC curve in this special case) is due to overfitting.
In the R package rms some of the functions that implement the above suggestions are lrm, validate, calibrate, and in the Hmisc package, varclus and redun.
Note that you are making strong linearity assumptions.
Comment: Significance of predictors (equivalent to CL excludeing 1.0) has little to do with predictive accuracy.  Estimate the $c$-index by postulating a model and fitting it.  It is OK to look at $P$ and CL as long as no action is taken.  Assess overfitting by using the heuristic shrinkage estimator $\hat{\gamma}$ (model likelihood ratio $\chi^2$ minus 5 divided by $\chi^2$ in your case) or by bootstrap validation.  If $\hat{\gamma} > 0.85$ you don't have much to worry about regarding overfitting.  If you have overfitting you cannot use $P$-values or confidence intervals to do anything about it.
Comment: Thanks for the comment! I saw many questions regarding confidence intervals in logistic regression but none of them was dedicated to estimating AUC for the situation of not-significant predictors. To clarify: does the situation when the AUC for model is high but confidence intervals of some of predictors include value "1" mean the overfitting? Or there may be any other "good" reason for that?


Como funciona o método equals() em Java?

Question: Na hora que eu vou usar o método equals() eu coloco um objeto do tipo Person e ele funciona, por que?
Como ele funcionam sendo que o objeto que eu passei não é do tipo Object?
Eu sei que tudo isso tem haver com a classe Object.
    @Override
    public boolean equals(Object obj) {
    Person contaDiferente = (Person) obj;
    if (this.salario == contaDiferente.salario) {
        return true;
    } else {
        return false;
    }

Comment: No java, todo objeto estende da classe [Object](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html)
Comment: @NaranTeodoro, acredito que a resposta [aqui](https://pt.stackoverflow.com/a/195006/6789) pode ajudar na compreensão da sua pergunta.
Answer: Isso chama-se polimorfismo. Veja a definição deste conceito. Ele permite o reuso de código.
Hierarquia de tipos
Então todos os tipos que você cria, no seu exemplo o Person, são ao mesmo tempo outros tipos. Pelo menos em Java sempre tem um outro tipo nos tipos por referência criados como uma classe.
Em Java só pode ter um tipo que herda uma estrutura de um tipo (classes), mas pode herdar de vários tipos que não formam um objeto completo (interfaces), ou seja, que não tem estado (variáveis), só comportamentos (métodos) e contratos (assinatura de métodos).
Entenda a diferença entre subclasse e subtipo.
Para garantir que uma nova classe sempre tenha um tipo ele é implicitamente herdado de Object. Esse é o tipo raiz do Java, ele tem alguns métodos já implementados que o seu tipo sempre terá em função de ter herdado de Object (veja a documentação quais são eles).
Um destes métodos é o equals(). geralmente usamos o @override.
Se você herdar de outro tipo ele não estará herdando diretamente de Object, mas indiretamente sim, porque se o seu tipo herda de um tipo (sempre classe) que deve obrigatoriamente ter herdado de Object, então na prática herda de Object também.
Sobreposição
Claro que os métodos podem ter uma implementação diferente. O tipo que você está herdando pode ter sobreposto o que o Object fazia. Assim como o seu tipo pode sobrepor.
A sobreposição é bastante útil para permitir que você execute o método de acordo com a necessidade do seu tipo.
Polimorfismo aplicado
Então quando você for escrever um método que precise lidar com alguma característica de Object não precisa escrever um para cada tipo que criar, pode criar um que recebe um objeto qualquer, parâmetro declarado genericamente como Object, e ele consegue lidar com aquilo porque ele é um Object, além de ser o seu próprio tipo (Person). Ele pode ser outros tipos ao mesmo tempo também. Ou seja, ele pode assumir várias formas, ele tem várias facetas.
Casting
Mas note que no seu caso só funciona porque está usando um cast. Você está acessando o membro salario de obj. Você sabe que Person tem esse membro. Mas ele está recebendo um Object, e você sabe que este tipo não tem o membro salario. A única forma do compilador aceitar que você acesse salario é se você transformar o objeto em Person. O `cast  faz isso.
Como dá certo? Porque o objeto concreto que foi passado para este método é um Person, ele recebe como Object, mas de fato é só uma faceta dele, existe uma "pessoa" ali dentro do objeto. Se tentar acessar um membro do Object, funciona. Se tentar acessar um membro de Person` não funciona, mesmo que você saiba que tem o membro lá. Você tem que instruir o compilador aceitar o que você deseja.
Mas pensa bem. E se você passar um Object mesmo, ou um Product, o que acontece com o código quando ele tenta fazer o cast? Ele tentará transformar um Product em Person. O objeto concreto recebido como Object não tem os membros de Person e o código falhará se tentar acessá-los.
Mas pode acontecer algo pior. Seu Person tem um name e o Product também tem um name. Mas são nomes bem diferentes, um de uma pessoa e o outro é de um produto, você não pode usar uma como se fosse o outro. Para isso definiu-se que sua aplicação já impede o erro no casting e um objeto do novo tipo não existirá. O casting  gerará um objeto nulo.
O correto é verificar se a transformação deu certo, caso contrário é um erro de programação seu.
Eu não gosto dessa forma de resolver as coisas, mas é a forma que o Java tem. É ineficiente, obriga tomar muitos cuidados e é fácil cometer um erro. Quebra a segurança de tipos que a linguagem propala como sendo um de seus trunfos.
Eu respondi uma pergunta com um exemplo mais seguro.
Seu código ficaria melhor assim:
@Override
public boolean equals(Object obj) {
    // Um objeto é sempre igual a ele mesmo
    if (this == obj) return true;
    // Um objeto nunca deve ser igual a null
    /* Uma pessoa só pode ser igual a outra pessoa.
     * Caso uma pessoa possa ser igual a uma de suas subclasses
     * use !(obj instanceof Person)
     */
    if (obj == null || getClass() != obj.getClass()) return false;
    // Converte a referencia para uma pessoa
    final Person other = (Person)obj;
    //alguma falhar ocorrer na transformação
    if (othe == null) return false;
    // Elas possuem o mesmo salario
    return this.salario == contaDiferente.salario;
}

Coloquei no GitHub para referência futura.
Nem todos objetos em Java derivam de Object
Nem todos os objetos em Java derivam de Object, pelo menos até a versão 15 )ou superior, estão prometendo desde a 9). Algo vai mudar em Java futuro que terá a possibilidade de criar tipos por valor que derivam de Object. Não se sabe ainda se os tipos atuais por valor que não derivam de Object vão passar derivar ou não. Então um Integer deriva de Object, mas um int não deriva. Isso tem implicações importantes que não vem ao caso aqui.
Conclusão
Alguma coisa me diz que este método está semanticamente errado. Isso não deveria comparar salários, ele deve estar preocupado com a identidade do objeto e salário não faz parte da sua identidade. E algo me diz que o tipo de salário não é o adequado para guardar um valor monetário.
Cuidado com informações que pega na internet, você nunca sabe o comprometimento que o autor tem em passar informação correta para você, e ele não conhece bem o seu contexto. Por exemplo, eu não sei se entende tudo o que estou escrevendo, o que dá margem para novas perguntas, mas não posso escrever um livro completo sobre o assunto, vai aprendendo uma coisa por vez.
Answer: Funciona porque qualquer instância/objeto do tipo Person (ou de qualquer outra classe) também é um Object.
De forma implícita, todas as classes Java herdam de Object.
Por isso que quando você acaba de criar uma classe ela já "vem com" os métodos:
clone()
equals(Object obj)
finalize()
getClass()
hashCode()
notify()
notifyAll()
toString()
wait()
wait(long timeout)
wait(long timeout, int nanos)

Pois esses são herdados da classe Object
Comment: então seu eu criar uma classe e as de mais (filhas) fazer um extends dela, eu posso fazer esse mesmo esquema, ou só funciona com a classe Objects ?
Comment: Pode fazer esse mesmo esquema. **Todas** classes em Java herdam de Object. Se o parâmetro do seu método for to tipo `Object` você pode passar qualquer tipo
Answer: No java, todo objeto estende da classe Object. Mesmo quando não utilizando explicitamente o extends Object, o Java, automaticamente, faz esse processo.
Hierarquia de uma classe:

Essa classe raiz é definida no pacote java.lang, ela define e implementa os métodos clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString e wait.


MouseLeftButtonUp never called after DragMove

Question: I have an element in XAML used to trigger a DragMove when you click on the element and hold down the mouse button.
<code><Grid>
    <CheckBox x:Name="myCheckBox" IsChecked="{Binding IsDragging}">I am red square dragging!</CheckBox>
    <Rectangle x:Name="myRect" Width="100" Height="100" Fill="Red"/>
</Grid>
</code>
The event handling is done code-behind the following way:
<code>    protected override void OnInitialized(EventArgs e)
    {
        base.OnInitialized(e);

        myRect.MouseLeftButtonDown += myRect_MouseLeftButtonDown;
        myRect.MouseLeftButtonUp += myRect_MouseLeftButtonUp;
    }

    void myRect_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        IsDragging = false;
    }

    void myRect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        IsDragging = true;
        DragMove();
    }
</code>
Even though dragging works, myRect_MouseLeftButtonUp is never called so IsDragging is never set to false after it is set to true. How do I make this work?
As a bonus, here is a gratuitous image of the element to coax you to answer this question.
Answer: DragMove is a blocking event ending with MouseLeftButtonUp. The correct behavior can be induced using
<code>    void myRect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        IsDragging = true;
        DragMove();
        myRect.RaiseEvent(new MouseButtonEventArgs(e.MouseDevice, e.Timestamp, MouseButton.Left)
        {
            RoutedEvent = MouseLeftButtonUpEvent
        });
    }
</code>
Also see C# WPF - DragMove and click


"Along the way" ear training in your daily routine

Question: I am an amateur musician, not good at ear training. But I want to improve, unfortunately my time is limited as music is just a hobby. So in this post the author suggests doing ear training in your daily schedule, simply by listening to the music that is around you and figuring melody and harmony out.
But how to implement that if you are still at the beginning? I mean when I hear something I can make a guess, and sometimes I am right, sometimes not, I don't know. For example if I am at the gym on the rowing machine as the author suggests, how to know that I am right? When I try to figuring out songs with my instrument I can match my guesses with the instrument and try to play around. So without feedback how to implement this learning strategy?
Do you have any suggestions on how to incorporate ear training in your daily schedule, apart from dedicated practice time, especially if you are at the beginning?
Answer: Unless you have perfect pitch you'd need at a minimum an app that has a piano keyboard so you can match the note with the sound you're hearing.
An important thing to do is listen for the key of the song. This is also called the tonic of the scale. Once you find the key, you will essentially know the 7 diatonic notes that belong to that scale. With that you can derive both the melody and harmony of what you're listening to.
The key is usually the last note the song resolves to.
So say you're going about your day and you listen to Shape of You. If you go to the very last note in the song, the key that it resolves to, is "YOU" at 3:56. The you that he sings, if you had a trusty piano app to compare it with, is in C#. His voice is the melody here. Another way, without going to the end of the song, is the root note of the first chord of the song also can indicate the key. And yet another way, is just by hearing it throughout the song, all notes want to resolve to it. You get better at identifying the key with experience.
In the beginning of the song you'll notice that there's a few notes. It starts off with: C# E C# C# E... This is a minor third interval. The note E is not in the C# major scale. So the song is in C# minor. The four chords it uses are C#m F#m A B. which are the i iv VI VII diatonic chords of the scale.
Take a look at this link it breaks down how the chords of the scale (harmony) relate to the notes of the scale (melody):
https://www.hooktheory.com/theorytab/view/ed-sheeran/shape-of-you
This is just a single example but alot of songs work this way. Most songs you'll find to be in a major key though.
So as you see, in order to figure out the song we had to use a bit of theory and we had to figure out what the key was.
The link that you posted is that of an advanced musician, he actually has a cool channel on youtube called Jazz Tutorial that I watch. It takes years of practicing your instrument, understanding scales, chords, theory, and ear training to be able to transcribe by ear throughout the day as was mentioned in that article.
Comment: +1 for mentioning that the link is by an advanced musician.  It's not always easy to tell whether a particular technique or method is suitable for one's level of learning.
Answer: I'd consider "ear training" to be a very wide set of skills. OP said they were "not good at ear training". But surely OP can recognise some things, right? I mean, you don't have to be a musician to recognise certain songs when you hear them, so the idea would be start from the easy things to recognise by ear, like say:

What song it is (if you know it already)
Whether the song is in minor or major (usually pretty doable)
What time signature the song is written in

Once you can recognise simple things like that (or more likely, you already can, depending on how serious this hobby is), then move on to harder things, like:

What the tonic of the song is (knowing what it sounds like when you hear it, not what note name it is, this will be very important)
What the tonic chord, subdominant chord, and dominant chord is (again, sound, not name)
Hearing some intervals
Chord qualities

This is where the stuff starts to get useful. When you do happen to have the instrument with you, you can verify to see whether you were right, or just work on it without the instrument. Eventually, one's goal should be to:

Recognise scale degrees
Recognise most chords
Be able to put together the harmony from this information

And now you can basically play by ear. Note that this will be much easier depending on one's knowledge of theory, and these skills will take time to develop. Simply by exposing oneself to music can one manifest ability over time.
Answer: I don't think that you can do ear training solely from the music around you without first having done some theory study and more formal ear training.  You don't stand a chance of recognizing the chord sequence (for example) of a song if you don't know what common chord sequences sound like already.
I do ear training (the more formal kind) in my car while I am driving.  What you need are ear training recordings that are not interactive.  These are recordings you play on your phone or car stereo and then just drive.  Since they are not interactive, you don't have to look at or touch anything--it's all just listening and singing.
Here are a few ear training recordings I know that are non-interactive (don't require you to interact with your phone or read a book):

Ear Training on the Run - Two Note Intervals - CD, or download audio files (I forget which)
Ear Training HQ.  Online, but you can download MP3 files.

And an app with a non-interactive mode:

Functional Ear Trainer - PC, Android, iPhone. - You can start an exercise in a looping, non-interactive mode and then just listen to it while you drive.

These are the ear training courses I am aware of that you can listen to while driving.  There are, I am sure, many others.
Once you've got some basic theory and formal ear training, then you will start to hear things in the music around you, and can practice that kind of ear training.
Comment: Hey, thanks the app you suggest seems to be perfect for non-interactive training. Unfortunately it does not seem to be available in the Google Play Store anymore, do you have similar other apps you can suggest?
Comment: @StefanH It's a shame it's not available anymore.  I don't know of any other hands-free ear training app, but there are many I have not looked at so you may find one.
Comment: Yes, I looked as some, but they are all interactive, have not found one with such a non-interactive training-mode like your mentioned app offers.
Comment: @StefanH I've just started listening to the Ear Training HQ recordings while driving, but so far it seems good (although expensive).  You might give those a try if you can afford them.
Comment: I found this set of mp3's for download: https://www.reddit.com/r/guitarlessons/comments/4hz83r/functional_ear_training_mp3_set/


Error authenticating squid with Active Directory and Kerberos

Question: I'm trying to integrate squid 3.5.19 with AD/Kerberos (Windows 2008 R2), but I get always TCP_DENIED:HIER_NONE
These are the errors in /var/log/squid/cache.log
2016/07/28 10:26:01.583 kid1| 29,4| UserRequest.cc(290) authenticate: No Proxy-Auth header and no working alternative. Requesting auth header.
2016/07/28 10:26:01.584 kid1| 29,9| UserRequest.cc(487) addReplyAuthHeader: headertype:46 authuser:NULL
2016/07/28 10:26:01.584 kid1| 29,9| Config.cc(188) fixHeader: Sending type:46 header: 'Negotiate'
2016/07/28 10:26:01.625 kid1| 29,9| UserRequest.cc(328) authenticate: header Negotiate TlRMTVNTUAABAAAAl4II4gAAAAAAAAAAAAAAAAAAAAAKAFopAAAADw==.
2016/07/28 10:26:01.625 kid1| 29,9| UserRequest.cc(332) authenticate: This is a new checklist test on:local=192.168.50.22:3128 remote=192.168.50.47:56015 FD 16 flags=1
2016/07/28 10:26:01.625 kid1| 29,4| UserRequest.cc(350) authenticate: No connection authentication type
2016/07/28 10:26:01.625 kid1| 29,9| Config.cc(36) CreateAuthUser: header = 'Negotiate TlRMTVNTUAABAAAAl4II4gAAAAAAAAAAAAAAAAAAAAAKAFopAAAADw=='
2016/07/28 10:26:01.625 kid1| 29,5| User.cc(39) User: Initialised auth_user '0x2857400'.
2016/07/28 10:26:01.625 kid1| 29,5| UserRequest.cc(95) UserRequest: initialised request 0x28576b0
2016/07/28 10:26:01.625 kid1| 29,9| Config.cc(267) decode: decode Negotiate authentication
2016/07/28 10:26:01.625 kid1| 29,9| UserRequest.cc(53) valid: Validating Auth::UserRequest '0x28576b0'.
2016/07/28 10:26:01.625 kid1| 29,5| UserRequest.cc(73) valid: Validated. Auth::UserRequest '0x28576b0'.
2016/07/28 10:26:01.625 kid1| 29,9| UserRequest.cc(53) valid: Validating Auth::UserRequest '0x28576b0'.
2016/07/28 10:26:01.625 kid1| 29,5| UserRequest.cc(73) valid: Validated. Auth::UserRequest '0x28576b0'.
2016/07/28 10:26:01.625 kid1| 29,9| UserRequest.cc(63) authenticated: user not fully authenticated.
2016/07/28 10:26:01.625 kid1| 29,9| UserRequest.cc(225) authenticate: auth state negotiate none. Received blob: 'Negotiate TlRMTVNTUAABAAAAl4II4gAAAAAAAAAAAAAAAAAAAAAKAFopAAAADw=='
2016/07/28 10:26:01.625 kid1| 29,9| UserRequest.cc(53) valid: Validating Auth::UserRequest '0x28576b0'.
2016/07/28 10:26:01.625 kid1| 29,5| UserRequest.cc(73) valid: Validated. Auth::UserRequest '0x28576b0'.
2016/07/28 10:26:01.625 kid1| 29,9| UserRequest.cc(63) authenticated: user not fully authenticated.
2016/07/28 10:26:01.625 kid1| 29,9| UserRequest.cc(53) valid: Validating Auth::UserRequest '0x28576b0'.
2016/07/28 10:26:01.625 kid1| 29,5| UserRequest.cc(73) valid: Validated. Auth::UserRequest '0x28576b0'.
2016/07/28 10:26:01.625 kid1| 29,9| UserRequest.cc(46) start: 0x28576b0
2016/07/28 10:26:01.625 kid1| 29,8| UserRequest.cc(134) startHelperLookup: credentials state is '2'
negotiate_kerberos_auth.cc(610): pid=11509 :2016/07/28 10:26:01| negotiate_kerberos_auth: DEBUG: Got 'YR TlRMTVNTUAABAAAAl4II4gAAAAAAAAAAAAAAAAAAAAAKAFopAAAADw==' from squid (length: 59).
negotiate_kerberos_auth.cc(663): pid=11509 :2016/07/28 10:26:01| negotiate_kerberos_auth: DEBUG: Decode 'TlRMTVNTUAABAAAAl4II4gAAAAAAAAAAAAAAAAAAAAAKAFopAAAADw==' (decoded length: 40).
negotiate_kerberos_auth.cc(673): pid=11509 :2016/07/28 10:26:01| negotiate_kerberos_auth: WARNING: received type 1 NTLM token
2016/07/28 10:26:01.626 kid1| 29,8| UserRequest.cc(266) HandleReply: helper: '0x2860438/0x2860438' sent us reply={result=BH, notes={message: received type 1 NTLM token; }}
2016/07/28 10:26:01.626 kid1| 29,6| UserRequest.cc(175) releaseAuthServer: releasing Negotiate auth server '0x2860438'
2016/07/28 10:26:01.626 kid1| ERROR: Negotiate Authentication validating user. Result: {result=BH, notes={message: received type 1 NTLM token; }}
2016/07/28 10:26:01.626 kid1| 29,9| UserRequest.cc(53) valid: Validating Auth::UserRequest '0x28576b0'.
2016/07/28 10:26:01.626 kid1| 29,5| UserRequest.cc(73) valid: Validated. Auth::UserRequest '0x28576b0'.
2016/07/28 10:26:01.626 kid1| 29,9| UserRequest.cc(53) valid: Validating Auth::UserRequest '0x28576b0'.
2016/07/28 10:26:01.626 kid1| 29,5| UserRequest.cc(73) valid: Validated. Auth::UserRequest '0x28576b0'.
2016/07/28 10:26:01.626 kid1| 29,9| UserRequest.cc(63) authenticated: user not fully authenticated.
2016/07/28 10:26:01.626 kid1| 29,9| UserRequest.cc(328) authenticate: header Negotiate TlRMTVNTUAABAAAAl4II4gAAAAAAAAAAAAAAAAAAAAAKAFopAAAADw==.
2016/07/28 10:26:01.626 kid1| 29,9| UserRequest.cc(53) valid: Validating Auth::UserRequest '0x28576b0'.
2016/07/28 10:26:01.626 kid1| 29,5| UserRequest.cc(73) valid: Validated. Auth::UserRequest '0x28576b0'.
2016/07/28 10:26:01.626 kid1| 29,9| UserRequest.cc(63) authenticated: user not fully authenticated.
2016/07/28 10:26:01.626 kid1| 29,9| UserRequest.cc(256) authenticate: auth state negotiate failed. Negotiate TlRMTVNTUAABAAAAl4II4gAAAAAAAAAAAAAAAAAAAAAKAFopAAAADw==
2016/07/28 10:26:01.626 kid1| 29,9| UserRequest.cc(53) valid: Validating Auth::UserRequest '0x28576b0'.
2016/07/28 10:26:01.626 kid1| 29,5| UserRequest.cc(73) valid: Validated. Auth::UserRequest '0x28576b0'.
2016/07/28 10:26:01.626 kid1| 29,9| UserRequest.cc(63) authenticated: user not fully authenticated.
2016/07/28 10:26:01.626 kid1| 29,9| UserRequest.cc(487) addReplyAuthHeader: headertype:46 authuser:NULL
2016/07/28 10:26:01.626 kid1| 29,9| Config.cc(188) fixHeader: Sending type:46 header: 'Negotiate'
2016/07/28 10:26:01.732 kid1| 29,9| UserRequest.cc(487) addReplyAuthHeader: headertype:46 authuser:NULL
2016/07/28 10:26:01.732 kid1| 29,9| Config.cc(188) fixHeader: Sending type:46 header: 'Negotiate'
2016/07/28 10:26:01.913 kid1| 29,9| UserRequest.cc(487) addReplyAuthHeader: headertype:46 authuser:NULL
2016/07/28 10:26:01.913 kid1| 29,9| Config.cc(188) fixHeader: Sending type:46 header: 'Negotiate'
2016/07/28 10:26:01.956 kid1| 29,9| UserRequest.cc(487) addReplyAuthHeader: headertype:46 authuser:NULL
2016/07/28 10:26:01.956 kid1| 29,9| Config.cc(188) fixHeader: Sending type:46 header: 'Negotiate'
2016/07/28 10:26:01.980 kid1| 29,9| UserRequest.cc(487) addReplyAuthHeader: headertype:46 authuser:NULL
2016/07/28 10:26:01.980 kid1| 29,9| Config.cc(188) fixHeader: Sending type:46 header: 'Negotiate'
2016/07/28 10:26:01.993 kid1| 29,9| UserRequest.cc(487) addReplyAuthHeader: headertype:46 authuser:NULL
2016/07/28 10:26:01.993 kid1| 29,9| Config.cc(188) fixHeader: Sending type:46 header: 'Negotiate'
2016/07/28 10:26:02.004 kid1| 29,9| UserRequest.cc(487) addReplyAuthHeader: headertype:46 authuser:NULL
2016/07/28 10:26:02.004 kid1| 29,9| Config.cc(188) fixHeader: Sending type:46 header: 'Negotiate'
2016/07/28 10:26:09.555 kid1| 29,6| UserRequest.cc(179) releaseAuthServer: No Negotiate auth server to release.
2016/07/28 10:26:09.556 kid1| 29,6| UserRequest.cc(179) releaseAuthServer: No Negotiate auth server to release.
2016/07/28 10:26:09.556 kid1| 29,5| UserRequest.cc(101) ~UserRequest: freeing request 0x28576b0
2016/07/28 10:26:09.556 kid1| 29,5| User.cc(21) ~User: doing nothing to clear Negotiate scheme data for '0x2857400'
2016/07/28 10:26:09.556 kid1| 29,5| User.cc(127) ~User: Freeing auth_user '0x2857400'.
2016/07/28 10:26:09.559 kid1| 29,6| UserRequest.cc(179) releaseAuthServer: No Negotiate auth server to release.
2016/07/28 10:26:09.559 kid1| 29,6| UserRequest.cc(179) releaseAuthServer: No Negotiate auth server to release.
2016/07/28 10:26:09.559 kid1| 29,5| UserRequest.cc(101) ~UserRequest: freeing request 0x286f4d0
2016/07/28 10:26:09.559 kid1| 29,5| User.cc(21) ~User: doing nothing to clear Negotiate scheme data for '0x286f2a0'
2016/07/28 10:26:09.559 kid1| 29,5| User.cc(127) ~User: Freeing auth_user '0x286f2a0'.
2016/07/28 10:26:09.563 kid1| 29,6| UserRequest.cc(179) releaseAuthServer: No Negotiate auth server to release.
2016/07/28 10:26:09.563 kid1| 29,6| UserRequest.cc(179) releaseAuthServer: No Negotiate auth server to release.
2016/07/28 10:26:09.563 kid1| 29,5| UserRequest.cc(101) ~UserRequest: freeing request 0x2857ab0
2016/07/28 10:26:09.563 kid1| 29,5| User.cc(21) ~User: doing nothing to clear Negotiate scheme data for '0x241e990'
2016/07/28 10:26:09.563 kid1| 29,5| User.cc(127) ~User: Freeing auth_user '0x241e990'.

I've created the keytab on Windows using ktpass:

ktpass /princ firstname.lastname@example.com /mapuser firstname.lastname@example.com /crypto  rc4-hmac-nt   /pass * /ptype KRB5_NT_PRINCIPAL /out C:\Soporte\winkrb5.keytab

and then copied to squid, and I see the following thing:
 klist -kt /etc/squid/HTTP.keytab
Keytab name: FILE:/etc/squid/HTTP.keytab
KVNO Timestamp           Principal
---- ------------------- ------------------------------------------------------
   3 12/31/1969 21:00:00 email@example.com

I wonder if the wrong timestamp has to do with the problem
Kerberos and relevant squid config files are as follows:
[logging]
 default = FILE:/var/log/krb5libs.log
 kdc = FILE:/var/log/krb5kdc.log
 admin_server = FILE:/var/log/kadmind.log
[libdefaults]
 dns_lookup_realm = false
 ticket_lifetime = 24h
 renew_lifetime = 7d
 forwardable = true
 rdns = false
 default_realm = EXAMPLE.LOCAL
 default_ccache_name = KEYRING:persistent:%{uid}

; for Windows 2008 with AES
    default_tgs_enctypes = aes256-cts-hmac-sha1-96 rc4-hmac des-cbc-crc des-cbc-md5
    default_tkt_enctypes = aes256-cts-hmac-sha1-96 rc4-hmac des-cbc-crc des-cbc-md5
    permitted_enctypes = aes256-cts-hmac-sha1-96 rc4-hmac des-cbc-crc des-cbc-md5

squid.conf:
auth_param negotiate program /usr/lib64/squid/negotiate_kerberos_auth -d  -s firstname.lastname@example.com
auth_param negotiate children 10
auth_param negotiate keep_alive on
acl kerb_auth proxy_auth REQUIRED

Is the keytab timestamp the cause of errors? Am I doing something wrong?
Comment: What about the browser? Does it support Kerberos auth and is it configured to do so?
Comment: I tested with Internet Explorer, AFAIK theroretically it suppors out of the box. Also with Firefox, with network.negotiate-auth.trusted-uris using `.example.local`. Anyone of them work.
Answer: The 'negotiate_kerberos_auth: WARNING: received type 1 NTLM token' in log means your browser supplied the Negotiate/NTLM token instead of Negotiate/Kerberos that your negotiate_kerberos_auth is able to handle.
You will need to have NTLM authenticator for Negotiate authentication scheme configured too. One way is to install and configure Samba as indicated on http://wiki.squid-cache.org/ConfigExamples/Authenticate/WindowsActiveDirectory#NTLM or use the redirection of NTLM credentials to LDAP server as indicated on http://docs.diladele.com/administrator_guide_4_6/active_directory/install_prerequisites_for_ntlm_authentication.html
If your machine is within domain and user is a domain user then most probably you have configured your proxy as IP address and Kerberos requires proxy to be set as FQDN in browser. For troubleshooting steps see http://docs.diladele.com/administrator_guide_4_6/active_directory/troubleshooting.html.
Comment: I login from Windows 7 and joined to domain. I'd want to use kerberos. I'm tried with both Internet Explorer and Firefox with proxy set as FQDN.
Comment: Is mandatory to use NTLM authenticator too besides kerberos?
Answer: Well, finally thanks @Rafael I've found the solution:
Replace:
auth_param negotiate program /usr/lib64/squid/negotiate_kerberos_auth -d  -s firstname.lastname@example.com

by:
auth_param negotiate program /usr/lib64/squid/negotiate_wrapper_auth -d --ntlm /usr/bin/ntlm_auth --diagnostics --helper-protocol=squid-2.5-ntlmssp --domain=EXAMPLE --kerberos /usr/lib64/squid/negotiate_kerberos_auth -d -s GSS_C_NO_NAME

Also, I've started smb and winbind services
Comment: Why not mark Rafael's answer as such then? :)


trace and derivative: understanding $\text{tr}\left(e_j e_i^T B^T B \right) = \langle B e_i e_j^T, B \rangle $

Question: where $B$ is $n \times n$ matrix
Can someone help me understand the above equality. what kind of inner product we are using here?
Also, why do we have $\frac{d}{dB}\left(\langle B e_i e_j^T, B \rangle \right) = B e_i e_j^T  $ ?
Comment: The inner product is defined by : $=tr(A^TB)$
Answer: The Frobenius product (aka Inner product) can be written in terms of the trace
$$A:B={\rm Tr}\big(AB^T\big)$$
The trace's cyclic property means that such products can be rearranged in various ways, e.g.
$$\eqalign{
A:B &= A^T:B^T &= B:A \\
A:BC &= AC^T:B &= B^TA:C\\
}$$
Write the function in terms of the Frobenius product. Then calculate its differential and gradient.
$$\eqalign{
\phi &= {\rm Tr}\left(e_je_i^TB^TB\right) \\
 &= e_je_i^T:B^TB \\
d\phi
 &= e_je_i^T:(dB^TB+B^TdB) \\
 &= (e_je_i^T)B^T:dB^T + B(e_je_i^T):dB \\
 &= B(e_je_i^T)^T:dB + B(e_je_i^T):dB \\
 &= B(e_ie_j^T+e_je_i^T):dB \\
\frac{\partial\phi}{\partial B}
 &= B\left(e_ie_j^T+e_je_i^T\right) \\
}$$


Why a tcache bin is associated with an arena?

Question: I'm using Ubuntu 18.04 with glibc-2.27, a distribution where the tcache re-allocation system is deployed. While debugging some image with gdb + gef (aka GDB Enhanced Features) I have noticed that the tcache bin is associated with an arena.
A generic heap bins command output look like this:
gef➤  heap bins
───────────── Tcachebins for arena 0x7ffff7dcfc40 ───────────────────────────────────────────────────────────────────────────────────────
Tcachebins[idx=0, size=0x10] count=1  ←  Chunk(addr=0x555555756260, size=0x20, flags=PREV_INUSE)

As one can tell from the output, the tcache bins are associated with an arena. This looks odd to me, since the whole idea of tcache (at least the way I got it) was to avoid the race between threads which was caused by locking arenas.
I did some research of Malloc Internals at glibc wiki, and I found what I already knew:

Each thread has a per-thread cache (called the tcache) containing a small collection of chunks which can be accessed without needing to lock an arena.

So, what is the relationship between arenas and tcache bins? if I can access the tcache without locking an arena why gef print an arena (address)? Thanks for any clarification!
Answer: This appears to be a presentation issue in the GDB plugin. Instances of the tcache are associated with threads, not arenas.  One tcache instance can contain allocations from multiple different arenas, not just the arena currently associated with the thread to which the tcache belongs.
Comment: Wow I actually never thought about one `tcache` having chunks from multiple arenas but yes, it does make sense. Thanks!


using scp in a python script on opensolaris

Question: I have written a python script to transfer files/folders between two machines. I have used scp for this and have included it as import scp
but it's giving me this error:
ImportError: No module named scp.

How can I fix it?
Comment: `import` is for importing other python modules, not running external commands.
Answer: I'm a bit confused...
Did it ever work before?
It seems like you just want to write a python script that calls the scp command, not a module.
In which case, do the following:

remove the import,
and just execute a simple command from your python script.
Answer: It sounds like /path/to/scp.py is not in your ${PYTHONPATH} environment variable.You can either move scp.py to somewhere within ${PYTHONPATH} or augment ${PYTHONPATH} to include /path/to either in your operating system or using sys.path in Python.
See also How do I copy a file to a remote server in python using scp or ssh?
Answer: pip install scp

Run the above command and then run the script again.


Mac OS X iterm2: backward delete word

Question: I've been trying to customize a key shortcut to delete backspace entire word.
I've tried everything online and still this doesn't work.
I'm using iterm2, using Natural Text Editing keys preset and still - doing command + delete will delete the entire line, and doing option + delete will delete the last word up until a certain special character (this could be slash, hyphen, underscore, semicolon, etc...).
Doing control + W gives the same result as option + delete.
I do want the functionality of deleting backwards until a certain special character, but I would also like to be able to delete entire word (until space).
Comment: I do not really got your question. Why do you want control + W to delete backwards a word when option + delete already does this?
Comment: As I said, at the moment, both control + W and option + delete unfortunately do the same thing - delete a word up until a special character. I did not configure it like that, it is just the current situation. I want some keyboard shortcut to delete entire word until space, potentially to have it be assigned to control + W instead of its existing assignment
Comment: I need the same behavior too, deleting backward word by word until a special character would be killing me; for faster backward deletion, we need to have the `ctrl` + `w` to delete all the words back to the first `space` character. Linux does the same why not in mac?
Answer: You may add corresponding keybinding to iterm2, or globally:
'Iterm2 preferences -> 'Keys' (or 'Profiles' -> 'Keys' - global | per-profile setting):

add desired keyboard shortcut (e.g. option + shift + backspace),
add Action - Under 'Send Keystrokes' select 'Send Hex Code' and type '0x17'
Comment: Funny enough, I want the behavior you're seeing (and am getting the behavior you want, if I understand correctly) - I'd like `option + delete` to delete word characters up to a special character, but I'm getting the behavior with `0x17` of deleting the entire space-delimited string.
Comment: 0x17 - is required sequence. Do you see an action `Send Hex Codes: 0x17` opposite to your key combination? Is there a chance that your combination has been overriden somewhere else?
Try some other really specific combination, just for test.
I've added following mappings:
- Cmd+Backspace - `Send Hex Codes:0x15` (delete line),
- Option+Backspace - `Send Hex Codes:0x1b 0x7f` (delete 'part' of the word backwards),
- Option+Shift+Backspace - `Send Hex Codes:0x17` (delete word backwards).
Working as expected.
Comment: Still doesn't work. If I write in my terminal for example `word1 word2-word3` and press the shortcut it only deletes the `word3` part, and I want for it to delete the `word2-word3` completely
Comment: I have set all of these, tried `Option as meta` set on or off, none of those work.
Thanks for all the help anyway
Comment: The different behaviour of `0x17` is likely due to your shell configuration WORDCHARS. See https://superuser.com/a/847842/21682 for example.

Mac OS X iterm2: backward delete word


How to check not exist field in document in vespa

Question: <code>curl --location --request POST 'http://xxxxxx/search/' \
--header 'Content-Type: application/json' \
--data-raw '{
    "offset": 0,
    "hits": 60,
    "ranking.softtimeout.factor": 0.7,
    "ranking.profile": "default",
    "yql": "select id, origin_id from mt_challenge where best_score = \"null\";",
    "timeout": "1500ms",
    "request_timeout": 5
}'
</code>
best_score field is float type how to checkout all document with not exist best_score field
Answer: Sorry but NaN/null values are not searchable so this is currently not supported. Using a default value at ingestion time which does not conflict with your real data can be used to work around this.
Comment: You can also modify your existing data using an "update where" to set a value on all documents currently having null, see https://docs.vespa.ai/en/reference/document-v1-api-reference.html#put
Comment: Thank you for the detailed response! The dot product is an great suggestion
Comment: I use the vespa dump to checkout all docuement and filter by linux command 'grep -v'
 to Pick out the one that meets the needs


How to identify the clients in SSL in order to use symetric keys?

Question: I was going to HTTPS communication over SSL. What I understood is both client and server do the handshake to obtain symmetric keys for application data encryption and decryption.
My doubt is when a client sends a piece of encrypted data to server then how the server will identify the client and use the corresponding keys for decryption?
Answer: As far as I know, it's the client's IP address and ephemeral port number that identify the connection to the application.
Answer: The SSL/TLS handhsake happens between the client and the server. At the end of the handshake, the client and the server will have established shared keys (and MAC secrets to ensure the integrity of the communication). In case of session resumption, a session identifier can be used to associate further connections with the same key.
The rest of the connection is identified using the normal TCP mechanism (assuming SSL/TLS over TCP).


How to get PublishedTask Parent Id using CSOM C#

Question: I need to get the Parent.Id of a PublishedTask using C# and CSOM.  Does anyone know how?
(Client-side Object Model, Project Server Online, running on SharePoint.)
The following code does not work:
ProjContext.Load(PublishedProject.Tasks);
ProjContext.ExecuteQuery();

foreach (PublishedTask Task in PublishedProject.Tasks)
{
    Console.WriteLine(Task.Parent.Id);
}

Altering the Load() method like this also doesn't work:
ProjContext.Load(PublishedProject.Tasks, t => t.Include(pt => pt.Parent))
ProjContext.Load(PublishedProject.Tasks, t => t.Include(pt => pt.Id, pt => pt.Parent))
ProjContext.Load(PublishedProject.Tasks, t => t.Include(pt => pt.Id, pt => pt.Parent, pt => pt.Parent.Id))

In all these cases, the PublishedTask.Parent is undefined.  PublishedTask.Parent.Id is also undefined.  An error is thrown when attempting to access any of these properties under any of these scenarios.
Does anyone know how to get the PublishedTask.Parent.Id?
Answer: The solution is to know when Task.Parent.Id is null, and when it has a value.  The code below tells that.
public static bool IsNull(ClientObject clientObject)
{
    //check object
    if (clientObject == null)
    {
        //client object is null, so yes, we're null (we can't even check the server object null property)
        return true;
    }
    else if (!clientObject.ServerObjectIsNull.HasValue)
    {
        //server object null property is itself null, so no, we're not null
        return false;
    }
    else
    {
        //server object null check has a value, so that determines if we're null
        return clientObject.ServerObjectIsNull.Value;
    }
}

I found this function here:
http://chrisdomino.com/Blog/Post/An-Investigation-Into-Nullability-In-The-SharePoint-Client-Object-Model
So the code below works, with the function above.
                ProjContext.Load(PublishedProject.Tasks);
                ProjContext.Load(PublishedProject.Tasks, t => t.Include(pt => pt.Id, pt => pt.Parent));
                ProjContext.ExecuteQuery();

                foreach (PublishedTask Task in PublishedProject.Tasks)
                {
                    string sParentId = null;
                    string sParentName = null;
                    if (!IsNull(Task.Parent))
                    {
                        sParentId = Task.Parent.Id.ToString();
                        sParentName = Task.Parent.Name;
                        string sMyName = Task.Name;
                    }
                    Console.WriteLine("{0}, {1}, {2}, {3}", Task.Name, sParentId, sParentName, Task.Work);
                }


How can I retrieve the memory utilization of a pod in kubernetes via kubectl?

Question: Inside a namespace, I have created a pod with its specs consisting of memory limit and memory requests parameters. Once up a and running, I would like to know how can I get the memory utilization of the pod in order to figure out if the memory utilization is within the specified limit or not. "kubectl top" command returns back with a services related error.
Comment: You should likely resolve your `kubectl top` issue, you can see the cpu and memory usage of your given pod like this: `kubectl top pod ${POD_NAME}`.
Answer: kubectl top pod <pod-name> -n <fed-name> --containers
FYI, this is on v1.16.2
Answer:
kubectl top pod POD_NAME --containers

shows metrics for a given pod and its containers.
If you want to see graphs of memory and cpu utilization then you can see them through the kubernetes dashboard.
A better solution would be to install a metrics server alongwith prometheus and grafana in your cluster. Prometheus will scrape the metrics which can be used by grafana for displaying as graphs.
This might be useful.
Answer: You need to install metrics server to get the metrics. Follow the below thread
Error from server (NotFound): podmetrics.metrics.k8s.io "mem-example/memory-demo" not found
Answer: Instead of building ad-hoc metric snapshots, a much better way is to install and work with 3rd party data collector programs which if managed well gives you a great solution for monitoring systems and a neat Grafana UI (or likewise) you can play with. One of them is Prometheus and which comes highly recommended.
using such PnP systems, you can not only create a robust monitoring pipeline but also the consumption and hence the reaction to the problem is well managed and executed compared to only relying on TOP


How can I remove a drive from a BTRFS RAID 1 setup to make it just a single (BTRFS) drive?

Question: I'm using Ubuntu 10.04 and have the /home directory setup as a BTRFS RAID 1 with two 2TB drives.
I'd like to make the /home directory just a single drive; how do I safely go about doing this?
Answer: Update: December 2020
The functionality of btrfs continues to evolve over time. The delete command is now an alias for the remove command which produces different results. Today, you would run the following commands:
btrfs balance start -f -sconvert=single -mconvert=single -dconvert=single <mount>
btrfs device remove <drive> <mount>

The first command converts all data from a mirrored setup to a single-copy setup. This effectively makes the RAID1 into a JBOD setup. The -f option is required to tell the filesystem to really reduce the resiliency of the data.
Once this completes, the second command removes the device from the JBOD. The filesystem will move any data from the removed device to the other device.
Original Answer
According to this btrfs wiki, you remove a device from a btrfs RAID, by issuing the command:
btrfs device delete <drive> <mount>

This is an online command, so for you, it would be (while /home is mounted):
btrfs device delete <drive> /home

Comment: WARNING: This procedure is NOT for removing a failing hdd from a btrfs RAID1 with two drives, you may loose your data.
Comment: I would recommend `btrfs balance start -f -sconvert=dup -mconvert=dup -dconvert=single `
Comment: You're going to want to use `btrfs-vol -r  /home` then, I think.  I'm not sure if that can be done online or not.
Comment: I don't have "`btrfs`".  I've got: `btrfsck`, `btrfs-convert`, `btrfsctl`, `btrfs-debug-tree`, `btrfs-image`, `btrfs-show`, `btrfstune`, and `btrfs-vol`.
I'll have to dig a bit.
Comment: as of 2020, there is no command `btrfs device delete` , and the command `btrfs device remove` will not convert raid1 to raid0
Comment: @am70 Thanks for the comment. I provided an updated answer.
Comment: @hrunting I just posted a similar question. Maybe you could elaborate on what to do when you a have a RAID6 array? https://serverfault.com/q/1100148/77770
Comment: @Nathan You cannot turn an individual drive in a RAID6 array into a standalone device and expect to do much with the data. The reason it works in the RAID1 setup above is that each drive in the RAID contains a copy of the data. In a RAID6 setup, the drives each contain only part of the data. The data is also chunked, so its representation on disk is not the same as it would be if it was a non-RAID filesystem.
Answer: Thank you for the update @hrunting!
However the re-balance using "single" profile did not work for me on Ubuntu 20.04.2 LTS.  I needed:
btrfs balance start -dconvert=raid0 -mconvert=raid0 -sconvert=raid0 -f <mount>
btrfs device remove <dev/partition> <mount>

to get rid of pesky RAID1 metadata/system and allow removal.
And as always, folks: back up your data to an independent physical media before attempting these - there's a good chance you will lose it all in this evolution.  I am a big fan of:
 btrfs send -f <bkupFile> <subvol>
 btrfs receive -f <bkupFile> <newsubvol>


A way to send 1 email once done reading from CSV

Question: I wrote a script to send an email if the values match a certain criteria. I'm wanting to send 1 email instead of multiple emails upon every check. I thought I can mitigate by throwing in another function but I can't figure out how to do it. Any ideas on how to accomplish this?
import csv, requests, xmltodict, smtplib, email.utils
from email.mime.text import MIMEText

def sendEmail(host, value, devicename):
    # Create the message
    msg = MIMEText('This is the body of the message.')
        msg['To'] = email.utils.formataddr(('Recipient', 'XXXXXX'))
        msg['From'] = email.utils.formataddr(('Author', 'XXXXX'))
        msg['Subject'] = 'Simple test message'
        server = smtplib.SMTP('XXXXXXX')
        server.set_debuglevel(True) # show communication with the server

    try:
        server.sendmail('XXXXXX', ['XXXXXX'], msg.as_string())
    finally:
        server.quit()

def check(hostIP, value):
    xml = """<?xml version="1.0" encoding="iso-8859-1"?>"""

    headers = {'Content-Type': 'application/xml'}
    response = requests.post('http://' + hostIP + '/RPC2', data=xml, headers=headers).text

    doc = xmltodict.parse(response)
    uptime = str(doc['response'])

    maxtime = '300'
    time = str(uptimeValue)
    day = time // (24 * 3600)

    if day >= maxtime:
        print 'it is'
        sendEmail(str(hostIP), str(value), str(devicename))
    else:
        print "it is not!"

def main():
    try:
        with open('list.csv', 'r') as file:
            reader = csv.DictReader(file)
            for row in reader:
                check(row['Host'], row['Value'])
    except Exception as error:
        print ValueError("Could not properly read the csv file")
        sys.exit(0)

if __name__ == "__main__":
   main()

Comment: Will the 1 email contain all of the information now split up over multiple email messages?
Comment: There is no `mail data`, you are sending a **empty** mail.
Comment: @ScottHunter - Yeah, I want the 1 email to contain all the data. Right now when I run the script, it sends 1 email per loop
Comment: @stovfl - Yeah. it's that way since I'm still testing. I want it to basically just aggregate all the data and send 1 email instead of multiple. In testing I get "This is the body of the message." 10 times with the number of devices I'm testing.
Answer: Replace
def sendEmail(message):

and
    if day >= maxtime:
        print 'it is'
        return (str(hostIP), str(value), str(devicename))

and
device_list = []
for row in reader:
    result = check(row['Host'], row['Value'])
    if result:
        device_list.append(', '.join(result))

sendEmail('\n'.join(device_list))

Comment: that actually worked!! But now I'm facing a problem with the functions if the day is less than the max time. It used to work but now it just breaks the loop. I put a return and then a pass after the else statement and that didn't help. Anything else I can put?
Comment: Basically it just doesn't go through the loop again.
Comment: I did not know that can be done! and that makes sense now. That worked perfectly!!
Answer: Instead of using check to send the email, use it to add an item to go into the message body, probably in an array or dictionary.  Then, after you are done processing your file, you can use that collected information to build the body of your email to give to a single call to sendEmail.
That means you don't call sendEmail from inside of check; you call it from main once you are done with the file.
Comment: So, now the question is...how do I have check wait until its done processing the csv before sending everything to the sendEmail function? I think that is the biggest thing I'm drawing a blank to.


How do I debug freezing issue in my iOS app

Question: I have an app made with Objective-C. This app always worked right. Yesterday, my Xcode was ignoring anything new that I created, so I updated it to Xcode 10.1 (10B61). Anyway, I was editing another app, not the following one.
A version of the app have been updated on App Store after that (I sent it 2 days ago) and 18pm my boss call me on my phone and told me that the app was crashing.
Today, I verified that the app isn't crashing, it is freezing. It is freezing when any of those things happens:

Open or close the keyboard
Open or close any activity
Do a request using AFNetworking framework

I tried do a git checkout to the init of the month, it still didn't worked. So I did a checkout to the init of the past month (9th October) and it is still freezing. But those versions of the app was working with no errors.
I tried debug it until on the assembly, I could fix AFNetworking changing the Framework's code for the first time ever. There is a dispatch_sync freezing the app when it try to set the request's header, I can change it to dispatch_async and it fix 1/3 of the problem, the requests are still working on async. Line: <code>./Pods/AFNetworking/Serialization/AFURLRequestSerialization.m:306</code>
<code>- (NSDictionary *)HTTPRequestHeaders {
    NSDictionary __block *value;
    dispatch_sync(self.requestHeaderModificationQueue, ^{
        value = [NSDictionary dictionaryWithDictionary:self.mutableHTTPRequestHeaders];
    });
    return value;
}
</code>
Now my boss told me that all the two apps are freezing. And I didn't updated anything in the first app mentioned. The apps are from the same project, they works like an Uber.
Anyone have some idea of what is happening?
====EDIT0====
Currently we noticed that the app can be build in another computer and it word like before. So the apps that are being build in my Mac are freezing, the problem is not the code, some library, update or whatever. Probably it is my computer, we will try to use another mac to build and send the apps.
If it resolves the problem, I will put the solution here and get a talk with Apple Support.
Comment: If you call `dispatch_sync` from the same queue then you have created a deadlock.
Comment: I'd suggest you create a [MCVE](https://stackoverflow.com/help/mcve). Create a blank project, add AFNetworking, and see if you can manifest the problem. Likely you won't, so you'll then have to start adding code patterned after how you have it in your app, until you can manifest the problem. (The other approach is to copy your project, start ripping out everything unrelated to the problem, and continue repeating this process until you can no longer manifest the problem.) But this question is too broad as it stands now.
Comment: sounds like you need to get a new Mac
Answer: The problem was that every iOS app that is builded in my Mac starts to random freeze in dispatch_sync functions. I will try format my PC or buy a new one.
It makes literally no sense, but solved the problem.


Can a subcategory of the category of sets be self-dual?

Question: Given that Set, the category of sets, is not self-dual, can a subcategory of Set be self-dual? If so, what are some examples?
Answer: Sure - consider the one-element category with a unique morphism. More generally, note that any small category is (isomorphic to) a subcategory of Set, so any small self-dual category gives an example.
Comment: @TaishiWalde An embedding is given by the product of all the covariant representables. This amounts to a slight extension of the fact that the Yoneda embedding is faithful.
Comment: Thank you, Noah!
Comment: @NoahSchweber Could you explain why/how every category embeds into the category of sets?