Block a machine from accessing the internet

Question: After some confirmation that I have thinking right in this scenario.
We have a number of wired and wireless machines which presently have direct internet access. I also have a Linux (Ubuntu) server which is used as a file server for the network.
Essentially I would like to be able to turn internet access on and off for machines.
My plan is to block these machines by MAC address at the router. I would then set up a proxy server on the Linux box (ie Squid) so that the machines I wish to restrict can access the internet via the proxy. As I can adjust access via ACLs in squid, I would be able to switch on or off a machines access to the internet without having to further adjust the router's MAC rules. And of course I could go further and create a few scripts to assist with this admin task.
Does this seem sound and have I over looked anything? Any help greatly appreciated.
Simon.
Comment: Thanks for the reply .. hard for me to accept the answer as its .. well not an "answer" :) .. greatly appreciated :)
Comment: I think you have covered all bases. Don't forget to also have a MAC allow list in your squid config, to prevent people bypassing your MAC-based filtering.
Answer: the best choose for this system is mikrotik router os:
it support all thing that you want,and it is easy to use also.
it have an accounting server + proxy server and ...
Comment: The OP already has a linux machine he wants to run squid on.
Comment: Thanks again .. I'm not after info how to do any of the things I've mentioned .. I'm looking for confirmation that the concept is sound and that I've not missed anything. I've researched the 'nuts and bolts'
Comment: mikrotik router os is a Linux OP.you can install it on a computer,with this os you do not need to install squid,it can block user by max ip address,it have a lot of figure and easy to use,u can install it in 2 minutes
Comment: follow http://www.linuxquestions.org/questions/linux-newbie-8/squid-acl-block-internet-by-ip-address-644884/
Comment: He wants to block users by their MAC addresses.
Comment: Thanks for the reply. But I'm really just interested in knowing if this scenario works as opposed to any other way of doing it. There are other considerations that have led me to go this way that I've not mentioned for brevity. So as I say more after a confirmation that I've not missed anything.
Comment: try this:http://www.cyberciti.biz/faq/howto-linux-unix-squid-mac-address-filtering/
Answer: I can think of exactly one point: a machine could change the MAC address of its network interface. In this case, your filtering could be circumvented...
But I can see nothing you could do against that, so you are as close to being safe as possible.
Answer: If you want to have control over HTTP traffic, then a proxy server like squid makes sense as it provides mechanisms to block specific IPs/domains/URLs, etc..
If you want to block Internet access for specific machines, this can be done several ways:

You can block specific MAC addresses on your router, but you should know that any machine can change its MAC address. For example, if a user has admin privileges on a machine, he will be able to change the MAC address.
You can block specific IP addresses on your router. Again, any admin user can change the IP address of a machine and thus your access list becomes useless.

The solution to such problems is to either prevent admin access for these machines' users (no MAC/IP can be changed), or have something like port security on your managed switch if available.
Comment: Hi. Yes the users concerned do not have admin access, so will be unable to change MAC address or IP. Thanks for the reply
Comment: Thanks to all that replied .. marking this as the answer for the thoroughness as such. Once again I appreciate all the efforts of those who replied.
Answer: Can you tell us why you picked MAC addresses as the basis for filtering?  As stated by glglgl, a MAC can be changed by a user.
Is it a necessity to your planning that you're able to define rules for arbitrary individual hosts on a given network?  Where I'm going is, if you're planning to role out a new config to a group at a time, IP subnets would be the way to go.
Potentially, a user could change their IP, or even spoof it, but only within their own broadcast domain.  If they tried to set/spoof to a different subnet IP, the network simply wouldn't route packets back to the host machine in that case, which would not help them circumvent your blocking scheme.
Answer: You could use wired and wireless EAP to separate your machines into two VLAN's, with and w/o internet access.


AWS - Direct Connect to Direct Connect

Question: Is it possible to create a point to point Direct Connect between AWS regions?
If I create a Direct Connection with the same Service Provider for:

(ap-northeast-1) in Equinix TY2, TY6 - TY8, Tokyo
(eu-west-2) in Telehouse West, London, GBR

Am I able to use these two ports on EC2 instances located in ap-northeast-1 and eu-west-2 to communicate directly?
If it is possible could you provide step by step instructions for setting this up?
Thanks.
Answer: Simply create a Direct Connect Gateway if you're wanting to connect to multiple regions to your on premise.
If you simply want to connect the 2 VPCs together with no need to connect to on premise then use a VPC Peer between your VPCs. This will use AWS backbone network to connect and transfer data between the 2 VPCs without any internet bound transit.
Comment: Hi, its probably going to be very close in actual speed and costly to have Direct Connect for this problem. I'd suggest contacting AWS support and talking through your current setup.
Comment: I want to use a service provider that shows to have low latency between tokyo and london (around 170ms) compared to the AWS backbone which is 220ms. Thats why I want to use Direct Connect
Comment: so this setup is possible? I would like to test the actual latency myself. I understand the cost involved, but if the latency is under 200ms its worth it for my use case. If you could outline the basic steps, I will accept your answer.
Comment: Hi, this would not be possible. Direct Connect requires the configuration of an on-premise router. AWS uses a managed router per subnet. I'd really suggest contacting AWS support

AWS - Direct Connect to Direct Connect


Google maps stop showing points v3

Question: Hi  i have trouble to find what  case a problem to showing  the way point on goggle maps as  there  were no changes in the code  for  a wile. way point stop showing  about last week. im not familiar with  google api

var map = null;
var markerArray = []; //create a global array to store markers
var myPoints =
[ [52.664167, -8.509825,' HQ','favicon.ico'] ,[52.836346, -6.913117,'point 1','http://maps.google.com/mapfiles/ms/icons/red.png ' ],[52.836202, -6.912101,'point2','http://maps.google.com/mapfiles/ms/icons/red.png ' ]];
function initialize() {
    var myOptions = {
        zoom:7,
        center: new google.maps.LatLng(53.112, -7.448),
        mapTypeControl: true,
        mapTypeControlOptions: {
            style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
        },
        navigationControl: true,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map"), myOptions);

    var mcOptions = {
        gridSize: 30,
        maxZoom: 15
    };
    var mc = new MarkerClusterer(map, [], mcOptions);

    google.maps.event.addListener(map, 'click', function() {
        infowindow.close();
    });

    // Add markers to the map
    // Set up markers based on the number of elements within the myPoints array
    for(var i=0; i<myPoints.length; i++){
        createMarker(new google.maps.LatLng(myPoints[i][0], myPoints[i][1]), myPoints[i][2], myPoints[i][3]);

    }

    mc.addMarkers(markerArray , true);
}

var infowindow = new google.maps.InfoWindow({
    size: new google.maps.Size(150, 50)
});

function createMarker(latlng, html, icons) {
    var contentString = html;
  var links = icons;
    var marker = new google.maps.Marker({
        position: latlng,
        map: map,
  icon: links ,

        zIndex: Math.round(latlng.lat() * -100000) << 5
    });

   // marker.setAnimation(google.maps.Animation.BOUNCE);

    google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent(contentString);
        infowindow.open(map, marker);
    });

    markerArray.push(marker); //push local var marker into global array
}

window.onload = initialize;

Any suggestions?
Answer: It seems that you are using MarkerClusterer which is a part of google maps utility library, which was moved recently. Somebody probably referenced the library straight from code.google.com/svn/... in your project, where it's not available anymore and that's why it's broken. You need to find all libraries which are referenced from https://google-maps-utility-library-v3.googlecode.com/svn and replace then with your own links.
Check this SO question, user had very similar issues to yours.
Also! check this question and answers on SO!!, there are users who experienced issues with the same library, to get more information. Read all answers, as replacing https://google-maps-utility-library-v3.googlecode.com/svn with https://rawgit.com/googlemaps/... is not a correct solution, you should download the library assets into your project and reference them from there or use CDN (but not git!).

Google maps stop showing points v3


Como fazer o telerik RadHtmlChart ser responsivo?

Question: Possuo 3 gráficos RadHtmlChart do Telerik em uma página, estou dividindo essa página em 3 para que eles fiquem sempre exibidos, sem a necessidade de rolar um scroll para visualizá-los.
Preciso que ao redimensionar a página, o RadHtmlChart se adeque a este redimensionamento.
É possível fazer isso ?
Tentei o seguinte:
 window.onresize = function () {
         $find("<%=chtPESO.ClientID%>").get_kendoWidget().resize();
         $find("<%=chtPARTIC.ClientID%>").get_kendoWidget().resize();
         $find("<%=chtVDA.ClientID%>").get_kendoWidget().resize();
 }

Porém sem nenhum resultado.
Tentei também utilizando o @media screen, porém sem sucesso também.
Como posso fazer isso ?
Answer: Você precisa usar as funções set_width e set_height para fazer o redimensionamento do gráfico.
Você só precisa calcular as dimensões adequadas para preencher toda a tela.

function adjustChartSize(chartClientId, width, height) {
   var chart = $find(chartClientId);

   if (chart) {
      chart.set_width(width);
      chart.set_height(height);
   }
}

window.onresize = function () {
   adjustChartSize("<%=chtPESO.ClientID%>"   , 300, 400);
   adjustChartSize("<%=chtPARTIC.ClientID%>" , 300, 400);
   adjustChartSize("<%=chtVDA.ClientID%>"    , 300, 400);
}


Will manually setting the expire timestamp in the boost_cache table actually expire the cache for a node?

Question: I am trying to get myself acquainted with Boost running on my Drupal 6 website. I know that boost has a boost_cache table where it stores the expires value for each node.
Now I want to know whether that if I set the value in the expire column manually to a preferred value, Boost will recognize it and expire the cached version of the node on the date in the timestamp and there is no other table related to Boost caching where I should make the changes.
Comment: Presumably you've tried it, did anything happen to suggest that manually setting the value _doesn't_ expire the cache straight away? Or are you worried about edge cases/other modules getting involved/etc?
Answer: You might prefer to use
boost_expire_node
which will actively flush the node from boost static html cache
It could be scheduled, if needed ( e.g a hook_cron_queue_info implementation worker callback ) or done as an immediate expiry


is it possible to read and write with the same file descriptor in C

Question: I am trying to write to a file and display the output of the thing i wrote with another process. The code i come up with:
void readLine (int fd, char *str) {
    int n;
    do {
        n = read (fd, str, 1);
    } while (*str++ != '\0');
}

int main(int argc,char ** argv){
    int fd=open("sharedFile",O_CREAT|O_RDWR|O_TRUNC,0600);
    if(fork()==0){
        char buf[1000];
        while(1) {
            readLine(fd,buf);
            printf("%s\n",buf);
        }
    }else{
        while(1){
             sleep(1);
             write(fd,"abcd",strlen("abcd")+1);
        }
    }
}

the output i want (each result spaced from the other with a period of one second):
abcd
abcd
abcd
....

Unfortunately this code doesn't work, it seems that the child process (the reader of the file "sharedFile") reads junk from the file because somehow it reads values even when the file is empty.
When trying to debug the code, readLine function never reads the written file correctly,it always reads 0 bytes.
Can someone help?
Comment: Please check carefully from the operating system programming manuals the meaning of the various possible return values from `read`.
Answer: First of all, when a file descriptor becomes shared after forking, both the parent and child are pointing to the same open file description, which means in particular that they share the same file position. This is explained in the fork() man page.
So whenever the parent writes, the position is updated to the end of the file, and thus the child is always attempting to read at the end of the file, where there's no data.  That's why read() returns 0, just as normal when you hit the end of a file.
(When this happens, you should not attempt to do anything with the data in the buffer.  It's not that you're "reading junk", it's that you're not reading anything but are then pretending that whatever junk was in the buffer is what you just read.  In particular your code utterly disregards the return value from read(), which is how you're supposed to tell what you actually read.)
If you want the child to have an independent file position, then the child needs to open() the file separately for itself and get a new fd pointing to a new file description.
But still, when the child has read all the data that's currently in the file, read() will again return 0; it won't wait around for the parent to write some more.  The fact that some other process has a file open for writing don't affect the semantics of read() on a regular file.
So what you'll need to do instead is that when read() returns 0, you manually sleep for a while and then try again.  When there's more data in the file, read() will return a positive number, and you can then process the data you read. Or, there are more elegant but more complicated approaches using system-specific APIs like Linux's inotify, which can sleep until a file's contents change.  You may be familiar with  tail -f, which uses some combination of these approaches on different systems.
Another dangerous bug is that if someone else writes text to the file that doesn't contain a null byte where expected, your child will read more data than the buffer can fit, thus overrunning it.  This can be an exploitable security vulnerability.
Here is a version of the code that fixes these bugs and works for me:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

void readLine (int fd, char *str, size_t max) {
    size_t pos = 0;
    while (pos < max) {
        ssize_t n = read(fd, str + pos, 1);
        if (n == 0) {
            sleep(1);
        } else if (n == 1) {
            if (str[pos] == '\0') {
                return;
            }
            pos++;
        } else {
            perror("read() failure");
            exit(2);
        }
    }
    fprintf(stderr, "Didn't receive null terminator in time\n");
    exit(2);
}

int main(int argc, char ** argv){
    int fd=open("sharedFile", O_CREAT|O_RDWR|O_TRUNC, 0600);
    if (fd < 0) {
        perror("parent opening sharedFile");
        exit(2);
    }
    pid_t pid = fork();
    if (pid == 0){
        int newfd = open("sharedFile", O_RDONLY);
        if (newfd < 0) {
            perror("child opening sharedFile");
            exit(2);
        }
        char buf[1000];
        while (1) {
            readLine(newfd, buf, 1000);
            printf("%s\n",buf);
        }
    } else if (pid > 0) {
        while (1){
            sleep(1);
            write(fd,"abcd",strlen("abcd")+1);
        }
    } else {
        perror("fork");
        exit(2);
    }
    return 0;
}


How to find the inverse function of $f(x)=\sin^2\left(\frac{2x+1}{3}\right)$ and find its domain.

Question: $f(x)=\sin^2\left(\frac{2x+1}{3}\right)$ which is restricted on $-\frac{3\pi+1}{2}\le x< -\frac{3\pi+2}{4}$
I know I have to switch the $f(x)$ and the $y$:
$x=\sin^2\left(\frac{2f^{-1}(x)+1}{3}\right) \to \arcsin^2(x)=\frac{2f^{-1}(x)+1}{3} \to f^{-1}=\frac{3\arcsin^2(x)-1}{2}$
To find the domain:
$$-\frac{3\pi+1}{2}\le \frac{2x+1}{3}< -\frac{3\pi+2}{4}$$
$$-\frac{9\pi+3}{2}\le 2x+1< -\frac{9\pi+6}{4}$$
$$-\frac{9\pi+5}{2}\le 2x< -\frac{9\pi+10}{4}$$
$$-\frac{9\pi+5}{4}\le x< -\frac{9\pi+10}{8}$$
The inverse function is $f^{-1}=\frac{3\arcsin^2(x)-1}{2}$ and its domain is $-\frac{9\pi+5}{4}\le x< -\frac{9\pi+10}{8}$
But I am slightly confused since Desmos will not map it correctly. I'm asking if this is the correct solution or I went wrong somewhere.
Thanks
Comment: Hint : Use $1-\cos 2\theta = 2\sin^2 \theta$
Comment: Hint: The range of $f(x)$ is the same as the domain of the inverse $f^{-1}(x)$.
Comment: $W = \sin^2 m$ most certainly does *not* mean $\arcsin^2 W = m$.  So begin with you must "unwrap", not just put things back in the same package..   If $W = \sin^2 m$ then $\sqrt W =\sqrt \sin^2 m = \pm sin m$ and $\sin m = \pm \sqrt W$.  (and it's either $\sqrt W$ or $-\sqrt W$.  You *do* have to figure out whether $\sin m$ is positive or not. .....
Comment: And then you have $\sin m = \pm \sqrt W$.  That means $\arcsin (\sin m) = \arcsin (\sqrt W)$ but we do not *know* that $\arcsin (\sin m) = m$.  If $\sin m = K$ there are an infinite number of $\theta$s so that $\sin \theta = K$ and $\arcsin (K)$ is only one of them.  And $m$ is only one of them.  It might be that $\arcsin (K)$ and $m$ are the same value so that $\arcsin K = m$ but it could just as likely be that $\arcsin K$ and $m$ are two *different* values that $\sin(\arcsin K) = \sin m = k$
Answer: $$\text{Let $y^2 = f(x)=\sin^2\left(\frac{2x+1}{3}\right)$}$$
So $y \in [-1,1]$ and
$y=\sin\left(\dfrac{2x+1}{3}\right)$
where $\dfrac{2x+1}{3} = 2n\pi + \theta $ and $-\pi \le \theta \le \pi$
for some $n \in \mathbb Z$.
We compute
\begin{align}
   \theta &= \arcsin y \\
   \dfrac{2x+1}{3} &= 2n \pi + \arcsin y \\
   2x &= 6n \pi - 1 + 3 \arcsin y \\
   x &= 3n \pi - \dfrac 12 + \dfrac 32 \arcsin y \\
\end{align}
Answer: a)
The function $f$ is not injective, therefore not bijective and cannot have an inverse therefore.
But you can calculate the individual branches (i. e. partial inverses) of $f$.
b)
You didn't rearrange your equation correctly.
Let $y$ denote $f^{-1}(x)$.
$$\sin^2\left(\frac{2y+1}{3}\right)=x$$
$$\left(\sin\left(\frac{2y+1}{3}\right)\right)^2=x$$
$$\sin\left(\frac{2y+1}{3}\right)=\pm\sqrt{x}$$
$$\frac{2y+1}{3}=\arcsin(\pm\sqrt{x})$$
$$\frac{2y+1}{3}=\pm\arcsin(\sqrt{x})$$
$$y=\pm\frac{3}{2}\arcsin(\sqrt{x})-\frac{1}{2}$$
Comment: thank you, I used your answer in my answer to make a full complete answer to OP's question
Answer: Look at the plot below (first picture). The function can be inverted only in the interval where it is bijective.
Take the derivative
$$f'(x)=\frac{4}{3} \sin \left(\frac{1}{3} (2 x+1)\right) \cos \left(\frac{1}{3} (2 x+1)\right)=\frac{2}{3} \sin \left(\frac{2}{3} (2 x+1)\right)$$
$$f'(x)=0\to  \sin \left(\frac{2}{3} (2 x+1)\right)=0$$
In the interval we are interested in, this happens when
$$\frac{2}{3} (2 x+1)=0;\;\frac{2}{3} (2 x+1)=\pi$$
which gives $$x_1=-\frac12;\;x_2=\frac{3 \pi }{4}-\frac{1}{2}$$
We will invert $f(x)$ in the interval $[x_1,x_2]$.
Set $y=f(x)$ and get
$$\sin ^2\left(\frac{1}{3} (2 x+1)\right)=y\to  \sin \left(\frac{1}{3} (2 x+1)\right)=\sqrt{y}$$
then
$$\frac{1}{3} (2 x+1)=\arcsin\sqrt y\to 2x+1=3\arcsin\sqrt y\to x=\frac{1}{2} \left(3 \arcsin\left(\sqrt{y}\right)-1\right)$$
Only now we swap $x$ and $y$ to get
$$f^{-1}(x)=\frac{1}{2} \left(3 \arcsin\left(\sqrt{x}\right)-1\right)$$
The domain of $f^{-1}(x)$ is the range of $f(x)$ that is $[0,1]$.
Look at the second picture below. The inverse function graph is the symmetric of the function $f(x)$ wrt the line $y=x$.

$$...$$
Answer: As IV_ said, your inverse function should be $y=\pm\frac{3}{2}\arcsin(\sqrt{x})-\frac{1}{2}$.
Your original function $ f(x)=\sin^2\left(\frac{2x+1}{3}\right)$ is equal to a sine function squared. If you graph f(x), you can see that its biggest value is $1$, and it achieves its smallest value at $x=(-3\pi+1)/2$ because the domain is restricted. Calculating the smallest $y$-value, we get $f((-3\pi+1)/2)$, which is approximately $0.382$ according to the graph. Thus, the range of $f(x)$ is $[0.382,1]$.
So, your function maps $x$-values in its domain to the range $[0.382,1]$. An inverse function reverses this process, so it maps $[0.382,1]$ back to the domain of the original function. That means that the domain of your inverse function is $[0.382,1]$.
Notice how we didn't have to find the domain of the original function to find the domain of the inverse?


Why is this a rational lattice?

Question: I've usually seen a rational lattice $\Gamma \subset \mathbb{R}^n$ defined like this:
$$\displaystyle \Gamma = \{Ax : x \in \mathbb{Z}^n \},$$
where $A \in \mathbb{Q}^{n \times n}$ is a matrix of full rank. However, some texts call $(2 \pi \mathbb{Z})^n$ a rational lattice, which does not seem to fit this definition. Is there an alternative definition of a rational lattice, or have I misunderstood the definition I've posted above? And if $(2\pi\mathbb{Z})^n$ is a rational lattice, then what distinguishes it from an irrational lattice?
Answer: This is likely an "alternate" definition, but really it's effectively just a notational change.  Instead of writing $1$, $2$, $3$, you write $2\pi$, $4\pi$, $6\pi$.  In other words, $(2\pi\mathbb{Z})^n \cong \mathbb{Z}^n$.  Essentially all you're doing is allowing $A$ to be scaled by an arbitrary non-zero real factor.  Presumably this is done to make some other equations look nicer, e.g. complex exponentials.  An "irrational lattice" would be one where there are points that aren't a linear combination with rational weights of the basis vectors.  Concretely, for the $\mathbb{Z}^2$ case, we have some vector $v_1$ that we identify with $(1,0)$ and another vector $v_2$ that we identify with $(0,1)$ and we want to know that every other point in the lattice is $pv_1 + qv_2$ for some pair of rationals $p$ and $q$.  That $v_1$ and $v_2$ have irrational coordinates in some other basis isn't really important.  If we choose a coordinate system where $v_1$ and $v_2$ are written $(2\pi,0)$ and $(0,2\pi)$ respectively, that's just changed how we talk about it but not the actual situation/interrelationships.

Why is this a rational lattice?


Razorpay Subscriptions API item price can not be set

Question: I am using RazorPay API to create Subscription plans. Now, I am facing this error :
When I add item amount in number it sends me error : Amount should be atleast 0.10 USD , https://prnt.sc/Gh9wWm363szh
When I add item amount in decimal it sends me error : Amount should be integer : https://prnt.sc/x6toKYCvF3vj
code :

try{
  const data = await razorpay.items.create(body);
  res.status(200).send(data);
}catch(err){
  console.log(err);
  res.status(400).send({
    message:err?.error?.description
  });
}

any help.
screenshots from API calls .
Answer: most payment apis use cents instead of dollers.
so if you want 5 doller then put 500 as the value
value = dollar_value * 100
your api min is 10 which is $0.10
review the documentation


How to Sort a DataGridTextColumn which uses a MultiBinding Converter

Question: I use a multibinding with a value converter to provide the visual display of a collection of items in my DataContext. Here is a snippet of the XAML;
<DataGrid.Columns>
    <DataGridTextColumn x:Name="Column1"
                        SortMemberPath="{Binding Path=SomeDataModelProperty}">
    <DataGridTextColumn.Binding>
        <MultiBinding Converter="{StaticResource MyCustomConverter}">
            <Binding Path="SomeDataModelProperty" />
            <Binding RelativeSource="{RelativeSource Self}" Path="ActualWidth" />

        </MultiBinding>
    </DataGridTextColumn.Binding>
</DataGridTextColumn>

The binding on the SortMemberPath is such that I can sort by the property in my DataContext. However, I get an error on the output window
Cannot find governing FrameworkElement or FrameworkContentElement for target element.

Googling this issue yields a result using DXGrid by DevExpress, but not one using the standard WPF data grid. Does anyone know the correct way to provide the sorting to the data grid column?
Answer: I figured it out, thanks to this article. In the end it is quite simple;
<DataGridTextColumn x:Name="Column1"
                    SortMemberPath="SomeDataModelProperty">

i.e. don't use a binding, just specify the property name directly.
Comment: This helped me a bit. However, I'd need to sort with respect to the value rendered by the converter and not a fix path in the data (my converter gives me one of two different properties depending on a third one). Suggestions?
Comment: It seems like the `SortMemberPath` is just a string and the grid control itself will resolve this to a column name, so binding is not the way forward. The only thing I can suggest is to bind the grid control to a pre-sorted collection, such as via a `CollectionViewSource` or something.


Not able to get elements using xpath in casperjs

Question: I am using casperjs and following is the code
var casper = require('casper').create();
var x = require('casper').selectXPath;

casper.start('http://google.co.in/', function() {
    // search for 'casperjs' from google form
    this.test.assertExists(x('//*[@type="text"]'), 'the element exists');

});
casper.run(function() {
    // echo results in some pretty fashion
    this.echo('').exit();
});

it wasn't able to find any element with attribute type as text though there are plenty.
This is the output I get
FAIL the element exists
#    type: assertExists
#    subject: false
#    selector: {"type":"xpath","path":"//*[@type=\"text\"]"}

Answer: Google has been known to not be bot-friendly. In order for this to work, you must set the UserAgent.
This was tested under CasperJS-1.0.0 and PhantomJS-1.8.0
var casper = require('casper').create({
  pageSettings: {
    userAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.21 (KHTML, like Gecko) Chrome/25.0.1349.2 Safari/537.21'
  }
});
var x = require('casper').selectXPath;

casper.start('http://google.co.in/', function() {
  // search for 'casperjs' from google form
  this.test.assertExists(x('//*[@type="text"]'), 'the element exists');
  this.test.assertExists({
    type: 'xpath',
    path: '//*[@type="text"]'
  }, 'the element exists');
});

casper.run(function() {
  // echo results in some pretty fashion
  this.echo('').exit();
});

You can visit http://whatsmyuseragent.com/ to see what your current UserAgent is.
Update: Removed CasperJS-1.0.0 code in favor of backwards-compatible code.
Answer: Similar answer as hexid, but to set the user agent, you need to start casper first (not the other way around):
var casper = require('casper').create({verbose: true});
var x = require('casper').selectXPath;

casper.start();
casper.userAgent('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.21 (KHTML, like Gecko) Chrome/25.0.1349.2 Safari/537.21');

casper.thenOpen('http://google.co.in/', function() {
    // search for 'casperjs' from google form
    this.test.assertExists(x('//input[@type="text"]'), 'the element exists');
});

casper.run(function() {
    // echo results in some pretty fashion
    this.echo('').exit();
});

Tested with PhantomJs 1.7.0 and Casper 1.0.0-RC4
Comment: As of CasperJS-1.0.0, you can set the UserAgent before casper.start()
Comment: Ah ok, downloaded CasperJs via Homebrew today. Seems not to be up to date then.


Erro no Laravel 4.2 - SQLSTATE[HY000] [2054] Server sent charset unknown to the client. Please, report to the developers

Question: Estou com problemas em uma aplicação web utilizando Laravel 4.2 a mesma funcionava em um ambiente Windows 7 com IIS + PHP 5.6 porém tive que trocar de máquina e assim criar um novo ambiente de desenvolvimento.
Então montei um novo ambiente com a seguinte configuração: Windows 10 Home, com o IIS 10, PHP 5.6.40 e MySQL Community Server 8.0.19, a aplicação funciona bem até tentar efetuar Login/Criar Usuários, quando ocorre comunicação com o banco de dados, nesse momento retorna um erro com a seguinte mensagem:
SQLSTATE[HY000] [2054] Server sent charset unknown to the client. Please, report to the developers

Em buscas na internet encontrei alguns lugares dizendo que isso se trata de um bug no método de autenticação do MySQL, tentei criar um novo usuário para acessar a base com um método de acesso diferente, porém continua retornando o mesmo erro.
Alguém já passou por esse problema e tem alguma sugestão para solução?
Configuração de acesso a base de dados database.php:
<code><?php

return [

    'fetch' => PDO::FETCH_CLASS,

    /*Default Connections*/
    'default' => 'mysql',

    /*Config Connections*/
    'connections' => [

        'mysql' => [
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'proj',
            'username'  => 'root',
            'password'  => '123456',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ],

        'sqlsrv' => [
            'driver'   => 'sqlsrv',
            'host'     => '192.168.10.19',
            'database' => 'CEPs',
            'username' => 'root',
            'password' => '1597xxx',
            'prefix'   => '',
        ],

    ],

    /*Config Migrations Path*/
    'migrations' => 'migrations',

    'redis' => [

        'cluster' => false,

        'default' => [
            'host'     => '127.0.0.1',
            'port'     => 6379,
            'database' => 0,
        ],

    ],

];
</code>
Log do erro laravel.log:
<code>[2020-06-25 18:32:05] production.ERROR: exception 'ErrorException' with message 'PDO::__construct(): Server sent charset (255) unknown to the client. Please, report to the developers' in C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:47
Stack trace:
#0 [internal function]: Illuminate\Exception\Handler->handleError(2, 'PDO::__construc...', 'C:\\PROJETOS\\pro...', 47, Array)
#1 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php(47): PDO->__construct('mysql:host=loca...', 'projetoX', 'veridian12345678', Array)
#2 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Database\Connectors\MySqlConnector.php(20): Illuminate\Database\Connectors\Connector->createConnection('mysql:host=loca...', Array, Array)
#3 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Database\Connectors\ConnectionFactory.php(57): Illuminate\Database\Connectors\MySqlConnector->connect(Array)
#4 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Database\Connectors\ConnectionFactory.php(46): Illuminate\Database\Connectors\ConnectionFactory->createSingleConnection(Array)
#5 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Database\DatabaseManager.php(176): Illuminate\Database\Connectors\ConnectionFactory->make(Array, 'mysql')
#6 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Database\DatabaseManager.php(64): Illuminate\Database\DatabaseManager->makeConnection('mysql')
#7 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(2922): Illuminate\Database\DatabaseManager->connection('mysql')
#8 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(2888): Illuminate\Database\Eloquent\Model::resolveConnection('mysql')
#9 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(1849): Illuminate\Database\Eloquent\Model->getConnection()
#10 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(1790): Illuminate\Database\Eloquent\Model->newBaseQueryBuilder()
#11 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(1764): Illuminate\Database\Eloquent\Model->newQueryWithoutScopes()
#12 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Auth\EloquentUserProvider.php(87): Illuminate\Database\Eloquent\Model->newQuery()
#13 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Auth\Guard.php(354): Illuminate\Auth\EloquentUserProvider->retrieveByCredentials(Array)
#14 [internal function]: Illuminate\Auth\Guard->attempt(Array, false)
#15 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Support\Manager.php(138): call_user_func_array(Array, Array)
#16 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php(211): Illuminate\Support\Manager->__call('attempt', Array)
#17 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php(211): Illuminate\Auth\AuthManager->attempt(Array, false)
#18 C:\PROJETOS\projetoX\app\controllers\LoginController.php(35): Illuminate\Support\Facades\Facade::__callStatic('attempt', Array)
#19 C:\PROJETOS\projetoX\app\controllers\LoginController.php(35): Illuminate\Support\Facades\Auth::attempt(Array, false)
#20 [internal function]: LoginController->efetuarLogin()
#21 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Routing\Controller.php(231): call_user_func_array(Array, Array)
#22 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php(93): Illuminate\Routing\Controller->callAction('efetuarLogin', Array)
#23 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php(62): Illuminate\Routing\ControllerDispatcher->call(Object(LoginController), Object(Illuminate\Routing\Route), 'efetuarLogin')
#24 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Routing\Router.php(967): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request), 'LoginController', 'efetuarLogin')
#25 [internal function]: Illuminate\Routing\Router->Illuminate\Routing\{closure}()
#26 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Routing\Route.php(109): call_user_func_array(Object(Closure), Array)
#27 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Routing\Router.php(1033): Illuminate\Routing\Route->run(Object(Illuminate\Http\Request))
#28 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Routing\Router.php(1001): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#29 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(781): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#30 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(745): Illuminate\Foundation\Application->dispatch(Object(Illuminate\Http\Request))
#31 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Session\Middleware.php(72): Illuminate\Foundation\Application->handle(Object(Illuminate\Http\Request), 1, true)
#32 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Cookie\Queue.php(47): Illuminate\Session\Middleware->handle(Object(Illuminate\Http\Request), 1, true)
#33 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Cookie\Guard.php(51): Illuminate\Cookie\Queue->handle(Object(Illuminate\Http\Request), 1, true)
#34 C:\PROJETOS\projetoX\vendor\stack\builder\src\Stack\StackedHttpKernel.php(23): Illuminate\Cookie\Guard->handle(Object(Illuminate\Http\Request), 1, true)
#35 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(641): Stack\StackedHttpKernel->handle(Object(Illuminate\Http\Request))
#36 C:\PROJETOS\projetoX\public\index.php(49): Illuminate\Foundation\Application->run()
#37 {main}

Next exception 'PDOException' with message 'SQLSTATE[HY000] [2054] Server sent charset unknown to the client. Please, report to the developers' in C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:47
Stack trace:
#0 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php(0): PDO->__construct()
#1 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Database\Connectors\MySqlConnector.php(20): Illuminate\Database\Connectors\Connector->createConnection('mysql:host=loca...', Array, Array)
#2 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Database\Connectors\ConnectionFactory.php(57): Illuminate\Database\Connectors\MySqlConnector->connect(Array)
#3 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Database\Connectors\ConnectionFactory.php(46): Illuminate\Database\Connectors\ConnectionFactory->createSingleConnection(Array)
#4 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Database\DatabaseManager.php(176): Illuminate\Database\Connectors\ConnectionFactory->make(Array, 'mysql')
#5 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Database\DatabaseManager.php(64): Illuminate\Database\DatabaseManager->makeConnection('mysql')
#6 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(2922): Illuminate\Database\DatabaseManager->connection('mysql')
#7 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(2888): Illuminate\Database\Eloquent\Model::resolveConnection('mysql')
#8 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(1849): Illuminate\Database\Eloquent\Model->getConnection()
#9 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(1790): Illuminate\Database\Eloquent\Model->newBaseQueryBuilder()
#10 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(1764): Illuminate\Database\Eloquent\Model->newQueryWithoutScopes()
#11 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Auth\EloquentUserProvider.php(87): Illuminate\Database\Eloquent\Model->newQuery()
#12 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Auth\Guard.php(354): Illuminate\Auth\EloquentUserProvider->retrieveByCredentials(Array)
#13 [internal function]: Illuminate\Auth\Guard->attempt(Array, false)
#14 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Support\Manager.php(138): call_user_func_array(Array, Array)
#15 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php(211): Illuminate\Support\Manager->__call('attempt', Array)
#16 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php(211): Illuminate\Auth\AuthManager->attempt(Array, false)
#17 C:\PROJETOS\projetoX\app\controllers\LoginController.php(35): Illuminate\Support\Facades\Facade::__callStatic('attempt', Array)
#18 C:\PROJETOS\projetoX\app\controllers\LoginController.php(35): Illuminate\Support\Facades\Auth::attempt(Array, false)
#19 [internal function]: LoginController->efetuarLogin()
#20 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Routing\Controller.php(231): call_user_func_array(Array, Array)
#21 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php(93): Illuminate\Routing\Controller->callAction('efetuarLogin', Array)
#22 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php(62): Illuminate\Routing\ControllerDispatcher->call(Object(LoginController), Object(Illuminate\Routing\Route), 'efetuarLogin')
#23 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Routing\Router.php(967): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request), 'LoginController', 'efetuarLogin')
#24 [internal function]: Illuminate\Routing\Router->Illuminate\Routing\{closure}()
#25 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Routing\Route.php(109): call_user_func_array(Object(Closure), Array)
#26 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Routing\Router.php(1033): Illuminate\Routing\Route->run(Object(Illuminate\Http\Request))
#27 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Routing\Router.php(1001): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#28 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(781): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#29 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(745): Illuminate\Foundation\Application->dispatch(Object(Illuminate\Http\Request))
#30 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Session\Middleware.php(72): Illuminate\Foundation\Application->handle(Object(Illuminate\Http\Request), 1, true)
#31 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Cookie\Queue.php(47): Illuminate\Session\Middleware->handle(Object(Illuminate\Http\Request), 1, true)
#32 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Cookie\Guard.php(51): Illuminate\Cookie\Queue->handle(Object(Illuminate\Http\Request), 1, true)
#33 C:\PROJETOS\projetoX\vendor\stack\builder\src\Stack\StackedHttpKernel.php(23): Illuminate\Cookie\Guard->handle(Object(Illuminate\Http\Request), 1, true)
#34 C:\PROJETOS\projetoX\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(641): Stack\StackedHttpKernel->handle(Object(Illuminate\Http\Request))
#35 C:\PROJETOS\projetoX\public\index.php(49): Illuminate\Foundation\Application->run()
#36 {main} [] []
</code>
Comment: mostre seu mecanismo de conexão como banco de dados.
Answer: A nova versão MySQL alterou um conjunto de caracteres padrão para utf8mb4. Alguns clientes não reconhecem esse conjunto de caracteres. Portanto, quando o servidor relata seu conjunto de caracteres padrão para o cliente e o mesmo não sabe o que significa o servidor gera o erro que ocorreu com você.
Altere o conjunto de caracteres para utf8, para ser compatível com clientes não atualizados. Talvez o link que segue te ajude...
https://stackoverflow.com/questions/3513773/change-mysql-default-character-set-to-utf-8-in-my-cnf
Comment: Realmente o problema é devido a alteração no conjunto de caracteres padrão no MySQL 8, localizei o arquivo de configuração **my.ini** em **"C:\ProgramData\MySQL\MySQL Server 8.0"** e setei as configurações da seguinte forma: **default-character-set=utf8** e **character-set-server=utf8** o que solucionou meu problema. Obrigado pela ajuda.
Comment: De nada Lenadro, sucesso!


Decoding SNMP OIDs using tshark (wireshark)

Question: I am debugging an SNMP trap problem using tshark (TShark 1.6.6) on a Linux platform.  (The target platform does not support the wireshark GUI.)  OIDs in PDUs are shown in numerical format even though I have MIBs installed in /usr/share/snmp/mibs with a link to that in /usr/local/share/mibs.  I tried -V.  There doesn't appear to be a tshark verbose or debugging option except for some memory debugging options.  I have checked the man page and find nothing on SNMP or MIBs.  I tried strace and I found a file /usr/share/wireshark/oid file but when I put the MIB directory there, I get a flex error, and a google search for what this mysterious file means turns up nothing.  I can copy and paste the OIDs into an snmptranslate command and it correctly translates them. I tried creating a ~/.wireshark directory with smi_modules and smi_paths ("/usr/share/snmp/mibs").  I did a tshark -G currentprefs to see if there was a relevant preference but there isn't.  I have googled this issue but I get way too much chaff to make any progress.  I checked unix.stackexchange.com, superuser.com, and this site.  I verified that people ask wireshark questions on this site (10,000 of them) to make sure it was on topic.
Example invocation:
tshark -R "snmp && ip.dst==<nms_ip>" -i eth0
Running as user "root" and group "root". This could be dangerous.
Capturing on eth0
  4.675952  <agent_ip> -> <nms_ip>  SNMP 115 sNMPv2-Trap 188.8.131.52.184.108.40.206.0 184.108.40.206.184.108.40.206.4.1.0

Contents of ~/.wireshark/preferences:
name_resolve: mtC
name_resolve_load_smi_modules: TRUE
snmp.display_oid: TRUE
snmp.desegment: TRUE
snmp.var_in_tree: TRUE

How do I get the OIDs to be displayed in symbolic format, e.g. sysUpTimeInstance and snmpTrapOID.0?
Comment: The "snmptranslate" command is a net-snmp tool. It has no relation to how well tshark does on doing the same thing. Just thought you could use that information. I've never gotten along with Wireshark's MIB loading functionality, myself, so I can't really offer anything more useful than that.
Comment: @Jolta: Well, I do realize that they don't in theory map one to the other, but that's to prove I at least have the right mibs installed.  One site told me that Wireshark looked for mibs in /usr/local/share/mibs so I made the link to /usr/share/snmp/mibs, so I was just demonstrating as a sanity check that snmptranslate can parse them.
Comment: Yes, it tells us that the mibs are installed correctly for snmptranslate to find them.
Comment: @Jolta:  I had hoped that setting `~/.wireshark/smi_paths` to the `net-snmp` mib directory would be sufficient to direct `tshark` to the mibs, but it doesn't seem so.  Thanks for your input, I appreciate it.
Answer: I found the information Resolving SNMP OID in snmpbulkwalk and tshark  useful.
Adding my MIBs to ~/.wireshark/smi_modules (via wireshark name resolution preferences), and ensuring the MIBs were located in the correct location
(on a mac I used dtrace to trace which files 'tshark' was accessing and located the path as /usr/share/snmp/mibs)


Powershell script to find a folder and copy to another folder

Question: I have a long directory path. The nested folder values keep changing as per project name. but last directory PackageTmp does not change, Looking forward to copy files from PackageTmp to another directory. I want to copy all subfolder and files from C:\unzip\Content\D_C\a\1\s\src\prj\obj\Release\Package\PackageTmp to C:\deploy. My code is copying to C:\deploy\PackageTmp.
Source Dir :- C:\unzip\Content\D_C\a\1\s\src\prj\obj\Release\Package\PackageTmp
Inconsistent Path :- \D_C\a\1\s\src\prj\obj\Release\Package\
Target Dir :- C:\deploy\
Non working code :- Get-ChildItem C:\unzip\ -Filter PackageTmp -Recurse | where {($_.psiscontainer)}| Copy-Item -Destination C:\deploy\ -Recurse -filter * -Force

Comment: There's a space in your `destination` parameter.
Comment: You updated it with unnecessary parameters now. What isn't working by the way? Does it not copy? You can specify the `-Directory` if you're looking for just the folder and are using posh v3.
Comment: I was working and got it copying to folder C:\deploy\PackageTmp,found it on another stack overflow question.Thanks. Looking if can copy to C:\deploy\ somehow
Answer: Analysis
Looks like you're almost there.  Your script is just finding the PackageTmp directory and then passing it down the pipeline where it's eventually copied to the destination, directory and contents all included. The trick is, once you find the PackageTmp directory, you then can use a wildcard to get everything that's IN the PackageTmp directory and copy that to your destination.
Code
I tweaked your non-working code, so hopefully this works for you.
# Get the PackageTmp directory
$PackageTmp = Get-ChildItem C:\unzip\ -Filter PackageTmp -Recurse | Where-Object {($_.psiscontainer)}

# Print the FullName of the PackageTmp directory
Write-Host "PackageTmp: $($PackageTmp.FullName)"

# Recursively copy everything in the PackageTmp directory to our destination directory
Copy-Item -Path "$($PackageTmp.FullName)\*" -Destination C:\deploy\ -Recurse -filter * -Force

Explanation
I store the PackageTmp location in a variable.  I can then specify that I want to copy everything INSIDE the PackageTmp directory by using the -Path "$($PackageTmp.FullName)\*" parameter, note the use of the wildcard \*.  This doesn't copy the PackageTmp directory, like you requested.
So effectively instead of doing this:
Copy C:\unzip\Content\D_C\a\1\s\src\prj\obj\Release\Package\PackageTmp -> C:\deploy\

We're just doing this:
Copy C:\unzip\Content\D_C\a\1\s\src\prj\obj\Release\Package\PackageTmp\* to C:\deploy\

Comment: Cool, Thanks a lot, is it possible to print value of Get-ChildItem, Like path of PackageTmp,
Comment: @AshishMishra Sure, you can store the contents of `Get-ChildItem` in a variable, print the `FullName` of the variable and then use that variable in a separate copy command.  Breaking up the command makes it easier to understand too. - I'll edit my answer to include this.


Problem in conceiving the derivation of 'Limit of Sequence to Zero Distance Point'.

Question: I was following the derivation of 'Limit of Sequence to Zero Distance Point'; here is the excerpt of the derivation:

First it is shown that: $$\forall n \in \mathbb N_{>0}: \exists x_n \in S: \left|{\xi - x_n}\right| < \dfrac 1 n$$
Suppose the contrary that $$\exists n \in \mathbb N_{>0}: \not \exists x \in S: \left|{\xi - x}\right| < \dfrac 1 n$$
Then $\dfrac1n$ is a lower bound of the set $T=\{|\xi−x|:x\in S\}.$
This contradicts the assertion that $\mathrm d(\xi,S)=0.$
We have from Power of Reciprocal that: $$\lim_{n\to \infty} \frac1n=0$$
So as $|\xi−x_n|<\dfrac1n,$ it follows from the Squeeze Theorem that: $$\lim_{n\to \infty} x_n=\xi$$

I didn't get how $\frac1n$ is the lower bound of $T\ _;$ just because there is no such $x$ for $n$ such that $|\xi - x|\lt \dfrac1n,$ how does it make $\frac1n$ as the lower bound of $T\;?$
Also, how did they use the Squeeze theorem to get $\lim_{n\to \infty} x_n=\xi\;?$
Could anyone please help me explaining these?
Answer:
The derivation didn't carry the negation of the quantified statement all the way through. In particular, the statement $\lnot(\exists x\in S\,\,|\xi-x|<\frac{1}{n})$ is equivalent to $\forall x\in S\,\,|\xi-x|\geq\frac{1}{n}$. Do you see now why $\frac{1}{n}$ is a lower bound for $T$?
Since $0\leq|\xi-x_n|<\frac{1}{n}$, the squeeze theorem says $x_n\to\xi$ as $n\to\infty$.


How to get the lowest future time (after now) from list of times with the current time

Question: How to get the lowest future time, after the current time.
Schedule Time :
06.00
12.30
17.45
Current Time:
10.20
The Schedule time are in
List<TimeSpan> lstDT

TimeSpan CurrentTimeSpan = TimeSpan.FromTicks(DateTime.Now.Ticks);

var min = lstDT
  .Select(x => new {
     diff = Math.Abs((x - CurrentTimeSpan).Ticks),
     time = x })
   .OrderBy(x => x.diff)
   .Last()
   .time;

Expected answer is 12.30 (since 12.30 is the next time after 10.20).
If the current time is 23:59 then the expected result is 6.00.
Thanks
Comment: All those exemple choose the next. Can you confirm that you want the closes and 6:20 for exemple return 6:00? Perhaps a little table with inspected input output will be a good [edit] for your question.
Comment: @CodeNotFound: this should be an answer. Could you post one?
Comment: @mjwills if the current time is 17.44 then the result should be 17.45.
if the time is 23.59 then the result should be 06.00
Comment: Whatever solution you choose, I'd suggest you test the 23:59 scenario.
Comment: @rAm It is crucial that you provide a table as per DragandDrop's suggestion.
Answer: I can see 4 issues with the current code; we have to

Get rid of date part (TimeOfDay)
Change Last into First
Be careful with break of day: 1:00 is closer to 23:59 than 22:00 (we have to analyze two values: difference and difference over the midnight)
Since we want future only we should drop Math.Abs (which makes past and future being equal) but put a condition

Implementation:
  List<TimeSpan> lstDT = new List<TimeSpan>() {
    new TimeSpan( 6,  0, 0), // pure times, no date parts
    new TimeSpan(12, 30, 0),
    new TimeSpan(17, 45, 0),
  };

  // Test Data:
  // DateTime current = new DateTime(2018, 10, 27, 11, 20, 0);
  // TimeSpan CurrentTimeSpan = TimeSpan.FromTicks(current.TimeOfDay.Ticks);

  // TimeOfDay - we don't want Date part, but Time only
  TimeSpan CurrentTimeSpan = TimeSpan.FromTicks(DateTime.Now.TimeOfDay.Ticks);

  // closest future time
  var min = lstDT
    .Select(x => new {
       // + new TimeSpan(1, 0, 0, 0) - over the midnight
       diff = x > CurrentTimeSpan
         ? (x - CurrentTimeSpan).Ticks
         : (x - CurrentTimeSpan + new TimeSpan(1, 0, 0, 0)).Ticks,
       time = x })
    .OrderBy(x => x.diff)
    .First()              // <- First, not Last
    .time;

Comment: Works great. Thanks a Ton.


Why doesn't vocal music use C clefs nowadays?

Question: I've noticed that in older scores the tenor parts (and sometimes the alto parts) are notated using C clefs, but nowadays you almost never see C clefs but instead the tenor parts are notated using an octave-shifted treble clef.
I've heard that it's to reduce the number of clefs one needs to learn, but this just seems like an oversimplification. This trips me up every time since I always have to remember to shift the notes down an octave. To me, C clefs are much easier to learn since I can immediately tell which note is which. I wish there was a better way to put it but I HATE the down-shifted treble clef. It reeks of laziness.
Beyond that, why doesn't vocal music nowadays use C clefs? And does anyone else prefer C clefs, or is the octave-shifted treble clef really easier?
Comment: If you have to remember to shift the notes on the treble clef down an octave, then I very much envy your vocal range. Why don't you prefer just using bass clef for tenor parts then?
Comment: The answers to the duplicate are equally applicable to vocal music.
Comment: I'd say you have absolute pitch, otherwise it's really not a problem to look at a note, and sing it an octave lower than written.
Comment: I'm the opposite of you, El Ectric - I find every C clef to require a double take (a la music with 3 or more ledger lines) and the octave-shifted treble clefs to be close to immediate.
Comment: @ToddWilcox For tenor parts I prefer if they are written in alto clef or tenor clef if the range is centred around middle C, and bass clef for passages involving mostly lower notes.
Comment: @KilianFoth Kinda but not exactly. I'm talking specifically in the context of vocal music (both solo and choral).
Comment: @Tim It's not that hard, but it's still enough of a hassle nonetheless.
Answer: I think they've simply fallen out of use, to a point where most musicians are unfamiliar with them. The vast majority of music for both voice and instruments are written in treble or bass clef. Back in the day, like in medieval / renaissance times, sometimes an entire piece would be notated with C clefs for tenor, alto, and soprano. That was done to avoid writing ledger lines I believe. Sometimes the C clefs would move around to lines that are even less standardized than the ones we're most familiar with like tenor clef, alto clef, and even the soprano clef. This probably made things easier to write for someone familiar with moving C clefs, but it also might've made some extra mental work for the people actually performing the piece. I'd probably speculate that we've moved to treble and bass clefs because it's a convenient standard, and if someone knows those clefs they'll be able to read like 90% of music. AFAIK, the only thing that uses a C clef by default is the viola, and it's used for high parts in low instruments like cello or bassoon. I'd say that things being mostly in treble or bass clef makes things easier for most people. Definitely for me, reading an old score with C clefs is way harder than something with treble+bass. I guess to sum it up, composers started gravitating to treble+bass as a standard, which means the performers became more familiar with those clefs, and that trend has continued, so now composers and performers both are most familiar with treble+bass and unfamiliar with C clefs for the most part. If it's a big enough problem, you could transcribe your parts into tenor clef if that's easier for you.
Comment: The practice of using soprano, alto, tenor, and bass clefs for the corresponding vocal parts was pretty much standard from the late Renaissance through the early 19th century.
Comment: That makes sense. Thanks for the correction. I was only certain that I've seen C clefs used in pre-baroque vocal music.
Comment: Yeah absolutely.  And now that I've thought about it a bit more and looked at some examples I remember that practice varied geographically: soprano parts in treble clef were more common in French music (and related styles) even in the baroque.  I recall that there are some soprano parts in treble clef in Monteverdi but I haven't had time to confirm it.
Answer: Most singers who can read music will have learned in the course of learning a musical instrument. Piano is the most common, and of other instruments the higher sounding ones are often preferred for students, both because they are cheaper and because there is more solo music written for them. Thus most of these singers will have some familiarity with the treble clef. On the other hand tenor clef will be familiar to relatively few singers - those who have studied cello, trombone or bassoon well past beginning level, and those who have made a point of learning to read tenor clef.
Singers reading music will often need to read from a grand staff. A separate vocal staff is not always provided; some songs are written as piano music on a grand staff, with the singer being expected to pick it out from the rest of the piano part, the words being placed between the treble and bass staves. Hymns and similar vocal works are also written using a grand staff, as this allows the four vocal parts to be written on two staves.
Singers who do not read music need to memorise their parts. This is normally done by repeated practice with the assistance of a pianist or an organist, who reads grand staff music.
Many songs are often sung by several different types of voice. With ranges roughly an octave apart, tenors and sopranos can share a lot of sheet music if the tenors read treble clef and sing it an octave lower.
To have access to all the above music a tenor needs to be able to access treble and bass clefs.
Note that the octave-transposed treble clef covers the notes D3 to G4 without ledger lines, while the tenor clef covers the range C3 to F4 without ledger lines. Either clef fits the tenor range well; there is little inherent difference in readability.
Tenor clef music may be useful in choral music written on separate staves and in music that is specifically for tenor. However, as noted above, knowledge of tenor clef is somewhat restricted. More tenors and more accompanists can read treble clef than can read tenor clef. As there are no particular advantages in using tenor clef, but there are significant advantages in using octave-transposed treble clef, the latter is now almost universally used.
Note that this discussion does not apply to instruments such as the cello, whose players must read bass, tenor, and (untransposed) treble clefs.
Comment: The only vocal music I've ever seen published in the 20th century using C clef has placed C on the second space from the top, which is conveniently where middle C falls on a treble clef one octave down, which is a notation I rather like, since it facilitates visual identification of the tenor part and makes obvious the octave in which it should be performed.  I don't know why some publishers would have stopped using it while others were still using it, since the idea that the funny clef simply means "treble clef one octave down" is a simple thing people should only need to learn once.
Answer:
why doesn't vocal music nowadays use C clefs?

The use of treble clefs for choral scores arose in the 19th century.  (Treble clef for soprano parts is common outside of German-speaking Europe even earlier, for example with Rameau and Purcell, but even Brahms used the soprano clef.) I don't have any source for this, but my sense is that it corresponds with the rise in amateur choral societies along with the practice of publishing songs in treble clef for use by amateur singers both female and male.  This reinforced familiarity with the treble clef, which had also become the standard clef for the right hand of keyboard scores (in northern Germany, at least, in the 17th and early 18th centuries, the soprano clef was more common for keyboard music and was virtually always used for soprano choral parts).
Because composers were writing increasingly for amateur singers who were more likely to know the treble clef than the C clefs, they started writing their choral scores in those clefs as well to reduce the possibility of transcription errors when choral parts or scores were copied from their full scores.

And does anyone else prefer C clefs, or is the octave-shifted treble clef really easier?

I'm sure that some people do, but most people I've spoken with about this decidedly do not.
Answer: I think it's fair to subdivided C clefs into two categories: those which place C on the second space from the top,and those which place it elsewhere.  Those which place it on the second space from the top are equivalent to a "treble" (fourth-line G) clef, transposed down an octave, but the transposition and intended vocal role made obvious.  Those which place it in other positions may reduce the need for ledger lines, but at the expense of requiring the performer to observe the precise location of the clef.  I don't think I've ever observed the non-octave-transposing forms in practical vocal music, and I think they're only used as the common clef for viola music (fourth-line C), which is similar to treble-clef violin music shifted a string lower) and an occasional clef for cello music (third-line C, similar to bass-clef cello music shifted a string higher).
I've seen a few variations on C clefs used for tenor parts that placed middle C on the second space from the top, and I don't really know why the convention would have subsided unless it was easier for publishers to use fewer clefs.  Havign a convention "something that isn't treble or bass clef means 'tenor part, treble clef down an octave'" seems like a nice easy way to help altos and tenors consistently identify their parts, especially if some parts split to using multiple staves or unify (if there are three treble and one bass parts, it's obvious that alto is second from the top and bass is second from the bottom, but what if there are three or five clefs?  Looking for "part just above the weird looking clef" (alto) or "weird looking clef a little above the bottom" would correctly identify parts in a wide variety of scenarios.
If conventions used when publishing notated music evolve similarly to those used when publishing text, it's likely that a publisher "got lazy" by doing something that publishing professionals would view as sloppy, but the intended audience didn't care, and thus other publishers jumped on the bandwagon.  A key principle is that handwritten manuscripts might use the same notation for two things that would have unambiguously different forms when professionally printed, the person producing the printed form would need to either understand the material or risk selecting the wrong form (e.g. using a C clef for tenor material that was supposed to be performed at written pitch).  If a publisher can get by just using a treble clef without having to worry about the octave in which the music should be performed, that eliminates the possibility of the publisher making a mistake.
The situation is somewhat analogous to a text publisher handling text like "That afternoon, the three of them went to visit Q.C.  Taylor and James said 'Hello'."  Using older conventions, if that text was supposed to be read as meaning "Taylor and James said 'hello' after three people visited Q.C.'", there would be extra space before "Taylor".  Including the space would improve clarity if that was the intended meaning, but would be disastrous if the intended meaning was "James said 'Hello' after three people visited Q.C. Taylor."  A convention that always puts a normal-width space before "Taylor" avoids the possibility of the typesetter getting the meaning wrong, but relies upon the reader figuring out the intended meaning which would usually be clear from context.
Comment: @phoog: Is there anything new about the concept of publishing a melofy in treble-clef only form?  The concept of "funny clef is treble clef, an octave down" would seem pretty easy to learn and remember, whether or not one remembered all of the particular forms such clefs could take.
Comment: //presumably you've never seen performance materials that were prepared in the middle of the 19th century or earlier// True; almost all of what I've seen has been printed after 1850.  I've seen the third-space C clef written using the "B" shaped thing, and I've seen a couple of other shapes that I figured were intended to serve the same purpose. What disadvantages do you see to that over a treble clef that's visually identical to the soprano and alto ones?
Comment: @phoog: Also, I just had another thought, given the timing of the disappearance of specialized clefs for vocal ranges: when would singers have started reading music from sheets containing everyone's parts?
Comment: No clef ever places its pitch on anything other than a line, with the exception of a short period starting in the late 1800s during which you can see various experiments using the tenor clef in the third space of the staff, which eventually gave way to the standard use of octave-transposed treble clef for the tenor voice.  " I don't think I've ever observed the non-octave-transposing forms in practical vocal music": presumably you've never seen performance materials that were prepared in the middle of the 19th century or earlier.
Comment: "I don't really know why the convention would have subsided unless it was easier for publishers to use fewer clefs": the rise of treble clefs for choral parts coincides with the rise of amateur choral societies and the practice of publishing songs in treble clef for use by male or female singers.
Comment: "when would singers": Good question. My impression (pun inadvertent) is that this too would have become common in the 19th century with the rise of amateur choral societies.  Regardless, it certainly must be a result of mechanical reproduction: Once you are engraving or typesetting parts, it's roughly the same amount of work to prepare a choral score as single choral parts. If you're publishing a vocal score for home use, it may make sense to give that to the members of the chorus. By contrast, writing out all the chorus parts by hand for each singer is far too much work.
Comment: Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/146355/discussion-between-phoog-and-supercat).


Prove there are $\xi$, $\eta$ with $f'(\xi)f'(\eta)=1$

Question: Let $f:[0,1]\to\mathbb{R}$ be continuous with $f(0)=0$, $f(1)=1$ and $f$ is differentiable on $(0,1)$. Show that there are distinct $\xi,\eta\in(0,1)$ so that $f'(\xi)f'(\eta)=1$.
I think this requires mean value theorem. But this does not help since if apply the theorem to $f$ we can only get $f'(c)=1$ for some $c$.
Comment: The result is very general and for any given positive integer $n$ it is possible to find $n$ distinct points $x_{1}, x_{2},\dots, x_{n}$ all in $(0,1)$ such that $\prod_{i = 1}^{n}f'(x_{i}) = 1$. This is already proved in MSE but unable to find the link.
Comment: @ParamanandSingh: That's [How to prove there exist distinct $a_{i}$ such $f'(a_{1})f'(a_{2})f'(a_{3})\cdots f'(a_{n})=1$](https://math.stackexchange.com/q/1373408).
Answer: Here is one way to do this:
Let $g(x) = f(x)^2 - x^2$.  Applying Rolle's theorem to $g(x)$, we obtain a value $\eta \in (0,1)$ such that $2f(\eta)f^\prime(\eta) - 2\eta = 0$, or $f(\eta) = \frac{\eta}{f^\prime(\eta)}$.  Now apply the mean value theorem to $f$ on the domain $[0, \eta]$ to obtain a value $\xi \in (0,\eta)$ such that $f^\prime(\xi) = \frac{f(\eta)}{\eta}$.  Then $\displaystyle f^\prime(\xi)f^\prime(\eta) = \frac{f(\eta)}{\eta}f^\prime(\eta) = \frac{\eta}{\eta f^\prime(\eta)}f^\prime(\eta) = 1$.
Comment: Sir what is the motivation behind considering the function $f(x)^2-x^2$?
Answer: Clearly the equation $f(x) = x$ has at least two roots in $[0, 1]$ (namely $x = 0, x = 1$). If there was another root in $(0, 1)$ (say $c$) then we know that there will be $\xi \in (0, c), \eta \in (c, 1)$ such that $f'(\xi) = 1 = f'(\eta)$ and we are done.
Hence let's assume that $x = 0, 1$ are the only roots of $f(x) = x$ in $[0, 1]$. Thus there are two possibilities: either $f(x) > x$ for all $x \in (0, 1)$ or $f(x) < x$ for all $x \in (0, 1)$.
We take the case when $f(x) > x$ for all $x \in (0, 1)$. Consider the point $d \in (0, 1)$ where $f'(d) = 1$ (its existence is guaranteed by Mean Value Theorem). Clearly $f(d) > d$ and hence there is a point $p \in (0, d)$ with $f'(p) = f(d)/d > 1$ and another point $q \in (d, 1)$ with $f'(q) = (1 - f(d))/(1 - d) < 1$. Thus we have found two points $p, q \in (0, 1)$ with $B = f'(p) > 1, A = f'(q) < 1$.
Now the proof is almost obvious. By Darboux theorem derivatives possess the intermediate value property and hence $f'(x)$ takes all values between $A$ and $1$ and all values between $1$ and $B$. And clearly we can choose two such values of $f'(x)$ whose product is $1$.
More generally we can choose as many pairs of values of $x$ as we want such that the product of derivative $f'$ at these points (corresponding to one chosen pair) is $1$. This proves the general result that for any $n$ we can find distinct points $x_{i}$ such that $\prod f'(x_{i}) = 1 $.


'react-native-fs' and 'react-native-fetch-blob' can not find the downloaded file

Question: I user 'react-native-fs' and 'react-native-fetch-blob' to test file download in android emulator,
the console show me that the download is success, but I can not find where the file is in my emulator file system,or maybe it did not download successfully,can anybody tell me what to do,thanks
Comment: Which directory did you specify for download?
Answer: Use this function for Android with react-native-fetch-blob.
It works for me and the downloaded file will be placed in the downloads directory of the device/emulator.
function downloadFile(url,fileName) {
  const { config, fs } = RNFetchBlob;
  const downloads = fs.dirs.DownloadDir;
  return config({
    // add this option that makes response data to be stored as a file,
    // this is much more performant.
    fileCache : true,
    addAndroidDownloads : {
      useDownloadManager : true,
      notification : false,
      path:  downloads + '/' + fileName + '.pdf',
    }
  })
  .fetch('GET', url);
}

Comment: First, the file type may be part of the download url. Second, I haven't tried iOS, but I think the code should be pretty much the same. Except for `DownloadDir` is only supported for Android, so you should use `DocumentDir`  instead, but then the file will be placed in the app's directory. At least, If the answer was helpful - You could up-vote it and mark as accepted.
Comment: Thanks for you answer,the code is useful,if you do not mind, I want to ask some question more,if I just know that it is a  file but do not know what the document type is  that i going to download,what should i do to advance the code, and do you have code about download in ISO,I am not good at Englisth,hope you can understand what i said,THANKS!THANKS!THANKS!
Comment: Thanks you so much for your assistance
Comment: notification false does not work for me, still shows notification when file download finished. Do you know why?


'Delay' na atribuição/existência de propriedades na instância vue

Question: Eu tenho o seguinte código:

Vue.component('child-comp', {
  template: '
Child here
',
  props: ['item'],
  methods: {
     alt: function(msg) {
     alert(msg);
     }
  }
});

new Vue({
  el: '#app',
  data() {
     return {
        item: null
     }
  },
  methods: {
     load_child: function() {
        this.item = true;
        this.$refs['comp1'].alt(this.item.toString());
     }
  }
});

  <button v-on:click="load_child">execute child</button>
  <child-comp v-bind:item="item" v-if="item != null" ref="comp1"></child-comp>

Como se pode ver ao primeiro clique dá erro, dizendo:

Cannot read property 'alt' of undefined / TypeError: this.$refs.comp1 is undefined

Quando na partir da linha anterior este componente (this.item = true;) deveria já existir/ser defined
Para contornar isto eu fiz:

Vue.component('child-comp', {
  template: '
Child here
',
  props: ['item'],
  methods: {
     alt: function(msg) {
     alert(msg);
     }
  }
});

new Vue({
  el: '#app',
  data() {
    return {
        item: null
    }
  },
  methods: {
     load_child: function() {
        this.item = true;
        setTimeout(() => {this.$refs['comp1'].alt(this.item.toString());}, 100);
     }
  }
});

  <button v-on:click="load_child">execute child</button>
  <child-comp v-bind:item="item" v-if="item != null" ref="comp1"></child-comp>

Mas não quero nem me parece de todo ser o correto.
Procuro uma alternativa a isto, será que implementei mal alguma coisa, ou deveria ter feito algo que não fiz/sei que existe.
Comment: Não seria suficiente correr `this.alt()` dentro do `mounted` do `child-comp`?
Comment: Sim @Sergio , neste caso que coloquei sim, mas no projeto que estou a fazer vão haver argumentos a entrar em "`alt()`", que são dinâmicos e variam consoante o componente pai
Comment: Ainda assim acho que devias usar `props`. Estar dependente de assincronismo pode dar problemas. Se `alt()` fôr assíncrona isso vai dar outros problemas.
Comment: @Sergio, tudo bem eu acredito e sei que sabe o que está a dizer. Mas poderia dar um exemplo do que seria a sua solução, como resposta. Aceitei aquela pois me parece a mais indicada até agora. Editei a resposta para que `alt()` recebesse argumentos
Comment: Dei uma resposta com o que faria no caso que me parece ser o teu. Se a pergunta for mais detalhada é mais fácil responder.
Answer: O que está ocorrendo é que você deve aguardar a alteração do DOM, pois o valor que precisa está nele. O vuejs tem a solução para estes tipos de comportamento, é o nextTick. Este comando serve para aguardar a alteração do DOM após o codigo acima e logo que o DOM for alterado, ele será executado. Consulte a documentação neste link para saber mais.

Vue.component('child-comp', {
  template: '
Child here
',
  props: ['item'],
  methods: {
    alt: function() {
      alert(this.item);
    }
  }
});

new Vue({
  el: '#app',
  data() {
    return {
      item: null
    }
  },
  methods: {
    load_child: function() {
      this.item = true;

      // LINHA ADICIONADA
      this.$nextTick(function() {
        this.$refs['comp1'].alt();
      });

    }
  }
});

  <button v-on:click="load_child">execute child</button>
  <child-comp v-bind:item="item" v-if="item != null" ref="comp1"></child-comp>

Answer: Em casos parecidos eu costumo usar arrays, passar o conteúdo por props e mostrar os elementos no DOM dependendo dessa array. Assim fica tudo sequencial, sem possíveis condições de corrida.

Vue.component('child-comp', {
  template: '

{{item.title}}

{{item.body}}

',
  props: ['item']
});
const url = 'https://jsonplaceholder.typicode.com/posts/';
new Vue({
  el: '#app',
  data() {
    return {
      items: [],
      postNr: 1
    }
  },
  methods: {
    load_child: function() {
      axios.get(url + this.postNr++)
        .then(res => this.items.push(res.data))
        .catch(e => console.log(e));
    }
  }
});

  <button v-on:click="load_child">load post</button>
  <child-comp :item="item" :key="item.id" v-for="item in items"></child-comp>

Comment: Olá Sérgio, postei uma pergunta há algum tempo aqui: pt.stackoverflow.com/questions/252268/… . Acha que há uma solução melhor do que a que eu respondi? Caso haja faça o favor de responder que será aceite com prazer, talvez trocar o componente currente pelo `NotFound`. (desculpe enviar mensagem mensagem aqui, não de outro jeito de falar consigo)


Generating correlated positive random numbers (given means, variances and degree of correlation)?

Question: I have a vector of means and a covariance matrix and I'm trying to create a data set that would like to
generate strictly positive numbers that would fit the parameters.
I have seen quite a few options but all of them resulted in creating some negative observations.
I have implemented the following code using the MASS package, but I have also tried the mvtnorm as well as several other options which have not produced the needed results.
means <- c(0.1505, 0.1596, 0.1552)

cov_matrix <- matrix(c(0.23915, -0.04556, -0.04729,
                     -0.04556, 0.21460, -0.05269,
                     -0.04729, -0.05269, 0.20674), ncol = 3)

t <- MASS::mvrnorm(n = 50, mu = means, Sigma = cov_matrix, empirical = TRUE)

head(t)
        [,1]     [,2]     [,3]
[1,]  0.1432  0.51024  0.29832
[2,]  1.0483  0.29353 -1.02127
[3,]  0.3252  0.45140 -0.41820
[4,] -0.3459  0.08166  0.04431
[5,] -0.3144  0.29057  0.55096
[6,]  1.1736 -0.63583 -0.56427

I feel as if it should be quite simple and I'm just missing something.
Comment: If you want strictly positive numbers when why use the normal distribution? It has support for negative numbers whatever parameters you use.
Comment: It could be quite simple, but you need to tell us what joint distribution you want the samples to have. If you want them to be multivariate normal as per your code, then you would need the means to be large compared to their variance and maybe run the code with different seeds until you obtained strictly positive values. On the other hand you could choose a distribution that does not have support for negative numbers such as lognormal or gamma.
Answer: Here is one suggested path.
Start by assuming that your data follows, for example, a Lognormal distribution, which provides a positive continuous probability function supported on the positive real numbers.
Required are the associated relationships in the Lognormal population between your target mean for, say variable Y, namely $Y_m$, and also its variance, $Var(Y)$, together with their relationship to the parameters of the generated lognormal deviate. The latter expressions for the mean is given by $\exp( \mu + \sigma^2/2)$ and the $Var(Y)$ is equal to $\exp(\sigma^2 -1)\exp(2\mu + \sigma^2)$. Also, per these two references, Wikipedia and this forum, we note:
$\text{Cov}(X,Y)=E(X)E(Y)[\exp(\rho\sigma_x\sigma_y)-1]$
So, upon dividing both sides of the expression by the product, $\sigma_X\sigma_Y$ (that is, the product of the respective standard deviations of X and Y in Lognormal population), we have a relationship connecting the parent bivariate Lognormal population target correlation with the $\rho$ associated with the correlation associated between the logs of X and Y, which I call simply ρ.
As the log of a Lognormal deviate is Normally distributed, one can now use relationships associated with the conditional distribution of $X$ given a value for $Y$, namely:
$E(X|Y=y) = \mu_X+ σ_X ρ(y−Y_m )/σ_Y $
$Var(X|Y=y) = σ_X^2 (1−ρ^2)$
Source, see, for example, Page 267 here.
So, the simulation procedure is to derive the corresponding parameter values of means, variances and correlation for the log of the lognonormal deviates. As these are Normally distributed, proceed to generate an appropriate random normal deviate for Y, and use the conditional Normal distribution expression to obtain the corresponding X. Repeat.
You now have a simulated population of bivariate Normal random deviates. Upon applying the exponential transform, you should now have a Lognormal population with the targetted parameter values.
Answer: Originally I preferred not to transform my distribution to log-normal, yet I have ended up doing so and I used the dmutate package for generating my numbers:
t <- rlmassnorm(n = 1000, mu = means, Sigma = cov_matrix, empirical = TRUE)


What should I do in case I have dominant feature in XGB model?

Question: I've recently faced a "strange" observation in my dataset. After XGB modeling with 20 features I plot top 10 features with the highest gain values. Result is shown below:
F1         140027.061202
F2          11242.470370
F3           9957.161039
F4           9677.070632
F5           7103.275865
F6           4691.814929
F7           4030.730915
F8           2775.235616
F9           2384.573760
F10          2328.680871

As you can see F1 dominates with gain compared to all other features (12x more gain than F2). I verified the results on test set, model is not overfitting and it gives a decent results (comparing to my figures of merit):
F1-score: 0.739812237993
Accuracy: 0.839632893701
Precision: 0.63759578607
Recall: 0.881059718486

Based on these results is it correct to conclude that F1 feature is enough for building a model?
In order to prove this, I re-run the modeling with the same parameters, but now having F1 as a standalone feature. Results are just slightly worse than previous (and no over-fit):
F1-score: 0.710906846703
Accuracy: 0.819880412472
Precision: 0.607953806173
Recall: 0.85583736242

My XGB parameters are super simple in both cases:
alg = XGBRegressor(
    n_estimators=200,
    max_depth=5,
    objective='binary:logistic',
    seed=27,
)

# Fit the algorithm on the data
metric = 'map'
alg.fit(X_train, y_train, eval_metric=metric)

After I exclude feature F1 and re-fit the model I get the similar verification metrics (slightly worse) but in that case feature F3 becomes "dominant" with a really high gain ~ 10000 while feature F2 is the next one with gain value ~ 10000.

Is there any technique to boost other features and increase the accuracy (or F1-score) by applying certain transformations on F1?
Is there any way to equalize features gain and improve the model?
Should I try to "collect" more features that can have comparable gain as F1?

Thanks!
Answer: Have you tried adding and tuning additional parameters and using grid search to find the optimal combination? To prevent over fitting I can suggest adding:

colsample_bytree: subsample ratio of columns when constructing each tree
subsample: Subsample ratio of the training instances
min_child_weight: prevent learning relations highly specific to the particular sample

Since you are using XGBRegressor, try modifying the objective function. I can also suggest monitoring the validation and training loss when building the trees.
Reference documentation
Comment: Thank you for the answer :) ! Yes, I did try to tune those parameters as well, and the result remains mostly unchanged, in terms of metrics and in terms of feature importance.

I did not try to run any optimization (grid search) because I was not sure if this will help. Also, are you suggesting that this model is overfitting? I check metrics results both on train and test data and they r more or less the same ...

I will def. try to monitor validation/training loss and check if something is suspicious there.


Save additional field using Mongoose Hook

Question: I would like to save the lower cased version of my name field so that I can perform efficient case insensitive searches on the name field. Since I am working with an external API I also store the unmodified response object into my database.
My question:
How can I save the lower cased version of my name field without modifying the API response (I still would like to pass the response object to my model when saving new documents).
Dummy model:
const userSchema = mongoose.Schema({
  name: {
    type: String,
    required: true,
    index: true,
  },
  name_lowercased: {
    type: String,
    required: true,
    index: true,
  }
})

I assume it's possible using the pre('save') or post('save') hooks, but I have no idea how I could modify the to be saved document in the hooks.
Answer: You can use the pre-save hook to save the lowercase version of the name field:
userSchema.pre('save', function(next) {
    this.name_lowercased = this.name.toLowerCase();
    next();
});

Comment: It appears it's not working for me. I think the hook isn't even called, probably because I use upsert? is there another approach?
Comment: I believe the pre-save hook is not called on update methods so you will need to use 'find()' and then update the name property and the 'save()'. This should then run pre-save.
Comment: wouldn't it be better to do this 'post' in case the field doesn't exist yet? I guess it depends if the object property is already set before this gets hooked


Least squares: How does the projection theorem lead to the normal equations?

Question: I understand why $\vec{{P}}$ (the orthogonal projection of $\vec{{b}}$ on w)
is the closest vector to $\vec{{b}}$ (The vector that does not give us a solution to the equation and that's why we need to do the process)
But I do not understand the equation:
$$A^TA\vec{{x}}=A^T \vec{{b}}$$
Why does multiplying each side of the original equation $A\vec{{x}}=\vec{{b}}$ (We know she has no answer)
by $A^T$ give us the closest answer?
Thank you
Answer: Given vector $\vec b$ and matrix $A$, the goal is to find $\vec x$ so that $A\vec x$ is as close to $\vec b$ as possible.
Let $W$ be the subspace of all vectors of the form $A\vec x$. Observe that $W$ is the set of all linear combinations of columns of $A$.
We are looking for the vector in $W$ that is closest to $\vec b$. Write $A\hat x$ for this vector. By the projection theorem, $A\hat x$ is the orthogonal projection of $\vec b$ onto $W$. Equivalently, $A\hat x-\vec b$ is orthogonal to every vector in $W$. As a special case, we conclude:
$$
\text{$A\hat x-\vec b$ is orthogonal to every column of matrix $A$}.
\tag{*}$$
Constraint (*) can be written in matrix form as:
$$ A^T(A\hat x-b) = 0,$$ which leads to the equation $A^TA\hat x=A^T\vec b$.
Comment: @NFLX Yes,  $\vec u^T\vec v=0$ means the vectors $\vec u$ and $\vec v$ are orthogonal. And if $\vec P$ is the vector in $W$ that is closest to $\vec b$ then the projection theorem states that the error vector $\vec b-\vec P$ is orthogonal to every vector in $W$.
Comment: Thank you very much for the explanation. i just do not understand how it helps us if: $$ A^T(A\hat x-b) = 0,$$  Is a multiplication of each column of A Gives us a solution because the multiplication is equal to 0?
Comment: @NFLX You can rewrite the matrix equation $A^T(A\hat x-b)=0$ into a system of linear equations that can be solved for the best $\hat x$. In other words, the projection theorem leads to conditions that the best $\hat x$ must satisfy. These conditions lead to an equation that we can solve.
Comment: (Sorry it's long)
I kind of explain everything to myself and just tell me if I'm right:
We are looking for the vector which is closest to $\vec{{b}}$ because $\vec{{b}}$
Is not a solution to $$A\vec{{x}}=\vec{{b}}$$.
We know that $\vec{{P}}$ (the Orthogonal projection is the closest solution.
So we demand that $$ A^T(\vec{{P}}-\vec{{B}}) = 0,$$
Because it tells us it's 90 degrees between $\vec{{P}}$ and $\vec{{B-P}}$
and that it proves to us that $\vec{{P}}$ is the closest?
Comment: Excellent!
really really thank you!
Answer: I'm not able to comment, but my answer to: How to formulate ordinary least squares regression in component formalism? may address your question. The reason multiplying each side by $A^T$ helps is that while $A$ may not have an inverse, $A^TA$ typically does, allowing you to solve for $\vec{x}$.
Answer: @nosuchthingasmagic gives a link to a question that gives a calculus-based derivation. Here's another one.
The standard notation is $\vec y = X \vec {\beta}$. Then we note that this has no exact solution, so really $X \vec {\beta} = \vec y + \vec {\epsilon}$. If $\vec \beta$ is optimal, then $X^T \vec {\epsilon}=\vec 0$. So when we multiply both sides by $X$, $\epsilon$ disappears and we're left with $X^TX \vec {\beta} = X^T\vec y$.
Why $X^T \vec {\epsilon}=\vec 0$? Well, that's saying that every row of $X^T$ is orthogonal to $\vec {\epsilon}$. And the rows of $X^T$ represent all the observations of a particular feature. So this is saying that for each feature, the observations of that feature are uncorrelated with the errors. If there were a correlation, we could adjust the corresponding $\beta_i$ to get a better fit.


A tough definite integral using contour integration

Question: For some reason, I am guessing that for any fixed $s_1,s_2>0$ and $\varepsilon >0$ being small, we have
\begin{align}&\quad-\int_0^\infty \frac{1}{2\pi} \log\left(\frac{(x-s_1)^2+s^2_2}{(x+s_1)^2+s^2_2}\right)\frac{4x\sin\varepsilon}{x^4-2x^2\cos\varepsilon +1}\,dx\\&= \log\left(\frac{1+s^2_1+s^2_2+2s_2\sin(\frac{\varepsilon}{2})+2s_1\cos(\frac{\varepsilon}{2})}{1+s^2_1+s^2_2+2s_2\sin(\frac{\varepsilon}{2})-2s_1\cos(\frac{\varepsilon}{2})}\right),
\end{align}
and I believe this can be shown by some clever contour integration. However, I really didn't figure out the contour that should be used to evaluate the integral... Any help or suggestions would be greatly appreciated!
Comment: Can you bring some context to this question? Like how did you guess that answer or where did the integral appear?
Comment: Hi, I guess the answer from a known result in a research paper, and I indeed tried Mathematica with some specific values of $s_1,s_2 >0$ and $\varepsilon>0$ (and the formula worked in these particular cases), these lead to my conjecture here.
Comment: I see, I would suggest to expand the terms inside the logarithm, add a new parameter near the $x$ term and differentiate with respect to it.
Afterwards a nasty rational integral, but solvable, should appear (given that the bounds are nice, contour integration should work there). In the end you'll just have to integrate back.
Comment: Something like this (I've used $a=s_1,b=s_2$ and added the additional term $\cos t$ in order to differentiate w.r.t. it - it will also look similar to the other term after it's differentiated):
$$I(t)=\int_0^\infty \ln\left(\frac{x^2-2ax\cos t+a^2+b^2}{x^2+2ax\cos t+a^2+b^2}\right)\frac{x\sin y}{x^4-2x^2\cos y +1}dx$$
$$\Rightarrow I'(t)=\int_0^\infty \left(\frac{2a x\sin t}{x^2-2ax\cos t+a^2+b^2}+\frac{2a x\sin t}{x^2+2ax\cos t+a^2+b^2}\right)\frac{x\sin y}{x^4-2x^2\cos y +1}dx$$
Expanding into partial fraction would be also really useful here, but it doesn't look like a pleasant task.
Comment: Interesting strategy, so you add a "$cos(t)$" factor inside the log, and my integral becomes $I(0)$ (up to a multiplicative constant)?
Comment: Yes, your integral will be $I(0)$, basically after evaluating $I'(t)$, you can also see that $I\left(\frac{\pi}{2}\right)=0$, so the original integral would be $I(0)=-(I\left(\frac{\pi}{2}\right)-I(0))=-\int_0^\frac{\pi}{2} I'(t)dt$. But again the hard task is to evaluate $I'(t)$.
Comment: I got you, thanks a lot! I will try to go in this direction and I will let you know if I succeed...
Comment: $(-\infty, \infty)$ (the integrand is even) can be deformed to a loop enclosing $[-s_1 + i s_2, s_1 + i s_2]$ (with a suitable choice of a branch of $\ln$). $\ln |z|$ cancels out, so the original integral is equal to
$$-2 i \sin \epsilon \left(
 -\int_{-s_1 + i s_2}^{s_1 + i s_2} f(x) \, dx +
 \operatorname* {Res}_{x = e^{i \epsilon/2}} f(x) \ln g(x) +
 \operatorname* {Res}_{x = -e^{-i \epsilon/2}} f(x) \ln g(x) \right), \\
f(x) = \frac x {x^4 - 2 x^2 \cos \epsilon + 1},
\quad g(x) = \frac {(x - s_1)^2 + s_2^2} {(x + s_1)^2 + s_2^2}.$$
This is still rather messy though.
Comment: Hi Maxim, I agree that it seems that the calculations are elementary yet extremely messy, however, the guess I posed above it yet somehow elegant and simple (and its validity is verified thorough "Mathematica" by some specific choices of parameters involved)...
Answer: As suggested, a contour integration technique can be used to evaluate this integral. Notice first that the integrand is an even function of $x$, then
\begin{align}
I&=- \frac{1}{2\pi}\int_0^\infty \log\left(\frac{(x-s_1)^2+s^2_2}{(x+s_1)^2+s^2_2}\right)\frac{4x\sin\varepsilon}{x^4-2x^2\cos\varepsilon +1}\,dx\\
&=- \frac{1}{4\pi}\int_{-\infty}^\infty \log\left(\frac{(x-s_1)^2+s^2_2}{(x+s_1)^2+s^2_2}\right)\frac{4x\sin\varepsilon}{x^4-2x^2\cos\varepsilon +1}\,dx
\end{align}
Considering the integral
\begin{equation}
J=- \frac{1}{2\pi}\int_{-\infty}^\infty \log\left(\frac{x-s_1+is_2}{x+s_1+is_2}\right)\frac{4x\sin\varepsilon}{x^4-2x^2\cos\varepsilon +1}\,dx
\end{equation}
where the log function is defined with a branch cut between the points $−s_1−is_2$ and $s_1−is_2$ with $s_2>0$. One can show that  it is purely real (see (**)). By expressing the real part (see (*)), we find $J=I$.
The function is holomorphic for $\Im x>0$ except at the poles $x_k$ of the rational fraction with $\Im (x_k)>0$. If the real axis is closed by the upper  half-circle $C_R$, the integral can then be evaluated by the residue method. The   $C_R$ contribution vanishes as $R\to\infty$.
Assuming $0<\varepsilon<2\pi$, the poles of interest are simple : $x_+=e^{i\varepsilon/2}$ and $x_-=-e^{-i\varepsilon/2}$. The residues are then evaluated as
\begin{align}
R_{\pm}&=\operatorname{Res}\left[  \log\left(\frac{x-s_1+is_2}{x+s_1+is_2}\right)\frac{4x\sin\varepsilon}{x^4-2x^2\cos\varepsilon +1},x_\pm\right]\\
&= \log\left(\frac{x_\pm-s_1+is_2}{x_\pm+s_1+is_2}\right)\frac{4x_\pm\sin\varepsilon}{\left.\frac{d}{dx}\left[x^4-2x^2\cos\varepsilon +1\right]\right|_{x=x_\pm}}\\
&=\log\left(\frac{x_\pm-s_1+is_2}{x_\pm+s_1+is_2}\right)\frac{\sin\varepsilon}{x_\pm^2-\cos\varepsilon}\\
&=\mp i\log\left(\frac{x_\pm-s_1+is_2}{x_\pm+s_1+is_2}\right)
\end{align}
and thus
\begin{align}
I&=-\frac{1}{2\pi}2i\pi \sum_{\pm} R_{\pm}\\
&=-\log\left(\frac{\cos\left(\frac{\varepsilon}{2}\right)-s_1+i(s_2+\sin\left(\frac{\varepsilon}{2}\right))}{\cos\left(\frac{\varepsilon}{2}\right)+s_1+i(s_2+\sin\left(\frac{\varepsilon}{2}\right))}\right)+\log\left(\frac{-\cos\left(\frac{\varepsilon}{2}\right)-s_1+i(s_2+\sin\left(\frac{\varepsilon}{2}\right))}{-\cos\left(\frac{\varepsilon}{2}\right)+s_1+i(s_2+\sin\left(\frac{\varepsilon}{2}\right))}\right)\\
&=-\log\left(\frac{(\cos\left(\frac{\varepsilon}{2}\right)-s_1)^2+(s_2+\sin\left(\frac{\varepsilon}{2}\right))^2}{(\cos\left(\frac{\varepsilon}{2}\right)+s_1)^2+(s_2+\sin\left(\frac{\varepsilon}{2}\right))^2}\right)
\end{align}
Finally,
\begin{equation}
I= \log\left(\frac{1+s^2_1+s^2_2+2s_2\sin(\frac{\varepsilon}{2})+2s_1\cos(\frac{\varepsilon}{2})}{1+s^2_1+s^2_2+2s_2\sin(\frac{\varepsilon}{2})-2s_1\cos(\frac{\varepsilon}{2})}\right)
\end{equation}
as proposed.

(*): using $\log\left( Z \right)=\frac{1}{2}\log\left|Z\right|^2+i\operatorname{Arg}(Z)$
(**): If
\begin{equation}
J=\int_{-\infty}^\infty \log\left(\frac{x-s_1+is_2}{x+s_1+is_2}\right)f(x)\,dx
\end{equation}
where $f(-x)=-f(x)$ and $s_{1,2}$ are real, then the complex conjugate
\begin{align}
J^*&=\int_{-\infty}^\infty \log\left(\frac{x-s_1-is_2}{x+s_1-is_2}\right)f(x)\,dx\\
&=\int_{-\infty}^\infty \log\left(\frac{-x+s_1+is_2}{-x-s_1+is_2}\right)f(x)\,dx\\
&=\int_{-\infty}^\infty \log\left(\frac{y+s_1+is_2}{y-s_1+is_2}\right)f(-y)\,dy\\
&=J
\end{align}
The integral is thus real.
Comment: Fantastic answer! I will examine your answer as soon as I can and I am going to accept this answer if it is correct. Thank you very very very much!
Comment: Hi Paul, if you don't mind, may I ask a quick question? Actually, I am not sure why we have \begin{align}
&- \frac{1}{4\pi}\int_{-\infty}^\infty \log\left(\frac{(x-s_1)^2+s^2_2}{(x+s_1)^2+s^2_2}\right)\frac{4x\sin\varepsilon}{x^4-2x^2\cos\varepsilon +1}\,dx\\
&=- \frac{1}{2\pi}\int_{-\infty}^\infty \log\left(\frac{x-s_1+is_2}{x+s_1+is_2}\right)\frac{4x\sin\varepsilon}{x^4-2x^2\cos\varepsilon +1}\,dx
\end{align} Also, I do not understand when you said " We used symmetry to show that the imaginary part of the integral vanishes". A minor suggestion is to use $z$ instead of $x$ for complex #s.
Comment: @FeiCao You are welcome! I added notes to explain the details.
Comment: Hi Paul, thanks for the details. However, I still have one confusion. To justify (*) you used $\log\left( Z \right)=\frac{1}{2}\log\left|Z\right|^2+i\operatorname{Arg}(Z)$ with $Z = \frac{x-s_1+is_2}{x+s_1+is_2}$. But I did not see why $\operatorname{Arg}(Z) = 0$, because we should have $\operatorname{Arg}\left(\frac{x-s_1+is_2}{x+s_1+is_2}\right) = \operatorname{Arg}(x^2-s^2_1+s^2_1+2is_1s_2)$, which is clearly not zero... Thank you very much!
Comment: The explanation was rather unclear. I reprased it, hoping it is more natural that way.
Comment: Thanks! It now makes more sense! I have accepted this great answer!
Answer: Following Zacky's comments, the biggest challenge is the evaluation of $$\int_{-\infty}^\infty \left(\frac{x^2}{x^2-2ax\cos t+a^2+b^2}+\frac{x^2}{x^2+2ax\cos t+a^2+b^2}\right)\frac{2a\sin t \sin y}{x^4-2x^2\cos y +1}dx,$$ note that the integrand is even in $x$ and we can restrict the domain of $t$ to be $[0,\frac{\pi}{2}]$. To calculate the above integral we let $f(z)$ to the integrand with $x$ being replaced by $z$, and integrate along a large upper semi-circle of radius $R$, call this path $\gamma_R$, then $f$ has four simple poles located at $\pm a\cos(t)+i\sqrt{(a\sin(t))^2+b^2}$, $e^{\frac{iy}{2}}$ and $-e^{-\frac{iy}{2}}$, respectively. However, the residue calculations at these points are very sophisticated and I didn't see any simplifications that can be made to make things clear...
Comment: A CAS solved the integral you wrote. The only qualifier for the result is $\color{red}{\text{monster}}$
Answer: Without contour integration.
I changed notations and focused on
$$\int \frac{ x  }{x^4-2 x^2 \cos
   (t)+1}\log \left(\frac{(x-a)^2+b^2}{(a+x)^2+b^2}\right)$$ Surprising or not, a CAS is able to compute the antiderivative which is a monster.
What I should do first is partial fraction decomposition to get
$$\frac x{x^4-2 x^2 \cos(t)+1}=\frac 1{r-s} \left(\frac x {x^2-r}-\frac x {x^2-s} \right)$$ where $r$ and $s$ are the roots of the quadratic equation in $x^2$; they are $r=e^{it}$ and $s=e^{-it}$.
I should decompose the logarithm too and integrate; each of the four required integrals has a closed form (not the most pleasant but perfectly workable).
Integrate from $0$ to $p$ and take the limit when $p\to \infty$.
Comment: Thank you for your comment, without contour integration I really didn't see how simple partial fraction decomposition will work... Also, may I know does the "monster" you got via the so-called "CAS" confirms my original guess for the result of the definite integral I want to compute at the very beginning?