Override Taxonomy Node content for different terms without using views

Question: I have taxonomy-term.tpl.php page in my theme folder which shows the standard layout of a taxonomy term page like : Title, Description and content listing corresponding to the taxonomy term. Now I wish override this standard layout for a particular term and I create another term file namely : taxonomy-term--32.tpl.php. For this I haven't added any function in the template.php file in my theme.
My problem is that for the term page with tid / term id = 32, does not entirely override the content block. Precisely, the listing of the contents related to that taxonomy term remains in the overridden web page as well, which is not what I want.
N.B : I have disabled the Taxonomy Term View from the admin end of the site as I am not absolutely sure about its functionality.
Comment: there is a possibility that Drupal use its cache rather than the new templates as suggested, clear your caches
Comment: What you want to do actually ?
Comment: That template only governs the display of the taxonomy term itself, as you seem to have realised. The $content array is simply a list of rendered nodes. If you want to override it, Views would be the simplest way to proceed. Otherwise you are likely to require some fairly messy and hard-to-maintain code.
Answer: Change your node.tpl.php file so that it only returns the parts of the node you want, if the current path of the page being viewed is taxonomy/term. Using the Bartik node.tpl.php file as an example, and assuming you only want the title (linked to the node) to be displayed, you could do something like this:
Original ("before"):
<div id="node-<?php print $node->nid; ?>" class="<?php print $classes; ?> clearfix"<?php print $attributes; ?>>

  <?php print render($title_prefix); ?>
  <?php if (!$page): ?>
    <h2<?php print $title_attributes; ?>>
      <a href="<?php print $node_url; ?>"><?php print $title; ?>

  <?php endif; ?>
  <?php print render($title_suffix); ?>

  <?php if ($display_submitted): ?>

      <?php print $user_picture; ?>
      <?php print $submitted; ?>

  <?php endif; ?>

  <div class="content clearfix"<?php print $content_attributes; ?>>
    <?php
      // We hide the comments and links now so that we can render them later.
      hide($content['comments']);
      hide($content['links']);
      print render($content);
    ?>

  <?php
    // Remove the "Add new comment" link on the teaser page or if the comment
    // form is being displayed on the same page.
    if ($teaser || !empty($content['comments']['comment_form'])) {
      unset($content['links']['comment']['#links']['comment-add']);
    }
    // Only display the wrapper div if there are links.
    $links = render($content['links']);
    if ($links):
  ?>

      <?php print $links; ?>

  <?php endif; ?>

  <?php print render($content['comments']); ?>

Altered ("after"):
<?php if (!$page && arg(0) == 'taxonomy' && arg(1) == 'term'): ?>
<?php
 // Note:
 // If you want to see all the values available to you at this point, uncomment
 // the next line (not safe for production environments):
 // print '
' . check_plain(print_r($node, 1)) . '
';
?>
<div id="node-<?php print $node->nid; ?>" class="<?php print $classes; ?> clearfix"<?php print $attributes; ?>>
  <?php print render($title_prefix); ?>
    <h2<?php print $title_attributes; ?>>
      <a href="<?php print $node_url; ?>"><?php print $title; ?>

  <?php print render($title_suffix); ?>

<?php else: ?>

<div id="node-<?php print $node->nid; ?>" class="<?php print $classes; ?> clearfix"<?php print $attributes; ?>>

  <?php print render($title_prefix); ?>
  <?php if (!$page): ?>
    <h2<?php print $title_attributes; ?>>
      <a href="<?php print $node_url; ?>"><?php print $title; ?>

  <?php endif; ?>
  <?php print render($title_suffix); ?>

  <?php if ($display_submitted): ?>

      <?php print $user_picture; ?>
      <?php print $submitted; ?>

  <?php endif; ?>

  <div class="content clearfix"<?php print $content_attributes; ?>>
    <?php
      // We hide the comments and links now so that we can render them later.
      hide($content['comments']);
      hide($content['links']);
      print render($content);
    ?>

  <?php
    // Remove the "Add new comment" link on the teaser page or if the comment
    // form is being displayed on the same page.
    if ($teaser || !empty($content['comments']['comment_form'])) {
      unset($content['links']['comment']['#links']['comment-add']);
    }
    // Only display the wrapper div if there are links.
    $links = render($content['links']);
    if ($links):
  ?>

      <?php print $links; ?>

  <?php endif; ?>

  <?php print render($content['comments']); ?>

<?php endif; ?>

Hope that helps...
:)
Comment: Im using a path auto module in my site for which, the immediate url that we see does not contain taxonomy/term. And as for the theme, a customized sub-theme of Omega is being used. We have a node--page.tpl.php page apart from the normal node.tpl.php in the node folder within the theme folder also and overriding both in accordance to your post did not make any difference.

All I want is to hide the listing in the taxonomy-term pages that I override for particular catregories by creating new taxonomy-term--[tid | machine_name_of_terms].tpl.php. Is that possible anyhow?
Comment: @RajatBandyopadhyay Don't worry... One of the (many) awesome things about Drupal is that using aliases (with or without pathauto) should not change the value of `arg(0)` or any `arg()` for that matter. i.e. if you create a page at `node/1` and then give it an alias of *home*, `arg(0)` is still going to be *node* and `arg(1)` is still going to be *1*. As far as using custom file templates, you could certainly apply the same structure to your `node--page.tpl.php` file, since what you see on the `taxonomy/term` pages is a list of node teasers. (Continued in next comment...)
Comment: You could also do what you suggested, using `taxonomy-term--[tid | machine_name_of_terms].tpl.php` files, although I believe the node teasers will still appear. For more control, consider using files that start with "page--". More possible files: `page--taxonomy--term--[term_id].tpl.php`, `page--taxonomy--term--%.tpl.php`, `page--taxonomy--term.tpl.php`, `page--taxonomy.tpl.php`, `taxonomy-term--[term_id].tpl.php`, `taxonomy-term--[machine_name].tpl.php`, etc. I'd also **highly** recommend playing around with the [devel_themer](http://drupal.org/project/devel_themer) module.
Comment: Thanks for all your support, @jerdiggity. I have tried also tried the `page--term--tid.tpl.php` way, but in that path I'll have to rebuild the entire page every time I override a particular taxonomy term. I want to override the main content block differently for different taxonomy pages, not the entire page (header, one-sidebar & the footer).
(continued in next comment ...)
Comment: While trying to find out ways to hack the taxonomy pages, I found out a module which might be resposnsible for the standard content listings even on the overridden taxonomy pages - the **Entity View Mode** module (Administrator > COnfigurations > System > Entity View Modes in Drupal #7). I have a option there as Taxonomy Term Page, but the View Mode is locked for any operations. Can you throw some light upon the same, if you have encountered such a circumstance anytime. Cheers !
Comment: @RajatBandyopadhyay -- You're welcome. Please see my most recent answer... :)
Answer: I don't want to simply edit my first answer because I think that's just as feasible an option as this one is (and less complicated). Regardless, this should do the trick. Basically all we're doing is modifying the page callback for the taxonomy/term/%taxonomy_term menu item.
1) Inside the sites/all/modules directory, create a folder (directory) called newtaxonomypages and inside that folder, create two files: newtaxonomypages.info and newtaxonomypages.module.
2) Paste the following code inside newtaxonomypages.info:
name = New Taxonomy Pages
description = Changes the display of taxonomy/term pages.
core = 7.x
dependencies[] = taxonomy

3) Paste the following code inside newtaxonomypages.module:
<?php

/**
 * @file
 * Alters the page callback for the menu item taxonomy/term/%taxonomy_term.
 */

/**
 * Implements hook_menu_alter().
 */
function newtaxonomypages_menu_alter(&$items) {
  // Change page callback to 'newtaxonomypages_page' from 'taxonomy_term_page'...
  $items['taxonomy/term/%taxonomy_term']['page callback'] = 'newtaxonomypages_page';
  // ... which was originally found in a separate file, but now it's defined below. So:
  unset($items['taxonomy/term/%taxonomy_term']['file']);
}

/**
 * The code inside this next function is the exact same code that you'd find on line 17 of
 * modules/taxonomy/taxonomy.pages.inc, except the nodes have been commented out and removed
 * from the view.
 */
function newtaxonomypages_page($term) {
  // If there is a menu link to this term, the link becomes the last part of
  // the active trail, and the link name becomes the page title. Thus, we must
  // explicitly set the page title to be the term title.
  drupal_set_title($term->name);

  // Build breadcrumb based on the hierarchy of the term.
  $current = (object) array(
    'tid' => $term->tid,
  );
  // @todo This overrides any other possible breadcrumb and is a pure hard-coded
  //   presumption. Make this behavior configurable per vocabulary or term.
  $breadcrumb = array();
  while ($parents = taxonomy_get_parents($current->tid)) {
    $current = array_shift($parents);
    $breadcrumb[] = l($current->name, 'taxonomy/term/' . $current->tid);
  }
  $breadcrumb[] = l(t('Home'), NULL);
  $breadcrumb = array_reverse($breadcrumb);
  drupal_set_breadcrumb($breadcrumb);
  drupal_add_feed('taxonomy/term/' . $term->tid . '/feed', 'RSS - ' . $term->name);

  // Set the term path as the canonical URL to prevent duplicate content.
  $uri = entity_uri('taxonomy_term', $term);
  drupal_add_html_head_link(array('rel' => 'canonical', 'href' => url($uri['path'], $uri['options'])), TRUE);
  // Set the non-aliased path as a default shortlink.
  drupal_add_html_head_link(array('rel' => 'shortlink', 'href' => url($uri['path'], array_merge($uri['options'], array('alias' => TRUE)))), TRUE);

  // Normally we would call taxonomy_term_show() here, but for backwards
  // compatibility in Drupal 7 we do not want to do that (it produces different
  // data structures and HTML markup than what Drupal 7 released with). Calling
  // taxonomy_term_view() directly provides essentially the same thing, but
  // allows us to wrap the rendered term in our desired array structure.

  $build['term_heading'] = array(
    '#prefix' => '
',
    '#suffix' => '
',
    'term' => taxonomy_term_view($term, 'full'),
  );

  // *****PART THAT HAS BEEN REMOVED: START***** //
  /*
  if ($nids = taxonomy_select_nodes($term->tid, TRUE, variable_get('default_nodes_main', 10))) {
    $nodes = node_load_multiple($nids);
    $build += node_view_multiple($nodes);
    $build['pager'] = array(
      '#theme' => 'pager',
      '#weight' => 5,
    );
  }
  else {
    $build['no_content'] = array(
      '#prefix' => '
',
      '#markup' => t('There is currently no content classified with this term.'),
      '#suffix' => '
',
    );
  }
  */
  // *****PART THAT HAS BEEN REMOVED: END***** //

  return $build;
}

4) Save both files, go to admin/modules, find the New Taxonomy Pages module (under Other), enable it, then Save configuration.
☺
Comment: Thanks a ton yet again @jerdiggty. I can't understand why changes in the `node--page.tpl.php` doesn't reflect in my site. Nevertheless, heartiest appreciation for the help that you did provide me twice over. I am working with Drupal over the past three months, and I have a lot to catch up. Hope you will be there in days to come. Cheers mate  :)


Spectral theory and Taylor expansion in quantum mechanics for unbounded operators

Question: I am reading spectral theory and quantum mechanics. We know that for an unbounded self-adjoint operator $A$, operator-valued functions such as $\exp(iAt)$, $t\in\mathbb{R}$ can be defined using spectral theorems. It seems to me that there does not exist a Taylor expansion for the exponentiation here i.e. we cannot write
\begin{equation}
 \exp(iAt) = \sum_n \frac{(iAt)^n}{n!}.
\end{equation}
However, in quantum mechanics, people always consider the case of Taylor expanding to obtain a perturbation theory. How is perturbation theory consistent with the spectral theory? Take, for example, the following simple (1+1)D Schrodinger Hamiltonian:
\begin{equation}
H = \frac{\hat{p}^2}{2m} + V(x) =: H_0 + V,
\end{equation}
where $\hat{p} = - i \hbar \partial_x$ and some (unbounded) potential $V(x)$. How could we justify the expansion of an $U$-matrix:
\begin{equation}
 U(t) := \exp(i H_0 t /\hbar )\exp(-i H t / \hbar) \to \sum_n \frac{(iVt/\hbar)^n}{n!},
\end{equation}
in general?
Answer: Indeed, neither the exponential series nor the limit definition of the exponential $e^{tA}=\lim_{n\to\infty}(1+\frac{t}nA)^n$ makes any sense for unbounded operators. However, if $A$ is the generator of a strongly continuous one-parameter semigroup (e.g., a self-adjoint Hilbert space operator) the "exponential" can be evaluated via the Post-Widder Inversion Formula:
$$
e^{tA}=\underset{n\to\infty}{\operatorname{s-lim}}\Big({\bf 1}-\frac{t}nA\Big)^{-n}=\underset{n\to\infty}{\operatorname{s-lim}}\Big(\Big({\bf 1}-\frac{t}nA\Big)^{-1}\Big)^n
$$
Here $({\bf 1}-\frac{t}nA)^{-1}$ is defined via the resolvent formalism, and $\operatorname{s-lim}$ is the limit in the strong operator topology,
cf., e.g., Corollary 5.5 in Chapter III of Engel & Nagel's, One-Parameter Semigroups for Linear Evolution Equations (2000).
It turns out that the resolvent is the key to characterizing pertubations of (unbounded) generators. From here on out I'll be a bit sloppy with notation and assumptions---in an attempt to get the concept across a bit better.

Theorem. Given a sequence $\{(A_n(t))_{t\geq 0}:n\in\mathbb N\}$ as well as $(A(t))_{t\geq 0}$ of generators of quasi-bounded semigroups ($\|(A+z)^{-k}\|\leq M(z-\beta)^{-k}$), if $\operatorname{s-lim}_{n\to\infty}(A_n-z{\bf 1})^{-1}=(A-z{\bf 1})^{-1}$ for some $z$ with $\operatorname{Re}z>\beta$, then
$$
\underset{n\to\infty}{\operatorname{s-lim}}\;e^{-tA_n}=e^{-tA}
$$

This is Theorem 2.16 in Chapter 9 of Kato's Perturbation Theory for Linear Operators (1980). As an application (Example 2.22 in Ch.9) Kato shows that
given $H$ self-adjoint and $V$ symmetric and $H$-bounded, for all $\varepsilon$ from a sufficiently small neighbourhood of zero one obtains a Taylor(-esque) expansion
$$
e^{it(H+\varepsilon V)} u = e^{itH} u + \varepsilon u^{(1)} (t) + \varepsilon^2 u^{(2)}(t) + o(\varepsilon^2)
$$
where $u^{(1)}(t)=-\int_0^te^{i(t-s)(H+\varepsilon V)}iVe^{is(H+\varepsilon V)}u\,ds$ (and $u^{(2)}$ similarly).


moving a drupal website to another server

Question: I've been working on a drupal website in a server for months but after moving it to another server and url some of the files are not read. for example it seems that it does not read colors.css or all of the image files used in the site.
for more clarification I must add that there is a joomla site on www.example.com then I've put my drupal site on  www.example.com/test this is the site which does not work properly.
and before that it had been working on www.something.com/test/test1.
I don't have a clue why it has happend! help
Comment: Do you have any errors or warnings on the status report page (admin/reports/status)?
Comment: Also grep /test/test1 in the site folder to see if any code is using such path. You can check this in Mysqldump output also
Comment: give us more details
Comment: rooby I can not login because when I enter username and password and click on the login bottom I redirect to the front page of www.example.com but my site is on www.example.com/test. at the end I won't log in!
Comment: @AgA as I am watching my html code by inspect element all the paths are true! I mean it is reading for example images from a valid directory in both sites!  just some charachteres at the end are different! for example the first site add `?nnrvgq` but the second one add `?nn8ugb` to the end of file paths. I add this detail for @Drupalist too.
Answer: check the sites/default/settings.php for $base_url
it might have been set for a sub-directory install, and may still reflect the original server location.
if it wasn't set originally, setting it now may help as well.
Comment: you mean that I must set $base_url to the  www.example.com/test url? it is in comment line now!
Comment: Im not saying you have to do anything, but that could be causing the problem - the $base_url sets the base folder for the installation - it is very common to need to set this in an installation found in a sub-folder (like /test in your case). don't forget to clear the cache after
Answer: default files permission was making problem. It's permission must be 755.
that was all!


"Error converting data type nvarchar to int" when executing Stored Procedure and reading return value

Question: I have a clr stored procedure which has an output value I am calling the sp from a vb.net app with the code below.  I am getting an error that says 'Error converting data type nvarchar to int.
1125092|63688|Ok' the values are exactly what I am expecting back from the sp but I can't understand why I am getting a convertion error can anyone help with this.
Code Calling the sp:
Try
            Dim CN As New SqlConnection
            CN.ConnectionString = My.Settings.myConnection
            CN.Open()

            Dim Profile As String = "40T"
            Dim StartPosition As String = "53.6924582,-2.8730636000"
            Dim Destination As String = "46.18186,1.380222"
            Dim RetValue As String = "Testvalue"

            Dim test As String ="53.816408201,-2.990582799997"

            Dim cmd As SqlCommand = New SqlCommand("GetDistanceAndTime", CN)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.CommandText = "GetDistanceAndTime"
            cmd.Parameters.AddWithValue("@Profile", Profile )
            cmd.Parameters.AddWithValue("@StartPosition", StartPosition)
            cmd.Parameters.AddWithValue("@Destination", Destination)
            cmd.Parameters.AddWithValue("@Result", SqlDbType.NVarChar )
            cmd.Parameters("@Result").Direction = ParameterDirection.Output
            Try

             cmd.ExecuteNonQuery()
                RetValue = cmd.Parameters ("@Result").value
                If RetValue .Length > 2 Then

                End If
            Catch ex As Exception
                MessageBox.Show(Err.Description)
            End Try
        Catch ex As Exception
            MessageBox.Show(Err.Description)
        End Try

The clr sp code:
public partial class StoredProcedures
{
    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void SqlStoredProcedure1(SqlString Profile, SqlString StartPosition, SqlString Destination, ref SqlString Result)
    {
        SqlString result;
        try
        {
            SqlPipe sqlPipe = SqlContext.Pipe;

            result = DistanceRequest.InvokeService(Profile.ToString(), StartPosition.ToString(), Destination.ToString());
            Result = result.ToString();
            SqlContext.Pipe.Send(result.ToString());
        }
        catch (Exception ex)
        {
            result = ex.Message;
            SqlContext.Pipe.Send(result.ToString());
        }

    }
}

Oddly when I execute the stored procedure from sql server manager it compiles the return value as an int!  I am even more baffled.
USE [myDB] GO

DECLARE @return_value int, @Result nvarchar(50)

EXEC @return_value = [dbo].[GetDistanceAndTime]
  @Profile = N'40T',
  @StartPosition = N'53.6924582,-2.8730636000',
  @Destination = N'46.18186,1.380222',
  @Result = @Result OUTPUT

SELECT  @Result as N'@Result'

SELECT  'Return Value' = @return_value

GO

Comment: Please always post the full and exact error message, not just part of it. Thanks.
Answer: The main problem is with this line:
AddWithValue("@Result", SqlDbType.NVarChar )

The problem is that SqlDbType.NVarChar is an enum yet is being used incorrectly here as the "value" to imply the datatype from. The datatype is, by default, int. This is why you are getting an error converting the stored procedure's Result parameter into the int that was declared for the SqlCommand parameter via the line shown above.
A secondary problem is:
While I have nothing against calling Web Services from within SQLCLR, it should only be done when the process is wholly contained within T-SQL. If you are in .NET code already, it makes no sense to execute a stored procedure only to call a Web Service. That is highly inefficient, for both the app layer and the DB layer.
Several other things:

Your .NET code returns void from the method, so no use capturing @return_value
Don't use the AddWithValue method. Instead, create each parameter with the correct datatype and then set the value, then add it to the Parameters collection.
Even though this was already mentioned as the problem, regarding creating parameters like this in the future, even using Add:
AddWithValue("@Result", SqlDbType.NVarChar )
When setting the datatype to a string type, be sure to also set the maxsize / length
This could be a problem:
RetValue = cmd.Parameters ("@Result").value
SqlParameter values are of type object. So either cast to string or add .ToString() to value.
You don't need to call ToString() on the SqlString input parameters. All Sql* types have a Value property that returns the value in the expected .NET type. Meaning, Profile.Value returns the string you are looking for.
You create SqlPipe sqlPipe = SqlContext.Pipe; but never use it. You could use it in both places that you are doing SqlContext.Pipe.Send(). So, instead use: sqlPipe.Send().


How to find the rotation vector by deriving the final vector with respect to the displacement?

Question: My understanding of a rotation of a vector can be done by using a 2D rotation matrix as shown below,
$R(\theta )=\begin{bmatrix}\cos \theta &\sin \theta \\-\sin \theta &\cos \theta \\\end{bmatrix}$.
This rotates column vectors by means of the following matrix multiplication,
$\begin{bmatrix}x'\\y'\end{bmatrix} = \begin{bmatrix}\cos \theta &\sin \theta \\-\sin \theta &\cos \theta \\\end{bmatrix}\begin{bmatrix}x\\y \end{bmatrix}$
For example, if you rotate the vector
x=$\begin{bmatrix}1\\1 \end{bmatrix}$ by 45 degrees (clockwise), then the new vector is  $\begin{bmatrix} \sqrt2 \\ 0 \end{bmatrix}$.
Other Method:
If I have only initial and final coordinates of the vectors

The initial vector is, V = $\begin{bmatrix}1\\1 \end{bmatrix}$ and the final vector is, v = V+d = $\begin{bmatrix} \sqrt2 \\ 0 \end{bmatrix}$.
The displacement between these vectors is d = $\begin{bmatrix} \sqrt2-1 \\ -1 \end{bmatrix}$.
Can I derive the final vector v with respect to displacement $\frac{\partial{v}}{\partial{d}}$ to get the rotation vector?
[but returns a identity matrix]
If so, does $\frac{\partial{v}}{\partial{d}} * d $ can be used to cross-check?
Comment: I have asked for the last part of the question (cross-check) in here     https://math.stackexchange.com/questions/3177310/how-to-cross-check-the-derivative-of-a-vector
Comment: A minor point that I initially found a bit confusing, and which other people might as well, is that your drawing of the initial vector is of $(-1,-1)$, not $(1,1)$. The arrow should be on the upper right part, not the lower left.
Comment: Thanks @JohnOmielan. I have corrected the drawing.
Answer: The rotation vector transforms the initial vector to the final vector. These operations are carried out in polar space, making it non feasible to conduct using differentiation.


Writing a duplet in Lilypond

Question: How can one write a duplet consisting of two quarter notes equal to a dotted-quarter note in 6/8 time? Like this:

I tried \tuplet 2/1. but the . is not accepted.
And if I write this:
\tuplet 2/3 {<mi la>4 <mi la>} <re sol>4 <sol si>8 <sol si>8

I get this:
Comment: Your first example was quite different.  This one has no time signature, so we'd have to guess that it's   6/8.  So long as you *are* working in some multiple of 3/8, a   Duplet makes sense.   If you are in  X/4,  use Richard's setup.
Comment: Ah, yes, my bad, wanted to write 6/8.
Answer: The fix here would be \tuplet 2/3 { c8 c }, which tells LilyPond "write two eighth notes in the span of three." This gives:

But I would personally find this confusing for a few reasons, so I might suggest instead something like c8 c c c8~ c16 c~ c8, which gives:

Edit: Now that I see the example, I wonder if there's an error in the original that you're copying. A duplet should be two units played in the duration normally occupied by three units, so the score example you're working with, in my opinion, should be written as \tuplet 2/3 { <e a>8 q } <d g>4 <g b>8:

But if we really want quarter notes there, thankfully LilyPond allows us to alter the actual durations; just "multiply" the note values by the given amount. So now we can use \tuplet 2/3 { <e a>4*1/2 q } <d g>4 <g b>8 to notate quarter notes, but the *1/2 means that they only occupy half that value (that is, eighth notes) in the score:
Comment: Since my first reaction to the top form is (since this is the first measure) that it's starting on the off-beat of One and is purely eighth-notes, I definitely prefer the second.
Comment: I updated my question because on the partition I got you can see there are two quarter notes in the duplet.
Comment: So, what I want is: write two quarter notes in the span of one and a half".
Comment: @Lhooq Hopefully the edit is more helpful!
Comment: I must admit I was kind of confused with these two quarters note but, sir, you rock with your complete answer!
Comment: Anytime, happy to help!
Comment: You could also use dotted eighth notes.  This seems to me much less confusing than the eighth-to-sixteenth ties, especially where the tie conflicts with the barring.

Writing a duplet in Lilypond


Expected number of steps to absorbtion - Markov chain

Question: I want to calculate the expected number of steps (transitions) needed for absorbtion. So the transition probability matrix $P$ has exactly one (lets say it is the first one) column with a $1$ and the rest of that column $0$ as entries.
$P = \begin{bmatrix} 1 & * & \cdots & * \\ 0 & \vdots & \ddots & \vdots \\ \vdots & & & \\ 0 & & & \end{bmatrix} \qquad s_0 = \begin{bmatrix} 0 \\ \vdots \\ 0 \\ 1 \\0 \\ \vdots \\  \\ \end{bmatrix}$
How can I now find the expected (mean) number of steps needed for the absorbtion for a given initial state $s_0$?
EDIT: An explicit example here:
$P = \begin{bmatrix} 1 & 0.1 & 0.8 \\ 0 & 0.7 & 0.2  \\ 0 & 0.2 & 0 \end{bmatrix}
\qquad s_0 = \begin{bmatrix} 0 \\ 0 \\ 1 \end{bmatrix} \implies s_1 = \begin{bmatrix} 0.8 \\ 0.2 \\ 0 \end{bmatrix} \implies s_2 = \begin{bmatrix} 0.82 \\ 0.14 \\ 0.04 \end{bmatrix} \ldots $
Comment: Sorry, can you explain that? (Perhaps I used the wrong words, since english is not my native language.) I mean the matrix where each entry $p_{ij}$ is the probability for getting from $j$ to $i$. So if you mulitply the initial vector with that matrix you get a new vector with a probability for each state (the distribution at that point of time). So the columns should add up to 1. (Right?)
Comment: But if that is so, your second column, as I can understand, has each entry 0. Then how can its elements add to 1? I think it is better, if you write down the matrix more explicitly.
Comment: @SomabhaMukherjee I hope I could make it clear now. $(P)_{1,3} = 0.8$ which the probability for the transition $\#3 \rightarrow \#1$.
Comment: This is not a transition probability matrix.
Comment: The rows of a transition matrix must add to 1!
Answer: We conventionally read row by row, but it appears that in your matrix, the columns add to 1 instead.
I'll write your matrix like this and proceed:
\begin{align*}
  P =
\begin{bmatrix}
0 & 0.2 & 0.8 \\
0.2 & 0.7 & 0.1  \\
0 & 0 & 1
\end{bmatrix}
\end{align*}
which is of the form
$$P=\left[
\begin{array}{c|c}
Q & R \\ \hline
0 & 1
\end{array}\right]
$$
and
\begin{align*}
  s_0 = \begin{bmatrix} 1 & 0 & 0  \end{bmatrix}
\end{align*}
For the expected time to absorption, we need to calculate
$\left(I-Q\right)^{-1} \cdot \begin{pmatrix}
1 \\
1
\end{pmatrix} \approx \left(\begin{array}{r}
1.92307692307692 \\
4.61538461538461
\end{array}\right)
$
Comment: Thank you very much for your answer! I am sorry for the confusion, as I had never proper education in probability theory I was not aware of this convention. (I think in linear algebra at least it is more common to use column vectors, thats why I wrote it down that way.)
Comment: You are welcome! I think I read about markov chains first from [this book](http://www.dartmouth.edu/~chance/teaching_aids/books_articles/probability_book/amsbook.mac.pdf). You may find it interesting too! (I see that book has become popular from the returned search result for "matkov chain")
Comment: Oh thank you very much, was not able to read the whole thing right now but it seems to be written in a way even I can understand=)
Comment: Welcome again, have fun!
Answer: Let $t_i$ denote the mean time to absorption at state $0$ starting from state $i$ then $t_0=0$, one is after $t_2$, and the usual one-step Markov property yields
$$t_2=1+\frac15t_1,\qquad t_1=1+\frac7{10}t_1+\frac15t_2,$$ which is solved by
$$
t_2=\frac{25}{13}=1.923\ldots
$$
For $n+1$ states, one gets an $n\times n$ affine system whose solution $(t_i)$ describes the mean times to absorption at state $0$ starting from each state $i\ne0$.
Comment: Thank for you answer, these two equations just gave me more valuable insight in how to solve this!


Find the limit $ \frac{(\arctan(2 - x) + \sin(x - 2)^2)}{ x^2 - 4}$ when x -> 2

Question: We have to use the $o(\text{o small})$ notation while solving this problem
Answer: Let $y=x-2 \to 0$ then
$$\lim_{x\to 2}\frac{\arctan(2 - x) + \sin(x - 2)^2}{ x^2 - 4} = \lim_{y\to 0}\frac{\arctan y + \sin (y^2)}{ y(y+4)} $$
and

$\arctan y = y+o(y^2)$
$\sin (y^2) = y^2+o(y^2)$
Comment: I would be more precise and write the error terms as $O(y^3)$ and $O(y^4)$. Actually, in this problem, all you need is $o(y)$.
Comment: Yes of course we can also use Big-O or o(y) to conclude but I prefer expand to the same order. The result follows without any additional difficulty.


Monitoring a device's download/upload usage on linksys router?

Question: I have multiple computers and 2 PS3's all using the same linksys router(one wireless). Is there a way to log how much each device downloads/uploads for a give period of time?
We still have download limits here in Belgium so I'm trying to see what really hogs up most of my connection out of all of these ...
I have a WRT54GS model.
Answer: If the default Linksys firmware on your router doesn't have the features you need, you might want to look at DD-WRT, or another open-source replacement firmware. These can enhance your router with many feature you don't get off-the-shelf. Check out BWlog for DD-WRT, not sure if it monitors per-host, but it might be a good place to start.
Answer: Unfortunately, all the 3rd-party firmwares for the WRT54GS model such as DD-WRT and Tomato will only monitor bandwidth for all ports combined.  You can possibly monitor each individual IPs with the Tomato or DD-WRT firmware via command-line by modifying the IP Tables with a counter and use a computer to collect the information via SNMP from your router.  You can use Gargoyle Router firmware which will monitor each port separately; however, the firmware is only for WRT54GL model.
Answer: I'm not sure this is possible on the WRT54GS model using native firmware.  I would investigate upgrading your firmware using http://dd-wrt.com to do this monitoring.
The other possible solution is to turn on the router's quality of service options to prioritize the content you desire. That way the PS3's can do their thing and just be scaled back only when something more important needs to be done.


IE9 sending HTML in button value from JSF to the server

Question: My Environment:

Primefaces 4.0 or Primefaces 5.0
JDK 1.7
Jboss7.1.1
JSF 2.2
Internet Explorer 9
J2EE1.6

When a form is submitted from IE9, IE sends back HTML text of  tag as part of button value from JSF page to the JSF servlet. This is causing servlet container to behave strangely, and not call methods on managed beans to process further. This works just fine in Firefox or Chrome browsers.
Here's the button value looks like from the server log:
contentForm:saveButtonId=Save

On the form the button value is just "Save". All the buttons are coming in with above html text in value.
What have I tried so far?

If I remove button value from button using javascript onSubmit, it works.
I tried to add a servlet filter to add a servlet request class wrapper which would change the button value if it had  in it, but it does not work. My assumption is JSF framework uses default servletRequest class as opposed to my wrapper class so the cleaned up parameters values are not used.
I was hoping to subclass FacesServlet but I cannot as it is final class.

Please let me know what my options are as it is really affecting our application in production environment and I am running out of time and options.
In order to reproduce it, first IE security settings must be set properly. Our settings are:
IE9->Tools-->Internet Options-->Securty-->Local Intranet is selected
Click on sites button and Automatically Detect intranet is selected.
Click on Advanced button and add currently selected web app url to the websites list.
For eg mine was added as http :// IpAddress_Or_DomainName_Of_WebApp
If the url is not added to above list of intranet sites, then IE behaves properly. Our production url is a intranet site and has been added to the list for additional security features. I cannot change that as it is a company wide policy, but i am hoping there is something I can do from JSF or Javascript or from servlet end to fix this.
Appreciate any help in this regards.
Comment: What does this comment mean? That you solved it? If so, delete or answer it yourself
Answer: If I remember correctly, IE9 and above defaults to simulating IE8 behavior on intranet sites. If you go to Settings -> Compatibility View Settings and uncheck "Display intranet sites in Compatibility View" that should stop happening.
Comment: Thanks but that settings is disabled by our corporate IT security so I cannot change it.
Comment: Can you tell them that people won't be able to do their work properly unless they change it?
Answer: OK, I got it working. I got the answer i needed in primefaces forum. One kind respondent saved me just in time. He suggested me to add metadata header tag to force IE8 behavior. And also suggested here IE8 standards mode meta tag
Thank you all.


Solve $x^3+x+3=0$ by Galois's theory

Question:
Solve with radicals the following equation $x^3+x+3=0$, using Galois Theory.

I'm just starting learning this and I do not have many ideas.
Comment: I tried to describe the process [in this answer](http://math.stackexchange.com/a/158152/11619). I'm sure many textbooks (N. Jacobson, Basic Algebra I,  would be my recommendation) do it better and more verbosely.
Answer: The abel ruffini theorem in galois theory is not a tautology its just an implication which leads us to a contradiction. In simple terms if a polynomial is solvable by radicals then a normal extension of fields must exist and since there are some quintic equations which cannot contain a ladder of normal extensions hence we arrive at the conclusion that not all polynomials are solvable by radicals. But at no point does Galois theory promise you the existence of roots in radical if a ladder of normal extensions exists. it is not upto Galois theory to come up with such an equation(s) in radicals. The solution to the above problem will not give you any insight into how radicals are directly related to the normal extensions.But it will certainly give you a good picture about the ladder of normal extensions.That is all you would need to understand the the fundamental theorem on Galois theory.

Solve $x^3+x+3=0$ by Galois's theory


SSL, Connection is not secure

Question: I'm using https://letsencrypt.org Cert for my web. Last thing i made yesterday is to set the cert and polish nginx config.
Before i went to sleep everything was great, my Connection was secured, the "locker" near address bar was green, it said SSL by Eset.
Today after i woke up it says "Connection is not secure", please have a loot at https://extrasalty.eu
nginx config for the web:
# cat /etc/nginx/sites-enabled/extrasalty.eu
server {
       listen         80;
       server_name    extrasalty.eu;
       return         301 https://$server_name$request_uri;
}
server {
        ssl_certificate /etc/letsencrypt/live/extrasalty.eu/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/extrasalty.eu/privkey.pem;

        #listen   80; ## listen for ipv4; this line is default and implied
        listen  443 ssl default_server;
        client_max_body_size 10m;
(...)

Edit:
It seems it's being "verified" and ssl cert is showing up as secure when i log in to the forums, but before that it says "Not secure", any clue why?
Answer: Your site uses resources that are included via plain HTTP, resulting in a mixed-content warning.
When you click the SSL icon in Chrome, you can see the following information:

Following the link below and reloading the page show that the following file is included:

Comment: Thanks @RaviTezu, i fixed my issues with the ssl, thanks StephenKing for the update as well. I have question regarding posts content. What if someone will upload image to the post which will be on http, not on https (using some external mirror). SSL warning will be prompted about that, right?
Comment: Until you serve all the content(hosted on your server) via https, there won't be any errors. I think, users uploading the images via http is a different problem and I think, you are the one who controls it. I don't have much info. about it. Sorry!
Comment: How the upload is done does not matter at all for the users downloading these files. But if your site runs with SSL, the user would receive a warning during upload, if the form's action would use plain text HTTP (``). So this shouldn't really be an issue.
Comment: May 2023: had the same issue using Nginx on Ubuntu 22.* setup. This was the answer - make sure proxy is enabled on your DNS hosting site (e.g., cloudflare)
Answer: I see the "Sign in through STEAM" image and google-font are loaded via http.
Here's the copy as curl of it: curl 'http://steamcommunity-a.akamaihd.net/public/images/signinthroughsteam/sits_large_noborder.png' -H 'Accept-Encoding: gzip, deflate, sdch' -H 'Accept-Language: en-US,en;q=0.8' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36' -H 'Accept: image/webp,image/,/*;q=0.8' -H 'Cache-Control: max-age=0' -H 'If-None-Match: "16e5-4d62d40919e00"' -H 'Connection: keep-alive' -H 'If-Modified-Since: Wed, 20 Feb 2013 19:49:12 GMT' --compressed
Serve it via https and the error will be gone.
Basically, change the src in the img tag to https and also, load the google-font via https. Give it a try.
Comment: Errors:

Mixed Content: The page at 'https://extrasalty.eu/' was loaded over HTTPS, but requested an insecure stylesheet 'http://fonts.googleapis.com/css?family=Source+Sans+Pro:200,300,400,600,700,200italic,400italic,600italic'. This request has been blocked; the content must be served over HTTPS.

(index):159 Mixed Content: The page at 'https://extrasalty.eu/' was loaded over HTTPS, but requested an insecure image 'http://steamcommunity-a.akamaihd.net/public/images/signinthroughsteam/sits_large_noborder.png'. This content should also be served over HTTPS.

SSL, Connection is not secure


How to `setUp` a `WidgetTester` for Multiple Tests in Flutter

Question: 1. The Problem
The testWidgets function is apparently only a subcase of the test function.
A use case I'm trying to solve right now is to pump the same widget for multiple testWidgets, a setUp for multiple testWidgets. However, how can I do this if it creates a new instance inside each test?
I've tried to initialize a WidgetTester outside the tests, in the main(), but WidgetTester has only a private constructor:
class WidgetTester
  extends WidgetController
    implements HitTestDispatcher, TickerProvider {
  WidgetTester._(TestWidgetsFlutterBinding binding) : super(binding) {
    if (binding is LiveTestWidgetsFlutterBinding)
      binding.deviceEventDispatcher = this;
}

I don't quite get how the Flutter team made this work, but initializing a WidgetTester in the same way they did inside the testWidgets function isn't working for me:
final TestWidgetsFlutterBinding binding
  = TestWidgetsFlutterBinding.ensureInitialized()
    as TestWidgetsFlutterBinding;
final WidgetTester tester = WidgetTester._(binding);

2. An Example
A simple example would be to try to break down the tests of the Flutter demo that is created with each new Flutter project from flutter create. In it, we could try to separate the initial setup test of the app from the tapping action test:
testWidgets('Initial setup', (WidgetTester tester) async {
  await tester.pumpWidget(MyApp());

  expect(find.text('0'), findsOneWidget);
  expect(find.text('1'), findsNothing);
});

testWidgets('Increment the counter on tap', (WidgetTester tester) async {
  await tester.pumpWidget(MyApp());

  await tester.tap(find.byIcon(Icons.add));
  await tester.pump();

  expect(find.text('0'), findsNothing);
  expect(find.text('1'), findsOneWidget);
});

The idea would be to try to move the await tester.pumpWidget(MyApp()); into a setUp function.
Answer: Below is what looks like the current way to solve for this in Flutter.
To summarize:

Create the group(..) structure inside main()
Create your own private methods from inside that structure, for each group of testing you want.  For each of these private methods:

Pass in the WidgetTester instance
Let them be async

And then you should only have a single call to testWidgets(..)

Inside this method, is where you call the private methods you set up to distribute test logic
Call each of these with await, so they don't run concurrently

So far I didn't find a way for the output to indicate each "sub-test" it ran, so just using print(...) statements for now.
This is a demo for some QR Code logic:
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:qr_code_demo/app/appRoutes.dart';
import 'package:qr_code_demo/view/appHome.dart';
import 'package:qr_code_demo/view/qrScanner.dart';

class MockNavigatorObserver extends Mock implements NavigatorObserver {}

void main() {
  group('MainPage navigation tests', () {
    NavigatorObserver mockObserver;

    _loadAppHomeScreen(WidgetTester tester) async {
      await tester.pumpWidget(
        MaterialApp(
          routes: AppRoutes.getRouteMap(),
          home: AppHomeScreen(),
          navigatorObservers: [mockObserver],
        ),
      );
    }

    setUp(() {
      mockObserver = MockNavigatorObserver();
    });

    Future<Null> _verifyLayoutElements(WidgetTester tester) async {
      print('_verifyLayoutElements');
      expect(find.byIcon(Icons.scanner), findsOneWidget);
      expect(find.byType(FloatingActionButton), findsOneWidget);
      expect(find.byType(RaisedButton), findsOneWidget);
    }

    Future<Null> _navigateToQrScannerScreen(WidgetTester tester) async {
      print('_navigateToQrScannerScreen');

      await tester.tap(find.byIcon(Icons.scanner));
      await tester.pumpAndSettle();

      verify(mockObserver.didPush(any, any));

      expect(find.byType(AppHomeScreen), findsNothing);
      expect(find.byType(QrScannerScreen), findsOneWidget);
    }

    testWidgets('AppHomeScreen WidgetTester', (WidgetTester tester) async {
      await _loadAppHomeScreen(tester);

      await _verifyLayoutElements(tester);
      await _navigateToQrScannerScreen(tester);
    });
  });
}

Thanks to:
https://iiro.dev/2018/08/22/writing-widget-tests-for-navigation-events/

Scroll to code for this file: test/navigation_test.dart

====
And double-thanks, because the navigation testing logic including with this example is thanks to @iiro's post: https://stackoverflow.com/a/51983194/2162226
Here is the appRoutes.dart file:
import 'package:qr_code_demo/view/appHome.dart';
import 'package:qr_code_demo/view/qrScanner.dart';

class AppRoutes {

  static const String AppHome = 'AppHome';
  static const String QrScanner = 'QrScanner';

  static String initialRoute() {
    return AppHome;
  }

  static getRouteMap() {

    return {
      AppRoutes.AppHome: (context) => AppHomeScreen(),
      AppRoutes.QrScanner: (context) => QrScannerScreen()
    };

  }
}

Answer: The answer is setUpAll() function. Use it first in the main method, outside of group and it runs only once.
Comment: Ok, but how to obtain `tester` in `setUpAll()` ?


file_create_url() equivalent in a javascript file?

Question: I am dynamically loading a script using $.getScript() which works fine, but when using the CDN module it comes from the webserver, instead of the CDN. Is there a way to get the correct CDN path in JS, similar to how file_create_url() works?
Answer: CDN has already done its magic by the time the JavaScript file has been prepared for output, and there are no AJAX callbacks allowing it to do its magic from the JS file itself.
If you do know ahead the name of the scripts you might load (or you can iterate from your theme or module) then the trick is to inject those paths as variables directly in your theme using inline JavaScript. For example, from your module or your theme's hook_html_head_alter method in template.php, to add one file (the method would be the same for a list of files, but an array would best be used):
<code>drupal_add_js('var my_js_file_1 = "' . file_create_url(drupal_get_path('theme', 'my_theme') . '/js/file_1.js') . '";' , 'inline');
</code>
then you can call
<code>$.getScript(my_js_file_1);
</code>
in your code.
The magic is done through the file_create_url, which is what the CDN module processes.


Cloning a VPS into a local VM?

Question: I've got a fun challenge for you all.  A company I'm working with is setting up its development/release workflow and environments.  The eventual production environment is a VPS hosted with a popular host running CentOS within a Virtuozzo virtualization environment.
We'd like to install a virtualized instance of the production environment (or something close to it) to each developer's local system.  This will allow each developer to test changes locally on his/her own system without tainting anyone else's system when things go wrong.
Clearly, there are many ways to do this, but I imagine there's a specific approach that is superior.  This is where I need your help.
So far, the best options seem to be as follows.

Export/Back up the Virtuozzo VM.  Download it to a local system and install it into a Virtuozzo Container.
This is the cleanest/purest solution, but it may not work.  The cloned VM would still have all of the network configuration from the VPS' network, for example.  Would it be hard to write a script to update these (and any other old configuration values) so they are more appropriate for the local VM?
Deploy the codebase to a similarly configured VM created in an arbitrary virtualization product.
This approach isn't as clean, but I know it can work (I've done it before).  For example, we could install the same version of CentOS, Apache, MySQL, etc. to a local VMWare VM.  The environment wouldn't be identical to the production environment, but it might be close enough to make this workflow feasible.
Another option?
What other options do you think there may be?  Or, is one of these options the best?  I'd love to hear your thoughts! :)

Edit:
Multiple cPanel/user accounts on the production server
I contacted our VPS' customer support to see if I could obtain access to the Virtuozzo backups; he denied our request but suggested that we might try creating a separate cPanel/user account for each developer.  This would allow the environment to be identical to that of the server, but would be a little more inconvenient since the environment would still be running remotely instead of locally.  Still, a decent option.
Answer: This will work perfectly for you ...
On the source VM instance ...
sudo su
cd /
tar cvpzf backup.tgz --exclude=/proc --exclude=/lost+found --exclude=/backup.tgz --exclude=/mnt --exclude=/sys /

On the target machine (dedicated or VPS)
tar xvpfz backup.tgz -C /

And make sure any dirs excluded are re-created
mkdir proc
mkdir lost+found
mkdir mnt
mkdir sys

You could just create a nice Microsoft VirtualPC Linux image and hand it out to everyone
Comment: Have you done this before?  This seems so simple, but sometimes the simplest solutions turn out to be the most reliable/elegant...
Comment: A few times actually. With the exception that we did it with dedicated servers. The only thing you need to bear in mind is the contents of /etc/fstab and /boot/grub/menu.lst - as these will be platform dependant.
Comment: The fact you can do it on a running server is the best bit :)
Comment: Fascinating.  This is one of those things for which there are MANY solutions, and yours is quite intriguing.  Thanks so much for your response. :)
Comment: Just as a follow-up - it has been a few years, and we actually went with the "similarly configured VM" approach described in my question.  I don't recall exactly why we did - I think I might have had a hard time getting the archived files to execute in the VM - you know, different platform and all from the actual server.  Or, maybe I had a hard time creating the archive.  Regardless, it's good to know that this works well on actual dedicated servers!  Thanks again for your answer.

Cloning a VPS into a local VM?


Image overlay turning on before mouse goes over the image

Question: I would like to understand why when I place my mouse anywhere on the page, the hover effect applies to the image, instead of activating when the mouse is placed directly on the image itself. I've tried changing the width and height of the main container, but that doesn't seem to fix it.
<https://codepen.io/jl88s/pen/oawOOM?editors=1100>

Comment: Please post a minimal viable code snippet using the tools available. Feel free to post a working link to codepen with a code snippet in the question itself. StackOverlow also allows for a tool to get a runnable snippet, which in a lot of cases remove the need for codepen.
Comment: Questions seeking debugging help must include the shortest code necessary to reproduce it **in the question itself.** NB - **Please don't abuse the code blocks to get around this requirement**.
Answer: My first solution causes another problem. The overlay disappears when you try to hover button. Sorry about that. Check my new solution.
Here is the css :
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
.container {
    width: 400px;
    margin-left: auto;
    margin-right: auto;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    position: relative;
}

.image {
    width: 400px;
    height: 400px;
    display: block;
}

.middle {
    transition: 2s ease;
    position: absolute;
   text-align: center;
    opacity: 0;
}
.container:hover .image {
    opacity: 0.3;
}

.container:hover .middle {
    opacity: 1;
}

.text {
    background-color: orange;
    color: white;
    font-size: 1em;
    padding: 16px 32px;
    text-transform: uppercase;
}

Answer: You have this problem because you set the :hover selector on your container, which takes the full height and width. So at the moment you enter the window, the :hover is triggered.
Set your container to display: inline-flex, remove the height and then postion the container where you need it relatively to its
parent.
Didn't add a parent, and did not position the container in the example, tell me if you want me to.
Edit
Added the centering in this example. (See html and body styles)
html, body {
    height: 100%;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
.container {
    display: inline-flex;
    justify-content: center;
    align-items: center;
    position: relative;
    margin: auto;
}

.image {
    width: 400px;
    height: 400px;
    display: block;
}

.middle {
    transition: 2s ease;
    position: absolute;
    text-align: center;
    opacity: 0;
}

.container:hover .image {
    opacity: 0.3;
}

.container:hover .middle {
    opacity: 1;
}

.text {
    background-color: orange;
    color: white;
    font-size: 1em;
    padding: 16px 32px;
    text-transform: uppercase;
}


Voice leading rule hidden fifths/octaves

Question: Good afternoon everyone!
Sometime 2 years ago I started practicing voice leading for 4 voices, but since I had to spend all my time on school I kind of stopped halfway. Since I graduated I've picked up practicing it again from the beginning (using a book) and I'm somewhat confused about the rule regarding hidden fifths/octaves.
I have to harmonize connections on I - IV - V in root position, but I feel like it's nearly impossible to avoid hidden fifths/octaves at all. The book stated that they are OK in the outside voices if the interval in the bass is bigger than the interval in the soprano, but if they appear between the other voices, "the ear has to decide" if it's acceptable or not. Now my musical ear is developed pretty well but I think this rule is a bit too vague since what one thinks is OK, the other may disagree.
So my question: What exactly are the rules stating whether hidden fifths/octaves are acceptable or not?

This is a little exercise I did yesterday. You get a given bass in root position and you have to harmonize the chords. I haven't checked the exercise entirely yet, but as you can see, there's a hidden octave between bar 2 and 3 between the bass and alto going from A - C to D - D. I could make the alto go from C to A below instead to avoid that, but then there would appear a hidden fifth between the tenor and alto and I believe that's worse than the current situation I'm having now. As far as I can remember my music teacher once said that they are allowed, AS LONG as the lower voice has a bigger interval than the upper voice. Can somebody confirm this?
(PS. Don't mind the erase marks etc... It's difficult focussing at 1 AM... ^^)
Comment: If you want to keep the current soprano line, I would say that you're better off (by classical rules) in keeping the middle voices higher- then you don't run into these problems.  By half notes, if the tenor and alto voices at the beginning are are e-a, d-f, b-e, c-e, then there's no problem.  As a general rule, if you keep to contrary motion (as far as possible) between the outer voices, and keep the top three voices close, most such voice leading problems will take care of themselves.
Comment: Yeah I figured out that, since these are the first exercises, it's better to keep it simple and boring for the sake of really memorizing the rules and once I start working out inverted chords, I have more possibilities
Comment: From a purely *musical* point of view, I would be more concerned that the bass and tenor are separated entirely by unisons and 5ths, except for one third. Remember the interval between bass and tenor can exceed an octave, and give yourself more space for the upper parts to move in.
Comment: You are right that IV-V in root position is always problematic, and even more so in the minor key because of avoiding augmented second interval, unless you are using the melodic minor scale where both chords can be major, or both minor. It's worth spending a bit of time systematically writing out *every* possible IV-V progression that follows all "the rules".
Answer: A hidden fifth occurs when an implied consonant passing note would create a parallel fifth.  A hidden octave occurs when an implied consonant passing note would create a parallel octave. So in your example, the motion from A to D implies a consonant passing tone A-C-D, and of course C-D and C-D creates a parallel octave.
Hidden fifths and octaves are admittedly a gray area, and depend on context.  For example, if you are writing an eight-part piece for double choir, hidden octaves and fifths are perfectly acceptable, but if you are writing a three-part piece, they are completely unacceptable.
In four-part harmony, generally speaking, they are allowed between middle voices.  Honestly they only become an issue when they occur between soprano and bass and the the soprano is the voice with the implied passing tone. Sticklers may point out your hidden fifth/octaves between the bass and soprano when the bass is making the leap, but you can see them all the time in masterworks.
You can avoid hidden fifths or octaves with
(a) Unusual doublings, e.g. omit the fifth in one of the chords and double the third, or
(b) Contrary motion.  If the consonant passing tone results in an antiparallel octave or fifth, it is OK. When moving, for example, from IV to V, it is common for the bass to ascend and all other voices to descend.
You could also avoid them by using an anticipation or suspension. This is sort of cheating, but there are examples of Bach doing it so it must be OK, right?
Comment: By "consonant" you mean "diatonic", right?
Answer: Wow, you are worrying too much XD
The canonical answer is:
A hidden octave or hidden fifth is forbidden only if the octave or (perfect) fifth is formed by the Soprano and another voice, and that the Soprano is jumping (i.e. adjacent notes do not form a second, but is a third or further away). This is because, in such cases, hidden octaves and fifths are too conspicuous. In your case, both voices are inner voices, the the octave is allowed, and in fact is completely normal.
If you are composing real music, the more important consideration would be the style of your music. If you are not composing common period music, the problem vanishes XD
Answer: It depends! Your fourth up in the bass and second up in the alto could be a permitted "horn fifth" or may be absolutely forbidden, depending on the style of counterpoint. Here's Schoenberg on the topic, from "Theory of Harmony" page 61 and following:

Not kennt kein Verbot! - Necessity knows no law!

...

Hence, exceptions were established, declaring: Hidden octaves and
  fifths are permissible provided they occur between two middle voices,
  or at most between a middle and an outer voice, so that they attract
  less attention. And: hidden octaves are better when one of the two
  voices moves by step and the other leaps a fifth or a fourth. Or: it
  is 'least objectionable' when the upper voice is the one that moves by
  step, say, from the seventh to the octave, or from the third to the
  fourth.

(There is also the "Mozart fifth" but Mozart apparently mostly used parallel fourths instead of parallel fifths for those...)
Comment: It should be treated as the same level of issue regardless of style as counter point is the study of Independence between voices. So hidden octaves and horn 5ths will weaken the independence of the lines which is sometimes desired, but specifically not in the study of counterpoint. While hidden octaves aren't necessarily a show stopper in every case, the do weaken the Independence noticeably.


Upgrade Openstack production grade system from Pike to Victoria or some stable newer version

Question: We are planning to upgrade our Openstack production environment from from Pike to Victoria version.

To upgrade what should be strategies?
What would be upgrade paths like Pike-> queens -> Rocky -> Stein -> train -> Ussuri -> Victoria
Is upgrade from Pike to Victoria version possible in one upgrade process.

We are using the open community version of Openstack version over Centos.
Please provide the inputs how should we proceed.
Answer: It's definitely possible to upgrade from Pika to Victoria, but it depends heavily upon your setup and your requirements. If you need to upgrade with zero downtime, or use a lot of custom options (e.g. 3rd party drivers, neutron-lbaas) it will be more difficult.
These are all too complicated to cover in a single Stackoverflow post and I would recommend that you hire a specialist, or ask on IRC or the official mailing lists for advice.
To address your individual points.

Depends heavily on your setup and requires a lot of research to determine the best strategy.
That is the recommended upgrade path. At the very least the database steps should be completed in that fashion.
Upgrading is for sure possible, but depends heavily on your environment and your requirements.

If your environment is small it might be worth to just build a new environment and manually migrate your workloads over.


Function searching by name and country

Question: It is working when i change it to search by points or some other integer, but I have to make it to search by name and country. The function is checking the array which already has data in it.
void search (competitor competitors[], int number)
{
if(number==0)
{
    cout<<"\n Array is empty!!!\n";
    return;
}

char country[50];
char name[50];
char choice;
bool flag;

do{
    cout << "\n\n Enter country: " << endl;
    cin >> country;
    cout << " Enter name: " << endl;
    cin >> name;

    flag = false;

    for(int i=0; i<number; i++)
    {
        if( country==competitors[i].country && name==competitors[i].name)
        {
            count << " Competitor found!" << endl;
        }
    }
    if (flag == false)

    cout << " Want to search again(Y/N)?: ";
    cin >> choice;

}while(choice == 'Y' || choice == 'y');

}
Comment: Besides incorrect use of the equality operator, why would anyone be using raw char arrays in C++ for user-inputted data? Just use an `std::string` and you've both fixed the buffer overflow and now have a convenient equality operator that actually compares string content.
Comment: Can you confirm type of `competitors[i].country` and `competitors[i].name` ?
Answer: I am assuming that competitors[i].country (name) is a char[].
You cannot compare char[] arrays using == (that will compare the base addresses of the array, not the content), you must strcmp(). Or, as this is C++, use std::string instead of char[] and use ==.
Comment: Yup, it was a `char[]`. Thank you. I had no idea I can not compare them.

Function searching by name and country


Scraping data in element using beautifulSoup

Question: I am doing web scraping using beautifulSoup. I managed to scrape the name, however the problem is, I don't really sure how to scrape if the data is in the elements for example the phone number and email are located as in the pictures below:

My code:
 import requests
    from bs4 import BeautifulSoup

    raw = requests.get('https://www.iproperty.com.my/property/findanagent.aspx?ty=as&ak=&rk=&pg=1&rmp=10&st=KL&ct=&st1=&ct1=#40091').text
    raw = raw.replace("
", "")

    soup = BeautifulSoup(raw, 'html.parser')

    import re

phone = ['data-content'])[0][1:][:-1] for d in soup.find_all('a',{'class':'csagentphonelead'})]
    name = [x.text.strip().split("\r\n")[-1].strip() for x in soup.find_all("p", class_='box-listing_agentCS')]
    website = [x.text.strip().split("\r\n")[-1].strip() for x in soup.find_all("a", class_='csagentemaillead')]

    num_page_items = len(name)
    with open('results180.csv', 'a') as f:
        for i in range(num_page_items):
        f.write(name[i] + "," + phone[i] + "," + website[i] + "," + "\n")

My results from scraping is "Click for Email" and "Click for Phone".
What should I fix so that the results are proper email and phone number?
Answer: You have to get the value of data attribute from the link. You can try this code -
import requests
from bs4 import BeautifulSoup

raw = requests.get('https://www.iproperty.com.my/property/findanagent.aspx?ty=as&ak=&rk=&pg=1&rmp=10&st=KL&ct=&st1=&ct1=#40091').text
raw = raw.replace("
", "")

soup = BeautifulSoup(raw, 'html.parser')

import re
#['data-content'])[0][1:][:-1] ## note sure what is this
# for d in soup.find_all('a',{'class':'csagentphonelead'}):
name = [x.text.strip().split("\r\n")[-1].strip() for x in soup.find_all("p", class_='box-listing_agentCS')]
phone = [x['data'].strip().split("\r\n")[-1].strip() for x in soup.find_all("a", class_='csagentphonelead')]
website = [x['data'].strip().split("\r\n")[-1].strip() for x in soup.find_all("a", class_='csagentemaillead')]

num_page_items = len(name)
with open('results180.csv', 'a') as f:
    for i in range(num_page_items):
        f.write(name[i] + "," + phone[i] + "," + website[i] + "," + "\n")


android adding to spinner causes crash due to filenotfound exception

Question: I am using a spinner that gets it information from a text file, which is created/ modified in another activity. When I open the activity that uses this method it crashes saying the exception of filenotfound. Though i have run the activity that creates the file and added data into the file previously. I tried both in my android device and on emulator.
Here is the method that loads the data into the spinner:
public void loadOpp() throws FileNotFoundException {
    BufferedReader inputReader = new BufferedReader(new InputStreamReader(
            openFileInput("Opponent")));
    String inputString;
    List<String> final2 = null;
    try {
        while ((inputString = inputReader.readLine()) != null) {
            final2.add(inputString);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    Spinner spinner= (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, final2);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(dataAdapter);
}

and here is the method that creates the text file in the other activity:
public void onSubmitClick(View view) {
    EditText mEdit;
    String filename = "Opponent";
    mEdit   = (EditText)findViewById(R.id.oppName);
    String string = mEdit.getText().toString();
    FileOutputStream outputStream;

    try {
      outputStream = openFileOutput(filename, Context.MODE_APPEND);
      outputStream.write(string.getBytes());
      outputStream.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
}

I understand the error, but i do not understand why it would cause the error if the file was created, so maybe the file isn't being created in the first place. Any thought on how to fix this error, and how to avoid it in the future/ what am i doing wrong basically.
Comment: Maybe you should start by checking if the file is actually created by using something like if(file.exists()){//do something}
Comment: try adding the extension  of the file String filename = "Opponent.txt";
Answer: It could be that when you are creating the file your either not creating it where you think you are or you are trying to create it where there really is no directory. You could try using
if(f.createNewFile()){
    System.out.println("FILE CREATED "+f.getAbsolutePath());
}else{
        System.out.println("FILE NOT CREATED "+f.getAbsolutePath());
                     }

And that would tell you that your creation of the file failed or not


Spring's Annotation Type Required deprecation

Question: Spring's Annotation Type Required is marked as deprecated

Deprecated.
as of 5.1, in favor of using constructor injection for required settings (or a custom InitializingBean implementation)

Same for relevant RequiredAnnotationBeanPostProcessor
But it's not clear what's the replacement, it seems that it should be unavailable.
Is this change prevent us marking method as required unless it's part of constructor method ? to prevent unexpected exceptions on class creation ?
Answer: There are three ways to inject a bean via annotation:
Field injection
@Autowired
private FooService fooService;

Setter injection
private FooService fooService;

@Autowired
public void setFooService(FooService fooService) {
    this.fooService = fooService
}

Constructor injection (this is the mentioned replacement)
private final FooService fooService;

@Autowired
public MyComponent(FooService fooService) {
    this.fooService = fooService;
}

As you can see, the only way to declare your Service final is by using the constructor injection, which replaces the @Required annotation because it forces the user of your class to instantiate it with the required services. The user does not have to be Spring, it could be a simple unit test as well.
You should use constructor injection for mandatory dependencies and setter injections for optional dependencies instead of field injection.
Some reasons why:

It makes it clear to everybody which dependencies are required
It makes testing easier
You can make your objects immutable

Further reading:

http://olivergierke.de/2013/11/why-field-injection-is-evil/
https://www.vojtechruzicka.com/field-dependency-injection-considered-harmful/
https://spring.io/blog/2007/07/11/setter-injection-versus-constructor-injection-and-the-use-of-required/

Update: non-annotated constructor injection
As one commentator was wondering about a final field annotated with @Autowired while the constructor was not annotated:

If a class only declares a single constructor to begin with, it will always be used, even if not annotated.

https://docs.spring.io/spring-framework/docs/5.2.5.RELEASE/javadoc-api/org/springframework/beans/factory/annotation/Autowired.html ("Autowired Constructors")
But even though it is not necessary to annotate the constructor in this case, I would still do it: it documents the code and if anyone will ever add another (non-annotated) constructor, the code will still work.
Comment: @user3625699 if your class has only one constructor, Spring will always use it, even if it is not annotated with `@Autowired`. So, actually you're using constructor injection and annotating the field is pointless (or could even be misleading). I advise you to annotate your constructor with `@Autowired` even though Spring does not require you to do so. It documents your intention and if you or somebody else adds another (non-annotated) constructor, the code will still work
Comment: I made my user DAO marked final working although injected through field in registration service.
Constructor needs to exist.
 Does that provide a dirty way to achieve the same functionality as with annotation Autowired on constructor in the end?
Comment: I know this question is quite old but I just came across a piece of code which could be useful to what I'm working on. It has Required annotation in a non Spring managed Bean. If I replace that with constructor injection with @Autowired, I get warnings saying it must be used within a Spring managed bean.
Answer: Yes , it is deprecated but still you can use it by mentioning below in xml file.
<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor" />

Comment: I asked for replacement, so your answer is irrelevant here


Effects of ignoring the sampling strategy

Question: Does the use of calibration/post-stratification imply that the sampling strategy/stratification/individual selection probabilities can be ignored altogether?
In a survey reweighting exercise, I have population totals for a complete cross-classification of four attributes: country, age, sex, and work status. EDIT: The sampling scheme is stratified by country (about the same number of respondents per country), and within each country stratified by age and sex (very roughly uniform). The work status is not part of the sampling stratification.
My task is to produce generic weights that can be used for identifying interactions between variables (stratification-response and response-response). Estimation of population means or totals might be another application. (At least that's what I understand. To be honest, I don't really know the application -- how much does it matter?)
Question: How will the calibrated weights be affected if information on the sampling strategy is added to the calibration procedure, e.g. in the form of prior weights? Is it a problem when for some combinations of categories there are only very few respondents?
Question related to the practical exercise: https://stats.stackexchange.com/q/29132/6432
Comment: What are you doing with the data?  Are you estimating some response from the data.  Are you sampling from subgroups?  Do you know anything about the the sampling scheme (e.g. a stratified random sampling where within strata samples are not proportionals)>  No one has responded so far because the description is so vague.  Why is there a need to reweight?
Answer: There may be more advanced weighting schemes that I'm not familiar with, but this is based on simple weighting schemes I've used with small numbers of groups
How will the calibrated weights be affected if information on the sampling strategy is added to the calibration procedure, e.g. in the form of prior weights:
I don't know what this means. There are only two groups of information you need to weight the data. You have your "golden" population sizes for each group (a group being, for example, employed males in the US age 18-24, estimated from an external data source assumed to be true) and your sampled sizes for each group. By multiplying the population percentage by the sample size for each group, you will get to the intended weighted sample size. Divide the weighted sample size by the actual sample size to get at the weight to apply for that group. Easy peasy.
(I can insert an example table, but I'm not sure how to put a table in my response like I've seen before. Can somebody help in a comment?)
Is it a problem when for some combinations of categories there are only very few respondents?
Yes. There are two problems when the weight for a group is significantly greater than 1 (1 being the case where no weighting is needed).
First, it may reflect a problem in the sampling. Weighting doesn't correct for significantly different people who were excluded from the sampling frame. It assumes that the people you missed were substantially the same as the people you got. If you conduct a landline telephone survey and weight by age to correct for the under-sampling of 18-24 year-olds, that doesn't really work, because the few young people who have landlines will be significantly different on almost any metric from the majority that don't.
Second, it can overstate the reliability of a finding. If the weighted sample size for a group is 100, it can look like a certain margin of error. But, the actual margin of error is based on the unweighted sample size, which may be 50 if that group had a weight of 2. For that reason, you should report the unweighted sample size for each metric. Most survey reporting software makes it easy to report weighted metrics and unweighted sample sizes simultaneously.
Comment: Thank you for your detailed explanations. Now I see that weighting is no panacea.


Scoring consistency within dataset

Question: Suppose I am given a set of structured data. The data is known to be problematic, and I need to somehow "score" them on consistency. For example, I have the data as shown below:
fieldA | fieldB | fieldC
-------+--------+-------
foo    | bar    | baz
fooo   | bar    | baz
foo    | bar    | lorem
..     | ..     | ..
lorem  | ipsum  | dolor
lorem  | upsum  | dolor
lorem  | ipsum  | baz

So assume the first row is considered the correct entry because there are relatively more data in that combination compared to the records in second and third row. In the second row, the value for fieldA should be foo (inconsistent due to misspelling). Then in the third row, the value of fieldC should be baz as other entries in the dataset with similar values for fieldA (foo) and fieldB (bar) suggest.
Also, in other part of the dataset, there's another combination that is relatively more common (lorem, ipsum, dolor). So the problem in the following records are the same as the one mentioned before, just that the value combination is different.
I initially dumped everything to a SQL database, and use statements with GROUP BY to check consistency of fields values. So there will be 1 query for each field I want to check for consistency, and for each record.
SELECT    fieldA, count(fieldA)
FROM      cache
WHERE     fieldB = 'bar' and fieldC = 'baz'
GROUP BY  fieldA

Then I could check if the value of fieldA of a record is consistent with the rest by referring the record to the object below (processed result of the previous SQL query).
{'foo': {'consistency': 0.99, 'count': 99, 'total': 100}
 'fooo': {'consistency': 0.01, 'count': 1, 'total': 100}}

However it was very slow (dataset has about 2.2million records, and I am checking 4 fields, so making about 9mil queries), and would take half a day to complete. Then I replaced SQL storage to elasticsearch, and the processing time shrunk to about 5 hours, can it be made somehow faster?
Also just out of curiosity, am I re-inventing a wheel here? Is there an existing tool for this? Currently it is implemented in Python3, with elasticsearch.
Answer: I just read your question and found it quite interesting. I did something similar using ntlk (python Natural Language Toolkit).
Anyway, in this case I think you dont need the sophisticated string comparison algorithms.
So I tried an approach using the python difflib. The Title sounds promising: difflib — Helpers for computing deltas¶
The difflib.SequenceMatcher class says:
This is a flexible class for comparing pairs of sequences of any type, so long as the sequence elements are hashable.
By the way I think that if you want to save time you could hold and process 2.000.000  3-tuples of (relatively short) strings easily in Memory. (see testruns and Mem Usage below)
So I wrote a demo App that produces 2.000.000 (you can vary that) 3-tuples of randomly slightly shuffled strings. The shuffled strings are based and compared with a default pattern like yours: ['foofoo', 'bar', 'lorem']. It then compares them using difflib.SequenceMatcher. All in Memory.
Here is the compare code:
def compare(intuple, pattern_list):
    """
    compare two strings with difflib
    intuple: in this case a n-tuple of strings
    pattern_list: a given pattern list.
    n-tuple and list must be of the same lenght.

    return a dict (Ordered) with the tuple and the score
    """
    d = collections.OrderedDict()
    d["tuple"] = intuple
    #d["pattern"] = pattern_list
    scorelist = []
    for counter in range(0,len(pattern_list)):
        score = difflib.SequenceMatcher(None,intuple[counter].lower(),pattern_list[counter].lower()).ratio()
        scorelist.append(score)
    d["score"] = scorelist
    return d

Here are the runtime and Memory usage results:
2000 3-tuples:
 - compare time: 417 ms = 0,417 sec
 - Mem Usage: 594 KiB
200.000 3-tuples:
 - compare time: 5360 ms = 5,3 sec
 - Mem Usage: 58 MiB
2.000.000 3-tuples:
 - compare time: 462241 ms = 462 sec
 - Mem Usage: 580 MiB
So it scales linear in time and Mem usage. And it (only) needs 462 seconds for 2.000.000 3-tuple strings tom compare.
The result looks like this:(example for 200.000 rows)
[ TIMIMG ]
build                function took 53304.028034 ms
[ TIMIMG ]
compare_all          function took 462241.254807 ms
[ INFO ]

num rows: 2000000
pattern: ['foofoo', 'bar', 'lorem']
[ SHOWING 10 random results ]
0: {"tuple": ["foofoo", "bar", "ewrem"], "score": [1.0, 1.0, 0.6]}
1: {"tuple": ["hoofoo", "kar", "lorem"], "score": [0.8333333333333334, 0.6666666666666666, 1.0]}
2: {"tuple": ["imofoo", "bar", "lorem"], "score": [0.6666666666666666, 1.0, 1.0]}
3: {"tuple": ["foofoo", "bar", "lorem"], "score": [1.0, 1.0, 1.0]}
....

As you can see you get an score based on the similarity of the string compared to the pattern. 1.0 means equal and everything below gets worse the lower the score is.
difflib is known as not to be the fastest algorithm for that but I think 7 minutes is quite an improvement to half a day or 5 hours.
I hope this helps you (and is not complete missunderstanding) but it was a lot of fun to program this yesterday. And I learned a lot. ;)
For example to track memory usage using tracemalloc. Never did that before.
I dropped the code to github (as a one file gist).
Comment: i havent got time to look at the solution, can i use it to "score" entries with multiple terms? e.g. "foo bar" vs. "fooz bar"
Comment: should work as well. difflib uses hashes to compare. so anything hashable will work.
Comment: lol, doesn't look like the tool I need. Because I don't have all the possible (relatively) correct canonical values and combinations for each field before hand.
Comment: You can do the comparison exactly at that time when you would do the compare run in the DB. Just like you stated in your question. As I understand you have 2.2 Mio Datasets and a compare pattern then. Thats fine. Just read anything into Mem and run the compare function. There is no need to have anything beforehand.
Comment: not sure if it is relevant, but i have updated the question with another example
Comment: Do you compare those to another pattern or also to foo, bar, baz ?? You can justgive it a try. Select all your rows. Put them in tuples (foo,bar,baz)... make a loop and call compare(tuple, pattern_list) that'll return the dict with the scores per call. See compare function above.
Comment: so currently all records are placed in a for loop (multiprocessing TBE), so for each record, I fire an SQL shown in the question, to get a statistics of the possible values for each field, then score the respective field accordingly
Comment: So I don't have a known list of pattern, and the pattern actually only applies to the dataset only.

Scoring consistency within dataset


7 - Panels node form, file/image gets removed when field not present in the custom form

Question: I am using Panels to customise node forms by separating them into multiple pages.
On one page I have text fields and on another file/image fields, when saving the text field form variant all the files get removed from the node - if I add a field in that form variant, it will get saved (not removed).
Is this a normal behaviour?
Answer: I've also encountered that. The node form and the image field's value will be submitted as empty and resulting in as you say that the image get removed from the node.
It is an unfortunate side effect of how the form api works. Someone could try to work around it in Panels, and/or limit panels to prevent you from doing this, but both of these would be hacks on top of form api.
One workaround is adding the pane General form that you will find under Form when adding panes. Then click settings for the pane and under CSS properties set the class to "element-invisible". This way all other form elements that you have not placed as panes will be present but hidden by css.

I know it feels wrong but at least it get's the job done :)


Deveríamos utilizar todas variáveis como privadas?

Question:
Deveríamos sempre utilizar os atributos de uma classe como privados?
O que as variáveis privadas ajudam a prevenir?
Como você decide se uma determinada propriedade deve ser privada ou não? CASO, por padrão, cada campo FOR privado, então por que há membros de dados públicos em uma classe?
Em que circunstâncias uma variável deve ser tornada pública?

Pesquisando na internet, são encontrados inúmeros "tutoriais", falando como é definido um atributo do tipo private, mas  não dá um escopo de uso do atributo, quando você deve usar um e não outro.
A melhor definição que eu encontrei foi no próprio SO:

Private:
  A única classe que tem acesso ao atributo é a própria classe que o define, ou seja, se uma classe Pessoa declara um atributo privado chamado nome, somente a classe Pessoa terá acesso a ele.
Comment: Eu fiz [uma resposta](https://pt.stackoverflow.com/a/189172/5878) sobre encapsulamento abordando mais o lado de aplicação de regras de negócio aos atributos, talvez lhe é útil em algo.
Answer: O que você costuma ler são as chamadas boas práticas. A pessoa diz o que você deve fazer sem dizer porque, muitas vezes porque a pessoa nem sabe o porquê. É comum inclusive a pessoa estar só repetindo o que ela "aprendeu" sem questionar o porquê daquilo.

Deveríamos sempre utilizar os atributos de uma classe como privados?

Eu acho que não, tem gente que acha que sim. Depende de uma série de circunstâncias e da linguagem que está usando, sem falar na filosofia que você adotou.

O que as variáveis privadas ajudam a prevenir?

Previne que outras classes acessem aquela variável de forma direta, como você já sabe.

Como você decide se uma determinada propriedade deve ser privada ou não? CASO, por padrão, cada campo FOR privado, então por que há membros de dados públicos em uma classe?

Cada um tem um critério. Algumas pessoas decidem que todas serão privadas. Algumas decidem tornar públicas só as que precisam do máximo de performance. Outras escolhem tornar pública todas as variáveis que só acessam o seu valor de forma simples sem precisar de processamento. Outras analisam o domínio como um todo para tomar uma decisão.
Novamente tudo isso depende da linguagem que está usando, da tecnologia acessória que pode exigir uma determinada forma, da filosofia adotada, dos requisitos de manutenção, a forma como a classe será consumida, só para citar os principais.
Cada uma tem implicações de manutenção se um dia precisar mudar. Tudo que é público faz parte da API, mudar isso pode ser traumático. Mas depende. Se você tem controle total sobre a aplicação isso não é tão difícil assim se tiver as ferramentas certas.
Tem casos que você quer que a variável realmente seja só um detalhe de implementação, aí tem que ser privada, tudo que é público faz parte do contrato que você precisa espeitar em grande parte das situações, mas não todas como algumas pessoas acham. Precisa ser pragmático.

Em que circunstâncias uma variável deve ser tornada pública?

Sempre que achar que a privada não é adequada, dentro dos parâmetros já passados acima.
Tem uma pergunta com mais informações e links importantes para se aprofundar sobre o assunto.
Complemento após edição da pergunta
Agora que foi tirada a independente-de-linguagem e colocada a tag de php eu diria que não tem necessidade usar variáveis privadas, pelo menos se tiver um getter e setter que fazem nada além de retornar o valor ou atribuir o valor passado. Lembre-se que PHP é uma linguagem de scripts.
Tem quem ache que, mesmo em linguagem de script deve-se adotar técnicas usadas em linguagens enterprise.
Por outro lado, o ganho de usar a variável pública é bem pequeno em PHP. As variáveis de classes em PHP são a mesma coisa, não há otimizações. Ainda assim eu prefiro não colocar linhas de código que eu nem sei se serão úteis.
Note que é usar os métodos mágicos é diferente de usar métodos getXXX() e setXXX() já que assim terá que mudar o código dos consumidores se precisar incluir algum processamento no acesso a estas variáveis. Mas reforço que se está fazendo um script, isso é tranquilo. Você está usando PHP para fazer scripts, certo?
Claro que se a variável genuinamente só deve ser acessada pela classe, se não está criando um encapsulamento artificial, aí o uso da variável privada é interessante.
Variáveis privadas ajudam obter a coesão e evitam o acoplamento não intencional.
Coloquei no GitHub para referência futura.


Suspicious connections in Apache access log

Question: I recently bought a VPS to run some basic websites on, and today I was checking the access and error logs of Apache and I noticed some strange connections in the access log.
Here are two examples:
<code>XXX.XXX.XX.XXX - - [27/Jul/2019:06:39:54 +0000] "CONNECT i.instagram.com:443 HTTP/1.1" 403 168 "-" "-"

XXX.XX.XX.XX - - [27/Jul/2019:08:35:10 +0000] "\x03" 400 0 "-" "-"
</code>
The second of the two IP addresses (that I have hidden) has been reported in multiple places for things like spam and malicious activity.
The VPS is pre-configured to run the LAMP stack, so I assume I have nothing to worry about. But I cannot find anything on the internet in relation to these logs, so wanted to check that things like this are expected to happen when a VPS is exposed to the internet.
Answer:
check that things like this are expected to happen when a VPS is
  exposed to the internet.

Yes, constant scans and malicious garbage is the norm on the Internet.

VPS is pre-configured to run the LAMP stack, so I assume I have
  nothing to worry about

False, choosing a particular platform does not make you secure. Failed request noise is not of concern, no. However, web apps do have vulnerabilities and they are exploited. Apply security updates and keep reviewing logs.


Drop UserMailRequired constraint

Question: In my Drupal site, only administrators create users. And users are created without an email. Users can edit themselves some of their account fields, but the email is not one of them.
But the user module adds a UserMailRequired constraint to the User entity email field. On submit, the user module's validator UserMailRequiredValidator adds a violation if the current user is not an administrator :
class UserMailRequiredValidator extends ConstraintValidator {

  /**
   * {@inheritdoc}
   */
  public function validate($items, Constraint $constraint) {
    /** @var \Drupal\Core\Field\FieldItemListInterface $items */
    /** @var \Drupal\user\UserInterface $account */
    $account = $items->getEntity();
    $existing_value = NULL;
    if ($account->id()) {
      $account_unchanged = \Drupal::entityManager()
        ->getStorage('user')
        ->loadUnchanged($account->id());
      $existing_value = $account_unchanged->getEmail();
    }

    $required = !(!$existing_value && \Drupal::currentUser()->hasPermission('administer users'));

    if ($required && (!isset($items) || $items->isEmpty())) {
      $this->context->addViolation($constraint->message, ['@name' => $account->getFieldDefinition('mail')->getLabel()]);
    }
  }

}

So, none of my user can edit its profile. On submit, they get an error message "Email field is required". Even if the field's #required property is set to false (using hook_form_alter) and the field not displayed (#access = false) in the form render array.
How can I drop this constraint from a custom module ? (using a hook for example)
Comment: https://drupal.stackexchange.com/q/250302/57
Comment: Setting fake email adresses during registration is not a good solution for me. As I said, administrators can already create accounts without an email adress. What I need is to drop the validation of this field when I submit `user_form` form.
Comment: Did you solve this. I just ran into it. No amount of changing #required in form alter works. I'm thinking the Plugin class has to be extended and changed. Wondering if you've already solved this @Eria
Comment: @eahonet : unfortunately, I don't remember :/ That was one year ago, and I'm not working on this project anymore. And I don't have access to the source code. Sorry...
Answer: For those that find themselves here trying to make User Emails Optional, use hook_validation_constraint_alter. You're not extending core's, you're creating a new constraint and using it instead of core's. In my case, I replace core's with... an empty function to achieve what I want.
Something like this in .module
function optional_account_email_validation_constraint_alter(array &$definitions) {
  $definitions['UserMailRequired']['class'] = '\Drupal\my_module_name\Constraint\OverrideUserMailRequired';
}

Then a copy of the two UserMailRequiredValidator.php and UserMailRequired.php files changed to your liking.
The module administerusersbyrole has a working example of this.
I'll see if I can get around to putting my custom module onto drupal.org in the coming months.
Comment: This is the wrong hook to implement, to remove a constraint. `hook_entity_base_field_info_alter()` is the one that allows to remove constraints. It has the pro that allows to remove a constraint from the field of a specific entity type, which is helpful when the constraint is added to different entity types and changing the constraint for all the entity types isn't desired.

Drop UserMailRequired constraint


Alert em apenas determinado navegador

Question: É possível somente com JavaScript e HTML fazer aparecer um alert somente e só quando o usuário estiver navegando pelo Internet Explorer?
Por exemplo quando for navegar pelo Google Chrome o alert não aparece, mas sim quando for navegar pelo Internet Explorer
Comment: O Explorer não é um _browser_. Editei sua pergunta e troquei "Explorer" por "Internet Explorer".
Answer: É possível obtendo as propriedades do navegador:
function identificarBrowse() {
    var nav = navigator.userAgent.toLowerCase();
    if (nav.indexOf("msie") != -1) {
        alert("IE");
    }
}

Nota
A forma de identificar IE11 foi alterada, usei a solução do Joachim:

https://stackoverflow.com/a/17907562/3444041

JSFiddle: https://jsfiddle.net/lbclucascosta/bxekLzaz/3/

Alert em apenas determinado navegador


ERROR: missing FROM-clause entry for table "new"?

Question: Olá, estou com o seguinte erro:

ERROR:  missing FROM-clause entry for table "new" CONTEXT:  Error
  occurred on dblink connection named "unnamed": could not execute
  command.
SQL statement "select             dblink_exec(            'host=...
  port=5432 dbname=** user = ** password = ***' , 'INSERT INTO
  teste.teste_replica ("id", "nome")            values (new.id, new.nome)')"
  PL/pgSQL function teste_bd() line 26 at SQL statement}

e essa é a minha linha de código que está com o erro:
ELSIF (TG_OP = 'INSERT') THEN
            select
            dblink_exec(
            'dbname=postgres
            hostaddr=***.**.**.***
            user=***
            password=***
            port=5432',
            '--INSERT INTO teste.teste_replica SELECT NEW.*;
            INSERT INTO teste.teste_replica ("id", "nome")
            values (NEW."id",NEW."nome")');
            RETURN NEW;

Lembrando que ambos são bancos testes, onde eu preciso replica o dados após insert, update e delete do servidor A para o servidor B, mas esse erro está me impedindo, sabe como posso arrumar?
Answer: Tente:
ELSIF (TG_OP = 'INSERT') THEN
            select
            dblink_exec(
            'dbname=postgres
            hostaddr=***.**.**.***
            user=***
            password=***
            port=5432',
            concat('INSERT INTO teste.teste_replica ("id", "nome") values (', NEW."id", ',', quote_literal(NEW."nome"), ')');
            RETURN NEW;

Comment: valeu deu certo porem agora o delete ta dando erro, sendo que estava ok


Setting up Windows Azure Pack with ADFS

Question: I'm trying to setup the Windows Azure Pack Admin Site with Authentication from ADFS.
I followed the given installation checklist from TechNet (http://technet.microsoft.com/en-us/library/dn469338.aspx). ADFS works fine for a lot of other applications in our environment. But here I will get an Server-Error:
SecurityTokenValidationException: ID4220: The SAML Assertion is either not signed or the signature's KeyIdentifier cannot be resolved to a SecurityToken. Ensure that the appropriate issuer tokens are present on the token resolver. To handle advanced token resolution requirements, extend SamlTokenSerializer and override ReadToken.
The configuration Database knows the correct token signing certificate, but the application will always throw an Error 500 at me.
Answer: Well I resolved the problem.
Microsofts deployment guide (http://technet.microsoft.com/en-us/library/dn469336.aspx), about how to deploy Windows Azure Pack with ADFS misses one little fact. The Relying Party has to set EnableJWT to true. Thanks for being precise about that in the manual...
Here's the Powershell snippet to adjust the RP
Set-AdfsRelyingPartyTrust -TargetIdentifier http://azureservices/AdminSite -EnableJWT $true

Comment: It is mentioned here: http://blogs.technet.com/b/privatecloud/archive/2013/12/17/federated-identities-to-windows-azure-pack-through-ad-fs-part-2-of-3.aspx

Setting up Windows Azure Pack with ADFS


Use relative parent directory links in Apache httpd mod_autoindex

Question: With Apache httpd mod_autoindex, the generated index pages use relative (without the leading slash) links for individual entries, for example:
    failure/

The "parent directory" link is absolute (includes the leading slash), though:
    Parent Directory

Such behaviour breaks parent directory links in a reverse proxy setup.
This has been observed on Apache httpd 2.4.6.
Can the parent directory link be made relative with native httpd configuration (without rewriting the URL)?
Comment: I don't think mod_autoindex has an option for that. This Q&A has some useful approaches though: https://stackoverflow.com/q/9718284/2952385
Comment: Thanks, skimming those answers discusses removal of "parent directory" - any particular answer that would discuss the relative/absolute link?
Answer: Taking a closer look, this might not be technically feasible.
A relative path in a subdirectory would be appended to the subdirectory.
For example, it would lead to incorrect paths like "/project/2023-08-23/project/".
This link could be "..", which works on Linux, but it might have other problems, especially cross-platform - and is not natively supported anyway.
For reverse proxying, rewriting this path in the generated HTML pages with mod_proxy_html might be the best option.


How to adjust my UIButton to maintain the aspect ratio

Question: I have made a View Controller on my storyboard and added to buttons to it,
I am starting a AVCaptureSession on my View Controller's view like this,
<code>AVCaptureSession *session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPresetMedium;

CALayer *viewLayer = self.view.layer;
NSLog(@"viewLayer = %@", viewLayer);
AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];

captureVideoPreviewLayer.frame = self.view.layer.bounds;
[self.view.layer addSublayer: captureVideoPreviewLayer];

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (!input) {
    // Handle the error appropriately.
    NSLog(@"ERROR: trying to open camera: %@", error);
}
[session addInput:input];

[session startRunning];
stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
[stillImageOutput setOutputSettings:outputSettings];

[session addOutput:stillImageOutput];
</code>
My problem is that my buttons get hidden as the preview layer takes up the whole screen I want the preview layer to assume whole screen but also want to display the buttons for capturing and closing the session
How can I go about it?
Answer: Try moving your button on top of the view controller views hierarchy with:
<code>[self.view sendSubviewToFront:button];
</code>
Do it after you've added captureVideoPreviewLayer as sublayer.