Android TV move focus between RecyclerViews

Question: I am building an Android TV app with the following layout:

Both lists on the left and on the right are RecyclerViews with vertical LinearLayoutManagers, Header view is static. Navigation with D-PAD works fine within one list, but when switching from one list to another there are issues. Focus moves from, say list1's item 5 to list2' item 5. When list 2 is short has less than 5 items, it just loses focus.
I want the last focused item index saved and when the user navigates list1-list2-list1 the item with that index to gain focus again and also prevent the views from losing focus. Is there any good solution to this?
Required behaviour:
user navigates to the top of the list and presses "UP" - focus stays where it was, nothing happens.
user navigates to the bottom of the list and presses "DOWN" - focus stays where it was, nothing happens.
user navigates from list1 to list2 - the item, that previously had focus in list2, gains focus or item0 gains focus if none was focused previously.
main layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <ImageView
        android:id="@+id/iv_background"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusable="false"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="27dp"
        android:layout_marginLeft="48dp"
        android:layout_marginRight="48dp"
        android:layout_marginTop="27dp"
        android:orientation="horizontal"
        >

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:orientation="vertical"
            >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:focusable="false"
                android:orientation="horizontal"
                >

                <ImageView
                    android:id="@+id/iv_poster"
                    android:layout_width="@dimen/episodes_list_item_height"
                    android:layout_height="@dimen/episodes_list_image_width"
                    />

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:orientation="vertical"
                    >

                    <TextView
                        android:id="@+id/tv_title"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        />

                    <FrameLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        >

                        <TextView
                            android:id="@+id/tv_release_date"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="left"
                            />

                        <TextView
                            android:id="@+id/tv_rating"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="right"
                            android:drawableLeft="@drawable/ic_star_white_24dp"
                            android:drawablePadding="@dimen/view_padding_small"
                            />

                    </FrameLayout>

                    <TextView
                        android:id="@+id/tv_description"
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="1"
                        />
                </LinearLayout>

            </LinearLayout>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/rv_seasons_list"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:focusable="false"
                />

        </LinearLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_episodes_list"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:focusable="false"
            />

    </LinearLayout>

</FrameLayout>

item layout, used for both lists:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/seasons_list_item_height"
    android:orientation="horizontal"
    android:focusable="true"
    >

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:layout_weight="1"
        />

    <TextView
        android:id="@+id/tv_episodes_count"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:ems="10"
        />

</LinearLayout>

Comment: could you post your xml files ?
Comment: so you want to move focus between both of your recycler view right could you explain bit more ?
Comment: right. i want to remember last focused position in recyclerviews. and when the user navigates from list1 to list2 and back to list1 i want to restore previously focused item of list1. the same goes for list2. also i want the focus not to move when the user presses 'up' on d-pad when item1 in list1 is focused and so on
Comment: have u tried any code to get focus for last position if not then check this link http://stackoverflow.com/questions/26682277/how-do-i-get-the-position-selected-in-a-recyclerview
Comment: jaydroider, selection and focus are different things. i can get the last focused position, but can't see a way to restore it.
Comment: you mean when you r navigating with remote then u facing this problem ?
Comment: exactly that. i'm using the standard nexus player remote (a d-pad)
Comment: could you please add more details to your suggestion? what exactly should i try?
Comment: Have you tried making them siblings? I have something similar to what you are doing and it works fine for me as long as they are in the same relative layout. I know it makes it a pain to style but at least things will stay in focus.
Comment: check out my answer to see a better way to do it
Answer: Digging through Google's Leanback library sources answered my payers.
If you face the same trouble, just wrap your RecyclerViews with this and don't forget to set
private boolean mPersistFocusVertical = false;

if you want the focus to be persisted when searching horizontally, like in my layout.
Comment: it's PersistentFocusWrapper class from Google's v17-leanback library. You can search for the latest version. Now it's available here: https://android.googlesource.com/platform/frameworks/support/+/master/v17/leanback/src/android/support/v17/leanback/widget/PersistentFocusWrapper.java
Comment: How did you use this private class? Can you give an example code on the solution, do you mean wrapping it in the xml?
Comment: just copy the code. it has no dependencies with private access. it works for me.
Comment: Link doesn't works (404). Could you please to provide correct link?
Comment: @ЄвгенГарастович Did you get any crashes with it? I get it: http://stackoverflow.com/questions/39856750/how-to-implement-scrolling-in-recyclerview-on-android-tv
Answer: Modify your main layout as below.
For directional navigation we can add properties like nextFocusDown, nextFocusLeft, nextFocusRight, nextFocusUp to focus relevant items as per our requirements.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<ImageView
    android:id="@+id/iv_background"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="false"
    />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="27dp"
    android:layout_marginLeft="48dp"
    android:layout_marginRight="48dp"
    android:layout_marginTop="27dp"
    android:orientation="horizontal"
    >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:orientation="vertical"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:orientation="horizontal"
            >

            <ImageView
                android:id="@+id/iv_poster"
                android:layout_width="@dimen/episodes_list_item_height"
                android:layout_height="@dimen/episodes_list_image_width"
                />

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical"
                >

                <TextView
                    android:id="@+id/tv_title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    />

                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    >

                    <TextView
                        android:id="@+id/tv_release_date"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="left"
                        />

                    <TextView
                        android:id="@+id/tv_rating"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="right"
                        android:drawableLeft="@drawable/ic_star_white_24dp"
                        android:drawablePadding="@dimen/view_padding_small"
                        />

                </FrameLayout>

                <TextView
                    android:id="@+id/tv_description"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    />
            </LinearLayout>

        </LinearLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_seasons_list"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:focusable="false"
            android:nextFocusDown="@+id/rv_episodes_list"
            />

    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_episodes_list"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:focusable="false"
        android:nextFocusUp="@+id/rv_seasons_list"
        />

</LinearLayout>

Hope this will solve your problem !!
Comment: Thank you for the idea, but this doesn't help. I need the focus to stay were it is when it reaches the top/bottom of the list. And it didn't affect moving focus left/right at all.
Comment: put screen shot of your app with relevant rows you have.
Comment: i've used your code to create a little test project here https://firstname.lastname@example.com/jgarin/tv-test.git when you press and hold d-pad's up/down focus moves from one list to another and previously focused position is not saved/restored
Comment: try to get last focus position of list 1 and store in share preferences.
Comment: getting and storing is not a problem, i can handle that. but restoring focus is. any ideas on that?
Comment: possible duplicate of http://stackoverflow.com/questions/27194044/how-to-properly-highlight-selected-item-on-recyclerview check this
Answer: To make my lists remember focused position, I used VerticalGridView from leanback library instead of RecyclerView. and it worked perfectly fine.
Comment: Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer).


What is the exact value of the radius in the Six Disks Problem?

Question: The disk covering problem: Find the smallest radius $r(n)$ required for $n$ equal disks to completely cover the unit disk. For $n=5,6$, the best layouts are,
$\hskip2.2in$
$\hskip2.2in$
with $r(5) \approx 0.609,\; r(6) \approx 0.5559$, and $r(5)$ as a root of an octic,
$$6^4x^8+2112x^7-3480x^6+1360x^5+1665x^4-1776x^3+22x^2-800x+5^4=0$$
with an unsolvable Galois group. (I already checked.)
However, for $n=7,8,9,10$, the best has a symmetric layout similar to,
$\hskip2.2in$
with $\displaystyle r_s(n) = \frac{1}{1+2\cos\big(\frac{2\pi}{n-1}\big)}$ hence is solvable in radicals.

Q: But what is the minimal polynomial of $r(6) \approx \frac1{1.798} \approx 0.5559 $?

I've searched the online literature and I know it is by Károly Bezdek but it seems it's hard to find its minpoly. (Chances are it will also have an unsolvable Galois group but I'm curious to test it.)

$\color{red}{Update:}$ Ed Pegg brought to my attention that $n=12$,
$\hskip2.2in$
has $r(12) \approx  \frac1{2.769} \approx  \frac1{x+1} \approx 0.361$ where $x$ is a root of the simple $x^3-2x-2=0$, hence is again solvable in radicals. Compare to the symmetric but non-optimal $r_s(12) \approx 0.372$.
Comment: A [MO post](https://mathoverflow.net/questions/97939/covering-disks-with-smaller-disks) for the case $n=7$.
Comment: The second link (to a page at `stetson.edu`) is currently broken.
Answer: The minimal polynomial for $r(6)$ is
$$\color{red}{7841367r^{18}-33449976r^{16}+62607492r^{14}-63156942r^{12}+41451480r^{10}-19376280r^8+5156603r^6-746832r^4+54016r^2+3072}$$
I obtained this result in a marathon coding session across Chinese New Year, going well into the night of 15 February and continuing into the afternoon of 16 February.

By symmetry, we can restrict ourselves to one half of the configuration, which I will take to be the upper half-plane. Then consider the smaller discs' centres and their points of intersection (both among themselves and with the outer unit disc, centred on the origin), and the following diagram is obtained:

The black dots are the key points in the problem; we will derive expressions for their coordinates later. Each red line segment is a radius of some small disc, so all of them have the same length, which I will call $r$. The red dot is the centre of the disc in the top-left corner of the diagram, and the two segments incident to it form a diameter.
Besides $r$, we define two other variables $a$ and $b$, then set the coordinates of the two key points on the $x$-axis as $A=(-a,0)$ and $B=(b,0)$ as shown in the diagram ($a,b,r$ are positive). The coordinates of the other key points can then be derived as follows (I will use $1$ to refer to the point with coordinates $(x_1,y_1)$ and so on, while $O$ assumes its usual meaning of the origin):

The law of cosines gives $\cos\angle1OA=\frac{1+a^2-r^2}{2a}$. Because this is also the supplement of $1$'s argument, we have
$$x_1=-\frac{1+a^2-r^2}{2a}$$
$y_1$ is then straightforward:
$$y_1=\sqrt{1-x_1^2}$$
We can do the same thing with $\angle2OB$ to get
$$x_2=\frac{1+b^2-r^2}{2b}\qquad y_2=\sqrt{1-x_2^2}$$
$3$ lies on the perpendicular bisector of $A$ and $B$:
$$x_3=\frac{b-a}2$$
If the altitude is dropped from $3$ to the $x$-axis, with $F$ as the foot, then $\angle3FB$ is right, so the Pythagorean theorem gives
$$y_3=\sqrt{r^2-\left(\frac{a+b}2\right)^2}$$
The coordinates of $4$ can now be derived as simply as adding two vectors (in this case, $\vec{B3}$ and $\vec{B2}$):
$$x_4=x_2+x_3-b\qquad y_4=y_2+y_3$$
$\angle BO5=\angle BO1-\angle5O1$, where it is trivial to compute $\sin\angle5O1=2r\sqrt{1-r^2}$ and $\cos\angle5O1=1-2r^2$. We already worked out $\sin\angle BO1$ and $\cos\angle BO1$ as $y_1$ and $x_1$ respectively, so applying angle addition formulae gives
$$x_5=\cos\angle BO5=x_1(1-2r^2)+y_1(2r\sqrt{1-r^2})\\
y_5=\sin\angle BO5=y_1(1-2r^2)-x_1(2r\sqrt{1-r^2})$$

For the given configuration to be valid, the distance between $4$ and $5$ must be $r$. This can be expressed as a constraint:
$$g(a,b,r)=(x_5-x_4)^2+(y_5-y_4)^2-r^2=0$$

Express the problem of finding $r(6)$ as an optimisation problem:
$$\text{minimise }f(a,b,r)=r\text{ subject to }g(a,b,r)=0$$
Now try to solve this using Lagrange multipliers. The Lagrangian is
$$f-\lambda g=r-\lambda g(a,b,r)$$
and all its first partial derivatives must be zero, giving a system of four equations:
$$-\lambda\frac{\partial g}{\partial a}=-\lambda\frac{\partial g}{\partial b}=1-\lambda\frac{\partial g}{\partial r}=-g=0$$
However, if $\lambda=0$ then the third equation reduces into the absurdity $1=0$. Thus $\frac{\partial g}{\partial a}=\frac{\partial g}{\partial b}=0$ and combining these with the fourth equation yields a system of three equations in three unknowns:
$$\frac{\partial g}{\partial a}=\frac{\partial g}{\partial b}=g=0$$
It is this last system that we will solve.

Before we proceed with solving for the minimal polynomial of $r(6)$, we need to convert $g$ into a polynomial form, even though this introduces extra solutions. This is done by removing square roots. If
$$g=p+m\sqrt q=0$$
where $p,m,q$ are arbitrary expressions, the root over $q$ can be removed by bringing $m\sqrt q$ to the RHS, squaring and moving back:
$$g=p^2-m^2q=0$$
All the original solutions of $g$ are retained in this lifting process, but care must be taken to keep the degrees small. Lifting with $q=-a^4+2a^2r^2+2a^2-r^4+2r^2-1$, then $q=-b^4+2b^2r^2+2b^2-r^4+2r^2-1$, already allows several small polynomials to factor out, which I removed by comparing with numerical approximations for the optimal $r,a,b$. I then performed one last lift with $q=\sqrt{-r^2+1}\sqrt{-a^2-2ab-b^2+4r^2}$, whereupon $g$ was an irreducible polynomial of total degree $22$ with $286$ monomials. The degree of $r$ in $g$ is always even, so I substituted $s=r^2$, lowering the total degree to $18$.

The Lagrangian system was finally solved in Singular via resultant computations: $a$ was eliminated from each of the pairs $\left(g,\frac{\partial g}{\partial a}\right)$ and $\left(g,\frac{\partial g}{\partial b}\right)$ and I factored out irrelevant factors as before. The two relevant polynomials in $s$ and $b$ were then combined into a third pair and $b$ was eliminated, giving a final univariate polynomial in $s$ of degree $616$. The gold at the end of the rainbow – $s$'s true minimal polynomial – is of degree $9$, and replacing back $s=r^2$ gives the degree-$18$ minimal polynomial of $r(6)$ that I wrote at the very top of this answer:
$$7841367r^{18}-33449976r^{16}+62607492r^{14}-63156942r^{12}+41451480r^{10}-19376280r^8+5156603r^6-746832r^4+54016r^2+3072$$

By eliminating $s$ instead of $b$ in the resultant of $c_1$ and $c_2$, $b$'s minimal polynomial can be obtained:
$$7841367b^{18}-7610085b^{16}-71679924b^{14}-45430974b^{12}+58085154b^{10}+26617188b^8-12289505b^6-5546269b^4+2814464b^2-327680$$
By eliminating $b$ instead of $a$ when computing the first two resultants, $a$'s minimal polynomial can be obtained:
$$7841367a^{18}-90811125a^{16}+344734812a^{14}-376618374a^{12}-189418782a^{10}+373328980a^8+71968935a^6-130579181a^4+27056736a^2-800000$$
$r$ is indexed as A299695 in the OEIS.

3 May 2021: Three years after this monumental computation I had learned enough of Gröbner bases and Singular's libraries to get a cleaner derivation of the minimal polynomial for $r(6)$. Instead of iteratively constructing points from a radius variable and two base points, I added more variables and eliminated some of them at once to get the same reduced $g$ as described above faster. A few aspects of the last stage were also sped up, such that the new combined code takes only $2.5$ minutes on the Python portion and $3$ minutes on the Singular portion.
This is the new model:

The constraints are $1A,B2,45,15$ with the vertex labels referring to the 2018 picture above; eliminating $t,u,v$ via the Gröbner-based procedure elim gives $g$ after one division by $r$ (this $r$ is equivalent to the $s$ of the old derivation). A resultant computation is still used for eliminating a variable (this time $b$) from $\left(g,\frac{\partial g}{\partial a}\right)$ and $\left(g,\frac{\partial g}{\partial b}\right)$, and getting $a$ and $b$'s minimal polynomials, but the final elimination for $r$ uses elim.
Comment: I do hope I didn't interrupt your reunion dinner. I was waiting for your answer the whole day today and was thinking, "....that sure is a loooong dinner." I visited Singapore years ago, even got myself one of those "Singapore is a _fine_ city" t-shirts. Thanks for the excellent and detailed answer, and kung hei fat choi. :)
Comment: P.S. I also checked the $9$th deg equation with Magma and it says the Galois group has order $362880$ and is the "Symmetric group G acting on a set of cardinality 9", hence is unsolvable.
Comment: @TitoPiezasIII Well, to put it more clearly: I started on the problem at midnight 16 February UTC+8, after _one_ reunion dinner, and had obtained the $g$ polynomial by the time I was too tired to stay awake longer. From late morning to early afternoon, I had a reunion _lunch_, during which I stumbled on Singular and got the requisite minpols. I spent another hour cleaning up my solution, and another three hours to type it in the answer box. The whole thing took 18 hours.
Comment: My thanks again for the significant effort you put into this question. I didn't realize what it would involve and I only hope you had "a whale of a time", as you put it. I'm surprised Mathworld didn't include the $18$th-deg minpoly since, as a nonic, it really isn't that long. I'm also wondering how in the world Károly Bezdek found $r(6)$ back in the 1970s, with little access to computers.


Como abrir na mesma guia um documento HTML vindo do fetch?

Question: Estou criando um site de enigmas em que a cada enigma, o usuário deverá inserir uma resposta no prompt e essa resposta será enviada ao servidor por requisição POST com a função fetch.
No lado do servidor, a resposta será verificada e caso ela esteja correta, eu retorno uma nova página HTML contendo um novo enigma. Caso a reposta do usuário esteja errada, o servidor retorna o código 403 para dizer que era uma resposta errada.
Já no lado do cliente, eu quero verificar se a resposta era certa ou errada. Se a resposta for incorreta será emitido um alert e se a resposta estiver certa, eu quero que o navegador renderize a nova página enviada pelo servidor.
O problema é que eu não sei como faço para redirecionar o cliente para essa página que o servidor enviou.
O que eu estava fazendo antes era escrever o texto recebido com o document.write, mas chega uma hora que não dá certo pois o conteúdo dos arquivos se acumulam na página.

function sendAnswer(){

    const answer = prompt("Digite sua resposta:").toLowerCase();
    if(!answer){return;}

    fetch("/", {
        method: "post",
        headers: {"Content-Type": "application/json"},
        body: JSON.stringify({answer: answer, fase: sessionStorage.fase})

    }).then(function(response){

        if (response.status != 200){
            alert("Resposta Errada!");
            return;
        }

        response.text().then(function(text){
            document.write(text);
        });

    });
}

O que eu quero é mudar o conteúdo da página por completo pelo arquivo HTML recebido do servidor. Como posso fazer isso ?
Comment: O HTML que o servidor retorna contém um `` (é um documento completo)? Ou é somente um trecho do HTML? Na página que o usuário submete a resposta, você tem alguma `div` que pode ser usada para inserir o HTML respondido pelo servidor? Ou você realmente deseja trocar o conteúdo todo da página?
Comment: É um documento completo. Eu gostaria de trocar todo o conteúdo da página como se eu estivesse fazendo um redirecionamento com o `location`.
Answer: Por este trecho:

O que eu estava fazendo antes era escrever o texto recebido com o document.write, mas chega uma hora que não dá certo pois o conteúdo dos arquivos se acumulam na página.

Creio que você conseguiu trocar o conteúdo da página pela resposta da requisição assíncrona. O seu problema então deve ser como limpar o documento antes de executar um outro document.write. Nesse caso, basta utilizar antes do document.write, o método document.open, que remove todos os nós do DOM presentes no documento.
Algo assim:
response.text().then(function(text) {
  document.open();
  document.write(text);
});

Veja exemplos similares. Primeiro, sem o document.open:

setInterval(() => {
  document.write(Date.now() + '
');
}, 750);

Com o document.open antes do document.write:

setInterval(() => {
  document.open();
  document.write(Date.now() + '
');
}, 750);
Comment: Obrigado Luiz, isso funcionou mas é possível adicionar na resposta como fazer isso só que sem apagar o arquivo anterior? Tipo assim, quando o usuário clicar no botão de voltar (setinha para esquerda do navegador) ele ir para a página anterior (antes da página recebida do `fetch`).
Comment: Quando você usa o `document.write` você não há como recuperar o estado anterior da página (não é uma nova página, é a **mesma** página, só que sobrescrita). O único meio de poder "voltar à página anterior" seria armazenar o HTML atual em uma variável antes de alterá-lo.
Comment: Ao que me parece, talvez seja mais interessante no seu caso não utilizar AJAX nesse caso, mas sim requisições `POST` por ``. Elas te permitem a mudança da página muito mais facilmente, além de permitirem, de fato, voltar à _página anterior_.


how to send attachment with uuencode?

Question: I am trying to send an attachment with uuencode, but I do not receive anything in my inbox:
<code>uuencode test.txt test.txt | mail -s "test" firstname.lastname@example.com
</code>
I just get a response saying: "You have new mail in /var/mail/user
Does anyone know how to correct this? I too realize that uuencode might not be used as much now, so if anybody could recommend sending attachments another way, it would be much appreciated.
Perhaps it is not recognizing the txt file input?
Thank you.
Answer: The way you are doing it looks correct. check logs what is really happen after sending. If you get anything, then it looks more than mail server error.
also if you do get something, but not the attachment-then problem would be in MUA - e.g. program you are using to read emails. Some of them get smart and remove the uu part, but not smart enought to add it as attachment.
mailx should show the uu part unchanged.
UPDATE
for alternative chech this:
https://unix.stackexchange.com/questions/102092/mail-send-email-with-attachment-from-commandline

how to send attachment with uuencode?


Sou obrigado a definir o tamanho de uma LinkedList em Java?

Question: Uma vez que a LinkedList em Java é uma lista duplamente encadeada, entendo que eu poderia inserir um elemento em qualquer índice que eu quisesse, certo? Ou errado?
Por exemplo, eu crio uma lista e uso o método add(indice, elemento) para adicionar o primeiro elemento num índice com rótulo 5.
Ou não é assim, e eu só posso adicionar no início ou fim da lista e só posso colocar um elemento no índice 5 se tiver pelo menos 5 deles na lista?
Answer: Basta fazer um teste simples:
LinkedList<String> list = new LinkedList<>();
list.add(1, "cde");

Esse código lança um IndexOutOfBoundsException, e o motivo está na documentação.
O construtor sem argumentos cria uma lista vazia (ou seja, com tamanho zero):
LinkedList<String> list = new LinkedList<>();
System.out.println(list.size()); // 0

E o método add, quando recebe um índice, lança a exceção IndexOutOfBoundsException se o valor do índice for menor quer zero ou maior que o tamanho da lista:

IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size())

Como o tamanho da lista é zero, passar 1 para o método add lança a exceção.

Resumindo, só podemos usar índices que estejam entre zero e o tamanho da lista:
LinkedList<String> list = new LinkedList<>();
list.add("abc");
list.add("def");
list.add(1,"xy");
System.out.println(list.size()); // 3
System.out.println(list); // [abc, xy, def]

list.add(3, "ultimo"); // índice é igual ao tamanho, é o mesmo que inserir no final
System.out.println(list); // [abc, xy, def, ultimo]

list.add(5,"abcde"); // IndexOutOfBoundsException

Comment: Isso que eu quis dizer. Ela não cria elementos em índices esparsos dinamicamente.
Answer: Isto está bem confuso, usa termos que provavelmente só você sabe o que significa.
Uma lista ligada não deixa de ser uma lista, você pode inserir novos elementos dentro dela no começo, no fim e no meio, como em toda lista. A diferença dela para outras listas é o custo de fazer isto. Uma das vantagens da lista ligada é poder inserir no meio com complexidade O(1), ou seja, é constante, a mais baixa possível. Claro, ele tem esse custo se você estiver na posição que deseja, caso não esteja, e frequentemente não está tem um custo de deslocamento até lá, e esse custo que em muitas outras listas é O(1), na lista ligada o custo é O(n). O método que insere no meio da lista é o add(). No que que a assinatura dele exige um índice, esse índice é para ser usado em um get() interno. O que acontece se você colocar um número de índice não existente em um get(), dá erro de faixa, certo?
Se a lista tem 5 elementos não pode inserir nada além do quinto elemento, ou seja não pode inserir no índice 6 (pode adicionar que é uma operação que não especifica onde vai inserir, é no final, ponto). Isto vale para qualquer lista, é física, você não pode colocar algo no índice 6 se só tem 5 elementos (vai do índice 0 ao 4, pode inserir no 5 porque é o mesmo que adicionar). Inserir é uma operação de chegar no elemento desejado e adicionar um elemento novo ali.
Note que criar um lista com um número de elementos (você pode ou não fazer isto) não garante que terá objetos nele, os objetos podem ser nulos, você teria que criar elementos e inicializá-los. Mas se desejar pode só adicionar elementos depois de criar a lista. O primeiro tem que ser no final já que não tem outros elementos. No início funciona também, não pode inserir no meio pela falta de elementos. Depois de existir pelo menos um elementos já pode inserir  no meio com o add(index, value).
Comment: Rótulo de índice = quis dizer o índice em si: o endereço do elemento na lista.


How to change the sender's name or e-mail address in mutt?

Question: How can I change the sender's name or e-mail address (From: header) used by mutt?
Comment: Do you want to change it once, in a configuration file, or you want to change it while composing an e-mail? By the way, this is probably better asked on SuperUser.
Comment: echo "File $Filename not present" | mutt -s '$MailSubject' -c "firstname.lastname@example.com"."xyz.yahoo.com"
Answer: Normally, mutt sets the From: header based on the from configuration variable you set in ~/.muttrc:
set from="Fubar <foo@bar>"

If this is not set, mutt uses the EMAIL environment variable by default. In which case, you can get away with calling mutt like this on the command line (as opposed to how you showed it in your comment):
EMAIL="foo@bar" mutt -s '$MailSubject' -c "abc@def"

However, if you want to be able to edit the From: header while composing, you need to configure mutt to allow you to edit headers first. This involves adding the following line in your ~/.muttrc:
set edit_headers=yes

After that, next time you open up mutt and are composing an E-mail, your chosen text editor will pop up containing the headers as well, so you can edit them. This includes the From: header.
Comment: I'd like to add that even without `set edit_headers=yes` you can press the `edit-from` shortcut (default: ESC f) to edit the from field.
Comment: Mutt also allows specifying an alternate muttrc file to use. Therefore for a one-shot change to the from address, create a new file: `echo set from="Fubar " > temprc` and then use it: `mutt -s "my subject" -F testrc email@example.com`
Comment: The file ***is*** `~/.muttrc` (i.e. `/home/$USERNAME/.muttrc`). You edit it with a text editor. If its not there, you create it. I wouldn't call it shell scripting, its just opening an editor, and writing a line in a file ;)
Comment: Setting `set use_envelope_from = yes` in .muttrc finally worked for me. This adds the -f option when calling sendmail to deliver the mail, forcing it to use the same address for the envelope as for the From: header field.
Comment: I'm new to shell scripting..
Can you explain how to change the configuration file??
and where it is available???
Comment: @AndrewSavinykh the link is dead :(
Comment: @3kstc seems like it is. Feel free to fix it!
Answer: If you just want to change it once, you can specify the 'from' header in command line, eg:
mutt -e 'my_hdr From:email@example.com'

my_hdr is mutt's command of providing custom header value.
One last word, don't be evil!
Comment: It works thanks. e.g: `echo "I love you body" | mutt -a "/tmp/file.zip" -s "ZIP" -c firstname.lastname@example.com -e 'my_hdr From:email@example.com' -- email@example.com`
Comment: Note the default domain (email@example.com) will be the system's `hostname`. [Changing the `hostname` on aws is easy](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/set-hostname.html), I think on other systems, it should be similar.
Answer: before you send the email you can press <ESC> f (Escape followed by f) to change the From: Address.
Constraint: This only works if you use mutt in curses mode and do not wan't to script it or if you want to change the address permanent. Then the other solutions are way better!
Comment: I wrote a response before realizing the `from` was wrong (I almost never use mutt). Saved my bacon, thanks!
Answer: One special case for this is if you have used a construction like the following in your ~/.muttrc:
# Reset From email to default
send-hook . "my_hdr From: Real Name <firstname.lastname@example.com>"

This send-hook will override either of these:
mutt -e "set firstname.lastname@example.com"
mutt -e "my_hdr From: Other Name <email@example.com>"

Your emails will still go out with the header:
From: Real Name <firstname.lastname@example.com>

In this case, the only command line solution I've found is actually overriding the send-hook itself:
mutt -e "send-hook . \"my_hdr From: Other Name <email@example.com>\""

Answer: For a one time change you can do this:
export firstname.lastname@example.com'; mutt -s "Elvis is dead" firstname.lastname@example.com

Comment: Thanks.  I put   export email@example.com'  in .bashrc and now it's permanent.
Comment: @Andrew How did you put it there? Did you just `nano` the file and append that one liner at the bottom of the file - or did you put other code around it?
Comment: @3kstc just put the export statement as I wrote it in you .bashrc - but be sure .bashrc is being sourced or else the variable EMAIL will not be set.  This may be the case when invoking mutt using cron or ssh or other shells (that are not bash) as all of the possible user profile related files may not be executed depending on how the shell is started
Comment: Note that `firstname.lastname@example.com' mutt ...` is sufficient (no `export`, no `;`). There is no need to export the `EMAIL` environment variable permanently. The syntax I show sets the environment variable for a single program invocation.
Answer: Step 1: Locate Muttrc which in case of Oracle Linux 7.6 could be found in /etc
Step 2: Open the file in vi editor  to add the below entries viz. vi /etc/Muttrc
Step 3: Contents to be added or/and uncommented
set from = "firstname.lastname@example.com"
set realname = "Realname of the user"
Step4: Save the file and exit. Also attempt sending an email with the syntax that follows,
echo "$Body of the email" | mutt -a "$name_of_the_attachment" -s $subject_line_of_the_email" -- $email@example.com
Answer: 100% Working!
To send HTML contents in the body of the mail on the go with Sender and Recipient mail address in single line, you may try the below,
export EMAIL="firstname.lastname@example.com" && mutt -e "my_hdr Content-Type: text/html" -s "Test Mail" "firstname.lastname@example.com" < body_html.html

File: body_html.html

 Test Mail

This is a test mail!

Note: Tested in RHEL, CentOS, Ubuntu.
Comment: There is no error. It just doesn't work. The correct solution is to use `set email@example.com` in `~/.muttrc`


Hide/remove js-view-dom-id in just one specific view

Question: I found, that the div with the id "js-view-dom-id" comes from views-view.html.twig. As I want to render one view output without any of Drupals HTML markup, I already hunted down the contextual links, which I can turn of for a specific view. Then I set the view format to an unused format, and removed all unwanted HTML markup from this views template as well.
Now I get plain {{ content.field_x.0 }}-output from this view. The only element left I need to get rid of is before mentioned "js-view-dom-id". I could remove this from views-view.html.twig, but then I would remove it from all views.
Is there any way to remove this markup for only one specific view? To be precise, I am using a custom content view mode, if that makes any difference.
Thank you very much in advance!
@edit:
Twig in views-view.html.twig looks like this:
{%
set classes = [
dom_id ? 'js-view-dom-id-' ~ dom_id,
]
%}

Is there any way to do something like:
{% if view mode is xy %}
// Do nothing
{%  else %}

{% endif %}

Answer: Yes, you can easily create an override for views-view.html.twig which will only apply to a single View.
Taking the example from Views template files:

View, named foobar. Style: unformatted. Row style: Fields. Display: Page.

views-view--foobar--page.html.twig
views-view--page.html.twig
views-view--foobar.html.twig
views-view.html.twig

So if your View is called "articles", you can use views-view--articles.html.twig to override the template for the whole View, views-view--articles--page.html.twig to target just a display named page for the same view, and so on.
Comment: Thank you very much. That works flawlessly! Sorrily I can't mark your anser as the right one, due to not having enough reputation here.


How to call request every x seconds in sapui5

Question: Hi i'm triying to call a request biding to my table and refresh every x seconds
        getInfo : function() {
        variables ...
        oDataModel.read("/ReservaSet?$filter=Asesor eq '" + this.varGlob.asesor + "'", {
            success: function(oData, response) {
            var oResults = oData.results;
            oModel.setData(oData.results);
            tablaCitas.setModel(oModel);
             },
            Error: function(oError){
            }
        });
        this.closeDialog();

it is possible? i need to call it every 5 minutes and refresh my table
i read that i could use setInterval or setTimeout but how this is call it again and again if my function it's fired by a button ?
Comment: Does this answer your question? [Change the cells of a table in an interval time using OData model](https://stackoverflow.com/questions/69379449/change-the-cells-of-a-table-in-an-interval-time-using-odata-model)
Answer: Try using    sap.ui.core.IntervalTrigger
with an example here
In a method you could add a listener function. You could start the triggering by setting the interval to the msecs (5mins: setInterval(300000)) when you press your button (if I understand this correctly to be your requirement) or during the onInit controller lifecycle event.
self = this;
self.heartbeatTrigger = new sap.ui.core.IntervalTrigger(0);
self.heartbeatTrigger.addListener(function(){
    self.beat();
});

Your beat function could do the "Refresh" work.
beat : function(){
    var self = this;
    console.log('beat..');
    self.getInfo();
},

Comment: Thanks for the answer. How do we remove the listener after a trigger?
Comment: @Vishnukk removeListener - the documentation link above refers


Problema com ruby on rails

Question: Estou com um erro de compilação no rails porém não dá para colocar todos os erros que aparece na tela do prompt de comando, mas nas linhas finais aparecem estas informações

[NOTE]
You may have encountered a bug in the Ruby interpreter or extension libraries. Bug reports are welcome. For details:
http://www.ruby-lang.org/bugreport.html
This application has requested the Runtime to terminate it in an
unusual way. Please contact the application's support team for more
information.

minha versão do ruby - ruby 2.3.3p222 (2016-11-21 revision 56859) [i386-mingw32]
minha versão do rails - Rails 5.1.4
Comment: Deu certo, obrigado. Só por curiosidade o erro que estava dando está comentado, mais para baixo.
Comment: Sem o log não dá pra ter certeza, mas parece ser um problema com ruby. Está usando Windows? Tente reinstalar.
Answer: O erro que está dando é este aqui.
maior_ou_menor.rb: [BUG] Segmentation fault
ruby 2.3.3p222 (2016-11-21 revision 56859) [i386-mingw32]
-- Control frame information -----------------------------------------------
c:0001 p:0000 s:0002 E:0001b0 (none) [FINISH]
-- C level backtrace information -------------------------------------------
C:\WINDOWS\SYSTEM32\ntdll.dll(NtWaitForSingleObject+0xc) [0x7717E7AC]
C:\WINDOWS\System32\KERNELBASE.dll(WaitForSingleObject+0x12) [0x75A5EB42]
C:\RailsInstaller\Ruby2.3.3\bin\msvcrt-ruby230.dll(rb_vm_bugreport+0xaa) [0x6305F9EA]
C:\RailsInstaller\Ruby2.3.3\bin\msvcrt-ruby230.dll(rb_bug_context+0x49) [0x62F06209]
C:\RailsInstaller\Ruby2.3.3\bin\msvcrt-ruby230.dll(rb_check_safe_obj+0x394) [0x62FD0794]
 [0x00401866]
C:\WINDOWS\SYSTEM32\ntdll.dll(RtlCaptureStackContext+0x1970d) [0x771B381D]
-- Other runtime information -----------------------------------------------

Loaded script: maior_ou_menor.rb
Loaded features:
0 enumerator.so
1 thread.rb
2 rational.so
3 complex.so
4 C:/RailsInstaller/Ruby2.3.3/lib/ruby/2.3.0/i386-mingw32/enc/encdb.so
5 C:/RailsInstaller/Ruby2.3.3/lib/ruby/2.3.0/i386-mingw32/enc/trans/transdb.so
6 C:/RailsInstaller/Ruby2.3.3/lib/ruby/2.3.0/i386-mingw32/enc/windows_1252.so
7 C:/RailsInstaller/Ruby2.3.3/lib/ruby/2.3.0/unicode_normalize.rb
8 C:/RailsInstaller/Ruby2.3.3/lib/ruby/2.3.0/i386-mingw32/rbconfig.rb
9 C:/RailsInstaller/Ruby2.3.3/lib/ruby/2.3.0/rubygems/compatibility.rb
10 C:/RailsInstaller/Ruby2.3.3/lib/ruby/2.3.0/rubygems/defaults.rb
11 C:/RailsInstaller/Ruby2.3.3/lib/ruby/2.3.0/rubygems/deprecate.rb
12 C:/RailsInstaller/Ruby2.3.3/lib/ruby/2.3.0/rubygems/errors.rb
13 C:/RailsInstaller/Ruby2.3.3/lib/ruby/2.3.0/rubygems/version.rb
14 C:/RailsInstaller/Ruby2.3.3/lib/ruby/2.3.0/rubygems/requirement.rb
15 C:/RailsInstaller/Ruby2.3.3/lib/ruby/2.3.0/rubygems/platform.rb
16 C:/RailsInstaller/Ruby2.3.3/lib/ruby/2.3.0/rubygems/basic_specification.rb
17 C:/RailsInstaller/Ruby2.3.3/lib/ruby/2.3.0/rubygems/stub_specification.rb
18 C:/RailsInstaller/Ruby2.3.3/lib/ruby/2.3.0/rubygems/util/list.rb
19 C:/RailsInstaller/Ruby2.3.3/lib/ruby/2.3.0/i386-mingw32/stringio.so
20 C:/RailsInstaller/Ruby2.3.3/lib/ruby/2.3.0/rubygems/specification.rb
21 C:/RailsInstaller/Ruby2.3.3/lib/ruby/2.3.0/rubygems/exceptions.rb
22 C:/RailsInstaller/Ruby2.3.3/lib/ruby/2.3.0/rubygems/defaults/operating_system.rb
23 C:/RailsInstaller/Ruby2.3.3/lib/ruby/2.3.0/rubygems/dependency.rb
24 C:/RailsInstaller/Ruby2.3.3/lib/ruby/2.3.0/rubygems/core_ext/kernel_gem.rb
25 C:/RailsInstaller/Ruby2.3.3/lib/ruby/2.3.0/monitor.rb
26 C:/RailsInstaller/Ruby2.3.3/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb
27 C:/RailsInstaller/Ruby2.3.3/lib/ruby/2.3.0/rubygems.rb
28 C:/RailsInstaller/Ruby2.3.3/lib/ruby/2.3.0/rubygems/path_support.rb
29 C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/did_you_mean-1.0.0/lib/did_you_mean/version.rb
30 C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/did_you_mean-1.0.0/lib/did_you_mean/core_ext/name_error.rb
31 C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/did_you_mean-1.0.0/lib/did_you_mean/levenshtein.rb
32 C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/did_you_mean-1.0.0/lib/did_you_mean/jaro_winkler.rb
33 C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/did_you_mean-1.0.0/lib/did_you_mean/spell_checkable.rb
34 C:/RailsInstaller/Ruby2.3.3/lib/ruby/2.3.0/delegate.rb
35 C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/did_you_mean-1.0.0/lib/did_you_mean/spell_checkers/name_error_checkers/class_name_checker.rb
36 C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/did_you_mean-1.0.0/lib/did_you_mean/spell_checkers/name_error_checkers/variable_name_checker.rb
37 C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/did_you_mean-1.0.0/lib/did_you_mean/spell_checkers/name_error_checkers.rb
38 C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/did_you_mean-1.0.0/lib/did_you_mean/spell_checkers/method_name_checker.rb
39 C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/did_you_mean-1.0.0/lib/did_you_mean/spell_checkers/null_checker.rb
40 C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/did_you_mean-1.0.0/lib/did_you_mean/formatter.rb
41 C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/did_you_mean-1.0.0/lib/did_you_mean.rb

[NOTE]
You may have encountered a bug in the Ruby interpreter or extension libraries.
Bug reports are welcome.
For details: http://www.ruby-lang.org/bugreport.html
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

Problema com ruby on rails


Simulating rate data (negative-binomial distribution)?

Question: I am attempting to simulate data that approximates rate data - that is: count data that generally fit a negative binomial distribution but also an offset term for survey effort.
I think I can simulate counts well using the negative-binomial function (rnbinom()), but then don't have a way to account for the offset term - which will be random with each survey. Put another way:
 What is the best way to simulate non-integer rate data?
 Is there a realistic way to simulate the offset term values?
 Do I need to use a different distribution beyond negative-binomial to generate a realistic range of non-integer values?

Background: Our surveys measure counts of individual per unit of survey effort (time), and the resulting rate is a positive non-integer ( >= 0). The survey count data seems well modeled with a negative-binomial distribution, and in a GLM framework, I would account for effort using survey time as an offset term. In the simulated data below, I generate a negative-binomial distribution to represent data within my actual survey. The offset term is simulated as a random uniform variable between 2-10 (the range of search times in minutes). Rate is then calculated as counts/time.
I plot histograms of both counts and rate to help demonstrate that rates here take many fractional values between integers. Because survey counts are often correlated with survey effort, it is critical that I ultimately use the rate data for analysis (i.e. figure 'B' below).

library(tidyverse)
theme_set(theme_classic())

d = data.frame(counts = rnbinom(n = 500,mu = 5,size = 1),  ## dispersion parameter 'theta' set to 1
               time = runif(500,2,10)) %>%
  mutate(rate = counts/time)

## Count data histogram
 ggplot(d, aes(x = counts))+
  geom_histogram(fill = 'peachpuff',color = 'black')+
  ylab('frequency')+
  scale_y_continuous(expand = c(0,0))+
  ggtitle('A: Histogram of Counts')

## Rate data histogram
ggplot(d, aes(x = rate))+
  geom_histogram(binwidth = .1, fill = 'dodgerblue1',color = 'black')+
  scale_x_continuous(breaks = seq(0,10,1))+
  scale_y_continuous(expand = c(0,0))+
  ylab('frequency')+
  ggtitle('B: Histogram of Rate')

Below I can readily simulate counts from my original survey data, but don't know how to properly simulate back data as a rate. For example, if I fit an intercept-only nbinom GLM , I can use the coefficient to simulate new negative-binomial distributions of counts that are very similar to the original data (i.e. uses a similar value for 'mu')
[I realize this seems circular in this example, but this is my approach with real data. First describe the mean value and dispersion 'theta' with a GLM, then simulate back datasets that mimic my original dataset]
I use this approach below both to generate back count data, but also by fitting a model with the offset term in order to simulate back a distribution that has the mean 'rate' from figure 'B'.

### Simulate back count data from the original survey data:

##describe mean value 'mu' by finding intercept
## 'theta'  could also be calculated
m1 = MASS::glm.nb(counts ~ 1, data = d)

# summary(m1)
# mean(d$counts)
# exp(m1$coefficients[1])

## simulated negative-binomial distribution using calculated 'mu'
d.sim = data.frame(new.counts = rnbinom(500,
                                        mu = as.numeric(exp(m1$coefficients[1])),  ## coef on log-scale, exponentiate to use
                                        size = 1))  ## holding dispersion parameter 'theta'  constant at 1

## Plot and compare with plot 'A' above
ggplot(d.sim, aes(x = new.counts))+
  geom_histogram(fill = 'peachpuff3',color = 'black')+
  ylab('frequency')+
  scale_y_continuous(expand = c(0,0))+
  ggtitle('C: Simulated Counts')

###########################################
###########################################

### Simulate back 'rate' data by including an offset term for effort in the GLM model
  ## the exponentiated coefficient should equal the mean of the raw rate data

m2 = MASS::glm.nb(counts ~ 1 + offset(log(time)), data = d)

# summary(m2)
# mean(d$rate)
# exp(m2$coefficients[1])

d.sim.2 = data.frame(new.counts = rnbinom(500,
                                        mu = as.numeric(exp(m2$coefficients[1])),  ## coef on log-scale, exponentiate to use
                                        size = 1))    ## holding dispersion parameter 'theta'  constant at 1

## compare these simulated 'rate' data with the non-integer 'true rate' data in figure D
ggplot(d.sim.2, aes(x = new.counts))+
  geom_histogram(binwidth = .1, fill = 'dodgerblue3',color = 'black')+
  scale_x_continuous(breaks = seq(0,10,1))+
  ylab('frequency')+
  scale_y_continuous(expand = c(0,0))+
  ggtitle('D: Simulated Rate')

So it is at this point that I've generated figure 'C' as a simulated dataset representing counts that I have observed in real life, which closely matches the original data in figure 'A'. The 'rate' data in figure 'D' is (necessarily) all integer values drawn from rnbinom(), and while the mean of figure 'D' is approximate to the mean of figure 'B', my sense is that these two distributions are not really equivalent.
So my questions again:

Is there a way that I could instead simulate data to match figure 'B' (non-integer rate data)?
Do you think that data in figure 'D' will work as an approximate to 'B' since the mean values (and dispersion) are similar?

For additional context, I'll be using the simulated datasets (many of them) to run other Monte-Carlo type simulation analysis (e.g. power analysis). I'm worried that if I use data generated in Figure 'D', it won't really represent what my actual survey data will be (figure 'B').
Comment: Why not use the time as a covariate in the negative binomial model, then divide the predicted values by the time to get the rate?
Comment: I'm not sure I understand. I am hoping to generate many new simulated datasets that resemble figure 'B' above. Does the suggestion to model time as a covariate allow me to generate random distributions centered on the mean of the data in figure B?
Answer: The way you generate your sample data (in place of your empirical data), does not align with the data generating process you describe. The count data from rnbinom(n = 500, mu = 5, size = 1) does not depend on the time. mu needs to be a function of the time variable, or else the counts are independent of time.
Also, setting size = 1 means there is no overdispersion (nor underdispersion), thus it should rather be called a Poisson distribution, which is a special case of the negative binomial distribution. But given your description of the DGP it sounds like there would be overdispersion in the empirical data.
To answer your first question, you see a code example below. Regarding your second question, no I don't think that would be a good idea.
library(tidyverse)
library(rstanarm)
options(mc.cores = parallel::detectCores())

n <- 1000

empirical <-
  tibble(
    time = runif(n, 2, 10),
    count = rnbinom(n = n, mu = time, size = 1) # Generate count data that actually depends on time
    ) |>
  mutate(rate = count/time)

m_stan <- stan_glm.nb(count ~ time, data = empirical)

simulated <-
  tibble(
    time = runif(n, 2,10),
  ) %>%
  mutate(
    count = posterior_predict(m_stan, ., draws = 1) |>
      as.vector(),
    rate = count/time
    )

d <- lst(simulated, empirical) |>
  bind_rows(.id = "data")

d |>
  select(data, count, rate) |>
  pivot_longer(c(count, rate)) |>
  ggplot() +
  geom_histogram(aes(value), binwidth = .2) +
  facet_grid(data ~ name, scales = "free")

Created on 2022-02-03 by the reprex package (v2.0.1)
Comment: You can take the estimated theta `m_stan$stan_summary["reciprocal_dispersion", "mean"]` from the regression and plug it into `size` in `rnbinom()` along with a linear function of time for `mu`. For example, with `mu = 4 * time`, the mean of the generated sample would be approximately 4 times the mean of time and the mean rate would be about 4. However, I don't know if it would be more appropriate to also scale theta in some way. You may want to ask this on [Cross Validated](https://stats.stackexchange.com/)
Comment: Thank you for the example. One more question, is there a simple way to modify this so that the simulated data could have a mean value greater than the empirical data by some known factor. e.g. If I wanted to generate data that had a mean 2X or 3X greater than the empirical mean?
Comment: I really don't see your goal here. If you want to generate data that follows a data generating process observed in your empirical data, then regression is your way to go. If you just want synthetic data with a givien `mu`, then you can use `rbinom()` possibly with a linear combination of other variables that yield your desired mu. What is it you're missing?
Comment: My ultimate goal was to be able to simulate two groups of data with a known difference in the mean value.  These datasets would be used in a power analysis to determine our ability to detect this known difference, given incremental levels of survey effort.  I hoped to simulate data in figure 'B' (which your example showed ) as well as a second set of data that was shifted in its mean by a known factor.  This would represent a doubling of population size for our species of interest.  Put another way, I'm trying to take the data I have and simulate different levels of population growth.


Flutter inputformatter for date

Question: I am looking for an example of an inputformatter for text field that will be a date mm/dd/yyyy, what I am trying to do as the user types update the format.  For instance user starts to type mm and the / is put in, then when the dd is typed in the / is put in.
Anyone done this or have an example?  I have done it in other languages but could not find a similar way to do in flutter/dart.
This is what I have tried so far, but can not get the logic correct.  Any ideas?
    class _DateFormatter extends TextInputFormatter {
  @override
  TextEditingValue formatEditUpdate(
      TextEditingValue oldValue,
      TextEditingValue newValue
      ) {
    final int newTextLength = newValue.text.length;
    int selectionIndex = newValue.selection.end;
    int usedSubstringIndex = 0;
    final StringBuffer newText = new StringBuffer();
    if (newTextLength == 2) {
      newText.write(newValue.text.substring(0, 2) + '/ ');
      if (newValue.selection.end == 3)
        selectionIndex+=3;
    }
    if (newTextLength == 5) {
      newText.write(newValue.text.substring(0, 5) + '/ ');
      if (newValue.selection.end == 6)
        selectionIndex += 6;
    }
    // Dump the rest.
    if (newTextLength >= usedSubstringIndex)
      newText.write(newValue.text.substring(usedSubstringIndex));
    return new TextEditingValue(
      text: newText.toString(),
      selection: new TextSelection.collapsed(offset: selectionIndex),
    );
  }
}

Thanks
Comment: Why not use a datepicker instead of let the user types?
Comment: @JorgeVieira, my users specifically tell me they want to type and do not want to have to click. Granted, we are using Flutter Web, so it's a different context than the typical Flutter app.
Answer: I was struggling with this too. I ended up with the following not so elegant solution:
class DateInputTextField extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _DateInputTextFieldState();
  }
}

class _DateInputTextFieldState extends State<DateInputTextField> {
  @override
  Widget build(BuildContext context) {
    return TextField(
      keyboardType: TextInputType.number,
      inputFormatters: [DateTextFormatter()],
      onChanged: (String value) {},
    );
  }
}

class DateTextFormatter extends TextInputFormatter {
  @override
  TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {

    //this fixes backspace bug
    if (oldValue.text.length >= newValue.text.length) {
      return newValue;
    }

    var dateText = _addSeperators(newValue.text, '/');
    return newValue.copyWith(text: dateText, selection: updateCursorPosition(dateText));
  }

  String _addSeperators(String value, String seperator) {
    value = value.replaceAll('/', '');
    var newString = '';
    for (int i = 0; i < value.length; i++) {
      newString += value[i];
      if (i == 1) {
        newString += seperator;
      }
      if (i == 3) {
        newString += seperator;
      }
    }
    return newString;
  }

  TextSelection updateCursorPosition(String text) {
    return TextSelection.fromPosition(TextPosition(offset: text.length));
  }
}

Answer: Here is an improved version based on Jochem Toolenaar answer. This version will automatically remove the forward slashes for you and limit the user to 8 digits.
class DateTextFormatter extends TextInputFormatter {
  static const _maxChars = 8;

  @override
  TextEditingValue formatEditUpdate(
      TextEditingValue oldValue, TextEditingValue newValue) {
    var text = _format(newValue.text, '/');
    return newValue.copyWith(text: text, selection: updateCursorPosition(text));
  }

  String _format(String value, String seperator) {
    value = value.replaceAll(seperator, '');
    var newString = '';

    for (int i = 0; i < min(value.length, _maxChars); i++) {
      newString += value[i];
      if ((i == 1 || i == 3) && i != value.length - 1) {
        newString += seperator;
      }
    }

    return newString;
  }

  TextSelection updateCursorPosition(String text) {
    return TextSelection.fromPosition(TextPosition(offset: text.length));
  }
}

Answer: Adding an updated version based on Arizona1911 and Jochem Toolenaar versions.
This one prevents the caret from jumping when modifying some of the text that was already typed.

class DateTextFormatter extends TextInputFormatter {
  static const _maxChars = 8;

  @override
  TextEditingValue formatEditUpdate(
    TextEditingValue oldValue,
    TextEditingValue newValue,
  ) {
    String separator = '/';
    var text = _format(
      newValue.text,
      oldValue.text,
      separator,
    );

    return newValue.copyWith(
      text: text,
      selection: updateCursorPosition(
        oldValue,
        text,
      ),
    );
  }

  String _format(
    String value,
    String oldValue,
    String separator,
  ) {
    var isErasing = value.length < oldValue.length;
    var isComplete = value.length > _maxChars + 2;

    if (!isErasing && isComplete) {
      return oldValue;
    }

    value = value.replaceAll(separator, '');
    final result = <String>[];

    for (int i = 0; i < min(value.length, _maxChars); i++) {
      result.add(value[i]);
      if ((i == 1 || i == 3) && i != value.length - 1) {
        result.add(separator);
      }
    }

    return result.join();
  }

  TextSelection updateCursorPosition(
    TextEditingValue oldValue,
    String text,
  ) {
    var endOffset = max(
      oldValue.text.length - oldValue.selection.end,
      0,
    );

    var selectionEnd = text.length - endOffset;

    return TextSelection.fromPosition(TextPosition(offset: selectionEnd));
  }
}

Thanks to caseyryan/flutter_multi_formatter
Answer: In the above solution, I found one overlapping problem with editing in between dates ( if try to edit the month field, the year values get overlapping)
So, I found one solution for this, But not an optimized solution, but it is covering almost all the scenarios,
     1. forward slash during adding fields
     2. remove the forward slash on on clearing fields
     3. between editing handling
      ...etc

class CustomDateTextFormatter extends TextInputFormatter {
  @override
  TextEditingValue formatEditUpdate(
      TextEditingValue oldValue, TextEditingValue newValue) {
    var text = _format(newValue.text, '/', oldValue);
    return newValue.copyWith(
        text: text, selection: _updateCursorPosition(text, oldValue));
  }
}

String _format(String value, String seperator, TextEditingValue old) {
  var finalString = '';
  var dd = '';
  var mm = '';
  var yyy = '';
  var oldVal = old.text;
  print('<------------------------- start---------------------------->');
  print('oldVal -> $oldVal');
  print('value -> $value');
  var temp_oldVal = oldVal;
  var temp_value = value;
  if (!oldVal.contains(seperator) ||
      oldVal.isEmpty ||
      seperator.allMatches(oldVal).length < 2) {
    oldVal += '///';
  }
  if (!value.contains(seperator) || _backSlashCount(value) < 2) {
    value += '///';
  }
  var splitArrOLD = oldVal.split(seperator);
  var splitArrNEW = value.split(seperator);
  print('----> splitArrOLD: $splitArrOLD');
  print('----> splitArrNEW: $splitArrNEW');
  for (var i = 0; i < 3; i++) {
    splitArrOLD[i] = splitArrOLD[i].toString().trim();
    splitArrNEW[i] = splitArrNEW[i].toString().trim();
  }
  // block erasing
  if ((splitArrOLD[0].isNotEmpty &&
      splitArrOLD[2].isNotEmpty &&
      splitArrOLD[1].isEmpty &&
      temp_value.length < temp_oldVal.length &&
      splitArrOLD[0] == splitArrNEW[0] &&
      splitArrOLD[2].toString().trim() ==
          splitArrNEW[1].toString().trim()) ||
      (_backSlashCount(temp_oldVal) > _backSlashCount(temp_value) &&
          splitArrNEW[1].length > 2) ||
      (splitArrNEW[0].length > 2 && _backSlashCount(temp_oldVal) == 1) ||
      (_backSlashCount(temp_oldVal) == 2 &&
          _backSlashCount(temp_value) == 1 &&
          splitArrNEW[0].length > splitArrOLD[0].length)) {
    finalString = temp_oldVal; // making the old date as it is
    print('blocked finalString : $finalString ');
  } else {
    if (splitArrNEW[0].length > splitArrOLD[0].length) {
      if (splitArrNEW[0].length < 3) {
        dd = splitArrNEW[0];
      } else {
        for (var i = 0; i < 2; i++) {
          dd += splitArrNEW[0][i];
        }
      }
      if (dd.length == 2 && !dd.contains(seperator)) {
        dd += seperator;
      }
    } else if (splitArrNEW[0].length == splitArrOLD[0].length) {
      print('splitArrNEW[0].length == 2');
      if (oldVal.length > value.length && splitArrNEW[1].isEmpty) {
        dd = splitArrNEW[0];
      } else {
        dd = splitArrNEW[0] + seperator;
      }
    } else if (splitArrNEW[0].length < splitArrOLD[0].length) {
      print('splitArrNEW[0].length < splitArrOLD[0].length');
      if (oldVal.length > value.length &&
          splitArrNEW[1].isEmpty &&
          splitArrNEW[0].isNotEmpty) {
        dd = splitArrNEW[0];
      } else if (temp_oldVal.length > temp_value.length &&
          splitArrNEW[0].isEmpty &&
          _backSlashCount(temp_value) == 2) {
        dd += seperator;
      } else {
        if (splitArrNEW[0].isNotEmpty) {
          dd = splitArrNEW[0] + seperator;
        }
      }
    }
    print('dd value --> $dd');

    if (dd.isNotEmpty) {
      finalString = dd;
      if (dd.length == 2 &&
          !dd.contains(seperator) &&
          oldVal.length < value.length &&
          splitArrNEW[1].isNotEmpty) {
        if (seperator.allMatches(dd).isEmpty) {
          finalString += seperator;
        }
      } else if (splitArrNEW[2].isNotEmpty &&
          splitArrNEW[1].isEmpty &&
          temp_oldVal.length > temp_value.length) {
        if (seperator.allMatches(dd).isEmpty) {
          finalString += seperator;
        }
      } else if (oldVal.length < value.length &&
          (splitArrNEW[1].isNotEmpty || splitArrNEW[2].isNotEmpty)) {
        if (seperator.allMatches(dd).isEmpty) {
          finalString += seperator;
        }
      }
    } else if (_backSlashCount(temp_oldVal) == 2 && splitArrNEW[1].isNotEmpty) {
      dd += seperator;
    }
    print('finalString after dd=> $finalString');
    if (splitArrNEW[0].length == 3 && splitArrOLD[1].isEmpty) {
      mm = splitArrNEW[0][2];
    }

    if (splitArrNEW[1].length > splitArrOLD[1].length) {
      print('splitArrNEW[1].length > splitArrOLD[1].length');
      if (splitArrNEW[1].length < 3) {
        mm = splitArrNEW[1];
      } else {
        for (var i = 0; i < 2; i++) {
          mm += splitArrNEW[1][i];
        }
      }
      if (mm.length == 2 && !mm.contains(seperator)) {
        mm += seperator;
      }
    } else if (splitArrNEW[1].length == splitArrOLD[1].length) {
      print('splitArrNEW[1].length = splitArrOLD[1].length');
      if (splitArrNEW[1].isNotEmpty) {
        mm = splitArrNEW[1];
      }
    } else if (splitArrNEW[1].length < splitArrOLD[1].length) {
      print('splitArrNEW[1].length < splitArrOLD[1].length');
      if (splitArrNEW[1].isNotEmpty) {
        mm = splitArrNEW[1] + seperator;
      }
    }
    print('mm value --> $mm');

    if (mm.isNotEmpty) {
      finalString += mm;
      if (mm.length == 2 && !mm.contains(seperator)) {
        if (temp_oldVal.length < temp_value.length) {
          finalString += seperator;
        }
      }
    }
    print('finalString after mm=> $finalString');
    if (splitArrNEW[1].length == 3 && splitArrOLD[2].isEmpty) {
      yyy = splitArrNEW[1][2];
    }

    if (splitArrNEW[2].length > splitArrOLD[2].length) {
      print('splitArrNEW[2].length > splitArrOLD[2].length');
      if (splitArrNEW[2].length < 5) {
        yyy = splitArrNEW[2];
      } else {
        for (var i = 0; i < 4; i++) {
          yyy += splitArrNEW[2][i];
        }
      }
    } else if (splitArrNEW[2].length == splitArrOLD[2].length) {
      print('splitArrNEW[2].length == splitArrOLD[2].length');
      if (splitArrNEW[2].isNotEmpty) {
        yyy = splitArrNEW[2];
      }
    } else if (splitArrNEW[2].length < splitArrOLD[2].length) {
      print('splitArrNEW[2].length < splitArrOLD[2].length');
      yyy = splitArrNEW[2];
    }
    print('yyy value --> $yyy');

    if (yyy.isNotEmpty) {
      if (_backSlashCount(finalString) < 2) {
        if (splitArrNEW[0].isEmpty && splitArrNEW[1].isEmpty) {
          finalString = seperator + seperator + yyy;
        } else {
          finalString = finalString + seperator + yyy;
        }
      } else {
        finalString += yyy;
      }
    } else {
      if (_backSlashCount(finalString) > 1 && oldVal.length > value.length) {
        var valueUpdate = finalString.split(seperator);
        finalString = valueUpdate[0] + seperator + valueUpdate[1];
      }
    }

    print('finalString after yyyy=> $finalString');
  }

  print('<------------------------- finish---------------------------->');

  return finalString;
}

TextSelection _updateCursorPosition(String text, TextEditingValue oldValue) {
  var endOffset = max(
    oldValue.text.length - oldValue.selection.end,
    0,
  );
  var selectionEnd = text.length - endOffset;
  print('My log ---> $selectionEnd');
  return TextSelection.fromPosition(TextPosition(offset: selectionEnd));
}

int _backSlashCount(String value) {
  return '/'.allMatches(value).length;
}

We can Use our custom formator as in inputFormatters like below
TextField(
  // maxLength: 10,
  keyboardType: TextInputType.datetime,
  controller: _controllerDOB,
  focusNode: _focusNodeDOB,
  decoration: InputDecoration(
    hintText: 'DD/MM/YYYY',
    counterText: '',
  ),
  inputFormatters: [
    WhitelistingTextInputFormatter(RegExp("[0-9/]")),
    LengthLimitingTextInputFormatter(10),
    CustomDateTextFormatter(),
  ],
),

Try out this, thank you. !
Answer: I found this as a simple solution
inputFormatters: [
                  LengthLimitingTextInputFormatter(10),
                  FilteringTextInputFormatter.allow(RegExp(r'^[0-9\/]*$')),
                  TextInputFormatter.withFunction((oldValue, newValue) {
                    String text = newValue.text;
                    if (newValue.text.length == 2 &&
                        oldValue.text.length !=3) {
                      text += '/';
                    }
                    if (newValue.text.length == 5 && oldValue.text.length !=6) {
                      text += '/';
                    }
                    return TextEditingValue(text: text);
                  })
                ],

Comment: You didn't understand it. From my side it's working absolutely fine. I have posted because someone is facing the same issue & I got solution while solving that. You are not a celebrity to come and comment on your post.  Here is my working code. If you can't test don't simply say it's not working.
Answer: An improvement of the original answer (Jochem Toolenaar and Alberto)
This would also validate the month in mm/yy format. That is months such as 90, won't be valid.
import 'dart:math' as math;
import 'package:flutter/services.dart';

/// Date formatter that will automatically remove the forward slashes for you and limit the user to 8 digits. Also validates the month
class DateTextFormatter extends TextInputFormatter {
  static const _maxChars = 5;

  @override
  TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
    if (!isValidDate(newValue.text)) {
      return oldValue;
    }
    var text = _format(newValue.text, '/');
    return newValue.copyWith(text: text, selection: updateCursorPosition(text));
  }

  String _format(String value, String separator) {
    value = value.replaceAll(separator, '');
    var newString = '';

    for (int i = 0; i < math.min(value.length, _maxChars); i++) {
      newString += value[i];
      if ((i == 1 || i == 3) && i != value.length - 1) {
        newString += separator;
      }
    }

    return newString;
  }

  TextSelection updateCursorPosition(String text) {
    return TextSelection.fromPosition(TextPosition(offset: text.length));
  }

  bool isValidDate(String value) {
    if (value.length == 1) {
      return ['0', '1'].contains(value);
    } else if (value.length == 2) {
      var number = int.parse(value);
      return number >= 1 && number <= 12;
    } else {
      return true;
    }
  }
}

Also note that you have to include this line:
inputFormatters: [FilteringTextInputFormatter.digitsOnly, DateTextFormatter()]

To enable the input filter character that are not numbers
Answer: I used Robert TextInputFormatter as follows
class DATETextInputFormatter extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(
  TextEditingValue oldValue, TextEditingValue newValue) {
int newTextLength = newValue.text.length;
int selectionIndex = newValue.selection.end;
int usedSubstringIndex = 0;
StringBuffer newText = new StringBuffer();
if (newTextLength == 3) {
  if (!newValue.text.contains('/')) {
    if (newValue.text[2] != '/') {
      newText
          .write(newValue.text.substring(0, usedSubstringIndex = 2) + '/');
    }
    if (newValue.selection.end >= 2) selectionIndex++;
  }
}
if (newTextLength == 6) {
  if (newValue.text[5] != '/') {
    newText.write(newValue.text.substring(0, usedSubstringIndex = 5) +
'/');
  }
  if (newValue.selection.end >= 5) selectionIndex++;
}
// Dump the rest.
if (newTextLength >= usedSubstringIndex)
  newText.write(newValue.text.substring(usedSubstringIndex));
return new TextEditingValue(
  text: newText.toString(),
  selection: new TextSelection.collapsed(offset: selectionIndex),
);
}
}

I call the class in a list of TextInputFormatter
List<TextInputFormatter> getTextInputFormatter(int idInputFormatter) {
...
return list = [
    **DATETextInputFormatter()**,
    FilteringTextInputFormatter.allow(RegExp('[0-9/]')),
    LengthLimitingTextInputFormatter(10)]

Finally in the TextFormField(
TextFormField(
...
autovalidateMode:AutovalidateMode.always,
inputFormatters:getTextInputFormatter(idInputFormatter),
keyboardType: TextInputType.number,
validator: (value) {if (value == null ||
value.isEmpty) { return 'need to inform date';}
if (value.length >= 10) {try {if (int.parse(value.substring(0,
2))>12) {return 'invalid date';}if (int.parse(value.substring(3,
5)) >31) {return 'invalid date';}var data =
DateFormat('MM/dd/yyyy').parse(value);
//test date is between 100 old or 100 years ahead
return ((data.isAfter(DateTime.now().add(Duration(days:36500)))) ||
(data.isBefore(DateTime.now().subtract(Duration(days:36500)))))
? 'invalid date': null;} catch (e) {return 'invalid date';
}
}
}
return null;
},

Comment: Please format your code correctly and provide more details on what you did. If you did exactly what someone originally answered, then you should have just commented on that answer instead
Answer: class DateFormatter extends TextInputFormatter {
    @override
    TextEditingValue formatEditUpdate(
        TextEditingValue oldValue,
        TextEditingValue newValue
        ) {
      final int newTextLength = newValue.text.length;
      int selectionIndex = newValue.selection.end;
      int usedSubstringIndex = 0;
      final StringBuffer newText = StringBuffer();
      if (newTextLength >= 3) {
        newText.write(newValue.text.substring(0, usedSubstringIndex = 2) + '/');
        if (newValue.selection.end >= 2)
          selectionIndex++;
      }
      if (newTextLength >= 5) {
        newText.write(newValue.text.substring(2, usedSubstringIndex = 4) + '/');
        if (newValue.selection.end >= 4)
          selectionIndex++;
      }

      // Dump the rest.
      if (newTextLength >= usedSubstringIndex)
        newText.write(newValue.text.substring(usedSubstringIndex));
      return TextEditingValue(`enter code here`
        text: newText.toString(),
        selection: TextSelection.collapsed(offset: selectionIndex),
      );
    }

Comment: Hi @AkashDadnwate, welcome to Stack Overflow! As you're new here, I strongly recommend that you take a look at the guide [how do I write a good answer](https://stackoverflow.com/help/how-to-answer). It'd be much better if you explain the code and clearly show how it solves the problem.
Comment: Please provide more details as to how your code fixes the OPs issue

Flutter inputformatter for date


Looking for a specific attribute containing a specific word

Question: I am tasked with going through our entire site, each aspx and ascx page and code behind .cs files to replace a word with another. the ctrl-shift-f, find all, functionality with regex came to mind, however, my regex pattern I made seems to find attribute names which doesn't necessarily contain the word I want to replace.
In my case, I am trying to replace the words "Supply site, Suppliers, Supply or Supplier" with "depot". Some things to note - we can ignore CDATA[], attributes always have a quotation mark " instead of an apostrophe. Additionally, for now I am only trying to cover our design layer- aspx and ascx files, so we are dealing with HTML. We don't have any HTML text, (e.g  HELLO )
Regex
((Text|HeaderText|ToolTip).*|\n*(Supply|Supplysite|Supply\Ssite|Supplier))$

The Question:
How can I change my regex to strictly find Text, HeaderText or ToolTip attributes which contains Supply/Supplysite/Supply site/ Supplier/ Suppliers ?
Comment: If you're talking about _attributes_ (a code example would be useful) shouldn't there be an `=` character to indicate/denote the start of the content for the attribute? If yes, use it in your regex.
Comment: Sounds like `\s(Text|HeaderText|ToolTip)="[^"]*(Supply(?:\s?site)?|Supplier)[^"]*"`
Comment: yeah, I tried to skip through it by a greedy match all (as to ignore the =" after the keywords "Text/ToolTip/HeaderText")
Comment: @WiktorStribiżew bravo. that's the ticket - does exactly what I need. Post it as your answer please , I will tick it as soon as I can
Answer: You may use
\s(Text|HeaderText|ToolTip)="[^"]*(Supply(?:\s?site)?|Supplier)[^"]*"

Details

\s - a whitespace
(Text|HeaderText|ToolTip) - any of the three alternative substrings
=" - a =" substring
[^"]* - any 0+ chars other than "
(Supply(?:\s?site)?|Supplier) - either of the two:

Supply(?:\s?site)? - Supply optionally followed with an optional whitespace and site substring
| - or
Supplier - a Supplier substring

[^"]* - any 0+ chars other than "
" - a " char.

See this regex demo.


scrapy No JSON object could be decoded

Question: I am trying to extract the data from a webpage using scrapy... and all the data is inside a javascript

when i use
json.loads(response.xpath("//script[2]/text").extract())

it gives me that error

No Json object could be decoded

I only need to get the first var ANUNTURI and everything inside it and put them in mysql.
UPDATE
I also tried this:
var = re.compile(r"var ANUNTURI= ({.*?});", re.MULTILINE | re.DOTALL)
json.loads(response.xpath("//script[2][contains(., 'var ANUNTURI')]/text()").re(var))

and the error that I get is this:

TypeError: expected string or buffer

and then I tried this:
json.loads("".join(response.xpath("//script[2][contains(., 'var ANUNTURI')]/text()").re(var)))

and I get:

NO JSON object could be decoded
Comment: so there is no way to extract that data?
Comment: Regular expressions?
Comment: Did you even extract that JSON from that JS code?
Comment: what do you mean exactly? i tried... json.loads to extract it
Comment: Use something like this: `"var\s+ANUNTURI\s*=\s*(.*?);"`
Comment: i ve tried it and its not working it gives me ** Expected string or buffer **
Answer: This is a possible way to extract the data, however with current presented code, it is hard to tell if variable embeds JSON or Javacript. Javascript in subtle ways could be superset of JSON object.
data = """/ Globals
var ANUNTURI = [ { "ID": "2750801",   "Data": "Azi 11:16",   "Zile_piata": "146",   "Zona": "Andronache",   "Nr_Camere": "2",   "suprafu": "65",   "Pret": "62.000 EUR",
    "Citit": "0",   "Tip_teren": "-",   "Etaj": "3 / 3",   "supraft": "-",
       "frontStradal": "-",   "Etichete": "",   "ArePoze": "7",   "Tip_spatiu": "-" },]

;\r\n    var ID_CAUTARE = 0;\r\n    var CATEG = 3;\r\n
var TRANZ = 2;\r\n
var SORTARE = "";\r\n
var ID_AGENT = "3012";\r\n
var ID_LOCALITATE = \'13822\';\r\n
var ID_JUDET = \'10\';\r\n
var CRITERIU_FILTRU = \'\';\r\n        // judet_schimbat = "";\r\n\r\n    $(\'form[name="anunturi"] input[name="sort"]\').val(SORTARE);\r\n\r\n', u"\r\n\r\n    $(function(){\r\n\r\n
var setTagValue = ' 0 ';\r\n
var comboTitle = [];\r\n\r\n        $('#combo_etichete').mpCombo({\r\n            cls: 'mpCombo etichete',\r\n            header_default_text: 'Indiferent',\r\n            interval_from_text: ' Peste ', \r\n
interval_to_text: ' Pana la ', \r\n            interval_between_text: ' si ', \r\n            combo_width: '162px', \r\n            menu_width: '160px',\r\n            onSelect: function() { // trigger click daca e inchisa cautarea avansata\r\n                if( $('#cautare_avansata').is(':hidden') ) {\r\n                    $('a#filtreaza').trigger('click');\r\n                }\r\n            }\r\n\r\n        });\r\n        \r\n        $('#combo_etichete').mpCombo({'setval': setTagValue});\r\n        comboTitle.push( $('#combo_etichete').mpCombo('gettitle') ); \r\n\r\n        if (comboTitle.length > 0) {\r\n            $('#combo_etichete dt a').text( comboTitle.join(', ') );      \r\n        }\r\n\r\n    });\r\n\r\n\r\n", u'\r\nvar gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");\r\ndocument.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E"));\r\n'
"""
from json import loads
from pprint import PrettyPrinter
lines = data.split("\r\n")
anunturi_json = lines[0].split("=")[1]
print anunturi_json
val = loads(anunturi_json)
pp = PrettyPrinter(indent=4)
pp.pprint(val)

Comment: @Omega `val.get('ID')`
Comment: thank you it worked...the only thing i had to do extra...i had to extract the **;** from the end of json
Comment: can you also tell me how to extract only the "ID" from that "var Anunturi" ?

scrapy No JSON object could be decoded


What does the syntax $\min_{1\leq n \leq N}$ mean in $\min_{1\leq n \leq N} y_n(\mathbf w^{*T}\mathbf x_n)$?

Question: I'm working through a machine learning problem and it uses the following syntax:
$$\rho=\min_{1\leq n \leq N} y_n(\mathbf w^{*T}\mathbf x_n)$$
I am not familiar with the syntax $\min_{1\leq n \leq N}$. What does that mean?
For reference, this is the full problem:
Comment: It's a common shortcut for the more formal $$\min\{f(n)\mid n\in\mathcal A\} = \min_{n\in\mathcal{A}}f(n)$$ where "$1\le n\le N$" is intended to mean $n\in\{1,2,\ldots, N\}.$ Although the appearance of two apparent variables "$n$" and "$N$" is a potential ambiguity, the absence of "$N$" in the expression for $f(n) = y_n\left(\mathbf{w}^{*T}\mathbf{x}_n\right)$ makes the meaning clear.
Answer: I think your confusion comes from the long subscript followed by a lowercase letter.
$1 \leq n \leq N$ is all part of the subscript. It means to pick the minimum of any value $y_n(\boldsymbol{w}^{*\,T}\boldsymbol{x}_n)$ where $n$ is between $1$ and $N$.
Comment: Just to clarify it would be the minimum value of $y_n(w^{*T}x_n)$ correct? The $y_n$ would be included? Edit: Now that I just finished the problem I would assume yes hahaha
Comment: That was indeed it. It wasn't until someone edited my original post that I realized that it wasn't $min_1$ by itself. That makes much more sense. Thank you! (I'll mark this correct as soon as the minimum time has lapsed)
Comment: @Grant Yes, that's correct, thanks for improving the answer!


Positivity of bounded form $\phi$ on C*-algebra $A$

Question: I am reading Lectures on von Neumann algebras by Stratila and Zsidò. I don't know what to do with exercise 5.1, do you have any tips?
Let $A$ be a $C^*$-algebra and $\phi$ a bounded form on $A$. If there exists an $a \in A$, $a>0$, such that $\phi (a)= ||\phi||\, ||a||$, then $\phi$ is positive.
Thanks for your help.
Answer: Rescaling $a$, you may assume that $\|a\|=1$. Then the condition tells us that $\phi$ attains its norm in a positive element of norm $1$.
We may moreover also assume that $A$ is unital (why?).
We show that this automatically implies that also $\phi(1)=\|\phi\|$.
Indeed, all you have to do is choose $\lambda\in \mathbb{C}$ with $|\lambda| = 1$ such that $\lambda\phi(1-a) \in [0, \infty[$. Then, since $\|a + \lambda(1-a)\| \le 1$ (why?), we have
$$\|\phi\| = \phi(a)  \le \phi(a)+ \lambda \phi(1-a) = \phi(a + \lambda(1-a)) \le \|\phi\|$$
and thus $\|\phi\| = \phi(a) + \lambda\phi(1-a) = \|\phi\| + \lambda\phi(1-a)$ so that $\phi(1-a)=0$, i.e. $\phi(1) = \phi(a) = \|\phi\|$.
Now, you can invoke the well-known fact that if $\phi$ is a bounded functional on a unital $C^*$-algebra that attains its norm in the identity, then it is automatically a positive functional. This is proven in every text on $C^*$-algebras.
Comment: Thanks a lot for your answer. However, it is not clear to me why one can assume that the $C^*$-algebra is unital.
Comment: @Schrödinger'scat Extend your functional to the unitisation (Hahn Banach). Apply the argument from the unital case to see that it is positive, and then restrict back to the original algebra. Restrictions of positive maps remain positive.


Deserialising JSON made up of various models C#

Question: I'm trying to work out how to deserialise a JSON response that can be made up of single or multiple models, so for instance, I have the following URL and Response from that endpoint:
https://api.site.com/products?query=fruit
Which would return something such as this:
{
    "fruit": [{ ... },{ ... }]
}

"Fruit" could be anything, but as an alternative, you can also do this:
https://api.site.com/products?query=fruit,pies
{
    "fruit": [{ ... }, { ... }],
    "pies": [{ ... }, { ... }]
}

So I know how to handle just one of the "selections" provided at a time, however how do I go about deserialising the response when there can be 2 separate models in the same response?
Comment: How did you deserialize fruit with its children? Apply the same technique to fruit's parent.
Answer: In case you know the json model before hand (also called data contract), then you can create a dedicated class. So, for the above scenario, the class would be
public class AnyClassName
{
    public List<Fruit> Fruit { get; set; }
    public List<Pie> Pie { get; set; }
}

And then use
Newtonsoft.Json.JsonConvert.DeserializeObject<AnyClassName>(jsonString)

In case you are not aware of the data-contract, then use
Newtonsoft.Json.JsonConvert.DeserializeObject(jsonString)

In this case you have to do a lot of coding to probe for the existence of an element and extract the value.
Comment: If you know the data structure of 30 models, I would advice to go with structured approach, the first option. Saves a lot of time in deserialization.
Comment: The difficulty comes in that there isn't just "Fruit" and "Pies", there's around 30 different combos of things you can request, so I couldn't make a model for each potential set of selections.


Oktavism and low larynx

Question: I have question about a nearly-unheard of oktavism technique. It is similar in nature to subharmonics. Apparently there is a technique where you "swallow your larynx" and depress is a very large amount using muscles almost never used for singing. Then, once it is extremely depressed, you can sing notes 2-3 steps lower than your usual low. I've been told by multiple people the oktavist of the Kovcheg ensemble, Pavel Myakotin uses it for notes between B1 and D2.
I've also been told some operatic singers, in addition to some Bassos, use it.
Does anyone know anything about this? I'm interested in it very much.
Answer: If someone told you that, they told you incorrectly,  I can and do sing into basso profundo range, and that's not a correct procedure. First,  irregardless of what you sing, you never activate the digastric (Swallowing muscles of your throat ) when you sing, that's poor singing technique.    What's actually happening is a harmonic, it's literally the reverse of what happens when a guitarist lightly touches a guitar string and causes the note to ring out at a much higher octave.
With octavists, most of them use harmonics in singing, it's pretty obvious when it's being done as the singer with have a REALLY reedy sound to their voice.  The technique involve doesn't involve swallowing, it's a type of vocal fry (I'm not kidding, I actually can do this!). It's effortless and the note produced is pretty loud as well!
There's a youtube video online that explains this better than I can, but I'll try.  Sing your most comfortable note, but don't sing it too loudly, then go into fry.  If this is done correctly, you'll be singing the same note one octave lower.
Check out youtube for the term "basso profundo" and you'll hear this being done,  any time the singer sounds reedy, he's using  harmonics!
Answer: The technique described in another answer is called subharmonics and is not what the OP was asking about.
Pavel uses subharmonics on notes below B1 however he is said to use a kind of "forceful strohbass" for notes between E2 and B1 according to a fellow Oktavist who performs with him and the Kovcheg ensemble.
What exactly is meant by that I do not know, it's a bit of a mystery what technique he uses between his subharmonic register and chest register.
I think it's unlikely he's "swallowing his larynx" as the OP puts it, it could be as simple as employing a well supported chest-fry mixture but whatever it is, it's unusually powerful for a natural bass-baritone like Myakotin and it ISN'T subharmonics.
His subharmonic notes are much more relaxed than what goes on in the E2 - B1 area.
Answer: I can tell you as someone that does sing can sing as a Basso Profundo that you never use your swalling (digrastic) muscles ever. It's a technique similar to singing in fry. If you're not sure what fry is, listen to any interview by Harry Belafonte, he literally speaks in Fry. He's generating two frequencies at the same time.
With the technique I use, you sing a note one octave higher than where you want to sing to, and go into vocal fry, at first, it sounds pretty bad and it's no where near where you want to be.  You're never "swallowing the larynx" nor anything even close. You simply slide into fry. It takes a while to learn, but once you learn it, you can singer not only lower, but in that particular style, you're low notes are louder although you're not trying to make the notes louder, nor are you straining or forcing the note.
Bottom line, never use the swallowing muscles in singing, ever!
That said, I kinda understand the explanation, in away, the sound is being produced a bit differently than is typical and the air is pulled in rather than breathed out, so I get why that description is used, but it's a pretty bad way to describe it!
Answer: I suggest, from what I have heard, that he uses his ventricular vocal folds in a similar style as in undertone singing/throat singing, not to be confused with overtone singing. It activates the ventricular folds which resonate an octave lower than the sung pitch of the regular folds, but because of their own physical properties, they don't resonate nearly as far down the range as you're regular folds go like with strohbass subharmonics (although an octave higher). I have to add that I'm a music pedagogy student and in no way qualified to this statement, it is just a suggestion to what might be happening, then again, I am not a professional, so take this with a grain of salt.
It does explain why he uses it only between B1 and D2, since the range of throat singing is very limited when you started learning. For example, my voice usually extends down to C#2, sometimes the subharmonics reach D#1, but my throat singing is stuck between A1 and C#2 while the outer notes are extremely uncomfortable to hit and require a lot of effort to maintain. I'm not a trained singer.

Oktavism and low larynx


HTML5 - Change opacity of a draw Rectangle

Question: Let's say I draw an HTML5 element of rectangle using this:
context.clearRect(25, 72, 32, 32);

How would I make it 50% transparent?
Answer: Use globalAlpha. You'll also have to draw with fillRect. clearRect just erases pixels. It can't partially erase so you'll have to use fillRect or other drawing primitives.
from w3schools.com:
    ctx.globalAlpha = 0.2;
    ctx.fillRect(50,50,75,50);
    ctx.globalAlpha = 1.0;

Comment: Note that you'll need to call `ctx.globalAlpha=1;` after those two lines in order to prevent further draw calls (even previous ones, believe it or not) from being partially transparent as well.
Comment: This makes the whole canvas transparent (on Chrome atleast)
Comment: Brian, it doesn't clear the whole canvas, only the size and location you give it. That's what those 4 numbers are; the first two being the X coordinate on the canvas, then the Y coord., and then the size X and Y of the rect you'd like to clear.
(Keep in mind that Y values instead get bigger as they go down, in Canvas)
Comment: You'll also need to be sure to call `clearRect` before all drawing occurs or else you'll end up with a "trailing effect" (unless that's what you're intending to do -- kinda neat looking).
Answer: ClearRect removes what was there and leaves it blank. The best way is to use a rgba fillStyle value as it will only make the rectangle (or any other shape that you are drawing) transparent. The code would be:
ctx.fillStyle = 'rgba(225,225,225,0.5)';
ctx.fillRect(25,72,32,32);

(thanks How to change the opacity (alpha, transparency) of an element in a canvas element after it has been drawn?)
Comment: I prefer this over the selected answer since it doesn't globally effect all drawing after using it.
Comment: You can save and restore the context so that it doesn't take affect globally by using `ctx.save()` and `ctx.restore()` wrapped around your other context changes.
Comment: I prefer this over the most popular because it is more concrete:  talks about the transparency of the fill (I want to 'clean' a rectangle), not the transparency of any operation.   The two solutions requires 4 lines (because the ctx.save() and ctx.restore() to preserve context).  Anyway, the two solutions are good for me.