Qual a diferença de arquitetura e engenharia de software?

Question:
Arquitetura de software
Engenharia de software

Aparentemente esses dois conceitos são relacionados, mas qual a diferença entre eles?
Answer: Você já ouviu dizer que Arquitetura de software é um conceito usado pela Engenharia de software?
Pois bem, basicamente é isso, a Arquitetura de software nada mais é do que um conceito usado pela Engenharia de software para englobar as definições dos componentes de software, suas propriedades externas, e seus relacionamentos com outros softwares.
O que é arquitetura de software?

A arquitetura de software de um sistema consiste na definição dos componentes de software, suas propriedades externas, e seus relacionamentos com outros softwares. O termo também se refere à documentação da arquitetura de software do sistema. A documentação da arquitetura do software facilita: a comunicação entre os stakeholders, registra as decisões iniciais acerca do projeto de alto nível, e permite o reuso do projeto dos componentes e padrões entre projetos.

De onde vem o conceito?

O cargo de "Arquiteto de Software" como empregado hoje na indústria do software é mais uma herança ruim da comparação entre desenvolvimento de software e construção civil. Nesta última, o arquiteto faz o projeto mas, em geral, não suja as mãos no cimento.

Um fato interessante é que apenas empresas grandes, com orçamentos folgados (que, em geral, desperdiçam tempo e dinheiro com futilidades e becos sem saída) costumam oferecer o cargo de arquiteto de software. Eles geralmente ficam em equipes de arquitetura, longe das equipes que "sujam" as mãos no código no dia-a-dia. Ora, isso, por si só, criam as "Torres de Marfim" e uma certa animosidade latente entre as diferentes equipes – que deveriam trabalhar em conjunto todos os dias.
Não faz sentido (e não é eficiente) ter equipes de arquitetura separadas, sem contato direto e constante com as equipes de desenvolvimento. Também não faz sentido empregar arquitetos de software que só planejam e não participam da execução diariamente.
Usando a já batida metáfora do desenvolvimento de software como jardinagem é fácil perceber que estamos longe do processo utilizado, por exemplo, na construção civil ou na indústria automobilística. Não é possível projetar todo o software com antecedência, como um prédio ou um carro, comprar a matéria-prima, contratar os operários e implementá-lo praticamente sem desvios. O projeto é o software e o software é o projeto.
O que é engenharia de software?

Engenharia de software é uma área da computação voltada à especificação, desenvolvimento e manutenção de sistemas de software, com aplicação de tecnologias e práticas de gerência de projetos e outras disciplinas, visando organização, produtividade e qualidade

Friedrich Ludwig Bauer foi o primeiro dizendo:

"Engenharia de Software é a criação e a utilização de sólidos princípios de engenharia a fim de obter software de maneira econômica, que seja confiável e que trabalhe em máquinas reais".

O próprio significado de engenharia já traz os conceitos de criação, construção, análise, desenvolvimento e manutenção.
O termo foi criado na década de 1960 e passou a ser utilizado oficialmente em 1968 na NATO Science Committee. Sua criação surgiu numa tentativa de contornar a crise do software e dar um tratamento de engenharia (mais sistemático e controlado) ao desenvolvimento de sistemas de software complexos. Um sistema de software complexo se caracteriza por um conjunto de componentes abstratos de software (estruturas de dados e algoritmos) encapsulados na forma de procedimentos, funções, módulos, objetos ou agentes e interconectados entre si, compondo a arquitetura do software, que deverão ser executados em sistemas computacionais.
Fontes:
Wikipédia, Wikipédia 2, Keep Learning, Fórum Outer Space
Comment: Quando eu era mais novo, sempre quis ser um *Arquiteto de Software*. Mas depois que ganhei conhecimento vi que esse título era mais uma frescura do que algo real. Basta ser um *Engenheiro de Software*, o resto se mostra com o trabalho.
Comment: +1 excelente resposta
Comment: @JorgeB. Obrigado :D
Comment: Amei muito detalhado
Answer: Arquitetura de Software
Quando se diz sobre a Arquitetura de Software, se diz sobre quais tecnologias e a modelagem do software.
O papel do arquiteto em uma equipe desenvolvimento (que sim, existem arquitetos que também sujam as mãos) é ajudar a evitar o débito técnico, quedas de performance, falta de escalabilidade, que pode ser causado pelos desenvolvedores.
Arquitetos de Software são profissionais que tem um bom conhecimento de soluções em alto nível. Conhecem design patterns, conceitos como SOLID, DRY, YAGNI e tentam aplicá-los onde for cabível e possível.
Arquitetos modelam o sistema todo dia pois, como disse o colega @RodrigoBorth:

O projeto é o software e o software é o projeto.

Projetos mudam todos os dias, seja por novas tecnologias, impossibilidade de executar o previsto, mudança de especificações, etc.
Engenharia de Software
Um Engenheiro de Software é um líder de equipes de desenvolvimento. Este profissional conhece técnicas de desenvolvimento ágil, liderança de equipes.
A função de um engenheiro de software é manter a equipe, com seu melhor índice de produção possível, sanando problemas do processo de desenvolvimento de software.
Portanto
Os arquitetos são aqueles que modelam e projetam o sistema e, os engenheiros são aqueles que controlam o processo até a conclusão.
Comment: Concordo @JorgeB. mas considerei como ocorre aqui onde trabalho. Os desenvolvedores seniores, com melhores conhecimentos sobre a engenharia são líderes de suas equipes de 3 a 5 pessoas. Todos programadores deveriam conhecer. Mas conhecimento é um processo evolutivo e, novos desenvolvedores ainda não conhecem o processo e vão conhecendo no decorrer do tempo. Idealmente todos deveriam ter conhecimentos sólidos de engenharia e arquitetura, mas na realidade alguns só querem cumprir seu horário e outros se especializam.
Comment: -1 "Um Engenheiro de Software é um líder de equipes de desenvolvimento." Errado, idealmente todos os programadores duma empresa deveriam ser Eng. de Software. E quando eu digo eng. de software não me refiro a nenhuma Licenciatura, mas sim ao facto de "engenheirar" software.
Comment: Caputo sendo um conhecedor ou não, um programador é por norma um Eng. de Software, sé é bom Eng ou mau isso já é outra historia.
Comment: Excelente resposta, amigo...
Comment: Concordo contigo @JorgeB. estou lendo bastante sobre as definições e pretendo melhorar a resposta em breve. Obrigado!
Comment: Essa definição de Engenheiro de Software é totalmente equivocada. Aliás nem explica o que é **Engenharia** de Software
Comment: @JorgeB. Descordo totalmente. Um Engenheiro de Software é um Programador mas um Programador pode não ser um Engenheiro de Software. Programador é aquele que sabe escrever código - passar um algoritmo para uma linguagem, quase como um tradutor. Um Engenheiro de Software é capaz de fazer o mesmo quem um Programador mas faz-lo dotados capacidades e conhecimentos técnicos de forma a o fazer sistematicamente e seguindo padrões e boas práticas. (Passei pelos dois lados e esta resposta baseia-se na minha experiência pessoal.)
Answer: Exemplo da vida real.
A Empresa Foo tem um problema. O Sistema de Informação que eles utilizam, não está atendendo as necessidades da empresa. Eles precisam de um Sistema mais personalizado que se encaixe melhor em seus processos. Buscando uma solução, seu diretor entra em contato com a empresa Software Bar. As partes conversam e firmam um contrato para o desenvolvimento de um novo sistema para e Empresa Foo. Então, a software Bar inicia o processo de desenvolvimento da solução. Lá, existem vários tipos de profissionais, dentre eles:
Engenheiro de Software: Responsável por identificar as necessidades exatas do cliente, descrever os processos da empresa, identificar os envolvidos (classes) nos processos, desenhar e aprovar o projeto* junto ao arquiteto, departamento comercial e cliente.
Arquiteto de Software: Responsável por identificar as melhores tecnologias para desenvolver a solução proposta pelo engenheiro, definir e reunir os recursos necessários para o projeto, coordenar o trabalho da equipe de desenvolvimento.
Ambos tem competência para participar do processo de desenvolvimento. A diferença está na ÊNFASE.
*Existem dois projetos. O projeto da solução, que envolve o diagnóstico do problema e a coleta de informações e dados do cliente. Este projeto é elaborado com o envolvimento do cliente. Após aprovação, este primeiro projeto vai para a equipe de Arquiterura que o utilizará como guia para o desenvolvimento do SOFTWARE EM SI, iniciando esta etapa com a elaboração de um segundo projeto estritamente interno.
ARQUITETURA E ENGENHARIA DE SOFTWARE

Arquitetura de software é o estudo da organização global dos sistemas de software bem como do relacionamento entre seus subsistemas e componentes. Ela serve como uma estrutura que permite o entendimento de componentes de um sistema e seus inter-relacionamentos, especialmente daqueles atributos que são consistentes ao longo do tempo e de implementações. Lamentavelmente, arquiteturas de software recebem pouco ou nenhum tratamento sistemático na maioria dos cursos de Ciência da Computação, tanto no nível de graduação quanto de pós-graduação. A Engenharia de software, por sua vez, capacita o profissional a se aprofundar em todos os conceitos, métodos e processos relacionados com o desenvolvimento de aplicações, incluindo especificação, projeto, desenvolvimento, verificação, validação e gerenciamento.

Prof. Flávio Mello - UFRJ https://www.im2c.poli.ufrj.br/members/fmello/aes.html
Answer: Entre dever conhecer e de fato conhecer é oque separar os profissionais, não é simples assim. Portanto, a indústria tem que parar de usar os nomes errados para atrair profissionais. Um engenheiro de software é uma pessoa normalmente muito experiente, anos de profissão, pois já passou por todos os cenário possíveis e pode de fato assumir uma responsabilidade tão grande. Quando vejo em vagas oferecidas com o título de Engenheiro de Software, onde pedem conhecimento técnicos como linguagem de programação, vejo o quanto estamos imaturos na área.
Comment: _Um engenheiro de software é uma pessoa normalmente muito experiente, anos de profissão, pois já passou por todos os cenário possíveis e pode de fato assumir uma responsabilidade tão grande_ . Não, Engenheiro de Software é a pessoa que concluiu o curso de Engenharia de Software. É uma carreira acadêmica formal e reconhecida. Creio que não dá muito espaço para interpretação pessoal.


Secure IIS-Hosted WCF-Service and use it in Xamarin.Forms

Question: I have an IIS-hosted WCF-service-application running on IIS-10. The WCF is already running on https. My problem is that the service is currently accessible to everybody over the link that I have configured on IIS.
The WCF is meant to transfer data between my Xamarin.Forms-App and a SQL-Database. I want to know how I can make my service secure over authentication(username, password?) or something like that, and how I can authenticate in Xamarin.Forms to "open" the connection.
I haven't found anything matching my case on Google, so maybe you can help me solve my problem.
Answer: You may use Multiple Authentication Schemes with WCF for Authentication. For IIS-hosted services, authentication can be set accordingly based on what is selected in IIS.
The element ServiceAuthenticationBehavior or <serviceAuthenticationManager> is used for authentication service. This article has a more detailed explanation.
ServiceAuthenticationBehavior sab = null;
sab = serviceHost.Description.Behaviors.Find<ServiceAuthenticationBehavior>();

if (sab == null)
{
    sab = new ServiceAuthenticationBehavior();
    sab.AuthenticationSchemes = AuthenticationSchemes.Basic | AuthenticationSchemes.Negotiate | AuthenticationSchemes.Digest;
    serviceHost.Description.Behaviors.Add(sab);
}
else
{
     sab.AuthenticationSchemes = AuthenticationSchemes.Basic | AuthenticationSchemes.Negotiate | AuthenticationSchemes.Digest;
}

And use element <serviceAuthenticationManager> in config file:
<behaviors>
      <serviceBehaviors>
        <behavior name="limitedAuthBehavior">
          <serviceAuthenticationManager authenticationSchemes="Negotiate, Digest, Basic"/>

        </behavior>
      </serviceBehaviors>
</behaviors>

You could read the article about Authentication of Xamarin.Forms for more help.


Basic Questions about regression formula, sampling variability, and 'identification'

Question: lets say I run the simple regression, $y_i = \beta_o + \beta_1x_i + \epsilon_i$.. Assume $cov(\epsilon,x)$=0
This yields the formula people write in terms of covariances for the slope parameter:
$\hat{\beta_1}$ = $\frac{\sum(x-\bar{x})y_i}{\sum({x-\bar{x})^2}}$
and then plugging in the true assumed dgp for y, we get:
= $\beta +  \frac{\sum(x-\bar{x})\epsilon_i}{\sum({x-\bar{x})^2}}$
With this, I have a few questions.

is this is now a statement not about the population, but the 'draw' of $\epsilon_i$'s we so happened to draw in this sample? so it is the numerator second term the $\textit{sample}$ covariance between epsilon and x? if true, can I think of each random sample as a given draw of $\epsilon_i$'s, and that draw is what drives the sampling variability of the estimator?

2.taking probability limits, the covaraince =0 seems to be sufficient for consistency of the estimator.   however, is covariance only not sufficient for unbiasedness? is mean indepence of $\epsilon$ and x needec for finite sample properties?

An also a question about thinking about 'identification'. if i think of the  model above as the causal model, and I can say my ols is consistent, does that mean I have 'identified' the true $\beta_1$? so can it hink of the model not being identified if the $cov(\epsilon,x) \neq 0$, which would say that $\hat{\beta}$ converges in probability to the true $\beta_1$ + some other term? so I fail to isolate the underlying parameter?
Answer: I will give you a bit more information about the slope estimator and then I will proceed to your individual questions.  To do this, let's keep your simple regression model, but drop the assumption that the explanatory variables and error terms are uncorrelated.  We can then see what happens if the correlation is non-zero.  Since $\sum (x_i-\bar{x}) = \sum x_i - n \bar{x} = n \bar{x} - n \bar{x} = 0$ you can write the slope estimator as:
$$\begin{align}
\hat{\beta}_1
&= \beta_1 + \frac{\sum (x_i-\bar{x}) \epsilon_i}{\sum (x_i-\bar{x})^2} \\[6pt]
&= \beta_1 + \frac{\sum (x_i-\bar{x}) (\epsilon_i-\bar{\epsilon})}{\sum (x_i-\bar{x})^2} \\[6pt]
&= \beta_1 + \frac{\tfrac{1}{n-1} \sum (x_i-\bar{x}) (\epsilon_i-\bar{\epsilon})}{\tfrac{1}{n-1} \sum (x_i-\bar{x})^2} \\[6pt]
&= \beta_1 + \frac{s_{X, \varepsilon}^2}{s_X^2}, \\[6pt]
\end{align}$$
where $s_{X, \varepsilon}^2 \equiv \tfrac{1}{n-1} \sum (x_i-\bar{x}) (\epsilon_i-\bar{\epsilon})$ denotes the "sample covariance" between the explanatory variables and errors.  (Note that this is unobservable, since the true errors are unobservable.)  This means that the estimation error in this estimator is equal to the ratio of this sample covariance over the sample variance of the explanatory variables.
Your interest lies in the case where you are random sampling values from a population, so it is legitimate to assume that the series of regression obesrvations (i.e., the series $(X_1,Y_1),(X_2,Y_2),(X_3,Y_3),...$) is exchangeable.  Under that assumption, the sample variance and covariance converge to the true variance and covariance.  Thus, if the explanatory variable has a non-zero variance (and obscuring the difference between weak and strong probabilisitic convergence) then we have:
$$\hat{\beta}_1 \rightarrow \beta_1 + \frac{\mathbb{C}(X,\epsilon)}{\mathbb{V}(X)}.$$
This gives us a general asymptotic result for the slope estimator under an assumption of exchangeability of the underlying regression data.  You can see that if the explanatory variables are uncorrelated with the error terms (and the variance of the explanatory variables is non-zero) then the second term in this equation vanishes, which gives the desired consistency property.  (Here I am intentionally obscuring the difference between weak and strong consistency; to get these you would apply either convergence in probability or almost sure convergence respectively.)

Your equation for $\hat{\beta}_1$ writes this estimator as a function of the true slope parameter, the sample explanatory values and the (unobserved) error terms $\epsilon_1,...,\epsilon_n$.  So yes, this equation uses only the error terms in the sample, and not any other error terms in the broader population.  You can indeed think of each sample as a draw of explanatory variables and a corresponding draw of error terms for those samples, and this is sufficient to give you the second term in your equation (which is indeed what drives the variability of the estimator).

The assumption that $\mathbb{C}(X,\epsilon)=0$ is sufficient for the above convergence result only when you assume exchangeability of the underlying series of regression observations.  If you assume exchangeability then you can use all the standard convergence properties of sample variance/covariance under random sampling.  If you do not assume this then you need some alternative direct assumption about the convergence of the second term in the equation for the slope estimator.  In standard expositions of limit properties in regression, it is usual to impose some direct requirement on the series of explanatory variables to ensure that they do not "explode" in a way that screws up the convergence.$^\dagger$  In any case, assuming that your convergence result holds, this shows that the slope estimator converges to a constant.  Thus, it is not just a statement of asymptotic unbiasedness --- it is an actual consistency result.  As to whether the zero-covariance assumption is needed for "finite sample properties", it really depends on what properties you are talking about.  Obviously, in any finite sample, that second term in the above equation is going to exert an influence on the slope estimator.  If the explanatory variables and error terms are correlated, we would expect the slope coefficient to systematically under- or over-estimate the true slope parameter.

In statistical parlance, the concept of "identification" pertains to whether or not the parameters affect the likelihood function.  You seem to be using the term in a more informal sense, to refer to determining the true causal effect in the regression.  In any case, it is certainly true that if you assume that the regression model is the true causal mechanism for the data, then consistent estimation of the parameters is equivalent to consistent estimation of the causal parameters.  And yes, if you don't have a consistent estimator, then you are not identifying the true causal parameters.

$^\dagger$ Specifically, what you are trying to prevent is the case where the magnitude of the explanatory variables gets larger and larger, so that some finite set of data points always "dominates" the regression.  For further information on this, have a look at standard convergence theorems for regression.


Find line in one csv in another csv

Question: I have been using this code below to find exact matches of strings from one csv to another:
with open(filename1, 'rb') as file1:
file1reader = csv.reader(file1)

for line1 in file1reader:
    with open(filename2, 'rb') as file2:
        file2reader = csv.reader(file2)

        for linenum, line2 in enumerate(file2reader, start=1):
            if str(line1) == str(line2):
                print('match found')
                matches.append(str(linenum))
                counts.append(linenum)
                if len(matches) >= MAX_COUNT:
                    break

    if len(matches) >= MAX_COUNT:
        break

Now, I have a file that has strings that are partial matches to another file. I would also like to have these be a match as exact strings are in the above code. I tried replacing the == with in but had no luck. Any help?
Example:
string1 = humpty

string2 = humptydumpty

Answer: Try checking both directions:
if (str(line1) in str(line2)) or (str(line2) in str(line1)):

I don't think the parentheses are strictly necessary but they make the meaning clearer.
Comment: Tried this but still no luck. The script seems to not be able to find a match, even though I am sure there are substrings that are present in the first list that are present in string in the second list.
Comment: I think it is doing this because it is comparing list objects and thus string1 = ['humpty'] and string2 = ['humptydumpty']
Comment: If that's the case, then `if str(string1[0]) in str(string2[0])` would work.. assuming they are one-item lists
Comment: Yup, that's it! Thank you
Answer: You're trying to check if a substring is present in a string.
For this you can use the in operator:
Example:
if "humpty" in "humptydumpty":
    print "Ok"

In your case:
if string1 in string2:
    print "Ok"

Find line in one csv in another csv


Add key pair value to array

Question: I have two arrays and want to add the values from the first array as a key-pair value to the second. So the key will say [topic] and the value being the value from the first array. I have provided the original arrays, as well as the results I am looking for. Thank you.
Array
(
[0] => 1
[1] => 2
[2] => 1
)
Array
(
[0] => Array
    (
        [id] => 19
        [first] => Michelle
        [last] => Walters
        [sun] => 1
    )

[1] => Array
    (
        [id] => 182
        [first] => Robin
        [last] => Conti
        [sun] => 1
    )

[2] => Array
    (
        [id] => 183
        [first] => Cheri
        [last] => Constantino-Shor
        [sun] => 1
    )

)
HERE IS THE RESULT I AM LOOKING FOR:
Array
(
[0] => 1
[1] => 2
[2] => 1
)
Array
(
[0] => Array
    (
        [id] => 19
        [first] => Michelle
        [last] => Walters
        [sun] => 1
        [topic]=> 1
    )

[1] => Array
    (
        [id] => 182
        [first] => Robin
        [last] => Conti
        [sun] => 1
        [topic]=> 2
    )

[2] => Array
    (
        [id] => 183
        [first] => Cheri
        [last] => Constantino-Shor
        [sun] => 1
        [topic]=> 1
    )

)

Comment: To whom may I address my invoice?
Comment: It is not clear to me how the to arrays relate.  Are teh values in teh first array related to the topic key in the sub-array to the second array?  Are the numerical keys between array 1 and array 2 the only relation?  Also, have you made any attempt at doing this yourself, as the solution here could be as simple as executing a for loop?
Comment: The first array is the array of values that will become the value for each `topic` key. This is a really really trivial question. Just use a `for` loop.
Answer: This is extremely straightforward - there are probably a million ways to do this - the most elementary being a for loop. Assuming your array of topic values is called $topics and your main array is called $users:

$topics = array(1, 2, 1);

$users = array(
    array(
        'id' => 19,
        'first' => 'Michelle',
        'last' => 'Walters',
        'sun' => 1,
    ),
    array(
        'id' => 182,
        'first' => 'Robin',
        'last' => 'Conti',
        'sun' => 1,
    ),
    array(
        'id' => 183,
        'first' => 'Cheri',
        'last' => 'Constantino-Shor',
    ),
);

$count = count($users);

for ($i = 0; $i < $count; $i++) {
    $users[$i]['topic'] = $topics[$i];
}

var_dump($users);

Yields:
array (size=3)
  0 =>
    array (size=5)
      'id' => int 19
      'first' => string 'Michelle' (length=8)
      'last' => string 'Walters' (length=7)
      'sun' => int 1
      'topic' => int 1
  1 =>
    array (size=5)
      'id' => int 182
      'first' => string 'Robin' (length=5)
      'last' => string 'Conti' (length=5)
      'sun' => int 1
      'topic' => int 2
  2 =>
    array (size=4)
      'id' => int 183
      'first' => string 'Cheri' (length=5)
      'last' => string 'Constantino-Shor' (length=16)
      'topic' => int 1

Add key pair value to array


pandas: add new column based on datetime index lookup of same dataframe

Question: I have the following data that I would like to add a new column to that is the current month-over-month percent change.  The date is the index in my dataframe
    date    close
1/26/1990   421.2999878
1/29/1990   418.1000061
1/30/1990   410.7000122
1/31/1990   415.7999878
2/23/1990   419.5
2/26/1990   421
2/27/1990   422.6000061
2/28/1990   425.7999878
3/26/1990   438.7999878
3/27/1990   439.5
3/28/1990   436.7000122
3/29/1990   435.3999939
3/30/1990   435.5

The simplest way I could think to do this is to add a column that will contain the previous month  end date and also for convenience, the previous month-end 'close' value - and from that I can calculate the current month-over-month change. So in the end, i would have a table that looks like this:

I was able to add the previous month end just fine, but I am having problems now trying to look up the previous month-end close based on the previous month end date. In the the code below, the first line works fine to add the previous month end date as a new column.  But the second does not - the idea is to use the prev_month_end date to look up the the month end close value and add that as a column.
df['prev_month_end'] = df.index + pd.offsets.BMonthEnd(-1)
df['prev_month_close'] = df[df.index == df['prev_month_end']]

Any help or suggestions on how to accomplish this would be appreciated.
Answer: You can have prev_month_close as follows:
df.reset_index(inplace=True)
df = df[['date', 'close', 'prev_month_end']].merge(df[['date', 'close']].rename(columns={'close': 'prev_month_close',
                                                                                'date': 'prev_month_end'}),
                                                    how='left', on='prev_month_end')

OUTPUT
             date       close prev_month_end  prev_month_close
    0  1990-01-26  421.299988     1989-12-29               NaN
    1  1990-01-29  418.100006     1989-12-29               NaN
    2  1990-01-30  410.700012     1989-12-29               NaN
    3  1990-01-31  415.799988     1989-12-29               NaN
    4  1990-02-23  419.500000     1990-01-31        415.799988
    5  1990-02-26  421.000000     1990-01-31        415.799988
    6  1990-02-27  422.600006     1990-01-31        415.799988
    7  1990-02-28  425.799988     1990-01-31        415.799988
    8  1990-03-26  438.799988     1990-02-28        425.799988
    9  1990-03-27  439.500000     1990-02-28        425.799988
    10 1990-03-28  436.700012     1990-02-28        425.799988
    11 1990-03-29  435.399994     1990-02-28        425.799988
    12 1990-03-30  435.500000     1990-02-28        425.799988

OR without using reset_index
df = df[['close', 'prev_month_end']].merge(df[['close']].rename(columns={'close': 'prev_month_close'}),
                                                    how='left', left_on='prev_month_end', right_index=True)

OUTPUT
                 close prev_month_end  prev_month_close
date
1990-01-26  421.299988     1989-12-29               NaN
1990-01-29  418.100006     1989-12-29               NaN
1990-01-30  410.700012     1989-12-29               NaN
1990-01-31  415.799988     1989-12-29               NaN
1990-02-23  419.500000     1990-01-31        415.799988
1990-02-26  421.000000     1990-01-31        415.799988
1990-02-27  422.600006     1990-01-31        415.799988
1990-02-28  425.799988     1990-01-31        415.799988
1990-03-26  438.799988     1990-02-28        425.799988
1990-03-27  439.500000     1990-02-28        425.799988
1990-03-28  436.700012     1990-02-28        425.799988
1990-03-29  435.399994     1990-02-28        425.799988
1990-03-30  435.500000     1990-02-28        425.799988

Comment: so in this case it, we must treat them as two different dataframes, and then merge - is that correct?
Answer: We can convert the index to period index, then group the dataframe by period and aggregate close using last, then shift the period index one month back and map it with the closing values, finally calculate the percent change
i = pd.to_datetime(df.index).to_period('M')
s = i.shift(-1).map(df.groupby(i)['close'].last())
df['mom_pct_change'] = df['close'].sub(s).div(s).mul(100)

                close  mom_pct_change
date
1/26/1990  421.299988             NaN
1/29/1990  418.100006             NaN
1/30/1990  410.700012             NaN
1/31/1990  415.799988             NaN
2/23/1990  419.500000        0.889854
2/26/1990  421.000000        1.250604
2/27/1990  422.600006        1.635406
2/28/1990  425.799988        2.405002
3/26/1990  438.799988        3.053077
3/27/1990  439.500000        3.217476
3/28/1990  436.700012        2.559893
3/29/1990  435.399994        2.254581
3/30/1990  435.500000        2.278068

Comment: shift(-1) or shift(1)?
Comment: It should be -1


BlendMode data in a bitamp

Question: I'm working on a viewer that extracts bitmaps from a .swf file. They're all defined as classes, so I can get them with:
<code>getBitmap = event.target.applicationDomain.getDefinition(name);
getBitmap = new getBitmap;
</code>
These are all different parts of an image and I've to rebuild it. I'm almost done, the only thing that's missing is the blend mode. Now, I've no idea where to get the blend mode from. Some parts do have just a normal blend mode and there are parts which have a different blend mode.
First off, I've no idea if it's even possible that a blendMode is defined in a bitmap. Is there a way to get all informations out of it?
Answer: No it's not possible if it's actually just a Bitmap that means it's literally a map of the image, like a Cartesian graph of coordinates to color values, that is literally a bitmap (well with regard to image bitmaps).  Image files in other formats are simply encoded versions of said bitmap to reduce memory size in most cases (colors can be RGB or RGB plus Alpha).  Blend mode (in flash) is a matter of interaction between multiple objects at run-time, it is to say how do you compute the display value based on the mixture of multiple objects.  If you so choose you could set a blend mode on some object, overlay it on another, draw the resulting container to a bitmap then extract the color information, however when you store an image there is no sense of blend mode associated with it (unless it's some proprietary format like PSD or something but not in any of the common image formats).  The only value that relates to the "blend" in the case of a bitmap would be the alpha value which determines how much of a color behind the object will affect the display color of that pixel, or rather how much that pixel will affect the color it's laid over.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BlendMode.html
^notice blend mode is a property of DisplayObject, which BitmapData does not extend from, therefore despite any other knowledge it's obvious that the BitmapData itself has no blendMode the only bit of info you'll find in the BitmapData docs on blendMode is with regard to the draw method in which case the blendMode is used to set the pixel values but subsequently is not an accessible property of the BitmapData.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BitmapData.html
Just thought I would toss in a couple more details for the uber nerds (those undoubtedly roiling at my lack of reference to CMYK and color spaces).  I know you can use other color models such as CMYK which is subtractive instead of additive and is the method used in print, however for displays it's all translated into some sort of RGB since light works in an additive nature.  The RGB may be 8-bit aka 256 color, 16-bit 65,536 colors, 24-bit 16,777,216 colors, or 32-bit 4,294,967,296 colors (if A is included it is another bit depth factor that increases the color space, however A is really the only thing related to "blending").  I'm almost positive anything higher is beyond 32-bit is above our threshold to distinguish, not to mention the way the mind works with visual data via relative changes it probably isn't as important as the relationship between the colors (contrast).
Beyond the color model there are color spaces which map the values to actual colors displayed (buddy of mine told me this was tweaked in some version of Android Honeycomb to reduce the saturation which some people liked and others were angered by).
This ultimately bleeds into biology and the study of cones and rods and how photons stimulate them and how they interact with one another then how this information is encoded in the nerve bundle in the eye how it is sent through the visual cortex and ultimately how our brains interpret those signals, this varies among humans due to differences in all layers of said biology and head development.  Anyhow this is getting way out of my personal nerd realm of expertise so I'll leave it hear for biology nerd to expand if they'd like.  Seems to be pretty good explanations here: http://webvision.med.utah.edu/book/part-ix-psychophysics-of-vision/the-primary-visual-cortex/ though it takes far more understanding than I have the time to delve into.
The other part that's interesting to me is the ability of hardware to accurately re-create whatever is ultimately the value determined after the color space is applied.  All electronic components have some level of error/variability (much like our eyes/brains) so undoubtedly there will be variances based on the hardware and even the operating temperature and other environmental factors.  Anyhow I'm sure I've now plunged far too deep, let me know if the first bit doesn't answer your question.

BlendMode data in a bitamp


delete a specific element within field collection programmatically

Question: This is driving me crazy it just sounds simple, but I cannot find the answer.
I have a field collection in a content type called field_collection_mine how you now while creating the node you can add elements to the field collection.
I have tried this:
$wrapper = entity_metadata_wrapper($entity_type, $entity);
$wrapper->field_collection_mine = array();
entity_save('field_collection_item',$wrapper);

The code above delete all the elements within the field collection, but that is now what I want!! I just want to delete for example the last element. E.g:

Field Collection mine
Element1
Element2
Element3

How can I for example delete just the element 3? The above code deletes all the elements, so field_collection_mine is left empty.
Comment: Check @milkovsky answer here http://drupal.stackexchange.com/questions/11062/how-do-i-load-a-field-collection-from-a-node

I think it is what you want
Comment: I already can access to the items of the field collection! this one for access to the first item $wrapper_egresos->field_salida_mercanc_a[0] but when I try to delete the item with unset, then in the node I cannot see the items but if I do a dpm to the array of items I can still see the other items, but they are not displayed. help!
Answer: You need to grab the id of the field collection and wrap itself in an entity wrapper. Use the save method in the wrapper to do this instead of the entity_save function. E.g.
 $wrapper = entity_metadata_wrapper('field_collection_item', $entity);
 $wrapper->field_specific_field_collection_item->set("");

or:
$wrapper->field_specific_field_collection_item = array();
$wrapper->save();

Comment: Look I do not with what I should replace 'field_specific_field_collection_item' as the unic thing I have is the machine name of my field collection 'field_collection_mine'.
Comment: The field collection can contain unlimited number of items, where each item has two fields (date and quantity) how can I delete the last added item?
Answer: Finally I could achieve what I wanted. What I did was this:
In my case I wanted to delete the last item within the field collection.
Take a look to this code, for beginners: remember that you must have the $entity already loaded, e.g.
// Remove the field value
unset($entity->field_salida_mercanc_a[LANGUAGE_NONE][count($entity->field_salida_mercanc_a[LANGUAGE_NONE])-1]);

// Reset the array to zero-based sequential keys
$entity->field_salida_mercanc_a[LANGUAGE_NONE] = array_values($entity->field_salida_mercanc_a[LANGUAGE_NONE]);

// Save the entity
entity_save($entity_type, $entity);

Thats all! The other way is:
// Get the node wapper
$wrapper = entity_metadata_wrapper($entity_type, $entity);
// Get the last position of the array items, thanks to the ->value() statement you can get the complete Array properties.
$field_collection_item_value = $wrapper->field_collection_mine[count($wrapper->field_collection_mine->value())-1]->value();
// Do not use 'unset' to delete the item due to is not the correct way, I use the entity_delete_multiple provided by the entity API
entity_delete_multiple('field_collection_item', array($field_collection_item_value->item_id));

The way above is good using entity_metadata_wrapper function, but with that way there is a complex bug that I do not know how to solve it, you can check it at #1880312 and after apply the patch in #9 you get the next issue.


Há alguma diferença entre compilar pelo terminal do linux ou pela IDE?

Question: Estou programando em C no linux há um semestre já, e sempre me deparo com pessoas que se dizem melhores programadores porque compilam e executam o programa através do terminal. Porém, diante de várias  pesquisas, sempre encontrei na internet artigos dizendo que a IDE foi feita para ajudar o programador, fazer com que o tempo que ele usaria compilando toda hora e rodando no terminal, melhorasse um pouco.
Na minha humilde experiência com os dois, realmente, eu achei muito mais fácil apenas compilar e rodar pela IDE. Mas como estou iniciando nessa área, gostaria de saber de vocês qual a diferença? É melhor mesmo compilar pelo terminal? Se sim, porquê?
Observação: Utilizo o codeblock.
Comment: @MarcielliOliveira Sua pergunta é bastante válida, mas parece fugir do foco do site. Mas pra não te deixar sem resposta: saiba o que a IDE está fazendo por trás dos panos quando vc clica no botão de compilar. Assim que você aprender isso, use o botão de compilar. Não existe glória nenhuma em perder tempo. Ninguém vai ligar pro quanto seu desenvolvimento foi "nobre" se você não entregar resultados tão rápido quanto esperado.
Comment: Aqui tem informação relevante sobre isto: http://pt.stackoverflow.com/q/101691/101
Comment: A pergunta é um bocado baseada em opiniões. Por exemplo aqui no meu trabalho compilamos a aplicação com um script previamente criado para executar comandos na linha de comandos. Dependendo do que pretende o IDE pode servir sim, desde que você saiba o que está a fazer, ou seja, como o IDE está a compilar o seu código. De resto é uma questão de gosto ou de necessidade.
Comment: Marcielli é mesmo só questão de gosto, claro que os geeks vão todos dizer que é melhor fazer *tudo* no terminal, mas tens o exemplo do Android Studio que faz tudo por nós e dá muito jeito. Mas para alguns casos específicos tem mesmo de ser a mão a compilação de uma app Android.
Comment: @Pablo adorei essa última frase. Mais nada.
Comment: Compilar o programa pela linha de comando eu uso somente em casos específicos, geralmente quando faço acesso remoto em minha maquina, porque não consigo carregar a IDE em modo gráfico. Quando eu estou programando uso atalhos da IDE para compilar acho mais rápido e prático, mas não tem nenhum problema usar qualquer uma dessas opções.
Comment: Entendi Jorge B. Como as pessoas falam muito do terminal, eu pensei que seria algo  que era "o certo a se fazer". Obrigada ;)
Comment: Obrigada a todos. Entendo que não é o foco do site. Caso achem melhor, eu deleto. Sem problemas. De todo modo, me ajudaram bastante.
Comment: @MarcielliOliveira eu editei a pergunta, com aquele título acho que já fica mais dentro do escopo do site. Se não gostar da edição pode reverter. Mas acho que deve manter a pergunta, é bem interessante.
Comment: Gostei sim Jorge, achei melhor também. Obrigada.
Answer: Acredito que seja importante saber como funciona no terminal, mas a não ser que você vá utilizar comandos muito específicos para compilar não há necessidade.
Toda IDE foi feita com o intuito de agilizar e ajudar o trabalho do programador, sendo que muitas delas permite que você passe parâmetros antes da compilação.
Existem diversos fatores que diferenciam um bom e um "mal" programador, escrever um  gcc prog.c -o prog  não é assim tão diferente de apertar o play na IDE.
Comment: Exatamente :) Tem muita gente que acha que fazer da maneira mais complicado o torna melhor, sendo que poderiam gastar este tempo melhorando o que realmente interessa em seus projetos.
Comment: O seu ultimo comentário foi quase tão bom quanto a resposta +1 nos dois :)
Comment: A IDE ajuda especialmente a retirar a parte mecânica do desenvolvimento e deixar o programador livre para se dedicar ao que softwares não conseguem fazer sozinhos.
Answer: A diferença é mais humana que física. Um programa compilado em um IDE pode até sofrer de algumas poucas diferenças devido aos parâmetros "genéricos" de compilação do IDE porém a maioria das pessoas não conhecem muitos parâmetros de seus compiladores para acrescentar algo e aqui aparece a diferença humana de se programar sem IDE: o conhecimento das ferramentas que o IDE usa, do processo de compilação e link edição.
Porém a vida é curta e o mercado restrito. Ficar lendo a documentação do GCC e do Linker pode ser uma perda de tempo e dinheiro.
Eu só precisei aprender mais sobre as ferramentas de geração de código quando algo dava errado ou um IDE era muito caro e uma alternativa custom era possível, dessa forma gosto do misto IDE leve e terminal como Code::Blocks + Guake (terminal suspenso) para programar sistemas embarcados e descarto os IDEs de base Eclipse e não tenho grana para um IAR.


How to use Unicode chars with Nokogiri::XML::DocumentFragment

Question: I want to use Unicode char with Nokogiri::XML::DocumentFragment.
frag = Nokogiri::XML::DocumentFragment.parse("<foo>ü</foo>")
=> <foo>ü</foo>

The unicode char is escaped. I need to set encoding: 'UTF-8' to get a readable char.
frag.to_html(encoding: 'UTF-8')
=> "<foo>ü</foo>"

Is there a option for encoding when parsing the string?
Nokogiri::HTML::DocumentFragment.parse treat the string as I expected, but I need to use XML.
frag = Nokogiri::HTML::DocumentFragment.parse("<foo>ü</foo>")
=> <foo>ü</foo>

Answer: According to the documentation here the text is internally stored as UTF-8 already.

Strings are always stored as UTF-8 internally. Methods that return text values will always return UTF-8 encoded strings. Methods that return XML (like to_xml, to_html and inner_html) will return a string encoded like the source document.

So if you call for example #text on your frag instead of printing the entire frag object, you'll see the ü printed correctly
puts frag.text
# => ü

Otherwise you can use #XML instead of #DocumentFragment directly and pass the encoding explicitly.
doc = Nokogiri.XML('<foo>ü</foo>', nil, 'UTF-8')
puts doc

# => <?xml version="1.0" encoding="UTF-8"?>
# => <foo>ü</foo>


Doctrine2 how to create aggregate subquery

Question: I have next table
id int not null auto icrement
email_name varchar
event_type varchar

email type - possible values(sent, open, click, unsubscribed)
I need to create query like this in doctrine2
SELECT COUNT(ee.id) as total, (SELECT COUNT(e.id) from email_statistic as e where e.event_type = 'open') as opened FROM email_statistic as ee

I want to select total amount, and how many opened emails for statistics
How can I do this wia doctrine?
   $qb = $this->createQueryBuilder('ee')
            ->select('count(ee) As total');

$qb2 = $this->createQueryBuilder('e')
            ->select('count(e) As opened');

        $qb2
            ->andWhere('es.eventType = :eventType')
            ->setParameter('eventType', 'open');

$qb->addSelect($qb2) --- this does not allowed by doctrine

Should I use NATIVE QUERY? Can I do this with dql?
Comment: put that second select directly into `$qb->select()`
Comment: @mmmm does not work
Comment: Query builder works with Entities via DQL. I don't see anything entity related here. And your guess is correct, you probably want to use native query here.
Answer: First, you should create an entity, for example EmailStatistic:
/**
 * @ORM\Entity()
 * @ORM\Table()
 */
class EmailStatistic
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id()
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="email_type", type="string", length=255)
     */
    protected $emailType;

    /**
     * @var string
     *
     * @ORM\Column(name="email_name", type="string", length=255)
     */
    protected $emailName;
}

Then you can use GeneralCaseExpression (http://www.doctrine-project.org/api/orm/2.4/class-Doctrine.ORM.Query.AST.GeneralCaseExpression.html).
$em = $this->get('doctrine')->getEntityManager();
$qb = $em->createQueryBuilder();
$result = $qb
    ->select('SUM(CASE WHEN (e.emailType = \'open\') THEN 1 ELSE 0 END) as opened')
    ->addSelect('SUM(e.id) as total')
    ->from('AppBundle:EmailStatistic', 'e')
    ->getQuery()
    ->getArrayResult();
// $result[0]['opened'] contains number of opened emails
// $result[0]['total'] contains total number of emails


Objective increasing while error decreasing in CNN

Question: I'm using MatConvNet to train a convolutional neural network, and i noticed that after the 5th iteration, the objective started fluctuating (mostly upwards) whereas the validation error was decreasing. Does this indicate overlearning? What system parameters can I change for a slower but more steady learning?
Thanks for any help!
Answer: When the network starts overfitting, it is generally the other way around: the training error keeps decreasing whereas the validation error increases or stagnates. In your case, you can play with different learning rates and annealing schedules, momentum, or use Batch Normalization (http://arxiv.org/abs/1502.03167).


How Do I Convert an Array of Characters To Integers?

Question: I am Just a begineer in C Programming. While solving a programming assignement I came across the need to convert an array of unsigned char to integer.
For Example:
unsigned char x[]="567";
unsigned char y[]="94";

Now I have to add the integer values in both x and y. That is:
int sum=661;

What is the simplest way to do this?
Answer: You're looking for atoi() .
Comment: Unless the assignment intends for you to handle arbitrarily large numbers stored this way. Then you need to implement decimal add-and-carry like you learned in grade school...
Comment: But using atoi I got This error" Cannot convert unsigned char* to const char*.
Comment: You can cast the unsignedness away; that doesn't mean much when using `char` to store characters (as opposed to bytes).
Comment: https://www.securecoding.cert.org/confluence/display/seccode/MSC34-C.+Do+not+use+deprecated+or+obsolescent+functions
Answer: You have at least two options if you use standard library. The first is atoi()  function from stdlib.h and second is sscanf()function from stdio.h.
Here are examples:
atoi()
char number_string[] = "47";
int number;

number = atoi(number_string);

sscanf()
char number_string[] = "47";
int number;
int return;

return = sscanf(number_string, "%d", &number);
/* it would be good idea to check the return value here */

sscanf() gives you a better error handling. Return value of sscanf() determines  how many items were succesfully read (how many variables were filled). If there was an input failure, EOF is returned.
atoi() returns 0 if no valid conversion could be performed.


Firebase cloud function dart kill all functions

Question: I am searching for preventing attacks like Ddos, I am not sure I came across a solution.
Case 1
in which from every request putting a increment value in firestore database. After certain value such as 100000 a cloud function will trigger which will destroy / deactivate all my cloud functions.
Case 2
Is there any easy way using if else in cloud functions.
I am new to Firebase cloud functions
I am writing cloud functions in dart
Is there any way to write security rules for calling function
Is there any way to limit invocations
Can cdn or another service integration help in this situation. I dnt want surprise bill
Answer: First of, see this documentation on the guidelines you should follow to avoid security attacks in Firebase.

in which from every request putting a increment value in firestore database. After certain value such as 100000 a cloud function will trigger which will destroy / deactivate all my cloud functions.

Unfortunately, this is not how a managed service works. Cloud Functions can only be triggered when invoked or during a response to an event. If there's no traffic, then the function is not running. It's not possible to deactivate them.
You can however, list all you functions and delete them one-by-one by using Cloud Functions Client Library and method deleteFunction().

Is there any easy way using if else in cloud functions.

For this question, are you referring to conditional statements or on how a traffic is redirected?

I am new to Firebase cloud functions I am writing cloud functions in dart

Currently, there is no official way to deploy a function running in Dart Runtime, though there are community supported projects that allow you to run Dart functions on other environments.
Node is the only runtime being supported in Cloud Function for Firebase as of the moment. See documentation here.

Is there any way to write security rules for calling function

Firebase security rules are for Cloud Firestore, Realtime Database, and Cloud Storage. See this SO that shows how to protect HTTP functions using auth id tokens and database rules.
Additionally, in this documentation, you can find how to setup security rules in your Firebase project. Sample scripts can be found here.

Is there any way to limit invocations

You can find a similar SO question here on limiting invocations in Firebase Cloud Functions. Additional details regarding Quotas and Limits can be found here.

Can cdn or another service integration help in this situation. I dnt want surprise bill

CDNs can help you bring down costs due to caching behavior, however it is not the complete solution to avoid surprise bills. One way to avoid this is to setup budget alerts to send email notifications whenever your project exceeds (or about to exceed) the set spend threshold. See documentation on Avoiding surprise bills here.
Comment: Does doing this will not cost https://stackoverflow.com/questions/50741936/rate-limiting-for-google-firebase-cloud-functions. And I mean detaching user from functions completely. And is there any way to write security rules before anyone can call my function. Just how we do this for firestore security rules
Comment: can u please look at this also https://stackoverflow.com/questions/68847507/firebase-cloud-functions-upper-limit-and-time-limit
Comment: As per checking, your new posted question has already been answered.
For this question, "And is there any way to write security rules before anyone can call my function. Just how we do this for firestore security rules", you can create a new question for this so the community can contribute. Additionally, you can check this link https://cloud.google.com/functions/docs/securing/authenticating on how to authenticate invocations.


Optimize image for web (C# & ASP.NET MVC)

Question: I am looking for a way to efficiently prepare large amount of images for website in .NET
Images are often largescale, unedited, 2-5MB 4000x8000px monster images  from phone camera.
I would like to generate thumbnails quickly, and as efficient as possible. (not to slow down CPU) or performance for user.
I also have to consider caching.
Modern CMS systems are using Image pre-processor that you can invoke via front-end. So I want to make something like this also, but my own. Cause in my case, I can't use CMS here.
Here is my code: I have a static helper method from Helper class.
I call it in razor every time I need to render an image.
public static string GetImgThumbnail(string web_path, int _width = 0, int _height = 0)
{
        //Default parameters
        Image image;
        Image thumbnail;
        string thumb_url = "/img/noimg.png";

        try
        {
            // web_path example input "/images/helloworld.jpg"
            //system_path returns system path of Ogirinal image in the system f.e.: "C:\projects\websites\mysite\images\helloworld.jpg"
            string system_path = HttpContext.Current.Server.MapPath(web_path);
            image = Image.FromFile(system_path);

            //Original image dimensions
            int width = image.Width;
            int height = image.Height;

            //Get New image dimensions
            if(_height == 0)
            {
                if(_width == 0)
                {
                    _width = 700;
                }
                _height = (_width * height) / width;
            }

            if (_width == 0)
            {
                if (_height == 0)
                {
                    _height = 700;
                }
                _width = (_height * width) / height;
            }

            //Generate Thumbnail, passing in new dimensions
            thumbnail = image.GetThumbnailImage(_width, _height, null, IntPtr.Zero);

            //Find out how to call newly created thumbnail.
            //Original image example name = "/images/helloworld.jpg" thumbnail would be "/images/helloworld_thumb_700x250.jpg" or analogical with .png or JPEG etc...
            //Suffix should be "_thumb_{width}x{height}"
            string suffix = string.Format("_thumb_{0}x{1}", _width.ToString(),_height.ToString());

            var bigImgFilename = String.Format("{0}{1}",
                     Path.GetFileNameWithoutExtension(system_path), Path.GetExtension(system_path));

            var newImgFilename = String.Format("{0}{1}{2}",
                     Path.GetFileNameWithoutExtension(system_path), suffix, Path.GetExtension(system_path));

            //New system path of new Thumbnail example: "C:\projects\websites\mysite\images\helloworld_thumb_700x250.jpg"
            var newpath = system_path.Replace(bigImgFilename, newImgFilename);

            //Set new web path, expect this to be: "/images/helloworld_thumb_700x250.jpg"
            thumb_url = web_path.Replace(bigImgFilename, newImgFilename);

            //Check if file exists, no need to overrite if file exists.
            if (!File.Exists(newpath))
            {
                thumbnail.Save(newpath);
            }

        }
        catch (Exception exc)
        {
            // If something goes wrong, just return backup image.
            thumb_url = "/img/noimg.png";
        }

        // return thumbnail
        return thumb_url;
}

Would love to hear some tips/suggestions or whether I am on the right path, or I should do it in different way?
Comment: Take a look at the ImageResizer project

I cant understand what your code is supposed to do when it calculates the width and height but the second set of ifs cannot possibly be correct because it checks if _width is 0 twice
Comment: Thanks, that was a typo, I corrected my code example.
Comment: Off topic but some practical idea: You could consider a cloud service like [cloudimage.io](https://www.cloudimage.io). They will download the image from your location once requested, resize (other manipulations possible like crop, apply filters, compress) and return a cached copy (from now on), everything done just by changing the URL. I know, one could program everything, but those services are just super-helpful sometimes.
Answer: So in your code, you calculate the thumbnail first. Then you calculate the filename and check if the thumbnail calculation was necessary.
I would do in a different order:

Load the image (since you need that to calculate the new filename)
Calculate the new filename
Check if the file is already there
If the file is already there, use it
If not, generate the thumbnail.

In code this would roughtly look like the following:
public static string GetImgThumbnail(string web_path, int _width = 0, int _height = 0)
{

    [...]
    string system_path = HttpContext.Current.Server.MapPath(web_path);
    image = Image.FromFile(system_path);

    // calculate new width and height
    [...]

    // calculate new filename
    [...]
        //New system path of new Thumbnail example: "C:\projects\websites\mysite\images\helloworld_thumb_700x250.jpg"
        var newpath = system_path.Replace(bigImgFilename, newImgFilename);

        //Set new web path, expect this to be: "/images/helloworld_thumb_700x250.jpg"
        thumb_url = web_path.Replace(bigImgFilename, newImgFilename);

        //Check if file exists, no need to overrite if file exists.
        if (!File.Exists(newpath))
        {
            //Generate Thumbnail, passing in new dimensions
            thumbnail = image.GetThumbnailImage(_width, _height, null, IntPtr.Zero);
            thumbnail.Save(newpath);
        }

My point is: Loading an image and calculating the size dosen't use much resources. The resizing part is a heavy on the CPU. So you will get faster responses, if you only transform the image if necessary.

Here are some points you can consider:

I call it in razor every time I need to render an image.

This will generate a thumbnail on every image view. This is very cpu heavy and most likely not what you want. Consider creating the thumbnail only once, save it to disk and start using the pre-rendered version.
Next issue:

//Check if file exists, no need to overrite if file exists.
if (!File.Exists(newpath))
{
    thumbnail.Save(newpath);
}

You first compute the thumbnail and then check if the computation has already be done. It should be the other way round.
Comment: While these points may be valid, this does not attempt to answer the OP's question. This should be a comment, not an answer.
Comment: Hi rollstuhlfahrer, you have some good points, totally agree with that. But mail issue with calling File.Exists() first, is that I need to get NewPath, and I can get new Path only after I gothrough OldPath (a.k.a) web_path. So I need to break it down first, then I know what file I need to look for (or create) what extension it has etc... cause user might add TIFF file format, that I totally did not expect.
Comment: Did some revision. Am I getting closer?
Comment: @rollstuhlfahrer Your answer was very helpful in this case, it did solve my case. Sincere thanks for that! :)


More trouble with the same proof

Question: I asked a question about a proof and now I have two more questions. Here is how I understand the proof (second half):
If $m_T$ has no multiple roots it looks like this: $(T-\lambda_1)...(T-\lambda_k)$. The idea is to show that every generalized eigen vectors also is an eigen vector. To do so pick an eigenvalue $\lambda = \lambda_i$ for some $i$ and let $U $ be the space spanned by the generalized eigen vectors of $\lambda$. Let $v$ be some vector in $U$. The goal is to show $(T-\lambda)v=0$. This can be shown easily by using that $T-\lambda_j = (T-\lambda)+(\lambda-\lambda_j) = N+cI$ from which it is clear that all $T-\lambda_j $ are invertible. Inverting all except $T-\lambda$ results in $(T-\lambda)v=0 $.

What I don't understand is: where is the assumption used that there
  are no multiple roots in $m_T$?
And: why is it stated in the solution that the only eigen value of
  $(T-\lambda_m)|_U$ is $0$? It does not seem to be used in the proof.
And also: where is it used that $V$ is a complex vector space?
Answer: $\DeclareMathOperator{\im}{im}$You mean $m_{T}$ is the minimal polynomial of $T$, and the $\lambda_{i}$ are distinct.
You may use the result that if $m_{T} = f g$, with $\gcd(f, g) = 1$, then the underlying vector space is the direct sum
$$
V = \ker(f(T)) \oplus \ker(g(T)).
$$
This is because there are polynomials $s, t$ such that
$$
1 = g s + f t,\tag{bezout}
$$
so that
$$
1 = g(T) s(T) + f(T) t(T),
$$
and for $v \in V$ one has
$$
v = g(T) s(T) v + f(T) t(T) v.
$$
Now $$g(T) s(T) v \in \im(g(T)) \subseteq \ker(f(T)),$$ as $f(T) g(T) = m_{T}(T) = 0$. So $v \in \ker(f(T)) + \ker(g(T))$, and (bezout) implies that $\ker(f(T)) \cap \ker(g(T)) = \{ 0 \}$.
Now start applying this to $f = X - \lambda_{1}$, $g = (X - \lambda_{2}) \cdots (X - \lambda_{k})$. Because the $\lambda_{i}$ are distinct, you have $\gcd(f,g) = 1$.
Comment: Thank you, very helpful. And: why is it stated in the solution that the only eigen value of $(T-\lambda_m)|_U$ is $0$?

More trouble with the same proof


How to delete all Persistent variables when a page is loaded?

Question: I have many variables set through variable_set. Is there any other way to delete them all at once apart from variable_del?
or How can find all the persistent variables set in the module?
Answer: It is considered bad practice to delete all the persistent variables whose name starts with the short name of the module. There could be a module whose short name starts with that short name; in that case you would be removing the persistent variables of that module.
For example, given the Views module, there could be a Views Enhancer module. Deleting all the persistent variable matching the SQL pattern '%views_%' would mean deleting the persistent variables of both modules.
Instead, you should have a list of all the persistent variables your module uses. (That is not hard, since you are writing the code of your module.) Using that list (for example, contained in $variables), a code like the following would delete all the variables.
  global $conf;

  db_delete('variable')
    ->condition('name', $variables, 'IN')
    ->execute();
  cache_clear_all('variables', 'cache_bootstrap');

  foreach ($variables as $name) {
    unset($conf[$name]);
  }

This is, essentially, the code of variable_del() adapted to delete a list of variable names contained in $variables.
You could also use that code to implement a function your module uses.
function variable_delete_multiple(array $variables) {
  global $conf;

  db_delete('variable')
    ->condition('name', $variables, 'IN')
    ->execute();
  cache_clear_all('variables', 'cache_bootstrap');

  foreach ($variables as $name) {
    unset($conf[$name]);
  }
}

Answer: You can always use MySQL to delete persistent variables set by variable_set:
delete from variable where name like '%module_name_variable_name%';
but assuming that you want to delete those variables on every page load consider another way to implement your code than using variable_get which is using the caching system


init not called on custom a class swift

Question: class Facts {
    var networkOperaton = NetworkOperation(url: "http://fact.tayfunturanligil.com")
    var factsArray : [String] = []
    init () {
        self.networkOperaton.downloadJSONFromURL({
            (a:[String]) -> [String] in
            println(a)
            self.factsArray = a
            return a
        })
    }
}

When I want to create an instance of Facts in my ViewController using var facts = Facts(), factsArray stays as an empty array. But it should be getting an array from downloadJSONFromURL function. Why this is happening? Even my println(a) is not called.
I tested downloadJSONFromURL function in a playground and it works. Also, networkOperation uses NSURLSession, could this be an issue?
Comment: Did you have a question?
Comment: Sorry for not being clear. I want my factsArray to be assigned an array but it does not. I think my entire init function is not being called because println(a) is never called. Why this might be happening?
Comment: Put print test just after init, not in the part below
Answer: The init() method is actually called. Try out this code:
class Facts {
   var factsArray : [String] = []
   init () {
       println("Init called")
       factsArray = ["Hello", "World", "Bye", "World"]
   }
}

ViewController.swift:
override func viewDidLoad() {
    super.viewDidLoad()

    var facts = Facts()
    println(facts.factsArray)
}

The problem with your code would be that network operation would be running in asynchronous mode. Instead of making network request in init method you can define another method in function which can then return status of request in completion block.
func updateArrayFromNetwork(completion: (Bool) -> ()) {
    var networkOperaton = NetworkOperation(url: "http://fact.tayfunturanligil.com")
    self.networkOperaton.downloadJSONFromURL({
        (a:[String]) -> [String] in
        println(a)
        self.factsArray = a
        completion(true)
    })
}

ViewController.swift
override func viewDidLoad() {
    super.viewDidLoad()

    var facts = Facts()
    println(facts.factsArray)

    facts.updateArrayFromNetwork { (completed: Bool) -> () in
        if completed {
            //Do something
        }
    }
}

Comment: Thanks. Thats solved the problem. but a couple new problems appeared. 1) the app is incredibly slow now( it is fast if I use NSData insted of NSURLSeesion. 2) I have a println("network init done") statement in my networkOperation init() but I see network init done statement twice. 3) dont I need to use dispatch_async(dispatch_get_main_queue() function somewere. Again thanks for the help
Comment: `NSData(contentsOfURL: NSURL, options: NSDataReadingOptions, error: NSErrorPointer)` is generally meant to load data from local url (Documents folder or any other file from local storage. You must use NSURLSession's sendAsynchronousRequest: [link][1]

[1]:https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/#//apple_ref/occ/clm/NSURLConnection/sendAsynchronousRequest:queue:completionHandler:
Answer: init() is called but downloadJSONFromURL() is an asynchronous method.
That means init() returns immediately the Facts instance after calling downloadJSONFromURL() without waiting for completion.

init not called on custom a class swift


Alocacao dinamica e ponteiros em Lua

Question: Gostaria de saber como faço alocação dinâmica e ponteiro de uma estrutura em Lua, tenho a seguinte instrução em C que preciso passar para Lua;
typedef struct ilha {
    char name;
    struct ilha *prox1, *prox2, *prox3;
} *Ilha;

Ilha aloc(Ilha x) {
    x = malloc(sizeof(struct ilha));
    return x;
}

int main() {
    A = aloc(A);
    B = aloc(B);
    B->name = "Hello Word";
    A->prox1 = B;
    printf("%d", A->prox1->name);
    return 0;
}

Answer:
Não precisas de passar uma cópia do ponteiro para a função de alocação
a função aloc() pode apenas devolver o ponteiro sem receber nada
Esqueceste-te de definir A e B na função main()
Verifica o resultado do malloc()
Não confundas char com char[] (ou char*) nem com int
no printf() usa "%d" com ints; %c com chars e %s com strings (char[] ou char*)

Depois de olhar para estes pontos, alterei o teu programa para:
#include <stdio.h>
#include <stdlib.h>

struct ilha {
    char name[100];
    struct ilha *prox1, *prox2, *prox3;
};

struct ilha *aloc(void) {
    struct ilha *x = malloc(sizeof *x);
    return x;
}

int main() {
    struct ilha *A = aloc();
    if (A == NULL) exit(1);
    struct ilha *B = aloc();
    if (B == NULL) exit(2);
    strcpy(B->name, "Hello Word");
    A->prox1 = B;
    printf("%s\n", A->prox1->name);

    return 0;
}

Em vez de definires o name como um array de 100 caracteres podes definir como um ponteiro para char, e posteriormente fazeres o malloc() necessario e strcpy().

Alocacao dinamica e ponteiros em Lua


Android transparent images copied to gallery obtain solid black background

Question: I use the following statement to copy an image from the assets to the gallery app, which works fine:
MediaStore.Images.Media.insertImage(getContentResolver(), myBitmap, myTitle ,  myDescription);

The images are png-files with a transparent background. They are displayed correctly when I load them from the assets to, for example an ImageView.
The problem is, that the formerly transparent background of the imported images became solid black in the gallery.
The png is a png24 created by gimp. I also tried a transparent gif and a png with transparency added by Apple's preview application with the same result.
Why it happens?
Comment: Have you tried the second method with the filepath parameter? Is the result the same?
Comment: Yes, same here. I also explicitly set the format for supporting the alpha channel: no change:
`BitmapFactory.Options options = new BitmapFactory.Options();
 options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap myBitmap = BitmapFactory.decodeFile(ctx.getFilesDir().getAbsolutePath()+java.io.File.separator+"locos"+"/BR03_1049.png", options);
MediaStore.Images.Media.insertImage(ctx.getContentResolver(),myBitmap,"standardlok" , "ohne" );`
Answer: I know this is an old question but I just had the same problem.  The problem is that MediaStore.Images.Media.insertImage stores the file with a MIME_TYPE of "image/jpeg", and jpeg doesn't support transparency.
One solution is to make your own content provider that uses another image format.  The Picasso image library might be worth a look too.


Tri-Sensor Scaled Hammer Action vs Fully Weighted keys (Graded Hammer Standard), what is the difference?

Question: What is the difference between the following? I am mainly interested in the difference in terms of how they feel when you press a note.
"Tri-Sensor Scaled Hammer Action"
And
"Fully Weighted keys (Graded Hammer Standard), Hard/Medium/Soft/Fixed sensitivity"
Comment: One important thing in an acoustic piano - or an acoustic guitar for that matter - is that it physically vibrates in the same room as you are as a physical person, and that in itself adds an important dimension. When you virtualize the room and put on headphones, something important disappears. Some digital pianos try to mimic this aspect by having lots of individual speakers pointing to various directions and maybe trying to make the piano's wooden body vibrate as well. I think this physicality adds something to the instrument's impact and impressiveness that helps motivation and learning.
Comment: The question arose while browsing for a new piano: (1) [piano 1 with tri-sense sclaed](https://www.sweetwater.com/store/detail/PX160BK--casio-privia-px-160-digital-piano-black)  (2) [piano 2 with graded hammer standard](https://www.sweetwater.com/store/detail/P125BK--yamaha-p-125-88-key-weighted-action-digital-piano-black)
Comment: They sound like marketing terms. ;) I've never understood the terminology, and just like real acoustic pianos are different and I don't like many of them, digital pianos are different and I don't like many of them. Some digital pianos are nicer to play than some acoustic pianos, but I have yet to find a digital piano that feels like an acoustic. Even my Yamaha Silent upright - activating the silent mechanism shortens the key action a little bit, and then it doesn't feel the same anymore. Every instrument can be _fun_ in its own way, and acoustic pianos can be bad. Try before you buy...
Comment: As piiperi says, there's action, then there's action. Even some 'real' pianos won't suit individuals. Whatever the jargon, the proper proof of the pudding is in the eating - try it out for yourself. Whatever it's called, it may or may not be right for you.
Comment: Thanks for the input guys. The way you put it, my question wasn't really answerable, so I just edited it.
Comment: @piiperiReinstateMonica - it's an interesting concept, but the action of any piano like this won't be affected by any of this, will it?
Comment: @Tim I meant to point out that there are other important factors to how an acoustic piano behaves besides the key action.
Comment: @piiperiReinstateMonica - no, that's fine. Interesting about the speakers, etc. But it would all need to have several reincarnations to be successful, like reverb can be hall, stadium, studio, etc...Maybe it can be really clever : Bosendorfer, Carnegie Hall, stage right, please...
Comment: @Tim If I understood correctly, the point with the speaker thingies is specifically not to try and create a different room, but to make the piano sound like an acoustic piano in the actual physical room where the piano is. No reverbs and stadiums. It's always a bit problematic if you have virtual room coming through loudspeakers - then you have TWO rooms on top of each other for that instrument - the virtual one and the real one. Which makes it feel less real and more like listening to a record. But if you have a real acoustic instrument, its sound only has the real room.
Comment: @piiperiReinstateMonica - fraught with too many problems! Headphones might save some of the idea - but even then, you'd need ones with at least two speakers on each ear!
Answer: Those terms do have specific meanings, although there is a bit of variation (for example, "progressive weighting" is used by some manufacturers, but it's identical to "graded hammer"). In the order presented:
Tri sensor means each key has three sensors.  You need at least one sensor just to tell if a key is being played, and you need at least two to deliver a dynamic range.    The dynamics produced by a piano depend on key velocity.  When you have two sensors the key will strike them in sequence, starting with the sensor closest to the hinge.  The smaller the time between the sensors being triggered, the faster the key must be moving, and the software will respond by delivering a higher dynamic level.  Adding a third sensor can allow you to do other stuff in addition, like the next item.
Scaled hammer means a note can be re-triggered without completely releasing the key.  This is the purpose of that third sensor - the one farthest from the hinge.  When the front of the key starts to rise, this sensor will release first, and as soon as it does, the key can be re-struck.  On pianos with just two sensors the key must return to the full-up position before you can re-trigger the key.
Fully weighted keys refers to the resistance of the key.  Keys can be either weighted or unweighted; weighted keys provide resistance by literally using a weight to simulate the resistance provided by the action of an acoustic piano.  The simplest weighted keys just add a weight to the back of the key mechanism, but most will lift a weight using levers or cams in the key mechanism.  "Fully weighted" means the resistance is completely provided by the weight, and that weight will also serve to return the key to its "home" position.  The other weighted option is "semi-weighted", which uses a spring in conjunction with a weight.  Fully weighted keys will typically have more resistance and a faster action.
Graded hammer means the lowest notes have more weight resistance than the highest notes.  Some folks will say that this makes them different from weighted keys - that's based on the belief that "weighted key" means the same weight, but that's not actually the case.  Weighting refers to the method of achieving the sense of touch, and grading is a difference in degree across the range.
Hard/medium/soft/fixed sensitivity doesn't actually change the "feel" of the keyboard, since that's set by the weights.  Instead, it puts limits on the dynamic range.  The lower the sensitivity, the louder the lower end of the dynamic range (the upper end is set by the volume knob or slider).  So a high sensitivity lets you get more shades of dynamic variation.  But if you don't have much touch control, high sensitivity can sound pretty awful, so the lower sensitivity grades are a better choice for less skilled pianists.
Answer: Honestly, based on these two terms I'd say they'd feel exactly the same. Hammer action will simulate acoustic pianos' feel of a hammer when pressing keys. However, even though both products descriptions say they use hammer action, the feel could be completely different based on the quality of that hammer action mechanism and the overall quality of the keys. But from what I can tell, these pianos are both hammer action.


Square of Vector Dot Product

Question: It is given in my textbook that, given two vectors x and z:

is equal to

How does this expansion happen
Answer: Let $x,z \in \mathbb{R}^n$, so we can write $x = (x_1,x_2,...,x_n)$ and $z=(z_1,...,z_n)$. Hence,
$$(x\cdot z)^2= (x_1z_1 + ....+x_nz_n)(x_1z_1 + ....+x_nz_n) $$
Notice that as we expand the sum, we get in the first monomial, second monomial, and so on the following
$$ (x_1z_1)^2 + x_1z_1 x_2 z_2 +x_1z_1 x_3 z_3 + ... + x_1z_1 x_n z_n $$
$$ x_2z_2 x_1 z_1 + (x_2z_2)^2 + x_2z_2 x_3 z_3 + ... + x_2z_1 x_n z_n $$
......
$$ x_nz_n x_1 z_1 + x_nz_n x_2 z_2 + x_nz_n x_3 z_3 + ... + (x_n z_n)^2 $$
Add the diagonals first and we obtain
$$ \sum_{i=1}^n x^2_iz^2_i $$
now, observe that the lower and upper triangular part of the array above are equal and so we are addings terms in the forsm $2 x_iz_i  x_j z_j $. Can you finish it?
Comment: wow..this is alot more complex than i thought. thx for the explanation. let me verify it before marking it
Answer: $$X.Z = x_1z_1 + x_2 z_2 +....+x_n z_n$$
$$(X.Z)^2 = ( x_1z_1 + x_2 z_2 +....+x_n z_n)^2$$
$$= (x_1^2z_1^2 +x_2^2z_2^2 +...+x_2^2z_n^2) +$$
$$ 2(x_1z_1x_2 z_2+x_1z_1x_3 z_3+....+$$
$$x_{(n-1)}  z_{(n-1)} x_n z_n)$$

Square of Vector Dot Product


Ways to arrange students in a class

Question: I was wondering what the total number of possible seating arrangements in a classroom is.

There are $10$ students.
There are $6$ benches, and each can seat $2$ students.
$8$ students are paired in $4$ benches.
Rest $2$ students sit alone on $2$ benches.

Is the total number of possible arrangements simply $^{12}P_{10}$ or do more restrictions need to be applied?
Answer: It is not quite that simple.  We must decide which seats are occupied, then fill them.
There are $\binom{6}{4}$ to select the four benches which will each receive two students.  For the other two benches, there are two ways to select which of the two seats will be occupied, which can be done in $\binom{2}{1}^2$ ways.  Now that we have decided which seats will be occupied, there are $10!$ ways to assign the students to the $10$ seats that will be filled.  Hence, there are
$$\binom{6}{4}\binom{2}{1}^210!$$
possible seating arrangements.

Ways to arrange students in a class


quick question on naming things related to metrics and topology

Question: sorry if this has been asked, but I am confused about how to name things related to topologies and metrics.
In topology, A topology on $X$ will be $\mathcal{O} \subset P(X)$ where it satisfies the three conditions. Where $(X,\mathcal{O})$ is named a topological space. But, I am confused about why we call $(X,d)$ a metric space. Would a better way to define this is the metric on $X$, which is the distance function $d$ that induces a topology on $X$(through the basis of balls) denoted as $\mathcal{O}$ where $(X,\mathcal{O})$ is called the metric space.
Comment: We call $(X,d)$ a metric space because it has a defined metric function on it, and we're considering them as a pair together for a relevant purpose; $(X,\mathcal{O})$ is the same, in that we look at it in a topological context and have defined a topology $\mathcal{O}$ on it. A metric will necessarily have an induced topology, generated by open balls, but the reverse need not be true, so they're distinct notions. You might call $(X,\mathcal{O}_d)$ that induced space. I'm not sure if that answers your question, but it's hard to ascertain what kind of answer you'd like in the first place.
Answer: We call $(X,d)$ a metric space, because $X$ is a set and $d$ is a metric. There's nothing more to that. Just like we call $(X,\mathcal{O})$ a topological space, because $X$ is a set and $\mathcal{O}$ a topology. We cannot call $(X,\mathcal{O})$ a metric space when $\mathcal{O}$ is not a metric. That would be very confusing.
You are right, that every metric induces a topology. But that doesn't mean that metric spaces are topological spaces. Besides formality, there isn't even a one-to-one correspondance between them: mutliple different metrics may induce the same topology (a.k.a. topologically equivalent metrics). We can call $(X,\mathcal{O})$ a metrizable topological space, meaning that $\mathcal{O}$ arises from some metric. But it is not a metric space.
And sometimes we are interested in metric properties instead of topological properties. For example in data analysis. Two different data sets may be topologically equivalent, but metrically very different. Another example is almost entire analysis, where completeness of the Euclidean metric plays crucial role, even though there are non-complete metrics on $\mathbb{R}$ that induce the same topology.
Given two metric spaces $(X,d)$ and $(X',d')$ we often consider the concept of a continuous function $X\to X'$ between them. As it turns out, continuity depends on the underlying topologies only, not on concrete metric. But as I said earlier, maybe we are interested in a stronger notion of distance-preserving functions, a.k.a. isometries. These require concrete metrics. For example $\mathbb{R}$ is homeomorphic to $(0,1)$ but not isometric (at least under the standard metric on both sides). So is $\mathbb{R}$ different from $(0,1)$? Depends on what properties we are looking at.
So all depends on what we are trying to achieve. Sometimes authors simply treat metric spaces as topological spaces. Meaning they only look at the induced topology. But you should always remember that these ideas are different.
Metric is not a topology. From a topological point of view it doesn't matter how far is Paris from Rome. But clearly it matters to you when you want to travel that distance, no?


bootstrap modal keeps opening

Question: I'm having some problems with a modal window in bootstrap.
I have a button, and when I click it, it shows a modal window, but when I click the close button, it closes and straight away it opens again, continually, and I can not close it!!!
here is the code:
html

          Chat now

                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>

Chat Now

chat chat chat

                <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>

js
$(document).ready(function(){
        $('.chatButtonDetails').click(function() {
         $("#chatModal").modal('show');
         return false;
        });
    });

Thank you in advance!!
Answer: Model is always outside of all content- This is bootstrap convention.
Please change your HTML like this, it should work,

          Chat now

                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>

Chat Now

chat chat chat

                <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>

Answer: your problem is that you have your modal inside your span element that you use to open.
close your span element like this and have your modal outside of it and it will work.

          Chat now

                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>

Chat Now

chat chat chat

                <button type="button" class="btn btn-danger" data-dismiss="modal" >Close</button>

Answer:  Before close on

Working Fiddle

bootstrap modal keeps opening


May a Sumo Rikishi be bald?

Question: Does the requirement to have a topknot mean that a rikishi who grows sufficiently bald must retire?
Comment: Interesting question... I know of a Yokozuna who had to retire for exactly that reason, but that was almost a century ago.
Answer: It may be a little late to answer, but this man is what you were looking for:

His name is Taikomaru Yutaka and his retirement ceremony was quite odd: no hair cutting, just some handshaking.

May a Sumo Rikishi be bald?


Targeting text field on node edit form from ctools modal

Question: I have a multivalue image field, and using FileField Sources, I've added a link that displays a ctools modal window that allows a user to search Apache Solr for images. The results are displayed in a table in the form. Each row in the table has a link that when clicked, should write the path to the returned image to the image field. Here's what I have so far:
/**
 * Drupal form to be put in a modal.
 */
function imgsearch_form($form, $form_state) {
  $form = array();

  $form['search_terms'] = array(
    '#type' => 'textfield',
    '#title' => t('Search text'),
    '#description' => t('Enter the search terms'),
  );

  $form['replace_div'] = array(
    '#prefix' => '
',
    '#suffix' => '
',
    '#weight' => 6,
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );

  return $form;
}

/**
 * Drupal form submit handler.
 */
function imgsearch_form_submit(&$form, &$form_state) {
  // Generate the new link using the submitted text value.
  $link = _nb_alters_make_link($form_state['values']['new_link_text']);

  // Search Solr for images.
  $images = nb_alters_search($form_state['values']['search_terms']);

  if (is_array($images) && count($images > 0)) {
    // Theme the results as a table.
    $header = array(t('Image'), t('Content'), t('Add to field'));
    $rows = array();
    foreach ($images as $image) {
      // Create image style derivative for each image.
      $imagestyle = array(
        'style_name' => 'thumbnail',
        'path' => $image['filepath'],
        'width' => '',
        'height' => '',
        'alt' => '',
        'title' => $image['filename'],
      );
      $styled_image = theme('image_style', $imagestyle);

      $rows[] = array(
        'image' => $styled_image,
        'name' => $image['filename'],
        'add' => ctools_ajax_text_button("select", "imgsearch/nojs/imgadd/" . $fieldname, t('Select')),
      );
    }

    $output = theme('table', array('header' => $header, 'rows' => $rows, array('class' => array('imgsearch-table'))));
  }
  else {
    $output = '
No images found
';
  }

  // Tell the browser to replace the old link with the new one.
  $form_state['ajax_commands'][] = ajax_command_replace('#imgsearch-replace-div', $output);
}

/**
 * Implementation of hook_menu.
 */
function nb_alters_menu() {
  $items = array();

  $items['imgsearch/%ctools_js/imgadd/%'] = array(
    'title' => 'Add Image',
    'page callback' => 'nb_alters_add_image',
    'page arguments' => array(1, 3),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );

  return $items;
}

/**
 *  Menu callback. Add the selected image to field_images
 */
function nb_alters_add_image($js, $fieldname) {
  if (!$js) {
    // We don't support degrading this from js because we're not
    // using the server to remember the state of the table.
    return MENU_ACCESS_DENIED;
  }
  ctools_include('ajax');
  ctools_include('modal');

  $commands = array();
  $commands[] = ajax_command_html("#edit-field-images-und-3-imgsearch-file-url", "filepath");

  // Tell the browser to close the modal.
  $form_state['ajax_commands'][] = ctools_modal_command_dismiss();

  print ajax_render($commands);
  exit;
}

The problem I'm having is when I click the "add" link, the form goes back to its original state (terms field and button, no results) and doesn't close. This is primarily because the page callback for my menu item - nb_alters_add_image() - isn't getting called. I've rebuilt the menus, and I've verified in the menu_router table that my menu item (imgsearch/%ctools_js/add/%) is there, so I know that's not the issue.
Can anyone tell what I'm doing wrong and what else I need to do to write the path to the image field and close the modal?
Thanks.
UPDATE: Well, it always helps to concatenate strings and variables properly (see the 'add' array item in the table array). Once I fixed that, it started hitting my page callback, but the modal isn't closing, and the value isn't being written to the image field.
UPDATE 2: After an IRC conversation with merlinofchaos where he pointed out my pretty obvious code errors, I've re-written my ajax function like so:
function nb_alters_add_image($js, $fieldname) {
  if (!$js) {
    // We don't support degrading this from js because we're not
    // using the server to remember the state of the table.
    return MENU_ACCESS_DENIED;
  }
  ctools_include('ajax');
  ctools_include('modal');
  $imagepath = 'http://mybucket.s3.amazonaws.com/styles/thumbnail/s3/images/2010/July/MainPhoto.jpg';
  $ajax_commands = array();
  $ajax_commands[] = ajax_command_html("#edit-field-images-und-2-imgsearch-file-url", $imagepath);
  // Tell the browser to close the modal.
  $ajax_commands[] = ctools_modal_command_dismiss();
  print ajax_render($ajax_commands);
}

This works in that it closes the modal and writes a value (both hardcoded for the moment just to get this part working), but the problem is that it adds a after the input element instead of writing the data to the textfield itself. Is there a different command I should be using to write the image path to the text field?
Answer: See my eventual resolution to this issue in the ctools issue queue.


Rewrite bad url extention to .html in NGINX

Question: A while back I made a mistake writing my urls and Google has them cached in old sitemaps. This is creating a ton of 404 errors in WMT that I need to clean up with a solid rewrite. I currently use the map module and rewrite each product individually.
Example:
/product1html/ /product1.html;

The mistake being in bold here: http://domain/productnamehtml/
The error here is I initially forgot the . before html and had an / at the end.
How can I rewrite the url to be http://domain/productname.html for every url that looks like /productnamehtml/.
Comment: Do you need only one particular url?
Comment: No it needs to be a wildcard match for every product which I can't work out the regex for.
Answer: The rewrite will look like this:
rewrite ^(.*[^.])html/?$ $1.html permanent;

Or you might want to place it into a location:
location ~* ^(?<product>.*[^.])html/?$ {
    return 301 $product.html;
}

Explanation: Both regex are identical, except the latter has a named capture product. We look for any sequence that ends in a non-dot followed by html, followed by (optional) /. Everything before the html is captured.
Comment: This answer fixed my issue right away. Now I need to look over the regex and understand why it works. Thanks for the help!
Comment: @Kanyin Added a short explanation..


How many players in the 2018 Stanley Cup finals have won a finals game before?

Question: The Washington Capitals and the Las Vegas Golden Knights have made it to 2017-18 Stanley Cup finals. Neither team has won a Stanley cup final game before. Of all the NHL players in the 2018 Finals game 1, how many have won at least one game in a Stanley Cup Final game before?
Comment: Aside: [FiveThirtyEight have covered a *very* similar question to this](https://fivethirtyeight.com/features/the-caps-and-knights-might-have-the-least-experienced-stanley-cup-final-rosters-ever/).
Comment: @PhilipKendall That's a great answer two days after this question was posted. You could copy and paste summary points into an answer here.
Comment: It's not actually your question as 538's is how many players have *played* in a Stanley Cup Final game whereas yours is how many have *won* a Stanley Cup Final game.
Comment: @PhilipKendall The reason I narrowed my question is I knew Washington got swept by Detroit the only time they were in the Stanley Cup Playoffs in their franchise history.
Answer: Brooks Orpik playing for Washington Capitals won Stanley Cup in 2009 with Pittsburgh Penguins.
Vegas goaltender Marc-André Fleury won three Stanley Cups - also with Penguins - in 2009, 2016 and 2017.
Both Orpik and Fleury also played in 2008, where Penguins lost to Detroit Red Wings.
In the 2017 Stanley Cup Finals, James Neal played for Nashville Predators, Predators lost the series to Penguins 2-4. He also currently plays for Vegas Golden Knights.
This should be all players with previous Stanley Cup final appearances from the rosters of the the two finalists according to sources below and also according to the Wikipedia article 2018 Stanley Cup Finals. (Here is link to the current revision. Notice the column "Finals appearance" in the section Team rosters.)

NHL Morning Skate: Stanley Cup Final Edition – May 27, 2018
Golden Knights leaning on Fleury, Neal heading into Stanley Cup Final
Ex-Penguins Brooks Orpik, Marc-Andre Fleury know Cup success
Brooks Orpik is only Capital with Stanley Cup finals experience: 'A big role model'
Comment: So the answer is three players have won in a Stanley Cup Final Game before (but necessarily won the cup): Orpik, Fluery and Neal? The other 50-odd players are looking for first win. (As of this time it's half that because Vegas won last night). Is this low number history making?