How can I calculate the variance of policy gradient method?

Question: In Reinforcement Learning, how can I calculate the variance of policy gradient method?
Comment: The variance of a policy would simply be $E[\pi(a|s)^2] - E[\pi(a|s)]^2$.
Comment: Variance is an attribute of random variables. Policy gradients is a class of algorithms, not a random variable, so the question is unclear.
Comment: @shimao in many literatures, variance of policies are considered for the stability of algorithm.
Comment: I think you have confused the variance of a policy and the variance of the performance of a policy learned using a particular algorithm.
Comment: @shimao thank you the hint, I am not asking for the variance of the performance of a policy. I am asking the variance of a policy.
Answer: Random variables have variance. The policy gradient method is not a random variable, it is a class of reinforcement learning algorithms. It doesn't make much sense to ask about the variance of the policy gradient method -- it is akin asking about the variance of addition, which likewise is not itself a random variable.
If you're asking about the variance of a random variable, then it is computed in the standard way for any random variable by applying the usual definition
$$
\text{Var}(X) = \mathbb{E}\left[(X-\mu)^2\right]
$$
where $\mathbb{E}[X]=\mu$. But it's not quite clear which random variable you are asking about, so it's not possible to say more.
Comment: I think he meant the variance of the gradients estimated by the policy gradient method. https://www.analyticsvidhya.com/blog/2020/11/baseline-for-policy-gradients/. This blog is about how to minimize the variance of gradients in policy gradient method. Does this help?
Comment: On the one hand, OP marked this answer as "accepted," so it seems this answered OP's question. On the other hand, users voted to close as "unclear," so I appreciate that there is some ambiguity in the question, & it could be interpreted in several ways. This answer acknowledges the ambiguity in 'If you're asking about ...' but it's infeasible to answer every possible reading. If you'd like, you could ask & answer a question about policy gradients -- But you would need to develop the question & answer much more thoroughly. A link to a blog post is not, itself, an answer.


Integral of $\sin (2x)/(1+\cos^2 x)$

Question: The question is to find

$$\displaystyle \int \frac {\sin (2x)}{1+\cos^2x}.$$

Can anyone help me? I need all the steps, because I need to understand what to do.
Thank you.
Answer: You can use the identity:
$$
\sin(2x)=2\sin(x)\cos(x).
$$
Then use a $u$-substitution with $u=1+\cos^2(x)$.
Comment: I'm too slow formatting integrals, I think!
Comment: Wow, that definately clears up alot. Sorry about any bad formatting when i made the question. I am new to this site.
Answer: By the double angle formula, $\sin(2x) = 2 \sin(x)\cos(x)$
$$\int \frac{\sin(2x)}{1 + \cos^2(x)}dx = \int \frac{2\sin(x)\cos(x)}{1+\cos^2(x)}dx$$
Let $u = 1 + \cos^2(x)$
$du = -2\sin(x)\cos(x) dx$
so...substituting, we get: $$\int \frac{2\sin(x)\cos(x)}{1+\cos^2(x)}dx = \int -\frac{1}{u} du$$
Can you take it from here?
Integrate with respect to $u$, then "back" substitute $u = 1 + \cos^2(x)$ into the result.
Answer: Note that $\sin(2x)=2\sin(x)\cos(x)$
Therefore the problem reduces to finding the integral: $\int \frac {2\sin(x)\cos(x)}{1+\cos^2(x)}dx=-\log(1+\cos^2(x))+C$
Comment: A photo finish.  But, you beat me by a second.  +1

Integral of $\sin (2x)/(1+\cos^2 x)$


Find a lower bound for a Riemann integral.

Question: Let $a,b$ be  strictly positive constants and let $p\in(1/4,3/4)$. Is there $c>0$ such that
$$\int_0^1(1-t^a)^{1/2+p}[2t-(2+b)t^{b+1}]dt>c \quad ?$$
*The constant $c>0$ may depend on $a,b$ or $p$.
I tried several values for $(a,b,p)$, and always observed a positive integral. So, I'm conjecturing that the integral is strictly positive.
Intuitively, $2t-(2+b)t^{b+1}$ is a concave function on $[0,1]$ which is weighted by a factor $(1-t^a)^{1/2+p}$. This factor is less than one, and decreases as $t\to 1$, and so it pushes the right tail of the function (which is negative) closer to zero. So it makes sense to think about a positive integral.
I wrote the integral in terms of Beta functions but didn't succeed.
Answer: $$\int_0^1 (1-t^a)^{1/2+p}[2t-(2+b)t^{b+1}]~dt$$
$$=\int_0^1 (1-t^a)^{1/2+p}~d(t^2-t^{b+2})$$
$$=\left.(1-t^a)^{1/2+p}(t^2-t^{b+2})\right|_0^1-\int_0^1(t^2-t^{b+2})(\frac 1 2+p)(1-t^a)^{p-1/2}(-at^{a-1})~dt$$
$$=0+\int_0^1(t^2-t^{b+2})(\frac 1 2+p)(1-t^a)^{p-1/2}(at^{a-1})~dt,\qquad (*)$$ which is positive. The latter integral is bounded by $$\max_{0\leq t\leq 1}(t^2-t^{b+2})\int_0^1(\frac 1 2+p)(1-t^a)^{p-1/2}\cdot at^{a-1}~dt=\max_{0\leq t\leq 1}(t^2-t^{b+2}),$$ where one uses the substitution $u=t^a$ for computing the last integral. It follows that when $b\rightarrow 0$, the integral approaches $0.$
By the above argument, there does not exist a uniform positive lower bound. However, one may still obtain a lower bound dependent on $a,b,$ and $p$:
Let $f(t)=t^2-t^{b+2}=t^2(1-t^b)\geq 0$. Then $f$ has a unique critical point at $t=\left(\frac 2{b+2}\right)^{1/b}$ in $(0,1)$ and $f$ is increasing on $[1/2,(2/(b+2))^{1/b}]$ (Note that $2^{b+1}>b+2$ for $b>0$ as can be checked by the fact that $g(x)=2^{x+1}-x-2$ satisfies $g(0)$ and $g'(x)>0$ for all $x\geq 0$). It follows from $(*)$ that $$\int_0^1 (1-t^a)^{1/2+p}[2t-(2+b)t^{b+1}]~dt$$
$$=\int_0^1 f(t)(\frac 1 2+p)(1-t^a)^{p-1/2}(at^{a-1})~dt$$
$$>\int_{1/2}^{(2/(b+2))^{1/b}} f(t)(\frac 1 2+p)(1-t^a)^{p-1/2}(at^{a-1})~dt$$
$$>f(1/2)\int_{1/2}^{(2/(b+2))^{1/b}} (\frac 1 2+p)(1-t^a)^{p-1/2}(at^{a-1})~dt$$
$$=\left[\left(\frac 1 2\right)^2-\left(\frac 1 2\right)^{b+2}\right]\cdot \left[\left.-(1-t^a)^{p+1/2}\right|_{1/2}^{(2/(b+2))^{1/b}}\right]$$
$$=\left[\left(\frac 1 2\right)^2-\left(\frac 1 2\right)^{b+2}\right]\cdot\left[\left(1-\left(\frac 1 2\right)^a\right)^{p+1/2}-\left(1-\left(\frac 2{b+2}\right)^{a/b}\right)^{p+1/2}\right],$$ hence a lower bound can be taken to be $$c(a,b,p):=\left[\left(\frac 1 2\right)^2-\left(\frac 1 2\right)^{b+2}\right]\cdot\left[\left(1-\left(\frac 1 2\right)^a\right)^{p+1/2}-\left(1-\left(\frac 2{b+2}\right)^{a/b}\right)^{p+1/2}\right]>0.$$
Comment: @CelineHarumi: there is no uniform positive bound for $a,b,p>0$ for example, of $a>0,p>1/2$, then as $b\rightarrow0$, the integral converges to $0$ as Pythagoras mentioned.
Comment: @CelineHarumi: By dominated convergence, if $b\rightarrow0$, the integral converges to $0$. you may need a lower bound for whatever application you are working on, but unless $b$ is bound to take values away from zero, you integral may get closed (but positive) to
$0$.
Comment: @CelineHarumi: all I am saying is that a uniform pier bound does not exists as dominated convergence shows when b gets closed to $0$.
Answer: Firstly, note that
\begin{align}
\int_0^1\lvert 1-t^a\rvert^{1/2+p}\lvert 2t-(2+b)t^{b+1}\rvert dt\leq\int_0^12t-(2+b)t^{b+1}=2<\infty.
\end{align}
Suppose that $1/4<p<1/2$. Now, using the Fubini's Theorem and the Binomial Theorem for the fractional exponent $q:=1/2+p$ ,
\begin{align}
&\int_0^1(1-t^a)^q[2t-(2+b)t^{b+1}]dt=\int_0^1\sum_{k=0}^\infty \binom{q}{k}(-t^a)^k[2t-(2+b)t^{b+1}]dt\\
=&\sum_{k=0}^\infty \binom{q}{k}(-1)^k \int_0^1\ t^{ak}[2t-(2+b)t^{b+1}]dt=\sum_{k=0}^\infty \binom{q}{k}(-1)^k\bigg[\frac{2}{ak+2}-\frac{2+b}{ak+2+b}\bigg]\\
=&\sum_{k=1}^\infty \binom{q}{k}(-1)^{k+1}\bigg[\frac{bak}{(ak+2)(ak+2+b)}\bigg]\\
=&\sum_{k\text{ odd}} \binom{q}{k}\bigg[\frac{bak}{(ak+2)(ak+2+b)}\bigg]-\binom{q}{k+1}\bigg[\frac{ba(k+1)}{(a(k+1)+2)(a(k+1)+2+b)}\bigg] \\
=&\sum_{k\text{ odd}} \binom{q}{k}ab\bigg[\frac{k}{(ak+2)(ak+2+b)}+\frac{(k-q)}{(ak+a+2)(ak+a+2+b)}\bigg]  \ *obs. (k-q\geq k-1)\\
\geq & \sum_{k\text{ odd}} \binom{q}{k}ab\bigg[\frac{ak(2ak^2+ak+6k+2bk+a)+2a+4k+2bk}{(ak+2)(ak+2+b)(ak+a+2)(ak+a+2+b)}\bigg] \quad *obs. \binom{q}{k}\geq 0,\text{if } k \text{ odd}\\
>&\binom{q}{1}ab\bigg[\frac{a(2a+a+6+2b+a)+2a+4+2b}{(a+2)(a+2+b)(a+a+2)(a+a+2+b)}\bigg]>0.
\end{align}
where $\binom{q}{k}=q(q-1)\dotsc(q-k+1)/k!$.
When $1/2\leq p\leq 3/4$, similar argument can be applied rewriting the sum by means of even indices.


How can I get the parent entity in a media preprocess hook?

Question: I have a paragraph type with a media entity reference field field_paragraph_image_ref.
I need to modify the media Twig template depending on a field value of the parent, which is a paragraph entity.
There are ways to get the parent entity of a paragraph, but media entities don't have a getParentEntity() method. Also with _referringItem I am unable to get up to the parent paragraph itself.
How can I get the field value from the parent paragraph in the media preprocess?
/**
 * Implements hook_preprocess_HOOK().
 */
function hook_preprocess_media__picture__image_without_link(&$variables) {
  $media = $variables['elements']['#media'];
  // A method to get the parent entity, that I cannot find in
  // var_dump(get_class_methods($media));
  // and finally get a field value from the parent paragraph entity.
}

Comment: Does this answer your question? [Preprocess image: how to get parent entity?](https://drupal.stackexchange.com/questions/245610/preprocess-image-how-to-get-parent-entity) - while this question handles images, it this the same scheme, look at the code line with `_referringItem` and look out for caching issues.
Answer: I passed the parent entity "manually" to the media instance from one hook to the other.
/**
 * Implements hook_preprocess_HOOK().
 */
function MYTHEME_preprocess_paragraph__picture(&$variables) {
  $paragraph = $variables['paragraph'];

  $media = $paragraph->field_paragraph_image_ref->entity;

  // Storing the parent paragraph as an attribute of the media instance.
  $media->parent_paragraph = $paragraph;
}

/**
 * Implements hook_preprocess_HOOK().
 */
function MYTHEME_preprocess_media__picture__image_without_link(&$variables) {
  $media = $variables['elements']['#media'];

  if (isset($media->parent_paragraph)) {
    $paragraph = $media->parent_paragraph;

    // Now the field of the paragraph can be accessed and pass to the media
    // Twig template.
    $variables['my_field'] = $paragraph->my_field;
  }
}

Answer: i have ended up doing:
function hook_preprocess_media__picture__image_without_link(array &$variables) {
   $media = $variables['elements']['#media'];
   $parent = Drupal::entityTypeManager()->getStorage('paragraph')->loadByProperties([
    'field_paragraph_image_ref' => $media->id()
  ]);
   $parent = reset($parent);
   var_dump($parent->get("field_paragraph_image_link_page")->getValue();
}

Comment: not that good because it would load all paragraphs that have that media id for entity ref


Is there a way to make a bar plot from only the non-diagonal elements of a confusion matrix?

Question: I'm running a classification task for 3 depth cateogries of earthquakes (shallow, inter, deep) and a 4th category for noise. I'm trying to visualise the varying size of mislabelling in classification by building a barplot from the non-diagonal elements of a confusion matrix.
making the confusion matrix and removing the diagonal elements:
cm = confusion_matrix(df_val, pred_labels, labels=inp.labels_list, normalize='true')
cm=cm[~np.eye(cm.shape[0],dtype=bool)].reshape(cm.shape[0],-1) #removes diagonal components
frequency = cm.flatten() #making into row vector

building a Dataframe:
mislabel_counts={
    'depth categories': ('shallow','shallow','shallow','inter','inter','inter','deep','deep','deep','noise','noise','noise'),
    'mislabelled as:': ('inter','deep','noise','shallow','deep','noise','shallow','inter','noise','shallow','inter','deep'),
    'frequency': tuple(frequency),
    }
mislabel_counts=pd.DataFrame(mislabel_counts)

The DataFrame ends up looking like this:
   depth categories    mislabelling  frequency
0           shallow           inter   0.362903
1           shallow            deep   0.209677
2           shallow           noise   0.233871
3             inter         shallow   0.177778
4             inter            deep   0.229630
5             inter           noise   0.296296
6              deep         shallow   0.183486
7              deep           inter   0.348624
8              deep           noise   0.238532
9             noise         shallow   0.166667
10            noise           inter   0.318182
11            noise            deep   0.257576

plotting:
sns.set_theme(font_scale=2.5)
sns.color_palette('colorblind')
sns.barplot(data=mislabel_counts, x='depth categories', y='frequency', hue='mislabelled as:', width=0.5)

The plot ends up looking like this:

My issue with this being that the plot saves spaces for the diagonal components even though they've been removed. Does anyone know how to prevent this from happening?
Answer: The easiest solution is to create a sns.catplot with sharex=False:
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

mislabel_counts = {
    'depth categories': ('shallow', 'shallow', 'shallow', 'inter', 'inter', 'inter', 'deep', 'deep', 'deep', 'noise', 'noise', 'noise'),
    'mislabelled as:': ('inter', 'deep', 'noise', 'shallow', 'deep', 'noise', 'shallow', 'inter', 'noise', 'shallow', 'inter', 'deep'),
    'frequency': [.362903, .209677, .233871, .177778, .229630, .296296, .183486, .348624, .238532, .166667, .318182, .257576]
}
mislabel_counts = pd.DataFrame(mislabel_counts)

sns.set()
sns.catplot(data=mislabel_counts, kind='bar', x='mislabelled as:', y='frequency', col='depth categories',
            hue='mislabelled as:', dodge=False, sharex=False)

plt.show()

With some fine-tuning, it looks more similar to a regular bar plot:
sns.set_theme(style='whitegrid')
g = sns.catplot(data=mislabel_counts, kind='bar', x='mislabelled as:', y='frequency', col='depth categories',
            hue='mislabelled as:', dodge=False, sharex=False, aspect=0.5, width=1)
for  ax in g.axes.flat:
    ax.set_xlabel(ax.get_title())
    ax.set_title('')
    ax.margins(x=0.05)
sns.despine(left=True, top=True, right=True)
g.fig.subplots_adjust(wspace=0)


Fitting negative binomial

Question: This is my first post on this forum so let me know if I am missing anything. I have count data which is over-dispersed, which I am treating as the outcome variable. I am looking to model the relationship between this and a set of predictive variables. I have done a poisson, quasi-poisson and now a negative binomial. My data file is named LR_tab7. This is my code for the three glm (bc sorry for the weird data names):
<code>> LR_tab7 <- read.csv("~/ongoing/24) Toine Run/LR_tab7.csv")
> library(MASS)

###quaispoisson

> pois<-glm(lang~Q8_early_lang+Group+Age+sex.2,family=quasipoisson(identity), data=LR_tab7) >summary(pois)

Deviance Residuals:
    Min       1Q   Median       3Q      Max
-1.8729  -1.0403  -0.8129   0.6000   2.7736

Coefficients:
                             Estimate Std. Error t value Pr(>|t|)
(Intercept)                   0.47234    0.14498   3.258 0.001181 **
Q8_early_lang[T.two_to_2.5]   0.27025    0.07606   3.553 0.000409 ***
Q8_early_lang[T.greater_2.5]  0.56247    0.14042   4.006 6.90e-05 ***
Q8_early_lang[T.greater_3]    1.27225    0.31448   4.046 5.86e-05 ***
Group[T.PR]                   0.15055    0.07768   1.938 0.053049 .
Group[T.AF]                   0.25314    0.07901   3.204 0.001423 **
Age                          -0.01997    0.01132  -1.765 0.078020 .
sex.2[T.M]                    0.02245    0.05970   0.376 0.706965
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for quasipoisson family taken to be 1.081649)

    Null deviance: 770.83  on 649  degrees of freedom
Residual deviance: 691.00  on 642  degrees of freedom
AIC: NA

Number of Fisher Scoring iterations: 8
###poisson

> poiswithoutquasi<-glm(lang~Q8_early_lang+Group+Age+sex.2,family=poisson(identity),
data=LR_tab7)
>summary(poiswithoutquasi)
   Coefficients:
                                 Estimate Std. Error z value Pr(>|z|)
    (Intercept)                   0.47234    0.13940   3.388 0.000703 ***
    Q8_early_lang[T.two_to_2.5]   0.27025    0.07313   3.695 0.000220 ***
    Q8_early_lang[T.greater_2.5]  0.56247    0.13501   4.166 3.10e-05 ***
    Q8_early_lang[T.greater_3]    1.27225    0.30238   4.207 2.58e-05 ***
    Group[T.PR]                   0.15055    0.07469   2.016 0.043834 *
    Group[T.AF]                   0.25314    0.07597   3.332 0.000862 ***
    Age                          -0.01997    0.01088  -1.836 0.066393 .
    sex.2[T.M]                    0.02245    0.05740   0.391 0.695682
    ---
    Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

    (Dispersion parameter for poisson family taken to be 1)

        Null deviance: 770.83  on 649  degrees of freedom
    Residual deviance: 691.00  on 642  degrees of freedom
    AIC: 1332.4

    Number of Fisher Scoring iterations: 8

###negative binomial

> NB<-glm.nb(lang~Q8_early_lang+Group+Age+sex.2, data=LR_tab7)
> > summary(NB)
Deviance Residuals:
    Min       1Q   Median       3Q      Max
-1.8692  -1.0287  -0.8478   0.6020   2.7092

Coefficients:
                              Estimate Std. Error z value Pr(>|z|)
(Intercept)                  -0.783814   0.293258  -2.673 0.007522 **
Q8_early_lang[T.two_to_2.5]   0.452437   0.117263   3.858 0.000114 ***
Q8_early_lang[T.greater_2.5]  0.790672   0.146478   5.398 6.74e-08 ***
Q8_early_lang[T.greater_3]    1.272113   0.187517   6.784 1.17e-11 ***
Group[T.PR]                   0.220222   0.192779   1.142 0.253306
Group[T.AF]                   0.436158   0.186818   2.335 0.019561 *
Age                          -0.029722   0.021942  -1.355 0.175557
sex.2[T.M]                    0.005163   0.104520   0.049 0.960603
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for Negative Binomial(239.2127) family taken to be 1)

    Null deviance: 768.89  on 649  degrees of freedom
Residual deviance: 690.69  on 642  degrees of freedom
AIC: 1335.8

Number of Fisher Scoring iterations: 1

              Theta:  239
          Std. Err.:  5570
Warning while fitting theta: alternation limit reached

 2 x log-likelihood:  -1317.8
</code>
I am trying to use the likelihood ratio to show the negative binomial is a better fit model. I couldn't use the quasipoisson in the likelihood ratio test because it kept coming up as NA. This is my code for the regular poisson and negative binomial lrtest:
<code>> X2<-2*(logLik(NB)-logLik(poiswithoutquasi))
> X2
> 'log Lik.' -1.383039(df=9)
> pchisq(X2, df = 1, lower.tail=FALSE)
> 'log Lik.' 1 (df=9)
</code>
Could someone help me interpret what this means and what I am doing wrong. I have very little statistical or R background and I am stuck on how to validate your model.
sample is 650, average count is 0.6 (there are options 0-3 on a scale)
Comment: What's your sample size, what's the average count?
Answer: First of all your negative binomial model was not fitted properly as the warning says, the overdispersion is quite large and the standard error is huge. You'd better fix this first, since it does not convince me that the negative binomial actually provides a better fit.
Secondly you cannot use a regular likelihood ratio test to show that the negative binomial fits better than the poisson (or more formally: prove that the overdispersion parameter is significantly different from zero). This is because the overdispersion parameter is restricted to be positive, and your null hypothesis
$$\theta=0$$
is thus on the edge of the parameter space. As a result the null distribution of the test statistic is an even mixture of a point mass at zero and the $\chi_{(1)}$ distribution (see e.g. Loeys et al.,2012). If you're not an expert at the subject I can recommend the odTest() function in the pscl package.

Fitting negative binomial


Downloading a Large Number of Files from S3

Question: What's the Fastest way to get a large number of files (relatively small 10-50kB) from Amazon S3 from Python? (In the order of 200,000 - million files).
At the moment I am using boto to generate Signed URLs, and using PyCURL to get the files one by one.
Would some type of concurrency help? PyCurl.CurlMulti object?
I am open to all suggestions. Thanks!
Answer: I don't know anything about python, but in general you would want to break the task down into smaller chunks so that they can be run concurrently.  You could break it down by file type, or alphabetical or something, and then run a separate script for each portion of the break down.
Answer: In the case of python, as this is IO bound, multiple threads will use of the CPU, but it will probably use up only one core. If you have multiple cores, you might want to consider the new multiprocessor module.  Even then you may want to have each process use multiple threads.  You would have to do some tweaking of number of processors and threads.
If you do use multiple threads, this is a good candidate for the Queue class.
Comment: Will multiprocessor.Pool objects work in this case? I'm not completely clear with the API for multiprocessing. What is the exact functioning of the Pool object?
I was thinking we cannot simply spawn 10s of thousands of processes so we could keep a pool and as one download finishes another could be added to it with the maximum number of processes defined in the pool definition as `p = Pool(N)`
Is this approach correct?
Answer: You might consider using s3fs, and just running concurrent file system commands from Python.
Answer: I've been using txaws with twisted for S3 work, though what you'd probably want is just to get the authenticated URL and use twisted.web.client.DownloadPage (by default will happily go from stream to file without much interaction).
Twisted makes it easy to run at whatever concurrency you want.  For something on the order of 200,000, I'd probably make a generator and use a cooperator to set my concurrency and just let the generator generate every required download request.
If you're not familiar with twisted, you'll find the model takes a bit of time to get used to, but it's oh so worth it.  In this case, I'd expect it to take minimal CPU and memory overhead, but you'd have to worry about file descriptors.  It's quite easy to mix in perspective broker and farm the work out to multiple machines should you find yourself needing more file descriptors or if you have multiple connections over which you'd like it to pull down.
Comment: As of today, the last commit to https://github.com/twisted/txaws was Dec 2018.
Answer: what about thread + queue, I love this article: Practical threaded programming with Python
Answer: Each job can be done with appropriate tools :)
You want use python for stress testing S3 :), so I suggest find a large volume downloader program and pass link to it.
On Windows I have experience for installing ReGet program (shareware, from http://reget.com) and creating downloading tasks via COM interface.
Of course there may other programs with usable interface exists.
Regards!


Express SQL UNION query Rails way

Question: I got a query which works well, but expressed in SQL. I would like to have the same query expressed using ActiveRecord Query Interface (Arel would also do fine). The query should preferably return ActiveRecord::Relation or, at least, it's result should be convertible to an array of Customer models.
The goal is to fetch company's customers having no associated import_logs with remote_type = 'account', as well as customers having an import_log with remote_type = 'account' and status = 'pending'.
A customer can have no import_logs associated at all, or have an import_log per each remote_type, or only for some remote_types. There can be only one associated import_log with specific remote_type value.
This reflects a requirement that a customer can be imported as account or contact or both and import_log tracks the status of the import.
Although import_log has a polymorphic association with customer, this is not relevant to the task.
Existing query:
Customer.find_by_sql(
  <<-SQL
    SELECT
      customers.*
    FROM
      customers
    WHERE
      company_id = #{@company.id}
      AND NOT EXISTS
          ( SELECT *
            FROM import_logs
            WHERE import_logs.importable_id = customers.id
              AND import_logs.importable_type = 'Customer'
              AND import_logs.remote_type = 'account'
          )
    UNION
    SELECT
      customers.*
    FROM
      customers,
      import_logs
    WHERE
      import_logs.importable_id = customers.id AND
      import_logs.importable_type = 'Customer' AND
      company_id = #{@company.id} AND
      import_logs.remote_type = 'account' AND
      import_logs.status = 'pending';
  SQL
)

Relevant portions of ImportLog model:
create_table "import_logs", force: true do |t|
  t.integer  "importable_id"
  t.string   "importable_type"
  t.string   "status",          default: "pending", null: false
  t.string   "remote_type"
  ...
end

add_index "import_logs", ["importable_id", "importable_type", "remote_type"], unique: true ...

class ImportLog < ActiveRecord::Base
  ...
  belongs_to :importable, polymorphic: true
  ...
end

Relevant portions of Customer model:
create_table "customers", force: true do |t|
  t.integer  "company_id"
  ...
end

class Customer < ActiveRecord::Base
  ...
  belongs_to :company
  has_many :import_logs, as: :importable
  ...
end

And of Company model, just in case:
class Company < ActiveRecord::Base
  ...
  has_many :customers
  ...
end

Comment: The second query in your union seems odd to me. Missing a join maybe. Or `EXISTS`. I wonder if it even does what is intended.
Comment: The second query fetches `customers` having an `import_log` present. Its top `WHERE` condition is synonymous with `INNER JOIN` so it's style difference. It does work as intended.
Comment: It's just that Rails typically fetches associations through `INNER JOIN`s. All right, I see the logic behind it. The only problem then is a `UNION`. I have to think for a while. In the meantime, check out [`scuttle.io`](http://scuttle.io/) that might give some hints on where to start.
Comment: Scuttle.io trips on this query. This is something I checked before posting here.
Answer: merge associations
In fact, there is only one assocation that is driven by a query constant.
"customers"."company_id" = #{@company.id}

It's the same as:
.merge(@company.customers)

...and this looks a bit safer and more sensible
Arel tables
We'll need it soon.
customers = Customer.arel_table

NOT EXISTS ... subquery
Arel can do that, the only not-so-obvious thing is how to reference the outside table:
ne_subquery = ImportLog.where(
                importable_type: Customer.to_s,
                  importable_id: customers[:id],
                    remote_type: 'account'
              ).exists.not

This results in a hunk of Arel AST we can feed to Rails' where-statement.
Now both queries become obvious:
first  = @company.customers.where(ne_subquery)
second = @company.customers.joins(:import_logs).merge(
           ImportLog.where(
           # importable_id: customers[:id], # `joins` already does it
           importable_type: Customer.to_s,
               remote_type: 'acoount',
                    status: 'pending'
           )
         )

That is pretty much a 1-to-1 conversion.
Union
That is a tricky part, and the only solution I have found has terribly ugly syntax and outputs a bit different query. Given A union B we can only build select X.* from (A union B) X. The effect is the same.
All right, let's get to it:
Customer.from(
  customers.create_table_alias(
    first.union(second),
    Customer.table_name
  )
)

Of course, to make this query more readable, you should:

Place it as a scope in Customer class
Split out reusable parts into scopes and associations
Comment: Thanks for the effort! This all works to the point of making the union. At that it trips with this message: `ActiveRecord::StatementInvalid: PG::ProtocolViolation: ERROR:  bind message supplies 0 parameters, but prepared state
ment "" requires 1`. Apparently, `company_id` does not get bound and I haven't yet found a way to fix that.
Comment: A dirty way would be to apply the condition on `company_id` yourself in a `where`-call instead of delegating it to the association. Well, that **would work**. I just don't like that style.
Comment: I tried that and it didn't work. Maybe I'm doing something wrong, could you give an example?
Comment: I can try. What is the SQL that results in this error?
Comment: Uhm... that's odd, two `$1`s. Looks like you'll have to bind a value yourself. I'm not sure how to do that, but I'll take a look. What would probably work is a separate where-clause with a condition string. Try that one out.
Comment: I wonder if Arel is to blame for that or AR's shortcuts. Duh.
Comment: Two `$1`s in fact expected since same `company_id` filter is used within two queries. Separate `where` clause doesn't help since it is what it is, a separate clause. It gets bound to `$2` placeholder. The question is how to bind to `company_id` value to the original `$1`.
Comment: @NicNilov a condition **string** might make a difference since it allows you to bind the value differently (i. e. by `?` or `:key`).
Comment: In a generic case, two `$1`s is a bug, those could have been different companies. Arel does no optimisations such as reducing duplicates, as far as I know. It's just plain Ruby-to-SQL.
Comment: Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/80757/discussion-between-nic-nilov-and-d-side).
Comment: I'm accepting the answer since it does push forward to the solution. I strongly suspect there is an issue in ActiveRecord which manifests in `$1` placeholders assigned incorrectly. I will post more in case I got more info on this.
Answer: Based on the code suggested by @D-side I was able to reach a working solution. Here is the initially suggested code:
customers = Customer.arel_table

ne_subquery = ImportLog.where(
  importable_type: Customer.to_s,
  importable_id: customers['id'],
  remote_type: 'account'
).exists.not

first  = @company.customers.where(ne_subquery)
second = @company.customers.joins(:import_logs).merge(
  ImportLog.where(
    importable_type: Customer.to_s,
    remote_type: 'account',
    status: 'pending'
  )
)

Customer.from(
  customers.create_table_alias(
    first.union(second),
    Customer.table_name
  )
)

Running it leads to this error:
PG::ProtocolViolation: ERROR:  bind message supplies 0 parameters, but prepared statement "" requires 1

: SELECT "customers".* FROM ( SELECT "customers".* FROM "customers"  WHERE "customers"."company_id" = $1 \
AND (NOT (EXISTS (SELECT "import_logs".* FROM "import_logs"  WHERE "import_logs"."importable_type" = 'Customer' \
AND "import_logs"."importable_id" = "customers"."id"))) UNION SELECT "customers".* FROM "customers" \
INNER JOIN "import_logs" ON "import_logs"."importable_id" = "customers"."id" \
AND "import_logs"."importable_type" = 'Customer' WHERE "customers"."company_id" = $1 \
AND "import_logs"."importable_type" = 'Customer' AND "import_logs"."remote_type" = 'contact' \
AND "import_logs"."status" = 'pending' ) "customers"

This error I believe is a manifestation of Rails issue #20077 which at this time stands unresolved. Since the issue relates to parameter binding, making that binding more explicit helps. Here is a working solution:
customers = Customer.arel_table

ne_subquery = ImportLog.where(
  importable_type: Customer.to_s,
  importable_id: customers['id'],
  remote_type: 'account'
).exists.not

first  = Customer.where(ne_subquery).where(company_id: @company.id)
second = Customer.joins(:import_logs).merge(
  ImportLog.where(
    importable_type: Customer.to_s,
    remote_type: 'account',
    status: 'pending'
  )
).where(company_id: @company.id)

Customer.from(
  customers.create_table_alias(
    first.union(second),
    Customer.table_name
  )
)

Note the .where(company_id: @company.id) are applied explicitly and first and second queries begin unscoped.

Express SQL UNION query Rails way


Define custom conversion for structuremap to register Commands, CommandHandlers automatically

Question: I'm using CQRS pattern in my recent project, so I defined some Commands that I call them CommandParameter and CommandHandlers.
For CommandParameters I have these Classes and Interfaces:
public interface ICommandParameter
{
}

public abstract class BaseEntityCommandParameter<T> : IAggregateRoot,ICommandParameter
    where T : ModelEntitySuperType, new()
{
    public T Entity { get; set; }

    protected BaseEntityCommandParameter()
    {
        Entity = new T();
    }
}

public class InsertCommandParameter<T> : BaseEntityCommandParameter<T>
    where T : class, new()
{
}

And for CommandHandlers I defined these Classes and Interfaces:
public interface ICommandHandler<TCommandParameter>
    where TCommandParameter :ICommandParameter
{
    void Handle(TCommandParameter parameter);
    string CommandCode { get; }
}

public class InsertCommandHandler<TCommandParameter, TEntity>
    : ICommandHandler<TCommandParameter>
    where TCommandParameter : BaseEntityCommandParameter<TEntity>, new()
    where TEntity : ModelEntitySuperType, IAggregateRoot, new()

and I used them to make appropriate CommandParameters and CommandHandlers for each Entity for example for Order I have:
public class OrderInsertCommandParameter:InsertCommandParameter<Order>
{
}

public class OrderInsertCommandHandler
    : InsertCommandHandler<OrderInsertCommandParameter, Order>
{
    private readonly IUnitOfWorkFactory _factory;

    public OrderInsertCommandHandler(IUnitOfWorkFactory factory,
        IRepository<Order> repository)
        : base(repository)
    {
        _factory = factory;
    }

    public override void Handle(OrderInsertCommandParameter parameter)
    {
        var uow = _factory.Create();
        parameter.Entity.OrderCreationTime = DateTime.Now;
        base.Handle(parameter);
        uow.Commit();
    }
}

I want to register these CommandParameters and appropriate CommandHandlers using structuremap automatically, How could I define a custom Conversion to do this?
Comment: You don't register `CommandParameter` classes, since they are not services. They are messages, simple data packages (DTOs) that are sent through the services. Your `CommandHandler`s are the services. You need to register them.
Answer: The following should do the trick:
container.Configure(r =>
{
    r.Scan(s =>
    {
        s.Assembly(typeof(ICommandHandler<>).Assembly);
        s.ConnectImplementationsToTypesClosing(typeof(ICommandHandler<>));
    });
});


Do we really need a control group when we can compare the treatment group with the status quo?

Question: Here I am not talking about medical research, where there is placebo effect need to be controlled with the blind design. In social science studies, we almost never do blind experiment. In this case, if we have data from all the subjects, do we really need to have a randomly selected control group to be compared to?
Say, if I have a teaching strategy to improve 5th grade students' math achievement and I want to test its effect. Student math achievement is measured by statewise standard assessments at every end of school year and all data will be extracted from district database directly. Which of the following is better:

randomly select 20 schools from 200 schools in a district, and then randomly assign 10 schools to treatment group (use the new teaching strategy) and 10 schools to control group (do business as usual). Compare the control and treatment.
randomly select 10 treatment schools from the same district. Compare the treatment with the rest of the district (control on previous achievement and demographics)?
randomly select 10 treatment schools from the same district. Compare the treatment with 10 randomly selected schools (post hoc) from the same district (control on previous achievement and demographics)?

Is method 1 a better design and why?
Comment: Would you not still expect a placebo effect in this situation (that telling teachers to use a new teaching strategy would have a placebo effect on the outcomes)?
Comment: To amplify the comment by @James: None of those designs will work as intended, because they do not rule out alternative explanations like "the effects you think you see are due only to the fact of paying obvious attention to the treatment schools, not that the treatment works." Arguably, the fallacy of overlooking this possibility is at the heart of many failed social and political experiments (as well as underpinning a great deal of pseudoscience).
Answer: If your control group was just going to be "business as usual" then by all means just use your out of sample as the control. From your description you're really just asking if you should use equal N's in control and experimental conditions. When considering the options you gave, the better defined control (large N) is the better solution.
However, quite often control is not business as usual. In that case you definitely need an assigned control group. Furthermore, control might be a matched control where the specific schools in the control group are matched as close as possible to the test group. When that happens your suggestion is not necessarily the best plan but that would depend on a number of factors.


PANDAS - PYTHON - LOCALIZAR VALORES DIFERENTES

Question: Boa tarde a todos!
Estou iniciando alguns estudos de Python e para por em pratica estou tentando otimizar algumas rotinas para o meu trabalho.
No setor onde trabalho temos o habito de cruzar muitos dados, principalmente os telefones de clientes, as vezes usamos Excel para fazer um PROCX ou V, mas decidi dar uma otimizada nas rotinas.
No caso estou usando a biblioteca pandas e tenho duas colunas iguais.
DF1 = NUMEROS JA USADOS
DF2 = NUMEROS QUE VOU USAR
Meu objetivo é fazer um cruzamento gerando um terceiro Data frame, onde os números que vou usar cruze com o já usados e retorne apenas os valores que não se repetem.
Esse terceiro DF seria meu arquivo final onde teria apenas oque não teria sido discado.
import pandas as pd

###LENDO ARQUIVO NOVO
Novo_Mailing_df = pd.read_csv('Novo_Mailing.csv')

Display('Novo_mailing_df)

0   21900000001
1   21900000002
2   21900000003
3   21900000004
4   21900000005
5   21900000006
6   21900000007
7   21900000008
8   21900000009
9   21900000010
10  21900000011
11  21900000012
12  21900000013
13  21900000014
14  21900000015
15  21900000016
16  21900000017
17  21900000018
18  21900000019
19  21900000020
20  2122771300
21  2122771301
22  2122771302
23  2122771303
24  2122771304
25  2122771305
26  2122771306
27  2122771307
28  2122771308
29  2122771309

###UPANDO JA TRABALHADOS
Discados_Callflex_df = pd.read_csv('Discados_Callflex.csv')

display(Discados_Callflex_df)

0   21900000001
1   21900000002
2   21900000003
3   21900000004
4   21900000005
5   21900000006
6   21900000007
7   21900000008
8   21900000009
9   21900000010
10  21900000011
11  21900000012
12  21900000013
13  21900000014
14  21900000015
15  21900000016
16  21900000017
17  21900000018
18  21900000019
19  21900000020

###USANDO OS JOINS PARA RETORNAR A DIFERENÇA

cruzamento_df = pd.merge(Novo_Mailing_df, Discados_Callflex_df, how='outer', on='telefone', indicator=True)

display(cruzamento_df)

0   21900000001 both
1   21900000002 both
2   21900000003 both
3   21900000004 both
4   21900000005 both
5   21900000006 both
6   21900000007 both
7   21900000008 both
8   21900000009 both
9   21900000010 both
10  21900000011 both
11  21900000012 both
12  21900000013 both
13  21900000014 both
14  21900000015 both
15  21900000016 both
16  21900000017 both
17  21900000018 both
18  21900000019 both
19  21900000020 both
20  2122771300  left_only
21  2122771301  left_only
22  2122771302  left_only
23  2122771303  left_only
24  2122771304  left_only
25  2122771305  left_only
26  2122771306  left_only
27  2122771307  left_only
28  2122771308  left_only
29  2122771309  left_only

Foi isso que fiz por hora, mas apenas consigo usar maneiras de devolver oque tem em comum entre um e outro, porem meu objetivo é devolver oque tem tem de diferente entre eles.
Meu objetivo final é saber se no que eu quero trabalhar hoje ja foi trabaçhado em algum outro dia, e no DF novo devolver só oque não trabalhei.
Answer: Você pode utilizar o isin do pandas
Importando o pandas
import pandas as pd

Criando os dataframes:
Novo_Mailing_df = pd.read_csv('../DADOS/Novo_Mailing.csv', sep = ';', names=['Coluna1'])
Discados_Callflex_df = pd.read_csv('../DADOS/Discados_Callflex.csv', sep = ';', names=['Coluna1'])

Aqui é a parte onde verificamos se valores de um dataframe estão presentes no outro:
Novo_Mailing_df[~Novo_Mailing_df['Coluna1'].isin(Discados_Callflex_df['Coluna1'])]

Saída:

Coluna1

20
2122771300

21
2122771301

22
2122771302

23
2122771303

24
2122771304

25
2122771305

26
2122771306

27
2122771307

28
2122771308

29
2122771309
Comment: Obrigado pelo retorno!
Vou tentar aplicar a sua explicação no meu projeto assim que tiver um tempo no  trabalho.
Também irei pesquisar sobre o isin para aprender mais sobre ele.
Comment: Obrigado pela sua ajuda!
Sua solução realmente funcionou , e enquanto pesquisava sobre oque me indicou aprendi algumas outras coisas sobre o PANDAS.
Comment: Fala @DiogenesCosta , tudo bom? que ótimo que pude contribuir e muito bom saber que você aprendeu mais coisas sobre o pandas. Grande abraço!


Trocar cor do "button" ao clicar nele ou ao validar preenchimento texto

Question: Olá galera estou tendo dificuldade em fazer com que o botão troque de cor quando os campos EditTexts estiverem preenchidos e voltar a cor inicial quando os campos estivem vazios e também trocar de cor quando pressionado.
Trecho de código presente no onCreate:
senha.addTextChangedListener(new TextWatcher(){
    @Override
    public void afterTextChanged(Editable s) {
        if(!validar(matricula.getText().toString(), senha.getText().toString())){
            //btn_Entrar.setBackgroundColor(Color.parseColor("#c9c9c9"));
            btn_Entrar.setClickable(false);
        }else if(!matricula.getText().toString().equals("")){
            btn_Entrar.setBackgroundColor(Color.parseColor("#2D89db"));
            btn_Entrar.setClickable(true);
        }
    }
    public void beforeTextChanged(CharSequence s, int start,int count, int after) {}
    public void onTextChanged(CharSequence s, int start,int before, int count) {}
});

Propriedade no XML:
android:background="@drawable/shape_edit_text"

shape_edit_text:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/shape_buttonlogin_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/shape_buttonlogin_normal"/>
</selector>

shape_buttonlogin_pressed:
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#ff0000"/>
</shape>

shape_buttonlogin_normal:
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#c9c9c9" />
</shape>

Answer: Quando você usa no XML o atributo background esta dando um StateListDrawable como background do Button. Já quando usa Button.setBackgroundColor está sobrescrevendo o background com um ColorDrawable e com isso perde o comportamento de estado para uma cor fixa, causando o problema.
Logo como solução usando o State List Drawable que já definiu para seu botão, podemos adicionar o estado state_enabled para atingir o objetivo.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" android:state_enabled="true">
        <shape>
            <solid android:color="#ff0000"/>
        </shape>
    </item>

    <item android:state_enabled="true" android:state_pressed="false">
        <shape>
            <solid android:color="#2D89db" />
        </shape>
    </item>

    <item android:state_enabled="false">
        <shape>
            <solid android:color="#c9c9c9" />
        </shape>
    </item>
</selector>

Simplificando sua lógica de verificação, ficaria:
senha.addTextChangedListener(new TextWatcher(){
    @Override
    public void afterTextChanged(Editable s) {
        if(!validar(matricula.getText().toString(), senha.getText().toString())){
            btn_Entrar.setEnabled(false);
        } else if(!matricula.getText().toString().equals("")){
            btn_Entrar.setEnabled(true);
        }
    }

    public void beforeTextChanged(CharSequence s, int start,int count, int after) {}
    public void onTextChanged(CharSequence s, int start,int before, int count) {}
});


Erro: procedure or function has too many arguments specified

Question: Criei uma procedure para popular uma grid view:

   create procedure [dbo].[spc_listaafiliadosadmin]
(
    @nome varchar(100),
    @login varchar(100),
    @cpf varchar(100)
)
as
begin
    if(@nome is not null)
        begin
            select
            a.idafiliado,
            u.login, u.nome NomeUsuario, upai.login  loginpai, u.email,u.cpfcnpj,
            'Sempre exclusivo - Grupo de Consumo' as NomeClube
            ,dbo.getsaldo(a.idafiliado) as saldo
            from usuario u
            inner join afiliado a on a.idusuario = u.idusuario
            inner join afiliado apai on apai.idafiliado = a.idafiliadopai
            inner join usuario upai on upai.idusuario = apai.idusuario
            where u.ativo = 1 and u.nome like @nome
        end
    else
        if (@login is not null)
            begin
                select
                a.idafiliado,
                u.login, u.nome NomeUsuario, upai.login  loginpai, u.email,u.cpfcnpj,
                'Sempre exclusivo - Grupo de Consumo' as NomeClube
                ,dbo.getsaldo(a.idafiliado) as saldo
                from usuario u
                inner join afiliado a on a.idusuario = u.idusuario
                inner join afiliado apai on apai.idafiliado = a.idafiliadopai
                inner join usuario upai on upai.idusuario = apai.idusuario
                where u.ativo = 1 and u.login like @login
            end
    else
        if (@cpf is not null )
            begin
                select
                a.idafiliado,
                u.login, u.nome NomeUsuario, upai.login  loginpai, u.email,u.cpfcnpj,
                'Sempre exclusivo - Grupo de Consumo' as NomeClube
                ,dbo.getsaldo(a.idafiliado) as saldo
                from usuario u
                inner join afiliado a on a.idusuario = u.idusuario
                inner join afiliado apai on apai.idafiliado = a.idafiliadopai
                inner join usuario upai on upai.idusuario = apai.idusuario
                where u.ativo = 1 and u.cpfcnpj = @cpf
            end
end
GO

no código a procedure é chamada da seguinte forma:
if (UsuarioUtil.IsAdmin())
{
    //UsuarioUtil uUtil = new UsuarioUtil();
    string nome = txtNome.Text;
    string login = TxtLogin.Text;
    string cpf = TxtCpf.Text;
    gdvAfiliados.DataSource = _DB.spc_listaafiliadosadmin(nome,login,cpf).ExecuteDataTable(); //_DB.vw_Afiliado.Where(a => a.ativo).ToList(); // uUtil.getAfils("");
    gdvAfiliados.DataBind();

    gdvAfiliados.HeaderRow.TableSection = TableRowSection.TableHeader;

    ScriptManager.RegisterStartupScript(Page, typeof(Page), "", "SetDataTable();", true);

}

Porém quando é executada dá esse erro

Procedure or function spc_listaafiliadosadmin has too many arguments specified.

Método da procedure:
public SPClass spc_listaafiliadosadmin(string nome,string login,string cpf)
{
    SPClass c = new SPClass(this);
    DbCommand cmd = c.context.Database.Connection.CreateCommand();
    cmd.CommandText = "spc_listaafiliadosadmin";
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandTimeout = 600000;

    if (nome != null)
        cmd.Parameters.Add(new SqlParameter("@nome",nome));

    if (login != null)
        cmd.Parameters.Add(new SqlParameter("@login",login));

    if (cpf != null)
        cmd.Parameters.Add(new SqlParameter("@cpf",cpf));

    c.command = cmd;

    return c;
}

Comment: O que é isto aqui? `_DB.spc_listaafiliadosadmin(nome,login,cpf)`. É uma classe do C#?
Comment: Eu crio uma instância do banco -> _DB <-  e chamo a procedure por ali!
Comment: Certo, mas eu não entendi esse `spc_listaafiliadosadmin`. Isto é implementado em algum lugar?
Comment: sim, as classes referentes as procedures são criadas dentro do projeto.
Comment: Pode colar o código do método na sua pergunta?
Comment: Ok..............
Answer: Esse problema só pode ser na passagem dos seus parâmetros para a proc, quando você faz os seus
if (nome != null)
if (cpf != null)
if (cpf != null)

isso pode ser null e com isso o paramentos não vai ser adicionados para a proc.
Corrigia essa parte para que passe alguma coisa mesmo que seja um valor Empty.
cmd.CommandText = "spc_listaafiliadosadmin";
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 600000;

if (nome != null)
    cmd.Parameters.Add(new SqlParameter("@nome",nome));

if (login != null)
    cmd.Parameters.Add(new SqlParameter("@login",login));

if (cpf != null)
    cmd.Parameters.Add(new SqlParameter("@cpf",cpf));


Cascading Resolution in Unity

Question: Suppose I have two types, TypeA and TypeB, that I want to register with Unity.  TypeB depends on TypeA so I want to inject TypeA into type B through constructor injection.  So I would like to write something like the following and have Unity be smart enough to cascade the resolution for me:
_container.RegisterType<ITypeA, TypeA>();
_container.RegisterType<ITypeB, TypeB>();

How can I tell Unity to resolve TypeA and inject into TypeB?
It looks like this is possible if using a config file, but I don't know how you would do it programmaticaly:
<type name="typeB" type="ITypeB" mapTo="TypeB">
   <lifetime type="Singleton"/>
   <typeConfig extensionType="...">
      <constructor>
        <param name="typeA" parameterType="ITypeA">
           <dependency/>
        </param>
      </constructor>
   </typeConfig>
</type>

Thanks in advance for any suggestions!

EDIT: So, Unity does handle this for me.  However, I think my issue is that I have a class with two constructors:
public TypeB(TypeA typeA)
{
    _x = typeA;
}

public TypeB() : this(Something.Value)
{
}

It seems that Unity is having trouble knowing which constructor to use.  The first constructor is for unit testing and the second should be used at during runtime.  Unity is having trouble with this.
Answer: You do it like this:
class TypeA
{
}

class TypeB
{

    [InjectionConstructor]
    public TypeB([Dependency] TypeA typeOfA)
    {

    }
}

Cascading Resolution in Unity


How to stop a cmd/ping flooder

Question: I banned a guy from my site for breaking rules and he mailed me saying he will cmd flood/ping the site until I unban him. He's been bringing the site down for three days now.
My site is with a hosting company and they've not been that helpful, just repeating the same things (check access logs for IPs and block them in htaccess). I can't see any issues in access logs though and assume ping floods wouldn't even get in there.
What can I do to stop him?
He's not from UK/US. I've mailed his ISP and mail provider days ago and they've done nothing, not even replied.
Additional: I closed off my site yet when I use the shell to view live updates of the access logs, I still see pages getting requested constantly. I've added these IPs to my htaccess but it doesn't stop, how can I sort that?
Comment: A pcap file with a small sample of the traffic would reveal a lot. Without that your question is more or less impossible to answer.
Comment: Do you have root access on the server or is it a managed hosting ?
Answer: You can do two things.
1) Get the attack blocked on a router. Blocking in .htaccess can't help you much against a DOS attack based on ping flood (i.e. ICMP protocol). You can, however, block it on a firewall or router. If there is a significant traffic that this attacker generates, your hosting provider will likely be interested to get it blocked. So, ask your hosting to block the attacker if this is the case. If you block the attacker using .htaccess only, you can prevent some HTTP based attacks as you might lower the amount of resources needed to process each request. This is not a very common case, however.
2) Find an abuse contact and write an abuse complaint. Use WHOIS tool to find ISP of the attacker. There is usually an abuse contact. You say that you have contacted the attacker's ISP without response. Are you sure you have used the right contact? Also, if the final and possibly smaller ISP does not react to your abuse complaints, you might contact the ISP on the next level (quite often there are multiple levels).
You should always be very specific and include detailed logs on when and what the attacker did to you. Such specific abuse complaints are more likely to be processed.
Answer: From what you say, I assume you are using a mutualized hosting and don't have access to the server serving your request.
Therefore, if the banned client is actually flooding the server with icmp ping requests, there is not much you can do, as htaccess don't affect pings. However, it is very hard to take a server down that way, unless you have access to numerous hosts, so you should probably check that it is not something else, such as http requests that are much more ressource intensive.

How to stop a cmd/ping flooder


Checar se múltiplos elementos HTML contém um valor de texto específico

Question: Quero veríficar se os elementos inseridos em inputs já existem em algum campo da tabela onde a classe é chamada "codigo".
Criei uma variável para conter todas as classes código para então verificar.
let codigos = document.querySelectorAll(".codigo")

Na parte "//criação de nova tabela" em verificações (ifs) foi inserida a condição que está como
else if (codigos.contains(id)) { window.alert("Id já cadastrado") }

Segue o código inteiro do projeto para os testes:

let botaoInserirPessoa = document.querySelector("#inserirPessoa");
let tabela = document.querySelector("#cadastrosPessoa")
let codigos = document.querySelectorAll(".codigo")

let campoId = document.querySelector("#id")
let campoNome = document.querySelector("#nome")
let campoCpf = document.querySelector("#cpf")
let campoRg = document.querySelector("#rg")
let campoNascimento = document.querySelector("#dataNascimento")
let campoSexo = document.querySelector("#sexo")

let bancoDeCadastros = [
  {
    "id": 1,
    "nome": "Francisca Julia da Costa",
    "cpf": "457.696.936-65",
    "rg": "47.360.897-2",
    "data_nasc": "23/03/1944",
    "sexo": "Feminino"
  },
  {
    "id": 2,
    "nome": "Noah Felipe Silva",
    "cpf": "956.531.431-70",
    "rg": "40.974.782-8",
    "data_nasc": "11/07/1964",
    "sexo": "Masculino"
  },
  {
    "id": 3,
    "nome": "Alícia Rosângela Melo",
    "cpf": "066.291.353-18",
    "rg": "36.214.141-1",
    "data_nasc": "18/02/1978",
    "sexo": "Feminino"
  },
  {
    "id": 4,
    "nome": "Cristiane Renata Ana das Neves",
    "cpf": "946.074.401-08",
    "rg": "32.301.736-8",
    "data_nasc": "10/05/1966",
    "sexo": "Feminino"
  },
  {
    "id": 5,
    "nome": "Priscila Benedita Vanessa Ferreira",
    "cpf": "888.282.394-68",
    "rg": "44.524.670-4",
    "data_nasc": "15/11/1966",
    "sexo": "Feminino"
  },
  {
    "id": 6,
    "nome": "Bianca Carolina Nunes",
    "cpf": "484.323.140-13",
    "rg": "44.466.563-8",
    "data_nasc": "16/03/1948",
    "sexo": "Feminino"
  },
  {
    "id": 7,
    "nome": "Yuri Vicente Manuel Silveira",
    "cpf": "250.111.093-56",
    "rg": "18.597.361-9",
    "data_nasc": "05/09/1974",
    "sexo": "Masculino"
  },
  {
    "id": 8,
    "nome": "Melissa Alessandra Barros",
    "cpf": "446.675.916-25",
    "rg": "25.598.673-7",
    "data_nasc": "13/01/2000",
    "sexo": "Feminino"
  },
  {
    "id": 9,
    "nome": "Márcia Daniela Lara da Cruz",
    "cpf": "932.803.826-02",
    "rg": "40.653.176-6",
    "data_nasc": "04/06/1986",
    "sexo": "Feminino"
  },
  {
    "id": 10,
    "nome": "Filipe Anderson Rafael Assis",
    "cpf": "704.869.005-41",
    "rg": "39.383.334-3",
    "data_nasc": "19/11/1995",
    "sexo": "Masculino"
  },
  {
    "id": 11,
    "nome": "Luís Vinicius Sebastião Jesus",
    "cpf": "035.960.588-56",
    "rg": "29.915.692-8",
    "data_nasc": "23/04/1965",
    "sexo": "Masculino"
  },
  {
    "id": 12,
    "nome": "Cecília Caroline Nascimento",
    "cpf": "034.396.672-78",
    "rg": "48.673.066-9",
    "data_nasc": "04/02/1951",
    "sexo": "Feminino"
  },
  {
    "id": 13,
    "nome": "Raimunda Sandra Ferreira",
    "cpf": "757.187.891-85",
    "rg": "21.189.806-5",
    "data_nasc": "17/11/1974",
    "sexo": "Feminino"
  },
  {
    "id": 14,
    "nome": "Andreia Isabelly Juliana Melo",
    "cpf": "736.348.985-85",
    "rg": "24.677.018-1",
    "data_nasc": "11/10/1963",
    "sexo": "Feminino"
  },
  {
    "id": 15,
    "nome": "Nicolas Bernardo Moura",
    "cpf": "061.370.865-26",
    "rg": "16.302.343-8",
    "data_nasc": "08/06/1989",
    "sexo": "Masculino"
  },
  {
    "id": 16,
    "nome": "Diego Benjamin Tiago da Luz",
    "cpf": "991.152.443-42",
    "rg": "38.315.100-4",
    "data_nasc": "04/06/1990",
    "sexo": "Masculino"
  },
  {
    "id": 17,
    "nome": "Marlene Emanuelly Yasmin Cavalcanti",
    "cpf": "781.233.625-52",
    "rg": "21.068.742-3",
    "data_nasc": "15/10/1961",
    "sexo": "Feminino"
  },
  {
    "id": 18,
    "nome": "Vanessa Isabela da Luz",
    "cpf": "872.138.490-85",
    "rg": "44.289.193-3",
    "data_nasc": "05/09/1982",
    "sexo": "Feminino"
  },
  {
    "id": 19,
    "nome": "Camila Flávia Vera Mendes",
    "cpf": "790.524.518-77",
    "rg": "40.847.994-2",
    "data_nasc": "01/07/1952",
    "sexo": "Feminino"
  },
  {
    "id": 20,
    "nome": "Luana Lívia Lara Campos",
    "cpf": "532.901.056-01",
    "rg": "14.732.946-2",
    "data_nasc": "19/08/1981",
    "sexo": "Feminino"
  },
  {
    "id": 21,
    "nome": "Josefa Nina Galvão",
    "cpf": "379.743.833-80",
    "rg": "13.878.690-2",
    "data_nasc": "27/01/1943",
    "sexo": "Feminino"
  },
  {
    "id": 22,
    "nome": "Natália Isabelly Silvana da Paz",
    "cpf": "454.172.867-29",
    "rg": "22.986.096-5",
    "data_nasc": "17/09/1980",
    "sexo": "Feminino"
  },
  {
    "id": 23,
    "nome": "Amanda Sebastiana Vieira",
    "cpf": "703.941.038-90",
    "rg": "50.251.196-5",
    "data_nasc": "08/06/1960",
    "sexo": "Feminino"
  },
  {
    "id": 24,
    "nome": "Noah Pedro Alves",
    "cpf": "074.733.106-58",
    "rg": "32.110.015-3",
    "data_nasc": "14/08/1992",
    "sexo": "Masculino"
  },
  {
    "id": 25,
    "nome": "Giovana Camila da Conceição",
    "cpf": "418.746.840-49",
    "rg": "35.520.215-3",
    "data_nasc": "25/06/1947",
    "sexo": "Feminino"
  },
  {
    "id": 26,
    "nome": "Pietro Joaquim Emanuel Gonçalves",
    "cpf": "003.502.230-25",
    "rg": "12.099.068-4",
    "data_nasc": "21/04/1999",
    "sexo": "Masculino"
  },
  {
    "id": 27,
    "nome": "Tereza Kamilly Mariana Porto",
    "cpf": "050.946.705-90",
    "rg": "39.830.941-3",
    "data_nasc": "16/06/1959",
    "sexo": "Feminino"
  },
  {
    "id": 28,
    "nome": "Caroline Emanuelly Lívia Moreira",
    "cpf": "673.831.546-57",
    "rg": "24.561.727-9",
    "data_nasc": "09/04/1948",
    "sexo": "Feminino"
  },
  {
    "id": 29,
    "nome": "Gabriel Ricardo da Paz",
    "cpf": "829.270.172-98",
    "rg": "30.407.114-6",
    "data_nasc": "24/03/1980",
    "sexo": "Masculino"
  },
  {
    "id": 30,
    "nome": "Giovanni Igor Augusto Pires",
    "cpf": "584.880.490-72",
    "rg": "33.874.271-2",
    "data_nasc": "24/08/1946",
    "sexo": "Masculino"
  }
]

//função que transforma todos dados pessoais na tabela editáveis.
let permitirEdicaoDados = ()=>{

  let dadosCadastrados = document.querySelectorAll(".dPessoa")

  dadosCadastrados.forEach(dado =>{
    dado.setAttribute("contenteditable", true)

  })
}

//função que adiciona o evento excluir ao botão lixeira.
let eventoBotaoExcluir = ()=>{

  let botaoExcluir = document.querySelectorAll('.material-icons')
  botaoExcluir.forEach((botao) => {

    botao.addEventListener('click', () => {
      let idLinha = botao.getAttribute('data-id')

      document.getElementById(`${idLinha}`).remove()
    })
  })
}

//função que inclui os cadastros já existentes na base de dados
let renderizarCadastros = banco =>{

  for (let i=0; i<bancoDeCadastros.length; i++){

      let novaTabela = `

                          ${banco[i].id}
                          ${banco[i].nome}
                          ${banco[i].cpf}
                          ${banco[i].rg}
                          ${banco[i].data_nasc}
                          ${banco[i].sexo}
                          delete_forever

`

      tabela.innerHTML += novaTabela
  }
}

let limparCamposFormulario = ()=>{
  campoId.value = ""
  campoNome.value = ""
  campoCpf.value = ""
  campoRg.value = ""
  campoNascimento.value = ""
  campoSexo.value = ""
}

//criação de uma nova tabela
botaoInserirPessoa.addEventListener("click", ()=>{

  let id = campoId.value
  let nome = campoNome.value
  let cpf = campoCpf.value
  let rg = campoRg.value
  let nascimento = campoNascimento.value
  let sexo = campoSexo.value

  if(id == "" || nome == "" || cpf == "" || rg == "" || nascimento == "" || sexo == ""){

    window.alert("Preencha todos os campos")

  }

  else if(tabelaTd.classList.contains(id)){

    window.alert("Id já cadastrado")

  }

  else{

    let novaTabela = `

                        ${id}
                        ${nome}
                        ${cpf}
                        ${rg}
                        ${nascimento}
                        ${sexo}
                        delete_forever

`

  tabela.innerHTML += novaTabela

  let novoCadastro = {
    id: id,
    nome: nome,
    cpf: cpf,
    rg: rg,
    data_nasc: nascimento,
    sexo: sexo,
  }

  bancoDeCadastros.push(novoCadastro)

  limparCamposFormulario()
  eventoBotaoExcluir()
  permitirEdicaoDados()
  }

})

renderizarCadastros(bancoDeCadastros)
eventoBotaoExcluir()
permitirEdicaoDados()
/*Root*/
*{
    font-size: 16px;
    margin: 0;
    padding: 0;
    font-family: Roboto;
    color: rgb(51, 51, 51);
}

/*Container principal*/
#container{
    text-align: center;
    align-items: center;
    margin: 15px;
}

/*Campos do formulário*/
#inserirDados div, input, button{
    margin: 5px 0 5px 0;
    padding: 3px;
}

#inserirDados input{
    width: 120px;
}

#inserirDados button{
    width: 60px;
    border: 0;
    border-radius: 8px;

    color: rgb(248, 248, 248);
    background-color: rgb(38, 98, 230);

    transition: .3s;
}
#inserirDados button:hover {
    cursor: pointer;
    background-color: rgb(42, 89, 190);
    transition: .3s;
}

/*Tabela*/
#cadastrosPessoa{
    margin: auto;
    border-collapse: collapse;
    text-align: left;
}

#cadastrosPessoa tr{
    transition: .2s;
}

#cadastrosPessoa tr:hover{
    background-color: rgb(235, 235, 235);
    transition: .2s;
}

#cadastrosPessoa th{
    border: 0;
}

#cadastrosPessoa td{
    border-bottom: 1px solid  rgb(51, 233, 111);
    font-weight: normal;

    padding: 5px;
}

#cadastrosPessoa .material-icons{
    cursor:pointer;
    display: contents;

    transition: .3s;
}

#cadastrosPessoa .material-icons:hover{

    color: tomato;
    transition: .3s;
}
<!DOCTYPE html>

    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Listagem de pessoas</title>

Adicionar colaborador

            <input type="text" name="id" id="id" placeholder="ID">
            <input type="text" name="nome" id="nome" placeholder="Nome">
            <input type="text" name="cpf" id="cpf" placeholder="CPF">
            <input type="text" name="rg" id="rg" placeholder="RG">
            <input type="text" name="dataNascimento" id="dataNascimento" placeholder="Nascimento">
            <input type="text" name="sexo" id="sexo" placeholder="Sexo">

            <button id="inserirPessoa">OK</button>

            ID
            Nome
            CPF
            RG
            Nascimento
            Sexo

<link rel="stylesheet" href="/main.css">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons"
rel="stylesheet">

Answer: E sempre bom você separar as funcionalidades do seu código e funcões específicas. No seu caso seria bom criar uma funcão que somente verifica se algum id já foi cadastrado e na hora de inserir um novo item na tabela, execute a funcão que verifica se o id informado já existe.
Vou criar uma funcão chamada checkIdAlreadyExist(id) onde ela recebe o id do item que vai ser cadastrado e verifica se ele já existe. Vou colocar o codigo e as explicacões:
Código:
function checkIdAlreadyExist(id) {
  const trs = document.querySelectorAll('tr');
  const idsArray = [];

  trs.forEach((tr) => {
    idsArray.push(tr.getAttribute('id'));
  });

  if (idsArray.includes(id)) {
    return true;
  } else {
    return false;
  }
}

Explicacões:

const trs = document.querySelectorAll('tr'); seleciona todos os elementos que sao um

const idsArray = []; array que vamos usar para armazenar os ids
No código abaixo, vamos iterar sobre todos os elementos tr e vamos "pegar" o atributo id de cada um deles usando getAttribute e adicioná-los ao array de ids que criamos (idsArray):

  trs.forEach((tr) => {
   idsArray.push(tr.getAttribute('id'));
  })

if (idsArray.includes(id)) {...} aqui fazemos a verificação pra ver se o id informado já existe, através do método includes, onde ele retorna true ou false.

Esta é a função e ela serve somente para isto e voce vai chamá-la dentro da função que adiciona um novo item na tabela. Baseado no retorno dela, voce decide o que fazer. No seu caso enviar uma mensagem para o usuário.
Insira esta chamada de funcão dentro do seu if, assim:
...
else if (checkIdAlreadyExist(id)) {
        window.alert('Id já cadastrado');
      }
....

Como fica seu código completo:

    let botaoInserirPessoa = document.querySelector('#inserirPessoa');
    let tabela = document.querySelector('#cadastrosPessoa');
    let codigos = document.querySelectorAll('.codigo');

    let campoId = document.querySelector('#id');
    let campoNome = document.querySelector('#nome');
    let campoCpf = document.querySelector('#cpf');
    let campoRg = document.querySelector('#rg');
    let campoNascimento = document.querySelector('#dataNascimento');
    let campoSexo = document.querySelector('#sexo');

    let bancoDeCadastros = [
      {
        id: 1,
        nome: 'Francisca Julia da Costa',
        cpf: '457.696.936-65',
        rg: '47.360.897-2',
        data_nasc: '23/03/1944',
        sexo: 'Feminino',
      },
      {
        id: 2,
        nome: 'Noah Felipe Silva',
        cpf: '956.531.431-70',
        rg: '40.974.782-8',
        data_nasc: '11/07/1964',
        sexo: 'Masculino',
      },
      {
        id: 3,
        nome: 'Alícia Rosângela Melo',
        cpf: '066.291.353-18',
        rg: '36.214.141-1',
        data_nasc: '18/02/1978',
        sexo: 'Feminino',
      },
      {
        id: 4,
        nome: 'Cristiane Renata Ana das Neves',
        cpf: '946.074.401-08',
        rg: '32.301.736-8',
        data_nasc: '10/05/1966',
        sexo: 'Feminino',
      },
      {
        id: 5,
        nome: 'Priscila Benedita Vanessa Ferreira',
        cpf: '888.282.394-68',
        rg: '44.524.670-4',
        data_nasc: '15/11/1966',
        sexo: 'Feminino',
      },
      {
        id: 6,
        nome: 'Bianca Carolina Nunes',
        cpf: '484.323.140-13',
        rg: '44.466.563-8',
        data_nasc: '16/03/1948',
        sexo: 'Feminino',
      },
      {
        id: 7,
        nome: 'Yuri Vicente Manuel Silveira',
        cpf: '250.111.093-56',
        rg: '18.597.361-9',
        data_nasc: '05/09/1974',
        sexo: 'Masculino',
      },
      {
        id: 8,
        nome: 'Melissa Alessandra Barros',
        cpf: '446.675.916-25',
        rg: '25.598.673-7',
        data_nasc: '13/01/2000',
        sexo: 'Feminino',
      },
      {
        id: 9,
        nome: 'Márcia Daniela Lara da Cruz',
        cpf: '932.803.826-02',
        rg: '40.653.176-6',
        data_nasc: '04/06/1986',
        sexo: 'Feminino',
      },
      {
        id: 10,
        nome: 'Filipe Anderson Rafael Assis',
        cpf: '704.869.005-41',
        rg: '39.383.334-3',
        data_nasc: '19/11/1995',
        sexo: 'Masculino',
      },
      {
        id: 11,
        nome: 'Luís Vinicius Sebastião Jesus',
        cpf: '035.960.588-56',
        rg: '29.915.692-8',
        data_nasc: '23/04/1965',
        sexo: 'Masculino',
      },
      {
        id: 12,
        nome: 'Cecília Caroline Nascimento',
        cpf: '034.396.672-78',
        rg: '48.673.066-9',
        data_nasc: '04/02/1951',
        sexo: 'Feminino',
      },
      {
        id: 13,
        nome: 'Raimunda Sandra Ferreira',
        cpf: '757.187.891-85',
        rg: '21.189.806-5',
        data_nasc: '17/11/1974',
        sexo: 'Feminino',
      },
      {
        id: 14,
        nome: 'Andreia Isabelly Juliana Melo',
        cpf: '736.348.985-85',
        rg: '24.677.018-1',
        data_nasc: '11/10/1963',
        sexo: 'Feminino',
      },
      {
        id: 15,
        nome: 'Nicolas Bernardo Moura',
        cpf: '061.370.865-26',
        rg: '16.302.343-8',
        data_nasc: '08/06/1989',
        sexo: 'Masculino',
      },
      {
        id: 16,
        nome: 'Diego Benjamin Tiago da Luz',
        cpf: '991.152.443-42',
        rg: '38.315.100-4',
        data_nasc: '04/06/1990',
        sexo: 'Masculino',
      },
      {
        id: 17,
        nome: 'Marlene Emanuelly Yasmin Cavalcanti',
        cpf: '781.233.625-52',
        rg: '21.068.742-3',
        data_nasc: '05/09/1982',
        sexo: 'Feminino',
      },
      {
        id: 18,
        nome: 'Vanessa Isabela da Luz',
        cpf: '872.138.490-85',
        rg: '44.289.193-3',
        data_nasc: '05/09/1982',
        sexo: 'Feminino',
      },
      {
        id: 19,
        nome: 'Camila Flávia Vera Mendes',
        cpf: '790.524.518-77',
        rg: '40.847.994-2',
        data_nasc: '01/07/1952',
        sexo: 'Feminino',
      },
      {
        id: 20,
        nome: 'Luana Lívia Lara Campos',
        cpf: '532.901.056-01',
        rg: '14.732.946-2',
        data_nasc: '19/08/1981',
        sexo: 'Feminino',
      },
      {
        id: 21,
        nome: 'Josefa Nina Galvão',
        cpf: '379.743.833-80',
        rg: '13.878.690-2',
        data_nasc: '27/01/1943',
        sexo: 'Feminino',
      },
      {
        id: 22,
        nome: 'Natália Isabelly Silvana da Paz',
        cpf: '454.172.867-29',
        rg: '22.986.096-5',
        data_nasc: '17/09/1980',
        sexo: 'Feminino',
      },
      {
        id: 23,
        nome: 'Amanda Sebastiana Vieira',
        cpf: '703.941.038-90',
        rg: '50.251.196-5',
        data_nasc: '08/06/1960',
        sexo: 'Feminino',
      },
      {
        id: 24,
        nome: 'Noah Pedro Alves',
        cpf: '074.733.106-58',
        rg: '32.110.015-3',
        data_nasc: '14/08/1992',
        sexo: 'Masculino',
      },
      {
        id: 25,
        nome: 'Giovana Camila da Conceição',
        cpf: '418.746.840-49',
        rg: '35.520.215-3',
        data_nasc: '25/06/1947',
        sexo: 'Feminino',
      },
      {
        id: 26,
        nome: 'Pietro Joaquim Emanuel Gonçalves',
        cpf: '003.502.230-25',
        rg: '12.099.068-4',
        data_nasc: '21/04/1999',
        sexo: 'Masculino',
      },
      {
        id: 27,
        nome: 'Tereza Kamilly Mariana Porto',
        cpf: '050.946.705-90',
        rg: '39.830.941-3',
        data_nasc: '16/06/1959',
        sexo: 'Feminino',
      },
      {
        id: 28,
        nome: 'Caroline Emanuelly Lívia Moreira',
        cpf: '673.831.546-57',
        rg: '24.561.727-9',
        data_nasc: '09/04/1948',
        sexo: 'Feminino',
      },
      {
        id: 29,
        nome: 'Gabriel Ricardo da Paz',
        cpf: '829.270.172-98',
        rg: '30.407.114-6',
        data_nasc: '24/03/1980',
        sexo: 'Masculino',
      },
      {
        id: 30,
        nome: 'Giovanni Igor Augusto Pires',
        cpf: '584.880.490-72',
        rg: '33.874.271-2',
        data_nasc: '24/08/1946',
        sexo: 'Masculino',
      },
    ];

    //função que transforma todos dados pessoais na tabela editáveis.
    let permitirEdicaoDados = () => {
      let dadosCadastrados = document.querySelectorAll('.dPessoa');

      dadosCadastrados.forEach((dado) => {
        dado.setAttribute('contenteditable', true);
      });
    };

    //função que adiciona o evento excluir ao botão lixeira.
    let eventoBotaoExcluir = () => {
      let botaoExcluir = document.querySelectorAll('.material-icons');
      botaoExcluir.forEach((botao) => {
        botao.addEventListener('click', () => {
          let idLinha = botao.getAttribute('data-id');

          document.getElementById(`${idLinha}`).remove();
        });
      });
    };

    //função que inclui os cadastros já existentes na base de dados
    let renderizarCadastros = (banco) => {
      for (let i = 0; i < bancoDeCadastros.length; i++) {
        let novaTabela = `

                              ${banco[i].id}
                              ${banco[i].nome}
                              ${banco[i].cpf}
                              ${banco[i].rg}
                              ${banco[i].data_nasc}
                              ${banco[i].sexo}
                              delete_forever

`;

        tabela.innerHTML += novaTabela;
      }
    };

    let limparCamposFormulario = () => {
      campoId.value = '';
      campoNome.value = '';
      campoCpf.value = '';
      campoRg.value = '';
      campoNascimento.value = '';
      campoSexo.value = '';
    };

    function checkIdAlreadyExist(id) {
      const trs = document.querySelectorAll('tr');
      const idsArray = [];

      trs.forEach((tr) => {
        idsArray.push(tr.getAttribute('id'));
      });

      if (idsArray.includes(id)) {
        return true;
      } else {
        return false;
      }
    }

    //criação de uma nova tabela
    botaoInserirPessoa.addEventListener('click', () => {
      let id = campoId.value;
      let nome = campoNome.value;
      let cpf = campoCpf.value;
      let rg = campoRg.value;
      let nascimento = campoNascimento.value;
      let sexo = campoSexo.value;

      if (
        id == '' ||
        nome == '' ||
        cpf == '' ||
        rg == '' ||
        nascimento == '' ||
        sexo == ''
      ) {
        window.alert('Preencha todos os campos');
      } else if (checkIdAlreadyExist(id)) {
        window.alert('Id já cadastrado');
      } else {
        let novaTabela = `

                            ${id}
                            ${nome}
                            ${cpf}
                            ${rg}
                            ${nascimento}
                            ${sexo}
                            delete_forever

`;

        tabela.innerHTML += novaTabela;

        let novoCadastro = {
          id: id,
          nome: nome,
          cpf: cpf,
          rg: rg,
          data_nasc: nascimento,
          sexo: sexo,
        };

        bancoDeCadastros.push(novoCadastro);

        limparCamposFormulario();
        eventoBotaoExcluir();
        permitirEdicaoDados();
      }
    });

    renderizarCadastros(bancoDeCadastros);
    eventoBotaoExcluir();
    permitirEdicaoDados();
<!DOCTYPE html>

    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Listagem de pessoas</title>

Adicionar colaborador

        <input type="text" name="id" id="id" placeholder="ID" />
        <input type="text" name="nome" id="nome" placeholder="Nome" />
        <input type="text" name="cpf" id="cpf" placeholder="CPF" />
        <input type="text" name="rg" id="rg" placeholder="RG" />
        <input
          type="text"
          name="dataNascimento"
          id="dataNascimento"
          placeholder="Nascimento"
        />
        <input type="text" name="sexo" id="sexo" placeholder="Sexo" />

        <button id="inserirPessoa">OK</button>

        ID
        Nome
        CPF
        RG
        Nascimento
        Sexo

  <link rel="stylesheet" href="/main.css" />
  <link
    href="https://fonts.googleapis.com/icon?family=Material+Icons"
    rel="stylesheet"
  />

Não adicionei o CSS pois não vem ao caso.
Esta é apenas uma sugestão do que podes fazer, agora só adapte para o seu código usando a variável codigo.
Comment: Olá @CmteCardeal, novamente seu código foi de uma enorme ajuda para mim, confesso que esta sua logica criando a função para descobrir a duplicidade da ID está um pouco além da minha capacidade, então vou estudar este seu código. **Agradeço novamente pela ajuda, muito obrigado!!!**
Comment: Nao se preocupe, a medida que voce for estudando e praticando, vai perceber que sua logica vai melhorar e vai conseguir resolver problemas muito  mais facilmente.


Interpreting residual deviance

Question: I have never understood residual deviance, other than the fact that it is a number that's useful for calculating $R^2$ of a linear Gaussian regression model.
Let's say R outputs a residual deviance of 2,000, and a null deviance of 6,000.... So $R^2$ is then $1 - \frac{2000}{6000} = 0.6$, and we can say that "60% of the deviance can be explained with the regression...."
But is there a way to explain, in plain English, what the values "2,000" and "6,000" actually mean?  E.g. "The distance from the mean is, on average, 2,000 units," or something (I know that's not right).
Answer: Deviance residuals are hard to understand and explain.  However, when you are conducting linear (OLS) regressions, the deviance residuals are the same as the basic residuals.  Consider this simple demonstration in R:
<code>set.seed(2016)
x   = rnorm(30)
y   = 3 + .5*x + rnorm(30)
mod = lm(y~x)
</code>
Here is a scatterplot of the regular residuals and the deviance residuals:

It's easy to see that they are the same thing in this case.
So all you need to know is, for example, what the total sum of squares is.  This is actually rather simple.  First, you calculate the mean of all of your response ($Y$) data.  Then you get the difference between every data point and the mean.  Lastly, you square those differences and add them up.  The sum of squares for your regression is the sum of the squared distances of each predicted response value from the mean of your observed responses, and the sum of squared errors is the sum of the squared distances of each observed response from the predicted response.  Here are the formulas:
\begin{align}
SSTO &= \Sigma_i (y_i - \bar y)^2  \\
SSR  &= \Sigma_i (\hat y_i - \bar y)^2  \\
SSE  &= \Sigma_i (y_i - \hat y_i)^2
\end{align}
In this answer, You can see a picture of $SSTO$ (there called $SSY$) and $SSE$.
Comment: How about for GLM and/or logit?
Answer: I presume you want to say residual variance instead of residual deviance.
For regression analysis, you can have as many predictor variables as you like, at least you can have $X,X^2,X^3,...,X^n$.
For each coefficient, there is a sacrification of 1 degree of freedom.
Suppose you have $n$ observations, and $p-1$ predictor variables, you've got $n-p$ degree of freedom for your regression model. And you have $n-1$ degree of freedom for the null variance, since you have to sacrifice 1 degree of freedom for the mean.
Coefficient of determination is $R^2=1-\frac{SSE}{SSTO}$
The adjusted coefficient of determination $R^2_{adj}=1-\frac{SSE/n-p}{SSTO/n-1}$
The coefficient of determination $R^2$ measures how well your regression fits the observation.
If there is a perfect matching, you have $SSE=0$.
For the worst case of null variance, you have $SSE=SSTO=(n-1)\sigma^2=S^2$.
The purpose of your regression model is to reduce the variance of estimation.
$SSR=SSTO-SSE$ measures the reduction of the variance of estimation from the null variance $SSTO$ to the current variance $SSE$.
Thus $R^2=\frac{SSR}{SSTO}=1-\frac{SSE}{SSTO}$ measures the proportion of variance reduction, and it measures how well your regression estimation fits the observation.
You can also use adjusted coefficient of determination $R_{adj}^2$ to include the effect of sacrification of degree of freedom.
Comment: *SSE* is measured in the same units as your dependent variable (DV), but squared. Its scale depends on the scale of DV, so you should treat it as big or small only comparing with the square of your DV values.
Comment: Thank you for your reply... but if SSE = 2,000, what does that mean?  2,000 what?  Are there units?  Is 2,000 a big number or a low number?  Where deos it come from?
Comment: $SSE=\sum (Y-\hat Y)^2$. $SSE$ measures the sum of error squares. $MSE=SSE/n-p=S^2_{\epsilon}=\hat \sigma^2_{\epsilon}$. It is the variance of the error term of your regression model. Since by assumption, the observation has the same constant variance as the error term $\epsilon$, we can say $SSE$ measures the variance of the observation from the estimation (expectation of the observation) under current regression model.

Interpreting residual deviance


How to solve $-3\int^{-5/6}_{11/6}u\delta (u)du$

Question: I working through the question in the link and i am stuck on something:
How to solve integration with Dirac Delta function?
How does one solve the equation:  $\int^{-5/6}_{11/6}(-\frac{1}{2}-3u)\delta (u)du$
My solution:
$\int^{-5/6}_{11/6}(-\frac{1}{2}-3u)\delta (u)du =$
$\int^{-5/6}_{11/6}(-\frac{1}{2})\delta (u)du-\int^{-5/6}_{11/6}(-3u)\delta (u)du=$
$-\frac{1}{2}\int^{-5/6}_{11/6}\delta (u)du-3\int^{-5/6}_{11/6}u\delta (u)du=$
$-1/2-3\int^{-5/6}_{11/6}u\delta (u)du $
I dont't know how to solve this equation:
$-3\int^{-5/6}_{11/6}u\delta (u)du$
Edit:
Would anyone mind explaining why: $\int^{-5/6}_{11/6}u\delta (u)du=0$. Or in wider sence why $\int^{\infty}_{-\infty}x\delta (x)dx=0$
Answer: The delta function has the property that $$\int_{-\infty}^{\infty}f(u)\delta(u-a)du=f(a)$$
or $$\int_{a-\epsilon}^{a+\epsilon}f(u)\delta(u-a)du=f(a)$$
for $\epsilon>0$ and $\delta(u-a)=0$ for $u\neq a$. In your case $f(u)=-\frac{1}{2}-3u$ and $a=0$ (which lies in the interval $[\frac{-5}{6},\frac{11}{5}]$), thus we have $$\int_{\frac{11}{6}}^{-\frac{5}{6}}f(u)\delta(u)du=-\int_{-\frac{5}{6}}^{\frac{11}{5}}f(u)\delta(u)du$$
$$=-f(0)=-(-\frac{1}{2})=\frac{1}{2}.$$
Answer: By definition (of the Dirac delta function),
$$\delta(u) = 0 \:\: \text{for}\:\: u\ne 0\implies -\int_{11/6}^{5/6}\left(3u+\frac{1}{2}\right)\delta(u) \:du=0$$
since the range of integration does not contain $0$.

EDIT $1$
I just took a look at the link in the post and realized that there could be a bunch of typos in the question. Henceforth, I shall assume that this is what the OP actually wants:
$$\int_{-5/6}^{11/6}\left(\frac{1}{2}-3u\right)\delta(u)\:du$$
We have that
$$\boxed{\:\int_c^d f(t)\delta(t-a)\:dt=f(a),\:\:\:\:\:\text{ if $a\in(c,d)$}\:\:}\tag{1}$$
Observe that $0$ is contained in the range of integration. Therefore,
$$\int_{-5/6}^{11/6}\left(\frac{1}{2}-3u\right)\delta(u)\:du=\frac{1}{2}-3(0)=\frac{1}{2}$$

EDIT $2$
Observe that
$$\int_{11/6}^{-5/6}u\delta(u)\:du=\int_{-5/6}^{11/6}u\delta(u)\:du $$
After an application of $(1)$, we arrive at the desired answer.
Comment: Hi, sorry the the integral was supposed to -5/6 not 5/6
Comment: Would you mind explaining why: $\int^{-5/6}_{11/6}u\delta (u)du=0$. Or in wider sence why $\int^{\infty}_{-\infty}x\delta (x)dx=0$
Comment: I have edited my answer. Please take a look now.


RAD Studio XE6 não conecta a nenhum banco de dados!

Question: Programa utilizado: RAD Studio XE6 (32bits).
Problema: Ao criar um novo projeto Desktop ou Mobile, ao tentar configurar a conexão a qualquer banco de dados, seja por FireDAC ou dbExpress, ocorre o seguinte erro:
"DBX Error:  Driver could not be properly initialized. Client library may be missing, not installed properly, of the wrong version, or the driver may be missing from the system path."
Qual seria a solução para este problema?
Comment: já tentou buscar pelas dlls de acesso ao banco na pasta de instalação? pode ser que o caminho para elas não está no PATH nem no diretorio do executável
Comment: onde vejo os diretórios no xe6?
Comment: elas vão estar no Lib ou no Bin da sua instalação do delphi. Daí elas devem está na variável de sistema PATH do seu windows ou ser copiada para dentro da pasta bin do seu projeto
Answer: Na verdade você precisa ir na guia de componentes "FireDAC Links", e colocar o componente no seu projeto, correspondente ao banco que você está tentando usar.
Por exemplo, eu uso o PostgreSQL e coloquei ao lado do componente de conexão o componente TFDPhysPgDriverLink da guia de componentes "FireDAC Links", simples assim.
Answer: Eu tive um problema parecido ao tentar conectar com uma base de dados do Postgre utilizando FireDAC no Delphi XE6.
A solução foi voltar a versão da DLL "libpq.dll" para 188.8.131.527.
No Firebird conectada normalmente.


Can not get the timepicker value using Angular.js and Jquery

Question: I need one help. I am implementing jquery.timepicker.js to get the time interval in my Angular application but i am not getting any value like that.I am explaining my code below.

      From Time :
      <input type="text" name="time" id="time" class="form-control oditek-form" placeholder="E.G-9.00AM" ng-model="time" ng-change="clearField('time');" maxlength="30" time-picker="" />

      To Time :
      <input type="text" name="time1" id="time1" class="form-control oditek-form" placeholder="E.G-9.00AM" ng-model="time1" ng-change="clearField('time1');" maxlength="30" time-picker="" />

    <input type="button" class="btn btn-success" ng-click="addTimeDetails();" id="saveData" value="Add" style="margin-right:20px;" />

my controller side code is given below.
var app=angular.module('spesh',[]);
app.controller('formController',function($scope,$http){
  $scope.addTimeDetails=function(){
    console.log('time',$scope.time,$scope.time1);
  }
})
app.directive('timePicker', [function () {
        return {
        restrict: 'A',
        require: 'ngModel',
        scope: {
            model: '=ngModel'
        },
        link: function ($scope, element, attrs, ngModelCtrl) {
            element.timepicker();
            element.on('changeTime', function () {
              $scope.$apply(function () {
                $scope.model = element.val();
              });
            });

        }
    }
}]);

Here i need when user will click on text field the time drop down will come and click on add button the selected time will show in console.MY complete code is here in plunkr.Please help me.
Answer: Change  to
Here is better solution: https://plnkr.co/edit/A0sSqXzx73XYn0f7UVGD?p=preview
When you require ngModel, you can use it's methods or read value. You don't need to read ngModel attribute.

$setViewValue(value, trigger); Update the view value.
This method should be called when a control wants to change the view
  value; typically, this is done from within a DOM event handler. For
  example, the input directive calls it when the value of the input
  changes and select calls it when an option is selected.
Comment: Well, mark it as answer than or delete a question. :)


Leitura de dados sobre arquivo .csv

Question: Possuo um sistema em PHP realizando a leitura de dados de um arquivo csv. Ele apresenta o número do código de barras onde irei relacionar ao banco de dados para atualização de acordo com o arquivo enviado no sistema. Exemplo:
Usuário envia arquivo CSV com dados: 'Barras' => 7896603803918, 'qtd' => '1'
Ao fazer a leitura do arquivo recebo o valor de barras: "7,8966E+12" o qual não é o mesmo apresentado no valor correto.
Ao abrir o arquivo .csv no excel, encontro realmente o texto sendo exibido como "7,8966E+12", porem quando clico para verificar o valor da função, recebo o número correto de 7896603803918.
Meu problema se resume a: O arquivo .csv me envia um número exponencial que não representa o número real, impossibilitando a importação. Qual seria a solução correta para que eu pudesse retirar esses valores de forma correta do arquivo .csv ou se é um problema que todos enfrentam.
Código sendo utilizado no momento:
<code><?php
$csv = str_getcsv(file_get_contents('data.csv', 'r'), ';');
var_dump($csv);
</code>
Comment: O problema é que Excel ou qualquer outro programa que está utilizando para gerar a planilha está reconhecendo o código de barras como número e fazendo a formatação, gerando, assim, o csv de fóruns errada. Você precisa configurar no editor a coluna como texto
Answer: Provavelmente você está tentando passar barras para int, sendo que o maior valor para int32 é 2^32-32, tente passar o valor para string ou então tente usar uma versão 64 bits do php, que aceita int64

Leitura de dados sobre arquivo .csv


Como limitar o maior valor numérico a ser digitado no C#?

Question: Queria ter um número máximo no comando:
salario = Convert.ToDouble (Console.ReadLine());
Exemplo: se passar de 4000 será inválido.
Comment: Não faça como a resposta abaixo, essa forma é errada, mesmo que pareça que funcione. Um exemplo  melhor: https://pt.stackoverflow.com/a/471939/101. Na verdade perguntas assim (que não são enunciados que a pessoa só pede para fazer o código) tem em dezenas aqui no site, seria só pesquisar, exemplo: https://pt.stackoverflow.com/q/336749/101. Note que aqui resolve outros problemas. Outra forma: https://pt.stackoverflow.com/q/348157/101
Answer: Existem várias formas de resolver esse problema. No caso, como você está solicitando um input do usuário, uma opção seria colocar uma condição dentro de um loop, impedindo o loop de terminar enquanto o usuário não coloca um valor dentro do range que você precisa. Por exemplo:
<code>Console.WriteLine ("entre o valor:");
       double salario = -1;
       do {
           salario = Convert.ToDouble (Console.ReadLine());
           if (salario > 4000) {
               Console.WriteLine("salário deve ser menor que 4000...");
               salario = -1;
           }
       } while (salario == -1);

       Console.WriteLine("salario={0}",salario);
</code>
Comment: Se o usuário digitar ao letra ao invés de um número, o que acontece?


Cocoa Static Library - Some Third Party Source Files Are Not Working Properly

Question: First of all I want to clarify that I may have not address the problem clearly as stated in the title of my problem. Please forgive because I really don't know how to express this.
I am building a Cocoa Static Library that includes Cocoa Http Server in it. The library was built successfully and when I use it in my project, I was also able to use the methods of the Cocoa Http Server. My app runs an http server that plays music on wifi speakers. Once the music starts playing, which means the speaker starts reading data from my server, this error is occurring.

The strange thing is, if I add the CocoaHttpServer to my App Project. This will never occur. The problem will be solved.
But this really confuses me. The lib contains exactly the same CocoaHttpServer, but still "unrecognized selector occurs". What is the difference between using a 3rd party lib in my library and using a 3rd party lib in my App Project?
I hope I explained my situation well. To summarize:
Problem occurs when:

I add the CocoaHttpServer to my library project, build it, and then use it directly for my app.

Problem solved when:

I still add the CocoaHttpServer to my library project, build it, BUT at the same time I also add the CocoaHttpServer to my App Project. It looks like I am importing the CocoaHttpServer two times.

Please help. Many Thanks!
Answer: You are using a categories in your static lib.
Read this:
Building Objective-C static libraries with categories


Share screen size is too big

Question: I want to share an image. I created a share intent. For some reason the share screen appears too big. It appears big on both Galaxy s4 and Nexus 5, but appears good on Genymotion. How can I fix it?
The code:
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("image/jpeg");
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///" +
    image.getAbsolutePath()));
startActivity(Intent.createChooser(sharingIntent, MainMenu.this
    .getResources().getString(R.string.shareImage)));

The screen:
Comment: What are you testing this on? I've seen some image discrepancies in notification bar icons when using emulators, but never an activity like this. Check your emulator DPI, maybe, or use a default one...
Comment: In my emulator its fine, in my Galaxy s4 or Nexus 5 its too big (the photo is a screenshot from a real Galaxy s4).
Answer: For an unknown reason, what made the screen look big is -
Intent.createChooser(sharingIntent, MainMenu.this.getResources().getString(R.string.shareImage)).
Just doing startActivity(sharingIntent); fixed my problem.

Share screen size is too big


How do I integrate $\int f(x) dx = \int \frac{e^{g(x)}}{g'(x)}\, dx$?

Question: I am developing a monetary economics model with an exponential growth term embedded within an exponential function. When I solve the equation, I wind up either with everything cancelling out, or with $\int f(x) dx = \int \frac{e^{g(x)}}{g'(x)} dx$.
Life would be so much easier, if it were $\displaystyle\int e^{g(x)}g'(x)\, dx$, but, sadly, $g'(x)$ winds up in the denominator.
Any suggestions, even just hints, would be greatly appreciated!
Comment: I would recommend that you type up the larger problem. You might be simplifying down to this integral incorrectly, or maybe there's a hidden assumption you're making.
Comment: This is hard to parse.  Do you mean $\int \frac {e^{g(x)}}{g'(x)} \,dx$?
Comment: Assuming I am reading it correctly (far from clear), I expect that you will need to phrase the answer in terms of the [Exponential Integral](https://en.wikipedia.org/wiki/Exponential_integral) or such.
Comment: The equation that I am dealing with looks like the one in my subject line and in Lulu's comment 1 above.

The starting point is:

$\int e^{-r{e^{{\rho}t}}t} dt$
Comment: Based on your starting point at least, it doesn't look like a nice closed form is going to exist: https://www.wolframalpha.com/input/?i=integral+of+e%5E(-te%5Et)+dt
Comment: Brenton, thank you. I tried Wolfram Alpha and got the same result. However, I did not know if that was definitive. For example, I have tested different equations of the form $\int e^u u'_t dt$, where $\mathbf u$ is a relatively complex formula, and Wolfram Alpha has choked on that.
Answer: If the starting problem is
$$I=\int e^{-r\, e^{\,\rho \, t}} \,dt$$ let
$$r\, e^{\,\rho \, t}=x \implies t=\frac{1}{\rho}{\log \left(\frac{x}{r}\right)}\implies dt=\frac{dx}{\rho  x}$$
$$I=\frac{1}{\rho}\int \frac{e^{-x}}{ x}\,dx=\frac{1}{\rho}\text{Ei}(-x)$$ where appears the exponential integral function already mentioned by lulu. Back to $t$
$$I=\frac{1}{\rho}\text{Ei}\left(-r\, e^{\,\rho\,  t}\right)$$
However, I do not see the connection with the problem in title.
Comment: Thank you for this, however my starting point is:

$\int e^{-r{e^{\rho t}}t} dt$
Comment: @Pecuniology. Sorry but I still do not see what you need. As asked in comments, could you provide the real problem from the start ?


Problemas com Spring Security

Question: Olá, pessoal. Estou criando um projeto utilizando Java EE e como IDE estou usando o Eclipse.
Neste projeto, estou usando o Spring Security para autenticar o procedimento do login.
Só que acontece o seguinte: eu criei uma página xhtml para cadastrar novos usuários.
Obviamente, quem acessa esta página não possui cadastro, pois ela está entrando nesta página
exatamente para criar um novo cadastro.
Nesta parte, o Spring Security serve para que os usuários não acessem as páginas internas do sistema
diretamente. Quando alguém tenta acessar alguma página interna sem passar pela tela de login, o Spring automaticamente redireciona o usuário para a página default. Mas eu quero retirar esta restrição SOMENTE PARA A PÁGINA DE CADASTRO DE USUÁRIOS para que qualquer usuário consiga acessar a página de cadastro mesmo sem realizar o login.
Alguém conheceria algum método para realizar este procedimento?
Estou postando o conteúdo do arquivo applicationcontext.xml do meu projeto. Acredito que é neste arquivo que é necessário realizar alguma alteração.
<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/security
        http://www.springframework.org/schema/security/spring-security-3.1.xsd">

    <beans:bean id="appUserDetailsService"
        class="com.sisRastrbov.security.AppUserDetailsService" />

    <http pattern="/Login.xhtml" security="none" />
    <http pattern="/Erro.xhtml" security="none" />
    <http pattern="/Main.xhtml" security="none" />
    <http pattern="/javax.faces.resource/**" security="none" />

    <http auto-config="false" use-expressions="true">
        <intercept-url pattern="/gado/**" access="hasAnyRole('ADMINISTRADORES')" />
        <intercept-url pattern="/usuario/**" access="hasAnyRole('ADMINISTRADORES','FUNCIONARIOS')" />
        <intercept-url pattern="/tag/**" access="hasAnyRole('ADMINISTRADORES','FUNCIONARIOS')" />
        <intercept-url pattern="/propriedade/**" access="hasAnyRole('ADMINISTRADORES','FUNCIONARIOS')" />
        <intercept-url pattern="/MinhasProp.xhtml" access="hasAnyRole('ADMINISTRADORES','FUNCIONARIOS')" />
        <intercept-url pattern="/area/**" access="hasAnyRole('ADMINISTRADORES','FUNCIONARIOS')" />
        <intercept-url pattern="/Home.xhtml" access="isAuthenticated()" />
        <intercept-url pattern="/Main.xhtml" access="isAuthenticated()" />

        <intercept-url pattern="/**" access="denyAll" />

        <form-login login-page="/Main.xhtml" default-target-url="/Home.xhtml" always-use-default-target="true" authentication-failure-url="/Main.xhtml?invalid=true"/>
        <logout logout-url="/j_spring_security_logout" invalidate-session="true"/>
    </http>

    <authentication-manager>
        <authentication-provider user-service-ref="appUserDetailsService">

        </authentication-provider>
    </authentication-manager>

</beans:beans>

Agradeço a todos que postarem qualquer resposta ou sugestão.
Answer: Para permitir que qualquer usuário acesse uma página específica você deve adicionar a seguinte linha:
<intercept-url pattern="/cadastro" access="permitAll"/>

Mas tem que adicionar depois de:
<intercept-url pattern="/**" access="denyAll" />

Problemas com Spring Security