When a NATURAL sign is used is it only for that single note?

Question: In the key of A there is a natural sign at a G# note in the fifth bar. Is it only for this G# or are the following G's natural as well?
Answer: Any accidental — in this case, a natural sign — is only in force until the end of the bar.
Given a key signature that includes G♯, all Gs are sharp unless a natural sign appears. All subsequent Gs on the same line or space and within the same measure are G naturals. After the barline, the key signature takes over again.
Answer: Natural signs, like all accidentals (sharps, flats, naturals) last for a whole bar. So all following G's in the bar will also be G natural and not G#.


FTPS SSH Host Key after IP Address Change

Question: I have a Secure FTP (FTPS) server that my remote sites to upload files to daily via scripted routines that run. I have had issues in the past when upgrading hardware and deploying new servers causing the RSA Fingerprint to change for that server. Then all my remote sites can't connect until I have the old key removed (usually via ssh_keygen -r myserver.com).
I now have to change the IP address for myserver.com and I wondered if there is anyway to proactively generate new host keys so that when the server address changes all my FTPS client  remote sites don't break?
Answer: (I think you mean sftp, not ftps. Ftps has nothing to do with SSH.)
The answer is "no." Think about it from a client's perspective: how can a client trust that they are actually communicating with the correct server rather than a rogue server? If the server was allowed to change IPs (or keys) at will with no complaint from the client then the client would happily connect to a rogue server.
It's inconvenient, but you should coordinate the change in IPs out of band. E.g., a secure email stating "At X time the server's IP will change to Y; you will need to update your client's key store." In reality, that may be too inconvenient so ssh clients offer some configuration options to reduce security. For example, OpenSSH has the option CheckHostIP which, when set to no disables IP checking. Still, it is in the client's hands, not the server's, to decide whether to trust the server or not.


Returning a simpler Iterator interface instead of a Map in Rust

Question: I'd like to write this:
fn fibs() -> std::iter::Iterator<int> {
    return std::iter::iterate((1i, 1i), |(a, b)| { (b, a + b) }).map(|(a, _)| a)
}

But if I do, I get this error:
error: explicit lifetime bound required
fn fibs() -> std::iter::Iterator<int> {
             ^^^^^^^^^^^^^^^^^^^^^^^^

If, instead I write out the full interface, it compiles:
fn fibs() -> std::iter::Map<'static, (int, int), int, std::iter::Iterate<'static, (int, int)>> {
    return std::iter::iterate((1i, 1i), |(a, b)| { (b, a + b) }).map(|(a, _)| a)
}

Is there a way to return the simpler interface?
Comment: Iterator is a trait and not a type. I don't know much about Rust but I think you need to pack it in Box. To return unboxed abstract types we need this: https://github.com/rust-lang/rfcs/pull/105
Answer: There is not at present any way to return an abstract type such as Iterator<T>, just as you can't assign that as the type of a variable (let x: Iterator<uint>; won't compile for the same reasons).
This has been discussed and is certainly desired; when it is done it will probably be in the form fn fibs() -> impl Iterator<uint>, but you can't do that yet.
Comment: It appears that the issue has been closed: https://github.com/rust-lang/rfcs/pull/105. Is the RFC superseded by another one?
Comment: I don't believe it has been for the moment, but it will be at some point.
Comment: Well, that's not an RFC but rather a tracking issue for the concept which is wanted.
Answer: as @ChrisMorgan said, for now you can't return an abstract type. If you are creating a public API and want to avoid depending too much on the specific implementation of iterate and map, you can encapsulate the return type into your own struct (which is more or less what the collections in the std library themselves do).
Something like:
// updated to rustc 0.13.0-nightly (2015-01-02)
use std::iter::{Map, iterate, Unfold};

type Fibs = Map<(int, int), int, Unfold<(int, int),
    (fn((int, int)) -> (int, int), Option<(int, int)>, bool),
    fn(&mut (fn((int, int)) -> (int, int), Option<(int, int)>, bool)) ->
        Option<(int, int)>>, fn((int, int)) -> int>;

struct Fibs2 {
    iter: Fibs,
}

impl Fibs2 {
    fn iter() -> Fibs2 {
        fn inner((a, b): (int, int)) -> (int, int) { (b, a + b) }
        let in_fn = inner as fn((int, int)) -> (int, int);

        fn first((a, _): (int, int)) -> int { a }
        let p_first = first as fn((int, int)) -> int;

        Fibs2{ iter: iterate((1i, 1i), in_fn).map(p_first) }
    }
}

impl Iterator<int> for Fibs2 {
    fn next(&mut self) -> Option<int> {
        self.iter.next()
    }
}

fn main() {
    for fib_n in Fibs2::iter().take(10) {
        println!("{}", fib_n);
    }
}

Comment: Thanks! I think Swift does the same kind of thing but generically with AnySequence https://developer.apple.com/library/tvos/documentation/Swift/Reference/Swift_AnySequence_Structure/index.html


Pre and Post Survey Analysis in ANOVA Excel

Question: I have a small end size of 6 participants. They all took the pre and post survey that had 10 questions with ranges associated with them. I did not ID the participants. I am wondering how to plug this into ANOVA in Excel to get appropriate metrics as I have tried a few ways that did not seem accurate. The goal is to see if the groups numbers for each question were better or worse after their intervention.
Answer: There are two issues with your question:

It is unfortunate that you don't have subject IDs.
Ideally, to see if scores on Q1 increased or decreased,
you would look at six differences, one for each subject.
If subjects are very homogeneous as to ability, this loss might not be crucial. But if some subjects are inherently better than others at answering Q1 (whether
pre or post), then you could lose a lot of information by losing the six 'pairs' of scores.
I have no idea what you mean by "appropriate metrics." You only addressed one goal and that is to see if results for each Question go up or down from pre to post test.

Pending an explanation of whether you really mean you have lost the pairs and an explanation of your actual goals, maybe it is useful to explore several scenarios for just one question. [Simulations and computations in R statistical software.]
(a) Subjects have varying abilities and IDs known, markedly increased scores from pre to post. The large increase (10 points) is detected with a P-value very near 0.
set.seed(416)
ability = runif(6, 100, 200)
pre = rnorm(6, 0, 2) + ability
post= rnorm(6, 0, 2) + ability + 10
d = post - pre
summary(d); sd(d)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  6.683   9.909  10.107  10.200  11.276  12.794
[1] 2.065929  # standard deviation of differences

t.test(d)

    One Sample t-test

data:  d
t = 12.094, df = 5, p-value = 6.827e-05
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
  8.031837 12.367957
sample estimates:
mean of x
  10.1999

(b) Subjects have varying abilities again, markedly increased scores from pre to post, IDs not known. Pairs are destroyed by scrambling the order of subjects on the post test. Pairing no longer reduces the variance of the differences. No significant difference is found; note large P-value.
set.seed(415)
ability = runif(6, 100, 200)
pre = rnorm(6, 0, 2) + ability
post= rnorm(6, 0, 2) + sort(ability) + 10
d = post - pre
summary(d); sd(d)

   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
-29.547 -26.588  -5.104   7.597  31.466
 75.430
[1] 42.57396

t.test(d)$p.val
[1]  0.6719451

To save space, only the P-value of the t test is shown:
t.test(d)$p.val
[1]  0.6802821

(c) Subjects all have about the same ability, markedly increased scores from pre to post, IDs lost. In an (unorthodox) attempt to "recover" pairing, the subjects are sorted from lowest to highest scores for both pre and post tests. The t test shows a highly significant result with a P-value almost 0. The danger is that sorting is
really cheating and could lead to false discovery.
set.seed(415)
ability = runif(6, 100, 112)
pre = sort(rnorm(6, 0, 2) + ability)
post= sort(rnorm(6, 0, 2) + sample(ability) + 10)
d = post - pre
summary(d); sd(d)
set.seed(415)
ability = runif(6, 100, 112)
pre = sort(rnorm(6, 0, 2) + ability)
post= sort(rnorm(6, 0, 2) + sample(ability) + 10)
d = post - pre
summary(d); sd(d)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  5.826   6.844   7.516   7.597   8.688   9.025
[1] 1.269072

t.test(d)$p.val
[1] 2.665522e-05

(d) Subjects all have about the same ability, markedly increased scores from pre to post, IDs known.
The increase in scores is detected with a small P-value.
set.seed(415)
ability = runif(6, 100, 105)
pre = rnorm(6, 0, 2) + ability
post= rnorm(6, 0, 2) + ability + 10
d = post - pre
summary(d); sd(d)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  3.082   6.735   8.560   7.597   8.984  10.128
[1] 2.559262

t.test(d)$p.val
[1] 0.0007693194

(e) Subjects all have about the same ability, markedly increased scores from pre to post, IDs not known.
The increase in scores is detected with a small P-value, in spite of the scrambling subjects to destroy pairs among subjects or about the same abilities.
set.seed(415)
ability = runif(6, 100, 105)
pre = rnorm(6, 0, 2) + ability
post= rnorm(6, 0, 2) + sample(ability) + 10
d = post - pre
summary(d); sd(d)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  2.479   5.241   8.339   7.597   9.127  12.827
[1] 3.688512

 t.test(d)$p.val
[1] 0.003950126

Conclusions:  You might start by comparing pre and post tests, even though you have lost the pairing.
If you see a significant difference between pre and post, it is probably real (and possibly understated).
In your report of the experiment, you need to admit
that you lost IDs, so the amount of change between pre and post may not be correct.
If you don't see a significant difference between pre
and post, see what happens if to try to restores by
sorting. If that gives a significant result, you may
take that as motivation to do the experiment over and
keep track of IDs this time. However, you should be
prepared for others to be skeptical whether the effect
is real.
Of course, this answer is a thought experiment based on simulated data. Your real data may or may not behave as
my simulated data did.
Comment: Thank you for this- I deliberately did not ID 6 participants only due to be confidential with their scores, but I do know who had answered which survey; I could assign them a number. I meant appropriate as, I don't know which ANOVA data that I paired is accurate. I don't know how best to put them into Excel to compare the pre and post surveys to see if there was a better result. Most answers did not change so I know just by that, it didn't. But need an ANOVA for my paper. Thank you for your help!
Comment: You could protect confidentiality by assigning code numbers to participants or asking them to generate short memorable passwords.


Python list comprehension with a function as the output and the conditional

Question: Given some function that can return None or another value and a list of values:
def fn(x):
    if x == 4:
        return None
    else return x

lst = [1, 2, 3, 4, 5, 6, 7]

I want to have a list of the outputs of fn() that don't return None
I have some code that works which looks like so:
output = []
for i in lst:
    result = fn(i)
    if result:
        output.append(result)

I can express this as a list comprehension like so:
output = [fn(i) for i in lst if fn(i)]

but it runs fn(i) for anything that doesn't return None twice which is not desirable if fn is an expensive function.
Is there any way to have have the nice pythonic comprehension without running the function twice?
a possible solution:
output = [fn(i) for i in lst]
output = [o for o in f if o]

Comment: You know what's Pythonic? Your for-loop.
Answer: Your problem is that None is produced by the function, not the thing you are iterating over. Try
 output = [fi for fi in map(fn, lst) if fi is not None]

Comment: This is essentially the answer I was looking for, a way to iterate over the results of the function. Thanks!
Answer: Just combine your solutions:
[x for x in [fn(i) for i in lst] if x is not None]

The downside is that any Nones in the original list not produced by fn will be removed as well.
Comment: The comparison with None should be changed to `x is not None`.


Como usar PHP no VSCode?

Question: Boa tarde!
Eu utilizava a IDE PHP Storm para programar em PHP, e ela permite a conexão com um servidor remoto e execução do código lá no servidor.
Agora, migrei para o VS Code e gostaria de saber como posso programar em PHP nessa IDE. Onde insiro os dados do servidor remoto? Como faço para executar um script? Obrigado!
Ps: Eu já instalei algumas extensões como IntelliSense do PHP e PHP Debug, mas ainda assim não sei o que fazer.
Ps2: Utilizo servidor do LocaWeb, essa programação em PHP seria para uma página web.
Comment: Tenha em mente que nem todos os programas possuem as mesmas ferramentas. Phpstorm é uma IDE, enquanto o VS Code é editor de texto com alguns plus. Certifique-se primeiro que o VS Code é capaz de fazer o que deseja. Aliás, por que mudou de o PHP Storm aparentemente satisfazia suas necessidades?
Comment: Sim, já pesquisei e é possível utilizar o PHP em VS Code (tanto que existem inúmeras extensões para facilitar esse processo), mas não encontrei nada sobre como, de fato, codificar e subir o código no servidor (afinal, por isso estou aqui fazendo a pergunta). E deixei de utilizar o PHP Storm pela maior praticidade do VS Code (pesquise um pouco a respeito dos prós dessa ferramenta), além de que o VS Code é totalmente gratuito, diferente do PHP Storm.
Comment: Não sei exatamente como funcionaria para o seu caso, mas por exemplo com o Git, quando vc faz o Commit ele envia o arquivo alterado direto para o repositório, ai basta vc dar um refresh na página que ela já pega o que foi mudado etc. Então o VS Code comunica sim com diretórios remotos, vc tem é que ver como vc vai enviar o arquivo pro seu server, uma forma como falei é configurando o Git lá, ou talvez com Docker, mas ai não tenho conhecimento pra opinar muito.
Comment: penso que quem marcou essa questão como ruim não se deu ao trabalho de entender que isso é válido a partir do momento que alguém deseje usar. @LucasVicente, gostaria de uma ajuda sua, fiz as instalações dos plugins, mas por algum motivo continuo recebendo alguns erros. Teria como entrar em contato diretamente comigo?
Answer: VSCode não é um IDE como o PHPStorm, então você não tem nativamente como colocar os dados do servidor e outras facilidades que o PHPStorm dava nativamente para você.
O que eu faço é ter meu VSCode com as extensões mais recomendadas do PHP instaladas, para isso é só você abrir o editor e ir na barra lateral na parte de extensões, na busca digite: @recommended php.
Depois, você pode utilizar o terminal integrado do VSCode (através da tecla de atalho ou então através da aba de comandos ctrl + shift + p digitando "Open new terminal". A partir daí você pode desenvolver normalmente, é possível utilizar o servidor nativo do PHP com php -S no próprio terminal, afinal o PHP é só um arquivo de texto que é interpretado no browser.
Existem outras extensões como as abaixo para poder facilitar um pouco:

https://marketplace.visualstudio.com/items?itemName=brapifra.phpserver
https://marketplace.visualstudio.com/items?itemName=PrimaFuture.open-php-html-js-in-browser
https://marketplace.visualstudio.com/items?itemName=MehediDracula.php-namespace-resolver
https://marketplace.visualstudio.com/items?itemName=bmewburn.vscode-intelephense-client
https://marketplace.visualstudio.com/items?itemName=neilbrayfield.php-docblocker
https://marketplace.visualstudio.com/items?itemName=FrogTheFrog.php-project

Se você usa git o VSCode já tem a extensão nativa do git, se não você pode buscar outras extensões com FTP para poder acessar o servidor.
Comment: Obrigado meu jovem!

Como usar PHP no VSCode?


Como verificar se cliente desconectou abruptamente

Question: Tenho esse código de um chat online utilizando socket e TCPListener, porém quando o cliente simplesmente desconecta abruptamente ou perde conexão, ele não é removido do ArrayList chamado clients.
public void ServiceClient()
    {
        Socket client = clientsocket;
        bool keepalive = true;

        while (keepalive)
        {
            Byte[] buffer = new Byte[1024];
            client.Receive(buffer);
            string clientcommand = System.Text.Encoding.UTF8.GetString(buffer);

            string[] tokens = clientcommand.Split(new Char[] { '|' });
            Console.WriteLine(clientcommand);

            if (tokens[0] == "CONN")
            {
                for (int n = 0; n < clients.Count; n++)
                {
                    Client cl = (Client)clients[n];
                    SendToClient(cl, "JOIN|" + tokens[1]);
                }
                EndPoint ep = client.RemoteEndPoint;
                //string add = ep.ToString();
                Client c = new Client(tokens[1], ep, clientservice, client);
                clients.Add(c);
                string message = "LIST|" + GetChatterList() + "\r\n";
                SendToClient(c, message);
                this.Invoke(new Action(() => lbClients.Items.Add(c.Name)));

                //txtLogs.AppendText("-> SISTEMA: ", Color.Red);
                //txtLogs.AppendText(DateTime.Now.ToString("HH:mm:ss") + "h - " + "Entrou no chat: " + c.Name, Color.White);
                //txtLogs.AppendText("\r\n");
            }
            else if (tokens[0] == "CHAT")
            {
                for (int n = 0; n < clients.Count; n++)
                {
                    Client cl = (Client)clients[n];
                    this.Invoke(new Action(() => SendToClient(cl, clientcommand)));
                    str = clientcommand;
                    var charsToRemove = new string[] { "CHAT|" };
                    foreach (var c in charsToRemove)
                    {
                        str = str.Replace(c, string.Empty);
                    }
                }
                //txtLogs.AppendText("-> CHAT: ", Color.DodgerBlue);
                //txtLogs.AppendText(str, Color.White);
            }
            if (tokens[0] == "PRIV")
            {
                string destclient = tokens[3];
                for (int n = 0; n < clients.Count; n++)
                {
                    Client cl = (Client)clients[n];

                    if (cl.Name.CompareTo(tokens[3]) == 0)
                    {
                        SendToClient(cl, clientcommand);
                        var str = clientcommand;
                        var charsToRemove = new string[] { "PRIV|", "|", tokens[3] };
                        foreach (var c in charsToRemove)
                        {
                            str = str.Replace(c, string.Empty);
                        }
                        txtLogs.AppendText("-> PRIVADO: ", Color.Gold);
                        txtLogs.AppendText("Para: ", Color.LimeGreen);
                        txtLogs.AppendText(tokens[3], Color.White);
                        txtLogs.AppendText(" De: ", Color.LimeGreen);
                        txtLogs.AppendText(str, Color.White);
                        txtLogs.AppendText("\r\n");
                    }
                    if (cl.Name.CompareTo(tokens[1]) == 0)
                    {
                        SendToClient(cl, clientcommand);
                        var str = clientcommand;
                        var charsToRemove = new string[] { "PRIV|", "|", tokens[1] };
                        foreach (var c in charsToRemove)
                        {
                            str = str.Replace(c, string.Empty);
                        }
                        txtLogs.AppendText("-> PRIVADO: ", Color.Gold);
                        txtLogs.AppendText("Para: ", Color.LimeGreen);
                        txtLogs.AppendText(tokens[1], Color.White);
                        txtLogs.AppendText(" De: ", Color.LimeGreen);
                        txtLogs.AppendText(str, Color.White);
                        txtLogs.AppendText("\r\n");
                    }
                }
            }
            else if (tokens[0] == "GONE")
            {
                int remove = 0;
                bool found = false;
                int c = clients.Count;
                for (int n = 0; n < c; n++)
                {
                    Client cl = (Client)clients[n];
                    SendToClient(cl, clientcommand);
                    if (cl.Name.CompareTo(tokens[1]) == 0)
                    {
                        remove = n;
                        found = true;
                        this.Invoke(new Action(() => lbClients.Items.Remove(cl.Name)));

                        txtLogs.AppendText("-> SISTEMA: ", Color.Red);
                        txtLogs.AppendText(DateTime.Now.ToString("HH:mm:ss") + "h - " + "Saiu no chat: " + cl.Name, Color.White);
                        txtLogs.AppendText("\r\n");
                    }
                }
                if (found)
                    clients.RemoveAt(remove);
                client.Close();
                keepalive = false;
            }
            /*else
            {
                int remove = 0;
                bool found = false;
                int c = clients.Count;
                for (int n = 0; n < c; n++)
                {
                    Client cl = (Client)clients[n];
                    SendToClient(cl, clientcommand);
                    if (cl.Name.CompareTo(tokens[1]) == 0)
                    {
                        remove = n;
                        found = true;
                        lbClients.Items.Remove(cl);
                        //lbClients.Items.Remove(cl.Name + " : " + cl.Host.ToString());
                    }
                }
                if (found)
                    this.Invoke(new Action(() => clients.RemoveAt(remove)));
                client.Close();
                keepalive = false;
            }*/
        }
    }

Com isso, como saber se o cliente não está mais operante e como removê-lo do ArrayList e do ListBox?
Answer: Uma maneira de fazer esse controle é o client ficar enviando mensagens para o server por um tempo pré-determinado.
Por exemplo, mesmo que o client não tenha nenhuma mensagem para enviar ao server, a cada 5 segundos ele deve enviar uma mensagem "estou aqui".
O server deve controlar os tempos de recepção para cada client. Se o server não receber mensagem de um client por 7 segundos (a cada 5 segundos o client deve mandar uma mensagem, e mais 2 segundos de tolerância) então o server retira o client de sua lista de clients.
Não trabalho com C# mas segundo esta página do MSDN quando você configura ReceiveTimeout no socket você precisa tratar exceções.
Nesse link do MSDN que coloquei tem um exemplo de tratamento de exceções, estou colocando aqui:
// Displays sending with a connected socket
// using the overload that takes a buffer and socket flags.
public static int SendReceiveTest2(Socket server)
{
    byte[] msg = Encoding.UTF8.GetBytes("This is a test");
    byte[] bytes = new byte[256];
    try  // <------------------- VEJA ISTO
    {
        // Blocks until send returns.
        int byteCount = server.Send(msg, SocketFlags.None);
        Console.WriteLine("Sent {0} bytes.", byteCount);

        // Get reply from the server.
        byteCount = server.Receive(bytes, SocketFlags.None);
        if (byteCount > 0)
            Console.WriteLine(Encoding.UTF8.GetString(bytes));
    }
    catch (SocketException e) // <------------- VEJA ISTO
    {
        Console.WriteLine("{0} Error code: {1}.", e.Message, e.ErrorCode);
        return (e.ErrorCode);
    }
    return 0;
}

Comment: Faço isso com um `ReceiveTimeout` no `Socket`?
Comment: No momento, adicionando o `ReceiveTimeout` quando há desconexão abrupta, o servidor retorna isso: "Foi forçado o cancelamento de uma conexão existente pelo host remoto"
Comment: **acho** que esta mensagem que você mostrou não se refere ao timeout expirado, mas sim ao fechamento do socket pelo client...para testar o controle de timeout no server você deve deixar o client rodando, mas não mandar nada para o server


MATLAB: Create a colored surface based on scatter points

Question: Using a dataset of the format [x,y,z,value], I can create a 3D scatter plot as shown in the images, where the color of each point at location (x,y,z) is based on value. Note that all of the images are of the same plot, just from different views. It is intended to be the surface of an octant of a sphere.

Is there a way I can do an interpolation of the colors in 3D such that we see a solid surface instead of individual points? I am looking for something along the lines of imagesc, but in 3D. I've tried a wide variety of functions, including scatteredInterpolant, patch, mesh, and surf, but those do not seem to work in 3 dimensions the way I'd like.
Comment: Why does `surf(x,y,z,c)` not work?
Comment: I get the error "Z must be a matrix, not a scalar or vector." I have no idea how I'd convert a position vector into a matrix
Comment: Can you post some example data?
Comment: I uplodaed the [full dataset](http://www.philipchow.info/uploads/dataset.txt). The first three columns are `[x,y,z]`; the remaining columns are different values. I'm reading in each column one at a time so that the `[x,y,z,value]` format still applies. You should use csvread to import data.
Answer: Based on your example data (with multiple shells), I put this program together. It uses trisurf so it doesn't need meshed data.
function PlotColoredSpheres(x,y,z)
    tol = 0.01;

    r = sqrt(x.^2 + y.^2 + z.^2);
    data = num2cell(sortrows([r, x, y, z], [1,2,3,4]),1);
    [R, X, Y, Z] = data{:};

    figure(1)

    RUnique = uniquetol(r, tol);

    for Ru = RUnique(:)'
        j = abs(R - Ru) < tol;
        tri = delaunay(X(j),Y(j));
        trisurf(tri, X(j), Y(j), Z(j));
        lighting phong
        shading interp
        hold on
    end
    hold off
end


How can I generate unique random CHAR(6) with values "AAAAAA" to "ZZZZZZ"?

Question: I'm not a sophisticated developer, as some of you already know, but maybe you could help me out with a rough design for this?
I would like to mimic the way airline reservation systems randomly generate unique record locators for their primary keys. None of the generated values can form obscene words, other specified values, or be a duplicate in an existing row, so a separate lookup table containing the prohibited values is required?
EDIT: Would it be better to populate a table with valid keys and randomly select a row which has not been updated with a key_already_used flag in order to generate the value, or should the value be randomly generated by a user-defined function, looked up to see if its not in the prohibited values table, then looked up to see if it has not already been inserted in the reservations table?
Comment: Letters offer more possible combinations (6!26 - prohibited values) versus (6!10) and letters are easier to remember than numbers?
Comment: The implementation of this sounds similar to what many URL shorteners do.  Check this question out for some possible ideas that may help you http://stackoverflow.com/questions/742013/how-to-code-a-url-shortener
Comment: "Would it be better to populate a table with valid keys" - 26^6 is almost 309 million keys so not a good solution. Just generate the key and then check to see if it's already used.
Comment: What's wrong with a numeric key?
Comment: @Marshall: url-shortener implies creating a function which converts Base 26 numbers to letters. Would this be a good approach for my goal?
Comment: Why "random" and not just incremental from AAAAAA to ZZZZZZ? after all you  need uniqueness ¿don't you?
Comment: It needs to be random, incremental is too predictable. Customers could figure it out and try to use someone elses key. Maybe I should even add an aditional CHAR as a modulo (check-char)?
Comment: This is sort of a similar program I wrote in ole QBASIC to generate unique incremental numbers http://stackoverflow.com/questions/9935974/in-sql-how-can-i-generate-every-possible-unique-combination-of-556
Comment: It's peculiar that in Informix 11.50 documentation I didn't see a random function, yet I spotted a ROUND function which can round DATE's and DATETIME's!
Answer: Here is a good example.
To convert from int to char sequence:
create function CustomerNumber (@id int)
returns char(5)
as
begin
  return char(@id / power(26,3) % 26 + 65) +
     char(@id / power(26,2) % 26 + 65) +
     char(@id / 26 % 26 + 65) +
     char(@id % 26 + 65)
end

And then all that you have to do is pass an unique integer and you will get an unique string (or a random integer, and you will get a random string)
(just remember that random is not the same as unique)
I guess in informix it should look like this?
CREATE FUNCTION CustomerNumber (id int)
RETURNING char(5);
 RETURN integer::char(id / POW(26,3) % 26 + 65) +
         integer::char(id / POW(26,2) % 26 + 65) +
         integer::char(id / 26 % 26 + 65) +
         integer::char(id % 26 + 65)
END FUNCTION

Comment: No.. this is SQLServer... but you question has no MySQL tag... oooh I see you have an informix tag... sorry, no informix skills here... but I'll see if I can find something...
Comment: hopefully it is pretty much correct...
Comment: The POW function raises its first numeric argument, the base, to the power of its second numeric argument, the exponent: http://publib.boulder.ibm.com/infocenter/idshelp/v115/index.jsp?topic=%2Fcom.ibm.sqls.doc%2Fids_sqs_1477.htm
Comment: (off this topic) answering your SQL flaws question
Comment: Is this example how you would do it in MySQL?
Comment: (Off this topic) I see that you feel SQL is full of flaws, perhaps later on you'd care to add your two cents in my question: http://stackoverflow.com/questions/3076855/what-deficiencies-do-you-feel-exist-with-sql-and-what-changes-would-you-make-to
Comment: (On topic) In your sample func, what does the POW mean?.. Oh, didn't see your edited equivalent informix func.. power(), hmm i'll try it.

How can I generate unique random CHAR(6) with values “AAAAAA” to “ZZZZZZ”?


Cost sensitive evaluation metrics: precision, recall and F-measure

Question: How can I compute precision, recall and F-measure when we define a cost matrix for evaluation?
In my problem I have 3 different classes, and some errors are more dangerous than others. I would like my evaluation metrics to take into account this information.
Comment: Given that you mention having 3 classes,  I assume you're interested in evaluating a classification model. Is that correct?
Answer:
In my problem I have 3 different classes, and some errors are more
  dangerous than others. I would like my evaluation metrics to take into
  account this information.

Misclassification cost should cover this sufficiently in my opinion.
Assuming a classification problem with 3 classes
$$Y=\{y_1,y_2,y_3\}$$
and a cost matrix
$$C=\begin{bmatrix}0 & c_{12} & c_{13}\\ c_{21} & 0 & c_{23}\\c_{31}&c_{32}&0 \end{bmatrix}$$
with the diagonal indicating no costs for correct classification and cost $c_{12}$ entailed by predicting an instance of $y_1$ to be $y_2$ instead.
Not that the cost matrix need not by symmetric and, based on your problem description, shouldn't be.
From your model, generate the confusion matrix (I will denote it as $\hat{Y}$ for lack of other notation) for your validation set and multiply it with $C$ to get the misclassification cost of your model
$$
MCC= \hat{Y} \times C
$$
When evaluating different models, lower misclassification cost is of course preferred
Answer: Note that, the term precision and recall is defined in binary classification case. (Same as other terms, such as false positive, false negative, etc.)
If you have 3 classes and want to use the precision and recall metrics, you need to define what is "positive" or "negative". One vs. all others is one way of defining it. For example, I want to classify three animals, cat dog and rabbit, I can define detecting cat is "positive". After the definition, these metrics (precision recall, false positive rate etc.) can be used.
A related discussion can be found in my answer here
Calculate accuracy using true/false positives/negatives


What type of analysis to choose for this data?

Question: I am trying to create a model of refrigeration having the energy consumption and the temperature over time. So far, I've tried regression but fitting this data into linear model seems impossible. Another thing that I've tried is cross correlation but it's insignificant (around 0.11 at lag 0). I also clustered the data and for another fridge I was able to state that if the fridge is in 'idle mode' (e.g. not consuming electricity) the temperature goes above certain value. However, for this fridge, this doesn't work as the data seems pretty random. Here is a scatter plot of the data, the bigger the circle, the higher the frequency.

Any ideas what type of analysis can I use to derive insights from this? I would like to know if there is any correlation between the kW data and the temperature data. A new plot for the full duration that I have:
Comment: What is the goal of the analysis? Which questions do you want to ask the data?
Comment: Can you please explain, what do you mean by "frequency" here?
Comment: Do you have the full time series?
Comment: Ah, sorry, I didn't realize that it's unclear. I mean how often those values appear in the data set. So the small dots are values which appeared only once while the big ones are values which appeared multiple times.
I have time series for 6 months, collected every 15 minutes so it's 4 measurements per hour. There are some missing values so I divided the data into chunks because of this.
Comment: If there is any correlation between the kW data and the temperature data.
Comment: This seems to need a proper time-series analysis, which can be a bit more involved than standard linear regression.
Comment: "If there is any correlation between the kW data and the temperature data." Please add this to the question.
Comment: What is "temperature data" here?
Comment: The y axis represents the temperature of a product in Celsius. It's a sensor inside the fridge which simulates a product (glycerin bottle) as the air temperature changes faster and product temperature is more stable and interesting. I also have the air temperature but it is basically the same 4 clusters with a slight shift of the degrees.
Comment: Well, do look at *duration* as suggested below. You want to look at **Watt times duration** (i.e. you want to measure **kWh** not kW - or **Joule**). Clearly a fridge that uses 0.6 kW, but only for 1 millisecond did not really "cool" anything. Joule is the appropriate unit for measuring "effort".
Answer: You clearly have a bimodal (multimodal for the second) distribution:

standby (on the left)
cooling (on the right)

It does not seem that the temperature settings affects the power intake.
Most likely, the fridge operates "binary", i.e. if the temperature is too high it starts cooling, and when it is cool enough it stops cooling again. Typical operation, because it's better to operate the cooling engine at a well-defined power intake and speed; and use duration to control.
Look for a correlation between cooling duration and temperature setting instead. The unit you want to observe is Joules, not kW (note that Joules relate to kWh!). I'd expect a correlation with Joules (= effort), not with power. Consider the power consumption of an appliance to be a sequence of constanta (off/on, or off/low/high); and expect them to use the time to steer the total effort.
Consider your stove. It probably has 4-6 different power settings only. In order to boil water (or anything), you would turn it on (probably to the highest setting; limited by e.g. safety considerations) and then wait for the water to boil. If you want to measure how much energy your stove used, you need to use "watt * time" (yielding kWh or Joules), one value is useless without the other.
Comment: +1 The idea of looking at duration is insightful and appealing.
Comment: Thank you. That seems as a very good suggestion for a start. However, if I expand the data into bigger period, I have this: http://rpubs.com/canar40/102389 which means 4 types of operation. I'm sorry, I should have uploaded this graph instead. I will try to look for patterns for 4 of them now.
Comment: Still, clearly *no correlation* with the temperature setting in that plot either.
Comment: Thank you for that! From my column with kWh gathered every 15 minutes, I took the difference (as it is simply metering data which adds up in time) and now I have proper consumption. However, I still can't find any correlation, even with the difference of my temperature data, ccf is 0.09 at lag 1. I am trying to exclude the defrost cycles now to see if something will change as the fridge does not use energy during that time but the temperature raises. I will be glad if you have any other suggestions!
Comment: If you look at a fixed time (e.g. 15 minutes) then you are again not taking duration into account. You need to aggregate *durations*, not split them into 15 minute chunks.
Comment: I am not sure I am able to aggregate duration as I have the temperature available each 15 minutes as well. That is actually why I took the temperature difference. In this case, I have the difference between time(i-1) and time(i), let's say 2 degrees and that the fridge consumed 0.03 kWh to decrease the temperature to this level. Am I missing something? By aggregate duration you mean on a longer basis, e.g. hourly, daily?
Comment: Aggregate *multiples* of 15 minutes, depending on how long it has been cooling. Assuming that cooling cycles can take longer than 15 minutes. But it may well be that this data is too coarse, and should have been measured differently the first place.
Comment: I guess you are right. Measuring this data on 15 minutes intervals is not enough. Here is a graph of my data http://rpubs.com/canar40/fridge (the red line is the kWh and the black is diff(air). It's clear that I observe the Cut In/Out process (https://www.refrigerationbasics.com/RBIII/controls1.htm) each 15 minutes.


Pandas + SQLAlchemy Oracle Float Issue

Question: I'm not sure if this is specifically a changed behaviour of SQLAlchemy 2.0 in combination with Pracle as it's not easy to reproduce. (seems to depend on the amount of decimal places)
I have a Pandas dataframe with just two columns and one line of data in the form of:
   seq_id     price
2     318 -9.860008

With the Dtypes:
seq_id      int64
price     float64

When using to_sql I get the  following Error (shortened):
sqlalchemy.exc.ArgumentError: Oracle FLOAT types use 'binary precision', which does not convert cleanly from decimal 'precision'.

After some trial and error the solution seems to be to pass the dtype specifically as a SQLAlchemy Type when inserting: (see also: https://docs.sqlalchemy.org/en/20/dialects/oracle.html#sqlalchemy.dialects.oracle.FLOAT)
dataframe.to_sql(name=temp_table, con=connection, if_exists='replace', index=False, dtype={'price': sa.FLOAT})

Is this the correct way to handle this? (Never needed any of this with other DB Backends)
Any risk of losing Precision?
Shouldn't SQLAlchemy be able to infer the correct type (as it seems to be the case with other float values)
Answer: This issue is discussed in SQLAlchemy forum. They have raised a bug for pandas.

Pandas + SQLAlchemy Oracle Float Issue


what is wrong with this shell script

Question: market_l="${echo $1 | awk '{print tolower($0)}'}"
echo $market_l

when i execute this its giving me an error below:
./test: market_l="${echo $1 | awk '{print tolower($0)}'}": The specified substitution is not valid for this command.

Answer: Did you mean to use the $() operator instead of ${}?
Comment: i want to convert the $1 argument supplied while executing the script to lowercase and store it in a variable and then echoing it.
Answer: you should use $() to assign output to a variable. not ${}
market_l="$(echo $1 | awk '{print tolower($0)}')"

or you can do it with ksh
#!/bin/ksh
typeset -l market_l
market_l="$1"
echo $market_l

Other ways to change case besides awk, fyi
$ echo "$1"|tr [A-Z] [a-z]

$ echo "$1"|sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/'

Comment: don't forget to use quotes: `"$1"`.  Always quote unless you specifically don't want to.
Comment: actually in the example, its alright not to use quotes since the output of echo is the same. but yes, you are right to always use quotes where necessary.
Answer: Could be, your system uses ksh88 by default. Run you script putting the next command:
ksh93 ./test

I'm sure, this answer will not help you because has passed 10 years, but it will be useful for someone who falls into the same issue.

what is wrong with this shell script


Can I block android phones from my company wireless network?

Question: Network is password protected, all employees have access to password.  Company policy is only company approved and provided devices are allowed on network.   We can not tell who is logging in on unapproved devices, but it is > 100 people.
Thanks.
Comment: What wireless hardware do you use?
Answer: This depends entirely on what wireless hardware you use. If you have something like Cisco ISE tied into Cisco Wireless LAN Controllers or WiSMs then sure. Aruba and Meraki (recently purchased by Cisco) also have modules for this kind of thing.
If you just have a few random SOHO APs floating around, then no. You can't do something like this. You need an enterprise wireless solution for this sort of thing.


fixed effects vs random effects vs random intercept model

Question: This might sound a repetitive question but after reading many articles and posts online, I could never understand it entirely. I read somewhere that a random intercept model is a type of random effect model. But I thought that in a fixed effect model we were also assuming random intercepts, one per each unit of interest. Also because we are looking "within" the unit, as in, at each unit. Can you please clarify?
Would you be able to clarify what is a mixed model as well please?
Comment: Could you 1) include 1-2 references to the random intercept model, you are mentioning, 2) include 2-3 formula's for these types of models? This will help the members of this forum. Thx.
Answer:
I read somewhere that a random intercept model is a type of random effect model.

Yes, this is correct. Random intercepts are random effects.

But I thought that in a fixed effect model we were also assuming random intercepts, one per each unit of interest.

No, in a fixed effects model, the fixed effects are, fixed. They don't vary by subject or any other unit of interest. The model will estimate one parameter for each fixed effect.

Also because we are looking "within" the unit, as in, at each unit. Can you please clarify?

I'm not sure what you mean here. With mixed models, the fixed effects estimates are "within". If you want to account for "between" effects then you can fit contextual effects by using group means, and offsets from group means.

Would you be able to clarify what is a mixed model as well please?

A mixed model is a model that has fixed effects, and random effects. For example, suppose we have repeated measures within subjects, and we have 6 subjects. We might fit the mixed effects model:
 y ~ X + (1| subject)

and this will fit a model with a fixed effect for X and a random effect (random intercept in this case) for subject.
We could also fit a fixed effects model:
y ~ X + subject

and this will fit a model with a fixed effect for X and fixed effects for each leel of subject.
Both models should provide a good estimate for X but as the number of subjects increases there will be better statistical power, and interpretabilty, with the mixed model.
Comment: @Daniela Rodrigues Does this answer your question ? If so, please consider marking it as the accepted answer. If not, please let us know why.
Comment: Sorry for the delay - By looking at your model y ~ X + subject, does it mean a fixed effect is a independent variable? Would you be able to provide a graphical representation of fixed effect, random effect and mixed models? I am still confused.
Comment: Yes fixed effects are independent variables. I'm not sure what you mean by a graphical representation. You can think of a simple X-Y plot. The fixed effect for X is the slope. In a model with random intercepts for subjects, each subject has their own intercept and all the intercepts are assumed to follow a normal distribution. If subjects are fixed effects instead then each subject has its own offset from the intercept.
Comment: But having their own intercept or an offset from the intercept isn't the same thing? In my mind an offset from the intercept means that for a different subject the regression line will start at a different point (intercept).
Comment: Yes it's basically the same thing. They both have their own offsets, but with fixed effects each subject one consumes one degree of freedom, wherea with random intercepts only a variance is estimated (because they are assumed to be normally distributed), so that's why it makes sense to have fixed effects for small numbers of subjects and random intercepts for lrage numbers. Yes, you are correct that you can think of it as each regression line starts at a different point on the y axis (but all having the same slope)
Comment: This makes sense, thanks a lot! And would it make sense to have fixed effects at the intercept and random effects at the slope?

In your example above, y ~ X + (1| subject), you explain the difference between fixed and random effects at the subject level, does it make sense to talk about fixed and random effects at the X level as well?

Does assuming random effects at a specific level have anything to do with a bayesian/probabilistic interpretation of the variable?
Comment: You;re welcome. Yes, you can have random effects for X as well, in which case it would be a random slope and the model would look like `y ~ X + (X | subject)` which says you have random intercepts for subject, a fixed global effect (overall slope) for X, and random slopes for X which means that each subject has it's own slope (as an offset from the overall slope). There is some analogy with the Bayesian approach. You could ask a new question about that :)
Comment: Thanks very much!


Local json migration: how to set the value of the urls variable?

Question: I'm currently really busy to get the migration work on my desk.
I want to import (migrate) a local json file into drupal8 using Migrate_Plus, Migrate tools.
For that I have the following migration yml configuration file

# Migration configuration for products.
id: product
label: 'Product'
migration_group: Products
# Here we define the migration source
source:
  plugin: url
  data_fetcher_plugin: file
  data_parser_plugin: json
  urls: /modules/custom/migrate_products/data/data.json
  item_selector: /data
  fields:
    -
      name: uuid
      label: 'uuid'
      selector: uuid/value

    -
     name: name
     label: 'Produkt Name'
     selector: name/value

    -
      name: supplierName
      label: 'Anbietername'
      selector: supplierName/value

  ids:
    uuid:
     type: string

destination:
    plugin: entity:node

process:
  type:
    plugin: default_value
    default_value: produkt

  title: name
  field_uuid: uuid
  field_suppliername: supplierName

  sticky:
    plugin: default_value
    default_value: 0
  uid:
    plugin: default_value
    default_value: 0

migration_dependencies:
  required: {}
  optional: {}

dependencies:
  enforced:
    module:
      - migrate_products

Like you can see, the json file is located inside the custom module directory.
The json file looks like this:

{
  "data": [
    {
      "uuid": {
        "value": "7d51-4406-a7ef-3a7916b977a9",
        "patient": false
      },
      "name": {
        "value": "Product1",
        "patient": false
      },
      "supplierName": {
        "value": "User1",
        "patient": false
      },
.....

After enbaling the required migration module, when running:
drush ms
I get the following output:

file parser plugin: could not retrieve data from /modules/custom/migrate_products/data/data.json
 Group: Products (Products)  Status  Total  Imported  Unprocessed  Last imported
 product                     Idle    n. v.  0         n. v.

Answer: The data fetcher plugin can't find your JSON file because the format is wrong. The plugin is expecting Drupal's URI format (e.g. public://path/to/data.json or private://path/to/data.json) under its private/public file dir. For example, see the included example migration with the module:
  # The data_parser normally limits the fields passed on to the source plugin
  # to fields configured to be used as part of the migration. To support more
  # dynamic migrations, the JSON data parser supports including the original
  # data for the current row. Simply include the 'include_raw_data' flag set
  # to `true` to enable this. This option is disabled by default to minimize
  # memory footprint for migrations that do not need this capability.
  # include_raw_data: true
  # Flags whether to track changes to incoming data. If TRUE, we will maintain
  # hashed source rows to determine whether incoming data has changed.
  # track_changes: true
  # Copy the example JSON file in artifacts folder to sites/default/files folder.
  urls:
    - 'public://migrate_json_example/products.json'

Comment: When I try this, I get an error: `Migration failed with source plugin exception: Error message: cURL error 1: Protocol "public" not supported or disabled in libcurl (see http://curl.haxx.se/libcurl/c/libcurl-errors.html) at public://articles/images/images.json.` - any idea what could be the cause?
Comment: It looks like `public://`URI isn't getting translated to the HTTP protocol before it is passed off to cURL for retrieval.
Comment: I am on Docker and what I wanted to achieve ultimately was to retrieve the JSON from a local route which wasn't working either. In the end I found out I had to declare the path as `nginx_back/api/article/images.json` where `nginx_back` is the name of my NGINX container. That works with the `data_fetcher_plugin` set to `http`. Worth noting...


How to get from this inequality to the next?

Question: So I need to prove something in the context of the binomial distribution. This particular bit is algebra-heavy, so I thought I'd post the question here. Basically, I can't seem to figure out what sort of manipulation took place that allowed the inequality in the first line to result in the inequality in the second line:

UPDATE: the specific context is to prove that
$m\le (n+1)p$
Given that $P(X=m) \ge P(X=m-1)$ and that $X$~$B(n,p)$.
The inequalities above are part of this solution:
Comment: On the face of it, this looks like a mistake. Maybe to be sure about it, it would be helpful to have the (mathematical) context in which the inequality arises.
Answer: This generally does not hold. (The reason you can't figure it out is because it's wrong!) I was playing around with it on desmos https://www.desmos.com/calculator/bwrji9t6ud and you'll see that it doesn't hold for $p$ too small. I can prove that the second inequality follows if we know that $p > 0.5$.
Firstly, by definition, you have \begin{align*}&{n\choose m} =\frac{n!}{m!(n-m)!},\\&{n\choose m-1} =\frac{n!}{(m-1)!(n-(m-1))!} = \frac{n!}{(m-1)!(n-m+1)!}\end{align*}
Secondly, consider $p^m(1-p)^{n-m}$ and $p^{m-1}(1-p)^{n-m+1}$. From the context of these inequalities, $p$ is positive and nonzero, so $p^{m-1}$ is also positive and nonzero. This means we can divide both sides by $p^{m-1}$ without affecting the inequality. Upon division, you get $p(1-p)^{n-m}$ and $(1-p)^{n-m+1}$ respectively.
Let's focus on $p(1-p)^{n-m}$. I will show that it is less than or equal to $p^{n-m}$. We know that $n > m$ and $0 < p < 1$. (If $p=0$ or $p=1$, the inequality is trivial.) I will denote $n-m = x$, so we know that x is a non negative integer. We want to show that $p^x \geq p(1-p)^x$.
First consider the case where $x=0$. This holds trivially, as $p^0 = 1 > p = p(1-p)^0$. Now we can consider the case where $x ≥ 1$.
Note that $p(1-p)^x = (p^{1/x}(1-p))^x$. Thus it suffices to show that $p ≥ p^{1/x}(1-p)$. Because $p$ is strictly greater than $0$ and less than $1$, $p^{1/x}$ gets larger as $1/x$ gets smaller. If we can show that the inequality holds for the smallest possible value of $1/x$, then we have shown that it holds for all values of $x$. Obviously there is no "smallest" value for $1/x$, it is just the limit as $x$ tends to infinity.
This case is also trivial given my assumption of $p > 0.5$. $\lim_{x\rightarrow\infty}p^{1/x} = p^0 = 1$. This leads to $p^{1/x}(1-p) = (1-p)$. Thus it is clear that if $p>0.5$, $p ≥ (1-p) ≥ p^{1/x}(1-p)$. (You can also see that $p >0.5$ is a necessity. if $p<0.5$, you can just find a large enough $x$ to reverse the inequality.)
This covers every case, so we end up with the inequality chain:
\begin{align*}
{n\choose m} p^m (1-p)^{n-m} &≥ {n\choose m-1} p^{m-1}(1-p)^{n-m+1}\\
\Rightarrow \frac{n!}{m!(n-m)!} p^m (1-p)^{n-m} &≥ \frac{n!}{(m-1)!(n-m+1)!} p^{m-1}(1-p)^{n-m+1}\\
\Rightarrow \frac{n!}{m!(n-m)!} p (1-p)^{n-m} &≥ \frac{n!}{(m-1)!(n-m+1)!}(1-p)^{n-m+1}\\
\mathrm{For}\ p > 0.5, \Rightarrow \frac{n!}{m!(n-m)!} p^{n-m} &≥ \frac{n!}{(m-1)!(n-m+1)!}(1-p)^{n-m+1}
\end{align*}
Comment: This is a comprehensive answer; I doubt the question writer expected this level of reasoning though, but this is still a thorough answer nevertheless


How do we visualize csv point cloud data?

Question: I am having an xyz file I want to preprocess and save it as a df, how do I visualize the csv file or convert it into xyz or las format in python
Comment: What about `df = pandas.read_csv()`? Have you looked into pandas, geopandas.
It is not clear what you mean by "visualize", what is Ias, and so on.
Comment: xyz is a point cloud data format, I have point cloud points data in csv file and I want to convert it into 3D format to visualize using open3d
Comment: Does [this](https://stackoverflow.com/questions/70598949/reading-pointcloud-from-csv-to-ros-pointcloud2) help? You would need  to use your point cloud format instead of
 `sensor_msgs`
Answer: save each xyz to a line, separate by a space, and add a column represent the color, and save these lines to a txt file, then open the file with cloud-compare.
the txt file will looks like:
<code>1.23 0.12 3.45 1
2.23 1.12 4.45 1
3.23 2.12 5.45 0
</code>


How to initialize variable with cleanup attribute?

Question: Is there a way to initialize a variable with the cleanup compiler attribute? or do I have to set the value after declaring the variable?
I've tried putting the cleanup attribute in front of = malloc(10); like in the example below and behind = malloc(10); but neither compiles.
#include <stdio.h>
#include <stdlib.h>

static inline void cleanup_buf(char **buf)
{
    if(*buf == NULL)
    {
        return;
    }

    printf("Cleaning up\n");

    free(*buf);
}

#define auto_clean __attribute__((cleanup (cleanup_buf)));

int main(void)
{
    char *buf auto_clean = malloc(10);
    if(buf == NULL)
    {
        printf("malloc\n");
        return -1;
    }

    return 0;
}

Is there a different syntax for using cleanup and initializing the variable on one line? Or do I have to set the value after declaring the variable like in the example below?
#include <stdio.h>
#include <stdlib.h>

static inline void cleanup_buf(char **buf)
{
    if(*buf == NULL)
    {
        return;
    }

    printf("Cleaning up\n");

    free(*buf);
}

/* Use this attribute for variables that we want to automatically cleanup. */
#define auto_clean __attribute__((cleanup (cleanup_buf)));

int main(void)
{
    char *buf auto_clean;

    buf = malloc(10);
    if(buf == NULL)
    {
        printf("malloc\n");
        return -1;
    }

    return 0;
}

Answer: Just mistypo. just...
//#define auto_clean __attribute__((cleanup (cleanup_buf)));
//                                                         ^
#define   auto_clean __attribute__((cleanup (cleanup_buf)))


Add View To page.tpl

Question: I hope that you could help me out with this. After much time ive finally managed to work out how to takeover the space that my breadcrumb was taking up. With this bit of space i thought about adding a banner in there or a welcome message. I dont want my site to look 'tatty' so the banner idea was soon forgotten. A welcome message would be nice but it will be going through all my site and im sure that folks will get a bit sick of it.
I was hoping to add a View of random quotes in there. You can see the 'spot' i mean on my frontpage here-
http://onlinebanter.com/
As i type this though i'm a wondering if i could make that spot a block region? Then i could have a welcome message on the frontpage and then use it as a region to add helpfull advice for differant sections of my site. also i could add a random quote view for within nodes etc.
Could anyone offer me advice on either adding a view to a page.tpl or creating a block region in the wee spot ive found?
All help appreciated.
thanks
Answer: You can open your theme's .info file and add regions[my_region] = My region. Now open your page.tpl.php and do print $my_region (the name of the variable corresponds to the regions array index - see the first sentence) where the region should be rendered. After this make sure to clear your Drupal's cache so that it picks up the new region and the modified page.tpl.php.
Views can generate block displays. After you created the block display, you can use the Drupal's default blocks interface to put the Views block into the region you created, or better yet use a module like: Context which gives you more control over when to show which blocks in what region and when not to...
Answer: If you go for the view block directly in the template, you can use views_embed_view('yourview', 'yourblockdisplay') in your block's block--id-of-block.tpl.php (name depends on your drupal version).
If you want to create a region (which is probably overkill here), you have to add it to your theme.info in this form regions[quotes_region] = Region name and add the relevant variable in yourtheme_preprocess_block, then add the block to the region.
Comment: Thanks Count. Overkill i dont do. I actually overkill the simplest of things not because im picky or owt i just go the long drupal way about most things. ah brilliant i like the sound of your quick fix.

Im on drupal 6 Count. I will go ....bah ive been drinking wine all night. i will leave this best for later tonight when drupalers like me come out to play. I really appreciate you helping me along the road. Ive only been at drupal 100+ a week for 6 years nearly :(
Comment: I don't think it's overkill, and I think adding a region where a block should be rendered is the correct way to do it. And I don't think you need to preprocess the theme to add this variable, it will be automatically generated, you can just do 'print $quotes_region' in your page.tpl.php file where the region should be rendered, if the name of the region is quotes_region. And it's not very nice way to create a Views block and then views_embed_view that block into block's own template? It doesn't make sense...

Add View To page.tpl


"When The Sun Goes Down" by Arctic Monkeys Chords

Question: Where do these chords come from in "When the Sun Goes Down" by Arctic Monkeys?
The chords do not line up with the key of B.  I am confused.
Here are the song chords used: B, D#7, Emaj7, D#m, C#m, F#7, E7, G#m, G, F#, Bm, Em, F#m, Bm, A, F#
These are the chords in the key of B: B,    C#m, D#m, E, F#, G#m, A#dim, Bmaj7, C#min7, D#m7, Emaj7, F#7, G#m7, A#m7b5
Some overlap, some do not.
Thank you!
Comment: Whoever gave you the idea that every chord in a piece has to be **diatonic** needs to answer to it. Just not true - never has been! True, your 'song chords used in B' are all correct, but that doesn't mean they're exclusive to B. So the question is being asked using false premises.
Comment: BTW, no one has welcomed Steven, so, welcome, Stephen.
Comment: Regarding the votes to close, this question is not asking someone to transcribe chords or anything else--specific chords are already provided. The example of a basic analysis question is "what key is this song in?" This question is asking for substantially more complex analysis than that. These are the reasons why I voted to keep it open.
Answer: "Where do these chords come from in "When the Sun Goes Down" by Arctic Monkeys?
The chords do not line up with the key of B. I am confused."
You are indeed!  Confused in your concept that the chords of a song SHOULD all be diatonic - using only the notes of the home scale.   Where did that come from?
Ditch that concept, then you can ditch a lot more 'theory' like 'modal interchange' and 'borrowing' that seek to justify the naughtiness of venturing outside the diatonic.  It's allowed.  Really!
OK, that's going too far.  Don't ditch them entirely.  Just recognise them for what they are - descriptions. There's the plain vanilla ice of the diatonic.  (Nothing wrong with that.  I like plain vanilla ice.)   Then there's the sprinkles of chromaticism.  It's still a vanilla ice, but with added exotic touches.   Then there's a whole range of mixtures and additions culminating in a full-blown Knickerbocker Glory containing multiple flavours of ice cream plus every imaginable extra!
Theory can measure the degree of exoticism. It can't tell us WHAT chord or note to choose, but it can list the ones that are close to home (good) or further out (also good).   Vanilla ice or Knickerbocker Glory?  Your choice!  Enjoy!
Comment: Put another way - keep your choice of notes to a maximum of twelve different ones - do not exceed that number.
Answer: The chords in the opening and closing slow section until the G#m are all diatonic to the key of B with the exception of the D#7. Making a 3 chord dominant (D#7) instead of minor is a common non-diatonic substitution. Not every chord in a song has to come from the diatonic chords of the song's key. Even relatively simple songs sometimes have non-diatonic chords in them.
The fast section changes key to B minor so starting with the G chord the chords in this section are diatonic to B minor:
Bm C#o D Em F#m G A
The 5 chord can either be F#m or F#7. They both can both be used in the key of B minor and in this case they both are used at different times.
Hope this explains it.
Comment: Hotel California does a similar thing, in key Bm, using E in one place, Em in another (obviously not as V chord).
Comment: @Tim Hotel California has such an interesting progression in the verse, Bm F#7 A E G D Em F#7. It is almost a completely diatonic progression except for the E chord but to me it sounds like 1m-5 in Bm, then 4-1 in E, then 4-1 in D, then 4m-5 in Bm, but it all ties together so well!
Comment: I had to check its diatonicity! But, yes, it is! That's if you belong to the 'minors are diatonic' school! Bit of borrowing, maybe? But what the heck, great sequence - a one off I believe, not emulated anywhere.
Answer: You're asking the wrong question, but it's a question that keeps getting asked over and over and over again. "Where did these non-diatonic chords come from." It has been asked by so many people, and people give the same answers, and it never helps, and the questions keep on coming. The OPs eventually give up and go away, but I have a feeling that few of them really understood much about what was said.
The types of answers that are usually given are:

Modal interchange and borrowing and secondary hyper plagalified pseudo-dominantification of supercalifragilisticexpialidocious structures. But if you learn to recite the "correct" theoretical abracadabra, is that understanding?
They're just notes. You can play anything anywhere, it's not forbidden to play non-diatonic notes. But how does this help, if the problem is that the OP can't reason about what's happening?

The real question you should ask is, "What happens when I use these non-diatonic chords?" You feel that something happens, which is good - you're not deaf!
What happens is: the harmonic feeling is temporarily changed. Call that "mode" or "harmonic context" or whatever, it is somehow modified by the non-diatonic chords. The feeling of the tonic note, the home note, is not necessarily changed, but the harmonic possibilities around the tonic are changed. Notes added to (played on top of) the chords do different things that they would have done without the change.
Let's take the first one, the B chord. What does it do, and what possibilities does it offer? You're probably expecting to hear only notes from the B major scale on that, and those notes won't surprise you. You could play for example an E# note on the B major chord and it would produce a B Lydian harmonic feeling like in the Simpsons theme, but it probably wouldn't occur to you to try such a bizarre thing because the only possible thing in your world is to play notes from the major scale based on the tonic.
Then the next chord, D#7. If the song's key was G#m, which is B major's relative minor key, would you be surprised to find a D#7 chord there? The D#7 chord changes the harmonic possibilities so that suddenly the E# note that felt like Simpsons on the B, feels completely natural, and should I say, melodic even. And you can play the F## (or G) note, and the F# that used to "belong" in your B major doesn't feel that good anymore. And if you press pause on that chord, and then instead of the E major chord that would be coming next, you play G#m ... it feels pretty natural to you, doesn't it? Almost as if the D#7 had resolved to the G#m, don't you think?
Then the Emaj7. It returns things back to normal. The same for D#m, C#m, F#7. All the notes of the B major scale feel ok now. Though ... on the F#7, if you press pause there and play a B major chord, it feels as if the F#7 had resolved, doesn't it?
But then, E7 ... it has a minor seventh, a dominant seventh, there's a D note in the chord? Try to play your D# note that felt appropriate on the Emaj7 just a few seconds ago, and now it doesn't fit in! You have to play a D now. And even the A# doesn't feel that nice, it's better with an A ... almost as if the scale was A major, three sharps?
Etc. etc. Every chord changes the way other notes feel.
But where did the chords come from into the composers mind? How did it occur to the composer to start causing harmonic changes like that? At least two possibilities:

(1) They were thrown in deliberately randomly. The composer wanted to have something "outside" the diatonic cage, and they picked some chord. Then they tasted the feeling for awhile, maybe they tried how the "outside" chord changes the harmonic possibilities ... and they liked it!
(2) The composer had previously encountered this kind of a chord and harmony change in some other song. They already knew what kind of a harmonic change it will create, and that was what they wanted to use.

So there. That's where the chords come from.


Select multiple properties where one of the properties must be unique in a pipeline

Question: Using Select-Object, you can select the first of multiple occurences of an object using -Unique. You can also select only some properties to be retained. But what if I want to check for the uniqueness of only one property, filter out all non-unique ones and not just leave one of them, and allow the other properties to pass through unchanged? Specifically, I have a list of PSCustomObjects where one property is a base name, and another is a shortened name generated automatically. I want to filter out elements where the short name occurs multiple times, but the base names of all items are all different. It seems Select-Object isn't a match for this task, but what is?
EDIT: To clarify - this should be the result:
> $A = [PSCustomObject]@{A = 1; B = 2}, [PSCustomObject]@{A = 2; B = 2}, [PSCustomObject]@{A = 4; B = 1}
> $A

A B
- -
1 2
2 2
4 1

> $A | Mystery-Cmdlet "B"

A B
- -
4 1

Comment: Maybe `$A | Group-Object -Property B | Where-Object Count -eq 1 | Select-Object -ExpandProperty Group`?
Comment: Did you mean `Select-Object`? `Where-Object` doesn't have a `-Unique` switch
Comment: Yes, sorry. I'll fix it now.
Comment: You can use `$a[2]` to see rows and `$a.a` to see specific columns. Does this answer your question?
Comment: @NekoMusume ...no
why would it?
Comment: What is `Mystery-Cmdlet`? This can show you the specified output `$a[2]`
Comment: `Mystery-Cmdlet` is something that solves the problem in the question. I want to go through an array and throw out all elements if the value of its B property is found in any other element's B value.
Comment: @JosefZ yes, that solves it, although I'd use `ForEach-Object Group` instead, thanks. Why not post that as an answer?
Answer:
I have a list of PSCustomObjects… I want to check for the uniqueness of only one property, filter out all non-unique ones and
  not just leave one of them, and allow the other properties to pass
  through unchanged…

Let's rewrite above requirements in terms of PowerShell cmdlets:
$A = @'
A,B
1,2
2,2
4,1
5,4
'@ | ConvertFrom-Csv -Delimiter ','        # a list of PSCustomObjects

$A |
  Group-Object -Property B |               # check for the uniqueness,
    Where-Object Count -eq 1 |             # filter out all non-unique ones,
      Select-Object -ExpandProperty Group  # and pass through the rest unchanged

Output: 62219608.ps1

A B
- -
4 1
5 4

Answer: I think you have to resolve it with code
$A = [PSCustomObject]@{A = 1; B = 2}, [PSCustomObject]@{A = 2; B = 2}, [PSCustomObject]@{A = 4; B = 1}

$A | Add-Member -NotePropertyName Count -NotePropertyValue init

for ($i=0; $i -lt $a.Length; $i++) {
$a[$i].Count = ($A.B | Where-Object {$_ -eq $a[$i].B}).Count
}

$a | Where-Object {$_.Count -eq 1}


What does `Kbytes RSS Dirty` mean for pmap?

Question: output of pmap:
Address           Kbytes     RSS   Dirty Mode   Mapping
00000000006b4000      60      16      16 rw---    [ anon ]

What's it saying ?
Answer: From man:

Address:  start address of map
Kbytes:   size of map in kilobytes
RSS:  resident set size in kilobytes
Dirty:    dirty pages (both shared and private) in kilobytes
Mode: permissions on map: read, write, execute, shared, private (copy on write)
Mapping:  file backing the map, or '[ anon ]' for allocated memory, or '[ stack ]' for the program stack
Answer: Here pamp specifics ...
http://www.cyberciti.biz/tips/howto-find-memory-used-by-program.html
http://linux.101hacks.com/unix/pmap/
Check here: https://stackoverflow.com/questions/118307/a-way-to-determine-a-processs-real-memory-usage-i-e-private-dirty-rss It explains what RSS and Dirty seems to be ...
Comment: -1 The question asks about the meaning of particular fields. The answer provides generic links about pmap usage.


Meaningful to retrieve original value after standardization using clustering

Question: I already referred these posts here and here.
Currently, I am working on customer segmentation using their purchase data.
So, my data has below info for each customer

Based on the above linked posts I see that for clustering, we have to scale the variables if they are in different units etc.
But if I scale/normalize all of them to uniform scale, wouldn't I lose the information that actually differentiates the customers from one another? But I also understand that monetary value could construed as high weighted feature because they might go upto range of 100K or millions as well.
Let's assume that I normalized and my clustering returned 3 clusters. How do I answer below questions meaningfully?
q1) what is the average revenue from customers who are under cluster 1?
q2) what is the average recency (in days) for a customer from cluster 2?
q3) what is the average age of customer with us (tenure) under cluster 3?
Response to all the above question using normalized data wouldn't make sense because they all amight be in a unform scale mean 0, sd 1 etc. If I say average age in cluster 3 is 0.356, it wouldn't make sense to users.
So, I was wondering whether it is meaningful to do the below
a) cluster using normalized/scaled variables
b) Once clusters are identified, use customer_id under each cluster to get the original variable value (from input dataframe before normalization) and make inference or interpret clusters? Is it okay to interpret clusters this way (and not using nornalized values)
c) I believe the pattern found in nornalized data is also applicable to original data (even though we disnt feed the original data representation to the model)
So, do you think it would allow me to answer my questions in a meaningful way
Is this how data scientists interpret clusters? they always have to link back to input dataframe?
Answer:
But if I scale/normalize all of them to uniform scale, wouldn't I lose the information that actually differentiates the customers from one another?

No, changing the scales doesn't discard information about the customer. As long as the transformation is monotonic and one-to-one, the rescaled data has exactly the same information about one customer to another customer. We want the transformation to be monotonic so that the relative ordering of the customers according to the feature is the same, and we want it to be one-to-one so that it's reversible (we can move from the original units to the scaled units and back to the original units exactly).

What is the average revenue from customers who are under cluster 1?
What is the average recency (in days) for a customer from cluster 2?
What is the average age of customer with us (tenure) under cluster 3?

This is just an elementary database operation. One table is the customer IDs and the cluster assignments. The second table has the customer IDs and revenue/other data elements. Then do an inner join and do your aggregation by cluster assignment.
Comment: Yes, understand. My question was not really on database operation but to know whether pattern found on normalized data, also applies to raw data. Your 1st part of answer helps..
Comment: So, once we find clusters, you suggest that I can use the original/raw values to make inference or interpret clusters. Instead of normalized values. Did I understand that right?
Comment: You get to decide how you want to answer your own research questions. I think most people find it easier to think about dollars than standardized dollars, such as $z$-scores of dollars relative to all customers included in the study.
Comment: Understand. based on your response, I have a follow up question on this. I created a new post. Would be helpful to have your views on this -  https://stats.stackexchange.com/questions/576745/why-not-link-features-instead-of-feature-selection-clustering


Facebook Graph API me/accounts returns empty Json data array

Question: I am getting the following empty data array for https://graph.facebook.com/me/accounts?access_token=XXXXXXXXX:
STEP 1: - Get the CODE using Oauth URL
STEP 2: - Get the access token using the above CODE in Oauth access token URL
Thanks in advance.
Comment: Add all the required permissions as mentioned in this answer: https://stackoverflow.com/a/76340626/12326605
Answer: What permissions does the access_token have?  (see https://developers.facebook.com/tools/debug)  You may not have requested the correct ones.
Comment: That's very strange.  You have the permission of `manage_pages` which is required for `me/accounts`.  Have you tried running the me/accounts in the Graph API Explorer tool to see if it returns the same thing?
Comment: I have the following output but me/accounts return empty array:   App ID:      11111111111 : MyAppName
User ID: 12345678900 : FB user name
Issued:         1325555945 : 5:59 pm Jan 2 2012
Expires: Never
Valid:  True
Origin:  Web
Scopes:  create_note manage_pages offline_access photo_upload publish_stream read_stream share_item status_update video_upload
Comment: yes. I tried in graph explorer as well and returns empty JSON data array only. Is there anything to do with user roles?
Answer: <code>function get_html($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_FAILONERROR, 0);
    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}

function get_json($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_FAILONERROR, 0);
    $data = curl_exec($ch);
    curl_close($ch);

    return json_decode($data);
}

if ($_GET['type'] == "page") {

    $pages =
        get_json("https://graph.facebook.com/me/accounts?fields=id,picture,name&access_token=$token")->data;

    foreach ($pages as $page) {
        $page->picture;
        $page->name;
        $page->id;
    }
}
</code>


Distributions of sampling statistics problem (not a hw problem)

Question: for the following question:

the lifetime of a particular brand of cutting blade has a mean of 40 hours and sd of 8 hrs. when one blade fails, it is immediately replaced by a new identical blade. suppose there are 36 blades on hand. use CLT to find approximate probabilities:
  a. what is the probability that sample average of lifetimes of the 36 blades exceeds 60 hrs?
  b. compute the probability that all these 36 blades will be used up in less than 1250 hrs.

let n=36 and sd = standard deviation.
can someone please explain why

a follows N(u, sd^2/n) and b follows N(n*u, n*sd^2)
Answer: Let random variables $X_1, X_2, \dots, X_n$ be the lifetime of the first blade, the second,and so on. Here $n=36$.
The $X_i$ are independent, and therefore the variance of their sum is the sum of their variances. Thus the sum $X_1+\cdots+X_n$ of the lifetimes has variance $8^2+8^2+\cdots +8^2$ ($n$ terms). This sum is $(n)(8^2)$, and hence the variance of the sum is $(n)(8^2)$. The standard deviation of the sum is therefore$(\sqrt{n})(8)$.
We also use an informal version of the Central Limit Theorem. The sum of $n$ independent identically distributed "nice" random variables is "nearly normal" if $n$ is large enough.
The numerical questions: (a) The sample average exceeds $60$ precisely if the sample sum exceeds $(36)(60)$. So in a sense problems (a) and (b) are very much alike.
But there is a slightly different approach to (a) which is probably a bit better. Let $S$ be the sample sum discussed above. Then the sample average $Y$ is equal to $\dfrac{S}{n}$. The random variable $Y$ is also nearly normal. It has mean $40$. The variance of $\dfrac{S}{n}$ is $\dfrac{1}{n^2}$ times the variance of $S$ calculated above. It follows that $Y$ has variance $\dfrac{(n)(8^2)}{n^2}$, that is, $\dfrac{8^2}{n}$. Since $n=36$, $Y$ has standard deviation $\dfrac{8}{6}$.
So we want the probability that a normal random variable with mean $40$ and standard deviation $\dfrac{8}{6}$ exceeds $60$. Since $60$ is a ridiculous number of standard deviation units away from $40$, the probability is nearly $0$.
(b) The mean of the sum is $1440$. We want the probability that a nearly normal random variable with standard deviation $48$ is less than $1250$. This is a standard calculation. Pretty unlikely!
Remark: You may be asking why is the variance of an independent sum equal to the sum of the variances. Here is a proof for a sum of two random variables $X$ and $Y$. The proof readily extends to longer sums.
Let $X$ and $Y$ be independent, with variances $\sigma^2_X$ and $\sigma^2_Y$. Recall that $\text{Var}(W)=E(W^2)-(E(W))^2$. Apply this with $W=X+Y$. We have
$$E((X+Y)^2)=E(X^2+2XY++Y^2)=E(X^2)+2E(XY)+E(Y^2)=E(X^2)+2E(X)E(Y)+E(Y^2).\tag{$!$}$$
(For the fact that $E(XY)=E(X)E(Y)$ we used independence.)  Also.
$$(E(X+Y))^2=(E(X)+E(Y))^2=(E(X))^2+2E(X)E(Y)+(E(Y))^2.\tag{$2$}$$
Using $(1)$ and $(2)$ and rearranging a bit, we find that
$$\text{Var}(X+Y)=E(X^2)-(E(X))^2 +E(Y^2)-(E(Y))^2.$$
But the above is just $\sigma^2_X+\sigma^2_Y$.
Comment: that explain everything. thanks :)


Conflicting column names in Ruport report table

Question: When using Ruport to make a CSV file for entities containing the same entities, the generated column names create conflicts, causing Ruport to show only the first occurence of this column(s). To make this abstract explanation more clear and less complicated, an example:
My class Zone inherits from ServerUnitConfig, which has a :belongs_to to a ServerUnit.
So Zone has for example server_unit.su_name as a field.
Zone also has a :belongs_to for Domain, which also inherits from ServerUnitConfig.
I want both to be included in my Ruport and to do this I have the following :include argument for my report_table of Ruport:
{
    :server_unit => {:only => 'su_name'},
    :domain => {:include => {:server_unit => {:only => 'su_name'}}, :only => {}
}

Reporting this with Ruport in a CSV file, gives a report showing only the server_unit.su_name column of server_unit not the one of Domain. Normally also the server_unit.su_name should be shown, but since Ruport only shows the field name and its parent, both cases show server_unit.su_name and this gives conflicts.
I would suggest to give a custom name to the field in the include, but I don't know how. An other idea, if it would be possible, is to tell Ruport one or the other way it is no problem to have identical column names, but I don't think that is possible. Has anybody an idea to solve this problem? It would help me a lot!
Thanks
Daan
Answer: You could use the report_table_by_sql method, which is a bit ugly. Or use the :method option to call a method with a slightly different name, not great either.
There is a :qualify_attribute_names option for each include that was used internally. I've written a patch here: https://gist.github.com/1057518 that will expose it, you can use it like so:
{:server_unit => {:qualify_attribute_names => 'serv', :only => 'su_name'}}

To apply the patch you'd need to "vendor" the acts_as_reportable gem in Rails, which can be a pain. I'll try and put it on the main repo at https://github.com/ruport/acts_as_reportable soon when I'm sure it has no problems.
Hope that helps,
Andrew
Comment: I already found out that methods are indeed an option and so I solved it this way. Your :qualify_attribute_names could be a useful addition to the code. It's still a lot of work for the slight difference it makes, but it could still come in handy. It does the same as methods I suppose, but in an a lot cleaner way. Thx... and Keep me informed about your plug-in.


How to group EC2 instances in order to delegate administration to different teams?

Question: Is it possible (using ARN) to make several groups of instances.
Then using different policies to grant access to a group of instance only and not the other instances?
For example :
<code>{
  "Statement": [
    {
      "Action": "ec2:*",
      "Effect": "Allow",
      "Resource": "*"
    },
    {
      "Effect": "Allow",
      "Action": "elasticloadbalancing:*",
      "Resource": "*"
    },
    {
      "Effect": "Allow",
      "Action": "cloudwatch:*",
      "Resource": "*"
    },
    {
      "Effect": "Allow",
      "Action": "autoscaling:*",
      "Resource": "*"
    }
  ]
}
</code>
Instead of "*" could we use a group or something like that? like a specific subnet? a Tag? or whatever...
Thanks for your help
Answer: No EC2 doesn't provide any way to do this with instances. ARN resource names are only available for products that are listed with a check in the Resource column and there is no other method of filtering out resources.
http://docs.amazonwebservices.com/IAM/latest/UserGuide/Using_SpecificProducts.html


json object missing root element when returned from api

Question: I try to invoke a method in an api using jquery, but I get an error saying

XML Parsing Error: no root element found

The API method:
[HttpGet]
public async Task<IActionResult> GetJob(int id)
{
    var result = await this.DB.GetJob(id);

    return Ok(result);
}

JQuery:
function InvokeAjax (controller, action, method, query = {}, body = {}, callback) {
    let url = "/Secure/" + controller + "/" + action;

    if (query.length > 0) {
        let tmp;
        let i = 1;

        query.each(function (index) {
            if (i === 1) {
                tmp = "?";
            }
            else {
                tmp += "&";
            }

            tmp = this.key + "=" + this.value;

            i++;
        });

        url += tmp;
    }

    $.ajax({
        type: method.toUpperCase(),
        url: url,
        data: JSON.stringify(body),
        dataType: "JSON",
        contentType: "application/json",
        success: function (result) {
            if (callback !== null && callback !== "undefined") {
                callback(result);
            }
        }
    });
}

The error says xml parsing error, is this the problem? I have defined JSON as return type so I don't know.
This is what gets returned from the api:

{"clientId":3,"finished":false,"dateLastInvoked":"2018-09-09T04:07:40.8996335","name":"Jobb
  nr 3","description":"Enda en jobb","items":[{"jobId":3,"comment":"løst
  gåten","items":[{"timeIntervalId":3,"dateEnd":null,"state":2,"differenceRaw":201.21893158,"difference":3.4,"id":4,"uId":"6ad40e18-7e18-489f-9503-da25eac533a1","active":true,"dateCreated":"2018-09-09T04:07:41.1398231","helper":{}},{"timeIntervalId":3,"dateEnd":null,"state":3,"differenceRaw":201.21897233333334,"difference":3.4,"id":5,"uId":"2e3bad04-e454-4241-8aa4-0f6a124e6837","active":true,"dateCreated":"2018-09-09T04:07:41.1398254","helper":{}}],"differenceRaw":402.43795291833334,"difference":6.7,"state":2,"id":3,"uId":"82de2d44-8de7-49c0-8674-08e8b579c4ae","active":true,"dateCreated":"2018-09-09T04:07:41.0085169","helper":{}}],"timeSpentRaw":402.43801081333334,"timeSpent":6.7,"state":2,"running":true,"id":3,"uId":"cbd16771-3a1b-409f-9e67-515d03eb5116","active":true,"dateCreated":"2018-09-09T04:07:40.8996351","helper":{}}
Answer: JSON is definitely not xml. But I think dataType is case sensitive. Or better use $.getJSON: https://api.jquery.com/jquery.getjson/
Anyway you should check your ajax call as it looks you are not using the parameters correctly.
Comment: so the returned json is ok? the browser parses it correctly so I think the returned data should be ok
Comment: Yes, the returned yson looks ok - but your call forces it to look for xml. Dont overconfigere ajax calls. Use shorthands and be sure to use correct parameters. This is all you need for ajax get: `$.ajax({
  dataType: "json",
  url: url,
  data: data,
  success: success
});`
Answer: You forgot to set content-type: application/json header at API end.

I am assuming the error is at client side.

You should use JSON() instead of OK()
Comment: I'm not sure I understand what you mean. I set contentType in the ajax call. I can't find any property named content-type
Comment: it turned out there was an error with the url, so it works fine now. thanks!


Get rid of the square roots of the denominator: $\dfrac{1}{\sqrt{7}-2\sqrt{5}+\sqrt{3}}$

Question:
How to get rid of the square roots of the denominator: $\dfrac{1}{\sqrt{7}-2\sqrt{5}+\sqrt{3}}$?

I squared the whole denominator, but that didn't help.
Also I searched for a propriety or identity like $A^2-B^2$, but I didn't see one that could fit.

Any help is appreciated.
Answer: The trick here is to multiply and divide by the conjugate.
For instance $$\frac{1}{1+\sqrt{2}} = \frac{1-\sqrt{2}}{(1+\sqrt{2})(1-\sqrt{2})} = \frac{1-\sqrt{2}}{-1}$$
We can do the same here, but we will do it twice.
$$\frac{1}{\sqrt{7}-2\sqrt{5}+\sqrt{3}} = \frac{\sqrt{7}-2\sqrt{5}-\sqrt{3}}{((\sqrt{7}-2\sqrt{5})+\sqrt{3})((\sqrt{7}-2\sqrt{5})-\sqrt{3})} $$
$$=\frac{\sqrt{7}-2\sqrt{5}-\sqrt{3}}{(\sqrt{7}-2\sqrt{5})^2-3} = \frac{\sqrt{7}-2\sqrt{5}-\sqrt{3}}{30-4\sqrt{35}}$$
Now multiply and divide by $30+4\sqrt{35}$ and you will elliminate the radical here as well.
Comment: I am glad I could help :)
Comment: Thanks @TimRaczkowski I thought I caught all of those typos.
Comment: I'm really really sorry. I'm still learning. Please forbear with me.
Comment: It's ok @Mathink. You should accept the answer you found most helpful. There isn't really any rule to it. It's your call.
Answer: $$\begin{align}\frac{1}{\sqrt 7-2\sqrt 5+\sqrt 3}&=\frac{1}{\sqrt 7-2\sqrt 5+\sqrt 3}\cdot \frac{\sqrt 7-2\sqrt 5-\sqrt 3}{\sqrt 7-2\sqrt 5-\sqrt 3}\\&=\frac{\sqrt 7-2\sqrt 5-\sqrt 3}{(\sqrt 7-2\sqrt 5)^2-3}\\&=\frac{\sqrt 7-2\sqrt 5-\sqrt 3}{4(6-\sqrt{35})}\cdot\frac{6+\sqrt{35}}{6+\sqrt{35}}\\&=\frac{(\sqrt 7-2\sqrt 5-\sqrt 3)(6+\sqrt{35})}{4}\end{align}$$
Answer: To get rid of the square roots of the denominator, you may use $a^2-b^2=(a+b)(a-b)$.
$$\dfrac{1}{\sqrt{7}-2\sqrt{5}+\sqrt{3}}\\=\dfrac{\sqrt{7}-2\sqrt{5}-\sqrt{3}}{(\sqrt{7}-2\sqrt{5})^2-3}\\=\dfrac{\sqrt{7}-2\sqrt{5}-\sqrt{3}}{7-4\sqrt{35}+20-3}\\=\dfrac{\sqrt{7}-2\sqrt{5}-\sqrt{3}}{24-4\sqrt{35}}\\=\dfrac{(\sqrt{7}-2\sqrt{5}-\sqrt{3})(24+4\sqrt{35})}{24^2-16*35}\\=\dfrac{(\sqrt{7}-2\sqrt{5}-\sqrt{3})(24+4\sqrt{35})}{16}\\=\dfrac{-24\sqrt3-20\sqrt5-16\sqrt7-4\sqrt{105}}{16}\\=\dfrac{-6\sqrt3-5\sqrt5-4\sqrt7-\sqrt{105}}{4}$$


selecting multiple records from database by interval of integers in java

Question: How can I select from derby database by interval of integers? Can it be done more effectively than using for cycle, incrementing an int and trying to select record from database for every int from interval? thanks
Answer: A query like this should do it:
select *
from mytable
where some_column between ? and ?

Derby uses JDBC and SQL, so you can create a query with placeholders (question mark) for the variable then supply values at runtime via query.setInt(1, 3)  and query.setInt(2, 7) for example to get the range of 3 to 7 (inclusive),
Answer: Do it on the database side, eg doing SQL like this will only give you each 3rd row. (It is assumed you have a table called mytable with primary key column id)
SELECT * FROM mytable WHERE id % 3 = 0

Comment: any idea how can i do that?


c variables allocation on memory, pointers

Question: How are variables are located in memory? I have this code
int w=1;
int x=1;
int y=1;
int z=1;

int main(int argc, char** argv) {
    printf("\n  w %d",&w);
    printf("\n  x %d",&x);
    printf("\n  y %d",&y);
    printf("\n  z %d",&z);
    return (EXIT_SUCCESS);
}

and it prints this
w 134520852
x 134520856
y 134520860
z 134520864

We can see that when other integer is declare and assigned, the address is moved four positions (bytes I suppose, it seems very logic). But if we don't assign the variables, like in the next code:
int w;
int x;
int y;
int z;

int main(int argc, char** argv) {
    printf("\n  w %d",&w);
    printf("\n  x %d",&x);
    printf("\n  y %d",&y);
    printf("\n  z %d",&z);
    return (EXIT_SUCCESS);
}

it prints this
w 134520868
x 134520864
y 134520872
z 134520860

We can see there are four positions between addresses, but they are not in order. Why is this? How works the compiler in that case?
In case you want to know why I'm asking this, it is because I'm starting to study some security and I'm trying to understand some attacks, for instance, how integer overflow attacks work, and I've been playing with pointers in C to modify other variables by adding more positions than the size of the variable and things like that.
Comment: Look for 'bss' in the search box.  The difference is between initialized data in the data segment and uninitialized data in the BSS (block started by symbol) segment, which holds zero-initialized data.  For example [Why is the BSS segment required?](http://stackoverflow.com/questions/9535250/why-is-the-bss-segment-required)
Comment: Use %p for address. Your variables in the second example will automatically assigned the 0 value.
Comment: @JimBalter It is an answer to *But if we don't assign the variables*
Comment: The compiler allocates the variables wherever it chooses.
Comment: @valter The question isn't about the values of the variables and the code posted doesn't depend on them.
Comment: After studying things more carefully, you might also (or instead - of BSS) be interested in ASLR -- Address Space Layout Randomization.  I'm not sure whether that's a factor in what you're seeing; it is more typically applied to loading shared libraries at different addresses.  (Also, it is better to end messages from `printf()` with newlines than to start messages with newlines.  It more nearly ensures that the messages are printed in a timely manner.)
Comment: @valter Well, its not really an answer, but Jonathan's addition made it one I suppose.
Answer: Your first example initialises variables, which generates different allocation code. By looking into the assembly file generated by gcc (gas) I get:
    .globl  _w
    .data
    .align 4
_w:
    .long   1
    .globl  _x
    .align 4
_x:
    .long   1
    .globl  _y
    .align 4
 _y:
    .long   1
    .globl  _z
    .align 4
 _z:
    .long   1

And this basically dictates the memory addresses.
Your second example creates uninitialised variables, and as Jonathan said, those go into BSS. The assembler code is:
.comm  _w, 4, 2
.comm  _x, 4, 2
.comm  _y, 4, 2
.comm  _z, 4, 2

And that means you can't guarantee the sequence in which those variables will be located in memory.
Answer: The second set of numbers is also consecutive, just not ordered the same as in the source. I think the reason for this is simply that when you initialize the variables the compiler puts them in order because it maintains the order of initializations, in the second case you just get a random order.
In any case this depends on the compiler; I get the same pattern (ordered, 4 bytes apart) in both cases.


How to find Twitch username from a user ID?

Question: I am trying to find a twitch username from its ID? How do I do that?
For example, if I have the twitch user https://www.twitch.tv/user, I can open up the API https://api.ivr.fi/twitch/resolve/user, and find the users "id":"12345678". But how can I out from an user id, identify the user or username?
Answer: You need to use the twtich API, you can register an app in the Developer portal, the use this code to get the channel name:
<code>import requests
import json

CHANNEL_ID = '51496027'

#https://dev.twitch.tv/console/apps/create
client_id = '**client_id_here**'
client_secret = '**client_secret_here**'
grant_type = 'client_credentials'

# Get access token using app authentication exchange
url = 'https://id.twitch.tv/oauth2/token'
payload = {
    'client_id': client_id,
    'client_secret': client_secret,
    'grant_type': grant_type,
}
response = requests.post(url, data=payload)
access_token = json.loads(response.text)['access_token']

# Set up headers with access token for API requests
headers = {
    'Authorization': f'Bearer {access_token}',
    'Client-Id': client_id,
}

# Make API requests
url = f'https://api.twitch.tv/helix/channels?broadcaster_id={CHANNEL_ID}'

response = requests.get(url, headers=headers)
data = json.loads(response.text)
print(data['data'][0]['broadcaster_name'])
</code>


How do I increase the width that activates navbar-toggle CSS3

Question: I'm using bootstrap CCS3.
I have a larger menu on my page. On some PC's with smaller resolutions my menu is overlapping itself. I would like to have the navbar toggle to change at a higher width to resolve this.
I cannot find anywhere to control the width in which toggle occurs. Where am I going wrong here?
Code:

                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">

                    </button>

Home

page2------------------

page3------------------

page4------------------

page5------------------

page6------------------

page7------------------

I added --------- to simulate the spacing each item takes. I wasn't permitted to include the original text due to internal policies.
Comment: post your code please
Comment: its alright. so basically what your asking is how to change menu width? or how to check what resolution client has? also, since you didnt include the css, is your menu horizontal? is it supposed to fit 100% of screen? what do you mean by overlapping? if you are are not allowed to post a full example with css, could you provide a picture with the problem shown? also it would be great if you could create a demo in http://jsfiddle.net where we can see the problem recreated. you dont have to use original code, just create the same problem so we can see it.
Comment: I'm using the original bootstrap CSS (unmodified).

Basically when the browser window is in a lower resolution it adds what can't fit to a second line. I'm instead wanting it to change to 3 bars. If I continue to reduce resolution (or browser window width) it will eventually activate toggle. I want to control the width for when toggle gets activated.
Comment: ok im sorry im just trying to understand the question, the bar has 3 states - 1) all of it shown fully in one row, 2) it breaks into two rows, 3) menus disappear.  do you want one of those to occur at a higher screen width?
Comment: Are you familiar with bootstrap?

When you reduce the width of the browser window, it will change to toggle mode when the width is reduced to a certain width. I'm wanting to increase the width at which the toggle mode activates in order to cause toggle to become active at a much higher width. I have looked in the bootstrap documentation site but haven't had much luck yet finding it.
Comment: sorry i forgot to include a file and didnt see the effect. i understand our question now, i think you might find an answer here: http://stackoverflow.com/questions/18192082/bootstrap-3-navbar-collapse
Comment: That worked. Your link referred me to another post which helped me figured out that the reason I was having a problem was because the bootstrap-min.css file was getting in the way. I was editing the bootstrap.css file and not *-min.css. resolved this by editing the *-min.css file.
Comment: Can you add your comment as a response so I can mark it as answered? Thanks
Answer: adding this as an answer like op asked:
the solution can be found Here
for any people who encounter this issue in the future, i will also add op's comment:
it seems op figured out that he has to edit both bootstrap.css and 'bootstrap-min.css`