How do I find the process ID that listens to SOCK_SEQPACKET in C/C++ program

Question: There is a server that starts and listens to a SOCK_SEQPACKET. I think a SOCK_SEQPACKET has a name, say, @Foo. It's per user, so initiated by each Linux user account, and there is only one service per user.
The goal is to make an external tool that forcefully kill the service when something goes wrong. The language is C++.
When I have to kill that service, I use netstat:
$ netstat -lnp | egrep Foo

I would not like to have a dependency on the net-tools package, though. I would like to do this with minimal dependencies on external tools and/or even libraries.
I've tried Google search, and could not find how. I guess I could perhaps read netstat source code to see how they do; I would like to defer that to the last resort, though. I have learned how I could kill the service, using netstat, which gives me dependency on net-tools. The service somehow runs as /proc/self or so. I could visit every process ID in /proc, and see if that looks like the service: see the executable name, etc. However, that is not necessarily sufficient indication to narrow down to that one single process, which uses the @Foo SOCK_SEQPACKET. Now, as non-expert in socket/network programming, I am running out of ideas about how I should proceed.
Comment: SOCK_SEQPACKET is not really a type of socket, it's more like a mode for that socket. Are you talking about an AF_UNIX socket?
Answer: As @user253751 says, that's not actually a socket type but more of a socket behavior type. As they suggested, it is probably a unix domain socket. Given the '@' sign in it, it is almost certainly using the unix domain socket's "abstract" namespace (which means the socket doesn't have an endpoint directly visible in the filesystem). At the system call level, such endpoints are created with a null byte as the first character (the @ is commonly substituted in command line utilities or wherever the socket is surfaced to user space for user convenience). See the unix(7) man page for more details.
Since the socket doesn't appear in the filesystem anywhere, it can be difficult to find the association of process to socket. One way you can find that is through use of the lsof utility -- but that is probably little improvement over the netstat utility.
It appears that the abstract namespace sockets do show up in the /proc/net/unix pseudo-file. In association with that, there is an inode column (second to the last). In conjunction with looking at each process's /proc/<pid>/fd directory, I think you can make the association.
For example, I created a small python program that creates a Unix domain SOCK_SEQPACKET socket and binds it to '@Foo'. That program is here:
import socket, time

sock = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET)
sock.bind('\0Foo')  # Note the \0 for the initial null byte
time.sleep(10000)   # Just hang around while we do other stuff

In a separate terminal window:
gh $ grep '@Foo' /proc/net/unix
0000000000000000: 00000002 00000000 00000000 0005 01 7733102 @Foo

gh $ sudo bash -c 'for proc in /proc/[0-9]*; do ls -l $proc/fd | grep 7733102 && echo $proc; done'
lrwx------ 1 gh gh 64 Feb  2 17:13 3 -> socket:[7733102]
/proc/339264

gh $ ps -fp 339264
UID   PID    PPID  C STIME TTY          TIME CMD
gh   339264   22444  0 17:11 pts/3    00:00:00 python3

gh $ ls -l /proc/339264/fd
total 0
lrwx------ 1 gh gh 64 Feb  2 17:13 0 -> /dev/pts/3
lrwx------ 1 gh gh 64 Feb  2 17:13 1 -> /dev/pts/3
lrwx------ 1 gh gh 64 Feb  2 17:13 2 -> /dev/pts/3
lrwx------ 1 gh gh 64 Feb  2 17:13 3 -> 'socket:[7733102]'

Explaining:

First, we do a grep on /proc/net/unix for the '@Foo' socket. That gives us the inode number 7733102 (second to last column). The inode number is unique to every socket on the system.
Next, as root, we do an ls -l /proc/<pid>/fd for each process on the system and grep for the inode number we found in the previous step. When found, we printed out the /proc/<pid> entry. (On linux, that pseudo-directory shows a symlink to a representation of what each file descriptor open in the process is. For sockets, that representation is always socket:[<inode>].) Here we found the process ID is 339264.

The remaining two steps are just confirming that that is in fact, our python process, and you can see its four open files (the first three [stdin, stdout, stderr] all pointing to its terminal's pseudo-tty, the fourth to the socket).
To make this into a more foolproof program, you'd need to account for the fact that the inode number you found in step 1 could be a substring of some other socket's inode number but that is left as an exercise for the reader. :)


Where are the database username and password stored?

Question: I'd like to send my Drupal installation to a colleague, but I don't want him to see the username and the password of my database. Where are these informations stored?
I've seen that the username and the password are written in sitename/sites/default/settings.php, but is it the only file with these informations? Is it sufficient if I change the username and the password in this file and then send my colleague all my Drupal files and my database?
Answer: The settings are stored in sites/default/settings.php. That is indeed the only file with that information and it is sufficient that you change the username and the password in that file.
Answer: In a single word "YES". Is sufficient if I change the username and the password in sitename/sites/default/settings.php.
Answer: IMO both previous answers (posted with 10 secs difference ...) are incomplete ...
The /sites/default/settings.php file indeed does contain that userid and password, and if you "don't want him to see the username and the password of my database" (as in your question), then you at a minimum remove that userid and password (which you want to keep secret) in that file.
However, your question also contains this:

Is it sufficient if I change the username and the password in this file?

To keep your userid and password secret you are done with only this change. But if you want your colleague to be able to actually use the Drupal installation you want to send, then IMO the answer to that question is NO. Because Drupal requires that the /sites/default/settings.php file that your colleague will be using contains the userid and password that matches with the database after it is restored by your colleague.
Note (credits: sanzante): you should also send your colleague a database dump. And your colleague only has to import the database and edit the settings.php and add the user and password for the imported database. That config (user and password) is not dumped in the database so the colleague must have his/her own.
Comment: Yes, he also should send him a database dump and his colleague only has to import the database and edit the settings.php and add the user and pass for the importad database. That config (user and pass) is not dumped in the database so the collegaue must have their own.
Comment: @sanzante : merci! You agree I include your comment in my answer also?
Comment: @Pierre.Vriens and sanzante you both are right. Actually we miss some points :)
Comment: No no, As I believe true is always true. I saw lots of your answer. Its always more details and descriptive. Which is easy to understand. Its great as usual.
Comment: Yes, I agree, no problem at all!
Comment: @Smalution : merci (also)! And sorry for my (constructive intended) critique to your answer (IMO not wrong, only incomplete), which was too tempting to just let it go without supplementing it.
Comment: This is just wrong. A mysqldump does not contain a database username and password so your conclusion that the answer is "NO" makes no sense. The answer is "YES".
Comment: Not sure why the other answers are incomplete?  The last sentence in the question clearly states that the OP is sending off files+database.


Assistance with Some Interesting Syntax in Some Ruby Code I've Found

Question: I'm currently reading Agile Web Development With Rails, 3rd edition.  On page 672, I came across this method:
def capitalize_words(string)
  string.gsub(/\b\w/) { $&.upcase }
end

What is the code in the block doing?  I have never seen that syntax.  Is it similar to the array.map(&:some_method) syntax?
Answer: It's Title Casing The Input. inside the block, $& is a built-in representing the current match (\b\w i.e. the first letter of each word) which is then uppercased.
Answer: gsub replaces everything that matched in the regex with the result of the block. so yes, in this case you're matching the first letter of words, then replacing it with the upcased version.
as to the slightly bizarre syntax inside the block, this is equivalent (and perhaps easier to understand):
def capitalize_words(string)
  string.gsub(/\b\w/) {|x| x.upcase}
end

or even slicker:
def capitalize_words(string)
  string.gsub /\b\w/, &:upcase
end

as to the regex (courtesy the pickaxe book), \b matches a word boundary, and \w any 'word character' (alphanumerics and underscore). so \b\w matches the first character of the word.
Answer: You've touched on one of the few things I don't like about Ruby :)
The magic variable $& contains the matched string from the previous successful pattern match. So in this case, it'll be the first character of each word.
This is mentioned in the RDoc for String.gsub:
http://ruby-doc.org/core/classes/String.html#M000817
Comment: As I said, one of the few things I don't like about Ruby. Wish Matz left magic variables with Perl.
Comment: Peter's answer (below) shows two more readable versions, neither of which uses the global variable. (Or, more accurately, any use of that global is bound up in the way `String#gsub` passes variables to a block -- how it does so is left up to the Ruby implementation.)


trying to connect to sql database and driver cannot be found

Question: i am using netbeans to write a java application. I need to connect to an sql database and i get this error:java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
I use maven project and I added the dependency : ojdbc14
<code>  try {     //load database driver
            Class.forName("oracle.jdbc.driver.OracleDriver");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger("Driver not found");
            System.out.println(ex);
            System.out.println("error");
        }
</code>
What can I do to fix this problem ? I am new to java and I do not know much
Comment: You are connecting to an Oracle database or a SQL Server database?                                                                                In general, when connecting to Oracle database you will need to have the Oracle ODBC driver installed, which are not part of standard windows installation.  I'm not sure how this plays with java, but you may need to install the Oracle database client in order to obtain the appropriate driver.
Answer: You need to install the driver on to your computer. Here is an answer
JDBC Driver class not found: oracle.jdbc.OracleDriver
Answer: I am just wondering: You said ojdbc14? That was the oracle jdbc driver to be used with Java 1.4.
I would suggest to use the latest version which is ojdbc8:
https://mvnrepository.com/artifact/com.oracle.jdbc/ojdbc8
That is of April 2019 and not of 2009 (ojdbc14).
Maybe even this information of 2016 is useful:
https://blogs.oracle.com/dev2dev/get-oracle-jdbc-drivers-and-ucp-from-oracle-maven-repository-without-ides
Comment: My problem is that I click add dependency search for ojdbc from oracle and it only shows ojdbc14 , Ojdbc6 or 8 are only from other sources available!
Comment: yes, Oracle wants you to create an account and accept the license agreement. You have to do this either when downloading and installing the driver manually (as suggested by Narayan in the local answer or when adding the Oracle Maven Site.
Answer: You can download the JDBC driver from OTN and use it too.


AngularJs ReferenceError: $locationProvider is not defined

Question: Estou com erro de

ReferenceError: $locationProvider is not defined.

Estou usando angular-ui-router. Podem me ajudar?
obs: Removi o #.
<code>    var app = angular.module("app", ['ui.router', 'angularUtils.directives.dirPagination']);

app.config([$locationProvider, function ($stateProvider, $urlRouterProvider) {

    $locationProvider.html5Mode(true);

    $urlRouterProvider.when("", "/Index");

    $stateProvider
    .state('/Empresas',
            { templateUrl: $("#linkBase").attr("href") + 'templates/Empresa/empresas.html',
                 controller: 'EmpresaController' })
    .state('/Usuarios',
            { templateUrl: $("#linkBase").attr("href") + 'templates/Usuario/usuarios.html',
                controller: 'UsuarioController' })

}]);
</code>
Comment: Alterei o config e nao deu mais o erro porem as rotas nao funcionam. .config(function ($stateProvider, $urlRouterProvider, $locationProvider)
Answer: No AngularJS você precisa injetar as dependências na ordem que vai receber como parâmetro da função.
O array que a função ".config()" recebe como parâmetro deve estar assim:
1 - Lista de nomes em formato string com o nome registrado do objeto (provider, constants, etc) separados por vírgula.
2 - O último parâmetro do array obrigatoriamente deve ser uma função, que terá os items de dependência especificados previamente no array injetados nos parâmetros desta função, obedecendo a mesma ordem. Os nomes dos parâmetros na função podem ser definidos como melhor entender, porém é costume utilizar o mesmo nome que foi registrado.
Para corrigir seu erro, utilize abaixo:
<code>var app = angular.module("app", ['ui.router', 'angularUtils.directives.dirPagination']);

app.config(['$stateProvider', '$locationProvider', '$urlRouterProvider'
   function ($stateProvider, $locationProvider, $urlRouterProvider) {

    $locationProvider.html5Mode(true);
    $urlRouterProvider.when("", "/Index");
    $stateProvider('/Empresas',
        { templateUrl: $("#linkBase").attr("href") + 'templates/Empresa/empresas.html',
             controller: 'EmpresaController' })
.state('/Usuarios',
        { templateUrl: $("#linkBase").attr("href") + 'templates/Usuario/usuarios.html',
            controller: 'UsuarioController' });
}]);
</code>


PLSQL apresenta erro erro: PLS-00103 em função ao utilizar um Exception

Question: O PLSQL está informando o erro PLS-00103 quando eu tento compilar esta função.
Observação: Caso eu tente executar apenas com a primeira opção deste case "P_TIP = 1" ele executa normalmente.
Caso eu tente adicionar o case "P_TIP = 2" ele simplesmente retorna este erro.
Reitero que os dois Selects dentro dos cases estão funcionando perfeitamente no banco, eu apenas copiei eles para a função inserindo o INTO P_VALOR.
<code>CREATE OR REPLACE FUNCTION FC_PERS_RAIOX (P_ANO INT, P_CODPARCMATRIZ INT, P_TIP INT)
RETURN FLOAT
IS
   P_VALOR FLOAT;
BEGIN

    BEGIN
        -- BUSCA A VENDA ACUMULADA PARA O CLIENTE
    CASE WHEN P_TIP = 1 THEN

         SELECT
         SUM(MRG.VLRTOT) INTO P_VALOR
         FROM AD_DADOSMRG_EX MRG
         WHERE
         MRG.DTENTSAI BETWEEN '01/01/'||P_ANO AND '31/12/'||P_ANO AND
         MRG.TIPMOV IN ('V', 'D') AND
         MRG.GRUPO IN ('VENDAS') AND
         MRG.CODPARCMATRIZ = P_CODPARCMATRIZ
         GROUP BY 1;
         -- BUSCA DESCONTO MEDIO DO CLIENTE
    CASE WHEN P_TIP = 2 THEN

         SELECT
         ROUND((1-(SUM(DECODE(TO_CHAR(MRG.DTENTSAI, 'YYYY'), P_ANO, MRG.VLRTOT,0))/SUM(DECODE(TO_CHAR(MRG.DTENTSAI, 'YYYY'), P_ANO, MRG.PRTAB,0))))*100,2) INTO P_VALOR
         FROM AD_DADOSMRG_EX MRG
         WHERE
         MRG.TIPMOV IN ('V', 'D') AND
         MRG.GRUPO IN ('VENDAS') AND
         MRG.CODPARCMATRIZ = P_CODPARCMATRIZ
         GROUP BY 1;
         END CASE;

    EXCEPTION WHEN NO_DATA_FOUND THEN

        P_VALOR := 0;
    END;

    RETURN P_VALOR;

END;
</code>
Answer: descobri o problema.
Eu estava criando dois CASES.
NA LINHA ONDE ESTÁ ESCRITO: CASE WHEN P_TIP = 2 THEN
O CORRETO SERIA SOMENTE: WHEN P_TIP = 2 THEN


Homomorphism property

Question: I have a problem with understanding one of homomorphism properties.
Given $f$ - is a homomorphism, $g \in G$ and $h = f(g)$, then $f^{-1}(h) = g \cdot Kerf$.
Can anyone please prove it?
Comment: Do you understand the meanings of $f^{-1}(h)$ (a set, not an element) and $g \cdot \ker f$ (a set,not an element)? If so, then this question is almost a practice of knowing and applying definitions.
Comment: $f^{-1}(h) = g. Ker \ f = Ker \ f \ . g$ proving that $Ker \ f$ is normal in $G$
Answer: Hint:
Take any element in $x\in g\ker f$, and check $f(x)=h$. This sets $\supseteq$.
For $\subseteq$, take an element $g'$ such that $f(g')=h=f(g)$, and find the value of $f\bigl(g'g^{-1}\bigr)$.
Answer: First you need to know that since $f$ is not necessarily bijective. $f^{-1}(h)$ is not an element, but is the pre-image of $h$ which is a set. So to prove equality of set we need to show that they are subset of each other.
First, let $x\in f^{-1}(h)$, which means $f(x)\in \{h\}$.
Therefore, $f(x)=h=f(g)$.
Write $x=g(g^{-1}x)$.
Since $f(g^{-1}x)=(f(g))^{-1}(f(x))=h^{-1}h=1$, $g^{-1}x\in \ker f$.
Hence $x\in g\cdot \ker f$
Converserly, let $x=ga$ where $a\in \ker f$.
Then $f(x)=f(ga)=f(g)f(a)=f(g)=h$ since $f(a)=1$.
We conclude that $x\in f^{-1}(h)$.

Homomorphism property


Criar pasta a partir de um arquivo em excel em python

Question: Criei um programa para criar pasta a partir de entrada de usuários, mas quero saber se tem como criar um programa pra ler um arquivo em excel e a partir dessas informações criar várias pastas para o caso de um número de pastas muito grande.
import os
import pandas as pd
import sys
import os.path
from openpyxl import load_workbook
import openpyxl
import fsspec

def restart_program():
    pass

diretório = '/'
EPS_UTD = input("Para x digite 1, para y digite 2: ")

if EPS_UTD == "1":
    QTD = input("Para quantas EPS deseja criar pastas?: ")
    if QTD == "1":
        EPS = input("Digite o nome da EPS: ")
        CONTRATO = input("Digite o número do contrato: ")
        ANO = input("Digite o ANO do projeto com 4 dígitos: ")
        PROJETO = input("Digite o número do projeto: ")
        if not os.path.exists(diretório+EPS.upper()+'/'+PROJETO+'_'+ANO+'/A'+ANO+PROJETO):
            os.makedirs(diretório+EPS.upper()+'/'+PROJETO+'_'+ANO+'/A'+ANO+PROJETO)
    if QTD == "2":
        (e assim por diante...)
    print("Pasta criada")
else:
    print("Entrada inválida")
    restart_program()
exit()

Answer: Basta criar um ficheiro xlsx com este formato. Carregar o workbook, obter o nº de filas, e criar um ciclo para ler cada fila e criar a pasta.

Nome EPS
Nº Contrato
Ano
Nº Projeto

Eps1
1
2000
1

Eps2
1
2001
2

Eps3
2
2002
3

import os
from openpyxl import load_workbook
import openpyxl

ficheiro_excel = 'Book1.xlsx'
diretório = './'

wb_obj = openpyxl.load_workbook(ficheiro_excel)
sheet_obj = wb_obj.active
qtd = sheet_obj.max_row

for i in range(2, qtd + 1):
    EPS = sheet_obj.cell(row = i, column = 1).value
    n_contrato = sheet_obj.cell(row = i, column = 2).value
    ANO = str(sheet_obj.cell(row = i, column = 3).value)
    PROJETO = str(sheet_obj.cell(row = i, column = 4).value)

    if not os.path.exists(diretório+EPS.upper()+'/'+PROJETO+'_'+ANO+'/A'+ANO+PROJETO):
        os.makedirs(diretório+EPS.upper()+'/'+str(PROJETO)+'_'+ANO+'/A'+ANO+PROJETO)


Estimating linear regression with OLS vs. ML

Question: Assume that I'm going to estimate a linear regression where I assume $u\sim N(0,\sigma^2)$. What is the benefit of OLS against ML estimation? I know that we need to know a distribution of $u$ when we use ML Methods, but since I assume $u\sim N(0,\sigma^2)$ whether I use ML or OLS this point seems to be irrelevant. Thus the only advantage of OLS should be in the asymptotic features of the $\beta$ estimators. Or do we have other advantages of the OLS method?
Comment: the only difference for finite samples is, that the ML-estimator for the residual variance is biased. It does not account for the number of regressors used in the model.
Answer: Using the usual notations, the log-likelihood of the ML method is
$l(\beta_0, \beta_1 ; y_1, \ldots, y_n) = \sum_{i=1}^n \left\{ -\frac{1}{2} \log (2\pi\sigma^2) - \frac{(y_{i} - (\beta_0 + \beta_1 x_{i}))^{2}}{2 \sigma^2} \right\}$.
It has to be maximised with respect to $\beta_0$ and $\beta_1$.
But, it is easy to see that this is equivalent to minimising
$\sum_{i=1}^{n}  (y_{i} - (\beta_0 + \beta_1 x_{i}))^{2} $.
Hence, both ML and OLS lead to the same solution.
More details are provided in these nice lecture notes.
Comment: The maximum likelihood estimator *is* OLS. As they are exactly the same, they will have the same asymptotic properties.
Comment: Thanks for your answer ocram. It's clear that both methods lead to the same solution. But the OLS should be more powerful since the estimators are more efficient than the ML estimators, no? I'm wondering about the differences and the advantages of both methods in the context when assuming $u\sim N(0,\sigma^2)$ and I'm interested in the features of the estimators. The values for $\beta$ are identic, but I've in mind that the asymptotic features of OLS estimators are preferable. roughly spoken: If ML and OLS give the same results and features of the estimators, why should we use OLS?
Answer: You are focusing on the wrong part of the concept in your question.  The beauty of least squares is that it gives a nice easy answer regaurdless of the distribution, and if the true distribution happens to be normal, then it is the maiximum likelihood answer as well (I think this is the Gauss-Markov thereom).  When you have a distribution other than the normal then ML and OLS will give different answers (but if the true distribution is close to normal then the answers will be similar).
Comment: Gauss-Markov is for when the the OLS estimator is the best (minimum-variance) estimator among all linear and unbiased estimators, so so-called BLUE (best linear unbiased estimator), and it does not even require a normal conditional distribution.


php - extended classes carry objects

Question: I tried to ask this question earlyer but I think I rushed the question and did not get accross what I was thinking...
Does the value and objects of $this get passed to an extened class?
<?php

class bar {

    public function foo( $t ) {

        $this->foo = $t;

    }

}

class foo extends bar {

    public function bar () {

        return $this->foo;

    }

}

$b = new bar();
$b->foo('bar');
$f = new foo();
echo $f->bar();

?>

if not, is there another decleration (instead of extends) that does without passing the object of the parent class to the child class?
regards,
Phil
Answer: Your example would yield an Undefined property: foo::$foo error. I think what you're trying to use is a static property:
class bar {
    protected static $foo;
    public function foo ($t) {
        static::$foo = $t;
    }
}

class foo extends bar {
    public function bar () {
        return static::$foo;
    }
}

Then the following:
$b = new bar();
$b->foo('bar');
$f = new foo();
echo $f->bar();

... would echo bar, which looks like what is you're trying to achieve.
Comment: @Phil: Slightly updated my answer. You may also want to check this: http://php.net/manual/en/language.oop5.late-static-bindings.php
Comment: @Phil: No, it is inherited from the parent class.

php - extended classes carry objects


Compare AD against CSV

Question: I am trying to create a script that will compare our AD against all the users in a CSV. Our HR department has the master database for all staff but when they make changes they rarely inform us so they now export all users from the HR database to a CSV.
I need to compare this against our AD and amend anyone that has been found to have changed or any new staff.
I have the below script but it just outputs all staff, I only want the staff that have changed or new staff that are not in AD to be emailed.
write-host "Using default CSV file or C:\scripts\csv\StaffChanges.csv"
$StaffCSVUPath = "C:\scripts\csv\StaffChanges.csv"

$logfile = "C:\scripts\logs\ADvsCMIS.csv"

if(test-path $logfile) {
    remove-item $logfile -force
}

function Email {
    #Send an email, called with recipient email address and message body
    param(
        [string] $emailaddress="",
        [string] $bodymsg=""
    )
    $bodymsg += "
"
    $bodymsg += Get-Content($logfile)
    Send-MailMessage -To $emailaddress -From "firstname.lastname@example.com" -Subject "(AD-CMIS_errors) Errors found between Active Directory and CMIS" -Body $bodymsg -BodyAsHTML -SMTPServer "exchserver"
}

function CheckOutputFile {
    #Called with folder\filename and type of file
    param(
        [string]$outputfilename = "",
        [string]$type = ""
    )
    if(test-path($outputfilename)) {
    } else {
        write-host "Creating $outputfilename"
        $msg = "Forename,Surname,Username,ID"
        $msg | out-file($outputfilename)
    }
}

#Snap-ins needed to use the commands within the script
if((Get-pssnapin -Name Microsoft.Exchange.Management.Powershell.E2010 -ErrorAction SilentlyContinue) -eq $null){Add-PSSnapin  Microsoft.Exchange.Management.PowerShell.E2010}
if((Get-pssnapin -Name Quest.activeroles.admanagement -ErrorAction SilentlyContinue)-eq $null){Add-pssnapin Quest.activeroles.admanagement}

#import users from csv file
$users = (import-Csv $StaffCSVUpath)
$count=0
$countAD=0

Get-QADUser -searchroot "domain/Users/Staff" -SizeLimit 0 -includedproperties employeeid,displayname | ForEach-Object ($_.samaccountname) {
    $found = 0
    $countAD+=1
    ForEach ($user in $users) {
        $count+=1
        $inital = $user.forename.substring(0,1)
        $name = $user.forename+" "+$user.surname
        $dispname = $inital+" "+$user.surname
        if ($user.id -eq $_.employeeid) {
            if ($user.surname -eq $_.lastname) {
                if ($inital -eq $_.firstname) {
                    if ($name -eq $_.name) {
                        if ($dispname -eq $_.displayname) {
                            $found = 1
                        }
                    }
                }
            }
        }
        if ($found -eq 1){break}
    }
    if ($found -eq 0) {
        if(($_.company -ne "testing") -band ($_.company -ne "service")) {
            CheckOutputFile $logfile "LOG"
            $msg = "
" + $_.firstname +" " + $_.lastname + " " + $_.samaccountname + " "+$_.employeeid +"
"
            $msg | Out-File $logfile -append
        }
    }
}

if (test-path $logfile) {
    #If there is anything to report
    write-host "Emailing Log file to ict"
    #Email file if $outputB exists
    $email = "email@example.com"
    $body = "Action Required: The users below do not exist within HR.  Contact HR Data manager to resolve issue, delete users manually if required."
    #email ict
    Email $email $body
}

Comment: Make them store UPNs in their base, then you can find a user quickly by its UPN. Thankfully UPNs are usually equal to emails, although it might not be exactly the case with you. And be warned as you're dealing with sensitive data, "amending" new staff in AD can potentially provide an inside data leak as an authorized user.
Comment: Just a quick note, the CSV file only contains the following fields:- ID, Firstname, Surname. The ID is usually just the user initials but is unique for every user.
Comment: If you have a way to map ID to `sAMAccountName`, `userPrincipalName` or `cn` or vice versa, do just that, and update attributes according to data in the CSV. I say do not create new users or delete/disable old ones. If not, devise a way to do this, you can use other attributes if necessary.
Answer: I don't use the Quest AD cmdlets, so my answer will be based on the built-in ones. Also, I'm going to assume that the (unique) employee ID for any given employee is not going to change and that there are no user accounts with an empty employeeId attribute.
First, prepare your data like this:
Import-Module ActiveDirectory

$hrUsers = @{}
Import-Csv 'C:\path\to\your.csv' |
  select id, firstname, surname,
         @{n='inital';e={$_.forename.substring(0,1)}},
         @{n='name';e={$_.forename+" "+$_.surname}},
         @{n='dispname';e={$_.forename.substring(0,1)+" "+$_.surname}} |
  % { $hrUsers[$_.id] = $_ }

$adUsers = Get-ADUser -Filter * -Property employeeid |
           ? { 'testing', 'service' -notcontains $_.company }

This creates a hashtable mapping each employee ID to the object with the respective user's attributes (including the derivative attributes initial, name, and dispname) and a list of AD users (excluding service and test accounts).
With the above you can determine new users like this:
$employeeIDs = @($adUsers | select -Expand employeeId)
$hrUsers.Values | ? { $employeeIDs -notcontains $_.id }

obsolete accounts like this:
$adUsers | ? { $hrUsers.Keys -notcontains $_.employeeId }

and modified users like this:
$adUsers | ? {
  $hrUsers[$_.employeeid].surname -ne $_.lastname -or
  $hrUsers[$_.employeeid].inital -ne $_.firstname -or
  $hrUsers[$_.employeeid].name -ne $_.name -or
  $hrUsers[$_.employeeid].dispname -ne $_.displayname
}

Answer: I have managed to get it working by changing the search fields
    if($user.firstname -eq $_.firstname)
    if($user.surname -eq $_.sn)
    if($user.ID -eq $_.employeeID)

This now checks AD against the CSV, emails any discrepancies and excludes any emails with the firstname "test" or "careers"
write-host "Using default CSV file or C:\scripts\csv\StaffChanges.csv"
$StaffCSVUPath = "C:\scripts\csv\StaffChanges.csv"

$logfile = "C:\scripts\logs\ADvsHR.csv"

if(test-path $logfile) {
    remove-item $logfile -force
}

function Email {
    #Send an email, called with recipient email address and message body
    param(
        [string] $emailaddress="",
        [string] $bodymsg=""
    )
    $bodymsg += "
"
    $bodymsg += Get-Content($logfile)
    Send-MailMessage -To $emailaddress -From "email@example.com" -Subject "(AD-CMIS_errors) Errors found between Active Directory and CMIS" -Body $bodymsg -BodyAsHTML -SMTPServer "exchserver"
}

function CheckOutputFile {
    #Called with folder\filename and type of file
    param(
        [string]$outputfilename = "",
        [string]$type = ""
    )
    if(test-path($outputfilename)) {
    } else {
        write-host "Creating $outputfilename"
        $msg = "Forename,Surname,Username,ID"
        $msg | out-file($outputfilename)
    }
}

#Snap-ins needed to use the commands within the script
if((Get-pssnapin -Name Microsoft.Exchange.Management.Powershell.E2010 -ErrorAction SilentlyContinue) -eq $null){Add-PSSnapin  Microsoft.Exchange.Management.PowerShell.E2010}
if((Get-pssnapin -Name Quest.activeroles.admanagement -ErrorAction SilentlyContinue)-eq $null){Add-pssnapin Quest.activeroles.admanagement}

#import users from csv file
$users = (import-Csv $StaffCSVUpath)
$count=0
$countAD=0

Get-QADUser -searchroot "domain/Users/Staff" -SizeLimit 0 -includedproperties employeeid,displayname | ForEach-Object ($_.samaccountname) {
    $found = 0
    $countAD+=1
    ForEach ($user in $users) {
        $count+=1
        if ($user.firstname -eq $_.firstname) {
            if ($user.surname -eq $_.sn) {
                if ($user.ID -eq $_.employeeID) {
                            $found = 1
                }
            }
        }
        if ($found -eq 1){break}
    }
    if ($found -eq 0) {
        if(($_.firstname -ne "careers") -band ($_.firstname -ne "test")) {
            CheckOutputFile $logfile "LOG"
            $msg = "
" + $_.firstname +" " + $_.lastname + " "+$_.employeeid +"
"
            $msg | Out-File $logfile -append
        }
    }
}

if (test-path $logfile) {
    #If there is anything to report
    write-host "Emailing Log file to ict"
    #Email file if $outputB exists
    $email = "firstname.lastname@example.com"
    $body = "Action Required: The users below do not exist within HR.  Contact HR Data manager to resolve issue, delete users manually if required."
    #email ict
    Email $email $body
}

Compare AD against CSV


Classification with Least Angle (LARS)-type algorithm?

Question: I am currently working on the LARS (Least Angle Regression) method.
I know it is a regression method, but I wonder if, like LASSO or Ridge techniques (e.g. the package gmlnet in R), it can be modified in order to achieve classification rather than regression.
Comment: Well, LARS is an algorithm to fit Lasso models. Of course, [glmnet fits sparse logistic regressions](https://cran.r-project.org/web/packages/glmnet/vignettes/glmnet_beta.html#log), although it doesn't use LARS (see section 3.2.2 in [Hastie, Tibshirani & Wainwright](http://web.stanford.edu/~hastie/StatLearnSparsity/). I'd assume that the nonlinearity in GLMs precludes using a least-angle approach. Why are you interested in specifically generalizing LARS?
Comment: Thank you for the precision. I am working on an implementation of LARS in a software and I was asked to look for possible improvements. Besides, I may need classification algorithms designed for high dimension very soon. So I thought to try to adapt LARS to classification if it is possible. If it's not, I'll try to implement another algorithm.
Answer: The only thing you would need to change is the inclusion of a link function to map the outcomes to the appropriate scale, i.e. $\mathbb{R}^k \to (0,1)^k$ and change the loss function to be a categorical loss such as multinomial log loss over $k$ categories.
These are essentially the only points of departure between regression problems with real-valued outcomes and outcomes which are restricted so some particular set.


Does D automatically rewrite opBinary to opOpAssign?

Question: for example, assuming that T implements the right operator overloads:
T t1, t2, t3;
t3 = t1 + t2; // t3.opAssign(t1.opBinary!"+"(t2)) for sure
t3 = t3 + t2; // rewritten to t3.opOpAssign!"+"(t2) ?

Is the last operation optimized by D ?
Answer: No, it is not. It is not possible, because opBinary and opOpAssign could have different semantic:
struct S
{
    int val = 5;

    S opBinary(string op)(S rhs) if (op == "+")
    {
        return S(val + rhs.val);
    }

    void opOpAssign(string op)(S rhs) if (op == "+")
    {
        val = val - rhs.val;
    }
}

void main()
{
    import std.stdio;

    S s1, s2, s3;
    writeln(s3); // S(5)

    s3 = s3 + s2;
    writeln(s3); // S(10)

    s3.val = 5;
    writeln(s3); // S(5)

    s3 += s2;
    writeln(s3); // S(0)
}

Answer: What is opOpBinary? Did you mean opOpAssign?
And no, it doesn't. For example, appending (~=) and concatenating (~) arrays are different operations (the former preallocates extra space at the end and the latter will always reallocate).
Comment: What was motivation for down-voting this answer?
Comment: in the last example it's the same: `t3 = t3 + t2` and `t3 += t2`. I don't  know why is come with the cat operator.
Comment: it was an error.Tried to reset to 0 later but it didn't work. nvm, nothing personal.


Gitzilla: missing python http.cookiejar module

Question: I am trying to get gitzilla to work for me on ubuntu 14.04 with python 2.7.6 and git 1.9.1.
This is my first time using either python or gitzilla.
After installing gitzilla and adding the symlinks for git hooks, pushing changes gives the following output:
geoff@jenkins:~/Example$ git push origin master
Counting objects: 5, done.
Writing objects: 100% (3/3), 282 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: Traceback (most recent call last):
remote:   File "hooks/update", line 9, in <module>
remote:     load_entry_point('gitzilla==2.0', 'console_scripts', 'gitzilla-update')()
remote:   File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 351, in load_entry_point
remote:     return get_distribution(dist).load_entry_point(group, name)
remote:   File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 2363, in load_entry_point
remote:     return ep.load()
remote:   File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 2088, in load
remote:     entry = __import__(self.module_name, globals(),globals(), ['__name__'])
remote:   File "build/bdist.linux-x86_64/egg/gitzilla/hookscripts.py", line 11, in <module>
remote:   File "build/bdist.linux-x86_64/egg/gitzilla/hooks.py", line 9, in <module>
remote:   File "build/bdist.linux-x86_64/egg/gitzilla/utils.py", line 10, in <module>
remote:   File "/usr/local/lib/python2.7/dist-packages/bugz/bugzilla.py", line 7, in <module>
remote:     import http.cookiejar
remote: ImportError: No module named http.cookiejar
remote: error: hook declined to update refs/heads/master
To /home/geoff/gitrepos/Example.git/
 ! [remote rejected] master -> master (hook declined)
error: failed to push some refs to '/home/geoff/gitrepos/Example.git/'

From this I figure I need http.cookiejar (or cookielib) installed but I don't know how to get it.
I've tried without success:
pip install http.cookiejar and
pip install cookielib
Both times pip complained about not finding anything but then I tried pip install cookiejar which did appear to install it, but I still get the same error
pip install cookiejar
Now this runs without complaint: python -c "import cookiejar"
And this complains the module isn't found: python -c "import http.cookiejar"
Any ideas how to get this working?
Comment: `http.cookiejar` is a python3 thing, but from what I see you're trying to use python2.7 where this is called `cookielib`. I think you have versions issue where some of your modules are Python3 and some Python2.7
Comment: I reinstalled it with `sudo python3 setup.py install` which seems to have fixed it.  (now it's giving me a configuration issue to resolve :)
Comment: Thanks - I'll try upgrading python
Comment: Thanks Alexander, `python3 -c "import http.cookiejar"` works.  Do you know how to tell gitzilla to use python3?  I was thinking maybe an alias but I don't think I should make it system wide because I don't know what may be using it.
Comment: good that it's all working now :)
Comment: There were a few issues going on.  I ended up porting gitzilla to python3 and adding token authentication required by the new Bugzilla versions.  For anyone that's looking, you can get it here: https://github.com/gtait/gitzilla
Answer: Install the cookiejar using
    pip install cookiejar
Comment: this comment helped a lot. then i imported using `from http.cookiejar import CookieJar`
Answer: Alexander's comment pointed out the problem (almost 3 years ago) which led to the answer shown in the comments to the question.  As seen in the comments, it was a mixup between packages from python2.7 and python3.  If you came here looking to resolve the same actual problem with gitzilla, the patch has been entered back into the master branch now so please use a recent version or the one I linked to in my comments above.
I'm adding this to close the question to stop answer spamming.


Weyl Tensor undefined or vanishing?

Question: So we have that the Weyl tensor in component form satisfies in dimension $n$
\begin{equation}
C_{abcd}=R_{abcd} -\frac{2}{n-2}\left(R_{a[c}g_{b]d}+R_{b[d}g_{c]a} \right)+\frac{2}{(n-1)(n-2)}Rg_{a[c}g_{b]d}
\end{equation}
The confusion is simple really. Many texts say that when $n=2$, the Weyl Tensor vanishes; but I stumbled across a text today (Gravitation: Foundations and Frontiers, By T. Padmanabhan - a great read to add) which says that it the Weyl tensor is in fact undefined in the first place for $n=2$!
After having used the fact that in two dimensions, the Riemann tensor can be expressed for $n=2$ as
\begin{equation}
R_{abcd}=Rg_{a[c}g_{d]b}=\frac{1}{2}R(g_{ac}g_{bd}-g_{ad}g_{bc})
\end{equation}
Without substituting in the value $n=2$, The above expression for the Weyl tensor seems to reduce to (using the Riemann Tensor defined above)
\begin{equation}
C_{abcd}=\frac{1}{2}R \left( \frac{n+1}{n-1} \right)(g_{ad}g_{bc}-g_{ac} g_{bd})
\end{equation}
which does not seem to in general vanish for $n=2$. So is it just the case that, looking at the above formula, the $n=2$ case is ill defined (as mentioned in the book) for some specific reason, or that indeed the Weyl tensor does in fact always vanish?
Thanks.
Answer: Your formula for the Weyl tensor is wrong. I haven't double-checked my computation, but I think the first term in parentheses should be $R_{a[c}g_{d]b}$, not $R_{a[c}g_{b]d}$, and the last term should be a multiple of $Rg_{a[c}g_{d]b}$.
In any case, there has to be something wrong with your formula, because the curvature tensor satisfies $R_{abcd}=\frac{1}{2}R(g_{ac}g_{bd}-g_{ad}g_{bc})$ for any metric with constant sectional curvature in any dimension, and for all such metrics, the Weyl tensor is identically zero.
The question of whether Weyl is zero or undefined in dimension $2$ depends on how you define it. If you define it by (the corrected version of) the formula you wrote down, then it clearly is undefined in dimension $2$ because of the $(n-2)$'s in the denominator. But there's another way to define it that does make sense in dimension $2$: Let $V$ be a finite-dimensional inner product space, and let $\mathscr R(V^*)$ denote the space of all algebraic curvature tensors on $V$ (covariant $4$-tensors that have the symmetries of the curvature tensor). Define $\operatorname{trace}\colon \mathscr R(V^*)\to \operatorname{Sym}^2(V^*)$ by
$$
(\operatorname{trace}R)_{ac} = g^{bd} R_{abcd}.
$$
Then you can define the Weyl tensor as the orthogonal projection of the Riemann tensor onto the kernel of the trace operator. This yields the usual formula in dimensions $4$ and up, but it yields zero in dimensions $2$ and $3$ because the trace operator is injective in those dimensions. So in that sense, the Weyl tensor is identically zero in dimensions $2$ and $3$.
Comment: On more thing to add to this excellent answer is that in dimension 3 the obstruction to conformal flatness of the metric is the [Cotton tensor](https://en.wikipedia.org/wiki/Cotton_tensor).
Comment: Ok, so in fact there is a "better definition" than say the one provided [here](https://en.wikipedia.org/wiki/List_of_formulas_in_Riemannian_geometry#Weyl_tensor). That's reassuring. Absolutley - it makes sense since the Weyl Tensor must be the traceless (ie orthogonal projection) component of the Riemann Tensor with its symmetries. In other words, one should use
Comment: $C=R-\frac{1}{n-2}\left(Ric-\frac{s}{n}g\right){~\wedge \!\!\!\!\!\!\bigcirc ~}g-\frac{s}{2n(n-1)}g{~\wedge \!\!\!\!\!\!\bigcirc ~}g$ and that the Einstein Tensor vanishes, likewise the first and third term for $n=2,3$

Weyl Tensor undefined or vanishing?


Understanding the functor category $\text{C}^X$ for $X$ a discrete set

Question: From Mac Lane's Category Theory:

Let $C$ be a category and let $X$ be a discrete set and let $A = \text{C}^X$ be the functor category.
Then $A$ has as its objects the $X$-indexed families $a = \{a_x : x \in X\}$ of objects of $C$.

I'm having a difficult time seeing how the objects of $A$ are $x$-indexed families of $C$.
If a functor $(F : X \rightarrow \text{C})$ is an object of $A$, then it's defined entirely on where it sends each $x \in X$.  So I can see how an object of $A$ should be a collection of objects of $C$ that are defined by the particular functor $F$, i.e., an object of $A$ should be $a_F = \{c \in C : F(x) = c\ \text{for some $x \in X$}\}$.
But I'm not seeing how the objects are $x$ indexed families.
Can someone explain what I'm missing in the analogy here?
Comment: I guess you mean $C=\operatorname{Set}$?
Comment: I mistyped that, $C$ should just be a category, not one in particular.
Answer: You are confusing the functor $F$ and its image in $C$.
Just like a function $f:X\to Y$ between sets is not the set $\{y\in Y\,|\, f(x)=y \text{ for some } x\in X\}$, a functor $F:X\to C$ is not $\{c\in C\,|\, F(x)=c \text{ for some } x\in X\}$.
Rather, $F:X\to C$ is the association of some $c\in C$ for each $x\in X$, so we may write $c_x\in C$ for this particular element, and this gives a family $\{c_x\in C\,|\, x\in X\}$. The fact that $X$ is discrete as a category implies that the functor is determined by just this family (there is no need to worry about the action on morphisms).
Comment: So it's essentially: $c = c_F = \{c_x \in C | x \in X, \text{with the transformation $F(x) = c_x$}\}$?
Comment: I don't understand what you mean by "transformation". When $X$ is discrete, a functor $F:X\to C$ is the same thing as a simple function from the set $X$ to the set of objects of $C$. A function is the same thing as a family (by definition).
Comment: I will be more clear.  What I mean is: each object is a functor, and each set indexed by $x$ depends on the functor chosen.
Comment: You should rather say "indexed by $X$", but yes.


Unbounded set in $\ell^2$

Question: Let $H=\{(x_n) \in \ell^2 : \sum_{n=1}^\infty \frac{x_n}{n}=1\}$
I want to prove that $H$ is unbounded in $\ell^2$.
My attempt:
Define $T : \ell^2 \rightarrow K$ (where $K$ is scalar field) by $T(x_n)=\sum_{n=1}^\infty \frac{x_n}{n}$
So $T$ is bounded linear functional so $T$ is continuous and $H$ is closed because it is inverse image of $\{1\}$. But I don't know how to proceed further to prove $H$ is unbounded.
Answer: Let $x^{n}$ have $n$ in position $n$ and $0$ elsewhere. Then $x^{n} \in H$ and $\|x^{n}\|=n$. Hence, $H$ is unbounded.

Unbounded set in $\ell^2$


Problemas $scope mdDialog angularjs

Question: Bom dia galera!
Estou com problemas ao passar um array para um md-dialog.
Ao clicar em um botão editar é enviado o array que está no $scope via locals para o controller do md-dialog, para que eu possa fazer alteração dos dados e salvá-los. Mas o problema é que toda a modificação que eu faço no array dentro do md-dialog é aplicada simultaneamente no array da pagina que está "atrás" do dialogo(página que enviou os dados), mesmo se eu cancelar o diálogo.
Por exemplo: Eu passo o array com várias linhas para o dialog, e se eu remover uma linha do array lá neste dialog ele também remove a linha do array da página de envio. Parece que eles estão vinculados e tudo que acontece em um se repete no outro mesmo se eu cancelar o diálogo. Dai quando eu volto no editar array está sem a linha que foi excluída antes mesmo tendo cancelado o dialog.
Agradeço desde já quem ajudar.
Abraço!
Comment: É possível compartilhar o código referente ao seu problema?
Answer: Sim, eles estão ligados, é uma feature do AngularJS, é a raiz do modo como ele funciona, o MVVM.
Se você quer fazer modificações no seu array dentro do escopo do md-dialog e se esse array precisa ser igual ao do escopo pai, você pode usar o angular.copy() para criar uma cópia do array e passar ele para o md-dialog.
Comment: Perfeito Juan!! Não fazia ideia da existência deste comando. Estou começando com o angular e me ajudou muito aqui. Obrigado mesmo. Grande abraço!

Problemas $scope mdDialog angularjs


Linux - AlpsPS/2 ALPS GlidePoint touchpad treated as mouse on HP zBook Studio G4

Question: I'm having issues with the 'AlpsPS/2 ALPS GlidePoint' (HP zBook Studio G4). It is working but is treated as a mouse device, effectively resulting in all the settings I make in 'Mouse and Touchpad' > 'Touchpad' being ignored. E.g. I can disable 'Tap to click' but with no effect. I can even disable 'Enable touchpad' and it will still work.
These are the relevant lines from the Xorg log file:
[     9.584] (II) config/udev: Adding input device AlpsPS/2 ALPS GlidePoint (/dev/input/event6)
[     9.584] (**) AlpsPS/2 ALPS GlidePoint: Applying InputClass "evdev touchpad catchall"
[     9.584] (**) AlpsPS/2 ALPS GlidePoint: Applying InputClass "evdev touchscreen catchall"
[     9.584] (**) AlpsPS/2 ALPS GlidePoint: Applying InputClass "touchpad catchall"
[     9.584] (**) AlpsPS/2 ALPS GlidePoint: Applying InputClass "Default clickpad buttons"
[     9.584] (**) AlpsPS/2 ALPS GlidePoint: Applying InputClass "libinput touchpad catchall"
[     9.584] (**) AlpsPS/2 ALPS GlidePoint: Applying InputClass "libinput touchscreen catchall"
[     9.584] (II) Using input driver 'libinput' for 'AlpsPS/2 ALPS GlidePoint'
[     9.584] (**) AlpsPS/2 ALPS GlidePoint: always reports core events
[     9.585] (II) input device 'AlpsPS/2 ALPS GlidePoint', /dev/input/event6 is tagged by udev as: Touchpad Touchscreen
[     9.585] (II) input device 'AlpsPS/2 ALPS GlidePoint', /dev/input/event6 is a touchpad
[     9.616] (II) XINPUT: Adding extended input device "AlpsPS/2 ALPS GlidePoint" (type: TOUCHPAD, id 17)
[     9.616] (**) AlpsPS/2 ALPS GlidePoint: (accel) selected scheme none/0
[     9.616] (**) AlpsPS/2 ALPS GlidePoint: (accel) acceleration factor: 2.000
[     9.616] (**) AlpsPS/2 ALPS GlidePoint: (accel) acceleration threshold: 4
[     9.616] (II) input device 'AlpsPS/2 ALPS GlidePoint', /dev/input/event6 is tagged by udev as: Touchpad Touchscreen
[     9.616] (II) input device 'AlpsPS/2 ALPS GlidePoint', /dev/input/event6 is a touchpad
[     9.616] (II) config/udev: Adding input device AlpsPS/2 ALPS GlidePoint (/dev/input/mouse1)
[     9.616] (**) AlpsPS/2 ALPS GlidePoint: Ignoring device from InputClass "touchpad ignore duplicates"

This is the output of 'xinput':
$ xinput --list
⎡ Virtual core pointer                       id=2   [master pointer  (3)]
⎜   ↳ Virtual core XTEST pointer                 id=4   [slave  pointer  (2)]
⎜   ↳ ALP0012:00 044E:120C                       id=10   [slave  pointer  (2)]
⎜   ↳ Generic USB Audio                          id=14   [slave  pointer  (2)]
⎜   ↳ PixArt Microsoft USB Optical Mouse         id=15   [slave  pointer  (2)]
⎜   ↳ AlpsPS/2 ALPS GlidePoint                   id=17   [slave  pointer  (2)]
⎣ Virtual core keyboard                      id=3   [master keyboard (2)]
    ↳ Virtual core XTEST keyboard                id=5   [slave  keyboard (3)]
    ↳ Power Button                               id=6   [slave  keyboard (3)]
    ↳ Video Bus                                  id=7   [slave  keyboard (3)]
    ↳ Sleep Button                               id=8   [slave  keyboard (3)]
    ↳ HP HD Camera: HP HD Camera                 id=9   [slave  keyboard (3)]
    ↳   USB Keyboard                             id=11   [slave  keyboard (3)]
    ↳   USB Keyboard                             id=12   [slave  keyboard (3)]
    ↳ Sennheiser Sennheiser SC60 for Lync        id=13   [slave  keyboard (3)]
    ↳ AT Translated Set 2 keyboard               id=16   [slave  keyboard (3)]
    ↳ HP Wireless hotkeys                        id=18   [slave  keyboard (3)]
    ↳ HP WMI hotkeys                             id=19   [slave  keyboard (3)]
    ↳ Generic USB Audio                          id=20   [slave  keyboard (3)]

I've manually disabled tapping as follows (but still no effect):
enter code herexinput set-prop 'AlpsPS/2 ALPS GlidePoint' 'libinput Tapping Enabled' 0
-->
$ xinput --watch-props 'AlpsPS/2 ALPS GlidePoint'
Device 'AlpsPS/2 ALPS GlidePoint':
   Device Enabled (142):   1
   Coordinate Transformation Matrix (144):   1.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 0.000000, 1.000000
   libinput Tapping Enabled (296):   0
   libinput Tapping Enabled Default (297):   0
   libinput Tapping Drag Enabled (298):   1
   libinput Tapping Drag Enabled Default (299):   1
   libinput Tapping Drag Lock Enabled (300):   0
   libinput Tapping Drag Lock Enabled Default (301):   0
   libinput Accel Speed (279):   0.996016
   libinput Accel Speed Default (280):   0.000000
   libinput Natural Scrolling Enabled (284):   1
   libinput Natural Scrolling Enabled Default (285):   0
   libinput Send Events Modes Available (263):   1, 1
   libinput Send Events Mode Enabled (264):   0, 0
   libinput Send Events Mode Enabled Default (265):   0, 0
   libinput Left Handed Enabled (286):   0
   libinput Left Handed Enabled Default (287):   0
   libinput Scroll Methods Available (288):   1, 1, 0
   libinput Scroll Method Enabled (289):   1, 0, 0
   libinput Scroll Method Enabled Default (290):   1, 0, 0
   libinput Click Methods Available (302):   1, 1
   libinput Click Method Enabled (303):   1, 0
   libinput Click Method Enabled Default (304):   1, 0
   libinput Middle Emulation Enabled (293):   0
   libinput Middle Emulation Enabled Default (294):   0
   libinput Disable While Typing Enabled (305):   1
   libinput Disable While Typing Enabled Default (306):   1
   Device Node (266):   "/dev/input/event6"
   Device Product ID (267):   2, 8
   libinput Drag Lock Buttons (295):   <no items>
   libinput Horizonal Scroll Enabled (268):   0

Any hints on how to get this thing working as an actual touchpad would be much appreciated! :)
EDIT: This is not a specific Mint issue - I tested with latest Fedora and it is the same problem there (I hoped that a newer libinput/gtk/whatever version would probably solve the issue).
EDIT 2: Doesn't work on KDE/Kubuntu w/ latest Plasma either.
Answer: I had this same issue. Fixed it by doing:
sudo modprobe -r psmouse; sudo modprobe psmouse
Answer found here: https://askubuntu.com/a/625064


How to show and hide div based on php condition

Question: My php code
<?php
session_start();

if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true){
    echo "logged in";
?>

<?php
}
else {
    echo "not logged in";

?>

<?php
}
?>

and html is

                Login

                     Loggedin

I just wanna show one div at a time. How to fix this?? However php echo funtion is working based on condition.
Comment: you could put your div in the PHP
Answer: Instead of putting the JavaScript code inside your PHP, just put the divs in there.
<?php
session_start();
?>

<?php
if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true) {
?>

           Loggedin

<?php
} else {
?>

           Login

<?php
}
?>

Comment: fyi, there is a handy language construct called empty()
Answer: Let PHP handle the work and don't involve JavaScript at all.
session_start();

if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] == true){

    echo '

             Login

';

    } else {

    echo '

             Loggedin

';
}

Answer: Some logic:
<?php

session_start();

if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true) {
    $logged = true;
} else {
    $logged = false;
}

?>

Your page content:
<?php if ($logged): ?>

I am displayed!

<?php else: ?>

I am displayed instead!

<?php endif; ?>

Answer: Simple php:
<?php
if($id > 0){
    echo '
'.$id.'
';
}
?>

Or add hide class to div in php:
<div class="oneClassName <?php if($id > 0){ echo 'hide'; } ?>">.....

And css:
.hide{display: none;}

Answer: you could wrap your divs with php

    <?if(($_SESSION["loggedin"]??false) === true):?>

             Loggedin

    <?else:?>

             Login

    <?endif;?>

Answer: Another way;
    <input type="hidden" id="status" value="<?php echo $_SESSION['loggedin']; ?>">


How can I make masonry and Imagesloaded work correct. (wordpress)

Question: Im trying to style a grid using the built in Masonry in Wordpress 3.9. It works great, except that the grid fails to display correct at first pageload, but adjust on the second. I guess this is because I don't call in ImagesLoaded, and the grid calculates before the images are loaded. This is the site: http://www.skateflix.se
To iniate Masonry I use this in Functions:
function my_masonry(){
wp_enqueue_script('masonry');
}
add_action('wp_enqueue_scripts', 'my_masonry');

I tried this to make ImagesLoaded to work but failed, placed in the header:

My HTML looks like this:
<div class="js-masonry"
<div class="feed-item"
<div class="feed-item"
<div class="feed-item"
<div class="feed-item"

What am I doing wrong here? Im a bit confused. Does the version of Masonry that ships with wordpress includes ImagesLoaded, or do I have to link that in myself?
Answer: Ok Made it work. Placed this function in a .js file (change query selector and Itemselector for your own container and the classes inside it):
(function( $ ) {
"use strict";
$(function() {
//set the container that Masonry will be inside of in a var
var container = document.querySelector('.js-masonry');
//create empty var msnry
var msnry;
// initialize Masonry after all images have loaded
imagesLoaded( container, function() {
    msnry = new Masonry( container, {
        itemSelector: '.feed-item'
    });
});
});
}(jQuery));

And then enqueue it in functions.php
wp_enqueue_script( 'masonry' );
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'masonryInit', get_stylesheet_directory_uri().'/js/masonry.js',      array( 'masonry', 'jquery' ) );


Proving that K-means corresponds to an EM algorithm?

Question: Just wanted to make sure that my proof is correct and that I am not missing anything in the process. Any thoughts?
"
To demonstrate mathematically that the K-means algorithm corresponds to an algorithm of Expectation Maximization, we need to show that the K-means algorithm can be derived from the EM algorithm for a specific choice of the likelihood function and the prior distribution.
The K-means algorithm can be derived from the EM algorithm for a mixture of K Gaussians with equal covariance matrix. In this case, the likelihood function is given by:
$$p(x_n|z_n=k,\mu_k,\Sigma)=\frac{1}{(2\pi\sigma^2)^{D/2}}\exp\left(-\frac{1}{2\sigma^2}\|x_n-\mu_k\|^2\right)$$
where $x_n$ is the $n$-th data point, $z_n$ is the latent variable indicating the cluster assignment of $x_n$, $\mu_k$ is the mean of the $k$-th cluster, $\Sigma$ is the covariance matrix, and $\sigma^2$ is the common variance.
The prior distribution is given by:
$$p(z_n=k)=\frac{1}{K}$$
which assumes that each cluster is equally likely.
The EM algorithm for this mixture of Gaussians is given by:

Initialize the means $\mu_k$ randomly.
E-step: Compute the posterior probability of the cluster assignment $z_n$ given the data $x_n$ and the current means $\mu_k$:

$$r_{nk}=\frac{\exp\left(-\frac{1}{2\sigma^2}\|x_n-\mu_k\|^2\right)}{\sum_{j=1}^K\exp\left(-\frac{1}{2\sigma^2}\|x_n-\mu_j\|^2\right)}$$
where $r_{nk}$ is the responsibility of the $k$-th cluster for the $n$-th data point.

M-step: Update the means $\mu_k$ using the responsibilities:

$$\mu_k=\frac{\sum_{n=1}^N r_{nk}x_n}{\sum_{n=1}^N r_{nk}}$$

Repeat steps 2 and 3 until convergence.

Now, let's show that the K-means algorithm corresponds to this EM algorithm. The K-means algorithm is given by:

Initialize the means $\mu_k$ randomly.
Assignment step: Assign each data point to the closest cluster mean:

$$z_n=\arg\min_k\|x_n-\mu_k\|^2$$

Refitting step: Update the means $\mu_k$ to the average of the data points assigned to them:

$$\mu_k=\frac{1}{N_k}\sum_{n:z_n=k}x_n$$
where $N_k$ is the number of data points assigned to the $k$-th cluster.
We can see that the K-means algorithm corresponds to the EM algorithm for the mixture of Gaussians with equal covariance matrix by setting $\sigma^2=\frac{1}{2}$ and $r_{nk}=1$ if $z_n=k$ and $r_{nk}=0$ otherwise. In this case, the likelihood function reduces to:
$$p(x_n|z_n=k,\mu_k)=\frac{1}{\sqrt{2\pi}}\exp\left(-\frac{1}{2}\|x_n-\mu_k\|^2\right)$$
which is the Gaussian distribution with unit variance. The prior distribution is given by:
$$p(z_n=k)=\frac{1}{K}$$
which assumes that each cluster is equally likely.
The E-step of the EM algorithm reduces to:
$$r_{nk}=\begin{cases}1 & \text{if }z_n=k\\0 & \text{otherwise}\end{cases}$$
which is equivalent to the assignment step of the K-means algorithm.
The M-step of the EM algorithm reduces to:
$$\mu_k=\frac{\sum_{n=1}^N r_{nk}x_n}{\sum_{n=1}^N r_{nk}}=\frac{1}{N_k}\sum_{n:z_n=k}x_n$$
which is equivalent to the refitting step of the K-means algorithm.
Therefore, we have shown that the K-means algorithm corresponds to an algorithm of Expectation Maximization for a specific choice of the likelihood function and the prior distribution."
Comment: Please state exactly what is the statement you want to prove along with the necessary assumptions. Only then it's possible to prove or disprove the result.
Comment: the literal prompt i was given is "prove that k-means corresponds to an EM algorithm".
Answer: The proof is wrong because $\sigma^2 = 1/2$ allows $r_{nk}$ to take on values between 0 and 1.  For the correct proof see Deriving K-means algorithm as a limit of Expectation Maximization for Gaussian Mixtures


PHP - MySQL query with Pagination

Question: How would I go about making a pagination script for this MySQL & PHP query.
if (isset($_GET['c'])) {
$c = $_GET['c'];
}

$query = mysql_query("SELECT * FROM Categories WHERE category = '$c' ");

        WHILE($datarows = mysql_fetch_array($query)):

        $id = $datarows['id'];
        $category = $datarows['category'];
        $code = $datarows['code'];

        endwhile;

$query2 = mysql_query("SELECT * FROM Games WHERE category = '$code' ");

WHILE($datarows_cat = mysql_fetch_array($query2)):

        $title = $datarows_cat['title'];
        $description = $datarows_cat['description'];
        $imgurl = $datarows_cat['image_name'];
        $category = $datarows_cat['category'];
        $views = $datarows_cat['view_count'];
        $pagename = $datarows_cat['pagename'];
                $featured = $datarows_cat['featured'];

if ($featured =="1") {$f = " Featured"; } else {$f = "";}
                    if(is_int($views/2)) {
$views = $views / 2;
} else { $views = $views / 2 + .5; }

if (strlen($description) > 95) {
    $desc= substr($description,0,95);
    $desmod = "$desc...
- Read More";
    }
else {$desmod = "$description";}

        echo "$f - $title - $desmod
";

endwhile;

And when I visit http://my-site.com/categories/Action for instance, The code looks up that category in my category table, then once it gets the unique code for that category, It runs another query to find all games in another table with that category code. Currently, however, I have 200+ games loading for a single category which causes a great amount of loading time.
Thanks for your help!
Answer: First of all find out how many games are there for a specific category
change the line
$query2 = mysql_query("SELECT * FROM Games WHERE category = '$code' ");

to
$sql="SELECT * FROM Games WHERE category = '$code' ";
$query_count=mysql_query($sql);

Add following after it
$per_page =30;//define how many games for a page
$count = mysql_num_rows($query_count);
$pages = ceil($count/$per_page);

if($_GET['page']==""){
$page="1";
}else{
$page=$_GET['page'];
}
$start    = ($page - 1) * $per_page;
$sql     = $sql." LIMIT $start,$per_page";
$query2=mysql_query($sql);

Then display the numbers of pages where you want

        <?php
        //Show page links
        for ($i = 1; $i <= $pages; $i++)
          {?>
          <li id="<?php echo $i;?>"><a href="linktoyourfile?c=<?php echo $c;?>&page=<?php echo $i;?>"><?php echo $i;?>

          <?php
          }
        ?>

Use CSS for pagination this will do the trick
Comment: When I do this, I get the error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/mcabinet/public_html/cat.php on line 34
Comment: And that line is: WHILE($datarows_cat = mysql_fetch_array($query2)):
Comment: Oh sorry friend. I have edited the answer. now its okay. check it out
Comment: Awesome! Thank You so much for your help! I've accepted your answer and +1.
Comment: Hey there! Is there any way out with which we can arrange all the rows in vice versa order. meaning the last page be the first page.
Answer: //database connation
<?php
    $conn = new mysqli("localhost", "root", "","database_name");
?>
<!DOCTYPE html>
enter code here

    <title>View Student Details</title>

 Student Details

    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">

            <form>

                        Sr.No.
                        Student ID
                        Student Name
                        Class
                        Gender
                        Birth of Date
                        Contact No.
                        Action

                    <?php
                        $count=0;

                        if(isset($_GET['page_count']))
                        {
                            $count=1;
                            $page_count=$_GET['page_count']-1;
                            $count=$page_count*10;
                        }

                        $q="SELECT * from student_detail LIMIT $count,10";
                        $result=$conn->query($q);
                        $j=0;
                        while($data=$result->fetch_array())
                        { $j=$j+1;
                    ?>

                        <?php echo $j ?>
                        <?php echo $data['std_id'] ?>
                        <?php echo $data['std_name'] ?>
                        <?php echo $data['std_class'] ?>
                        <?php echo $data['gender'] ?>
                        <?php echo $data['bod'] ?>
                        <?php echo $data['contact'] ?>

                                    <a href="delete_master.php?std_id=<?php echo $data['std_id']; ?>" class="btn btn-danger">Delete
                                    <a href="update.php?std_id=<?php echo $data['std_id']; ?>" class="btn btn-danger">Update

                    <?php } ?>

                    <?php

                        $q="SELECT count(std_id) from student_detail";
                        $result=$conn->query($q);
                        $data=$result->fetch_array();
                        $total=$data[0];
                        $total_page=ceil($total/10);

                        if($total_page>1)
                        {
                            for($i=1;$i<=$total_page;$i++)
                            {
                    ?>

<a href="view.php?page_count=<?php echo $i;?>" name="page_count" id="page_count"><?php echo $i; ?>

                    <?php
                            }
                        }
                    ?>

            </form>

Answer:     Pagiantion, it is working simple and easy
       <?php
        $sql = "SELECT COUNT(id) FROM contact_info";

        $rs_result  = $conn->query($sql);
        $row = mysqli_fetch_row($rs_result);
        $total_records = $row[0];
        echo $total_records;
        $previous   = 1;
        $total_pages = ceil($total_records / $limit);
        $next  = $_GET["page"] + 1;
        $previous  = $_GET["page"] - 1;
        $pagLink = "";
        if($previous ==0)
        {
        $prev  = "Previous";
        }
        else
        {
          $prev  = "Previous";
        };
        echo $prev;

        "
";
        for ($i=1; $i<=$total_pages; $i++)
        {
        $pagLink .= "".$i."";
        };
        echo $pagLink;

        $nex  = "Next";
        echo $nex;

        ";

";
      ?>

Get all data from database.

            $limit = 1;
            if (isset($_GET["page"]))
            {
                $page  = $_GET["page"];
            }
            else
            {
             $page=1;
            }
            $start_from = ($page-1) * $limit;
            $sql = "SELECT * FROM contact_info ORDER BY id desc LIMIT $start_from , $limit";

Comment: Code only answers arent encouraged as they dont provide much information for future readers please provide some explanation to what you have written
Answer: $page = 1;
$limit = 10;
$offset = ($limit * $page) - $limit;

$query = mysqli_query(
    $connect,
    "SELECT * FROM Games WHERE category = '$code' limit $limit offset $offset"
);

PHP - MySQL query with Pagination


How to change order of integration?

Question: $$\int_0^{\frac{\pi}{2}}\int_0^{2a \cos \theta} f(r, \theta) \ dr \ d\theta$$
Hi everyone, Can you please help me in changing the order of integration in polar coordinates?
A general theory or method will be appreciated. I had searched a lot but unable to find the related theory.
Thanks
Comment: $$ \int_0^{\pi/2}\int_0^{2a\cos(\theta)}f(r,\theta)\,\mathrm{d}r\,\mathrm{d}\theta =\int_0^{2a}\int_0^{\cos^{-1}\left(\frac{r}{2a}\right)}f(r,\theta)\,\mathrm{d}\theta\,\mathrm{d}r $$
Comment: This doesn't actually look like a case where changing the order is likely to serve any purpose.
Comment: Usually one starts with plotting equations and looking at them.
Comment: @MichaelHardy, in the question it's asked to change the order.
Answer: Here is a slightly different way to do it
The region is a semicircle centered at (a,0)
i.e.
$x^2 + y^2 = 2ax\\
x = r\cos \theta\\y=r\sin\theta\\
r^2 = 2ar\cos \theta\\
r = 2a\cos\theta$
$u = r-a\\
\phi = 2\theta$
$\int_0^a\int_0^\pi f(u+a, \frac {\phi}{2}) (2\ d\phi \ du)$
Not exactly what you were asked for.
Comment: I don't see no circle.
Comment: plot $r =  2a \cos \theta$  You will get a circle.
Comment: Erm, so you assert that $(\theta,\cos\theta)$ desribes a circle when $\theta\in [0,\pi/2]$?
Comment: I have added some detail for you,
Comment: I still don't see any circle.
Comment: Then how would you represent a circle of radius a, centered a units from the origin in polar coordinates?
Comment: Now, I see what you are saying. It's in polar coordinates only. Ok, then fine. I see the circle now. :-)

How to change order of integration?


Automate switch user in script

Question: I have to write a script on ubuntu and some commands require root privileges. The script is full of normal linux commands. Is it possible to write a switch user(su) script that automates the password prompt without needing user intervention?. Need help...
I forgot to tell you that I'm asked to disable sudo access to all users and I did that. Alternate option?
Thank you!
Answer: you should use sudo. In the file /etc/sudoers you can configure the commands which should be run with root privileges and whether a password is needed or not.
Answer: Have a look at sudo. With this you can run specific commands with root privileges with or without the requirement to give a password.
It should already be installed on your Ubuntu system
EDIT
Sudo has been designed to do exactly what you want in a secure, controlled and auditable manner. The safest thing to do is to allow users to run the script via sudo. This doesn't then allow users access to the individual commands.
Discussion with the policy maker's should ensue.
Edit 2
You have to grant a user permission to run a particular shell via sudo before they can successfully run sudo -s. Just giving a user permission to run your script does not grant permission to run sudo -s.
Comment: I forgot to tell you that I'm asked to disable sudo access to all users and I did that. Alternate option?
Answer: Try 'bash -p', if -p not given then bash will keep the original uid/gid even if suid.
Comment: But I do agree, you probably should not solve it this way ;).
Comment: you mean to say just type 'bash -p'? and this way I didn't see any output and it is not changed to root user.
Comment: Sorry I misunderstood the question. If you were to create a suid bash, the bash -p would work. but like someone mentioned earlier, you cannot suid shell scripts
Answer: As you said, you are unable to use sudo, you may think the other way around. Are you able to run the script as root and su to an user with lesser permission for the parts which do not need to be run as root?
As already said, sudo is exactly what you need and the best solution (you know, that you can limit sudo access for each command separate?)
Answer: The approach you describe would require that anyone who could run the script would be able to read the script - and hence the root password.

I'm asked to disable sudo access to all users

! you've been asked to disable the sub-system which has been specifically designed by some very smart people to provide limited root access to designated users and now you need to provide root access to designated users with your own home grown solution (and have proposed a very bad way of solving the problem).
You could set the sticky bit on the file permissions - but it'd take a very long time to go into the implications of doing that - and it seems that neither you nor the person who asked you to disable sudo really understand the Unix permissions model.
Why not just lose the pretence and give the users the root password?
Comment: The main reason to disable sudo is to tighten the security as any one can use the command "sudo -s" and gain acces to root by entering his own password.
Comment: @user53864: You can selectively enable commands and their options using settings in `/etc/sudoers` (use `visudo` to edit).
Answer: Do your own wrapper:
/* mywrapper.c */
#include <unistd.h>

extern char **environ;

main() {
    environ=NULL; /* clear environment variables for sanity */
    setuid(0);
    return execl("/usr/local/bin/script.sh", "script.sh", NULL);
}

Then gcc -o mywrapper mywrapper.c ; chown root mywrapper; chmod u+s mywrapper and you're ready to go.
Comment: That's a workaround for the shell resuming normal privileges when running a setuid script - not a solution to the OPs problem

Automate switch user in script


Can't remove forced HTTPS redirect from Nginx?

Question: I had originally setup my Nginx configuration to automatically redirect HTTP to HTTPS. Due to my work security policy and Cloudflare integration, HTTPS will not resolve inside my network.
I'd simply like to remove the forced redirect to HTTPS. But after commenting out the block with the URI rewrite, the server still redirects automatically.
Here's my default config for Nginx
#HTTPS redirect (if necessary)
#server {
#       listen      80;
#       server_name example.com;
#       rewrite     ^   https://$server_name$request_uri? permanent;
#}

server {
        listen 80;
        listen [::]:80 default_server ipv6only=on;
        listen 443 ssl spdy;
        root /var/www/example/current/public;
        index index.php index.html index.htm;

        spdy_chunk_size 8k;
        spdy_headers_comp 7;

        server_name example.com;
        # Point to ssl certificates
        ssl_certificate /root/example.com.crt;
        ssl_certificate_key /root/example.com.key;
        # Allow only secure TLS protocols
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        #ssl_prefer_server_ciphers on;
        #ssl_ciphers AES256+EECDH:AES256+EDH:!aNULL;

        ssl on;

        # Set the buffer size to 1400 bytes (that way it fits into a single MTU$
        ssl_buffer_size 1400;
#       add_header Strict-Transport-Security max-age=63072000;

        gzip on;
        gzip_min_length 1280;
        gzip_buffers    16 8k;
        gzip_comp_level 4;
        gzip_http_version 1.0;
        gzip_types    text/plain text/html text/css application/javascript appl$
        gzip_vary on;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ /index.php$is_args$args;
        }

        # pass the PHP scripts to FastCGI server listening on /var/run/php5-fpm$
        location ~ \.php$ {
                try_files $uri /index.php =404;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_nam$
                include fastcgi_params;
        }

        location ~* \.(css|js|gif|jpe?g|png|woff2?)$ {
                #gzip on;
                #gzip_vary on;
                expires 168h;
                add_header Pragma public;
                add_header Cache-Control "public, must-revalidate, proxy-revali$
                add_header Vary "Accept-Encoding";
        }

        #include /etc/nginx/global/*;
}

Any help would be greatly appreciated! Thank you.
Answer: First of all, remove ssl on;
Second, you had Strict-Transport-Security header and browsers remembered that for 2 years (as that commented out header says). Add it back with max-age=0 to remove the effect.
If you can't do that (or want immediate effect) clear HSTS from your browser like this article says.


Calcular a media de uma matriz

Question: Estava tentando calcular a media de uma matriz sem utilizar o numpy, mas n consegui chegar ao algoritimo  desejado
def mediah(matriz:List):
    soma = 0
    for linha in matriz:
        for aij in linha:
                soma += aij
    return soma/len(matriz)

Esse foi o codigo ao qual consegui chegar, mas o tipo de operação na linha 5 não é suportado.
Answer: O problema é que len(matriz) retorna o número de linhas da matriz, não a quantidade de elementos. Já que está percorrendo a matriz, você pode somar em uma variável o tamanho de cada linha para obter a quantidade total de valores.
def media(matriz):
  soma = 0
  tamanho = 0

  for linha in matriz:
    soma += sum(linha)
    tamanho += len(linha)

  return soma / tamanho

Calcular a media de uma matriz


How do you express the key of a song in tablature?

Question: I can play open chords, barre chords on the guitar. Also I can find the key of a song, and from that can find the chords. But when it comes to tabbing the song, I get stuck.
If a song is say in C major, and I want to tab it, then do I need to start with the C note and then find the other notes relative to it?
Answer: The way tablature works is actually more simple than that. You don't need to even think about the key. Just directly transcribe the note you play to a note on tab. eg if you play 5th fret on the 2nd string, just transcribe that.
The only question you need to ask yourself is where on the neck you wish to play a particular note, and this will usually depend on how you want to tie the previous and following notes together - for speed you'll place them in a handy place to move your fingers to.
In your example, if the song is in C major, knowing how C major works on a guitar neck will give you a good insight as to what chord shapes will crop up, but you don't need that knowledge to just copy your finger position to a number on the tab.


MySQL cursor is fetching the only first record multiple times

Question: My Stored Procedure is like this:
CREATE PROCEDURE `kiosk_pro`.`setTimeAdjustForNext`(tokenno int,cdate date)
    /*LANGUAGE SQL
    | [NOT] DETERMINISTIC
    | { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
    | SQL SECURITY { DEFINER | INVOKER }
    | COMMENT 'string'*/
    BEGIN

        declare lo_SNO int;

        declare lo_TokenNo int;

        DECLARE done INT DEFAULT FALSE ;

       Declare mycur Cursor for( select  SNO  from kiosk_token_generation  where TokenDate=cdate and  TokenNo >= tokenno order by TokenNo );
              DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = TRUE;
       open mycur;

        time_loop :LOOP

              FETCH mycur into lo_SNO;

             if done then
                 close mycur;
                  leave time_loop;
             end if;

            select lo_SNO ;

           end loop time_loop;

    END$$

Actually my table having 3 records 1,2,3 in SNO column and 1,2,3 in
TokenNo column, i'm calling above stored procedure by passing date
='2014-02-22' and tokenno=1, but it's returning 1 value three times.
Where did I make a mistake?
Answer: Your SP work fine
Maybe you have another SP in a different DB
create table kiosk_token_generation (SNO  int, TokenNo int, TokenDate datetime);
INSERT INTO kiosk_token_generation (SNO, TokenNo, TokenDate) VALUES (1,1,'2014-02-22');
INSERT INTO kiosk_token_generation (SNO, TokenNo, TokenDate) VALUES (2,2,'2014-02-22');
INSERT INTO kiosk_token_generation (SNO, TokenNo, TokenDate) VALUES (3,3,'2014-02-22');

DELIMITER $$
CREATE PROCEDURE setTimeAdjustForNext(tokenno int,cdate date)
    BEGIN
       declare lo_SNO int;
       declare lo_TokenNo int;
       DECLARE done INT DEFAULT FALSE;
       Declare mycur Cursor for( select  SNO  from kiosk_token_generation  where TokenDate=cdate and  TokenNo >= tokenno order by TokenNo );
       DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = TRUE;
       open mycur;
       time_loop :LOOP
              FETCH mycur into lo_SNO;
             if done then
                 close mycur;
                 leave time_loop;
             end if;
            select lo_SNO ;
           end loop time_loop;
    END$$
DELIMITER ;

select  SNO  from kiosk_token_generation  where TokenDate='2014-02-22' and  TokenNo >= 1 order by TokenNo

CALL setTimeAdjustForNext (1, '2014-02-22');

Comment: instead of SNO in cursor if we take TokenNo then its not working properly


How to get update status of multiple Drupal sites

Question: I have several servers each with numerous Drupal sites on.
Not all of them are developed by me.
How would you keep on top of the sites to make sure they are all up to date security wise.
In an ideal world I would like them to report to a central location. From that one place I can be alerted.
Worse case I will write my own module to call a Web Service and send the status across. How ever if this has all ready been done will save some time.
Answer: A search on drupal.org found Drupal Remote Dashboard
If that module doesn't work for you, I would suggest a thorough search of drupal.org before jumping into creating your own module.
Answer: You may want to look into Aegir for this:

Ægir is a mature set of contributed modules for Drupal that aims to
  solve the problem of managing a large number of Drupal sites. It does
  this by providing you with a simple Drupal based hosting front end for
  your entire network of sites. To deploy a new site you simply have to
  create a new Site node. To backup or upgrade sites, you simply manage
  your site nodes as you would any other node.


Kerberos authentication to root domain in Active Directory

Question: I have an environment in Active Directory that is composed of a root and a child domain, let's call them my.root.domain.com and root.domain.com
I have a RHEL7 server I've spun up which has no problem at all authenticating against the "my.root.domain.com" domain. I cannot, however, authenticate using an account in the "root.domain.com" domain. I can look up accounts using id in both domains:
id -a firstname.lastname@example.com

and
id -a firstname.lastname@example.com

both show my AD groups. As the root user, I can even "su - " to any user in either domain (doesn't require authentication). The problem arises when authentication happens. Trying to authenticate to email@example.com fails.
in smb.conf I have:
...
workgroup = MY
realm = MY.ROOT.DOMAIN.COM
security = ads
...

in /etc/krb5.conf I have:
[libdefaults]
dns_lookup_realm = true
ticket_lifetime = 24h
renew_lifetime = 7d
forwardable = true
proxiable = true
rdns = false
default_ccache_name = KEYRING:persistent:%{uid}
default_realm = MY.ROOT.DOMAIN.COM
dns_lookup_kdc = true
[realms]
MY.ROOT.DOMAIN.COM = {
}
ROOT.DOMAIN.COM = {
}

[domain_realm]
my.root.domain.com = MY.ROOT.DOMAIN.COM
.my.root.domain.com = MY.ROOT.DOMAIN.COM
root.domain.com = ROOT.DOMAIN.COM
.root.domain.com = ROOT.DOMAIN.COM

A packet trace does reveal that when trying to authenticate as firstname.lastname@example.com, it sends a KRB query of:
CNameString: ROOT.DOMAIN.COMuser2
realm: MY.ROOT.DOMIAN.COM

I know I'm the village idiot when it comes to the AD stuff, but hoping someone has a good suggestion. I'm fairly certain my krb config is not quite what it should be.
Does anyone have any suggestions to utilize accounts from the domain "root.domain.com" domain to authenticate against?
Answer: Under [realms] section in /etc/krb5.conf you must declare at least one KDC for every realm:
[realms]
MY.ROOT.DOMAIN.COM = {
kdc = <FQDN_KDC_MY.ROOT.DOMAIN.COM>
}

ROOT.DOMAIN.COM = {
kdc = <FQDN_KDC.ROOT.DOMAIN.COM>
}


Unable to Write to C: (system drive) from a network resource on Windows 8.1

Question: I have a problem with copying from a network drive to the root of the C drive or to "program files(x64)" or to directly under "\Users" directly from the network. Here's the error I'm getting:
"X:\ is unavailable. If the location is on this PC, make sure the device or drive is connected or the disc is inserted, and then try again. If the location is on a network, make sure you're connected to the network or Internet, and then try again. If the location still can't be found, it might have been moved or deleted."
I can however copy the network file to a temporary location (such as desktop), another folder I created directly under C drive etc. It is also not a network connectivity issue.
This happens on  fully patched Win 8.1 machines on a windows domain network with a domain admin account (and also with a local admin account). Shares are also Windows 2012 Servers, not a third party NAS etc.
Another important thing is, it's fine if you are copying from any local resource to the root of C or to other problematic locations. So this is not a basic permission issue I assume.
Looking to see what is causing this and whether I can change this behavior. You can see the effective permissions on one of the problematic folders below. As you can see I should have full access to this folder.
Comment: Is it a UAC issue?
Comment: No, UAC is disabled. (set to never modify)
Comment: How did you disable UAC?  There's about 10 registry keys that control UAC; only one ties to the slider in the Users control panel. For a quick test, use "Run as Administrator" on Windows Explorer from the source PC and try the copy again. I doubt this, though, as I copy from Win8 to Win2012R2 all the time without trouble (and our GPOs force UAC on, so Win8 tiles don't break). Are you accessing the same share for each copy? Any chance you have one share set to "Full Control" and another to "Read"?
Comment: I told you that I set it to "never modify" specifically. I'm not going to change the registry settings.
Comment: I tried "run as admin"  running explorer.exe from CMD. Did not help.

I can copy from Win 8 to Win 2012 (into C drive just fine), have you tried the opposite?

I have tried with several different shares, even on several different servers, all has the same behavior.

I do have full control over the shares I'm copying from, but even a read permission is enough to copy FROM a share. The important part is the destination (C:). But as you can see on the screenshot, I have all the permissions.
Comment: Better question is, "Why did you disable UAC?" It's almost always a really bad idea, and there's generally a better way to get the permissions you need.
Comment: Also: you do know that standard users have not had write access to the root of the C: drive or to the program files folders since Windows 2000, right?  It's poor practice to write to those locations yourself.  The restriction includes the program files folder for your own program, and it also applies to administrator accounts in Vista and later that are not specifically elevated (perhaps causing you to disabled UAC), but you can get around this by setting permissions on _just the specific folders_ that you need to allow standard users to have write access.
Comment: Short version: don't write things to the C:\ drive. **Just don't do it.**
Answer: The error message is pretty clear:

X:\ is unavailable.

Whatever user is running this process is not able to connect to the network drive. It hasn't even started to look at C:\ drive yet (and BTW, writing to the root of the C:\ drive is a really bad idea.)
If this is a drive mapped for your user account, and you elevate to run as administrator, mapped drives won't work. You'll need to access the share via it's UNC (<code>\\server\share</code>) path.
Answer: It sounds like it is a permissions issue. Windows 8.1 protected the root of the C drive pretty heavily.
You should be able to get around the protection by setting Everyone to have full control of the C drive and making sure the C drive is shared. When you share the C drive, make sure that under the Share Permissions; Everyone has full control.
That should bypass the built in security for the C drive.
Have you tried doing a Google search on the error message?
Comment: I don't see why this should be a permission issue as that user has full control and still cant write to it.

Also, sharing C: and giving everyone permissions is not an option for security reasons. And I don't think it will solve it, as that permission is share level but I'm still locked by file system level issues.
Comment: Is the network user also a user account on the machine? If not, then maybe you can try adding them to the local users.
Comment: Is there any particular reason that you need to copy directly to the root of the C drive from the network?
Comment: Yes, network user is also on local admin accounts. There is no specific reason to write directly to C but some users need to do direct changes to folders under Program Files ie.
Comment: For a quick test, you can try making a particular folder under the Program Files directory shared with Everyone set to Full Control. Then see if you can copy files from the network to that directory?
Comment: I forgot to mention. If you create any folder under C:, you can copy directly to that folder. So creating a folder even without sharing works fine. But again, people want to copy directly into Program Files, which does not work.


Is it viable to run your own private Kubernetes-Cluster?

Question: I am currently doing some research for my company concerning Kubernetes. We want to evaluate hosting our own Kubernetes-cluster from our bare-metal servers inside our private computing centre.
I only ever worked with managed Kubernetes (like Google Cloud etc.) and of course minikube for local testing.
Has anyone ever worked with a self-hosted and self-installed Kubernetes-cluster and give me some evaluation on Know-How and time needed to configure and administrate such cluster?
Comment: You would need to provide more details. How big of a cluster. What is to be intended to run there?
Comment: Hi, the idea was to first create a small cluster (about 3 or 4 nodes) and then provide it to our developers. I can't define what exactly is meant to run there because we have several projects that could be deployed to Kubernetes. Later, we maybe want to create more clusters for more projects and now I'm wondering whether administrating such systems (as I am the only admin) is too much work. Especially considering my not so present knowledge about Kubernetes inner-workings.
Answer:
Has anyone ever worked with a self-hosted and self-installed Kubernetes-cluster and give me some evaluation on Know-How and time needed to configure and administrate such cluster?

I have run some non-trivial number of clusters before GKE and EKS were a thing, although I'm thankful I was doing so in a IaaS setup, so I didn't have to rack things and if something went toes up, I just asked the cloud provider to kill it. There are two separate parts to your question, and they are distinct amounts of work: configuring, and administering
Configuring a cluster could happen in as little as 30 minutes after you have the machines in a shape that they will boot up and read their user-data (I presume even bare metal has a corresponding cloud-init scheme, even if less emphasis on the "cloud" part), thanks to the utterly magic kubeadm and its friend etcdadm
However, after kubernetes is up-and-running, that's when the real work starts -- often characterized as "day two" operations, and it's a thick book of things that need monitoring and things that can go toes up.
For absolute clarity, I don't mean to dissuade you: when the cluster(s) (is|are) in good shape, it's like magic and a startling number of things Just Work™. But, like many things magical, when they get angry, if you aren't already familiar with the warning signs or recognize the sound of their gunfire, it can make a frustrating situation even more frustrating.

I'm wondering whether administrating such systems (as I am the only admin) is too much work. Especially considering my not so present knowledge about Kubernetes inner-workings

It's that last part that will be the killer hurdle to overcome, IMHO, since -- like many pieces of software -- once you understand how they're glued together, troubleshooting them is generally a tedious but tractable problem. However, managing kubernetes itself is only one part of the toolset required to keep a kubernetes cluster alive:

systemd (yup, still required unless you go with one of the really, really esoteric machine images that boot directly into kubelet and containerd)

how systemd's cgroups management and docker or containerd's cgroups management fight

docker (or containerd)

including its auth system, if you have images that require credentials to pull

CNI

and every nuance of the CNI provider you decide to go with, because inevitably they are going to puke and putting out that fire is some ... good fun

etcd(!!!!!!!!!!!!!!!!!!)

membership management
backups -- and they're only "backups" if they're tested, so some disaster recovery drills go a long way toward lowering the number of 3am pager calls
while it might not affect you if you have a small enough cluster, triaging etcd performance stalls is also a nightmare

the roles played by the parts of the control plane, in order to know who's logs to go spelunking around in based on the observed behavior:

apiserver
controller-manager
scheduler
kube-proxy
kubelet

intimate knowledge of the major moving parts of all, and I do mean all, of the vanilla resource types: Node, Pod, Deployment, Service, ConfigMap, Secret (including the 4 major sub-types of Secret); StatefulSets are optional, but handy to understand why they exist
the RBAC subsystem: Role, RoleBindings, ClusterRole, ClusterRoleBindings, and how auth makes it from an HTTPS request down into the apiserver's handler to get translated into a Subject that can be evaluated against those policies, which like all good things related to "security" is its own bottomless well of standards to know and tools to troubleshoot

and while it might not affect you with a bare-metal setup, usually kubernetes interacts with the outside world via something like MetalLB and the CNI IPAM solution, and troubleshooting those requires knowing what kubernetes expects them to do, and then reconcile that with what they are actually doing

I personally have not yet taken the CKA but it may behoove you to at least go through the curriculum to get a sense for what kinds of topics the CNCF considers essential knowledge. If you're not spooked, then, hey, maybe you can get your CKA out of this exercise, too :-)
Good hunting!
Comment: A few more things to add: Logging, getting logs from pods is a PITA unless you have something shipping to a centralised log server, user authentication. If you are going to have multiple clusters, and (for example) a development cluster then your users need a way of authenticating. Secret storage - HC Vault?. Metrics: Prometheus & Graphana? Network Policy, Service mesh, How will you deploy things to your cluster? GitOps? Package managers? Helm... The list goes on. Some of these decisions are present whether you use onsite or cloud, but you get the idea, there's a lot.
Comment: BTW To answer your question, yes it is viable. Just harder than cloud IMO. You get the flexibility, but you need to implement pretty much everything yourself, and thats a lot of work.
Comment: Hi guys, first of all @mdaniel thank you very much for the detailed analysis :) Really appreciate it! I took all points into consideration and handed them to my supervisors and we decided against our own Kubernetes at the moment. However, as I'm having a personal interest in this topic I'll get into this quite a bit throughout the next months. So, once again thank you for you answers and for providing your own experiences!!!


Criação de um forum, duvida entre quais linguagens usar

Question: estou criando um fórum de discussão do zero, mas ja tenho boa parte do esqueleto web como: criação de um topico, responder e visualizar topico. Minha duvida é entre usar o php (não manjo nada ainda) ou jquery mais API, e o banco a ser utilizado será o mongodb. O que vocês com mais experiência aconselha?
Answer: Boa noite Mayla, como você já parece ter escolhido usar o MongoDB acho que seria interessante partir para a utilização de Express, NodeJS e AngularJS. Um framework do tipo JavaScript Fullstack.
Entre as vantagens:

Você trabalha somente com javascript;
Tem ampla documentação e componentes na internet;
jQuery é uma ótima biblioteca, mas adorei trabalhar com o AngularJS, o código fica menor e é mais fácil de manter e evoluir, se integra bem com bootstrap, como não sou um designer de mão cheia isso é um diferencial importante pra mim;
Roda em qualquer plataforma: Windows, Linux ou iOS;

Espero ter ajudado.
Comment: Complementando a resposta: para mais detalhes acesse http://mean.io/#!/


Given parabola $y^2=4x$, find locus of mid points of chords that are of length $2l$

Question: It's easy to find the equation of the chord using $$T=S_1$$
$$2ax-ky +2ah-4h+k^2=0$$ where (h,k) are midpoints of the chord
I identified to possible ways to solve this. Either solve this equation and the equation of the parabola, obtain the points of intersection in terms of h and k, find the distance between the two points and equate it to 2l
Or
Use the coordinates $(t_1)$ and $(t_2)$ on the parabola, which would given the midpoints, and then finding the distance.....
I tried both of them, but they are so awfully long, I am convinced there is proper, much shorter method to solve this. How should I solve it correctly?
The answer is $(4x-y^2)(y^2+4)=4l^2$
Answer: Note that $a =1$. So, the chord equation reduces to,
$$2x-ky +k^2-2h=0$$
The two intersection points with the parabola can then be found by substituting $y^2=4x$ into the equation,
$$y^2 -2ky+2k^2-4h=0$$
which leads to
$$y_1+y_2 = 2k, \>\>\>\>\>y_1y_2 = 2k^2-4h$$
Evaluate,
$$(y_2-y_1)^2 = (y_2+y_1)^2 -4y_1y_2 = 4(4h-k^2)$$
$$(x_2-x_1)^2 = (\frac1{4}y_2^2-\frac1{4}y_1^2)^2= \frac1{16}(y_1+y_2)^2(y_1-y_2)^2
= k^2(4h-k^2)$$
Then, matching the length of the chord $(x_2-x_1)^2 +(y_2-y_1)^2 = 4l^2$ to get the locus,
$$ k^2(4h-k^2) + 4(4h-k^2) = 4l^2$$
or, in the form,
$$(4h-k^2)(k^2+4)=4l^2$$


When generating a corpus for latent Dirichlet allocation, does word frequency within documents matter?

Question: I have my set of documents and have extracted the unique words from them, including a count of the number of times each word appears in the document.  But it would seem from the documentation on the Python library I'm using, that word count within documents is immaterial, and that co-occurrence is the main thing.
Am I correct in that understanding?
Answer: Note that LDA has the assumption that each topic, or a language model, is a multinomial distribution over all words. For a document, multiple occurrence of the same word is a draw from this multinomial distribution. So, in theory, word counts matter.
However, in terms of inference, if you use Gibbs sampling, you probably need to re-sample topic assignments for multiple occurrence. But if you use variational inference, you just do it the same across multiple occurrence.