File uploads taking more time

Question: We recently moved to a new server. We have observed that the file upload done from the ftp command is taking around 5sec for 5MB file. But same file uploaded through CURL command is taking about 140 seconds. Any idea what could be wrong here ?
Cheers
M
Answer: Uploading through FTP means transferring binary data directly to the filesystem using TCP.
Uploading throught cURL means transferring base64 encoded data (1.5x bigger) to the web server using HTTP (which uses TCP). The web application might create a 5MB memory object and then write it on disk, somewhere (it depends a lot on your application).
Do you know how is handled the upload on your HTTP server?
Nginx has a pretty decent module for that: nginx upload module
Comment: transferring through curl command also takes more time . So it has nothing to do with http server i guess.
Comment: cURL uses your webserver, doesn't it? Can you past the cURL command you're using?
Comment: curl -F name=yyy -F filename=@/xxx/xxxxx/somefile.txt  http://someurl.com
Comment: now, add `-v`, like `curl -v -F name=yyy…` you'll see that HTTP (and thus your webserver) is used. Apparently you can use [FTP with curl too](http://www.cs.sunysb.edu/documentation/curl/index.html) but you aren't in the example you gave me.

File uploads taking more time


SmartSVN - Unable to create new repository profile

Question: I have just installed SmartSVN on this fedora system.
The application starts (on running ./smartsvn.sh) with its usual UI but many things are not working.
Creating New repository profile
Trying to create a new repository profile (Repositories-> Repository Profiles-> Add)
An Error occurred while processing an SVN command -
Cannot connect to 'svn+ssh://192.168.0.103': There was a problem while connecting to 192.168.0.103:22

Quick Checkout
Trying to do Quick Checkout (less configuration)
An Error occurred while processing an SVN command -
 Malformed XML.

Some Observations
When I run the smartsvn.sh file like this:-
./smartsvn.sh

It shows this in the console -
Warning: /bin/java does not exist
Could not lock /root/.smartsvn/_lock_
Switched to running instance

I was using SmartSVN in another system before this where it was working. There too, it was showing the warning like Warning: /bin/java does not exist but this part was not showing:-
Could not lock /root/.smartsvn/_lock_ Switched to running instance
I have only JRE installed in both the systems and not JDK. Eclipse IDE which also needs only JRE is running perfectly fine. So, what could be the reason? Any pointers?
Updates
I have just confirmed that it is a smart svn issue. Command line svn is working fine on my system. I created a repository and did a successful checkout.
Thanks,
Sandeepan
Comment: My experience with SmartSVN (Windows version) was not good. Try http://naughtysvn.tigris.org/ or kdesvn.
Answer: This looks to me like a ssh tunneling problem, SmartSVN is trying to open a ssh connection to a remote server in order to create the new directory, this could be caused by

Incorrect credentials while trying to connect to the remote server
SmartSVN is unable to find the ssh binary to execute the command

Try ssh-ing into that remote server without SmartSVN (if possible) and see if that works, also checking the log of the remote server will help you diagnose if you actually arrive to connect there (which would eliminate the second cause out of the possible list)
Try it and let me know which one works for you best
Comment: My repository is actually in the same system, not on a remote system. I am using the root user credentials in smartsvn.


Add control to TableLayoutPanel's row that is full

Question: I want to add a control (Panel in this case) to a TableLayoutPanel in a specific row (column doesn't matter), but if this row is full of controls and I try to add another one on this row, the control is added in the next row.
Is there something I can do to know if the control that I want to add to any row is going to fit in the row that I want?
Actual behaviour that I'm experimenting

As you can see on the image, those 5 panels are appointments set to the same hour (15hs)
The program I'm developing is about appointments, so the Panel that I want to add to the TableLayoutPanel is an appointment, so I need to know if the control that I'm adding to the row is going to fit or not.
Answer: I finally figured out to resolve this issue in the backend side, directly asking if the initial time and the final time of the appointment was inside the range of another appointment. Because I can have a variable number of appointments at the same time, if the appointment is in the range of another one, i just increment an int value and checks if the amount of appointments overlapped are >= than the max amount allowed.
Hope it helps anyone


NAS as VFP file Server?

Question: Looking to replace Win XP Pro 'file server' we have for a Visual FoxPro invoicing app running upto 15 users with around 10 DBF's. Basically we've hit XP's incoming connection limit...
Any suggestions on a NAS for this usage (make/model)? I realise I could use FreeNAS, or Linux+SAMBA, but thought a dedicated NAS would make life easier!
Thanks in advance!
Answer: I've seen the Visual Fox Pro / DBF situation before and my best answer is to stay away from the small, all-in-on little "Home" NAS devices.  These typically don't pack enough CPU power to get the throughput you'll need to read through those DBF files.  Either pickup a more business oriented boxed NAS solution or go the route of FreeNas / Openfiler on a low end PC with dual drives and use software drive mirroring to help protect from hardware failure.
Answer: Be carefull with Samba based file servers (NAS devices often are running some Linux with Samba), since the file-locking options may be different from the default Windows XP file sharing settings.
So test that before putting it into production!
Comment: There are samba options in the config file that can correct this issue.  See this article on the issues and their solutions when dealing with file locking and oplock issues with databases on a Samba share.

http://www.drouillard.biz/Tips&Tricks/Samba/Oplocks.htm

There is a lot of information on the internet about this so Google is your friend.  Or ask another question here on Server Fault if you run into issues that you can't resolve.
Comment: I didn't say that I have a problem with that. Its just that with some (low end?) NAS devices you can't change the (samba) locking behavior.
Comment: Thanks for all the info. With this and some google research, I'm going to go down the route of a PC with FreeNas or Linux+Samba which will allow more configuration options for what I need. I've not seen any particular problems posted so guess it's going to work out fine.
Answer: An alternative option - you can patch away the 10-inbound-connection limit, something commonly done by people using Bittorrent.
http://akiraxky.wordpress.com/2009/05/28/windows-xp-sp2-tcpip-sys-connection-limit-patch/

To remove the tcpip.sys socket creation limit:

Backup your original tcpip.sys file before editing please, this is somewhat important !
In your hex editor, go to offset 4F322 hex (or 324386 decimal).
Change 0a 00 00 00 to 00 00 0a 00

There's various automated patchers around too, http://www.lvllord.de/ for example
Comment: Windows file sharing connections and TCP connections are two different things.  To get around this problem using windows you need a Windows Server box with enough CALs (Client Access Licenses) to cover the needed connection count.
Comment: Thanks for the answer. The TCP patch has already been applied. There is still limit on XP Pro's Shares to 10 concurrent users though, and I've not been able to find a way around this!

NAS as VFP file Server?


How to manually install chat module in Humhub

Question: I just installed humhub and downloaded the chat module from github. https://github.com/ychoucha/HUMHUB-module-chat
I gitcloned it to humhub/protected/modules. But, it does not show up in the modules section of administration login. How do I install this or other third part modules in Humhub?
Answer: According to this link https://github.com/humhub/humhub/issues/165 on Sep 12, 2014, Luke said:
You can also install modules manually:

Download Module (e.g. mail)
Put it into protected/modules/mail
Enable it in Administration -> Modules

I've tried with the Tasks modules and it worked. Make sure you have the latest stable version of the code from the Humhub repository. You also must be able to download that directly from the Administration > Modules > Find Online
Answer: The module no longer works since humhub v1.0.
According to the docs you need to add a config.php file.
https://www.humhub.org/docs/guide-dev-module.html
config.php (replaces autostart.php)
<code><?php

use humhub\modules\dashboard\widgets\Sidebar;

return [
    'id' => 'chat',
    'class' => 'humhub\modules\chat\ChatModule',
    'namespace' => 'humhub\modules\chat',
    'events' => [
    array(
        'class' => Sidebar::className(),
        'event' => Sidebar::EVENT_INIT,
        'callback' => array(
            'humhub\modules\chat\ChatModule',
            'onSidebarInit'
            )
        ),
    ],
];
</code>


how to preview video camera from tv tuner in vb.net

Question: how can i preview / record or only preview video from my tv tuner card in vb.net....i have usb stick deny tuner card in my laptop
Answer: A lot of TV capture cards, webcams etc support DirectShow. A few months ago I was looking at how easy it would be to capture video from C# and it can be done. DirectShow.NET is a wrapper around most of the DirectShow APIs. There are also a couple of commercial DirectShow controls for .NET like VisionLAB. I've not used them myself and it looks pretty complicated, but it can be done.
Otherwise, as Red-nosed unicorn suggests you might need to start reading the vendor's documentation.
Answer: That depends entirely on your TV card. Does the vendor provide an API documentation? This would specify how the TV card can be addressed programmatically.
Comment: could you please let me in detail about the whole process of api or any process


Space created at the bottom of a panel with foundation framework

Question: I'm playing with foundation framework and i'm trying to make a simple login form with it.
Here's my markup
<form>

                                Login: <input type="text" placeholder="Login">

                                Senha <input type="password">

                            <input type="submit" value="OK" class="tiny button right">
                            Esqueci minha senha

        </form>

But at the bottom of it, there's some space created.
How can i remove this space?
Answer: The space you are talking about is created by the buttons bottom margin. To remove it simply add a CSS class like such:
.panel .button { margin-bottom: 0; }

Working example: JSFIDDLE


Checkbox retorna sempre false quando faço .is(":checked");

Question: Código é este:

Problema é, o cara seleciona a checkbox, retorna false, ele desmarca, retorna false.. sendo que true retorna quando a caixa é selecionada, e false quando é desselecionada..
Eu usei um alert(check); e ele retorna true, e false, para marcado e não marcado, exatamente como era para fazer, mas na verificação do IF ele sempre retorna uma opção, que no caso é false..
Comment: voce esta checando se a var check eh igual a "true"(string) inves de testar se ela eh `true` (note sem as aspas)
Comment: Verdade.. eu pensei que, uma variavel recebendo um valor, o valor virava string, mas como não há declaração de tipo de variavel, acho que é por isso que ela acaba se "adequando" ao que foi sugerido ela receber..
obrigado!!
Answer:

$(document).ready(function () {
    alert("funfo");
    $("#segunda").click(function () {
        var check = this.checked;
        if (check) alert("true");
        else alert("false");
        alert('O valor do check é: ' + check);
    });
});

<input type="checkbox" id="segunda" />

Quando você usa .is(":checked") isso vai retornar um Booleano. Ou seja true ou false. Na sua comparação está a comparar com "true". Quando usa aspas está a usar o tipo String e não Booleano. Assim a comparação deve ser if (check == true){. Na verdade essa comparação é desnecessária, basta fazer if (check){.
Um outro aspeto que pode mudar (e mudei no código do exemplo da resposta) é que não precisa de $("#segunda").is(":checked"), pode usar JavaScript puro, sem jQuery, (this.checked) uma vez que está à procura do checked do elemento que foi clicado. (Quanto menos jQuery melhor...).


Unknown column 'type' in 'where clause'

Question: Hay Everyone i've got a bit of a problem and just wondering if any of you can help me out.
i'm a bit of a noob when it comes to MySQL and PHP but the problem is i'm geetin a error when i try to add a comment into my Database here is my public function code
    public function addComment($movieid, $comment, $user_id)
{
    $comment = stripslashes(strip_tags($comment));
    $comment = mysql_real_escape_string($comment);
    $movieid = mysql_real_escape_string($movieid);
    $user_id = mysql_real_escape_string($user_id);
    if (!($e = mysql_query("SELECT * FROM comments WHERE target_id='{$movieid}' AND user_id='{$user_id}' AND comment='{$comment}' AND type=2"))) {
        exit(mysql_error());
    }
    if (mysql_num_rows($e) == 0) {
        if (!($e = mysql_query("INSERT INTO comments(target_id,user_id,comment,date_added,type) VALUES('{$movieid}','{$user_id}','{$comment}',NOW(),2)"))) {
            exit(mysql_error());
        }
    }
}

and here is my MySQL comment table Structures
 1  id                int(11)                       No  None    AUTO_INCREMENT
 2  movieid           int(11)                       No  None
 3  user_id           int(11)                       No  None
 4  target_id         int(11)                       No  None
 5  comment           text       utf8_general_ci    No  None
 6  date_added        datetime                      No  None

but i get this error when i try to add a comment
Unknown column 'type' in 'where clause'

so could someone help help me
thanks
Comment: Doesnt exist `type` colum in your DB table
Comment: where is the column "type" in ur table ?
Comment: Also don't use mysql eh?
Comment: There isno type column in your table, you need to add it first
Comment: why are you using mysql_* functions even if they're deprecated?
Answer: Your table doesn't have a column named type . This query will fail. Either you need to add the column type to your table or need to change the query to exclude the reference to type altogether. What does type refer to ?
I want to understand what are you trying to achieve with this query :

SELECT * FROM comments WHERE target_id='{$movieid}' AND user_id='{$user_id}'
      AND comment='{$comment}' AND type=2

How will the DB system filter the results when there is no type in the table itself ?
Rule of thumb is First try to execute the query directly over the DB using some IDE or command line , check for the correctness of the query and verify the data as well, before using it in the code !
Answer: You dont have type column in your database that is the problem
Answer: It's exactly what it says - your table does not have a column named "type". The first mysql_query statement contains type = '2' as a condition.
If you remove that part of the statement, it will work - However, I can't judge if the baviour will be correct then :-)
Answer: The "type" column is missing from your table.
Answer: Change your query to this. ( I have removed the type column, since it doesn't exist )
INSERT INTO comments(target_id,user_id,comment,date_added) VALUES('{$movieid}','{$user_id}','{$comment}',NOW())

Unknown column 'type' in 'where clause'


CSS, firefox nao reconhece fonte Bree Serif

Question: Uso sources do google, porem obtive a surpresa hoje em que fui testar meus arquivos no firefox e percebi que a fonte nao era carregada. Veja
http://jsfiddle.net/xrckh1db/
@font-face {
font-family: 'Bree Serif';
font-style: normal;
font-weight: 400;
src: local('Bree Serif'), local('BreeSerif-Regular'), url(http://fonts.gstatic.com/s/breeserif/v5/0daoUMW28nkWOnFz2G4AAgsYbbCjybiHxArTLjt7FRU.woff2) format('woff2');
unicode-range: U+0100-024F, U+1E00-1EFF, U+20A0-20AB, U+20AD-20CF, U+2C60-2C7F, U+A720-A7FF;

}
@font-face {
  font-family: 'Bree Serif';
  font-style: normal;
  font-weight: 400;
  src: local('Bree Serif'), local('BreeSerif-Regular'), url(http://fonts.gstatic.com/s/breeserif/v5/LQ7WLTaITDg4OSRuOZCpswsYbbCjybiHxArTLjt7FRU.woff2) format('woff2');
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2212, U+2215, U+E0FF, U+EFFD, U+F000;
}
div {
    font-family: 'Bree Serif';
    }

Comment: Aqui abre bem em varios browsers. O problema também é que vc está disponibilizando em um formato só.
Comment: Esse negócio de local é complicado, falam que economiza banda se a pessoa tiver a fonte, mas nada garante que seja a mesma. E outra, linkar direto pra URL do google não é garantido que vá sempre funcionar (como vc percebeu). O google trabalha com @includes. Se não quer usar includes, copie a fonte pro seu servidor (e veja se é permitido na licença).
Answer: Talvez seja mais interessante utilizar o Quick-use, como o Google indica. Não coloco minha mão no fogo mas acredito que dificilmente você terá um problema de carregamento pelo "Google estar offline".
As três formas de importação descritas no Google Fonts:

Padrão: Importando diretamente no HTML

<!doctype html>

    <link href='http://fonts.googleapis.com/css?family=Bree+Serif' rel='stylesheet' type='text/css'>

StackOverflow

Importando no CSS com @import

@import url(http://fonts.googleapis.com/css?family=Bree+Serif);

.minha-fonte {
  font-family: 'Bree Serif', serif;
}

StackOverflow

Importando com Javascript

<!doctype html>

StackOverflow


How to auto-adjust container width when image inside is resized?

Question: I'm having an issue, which I will simplify here for sake of clarity (its really frustrating me).
I have a container with two divs in it (and one has an image inside, which will be resized), like so:
HTML

CSS
#container {
height:350px;
background-color: #000

}

img {

max-width: 60%;
height: auto;

}

#block1 {
background-color: #fff;
float:left;
height:auto;
width: auto;
border-style: solid;
border-width: 1px;
border-color: #000;
}

#block2 {
background-color: #456545;
height:100%;
overflow: hidden;
}

Here is an image with the problem
The image above illustrates my problem. After resizing the image inside #block1, the height of #block1 reduces, to be the same new height as the image (which is what I want). However, I want the width to do this as well, which doesn't happen. The width stays the same, so the width of #block1 does not change to be equal to the new size of the image (it stays the size of the original image).  I want the width of #block1 to automatically reduce in accordance with the new smaller size of the image.
Is this possible via CSS?
Thanks
Answer: Update:
In the event that you do not want your img element to occupy the full dimensions of the containing div, you can have the img element scaled by a percentage of the container for both height and width by setting the percentage for the padding property. See below for what I mean.
Old:
You can't have max-width set at 60% for the img and expect to have the width of the container match that of the img. See below when I have removed the max-width restriction:

*, :before, :after {
 box-sizing: border-box;
  padding: 0;
  margin: 0;
}

#container {
  height: 350px;
  background-color: #000
}

img {
  width: 100%;
  padding: 0 50% 50% 0;
}

#block1 {
  background-color: #fff;
  float: left;
  height: auto;
  width: 20%; /* change this to see that the div fits the image properly. */
  border-style: solid;
  border-width: 1px;
  border-color: #000;
}

#block2 {
  background-color: #456545;
  height: 100%;
  overflow: hidden;
}

Comment: Thanks for your answer! I'm still having the same issue though. If you reduce the img height and width to 50% both in your code above, for example, the issue from the picture I posted still continues. The height scales but the width does not reduce. Unless I'm doing something wrong..

img {
  height: 50%;
  width: 50%;
}
Comment: If you want to match the container's dimensions with that of the image, change the container dimensions and not the image dimensions.
Comment: Awesome, it worked!! Thanks for all the help, wish I could give 100 upvotes lol
Comment: You're welcome! I'll take the one you can give, though. :)


Matching continuous function through moments

Question: Let $(h_n)$ be a sequence of continuous functions $h_n : [0,1] \to \mathbb R$ such that $$\sum_{i=0}^{\infty} (\int_0^1 h_n(x) x^i dx)^2 \to 0, \;\; (n \to \infty).$$ Does it then follow that $\int_0^1 h_n(x)^2 dx \to 0$ as $n \to \infty$?
Answer: No, the functions could have a peak near $0$, or rather double $\pm$ peak to have zero integral. Like this:
$$
h_n(x) = n\chi_{[1/n,2/n]} - n\chi_{[0,1/n]}
$$
Then the $0$th moment vanishes, and for $i\ge 1$,
$$
\int_0^1 h_n(x)x^i = n\frac{2^{i+1}-2}{(i+1)n^{i+1}} \le \frac{2^{i+1}}{(i+1)n^{i}}
$$
One can see that the sum of squares is $O(n^{-2})$.
Yet, $\|h_n\|_{L^2}\to\infty$.


Kullback–Leibler divergence between multivariate t and the multivariate normal?

Question: I want to calculate the Kullback–Leibler divergence between a multivariate $t$ distribution and a multivariate normal distribution, for different values of the degrees of freedom $\nu$.
However, this requires a multiple integration that seems to be difficult to calculate numerically for dimensions larger than 2. Is there a known result to calculate this integral or a numerical trick?
I understand there are general multivariate numerical integration methods. I was just wondering if there is a simpler ad hoc tool I could use as these are popular distributions, so I guess there may be some simpler tools.
Comment: Monte Carlo integration?
Comment: @seanv507 Thanks, that is too general to be of any use, but I appreciate the pointer.
Comment: t-distribution is a scale mixture of Gaussian distributions and fining KL divergence to mixtures is notoriously difficult. You can lower bound on it if you are interested.
Comment: This paper has what you are looking for. The final solution is on page 8 and is quite cumbersome. https://link.springer.com/content/pdf/10.1007/s40304-019-00200-8.pdf
Answer: There is a numerical solution based on one-dimensional numerical integrals here:
Kullback Leibler divergence between a multivariate t and a multivariate normal distributions

I doubt there is a closed form solution, but the 1D numerical integral seems simple.


What is the formula for computing residual in fipy?

Question: I know that we can get the residual if use 'sweep' to solve the equation, but what is the formula for computing residual in fipy?
Is it the difference between the two sides of the equation?
And why the residual of my equation decrease very slowly?
Can anyone help me? Thanks !
Answer: The residual returned by sweep() is the L2-norm of L*x - b (where L is the matrix, x is the solution vector, and b is the right-hand-side vector for the system of linear equations obtained from discretizing your equation(s)). I have filed issue #734 to add documentation of this.
As to why the residual is decreasing very slowly, that's impossible to say without knowing what equations you are solving and how you are solving them.
Comment: Thank you from the bottom of my heart. Wish you have a nice day.


Setting/getting values through pointers in C++

Question: I'm trying to learn how pointers work in C++, except for the last hour I've only managed to make it crash.
#include<string>

// a sample structure
typedef struct test_obj_type_s{

    // with some numbers
    int x;
    float y;
    double z;

    std::string str; // a standard string...
    char * str2; // a variable-length string...
    char str3[2048]; // a fixed-length string...

    struct test_obj_type_s *next; // and a link to another similar structure
} test_obj_type_t;

// a sample pointer
test_obj_type_t * last_test_obj;
test_obj_type_t obj;

// let's go
int main(){

    // let's assign some demo values
    obj.x = 12;
    obj.y = 15.15;
    obj.z = 25.1;
    obj.str = "test str is working";
    obj.str2 = "test str2 is working";
    strcpy_s(obj.str3, "test str3 is working");

    // let's also assign some circular references
    obj.next = &obj;

    // now...
    last_test_obj = &obj;

    test_obj_type_t * t1 = last_test_obj;
    test_obj_type_t t2 = obj;

    // now let's have some fun;
    printf("%d %d %d %s %s %s", t2.x, t2.y, t2.z, t2.str, t2.str2, t2.str3);
    printf("%d %d %d %s %s %s", t2.next->x, t2.next->y, t2.next->z, t2.next->str, t2.next->str2, t2.next->str3);
    printf("%d %d %d %s %s %s", t1->x, t1->y, t1->z, t1->str, t1->str2, t1->str3);
    printf("%d %d %d %s %s %s", t1->next->x, t1->next->y, t1->next->z, t1->next->str, t1->next->str2, t1->next->str3);

    printf("I survived!");
}

What am I missing?
Or what am I doing wrong?
Comment: Why are you using `printf` in C++
Comment: << from iostream, and you'll get rid of your crash, since I believe you have an answer.
Comment: iostream- `std::cout`
Comment: "I'm trying to learn how pointers work in C++, except for the last hour I've only managed to make it crash." - and it will stay like this for the rest of your career. Because even experienced senior software developers create pointer bugs.
Comment: @EdHeal what should I be using
Answer: Your program exhibits undefined behavior when it passes an std::string to printf. %s format specifier expects a const char* as a parameter. Everywhere you have something.str or something->str in your series of printf statements, replace it with something.str.c_str()
Similarly, %d expects int as a parameter, but you are passing double. To print something.y and something.z, use %f specifier instead.
Answer: You can move to the c++ stream output, it's more simple way to do such things:
// now let's have some fun;
std::cout<<t2.x<<t2.y<<t2.z<<t2.str<<t2.str2<<t2.str3<<std::endl;
std::cout<<t2.next->x<<t2.next->y<<t2.next->z<<t2.next->str<<t2.next->str2<<t2.next->str3<<std::endl;
std::cout<<t1->x<<t1->y<<t1->z<<t1->str<<t1->str2<<t1->str3<<std::endl;
std::cout<<t1->next->x<<t1->next->y<<t1->next->z<<t1->next->str<<t1->next->str2<<t1->next->str3<<std::endl;

Comment: Just what happens if `"%d %d %d %s %s %s"` in the OP's answer actually comes from a user-defined language-dependent setting? `std::cout` and friends cannot always make up for the flexibility of format strings. Boost.Format tries to combine the flexibility of format strings with the type safety of C++.


Passing global variables to alter hooks

Question: The problem I have is that it seems that I can use global variable created inside another module, only if I use it inside callback function. I successfully tested it by inserting code inside existing modules.
The global variable is $subtotal.
function uc_checkout_pane_simple_quote($op, &$arg1, $arg2) {
    global $subtotal;
drupal_set_message($subtotal);

function uc_webform_pane_checkout_pane_callback($nid, $op, &$arg1, $arg2) {
  global $subtotal;
  drupal_set_message($subtotal);
}

In both cases the message shows the correct value.
However, inside other functions it is does not work.
function uc_webform_pane_checkout_pane_alter(&$panes) {
  global $subtotal;
  drupal_set_message($subtotal);
}

function uc_cartdetails_checkout_pane_alter(&$panes) {
  global $user;
  global $subtotal;
  drupal_set_message($subtotal);
}

What am I missing here?
What I am trrying to do: to show or hide checkout pane based on $subtotal value.
function uc_cartdetails_checkout_pane_alter(&$panes) {
global $subtotal;
  foreach ($panes as &$pane) {

    if (($pane['id'] == 'webform_nid780') && ((40 - $subtotal) > 0) ) {
      $pane['enabled'] = FALSE;
      }

    }
 }

Answer: The problem is not the global variable, but the fact you are using a function like drupal_set_message() inside a alter hook, which is not necessarily invoked when Drupal is outputting a page.
If you want to debug a hook, it's better you go with watchdog() and check in admin/reports/dblog if there is any message. You need to enable the Database Logging or the Syslog module to see any output; in the latter case, you will see the output on the syslog of your computer, instead of admin/reports/dblog.
Comment: Hi, the result is the same. I found out that I can get the correct value even from this function "function uc_cartdetails_form_alter(&$form, $form_state, $form_id) {..." however from inside "function uc_simple_quote_checkout_pane_alter(&$panes) {..." I am not able to get anything. Possible that there are some arguments missing? I am really out of ideas.
Comment: If you don't get anything from `watchdog()` then something else is wrong, but calling it from those hooks should work, supposing that those are really hooks. In the case you misspelled the hook names, then the code will not be executed, and you will not get any output.
Comment: everything seems ok. No spelling errors. I have one module (uc_cartdetails) with 2 functions, where calling global variable from both functions gives ok and zero result. Function uc_cartdetails_form_alter(&$form, $form_state, $form_id) {global $subtotal;watchdog('title',$subtotal); - gives result and function uc_cartdetails_checkout_pane_alter(&$panes) {global $subtotal; watchdog('title', $subtotal); - no result
Answer: I'm not sure why globals don't behave well in Drupal, but it seems like what you're really asking is how to get global-like behaviour.
What you want is drupal_static. Your code should look something like:
function uc_webform_pane_checkout_pane_alter(&$panes) {
  //'uc_checkout_subtotal' can be whatever you like.
  //I assume '0' is a good default for this variable.
  $subtotal = &drupal_static('uc_checkout_subtotal', 0);
  drupal_set_message($subtotal);
}

function uc_cartdetails_checkout_pane_alter(&$panes) {
  $subtotal = &drupal_static('uc_checkout_subtotal', 0);
  drupal_set_message($subtotal);
}

...and so on, applying that same pattern wherever you need to write or read your global values.
Answer: Well, for whatever reason inside ubercart alter functions I was not able to use globals. I ended up with basic altering of form like this:
function hook_form_alter(&$form, $form_state, $form_id) {

 if ($form_id == 'uc_cart_checkout_form' && isset($form['panes']['webform_nid780'])) {
 global $subtotal;
 if ((40 - $subtotal) > 0)  {
 $form['panes']['webform_nid780']['#access'] = FALSE;
    }

 }
}

which works no problems.

Passing global variables to alter hooks


Why does $\epsilon$ become a continuous function of $\Delta x$?

Question: This Proof of Chain Rule comes from the textbook of Stewart

I'm stuck here: " If we define $\epsilon$ to be $0$ when $\Delta x=0$, then $\epsilon$ becomes a continuous function of $\Delta x$".
How come $\epsilon$ becomes a continuous function of $\Delta x$?  $\quad$ Isn't $\epsilon$ just continuous at $\Delta x=0$?
Thank you so much!
Comment: You are absolutely right, except if somewhere earlier in the text it was specified that $f$ is continuous. Then $\varepsilon$ would also be continuous. It makes no difference for the chain rule, though.
Comment: I prefer to write $f(a+h)=f(a)+f'(a)\cdot h+o(h)$, or $f(a+h)=f(a)+f'(a)\cdot h+\psi(h)$ where $\lim_{h\to0}\psi(h)/h=0$
Answer: if $\epsilon$ is continuous in $\Delta x$ then $\epsilon$ is bounded in any closed interval around $0$:
$I=[-\Delta x, \Delta x]$ this prevents $\epsilon$ from becoming infinite when $\Delta x=0$.
and the limit exists.


Atribuir cor de fundo na DIV quando ordenar por ordem alfabética no jQuery

Question: Boa noite!
Estou tentando montar uma lista da seguinte maneira:

id

status

C

1

C

2

A

3

  <input type="hidden" id="acao" value="asc" />

O formato que é impresso é o mesmo de uma tabela, porém com div's.
Para facilitar a visualização da linha, eu crei uma linha com background FFF e a outra DCDCDC e assim por diante, ficando 'cor sim, cor não', porém quando eu faço a ordenação, as cores ordenam juntos fazendo com que a funcionalidade da cor perca o objetivo, que é facilitar a visualização.
Há alguma maneira de fazer a correção da cor na função abaixo?
    $("#orderStatus").click(function(){
        var acao = $('#acao').val();
        var statusList = $(".listStatus");
        statusList.sort(function(a, b) {
            if(acao == "asc"){
                $('#acao').val('desc');
                var res = $(b).attr("status") > $(a).attr("status");
            }
            else{
                $('#acao').val('asc');
                var res = $(a).attr("status") > $(b).attr("status");
            }
            return res;

        });

        $('#container').html(statusList);
    });

Comment: O certo seria você usar css para montar o esquema de 'cor sim e cor não'. Usando `nth-child(even)` ao invés de `style`
Answer: Sugiro fazeres isso com CSS e tirar a lógica das cores do HTML:

#container div div:nth-child(2n){
     background-color: #dcdcdc;
    }

    #container div div:nth-child(2n + 1){
     background-color: #fff;
    }

id

status

C

1

C

2

A

3

    <input type="hidden" id="acao" value="asc" />

Comment: Sergio, deu certo!!! Muito Obrigado!
Comment: Acabei me explicando mal e o entendimento foi outro, a saida que eu desejo é a seguinte
`id | status
 c  | 1
 c  | 2
 a  | 3`
Onde id e status são uma linha, c e 1 são outra linha, c e 2 são outro linha , e por fim, a e 3 são uma outra linha. A solução acima faz com que as colunas mudem de cor. Mas agradeço pela dica, não sabia que dava para fazer isso no CSS3
Comment: @BrunoFolle ok, então poderias fazer assim: https://jsfiddle.net/zw50xtx0/1/ também só com CSS...


Can I load a figure stored as an HTML file (generated using Plotly's python library) within Dash?

Question: I've used Plotly's Python library, but I'm a newbie to Dash.
Here's my use case:
We have a Wordpress site that has a section called "dashboard". The site has multiple users and we'd like to show different charts/dash apps to each user based on only their data.
So far, I've succeeded in embedding the HTML generated using Plotly's library within Wordpress. This is sufficient but not very aesthetically pleasing. What I want to do next is when a user clicks on "dashboard", they'd be directed to a dash app, which will be running as a docker container on, say, AWS ECS. One container for each customer.
So, here's the main question: We generate charts for each user separately and store them on S3 at the moment. So when my dash container is running, is there a way for me to load a particular chart (HTML) and embed that into my Dash app?
The reason I'm asking this is - the charts use data from a MySQL database and if dash requires a constant connection to the data source to render the charts, that'd be terribly slow.
Comment: You could store your data as JSON in S3 and parse it in dash - Boto3 helps you to interact with AWS services
Comment: is there a way to load graph stored as JSON in Dash? Could you point me to the API reference if possible?
Comment: You can just parse your json in python and use it as data input. `json.load()`
Comment: I know how to parse json in python, I'm also aware of boto3. I'm asking if there's a way to use a graph stored as JSON or HTML in Dash?
Comment: Relevant: https://stackoverflow.com/q/67380802/1256347
Answer: Found a pretty simple solution for this use case:
Plotly supports exporting a Figure object as JSON.

fig.to_json()

Dump this into a file and store onto the disk or S3.
You can regenerate the original figure object from the JSON like so:
import plotly.io as pio

with open('path_of_fig_stored_as_json', 'r') as f:
    fig = pio.from_json(f.read())

Can I load a figure stored as an HTML file (generated using Plotly’s python library) within Dash?


Archetypical user setup for capistrano to tomcat deployment on Linux box

Question: I'm relatively new to the CD and DevOps topics and I am trying to setup automatic deployments with capistrano, which works well on my local add hoc test environment. I wonder however how one would ideally like to setup the users on the future target environment.
E.g. the Ubuntu Tomcat installation procedure sets up a tomcat(6|7) user who owns the tomcat process. It is not possible to ssh into the box using this user, though, and for capistrano you need a user with ssh access for running all commands.
How do people normally solve this problem? Of course I thought of my own solution but I'm interested in hearing if there is a best practice for this.
There will be another party setting up my target environment in the end, but I can have a say in this so I would like to know what I ideally want here.
Answer: There are a variety of methods for doing it, but the top one is to set up the sudo system so the SSH-enabled deployment user (which you'll need to set up) can impersonate tomcat or root as needed for doing deployments. The actual code directories may be owned by tomcat, or may be owned by the deployment user with rights set so that tomcat can use the files.
<code>desc "Restarts the tomcast service"
task restart_tomcat do
  set :user, "deployuser"
  run "sudo service tomcat restart", :roles => :tomcat_servers
end
</code>
Or suchlike. If sudo requires a password, capistrano is smart enough to prompt you for it.
Comment: Thanks, I'll try that. I'm using puppet to provision the target environment and I'm new to that, too, so I haven't managed to completely validate your solution, yet. It looks promising though. Some fiddling with the sudoers file today should do the trick.
Answer: Check the package's official recommendation on how to set it up. Look at how your choosen distribution manages the issue in their package. There might be some HOWTO for the distribution or the package, look for those. But be careful when reading on how this was solved in some other version, it often happens that configuration and capabilities change radically from one version to the next (and other supporting packages might also have changed).


Testing a t-statistic from an unconditional linear mixed effects regression (LMER) model

Question: Trying to figure out if I should conduct an Hierarchical Linear Modeling (HLM) analysis with a data set. The output for the unconditional model gives me this.

Yes, I know the intraclass correlation coefficient (ICC) is large, but I'm trying to determine 'significance' of the model without the ICC. If the model is significant and the ICC is large, I know to go through the analysis. So what I'm looking for/ hoping to get advice on is how to comfortably interpret that t value, which seems of rather small magnitude.
Any and all advice for this is appreciated, but actual answers are the best :) ;)
Thanks in advance!
Comment: It's hard to understand what you are asking here. What do you mean by the 'significance of the model'? significance with respect to what?
Answer: It seems that you are trying to determine whether the model is significant based on the t test for the fixed intercept. However, the insignificance of the fixed intercept only indicates that the population average of the outcome is not significantly different from 0. In contrast, the variance estimation results reveal that the outcome varies a lot both within and across schools, with an ICC of 0.24.


Unity Transform position isn't changing position

Question: I am trying to change the position of a border to be just off the top of the screen. I did this by taking the top of the screen, then adding half the scale of the object (since it is centered in the middle). However, when I do this nothing changes at all. I printed out the coordinates and it says it moved, but it didn't. Here is my code (from the Start() method in WallScalar class).
Note: The following code is the only code in the WallScalar class, this script is applied to the "Top" gameObject.
      transform.position =
      new Vector3(0, Camera.main.orthographicSize + (transform.localScale.y / 2), 1);

      print("yCoor: " + transform.position.y);

Since the object has a scale of one, and the top is at y = 5, the object should be placed at y = 5.5f. With that print statement, it says it is at y = 5.5f but it didn't move on my screen. Here is an image for more details:

As you can see, it says it was moved to yCoor = 5.5f, but it didnt, because when I manually type 5.5 for y, it isn't visible, it is just off the screen.
Any help with figuring this out would be greatly appreciated.
Update: If i put a print statement such as
      print("yCoorU: " + transform.position.y);

In my update method, it reads y = 5, so something is forcing it back to be 5, but know that nothing else has any call to the "Top" GameObject that the script is being applied to.
Comment: what does the wall scalar script do ? Perhaps its moving your wall back to 5?
Comment: @UriPopov I will edit and clarify, that is where the code you see above lives.
Comment: what is the size of your orthographic camera ?
Comment: another question.Is walls the parent object of Top at 0,0,0 ?
Comment: @UriPopov it is, and it is of size 5
Answer: So, what I figured out is that you have to disable the object you are trying to change position on, set the position and than enable it.
Player.SetActive(false);
Player.transform.position = new Vector3(PositionX, PositionY, PositionZ);
Player.SetActive(true);

This might be because during gameplay the player or the object you are trying to move is in static mode.
Chirag Shah
Answer: Since you are dividing by an int, I think that the result of transform.localScale.y / 2 might be 0 instead of 0.5 (although I don't undestand why your print would give the right value)
Comment: **Camera.main.orthographicSize**`+ (transform.localScale.y / 2)`. You forgot the `+Camera.main.orthographicSize` part. so getting float is not a surprise here.
Comment: I don't think that it makes a difference. the part in parenthesis might be cast to int
Comment: I read your answer wrong. My comment is a reply to **I don't understand why your print would give the right value**. I thought you said **I don't understand why your print would give a float value**. I read it again and realized it's a different stuff...
Answer: I managed to replicate your problem by having a 0.5 value of the Y of the parent object. Make sure your parent object is at 0,0,0
Comment: It is, I double checked
Comment: only other thing I can think of is that the scale of the parent might be affecting it.


Ordem dos comandos no robots.txt

Question: Olá, encontrei um arquivo robots.txt com a seguintes diretivas dentro do arquivo

User-agent: Googlebot

Allow: *

User-agent: Bingbot

Allow: *

User-agent: facebot

Allow: *

User-agent: *

Disallow: /

Minha pergunta é, o último comando de disallow não vai cancelar todos os anteriores?
Answer: Não vai bloquear, as regras não são necessariamente "em ordem", as regras com o "nome" citado tem "preferência" sobre um wildcard/curinga (*), como no próprio exemplo do suporte google:

https://support.google.com/webmasters/answer/6062596?hl=pt-BR

Permitir acesso a um único rastreador
User-agent: Googlebot-news
Allow: /

User-agent: *
Disallow: /

Permitir acesso a todos os rastreadores, exceto um deles
User-agent: Unnecessarybot
Disallow: /

User-agent: *
Allow: /

Outra situação que deve notar, é que o uso de um asterisco (*), corresponderá a todos os rastreadores, exceto aos diversos rastreadores Google AdsBot, que precisam ser nomeados explicitamente
Comment: @hugocsl isso sim, bloqueia todos, mas na "ordem", o "allow 'explicito'" aparentemente dá preferencia ao BOT "nomeado", mas depende de como vai escrever, no caso revisei a resposta é `um wildcard` e não `o wildcard`, logo talvez em breve eu precise reformular melhor a resposta para detalhar exemplos que sobreescreveriam, mas preciso ter certeza de tudo, pq só me tive a base os textos do suporte google, e talvez tenha "perdido" algo, além do que preciso complementar.
Comment: Eu achava que p `Disallow: /` negava o acesso a todos os diretórios internos, tipo publicHTML contina com acesso, mas publicHTML/configuration o bot não teria acesso. Tipo que o bot só enxergaria o que está na raiz, e não as pastas dentro do diretório... Mas acho que eu estava achando errado rss

Ordem dos comandos no robots.txt


For loop not working with js map function

Question: I am trying to print array in html, but my for loop is not working. I am trying to get the length of dataArrayNew but its not returning back where my for loop is added. If i console.log(dataArrayNew.title); , I am able to see the correct result. I am also attaching the print screen of my browser console.

var dataArrayNew = [];

function fetch_section_data_1(){

var keys = Object.keys(localStorage).filter(function(key) {
  return /^section\d+$/.test(key);
});

var dataArray = keys.map(function(key) {
    dataArrayNew = JSON.parse(localStorage.getItem(key));
    console.log(dataArrayNew.title);
    //lengtharray = dataArrayNew.length;
    //console.log(lengtharray);
    //return JSON.parse(localStorage.getItem(key));
});

var $table = $( "

" );
for(i=0;i<dataArrayNew.length;i++){
    var array_no = dataArrayNew[i];
    var $line = $( "

" );
    $line.append( $( "	" ).html( array_no.title ) );
    $table.append( $line );
    console.log(dataArrayNew.title);
}

$table.appendTo(document.body);

}

Comment: Your `.map()` callback doesn't have a `return` statement. It should. Add `console.log(dataArray)` after the `.map()` and you'll see why.
Comment: i uncommented my return statement and added what you suggested, i can see this in my console - `https://www.dropbox.com/s/nexj0maebb3xm35/Screenshot%202017-05-17%2008.36.13.png?dl=0`   but still for loop not working
Comment: ok it worked , if i click on the title, how can i get its corresponding id from array, please suggest
Answer: The reason why you cannot see the length property is because you are reassigning dataArrayNew from an array to a result JSON.parse which is an object, and objects do not have the property length. Instead of reassigning the value of dataArrayNew, why don't you push the value of JSON.parse(localStorage.getItem(key)) to it, like so:
dataArrayNew.push(JSON.parse(localStorage.getItem(key)));

So your code should look something like this:

var dataArrayNew = [];

function fetch_section_data_1(){

  var keys = Object.keys(localStorage).filter(function(key) {
    return /^section\d+$/.test(key);
  });

  var dataArray = keys.map(function(key) {
    var currIn = JSON.parse(localStorage.getItem(key));
    console.log(currIn);
    // push data to dataArrayNew
    dataArrayNew.push(currIn);
    //lengtharray = dataArrayNew.length;
    //console.log(lengtharray);
     //return JSON.parse(localStorage.getItem(key));
  });

  var $table = $( "

" );

  // You should be able to get the length here now
  for(var i = 0; i < dataArrayNew.length; i++){
      var array_no = dataArrayNew[i];
      var $line = $( "

" );
      $line.append( $( "	" ).html( array_no.title ) );
      $table.append( $line );
      console.log(dataArrayNew.title);
  }

  $table.appendTo(document.body);

}
Comment: Yeah I did something similar, I just used dataArray itself after the map function, how can I console.log the id of the title I clicked
Comment: In that case, it would be `console.log(currIn.title)`
Comment: How to add click on title to get corresponding id
Comment: You probably need an event handler for that. Look up onClick on MDN: https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XUL/Attribute/onclick


Two variable sorting algorithm

Question: I'm looking for an algorthm for sorting location points (latitude and longitude) from west to east and south to north.
When sorting, the points should be sorted starting in the west and south. When two points are compared, the longitude is compared first. The greater value (more west) point is higher in the list. If the two points have the same longitude, unlikely but possible, the latitudes of the two points are compared. The lowest value (more south) is placed higher in the list.
Does this algorthm exist somewhere? Maybe in C#?
ps- these calculations will be limited to points within the continental United States. There will be no negative latitude / longitude values.
Comment: It's not clear to me why this would require anything more sophisticated than the ordinary built-in sorting functions in the .NET Framework (e.g. `List.Sort`, `Enumerable.OrderBy`.)  The comparison function for the sort would presumably compare first the longitude, and then the latitude, just as you described.
Comment: Check out the second answer here: http://stackoverflow.com/questions/289010/c-list-sort-by-x-then-y . Alternatively, define your own comparison function and pass that to any standard sorting algorithm.
Comment: I would know how to do it in php, or try looking at LINQ
Answer: using System.Linq;

var sortedPoints = points.OrderByDescending(p => p.Longitude).ThenBy(p => p.Latitude);

Comment: I think he said Longitude first.
Answer: The algorithm doesn't exist out-of-the box in .NET (C# is a language and doesn't generally implement algorithms, you will typically find it in the .NET Base Class Library).
However, you could easily create a Coordinates structure/class with Latitude/Longitude properties (each as a double, I take it) and then implement IComparable<T>.
The implementation would then look something like this:
public class Coordinates : IComparable<Coordinates>
{
    public double Latitude { get; set; }
    public double Longitude { get; set; }

    public int CompareTo(Coordinates other)
    {
        // If the other instance is null, assume that
        // it is at 0,0?  You need to make that determination.
        if (other == null) return 1;

        // Compare longitude (double implements
        // IComparable<double>.
        int comparison = Longitude.CompareTo(other.Longitude);

        // If not 0, return the value.
        if (comparison <> 0) return comparison;

        // Compare latitude.  Inverse the result, as the more
        // south point (closer to 0) is greater.
        // Just return the value, if they are different, the
        // comparison value will be correct, if they are the
        // same, then comparison will be 0.
        return -Latitude.CompareTo(other.Latitude);
    }
}

Now, you can populate instances of these, place them in an array and pass it to the static Sort method on the Array class.  The Sort method will use the IComparable<T> implementation to sort the array.
Or you can place them in a List<T> (probably easier, since you might not know the number of elements beforehand) and then call the Sort method on the instance; it too will use the IComparable<T> implementation to sort itself.
You also mentioned two points being the same.  Since Latitude and Longitude are represented as a double, you run the risk of floating point errors.  If you want to mitigate these errors, you can easily change the property to a decimal (which guarantees precision, up to a certain point); this way, you will guarantee precision, and it implements IComparable<decimal>, which means that the implementation of IComparable<Coordinates> will just work with the switch.
Comment: Frequently Lat and lon are scaled to integers which are large enough to carry the precisions desired.

Two variable sorting algorithm


Will practice improve the tone on my new flute?

Question: I just got a new flute- a good quality Yamaha that was quite expensive. I would love to say that it plays perfectly-but unfortunately, that's not quite the case.
Whenever I play it, it sounds a little raspy/wispy, and the notes aren't very clear. I haven't played it very much, and someone has advised me that because the flute is new, it needs time for all the pads to adjust. When I start to practice it more, it will start to sound better. The same person also told me that because the flute is more advanced than my beginner flute, I need to focus my air and play stronger, or with more air.
I did try to practice more (although I still haven't practiced it that much), and the tone quality does improve when I sit up straighter and hold my flute at a certain angle (I read an article with tips to improve tone).
However, I kind of need to know- will practicing it (or the other tips that I have been given/I have found) work to improve the flute's tone? Or is there a problem and will I need to send the new flute back?
Please also advise any more tips to improve tone quality, if you have any.
Thank you so much!! Good day!!
Comment: By any chance, did the upgrade involve changing from a flute with no open hole keys to one that does have some open whole keys?
Answer: Practicing only helps when it is done correctly. The problems are likely not due to your flute, but are most likely due to your embouchure or posture/breathing, throat openness, etc. You need a good teacher to help you learn how to produce a good quality sound throughout the whole register of the flute. An in-person teacher is especially important for helping to identify posture issues and also demonstrate first-hand proper breathing and such. A second option is to try a skype lesson. I don't think it is as useful as an in-person teacher, but would be better than trying to interpret words or videos online. You really need someone to look at what you are doing. Advanced players can make a lousy instrument sound great, but a fantastic instrument cannot make a beginner sound amazing.
Answer: Yes, practice will improve tone. At least, good practice will.
I'm not a flute player, but I did buy a new saxophone last year. Went from an Armstrong student model that was very old, to a new Yamaha YTS-62. It took me some time to unlearn habits, and start getting a better tone on the new instrument. I don't see why that won't be the case for a flute. Particularly if you haven't been playing for very long.
The best tip I can give is to find either a teacher, or a friendly flute player with a bit more experience. They will be much more help that random people on the internet. They will be able to play your flute, and tell you if there's anything physically wrong with it. If there's a local community concert band or orchestra, they might have someone who can help you. Of course, quality will vary; you'll find brilliant musicians, and those who could be better characterised as flute owners than flute players. Paying money for a teacher is more likely to get a better quality of musician (although not always).
There are also some existing questions on this site that might help you. Here's a couple I found with a quick search:

Flute: Techniques for clear sonorous sound
How do I get my flute to sound clean?

To be fair, the answers do tend to be"practice more".
Answer: Practising is not going to improve the tone of your flute. Assuming it's working properly and hasn't been damaged in transit, a brand new flute sounds as good as it's ever going to sound, however it might take you a while to adjust to a different embouchure hole. If the flute is not working properly, no amount of practice will make it better.
Practising, assuming you're doing it correctly,  will improve your ability to get a good sound out of any flute.
Comment: @SteveMansfield Of course I'm assuming that a brand new instrument will be working properly. But whether it is or isn't working properly, practising on it is not going to improve *the flute*.  That's what the question asks.
Comment: A player will only get the best out of any instrument if they've got good technique. You can't possibly say that the tone is the best it's ever going to be until an experienced flute player has played the same flute and can tell whether the problem is with the flute or the player.

If I played a Stradivarius violin it would sound absolutely appalling, but that's because I've no idea how to play violin, not because the instrument is bad.


Kafka Topics--should I have more or fewer of them?

Question: We are new to Kafka, so I am looking for some high level guidance.  We have data for a single entity (we can call it an "Order") that is essentially a number of different entities (we can call one a "Widget" and one a "Gizmo," but there are about 20 different entity types).
Obviously, there is benefit to thinking of Orders as a single topic because all the parts are related to one order.  But design wise, does it make more sense for these to be separate topics (Orders, Widgets, Gizmos, etc.)?
There is no direct correlation between the Widgets and Gizmos--the benefit of keeping them together would be things like order of processing, etc.  And suggestions or good resources to read would be very helpful.  Thanks!
Comment: For reading - https://stackoverflow.com/questions/17205561/data-modeling-with-kafka-topics-and-partitions
Answer: I would recommend initially recording the event as a single atomic message, and not splitting it up into several messages in several topics. It's best to record events exactly as you receive them, in a form that is as raw as possible. You can always split up the compound event later, using a stream processor—but it's much harder to reconstruct the original event if you split it up prematurely. Even better, you can give the initial event a unique ID (e.g. a UUID); that way later on when you split the original event into one event for each entity involved, you can carry that ID forward, making the provenance of each event traceable.
Comment: The above answer is an excerpt from
https://www.confluent.io/blog/put-several-event-types-kafka-topic/. Ideally the article should have been cited in your answer.


How to map value from nested dictionary to multiple columns in dataframe

Question: For example: I have df like this:
id      Status         Country       Income
1          4               2          3
2          5               3          2

and dictionary like this:
d_dict = {Status : { '4':'Married', '5':'UnMarried'},
        Country: { '2': 'Japan' , '3': 'China'},
        Income: {'3': "5000-10000", 2: "11000-20000"}}

I want to map the values based on nested dictionary. I can do for one column like this:
for k,v in d_dict.items():
    max_d[k] = max(v, key=v.get)
df['Status'] = df['Status'].map(max_d)

But I have more than 2000 columns and I am not sure how I can do for multiple columns.
Answer: You can try replace
df=df.astype(str).replace(d_dict)
df
Out[259]:
   id     Status Country      Income
0   1    Married   Japan  5000-10000
1   2  UnMarried   China 11000-20000

Comment: there are some columns where i don't need dictionary or i can't use... Is there any way to handle that?
Comment: Key of dictionary is always the name of column and if it matches then it should use the dictionary.
Comment: @s_khan92 not sure what you mean , the replace is match the columns first , then replace value within the dict
Comment: @s_khan92 first key in dict will match the columns , the second key will look the columns value replace the value in dict
Comment: I dont know why but its not able to match.. and giving wrong values


Improve estimate of zeros and how to deal with concurvity in GAM NB

Question: I'm trying to model the number of deaths for each age, for 4 regions, across 5 years. I fitted several models, but end up with the following model:
m1 <- gam(deaths ~ ti(region, bs='re') + ti(ageCenter, bs='cr', k=40) +
        ti(ageCenter, region, k=c(40,4)) + ageCenter*yearCenter + offset(log(PopMedia)),
        family = nb, data = male, method = "REML")

The model is reasonable, all variables are significant. However, the concurvity is not great for region:
         para ti(Regiao) ti(ageCenter) ti(ageCenter,Regiao)
worst       1  0.8335333   1.000000000         0.0635483181
observed    1  0.8335333   0.007150858         0.0003614262
estimate    1  0.8335333   0.029291067         0.0005498903

Nevertheless, I compared the observed number of deaths with the predicted and the estimate was low for one region (the smaller region). I tried to improve the model and I get a closer estimate for the smaller region but another region decreased the estimate number of zeros. This is the new model:
m2 <- gam(Obitosdx ~ ti(Regiao, bs='re') + ti(ageCenter, bs='cr', k=40, by=Regiao) +
        ti(ageCenter, Regiao, k=c(40,4)) + ageCenter*yearCenter + offset(log(PopMedia)),
        family = nb, data = male, method = "REML")

All variables are still significant, the concurvity remains unchanged as there's still ti(region) in the model. Residuals between the two models are similar.
My first question relates to the models. I'm not sure if it's appropriate to have ti(ageCenter, bs='cr', k=40, by=Regiao) in the same model as ti(ageCenter, Regiao, k=c(40,4)). Wouldn't the ti product interaction also give different smooths for each level of Region?
My second question is the concurvity for ti(region) worrisome? If the variable is significant how to deal with this?
These are the two models, comparing the m1 and m2 for the smallest region and a region intermediate. Do you have any suggestion for improvement? The estimate of the number of zeros is quite low. For the smallest region, the m2 estimates a closer number of zeros, while for the intermediate region, the m1 estimates a closer number of zeros. Any suggestion is most welcome!

EDIT
Following @Gavin suggestion, I've recoded Region to a factor variable, which should have been a factor to begin with. However, I still get a large deviation for young ages. Any suggestion? The plot above with the intermediate region corresponds to Alentejo, while the smallest to RAM.
Answer: You can't fit a smooth to a factor variable and this means that your ti(ageCenter, Regiao, k=c(40,4)), ti(Regiao, bs = "re"), and ti(ageCentre, by = Regiao) are not doing what you might expect them too.
That the former even works at all indicates that Region (or Regiao, which is it? Does it matter?) is not stored as factor variable. That means that the by smooth is actually a linear effect of Region modified by a smooth of ageCentre, and not separate smooths for the regions.
The high concurvity of the terms is most likely due to the above problems.
You are also going to need to be very careful with the ageCentre*yearCentre term as this will include a linear effect of ageCentre and somewhere among the bases of the splines you'll have a linear effect of ageCentre also, somewhere in the null space of one of the splines. This is going to introduce some identifiability issues; or it may, unless mgcv is being very clever to remove those effects (and I don't think it is doing this).
Assuming Region is a factor (it is isn't, recode it to one), then a model with separate smooths of ageCentre for each region plus the effect of ageCentre and yearCentre might be written as
m1 <- gam(deaths ~ ti(yearCenter) +
                   region +
                   ti(ageCenter, bs='cr', k=40, by = region) +
                   ti(ageCenter, yearCenter) +
                   offset(log(PopMedia)),
          family = nb, data = male, method = "REML")

I made the ageCentre by yearCentre thing a smooth, and I doubt you want to estimate a random effect with four levels (but if you must, put the re basis and smooth back on the region term).
Comment: Thank you for your response! So the problem is probably silly, since aparently `Region` is not a factor. But it should be =/ I thought I only needed `ti(ageCenter, bs='cr', k=40, by = region)` but I was confused as to why the model improved with `ti(Regiao, bs = "re")` and `ti(ageCenter, Regiao, k=c(40,4))`. I may be misunderstanding something but `ageCenter*yearCenter` should be similar to a non-smoothed `ti`? Completely wrong? I didn't included a smooth for year because I didn't thought it was relevant but an interaction age year would be, as we will tend to have an older population.
Comment: Thank you for your answer @Gavin. It was a stupid mistake. However, I still get a large deviation for the young ages, as seen in the residuals figure uploaded. I think an adaptive smooth would work instead of a `ti`. Do you think that would be appropriate?
Comment: You may have an issue in that the model may not be flexible enough to simultaneously predict low values and some higher counts. This would imply the expected count varies more than is allowed under the estimated NB distribution. This could be because the overdispersion parameter is not constant for all observations but may itself vary with covariates (i.e. you want a model for theta), or you may have zero-inflation, or the NB may not be a good conditional distribution for the response for other reasons.
Comment: I would look at a rootogram of the residuals (see a post i wrote on my blog for details on how to do that: https://www.fromthebottomoftheheap.net/2016/06/07/rootograms/ )
Comment: I actually think the GAM with an adaptive smooth works well. This model `gam(deaths ~ yearCenter + Region + s(ageCenter, bs='ad', k=50, by=Region) + offset(log(PopMedia)), family = nb, data =female, method = "REML")` is able to predict a closer number of zeros than using the `ti`. I will have a look at your page. Thank you for your help!


Carregamento de imagem dentro de laço for

Question:     var image = new Image();
    image.src = 'images/logos/jpg_group.jpg';
//  image.src = VARimg_categoria[y];
    image.onload = function() {
        var canvas = document.createElement('canvas');
        canvas.height = 250;
        canvas.width = 250;
        var context = canvas.getContext('2d');
        context.drawImage(image, 0, 0);
        var imageData = canvas.toDataURL('image/jpeg').replace(/^data:image\/(png|jpg|jpeg);base64,/, ""); //remove mimetype
        for(var y = 0; y < VARimg_categoria.length - 1; y++) {
            //image.src = VARimg_categoria[y];
            DatecsPrinter.printImage(
                imageData, //base64
                canvas.width,
                canvas.height,
            1);
        }
    }

Desta maneira ele esta carregando a imagem em image.src normal, mas preciso carregar as imagens (caminhos), da array: image.src = VARimg_categoria[y] dentro do laço for. Como faço isto tendo o tempo de carregamento do load()?
Answer: Se entendi bem, seu laço for tem que ficar por fora de tudo:
for(var y = 0; y < VARimg_categoria.length; y++) {
    var image = new Image();
    image.src = VARimg_categoria[y];
    image.onload = function() {
        var canvas = document.createElement('canvas');
        canvas.height = 250;
        canvas.width = 250;
        var context = canvas.getContext('2d');
        context.drawImage(image, 0, 0);
        var imageData = canvas.toDataURL('image/jpeg').replace(/^data:image\/(png|jpg|jpeg);base64,/, ""); //remove mimetype

        DatecsPrinter.printImage(
            imageData, //base64
            canvas.width,
            canvas.height,
            1
        );
    }
}

Comment: Por fora, se tem 3 img por ex. só imprimi 1! Não sei se é o retorno da função DatecsPrinter ou se é o load() que não acompanha o for;
Comment: O loop cria várias imagens e funções de onload, uma para cada imagem. Talvez o printImage é que não consiga lidar com uma fila de pedidos.
Answer: Em vez de usar for, você pode fazer um loop numa função cada vez que uma imagem for carregada.
// exemplo de uma array com caminhos de 4 imagens separadas por vírgula
caminhos = "https://homepages.cae.wisc.edu/~ece533/images/airplane.png,https://homepages.cae.wisc.edu/~ece533/images/arctichare.png,https://homepages.cae.wisc.edu/~ece533/images/baboon.png,https://homepages.cae.wisc.edu/~ece533/images/frymire.png";
var VARimg_categoria = caminhos.split(",");

var y = 0;
var image = new Image();
function carregaImagem(y){
    image.src = VARimg_categoria[y];
    image.onload = function(){
        // para exemplificar, esta linha joga as imagens dentro de uma div
        document.getElementById("dvd").innerHTML = document.getElementById("dvd").innerHTML+y+'.';

        // faz o que quiser aqui após o carregamento da imagem

        y++; // incrementa o y para ver se ainda há imagens
        if(y < VARimg_categoria.length){
            carregaImagem(y); // próxima imagem a ser carregada
        }else{ // else opcional quando todas as imagens tiverem sido carregadas
            alert("Todas a imagens foram carregadas");
        }
    }
}
carregaImagem(y); // chama a função

Dá uma olhada nesse Fiddle: https://jsfiddle.net/m6ft0jc3/3/
Comment: Deu certo, havia feito antes mas não tinha dado, acho que não estava passando o contador como parâmetro na função 'carregaImagem(y);' obrigado.


Spring security facebook authentication plugin with JSON doesn´t create a jsessionid

Question: I have a rest API made with Grails, I can use spring security via Ajax auth, and store the jsessionid cookie in the client, but when i login with 'j_spring_security_facebook_json?access_token=' it doesn´t create the jsessionid cookie... and i can´t store the session in the client.
Does exist any solution for this problem? thanks.
Comment: how do you use this api? from which client? just as ajax endpoint from same browser or standalone restful api?
Comment: Android app and iOS app. Thanks.
Answer: As you're using an external client (mobile app), in most cases you have to use something different for authenticating your requests. Not a cookie. Usually it's OAuth2 or just a custom signature based on a shared key.
j_spring_security_facebook_json auth is supposed to be used as an exchange point, where server can pass a key/token for mobile client that have valid fb token. Then this token could be used for authentication of following requests.
See "How to extend JSON response" at http://splix.github.io/grails-spring-security-facebook/guide/3%20Usage.html#3.5%20Json%20Authentication
Comment: Yes, i´m not using the OAuth2 plugin because i can´t know how to combine with your facebook plugin. I have the FacebookAuthService with custom fields in the 'input Map' but my problem is. How add a oauth2 token to the input map? Thanks Igor, and sorry for my english level.
Comment: it could be anything, oauth2 is just an example, it's for ideal scenario. basically you need any other security filter configured to authenticate mobile clients, you could write your own auth filter if you don't want OAuth


Add a taxonomy ID class to the body tag

Question: I have a taxonomy vocabulary whose various parents and children need to have different styles. I would need the tid of the every taxonomy page to appear as a class in the body tag in order to make CSS selectors.
I tried using How can I dynamically add URL or taxonomy to body class
to work out a solution, but it seems to work only for nodes. I get a class called path-taxonomy for every taxonomy term and that's it.
I would prefer not to install any module.
Answer: The linked example gets the path. Here better use routing. Check the name of the matched route and then get a parameter which is defined in the route. Normally you use getParameter() to get the loaded entity, in this case it's getRawParameter() for the id:
mytheme.theme:
function mytheme_preprocess_html(&$variables) {
  if (\Drupal::routeMatch()->getRouteName() == 'entity.taxonomy_term.canonical') {
    $variables['term_id'] = \Drupal::routeMatch()->getRawParameter('taxonomy_term');
  }
}

html.html.twig:
{%
  set body_classes = [
    term_id ? 'term-id-' ~ term_id,
  ]
%}

Comment: Awesome answer, thanks. At first I had huge problems with the changes not wanting to take effect, I spent literally an entire day clearing caches, setting up twig, and the changes appeared only this morning, just as I was thinking there was no more hope for my site.

Add a taxonomy ID class to the body tag


Executar Script em tela aberta por Modal em Ajax. C#

Question: Problema: Estou na view Index, nela tenha um botão que abre a view Create em uma tela modal, nessa tela modal quero executar um código javascript, mas por algum motivo o mesmo não está sendo executado.
Estou abrindo uma view em modal com o seguinte código:
$("#btnNovo").click(function () {
  $(".modal-body").load("/Pais/Create", function () {
    $("#exampleModal").modal("show");
  });
});

View aberta em modal.
@model Projeto.WebERP.EntityFramework.Entities.Pais
@{
    ViewBag.Title = "Create";
    Layout = null;
}

 Cadastro de País

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.HiddenFor(model => model.Handle)

                 Descrição:
                @Html.TextBoxFor(model => model.Descricao, new { type = "text", @class = "form-control", id = "txtDescricao" })
                @Html.ValidationMessageFor(model => model.Descricao, "", new { @class = "text-danger" })

                 Sigla:
                @Html.TextBoxFor(model => model.Sigla, new { type = "text", @class = "form-control", id = "txtSigla" })
                @Html.ValidationMessageFor(model => model.Sigla, "", new { @class = "text-danger" })

                 Data Cadastro:
                @Html.TextBoxFor(model => model.DataCadastro, new { type = "datetime", @class = "form-control", readOnly = true, id = "txtDataCadastro" })
                @Html.ValidationMessageFor(model => model.DataCadastro, "", new { @class = "text-danger" })

                 Data Alteração:
                @Html.TextBoxFor(model => model.DataAlteracao, new { type = "datetime", @class = "form-control", readOnly = true, id = "txtDataAlteracao" })
                @Html.ValidationMessageFor(model => model.DataAlteracao, "", new { @class = "text-danger" })

                <input type="submit" id="sumitForm" value="Save" class="btn btn-default" />

}
@section Scripts{

}

Comment: Não coloque seu javascript no modal e sim na view original (que abre o modal) e funcionará.
Comment: Então, aí que está, mas se eu coloca e meu JavaScript na view original eu não consigo pegar as tão da view que está no modal!
Comment: o que seria as "tão" da view?
Answer: Pelo que analisei do seu código, caso o modal seja carregado pelo ajax, inclua o código js no success do ajax, ele será seu .ready
$("#btnNovo").click(function () {
  $(".modal-body").load("/Pais/Create", function () {
    $("#exampleModal").modal("show");
    alert("alerta");
  });
});


Counting such functions

Question: How many total functions are there from a set with $3$ elements to a set with $4$ elements?
Just as a reminder, "total" functions are simply functions defined on all the elements of given domain. I calculated it as $4^3$ from multiplication principle, but unsure on that solution. Moreover, I am also curious on if we replaced "total" function with "one-to-one" and "onto", would the same answer be correct in both cases?
Would be welcomed to know your thoughts on this! Thanks!
Comment: Can anyone, please, check my answer and whether it is true for other functions, as well?
Answer: You are correct, by the definition of a function you must use all $3$ elements and there are four choices for each resulting in $4^{3}$ distinct possibilities. For one-to-one we must use all $3$ elements in the domain but the number of possibilities decreases for each element used (since one-to-one means that $ x \neq y \implies f(x) \neq f(y)$ so that for the first element there are $4$ possibilities, for the second, $3$ possibilities, and for the third, $2$ possibilities giving us $4 \cdot 3\cdot 2 = 24
$ possibilities. There are no surjective functions since if we map each element to a single element of the codomain (as is required by a function), there is always some element that is left over in the codomain since $4>3$.

Counting such functions


While transposing a note from one key to another, how to know the correct octave

Question: Transposition is simple: C in C major reads as G in G major, but if that very C is C4, what G (G4 or G5) is the corresponding G? What is the rule to figure out the octave?
Answer: This is impossible to answer, up-transpose is as legitimate as down-transpose. It completely depends on the purpose of the transposition. For a singer the adjustment may be necessary to match his or her vocal range. It then depends, whether the high notes were the problematic ones or the low ones.
The only simple cases are adjusting a given score for a transposing instrument: then the diffrerence between the sounding note and the written one is clearly defined.
Comment: As @guidot implies, the decision is based more on /why/ you're transposing than /what/ you're transposing. Even in your example, if your MIDI piece is designed to be played on a piano soundfont and the melody is already in the bass register, down-transposing will make the melody much muddier than up-transposing. And so the decision would be based on how much you value a clear melody versus a deep, rich bass tone. As it is, there are barely any hard-and-fast rules in writing, playing, or recording music. It's more than a little optimistic to hope to find such a rule for modifying music.
Comment: It also depends on the instrument tone you get after transposing. You may even consider transposing different parts of the piece up and down to get better sounding tones in every part of the piece.
Comment: Well, you've (somehow) answered it: "depends on the purpose".But, let's imagine that we want to know how a given MIDI piece in, say, C major sounds in, say, G major. It would make sense to down-transpose here, as to minimize the overall distances between notes (as `c` is closer to the `g` one octave below). Such a general scenario is where I'm framing the question.


I want to change default product variation machine name?

Question: I am working in Drupal Commerce kickstart. It's have default content types like hats,bags_cases,tops and all. I want to change the machine name also.Is it possible?

I am going to change structure->content types -> edit

But I can't change it...
Answer: Yes its possible.
if you want to edit the machine name of your content type name 'pages' then goto 'admin/structure/types/manage/page' --> click the "edit" button and update with the required value.
Same way you can do it for any content types.

Thanks
Comment: Its possible in Drupal 7. But i am using Drupal commerce kickstart.
Comment: product variation types are not shown in the content type structure menu for commerce kickstart profiles
Answer: Sadly the poster above didn't read your question properly and I wasted quite a bit of time thinking it could be done only I was looking in the wrong place or something.
Anyway, as far as I can see there is no drupal way to change the name of the machine name for a product variation. What I did to get around this was to 'hack' the database directly.
As always, backup you db first!
First of all I changed the variation type title to what it should be, in my case I was changing 'Drinks' to 'Charms'.
I did spend a lot of time trying to figure out which tables I needed to update but in the end the most efficient and error free way was to dump the whole database then open the .sql file into a text editor like Sublime text (I tried Coda but it was too sloooow as the file was 28mb) and do a search and replace, searching for 'drinks' and replacing it with 'charms' in my case.
Once done and saved to a new file I just re-imported it. Job done, so far no side effects. I'll post here if I find any. :-)


How to enable hotlink protection without hardcoding my domain in the Apache config file?

Question: Been surfing around for a solution for a couple days now.
How do I enable Apache hotlink protection without hardcoding my domain in the config file so I can port the code to my other domains without having to update the config file every time?
This is what I have so far:
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?example\.(com|net|org) [NC]
RewriteRule \.(gif|ico|jpe|jpeg|jpg|png)$ - [NC,F,L]

... And this is what Apache suggests:
SetEnvIf Referer example\.com localreferer
<FilesMatch \.(jpg|png|gif)$>
    Order deny,allow
    Deny from all
    Allow from env=localreferer
</FilesMatch>

... both of which hardcode the domain in their rules.
The closest I came to finding any info that covers this is right here on ServerFault, but the conclusion was that it cannot be done. Based on my research, that appears to be true, but I didn't find any questions or commentary dedicated soley to this question.
If anyone's curious, here is the link to the Apache 2 docs that cover this topic.
Note that Apache environment variables (e.g. %{HTTP_REFERER}) cannot be used in the RewriteCond's CondPattern argument.
Answer: What about checking to see if Host matches the Referrer? E.g.
RewriteCond %{HTTP_REFERER} !%{HTTP_HOST}

Edit
This won't work, as stated by the OP. For Apache 2.4 you can use:
RewriteCond expr "! %{HTTP_REFERER} -strmatch '*://%{HTTP_HOST}/*'"

For 2.2, you could write a small handler in mod_perl or mod_python that could perform the compare and issue the 403 error.
To achieve this with mod_python:
Create a file in your docroot (in my case /var/www/) called hotlink.py
from mod_python import apache

def headerparserhandler(req):
    if req.headers_in.get("Host") != req.headers_in.get("Referer"):
            return apache.HTTP_FORBIDDEN
    return apache.OK

In you Apache config:
 <Directory /var/www/>
   ...
   AddHandler mod_python .jpg
   AddHandler mod_python .gif
   PythonHeaderParserHandler /var/www/hotlink.py
   #PythonDebug On
 </Directory>

Now all requests for .jpg and .gif will be first checked by hotlink.py. With mod_python you can also check against the req.server.server_hostname attribute to check against ServerName instead of the incoming Host header.
Comment: Seems like it should work, right? Unfortunately, as the last sentence of my question points out, you can't use environment variables in the `RewriteCond`'s *CondPattern*.
Comment: @Jeff Doh, I should have checked. I've just seen in Apache 2.4 redirect docs suggest something. I'll update my answer and think about 2.2.
Comment: It seems to me that unless there is a way to compare `%{HTTP_REFERER}` with `%{HTTP_HOST}% on the `RewriteCond`'s *TestString* and then compare it to "something" (maybe even nothing!) on the same *CondPattern*, this simply can't be done with Ap2.2.
Comment: @Jeff I should have read your post more carefully as you clearly stated that RewriteCond can't compare HTTP headers. The only way I can think to achieve this is by writing a handler in mod_python or mod_perl.
Comment: Truly appreciate your effort. +1 and checkmark.


If $A,B,C$ are exhaustive events , Then $P(C\cap (A\cup B)')$ is

Question:
If $A,B,C$ are exhaustive events satisfying $$ P((A\cup B)\cap \bar{C}) = \frac{1}{5}, P(B \cap C)-P(A\cap B \cap C) = \frac{1}{15}$$
and $\displaystyle P(A\cap C) = \frac{1}{10}$. Then $\displaystyle P(C\cap (A\cup B)')$ is

$\bf{Attempt}:$ Using $$\displaystyle P(C\cap (A\cup B)') = P(C\cap (\bar{A}\cap \bar{B})) = P(C)+P(\bar{A}\cap \bar{B})-P(C\cup (\bar{A}\cap \bar{B}) )$$
Could some help me how to solve it, Thanks
Comment: Is some data missing? I don't see any way to compute $P(A \cap B)-P(A \cap B \cap C)$.
Comment: @Durgesh  Well, change $A\cap B$ into $A\cap C$ and things are settled.
Comment: Are you sure that you made no mistakes? It would be nice if we had $P(A\cap C)=\frac1{10}$ as you could say in the deleted answer. I am afraid that like this the number of solutions is uncountable.
Comment: @drhab Sorry. I did the mistake of taking $A\cap C$ in place of $A \cap B$. Yes, It would be nice if $P(A\cap C) = \frac{1}{10}$.
Comment: @expiTTp1z0 Republish your solution!
Comment: @drhab Done. You are right. The number of solutions will be infinite. Given the condition of exhaustive events, I think the OP might have made a mistake in writing the question.
Answer: Note that this is not the correct solution to the problem. This is the solution to the modified problem where $P(A \cap C) = \frac{1}{10}$ is given instead of $P(A \cap B) = \frac{1}{10}$.
Use venn diagram and use the fact that $P(A\cup B \cup C) = P(S) = 1$ by assumption of exhaustive events. Required probability is $1-1/5-1/15-1/10$.
$P((A\cup B)\cap \bar{C}) = P(Orange) = \frac{1}{5}$
$P(B \cap C)-P(A\cap B \cap C) = P(\bar{A}\cap B \cap C) = P(Green) = \frac{1}{15}$
$P(A\cap C) = P(Red) = \frac{1}{10}$
To compute, $P(C\cap (A\cup B)') = P(Blue)$.
Comment: How did you figure out what $P(A \cap B)-P(A \cap B \cap C)$ was?


How to change DateTime field value on form submit

Question: I am trying to update a DateTime field in an altered form submit. Something like:
<code>function my_module_form_submit($form, FormStateInterface $form_state) {
  if (!$profile->field_someother_field->isEmpty()) {
    $date = new DrupalDateTime();
    $date->setTimezone(new \DateTimezone(DATETIME_STORAGE_TIMEZONE));
    $string = $date->format(DATETIME_DATETIME_STORAGE_FORMAT);
    $form_state->setValue('field_date_updated_details', [$string]);
  }
}
</code>
But this fails with various messages (depending on whether I set the value as an array or directly as string).
Any advice?
Answer: The form value need to be an call DrupalDateTime, so you can directly pass $date as value.
Comment: I tried that as well and it still throws an exception :/
Answer: I've been working on a similar problem, and the following kind of thing works for me:
<code>$form_state->setValue('field_date_updated_details', [['value' => $date]]);
</code>
As another respondent noted, I'm not using a string, but the actual DrupalDateTime object, and setting the 'value' inside that nested array.


Re-authenticate a user with X-CSRF-Token

Question: I am writing an Android application that consumes the Drupal 8 REST features. I have managed to write REST resources and make API calls with the android app to these resources with success.
My question is about the login and sessions. When the users logs in for the first time we get X-CSRF-Token, which has an expiration date. I also use this token to authenticate myself when making calls to the REST resources.
Does Drupal has a REST feature that refreshes the token without having to enter the user credentials again?
The idea is to have an app on which you log in only once until the users logs out manually.
Answer: Rasjaad, You can use android account manager https://developer.android.com/reference/android/accounts/AccountManager for this purpose
Here is a sample Drupal 8 Android app code that uses account manager to reauthenticate. https://github.com/onedrupal/One-Drupal-Android
https://github.com/onedrupal/One-Drupal-Android/blob/master/app/src/main/java/com/technikh/onedrupal/activities/ActivityAuthentication.java
At the launch of the activity AccountManager.getAuthTokenByFeatures is called
mAccountManager.getAuthTokenByFeatures(AccountUtils.ACCOUNT_TYPE, AccountUtils.AUTH_TOKEN_TYPE, null, this, null, null, new ActivityAuthentication.GetAuthTokenCallback(), null);

Here is the AccountAuthenticator  https://github.com/onedrupal/One-Drupal-Android/blob/master/app/src/main/java/com/technikh/onedrupal/authenticator/AccountAuthenticator.java
authToken = AccountUtils.mServerAuthenticator.signIn(site_protocol+site_domain, account.name, password);


Restarting an XMLHttpRequest on receiving a 404 response

Question: I have the following method that iterates over an array containing objects that have a URL property (floodAreaData[0].polygon) which I fetch over an XMLHttpRequest, the results of which are passed to loadFloodAreaPolygon function.
This is working as expected, my issue I've run into is that one of the URLs is returning a 404, and causes the following error to be displayed in the console window:
Access to XMLHttpRequest at 'https://environment.data.gov.uk/flood-monitoring/id/floodAreas/051WACDV5B3a/polygon' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I've spent more time than I'd like trying to implement a CORS policy without any luck and increasing frustration.
My question is can I just reset the XMLRequest to continue processing from the next available URL? I tried adding a try catch block but for some reason the message within is not getting printed to the console when I test..
function getFloodAreaPolygon() {
  try {
    if (floodAreaData.length > 0) {
      let request = new XMLHttpRequest();
      let url = floodAreaData[0].polygon;
      request.open("GET", url, true);
      request.onload = function () {
        let data = JSON.parse(this.response);
        if (request.status >= 200 && request.status < 400) {
          data.features.forEach(geojsonFeature => {
            loadFloodAreaPolygon(floodAreaData[0], geojsonFeature);
            floodAreaData.shift();
            getFloodAreaPolygon();
          });
        } else {
          console.log("error");
        }
      };
      request.send();
    }
  } catch {
    console.log("request error");
  }
}

Comment: Just two thoughts: 1) You can't possibly add CORS headers in British government sites unless you're, well, the government 2) try / catch is pretty much useless unless someone throws
Comment: Regarding CORS: if you could simply implement a CORS policy to solve this, why have CORS in the first place? No, that error essentially means that you cannot use that API from the JS client side of your website; it's supposed to be used from the backend (where CORS is a non-issue).
Comment: If the 404 response doesn't have the Access-Control-Allow-Origin response header, then browsers block your frontend JavaScript code from accessing any properties of the response — including `.status`. Or to put it in other terms, if a response has no Access-Control-Allow-Origin response header, there's no way for your frontend JavaScript code to programatically determine whether it was a 404 (or any other response status you might want to check for).
Answer: The load event only fires if the request is successful. If it isn't, then you get an error event.
If there is a network or CORS error then the status of the XHR object will be 0. You can't distinguish between types of status 0 error from JS (the developer console will display more information, but your code running on someone else's computer can't).
So write an error handler, test to see if the status is 0, and if it is: Call whatever function will make your code request "the next available URL" (it isn't clear how your code does that in the first place, but I guess it would involve calling getFloodAreaPolygon and passing an argument to make it use a different index on the line let url = floodAreaData[0].polygon;).
const xhr = new XMLHttpRequest();
xhr.open("GET", "https://example.com/404");
xhr.addEventListener("error", event => {
  console.log({event, xhr});
  if (xhr.status === 0) {
    console.log("Network or CORS error");
    // continue processing from the next available URL
  }
});
xhr.send();


Migrating a Drupal Node.js website from localhost to web server

Question: Due to specific website feature requirements, which do not seem to be possible in Drupal 7 without Node.js integration, I am strongly considering to integrate Drupal 7 with Node.js modules offering real time interactivity. I understand the basic concepts however I have never actually used Node.js on a website before. I have installed Node.js on my computer under Program Files (Windows) and begun testing the integration with a test website on localhost (WAMP).
My concern is for when I later will need to migrate a client's website to an actual web server. I would like to ask those who have had some experience with such Drupal/Node.js integration, what is a streamlined workflow for migration. For example, I am wondering if Node.js should be installed into the same folder as the website directory instead of Program Files, so that when testing is finished I will be able to ftp the entire website directory with the Node.js installation without needing to reinstall Node.js and reconfigure everything. I am not sure if copying everything over would work or if Node.js needs to be installed again separately on the web server?
I would like to get a clearer understanding of this process before I make the leap into Node.js integration with future client websites. I believe Node.js integration introduces some great new features however it could potentially be a giant can of worms if not handled properly.
Comment: I can't see a Drupal-related question here (just asking about installing/migrating a node.js application). If there's a Drupal-related aspect, other than the fact that you happen to have a Drupal site _connecting_ to your node.js server, please update the question with those details and flag for re-opening
Comment: The question is concerned specifically with migrating a Drupal website with Node.js integration; therefore outlining a workflow to have both systems working side by side. Posting this on Stack Overflow is likely to be flagged as excessively "Drupal specific".
Comment: As far as the Drupal site is concerned you need do nothing but copy the files across. Setting up the node.js server isn't something we'd cover here though. There isn't such a concept as a Drupal Node server; Drupal is Drupal, Node is Node. Once you've got a Node server set up, and the application installed and configured, Drupal will be able to connect to it
Answer: The typical NodeJS setup usually involves a Linux server and a nginx web server/proxy that delegates the requests to the application running on a specific/semi-random unprivileged port. In other words, nginx will have a site declaration (vhost) for your Drupal and a another site declaration for your NodeJS application. Your NodeJS application runs in the backgrounds and listens for requests from nginx.
NodeJS itself is usually installed through the Linux distribution's packaging system, similarly to PHP.
On the filesystem, I would treat the two applications as separate projects, in two separate directories on the server. It's pretty rare to find developers who have a good understanding both Drupal/PHP and NodeJS, so keeping it separate will make it easier for other people eventually working on that project to familiarize themselves with the environment.
Comment: Thanks for that information. I don't use Linux at all so I am planning to test this on Windows, I assume this is possible since there is a Node.js installer for Windows.

For the migration, when I am choosing a host how can I determine whether their server is able to run Node.js alongside my Drupal website? Are there specific requirements that will be needed for this to work? I am not that familiar with servers, so will I need to simply copy/ftp the Node.js files or do some sort of manual install of Node.js on the server?
Comment: There are many ways to configure NodeJS, but most typically it's on a VPS. However, I have to agree with Clive. The basic answer is that yes, those are two separate apps, and should be managed separately, as any other two web app/services that talk to each other. Beyond that, it's offtopic for this forum. There are lots of good resources on Stack Exchange to learn more about NodeJS. Good luck!


Creating a List<> Member with Spring Roo

Question: The Spring Roo command field set creates a Set<> member.

field set --fieldName --type

How does one go about creating a List<> instead?
Comment: I'm no longer prototyping with this technology and cant tell when I mark an answer as correct. Someone else will have to let me know.
Answer: From the latest relese(1.2.4) onwards roo supports lists.
field list --fieldname -- type

Answer: What is your persistence implementation?  Are you using JPA or JDO?  Hibernate (JPA provider) will technically "support" lists but makes no guarantee as to the order returned (maintaining order in a database doesn't come for free) and so you may as well use a Set.
It's possible that Roo doesn't bother with lists under that reasoning.
Answer: I just change it in the java code from set to list. If the roo console is open, It updates automatically .aj files. It just works.


Distribution of a random convergent sequence of nested intervals

Question: Starting from the interval $[0,1]$, generate two uniform random numbers $x_1,y_1$ and sort them so $x_1<y_1$. This yields an interval $[x_1,y_1]$. Generate two numbers uniformly from this interval, sort them as $x_2<y_2$, and repeat. This produces a nested sequence of intervals whose lengths decrease geometrically (on average), and converges to some $x^*$ almost surely. What is the distribution of $x^*$?
So far, I have the following: Let $f(a)=P(x^*\le a)$. If the two random numbers $x_1,y_1$ are both smaller than $a$ then $x^*\le a$ for sure. If $x_1<c<y_1$, then we have the same problem again with a new value for $a$, namely $\frac{a-x_1}{y_1-x_1}$. Similarly if $y_1<c<x_1$. Thus we obtain the following integral equation for $f$:
$$f(a)=a^2+2\int_0^a\int_a^1f\left(\frac{a-x}{y-x}\right)\,dy\,dx$$
At this point I'm stuck. Is this equation elementarily solvable?
Comment: I think it might be more useful to determine the distribution of $(x_1, y_1)$ in $[0,1]^2$, then use this information and the similarity you've noted between the distributions of $x^*$ on $[0,1]$ and it distribution on $[x_1, y_1]$ to deduce $f$.
Comment: @PaulSinclair I'm afraid I don't see how that is any different from what I did. The distribution of $(x_1,y_1)$ in $[0,1]^2$ after sorting is just uniform on the triangle $0\le x\le y\le 1$, and the similarity is between the distribution of $x^*$ on $[0,1]$ and the *marginal* distribution of $x^*$ on $[x_1,y_1]$ once a choice of $x_1$ and $y_1$ is made. Unfortunately this doesn't directly translate to a self-similarity of the function so much as a similarity between $f$ and an integral over $f$, i.e. the exact integral equation I wrote down.
Answer: We can reparameterize the integral with $x=a-u$, $y=a+v$, and $t=\frac u{u+v}$ to eliminate one of the integrals, and use $f(1-a)=1-f(a)$ to make the integral more symmetric:
\begin{align*}
f(a)&=a^2+2\int_0^a\int_0^{1-a}f\left(\frac{u}{u+v}\right)\,dv\,du\\
&=a^2+2\int_0^a\int_{u/(1-a+u)}^1\frac{u}{t^2}f(t)\,dt\,du\\
&=a^2+\int_0^1\frac{f(t)}{t^2}\int_0^{\min((1-a)t/(1-t),a)}2u\,du\,dt\\
&=a^2+\int_0^1\frac{f(t)}{t^2}\min\left(\frac{(1-a)t}{1-t},a\right)^2\,dt\\
&=a^2+\int_a^1f(t)\frac{a^2}{t^2}\,dt+\int_0^af(t)\frac{(1-a)^2}{(1-t)^2}\,dt\\
\end{align*}
Let $g(a)=\int_a^1f(t)\frac{a^2}{t^2}\,dt$, $h(a)=\int_0^af(t)\frac{(1-a)^2}{(1-t)^2}\,dt$, and $K=g(1)=\int_0^1f(t)/t^2\;dt$. Then we have the ODE:
\begin{align*}
\tag{1}f(a)&=a^2+g(a)+h(a)\\
\tag{2}g'(a)&=-f(a)+\frac2ag(a)\\
\tag{3}h'(a)&=f(a)-\frac2{1-a}h(a)\\
\tag{4}f(1/2)&=1/2\\
\end{align*}
Because $0$ is a singular point, it doesn't make a good initial condition, but it is possible to solve this ODE with an arbitrary choice of $h(1/2)$, and determine the value of $h(1/2)$ such that $f(0)=0$. Doing this numerically, I find $h(1/2)=1/16$ (and hence $g(1/2)=3/16$) to high accuracy. Here is a picture of $f(a)$ (top), $g(a)$ (middle) and $h(a)$ (bottom) for $a\in [0,1]$.

If you think it looks a lot like a cubic, you're right! It's indistinguishable from the unique cubic that passes horizontally through $(0,0)$ and $(1,1)$, namely $f(a)=3a^2-2a^3$. And lo and behold, if you plug this into the original integral equation, it is a solution. Go figure.