text:
Q: How to detect most of uncommon hyperlinks and implicit redirects in a webpage? I create website crawler in C++/Qt4.8.5 for Windows. I've found that sometimes the page could contain implicit(pseudo) redirect like location.replace("some site"); in a script tag. And the result code of the request is 200 instead of something like Permanently Removed. I don't use  regular expressions for finding links to other pages, because it is not robust. I check attributes of HTML nodes instead of it. Primitive crawlers detect only href attribute in a tag. But there are other nodes which can contain urls. For example, <META HTTP-EQUIV="REFRESH" CONTENT="0; URL=/relative_url">. Where is unofficial list of tags/attributes (including script) which are relevant to url containers, implicit redirects? Maybe, some library in C++/Java contains handling of these cases.
Also, I've noticed that adding header of the preferred language to the request could not change page language for the site. I mean that Chrome has got page in some language, but my crawler has got page in another, default language, though it tries to mimic Chrome User Agent and language preferences. I've noticed that cookies in Chrome contained language property for that site. Are there any additional properties which can help server to generate page in the desired language?

A:
Are there any additional properties which can help server to generate page in the desired language?

There would be several more possibilities, for example geo location (identified by IP, geolocation API, ...).


text:
Q: Send packet on one interface and receive it on another one for testing purposes I would like to test the DHCP client that I wrote on Ubuntu 16.04.6 LTS. I would like to do it locally on my laptop i.e. send and receive DHCP packets inside.
Here is what I tried:

*

*created two dummy interfaces: dummy0, dummy1

*created bridge for these 2 interfaces: br0
The goal is to send DHCP discover from dummy0 and receive it on dummy1, I'm sending DHCP discover on dummy0, unfortunately I don't see any packets on dummy1 interface. If it could be received the next step would be to run DHCP server on dummy1 to test how my client is doing.
I see DHCP discover in wireshark on dummy0 interface. I see nothing on dummy1. Additionally: I see packet on br0.
I assume there may be better way to do this, could you please tell me how this can be done?

A: "Boomerang packets" (sent out from one local interface, received on another local interface) are considered a routing error (loop somewhere out there) and are dropped by the kernel by default.
Also, dummy devices are a pain to work with.
Better approach: Create a network namespace (or two), put a veth pair between the network namespace and the main namespace (or the two network namespaces). No bridge needed. Then test all you want.
Run an xterm in each namespace to make your life easier.
Here's a script I use to do that. Modify as you like.
#!/bin/bash

# Setup network namespace with veth pair, start xterm in it

# nsterm ns0 veth0 10.0.0 yellow 24
#
# tested: yellow, lime, orange, cyan

if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi

NS=${1:-ns0}
DEV=${2:-veth0}
DEV_A=${DEV}a
DEV_B=${DEV}b
ADDR=${3:-10.0.0}
ADDR_A=${ADDR}.254
ADDR_B=${ADDR}.1
MASK=${5:-24}
COL=${4:-yellow}

# echo ns=$NS dev=$DEV col=$COL mask=$MASK

ip netns add $NS
ip link add $DEV_A type veth peer name $DEV_B netns $NS
ip addr add $ADDR_A/$MASK dev $DEV_A
ip link set ${DEV}a up
ip netns exec $NS ip addr add $ADDR_B/$MASK dev $DEV_B
ip netns exec $NS ip link set ${DEV}b up
ip netns exec $NS ip link set lo up
ip netns exec $NS ip route add default via $ADDR_A dev $DEV_B
ip netns exec $NS su -c "xterm -bg $COL &" your_username

A: I think the answer (commands) here is easy to understand and follow.
For veth pair, ping does not recognize interface name and tc qdisc netem does not work

redpajama_set_name: RedPajamaStackExchange


text:
Q: Pokemon Enemy Random Generator Apparently I'm trying to make a pokemon simulator using Java and have a question for this(so far I only managed to make the basic JFrame with a moving oval(will be modified later as the moving character) in a window). How do I code random encounter with enemies like one in a pokemon game(using Java)? Thanks a lot. I'm new and will be pleased if anyone is willing to guide me on this game making.

A: Read a little on how games work. you could generate random world coordinates where you have a probability of encountering a pokemon in that given range over time :D. then you could build up on this by defining biome coordinates that would generate only specific types of pokemon with a given probability.


text:
Q: apache rewrite rules https to directory I want apache to:

*

*redirect http users to https

*if they access http(s)://example.com/1234 redirect to /view/1234

I have tried different flavours but cannot get it work. When a user access the website, they get redirected to https://, however I cannot get the /{numeric} to redirect to add /view/. The numeric rule was working okay until I put the https redirect on.
I have tried it with RewriteCond %{HTTPS} = on, but this causes 500 errors.
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://domain.example.org/$1 [R,L]
RewriteRule ^(\d+)$ https://domain.example.org/view/$1 [R=301,L]

A: Will this not work for you?
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Try this to test:
http://htaccess.madewithlove.be/
Ok it sounds like you actually want something like this:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{REQUEST_URI} /1234
RewriteRule (.*) https://%{HTTP_HOST}/view%{REQUEST_URI} [L]

A: Replace first RewriteCond with:
RewriteCond %{SERVER_PORT} !^443$

A: This is going to be an off the cuff answer, but, I believe the rewrite rules match in a first come/first serve basis.  Since the path structure changes, I'll bet your http://domain.example.org/1234 users are getting redirected to the https equivalent but without the path change.  Try switching the order of the rules and you'll get closer to a full solution.
If that doesn't work, let me know and I'll fire up an Apache instance and we'll see what we can get working in tandem.

A: Thanks for your help.
I finally have it working. I used the following:
RewriteCond %{HTTPS} off
RewriteRule ^/?(.*) https://domain.example.org/$1 [R,L]
RewriteRule ^([0-9]+)/?$    https://%{HTTP_HOST}/view/$1    [NC,L]

So any numeric requests have /view added on, any attempts to access the site with http:// gets forwarded to https://. Seems to work well.


text:
Q: The difference between linear independence/dependence for vectors and equations? If we have a set of vectors $(v_1, \ldots, v_n)$ that are linearly independent, then it means the only solution to $a_1v_1 + \ldots a_nv_n = 0$ is the trivial solution of $a_1 = \ldots = a_n = 0$.
For linear independence of equations, (which is inhomogenous) like the one below,
\begin{align}
x_1 \begin{bmatrix}a_{11}\\a_{21}\\ \vdots \\a_{m1}\end{bmatrix} +
 x_2 \begin{bmatrix}a_{12}\\a_{22}\\ \vdots \\a_{m2}\end{bmatrix} +
 \cdots +
 x_n \begin{bmatrix}a_{1n}\\a_{2n}\\ \vdots \\a_{mn}\end{bmatrix}
 =
 \begin{bmatrix}b_1\\b_2\\ \vdots \\b_m\end{bmatrix}
\end{align}
we have linear independence if none of the equations can be written in terms of others. My questions is what the connection is between linear independence of vectors versus equations. It seems that for vectors, it is just the special case of a homogeneous system of equations since the constant terms are zero. Is there more to it? Thanks.

A: Let $A$ be the matrix
$$
A=\left(\begin{array}{cccc}
a_{11} & a_{12} & \cdots & a_{1n}\\
a_{21} & a_{22} & \cdots & a_{2n}\\
\vdots & \vdots & \vdots & \vdots \\
a_{n1} & a_{n2} & \cdots & a_{nn}\\
\end{array}\right)
$$
and let $b$ the vector
$$
b=\left(\begin{array}{c}
b_1\\
b_2\\
\vdots\\
b_n
\end{array}\right).
$$
Trivially the space of solutions of your problem is the space
$$
S = \left\{x\in\mathbb{R}^n| A\cdot x=b\right\}
$$
the system as a unique solution if and only if $det(A)\neq 0$ which is true if and only if the vector columns of $A$ are linearly independent which is equivalent to say that none of the equations of the system can be written in terms of others. For the case $b=0$ since $A$ is linear and then $A\cdot 0=0$, if the solution is unique, it can be only the zero vector, so $x_1=0,...,x_n=0$.

A: Technically speaking, linear dependency of equations might not make sense in general. When people talk about equations being linearly dependent or independent, it is assumed to be of the form
$$Ax = b$$
where $A$ is a matrix, and $x$ and $b$ are vectors of appropriate dimensions. (And the unknowns are components of $x$.) In this case, linear dependence of the system of equations is simply the linear dependence of "rows" of $A$.
Note that you can have a linearly independent system of equations even when $A$ is not square.
When the system of equations is linearly dependent, it's possible to find $b$ such that $Ax = b$ doesn't have a solution.

A: If one of the equations can be written in terms of the other, it means one of the rows of the following augmented matrix can be written in terms of others:
$$\left(\begin{array}{cccc|c}
a_{11} & a_{12} & \cdots & a_{1n} & b_{1}\\
a_{21} & a_{22} & \cdots & a_{2n} & b_{2}\\
\vdots & \vdots & \vdots & \vdots & \vdots\\
a_{n1} & a_{n2} & \cdots & a_{nn} & b_{n}\\
\end{array}\right)
$$
So basically we are talking about linear independence of $n$ row vectors each of dimension $(n+1)$.


text:
Q: esimport overflowerror indexing bulk CSV files in elasticsearch I am trying to index elasticsearch using below esimport library. Input csv files are of 500 MB size.
I am using below code in command prompt in wondows 10 server.
start python -m esimport -s 10.10.31.111:9200 -f C:\CSV\1.csv -i bdm_crm_eve -t alldata -d "|" -T 60000 -bc 5000 -m C:\Data\latest_template_1224.json
I am getting below error and i didn't get this anywhere on internet. Can anyone help me with this please?

A: I found the answer myself. In esimport.py file in the package setup, CSV field size limit was causing this issue. I did some testing and provided the limit manually by changing csv.field_size_limit(sys.maxsize) into csv.field_size_limit(2147483647). This works fine now


text:
Q: Find loop value inside string Check year in the title if found get the string before year e.g.testmovie, how can I do it?
int main()
{
    string title = "testmovie2015.mkv";
    for (int year = 2000; year <= 2018; year++)
    {
        // code
    }
}

A: The string class has two functions that are useful here: find and substr.
Together with to_string, you can use them like:
#include <iostream>
#include <string>

int main() {
    std::string title = "testmovie2015.mkv";
    int y = 2015;
    std::string year = std::to_string(y);
    std::string::size_type n = title.find(year);
    if (n != std::string::npos)
    {
        std::string a = title.substr(0, n);
        std::cout << a << std::endl;
    }

    return 0;
}

A: This works.
#include <string>
#include <iostream>
using namespace std;
int main()   {
char title[20] = "testmovie2015.mkv";
int year=0,i=0;
for(i=0;title[i]!='\0';i++){
if(isdigit(title[i])&&isdigit(title[i+1])&&isdigit(title[i+2])&&isdigit(title[i+3]))
{   year=1;
    break;
}
                    }
if(year)
{
 year=((title[i]-'0')*1000)+((title[i+1]-'0')*100)+((title[i+2]-'0')*10)+(title[i+3]-'0');
 if(year>=2000&&year<=2018)
 {   int k=0;
     while(k<i)
     {
         cout<<title[k];
         k++;
     }
 }
 else
 {
     cout<<"Year found: "<<year<<", but out of given range";
 }
}
else
{
    cout<<"No year found in the string";
}

        }


text:
Q: Getting coordinates and population of all cities in a country Can I get something like that on any of google services? or is there another place that can help me?
I need both coordinates and population but most of the lists I find are either one or another and have completely different formats.
Is there a website that can help me?

A: There's no such API where you can get both coordinates and population.
For the coordinates or any geographical information regarding a specific place or country, you may use Google Maps Platform API.
With regards to the population and you think that it, I would suggest to file for a Feature Request to include population (like estimate number people for a particular place/city/area/region/country) on the response in our Google Public Issue Tracker.
Issue Tracker is a tool used internally at Google to track bugs and feature requests during product development. It is available outside of Google for use by external public and partner users who need to collaborate with Google teams on specific projects.
Hope this information could be helpful on your side.


text:
Q: Can I have docker-compose restart a container when a file changes? Docker-compose is nice- using docker-composer up -d will restart all containers where the docker-compose.yml has changed. It will not however pickup if I have changed the traefik.toml that one of my container needs to read on startup.
Is it possible to have docker-composer take arbitrary files into consideration when recreating containers?

A: Unfortunately no, Docker-compose is just a handy tool to ease starting containers, it doesn't care about your state.
To mimic such behavior you can extend the entrypoint of the image and handle such use case by yourself.

A: You can, if you set up a volume for your files in question (example below) or locate the volume on your system as docker creates it.
volumes:
    - type: bind
      source: .
      target: /srv/www

Simply set up a watch command or cronjob to check the files. If a change is found you can simply cd {build dir} && docker-compose down && docker-compose up -d
I recommend you have a script to launch both the watcher and docker-compose or an alias for ease of use.
An alternative, you can have the docker container check its own files, and if it sees a change just exit. Have a cronjob or watch script running which runs docker-compose up -d in response to the container not running.

A: You can use inotifywait to track docker-compose.yml changes and then call docker-compose up -d so that only services with changed configuration will restart.
If you want restart traefik container then inotifywatch the traefik.toml file and docker-compose restart [traefik-service-name-here]. You can also rebuild and recreate services as well.
Here is part of my development bash script:
#!/usr/bin/env bash

PID=0
trap 'kill $PID' EXIT

if [ -z "$(which inotifywait)" ] ; then
  echo "Install inotify-tools"
  exit 1
fi

while true; do
  sleep 2;
  inotifywait -qm --event modify --format '%e' ./docker-compose.yml | echo ""
  docker-compose up -d &
done &
PID=$!

docker-compose  up

kill $PID


text:
Q: Replace double for loop (with indexing into other arrays) over numpy array Here's the example I'm working on:
 processed_data = np.empty_like(data)
 min_per_col = np.amin(data, axis=0) # axis0 for col, axis1 for row
 max_per_col = np.amax(data, axis=0) # axis0 for col, axis1 for row
 for row_idx, row in enumerate(data):
     for col_idx, val in enumerate(row):
         processed_data[row_idx][col_idx] = (val - min_per_col[col_idx]) / (max_per_col[col_idx] - min_per_col[col_idx])

data is defined as a 2d numpy array. I am essentially trying to perform some operation on each element in data using the relevant values in min_per_col and max_per_col.
I can't seem to figure out the approach to take. It seems like from these posts the answer is to reshape the arrays so that broadcasting works.
Intuitively, I think the way it would work with broadcasting would be:
# Results of min_per_col:
#     [min1 min2 min3 min4 min5]

# Transformation to (call this 2d_min_per_col):
#     [[min1 min2 min3 min4 min5],
#      [min1 min2 min3 min4 min5],
#      [min1 min2 min3 min4 min5]
#      ...
#      [min1 min2 min3 min4 min5]]
# which basically duplicates min_per_col into a 2d array form.

# Do the same for max (2d_max_per_col)

# processed_data = (data - 2d_min_per_col) / (2d_max_per_col - 2d_min_per_col)

Does this approach make sense? Or is there another answer for how to approach something like this?
Please let me know if there's anything else that would be helpful to include for this post! Thank you.
EDIT: Thanks for the help Mad Physicist! After trying this:
processed_data = np.empty_like(data)
min_per_col = np.amin(data, axis=0) # axis0 for col, axis1 for row
max_per_col = np.amax(data, axis=0) # axis0 for col, axis1 for row
for row_idx, row in enumerate(data):
    for col_idx, val in enumerate(row):
        processed_data[row_idx, col_idx] = (val - min_per_col[col_idx]) / (max_per_col[col_idx] - min_per_col[col_idx])
print("version 1\n", processed_data)

processed_data = (data - min_per_col) / (max_per_col - min_per_col)
print("version 2\n", processed_data)

return processed_data

It works identically, and is much faster!
version 1
 [[0.25333333 0.13793103 0.14285714]
 [0.32       0.79310345 0.92857143]
 [0.13333333 0.48275862 0.51785714]
 ...
 [0.28       0.4137931  0.125     ]
 [0.01333333 0.24137931 0.75      ]
 [0.08       0.20689655 0.23214286]]
version 2
 [[0.25333333 0.13793103 0.14285714]
 [0.32       0.79310345 0.92857143]
 [0.13333333 0.48275862 0.51785714]
 ...
 [0.28       0.4137931  0.125     ]
 [0.01333333 0.24137931 0.75      ]
 [0.08       0.20689655 0.23214286]]

Thanks for the fast help :D

A: You have the gist of it, but the whole point of broadcasting is that you don't need to expand arrays to do operations on them: the shapes are lined up on the right. So for example, let's say data.shape is (M, N) your array shapes look like this to the math operations:
data:           (M, N)
processed_data: (M, N)
min_per_col:       (N,)
max_per_col:       (N,)

Notice that min_per_col and max_per_col line up perfectly as they should. That means that your entire loop becomes simply
processed_data = (data - min_per_col) / (max_per_col - min_per_col)
#                    (M, N)                         (N,)
#                                   (M, N)

The comments under each operator show the shape of the broadcasted output.
As an aside, you can compute the denominator in a single step using np.ptp:
processed_data = (data - np.min(data, axis=0)) / np.ptp(data, axis=0)


text:
Q: Rails - change relation select I have the following relation:
class Complaint < ActiveRecord::Base
  belongs_to :contact
  has_one :address, as: :addressable, dependent: :destroy
end

And
class Contact < ActiveRecord::Base
  has_many :complaints
  has_many :personal_addresses, -> { where type: 'personal' }, as: :addressable, class_name: "Address", dependent: :destroy
end

When the complaint has a contact, I need to use the first personal_address from contact, and when it doesn't have, I need to use the address of complaint.
I have it already working, but now that data has starting to grow, things got slow. So I need to select all of the complaints and include the address in the same sql.
For this, I overloaded the address method in complaint with the following:
if self.use_contact_address && !self.contact_id.nil?
    Address.where(addressable_type: 'Contact').where(addressable_id: self.contact_id).where(type: 'personal').first
else
    read_attribute(:address)
end

The problem with this is that the custom select returns an array. I changed to return the first element of the array, but with this rails has made a select for each element.
Someone knows how to overload the address method to implement in a way that rails do a single select to return all of the address from the DB?
What I need: Load all the complaints from the db and do it in a way that the AR query loads the address together. But if the complaint uses contact address, I need that the AR knows that it needs to load the address using a different query.
The query I need to do is the following: Complaint.all.includes(:address)
Thanks and sorry for the bad english.

A: what if
class Contact
  def address
    personal_addresses.first.presense || complaints.first.address
  end
end

A: @contact.complaints.all

This would perform one select to get all related complaints to an instance of your contact. Not sure if that would solve your issue though, as the description is a bit unclear.

A: complaints=Complaint.includes(:address, {contact: :personal_addresses}).all

complaints.first.contact.personal_adresses.first

it should not fire additional queries for any member of complaints when accessing to the address and contact.personal_adresses relations


text:
Q: javascript - proper way to do thousands of $.getJSON calls to avoid ERR_INSUFFICIENT_RESOURCE Problems:
I'm doing some data lookup from a website contain thousands of .json files, now I'm reading those .json with a straight forward for loop $.getJSON calls. It works fine within ~3000 files, but when the size goes up to 5000 or even 20000 (max number I've to read). The Chrome browser becomes super slow or get ERR_INSUFFICIENT_RESOURCE.
What I'm doing right now:

*

*scan through data example.org/1.json to 20000.json

*read these .json content into arrayone[], process array of data captured

*based on arrayone[], I do another $.getJSON on example.org/someplace/1.json to 20000.json

*read these .json content into arraytwo[]

*compare arrayone[] & arraytwo[]
So given I've to read 20000 files, it's actually 40000 $.getJSON call.
Seek helps:
Is there a smarter way to read multiple .json with better performance?
Hope it's clear, thanks in advance!


text:
Q: Draw a string with gradient colors Is there a way to draw a string in NSView with gradient colors? Gradient is not a background, but the letters itself. Maybe some mask, or so?
(Actually, it's not a text, but an icon font).

A: override func draw(_ rect: CGRect) {

    //Add gradient layer
    let gl = CAGradientLayer()
    gl.frame = rect
    gl.endPoint = CGPoint(x: 0, y: 0)
    gl.colors = [UIColor.red.cgColor, UIColor.blue.cgColor]
    layer.addSublayer(gl)

    //create a text layer mask
    let tl = CATextLayer()
    tl.frame = rect
    tl.string = "My String in Gradient"

    //Add mask to gradient layer
    gl.mask = tl

}

Hope this helps!


text:
Q: Set cursor to correct location after Google Sheets after insertRowBefore I have the following Google Sheets function:
function addRow() {
   var destinationSheet = SpreadsheetApp.getActiveSheet();
   var currentRow = destinationSheet.getActiveCell().getRow();

  // Insert the row
  destinationSheet.insertRowBefore(currentRow);

  // Copy the source to destination (currentRow is the blank line)
  var sourceRange = destinationSheet.getRange(currentRow + 1, 1, 1, destinationSheet.getLastColumn());
  var targetRange = destinationSheet.getRange(currentRow - 0, 1, 2, destinationSheet.getLastColumn());

  // Fill upwards
  sourceRange.autoFill(targetRange, SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);
  SpreadsheetApp.getUi().alert(destinationSheet.getActiveCell().getRow());
};

It creates a blank row above the current one and copies the current row into the blank one. This works.
The cursor (visually) ends up in the new row. In other words, visually, when I do the insertRowBefore, the cursor stays where it is and the rows below move down.
However, when I then type into the cell (first column) without moving the cursor beforehand, it types into the cell where the cursor is but the moment I press Return, the data "jumps" to the cell below as if I had been typing there. It is as if the cursor is actually in the row below and the UI is out of sync. The alert, however, "tells" me I am in the right row (the one that was empty and then filled).
Interestingly, if I re-run the function without moving the cursor in the UI, I notice that the active row has incremented by one. So, I think that the UI moves the cursor after the function has finished but fails to show it graphically.
My question is: how do I either:

*

*Get the UI to show the active cursor location correctly? OR, ideally

*Prevent this "behind the scenes" movement of the cursor so that it remains in the correct cell (column 1 of the row that was added).

A: The Google Apps Script Spreadsheet Service hasn't a methods to control the cursor position, but it is able to set the the active cell.
One way among many is the following:
function setCurrentCell(){
  const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = spreadsheet.getSheetByName('Sheet1');
  const range = sheet.getRange('B2'); // This should be a single cell
  range.activateAsCurrentCell();
}

Another way is to use activate() on a Range object. The current cell will be top left cell in the range.
Reference

*

*https://developers.google.com/apps-script/reference/spreadsheet/range#activateAsCurrentCell()

*https://developers.google.com/apps-script/reference/spreadsheet/range#activate
Related

*

*Google sheets, make active range the top left cell of the window

A: I fixed it by adding the following 3 rows to the end of the function:
  destinationSheet.getRange(currentRow - 1, 1, 1, destinationSheet.getLastColumn()).activate();
  destinationSheet.getRange(currentRow, 1, 1, destinationSheet.getLastColumn()).activate();
  destinationSheet.getRange(currentRow, 1).activate();

This is the only way I managed to get it working. It:

*

*Activates the preceding row

*Activates the current row

*[Optional] Activates the first cell of the current row.

Then when I type into the cell, it does not drop down a row when I hit enter. Note that for the first 2 lines of extra code, it is necessary to select more than the first column - hence why the whole row is selected. The problem is therefore resolved after the first 2 lines of code. The third is just cosmetic to select only the first column in the row.
I do not know why this works and why other variants do not work.


text:
Q: Java String vs Stream I have a function that does something like this.
try {
    Process p = runtime.exec(fullCommand, null);

    BufferedReader outReaderOutput = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line = outReaderOutput.readLine();
    String output = "";

    while (line != null) {
        output += line + "\n";
        line = outReaderOutput.readLine();
    }
    return output;
} catch (IOException e) {
    throw new RuntimeException(e);
}

Is there a way to return Stream instead of the entire String? What would be the benefit of that?

A: A string is basically stored in memory; its contents are completely available at the time you receive the String instance. You can directly search the string or find other information - such as the length - of the string.
If you receive a Reader instance you don't know the size of the string, and many functions such as searching are not (directly) available. On the other hand, you don't need to store the entire string in memory.
Moreover, not all of the contents need to be available at the time you receive the Reader instance. A good example is receiving text over a socket; you may want to handle part of the contents when they are received, not after the full message is received.
The complete output of an application - in your example code - may also not be directly available; your method blocks until it is (i.e. until your fullCommand application exits).

If you want to return a character stream you can simply return the BufferedReader instance. If it makes sense to return a binary stream from a Reader most of the time, but if this is required then read this answer.
EDIT: if you just need binary you should of course directly return p.getInputStream() in your example.

A: You can easily return the p.getInputStream()
That depend on your implementation.

A: In the end, you're better off returning a string if you need the whole file at once.
As it stands, I don't think the Java compiler can optimize this to a StringBuilder, which basically allows amortized constant time string concatenation.
You're better off using a StringBuilder like this:
StringBuilder sb = new StringBuilder();
while(outReaderOutput.hasMoreLines())
{
    sb.append(outReaderOutput.readLine()).append('\n');
}

EDIT: wow, should not have missed that.


text:
Q: Find the greatest right number from the current number in the array algorithm My algorithm should find the greatest right number from the current number in an input array, for example, given the following int[] input:

5, 9, 6, 1, 3, 2

My algorithm will output:

9, 6, 3, 3, 2, 2

Here is my current code:
public static int[] FindGreatestRightNumber(int[] input)
{
    var output = new int[input.Length];
    for (var i = 0; i < input.Length; i++)
    {
        int maxRightNumber = (i == input.Length - 1 ? input[i] : 0);

        for (var j = i+1; j < input.Length; j++)
        {
            var currentNumber = input[j];
            if (maxRightNumber < currentNumber)
                maxRightNumber = currentNumber;
        }
        output[i] = maxRightNumber;
    }

    return output;
}

I was told it could be much faster, how? any idea?
UPDATE: Please don't use LINQ in your answers, I would like to get familiar with faster ways to solve the problem using simple code, no LINQ, IEnumerable Extension Methods etc.

A: You can do this in a single pass from the right hand side. The trick is realizing maxRightVal(n) = max(maxRightVal(n+1), values(n+1)):
var output = new int[input.Length];
output[input.Length-1] = input[input.Length-1];

for(int i = input.Length - 2; i >= 0; i--)
    output[i] = output[i+1] > input[i+1] ? output[i+1] : input[i+1];

A: very simple if you want to skip some items and search the max
int[]arr = {5, 9, 6, 1, 3, 2};
int currentIndex = 2;
int currentValue = 6;
int max = arr.Skip(currentIndex).Where(f => f > currentValue).Max();

EDIT if you want to simply sort an array, then:
   int[] sorted = arr.OrderByDescending();

A: Why not just using Enumerable.Max() method?

Returns the maximum value in a sequence of Int32 values.

int[] input = new int[] { 5, 9, 6, 1, 3, 2 };
int biggest = input.Max();
Console.WriteLine(biggest); // 9

Here is a DEMO.
Since, I see the question better now, VLad's answer looks the right one.

A: Start from the (n-2)th elements, maintain a current max array which is initialised with nth element. Keep updating it if the current element is greater than the element in max array. Continue this until the first element is reached.

A: This takes the largest value to the right of each element;
 int[] input = {5, 9, 6, 1, 3, 2};
 int[] output = input
            .Take(input.Length-1)
            .Select( (x,i) => input.Skip(i+1).Max()).ToArray();


text:
Q: How can I stop the Unity bar from causing cursor delay between two monitors? If I have two monitors, side by side, then the Unity bar will interfere with mouse movement. I set the unity bar to only display if I move the mouse to the top left corner of the screen, but the mouse still pauses momentarily when I move the mouse from the right display to the left display. The place it pauses, is exactly on the left border of the right screen.

A: You must have sticky borders enabled. Remove the option sticky borders.

A similar problem has been answered here: How do I disable mouse magnet on middle edge with multi monitors?

A: Another post here will help, and provide you with some options to set things up the way you want.


text:
Q: Add a new column with value from another column matching a pattern I have a dataframe such as :
COL1 COL2
A    eucaryotes; mammal; carnivoridae; carnivorinae; carnivorus
B    viruses; Retroviridae
C    viruses; mononegavirales; Phenuiviridae; Ascovirinae; Reovirus
D    Unclassified; RNA virus

And I would like to parse the COL2 column with element separated by ";" and add a COL3 column with the element containing "viridae" for each row.
I should then get:
COL1 COL2                                                           COL3
A    eucaryotes; mammal; carnivoridae; carnivorinae; carnivorus     carnivoridae
B    viruses; Retroviridae                                          Retroviridae
C    viruses; mononegavirales; Phenuiviridae; Ascovirinae; Reovirus Phenuiviridae
D    Unclassified; RNA virus                                        NA

Does someone have an idea please ?
Here is the dataframe in dict format if it can helps
{'COL1': {0: 'A', 1: 'B', 2: 'C', 3: 'D'}, 'COL2 ': {0: 'eucaryotes; mammal; carnivoridae; carnivorinae; carnivorus', 1: 'viruses; Retroviridae', 2: 'viruses; mononegavirales; Phenuiviridae; Ascovirinae; Reovirus', 3: 'Unclassified; RNA virus '}}

A: You could do something like this:
import pandas as pd
import re

df = {'COL1': {0: 'A', 1: 'B', 2: 'C', 3: 'D'}, 'COL2': {0: 'eucaryotes; mammal; carnivoridae; carnivorinae; carnivorus', 1: 'viruses; Retroviridae', 2: 'viruses; mononegavirales; Phenuiviridae; Ascovirinae; Reovirus', 3: 'Unclassified; RNA virus '}}

df = pd.DataFrame(df)

You can then use the following method: First tranform the column into a column of lists
df['COL2_list']= df['COL2'].str.split(';')
df = df.reset_index()

Then parse through every row of the df to find th strings you want (here I choose 'ridae'):
DF = []
for i in range(len(df)):
    a = df[df.index==i]
    b = [string for string in a['COL2_list'][i] if 'ridae' in string]
    a = np.where(len(b)!=0, b,'NAN')
    DF.append(a)

DF = pd.DataFrame(DF, columns = ['COL3'])
DF

which gives you
 COL3
0    carnivoridae
1    Retroviridae
2   Phenuiviridae
3            None

and then concatenate the results:
Full = pd.concat([df,DF], axis=1)

which gives:
index COL1                                               COL2  \
0      0    A  eucaryotes; mammal; carnivoridae; carnivorinae...
1      1    B                              viruses; Retroviridae
2      2    C  viruses; mononegavirales; Phenuiviridae; Ascov...
3      3    D                           Unclassified; RNA virus

                                           COL2_list            COL3
0  [eucaryotes,  mammal,  carnivoridae,  carnivor...    carnivoridae
1                           [viruses,  Retroviridae]    Retroviridae
2  [viruses,  mononegavirales,  Phenuiviridae,  A...   Phenuiviridae
3                        [Unclassified,  RNA virus ]            None

Slightly different from what you wrote but it is because you didn't spell things right.

A: To match your example:
df.assign(COL3=df['COL2'].str.extract('(\w+v[io]ridae)'))

  COL1                                               COL2           COL3
0    A  eucaryotes; mammal; carnivoridae; carnivorinae...   carnivoridae
1    B                              viruses; Retroviridae   Retroviridae
2    C  viruses; mononegavirales; Phenuiviridae; Ascov...  Phenuiviridae
3    D                            Unclassified; RNA virus            NaN

To match your stated requirement of finding words ending with viridae:
df.assign(COL3=df['COL2'].str.extract('(\w+viridae)'))

  COL1                                               COL2           COL3
0    A  eucaryotes; mammal; carnivoridae; carnivorinae...            NaN
1    B                              viruses; Retroviridae   Retroviridae
2    C  viruses; mononegavirales; Phenuiviridae; Ascov...  Phenuiviridae
3    D                            Unclassified; RNA virus            NaN


text:
Q: How to get co-ordinates of all gas/fuel/petrol stations on Google Maps? I'm developing a petrol price comparison app for my final year project in university (something similar to GasBuddy or PetrolPrices if you're living in the UK/US). I want to create an Android app that features an in-app map that's prepopulated with all the petrol stations in the country (Ireland).
I was going to use the Google Maps API for the in-app map and then add custom markers for all the petrol stations. I'm going to use Firebase as my database and wanted a section of it to be a list of all the petrol stations with their information such as; co-ordinates, opening times, petrol prices etc. I would use the Google Places API but I only want petrol stations to come up on the app without a user having to input a search query for them, which means the map will have to be pre-populated already.
Is there anyway that I can get all the co-ordinates of all the petrol stations listed on Google Maps to input into my database so I can prepopulate the in-app map with custom markers?
Thanks in advance

A: Note that Stack Overflow is not a free code writing service.You are expected to try to write the code yourself. After doing more research if you have a problem you can post what you've tried with a clear explanation of what isn't working and providing a Minimal,Complete, and Verifiable example.I suggest reading How to Ask a good question and the perfect one. Also, be sure to take the tour read this.
That said, you can use Places API - Nearby Search to achieve this. And by using the type filter and setting it to gas_station, you will get details of the place such as their coordinates. See this for the list of supported types.
IMPORTANT NOTE:
Be careful on what you store in your database. According to Google Maps Terms of service under 3.2.3 (a) No Scraping, customer will not: (i) pre-fetch, index, store, reshare, or rehost Google Maps Content outside the services
There is, however, some data that you can store/cache temporarily. See their service specific terms for more information on this.


text:
Q: Breaking out of monad sequence Is it possible to break out of a monad sequence?
For instance, if I want to break out of a sequence earlier based on some condition calculated in the middle of the sequence. Say, in a 'do' notation I bind a value and based on the value I want to either finish the sequence or stop it. Is there something like  a 'pass' function?
Thanks.

A: Directly using if
You could do this directly as Ingo beautifully encapsulated, or equivalently for example
    breakOut :: a -> m (Either MyErrorType MyGoodResultType)
    breakOut x = do
        y <- dosomethingWith x
        z <- doSomethingElseWith x y
        if isNoGood z then return (Left (someerror z)) else do
            w <- process z
            v <- munge x y z
            u <- fiddleWith w v
            return (Right (greatResultsFrom u z))

This is good for simply doing something different based on what values you have.
Using Exceptions in the IO monad
You could use Control.Exception as Michael Litchard correctly pointed out. It has tons of error-handling, control-flow altering stuff in it, and is worth reading if you want to do something complex with this.
This is great if your error production could happen anywhere and your code is complex. You can handle the errors at the top level, or at any level you like. It's very flexible and doesn't mess with your return types. It only works in the IO monad.
import Control.Exception

Really I should roll my own custom type, but I can't be bothered deriving Typable etc, so I'll hack it with the standard error function and a few strings. I feel quite guilty about that.
handleError :: ErrorCall -> IO Int
handleError (ErrorCall msg) = case msg of
   "TooBig" -> putStrLn "Error: argument was too big" >> return 10000
   "TooSmall" -> putStrLn "Error: argument was too big" >> return 1
   "Negative" -> putStrLn "Error: argument was too big" >> return (-1)
   "Weird" -> putStrLn "Error: erm, dunno what happened there, sorry." >> return 0

The error handler needs an explicit type to be used in catch. I've flipped the argument to make the do block come last.
exceptOut :: IO Int
exceptOut = flip catch handleError $ do
     x <- readLn
     if (x < 5) then error "TooSmall" else return ()
     y <- readLn
     return (50 + x + y)

Monad transformers etc
These are designed to work with any monad, not just IO. They have the same benefits as IO's exceptions, so are officially great, but you need to learn about monad tranformers. Use them if your monad is not IO, and you have complex requirements like I said for Control.Exception.
First, read Gabriel Conzalez's Breaking from a loop for using EitherT to do two different things depending on some condition arising, or MaybeT for just stopping right there in the event of a problem.
If you don't know anything about Monad Transformers, you can start with Martin Grabmüller's Monad Transformers Step by Step. It covers ErrorT. After that read Breaking from a Loop again!
You might also want to read Real World Haskell chapter 19, Error handling.
Call/CC
Continuation Passing Style's callCC is remarkably powerful, but perhaps too powerful, and certainly doesn't produce terribly easy-to-follow code. See this for a fairly positive take, and this for a very negative one.

A: So what I think you're looking for is the equivalent of return in imperative languages, eg
def do_something
  foo
  bar
  return baz if quux
  ...
end

Now in haskell this is doesn't work because a monadic chain is just one big function application. We have syntax that makes it look prettier but it could be written as
bind foo (bind bar (bind baz ...)))

and we can't just "stop" applying stuff in the middle. Luckily if you really need it there is an answer from the Cont monad. callCC. This is short for "call with current continuation" and generalizes the notation of returns. If you know Scheme, than this should be familiar.
import Control.Monad.Cont

foo = callCC $ \escape -> do
   foo
   bar
   when baz $ quux >>= escape
   ...

A runnable example shamelessly stolen from the documentation of Control.Monad.Cont
whatsYourName name =
  (`runCont` id) $ do
    response <- callCC $ \exit -> do
      validateName name exit
      return $ "Welcome, " ++ name ++ "!"
    return response

validateName name exit = do
  when (null name) (exit "You forgot to tell me your name!")

and of course, there is a Cont transformer, ContT (which is absolutely mind bending) that will let you layer this on IO or whatever.
As a sidenote, callCC is a plain old function and completely nonmagical, implementing it is a great challenge

A: So I suppose there is no way of doing it the way I imagined it originally, which is equivalent of a break function in an imperative loop.
But I still get the same effect below based in Ingo's answer, which is pretty easy (silly me)
doStuff x = if x > 5
            then do
                 t <- getTingFromOutside
                 doHeavyHalculations t
             else return ()

I don't know though how it would work if I need to test 't' in the example above ...
I mean, if I need to test the bound value and make an if decision from there.

A: You can never break out of a "monad sequence", by definition. Remember that a "monad sequence" is nothing else than one function applied to other values/functions. Even if a "monad sequence" gives you the illusion that you could programme imperative, this is not true (in Haskell)!
The only thing you can do is to return (). This solution of the practical problem has already been named in here. But remember: it gives you only the illusion of being able to break out of the monad!


text:
Q: Сравнение символа строки с VbNewLine (VbCrLf) не срабатывает Сравнение не срабатывает из-за чего цикл не останавливается, и в итоге программа аварийно завершается при выходе за границы строки.
Module Module1
    Function ReadUntilNewLIne(str As String)
        Dim currentIndex As Integer = 0
        Dim line As String = ""
        While str(currentIndex) <> vbCrLf
            line &= str(currentIndex)
            currentIndex += 1
        End While
        Return line
    End Function
    Sub Main()
        Dim arr(2) As String
        Dim line As String
        Dim str As String = "Hello" & vbNewLine
        str &= "How are you?" & vbNewLine
        str &= "Bye" & vbNewLine
        line = ReadUntilNewLIne(str)
        Console.WriteLine(str)
        Console.ReadKey()
    End Sub

End Module

Как правильно сравнить символ с символом переноса строки?

A: Попробуйте так:
Function ReadUntilNewLIne(str As String)
    Return Split(str, vbNewLine)(0)
End Function

@JustLearn я не понимаю, что значит (0) после split(str, vbnewline).

Функция Split делит строку по заданному разделителю в массив строк с Option Base 0.
Т.е. Split(str, vbNewLine) делит значение из переменной str по разделителю vbNewLine (символы новой строки) - иными словами, по строке на элемент. В результат получается некий array. И теперь добавим эти самые скобки - получим array(0) - т.е. нулевой элемент массива, или самая первая строка из переменной.
А всё...


text:
Q: How to encode url string with object in request parameters spring I.m trying to do request with the object as a request parameter in my Spring application.
new RestTemplate().getForObject("http://127.0.0.1:8080/items?filter={\"level\":\"BIG\"}", Item.class);

But get error:

As I discovered, the problem occurs when UriBuilder parses the string with url. I.e. new URI(...) gives the error. When I do the same request with Postman I get a desirable response. How I can properly encode url? I`m tried to use java.net.URLEncoder but this gives "IllegalArgumentException: URI is not absolute" error.
UPDATE:
Filter class in the request is used to do parameterized query ( Query() ) in mongodb in the remote server. Filter class:
@Getter
@NoArgsConstructor
public class Filter {

private Map<String, String> criteria;

@JsonAnySetter
public void add(String key, String value) {
    if (criteria == null) {
        criteria = new HashMap<>();
    }

    criteria.put(key, value);
}
}

Item object class:
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Item {
private String id;
private String name;
private Instant timestamp;
}

A: i have problem like yours and i solve it using following snippet code
UriComponentsBuilder renewURIBuilder= UriComponentsBuilder.fromHttpUrl("http://127.0.0.1:8080").path("/items").queryParam("filter", "{'level':BIG}");
UriComponents uriComponent=renewURIBuilder.build(true);
URI uri=uriComponent.toUri();

then call your resttemplate like the following
new RestTemplate().getForObject(uri, Item.class);

hope it help you


text:
Q: View does not update when changing RealmObject passed as binding I have a view, where I have an array of RealmObjects. When I pass them into another View via @Binding and try to edit them there, the View does not get updated.
This is a simplified example. When the name changes, it dumps the correct name into the console, but the View does not update. Tried to force a reload by changing the .id() of the View, but this doesn't help either.
class Person: Object, ObjectIdentifiable {
  @Persisted(primaryKey: true) var _id: ObjectId
  @Persisted var name: String = ""
}

struct ParentView: View {
  @State private var persons: [Person] = []

  var body: some View {
    ChildView(persons: $persons)
  }
}

struct ChildView: View {
  @Binding var persons: [Person]

  var body: some View {
    ForEach(person) { person in
      Text(person.name)
        .onTapGesture {
          person.name = "New Name"
          dump(person)
        }
    }
  }
}

A: One issue is mixing two different technologies by assigning Realm objects to a Swift array - generally speaking, that may not be the best idea.
Realm objects are lazily-loaded and massive datasets take almost no memory. However, as soon as those objects are cast to Swift functions and constructs they are all loaded into memory and can overwhelm the device.
Best practice is to keep Realm objects within Realm stuctures e.g. Collections like Results and Lists.
To address the question:
Realm provides an object @ObservedResults which is a property wrapper that invalides a view when observed objects change. e.g. If the app has @ObservedResults of a group of People objects, if one of those changes, the associated view will also update. From the docs

You can use this property wrapper to create a view that automatically
updates itself when the observed object changes.

So change this
struct ChildView: View {
  @Binding var persons: [Person]

to this
struct ChildView: View {
  @ObservedResults(Person.self) var persons

I think the rest of the code is good to go as is.


text:
Q: Extend max connections - Python websockets 9.1 and simple-websocket-server 0.4.1 I wish I could have about 3k connections at once, but when running the server I'm having a bad crash (on about 500 connections in Win 10 x64 and on about 1019 connections on Ubuntu) with both of websockets libraries:
Here is the stresstestscript.py which cause the issue (even if I run this one in different machines over the lan):
import websocket,time
serveraddr="ws://localhost:12345"
ws = websocket.WebSocket()
ws.connect(serveraddr)
list = []
for b in range(3000):
    list.append(websocket.WebSocket())
count=1
for x in  list:
    count+=1
    x.connect(serveraddr)
    time.sleep(0.01)
time.sleep(5000)

Here is the server.py (the exact sample on websockets official documentation):
#!/usr/bin/env python

# WS server example that synchronizes state across clients

import asyncio
import json
import logging
import websockets

logging.basicConfig()

STATE = {"value": 0}

USERS = set()

def state_event():
    return json.dumps({"type": "state", **STATE})

def users_event():
    return json.dumps({"type": "users", "count": len(USERS)})

async def notify_state():
    if USERS:  # asyncio.wait doesn't accept an empty list
        message = state_event()
        await asyncio.wait([user.send(message) for user in USERS])

async def notify_users():
    if USERS:  # asyncio.wait doesn't accept an empty list
        message = users_event()
        await asyncio.wait([user.send(message) for user in USERS])

async def register(websocket):
    USERS.add(websocket)
    print("add")
    await notify_users()

async def unregister(websocket):
    USERS.remove(websocket)
    await notify_users()

async def counter(websocket, path):
    # register(websocket) sends user_event() to websocket
    await register(websocket)
    try:
        await websocket.send(state_event())
        async for message in websocket:
            data = json.loads(message)
            if data["action"] == "minus":
                STATE["value"] -= 1
                await notify_state()
            elif data["action"] == "plus":
                STATE["value"] += 1
                await notify_state()
            else:
                logging.error("unsupported event: %s", data)
    finally:
        await unregister(websocket)

start_server = websockets.serve(counter, "localhost", 12345)

asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

Here is the server error (on windows, when exceed the 500 connections opened at once):
Traceback (most recent call last):
  File "websockettestserver.py", line 70, in <module>
    asyncio.get_event_loop().run_forever()
  File "C:\Python37-32\lib\asyncio\base_events.py", line 539, in run_forever
    self._run_once()
  File "C:\Python37-32\lib\asyncio\base_events.py", line 1739, in _run_once
    event_list = self._selector.select(timeout)
  File "C:\Python37-32\lib\selectors.py", line 323, in select
    r, w, _ = self._select(self._readers, self._writers, [], timeout)
  File "C:\Python37-32\lib\selectors.py", line 314, in _select
    r, w, x = select.select(r, w, w, timeout)
ValueError: too many file descriptors in select()

Like I said, I wish I could have more simultaneous connections, increasing the about 1000 connections on Linux to 3000-4000 (!?).
How to fix this?

A: 1.run more diff server.py on diff port
2.use nginx to Distribute the requests


text:
Q: Django annotation with reference to another table I have two Django models as follows:
class TableA(Model):
    some_string_id: str
    something_i_need: int

class TableB(Model):
    some_string_id: str

I am trying to create a group_by operation of some_string_id of TableA, but also add the something_i_need from TableB accordingly. I thought on doing something as follows, but am not sure what to replace the ??? with. Any ideas?
TableA.objects.filter(...).values("some_string_id").annotate(something_i_need=???)

P.S:
I know that a foreign key could have helped, but those are not my tables to edit...


text:
Q: Get max result from SQL with VB.net I have the following code:
query = "SELECT ResultDate, Result FROM ASPECTRESULTS WHERE EUPN = '" & StudentEUPN & "' AND SubjectCode = '" & SearchClass & "' AND ACCYEAR = '" & AccademicYear & "' ORDER BY RESULTDATE"

I want to get the latest result based on the date of the result.
I tried using SELECT MAX(ResultDate) but with no luck.
Any help appreciated.
Regards
Graham

A: You could write (assuming that ResultDate is a Date type column in your table)
query = "SELECT TOP 1 ResultDate FROM ASPECTRESULTS WHERE ..... " & _
        "ORDER BY RESULTDATE DESC"

This will return the highest date in the ResultDate column
Said that, you should really study how a parameterized query works and why you should ALWAYS use parameters instead of string concatenations
EDIT
From your comment above (ResultDate is a Char(10)), then getting this field correctly ordered is real pain, in particular if you store the date as dd/MM/yyyy. With this format in a char ordering the value '11/01/2013' follows the value '01/01/2014'. This is one of the many reasons to ALWAYS store datetime values in datetime fields. The database will know how to correctly handle a date without considering how is represented by a string.
You have two options to overcome from this hurdle.

*

*Change the datatype of the field ResultDate to a DateTime field (recommended)

*Store your values using a 'sortable' char format  (yyyy/MM/dd with
always two digits for months and days (not recommended)


text:
Q: FreeBSD Netboot over TFTP Is there a way to boot FreeBSD over TFTP?
All the instructions I've seen require you to transition to NFS after PXEBOOT has loaded.

A: No, you can only grab a kernel image and initial ramdisk over TFTP.
Once the kernel has loaded it needs somewhere to load the root filesystem from. The FreeBSD kernel only knows how to use UFS, ZFS or NFS as its root filesystem.
You may be able to create a ramdisk loaded over TFTP from something like mfsBSD to have a UFS image in memory.


text:
Q: JAXB javax.xml.bind.PropertyException I am getting the below error when i try to read XML file that has some japanese characters.
javax.xml.bind.PropertyException: jaxb.encoding
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.getProperty(AbstractUnmarshallerImpl.java:360)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.getProperty(UnmarshallerImpl.java:423)
at com.jaxb.JAXBTest.main(JAXBTest.java:23)
enter code here

package com.jaxb;
import java.io.FileReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class JAXBTest
{
    public static void main(String args[])
    {

        try
        {
            JAXBContext context = JAXBContext.newInstance(com.pain.jaxb.ver2.Document.class);
            Unmarshaller um = context.createUnmarshaller();
            um.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            com.pain.jaxb.ver2.Document PainTransferList2 = (com.pain.jaxb.ver2.Document) um.unmarshal(new FileReader("C:/WorkArea/JAXB/src/com/pain/messages/APXSEPAS_510812_1.XML"));

        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

    }

}

Please advice.
Thanks
Rafi

A: You're setting a Marshaller property on an Umarshaller:
Unmarshaller um = context.createUnmarshaller();
um.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");

You can only set Unmarshaller properties on an Unmarshaller.
Remove the setProperty and try again.


text:
Q: requests.get() not returning proper text order when reading youtube HTML I'm using requests.get() to parse youtube HTML text. When I print the output after passing a link, one video is out of order.I am trying to play the videos in order using BeautifulSoup and the out of order video shows up before all other videos. Any advice or potential fixes would be helpful.
    true = requests.get(link +searched)
    page = true.text
    #print(page)
    soup = BeautifulSoup(page, 'html.parser')
    #print(soup)
    search_results = soup.findAll('a', attrs={'class': 'yt-uix-tile-link'})
    #print(search_results)

Additional code for more reference:
import pafy
import vlc
import requests
from bs4 import BeautifulSoup
import time
link="https://www.youtube.com/results?search_query="
youtube = "https://www.youtube.com"
word = "jid playlist"

def findlnks(searched):
    if '&list' not in searched:
        true = requests.get(link +searched)
        page = true.text
        #print(page)
        soup = BeautifulSoup(page, 'html.parser')
        #print(soup)
        search_results = soup.findAll('a', attrs={'class': 'yt-uix-tile-link'})
        #print(search_results)
    else:
        true = requests.get(searched)
        page = true.text
        soup = BeautifulSoup(page, 'html.parser')
        #print(soup)
        search_results = soup.findAll('a', class_ ="spf-link playlist-video clearfix yt-uix-sessionlink spf-link")
        #print(search_results)
    return search_results

if 'mix' in word or 'playlist' in word:
    total_results = findlnks(word)
    i =0
    playlist_size=0
    #while i< 10:
        #print(total_results[i]['title'])
        #i+=1
    while 'list' not in (total_results[i])['href']:
        print(total_results[i]['href'])
        i = i + 1

    playlist_results=findlnks(youtube + ((total_results[i])['href']))

    while playlist_size<40:
        #print(youtube + (playlist_results[playlist_size])['href'])
        playlist_size = playlist_size +1

    while (playlist_results[playlist_size])['href'] !='\0':
        url = youtube + (playlist_results[playlist_size])['href']
        #print(url)
        video = pafy.new(url)
        best = video.getbest()
        playurl = best.url
        Instance = vlc.Instance()
        player = Instance.media_player_new()
        media = Instance.media_new(playurl)

        media.get_mrl()
        player.set_media(media)
        player.play()
        playing = set([1, 2, 3, 4])
        time.sleep(1)
        duration = player.get_length() / 1000
        mm, ss = divmod(duration, 60)

        while True:
            state = player.get_state()
            if state not in playing:
                break
            continue
        playlist_size = playlist_size +1

A: Youtube uses ajax technology for the site. The requests library only works with the non-ajax site. Consider using the headless browser to get the html from youtube then extract the data.
You can you firefox in headless mode or phantomjs browser
I found this tutorial, it may help for you.
https://towardsdatascience.com/web-scraping-using-selenium-and-beautifulsoup-99195cd70a58
# Firefox session
driver = webdriver.Firefox()
driver.get(videos_url)
# Try to increase the time if the page need more time to be fully loaded
driver.implicitly_wait(100)


text:
Q: Not showing mesage when validating excel file in laravel import I am using excel 3.1 in Laravel for importing excel files. I have included a validation for the name field. So whenever the name field is duplicating it will not import that row data to DB. everything works fine up to here. but the problem is I want to show success message in the controller. But the message is not showing and not showing any other error messages.
This is my import function
namespace App\Imports;

use App\Products;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Maatwebsite\Excel\Concerns\SkipsOnError;
use Maatwebsite\Excel\Concerns\SkipsErrors;
use Maatwebsite\Excel\Concerns\Importable;
use App\Categories;
use App\Brand;
use App\Unit;
use Maatwebsite\Excel\Concerns\WithValidation;
// use Illuminate\Validation\Rule;
// use Throwable;
class ExcelImport implements ToModel, WithStartRow, WithValidation, SkipsOnError
{
    use Importable, SkipsErrors;
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {
        $name = $row[0];
        $des = $row[1];

            $product = new Products([
                'name' => $row[0],
                'description' => $row[1],
            ]);
            return $product;
       }

    public function startRow(): int
    {
        return 2;
    }
    public function rules(): array{
        return[
            '0' => 'unique:products,name',

        ];
    }

}

this is my controller
public function uploadfile(Request $request)
    {
        $this->validate($request, [
            'file' => 'required',
        ]);

        if($request->hasfile('file'))
        {

            foreach($request->file('file') as $file)
            {

                $import = new ExcelImport;
                $import = $import->import($file);

            }
         //if the import is successful then I want to show some messages here
        }

        else{
            return back()->with('error','File contains invalid data. Please upload a valid file.');
        }
    }


text:
Q: Receiving server broadcast on mobile in real time I have to write a simple mobile application that is able to receive broadcast from a server, connected using GPRS, over a secure SSL connection.
The amount of data the mobile needs to receive is very small (a string of about 100 characters length for a single broadcast and maximum 10 broadcasts per minute).
I need to display it on screen, generate an alert sound, and vibrate the device.
The client doesn't need to send anything back to the server. (Of course, at the time of initialization the client app needs to send some data for authorization.)
The target mobile platforms are Symbian S60 and Blackberry.
How can I do this? What tools, SDK and third-party framework (if any) should I use?
I am familiar with Java and network programming but not for mobile devices.

A: You may want to check out the BlackBerry Push API.  It is available for both native apps as well as widgets.

A: Java ME can probably be use to connect a secure socket to the server. Of course you need to keep it alive all the time as their is no easy way for the server to initiate a connection.
If you need the server to initiate the connection you can use SIP, but I don't think there are any Symbian S60 devices that support the SIP API for Java ME (JSR-180). Symbian has a C++ SIP API.


text:
Q: increasing the performance of uitableview I have this application that I am letting the user set up a search query based of values selected by them from a bunch of xml data that will then be used to search the database for stuff.
At the moment I have 3 cells that can accept search parameters for the query, each cell loads its data onto the same uitableview so I have to do abit of fudging with my if statements to make sure everything works correctly for the correct dataset.
the first cell allows the user to pick a motorbike manufacture, all of the manufactures are listed on the table view. the xml that is being loaded has a key value that is related to the second list which lists the model of every manfacture. I am wanting to set up the list of manufactures based of the manufacture selection as soon as the user selectes a tableviewcell.
so that when the user is back to the main view then selects the second cell the (dictionary) that i have created loads really fast.
At the moment everything is done each time the subview is loaded.. so because my manufacture list is so big, when the user selects the manufacture they would like to search when the come back to the subview with the manufactre selected the id has to be compaired to every single model of every single manufacture to get the list to display which takes a very long time.
I would like to know how you would attempt something like this. below is a flow chart of how I am thinking to do it. I would like some comments/suggestions on if you think im doing it right or not and also how to even attempt this.

any help would be greatly appreciated (if you cannot read the text in the image you can right click save to desktop and its alot bigger)

A: To do this properly you probably want to use some sort of data structure in your application which allows fast querying rather than doing multiple parses of your xml input file.
For a simple "first-cut" solution you could parse the XML file into an NSDictionary and query this for Manufacturer -> Model relationships. You can build the NSDictionary once off the XML file then persist the result somewhere and searching will be much faster than looking through the XML each time.
For something more robust it'd be worth looking into using CoreData to store your Manufacturer -> Model relationship. This would take more code on your behalf but would give you a much nicer object model.


text:
Q: docker swarm service all replicas in one node I created a swarm with 4 node, 2 manager, 2 worker. one of the manager is in drain mode.
i am running 3 replicas of an nginx based web app built using vue.js only, no backend, no db.
now when i run the docker service ls command, i get the following:
$ docker service ls
ID                  NAME                  MODE                REPLICAS
4nk9b9zm3rlp        friendly_hugle        replicated          0/3
qwqj07gu03zu        intelligent_khayyam   replicated          0/3
37tol0zrs82n        strange_hodgkin       replicated          0/3
dvl4r0vimqtd        suspicious_greider    replicated          3/3

why all my replicas are running on one node (3/3)? Just started learning docker.

A: run the command,Check whether the running node is in normal state,
docker node ls
docker service ps strange_hodgkin


text:
Q: Resizing a partition in Linux - Bad magic number in super-block error I was trying to resize my partition with parted and resize2fs.
I tried the following:
#parted
Partition Table: msdos
Number  Start   End     Size    Type     File system     Flags
 1      2097kB  21.0GB  21.0GB  primary  ext4            boot
 2      21.0GB  500GB   479GB   primary  ext4
 3      500GB   500GB   536MB   primary  linux-swap(v1)

(parted) rm 2
(parted) mkpart
Partition type?  primary/extended? primary
File system type?  [ext2]? ext4
Start? 41GB
End? 500GB
(parted) q
Information: You may need to update /etc/fstab.

#resize2fs /dev/sda2
resize2fs 1.42.5 (29-Jul-2012)
resize2fs: Bad magic number in super-block while trying to open /dev/sda2
Couldn't find valid filesystem superblock.

Unfortunately I can't understand why this doesn't work. It was an ext4 partition. I would like to resize the partition without loss of data.
fdisk list before operation:
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        4096    40962047    20478976   83  Linux
/dev/sda2        40962048   975718399   467378176   83  Linux
/dev/sda3       975718400   976764927      523264   82  Linux swap / Solaris

Now fdisk shows:
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        4096    40962047    20478976   83  Linux
/dev/sda2        80078848   975718399   447819776   83  Linux
/dev/sda3       975718400   976764927      523264   82  Linux swap / Solaris

A: First, I'd like to be very clear on an important distinction:

*

*Partitions are simply pointers to where filesystems begin and end. On modern disks, these start and end points are expressed as sector values.

*Filesystems are complex data structures that reside within partitions (or other containers). Typically, filesystems are defined relative to the start point of the containing partition.

Your /dev/sda2 used to begin at sector 40,962,048. You changed it so that it now begins at sector 80,078,848. Thus, the critical early parts of the filesystem now begin outside the partition. An analogy might be if you somehow moved the front cover of a reference book, sitting flat on a table, a centimeter or so down without moving any of the pages of the book. When you opened the book, you'd find that the table of contents was missing, along with the first chapter or two. Those pages would now be floating freely, extracted from the book. You'd therefore be unable to find anything in the book except by reading the whole thing -- and even then, parts of it would be missing.
Generally speaking, any partitioning operation that involves moving or resizing the start of a partition is best done using GParted, which knows the proper order of operations for moving and resizing partition and filesystem data structures to keep everything working properly. Even GParted is not without its risks when resizing partitions, though -- especially not when the start point is involved, since this requires moving significant amounts of data, so a bug, power failure, hardware fault, or other problem can end up trashing the whole partition. If you must do it without GParted, you can, but you have to fully understand the distinction between a partition and a filesystem that I've outlined, and understand the implications of this distinction for the order in which you perform various operations (resize filesystem, move filesystem, change partition boundaries).
In your specific case, what I recommend is:

*

*Delete your new /dev/sda2.

*Re-create your /dev/sda2 using its original start and end sector values. Be sure these are exactly the same as they were before.

*Check that the re-created /dev/sda2 is accessible.

*Back up your important data from /dev/sda2.

*Re-evaluate whether you really need to move the start of the partition. I don't know what your ultimate goal is, so I can't do this for you. Perhaps moving the end point would work as well (and that would be safer); or maybe adding another disk would be a better option; or maybe you don't really need to repartition at all.

*If you must repartition the disk, use GParted to do the job.


text:
Q: Click action only not working in mareshmallow Here is the my full xml ....where inside linear layout action click is not working fully in marshmallow 6.0.But,in other android version up-to 5.1 its working perfectly.
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/root_coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

 <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

  <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                android:background="@color/appnewcolor"
                android:theme="@style/AppTheme.AppBarOverlay" >

                <com.unikkapps.android.contest.utils.Text
                    android:id="@+id/toolbar_title"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:gravity="center"
                    android:textSize="20dp"
                    android:textColor="@color/white"
                    android:text="Flickty" />
             </android.support.v7.widget.Toolbar>
         </android.support.design.widget.CollapsingToolbarLayout>

        <com.unikkapps.android.contest.utils.TablayoutCustom
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:background="@color/appnewcolor"
            android:elevation="6dp"
            android:fontFamily="sans-serif"
            android:minHeight="?attr/actionBarSize"
            android:paddingLeft="@dimen/dim_10"
            app:tabGravity="fill"
            app:layout_collapseMode="pin"
            app:tabIndicatorColor="@color/white"
            app:tabMode="scrollable"
            app:tabSelectedTextColor="@color/white"
            app:tabTextColor="@color/white_87" />

          </android.support.design.widget.AppBarLayout>

       <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

       <LinearLayout
        android:id="@+id/bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_above="@+id/pager"
        android:layout_gravity="bottom"
        android:gravity="center"
        android:background="@color/black"
        android:orientation="horizontal"
        android:clickable="true"
        android:weightSum="5">

        <LinearLayout
            android:id="@+id/refresh_layout"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:layout_marginLeft="10dp"
            android:layout_weight="1"
            android:gravity="center">

            <ImageView
                android:id="@+id/iv_refresh"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@null"
                android:src="@drawable/ic_bt_bar_home" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/search_layout"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:layout_marginLeft="10dp"
            android:layout_weight="1"
            android:gravity="center">

            <ImageView
                android:id="@+id/iv_search"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@null"
                android:src="@drawable/ic_bt_bar_search" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/camera_layout"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:layout_marginLeft="10dp"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/iv_plus"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@null"
                android:src="@drawable/ic_bt_bar_camera" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/watchpost_layout"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:layout_marginLeft="10dp"
            android:layout_weight="1"
            android:gravity="center">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center">
                <ImageView
                    android:id="@+id/iv_cam"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:background="@null"
                    android:src="@drawable/ic_bt_bar_notification" />

                <com.unikkapps.android.contest.utils.Text
                    android:id="@+id/notificationcount"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignEnd="@+id/iv_cam"
                    android:layout_alignRight="@+id/iv_cam"
                    android:layout_alignTop="@+id/iv_cam"
                    android:layout_marginLeft="@dimen/dim_10"
                    android:gravity="right"
                    android:text=""
                    android:textColor="@color/appnewcolor"
                    android:textSize="14dp" />
            </RelativeLayout>

        </LinearLayout>

        <LinearLayout
            android:id="@+id/contest_layout"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:layout_marginLeft="10dp"
            android:layout_weight="1"
            android:gravity="center">

            <ImageView
                android:id="@+id/iv_context"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@null"
                android:src="@drawable/ic_bt_bar_contest" />

        </LinearLayout>

    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

<ListView
    android:id="@+id/list_slidermenu"
    android:layout_width="290dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/white"
    android:divider="@null"
    android:gravity="center_vertical|center_horizontal" />
  </android.support.v4.widget.DrawerLayout>

Thank you for your help guys in advance.

A: add property in your Linear layout as below,
android:onClick="onClick"
and add method in your Java class,
   public void onClick(View v)
{
    switch (v.getId())
    {
        case R.id.bottom:
            Toast.makeText(MainActivity.this,"on click works",Toast.LENGTH_LONG).show();

    }

}

It's Work, Tested in both 5.0 and 6.0


text:
Q: How to take average between NaN values? I have a simple list that contains numbers and NaN values. Is there a way to take the AVG between two NaN values? an example could be like this:
list = [NaN, 5, 6, 7, NaN, NaN, NaN, 6, 2, 8, 5, 4, NaN, NaN]

and I would expect an output like
Output = [6,5]

A: Use the groupby from itertools -
import numpy as np
from itertools import groupby
NaN = np.nan
lst = [NaN, 5, 6, 7, NaN, NaN, NaN, 6, 2, 8, 5, 4, NaN, NaN]
[np.mean(list(g)) for k, g in groupby(lst, key=lambda x: x is not NaN) if k]
# [6.0, 5.0]

A: A simple method requiring no additional skills:
import numpy as np

## NaN is assumed to be pre-defined by the users, e.g.: NaN = np.nan or NaN = float('nan')

def get_mean_between_nan(ar):
    out = list()
    t = list()
    for x in ar:
        if np.isnan(x):
            if len(t) > 0:
                out.append(np.mean(t))
                t = list()
        else:
            t.append(x)
    if len(t) > 0:
        out.append(np.mean(t))
    return out

A: Following on from https://stackoverflow.com/a/30825549/1021819, first split the list into a chunked list-of-lists:
NaN=None # or np.nan, float('nan'), 'nan' or any other separator value you like = even '/'
my_list = [NaN, 5, 6, 7, NaN, NaN, NaN, 6, 2, 8, 5, 4, NaN, NaN]

from itertools import groupby
chunks = list(list(g) for k,g in groupby(my_list, key=lambda x: x is not NaN) if k))
# [[5, 6, 7], [6, 2, 8, 5, 4]]

Then you can use the built-in statistics.mean() as follows:
import statistics
output = [statistics.mean(chunk) for chunk in chunks]
# [6, 5]

There you go.
Notes:

*

*No need for numpy. But if you do want a pure numpy solution you can use https://stackoverflow.com/a/31863171/1021819

*Don't use list for your variable name since it is the name of a built-in type!


text:
Q: Image links change to an image that exists This is a really bizarre issue that I've been having.
It happens to images that seem to have a name that is similar to something else.
For instance, if I have an image named image0001.png and I try to display an image with the source of image0010.png - which doesn't exist - then the image does not display as nothing, or fire onerror image, instead it displays as image0001.png which does exist.
I have no idea how to to fix this because I don't really know what is going on.
I am using the following code to fetch results from the database and produce a table with item details.
<?php while ($row = $retval->fetch_array()) { ?>

            <a href="/path/to/item/<?= $row['id']; ?>.php"><?= $row['id']; ?>

        {$row['name']}

            <a href="/path/to/item/<?= $row['id']; ?>.php">
                <img src="/images/<?= $row['id']; ?>.jpg" onerror="this.src='/images/error.png';">

        <?= $row['description']; ?>

<?php } ?>

A: Sounds like mod_speling is enabled on your server.

Requests to documents sometimes cannot be served by the core apache server because the request was misspelled or miscapitalized. This module addresses this problem by trying to find a matching document, even after all other modules gave up.

Documentation
You can try to disable this apache module in a .htaccess file:
CheckSpelling Off

A: The obvious other alternative is to use file_exists, removing the reliance of JavaScript and ensuring that the image is shown if it exists or an error image if not.
<?php while ($row = $retval->fetch_array()) { ?>

            <a href='/path/to/item/<?= $row['id']; ?>.php'><?= $row['id']; ?>

        {$row['name']}

            <a href='/path/to/item/<?= $row['id']; ?>.php'>
                <?php if (file_exists("/images/{$row['id']}.jpg")) { ?>
                    <img src="/images/<?= $row['id']; ?>.jpg" />
                <?php } else { ?>

                <?php } ?>

        <?= $row['description']; ?>

<?php } ?>


text:
Q: Does the t statistic have uses unrelated to hypothesis testing? Are there non-hypothesis testing uses for Student's t statistic?

A: A "hypothesis test" in the strictest sense always results in a binary outcome of either rejecting or failing to reject a null hypothesis. T-statistics are generally turned into p-values, which are then compared against some pre-defined threshold to make that binary determination. It is possible to use the t-statistic itself, however, as a general measure of "deviation from the null", without ever having to take the final step of testing whether the null hypothesis should be rejected or not. Using a t-statistic in this way is still derived from a hypothesis testing framework, but does not actually result in a test of whether the null should be rejected or not, so I'd argue this is not strictly a "hypothesis test".
As an example, the t-statistic can be used as a means of ranking features by significance, while accounting for the directionality of the differences. Gene set enrichment analysis, for example, searches for sets of consistently up- or down-regulated genes, so the directionality of differences is important for this method. Ranking features by their p-value will draw no distinction between up- and down-regulated genes, and simply put the most significant genes at the top of the list. Ranking by the t-statistic, on the other hand, will put the most significant up-regulated genes at one end of the list, and the most significant down-regulated genes at the other end. Although the magnitude of the t-statistic is directly related to the p-value, the sign of the t-statistic is lost when calculating a p-value for a hypothesis test. Ranking genes in this way respects the directionality and how incompatible with the null hypothesis each gene is, but does not actually make any determination if any gene is "significantly dysregulated" or not.

A: When you ask about "the t-statistic", I think about the concrete quantity $$\frac {{\bar {X}}-\mu }{S/{\sqrt {n}}}$$
To actually calculate this quantity, we have to specify $\mu$. This is typically chosen in reference to some given null hypothesis. So to me it seems awkward to try to disentangle "the statistic" from the null hypothesis to which it is implicitly linked by $\mu$. Setting $\mu$ to 0, for example, which you're implicitly doing when you type t.test(rnorm(10))$statistic into R, is implicitly related to the hypothesis test $H_0: \mu = 0$.
Where I think of Student's t-distribution as useful is as a parametric form for fitting to data. At the end of the day, it's just another symmetric, bell-shaped distribution. It just has fatter tails than a Gaussian. So it can be used to model things for which you'd like to preserve that symmetry and bell-shape, but give the extreme outcomes more probability mass than a Gaussian does. I know it's used in finance to model asset-returns (link 1, link 2) for example, but I can't speak to how successful or useful these kinds of models are because I don't use them myself.
I'd suspect them to be of particular use to hierarchical modelers who have some prior knowledge that points to fat tails. Gelman briefly discusses using the t instead of the the Gaussian in fat-tail situations in section 17.2 of Bayesian Data Analysis.


text:
Q: How is the PDO database APIsafeguarding your application against SQL injections? People tend to get religious about picking the PDO database API over mysqli. Often you find questions regarding mysqli answered with statements like 'Forget mysqli, use PDO. Its the safe way'. So I follow along, getting a grasp of this PDO concept, preparing statements and binding them etc.
But what is the big deal? Why are people making this effort to use there database by this means? How is this  safeguarding your application against SQL injections?

A: PDO isn't a safeguard against SQL injection. You can still write utterly dangerous injectable queries all you want in PDO, and PDO won't care.
What PDO does is provide TOOLS that allow you to write queries safely.
But don't go blame PDO if it providers a safe hammer, and then you go on using your forehead to drive in some nails. PDO did its job and provided the tools, you're the one with nail holes in your skull.


text:
Q: Passing a method to another method for thread starting I'm currently in the process of creating a class that has general methods that specialised methods of the class call. One of these general methods needs to get a method as parameter to use it to start a new thread.
My question is how this parameter must be done (thus what I must replace  with, or what do I need to change in total to make it work as intended):
Class mytest
{
   public void myPublicStarter()
   {
       Starter("Test", this.DoWork);
   }

   public void DoWork(object myIDString)
   {

   }

   private void Starter(string myIDString, <METHOD> paramethod)
   {
        Thread myThread = new Thread(paramethod);
        myThread.Start(myIDString);
   }

}

A: When you create a new Thread object you have to pass it a delegate, i.e. an object that refers to a method.  That delegate must be type ThreadStart if the entry method has no parameters and ParameterizedThreadStart if it does.  In your case, your DoWork method has a parameter so the delegate type must be the latter.  Because the paramethod parameter is declared as that type, the compiler knows to cast this.DoWork as that type when you call Starter.  If the method signature did not match that of the delegate then a compilation error would occur.
public void myPublicStarter()
{
    Starter("Test", this.DoWork);
}

public void DoWork(object myIDString)
{

}

private void Starter(string myIDString, ParameterizedThreadStart paramethod)
{
     Thread myThread = new Thread(paramethod);
     myThread.Start(myIDString);
}

A: Well, in .NET you have the concept of delegates, and you have Actions and Functions which are in fact just delegates.
A Function represents a function that takes some input parameters and has a return value.
An action represents a method that takes some input parameters but doesn't return any value.
So it's depending on what you woud like to achieve.
Here's a demo sample:
static void Main(string[] args)
{
    myPublicStarter();
}

public static void myPublicStarter()
{
    Starter("Test", () => DoWork("My Id String Passed here"));
}

public static void DoWork(object myIDString)
{
    Console.WriteLine(myIDString);
}

private static void Starter(string myIDString, Action paramethod)
{
    paramethod.Invoke();
}

}
Note: I've made the classes static as I've converted it to a console application.

Now, to adapt the code to work with threads, you can use the following:
static void Main(string[] args)
{
    myPublicStarter();
}

public static void myPublicStarter()
{
    Starter("Test", x => DoWork("My Id String Passed here"));
}

public static void DoWork(object myIDString)
{
    Console.WriteLine(myIDString);
}

private static void Starter(string myIDString, Action<object> paramethod)
{
    Thread myThread = new Thread(x => paramethod(x));
    myThread.Start(myIDString);
}

This will generate the exact same output as listed above.


text:
Q: Using If/Else for PHP menu navigation I'm looking for some feedback on using If/Else for my menu navigation or links to resources.  I have done a few quick searches and have found items relating to using PHP to keep track of the current page and apply specific CSS rather than implementing the menu itself.
What I'm trying to do is have the menu change my content div based on the menu.  So if "home" is clicked, the div changes to home and if "about" is clicked, the div changes to about.
What I've done so far is to create a variable 'cl' that I set using the id attribute in each div.  In my index page I use the following:
//if cl is not set include default content div
if(!isset($_GET['cl']))
{
     //include main div
}

//if cl is set, check value and include the correct div
//anything that does not match a hard-coded cl value --SHOULD-- default to main
//div...may change this to be a custom error page
if(isset($_GET['cl']))
{
    if($_GET['cl']=="content-main")
    {
        //include main content div
    }
    elseif($_GET['cl']=="content-anotherPage")
    {
        //include another page div
    }
    else
    {
        //include main content or possibly custom error
    }
}

I have more elseif blocks thrown in for other menu items, but left this small to just show what I'm doing.
Is this method a valid approach?  Are there any basic things I'm missing that I should be aware of?  Is my If/else block secured? (By secure I mean changing the value of URL should just revert to the default content div.  I tried placing a basic javascript alert script into the URL and it did default to the main page, but not sure if my basic test relates to actual security).
First time poster...please let me know if I have not followed etiquette and I will gladly correct!
Mike

A: instead of bunch of if/elseif's I'd rather use switch/case syntax:
switch( $_GET['cl'] ) {
    case "content-main":
       ...
       break;

    default:
       ...
       break;
}

A: If you have lots of conditions, I would use a switch statement instead  http://www.w3schools.com/php/php_switch.asp as it's much faster. If you only have a few conditions like less than 5 or 6, if/else is fine.


text:
Q: Why org.aopalliance.intercept.Joinpoint#proceed throws Throwable? I am dealing with Spring AOP, my code is like -
@Component
public class MyClass implements MethodInterceptor{

    @Override
    public Object invoke(MethodInvocation invocation){
      //some code here
        try {
            return joinPoint.proceed();
             } catch (Throwable throwable) {
                throw Throwables.propagate(throwable);
             }
        }
  }

As handling Throwable is not a recommended(since it also includes errors), I want to avoid that.
However, I am curious to know that why org.aopalliance.intercept.MethodInterceptor throws throwable in the first place? Throwing an Exception shouldn't be sufficient?

A: To answer the question we need to look at the purpose of the org.aopalliance.intercept.Joinpoint#proceed which is (as per docs)

Proceed to the next interceptor in the chain.

This means that it will intercept the next method execution in the chain. The actual method when invoked can throw any any kind of Exception and / or Error (there is no way to ensure / restrict the method not to do so be it recommended or not).
So to be generic enough (and also compatible with legacy / third party / library code) the developers of AOPAlliance must have taken a conscious decision of throwing the top most level class i.e. Throwable.
Hope this helps!


text:
Q: Can quest critical NPCs be killed during wasteland traveling? In Fallout 3 there are some parts of some quests that involve NPCs walking with you (or leading you) through the wasteland. One example of this is traveling with your dad to Rivet City. It's one hell of a walk, and surely there are some monsters on the way.
I have never had the time to check it out, but can the NPCs be killed by some randomly encountered monster in this case? Or maybe it is scripted so that no monsters appear? Or maybe said NPCs can defend themselves?

A: The game has a way of protecting certain "critical" NPCs that must survive in order to continue the game - they are marked as "Essential."
Most essential NPCs that follow you or lead you will defend themselves if attacked.  However, if they sustain damage enough to be killed, they will simply be knocked unconscious, and they will get up as if nothing happened later.
There are some side quests that it is possible to fail if a key NPC dies, but in almost every case where the death of an NPC would prevent you from finishing the game, the required NPCs are marked as essential.
The Fallout Wikia has pages for each character, and there is typically an information box that specifies whether or not the character is essential, and under what circumstances they get or lose this attribute.
In the specific case you mention, your father is marked as essential during the trip to Rivet City, so there is no danger of him dying during this trip.  (Do note that there are spoilers on that page if you haven't finished the game...)


text:
Q: Properties of periodic functions Let $f$ and $g$ be periodic functions of period $p$.  Then $af(x)+bf(x)$ with $a,b$ constants and $f(x)g(x)$ are both of period $p$
I'm not exactly sure how to prove these properties of periodic functions.  I think I may have proven the first one, but as I remarked, I'm not sure.
To prove the first one, $f(x)=f(x+p)$.  So $a f(x+p)+bg(x+p)=af(x)+bg(x)$.

A: The same approach works for the other example:
$f(x+p)g(x+p)=f(x)g(x+p)=f(x)g(x)$. The first equality follows from the $p$-periodicity of $f$, the second from that of $g$.


text:
Q: Как получить график громкости аудио без его воспроизведения? new AudioContext насколько я понял работает только во время воспроизведения аудио, есть какой то способ получить данные о громкости звука в зависимости от времени без jquery и кликов на скрытый тег audio? Может это можно как то в php сделать

A: Способ есть - всё тот же AudioContext:

<meta charset="utf-8">
<title>Визуализатор</title>

<audio id=audio src=sound.mp3" controls></audio>
<canvas id=canvas width=512 height=256></canvas>

График появляется по загрузке страницы без всяких кликов. Единственное, приходится где-то полминуты подождать, пока загрузятся аудиоданные. На это время может подвиснуть страница. Во избежание этого можно задействовать worker.
Живая ссылка


text:
Q: Python - Handling broken URLs that are being passed into a redirect handler? I have a text file with a couple thousand URLs in which I am trying to get all of the redirected URLs from.  So far I have achieved the ability to obtain the redirected URLs as long as the original URL does not redirect to a dead end or broken link with:
def get_redirect_url(url):
    opener = urllib2.build_opener(urllib2.HTTPRedirectHandler)
    request = opener.open(url)
    return request.url
with open ('textFile.txt', 'r') as myFile:
    urls = [line.strip() for line in myFile]
    redirect_urls = [get_redirect_url(url) for url in urls]
    print redirect_urls

However, occasionally the program runs into a "page not found" or a completely blank page which causes the program to fail.  Is there a solution to handling these broken links by either erasing or simply bypassing them when approached by the program?  Cheers all. Thanks in advance.

A: Modify your function to handle exceptions:
def get_redirect_url(url):
    opener = urllib2.build_opener(urllib2.HTTPRedirectHandler)
    try:
        request = opener.open(url)
        return request.url
    except urllib2.HTTPError as e:
        pass # or do something

You'll get a None returned using the above where an exception occurs - so you may wish to filter those out, or otherwise ignore them in your list comp.


text:
Q: Servlet, Jsp and Hibernate. How to print a query result in a jsp page I've a Java Servlet which takes a list of messages from a database using hibernate.
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
    SessionFactory factory = session.getSessionFactory();
    Session s = factory.openSession();
    List<Message> messages = s.createQuery("FROM Message").list();
    //print this list in home.jsp
}

How can I send this messages to home.jsp?

A: SERVLET:
 protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
        SessionFactory factory = session.getSessionFactory();
        Session s = factory.openSession();
        List<Message> messages = s.createQuery("FROM Message").list();

        //associate with a request attribute
        request.setAttribute("messages", message);

        //forward to your JSP
        request.getRequestDispatcher("messages.jsp").forward(request, response);
    }

JSP
<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>

<html>
   <body>
      <%-- will iterate the messages collection put in
           request scope in the servlet --%>
      <c:forEach items="${messages}" var="message">
         Message = ${message.someProperty"}
      </c:forEach>
   </body>
</html>

Useful references:
https://www.tutorialspoint.com/jsp/jstl_core_foreach_tag.htm
https://www.tutorialspoint.com/jsp/jsp_expression_language.htm


text:
Q: Will my Android App with Facebook SDK v4.3 work on versions lower than 4.4 Kitkat? So the new Facebook SDK 4.3 is out, I am doing the ceremony to update my app.
To get rid of all the errors that pop up, I reach a point where I must step up the JDK version of my app from 1.6 to 1.7.
This also means that I must also step up my minumum Android API from 9 to Kitkat (19).
I need to support at least, 4.0 - 4.3 users too.
Has somebody been able to make facebook SDK 4.3v work with Android versions older than 4.4 Kitkat(19)???

A: I did not want to give up my pre-KitKat users, so I found a way to make the same old SDK compliant with facebook´s new API. No need to install a new SDK, and no need to re-implement Facebook login and authentication inside my app (Session was replaced by 3 more objects, and other changes I dont have time to change.)
So the recipe is simple, just open:
src > com.facebook.android > Facebook.java

..and change this line of code (aprox. line #87):
protected static String GRAPH_BASE_URL = "https://graph.facebook.com/";

to this:
protected static String GRAPH_BASE_URL = "https://graph.facebook.com/v2.0/";

Voila!!
Maybe later, Facebook totally deprecates my entire Facebook SDK 3.19.1, but for today, the day has been saved.
Source:
https://developers.facebook.com/docs/apps/upgrading#v1tov2