Google app engine - limit file size upload

Question: I would like to limit the size during a file upload according to the next requirements:
1- Client side ( this is almost impossible unless using client plugins such as Flash or Applets ) so I discard this one
2- On the server side, can I know the size of a file / image / video before storing it in the database?
Thanks!
Answer: With the Blobstore service, currently there is no way to limit the file size upload (open issue here).
Storing your data using a simple BlobProperty, you could check the size of the blob after the upload with len(uploaded_blob).
EDIT:
this is now fixed
Answer: Since release 1.5.4 create_upload_url takes additional optional arguments that can limit the upload size
create_upload_url(success_path, max_bytes_per_blob=None, max_bytes_total=None, rpc=None, gs_bucket_name=None)

As I get it max_bytes_per_blob limits the size of each file being uploaded, while max_bytes_total limits the total size of all files uploaded in one request.
For details see https://developers.google.com/appengine/docs/python/blobstore/functions
For example to limit the upload size to 5MB, call it like this:
upload_url = blobstore.create_upload_url('/upload', max_bytes_total=5000000)

If the upload size is larger than the limit, HTTP status 413 (Request Entity Too Large) is returned.
If you need, you can intercept it in jQuery like this:
    $("#file_form").ajaxForm({
        success : function(response) {
            ...
        },
        error : function(jqXHR, textStatus, errorThrown) {
            if (jqXHR.status == 413) {
                $("#error_message").text("Uploaded files should not be larger than 5MB");
            }
        }
    });

Answer: If you're using the Google Blobstore (which you probably will be on App Engine) then I think you're in a little bit of difficulty.  As this post states your option is to immediately check the File Size and delete it if too large.  Perhaps architecturally it is difficult for Google to offer validation on the BlobStore before the data is stored.  I don't know.
Perhaps you want to star the issue on the Google App Engine Issue Tracker.  It's unclear if you are using Java or Python, but if you look at the relevant Google API Docs you can determine how to check the size of the Blob entries.


Como usar o ternário aqui?

Question: <code>$rs = $pbc->selectPublication(filter_input(INPUT_GET, 'id', FILTER_DEFAULT));
</code>
Tenho esse código que é passado como argumento do método para selecionar os posts no sistema, mas como posso usar ele com ternário, caso ele retorne true e seja filtrado sem problemas passe o valor, ou se der algum problema, envie 0?
Minha classe select:
<code>$selectSQL = $this->pdo->prepare('SELECT values WHERE id = ? LIMIT 1');

            $selectSQL->bindValue(1, $codigo, PDO::PARAM_INT);
            $selectSQL->execute();
            $row = $selectSQL->fetch(PDO::FETCH_ASSOC);

            return $row;
</code>
Comment: Se o retorno dele, quando dá erro, já é zero, por que você precisa de um ternário?
Comment: Qual o retorno dele, caso dê erro?
Comment: 0,  ou se der algum problema, envie 0.
Comment: Não sabia que retorna 0
Comment: Ah, achei que tu tinha falado que retornava zero.
Comment: Mas tem como você colar o método `selectPublication` para facilitar a resposta?
Answer: <code>$rs = $pbc->selectPublication(filter_input(INPUT_GET, 'id', FILTER_DEFAULT));

echo ($rs) ? $rs : '0';
</code>
Se a variável tem retorno, então ou ela tem "conteúdo" ou é 0 ou false correto ?!
Com isso:
<code>$rs = true;
echo ($rs) ? $rs : '0';
$rs = false;
echo ($rs) ? $rs : '0';
$rs = 'okok';
echo ($rs) ? $rs : '0';
// pegadinha
$rs = 'false';
echo ($rs) ? $rs : '0';
</code>
Comment: A certos comentários que devem ser evitados, leia aqui, vai te ajudar: https://pt.stackoverflow.com/help/privileges/comment
Comment: Amigo, não fui eu que dei -1
Comment: Como isso ($rs) ? $rs : '0'; funciona? Coloque na resposta por favor.
Comment: É assim que comenta na resposta ? nunca usei stack
Comment: Cara como assim dá -1 !?
Pode-se ter vários entendimentos da questão, como vários resultados... peloamor...
Comment: Foi o Felipe Avelar então... que sacanagem...

Como usar o ternário aqui?


Por qual motivo é necessário subir o ng serve em 0.0.0.0?

Question: Instalei o Angular dentro de um container Docker (fabiojanio/ionic), entretanto, só estou conseguindo acessar o aplicativo quando inicio o server desta forma:
ng serve --host 0.0.0.0

Gostaria de usar só:
ng serve

Como fazer isso?
Comment: Já tentou mudar a baseUrl do protractor.conf.js?
Answer: Isso não  é necessariamente em relação ao Angular, é mais relacionado a rede e docker.
Quando se usa o 127.0.0.1, você está dizendo para o seu servidor escutar/servir ao adaptador de rede virtual que atende ao host interno, também conhecido como loopback.
Você precisa utilizar 0.0.0.0 para que este servidor, rodando dentro do seu container, seja acessível por todas as interfaces de rede. Incluindo às de acesso externo, no caso de servidores, por exemplo.
Quando fizer deploy da aplicação para o host, precisa considerar utilizar o 0.0.0.0 para que o "redirecionamento" enter a interface de rede publica e a interface de rede do seu container, funcione.
Se o seu desejo é deixar o 0.0.0.0 como padrão, para não precisar digitar, você precisará definir no arquivo de configurações do angular, algo assim: https://stackoverflow.com/a/42788609/6394559
Answer: Usando o —-host 0.0.0.0 você está liberando acesso externo ao servidor de desenvolvimento do Angular.
Por padrāo o server sobe como host localhost
Oque impederia de acessar o server angular de fora do container.
Acredito que deva manter o host conforme ja tem feito.
Espero ter ajudado


Print all elements in PHP array HTML view

Question: I have a codeigniter project, which describe in simplest form with following tables
tbl_direct_fuel table
+----------------+------------+------------+
| direct_fuel_id | vehicle_id | issue_date |
+----------------+------------+------------+
|              1 |       1000 | 2019-10-01 |
|              2 |       1001 | 2019-10-02 |
+----------------+------------+------------+

tbl_direct_fuel_details table
+-----------------------+----------------+---------+----------+
| direct_fuel_detail_id | direct_fuel_id | item_id | fuel_qty |
+-----------------------+----------------+---------+----------+
|                   100 |              1 |      50 |     1.00 |
|                   101 |              2 |      60 |     2.00 |
+-----------------------+----------------+---------+----------+

store_item table
+---------+-----------+
| item_id | item_name |
+---------+-----------+
|      50 | DOT 3     |
|      60 | DOT 4     |
|      70 | DOT 5     |
+---------+-----------+

I tried to print fuel_qty in numbers & also in words as follows using my view
1.00    One and Cents Zero Only
2.00    Two and Cents Zero Only

Model
public function directFuelQtyById($id) {

        $this->db->select('tbl_direct_fuel_details.fuel_qty');
        $this->db->from('tbl_direct_fuel_details');
        $this->db->join('tbl_direct_fuel', 'tbl_direct_fuel_details.direct_fuel_id=tbl_direct_fuel.direct_fuel_id', 'inner');

        $this->db->where('tbl_direct_fuel.status=1 and tbl_direct_fuel.direct_fuel_id="'.$id.'"');
        $this->db->order_by('tbl_direct_fuel.direct_fuel_id','DESC');
        $q = $this->db->get();

        if ($q->num_rows() > 0) {
            return $q->result();
        }
        return false;
    }

Controller
public function convert_number_to_words($number)
    {

        $hyphen = ' ';
        $conjunction = ' and ';
        $separator = ' ';
        $negative = 'negative ';
        $decimal = ' and Cents ';
        $dictionary = array(
            0 => 'Zero',
            1 => 'One',
            2 => 'Two',
            3 => 'Three',
            4 => 'Four',
            5 => 'Five',
            6 => 'Six',
            7 => 'Seven',
            8 => 'Eight',
            9 => 'Nine',
            10 => 'Ten',
            11 => 'Eleven',
            12 => 'Twelve',
            13 => 'Thirteen',
            14 => 'Fourteen',
            15 => 'Fifteen',
            16 => 'Sixteen',
            17 => 'Seventeen',
            18 => 'Eighteen',
            19 => 'Nineteen',
            20 => 'Twenty',
            30 => 'Thirty',
            40 => 'Fourty',
            50 => 'Fifty',
            60 => 'Sixty',
            70 => 'Seventy',
            80 => 'Eighty',
            90 => 'Ninety',
            100 => 'Hundred',
            1000 => 'Thousand',
            1000000 => 'Million',
        );

        if (!is_numeric($number)) {
            return false;
        }

        if ($number < 0) {
            return $negative . $this->convert_number_to_words(abs($number));
        }

        $string = $fraction = null;

        if (strpos($number, '.') !== false) {
            list($number, $fraction) = explode('.', $number);
        }

        switch (true) {
            case $number < 21:
                $string = $dictionary[$number];
                break;
            case $number < 100:
                $tens = ((int)($number / 10)) * 10;
                $units = $number % 10;
                $string = $dictionary[$tens];
                if ($units) {
                    $string .= $hyphen . $dictionary[$units];
                }
                break;
            case $number < 1000:
                $hundreds = $number / 100;
                $remainder = $number % 100;
                $string = $dictionary[$hundreds] . ' ' . $dictionary[100];
                if ($remainder) {
                    $string .= $conjunction . $this->convert_number_to_words($remainder);
                }
                break;
            default:
                $baseUnit = pow(1000, floor(log($number, 1000)));
                $numBaseUnits = (int)($number / $baseUnit);
                $remainder = $number % $baseUnit;
                $string = $this->convert_number_to_words($numBaseUnits) . ' ' . $dictionary[$baseUnit];
                if ($remainder) {
                    $string .= $remainder < 100 ? $conjunction : $separator;
                    $string .= $this->convert_number_to_words($remainder);
                }
                break;
        }

        if (null !== $fraction && is_numeric($fraction)) {
            $string .= $decimal;
            $words = array();
            foreach (str_split((string)$fraction) as $number) {
                $words[] = $dictionary[$number];
            }
            $string .= implode(' ', $words);
        }

        return $string;
    }
    /////////////////////

    public function printFuel($id){

        $this->data['printData']=$this->Fuel_model->directFuelById($id);
        $fuel = $this->Fuel_model->directFuelQtyById($id);
        foreach($fuel as $obj)
    {
    $this->amount_word = $this->convert_number_to_words($obj->fuel_qty) . '
'." Only";
    }

        $this->load->view('/template/directFuel/printFuel',  $this->data);

    }

View (printFuel)

    <title>Fuel Order</title>

<?php
if (!empty($printData)) {
  $offset=1; // cm
  $cnt=0;
  foreach ($printData as $item) {
?>

  <p style="position: absolute;top: <?= ($cnt*$offset)+20 ?>cm;right: 12cm"><?=$item->fuel_qty?> Litres

  <p style="position: absolute;top: <?= ($cnt*$offset)+20 ?>cm;right: 8cm"><?=$this->amount_word?>

<?php $cnt++;
  } // close your foreach HERE
  ?>

<?php
}
?>

But when converting to words, the function prints only the last element for <?=$this->amount_word?>,  like below:
1.00    Two and Cents Zero Only
2.00    Two and Cents Zero Only

How to get the desired output?
1.00    One and Cents Zero Only
2.00    Two and Cents Zero Only

Answer: In your function printFuel($id) you are overwriting $this->amount_word with each new array value. Therefore you only get the very last value.
So just convert each amount  you get in $item->fuel_qty into a text string $item->fuel_qty_str using the function convert_number_to_words() in your controller:
public function printFuel($id){
    $arr=$this->Fuel_model->directFuelById($id);
    foreach($arr as $key=>$obj)
    {
       $str=$this->convert_number_to_words($obj->fuel_qty) . '
'." Only";
       $arr[$key]->fuel_qty_str=$str;
    }
    $data['printData']=$arr;
    $this->load->view('/template/directFuel/printFuel',  $data);

}

and then output the array in your view:
foreach ($printData as $item) {
  ?>

      <p style="position: absolute;top: <?= ($cnt*$offset)+20 ?>cm;right: 12cm">
        <?=$item->fuel_qty?> Litres

      <p style="position: absolute;top: <?= ($cnt*$offset)+20 ?>cm;right: 8cm">
        <?=$item->fuel_qty_str?>

  <?php $cnt++;
}


Can we project onto a component in a product measure?

Question: Suppose we have a measure space $(X,\Sigma)$ with a measure $\mu$, and we can decompose the measure space into the product of two spaces: $X=X_1 \times X_2$, $\Sigma= \Sigma_1 \otimes \Sigma_2$.
Is it in general possible to find measures $\mu_1, \mu_2$ on the two components so that for any measurable sets $B_1 \in \Sigma_1, B_2 \in \Sigma_2$, we have $\mu(B_1 \times B_2)=\mu_1(B_1) \mu_2(B_2)$?
Are these measures unique? Is there a canonical way of doing it?
So what I'm asking is if we know that a space is a product space whether we can recover the original measures.
Answer: A measure $\mu$ on $(X,\Sigma)$ induces measures $\mu_1,\mu_2$ on $(X_1,\Sigma_1)$ and $(X_2,\Sigma_2)$ prescribed by $B_1\mapsto\mu(B_1\times X_2)$ and $B_2\mapsto\mu(X_1\times B_2)$.
These measures $\mu_1,\mu_2$ induce a measure $\mu_1\times\mu_2$ on $(X,\Sigma)$ but it is not guaranteed that $\mu=\mu_1\times\mu_2$.
If equality holds then the measures $\mu_1,\mu_2$ can be called independent.
If equality lacks then the measures $\mu_1,\mu_2$ can be called dependent.
Comment: This gives us canonical measures on the component spaces, although in the cases I am thinking of (Lebesgue measure on R^2) it is a rather uninteresting one. I imagine there need not exist a working construction in general, but I'll wait a bit longer before accepting.
Comment: Take your time, of course. The spaces $(X,\Sigma)$ and $(X_1\times X_2,\Sigma_1\times\Sigma_2)$ are isomorphic, though. So the first will not be more interesting than the second.


Deceptive Cadence second inversion in Secondary half-diminished 7th chord

Question:
The image above (from Korean composer Byung Dong Paik's Theory of Harmonics, page 227) shows Deceptive Cadence progressions of Secondary half-diminished 7th chord
As you can see, the progression in the image above resolves the Secondary half-diminished 7th chord to a substitution chord.
But the substitution chord comes out as a second inversion, and what happens after that doesn't make sense.
The progression on the left side of the image seems to make sense. The bass goes in sequence and the 4th stays. This is one of the progress of the second inversion.
but, on the right side of the image, the base is sequential but the 4th of the ii46 jumps down from V.
Is this some kind of second inversion 'cadence progression?' (i46->V) but I don't even know if vi, which is the substitution chord of i , can do cadence progress because of the second inversion. (vi46->V)
In the image, ii46 is followed by V and then I. I is V in F and if vi46->V proceeds, shouldn't 'I' come immediately after ii46? In my opinion, this doesn't make sense either.
So I hope someone can tell me how it went.
Comment: is the iii64 acting like a V chord which goes to a deceptive resolution? iii64 often replaces V.
Comment: @armani Oh no, I see. I think you're right... If you leave it as an answer, I'll pick it up.
Comment: As to "shouldn't I come after ii46," remember that regardless of what cane before, ii chords are almost always used just before the dominant.
Comment: @nuggethead
It seems that the answer has already been found. I didn't think that a substitution chord (vi=I) of another key would also form a substitution chord (iii=V) in the original key. I guess I asked a stupid question.
Comment: @guss2222 not at all a stupid question! I am.not convinced your.book explained it well enough, or marked the chords clearly enough to avoid confusing people!
Comment: @nuggethead Unfortunately, looking back, I don't think I've found the answer yet! I thought that the  second inversion of the substitution chord wouldn't need to follow the normal progression of the second inversion of the major triad, but looking back at the beginning of the book, it says to proceed the same as the progression of the second inversion of the major triad.
Comment: @nuggethead I think it's not common because it's a substitution chord, but because it's a second inversion of a chord that resolves the leading tone. I've asked a similar question before, but in response, I heard that it is possible because the i46 incompletely resolves the leading tone.
Answer: Is this book using the term "substitution chord?" I think that concept will just add an unnecessary layer of complexity.
One way to think about these 6/4 chords is contrapuntally. They resolve like passing 6/4 chords, bass moving down by step to a root position chord.
In terms of root progression things look complicated, but you can break it down into simpler pieces by understanding a simple progression can take the insertion of a secondary dominant before nearly any chord, and technically a dominant can resolve deceptively to nearly any non-tonic chord. When you consider those two points of harmonic elaboration, the harmony reduces to mostly root progression by descending fifths, the fundamental type of progression.
Comment: You're being too literal, too much textbook thinking. A passing 6/4 to a V would be a ii chord, and the chord in question is a ii6/4. The critical part of a passing 6/4 is not literally holding the root of the 6/4 to become the fifth of the next root position chord, it's the step-wise motion of the bass moving through the fifth of the 6/4 chord.
Comment: A typical second inversion passing progression should perfect 4th in chord is stay in place. But the right side of the image is not.
Comment: These 6/4 chords are clearly, and easily explain as passing 6/4 chord or simply as common contrapuntal motions.
Comment: This is how I look at it: if the D5 in the penultimate bar was repeated or held until beat 3 for the V chord and then it descended to B4, would you be satisfied that ii6/4 is a passing 6/4?
Comment: A typical second inversion passing progression I was talking about is the same book as the example in the main text. and yes  Your last example is how the book says it.

And I asked a similar question about the second inversion before, and I knew that the rest of the voices other than the bass and 4th do not have to move like the example in the book, but the 4th and the bass should only move as set.

In other words, I thought I had some flexibility in thinking with the second inversion question I asked earlier, but if you're right, I guess it wasn't.
Answer: The fourth and third last chords look like it had a quick modulation to the sub-dominant key. How else do you explain the B flat?
You also have a E resolving like a leading tone should which strengthens this idea.
It also looks like a normal leading tone chord with its seventh added. Just one in F major.
Comment: I already wrote this example 'progressions of Secondary half-diminished 7th chord'.

'viiø43/IV' stands for Secondary half-diminished 7th, which has a leading tone and is resolved in ii46.

In other words, what you are describing is something I already know and not what I asked.

What I'm curious about is that the ii46 is a second inversion inversion, but it's not a normal second inversion progression.


Entity framework Cliente Venda

Question: Estou com duvidas de como montar o relacionamento entre as classes Cliente e Venda, dando uma olhada no site da microsoft, fiquei com duvida se a classe deve ter a navegação para outra.
Quero o cliente tenha uma lista de Vendas e que Vendas tenha uma lista de ItemVenda.
Eu devo colocar uma propriedade para fazer a navegação de Venda para cliente? Pois eu vejo que a Venda não precisa guarda o cliente e sim o cliente guardar a venda dele.
public class Cliente
{
    public int Id { get; set; }
    public string Nome { get; set; }
    public string Sobrenome { get; set; }
    public string Email { get; set; }
    public DateTime DataCadastro { get; set; }
    public bool Ativo { get; set; }

    public ICollection<Venda> Vendas { get; }
}

public class Venda
{
    public int Id { get; private set; }
    public DateTime Data { get; private set; }
    public Cliente Cliente { get; set; }
    public int ClienteId { get; set; }

    public ICollection<ItemVenda> ItemVenda { get; }
}

public class ItemVenda
{
    public int Id { get; set; }
    public int IdProduto { get; set; }
    public int IdVenda { get; set; }
    public int Quantidade { get; set; }
    public double Valor { get; set; }

    public virtual Produto Produto { get; set; }
    public ICollection<Produto> Produtos { get; private set; }

}

Qual o jeito certo de configurar eles no EntityTypeConfiguration
public class ConfiguracoesdeCliente : EntityTypeConfiguration<Cliente>
{
    public ConfiguracoesdeCliente()
    {
        HasKey(c => c.Id);

        Property(c => c.Nome)
            .IsRequired()
            .HasMaxLength(150);

        Property(c => c.Sobrenome)
            .IsRequired()
            .HasMaxLength(150);

        Property(c => c.Email)
            .IsRequired();

        //HasMany(p => p.Vendas);

        HasOptional(p => p.Vendas)
            .WithMany()
            .HasForeignKey(p => p.Vendas);

    }
}

public class ConfiguracoesdeVenda : EntityTypeConfiguration<Venda>
{
    public ConfiguracoesdeVenda()
    {
        HasKey(v => v.Id);

        Property(v => v.Data)
            .IsRequired();

        HasMany(v => v.ItemVenda)
            .WithMany();
    }
}

public class ConfiguracoesdeItemVenda : EntityTypeConfiguration<ItemVenda>
{
    public ConfiguracoesdeItemVenda()
    {
        HasKey(p => p.Id);

        Property(p => p.VendaId)
            .IsRequired();

        Property(p => p.ProdutoId)
            .IsRequired();

        Property(p => p.Quantidade)
            .IsRequired();

        Property(p => p.Valor)
            .IsRequired();

        HasRequired(p => p.Venda)
            .WithMany(p => p.ItemVenda);

        HasRequired(p => p.Produto);

        //HasRequired(iv => iv.Venda);
    }
}

Comment: A versão é 220.127.116.11
Comment: Qual a versão do Entity Framework?
Answer: Isso depende muito de como você quer manipular seus dados.
No geral criamos uma lista no Cliente com suas vendas e um Cliente em venda para facilitar a navegação caso você esteja com a venda e queira buscar o cliente daquela venda.
Sua propriedade public int ClienteId { get; set; } poderia ser acompanhada com uma propriedade do próprio cliente escrito como virtual que faz com que o cliente já seja carregado dependendo das configurações do Configuration.LazyLoadingEnabled do EF.
Na documentação do EF tem passo a passo de como você pode montar sua lógica de acordo com o que você precisar.
Comment: Então na classe Venda eu coloco o Cliente Cliente e o int CLienteId?
Comment: Se eu montar assim como vc falou, como eu configuro as 2 classes uma com a outra

Entity framework Cliente Venda


3:1 (or 1:3) interval, does it exist ? How to perform it from a C?

Question: Given that a composed sound is composed of a fundamental frequency and its harmonics (multiples of the fundamental), we could easily image that a pitch (with a fundamenal frequency f) is played and then another pitch with fundamental frequency 3f is played. Wouldn't the resulting sound be highly agreable to hear ? I tend to think it would be agreable because all the harmonics 3f, 6f, 9f, and so on would "resonate".
What would be the name of such an interval (3:1 or 1:3 would be the ratio, but is there a name?) ?
For example, if I play a C (third fret on the second string of a guitar), what should be the next key I need to play in order to do a 3:1 (or 1:3) interval ?
Thank you in advance.
Comment: That C note is 3rd fret, **5th** string. They're always numbered thin to thick.
Answer:
Wouldn't the resulting sound be highly agreable to hear ?

Likely yes. It is attributed to Pythagoras to discover that notes which frequency ratios can be expressed with small integers are consonant.

What would be the name of such an interval (3:1 or 1:3 would be the ratio, but is there a name?) ?

Twelfth or an octave + fifth.

For example, if I play a C (third fret on the second string of a guitar), what should be the next key I need to play in order to do a 3:1 (or 1:3) interval ?

I guess you mean C on the third fret of the fifth string? (Counting starts from the treble strings). The note would be G on the third fret of the first string.
See also Wikipedia article on Harmonic series.
Comment: As a side note,  folks should visualize the fundamental as the entire string moving up and down;  the first harmonic as the upper half vibrating opposite to the lower half;  the second harmonic as the middle third opposite to teh top and bottom thirds, and so on.


Change the submit function

Question: I am using VBO and I want to change the submit function on my form. I have tried the following code:
function a_views_bulk_operations_form_alter(&$form, &$form_state, $vbo) {
  if($form_state['step']=='views_bulk_operations_confirm_form'){
    $form['#submit'] = 'a_myfunc';
  }
}

function a_myfunc(&$form, &$form_state, $vbo) {
  dpm('Hit');
}

The a_myfunc is not hit. Instead I go to the normal VBO processing page. How can I change the submit function>
Answer: You're overriding wrong submit handler, normally VBO submit handlers are available under $form['select'] key in $form array. You can confirm this in your case by printing $form in hook_form_alter(). Make sure to use proper form ID as well before using, you can confirm form ID by printing $form_id variable available in hook_form_alter(). Below is example of altering VBO Form and overriding submit handler (In my case I've only one submit button).
/**
 * Implements hook_form_alter().
 */
function [MODULE_NAME]_form_alter(&$form, &$form_state, $form_id) {
  // Confirm $form_id before using.
  // echo "{$form_id}
";
  switch ($form_id) {
    case 'VBO_FORM_ID':
      // In VBO, normally submit handlers are available under 'select' key.
      $form['select']['submit']['#submit'] = array('MODULE_NAME_vbo_submit');
      break;
  }
}

/**
 * Submit handler for Views bulk operations.
 */
function MODULE_NAME_vbo_submit($form, &$form_state) {
  // Do your processing here.
}

Change the submit function


Does it follow that $\pi(p_k\#) > \frac{p_{k-1}\#}{3}$ for $k \ge 7$

Question: Let:

$p_k$ be the $k$th prime
$p\#$ be the primorial for $p$
$\pi(x)$ be the prime counting function

From Rosser & Schoenfeld, 1962, for $x \ge 17$:

$$\pi(x) > \frac{x}{\ln x}$$

It seems counterintuitive to me that:

$\pi(p_k\#) > \dfrac{p_{k-1}\#}{3}$

Is the following reasoning valid?
(1)  From Rosser 1941 and Dusart 1999 (references here), for $k > 6$:

$$k(\ln(k\ln k)-1)<p_{k}<k{\ln(k\ln k)}\!$$

(2) From $p_k + k > k\ln(k\ln k)$,

$e^{\left(\frac{p_k+k}{k}\right)} > k\ln k$

(3) $2e^{\left(\frac{p_k+k}{k}\right)} > p_k$ since:

$2k\ln k > k\ln k + k\ln(\ln k) = k\ln(k\ln k) > p_k$

(4) For $k \ge 5, 3p_k > \ln p_k\#$ since:

For $k \ge 7, p_k \ge 2k$ since $p_5 = 11 > 2\times 5$;  Assume $p_k > 2k$;  $p_{k+1} \ge p_k + 2 \ge 2k+2 = 2(k+1)$

$3p_k > 2p_k + 2k > k\ln p_k > \ln (p_k)^k > \ln(p_k\#)$

(5) Thus, for $k \ge 7$, $\pi(p_k\#) > \dfrac{p_k\#}{\ln(p_k\#)} > \dfrac{p_{k-1}\#}{3}$
Answer: Your proof is valid, and it is true that $\pi(p_k\#)> p_{k-1}\#/3$. I'm not sure why you think this is counterintuitive?
Asymptotically $p_k\#\approx e^{k\ln(k)}$ and so $\pi(p_k\#)\approx e^{k\ln(k)}/\ln(k\ln k)$. Therefore
$$ \frac{\pi(p_k\#)}{p_{k-1}\#} \approx \frac{e^{k\ln(k/k-1)+\ln(k-1)}}{\ln(k\ln(k))}\gg \frac{k}{\ln(k)} $$
In particular the ratio will $\to \infty$ as $k\to \infty$, so one shouldn't be surprised that it is $>1/3$ even for small $k$.
Comment: Thanks for confirming!  The resulting of primes seemed too large.  I am glad to hear that my intuition is wrong in this case.


IPv6: I'm confused about addresses

Question: I'm starting studying IPv6 and I'm having some troubles understanding some aspects.
Let's clarify if I'm getting this right!
A single host may have:
- a unicast global address
- a link local address
Then it joins multicast groups, solicited node group for example, one for each unicast address.
My first question is:
If a host needs to send a packet, which is its source address? the link-local one or the global one?
Now, let's move to ARP. I've read ARP no longer exists.
If I need to send a packet towards a host which is on my same link I'll use neighbor-solicitation to obtain its local-link address.
First of all...is this the real goal of this procedure? To obtain the local-link address?
Then...I suppose the IPv6 packet will be encapsulated within an Ethernet frame...which is its destination address considering braodcast is no longer an option and we don't know the MAC of the other host?
I've read, the IPv6 packet is sent to the multicast group "solicited-node", whose last 24 bits are obtained from the address of the target...but how can I create it if the address of the target is what i want to obtain?
Hope you can help go through this fog :)
thanks in advance!
Answer: The protocols commonly used to assign global addresses depend on the host already having a link-local address. So in most cases link-local is mandatory rather than optional.
A host can have multiple local and multiple global addresses assigned to each interface.
Source IP must be in the proper scope for the destination address a packet is send to. So sending to a link-local address will use link-local source. And sending to a global address will use a global source. If a packet is send to a global address through an interface which doesn't have a global address the host might use a global address from a different interface.
ARP isn't used for IPv6. But when running IPv6 on Ethernet a replacement is needed. That is ND which is more or less just ARP encapsulated in ICMPv6 packets.

IPv6: I'm confused about addresses


Converter um array de números em uma única String em php

Question: como eu transformo um array de números em um única string em php?
No código abaixo a variável $result é o array de números, e tentei converter ele para uma única string com a função implode(). Mas parece que não funciona, pois o php dá a mensagem 'Notice: Array to string conversion in..."  (LINHA DA QUERY
E no banco de dados não deu certo a persistência...
Como fazer essa conversão?
//Result é o array de números. Preciso convertê-lo para uma String...
$resultado = implode($result);

        mysqli_query($con,"INSERT INTO forum (codUsuario,titulo,mensagem) VALUES('$result','$titulo','$mensagem');");

Comment: Faltou passar por qual delimitador a string deve ser juntada. `$resultado = implode(',', $result);`
Comment: Os números devem ficar concatenados sem delimitadores...
Comment: Por exemplo, se tenho um array array = [1, 2, 3]; Quero uma string "123"
Comment: Vc precisa de um delimitador, antes de salvar no banco pode dar str_replace para removê-lo
Answer: Na função implode, eu coloquei o delimitador vazio e deu certo:
$resultado = implode('', $result);

Answer: O problema é que você está incluindo o $result na query e não o $resultado, você está fazendo:
$resultado = implode($result);

mysqli_query($con,"INSERT forum (...) VALUES('$result','$titulo','$mensagem');");
                                                 ^^^^

Basta alterar para $resultado e irá funcionar normalmente. O implode, como documentado, aceita inverter os parâmetros. Isso é, tanto implode($array, '') quanto implode('', $array) funcionam, assim como apenas usar o implode($array) neste caso.
Answer: Outra opção - não precisa de delimitador
Ideone
$meuArray = array( 1, 2, 3 );
$result="";
foreach( $meuArray  as  $valor ) {
    $result .= $valor;
}
echo $result; // 123

Sandbox
$meuArray =  [1, 2, 3];
$result="";
foreach( $meuArray  as  $valor ) {
    $result .= $valor;
}
echo $result;


how can i prove that square root of n is space constructible

Question: I know that square-root of n is space-constructible.
I can't prove it by the space-constructible definition.
How can I show that only $\sqrt{n}$ space is used?
Comment: @anon, my comment was intended to extract some useful information from OP. I think it succeeded.
Comment: You don't need anything nearly so complicated as Newton - keep in mind that there are no time bounds on the construction.  Just test all the numbers $t$ from 1 forward until you find one with $t^2\gt n$.  This is easy to do in $O(\log n)$ space just by using binary.
Comment: @Gerry: The tag says "complexity," so...
Comment: I don't know what space-constructible means. I know $\sqrt n$ is "Euclid-constructible," that is, I can show you how to construct it with ruler and compass, if that's what you want.
Comment: Thanks. space-constructible means that it is possible to construct in o(n) space from 1^n to the binary representation of f(n). in our case f(n) is sqrt. Maybe the Euclid-constructible can help.
Comment: @Orgad, when dealing with space complexity we normally use separate input (read only), output (write only), and working (r/w) tapes. The space is working tape space.
Comment: I am not sure here, but $\sqrt{x}$ is a nice function, so the Newton method should work nicely. Not sure if it converges fast enough though.... Basically, the Newton method om this case leads to the following iteration $x_0 =[ \sqrt{n}]$ (I think this is the best starting value) and uses $x_{n+1}=\frac{x_n + \frac{2}{x_n}}{2}$.
Comment: Relevant [1](http://math.stackexchange.com/questions/21231/importance-of-constructible-functions) and [2](http://cl-informatik.uibk.ac.at/teaching/ss07/alth/ohp/week5-2x2.pdf).
Comment: @Eyal: Note that Wikipedia specifies using $O(f(n))$ space rather than the $o(n)$ you quote in your comment. I assume that when $f(n)$ grows slower than linearly, you're supposed to imagine a read-only input tape and a restricted-length working tape.
Comment: Henning, exactly. There is the definition of space constructibility. and I need to prove that sqrt(n) applies to it.
Comment: @Steven: going forward from 1 was one of my thoughts. I just wasn't sure. Seems that this is the way.
Comment: The input is $1^n$. That means writing a binary representation of the input requires $n$ digits, not $\log n$.
Answer: Definition of space-constructible functions goes like this:

A function $f$ is space-constructible if there is a TM that, on input $1^n$, uses $\Theta(f(n)$ cells of the TM.

There might be slightly different version of this definition, but the point is that you can prove that a function is space-constructible simply by constructing a multi-tape TM that computes it and uses only $f(n)$ cells on every tape excluding the input tape (because you always need $n$ cells to read the input). However, you cannot write symbols on the first tape - it is read-only.
So for your example, such TM would be quite easy:
Lets have a 3-tape TM that uses the first tape for the input, second tape starts with the string "1" on it and the third tape is used for the output and is initially blank Note that I compute the function only in natural numbers (so lets assume $f(n) = \lceil \sqrt{n} \rceil$)!
You read the string on your second tape and move the appropriate number of cells on the input tape to the right. If you reach end of the second tape, you write "1" on your third output tape and move to the right and reset the position on second tape. Then you check the number of ones on your second tape and your third tape. If they are equal but you have not reached the end of the input yet, you have to continue. Increment the string on your second tape and reset everything on your third tape.
Continue until you reached the end of your input. You can easily see, that the third and second tape will finally have the square root on them and you wont need more that $\lceil \sqrt{n} \rceil$ cells.
Lets demonstrate it on a number $4$ ($x$ marks the head position before the cell):
input: x1111 -> 1x111 -> wait for check -> x1111 -> 11x11 -> 1111x -> end of input
second: x1 -> 1x -> check length and increment-> x11 -> 11x -> check and reset position -> x11
third: xB -> 1x -> check length and reset-> xB -> 1x -> check -> 11x -> check length and FINISH
If the number does not have a natural square root, we simply write one 1 on the third tape and output it. I hope its clear now.


Shiboken2 ... ImportError: DLL load failed: The specified procedure could not be found

Question: I've recently been running into a Import Error from Shiboken2 that I didn't have before with my executable. I have an application that I built in PyCharm and I build an .exe for it with CX_Freeze. I have tried every single latest version of Python but I am 100% confident now that it's not the version of Python I have that is the issue. Especially, since the app run's perfectly when I run the source code, but when I use my Setup.py script to build it, I keep getting the following issue when I try to run it:

Here is how my Setup.py script looks:
<code>import sys
import os
from cx_Freeze import setup, Executable

sys.path.append(os.path.abspath("./src/"))
sys.path.append(os.path.abspath("./src/gui/rc/"))
sys.path.append(os.path.abspath("./database/component_actions"))

# Dependencies are automatically detected, but it might need
# fine tuning.
buildOptions = {
    "packages": [
                # Facile sub-packages
                 "src.gui",
                 "src.qt_models",
                 "src.data",
                 "src.libs",
                 "src.tguiil",
                 "src.graphics",
                 "src.tools",
                 ],

    "includes": ["scipy.sparse.csgraph._validation",
                 "scipy.ndimage._ni_support",
                 "scipy._distributor_init",
                 ],

    "include_files": ["database/",
                      "src/tguiil/",
                      "src/data/"
                      ],

    "excludes": ["scipy.spatial.cKDTree",
                 ]
}

installOptions = {"skip_build":True}

base = None

# Uncomment for GUI applications to NOT show cmd window while running.
if sys.platform =='win32':
    base = 'Win32GUI'

executables = [
    Executable(script = 'src/facile.py', base=base, targetName = 'facile.exe', icon = 'resources/facade_logo_256.ico')
]

setup(name='***',
      version = '***',
      description = '***',
      options = {
          "build_exe": buildOptions,
          "install_exe": installOptions,
      },
      executables = executables)
</code>
(Sorry there's some info I am trying to remain hidden so that's why I use ***)
When the Problem started:
I updated Shiboken 2 to version 5.15.0 but I was aware that I needed to update Pyside2 so I don't understand how this updated packages effects my executable build.
If need be I can show all my dependencies but I don't think that's the issue because as I said before the source code works properly when I run it on PyCharm, only when I build the executable and try to run it do I have this issue. Maybe this is a PATH issue? Or some dependency I need to include in the setup.py?
B.T.W. I am using a Virtual Environment with Python 3.7.4 32-bit as the interpreter.
Answer: I believe this issue stemmed from my environment. I don't really have an adamant answer because I did multitude of things such as the following in this order:

Downloaded PyCharm 2020 and deleted PyCharm 2019.
Deleted all my past Python packages/executables/dependencies, deleted my old virtual environment, and old build folder.
Reinstalled Python 3.7.4 and checked the PATH tab.
Added a Python Interpreter with the Python 3.7.4 in virtual environment through PyCharm 2020's IDE.
Pip installed all necessary dependencies from my requirements.txt.

I made sure to give PyCharm enough time to think (load) in between each step and once I was finished I built the executable and it ran correctly.
I hope this helps anyone who runs into a similar problem.


The centre of the earth

Question: I'm a real beginner here (first post and first foray into math since high-school, trying to catch up), so I'm going to try my best to explain my problem in mathematical terms then follow up with an intuitive explanation. Thanks in advance!

Maths:

Given two points $A$ and $B$ on a sphere, where the coordinates and the angle of the normal of the tangential plane against the sphere of which are known, what is the centre of that sphere?

I'm not sure how best to represent such an angle, it seems it would vary based on the problem/context; perhaps somebody could advise? Let's say I start off with a spherical coordinate without the radius.
Intuitive:

If I am a person stood on a perfectly spherical planet, I can feel the direction of gravity. If I walk a known distance in a known direction, I can feel the pull of gravity pulling from a different absolute direction (assuming I possess an absolute orientation reference!). How can I calculate the centre of that planet and my position relative to it?

Thanks very much. Please excuse my weak expression of my problem.
Comment: If you have the two normal vectors at the given points (however they are described), then just extend them to lines...they intersect at the center.
Comment: I agree. and by the way, what do you mean by "an absolute orientation reference" ?
Comment: I was addressing a weakness in my metaphor; as an observer I would not be able to perceive a change in the direction of gravity (it would always be pointing down!). But that's one for physics SE.
Answer: Your problem is:

Let $S\subset\mathbb{R}^3$ be a sphere (remark: this will in fact work for an $(n-1)$-sphere in $\mathbb{R}^n$), and assume we are given two distinct points $x,y\in S$ not antipodal to each other and the normal vectors to the sphere at these points, $n_x,n_y$. What is the center (and radius) of the $S$?

The solution, as mentioned in the comments, is to take the straight lines defined by the normal vectors and intersect them. The lines can be parametrized by
$$\{x + tn_x\mid t\in\mathbb{R}\},\qquad\{y + sn_y\mid t\in\mathbb{R}\}.$$
Their intersection is the solution of
$$x + tn_x = y + sn_y.$$
This is an equation in the $3$ coordinates. Write it down and find $t$ (and $s$), then insert in the formula for the line through $x$ to find the center.
Remark: If the points are antipodal, then the lines coincide. In that case, the center is the point given by $c=\frac{x+y}{2}$.
Comment: Excellent answer thank you, I will work my way through what you have said!

The centre of the earth


Two signals supposed to be identical (plant redundancy) differ due to weather effects. How design a model to correct for weather effects?

Question: Context
Imagine a plant where a piece of equipment is placed outside, subject to wind and temperature changes. To monitor a specific aspect of this equipment, two identical sensors were installed at different positions at the equipment. The sensors and produced by the same manufacturer, and they measure exactly the same thing. Two sensors were installed due to redundancy. When the two signals differ by more than 5%, control room personnel receive an alarm and an automatic work order is generated instructed technicians to inspect the sensors to see what is wrong.
However, after some time in operation, it is noticed that the alarms generated often are not due to actual different readings by the sensors, but because the local environment where the sensors are placed are subject to different weather effects. Changes in wind direction, wind strength and temperature correlate with the observed sensor drift. A data scientist is now asked to build a model that quantifies how much of the drift that can be attributed to weather effects. The goal is for the control room staff to only receive alarms when the signals differ by more than 5% if these 5% cannot be explained by the weather effects.
Data
We have access to 5 years of wind strength data, wind direction data, and temperature data at the plant (not necessarily exactly where the sensors are placed). In addition, we also have access to 5 years of sensor data from both transmitters. All data is updated every 10 seconds.
My question
What would be a simple way to attack this model request? The question pertains only to what kind of model to build and how to analyze the data. Deploying the model into production is not within the question scope.
My initial thoughts
Make a regression model with the drift (difference between the two signals) and wind strength, wind direction, and temperature as features. Select subset of data for training:

subset where the two signals with confidence were not drifting
capture a range of different weather effects (strong wind, weak wind, all directions, large temperature range)

But I do not have much experience with this type of analysis. Can anyone provide some tips and guidance? I would love to prototype this in Python, and eventually move this to a more suited enterprise tool. I put the self-study tag to indicate that tips and hints are welcome, but this is an actual case from the industry.
Answer: I don't know about the 5 percent thing, but you could model the difference between the sensor readings using weather features. Then, when actual differences are "different enough" from predicted differences, you could sound the alarm.
If you have labels in your data that indicate when there actually was a problem, this is much easier. Then you could predict if there's a problem using sensor and weather data.
OP requested an example, so here goes. At any time t, suppose your sensor readings are
$$
s_1(t), s_2(t)
$$
and your weather readings are
$$
w_1(t),w_2(t), w_3(t)
$$
Let
$$
d(t) = s_1(t) - s_2(t)
$$
be the difference between sensor readings. Then you can fit a linear regression like
$$
d(t) = \beta_0 + \beta_1w_1(t) + \beta_2w_2(t) + \beta_3w_3(t)
$$
to the data. The idea here is your model "learns" the normal relationship between sensor differences and weather effects. Of course, it doesn't have to be a linear regression.
Once you've fit the model, you feed it weather data and monitor difference between the actual and predicted sensor differences:
$$
d(t) - \widehat{d}(t)
$$
If actual differences are "different enough" from predicted differences, then weather alone can't explain what's happening, so it must be something else. You'll have to define "different enough".
Comment: Could you provide more details about how you would attempt to fulfill the request outlined in the question? I only have the two sensor data and the three weather data time series mentioned in the question.


Continuity of $f^{-1}$

Question: So I read this theorem:

Suppose $f$ is a continuous $1-1$ mapping of a compact metric space $X$ onto a metric space $Y$. Then the inverse mapping $f^{-1}$ defined on $Y$ by $$ f^{-1}(f(x))=x \quad x\in X $$ is a continuous mapping of $Y$ onto $X$.

Can someone please explain why compactness of $X$ is necessary here? I mean why is it not possible for $f^{-1}$ to be continuous without $X$ being compact?
Answer: Let's just prove it, and will be clear where we'll use compactness. Let $F \subseteq X$ be closed. Let's prove that the inverse image $(f^{-1})^{-1}(F)$ of $F$ by $f^{-1}$ is closed. Since $F$ is closed and $X$ is compact, $F$ is also compact. Since $F$ is continuous, $f(F) = (f^{-1})^{-1}(F)$ is compact in $Y$. Since $Y$ is a metric space, it is Hausdorff, and in these spaces, every compact set is closed. As we wanted.
Essential facts:

Direct image of a compact set by a continuous function is compact;
A closed subset of a compact set is compact itself;
Every compact subset of a Hausdorff space is closed.
Answer: Counterexample: $f: [0, 2\pi) \to \mathbb S^1$, $f(t) = e^{it}$ is continuous and bijective, but do not have a continuous inverse.
To see why the inverse is not continuous, note that
$$f([0, \pi) = \{ e^{it} | t\in [0, \pi)\}$$
is not open in $\mathbb S^1$. That is, for the open set $[0, \pi)$ in $[0, 2\pi)$, $f^{-1}([0, \pi))$ is not open in $\mathbb S^1$. Thus $f^{-1}$ is not continuous.
Comment: Why is the inverse not continuous?
Comment: @YoTengoUnLCD Please see the edit.
Answer: Let $X=\{0\}\cup (1,2]$.  let $Y=[1,2]$. Let $f(0)=1$. Let $f(x)=x$ for $x\in (1,2]$.Then $f$ is a continuous bijection but the inverse $g=f^{-1}$ is not continuous because $A=(1,2)$ is closed in $X$ but $g^{-1}A=f A=A$ is not closed in $Y$.The key point of the proof of the theorem is that if $X$ were a compact Hausdorff space then any closed $A\subset X$ is compact so its image under $f$ is compact,and therefore closed in $Y$ (because $Y$ is Hausdorff) .Note that we can replace "metric" with the more general "Hausdorff".

Continuity of $f^{-1}$


When Something Goes Wrong: Good contingency planning?

Question: I work at a small firm with little technical skill/knowledge.
One colleague had a hard drive die without any backup, and we recently had a virus come through and infect our test server (the gumblar.cn one) which we may or may not have transferred to a client's server.
After these two events, management danced around promoting good practices to avoid future occurences, for about a week.
Changing the company's culture to take this more seriously is one problem I'll try and deal with, but my question is...
What events should be planned for?
I suppose there are natural disasters, hardware failures, people quitting (bus factor?).
Comment: This question may or may not be programming related; as it is currently written I'm inclined to either edit or close it.
Comment: We can pretend it is programming related - the problems outlined can occur at any software development shop and it would be nice to kinow what to plan for in advance.
Comment: Edit it since programming or scripts may come into play.
Comment: Admittedly, the hard drive lost belonged to a designer with little code onboard, but to have 100's of man hours disappear is frustrating/irreversable for anyone.
Answer: Think for a while what equipment and services you use and how likely it is that they fail or become unavailable for a while. Build a list. Evaluate how likely each problem is to happen, how much it will cost you and how much a backup solution costs. Then you decide.
Answer: Here are some common things:

Shared Directories on a Fault Tolerant server to be used as a policy for user files & data (with appropriate security). Event=data loss limitation
Scheduled Backups of the Server. Event= data loss limitation
Firewall Proxy with logging and intrusion detection. Event=Data damage and theft
Enterprise Virus Software deployed on server and clients. Event=Virus Infection, Data theft, System Damage
Automated IT assets tracking software that reports on hardware and software changes happening on servers and clients. Event=Data and Hardware theft, unauthorised modification
Off Site storage of data. Event=Data Loss limitation
Firefighting Equipment & Automated firefighting mechanisms. Event=Fire
Internet Filtering Proxy such as WebMarshall. Event=Protection against "drive-by" infections and risks.

etc. etc. You should be able to find much more comprehensive strategies, measures etc. on the Internet.
Comment: Fire, yeah... if the data back up is still in the same building and it goes up in flames, it's a bit pointless...


Difference between different kinds and brands of reeds on the alto sax

Question: I've been playing with traditional Vandoren reed strenght 3, and Rico Royal strengt 3,5
I want to try the Vandoren Java.
What is the difference between red and green java? And is is a big difference from the traditonal Vandoren or Rico?
Comment: This is entirely a matter of what works for you.  Not to mention that, if reeds are anything like what they were in my playing days :-( , you'll have to try at least a couple boxes' worth to decide if the good ones are to your liking.
Comment: True that what you prefer is a matter of what works for you, but I think the questioner is asking about the objective difference between two products by the same manufacturer. Since I haven't actually used them, I'll just post this as a comment, but according to their website the red box is supposed to be more flexible and have more "tonal body" than the green. My guess is that green will be a bit firmer and easier to control, but red may have more pitch flexibility.
Comment: As a clarinetist, I find that Vandorens are about 1/4 strength harder than Ricos.
Answer: If you have a local music shop, I'd suggest going down there and sampling a few different varieties. I, personally, have found little success with the JAVA series and have seen better consistency in Rico Jazz Select (cheaper, too!).


Where is my container's memory going?

Question: I have a container leaking memory. Or at least, reported memory consumption goes up fast. If I run top, I get this:
top - 16:56:51 up 6 days, 17:25,  0 users,  load average: 0.16, 0.27, 0.31
Tasks:   4 total,   1 running,   3 sleeping,   0 stopped,   0 zombie
%Cpu(s):  1.3 us,  0.7 sy,  0.0 ni, 98.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem:   7676380 total,  4089380 used,  3587000 free,   675164 buffers
KiB Swap:        0 total,        0 used,        0 free.  2586496 cached Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
    1 root      20   0   46924  15196   6456 S   0.0  0.2   0:15.54 supervisord
    8 root      20   0 3526084  47976  29660 S   0.0  0.6   0:59.15 dotnet
  568 root      20   0   20364   3332   2728 S   0.0  0.0   0:00.09 bash
 2800 root      20   0   21956   2420   2032 R   0.0  0.0   0:00.00 top

The number I get reported out at this moment is 90M, which doesn't add up. I'm pretty sure /sys/fs/cgroup/memory/memory.usage_in_bytes is what's reported:
> cd /sys/fs/cgroup/memory/
> cat cgroup.procs
1
8
568
2494

> cat memory.usage_in_bytes
92282880

> pmap -p 1 | tail -n 1
 total            46924K

> pmap -p 8 | tail -n 1
 total          3599848K

> pmap -p 568 | tail -n 1
 total            20364K

> ps 2494
  PID TTY      STAT   TIME COMMAND

To my eyes there's a significant amount of memory 'missing' here... if I cat memory.usage_in_bytes again over the time I've been typing this:
> cat memory.usage_in_bytes
112291840

> pmap -p 1 | tail -n 1
 total            46924K

> pmap -p 8 | tail -n 1
 total          3452320K

> pmap -p 568 | tail -n 1
 total            20368K

Nothing obviously accounts for this memory usage. If I look at memory.stat I see this:
# cat memory.stat
cache 89698304
rss 30699520
rss_huge 0
mapped_file 1552384
writeback 0
pgpgin 102007
pgpgout 72613
pgfault 115021
pgmajfault 8
inactive_anon 1519616
active_anon 30789632
inactive_file 417792
active_file 87654400
unevictable 4096
hierarchical_memory_limit 18446744073709551615
total_cache 89698304
total_rss 30699520
total_rss_huge 0
total_mapped_file 1552384
total_writeback 0
total_pgpgin 102007
total_pgpgout 72613
total_pgfault 115021
total_pgmajfault 8
total_inactive_anon 1519616
total_active_anon 30789632
total_inactive_file 417792
total_active_file 87654400
total_unevictable 4096

Then a short moment later:
# cat memory.stat
cache 89972736
rss 30777344
rss_huge 0
mapped_file 1552384
writeback 0
pgpgin 102316
pgpgout 72836
pgfault 115674
pgmajfault 8
inactive_anon 1519616
active_anon 30867456
inactive_file 417792
active_file 87928832
unevictable 4096
hierarchical_memory_limit 18446744073709551615
total_cache 89972736
total_rss 30777344
total_rss_huge 0
total_mapped_file 1552384
total_writeback 0
total_pgpgin 102316
total_pgpgout 72836
total_pgfault 115674
total_pgmajfault 8
total_inactive_anon 1519616
total_active_anon 30867456
total_inactive_file 417792
total_active_file 87928832
total_unevictable 4096

But I'll be honest; I don't really know what I'm looking at here. I am looking suspicously at active_file but again; I don't really know what I'm looking at.
Some notes and observations:

The container is scheduled by Kubernetes.
This program writes a lot of data to stdout.
Reducing the amount of data written to console to near-zero solves the reported memory leak.
Deploying a program which only writes lots of data to stdout does not seem exhibit the same memory leak(!)

So! How should I go about finding where this memory is getting consumed? Is there anything obvious to anybody - perhaps something's staring me in the face or I'm not looking at something I should be?
Thanks!
Answer: In short; memory.usage includes disk caches. I should have been measuring (memory.usage - memory.cache). The issue (and perceived memory leak) was that supervisord was writing my program's stdout to a log file.

Where is my container's memory going?


Generics and Entity Framework: How do I return a different type depending on a column value

Question: We have a Persons table which stores different types of persons (Buyer, Seller, Agent, etc).  Our ORM is Entity Framework CodeFirst (CTP5).  We're using the repository pattern for good TDD and mocking.  In the PersonRepository I want to return a specific type so I can do things like this:
Agent a = repository.Get<Agent>(5005);  // Where 5005 is just an example Id for the person
a.SomeAgentProperty = someValue;
Buyer b = repository.Get<Buyer>(253);   // Again, a simple PersonId.
b.SomeBuyerProperty = someOtherValue;

The idea is that I know what kind of person I'm getting when I get it from the repository.  And, yes, I could just create X different Get methods called GetBuyer(int PersonId), GetSeller(int PersonId) and so on.  But that has a code smell.
How would the generic function look?
Here is my repository interface so far:
public interface IPersonRepository
{
    Person Get(int PersonId);   // To get a generic person
    T Get<T>(int PersonId);     // To get a specific type of person (buyer, agent, etc.)
    void Save(Person person);
    void Delete(int p);
}

And my concrete implementation:
    public T Get<T>(int PersonId)
    {
        //Here's my question: What goes here?
    }

Answer: Use the OfType<T>() method, which will result in EF doing an INNER JOIN to the specified T if your using TPT, or a filter based on the discriminator if your using TPH.
public TPerson Get<TPerson>(int PersonId) where TPerson : Person
{
    return ctx.People
              .OfType<TPerson>()
              .SingleOrDefault(x => x.PersonId == PersonId);
}

And that will work just like you wanted:
Agent a = repository.Get<Agent>(5005);

Answer: I would suggest to use something like:
public T Get<T>(int PersonId) where T: new()
{
    return new T(PersonId);
}

and load data in the constructor or implement some kind of Load method for each type of your entity, like:
interface IEntity
{
    void Load(int Id);
}

class CBuyer: IEntity
{
    public Load(int Id) { ... }
}

public T Get<T>(int PersonId) where T: IEntity, new()
{
    T ent = new T();
    ent.Load(PersonId);
    return ent;
}

Comment: I wish I could see the code for Load, how does it get the Context?


ListView está travando

Question: Tenho está ListView
layout.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    xmlns:ads = "http://schemas.android.com/apk/res-auto"
    android:background="#ffffff"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/praias"
        android:layout_centerHorizontal="true"
        android:layout_above="@+id/adView" />

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="@string/banner_unit_id"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>

</RelativeLayout>

Main.class:
adapter = new AdapterListView(MainActivity.this,this,itens,height,width,daoItem,getPackageName());
praias.setAdapter(adapter);
praias.setClickable(true);

Adapter.class:
public View getView(final int position, View v, ViewGroup parent) {
    view = mInflater.inflate(R.layout.layoutlist, null);
    final ListenerItem categoria = itens.get(position);

    TextView nomePraia = (TextView) view.findViewById(R.id.nomePraia);
    ImageView foto = (ImageView) view.findViewById(R.id.fotoPraia);
    final ImageButton btFavorito = (ImageButton) view.findViewById(R.id.btFavorito);
    TextView descricao = (TextView) view.findViewById(R.id.descricao);
    final Button bt = (Button) view.findViewById(R.id.bt);
    Button btVerMais = (Button) view.findViewById(R.id.btVerMais);
    String desc="";
    if(categoria.descricao.length()<397){
        btVerMais.setVisibility(View.INVISIBLE);
        desc=categoria.descricao;
    }else{
        for(int i=0;i<397;i++){
            desc=desc+categoria.descricao.charAt(i);
        }
        desc=desc+"...";
    }
    nomePraia.setText(categoria.nome);
    String nome_foto = categoria.nome_foto;
    int drawableId = context.getResources().getIdentifier(nome_foto, "drawable", packageName);
    Log.e(R.drawable.itanhaem3+"",drawableId+"");
    Bitmap bm = BitmapFactory.decodeResource(context.getResources(), drawableId);
    bm = bm.createScaledBitmap(bm, width, width, true);
    foto.setImageBitmap(bm);

    descricao.setText(desc);

    if (categoria.favorito) {
        btFavorito.setImageResource(R.drawable.ic_action_favorite_v);
    } else {
        btFavorito.setImageResource(R.drawable.ic_action_favorite);
    }
    btVerMais.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ListenerItem listenerItem = new ListenerItem();

            listenerItem = itens.get(position);
            open(listenerItem);
        }
    });
    btFavorito.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ListenerItem listenerItem = new ListenerItem();

            listenerItem = itens.get(position);

            if (listenerItem.favorito) {
                listenerItem.favorito = false;
                btFavorito.setImageResource(R.drawable.ic_action_favorite);
                chamaToast(false);
            } else {
                listenerItem.favorito = true;
                btFavorito.setImageResource(R.drawable.ic_action_favorite_v);
                chamaToast(true);
            }

            try {
                daoItem.createOrUpdate(listenerItem);

            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    });
    activity.registerForContextMenu(bt);

    bt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            bt.setId(position);
            activity.openContextMenu(bt);

        }
    });

    return view;
}

public void open(ListenerItem listenerItem){
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
    alertDialogBuilder.setMessage(Html.fromHtml(listenerItem.descricao));
    alertDialogBuilder.setPositiveButton(R.string.voltar,null);
    AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.show();
}

Sempre que esta passando de um item para o outro trava um pouco , não sei o porque está tão lento este ListView
Imagens de tela:
Comment: Como assim está travando, você quer dizer na hora da atualização?
Comment: quando vai descendo no scroll , indo para os itens debaixo, ai fica lento, parece que e na hora de carregar o item debaixo
Comment: Na verdade os items já estão carregados.. tem imagens nessa listView?
Comment: sim, tem uma imagem. vou colocar uma foto
Answer: O problema é que voce esta processando sua imagem dentro de sua UI Thread.
Para resolver este problema, o próprio Google recomenda voce utilizar o AsyncTask, que executa tudo em background e fora de sua UI Thread.
public class LoadBitmapImage extends AsyncTask<Integer, Void, Bitmap> {
    private final WeakReference<ImageView> imageViewReference;

    public LoadBitmapImage(ImageView imageView) {
        // Use a WeakReference para ter certeza que sua ImageView sera reciclada
        imageViewReference = new WeakReference<ImageView>(imageView);
    }

    // Carregando a imagem em background
    @Override
    protected Bitmap doInBackground(Integer... drawableId) {
        // Aqui voce utiliza seus proprios metodos para carregar e configurar sua Bitmap
        Bitmap bm = BitmapFactory.decodeResource(context.getResources(), drawableId);
        bm = bm.createScaledBitmap(bm, width, width, true);
        return bm;
    }

    // Quando completo, coloque o Bitmap em sua ImageView
    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (imageViewReference != null && bitmap != null) {
            final ImageView imageView = imageViewReference.get();
            if (imageView != null) {
                imageView.setImageBitmap(bitmap);
            }
        }
    }
}

E, para carregar sua imagem, voce simplesmente:
...
ImageView foto = (ImageView) view.findViewById(R.id.fotoPraia);
LoadBitmapImage bitmapTask = new LoadBitmapImage(foto);
bitmapTask.execute(drawableId);
...

Referencia: http://developer.android.com/training/displaying-bitmaps/process-bitmap.html
Comment: se for passando o listview sem parar para esperar carregar a imagem nem mostra nenhuma imagem, tem como usar isso ai para carregar todas as imagens assim que entrar na pagina? este metodo esta demorando para carregar a imagem e se passar do item sem esperar carregar a imagem ela nem aparece
Comment: @IlgnerdeOliveira isto não é nada performatico para seu aplicativo. Entrar em uma tela e carregar todos os drawables em background. Além de consumir muita memoria, pode dar problemas de OutOfMemory. Fica a seu critério.
Comment: ata , mais desse jeito ai nem posso usar porque esta demorando muito pra carregar a imagem , tava pensando em carregar umas duas fotos a frente da que a pessoa está nela ou algo assim porque desse jeito que está só começa a carregar quando chega o item e o texto carrega primeiro e fica la o texto sem a foto
Comment: @IlgnerdeOliveira tente implementar de uma maneira que que te atenda! Voce pode criar um cache das imagens e trabalhar com elas. Mas tente utilizar o este método para carregar as imagens para que nao enfrente problemas de memoria
Comment: vou olhar aqui e qualquer coisa que der errado eu pergunto de novo

ListView está travando


How do I create custom content types in drupal?

Question: I need to create a content type that is similar to a textarea, but I need to do some server side formatting before it can go out to the page.
I want a user to be able to input a chunk of text, then if it's a link of a certain type, leave it be, if it's not a link, create a link of sorts out of it with some server side (PHP) magic.
Can this be done in Drupal? Can someone point me in the right direction?
Answer: Yes this can be done.
For the processing you'll need a custom module. Here's a tutorial to get you started with modules.
As for the specific functions you'll need all revolve around hook_nodeapi (d6) or node api hooks (d7).
Comment: All I need is to do some processing/formatting to the input that the user provides, then everything else should fit right in. Isn't creating a module for this overkill? Can't I just add a line or two of code to an existing module?
Comment: If you add a few lines of code to an existing contrib module what happens when you upgrade it in the future? You'll have to re do your change again, general rule of thumb is not to hack core or contrib.
Answer: If I understand well your question, I guess that you can use Field validation module http://drupal.org/project/field_validation for validate fields in Drupal 7

This module adds an extra tab to each field instance, allowing you to
  specify validation rules for your field instances.
By using the hooks provided by this module, you can also define your
  own validation rules in your own modules.

this link could be useful to read about write complex rules to your field with Field validation module http://www.lullabot.com/articles/module-monday-field-validation
if you are a developer, RichForm http://drupal.org/project/richform is another module for validate fields:

This module gives you a framework to easily make client side and
  server side validation in the same time. It increases the validation
  features of the Drupal Form API (FAPI). If you want to have a powerful
  tool for creating custom validation logic to any text field, without
  writing a tons of codes, this module is for you!

Also, Form API Validation seems a possible alternative http://drupal.org/project/fapi_validation


Best Linux network tuning tips?

Question: I figure it's a good idea to have a central question for linux network tuning. I'm particularly interested in tuning options for the following.

Web server
Varnish / Squid server
DB server
File server
App server
Comment: So much of this is very subjective based on what you're actually doing/serving.
Comment: ... and different applications require different OS tuning. For example a fileserver on a lan should be configured with big buffers to server a moderate number of users quickly. Whereas a public website must be frugal with the amount of buffering offered to each connection to avoid resource starvation
Comment: That's why I'm looking for different suggestions for different servers, not a one size fits all.
Comment: Then ask separate questions.
Answer: At this point it's generally not worth the overhead of "tuning" within the kernel. Unless you have the test gear (or a cluster to A/B options on) and very good documentation it's not worth it for the sys-admin overhead alone.
These days the linux kernel has good auto-tuning of TCP, you can do better, however those are not the default because they're seen to be "greedy" and not fair on the Internet.
As close as I get to tuning is:

Mount noatime (implies nodiratime), using ext3 or xfs
Run only needed services
For services like squid & mysql allow them to use more RAM

With rare exceptions those three will get you 90% there.
Answer: network stack tuning i put on routers/firewalls connecting to net hundreds of users
<code>net/ipv4/route/max_size=262144
net/ipv4/netfilter/ip_conntrack_tcp_timeout_established=18000
net/ipv4/neigh/default/gc_thresh1=1024
net/ipv4/neigh/default/gc_thresh2=2048
net/ipv4/neigh/default/gc_thresh3=4096
net/netfilter/nf_conntrack_max=128000
net/netfilter/nf_conntrack_expect_max=4096
</code>
[ mostly empirically tweaked over years based on some unpleasant crashes ]
for file/sql/poroxy server you definitivly want to mount partition with data/spool with noatime,nodiratime and for reiserfs - notail ] to avoid additional disk trashing whenever file is accessed for read.
and you really want [ maybe you don't know about it yet ;-] gather statistics - eg with munin - to do some capacity planning and see if your tuning indeed decreases system load.
Answer: One thing you may want to look at web100.  It is a kernel patch, and a userland too designed to optimized the Linux kernel for high-performance networks.

The Web100 project is aimed at
  producing a complete host-software
  environment that will run common Web
  applications at 100% of the available
  bandwidth, regardless of the magnitude
  of a network's capability.

Also check out High Performance SSH/SCP.

SCP and the underlying SSH2 protocol
  implementation in OpenSSH is network
  performance limited by statically
  defined internal flow control buffers.
  These buffers often end up acting as a
  bottleneck for network throughput of
  SCP, especially on long and high
  bandwith network links. Modifying the
  ssh code to allow the buffers to be
  defined at run time eliminates this
  bottleneck.

Best Linux network tuning tips?


The PHP version is not correctly detected

Question: I'm running Drush on a host that has multiple versions of PHP installed. By default, php --version would give you 5.2.x, but php53 and php54 are also available. So in my .bash_profile I have alias php='php53' (php --version = 5.3.13), which makes Drush work for some commands.
However, I still get "Your command line PHP installation is too old. Drush requires at least PHP 5.3.2" messages for some very useful commands.
A couple of really annoying instances:

drush pm-update will report that it has backed up and then updated all modules and core successfully, then return the PHP version error and restore all backups. In verbose mode, the command that comes before the error is:
php /home/[username]/drush/drush.php  --backend=2 --verbose --root=/home/[username]/webapps/drup --uri=http://[domain name]  pm-updatecode-postupdate 2>&1
drush archive-dump will immediately return the PHP version error

The other commands I have tried so far work fine. I'm not sure why this would be, if Drush is sometimes using the php53 binary, and other times not, or if it just has trouble detecting the version correctly, but some very important features are broken for me.
Answer: Drush doesn't use just php directly as a command, but need the full path. With the last version of Drush, you can add the following line in your .bash_profile to set which PHP you want to use.
export DRUSH_PHP=/Applications/MAMP/bin/php/php5.3.14/bin/php
Here is just an example of MAMP, but you can change it as well with your own path.
Comment: To refine the above explanation: when Drush calls back into itself, it execs 'php', and takes the first php in  your PATH.  Bash aliases are not visible to php exec, so alias php='php53' is not seen. The explanation above fixes the problem, as would `export DRUSH_PHP=php53`. Another solution would be to insure that php53 appears in your PATH before any other PHP when running Drush.
Comment: If you're using MAMP, configuration of the DRUSH_PHP variable is not enough.

See http://docs.drush.org/en/7.x/configure/
Comment: DRUSH_PHP is only supported in Drush 8 and earlier. In Drush 9 and later, you are expected to manage your PATH such that the PHP that you would like to use appears first.
Comment: if you are using oh_my_zsh be sure .zshrc
Comment: This fixed it for me. Though I had to do `source .bash_profile` for immediate effect
Comment: sorry but I can't understand where this file .bash_profile is. Where can I find ti?
Comment: @Shyghar it's placed at your home folder, on Linux you can see it by doing `cd` and then `ls -la` If you don't see the file, look on Google how to create it.
Comment: @yvan I'm on windows 10
Comment: Adding the following to .zshrc fixed it for me `export DRUSH_PHP=/Applications/MAMP/bin/php/php7.4.2/bin/php` and
`export PATH="/Applications/MAMP/Library/bin:/Applications/MAMP/bin/php/php7.4.2/bin:$PATH"` afterwards you have to run `source ~/.zshrc`
Answer: Here's another solution:

Create a symbolic link to the desired version of php (php5.4 in my case)
ln -s /usr/local/bin/php54 ~/bin/php
we need to change precedence of ~/bin in $PATH so that it appeared before /usr/local/bin (where original php interpreter sits)
find ~/ -name .bash_profile | xargs sed -i 's/\$PATH:\$HOME/bin/\$HOME/bin:\$PATH/g'
reload .bash_profile
source ~/.bash_profile

sourced from: http://webiga.com/2013/06/22/change-default-version-of-php-at-webfaction/
Answer: The problem for me was that drush was not working with the correct PHP version, I think the problem was that it was using PHP 7, and I needed PHP 5.6. So I ran in the console:

whereis php

And the result was (see the PHP path for 5.6):

Then I went to my ~/.bashrc file and added one line that sets PHP path for drush (and save):

export DRUSH_PHP=/usr/bin/php5.6

Then I updated .bashrc with the following (don't miss the point in the start of the command):

. ~/.bashrc

And that's it! Hope that helps!
Answer: I just ran into this after upgrading Drush on Windows. The server has an older version of PHP running and it wasn't picking up the version of PHP that came with Drush.
I was able to fix it by specifying the path to php.exe in the drush.bat file, located at
c:\documents and settings\all users\application data\drush\drush.bat
I just added the full path to PHP by changing this line:
@php.exe "%~dp0drush.php" --php="php.exe" %*
to this:
@"c:\program files\drush\php\php.exe" "%~dp0drush.php" --php="php.exe" %*
Answer: Assuming you installed drush with composer and you want to run your command using php 7 (this works for acquia cloud envs too)
/usr/local/php7.1/bin/php ../vendor/drush/drush/drush.php --php=/usr/local/php7.1/bin/php [your_drush_command_here]


Interpreting 2D correspondence analysis plots (Part II)

Question: I'd like to ensure that I understand the process correctly. This is a follow-up question to Interpreting 2D correspondence analysis plots
library(reshape)
library(ca)

df <- read.csv(file="http://www.bertelsen.ca/R/smokers.csv")
colnames(df)[7] <- "value"  ## make reshape smart
df <- cast(df, SMOKER ~ GEO) ## reshape data
row.names(df) <- df$SMOKER ## rename rows
df <- df[2:ncol(df)] ## reset df
df <- df[-4,] ## Let's only look at people who have smoked
df <- df[c("AB","BC","ON","QC")] ## and only the biggest 4 provinces (KISS)
plot(ca(df))

summary(ca(df))

Output
Principal inertias (eigenvalues):

 dim    value      %   cum%   scree plot
 1      0.002523  99.9  99.9  *************************
 2      3e-06000   0.1 100.0
 3      00000000   0.0 100.0
        -------- -----
 Total: 0.002526 100.0

Rows:
    name   mass  qlt  inr    k=1  cor ctr    k=2 cor ctr
1 | Crrn |  265 1000  191 |  -43 1000 191 |    1   0  43 |
2 | Dlys |  201 1000  351 |  -66 1000 351 |   -1   0  70 |
3 | Frmr |  470 1000  432 |   48 1000 432 |   -1   0  98 |
4 | Occs |   65 1000   26 |   31  964  25 |    6  36 789 |

Columns:
    name   mass  qlt  inr    k=1  cor ctr    k=2 cor ctr
1 |   AB |  116 1000  146 |  -56 1000 146 |   -1   0  34 |
2 |   BC |  142 1000  775 |  118 1000 776 |   -1   0  41 |
3 |   ON |  434 1000    7 |   -6  909   6 |    2  91 540 |
4 |   QC |  308 1000   72 |  -24  994  72 |   -2   6 385 |

Looking at summary(ca(df)) I see that nearly 100% of the inertia is described by the row profile for both modalities (Type of smoker and Province, respectively).

What (I think) should be immediate takeaways are:

You are more likely to be a daily smoker if you live in AB, QC, or ON
You are more likely to be a former smoker if you live in BC
You are least likely to be a daily smoker if you live in BC (this fits with Canadian wide understanding of BC's "active lifestyle" culture)

What could we say about occasional smokers? What would your analysis tell us through this correspondence plot and it's associated summary?
Data Source: Statistics Canada, Canadian Community Health Survey (CCHS 3.1), 2005. The CANSIM table 105-0427 was an update of CANSIM table 105-0227. More current data are in CANSIM tables 105-0501 and 105-0502.
Answer: I'm an ecologist, so I apologise in advance is this sounds a bit strange :-)
I like to think of these plots in terms of weighted averages. The region points are at the weighted averages of the smoking status classes and vice versa.
The problem with the above figure is the axis scaling and the fact that you can't display all the relationships (chi-square distance between regions and chi-square distance between smoking status) on the one figure. By the looks of it, the figure is using a what is known as symmetric scaling which has been shown to be a good compromise preserving as much of the information in the sets of scores as possible.
I'm not familiar with the ca package but I am with the vegan package and it's cca function:
require(vegan)
df <- data.frame(df)
ord <- cca(df)
plot(ord, scaling = 3)

The last plot is a bit easier to read than the one you show but AFAICT they are the same (or at least similarly scaled).
So I would say that occasional smokers are lower in number than expected in QC, BC and AB, and most associated with ON, but that in all regions, occasional smokers are low in number - they differ markedly from the expected number.
However, there is a single dominant "gradient" or axis of variation in these data and as the second axis represents so little variation, I would likely not interpret this component at all.
Comment: I would probably add, that the difference between regions in terms of occaisonal smokers is largely restricted to the second axis of variation and as we can see in the stats this is such as small component as to not really be worth interpreting. The structure in the data is really the contrast between BC and the other regions in terms of daily smokers and former smokers.


What does stringsAsFactors = F while reading data in R mean?

Question: I have seen many code samples which use the setting while reading data:
train <- read.csv('train.csv', stringsAsFactors = F)
test  <- read.csv('test.csv', stringsAsFactors = F)

From a little search I understand that it prevents conversion of string to factors and treats them as vectors.
But what are factors and why are they required?
Vectors should have same datatype , logically string is one , so why convert string fields to factors?
When do you use stringsAsFactors = T ?
Comment: https://www.stat.berkeley.edu/classes/s133/factors.html
Answer: Factors are variables, that have set and unchangeable (it is possible, but you shouldn't need to do it) values  called levels. The corresponding variable then only holds a number corresponding to the index of the level instead of the actual string.
It is memory-wise much less expensive to hold a vector of integers that strings!!
Say you have a x=("hello", "name", "hello) you can convert it to a factor variable and you will have x = c(1,2,1), levels=c("hello", "name"). The more strings and longer you have the more memory you save.
Also from computational point of view, you cannot put strings in models, so it represents categorical variables, as pointed out by Ray
Answer: Factors are important when you are doing classification or regression in R using categorical variables. Try fitting a simple linear regression model (lm) with just a vector of strings. It is going to give you an error. Give it a shot with factors, and it will go through.
If you are looking for a more in-depth answer, please read the following link:
https://www.stat.berkeley.edu/classes/s133/factors.html
Comment: Your statement is false, R will convert a character vector for you.


creating DLL in VB NET. Does it HAVE to be a class library?

Question: I have another program (which I do not have the source for, already compiled) that is calling a DLL.  I want to swap the DLL out and put my own in its place, to run my own model (inside the DLLs) rather than this other one that comes with the software.
I have made DLLs in fortran using the G95 compiler that work declaring them dynamically similarly to below.  I have also looked into creating DLLs with VB NET, but it seems the only options are Class Libaries, which don't get called and declared the same way as below.  I do have the code for the function declaration from the developer, but that is all (see below)
Is there a way to create working DLLs using VB NET that don't involve the class library, that I could name the dll file "318dll.dll", and have a function inside called CalcLoss and thats it, not class, one function only.  The only way I know how to create DLLs using VB NET involves the class library and then you have to include existing item, and it magically appears available in your project.
Here is the template calling code for the fortran DLLs that have been working.  how do I make this kind of DLL with VB NET?
Thanks.
<code>Declare Sub CalcLoss Lib "318dll.dll" (TanTHT As Single, RanTHT As Single, EXTNSN As Boolean, ByVal Mode As String, Loss As Single, FSPLSS As Single)
</code>
Comment: As everyone else has said so far, the short answer is, you don't; at least, not without some workarounds which take more time and introduce more unnecessary potential for problems than the function you want to write. I'd be interested to know why the insistence on VB.NET though—is it specifically to practise VB, or can you not do what you need to in some other language (like Fortran)?
Answer: I think you are wanting to create a Win32 DLL library, not a .NET library. That's why you're not seeing the option you want. I'm not sure if there is a way to do this in VB.NET. You will probably need to switch to regular Visual Basic (not .NET) to do this.
Comment: VB6 won't do it either.  It will create COM class libraries.  It'll have to be native C/C++ to create a DLL with a native API.
Comment: @Toby: VB6 can actually coerced to produce Win32 DLLs, but there's a certain bit of hackery involved.
Answer: Option 1: Use C++/ME to create an unmanaged wrapper around your VB.NET DLL.
Option 2: Upgrade to a .NET version of FORTRAN.
Answer: If you want to write code in .Net (VB.Net/C#) and expose it to unmanaged applications you could try this tool I wrote.  It allows you to tag methods with Attributes and then reassembly the assembly to allow the exports.
There is no support on this code, but it's free to play with.  USE AT YOUR OWN RISK

Unmanaged Exports

... Extended ...
After you compile you assembly with my ExportAttribute on the method to export, you can run the recompiler to disassemble and reassemble your application.  This was only written as a proof of concept.  But it worked between VB6/Winbatch to .Net assemblies.  It was also tested with the .Net DllImport/PInvoke functions.
Answer: Johnathan allen's option #1 is best. You can use .NET libs from c++, VB6.0, etc, MS has docs on it. Just use your library like that.
HOWEVER if for some reason that won't work do this (warning, HACK) if you only call it 1x/sec or less:
C++ Win32 dll: writes the parm values to a temp file. Starts your VB.NET code compiled into an EXE that reads the tempfile and writes another w/ the results. Win32 dll waits for the VB EXE, then reads the file (really wait for it with process lib, don't just look for the output file, that's a world of pain waiting to happen)
Answer: So the solution turned out to be just to write the DLL in fortran, because i could just copy and paste a bunch of code already infortran, rather than have a VB dll call a fortran dll.  now its all one dll, and it works.  Thanks for everyone's input.


Wordpress Job Manager Pagination Issue

Question: I am using the Wordpress Jobs Manager plugin. It all works great except the pagination feature. When enabled, the pagination buttons just redirect the user to the homepage instead of to the paginated page of results. As far as i can tell this is a problem with permalinks but i wondered if anyone had a workaround as i cannot use the standard permalinks on my site.
Does anyone know of any possible fixes? Its a custom built theme.
Thanks for you help guys
Answer: I know its a long time since you posted, but i had this issue today and came across your post.
I did some investigating and found it to be a problem with the way wordpress was dealing with the rewrites for canonical redirects.
I did a search on the internet and found the following post on wordpress.stackexchange.com by Celine Garel which deals with this situation for custom post types, i just changed it to work with a page.
https://wordpress.stackexchange.com/questions/134339/pagination-on-custom-post-type-not-working-if-permalinks-set-to-rewrite-url
Add this to your themes function file,
<code>    add_filter( 'redirect_canonical','custom_disable_redirect_canonical' );
function custom_disable_redirect_canonical( $redirect_url ){
    if ( is_page('Your_job_list_page') ) $redirect_url = false;
    return $redirect_url;
}
</code>
This fixed it for me. Hope it helps you or anybody else.
Comment: This is really cool - thank you. However, i notice that this function really messes up the order of jobs and they even change order each time you visit the page. Any ideas how to fix this?
Comment: I'm so sorry, i did not see this comment or get notification, i'll have a look into it. I have to say i did not notice this behavior.

Wordpress Job Manager Pagination Issue


Operation is not valid due to the current state of the object with Full Trust Proxy

Question: I have an event receiver accessing a SharePoint proxy operation via the following code:
<code>var output = SPUtility.ExecuteRegisteredProxyOperation(Common.SQLOperationAssemblyName, Common.SQLOperationTypeName, args);
</code>
Every time it's hit I get the following error:
Operation is not valid due to the current state of the object.; StackTrace:    at Microsoft.SharePoint.Utilities.SPUtility.ExecuteRegisteredProxyOperation(String assemblyName, String typeName, SPProxyOperationArgs args)     at Segal.Extranet.Receivers.UsersReceiver.UsersReceiver.ItemAdding(SPItemEventProperties properties)    c85ec89c-668d-3009-92f8-722ef549ab79
Any help would be appreciated
Answer: I figured this out the next morning. The event receiver which was accessing the proxy operation needed to be installed as a sandboxed solution. It was being installed through powershell was a farm solution.
Hope this helps anyone who comes across this!


Python programming. Syntax Error whilen using min and max

Question: I wanted to write a program which takes 3 numbers as inputs and returns the middle number.
def clip(lo, x, hi):
'''
Takes in three numbers and returns a value based on the value of x.
Returns:
 - lo, when x < lo
 - hi, when x > hi
 - x, otherwise
'''
# Your code here
    r1 = max(lo, x)
    r2 = min((hi, x)

    return min(r1, r2) #This is Line13

r = clip(3, 5, 7)

If I have done something wrong in writing the logic, then that's not a problem. But the problem is it shows me syntax error in line 13. Why does it show syntax error.
Comment: Note that this can be written `return min(hi, max(lo, x))`
Comment: For the future, I'd highly recommend not only learning how to read the error output (it tells you where your error is) but to use a nice IDE that will point out your errors on the fly. For example PyCharm/Sublime Text, or you could even use vim+syntastic.
Answer: The r2 assignment line has an additional (
Answer: From the docs https://docs.python.org/2/tutorial/errors.html

The parser repeats the offending line and displays a little 'arrow'
  pointing at the earliest point in the line where the (syntax) error was
  detected. The error is caused by (or at least detected at) the token
  preceding the arrow.

That's why syntax error is being reported at the return statement since it is where a matching closing parenthesis is expected (in your code).
def clip(lo, x, hi):
    r1 = max(lo, x)
    r2 = min(hi, x)  # min((h1, x) is the problem cause
    return min(r1, r2)

r = clip(6, 5, 7)
print r

Answer: I think the line is the one above the line you are indicating -
r2 = min((hi, x)

You have an unclosed bracket, you dont even need another opening bracket there.
It should be -
r2 = min(hi, x)


Given $B>0$, and $c_1,c_2>0$, does there exist $A\in\mathbb{R}^{2\times 2}$, such that $ABA^*= \operatorname{diag}\{c_1,c_2\}$?

Question: For the given $c_1>0,c_2>0$, and positive definite matrix $B\in\mathbb{R}^{2\times 2}$, does there exist $A\in\mathbb{R}^{2\times 2}$, such that $ABA^*= \operatorname{diag}\{c_1,c_2\}$?
I know that since $B>0$, there exists nonsingular $D$, such that $DBD^*=I$.
Is there way to to push ones in identity to $c_1$ and $c_2$?
Comment: Is $^*$ the transpose? Because if so, the result that you claim to "know" does not seem true at all
Answer: This follows from Sylvester's law of inertia. Alternatively, orthogonally diagonalise $B$ first and solve for $A$. You may also solve the equations $XBX^T=I$ and $I=Y\operatorname{diag}(c_1,c_2)Y^T$ individually for real invertible matrices $X$ and $Y$, and set $A$ to $Y^{-1}X$.
Comment: thanks for help


Calculate $I=\int_0^1\frac{\ln(1+x)}{1+x^2}\,dx$

Question: How can I calculate the integral $I=\int_0^1\frac{\ln(1+x)}{1+x^2}\,dx$ by substituting $t=\frac{1-x}{1+x}$.
Comment: @RonGordon That's why I put here because it's not an ordinary integration by substitution.
Comment: @pourjour You mean that you're actually integrating from $0$ to $\frac{1-x}{1+x}$? (I don't see how that makes sense)
Comment: @anorton sorry I meant 1 not t
Answer: OK, the substitution you suggest works very well.  Note that
$$x = \frac{1-t}{1+t}$$
$$1+x = \frac{2}{1+t}$$
$$1+x^2 = \frac{2(1+t^2)}{(1+t)^2}$$
$$dx = -\frac{2}{(1+t)^2} dt$$
Then
$$\int_0^1 dx \frac{\log{(1+x)}}{1+x^2} = \int_0^1 dt \frac{\log{2} - \log{(1+t)}}{1+t^2}$$
With a little bit of algebra, we see immediately that
$$\int_0^1 dx \frac{\log{(1+x)}}{1+x^2} = \frac{\pi}{8} \log{2}$$
Answer: Another way. Setting $x= \tan(t)$, we get that
\begin{align}
I & = \int_0^{\pi/4} \ln(1+\tan(t))dt = \int_0^{\pi/4} \ln \left(\sqrt2 \cos(\pi/4-t)\right) dt - \int_0^{\pi/4} \ln(\cos(t)) dt\\
& = \int_0^{\pi/4} \ln(\sqrt2) dt + \underbrace{\int_0^{\pi/4}\ln \left(\cos(\pi/4-t)\right) dt}_{\pi/4-t \to t \text{ gives }\int_0^{\pi/4} \ln(\cos(t)) dt} - \int_0^{\pi/4} \ln(\cos(t)) dt\\
& = \dfrac{\pi \ln(2)}8
\end{align}


Como dinamizar uma sequência numérica de 3 em 3 números seguindo a ordem?

Question: Tipo:
1 2 3 ... 4 5 6 ... 7 8 9
E assim por diante.
Exemplo

var clic = 0;
function mais() {
  if (clic == 1) {
    document.getElementById('txt').textContent += "1 2 3 "
  } else if (clic == 2) {
    document.getElementById('txt').textContent += "4 5 6 "
  } else if (clic == 3) {
    document.getElementById('txt').textContent += "7 8 9 "
  } else {
    document.getElementById('btn').style.display = 'none';
  }
}
<input type="button" value="mais" id="btn" onclick="mais(clic++)" />

No exemplo acima coloquei um limite para cada clique, não é necessário limitar podendo ser ilimitado para o que necessito.
Sendo assim, apenas quero sempre jogar de 3 em 3 números pegando sempe a último número e dar continuidade na sequência.
O que quero é automatizar e melhorar a função sem que precise definir manualmente os números e a sua ordem.
Answer: Tem diversas formas, por exemplo, guardando os itens em um Array() e depois imprimindo todos em uma div.
Exemplo:

var items = Array();
var div1 = document.getElementById("div1");
function moreItems(total)
{
  for(i = 0; i < total; i++)
  {
      items.push(items.length + 1);
  }
  viewItems(items);
}

function viewItems(items)
{
  div1.innerHTML = '';
  for(i = 0; i < items.length; i++)
  {
      div1.innerHTML += items[i] + ' ';
  }
}

<button type="button" onclick="moreItems(3)">Acrescentar</button>

Outro exemplo
Observação: o código html vem antes do código javascript para não ter problemas e causar erros.
Answer: Segue outras alternativas:
Temos que definir três variáveis no escopo global que irão funcionar como inicialização, outra como condição, e um contador de cliques.
Exemplo 1

var n = 0;
var i = 0;
var clic = 0;
function mais() {
  clic++
  i += 3 // condição
  if (clic) {
    for (var x = n; x < i; x++) {
      document.getElementById('txt').textContent += x + '\n'
    }
  }
  if (clic) {
    n += 3 // inicialização
  }
}
<input type="button" value="mais" id="btn" onclick="mais(1)" />

Exemplo 2

var clic = 0;
var num = document.getElementById("txt").textContent;

function mais() {
  var i = 0 + clic; // inicialização
  var n = 3 + clic; // condição
  for (var i; i < n; i++) {
    num += i + '\n';
  }
  document.getElementById("txt").textContent = num;
  clic += 3;
}

<input type="button" value="mais" onclick="mais()" />

NOTA - O Script do Exemplo 2 deve ser inserido no fim do documento HTML entre o  ... , para evitar falhas na execução.
A razão de termos colocado o elemento

  <input type="button" id="click" value="Clique"/>

Valor de N:

QTDE de Clicks:


Why would my import of multiple selections work in one content type but not the other?

Question: I've got two content types that both have one off CSV file imports. They both have fields where you can select multiple values. The csv is comma delimited and the multiple selection data, for the field in question, is separated from the rest of the data by quotation marks, within the quotation marks there are no spaces between values; each value is instead comma separated.
For one of the content types this works perfectly, multiple values are put into the field. This field is a node reference.
However for the other content type this doesn't work instead if there are multiple values in the csv no values are given to the content, there are no error messages in any logs. This field is a multi check-box/radio input. I've checked and double checked to make sure the are no spelling discrepancies and I've ensured the field is set up correctly. If you manually create a new node of this content type you can allocate multiple values within this field.
Both fields are set to take unlimited values.
Answer: Eventually finding no solution to the problem I decided to replicate the multiple field selection import that was working.
I made a new content type which contained nothing but the data items I wanted to be able to select and then used a node reference from the original content type. This isn't the most opportune method as whenever this data is displayed it is displayed as a link, a link which then takes you to a page which has nothing on it apart from the name of the data. However imports correctly put the desired data into the the field.
My suspicions regarding this issue are around how the node reference handles multi-fields. Perhaps handling them in a similar way to the import, as in [datafield1,dataf2,"dataf3,dataf3,dataf3",dataf4](as clarification, the previous example is how the csv is laid out). However I've no idea whether or not this actually has any grounds.


Word boundry \b in Java RegEx

Question: I am having difficulty with using \b as a word delimiter in Java Regex.
For
text = "/* sql statement */ INSERT INTO someTable";

Pattern.compile("(?i)\binsert\b");  no match found
Pattern insPtrn = Pattern.compile("\bINSERT\b"); no match found
but
Pattern insPtrn = Pattern.compile("INSERT");  finds a match
Any idea what I am doing wrong?
Answer: When writing regular expressions in Java, you need to be sure to escape all of the backslashes, so the regex \bINSERT\b becomes "\\bINSERT\\b" as a Java string.
If you do not escape the backslash, then the \b in the string literal is interpreted as a backspace character.
Answer: Use this instead: -
Pattern insPtrn = Pattern.compile("\\bINSERT\\b")

You need to escape \b with an extra backslash..

Word boundry \b in Java RegEx