HTML Imagem base64 em emails

Question: Eu tenho uma API em python que envia emails. O problema é que eu monto um HTML e coloco uma tag  e eu coloco uma varaivel de base64 que é o src. Porém 90% dos serviços de e-mail não aceitam base64. Existe alguma outra saida ?
Segue o Código:
def show_logotipo():
    from PIL import Image
    import StringIO
    import base64
    logotipo = db(Parametros_Gerais.id == 1).select().first().logotipo
    pic = Image.open(os.path.join(request.folder,'uploads',logotipo))
    buf= StringIO.StringIO()
    pic.save(buf, format='PNG')
    buf.seek(0)
    buf_s = buf.read()
    logo_final = base64.b64encode(buf_s)
    return logo_final

logo_final = show_logotipo()
            mail_body = ""\
                            ""\
                                ""\
                            ""\
                            ""\
                                "
Você foi convidado a uma nova sala de conferencia
"\
                                "
Número da conferencia: "+str(busca_conf[0]['confno'])+"
"\
                                "
Senha de acesso: "+str(user_pw)+"
"\
                                "
Horario: "+str(busca_conf[0]['starttime'].strftime('%Y-%m-%d %H:%M'))+"
"\
                                "
Qualquer dúvida sobre essa conferencia, envie um e-mail para: "+email_admin+"
"\
                                "
--
"\
                                ""\
                            ""\
                        ""
            print mail_body
mail.send(
                to=item_form['email'],
                subject='Convite para uma nova sala de Conferencia',
                message=mail_body,
                    attachments=mail.Attachment('/var/www/audio_conf.ics')
)

Comment: Acho que não vai resolver o seu problema, mas as vezes te da uma luz... https://pt.stackoverflow.com/questions/286570/imagem-em-e-mail-html?noredirect=1#comment583592_286570
Answer: Manda um email multipart, como a seguir (python3):
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

def enviaEmail(strFrom, strTo, assunto, msgText=None, msgHTML=None, img1=None, img2=None):
    msgRoot = MIMEMultipart('related')
    msgRoot['Subject'] = assunto
    msgRoot['From'] = strFrom
    msgRoot['To'] = strTo
    msgRoot.preamble = 'This is a multi-part message in MIME format.'

    msgAlternative = MIMEMultipart('alternative')
    msgRoot.attach(msgAlternative)

    if msgText:
       msgText = MIMEText(msgText)
       msgAlternative.attach(msgText)

    if msgHTML:
        #  Imagem no html:
        msgText = MIMEText(msgHTML, 'html')
        msgAlternative.attach(msgText)

    if img1:
        fp = open(img1, 'rb')
        msgImage = MIMEImage(fp.read())
        fp.close()
        msgImage.add_header('Content-ID', '<imagem1>')
        msgRoot.attach(msgImage)

    # Outra imagem....
    if img2:
        fp = open(img2, 'rb')
        msgImage = MIMEImage(fp.read())
        fp.close()
        msgImage.add_header('Content-ID', '<imagem2>')
        msgRoot.attach(msgImage)

    smtp = smtplib.SMTP()
    smtp.connect('correio.smtp.com.br')
    smtp.sendmail(strFrom, strTo, msgRoot.as_string())
    smtp.quit()

msgHTML="""
  <!DOCTYPE html>

    <meta charset="UTF-8">
       <title>TESTE</title>

Imagem 1

Imagem 2

"""
enviaEmail("firstname.lastname@example.com",
           "email@example.com",
           "Assunto ",
           msgHTML=msg,
           img1="imagem/caminho1.png"
           img2="imagem/caminho_imagem2.png")

Comment: Vlw, me ajudou muito!

HTML Imagem base64 em emails


Revolution Slider stuck at loading

Question: I need your help. I am running a site (D7.43, jQuery 1.10) with the Jollyany theme under the hood. The theme comes packaged with the revolution Slider from ThemePunch. I am trying to include a slider into a page, but am then confronted with the slider being empty and the loading spinner spinning away merrily forever. Navigation bullets and left right arrows are visible but do nothing.
You can see the page here: wixschool.net/test-class.
This page shows the revolution slider in place. The slider shows the last of three images, constant spinner, no transition to next slide. Navigation also doesn't go anywhere, slider completely stuck. Slider scripts are called in the head, html for the slider is put in place and seems OK.
(In fact the sldier does not even show any picture, but I've removed the 'visiblity hidden" from the CSS of the background image so its easier to see the slider.. and it then shows the last of three images, not the first..)
There were suggestions out there that it may be (double call to) script issue, so I have tried switching off all other scripts that are called in the head  and below the body to see if that would make a difference, but besides losing functionaltiy that those scripst supply the slider stays stuck..
Any suggestions as to why the slider doesn't work is extremely welcome!
Edit: I just noticed that on the test-class page the TB MegaMenu dropdown in the menu don't work (any other page they do..). Tried removing the entire menu as well as the TB Menu scripts but still no working slider..
Comment: I see [two errors](http://i.stack.imgur.com/7LnXU.jpg)
Comment: Thanks! yes I saw those too, don't know how to correct those scripts though.. I did try just removing them altogether (no idea what countdown or hammer .js do), made no difference to the site but slider still not working..
Comment: If a site throws JavaScript errors all scripts block on that page. So the errors prevent the slider (and the menu) from working. You can try to install the theme on a vanilla site (no other modules), have a look at http://simplytest.me if it doesn't work either, check the theme (or module?) issue queue for a solution or report the bug there.
If it does work on a vanilla website it might be an incompatibility with another module and then there is little we can do.
Comment: In `hammer.js` It appears to me that line 168 needs to be `addEventListeners` with an **s** and line 180 needs to be `removeEventListeners` also with an **s** at the end.
Comment: I've tried adding the two s's, now still get an error about line 168, saying that `target.addEventListeners` is not defined? The hammer.js script is called by the menu (the mobile menu) and the revolution slider both (the hammer.js is included in the jQuery.themepunch.plugins.min.js)
Comment: Have been playing around, it is clearly the hammer.js issue, during one fiddle I did get the revolution slider to work, but then errors came back. I tried renaming all  `EventListener` items in hammer to `EventListeners` and also the reverse, but whatever the name get the warning the function `EventListener(s)` doesn't exist. Also tried removing the hammer bit from the slider plugin js, but that didn't help either. I'll report back if I find out more! Thanks for the help and pointers.
Comment: Interesting note: the hammer.js error does not appear on any other pages even thought that script is called everywhere; so I assume it must be that the `EventListener` handle is used twice (once for the menu and once for the rev slider?) and thus clashes?
Comment: Thanks for all your pointers. Disable the hammer.js script from the mmenu module and bingo, Revolution slider works like a charm :) no-sssweat, if you put that in a proper answer I can give you a vote up?
Answer: Reason for Revolution Slider failure found: the Rev Slider uses the hammer.js jQuery plugin. Elsewhere on my site I have installed the jQuery mMenu, which also has hammer.js among its scripts. I disabled hammer.js in mmenu, and the Rev Slider comes alive immediately. The clash centres around the event listeners functions in the hammer script.
Haven't solved all though, mmenu still works fine but I do get a 'where is hammer.js' error thrown at me now of course. Will report when and how I solve that..

Revolution Slider stuck at loading


Is it possible to build a GUI using platform-native code (i.e. Cocoa) in QT?

Question: Is it possible to build a GUI using platform-native code, like Cocoa, in QT?
Do you know of any resources or links that show examples or demonstrations of how to get started doing this?
I can only find sites that explain how QT uses interfaces to link to the Cocoa API, which seems to imply that the UI is still built with QT and it doesn't support platform-specific technology like Core Animation, etc.
Thanks
Turgs
Answer: Yes, that's possible, you can combine Objective C code using cocoa with C++/Qt code. There are not too many people doing it though, so it will be a bit of an experiment.
Have a look at these Qt wrappers for Cocoa widgets to see how it can work.
Answer: I never tried to do it but I think it should be possible. I suggest you try to read this: http://el-tramo.be/blog/mixing-cocoa-and-qt/.
Answer: Yes, you can set your QStyle, such as QMacStyle. Check out more here:
http://doc.qt.nokia.com/4.7/style-reference.html
Comment: This does not answer the question. The OP is aware of how to build a Qt Mac-style UI, but he wants to know if it's possible to make the UI using native Cocoa with the back-end code in Qt.
Comment: Yes Rob that's right. I want to know if it's possible to make the UI using native Cocoa with the back-end code in Qt.


How to calculate the feature importance for multi-label classification problems

Question: I am looking for some sources about "how to calculate the feature importance for multi-label classification problems". would you give me some information with related python source code on how to apply feature importance in multi-label datasets?
Comment: Welcome to Cross Validated! What's wrong with the methodology you would use on a binary problem?
Answer: Welcome to Cross Validated!
It depends on your model, but broadly speaking, I would heavily recommend some version of Permutation Feature Importance to figure out which features are helpful. Read more here: https://scikit-learn.org/stable/modules/permutation_importance.html
This technique works with basically any algorithm and any target type (binary, multi-class, regression etc.)
There are various packages that implement it, like sklearn in Python and Boruta in R.
Here's the intuition for how Permutation Feature Importance works:
Broad idea is that the more important a feature is, the more your performance should suffer without the help of that feature. However, instead of removing features to see how much worse the model gets, we are shuffling/randomizing features. The idea there is that by shuffling it, you still have the same number of variables (since you didn't remove one), and the shuffled variable has the same distribution as the original, but it's randomly ordered now (so any real connection between that variable and your target should be destroyed).
Procedure:

Train single model (Model 1) on all features, obtain its performance on some validation set.
Shuffle one of the features (this means just randomize the order of values in that variable vector). So for example, if you have 1000 rows of data, you take the 1000 values of variable A and randomize their order.
Run Model 1 (do not re-train it. Run the Model 1 as you trained it on all the original features, but feed in a dataset with Variable A randomized). Observe difference in performance relative to when you had variable A non-randomized.
Repeat for all variables.

This way, you can see what variables are important and which are not. I hope it makes sense that broadly speaking, if you totally jumbled up the values of a variable and performance wasn't impacted, that variable probably wasn't very important to your model.
Please carefully read the links, as there are some considerations. For example, if there's a lot of multi-collinearity between features, this might have problems. Additionally, this is a measure of how important the features are to one specific model. If your model is terrible, then this feature importance might not be a good representation.
Quick tip for Permutation Feature Importance: In order to have a faster and more logical way of running this, try clustered Permutation Feature Importance (this also solves problems related to have multicollinearity among your features)(https://scikit-learn.org/stable/auto_examples/inspection/plot_permutation_importance_multicollinear.html#sphx-glr-auto-examples-inspection-plot-permutation-importance-multicollinear-py) . Essentially, group your features into several groups (by which variables are most similar/correlated), and then run permutation feat. imp. on each of the entire groups, not on individual variables.
I hope this gives you some good directions to explore!


Why is $\left|\int_0^{R_1} \frac{e^{-xR_2}\sin x}{x}dx \right| \leqslant \int_0^{R_1} e^{-xR_2}dx$

Question: Let $R_1 \in \mathbb{R^+}$ and $R_2 \in \mathbb{R^+\cup\{0\}}$
Since $|\sin x/x|<1$ for $x>0$,
why,
$$\displaystyle\left|\int_0^{R_1} \frac{e^{-xR_2}\sin x}{x}dx \right| \leqslant \int_0^{R_1} e^{-xR_2}dx$$
I think, $$\displaystyle\left|\int_0^{R_1} \frac{e^{-xR_2}\sin x}{x}dx \right| < \int_0^{R_1} e^{-xR_2}dx$$
Someone could help me. Thank you.
Comment: If $A<B$, then $A\leq B$. What is the problem? There is no contradiction here.
Answer: The statement:
$$f(r)\leq g(r),\text{ for all $r\in X$}$$
does not mean that they necessarily are equal for some $r$. It is just the statement that for all $r\in X$ either $f(r)<g(r)$ or $f(r)=g(r)$.
So there is no contradiction in your question.
Quite often in an inequality, if we only need $\leq$ for a later argument, we stick with $\leq$ at every step, even when we can show $<$.


If $C_0, C_1, C_2, .., C_n$ are the binomial coefficients in the expansion of $(1+x)^n$

Question: If $C_0, C_1, C_2,...,C_n$ are the binomial coefficients in the expansion of $(1+x)^n$, prove that:
$$C_{r}.C_{n} + C_{r+1}.C_{n-1} +......+ C_{n}.C_{r} = C(2n, n+r) =\dfrac {(2n)!}{(n-r)! (n+r)!}$$
Is there any way to approach this sort of questions using calculus (derivatives or integration)?
Comment: Your equation to prove is a special case of [Vandermonde's identity](https://en.wikipedia.org/wiki/Vandermonde%27s_identity), for your reference.
Comment: @MinusOne-Twelfth, How does that help?
Comment: It gives you a term to help you search with. Like you can try searching for "Vandermonde identity calculus proof" or whatever. It would be harder to search for things like this if you didn't know the name of the identity.
Answer: THis suffices to differentiate $(1+x)^{2n}=(1+x)^n(1+x)^n $ $n+r$ times.
Differentiating the right side first,
By (General Leibniz rule), it's derivative is given by differentiating the first part $k$ times and the second part $(n+r)-k$ times.
It's derivative is given by $\sum_{k=0}^{n+r} {{n}\choose{k}}((1+x)^n)^{(k)}((1+x)^n)^{(n+r-k)}$
$=\sum_{k=0}^{n-r} {{n}\choose{k+r}} ((1+x)^n)^{(r+k)}((1+x)^n)^{(n-k)}$
Evaluating at $x=0$,
$=\sum_{k=0}^{n-r} \dfrac{(n+r)!}{(k+r)!(n-k)!} \dfrac{n!}{(n-r-k)!}\dfrac{n!}{k!}$
$=(n+r)! \sum_{k=0}^{n-r} \dfrac{n!}{(k+r)!(n-r-k)!} \dfrac{n!}{(n-k)!k!}$
$=(n+r)! \sum_{k=0}^{n-r} {{n}\choose{k+r}}{{n}\choose{n-k}}$
Differentiating left side gives $(n+r)!{{2n}\choose{n+r}}$
Giving the desired equality.
Comment: @L KM,How do we know to differentiate $n+r$ times?
Comment: I actually work using generating function implicitly. Which does not require differentiation. Hence we can compare each coefficient easily with that.
Comment: The proof is as follows, $(1+x)^{2n}=(1+x)^n(1+x)^n$. Look at the coefficient of $x^{n+r}$ on each side. On the left side, it is ${2n}\choose{n+r}$. On the right hand side, it is exactly the sum you stated, $\sum_{k=0}^{n-r} {{n}\choose{k+r}}{{n}\choose{n-k}}$
Comment: @L KM, Would you please take time to mention the general strategy to solve these sorts of problems using calculus?
Comment: To be more precise, the attain $x^{n+r}$ on the right hand side $(1+x)^n(1+x)^n=(\sum_k {{n}\choose{k}} x^k)\cdot (\sum_k {{n}\choose{k}} x^k) $, we need $x^k$ from the left sum, $x^{n+r-k}$ from the right sum. Note that $n\ge k, n\ge (n+r-k)$, hence $n\ge k \ge r$.
Comment: General strategy is to work more on generating function. There are of course general algorithm using computer to solve these problem, but not so useful for non-IT guys.
Comment: @L KM, what simplification did you do just before the step $x=0$
Comment: $\sum_{k=0}^{n+r} {{n}\choose{k}}((1+x)^n)^{(k)}((1+x)^n)^{(n+r-k)}$, if $k \lt r$, then $(n+r-k) \gt n$,  $((1+x)^n)^{(n+r-k)}=0$. If $k\gt n$, $((1+x)^n)^{(k)}=0$, hence we need only to take range of $k$ from $ r$ to $n$.
Answer: We can solve this problem using algebra without needs for differentiation or integration. It is convenient to use the coefficient of operator $[x^k]$ to denote the coefficient of $x^k$ in a series. This way we can write for instance
\begin{align*}
\binom{n}{k}=[x^k](1+x)^n\tag{1}
\end{align*}

We start with the right-hand side and obtain
  \begin{align*}
\color{blue}{\frac{(2n)!}{(n-r)!(n+r)!}}&=\binom{2n}{n+r}\\
&=[x^{n+r}](1+x)^{2n}\tag{2}\\
&=[x^{n+r}]\sum_{k=0}^n\binom{n}{k}x^k\sum_{l=0}^n\binom{n}{l}x^l\tag{3}\\
&=\sum_{k=0}^n\binom{n}{k}[x^{n+r-k}]\sum_{l=0}^n\binom{n}{l}x^l\tag{4}\\
&=\sum_{k=0}^n\binom{n}{k}\binom{n}{n+r-k}\tag{5}\\
&\,\,\color{blue}{=\sum_{k=r}^n\binom{n}{k}\binom{n}{n+r-k}}\tag{6}
\end{align*}
and the claim follows.
Comment:

In (2) we write $\binom{2n}{n+r}$ using the coefficient of operator according to (1).
In (3) we write $(1+x)^{2n}=(1+x)^n(1+x)^n$ and expand.
In (4) we use the rule $[x^{p+q}]A(x)=[x^p]x^{-q}A(x)$.
In (5) we select the coefficient of $x^{n+r-k}$.
In (6) we note that $\binom{p}{q}=0$ if $q>p$ and set the lower index consequently to $k=r$.
Comment: How did you get $(1)$?
Comment: @pi-π: From the binomial expansion $(1+x)^n=\sum_{j=0}^n\binom{n}{j}x^j$.


Issue creating a list in the DOM

Question: My assignment is to create html inside the javascript with the DOM. I am having difficulties getting the list loaded and have tried multiple ways to get it going. Im including my attempts as the error so it can be easily spotted. The exact instructions of the assignment are;
a.Add an h1 element and give it the text "My Generated Document"
b.Add two p elements to the document after the h1 and put some latin text as the content.
c.Assign the first p element an id of "para1".
d.Create an ul element and add it after the  elements. Fill the list with 4 li that list your favorite things to do.
e.Assign all of the list items a class of "fav".
f.Insert a 5th li with another favorite thing as content just before the 4th li. Do not assign it a class attribute.
g.Give all the li tags with a class of "fav" a font-family: Arial, Helvetica, sans-serif and a font-size: 14px style
h.Give the p element with the id of "para1" a black solid border of a size of your choosing and a yellow background color.
<code><!DOCTYPE html>
<html>
<head>
<title>Generator</title>
<meta charset="utf-8">
</head>
<body id = "page">
<script type="text/javascript" src="js/generator.js">

window.onload = function () {
var body = document.getElementById("page");
var h1 = document.createElement("h1");
h1.innerHTML = "My Generated Document";
body.appendChild(h1);
var para1 = document.createElement("p");
para1.id = "para1";
para1.innerHTML = "Lorem ipsum jibba jabba";
body.appendChild(para1);
var p = document.createElement("p");
p.innerHTML = "Curabitur blah blah";
body.appendChild(p);
var ul = document.createElement("ul");
var li1 = document.createElement("li");
li1.className = "fav";
li1.innerHTML = "One";
ul.appendChild(li1);
var li2 = document.createElement("li");
li2.className = "fav";
li2.innerHTML = "Two";
ul.appendChild(li2);
var li3 = document.createElement("li");
li3.className = "fav";
li3.innerHTML = "Three";
ul.appendChild(li3);
var li4 = document.createElement("li");
li4.className = "fav";
li4.innerHTML = "Four";
ul.appendChild(li4);
var li5 = document.createElement("li");
li5.innerHTML = "Five";
ul.appendChild(li5);
ul.insertBefore(li4, ul[3]);
body.appendChild(ul);

}

</script>
</body>
</html>
</code>
Comment: Why don't you drop over to http://jsfiddle.net/ and create a working example, then link to it from your question. You will find people more helpful then.
Comment: Thanks for the tip, ill be sure to use it for future questions
Answer: Looks like you forgot to add ul to the body: body.appendChild(ul)
Comment: Thanks, ill be sure to add that.

Issue creating a list in the DOM


Blocking an IP range on Windows server 2008

Question: How to define a rule in Windows server 2008 firewall to be applied to an IP range. For example blocking IPs which start with 202 I mean all IPs which meet 202...*
There are two options when creating a new rule:

This IP address or subset:
This IP address range:

How should I define such a wide range ?
Comment: I'd first ask the question, "why are you blocking all those IPs in the first place?"  A blanket answer wouldn't fit here, nor would it be beneficial to you in the long term.
Comment: That is the range between 188.8.131.52 to 188.8.131.52. I assume that you mean IPv4.
Comment: Yes I mean IP v4. I was wondering if this wide range has a negative performance issue or not.
Answer: The range begins at 126.96.36.199 and ends at 18.104.22.168. You can also represent it as 126.96.36.199/8.
Why /8?
An IPv4 address is a 32-digit binary number that has been conveniently divided into 4 groups. The address 22.214.171.124 expressed in binary is
11001010 00000000 00000000 00000000

In this context /8 means "match the first 8 binary digits", or any address that begins with 202..
Read all about it at the Wikipedia article for Classless Inter-Domain Routing.
Comment: Why `22.214.171.124/8` ? What does 8 mean here ?
Comment: @Xaqron I updated my answer to address your question.


How can I deserialize an array in a background thread using Rust's serde?

Question: I've got a json file with an internal array (e.g. {"a":"b", "c": [...]}) that can be incredibly long; too long to fit into memory, and as such cannot be handled by the default Vec deserializer.
I saw an approach written in C# that did the following:

when deserializing, for each item, put the item on a ConcurrentQueue
consumers run in a background thread, which pull deserialized items off the queue for processing.

Now, I could simply implement my deserializer to do the processing, as is described here.  However, I'd like to try and implement an approach similar to that described above.  I've gotten far enough to have discovered channels, and that the Deserializer and SeqAccess traits don't implement Send and therefore cannot safely be moved across threads.
I've tried a few configurations, similar to what's described here, but isn't quite the approach I'd like to take.
Here's some sample code:
// generator.rs
// This lets me replace `Vec<T>` with `ChannelGenerator<T>`
// and treat it the same, except `next()` is pulling
// a T obj off the channel, rather than from the whole Vec in memory

pub struct ChannelGenerator<T> {
    pub(crate) receiver: Receiver<T>,
}

impl<'f, T> Iterator for ChannelGenerator<T> {
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        let res = self.receiver.recv();
        res.map_or(None, |i| Some(i))
    }
}

// struct.rs

#[derive(Deserialize)]
pub struct File {
    pub a: String,
    pub b: String,
    #[serde(deserialize_with = "deserialize_to_channel")]
    pub items: ChannelGenerator<Item>,
}

use std::{
    fmt,
    marker::PhantomData,
    sync::mpsc::{sync_channel, Receiver, SyncSender},
    thread,
};

use serde::{
    de::{SeqAccess, Visitor},
    Deserialize, Deserializer,
};

use crate::sync_array_serde::generator::ChannelGenerator;

pub struct ChannelVisitor<T> {
    pub sender: SyncSender<T>,
    pub f: PhantomData<fn() -> T>,
}

impl<'de, T> Visitor<'de> for ChannelVisitor<T>
where
    T: Deserialize<'de> + Send,
{
    type Value = ();

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("an array of objects of type T")
    }

    fn visit_seq<S>(self, mut seq: S) -> Result<Self::Value, S::Error>
    where
        S: SeqAccess<'de>,
    {
        while let Some(n) = seq.next_element::<T>()? {
            if self.sender.send(n).is_err() {
                break;
            }
        }
        Ok(())
    }
}

// An implementation of a serde deserializer that returns a channel receiver immediately,
// and in a background thread starts deserializing objects and sending them to a channel,
// up to a max amount of memory.
// The deserialized struct then has a ChannelGenerator, which iterates by receiving items from the channel.
pub fn deserialize_to_channel<'de, T, D>(deserializer: D) -> Result<ChannelGenerator<T>, D::Error>
where
    T: Deserialize<'de> + Send,
    D: Deserializer<'de>,
{
    let (sender, receiver) = sync_channel::<T>(0);

    // compiler error here about not being able to move D into a thread safely
    thread::spawn(move || {
        let visitor = ChannelVisitor {
            sender,
            f: PhantomData,
        };
        deserializer.deserialize_seq(visitor).unwrap();
    })
    .join()
    .unwrap();
    Ok(ChannelGenerator { receiver })
}

The problem I'm at now, is

D: Deserializer cannot be sent between threads safely

I've tried spawning the thread inside of visit_seq, but I get a similar issue with S: SeqAccess, since neither implement the Send trait, and I can't introduce it since that would make my implementation more restrictive than the Visitor trait.
Any insight into how I could implement an array deserializer that starts the deserialization by tossing messages onto a queue, and handle the deserialized objects by reading from the queue in two separate threads?
Writing that last piece out, I'm realizing maybe I should spawn the receiving thread first in the background, and then start deserializing...  I'll try that out and get back to this.  Any hints in the meantime would be appreciated!
edit: okay that didn't work. I just added two more channels to send the input/output back and forth between two external threads, but the main problem is still that returning the deserialized outer struct is blocked on putting all of the internal items onto the channel, before we can return the receiver. I need some way to either pass in the receiver to the deserializing function, or to run the deserialization at that level in a background thread.
Latest code can be found here, entry point is src/main.rs.
Comment: The reason that github one works and yours doesn't is that serde only has openings for passing values at the beginning and end. You can't, in the middle of deserialization, toss a `Sender` to a `deserialize_with` function, and you can't get a `Receiver` out of a `deserialize_with` until everything has been deserialized. So, this only works if your visitor visits the entire input.
Comment: side note: at the moment your implementation does not return the channel receiver immediately, it `join()`s and thus you cold just strip all the thread stuff with out change of semantics, you probably want to return the handle alongside the `Receiver` or not at all, btdefinitely you don't want to `.join()`
Comment: @ChayimFriedman can't add further constraints when implementing a trait.  i would have to add that constraint to serde's Visitor trait (iirc), which isn't my code.
Comment: you cannot `derive(Deserialize)` then, because the `deserializer` it would use does not conform to the requirements on the function in the `deserilize_with` annotation, `deserialize_to_channel`.  this just moves the error elsewhere.  it seem like i just have to implement a custom deserializer that can be made thread-safe.
Comment: The compiler is suggesting you a fix (add `+ Send`), why didn't you apply it?
Comment: You don't need to add that when implementing the trait, you need to add that to `deserialize_to_channel()`.
Answer: You are right that you need some way to pass a SyncSender to the deserialize function. The best way to pass state into deserialization is to implement DeserializeSeed instead of Deserialize. The documentation for DeserializeSeed states:

If you ever find yourself looking for a way to pass data into a Deserialize impl, this trait is the way to do it.

In our case, you're needing to pass in a sender so that the data can be sent to another thread as it is deserialized.
Unfortunately, DeserializeSeed implementations are not derivable, so we will need to write our own trait implementation. It contains a bit of boilerplate, but I'll break it down as best I can.
Deserializing the Items
In serde, we can deserialize from a "seed" which contains state needed for the deserialization. In our case, the state contains the SyncSender. So, we can define a seed for deserializing the items as follows:
struct ItemsSeed<T> {
    sender: SyncSender<T>,
}

Now we implement DeserializeSeed for ItemsSeed. The implementation looks very similar to a normal Deserialize implementation, and the Visitor we define is very similar to the one in your example code:
impl<'de, T> DeserializeSeed<'de> for ItemsSeed<T>
where
    T: Deserialize<'de>,
{
    type Value = ();

    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct ItemsVisitor<T> {
            sender: SyncSender<T>,
        }

        impl<'de, T> Visitor<'de> for ItemsVisitor<T>
        where
            T: Deserialize<'de>,
        {
            type Value = ();

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("an array of objects of type T")
            }

            fn visit_seq(self, mut seq: A) -> Result<Self::Value, A::Error>
            where
                A: SeqAccess<'de>,
            {
                while let Some(n) = seq.next_element()? {
                    // Might want some better error handling here.
                    if self.sender.send(n).is_err() {
                        break;
                    }
                }
                Ok(())
            }
        }

        deserializer.deserialize_seq(ItemsVisitor {
            sender: self.sender,
        })
    }
}

The main difference between this and a normal Deserialize implementation is that we have access to the sender during deserialization. We also define a return value, which in our case is just type Value = ();, as the only deserialized data is already sent to another thread to be processed there instead.
Deserializing the File struct
Since your example includes data besides the incredibly long array, we want to deserialize that data into a File struct at the same time. As I mentioned before, we can't actually derive a DeserializeSeed implementation like we can a Deserialize implementation, so unfortunately the #[derive(Deserialize)] turns into a lot of boilerplate. This is the DeserializeSeed implementation for File:
#[derive(Debug)]
pub struct File {
    pub a: String,
    pub b: String,
}

pub struct FileSeed<T> {
    sender: SyncSender<T>,
}

impl<'de, T> DeserializeSeed<'de> for FileSeed<T>
where
    T: Deserialize<'de>,
{
    type Value = File;

    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer<'de>,
    {
        enum Field {
            A,
            B,
            Items,
        }

        impl<'de> Deserialize<'de> for Field {
            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
            where
                D: Deserializer<'de>,
            {
                struct FieldVisitor;

                impl<'de> Visitor<'de> for FieldVisitor {
                    type Value = Field;

                    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                        formatter.write_str("`a`, `b`, or `items`")
                    }

                    fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
                    where
                        E: de::Error,
                    {
                        match value {
                            "a" => Ok(Field::A),
                            "b" => Ok(Field::B),
                            "items" => Ok(Field::Items),
                            _ => Err(de::Error::unknown_field(value, FIELDS)),
                        }
                    }
                }

                deserializer.deserialize_identifier(FieldVisitor)
            }
        }

        struct FileVisitor<T> {
            sender: SyncSender<T>,
        }

        impl<'de, T> Visitor<'de> for FileVisitor<T>
        where
            T: Deserialize<'de>,
        {
            type Value = File;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("struct File")
            }

            fn visit_map(self, mut map: A) -> Result<Self::Value, A::Error>
            where
                A: MapAccess<'de>,
            {
                let mut a = None;
                let mut b = None;
                let mut items = None;

                while let Some(key) = map.next_key()? {
                    match key {
                        Field::A => {
                            if a.is_some() {
                                return Err(de::Error::duplicate_field("a"));
                            }
                            a = Some(map.next_value()?);
                        }
                        Field::B => {
                            if b.is_some() {
                                return Err(de::Error::duplicate_field("b"));
                            }
                            b = Some(map.next_value()?);
                        }
                        Field::Items => {
                            if items.is_some() {
                                return Err(de::Error::duplicate_field("items"));
                            }
                            items = Some(map.next_value_seed(ItemsSeed {
                                sender: self.sender.clone(),
                            })?);
                        }
                    }
                }

                if items.is_none() {
                    return Err(de::Error::missing_field("items"));
                }

                Ok(File {
                    a: a.ok_or_else(|| de::Error::missing_field("a"))?,
                    b: b.ok_or_else(|| de::Error::missing_field("b"))?,
                })
            }
        }

        const FIELDS: &[&str] = &["a", "b", "items"];
        deserializer.deserialize_struct(
            "File",
            FIELDS,
            FileVisitor {
                sender: self.sender,
            },
        )
    }
}

This is very similar to the Manually implementing Deserialize for a struct example given on the official serde website. The main difference is that when we deserialize the items, we pass the SyncSender state using next_value_seed() method. Additionally, we don't actually include items in the File struct, because there is no data returned by the deserialization, as it's all processed in a separate thread.
Running deserialization on a separate thread
Now we put everything together. We can create our synchronous channel, spawn the deserialization in a separate thread, and process the received values as deserialization is occurring in the background.
fn main() {
    let (sender, receiver) = sync_channel::<u32>(0);

    // Deserialize in a separate thread.
    let deserialize_thread = thread::spawn(|| {
        let mut deserializer = serde_json::de::Deserializer::from_str(
            "{\"a\": \"foo\", \"b\": \"bar\", \"items\": [0, 1, 2, 3, 4, 5]}",
        );
        FileSeed { sender }.deserialize(&mut deserializer)
    });

    while let Ok(value) = receiver.recv() {
        // Process the deserialized values here.
        dbg!(value);
    }

    // You can also access the `File` after deserializing is complete.
    dbg!(deserialize_thread.join());
}

Note that I chose to make T be u32, but you can use any generic T that implements Deserialize.
Running this program outputs the following:
[src/main.rs:197] value = 0
[src/main.rs:197] value = 1
[src/main.rs:197] value = 2
[src/main.rs:197] value = 3
[src/main.rs:197] value = 4
[src/main.rs:197] value = 5
[src/main.rs:200] deserialize_thread.join() = Ok(
    Ok(
        File {
            a: "foo",
            b: "bar",
        },
    ),
)

You can see and run my whole solution on this playground.
Comment: this is a huge help, thank you for writing that out!  i felt like i kept going in circles trying to figure out pieces of how to use serde.  appreciate this answer a ton, [here's my implementation so far](https://github.com/DrewMcArthur/rust-cms-json-parser/commit/d3a4a3576206a0bd5fbadd3cc8a97af8ee1eb46b) :)


Rules > Send mail to product's creator?

Question: I tried to use a rule found in another topic (https://www.drupal.org/node/2050565), but it doesn't work. I want to send an automatic email to the product's creator, when the product is sold.
{ "rules_send_email_to_product_owner" : {
"LABEL" : "Send email to product owner",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules", "commerce_checkout" ],
"ON" : { "commerce_checkout_complete" : [] },
"IF" : [
  { "data_is" : {
      "data" : [ "commerce-order:commerce-line-items:0:type" ],
      "value" : "product"
    }
  }
],
"DO" : [
  { "mail" : {
      "to" : [ "commerce-order:commerce-line-items:0:commerce-product:creator:mail" ],
      "subject" : "Test mail",
      "message" : "Test mail nr 1",
      "from" : "Sender",
      "language" : [ "commerce-order:state" ]
    }
  }
]

}
}
Any help would be greatly appreciated :)
Comment: Hi and welcome to Drupal Answers. What field is `creator`? Have you verified that your site sends mails? Havr you verified that the rule triggers? Can you test additional actions?
Comment: Yes i manage to send emails to buyers with Rules, i have only this problem with Seller.
Answer: You need to debug the rule!
This is not a direct answer but can help you find the problem. And it is too long to post as a comment. You should follow these steps regardless of other similar rules working (a teeny tiny difference can be causing the rule to fail).
Required tools
Install the devel module. but NOT on production site, only on your development site. It will add an extra action to rules debug a value under devel section.

Is the rule actually triggered?
Method 1:
Add a simple action, display a message and enter a simple text. Do the required actions actually needed to fire the rule (buy a sample product). Is the message displayed or not?
Drawback: If the message is not displayed, you wont actually know if the action is not triggered OR there is some error in rule definition, preventing the rule engine from executing it.
Method 2: Go to admin/config/workflow/rules/settings On your site, there is a checkbox to enable rules debugging. It's something like dpm() but for rules only with helpful information. Whenever a rule is triggered, all the thing happening (or failing) will show up in the debugging area. If something has failed it will be printed in red so you can easily find it.
Drawback: Can get annoying going through the rule stacktrace :|
If it's not firing:
There should be something wrong with the event/conditions you have set, Or a fatal error in the rule action definition. If the later find it with method 2 above.
Are the data you are using not null and valid?

Temporarily remove the condition commerce-order:commerce-line-items:0:type. Since you are working on development site this should not be a problem. In the actions (Important: In the beginning, as the first action) add the action debug a value. the value to debug would be "commerce-order:commerce-line-items:0:type. Go and buy the sample product, the rule should fire, and devel's debug a value would print the type. Check if it is set and is what you want it to be.
Do the same for commerce-order:commerce-line-items:0:commerce-product:creator:mail.
Do the same for "language" : [ "commerce-order:state" ] --> However this sounds fishy! why would you set the language as the state?!! You should read the language from user entity (the creator).

Have fun debugging rules!

Rules > Send mail to product's creator?


Is the barycenter of a simplex invariant under affine trasformations?

Question: While reading Lee's Introduction to Topological Manifolds chapter $13$, about the subdivision operator in homology, I realized that the author only defines the barycenter of a $p-$simplex $\sigma=[v_0,\dots,v_p]$ as the only point $b_\sigma$ in its interior such that all its barycentric coordinates are equal, that is
$$b_\sigma=\sum_{i=0}^p\frac{1}{p+1}v_i$$
And then, if $\alpha:\Delta_p\longrightarrow\mathbb{R}^n$ is a singular affine simplex (that is, an affine map from the standard $p-$simplex of $\mathbb{R}^p$ into $\mathbb{R}^n$ for some $n$), instead of defining the barycenter of $\alpha$, he works with $\alpha(b_p)$ (where $b_p$ denotes the barycenter of the standard $p-$simplex in $\mathbb{R}^p$). My questions are the following;

The image of a simplex under an affine transformation is always another simplex?
Isn't the image of the barycenter of a simplex the same as the barycenter of its image?

Thanks in advance for your help.
Comment: I answered that already below : yes. Simply, you need to not get fooled by the fact that the actual dimension of the image is not relevant. You need to keep track of all the vertices even if there is some overlap.
Comment: The definition of simplex that you gave does not seem to imply that the vertices are affinely independent.
Comment: When you think that it should be the middlepoint, you are forgetting that what you see is not a line segment, it is a singular $p$-simplex.
Comment: @ArnaudMortier, Well, that's the point; the image of a singular affine simplex should be a regular simplex in $\mathbb{R}^n$, so again, is the barycenter of the image equal to the image of the barycenter?
Comment: I see that those simplicial complexes are not geometric, they are continuous images of geometric complexes. Then the images of barycentric centers are quite a bit arbitrary. You can still prove things, especially when the map of a geometric complex is "nice".
Comment: @WlodAA Not quite; when a $p-$simplex is affine, we are specifically talking about the only affine transformation that sends the points $e_0,\dots,e_p$ or $\mathbb{R}^p$ to $p$ points in $\mathbb{R}^n$, although they need not be affinely independent, or even distinct.
Comment: @Akerbeltz, sure, but then there are complications. The image of, say 3-simplex may have a 2-simplex as its image while one original vertex may fall anywhere. Then the image of the original barycenter will not be the barycenter of that 2-simplex. The thing would need explicit combinatorial subdivisions, quite a bit removed from the naive geometric image-complex.
Comment: @WlodAA Why would the barycenter fall anywhere? I think the explaination of Arnaud is quite satisfactory, and would contradict your assertion.
Comment: I see your point now. Make it into an answer, and I will accept it soon.
Comment: The answer is YES, granted that the images of the vertices are linearly independent. Thus, this is a YES for affine injections (hence affine isomorphisms).
Comment: But in general, an affine simplex does not have to send the vertices of the standard simplex to affinely independent points in $\mathbb{R}^n$... so?
Comment: My main concern is with affine transformations that don't preserve affine independence... can we find, for example, an affine map that deforms, lets say, a $p-$simplex into a line segment, carrying the barycenter of the first into some point of the second, that is not its middlepoint?
Answer: The key point is that

An affine transformation preserves all affine combinations, in the sense that $$f\left(\operatorname{Bar}(\alpha_i, P_i)\right )=\operatorname{Bar}(\alpha_i, f(P_i))$$
  whenever $\sum \alpha_i=1$.

As a result, the answer to both questions is YES, since

A simplex is the convex hull of its vertices, and the convex hull is by definition the set of all affine combinations with non-negative coefficients.
The barycentre is the only affine combination with equal weights.
Comment: Just a simple question about your terminology; what does $\text{Bar}(\alpha_i,P_i)$ represent?
Comment: @Akerbeltz The barycentre of some points $P_i$ with coefficients $\alpha_i$.


Como atualizar o valor de uma celula com base em outra no Grid View WPF

Question: Eu tenho a seguinte Grid, gostaria de saber como posso fazer para que quando sair do campo Ganho ou Gasto ele atualize o Total?
Caso exista outra forma de fazer fico feliz em conhecer.
Total = (Ganho - Gasto)
    private void txtGanho_LostFocus(object sender, RoutedEventArgs e)
    {
        TextBox txtGanho = (TextBox)sender;
        //Aqui eu tenho o valor do Ganho, como posso encontrar na Grid os TextBox com Gasto e Total
    }

    private void txtGasto_LostFocus(object sender, RoutedEventArgs e)
    {
        TextBox txtGasto = (TextBox)sender;
        //Aqui tenho o valor do Gasto, como posso encontrar na Grid os TextBox com Ganho e Total
    }

Arquivo .xaml
   <DataGrid x:Name="dgControleFinanceiro" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Id" Binding="{Binding Id}" IsReadOnly="True" />
            <DataGridTextColumn Header="Data" Binding="{Binding Data}" IsReadOnly="True"/>

            <DataGridTemplateColumn Header="Ganho do dia"  IsReadOnly="False">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Name="txtGanho" Text="{Binding Ganho}" LostFocus="txtGanho_LostFocus"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

            <DataGridTemplateColumn Header="Gasto do dia"  IsReadOnly="False">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Name="txtGasto" Text="{Binding Gasto}" LostFocus="txtGasto_LostFocus"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

            <DataGridTemplateColumn Header="Total do dia" IsReadOnly="true">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Name="txtTotal" Text="{Binding Total}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

        </DataGrid.Columns>
    </DataGrid>

Answer: A melhor forma de se fazer isso, é através de uma ViewModel. Essa ViewModel deverá implementar a interface INotifyPropertyChanged. O UpdateSourceTrigger do Binding das propriedades ValorGanho e ValorGasto precisa ser LostFocus, que quando perder o foco, o set será executado. Então sempre que acontecer um set em ValorPago ou ValorGanho, a View será notificada da mudança no Total, refletindo na grid.
public class ControleFinanceiroViewModel : INotifyPropertyChanged
{
   public event PropertyChangedEventHandler PropertyChanged = delegate { };

   protected void NotificarMudancaPropriedade([CallerMemberName] string nomePropriedade = null)
   {
     if (nomePropriedade != null)
     {
        PropertyChanged(this, new PropertyChangedEventArgs(nomePropriedade));
     }
   }

   private decimal valorGasto;
   public decimal ValorGasto
   {
     get { return this.valorGasto; }
     set
     {
       this.valorGasto = value;
       this.NotificarMudancaPropriedade();
       this.NotificarMudancaPropridade("Total");
     }
   }

   private decimal valorGanho;
   public decimal ValorGanho
   {
     get { return this.valorGanho; }
     set
     {
       this.valorGanho= value;
       this.NotificarMudancaPropriedade();
       this.NotificarMudancaPropridade("Total");
     }
   }

   public decimal Total
   {
     get { return this.ValorGanho - this.ValorGasto; }
   }
}

O Binding ficará assim da forma abaixo, e você remove o evento LostFocus
<DataTemplate>
    <TextBox Name="txtGanho" Text="{Binding Ganho, Mode = TwoWay, UpdateSourceTrigger = LostFocus}" />
</DataTemplate>

Não é uma boa prática em WPF trabalhar com eventos, uma vez que pode trabalhar com MVVM. E recuperar objetos de outra célula da grid em WPF pode ser bem trabalhoso. Então deixo essa abordagem.
https://msdn.microsoft.com/pt-br/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx
Comment: Obrigado pela sua resposta @Murilo assim que eu conseguir vou tentar implementar desta forma. Eu não sabia que existia esse MVVM, você conhece algum material para que eu possa aprender? Pois eu programo tudo com eventos.
Comment: Não tenho material especifico. Aprendi com fóruns e tutoriais(via google mesmo); Mas você pode conseguir recuperar seu textbox através da TreeVisual https://msdn.microsoft.com/pt-br/library/system.windows.media.visualtreehelper(v=vs.110).aspx http://www.wpftutorial.net/logicalandvisualtree.html


Strange notation in a Chopin waltz

Question: I have two questions regarding Chopin's Waltz in E minor.

On the picture there are two A notes. How does one play the A's? (The rythm  is 3/4  ). For me this does not make any sense?

In the piece are staccato notes combined with pedals. How are they played? Because the staccato automatically vanishs if I press the sustain pedal.
Comment: I suggest to split this to two questions. First one is easy (and surely a duplicate of an existing one, liable to close votes): The half note is intended as separate voice lasting during the following two eights and the quarter of the second voice.
Comment: You can find an answer for the second part of your question [here](https://music.stackexchange.com/a/4051/34776).
Comment: First part is answered [here](https://music.stackexchange.com/q/61200/2600), so in total both parts are duplicate and I'll vote to close.
Answer: Those are two simultaneous voices.  The execution basically is that you play the attack (key down) of the A to fit with the beamed eighth phrase but you release that key only together with the quarter note C.
This sort of multivoice notation that requires making some judicious choices for approximating on the piano (which has only a single manual) is not exactly rare.

Strange notation in a Chopin waltz


Equivalence Relations and 1-1 Correspondences

Question: I have an engineering instructor who has claimed that "equivalence relations and one-to-one correspondences are pretty much the same thing".
However, I believe the answers to both of the following statements are decided false:
1. If $R_1$ is an equivalence relation on a (possibly infinite) set $A$, then $R_1$ is a one-to-one correspondence from $A$ to $A$.
2. If $R_2$ a one-to-one correspondence from $A$ to $A$, then $R_2$ is an equivalence relation on $A$.
Unfortunately I cannot provide more context to the instructor's statement; frankly, I have no idea why he even brought this up. But can his statement actually make sense, if it is put on a more precise footing?
Comment: Equivalence relations and partitions, certainly, but I do not see any useful connection between equivalence relations and one to one correspondences.
Answer: Are you sure he didn't say equivalence relations and partitions are the same thing?
(Wikipedia)
However, it is certainly false that equivalence relations are the same as bijections from a set to itself. If nothing else, consider that for example, there are 5 equivalence relations on the set $X=\{a,b,c\}$, and 6 bijections from $X$ to itself.
Answer: $A=\{a,b,c\}$. $R_1=\{(1,1),(2,2),(3,3), (1,2), (2,1)\}$ is an equivalence relation on $A$, but not a function.
$R_2=\{(1,2),(2,3), (3,1)\}$ is a one-one correspondence from $A$ to $A$ but not reflexive.


How to deep compare each element with other elements in a list in scala (optional fileds)

Question: I have case class that represents report, and report have expenses.
case class FakeExpense(amount: Option[Double], country: Option[String], currency: Option[String])
case class FakeReport(id: Int, expenses: List[FakeExpense])

and I want to return true/false if report is valid or not, and it is not valid if there is 2 expenses with the exact same fields...what would be the right way with scala to do something like this?
a valid report:
val report = FakeReport(1, List(FakeExpense(Some(150), Some("US"), Some("USD")),FakeExpense(Some(85), Some("DE"), Some("EUR"))))

a non valid report:
val report = FakeReport(2, List(FakeExpense(Some(150), Some("US"), Some("USD")),FakeExpense(Some(150), Some("US"), Some("USD"))))

Thanks!
Answer: Consider List.distinct approach like so
def isValidReport(report: FakeReport): Boolean =
  report.expenses.distinct.length == report.expenses.length

The above solution makes three passes through the list. Applying Luis' comment we can reduce to two passes like so
def isValidReport(report: FakeReport): Boolean = {
  report.expenses.sizeIs == report.expenses.iterator.distinct.length
}

We shave off one pass because distinct.length collapses to a single transformation in iterator.distinct.length. sizeIs is a potential quick-savings where we might not have to go through the whole list.
Here is a single-pass tail-recursive solution based on HashSet having O(eC) lookup and insertion
def isValidReport(report: FakeReport): Boolean = {
  val set = mutable.HashSet[FakeExpense]()
  @tailrec def loop(l: List[FakeExpense]): Boolean = l match {
    case Nil => true
    case h :: t => if (set.add(h)) loop(t) else false
  }
  loop(report.expenses)
}

Also note how we might return early on first duplicate.
Comment: @LuisMiguelMejíaSuárez I could not figure out how `iterator` helps however I have updated question with single-pass solution based on `HashSet`.
Comment: I would use an `iterator` on the first one and `sizeIs` to reduce the number of iterations.
Comment: the `iterator` before the `distinct` would reduce the number of iterations from two to one.
Comment: @LuisMiguelMejíaSuárez Ah, do you mean because `iterator.distinct.length` becomes single transformation?
Comment: Yeah, anyways your `HashSet` solution is better :p
Answer: If you need exactly same reports disregarding double comparison with epsilon(|a-b| < epsilon, where epsilon is small enough), you just can turn list to set as it will remove duplicate entries and then check if the size of your list changed.
fakeReport.expenses.toSet.length == fakeReport.expenses.length

It will work till everything that inside your classes you want to use with == correctly implement equals methods (primitive types, most of default classes, case classes and objects do so). What will not implement this correctly is an array and possibly certain java classes and these classes wrapped into AnyVal classes.


Flutter equivalent to curl --resolve

Question: I want make https request to ip address, but it should use domain name ssl certificate,
basically I want curl --resolve equivalent in flutter. or local DNS inside the app.
Working  curl command curl --resolve example.com:443:192.168.1.55 https://example.com
Answer: To set custom DNS resolution for the request, use dart:io's HttpClient and make a custom instance with the desired configuration.
import 'dart:io';

import 'package:http/http.dart' as http;
import 'package:http/io_client.dart' as http;

Future<http.Response> resolve(String url, String domain, String ipAddress) async {
  // Create an HttpClient with the custom DNS resolution
  final httpClient = HttpClient()
    ..badCertificateCallback = ((X509Certificate cert, String host, int port) => true)
    ..connectionTimeout = const Duration(seconds: 10);

  // Add the custom IP address for the domain
  httpClient.findProxy = (uri) => 'PROXY $ipAddress:443';

  // Create an http.Client using the custom HttpClient
  final client = http.IOClient(httpClient);

  // Make the request
  final response = await client.get(Uri.parse(url), headers: {
    'Host': domain,
  });

  // Close the client to avoid resource leaks
  client.close();

  return response;
}

Comment: Thanks for reply, but I am getting **Connection closed before full header was received** error

Flutter equivalent to curl --resolve


Chain rule of multivarible function.

Question: If $u=x^{4}f(y/x,z/x)$ then I have to show that $xu_{x}+yu_{y}+zu_{z}=4u.$ How to prove it? $u_{x}=4x^{3}f(y/x,z/x)+x^{4}f_{p}p_{x}+f_{q}q_{y}$ if $p=y/x,q=z/x$. Now how to proceed further. Please help. Thanks.
Comment: That should be $q_x$.
Answer: It is just careful expansion:
$$
t = x u_x + y u_y + u_z = 4x^4 f(p,q) + x^4( x f_p p_x + x f_q q_x + y f_p p_y + y f_q q_y + z f_p p_z + z f_q q_z)
$$
Further
$$
p_x = -y/x^2, p_y = 1/x, p_z = 0 \\
q_x = -z/x^2, q_y = 0, q_z = 1/x
$$
So
$$
t = 4x^4 f(p,q) + x^4(-(y/x)f_p -(z/x)f_q + (y/x)f_p + (z/x) f_q) = 4u
$$
Answer: Hints: It may be helpful to think of $f(x_1,x_2)$ where $$x_1=x_1(x,y,z)=y/x$$ $$x_2=x_2(x,y,z)=z/x$$
Then, for example, $$u_x=4x^3f(x_1,x_2) + x^4\tfrac{\partial}{\partial x}[f(x_1,x_2)]$$ Now compute $$\tfrac{\partial}{\partial x}[f(x_1,x_2)] =f_1(x_1,x_2)\tfrac{\partial x_1}{\partial x} + f_2(x_1,x_2)\tfrac{\partial x_2}{\partial x}$$
and you should be able to take it from here. ($u_y$ and $u_z$ are similar, but won't involve the product rule).
Comment: I know upto your hint as I mention in my question...its not the solution..

Chain rule of multivarible function.


ComboBox deixando form lento ao carregar dados winforms C# visual studio

Question: Boa noite, estou com um dúvida. Tenho um cadastro de clientes que lista as cidades em um combobox, só que toda vez que abro o cadastro ele carrega os dados e isso deixa lento o carregamento do form, tem alguma forma de fazer a consulta apenas a primeira vez que abre o form e depois reutilizar?
Esse é o Código que popula o checkBox
       String nomeConexao = LoginInfo.StringConexao;
       String string_conn =
       ConfigurationManager.ConnectionStrings[nomeConexao].ConnectionString;
        SqlConnection conn = new SqlConnection(string_conn);
        try
        {
            conn.Open();
        }
        catch (SqlException sqle)
        {
            MessageBox.Show("Falha ao efetuar a conexão. Erro: " + sqle);
        }
        String CodCidadeEmpresa = DadosEmpresa.CodCidade;

        String sql = "SELECT COD, CIDADE FROM CODMUNICIPIO ORDER BY CIDADE";
        SqlDataAdapter da = new SqlDataAdapter(sql, conn);
        DataTable dtResultado = new DataTable();
        dtResultado.Clear();
        CbCidade.DataSource = null;
        da.Fill(dtResultado);
        CbCidade.DataSource = dtResultado;
        CbCidade.ValueMember = "COD";
        CbCidade.DisplayMember = "CIDADE";
        CbCidade.SelectedItem = "";
        CbCidade.SelectedValue = CodCidadeEmpresa;
        CbCidade.Refresh();
        conn.Close();

Comment: blz, Muito Obrigado
Answer: Uma outra opção seria carregar o DropDown de forma assíncrona, assim não iria travar o form enquanto ele é carregado:
Crie um método assíncrono da seguinte forma:
private async void CarregaDropDownCidades()
{
    String nomeConexao = LoginInfo.StringConexao;
    tring string_conn =
    onfigurationManager.ConnectionStrings[nomeConexao].ConnectionString;
    SqlConnection conn = new SqlConnection(string_conn);
    try
    {
        conn.Open();
    }
    catch (SqlException sqle)
    {
        MessageBox.Show("Falha ao efetuar a conexão. Erro: " + sqle);
    }
    String CodCidadeEmpresa = DadosEmpresa.CodCidade;

    String sql = "SELECT COD, CIDADE FROM CODMUNICIPIO ORDER BY CIDADE";
    SqlDataAdapter da = new SqlDataAdapter(sql, conn);
    DataTable dtResultado = new DataTable();
    dtResultado.Clear();
    da.Fill(dtResultado);

    Invoke((MethodInvoker)delegate
    {
        CbCidade.DataSource = null;
        CbCidade.DataSource = dtResultado;
        CbCidade.ValueMember = "COD";
        CbCidade.DisplayMember = "CIDADE";
        CbCidade.SelectedItem = "";
        CbCidade.SelectedValue = CodCidadeEmpresa;
        CbCidade.Refresh();
    });
    conn.Close();
}

No load do seu forma faça a seguinte chamada:
private async void Form1_Load(object sender, EventArgs e)
{
    await Task.Run(() =>
    {
        CarregaDropDownCidades();
    });
}

Comment: Cara funcionou muito bem, ele só da uma leve trancadinha no abrir mas nada que se compare ao que era. Muito obrigado. Abraço
Comment: @Rafa Não tem mais itens carregando dados de banco? você pode fazer isso para todos componentes que estejam "pesando" o `load` dor `form`
Comment: Vou dar uma organizada no meu código, certamente deve ter algo.
Answer: Tente remover o ORDER BY CIDADE do seu select e faça a ordenação diretamente no combo CbCidade.Sorted = true;.
Se a dica acima não resolver, tente então o segundo método
Crie uma classe cidades
public class Cidades
{
  public int id {get; set;}
  public string nome {get; set;}
}

Depois no form principal crie uma lista publica
public List<Cidades> ComboCidades {get; set;}

E ai na inicialização do seu sistema você carrega ela com os dados do select
Então quando abrir seu form que possui o combo de cidades você preenche ele com os itens da lista +/- assim
CbCidade.DataSource = ComboCidades;
CbCidade.DisplayMember = "nome";
CbCidade.ValueMember = "id";


Como passar dados de uma tabela para outra com checkbox?

Question:
Olá, pessoal. Sou novo em programação Web e estou atualmente estudando alguns conceitos de Angular. Meu problema é o seguinte: passar os itens selecionados de uma tabela, por checkbox, para uma outra tabela. Alguém poderia me ajudar? Muito obrigado!
Comment: Você pode colocar o código que tem até então como exemplo?
Answer: Você só precisa filtrar a segunda tabela de acordo com o atributo selecionado na primeira:

angular
  .module('ModuloTabelas', [])
  .controller('ControllerTabelas', ControllerTabelas);

ControllerTabelas.$inject = [];

function ControllerTabelas() {
  var tabelas = this;
  tabelas.dados = [];

  iniciar();

  function iniciar() {
    tabelas.dados.push({
      codigo: 1,
      selecionado: false,
      texto: 'opcao 1'
    });
    tabelas.dados.push({
      codigo: 2,
      selecionado: false,
      texto: 'opcao 2'
    });
    tabelas.dados.push({
      codigo: 3,
      selecionado: false,
      texto: 'opcao 3'
    });
    tabelas.dados.push({
      codigo: 4,
      selecionado: false,
      texto: 'opcao 4'
    });
    tabelas.dados.push({
      codigo: 5,
      selecionado: false,
      texto: 'opcao 5'
    });
  }
}
table {
  font-family: arial, sans-serif;
  border-collapse: collapse;
  display:inline-block;
  vertical-align: text-top;
}

td, th {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}

tr {
  height: 37px;
}

tr:nth-child(even) {
  background-color: #dddddd;
}

            Opções

            Código

            Texto

            <input type="checkbox" ng-model="linhaTb1.selecionado" />

            {{linhaTb1.codigo}}

            {{linhaTb1.texto}}

            Filtradas

            Código

            Texto

            {{linhaTb2.codigo}}

            {{linhaTb2.texto}}

Comment: Poxa vida, era mais simples do que pensei. Pra quem está iniciando, às vezes parece complicado. Muito obrigado, Sorack. Vai me ajudar bastante. Abraços!


Compute an integral related with gaussian

Question: Can anyone help me to solve this integral, I think we can try polar coordinate and some property of Gaussian  density, but I stuck for long time. Also the WolframAlpha cannot compute this integral.
$$
\int_{0}^{\infty} \int_{-2^{n / 2}x}^{\infty} \frac{1}{2 \pi\sqrt{(j-1)}} \exp \left(-\frac{x^{2}}{2(j-1)}-\frac{y^{2}}{2}\right) d y d x
$$
Thanks a lot!
Comment: Too tired and go to sleep, hope to see some answer when wake up
Answer: The inner integral is simple and we are left with
$$2 \sqrt{2 \pi(j-1) }\,I=\int_0^\infty e^{-\frac{x^2}{2(j-1)}}\,dx+\int_0^\infty e^{-\frac{x^2}{2(j-1)}}\text{erf}\,\left(2^{\frac{n-1}{2}} x\right)\,dx$$ The first integral is simple
$$\int_0^\infty e^{-\frac{x^2}{2(j-1)}}\,dx=\sqrt{\frac{\pi  (j-1)}{2}  }$$ For the second one let $2^{\frac{n-1}{2}} x=t$  and $k=\frac{2^{-n}}{j-1} $ to make it
$$2^{-\frac {n-1}2}\int_0^\infty e^{-k t^2}\text{erf}(t)\,dt=\frac{2^{-\frac {n-1}2}}{\sqrt{\pi k} }\tan ^{-1}\left(\frac{1}{\sqrt{k}}\right)$$
Look here (formula $4.3.2$)
As a result
$$I=\frac 14+\frac 1 {2\pi} \tan ^{-1}\left(\sqrt{2^n(j-1)}\right)$$
Comment: Thanks a lot, actually I stuck at "erf" term, I need to learn it! Thanks for your resource
Answer: If $Z$ and $Y$ are independent standard Gaussian random variables and $X=\sqrt{j-1}\cdot  Z$, then the integral is
$$P(X \ge 0, Y \ge -2^{n/2} X) = P(Z \ge 0, Y \ge -2^{n/2} \sqrt{j-1} \cdot Z)$$
If you plot the region of the $(z,y)$ plane defined by the constraints on $(Z,Y)$, you will see it is a sector whose boundaries are given by the lines $z=0$ and $y=-2^{n/2} \sqrt{j-1} \cdot z$. (I strongly encourage you to draw a picture of this region.) Because the distribution of $(Z,Y)$ is radially symmetric, the probability that $(Z,Y)$ lies in this sector is the angle of this sector divided by $2\pi$.
Comment: This graphical idea is pretty good, I will try it! thanks a lot!


Density of the cannonball problem packing

Question: The Cannonball problem states that you can make a rectangle and a pyramid with a rectangle base using $4900$ cannonballs (with radius $1$).

The question is, what is the density of the packing in the rectangle?

I think: the packing can be written as:
$$\frac{\text{total area of the cannonballs}}{\text{total area of the rectangle}}$$
Now, a rectangle is just a special type of a regular n gon, the area of such a gon is equal to:
$$\frac{ns^2\cot(\pi/n)}{4}$$
And the area of circle with radius $1$ are given by:
$$\pi$$
So:
$$\frac{\pi\cdot\text{number of cannonballs}}{\frac{1}{4}\cdot n\cdot\left(\text{number of cannonballs per side}\right)^2\cot(\pi/n)}$$
Now, I got:
$$\frac{\pi\cdot4900}{\frac{1}{4}\cdot4\cdot70^2\cot(\pi/4)}=\pi$$
But that is impossible because this means that the cannonballs are taking more space than the area of the rectangle in the first place.
What am I doing wrong?
Answer: A circle occupies $\frac \pi4$ of the square it is inscribed in.  If the cannonball has radius $1$ the square has side $2$, which is the factor $4$ you are missing.   You took $s=1$
Comment: Do you mean that $s=2\cdot\text{number of cannonballs per side}$?
Comment: Yes. I was just thinking of one ball in a square. If you have $70$ on a side the square is $140 \times 140$


ServiceWorker registration failed: DOMException: Only secure origins are allowed (see: https://goo.gl/Y0ZkNV)

Question: I wrote code to get GCM id to send chrome push notifications. I wrote below code to get GCM id.
<code>var ChromePushManager = function (serviceWorkerPath, callback) {
    if ('serviceWorker' in navigator) {
        navigator.serviceWorker.register(serviceWorkerPath).then(function (registration) {
            // Registration was successful
            console.log('ServiceWorker registration successful with scope: ', registration.scope);

            registration.pushManager.subscribe({userVisibleOnly: true}).then(function (subscription) {
                console.log("subscription.subscriptionId: ", subscription.subscriptionId);
                console.log("subscription.endpoint: ", subscription.endpoint);

                // TODO: Send the subscription subscription.endpoint
                // to your server and save it to send a push message
                // at a later date
                var register = ChromePushManager.getRegistrationId(subscription);
                callback(null, register);
            });
        }).catch(function (err) {
            // registration failed :(
            console.log('ServiceWorker registration failed: ', err);
        });
    } else {
        callback('Service workers aren\'t supported in this browser.', null);
    }
};

ChromePushManager.getRegistrationId = function (pushSubscription) {
    if (pushSubscription.subscriptionId) {
        return pushSubscription.subscriptionId;
    }

    var endpoint = 'https://android.googleapis.com/gcm/send/';
    parts = pushSubscription.endpoint.split(endpoint);

    if (parts.length > 1)
    {
        console.log("RegistrationId : " + parts[1]);
        return parts[1];
    }
}

var chromePushManager = new ChromePushManager('../js/service-worker.js', function (error, registrationId) {
                    if (error !== null) {
                        console.log(error);
                    }
});
</code>
It is working fine in http://localhost:8080. I uploaded this code in server. I was trying to execute code in server. I was getting error.
ServiceWorker registration failed:  DOMException: Only secure origins are allowed (see: https://goo.gl/Y0ZkNV).
I don't know what this error says. Could you please any once explain me what this error mean and give me a solution to resolve this error at my server side.
Thank you.
Comment: You have a problem with Cross Origin Domain if I had to guess...
Comment: What is Cross Origin Domain how can i resolve this issue
Comment: https://en.wikipedia.org/wiki/Same-origin_policy
Comment: u require your domain to be https ...
Answer: You need to use https on the server.
In error: Only secure origins are allowed (see: link).
From link in error:
Comment: You should also be aware that a service worker can't be initialised if the page is loaded locally e.g. for testing purposes. An error message can be avoided by testing against this additional condition: (!/^(?:|localhost|127\.0\.0\.1)$/.test(location.hostname))


Horizontal scroll and focus on the last number in a TextView

Question: I am doing a calculator app.
I have a TextView where the user writes the numbers to elaborate.
The TextView is inside a HorizontalScrollView. The HorizontalScrollView is inside a vertical LinearLayout. The TextView is single line, and, when the user writes a long number, the number comes out of the screen.
The TextView should follow the last number added by the user, so he can view the last numbers added without scrolling the screen. I don't want an autoscroll function that repeat the scroll every second, i only want focus on the last number added.
This is the TextView
<HorizontalScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <TextView
                android:id="@+id/numberDisplay"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="0dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="16dp"
                android:hint="0"
                android:scrollHorizontally="true"
                android:maxLines="1"
                android:layout_gravity="right"
                android:textSize="70sp" />

        </HorizontalScrollView>

Answer: Try this:

In your activity create variables for your scroll view and text view:
TextView textView;
HorizontalScrollView scrollView;

Initialize them in onCreate():
scrollView = (HorizontalScrollView) findViewById(R.id.scrollView); // Add this id to xml
textView = (TextView) findViewById(R.id.numberDisplay);

Each time you update text of your text view update scroll position of the scroll view:
scrollView.scrollTo(textView.getRight(), textView.getTop());

Update:
I realized that if you call textView.setText(...) and then immediately scrollView.scrollTo(...) it does not work since text view size is not updated immediately. You should rather update scoll position like this:
    scrollView.post(new Runnable() {
        @Override
        public void run() {
            scrollView.scrollTo(textView.getRight(), textView.getTop());
        }
    });


What's the Difference between the two of writings of arrays in typescript

Question:
TypeScript, like JavaScript, allows you to work with arrays of values.
  Array types can be written in one of two ways. In the first, you use
  the type of the elements followed by '[]' to denote an array of that
  element type:

var list:number[] = [1, 2, 3];

The second way uses a generic array type, Array:

var list:Array<number> = [1, 2, 3];

Are there any difference between these two syntax? Is there a good reason for 2 ways to write these? (I know generics are a thing in other languages but I've never used generics in any past lang).
Documentation
Comment: I would imagine it's to align with Javascript in a strongly-typed way. I mean, in Javascript you can also do `[]` and `new Array()`
Answer: They are equivalent. The number[] form is shorthand for the Array<number> generic.
The "array type literal" from section 3.7.4 of the spec is defined as:

An array type literal references an array type (section 3.3.2) with the given element type.

The "array type" from section 3.3.2 are described as:

Array types are named type references created from the generic interface type 'Array' in the global module with the array element type as a type argument.

This notes that arrays are defined as a generic type with an argument. The generic is the definitive version, but that description is followed immediately by:

Array type literals (section 3.7.4) provide a shorthand notation for creating such references.

There is no difference, the type[] form exists as a shorthand using pre-existing JS patterns.


Concat list and string in Python

Question: I am a bit do not understand the principle of the addition of lists and strings in Python.
For example, we have a list:
students = ['Ivan', 'Michael', 'Olga']
The result of the expression:
students += 'John'
will be:
['Ivan', 'Michael', 'Olga', 'J', 'o', 'h', 'n']
And in this case, string 'John' will be processed as a list and every symbol will be added to list students.
But why the processing of expression:
students = students + 'John'
occurs otherwise?
In this case, we just get an error.
I always thought that expressions a += b and a = a + b are equivalent.
But why here in one case the string is expanded to the list, and in the other case this does not happen and we get an error?
Comment: In [this](https://stackoverflow.com/questions/6951792/python-a-b-not-the-same-as-a-a-b) topic you can see why.
Comment: @mxsin, you can have a look at this https://hygull.github.io/codes/python2.7/stackoverflow/Q1.html. Here I have taken your problem as input and got the desired output.
Answer: This is expression a += b or a = a + b will not help you with list.
If you want to add one element to list then you can try.
students = ['Ivan', 'Michael', 'Olga']
students.append('John')

If you want to join to list. Then you can do.
students = ['Ivan', 'Michael', 'Olga']
student = ['John']
students = students + student

Or
 students.extend(student) #This list concatenation method is bit faster.

If you want to dig deeper. You can refer to this article here

Concat list and string in Python


What is this type of function called? How can I translate it to a different origin?

Question: A factory produces 1 widget per week. A builder builds 1 factory each week. A construction firm trains 1 new builder each week. Partially-produced things do not produce anything. Starting with 1 firm, how many of each thing will there be after $t$ weeks?
If we let ${n \choose k} = 0$ for $k > n$, then we find that:
$$
\begin{align}
\text{firms: } f_3(t) &= 1 &= {t \choose 0} \\
\text{builders: } f_2(t) &= t &= {t \choose 1} \\
\text{factories: } f_1(t) &= 1,3,6,10,\ldots &= {t \choose 2} \\
\text{widgets: } f_0(t) &= 1,4,10,20,\ldots &= {t \choose 3}
\end{align}
$$
In general: if at week $t_0$ we have $k_0$ widgets, $k_1$ factories, $k_2$ builders, and $k_3$ firms (and so on), then how many of each do we have $t$ weeks after $t_0$?
$$
\begin{align}
%f_i(t) &= &=& \sum_{j=i}^n {t \choose j-i} k_j \\
f_3(t) &= k_3 &=& \sum_{j=3}^n {t \choose j-3} k_j\\
f_2(t) &= k_2 + {t \choose 1}k_3 &=& \sum_{j=2}^n {t \choose j-2} k_j \\
f_1(t) &= k_1 + {t \choose 1}k_2 + {t \choose 2}k_3 &=& \sum_{j=1}^n {t \choose j-1} k_j \\
f_0(t) &= k_0 + {t \choose 1}k_1 + {t \choose 2}k_2 + {t \choose 3}k_3 &=& \sum_{j=0}^n {t \choose j}k_j
\end{align}
$$
How can we translate this to a new origin $t_1$? That is, what $K$ values can we use for the derived function $F$ so that $F_0(t) = f_0(t-t_1)$? I would expect it to work with $K_i=f_i(t_1)$.
When I define $f$ with $k$ values $[4,3,2,1]$, I find that $f(5) = [49,33,12,1]$. But if I define $F$ with those $K$ values, I get:
$$
\begin{align}
f_0(8) &=& 4 + 3{8 \choose 1} + 2{8 \choose 2} + 1{8 \choose 3} &= 140 \\
F_0(3) &=& 49 + 33{3 \choose 1} + 12{3 \choose 2} + 1{3 \choose 3} &= 185
\end{align}
$$
What's going wrong here?
Edit: I see now that I shouldn't have been trying to use the intermediate calculations for $f_i$ as shortcuts to $f_{i+1}$. When I calculate each $f_i(5)$ independently I get $K=[49,23,7,1]$, which gives $F_0(3)=140$, as expected.
Is there a name for this type of function? What kind of literature can I consult to find more like this?
Answer: They appear to be forms of Newton's interpolating polynomials, special cases of  Newton's Forward Difference Formula for equally-spaced points (viz., $x_i \in \{ 0,1,...,n \}$):
$$P(x) = \sum_{k=0}^n \binom{x}{k}\Delta^k f_0
$$
where $\Delta^k f_0$ denotes the forward differences corresponding to an interpolated function $f$.
Comment: Ah, I see. Declaring the rates of change $k_i$ axiomatically is equivalent to taking them from measurements.


MySQL Insert if not Updated

Question: I have three tables in MySQL
wi_indv_training(trn_id,ind_id,is_deleted)
wi_trn_org(trn_id,grp_id,is_deleted)
wi_indv_org(ind_id,grp_id,is_deleted)

Here, Multiple groups can be assigned to a training which is handled by wi_trn_org table.
Multiple Individuals can be assigned to many groups. And Many Individuals are involved in training. Here, we are provided individuals are already associated with groups.
Now while Inserting a group to training, individuals in that group must be associated with the training. There might already be the group and individuals(some or all) in the given training flagged with is_deleted=yes.
Now,While Inserting, I want to flag the deleted group and individual in training with is_deleted=no, if the data is already in respective tables else the data should be inserted as new data. For This I have implemented following approach which is very slow in performance;
foreach ($grp_id as $key => $value) {
    # code...
    $rs=$this->db->pdoQuery("SELECT * FROM wi_trn_org
                             WHERE trn_id='$trn_id'
                             AND grp_id='$value'")->results();
    if(count($rs)>0)
    {
        $this->db->pdoQuery("UPDATE wi_trn_org SET is_deleted=0
                            WHERE trn_id='$trn_id' AND grp_id='$value'");
        $grp_indv=$this->db->pdoQuery("SELECT ind_id
                                       FROM wi_indv_org
                                       WHERE grp_id='$value'
                                       AND is_deleted=0")->results();
        foreach ($grp_indv as $ke => $va)
        {
            $ind_id=$va['ind_id'];
            $rows=$this->db->pdoQuery("SELECT * FROM wi_indv_training
                                      WHERE trn_id='$trn_id'
                                      AND ind_id='$ind_id'")->results();
            if(count($rows)>0)
            {
                $this->db->pdoQuery("UPDATE wi_indv_training
                                     SET is_deleted=0,indv_source='Group'
                                     WHERE trn_id='$trn_id'
                                       AND ind_id='$ind_id'");
            }
            else
            {
                $this->db->insert("wi_indv_training",
                                   array("ind_id"=>$ind_id,
                                         "indv_source"=>'Group',
                                         "trn_id"=>$trn_id,
                                         "attendance"=>"yes",
                                         "active"=>"yes"));
            }
        }
    }
    else
    {
        $this->db->insert("wi_trn_org",array('trn_id'=>$trn_id,'grp_id'=>$value));
        $grp_indv=$this->db->pdoQuery("SELECT ind_id
                                       FROM wi_indv_org
                                       WHERE grp_id='$value'
                                       AND is_deleted=0")->results();
                foreach ($grp_indv as $ke => $va)
                {
                    $ind_id=$va['ind_id'];
                    $rows=$this->db->pdoQuery("SELECT *
                                               FROM wi_indv_training
                                               WHERE trn_id='$trn_id'
                                               AND ind_id='$ind_id'")->results();
                    if(count($rows)>0)
                    {
                        $this->db->pdoQuery("UPDATE wi_indv_training
                                             SET is_deleted=0,
                                                 indv_source='Group'
                                             WHERE trn_id='$trn_id'
                                               AND ind_id='$ind_id'");
                    }
                    else
                    {
                        $this->db->insert("wi_indv_training",
                                          array("ind_id"=>$ind_id,
                                                "indv_source"=>'Group',
                                                "trn_id"=>$trn_id,
                                                "attendance"=>"yes",
                                                "active"=>"yes"));
                    }
                }
            }
        }

Can anyone help regarding the best algorithm for performance enhancement?
Answer: The main problem here is you have a multiple queries in a loop:
foreach ($grp_id as $key => $value) {
            # code...
            $rs=$this->db->pdoQuery("SELECT * FROM wi_trn_org WHERE trn_id='$trn_id' AND grp_id='$value'")->results();
...
}

There is a performance hit for every SQL query, so if your loop ran 1,000 times, you've hit that performance bottleneck 1,001 times.
The way to solve this is to restructure your query inside the loop as part of the query outside the loop. The first query could be rewritten as a JOIN with the original query, and that may give you enough performance to solve your problem. If that isn't enough,  you could portnetialy collapse all of this into a Stored Routine.
Comment: Can you please elaborate with example. I tried with using INSERT INTO... ON DUPLICATE KEY UPDATE. but I couldn't get the result.
Comment: did you do what I suggested re: trying to get the first SQL SELECT statement out of the loop? Did that work? That would be simpler than trying to tackle the whole thing -- depending on your database size, that may be sufficient.
Comment: That reduced the overhead of multiple SELECT query in Loop which in turn reduces the time lag.
Answer: I researched and found a solution to my query i.e.
        $this->db->pdoQuery("UPDATE wi_trn_org SET is_deleted=1 WHERE trn_id='$trn_id'");
        $this->db->pdoQuery("UPDATE wi_indv_training SET is_deleted=1 WHERE trn_id='$trn_id'");
        $this->db->pdoQuery("INSERT INTO wi_trn_org( trn_id, grp_id ) SELECT $trn_id AS trn_id, grp_id FROM wi_group WHERE grp_id IN ($grp) AND grp_id NOT IN (SELECT DISTINCT (grp_id) FROM wi_trn_org)");
        $this->db->pdoQuery("UPDATE wi_trn_org SET is_deleted=0 WHERE trn_id='$trn_id' AND grp_id IN ($grp)");
        $this->db->pdoQuery("INSERT INTO wi_indv_training( ind_id, trn_id ) SELECT ind_id, $trn_id AS trn_id FROM wi_indv_org WHERE grp_id IN ($grp) AND ind_id NOT IN ( SELECT DISTINCT (ind_id) FROM wi_indv_training WHERE trn_id =$trn_id)");
        $this->db->pdoQuery("UPDATE wi_indv_training SET is_deleted=0,indv_source='Group',attendance='yes',active='yes' WHERE trn_id='$trn_id' AND ind_id IN (SELECT DISTINCT(ind_id) FROM wi_indv_org WHERE grp_id IN ($grp))");

This provided me the solution with the performance enhancement in some extent. Does this algorithm really enhances the Query Performance?
Comment: Let's see. You said you got some performance. You posted a few queries. The result is you got performance, according to what you wrote. Ergo, the only possible logical conclusion that the above code indeed improves performance. Question is, do you understand what's going on behind the scenes and how exactly databases are tuned for performance?
Comment: Yes I understood what's going on behind.

MySQL Insert if not Updated


Please explain the following code of a Python for

Question: I found this code on a Python Twitter Tools (ptt) example:
query = twitter.friends.ids(screen_name = username)
for n in range(0, len(query["ids"]), 100):
    ids = query["ids"][n:n+100]

https://github.com/ideoforms/python-twitter-examples/blob/master/twitter-friends.py
What my understanding tells me is that the range function on the for function works somewhat like this: range(initial_value, ending_value). What does the third argument mean stand for?
Also, apparently query is a string and it's accessing the ids feature inside the friends feature of twitter. What I don't get is what does the [n:n+100] do? I don't get the [] after the ['ids'] (perhaps it's a dict()?) nor the ":", what's that operand?
Also, my question looks pretty bad, this is my first post. Sorry about that, I'll work on that.
Comment: I would've started here `https://docs.python.org/2/library/functions.html#range` and try to find as much possible answer as I can before asking the questions.
Answer: The optional third argument to range is the step - in this case, the values in the range go up by 100 every iteration.
the [:] syntax is a slice. In this case, if query["ids"] is a string, it's a string slice, or substring. See the python tutorial on strings here: https://docs.python.org/2/tutorial/introduction.html#strings
Comment: ``string.get(string)`` results in a TypeError (string indices must be integers, not str).
Comment: Yes, but in the original post, the expression `query["ids"]` returns a string, which was then sliced into - `query` itself is not a string.
Answer: Firstly, the third params in the range is how much to increment, 100, 200, 300, etc. The [n:n+100] is used to get a subset. Starting at n and going to n+100, similarly like range.


Limit $\lim_{x\to \frac 1 {e^2}}\frac{2+5\log x}{2+\log x}$ with Taylor

Question: So let's assume $$\frac{2+5\log x}{2+\log x}$$
This function has a vertical asyntote in $x=1/e^2$ but i am able to solve that limit  with algebric tricks.
So i taught that i can expand the 2 function with Taylor in the centre $x=1/e^2$ and then calculate the limit in order to find out the real limit.
My problem is:wich basic function i should expand? Logx or Log(x+1) operating some substitution?
Thank's to everybody that will help me.
Comment: I really don't understand what you're asking - what do you mean "find the infinity"? The function has an asymptote and doesn't have a removable discontinuity. So $\lim_{x \to 1/e^2} |f(x)| = \infty$.
Comment: Why would Taylor expansion be relevant? The denominator vanishes at $1/e^2$ and the numerator does not.
Comment: But you *already* have a non-zero limit in the numerator; if you can compute $2 + \log 1/e^2$, then surely you can also compute $2 + 5 \log 1/e^2$.
Comment: To be completely honest, I think you're trying to use a tool that you don't understand yet. If you don't know how to relate the fact that $2 + 5 \log 1/e^2$ is non-zero to the asymptote, then it's very unlikely that you're going to use Taylor series correctly. Moreover, Taylor series are (quite frankly) not a relevant tool here.
Comment: I am not able to solve that limit with algebric methods so i would like to try with taylor expansion or any other possible way.
Comment: to find some different terms and assume that at the denominator will be something  that gives me 0 so after operating  n/0 i can find te infinity.
Comment: computing like this just give me real value,that's what i don't want
Comment: There are no tricks here, "$c/0=\pm\infty$" for $c\neq 0$ is limits 101. That's what vertical asymptote means. (Of course, the expression in quotation marks is formally completely incorrect, but gets the idea across).
Comment: The function doesn't have a finite *limit*, that is for certain. Could you perhaps be wondering about the *constant-order term* at that point? [WolframAlpha](http://www.wolframalpha.com/input/?i=expansion+of+(2+%2B+5+log+x)%2F(2+%2B+log+x)+at+x%3D1%2Fe%5E2) claims that it is one.
Answer: We don't need Taylor indeed by $x=\frac {e^y} {e^2}\to 1$ with $y\to 0$ we have
$$\lim_{x\to \frac 1 {e^2}}\frac{2+5\log x}{2+\log x}=\lim_{y\to 0}\frac{2+5y-10}{2+y-2}=\lim_{y\to 0}\frac{-8+5 y}{ y}=\pm \infty$$
therefore the limit doesn't exist.
Note that by Taylor's expansion at $x=\frac1{e^2}$ we obtain

$\log x = -2+xe^2-1+o(xe^2-1)$

$$\frac{2+5\log x}{2+\log x}=\frac{-8+5(xe^2-1)+ o(xe^2-1)}{xe^2-1+o(xe^2-1)}=\frac{\frac{-8}{xe^2-1}+5+ o(1)}{1+o(1)}$$
therefore the limit depends upon
$$\lim_{x\to \frac 1 {e^2}} \frac{-8}{xe^2-1}$$
which leads to the same result in a more complicated and convoluted way.
Comment: @AndreyKudinov Taylor's expansion leads to the same kind of expression but in a more complicated way, You should become practical with substitution, standard limits, squeeze theorem, etc. before to apply Taylor. Using Taylor without this basic knowledge is just useless and dangerous.
Comment: @AndreyKudinov Indeed it is not effective and useful in that case, we know that the numerator tends to a negative value and the denominator tends to zero, therefore the only fact to clarify is how the denominatorgoes to zero. A simple substitution helps to calrify that the limit doesn't esist. Taylor is the last thing to use here.
Comment: Ok this algebric way seems easy but i am not that practical with subtsitution so would it work using taylor expansion?
Comment: Thank you for the tip..Anyway,operating the taylor method on this limit  would just require a lot of time or this is not a basic expansion that i can find on  the Mac Lurin tables of expansion?
Comment: @AndreyKudinov To find the expansion let $x=(1/e^2)(1+y)$with $y\to 0$ then $\log x = -2+\log(1+y)=-2+y+o(y)$ and $y =xe^2-1$.
Comment: Thank you for the patience.
Comment: @AndreyKudinov You are welcome it is a plesure for me if I can transmit some of my experience on that! Bye
Comment: You might also want to introduce Laurent series to the OP.
Comment: @Szeto I believe it was useful in that case to show something in more detail to convince the OP that Taylor is not the proper tool here and that a good knowledge of the basic tools is always a foundamental skill.
Answer: There is no need for Taylor expansion, nor for substitutions.
Since
$$
\lim_{x\to e^{-2}}(2+\log x)=0
$$
with $2+\log x>0$ if $x>e^{-2}$, $2+\log x<0$ if $x<e^{-2}$, and
$$
\lim_{x\to e^{-2}}(2+5\log x)=-8
$$
we have
$$
\lim_{x\to e^{-2}-}\frac{2+5\log x}{2+\log x}=\infty
\qquad
\lim_{x\to e^{-2}+}\frac{2+5\log x}{2+\log x}=-\infty
$$
Answer: $$L=\lim_{x\to e^{-2}}\frac{2+5\ln(x)}{2+\ln(x)}$$
If we look at the top and bottom individually we see that:
$$\lim_{x\to e^{-2}}\left[2+5\ln(x)\right]=2+5(-2)=-8$$
$$\lim_{x\to e^{-2}}\left[2+\ln(x)\right]=0$$
so at first glance it would appear to diverge to $\pm\infty$
if we look a this closer we can rewrite it as:
$$\lim_{x\to e^{-2}}\left[\frac{2+5\ln(x)+8}{2+\ln(x)}-\frac{8}{2+\ln(x)}\right]$$
if we use a substitution $y=\ln(x)$ this becomes:
$$\lim_{y\to -2}\left[\frac{10+5y}{2+y}\right]-\lim_{y\to -2}\left[\frac{8}{2+y}\right]=5-\lim_{y\to -2}\left[\frac{8}{2+y}\right]$$
and this second limit is clearly divergent to $\pm\infty$ so the limit does not exist


Prove $xf(x) - \int_{0}^{x} f(t) \,dt = \int_{f(0)}^{f(x)} g(t) \,dt, g(x) = f^{-1}(x), \forall f(x)$?

Question: I have proved it for all functions of the form $f(x) = x^{a}, \forall a, x,$ but I am not sure if it is true for all functions.

Proposition
$$xf(x) - \int_{0}^{x} f(t) \,dt = \int_{f(0)}^{f(x)} g(t) \,dt, g(x) = f^{-1}(x), \forall f(x)$$

Proof
Let $f(x) = x^{a}, a \neq -1$, then:
$f(x)x = x^{a + 1}$,
$F(x) = \frac{1}{a + 1} x^{a + 1}$,
$F(0) = 0$, and
$f(x)x - \int_{0}^{x} f(t) \,dt = \color{lime}{\frac{a}{a + 1} x^{a + 1}}$.
Since $f(x) = x^{a}, g(x) = x^{\frac{1}{a}}$,
$G(x) = \frac{a}{a + 1} x^{\frac{a + 1}{a}}$,
$G(f(x)) = \frac{a}{a + 1} x^{a + 1}$,
$G(f(0)) = 0$, and
$G(f(x)) - G(f(0)) = \color{lime}{\frac{a}{a + 1} x^{a + 1}}$.

Let $f(x) = x^{a}, a = -1$, then:
$f(x)x = 1$,
$F(x) = \ln|x|$,
$F(0) = -\infty$, and
$f(x)x - \int_{0}^{x} f(t) \,dt = \color{lime}{\infty}$.
Since $f(x) = x^{a}, g(x) = x^{a}$,
$G(x) = \ln|x|$,
$G(f(x)) = -\ln|x|$,
$G(f(0)) = -\infty$, and
$G(f(x)) - G(f(0)) = \color{lime}{\infty}$.

I believe that there may be a few exceptions to this, but I am not sure.
I am not sure as to whether or not this formula has been derived / used before.
If you would like to see how I derived it, please, ask me!
Comment: @TimRaczkowski I know that.
Comment: Sorry, I misread it.
Comment: @TimRaczkowski It was not your fault! :-)
Comment: See tris previous [question](http://math.stackexchange.com/questions/1115222/show-rigorously-that-the-sum-of-integrals-of-f-and-of-its-inverse-is-bfb-af/1115317#1115317).
Comment: @JuliánAguirre Thank you for sharing this with us.
Answer: You can use the Fundamental Theorem of Calculus (Leibniz's Rule) to show that the derivatives of the LHS and RHS wrt $x$ are identical ($=xf'(x)$).
Which means that LHS = RHS $+ c$.
All you need to do now is to show $c=0$ and that's trivial by taking $x=0$.
Comment: Ah, nice. Thank you very much. Leibniz' rule seems to pop up in many areas of calculus. I should really check it out. :-)
Answer: Yes, it's true for all invertible functions. Think of the integrals in terms of areas under graphs; notice that the areas from the two integrals combine to form an $x$ by $f(x)$ rectangle. (This seems similar to integration by parts; maybe there's a proof using it)
Comment: Yes, that is how I derived the formula. Ah, well, that is great! Thank you for your help.
Comment: Is this a well-known formula? I judge by the speed of your response that it is.
Comment: No, it's not well known; at least, not that I know of.


converting a known proportion to estimate probability per day

Question: I need some help understanding how to calculate the rate of occurrence per day or probability per day. If we know that almost 5% of the patients develop an infection in general, how can we use this information to estimate the rate of occurrence or probability per day?
Assuming I have N number of patient per day and I want to randomly convert some of them to infected based on given probability p. The way I am doing is that I am calling the below function per day and in this function, I am looping over all the patients and for every patient, I am doing a Bernoulli trial with the probability p to see if patient got infected or not. I am using Python.
trial = self.get_bernoulli_trial(p)

def get_bernoulli_trial(self, p):
        return np.random.binomial(1, p)

If trial returns 1, I make that patient infected otherwise do nothing.
So the question is, is this a right way of doing this and how to estimate p per day for Bernoulli trial from the knowledge that 5% patients on average develop infection. If I use p = 0.05, would that be correct?
Any help will be appreciated.
Comment: If you know that p=5% what is there to estimate?
Comment: Question is would it be correct to use p = 5% per day from the estimate that on average 5% gets infected. And I do not know anything about the time period of the general estimate. Or may be I am just confused and there is nothing to estimate.
Comment: I am just confused in converting probability from an unknown time period to probability per day.
Answer: With your description, and code, you seem to assume that the infections are independent Bernoulli trials with same probability of infection $p$. In such case, since they are independent, it does not matter if you group them in days, or look at the global incidence, the probability remains the same. For each of the patients, independently, it is $p=0.05$ by definition.
Of course, this does not (and probably isn't) a realistic assumption. I guess that it could be possible that such events are clustered at time, for example, if there is an influenza outbrake, the probability of observing such patient gets large, but few months later it gets much smaller. However, to estimate such changes over time, you would need to know much more details, and have adequate temporal data. You cannot de-aggregate the single total number to learn about such variability.
Answer: $Binom(1, p)$ is not an appropriate function to use for generation, as the samples are drawn without replacement from a fixed pool of patients.
If you know that 5% of however many patients you have will be infected and you are interested in simulating whether any individual patient gets sick, you can use
$$p(sick) = Hypergeom(Total\ patients,Expected\ sick,Already\ added)$$
as the Bernoulli probability where $Expected$ is a sample from $Binom(Total, p)$ (where p=0.05 in this case), and $Added$ is the number of patients you rolled for so far plus one (you'd need to keep track of every patient that rolled a success as you set them as infected).
So for example, if the first patient out of 100 would use $Hypergeom(100, 5, 1)$, the second $Hypergeom(100, 5, 2)$ and so forth.
If you're not quite certain of the fraction, you can go one further back, and draw the $p$ in Binominal from a Beta distribution with the count of the infected patients and total patient count as parameters.
Comment: Why would it be a "fixed pool of patients"?! It is not that there is a fixed pool of people, some sick, some not, and you are drawing some of them from the pool. Any of the people can randomly get ill. Moreover a person may one day be healthy and ill another day, this is a direct form of "replacement". This scenario is closer to sampling "with replacement". I see no reason for assuming hypergeometric distribution unless you could give more detailed rationale for it.