SimpleDateFormat parsing troubles

Question: I'm having trouble parsing a date that I'm trying to reformat. The SimpleDateFormat is giving me a real headache.
I'm getting this date from a news feed:
Wed, 06 Mar 2013 09:22:00 +0100

And I need to format it to this:
06.03.2013

I could just use a hashmap with all the months but I would like to use the SimpleDateFormat, since that's what it's for.
But I can't seem to find the right pattern.
Comment: Do you know the format you receive? What is +0100 at the end?
Answer: Try adding following code snippet:--
    //SimpleDateFormat fmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss +SSSS");
SimpleDateFormat fmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss +SSSS", Locale.ENGLISH);
        Date date = null;
        try {
            date = fmt.parse("Wed, 06 Mar 2013 09:22:00 +0100");//Hardcoded for your case...
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        SimpleDateFormat fmtOut = new SimpleDateFormat("dd.MM.yyyy");

        System.out.println("Date :-- " +fmtOut.format(date));

Comment: thx. i tried your snippet but am getting a NPE wenn trying to Log. Even with hardcoded string.
Comment: Well, I checked running the code here, and it worked fine, try copy-paste the given code as given i did made some changes earlier, .. Are you getting ParseException in catch block provided..
Comment: ok, i will, although i think i had the latest verion. but i'll still give it a new go incase im missing something. No the Exception throws in your last line, wich i btw i changed to an Log statement, but that shouldnt matter, right?
Comment: hmm, you are not getting ParseException means your date object returned not null, and still NPE.. weird, plz double check with first line of code snipet matches exactly , also check in logtrace for ParseException if any..
Comment: sry, your right, I am getting ParseException! java.text.ParseException: Unparseable date: "Wed, 06 Mar 2013 09:22:00 +0100" (at offset 0). here is how i implementet it atm http://pastebin.com/vsFNAYzN
Comment: obviously im getting the NPE becouse the inital parse turns out null..but why with me and not at your end?
Comment: ok, seems i have to pass the right Locale to the SimpleDateFormat, checking which one now
Comment: Thx, works with Locale.US. Edited your anwser to be correct for future reader, Thx Alot
Comment: @CRUSADER `S` is for milliseconds, the timezone is `z`, `Z`, or `X`, in this case `Z` I think.

SimpleDateFormat parsing troubles


Length of database result without using count?

Question:
Possible Duplicate:
How can I count the numbers of rows that a mysql query returned?

To get the length of the return of a mysql result, I am using:
$counter=0;
while($row = mysql_fetch_array($mysqlqueryresult)){
$counter++;
}
echo $counter;

Is there a better way to get the number of rows returned by mysql?
Note - Not allowed to use COUNT in the mysql or any mysql tricks, only php on the mysql output as here.  So starting point is a mysql returned thing of $mysqlqueryresult; which itself is the result of a basic SELECT query.
Comment: @Michael, it's easy to search for the answer if you already know the answer.
Comment: http://stackoverflow.com/search?q=mysql_num_rows
Answer: Use mysql_num_rows.
$counter = mysql_num_rows($mysqlqueryresult);

If you've updated the table, the number of affected rows is given by mysql_affected_rows.
Comment: This is one of those instances where the question has been asked so many times it becomes impossible to find a dulicate to link.
Answer: Use mysql_num_rows($mysqlqueryresult)
Answer: mysql_num_rows() is the function you're looking for.
edit. I arrived too late. ;)
Answer: you Can use mysql_num_rows() routine .
$counter = mysql_num_rows($mysqlqueryresult);

Answer: Well, just for the method's sake (although mysql_num_rows() is the preferred method), a pure php one without using a loop:
count($mysql_fetch_array($mysqlqueryresult));
(or am I completely mistaken?)


Using WHERE clause with multiple left join in php

Question: I successfully did a LEFT JOIN using PHP, however, I am having a little difficulty adding a WHERE clause to enable me select unique record from all existing details in all the tables where a match is found.
This is my code :
  <?php
$sql = "SELECT staff.*, staff_bio.bio_id, staff_cert.*, staff_edu.*, staff_pos.*, staff_res.*\n"
    . " FROM staff staff\n"
    . "LEFT JOIN staff_bio \n"
    . " ON staff.nuc_id = staff_bio.bio_id\n"
    . "LEFT JOIN staff_cert\n"
    . " ON staff.nuc_id = staff_cert.pro_id\n"
    . "LEFT JOIN staff_edu\n"
    . " ON staff.nuc_id = staff_edu.edu_id\n"
    . "LEFT JOIN staff_pos\n"
    . " ON staff.nuc_id = staff_pos.rank_id\n"
    . "LEFT JOIN staff_res\n"
    . " ON staff.nuc_id = staff_res.res_id\n"
    . "WHERE staff.nuc_id = $userRow['staff_no'] ";//this is where i'm having issues
?>

My final output should be something like :
<?php echo $userRow['sch_name']; ?>
<?php echo $userRow['fac_name']; ?>
<?php echo $userRow['dep_name']; ?>

All coming from different tables. Any help will be greatly appreciated.
Comment: First of all don't put the raw input of the user in the query since its insecure.The second thing is do you actually need just the LEFT JOIN do you know how it works?And a suggestion from me is that you put your whole database schema and that you explain what you want.Somebody will show how it can be done in a whole different way.....than just to put a n number of left joins, and bind the params to it since its the most secure way of doing it.Also I'm concerned of how your php code looks like :| But it's up to you what your initial goal?To make it work or to learn from this?
Comment: i'm actually creating a college accredidation app. so i have different tables for staff. tbls consist of biodata, academic records, professional qualifications and a few other tables. why i choose a left join is to enable the staff view his complete profile from all the tables when logged in. PS: i just started learning PHP so i'm not much of an expert
Comment: I think that you don't understand the left join.Update your answer please
Comment: ok.. i believe LEFT JOIN returns all rows from the left (parent) table, even if there are no matches in the right (child) table. so if that is the case, when a user logs in and complete his/her form, he/she should be able to view information entered and possibly edit
Comment: You lost the point there...Show your table structure and explain your question in detail.I'm just sick of thise poor answers on stack.....
Comment: i wish i could show you a sreenshot, but there's no place to add images here
Answer: You can do like this:
. " WHERE staff.nuc_id = ".$userRow['staff_no'] ;//this is where i'm having issues

Why do you need to concatenate query, it can be written as if you want formatting :
<?php

    $sql = "SELECT staff.*, staff_bio.bio_id, staff_cert.*, staff_edu.*, staff_pos.*, staff_res.*
         FROM staff staff
        LEFT JOIN staff_bio
         ON staff.nuc_id = staff_bio.bio_id
        LEFT JOIN staff_cert
         ON staff.nuc_id = staff_cert.pro_id
        LEFT JOIN staff_edu
         ON staff.nuc_id = staff_edu.edu_id
        LEFT JOIN staff_pos
         ON staff.nuc_id = staff_pos.rank_id
        LEFT JOIN staff_res
         ON staff.nuc_id = staff_res.res_id
        WHERE staff.nuc_id = ".$userRow['staff_no'];//this is where i'm having issues

     ?>

Comment: If you mean variable in quotes, i think that's right if there double quotes
Comment: well, it was giving some parsing error so I changed to this for quick solution
Comment: 'WHERE staff.nuc_id = ".$userRow['staff_no'];' seriously?
Comment: @Arslan.H you should read the above comment. If you paste code snippet to any online php syntax checker, it will show parsing error. I have just tried to resolve that error quickly nothing else
Comment: It's not about the error its about how poor you have continue to write the rest of the question.At least if you are answering explain it the proper way.....
Comment: thank you eveyone for your contribution. i'm stil learning bit by bit. hopefully i'l get better soon.


Injectivity of a map $\text{Map}(\mathbb{R},\mathbb{R})\to\mathbb{R}^n$

Question: Let $f_i:\mathbb{R}\to\mathbb{R}, x\mapsto x^i$ and $U_n=span_{\mathbb{R}}\{f_0,...,f_{n-1}\}$ for some integer $n>0$.
Why is the map $f:U_n\to\mathbb{R}^n, f\mapsto (f(a_1),...,f(a_n))$ for some distinct fixed $a_i\in\mathbb{R}$ injective?
Comment: That helps a lot, thank you :)
Comment: Hint: $U_n$ is degree $\leq n-1$ polynomials. $f$ is a linear map. What can you say about an element in its kernel?
Answer: Elements of $U_n$ are exactly polynomials of degree at most $n-1$. So you're asking if for any fixed $\{a_i\}$, there are two different polynomials $g,h$ s.t. $g(a_i)=h(a_i)$ for each $a_i\in\{a_i\}$. (What we're really proving here is that polynomials of degree $n-1$ are determined by $n$ points.)
Let $g,h$ be polynomials of degree at most $n-1$ that agree on all $n$ points. Then $g-h$ is a polynomial of degree at most $n-1$ with $n$ distinct roots. But nonconstant polynomials of degree $n-1$ have at most $n-1$ roots, so $g-h$ is constant, and it has a root, so it is the $0$ polynomial. So $g=h$. This works as long as we have enough distinct points, which is why it sometimes fails in the finite field case.
Comment: n sufficiently "generic points," or in "general position"


In a component how to wait for an action result to invoke other action?

Question: I am using React + Flux for my application. General flow is I will call action method from JSX component and that action method will invoke store which updates state and emits change event and the state will be updated in JSX component.
Now the problem is, I have separated actions and stores into different files based on their work(2 action files and 2 store files).
Can I write this kind of code in JSX component ? (action2.method2() call is waiting for result of actions1.method1() ). If not, how can I improve this ? method1() and method2() are two different actions methods, so we can't call actions2.method2() inside body of actions1.method1() and also method2 needs access to state updated by method1().
this.getFlux().actions.actions1.method1()
        .then(() => {
          this.getFlux().actions.actions2.method2(this.state.method1UpdatedState);
         })
        .catch((error) => { console.log("error : ", error)} );

Answer: I would recommend keeping actions as simple as possible, and move all business logic to your stores. So, if you need data from store1 in store2 just inject store1 in your store2 as a dependency and listen to the specific event, generated by action1 in your store2. You definitely, should avoid implementing this logic in your component.
For example, this how store2.js should look
import store1 from './store1';

store2.dispatchToken = AppDispathcer.register((payload) => {
   var action = payload.action;

   switch(action) {
       case 'ACTION_1':
         AppDispathcer.waitFor([store1.dispatchToken]);
         let data = store1.getDataFromAction1();
         // ... here you can update store2 data
         // ... and call store2.emitChange();
         break;
   }

});


Unexpected result with 2d array - whole column is changed

Question: Given a 3x3 matrix I want to set the element in the first row, 3rd column to true.
I would expect that m[0][2] = true would do it, but it does not. It sets the whole column to true!
My code to create the array and set the value is below. What caused this behavior?
const size = 3;

const m = Array(size).fill(Array(size).fill(false))

console.log(m);
/* [[false, false, false],
    [false, false, false],
    [false, false, false]]
*/

m[0][2] = true;

console.log(m);
/*
   [[false, false, true],
    [false, false, true],
    [false, false, true]]
*/

Comment: Does this answer your question? [Array.fill(Array) creates copies by references not by value](https://stackoverflow.com/questions/37949813/array-fillarray-creates-copies-by-references-not-by-value)
Comment: The behaviour you describe is already *telling* you that they're the same reference. That specific dupe suggestion is just one of many signposts around this behaviour with Array.fill - https://stackoverflow.com/q/27613126/3001761, https://stackoverflow.com/q/60432172/3001761, https://stackoverflow.com/q/57514341/3001761, ...
Answer: You are filling the first array with the same array in every spot.

let array1 = [false, false, false];
let array2 = array1;
array1[2] = true;
console.log(array2);

Use Array.from

let size = 3;
let m = Array.from({ length: size }, _ => Array.from({ length: size }, _ => false));
console.log(m);
m[0][2] = true;
console.log(m);

This way you have a different instance of an array in each slot of the top level array.
You could use Array.fill in the function as false is a value object not a reference object.

let size = 3;
let m = Array.from({ length: size }, _ => Array(size).fill(false));
console.log(m);
m[0][2] = true;
console.log(m);
Comment: Thanks for the pointer. Didn't know that fill passed the values by reference
Answer: 7 Ways to create 2D matrices in JavaScript:
One liners:
const size = 3;
const val = null;

const m1 = Array(size).fill(Array(size).fill(val)); // issue: rows are all the same.
const m2 = Array(size).fill([...Array(size).fill(val)]); // issue: rows are the same;

const m3 = Array(size).fill().map(_ => Array(size).fill(val));
const m4 = Array.from(Array(size), _ => Array(size).fill(val));

const m5 = Array.from({length: size}, _ => Array.from({length: size}, _ => val))

All of these will create size x size matrices. However, using Array.fill directly will create copies by reference and not by value (e.g. m1 and m2). You can use Array.fill inside a mapper function to fix the issue (e.g. m3, m4, and m7). Finally, you can create matrices using Array.from twice.
There are 2 more ways of creating matrices but not as elegant.
Using for loops:
const m6 = [];
for(let i = 0; i < size; i++) {
    m6[i] = [];
    for(let j = 0; j < size; j++) {
        m6[i][j] = val;
    }
}

const m7 = Array(size);
for(let i = 0; i < m7.length; i++) {
    m7[i] = Array(size).fill(val);
}


Shortcuts for $\int_{\kappa}F dx$

Question: Let $F(x,y,z):=\begin{pmatrix}
x^{2}+5y+3yz \\ 5x +3xz -2 \\ 3xy -4z \end{pmatrix}$
and $\kappa: [0, 2\pi] \to \mathbb R^{3}, t\mapsto\begin{pmatrix}\sin t\\ \cos t \\ t \end{pmatrix}$
I was asked to find $\int_{\kappa}Fdx$.
I have tried to calculate it directly:
$\int_{\kappa}Fdx=\int_{0}^{2\pi}\begin{pmatrix}
\sin (t)^{2}+5\cos t+3\cos {(t)}t \\ 5\sin{(t)} +3\sin{(t)}t -2 \\ 3\sin{(t)}\cos{(t)} -4t \end{pmatrix}\cdot \begin{pmatrix}\cos t\\ -\sin t \\ 1 \end{pmatrix}dt$
and basically I get something that I cannot calculate.
I have been given the tip of using path independence.
First I have seen that $DF(x,y,z)$ is symmetrical, so I can use path independence.
I am new to curve integrals, so I am unsure what curve $\overline{\kappa}:[a,b]\to \mathbb R^{3}$ (where $\overline{\kappa}(a)=\kappa(0)$ and $\overline{\kappa}(b)=\kappa(2\pi)$) I am supposed to use, in order to have a better integral to calculate.
Using $\overline{\kappa}(t)=\begin{pmatrix} 0 \\ 1 \\ t \end{pmatrix}$
we get $\int_{0}^{2\pi}\begin{pmatrix} 5+3t \\ -2 \\ -4t \end{pmatrix}\cdot \begin{pmatrix} 0 \\ 0 \\ 1 \end{pmatrix}dt=-4\int_{0}^{2\pi}t dt=-8\pi^{2}$
Answer: From $(0,1,0)$ to $(0,1,2\pi)$? It's hard to go wrong with a straight line.
And with $x$ and $y$ the same between the endpoints and $x$ zero throughout, lots of things just zero out cleanly.
Comment: I've used your tip, is it correct?
Comment: Looks good to me.
Answer: You can use $t\mapsto\begin{pmatrix}0\\1\\t\end{pmatrix}$ for $t\in[0,2\pi]$.

Shortcuts for $\int_{\kappa}F dx$


Running an ICC with variable number of raters

Question: I have a dataset where 69 videos are rated by 130 raters, but each rater only rates 10 videos each.
I need to calculate some measure of rater reliability, but everything I have attempted won't run because of the missing data.
After some reading, I have found that ICC(1) might be appropriate for this type of data, however, when I go to run this model in SPSS the output says there are not enough variables to calculate (assuming because of listwise deletion to deal with missing data).
Before I go insane working out how to run this, can anyone enlighten me as to how I might actually run the ICC(1) on the data?
I have my data set up as columns for the raters and rows for the videos.
Thanks!
Comment: Maybe read [this](http://neoacademic.com/2011/11/16/computing-intraclass-correlations-icc-as-estimates-of-interrater-reliability-in-spss/). Let me know if it helps.
Answer: First, make sure your data is set up properly. Second, make sure that each video was rated by two or more raters. Third, make sure you are using the proper ICC formulation. Lastly, make sure you are using the ICC function properly.

You should set up your data as a matrix with each rater as a separate column and each row as a separate video. So you should end up with a $69\times130$ matrix. Make sure that your missing cells (i.e., rater-video combinations) are marked as "missing" in whatever program you are using.
In most functions, each video needs to be rated by two or more raters. Other videos should be thrown away (or a specialized function that accounts for this needs to be used).
I would not recommend ICC(1) for your described purpose. This formulation assumes that raters are not a meaningful source of variance, which they almost certainly are. Instead, you should use ICC(A,1) or ICC(C,1); these are sometimes called two-way mixed models for single-measures and either absolute agreement or consistency. Use ICC(A,1) if you want the raters to use the exact same scores and be fully interchangeable; use ICC(C,1) if you're okay with each rater having their own mean.
This will depend on the exact function you are using, but make sure you send the data to the function in the exact way that it expects.

If you use MATLAB, I have made the ICC_A_1 and ICC_C_1 functions available.
If you use SPSS, you can use the RELIABILITY function.
Give it a shot; if you run into problems, feel free to send me the data and I can do it for you (or at least seeing the data will give me more insight into the problem).


Quais as diferença entre AlarmClock e AlarmManager?

Question: Quais as diferenças entre o AlarmClock e o AlarmManager?
Comment: só pra saber, em qual das categorias se enquadra na parte de repostas ruins?
Answer: As classes têm propósitos diferentes:
AlarmClock disponibiliza um conjunto de constantes cuja função é facilitar a criação de um Intent para lançar/controlar uma aplicação, existente no dispositivo, que responda aos Intent ACTION_SET_ALARM e ACTION_SET_TIMER.
O seguinte código chama a aplicação padrão, ou pede para escolher uma, e regista um alarme para as 15:30.
Intent createAlarm = new Intent(AlarmClock.ACTION_SET_ALARM);
createAlarm.putExtra(AlarmClock.EXTRA_HOUR, 15);
createAlarm.putExtra(AlarmClock.EXTRA_MINUTES, 30);
startActivity(createAlarm);

AlarmManager é uma classe que
permite registar um Intent para lançar a sua aplicação(Activity), serviço ou BroadcastReceiver, a determinado dia e hora.
O código seguinte define um alarme para lançar um BroadcastReceiver todos os dias às 10 horas:
//Definir a hora de início
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);

Intent tarefaIntent = new Intent(context, ExecutarTarefaProgramadaReceiver.class);
PendingIntent tarefaPendingIntent = PendingIntent.getBroadcast(context,1234, tarefaIntent,0);

AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

//Definir o alarme para acontecer todos os dias às 10 horas
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                 AlarmManager.INTERVAL_DAY, tarefaPendingIntent);

Alguns exemplos que usam AlarmManger:

Como executar um método em tempo determinado?
Notificação em uma certa hora sem necessidade de abrir o programa.
Notificações em tempo determinado e ao ligar celular.

Usando o AlarmManager pode criar uma aplicação tipo "despertador" para que possa ser lançada/controlada por Intents criados com a ajuda do AlarmClock.
Answer: Apesar de serem duas classe totalmente distintas, veja abaixo uma breve explicação sobre cada uma:
AlarmClock
{public final class AlarmClock }
Contém um conjunto de constantes e extras que podem ser usados para iniciar uma activity para definir um novo alarme em um aplicativo de "despertador".
Exemplo:

AlarmClock.EXTRA_HOUR: Hora do alarme a ser ajustada
AlarmClock.EXTRA_MINUTES: Minuto do alarme a ser ajustado
AlarmClock.EXTRA_DAYS: Dias do alarme a ser ajustado
AlarmClock.EXTRA_MESSAGE: Mesagem do alarme
AlarmClock.EXTRA_RINGTONE: Um toque para ser disparado no alarme
AlarmClock.EXTRA_VIBRATE: Se irá vibrar
outros

Como é usado:
Intent alarme = new Intent(AlarmClock.ACTION_SET_ALARM);
alarme.putExtra(AlarmClock.EXTRA_HOUR, 10);
startActivity(alarme);

AlarmManager
{public class AlarmManager  }
Permite agendar sua aplicação para ser executado em algum instante futuro. Quando um alarme dispara, uma intensão é transmitida pelo sistema, então seu aplicativo responde a essa intenção de transmissão e executa uma ação, tal como abrir o aplicativo, notificar o usuário via notificação na barra de status ou realizar outro tipo de ação.
Em vários tipos de aplicativos, necessitamos que alguma ação seja agendada para ser executada algum tempo depois, ou que seja feita periodicamente fora do ciclo de vida de sua aplicação. Para isso o sistema Android disponibiliza a o recurso de Alarm através da classe AlarmManager que utiliza melhor os recursos do sistema do que se tivéssemos controlando através ou Timer.
Como é usado:
AlarmManager alarme=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i=new Intent(context, OnAlarmReceiver.class);
PendingIntent pi=PendingIntent.getBroadcast(context, 0, i, 0);
alarme.setRepeating(
    AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), PERIOD, pi);

Para saber mais detalhes, consulte a documentação sobre AlarmClock e AlarmManager.


Finding the total number of triangle

Question: I am trying to calculate the number of triangles in a simple graph. I am totally aware of the upper bound for a complete graph $K_n$.  That is, the number of ways of choosing three vertices is
$$
\binom{n}{3}=\frac{n(n-1)(n-2)}{6}\sim\frac{n^3}{6}\text{ as }n\to\infty.
$$
That's $O(n^3)$ is the bound for the number of triangles. I do have also an idea about how to calculate the expected number of triangles in a random graph. But the issue that I am facing is how to formulate the number of triangles in a simple graph i.e by taking simple examples and then generalising it. I went through this question: minimum number of triangles in a simple graph it was quite insightful but still, I am not able to succeed. Any help or hint is appreciated.
Comment: I can't imagine there's any kind of useful formula for this. You could have two graphs that differ only in one edge but have very different numbers of triangles.
Comment: Does this answer your question? https://math.stackexchange.com/questions/730294/expected-number-of-triangles-in-a-random-graph-of-size-n
Comment: Trace of the cube of the adjacency matrix divided by 6?
Comment: @GerryMyerson I agree even I'm trying to come up with a formula by adding one by one  vertices but no luck yet
Comment: @YipJungHon Yup I am aware of the method used in the link provided by you. Since I mentioned in the question I'm aware of how to calculate expected number of triangles
Comment: @Casteels hmmm...I am actually aware of the adjacency matrix but I had no idea that you can use it for calculating the number of triangles. Could you mind elaborating on your take?
Answer: The adjacency matrix is
$$A=[a_{ij}]$$
where $a_{ij}=1$ if and only if ${i,j} \in E;$ Otherwise $a_{ij}=0.$
The entries of $A^2=[a'_{ij}]$ are
$$
a'_{ij}=\sum_{k \in V} a_{ik} a_{kj}
$$
which counts the number of length 2 paths from $i$ to $j$ but double counts since the graph is undirected.
The entries of $A^3=[t_{ij}]$ are
$$
t_{ij}=\sum_{k \in V} a_{ik} a'_{kj}
$$
which counts the number of length 3 paths from $i$ to $j$. The trace of $A^3$ sums the number of triangles except all 3 permutations of triangle $\{i,j,k\}$ appear. Thus the overcount factor is $6.$

Finding the total number of triangle


TCP handshake scapy code not working

Question: I am facing a problem during TCP handshake using Scapy. My Scapy code is sending SYN packet. Target is responding with SYN/ACk Packet. I could see SYN/ACK packet in tcpdump but its not receiving at scapy code.
Note: There is no RST packet sent from linux m/c.
Here is my code snippet:
@ATMT.condition(IPCP_OPEN)
def send_syn(self):
    time.sleep(10)
    self.src_port = random.randrange(1024,65535)
    print "Sending SYN packet"
    print "Src MAC: "+self.mac+" dest MAC: "+DEST_MAC
    send_pkt = Ether(src=self.mac,   dst=DEST_MAC)/Dot1Q(vlan=self.vlan_out)/Dot1Q(vlan=self.vlan_in)/PPPoE(sessionid=1)/PPP(proto=IP_PROTO)/IP(src="22.214.171.124",dst="10.10.10.10")/TCP(sport=self.src_port, dport=80, flags="S")
    hexdump(send_pkt)
    print "src_port: "+src_port
    sendp(Ether(src=self.mac, dst=DEST_MAC)/Dot1Q(vlan=self.vlan_out)/Dot1Q(vlan=self.vlan_in)/PPPoE(sessionid=self.sess_id)/PPP(proto=IP_PROTO)/IP(src="220.127.116.11",dst="10.10.10.10")/TCP(sport=self.src_port, dport=80, flags="S"), iface=self.iface, verbose=False)
    # hexdump(pkt)
    raise self.WAIT_SYNACK()

Transitions from WAIT_SYNACK
@ATMT.receive_condition(WAIT_SYNACK)
def receive_synack(self,pkt):
if (TCP in pkt):
   print "Recvd SYN/ACK"
   hexdump(pkt)
   #self.src_port = pkt[TCP].dport
   self.seq_no = pkt[TCP].ack
   self.ack_no = pkt[TCP].seq
   print "processing SYN/ACK"
   raise self.GOT_SYNACK()

My master_filter is as follows
def master_filter(self, pkt):
return (PPPoED in pkt or PPPoE in pkt) and  (pkt[Dot1Q:1].vlan==self.vlan_out) and (pkt[Dot1Q:2].vlan==self.vlan_in)

Why my scapy code is not receiving SYN/ACK packet ?
Comment: Could you a) add information on scapy version - scapy 2.x or scapy3k; b) OS version; c) more explanation on network? Also, I would start with simple case first - e.g. just direct IP communication without PPP and Ethernet layer to check your code, and adding complexity on top of that.
Comment: Hi, Eriks, My scapy version is scapy (2.3.1) , OS version -
Comment: OS version is
[root@bngce-sw-lnx3 ~]# uname -a
Linux bngce-sw-lnx3.englab.juniper.net 2.6.18-194.el5 #1 SMP Tue Mar 16 21:52:43 EDT 2010 i686 i686 i386 GNU/Linux
Comment: Network is my Linux machine is connected to Juniper MX router.
MX(ge-0/0/1)  --------------     Linux M/c(eth1)
Answer: You're missing TCP because you're just filtering for PPPoE*. Change your filter to pkt.haslayer(TCP) or (...
def master_filter(self, pkt):
    return (PPPoED in pkt or PPPoE in pkt) and  (pkt[Dot1Q:1].vlan==self.vlan_out) and (pkt[Dot1Q:2].vlan==self.vlan_in)

TCP handshake scapy code not working


Suppose $\sum_{n=1}^{\infty}{x_n} < \infty$,$\sum_{n=1}^{\infty}{|y_n - y_{n+1}|} < \infty$

Question: Suppose $\sum_{n=1}^{\infty}{x_n} < \infty$,$\sum_{n=1}^{\infty}{|y_n - y_{n+1}|} < \infty$. Then prove that $\sum_{n=1}^{\infty}{x_ny_n}$ converges.
Since $\sum_{n=1}^{\infty}{x_n} < \infty$, by divergence test, we have  $\lim_n{x_n}=0$
Since $ \sum_{n=1}^{\infty}{|y_n - y_{n+1}|} < \infty $, we have $ \sum_{n=1}^{\infty}{(y_n - y_{n+1})} < \infty $. By divergence test. $\lim_n{(y_n-y_{n+1})}=0$. Hence, we have $|y_n - y_m| \leq |y_n - y_{n-1}| + ... + |y_{m+1}-y_m|<\dfrac{\epsilon}{N}+...+\dfrac{\epsilon}{N}=\epsilon$. Hence, we have $(y_n)$ is a cauchy sequence. Hence, $\sum_{n=1}^{\infty}{x_ny_n}$ converges.
Is this proof correct?
Answer: This is a direct consequence of the
Generalized Dirichlet Convergence Test
The standard Dirichlet Test for convergence takes two real sequences
$a_n$ and $b_n$ where the $a_n$ have bounded partial sums and the $b_n$
tend to $0$ monotonically.  We will derive a slightly more general test
using the same ideas.
Let $a_n$ and $b_n$ be two complex sequences and let
$$
A_n=\sum_{k=1}^na_k\tag{1}
$$
Using the convention that $A_0 = 0$, we have that $a_k = A_k - A_{k-1}$.
Therefore,
$$
\begin{align}
\sum_{k=1}^na_kb_k
&=\sum_{k=1}^n(A_k-A_{k-1})b_k\\
&=\sum_{k=1}^nA_kb_k-\sum_{k=0}^{n-1}A_kb_{k+1}\\
&=A_nb_n+\sum_{k=1}^{n-1}A_k(b_k-b_{k+1})\tag{2}
\end{align}
$$
Equation $(2)$ leads immediately to the following generalization of the
Dirichlet convergence test.
Lemma:
Suppose $a_n$ and $b_n$ are two complex sequences and each of the
following three conditions is satisfied
The partial sums of the $a_n$ are bounded independently of $n$; i.e.
$$
\left|\sum_{k=1}^na_k\right|\le A\tag{3}
$$
The $b_n$ are of bounded variation; i.e.
$$
\sum_{k=1}^\infty\left|b_k-b_{k+1}\right|\le B\lt\infty\tag{4}
$$
The $b_n$ tend to $0$; i.e.
$$
\lim_{k\to\infty}b_k=0\tag{5}
$$
Then, the series
$$
\sum_{k=1}^\infty a_kb_k\tag{6}
$$
converges, and the absolute value of its limit is no greater than $AB$.
Notice that condition $(4)$ above replaces the monotonicity in the standard
Dirichlet test.  In fact, it is not hard to show that any real sequence
of bounded variation is the difference of two monotonic sequences.  This
means that for real sequences, the generalized test is neither stronger
nor weaker than the standard Dirichlet test, but a sequence of bounded
variation might need to be broken into the difference of two monotonic
sequences before having the standard Dirichlet test applied.
This version of the Dirichlet test becomes more useful when checking
the convergence of complex series.

Critique of the proposed answer
Point 1: You had that
$$
\sum_{n=1}^\infty|y_n-y_{n+1}|\lt\infty
$$
There is no need to remove the absolute values, in fact
$$
\sum_{n=1}^\infty(y_n-y_{n+1})\lt\infty
$$
leaves the possibility that the sum is unbounded below.
Point 2: You use the fact that $|y_n-y_{n+1}|\to0$ to derive that $y_n$ is a Cauchy sequence. This is not true. Consider $y_n=\sqrt{n}$:
$$
\begin{align}
|y_n-y_{n+1}|
&=\sqrt{n+1}-\sqrt{n}\\
&=\frac1{\sqrt{n+1}+\sqrt{n}}\\[3pt]
&\to0
\end{align}
$$
however, $y_n$ is not Cauchy.
Answer: Let $N\ge2$ then by telescoping
$$y_N=y_1-\sum_{n=1}^{N-1}(y_n-y_{n+1})$$
hence the sequence $(y_N)$ is convergent since $\displaystyle \lim_{N\to\infty}\sum_{n=1}^{N-1}(y_n-y_{n+1})=\sum_{n=1}^{\infty}(y_n-y_{n+1})<\infty$ so $(y_n)$ is bounded say by $M>0$ and by $$\inf(Mx_n,-Mx_n)\leq x_ny_n\leq\max(Mx_n,-Mx_n)$$
and $\displaystyle \sum_{n=1}^\infty x_n$ is convergent we have the result.


An ODE problem that seems weird!

Question:
Well, as you can see in the image above, the problem consists of a Linear-Non Homogeneous ODE. It goes on and tasks the student with transforming such ODE into a Linear 4th Order ODE containing only the variable Y1. It states then that this ODE is Non-Homogeneous as well.
However as I was solving the first step of the problem ( using the procedure described in the link: http://homepage.ntu.edu.tw/~ihwang/Teaching/Fall13/Slides/DE_Lecture_07_handout_v2.pdf) I stumbled upon a big issue: the new ODE that had arisen was Homogeneous! Am I doing anything wrong? Thanks in advance
Answer: No, you're not doing anything wrong (that I can see).  A linear DE is homogeneous iff $0$ is a solution.  It happens in this case that $Y = \pmatrix{0\cr 0\cr 0\cr 1\cr}$ is a solution of your system  (because $-\phi$ happens to be the fourth column of $A$).  Therefore any linear DE for $Y_1$
(or, for that matter, $Y_2$ or $Y_3$) will have to be homogeneous.
The procedure to produce a higher-order linear DE from a nonhomogeneous system will generally produce a nonhomogeneous DE, but occasionally that DE will turn out to be homogeneous.
Comment: Ummm so I implemented such method in a small Matlab script and I received the following answer for Y1: D^4*Y1 +0.3D^3*Y1 + 0.3D^2*Y1 - D^3 = 0. What is the meaning of the D all by itself? @RobertIsrael
Comment: That depends on what you put in your Matlab script (probably not the right thing...)
Comment: Here follows the code!

syms D y1 y2 y3 y4

Lmatrix= [ (D-1) 2 1 1; -0.5 D+1 0.5 0.5; -0.1 -0.1 D+0.1 0.1; -0.2 0.1 0.2 D+0.2];

L = det(Lmatrix);
phi = [ 1 0.5 0.1 0.2]';
phinew = sym(phi);

g1matrix=[phinew,Lmatrix(:,2:4)];
g2matrix=[Lmatrix(:,1),phinew,Lmatrix(:,3:4)];
g3matrix=[Lmatrix(:,1:2),phinew,Lmatrix(:,4)];
g4matrix=[Lmatrix(:,1:3),phinew];

g1 = det(g1matrix);
g2 = det(g2matrix);
g3 = det(g3matrix);
g4 = det(g4matrix);

expand(L*y1-g1)
Comment: Sorry forgot to mark you in the comment! @RobertIsrael
Comment: I'm not sure what you think that code is doing, but it's certainly not finding the DE for Y1.

An ODE problem that seems weird!


Layout is being overlapped by buttons. How do I hide the buttons?

Question: I have been learning for a couple of weeks and have been struggling with layouts in this situation. It's just a small project intended for learning. Button upg calls the LinearLayout ShopL as a popup window but it overlaps.Basically i want the red buttons to be behind(hidden from) the blue Linearlayout (see picture).
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/background_dark"
    tools:context="com.example.daniela.test003.MainActivity"
    tools:layout_editor_absoluteY="81dp">

    <TextView
        android:id="@+id/Total"
        android:layout_width="287dp"
        android:layout_height="25dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Total Geld"
        android:textColor="@android:color/holo_green_dark"
        android:textSize="24sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/perClickShower"
        android:layout_width="188dp"
        android:layout_height="25dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="perClickShower"
        android:textColor="@android:color/holo_green_dark"
        android:textSize="18sp"
        app:layout_constraintBottom_toTopOf="@+id/perSecondShower"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.262"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/Total"
        app:layout_constraintVertical_bias="0.85" />

    <TextView
        android:id="@+id/perSecondShower"
        android:layout_width="188dp"
        android:layout_height="0dp"
        android:layout_marginBottom="372dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:text="perSecondShower"
        android:textColor="@android:color/holo_green_dark"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.262"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:background="@android:color/holo_red_dark"
        android:text="btn1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/upg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@android:color/holo_red_dark"
        android:text="upg"
        app:layout_constraintBottom_toTopOf="@+id/btn1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.16"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/perSecondShower"
        app:layout_constraintVertical_bias="0.892" />

    <LinearLayout
        android:id="@+id/ShopL"
        android:layout_width="300dp"
        android:layout_height="450dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@android:color/holo_blue_dark"
        android:visibility="visible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="81dp"
            android:layout_marginTop="159dp"
            android:layout_weight="0"
            android:baselineAligned="false"

            android:text="Button" />
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

Answer: If you run this layout on multiple phones, you'll notice that the "bad" behavior (the buttons appearing even though they're "covered" by the linear layout) will only happen on phones with Android Lollipop or newer.
Lollipop introduced the concept of "elevation", and all buttons on Lollipop+ have elevation by default. Pressing on one of these buttons also adds elevation for the duration of the press (that's why the shadow the button casts on the background appears to grow).
Elevation works just like the real world; things with higher elevation appear above things with lower elevation. Your linear layout doesn't define an elevation value, and linear layouts don't have elevation by default, so its elevation is 0dp. A button in the default resting state has an elevation of 2dp. As long as the button's elevation is greater than the linear layout's, the button will appear "in front" of the linear layout.
An easy solution would be to add some large amount of elevation to your linear layout:
android:elevation="12dp"

This can have other side-effects, but it's possible it will just work for you.


Clustering / Grouping on image's pixels

Question: I have an image, and im building a model to recognize a pattern in that image and classify it. There is however a lot of noise in the rest of the image, but the actual pattern to classify will always be the biggest continuous figure in the whole image.
I want to pre process to isolate this bit, before running it in something like a CNN, and I was wondering if standard libraries had an algorithm that was able to find this 'longest non-zero streak' in my image matrix.
Answer: What you're describing seems to fall under image segmentation. There are many algorithms for it, some listed on wikipedia page (actually some of them perform clustering on pixel values). If you're interested in using deep learning for that, you can check out kaggle, since there are many competitions on segmentation, and you can find architectures and pretrained models there.
If you're interested in Python, you might want to check out scikit-image. It even has a tutorial on image segmentation.
Comment: Would rather do it as a simpler pre processing before pushing it to my Neural Net. Any of those come out of the box? Couldn't find any on sklearn or numpy.
Comment: I've included a link for an implementation in my answer.
Comment: Amazing, I'll look it up. For the record I voted up, but too noob on this channel to notice
Comment: No problem, good luck. Would you consider editing question title to something like "clustering/grouping on image's pixels"? It might be more useful if someone had a similar question (dbscan is pretty specific).

Clustering / Grouping on image's pixels


Date with time not viewing on Form

Question: If I set a date programatically with this code:
$currentDateString=date("Y-m-d H:i:s", strtotime($current_date));
 $static_media->set('field_posted' , $currentDateString);

the data in field is there , when I do a get-> i can see the date, but it does not show up in the edit view of the entity. (just shows as blank)
If I modify the date in the edit view, when I do a 'get->' the date comes out with the EDT timezone in the time , and then it does show up in the edit form.
I tried inserting it with the EDT Timezone, but I get a message that the data is too long
Drupal\Core\Entity\EntityStorageException: SQLSTATE[22001]: String data, right truncated: 1406 Data too       [error]
long for column 'field_pup_posted_value' at row 1: INSERT INTO {media__field_pup_posted} (entity_id,
revision_id, bundle, delta, langcode, field_posted_value) VALUES (:db_insert_placeholder_0,
:db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4,
:db_insert_placeholder_5); Array
(
    [:db_insert_placeholder_0] => 562729
    [:db_insert_placeholder_1] => 1045632
    [:db_insert_placeholder_2] => published_products_static
    [:db_insert_placeholder_3] => 0
    [:db_insert_placeholder_4] => en
    [:db_insert_placeholder_5] => 2017-06-08EDT17:19:31
)

Answer: I had to insert it with a T in between the date and time as follows:
$currentDateString=date("Y-m-d", strtotime($current_date))."T".date("H:i:s", strtotime($current_date));

Date with time not viewing on Form


Training separate models for sets of features?

Question: I'm wondering if it is sensible to train different models for different sets of features (I do not mean one model for each feature).
Say I have 4 features - 2 are nominal while the other two are 2 continuous.
Does it make sense to train a model on 2 nominal attributes to make a prediction and another model based on the remaining 2 attributes? In the end one can ensemble the results (say by voting).
What I'm suggesting is a sort of vertical stacking. Is this a valid method? If yes, is there a formal term for it so I can read up more?
Answer: Yes, depending on the method.  What you are describing above is part of how a Random Forest (a type of ensemble) works.  Each tree in the forest is trained using only a subset of the features (see the mtry argument in R's randomForest package, for example).  In a Random Forest, each decision tree is also trained on a subsample of the training set, which adds additional randomness, and contributes to added diversity among the base models (trees) in the ensemble.
Using a subset of features for each constituent model of the ensemble can also be helpful in stacking.


Mostrar mensagem na tela caso não houver ligação a Internet

Question: Criei uma app android que consome dados de uma webservice rest feita em Php, até agora tudo funcionando perfeitamente. O que eu quero agora é mostrar uma mensagem na tela caso não estiver com acesso a Internet ou seja informar ao utilizador para ligar a Internet caso ele se esqueça. Alguém pode me ajudar com isso. obrigado desde já :).
Segue aqui o meu código.
public class MainActivity extends AppCompatActivity  {
    String myJSON;
    Handler handler;
    private Context context;
    private static final String TAG_RESULTS="resultado";
    private static final String TAG_ID = "id";
    private static final String TAG_NAME = "name";
    private static final String TAG_ADD ="address";

    JSONArray peoples = null;

    ArrayList<HashMap<String, String>> personList;
    ListView list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        list= (ListView) findViewById(R.id.listView);
        personList = new ArrayList<HashMap<String,String>>();
        getData();

    }
    public void restartActivity(View view)
    {
        // do your work Here
        Intent intent= new Intent(MainActivity.this, MainActivity.class);
        startActivity(intent);
    }

    public void getData() {

        class GetDataJSON extends AsyncTask<String, Void, String> {
            @Override
            protected String doInBackground(String... params) {
                    BufferedReader inputStream = null;
                    String result = null;

                    try {

                        URL url = new URL("http://10.127.127.1/ws/aula.php");
                        HttpURLConnection con = (HttpURLConnection) url.openConnection();
                        StringBuilder sb = new StringBuilder();
                        inputStream = new BufferedReader(new InputStreamReader(con.getInputStream()));
                        String line = null;
                        while ((line = inputStream.readLine()) != null)
                        {
                            sb.append(line + "\n");
                        }

                        result = sb.toString();
                    } catch (Exception e) {

                    }
                    finally {
                        try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
                    }
                    return result;
                }
                @Override
                protected void onPostExecute(String result){
                    myJSON=result;
                    showList();
                }
            }
            GetDataJSON g = new GetDataJSON();
            g.execute();
        }

        protected void showList(){

            try {
                JSONObject jsonObj = new JSONObject(myJSON);
                peoples = jsonObj.getJSONArray(TAG_RESULTS);
                for(int i=0;i<peoples.length();i++){
                    JSONObject c = peoples.getJSONObject(i);
                    String id = c.getString(TAG_ID);
                    String name = c.getString(TAG_NAME);
                    String address = c.getString(TAG_ADD);
                    HashMap<String,String> persons = new HashMap<String,String>();
                    persons.put(TAG_ID,id);
                    persons.put(TAG_NAME,name);
                    persons.put(TAG_ADD,address);
                    personList.add(persons);
                }

                ListAdapter adapter = new SimpleAdapter(
                        MainActivity.this, personList, R.layout.list_item,
                        new String[]{TAG_ID,TAG_NAME,TAG_ADD},
                        new int[]{R.id.id, R.id.name, R.id.address}
                );

                list.setAdapter(adapter);

            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(context, "Error parsing JSON data.", Toast.LENGTH_SHORT).show();
            }

        }

}

Answer: Coloca esse código dentro do onPostExecute
ConnectivityManager cm =
                (ConnectivityManager)getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        boolean isConnected = activeNetwork != null &&
                activeNetwork.isConnectedOrConnecting();
        if (isConnected == true){
          Toast.makeText(getApplicationContext(), "Conectado.", Toast.LENGTH_LONG).show();
        }else{
            Toast.makeText(getApplicationContext(), "Não foi possível conectar. Cheque a internet e tente novamente.", Toast.LENGTH_LONG).show();
        }

Comment: Colocei no OnPostExecute e funcionou bem..Valeu o problema estava era  no meu código inicial.
Comment: Coloquei esse código na aplicação e funcionou pela metade, ele notifica quando está conectado a Internet, ate ai tudo bem, mas não estou conseguindo que mostre a mensagem " Não foi possível conectar" porque a aplicação para de funcionar quando não consegue conectar com a Internet. Tem algo de errado com o meu código inicial e não estou conseguindo localizar. Alguma ajuda??
Comment: Esse código tem que rodar no OnCreate antes de chamar o seu getData().
Comment: Como posso fazer isso??
Comment: Eu uso ele no`OnPostExecute` e funciona normal, tanto com como sem internet, @YanikSantos. Mas tenta fazer como o Márcio disse, coloca no `onCreate`. Se possível, posta o erro que dá tb.


Combine display block with opacity fade in (bootstrap tabs)?

Question: I have elements with 2 classes (fade and hidden)

.fade {
    opacity: 0;
    -webkit-transition: opacity .15s linear;
    -o-transition: opacity .15s linear;
    transition: opacity .15s linear;
}

.hidden {display: none;}

When clicked, a class gets added (.in and .shown)
.fade.in {
    opacity: 1;
}

.shown {display: block !important;}

My issue is, by doing the display: block, there is no animation with opacity. Just the element showing in full.
Anyway to keep the animation fade with opacity?
Comment: Why not opacity alone??
Comment: @FridayAmeh Because opacity won't hide the margins and padding, so the elements still take up space in the browser.
Comment: Possible duplicate of [Transitions on the display: property](https://stackoverflow.com/questions/3331353/transitions-on-the-display-property)
Comment: See second answer.
Comment: @tavkomann That answer won't work because visibility: hidden; won't work for my situation.
Comment: Second answer does not use `visibility: hidden` but `opacity` and `height`.
Comment: @tavkomann Still doesn't work. Using height doesn't get rid of spacing that margins take up on the page.
Answer: Instead of using display, one could use position: absolute. See the following example:

.fade {
  opacity: 0;
  transition: opacity 1s linear;
}

.hidden {
  position: absolute;
  pointer-events: none;
}

.fade.in {
  opacity: 1;
}
<button onclick="document.getElementById('example').className = 'fade in';">Click</button>

Example

Text...


Observation about Sylow's theorem and the index of Sylow p-group

Question: Let G be a finite group of order $p^{r}m$ where $p \in \mathbb{P}, r>0$ and $p$ does not divide $m$.

One of Sylow's theorem is this:
If $n$ is the number of Sylow p-subgroups then $n$ divides $m$ and $n \equiv 1 \pmod{p}$.

I made an observation and wonder if they are true.
Since p-group/ Sylow p-subgroups are contained in G, G contains at least one p-group/Sylow p-subgroup. In general, there are m p-group/Sylow p-subgroup. Is this correct?
Secondly, by the above theorem I have stated, how does the fact that n divides m denote the index of the p-group/ Sylow p-subgroup in G?
The index of a subgroup in a group G is the distinct number of the subgroup in G. This is given by the fact that the order of the subgroup divides the order of a group G.
Greatly appreciate if anyone would illuminate my doubts.
Thanks in advance.
Comment: It is **not** the case that in general $m$ is the number of Sylow $p$-subgroups. Alas, I cannot understand your remaining questions.
Comment: However, $m$ is indeed always the index of a $p$-Sylow subgroup of $G$. This is simply because a $p$-Sylow subgroup has order $p^r$ by definition.
Answer: As to your first question, as noted in a comment, consider for instance $S_{3}$, and take $p = 3$, so $m = 2$, but $n = 1$.
As to

The index of a subgroup in a group $G$ is the distinct number of the subgroup in $G$.

you probably mean

The index of a subgroup $H$ in a group $G$ is the number of distinct left, say, cosets of the subgroup $H$ in $G$.

Note that this number is denoted by $\lvert G : H \rvert$, and it is called the index of $H$ in $G$.
The reason $n \mid m$ (which appears to be what you are asking) is that $n = \lvert G : N_{G}(P) \rvert$, where $P$ is a Sylow $p$-subgroup, and $N_{G}(P)$ is its normalizer in $G$. Now since $P \le N_{G}(P)$ we have by Lagrange
$$
m = \lvert G : P \rvert = \lvert G : N_{G}(P) \rvert \cdot \lvert N_{G}(P) : P \rvert = n \cdot \lvert N_{G}(P) : P \rvert,
$$
so that $n \mid m$.
Comment: Thank you very much. Something for me to digest.


Extract number of users connected to IPSsec/L2tpd server

Question: We have a new corporate VPN server running Ipsec (Openswan) + xl2tpd + pppd. When a user connects, pppd runs the /etc/ppp/auth-up script which logs the time and username that has connected to /var/log/pppstats. However, on our older OpenVPN system we were able to finger the OpenVPN daemon to query the number of currently connected users and this is a metric which our management require to be reported via our Zabbix monitoring servers.
It would seem relying on the log file to analyze the number of connected users to be extremely unreliable since if a user shuts down their machine without terminating the connection then we have an eternally connected user. Any ideas on how this metric could be extracted reliably as possible?
Answer: One option would be to check the output of netstat.  Even something as simple as this should work (it would probably need updating for the exact service names:
<code>netstat -ano | grep poptop | wc -l
</code>
Comment: Actually that helped me find the solution. netstat -i | grep "ppp" | wc -l gives me a count of connected users since pppd creates a new virutal interface for each connected user. Thanks!


Identify 2 lines on a circle, then find length of arc of those 2 points

Question: I'm not very good at math, so please be kind.
Let's say that i have the a line called q and that line goes through the circle C
The line has been drawn with 2 coordinates, which then I calculate the angle of the line using arctan and then drawing it with cos and sin (you might ask yourself why i do that but its necessary)
So now what I need to figure out is the 2 points where q intercepts the Lines of circle C, then calculate both ways around the circle and be able to set points around the circle, as i would wish to
Here's a little picture i tried to do idk if it helps
https://i.stack.imgur.com/8jWzJ.png (i cant post images yet bruh)
if you have any other questions and you feel very kind, feel free to ask instead of giving me a downvote or something idk
Comment: By two coordinates of a line, do you mean the slope and the y-intercept?
Comment: @mathlander well what i originally used to draw the line was using 2 coordinates, then taking arctan of y2-y1, x2-x1, then using the angle i got to draw the line: so x1 and y1 is the origin, then x2 = x1 + l * cos(arctan), y2=y1 + l * sin(arctan)
Answer: From the two coordinate points you use to draw q, you can find the equation of q (y=mx+b, with m=(y2-y1)/(x2-x1)) (I don't know how to write equations on math exchange I'm sorry.) If you know the coordinate of the center of your circle and its radius, you can find the equation of the circle (x−x1)^2+(y−y1)^2=r2.
If you want to find the points where they intersect, you can set these two equations equal to each other, and solve to find the coordinates of your two intersection points.
Finding the centre angle between the two intersection points now is a bit more difficult. You can take one of the line segments between your center and a intersection point as 'new' x-axis with the origin of your coordinate system coinciding with your circle center. The you can describe the position of your second intersection point in terms of this new coordinate system and then use arctan(y/x) for you center angle.
Now for the arc you just multiply this center angle by the radius of the circle.


Difference between polynomial and linear combination

Question: $$
P=a_0+a_1x+a_2x^2+\cdots+a_nx^n\\
V=a_1v_1+a_2v_2+a_3v_3+\cdots+a_nv_n
$$
Why is Polynomial infinite dimensional here, but the linear combination is not?
Or are there any difference between these two expressions based on both have the similar form.
Thanks
Answer: The reason that the space of polynomials is considered infinite dimensional is that while a single polynomial can only have finitely many terms, each of finite degree, there is no bound on how many terms it can have, or which degree those terms can have.
In your example, you've stopped the polynomial at degree $n$, and if we can only use up to and including degree $n$, then polynomials become a finite-dimensional vector space (of dimension $n+1$, to be specific). But without any such restriction, the infinitely many polynomials $1, x, x^2, x^3, \ldots$ are all linearly independent, and as such the vector space is infinite dimensional.
There is nothing stopping you from doing the same thing with "regular" vectors, i.e. lists of numbers. You can allow addition of two vectors with different number of entries by padding the shortest one with $0$'s on the right until they have the same length, and then add them together the usual way. Also, we may requirre that any trailing zeroes on the right side of a vector are cut (except the final zero if all entries are zero). So, for instance
$$
(1, 2, 3) + (1, 0, 0, 1, 2) = (1, 2, 3, 0, 0) + (1, 0, 0, 1, 2) = (2, 2, 3, 1, 2)\\
(1, 2, 3) + (5, 4, -3) = (6, 6, 0) = (6, 6)
$$
(scaling is done the normal way). This will make a vector space that is entirely analoguous to the space of polynomials, only expressed the way you are used to seeing vectors. It also has the geometric interpretation of including $\Bbb R$ as the $x$-axis in $\Bbb R^2$, including $\Bbb R^2$ as the $xy$-plane in $\Bbb R^3$, and so on all the way up through the dimensions, and looking at the final union of all these vector spaces as a single vector space.
This is, in some sense, the smallest infinite-dimensional vector space you can make. Unlike many other, "larger" infinite-dimensional vector spaces, this one actually has a basis even without invoking Zorn's lemma. That means we can actually know what the elements of the basis are.
Comment: thanks for answer question @Arthur
Comment: From linear algebra done right by Alex, List of vectors, which is vector space was mentioned as a finite, For example F2's length is 2. But F(infinite) is infinite length. Is contradiction here cause list has length?


Nginx use path as PHP parameter

Question: I want to use URL path as URL param
here is what I want to achieve
whenever a user enters example.com/abc then the request should automatically redirect to example.com/?abc without modifying the URL at the user's end. Currently nginx treating /abc as path and throwing 404.
Additionally, existing files should not be redirected, e.g. example.com/demo.php should be served as it is.
Answer: You need to use a rewrite engine... for example like this (in server config):
After some research, this worked for me -
location / {
...
   if (-f $request_filename) {
        break;
    }
    rewrite ^/([^?].*) /?$1 permanent;

}

OLD answer -
server {
    ...
    if (!-f $request_filename){
       rewrite ^/(.*) /?$1;
    }

    ...
}

Comment: Thank you for the quick answer but it is redirecting all URLs including existing files. I don't want to redirect files let's say `domain.com/hello.php` should be served as it is. How do I achieve that?
Comment: @PritamPawade edited, also changed a bit regex, so it wouldn't count anything that starts with `?` - that may not be important (as it probably is counted as an argument, not url) but, depending on the rest of the server configuration, it could be
Comment: Thanks but it didnt work
Comment: @PritamPawade what didn't work? does the file exist? Because the if checks if the file exists. Try now.
Comment: Thanks for the help but I made some research myself and this worked for me, updating your answer, so it will help someone in future
Answer: Try the following:
server {
    root /example/path;
    server_name example.com;

    try_files $uri $uri/ /index.php?$uri;
}

This will make first nginx check if a file or directory matching the request URI part exists, and nginx serves it.
If there is no match, then the request is passed to /index.php with URI part as query argument.
Comment: Thanks, but it didn't work
Comment: Please explain in more details, what exactly you tried, what was the result and what was the expected result.

Nginx use path as PHP parameter


What statistical analysis should I use for Likert-Scale data?

Question: I am trying determine if there is a significant difference in preference of activities between males and females.I'm using a survey with the following question on 7-point likert scale:
Question 1: I like activity 1. Strongly disagree ---- Strongly agree
Question 2: I like activity 2. Strongly disagree ---- Strongly agree
Question 3: I like activity 3. Strongly disagree ---- Strongly agree
What statisical analysis could I use to determine if there is a significant difference in preference and what activity is most prefered by the sexes?
Comment: This is a frequently asked question. Please read the linked thread. If you still have a question after reading that, come back here & edit your Q to state what you've learned & what you still need to know. Then we can provide the information you need rather than just duplicating material elsewhere that didn't help you.
Answer: When the response options are linked to specific categorical responses, the Likert data are ordinal and should be analyzed with non-parametric statistics because the distances between the response options are not consistent. For example, the distance between strongly agree and agree is not necessarily the same as the distance between agree and neutral. The only exception to this rule is when you aggregate questions. When you combine 2 or more questions into an aggregated score then it is fine to use parametric analyses when comparing scores. However, questions that are aggregated should load onto the same factor in a principal components analysis.


Estou a ter um problema ao carregar uma imagem com Multer no servidor, o ficheiro carrega para a 'path' mas não para a base de dados

Question: Na minha máquina funciona perfeitamente, a imagem é carregada para a path e todos os dados para a base de dados mas no servidor ubuntu com o mesmo código e a mesma base de dados apenas a imagem é carregada para a path e nenhum dos dados é carregado para a db.

const path = require('path')
const multer  = require('multer')

const uploadImg = function (req, res) {

  let file_name = ''
  var values = [];

  const storage = multer.diskStorage({

      destination: path.join(__dirname, '../../public/imgcomment/'),
      filename: function(req, file, cb){
        const uni = new Date().getTime();
        file_name = uni + '-' + file.originalname
        cb(null, uni + '-' + file.originalname);
      }

  });

  const upload = multer({
  storage: storage,
  fileFilter: function(req, file, cb){
      checkFileType(file, cb);
  }
  }).single('images');

// Check File Type
  function checkFileType(file, cb){

    // Allowed ext
    const filetypes = /jpeg|jpg|png|gif/;
    // Check ext
    const extname = filetypes.test(path.extname(file.originalname).toLowerCase());
    // Check mime
    const mimetype = filetypes.test(file.mimetype);

    if(mimetype && extname){
      return cb(null,true);
    } else {
      cb('Error: Images Only!');
    }

  }

  upload(req, res, (err) => {

    const nome = req.body.nome;
    const descricao = req.body.descricao;
    const preco = req.body.preco;
    const category = req.body.category;

      if(err){

        console.log(err)

      } else {

        if(req.file == undefined){

          console.log('Error: No File Selected!')

        } else {

          values.push([nome, descricao, preco, file_name, category]);
          console.log(values)

          db.query('INSERT INTO produtos (nome, descricao, preco, images, categoria) VALUES ?', [values], function (err, result) {
            if (err) {
              error = 'SQL ERROR ' + err.sqlMessage;
            }
            else {
              error = 'Blog added successfully';
            }
            req.flash('success', error);
            res.redirect('/todolist');
          });

        }
      }
    })
}

module.exports = {
    uploadImg: uploadImg
}

Alguém sabe o que pode causar este problema? muito obrigado
Answer: Provavelmente é porque você precisa dos dados e do arquivo ao mesmo tempo e no server o parsing pode demorar um pouco. Experimente usar o método any ao invés do single no multer.


Typo3 styles.content.getLeft

Question: actually i work on a typo3 template on Typo 7.6.12.
My Problem is that I am not able to get my content from the columns Left, Right or Border. Only normal is working.
My idea is, that the user can edit a footer-element on the left column and the basic content on the normal column.
Thats my code:
...
10 = FLUIDTEMPLATE
10 {
file = fileadmin/templates/gtstemplate/index.html
layoutRootPath = fileadmin/templates/gtstemplate/layouts/
partialRootPath = fileadmin/templates/gtstemplate/html/partials/

variables {
    content < styles.content.get

    footer < styles.content.getLeft
    footer.slide = -1
  }
}
...

and my html code.

    <f:format.html parseFuncTSPath="">{content}</f:format.html>

  <f:format.html parseFuncTSPath="">{footer}</f:format.html>
  <f:debug title="Results of customers query">{footer}</f:debug>

With footer.slide = -1 i want the footer module to be inherited on all pages.
It's really confusing, cause the normal column is working with any problems, if I use the modules on it. But any other column isn't working.
The Debug puts a NULL out. With older Typo3 versions it worked without problems
CSS Content is included.
Would be cool if someone has any idea.
The Best ans thank you.
Answer: Have you included css_styled_content or fluid_styled_content? fluid_styled_content is the state-of-the-art extension, widely compatible to css_styled_content.
fluid_styled_content has no getLeft component! You can easily define it yourself:
styles.content {
  getLeft < .get
  getLeft.select.where = colPos = 1
}

Use the TypoScript Object Browser to examine the styles component for details.
Comment: Its worse to mention, that even css_styled_content in Version 8.7 dropped that. So even if you still use css_styled_content with TYPO3 8 LTS you have to use this solution.

Typo3 styles.content.getLeft


Missing php5-fpm after PHP5.5 upgrade

Question: I just updated my PHP on Ubuntu 12.04

Now, with all weirdness, I did a simple phpinfo.php on my server, then this 502 Bad Gateway starts appearing. I check the /etc/nginx/sites-available/default nginx configuration file, and it seems that everything is normal:
server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        root /usr/share/nginx/html;
        index index.php index.html index.htm;

        # Make site accessible from http://localhost/
        server_name localhost;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ /index.html;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }

        location /doc/ {
                alias /usr/share/doc/;
                autoindex on;
                allow 127.0.0.1;
                allow ::1;
                deny all;
        }

        # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
        #location /RequestDenied {
        #       proxy_pass http://127.0.0.1:8080;
        #}

        #error_page 404 /404.html;

        # redirect server error pages to the static page /50x.html
        #
        #error_page 500 502 503 504 /50x.html;
        #location = /50x.html {
        #       root /usr/share/nginx/html;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

                # With php5-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
                # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
                deny all;
        }
}

Not sure what the problem is, my PHP is fine before I did that upgrade.
EDIT
Just noticed, I go to the directory of /var/run/php5-fpm.sock but did not see any php5-fpm.sock in there. Is this the cause?
Comment: Check if php-fpm service is up and running.
Comment: @LeandroGarcia: Probably not running in that case (you can try again with just `sudo netstat -pant` in case the process has a different name); you can cross check with `ps -ef | grep php`. If it is running, but not listening, kill the running process(es). Presuming that php5-fpm is the service name (probably is), the (re)start/stop syntax is correct. Things to check include: your php-fpm logs (`/var/log/php-fpm.log` or similar) and what your init script is doing.
Comment: Is PHP-FPM actually running (and listening on the specified port)? (`netstat -pant | grep php`). If not restart it (stop it first if needed)
Comment: I tried doing `sudo service php5-fpm start` but nothing happens, same to `sudo service php5-fpm stop` and `sudo service php5-fpm restart`. Am I doing it right?
Comment: @cyberx86 - Did this `sudo netstat -pant | grep php` but results to no output.
Comment: @cyberx86 - `sudo netstat -pant` shows no relevance for php5-fpm. `ps -ef | grep php` shows `user1 12008  4395  0 01:32 pts/0    00:00:00 grep --color=auto php` php is in red color. While `/var/log/php-fpm.log` just contains error log file _re-opened, Finishing..., exiting, bye bye!_
Comment: So, (as expected) php-fpm isn't running - you need to start it. There should be an init script in `/etc/init.d` (probably `/etc/init.d/php5-fpm`). You can run it directly (/etc/init.d/php5-fpm start) (but the result should be the same as running `service ...`. If it doesn't start php-fpm for you, open the file, and see if you can redirect any errors to the console (e.g removing any references to /dev/null);
Comment: Just to address your update (the netstat command was actually for php-fpm listening on a port (which yours isn't - didn't see that line was commented out in your config) - but, your problem is that php-fpm isn't even running; your .sock file should be recreated once you start php-fpm.
Comment: @cyberx86 - Did this `/etc/init.d/php5-fpm start` but no avail. I don't quite understand what you mean by _if you can redirect any errors to the console (e.g removing any references to /dev/null)_.
Comment: let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/9887/discussion-between-cyberx86-and-leandro-garcia)
Answer: Check that PHP-FPM is running:
ps -ef | grep php

In your case, it wasn't running.
Check that it is installed:
dpkg -s php5-fpm

In your case, the package was marked as 'Status: deinstall'.
The cause of this was that you upgraded PHP from a PPA (ppa:ondrej/php5), which caused your original copy to be removed. You might have to reinstall any non-core packages to regain that functionality.
To add back php-fpm, you can run:
apt-get install php5-fpm

In your case, this failed with:
php5-fpm : Depends: libsystemd-daemon0 (>= 31) but it is not installable

Since the publisher of the PPA you used has another PPA (ppa:ondrej/systemd) that provides this package, install that to resolve the missing dependencies, and re-run the install.
Once complete, verify your install was successful and that php-fpm is running.
Comment: Thanks so much for the assistance you provided on chat! Huge thanks!

Missing php5-fpm after PHP5.5 upgrade


Ckeditor: Add Cite tag

Question: I am using Drupal Ckeditor (The main one, not the WYSIWYG module).
I would like to add buttons to the interface for inserting <quote> and  tags.
How can I do this?
It already has a blockquote, but I would like <quote> and  as well.
Comment: you need to create a custom ckeditor plugin. You can find tutorials here http://docs.ckeditor.com/#!/guide/plugin_sdk_sample
Comment: @No Ssweat -The plugin in the sample tutorial doesn't seem to work in Drupal. It will load in the UI, but the button cannot be added to the toolbar. Is there a trick to making the button work?
Comment: You must be doing something wrong, did you do everything I posted as comment on Drupalist answer? In admin/config/content/ckeditor/edit/Advanced (which is FILTER HTML) In Editor Appearance it should be listed under "available buttons" which you then have to drag up into "Current toolbar".
Comment: Did you create the icons folder with a png image inside your plugin folder?
Comment: @NoSssweat Yes, I did. I also tried downloading the sample plugins in the link you posted. I can activate them in the Drupal UI, but the button does not appear. Do the sample plugins work for you?
Comment: yup the timestamp tutorial plugin worked for me. What you could do is download a simple ckeditor plugin from their website and then modify it.
Comment: @NoSssweat I tried the abbr plugin (without any modifications). It loads, but no button appears. The timestamp works for me, but the abbr one doesn't. Thanks for your continued help.
Answer: Here is a good tutorial in Custom buttons on CKEditor or Adding CKEditor Plugins Manually in Drupal .
Before asking the question you should have let us know what you have done so far.

If you were checked the links, you would see that in the Adding CKEditor Plugins Manually in Drupal and Add Plugin Part of CKEditor Module section it explained where to put the plugin.
Comment: you need to put them in sites/all/modules/ckeditor/plugins DO NOT put them in sites/all/modules/ckeditor/ckeditor/plugins. Then go to admin/config/content/ckeditor/edit/Advanced (<-- this url is for filter HTML) and under Editor Apperance, there is a plugins section, check mark your plugin.
Comment: Then go to admin/config/content/formats/filtered_html and add the  tag into the Limit allowed HTML tags section.
Comment: PS: never allow regular users to type in FULL HTML as they can embed evil.js onto your site.
Comment: By default, CKeditor comes with several plugins listed (but not installed) in the Drupal UI under "editor appearance". If I download these plugins and put them in my Drupal Ckeditor Plugin folder, I can activate them. However, how do I activate a plugin that is not listed in the Drupal UI? If I make my own plugin or download one from Ckeditor.com, it doesn't appear in the Drupal UI. If I try to activate it by using "advanced options" (e.g. `config.extraPlugins = 'codeTag';`), then Ckeditor fails to load on a node.
Comment: @big_smile I updated the answer
Comment: @Drupalist I did read the links: The plugins are in the right place (If they weren't then Plugins that are listed by default wouldn't work).
Comment: @Drupalist I got the plugin to appear (The plugin folder needs to have permissions 777). However, when I activate it, it doesn't appear in the Drupal toolbar. This happens even if I use the plugin in the links. E.g. https://github.com/ckeditor/ckeditor-docs-samples/tree/master/tutorial-abbr-1  <- This plugin appears in the Drupal UI, but the button doesn't appear in the toolbar.

Ckeditor: Add Cite tag


rsync (or robocopy) from multiple computers - what happens?

Question: If a Linux server has two different rsync jobs nightly for the same folder to two different destinations, do both destinations end up with the same end set of files?  Or does the first job run, and set something on the source folder/files that would cause the 2nd rsync job to not realize the daily changes/updates to the source?
Same for a Windows environment using something like robocopy, or even a "differential" backup using BUE or similar.
Does each "sync" compare the destination to the source and update the destination regardless of if it is synced multiple times to different destinations?
Answer: For rsync: Both jobs are independent of each other. In standard settings, they compare file size and mod time for source and target, those doesn't get modified on the source.
I guess you think about the Archive file attribute of the FAT filesystem, but rsync doesn't use something like that.


Which algorithm would be most appropriate for this

Question: Can someone tell me which algorithm would be best to solve this problem?  I am thinking this belongs in the category of 'Market Basket Analysis' aka ('Affinity Analysis'), but I am not sure.
Here's the problem:
A user belongs to certain types of groups. When our client gives us a set of users that belong to a particular group, we need to tell them which other groups those users might be most interested in as well as most disinterested in.
To me this sounds similar to the classic Market Basket Analysis example: 'customers who buy diapers also buy beer'.  So I was thinking - if I pre-compute all the affinities within groups then I will be able to answer our client's question.  No?
Would greatly appreciate any thoughts on this?  What kind of algorithm would be most appropriate for this?
Thanks for your help.
Answer: Yes, this can be solved with Market Basket Analysis.
Since Market Basket Analysis is a special case of Recommender Systems, so you will find a lot of helpful algorithms in this area, too. This question seems to be similar to yours Algorithm to calculate difference in users' tastes,
additionally you might find Recommendation for a book about recommender systems helpful.
However, if you do not have explicit information about particular user dislikes, you will have a hard time to distinguish "dislike" from "does not know about" i.e. "not sure whether likes or not". But I would not worry about this, the most important task is to recommend the most interesting groups ... the rest is simply not shown.


Have Python output .csv with space-padded, fixed width, but comma-separated columns?

Question: Let's say I have CSV data as note below; let's call this original.csv:
name,value1,value2
firstname,34326408129478932874,553
secondname_a_very_long_one,65,123987
thirdname_medium,9686509933423,33

Basically, it's either single word text (no space separation, so no need for quoting) or numbers (here integers, but could be floats with decimals or scientific 1e-5 notation) - and there is no expectation that a comma could appear somewhere (outside of being a delimiter), so no need for special handling of the quoting of comma either ...
So, to ease the strain on my eyes when I view this .csv file in a text editor, I would like to format it with fixed width - space padded (left or right padding choice made per column, and separately for header row); note that the file is still comma delimited as a data format, the fixed width is just for viewing in editor - this is how I would like it to look like, let's call this tmpfw.csv:
name                      , value1              , value2
firstname                 , 34326408129478932874, 553
secondname_a_very_long_one,                   65, 123987
thirdname_medium          ,        9686509933423, 33

So here, the heading row is all left-aligned (right-padded with spaces); columns name and value2 are also left-aligned (right-padded with spaces); and column value1 is right-aligned (right-padded with spaces). The columns are sized (in characters) according to the largest string length of the data in that column; and there is an extra space as visual delimiter after the commas.
Of course, if I want to use this data in Python properly, I'd have to "strip" it first - but I don't mind, since as I mentioned, the data is such that I don't have to worry about quoting issues; here is a Python example of how I could use tmpfw.csv - let's call it test.py:
import sys
import csv
import pprint

with open('tmpfw.csv', newline='') as csvfile:
  my_csv = csv.reader(csvfile)
  my_csv_list = list(my_csv)

my_csv_list_stripped = [list(map(str.strip, irow)) for irow in my_csv_list]

print("\nmy_csv_list:\n")
pprint.pprint( my_csv_list )
print("\nmy_csv_list_stripped:\n")
pprint.pprint( my_csv_list_stripped )

#print("\nreprint stripped as csv:\n")
#csvwriter = csv.writer(sys.stdout) # just print out to terminal
#csvwriter.writerows(my_csv_list_stripped)

This is what I get printed:
$ python3 test.py

my_csv_list:

[['name                      ', ' value1              ', ' value2'],
 ['firstname                 ', ' 34326408129478932874', ' 553'],
 ['secondname_a_very_long_one', '                   65', ' 123987'],
 ['thirdname_medium          ', '        9686509933423', ' 33']]

my_csv_list_stripped:

[['name', 'value1', 'value2'],
 ['firstname', '34326408129478932874', '553'],
 ['secondname_a_very_long_one', '65', '123987'],
 ['thirdname_medium', '9686509933423', '33']]

I can use this as a base to convert the numbers to int later - so, I can use such a fixed-width csv fine, all is good ...
So, my question is: let's say I have the original.csv - what would be the easiest way in Python to obtain a "fixed-width formatted" tmpfw.csv? Do csv or pandas or other libraries have facilities for exporting a CSV format like this?
Comment: Do you need to also edit the CSV while you're looking at it? If not, I wouldn't mess with the CSV format itself; instead just make it look really pretty (and different), https://csvkit.readthedocs.io/en/latest/scripts/csvlook.html to make a table. If you do need to edit it by hand, I'd still recommend not changing the structure and get a plugin like https://marketplace.visualstudio.com/items?itemName=mechatroner.rainbow-csv.
Comment: If you find you must edit the CSV, add the spaces **after** the comma, before the next cell of data. That way you can use the `skipinitialspace=True` option from Python's csv.reader to get it "back to normal"—no special processing necessary.
Comment: Also, somewhat distantly related, but for reference: https://stackoverflow.com/questions/18911984/align-column-names-with-data-in-a-csv-file-using-python
Answer: Sure – compute what the maximum length of each column is, then .ljust() them accordingly when printing:
import csv
import io

# pretend reading csv from file
csv_data = list(csv.reader(io.StringIO("""
name,value1,value2
firstname,34326408129478932874,553
secondname_a_very_long_one,65,123987
thirdname_medium,9686509933423,33
""".strip())))

n_cols = len(csv_data[0])
col_widths = [max(len(row[i]) for row in csv_data) for i in range(n_cols)]

for row in csv_data:
    print(', '.join(val.ljust(width) for val, width in zip(row, col_widths)))

This prints out
name                      , value1              , value2
firstname                 , 34326408129478932874, 553
secondname_a_very_long_one, 65                  , 123987
thirdname_medium          , 9686509933423       , 33

and naturally you could open a file and print(..., file=...) instead.
Comment: Awesome, thanks @AKX - that works great! +1 for the "surprise" - I kind of thought "plain Python" solution would be a lot more complicated/verbose, but your solution is quite readable.


Run code when a session ends

Question: Is there a hook or something similar that triggers when a PHP session ends?
When a user signs in, I'm writing a few things to the cache, and I need to delete them from the cache when the user exists the site. If the user manually logs out, that's no problem, but I still need to clear that data when users close the browser.
Answer: Drupal doesn't have hooks that are invoked when a session ends, probably also because the code would not be able to understand if the session cookie is not present because it expired (which means the session ended) or because the browser has not been used to log in on a Drupal site.
Drupal invokes a hook when the user is logged out, either because the user clicked on the Log out link, or because the user was forced to log out, which is what happens when the site is in maintenance mode and the account isn't exempted from being automatically logged-out.
Drupal 7 and Drupal 8 invoke hook_user_logout() when the user is logged-out, and the code invoking the hook also destroys the user session.
Since you are talking of cache to clear, if you are using Drupal 8, you can also investigate if there are cache tags or contexts that tell Drupal to invalidate the data when the session is destroyed.
Cache contexts lists session.exists as context that can be associated to cache data. I didn't test it, but from the context name, I take it invalidates the cache data when the session doesn't exist, which is what you are trying to achieve. (See also Cache API for more information on cache tags and cache contexts in Drupal 8.)
As side note, a Drupal session doesn't normally end when users close their browser. It would happen when using an incognito window with Google Chrome, or the equivalent of other browsers, but when you are not using that, the session doesn't end when the browser is closed, or when the browser tab is closed. (If that were to happen, it would mean logging off the users.)
Answer: There is no Drupal hook for session management. The closest thing [to inserting your own custom code in session management] is overriding core session management functions.

In Drupal 7, session management is in session.inc which you can override with your own in the settings.php config.
In Drupal 8, the equivalent is the SessionManager service which can do via a service override in your services.yml config.

While you could simply copy/paste/override (existing session management code) to hook into execution, you should consider an alternative seeing how session cache cleanup can be performed without hooking into session management (e.g. cronjob, etc).
Answer: There is no possible way you can know when a user closes the browser. But as a general rule if something is written to cache, it should be disposable, and can be regenerated on demand if necessary. Cache is not meant for data that has any value in itself.
Assuming that is the case here, what you need to do is to ensure your cached items expire after a while. Use a timestamp value for the expiry option of cache_set() for Drupal 7 or CacheBackendInterface::set() for Drupal 8.

Run code when a session ends


AMP-YouTube - Lazyloading the placeholder image

Question: When using the amp-youtube component, I have noticed that it uses the static cover image for the video as a placeholder until the user scrolls towards the video at which point the video itself gets loaded in!
This is great! However, when testing my page in PageSpeed insights, I'm getting penalized for "Defer offscreen images" because the placeholder image that is used is itself not Lazyloaded.
All other amp-img on the page get Lazyloaded so I'm hoping there is a way to enable it for the YouTube code too?
This is an example of a page with YouTube videos on it: https://nationalguitaracademy.com/acoustic-guitar-tabs/amp/
PageSpeed Results for the Page: https://developers.google.com/speed/pagespeed/insights/?url=https%3A%2F%2Fnationalguitaracademy.com%2Facoustic-guitar-tabs%2Famp%2F
TLDR: The video itself gets lazyloaded, but the static image used until the video loads is not lazyloaded.
Answer: Trying adding a custom placeholder like this:
<code><amp-youtube
 // attributes here
>
  <amp-img
    src="// placeholder image "
    placeholder
    layout="fill"
  />
</amp-youtube>

</code>
If that doesn't work, there really isn't a way to fix Lighthouse or Speed Insights erorrs when it comes to AMP cause you can't edit the source code. Your only hope is to file a bug report in the AMP GitHub project.


Nagios multiple check attempts for hard OK

Question: Looking for a configuration option to make checks that respond with OK not reach a hard state until max_check_attempts has been reached.
Issue is we'll have a service that we can bring online then will go right back down in a couple of minutes, but when it comes back up it sends out the OK notification, which closes out the issue in our ticketing system.
Comment: Maybe you should redesign your check so that it does not report success unless the service is really fully running ?
Comment: Can't. Monitoring a projector (about 150 of them) where it will report that it's off, you can turn it on, then it will turn itself off a couple of minutes later. No warnings or anything, it's just self preservation of the projector.
Answer: My recommendation would be to first determine how long after an outage of a projector is an acceptable time-frame before considering an outage a new outage versus still part of the last outage.
Depending on how like that window is, I would say to follow @b0fh suggestion and redesign the check.  If the window is short (several minutes) simply tell the check on an OK result to sleep for X number of minutes and then rerun the check; if it passes the second time then send the exit code 0.  However, if the window is longer then several minutes, I would say a better option is to redesign the check essentially with status caching (so that you can compare an outage/device up against the cache).  In order for this method to be more effective, you may need to run the script/check as a scheduled job on the Nagios host and have it send passive check results to Nagios.
Answer: Nagios considers a host or service that is acting in the manner you describe to be flapping. You may wish to tweak your flap detection for this host/service.
Comment: Not quite.  A flapping host/service is one that is constantly going back and forth.
Answer: Use the check_command to override default host check and define a custom check/script that will do desired number of checks before declaring state.  Even easier, again using the check_command, define a new check-host-alive that has multiple pings before declaring host is down.  The default is one ping.


Conditional Probability of Poisson Variables

Question: I have two independent Poisson variables $X$ and $Y$ with parameters $\lambda$ and $\mu$, respectively. I defined $Z=X+Y$ and found that $Z$ is also Poisson-distributed with parameter $\lambda + \mu$.
My task was to compute $P(X=k|Z=n)$ for $0 \leq k \leq n$. I know from this question that the solution is ${n \choose k} p^k(1-p)^{n-k}$, but I couldn't figure out how to get this solution.
I noticed that since $n \geq k$, asking for $X=k$ is equivalent to $X=n-m$ for some $m$ such that $n-m = k$. So I used the formula for conditional probability and tried to find $P(X=n-m \land Z =n)$ using the sum $\sum_{m=0}^n P(X=n-m; Z=n)$, but I was not successful in this.
Can anyone show me how to reach the desired conclusion?
Comment: What exactly prevents you from computing P(X=k,Z=n)=P(X=k,Y=n-k) and P(Z=n) and to divide the former by the latter?
Comment: @Did That's the answer by Math-fun supplied below. It just didn't occur to me. I had the idea, but I applied it to $X$ rather than to $X+Y = Z$. It would've probably occurred to me had I thought about decomposing $Z$ into $X$ and $Y$.
Comment: Note in addition that "to find $P(X=n-m \land Z =n)$ using the sum $\sum_{m=0}^n P(X=n-m; Z=n)$" seems to refer to some strategy I cannot fathom.
Comment: @Did That's reasonable. That attempt was quite unsuccessful.
Answer: \begin{align}
P(X=k|Z=n)&=\frac{P(X=k,Z=n)}{P(Z=n)}\\
&=\frac{P(X=k,X+Y=n)}{P(Z=n)}\\
&=\frac{P(X=k,Y=n-k)}{P(Z=n)}\\
&=\frac{P(X=k)P(Y=n-k)}{P(Z=n)}\\
&=\frac{\frac{e^{-\lambda}\lambda^k}{k!}\frac{e^{-\mu}\mu^{n-k}}{(n-k)!}}{\frac{e^{-\lambda-\mu}(\lambda+\mu)^n}{n!}}\\
&=\frac{n!}{k!(n-k)!}\frac{\lambda^k}{(\lambda+\mu)^k}\frac{\mu^{n-k}}{(\lambda+\mu)^{n-k}}\\
&=\frac{n!}{k!(n-k)!}p^k(1-p)^{n-k}\\
\end{align}
where $p=\frac{\lambda}{\lambda+\mu}$.


How would I use nested for loops to make a solid rectangle out of asterisks using java?

Question: import java.util.Scanner;

public class Box {

    public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);

        int length;
        int width;
        int rectangle;

        // input and output here

        System.out.print("Input the width of the box: ");
        width = keyboard.nextInt();

        System.out.print("Input the length of the box: ");
        length = keyboard.nextInt();

        // use nested for loops here

    }

}

This is just what the base of my program is, I need a program that prints a solid rectangle out of asterisks using nested for loops.
Comment: show us what you have tried.
Comment: How do you print one asterisk? How do you print 1 line of `width` asterisks? How do you print `height` lines?
Answer: for(int i = 0; i< height; i++) {
    for(int j=0; j<width; j++) {
        System.out.print("*");
    }
    System.out.println();
}

This should do the trick, next time make sure to let us know what you have tried already. :)
Answer: import java.util.Scanner;

public class q1 {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);

        int length;
        int width;
        int rectangle;

        // input and output here

        System.out.print("Input the width of the box: ");
        width = keyboard.nextInt();

        System.out.print("Input the length of the box: ");
        length = keyboard.nextInt();
        for(int i = 0; i< length; i++) {
            for(int j=0; j<width; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }

}

/*
 * program output

Input the width of the box: 6
Input the length of the box: 7
*******
*******
*******
*******
*******
*******
*/

Comment: Change the width in the second for loop to length because previously it printed the width as the length.


Finding the area between two curves.

Question: Context: High School question.

Find the surface area between the curve of the function $y=6-3x^{2}$
  and the function $y=3x$ in the interval $[0,2]$

My approach:
-We must find the points of intersection first:
$$6-3x^{2}=3x$$
$$x^{2}+x-2=0$$
$$x=-2,x=1$$
Then we must evaluate these integrals to find the area:
$$A=\left | \int_{0}^{1}(-3x^{2}-3x+6)dx \right |+\left | \int_{1}^{2}(-3x^{2}-3x+6)dx \right |$$

My question is why couldn't we just evaluate this integral as usual to find the area?

$$\left | \int_{0}^{2}(-3x^{2}-3x+6)dx \right|$$
Comment: The integral $\int_{-1}^1 x \mathrm{d}x$ is equal to $0$ but this is not the area under the curve $y=x$ between $x=-1$ and $x=1$.
Answer: Because the area is the integral of (function which is above) minus (function which is below). In other words, it is equal to$$\int_0^1\lvert6-3x^2-3x\rvert\,\mathrm dx.$$But$$\lvert6-3x^2-3x\rvert=\begin{cases}6-3x^2-3x&\text{ if }x\in[0,1]\\3x-(6-3x^2)&\text{ otherwise.}\end{cases}$$
Answer: We want to add up the two pieces of area.
If you integrate them as one term. You are actually computing the difference between the two areas.
\begin{align}
\left|\int_0^2-3x^2+6-3x\, dx\right|&=\left|\int_0^1-3x^2+6-3x\, dx+\int_1^2-3x^2+6-3x\, dx\right|\\
&=\left|\int_0^1-3x^2+6-3x\, dx-\int_1^2 3x-(-3x^2+6)\, dx\right|\\
&=\left|\int_1^2 3x-(-3x^2+6)\, dx-\int_0^1-3x^2+6-3x\, dx\right|\\
\end{align}
Comment: But why then if we want find the area between two curves like $y=6x-x^{2}$ and $y=x^{2}-2x$ we take the integral without worrying about this, although some parts of the latter function are below the x-axis?
Comment: the important thing is not whether they are above or below the $x$-axis, but whether the relative position of the two curve swaps.
Comment: Could you explain further please.
Comment: the area between two curve between $f$ and $g$ can be obtained via $\int |f-g| \, dx$. To remove the absolute value inside the integral, we can split the integral into the region where $f \ge g$ and the region where $g \ge f$ and then sum them up.  We can't just move the absolute value outside the integral directly.
Comment: why wasn't the absolute value needed for other problems? i.e when should I put it?
Comment: it is needed, just that perhaps you have been very lucky that you didn't encounter problems where the curve intersect and one curve overtake the other.
Comment: I'm sorry, but why is it needed? What is its function?
Comment: Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/93235/discussion-between-siong-thye-goh-and-positron12).
Answer: Net area is shown integrated from difference of two curves$ y_2-y_1=-3x^{2}-3x+6 $  starting at $(0,6)$.
At first the area integal $ -x^3-\dfrac{3 x^2}{2}+6 x $ is positive, maximum at $x=1,$ then at $x\approx 1.81174 $ zero when two areas balance out and thereafter it is negative.

At $x=2$ net evaluated area is $ A= -2$ shown.
Answer: It is $$\int_{-2}^16-3x^2-3xdx$$ the result is given by $$\frac{27}{2}$$ You can add a constant $C$ to both curves and you must not care if the curves are below the axes or not.

Finding the area between two curves.


Inicialização de variáveis de instância no ActiveRecord

Question: Estou com dificuldade em inicializar variáveis de instância no Rails, preciso utilizar uma variável em diversos métodos, mas essa precisa ser inicializada previamente, por exemplo:
class Test < ActiveRecord::Base
  @test = 1

  def testar
    @test+1
  end
end

t = Test.new
t.testar

Gera o seguinte erro:
test.rb:4:in `testar': undefined method `+' for nil:NilClass (NoMethodError)
    from test.rb:9:in `'

Pois @test não foi inicializado, então vejo que não funciona realizar essa inicialização no corpo da classe, existe uma forma mais elegante de fazer isso do que você usar after_initialize? Como no exemplo abaixo:
  def after_initialize
    @test = 1
  end

Answer: Até onde eu sei não há outra forma recomendável, a não ser usando o after_initialize.
O initialize original da classe pode não ser invocado em alguns casos pelo ActiveRecord, então. o after_initialize foi criado justamente para resolver o problema de inicialização.
Há uma discussão sobre tudo isso nesta questão do SO e neste artigo. Veja a também a documentação explicando o after_initialize.
Answer: Do jeito que você fez, atribuindo @test = 1 dentro da classe, você está definindo uma variável da classe e não de instância.
Para fazer o que você quer, você pode usar o operador ||=. Se você usa a variável em mais de um local, sugiro acessá-la através de um método de leitura.
class Test < ActiveRecord::Base
  def test
    @test ||= 1
  end

  def testar
    test + 1
  end
end

t = Test.new
t.testar # => 2

O operador @test ||= 1 funciona como se fosse @test = @test || 1.
A primeira vez que você chamar o método test, a variável @test é nil, então ele atribui 1 a @test (e retorna esse valor). Nas próximas chamadas, como @test já está definido, o método apenas retorna o valor da variável.