Testing statistical software

Question: What techniques/approaches are useful in testing statistical software? I'm particularly interested in programs that do parametric estimation using maximum likelihood.
Comparing results to those from other programs or published sources is not always possible since most of the time when I write a program of my own it is because the computation I need is not already implemented in an existing system.
I am not insisting on methods which can guarantee correctness. I would be happy with techniques that can catch some fraction of errors.
Answer: One useful technique is monte carlo testing.  If there are two algorithms that do the same thing, implement both, feed them random data, and check that (to within a small tolerance for numerical fuzz) they produce the same answer.  I've done this several times before:

I wrote an efficient but hard to implement $O(N\ log\ N)$ implementation of Kendall's Tau B.  To test it I wrote a dead-simple 50-line implementation that ran in $O(N^2)$.
I wrote some code to do ridge regression.  The best algorithm for doing this depends on whether you're in the $n > p$ or $p > n$ case, so I needed two algorithms anyhow.

In both of these cases I was implementing relatively well-known techniques in the D programming language (for which no implementation existed), so I also checked a few results against R.  Nonetheless, the monte carlo testing caught bugs I never would have caught otherwise.
Another good test is asserts.  You may not know exactly what the correct results of your computation should be, but that doesn't mean that you can't perform sanity checks at various stages of the computation.  In practice if you have a lot of these in your code and they all pass, then the code is usually right.
Edit:  A third method is to feed the algorithm data (synthetic or real) where you know at least approximately what the right answer is, even if you don't know exactly, and see by inspection if the answer is reasonable.  For example, you may not know exactly what the estimates of your parameters are, but you may know which ones are supposed to be "big" and which ones are supposed to be "small".
Answer: Not sure if this is really an answer to your question, but it is at least tangentially related.
I maintain the Statistics package in Maple. An interesting example of difficult to test code is random sample generation according to different distributions; it is easy to test that no errors are generated, but it is trickier to determine whether the samples that are generated conform to the requested distribution "well enough". Since Maple has both symbolic and numerical features, you can use some of the symbolic features to test the (purely numerical) sample generation:

We have implemented a few types of statistical hypothesis testing, one of which is the chi square suitable model test - a chi square test of the numbers of samples in bins determined from the inverse CDF of the given probability distribution. So for example, to test Cauchy distribution sample generation, I run something like
with(Statistics):
infolevel[Statistics] := 1:
distribution := CauchyDistribution(2, 3):
sample := Sample(distribution, 10^6):
ChiSquareSuitableModelTest(sample, distribution, 'bins' = 100, 'level' = 0.001);

Because I can generate as large a sample as I like, I can make $\alpha$ pretty small.
For distributions with finite moments, I compute on the one hand a number of sample moments, and  on the other hand, I symbolically compute the corresponding distribution moments and their standard error. So for e.g. the beta distribution:
with(Statistics):
distribution := BetaDistribution(2, 3):
distributionMoments := Moment~(distribution, [seq(1 .. 10)]);
standardErrors := StandardError[10^6]~(Moment, distribution, [seq(1..10)]);
evalf(distributionMoments /~ standardErrors);

This shows a decreasing list of numbers, the last of which is 255.1085766. So for even the 10th moment, the value of the moment is more than 250 times the value of the standard error of the sample moment for a sample of size $10^6$. This means I can implement a test that runs more or less as follows:
with(Statistics):
sample := Sample(BetaDistribution(2, 3), 10^6):
sampleMoments := map2(Moment, sample, [seq(1 .. 10)]);
distributionMoments := [2/5, 1/5, 4/35, 1/14, 1/21, 1/30, 4/165, 1/55, 2/143, 1/91];
standardErrors :=
  [1/5000, 1/70000*154^(1/2), 1/210000*894^(1/2), 1/770000*7755^(1/2),
   1/54600*26^(1/2), 1/210000*266^(1/2), 7/5610000*2771^(1/2),
   1/1567500*7809^(1/2), 3/5005000*6685^(1/2), 1/9209200*157366^(1/2)];
deviations := abs~(sampleMoments - distributionMoments) /~ standardErrors;

The numbers in distributionMoments and standardErrors come from the first run above. Now if the sample generation is correct, the numbers in deviations should be relatively small. I assume they are approximately normally distributed (which they aren't really, but it comes close enough - recall these are scaled versions of sample moments, not the samples themselves) and thus I can, for example, flag a case where a deviation is greater than 4 - corresponding to a sample moment that deviates more than four times the standard error from the distribution moment. This is very unlikely to occur at random if the sample generation is good. On the other hand, if the first 10 sample moments match the distribution moments to within less than half a percent, we have a fairly good approximation of the distribution.

The key to why both of these methods work is that the sample generation code and the symbolic code are almost completely disjoint. If there would be overlap between the two, then an error in that overlap could manifest itself both in the sample generation and in its verification, and thus not be caught.
Comment: Thanks for your answer. I am "accepting" the other answer as I'm allowed to pick only one and that seemed to fit my current situation slightly better. But your answer was very helpful too.
Answer: Bruce McCullough had a bit of a cottage industry in assessing statistical software (in the widest sense; he also tested Microsoft Excel. And found it wanting). Two papers that illustrate part of his approach are here and here.
Answer: A lot of detail is given by the President of StataCorp, William Gould, in this Stata Journal article.1 It is a very interesting article about quality control of statistical software.

Testing statistical software


Want to process node edit in two different ways

Question: On every node page there is an edit and submit, you can set when the edit is available and such.
So, what I want to do is this.
When a regular user uses the edit button(which I want to be available for all site users), I want it to open the node and allow someone to directly edit it.  Just like when the owner edits it, it will open everything and allow the user to make changes.  But instead of actually saving the changes to that node, I want it to create a new node that copies the old one and saves this other user's changes.
When a moderator or the creator of the node edits, I want it to edit the node directly and saves the changes to that specific node.
I need to either create two buttons that show up differently on page for certain users, or use the same button but use some form validation technique to check the permissions of who is submitting the form and do the above accordingly.  Are there any ideas?  Any help is greatly appreciated!! Thanks
Answer: You can use revisioning module
Revisioning is a module for the configuration of workflows to create, moderate and publish content revisions.
Using this module when you edit a node, a new revision is created and it goes to modaration mode.
For more info check the module page
Comment: I feel this would be more simple and allow more customization for me if I do this with my own code.  Any ideas on how to do it without another module?


logistic regression - unbalanced classes

Question: Let's suppose I have a dataset with classes A and B, with class A occurring in 1% of cases and class B occurring in 99% of cases. Perhaps class A is a loan default.
Suppose I want to "understand" what factors make one class A vs B by fitting a logistic regression to dependent variables X and then looking at model coefficients. Does it make sense to put a higher class_weight on class A, such as putting a weight of 99 on class A and 1 on class B? Or does the intercept already take care of this?
What if the logistic regression has a regularization parameter, would class imbalance matter more in this scenario? (because the model would be more inclined towards a constant "Predict B" model to reduce the penalty on coefficient size).
I've seen many economics papers in which a unregularized logistic regression is run on data and then the authors interpret coefficient sizes and significance, just wondering how valid this is.
Answer: I assume by "factors" you mean which features or variables are "most" important for distinguishing between the 2 classes? I think that you first need to establish a performance metric to check if the results make sense. E.g., on such a dataset, just always predicting the majority class will already give you a classification accuracy of 99% (or error of 1%). Depending on the size of the dataset, you may want to look at ROC auc or precision-recall aucs (maybe F1) to get an idea how well your model discriminates between the classes.
In addition (also depending on the size of your dataset) you may want to cross-validate, e.g., 0.632+ bootstrapping, 10-fold CV, etc to get an idea about the stability of your model. If you have a sufficiently large dataset, regularization (L1 or L2) may give you a good idea of how important certain features are, answering your question "Suppose I want to "understand" what factors make one class A vs B " -- but this only makes sense if a linear model is appropriate given the data, and if there's sufficient stability of your model in CV I'd say.
Comment: Thanks Sebastian. I've read many papers in which the implicit assumption is that the underlying data was generated from a linear model, y = Xb + e, and then inference is done by looking at the coefficient sizes and standard errors. But this doesn't quite make sense to me since why should we expect for the true model to be linear? Doesn't it make more sense to try to find a model with a high AUC and look at the coefficients of that model?
Comment: "why should we expect for the true model to be linear?" That's the assumption of logistic regression since it is a generalized linear model. Let's say your data is not linear, then your model will perform poorly but you still have the linear relationship between inputs and weights z = w_0 + x_1*w_1 + ... +  x_n*w_n. That's not going to change, but if your model doesn't capture the structure of the data, the weight coefficient's magnitudes may be meaningless for an importance interpretation -- they still tell you about which features are important for the classifier to make the decision though.
Comment: " Doesn't it make more sense to try to find a model with a high AUC and look at the coefficients of that model?" A (standard) logistic regression model is always linear, so if you have a non-linear problem, it maximizing accuracy or AUC via model selection is not going to help. You could try algorithms for non-linear hypothesis spaces, but then you can't simply read the weight coefficients (if its parametric) as importances.
Comment: but very few models in reality would be perfectly linear, so I was thinking of interpreting the model more in the sense of being a good linear approximation to the true conditional expectation E[Y|X]
Comment: I absolutely agree with you. What I was trying to explain was that it is not about the "goodness" of the linear approximation but is something like "the feature coefficients tell you how important each feature is for the logistic regression output" rather than "the coefficients tell you if a feature is generally a good predictor or not, whether you assume a linear model or not". Or in other words the coefficients tell you how important the features are under a linear assumption.
Comment: But don't the parameters of a model with very good goodness of fit become more meaningful? Similar to how we "explain" things using classical mechanics even though those equations are only approximations to the truth. For example, let's say the true conditional expectation function is f(X) = E[Y|X] and our fitted linear function is g(X). If some dependent variable has a large influence on g(x), can't we say it has a large influence on f(X) if g is very "close" to f (according to our desired metric)?
Comment: "parameters of a model with very good goodness of fit become more meaningful?" Yeah, they are more meaningful then under the assumption of a linear relationship. A "large" feature weight has always a "large" influence on "g(x)"  -- given that the features are standardized/normalized of course. And if a linear model is reasonable for the given dataset, we can then also conclude that it has a large influence on f(x) if this was what you were asking.
Comment: How do you tell if a linear model is "reasonable"? If a linear model has an extremely high goodness of fit, doesn't that imply that a linear model is reasonable? (is there an example in which a linear model has very high goodness of fit but a linear model is not reasonable?) Of course there could be a model such as y = x + e where var(e) >> var(x) and e is gaussian noise, in which I guess checking that E[e|x] = 0 would be more appropriate
Comment: "is there an example in which a linear model has very high goodness of fit but a linear model is not reasonable?" -> I think this is not a large problem with linear models, but I would say that good generalization performance would be an indicator. E.g., you may determine a good fit when you'd do resubstitution evaluation, but the model doesn't perform well on new data due to overfitting. The coefficients tell you then which coefficients are important for yielding the classifier output, but it wouldn't tell you that much about which features are really important (on a population level)
Comment: Right but let's say we have a linear model that has a very high goodness of fit out of sample. Given only this fact, would you say that the linear model is "reasonable?" In which case the coefficients would tell us something about importance of different variables on a population level.


Detect System Service Tag using JS

Question: Here i am trying to get system service tag/BIOS id using JS just Like Dell do.
Is there any way to call installed application from js.?
Any help is appreciated.
Thank you.
Answer: That would not be possible via javascript, within browser context. So, no,.there is not a way. Back at the drawing board, as they say - LOL
Comment: they dont. that installed app reads that value from the operating system, then thwy have some mechanism of passing that value to the webpage running within the browser. That mechanism is likely a remote webservice, which receives the value sent by the app, and then makes it available.to the browser script when it asks. You can see which network calls the browser is making via dev tools
Comment: or maybe their installed app launched an http listener at some port, and so you might finf http:// localhost:someport in that trace (F12 > network.tab). Inspect what the browser is doing, maybe you can mimick. But even if you're successful, it will only work on machines with that software installed and running. What do you need that value for anyway?
Comment: Then how the dell driver website can automatically detect my system Info.
Comment: They likely use software external to the browser, which the browser communicates with via a median remote website, or via a browser extension (if those even have elevated permissions). Inspect your machine, and you will likely find some dell software installed. Such software has elevated permissions, while browser doesn't. That's how.
Comment: Yes dell-support application is there , i am trying to know how they access that application from js via browser.

Detect System Service Tag using JS


Two equivalent definitions of weak equivalences of simplicial sets

Question: In and André Joyal and Myles Tierney their Notes on Simplicial Homotopy, two different definitions of a weak equivalence are given, which they say are equivalent (p. 59). I, however, only see why the one implies the other and would like some help in showing the converse direction. The definitions are:

Definition 1. A morphism $f : X \rightarrow Y$ is a weak equivalence if for every Kan complex $K$, the induced map
  $$
[f, K] : [Y, K] \rightarrow [X, K]
$$
  of morphisms up-to-homotopy is a bijection.

and

Definition 2. A morphism $f : X \rightarrow Y$ is a weak equivalence if for every square of the form
  $\require{AMScd}$
  \begin{CD}
    X @>x>> \overline X\\
    @V f V V @VV \overline f V\\
    Y @>>y> Y
\end{CD}
  where $x, y$ are anodyne, $\overline X, \overline Y$ are Kan, the induced diagonal filler $\overline f$ is a homotopy equivalence.

To see that Definition 1 implies Definition 2, suppose that $f$ is a Definition-1-weak-equivalence and we are given a square as above. Then by 3-for-2 of Definition 1 (which I know how to show), we have that $yf$ is a weak equivalence, and thus also that $\overline f x$ is. By the same property, it follows that $\overline f$ is a weak equivalence. Now $\overline f$ is a weak equivalence between Kan complexes, which using Definition 1, can easily be shown to be a homotopy equivalence.
The only idea that I have for the converse direction is that we could try to show that $K^f : K^Y \rightarrow K^X$ is a homotopy equivalence, from which Definition 1 directly follows. However, I wouldn't know how to build anodyne maps from $X \rightarrow K^Y$ and from $Y \rightarrow K^X$.
Can anyone help me show that Definition 2 implies Definition 1?
Note: I also don't know how to show 3-for-2 for Definition 2. In particular, I'm not able to prove that $gf$ is a w.e. whenever $f$ and $g$ are. So, if you use that property in your answer I would appreciate it if you also explain that step.
Answer: This is a bit tough because it depends on the context and I'm not deeply familiar with these notes. Here is a suggestion: consider a Kan complex $K$ and now consider the map $K^Y \to K^X$. Build a square $\require{AMScd}$
\begin{CD}
    X @>x>> \overline X\\
    @V f V V @VV \overline f V\\
    Y @>>y> \overline Y
\end{CD}
where $X\to \overline X$ is anodyne and $\overline X$ is Kan, this exists by Theorem 3.1.1, similarly for $Y$. It follows from Definition 2 that $\overline f$ is a homotopy equivalence. Now, we obtain a dual diagram:
\begin{CD}
    K^X @<K^x<< K^\overline X\\
    @AK^fAA @AA K^\overline f A\\
    K^Y @<<K^y< K^\overline Y
\end{CD}
It follows from Theorem 3.2.1 that $K^x$ and $K^y$ are trivial fibrations between Kan complexes and by Prop 3.2.5, they are homotopy equivalences. Since $\overline f$ is a homotopy equivalence, it follows that $K^\overline f$ is a homotopy equivalence (try to prove this, it is stated but I think not proved in the notes). Thus, $K^f$ is a homotopy equivalence between Kan complexes and hence we get Definition 1.
Comment: Yes, this works. Thanks! Even though you're not familiar with the notes, you refer to the relevant theorems, which makes your answer very helpful. I guess the crucial argument that I did not think of was to take the exponent of the whole diagram.
Comment: I was worried about creating a circular argument since the full model structure hadn't been established yet, and the equivalence is clear once you have the model structure.


Re-use value entered in component in previous webform page on following page in markup

Question: I'm creating a webform with multiple pages.  On the first page, I ask for "Name" (textfield), and on the second page, I would like to have a markup component that incorporates the value entered for "Name" on the first page.
I have tried using webform's token system in Webform 3.x and the new token functionality in Webform 4.x, as well as hook_form_alter(), all to no avail.
In regard to the later, in glue.module I defined:
function glue_form_alter(&$form, &$form_state, $form_id) {
  if (preg_match("/webform/i", $form_id)) {
   dsm($form);
  }
}

and the resulting array displays the "name" component value (from page one) as empty on page two.
Would someone please help me?
Answer: For one I would be wary of matching $form_id like that.
It means you will be altering any webform you might add in future.
Plus it is possible for other form id's to contain webform, for example webform_node_form, which is the node add/edit form for the webform content type.
There is also webform_components_form, webform_component_edit_form and probably lots of others.
If you really want to regex it I would use webform_client_form_\d+
However if I wanted to do all webforms I would use hook_form_BASE_FORM_ID_alter(), like this:

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function hook_form_webform_client_form_alter(&$form, &$form_state, $form_id) {

}

and to do just one webform I would use hook_form_FORM_ID_alter(), like this:
/**
 * Implements hook_form_FORM_ID_alter().
 */
function hook_form_webform_client_form_922_alter(&$form, &$form_state) {

}

Now for your actual question, you can accomplish this in form alter:
/**
 * Implements hook_form_FORM_ID_alter().
 */
function hook_form_webform_client_form_922_alter(&$form, &$form_state) {
  // If we are on the second page.
  if ($form['details']['page_num']['#value'] == 2) {
    // You can get the value via the submitted_tree using the field key.
    $name = $form_state['values']['submitted_tree']['name_field_key'];
    // However I would recommend instead using submitted.
    // The reason is that with fieldsets the submitted_tree gets more complex,
    // and also, users can change the field key (who knows why that's allowed)
    // so that is unreliable. The submitted array uses the webform field cid
    // so it won't change.
    // (1 is the id of my name field)
    $name = $form_state['values']['submitted'][1];

    // There are 2 main ways you can add the markup.
    // 1. If you actually have a webform markup component on the second page
    // and you want to set its value. This means you can control the field via
    // the webform UI.
    // I use check plain because this is data entered by the user and needs to
    // be sanitised. You could also use filter_xss() if the field might
    // contain HTML.
    $form['submitted']['markup_field_key']['#markup'] = '
' . check_plain($name) . '
';
    // 2. If you don't have a webform markup component and just want to add
    // markup in, you can do this, just using regular form API.
    $form['submitted']['my_markup_field'] = array(
      // Defaults to type #markup.
      '#markup' => '
' . check_plain($name) . '
',
      '#weight' => 10,
    );
  }
}


When can we say a given scale has a tonal center?

Question: I have started studying music theory recently and I stumbled upon the concept of scales.
The way I understand it (please correct me if I am wrong):
It can be difficult sometimes to create a good harmonization between the melody and the rhythm if we include all of the existing 12 notes into a piece. Thus when a composer wants to compose a piece, he generally wishes to select a subset of the 12 existing notes and decides that his piece would mostly include only these notes. Thus the notes that he selects for his piece collectively form a 'scale'.
So basically, if my understanding is correct there can be many varied types of scales that can be formed. They can serve different purposes as each scale would have a very different flavor and mood attached to it.
I was then introduced to the concept of a tonal center: one note of your scale can be emphasized and it would pretty much act as a 'central point' around which the entire piece would seem to revolve about.
Now this was a very intriguing concept. But as I dived deeper into the topic, I got to know that there exist scales that do have a pretty well defined tonal center- such as the major and minor scales and their different modes.
But along with those, (since a scale is just a selection of notes from the existing 12 notes) there can also be scales defined which practically do not have a tonal center- such as the whole-tone scale or the chromatic scale.
One observation I was capable of making was that the major and minor scales are constructed with a combination of whole steps and half steps, i.e. the notes are differently spaced. I think this is one of the features required to have a tonal center.
As for the whole-tone scale or the chromatic scale all the notes are equidistant. I guess it becomes monotonous and our brain is not able to settle on one particular note as the 'home-base'. Or maybe scales with such a symmetric construction cannot have a tonal center.
These are just some theories I came up with after some thought and observations, which maybe be completely wrong, and they also lack a logical and concrete explanation backing it.
Hence I thought let's just jump a step back to a much more fundamental question. I was looking for maybe a more informative explanation as to why a scale even has a tonal center. What is the music theory involved behind any scale having or not having a tonal center?
If I want to create a new scale for my piece suppose, what must I keep in mind while selecting notes, to make it have a tonal center?
What are some necessary features in a scale's construction for it to have a tonal center? How to look at a scale's construction and comment about if it can have a tonal center or not?
For example:
Can the wholetone-halftone scale have a tonal center?
(It is an octatonic scale and constructed as such: W H W H W H W H )
I am very new to the music world, it's been a little more than 6 months now to be precise. Kindly help me with these questions that have been clouding my head since quite a long time now. It would keep me motivated :) .
Thank you in advance!
Comment: I once composed a piece that sounded tonal enough, yet exclusively used the halftone-wholetone scale. I still doubt that means that the scale I used has a tonal center.
Comment: That's nice @Dekkadeci !
Do you remember the rhythm or maybe the progression that you used in your piece?
Comment: Let's say you play the entire G half-whole scale as a dominant chord going into C. What is "the scale"? C is not in the G half-whole scale at all, but it can still be your tonic at the same time. The tonic is in the listener's mind, it's not in "the scale", whatever that is.
Comment: @Ninad - The rhythm was quite syncopated in places; I meant the piece to sound like heavy metal on a music loop. In places, I behaved as if E, G, or D were the tonic.
Comment: @piiperiReinstateMonica Yes, that was a good example. I think I kind of get it now... That helped! Thank you :)
Comment: @Dekkadeci Oh! Must have been a great composition. It sounds very interesting!
Answer: Tonal center is a name of the note that "feels like home". If you can name such note, then it is the tonal center. But you can use many different chords and scales in a single piece, while still maintaining the same tonal center.
A very simple example: you have a progression in C major, you use C ionian scale, but when the dominant G chord comes, you switch to G altered scale (G Ab A# B C# D# F). G altered doesn't even contain note C, but does it mean the tonal center changes? No! The root of the chord is G, but it produces a tension you want to resolve to C.

when a composer wants to compose a piece, he generally wishes to select a subset of the 12 existing notes and decides that his piece would mostly include only these notes. Thus the notes that he selects for his piece collectively form a 'scale'.

I don't believe this is accurate. You can find many examples (in particular in jazz and in styles borrowing from jazz) that don't stick to a single scale, that modulate often, yet still have a well defined tonic.
You certainly can base your composition on C whole-half scale, and make it feel like it's in C. On the other hand you can take notes from C ionian scale, and play them in the way that F feels like the tonal center... in which case it would be more accurate to say you use F lydian.
Comment: Thank you for your answer. It helped a lot! Can I maybe extend the discussion by asking if the tonal center of a piece is just an 'illusion' ? Only because a particular note is given an extra push the human brain just feels that it is actually the center of tonality?

The point I am trying to make is explained very well by giving a good analogy in the following video (you may choose to skip to 2:45).
youtube.com/watch?v=UcviIQg_BlU&t=6s

Thanks a lot in advance!
Comment: @Ninad All music theory is description of our feelings. In some sense all music is an illusion. We like to speak about vibrations of air, physical properties like frequency, amplitude, time, but in the end music is what happens in our brains in response to all of that.
Answer: One possible approach to this is exactly what you mentioned in your post: to recognize the inherent asymmetry in tonal music. The perfect fifth, for instance, does not split the octave in two, but rather just barely misses it. Same with the major triad: the third doesn't split the perfect fifth in two, but it just barely misses.
So one of the ways in which a scale is able to create a sense of a tonal center is by the inherent asymmetrical structure of the scale. This is often done by constructing the scale with different intervals (like half and whole steps) and by arranging those intervals in asymmetrical ways (like WWHWWWH as opposed to HWHWHWHW).
The corollary to this is that perfectly symmetrical scales do not have any inherent tonal center; if you were to play a chromatic or whole-tone scale, you would sense that either a) no pitch feels like a tonal center, or b) all pitches feel equally like a possible tonal center. In music based on these scale collections, other aspects of the piece have to help the listener determine what the tonal center is.
With all of that said, other aspects of the music can help suggest a tonal center no matter what scale collection is being used (if it's even just a single scale collection, which is very very rare). Extremes of register (i.e., the highest and lowest pitches) can help point towards a tonal center, extremes of dynamic, metrical emphasis, the lengths of held pitches, etc., can all suggest a tonal center, even in asymmetric collections like the whole-tone and chromatic scales.
Comment: Thank you for your answer. It helped me look at the whole thing more clearly (especially the last paragraph)!
Comment: I wouldn't classify the perfect fifth as "barely misses" cutting the octave in half cents-wise when there's an entire semitone (and not just a comma) between it and the interval that truly splits the octave in half cents-wise: the tritone.
Answer: I think you've already pointed in your question to one reason that a scale can have a tonal centre: because we're free to simply define it as having one. People are free to talk about the D chromatic scale, for example - and it might be entirely sane to describe the tonality of a piece as 'D chromatic', in some cases.
The reason why a particular scale might have a subjective sense of tonal centre comes down to the relationships between that central note and the other notes in the scale. One reason that the major scale has such a strong sense of tonal centre is that the tonic is accompanied by the major third, perfect fourth, perfect fifth, and major sixth - all notes that have a strong sense of consonance with the tonic. And as Tim says in the comment below, one of the notes that is very dissonant with the tonic is the 'leading note' a semitone below. So the major scale can create a sense of consonance with the tonic, and also of 'leading in' to it.
And yet - if we consider the modes, we realise that the exact same same interval pattern could have a number of other tonal centres, if we use phrasings in the music that emphasise those instead. This speaks to the fact that when it comes to scales, tonal centre really is partly just a question of definition, as well as a question of perception - and, leading on from that definition, an expectation of how we expect the scales to be used. C major and D dorian have exactly the same notes in, so we can't really say that that set of notes suggests one of those tonal centres and not the other. However, if we've defined the scale as D dorian, we're expecting those notes to be used in phrasings that gravitate towards D.
Comment: Don't forget the *leading note* - possibly even more telling. (Middle para.)
Comment: I think the full name of "D dorian scale" should be "the scale of the D dorian mode". A mode has a tonal center, but a scale doesn't.
Comment: @Tim very true - edited!
Comment: @piiperiReinstateMonica - a scale is merely a set of notes put into pitch order - usually played ascending/descending, but will always have a start/end pitch, so maybe it does have a tonal centre - that start (home) note. And, D Dorian isn't the same as the Dorian of D
Comment: I did not know something like that existed ( D chromatic scale ). Thank you for some good insights. @topoReinstateMonica
Comment: @Tim Well pointed! The leading note is quite significant.
Comment: @Tim A scale doesn't "have" a tonal center, it doesn't carry one with it. A tonal center - if there is one - is formed in the listener's mind and it's at least slightly subjective. Using notes from a certain scale doesn't guarantee anything about a tonal center. Playing a scale as a phrase is another thing - a phrase has a start and an end, and its notes land on beats in some rhythmical sense. People are confused about modes etc. because they can't find the tonal center by looking and calculating, they'd have to _feel_ it, which is not a logical and intellectual thing to do.
Comment: @piiperiReinstateMonica obviously I agree that the 'manifestation' of the tonal centre is in the listener's mind, but I think when most people talk about "The C major scale", they generally mean something like "The notes CDEFGAB, accompanied by an expectation that C is the tonal centre"? Hence why the taught form of the C major scale tends to start and end on C, so that the phrasing agrees with that expectation.
Answer: Following the theory by Schenker a "train" of a 5th 54321 or octave 87654321 or a triad 531 defines the tonal center, no matter if minor or major or any other mode.
Urlinie in relation to the tonic triad.
The fundamental line (German: Urlinie) is the melodic aspect of the Fundamental structure (Ursatz), "a stepwise descent from one of the triad notes to the tonic" with the bass arpeggiation being the harmonic aspect.[3] The fundamental line fills in the spaces created by the descending arpeggiation of the tonic triad. Its first tone (primary tone, head tone) may be scale degree 8, scale degree 5 or scale degree 3.
There are no tonal spaces other than those of scale degree 1—scale degree 3, scale degree 3—scale degree 5, and scale degree 5—scale degree 8. There is no other origin for passing-tone progressions, or of melody.[4]
https://en.wikipedia.org/wiki/Fundamental_structure


is Android key for maps similar to youtube key?

Question: I am using google maps and youtube in one application, so they need two keys.
Can I generate one key fot both of them?
I can create a project in google console then create a new client id then create an Android key. Does it work for both maps and youtube?
Answer: The simple answer to your question is no.. You can not generate one key fot both of them. Sad truth, i know. In the future this may become possible as Youtube is owned my Google now.
Comment: What I am doing in the google console is: going to 'services', turn on the youtube and google maps , then going to 'api access' and generating a key with SHA1 and package name, so to who does it belong? maps or youtube?
Comment: Can you help me to build a youtube key.


real analysis problem about $C^1$ functions

Question: Let $f: I \to \mathbb{R}$ be a $C^1$ function, where $I$ is an open interval in $\mathbb{R}$. Suppose that $f(0)\not = 0$. Prove that there are neighborhoods $U$ and $V$ of $0$ in $\mathbb{R}$ such that for all $x\in U$ there exists a unique $y\in V$ such that $$xf(y) = \int_0^y f(xt) dt,$$ and prove that the function $g:x\in U \mapsto y \in V$ defined by this is a $C^1$ function and satisfies $g'(0) = 1$.
How do I prove this? In reading through it, it seems that it may involve the Mean Value Theorem, but I just cant seem to piece it together and don't really know where to start.
thanks to all who take time to help
Answer: It is an application of the Implicit function theorem. Define
$$
F(x,y)=x\,f(y)-\int_0^yf(x\,t)\,dt\implies F(0,0)=0.
$$
We have to show that it is possible to solve for $x$ as a function of $y$ in a neighborhood of $(0,0)$. For this, check that the conditions of the Implicit function theorem are satisfied.
Comment: How can $F(x,y)$ involve a third variable, t?
Comment: It doesn't.${}$


Redundant entry of a Fugal Exposition

Question: When analyzing a fugue, I get confused at times about where to put the redundant entry. Is it the 5th or more entry of the subject and/or answer? I'm not sure.
Answer: In a 'textbook fugue' it's an (optional, and not ALL that common - so don't worry if you can't find it!) extra occurrence of the subject in the exposition section of a fugue, after all the voices have stated the subject (or countersubject),   So, in a 4-voice fugue it will be the 5th occurrence of the subject.
Not to be confused with anything that happens after the exposition.  And not all fugues are 'textbook'.
Comment: I would modify the above answer to say that one expects an entry in each voice. The redundant one, doesn't necessarily have to be the last sounding, although it is likely to be.

Redundant entry of a Fugal Exposition


Do equality constraints in an LP/MILP increase the difficulty of the program?

Question: I'm formulating a large MILP to solve a problem which resembles a directed graph. I have options to reduce the number of variables, but making my implementation code more complicated.
For example, a simple vertex conservation constraint could be implemented as follows, where v1 is the value of the vertex and $in1.*$ are incoming arcs, and $out1.*$ are the outgoing args:
$$in1.1 + in 1.2 + in1.3 = v1$$
$$v1 = out1.1 + out1.2 $$
Technically I could eliminate the $v1$ variable and simply equate the two to achieve the same result:
$$in1.1 + in 1.2 + in1.3 = out1.1 + out1.2$$
However this complicates my code and as well as my interpretation of the solution (eg. I have to manually calculate the value of $v1$ afterwards). Also, I would hope that any decent solver would eliminate a variable for each equality constraint via substitution, suggesting that the equality constraints do not add any difficulty to the optimization.
As an extreme case, obviously an LP with this constraint:
$$x1 = .....$$
should be no more or less difficult than an LP with these constraints:
$$x1 = .....$$
$$x2 = x1 + 123$$
$$x3 = x2 + 234$$
Since the values of $x2$ and $x3$ are directly determined by $x1$. In general I would expect that any equality constraint such as:
$$x1 + x2 + x3 + x4 = 0$$
would be eliminated simply by substituting $x1$ with $(-x2 - x3 - x4)$ throughout all constraints and objective function.
My questions:
1) Is it correct that that equality constraints do not increase the difficult of optimization problems (LP/MILP at least)? Any exceptions to this?
2) If so, would I be correct to assume that there is no downside to formulating a problem with an easier implementation which uses more variables, but constrained by equality?
Comment: In programming they say "premature optimization is the root of all evil", which in this case means you should try doing it the easy way (in terms of implementation) and only complicate the implementation if you really need a runtime improvement.
Answer: If you are asking whether additional equation constraints increase difficulty (as opposed to whether equations are any better or worse than inequalities), the answer is a resounding "maybe". On the one hand, more equations may mean more rows in the constraint matrix, which may make decomposition of the matrix, updates to the basis matrix etc. slower due to requiring more operations (and consuming more RAM). Adding auxiliary variables also means more columns (more storage, somewhat more work per iteration). On the other hand, additional constraints may make the constraint matrix sparser, and modern solvers handle sparse matrices faster than dense ones.
Keep in mind, though, that modern solvers tend to do a "presolve" step, in which the problem you fed the solver is transformed into an equivalent but possibly more compact version. During presolve, some rows and columns may be eliminated from the constraint matrix, by a variety of techniques. The presolver may recognize that, in your example, $v1$ can be eliminated algebraically. It might also recognize that some combination of constraints and/or objective coefficients imply that certain variables must take specific values, and eliminate them. A particular presolver might also look at your problem and decide not to eliminate $v1$ because doing so would turn two sparse constraints into one denser constraint.
Back when men were men and computers had vacuum tubes (except the newfangled ones, which had those transistor things), squeezing a formulation down to something compact was important. Today, I would advocate for using the most convenient and/or human-readable model (meaning, in your case, leaving $v1$ in) and letting the presolver decide. That's my personal practice, for several reasons:

the people who write the presolver are smarter than I am;
the presolver is considerably less likely to make a mistake compacting the model than I am to make an algebra error doing so; and
my original model (before any "cleverness" on my part) tends to be both easier to explain to other people and easier to maintain (if it needs changing down the road).


How to install whm,cPanel in my laptop?

Question: I want to install whm & cpanel in my laptop. I install centOs 6.4 using oracle virtualBox as per instruction/iso in http://www.comunidadhosting.com/t/tutorial-de-como-instalar-cpanel-en-localhost.18879/.
After install it i check IP and it show me 10.0.2.15 . but my main problem is I am not able ping to guest machine from my host. When I ping from my guest to host machine then it's working fine , and host get internet access also.
I use NAT in networking.
Can any body tell me what is the problem and how to solve it ?
Comment: i suggest you use bridged connection and use same network range on your centos also: like if you use : 192.168.1.1 on main PC TAP than use 192.168.1.2 on centos network setting
Answer: You need to check if you have a bridged connection OR a NAT connection with the outside host network. Both should have the same network connection and IP may be same/different.
And, if you have done this, then you need to disable the firewall on the guest-machine side so that your host machine could connect to the VM.
Your ping may then be failing because of getting blocked by the VM's firewall.
Comment: Hi @asad thanks for answer , I already mention that I use NAT setting in virtualBox.
I am unable to ping from centOs (Guest) to Win 8.1(Host) machine, but I able to ping opposite.
Comment: @SKG-Would you please describe at which step are you stuck? Actually, I reversed the answer. I carefully looked your question, you need to ping your guest from your host. Soo, turn off the firewall of VM. Check if `firewalld` is creating trouble for you in CentOS(VM).
Comment: As per instruction I stop firewall in vm but it didn't work. Now I forward the port to vm and it's working now

How to install whm,cPanel in my laptop?


Restrict coupon for same shipping address (or user creating multiple email ids)

Question: We have scheme "Register & Earn Coupon of Rs. 200"
Some users create multiple gmail accounts to avail multiple coupon.
They get the order shipped at the same place.
Is there a solution available to handle this?
Comment: shipping address validation is quiet complex in terms of character and word. User might have multiple locations. Address is not feasible solution I guess.
Answer: There is surely a way to block this, you can alter the forms and add validations. But do you really want one to one mapping between shipping address and user account?
What if the old user wants to purchase the same product again, or a new user signing up stays in the same apartment?
Maybe you need to review your business rules and figure out a better way to restrict this behaviour.
Maybe IP address?


How to determine if the bottom of a website was reached on iPhone browser?

Question: If an iPhone user scrolls vertically down to the bottom of a site, I need to know this (via JavaScript event).
I already checked on this How do you know the scroll bar has reached bottom of a page and this solution seems to work well on desktop browsers (tested on Google Chrome).
On iPhone though it won't work.
Is there a better solution?
Comment: @Sebas I don't know. If I use the code proposed in the link above that I posted, the function foo() simply won't get called (on iPhone at least): `$(window).scroll(function(){
if ($(window).scrollTop() == $(document).height() - $(window).height()){
foo();
}
});`
Comment: did you check this? http://bugs.jquery.com/ticket/6724
Comment: @Seba That's it! Using the workaround posted in that link did the trick. If you want you can post your answer as a reply here and I'll mark it as the right answer.
Comment: ok! thanks, i'm doing so
Answer: Maybe this is due to this JQuery deffect, did you check this?
bugs.jquery.com/ticket/6724
They provide a workaround.
rgds.
Answer: You can use a jquery plugin called iosScript Refer to this location for more details.


"cannot convert type 'system.linq.igrouping to Models.CashCall"

Question: My code is as below
 public string GetCashCallsbyDate(DateTime _date)
    {
        using (var context = new TildaDbContext())
        {
           List<CashCall> cashcallName = new List<CashCall>();
            try
            {
                CashCall cashCall = new CashCall();
                var cashcalls = context.CashCalls.Where(d => d.Date == _date).GroupBy(g => g.CashCallName).ToList();
                foreach (var data in cashcalls)
                {
                    cashCall = data;   // error here
                    cashcallName.Add(cashCall);
                }
                string CashCallsbyDate = JsonConvert.SerializeObject(cashcallName);
                return CashCallsbyDate;
            }
            catch (Exception e)
            {
                return null;
            }
        }
    }

I am getting compile time error as

cannot convert type 'system.linq.igrouping to
  Models.CashCall

Please help. Thanks.
Answer: This is because data on each loop is a group of one or more CashCall objects that share the same name.
Presumably you are grouping for a reason, so it may not be appropriate just to ungroup the data (otherwise the GroupBy would be completely redundant) by loopnig over contents in each group as suggested elsehwhere. So you could do something like just get the first item in the group. For example:
foreach (var data in cashcalls)
{
    cashCall = data.First(); // Gets the first element in the group.
    cashcallName.Add(cashCall);
}

You may want to aggregate the data from all the items in the group and create a new CashCall object based on that aggregation, but that is entirely up to your use case.
If you do want all the cashCall objects, but just want all the ones with the same name grouped together, it would be better to use an OrderBy instead of the GroupBy.
Comment: Finally achieved it. Thanks a lot.


Show current date in the calander

Question: Show current date in the calander
from tkinter import *
from tkcalendar import *
root = Tk()
root.geometry("500x300")

date_field = DateEntry(root, selectmode="day", year=2021, month=12, day=8)
date_field.grid(row=3, column=1, ipadx="100")

root.mainloop()

Comment: I'm voting to close this question because The question was changed after answers were posted
Comment: You have completely changed the question.  Better post another question instead.
Comment: Does this answer your question? [How do I create a date picker in tkinter?](https://stackoverflow.com/questions/4443786/how-do-i-create-a-date-picker-in-tkinter)
Comment: It seams to me that someone wants all the answers with no effort at all. Please close this question.
Answer: One of the way:
...
timeslots = [f'{hr:02}:{mi:02}' for hr in range(24) for mi in range(0, 60, 15)]
kommt_field = ttk.Combobox(root, values=timeslots)
...

Answer: from tkinter import *
from tkcalendar import *
root = Tk()
root.geometry("500x300")

date_field = DateEntry(root, selectmode="day"8)
date_field.grid(row=3, column=1, ipadx="100")

root.mainloop()

Comment: There is no need to make a self-answer question because this thing has been asked a lot of times. First, check if the question is a duplicate before posting.
Comment: Also, please remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, can you [edit] your answer to include an explanation of what you're doing and why you believe it is the best approach to your original question?

Show current date in the calander


Adjust weight for menu items in different languages

Question: My site will be in English and Arabic. English reads from left to right and Arabic from right to left. Drupal 8 works great with this if you enable all 4 multilingual extentions.
The only problem I have is that I translated the main menu, but I can't find how I can add a different weight to the separate menu items, this since I want the menu to be mirrored on Arabic.
If you go to the separate menu items you can add a weight but above it it is written: Weight (all languages)
How can I adjust the weight per language per item?
UPDATE: the answer by Owais Alam does not handle the adjustment of menu-items-weight per language in Drupal 8
Answer: With Drupal 7 core alone, user-defined menu items are not translatable. The Menu translation module, part of the Internationalization (i18n) package, allows users to select a translation mode for each menu (Figure 1).
The following modes are available:

No Multilingual Options
Translate and Localize
Fixed Language

Translate and Localize Menus
For most use cases, and especially if the structure of your menus is consistent across languages, Translate and Localize is the preferred method of translating menus. You can create one menu for all languages, and translate or localize each menu item. There are two ways that menu items will be translated:

You can set a language when creating a custom menu item so that the
menu item will only show up for that language (Figure 1). Menu items
that link to nodes in a particular language will be treated this way.
You can localize other custom menu items without a language (for
example, menu items linking to Views pages). Use the Translate tab to
translate the menu item title and description (Figure 2). Translators
can also use the 'Translate interface' pages to translate these menu
items (Figure 3).

Figure 2

Figure 3
Fixed Language Menus
If you choose Fixed Language, you'll have to set up a separate menu in each language. This could become tedious if have a lot of languages enabled on your site, but is useful if the content or menu structure is different for each language.
Menu Items Linked to Nodes
Useful modules:

For Entity translation use entity_translation_i18n_menu submodule of
Entity translation.
For content translation use Menu translation - Node.

A menu item linked to a node will be displayed only when the node language matches the page language. This is due to how the menu system works and the 'Language selection' feature in i18n. Therefore, to get translated menus items that link to nodes, you first need translated content: see Translating content to different languages.
Comment: Thank you for your reply. Even though this in no way answer my question. I am working with Drupal 8 (as is written) and this is for 7. Furthermore it does not cover the weight of menu-items in any way. Do you have any idea how to adjust the weight per language in Drupal 8 with translation core modules enabled?
Comment: ok hope this will help you https://www.drupal.org/node/2415945
Comment: @OwaisAlam You should let others know the Drupal version in your question, otherwise, it will ruins others' effort.
Comment: @JimmyKo i have mentioned Drupal 7. Thanks for your suggestion.
Comment: @OwaisAlam please add it in tag as well :)
Comment: Could I ask you to delete the anwser since it is totally of topic and might prevent people who actually know how to change the weight per language to anwser especially since someone up voted this anwser.
Comment: @WillyBurb : even though I agree that this answer is about D7 (while your question is, from the beginning, about D8), I do not think that "this" answer will stop anybody, who does have a D8 answer for you, to post another D8-related attempt to answer your question. If I was you, I'd edit my own question by adding a note to the end of it like "*Note*: the answer from [Owais Alam](http://drupal.stackexchange.com/users/66189/owais-alam) does not help (it's about D7, my question is about D8)". As a variation, just add that as an extra comment below your question. Good luck!


Screen inside another screen is possible in caliburn micro

Question: I am adding Screen inside another screen in caliburn Micro wpf. Is it best practice? If it is best way, Why?
Answer: Yes you can do it. Take a look on a documentation before ask, and please form your questions properly.
Here is an example of multiple views rendering using Caluburn Micro:
Add multiple views inside a view using WPF and Caliburn.Micro
Comment: Thank you, for your valuable information


Allow public to edit content type

Question: I have a content type that users can vote on so I need that content type to keep tally of the votes.
I'm assuming I need to allow the public to edit that content type but, unsure how to go about doing so.
I on the daily basis use web services on other projects and would just POST the data to the entry on click of a button or something. Is it a similar way just POST a change to the content type via ajax in drupal?
Sorry if this is a dumb question, majority of my skills are in front-end dev not server side so any help is appreciated.
Answer: I'm not sure I understand you correctly but let me clarify:
A.If you have a new content type that users can vote for that. You just use the votingapi http://drupal.org/project/votingapi and use with Fivestar module http://drupal.org/project/fivestar or plus1 module http://drupal.org/project/plus1. You will go to plus1 or fivestar for setting the content type that be voted.
B.To determine the total of the votes of your content type you should do following:

Create a new view and choose the type of your content, remove sort options, fields and other default settings.
Add a field for "Content: Type" Expand the "advanced" part on the right and set "Use aggregation" to "Yes"
Create the relationships with votes table("Content: Votes")
Add another field for "Content: Votes", click "Aggregation settings" and set the aggregation type to "count"
The second "Content: Votes" should now look like " COUNT(Votes: Value)"  and now you can see your content type and the total votes of that.

C. In order to anonymous users can edit your content type just go to permissions and set configuration for anonymous users.
Answer: If the "edit" type you need is just for voting purpose, take a look to Flag module
Answer: The Fivestar module allows voting permissions to be configured by role, including the anonymous role. The voting is tallied by the module, not in the content type being voted on, so the user does not need any permission but 'View Content'.
You should never give content edit privileges to an anonymous user. Requiring a user to log in before they are allowed to change information on your site is a basic security measure. If a user makes a change, you are able to attribute the change to the user. If the user does something unwanted, then you can deal with that user accordingly.

Allow public to edit content type


Word problem about travel time on a river with a current

Question: On a river there is no current from A to B, but a current from B to C. A man rows down a stream from A to C in 3 hrs and C to A in 7/2 hrs; had there been the same current in all the way as from B to C his journey down stream would have taken 11/4 hrs; find the time his return journey would have taken under the same circumstances.
Comment: Welcome to math.SE: since you are a new user, I wanted to let you know a few things about the site. In order to get the best possible answers, it is helpful if you say what your thoughts on the problem are so far; this will prevent people from telling you things you already know, and help them write their answers at an appropriate level. Also, many find the use of imperative ("Find") to be rude when asking for help; please consider rewriting your post.
Comment: maybe it is related somehow  to triangle ?
Answer: Hint:  When you have a problem like this, it is useful to think 1)what are the variables and 2)what are the equations that connect them.  Write down the variables you see, then the equations that they satisfy.  Clearly the basic equation we need is $\text{speed}=\frac{\text{distance}}{\text{time}}$.  Where do we find distances, times, and speeds?  Let $a$ be the distance from A to B, let $b$ be the distance from B to C.  Let $r$ be the speed he rows and let $c$ be the speed of the current.  The first condition is then $\frac ar + \frac b{r+c}=3$  We know this because the lack of a current from A to B means his speed is just his rowing speed, but from B to C he was sped along by the current.  You should find two more similar equations in the story.  This will lead to three equations in four unknowns, so there will be one piece of information missing.  However the questioner promises you that you can find the time of the return trip if it were all against the current.


Returning multiple aggregated columns from Subquery

Question: I am trying to extend an existing query by aggregating some rows from another table.
It works when I only return one column like this:
Select DISTINCT
   Contracts.id,
   Contracts.beginTime,
   Contracts.endTime,
   Suppliers.name
   (SELECT COUNT(p.id) from production as p where p.id_contract = Contracts.id)
FROM Contracts
LEFT JOIN Suppliers on Contracts.id = Suppliers.id_contract

Then I tried to add another column for the aggregated volume:
Select DISTINCT
   Contracts.id,
   Contracts.beginTime,
   Contracts.endTime,
   Suppliers.name
   (SELECT COUNT(p.id), SUM(p.volume) from production as p where p.id_contract = Contracts.id)
FROM Contracts
LEFT JOIN Suppliers on Contracts.id = Suppliers.id_contract

However, this returns the following error:

Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

I experimented a bit with the EXISTS keyword, but couldn't figure out how to make it work. Also I'm not sure whether this is the way to go in my case.
The desired output would be like so:
contract1Id, supplierInfoContract1, nrItemsContract1, sumVolumeContract1
contract2Id, supplierInfoContract2, nrItemsContract2, sumVolumeContract2

Comment: exists is a subquery where you join a parent table with a child table in the subquery.  It returns true if there are results in the subquery.   What is your desired output?
Comment: Added the desired output. I hope it is clear
Comment: your code needs two subqueries each one return only one result then the error message will not occur.  The error message is telling you that your subquery is returning two results for one variable.
Answer: Instead of using DISTINCT and subqueries, use GROUP BY and normal joins to get the aggregates. And always use aliases, it will make your life easier:
SELECT
    c.id,
    c.beginTime,
    c.endTime,
    s.name,
    COUNT(p.id) prod_count,
    SUM(p.volume) prod_vol
FROM Contracts c
LEFT JOIN production p on p.id_contract = c.id
LEFT JOIN Suppliers s on c.id = s.id_contract
GROUP BY c.id, c.beginTime, c.endTime, s.name;

Another option is to APPLY the grouped up subquery:
SELECT DISTINCT
    c.id,
    c.beginTime,
    c.endTime,
    s.name,
    p.prod_count,
    p.prod_vol
FROM Contracts c
LEFT JOIN Suppliers s on c.id = s.id_contract
OUTER APPLY (
    SELECT
        COUNT(p.id) prod_count,
        SUM(p.volume) prod_vol
    FROM production p WHERE p.id_contract = c.id
    GROUP BY ()
) p;

You can also use CROSS APPLY and leave out the GROUP BY (), this uses a scalar aggregate and returns 0 instead of null for no rows.
One last point: DISTINCT in a joined query is a bit of a code smell, it usually indicates the query writer wasn't thinking too hard about what the joined tables returned, and just wanted to get rid of duplicate rows.
Comment: Actually, the existing query is much bigger then my example, and the select contains about 20 columns. So I may need to add all of those to the group by clause. Not very elegant, but I will try it.
Comment: Have updated with another option that may work better for you
Comment: Thanks, the second option works perfectly fine. We use a self-written C++ library to generate our queries, which only supports a simple query structure and the second option works fine with it too.
Actually I removed the DISTINCT keyword, as it wasn't necessary.
Answer: You should use it like below:
Select DISTINCT Contracts.id, Contracts.beginTime, Contracts.endTime, Suppliers.name
(SELECT COUNT(p.id) from production as p where p.id_contract = Contracts.id) as CNT,
(SELECT SUM(p.volume) from production as p where p.id_contract = Contracts.id) as VOLUME
FROM Contracts
LEFT JOIN Suppliers on Contracts.id = Suppliers.id_contract

Comment: I was thinking about that, but I may want to add multiple additional aggregated columns in the future. I guess this would be very inefficient, as the table will also get big over time.
Comment: Yeah if you have too many aggregated columns, then reply by CharlieFace will be ideal. Ensure to have proper primary key, indexes and where condition in place while applying joins to larger tables.
Answer: I guess you can try to rework your query as
 SELECT X.ID,X.beginTime,X.endTime,X.name,CR.CNTT,CR.TOTAL_VOLUME
 FROM
 (
     Select DISTINCT Contracts.id, Contracts.beginTime, Contracts.endTime,      Suppliers.name
     FROM Contracts
     LEFT JOIN Suppliers on Contracts.id = Suppliers.id_contract
 )X
 CROSS APPLY
 (
   SELECT COUNT(p.id)AS CNTT,SUM(p.volume) AS TOTAL_VOLUME
   from production as p where p.id_contract = X.id
 )CR

Answer: I reworked you query slightly by separating the subqueries.
Select DISTINCT
   Contracts.id,
   Contracts.beginTime,
   Contracts.endTime,
   Suppliers.name,
   (SELECT COUNT(p.id) from production as p where p.id_contract = Contracts.id) count_id,
    (SELECT SUM(p.volume) from production as p where p.id_contract = Contracts.id) sum_volume
FROM Contracts
LEFT JOIN Suppliers on Contracts.id = Suppliers.id_contract


How can I make a strongly typed set of string values?

Question: I know enums can't be strings
I would like a data structure with the utility of an enum, but one which returns strings instead of integers. To be clear, I want the return type to be the enum-like type, not string. Basically, I want to be able to force a property to be usable as a string but is only allowed to be set to a value in a defined set of strings. Something like
stringenum Unit {
    Pixels = "px",
    Inches = "in"
}

class Settings {
    public Unit Unit { get; set; }
}

var settings = new Settings() { Unit = Unit.Pixels };

...

unitLabel.Text = settings.Unit;

I've seen some solutions that just create a class with properties that return a certain string. However, I need the return type to be limited to a set, not just any string.
EDIT FOR CLARIFICATION
Consider my previous example in addition to this method:
public void WriteUnit(Unit unit)
{
    Console.WriteLine(unit);
}

// Calling
WriteUnit(Unit.Pixels); // Prints "px"
WriteUnit("px"); // ARGUMENT EXCEPTION

This method will throw an ArgumentException if you pass it a string. It only accepts the type. This is specifically what I'm looking for.
Comment: Indeed @Fildor, maybe a [DescriptionAttribute](https://learn.microsoft.com/dotnet/api/system.componentmodel.descriptionattribute) can match the question, a little rude.
Comment: Why not use the enum member name (and ToString() it when you want a string), but the rest of the time use it as an enum? `enum Unit { px, [in] }` and `var x = Unit.px; string xs = x.ToString()` - typically you only want a string for output purposes...
Comment: Have a look at this answer: https://stackoverflow.com/a/2650090/982149
Comment: Whenever you want the string, but remember that you can often use something that calls toString anyway - for example if you Console.WriteLine'd it, it would call ToString.. If you string interpolated it it would also. You can probably boil the "number of places that call tostring on the enum" in your code down to one or two...
Comment: You could use the typing mech then... `class Unit{ public Unit Pixel { get => return PixelUnit } }` `class PixelUnit:Unit { public override string ToString() => "px"; }}` ? Should work in your WriteUnit, Console.WriteLine will call ToString().. Unit could have a string parsing routine if you need.. ? If you really wanted something to explode when a string is passed, could add an overload of WriteUnit that explodes.. but it's definitely odd to add an overload just to honeypot a developer into seeing a "don't call this method with a string".. easier to just not provide the method at all, i think
Comment: No, because the return type of those properties is "string".
Comment: Yes, that's why I asked the question.
Comment: There's no solution than for example a set of consts, fields or properties in a dedicated static class, as I know. You can't use string as enum values, only primitve integer numbers & chars are allowed: [Enumeration types (C# reference)](https://docs.microsoft.com/dotnet/csharp/language-reference/builtin-types/enum) • [Enums Specification](https://docs.microsoft.com/dotnet/csharp/language-reference/language-specification/enums) • [Enum Class](https://docs.microsoft.com/dotnet/api/system.enum) • [Enum.ToUInt64](https://source.dot.net/#System.Private.CoreLib/Enum.cs,b9252aea503e83b4,references)
Comment: I think you could work around that, though. For example by using annotations on the enum values.
Comment: @Fildor do you have an example?
Comment: Actually @DavidL's answer using a dictionary as a dispatch table should fit, a good compromise. My preference would however go towards the use of an attribute, cleaner to declare I find
Comment: Thanks for the replies. I've considered a dictionary, but I want to be able to set a property to a type which limits it to that set of strings.
Comment: @OlivierRogier A viable solution ... with the downside that it is not obvious from the enum code alone, that you have to maintain the dictionary, too. But, well ... I'd either put them in the same file or add a comment.
Comment: Consider how with an `enum` I can make a property `public Units MyUnit { get; set; }` that would then only allow children of the Units enum. I want that same behavior with string values. Returning a string isn't the issue. Does that make sense?
Comment: @CaiusJard wouldn't it just return "1" instead of 1? I'd need it to be any defined string.
Comment: @CollinBrittain no, ToString from an enum returns the enum name.
Comment: https://learn.microsoft.com/en-us/dotnet/api/system.enum.tostring?view=net-5.0
Comment: @Magnetron Oh I see. Can I do that in the definition or would I have to do it whenever I want the string value?
Comment: Also, if you want your code nice and to C# conventions you could use an attribute to give a different ToString output: https://stackoverflow.com/questions/479410/enum-tostring-with-user-friendly-strings - not sure what your thoughts are on going to those lengths to be able to have e.g. `Unit.Inches` output as `"in"`
Comment: @CaiusJard Thanks for the info. I think what I want is that behavior but without the need to add .ToString() everywhere that I use the enum. My feeling from all of the responses is that C# isn't really capable of doing what I want.
Comment: But that's just it - you don't ToString it "everywhere* because not everywhere can possibly *need* it as a string.. Like for example, if I make a Person class, and set their age, and pass them to another class that .. I dunno, stores them in a list and calcualtes the average ages, I don't "serialize the Person to json" just to pass it, or serialize it to store it, deser it to calcualte the age... I'm pretty sure this "I need it as a string everywhere" is actually really only "i need it as a string in my file writing routine"
Comment: @CaiusJard See my edit, maybe it will make more sense. I'm not sure I'm explaining very well.
Comment: (Or put another way, you use a textbox to ask the person age, you parse it to an int, your program stores it as as an int in a db column typed as int, maths it as an int, passes it as an int.. it's whole life it's an int.. Then you write it to CSV and only then it becomes a string again, at the last moment. Every program you ever wrote got strings from the user, transformed them, kept the transformation, and only string'd them again at the final moment)
Comment: I see the edit, but it's an odd problem to solve; you're the developer, and only you can cause and resolve the argument exception so maybe just "don't pass a string" (which you cannot do if it's an enum; its not a runtime argument exception but a compile time "cannot convert from string to enum" error that you'll get).. Who are you trying to control here? You say you want runtime errors as though the user will have some way of passing a string to your expects-enum method, but it seems like youre trying to control compile time errors (by other devs on the team?)
Comment: @CaiusJard The point of the exception was to illustrate that the runtime respected the strongly typed behavior I want, not that I can't avoid an exception.
Answer: As mentioned in the comments, you cannot directly map an enum to a string.
That said, there is nothing preventing you from creating a map of enum to string values, that can only be accessed via the enum.  If you maintain the mapping, you can guarantee that the value always exist.
public enum Unit
{
    Pixels,
    Inches
}

public static class UnitMapper
{
    private static readonly Dictionary<Unit, string> _map
        = new Dictionary<UserQuery.Unit, string>()
        {
            { Unit.Pixels, "px" },
            { Unit.Inches, "in" }
        }

    public static string GetUnit(Unit unit)
    {
        return _map[unit];
    }
}

Based on your additional comments, this can be combined with a custom user-defined implicit operator to give you the type of functionality you are looking for, although you will still have to call the overridden .ToString() to output a string.
public struct UnitWrapper
{
    private readonly string _unitString;
    private readonly Unit _unit;

    public UnitWrapper(Unit unit)
    {
        _unit = unit;
        _unitString = UnitMapper.GetUnit(_unit);
    }

    public static implicit operator UnitWrapper(Unit unit)
    {
        return new UnitWrapper(unit);
    }

    public override string ToString() => _unitString;
}

This can then be used as follows:
public class Settings
{
    public UnitWrapper UnitWrapper { get; set; }
}

var settings = new Settings { UnitWrapper = Unit.Pixels };
string px = settings.UnitWrapper.ToString();

Comment: @Fildor that just takes advantage of string format overrides. You could easily accomplish the same thing with `Console.WriteLine($"{unit}");`. In either case, it completely obfuscates intent. I agree, it is a terrible hack :). At least when explicitly calling `.ToString()`, it is obvious what you are trying to do.  It's a good simplification and it avoids any sort of external map
Comment: @DavidL I found myself "chasing my tail" wanting to squish all the requirements into one type ... I really don't think it's worth the hassle, too. And it's surprising the client.
Comment: Thanks, David. The core problem I need solved is I want to constrain a property to a type that only allows certain string values. This would indeed allow me to get a string from a set using a method, but not define a property with a type that bounds the set of allowed strings.
Comment: I'm not sure I follow.  If you override the setter of a property to expand this use case, you can always limit exactly what is allowable for any specific property.
Comment: So with your answer, I couldn't give a class a property of type `Unit` that evaluates to a string. I would have to call a method to get a string based on the Unit value. I'm essentially asking, how do I make a type that behaves as though an enum could return strings instead of ints.
Comment: You _maybe_ could get something that behaves like you want with [User-defined conversion operators](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/user-defined-conversion-operators) , but it wouldn't be an enum.
Comment: @Fildor It doesn't need to be an enum, just mimic the behavior. I'll check that out!
Comment: @CollinBrittain thanks for the additional details. I agree, a user-defined conversion operator is the right approach, with the caveat that the type will have to wrap Unit and you can't implicitly extend string, so you would have to call ToString. I've updated my answer with those details.
Comment: Honestly, I don't think I'd bother with the dictionary overhead; cast it to an int and use it to index a `string[]` ?
Comment: Yeah that's a bummer. I would really like to not call ToString() (see my edit in the question). I think the ultimate answer is going to be that C# is not capable of this behavior. I appreciate your help.
Comment: @CaiusJard that's a great optimization and would work as well. I was envisioning that there was more that the OP wanted to do based on the original question, but if the entire intent is to simply map, that would be a nice shortcut, it is really splitting hairs at this level.
Comment: @DavidL Mapping is not what I'm looking for. I think the example in my question edit should clarify.
Comment: @CollinBrittain "Thanks, David. The core problem I need solved is I want to constrain a property to a type that only allows certain string values. This would indeed allow me to get a string from a set using a method, but not define a property with a type that bounds the set of allowed strings." Your options are: 1. Make a Runtime Check. using contains 2. Turn the Property type into a Enum - as it always should have been - and have the string interpretation of the Enum as a side project/secondary get-only property.
Comment: @CollinBrittain if you are constraining an input/output to a range of values, the easiest way to guarantee that is a map.  In addition, I am not aware of any language feature that can do specifically what you want to do.  To my knowledge, this is the closest you can get.
Comment: This was my shot at this: https://dotnetfiddle.net/8A9Ye4 - SO hacky ... I don't like it.
Comment: @Fildor I think it comes down to a frustration with the language for me. I've been using rust which allows enums that can evaluate to any type you want instead of just an int, and still gives you the strongly typed functionality.
Comment: Well, sometimes you chose the tool, sometimes you adapt to the features. But here's another shot: https://dotnetfiddle.net/Ocfoub - using enum and an extension method.
Comment: ^^ Coming from Java, I also had my struggles with enum. There, this would also be easi_er_. But I learned to actually like it somehow. Forces you to code cleaner, if you are forced to keep the strings on the edges of the system.


Python: Show content of hidden parameters for debugging

Question: my core question is: how can I visualize hidden parameters of a class for debugging?
I'm using spyder 3.6 for coding and have e.g. this test code:
class ExampleClass():

    def __init__(self, number):
        self.__hiddenVar = number
        self.visibleVar = number
        print(str(self.__hiddenVar ) + 'at init')

    def plus_2_times_4(x):
        return(4*(x + 2))

    def arithmetic(self):
        print(str(self.__hiddenVar ) + 'at arithmetic')
        return(ExampleClass.plus_2_times_4(self.__hiddenVar ))

instance = ExampleClass(number = 4)
instance.arithmetic()

Now I want to stop the run let's say at the printout of "arithmetic" and debug the content of "self".
When I run "self.visibleVar" in console window of spyder, it shows the conent, in this case "4".
When I do the same with "self.__hiddenVar" it throws an error: *** AttributeError: 'ExampleClass' object has no attribute '__hiddenVar'
Well, I understand why this is the case: it is a hidden variable and this is how it is supposed to be. But how can I debug in that situation? Do I have any possibility to show the content even though it is a hidden variable?
Answer: As the first thing, I would suggest you to read:

What is the meaning of single and double underscore before an object name?
How to access "__" (double underscore) variables in methods added to a class

To resume the content that you really need, I must say that the interpreter "transforms" __hiddenvar to _ExampleClass__hiddenvar.
Comment: This community is really awesome: I ask a question and get an answer within 5 minutes that is really helpful! Great and thanks a lot.
Comment: @Data4711, I had exactly your same reaction around a year ago! Check other StackExchange communities, like CodeReview, they will leave you amazed.


Ferramenta para gerar XPath

Question: Olá, estou fazendo um spider para capturar com XPath alguns dados da web.
Porem a criação de xpath é meio trabalhosa. Alguém conhece alguma forma de treinar XPath? Exemplo; Clico 5 vezes em um link e alguma ferramenta gera o xpath.
Qualquer dica é bem vinda.
Obrigado!
Answer: Fiz um compilado das respostas encontradas aqui:
https://stackoverflow.com/questions/3030487/is-there-a-way-to-get-the-xpath-in-google-chrome
"Chrome Dev Tools"
Ao usar o "Chrome Dev Tools" (Pode acessar clicando com o botão direito em algum elemento e pedir para "inspecionar elemento") você pode clicar com o botão direito no elemento, em seguida "Copiar XPath".
Add-on para o Chrome.
https://chrome.google.com/webstore/detail/xpath-helper/hgimnogjllphhhkhlmebbmlgjoejdpjl
Console Javascript
Você pode usar também o $x no console javascript do Chrome.
ex: $x("//img")
XPather
Não cheguei a testar, mas esta ferramenta aparentemente parecer ser o que você quer.
XPather
Comment: Legal a compilação, Arthur, +1! Já usei muito essa add-on para o Chrome. Adicionei uma resposta com o http://selectorgadget.com -- dá uma olhada caso vc não conheça, é bem legal. ;)
Comment: A idéia do XPather parece ser o que preciso.. mais não consegui instalar no Firefox
Answer: Experimente o bookmarklet SelectorGadget.
Ele funciona assim: logo depois que você aciona, você clica numa coisa que você quer capturar. Ele gera um seletor bem genérico, e destaca em amarelo as seções capturadas.
Daí pra frente, você pode refinar a seleção com: 1) clicando numa área destacada pra remover da seleção; ou 2) clicando numa área não destacada pra adicionar a seleção.
Por padrão ele gera um seletor CSS, mas tem um botão XPath para pegar a expressão equivalente em XPath.
Answer: Tem um add-on para o Firefox que faz consultas XPath em uma página, chamado XPath Checker.
Pagina do add-on
Answer: Instalar xmllint (ferramente minúscula, normalmente já instalado em linux) e toca a praticar xpath:
xmllint --xpath '//symbol' file.xml

ou, tratando-se de html:
xmllint --html --xpath '//table' file.html

ou mesmo
xmllint --html  --xpath '//a' 'http://a.b.c/index.html' > links.txt

Ferramenta para gerar XPath


About $R/I$ Where $I$ is a Prime Ideal

Question: A well known result in Commutative Algebra says: for a commutative ring $R$ with $1$, $R/I$ is an Integral Domain if and only if $I$ is a Prime Ideal of $R$. Can this result be generalised for non commutative rings?
Answer: If $R$ is a noncommutative ring, there are two different notions of prime ideals, both of which are equivalent to the usual definition in case $R$ is commutative.

A proper ideal $P$ of $R$ is a prime ideal if the following equivalent conditions hold.

For ideals $I$, $J$ of $R$, if $IJ \subset P$ then $I \subset P$ or $J \subset P$.
For left ideals $I$, $J$ of $R$, if $IJ \subset P$ then $I \subset P$ or $J \subset P$.
For right ideals $I$, $J$ of $R$, if $IJ \subset P$ then $I \subset P$ or $J \subset P$.
For all $a$, $b \in R$, if $aRb \in P$, then $a \in P$ or $b \in P$.

A proper ideal $P$ of $R$ is a completely prime ideal if, for all $a$, $b \in R$, $ab \in P$ implies $a \in P$ or $b \in P$.

It follows that an ideal $P$ of $R$ is a completely prime ideal if and only if $R/P$ is a domain (in the sense that $R/P$ is a nonzero ring which has no zero-divisors except for $0$ itself; it need not be commutative).
A ring in which $0$ is a prime ideal is called a prime ring. Thus an ideal $P$ of $R$ is prime if and only if $R/P$ is a prime ring.
Every completely prime ideal is a prime ideal. The converse is true for commutative rings, but not in general for noncommutative rings. Every maximal ideal is a prime ideal, but not necessarily completely prime. For instance, this is the case in any simple ring which is not a domain.
So, if $K$ is a field, then $M_n(K)$ is a prime ring (the unique proper ideal, $0$, is a prime ideal). However $M_n(K)$ is not completely prime for $n > 1$ since there are non-trivial zero-divisors. In the matrix ring $M_n(\mathbb Z)$ ($n>1$) every prime number generates a prime but not completely prime ideal, since $M_n(\mathbb Z)/pM_n(\mathbb Z) \cong M_n(\mathbb Z/p \mathbb Z)$ is a prime ring which is not a domain.

About $R/I$ Where $I$ is a Prime Ideal


The normalizer of permutation

Question:
Let $\sigma = (1 2 \dots 9) \in S_{10}$.
a) Calculate the size of the normalizer $N_{S_{10}}(<\sigma >)$.
b) Describe exactly the elements in  $N_{S_{10}}(<\sigma >)$.

I am not sure how to approach this. I understand that we look for permutations that fixes $10$, and yet I can't see what to do further...
I know that $\tau \sigma \tau^{-1} =(\tau(1) \dots \tau(9))$ by definition, and also that for $\tau$ to be in  $N_{S_{10}}(<\sigma >)$ than it is required that $\tau \sigma \tau^{-1} =(\tau(1) \dots \tau(9)) = \sigma^i$ for some $i$.
How can I continue from here?
Comment: Let $g \in N = N_{S_{10}}(\langle \sigma \rangle)$. Since $\sigma \in N$, by multiplying $g$ by a power of $\sigma$ you can assume $g(1)=1$. Now $g^i(1)=i+1$, so $g\sigma g^{-1} = g^i \Leftrightarrow g(2)=i+1$, so $g(2)=2,3,5,6,8$ or $9$.
Comment: @Derek Holt It is really difficult for me to follow all of the assumptions you made... Could you please elaborate?
Answer: I'll try to continue from where you stopped...
It has to be $o(g\sigma g^{-1})=o(\sigma^i)=9$ so $gcd(i,9)=1\Rightarrow i=1,2,4,5,7,8$
The $g\in S_{10}$ with the above property (for fixed $i$) form a coset of $C_{S_{10}}(<\sigma>)=<\sigma>$ and the number of such cosets is $6$ (because we have $6$ $i$'s)
For example for the $g\in S_{10}$ with $g\sigma g^{-1}=\sigma^2$ we see that all $t\in S_{10}:t\sigma t^{-1}=\sigma^2$ are $gC(<\sigma>)=\{t: t=gu \text{ with } u\sigma u^{-1}=\sigma\}$
And since $|<\sigma >|=9$, $|N_{S_{10}}(<\sigma>)|=9\cdot 6=54$
For (b) the elements of $N_{S_{10}}(<\sigma>)$ are these of the form $g\sigma^l$ with $\sigma^l\in<\sigma> $ and $g\in S_{10}$ with the property described in the first line.
I hope it is correct!
Comment: @ChikChak I edited my answer
Comment: @ChikChak I think that (b) is easy to see from my 3rd line
Comment: @ChikChak I edited my answer again
Comment: Could you explain more the second paragraph?
Comment: Cool! I think I got it. What about b)?
Comment: Oh it is the powers (within the numbers you wrote) of $\sigma$, right?

The normalizer of permutation


What's the difference between .post() , .create() and perform_create() in views.py and .create() in serializers.py

Question: I'm confused with .post() and .create() and perform_create() in views.py and .create() in serializers.py
seems like .create() in serializers.py get the validated_data, so it can not check serializer.is_valid
Here is the serializers.py:
class PostListSerializer(ModelSerializer):
    class Meta:
        model = Post
        fields = [
            'title',
            'content',
            'publish',
        ]

    def create(self, validated_data):
        print("-------------------")
        print("5555555 - create", validated_data)
        return Post.objects.create(**validated_data)

And if my code is :
views.py
class TestPostListAPIView(ListCreateAPIView):
    queryset = Post.objects.all()
    serializer_class = PostListSerializer

    def post(self, request, *args, **kwargs):
        serializer = PostListSerializer(data=request.data)
        if not serializer.is_valid(raise_exception=False):
            # deal with serializer.errors
            return Response({'[post]custome error 1 !!!!!!!!!'}, status=status.HTTP_400_BAD_REQUEST)
        else:
            print("[post]- serializer.validated_data ", serializer.validated_data)
            serializer.save()
        return Response({'[post]Create success'}, status=status.HTTP_201_CREATED)

the process will go .post() in views.py --> .create() in serializers.py  --> Response to client
If my views.py use:
class TestPostListAPIView(ListCreateAPIView):
    queryset = Post.objects.all()
    serializer_class = PostListSerializer

    # def post(self, request, *args, **kwargs):
    #     serializer = PostListSerializer(data=request.data)
    #     if not serializer.is_valid(raise_exception=False):
    #         # deal with serializer.errors
    #         return Response({'[post]custome error 1 !!!!!!!!!'}, status=status.HTTP_400_BAD_REQUEST)
    #     else:
    #         print("[post]- serializer.validated_data ", serializer.validated_data)
    #         serializer.save()
    #     return Response({'[post]Create success'}, status=status.HTTP_201_CREATED)

    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        print("44444-1 perform_create ", serializer)
        if not serializer.is_valid(raise_exception=False):
            print("44444-3 serializer.is_valid() ", serializer.is_valid())
            return Response({'custome error!!!!!!!!!'}, status=status.HTTP_400_BAD_REQUEST)
        else:
            print("44444-7 serializer.validated_data ", serializer.validated_data)
        self.perform_create(serializer)
        return Response({ 'Create success'}, status=status.HTTP_201_CREATED)

    def perform_create(self, serializer):
        print("1111-1 perform_create ", serializer)
        if not serializer.is_valid(raise_exception=False):
            print("1111-3 serializer.is_valid() ", serializer.is_valid())
            return Response({'custome error!!!!!!!!!'}, status=status.HTTP_400_BAD_REQUEST)
        else:
            print("1111-7 serializer.validated_data ", serializer.validated_data)
        user = User.objects.filter(id=8)
        serializer.save(user=user.first())
        print("1111-8 perform_create ")

the process is .create() in views.py --> .perform_create() in views.py --> .create() in serializers.py --> Response to client
Both can work!
And I found if I post invalid data, the process will not go to .perform_create()  part even I comment the .create() part, so seems like .perform_create() can not check
serializer.is_valid(),neither
So What's the difference between .post() and .create() in views.py  , should I just use .post() or should I use .create() plus .perform_create()
I want to know because I have to use custome response message like {'custome error!!!!!!!!!'} ,
but I'm confused with their effect, where should I put the logic
Answer: From the docs about perform_create hook and others:

These hooks are particularly useful for setting attributes that are
  implicit in the request, but are not part of the request data. For
  instance, you might set an attribute on the object based on the
  request user, or based on a URL keyword argument.


Configuring Bind dns with webmin

Question: I am new to VPS. I have recently bought a new VPS and trying to set up Bind DNS server on it using webmin.
My VPS provider has provided me with three IPS.
<code>Main IP: 50.*.*.10
Additional IP 1: 50.*.*.11
Additional IP 2: 50.*.*.12
</code>
As I understand I need to setup a Master Zone and a Salve Zone on my VPS and then update the two namespace records on my domain providers site.
My question is,
Do I need to setup both Master and Slave Zones on my VPS (I only have one server)? or can I just create a master zone with two NS records (ns1 and ns2) using additional IPs and just use them to update domain settings on my domain provider?
Also, are there any good tutorials which I can use for this? I've googled a lot but failed to find a detailed tutorial, all I have found till now are few screenshots and short descriptions which don't make sense.
Comment: [Administration panels are off topic](http://serverfault.com/help/on-topic). [Even the presence of an administration panel on a system,](http://meta.serverfault.com/q/6538/118258) because they [take over the systems in strange and non-standard ways, making it difficult or even impossible for actual system administrators to manage the servers normally](http://meta.serverfault.com/a/3924/118258), and tend to indicate low-quality questions from *users* with insufficient knowledge for this site.
Answer: All you need is to create the master zones and use your domain registrar to change the NS entries to your server.
You create a master zone for the domain, not the IPs. The only time you need this is for rDNS entries. The slave zone is when the daemon is simply acting as a failover for another DNS server.
Comment: Thanks, makes sense....I'll try this and see if it works.
Comment: What can I do if my DNS server fails? CAn I just forward queries to my ISP's DNS or Google public DNS?
Comment: You would need to set up a secondary DNS. Afraid.org has a free backup DNS feature that will work. I have this setup for my own personal domain. http://freedns.afraid.org/

Configuring Bind dns with webmin


Why does this map not show up?

Question: Trying to figure out why this doesn't show up, it is supposed to show a map with layers to show the weather. I used a combination of Openlayers and OWM APII've tried this on Google Chrome and Firefox and yet to no avail:

Comment: The map ids don't match.
Comment: While I fixed that, the map wouldn't work if you try to run the document locally, right?
Answer: If you change

to

it will work.
It's because when you create the map initially you set the name to basicMap, and the div id has to match:
map = new OpenLayers.Map("basicMap");

Comment: While I fixed that, the map wouldn't work if you try to run the document locally, right?

Why does this map not show up?


Integrals Involving The Modulus Function.

Question: Let $$I = \int_{-\frac{\pi}{6}}^{\frac{\pi}{6}} \frac{\pi + 4x^5}{1-sin(|x|+\frac{\pi}{6})}$$
I need to compute $I$.
It does look like a standard integral except for the fact that $x$ is in the modulus. Since the limits are of the form $-a$ to $a$, I can replace $x$ with $-x$ in the integral.
The problem arises with the modulus function. Since it is not differentiable wherever the modulus reaches zero, I have to divide the integral by taking two separate cases - one as the function goes from $-a$ to $0$ and another from $0$ to $a$ to get rid of the modulus.
Of course, solving the integral without the modulus is tricky itself, but how do you go about integrals with a modular functions? Is case work a must?
Comment: Yes taking cases is one way to avoid the modulus function. The other case is trying to eliminate the modulus itself by arguing that its input is always positive eg. $$|x^2| = x^2$$
Comment: how would you go about solving the integral though?
Comment: Take cases for values of $x$ where the function goes negative and positive.
Answer: HINT:
Use $I=\int_a^bf(x)\ dx=\int_a^bf(a+b-x)\ dx$
$I+I=\int_a^b\{f(x)+f(a+b-x)\}\ dx$
Then  divide the integral range from $-\pi/6,0;0;\pi/6$
and use $|x|=+x$ if $x\ge0$  and $=-x$ if $x\le0$


HTML5 Transition and Transform effect.

Question: When I click a button, I wanna do some things listed below using HTML5 and CSS3. But I don't know how can I achieve these things at the same time.
When I click a button:
Change element A's CSS3 property -webkit-transform to rotateY(180deg) in 1 second (using -webkit-transition:-webkit-transform 1s;)
Change element A's CSS3 property -webkit-transform to scale(0.8) in 0.5 second (using -webkit-transition:-webkit-transform 0.5s;). Then change element A's CSS3 property -webkit-transform back to scale(1) in 0.5 second (using -webkit-transition:-webkit-transform 0.5s;).
Are there any solutions about this issue?
Thank you!
Answer: You'd have to use animation rather than transitions:
@-webkit-keyframes myAnimation{
    from {
        -webkit-transform: rotate(0deg) scale(1);
    }
    50% {
        -webkit-transform: rotate(90deg) scale(0.8);
    }
    to {
        -webkit-transform: rotate(180deg) scale(1);
    }
}

div {
    -webkit-animation-duration: 1s;
}
div:hover {
    -webkit-animation-name: myAnimation;
    -webkit-transform: rotate(180deg) scale(1);
}

Here's a demo.

HTML5 Transition and Transform effect.


Drupal website goes offline every 2 days for 15 minutes

Question: We provide hosting services (Dedicated Server) to a client whose website is based on drupal. This month, his website goes offline for 15 minutes every 2 days.
We already have monitoring tools (i.e pinguzo) showing that the server never went down. And the log files show nothing that is related to this issue. Also, there is no crontab entry executing every 2 days.
And we already checked the website many times at the same hour, but we never encountered the website while offline.
Nevertheless, the client still sends reports about this issue, and we do not know what could be the culprit.
Where should I look? Is there anything in drupal that can cause this?
Thank you.
Answer: Drupal can be configured to use a "poor man's cron" (exectuting jobs after a normal visitor's HTTP request). If you have access to the Drupal Admin UI, check Admin > Reports > Recent log messages, this is a core module helpful for debugging (it might be disabled though)
Comment: Thank you so much, I do not have access to the admin UI, but I will ask the client to send me the log messages.  Thanks again +1 ,
Comment: If you have access to the DB, you could look for a table called "watchdog", AFAIK this is the data source for the module
Comment: Thank you so much, I contacted the client and told asked him to check the reports, best !


Is it possible to run two services on the same ports with different IP but on the same machine?

Question: My question is simple.
Is it possible to run TWO different game servers using exact same port but on different IP's using same network interface on the same server ?
Does each IP adress have it's own port pool or is it limited to machine's network interface ?
Comment: Irrelated, but blocking your site completely to IE users is not particularly fair. If someone want to quickly view the files they shoudln't be *forced* to download another browser or do some trickery.
Answer: It is possible. You just have to bind on the right IP address/interface each service using the same port.
Ports (be them UDP or TCP) have their own pool per IP address.
You can listen on the same port if you change: IP address or protocol (UDP or TCP).
See: http://www.bleepingcomputer.com/tutorials/tcp-and-udp-ports-explained/
Answer: Yes.
Sockets (IP:Port) come in pairs and have to be unique.
You can also bind multiple IPs to a single NIC.
Answer: This is easy only if the application supports binding limitations. If it does not then you can put the app into an LXC container if we are talking about Linux or Java (or something running well under WINE). If Windows does not offer something similar then you can use VMs.
Answer: Yes, that's an entirely valid way to do it. The IP:port combo has to be unique, but you can certainly have port 80 on 184.108.40.206 and on 188.8.131.52.
Answer: Via HAproxy you can do it:
https://www.linuxbabe.com/linux-server/ocserv-vpn-server-apache-nginx-haproxy
Best wishes for You,
Kamran
Comment: While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference.  Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/536260)