slapd : attribute type undefined

Question: I'm trying to put a config of slapd on the new server.
I did it the same way, but it was probably two years ago and in slightly different environnement (I'm on the Debian actually and it was on CentOS before).
I have a config file in ldif format and I try to do :
slapadd -n 0 -F /etc/ldap/slapd.d -l /root/server_slapd_config.ldif

And I get

5c75466b <= str2entry: str2ad(olcDbCacheSize): attribute type
  undefined
slapadd: could not parse entry (line=2087)

The strange thing that I could not figure out why I get this error, as olcDbCacheSize is defined in my server_slapd_config.ldif
Here the lines in my server_slapd_config.ldif :
1)
olcObjectClasses: ( OLcfgDbOc:1.1 NAME 'olcBdbConfig' DESC 'BDB backend conf
 iguration' SUP olcDatabaseConfig STRUCTURAL MUST olcDbDirectory MAY ( olcDb
 CacheSize $ olcDbCheckpoint $ olcDbConfig $ olcDbCryptFile $ olcDbCryptKey
 $ olcDbNoSync $ olcDbDirtyRead $ olcDbIDLcacheSize $ olcDbIndex $ olcDbLine
 arIndex $ olcDbLockDetect $ olcDbMode $ olcDbSearchStack $ olcDbShmKey $ ol
 cDbCacheFree $ olcDbDNcacheSize $ olcDbPageSize ) )

2)
olcAttributeTypes: ( OLcfgDbAt:1.1 NAME 'olcDbCacheSize' DESC 'Entry cache s
 ize in entries' SYNTAX OMsInteger SINGLE-VALUE )

3)
dn: olcDatabase={1}bdb,cn=config
objectClass: olcDatabaseConfig
objectClass: olcBdbConfig
olcDatabase: {1}bdb
[skip]
olcDbCacheSize: 1000

So, DbCacheSize declared in olcBdbConfig attributes.
There is olcAttributeTypes for DbCacheSize
And dn: olcDatabase={1}bdb,cn=config is "objectClass: olcBdbConfig"
Why I get "attribute type undefined"?
Some additional info.
System : Debian 9 (Stretch)
# dpkg -l | grep -i ldap
ii  ldap-utils                    2.4.44+dfsg-5+deb9u2           amd64        OpenLDAP utilities
ii  ldapscripts                   2.0.7-2                        all          Add and remove users and groups (stored in a LDAP directory)
ii  libldap-2.4-2:amd64           2.4.44+dfsg-5+deb9u2           amd64        OpenLDAP libraries
ii  libldap-common                2.4.44+dfsg-5+deb9u2           all          OpenLDAP common files for libraries
ii  slapd                         2.4.44+dfsg-5+deb9u2           amd64        OpenLDAP server (slapd)

Thanks for your help.
Update:
A small update.
Just to check. If I remove olcDbCacheSize string, I get the error for the next line : olcDbCheckpoint

5c7553fc <= str2entry: str2ad(olcDbCheckpoint): attribute type
  undefined

Update 1:
Here the lines started with olcDb*.
All the lines are in dn: olcDatabase={1}bdb,cn=config section.
I skipped some lines in the middle (they are not started with olcDb).
dn: olcDatabase={1}bdb,cn=config
objectClass: olcDatabaseConfig
objectClass: olcBdbConfig
olcDatabase: {1}bdb
[skip]
olcDbDirectory: /var/lib/slapd
olcDbCacheSize: 1000
olcDbCheckpoint: 512 15
olcDbConfig: {0}# This configuration file sets BerkeleyDB options when using
olcDbConfig: {1}# backend, for the database held in the directory where this
olcDbConfig: {2}#
olcDbConfig: {3}# Set directory to use for transaction logs:
olcDbConfig: {4}#set_lg_dir /var/lib/ldap/logs
olcDbConfig: {5}# Set in-memory transaction log cache (2MB)
olcDbConfig: {6}set_lg_bsize 2097152
olcDbConfig: {7}# Set max transaction log file size, must be >=4* lg_bsize
olcDbConfig: {8}#set_lg_max              10485760
olcDbConfig: {9}set_lg_max              20971520
olcDbConfig: {10}# Set in-memory database cache
olcDbConfig: {11}#set_cachesize   0       1048576        0
olcDbConfig: {12}
olcDbConfig: {13}
olcDbConfig: {14}# For batch imports, disabling transaction logging totally
olcDbConfig: {15}# improve performance:
olcDbConfig: {16}# set_flags DB_TXN_NOT_DURABLE
olcDbConfig: {17}
olcDbConfig: {18}
olcDbNoSync: FALSE
olcDbDirtyRead: FALSE
olcDbIDLcacheSize: 0
olcDbIndex: objectClass eq
olcDbIndex: entryUUID eq
olcDbIndex: entryCSN eq
olcDbIndex: cn eq,subinitial
olcDbIndex: uid eq,subinitial
olcDbIndex: uidNumber eq
olcDbIndex: gidNumber eq
olcDbIndex: sn eq,subinitial
olcDbIndex: givenName eq,subinitial
olcDbIndex: member eq
olcDbIndex: mail eq,subinitial
olcDbIndex: FinLogin eq,approx,sub
olcDbIndex: uniqueMember eq
olcDbIndex: memberUid eq
olcDbLinearIndex: FALSE
olcDbMode: 0600
olcDbSearchStack: 16
olcDbShmKey: 0
olcDbCacheFree: 1
olcDbDNcacheSize: 0

Comment: Those are related to BDB and HDB Database Directives. Can you show your configuration file? that is ```/root/server_slapd_config.ldif```
I am specifically interested in all values that starts with: ```olcDb*```
Comment: Thank you for reply. I put "Update 1" in the question.
Comment: I usually never edit manually those configs the way you purpose.
I would suggest to clean up the config completely and then build everything from the slapd.conf file as an example. You eiter not importing some of the schemas or not referencing database properly in the config. Check for the configuration I referenced in one of my previous answer, specifically look for moduleload, include(schema) and the way I remove config folder and recreate automatically based on slapd.conf
https://serverfault.com/questions/941612/nslcd-with-domain-com-and-sub-domain-com/947647#947647
Comment: I agreed with you in theory. Your approach is certainly the right one. But for my case it means  also a lot of work.

But, you give me some idea and it seems that it's work. I'm not sure for 100%, but it seems that in moduleload section (dn: cn=module{0},cn=config, objectClass: olcModuleList...) there was not back_bdb.la module. I add it and it starts to work.

Now I have the new problems... may be finally I will follow your advice to recreate slapd.conf... or I will post the new question. :-)

In any case, thank you very much for help.
Comment: Zkharov, could you possibly hit a thumbs up button on the post I mentioned above in case it was helpful to you?
Answer: Some schema descriptions are hard-coded in backend modules or overlays.
That's e.g. the case for attribute type olcDbCacheSize being defined in servers/slapd/back-bdb/config.c and your local schema definition has no effect.
I suspect that the back-bdb backend is not loaded and therefore those schema definitions are not available in your slapd instance.

slapd : attribute type undefined


cakephp change url to new url

Question: I have the CakePHP program ( version 2.1.3) what is running already ( on Centos 6.x and Apache ).
Now I want to change url from: http://domain.com/frontend/login
to new url like: http://domain.com/user-login.html
I changed routes.php like below:
Router::connect(
    '/:slug.html',
    array('controller' => 'frontend', 'action' => 'login'),
    array(
        'pass' => array('slug')
    )
);

and added the following code in my login.ctp :
<?php
 echo $html->link('user login',
        array(
            'controller' => 'frontend',
            'action' => 'login',
            'slug' => Inflector::slug('user login')));
?>

It success and now I can access with new URL: http://domain.com/user-login.html
But I also still can access with old url: http://domain.com/frontend/login
I only want to access new URL, and remove old URL.
How I can do? please guide me the details.
Thank.
Comment: frontend is controller and login is function, If you write controller and action both in url then it is accessible, if user open this then you need to redirect user to second url or you need to write code .httaccess file
Comment: I dont know how to change .htaccess file. Can you tell me how to config .htaccess file?
Answer: ptica's answer is a good one, but if you want something that doesn't disable all of CakePHP's default routes then I'd add redirect rules to the .htaccess file:-
Redirect 301 /frontend/login /user-login.html

.htaccess is a better place to put your redirects than in the routes file as it will be more efficient. It would probably be a good idea to put a comment about this in the routes file for future reference though; otherwise you or someone else will look at the project one day and be confused at what is redirecting!
There shouldn't really be an issue with leaving the route as is though as long as you:-

always use Cake's Router/HtmlHelper to generate links so that the correct alias is used
include a canonical url on the page so that search engines use the correct url

That way the app should never reference /frontend/login.
Comment: I tried to setup like your code in .htaccess file ( in root folder, the same with app folder )
I can not open link:  http://domain.com/user-login.html
My expectation: Open link : http://domain.com/user-login.html
remove old link: http://domain.com/frontend/login

I tried to config:
RewriteRule  ^user-login.html  /frontend/login/
But it still not success
Comment: @haicnpmk44 you need to open a new StackOverflow question for this. Make sure you show your .htaccess code in the question. There are lots of .htaccess experts on this site. :-)
Answer: at the bottom of your routes.php file there is this statement:

/**
 * Load the CakePHP default routes. Remove this if you do not want to use
 * the built-in default routes.
 */
    require CAKE . 'Config' . DS . 'routes.php';

just comment out the require line and you should be all set

cakephp change url to new url


Parsing raw TCP data line by line in Python

Question: I'm getting stock market data via TCP. I'm trying to connect with Python to the broker, e.g. via Telnet (telnetlib) with read_eager() or via socket with socket.recv().
This is how the output looks like:
DE000XX6XP73;0.0000;2.44;G;2020-04-24;18:22:04.200'
';\r\nDE000XX9JX82;0.0000;0.0;G;2020-04-24;18:22:06.2'
'18;\r\nDE000XX9XXW3;0.0000;127.17;G;2020-04-24;18:22'
':21.954;\r\nDE000XX68XV1;0.0000;3.22;G;2020-04-24;18'
':21:11.517;\r\nDE000XX3H0X0;0.0000;1.81;G;2020-04-24'

and so on.
As you can see, this is one line:
DE000XX6XP73;0.0000;2.44;G;2020-04-24;18:22:04.200;
How can I parse each line? The TCP data stream never ends - it has no end of file and I need to process it in "real-time". The problem is that I'm getting "broken lines" most of the time, e.g.  DE000XX3H0X0;0.0000;1.81;G;2020-04-24
Comment: May help you:  https://stackoverflow.com/questions/42987650/process-socket-data-that-ends-with-a-line-break
Answer: I managed to find an own solution in Python which wasn't working as I expected. (Sync) Python was way too slow to handle those requests in real-time, it somehow stopped working after some time. I switched to async JS which is now working flawlessly.


Como criar um executável a partir de python

Question: Eu estou fazendo um serviço onde tenho que fazer um programa que faça leitura e criação de arquivos com dados numéricos. O problema que tenho é que os computadores em que o programa será utilizado não são acessíveis a mim.
Por causa disso, eu precisava converter meu arquivo .py para .exe
O programa está pronto em Python 3.5 e parece que o único programa que pode ajudar a fazer a conversão é o CX_FREEZE. Eu usei ele e formei um arquivo...
Tenho 2 problemas:
O arquivo não funciona em computadores que sejam windows 7 (dependendo da versão) , e que não tenham algumas dlls (não são sempre as mesmas)
O Executável está vindo acompanhado de VARIAS pastas com MUITOS arquivos. Isso não me parece viável, queria alguma maneira de "empacotar" os arquivos dentro do meu executável.
O programa não é tão grande, e não usa tantas bibliotecas, Somente OS e DATETIME.
Já tentei utilizar o Pyinstaller e INNO setup, mas nenhum me dá alguma luz.
Gostaria de saber se alguem tem alguma alternativa ou dica que eu possa usar.
import sys
from cx_Freeze import setup, Executable

build_exe_options = {"packages": ["os", "datetime"], "excludes": []}

base = None
if sys.platform == "win32":
    base = "Console"  # para execuções em terminal

setup(name="GetSpecJoin",
      version="0.1",
      description="My GUI application!",
      options={"build_exe": build_exe_options},
      executables=[Executable("240117.py", base=base)])

Comment: É que essas ferramentas de empacotamento simplesmente pegam as DLLs em uso *no seu sistema*. Por isso que não funciona no sistema de destino, se ele for diferente. Pra resolver, só se vc tiver um sistema igual (isto é, na mesma versão do Windows) e empacotar lá também. Enquanto isso, considere também usar o Cython: http://cython.org/ pra converter o seu código Python pra C e depois compilar o código C em um executável. Boa sorte!
Comment: Os arquivos adicionais são as dependências (do Python e de bibliotecas usadas, incluindo talvez o próprio cx_freeze). O envio de múltiplos arquivos não devia ser um impedimento pra vc. Existe [essa pergunta](http://stackoverflow.com/q/112698/2896619) no SOen com respostas que podem te ser úteis. Mas eu não testei, não sei se funciona o que dizem ali. De qualquer forma, se vc realmente quer ter um arquivo só e não quer solicitar a instalação do Python no cliente, talvez seja melhor simplesmente usar outra linguagem (C, por exemplo).
Comment: Eu entendo que essa seria a alternativa mais garantida. Se eu não conseguir resposta em alguns dias terei que fazer isso. O fato é que o cliente não faz questão que o python não seja instalado, ele até aceitou, mas eu acho estranho estar sendo tão complicado de fazer algo tão simples como compilar o arquivo para distribuição.
A ideia de fazer um .exe singular, é que poderá ser pelo menos um pouco portátil.
Comment: Não é que seja complicado. Vc é que não quer enviar as DLLs de dependências junto. E se não rodou no Windows 7 pode ser porque lá faltem algumas DLLs que a sua versão do SO já tem (é comum ocorrer com os [Redistribuíveis do Visual Studio 2015](https://www.microsoft.com/pt-br/download/details.aspx?id=48145), por exemplo, de que algumas bibliotecas do Python dependem). Seria só o caso de instalar, se for essas que estão faltando. De qualquer forma, certamente seria mais fácil ter só um exe, mas aí o mais descomplicado mesmo seria usar uma linguagem apropriada.
Comment: Eu tentei inserir as dll manualmente, mas parece que os arquivos que estão sendo exigidos (api-win-msi-crt-[...].dll) não funcionam na versão do windows 7  instalada (sem sp1) em outras versões com Sp2 o programa funcionou. Ao tentar atualizar o sistema para sp1, o computador fica carregando por horas e não apresenta progresso. até as hotfix da microsoft que deviam corrigir o erro não funcionaram. O problema maior esta sendo as máquinas do cliente com sistema desatualizado, O sistema está "travado" no windows 7 sem nem mesmo sp1.
Comment: Muito Obrigado! Vou verificar. uma ultima tentativa. :)
Comment: Não sou profissional, mas peço licença para contar o que fiz quando brinquei de criar .exe de programas python. Baixei os arquivos de instalação do Python, se não me engano uma versão minima e coloquei junto com os arquivos do meu programa, empacotei tudo usando o INNO e configurando-o para instalar o Python também, funcionou, pena que só os arquivos do python somam perto dos 100Mb, mas compactados pelo INNO tudo caiu pra mais ou menos 30Mb.
Comment: Me lembrei agora de uma outra forma que usei; Baixei os arquivos do Python, não o instalador mas sim os arquivos e pastas mesmo e deixei junto com meu programa. No INNO configurei para adicionar uma entrada no registro do Windows, para que os arquivos .py(na verdade mudei para uma extensão que não existe, a fim de evitar conflitos) sejam abertos com o executável do Python(pythonw.exe) que está na pasta do python que acompanha meu programa. É meio confuso, mas funcionou aqui.
Comment: O que deu errado com o Pyinstaller? Eu uso python 3.5 e meu pyinstaller funciona perfeitamente. Inclusive, consegue-se criar um arquivo .exe único com todas as dlls inseridas nele com o Pyinstaller, ao invés de uma pasta cheia de arquivos.
Comment: O pyinstaller simplesmente não executa, ele nem abre. eu segui uns 2 tutoriais e deu na mesma
Comment: Tente a biblioteca py2exe 0.9.2.2. Link para download: https://pypi.python.org/pypi/py2exe/
Comment: Basta você compilar o que o cx_Freeze gera com o IExpress. De uma olhada em minha resposta. Abs
Answer: Voce pode usar o cx_Freeze.

pip install cx_Freeze
Criar um arquivo chamado setup.py no mesmo diretorio do seu arquivo
(exemplo teste.py)
Dentro do arquivo setup.py você joga o código que vou deixar no
final da resposta
Execute o comando python setup.py build
Dentro da pasta build vai ter seu executável.

setup.py
from cx_Freeze import setup, Executable
import sys

base = None

if sys.platform == 'win32':
    base = None

executables = [Executable("teste.py", base=base)]

packages = ["idna"]
options = {
    'build_exe': {

        'packages':packages,
    },

}

setup(
    name = "Nome Executavel",
    options = options,
    version = "1.0",
    description = 'Descricao do seu arquivo',
    executables = executables
)

Sobre o seu problema com diversos arquivos além da executavel
O cx_Freeze não compila apenas o .exe, porém na própria documentação dele, é indicado o uso do IExpress, para você compactar todo o diretorio gerado pelo cx_Freeze em um unico .EXE

You can use IExpress to compress the build directory from cx_Freeze into a self-extracting archive: an exe which unpacks your application into a temporary directory and runs it. IExpress is a utility that's included with Windows, intended for making installers, but it works equally well if you tell it to run the cx_Freeze-built exe after extraction.

Fonte: http://cx-freeze.readthedocs.io/en/latest/faq.html
Comment: Creio que o problema dele é bem maior do que isto, na verdade falta o AP da pergunta informar qual erro exatamente ocorreu.
Comment: Ok, estou trabalhando em melhorar a resposta, e estudando a fundo tanto o cx_Freeze quanto pynsist e alguns outros. Já edito a resposta.
Comment: Eu encontrei outras formas de dar continuidade ao desenvolvimento sem precisar fazer exatamente isso, mas o conteúdo está muito bom, realizei alguns testes com outros programas que tenho e funcionou bem, acho que quando tentei eu estava fazendo algo errado.


Yii2: yii\web\UrlManager does'nt make user-friendly urls

Question: Here's urlManager configs:
return [
    'class'               => \yii\web\UrlManager::class,
    'enablePrettyUrl'     => true,
    'showScriptName'      => false,
    'baseUrl'             => '',
    'rules'               => [
        'page/<id:[\\w-_]+>' => 'page/index',
    ],
];

Here's urlManager using:
$menuItems = [
    [
        'label' => 'Home',
        'url' => Yii::$app->urlManager->createAbsoluteUrl(['/page']),
    ],
    [
        'label' => 'About',
        'url' => Yii::$app->urlManager->createAbsoluteUrl(['/page', 'id' => 'about']),
    ],
    [
        'label' => 'Contact',
        'url' => Yii::$app->urlManager->createAbsoluteUrl(['/page', 'id' => 'contact']),
    ],
];

And here's results from browser:
http://localhost/page?id=about
http://localhost/page?id=contact
What's wrong in my code?
Answer: You need to use exact route when creating URLs:
$menuItems = [
    [
        'label' => 'Home',
        'url' => Yii::$app->urlManager->createAbsoluteUrl(['/page']),
    ],
    [
        'label' => 'About',
        'url' => Yii::$app->urlManager->createAbsoluteUrl(['/page/index', 'id' => 'about']),
    ],
    [
        'label' => 'Contact',
        'url' => Yii::$app->urlManager->createAbsoluteUrl(['/page/index', 'id' => 'contact']),
    ],
];


Editing content redirects to apache test page?

Question: Everything in the site works except for when saving it redirects to an apache test page.  Any ajax request returns an error and the apache test page as debug text?  Any idea?  php 5.3 all requirements met.
Clarification
For example:
When editing content and clicking save I am taken to an apache test page.
The url display in the browser is site.com?q=node/1/edit&destination=admin/content
If I refresh the page and send the postdata again I get the apache start page.
If I load that page (like put the cursor in the URL bar and hit enter) it goes to the content edit screen but nothing is saved.
All forms that submit post data it seems do not work.  I dont have this problem anywhere else on the server.
Comment: Please give specific URLs. "The apache test page" could be reached by any number of URLs. Please give a specific recipe that creates this problem. Instead of saying "any ajax request returns an error and the apache test page as debug text" say *which* ajax request. Instead of saying "except for when saving" say "When saving page ..."
Comment: This is a very localized question and you have also given little-no information.  What error has been returned?
Comment: Nothing shows up in the apache log. No error from drupal.  It just redirects to the apache test page.  Not trying to use clean urls.  Fresh install of 7.7.
Comment: added a little bit more info.
Answer: This was an issue with mod_security.  Disabling it fixes the problem...but is not advised.


How to compare observed vs. expected events?

Question: Suppose I have one sample of frequencies of 4 possible events:
Event1 - 5
E2 - 1
E3 - 0
E4 - 12

and I have the expected probabilities of my events to occur:
p1 - 0.2
p2 - 0.1
p3 - 0.1
p4 - 0.6

With the sum of the observed frequencies of my four events (18) I can calculate the expected frequencies of the events right?
expectedE1 - 18 * 0.2 = 3.6
expectedE2 - 18 * 0.1 = 1.8
expectedE1 - 18 * 0.1 = 1.8
expectedE1 - 18 * 0.6 = 10.8

How can I compare observed values vs expected values? to test if my calculated probabilities are good predictors?
I thought of a chi-squared test, but the result change with the sample size (n=18), I mean, if I multiply observed values by 1342 and use the same method the result is different. Maybe a wilcox paired test works, but what do you suggest?
If can suggest in R, it would be better.
Answer: You mention that you get different results if you multiply all values by $1342$. This is not a problem. You should get very different results. If you flip a coin and it comes up heads, this doesn't say very much. If you flip a coin $1342$ times and you get heads every time, you have much more information suggesting that the coin is not fair.
Usually you want to use alternatives to a $\chi^2$ test when the expected number of occurrences is so low (say, under $5$) in a large percentage of your categories (say, at least $20\%$). One possibility is Fisher's exact test, which is implemented in R. You can view the $\chi^2$ test as an approximation to Fisher's exact test, and the approximation is only good when more of the expected counts are large.
Comment: Thank you, which one is better for this: just the fisher test? or the fisher test with p simulated value? and why?
Comment: Simulation introduces errors which might be small, but which shouldn't be necessary for small values. If you have $k$ categories and $n$ objects, then the number of possible outcomes is $n+k-1 \choose n$. When this is small by the standards of computers (perhaps less than $10^7$) then I would just use exact calculations. If the exact calculations are slow, test the errors of the simulations and see if they are acceptable for the speed increase.


p5: Using Sine wave in WEBGL to change y-axis

Question: I have some spheres that I want to move straight on the x axis with only their y-axis changing based on the sine wave (you know the up and down yo-yo pattern). It is pretty simple in 2d but for some reason, in WEBGL, the angle changes in the sin function is not working for this.y.

let particles = [];
let quantity = 20;

function setup() {
  createCanvas(300, 300, WEBGL);

  for (let i = 0; i < quantity; i++) {
    let particle = new Particle();
    particles.push(particle);
  }
}

function draw() {
  background(15);
  orbitControl();
  directionalLight([255], createVector(0, 0, -1));
  translate(-width / 2, 0);

  particles.forEach(particle => {
    particle.update();
    particle.edges();
    particle.show();
  });
};

// particle.js
function Particle() {
  this.angle = 0;
  this.r = 10;
  this.x = random(width);
  this.y = sin(this.angle) * 200; // I am changing this angle later but it's not working
  this.z = random(-50, 50);
  this.pos = createVector(this.x, 0, 0);
  this.vel = createVector(2, this.y, 0);

  this.update = function() {
    this.pos.add(this.vel);
    this.vel.limit(4);
    this.angle += 3;
    // print(this.pos);
  }

  this.edges = function() {
    if (this.pos.x >= width) {
      this.pos.x = 0;
    }
  }

  this.show = function() {
    noStroke();
    push();
    translate(this.pos.x, this.pos.y, 0);
    sphere(this.r);
    pop();
  }
}

Answer: You only call sin one time in the construtor. You probably meant to apply it to this.y on every frame based on this.angle, or some other incrementing value. Also, there are duplicate vectors; pick either an x/y pair or use a vector x/y, but not both.

let particles = [];
let quantity = 20;

function setup() {
  createCanvas(300, 300, WEBGL);

  for (let i = 0; i < quantity; i++) {
    let particle = new Particle();
    particles.push(particle);
  }
}

function draw() {
  background(15);
  orbitControl();
  directionalLight([255], createVector(0, 0, -1));
  translate(-width / 2, 0);

  particles.forEach(particle => {
    particle.update();
    particle.edges();
    particle.show();
  });
};

// particle.js
function Particle() {
  this.angle = 0;
  this.r = 10;
  this.x = random(width);
  this.y = 0;

  this.update = function() {
    this.angle += 0.05;
    this.y = sin(this.angle) * 100;
  }

  this.edges = function() {
    if (this.x >= width) {
      this.x = 0;
    }
  }

  this.show = function() {
    noStroke();
    push();
    translate(this.x, this.y, 0);
    sphere(this.r);
    pop();
  }
}


Poisson xgboost with exposure

Question: I was trying to model a count dependent variable with uneven exposure. Classical glms would use log(exposure) as offset, also gbm does, but xgboost does not allow for offset until now...
Trying to find a drawback this example in crossvalidated (Where does the offset go in Poisson/negative binomial regression?) suggested me to model frequency (real number) instead of counts weighting by Exposure.
I tried to work aroung some xgboost code to apply the same method on my data but I failed.... Below the code I set out:
library(MASS)
data(Insurance)
library(xgboost)
options(contrasts=c("contr.treatment","contr.treatment")) #fissa i

Insurance$freq<-with(Insurance, Claims/Holders )
library(caret)

temp<-dplyr::select(Insurance,District, Group, Age,freq)
temp2= dummyVars(freq ~ ., data = temp, fullRank = TRUE) %>% predict(temp)

xgbMatrix <- xgb.DMatrix(as.matrix(temp2),
                     label = Insurance$freq,
                     weight = Insurance$Holders)

bst = xgboost(data=xgbMatrix, label = Insurance$freq,    objective='count:poisson',nrounds=5)
#In xgb.get.DMatrix(data, label) : xgboost: label will be ignored.
#strange warning

Insurance$predFreq<-predict(bst, xgbMatrix)

with(Insurance, sum(Claims)) #3151
with(Insurance, sum(predFreq*Holders)) #7127 fails

Can anybody help? Also, I was wondering if It were possible to run all using caret's train...
Answer: According to the answer in:
https://stackoverflow.com/questions/34896004/xgboost-offset-exposure
xgboost can handle offset term as in glm or gbm using setinfo, but this method is not documented very well.
In your example, the code would be:

    setinfo(xgbMatrix,"base_margin",log(Insurance$Holders))

Answer: Your code works just fine, you just need to increase the parameter nround to have the desired result. The Boosting models don't converge at the first iterations.
xgbMatrix <- xgb.DMatrix(as.matrix(temp2),
                         label = Insurance$freq,
                         weight = Insurance$Holders)

bst = xgboost(data=xgbMatrix, objective='count:poisson', nrounds=500, verbose = 0)

Insurance$predFreq<-predict(bst, xgbMatrix)

with(Insurance, sum(Claims)) #3151
with(Insurance, sum(predFreq*Holders)) #same

Poisson xgboost with exposure


Undefined index und error

Question: I'm getting undefined index:und error. below is my code
<code>$photoItem = file_create_url($node->field_thumb_img[LANGUAGE_NONE][0]['uri']);
</code>
Answer: why not

file_create_url($node->field_thumb_img[$node->language][0]['uri']);
Answer: you can do $photoitem = field_get_items('node', $node, 'field_thumb_img') ;
field_get_items($entity_type, $entity, $field_name, $langcode = NULL);
see https://api.drupal.org/api/drupal/modules!field!field.module/function/field_get_items/7

Undefined index und error


Poisson regression with offset vs poisson regression with weight

Question: I would like to know the linear expression of weight and offset in terms of poisson regression in glm.
for instance for offset
   glm( y ~ x + offset(of), data, family=poisson(link="log"))

the above glm model can be expressed as:
  log(y) = constant + beta*x + log(of).

My question is what is the equivalent expression for the following glm model:
glm(y ~ x, weight=weight, data, family=poisson(link="log")).

From my understand offset and weight are different and impact the estimates differently.
Comment: I thought about this more. I've deleted my previous comments and posted a longer answer instead.
Comment: Thanks @Gordon Smyth
Answer: I think your question may be prompted by the fact that Poisson log-linear glms can be used to model either counts or frequencies. If count is a vector of counts and of is a vector of positive offsets, then the following two fits are equivalent:
fit.count <- glm(count ~ x + offset(of), family=poisson)

and
frequency <- counts / of
fit.freq <- glm(frequency ~ x, family=poisson, weights=of)

Typically here of is the total exposure time. In the first version, the counts are modeled in the usual glm way, and the fact that the expected counts are proportional to exposure time is coded in the offset.
In the second version, we compute the frequency of counts per unit exposure time. This explicitly removes the dependence of the counts on exposure time but changes the variance. The weights argument then has to be used to reflect the fact that frequencies based on the longer exposure times are more precise.
Although the two response variables are on different scales, the two glm fits are completely equivalent in all important respects. They will yield the same coefficient estimates, the same deviances and the same p-values.
The equivalence of the two models has previously been pointed out by Alan Chalk on this forum: Can Weights and Offset lead to similar results in poisson regression?
This same trick can be used for any glm with an offset and a log-link. It can also be used for binomial glms where the response variable can be either the binomial count or the proportion of successes.


S. L. Loney's Coordinate Geometry (1895 Ed.) Article 66, Section III

Question: I couldn't decipher the result mentioned in the second paragraph of section III (particularly due to the words "in which the origin lies") but based on the further elaboration by the author in square brackets [] i.e.,
The angles between the two lines & the angles between their perpendiculars from the origin are supplementary (i.e., their sum is 180$^\circ$).
And second thing I couldn't understand that is also mentioned in the square brackets is, "O, $R_1$, $R_2$ & A lie on a circle" $-$ How ?
I've tried to prove the supplementary angle result based on the theorems of Circle mentioned in the 10 standard math textbooks (last lesson taught in Euclidean geometry in most of the schools) but I failed.Here's the image of the exact pageAnd if image link does't work, here's the archive direct link to the book page no. 43:
https://archive.org/stream/elementsofcoordi00lone#page/43/mode/1up
So I've got two problems the one in bold above & the How question.
Any help would be appreciated.
Answer: The beginning of Article 66 on page 42 gives the missing context that the two lines intersect in the point $A$. In order to understand Section III and the bracketed text, it is best to draw a new diagram with the point $O$ as one end and $A$ as the other end of a diameter of a circle. Any other point $R$ on the circle forms a right triangle with the diameter being the hypotenuse and the right angle being at $R$ and, thus, the line $OR$ is perpendicular to line $RA$. Here, the two lines intersect the circle at $R_1$ and $R_2$.
The two right triangle side angles are complementary since their sum plus the right angle add up to two right angles. If the points $R_1$ and $R_2$ are on opposite sides of the diameter $OA$, then the angle between $R_1O$ and $R_2O$ is supplementary to the angle between $R_1A$ and $R_2A$, which is easy to see. If they were of the same side, then the angle between the perpendiculars from $O$ is the same as the angle between the lines from $A$.
Comment: I only have one confusion now:
Is it always the case that with OA as the diameter of a circle, if perpendicular be drawn  from O to any line through A, it always lie on (or inside?) the circle?
Comment: Given two intersecting lines at point $A$, they form four angles in equal pairs of **vertical** angles. The angle measures of the two pairs are supplementary, for which see http://en.wikipedia.org/wiki/Angle. The text is saying the same thing, and it is assuming that the points $R_1$ and $R_2$ are on the same side of the line $OA$.
Answer: Hope the following picture can help.

The key factor is "$\angle OR_1L_1 + \angle OR_2L_2 = 180^0$" implies "$OR_1AR_2$" is a cyclic quadrilateral".
$\angle R_1AL_2$ being the exterior angle of that quadrilateral, we therefore have  $\angle R_2OR_1 = \angle R_1AL_2 = \beta - \alpha$.
Comment: I only have one confusion now:
Is it always the case that with OA as the diameter of a circle, if perpendicular be drawn  from O to any line through A, it always lie on (or inside?) the circle?
Comment: @TheOmega  Both are yes because of the converse of the "angles in semi-circle" theorem (otherwise also known as the Thale's theorem)>
Comment: Yeah, obviously, Oh man this thinking in analytical geometry has blinded me toward Synthetic geometry.
Thanks [Mick](https://math.stackexchange.com/users/42351/mick)


Laravel Dusk - click on element based on attribute

Question: I have this code:

      <button offset="-30" role="link">2:30 pm</button>

      <button  offset="-15" role="link">2:45 pm</button>

      <button  offset="0" role="link">3:00 pm</button>

      <button  offset="150" role="link">5:30 pm</button>

   <button  disabled="" role="link"></button>

I'm using Laravel Dusk and
How I can click on a button based on datetime attribute?
I tried:
$browser->click($browser->element('li[datetime="2019-06-03T15:00"]'))->pause(2000);

but I got an error:

ErrorException (E_WARNING) preg_match() expects parameter 2 to be
  string, object given

Is there any way to click on an attribute?
Comment: Just pass a string, don't pass the element. `$browser->click('li[datetime="2019-06-03T15:00"]')`. The click function expects a selector (as a string) and not a `browser` element, hence the preg_match error.
Comment: @Ohgodwhy This dont works
Comment: Try these 2 links too which are extremely useful for me [github](https://github.com/laravel/dusk/issues/303#issuecomment-311656596) and [forum](https://www.5balloons.info/understanding-selectors-laravel-dusk-browser-testing)
Answer: this worked:
$btn = $browser->element('YOUR_SELECTOR');
$btn->click();


Controlling for a variable when estimating interaction effects

Question: Consider the relationships among four variables: sex, activity, height, and weight, as depicted below:

Suppose that weight is the response variable. When sex serves as the predictor, there is no need to control for any covariates. However, when activity is the predictor, we should include both sex and height as covariates.
Here are my questions (with the understanding that weight is always the response variable):
(1) When height is the predictor, what variable(s) should we control for?
(2) Suppose we wish to estimate the interaction effect between sex and activity ("Do the two sexes differ in the association between activity and weight?"). Should we control for height in this scenario?
(3) Suppose we wish to estimate the interaction effect between sex and height ("Do the two sexes differ in the association between height and weight?"). Should we control for activity in this case?
Answer:
You must control for sex, because it sets up a backdoor path, and is hence a confounding variable. You should NOT control for activity, because some of the causal effect of height on weight is mediated through activity.

For sex and activity interaction, you should include height because it is a backdoor path to the combined cause {sex, activity}.

For the sex and height interaction, there's no need to include activity, because it is not a confounder. However, it's possible you might get more precision by including it, since it does directly influence the response.
Comment: That's a good question. I think I would still include (that is, control for) `height`, even if you choke off some of the causal effect of the mediator, and here's why: you would still get the specific effect of `sex` and `activity` on `weight` while avoiding confounding. This seems to me a better way of getting at the interaction of `sex` and `activity` than not including `height`. But I have no other, more fundamental way of thinking about this case.
Comment: Thank you for your answer! Just to clarify, for both questions (2) and (3), `weight` always remains the response/outcome variable. These two questions are specifically centered around the respective interaction effects on `weight`. Considering this clarification, would your answer change?
Comment: Oh, I didn't read your question carefully enough. I'll edit.
Comment: Thanks! With one exposure, it is easy to understand the situation. However, I still have trouble handling an interaction effect between two exposures and determining how to control for a variable when two exposures are involved. Any literature about this topic? Is there a way to figure this out using, for example, the R package `dagitty`?
Comment: Here is your answer to question (2): "For sex and activity interaction, you should include height because it is a backdoor path to the combined cause {sex, activity}." I'm confused about this because `height` also acts as a mediator between `sex` and `weight`. In this case, should we avoid using it as a covariate for the interaction between `sex` and `activity` on `weight`?


mySql - get common values from multiple queries

Question: My database table looks like this:

I am trying to run a query that would return all the common user_id's where - for instance, field_id = 552 AND value = 'Savannah' AND field_id = 553 AND value = 'GA' AND field_id = 554 AND value = 'USA'. (so in this case, based on the records visible in my screenshot, the result would simply be '1').
I've been looking at joins and unions, but I feel I've gone down a rabbit hole and can't find my way back out.
Is this possible?
Answer: Here's one option using conditional aggregation:
select user_id
from yourtable
group by user_id
having max(case when field_id = 552 then value end) = 'Savannah' and
       max(case when field_id = 553 then value end) = 'GA' and
       max(case when field_id = 554 then value end) = 'USA'

SQL Fiddle Demo
Comment: This seems to do exactly what I need it to, thank you.


Streamline web.config connectionString encryption

Question: Is there a way to streamline the process of encrypting the connectionstrings after you deploy a web application?
Here's the backstory.  We manage many .NET websites.  We move them to a development server and then eventually up to the production server.  But after each site is created on the development box or the server box, I have to go onto the server and encrypt the sensitive data (usernames / passwords / connectionStrings).
This becomes tedious when you start talking hundreds of websites.  Is there a better way than doing it by hand?  Something that a developer who doesn't have access to Remote into the server could run after they deploy the application?
Or is this just wishful thinking?
Comment: How exactly are you encrypting those sections?
Comment: Through the command line using the aspnet_regiis tool.
Comment: Have you come up with something better in the year since you originally wrote this? Do share, if so!
Comment: Unfortunately not.  It seems Microsoft hasn't implemented an easy way to remotely do this.  :(
Comment: I would use deployment software (like Octopus for .NET), and then run a powershell script post deployment that would execute those commands for you.
Answer: I would use deployment software (like Octopus for .NET), and then write a post deployment powershell script that would execute those commands for you.  Add that powershell script to the PostDeploy hook, and it should take care of that  (Most, if not all, mainstream deployment tools come with ways to execute commands after a deployment).


O identificador de varias partes "System.Data.DataRowView" não pode ser associada

Question: try
                {
                if (cmbFavorecido.SelectedValue != null )
                {

                    txtID.Text = cmbFavorecido.SelectedValue.ToString();

                    cn = conexao.ConectarSqlServer();
                    cn.Open();
                    SqlCommand cmd = new SqlCommand("select CNPJ_FORNECEDOR FROM FORNECEDOR WHERE ID_FORNECEDOR =  " + txtID.Text, cn);
                    SqlDataAdapter ad = new SqlDataAdapter(cmd);

                    DataSet ds = new DataSet();
                    ad.Fill(ds, "TbFornecedor");
                    txtCnpj.Text = ds.Tables["TbFornecedor"].Rows[0].ItemArray[0].ToString();

                }

Comment: Poste o código, imagem não é recomendada neste caso.
Comment: Como o @rubStackOverflow já disse, não coloque uma imagem do seu código. Clique em [edit] e poste o código propriamente dito. Aliás, dê uma olhada em [como criar um exemplo mínimo, completo e verifícavel](/help/mcve) e [ask] *=)*.
Comment: Pronto, postei o codigo, se puder me ajudar serei grato.
Answer: Tente fazer da seguinte forma.
 if (cmbFavorecido.SelectedValue != null)
            {
                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = myConnString;
                try
                {
                    String txtID = cmbFavorecido.SelectedValue.ToString();
                    txtID.Text = txtID;

                    //cn = conexao.ConectarSqlServer();
                    //cn.Open();

                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = "select CNPJ_FORNECEDOR FROM FORNECEDOR WHERE ID_FORNECEDOR = @txtID";
                    cmd.Parameters.AddWithValue("@txtID", txtID);

                    SqlDataAdapter da = new SqlDataAdapter();
                    DataTable dt = new DataTable();

                    da.SelectCommand = cmd;
                    conn.Open();
                    da.Fill(dt);

                    txtCnpj.Text = dt.Rows[0][0];

                }
                catch (SqlException sqle)
                {
                    // MessageBox.Show("Falha ao efetuar a conexão. Erro: " + sqle);
                }
                finally
                {
                    conn.Close();
                }
            }

A uma coisa importante em seu código que você precisa ter cuidado, que é a passagem de parametros. Da forma que você esta fazendo você corre o risco de alguém usar Injeção SQL, por isso user sempre Parameters.AddWithValue("@txtID", txtID); para passar parâmetros.
Assim os tipos dos parâmetros são adequados aos tipos das colunas e a instrução SQL fica por conta do ADO.NET.
Além disso, uma boa prática é efetuar validações.
Comment: Marconcillio Souza, quando vou executa o código,na linha onde tem o "ad.fill(dt); da o seguinte erro com a mensagem   :                                "Fill: Propriedade SelectCommand.Connection não foi inicializada."
Comment: Veja agora .... faltou passa sua string de conexão .
Comment: Cara tive que mudar o nome da variavel txtID pois estava dando conflito com o nome da TextBox, no ad.SelectCommand foi atribuido  o cmd (objeto do sqlcommand) , na hora de pedi pra exigir no TextBox acrescentei o .toString();                                                                                só que ainda continua dando erro no ad.fill(dt) dessa vez com a seguinte mensagem "Não existe mapeamento do tipo de objeto System.Windows.Forms.TextBox para um provedor gerenciado de tipo nativo"    cara desde já agradeço por essa ajuda


How to add a boolen attribute to a form?

Question: I am trying to add the boolean attribute "novalidate" to all my forms.
I have tried numerous approaches in my theme_preprocess_form function but no luck. I see in some preprocess functions, $variables['attribute'] is an object and has methods like addClass() and setAttribute(). But in _preprocess_form it is an array. Plus setAttribute doesnt sound like it will do boolean attributes anyway.
I tried to simply override the twig template for a form and do this:
<form{{ attributes }} novalidate>
  {{ children }}
</form>

but even that outputs:
<form novalidate="">

Comment: `novalidate=""` is fine for the HTML spec - boolean attributes can have a value as long as it's an empty string or the same (case-insensitive) string as the attribute name. So `novalidate="novalidate"` would also be fine. If you follow those rules, the attribute's existence is taken to mean `true` regardless of the value. See https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes#boolean_attributes
Comment: You should put that as an answer Clive - it's the correct answer to the question.
Comment: I was in two minds @Jaypan, maybe the "proper" answer to this is one which allows Twig/Drupal to output the bare `novalidate`, even though it doesn't technically matter? Probably over-thinking it, I'll add an answer
Comment: Well, if someone comes along with that other answer, then it too can be an answer to the question. Both will be valid answers.
Comment: Your browsers' dev consoles usually render it as ``, but it is in fact still ``. You can use the ancient method of viewing the source ("View Page Source" in Firefox and Google Chrome) to see the actual HTML markup.
Answer: You may be attempting to solve a non-issue here. novalidate="" is fine for the HTML spec - boolean attributes can have a value as long as it's an empty string or the same (case-insensitive) string as the attribute name. So novalidate="novalidate" would also be fine.
If you follow those rules, the attribute's existence is taken to mean true, regardless of the value.
See https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes#boolean_attributes for more info.
Comment: Yes, I figured this would work functionally although I see there are a few issues on D.org with people asking how to add "boolean attributes" as it is supported in HTML5 and was wondering if there was a way to do this the "right" way.
Comment: Yeah it's the sort of thing I'd want too - it's my HTML, give me what I want! But there's too much in the way that could be manipulating the HTML to worry about it IMHO. For example, when you parse HTML into a `DOMDocument`, it's held in an XML structure. Attributes can't have empty values in XML, so boolean HTML attributes are given an empty value by default, and when the doc is saved as HTML, you get what you've seen in your question. Drupal uses `DOMDocument` in various parts of the pipeline, so that might even be literally what's happening here. Too much effort to change I'd say


Undo Database Restore in SQL Server 2008

Question: I accidentally restored a SQL Server 2008 database with a wrong backup. Is there any way to undo this and restore to previous version?
-Vivek
Answer: No. You've replaced what's there with your restore. You would have to treat this the same as someone dropping your database - go to the most recent full database backup you have and then restore all subsequent log backups.
Sorry... you probably have that sick feeling right now. I feel bad for you.
Comment: it's on a windows 2008 server. any chances of retrieving a lost file?
Comment: Nope, sorry....


Is $f'(a)\ge0$ or $f'(a)>0$

Question:
If $f$ is smooth s.t. $f<0$ on $(0,a)$ and $f>0$ on $(a,1)$ is then $f'(a)\ge0$ or $f'(a)>0$ ?

Is it possible that $f'(a)=0$, maybe you have an example ?
Comment: Try $f(x)=(x-a)^3$
Comment: I think it's funny how almost everybody (me included) thought of the same thing.
Comment: @RiccardoOrlando except me
Comment: Oh well. Getting a piece of paper and drawing stuff usually helps.
Answer: here is a simple example :
$f:x\to (x-a)^3$

Is $f'(a)\ge0$ or $f'(a)>0$


The sum of $2$ consecutive primes, Are there more $\mod 6=0$?

Question: All prime numbers greater than $3$ take the form of either $6x+1$ or $6y-1$.
So the only options for the sums of $2$ consecutive primes are:
$(6x+1 + 6y-1) \mod 6=0$
$(6x-1 + 6y+1) \mod 6=0$
$(6x+1 + 6y+1) \mod 6≠0$
$(6x-1 + 6y-1) \mod 6≠0$
According to the limited available options above, the chances for $(6x+1 + 6y-1) \mod 6=0$ or $(6x-1 + 6y+1) \mod 6=0$ to occur are only $50\%$,
However:
Out of the first $200000$ prime numbers, when adding a prime number with the following prime number, $115141$ resulted in $\mod 6=0$ which is about $57.5705...\%$
When I mention "following prime number", I mean it as:  $5+7$  ($7$ is following $5$). another example: $31+37$ ($37$ is following $31$),  another example: $43+47$ ($47$ is following $43$).
Why is it that there are more $\mod 6=0$ occurrences then $\mod 6≠0$ occurrences, when the probability shows that the chances for $\mod 6=0$ is just $50\%$
Am I expecting to see these kind of statistics (margins) continue infinitely? Can it be proven?
Or maybe it is just that my sample test of 200000 is too small, and I should expect a correction in larger samples?
Update: Per @Peter 's comment:
A slight decrease if we continue until [$3⋅10^9+19,3⋅10^9+37$] :
[$79805741,64643795,0.55248180928736247377077071400215505019$]
Comment: Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackexchange.com/rooms/120324/discussion-on-question-by-isaac-brenig-the-sum-of-2-consecutive-primes-are-th).
Answer: This is not a proof, but reasoning to explain why the excess above $50\%$ is around $1/\ln N$, and shrinks very slowly.
Consider numbers near $N$ but only the ones of form $6n\pm1$.  Any random number has a chance of $1/\ln N$ to be prime, but since numbers of the other four remainders are never prime, the chance any one of ours is prime is $$p=\frac3{\ln N}$$
For any prime, assuming a random model for primes, the chance the next prime is opposite remainder, and none of the ones in-between is prime, is
$$p+(1-p)^2p+(1-p)^4p+\ldots\\=\frac p{1-(1-p)^2}\\=\frac1{2-p}$$
When $N=10^6$, this estimate is $56.09\%$.
Comment: You should emphasize that this is only a heuristic. I know this is a very common technique used in analytic number theory, used to make conjectures and usually gives the right results. Those unfamiliar with analytic number theory could think this tries to be a proof. This is not a formal proof. It is, however, a good reason to think that the limit is 50%.
Comment: @jjagmath When you say "obviously diverges", are you reading $p$ as a prime number or as a probability when $N\ge 21$?


How to setup TLS for postfix? Should I use SASL with dovecot?

Question: I need to setup TLS/SSL for postfix, so I can send mail from my iphone via my smtp mailserver (or else i will get relay access denied messages).
From what I understand this is one way:
https://www.rosehosting.com/blog/set-up-ssl-encrypted-connection-in-postfix-dovecot-and-apache/
But I can also do it with the SASL framework and use dovecot:
https://wiki2.dovecot.org/HowTo/PostfixAndDovecotSASL
Which is better/more appropriate?
Answer: Both? Dovecot and Postfix are different things, you can encrypt your MTA traffic with TLS, and have your dovecot connections handled and authenticated using SASL. It's not a case of one or the other, you should implement both.
Comment: I realise that i should implement both, but the second link clearly states: "Postfix supports SMTP AUTH through Dovecot SASL "
Comment: Yes, it does. So set your Postfix config to handle traffic over TLS, and specify that it should auth against Dovecot with sasl, as per `smtpd_sasl_type = dovecot` etc. Then configure your Dovecot to support those incoming auth requests (I don't think this will take much, if any, work).
Comment: Essentially, set your auth up as per your second link, but configure your postfix TLS params as per the first link. Do both. As an advisory: TLS configuration for postfix goes a lot further than is detailed in link 1.
Comment: Are you saying there is more to configure after doing both links? Is there any complete guide you know off that covers the whole spectrum? I'm new to all this. All i want is to send email from my iphone via my newly setup postfix server :-)
Comment: Doing the stuff in the two links provided will get you that far. The point I was making is that there's a lot more around securing a server/MTA. I'd suggest googling: securing postfix, securing Dovecot, securing a Debian/Ubuntu/your os server.
Comment: @ATv no worries, if you're happy with the answer give a tick.


Desktop To Command Line Toggle for Default Command Line Environment?

Question: [Fedora 30 Webserver] I am wanting to use MySQL Workbench and Wireshark (amongst other things) on my server, but neither of those two programs can be made to work on the command line; so what I am wondering is whether or not there is anything that I could install that would allow me to toggle back and forth between a pseudo-desktop environment and command-line in order to run them?
I have good reasons for prefering the command line, only a 14" screen on the webserver (laptop) and do not want to be using VNC or installing a full desktop environment on the web server laptop if at all avoidable.
Answer: Either install a desktop environment or don't, pick one. For a server, it is traditional to not have a desktop. Less to secure, update, and run in RAM. Download packet traces somewhere else, and connect to SQL remotely.
Virtual machines and containers are common these days. You can manage the same no-GUI environment for dev (VM on laptop) test and production (VM in cloud).
Linux desktops with CLIs on emulated terminals or virtual consoles still exist, of course. Command line is just another GUI app to pull up. However, these tend to be management or personal boxes. Anything running a serious service is on a server.
Comment: Thanks, - I will stick with multiple machines, as I have at present. I am not keen on cloud-based solutions where my data is liable to be arbitrarily deleted, censored, and handed over on demand to whatever agency demands it or at the whim of the service provider and in line with recent EU or US legislation.


zxing :unable to read barcode which is a part of a larger image

Question: i am using javase package of zxing. In my images barcodes are actually a part of a larger image. When i provide those images as it is then zxing is unable to read the barcode but if i crop the image to just the barcode then zxing runs just fine. Is there some possible way to make zxing recognise the barcode in large images as well without me providing it the barcode coordinates.
Thank You
as an example consider these 2 images - https://www.dropbox.com/sh/heyid7mwkljrhdb/A4aFanT7Vg
Comment: I don't have an answer but hope this post helps you:

http://stackoverflow.com/a/12721976/259889
Comment: any news on this? I'm having issues even when the barcode is ~1/8th total size of page, but in top-left corner
Answer: I'm able to read this image. Some just won't decode by chance. You should try using TRY_HARDER mode hint if you are decoding on a desktop computer. Also try swapping out a different Binarizer.
Comment: when does a `Binarizer` come into play for decoding QR codes? I haven't seen that in examples


Extract data from an array correctly

Question: I am trying to get data from an array on form of:
  Array ( [actual-1] => 2 [action-1] => blabla [actual-2] => 1 [action-2] => sss [actual-49] => 3 [action-58] => sasa )

 ID 1 Level 2 action blabla
 ID 2 level 1 action sss
 ID 49 level 3 action sasa

I tried the following code:
foreach(array_chunk($array,3,true) as $val){

   foreach($val as $k=>$v){
      if(strpos($k, "actual") !== false){
          $temp = explode("-",$k);
          $id = $temp[1];
          $actual = $v;
      }
      if(strpos($k, "action") !== false){
          $action = $v;
      }
   }
    echo "ID ".$id." Level ".$actual." action ".$action;
    echo "
";
}

But what I get instead was:
ID 2 Level 1 action blabla
ID 49 level 3 action sasa

Answer: You're chunking your array into groups of 3 elements instead of 2. Change the second argument in array_chunk to 2.
Answer: foreach(array_chunk($array,3,true) as $val){

needs to be
foreach(array_chunk($array,2,true) as $val){

Comment: Thank's a lot, I dont know why I put 3 there.

Extract data from an array correctly


Models of fractal with rational valued dimension

Question: Most constructive calculable models of geometric objects with non integer dimensionality as Cantor set, Koch curve, Sierpinsky triangle, etc.;  give rise to values of dimensionality that are integer base logarithms  of integers as $\frac{\log(2)}{\log(3)}$, $\frac{\log(3)}{\log(4)}$, $\frac{\log(3)}{\log(2)}$, etc. ; and in consequence are trascendental numbers because of Lindemann results.
Do you know some finite deterministic recipe for constructing geometric objects with rational values of dimensionality as $\frac{1}{2}$ or $\frac{3}{2}$?
Conversely: Could it be shown that such recipes don't exist at all?
Answer: The "usual" ternary Cantor set is the fixed point (or attractor, or invariant set) of an iterated function system with two maps on $\mathbb{R}$:
$$\varphi_1(x) = \frac{1}{3}x \qquad\text{and}\qquad \varphi_2(x) = \frac{1}{3} x + \frac{2}{3}. $$
That is, the Cantor set is the unique nonempty compact set $\mathscr{C}$ such that
$$\mathscr{C} = \varphi_1(\mathscr{C}) \cup \varphi_2(\mathscr{C}). $$
As you note, the dimension of the Cantor set is $\log_{3}(2)$ (though this depends on what notion of dimension we are using—I'll assume that the Hausdorff dimension or the box-counting dimension is meant; in this case, they coincide).  It turns out that this dimension can be computed in terms of the iterated function system.
Suppose that $\{\varphi_j\}_{j=1}^{J}$ is a finite collection of maps on $\mathbb{R}^d$ of the form
$$ \varphi_j(x) = r_j O_j x + b_j, $$
where each $r_j \in (0,1)$ is the contraction ratio of the map $\varphi_j$, each $O_j$ is some unitary map (i.e. a reflection in $\mathbb{R}$, or a rotation or reflection in $\mathbb{R}^2$), and each $b_j \in \mathbb{R}^d$ is a translation (such a map is called a contracting similitude, and the collection of maps is a self-similar iterated function system).  A fairly powerful theorem (the Banach fixed-point theorem) guarantees that there will be a unique nonempty compact set $\mathscr{A}$ such that
$$ \mathscr{A} = \bigcup_{j=1}^{J} \varphi_j(\mathscr{A}). $$
Moreover, if the iterated function system is sufficiently "well-separated," i.e. if there is some open set $U$ such that

$\varphi_j(U) \subseteq U$ for all $j$, and
$\varphi_j(U) \cap \varphi_k(U) = \emptyset$ for all $j\ne k$,

then the Hausdorff dimension of $\mathscr{A}$ will be the unique real solution $s$ to the Moran equation, which is given by
$$ 1 = \sum_{j=1}^{J} r_j^s. $$
If all of the contraction ratios are the same, i.e. if $r_j = r$ for all $j$, then the Moran equation simplifies to
$$ 1 = \sum_{j=1}^{J} r^s = J r^s
\implies r^s = \frac{1}{J}
\implies s \log(r) = -\log(J)
\implies s = -\frac{\log(J)}{\log(r)}. $$
Note that $r < 1$, hence $\log(r) < 0$, which implies that $s$ is positive (which is what we would expect).

All of this gives us one possible way of engineering sets of arbitrary Hausdorff dimension.  For example, suppose that we wish to construct a set with Hausdorff dimension $\frac{1}{2}$.  In a fair and just world, we could find such a set as the attractor of an iterated function system $
\{\varphi_1, \varphi_2\}$ on $\mathbb{R}^2$, so suppose that we live in a fair and just world.  Since the dimension of our set $s = \frac{1}{2}$, and we have two maps, the Moran equation gives us
$$ \frac{1}{2} = -\frac{\log(2)}{\log(r)}
\implies \log(r) = -2\log(2)
\implies r = \mathrm{e}^{-2\log(2)} = \frac{1}{4}. $$
Therefore an iterated function system with two maps of contraction ratio $\frac{1}{4}$ will get the job done, assuming that we can find one which satisfies the separation condition above.  But this can be done by setting
$$ \varphi_1(x) = \frac{1}{4} x
\qquad\text{and}\qquad
\varphi_2(x) = \frac{1}{4}x + \frac{3}{4}. $$
Note that
$$ \varphi_1((0,1)) = (0,\tfrac{1}{4})
\qquad\text{and}\qquad
\varphi_2((0,1)) = (\tfrac{3}{4},1), $$
where both sets are disjoint and contained in $(0,1)$.  Thus this iterated function system does, in fact, satisfy the separation condition, and we do live in a fair and just world!  In other words, we have found a way of building a set with Hausdorff dimension $\frac{1}{2}$.

More generally, given any positive real number $s$, it is possible to build an iterated function system with attractor $\mathscr{A}$ such that the Hausdorff dimension of $\mathscr{A}$ is equal to $s$.  The general idea is the same as shown above, but the details might require a little more care.  For example, if $s > 1$, then more than two maps will be required to build such a set, and we'll have to work in a larger ambient space (i.e. $\mathbb{R}^d$ with $d > 1$).  Alternatively, we could construct Cantor-like sets such as the one above, then start working with Cartesian products (though there are some other technical details which need to be addressed).
Comment: The "typical" examples of sets constructed in this manner will have irrational Hausdorff dimension, but this shouldn't be surprising---the set of rational numbers is small with respect to the set of real numbers (in most senses of the word "small").  It seems reasonable to expect that we would have to work harder to find rational examples.
Comment: It appears (not specifically in the case that you have given) that in most situations: in order to get rational Hausdorff´s, you should choose irrational contraction ratios and vice versa
Comment: Anyway, in some sense, it seems somewhat paradoxical that more 'rational'  (intuitive ,concise) formulations models lend to more 'complex' qualificators (attributes) of the constructed object.


Logic: How to disable live microphone playback?

Question: How do I get logic to stop doing a live playback of whatever the microphone hears?  I only have this problem in one project, but it's driving me nuts.  It interferes with recording, because there is a slight delay before the sound is played back.  I just want to turn it off.
Even when every track in the project is muted and nothing is being recorded, you can still hear sounds picked up by the microphone in the audio output.
Answer: There is an "I" button next to the "R" record button, both in the track and on the channel strip. This is for input monitoring. If this is on then Logic will always play the input from that channel regardless of whether it's armed for recording. This combined with direct monitor selected on your interface will mean your sound is being played from two sources at once and there's usually a bit of latency on the DAW side.
When I record I use direct monitoring on my interface and make sure the "I" is deactivated. That way I am hearing my sound before it reaches Logic with no latency. However the "I" can be useful when punching combined with "auto input monitoring". When in play mode you hear the original track and your instrument is silenced until the actual punch. It depends on how you like to work.
Answer: Make sure to turn Input Monitoring off for the audio track you are recording to. You can find the Input Monitoring button on the track in the Mixer; the button has an "I" on it. Open the Mixer by pressing "X".
Answer: I had the same issue. Even with no tracks armed or live the mic could still pick up the room.  It turned out that somehow I must have created an Aux track and that's where it was routed to. When I brought up the mixer I could see the levels reacting on that Aux track. The issue was resolved after I deleted that track.


SQL return multiple count statements

Question: I have a table which contains ski resorts around the world.  The table also identifies which country and "sector" (USA/Europe/Asia etc.) they are located in.  I'm trying to determine the number of resorts in each sector.
Sample Data :
resort_id, resort_name, country_id, sector_id
3376    Chréa           204  204
3377    Tikjda          204  204
3384    Beidahu         208  205
3481    Canyon Ski Area 225  206
3482    Castle Mountain 225  206
3483    Drumheller      225  206

I need to be able to determine the number of resorts in each sector i.e. :
Sector       Resorts
--------------------
204          2
205          1
206          3

Any help would be much appreciated thanks.
Answer: select sector_id,
       count(*)
from   resortTableName
group by sector_id

and to address your edited question:
select sector_id,
       count(distinct resort_name) as resortCount,
       count(distinct country_id) as countryCount
from   resortTableName
group by sector_id

Comment: However, in my haste, I've oversimplified my question.  I also need to determine the number of countries in each sector in the same query, thus the "multiple" of the title.  Any Suggestions ?
Answer: You just need to count and group the results by the sector:
SELECT   sector_id, COUNT(*)
FROM     resorts
GROUP BY sector_id

Answer: It is a simple group By
Select count(*) As Resorts, sector_id as Sector
from yourtablename
group by sector_id

SQL return multiple count statements


How to set default value for variable?

Question: Imagine I have the following code in javascript
function test(string) {
    var string = string || 'defaultValue'
}

What is the Python way of initiating a variable that may be undefined?
Comment: The answers tell you how to have a parameter with a default value, but maybe you want this: http://stackoverflow.com/questions/23086383/how-to-test-nonetype-in-python
Answer: In the exact scenario you present, you can use default values for arguments, as other answers show.
Generically, you can use the or keyword in Python pretty similarly to the way you use || in JavaScript; if someone passes a falsey value (such as a null string or None) you can replace it with a default value like this:
string = string or "defaultValue"

This can be useful when your value comes from a file or user input:
string = raw_input("Proceed? [Yn] ")[:1].upper() or "Y"

Or when you want to use an empty container for a default value, which is problematic in regular Python (see this SO question):
def calc(startval, sequence=None):
     sequence = sequence or []

Comment: It's worth mentioning, for `None` specifically, do `string = string if string is not None else "defaultValue"`. Reference: [How to "test" NoneType in python?](https://stackoverflow.com/q/23086383/4518341)
Comment: I wouldn't use `sequence or`, cause it's too loose. For example, if I accidentally passed in the integer `0`, I'd expect a `TypeError`, but this'd silently use the default. This'd be better: `if sequence is None or len(sequence) == 0: sequence = []`. But thinking about that, why bother swapping out empty sequences? If you're actually intending to only replace `None`, do that explicitly instead of using `or`: `sequence = sequence if sequence is not None else []`.
Answer: For a function parameter, you can use a default argument value:
def test(string="defaultValue"):
    print(string)

test()

Answer: You can use default values:
def test(string="defaultValue")
    pass

See https://docs.python.org/2/tutorial/controlflow.html#default-argument-values

How to set default value for variable?


Python alsaaudio capturing sound

Question: I'm trying to capture data from microphone. Code is pretty simple but I get the error alsaaudio.ALSAAudioError: Capture data too large. Try decreasing period size for some reason. Tried google it but nothing...
res = []

recoder = alsaaudio.PCM(type=alsaaudio.PCM_CAPTURE)
recoder.setchannels(1)
recoder.setrate(8000)
recoder.setperiodsize(80)
recoder.setformat(alsaaudio.PCM_FORMAT_S8)

while len(res) < 8000*5:
    len, frame = recoder.read()

    if len(frame) != 0:
        print(len)
        res.extend(frame)

The most interesting part is that decreasing doesn't help. But increasing changes the error: period size more than 135 gives me Input output error.
OS Ubuntu 16.04.
Does anybody know what the problem is?
UPDATE
Removing recoder.setchannels(1) does the trick and I can set rate/period to 8000/80 and it works but can't set to 8000/10 for example. Got the same error. It seems like a bug.
Comment: Try lowering the rate as well as period to something redicilously low?
Comment: @Torxed 7/1 for rate/period works. 8/1 doesn't. But.. Nothing is printed.
Comment: Do you have to use `alsaaudio`? You could try [sounddevice](http://python-sounddevice.rtfd.org/) ...
Answer: First of all in your code,
len, frame = recoder.read()

len is the keyword in python, you can not use it as variable like that.
Otherwise it runs on my laptop OS, no such error you mentioned, but I did face the same issue as you.
What I did is specifying the PCM mode and giving it an appropriate period size.

Python alsaaudio capturing sound


Unsupervised answering for a predefined set of questions

Question: I am working on a project to read up a text segment and find answers to a specific set of questions, in order to do some information extraction.
I have a set of text corpus (each of about 3000 words), and I have about 10 questions to get the important facts out of the text. However, I do not have a set of answers to these question, so I cannot take a supervised approach to train a model.
What kind of an unsupervised approach can i use to get the answers ( this can be extractive text segments) from the text corpus.
I have thought about using word embeddings to find words in the text that match, or are close to the words in the question, but I'm not too sure on how to go about this.
Any ideas would be greatly appreciated!
TIA
Answer: One approach could be segmenting each text into paragraphs (if you have some formatting clues like double end-of-line) or sentences. Then for each chunk (paragraph / sentence), you can compare if it is similar with your questions. For similarity, you need to vectorize both question and chunk and here word embeddings can help although you need to come up with vectors for these chunks / questions, so more like doc2vec approach or manually averaging word vectors. In the latter case, I would suggest weighing with tf-idf of words.


Get Class Name with extension in C#?

Question: I am trying to get class name with the extension (e.g. Employee.cs or Employee.aspx.cs) in my code. I was able to get the name of the class without the extension but does anybody know how can i also get extension of the class??
This is what i did to get class name:
var frame = new StackFrame(1);
string className = frame.GetMethod().ReflectedType.Name;

Answer: That is not possible with reflection. The name of the source compilation file is not part of the metadata of the type.
Answer: Call frame.GetFileName().
This will only work if you have the PDB file.

Get Class Name with extension in C#?


Possible 10-card hands from 10 standard deck of cards

Question: A certain casino uses 10 standard decks of cards mixed together into one big deck, which we will call a superdeck. Thus, the superdeck has 52 · 10 = 520 cards, with 10 copies of each card. How many different 10-card hands can be dealt from the superdeck? The order of the cards does not matter, nor does it matter which of the original 10 decks the cards came from. Express your answer as a binomial coefficient.
This question is already answered in the link below:
Combinatorics: Number of possible 10-card hands from superdeck (10 times 52 cards)
But I still have questions:
1.What does a 10-card hand mean? Does it mean all cards of the same kind?
2.What if the question had 11 decks instead of 10. Would $$\binom {52+11-1}{10}$$ be correct?
3.How are there 52+11-1 slots instead of 52*10=520 slots for 10 deck of cards?
Answer: "all cards of the same kind?"  No.  A hand of cards is just a subset of the cards.  Here, since we have duplicate cards we need to be a bit more careful with how we phrase it... it is a submultiset of the cards.  Order within the hand does not matter.  Two hands are considered the "same" if what cards in what quantities are the same in one hand compared to another.  For example, a hand that contains five $A\spadesuit$ four $K\heartsuit$ and one $K\clubsuit$ is considered the same as any other hand which contains those same type of cards in the same quantities, regardless order and what deck.  Here it is assumed that which deck a card comes from is irrelevant.  If every deck of cards had different art styles and a different cardback and could be distinguished it would have been a very different problem, one with an answer of $\binom{520}{10}$
"What if the question had 11 decks"  No, the answer will still be $\binom{10+52-1}{52-1}=\binom{10+52-1}{10}$, we did not need an $11$ there.  The $10$ that appeared in the formula here happened to be our handsize not the number of decks.  That the number of decks was ten is only relevant because it is greater than our number of cards in hand, allowing us to treat this as though it were pulling with replacement.  If it were five decks instead, we would need to be more careful, likely needing to combine stars-and-bars with inclusion-exclusion.
"how are there $52+10-1$ slots instead of $52\cdot 10=520$ slots for ten decks of cards?"  I don't know what you mean by slots here, but this is again a standard application of stars-and-bars.  We have different categories.  We want some collection of some number of those categories, repetition allowed order not relevant.  In order to split the categories up, we only need a barrier between the categories.
Consider this small example of having five identical cookies and we ask how we can split them up between me and you.  I say, we can draw a line.  All cookies on the left side of the line are mine and all cookies on the right side of the line are yours.  We have the possibilities:  $\mid \circ \circ \circ \circ\circ$ where I get none and you get five, we have $\circ \mid \circ \circ \circ \circ$ where I get one and you get four... on up to $\circ \circ \circ \circ \circ \mid$ where I get all five and you get none.  I only needed one barrier to separate these into two piles, not two barriers.  Similarly, if I wanted to split it into $k$ piles I only need $k-1$ barriers.
The essence of stars-and-bars is again to distribute $n$ identical balls (cards in our hand) into $k$ distinct bins (cardtypes) where order in which balls appear in bins doesn't matter this is like picking some arrangement of $n$ stars and $k-1$ bars which can be done in $\binom{n+k-1}{k-1}$ ways, equivalently $\binom{n+k-1}{n}$ ways.
Comment: So...I treat the 10 card-hand as one cookie each and the identical card sets (52) as the barriers and then apply stars and bars?
Comment: You need $51$ barriers to separate into $52$ categories, each category corresponding to a different rank-suit cardtype, the ten cards *from the ten-card hand* as one cookie each, not the hand itself as a single object.


Firebase java: during transaction errors, should I return Transaction.abort() vs Transaction.success(...)?

Question: Short version:
In a Firebase transaction (in Java), if I get unexpected or inconsistent (stale) values from MutableData.getValue(), how should I error check and make sure that the transaction runs repeatedly if necessary? When do I use Transaction.abort() vs Transaction.success()?
Long version:
I wanted to write a Firebase transaction in Java that

checks if the value "foo" is stored at a location, logging errors if it's different or missing, and then
updates the value to "bar".

We expect "foo" to be at the location because it's successfully written by the same client before the transaction is run.
Consider the following:
public Transaction.Result doTransaction(MutableData data) {
    String value = (String) data.getValue();
    if(value == null || !Transaction.equals("foo")) {
        return Transaction.abort();
    }
    data.setValue("bar");
    return Transaction.success(data);
}

I know data.getValue() may return stale values (including null) because of the eventual consistency of Firebase, and so the transaction may need to execute multiple times. But it seems like the transaction only ever runs once if I return Transaction.abort() in the if statement. If I replaced it with Transaction.success(data), wouldn't I be committing bad values? How do I detect when the value is actually null or not "foo"?
Answer: (Self answer; correct if necessary!)
Through experimenting, I've found that I need to use Transaction.success(...) if I want the transaction to ever run again. Transaction.abort() always stops Firebase from trying again, so I should only use it when I'm sure that the transaction cannot proceed. (e.g. if data.getValue() indicates that something is off at the data location, and it will not be corrected by any pending writes.) In my example, data.getValue() == null seems like a particularly bad case to abort(), since there may be new values pending from the earlier write.
If I return Transaction.success(data) in the error cases, Firebase will make sure the consistency eventually works out. e.g. I will not accidentally end up writing null to that location, unless the value given by data.getValue() really is missing.
I can do all the error checking in the Transaction.Handler.onComplete(...) method, which has a DataSnapshot parameter to check the final data at the location.
Comment: This looks mostly correct. Transaction.abort() should be used when the set operation is no longer applicable (i.e. some value in the data has changed in a manner that the update is no longer appropriate). In all other cases, .success() is the appropriate choice. As you've discovered, onComplete() is the right place for doing all the success work that should run exactly once.
Comment: @Kato Is there anything in particular that's wrong, or just phrasing or something? (Just trying to learn more : ] )


Retornar dados de pesquisa no banco em JTable

Question: Estou desenvolvendo uma interface que deve retornar em uma JTable os valores de uma tabela mysql. Desenvolvi o seguinte método:
public class Teste extends javax.swing.JFrame {

    private JTable table;
    DefaultTableModel modelo = new DefaultTableModel();

    public Teste() {
        initComponents();
    }

    public void popularJtable(String sql) {

        try {
            Connection con = new ConnectionFactory().getConnection();
            PreparedStatement stmt = con.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery(sql);
            modelo = (DefaultTableModel) table.getModel();

            modelo.addColumn("tipoCurso");
            modelo.addColumn("nomeCurso");

            while (rs.next()) {
                modelo.addRow(new Object[]{
                    rs.getString("tipoCurso"),
                    rs.getString("nomeCurso")
                });
            }
            table = new JTable(modelo);
            jScrollPaneCursos = new JScrollPane(table);
            stmt.close();
            con.close();

        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jScrollPaneCursos = new javax.swing.JScrollPane();
        Pesquisar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        Pesquisar.setText("Pesquisar");
        Pesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PesquisarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(59, 59, 59)
                        .addComponent(jScrollPaneCursos, javax.swing.GroupLayout.PREFERRED_SIZE, 264, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(146, 146, 146)
                        .addComponent(Pesquisar)))
                .addContainerGap(77, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(93, Short.MAX_VALUE)
                .addComponent(Pesquisar)
                .addGap(18, 18, 18)
                .addComponent(jScrollPaneCursos, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(66, 66, 66))
        );

        pack();
    }// </editor-fold>

    private void PesquisarActionPerformed(java.awt.event.ActionEvent evt) {
        String sql = "SELECT tipoCurso, NomeCurso FROM curso";
        popularJtable(sql);
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Teste().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton Pesquisar;
    private javax.swing.JScrollPane jScrollPaneCursos;

}

A idéia é chamar esse método dentro de um botão pesquisar, onde passo uma string com o script mysql (é um select nesse caso). Porém, aponta o seguinte erro:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

e aponta para a linha:
modelo = (DefaultTableModel) table.getModel();

Comment: Refiz o projeto, testei e o problema continua o mesmo e na mesma linha. A tabela que era para aparecer era a tabela curso, que tem as coluna idCurso, tipoCurso e nomeCurso.
Comment: Em qual linha agora?
Comment: modelo = (DefaultTableModel) table.getModel();
Answer: Você está tentando recuperar um model de uma tabela que nem foi criada ainda. Esta é a causa do nullpointer, a variável table está nula.
Nem tem como sugerir nada porque esse novo código não faz muito sentido, mas se você não cria a tabela já populada quando a aplicação inicia, remova essa linha:
modelo = (DefaultTableModel) table.getModel();

Uma vez que o modelo já foi instanciado como DefaultTableModel junto com a classe.
Apesar dessa correçao não ter a ver com a pergunta inicial, do jeito que esse codigo está, nao irá exibir a tabela, e não ha necessidade de instanciar uma nova tabela ou adicionar ela ao container com scroll cada vez que for feita uma pesquisa. Então altere as linhas:
table = new JTable(modelo);
jScrollPaneCursos = new JScrollPane(table);

para:
table.setModel(modelo);

Mova a adição da tabela ou para o construtor da classe ou adicione uma validação que impeça a linha abaixo de configurar mais de uma vez a tabela como viweport do JScrollPane:
if(jScrollPaneCursos.getViewPort().getView() == null) {
    jScrollPaneCursos.setViewportView(table);
}

Também será necessário instanciar a tabela no campo respectivo:
private JTable table =  new JTable();

Para exibir dados novos na tabela não é necessário recria-la e nem adicioná-la mais de uma vez ao container. JTable em si é apenas um componente visual, quem gerencia os dados dentro dela é o model, portanto, basta alterar o model que os dados serão exibidos e modificados.
Comment: Essa alteração solucionou  a exceção nullpointer, entretanto, ainda não aparece a tabela.
Comment: Eu pretendo, que depois de acionar o botão pesquisar, a tabela apareça carregada com os registros.
Comment: @Costa.Gustavo editei a resposta, mas sugiro que da proxima pergunta sempre seja claro e objetivo na suas duvidas, fornecendo um exemplo minimo.
Comment: Obrigado Articuno, vou tentar ser mais claro da próxima vez. A paz esteja com você.
Answer: O trecho abaixo está incorreto. Primeiramente temos que instanciar o objeto table(JTable) para poder utilizar seus atributos
modelo = (DefaultTableModel) table.getModel();
        modelo.addColumn("tipoCurso");
        modelo.addColumn("nomeCurso");
        while (rs.next()) {
            modelo.addRow(new Object[]{
                rs.getString("tipoCurso"),
                rs.getString("nomeCurso")
            });
        }
        table = new JTable(modelo);

Segue abaixo o modo correto:
table = new JTable(modelo);
modelo = (DefaultTableModel) table.getModel();
        modelo.addColumn("tipoCurso");
        modelo.addColumn("nomeCurso");
        while (rs.next()) {
            modelo.addRow(new Object[]{
                rs.getString("tipoCurso"),
                rs.getString("nomeCurso")
            });
        }


Can I run 2 active polls on my site?

Question: We have a regular poll on our homepage, which I set up by selecting 'most recent poll'.
However, I want to add a different poll on a seperate page on my website. Can I do this? I can't find polls as a block or node function other than 'most recent poll'
Help?
Answer: Define an auxiliary content type (say MyPoll).
I added a Node reference CCK field to standard Poll and a CCK Node Referrers (backreference) field to MyPoll. You can then easily create one (or even more!) polls as "children" of your MyPoll node, and attach their teaser content (the voting widget) to the MyPoll node.
As MyPoll is a "standard" content type, you can do whatever you want with it in views... including the display of the referred poll widgets. It's not straightforward...
I was inspired by http://www.lullabot.com/articles/photo-galleries-views-attach. Look at it this way: "galleries" are "mypolls", and "images" are "polls".
Answer: You can also see and try this module Pollfield

Can I run 2 active polls on my site?


box-view , Uploading buffer getting error Unsupported document type

Question: view to upload a Buffer to Box. But I have been receiving the following error :
<code>{ message: 'Bad request',type: 'error',details: [ { message: 'Unsupported document type.' } ] }
</code>
my code is the following :
<code>    var boxViewLib= require('box-view');
    var boxView =boxViewLib.createClient(process.env.BOX_KEY);

    var boxOptions = {
        params:{ name:'red.jpg'}
        ,retry : true
    }
// the params.Body is a Buffer
boxView.documents.uploadFile(params.Body, boxOptions, function(err, boxData) {
                                     console.log('Upload to Box View');
                                     console.log('Err '+ err);
                                     console.log( boxData);
});
</code>
response is :
<code>Upload to Box View
Err Error: Bad request
{ message: 'Bad request',
  type: 'error',
  details: [ { message: 'Unsupported document type.' } ] }
</code>
Answer: Are you actually trying to upload a JPEG image? The View API currently only supports documents and text files (see the full list here: https://developers.box.com/view/#filetypes).
Otherwise, your code looks fine.


Extreme differences between control group and affected group

Question: I am reviewing an article and cannot be overly specific but it involves one group of people with a medical condition and another group without it; the dependent variables are various mental health issues. The problem is that the two groups are radically different on a number of relevant demographics (age, sex, race/ethnicity etc. These are related to both the condition and the outcomes). The authors tried to deal with this by adding them as covariates, but my intuition is that this is likely to fail for very large difference.  I have a vague memory of reading about this as well, but I can't remember where I read it.
Any thoughts or links appreciated.
EDIT: The sample sizes were 178 cases and 270 controls. The correlation among the covariates wasn't reported, but it can be assumed to be pretty high for some pairs (e.g. ethnic group and rural vs. urban and income).
EDIT 2: What I want to know is a) Whether the extreme differences between the groups make it impossible to properly control for them using regression and b) If so, what else could be done. I'm thinking about matching of various kinds.
Comment: The phrase Lord's paradox comes to mind. Perhaps that is what you had stashed somewhere in your cortex?
Comment: What is the sample size of each group like? How common are the covariates? Are the covariates strongly correlated?
Comment: Hi Alexis, I edited my question.
Comment: I have never heard of [Lord's paradox](https://en.wikipedia.org/wiki/Lord%27s_paradox)! Thank you, @mdewey
Comment: Thanks mdewey, but I don't think Lord's Paradox is it. This was a cross-sectional study, and Lord's paradox seems to relate only to data with two time points (or possibly more).
Comment: Is there a specific question you want answered? It's unclear how to adequately respond to your post.
Answer: There is the potential for bias due to model misspecification when using regression to adjust for confounding. Regression can work, but when the groups are dissimilar to each other, it relies heavily on extrapolation. Regression essentially estimates the missing potential outcomes from the units in the other group. If the groups differ from each other, then the regression model requires extrapolation, which often yields bias. One can rely on extrapolation, but skeptical readers should not accept an extrapolated estimate as being meaningful or useful. This is the central thesis of Ho, Imai, King, and Stuart (2007) and King and Zeng (2006). Rubin also wrote about this as long ago as 1973 in his early works on the analysis of observational studies (Rubin, 1973). You as a reviewer can say that you don't believe the results because they rely heavily on extrapolation and the regression model is subject to misspecification.
The authors should be clear about what estimand they are targeting. I describe how to make this choice in this article (still on arxiv but submitted). A simple regression of the outcome on the treatment and covariates with no interactions between them does not estimate the ATE. The ATE may be impossible to estimate without extrapolation. Instead, the authors might target the ATO, the treatment effect in the population of overlap between the groups. There are methods that directly estimate the ATO with as good precision as possible, such as overlap weighting (Li, Thomas, & Li, 2018). If these are not successful at eliminating imbalance, then the groups are too fundamentally different from each other and in principal incomparable without extrapolation.

Ho, D. E., Imai, K., King, G., & Stuart, E. A. (2007). Matching as Nonparametric Preprocessing for Reducing Model Dependence in Parametric Causal Inference. Political Analysis, 15(3), 199–236. https://doi.org/10.1093/pan/mpl013
King, G., & Zeng, L. (2006). The dangers of extreme counterfactuals. Political Analysis, 14(2), 131–159. https://doi.org/10.1093/pan/mpj004
Li, F., Thomas, L. E., & Li, F. (2018). Addressing Extreme Propensity Scores via the Overlap Weights. American Journal of Epidemiology, 188(1), 250–257. https://doi.org/10.1093/aje/kwy201
Rubin, D. B. (1973). The Use of Matched Sampling and Regression Adjustment to Remove Bias in Observational Studies. Biometrics, 29(1), 185–203. https://doi.org/10.2307/2529685


How to understand this new phase format table in cricket world cup 2019?

Question: Recently, cricket world cup 2019 live TV channels are showing this new phase format table:

Overs
India
Best performance
New Zealand

1-10

Bumrah - 2/64

10-20

Kane Williamson - 52

20-30

30-40

40-50

Phases Won

I am confused. What is this table, and how do I understand it?
Answer: It is similar to sessions in test cricket, but here 10 overs are considered to be one session or a phase. The team dominant throughout the session wins it, mentioned in the column below the team's name and notable contributors of that session are mention in the third column (Bumrah and Williamson in the above picture). It is used to analyse the progress of the game and for comparison of both the teams performances at different times during the game.
Answer: Yes,the phases are similar to sessions in Test cricket.in one day match 10 overs are considered as a session.hence five sessions in one match.The team which dominates respective phase is the winner of that phase.The player who performs best in respective phase is the best performer of that phase.Like Bumrah in 1-10 overs and Williamson in 10-20 overs.


React - carousel

Question: I'm creating my own carousel and I want to add circles navigation on the bottom of slider. I am fetching data from mongoDb (3 pictures) and load them in App component and passing via props to Carousel component.
I want to map trough array and set index to data-slider property and later read this with e.target.dataset.slider and change in method changeSlider() to this value in data-slider property.
I have weird problem, when I click on this buttons circles sometimes I have value === 2, 0, 1 but sometimes I'm getting undefined and my slider don't know which slider make active.

          {this.props.images.map((el, index) => {
            return (
              <button
                key={index}
                onClick={this.setActiveSlide}
                className="circle-empty"
                data-slider={index}
              >

              </button>
            );
          })}

Method:
setActiveSlide = e => {
    let slider = e.target.dataset.slider;
    this.setState({
      slider: slider
    });
  };

Call setActiveSlide method onClick gave me this result:
Comment: It's because you have an `i` tag, and if you click this instead of the button the event the target will equal this.  Try using `currentTarget`, this is the target of the Event you attach the onClick too, instead of the target you clicked.
Comment: Works fine for me: https://codesandbox.io/s/p73pnxjwpj
Comment: @ChrisG no, it still doesn't work.. correctly..
Comment: @Keith Right, I only tested this on Firefox. Chrome behaves differently.
Comment: @ChrisG  That's interesting, I assume it's because of this -> https://bugzilla.mozilla.org/show_bug.cgi?id=1089326    Only been open for 4 years now, but looks like Firefox will soon behave the same as Chrome / Edge.
Answer: Your event is probably firing from the icon sometimes simply change the event target to currentTarget
setActiveSlide = e => {
  // I've changed this variable to const because our data-slider is not
  // going to change.
  const slider = e.currentTarget.dataset.slider;
  // Using es6 object property shorthand if we have both
  // the same variable name and field we can simply pass in just the
  // field name and it will auto convert it for us
  this.setState({
    slider
  });
};

React - carousel


JQuery 2 runat:server

Question: I am trying to get mouse coordinated from asp.net page in to run at server "TextBox".
jQuery(document).ready(function () {
    $(document).mousemove(function (e) {
        $('#status').html(e.pageX + ', ' + e.pageY);
    });
})

Working

0, 0

Needed
<asp:TextBox ID="status" runat="server"></asp:TextBox>

And I can't understand where the error is and why is not working.
Comment: your Jquery code is correct but when ur textbox control is render by server so ur control id is change so jquery not able to find ur textbox ..
Answer: use a cssClass or dont use a ASP.net controller for it
<asp:TextBox ID="status" runat="server" cssClass="status" />

and use a class selector (and set its value, not html):
jQuery(document).ready(function () {
    $(document).mousemove(function (e) {
        $('.status').val(e.pageX + ', ' + e.pageY);
    });
});

Answer: You better add ClientIDMode="Static" to your asp.net control so that the ID will not change on run time
<asp:TextBox ID="status" ClientIDMode="Static" runat="server"></asp:TextBox>

Answer: you have to use a ClientID because ur control id change when it render by server
jQuery(document).ready(function () {
    $(document).mousemove(function (e) {
        $('#'+'<%=status.ClientID %>').html(e.pageX + ', ' + e.pageY);
    });
})

JQuery 2 runat:server


Multiple choices select with data module created view

Question: on my website I have a custom table, and I used the data module to create a view that uses the entries in that table.
I have created an exposed filter for one of the columns. Since I needed a select form and only search box was available, I used this code:
function mymodule_form_views_exposed_form_alter(&$form, $form_state) {

$form['myfield']['#type'] = 'select';
$form['myfield']['#multiple'] = true;
$form['myfield']['#default_value'] = array("" => "All");
$form['myfield']['#options'] = $values;
$form['myfield']['#attributes'] = array('size' => 10);

}
The problem is that when I select multiple values, it performs an "AND", but I need an "OR".
How can I procede?
Thanks!
Answer: *markus_petrux* answered, and josepvalis gave a helpful example of this in Drupal Groups in 2010.
You can use hook_views_pre_view to alter the filter to use 'OR'. Trimming josepvalis's example, try redefining the filter & applying that:
function my_module_views_pre_view(&$view) {
    $new_view_filters['field_multiselect_value'] = array(
        'id' => 'field_multiselect_value',
        'table' => 'my_table',
        'field' => 'my_column',
        'operator' => 'or',
        'value' => $array_of_values,
        'group' => '0',
        'exposed' => FALSE,
        'relationship' => 'none',
    );
    $view_overrides = array();
    $view_overrides['filters'] = $new_view_filters;
    foreach ($view_overrides as $option => $definition) {
        $view->display_handler->override_option($option, $definition);
    }
}

Comment: Thanks! In the end I couldn't make your solution work, but starting from there I found something that worked. It's probably a bit hacky, but I used a hook_views_query_alter and I set the where condition operator to 'IN'.
Comment: Hmmm, would you mind posting your solution, please? You may have the better answer.
Comment: Here's the code: http://pastebin.com/k2R1fy32 . Sorry, I couldn't format the code here in the comment section. It's very hacky, since I'm targeting specific array indexes... basically I'm just setting the operator to 'IN'. Then I'm also unsetting the condition if the value is set to empty string to have a "SHOW ALL" option. It's horrible, but I needed something quick.
Comment: I have updated the code a bit, much less ugly: http://pastebin.com/FXzMKxe6