Q:

Efficiently comparing data across rows in a Pandas Dataframe

I have a CSV file of monthly cell phone bills in no particular order that I read into a Pandas Dataframe. I'd like to add a column for each bill that shows how much it differed from the previous bill for the same account. This CSV is just a sub-set of my data. My code works fine, but is pretty sloppy and very slow when you look at a CSV file close to a million rows.
What should I be doing to make this more efficient?
CSV:
Account Number,Bill Month,Bill Amount
4543,3/1/2015,300
4543,1/1/2015,100
4543,2/1/2015,200
2322,1/1/2015,22
2322,3/1/2015,38
2322,2/1/2015,25

Python:
import numpy as np
import pandas as pd
data = pd.read_csv('data.csv', low_memory=False)

# sort my data and reset the index so I can use index and index - 1 in the loop
data = data.sort_values(by=['Account Number', 'Bill Month'])
data = data.reset_index(drop=True)

# add a blank column for the difference
data['Difference'] = np.nan

for index, row in data.iterrows():

    # special handling for the first row so I don't get negative indexes
    if index == 0:
         data.ix[index, 'Difference'] = "-"
    else:
        # if the account in the current row and the row before are the same, then compare Bill Amounts
        if data.ix[index, 'Account Number'] == data.ix[index - 1, 'Account Number']:
            data.ix[index, 'Difference'] = data.ix[index, 'Bill Amount'] - data.ix[index - 1, 'Bill Amount']
        else:
           data.ix[index, 'Difference'] = "-"

print data

Desired Output:
   Account Number Bill Month  Bill Amount Difference
0            2322   1/1/2015           22          -
1            2322   2/1/2015           25          3
2            2322   3/1/2015           38         13
3            4543   1/1/2015          100          -
4            4543   2/1/2015          200        100
5            4543   3/1/2015          300        100

A:

try this:
In [37]: df = df.sort_values(['Account Number','Bill Month'])

In [38]: df['Difference'] = (df.groupby(['Account Number'])['Bill Amount']
   ....:                       .diff()
   ....:                       .fillna('-')
   ....:                    )

In [39]: df
Out[39]:
   Account Number Bill Month  Bill Amount Difference
3            2322 2015-01-01           22          -
5            2322 2015-02-01           25          3
4            2322 2015-03-01           38         13
1            4543 2015-01-01          100          -
2            4543 2015-02-01          200        100
0            4543 2015-03-01          300        100

Explanation:
diff() will be applied to each group separately - it'll return the difference between the "next" value and the current value:
In [123]: df.groupby(['Account Number'])['Bill Amount'].diff()
Out[123]:
3      NaN
5      3.0
4     13.0
1      NaN
2    100.0
0    100.0
dtype: float64

fillna('-') - fills all NaN's with the specified value: -:
In [124]: df.groupby(['Account Number'])['Bill Amount'].diff().fillna('-')
Out[124]:
3      -
5      3
4     13
1      -
2    100
0    100
dtype: object


Q:

Proof: Sum of dimension of orthogonal complement and vector subspace

Let $V$ be a finite dimensional real vector space with inner product $\langle \, , \rangle$ and let $W$ be a subspace of $V$. The orthogonal complement of $W$ is defined as
  $$
W^\perp= \left\{ v \in V \,:\, \langle v,w \rangle = 0 \text{ for all } w \in W \right\}.
$$
  Prove the following: $\dim W + \dim W^\perp= \dim V$.

I'm not sure how to find the relationship between number of basis vectors in $W$ and $W^\perp$.

A:

Let $\beta=\{w_1,w_2,\ldots,w_k\}$ and $\gamma=\{x_1,x_2,\ldots,x_m\}$ be the bases for $W$ and $W^\perp$, respectively. It suffices to show that
$$\beta\cup\gamma=\{w_1,w_2,\ldots,w_k,x_1,x_2,\ldots,x_m\}$$
is a basis for $V$.
Given $v\in V$, then it is well-known that $v=v_1+v_2$ for some $v_1\in W$ and $v_2\in W^\perp$. Also because $\beta$ and $\gamma$ are bases for $W$ and $W^\perp$, respectively, there exist scalars
$a_1,a_2,\ldots,a_k,b_1,b_2,\ldots,b_m$ such that
$v_1=\displaystyle\sum_{i=1}^ka_iw_i$ and $v_2=\displaystyle\sum_{j=1}^mb_jx_j$. Therefore
$$v=v_1+v_2=\sum_{i=1}^ka_iw_i+\sum_{j=1}^mb_jx_j,$$
which follows that $\beta\cup\gamma$ generates $V$. Next, we show that
$\beta\cup\gamma$ is linearly independent. Given
$c_1,c_2,\ldots,c_k,d_1,d_2,\ldots,d_m$ such that
$\displaystyle\sum_{i=1}^kc_iw_i+\sum_{j=1}^md_jx_j={\it 0}$, then
$\displaystyle\sum_{i=1}^kc_iw_i=-\sum_{j=1}^md_jx_j$. It follows that
$$\sum_{i=1}^kc_iw_i\in W\cap W^\perp\quad\mbox{and}\quad
\sum_{j=1}^md_jx_j\in W\cap W^\perp.$$
But since $W\cap W^\perp=\{{\it 0}\,\}$ (gievn $x\in W\cap W^\perp$,
we have $\langle x,x\rangle=0$ and thus $x={\it 0}\,$), we have
$\displaystyle\sum_{i=1}^kc_iw_i=\sum_{j=1}^md_jx_j={\it 0}$. Therefore
$c_i=0$ and $d_j=0$ for each $i,j$ becasue $\beta$ and $\gamma$ are bases
for $W$ and $W^\perp$, respectively. Hence we conclude that $\beta\cup\gamma$ is linearly independent.

A:

Hint:
Take a basis $w_1,\dots,w_r$ of $W$, and consider the linear forms on $V$ defined by $w_i^*:v\mapsto\langle w_i,v\rangle$.
These  linear forms are linearly independent, hence the solutions of the system of equations $w_i^*(v)=0,\ i=1,\dots r$ has codimension $r$ by the rank-nullity theorem. These solutions are precisely the orthogonal complement $\;U^{\bot}$.

A:

It is sufficient to show that $V=W\oplus W^{\perp}$. If $v\in W\cap W^{\perp}$, then $\left\langle v,v\right\rangle=0$. Hence it remains to show that any vector $v\in V$ can be written as $v=w+w'$ with $w\in W$ and $w'\in W^{\perp}$.
Choose an orthonormal basis $\left\{w_1,\dots , w_k\right\}$ of $W$ and extend to an orthonormal basis $\left\{w_1,\dots,w_k,v_{k+1},\dots ,v_n\right\}$ of $V$. By definition $v_i\in W^{\perp}$ for all $n\geq i\geq k+1$. Hence any $v\in V$ can be decomposed as we needed to show.


Q:

Combine dataframe excluding redundant columns in R

I have two dataframes, one with 56 528 obs. for 18 variables, and one with 56 528 obs. for 45 variables. The observations are in the same 'order' since those dataframe are a subset of an original dataset. 8 variables of my first dataframe are  absent in my second dataframe, and I want to add the columns from the first dataframe that are missing in the second, without having to write the names of these columns one by one. I would like a generalizable function.
I read that merge (dataframe1, dataframe2) is supposed to do the job. In my case, it gives me a dataframe with 1 372 978 obs. for 53 variables. The number of variables looks ok, but I don't know what happened with the rows... Any recommandations ?
Here an example :
data(nhanes) #25 obs. 4 var.

i<-subset(nhanes,select=c(bmi,hyp)) #df1 with two variables from nhanes, 25 obs. 2 var.

nhanes_m<-subset(nhanes, select=c(age,bmi,chl)) #df2 with three variables from nhanes, 25 obs. 3 var.

Like in my example, here we have two dataframe, with the same number of observations, in the same order, but not exactly the same columns. If I want to combine df1 and df2, so that I obtain again 25 obs. for 4 variables (the redundant columns are excluded, we just 'bind' the columns that are unique between df1 and df2 to one or the other dataframe), how should I do ?
nhanes_f<-merge(nhanes_m,i)  #gives 97 obs. 4 variables, instead of 25 obs. 4 var.

A:

Presumably the names of the columns that the two data frames share will be the same, so just find the names that don't match, subset the data frame by them and cbind them together. Here's a reprex:
(df1 <- data.frame(a = 1:5, b = 11:15, c = 21:25, d = 31:35))
#>   a  b  c  d
#> 1 1 11 21 31
#> 2 2 12 22 32
#> 3 3 13 23 33
#> 4 4 14 24 34
#> 5 5 15 25 35

(df2 <- data.frame(a = 1:5, b = 11:15, e = 21:25, f = 31:35))
#>   a  b  e  f
#> 1 1 11 21 31
#> 2 2 12 22 32
#> 3 3 13 23 33
#> 4 4 14 24 34
#> 5 5 15 25 35

cbind(df1, df2[!(names(df2) %in% names(df1))])
#>   a  b  c  d  e  f
#> 1 1 11 21 31 21 31
#> 2 2 12 22 32 22 32
#> 3 3 13 23 33 23 33
#> 4 4 14 24 34 24 34
#> 5 5 15 25 35 25 35

Created on 2020-06-22 by the reprex package (v0.3.0)


Q:

Serious Problems with Speed and Lag continually worsening in 14.04

I recently installed 14.04 on a clean HD. 64-bit unity. Since the computer isn't mine the specs I don't currently have on hand, but I can tell you that it shouldn't be a big problem.
The problem(s) that I have been experiencing are quite difficult to troubleshoot for, since what I am experiencing is not to my knowledge an explicit error, but rather a usability problem that worsens with each boot.
Take this particular time that I booted my computer. It took 10 minutes just to get past a blank screen with a cursor. Then I saw a second black screen, with a different font, for another 5 minutes. Then I saw the default 14.04 background. Then I waited another 10 minutes to see the login, then it crashed. So i went through the process again. You get the picture, right? Booting up takes half an hour or more.
Every other action that I want to perform or program that i want to load is similarly tedious. When I open terminal it takes about 60 seconds to see the username and computer name. My browser freezes about every 20 seconds... etc.
When I installed this new version about 2 weeks ago, the problem was slight and hardly noticable. I did my graphic design work without the slightest wink of difficulty. It has since then started causing problems and continued to worsen and now my computer is hardly usable.
Is there any way to discover what this might be? Since the problem presents itself upon first booting the computer, I doubt it has anything to do with mal-ware,spy-ware,viruses, etc. Perhaps I have the wrong install or maybe the problem is hard-ware related. How can I proceed? Should I just wipe and try a different version?
Thanks,
An Ubunt00n00b

A:

If I understand correctly the only OS on the machine is Ubuntu? It may be a driver issue, some bug or a hundred other things. Depending on how new your computer is(newer not always better) , try installing 12.04 on it, and see if the problem persists they are virtually identical in UI. System specs may come in useful, it may be some weird driver bug.


Q:

Binomial of Mersenne prime power.

Let $f(x)$ be irreducible over $\mathbb{Z}_2$ of degree $p$, where $p$ is prime. Let $2^p-1$ be a Mersenne prime. I have to show
  \begin{equation*}
f(x) \mid x^{2^p-1}-1.
\end{equation*}

I am really not sure where to go with this. So far, since $2^p-1$ is prime, I have that $x^{2^p-1}-1$, in $\mathbb{Z}_2$, splits into:
$$x^{2^p-1}-1=(x-1)(x^{2^p-2}+x^{2^p-3}+\cdots+1)=(x+1)(x^{2^p-2}+x^{2^p-3}+\cdots+1)$$
i.e. it splits into $x-1$ and the respective cyclotomic polynomial.
But the cyclotomic polynomials are irreducible, so I don't know how I can show that $f(x)$ can divide this.

A:

Let $f\in\mathbb F_2[X]$ be an irreducible polynomial of degree $n$.
Let $q=2^n$. Then $f$ has a root $\alpha$ in $\mathbb F_2[X]/(f)\cong \mathbb F_q$. As $\mathbb F_q^\times$ is cyclic of order $q-1$, we have $\alpha^{q-1}=1$, i.e., $\alpha$ is also a root of $X^{q-1}-1$ and so $\gcd(f,X^{q-1}-1)\ne 1$. As $f$ is irreducible, we conclude $f\mid X^{q-1}-1$.
Your problem is a special case of this (namely when $n=p$ and $q-1=2^n-1$ are prime).


Q:

Check Database Integrity fails

My nightly maintenance plans are failing for two databases. I'm not even sure what this means.  Any ideas on how to fix?
Here's the error I have from the maintenance plan:
Executing the query "DBCC CHECKDB(N'Site_DB')  WITH NO_INFOMSGS
" failed with the following error: "The Index Allocation Map (IAM) page (0:0) is pointed to by the previous pointer of IAM page (1:747) in object ID 0, index ID -1, partition ID 0, alloc unit ID 72057597046816768 (type Unknown), but it was not detected in the scan.
CHECKDB found 1 allocation errors and 0 consistency errors not associated with any single object.
CHECKDB found 1 allocation errors and 0 consistency errors in database 'Site_DB'.
repair_allow_data_loss is the minimum repair level for the errors found by DBCC CHECKDB (Site_DB).". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.

The interesting part is that 2 different databases are reporting this with the exact same page id.  Not sure why that is.

A:

I ran across an article which gave some hints on how to get to the issue.
So I ran the following:
DBCC TRACEON(3604,-1)
GO
DBCC PAGE('Site_DB', 1, 747, 3)

The Metadata: IndexId value result was -1.  The article said if it was greater than 1 then drop and recreate the non-clustered index.  If it was 0 or 1 then restore from backup or try the repair.  Unfortunately, neither of those situations cover a -1 value...
So I went ahead and ran
DBCC CHECKDB('Site_DB', REPAIR_ALLOW_DATA_LOSS) WITH NO_INFOMSGS, ALL_ERRORMSGS

This came back and said if found the allocation errors and fixed them.  Just to be sure I ran DBCC CHECKDB('SiteDB') WITH NO_INFOMSGS once more and it came back clear.


Q:

Trying to send email to list of users

I'm trying to set up a thing to send an email to users who have their Subscribed set to true. I think I'm getting the users, but the email address is something I'm working on.
I tried this to get the addresses, but it's not working:
var subbed = db.Users.Where(u => u.Subscribed == true);

var emailFrom = WebConfigurationManager.AppSettings["emailfrom"];

var emailTo = subbed.Select(s => s.Email).ToString();

MailMessage mailMessage = new MailMessage(emailFrom, emailTo)
{
    Subject = "This Is The Subject",
    IsBodyHtml = true,
    Body = "This is the body"
};

The emailTo variable seems to be the problem from what I can tell in the debugger.

A:

emailTo is currently calling the ToString method of your collection, which is probably not what you want. If you want a delimited list of email addresses, you might do something like this:
var emailTo = string.Join(";", subbed.Select(s => s.Email))
// example output: "bob@example.com;jane@example.com"


Q:

Unable to send message from context menu to content script

I want to send a message to the content script, when the user selects a context menu item. The content script will then, based on the message, format the webpage as required.
I was unable to do so and I have been able to locate the problem to the code responsible for sending the message (in the context menu script) or the one responsible for receiving message (in the content script).
Below, I try to present a simplified version of the code that tries to duplicate the problem:
manifest.json
{
    "manifest_version": 2,
    "name": "MCVE for SO",
    "version": "1",

    "description": "Demonstrate message passing from context menu to content script",

    "author": "Nityesh Agarwal",

    "permissions": [
        "tabs",
        "activeTab",
        "contextMenus"
    ],

    "content_scripts": [
        {
            "matches": ["<all_urls>"],
            "js": ["markr.js"]
        }
    ],

    "background": {
        "scripts": ["backgroundContextMenus.js"]
    }
}

backgroundContextMenus.js:
chrome.runtime.onInstalled.addListener(function(){
    chrome.contextMenus.create({"title": "Testing",
                                "contexts": ["selection"],
                                "id": "testing"});
});
chrome.contextMenus.onClicked.addListener(function(info, tab){
    console.log("testing..");
    console.log("Before sending the bold message");
    chrome.tabs.sendMessage(tab.id, {changeParameter: "bold"});
    console.log("After sending the bold message");
});

markr.js:
chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse){
        console.log("Text toggled bold");
    });

Upon selecting the context menu item, the console shows these messages:

backgroundContextMenus.js:8 testing..
  backgroundContextMenus.js:9 Before sending the bold message
  backgroundContextMenus.js:11 After sending the bold message

So, as you may notice, there is no log saying something like

Text toggled bold

This means that the code inside the markr.js file isn't getting executed. Therefore, I suspect that there must be something wrong with the code responsible for sending message: chrome.tabs.sendMessage(tabs[0].id, {changeParameter: "bold"});)
Here's another code snippet which I tried to duplicate but it gave the same problem - https://stackoverflow.com/a/14473739/7082018

I am unable to figure out what exactly is wrong with it. It would be of great help if someone could help tell me how I might be able to successfully pass messages between the context menu and the content page.

A:

Taking a cue from this answer I modified my backgroundContextMenus.js as follows:
function ensureSendMessage(tabId, message, callback){
  chrome.tabs.sendMessage(tabId, {ping: true}, function(response){
    if(response && response.pong) { // Content script ready
      chrome.tabs.sendMessage(tabId, message, callback);
    } else { // No listener on the other end
      console.log("Injecting script programmatically");
      chrome.tabs.executeScript(tabId, {file: "markr.js"}, function(){
        if(chrome.runtime.lastError) {
          console.error(chrome.runtime.lastError);
          throw Error("Unable to inject script into tab " + tabId);
        }
        // OK, now it's injected and ready
        console.log("Sending msg now");
        chrome.tabs.sendMessage(tabId, message, callback);
      });
    }
  });
}

chrome.runtime.onInstalled.addListener(function(){
    chrome.contextMenus.create({"title": "Testing",
                                "contexts": ["selection"],
                                "id": "testing"});
});

chrome.contextMenus.onClicked.addListener(function(info, tab){
    ensureSendMessage(tab.id, {greeting: "hello"});
});

Then I modified the markr.js as follows:
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
    if(request.ping){
        sendResponse({pong: true});
        return;
    }
    console.log("Text toggled bold");
});

Now the console logs are exactly what one may expect:
Console log:

markr.js:11 Text toggled bold

Remember that this log is present console log of the webpage's devtools and not the background script's inspect views.


Q:

O que é significa a linha questao += 1 neste código?

Alguém pode me explicar a função da linha questao +=1?
Sou iniciante e estou tentando entender a lógica por trás do código e não apenas fazer funcionar.
 pontos = 0 questao = 1 while questao <=3:
     resposta = input('Resposta da questão {}: '.format(questao))
     if questao == 1 and resposta == 'b':
         pontos = pontos + 1
     if questao == 2 and resposta == 'a':
         pontos = pontos + 1
     if questao == 3 and resposta == 'd':
         pontos = pontos + 1
     questao +=1 print('O aluno fez {} pontos'.format(pontos))

A:

Em resumo, o operador += soma o valor seguinte e adiciona à variável anterior. No caso:
questao +=1

é (mais ou menos)* equivalente à
questao = questao + 1

*Não é exatamente equivalente, como @Jefferson Quesado apontou, uma vez que o sinal += em python substitui o método especial iadd. Como @hkotsubo sugeriu, para uma explicação mais detalhada da diferença entre as duas formas, veja esta resposta.
Nesse caso, ao executar a linha "questao += 1", a variável questao recebe seu próprio valor atual somado de 1.
Como citado pelo @Luiz Augusto, mais detalhes podem ser consultados aqui.
Para entender o código por completo, é preciso analisar linha a linha:
#Variável pontos possui valor 0, questao possui valor 1
pontos = 0
questao = 1

#A instrução while indica que o bloco de código seguinte será executado enquanto questao <= 3.
#Ou seja, cada linha abaixo será percorrida e, ao chegar ao final, será analisado se o valor contido na variável questao é menor ou igual a 3.
#Na primeira vez que esse código é executado, a variável questao possui valor igual a 1, portanto o bloco de código é executado
while questao <=3:

    #A variável resposta recebe o valor do "input".
    #O "input" apresenta ao usuário a frase: 'Resposta da questão {}:', onde {} é substituído pelo valor contido na variável questao.
    #Logo, na primeira execução, "input" apresenta ao usuário a seguinte frase: Resposta da questão 1
    #Após apresentar a frase, "input" aguarda que o usuário digite um valor e aperte enter.
    #O valor digitado é armazenado na variável resposta
    resposta = input('Resposta da questão {}: '.format(questao))

    # O if analisa 2 condições: se o valor da variável questao é igual a 1 e se o valor da variável resposta é igual a b.
    # Caso as duas condições sejam verdadeiras, ele altera o valor da variável pontos para pontos + 1.
    # Caso alguma das condições seja falsa, ele ignora a próxima linha e pula para o próximo if.
    # Detalhe: pontos = pontos + 1 poderia ser reescrito como pontos += 1
    if questao == 1 and resposta == 'b':
        pontos = pontos + 1

    # Aqui o if analisa 2 condições: se o valor da variável questao é igual a 2 e se o valor da variável resposta é igual a a.
    # Caso as duas condições sejam verdadeiras, ele altera o valor da variável pontos para pontos + 1.
    # Caso alguma das condições seja falsa, ele ignora a próxima linha e pula para o próximo if.
    if questao == 2 and resposta == 'a':
        pontos = pontos + 1

    # Aqui o if analisa 2 condições: se o valor da variável questao é igual a 3 e se o valor da variável resposta é igual a d.
    # Caso as duas condições sejam verdadeiras, ele altera o valor da variável pontos para pontos + 1.
    # Caso alguma das condições seja falsa, ele ignora a próxima linha e pula para a linha seguinte.
    if questao == 3 and resposta == 'd':
        pontos = pontos + 1

    # Aqui ele altera o valor da variável questao.
    # Na primeira execução ela irá mudar de 1 para 2.
    # Na segunda execução ela irá mudar de 2 para 3.
    # Na terceira execução ela irá mudar de 3 para 4.
    # Não haverá a quarta execução, pois o while irá notar que o valor da variável questao (3) é não é menor ou igual a 3
    questao +=1

#Por fim, após executar o código dentro do while 3 vezes, ele irá imprimir a frase seguinte, substituindo {} pelo valor contido na variável pontos.
#O valor na variável pontos irá depender dos valores que o usuário digita no input
print('O aluno fez {} pontos'.format(pontos))


Q:

show the result of type erasure in java

Is there a way to display type erasure in java? I mean to see how code will look like after type erasure?
For example to show that
List<? extends Number> a; //before
List a; //after

or
class Node<T extends Number> {
   private T variable;
}

become
 class Node {
     private Number variable;
 }

and etc.

A:

Yes. Just compile the code and use javap:
javap -c com.mypackage.MyClass
Or you can use this plugin for Eclipse: http://asm.ow2.org/eclipse/index.html


Q:

How to implement correct work of generator without changing entry function?

From some component, I will send a string with a specific name and will return some set of data  (if this name in object key). If object property contains an array with few objects with the same structure but different values, I want to send on every call next object.
But when I try to do that I got the same value every time:
{ value: { a: 1 }, done: false }
{ value: { a: 1 }, done: false }
{ value: { a: 1 }, done: false }

instead of:
{ value: { a: 1 }, done: false }
{ value: { a: 2 }, done: false }
{ value: { a: 3 }, done: true}

So how to get the expected result which I described above without changing this part of code: (and names of the object "mocks" properties  (foo, bar, baz))
for (let i = 0; i < 3; i++) {
  foo.Query('c');
}

All code which you can run and see the result

const test = {
  a: { id: 1 },
  b: { id: 2 },
  c: [{ a: 1 }, { a: 2 }, { c: 3 }]
};

function* generLoop(elem) {
  // eslint-disable-next-line no-undef,no-restricted-syntax
  for (el of test[elem]) {
    // eslint-disable-next-line no-undef
    yield el;
  }
}

function createLoop(elem) {
  let gen;
  if (elem === 'c') {
    gen = generLoop(elem);
  }
  console.log(gen.next());
  return () => (gen ? gen.next() : test[elem]);
}

const iterators = {};

const mocks= {
  Query: elem => ({
    foo: test.a,
    bar: test.b,
    baz: createLoop(elem)
  })
};

for (let i = 0; i < 3; i++) {
  mocks.Query('c');
}

A:

Change Query so that, when an item is queried, it looks up the current iterator for that item, if it exists (and creates a new iterator if not). Then log the result of calling the iterator inside of Query (not inside of createLoop, whose job it is to just create the iterator):

const test = {
  a: {
    id: 1
  },
  b: {
    id: 2
  },
  c: [{
    a: 1
  }, {
    a: 2
  }, {
    c: 3
  }]
};

function* generLoop(elem) {
  // eslint-disable-next-line no-undef,no-restricted-syntax
  for (el of test[elem]) {
    // eslint-disable-next-line no-undef
    yield el;
  }
}

let gen;

function createLoop(elem) {
  if (elem === 'c') {
    gen = generLoop(elem);
  }
  return () => (gen ? gen.next() : test[elem]);
}
const iterators = {};
const foo = {
  Query: elem => {
    iterators[elem] = iterators[elem] || createLoop(elem);
    console.log(iterators[elem]());
  }
};

for (let i = 0; i < 3; i++) {
  foo.Query('c');
}

Or, if the iterator-caching functionality has to be done in the strangely-named createLoop function (said function would be more properly named iterateLoop or something, since the implementation requires it to only iterate, but not necessarily create the iterator each time it's called):

const test = {
  a: {
    id: 1
  },
  b: {
    id: 2
  },
  c: [{
    a: 1
  }, {
    a: 2
  }, {
    c: 3
  }]
};

function* generLoop(elem) {
  // eslint-disable-next-line no-undef,no-restricted-syntax
  for (el of test[elem]) {
    // eslint-disable-next-line no-undef
    yield el;
  }
}

let gen;

function createLoop(elem) {
  if (!gen) {
    gen = generLoop(elem);
  }
  return gen.next();
}
const mocks= {
  Query: elem => ({
    foo: test.a,
    bar: test.b,
    baz: createLoop(elem)
  })
};

for (let i = 0; i < 3; i++) {
  console.log(mocks.Query('c').baz);
}


Q:

Perform UPDATE without SELECT in eclipselink with Spring Data

I am using Spring Data(ver1.9) - JPA - EclipseLink(2.6.1) - Apache Tomcat. I want to update an entity object without checking for its existence.
Example Entity,
@Entity
@Table(schema = "customSchema")
public class Person implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(nullable = false)
    private Integer id;

    @Column(nullable = false, length = 60)
    private String firstName;

    public Person() {
        this.id = null;
        this.firstName = "";
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

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

Example Spring Data Repository,
@Repository
public interface PersonRepository extends JpaRepository<Person, Integer> {
}

The code that i execute and the sql that is generated,
int id = 5; // This id is existing in my database.
Person Person = personRepository.findOne(id);
// SQL: One SELECT is executed. This is normal.

person.setFirstName("aNewFirstName");

personRepository.saveAndFlush(person);
// SQL: One SELECT is executed and then an update is executed.

The persistence.xml, (layer 2 cache is disabled for thread safety, it can be enabled selectively using @Cacheable annotation)
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="my_peristenceUnit" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>package.to.entity.Person</class>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <shared-cache-mode>NONE</shared-cache-mode>
  </persistence-unit>
</persistence>

My problem is existing in the saveAndFlush command. EclipseLink is checking the fields from the table row to determine which of them have changed using a SELECT sql command. This helps to create an update command that contains only the edited fields.
The same happens with the delete command!
Question
Is there any way to avoid the select sql execution of the update(saveAndFlush) or delete(delete) commands?
What to do if i prefer to update the whole row of the table? Without checking which fields have changed?
I have seen another SO question similar to this but this solution is not working for my example. I used the @ExistenceChecking(value = ExistenceType.ASSUME_EXISTENCE) annotation for the Person Entity and nothing changed when the saveAndFlush command was executed.

A:

EclipseLink has two different cache levels.
The first one is the Persistence Unit Cache. This is a shared cache (L2) or Layer 2 cache. This cache is the one you disabled,
<shared-cache-mode>NONE</shared-cache-mode>

Even if you disable it you can use it explicitly using the @Cacheable annotation.
The second is the Persistence Context Cache. This is an isolated cache (L1) that services operations within an EntityManager. This cache level is existing in your application. This cache is the key to avoid this behavior from the EclipseLink provider.
You call two different times your repository methods. To make use of the L1 cache this calls have to use the same EntityManager. In your case you are using different EntityManagers. Below an example to use the same EntityManger for your two different repository calls.
@Service
public class MyEnityService {

    @Transactional
    public Boolean create() {
        int id = 5; // This id is existing in my database.
        Person Person = personRepository.findOne(id);
        // SQL: One SELECT is executed. This is normal AND CACHED FROM YOUR ENTITY MANAGER.

        person.setFirstName("aNewFirstName");

        personRepository.saveAndFlush(person);
        // SQL: ONLY ONE update is executed. NOTHING ELSE.

        return true;
    }
}

Be carefull, the @Transactional annotation is not working on a Controller method. You have to use a service for this.


Q:

Why spring-data-gemfire reposories do not use putAll?

I noticed that spring-data-gemfire repositories are extremely slow in client-server architecture when it comes to uploading a big data set into gemfire cache.
I checked the sources and I see that
save(Iterable<U> entities)

just iterates through all the entries and puts them individually.
Why doesn't it use Region.putAll? - it would be a lot faster.

A:

Indeed, you are correct.  This predates me, but I will implement this change.  See https://jira.spring.io/browse/SGF-343 for further details.
We do have 1 more scheduled 1.4 SDG release (1.4.5 for SR5) in Spring Data Dijkstra release train (https://github.com/spring-projects/spring-data-commons/wiki/Release-Train-Dijkstra).  This change will also be targeted at the SDG 1.5.1 (Evans SR1) release and will obviously be included in the upcoming 1.6 M1 (Fowler) release.
If you are not using 1 of these SDG versions, then I encourage you to upgrade as I will not back port this change to 1.3.x or earlier.
Cheers!


Q:

How to uninstall Java 9 on macOS Sierra

I'm having trouble removing Java 9 from my macOS Sierra system.
I accidentally installed Java 9, instead of Java 8, so now all my Eclipse projects give me these warnings: Build path specifies execution environment JavaSE-1.8. There are no JREs installed in the workspace that are strictly compatible with this environment.
I have used the following guide, but it did not work. Here is my command output when running java -version from the command line:
java version "9"
Java(TM) SE Runtime Environment (build 9+181)
Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)

A:

for macOS high sierra removing java 9:
delete the java folder you find in the dir shown to you after executing:
/usr/libexec/java_home -V
use "Go to" in finder and copy and paste the dir to get there

A:

There are two commands which are very simple and useful. If you want to keep multiple versions simply set JAVA_HOME to version you want to use.
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_(version you want to use).jdk/Contents/Home
Example--> export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home
Else in addition to above command remove/uninstall additional jdk version from your system with below command.
sudo rm -rf /Library/Java/JavaVirtualMachines/jdk-(version you want to remove).jdk/
Example --> sudo rm -rf /Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/
Please refer below github link for additional details -->
https://gist.github.com/schnell18/bcb9833f725be22f6acd01f94b486392
Thanks

A:

On MacOS you can list what JDKs you have installed and where they are installed to with:
/usr/libexec/java_home -V

If you want to select one of the installed JDKs to be used as default, you can do:
/usr/libexec/java_home -v 1.8

Verify which is now default with java -version.
You can manually add the location of other installed JREs in Eclipse via Preferences / Java / Installed JREs , press Add, and then point it to one of the locations should with the -V option above. You can then check it to select which is default for your projects.


Q:

ios view coordinate system

I've just started iOS drawing programming and I found out that the coordinate system is different from Mac OS X, basically the point of origin on iOS is on the top left instead of the bottom left as on the mac. Just wondering if anyone knows why Apple made this change, and will it change on the future for Mac as well? I also saw on the docs that the coordinate system on iOS cannot be flipped, so I need to rewrite the code?

A:

You are correct about the coordinate system. The reason, if I recall correctly, is that most platforms' drawing systems use upper-left as the coordinate origin. Mac OS X used lower-left because that's what NeXT used.
It probably won't change on the Mac in the future because then everyone would have to totally rewrite their drawing code.


Q:

TYPO3 4.5: Is the TCA supposed to show up in "Plugin Options"?

I'm in the process of creating my very first extension in Typo3 (version 4.5.22).  I'm using Dmitry Dulepov's "TYPO3 Extension Development" book as a guide (a very useful reference).  I used Kickstarter to get things going, and one of the things it generated was a $TCA that goes along with my own table in the database.
Now, my understanding is that the TCA determines what options appear in the backend for my plugin, under "Plugin Options," when you create a new "Insert Plugin" content item.  The problem is, this section remains blank.  When I choose my plugin, there are no options displayed.
I was able to build a Flexform and get it to show up under "Plugin Options," but an extremely important component of my plugin is the ability to use the "suggest" form to search for records, and from what I've read, it's not possible to get those to work properly using Flexforms.
Am I missing a simple function in ext_tables to get my TCA to show up in the "Plugin Options" section?  Sorry if I'm completely off-base here; the sheer complexity of Typo3 has left me in a bit of a daze.
Here's my ext_tables.php, which was generated by Kickstarter:
<?php
if (!defined('TYPO3_MODE')) {
    die ('Access denied.');
}

t3lib_extMgm::allowTableOnStandardPages('tx_newsbuilder_newsitems');

t3lib_extMgm::addToInsertRecords('tx_newsbuilder_newsitems');

$TCA['tx_newsbuilder_newsitems'] = array(
    'ctrl' => array(
        'title'     => 'LLL:EXT:newsbuilder/locallang_db.xml:tx_newsbuilder_newsitems',
        'label'     => 'uid',
        'tstamp'    => 'tstamp',
        'crdate'    => 'crdate',
        'cruser_id' => 'cruser_id',
        'default_sortby' => 'ORDER BY crdate',
        'delete' => 'deleted',
        'enablecolumns' => array(
            'disabled' => 'hidden',
        ),
        'dynamicConfigFile' => t3lib_extMgm::extPath($_EXTKEY) . 'tca.php',
        'iconfile'          => t3lib_extMgm::extRelPath($_EXTKEY) . 'icon_tx_newsbuilder_newsitems.gif',
    ),
);

t3lib_div::loadTCA('tt_content');
$TCA['tt_content']['types']['list']['subtypes_excludelist'][$_EXTKEY.'_pi1'] = 'layout,select_key';

t3lib_extMgm::addPlugin(array(
    'LLL:EXT:newsbuilder/locallang_db.xml:tt_content.list_type_pi1',
    $_EXTKEY . '_pi1',
    t3lib_extMgm::extRelPath($_EXTKEY) . 'ext_icon.gif'
),'list_type');

if (TYPO3_MODE === 'BE') {
    $TBE_MODULES_EXT['xMOD_db_new_content_el']['addElClasses']['tx_newsbuilder_pi1_wizicon'] = t3lib_extMgm::extPath($_EXTKEY) . 'pi1/class.tx_newsbuilder_pi1_wizicon.php';
}

// Flexform
//$TCA['tt_content']['types']['list']['subtypes_addlist'] [$_EXTKEY . '_pi1'] = 'pi_flexform';
//t3lib_extMgm::addPiFlexFormValue($_EXTKEY . '_pi1', 'FILE:EXT:' . $_EXTKEY . '/pi1/flexform_ds.xml');

?>

Here, additionally, is my tca.php:
<?php
if (!defined('TYPO3_MODE')) {
    die ('Access denied.');
}

$TCA['tx_newsbuilder_newsitems'] = array(
    'ctrl' => $TCA['tx_newsbuilder_newsitems']['ctrl'],
    'interface' => array(
        'showRecordFieldList' => 'hidden,tt_news_uid,singleview_uid,fishback_uid,fishback_url,headline,subheading,link'
    ),
    'feInterface' => $TCA['tx_newsbuilder_newsitems']['feInterface'],
    'columns' => array(
        'hidden' => array(
            'exclude' => 1,
            'label'   => 'LLL:EXT:lang/locallang_general.xml:LGL.hidden',
            'config'  => array(
                'type'    => 'check',
                'default' => '0'
            )
        ),
        'tt_news_uid' => array(
            'exclude' => 0,
            'label' => 'LLL:EXT:newsbuilder/locallang_db.xml:tx_newsbuilder_newsitems.tt_news_uid',
            'config' => array(
                'type' => 'group',
                'internal_type' => 'db',
                'allowed' => 'NO_TABLE_NAME_AVAILABLE',
                'size' => 1,
                'minitems' => 0,
                'maxitems' => 1,
            )
        ),
        'singleview_uid' => array(
            'exclude' => 0,
            'label' => 'LLL:EXT:newsbuilder/locallang_db.xml:tx_newsbuilder_newsitems.singleview_uid',
            'config' => array(
                'type' => 'group',
                'internal_type' => 'db',
                'allowed' => 'tt_news',
                'size' => 1,
                'minitems' => 0,
                'maxitems' => 1,
            )
        ),
        'fishback_uid' => array(
            'exclude' => 0,
            'label' => 'LLL:EXT:newsbuilder/locallang_db.xml:tx_newsbuilder_newsitems.fishback_uid',
            'config' => array(
                'type' => 'group',
                'internal_type' => 'db',
                'allowed' => 'pages',
                'size' => 1,
                'minitems' => 0,
                'maxitems' => 1,
            )
        ),
        'fishback_url' => array(
            'exclude' => 0,
            'label' => 'LLL:EXT:newsbuilder/locallang_db.xml:tx_newsbuilder_newsitems.fishback_url',
            'config' => array(
                'type'     => 'input',
                'size'     => '15',
                'max'      => '255',
                'checkbox' => '',
                'eval'     => 'trim',
                'wizards'  => array(
                    '_PADDING' => 2,
                    'link'     => array(
                        'type'         => 'popup',
                        'title'        => 'Link',
                        'icon'         => 'link_popup.gif',
                        'script'       => 'browse_links.php?mode=wizard',
                        'JSopenParams' => 'height=300,width=500,status=0,menubar=0,scrollbars=1'
                    )
                )
            )
        ),
        'headline' => array(
            'exclude' => 0,
            'label' => 'LLL:EXT:newsbuilder/locallang_db.xml:tx_newsbuilder_newsitems.headline',
            'config' => array(
                'type' => 'text',
                'cols' => '48',
                'rows' => '1',
            )
        ),
        'subheading' => array(
            'exclude' => 0,
            'label' => 'LLL:EXT:newsbuilder/locallang_db.xml:tx_newsbuilder_newsitems.subheading',
            'config' => array(
                'type' => 'text',
                'cols' => '48',
                'rows' => '1',
            )
        ),
        'link' => array(
            'exclude' => 0,
            'label' => 'LLL:EXT:newsbuilder/locallang_db.xml:tx_newsbuilder_newsitems.link',
            'config' => array(
                'type'     => 'input',
                'size'     => '15',
                'max'      => '255',
                'checkbox' => '',
                'eval'     => 'trim',
                'wizards'  => array(
                    '_PADDING' => 2,
                    'link'     => array(
                        'type'         => 'popup',
                        'title'        => 'Link',
                        'icon'         => 'link_popup.gif',
                        'script'       => 'browse_links.php?mode=wizard',
                        'JSopenParams' => 'height=300,width=500,status=0,menubar=0,scrollbars=1'
                    )
                )
            )
        ),
    ),
    'types' => array(
        '0' => array('showitem' => 'hidden;;1;;1-1-1, tt_news_uid, singleview_uid, fishback_uid, fishback_url, headline, subheading, link')
    ),
    'palettes' => array(
        '1' => array('showitem' => '')
    )
);
?>

A:

TCA is Table Configuration Array. It configures appearance of records from db tables in your TYPO3 BackEnd in Web -> List module. So, if you go to this module and create new entry for tx_newsbuilder_newsitems, you'll see your record create/edit form, as configured in tca.php and ext_tables.php.
Your plugin, inserted on a page, is a usual Content Element, saved in tt_content table, so it is shown, like defined in TCA of tt_content. But this table has configured pi_flexform field, which is of type flex and can contain flexible data inside, which FlexForm actually is. For this purpose you need to create a proper XML file for your FlexForm and instruct TYPO3 to use it.
In TYPO3 wiki you can find detailed information about FlexForms usage.
P.S. It should be possible to use suggest wizard in FlexForm, because FF notation is almost same as TCA notation and all the wizards should be available.


Q:

how to set cell width in javascript just after creating it with 'document.createElement()' method?

I've created cells for a dynamic table in html page using "document.createElement('td')" method & now wish to set width of each individual cell with some different value.
Tried "cell.width = width" but it didn't work.
how can I achieve it?

A:

Use style.width:
var cell = document.createElement('td');
parent.appendChild(cell);
cell.style.width = '200px';

Here is an example.

A:

To post my comment as an answer:
Use cell.style.width = width;.


Q:

Run python script on Google Cloud Compute Engine

I know this is an exact copy of this question, but I've been trying different solutions for a while and didn't come up with anything.
I have this simple script that uses PRAW to find posts on Reddit. It takes a while, so I need it to stay alive when I log out of the shell as well.
I tried to set it up as a start-up script, to use nohup in order to run it in the background, but none of this worked. I followed the quickstart and I can get the hello word app to run, but all these examples are for web applications and all I want is start a process on my VM and keep it running when I'm not connected, without using .yaml configuration files and such. Can somebody please point me in the right direction?

A:

Well, at the end using nohup was the answer. I'm new to the GNU environment and I just assumed it didn't work when I first tried. My program was exiting with an error, but I didn't check the nohup.out file so I was unaware of it..
Anyway here is a detailed guide for future reference (Using Debian Stretch):

Make your script an executable
chmod +x myscript.py

Run the nohup command to execute the script in the background. The & option ensures that the process stays alive after exiting. I've added the shebang line to my python script so there's no need to call python here
nohup /path/to/script/myscript.py &

Logout from the shell if you want
logout

Done! Now your script is up and running. You can login back and make sure that your process is still alive by checking the output of this command:
ps -e | grep myscript.py


Q:

(PHP) file_get_contents with a Authorization Header

How do I use file_get_contents to send a authorization header
I currently have file_get_contents get it with a user agent but I also would like to add a authorization header so I get a functional response instead of a 401 error
$options = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.5 Safari/605.1.15\r\n"
  )
);

$context = stream_context_create($options);
$url = file_get_contents("http://websitethatneedsaauthorizationheader.com", false, $context);

I basically need to copy the authorization header from here:

and then use that on this website and not get a 401 error

this is what it is supposed to look like with the authorization header

A:

After the \n of the User-Agent header, add the autorization header, like this:
$options = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"User-Agent: SomeUser\r\nAuthorization: SomeKey"
  )
);


Q:

FastColoredTextbox issue scrolling the text and positioning the caret

This question is about the extended control: FastColoredTextbox here: http://www.codeproject.com/Articles/161871/Fast-Colored-TextBox-for-syntax-highlighting
I'm trying to auto scroll down the text and to put the text-cursor in the last character of the appended text (the total length of the text property I suppose).

Text scrolling problem

This is working for the automatic text scrolling:
  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        FastColoredTextBox1.Text = str
        FastColoredTextBox1.ScrollLeft()
        FastColoredTextBox1.Navigate(FastColoredTextBox1.Lines.Count - 1)
  End Sub

...But I want to make it more generic, and this don't work:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    FastColoredTextBox1.Text = str
End Sub

Private Sub FastColoredTextBox1_TextChanged(sender As Object, e As FastColoredTextBoxNS.TextChangedEventArgs) Handles FastColoredTextBox1.TextChanged
    sender.ScrollLeft()
    sender.Navigate(FastColoredTextBox1.Lines.Count - 1)
End Sub

Any error or exception, just don't will work, the text is not scrolled like in the first example.

Keyboard cursor position problem

About the text-cursor, I've tried this but don't work too:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    FastColoredTextBox1.Text = str
    FastColoredTextBox1.SelectionStart = FastColoredTextBox1.Text.Length
End Sub

Any error or exception, just don't work.

UPDATE

Tried the @ebyrob solution but does not work, it does not scroll to down.
Imports FastColoredTextBoxNS

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim str As String = String.Empty
        For x As Int32 = 1 To 1000 : str += vbNewLine & x : Next
        FastColoredTextBox1.Text = str
    End Sub

    Private Sub FastColoredTextBox1_TextChanged(sender As Object, e As TextChangedEventArgs) Handles FastColoredTextBox1.TextChanged
        GoEnd()
    End Sub

    Public Sub GoEnd()
        If FastColoredTextBox1.Lines.Count > 0 Then
            FastColoredTextBox1.Selection.Start = New Place(FastColoredTextBox1.Lines(FastColoredTextBox1.Lines.Count - 1).Count, FastColoredTextBox1.Lines.Count - 1)
        Else
            FastColoredTextBox1.Selection.Start = New Place(0, 0)
        End If
        FastColoredTextBox1.DoCaretVisible()
    End Sub

End Class

Update Two

The @ebyrob code works only if I call "GoEnd" always after appending the text, like this example:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim str As String = String.Empty
    For x As Int32 = 1 To 1000 : str += vbNewLine & x : Next
    FastColoredTextBox1.Text = str
    GoEnd()
End Sub

That is the same problem of my first problem that I've commented (TextChanged event is not working as expected), I need to make it efficient and generic, I was supposed the "TextChanged" event of the Control would be the correct event to put the "GoEnd()" but definitivelly is not.
I don't want to make this ever:
1. Add text
2. Call GoEnd
3. Add more text
4. Call again GoEnd

I wan't to put "GoEnd" in a event which succed when text is changed to simplify the things:
1. add text
2. add more text

UPDATE THREE

All the problems are solved, just changed the event name:
Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim mytext As String = String.Empty
        For x As Int32 = 1 To 1000 : mytext += vbNewLine & x : Next
        FastColoredTextBox1.Text += mytext
        FastColoredTextBox1.Focus()
    End Sub

Private Sub FastColoredTextBox1_TextChanged(sender As Object, e As FastColoredTextBoxNS.TextChangedEventArgs) _
    Handles FastColoredTextBox1.TextChangedDelayed

    sender.ScrollLeft()
    sender.Navigate(sender.Lines.Count - 1)
    FastColoredTextBox1.SelectionStart = FastColoredTextBox1.Text.Length

End Sub

End Class

A:

Apparently there is already a method just for this operation:
    public void GoEnd()
    {
        if (lines.Count > 0)
            Selection.Start = new Place(lines[lines.Count - 1].Count,
                                        lines.Count - 1);
        else
            Selection.Start = new Place(0, 0);

        DoCaretVisible();
    }

Note you'll also want:
Dim tbSender as FastColoredTextBox
tbSender = DirectCast(sender, FastColoredTextBox)
tbSender.Text = str
tbSender.GoEnd()
' ...

Finally I recommend:
Option Explicit

In just about all VB code.
EDIT: after testing the control, delayed event works
FastColoredTextBox1.TextChangedDelayed += FastColoredTextBox1_TextChanged;

Note: This means a default 100 ms delay for each event fired.


Q:

Cannot run php-gpio through apache and using exec() with sudo

On my Raspberry Pi Model B-Rev2 running Raspbian 3.10.25 I following the instructions on https://github.com/ronanguilloux/php-gpio to control the pins. But I simple cannot get it to work. According to instructions this should be the command in triggerMyScript.php:
exec('sudo -t /usr/bin/php ./myGpioScript');

But depending on content in myGpioScript I get errors in Apache log error.log saying stuff like command not found and No such file or directory.
I have also made additions to /etc/sudoers as instructed.
It works perfectly fine if I run php triggerMyScript.php from the command line.

A:

After spending several hours I found the solution. I needed to do the following things beside what the instructions on php-gpio says:

in myGpioScript the first line had to be #!/usr/bin/php instead of #!/usr/bin/env php
I had to add php after -t, like this: exec('sudo -t php /usr/bin/php ./myGpioScript'); (which makes sense when you think about it, but instructions doesn't say it like that)
I had to add this to the sudoers file: www-data ALL=NOPASSWD: /usr/bin/php - so that www-data can also run php without limitations. Specifying permission for the actual script files was actually not necessary!

File permissions on any of the files are not relevant either, so just leave them low.


Q:

Trick Joseki? Shoulder hit after keima to 3-4-stone

In a game I played, the following "Joseki" occurred:
I played the low keima against the 3-4-point, and Black responded with a shoulder hit. This move is not in the book by Takao Shinji.
In the game I attached under the 3-4 stone and created a living corner group, but Black's influence was tremendous. What should White do in this situation:
$$B
$$ ---------------------
$$ |..................|
$$ |..................|
$$ |..............1...|
$$ |..................|
$$ |...............2..|
$$ |..............3...|
$$ |..................|
$$ |..................|

A:

Black's pincer is too close to White's stone (it should be at least one space lower). (This variation is not in the joseki books because it is not joseki.) That means that White can "lean on it while indirectly attacking Black's corner stone.
White should play Q15,Black plays p15, White cuts with P14, threatening to make a ladder to capture stone 3, Black extends from this stone, White ataris with P16, and quashes the corner stone with P17.
$$B
$$ ---------------------
$$ |..................|
$$ |..................|
$$ |..............1...|
$$ |.............8....|
$$ |.............542..|
$$ |.............637..|
$$ |..................|
$$ |..................|


Q:

facebook sdk only public sharing

I have manage to share with Facebook sdk from my own app.
my question is,how can i get which privacy the user selected?
         Example: public / friends / only me

And if i can decide for the user which privacy?
thanks a lot!

A:

There is a privacy field in the Post API: https://developers.facebook.com/docs/graph-api/reference/v2.8/post
You can only set a privacy that includes less friends than the default privacy setting a user selected for your App:

This field cannot be used to set a more open privacy setting than the
  one granted.

Source: https://developers.facebook.com/docs/graph-api/reference/v2.8/user/feed#publish


Q:

Unable to create bundle on iTunes Connect for free app

I am trying to Create Bundle for uploading free iOS App to iTunes Connect. Create Button is disabled even after entering Name.

Create button is enabled only if I am adding existing Apps in Bundle which I am distributing free of cost.
I am facing this issue after Apple has updated Agreement.
Can anybody help me out to resolve this?
Thanks in Advance.

A:

You have to accept the new agreement.
Go into iTunes Connect, then agreements, tax and banking.
Click request on the two agreements at the top and agree to them.
Once you have agreed you will be instantly allowed access to create new apps.


Q:

Custom authentication adapter in ADFS as a primary step

We have a setup which involves a third party web application using ADFS as its access control server. That worked well for a while.
Now we need to install a custom Authentication Policy in ADFS, in order to delegate the authentication process to yet another custom, in-house built server and employ custom, dynamic rules.
From our understanding, this is a bird's eye view of the general ADFS authentication pipeline:

Basically, it's quite easy to augment the authentication process with a custom, secondary step but it's impossible to skip the Primary authentication sub process.
We did manage to implement a Multi Factor Authentication Adapter (basically a .NET assembly which subscribes to the ADFS conventions) and successfully install it within our ADFS:

Unfortunately, our situation requires us to customize the very first step and react differently to various scenarios. For instance:

maybe host a custom login form which validates credentials with the AD
maybe react to a custom-built, non ADFS OAuth access token
etc

Having the end user be forced to provide credentials from the get go is a deal breaker.
Does anyone have an idea about how this could be achieved?
We're using ADFS on Windows Server 2012 R2. This version of ADFS doesn't require IIS to be preinstalled.
We found an interesting article regarding ADFS 2.0 which is much older than what we're using and requires IIS. The article provides an example of how to alter the sign in page of what seems to be the primary authentication step:
https://social.technet.microsoft.com/wiki/contents/articles/1600.ad-fs-2-0-how-to-change-the-local-authentication-type.aspx
We didn't manage to replicate that example on our ADFS.

A:

As per @maweeras, you can't for ADFS 3.0 and greater.
ADFS 2.1 and below ran on IIS so you had access to the web site and could make changes in the code.
This lead to people introducing all kinds of security holes for which ADFS got blamed so it was locked down.
As per the suggestion, federate e.g. with identityserver which is open source and do the changes there.


Q:

How does QML handle app quitting from menu bar on Mac?

I have an application which runs both on Windows and Mac.
The application is written in QML.
The most outer component in my QML code is an ApplicationWindow.
The ApplicationWindow component's API has a property called menuBar.
Here is a dummy example, which reproduces this with a MenuBar and a quitting Action:
ApplicationWindow {
    id: _applicationWindow

    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    Component.onCompleted: {
        console.info("ApplicationWindow completed.")
    }

    menuBar: MenuBar {
        Menu {
            title: "File"

            MenuItem {
                action: Action {
                    id: _myQuttingAction

                    text: "Quit MyApp"
                    shortcut: "Ctrl+Q"

                    onTriggered: {
                        // deinit...
                        console.info("I got called!")
                        Qt.quit()
                    }
                }
            }
        }
    }
}

When I run my application on Mac the following top menu bar titles appear:
Apple Icon menu | MyApp | File
If I drop down the "File" menu this is what I get:

As you can see the drop down is empty. Actually it isn't empty because it is pressable and if I press it, it does quit my application with all deinit calls in the action's onTriggered event.
If I drop down the "MyApp" menu, at the bottom I see "Quit MyApp" with the shortcut Cmd+Q. If I press this, this quits my application as well, but without calling my action's onTriggered event, thus my real application crashes on quit.
Unfortunately pressing Cmd+Q also calls the quit from the "MyApp" not my specified action.
On Windows I don't have this problem at all and the "Quit MyApp" appears under the "File" menu and does what I expect it to do.
I tried to google this problem and read the documentation without luck yet. Anybody know what is happening here?

A:

It is not realistic to expect that Qt quitting will somehow magically use your particular menu action, which is completely optional and arbitrary.
It is possible that it is not your shortcut that's invoking the action, but something built in that is platform specific, like how Alt+F4 works on windoze for example.
Since you are using ApplicationWindow you can implement a custom exit handler, which will be used regardless how you quit the application:
  onClosing: {
    // do your stuff
    close.accepted = true // exits, if set to false the quit request will be ignored
  }

Update: Since you say the above code doesn't work as expected, the quitting must be happening at an even lower level that doesn't even notify qtquick, which could also explain the crashing. I don't currently have a macos machine to look into it, however there is decent chance you could get it to quit property by hooking a handler to the QCoreApplication::aboutToQuit() signal.


Q:

what does do forwardRef in angular?

What does forwardRef do in angular, and what is its usage?
here is an example:
import {Component, Injectable, forwardRef} from '@angular/core';

export class ClassCL { value; }

@Component({
    selector: 'my-app',
    template: '<h1>{{ text }}</h1>',
    providers: [{provide: ClassCL, useClass: forwardRef(() => ForwardRefS)}]
})
export class AppComponent {
    text;

    constructor( myClass: ClassCL ) {
        this.text = myClass.value;
    }
}

Injectable()
export class ForwardRefS { value = 'forwardRef works!' }

A:

From Angular's API docs on forwardRef:

Allows to refer to references which are not yet defined.
For instance, forwardRef is used when the token which we need to refer to for the
  purposes of DI is declared, but not yet defined. It is also used when
  the token which we use when creating a query is not yet defined.

There is a good write up at
Angular In Depth.
Here's an extract:

Why does forwardRef work?
Now the question may pop up in your head how the forwardRef works. It actually has to do with how closures in JavaScript work. When you capture a variable inside a closure function it captures the variable reference, not the variable value. Here is the small example to demonstrate that:
let a;
function enclose() {
    console.log(a);
}

enclose(); // undefined

a = 5;
enclose(); // 5

You can see that although the variable a was undefined at the moment the enclose function was created, it captured the variable reference. So when later the variable was updated to the 5 it logged the correct value.
And forwardRef is just a function that captures a class reference into closure and class becomes defined before the function is executed. Angular compiler uses the function resolveForwardRef to unwrap the token or provider type during runtime.

A:

According to Angular's documentation:

Allows to refer to references which are not yet defined.

I believe that in order to better understand how forwardRef works, we need to understand how things happen under the Javascript's hood. I will provide an example of a specific case in which you may need to use forwardRef, but take into account that other different cases may arise.
As we may know, Javascript functions are hoisted to the top of its execution contexts. Functions are objects themselves and other objects may also be created from functions. Because functions allows programmers to create object instances, ECMAScript 2015 created some sort of syntactic sugar in order to make Javascript to feel a little more closer to class based languages, like Java. Enter the class:
class SomeClassName { }

If we go into a Javascript compiler (in my case I am using Babel) and paste this, the result would be:
"use strict";

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var SomeClassName = function SomeClassName() {
  _classCallCheck(this, SomeClassName);
};

The most interesting part is to notice that our class in reality is a function in the background. Like functions, variables are also hoisted within its execution context. The only difference is that while we can call functions (because we can reference its pointer even though it was hoisted), variables are hoisted and given a default value of undefined. The variable is assigned a value, probably other than undefined, at runtime at the given line of the assignment. For instance:
console.log(name);
var name = 'John Snow';

Actually becomes:
var name = undefined;
console.log(name) // which prints undefined
name = 'John Snow';

Ok, with all this in mind, let's now jump into Angular. Let's say we have the following code in our app:
import { Component, Inject, forwardRef, Injectable } from '@angular/core';

@Injectable()
export class Service1Service {
    constructor(private service2Service: Service2Service) {
    }

    getSomeStringValue(): string {
        return this.service2Service.getSomeStringValue();
    }
}

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {
    constructor(private service1Service: Service1Service) {
        console.log(this.service1Service.getSomeStringValue());
    }
}

export class Service2Service {
    getSomeStringValue(): string {
        return 'Some string value.';
    }
}

And of course, we need to provide these services. Let's provide them in our AppModule:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent, Service1Service, Service2Service } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [Service1Service, Service2Service],
  bootstrap: [AppComponent]
})
export class AppModule { }

The important line in our AppModule's metadata is:
providers: [Service1Service, Service2Service]

If we run this code, we'll get the following error:

Hmmmm, interesting... What's going on here? Well, based on the explanation given before, Service2Service becomes a function in the background, but this function gets assigned into a variable. This variable is hoisted, but the value of it is undefined. Because of all this, the parameter cannot be resolved.
Enter forwardRef
In order to solve this issue we have the beautiful function named forwardRef. What this function does is that it takes a function as a parameter (in the example I show I use an arrow function). This function returns a class. forwardRef waits until Service2Service is declared, and then it triggers the arrow function being passed. This results in returning the class that we need in order to create the Service2Service instance. So, your app.component.ts code would look like this:
import { Component, Inject, forwardRef, Injectable } from '@angular/core';

@Injectable()
export class Service1Service {
    constructor(@Inject(forwardRef(() => Service2Service)) private service2Service) {
    }

    getSomeStringValue(): string {
        return this.service2Service.getSomeStringValue();
    }
}

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {
    constructor(private service1Service: Service1Service) {
        console.log(this.service1Service.getSomeStringValue());
    }
}

export class Service2Service {
    getSomeStringValue(): string {
        return 'Some string value.';
    }
}

In conclusion, based on the example I have provided, forwardRef allows us to reference types that are defined later on in our source code, preventing our code from crashing and providing more flexibility in the way we organize things in our code.
I really hope my answer serves you well. :)


Q:

How to get actual JavaScript value in onclick from webbrowser control?

I'm looking for a way to get the JavaScript code defined inside of onclick.
I'm using .NET 2.0 C# Visual Studio 2005.
Example:
click here

My goal is to get the string "window.location.href='someURL'".
Scenario:
A user clicks on web page element, the tag shown above for instance, inside of WebBrowser control. Then the clicked tag is refereed to HtmlElement object.
In WebBrowser control I then call HtmlElement object's getAttribute("onclick"), it just gives me "System.__ComObject".
I've searched how to deal with it then found that it can be casted then get the value.
if (tag.GetAttribute("onclick").Equals("System.__ComObject"))
{
    Console.WriteLine("dom elem  >>>>>>>>>>> " + tag.DomElement.ToString());
    mshtml.HTMLSpanElementClass span = (mshtml.HTMLSpanElementClass)tag.DomElement;

    Console.WriteLine("js value ===>" + span.onclick);
}

Output:
dom elem  >>>>>>>>>>> mshtml.HTMLSpanElementClass
js value ===> System.__ComObject

As it shown, span.onclick still give me System.__ComObject, what am I doing wrong?
In Why does HtmlElement's GetAttribute() method return “mshtml.HTMLInputElementClass” instead of the attribute's value?  this guy said it worked in his case, and I've followed it, but mine is somewhat not working...
UPDATE
Research, research.....
I can add reference VisualBasic.dll to my C# project then call the method to find out who is this System.__ComObject really is.
Console.WriteLine(Microsoft.VisualBasic.Information.TypeName(span.onclick));

Output:
JScriptTypeInfo

It looks like this is a JScript type... how can I access this object?
More detail
The above description is based on my current project. The project is to create something like Selenium IDE. It uses WebBrowser control instead.
Selenium IDE creates 3 different things to record an element in the web document.
1. actionType
2. xpath
3. value

For instance,
type, //input[@id=foo], "hello world"
clickAndWait, //link=login, ""

Selenium IDE recognize page load so it changes actionType between "click" and "clickAndWait". My case, I want to make it simple.
If I click on the element and if it is anchor tag or has page load kind of javascript
such as onclick=window.location.href='blah' then I want to set the actionType to "clickAndWait".

A:

There are number of ways you can do it.

There is an Event object in DOM, which will give you information about which element generated this event.
You can look at here, http://msdn.microsoft.com/en-us/library/ff975965%28v=VS.85%29.aspx
This one is good, you can use this easily, you will get the event object as method parameter which you can investigate parameters to find out the source of the event. http://support.microsoft.com/kb/312777

Another alternative is to use a custom navigation url and act upon it

Override BeforeNavigate event
Check for Navigation url if it contains "mycommand:click" or "mycommand:clickandwait" 3. If it contains any of this, then set cancel as true. (this will stop navigation by browser).
Then you can navigate your webbrowser code from your C# code and pass cancel as true.

Another Alternative method is to use External object, WebBrowser allows you to set an ObjectForScripting which you can access within Javascript of HTML.
ObjectForScripting in .NET 2.0
[ComVisible(true)]
public class MyClass
{
   // can be called from JavaScript
   public void ShowMessageBox(string msg){
       MessageBox.Show(msg);
   }
}

myBrowser.ObjectForScripting = new MyClass();
// or you can reuse instance of MyClass

And you can call,
window.external.ShowMessageBox("This was called from JavaScript");

A:

Cast the element object to mshtml.IHTMLDOMNode, then read the attributes via IHTMLDOMNode.attributes. HtmlElement.GetAttribute is getting the IDispatch interface of the jscript function generated from the embedded attribute.

A:

As per Sheng Jiang's response, here is some working sample:
IHTMLElement element = YourCodeToGetElement();
string onclick = string.Empty;

IHTMLDOMNode domNode = element as IHTMLDOMNode;
IHTMLAttributeCollection attrs = domNode.attributes;

foreach (IHTMLDOMAttribute attr in attrs)
{
    if (attr.nodeName.Equals("onclick"))
    {
        string attrValue = attr.nodeValue as string;
        if (!string.IsNullOrEmpty(attrValue))
        {
            onclick = attr.nodeValue;
            break;
        }
    }
}


Q:

how to write a rake task to bundle install then rake db:migrate then rake db:seed?

How to write a rake task that will bundle install then rake db:migrate then rake db:seed.
namespace 'install' do
  'bundle install'
  'rake db:migrate'
end

A:

This should work but consider using Capistrano/Chef for deployment:
namespace :install do
  task :db_reset do
    # bundle install - I do not believe attempting this in a rake file
    # is recommended as one would need to ensure it is run in your application
    # directory and rvm has loaded correct version of ruby/gemsets (.rvmrc required)
    Rake::Task['db:migrate'].invoke
  end
end

alternatively, you can setup a shell alias to do
bundle install && bundle exec rake db:migrate && bundle exec rake db:seed


Q:

How to convert 220 VDC to 24 VDC

I’m working on a project where I need to build a converter/power supply for converting 220 VDC to 24 VDC.
Yes, its 220 VDC not AC.
This convertor will be mounted on a locomotive type vehicle designed in 1960 but still in use. The vehicle has only one power option i.e. 220 VDC. We need to make 24VDC out of this for connecting surveillance camera, fan etc. Current required is not more than 10 Amps.
I tried searching on google, found converters that goes up to max 90 VDC i.e 90 to 24 VDC anything beyond that is not available.
What type of converter can I use for this voltage conversion?

A:

You can use a general industrial power supply. Here is an example from ABB (90-300 V DC input, 24 V DC, 10 A output): CP-C.1 24/10.0
Another one from Phoenix Contact, certified for EN 50155 (rolling stock applications): QUINT-PS/1AC/24DC/10/CO


Q:

Using Ienumerable.TakeWhile but only returns one set of results

First off I want to apologize if my code is bad or if my description is poor. This is one of my first times working with C# threading/tasks. What I'm trying to do in my code is to go through a list of names and for each 50 names in the list, start a new task and pass off those 50 names to another method that will perform calculation heavy methods on the data. My code only works for the first 50 names in the list and it returns 0 results for every other time and I can't seem to figure out why.
public static async void startInitialDownload(string value)
    {
        IEnumerable<string> names = await Helper.getNames(value, 0);
        decimal multiple = names.Count() / 50;
        string[] results;
        int num1 = 0;
        int num2 = 0;

        for (int i = 0; i < multiple + 1; i++)
        {
            num1 = i * 50;
            num2 = (50 * (i + 1));
            results = names.TakeWhile((name, index) => index >= num1 && index < num2).ToArray();
            Task current = Task.Factory.StartNew(() => getCurrentData(results));
            await current.ConfigureAwait(false);
        }
    }

A:

Realise the enumerable into a list, so that it will be calculated once, not each iteration in the loop. You can use the Skip and Take methods to get a range of the list:
public static async void startInitialDownload(string value) {
  IEnumerable<string> names = await Helper.getNames(value, 0);
  List<string> nameList = names.ToList();
  for (int i = 0; i < nameList.Count; i += 50) {
    string[] results = nameList.Skip(i).Take(50).ToArray();
    Task current = Task.Factory.StartNew(() => getCurrentData(results));
    await current.ConfigureAwait(false);
  }
}

Or you can add items to a list, and execute it when it has the right size:
public static async void startInitialDownload(string value) {
  IEnumerable<string> names = await Helper.getNames(value, 0);
  List<string> buffer = new List<string>();
  foreach (string s in names) {
    buffer.Add(s);
    if (buffer.Count == 50) {
      Task current = Task.Factory.StartNew(() => getCurrentData(buffer.ToArray()));
      await current.ConfigureAwait(false);
      buffer = new List<string>();
    }
  }
  if (buffer.Count > 0) {
    Task current = Task.Factory.StartNew(() => getCurrentData(buffer.ToArray()));
    await current.ConfigureAwait(false);
  }
}


Q:

calling another frame ASP.NET C# function with javascript

in my MainFrame.aspx i have create the panel with load ajaxFrm.aspx
<ext:Panel ID="pnlAjax" runat="server" Height="30" Title="" Header="false" Frame="true">
<AutoLoad Url="ajaxFrm.aspx" Mode="IFrame" ShowMask="false" />
</ext:Panel>

i the ajaxFrm.aspx having this C# LoadVehStore2 Function
function LoadVehStore2() {

}

in MainFrame.aspx i have a javascript when the page are load then run the javascript
frmAjax = parent.frames["pnlAjax"];
frmAjax.LoadVehStore2();

this thing can work proper in IIS,
but in Ubuntu Mono 3.3.0 are fail, why? how to solve this?

A:

That's not C#. It's Javascript. I doubt that the problem is with Mono, I suspect the problem is with the browser.
Since the iframe is created by Ext.Net you have to reference it with the name Ext.Net gives to it. To get the name just use the developer tools of the browser, but it should be pnlAjax_IFrame.
Or, if it's the only iframe in the parent page you'd better reference it by id:
frmAjax = parent.frames[0].LoadVehStore2();


Q:

Jcrop default selection box

$('#previewImg').Jcrop({
        onChange: showCoords,
        onSelect: showCoords,
        minSize:[300,100],
        maxSize:[900,900],
        aspectRatio: 3/1
    });

I have an image use jcrop, however i need to have a default box 300,100 before user click on image to make it appear.
so the box will already appear & at center before user click the image.

A:

you have to set the setSelect option while creating the jcrop
$('#previewImg').Jcrop({
        onChange: showCoords,
        onSelect: showCoords,
        setSelect: [0, 160, 160, 0],// you have set proper x and y coordinates here
        minSize:[300,100],
        maxSize:[900,900],
        aspectRatio: 3/1
    });

Here is the demo for setSelect option http://deepliquid.com/projects/Jcrop/demos.php?demo=advanced


Q:

Minimum Tile Ordering

Minimizing Tile Re-ordering Problem:
Suppose I had the following symmetric 9x9 matrix, N^2 interactions between N particles:
(1,2) (2,9) (4,5) (4,6) (5,8) (7,8),

These are symmetric interactions, so it implicitly implies that there exists:
(2,1) (9,2) (5,4) (6,4) (8,5) (8,7),

In my problem, suppose they are arranged in matrix form, where only the upper triangle is shown:
t       0     1     2     (tiles)
  #   1 2 3 4 5 6 7 8 9
  1 [ 0 1 0 0 0 0 0 0 0 ]
0 2 [ x 0 0 0 0 0 0 0 1 ]
  3 [ x x 0 0 0 0 0 0 0 ]
  4 [ x x x 0 1 1 0 0 0 ]
1 5 [ x x x x 0 0 0 1 0 ]
  6 [ x x x x x 0 0 0 0 ]
  7 [ x x x x x x 0 1 0 ]
2 8 [ x x x x x x x 0 0 ]
  9 [ x x x x x x x x 0 ] (x's denote symmetric pair)

I have some operation that's computed in 3x3 tiles, and any 3x3 that contains at least a single 1 must be computed entirely. The above example requires at least 5 tiles: (0,0), (0,2), (1,1), (1,2), (2,2)
However, if I swap the 3rd and 9th columns (and along with the rows since its a symmetric matrix) by permutating my input:
t       0     1     2
  #   1 2 9 4 5 6 7 8 3
  1 [ 0 1 0 0 0 0 0 0 0 ]
0 2 [ x 0 1 0 0 0 0 0 0 ]
  9 [ x x 0 0 0 0 0 0 0 ]
  4 [ x x x 0 1 1 0 0 0 ]
1 5 [ x x x x 0 0 0 1 0 ]
  6 [ x x x x x 0 0 0 0 ]
  7 [ x x x x x x 0 1 0 ]
2 8 [ x x x x x x x 0 0 ]
  3 [ x x x x x x x x 0 ] (x's denote symmetric pair)

Now I only need to compute 4 tiles: (0,0), (1,1), (1,2), (2,2).
The General Problem:
Given an NxN sparse matrix, finding an re-ordering to minimize the number of TxT tiles that must be computed. Suppose that N is a multiple of T. An optimal, but unfeasible, solution can be found by trying out the N! permutations of the input ordering.
For heuristics, I've tried bandwidth minimization routines (such as Reverse CutHill McKee), Tim Davis' AMD routines, so far to no avail. I don't think diagonalization is the right approach here.
Here's a sample starting matrix:
http://proteneer.com/misc/out2.dat
Hilbert Curve:

RCM:

Morton Curve:

A:

There are several well-known options you can try (some of them you have, but still):

(Reverse) Cuthill-McKee reduced the matrix bandwidth, keeping the entries close to the diagonal.
Approximage Minimum Degree -  a light-weight fill-reducing reordering.
fill-reducing reordering for sparse LU/LL' decomposition (METIS, SCOTCH) - quite computationally heavy.
space filling curve reordering (something in these lines)
quad-trees for 2D or oct-trees for 3D problems - you assign the particles to quads/octants and later number them according to the quad/octant id, similar to space filling curves in a sense.
Self Avoiding Walk is used on structured grids to traverse the grid points in such order that all points are only visited once
a lot of research in blocking of the sparse matrix entries has been done in the context of Sparse Matrix-Vector multiplication. Many of the researchers have tried to find good reordering for that purpose (I do not have the perfect overview on that subject, but have a look at e.g. this paper)

All of those tend to find structure in your matrix and in some sense group the non-zero entries. Since you say you deal with particles, it means that your connectivity graph is in some sense 'local' because of spatial locality of the particle interactions. In this case these methods should be of good use.
Of course, they do not provide the exact solution to the problem :) But they are commonly used in exactly such cases because they yield very good reorderings in practice. I wonder what do you mean by saying the methods you tried failed? Do you expect to find the optimum solution? Surely, they improve the situation compared to a random matrix ordering.
Edit Let me briefly go through a few pictures. I have created a 3D structured cartesian mesh composed of 20-node brick elements. I matched the size of the mesh so that it is similar to yours (~1000 nodes). Also, number of non-zero entries per row are not too far off (51-81 in my case, 59-81 in your case, both however have very different distributions) The pictures below show RCM and METIS reorderings for non-periodic mesh (left), and for mesh with complete x-y-z periodicity (right):
Next picture shows the same matrix reordered using METIS and fill-reducing reordering

The difference is striking - bad impact of periodicity is clear. Now your matrix reordered with RCM and METIS

WOW. You have a problem :) First of all, I think there is something wrong with your rcm, because mine looks different ;) Also, I am certain that you can not conclude anything general and meaningful about any reordering based on this particular matrix. This is because your system size is very small (less than roughly 10x10x10 points), and you seem to have relatively long-range interactions between your particles. Hence, introducing periodicity into such small system has a much stronger bad effect on reordering than is seen in my structured case.
I would start the search for a good reordering by turning off periodicity. Once you have a reordering that satisfies you, introduce periodic interactions. In the system you showed there is almost nothing but periodicity: because it is very smal and because your interactions are fairly long-range, at least compared to my mesh. In much larger systems periodicity will have a smaller effect on the center of the model.
Smaller, but still negative. Maybe you could change your approach to periodicity? Instead of including periodic connectivities explicitly in the matrix, construct and reorder a matrix without those and introduce explicit equations binding the periodic particles together, e.g.:
V_particle1 = V_particle100

or in other words
V_particle1 - V_particle100 = 0

and add those equations at the end of your matrix. This method is called the Lagrange multipliers. Here is how it looks for my system

You keep the reordering of the non-periodic system and the periodic connectivities are localized in a block at the end of the matrix. Of course, you can use it for any other reorderings.
The next idea is you start with a reordered non-periodic system and explicitly eliminate matrix rows for the periodic nodes by adding them into the rows they are mapped onto. You should of course also eliminate the columns.
Whether you can use these depends on what you do with your matrix. Lagrange multiplier for example introduce 0 on the diagonal - not all solvers like that..
Anyway, this is very interesting research. I think that because of the specifics of your problem (as I understand it - irregularly placed particles in 3D, with fairly long-range interactions) make it very difficult to group the matrix entries. But I am very curious what you end up doing. Please let me know!


Q:

Any set A has void set as its subset? if yes how?

It is said that null set is a subset of every set.but the definition for subset says that "if every elements of A is also an elements of B then $A \subset C$  ".
Therefore is it mean every set contains null element as its element? How is it possible a set contain void set as it subset?

A:

Imagine doing an checklist to determine if a set is a subset of another:
Case one:  Is {fred, wilma, dino} a subset of {barney, dino, Mr. Slate, wilma, betty, fred}.
Okay, is fred on the list?  Check.  Is wilma on the list?  Check.  Is dino?  Check.  Okay it's a subset.
Case two:  is {bananas, milk, peaches} a subset of {peaches, oranges, bananas, grapes}?
Okay, is bananas on the list?  Check.  Is milk?  Nope.  then it's not a subset.
Case three: is {} a subset of {green, red, yellow}?
Um...  Well, go on... what's the first item on the list?...  Um, there isn't any...  Well, is everything on your list in my list?  Well, there isn't anything on my list...  Then is there anything on your list that isn't on my list?..  Um, there's nothing on my list...  Well, then everything on your list is on my list, isn't it.  After all, there's nothing on your list that isn't on my list, right?  ... Um, I guess so....  So it's a subset--- a void, empty, trivial subset but a subset, none-the-less.

A:

Definition: $A\subseteq B$ if $x\in A$ implies $x\in B$
$x\in \emptyset$ is false for any $x$, so the statement "$x\in \emptyset$ implies $x\in B$" is true for any set $B$.

Alternatively:
Definition: $A\subseteq B$ if every element of $A$ is in $B$
Since there is nothing in the empty set, every element of the empty set (i.e. nothing) is in every other set.

A:

By definition, $A\subseteq B$ if and only if $x\in A \Rightarrow x\in B$, which is equivalent to $x\notin B \Rightarrow x\notin A$. If $A=\emptyset$ then this last implication holds for any set $B$.


Q:

copy large table from sql server to odbc linked database (postgresql) in ssms

I'm trying to copy created a whole database in SQL Server to Postgres. I've been trying to write a script that can run in ssms with a postgres instance set up as a linked server. This will not be a one off operation.
I've managed to create a script that creates most of the schema i.e. tables, constraints, indexes etc. I do this by using the information_schema tables in sql server and formatting the data to form valid sql for postgres and run EXEC(@sql) AT POSTGRES statement, where POSTGRES is the linked server and @SQL a variable containing my statement. This is working fine.
Now I'm trying to insert the data using a statement like this:
INSERT INTO OPENQUERY(POSTGRES,'SELECT * FROM targettable')
SELECT *
FROM sourcetable

The statements are actually slightly modified in some cases to deal with different data types, but this is the idea. The problem is when the table is particularly large, this statement fails with the error :
Msg 109, Level 20, State 0, Line 0
A transport-level error has occurred when receiving results from the server. (provider: Named Pipes Provider, error: 0 - The pipe has been ended.

I think the error is caused by either postgre or sql server using too much memory generating the large statement. I've found that if I manually select only parts of the data to insert at a time, it works. For instance, the top 10000 rows. But I don't know a way to write a general statement to select only x amount of rows at a time that isn't specific to the table I'm referencing.
Perhaps someone can suggest a better way of doing this though. Keep in mind I do need to change some of the data before inserting it into postgres e.g. geospatial information is transformed to a string so postgres will be able to interpret it.
Thanks!

A:

I ended up using the OFFSET X ROWS FETCH NEXT Y ROWS ONLY introduced in SQL Server 2012 so the complete statement looked like this:
INSERT INTO OPENQUERY(POSTGRES,'SELECT * FROM targettable')
SELECT *
FROM sourcetable
ORDER BY 1
OFFSET 0 ROWS FETCH NEXT 10000 ROWS ONLY

And everything is working and error appears! I actually iterate through the OFFSET value adding 10,000 to it on every iteration using dynamic SQL.
Not the cleanest or nicest solution. I think most people would be better writing something in another language as mentioned by Michal Niklas, but this worked for me.


Q:

Getting XML File from Site to Phone

Ok. So I have my first ever iPhone app prototyped and it will parse a given XML file. This file is in with all of the other files that make up the app for testing purposes but eventually I want the app to pull the XML data from a web service.
So I've written a PHP script that reads data and creates an XML file for me. But how do I go about getting the XML file from the web server to the phone?
This is what I have:

An XML file accessible via URL - i.e. //localhost/v/p.xml
An iOS 6 app that can parse an XML file which is currently loaded in the app (put under the Supporting Files folder in my XCode project).

So what's the best way to get the XML file to the phone? Is LazyTableImages something I should be looking at?
Should I do this:
NSURL *url = [[NSURL alloc] initFileURLWithPath:@"http://localhost/v/p.xml"];
NSData *xmlData = [[NSData alloc] initWithContentsOfURL:url];

//NSString *path = [[NSBundle mainBundle] pathForResource:@"profileXML" ofType:@"xml"];
//NSData *xmlData = [[NSData alloc] initWithContentsOfFile:path];

Where to top 2 lines replace the commented out line? Is it that simple?

A:

You can use stringWithContentsOfURL:encoding:error:, documented here:
http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html
For example:
[NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://localhost/v/p.xml"] encoding:NSUTF8StringEncoding error:nil];

Update:
As Jim says, NSURLConnection is non-blocking, and it allows you to submit credential if you want to protect your file from public access. If you want to use that, it looks like this:
NSString *authenticationURL = @"http://foo.bar";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:authenticationURL]];
NSString *escUsername = [username stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *escPassword = [password stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

[request setHTTPMethod: @"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setHTTPBody:[[NSString stringWithFormat:@"username=%@&password=%@", escUsername, escPassword] dataUsingEncoding:NSUTF8StringEncoding]];

NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

#pragma mark Url connection delegate methods

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    NSString *responseText = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@", responseText);
}


Q:

How to get the i-th integer in a integer parameter pack?

How to get the i-th integer in a integer parameter pack? For example
template<int... Is>
struct A
{
    enum { CONSTANT_0 = Is[0] }; //Assume the sizeof...(Is) > the index requested
};

A:

Like that:
template <size_t I, int N, int... R>
struct pick : pick <I - 1, R...> { };

template <int N, int... R>
struct pick <0, N, R...> : std::integral_constant <int, N> { };

so that
pick <3, 1, 2, 3, 4, 5, 6>::value

equals 4, and
template<int... Is>
struct A
{
    enum { CONSTANT_0 = pick <0, Is...>::value };
};

is how you would use it in your case.

Another way:
template <size_t I, int... N>
using pick = typename std::tuple_element <I,
   std::tuple <std::integral_constant <int, N>...>
>::type;


Q:

SQL database ODBC connection Multi-part identifier could not be bound

We're using PADS Professional xDX Designer (from MentorGraphics) with a SQL parts database connected through ODBC using views. All seems to be working just fine, except for our Inductor parts where somehow I get an error assigning the Sat. Current to a part.
I get the following error:
An ODBC error occurred:
The multi-part identifier "Sat. Current" could not be bound.
The multi-part identifier "Sat. Current" could not be bound.
Statement(s) could not be prepared.
State:37000 Native:4104,Origin:[Microsoft[ODBC SQL Server Driver][SQL Server]
State:37000 Native:4104,Origin:[Microsoft[ODBC SQL Server Driver][SQL Server]
State:37000 Native:8180,Origin:[Microsoft[ODBC SQL Server Driver][SQL Server]
(1400)

My SQL query used to create the view:
--DROP view _ARV_Inductor
--CREATE VIEW _ARV_Inductor AS
SELECT [Number] as [Part Number],
[Description],
CONVERT(float,[Value]) as [Value],
[Case],
[Variant_Member] as [Variant],
CONVERT(float,[Rdc]) as [Rdc],
CONVERT(float,[Tolerance]) as [Tolerance],
CONVERT(float,[Nom. Current]) as [Nom. Current],
CONVERT(float,[Sat. Current]) as [Sat. Current],
[Temp Min],
[Temp Max],
[Datasheet],
[CostPerUnit] as Price,
[Manufacturer_member] as [Manufacturer],
[Part name],
[RS],
[Farnell],
[Supplier_member] as [Supplier],
[Order code],
[Location]
FROM [PDXpertDB].[viewer].[SourceItemMasterView] where ([Type]='Inductor' AND [ShowInDX]='true')

All other properties assign correctly, when I use this view in SQL Server Management Studio I also get no errors and it shows the correct list.
Any suggestions what I might be doing wrong? Is it even a fault in my SQL part or might it have something to do with xDX designer?

A:

I would check if you have "Quoted Identifiers" set in the DSN, it looks like its interpreting [Sat. Current] as the column [Current] in the table [Sat]


Q:

Python class input argument

I am new to OOP. My idea was to implement the following class:
class name(object, name):
    def __init__(self, name):
        print name

Then the idea was to create two instances of that class:
person1 = name("jean")
person2 = name("dean")

I know, that is not possible, but how can I pass an input-argument into an instance of a class?

A:

The problem in your initial definition of the class is that you've written:
class name(object, name):

This means that the class inherits the base class called "object", and the base class called "name". However, there is no base class called "name", so it fails. Instead, all you need to do is have the variable in the special init method, which will mean that the class takes it as a variable.
class name(object):
  def __init__(self, name):
    print name

If you wanted to use the variable in other methods that you define within the class, you can assign name to self.name, and use that in any other method in the class without needing to pass it to the method.
For example:
class name(object):
  def __init__(self, name):
    self.name = name
  def PrintName(self):
    print self.name

a = name('bob')
a.PrintName()
bob

A:

>>> class name(object):
...     def __init__(self, name):
...         self.name = name
...
>>> person1 = name("jean")
>>> person2 = name("dean")
>>> person1.name
'jean'
>>> person2.name
'dean'
>>>

A:

You just need to do it in correct syntax. Let me give you a minimal example I just did with Python interactive shell:
>>> class MyNameClass():
...   def __init__(self, myname):
...       print myname
...
>>> p1 = MyNameClass('John')
John


Q:

Trouble converting a fractal equation to a Python algorithm

I'm trying to convert the equation outlined here into Python code:
r = +/- (1+1.414sin(theta)cos(theta)-0.5cos(theta)cos(theta))^(1/6)exp(-0.4714(theta))

This is my result (slightly hacked for testing purposes):
import random
import pygame
import math
from pygame.locals import *

def random_spiral_pos(maxradius,theta=None):
    "Finds a random position in a spiral galaxy pattern."
    #Get a random angle (in rad). Could do this with a random
    #variable in the range (0,2*pi), but this i clearer if inefficient
    if theta == None:
        theta=math.radians(random.randint(0,360))

    #Then use a fractal equation to get distance from center as a function
    #of angle
    #Source: http://www.philica.com/display_observation.php?observation_id=52
    r = (1+1.414*math.sin(theta)*math.cos(theta) -0.5*math.cos(theta)*math.cos(theta))**(1/6)*math.exp(-0.4714*theta)

    print(r)

    #R will be in the range 0-1, so we multiply it by
    #the radius of our drawing area
    r=r*maxradius

    #Convert the angle into polar coordinates, give the resultant vector
    #magnitude R (polar coordinates are a direction vector from the
    #origin), then floor those values so Pygame can use them
    x=math.floor(math.cos(theta)*r)
    y=math.floor(math.sin(theta)*r)

    x=x+maxradius//2
    y=y+maxradius//2
    return (x,y)

if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((600,600))
    clock=pygame.time.Clock()
    pygame.key.set_repeat(25,5)

    #Main loop
    while 1:

        #timing
        clock.tick(60)

        #gfx
        screen.fill((0,0,0))
        screen.lock()
        for t in range (0,360):
            screen.set_at(random_spiral_pos(100,math.radians(t)),(255,255,255))
        screen.unlock()
        pygame.display.flip()

However, the result seems to merely be a simple spiral. It's likely that I've just made an error in converting the mathematical function to a Python statement, as the mathematics on display here are some ways beyond my education. Is this the case, and how should it be expressed if so?

A:

There's at least one mistake here.  You have this:
r = (1+1.414*math.sin(theta)*math.cos(theta)*-0.5*math.cos(theta)*math.cos(theta))**(1/6)*math.exp(-0.4714*theta)

and, according to the link, it should be this:
r = (1+1.414*math.sin(theta)*math.cos(theta) -0.5*math.cos(theta)*math.cos(theta))**(1/6)*math.exp(-0.4714*theta)

Also, be careful with expressions like (1/6) in Python. If you're using Python2.X, then this will perform integer division and the result is 0. To get floating point division as standard, you need to put
from __future__ import division

at the top of your script.


Q:

Filter directories from list of files and directories

I have a file containing full path names of files an directories.
From this list I would like to filter out any pathnames that reference directories so that I am left with a list containing only file paths.
Can anybody think of an elegant solution?

A:

while IFS= read -r file; do
  [ -d "$file" ] || printf '%s\n' "$file"
done <input_file

Would print the files that are not determined to be of type directory (or symlink to directory). It would leave all other type of files (regular, symlink (except to directories), sockets, pipes...) and those for which the type cannot be determined (for instance because they don't exist or are in directories which you don't have search permission to).
Some variations depending on what you meant by file and directory (directory is one of many types of files on Unix):

the file exists (after symlink resolution) and is not of type directory:
[ -e "$file" ] && [ ! -d "$file" ] && printf '%s\n' "$file"

file exists and is a regular file (after symlink resolution):
[ -f "$file" ] && printf '%s\n' "$file"

file exists and is a regular file before symlink resolution (excludes symlinks):
[ -f "$file" ] && [ ! -L "$file" ] && printf '%s\n' "$file"

etc.