How do we pass php code as a twig variable in global custom text field of views

Question: How can we pass this php code as a twig variable in global custom text in views without using views php module?
I have created a view of news content with masonary layout .
<?php
$language = \Drupal::languageManager()->getCurrentLanguage()->getId();
$alias = \Drupal::service('path.alias_manager')->getAliasByPath('/node /'.$row->nid);
echo $language.$alias;
?>

I just want to render it as a twig variable in custom text field of views without creating template because i have already created a page long ago i dont want to rework by creating template.
Answer: To place PHP code in a field extend a field plugin and override render(). But in your case you should get the result of this code from the existing EntityLink plugin (in UI "Link to Content") and the option output_url_as_text. If you want to use it as Twig variable in another field, hide the entity link field from display and place it in front of the other field.
Comment: actually i am using multilingual website in which link to content field navigate to native language rather than respective language, for this i am trying to write custom code for creating link to content which is language based its working fine in views php but my client restricted me not to use views php
Comment: Then place the PHP code in `render()` of a field plugin. You can generate one with Drupal Console: `drupal generate:plugin:views:field`
Comment: can you provide some reference link i am not able to understand what you saying
Comment: When you run this console command with the default options (you only need to specify an existing custom module) and clear the cache you'll find a new field with the description "My awesome custom views field plugin." in Views UI. Then put the PHP code in render() of the generated code.


Mounting a drive via systemd unit file on CoreOS unsuccessful

Question: When I'm trying to mount a formatted drive via a systemd unit (from my cloud_config):
- name: var-lib-ceph-osd-ceph\x2d1.mount
  command: start
  content: |
    [Unit]
    Description=Mount Ceph disk (/dev/sdb) to /var/lib/ceph/osd/ceph-${host.id}
    Before=docker.service
    [Mount]
    What=/dev/sdb
    Where=/var/lib/ceph/osd/ceph-1
    Type=btrfs
    User=64045
    Group=64045

but unfortunately if fails with a status=217/USER error.
$ systemctl status "var-lib-ceph-osd-ceph\\x2d1.mount"
â var-lib-ceph-osd-ceph\x2d1.mount - Mount Ceph disk (/dev/sdb) to /var/lib/ceph/osd/ceph-1
   Loaded: loaded (/etc/systemd/system/var-lib-ceph-osd-ceph\x2d1.mount; static; vendor preset: disabled)
   Active: failed (Result: exit-code) since Fri 2016-01-22 12:32:04 UTC; 33s ago
    Where: /var/lib/ceph/osd/ceph-1
     What: /dev/sdb
  Process: 833 ExecMount=/bin/mount /dev/sdb /var/lib/ceph/osd/ceph-1 (code=exited, status=217/USER)

$ sudo /bin/mount /dev/sdb /var/lib/ceph/osd/ceph-1

$ ls -la /var/lib/ceph/osd/ceph-1
total 5242936
drwxr-xr-x 1 64045 64045        238 Jan  6 10:53 .
drwxr-xr-x 3 64045 64045       4096 Jan  6 10:41 ..
drwxr-xr-x 1 root  root           0 Jan  6 10:49 async_snap_test
-rw-r--r-- 1 64045 64045         37 Jan  6 09:43 ceph_fsid
drwxr-xr-x 1 64045 64045      49782 Jan  7 12:14 current
-rw-r--r-- 1 64045 64045         37 Jan  6 09:43 fsid
-rw-r--r-- 1 64045 64045 5368709120 Jan 21 07:56 journal
-rw------- 1 64045 64045         56 Jan  6 09:43 keyring
-rw-r--r-- 1 64045 64045         21 Jan  6 09:43 magic
-rw-r--r-- 1 64045 64045          6 Jan  6 09:43 ready
drwxr-xr-x 1 64045 64045         26 Jan  6 09:43 snap_1
-rw-r--r-- 1 64045 64045          4 Jan  6 09:43 store_version
-rw-r--r-- 1 64045 64045         53 Jan  6 09:43 superblock
drwxr-xr-x 1 root  root           0 Jan  6 10:49 sync_snap_test
drwxr-xr-x 1 root  root           0 Jan  6 10:49 test_subvol
-rw-r--r-- 1 64045 64045          2 Jan  6 09:43 whoami

If I execute the mount command manually, it works without problem. Somebody got an idea wha't wrong? Google hasn't got much to offer unfortunately...
Answer: Turns out one is probably not allowed to use
User=64045
Group=64045

Once I removed this, I was successfully able to start the unit.


is Pareto distribution exponential dispersion family and the form is unique?

Question: I am trying to show that Pareto distribution with $$f(y;\alpha)=\alpha y^{-\alpha-1} $$ is exponential dispersion (ED) family which means that it can be rewritten as:
$$f(y;\theta,\phi)= \exp\left\{\frac {y\theta-b(\theta)} {a(\phi)} + c(y;\phi)\right\}$$ with $E[Y]=b^{\prime}(\theta)$ and $Var[Y]=b^{\prime\prime}(\theta)a(\phi).$
What I found is there are multiple ways to rewrite the PDF in the exponential dispersion family form, but since I know that $$E[Y]=\frac {\alpha} {\alpha-1}$$ and $$Var[Y]=\frac {\alpha} {(\alpha-1)^2(\alpha-2)},$$ I can solve the differential equations to get $$b(\theta)=\alpha + {\rm log}(\alpha-1)$$ and $$a(\phi)=\frac {-\alpha} {\alpha-2}.$$ This suggests that the form of $f(y;\theta,\phi)$ is unique.
So, is the ED family form of the PDF for Pareto distribution unique? If so, what is the correct form?
Comment: Your question is confusing as (a) you use three different notations for the parameter, ie $\alpha,\theta,\phi$ and (b) you use the same notation for $y$ in the Pareto and $y$ in the exponential family. Since $f(y;\alpha)=\exp\{\log(y)(-\alpha-1)+\log(\alpha)\}$, you get the natural representation. Note also that you are missing the indicator function in the Pareto density.
Comment: You still didn't fix the missing indicator in the density.  We need to know the range!
Comment: Thanks! (a) I forgot to mention that this is a reparameterization, we can find $\theta$ and $\phi$ in terms of $\alpha$ (b) yes, this is the natural form for exponential family, but not in the form of ED as I illustrated above. Yes, I skip the indicator function $I(Y>1)$.
Comment: Have you found the solution? Because, me either, I'm interested in showing that the GPD comes from EDM and I can't find a solution
Answer: You have given a Pareto density with a fixed lower limit of range 1,
$$ f(y; \alpha)= \alpha y^{-(\alpha+1)}, \quad y \ge 1
$$
Taking logarithms we can write this in exponential family form
$$f(y; \alpha)= \exp\left\{ -(\alpha+1)\log y + \log\alpha  \right\}
$$ which does not have any dispersion parameter, but introducing $\phi\equiv 1$ we can recognize the exponential dispersion family form as
$$f(y; \alpha)= \exp\left\{ -\frac{(\alpha+1)\log y}{\phi} + \log\alpha  \right\}
$$
so the answer is yes, the Pareto family with known lower limit of 1 is an exponential dispersion family with canonical statistic $\log y$. Note that $\log y$ does have an exponential distribution, see Distribution of the exponential of an exponentially distributed random variable?
And, yes, this form is unique.


Shared memory: shmget fails: No space left on device -- how to increase limits?

Question: I call shmget many times in my program, to get shm of of average size 85840 bytes. I obtain about 32771 shm ok, then shmget doesn't return a shm, but the error: "No space left on device".
I have increased the kernel limits to:
$ sysctl -A|grep shm
kernel.shmmax = 33554432
kernel.shmall = 1677721600
kernel.shmmni = 409600

But still get the issue. Why?
Do I have to put something into /etc/security/limits.conf too? I only have
"user             -       nofile          1000000"
because the program also opens about as many files as shms.
This is the output of free
$ free
          total       used       free     shared    buffers     cached
Mem:       8150236    7261676     888560          0     488100    3270792
-/+ buffers/cache:    3502784    4647452
Swap:     12287992     554692   11733300

And ipcs
$ ipcs -lm

------ Shared Memory Limits --------
max number of segments = 409600
max seg size (kbytes) = 1638400
max total shared memory (kbytes) = 6710886400
min seg size (bytes) = 1

Since I assume shm is capable of being swapped out, there should be enough space.
Comment: IPC_PRIVATE is in use with flags IPC_CREAT | IPC_EXCL | 0700
Comment: You did reload the kernel parameters after modifying sysctl.conf by doing something like `sysctl -p`, right?
Comment: I set them using sysctl -w and verified as with the command shown
Answer: Use ipcs -l to check the limits actually in force, and ipcs -a and ipcs -m to see what is in use, so you can compare the output. Look at the nattch column: are there segments with no processes attached that were not removed when processes exited (which normally means the program crashed)? ipcrm can clear them, although if this is a test machine, a reboot is quicker (and will make sure your changes to limits are picked up).
Your kernel parameters seem odd. In particular, shmall is a count of pages, not bytes, and 4kB is the default page size (run getconf PAGESIZE to check what you are using). How many terabytes of RAM do you have?
Now, you say you get about 32771 shared memory segments, which is also about 32768 (or 2 to the 15) which suggests a signed 16-bit int is the limiting factor. And what kernel are you running (as this will have its own limits)? The two may be related.
Comment: Setting SHMMAX to something huge is fairly common practice. Oracle, for example, recommends settings it to 4294967296 on 32-bit systems, iirc, which is a lot more than the 1677721600 above. It doesn't hurt to set SHMALL and SHMMAX to values that are higher than the available amount of memory. The value Oracle recommends for SHMALL is 1073741824, which, in pages of 4KiB, is the same as recommended for SHMMAX.
Comment: PAGESIZE is 4kb, I use Linux 3.0.22. Interesting point about the base2 coincidence, I'll investigate on that. Thanks @ramruma.
Comment: I added the output of ipcs -l, I don't think that's the problem.
Comment: Wouldn't a limiting factor involving a signed int always mean that the amount of shared segments will be *lower* than 32768? Or am I misunderstanding you?
Comment: Oracle has always had the SHMMAX recommended setting wrong.
Answer: It turns out shmmni is limited to 32768 in the kernel:
#define IPCMNI 32768  /* <= MAX_INT limit for ipc arrays (including sysctl changes) */

in the file  ...version.../include/linux/ipc.h.
So short of recompiling the kernel, that is the hard limit on the number of shared memory segments.
Comment: That seems to be it. I'll try recompiling the kernel
Comment: Here, they talk about that it's the limit, http://www.tebyan.net/index.aspx?pid=31159&BookID=23749&PageIndex=136&Language=3 and that you can basically just change it: http://us.generation-nt.com/answer/patch-1-3-ipc-rename-ipcmni-ipcmni-max-help-198345051.html I think kernel-developers are just afraid of carrying a too-large array around.
Comment: Hi I have same problem as this. How to fix it? I don't understand above solution. It means modify file ipc.ch and then recompile kernel? instruction in detail please. thanks for help.
Answer: As shmget() allocates a new shared memory segment, and you seem to use a lot of them (considering your limits.conf), isn't possible you are using too many shared memory segments? I do not have a lot of experience with calling shmget(), but it seems to me the number of possible open files (1000000) is larger than the amount of allowed shared memory segments (SHMMNI), which is 409600.
Answer: Just do:
sysctl -a | grep shm

and find your kernel.shmmni entry. Double that entry value in your /etc/sysctl.conf.


iOS tableview scrolling problems

Question: I have two views in a viewcontroller. A tableview is embedded inside a bottom view and segue is immediately triggered to that tableview. So I'm showing a static view on top of the screen and a tableview on the bottom. The problem is that scrolling in the tableview is unresponsive. It registers taps only on the lower half. Basically there's an invisible line in the middle of the tableview and the app doesn't register anything above it (it registers only those taps which hit the first view I mentioned which is much higher than the tableview) So the user is only able to scroll if he touches only the bottom half of the tableview. Tableview functions correctly and displays valid data though

Any ideas?
Answer: Did you check the View Hierarchy Debugger (Debug->View Debugging->Capture View Hierarchy)? Typical issues to look for are:

Transparent views above your tableView that gobble up touch events.To fix this, do one of the following:

Remove the offending transparent view
Fix the view hierarchy by moving the offending transparent view behind the tableView
Fix the layout to make sure the transparent view's bottom anchor is not below the tableView's top anchor

The table view extends above its superview without being clipped. Touch events are received at the root of the view hierarchy and forwarded to the appropriate subview. In this case touch events outside the bounds of the bottom view will not be sent to the bottom view. That's why the bottom view can only forward touch events that originate within its bounds. To fix this, do one of the following:

Enable clipsToBounds on the container view (of course, this will just hide the part of the table that is not interactive)
Fix the layout to make sure the tableView is fully within the bounds of its superview

iOS tableview scrolling problems


Determining solution curve of ODE

Question: I have been introduced to autonomous ODE's and have recently come across the notion of a non-autonomous differential equation. After some reading, I came across this logistic model,
$$\frac{dx}{dt}=x(a(t)-b(t)x), \ \ \ \ x(s)=x_0.$$
Where $a(t),b(t)>0$.
And apparently, this logistic equation has 'relatively straightforward' asymptotic behaviour since there exists an explicit solution. My question is, how would you go about finding this explicit solution? All of the variables in this equation are dependent on time. It doesn't seem to be separable either. A hint would be greatly appreciated, just to get me started at finding a solution $\space x(t) \space$ to the above equation.
Thank you in advance!
Comment: What do you know about the functions $a$ and $b$? Any conditions on them? You could possibly work your way to obtaining a bound on $x(t)$ using Gronwall's lemma, which would give you some information as to how $x$ behaves.
Comment: I have made some changes to the question. Wolfram Alpha suggests that the solution comes in the form; $$x(t)=\frac{e^{\int_1^t a(\zeta) d \zeta}}{c_1+\int_1^t b(\sigma)\left( e^{\int_1^\sigma a(\zeta)d\zeta} \right)d\sigma}$$
Answer: Note: I'm going to use a dot to denote differentiation w.r.t $t$.
First expand and move the linear $x$ term to the L.H.S:
$$\dot{x}-a(t)x=b(t)x^2$$
Make the substitution $s=x^{-1}$:
$$\dot{x}-\frac{a(t)}{s}=\frac{b(t)}{s^2}$$
Notice that $x=\frac{1}{s}$, therefore $\dot{x}=\frac{-\dot{s}}{s^2}$. So
$$\frac{-\dot{s}}{s^2}-\frac{a(t)}{s}=\frac{b(t)}{s^2}$$
Multiply through by $-s^2$:
$$\dot{s}+a(t)s=-b(t)$$
Now define $u=\exp\left(\int a(t)\mathrm{d}t\right)$ and multiply through:
$$u\dot{s}+u\cdot a(t)s=-u\cdot b(t)$$
Now notice that
$$\dot{u}=\exp\left(\int a(t)\mathrm{d}t\right)\frac{\mathrm{d}}{\mathrm{d}t}\left(\int a(t)\mathrm{d}t\right)=a(t)u$$
Therefore the differential equation can be stated as
$$u\dot{s}+\dot{u}s=-u\cdot b(t)$$
Notice that $u\dot{s}+\dot{u}s=\frac{\mathrm{d}}{\mathrm{d}t}(u\cdot s)$:
$$\frac{\mathrm{d}}{\mathrm{d}t}(u\cdot s)=-u\cdot b(t)$$
$$s=\frac{-1}{u}\int u\cdot b(t)\mathrm{d}t$$
Now since $s=1/x$,
$$x(t)=\frac{-\exp\left(\int a(t)\mathrm{d}t\right)}{\int \exp\left(\int a(t)\mathrm{d}t\right)\cdot b(t)\mathrm{d}t}$$
Which should line up with Wolfram's output. Suggested reading: https://en.wikipedia.org/wiki/Bernoulli_differential_equation
Comment: Just for clarification, is the initial condition $x(s)=x_0$ only come into effect when evaluating the respective integrals contained within the solution $x(t)$? Also, the wolfram alpha solution includes a $+c$ on the denominator, similarly doe this only come into effect after evaluating the given integrals?
Comment: Yes the arbitrary constant comes from the fact that the integrals in my answer are **in**definite.
Answer: It is perhaps noteworthy to remark that for $x_0=0$ the unique solution is $x(t)=0$ for all $t$. This implies that when $x_0>0$ (or $x_0<0$) any maximal solution will stay non-zero. Thus the change of coordinates $z=1/x$ is legitimate in such a case:
$$ \dot{z}=-\dot{x}/x^2= -1/x(a_t-b_t x) = -z a_t + b_t$$
leading to $$ \dot{z} + a_t z = b_t$$
which may be solved using standard methods. When $a$ and $b$ are defined for all $t$ then so is the flow for the latter ode. It may, however, cross $z=0$ corresponding to $x$ passing through infinity in a regular way on the projective line.

Determining solution curve of ODE


Algoritmo para criação de aposta na Lotofácil

Question: Uma das formas de apostar na lotofácil é escolher 15 números do número 1 à 25. Gostaria de um algoritmo em c# que gerasse apostas. Lembrando que em cada combinação os números não podem se repetir entre si e as apostas não devem se repetir.
Já fiz alguma coisa mas não ficou bom. A minha estratégia foi de criar um arraylist de 15 números, para cada posição gerar um número ramdômico de 1 a 25 e verificar se esse número estava em alguma outra posição, no final eu ordenava os números e verificava se existia alguma outra aposta com os mesmos números, caso não existisse eu armazenava.Gostaria de conhecer novas formas de pensar.
Comment: Qual é a dúvida? Você quer escrever um programa que escolha conjuntos de 15 números aleatórios sem repetir? Já tentou algo? O que deu errado na sua implementação?
Comment: Nesta comunidade, nós ajudamos a resolver problemas com código e não trabalhamos de graça. Por favor, mostre-nos o que você tentou e está com dificuldade. Se não sabe por onde começar, mostre o que pesquisou e o que tem até agora. Não se ofenda, não é nada pessoal. É apenas como esta comunidade é. Se tiver dúvidas, por favor, leia a [faq](http://pt.stackoverflow.com/faq).
Comment: Se for pensar nesse sentido, "trabalhar de graça", a comunidade passa a não fazer sentido. Todo a dúvida que é solucionada (via código ou não) no fim das contas alguém trabalhou para solucioná-la (usou sua experiência ou conhecimento).
Comment: Douglas, ao invés de criar um comentário, quando você quiser adicionar detalhes ou informações à sua pergunta **edite-a**. Assim ninguém vai perder o contexto extra por não ter lido o comentário. Dessa vez eu fiz pra você, mas tente sempre lembrar disso.
Answer: Eu faria da seguinte maneira.
Solução 1

Criaria uma lista de 25 posições numerados de 1 a 25
Faria varios embaralhamentos na lista
Pegaria os 15 primeiros elementos
Verificaria se já n tenho estes 15 números
Repetiria o processo para o numero de vezes necessário

Solução 2

Geraria todas as 3.268.760
Escolheria randomicamente quantas eu preciso

A escolha entre cada uma das soluções depende da escolha espaço/tempo.
Caso precise do algoritmo aqui tem a base teórica e varias implementações inclusive C#.
Comment: Eu pensei nas 3.268.760, sei que tem uma solução matemática (binomial não sei do que), o problema é que não achei o algoritmo para gerar as combinações.
Comment: Foi na mosca! Muito obrigado!
Comment: Recomendo que na Solução numero 2, use uma estrutura de dados adequada, por exemplo uma arvore, para o acesso aos dados ser rapido.
Comment: @Urbester: gerar todas e randomizar demora um pouco, como demonstro na minha resposta. Aqui leva uns 5 segundos, para randomizar 3.268.760 de valores inteiros.
Answer: Dado um algoritmo capaz de pegar a n-ésima combinação, basta você usar números inteiros aleatórios para obter os jogos.
Classe para obter a n-ésima combinação
Fiz uma classe capaz de obter a n-ésima combinação. Basta instanciar ela, passando o número de elementos a serem retornados (no caso 15), e um array com todos os elementos possíveis (arra de números de 1 até 25). Depois de criada chame o método PegarCombinacao passando o índice da combinação, sendo 0 a primeira possível e 3268759 a última possível.
Garantias de velocidade
A vantagem na velocidade está exatamente no fato de representarmos qualquer um dos jogos possíveis, simplesmente como sendo um int... sendo que selecionar números inteiros aleatoriamente é muito rápido.
Além disso, a classe de pegar a n-ésima combinação, é feita sem iterar sobre todas as combinações. Eu uso um algoritmo baseado em somatórios para achar a exata combinação, sem ter de iterar. Para 15 elementos a serem retornados, o máximo de iterações deste algoritmo é 15 * 15 no pior caso, e 1 * 1 no melhor caso.
Código da classe
public class Combinador<T>
{
    private readonly int _cnt;
    private readonly T[] _items;
    private readonly List<int[]> _somatorios;

    public Combinador(int cnt, T[] items)
    {
        _cnt = cnt;
        _items = items;
        var line0 = new[] { 1 }.Concat(Enumerable.Repeat(0, cnt)).ToArray();
        var lines = new List<int[]>(cnt) { line0 };
        for (int itLine = 1; itLine <= cnt; itLine++)
        {
            var prevLine = lines[itLine - 1];
            var newLine = new int[line0.Length];
            for (int itCol = 0; itCol < newLine.Length; itCol++)
                newLine[itCol] = (itCol > 0 ? newLine[itCol - 1] : 0) + prevLine[itCol];
            lines.Add(newLine);
        }
        _somatorios = lines;
    }

    public T[] PegarCombinacao(int seed)
    {
        return GerarIndices(_somatorios, _items.Length - _cnt, _cnt, seed)
            .Select(i => _items[i])
            .ToArray();
    }

    private static IEnumerable<int> GerarIndices(List<int[]> lines, int fs, int ts, int num)
    {
        if (ts <= 0) yield break;
        var line = lines[ts];
        var min = 0;
        for (int itFs = 0; itFs <= fs; itFs++)
        {
            var max = min + line[itFs];
            if (num < max)
            {
                var num2 = num - min;
                yield return fs - itFs;
                foreach (var idx in GerarIndices(lines, itFs, ts - 1, num2))
                    yield return fs - itFs + idx + 1;
                yield break;
            }
            min = max;
        }

        throw new Exception("O parâmetro deve ser menor que " + min);
    }
}

Exemplo de uso
Leia os comentários, eles são muito importantes.
var comb = new Combinador<int>(15, Enumerable.Range(1, 25).ToArray());
var rnd = new Random();

// Criando os jogos de 1 a 1 e colocando num HashSet até ter N elementos
// esse método é rápido para poucas combinações, e vai ficando mais lento
// tendendo a infinito quando o número se aproxima da quantidade máxima.
// Mesmo assim, essa técnica é a recomendável para até 3 milhões de jogos.
// Use para 0% até 95% de todas as alternativas.
var numeros = new HashSet<int>();
int tentativas = 0;
while (numeros.Count < 200000)
{
    numeros.Add(rnd.Next(3268760));
    tentativas++;
}
var jogosAleatorios2 = numeros
    .Select(comb.PegarCombinacao)
    .ToArray();

// Criando todos os jogos e pegando N aleatórios
// esse método é lento, e não recomendo se não for
// gerar todos os jogos ou algo muito próximo de todos.
// Use somente para 95% até 100% de todas as alternativas.
var jogosAleatorios1 = Enumerable.Range(0, 3268760)
    .OrderBy(a => rnd.Next())
    .Select(comb.PegarCombinacao)
    .Take(100000)
    .ToArray();

As recomendações que fiz nos comentários são baseadas em testes executados na minha máquina, pode ser que na sua os resultados sejam um pouco diferentes.
Answer: Já que eu estou tranquilo, eu desenvolvi uma rotina que gera esses "jogos" aleatoriamente.
Deem uma olhada:
  public List<int>[] GeraJogos(int numeroDeJogos)
    {
        List<int>[] jogos = new List<int>[numeroDeJogos];
        int i = 0;

        while (i < numeroDeJogos)
        {
            var lotoFacil = new List<int>();
            lotoFacil = LotoFacil();

            if (jogos.Contains(lotoFacil))
            {
                continue;
            }

            jogos[i] = lotoFacil;
            i++;
        }

        return jogos;
    }

    public List<int> LotoFacil()
    {
        var resultado = new List<int>();

        var randow = new Random();
        int randowNumber;
        int i = 0;

        while (i < 15)
        {
            randowNumber = randow.Next(1, 25);

            if (resultado.Contains(randowNumber))
            {
                continue;
            }

            resultado.Add(randowNumber);
            i++;
        }

        return resultado;
    }

Comment: Muito bom Matheus! Sua ideia é muito boa!


How do I enable WebDAV write access in WebSphere Portal 8.0?

Question: As an administrator in WebSphere Portal 8.0, I can access and modify theme-related files with a WebDAV client connected to http://servername:10040/wps/mycontenthandler/dav/fs-type1.
I now want to give another user write access to the same files, but this user shouldn't be a Portal administrator.
IBM documentation states (on a page called Using WebDAV file store):

"To give users write access to resources contained in these folders, assign the users MANAGER role on the virtual resource THEME MANAGEMENT in portal access control. Managers can create, modify, or delete such resources."

I have done this successfully in the past with Portal 7.0, but it doesn't work in Portal 8.0.
When the user tries to modify a file through Portal 8.0 WebDAV, the operation fails and the server's SystemOut.log file contains this message:
com.ibm.wps.logging.ExceptionLogger logThrowable(th) An exception occurred: [null].
Enable traces for [com.ibm.wps.logging.ExceptionLogger=all] to see the exception stack trace.
If I enable the suggested trace and the user tries again, the server's trace.log file contains a stack trace like the following (though I've removed many lines to make it shorter):
com.ibm.wps.logging.ExceptionLogger logThrowable(th) Exception
                                 com.ibm.wps.resolver.servlet.exceptions.ModelIOException
    at com.ibm.wps.resolver.webdav.tree.WebdavTreeModelUpdateDataSink.read(WebdavTreeModelUpdateDataSink.java:182)
    at com.ibm.wps.resolver.servlet.ContentHandlerUpload.handleDownloadAndDispose(ContentHandlerUpload.java:817)
    at com.ibm.wps.resolver.servlet.ContentHandlerUpload.handleUpload(ContentHandlerUpload.java:1020)
    at com.ibm.wps.resolver.servlet.ContentHandlerUpload.doFilter(ContentHandlerUpload.java:378)
    ...
    at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1700)
Caused by: com.ibm.wps.resolver.webdav.admin.exception.WebdavCannotCommitException
    at com.ibm.wps.filestore.JCRWebdavTreeModelController.commit(JCRWebdavTreeModelController.java:354)
    at com.ibm.wps.resolver.webdav.tree.AbstractWebdavTreeModelCreateUpdateDataSink.updateItem(AbstractWebdavTreeModelCreateUpdateDataSink.java:1028)
    at com.ibm.wps.resolver.webdav.tree.AbstractWebdavTreeModelCreateUpdateDataSink.updateItem(AbstractWebdavTreeModelCreateUpdateDataSink.java:955)
    at com.ibm.wps.resolver.webdav.tree.WebdavTreeModelUpdateDataSink.read(WebdavTreeModelUpdateDataSink.java:157)
    ... 80 more
Caused by: javax.jcr.access.AccessDeniedException: ACC3501E: The requested operation violates one or more access constraints on path:
/filestore/fs-type1/themes/ISWPortal8.0/ISWExt/css/isw-style-ie6.css/jcr:content/icm:lastModified or descendant.
    at com.ibm.icm.jcr.PropertyImpl.checkSetValueAllowed(PropertyImpl.java:444)
    at com.ibm.icm.jcr.PropertyImpl.setValue(PropertyImpl.java:310)
    at com.ibm.icm.jcr.NodeImpl.setProperty(NodeImpl.java:3990)
    at com.ibm.icm.jcr.NodeImpl.updateSaveProperties(NodeImpl.java:3176)
    at com.ibm.icm.jcr.NodeImpl.save(NodeImpl.java:2825)
    ...
    at com.ibm.icm.jcr.WorkspaceImpl.save(WorkspaceImpl.java:2776)
    at com.ibm.icm.jcr.WorkspaceImpl.save(WorkspaceImpl.java:2765)
    at com.ibm.wps.filestore.JCRWebdavTreeModelController.commit(JCRWebdavTreeModelController.java:348)
    ... 83 more

How do I allow non-administrator users to modify these files?
Answer: Depending on your usage there are two items to look at.
General the access is limited to administrator groups which is documented here at Portal IC. It is possible to open up certain areas for end user specific usage, but I guess this is not what you are looking for.
Based on the trace you want to have the user be able to modify the WebDav for Themes. This is controlled different by a Virtual Resource of WebSphere Portal.
You need to grant access to the Virtual Resource Markups for the group of users you want to enable write access to the themes folder
Comment: Thanks for your answer. I won't have time to check whether this works for several days at least, as I'm busy on other projects.


Complementarity and Substitutability

Question: I am reading a paper in the international journal of game theory entitled Unequal Connections by Goyal and Joshi (2006) and it has the following sentences: "If strategic complementarity obtains... In contrast, if substitutability holds..." I am not familiar with the terms "complementarity" and "substitutability" and a google search only turns up more articles which use the terms. What is the intuitive and/or mathematically rigorous definition for the two terms?
Comment: See [strategic complementarity](https://en.wikipedia.org/wiki/Strategic_complements).
Comment: For *substitutability* see also [Game theory intro](https://www.cs.ubc.ca/~kevinlb/teaching/cs532a%20-%202006-7/lectures/lect3.pdf), slide 10.
Answer: You can look up the mathematical definitions in any game theory text book.
The interpretation is basically that the players' actions reinforce/offset (complement/substitute) each other.
For example, if a buyer is more likely to buy a good at some price, when it is more likely that all other buyers are taking that price, then buyers are strategic complements.
Alternatively, consider two firms. For some model parameters, it could be that one firm wants to produce less when the other firm produces more. Then, the firms are strategic substitutes.
Mathematically, you are interested in the sign of the cross derivative of the payoff function with respect to the players actions.

Complementarity and Substitutability


Angular: Ligar vários campos a um reactive form

Question: O problema é que possuo vários select multiple e quando seleciono algumas opções de um select elas são adicionadas ao ReactiveForm corretamente, porém quando eu seleciono alguma opção de outro select elas são sobre escrevidas, preciso que não se sobreescrevam. Eles são gerados automaticamente.
this.form = this.builder.group({
  id: [],
  permissoes: ([{}]),
}, {});

    <select class="form-control" formControlName="permissoes" [compareWith]="compareFn" multiple>
        <option *ngFor="let permissao of permissoes" [ngValue]="permissao">{{permissao.nome}}</option>
    </select>

Todos os selects são iguais, é possivel eu juntar todos os selects neste array de permissões ?
Comment: Olá @Eduardo, bem vindo ao **SOpt**. Aproveite para fazer o [Tour](https://pt.stackoverflow.com/tour) para conhecer melhor o funcionamento do site.
Comment: Questão relacionada [nessa resposta](https://pt.stackoverflow.com/a/328082/4272)
Answer: Pra cada select ou input que vc tiver vc tem que adicionar uma linha correspondente quando criar seu form. E atraves do campo formControlName vc mappeia cada select/input pro seu respectivo controle.
this.form = this.builder.group({
  id: [],
  permissoes: ([{}]),
  outroSelect: ([{}]),
});

    <select class="form-control" formControlName="permissoes" [compareWith]="compareFn" multiple>
        <option *ngFor="let permissao of permissoes" [ngValue]="permissao">{{permissao.nome}}</option>
    </select>

        <select class="form-control" formControlName="outroSelect" [compareWith]="compareFn" multiple>
            <option *ngFor="let outraCoisa of outroSelect" [ngValue]="outraCoisa ">{{outraCoisa .nome}}</option>
        </select>

Comment: Esqueci de mencionar que eles são gerados automaticamente do banco de dados.
Comment: Como assim? Vc pode usar o formArray nesse caso talvez então.


If you need to open ports from DMZ to LAN, at what point is the separation no longer worth it?

Question: If you have a DMZ with one or more servers/services (maybe FTP, HTTP en SMTP). And you have a LAN with typical serversservices (like file sharing, Active Directory, database server).
Depending on the integration of services and resources, the firewall between the DMZ and LAN could have a lot of open ports. At what point would you consider not using a DMZ?
Thanks!
Answer: There is no realistic measurement in which I would consider not using a DMZ.  With security, every extra bit you can get helps. The idea is, to limit the amount of attacks possible attacks.  As admins, we often have to fight for the security we are allowed to implement, so take whatever you can get.  Even if you have to completely open a host in your lan to the DMZ, one host is a lot better than every host.
A realistic example from what you said is putting a front-end exchange server in the DMZ, and a the back-end and active directory in the LAN.  Even though these can still be reached from the DMZ, at least you have limited the possible ways to get through.  You can then put your efforts into watching for any security notices related to these particular services.
Comment: Every time I hear the words "With security, every extra bit you can get helps." I cringe.  Adding layers to security for the sake of security usually causes more complexity for the admins and users.  With more complexity comes the chance of misconfiguration by admins and bypass measures by the users.

Security is always a balance between 'secure enough' and inconvenience.
Comment: Honestly I feel that DMZ sounds good in theory, but is not that straight forward in practice. Almost any typical service in the DMZ would need access to the server in the LAN, just for authentication.

Anyway, thanks!
Comment: Depends on what the company does.  If you run websites, they probably would not need to contact that LAN for any reason.  Same goes with FTP sites.
Comment: Dscoduc:  Good point, I guess it depends on where you are.  If you have to fight to just get the most basic measures implemented, then every bit helps from that view.  If you you work in a more enterprisey place were all sort of layers of security get in the way, then you are right, then complexity becomes the trade off.
Answer: The goal of a DMZ is usually to separate the untrusted network from the trusted network.  Depending on the level of firewalls you can control many aspects of the connection besides just opening up ports.
My experience has been that companies put in place a policy that prevents any untrusted connections from communicating directly with trusted resources, with exceptions being carefully reviewed.  For example, reverse proxy servers in the DMZ can provide pre-authentication for untrusted connections before they are served up data from trusted resources.
Another point worth considering is that even with opening up ports, it is possible to control which devices can communicate over those ports.  For example, it might be necessary to open up a port to allow SQL communication between a web server in the DMZ and a SQL server in the trusted newtwork, but you could restrict the traffic flowing through that port to only the Web server and the SQL server.
Utimately it's up to you to craft a boundary policy that will provide the minimal level of access to satisfy your application requirements.
Answer: I dislike the term "DMZ" because it implies that there is a single zone into which you toss things you don't really specifically trust.  Lots of places in fact will just have some random switch or hub connected to another interface on their firewall, and call that their "DMZ".
I much prefer to segregate individual services into individual zones (typically a mixture of interfaces on the firewall and vlans on a switch connected to the firewall).  From there I'll do a mixture of symmetric natting or direct routing with ACLs / firewall rules on a per service / per server basis.  If you're an FTP server, you get ftp related access; DNS server? You get port 53, etc.  But I never put the ftp server and the DNS server on the same broadcast domain.
For me the point of a "DMZ" is mostly to act as a single point to monitor and regulate traffic, it isn't just to block traffic.  You setup policies based on the service and you have a single chokepoint to setup your logging, monitoring, and filtering.
Of course, if you do wind up having to nat directly into your corporate network for whatever reason, at least you can still monitor the logs for that specific rule that brings the traffic in, and monitor that specific host a bit more closely than the others.  Ideally though, you can just move that server into the DMZ as well and just allow more access from inside the corporate zone into that service zone than would be allowed from outside; this gives you 100% visibility on the traffic generated by that host so that if it is compromised by the internet facing service, you quickly see that it is from the traffic logs.
Answer: Shouldn't you move some of the services to the DMZ? Try to keep the LAN unreachable from anywhere. Then, manage the security in the DMZ area.
Comment: or, add a second DMZ, with internal services.


df.groupby(...).apply(...) function in dask dataframe

Question: I was using Python dask to process a large csv panel data set (15+GB), and I needed to conduct a groupby(...).apply(...) function  to delete the last observations for each stock in each day. My dataset looks like
 stock     date     time   spread  time_diff
  VOD      01-01    9:05    0.01     0:07
  VOD      01-01    9:12    0.03     0:52
  VOD      01-01   10:04    0.02     0:11
  VOD      01-01   10:15    0.01     0:10
  VOD      01-01   10:25    0.03     0:39
  VOD      01-01   11:04    0.02    22:00
  VOD      01-02    9:04    0.02     0:05
  ...       ...     ...     ....     ...
  BAT      01-01    13:05   0.04    10:02
  BAT      01-02    9:07    0.05     0:03
  BAT      01-02    9:10    0.06     0:04
  ...       ...     ...     ....     ...

If the dataframe was in pandas then this can be done by
df_new=df_have.groupby(['stock','date'], as_index=False).apply(lambda x: x.iloc[:-1])
This code works well for pandas df. However, I could not execute this code in dask dataframe. I have made the following attempts.
ddf_new=ddf_have.groupby(['stock','date']).apply(lambda x: x.iloc[:-1]).compute()
or
ddf_new=ddf_have.groupby(['stock','date']).apply(lambda x: x.iloc[:-1],  meta=('stock' : 'f8')).compute()
or
ddf_new=ddf_have.groupby(['stock','date']).apply(lambda x: x.iloc[:-1],  meta=meta).compute()
Unfortunately, none of them worked. Can anyone help me to get the right code for dask dataframe ? THX
Comment: If you don't run `compute` you're not performing anything but creating a graph. The golden rule with dask is that if the operation runs in pandas you don't need to switch to dask. Do you mind to produce a [mcve](/help/mcve)?
Comment: @rpanai   thanks. I've added a quick example.
Comment: About performances: is your data stored on a single `CSV` file?
Comment: @rpanai  yeah...............
Answer: I think that for your specific case the problem is the meta you are assigning. This should work.
import pandas as pd
import numpy as np
import dask.dataframe as dd

dates = pd.date_range(start='2019-01-01',
                      end='2019-12-31',
                      freq='5T')

out = []
for stock in list("abcdefgh"):
    df = pd.DataFrame({"stock":[stock]*len(dates),
                       "date":dates,
                       "spread":np.random.randn(len(dates))})
    df["time_diff"] = df["date"].diff().shift(-1)
    df["time"] = df["date"].dt.time.astype(str)
    df["date"] = df["date"].dt.date.astype(str)
    out.append(df)
df = pd.concat(out, ignore_index=True)

del out

ddf = dd.from_pandas(df, npartitions=4)

out = ddf.groupby(['stock','date']).apply(lambda x: x[:-1],
                                          meta={"stock":"str",
                                                "date":"str",
                                                "spread":"f8",
                                                "time_diff":"str",
                                                "time":"str"})
out = out.compute().reset_index(drop=True)

If you can nicely partitions your file by stock-day and save in to_parquet you can have some better performance as you can use map_partitions instead of apply.
Comment: This timestamp `2018-01-01 00:00:00 ` is not on my example. Do you mind to check the `dtypes` of your dataframe?
Comment: thanks for replying.  I see that you have defined each column in pandas dataframe before converting it to dask. I used dd.read_csv to import data to dask dataframe at the beginning with each column defined.   i tried your code and I still could not get output.   ValueError: Key 2018-01-01 00:00:00 not in level Index.
Comment: I was just creating a `df`. If you use `df.to_csv("out.csv", index=False)` and then `ddf =  dd.read_csv("out.csv")` it's the same as your working case.
Comment: Thanks for replying. I used `ddf.astype` and i got stock is object, date is object, spread is float64 and time is str.  However, when I tried `meta={"stock":"object",
                                                       "date":"object",
                                                       "spread":"f8",
                                                       "time_diff":"f8",
                                                       "time":"str"}`   , i got     `    The columns in the computed data do not match the columns in the provided metadata    `   .     Ant idea ? Thanks again.
Comment: After quite a few tries, I found I can run code if I groupby single column index without providing `meta`. If I groupby two columns, then I can only run simple syntax such as  `ddf.groupby(['stock','date']).sum() ` .
Comment: @FlyUFalcon this is right. But it works only when you use the same implemented method on one or more columns.


few garbage collections are reported in logs, although our benchmark program creates millions of objects per second

Question: i am benchmarking a JSON transformation library called JOLT. The library looks pretty good but
we wanted to make sure that it did not stress our garbage collector too much.
I therefore created a small program (reproduced below) that  does the following:
- precompiles JSON tranformation scriptlet
- loop for a long time in each of ten threads, doing the following:
    - create randomized JSON string input (approx 300 bytes)
    - parse input using JOLT and apply tranform

When we look at our gargbage collector logs we never see a full GC (even after running for half
a day), and even more suspicious, the only GC's that are reported to happen occur in the first
couple of minutes of launching the program, then hours and hours pass and we don't see
a single additional GC.  The program is performing an impressive number of transactions
per second.. and according to the JOLT documentation:

"The transform process will create and
  discard a lot of objects, so the garbage collector
  will have work to do."

So, I guess I should be happy that the our gc log looks like this even after an hours long run:
Server VM (25.66-b17) for linux-amd64 JRE (1.8.0_66-b17), built on Oct  6 2015 17:28:34 by "java_re" with gcc 4.3.0 20080428 (Red Hat 4.3.0-8)
Memory: 4k page, physical 12439584k(11065340k free), swap 12721148k(12721148k free)
CommandLine flags: -XX:InitialHeapSize=199033344 -XX:MaxHeapSize=3184533504 -XX:MaxNewSize=348966912 -XX:MaxTenuringThreshold=6 -XX:OldPLABSize=16 -XX:+PrintGC -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:SoftRefLRUPolicyMSPerMB=1 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseConcMarkSweepGC -XX:+UseParNewGC
2017-01-10T11:13:39.752-0800: 1.458: [GC (Allocation Failure) 2017-01-10T11:13:39.753-0800: 1.459: [ParNew: 51904K->6464K(58368K), 0.0123836 secs] 51904K->8148K(188096K), 0.0138403 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
2017-01-10T11:13:40.416-0800: 2.122: [GC (Allocation Failure) 2017-01-10T11:13:40.416-0800: 2.123: [ParNew: 58368K->6463K(58368K), 0.0193642 secs] 60052K->10934K(188096K), 0.0197870 secs] [Times: user=0.03 sys=0.00, real=0.02 secs]
2017-01-10T11:13:40.611-0800: 2.317: [GC (Allocation Failure) 2017-01-10T11:13:40.611-0800: 2.318: [ParNew: 58367K->4378K(58368K), 0.0098514 secs] 62838K->12472K(188096K), 0.0102622 secs] [Times: user=0.03 sys=0.00, real=0.01 secs]
2017-01-10T11:13:40.762-0800: 2.468: [GC (Allocation Failure) 2017-01-10T11:13:40.762-0800: 2.468: [ParNew: 56282K->6464K(58368K), 0.0067945 secs] 64376K->14714K(188096K), 0.0071342 secs] [Times: user=0.03 sys=0.00, real=0.01 secs]
2017-01-10T11:13:40.954-0800: 2.661: [GC (Allocation Failure) 2017-01-10T11:13:40.955-0800: 2.661: [ParNew: 58368K->6464K(58368K), 0.0102488 secs] 66618K->17490K(188096K), 0.0108123 secs] [Times: user=0.03 sys=0.00, real=0.01 secs]
2017-01-10T11:13:41.216-0800: 2.922: [GC (Allocation Failure) 2017-01-10T11:13:41.216-0800: 2.922: [ParNew: 58368K->6464K(58368K), 0.0110529 secs] 69394K->21205K(188096K), 0.0114348 secs] [Times: user=0.04 sys=0.00, real=0.01 secs]
2017-01-10T11:13:41.412-0800: 3.119: [GC (Allocation Failure) 2017-01-10T11:13:41.413-0800: 3.119: [ParNew: 58368K->5049K(58368K), 0.0073977 secs] 73109K->22139K(188096K), 0.0077819 secs] [Times: user=0.02 sys=0.00, real=0.00 secs]

But maybe this is too good to be true?  Perhaps I did not specify the correct JVM switches to
catch >all<  the GC's, and there really are some happening that are not logged ?
The program is continually running (and continually producing small objects) so it would seem
that this would fill up eden space, and a memory allocation failure would occur, and
a GC would be initiated as a result..  Even though I have (I think) a decent  grasp of garbage collector
concepts, I'm kind of puzzled.   The only things I can think may be happening here are: 1) maybe
JOLT allocates its small objects on the stack (like in a big array) instead of the heap?  or 2) (more likely), maybe I did not specify my JVM switches correctly.  Here is what I did w/ the switches:
export MAVEN_OPTS=" -XX:+UseConcMarkSweepGC \
-XX:SoftRefLRUPolicyMSPerMB=1 \
-XX:+PrintGCTimeStamps \
-XX:+PrintGCDateStamps \
-XX:+PrintGCDetails \
-Xloggc:/tmp/gc.log  -Xmx2048m "

I then ran the program as a unit test like this:
mvn clean test

The source code for the test is given below.
(if desired i can post the full maven project for this thing on github.)  Thanks in
advance for the help !
import com.bazaarvoice.jolt.Chainr;
import com.bazaarvoice.jolt.JsonUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.math.stat.descriptive.SummaryStatistics;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.DisplayName;

import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;

public class BenchmarkJoltTest {
    static int numThreads = 20;
    static int numSecondsToRun = 360 * 1000; // 10 hours
    static boolean randomize = true;    /* for non-random we will validate that the output always remains constant */

    static int hashOfExpectedOutput = 0;
    static SummaryStatistics summaryStatistics  = new SummaryStatistics();

    static String inputJson = "";
    static String transformSpecJson  = "";
    static Random random = new Random();
    static Chainr chainr = null;
    static AtomicLong completedTransformsCountForInterval = new AtomicLong(0);
    static AtomicLong completedTransformsCountGrandTotal = new AtomicLong(0);

    //summaryStatistics

    @BeforeClass
    public static void initialize() throws Exception {
        System.out.println("HI HI HO");
        inputJson =
                String.join( " ", IOUtils.readLines(ClassLoader.getSystemResourceAsStream("input.json")));
        transformSpecJson =
                String.join( " ", IOUtils.readLines(ClassLoader.getSystemResourceAsStream("spec.json")));
        chainr = Chainr.fromSpec(JsonUtils.jsonToList( transformSpecJson ));

        /* Warm up: do JIT compilation, load classes and whatever else might need to be done only during startup */
        for (int i = 0; i < 100; i++) {
            runOneTransform(false);
        }
    }

    @Test
    @DisplayName("Benchmark for Jolt Json transforms")
    public void runBenchmark() throws Exception {
        runTransformsInThreadPool(10 /* threads */);
        System.out.println("OK - here");

        for (int i = 0; i < numSecondsToRun; i++) {
            Thread.sleep(1000);  /* 1 second */
            double value = completedTransformsCountForInterval.doubleValue();
            System.out.println("adding to count: " + value );
            summaryStatistics.addValue(value);
            completedTransformsCountForInterval.set(0L);           // reset counter
            if (completedTransformsCountForInterval.get() > 100) {
                throw new Exception("how did the counter suddenly jump so high? -- unexpected");
            }
        }

        System.out.println(
                "Summary statistics for number of transforms performed per second\n\n" +
                        summaryStatistics.toString() + "\ntotalCount: "+ completedTransformsCountForInterval);
        System.out.println("Grand total of completed operations: " + completedTransformsCountGrandTotal);
        System.exit(0);
    }

    private void runTransformsInThreadPool(final int threadCount) throws InterruptedException, ExecutionException {
        Callable<Void> task = new Callable<Void>() {
            @Override
            public Void call() throws Exception {
                while (true) {
                    runOneTransform(true /* include in official count */);
                }
            }
        };
        List<Callable<Void>> tasks = Collections.nCopies(threadCount, task);
        ExecutorService executorService = Executors.newFixedThreadPool(threadCount);

        for (Callable c : tasks) {
            System.out.println("submitting " + c);
            executorService.submit(c);
            System.out.println("finished submitting ");
        }

        System.out.println("INVOKED all");
    }

    private static void runOneTransform(boolean includeInOfficialCount /* don't include if in warm-up mode */) throws Exception {
        String targetJson = randomize ?  getRandomizedJson() :  inputJson;
        Object transformedJson = chainr.transform(JsonUtils.jsonToObject(targetJson));
        String transformedJsonAsString = JsonUtils.toJsonString(transformedJson);
        if (hashOfExpectedOutput == 0) {
            hashOfExpectedOutput  = transformedJsonAsString.hashCode();
        }
        if (! randomize && hashOfExpectedOutput != transformedJsonAsString.hashCode()) {
            throw new Exception("hash codes of transformed strings don't match. multi-threading issue ?");
        }

        if (includeInOfficialCount) {
            completedTransformsCountForInterval.incrementAndGet();
            completedTransformsCountGrandTotal.incrementAndGet();
        } else {
            System.out.println(transformedJsonAsString);
        }
    }

    private static String getRandomizedJson() {
        Integer num =  random.nextInt() % 10;
        inputJson.replaceAll("3", num.toString());
        return inputJson;
    }
}

Comment: Don't forget that modern JVMs have Escape Analysis. So if the operation "will create and discard a lot of objects", this implies lots of purely local objects whose life time can be predicted without subsequent garbage collection or which might even get scalarized, once the optimizer kicked in. You may compare with a run with the `-XX:-DoEscapeAnalysis` option…
Comment: -XX:-DoEscapeAnalysis ... that's a good one.  will try. thank you!
Answer: It looks like runOneTransform is only creating very shortlived objects. Since the cost of tracing GCs depends on the size of the live object set and those objects don't live long it means the GC has almost nothing to do.
It's quite possible that in this artificial test-case you're mostly measuring the cost of the actual code executing and of allocations, not of garbage collections.
Real-world applications often have a less left-skewed lifetime histogram, requiring more tracing and copying work from the collectors.
Comment: good call. thanks..  i am going to post the answer that i got from the JOLT developers themselves and accept that. but i appreciate the pointer ;^). thnx
Answer: The logs indicate that the garbage collector >>is<< running.  Each one of those lines is a run of the ParNew collector.  However, if I am interpreting the log correctly, they are all collections of the "new object" space, and that is pretty fast.
If your application continued to run like this, it would eventually get to the point where the CMS collector needed to run to collect garbage that have been promoted out of the new space.

The fact that your application does not appear to be generating much load is not necessarily suspicious.  The stuff you found in the Jolt documentation could be out of date or inaccurate.  (For instance, they may have been generalizing incorrectly.)
If you suspect that your application is not working correctly, look for another way to validate the results.

Maybe I did not specify my JVM switches correctly.

A low GC rate is a >>good<< thing.  You should only look at tweaking the switches if there is a problem.
Comment: Thanks, Stephen. bad me ! my title said "is not running" should have been "is running far more infrequently than expected". And yes.. the logs (and also looking in gc-viewer reveal no full GC's were ever done .. even after 12 hours of running. I will correct the title.     And yes.. you are correct other ways of validating the program are indeed appropriate..   you will note the benchmark does xformations/second.  I got great numbers this way (~ 200k xforms/sec)
Answer: Thanks to Stephen C, @the8472, @Holger for the tips.  I am going to summarize the answer that I got from the Jolt developers (specifically, Milo Simpson), and post a link to the details.
In summary:   the explanation Milo gave was that EscapeAnalysis was kicking in and that the objects Jolt is creating to do the transforms are being allocated on the stack.  If I wanted to force GC's (which I'd rather not do in production ;^)  he recommended  setting up the test so that references to the output JSON cross thread boundaries.
I don't think I'll do that because the point of our exercise was to assess Jolt's performance... and it seems to be very solid.
I learned a lot from this exercise and recommend reading Milo's answer in full. https://github.com/bazaarvoice/jolt/issues/305#issuecomment-271861868


What happens after backpropagation in the learning phase within Neural Network?

Question: Within a Neural Network during the learning phase ,there are two mechanisms happening. Feedforward and Backpropagation. Taking an example of XOR operation.
<code>A   B   Q
0   0   0
0   1   1
1   0   1
1   1   0
</code>
For the first pass(0,0->0) feedforward takes place, then backpropagation happens.After this step all the weights are recalculated .
What happens now?
Question-1:Again the same input 0,0 is feedforward with the new calculated weights(during the backpropagation) and then backprogated until the error becomes nill? If yes, what happens if the error is never nill? Which brings me to the next question.
Question-2:When would the learning for the next pass(0,1->1) happen?
 Question-3: Suppose the concluded weights for the 1st pass is 10.3,-2.3,5.5 .The second pass(0,1->1) starts the feedforward with the concluded weights of the first pass?
Answer: The answer depends on which algorithm you are using. For instance, a naive algorithm would be to calculate the loss on entire training data set (sample), then get the numerical gradient. Then use the gradient to suggest the step change in parameters. Changing parameters is learning, as long as your loss is decreasing. Hence, each change in parameters requires you feed forward entire sample.
You could go another extreme and change the parameters at every observations, i.e. batch size = 1. You feed forward just one observation, and this lets you estimate the gradient. The gradient will be very noisy, of course.
Somewhere in between is the batch size >1 but less than sample size. Here, you could smooth out the noisy gradient estimation by running a few observations before adjusting the parameters. It turns out that batch optimization is most optimal usually.
Answer: No Same inputs is not passed, you iterate over each instance in training set one by one till you iterate all instances in training set(this you do as many times as your number of epochs or iterations that you provide as hyper parameter).
While iterating over each you adjust weights as per each instance, Over multiple iterations.
As you have four instances and lets say you ran it for 50 iterations.
So yes each instance will be sent to the Network 50 times, but not all 50   together.
All instances(training) will be sent one by one 50 times.
Yes, for question 3. but first pass doesn't mean 50 instances of 0,0. it just mean 1 instance of 0,0 then over that we pass 1,0 and so on fifty times.
Hope this answers multiple questions that you have.
Comment: _"you iterate over each instance in training set one by one till you iterate all instances in training set_". In practice, that's what people do with SGD-based learning, but it's not necessarily true in general.
Comment: @Firebug, I have some understanding of batch learning and SGD's. But not able to think of scenarios where you will not learn over all learning data... apart from the case when you have a very huge set of data(where you use subset of training data to train) or you go for cross validation. Please update me with a basic example and if possible some reference so that I can enhance my learning.
Comment: It was not about using every instance. Instead, I'm referring to the iterative process. Iterating over the instances one at a time or in batches are possible schemes in SGD, but, in theory, nothing precludes you from using all the stances at once to calculate the gradient in a gradient descent scheme. Practical considerations are necessary though, as always.


Erro ao iniciar a aplicação utilizando PM2 com propriedades customizadas em pedido expresso

Question: neste diretório defini no arquivo index.d.ts algumas propriedades customizadas na Request do express que serão utilizadas por um middleware de autenticação.

Como pode ser visto na imagem abaixo:

Porém, ao iniciar a aplicação com PM2 recebo o seguinte erro:
src/middlewares/ensureAuthentication.ts(34,13): error TS2339: Property 'authenticatedUserId' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'
O que devo fazer para resolver este problema ?
Este é meu tsconfig:
Comment: Tem algum motivo para rodar a aplicação em TS sem transpilar para JS? Ou seja, qual o impedimento do PM2 rodar a aplicação gerada pelo build (tsc)?
Answer: Tive um problema com PM2 usando typescript recente, se caso quando você inicia a aplicação normalmente ele executa, você pode usar a abordagem de startar o pm2 fazendo ele rodar o seu script de start da aplicação, ex:
pm2 start npm --name "nomeapppm2" -- run {script_name}


Average time between user sign up and first action in MYSQL

Question: I have been tasked to find the average time between user sign up and their first action on the app, i.e. joining a game.
We have an app that helps people find other basketball players.
I know that I have to use two tables
the first table being the users table and the second being the game_users
Users Table
id | first name | last name | email | created_at
12| John |Smith | firstname.lastname@example.com| 2019-01-16 07:41:49

Game users Table
game_id | user_id | is_admin | active |created_at
23 |        14 |     0      |   1      | 2020-04-18 09:42:34

active here refers to joining a game
How can I tell get the first time a user joined a game? and then compare that date with the sign up date from the users table?
my expect outcome would be something like this
game_id | user_id | Sign Up | game_user created at | time_between
23       | 12     |2019-01-16 07:41:49| 2019-01-18 09:42:34 | 2

Comment: Add the desired output with some detailed sample data.
Comment: *my expect outcome would be something like this* ??? Average - this is "over all" or "over a group", not for one separate user.
Comment: I don't need the average for one separate user, ill calculate the average after finding the difference for each user
Answer: To get average time use:
SELECT AVG(TIMEDIFF(games.created_at, users.created_at))
FROM users
JOIN ( SELECT user_id, MIN(created_at) created_at
       FROM games
       GROUP BY user_id ) games ON users.id = games.user_id

To get the time for each separate user:
SELECT users.*, games.*, TIMEDIFF(games.created_at, users.created_at)
FROM users
JOIN ( SELECT user_id, MIN(created_at) created_at
       FROM games
       GROUP BY user_id ) games ON users.id = games.user_id


SignalR 2 StartAsync never returns

Question: In the following part of a class, StartAsync never returns.
Any ideas why?  The server appears to be working fine, and works with Javascript clients.
SignalR client version is v1.0.0-rc1-final
    public HubUtil(string baseUrl) //string clientId
    {
        connection = new HubConnectionBuilder()
            .AddJsonProtocol()
            .WithUrl(baseUrl)  // baseUrl is "https://hostname/hubname"
            .Build();

        connection.Closed += Connection_Closed;
        StartIfNeededAsync();
    }

    private Task Connection_Closed(Exception arg)
    {
        return StartIfNeededAsync();
    }

    public async Task StartIfNeededAsync()
    {
        if (_connectionState == ConnectionState.Connected)
        {
            return;
        }

        try
        {
            await connection.StartAsync(); // Never connects
            _connectionState = ConnectionState.Connected;
        }
        catch (Exception ex)
        {
            _connectionState = ConnectionState.Faulted;
            throw;
        }
    }

From a basic console app this is how hubutil is called:
    static void Main(string[] args)
    {
        var hub = new HubUtil("https://host/hubname");
        hub.Invoke("checkin", "id", "");
    }

Comment: Are you passing a valid URL? Your comments just say clientId. If not calling a proper URL it will hang as there is nothing to connect to. Show a [mcve]
Comment: Some months before, I ran into similar problem but with different third party library. Then, problem was hidden inside asynchronous methods. They were synchronously waiting on asynchronous code behind the scene. Only workaround was to wrap my call to their async code to Task.Run. Then I made it running on threadpool thread and suddenly it worked.
Comment: It will be much better for you if you read the warnings that Visual Studio is throwing at you. `StartIfNeededAsync` is an async method without await, that's the first sign something is terribly wrong
Comment: I've updated it to clarify that the issue happens regardless of whether it has an await or a .Wait()
Comment: If you look at the output window, does anything else happens? Does it timeout if you leave it for a minute, for example? From where are you instantiating `HubUtil`? What project type is this running on?
Comment: Quite literally nothing happens...no output in the console.  It's a .net standard 2.0 library, running from a WPF application.  It's instantiated from a main window xaml
Comment: @Nkosi It's passing in the host + "/hubname".
Comment: Then the issue may be related to other factors external to the code shown. Show how and where this util is called. blocking on `StartAsync` is the symptom and not necessarily the cause.
Comment: Updated the question.  It's just instantiated from the constructor of a WPF window.
Comment: It could be a compatibility problem between SignalR 2 and SignalR Core. Do you see any useful output on the SignalR 2 server?
Comment: Try to call `Task.Run(() => StartIfNeededAsync());` in constructor instead of `StartIfNeededAsync();`
Comment: Well it doesn't block anything for sure, but SignalR isn't connecting still.  Although now the task is ending, it says it's ended successfully
Comment: I wrote a basic console app to rule out WPF involvement but no change
Comment: When task is ended successfully, it means that connection was made or an exception occurred. Can you confirm this?
Comment: On the StartAsync, the task is RanToCompletion, Void result and a null exception (which sounds like it's worked to me...)
Answer: Probably trying to do too many things at the same time.
Remove StartIfNeededAsync from constructor
public HubUtil(string baseUrl) {
    connection = new HubConnectionBuilder()
        .AddJsonProtocol()
        .WithUrl(baseUrl)  // baseUrl is "https://hostname/hubname"
        .Build();

    connection.Closed += Connection_Closed;
}

private Task Connection_Closed(Exception arg) {
    return StartIfNeededAsync();
}

public async Task StartIfNeededAsync() {
    if (_connectionState == ConnectionState.Connected) {
        return;
    }

    try {
        await connection.StartAsync();
        _connectionState = ConnectionState.Connected;
    } catch (Exception ex) {
        _connectionState = ConnectionState.Faulted;
        throw;
    }
}

//...

and make it an explicit call also taking SSL into consideration.
//Handle TLS protocols
System.Net.ServicePointManager.SecurityProtocol =
    System.Net.SecurityProtocolType.Tls
    | System.Net.SecurityProtocolType.Tls11
    | System.Net.SecurityProtocolType.Tls12;

var hub = new HubUtil("https://host/hubname");
await hub.StartIfNeededAsync();
hub.Invoke("checkin", "id", "");

Comment: I can't see how moving one call from a constructor to the same place in the calling method is going to help here?
Comment: @Paul It could be a deadlock issue, and the explicit `await` on a separate call could fix that. That's the difference
Comment: @Paul By not awaiting the async call you could be calling Invoke before the connection has time to complete.
Comment: @Paul reviewing a sample on GitHub, the `StartAsync` is a distinct step that must be awaited. https://github.com/aspnet/SignalR/blob/4ac8e786cfd86fa3b40321f932ea0db00f9d8ae1/samples/ClientSample/HubSample.cs
Answer: I answered a similar question on SO already. I don't want to copy and paste it here so here is the link instead:
https://stackoverflow.com/a/58551924/603807
Since this is a link to my own answer on the SO site I would hope this takes a pass for the moderators who don't like link only answers.
Comment: This doesn't actually help, when the `.StartAsync()`-call doesn't ever return.
Comment: I think it "appears" as though it never returns because I was having the same problem. You have to wait for it to return by actually using the wait method. Yes I know that defeats the purpose of Async calls, but it's not my API. I have researched this a lot and I cannot find another solution.
Comment: I meant on the http-Level the call never returns - so it won't make a difference whether or not I .Wait() for it. Anyway, I found the solution to my specific problem: Had ot install the WebSockets-feature for IIS.

SignalR 2 StartAsync never returns


Jquery click event not triggering

Question: I'm remaking my website and want to do everything from scratch (no bootstrap, foundation, etc). Right now I'm making the navbar and I'm trying to get it to toggle slide in and out on click. For some reason click events aren't working and I'm not sure what I did wrong. I made a rough example in codepen here
HTML

CSS
.box {
  transform: translateX(0);
  width: 100px;
  height: 100px;
  background-color: red;
  transition: transform 250ms;
}
.slide {
  transform: translateX(250px);
}

JS
$(function() {
  $('.box').on('click', function(slideToggle) {
    $(this).toggleClass('slide');
  });
});

Comment: jQuery is not available, if you inspect the page you will see `Uncaught ReferenceError: $ is not defined`
Answer: For you codepen example:
Go to settings -> Add library -> search jquery and select.
If this is the same case with your real code:
open console -> check if $ is not defined or $ is not jquery ?
if $ not defined, add a valid jquery cdn url.
if $ is not jquery, try to find a conflicting library.
Answer: Everything is good in your code except your jQuery reference, please check your downloaded jquery path, or you can use like below, now i am using jquery directly from the jquery official site

extra note:
   found you added some new CSS3 attribute ("transition: transform 250ms;"),  when ever you use the CSS3 attribute please make sure you should reset the attribute for all browsers, like wekit,moz,
thanks
Comment: oh gosh im an idiot haha. i was messing with the code and trying different selectors and events but never checked my reference haha. thanks!
Comment: haha no issues bro. when you have facing some issue in your code, go n check the console, you will get to know what exactly the error is, if something is undefined which means may be your reference to the function is not proper or some typo you made,  if in the case of $ or jquery says its undefined which means jquery is not loaded in your page. and please add the reset for the css also otherwise your page will break in firefox and safari browsers
Answer: It seems to work fine here in a snippet. If you replace your script on codepen with the vanilla version below, it works there too. (Note: only tested in Chrome)

$(function() {
  $('.box').on('click', function(slideToggle) {
    $(this).toggleClass('slide');
  });
});
.box {
  transform: translateX(0);
  width: 100px;
  height: 100px;
  background-color: red;
  transition: transform 250ms;
}

.slide {
  transform: translateX(250px);
}

Alternative script for your codepen:
document.querySelector(".box").addEventListener("click", function(event){
  event.target.classList.toggle("slide");
});

Answer: You have not loaded jQuery correctly for codepen. To add jQuery simply replace the

with

Answer: You can add jquery cdn manually in setting just click the setting icon and click the javascript tab  then under that search the jquery and hit enter.
screenshot for jquery cdn codepen setting

Jquery click event not triggering


jQuery Validation improperly true

Question: I currently have a form that I am validating with the jQuery validator plugin. Within the form, I have one field that will be, depending on the input another field, required or not.
 <select name="supportiveServices" id="supportiveServices" style="float: left; margin-left: 20px; width: 30%;">
        <option value="">Select One</option>
        <option value="Accepted">Accepted</option>
        <option value="Declined">Declined</option>
    </select>

 <input name= "serviceStartDate" id= "serviceStartDate" type="date" style="width: 50%;"></input>

jQuery
 serviceStartDate: {
            required: function(element) {
                 if($("#supportiveServices").val() == 'Accepted') {
                    return true;
                }
                 else {
                    return false;
                 }
            },

        },

This dependency requirement seems to work fine but the issue I am consistently running into is I that I need the date to valid as well. I have added a method to ensure dates are valid.
jQuery.validator.addMethod("DateFormat",
    function(value, element) {
    return value.match(/^\d\d?\/\d\d?\/\d\d\d\d$/);
 },
  "Please enter a date in the format dd/mm/yyyy";
 );

I am new to this plugin but I have tried the following two solutions, both of which return true and require input:

Creating a function that basically returns true if the date is empty and false if not.
serviceStartDate: {
        required: function(element) {
             if($("#supportiveServices").val() == 'Accepted') {
                return true;
            }
             else {
                return false;
             }
        },

        DateFormat: function(element) {
            if($("#serviceStartDate").val() != 0 || $("#serviceStartDate").val() != null || $("#serviceStartDate").val() != undefined || $("#serviceStartDate").val() != '') {
                return true;
            } else {
                return false;
            }
        }

    },

Create a separate method.
 jQuery.validator.addMethod("DateFormatSupport",
      function(value, element) {
         if ($("#serviceStartDate") == 0 ) {
         return value.match(/^\d\d?\/\d\d?\/\d\d\d\d$/);
       }
    },
     "Please enter a date in the format dd/mm/yyyy"//removed ;
    );

and calling it.
        serviceStartDate: {
            required: function(element) {
                 if($("#supportiveServices").val() == 'Accepted') {
                    return true;
                }
                 else {
                    return false;
                 }
            },

            DateFormatSupport: true
            }

        },

I basically need the validator to ensure that if the client doesn't choose "Accepted" for supportive services, then serviceStartDate can be optional. If "Accepted" for supportive service is chosen, than they will be required to fill in serviceStartDate and the date will need to valid as well. As a precaution, I would like if they filled out anything in serviceStartDate, whether it is optional or not, to be a date - This is going to a database so it has to either be null or a date.
Any advice?
Comment: Have you tried the `date` rule that is already built into the plugin?
Answer:
I would like if they filled out anything in serviceStartDate, whether it is optional or not, to be a date... Any advice?

You merely need to declare the built-in date rule on your date field, while making the required rule dependent upon your select element as you've already done.  There is no need for a custom rule in this situation.  There's also no need to make the date rule conditional as all rules are automatically ignored whenever an optional field is left blank.
rules: {
    supportiveServices: {
        required: true
    },
    serviceStartDate: {
        required: function(element) {
            return ($("#supportiveServices").val() == 'Accepted') ? true : false;
        },
        date: true
    }
}

Working DEMO:  jsfiddle.net/y1Lytetm/
NOTES:

I used ternary shorthand for your conditional... it's one line of code replacing six.
There is no such thing as an input container element, so I removed your closing </input> tag, which does not exist as valid HTML.

jQuery Validation improperly true


Syntax for "extending" Prototype in ECMAScript 6

Question: Since classes in ECMAScript 6 is simply syntactical sugar over JavaScript's existing prototype-based inheritance, it is still possible to "extend" (for lack of a better word) the prototype of an existing class with EMCAScript 5 syntax and add methods to it even after the class declaration in a completely separate file.
/* Dog.js */

class Dog {
    woof() {
        console.log('woof woof');
    }
}

module.exports = Dog;

/* main.js */

var Dog = require('./Dog');

Dog.prototype.bark = function() {
    console.log('bark bark');
};

var dog = new Dog();
dog.woof(); // 'woof woof'
dog.bark(); // 'bark bark'

Is there a way to achieve the same functionality as above, but maintain the ECMAScript 6 class syntax in main.js as opposed to going back to the prototype syntax? Trying something such as class Dog extends Dog throws an error as the Dog class is already declared.
Note that this is not extending in the sense of inheritance, as that would require creating a new class rather than adding methods to an existing class.
class Dog {
    woof() {
        console.log('woof woof');
    }
}

class AdvancedDog extends Dog {
    bark() {
        console.log('bark bark');
    }
}

var dog = new Dog();
dog.woof(); // 'woof woof'
dog.bark(); // Error, not what is needed.

Comment: Not really. `extends` means that the new class will inherit everything the old class has, it doesn't extend the old class with new properties. You have to "extend" the object, or it's prototype, with new properties,for that.
Answer: No, there's no way of doing what you want to achieve.
A class can't be partially declared across many files.
BTW, you should be able to use some task runner like Grunt or Gulp and implement a task to merge many files declaring the same file that could generate a single file with a single class declaration from the other ones.
Probably this could be also implemented as a Babel plugin.
Answer: The semantics of extends is prototypical inheritance, while the wanted behaviour is prototype modification. It is not an everyday task and it is not something that has special syntax in ES6.
It is possible to do this with
Object.assign(Dog.prototype, {
    bark() {
        console.log('bark bark');
    }
});

Notice that bark method in object literal is enumerable, while ES6 class methods are non-enumerable. If this trait is crucial, Object.defineProperty should be additionally used.


Erro em cálculo

Question: Olá, estou com um projeto de uma calculadora de consórcio que está apresentando erro. Ela recebe do usuário os seguintes dados:

Valor do consórcio
Quantidade de meses
% do fundo de reserva
% da taxa administrativa

Após isso ele calcula o valor da parcela mensal.
O meu problema é o seguinte: Se eu colocar a quantidade de meses 10 funciona se colocar 15 já dá erro.
Segue o código:
BigDecimal cota = new BigDecimal(jTextField1.getText());
BigDecimal prazo = new BigDecimal(jTextField2.getText());
BigDecimal reserva = new BigDecimal(jTextField3.getText());
BigDecimal adm = new BigDecimal(jTextField4.getText());

DecimalFormat decimal = new DecimalFormat("0.##");

//Calculo Fundo Comum
BigDecimal pc = new BigDecimal("100");
BigDecimal percentualMensal = pc.divide(prazo);
BigDecimal parcelaMensal = percentualMensal.multiply(cota);
BigDecimal parcelaMensal1 = parcelaMensal.divide(pc);

//Calculo Taxa Administrativa
BigDecimal a1 = adm.divide(prazo);
BigDecimal parcelaAdm = a1.multiply(cota);
BigDecimal parcelaAdm1 = parcelaAdm.divide(pc);

//Calculo Fundo Reserva
BigDecimal r1 = reserva.divide(prazo);
BigDecimal parcelaReserva = cota.multiply(r1);
BigDecimal parcelaReserva1 = parcelaReserva.divide(pc);

BigDecimal calculo = parcelaMensal1.add(parcelaAdm1.add(parcelaReserva1));

String a = decimal.format(calculo);

jLabel5.setText("Valor: "+a);

Comment: Com 10 funciona porque 10 é divisor de 100. 15 não é divisor de 100, então vai dar essa exceção. Você tem que dizer o que você quer fazer quando a divisão dá uma dízima periódica.
Comment: Mas qual é o erro?
Comment: `pc.divide(prazo);` - E se a divisão der uma dízima periódica, o que ele deveria fazer? Do jeito que está, vai dar `ArithmeticException`.
Answer: O problema já foi indicado pelo @VitorStafusa nos comentários, que é o fato de a divisão resultar numa dizima infinita periódica, no método divide.
Se considerar um prazo de 15 meses, a divisão que está a fazer é 100/15 que dá 6.6666666 infinitamente.
Exemplo do problema em código (com o valor 15 forçado):
BigDecimal v1 = new BigDecimal(100);
BigDecimal v2 = new BigDecimal(15);
BigDecimal v3 = v1.divide(v2);

System.out.println(v3);

Que gera uma exceção no método divide:

java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.

Veja a exceção a ocorrer no Ideone
Solução
A solução para este problema é na verdade simples. Precisa apenas de alterar a sua chamada ao método divide indicando em quantas casas decimais quer o resultado e o modo de arredondamento:
BigDecimal v3 = v1.divide(v2, 4, RoundingMode.HALF_UP);
//casas decimais--------------^ ,  ^-------- modo de arredondamento

Veja também este exemplo no Ideone
Tem à sua disposição 3 modos de arredondamento:

HALF_DOWN
HALF_UP
HALF_EVEN
Comment: @JeffersonQuesado Sim faz sentido pois não vai sempre para o mesmo lado, logo com uma larga quantidade de cálculos o erro é menor, estatisticamente falando
Comment: `HALF_EVEN` é o chamado arredondamento bancário. Estatisticamente, é o que gera menos erros em um conjunto grande de operações. Devido a essa natureza de gerar poucos erros, ele é usado pelos bancários pra fazer contas

Erro em cálculo


implementação do Split() em C++

Question: Estava procurando uma forma de implementar a função .split() do C# em C++ e encontrei o seguinte código no google:
std::vector<std::string> split(const std::string& text, char sep)
{
    std::vector<std::string> tokens;
    std::size_t start = 0, end = 0;

    while ((end = text.find(sep, start)) != std::string::npos)
    {
        tokens.push_back(text.substr(start, end - start));
        start = end + 1;
    }

    tokens.push_back(text.substr(start));
    return tokens;
}

Coloquei o código no meu projetinho, testei e vi que funcionava, então parti logo pra tentar entender exatamente como o algoritmo funciona.
Entendi a maior parte do código, mas travei na parte da condição do while:
while ((end = text.find(sep, start)) != std::string::npos)

Eu meio que compreendi que ele verifica se ainda há o valor do char sep (uma ',' por ex.) na string, mas não consegui entender como ele faz isso.
Alguém poderia me explicar detalhadamente como esse trecho do código funciona?
Muito obrigado.
Answer: O método find procura a posição do caractere sep começando a partir do índice start, se ele achar ele retorna a posição do caractere para end, senão retorna npos para end.
Por isso que a condição do while é que ele continue iterando enquanto end seja diferente de npos.
O método substr copia uma determinada quantidade de caracteres de uma string começando por um índice que seria start e a quantidade seria end - start.
Simplificando seria isso:
std::string text { "texto1,texto2,texto3" };

size_t start = 0, end = 0;

end = text.find(',', start); // start(0), end(6) != npos (true, executa o while)

{
  text.substr(start, end - start); // 0, (6 - 0 = 0) = texto1

  start = end + 1; // 6 + 1 = 7
}

end = text.find(',', start); // start(7), end(13) != npos (true, executa o while)

{
  text.substr(start, end - start); // 7, (13 - 7 = 6) = texto2

  start = end + 1; // 13 + 1 = 14
}

end = text.find(',', start); // start(14), end(npos) != npos (false, encerra o while)

text.substr(start); // copia do índice 14 até o final, que seria o texto3

Links para a documentação das funções:
https://en.cppreference.com/w/cpp/string/basic_string/find
https://en.cppreference.com/w/cpp/string/basic_string/npos
https://en.cppreference.com/w/cpp/string/basic_string/substr
Comment: Deixa eu ver se entendi; se o .find() não encontrar o caractere (end = text.find(sep, start)), ele retorna npos para o end e em seguida avalia se o end é diferente de npos, certo? (end != std::string::npos)
Comment: muito obrigado, cara. entendi perfeitamente agora!
Comment: @Pedro sim, isso mesmo.
Answer: Existe algo mais simples do que isso:
#include <bits/stdc++.h>
using namespace std;

vector <string> split(string text, char separator = ' '){
    string str;
    stringstream ss(text);
    vector <string> result;
    while(getline(str, ss, separator)){
        result.pushback(str);
    }
    return result;
}

Eu sei que não quer ver isso, mas tem como reverter:
#include <bits/stdc++.h>
using namespace std;

string unsplit(vector<string> setence, char spliter = ' '){
    string result;
    for(int i = 0; i < setence.size; i++){
        result = result + setence[i] + spliter;
    }
    result.remove(result.size - 1);
    return result;
}

Answer: Escrevi um split() como se fosse um algoritmo da stl em C++ Moderno :
template<typename it, typename valueT, typename outIt>
void split(it first, it last, const valueT& e, outIt&& o) {
    auto next = first;
    while ( next != last ) {
        next = std::find(first, last, e);
        *o++ = { first, next };
        first = next;
        first++;
    }
}

1 - Funciona com iteradores, begin(), end(), istream_iterator...
string x = ",teste,oi,vamos";
split(x.begin(), x.end(), ',', ostream_iterator<string>(cout, "\n"));

2 - Funciona com sequencias vazias;
 string x = ",teste,oi,vamos";
 string y = ",,,,";
 string z = "";

3 - Funciona com qualquer container, não só strings;
 vector v = {30, 20, 0, 40, 50, 10, 70, 0, 20, 30};
 vector<vector<int>> splitted;
 cout << "Separando um vetor com em vários vetores " << endl;
 split(v.begin(), v.end(), 0, back_inserter(splitted));

4 - Tem versão com lambda;
split_if(v.begin(), v.end(), [](auto const& val) { return val < 20; }, back_inserter(w));

5 - Não faz cópia de valores ( salvo o caso de o iterador copiar );
vector<string_view> w;  // <-  string view não copia
split(x.begin(), x.end(), ',', back_inserter(w));

exemplo funcional https://godbolt.org/z/x76ojc

implementação do Split() em C++


How to get exposed filters for entity reference to render?

Question: I am using a entity reference view and I am able to filter via taxonomy terms directly, but when I set it to exposed and let the user decide, it does not display on the form. How can I get the expose functionality to actually work?
It works on other things like pages and blocks, but not entity reference.
I have Views Entity Reference Filter and Better Exposed Filters already installed, but that didn't seem to fix the issue.
Comment: You said it is not shown on the form.  For which users-- admins, authenticated users, anonymous users?
Comment: Its not shown on the form for any users, no matter the role, including admins
Answer: Instead of a Views filter you can filter via the widget autocomplete if you add a relationship for the taxonomy field, add the taxonomy Name field to the field list and then select the Name field as search field in Format:Entity Reference list | Settings.
Comment: As [commented already in your previous question](https://drupal.stackexchange.com/questions/310485/how-do-you-add-a-filter-with-manage-form-display#comment394588_310485) this is a very basic thing. If you want to use a full featured View then try an entity browser implementation, like for example https://www.drupal.org/project/content_browser
Comment: Not quite what i'm looking for. The user won't know which term to search for so I have it set as checkboxs.


React Native Maps

Question: Estou tentando implementar a biblioteca React-native-maps no meu projeto, quando executo react-native run-android recebo este erro no CMD:
* Where:
Build file 'C:\Users\Henrique\Documents\New folder\NoExpo\AwesomeProject\node_modules\react-native-maps\lib\android\build.gradle' line: 46

* What went wrong:
A problem occurred evaluating project ':react-native-maps'.
> Could not find method compileOnly() for arguments [com.facebook.react:react-native:+] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 7.449 secs
Could not install the app on the device, read the error above for details.
Make sure you have an Android emulator running or a device connected and have
set up your Android development environment:
https://facebook.github.io/react-native/docs/getting-started.html

Configurei o arquivo build.gradle assim:
dependencies {
    compile project(':react-native-maps')
    compile fileTree(dir: "libs", include: ["*.jar"])
    compile "com.android.support:appcompat-v7:23.0.1"
    compile "com.facebook.react:react-native:+"  // From node_modules
}

Comment: Qual a versão do **Gradle** ?
Comment: classpath 'com.android.tools.build:gradle:2.2.3'
Comment: Altere para `classpath 'com.android.tools.build:gradle:3.0.1'` e veja se funciona.
Answer: Se você não tiver propriedades definidas para todo o projeto definidas e tiver uma versão de serviços de reprodução diferente daquela incluída nessa biblioteca, use o seguinte (alternar 10.0.1 para a versão desejada):
...
dependencies {
   ...
   implementation(project(':react-native-maps')){
       exclude group: 'com.google.android.gms', module: 'play-services-base'
       exclude group: 'com.google.android.gms', module: 'play-services-maps'
   }
   implementation 'com.google.android.gms:play-services-base:10.0.1'
   implementation 'com.google.android.gms:play-services-maps:10.0.1'
}
Em seu android/settings.gradle:
include ':react-native-maps'
project(':react-native-maps').projectDir = new File(rootProject.projectDir,'../node_modules/react-native-maps/lib/android')
Answer: Complementando, o post acima, se você realizou os reparos e continua dando problema, você pode limpar o build utilizando os seguintes comandos.
Limpar android
rm -rf android/app/build && rm -rf android/build && rm -rf android/.gradle && rm -rf android/build.gradle-e

Limpar IOS
rm -rf ios/build && rm -rf ios/Pods && rm -rf ios/Podfile.lock

Pode até criar um script no seu package para agilizar esse processo.
"clean": "yarn clean:android && yarn clean:ios && rm -rf node_modules && rm -f yarn.lock",
"clean:android": "rm -rf android/app/build && rm -rf android/build && rm -rf android/.gradle && rm -rf android/build.gradle-e",
"clean:ios": "rm -rf ios/build && rm -rf ios/Pods && rm -rf ios/Podfile.lock",
"build": "yarn && yarn build:android && yarn build:ios",
"build:android": "npx jetifier",
"build:ios": "cd ios/ && pod install",

Isto automatizaria o build.
Boa sorte!

React Native Maps


How to return randomly multiple rows in PostgreSQL?

Question: PostgreSQL version: 9.6.18
I would like to know whether it is possible to select randomly multiple rows from a table (possibly based on a random selection of the values of a given column in the table, for example the primary key, sequence, etc.)? Let's say that I have a table containing 20 rows and I wish to return 4 rows randomly. After some Googling I saw that for 1 single row, the simultaneous use of offset, random() and limit in the select clause had been suggested as a solution. So I tried to modify a bit the concept in order to adapt it for returning randomly multiple rows and not just a single row. Here is my test case:
with testtab as
(
    select 'pkey-01' as primary_key, 'value-01' as colval union all
    select 'pkey-02' as primary_key, 'value-02' as colval union all
    select 'pkey-03' as primary_key, 'value-03' as colval union all
    select 'pkey-04' as primary_key, 'value-04' as colval union all
    select 'pkey-05' as primary_key, 'value-05' as colval union all
    select 'pkey-06' as primary_key, 'value-06' as colval union all
    select 'pkey-07' as primary_key, 'value-07' as colval union all
    select 'pkey-08' as primary_key, 'value-08' as colval union all
    select 'pkey-09' as primary_key, 'value-09' as colval union all
    select 'pkey-10' as primary_key, 'value-10' as colval union all
    select 'pkey-11' as primary_key, 'value-11' as colval union all
    select 'pkey-12' as primary_key, 'value-12' as colval union all
    select 'pkey-13' as primary_key, 'value-13' as colval union all
    select 'pkey-14' as primary_key, 'value-14' as colval union all
    select 'pkey-15' as primary_key, 'value-15' as colval union all
    select 'pkey-16' as primary_key, 'value-16' as colval union all
    select 'pkey-17' as primary_key, 'value-17' as colval union all
    select 'pkey-18' as primary_key, 'value-18' as colval union all
    select 'pkey-19' as primary_key, 'value-19' as colval union all
    select 'pkey-20' as primary_key, 'value-20' as colval
)
select
    t1.primary_key,
    t1.colval
from testtab as t1 offset floor(random() * (select count(*) from testtab as t2)) limit 4;

The above mentioned code in which I've modified limit 1 to limit 4, indeed returns 4 rows randomly to some extent, that is, the offset is a random. But then the problem is that the 4 returned rows are always contiguous. So for example if the offset is 3, then the query will definitely return in order 3, 4, 5 and 6.
primary_key    colval
------------  ---------
pkey-03        value-03
pkey-04        value-04
pkey-05        value-05
pkey-06        value-06

I would like to know whether there is any way to achieve this in a way that the returned rows are not in a contiguous block? So for example instead of 3, 4, 5 and 6 the query would actually return four random rows, something like 13, 1, 8, 16 etc.
So I'm looking for something like Sample() function in R or PROC SURVEYSELECT in SAS that can achive the same in PostgreSQL. Is it possible?
Thanks in advance.
Comment: `SELECT * FROM testtab ORDER BY random() LIMIT 4;`?
Answer: you can order by random() and it simplifies the query
with testtab as
(
    select 'pkey-01' as primary_key, 'value-01' as colval union all
    select 'pkey-02' as primary_key, 'value-02' as colval union all
    select 'pkey-03' as primary_key, 'value-03' as colval union all
    select 'pkey-04' as primary_key, 'value-04' as colval union all
    select 'pkey-05' as primary_key, 'value-05' as colval union all
    select 'pkey-06' as primary_key, 'value-06' as colval union all
    select 'pkey-07' as primary_key, 'value-07' as colval union all
    select 'pkey-08' as primary_key, 'value-08' as colval union all
    select 'pkey-09' as primary_key, 'value-09' as colval union all
    select 'pkey-10' as primary_key, 'value-10' as colval union all
    select 'pkey-11' as primary_key, 'value-11' as colval union all
    select 'pkey-12' as primary_key, 'value-12' as colval union all
    select 'pkey-13' as primary_key, 'value-13' as colval union all
    select 'pkey-14' as primary_key, 'value-14' as colval union all
    select 'pkey-15' as primary_key, 'value-15' as colval union all
    select 'pkey-16' as primary_key, 'value-16' as colval union all
    select 'pkey-17' as primary_key, 'value-17' as colval union all
    select 'pkey-18' as primary_key, 'value-18' as colval union all
    select 'pkey-19' as primary_key, 'value-19' as colval union all
    select 'pkey-20' as primary_key, 'value-20' as colval
)
select
    t1.primary_key,
    t1.colval
from testtab t1
order by random()
limit 4;

Comment: Thank you very  much for your time and your help. Just to make sure that I understand the concept correctly, in the solution you suggest, if the table has N rows then there will be N calls of the random() function, that is, one random() call per row, then the N generated random values are sorted based on the default ascending order, yet because they were attributed to rows randomly, the sort doesn't always put rows in contiguous blocks and at the end, using LIMIT 4 just retrieves the first four of the sorted random values, and therefore 4 randomly selected rows. Do I understand correctly?
Comment: Thank you very much. I didn't know this method. Very intersting and quite useful!


Python South not picking up changes made in add_to_class() method

Question: I've added a field to the main Django 'User' model, by inserting a User.add_to_class() to Askbot's models.init
the added code is the following:
#askbot-devel/askbot/models/__init__.py
User.add_to_class('show_active_status', models.BooleanField(default = False))

then I run South's schemamigration
$ ./manage.py schemamigration askbot --auto
Nothing seems to have changed.

As you can see it doesn't pick up the changes.
I've checked the database and nothing has changed at all.
Additionally, I've also checked that I'm working on the right init file because, besides tha fact that South is not picking up changes, when I run the server after saving the above changes it all breaks up, giving an error: 'current transaction is aborted, commands ignored until end of transaction block'
What am I doing wrong?
Answer: Despite living in your askbot app, this code changes a model in Django's auth app. So according to south, the only thing that's changed is the auth app. Given that this app is not managed by South, you run into a problem.
I'd not recommend using migrations for third-party apps for the simple fact that the migrations get lost in moving the code to different deployment environments.
Your best bet for an existing deployment is to manually add the column to the User database table. New deployments will automatically pick up the new field and create the column during the syncdb command.