text:
Q: How to set aws-sdk credentials using Elastic Beanstalk? I'm running an express app in Elastic Beanstalk and in one route I'm using the aws-sdk to publish a notification to sns.
This works when running locally, but in the Elastic Beanstalk environment how would/could I set up the credentials 'myprofile'?
router.post('/publish', async (req, res) => {
  var AWS = require('aws-sdk')
  AWS.config.update({region: 'us-east-2'})
  // myprofile exists locally, but how do I deal with this in the elastic beanstalk environment?
  var credentials = new AWS.SharedIniFileCredentials({profile: 'myprofile'})
  AWS.config.credentials = credentials
  //...more stuff
})

A: You can use IAM instance profile to provide permissions to your ec2 instance, so when your application loads the SDK, the credentials passed will be automatically loaded.
Check this link https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/concepts-roles.html - what you will need is the instance profile. In order to create a instance profile go to the IAM console > Roles, then choose the service EC2 as a service that can assume this role. Then attach the policies that your application needs calling (SNS stuff).
On the beanstalk settings, under security, you will be able to set the IAM instance profile that you just created - so the instances on this environment should have the role associated with it.
Your code should look like then:
router.post('/publish', async (req, res) => {
  var AWS = require('aws-sdk')
  AWS.config.update({region: 'us-east-2'})

  //...more stuff
})

Also check if you can require and set the region outside of the controller ;)


text:
Q: Supersymmetry invariants On page 158 of Fields, the supersymmetry algebra is represented in terms of the action on supercoordinates as
$$\delta \theta^\alpha = \epsilon^\alpha$$
$$\delta\bar{\theta}^{\dot{\alpha}} = \bar{\epsilon}^{\dot{\alpha}}$$
$$\delta x^{\alpha\dot{\beta}} = \frac{1}{2}i(\epsilon^\alpha \bar{\theta}^{\dot{\beta}} + \bar{\epsilon}^{\dot{\beta}}\theta^\alpha)$$
Further down on the page, the following statement is made:

In the supersymmetric case the infinitesimal invariants under the q's (and therefore p) are
$$d\theta^\alpha, \qquad d\bar{\theta}^{\dot{\alpha}}, \qquad dx^{\alpha\dot{\beta}} + \frac{1}{2}i(d\theta^\alpha)\bar{\theta}^{\dot{\beta}} + \frac{1}{2}i(d\bar{\theta}^{\dot{\beta}})\theta^\alpha$$

I get a sense that these should follow from the definition of the supersymmetry transformations, but what exactly does the quoted statement mean?
Am I correct in inferring that $d\theta^\alpha = \theta'^\alpha - \theta^\alpha$ where $\theta'^\alpha$ is $\theta^\alpha$ + SUSY variation of $\theta^\alpha$ by an infinitesimal SUSY parameter (more precisely by an infinitesimal scalar times the supersymmetry parameter $\epsilon$)? Thinking of the SUSY variation $\delta_\epsilon$ as something similar to a BRST variation, is this statement about nilpotency of the algebra in form notation or something?

A: No, you are not corrected in your interpretation of the statement. The statement is that theses differentials, or $1$-forms, are supersymetric in the sense that:
$d(\theta^{\alpha}+\delta_{\epsilon}\theta)=d(\theta^{\alpha}+\epsilon^{\alpha})=0$
and the same for the other differentials. These $d$ is not the same as $\delta_{\epsilon}$. It is just a exterior derivative acting on "smooth functions".
No, the supersymmetry is not a BRST symmetry. The only thing in common between these two is the fact that they are both fermionic symmetries, just that. The supersymmetry is not nilpotent, quite the contrary, it obeys the following algebra:
$$
\{Q_\alpha,Q_{\dot\beta}\}\propto P_{\alpha\dot\beta}
$$
as you can check using the formulas above and the fact that $\delta_{\epsilon}=\epsilon^{\alpha}Q_\alpha+\bar{\epsilon}^{\dot\beta}Q_{\dot\beta}$, and $P_{\alpha\dot\beta}$ being the generator of translations of $x_{\alpha\dot\beta}\rightarrow x_{\alpha\dot\beta}+\Lambda_{m}\sigma^{m}_{\alpha\dot\beta}$.

redpajama_set_name: RedPajamaStackExchange


text:
Q: Find the index of maximum value in numpy exclude zero values I have the following numpy:
e = np.array([0, -1.3, 0, -3.9, 0, -0.9, 0])

I want to find the index of the maximum value in the list, but exclude 0.
The following code example returns first element in the list, because its value is 0.
result = np.argmax(e)
print(result)

But I want not to consider zeros in locating the maximum value:
The expected result should be 5 which is the index of the element with -0.9 value.
I do not want to write code that will run on the list iteratively.
Any ideas?

A: One possible approach would be:
import numpy as np

e = np.array([0, -1.3, 0, -3.9, 0, -0.9, 0])

result = np.nanargmax(np.where(e != 0, e, np.nan))
print(result)

Output
5

Note that this does not change the input array (in this case e).

A: One simple solution would be to replace 0 with -np.inf
a[a == 0] = -np.inf
np.argmax(a)
>>> 5


text:
Q: After IF in foreach remove item from array! php In my foreach loop I want the item that comes in the IF not te be outputted anymore after the if statement. It keeps coming back... How can I fix this?
$content1 = "";

foreach($array as $key => $value) {
    if(!empty($brandnid)) {
        $content1 .= ',"' . $key . '":{"und":[{"nid":"[nid:' . $value . ']"}]}';
        unset($array[array_search('field_brandid',$array)]);
    }
    $content1 .= ',"' . $key . '":{"und":[{"value":"' . $value . '"}]}';
}

if(!empty($title) && !empty($day) && !empty($month) && !empty($year)) {
    $postContent = '{"type":"carmodels","title":"' . $title . '"' . $content1 . ',"field_modelyear":{"und":[{"value":{"date":"' . $dateBuild . '"}}]}}';
}elseif(!empty($day) && !empty($month) && !empty($year)) {
    $postContent = '{"type":"carmodels","field_modelyear":{"und":[{"value":{"date":"' . $dateBuild . '"}}]}' . $content1 . '}';
}else {
    $postContent = '{"type":"carmodels"' . $content1 . '}';
}

In above case I entered fields color (normal field) and the brandid and it displays the following in $postContent:
{"type":"carmodels","field_modelcolor":{"und":[{"nid":"[nid:black]"}]},"field_modelcolor":{"und":[{"value":"black"}]},"field_brandid":{"und":[{"nid":"[nid:24]"}]},"field_brandid":{"und":[{"value":"24"}]}}

But it should only do the brandid in the if statement (once) and after that the normal field which doesnt reach the IF also once


text:
Q: Resize WebGL context in iOS iOS WebGL has a bug, which can be reproduced with following HTML:

    <title>Resize WebGL</title>
    <meta name="viewport" content="width=device-width, user-scalable=no">

<input type="button" id="resizeButton" value="resize">

<canvas id="chart" style="width:100px; height:100px; border:1px solid #000;">

</canvas>

Press "resize", and iOS stomps on this text!

I have uploaded this HTML to http://adam1234.s3-website-us-east-1.amazonaws.com/bug.html so you can run it easily.
For people who don't have an iOS device, I have uploaded screenshots of my iPad before and after pressing the button, which show the bug in iOS.
This code works on desktop browsers and Android, but in iOS the resized WebGL window goes outside the Canvas and stomps on stuff outside the canvas.  This is a bug in iOS.  The bug goes away if devicePixelRatio is set to 1, but I want to use the higher resolution of retina screens.
How can I resize a high resolution WebGL canvas in iOS?

A: I found a work-around.  The app should put the WebGL <canvas> element inside a
, and recreate the <canvas> element whenever the
 size changes.   Also, the <canvas> should have no border to avoid another iOS bug in placing the GL window inside the canvas with thick border in retina displays.   If you want a border, then just put the border on the
 element.


text:
Q: Trimming text strings in SQL Server 2008 I have a table in a SQL Server 2008 database. This table has a nvarchar(256) column called 'Name'. Unfortunately, the values in this field have extra spaces included. For instance the name 'Bill' is actually stored as 'Bill                       ' in the table.
I want to update all of the records in this table to remove the extra spaces. However, I was surprised to learn that SQL does not have a TRIM function.
How do I update all of the records at once to remove the extra spaces?
Thank you!

A: SQL Server does not have a TRIM function, but rather it has two.  One each for specifically trimming spaces from the "front" of a string (LTRIM) and one for trimming spaces from the "end" of a string (RTRIM).
Something like the following will update every record in your table, trimming all extraneous space (either at the front or the end) of a varchar/nvarchar field:
UPDATE
   [YourTableName]
SET
   [YourFieldName] = LTRIM(RTRIM([YourFieldName]))

(Strangely, SSIS (Sql Server Integration Services) does have a single TRIM function!)

A: I would try something like this for a Trim function that takes into account all white-space characters defined by the Unicode Standard (LTRIM and RTRIM do not even trim new-line characters!):

IF OBJECT_ID(N'dbo.IsWhiteSpace', N'FN') IS NOT NULL
    DROP FUNCTION dbo.IsWhiteSpace;
GO

-- Determines whether a single character is white-space or not (according to the UNICODE standard).
CREATE FUNCTION dbo.IsWhiteSpace(@c NCHAR(1)) RETURNS BIT
BEGIN
 IF (@c IS NULL) RETURN NULL;
 DECLARE @WHITESPACE NCHAR(31);
 SELECT @WHITESPACE = ' ' + NCHAR(13) + NCHAR(10) + NCHAR(9) + NCHAR(11) + NCHAR(12) + NCHAR(133) + NCHAR(160) + NCHAR(5760) + NCHAR(8192) + NCHAR(8193) + NCHAR(8194) + NCHAR(8195) + NCHAR(8196) + NCHAR(8197) + NCHAR(8198) + NCHAR(8199) + NCHAR(8200) + NCHAR(8201) + NCHAR(8202) + NCHAR(8232) + NCHAR(8233) + NCHAR(8239) + NCHAR(8287) + NCHAR(12288) + NCHAR(6158) + NCHAR(8203) + NCHAR(8204) + NCHAR(8205) + NCHAR(8288) + NCHAR(65279);
 IF (CHARINDEX(@c, @WHITESPACE) = 0) RETURN 0;
 RETURN 1;
END
GO

IF OBJECT_ID(N'dbo.Trim', N'FN') IS NOT NULL
    DROP FUNCTION dbo.Trim;
GO

-- Removes all leading and tailing white-space characters. NULL is converted to an empty string.
CREATE FUNCTION dbo.Trim(@TEXT NVARCHAR(MAX)) RETURNS NVARCHAR(MAX)
BEGIN
 -- Check tiny strings (NULL, 0 or 1 chars)
 IF @TEXT IS NULL RETURN N'';
 DECLARE @TEXTLENGTH INT = LEN(@TEXT);
 IF @TEXTLENGTH < 2 BEGIN
  IF (@TEXTLENGTH = 0) RETURN @TEXT;
  IF (dbo.IsWhiteSpace(SUBSTRING(@TEXT, 1, 1)) = 1) RETURN '';
  RETURN @TEXT;
 END
 -- Check whether we have to LTRIM/RTRIM
 DECLARE @SKIPSTART INT;
 SELECT @SKIPSTART = dbo.IsWhiteSpace(SUBSTRING(@TEXT, 1, 1));
 DECLARE @SKIPEND INT;
 SELECT @SKIPEND = dbo.IsWhiteSpace(SUBSTRING(@TEXT, @TEXTLENGTH, 1));
 DECLARE @INDEX INT;
 IF (@SKIPSTART = 1) BEGIN
  IF (@SKIPEND = 1) BEGIN
   -- FULLTRIM
   -- Determine start white-space length
   SELECT @INDEX = 2;
   WHILE (@INDEX < @TEXTLENGTH) BEGIN -- Hint: The last character is already checked
    -- Stop loop if no white-space
    IF (dbo.IsWhiteSpace(SUBSTRING(@TEXT, @INDEX, 1)) = 0) BREAK;
    -- Otherwise assign index as @SKIPSTART
    SELECT @SKIPSTART = @INDEX;
    -- Increase character index
    SELECT @INDEX = (@INDEX + 1);
   END
   -- Return '' if the whole string is white-space
   IF (@SKIPSTART = (@TEXTLENGTH - 1)) RETURN '';
   -- Determine end white-space length
   SELECT @INDEX = (@TEXTLENGTH - 1);
   WHILE (@INDEX > 1) BEGIN
    -- Stop loop if no white-space
    IF (dbo.IsWhiteSpace(SUBSTRING(@TEXT, @INDEX, 1)) = 0) BREAK;
    -- Otherwise increase @SKIPEND
    SELECT @SKIPEND = (@SKIPEND + 1);
    -- Decrease character index
    SELECT @INDEX = (@INDEX - 1);
   END
   -- Return trimmed string
   RETURN SUBSTRING(@TEXT, @SKIPSTART + 1, @TEXTLENGTH - @SKIPSTART - @SKIPEND);
  END
  -- LTRIM
  -- Determine start white-space length
  SELECT @INDEX = 2;
  WHILE (@INDEX < @TEXTLENGTH) BEGIN -- Hint: The last character is already checked
   -- Stop loop if no white-space
   IF (dbo.IsWhiteSpace(SUBSTRING(@TEXT, @INDEX, 1)) = 0) BREAK;
   -- Otherwise assign index as @SKIPSTART
   SELECT @SKIPSTART = @INDEX;
   -- Increase character index
   SELECT @INDEX = (@INDEX + 1);
  END
  -- Return trimmed string
  RETURN SUBSTRING(@TEXT, @SKIPSTART + 1, @TEXTLENGTH - @SKIPSTART);
 END ELSE BEGIN
  -- RTRIM
  IF (@SKIPEND = 1) BEGIN
   -- Determine end white-space length
   SELECT @INDEX = (@TEXTLENGTH - 1);
   WHILE (@INDEX > 1) BEGIN
    -- Stop loop if no white-space
    IF (dbo.IsWhiteSpace(SUBSTRING(@TEXT, @INDEX, 1)) = 0) BREAK;
    -- Otherwise increase @SKIPEND
    SELECT @SKIPEND = (@SKIPEND + 1);
    -- Decrease character index
    SELECT @INDEX = (@INDEX - 1);
   END
   -- Return trimmed string
   RETURN SUBSTRING(@TEXT, 1, @TEXTLENGTH - @SKIPEND);
  END
 END
 -- NO TRIM
 RETURN @TEXT;
END
GO

A: You can use the RTrim function to trim all whitespace from the right. Use LTrim to trim all whitespace from the left. For example
UPDATE Table SET Name = RTrim(Name)

Or for both left and right trim
UPDATE Table SET Name = LTrim(RTrim(Name))

A: You do have an RTRIM and an LTRIM function. You can combine them to get the trim function you want.
UPDATE Table
SET Name = RTRIM(LTRIM(Name))

A: I know this is an old question but I just found a solution which creates a user defined function using LTRIM and RTRIM. It does not handle double spaces in the middle of a string.
The solution is however straight forward:
User Defined Trim Function

A: No  Answer is true
The true Answer is Edit Column to NVARCHAR and you found Automatically trim Execute but this code
UPDATE Table
SET Name = RTRIM(LTRIM(Name))
use it only  with Nvarchar if use it with CHAR or NCHAR it will not work

A: This function trims a string from left and right. Also it removes carriage returns from the string, an action which is not done by the LTRIM and RTRIM
IF OBJECT_ID(N'dbo.TRIM', N'FN') IS NOT NULL
    DROP FUNCTION dbo.TRIM;
GO
CREATE FUNCTION dbo.TRIM (@STR NVARCHAR(MAX)) RETURNS NVARCHAR(MAX)
BEGIN
    RETURN(LTRIM(RTRIM(REPLACE(REPLACE(@STR ,CHAR(10),''),CHAR(13),''))))
END;
GO


text:
Q: React not displaying images after deploy I created a react app and it looked good , until I deployed it. After deployment, i cannot see the images, even if react loads them (i see that in the networking tab on chrome). Also the Content type was set right. Also when i go directly to the miage links, chrome renderes them.
This is the component responsable for showing the images:
import "./../styles/firstPart.css";
import logo from "./../images/croped_down.png";
import background from "./../images/background.jpg";
import PreloadImage from "react-preload-image";
import Navbar from "./../components/navbar";

const FirstPart = () => {
  return (

      <PreloadImage className="background" src={background} />
      <PreloadImage className="logo" src={logo} />
      <Navbar />

  );
};

export default FirstPart;

A: Image path is relative and works for local. The same won't be working in prod as it takes absolute page. Check the path of image in Developer Console and see if the path is same as existed


text:
Q: How do you combine 2 columns to one in R with data duplicated for each column? I have some data which has features which are of the same "class" (or type).
x <- tibble(char = c("a", "b", "c", "d"), x1 = c(1:4), x2 = c(11:14))
x
# A tibble: 4 x 3
   char    x1    x2
  <chr> <int> <int>
1     a     1    11
2     b     2    12
3     c     3    13
4     d     4    14

Now, I what I am looking for is a function to collapse x1 and x2 into a single column x.
y <- some_transformation_function(x)
y
# A tibble: 8 x 2
   char     x
  <chr> <int>
1     a     1
2     b     2
3     c     3
4     d     4
5     a    11
6     b    12
7     c    13
8     d    14

I am not looking for a function that modifies the data in anyway (which is what existing questions seem to answer - I haven't found a similar question to what I'm asking).


text:
Q: Linear transformation with range of a vector enter image description here
What are the steps or any guidance in solving this problem?
I'm very confused

A: The matrix $A$ is a 3x5 matrix. We are given that $T(v) = Av $ where $T: \mathbb{R}^5 \to \mathbb{R}^3$. The given vector $(3,2,1)^T$ being in $\mathbb{R}^3$ means that it is in the range of $T$. To find all vectors that are mapped to that vector, let $v= (v_1,v_2,v_3,v_4,v_5) $ and solve
$$ Av = (3,2,1)^T $$


text:
Q: Struggling with extracting values from XML data (T-SQL) Here is a snippet of my XML data from the bottom of the file showing the end tags (it's a huge file):
<EncounterValidationResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.dhcs.ca.gov/EDS/DHCSResponse" ResponseVersion="1.2">
  <EncounterFileName>EncounterFileName</EncounterFileName>
  <EncounterSubmitterName>EncounterSubmitter</EncounterSubmitterName>
  <EncounterSubmissionDate>2019-12-13T08:47:00.0000000-08:00</EncounterSubmissionDate>
  <ValidationStatus>Accepted</ValidationStatus>
  <Transactions>
    <Transaction>
      <IdentifierName>BatchNumber</IdentifierName>
      <TransactionNumber>1277104</TransactionNumber>
      <Identifiers>
        <Envelope IdentifierName="OriginatorTransactionId" IdentifierValue="1277104-00" />
      </Identifiers>
      <Encounters>
        <Encounter Status="Accepted">
          <IdentifierType>TransactionIdentifier</IdentifierType>
          <EncounterReferenceNumber>8129339999264</EncounterReferenceNumber>
          <EncounterId>1934049991539</EncounterId>
          <EncounterResponses>
            <Response Severity="Warning">
              <Id>0x0006D</Id>
              <Description>MEDS indicates multiple records exist for this beneficiary</Description>
            </Response>
            <Response Severity="Warning">
              <Id>0x00221</Id>
              <Description>Prescriber Taxonomy (296)  is either not provided or is invalid</Description>
            </Response>
            <Response Severity="Warning">
              <Id>0x00249</Id>
              <Description>Reason for Service Code (439-E4) is requested per the payer sheet but no value is provided/specified.</Description>
            </Response>
            <Response Severity="Warning">
              <Id>0x00249</Id>
              <Description>Professional Service Code (440-E5) is requested per the payer sheet but no value is provided/specified.</Description>
            </Response>
            <Response Severity="Warning">
              <Id>0x00249</Id>
              <Description>Result of Service Code (441-E6) is requested per the payer sheet but no value is provided/specified.</Description>
            </Response>
          </EncounterResponses>
        </Encounter>
      </Encounters>
    </Transaction>
  </Transactions>
</EncounterValidationResponse>

I'm trying to extract all Encounter Status values, EncounterReferenceNumbers, Response Severity values, Id and Description values. I would like the query to return five columns, Encounter Status, Encounter Reference Number, Severity, Id, and Description.
I've been searching the internet for days and have tried several things but can't seem to get it.
Any help is appreciated!

A: Something like this demo:
declare @x XML='<EncounterValidationResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.dhcs.ca.gov/EDS/DHCSResponse" ResponseVersion="1.2">
  <EncounterFileName>EncounterFileName</EncounterFileName>
  <EncounterSubmitterName>EncounterSubmitter</EncounterSubmitterName>
  <EncounterSubmissionDate>2019-12-13T08:47:00.0000000-08:00</EncounterSubmissionDate>
  <ValidationStatus>Accepted</ValidationStatus>
  <Transactions>
    <Transaction>
      <IdentifierName>BatchNumber</IdentifierName>
      <TransactionNumber>1277104</TransactionNumber>
      <Identifiers>
        <Envelope IdentifierName="OriginatorTransactionId" IdentifierValue="1277104-00" />
      </Identifiers>
      <Encounters>
        <Encounter Status="Accepted">
          <IdentifierType>TransactionIdentifier</IdentifierType>
          <EncounterReferenceNumber>8129339999264</EncounterReferenceNumber>
          <EncounterId>1934049991539</EncounterId>
          <EncounterResponses>
            <Response Severity="Warning">
              <Id>0x0006D</Id>
              <Description>MEDS indicates multiple records exist for this beneficiary</Description>
            </Response>
            <Response Severity="Warning">
              <Id>0x00221</Id>
              <Description>Prescriber Taxonomy (296)  is either not provided or is invalid</Description>
            </Response>
            <Response Severity="Warning">
              <Id>0x00249</Id>
              <Description>Reason for Service Code (439-E4) is requested per the payer sheet but no value is provided/specified.</Description>
            </Response>
            <Response Severity="Warning">
              <Id>0x00249</Id>
              <Description>Professional Service Code (440-E5) is requested per the payer sheet but no value is provided/specified.</Description>
            </Response>
            <Response Severity="Warning">
              <Id>0x00249</Id>
              <Description>Result of Service Code (441-E6) is requested per the payer sheet but no value is provided/specified.</Description>
            </Response>
          </EncounterResponses>
        </Encounter>
      </Encounters>
    </Transaction>
  </Transactions>
</EncounterValidationResponse>';

with xmlnamespaces (DEFAULT 'http://www.dhcs.ca.gov/EDS/DHCSResponse')
select  t.n.value('@Status','nvarchar(100)') status,
     t.n.value('EncounterReferenceNumber[1]','nvarchar(100)') EncounterReferenceNumber,
     r.n.value('@Severity','nvarchar(100)') Severity,
     r.n.value('Id[1]','nvarchar(100)') Id,
     r.n.value('Description[1]','nvarchar(100)') Description
from @x.nodes('/EncounterValidationResponse/Transactions/Transaction/Encounters/Encounter') t(n)
cross apply t.n.nodes('EncounterResponses/Response') r(n);


text:
Q: Sqlite Out of Memory when preparing update statement I have one problem with my application.
I create a one AsyncTask for downloading list of files from server . When all the files are download after that i update the database. But when i called the update query its give me the below error.

Failure 21 (out of memory) on 0x0 when
  preparing update

Can any one tell me why this error occurs ?
Sample Code
public void setStatus(int index)
{
    try
    {
        db.OpenDatabase();
        db.updateStatus(id.get(index), 1);
        db.closeDatabase();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

Above function called from the AsyncTask ....
public void updateStatus(int id,int status)
{
    try
    {
        db.execSQL("update sample set status =" + status + " where id = " + id);
    }
    catch(Exception e){e.printStackTrace();}
}

A: This may not be related to the database pe se, but rather to the fact that the memory (heap) is almost full and opening the database completely fills it up.
Remember that most handsets have 48MB of heap or even less.

A: Sometime while working I also got the same error.
I used this link
"Failure 21 (out of memory)" when performing some SQLite operations
It said that this error occurs when you try to work on a closed DB.
I looked back into my code and found that I was also doing the same. Got it working afterwards
I think you are also trying to work on a closed DB.

A: Have you tried to use the update() method instead of execSQL()?
public void updateStatus(int id,int status)
{
    try
    {
         ContentValues values = new ContentValues();
         values.put("status", status);
         db.update ("sample", values, "id = ?", new String[]{Integer.toString(id)});
    }
    catch(Exception e){e.printStackTrace();}
}

A: I has "out of memory" error (21) when I try to call sqlite3_prepare() with a NULL pointer to database handle.
Check if your handle is valid and the database is opened.


text:
Q: Different XML values for SmartPhone vs Tablet I am trying to have different XML values for imageview height's and imageview width's for my app. The problem is that i Need different values for the height's and width's for the smartphone version and the tablet version. Would there a way to do this? Thanks in advance.

A: You can use Resource Qualifiers in Android Check Here
Define the image width & height in dimens file
values/dimens.xml
<resources>
    <dimen name="image_width">100dp</dimen>
</resources>

values-sw600dp/dimens.xml
<resources>
    <dimen name="image_width">150dp</dimen>
</resources>

In the activity/fragment xml
<ImageView
     android:width="@dimen/image_width"
     android:height="@dimen/image_height" />


text:
Q: Resetting column based on criteria Trying to reset Batting Order so when inning = 0, batting order restarts at one, need to do this because my original formula did not account for opponent being the same
Here is my formula so far...
    df['Batting Order'] = df.groupby('opponent'['pa'].cumsum().mod(9).apply(lambda x: 9 if x == 0 else x)
Input
      pa  Batting Order  inning opponent
2666   1              8      15   Toledo
2667   1              9      16   Toledo
2668   1              1      17   Toledo
2669   0              1      17   Toledo
2670   1              2      17   Toledo
2671   1              3      18   Toledo
2672   1              4      19   Toledo
2673   1              5      19   Toledo
2674   1              6      20   Toledo
2675   1              7       0   Toledo
2676   1              8       1   Toledo
2677   1              9       1   Toledo
2678   0              9       1   Toledo
2679   1              1       1   Toledo
2680   1              2       1   Toledo
2681   0              2       1   Toledo
2682   0              2       1   Toledo
2683   0              2       1   Toledo
2684   1              3       1   Toledo
2685   1              4       2   Toledo
2686   1              5       3   Toledo
2687   1              6       4   Toledo
2688   1              7       5   Toledo
2689   1              8       6   Toledo

This is the output that I want:
Output
      pa  Batting Order  inning opponent
2666   1              8      15   Toledo
2667   1              9      16   Toledo
2668   1              1      17   Toledo
2669   0              1      17   Toledo
2670   1              2      17   Toledo
2671   1              3      18   Toledo
2672   1              4      19   Toledo
2673   1              5      19   Toledo
2674   1              6      20   Toledo
2675   1              1       0   Toledo
2676   1              2       1   Toledo
2677   1              3       1   Toledo
2678   0              3       1   Toledo
2679   1              4       1   Toledo
2680   1              5       1   Toledo
2681   0              5       1   Toledo
2682   0              5       1   Toledo
2683   0              5       1   Toledo
2684   1              6       1   Toledo
2685   1              7       2   Toledo
2686   1              8       3   Toledo
2687   1              9       4   Toledo
2688   1              1       5   Toledo
2689   1              2       6   Toledo


text:
Q: How to prevent users from leaving the site with the an exit popup? JS I want to be able to have a copy of my website to reappear every time the user tries to leave the site. Is there such a method/event handler?
No need to mention the bad UI, I just want to explore more with event handlers

A: You could use the onbeforeunload event.
window.onbeforeunload = function() {
   return 'Are you sure that you want to leave this page?';
};

A: There is no way to prevent a user from leaving your website, let alone spawn a new tab upon them exiting.
Here is a similar question that discusses why this is a bad idea and why most modern browsers have since removed this ability:

"How to block users from closing a window in Javascript?"

The example below is the purely theoretical:
<!html>

    <title>Persistent Page...</title>

Persistent Page...


text:
Q: Unable to allocate array with shape (35126, 224, 224, 3) and data type float32 I'm making a Diabetic Retinopathy Detection by using machine learning in python.
I have a 35126 image in training data an I want to let them into array but I have a problem to allocate array with shape (35126, 224, 224, 3) it show
MemoryError: Unable to allocate array with shape (35126, 224, 224, 3) and data type float32
I have a 16GB Ram
This is my code using keras
import os, sys
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import cv2
from sklearn.svm import SVC
from PIL import Image
from sklearn.model_selection import train_test_split
from sklearn import preprocessing
import glob
import pywt
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.utils import to_categorical
from keras.preprocessing import image
from keras.utils import to_categorical
from tqdm import tqdm
from sklearn.neighbors import KNeighborsClassifier
from skimage import exposure
from sklearn.model_selection import KFold
from sklearn.ensemble import RandomForestClassifier

import warnings
warnings.filterwarnings("ignore")
train_image = []
df_train = pd.read_csv('trainlabel/trainLabels.csv')
y = df_train['level']
score_svm = []
score_rf = []

def getscore(model,x_train, x_test, y_train, y_test):
    model.fit(x_train,y_train)
    return model.score(x_test,y_test)
#tqdm(range(df_train.shape[0]))

fig = plt.figure(figsize=(25, 16))
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
for i in tqdm(range(df_train.shape[0])) :
    img = image.load_img('train/'+'trainimage/'+'train/'+df_train['image'][i]+'.jpeg',target_size=(256,256,3))
    img = image.img_to_array(img)
    img = img / 255
    print(type(img))
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    img = exposure.equalize_hist(img)
    img_adpeq = exposure.equalize_adapthist(img,clip_limit=0.03)
    print(type(img_adpeq))
    train_image.append(img_adpeq)
    print(type(train_image))

X = np.asarray(train_image)
print(type(X))
nsamples, nx, ny = X.shape
X2 =X.reshape((nsamples,nx*ny))
print(X2.shape)
kf = KFold(n_splits=5)
for train_index, test_index in kf.split(X):
    print("TRAIN:", train_index, "TEST:", test_index)
    X_train, X_test = X[train_index],X[test_index]
    y_train, y_test = y[train_index], y[test_index]
    nsamples, nx, ny = X_train.shape
    X_train = X_train.reshape((nsamples,nx*ny))
    nsamples, nx, ny = X_test.shape
    X_test = X_test.reshape((nsamples, nx * ny))
    score_svm.append(getscore(SVC(),X_train,X_test,y_train,y_test))
    score_rf.append(getscore(RandomForestClassifier(),X_train,X_test,y_train,y_test))

print("SVM")
print(score_svm)
print("Random Forest")
print(score_rf)

MemoryError: Unable to allocate array with shape (35126, 254, 256, 3) and data type float32

it's my first time ,so it seem like a dirty code sorry.


text:
Q: Incremental Data Storage from MongoDB to Amazon S3 in Parquet Format I have a database in MongoDB. That is updating every minute from various user PCs. Now I want to store the data into Amazon S3 bucket (preferable in Parquet, otherwise CSV). But I do not want to store the full MongoDB data into S3 every-time. I only want to save the incremental data in S3.
I was thinking to use Kafka in between MongoDB and S3. But there are two issues in that:
Issue 1: I do not know how to store the incremental data from MongoDB to S3 in Parquet/CSV format without any paid solution
Issue 2: I do not know, whether this is a good/practical solution
Can anyone suggest any solution to achieve this kind of job please?

A: Parquet will be very advantageous when you save huge data, say 10k rows. When you say incremental records, I am thinking you will be saving every minute and at max 1-4k records you may get. Saving as parquet will not be helpful here. Instead

*

*use JSON --advantage being you don't have to worry about special chars/ encoding, column placements, nested columns etc. Gson parser will take care of all of them. Meaning read from Mongo cdc and write to JSON end of every 1minute (writing at end of minute will make sure that you have 1 fat file instead of 60 continuous files). Understand that S3 bills you by the number of file reads you do. Meaning, if you store as 60 files and read 60 files --it will be more costly than reading 1 single fat file

*have a snapshot in Parquet. keep merging the Jsons to the parquet files using some Spark job.
You may alternatively consider delta bricks -- I myself have not used it. But the advantage is that you can keep writing to the data store in delta format and this delta bricks will take care of merging the data periodically (using vacuum command) -- and makes sure you get the latest Parquet always.

A:
store the incremental data from MongoDB (in Kafka)

Use Debezium or Mongo CDC connector

do not want to store the full MongoDB

You can disable initial Debezium snapshot, but then any update/delete after starting the connector wouldn't be processed correctly.

to S3 in Parquet/CSV format

Mongo documents typically aren't tables, so do not use CSV; Parquet handles nested data much better.
You can use Confluent S3 sink connector for this.

without any paid solution

All the above are free / open source tools. S3 costs money unless you use an alternative solution such as MinIO. BUT, you're still paying for servers for running all these tools, and it'll probably be more costly to recover / maintain than using AWS S3 directly.


text:
Q: How to interpret NRMSE (Normalised Root Mean Square Error) without comparing models? I have fitted some robust mixed effects linear regression models (using robustlmm::rlmer in R).
I have calculated the normalised root mean square error (NRMSE) for these models but I want to make sure that I am interpreting this correctly.
The NRMSE for one model is 0.13 and for the other is 0.85 (the outcome variables are not in comparable units and I'm not comparing them to each other, I just want to know how well they fit the data).
Am I correct in thinking that an NRMSE of 0 means a perfect fit and an NRMSE of 1 means fit equivalent to a random model? So the model with NRMSE of 0.13 has a good fit and the one with an NRMSE of 0.85 has a pretty bad fit but still slightly better than random? Are there any rules of thumb about what kinds of NRMSE values are considered good/acceptable/poor?
(Sorry if this already has an answer somewhere; I did search the existing questions as it felt like it would have been asked before, but I couldn't find anything relevant!)


text:
Q: What household electrical devices could serve as an appropriate Master for the CE Tech Energy Saving Surge Protector? I am attempting to use the Energy Saving feature of this surge protector: http://www.homedepot.com/p/CE-TECH-4-ft-8-Outlet-Energy-Saving-Surge-Protector-HDC804GWH/205893722
Using my laptop as the Master, my issue is that while the laptop is on, the controlled devices switch on and off sporadically. They remain off as expected while the laptop is off.
The sporadic switching can be triggered by user interaction with the laptop (opening an application, etc.), or can occur for no apparent reason. Presumably, the laptop is communicating a variable level of electricity to the surge protector, causing it to malfunction.
My goal is to use the Master to cut power from my devices which otherwise emit charging lights, etc., to maintain a darker room at night, but the Master doesn't have to be the laptop. What commonly owned device(s) would better communicate a steady, optimal electricity level to the surge protector?

A: As @RickBrant said - the issue you are having is that your laptop is too efficient, and the power board is unable to reliably to detect if its on by its power draw.
It would defeat the power-saving purpose of the device, but you could put a double-adapter onto the master socket and have something like a (non LED or high powered LED) lamp on the same outlet as the Laptop so that its power draw is great enough to switch the other devices on.
You might (or might not) also be able to get away with setting your laptops power profile to maximum performance, screen on maximum when connected to power ( and add a challenging screensaver so the CPU is working in the background to render it ) to ensure a more consistent/higher power draw when your laptop is powered by mains.  (Also, if you have a spinning disk as opposed to an SSD, see if you can keep it from spinning down when plugged in)


text:
Q: \# not supplying command number in bash I am trying to print the number of commands in bash using \#. I have printf 'This is command number' and then when I put \# it just prints 'This is command number'. How do I use this prompt to print the command number?

A: \# only represents the current command number in the context of prompt expansion. Since bash 4.4, though, you can use the @ operator to expand prompt escapes in an arbitrary parameter expansion.
$ x='Command number \#'
$ echo "$x"
Command number \#
$ echo "${x@P}"
Command number 4


text:
Q: Do I need to cleanup files created by tempnam() myself? I created a bunch of tempnam() files for use during my request. Do I need to specifically unlink() them when they're not needed anymore, or will PHP / the system take care of it themselves?

A: Files created by tempnam() will not be deleted by PHP.
You'll have to unlink() them manually.

If you need a temporary file thats automatically deleted by PHP, consider using tmpfile():

Creates a temporary file
The file is automatically removed when closed (for example, by calling fclose(), or when there are no remaining references to the file handle returned by tmpfile()), or when the script ends.

A: Yes, unlink (delete) temp files when you're done with them. Some, but not all, OSs clean up their temp directories once in a while. But tempname() uses directories other than the OS temp directories.


text:
Q: Kill process in swift I am trying to make a Mac application that will automatically close a code designated application running on the OS.  I am trying to use killall (like in Terminal).  Whenever I try to run the program, I get, "sysctl: unknown oid 'killall'".
Here's my code:
let task = Process()
    task.launchPath = "/usr/sbin/sysctl"
    ///usr/sbin/sysctl
    task.arguments = ["killall","iTunes"]
    let pipe = Pipe()
    task.standardOutput = pipe
    task.standardError = pipe
    task.launch()
    task.waitUntilExit()
    let data = pipe.fileHandleForReading.readDataToEndOfFile()
    let output: String = NSString(data: data, encoding: String.Encoding.utf8.rawValue) as! String
    print(output)

Thanks in advance!

A: my 2 cents:
You succeed in killing "iTunes" only if You Xcode App will run with SandoBox DISABLED
All examples on stack overflow about Process are misleading as they call "ls" or "echo" that are always executed in system folders.

A: I'd suggest you first read the man page for sysctl -- it's used to get and set kernel state. Does that sound like something you want?
The path to killall is /usr/bin/killall, which you can find from Terminal:
> which killall
/usr/bin/killall

Here's the full Swift code:
let pipe = Pipe()

let task = Process()
task.launchPath = "/usr/bin/killall"
task.arguments = ["iTunes"]
task.standardOutput = pipe
task.standardError = pipe
task.launch()
task.waitUntilExit()

let data = pipe.fileHandleForReading.readDataToEndOfFile()
if let output = String(data: data, encoding: .utf8) {
    print(output)
}


text:
Q: VLC: Play network video and audio from different source? I wish to play a network stream which comes in a format of m3u8.
Unfortunately video and audio are in separated files, so I tried to open a network stream and then "Play another media synchronously (extra audio file, ...)":

The problem is that VLC seems to buffer 1 second of the video, then play it, then buffer 1s of audio then play it (with video stalled). So kind of a chess table: VAVAVAV.
Is there any option to really buffer and synchonise the video and audio if both are coming from network?
Using 2 separated VLC instances I can play both video and audio, but synchronizing them are a but tidious work.
Also, (how) can I change the prebuffer (or cache, or whatever) to really buffer at least 1 minute of video AND audio, and then start playback?
I don't mind waiting at the beginning. But now video stalls in every 5-10seconds for a short time.


text:
Q: Why a Bash script still outputs to stdout even I redirect it to stderr? I have a Bash script:
#!/bin/bash
echo this should be visible only in the stderr output but it is not 1>&2
exit 0

Also, I have a custom app that starts this script. The app captures both - stdout and stderr. The stdout is empty and the stderr contains the text provided. So far, so good. But when I start the script from the console (physical terminal):
./my_script.sh

I expect it to not print anything on the display, but it prints.
I tried various combinations of redirecting, e.g., >&2, >/dev/stderr, etc. but results are the same. I also tried to redirect stdout to null 1>&2 >/dev/null, but obviously it also sends my text to the null.
Where is this configured? How can I make my terminal to not display stderr?

A: To stop the script from sending its standard error stream to the terminal, you need to redirect it to /dev/null or a file.
You do this by invoking the script with the standard error stream redirected on the command line or letting the script itself redirect its error stream.
To redirect the error stream to /dev/null on the command line, invoke the script using
./my_script 2>/dev/null

If you want the script to discard its standard error stream automatically, then redirect the stream to /dev/null within the script:
#!/bin/bash

# Discard any diagnostic output, errors, and interactive prompts.
exec 2>/dev/null

echo 'This should not be visible.' >&2
echo 'This should be visible.'

Change /dev/null to the pathname of a file to pass the diagnostic output to that file instead.
The standard output stream is not affected by the redirection of the standard error stream to /dev/null or to a file.
Note that a script should not ordinarily be terminated by exit 0.

A: To cause this one specific script to discard its standard output:
Given a script:
date
date -r /var/empty/foo

edit it to become a group command.  Grouping the commands together in this way permits the standard output and standard error streams of the combined commands to be handled (redirected, piped, discarded, etc.) as though the group was logically a single command.
For example:
{
date
date -r /var/empty/foo
} > /dev/null

will discard the standard output but not alter the routing of the standard error stream.
The following script:
{
date
date -r /var/empty/foo
} > /dev/null 2>&1

will redirect its standard output to /dev/null, and then will redirect its standard error to its standard output, thus discarding both stderr and stdout.  That particular syntax is equivalent to:
{
date
date -r /var/empty/foo
} > /dev/null 2>/dev/null


text:
Q: BehaviorSubject.First() obsolete, what is the alternative? So I have a BehaviorSubject<string> which I wish to use for a property. I have been using the blocking First() method to get the current value that the BehaviorSubject holds in a property getter.
The First() operation is now obsolete, so what is the new way to achieve this?

A: James's answer is great, but I thought it worth mentioning the general alternatives to First that works for any IObservable.  But definitely use the Value property if you know it is a BehaviorSubject.
// synchronously wait for the value
var value = someObservable.FirstAsync().Wait();

// async await
var value = await someObservable.FirstAsync();

A: With BehaviorSubject<T> you are guaranteed* to have a First value always available.
However, IObservable<T> query operators can't work under that assumption in general, so the Obsolete attribute was added to drive people towards asynchronous methods like FirstAsync - helping them to "fall into the pit of success".
Because of the special case of BehaviorSubject<T> always having a value set, it has a thread safe Value property. So, assuming that you have something like this:
var subject = new BehaviorSubject<string>("Some value");

Then you can do this:
var current = subject.Value;

*Attempting to acess the value of a BehaviorSubject<T> instance after its Dispose method has been called, whether by the Value property or a query operator like FirstAsync, will cause an ObjectDisposedException to be thrown.


text:
Q: Certificate verify failed : unable to get local issuer certificate (_ssl.c:1091) | This error come for this python library pyncclient This error come for nextcloud client python library :
pip install pyncclient

This code is implement :
import nextcloud_client

nc = nextcloud_client.Client('http://domain.tld/nextcloud')

nc.login('user', 'password')


text:
Q: How to hide console window on Windows when using scons-qt4 plugin? When I'm building on Windows with scons-qt4 plugin, my application always opens a console window, even though it has its own windows. With QMake, you could force application to do the same by adding CONFIG += console or something similar, but the default behaviour is to suppress it. QMake can handle it - I'm sure scons can, too.
The only way I see so far to solve this problem would be to use #ifdef around int main():
#ifdef WIN32
int WinMain (int _argc, char **_argv)
#else
int main (int _argc, char **_argv)
#endif

But that's just abominable!

A: So, it seems, my question was almost already answered here. Now, applied to scons, these two lines did it for me:
if (env ['PLATFORM'] == 'win32'):
    env.Append (LINKFLAGS = ['-Wl,-subsystem,windows'])

Also, kind thanks to the folks on the [scons-users] mailing list. In particular, David Van Maren pointed out that:

You might look for whether you are
  linking against the qtmain library.
  The latest Qt documentation only says
  this about it:
--------------- The qtmain Library
qtmain is a helper library that
  enables the developer to write a
  cross-platform main() function on
  Windows and on the Symbian platform.
  If you do not use qmake or other build
  tools such as CMake, then you need to
  link against theqtmain library.

It may not be related, but I think
  that once I added it to the link, the
  bogus console disappeared for our Qt
  gui applications.

This may be another way to solve the problem, for example, when program is compiled with other tools than GCC + MinGW.


text:
Q: Removing GPS data using from an image using Pillow and Piexif I am working on creating a tool to pull the exif data from an image and remove the GPS data. I use Pillow to read in the file. I then use the following function to extract the exif and remove the GPS data.
def manipulate_exif(img, remove_gps):

    try:
        import piexif
    except ImportError:
        raise

    try:
        exif = piexif.load(img.info['exif'])

        if remove_gps:
            exif.get('GPS', None).clear()
            print '{}'.format(pprint.pformat(exif))

    except Exception:
        exif = {}

    return piexif.dump(exif)

from PIL import Image
img = Image.open('./IMG_0264.jpg')

# Creates Issue
exif_copy_bad = manipulate_exif(img, True)

# No issue
exif_copy_good = manipulate_exif(img, False)

img.save('./Test_Bad.jpg', 'JPEG', exif=exif_copy_bad)
img.save('./Test_Good.jpg', 'JPEG', exif=exif_copy_good)

When I use it with the remove_gps flag set to False the function works as expected. However when I set remove_gps to True, I get weird field overflow issues in some of the fields and I get Bad IFD1 directory error in the exif file where the GPS data existed before.
I am not sure if this is a bug in Piexif or I am using Piexif incorrectly.


text:
Q: Usage of do_action and add_adction I'm trying to use add_action to add HTML output to a plugin's front end area,
Below is the snippet from plugin

<a href="<?php echo $dashboard_links['commissions'] ?>"><?php _e( 'Commissions', 'yith-woocommerce-affiliates' ) ?>

<a href="<?php echo $dashboard_links['clicks'] ?>"><?php _e( 'Clicks', 'yith-woocommerce-affiliates' ) ?>

<a href="<?php echo $dashboard_links['payments'] ?>"><?php _e( 'Payments', 'yith-woocommerce-affiliates' ) ?>

<a href="<?php echo $dashboard_links['generate_link'] ?>"><?php _e( 'Generate link', 'yith-woocommerce-affiliates' ) ?>

<a href="<?php echo $dashboard_links['settings'] ?>"><?php _e( 'Settings', 'yith-woocommerce-affiliates' ) ?>

    <?php do_action( 'yith_wcaf_after_dashboard_links', $dashboard_links ) ?>

I'm trying to extend it by adding the snippet written below in functions.php
function add_link_to_aff() {
    $html_output =  "
<?php _e( 'Test', 'yith-woocommerce-affiliates' ) ?>
";
    return $html_output;
}
add_action( 'yith_wcaf_after_dashboard_links', 'add_link_to_aff', 10 );

This is the snippet which I'm g, I also tried passing $dashboard_links to the function but it didn't worked.

A: You need to echo your data. Try this instead -
function add_link_to_aff() {
    $html_output =  "
" .  __( 'Test', 'yith-woocommerce-affiliates' ) .  "
";
    echo $html_output;
}
add_action( 'yith_wcaf_after_dashboard_links', 'add_link_to_aff', 10 );


text:
Q: Failed: Cannot read property 'setValue' of undefined for a login test I have written the following tests:
import { DebugElement } from '@angular/core';
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { AuthenticationService } from '../services/authentication.service';
import { By } from '@angular/platform-browser';
import { LoginComponent } from './login.component';
import { ReactiveFormsModule } from '@angular/forms';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
import { RouterTestingModule } from '@angular/router/testing';
import { GlobalDataService } from '../services/global-data.service';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { DatePipe } from '@angular/common';
import { MatDialogModule } from '@angular/material/dialog';
import { OverlayModule } from '@angular/cdk/overlay';

describe('LoginComponent', () => {
  let component: LoginComponent;
  let fixture: ComponentFixture<LoginComponent>;

  beforeEach(waitForAsync(() => {
    TestBed.configureTestingModule({
      declarations: [ LoginComponent ],
      imports: [ ReactiveFormsModule, MatFormFieldModule, MatProgressSpinnerModule, RouterTestingModule, HttpClientTestingModule,
         OverlayModule, MatDialogModule ],
      providers: [ GlobalDataService, DatePipe, AuthenticationService ]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(LoginComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });

  it('should set instance correctly', () => {
    expect(component).not.toBeNull();
   });

  it('should call auth login method', waitForAsync(() => {
    let loginElement: DebugElement;
    const debugElement = fixture.debugElement;
    const authenticationService = debugElement.injector.get(AuthenticationService);
    const loginSpy = spyOn(authenticationService, 'login').and.callThrough();
    loginElement = fixture.debugElement.query(By.css('form'));
    // to set values
    component.loginForm.controls.username.setValue('test@test.com');
    component.loginForm.controls.password.setValue('testpassword');
    loginElement.triggerEventHandler('ngSubmit', null);
    expect(loginSpy).toHaveBeenCalledTimes(1); // check that service is called once
    // ^ this fails without correct login!
   }));

  it('should render "Login" at the top of the login page', () => {
    fixture.detectChanges();
    const compiled = fixture.debugElement.nativeElement;
    expect(compiled.querySelector('h5').textContent).toContain('Login');
  });
});

My Login.component.ts:
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

import { AuthenticationService } from '../services/authentication.service';
import { GlobalDataService } from '../services/global-data.service';

/* tslint:disable variable-name */
@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {

  returnUrl: string;
  loginForm: FormGroup;
  submitted = false;
  error = '';
  loading = false;
  public errorMsg = 'Please login to continue';
  public redirected: boolean;
  public utm_source: string;

  constructor(private router: Router, private formBuilder: FormBuilder, public DataService: GlobalDataService,
              public authenticationService: AuthenticationService, private activatedRoute: ActivatedRoute) {
      this.activatedRoute.queryParams.subscribe(params => {
      const param = params.utm_source;

      if ([ 'a', 'b', 'c', 'd', 'e'].includes(param)) {
        this.redirected = true;
        this.utm_source = param;
      } else {
        this.redirected = false;
      }
  });
  }

  ngOnInit(): void {
    this.loginForm = this.formBuilder.group({
      email: ['', [Validators.required, Validators.email]],
      password: ['', [Validators.required, Validators.minLength(6)]]
  });
  }

// convenience getter for easy access to form fields
get f() { return this.loginForm.controls; }

  onSubmit(loginsubmit) {
    this.submitted = true;
    // stop here if form is invalid
    if (this.loginForm.invalid) {
        return console.log('LoginForm Invalid');
    }
    this.loading = true;

    this.authenticationService.login(this.f.email.value, this.f.password.value)
        .pipe()
        .subscribe(
            data => {
              // it was this.returnUrl instead of avior/dashboard
                if (this.redirected) {
                  this.router.navigate([this.utm_source]);
                } else {
                  this.router.navigate(['landing-page']);
                }
            },
            error => {
                console.log('Login->authservice->err: ', error);
                this.error = error;
                this.loading = false;
            });
}
}

The error I get happens on the should call auth login method test with the following error:
Failed: Cannot read property 'setValue' of undefined

So it appears that the LoginForm form is somewhy and somehow undefined. Any clue why that might be? The definition of the form field appears to be fine, I have honestly no clue what's wrong. The most weird part is that it used to work and now it doesn't after migrating the code. I tried mendling with the code with no avail.

A: In you test setup you are trying to set a value on a control that doesn't exist in your form.
component.loginForm.controls.username.setValue('test@test.com'); // username doesn't exisit
the property is called email. try to change the line to:
component.loginForm.controls.email.setValue('test@test.com');

A: I had similar error:
Ensure to to initialize the form creation and patch under constructor and ngOnInit respectively i.e
 // creation goes in constructor
  dataFormGroup:FormGroup
  constructor(private formBuilder: FormBuilder) {
    this.dataFormGroup = this.formBuilder.group({
     ......
     ..........
   })

// patch/setvalue goes under ngOnInit
ngOnInit() {
  let urlData = history.state
  this.mode = urlData.mode
  this.update_data = urlData.user
  if (this.mode == "edit") {
    this.dataFormGroup.patchValue({
      first_name: this.update_data.first_name,
      last_name: this.update_data.last_name,
      ...
      .....
    });
  }
}


text:
Q: Definition of intersection of a family of sets The definition of an intersection of family($F$) is:
$$\cap F=\bigl\{x\mid\forall A: (A\in F \implies x\in A)\bigr\} $$
If my understanding serves me correctly this notation means that all the $x$ that pass the elementhood test are in the set. So $\forall A: (A\in F \implies x\in A)$ must be either true or false for each different $x$. But in this case isn't $F$ also a free variable, so we can't assign a truth value to $\forall A:(A\in F \implies x\in A)$ as it contains a free variable $F$ ($x$ and $A$ are bounded). Is there some implicit assumption about $F$ ? Also in case of an indexed family $F=\{A_i\mid i\in I\}= \bigl\{x\mid\exists i\in I: x=A_i\bigr\}$, is $A_i$ (which is a function of $i$) bound or free (if free isn't it the same problem as the first question) ?

A: $F$ is not a free variable. It denotes a particular family of sets which you're taking the intersection of. For instance, if $F = \{\{1, 2\}, \{1, 3\}\}$ then your definition says that $\cap F$ consists of those $x$ which are in every set which is in $F$. In my example, the only such $x$ is $1$. Thus the statement $\forall A(A \in F \implies x \in A)$ is true for $1$, but false for $2$ and $3$.
The indexed family just gives a way of listing an infinite set of sets. Namely, it says that for each element $i$ of the set $I$, we have a set $A_i$ which is in $F$ (as an element, not a subset). If one assumes that $A_i \neq A_j$ whenever $i \neq j$, then this implies that the number of sets in $F$ has the same cardinality as $I$.


text:
Q: I'm getting a error Function eregi() I'm getting a Deprecated: Function eregi() is deprecated in C:\website\Web_Intersect_v1.34\profile.php on line 231.
I try to use preg_match() and that dosn't work. What should I use to make the code work.
$OSList = array
(
    // Match user agent string with operating systems
    'Windows 3.11' => 'Win16',
    'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
    'Windows 98' => '(Windows 98)|(Win98)',
    'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
    'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
    'Windows Server 2003' => '(Windows NT 5.2)',
    'Windows Vista' => '(Windows NT 6.0)',
    'Windows 7' => '(Windows NT 6.1)|(Windows NT 7.0)',
    'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
    'Windows ME' => 'Windows ME',
    'Open BSD' => 'OpenBSD',
    'Sun OS' => 'SunOS',
    'Linux' => '(Linux)|(X11)',
    'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
    'QNX' => 'QNX',
    'BeOS' => 'BeOS',
    'OS/2' => 'OS/2',
    'Mac OS' => 'Mac OS',
    'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match) {
    // Find a match
    if (eregi($Match, $agent)) {
            break;
    } else {
        $CurrOS = "Unknown OS";
    }
}
$device = "$user_device : $CurrOS";

A: $agent = $_SERVER['HTTP_USER_AGENT'];
if (preg_match("/iPhone/", $agent)) {
$user_device = "iPhone Mobile";
}   else if (preg_match("/Android/", $agent)) {
$user_device = "Android Mobile";
} else if (preg_match("/IEMobile/", $agent)) {
$user_device = "Windows Mobile";
} else if (preg_match("/Chrome/", $agent)) {
$user_device = "Google Chrome";
} else if (preg_match("/MSIE/", $agent)) {
$user_device = "Internet Explorer";
} else if (preg_match("/Firefox/", $agent)) {
$user_device = "Firefox";
} else if (preg_match("/Safari/", $agent)) {
$user_device = "Safari";
} else if (preg_match("/Opera/", $agent)) {
$user_device = "Opera";
}


text:
Q: Error: EACCESS, permission denied at Object.fs.mkdirSync Getting this error when trying to install meanjs:crud-module
Error: EACCES, permission denied '/opt/mean/public/modules/trees'
at Error (native)
at Object.fs.mkdirSync (fs.js:751:18)
at Function.sync (/usr/local/lib/node_modules/generator-meanjs/node_modules/yeoman-generator/node_modules/mkdirp/index.js:71:13)
at yeoman.generators.NamedBase.extend.renderModule (/usr/local/lib/node_modules/generator-meanjs/crud-module/index.js:87:8)
at /usr/local/lib/node_modules/generator-meanjs/node_modules/yeoman-generator/lib/base.js:395:14
at processImmediate [as _immediateCallback] (timers.js:358:17)

I've basically chown/chmod every folder on my server, and I've uninstalled/reinstalled yo using sudo npm and -g several different times. Can't figure it out. Anyone got any help for me?


text:
Q: Elasticsearch: better to have more values or more fields? Suppose to have an index with documents describing vehicles.
Your index needs to deal with two different type of vehicles: motorcycle and car.
Which of the following mapping is better from a performance point of view?
(nested is required for my purposes)
    "vehicle": {
        "type": "nested",
        "properties": {
            "car": {
                "properties": {
                    "model": {
                        "type": "string"
                    },
                    "cost": {
                        "type": "integer"
                    }
                }
            },
            "motorcycle": {
                "properties": {
                    "model": {
                        "type": "string"
                    },
                    "cost": {
                        "type": "integer"
                    }
                }
            }
        }
    }

or this one:
"vehicle": {
    "type": "nested",
    "properties": {

        "model": {
            "type": "string"
        },
        "cost": {
            "type": "integer"
        },
        "vehicle_type": {
            "type": "string"     ### "car", "motorcycle"
        }

    }
}

The second one is more readable and thin.
But the drawback that I'll have is that when I make my queries, if I want to focus only on "car", I need to put this condition as part of the query.
If I use the first mapping, I just need to have a direct access to the stored field, without adding overhead to the query.

A: The first mapping, where cars and motorcycles are isolated in different fields, is more likely to be faster. The reason is that you have one less filter to apply as you already know, and because of the increased selectivity of the queries (e.g less documents for a given value of vehicle.car.model than just vehicle.model)
Another option would be to create two distinct indexes car and motorcycle, possibly with the same index template.
In Elasticsearch, a query is processed by a single-thread per shard. That means, if you split your index in two, and query both in a single request, it will be executed in parallel.
So, when needed to query only one of cars or motorcycles, it's faster simply because indexes are smaller. And when it comes to query both cars and motorcycles it could also be faster by using more threads.
EDIT: one drawback of the later option you should know, the inner lucene dictionary will be duplicated, and if values in cars and motorcycles are quite  identical, it doubles the list of indexed terms.


text:
Q: How to avoid thread interference while updating database in multithreading I have a java class ,which has two methods .These methods will be called one after the other. These methods are making rest calls to different services. After getting data from rest call ,i'm updating multiple tables and some of the tables will be updated by both methods. My task is to implement multithreading to run both methods simultaneously but avoid threads to override data while they are updating the same table. What are the different ways to do it and how can i do it effectively in java8.

A: To avoid race condition, you should wait for both thread collect all data from 2 rest calls then do the database modification, like example:
ExecutorService es = Executors.newFixedThreadPool(2);
List<Callable<Object>> todo = new ArrayList<Callable<Object>>();

todo.add(Executors.callable(new TaskCallRest1()));
todo.add(Executors.callable(new TaskCallRest2()));

List<Future<Object>> answers = es.invokeAll(todo);

For database synchronization, you can use optimistic lock to prevent multiple update. Optimistic lock simple is version of your record and the query look like:
update A set B = b, version = 2 where version = 1;

Only one thread can execute successful this query due to database ACID.
If you use Hibernate for persistence layer, you can looking for Hibernate Versioning

A: It is better to view this problem from data integrity perspective rather than threads. Because your application could run in a cluster (if not now, maybe later). There could be other users updating same table, from other applications.
The solution depends on the level of contention(the number of threads trying to update the same database table).
If there is high contention, use pessimistic locking. Refer pessimistic locking
If there is low contention, use optimistic locking. Refer optimistic locking


text:
Q: Google Cloud Speech permission issue I am trying to process speech in real time in order to transcribe it, using Google Cloud's speech API.
I have a service account associated with the speech service, plus a local JSON which should authorize my requests to Google Cloud (it contains a couple of credentials). Even if the key is set to expire on January 1st, 10000, attempting to obtain the transcript generates the following error:
google.api_core.exceptions.Unauthenticated: 401 Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project. [reason: "ACCESS_TOKEN_EXPIRED"
domain: "googleapis.com"
metadata {
  key: "method"
  value: "google.cloud.speech.v1.Speech.StreamingRecognize"
}
metadata {
  key: "service"
  value: "speech.googleapis.com"
}
]

What am I doing wrong? I cannot seem to find a solution for this online.
Thank you!


text:
Q: ifstream doesn't read the double to the end c++ I am trying to do external sorting by reading a big file and writing them into small pieces first. But when I get double values with ifstream from the file although the numbers are 12 digits, it only gets first 6 digit.
#include<fstream>
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<stdlib.h>

using namespace std;

void createRun(double *arr, int size, int orderoffile){
  make_heap(arr, arr+size);
  for(int i=0; i<size ;i++){
    pop_heap(arr, arr+size-i);
  }
  string filename = "sortedTemp" ;
  filename += to_string(orderoffile) + ".txt";

  ofstream ofs(filename.c_str());

  for(int i=0;i<size-1;i++){
    ofs<<arr[i]<<endl;
  }
  ofs<<arr[size-1];
}

int main(int argc, char *argv[]){

ifstream ifs(argv[1]);

int num;
ifs>>num;

int files=num/1024;
double arr[1024];

for(int i=0; i<files ; i++ ){
  for(int j=0 ; j<1024;j++){

    ifs>>arr[j];

  }
  createRun(arr, 1024 ,i);
}

int remaining;
if(num%1024!=0){
  remaining =num%1024;
  files++;

  for(int i=0 ; i< remaining; i++){
    ifs>>arr[i];
  }
  createRun(arr, remaining ,files-1);

}
ifs.close();

  return 0;

}

The file I am trying to read is contains 2048 doubles, I copied the first 32.The first integer just gives the number of doubles in file.
>
 2048
185261.886524
237358.956723
139921.832662
128702.977104
93468.067919
415572.438294
394341.383634
317808.343493
63075.462939
907012.789025
393656.286891
418873.927354
892347.117718
764276.357958
300568.750436
717272.259456
354591.446610
431749.187142
63565.857898
288241.215494
526117.693273
680793.623315
68218.415160
391965.952529
66870.811087
3982.888630
469794.327128
492466.036574
865900.028660
717094.169557
32770.089775

it just writes the file like this.
836.661
3366.63
3982.89
5394.17
6764.96
9018.77
9034.42
9058.47
10259.5
11236.4
12419.8
13368.7
13659.5
13876
15553.6
15657.5
16682.7
17647.7
19490.9
19519
19632.4
19936.8
22118
23157.6
23718.4
23781.5
24640.4
24889.4
25054.2
25151.9
25647.5
26293.4

A: Binary floating points don't know how many significant digits there may be (this is to some extend different to decimal floating points which are not normalized and indicate a number of significant digits). Floating point numbers are formatted based on the flag stored in the precision() field of a stream. If you want to retain more digits you could consider using std::numeric_limits<double>::digits10 as a potential setup. Note that depending on the values and other formatting flags (notably whether std::ios_base::fixed is set) you may defeat the formatting algorithm and get values which are not properly rounded but more closely represent that actually stored binary value than the approximated decimal value.

A: To preserve the formatting of your numbers, you should read them as strings rather than as doubles. Then you have 3 options for the actual sorting:

*

*Compare the strings directly. This can get tricky as you need to track the decimal point and allow for a possible negative sign at the front - there will be lots of edge cases.

*Convert the string to a double each time you need to compare.

*Instead of keeping an array of double or string, create a structure that contains both and populate the array with that. Use the double for comparison, and the string for output.


text:
Q: How to combine a stateful browser creating tabular data with a Wicket web application? I am trying to write a wicket client, which is some what stateful. This gives me some head aches as I have a form presenting some tabular data. The user must be able to add/remove rows to the data, without provoking an Ajax request to the server every time a row has been added/delete. I have implemented the add row functionality in the browser by usage of jQuery which works perfectly. The question is how I get this to work with Wicket?
My html snippet:

    ...

      <input wicket:id="berth.name" type="text"/>
      <input wicket:id="berth.position.lattitude" type="text"/>
      <input wicket:id="berth.position.longitude" type="text" />
  <input wicket:id="arrival" type="text"/>
      <input wicket:id="departure" type="text"/>
      <button type="submit" class="btn btn-danger">Delete</button>

My Java code:
private CompoundPropertyModel<List<Voyage>> model = new CompoundPropertyModel<>(new VoyageModel());

public MyForm(String id) {
  super(id, "MyForm");
  WebMarkupContainer modalBody = new WebMarkupContainer("modalBody", model);
  add(modalBody);

  final PropertyListView<Voyage> lv = new PropertyListView<Voyage>("voyagePlan") {
    @Override
    protected void populateItem(ListItem<Voyage> item) {
      item.add(new TextField<String>("key"));
      item.add(new TextField<String>("berth.name"));
      item.add(new TextField<String>("berth.position.lattitude"));
      item.add(new TextField<String>("berth.position.longitude"));
      item.add(new TextField<String>("arrival"));
      item.add(new TextField<String>("departure"));
    }
  };

  modalBody.add(lv);

  saveLink = new AjaxSubmitLink("save") {
    @Override
    protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
      shipService.saveVoyageInformation(model.getObject());
      target.add(this.getParent());
    }
  };
  add(saveLink);
}

public class VoyageModel extends LoadableDetachableModel<List<Voyage>> {
  @Override
  protected List<Voyage> load() {
    return myService.getVoyage(mmsi);
  }
}

Having this code, then problem more specifically becomes, how to update the List instance when new rows has been added and submitted? Currently only changes to existing rows  are updated. New rows are never saved into the database.
I have thought of writing functionality for sending data as json by following the approach described here: http://thewicketevangelist.blogspot.dk/2011/12/marrying-wicket-and-jquery-ajax.html. If I understand correctly, this will however mean that form input field validations like TextField.setRequired etc are no longer usable. Is that correctly understood?
I would like an approach close to the normal Wicket web request handling, with all the benefits of input validation etc.

A: If I understand correctly, what you want is to create a dynamic form, "dynamic" meaning that the structure of the form is defined and potentially changed during runtime.
That can of course be done with Wicket but it is not easy with any server side web framework since the client-side representation has to be kept in sync with the server-side Java components. The JQuery-approach sending JSON to the server is somewhat bypassing the Wicket features, as you already mentioned, and you would have to re-code some things that Wicket usually already does for you.
As I see it, you have the following options:

*

*you can code your own "dynamic form" component with Wicket, which you already started in your code snippet. You would have to solve several problems like the one you already stumbled upon: how to update your Wicket List component (perhaps my blog post will help with that: http://tom.hombergs.de/2013/03/advanced-wicket-dynamic-listview-within.html).

*you can use Wicked Forms (http://wicked-forms.org) which is a library I am currently developing to create dynamic forms with Wicket. It's not released yet, but you can use it at your own risk (or you can have a look at the sources and build your own). There is a showcase on the project homepage which shows the features. Have a look if it meets your requirements.

A: You should be able to do it with fragments but not without Ajax.
Create a fragment representing a voyagePlan row.
Change your PropertyListView to populate fragments.
Create an AbstractDefaultAjaxBehavior that generates a new Voyage object + fragment, adds it to your PropertyListView and adds your modalBody to target to reload the list.
Override renderHead to add a javascripts function that triggers the AbstractDefaultAjaxBehavior
Have jquery call that javascript function when the user wants to add a new row.


text:
Q: Flatten only deepest level in scala spark dataframe I have a Spark job, which has a DataFrame with the following value :
{
  "id": "abchchd",
  "test_id": "ndsbsb",
  "props": {
    "type": {
      "isMale": true,
      "id": "dd",
      "mcc": 1234,
      "name": "Adam"
    }
  }
}

{
  "id": "abc",
  "test_id": "asf",
  "props": {
    "type2": {
      "isMale": true,
      "id": "dd",
      "mcc": 12134,
      "name": "Perth"
    }
  }
}

and I want to flatten it out elegantly (as no of keys is unknown and type etc) in such a way that props remains as a struct but everything inside it is flattened off (irrespective of the level of nesting)
The output desired is :
{
  "id": "abchchd",
  "test_id": "ndsbsb",
  "props": {
    "type.isMale": true,
    "type.id": "dd",
    "type.mcc": 1234,
    "type.name": "Adam"
  }
}

{
  "id": "abc",
  "test_id": "asf",
  "props": {
      "type2.isMale": true,
      "type2.id": "dd",
      "type2.mcc": 12134,
      "type2.name": "Perth"
  }
}

I used the solution mentioned in
Automatically and Elegantly flatten DataFrame in Spark SQL
however, I'm unable to keep the props field intact. It also gets flattened off.
Can somebody help me with extending this solution?
The final schema should be something like :
root
 |-- id: string (nullable = true)
 |-- props: struct (nullable = true)
 |    |-- type.id: string (nullable = true)
 |    |-- type.isMale: boolean (nullable = true)
 |    |-- type.mcc: long (nullable = true)
 |    |-- type.name: string (nullable = true)
      |-- type2.id: string (nullable = true)
 |    |-- type2.isMale: boolean (nullable = true)
 |    |-- type2.mcc: long (nullable = true)
 |    |-- type2.name: string (nullable = true)
 |-- test_id: string (nullable = true)

A: I've been able to achieve this with the RDD API :
val jsonRDD = df.rdd.map{row =>
  def unnest(r: Row): Map[String, Any] = {
    r.schema.fields.zipWithIndex.flatMap{case (f, i) =>
      (f.name, f.dataType) match {
        case ("props", _:StructType) =>
          val propsObject = r.getAs[Row](f.name)
          Map(f.name -> propsObject.schema.fields.flatMap{propsAttr =>
            val subObject = propsObject.getAs[Row](propsAttr.name)
            subObject.schema.fields.map{subField =>
              s"${propsAttr.name}.${subField.name}" -> subObject.get(subObject.fieldIndex(subField.name))
            }
          }.toMap)
        case (fname, _: StructType) => Map(fname -> unnest(r.getAs[Row](fname)))
        case (fname, ArrayType(_: StructType,_)) => Map(fname -> r.getAs[Seq[Row]](fname).map(unnest))
        case _ => Map(f.name -> r.get(i))
      }
    }
  }.toMap

  val asMap = unnest(row)
  new ObjectMapper().registerModule(DefaultScalaModule).writeValueAsString(asMap)
}

val finalDF = spark.read.json(jsonRDD.toDS).cache

The solution should accept deeply nested inputs, thanks to recursion.
With your data, here's what we get :
finalDF.printSchema()
finalDF.show(false)
finalDF.select("props.*").show()

Outputs :
root
 |-- id: string (nullable = true)
 |-- props: struct (nullable = true)
 |    |-- type.id: string (nullable = true)
 |    |-- type.isMale: boolean (nullable = true)
 |    |-- type.mcc: long (nullable = true)
 |    |-- type.name: string (nullable = true)
 |-- test_id: string (nullable = true)

+-------+----------------------+-------+
|id     |props                 |test_id|
+-------+----------------------+-------+
|abchchd|[dd, true, 1234, Adam]|ndsbsb |
+-------+----------------------+-------+

+-------+-----------+--------+---------+
|type.id|type.isMale|type.mcc|type.name|
+-------+-----------+--------+---------+
|     dd|       true|    1234|     Adam|
+-------+-----------+--------+---------+

But we can also pass more nested/complexe structures like for instance :
val str2 = """{"newroot":[{"mystruct":{"id":"abchchd","test_id":"ndsbsb","props":{"type":{"isMale":true,"id":"dd","mcc":1234,"name":"Adam"}}}}]}"""

...

finalDF.printSchema()
finalDF.show(false)

Gives the following output :
root
 |-- newroot: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- mystruct: struct (nullable = true)
 |    |    |    |-- id: string (nullable = true)
 |    |    |    |-- props: struct (nullable = true)
 |    |    |    |    |-- type.id: string (nullable = true)
 |    |    |    |    |-- type.isMale: boolean (nullable = true)
 |    |    |    |    |-- type.mcc: long (nullable = true)
 |    |    |    |    |-- type.name: string (nullable = true)
 |    |    |    |-- test_id: string (nullable = true)

+---------------------------------------------+
|root                                         |
+---------------------------------------------+
|[[[abchchd, [dd, true, 1234, Adam], ndsbsb]]]|
+---------------------------------------------+

EDIT: As you mentioned, if you have records with different structure you need to wrap the above subObject value in an option.
Here's the fixed unnest function :
def unnest(r: Row): Map[String, Any] = {
  r.schema.fields.zipWithIndex.flatMap{case (f, i) =>
    (f.name, f.dataType) match {
      case ("props", _:StructType) =>
        val propsObject = r.getAs[Row](f.name)
        Map(f.name -> propsObject.schema.fields.flatMap{propsAttr =>
          val subObjectOpt = Option(propsObject.getAs[Row](propsAttr.name))
          subObjectOpt.toSeq.flatMap{subObject => subObject.schema.fields.map{subField =>
            s"${propsAttr.name}.${subField.name}" -> subObject.get(subObject.fieldIndex(subField.name))
          }}
        }.toMap)
      case (fname, _: StructType) => Map(fname -> unnest(r.getAs[Row](fname)))
      case (fname, ArrayType(_: StructType,_)) => Map(fname -> r.getAs[Seq[Row]](fname).map(unnest))
      case _ => Map(f.name -> r.get(i))
    }
  }
}.toMap

New printSchema gives :
root
 |-- id: string (nullable = true)
 |-- props: struct (nullable = true)
 |    |-- type.id: string (nullable = true)
 |    |-- type.isMale: boolean (nullable = true)
 |    |-- type.mcc: long (nullable = true)
 |    |-- type.name: string (nullable = true)
 |    |-- type2.id: string (nullable = true)
 |    |-- type2.isMale: boolean (nullable = true)
 |    |-- type2.mcc: long (nullable = true)
 |    |-- type2.name: string (nullable = true)
 |-- test_id: string (nullable = true)


text:
Q: Function returning a number of 2D array locations in C doesn't work I'm new to c and trying to create the minesweeper game. I think my code has some problems with the data type conversion but I don't understand why. Checked this link
How to cast or convert an unsigned int to int in C?
and tried to implement the info in my code.
I'm trying to implement  a function that returns the number of neighboring locations of a x,y coordinated 2D array that have mines.
int neighbours(const Field *f, unsigned int x, unsigned int y)
{
    int z = 0;
    int i,j;
    unsigned int a = y-1;
    j =(int)a;
    unsigned int b =x-1;
    i = (int)b;

    for(; j<=(a+2); j++){
        if(j>=0 && j<=f->ysize){
            for (; i<=(b+2); i++){
                if ( i>=0 && i<=f->xsize && (f->places[j][i] == UNKNOWN_MINE ||f->places[j][i] == KNOWN_MINE)){
                    z++;
                 }

             }
            i = b;
        }
    }
    return z;
 }

A: The code
unsigned a = y-1;

gives you a == UINT_MAX if y == 0
the condition
  j >= 0

is just saying "if this non-negative integer is not negative", so not very helpful.
A good idea would be to store the data with some padding, where the rectangle map is padded with one width of integers on each side, all having a uniqe value you would call something like PADDING_FIELD
This way your function would simpler, more effecient:
int neighbours(const Field *f, unsigned int x, unsigned int y)
{
  int z = 0;

  assert(x > 0 && x < UINT_MAX);
  assert(y > 0 && y < UINT_MAX);

  for(unsigned int j = y-1; j <= y+1); j++) {
        for (unsigned int i = x-1; i <= x+1; i++) {
            if ((f->places[j][i] == UNKNOWN_MINE ||f->places[j][i] == KNOWN_MINE)){
                z++;
            }
        }
  }
  return z;
}

This way you don't need to check being out of bounds, if you would address something outside, you would just get f->places[j][i] == PADDING_FIELD , which doesn't match anything.
Also no problem with type conversions, you know x or y can't be zero or UINT_MAX.
EDIT
You could also eliminate one of the checks in the little loop, if you choose the values of UKNOWN_MINE and KNOWN_MINE carefully. Could do something like:
    static const field mine = 4;
    static const field known = 8;

    inline bool is_mine(field f)
    {
            return (f & mine) != 0;
    }

    inline bool is_knownmine(field f)
    {
            return (f & mine & known) != 0;
    }

    inline bool is_unknownmine(field f)
    {
            return is_mine(f) && !is_knownmine(f);
    }

    int neighbors(....)
    {
     .....
      if (is_mine(f->places[j][i]) {
          ++z;
      }
     .....
    }


text:
Q: MySQL backup / replication strategy - minimize down time and minimize data loss Just wondering what is the best way to protect data loss in the event of hardware failure?
The goal is to minimize server downtime and minimize data loss.
We cannot afford big-name cloud database vendors, we will have to do it on our dedicated server(s).
I know traditional backup is not sufficient for this, I am thinking about real-time replication.
One thing I am not sure is, if the master failed due to hardware issues, will the replication automatically becomes master? do I need to config anything from my application (Spring boot)?  If the master's hardware issue is fixed, will master automatically sync all data from the replication?  Or there are still manual works?
Can someone please recommend a guide for this?
This is what I am thinking, any comments?

Thanks!


text:
Q: Intuition behind the fact that the figure eight in plane is not a single integral curve of a vector field
Consider the figure eight in the plane, with concrete parametrization, for instance: $$f:(-\pi, \pi) \to \mathbb{R}^2, f(t) = (\sin(2t), \sin(t)).$$

We know that the image of $f$ is an immersed submanifold of $\mathbb{R}^2$, but it is not embedded in $\mathbb{R}^2$. My question is: why can't the image of $f$ be a single integral curve of (smooth) a vector field $X$ on $\mathbb{R}^2$? What is the intuition behind it, and how would a formal proof look like?

I assume that this is because of the self-intersection point, since then, the vector field would have to "split" its direction, but is this the full intuition behind it?
I also think that the figure eight should be the union of three integral curves of a vector field.

A: Following up on the remark in the comments, the fact that there is no way to "tell the particle where to go" means that there is no well-defined tangent space at $(0,0)$. For, suppose we had a curve $\gamma$ on some open interval $0\in I$ of $\mathbb R$. Let $S=\gamma(I)$ and $\gamma(0)=0.$  Then, if $0\in U\subseteq \mathbb R^2$ be an open neighborhood, $S\cap U$ is open in $S$. Take $U$ sufficiently small so that $(S\cap U)\setminus \{0\}$ has four connected components. Then,  $S\cap U$
cannot be homeomorphic to any open interval $J$ because  $J\setminus \{0\}$ has two connected components.
Another way to see this which only uses the fact that
$T_0S= \{v\in T_0\mathbb R^2:v(f)=0\ \text{for all}\ f\in C^{\infty}(\mathbb R^2)\ \text{such that}\ f_{|S}=0\}$ and a quick calculation of derivatives is to note that
$\gamma_1(t) = (\sin 2t, \sin t))$ and $\gamma_2(t) = (-\sin 2t, \sin t)$ both have their images in $S$ and give rise to derivations $\gamma_1'(0)=(2,1)$ and $\gamma_2'(0)=(-2,1)\ \textit{in}\ T_0\mathbb R^2.$
Now, suppose $f\in C^{\infty}(\mathbb R^2)$ such that $f_{|S}=0.$ Then, for
$i=1,2,\ \gamma_i'(0)(f)=\displaystyle\left(\frac{d}{dt}\right)_{t=0}\left(f\circ \gamma_i(t)\right)=0$.
But now since $\gamma_1'(0)$ and $\gamma'_2(0)$ cannot both be in $T_0S$, we have that
$T_0S\neq \{v\in T_0\mathbb R^2:v(f)=0\ \text{for all}\ f\in C^{\infty}(\mathbb R^2)\ \text{such that}\ f_{|S}=0\}.$

A: The Cassini ovals form a family of curves that contains your lemniscate as a special individuum (by the way: your parametrization produces a "vertical" lemniscate). Here is a sketch of the family:

You can orient all occurring loops counterclockwise and then define a smooth vector field ${\bf v}$ in ${\mathbb R}^2$ having these curves as integral curves. This ${\bf v}$ has three zeros at $(0,0)$ and $(\pm c,0)$. Each half of the lemniscate is a "full" integral curve, since the moving particle takes time $-\infty<t<\infty$ to make the loop from $(0,0)$ to $(0,0)$.


text:
Q: Running a .net core windows service with admin privileges I have created a windows service using .net core and Topshelf.
I am not able to access the com objects when I run as a service whereas when I run the exe from command prompt with admin access, It's working as expected.
Could you please help. Thanks


text:
Q: How can I write a Pandas Dataframe containing a dictionary to Bigquery? I have JSON data from API which contain page number called 'offset' and 'items' which is a nested JSON like below:
{
    "data": {
        "offset": 0,
        "pageSize": 20,

        "items": [
            {
                "id": "6biewd5a",
                "title": "AAAAAAAAAAAAAA"
             },
                {
                "id": "er45ggg",
                "title": "BBBBBBBBBBBBBBBB"
             }
                 ]
             }
  }

I am creating a dataframe to write to bigquery.
Here is my code.
import requests
from requests.auth import HTTPBasicAuth
import json
from google.cloud import bigquery

import pandas
import pandas_gbq

URL = 'xxxxxxxxxxxxxxxxxxxx'

auth = HTTPBasicAuth('name', 'password')

r = requests.get(url=URL, auth=auth)

# Extracting data in JSON format
data = json.loads(json.dumps(r.json()))
# data = r.json()
print(type(data))
offset = str(data['data']['offset'])
json_data = data['data']['items']

type(json_data)

df = pandas.DataFrame(
    {
    'offset': offset,
    'json_data': json_data

)
# df['json_data'] = df['json_data'].astype('string')

client = bigquery.Client(project='ncau-data-newsquery-sit')
table_id = 'sdm_adpoint.testfapi1'

job_config = bigquery.LoadJobConfig(
        schema=[
            bigquery.SchemaField("offset", "STRING"),
            bigquery.SchemaField("json_data", "STRING", "REPEATED")
            # , index=[0]

        ],
        autodetect=False

)

df.head()

pandas_gbq.to_gbq(df, table_id, project_id='ncau-data-newsquery-sit', if_exists='append')

I have made dataframe out of this like below:
Dataframe
But when I write to bigquery using
pandas_gbq.to_gbq(df, table_id, project_id='ncau-data-newsquery-sit', if_exists='append')

It throws the below error:

ArrowTypeError: Could not convert {'id': '6biewd5a',......

To overcome this, I tried this:
df['json_data'] = df['json_data'].astype('string')

It's working, but it's merging all rows as one, and writing each letter in a separate row.
I want data to be written as a row, the same way as displayed in a Pandas dataframe.
I tried to write to Bigquery using
pandas_gbq.to_gbq(df, table_id, project_id='ncau-data-newsquery-sit', if_exists='append')
But it throws an error

ArrowTypeError: Could not convert {'id': '6biewd5a',......

Then I tried
df['json_data'] = df['json_data'].astype('string')

But it's merging all rows as one, and writing each letter in separate rows.
I want data to be written as a row, the same way as displayed in Pandas dataframe.

A: Your JSON schema is very similar to the schema given here in a Google Cloud Platform example:
Example schema
So you will have to make the below change to your schema definition in your code.
For the items schema, you will have to define the schema as below:
bigquery.SchemaField(
        "items",
        "RECORD",
        mode="REPEATED",
        fields=[
                bigquery.SchemaField("id", "STRING", mode="NULLABLE"),
                bigquery.SchemaField("title", "STRING", mode="NULLABLE"),
             ],
        )


text:
Q: Асимптотическая сложность худшего случая алгоритма Бойера-Мура + правило Галиля Всем привет! Есть вопрос относительно асимптотической сложности алгоритма Бойера-Мура. С одной стороны, в большинстве источников написано, что худший случай это когда есть исходный текст, например, "ааааааааа" и шаблон(образ) "аааа", в таком случае асимптотическая сложность - О(n*m), где n - длина исходного текста, m - длина шаблона. С другой стороны, есть еще информация, что худший случай имеет сложность О(n+m). Не понятно, при каком исходном тексте и шаблоне можно получить такую сложность?
Также не совсем понятной является связь правила Галиля и алгоритма Бойера-Мура.


text:
Q: How to generate multiple files from r script function with knitr I would like to generate a number of reports from an r script. I would like to avoid the duplication needed when compiling from rmd files and I'd like to generate the reports from the r script itself if possible.
The script has a function which should generate each report from a list of dataframes where each report represents the processing of one dataframe. I just can't understand how to generate each report separately. At the moment the report shows all of the dataframes in one file
The original script is long so I have provided a minimal version below.
    structure(list(a = structure(c(1L, 3L, 2L), .Label = c("boo",
    "saa", "yaew"), class = "factor"), b = structure(c(2L, 3L,
    1L), .Label = c("mfds", "shu", "ren"), class = "factor"), c = structure(c(2L,
    1L, 3L), .Label = c("22", "23", "5345"), class = "factor")), .Names = c("a",
    "b", "c"), row.names = c(NA, -3L), class = "data.frame")

    ReportOp<-function(n) {
  this_is_a_name <- n;
  this_is_my_data <- ldf[[n]]

  #' ---
  #' author: Me
  #' date:
  #' ---

  #+results='asis', echo=FALSE
  knitr::kable(this_is_my_data, digits = 2)
  #'
}

At the moment I am just generating the report with everything in it using thecompile notebook button in R studio.
I tried using knitr::spin on the above script as follows from a separate file:
library(knitr)
o=spin('/Users/sebastianzeki/Desktop/UntitledTBB.R')
knit2html(o,output="/Users/sebastianzeki/Desktop/out.html")

but again I only get one report outputted rather than three.

A: You would make a single rmarkdown parameterized report. Define the parameters in the YAML.   You render the report by passing the parameters to rmarkdown::render() in a list.
You'd run this over multiple sets of parameters by various means in r, loops, the apply family (mapply in this case), dplyr::do, or the purrr:map() functions.  In this example, I use purrr::pmap().
Report.Rmd
---
title: "`r sprintf('mtcars %s vs. %s', params$variable1, params$variable2)`"
output: html_document
params:
  variable1: "mpg"
  variable2: "cyl"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(ggplot2)
```
#Plot
Crazy report text!

```{r cars}
ggplot(mtcars, aes_(x = as.name(params$variable1), y = as.name(params$variable2))) +
  geom_point()
```

Render Function
render_report <- function(var1, var2) {

  template <- "path_to/Report.Rmd"

  out_file <- sprintf("report %s vs. %s", var2, var1)

  parameters <- list(variable1 = var1,
                     variable2 = var2)

    rmarkdown::render(template,
                      output_file = out_file,
                      params = parameters)
  invisible(TRUE)
}

Running over multiple parameters.
library(purrr)

params_list <- list(list("mpg","mpg","mpg"),
                    list("drat","wt","qsec"))

pmap(params_list, render_report)

Output
Three html files, each named by the parameters, each with the scatterplot according to the parameters.

*

*drat vs mpg

*qsec vs mpg

*wt vs mpg


text:
Q: Codeigniter - Check if a value from array exists in database not working I have a table called user where I have a column called sub_teams. I have stored values in there like Construction,Teamwork,Website etc.
I have an announcement table called announcements where I have a body column and a sub_teams column.
All values in both sub_teams column in the users and the announcements has values stored separated by commas. What I want to achieve is that if an announcement has Construction,Website in one of the rows and the user has Website,Teamwork then he is able to view the announcement since he is in the Construction sub_team. Edit: But I want to display all rows of announcements that have the sub_teams not just one.
How can I achieve this? I have tried this:
    $uid = $this->session->userdata('uid');
    $this->db->select('sub_teams');
    $this->db->from('users');
    $this->db->where('uid', $uid);
    $sub_teams_query = $this->db->get();
    $sub_teams_result = $sub_teams_query->row();
    $sub_teams = explode(',', $sub_teams_result->sub_teams);

    $this->db->limit($limit, $start);
    $this->db->select('*');
    $this->db->from('announcements');
    $this->db->where('active', 1);
    $this->db->where('date_created <', date('Y-m-d H:m:s')); //hide the announcements that are new and not published yet.

    foreach ($sub_teams as $sub_team) {
        $this->db->or_like('view_sub_teams', $sub_team);
    }

    $this->db->join('users', 'users.uid = announcements.uid');
    $this->db->order_by('date_created', 'desc', 'after');
    $query = $this->db->get();

It seems to not work as it displays every single announcement. Thanks guys.

A: If im not wrong, you are fetching announcements for a particular user. Its returning all rows because in second query, you have missed where for userId condition
$this->db->where('uid', $uid);

Add the where condition in your second query.


text:
Q: Oracle SQL queries for subclass-table - Inheritance I have a Staff table with staffNo as the primary key
and staff details present in two other tables inheriting from it:  FTLecturer and PTLecturer. I used the Optional Or relationship there.
My question is: how to write Oracle SQL queries for these sub-classes?
I think I do not need a PK for them again because they inherit from `Staff'. Is it so?
This is the Staff table
CREATE TABLE Staff
(staffNo number(10) NOT NULL,
 firstName varchar2(50) NOT NULL,
 lastName number(2) ,
address varchar2(50) NOT NULL,
CONSTRAINT courseID_pk PRIMARY KEY (staffNo),
CONSTRAINT fk_Module
FOREIGN KEY (moduleID)
REFERENCES Module(Module Module ID)
);

I need to create the FTLecturer table with the Optional Or relationship.

A: Inheritance is a concept from Object-Oriented Programming: it has no meaning in relational databases.  The most important impact of this is that child tables do need a primary key.

"I need to create the FTLecturer table with the Optional Or relationship."

What you're describing is called an Arc in relational databases: a lecturer can be full-time or part-time but not both.  We can enforce this with a decent set of constraints.
First add a column to the parent table to identify the Lecturer type.
alter table staff add staffType varchar2(10);

This should be validated with a foreign key against a reference data table, and at a pinch a check constraint.  Then we add a unique constraint (yes, as well as a primary key):
alter table staff add constraint staff_uk unique(staffNo, staffType);

We can use this to enforce the arc on the child tables.
create table FTLecturer (
    staffNo number not null,
    staffType varchar2(10) not null,
    tenure varchar2(3) not null,
    constraint FTLecturer_pk primary key (staffNo),
    constraint FTLecturer_ck check (staffType = 'FTL'),
    constraint FTLecturer_Staff_fk foreign key (staffNo, staffType)
       references staff (staffNo, staffType);

Note that the foreign key means we can only insert rows in this table which have a parent of the correct type in the STAFF table.  This is why we need the StaffType column and that unique constraint on the STAFF table.
Likewise for Part-Time Lecturers:
create table PTLecturer (
    staffNo number not null,
    staffType varchar2(10) not null,
    hours number not null,
    constraint PTLecturer_pk primary key (staffNo),
    constraint PTLecturer_ck check (staffType = 'PTL'),
    constraint PTLecturer_Staff_fk foreign key (staffNo, staffType)
       references staff (staffNo, staffType);

Since Oracle 11g we can use a virtual column to apply a constant value for the staffType on the child tables.  This avoids the need for the check constraints.  Find out more.
To create a record for a specific member of staff it is good practice to populate the parent and child in the same action; we can use the INSERT ALL syntax to populate multiple tables.  Find out more.
Finally, you can build a helpful API with views.  For instance:
create or replace view fulltimeLecturer as
     select staff.*
            , ftl.tenure
     from staff
          join ftlecturer ftl
               on staff.staffno = ftl.staffno
                  and staff.stafftype = ftl.stafftype;

create or replace view parttimeLecturer as
     select staff.*
            , ptl.hours
     from staff
          join ptlecturer ptl
               on staff.staffno = ptl.staffno
                  and staff.stafftype = ptl.stafftype;

This may strike you as a lot of work, and inflexible to boot.  Here is the difference between Object Programming and Relational Databases.  OOP is primarily driven by helping developers to write code; RDBMS is focused on guaranteeing the integrity of the stored data.