Android MediaPlayer Error -1004

Question: I'm having a problem with the MediaPlayer. This is in a SherlockFragment and it worked when I created buttons using XML.
The working code looked like this:
@Override
public void onClick(View v) {
    switch (v.getId()) {

    case R.id.stop:
        if(mPlayer!=null && mPlayer.isPlaying()){
            mPlayer.stop();
            buttonPlay.setText("Play");
            mPlayer.reset();
            buttonPlay.setEnabled(true);
        }
        break;
    case R.id.play:
        mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        buttonPlay.setEnabled(false);
        buttonPlay.setText("Playing...");
        try {
            mPlayer.setDataSource(url);
        } catch (IllegalArgumentException e) {
            Toast.makeText(getActivity(), "mPlayer.setDataSource(url); error 1", Toast.LENGTH_LONG).show();
        } catch (SecurityException e) {
            Toast.makeText(getActivity(), "mPlayer.setDataSource(url); error 2", Toast.LENGTH_LONG).show();
        } catch (IllegalStateException e) {
            Toast.makeText(getActivity(), "mPlayer.setDataSource(url); error 3", Toast.LENGTH_LONG).show();
        } catch (IOException e) {
            e.printStackTrace();
        }

        mPlayer.prepareAsync();
        final ProgressDialog progressDialog;
        progressDialog = ProgressDialog.show(getActivity(), "", "Fetching files...");
        mPlayer.setOnPreparedListener(new OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                //Now dismis progress dialog, Media palyer will start playing
                progressDialog.dismiss();
                mp.start();
            }
        });
        break;
    }
}

It worked fine but I need it to working for programmatically created buttons like this:
           play[i].setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Log.e("button", "play clicked ");
                    mPlayer = new MediaPlayer();
                    mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                    try {
                        mPlayer.setDataSource(url);
                    } catch (IllegalArgumentException e) {
                        Toast.makeText(getActivity(), "mPlayer.setDataSource(url); error 1", Toast.LENGTH_LONG).show();
                    } catch (SecurityException e) {
                        Toast.makeText(getActivity(), "mPlayer.setDataSource(url); error 2", Toast.LENGTH_LONG).show();
                    } catch (IllegalStateException e) {
                        Toast.makeText(getActivity(), "mPlayer.setDataSource(url); error 3", Toast.LENGTH_LONG).show();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    play[a].setText("Playing...");

                    for(int j =0; j < jsons.length; j++){
                        if(a != j ){
                            play[j].setEnabled(false);
                            stop[j].setEnabled(false);
                            Log.e("button", "making buttons false");
                        }
                    }

                    mPlayer.prepareAsync();
                    final ProgressDialog progressDialog;
                    progressDialog = ProgressDialog.show(getActivity(), "", "Fetching files...");
                    mPlayer.setOnPreparedListener(new OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mp) {
                            //Now dismis progress dialog, Media palyer will start playing
                            progressDialog.dismiss();
                            mp.start();
                            Log.e("button", "playing ");
                        }
                    });
                }
            });

            stop[i].setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Log.e("button", "stop clicked ");
                }
            });

I'm getting this:
E/MediaPlayer(8362): error (1, -1004)
E/MediaPlayer(8362): Error (1,-1004)

url = "http://******.******.ie/music/Mark%20Beswick%20-%20My%20Help.mp3";
What I am doing wrong, please?
Btw I'm testing on a 4.1.1 Android, could this be the problem?
Answer: This is the solution:
<permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.INTERNET" />

<permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Thanks to the first answer on this question - Play SoundCloud Track
Answer: The error code 1004 bothered me in some devices too. Finally, I encoded the URL and solved it!
url = "http://******.******.ie/" + Uri.encode("music/Mark Beswick - My Help.mp3");

Android MediaPlayer Error -1004


How to store pyc files with setuptools3 in bitbake recipe in the same way setuptools does?

Question: I added the python-helloworld from https://github.com/yoctocookbook/meta-custom/tree/master/recipes-python/python-helloworld to my yocto build, which works fine. The files get precompiled and are copied to the image:
ls /usr/lib/python2.7/site-packages/helloworld
__init__.py   __init__.pyc  main.py       main.pyc

I need to use python3. Changing inherit setuptools to inherit setuptools3 in the recipe adds python 3.7 instead 2.7 to my image and the script works, but the precompiled files are now in the subfolder __pycache__ with an extension to their name:
ls /usr/lib/python3.7/site-packages/helloworld/__pycache__/
__init__.cpython-37.pyc  main.cpython-37.pyc

I want to remove the .py source code files from the target build.
With setuptools I can delete the .py files and the script works fine using the .pyc files. With setuptools3, python3 raises a ModuleNotFoundError after deleting the .py files.
Moving and renaming (like __pycache__/main.cpython-37.pyc to main.pyc) it works again.
What is the correct way to tell setuptools3 to create the precompiles .pyc files in the same way as setuptools does to avoid this moving and renaming?
Comment: read through https://www.python.org/dev/peps/pep-3147/ you seem to be relying on a legacy behaviour see https://www.python.org/dev/peps/pep-3147/#case-4-legacy-pyc-files-and-source-less-imports but case 3 is normal behavior https://www.python.org/dev/peps/pep-3147/#case-3-pycache-foo-magic-pyc-with-no-source
Comment: This answers my question how to get setuptools3 to create these .pyc files: It should not be possible because having .pyc files without source works, but is legacy and thus should be avoided.
Can you please post this as an answer instead of a comment? I would like to mark it as solved.
Answer: When reading through pep-3147 you seem to be relying on a legacy case 4 behavior but case 3 is normal behavior, I would suggest to not rely on legacy behavior as it is supposed to go away.


How to give json values from array to dictionary?

Question: I have array inside its stored json values.when i try to give this value to dictionary its given error.
 arrhole{
        "cust_groups" = "";
        "customer_id" = 1;
        "customer_name " = "";
        "date_added" = "<null>";
        "delivery_method" = "";
        fname = kishore;
        gift = "";
        lname = kumar;
        message = "";
        month = "";
        "order_id" = 1;
        "pay_method" = "";
        "payment_firstname" = "";
        "payment_lastname" = "";
        phone = 9043563659;
        region = "";
        reward = "";
        "scheduled_date" = "28/08/2015";
        "sen_email" = "";
        "sen_name" = "";
        "shipping_city" = "";
        "shipping_company" = "";
        "shipping_country" = IND;
        "shipping_region" = 1503;
        "unit_price" = 22;
        voucher = "";
    }

This code i have used:
 -(void)responseFunction:(NSMutableDictionary *)response
    {
        NSLog(@"response method%@",response);
        BOOL success;
        success =[[response objectForKey:@"success"] boolValue];
        arrHoleOrderDetails =[response objectForKey:@"order"];
        NSLog(@"arrhole%@",arrHoleOrderDetails);
        if(success)
        {
            for (NSDictionary *dic in arrHoleOrderDetails)
            {

                NSLog(@"dic===%@",dic);
            }
        }

OUTPUT:
dic===city
 dic===shipping_address
 dic===shipping_lastname
 dic===pay_method
 dic===customer_id
 dic===country
 dic===region

like this its displaying in my log.i displaying here only some values.
1.Is it json response problem?
2.or its my silly mistake?
Full Response:
 order =     {
        address = "";
        address1 = "";
        address2 = "";
        affliate = "";
        amount = "";
        city = "";
        comment = "";
        company = "";
        "company_id" = "";
        country = "";
        coupon = "";
        "cust_groups" = "";
        "customer_id" = 2;
        "customer_name " = "";
        "date_added" = "<null>";
        "delivery_method" = "";
        email = "";
        fax = "";
        fname = arun;
        gift = "";
        lname = "";
        message = "";
        month = "";
        "order_id" = 2;
        "pay_method" = "";
        "payment_firstname" = "";
        "payment_lastname" = "";
        phone = "";
        postcode = "";
        product = 4;
        quantity = 5;
        "rec_email" = "";
        "rec_name" = "";
        region = "";
        reward = "";
        "scheduled_date" = "28/08/2015";
        "sen_email" = "";
        "sen_name" = "";
        "shipping_address" = "";
        "shipping_address1" = "";
        "shipping_address2" = "";
        "shipping_city" = "";
        "shipping_company" = "";
        "shipping_country" = "";
        "shipping_firstname" = "";
        "shipping_lastname" = "";
        "shipping_postcode" = "";
        "shipping_region" = "";
        status = "";
        stores = "";
        total = "";
        "unit_price" = 540;
        voucher = "";
    };
    success = 1;
}

Comment: What do you exactly want? because you have array of strings
Comment: How do you want to treat this arrHoleOrderDetails array?
Comment: That is my json response i have stored in array,and i need to show that in label,so i am passing the values from array to dictionary ,but i mentioned output above like that i get.
Comment: Actually in json response, you get dictionary named "order".
Comment: yes correct.... @iPhone
Comment: You didn't give us full response - there is no "success" key in your dictionary in question and also no "order" key. Also you didn't show output of NSLog(@"arrhole%@",arrHoleOrderDetails);
Comment: Have you only this response from json or you have more than 1 array in json response?
Comment: only this response i have in this json @iPhone
Comment: @GrzegorzKrukowski i gave that as per your suggestion please verify that.
Comment: your Json Response Format is wrong bro..
Comment: kindly mention whats wrong in the json response bro i need to explain it @SandyPatel
Comment: i have little change {
        "cust_groups" : "",
        "customer_id" : 1,
        "customer_name " : ""

  },
Answer: Try following code
   -(void)responseFunction:(NSMutableDictionary *)response
        {
            NSLog(@"response method%@",response);
            BOOL success;
            success =[[response objectForKey:@"success"] boolValue];
            arrHoleOrderDetails =[response objectForKey:@"order"];
            NSLog(@"arrhole%@",arrHoleOrderDetails);
            if(success)
            {
                yourlable.text = [arrHoleOrderDdetails valueForKey:@"address"];
                yourlabel2.text = [arrHoleOrderDdetails valueForKey:@"address1"];

//In case of integer value try following
yourlbl3.text = [NSString stringWithFormat:@"%d",[arrHoleOrderDdetails valueForKey:@"address1"]];

    //go ahead in same manner.

    }

Comment: kindly explain whats wrong in my json format ,because i need to inform it bro @iphone
Comment: If you have any integer value then convert it in string.
Comment: Actually you have array of strings not dictionary so no need to use for loop. You can directly assigned these values to your UILabel
Comment: but convert your integer value in string otherwise it will crash your app
Comment: its working for me ...but i like to know whats problem in previous code.
Comment: Actually it was string in your array not dictionary in your array and you were trying to fetch dictionary from your array
Comment: so if i have multiple array values inside json then i have to go for dictionary ,i am correct
Comment: In your order dictionary, currently 1 array is there but if you have multiple array then you have to use for loop but in your case that structure didn't work. At that time json response structure will be different.
Answer: That should work:
if(success)
{
    //display all keys and values
    for (NSString* key in [arrHoleOrderDetails allKeys])
    {
        NSString* value = [arrHoleOrderDetails objetForKey:key];
        NSLog(@"%@===%@",key, value);
    }

    //access single value
    NSLog(@"%@", arrHoleOrderDetails[@"scheduled_date"]);
}

If you iterate on NSDictionary, you need to iterate on keys and get values for given key.
Also remember that NSDictionaries are access randomly - not having any order.


Unable to understand a solved problem: Impulse & Impulsive force.

Question: Why the acceleration $f$ of $m$ to the attached solution of the problem has been taken as $\frac{m-M}{m+M}g$.
Answer: Since masses $m$ and $M$ are pulling in opposite directions, force acting on the system is $F = (m-M)g$ (these are just gravitational forces). The mass of the system is $m+M$, so by Newton's second law we have $$(m+M)f = (m-M)g$$
Comment: Also, $m > M$ seems as dubious as $\varepsilon < 0$, but I'm not a physicist.


Find the farest point from a vertice in a cube with in a specified straight line.

Question: In a specific scenario there is a cube with 1 unit of size in each axis. One of the vertices of this cube is placed in the origin point and all others points are in the positive area of each axis like that:

Some definitions:

$A$ is a vertex of the $cube$ with the coordinates $(a_x, a_y, a_z)$.
$G$ is a point belonging to the cube (inside or tangent to the cube surface) with the coordinates $(g_x, g_y, g_z)$.
$r$ the straight that passes in $A$ and $G$.
$F$ is the farest point from $A$ that belongs to $r$ and the $cube$ at the same time.

Check the draw bellow:

Question:
How can I find the F point?
Comment: Where is $B$ in your drawing? Did you mean $G$?
Comment: Yes B = G, sorry, I will update.
Answer: Somewhat generically:
a line through two points $A$ and $B$ has parametric formula $f(t) = A + (B-A)t$.
A plane of form $C\cdot x=k$ and this line meet at $C\cdot(A+(B-A)t)=k$, which can be transformed to $$t=\frac{k - C\cdot A}{C\cdot(B-A)}$$.
For a cube like this, the planes are $-x=0$, $-y=0$, $-z=0$, $x=1$, $y=1$, and $z=1$.  In order to find the intersections between the cube and the line, we will find the intersections of these planes with the line, as $t$ values.  Then, sort the planes and $t$ values by the $t$ values; if every dimension the line moves in appears once in the first half of the list, then the line passes through the cube and the line enters the cube at the $t$ value just before the midway point on the list and exits the cube at the $t$ value just after.
This last trick only works if you know that every face in your polytope has a parallel counterpart; otherwise you have to check whether a point at $t=-\infty$ gives $C\cdot(A+(B-A)t)<k$ or not for each face plane -- $<$ means you start on the "inside" side, $>$ means outside -- and then scan through the sorted list of $t$ values and planes to see whether all out-in transitions happen before any in-out transitions; the last out-in transition is where the line enters the polytope, the first in-out transition is where the line exits.
In your case, you will find that three of the $t$ values are $0$, and the other three are positive.  The smallest positive $t$ value gives the point where the line exits the cube.


Calculation of residual standard deviation and r-squared

Question: Related questions here and here (but not answered that satisfactorily in my view).
From Gelman and Hill, Q3.2:

Suppose that, for a certain population, we can predict log earnings
from log height as follows:

A person who is 66 inches tall is predicted to have earnings of    $30,000.
Every increase of 1% in height corresponds to a predicted    increase of 0.8% in earnings.
The earnings of approximately 95% of people fall within a factor of 1.1 of predicted values.

(a) Give the equation of the regression line and the residual standard
deviation of the regression.
(b) Suppose the standard deviation of log heights is 5% in this
population. What, then, is the R2 of the regression model described
here?

I can calculate the equation of the regression line easily enough using the data given and: $$\text{log}(y) = \alpha + \beta \, \text{log}(x)$$
where $y$ is earnings and $x$ is height.
I then took: $$\text{log}(1.1)/1.96 \approx 0.0486$$ to be the residual standard deviation.
I think this then gives $$R^2 = 1 - \frac{0.0486^2}{0.05^2} \approx 0.0541$$
which I think is wrong?
Questions: what are the correct residual standard deviation and $R^2$?
NB: the question in the book for part (b) says log heights but I think that's a typo and they meant log earnings.
Answer: I think that making the error term $\epsilon$ explicit would help. In log terms we would have:
$$\log(y) = \alpha + \beta \log(x) + \epsilon.$$
Taking exponents, the actual model would look like the following:
$$ y = e^{\alpha} x^\beta e^{\epsilon}.$$
Note that whenever the question talks about a log variable we should refer to the first model and otherwise to the second. Hence, the third point:

The earnings of approximately 95% of people fall within a factor of
1.1 of predicted values.

refers to the second model, where, as you can see, the error term $e^\epsilon$ is no longer gaussian due to exponentiation, making the 95%-1.96$\sigma$ rule not valid. Instead, what the sentence is saying is that
95% percent of the values of $e^\epsilon$ are below 1.1, or equivalently that the CDF value at 0.95 is 1.1. If $\epsilon$ is normal, $e^\epsilon$ is log-normal with parameters 0 and $\sigma_\epsilon$, the latter being the one you can infer from the information about the CDF. Unfortunately, it's not straightforward to do this analytically (due to the form that the aforementioned CDF takes) which makes me doubt whether all of this is correct (either the question or my intrepretation/answer).


Redirect to home after NODE_ACCESS_DENY

Question: My D7 customer uses the Workbench suite of modules to use taxonomy to allow fine-grained access to nodes successfully. But with a large amount of data to manage, sometimes they want to unpublish a node for the time being while keeping the data available in case they want to publish again. In Workbench, I've set up an "Archived" option which successfully renders that content unavailable to anonymous users. So when anonymous users visit that node, workbench, using hook_node_access() renders that node to NODE_ACCESS_DENY and it appears Drupal takes over and redirects the user to /user. That's good, but the customer wants to redirect back to root if they are anonymous.
I thought about modifying Workbench using their API but it seems to be after stepping through how Drupal handles the menu_execute_handler() that the redirection to /user occurs. I need workbench to return the DENY so I can use that as one condition to dynamically alter how Drupal renders the view in a DENY situation. But I haven't found which part of core handles this redirection.
Hunting through searches I found this suggestion which suggests to handle all 403s.
<code>function my_module_menu() {
  $items['403redirect'] = array(
    'page callback' => 'mymodule_unpublished_redirect',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
    );
return $items;
}

function mymodule_unpublished_redirect() {
    if (<TRUE>) {
        header('location:'. url('/', array('absolute' => TRUE)), TRUE, 302);
    }
}
</code>
But when visiting the node that is "Archived" redirects to /user which suggests that this function is ignored at this point. So how should I alter this redirect?
Comment: "For this to work, you have to set the 403 page under Administer > Site Configuration > Error Reporting" (from the linked suggestion) Did you do that?
Comment: That points me to what is likely the solution. In that dialog I can point to a URL that will be in this custom module and can make a better callback in deciding how to handle the redirect. If you would form into an answer, I'll choose it. Thx.
Answer: The idea of creating a menu entry to redirect in the case of a 403 is fine, except that you need to configure the core handler to point to it, which is the element missing from your question (it's mentioned in the comment you linked to).
Once you have defined your menu entry go to Administer > Site Configuration > Error Reporting and set the value of the 403 page to the corresponding path.
Now the page callback for your menu entry will be invoked whenever there is access denied, so you can supply the appropriate logic for your needs.
Comment: For Drupal 8 and 9, Admin Panel -> Configuration -> System -> Basic site settings.

Redirect to home after NODE_ACCESS_DENY


Wordpress Network per site Custom URL Slugs

Question: I'm attempting to setup a Multisite Wordpress Network, but one of the requirements is that each site needs to have their own custom slug in the URL. For example:
<code>http://www.site1.com/blog/
http://www.site2.com/reviews/
http://www.site3.com/commentary/
</code>
Currently, so far as I can tell, there is no way of tailoring a per site slug. Domain Mapping is simple using the plugin Wordpress MU Domain Mapping (It's compatible with Wordpress 3+ Networks.) But what I really need is the added ability of those custom slugs. Has anyone seen anything of that sort?
I'll revert to a large collection of individual Wordpress Instances if I have to, but this network setup would be the perfect solution if I could get the slugs to work.
Answer: The slugs have nothing to do with your multisite setup. Look into modifying each site's permalink structure instead.
UPDATE
Maybe try to write your own rewrite rules?
Comment: Unfortunately unique per site rewrites won't work with Network sites. Subdomain networks are forced to root '/' and even if you make an elaborate rewrite to try and force people to use a '/reviews/', etc, the Wordpress links will try and force you back to the root again. The solution has to be supported internally within Wordpress as well.
Comment: Maybe try to write your own [rewrite rules](http://codex.wordpress.org/Class_Reference/WP_Rewrite)?
Comment: Hmm. Hadn't seen that section. Is definitely the best potential yet. Thanks!
Comment: Unfortunately these don't override the built in references to point the URL to the root. Thus while the initial connection using the rewrite works (ie, connecting to http://www.site2.com/reviews/) clicking onto any of the links themselves send you back to the root for the URL.
Answer: The resulting research is that you can't do what I was after with the default build of Wordpress using the Multisite features and the Wordpress MU Domain Mapping.
The arrangement can work sub-directory separation of sites, but the only way to allow the sub-directory to exist for domain mapped addresses is to build an entirely new plugin that does the domain mapping with support for that, or to modify the Wordpress MU Domain Mapping plugin so that it has the support for it, effectively creating a new plugin out of it.


Python replace substring given pattern

Question: I am doing some data cleansing and want to remove the whole string between the characters:
"
kódy:" and "
" The strings are located in a dataframe and for each record, different characters can be found between the two characters, so I thought using combining str.place or re.sub with some kind of a wilcard could work, but I've not been succesful.
This is my sample input:

kódy: 2008212017 2008212025 2008212041 2008212066 2008212074 2008212108 2008212116 2008212124 2008212132 2008212140 2008212165 2008212199 2008212207 2008212215 2008212223 2008212231 2008212249 2008212256 2008212264 2008212272 2008212314 2008212355 2008212363 2008212389 2052500028 2052500036 2052500051 2052500069 2052500093 2052500101 2054384017 2054384041 2054384066 2054384090 2054384116 2054384124 2054384132 2054384140 2054384157 2054384165 2054384181 2054384199 2054384207 2054384215 2054384223 2054384249 20543842494 2054384348 2081043032 2081043057 2081043081 2081043214 2081043222 311088575007 311095577004 311095711009 4210013769006 62008212110

 924071180 924071181 924071182

And the desired output:

 924071180 924071181 924071182

Any help would be appreciated!
Cheers,
Stepan
Comment: Does this answer your question? [How to delete the words between two delimiters?](https://stackoverflow.com/questions/8784396/how-to-delete-the-words-between-two-delimiters)
Comment: what part's do you want to remove and what parts do you want to keep?
Comment: I assume that the missing periods in the desired output vs the input data is a mistake?  You don't say anything about removing the periods in your description.
Comment: @CryptoFool you are correct, thanks for pointing out.
Comment: @ShoaibWani the whole string between the two characters. I will be more specific in the description, thanks.
Answer: You can use a regular expression substitution to get what you want in a single call:
data = """
kódy: 2008212017 2008212025 2008212041 2008212066 2008212074 2008212108 2008212116 2008212124 2008212132 2008212140 2008212165 2008212199 2008212207 2008212215 2008212223 2008212231 2008212249 2008212256 2008212264 2008212272 2008212314 2008212355 2008212363 2008212389 2052500028 2052500036 2052500051 2052500069 2052500093 2052500101 2054384017 2054384041 2054384066 2054384090 2054384116 2054384124 2054384132 2054384140 2054384157 2054384165 2054384181 2054384199 2054384207 2054384215 2054384223 2054384249 20543842494 2054384348 2081043032 2081043057 2081043081 2081043214 2081043222 311088575007 311095577004 311095711009 4210013769006 62008212110

 924071180 924071181 924071182

"""

import re

r = re.sub(r"
kódy:.+?
", "", data)

print(r)

Result:

 924071180 924071181 924071182

Comment: That seems promising, is there any possibility to apply this over the whole dataframe column? Thanks!
Answer: You can use split.
st = "
kódy: 2008212017 2008212025 2008212041 2008212066 2008212074 2008212108 2008212116 2008212124 2008212132 2008212140 2008212165 2008212199 2008212207 2008212215 2008212223 2008212231 2008212249 2008212256 2008212264 2008212272 2008212314 2008212355 2008212363 2008212389 2052500028 2052500036 2052500051 2052500069 2052500093 2052500101 2054384017 2054384041 2054384066 2054384090 2054384116 2054384124 2054384132 2054384140 2054384157 2054384165 2054384181 2054384199 2054384207 2054384215 2054384223 2054384249 20543842494 2054384348 2081043032 2081043057 2081043081 2081043214 2081043222 311088575007 311095577004 311095711009 4210013769006 62008212110

 924071180 924071181 924071182
"
st.split('
')

Result:
['
kódy: 2008212017 2008212025 2008212041 2008212066 2008212074 2008212108 2008212116 2008212124 2008212132 2008212140 2008212165 2008212199 2008212207 2008212215 2008212223 2008212231 2008212249 2008212256 2008212264 2008212272 2008212314 2008212355 2008212363 2008212389 2052500028 2052500036 2052500051 2052500069 2052500093 2052500101 2054384017 2054384041 2054384066 2054384090 2054384116 2054384124 2054384132 2054384140 2054384157 2054384165 2054384181 2054384199 2054384207 2054384215 2054384223 2054384249 20543842494 2054384348 2081043032 2081043057 2081043081 2081043214 2081043222 311088575007 311095577004 311095711009 4210013769006 62008212110', '
 924071180 924071181 924071182 ', '']

Or:
import re
t = re.sub('
kódy.*?
', '', st)

Result:
'
 924071180 924071181 924071182
'

Python replace substring given pattern


Why do regression coefficients change when excluding variables?

Question:
Possible Duplicate:
How exactly does one "control for other variables"?

In my linear model fitted.model <- lm(spending ~ sex + status + income, data=spending), my results were as follows:
Coefficients:
                       Estimate  Std. Error t value   Pr(>|t|)
(Intercept)    22.55565   17.19680   1.312   0.1968
sex         **-22.11833**  8.21111  -2.694   0.0101 *
status          0.05223    0.28111   0.186   0.8535
income          4.96198    1.02539   4.839 1.79e-05 ***
verbal         -2.95949    2.17215  -1.362   0.1803

Now, when I held sex and all other predictors constant in new lm model
mydata<-lm(spending ~ sex, data=spending) my coefficient was
Coefficients:
            Estimate Std. Error t value Pr(>|t|)
(Intercept)   29.775      5.498   5.415 2.28e-06 ***
sex        **-25.909**    8.648  -2.996  0.00444 **

Questions: Should the sex value of -22.118 be the same in my new model? Because I now get -25.909. Since I am holding all constant, I should get a different value, please clarify?...
Comment: Even if the same data (response variable values) are used in two models removing covariates could change coefficients simply because a covariate removed is correlated with one that remains.  I believe that this can be avoided if the covariates can be constructed to be orthogonal such as with orthogonal polynomials.
Comment: Your question is related to this CV question: [How exactly does one "control for other variables"?](http://stats.stackexchange.com/questions/17336/how-exactly-does-one-control-for-other-variables). It is explained why regression coefficients change when we include other variables (which is also referred to as "controlling for").
Comment: Your two models appear to involve two dataset names, `dataset` and `spending`. If the data differ, then *of course* you should expect the results to differ! Please, then, explain the relationship between those two datasets.
Comment: Actually the data set is the same, spending. I typed the wrong name of the dataset. I needed to determine the difference in predicted spending for a male vs. a female with other variables held constant. But i was questioning if the value of the sex in my 1st model using all constants should be the same value when one variable and all other constants.
Comment: @berndWeiss Good reference to a duplicate or near duplicate. I gave an answer to the OPs specific question but the responses in the link should give the OP a deeper understanding of what is going on and what the issues are.  Maybe it would be appropriate to close this question.
Answer: The answer must be that the variables are correlated.  One model uses sex and income which are eboth very significant and two other variables that appear to be non-significant. The other model uses only sex as a covariate. The coefficients for the intercept and sex both change a little and the intercept becomes significant when income and the other variables are removed.  This should not be a surprise.  I suspect that a lot of this change is due to removing income which is an important variable in the model. For the full model you should look at the sample covariance matrix and I am sure you will see that some of these variables are correlated and probably at least one with sex.  Check that out.
As I mentioned in my comments the variables need to be uncorrelated for the coefficients to be guaranteed not to change when other covariate are removed as in the case when the variables are polynomial terms that are orthogonal.
Comment: Thanks for the information and I will read the sections indicated above. I'm new to R so I dont know how to do a covariance matrix but I will look in related topics.


Can't capture google search URL using jnetpcap

Question: I am developing an application to capture all HTTP urls. I used pcap.LOOP_INFINATE for persistent capturing, but when i search on google, it does not capture any packets. Kindly give me a solution for this.
This is my sample code:
<code>int snaplen = 64 * 1024;
int flags = Pcap.MODE_PROMISCUOUS;
int timeout = 10*1000;
Pcap pcap =   Pcap.openLive(device.getName(), snaplen, flags, timeout, errbuf);

PcapPacketHandler<String> jpacketHandler = new PcapPacketHandler<String>() {
    final Tcp tcp = new Tcp();

    final Ip4 ip = new Ip4(); Arp arp=new Arp();Payload payload = new Payload();
    Ethernet eth=new Ethernet();Http http = new Http();Udp udp=new Udp();

    public void nextPacket(PcapPacket packet, String user) {

        if (packet.hasHeader(tcp) && packet.hasHeader(http)) {

            if(tcp.destination() == 80) {

                if(http.hasField(Http.Request.Accept) && http.fieldValue(Http.Request.Accept).contains("text/html"))
                {

                    String host = http.fieldValue(Http.Request.Host);
                    String url = host + http.fieldValue(Http.Request.RequestUrl);
                    String referer =  http.fieldValue(Http.Request.Referer);
                    System.out.println("Request: " +" - " + url);
                }
            }
        }
        }
};
pcap.loop(pcap.LOOP_INFINATE,jpacketHandler, null);
</code>
Comment: google search uses https, e.g. port 443 and not port 80. And it is encrypted, so you will not get usable data by simple packet capture.
Answer:
google search uses https, e.g. port 443 and not port 80. And it is encrypted, so you will not get usable data by simple packet capture
-- Steffen Ullrich
Comment: Dont just copy someones comment and put it as an answer
Comment: Probably a question for meta, but, @Chausser ... why not? Steffan has enough history here to know that comments are ephemeral, and that if he wants rep, he should answer. Hell, you don't need rep to answer, but you do, to comment. What would be wrong is to copy an answer, and post it as a comment. The right place for this information is as an answer.
Comment: Because copying someone else comment and posting it as an answer is doing so only to possibly gain reputation. SO is meant to be a quality question and answer service. By simply "moving" information in a comment to an answer you are not providing any additional substance. It is also a possibility that he posted it as a comment and not as an answer for other reasons. There is a post in meta that i cant seem to find right now that basically says rep only actions are frowned upon and to only do things that add quality and substance to the site.
Comment: @Chausser - and all meaningful information in any comments should be edited back in to answers, or the question, and not left in comments.... the attribution is appropriate, I agree.
Comment: And i agree, the problem i have with it is that it is a straight rip. With nothing else added. If someone feels that a comment answers the question then suggest the that the person who posted the comment move it. Or if you are going to use it yourself to answer the question add your own substance to go along with someone else work
Comment: @Chausser If someone posts a comment instead of an answer, then it is perfectly fine for someone to copy that content and post it as an answer as long as attribution is provided - which was done via an edit.


Understanding p-value

Question: I know that there are lots of materials explaining p-value. However the concept is not easy to grasp firmly without further clarification.
Here is the definition of p-value from Wikipedia:

The p-value is the probability of obtaining a test statistic at least
  as extreme as the one that was actually observed, assuming that the
  null hypothesis is true. (http://en.wikipedia.org/wiki/P-value)

My first question pertains to the expression "at least as extreme as the one that was actually observed." My understanding of the logic underlying the use of p-value is the following: If the p-value is small, it's unlikely that the observation occurred assuming the null hypothesis and we may need an alternative hypothesis to explain the observation. If the p-value is not so small, it is likely that the observation occurred only assuming the null hypothesis and the alternative hypothesis is not necessary to explain the observation. So if someone wants to insist on a hypothesis he/she has to show that the p-value of the null hypothesis is very small. With this view in mind, my understanding of the ambiguous expression is that p-value is $\min[P(X<x),P(x<X)]$, if the PDF of the statistic is unimodal, where $X$ is the test statistic and $x$ is its value obtained from the observation. Is this right? If it is right, is it still applicable to use the bimodal PDF of the statistic? If two peaks of the PDF are separated well and the observed value is somewhere in the low probability density region between the two peaks, which interval does the p-value give the probability of?
The second question is about another definition of p-value from Wolfram MathWorld:

The probability that a variate would assume a value greater than or
  equal to the observed value strictly by chance.
  (http://mathworld.wolfram.com/P-Value.html)

I understood that the phrase "strictly by chance" should be interpreted as "assuming a null hypothesis". Is that right?
The third question regards the use of "null hypothesis". Let's assume that someone wants to insist that a coin is fair. He expresses the hypothesis as that relative frequency of heads is 0.5. Then the null hypothesis is "relative frequency of heads is not 0.5." In this case, whereas calculating the p-value of the null hypothesis is difficult, the calculation is easy for the alternative hypothesis. Of course the problem can be resolved by interchanging the role of the two hypotheses. My question is that rejection or acceptance based directly on the p-value of the original alternative hypothesis (without introducing the null hypothesis) is whether it is OK or not. If it is not OK, what is usual workaround for such difficulties when calculating the p-value of a null hypothesis?

I posted a new question that is more clarified based on the discussion in this thread.
Comment: You have caught a subtlety that often goes unrecognized: "more extreme" needs to be measured in terms of *relative likelihood of the alternative hypothesis* rather than in the obvious (but not generally correct) sense of being further out in the tail of the null sampling distribution. This is explicit in the formulation of the [Neyman-Pearson Lemma](http://en.wikipedia.org/wiki/Neyman%E2%80%93Pearson_lemma), which is used to justify many hypothesis tests and to determine their critical regions (and whence their p-values). Thinking this through will help answer your first question.
Comment: Of possible interest: [Is there an error in the one-sided binomial test in R?](http://stats.stackexchange.com/q/44125/10525)
Comment: As I recall, the Neyman-Pearson Lemma is optimal for simple vs. simple hypothesis tests (Ho: mu=mu_0, Ha: mu=mu_a). For composite tests (Ho: mu=mu_0, Ha: mu>mu_a) there is an alternative test.
Answer: First answer
You have to think at the concept of extreme in terms of probability of the test statistics, not in terms of its value or the value of the random variable being tested. I report the following example from
Christensen, R. (2005). Testing Fisher, Neyman, Pearson, and Bayes. The American Statistician, 59(2), 121–126
$$
\phantom{(r\;|\;\theta=0}r\; | \quad 1 \quad \quad 2 \quad \quad 3 \quad \quad 4\\
p(r\;|\;\theta=0) \; |\; 0.980\;0.005\; 0.005\; 0.010\\
\quad p\;\mathrm{value} \; \; | \;\; 1.0 \quad 0.01 \quad 0.01 \;\; 0.02
$$
Here $r$ are the observations, the second line is the probability to observe a given observation under the null hypothesis $\theta=0$, that is used here as test statistics, the third line is the $p$ value. We are here in the framework of Fisherian test: there is one hypothesis ($H_0$, in this case $\theta=0$) under which we want to see whether the data are weird or not. The observations with the smallest probability are 2 and 3 with 0.5% each. If you obtain 2, for example, the probability to observe something as likely  or less likely ($r=2$ and $r=3$) is 1%. The observation $r=4$ does not contribute to the $p$ value, although it's further away (if an order relation exists), because it has higher probability to be observed.
This definition works in general, as it accommodates both categorical and multidimensional variables, where an order relation is not defined. In the case of a ingle quantitative variable, where you observe some bias from the most likely result, it might make sense to compute the single tailed $p$ value, and consider only the observations that are on one side of the test statistics distribution.
Second answer
I disagree entirely with this definition from Mathworld.
Third answer
I have to say that I'm not completely sure I understood your question, but I'll try to give a few observations that might help you.
In the simplest context of Fisherian testing, where you only have the null hypothesis, this should be the status quo. This is because Fisherian testing works essentially by contradiction. So, in the case of the coin, unless you have reasons to think differently, you would assume it is fair, $H_0: \theta=0.5$. Then you compute the $p$ value for your data under $H_0$ and, if your $p$ value is below a predefined threshold, you reject the hypothesis (proof by contradiction). You never compute the probability of the null hypothesis.
With the Neyman-Pearson tests you specify two alternative hypotheses and, based on their relative likelihood and the dimensionality of the parameter vectors, you favour one or another. This can be seen, for example, in testing the hypothesis of biased vs. unbiased coin. Unbiased means fixing the parameter to $\theta=0.5$ (the dimensionality of this parameter space is zero), while biased can be any value $\theta \neq 0.5$ (dimensionality equal to one). This solves the problem of trying to contradict the hypothesis of bias by contradiction, which would be impossible, as explained by another user. Fisher and NP give similar results when the sample is large, but they are not exactly equivalent. Here below a simple code in R for a biased coin.
n <- 100  # trials
p_bias <- 0.45  # the coin is biased
k <- as.integer(p_bias * n)  # successes

# value obtained by plugging in the MLE of p, i.e. k/n = p_bias
lambda <- 2 * n * log(2) + 2 * k * log(p_bias) + 2 * (n-k) * log(1. - p_bias)

p_value_F <- 2 * pbinom(k, size=n, prob=0.5)  # p-value under Fisher test
p_value_NP <- 1 - pchisq(q=lambda, df=1)  # p-value under Neyman-Pearson
binom.test(c(k, n-k))  # equivalent to Fisher

Comment: +1 for pointing out a great article I didn't know about. (Also for some much needed skepticism about the utility of Mathworld's view of statistics).
Comment: @Zag, I disagree rather with this answer: you don't _have_ to think of the concept of extreme in terms of probability.  Better to say that _in this example_ the probability under the null is being used as the test statistic - but that's not mandatory.  For example, if the likelihood ratio, as mentioned by whuber, is used as a test statistic, it will not in general put possible samples in the same order as will probability under the null.  Other statistics are chosen for maximum power against a specified alternative, or all alternatives, or for high power against a vaguely defined set.
Comment: Thank you very much! So the p-value is \int_{x : f(x) <= k} f, where f is the PDF of a test statistic and k is the observed value of the statistic. Thank you again.
Comment: Regarding the third answer, what is proved in your answer is unfairness of the coin because fairness assumption is rejected. On the contrary, to prove fairness of the coin by contradiction, I have to assume unfairness \theta \neq 0.5 and calculate p-value of my data. How can I do it? My point is the difficulty originated from the \neq sign of the unfairness assumption. Do I have to introduce some tolerance level for fairness, say 0.4 < \theta < 0.6, and calculate p-value in terms of \theta and integrate it over 0 < \theta < 0.4 and 0.6 < \theta < 1 ?
Comment: One more question. This [link](http://alturl.com/ud6ov) explains "one-sided" p-value. It says one-sided p-value answers questions like "null hypothesis, that two populations really are the same ... what is the chance that randomly selected samples would have means as far apart as (or further than) observed in this experiment with the specified group having the **larger** mean?" Is it an appropriate use of one-sided p-value? I think the null hypothesis itself should be expressed as an inequality in this case (instead of equality and one-sided test).
Comment: @Scortchi, I see your point and I edited the answer to address it. I hope it's more clear now. Thanks for pointing my attention to it. I also added more material on NP test.
Comment: @JDL (re your comment on Qn3), yes, you would introduce a null like $\theta<0.4\ \vee\ \theta>0.6$. But you wouldn't integrate over $\theta$; you'd calculate for any given sample the probability under the nearest null. Integration over $\theta$ would only make sense if you had a probabilty distribution over $\theta$, which you don't in the frequentist paradigm.
Answer: (1) A statistic is a number you can calculate from a sample.  It's used to put into order all the samples you might have got (under an assumed model, where coins don't land on their edges & what have you).  If $t$ is what you calculate from the sample you actually got, & $T$ is the corresponding random variable, then the p-value is given by
$\newcommand{\pr}{\mathrm{Pr}}
\pr\left(T\geq t\right)$ under the null hypothesis, $H_0$.
'Greater than' vs 'more extreme' is unimportant in principle. For a two-sided test on a Normal mean we could use
$\pr(|Z|\geq |z|)$ but it's convenient to use
$2\min [\pr(Z\geq z),\pr(Z\leq z)]$
because we have the appropriate tables. (Note the doubling.)
There's no requirement for the test statistic to put the samples in order of their probability under the null hypothesis.  There are situations (like Zag's example) where any other way would seem perverse (without more information about what $r$ measures, what kinds of discrepancies with $H_0$ are of most interest, &c.), but often other criteria are used.  So you could have a bimodal PDF for the test statistic & still test $H_0$ using the formula above.
(2) Yes, they mean under $H_0$.
(3) A null hypothesis like "The frequency of heads is not 0.5" is no use because you would never be able to reject it.  It's a composite null including "the frequency of heads is 0.49999999", or as close as you like.  Whether you think beforehand the coin's fair or not, you pick a useful null hypothesis that bears on the problem.  Perhaps more useful after the experiment is to calculate a confidence interval for the frequency of heads that shows you either it's clearly not a fair coin, or it's close enough to fair, or you need to do more trials to find out.
An illustration for (1):
Suppose you're testing the fairness of a coin with 10 tosses. There are $2^{10}$ possible results. Here are three of them:
$\mathsf{HHHHHHHHHH}\\
\mathsf{HTHTHTHTHT}\\
\mathsf{HHTHHHTTTH}$
You'll probably agree with me that the first two look a bit suspicious.  Yet the probabilities under the null are equal:
$\mathrm{Pr}(\mathsf{HHHHHHHHHH}) = \frac{1}{1024}\\
\mathrm{Pr}(\mathsf{HTHTHTHTHT}) = \frac{1}{1024}\\
\mathrm{Pr}(\mathsf{HHTHHHTTTH}) = \frac{1}{1024}$
To get anywhere you need to consider what types of alternative to the null you want to test.  If you're prepared to assume independence of each toss under both null & alternative (& in real situations this often means working very hard to ensure experimental trials are independent), you can use the total count of heads as a test statistic without losing information. (Partitioning the sample space in this way is another important job that statistics do.)
So you have a count between 0 and 10
t<-c(0:10)

Its distribution under the null is
p.null<-dbinom(t,10,0.5)

Under the version of the alternative that best fits the data, if you see (say) 3 out of 10 heads the probability of heads is $\frac{3}{10}$, so
p.alt<-dbinom(t,10,t/10)

Take the ratio of the probability under the null to the probability under the alternative (called the likelihood ratio):
lr<-p.alt/p.null

Compare with
plot(log(lr),p.null)

So for this null, the two statistics order samples the same way. If you repeat with a null of 0.85 (i.e. testing that the long-run frequency of heads is 85%), they don't.
p.null<-dbinom(t,10,0.85)
plot(log(lr),p.null)

To see why
plot(t,p.alt)

Some values of $t$ are less probable under the alternative, & the likelihood ratio test statistic takes this into account.  NB this test statistic will not be extreme for
$\mathsf{HTHTHTHTHT}$
And that's fine - every sample can be considered extreme from some point of view.  You choose the test statistic according to what kind of discrepancy to the null you want to be able to detect.
... Continuing this train of thought, you can define a statistic that partitions the sample space differently to test the same null against the alternative that one coin toss influences the next one.  Call the number of runs $r$, so that
$\mathsf{HHTHHHTTTH}$
has $r=6$:
$\mathsf{HH}\ \mathsf{T}\ \mathsf{HHH}\ \mathsf{TTT}\ \mathsf{H}$
The suspicious sequence
$\mathsf{HTHTHTHTHT}$
has $r=10$. So does
$\mathsf{THTHTHTHTH}$
while at the other extreme
$\mathsf{HHHHHHHHHH}\\
\mathsf{TTTTTTTTTT}$
have $r=1$.  Using probability under the null as the test statistic (the way you like) you can say that the p-value of the sample
$\mathsf{HTHTHTHTHT}$
is therefore $\frac{4}{1024}=\frac{1}{256}$. What's worthy of note, comparing this test to the previous, is that even if you stick strictly to the ordering given by probability under the null, the way in which you define your test statistic to partition the sample space is dependent on consideration of alternatives.
Comment: You say that the definition Pr(T \ge t; H_0) can be applicable to any multimodal (of course, including bimodal) PDF of a test statistic. Then, you and Zag give different p-values for multimodal PDF of a test statistic. IMHO, Zag's definition is more resonable because the role of p-value is to quantify how likely (or weird) the observation is under the null hypothesis, as he pointed. What is your rationale for the definition Pr(T \ge t; H_0) ?
Comment: @JDL, that _just is_ the definition of a p-value. The question then becomes how to find a 'good' test statistic (& how to define 'good'). Sometimes the probability under the null (or any function of the data that gives the same ordering) is used as the test statistic.  Sometimes there are good reasons to choose others, which fill up a lot of space in books on theoretical statistics. I think it's fair to say they involve explicit or implicit consideration of alternatives. ...
Comment: @JDL, ... And if a particular observation has low probability under _both_ null & alternative it seems reasonable _not_ to regard it as extreme.
Comment: Thank you for your answers, @Scortchi. I posted a new question and have seen your comments just now after the posting. Anyway, I'm still not clear about the definition. Thank you again for your kindly answers.
Comment: I added an illustration

Understanding p-value


How can I update content automatically every X hours?

Question: I have a content type that I want updated every five hours. When some nodes haven't been updated, the I'd like to alert a content admin with a warning message containing the list of nodes that have not been updated, e.g. :

[node:title] has not been updated! please update

The nodes can also be displayed in a block
Comment: `The nodes can also be displayed in a block` You should do this with Views as Shawn mentions.
Answer: This can be completely driven by Views & Views System Message module.
You can create a block view for node content of type XXX that has an added Filter Criteria Content: Updated date with the Operator set to Is less than or equal to and the Value type set to an An offset... of "-5 hours" Value. That will get you a block view of content XXX that was edited more than 5 hours ago.
The last part, the alert message, can be added in the Header area by selecting the Global: System message type and setting the parameters to your liking. Once the block view is saved, you can add the block to whatever region you feel most appropriate.


Form state redirect to node path plus fragment

Question: How can I redirect a form to the canonical node path + fragment?
Redirect: /nodepathalias#tag
$form_state->setRedirect('entity.node.canonical',
  array('node' => $node->id()),
));

Where should I add the fragment information?
Answer: Use the third argument of the setRedirect function:

array $options: (optional) An associative array of additional options.
  See \Drupal\Core\Url for the available keys.

And if you check that doc:

@param array $options   (optional) An associative array of additional
  URL options, with the   following elements:

'query': An array of query key/value-pairs (without any URL-encoding)
  to append to the URL.
'fragment': A fragment identifier (named anchor) to append to the URL.
  Do not include the leading '#' character.
'absolute': Defaults to FALSE. Whether to force the output to be an
  absolute link (beginning with http:). Useful for links that will be
  displayed outside the site, such as in an RSS feed.
'attributes': An associative array of HTML attributes that will be
  added to the anchor tag if you use the \Drupal\Core\Link class to make
  the link.
'language': An optional language object used to look up the alias
  for the URL. If $options['language'] is omitted, it defaults to the
  current language for the language type LanguageInterface::TYPE_URL.
'https': Whether this URL should point to a secure location. If not
  defined, the current scheme is used, so the user stays on HTTP or HTTPS respectively. TRUE enforces HTTPS and FALSE enforces HTTP.

In your case you should use just fragment:
$options = [
  'fragment'   => 'tag',
];

$form_state->setRedirect('entity.node.canonical',
  ['node' => 8],
  $options
);

Answer: You must add 'fragment' in third argument of setRedirect method,  $options (@see https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Form%21FormState.php/function/FormState%3A%3AsetRedirect/8.2.x).
You can see how this option is used in https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Url.php/class/Url/8.2.x , in the method toUriString()
Example:
$form_state->setRedirect('entity.node.canonical',
  array('node' => $node->id()),
  ['fragment' => 'tag']
));


Why approximate delta-method Variance isn't multiplied by $\frac{1}{n}$?

Question: I'm reading Casella-Berger chapter 10, where they introduce asymptotic evaluations.
I don't seem quite to understand how the factor $\sqrt{n}$ works when we are using asymptotic evaluations in order to estimate the approximate variance of estimators.
Following their example (on p. 473, here's a screenshot), given that, according to the asymptotic efficiency of MLEs (where $\hat{\theta}$ is a MLE from an iid sample, $\tau(.)$ is continuous and $I(\theta)$ is the information number for a single observation):
$\sqrt{n} \left[ \tau(\hat{\theta}) - \tau(\theta) \right] \rightarrow_d N \left(0, \frac{1}{I(\theta)}\right)$
and that, according to the Delta method, if $\sqrt{n} \left[ \hat{\theta} - \theta \right] \rightarrow_d N\left(0, \sigma^2\right)$, then:
$\sqrt{n} \left[ h(\hat{\theta}) - h(\theta) \right]\rightarrow_d N(0, \sigma^2[h^{'}(\theta)]^2)$
Why is it that (according to Casella-Berger, p. 473), the approximate variance of the estimator is given by:
$Var(h(\hat{\theta})|\theta) \approx \frac{[h^{'}(\theta)]^2}{I_n(\theta)}$
instead of:
$Var(h(\hat{\theta})|\theta) \approx \frac{[h^{'}(\theta)]^2}{I_n(\theta)} \cdot \frac{1}{n}$
I can't understand why, in this example and others (e.g. at p.243, example 5.5.25), the approximate variance of the estimator doesn't get multiplied by the $\frac{1}{n}$ term. Can anyone help me understand? Thanks in advance.
Comment: You are confusing the unit-level and sample-level information. The first expression is you write $\sqrt n\{\tau(\widehat \theta) - \tau(\theta)\} \to N(0,I_n(\theta)^{-1})$ presumes that $I_n$ is the unit-level Fisher information, whereas your screenshot clearly has $I_n(\theta) = E \frac{\partial^2}{\partial \theta^2} L(\theta \mid \mathbf X)$ as the sample-level Fisher information, which by linearity of expectation is $n$ times the unit-level information in this case.
Comment: Yep, that totally eluded me. Thank you so much, now it's clear.
Answer: Briefly, $h(\cdot)$ is a change of variable, and not a statistic. In the assumption of the $\delta$-method, we already have that $\hat{\theta}$ is scaled by $\sqrt{n}$ so that the limiting distribution has a finite, non-zero variance. As a corollary we can say $\text{Var}(\theta) \approx I_n(\theta)^{-1} / n$  for large $n$, and a similar statement can be made about the change of variable, $h(\hat{\theta})$.


Is there a closed form for series of $\sum_k \frac{x^k}{1-x^{2k+1}}$

Question: is there a way to evaluate $\sum_{k=0}^{+\infty} \frac{x^k}{1-x^{2k+1}}$ in terms of popular functions or even in terms of the q-digamma function?
$0<x<1$
I tried to write the denominator as an geometric infinite series. Expression got cuter but didn't get easier. I can't see any integration or trick to work this one.
Wolfram gives me the partial sum in terms of the x-digamma function but it uses complex values in the digamma which makes hard for me to take the limit as I know nothing about this function.
So I gave it a better search in this forum and found that:
$\sum_{k=0}^{\infty} \frac{x^k}{1-x^{2k}} = L(x) - L(x^2)$
where $L(x) = \sum_{k=0}^{\infty} \frac{x^k}{1-x^{k}}$ is the Lambert series of $x$
Answer: Write $$H(x):=\sum_{k=0}^{+\infty} \frac{x^k}{1-x^{2k+1}},$$ and $$G(x):=xH(x^2)=\sum_{k=0}^{+\infty} \frac{x^{2k+1}}{1-x^{4k+2}}.$$
One can then calculate the Taylor expansion of $G$:
\begin{eqnarray*}
G(x) &=& \sum_{k = 0}^{+\infty} \sum_{j = 1\\2\nmid j}^{+\infty}x^{j(2k+1)}\\
&=& \sum_{k = 1\\2\nmid k}^{+\infty}\sum_{j = 1\\2\nmid j}^{+\infty}x^{jk}\\
&=& \sum_{n = 1\\2\nmid n}^{+\infty} \sigma_0(n) x^n,
\end{eqnarray*}
where $\sigma_0(n)$ is the number of divisors of $n$.
Comment: don't worry. I'll take the expansion of the sum as a Lambert series of $x$ because it is close enough of being closed (because of x-polygamma function) and this expansion is easily obtained from $G(x)$
Answer: This is not an answer.
The problem is that morever the functions involved in the expression are extremely expansive to compute.
Just an idea : consider
$$\int_0^\infty \frac{x^k}{1-x^{2k+1}}\,dk=-\frac{\tanh ^{-1}\left(\sqrt{x}\right)}{\sqrt{x} \log (x)}\qquad \text{if} \qquad \Re(\log (x))<0$$
The summation and the integral are highly correlated. Computing the partial sum for $p=10^4$, some results
$$\left(
\begin{array}{ccc}
x & \text{integral} &\text{summation} \\
 0.05 & 0.33955 & 1.10527 \\
 0.10 & 0.44971 & 1.22232 \\
 0.15 & 0.55613 & 1.35345 \\
 0.20 & 0.66857 & 1.50163 \\
 0.25 & 0.79248 & 1.67070 \\
 0.30 & 0.93279 & 1.86569 \\
 0.35 & 1.09508 & 2.09328 \\
 0.40 & 1.28642 & 2.36245 \\
 0.45 & 1.51634 & 2.68566 \\
 0.50 & 1.79825 & 3.08062 \\
 0.55 & 2.15187 & 3.57332 \\
 0.60 & 2.60743 & 4.20356 \\
 0.65 & 3.21372 & 5.03517 \\
 0.70 & 4.05453 & 6.17713 \\
 0.75 & 5.28602 & 7.83090 \\
 0.80 & 7.23316 & 10.4122 \\
 0.85 & 10.6911 & 14.9279 \\
 0.90 & 18.1929 & 24.5461 \\
 0.95 & 43.5702 & 56.2745
\end{array}
\right)$$
Put them on a graph. Using a quick and dirty linear regression based on much more data points gives $(R^2=0.999683)$
$$\text{summation}=1.21847\,\, \text{integral}+1.14809$$
$$\begin{array}{clclclclc}
 \text{} & \text{Estimate} & \text{Standard Error} & \text{Confidence Interval} \\
 a & 1.14809 & 0.07802 & \{0.99325,1.30293\} \\
 b & 1.21847 & 0.00220 & \{1.21409,1.22285\}
\end{array}$$
Comment: Your integral makes no sense.
Comment: @J.G. I know that, be sure.
Answer: A closed form might not be available, but a Taylor series certainly is. Let's begin with the partial fraction decomposition$$\frac{x^k}{1-x^{2k+1}}=\sum_{j=0}^{2k}\frac{\exp\frac{2\pi j(k-1)i}{2k+1}}{\prod_{l\ne j}\left(\exp\frac{2\pi li}{2k+1}-\exp\frac{2\pi ji}{2k+1}\right)}\frac{1}{1-\exp\frac{-2\pi ji}{2k+1}\cdot x},$$where $l$ runs from $0$ to $2k$ except at $j$. Let $[x^n]f(x)$ denote the coefficient of $x^n$ in $f(x)$ so$$[x^n]\frac{x^k}{1-x^{2k+1}}=\sum_{j=0}^{2k}\frac{\exp\frac{2\pi j(k-n-1)i}{2k+1}}{\prod_{l\ne j}\left(\exp\frac{2\pi li}{2k+1}-\exp\frac{2\pi ji}{2k+1}\right)}.$$Thus$$\sum_k\frac{x^k}{1-x^{2k+1}}=\sum_{n\ge0}\left(\sum_k\sum_{j=0}^{2k}\frac{\exp\frac{2\pi j(k-n-1)i}{2k+1}}{\prod_{l\ne j}\left(\exp\frac{2\pi li}{2k+1}-\exp\frac{2\pi ji}{2k+1}\right)}\right)x^n.$$The denominator can be rewritten using $e^{ia}-e^{ib}=2ie^{i(a+b)/2}\sin\frac{a-b}{2}$, so$$\prod_{l\ne j}\left(\exp\frac{2\pi li}{2k+1}-\exp\frac{2\pi ji}{2k+1}\right)=(-1)^j4^k\exp\frac{-\pi j}{2k+1}\prod_{l\ne j}\sin\frac{\pi(l-j)}{2k+1}.$$Hence$$\sum_k\frac{x^k}{1-x^{2k+1}}=\sum_{n\ge0}\left(\sum_k\frac{1}{4^k}\sum_{j=0}^{2k}\frac{\exp\frac{-2\pi j(n+1)i}{2k+1}}{\prod_{l\ne j}\sin\frac{\pi(l-j)}{2k+1}}\right)x^n.$$But I'm not sure we can simplify it much more than that.
Comment: very nice insight I would never think about the complex factorization. Probably there is a simplification for the complex part in the taylor series you got as the derivatives at zero are reals
Comment: @mranon Averaging $j$ vs $2k-j$ would help.


Euro symbol is shown as \u00e2\u0082\u00ac and â¬â¬

Question: I know there are a lot of topics out there concerning this problem, but I've spend the last hours trying various approaches an I'm nowhere close to the solution. So here it goes ...
This is my jquery:
$.post('inc/app_json_f.php',{params:priv_params}, function (data) {
  console.log(data);
  // displays: \u00e2\u0082\u00ac instead of €
  data = JSON.parse(data);
  console.log(data);
  // displays: â¬â¬ instead of €
}

This is my php (the app_json_f.php page):
$qry = 'select ... from ... where ...';
$data = $db->do_select($qry);

echo json_encode(f_utf8_json($data));

The f_utf8_json function will check the $data and convert every value with utf8_encode().
At this moment, the echo json_encode(...) will display the following in the console:
\u00e2\u0082\u00ac
Whereas I would like to see the € sign.
The data comes from a MySQL database which has collation utf8_general_ci. PHP's charset is UTF-8.
Any suggestions?
Comment: PHP defaults to connect with latin1 charset maybe thats the problem. So you need to change the connection charset of your PHP MySQL connection -> see http://stackoverflow.com/questions/10829816/set-character-set-using-mysqli for mysqli and see http://stackoverflow.com/questions/4361459/php-pdo-charset-set-names for PDO
Comment: Forgot to mention that: I already added mysqli_set_charset($this->db_conn, "utf8"); right after connecting.
Answer: for send file using curl you need to use  this working code.
if(!empty($postfields['image'])){
$file_name_with_full_path = $postfields['image'];
  if (function_exists('curl_file_create')) { // php 5.6+
    $cFile = curl_file_create($file_name_with_full_path);
  } else { //
    $cFile = '@' . realpath($file_name_with_full_path);
  }
  $postfields['fileToUpload'] =  $cFile;

}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
$result=curl_exec ($ch);
curl_close ($ch);

User curl_file_create
Answer: When using json_encode(), include JSON_UNESCAPED_UNICODE as the second argument so that you don't get \u0123 type codes, but instead get UTF-8.
The hex for UTF-8 encoding of € is E2ACE2AC.  (Ditto for MySQL's utf8 and utf8mb4.)
If you get €, then you have "Mojibake".
If you take UNHEX(E2ACE2AC) and treat it as any of cp1250, cp1256, cp1257, latin1, latin5, latin7, you get â¬â¬.  It sounds like you are compounding errors to get there.
See the "best practices" and other debugging tips here:
Trouble with utf8 characters; what I see is not what I stored
Answer: By removing the f_utf8_json function (the one that converts the values to utf8 with utf8_encode()), everything works ok. Strange, because I added this function earlier in the project because it was necessary to get the right results. I have to double check, but possibly another database setting was the cause of this.
Problem solved.


Drupal 8 Webform URL alias with parameters

Question: On the URL PATH SETTINGS,  I want to define the Webform URL alias to include a variable parameter e.g. /my-web-form-url/{id}
Is this possible? I want to pass this 'id' as a hidden value in the form.
Comment: This solution was very simple and worked for me: https://drupal.stackexchange.com/a/278757/67688
Answer: You would need to write some custom code to support custom URL parameters. (see How to (truly) alter a URL in Drupal 8)
You could easily pass the 'id' as a query string parameter, which is supported as long as you enable 'Allow elements to be populated using query string parameters.' under a Webform's > Settings > Form Behaviors.


WSASYSCALLFAILURE with overlapped IO on Windows XP

Question: I hit a bug in my code which uses WSARecv and WSAGetOverlapped result on an overlapped socket. Under heavy load, WSAGetOverlapped returns with WSASYSCALLFAILURE ('A system call that should never fail has failed') and my TCP stream is out of sync afterwards, causing mayhem in the upper levels of my program.
So far I have not been able to isolate it to a given set of hardware or drivers. Has somebody hit this issue as well, and found a solution or workaround?
Answer: How many connections, how many pending recvs, how many outsanding sends? What does perfmon or task manager say about the amount of non-paged pool used? How much memory in the box? Does it go away if you run the program on Vista or above? Do you have any LSPs installed?
You could be exhausting non-paged pool and causing a badly written driver to misbehave when it fails to allocate memory. This issue is less likely to bite on Vista or later as the amount of non-paged pool available has increased dramatically (see  http://www.lenholgate.com/blog/2009/03/excellent-article-on-non-paged-pool.html for details). Alternatively you might be hitting the "locked pages" limit (you can only lock a fixed number of pages in memory on the OS and each pending I/O operation locks one or more pages depending on buffer size and allocation alignment).
Comment: About 10 connections with one pending receive per connection. Sends are implemented blocking, so that there is at most one outstanding send. I'll check for the rest later.

Testing on Vista will take some time, since we don't have a test cluster yet. Thanks for all the pointers - I'll post again once I tracked it down.
Comment: Unlikely to be resource limits then. Possibly just a bad driver? Can you try on a machine with a different network card and drivers?
Comment: We have the issue consistently on different machines and drivers.

I'm beginning to think that there is a bug with overlapped IOs on XP. Our application is somewhat different in that a bunch of render slaves sends pixel data to a single machine, as opposed to server applications which send data to a bunch of clients.
Comment: "Bug with overlapped IOs on XP" If so, I've never seen it and I do a LOT of work with overlapped socket IO and have done for 10 years or so. It's more likely that you have a bug in your code, sorry ;) Can you post some code that shows the area that fails?
Comment: Sure - the connection is here: http://www.equalizergraphics.com/cgi-bin/viewvc.cgi/trunk/src/lib/net/socketConnection.cpp?view=markup

It is de-multiplexed by a WaitForMultipleObjects in here: http://www.equalizergraphics.com/cgi-bin/viewvc.cgi/trunk/src/lib/net/connectionSet.cpp?view=markup

And used from Node::runReceiverThread/handleData in here: http://www.equalizergraphics.com/cgi-bin/viewvc.cgi/trunk/src/lib/net/node.cpp?view=markup
Comment: Personally I wouldn't be setting the event in your overlapped when a read returns 0 indicating that the read has completed straight away; it's being set by the read call, there could be a race condition between you accessing it after WSARecv() returns and whatever you're doing with it (destroying it eventually?) after your wait on it returns in another thread...
Comment: This question says that the event is not set by the WSARecv in this case: http://stackoverflow.com/questions/2511690/hevent-member-in-overlapped-win32-structure

In any case, I never hit this condition, plus the read and WaitForMultipleObjects are in the same thread.
Comment: That surprises me about the event, but then I don't tend to use the event I just use an IOCP to deal with completions. Fair enough re the threading.
Comment: You never answered my question about whether you have any layered service providers installed on the machines in question; virus checkers and firewalls sometimes install them. I read from a MS guy that `WSASYSCALLFAILURE` is the result if lastError isn't set correctly by the function and that LSPs tend to be the main culprit...
Comment: I finally got around to check this. There are no additional LSP's installed.
Comment: That's a pity... I'm out of ideas.
Comment: Thanks anyway. I'm trying to implement a workaround, and immediately have hit the next Winsock oddity. Sigh.

http://stackoverflow.com/questions/3296920/winsock-blocking-sockets-multithreading-deadlock
Answer: It seems I have solved this issue by sleeping 1ms and retrying the WSAGetOverlapped result when it reports a WSASYSCALLFAILURE.
I had another issue related to overlapped events firing, even though there is no data, which I also had to solve first. The test is now running for over an hour, with a few WSASYSCALLFAILURE handled correctly. Hopefully the overnight test will succeed as well.
@Len: thanks again for your help.
EDIT: The overnight test was successful. My bug was caused by two interdependent issues:
Issue 1: WaitForMultipleObjects in ConnectionSet::select occasionally
signals data on an empty socket, causing SocketConnection::readSync to
deadlock.
Fix: Do a non-blocking read on the first byte of each packet. Reset
ConnectionSet if socket was empty
Issue 2: WSAGetOverlappedResult returns occasionally WSASYSCALLFAILURE,
causing out-of-sync on the TCP stream.
Fix: Retry WSAGetOverlappedResult after a small sleep period.
http://equalizer.svn.sourceforge.net/viewvc/equalizer?view=revision&revision=4649
Comment: Hmm... I'm generally very wary of 'fixes' which require sleeps... I expect it's more likely that there's a race condition in your code and that your 'fix' will come back to bite you in a couple of years time when you move to hardware that you can't dream of yet ;)
Comment: I agree on a general basis with your comments, and normally don't do 'fixes' like that.

In this instance however, I have debugged the code to death and am fairly convinced that it's not a race in my code. The error code is WSASYSCALLFAILURE, which is an Un-Error. Furthermore it occurs only with unusual communication patterns (many nodes sending one node a lot of data), which might be the reason it's still in WinSocks. We've had other issues with Winsocks before, e.g., this one: http://www.mombu.com/microsoft/alt-winsock-programming/t-wsasendto-resulting-in-stack-corruption-678924.html
Comment: Again that looks likely to be a race condition IMHO... How do you know when your per i/o is done with, I don't see any reference counting going on and, IMHO, you need reference counting of per socket and per i/o data to ensure you clean up at the right time and not before...
Comment: Data receive is done by a single thread. The data is received into a eq::net::Command, which is referenced when it's inserted into a net::CommandQueue to be dispatched to the handling thread, and dereferenced when it has been handled. The net::CommandCache takes care to recycle these packets for the ReceiverThread. I've extensively reviewed and tested this code using valgrind on Linux.


Show that if $g\circ f$ is injective, then $f$ must be injective. Is it true that $g$ must also be injective?

Question: I am now self-studying Terence Tao's Analysis 1. I am trying to solve all of the exercises. The question I have a problem with is
Let $f: X\rightarrow Y$ and $g:Y\rightarrow Z$ be functions. Show that if $g\circ f$ is injective, then $f$ must be injective. Is it true that $g$ must also be injective?
I solved the first part and tried to do the second part, but I could not solve it. I checked the solutions, and it was explained with a counterexample. However, as a practice, I tried to do it using contradiction. This is what I got so far.
Suppose $f$ is injective and $g$ is not injective. By definition, $\exists y,y'\in Y s.t. y\neq y'$ with $g(y)=g(y')$ and if $x\neq x'$ then $f(x)\neq f(x')$.
Pick $y,y'\in Y$ such that $y\neq y'$ with $g(y)=g(y')$. Using axiom of substitution, $g(f(x))=g(f(x'))$. Since $y\neq y'$ implies $x\neq x'$ by definition, $g\circ f$ with $x,x'$ as inputs have same outputs. Therefore, $g\circ f$ is not injective, which is a contradiction. So, $g$ should also be injective.
I am unsure which part has errors since it should be '$f$ injective only.' I guess that I cannot use the axiom of substitution for objects that are not equal. Is there any way that I can prove this without using a counterexample?
Thank you in advance.
Comment: I suggest:  study the counterexample you were given.  Go through your argument line by line using that example until you spot the error.
Comment: [This is intended as a helpful comment.] It seems to me to be perverse to prove that "$g$ need not be injective" by any other method than finding an example. If you are learning mathematics you should be learning that this cries out for an explicit example.
Comment: Your use of axiom of substitution is wrong. $f(x) \neq f(x')$ so you don't get $g(f(x)) = g(f(x'))$ directly through the axiom. You also can't use the axiom through $y = f(x), y' = f(x')$ (leveraging the non-injectivity of $g$) because you don't know those equalities either.
Comment: Thank you so much for all the helpful comments on pointing out my errors, giving suggestions, and even a piece of advice! I think my sequence of logic is messed up according to 0XLR. I will expose myself to more exercises :)
Comment: If the primary intention of your question is to get comment on your own solution (or attempt), then you should use the tag ([tag:solution-verification]). See also [the corresponding tag-info](https://math.stackexchange.com/tags/solution-verification/info).
Comment: If $|Y| \le 1$, then $g$ must be injective.
Answer: If you read carefully your attempt, you are in fact proving this:$\newcommand{\inv}[1]{{#1}^{-1}}$
Claim. If $g\circ f$ is injective and $f$ is surjective, then $g$ is injective.
Notice, that you have replaced $y$ by $g(x)$ and $y'$ by $g(x')$. But for this you need to now whether there actually exists some element $x\in X$ fulfilling $y=g(x)$. (And the same for $y'=g(x')$.) If you add the assumption that $g$ is surjective, that would be enough to make this step correct.
The fact that this claim is valid should not be surprising. Injectivity of $g\circ f$ implies that $f$ is injective. If you add that $f$ is also surjective, you get that $f$ is a bijection. So there exists an inverse $\inv f$. And you get that
$$g=(g\circ f)\circ \inv f,$$
so it is a composition of two injective functions. Thus $g$ is injective.
And even without writing down the formal proof, it should not be surprising that if you composing a function with a bijective function does not influence injectivity. (And the same is true for surjectivity.)

Some questions related to the original problem:

If $g \circ f $ is injective, why must $f$ be injective but not $g$?
Is it true if $g\circ f$ is injective then $g$ is injective?
Comment: I see it now. Because of "But for this, you need to know whether there actually exists some element ∈" this part, I am assuming the surjectivity of $f$. Thank you for your detailed answer!
Answer: I would say that g is not always injective because it depends of the domain and of the codomain. Look at this counterexample:
g: R → R , g(x) = |x|
f: N → N, f(x) = 2x + 1
f is injective, g∘f is injective, but g is not.
Comment: The OP is already aware of the existence of counterexamples.


mySql - search rows without reference in second table

Question: I have 2 tables.
How do i search for all rows in the first table that has no reference in the second table.
The connection field is: res_srvs.id = inv_supp2srv.srvID
So, I want to get all table "res_srvs" rows that has no srvID in table "inv_supp2srv".
TABLE: res_srvs
Collation   Attributes
id  int(11)
clientID    int(6)
resNum  int(9)
net decimal(7,2)
tax decimal(7,2)
from_date(date)

TABLE: inv_supp2srv
Collation   Attributes
clientID    int(6)
invNum  int(10)
srvID   int(11)
amount  decimal(7,2)
valid   tinyint(1)

This is what i tried:
SELECT srv.net , srv.tax , srv.net+srv.tax AS amount, srv.id AS srv_id
FROM res_srvs AS srv , inv_supp2srv AS i2s
WHERE srv.clientID = 1
AND srv.from_date >= '2020-03-01'
AND i2s.clientID = 1
AND i2s.srvID = srv.id
AND (NOT EXISTS
    (
    SELECT *
    FROM inv_supp2srv AS i2s
    WHERE i2s.srvID = srv.id
    )
)

Answer: What you want is a left outer join with exclusion :

SELECT r.*
FROM res_srvs r
LEFT JOIN inv_supp2srv i
ON r.id = i.srvID
WHERE i.srvID IS NULL
AND (
-- Your others where clauses go there
);

Comment: Hi Cid. thank you very much, but the select i wrote here is part of a bigger one. so is it possible to write WHERE .... AND not exist in second table ?
Comment: Left Join will perform best, regardless of the query size. The above condition would work as long as srvID is the only column you want to check on.
If there are multiple columns you can add "AND" to the join query.
Comment: @roi you can, but I prefer using `JOIN`s, it makes the queries clearer and could be faster  to execute. To give an accurate answer, data sample, table structure with expected results would be welcome. (In a  [fiddle](https://www.db-fiddle.com/) in example)
Answer: You can use LEFT JOIN for second table and filter by NULL joined value like:
SELECT srv.net , srv.tax , srv.net+srv.tax AS amount, srv.id AS srv_id
FROM res_srvs AS srv
LEFT JOIN inv_supp2srv AS i2s ON i2s.srvID = srv.id
WHERE
    srv.clientID = 1
    AND srv.from_date >= '2020-03-01'
    -- AND i2s.clientID = 1 not relevant condition
    AND i2s.srvID IS NULL;

Another approach is using NOT EXISTS condition:
SELECT srv.net , srv.tax , srv.net+srv.tax AS amount, srv.id AS srv_id
FROM res_srvs AS srv
WHERE
    srv.clientID = 1
    AND srv.from_date >= '2020-03-01'
    AND NOT EXISTS (
        SELECT srvID FROM inv_supp2srv AS i2s WHERE i2s.srvID = srv.id
    );

Comment: Hello again Slava. thank you, but the select i wrote here is part of a bigger one. so is it possible to write WHERE .... AND not exist in second table ?
Comment: @roi, sure you can. I added this approach to my answer. Both queries should to return same result, just you need to test which give better performance.
Comment: I think you missed `AND` before `NOT EXISTS`, no?
Answer:
I want to get all table res_srvs rows that have no srvID in table inv_supp2srv.

It looks like you are overcomplicating this. I don't see the point for the join between the tables in the outer query - it attempts to match the tables, which contradicts the not exists condition.
I think you  just want:
select r.*
from res_srvs r
where
    r.from_date >= '2020-03-01'
    and r.clientID = 1
    and not exists (
        select 1
        from inv_supp2srv i
        where i.srvID = r.id and i.clientID = r.clientID
    )

I am unsure whether you want clientID in the correlation clause or not - your query makes it look like it is the case, so I added it.
Comment: Well, the query is much longer, so i need the join for other staff...
Comment: @roi: you want rows that do no exist in the other table, so I don't understand why you want to join it.


Firebase cloud messaging (FCM) Admin - is it okay to store the service account API keys in the local storage

Question: Is it secure to simply save it in the local storage along with my project files? Or is it safer to store them as environment variables?
Answer: I would in general advice against saving and pushing credential files into a repository for others to see.
You may consider encrypting you credential file. Everyone who has to work with the project then has to decrypt it first and you have control who receives the access to it.
Have a look Googles KMS: https://codelabs.developers.google.com/codelabs/cloud-encrypt-with-kms/index.html#0


How to escape markup with check_markup in a twig template?

Question: I am getting following error in drupal 8 :
The website encountered an unexpected error. Please try again later.
Twig_Error_Syntax: Unknown "check_markup" function.
Twig template line :
<code>{% set featured_portfolio_text = check_markup(featured_portfolio_array['field_portfolio_link_text']['#items'][0]['value'],'full_html') %}
</code>
Is there any alternative to check_markup function for twig?
Answer: This seems like something you could do in a preprocess function and create that variable. At that level, you can use check_markup to check it.
Be sure to see the two notes on the docs page:

Note: Because filters can inject JavaScript or execute PHP code,
  security is vital here. When a user supplies a text format, you should
  validate it using $format->access() before accepting/using it. This is
  normally done in the validation stage of the Form API. You should for
  example never make a preview of content in a disallowed format.
Note: this function should only be used when filtering text for use
  elsewhere than on a rendered HTML page. If this is part of a HTML
  page, then a renderable array with a #type 'processed_text' element
  should be used instead of this, because that will allow cacheability
  metadata to be set and bubbled up and attachments to be associated
  (assets, placeholders, etc.). In other words: if you are presenting
  the filtered text in a HTML page, the only way this will be presented
  correctly, is by using the 'processed_text' element.

Here is a similar question/answer (albeit from 2014) that may still apply.


A problem in elementary analysis

Question: A friend asked me this problem but I couldn't solve.  Not homework.

Given reals $x>1$, $\epsilon>0$, does there always exist $i,n \in
\mathbb N$ such that $|x^i -n |<\epsilon$.  If the answer is in the
  negative, is there a nontrivial subset  $S \subset (1,\infty)$ such
  that all $x\in S$ satisfy this?
Comment: http://en.wikipedia.org/wiki/Pisot%E2%80%93Vijayaraghavan_number
Answer: We are going to find a sequence of nested intervals $(a_i,b_i)$ such that if $x\in[a_i,b_i]$ then $x^i\in [n_i+\frac{1}{3},n_i+\frac 2 3]$ for some integer $i$.
Then you'd have a counter-example by taking the limit.
Let $a_1=9\frac 1 3$ and $b_1=9\frac 2 3$.
Now, assume you have $(a_i,b_i)$ and $n_i$ such that $a_i^i = n_i + \frac{1}{3}$ and $b_i^i= n_i +\frac 2 3$.
Then look at the interval $[a_i^{i+1},b_i^{i+1}]$.  By choose $a_1>9$, we see that this interval is at least $9$ times the length of the $[a_i^i,b_i^i]$, which is length $\frac{1}{3}$, so, in particular, this interval must contain some full integer interval, $[n_{i+1},n_{i+1}+1]$.  Now, choose $a_{i+1}=\sqrt[i+1]{n_{i+1}+\frac{1}3}$ and $b_{i+1}=\sqrt[i+1]{n_{i+1}+\frac 2 3}$.  Then $[a_{i+1},b_{i+1}]\subset [a_i,b_i]$ and, for any value $x\in [a_{i+1},b_{i+1}]$, $x^i$ is at least $\frac 1 3$ away from the nearest integer.
Now, if $x\in \cap_i [a_i,b_i]$, then $|x^i-n|>\frac{1}{3}$ for all $i>0$ and all integers $n$.
Therefore the answer is in the negative.
I think it is fairly easy to use a variation of this argument to prove that the set of counter-examples are dense in the set $(1,\infty)$.
I think a similar argument can show that the set of reals satisfying your condition is also dense.
Comment: What real number is in the sequence of nested open intervals $(0,1),(1/2,1),(2/3,1),(3/4,1),\dots$?
Comment: Ah, okay, you can take closed interval then. @GerryMyerson :)
Comment: @GerryMyerson Changed to use closed intervals, but not really necessary, because you can ensure that $a_{i+1}>a_i$ and $b_{i+1}<b_i$.

A problem in elementary analysis


Need certain xPath search pattern

Question: I want to read a certain XmlNode from a XmlTextReader.
My file looks like this:
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<data>
   <legend>
      <element>
         <tag> aqua </tag>
...

while the "aqua" text is surrounded by "/r/n". So I tried to read it somehow like:
dataNode.SelectNodes("//legend/element").where("Tag".Trim() == "aqua");

of course this is just scrap and not working so I need the right one.
Can someone name the right XPath pattern ?
Comment: This is not valid XML, a tag name has to immediately follow a `<`, e.g. `` instead of `< data >`. I don't know if this is really your source file or just bad formatting here. Also, your shortening is not really good, as you are missing all the closing tags (thus making it invalid XML). Furthermore, what is "/r/n" you are talking about - I don't see it anywhere.
Comment: As I wrote, this is just scrap. The XML is just partially pasted. And I needed to separate the <> from the text to prevent the edit control from cutting it out of the post. And of course I didn't paste the /r/n, which you wouldn't see anyway.
Comment: This is blatently wrong. I just edited your question, there is no problem if you remove the space. Please format your XML properly, in this case it is not just nicer-looking, it is **required** so the XML is valid. Also, you **need* closing tags, e.g. it is ` aqua ` or ` aqua `? With /r/nT do you mean a line break? I have never seen it with normal slashes, it should be \r\n
Comment: You're right. Sloppy jotting me. Of course \ not /.
Happily Martin got what I meant. Issue closed.

PS: "Browser" not equals "Browser". Don't ask why !
Answer: As an XPath 1.0 expression I think you want //legend/element[tag[normalize-space() = 'aqua']]. That selects element elements that have a tag child elements whose normalized string value is aqua.

Need certain xPath search pattern


Executing Python Command from Discord.py

Question: So what I want to do is that I want to create an evaluation command in discord.py. So when the user says
.evaluate print("hi")

I want it to to send the code to python and run it. I seen ways with subprocess.Popen or os.system or system generally, but it only runs shell commands. I was wondering how I do this?
Answer: You can use the python eval() function. Of course allowing people to execute commands in your script is a security risk, please proceed with extreme caution. Not sure what you are trying to accomplish, this type of activity should be avoided.
Try:
@client.command()
async def evaluate(ctx, *, cmd=None):
    try:
        eval(cmd)
        await ctx.send(f'Your bot friend executed your command --> {cmd}')
    except:
        print(f'{cmd} is an invalid command')
        await ctx.send(f'Your bot friend could not execute an invalid command --> {cmd}')

Console Output
None is an invalid command
hi
nocommand(junk) is an invalid command

Discord dialog (using a "?" prefix instead of ".":
Comment: why is this dangerous? Like how is it a security risk?
Comment: @AK-10CA-GlenforestSS2172
If any user can run python commands, then they can do anything on the system that the bot is running, such as installing additional programs from the internet, running malware, stealing environment variables, and harvesting any personal information on the bot's hardware.
Answer: import contextlib
import io
@bot.command()
async def eval(ctx, *, code):
    str_obj = io.StringIO() #Retrieves a stream of data
    try:
        with contextlib.redirect_stdout(str_obj):
            exec(code)
    except Exception as e:
        return await ctx.send(f"```{e.__class__.__name__}: {e}```")
    await ctx.send(f'```{str_obj.getvalue()}```')

Keep in mind, the example above is very simple, and can be expanded on. For example, exec has a globals argument so that you can use global(pre-defined) variables in your eval statements. You can even format your code so that you can include: ```py
{your code here}```
around your eval command. Whatever you do, make sure no one has access to your eval command but you. Seriously, they might as well be sitting at your computer if they have access to it.
Comment: How would I allow block codes like with
 ```py
#code
```
Comment: If someone has access to your eval command, they will be able to do horrible things with it. They can delete channels, ban members, and get personal information from your computer (or the vps your bot is run on). The bottom line is to not give it to anyone.
Answer: Here is the command being owner only.
@client.command()
@commands.is_owner()
async def evaluate(ctx, *, cmd=None):
    try:
        eval(cmd)
        await ctx.send(f'Your bot friend executed your command --> {cmd}')
    except:
        print(f'{cmd} is an invalid command')
        await ctx.send(f'Your bot friend could not execute an invalid command --> {cmd}')


Como fazer uma consulta utilizando variaveis com PHP

Question: Estou tentando fazer a seguinte consulta no banco através do PHP:
SELECT * FROM nwpedidos WHERE `nomeUsuario` = 'Nome'

Porém, o 'Nome' varia de acordo com quem faz a consulta, pra isso passo uma variavel via javascript. O problema é que a consulta não retorna nada se eu utilizar variavel como no exemplo abaixo, mas se eu usar o comando completo como no exemplo acima, tudo funciona normalmente.

<?php
require_once("connect.php"); //conectar no banco
$var = ""; //pegar o conteudo da variavel JS
$sql = "SELECT * FROM `nwpedidos` WHERE `nomeUsuario` = '" . $var . "'"; //consulta no banco (que não retorna nada)
$resultado = $MySQLi->query($sql) OR trigger_error($MySQLi->error, E_USER_ERROR);
while ($dado = $resultado->fetch_object()) {
?>

Comment: cara da um var_dump($var); e ve posta aqui o resultado acredito que a informação dentro do $var que está incorreta por isso não retorna nada!
Comment: Por que não salvar o nome do usuário na sessão?
Comment: o var_dump ja havia testado, a principio está certo. 'string(35)'
Comment: @ViniciusDeJesus com cookies por exemplo?
Comment: @GabrielSarates usando Sessions, como você pega o nome do usuário e salva em `var a`?
Comment: @ViniciusDeJesus com o javascript, o usuário está escrito num span com o ID LoginName1.
Answer: Amigo, não sei como você está obtendo o valor de LoginName1.
Mas suponho que você tenha este valor em algum lugar da sua página (Seja via session ou banco de dados)
E por usar innerText, talvez esteja em algum elemento de texto, , , etc..
Da forma que está fazendo, a variável $var, está vazia.
Reproduzi então o seu exemplo, para que você tenha uma noção de como passar um valor para a variável $var.

    <title>Teste Query</title>

<meta charset="utf-8">

    <form method="POST" action="#">
        Login Name
        <input type="text" id="login-nome-input" value="usuario:gustavo">
    <button type="button" id="btn-pesquisar"  >Pesquisar</button>

    usuario:frederico

    </form>

<?php
   $var = "";
   echo $var;
   require_once("connect.php"); //conectar no banco
   $sql = "SELECT * FROM `nwpedidos` WHERE `nomeUsuario` = '" . $var . "'";
   // continua o codigo

?>
Comment: @GabrielSarates, valeeuu..
Se minha resposta foi útil em alguma coisa, aceite-a por favor!! T+
Comment: Obrigado pela resposta Wagner, tentei do seu método mas a consulta não retorna nada. Usei 'echo $sql' pra ver como ficou a consulta e apareceu um espaço depois da variavel, tentei usar um trim na variavel $var mas mesmo assim o espaço continua.  troquei o writeln por apenas write na $var e o espaço sumiu, mas mesmo assim a consulta não retorna nada
Comment: Se puder enviar um código mais completo, vai ajudar, só para eu saber como voce está recebendo o valor LoginName1; atualize a questão com mais detalhes
Comment: O LoginName1 é um span. Mas consegui resolver o problema usando Session mesmo, obrigado!
Answer: Exemplo com recarregamento da página.
Vamos supor que você está no arquivo index.php ok.
1°) Arquivo index.php parte (html), criação do botão (Atualizar)

<input type="button" id="submitUser" value="Atualizar" style="cursor: pointer;">

2º) Arquivo index.php parte (js)

//Criando obj user
user = new Object();

//Aaproveitando sua função...
//Função para obter o nome do usuário logado
function getName(){
    var b = document.getElementById("LoginName1").innerText;
    var res = b.split(":");
    return res[1].trim();
}

//Espera meio segundo para garantir o carregamento... DOM
setTimeout(() => {
    //Atribuindo o nome do usuário
    user.name = getName();

    //Cria url para o mesmo arquivo (index.php) passando parâmetro usuario_name
    var url = "location.href='index.php?user_name="+user.name;

    //Localiza o botão que vai submeter
    var submitUser = document.getElementById('submitUser');

    //Adiciona ao botão o url passando o usuário na ação do click
    submitUser.setAttribute("onclick", url)
}, 500);

3°) Arquivo index.php parte (php)

<?php
/*Recebendo o user_name pela url com ($_GET)
Só faz a consulta se existir o user_name na url.*/
if (isset($_GET['user_name']))
{
    /*Atribuindo o usuário recebido por ($_GET) a váriavel ($userName)*/
    $userName = $_GET['user_name'];

    /*Conectar no banco*/
    require_once("connect.php");

    /*Consulta no banco*/
    $sql = "SELECT * FROM nwpedidos WHERE nomeUsuario = '$userName' ";
    $resultado = $MySQLi->query($sql) OR trigger_error($MySQLi->error, E_USER_ERROR);
    while ($dado = $resultado->fetch_object()) {
} else {
    /*Caso o usuário não seja passado na url, criar um alert*/
    echo ""
}
?>

Se quiser sem recarregar a pagina, podes usar Ajax...


How to setup TrafficCounter in netty?

Question: I am writing an application where I need to track the sent/received bytes and bandwidth. I am using a library that itself uses Netty 5.0.
The only access I have to the Netty code is by extending this class of the library I am using.
How do I create and setup a TrafficCounter in this class? I couldn't find any examples of how to do it on the web (at least not in Netty 5.0). I have multiple threads using Netty so I need the traffic per channel.
Answer: You could have a look to this PR which still contain a full example on how to use TrafficCounter (in fact TrafficShapingHandler):
https://github.com/fredericBregier/netty/tree/8f704e6020de364b031a77e1ee403d3ae4d8e10d/example/src/main/java/io/netty/example/discard
And in particular for DiscardServer which contains both Global and Channel Traffic Shaping, where the last one (ChannelTrafficShaping) is the one you're looking for.
So what you could have is the following:
<code>        bootstrap.handler(new ChannelInitializer<Channel>() {
            @Override
            public void initChannel(Channel channel) throws Exception {
                getPacketProtocol().newClientSession(client, TcpClientSession.this);

                channel.config().setOption(ChannelOption.IP_TOS, 0x18);
                channel.config().setOption(ChannelOption.TCP_NODELAY, false);

                ChannelPipeline pipeline = channel.pipeline();

                refreshReadTimeoutHandler(channel);
                refreshWriteTimeoutHandler(channel);

                pipeline.addLast("encryption", new TcpPacketEncryptor(TcpClientSession.this));
                pipeline.addLast("traffic", new ChannelTrafficShapingHandler(0, MAXCHANNELTHROUGHPUT, 1000)); // ADDED
                pipeline.addLast("sizer", new TcpPacketSizer(TcpClientSession.this));
                pipeline.addLast("codec", new TcpPacketCodec(TcpClientSession.this));
                pipeline.addLast("manager", TcpClientSession.this);
            }
        }).group(this.group).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, getConnectTimeout() * 1000);
</code>
And look of course at the API: Traffic and in particular ChannelTrafficShapingHandler

How to setup TrafficCounter in netty?