Create custom variables for a template file inside a menu callback function

Question: here is a good one that has always stumped me.
I would love to see a solution in both D6 and D7.
I create custom modules and usually work with the menu api to create menu type -> MENU_CALLBACK.
Here is an example:
function example_menu() {
  $items['example/page1'] = array(
    'page callback'     => 'example_page1_callback',
    'access callback'   => TRUE,
    'type'              => MENU_CALLBACK,
  );
  return $items;
}

I would like to see the exact code solution to provide three different variables to be printed in a custom theme template based on the following code.  To add to that challenge, I would like the code to be written in the menu callback function only.  I know there are many possible ways to do it but could never figure out how to do it in a menu callback function which would be the most useful way.
//****************************************
// This code block goes in a custom module called example_menu_variables.module

// hook_menu example
function example_menu() {
  $items['example/page1'] = array(
    'page callback'     => 'example_page1_callback',
    'access callback'   => TRUE,
    'type'              => MENU_CALLBACK,
  );
  return $items;
}

// hook_meny callback function
function example_page1_callback() {
  // please provide code within this function to turn these next three
  // variables into separate output variables for the template.
  $variable_to_print1 = 'Foo';
  $variable_to_print2 = 'Bar';
  $variable_to_print3 = array('bing', 'bang', 'bong');
  return;
}
//****************************************

//****************************************
// This block of code represents the part of the custom theme template related to the menu location from the hook_menu call
// D6 template name would be page-example-page1.tpl.php
// D7 template name would be page--example--page1.tpl.php

<?php print  $variable_to_print1?>

<?php print  $variable_to_print2?>

<?php print  $variable_to_print3[0] . ' ' . $variable_to_print3[1] . ' ' . $variable_to_print3[2]?>

/*
output should look like:
Foo
Bar
bing bang bong
*/

//****************************************

Hope this is the kind of problem that someone wouldn't mind solving.  Thanks for your time.  :)
Ray James
Comment: If I understand you correctly, that is the purpose of [`hook_theme()`](http://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_theme/7). Why does the code need to be contained within the menu callback function?
Comment: Hi Adam and thanks for the response.  I guess it would be great to see what the code would look like both ways.  Using hook_theme() requires a ton of setup just to get one custom variable into the theme layer.  I would like a simple solution that can be done in the same place that you would be gathering your data from the database to display as output.  It would be really great to have a function that just sends that variable like this -> drupal_add_template_variable($var, $varname); or something like that.  I am not sure if such a thing exists in Drupal.  Thanks.
Answer: I don't believe you can do this without using hook_theme, but this is exactly what it's for and it's quite simple. (This code is for Drupal 6.)
/**
 * Implements hook_menu().
 */
function example_menu() {
  $items['example/page1'] = array(
    'page callback'     => 'example_page1_callback',
    'access callback'   => TRUE,
    'type'              => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Page callback function
 */
function example_page1_callback() {
  $variable_to_print1 = 'Foo';
  $variable_to_print2 = 'Bar';
  return theme('example_contents', $variable_to_print1, $variable_to_print2);
}

/**
 * Implements hook_theme().
 */
function example_theme() {
  return array(
    'example_contents' => array(
      'arguments' => array('variable_to_print1' => null, 'variable_to_print2' => null),
      'template' => 'example-contents',
    ),
  );
}

$variable_to_print1 and $variable_to_print2 are now available in example-contents.tpl.php.
Comment: @rayjamesfun With the best will in the world, you're heading in entirely the wrong direction at the moment. Drupal has a templating system, if you want to take advantage of that you need to follow its rules, which Adam has outlined perfectly. If you don't, then you'll need to resort to plain ol' PHP which we can't get into here; we offer Drupal solutions here, not solutions to get _around_ Drupal :)
Comment: Hi Adam, thanks for the code.  I put it together and ran it but got a ton of errors stating that all the other standard variables are undeclared -> Notice: Undefined variable: logo in include() (line 92 of C:\xampp\htdocs\d7themeaddvar\site\themes\bartik\templates\example-contents.tpl.php) I put the files in a git repository for easy download -> https://github.com/rayjames/d7themeaddvar
Comment: `content-example.tpl.php` does not replace `page.tpl.php`. Only the defined variables will be available in it (in this case `$variable_to_print1` and `$variable_to_print1`). If you want to make a new variable available to page.tpl you should look into `hook_preprocess()`.
Comment: Hmm, we seem to be going in a different direction here.  I'm not sure what your code does anymore.  As per the initial problem, I asked for code to make variables for print in a custom template, but I guess I need to be more specific because I assumed everyone would understand that the hook_menu item would point to a "page" on the site and that there would be a custom template for said "page" that the variables would be available for.  I can't figure out how to get your code to print the variables.  Thanks.
Comment: Hi Clive.  I hear ya, and I totally agree.  I'm in now way trying to go around Drupal, just looking for an easier solution that I already thought was there.  I can't get Adam's code to work.  I am no newb to Drupal.  I have built some extremely complex sites but still can't seem to figure out how to wield the theme system like a ninja.  Just looking for a complete example solution at this point in the way of the Drupal.  Thanks.
Comment: The code in the answer is for Drupal 6, any chance you're trying it in a D7 site?
Comment: Yeah, he is linking to hook_theme for D7 so I assumed his code was also for D7.  I'll try it with D6 and get back to you guys.  Thanks.
Answer: The code written by Adam is correct (my +1) Your variables are available in example-contents.tpl.php file - it is a template file of your module output.
However, if you need these variables in page.tpl file, then you can just create them:
in your module file:
/**
 * Page variables
 *
 * @return
 *  custom page variables
 */
function custom_variables(){

  $variable_to_print1 = 'Foo';
  $variable_to_print2 = 'Bar';
  $variable_to_print3 = array('bing', 'bang', 'bong');

  return array( 'variable_to_print1'    => $variable_to_print1,
                'variable_to_print2'    => $variable_to_print2,
                'variable_to_print3'    => $variable_to_print3
  );
}

in your template.php file:
/**
 * Override or insert variables into the page templates.
 *
 * @param $variables
 *   An array of variables to pass to the theme template.
 * @param $hook
 *   The name of the template being rendered ("page" in this case.)
 */
function YOURTHEME_preprocess_page(&$variables, $hook) {

  // pass your module variables to page--example1.tpl.php file
  if (in_array('page__example1', $variables['theme_hook_suggestions'])){
    if (module_exists('your_module')){
        $your_module_variables = custom_variables();
        $variables['variable_to_print1'] = $your_module_variables['variable_to_print1'];
        $variables['variable_to_print2'] = $your_module_variables['variable_to_print2'];
        $variables['variable_to_print3'] = $your_module_variables['variable_to_print3'];
    }
  }
}

Comment: All above mentioned examples works perfect. My answer concerning a D7 solution. It doesn't matter if your page has dynamic arguments or not. Your variables will be available on every "example1" page ("example1" it's a content type) If you need them on every content type page, then just remove the first IF statement in preprocess_page from my example. You can create your variables anywhere (in your module for example) but then, they have to pass through YOURTHEME_preprocess_page function, because it's the place where your variables are defined as a page variables.
Comment: Hi Jack, is this a D6 or D7 solution?  I was wondering if you could link to the tutorial or Drupal page that might explain what is going on in your code better too.  I am assuming that you do not know of a solution that can be implemented within the menu callback, correct?  I am also assuming that you would/could write a different function for each "page" so as to keep the variables organized.  This doesn't look like it would work with a page that has dynamic page arguments either.  Thanks.
Answer: I got this working in Drupal 7, simply by setting the variable in the render array returned from the page callback.
In .module file
function example_page1_callback() {
$render_array['variable_to_print1'] = 'Foo';
$render_array['variable_to_print2'] = 'Bar';

// set other render array items for the page

return $render_array;
}

In page.tpl.php file

  1st variable = <?php print render($page['content']['system_main']['variable_to_print1']); ?>

  2nd variable = <?php print render($page['content']['system_main']['variable_to_print2']); ?>

Comment: My god man you did it!  You figured it out!  You totally rock. I have one edit though.  It's system_main not system-main for Drupal 7 at least.  Thank you so much.
Answer: Had same problem, this is what worked for me on Drupal 7.x
function example_menu() {
  $items['example/page1'] = array(
  'page callback'     => 'example_page1_callback',
  'access callback'   => TRUE,
  'type'              => MENU_CALLBACK,
  );
return $items;
}

function example_page1_callback() {
  $variable_to_print1 = 'Foo';
  $variable_to_print2 = 'Bar';
  $variable_to_print3 = array('bing', 'bang', 'bong');

  //use below given statement when you only want to pass one variable
  // to your template
  //return theme('example',array('output'=>$variable_to_print1));

  //use this statement when you want to pass array
  return theme('example',array('output' => $variable_to_print3));
}

In addition to all this you need a example_theme() hook
function example_theme(){
    //lets say your template file is called example.tpl.php and it resides
    //within your module directory.
    $custom_example_template = array();

    $custom_example_template['example'] = array(
        'template' => 'example',
        'variables' => array('output' => NULL)
    );
}

Now what about your example.tpl.php

    <?php
             //use this code when you have passed only one variable
             print $variables['output'];
    ?>

You can name your variables whatever you want, you don't need to stick with 'output' as I did. For some reason you can't see my foreach loop to loop through the array we have passed.
Comment: I don't believe this will pass the variables to page.tpl.php, i.e. header, footer; only to the content.
Answer: I got Jack's method to work, although I must say the render_array idea is simpler.  Here's what worked for me:
In Module:
//page callback to create the page and include some CSS & Javascript
function my_callback() {
   drupal_add_css(...);
   drupal_add_js(...);
   return '';
}

function _my_module_vars(){
   $var1 = 'foo'
   $var2 = 'bar'
}

In theme's Template.php
function MYTHEME_preprocess_page(&$vars) {
   //add some custom variables to the module page
    if(in_array('page__my_content_type', $vars['theme_hook_suggestions'])) {
       if(module_exists('my_module')) {
          $my_vars = _my_module_vars();
          $vars['var1'] = $my_vars['var1'];
          $vars['var2'] = $my_vars['var2'];
    }
  }
}

In page.tpl.php  (or you could use page--my_content_type.tpl.php):
print $var1;
print $var2;

Comment: Just to clarify, the reason I also wanted to do this way (as opposed to hook_theme in the module method) is that here, the variables are available in the page.tpl.php file.  In the hook_theme way, the module template file is rendered into the page's content variable (if I remember correct), which makes it tough to do much with it.


Getting library not found for -lstdc++.6 while building the Mfp 6.3 Iphone App in Xcode 10.1

Question: Getting this error when running any MFP 6.3 app in Xcode 10.1:

Showing All Issues :-1: library not found for -lstdc++.6
:-1: linker command failed with exit code 1 (use -v to see invocation)

The same is working in Xcode 9.0/Xcode 8.0 version.
How this issue can be particularly solved for IBM Mobilefirst 6.3 iPhone Apps. Also, Apple Store is not accepting build made with Xcode 9.0. Also tried MFP 6.3 latest fix but it is not solving the issue.
Answer: MobileFirst 6.3 doesn't support iOS 12/Xcode 10 - you should upgrade to MobileFirst 7.1 or 8.0.
Having said that, you can try the solution given in this link ( https://mobilefirstplatform.ibmcloud.com/blog/2018/07/23/mfp-support-for-ios12/#known-issues )
Answer: lstdtc++ is deprecated in recent versions. I have faced this issue and it is resolved after following the steps mentioned in https://github.com/Kila2/libstdc-.6.0.9.tbd
Download the Files from Github
1. For simulator to work, copy the files from iPhoneSimulator to path "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/lib/"
2. For actual devices to work, copy the files from iPhoneOS to paths as mentioned in the link w.r.t Xcode version.
I did the same and restart XCode. It works post this.
Comment: Consider editing your post and describing those steps mentioned in link.
Comment: I have added more info.


Determine limit of telescoping series

Question:
Suppose: $f(x + y) = f(x) + f(y)$
The answer should be in the form of $f(a)$
So I've written $f(x + y) = f(\frac{1}{k + 1}) + f(\frac{-1}{k - 1})$
Then I wrote a couple of sequences and saw that terms cancel each other out and I get $f(1) + f(\frac{1}{2}) - f(\frac{1}{N}) - f(\frac{1}{N + 1})$
which with N approaching infinity gives me $f(1) + f(\frac{1}{2}) + f(0) + f(0)$
and since the statement about $f$ , I get the answer as  $f(\frac{3}{2})$
Is this the right way to approach this?
Comment: That looks like the right way to approach the question, assuming that $f(x+y) = f(x) + f(y)$ holds. Also notice, one of your partial fractions should have $k-1$ in the denominator.
Comment: @Joppy Oh yeah that's right
Comment: Are you aware that the only functions that verify $f(x + y) = f(x) + f(y)$ are the linear functions $f(x)=ax$ ? Thus your issue become much simpler ... it becomes the convergence of the series $2a \sum {1}{k^2-1}$.
Comment: @JeanMarie That's not true unless continuity of $f$ is assumed. But the OP also uses continuity when taking the limit of $f \left( \frac{1}{N} \right)$, so maybe it's meant to assume it?
Comment: @Adayah You are right. This( Cauchy) result assumes continuity, and I had done it implicitly, forgetting to mention it explicitly.
Comment: Not sure I follow, I've been given the first two statements so I assumed this was the best way to get $f(a)$
Comment: sorry : read in my previous answer : "convergence of $2a \sum \dfrac{1}{k^2-1}$'
Comment: @JeanMarie I was supposed to use the result from the previous exercise which was to solve the limit of the same series without the $f()$ which ended up giving me the limit as $\frac{3}{2}$, so I thought I should solve it in the same way. I don't see how I can get a definitive answer for that series, all I see is that it approaches 1
Answer: You solution is correct, but there is one thing left to justify: why does
$$\lim_{N \to \infty} f \left( \frac{1}{N} \right) = 0 \ ?$$
Comment: @JohnDoe Hint: prove that $f \left( \frac{1}{N} \right) = \frac{1}{N} f(1)$.
Comment: isn't it $ = f(0)$?
Comment: @JohnDoe It is, but why?
Comment: I know why but I don't know how to prove it
Comment: could I say that because $f(x+y)=f(x)+f(y)$ then the function has to be linear which also means that it is homogenous?
Comment: @JohnDoe A function such that $f(x+y) = f(x) + f(y)$ does not have to be linear. It has to be linear over $\mathbb{Q}$, i.e. $f(\alpha \cdot x) = \alpha \cdot f(x)$ for all $\alpha \in \mathbb{Q}$. But proving $f \left( \frac{1}{N} \right) = \frac{1}{N} f(1)$ is a special case of it, which is better done directly.

Determine limit of telescoping series


How to translate this code from Intel(nasm) to AT&T(gas) syntax?

Question: gdt64:
  dq 0 ; zero entry
.code: equ $ - gdt64
  ; 0x08 kernel CS 0x8
  ; bits set: descriptor type, present, executable, 64bit
  dq (1<<53) | (1<<47) |                     (1<<44) | (1<<43) ; kernel
  ; 0x10 user CS
  ; works when its set to kernel mode, but when in user mode, it doesnt, duh
  ;dq (1<<53) | (1<<47) |                     (1<<44) | (1<<43) ; kernel
  dq (1<<53) | (1<<47) | (1<<46) | (1<<45) | (1<<44) | (1<<43)

I understand that dq is .long. And I can translate first part of this:
gdt64:
  .long 0 // zero entry
.code = . - gdt64

But How to translate line like this:
  dq (1<<53) | (1<<47) | (1<<46) | (1<<45) | (1<<44) | (1<<43)

Comment: Have you tried `.quad (1<<53) | (1<<47) | (1<<46) | (1<<45) | (1<<44) | (1<<43)`?
Comment: Not an assembly expert, but wouldn't it be just a number with respective bits set?
Comment: Yes I think it is a number with respective bit set.
Answer: First of all, dq in NASM assembles an 8-byte quadword, whereas .long in x86 GAS is a 4-byte doubleword, so that's not what you want.  The correct equivalent is .quad.
Your (1<<53) | ... is just an arithmetic expression using C-like shift and bitwise operators, and dq (1 << 53) | ... assembles a quadword having as its value the result of this expression.  GAS accepts this syntax as well, so you can simply write
.quad (1<<53) | (1<<47) | (1<<46) | (1<<45) | (1<<44) | (1<<43)


Why 'e' in softmax?

Question: I am doing an introduction to ML with tensorflow and I came across softmax activation function. Why is in the softmax formula e? Why not 2? 3? 7?
$$
\text{softmax}(x)_i = \frac{\exp(x_i)}{\sum_j \exp(x_j)}
$$
$$
\begin{eqnarray}
  \sum_j a^L_j & = & \frac{\sum_j e^{z^L_j}}{\sum_k e^{z^L_k}} = 1.
\tag{79}\end{eqnarray}
$$
Tensorflow tutorial
NN book
Comment: How does this slow the computation? In any case you'd be dealing with floating-point numbers...
Comment: Make an experiment and check how much computation time would you save if you used floating point numbers vs integers in here. This would be negligible, especially when you'd use it inside a complicated algorithm like neural networks. [Laziness is a virtue of a programmer](http://threevirtues.com/), don't waste your time for useless optimizations.
Comment: Possible duplicate of [What is the reason why we use natural logarithm (ln) rather than log to base 10 in specifying function in econometrics?](https://stats.stackexchange.com/questions/27682/what-is-the-reason-why-we-use-natural-logarithm-ln-rather-than-log-to-base-10)
Comment: @Tim I think the answer to that question really doesn't get at the heart of the issue here. Usually you're not trying to interpret the softmax variables in the same way as you would with functions in econometrics. I thought it was more that it was easier to calculate the derivatives of softmax.
Comment: @Tim I understand why compounded interest limit yields 'e' but I am unable to transpose the reason for this connection into softmax.
Comment: It seems it could be any arbitrary base and it would give us approximatelly (maybe even precisely?) correct image of what the distribution looks like. And working with irrational numbers such as 'e' surely slows down computation.
Comment: @Tim I assume that there must be difference between raising an integer and a transcendental number to the same power.
Comment: Late comment; This originates from statistical physics, i.e., Boltzmann's probability over states.
Answer: Using a different base is equivalent to scaling your data
Let $\mathbf{z} = \left(\ln a\right) \mathbf{y}$
Now observe that $e^{z_i} = a^{y_i}$ hence:
$$ \frac{e^{z_i}}{\sum_j e^{z_j}} = \frac{a^{y_i}}{\sum_j a^{y_j}}$$
Multiplying vector $\mathbf{y}$ by the natural logarithm of $a$ is equivalent to switching the softmax function to base $a$ instead of base $e$.
You often have a linear model inside the softmax function (eg. $z_i = \mathbf{x}' \mathbf{w}_i$). The $\mathbf{w}$ in $\mathbf{x}' \mathbf{w}$ can scale the data so allowing a different base wouldn't add any explanatory power. If the scaling can change, there's a sense in which different base $a$ are all equivalent models.
So why base $e$?
In exponential settings, $e$ is typically the most aesthetically beautiful, natural base to use: $\frac{d}{dx} e^x = e^x$. A lot of math can look prettier on the page when you use base $e$.
Comment: @SebastianNielsen My point was that $f(x) = \frac{a^x}{a^x + 1} $ and $f(x) = \frac{e^{bx}}{e^{bx} + 1}$ are literally the **same** function for $b = \ln a$. Basic math: $e^{x \ln a} = e^{\ln a^x} = a^x$.
Comment: MatthewGunn I think he wanted to know if rewriting the softmax equation to: $\text{softmax}(x)_i = \frac{\ln(x_i)}{\sum_j \ln(x_j)}$ would yield the same result as $$\text{softmax}(x)_i = \frac{\exp(x_i)}{\sum_j \exp(x_j)}$$. However the statement @Jack made that: "$e^x =ln(x)$" isn't true, so I am not sure where he was going with that
Comment: Would the function be the same if e to the z was replaced with ln(z) ?
Comment: @Jack I'm not sure I follow what you specifically had in mind?
Comment: If e^x = ln(x). Can the softmax function be written using ln(x) instead of e^x?
Comment: You could write the [softmax](https://en.wikipedia.org/wiki/Softmax_function) as $\operatorname{Softmax}(\mathbf{x})_i = \frac{e^{x_i}}{\sum_{j=1}^k e^{x_j}} = \exp\left(\ln\left( \frac{e^{x_i}}{\sum_{j=1}^k e^{x_j}} \right) \right) = \exp\left( x_i - \ln\left( \sum_{j=1}^k e^{x_j} \right)\right)$. But other than that, I really don't know what you're looking for?
Comment: **I have a question**. The only reason we raise the independent variable as an exponent (x, y, z - whatever you want to call it) is so that we can avoid negative values canceling out postive values right? If that is true (please tell me if you know that is the casae), then we can replace $e^x$ with $|x|$ right? $\text{softmax}(x)_i = \frac{|x_i|}{\sum_j |x_j|}$
Comment: @SebastianNielsen No, that's different. Graph $y = \frac{e^x}{e^x + 1}$ and $y = \frac{|x|}{|x| + 1}$ and see they're rather different. The former is strictly increasing in $x$. The latter is decreasing in $x$ for $x < 0$.
Answer: Some math becomes easier with $e$ as a base, that's why. Otherwise, consider this form of softmax: $\frac{e^{ax_i}}{\sum_j e^{ax_j}}$, which is equivalent to $\frac{b^{x_i}}{\sum_j b^{x_j}}$, where $b=e^a$.
Now, consider this function: $\sum_i\frac{e^{ax_i}}{\sum_j e^{ax_j}} x_i$.
You can play with coefficient $a$ making the function less or more soft max.
When $a\to\infty$, it is $\max(x)$ because $\lim_{a\to\infty}\frac{e^{ax_i}}{\sum_j e^{ax_j}}=\mathrm{argmax}(x)$.
When $a=1$ it is $\mathrm{softmax}(x)\cdot x$ - a smoother version of max.
When $a=0$ it is as soft as it gets: a simple average $\frac 1 n \sum_i x_i$
Answer: This is indeed a somewhat arbitrary choice:

The choice of the softmax function seems somehow arbitrary as there are many other possible normalizing functions. It is thus unclear why the log-softmax loss would perform better than other loss alternatives.

An Exploration of Softmax Alternatives Belonging to the Spherical Loss Family

Some potential reasons why this may be preferred over other normalizing functions:

it frames the inputs as log-likelihoods
it is easily differentiable
Answer: $\DeclareMathOperator*{\argmax}{\arg\!\max}$
In the context of classification, we want to predict the most likely class $c^*$ that a feature vector $\mathbf x$ belongs to as $$c^* = \argmax_{i} P(C = c_i \mid \mathbf x)$$ where $c_1,\dots,c_N$ are the $N$ different classes. It is often convenient to re-write $P(C = c_i \mid \mathbf x)$ using Bayes' rule. Note that
\begin{align}
P(C = c_i \mid \mathbf x) &= \frac{p(\mathbf x \mid C = c_i)P(C = c_i)}{p(\mathbf x)} \\
&= \frac{p(\mathbf x \mid C = c_i)P(C = c_i)}{\sum_{j=1}^N p(\mathbf x \mid C = c_j) P(C = c_j)}
\end{align}
For any $a,b \in \mathbb R$, $$a^{\log_a(b)} = b$$ So,
\begin{align}
P(C = c_i \mid \mathbf x) &= \frac{p(\mathbf x \mid C = c_i)P(C = c_i)}{\sum_{j=1}^N p(\mathbf x \mid C = c_j) P(C = c_j)} \\
&= \frac{a^{\log_a\left(p(\mathbf x \mid C = c_i)P(C = c_i)\right)}}{\sum_{j=1}^N a^{\log_a\left(p(\mathbf x \mid C = c_j) P(C = c_j)\right)}}
\end{align}
Letting $a = e$ such that
\begin{align}
z_i &= \ln\left(p(\mathbf x \mid C = c_i)P(C = c_i)\right) \\
z_j &= \ln\left(p(\mathbf x \mid C = c_j)P(C = c_j)\right)
\end{align}
and $$\mathbf z = \begin{bmatrix} z_1 \\ \vdots \\ z_N\end{bmatrix}$$ we get
\begin{align}
P(C = c_i \mid \mathbf x) &= \frac{\exp\left(\ln\left(p(\mathbf x \mid C = c_i)P(C = c_i)\right)\right)}{\sum_{j=1}^N \exp\left(\ln\left(p(\mathbf x \mid C = c_j) P(C = c_j)\right)\right)} \\
&= \frac{\exp\left(z_i\right)}{\sum_{j=1}^N \exp\left(z_j\right)} \\
&= \text{softmax}(\mathbf z)_i
\end{align}
However, we could have chosen $a$ to be any other real number other than $e$. So, there isn't really anything special about $e$ except its nice properties.

Why 'e' in softmax?


Derivative of the inverse of a composition of functions

Question: I have the following question here.

Let $F$ be a one-one-function such that $F=f \circ g$. If $g(1)=2$,$g'(1)=3$,$f(2)=3$,$f'(2)=4$, find $(F^{-1})'(3)$.

I'm not really sure how to approach this.
I know that $(F^{-1})'(3)=\frac{1}{F'(F^{-1}(3))}$ but I don't know the value of the inverse of the composition at $3$.
The only other thing I know is that $F'=f'(g(x))g'(x)$ but I'm not even sure if that's helpful here. Any help would be greatly appreciated thanks!
Answer: Since $F(x)=f\circ g(x)$, we have $x=F^{-1}\circ f\circ g(x)$. Differentiating both sides with respect to $x$,
$$1=\left(F^{-1}\right)'(f\circ g(x))f'(g(x))g'(x)$$
and when $x=1$,
$$1=\left(F^{-1}\right)'(f\circ g(1))f'(g(1))g'(1)=\left(F^{-1}\right)'(f(2))f'(2)3=\left(F{-1}\right)'(3)\cdot 4\cdot 3.$$


What's the difference between passport and oauth?

Question: I'm trying to build an authentication service with express.js, but I didn't catch the idea of authentication modules yet.
What's difference between passport and oauth middleware? Are they dependent of each other? Is useless to have the BearerStrategy without an oauth server to generate tokens to the BearerStrategy validate? Am I on the right way?
I've read about oAuth2 and Its authentication flow, but I'm still lost with this uncoupled code.
I'm trying to build the Resourse Owner Password authentication with refresh token for my AngularJS frontend communicating with the backend API, and I'm facing with many combinations of password.js strategies (Basic, Bearer, ClientPassword) with oauth2orize on the other side.
So, I'd like to know a very simple explanation of how authentication works on NodeJS. At really I know that Express is not inventing a new way of how authentication works, but the modules are too unobtrusive that I need to understand the base of how It works to achieve them working together.
Answer: Passport is authentication middleware.
OAuth is authorization middleware.
To understand the difference:

Authentication is the process of ascertaining that somebody really is who he claims to be.
Authorization refers to rules that determine who is allowed to do what. E.g. Bob may be authorized to create and delete databases, while Bobbette is only authorized to read.

In other words. Authentication is your username + password. Authorization is what you're allowed to do.
Passport will allow you to authenticate the user before allowing access to your API. It does not (directly, it's possible) allow to check if a user is allowed to perform an action after authentication.
Check this Wikipedia for more on Authentication vs Authorization.
What OAuth does that Passport doesn't, is that it allows users to grant a service access to their personal information. It also allows users to allow or disallow certain privilages (scopes in OAuth).
Do note that there are a lot of OAuth flavors. The most common is the version with authorization grant types seen when authorizing with Facebook or Google. But there are many others including the Resource Owner Password strategy you mentioned.
Comment: Thanks man! I've never noticed something so trivial like the difference between Authentication and Authorization! You went to the exactly point of my doubt! Thanks again! ;)


Spring Cloud Dataflow UI Not Rendering Whitelisted Properties

Question: I have Spring Cloud DataFlow v1.3.1.RELEASE running locally, and I've created a small sample 'processor' app to illustrate what I see happening.
The Boot application has two @ConfigurationProperties classes:
DemoApplicationProperties:
@ConfigurationProperties
@Validated
public class DemoApplicationProperties {

    /**
     * The first name of the person.
     */
    private String firstName;
    /**
     * The last name of the person.
     */
    private String lastName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}

and DemoApplicationPropertiesTwo:
@ConfigurationProperties
@Validated
public class DemoApplicationPropertiesTwo {

    /**
     * The person's middle name.
     */
    private String middleName;
    /**
     * The date of birth.
     */
    private String birthdate;

    public String getMiddleName() {
        return middleName;
    }

    public void setMiddleName(String middleName) {
        this.middleName = middleName;
    }

    public String getBirthdate() {
        return birthdate;
    }

    public void setBirthdate(String birthdate) {
        this.birthdate = birthdate;
    }

}

I also include a unit test to make sure the BootApplicationConfigurationMetadataResolver is resolving all the whitelisted classes appropriately.
public class WhiteListTests {

    private BootApplicationConfigurationMetadataResolver metadataResolver;

    @Test
    public void testMetadataResolver() {
        metadataResolver = new BootApplicationConfigurationMetadataResolver(this.getClass().getClassLoader());

       Resource app = new FileSystemResource(".\\target\\classes\\");

       List<ConfigurationMetadataProperty> list = metadataResolver.listProperties(app);

       for(ConfigurationMetadataProperty listItem : list) {
           StringBuilder sb = new StringBuilder();
           sb.append(listItem.getId() + ": " + listItem.getName() + " :: " + listItem.getType());
           System.out.println(sb.toString());
       }
    }
}

The output of the unit test is as expected:
birthdate: birthdate :: java.lang.String
middle-name: middle-name :: java.lang.String
first-name: first-name :: java.lang.String
last-name: last-name :: java.lang.String

However, when I register the Boot application as a 'processor' in Spring Cloud Dataflow, and inspect the registered application, the UI only partially renders the discovered whitelisted properties:

I have a ZIP file of the project source code, but for whatever reason, cannot figure out how to attach it here.
Comment: I cannot make it working as wll. Could you find a way to do it? I asked my own question here https://stackoverflow.com/questions/59412806/spring-cloud-data-flow-custom-application-properties
Answer: Inside the file spring-configuration-metadata-whitelist.properties did you add the two classes in the property ?
Example
configuration.classes = org.springframework.cloud.stream.app.file.sink.FileSinkProperties

and
com.anotherpackage.MainConfig.java

Comment: yes.  I did include both classes in my whitelist properties file.
Answer: Both the properties class must be declared in the spring-configuration-metadata-whitelist.properties file. Shell, Dashboard, and REST endpoints should then be able to produce the results consistently.
Here's the same example in action.
Comment: Here are the contents of my `sping-configuration-metadata-whitelist.properties` file:

`configuration-properties.classes=com.example.demo.DemoApplicationProperties,com.example.demo.DemoApplicationPropertiesTwo`
Comment: Looks good. It should be able to work as-is. Is the file under `resources/META-INF` folder? If yes, I'd up for reviewing the project if you can share it with dropbox or elsewhere.
Comment: Also, please check the latest commit [here](https://github.com/sabbyanandan/sandbox/tree/master/channing). If there's >1 property class, you'd have to make sure they have different prefixes. O'wise, the last property class wins.


Porque o first-child está selecionando o elemento ?

Question:

<!doctype html>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

Este é um parágrafo.

Este é um parágrafo.

Este é um parágrafo.

No exemplo acima o first-child está selecionando e aplicando uma borda no elemento  e no elemento
 porque isso está acontecendo não era para apenas o elemento body receber a borda já que ele é o primeiro elemento filho do documento?
Notem: no stackoverflow não funciona tem que ser em um documento separado.
Answer: Você precisará identificar a qual elemento o first-child será aplicado.
Nesse caso, como você não especificou, ele aplicou ao body.
Tente o seguinte código:
p:first-child {
        border: 1px solid red;
    }

Nesse caso, será aplicado a borda, ao seu primeiro parágrafo.

Caso queira que sua borda seja aplicada ao body, experimente utilizar
body{
  border: 1px solid red;
}

Comment: @joãobatista eu acabei de testar aqui, e na realidade o acontece com o seu código é que por não estar especificado uma tag, ele irá aplicar a todos os primeiros filhos.

https://developer.mozilla.org/pt-BR/docs/Web/CSS/:first-child
Comment: Mas porque isso ocorre do `first-child` aplicar a borda no elemento `` e ``?
Comment: Verdade! era isso mesmo, valeu!


ckeditor installation

Question: I went to the ckeditor website made a custom build of an ckeditor with the plugins I want, then downloaded and extracted it to
sites/all/libraries/ckeditor

(I previously installed the libraries and ckeditor modules)
then I went to the global configurations settings for ckeditor
admin/config/content/ckeditor/editg
For the "> Path to CKEditor * " value I entered
%l/ckeditor

which sets the location of ckeditor to
/sites/all/libraries/ckeditor

and I set the " Path to the CKEditor plugins directory " value to
%l/ckeditor/plugins

which sets the location of the plugins to
/sites/all/libraries/ckeditor/plugins

I save the configuration and flush all caches.  But whenever I go to edit a piece of content there is NO editor and NO body field at all on the page.
I can re-enter the default url used to install ckeditor and the editor will appear on the page, but of course, without my plugins. I have no error messages.  Anyone have any idea why ckeditor can't find itself?
thanks
Answer: Just a quick answer to my question for other drupal noobies making the same error as I have.  There seems to be a bit debate between installing stand alone ckeditor or installing ckeditor via durpal's wysiwyg module (convenient for installing multiple wysiwyg editors, if need that level of choice).  For more info read this.
At the risk of drawing wrath, with drupal, "more modules" = "more chances for errors and conflicts" so I am go with the stand alone ckeditor install.
First install the ckeditor drupal module, from the drupal project page.
Then, download the BASIC ckeditor script library from ckeditor.com .
NOTE: DO NOT ATTEMPT TO ADD PLUGINS AT THIS POINT, JUST DOWNLOAD THE PLAIN, VANILLA, CLASSIC DOWNLOAD. This is what caused the error above, some plugins do NOT "play nicely" with drupal.
After you have the download of the plain ckeditor script library downloaded, place into the  /sites/all/libraries/ directory, so that you end up with /sites/all/libraries/ckeditor/ckeditor.js  file structure.
Then in your drupal site go to /admin/config/content/ckeditor  and edit the "Global Settings" , so that the "Path to ckeditor" is  %l/ckeditor (sites/all/libraries/ckeditor) and the "Path to plugins" is %m/plugins (sites/all/modules/plugins)
NOTE: By default ckeditor points to a web address (CDN) for the "path to editor" value, which is mighty inconvenient if you work offline on a local dev server without internet from time to time.
Save Configurations
Assuming your install was successful, meaning that you did not have any red text drupal messages at /admin/config/content/ckeditor page complaining about missing libraries, you can go to /node/add/page and see if your locally installed ckeditor is working properly. If it loads, you are good to go.
Now for the fun part, adding all those plugins that made your mouth water, but you couldn't add before you downloaded the ckeditor script library.
Go to ckeditor.com make a list of all the plugins that you desire for your local, stand alone editor, then download each plugin individually.
Extract each plugin that you downloaded to your sites/all/modules/ckeditor/plugins folder.
NOTE:  DO NOT extract the plugins folder located in the drupal sites/all/libraries/ckeditor/plugins folder... all your neat plugins and hours of work will be wiped out when you update ckeditor in the future.
After all your plugins are extracted, edit your "ckeditor.config.js" file located at sites/all/modules/ckeditor.
look for "config.extraPlugins = '';" and enter the name of your plugin.  For more than plugin, separate each plugin with a " , " .
For more details on plugin installation READ THE DOCUMENTATION HERE.
I highly recommend, adding plugins one at a time and testing in your browser, so that you can easily remove the mean plugin that won't play nice with others.
At this point go back to /admin/config/content/ckeditor and edit INPUT FORMAT that you want to use with ckeditor.  Look under "Editor Appearance" and scroll down, your added plugins should appear as checkbox options. Select the ones you want...then get back to "drupaling" :-)
Answer: Great tutorial!
My only addition would be: After you enabled the module and set the global settings as described, you have to turn on the CKEditor for the Advanced Text Format configuration here: /admin/config/content/ckeditor/edit/Advanced (under Basic Setup)

ckeditor installation


MCollective cannot load the Stomp library?

Question: CentOS 5.5
I'm facing with this problem when restarting mcollective:
# service mcollective restart
Shutting down mcollective:
Starting mcollective: /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require': no such file to load -- stomp (LoadError)
    from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
    from /usr/lib/ruby/site_ruby/1.8/mcollective.rb:2
    from /usr/sbin/mcollectived:3:in `require'
    from /usr/sbin/mcollectived:3
                                                           [FAILED]

The STOMP protocol Ruby library is already installed:
# gem list stomp

*** LOCAL GEMS ***

stomp (1.2.2, 1.1.8)

I'm running ruby 1.8.7 with belows RubyGems environment:
# ruby -v
ruby 1.8.7 (2008-05-31 patchlevel 0) [x86_64-linux]
# gem env
RubyGems Environment:
  - RUBYGEMS VERSION: 1.3.7
  - RUBY VERSION: 1.8.7 (2008-05-31 patchlevel 0) [x86_64-linux]
  - INSTALLATION DIRECTORY: /usr/local/lib/ruby/gems/1.8
  - RUBY EXECUTABLE: /usr/local/bin/ruby
  - EXECUTABLE DIRECTORY: /usr/local/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-linux
  - GEM PATHS:
     - /usr/local/lib/ruby/gems/1.8
     - /root/.gem/ruby/1.8
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - http://rubygems.org/

As @R.I.Pienaar pointed out, I'm mixing up 2 Ruby versions: one was installed by yum and another was compiled from source.
I've uninstalled all binary ruby-* packages and the question should change to:
How to make MCollective find out the Ruby which was compiled from source (into /usr/local/bin)?:
# cd /usr/local/src/marionette-collective
# rake rpm
...
Building RPM for mcollective-2.0.0-1
================
which: no rpmbuild-md5 in (/usr/lib64/qt-3.3/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/bin:/opt/redis/bin)
error: Failed build dependencies:
    ruby is needed by mcollective-2.0.0-1.el5.noarch
    ruby(abi) = 1.8 is needed by mcollective-2.0.0-1.el5.noarch
rake aborted!
Failed: rpmbuild -D 'version 2.0.0' -D 'rpm_release 1' -D 'dist .el5' -D 'use_lsb 0' -ba /usr/src/redhat/SPECS/mcollective.spec
/usr/local/src/marionette-collective/Rakefile:35:in `safe_system'
(See full trace by running task with --trace)

or how to compile MCollective from source?
Answer: Looks like you have 2 rubies or some missconfiguration on your system,
mcollective is using the system ruby which is in /usr/lib/ruby/*
Starting mcollective: /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require': no such file to load -- stomp (LoadError)

While your gems are installing into /usr/local/*
  - INSTALLATION DIRECTORY: /usr/local/lib/ruby/gems/1.8

Comment: Thanks. How can I make the MCollective found out the Ruby which was compiled from source (into `/usr/local/bin`)? Updated my question.
Comment: Incidentally, R.I. Pienaar wrote mcollective. :)
Answer: You are trying to building a RPM packages off from a .spec file that requires that the ruby package be installed. Since you compiled ruby from scratch, that will be a problem.
So you solution is to create or find a spec file for ruby and build a ruby binary package off based off that spec. Here is mine, for example (which I forked from 1.9.2):
https://github.com/rilindo/ruby-1.9.3-rpm
Once the binary package is built, reinstall ruby with that package, remove/disable the current ruby install that you compiled manually, then build mcollective package from the .spec file.
Comment: Thanks. I installed Ruby 1.8.7 binary package from the [RBEL](http://rbel.frameos.org/) repo and now it works fine.


1st program, return true if List 1 has more elements than List 2

Question: This is my first program in prolog, I've been reading about it and just don't seem to grasp a couple core concepts(I think).  I'm trying to write a functor that will take two lists and return true only if the first list has more elements.  I've gotten a few simple programs to work, but I have hit a roadblock here.  I'm trying to call size inside of isLonger and set temp variables to the return of size.  This seems like a bad(and incorrect) way of going about this in prolog.  I'm getting a:
ERROR: >/2: Arguments are not sufficiently instantiated
% List 1

a([cat, dog, horse]).
b([1, 2, 3, 4]).
c([x, [a, b], y, z]).
c([red, yellow, green, blue]).

% isLonger function
isLonger([],[]).
isLonger(L1,L2) :-  A = size(L1,N), B = size(L2,N), A > B.

 % size([],N).
size([_|T],N) :- size(T,N1), N is N1+1.

Input:
isLonger([x,y,z], [7,8,9,10]).
Comment: In prolog there are predicates, not functions. You can't get the size by doing `A = size(L1,N)`. That expression attempts to instantiate `A` to match `size(L1,N)`. It doesn't execute `size` as a function and return a value. The `size` predicate never instantiates `N`, so the comparison `>` occurs with uninstantiated variables, which gives the error. Also, there's a built-in predicate `length` you can use for this purpose.
Answer: A list L1 is longer than L2 if L1 is not empty and L2 is empty; or if the tail of L1 is longer than the tail of L2.
longer([_|_], []).
longer([_|T1], [_|T2]) :- longer(T1, T2).

Unfortunately, that solution would leave lost of choice points behind on several Prologs which can index predicates on the first argument only. That can be solved just reversing the argument order:
longer(L1, L2) :- shorter(L2, L1).
shorter([], [_|_]).
shorter([_|T1], [_|T2]) :- shorter(T1, T2)


Select dataframe columns based on conditions

Question: Input DataFrame :
    a   b  c    d   e    f    g       j   k  l    m    n    o    p    q   r      s  t
2  33  17  0  418  -5  -81  NaN   14336  81  1  462  -24  NaN   81    1   462  -24 NaN
5  33  17  0  415  -5  -116 NaN   14336  81  0  487   -5  116   81    1   462  -24 NaN
7  33  17  0  413  -5  -116 NaN   14336  81  1  462  -24  NaN   81    1   462  -24 -111

Check the value of column c if it's equal to 0: insert the values of d e f into x1 x2 x3
Check the value of column l if it's equal to 0: insert the values of m n o into y1 y2 y3
if he value of column l is equal to 1 :
check columns o and t : the first that contains a value, insert m n o, or r s t into columns z1 z2 z3
Output DataFrame:
    x1  x2    x3    y1    y2    y3    z1   z2    z3
2  418  -5   -81   NaN   NaN   NaN   462  -24   NaN
5  415  -5  -116   487    -5   116   462  -24   NaN
7  413  -5   -81   NaN   NaN   NaN   462  -24  -111

Comment: Can you explain more `the first that contains a value` ?
Comment: Is there a particular part of the problem where you got stuck?
Comment: How working my solution? It is what you want?
Answer: I believe need where for filter by condition with concat for join all DataFrames together and combine_first for replace NaNs by another DataFrame:
df1 = df[['d','e','f']].where(df['c'].eq(0))
df1.columns = ['x1','x2','x3']

df2 = df[['m','n','o']].where(df['l'].eq(0))
df2.columns = ['y1','y2','y3']

df31 = df[['m','n','o']].where(df['l'].eq(1) & df['o'].notnull())
df31.columns = ['z1','z2','z3']

df32 = df[['r','s','t']].where(df['l'].eq(1) & df['t'].notnull())
df32.columns = ['z1','z2','z3']

df = pd.concat([df1, df2, df31.combine_first(df32)], axis=1)
print (df)
    x1  x2   x3     y1   y2     y3     z1    z2     z3
2  418  -5  -81    NaN  NaN    NaN    NaN   NaN    NaN
5  415  -5 -116  487.0 -5.0  116.0    NaN   NaN    NaN
7  413  -5 -116    NaN  NaN    NaN  462.0 -24.0 -111.0

Comment: the answer differs from the desired output as the question is not clear.


Wordpress query with AND returns data as OR

Question: I need to build WP_Query that will return data from database where both conditions are fulfilled, because of that I'm using AND as relation but returned data are different than I expected. To make it more clear I will post WP_Query arguments here.
Array(
[post_type] => Array
    (
        [0] => event
    )

[post_status] => publish
[paged] => 1
[posts_per_page] => 1000
[tax_query] => Array
    (
        [relation] => AND
        [1] => Array
            (
                [taxonomy] => event_dates
                [field] => slug
                [terms] => Array
                    (
                        [0] => thursday
                        [1] => exhibitions
                    )

            )

    )

)

With this arguments I thought that I'll get events with type exibition AND are on Thursday.
Thanks in advance for help.
Answer: The AND is on the wrong level. The WP_Query arguments should look like something like this:
...
'tax_query' => array(
    array(
    'taxonomy' => 'event_dates',
    'field' => 'slug',
    'terms' => array('thursday', 'exhibitions'),
    'operator' => 'AND'
    )
)
...


How to remove "catalog" from Ubercart Catalog View URL

Question: I need to remove the word "catalog" from the URL when viewing a category of products. I am using the stock uc_catalog view. Its Page (Table) display has a URL of "/_catalog". What can I change so that the word catalog will be completely excluded?
For example, I'd like my category URLs to be like so:

mysite.com/shirts
mysite.com/pants

instead of:

mysite.com/catalog/shirts
mysite.com/catalog/pants

Thanks!
Answer: Logical we can't change the way the catalog link generation of Ubercart. The format must be catalog/term_id (with default way) and catalog/term_name (With pathauto module). For me the only thing we can deal with that is creating the URL Aliases manually.


Button to selectively include content in an RSS feed

Question: When creating or editing some content types I want to be able to have the option to add the content to an RSS feed thats generated by Views.
Is this achievable?
Answer: Well, you can just add a field to the content type(s) you are collecting with your view and use that to filter the rss feed. You could easily add a "include in rss" checkbox for example with whatever default value you like.
Comment: Good answer, and doing this with flags is also an option.
Comment: If you want to be a little lazy, you could also re-use the "Promote to front page" checkbox on each node.
Then, over on the view, simply add a filter for "Promote = TRUE"
Either solution is easily achievable. I'd be willing to give you a free and quick hand if you need. Reach me at dougvann.com/contact


Qual é a diferença entre projeto ("project") e aplicação ("app") no Django?

Question: Eu tinha feito alguns testes como framework Django há um tempo atrás, e só agora que voltei a testá-lo novamente, reparei que existem dois comandos, que acabaram me confundindo:
django-admin startproject nome_do_projeto

E também temos:
django-admin startapp nome_do_app

Eu não me lembro de ter precisado usar o comando django-admin startapp das últimas vezes que o utilizei. Isso se trata de alguma opção nova?
Tenho algumas pergunta a fazer:

Qual é a diferença entre um e outro?
Para o Django, o que é um "projeto" e o que é uma "aplicação"?
Existem diferenças de estrutura?
Comment: Tenho a pequena impressão que alguma coisa mudal, pois eu não lembro da época que eu mexia no Django de ter o script `apps.py` dentro  da pasta do projeto...
Answer: Primeiro, vamos analisar a estrutura de diretórios que é gerada quando criamos um projeto:
mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py

Projeto criado com o comando django-admin startproject mysite, a título de informação.

O diretório pai mysite é onde tudo será organizado. O projeto e os aplicativos serão organizados neste diretório. No início, é comum o desenvolvedor acreditar que este é o projeto Django criado, mas não, é apenas um diretório para separar tudo do resto dos arquivos na máquina. Tanto que o nome deste diretório não é importante - só leva o mesmo nome do projeto por padrão.
O diretório mysite interno, por sua vez, é o projeto Django criado. Visto que a definição no glossário consta que o projeto é um pacote Python (um diretório de códigos) que contém todas as configurações da instância do Django em questão. Pode ainda conter configurações de banco de dados, configurações gerais da aplicação, entre outros.

O projeto por si só não faz muita coisa, pois, na filosofia do Django, um projeto deve ser composto de aplicativos (app).
Os aplicativos (app), por sua vez, podem ser definidos como um conjunto de funcionalidades interrelacionadas que são definidas para criar ou manter um aspecto do projeto. Em geral, as apps são definidas para serem o mais reutilizáveis possível, sendo encarregadas de apenas uma função bastante especifica. Também considera-se que as apps serão, ao máximo, independentes entre si - sendo dependentes apenas quando realmente faz sentido.
Por exemplo, se sua aplicação (projeto) necessitará de uma área de Blog, você criará uma app para isso, mas para APENAS isso. Nesta app você definirá todas as estruturas de tabelas do banco de dados necessárias e todas as regras de negócio para que um blog seja funcional.
Mas eu quero criar um sistema de gerenciamento de acesso para usuários no meu blog, ok, mas faça isso em outra app. Não tem um sistema de gerenciamento de usuários? Outra app. Em outras palavras (que são quase as mesmas), as apps são responsáveis poder denifir os aspectos da aplicação/projeto e poderão ser reutilizadas em quantos projetos forem necessárias.
Resumidamente, o projeto é responsável por configurar a instância do Django em questão conforme as necessidades da aplicação e possui um conjunto de apps que definem os aspectos da mesma.

Lembre-se que o Django é um pacote Python instalado no servidor, então é o mesmo código sendo executado pelos diversos sites que podem estar hospedados no mesmo, por isso diz-se sobre a instância do Django.

Ressalto, aqui, que ao meu ver a melhor tradução para o termo app é aplicativo e não aplicação, pois este é normalmente utilizado para referir-se ao conjunto como um todo, enquanto aplicativo expressa uma ideia menos generalista.
Answer: Você  pode ter vários aplicativos dentro de um projeto. Cada App pode cuidar de uma parte diferente do projeto. Por exemplo, existe um App chamado admin que já é instalado por defult no projeto que serve , dentre outras coisas, para gerenciar as contas de usuário,  Esse App e fornecido pelo próprio django, Aí você pode criar os seus com suas finalidades de forma a atender as necessidades do projeto


I have been asked to determine whether this binary relation is reflexive or irreflexive and symmetric

Question: I have been asked to determine whether this binary relation is reflexive or irreflexive and symmetric or not
On the set $\{1,2,3\}$, the relation
$\{(1,1), (1,2), (2,2), (3,3) \}$
I haven't been given any more information.
Would I be correct in saying that it is reflexive due to $(1,1), (2,2), (3,3)$ all belonging to the set given?
Also, I have stated it is not symmetric due to some of the relation not belonging to the set?
I apologise if I don't make much sense there, I'm new to this and trying to learn if someone could explain if I'm on the right lines or completely off?
Thank you for your time.
Comment: I don't understand what you meant in your second question.
Comment: It is not symmetric because you have $1R2$ but not $2R1$
Answer: Q1: Indeed, you're correct. Since for all $a$ elements in your set $S$, $(a, a) \in $ your relation $R$.
Q2: If you're trying to ask if you can state that it's not symmetric due to some pair not being in $R$, then indeed you can, as long as you prove why. In this particular case, $R$ is clearly not symmetric because $(1,2) \in R$ but $(2,1) \notin R$. What about antisymmetry, however?
Answer: A relation R on a set X(ie,R is a subset of X cross X) is said to be reflexive if xRx for all x belongs to X. Here,X={1,2,3},also given that,{(1,2),(2,2,),(3,3)} belongs to R, which means it is reflexive.
A relation is said to be symmetric if xRy implies yRx,where x,y belongs to X.
In the question it is given that,(1,2) belongs to R but (2,1) not belongs to R.Hence it is not symmetric.


Submitting form into a div

Question: Can anyone tell me why this is not working or give me another way into doing what I want.
I have a form on a page when click submit I want it to process into add.php and for it to open up in a DIV called right.
Form page

<form action="add.php" method="post" enctype="multipart/form-data" id ="myForm">
 Name: <input type="text" name="name">

 E-mail: <input type="text" name = "email">

 Phone: <input type="text" name = "phone">

Photo: <input type="file" name="photo">

<input type="submit" value="Upload">

</form>

Now if I add action to form to direct it to add.php all works fine so other script is ok yet when i do it this way,nothing happens it does not load add.php into the 'right' div like I want it to.
Anyone got any suggestions?
Comment: Whats the response code? You can use Firebug to debug and see the response code of the ajax request on the console.
Answer: Your $("#Submit") selector does not match anything (the submit button has no id and is defined after that bind attempt), so the function is not bound to any event, thus never executed.
Instead the form acts the way it should: upon submit it posts its content to the url specified in the 'action' attribute. This is what happens, that div is never touched.
you have to go back to understand how jquery selectors work. How to bind a function to an event.
Comment: Yeah sorry action is on form that's where I was testing, I want it without the action in form so it loads in the div and not actual url and obviously the script does the magic.
Comment: If I understand that comment right you claim that the action attribute in the form declaration is just for debugging purposes. If so then edit your question and remove it. Second: even without the $("#Submit") does not match anything. The js function will never get executed. You might want to try using a js debugger inside a browser and check that.
Answer: Is this your exact code?  There are a few issues:

$("#Submit").click should be wrapped in a document ready
handler so it doesnt run before the page has actually loaded.
There is no button that matches #Submit
There is no div that matches #right

Try

<form action="add.php" method="post" enctype="multipart/form-data" id ="myForm">
 Name: <input type="text" name="name">

 E-mail: <input type="text" name = "email">

 Phone: <input type="text" name = "phone">

Photo: <input type="file" name="photo">

<input type="submit" value="Upload" id="Submit">

</form>

Comment: It has worked thank you, Only problem I'm having now is when it processes add.php it puts everything into database other than the image url and don't upload image to server like it is ment to and does if I run it through url.
Comment: See the answer [here](http://stackoverflow.com/questions/2320069/jquery-ajax-file-upload) -
Comment: Have looked don't understand it i'm quite new to all this,as said all writes to database from the form when processed in add.php only the image is not inserting or uploading to server, add.php is all php coding.
Comment: Basically your AJAX upload will subit all the form data EXCEPT the actual files.
If you are new then I would go for the iframe method of file upload as the AJAX method is pretty new and may confuse you.  Try following [this](http://www.ajaxf1.com/tutorial/ajax-file-upload-tutorial.html) tutorial in isolation outside of your actual code.
Comment: It would be nice if someone could just nicely add it to the script above it's not like a whole complete script write that is what this site is for? helping someone with something they can't do or understand.
Comment: that wasn't ment to sound so cheeky and rude please take it the nicest way as possible. sorry again.
Comment: People are trying to help you learn.  You dont learn by copying and pasting other peoples code.  You learn by following other people's examples and adapting them for your own use.  If you want someone to do it for you, pay them! (www.rentacoder.com)!
Answer: you have  to create on div in body tag which id will be right and
<input id="submit" type="submit" name="upload" >

add new  div in body tag like this

Submitting form into a div


why Z score values are correlated in opposite direction of raw value?

Question: I am trying to find the correlation between age at onset of diabetes and Body Mass Index of patients. When i performed correlation test in R I get a positive "r" between age and BMI.. but when the same BMI is converted to z-scores I am getting negative correlation. How would interpret this result. Can someone please help.. Following is my results from R

WITH BMI

Call:
lm(formula = age ~ bmi, data = temp)

Residuals:
    Min      1Q  Median      3Q     Max
-4.3649 -1.8292 -0.2815  1.3361  8.6716

Coefficients:
            Estimate Std. Error t value Pr(>|t|)
(Intercept)  5.76891    0.45737  12.613   <2e-16 ***
bmi          0.23689    0.02382   9.944   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 2.427 on 508 degrees of freedom
Multiple R-squared:  0.1629,    Adjusted R-squared:  0.1613
F-statistic: 98.89 on 1 and 508 DF,  p-value: < 2.2e-16

WITH BMI Z SCORE

Call:
lm(formula = age ~ z, data = temp)

Residuals:
    Min      1Q  Median      3Q     Max
-4.4553 -1.9565 -0.1407  1.4838  7.0065

Coefficients:
            Estimate Std. Error t value Pr(>|t|)
(Intercept)  10.0316     0.1188  84.433  < 2e-16 ***
z            -0.7271     0.1439  -5.054 6.05e-07 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 2.588 on 508 degrees of freedom
Multiple R-squared:  0.04787,   Adjusted R-squared:  0.046
F-statistic: 25.54 on 1 and 508 DF,  p-value: 6.055e-07

This is the code to calculate Z-score. The code is in PERL

http://pastie.org/10843530

Following is the regression result of log transformed age vs z , actual BMI and gender
lm(formula = log(age) ~ NZ + bmi + gender, data = temp)

Residuals:
     Min       1Q   Median       3Q      Max
-0.50191 -0.07392  0.01485  0.08512  0.46986

Coefficients:
             Estimate Std. Error t value Pr(>|t|)
(Intercept)  0.731636   0.046103  15.869   <2e-16 ***
NZ          -0.305887   0.010358 -29.532   <2e-16 ***
bmi          0.079247   0.002353  33.681   <2e-16 ***
genderM     -0.022121   0.012759  -1.734   0.0836 .
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.1416 on 506 degrees of freedom
Multiple R-squared:  0.6981,    Adjusted R-squared:  0.6963
F-statistic:   390 on 3 and 506 DF,  p-value: < 2.2e-16

Comment: First: why do you have *another* data set? Second: the mistake is that you subtract the raw BMI from the mean. This reverses signs. You should do it the other way around: subtract the mean from the raw data.
Comment: You should show how you compute `z` from `bmi`... or better, re-examine how you compute it, and spot your mistake.
Comment: Ah, thank you. That makes things a lot clearer. What I'm *now* not understanding is what you mean by "age and gender matched $z$ scores". Could you perhaps edit your post to include a plot of `z` against `bmi`, as well as the specific code you used to calculate `z`?
Comment: To compute the z scores, I have another reference data set with Age Gender and Body Mas Index (BMI). I computed mean(SD) BMI of Age and gender specific subsets of reference data .. Then subtracted the raw BMI (of my data set) from the computed Mean and then divided by SD.
Comment: @Stephan Kolassa, Thanks for the reply. I use different reference data set for the z score calculation because, my sample has all diabetic patients.. and I need non diabetic healthy people as reference to calculate z-score. And the second : I am very sorry that's a typo .. I actually subtracted the mean from the raw data only..
Comment: Ah. But if your raw data and your $z$ scores come from different populations, you have different regressions - why would you expect the correlations to have the same sign? Plus, I probably am still not understanding what you do. It seems to me ilke you are regressing some people's age on *other* people's $z$-transformed BMIs (judging from your explanation of the "other dataset"). Why would you do that?
Comment: @StephanKolassa, I think choosing national or international representative data set is a standard practice.. thats why I chose a national dataset as a reference for calculating z score.
Comment: So you do something like `z = (mu_ref - bmi) / sd_ref`. No wonders the sign changes, the higher `bmi` is, the lower `z` is. Why not try  `z = (bmi - mu_ref) / sd_ref` instead?
Comment: Elvis, Yes that what I did.. subtracted the mean from the raw data only which is `z = (bmi - mu_ref) / sd`
Comment: Yes, but why do you regress person A, B, C's age on person X, Y, Z's $z$-transformed BMIs? I don't see how this makes sense.
Comment: You wrote "subtracted the raw BMI (of my data set) from the computed Mean and then divided by SD", that is `(mu_ref - bmi) / sd_ref`. Please double check.
Comment: @StephanKolassa, following is from research paper `BMI z-scores are calculated relative to an external reference (whether national or international) and not to an internal reference. The effect of this is that more than 5% of the population could be, for example, above the 95th percentile of BMI-for-age. Whereas, if an internal reference were used, exactly 5% of every sample would be above the 95th percentile, and the specific BMI cut point designating the 95th percentile would not be the same across samples. For this reason, BMI z-scores are based on an external reference`
Comment: @StephanKolassa I don't think arshad regresses on other persons z-scores. He/She just wants an effect with units "x standard deviations from the mean", and for standard deviation and mean (s)he uses a healthy population, this sort of makes sense.
Comment: @StephanKolassa, As Elvis said,  Thats what I did..
Comment: That's why I'm so confused about all this talk about "other datasets", when the code includes a data.frame called `temp` with a variable called `age` in both cases. It seems like these are actually two different data.frames. Correct? In which case, we have two different regressions from two different populations, and I'd say there is any number of reasons why we could get different correlation signs. For instance, are both populations age-matched? The relationship between age and BMI may be very different between adults and young children.
Comment: @StephanKolassa, sorry for the confusions.. the regression is done on the same data set. I used the reference dataset only to get the mean and sd .. which I computed age and gender specific. I used this mean and SD to calculate age and gender matched z scores in my dataset..
Comment: @StephanKolassa, by "age and gender matched z scores", I meant.. I calculated mean BMIs for each age and gender stratified.. ex: I group all people in age 15 and gender Male from my reference data set.. then compute mean and SD BMI for this group. Then using this mean and SD I calculate z score for the male patients in the age of 15 in my staudy data set by (BMI of the patient - MEAN from the ref data)/SD of ref data.. I edited my original query with plot and codes to calculate z score
Comment: Great, with the plots it's now beginning to be clear and interesting. You have a very clear relation between bmi (measured at onset?) and age at onset. The bmi and the z score (using a reference depending on age and sex) are well correlated. However the relation between z and age at onset is in the other direction!
Comment: I am thinking out loud. The higher the bmi, the higher the age at onset. Is that something expected? BTW, is that type I or type II diabetes? I assume type I, as there are many young patients, am I right? Early onset could mean "more genetic than environment (including bmi)", hence this positive correlation, late onset implying a bad environment, hence a high bmi. However when you consider z, which is more ore less the bmi corrected by age+sex, you find that high z correspond to an early onset. However this could be an artifact, the distribution of the age is right skewed.
Comment: Dis you try to log transform the age? And to put z and bmi together in a regression? Shouldn't the variable sex ebe include as a predictor as well?
Comment: Thank you. Is this the bmi measured at onset? I feel that this question is slowly turning into a question of epidemiology...
Comment: yes, BMI was measured at onset.
Comment: So early onset means high z value (high BMI for an individual of this age) but low BMI -- well, yes, the younger you are, the lower your BMI, that seems to make sense, especially for the range of ages in your sample. It makes sense, don't you think? So the (slightly overweight) guys you have in your incident diabetes sample have a bmi correlated with their age. The fact that the younger they are, the chubbier, doesn't counterbalance this.
Comment: This now seems very clear to me. I hope I convinced you. I'll edit my answer below. Thank you for this question which is a good example of how tricky real life problems can be!
Answer: I will first address a reformulation of your question, which is statistically relevant: if $\def\cov{\text{cov}}\cov(X_1, X_2) > 0$, $\cov(X_2, Y) > 0$, is it possible that $\cov(X_1, Y) < 0$ ?
The answer is yes, although it can seem counterintuitive. Here is an example of positive definite matrix exhibiting the desired behavior:
> D <- matrix( c(1, 0.2, -0.1, 0, 0.9, 0.3, 0, 0, 0.8), nrow=3 )
> D
     [,1] [,2] [,3]
[1,]  1.0  0.0  0.0
[2,]  0.2  0.9  0.0
[3,] -0.1  0.3  0.8
> D %*% t(D)
     [,1] [,2]  [,3]
[1,]  1.0 0.20 -0.10
[2,]  0.2 0.85  0.25
[3,] -0.1 0.25  0.74

So you can have $\cov(X_1, X_2) = 0.2$, $\cov(X_2, Y) = 0.25$ and $\cov(X_1, Y) = -0.1$.
This case corresponds to a situation where

$X_1$ has variance $1$,
$X_2 = 0.2\> X_1 + \varepsilon$ with $\varepsilon$ independent of $X_1$ and
$\def\var{\text{var}}\var(\varepsilon) = 0.9^2$
$Y = -0.1 \> X_1 + 0.3 \>\varepsilon + \varepsilon' = -0.16 \> X_1 + 0.3 \> X_2 + \varepsilon'$ with $\varepsilon'$ independent of $X_1, \varepsilon$ and $\var(\varepsilon') = 0.8^2$.

More generally, this will not be possible if $Y$ is independent of $X_1$ conditionally to $X_2$.
Now back to your problem: in your sample, early onset implies high $z$ value (i.e. high BMI for an individual of this age) but low BMI -- well, yes, the younger you are, the lower your BMI (cf this chart for the evolution of the BMI in the age range in your sample). So the (slightly overweight) guys you have in your incident diabetes sample have a BMI correlated with their age. The fact that the younger they are, the chubbier, doesn't counterbalance this.
I think it could be interesting to try to produce a chart of BMI for incident diabetes cases, as a function of age, and to compare it to the above linked chart. I think this will help to grasp what's happening in these data.


Should I use t() in hook_update_N() or admin UI in a custom module?

Question: Background / motivation
Many implementations of hook_update_N() and hook_install() in contrib and core modules in D7 and D8/D9 contain calls to the t() function. In general, I think it is considered a "best practice" to use t() for all kinds of strings that will be displayed to any user unknown to us.
For custom site-specific modules, I often avoid the t(), if I know that the message will never be shown to anybody outside the developers. This applies especially to hook_update_N() and hook_install(). Sometimes I use format_string() instead, to get the replacement without the translation.
My own motivation is like this:

I don't want these one-off strings to pollute the database and the translations UI. (not sure under which circumstances this would happen)
The audience of an update hook is a small team of developers and devops, who are used to code comments written in English.
I want the original English message to appear in logs, UI and CLI output, and in copy+paste snippets sent around between developers.
I want developers / site builders to always see the same string.

Personally, I sometimes even go further, and don't use t() for names of custom field formatters, plugins etc.
I have a colleague who is not convinced by my pragmatic arguments, and wants to do things "by the book". He places high value on consistency, and wants to use t() everywhere.
Perhaps one argument in favor of t() is that it would make it easier to later copy parts of the code into a contrib or in-house module, without having to modify all the strings. Not very likely for a typical hook_update_N(), but the possibility exists.
Question
Can it be "acceptable" as a team policy to not require t() for strings with an "internal" audience?
In which cases could it be acceptable to not use t()?
Is there any convention or good reason we could use to decide this, beyond personal preference?
EDIT
There are some more specific questions hiding in the original question. I am putting these in a section clearly marked as "EDIT" to not be accused of a Heisenberging.

In which cases could it be acceptable to not use t()?

Specific cases which could be discussed:

Messages returned from hook_update_N().
Exceptions which are thrown in hook_update_N() on failure. (#817160 suggests to NOT translate exception messages.)
Names of field formatters and other plugins defined in custom modules. An argument could be made that original names should not be translated.
(watchdog() / Drupal::logger() calls are mentioned in a comment, but I am less interested in those atm)

The arguments for/against apply differently to these cases.
Comment: `I don't want these one-off strings pollute the database and the translations UI` These will only get added until someone attempts to see the page that contains those `t()` strings in a different language. See [Why is an untranslated string not showing up?](https://drupal.stackexchange.com/a/247325/27710).
Comment: By the way, for `watchdog` known as [Drupal::logger](https://api.drupal.org/api/drupal/core!lib!Drupal.php/function/Drupal%3A%3Alogger/8.2.x) in D8 & D9. you don't pass `t()` to it as it automatically does the translation on output. See [Watchdog and translations](https://drupal.stackexchange.com/questions/88917/watchdog-and-translations)
Comment: For the database pollution, isn't there also a mechanism that detects t() calls by looking directly at the code? I think it only looks at specific files, I don't know if *.install is one of them.
Comment: `isn't there also a mechanism that detects t() calls by looking directly at the code?` possibly, but I don't really know the answer to that. You'll have to test it out, but my guess would be no with .install.
Answer: Ever hear the expression "You Play Like You Practice"? If you get in the habit of NOT using t() for strings that are translatable just because it's only for your site ("practice"), then when it counts (i.e. "play") you will probably also leave out t(), because that's what you're used to.
There is really no downside to doing it correctly here. You're not saving anything or making any measurable difference in performance. The upside is that if you do it the same everywhere, you're not going to introduce a bug by leaving off t() where it is needed, and you can then also use the automated coding standards tools to check your code without seeing a lot of fails from your missing t() functions - that keeps these many fails from obscuring the few real problems you might have.
Comment: Nice slogan, "You Play Like You Practice".
Comment: Perhaps different arguments apply to plugin names, e.g. field formatters or custom views plugins. If people view those in a different language, the English name might still be preferable because it is more recognizable.
Comment: I am accepting this answer, but I also welcome others who want to share their own thoughts or go into more detail case by case.


A Probability Question From Real Life

Question: I've tried asking this question of various mathematicians, but never gotten an answer.  Perhaps because it is not well posed.  I'll present the problem, but it may be that certain simplifying assumptions must be made in order to get a handle on it.
At the Manhattan Scrabble Club four games are played by each player during an evening tournament.  Each game is played by two players.  (If there are an odd number of players present on a given night, then the moderator plays too, otherwise he doesn't play.) There is a slight advantage to the player who plays first, so it is desirable that no player should play first more times than his opponents.  For this reason before a game between two players begins, their records for previous games on that night are compared.  If they have each played first in the same number of previous games, then each of them selects a tile from the bag without looking.  The player who selects the tile earliest in alphabetical order gets to go first in the new game. (If they choose the same letter, then each of them returns his tile to the bag and they choose again until someone prevails.) If one player has played first in more previous games than the other, then his opponent gets to play first in the new game.  In actual play there is a moderator who tries to match player according to their records for that night .  For example: if, before the fourth game, there are two players each of whom has won all three of his previous games then the moderator will try to match them against each other.  Perhaps it would be best to leave out the effect of the moderator  initially when trying to analyse the setup.
Despite these measures, it seems clear that it is possible that one or more players will play first in each of his, or their games.
First question:  What is the smallest number of players who must be present at the tournament such that it is possible that at least one player plays first in all four of his games?
Second question:  Given that the number of players in the tournament is as in the answer to the first question, can we assign a probability to the occurence that on a certain night one or more of the players has played first in all four games?  If so, what is that probability?
Comment: @BenjaminDickman  I removed the impossible condition that there are two players who have played each other, yet they both have perfect records.  Thanks for pointing this out.
Comment: Does everyone play their first game at the same time?
Comment: @benjaminDickman  It seems that you are right and my question has indeed been migrated, because it is not research level.  My favorite teacher, David M. Bloom, once wrote me that he did not consider himself good enough to be a research mathematician.  He was a Putnam Fellow.
Comment: @DavidS.Newman Problem solving and research mathematics are not one and the same; unsurprisingly, then, they have different websites. Hopefully you get a satisfying answer here! (And for the record: I'm a major Scrabble fan.)
Answer: Note that you ask for possible, as opposed to guaranteed.
In order for someone to go first in all four games, the two people who play at the final round must have gone first in the first 3 games. .
This in turn implies that there are at least 4 people who have gone first in the first 2 games, and hence at least 4 people who have gone first in the first game and hence at least 16 people.
To show that 16 is enough, we just need to find a construction. This is very easy, by simply letting the person who goes first win the game, we see that the conditions are statisfies.
Hence the answer is 16.
Comment: There is a typo in the second paragraph, it should be " and hence at least 8 people  who have gone first in the first game ".
Answer: Second Question: very small (less than 0,01%)
In order to have a player from the 16 start all of his games first, things must go "ideally". That means that all those who played first in their game 1, should play against each other in game 2. From these, there will be 4 that have played first in both their games. They must play against each other in game 3. To find the actual probability we have:
Lets say that a player that has started first in his last n games has a streak of n.
1st round of games: The probability that 8 of the 16 players start first is $ P(A) = 1 $.
2nd round of games: We have 8 players with a streak of 1 that must play against each other. The first has 7 "good" opponents and 7 bad (the remaining 8 minus the one he played against in round 1). So 7/14. Once these two are paired, the next one has 5 "good" opponents and 7 "bad". So 5/12. In total:
$ P(B) = \frac{7}{14} \cdot \frac{5}{12} \cdot \frac{3}{10} \cdot \frac{1}{8} $
Notice that the probability of having 4 players with a streak of 3 in the end of the second round is $ P(A) \cdot P(B) $.
3rd round: The reasoning is the same: we have 4 people that must play each other. Each one has already played against two people from the remaining 12. So $ P(C) = \frac{3}{13} \cdot \frac{1}{11} $.
4th round: If we managed to get here with 2 people having a streak of 3 (and the remaining 14 a smaller streak) - we just have to pair these 2. That gives $ P(D) = \frac{1}{12} $.
In total your probability is $ P(A) \cdot P(B) \cdot P(C) \cdot P(D) = \dfrac{1}{73.216} $
On a sidenote - the bigger the advantage the starting player has and considering how the moderator matches players, this probability can become significantly higher. If the player who plays first is sure to win - then it is actually certain that you will have someone with a streak of 4 (but in that case you are not really playing Scrabble - you are playing an extremely convoluted coin toss game :) ).

A Probability Question From Real Life


Is there a way to monitor network traffic from outside the Vnet in Azure?

Question: I can view the network traffic into an Azure VM, but I would like to view network traffic coming from communications outside the LAN. Is it possible to show this info in Azure?
IE I have this graph which shows total network usage, but I would like to exclude communication from within the same LAN. Is such a thing possible in Azure or would I have to do it from within the VM itself?
Answer: Based on your scenario, you could choose Azure Network watcher.
Network Watcher - Scenario-based monitoring is provided with the features in Network Watcher. This service includes packet capture, next hop, IP flow verify, security group view, NSG flow logs. Scenario level monitoring provides an end to end view of network resources in contrast to individual network resource monitoring.
Comment: Hi, does it work for you?
Comment: Hi,

I'm just about to try network watchers, will report back here when I do :)
Comment: Should I be using `packet capture` to achieve total upload/download amount? Or is it some other tool within network watcher?
Comment: I think you could try. Based on my knowledge, Azure does not provide other same tool.


How to build the Qt-SQL-driver-plugin 'QSQLCIPHER' for SQLite-DB with SQLCipher-extension using the Windows/MinGW-platform?

Question: This is not typically a question where to find a step-by-step guide, but rather the guide itself.
My intention with this post is to give others a hint, who have the same problems in compiling the driver-plugin as I just had recently.
How to build the Qt-SQL-driver-plugin 'QSQLCIPHER' for SQLite-DB with SQLCipher-extension using the Windows/MinGW-platform?
Comment: The **Q**uestion & **A**nswer format of StackOverflow must be followed. I suggest that you post your answer as a actual answer under your question and reduce the question to an actual question (you need to wait a little). It would be better if you add some problems that you've run into to the question so that it is clear that you've tried to solve the problem (I know that you solved the problem, but questions must stand on their own. They cannot depend on potential answer even if you already have an answer.) See also: http://meta.stackexchange.com/questions/17463
Comment: If you run into problems in step 5 with gawk complaining 'backslash not last character on line', then go and fix your line edings to unix format in the two makeopcode?.awk files.
Comment: The tutorial were very informative, how ever, after following all the steps you set, I stopped here: `D:\Qt\5.5.1\5.5\src\qtbase\src\plugins\sqldrivers\sqlcipher>mingw32-make
mingw32-make -f Makefile.Release all
mingw32-make[1]: Entering directory 'D:/Qt/5.5.1/5.5/src/qtbase/src/plugins/sqldrivers/sqlcipher'
mingw32-make[1]: *** No rule to make target 'release/smain.moc', needed by 'release/smain.o'.  Stop.
mingw32-make[1]: Leaving directory 'D:/Qt/5.5.1/5.5/src/qtbase/src/plugins/sqldrivers/sqlcipher'
makefile:38: recipe for target 'release-all' failed
mingw32-make: *** [release-all] Error 2`
Comment: The driver was built under Windows 7 and runs as well under Windows 8.1. But I have no experience with other Windows versions. @rrrfusco: Or are you asking about other OS like Linux? This will not work.
Comment: @rrrfusco: Sorry, I cannot comment to your question directly. I have built the driver on Windows 7 (64 bit) and the driver also runs on Windows 8.1 (64 bit). But I have no experience on 32 bit Windows. Are your XP and Win8 32 bit systems? I will try to get a 32 bit system to test mydriver and get with an answer.
Answer: How to build the Qt-SQL-driver-plugin 'QSQLCIPHER' for SQLite-DB with SQLCipher-extension using the Windows/MinGW-platform:

Qt 5.4.0 for Windows/MinGW

Download Qt
Install including the sources e.g to C:\Qt\Qt5.4.0

OpenSSL for Windows

Download Win32 OpenSSL v1.0.2a
Download Visual C++ 2008 Redistributable
Install Visual C++ 2008 Redistributable by executing 'vcredist_x86.exe'
Install OpenSSL v1.0.2a by executing 'Win32OpenSSL-1_0_2.exe'

Target directory e.g. C:\OpenSSL-Win32
During installation choose the option to install the libraries to the Windows system directory (C:\Windows\SysWOW64)

MinGW - Minimalist GNU for Windows

Download and install 'mingw-get-setup.exe'
Start of MinGW Installer

Installation of MSYS Base System

Selection: All Packages -> MSYS -> MSYS Base System
Select msys-base (Class 'bin') for installation
Menu: installation -> apply changes
Installation of files by default to directory C:\MinGW

Installation of Tcl/Tk

Selection: All Packages -> MinGW -> MinGW Contributed
Select 'mingw32-tcl' and 'mingw32-tk' (Class 'bin') for installation
Menu: installation -> apply changes
Installation of files by default to directory C:\MinGW

Copy content of C:\MinGW to the Qt-MinGW-directory C:\Qt\Qt5.4.0\Tools\mingw491_32
Create file 'fstab' in C:\Qt\Qt5.4.0\Tools\mingw491_32\msys\1.0\etc

Insert content as follows:
#Win32_Path                        Mount_Point
C:/Qt/Qt5.4.0/Tools/mingw491_32    /mingw
C:/Qt/Qt5.4.0/5.4                  /qt
C:/                                /c

zlib-Library

Download zlib-dll-Binaries
Extract and copy file 'zlib1.dll' to the Qt-MinGW-directory C:\Qt\Qt5.4.0\Tools\mingw491_32\msys\1.0\bin

SQLCipher

Download the SQLCipher-zip-file
Extract the zip-file e.g. to C:\temp\sqlcipher-master
Copy OpenSSL-Win32-libraries

Copy C:\OpenSSL-Win32\bin\libeay32.dll to C:\temp\sqlcipher-master
Copy C:\OpenSSL-Win32\lib\libeay32.lib to C:\temp\sqlcipher-master

Build SQLCipher.exe

Execute MSYS: C:\Qt\Qt5.4.0\Tools\mingw491_32\msys\1.0\msys.bat
$ cd /c/temp/sqlcipher-master
$ ./configure --prefix=$(pwd)/dist --with-crypto-lib=none --disable-tcl CFLAGS="-DSQLITE_HAS_CODEC -DSQLCIPHER_CRYPTO_OPENSSL -I/c/openssl-win32/include /c/temp/sqlcipher-master/libeay32.dll -L/c/temp/sqlcipher-master/ -static-libgcc" LDFLAGS="-leay32"
$ make clean
$ make sqlite3.c
$ make
$ make dll
$ make install

Save the executable SQLite/SQLCipher-database e.g. to C:\sqlcipher

Copy C:\temp\sqlcipher-master\dist\bin\sqlcipher.exe to C:\sqlcipher.
The file 'sqlcipher.exe' is the crypting equivalent to the non-crypting original command line interface 'sqlite3.exe'.
Copy C:\temp\sqlcipher-master\sqlite3.dll to C:\sqlcipher.
This file is the SQLite-library extended by the encryption.

The SQLite-database with SQLCipher-extension is now ready for work.

Build Qt-QSQLCIPHER-driver-plugin

Create directory:
C:\Qt\Qt5.4.0\5.4\Src\qtbase\src\plugins\sqldrivers\sqlcipher
Create the following three files within the new directory:

File 1: smain.cpp:
#include <qsqldriverplugin.h>
#include <qstringlist.h>
#include "../../../../src/sql/drivers/sqlite/qsql_sqlite_p.h" // There was a missing " at the end of this line
QT_BEGIN_NAMESPACE
class QSQLcipherDriverPlugin : public QSqlDriverPlugin
{
    Q_OBJECT
    Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QSqlDriverFactoryInterface" FILE "sqlcipher.json")
public:
    QSQLcipherDriverPlugin();

    QSqlDriver* create(const QString &);
};
QSQLcipherDriverPlugin::QSQLcipherDriverPlugin()
    : QSqlDriverPlugin()
{
}
QSqlDriver* QSQLcipherDriverPlugin::create(const QString &name)
{
    if (name == QLatin1String("QSQLCIPHER")) {
        QSQLiteDriver* driver = new QSQLiteDriver();
        return driver;
    }
    return 0;
}
QT_END_NAMESPACE
#include "smain.moc"

File 2: sqlcipher.pro
TARGET = qsqlcipher
SOURCES = smain.cpp
OTHER_FILES += sqlcipher.json
include(../../../sql/drivers/sqlcipher/qsql_sqlite.pri)
wince*: DEFINES += HAVE_LOCALTIME_S=0
PLUGIN_CLASS_NAME = QSQLcipherDriverPlugin
include(../qsqldriverbase.pri)

File 3: sqlcipher.json
{
    "Keys": [ "QSQLCIPHER" ]
}

Copy directory C:\Qt\Qt5.4.0\5.4\Src\qtbase\src\sql\drivers\sqlite
to C:\Qt\Qt5.4.0\5.4\Src\qtbase\src\sql\drivers\sqlcipher
Customize file C:\Qt\Qt5.4.0\5.4\Src\qtbase\src\sql\drivers\sqlcipher\qsql_sqlite.pri
The content of the file shall be like:
HEADERS += $$PWD/qsql_sqlite_p.h
SOURCES += $$PWD/qsql_sqlite.cpp
!system-sqlite:!contains(LIBS, .*sqlite3.*) {
    include($$PWD/../../../3rdparty/sqlcipher.pri)     #<-- change path of sqlite.pri to sqlcipher.pri here !
} else {
    LIBS += $$QT_LFLAGS_SQLITE
    QMAKE_CXXFLAGS *= $$QT_CFLAGS_SQLITE
}

The remaining two files in this directory need not to be changed.
Create file 'sqlcipher.pri' in directory C:\Qt\Qt5.4.0\5.4\Src\qtbase\src\3rdparty with following content:
CONFIG(release, debug|release):DEFINES *= NDEBUG
DEFINES += SQLITE_OMIT_LOAD_EXTENSION SQLITE_OMIT_COMPLETE SQLITE_ENABLE_FTS3 SQLITE_ENABLE_FTS3_PARENTHESIS SQLITE_ENABLE_RTREE SQLITE_HAS_CODEC
!contains(CONFIG, largefile):DEFINES += SQLITE_DISABLE_LFS
contains(QT_CONFIG, posix_fallocate):DEFINES += HAVE_POSIX_FALLOCATE=1
winrt: DEFINES += SQLITE_OS_WINRT
winphone: DEFINES += SQLITE_WIN32_FILEMAPPING_API=1
qnx: DEFINES += _QNX_SOURCE
INCLUDEPATH +=  $$PWD/sqlcipher c:/openssl-win32/include
SOURCES +=      $$PWD/sqlcipher/sqlite3.c
LIBS += -L$$PWD/sqlcipher/lib -lsqlcipher -leay32 -lsqlite3
TR_EXCLUDE += $$PWD/*

Create and fill C:\Qt\Qt5.4.0\5.4\Src\qtbase\src\3rdparty\sqlcipher

Create the two directories:
C:\Qt\Qt5.4.0\5.4\Src\qtbase\src\3rdparty\sqlcipher
C:\Qt\Qt5.4.0\5.4\Src\qtbase\src\3rdparty\sqlcipher\lib

Copy the following files to C:\Qt\Qt5.4.0\5.4\Src\qtbase\src\3rdparty\sqlcipher:
C:\temp\sqlcipher-master\shell.c
C:\temp\sqlcipher-master\sqlite3.c
C:\temp\sqlcipher-master\sqlite3.h
C:\temp\sqlcipher-master\sqlite3ext.h

Copy the following files/directories to C:\Qt\Qt5.4.0\5.4\Src\qtbase\src\3rdparty\sqlcipher\lib:
C:\temp\sqlcipher-master\dist\lib
C:\temp\sqlcipher-master\sqlite3.dll
C:\OpenSSL-Win32\bin\libeay32.dll

The directory now consists of the following files and directories:
C:\QT\QT5.4.0\5.4\SRC\QTBASE\SRC\3RDPARTY\SQLCIPHER
|   shell.c
|   sqlite3.c
|   sqlite3.h
|   sqlite3ext.h
|
\---lib
    |   libeay32.dll
    |   libsqlcipher.a
    |   libsqlcipher.la
    |   sqlite3.dll
    |
    \---pkgconfig
            sqlcipher.pc

Compile the QSQLCIPHER-driver-plugin for Qt:

Open Qt-command line C:\Windows\System32\cmd.exe /A /Q /K C:\Qt\Qt5.4.0\5.4\mingw491_32\bin\qtenv2.bat
Execute the following commands:
cd C:\Qt\Qt5.4.0\5.4\Src\qtbase\src\pluins\sqldrivers\sqlcipher
qmake
mingw32-make

This builds the QSQLCIPHER-driver-plugin within the following directory:
C:\QT\QT5.4.0\5.4\SRC\QTBASE\PLUGINS\SQLDRIVERS
    libqsqlcipher.a
    libqsqlcipherd.a
    qsqlcipher.dll
    qsqlcipherd.dll

Copy 'qsqlcipher.dll' and 'qsqlcipherd.dll' to the SQL-driver-plugin-directory C:\Qt\Qt5.4.0\5.4\mingw491_32\plugins\sqldrivers.

Create a new encrypted SQLite/SQLCipher-database

Create new SQLite-Plaintext-database 'plaintext.db' with a test table and some test data

Change directory to C:\sqlcipher, which contains 'sqlcipher.exe' and 'sqlite3.dll' (see above).
C:\sqlcipher>sqlcpher.exe plaintext.db
  SQLCipher version 3.8.6 2014-08-15 11:46:33
  Enter ".help" for instructions
  Enter SQL statements terminated with a ";"
  sqlite> create table testtable (id integer, name text);
  sqlite> insert into testtable (id,name) values(1,'Bob');
  sqlite> insert into testtable (id,name) values(2,'Charlie');
  sqlite> insert into testtable (id,name) values(3,'Daphne');
  sqlite> select * from testtable;
  1|Bob
  2|Charlie
  3|Daphne
  sqlite> .exit

Open C:\sqlcipher\plaintext.db using a standard text-editor:
Database scheme and test data can be read in plaintext.
Encrypting the plaintext-database
This will create the database C:\sqlcipher\encrypted.db using the key 'testkey'.
C:\sqlcipher>sqlcipher.exe plaintext.db
    SQLCipher version 3.8.6 2014-08-15 11:46:33
    Enter ".help" for instructions
    Enter SQL statements terminated with a ";"
    sqlite> ATTACH DATABASE 'encrypted.db' AS encrypted KEY 'testkey';
    sqlite> SELECT sqlcipher_export('encrypted');
    sqlite> DETACH DATABASE encrypted;
    sqlite> .exit

Open C:\sqlcipher\encrypted.db using a standard text-editor:
Data are now encrypted.
For more useful information visit:
https://www.zetetic.net/sqlcipher/sqlcipher-api/

Usage of the SQLite-database with SQLCipher-extension and access via Qt

Create a new Qt-command-line-project e.g. 'qsqlcipher'
Project file
QT       += core sql
QT       -= gui
TARGET = qsqlcipher
CONFIG   += console
CONFIG   -= app_bundle
TEMPLATE = app
SOURCES += main.cpp

Test-program 'main.cpp'
#include <QCoreApplication>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QDebug>
#include <QString>
int main(int argc, char *argv[]) {
    QCoreApplication a(argc, argv);
    qDebug() << QSqlDatabase::drivers();
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLCIPHER");
    db.setDatabaseName("C:/sqlcipher/encrypted.db");
    db.open();
    QSqlQuery q;
    q.exec("PRAGMA key = 'testkey';");
    q.exec("insert into testtable (id,name) values(4,'dummy')");
    q.exec("SELECT id,name anz FROM testtable");
    while (q.next()) {
        QString id = q.value(0).toString();
        QString name = q.value(1).toString();
        qDebug() << "id=" << id << ",  name=" << name;
    }
    db.close();
    return 0;
}

Compile and execute
("QSQLCIPHER", "QSQLITE", "QMYSQL", "QMYSQL3", "QODBC", "QODBC3", "QPSQL", "QPSQL7")
id= "1" ,  name= "Bob"
id= "2" ,  name= "Charlie"
id= "3" ,  name= "Daphne"
id= "4" ,  name= "dummy"

When delivering a Qt-program do not forget the Qt-libraries, the platforms-libraries, SQL-driver-plugin 'qsqlcipher.dll' and the OpenSSL-library 'libeay32.dll'.
Example for the test program above:
C:\TEMP\QSQLCIPHER-TEST
|   icudt53.dll
|   icuin53.dll
|   icuuc53.dll
|   libeay32.dll
|   libgcc_s_dw2-1.dll
|   libstdc++-6.dll
|   libwinpthread-1.dll
|   qsqlcipher.exe
|   Qt5Core.dll
|   Qt5Sql.dll
|
+---platforms
|       qminimal.dll
|       qoffscreen.dll
|       qwindows.dll
|
\---sqldrivers
        qsqlcipher.dll

Caution: The test program contains the key:
...
q.exec("PRAGMA key = 'testkey';");
...

This key string in the binary file of the test program can easiliy be read using a hex-editor, which is, to my opinion, a lack in security:
...
00002C90  70 68 65 72 2F 65 6E 63 72 79 70 74 65 64 2E 64  pher/encrypted.d
00002CA0  62 00 50 52 41 47 4D 41 20 6B 65 79 20 3D 20 27  b.PRAGMA key = '
00002CB0  74 65 73 74 6B 65 79 27 3B 00 00 00 69 6E 73 65  testkey';...inse
00002CC0  72 74 20 69 6E 74 6F 20 74 65 73 74 74 61 62 6C  rt into testtabl
...

For approaches how to solve this problem, ask the search engine of your own choice. ;-)
E.g. search for: hide string in executable
Comment: Cool! BTW: don't hard code key, generate it on the fly.
Comment: hello,
I want do this work in qt 5.9 and above. how can i do it? because structure is change in qt new versions. thanks a lot for any help.
Comment: @Ahmad Were you successful in builiding qsqlcipher.dll. Could you provide the steps to follow?
Comment: @Veera yes finally i successfully build it and build Qt plugin for using it in qt. how can i send some  information for you?


Nging redirect http and https to https://domain (without www)

Question: This is my Nginx config:
upstream app_server {
  # Bindings to the Gunicorn server
  server 127.0.0.1:8002 fail_timeout=0;
}

server {
  listen 80;
  server_name "~^www\.(.*)$";
  return 301 https://$host$request_uri;
}

server {
  access_log path_to_nginx-access.log;
  error_log path_to_nginx-error.log;

  listen 443 ssl;
  server_name _;
  ssl_certificate path_to_nginx.crt;
  ssl_certificate_key path_to_nginx.key;

  client_max_body_size 4G;
  keepalive_timeout 5;
  root path_to_root;

  location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Host $host;
    proxy_redirect off;

    if (!-f $request_filename) {
      proxy_pass http://app_server;
      break;
    }
  }
  error_page 500 502 503 504 /500.html;
  location = /500.html {
    root path_to_templates;
  }
}

with this config I can redirect:

http://domain.com -> https://domain.com
https://domain.com -> https://domain.com (same)
http://www.domain.com -> https://domain.com
https://www.domain.com -> Error

How do I make my server redirect https www to https non-www.
Keep in mind that I need to handle multiple domains with the same Nginx server (vide server_name).
Thanks!
Comment: With https, you need to have a certificate that covers all the domains you are planning to redirect. You can have a certificate that covers multiple domains, but you need to know all the domains when ordering the certificate. However, it might be possible that nginx cannot use similar `server_name` setup with `https` as you use with `http`, because the domain name is negotiated in a different spot with `https`. So, this might be impossible to achieve.
Comment: Sure! I got you. How would I solve it for one domain only?
Answer: For a single domain you can achieve it like this:
server {
    listen 443;
    server_name www.domain.com;
    ssl_certificate path_to_certificate;
    ssl_certificate_key path_to_key;

    return 301 https://domain.com$request_uri;
}

Comment: my certificate only supports domain.com (without wildcards). By adding the code above I'm getting "invalid certificate error". Any tips?
Comment: In that case you need to get a certificate that is valid for both `domain.com` and `www.domain.com`. Usually CAs give you certificates that are valid for both. You need to contact your CA and ask how to get a certificate that is valid for both.