问:
Make image overlap sections on tablet/desktop but not on mobile
I want my image to overlap two sections on tablet/desktop but not on mobile. Currently, it's doing so but it's causing the image to overlap the text in the first section.

I've tried adding "margin-top: -226px" to the image when the min-width is 768px (but again, it overlaps the text in the first/hero section).

```<code><!-- #HERO -->

  <div class="hero-bg center-text">
    <div class="center-container">
      <h2 class="join">
        <span class="join-the">Join The</span>
        <img class="shelf-squad-logo" src="https://shelfstuff.staging.thisismess.io/wp-content/plugins/readraterewards/static/img/logo-self-squad.svg">
      </h2>
      <h1 class="rate-read-rewards">
        <img class="read-rate-rewards-logo" src="https://shelfstuff.staging.thisismess.io/wp-content/plugins/readraterewards/static/img/logo-read-rate-review.svg">
      </h1>
      <p class="enter-for-a-chance">Enter for a chance to win a Shelf Squad Summer Reading prize pack!</p>
      <p class="read-or-listen-to-excerpt">
        Read or listen to an excerpt of any book below and submit a review for a chance to win. Not able to read or listen? Visit the official rules here on how to send in a write-in entry.
      </p>
    </div>
  </div>

<!-- #PRIZES -->

  <div class="prize-bg center-text">
    <div class="center-container">
      <img class="prize-pack" src="https://storage.googleapis.com/shelfstuff-staging/uploads/2019/05/210d080f-thumbnail-1.jpg">
      <p class="grand-prize">Grand Prize</p>
      <h2 class="grand-prize-desc">One grand prize winner every two weeks will receive a Shelf Squad prize pack including:</h2>
      <ul>
        <li class="prize-bullet">A set of amazing FREE books</li>
        <li class="prize-bullet">1 Shelf Stuff tote bag</li>
        <li class="prize-bullet">1 $50 gift card</li>
        <li class="prize-bullet">1 Shelf Stuff pin</li>
        <li class="prize-bullet">1 Shelf Stuff activity book</li>
      </ul>
    </div>
  </div>
</code>```

```<code>/*------------------------------------*\
  #UNIVERSAL
\*------------------------------------*/

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.center-container {
  max-width: 1220px;
  padding: 40px;
  margin: 0 auto;
}

img {
  max-width: 100%;
  height: auto;
}

ul {
  color: white;
  margin-top: 24px;
  font-weight: 700;
  font-size: 18px;
  line-height: 30px;
  list-style: none;
  text-align: left;
}

li {
  padding-left: 18px;
}

/*------------------------------------*\
  #HERO
\*------------------------------------*/

.hero-bg {
  background: blue;
}

.join-the {
  color: white;
  text-transform: uppercase;
  font-size: 21px;
  font-weight: 900;
  display: block;
}

.read-rate-rewards-logo {
  margin: 45px auto 40px auto;
}

.enter-for-a-chance {
  color: white;
  font-size: 36px;
  font-weight: 700;
  line-height: 48px;
  margin: 14px 0;
}

.read-or-listen-to-excerpt {
  color: white;
  font-weight: 700;
  font-size: 20px;
  line-height: 31px;
  max-width: 632px;
  margin: 1em auto;
}

/*------------------------------------*\
  #PRIZES
\*------------------------------------*/

.prize-bg {
  background: orange;
}

.grand-prize {
  color: var(--purple-color);
  text-transform: uppercase;
  margin: 33px auto;
  font-weight: 900;
  font-size: 24px;
}

.grand-prize-desc {
  color: white;
  font-weight: 700;
  font-size: 36px;
  line-height: 55px;
}

/*------------------------------------*\
  #UTILITY
\*------------------------------------*/

.center-text {
  text-align: center;
}

/*------------------------------------*\
  #RESPONSIVE
\*------------------------------------*/

@media only screen and (min-width: 768px) {
  .container {
    max-width: 1300px;
  }

  .prize-pack {
    margin-top: -226px;
  }
}
</code>```

On tablet/desktop, I would like there to be enough room in the first section to fit half of the image. Half of the image should be in the hero and half should be in the prizes section.

this is what I want it to look like

答:
Craig it's worth jumping into ... Bootstrap. Mobile first, 100% responsive framework. Master the grid!

Get Bootstrap

Body:
I want my image to overlap two sections on tablet/desktop but not on mobile. Currently, it's doing so but it's causing the image to overlap the text in the first section.

I've tried adding "margin-top: -226px" to the image when the min-width is 768px (but again, it overlaps the text in the first/hero section).

        Join The

Enter for a chance to win a Shelf Squad Summer Reading prize pack!

        Read or listen to an excerpt of any book below and submit a review for a chance to win. Not able to read or listen? Visit the official rules here on how to send in a write-in entry.

Grand Prize

One grand prize winner every two weeks will receive a Shelf Squad prize pack including:

A set of amazing FREE books

1 Shelf Stuff tote bag

1 $50 gift card

1 Shelf Stuff pin

1 Shelf Stuff activity book

/*------------------------------------*\
  #UNIVERSAL
\*------------------------------------*/

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.center-container {
  max-width: 1220px;
  padding: 40px;
  margin: 0 auto;
}

img {
  max-width: 100%;
  height: auto;
}

ul {
  color: white;
  margin-top: 24px;
  font-weight: 700;
  font-size: 18px;
  line-height: 30px;
  list-style: none;
  text-align: left;
}

li {
  padding-left: 18px;
}

/*------------------------------------*\
  #HERO
\*------------------------------------*/

.hero-bg {
  background: blue;
}

.join-the {
  color: white;
  text-transform: uppercase;
  font-size: 21px;
  font-weight: 900;
  display: block;
}

.read-rate-rewards-logo {
  margin: 45px auto 40px auto;
}

.enter-for-a-chance {
  color: white;
  font-size: 36px;
  font-weight: 700;
  line-height: 48px;
  margin: 14px 0;
}

.read-or-listen-to-excerpt {
  color: white;
  font-weight: 700;
  font-size: 20px;
  line-height: 31px;
  max-width: 632px;
  margin: 1em auto;
}

/*------------------------------------*\
  #PRIZES
\*------------------------------------*/

.prize-bg {
  background: orange;
}

.grand-prize {
  color: var(--purple-color);
  text-transform: uppercase;
  margin: 33px auto;
  font-weight: 900;
  font-size: 24px;
}

.grand-prize-desc {
  color: white;
  font-weight: 700;
  font-size: 36px;
  line-height: 55px;
}

/*------------------------------------*\
  #UTILITY
\*------------------------------------*/

.center-text {
  text-align: center;
}

/*------------------------------------*\
  #RESPONSIVE
\*------------------------------------*/

@media only screen and (min-width: 768px) {
  .container {
    max-width: 1300px;
  }

  .prize-pack {
    margin-top: -226px;
  }
}

On tablet/desktop, I would like there to be enough room in the first section to fit half of the image. Half of the image should be in the hero and half should be in the prizes section.

this is what I want it to look like


来源: stackexchange

问:
Find possible words that can be made from 7 random letters
I'm trying to build a program in Python that, given a list of words and a list of letters, can determine what words can be made with 7 random letters from the list of letters.

I have code that shows all possible words from the list that are possible with all the letters in the list, but I need to change it to choose 7 random letters from the list, then see which words from the list are possible to make with those chosen letters.

Here's the code that uses all letters and determines what words are possible with them:

```<code>from collections import Counter

words = ['hi','how','are','you']
letters = ['h','i','b', 'a','r','e', 'l', 'y', 'o', 'u', 'x', 'b']

# For every word
for word in words:

    # Convert the word into a dictionary
    dict = Counter(word)
    flag = 1

    # For every letter in that word
    for key in dict.keys():

        # If that letter is not in the list of available letters, set the flag to 0
        if key not in letters:
            flag = 0

    # If the flag remains 1 (meaning all letters in the word are in the letters list), then print the word.
    if flag == 1:
        print(word)
</code>```

Right now, it correctly prints the possible words with all the letters, but I want it to choose 7 random letters from the list and print what words are possible.

答:
Random.sample():

import random as rnd
letters = ['h','i','b', 'a','r','e', 'l', 'y', 'o', 'u', 'x', 'b']
hand = rnd.sample(letters, 7)
```

Then proceed as before.

ContentLicense: CC BY-SA 4.0

Body:
I'm trying to build a program in Python that, given a list of words and a list of letters, can determine what words can be made with 7 random letters from the list of letters.

I have code that shows all possible words from the list that are possible with all the letters in the list, but I need to change it to choose 7 random letters from the list, then see which words from the list are possible to make with those chosen letters.

Here's the code that uses all letters and determines what words are possible with them:

from collections import Counter

words = ['hi','how','are','you']
letters = ['h','i','b', 'a','r','e', 'l', 'y', 'o', 'u', 'x', 'b']

# For every word
for word in words:

    # Convert the word into a dictionary
    dict = Counter(word)
    flag = 1

    # For every letter in that word
    for key in dict.keys():

        # If that letter is not in the list of available letters, set the flag to 0
        if key not in letters:
            flag = 0

    # If the flag remains 1 (meaning all letters in the word are in the letters list), then print the word.
    if flag == 1:
        print(word)

Right now, it correctly prints the possible words with all the letters, but I want it to choose 7 random letters from the list and print what words are possible.

Body:
Random.sample():

import random as rnd
letters = ['h','i','b', 'a','r','e', 'l', 'y', 'o', 'u', 'x', 'b']
hand = rnd.sample(letters, 7)

Then proceed as before.


问:
Don't understand why print does not display results of hmget
I don't understand why a print does not display results of hmget in redis using python

You name it, I've tried it.

```<code>def newcode(R=r):
    cnt = 1
    for cnt in range(0,10):
        rec=R.hmget('rec-'+str(cnt), 'key' , 'txt')
        print(rec)
    cnt += 1
</code>```

Here is what is returned:

```<code>Pipeline<ConnectionPool<Connection<host=127.0.0.1,port=6379,db=0>>>
</code>```

I expected something like:  1 "This is the text" which would display the key and text values stored in the hash.

答:
i copy your method and i prove it and work fine

```<code>import redis
import json

def newcode(R):
    for cnt in range(0, 2):
        rec = R.hmget('rec-' + str(cnt), 'key', 'txt')
        print(rec)

conn = redis.Redis('localhost')

user = {'name': 'username','key': 25,'txt': 'football','response': 5}
meat = {'name': 'username','key': 22,'txt': 'basquetball','response': 5}

conn.hmset("rec-0", user)
conn.hmset("rec-1", meat)

newcode(conn)
</code>```

and the output was :

```<code>[b'25', b'football']
[b'22', b'basquetball']
</code>```

Body:
I don't understand why a print does not display results of hmget in redis using python

You name it, I've tried it.

def newcode(R=r):
    cnt = 1
    for cnt in range(0,10):
        rec=R.hmget('rec-'+str(cnt), 'key' , 'txt')
        print(rec)
    cnt += 1

Here is what is returned:

Pipeline<ConnectionPool<Connection<host=127.0.0.1,port=6379,db=0>>>

I expected something like:  1 "This is the text" which would display the key and text values stored in the hash.

Body:
i copy your method and i prove it and work fine

import redis
import json

def newcode(R):
    for cnt in range(0, 2):
        rec = R.hmget('rec-' + str(cnt), 'key', 'txt')
        print(rec)

conn = redis.Redis('localhost')

user = {'name': 'username','key': 25,'txt': 'football','response': 5}
meat = {'name': 'username','key': 22,'txt': 'basquetball','response': 5}

conn.hmset("rec-0", user)
conn.hmset("rec-1", meat)

newcode(conn)

and the output was :

[b'25', b'football']
[b'22', b'basquetball']


问:
How to turn a json object into individual rows
I have a table with a "rules" column that looks like this:

```<code>{"111_abc":0,"222_def":0,"333_abs":0}
</code>```

I'd like to write a postgres query to turn this into something like this:

```<code>rules
111_abc
222_def
333_abs
</code>```

I have tried several renditions of json_extract_path or json_array_element - no luck.

答:
Use the function jsonb_object_keys(), e.g.:

```<code>with my_table(rules) as (
values
    ('{"111_abc":0,"222_def":0,"333_abs":0}'::jsonb)
)

select jsonb_object_keys(rules) as rules
from my_table;

  rules
---------
 111_abc
 222_def
 333_abs
(3 rows)
</code>```

Body:
I have a table with a "rules" column that looks like this:

{"111_abc":0,"222_def":0,"333_abs":0}

I'd like to write a postgres query to turn this into something like this:

rules
111_abc
222_def
333_abs

I have tried several renditions of json_extract_path or json_array_element - no luck.

Body:
Use the function jsonb_object_keys(), e.g.:

with my_table(rules) as (
values
    ('{"111_abc":0,"222_def":0,"333_abs":0}'::jsonb)
)

select jsonb_object_keys(rules) as rules
from my_table;

  rules
---------
 111_abc
 222_def
 333_abs
(3 rows)


问:
How to add a known type to a List<T>?
The overall goal here is like this:  We have a lot of CSV files of various names and format stored in Azure blob storage.  We need to convert them to lists.

I have an interface:

```<code>public interface IGpasData
{
    List<T> ConvertToList<T>(StreamReader reader);
}
</code>```

And then here's an example of a class that Implements it:

```<code>public class GpasTableOfContent : IGpasData
{
    public string TocProp0 { get; set; }
    public string TocProp1 { get; set; }
    public string TocProp2 { get; set; }

    public List<T> ConvertToList<T>(StreamReader reader)
    {
        List<T> dataList = new List<T>();
        while (!reader.EndOfStream)
        {
            var lineItem = reader.ReadLine();
            GpasTableOfContent dataItem = new GpasTableOfContent
            {
                TocProp0 = lineItem.Split(',')[0],
                TocProp1 = lineItem.Split(',')[1],
                Type = lineItem.Split(',')[2]
            };
            dataList.Add(dataItem);
        }
        return dataList;
    }
}
</code>```

To keep going with the example of the class above, there is a file called ToC.csv.  In a class that is designed to convert THAT file into a list, I make this call:

```<code>List<GpasTableOfContent> gpasToCList = ConvertCloudFileToList<GpasTableOfContent>("ToC.csv", "MyModel");
</code>```

Some other possible examples:

```<code>List<GpasFoo> gpasFooList = ConvertCloudFileToList<GpasFoo>("foo.csv", "MyModel");

List<GpasBar> gpasBarList = ConvertCloudFileToList<GpasBar>("bar.csv", "MyModel");
</code>```

Here's ConvertCloudFileToList:

```<code>private List<T> ConvertCloudFileToList<T>(string fileName, string modelName)
    {
        // Get the .csv file from the InProgress Directory
        string filePath = $"{modelName}/{fileName}";
        CloudFile cloudFile = _inProgressDir.GetFileReference(filePath);

        List<T> dataList = new List<T>();

        // Does the file exist?
        if (!cloudFile.Exists())
            return dataList;

        using (StreamReader reader = new StreamReader(cloudFile.OpenRead()))
        {
            IGpasData gpasData = (IGpasData)Activator.CreateInstance<T>();
            dataList = gpasData.ConvertToList<T>(reader);
        }

        return dataList;
    }
</code>```

And that brings us back to ConvertToList.  The problem is here:

```<code>dataList.Add(dataItem);
</code>```

Can not convert 'GpasFoo' to 'T'

Not sure how to work around this.

答:
You can't do what you want here without providing some additional logic. The problem is that you have a string from reading the CSV file, and you want to convert it to a T, but there is no rule for converting a string into any arbitrary type.

One approach would be to change the method to also take a delegate Func that is used to convert each line into a T. Then if, for example, your data is guaranteed to consist of doubles, you could pass t => Double.Parse(t) for that argument. Of course, this approach requires that you change the signature of the interface method you are implementing.

If you are not able to change the signature of the interface method, then all I can suggest is trying to handle a pre-defined set of types and throwing an exception for other types.

Title: How to add a known type to a List<T>?

Body:
The overall goal here is like this:  We have a lot of CSV files of various names and format stored in Azure blob storage.  We need to convert them to lists.

I have an interface:

public interface IGpasData
{
    List<T> ConvertToList<T>(StreamReader reader);
}

And then here's an example of a class that Implements it:

public class GpasTableOfContent : IGpasData
{
    public string TocProp0 { get; set; }
    public string TocProp1 { get; set; }
    public string TocProp2 { get; set; }

    public List<T> ConvertToList<T>(StreamReader reader)
    {
        List<T> dataList = new List<T>();
        while (!reader.EndOfStream)
        {
            var lineItem = reader.ReadLine();
            GpasTableOfContent dataItem = new GpasTableOfContent
            {
                TocProp0 = lineItem.Split(',')[0],
                TocProp1 = lineItem.Split(',')[1],
                Type = lineItem.Split(',')[2]
            };
            dataList.Add(dataItem);
        }
        return dataList;
    }
}

To keep going with the example of the class above, there is a file called ToC.csv.  In a class that is designed to convert THAT file into a list, I make this call:

List<GpasTableOfContent> gpasToCList = ConvertCloudFileToList<GpasTableOfContent>("ToC.csv", "MyModel");

Some other possible examples:

List<GpasFoo> gpasFooList = ConvertCloudFileToList<GpasFoo>("foo.csv", "MyModel");

List<GpasBar> gpasBarList = ConvertCloudFileToList<GpasBar>("bar.csv", "MyModel");

Here's ConvertCloudFileToList:

private List<T> ConvertCloudFileToList<T>(string fileName, string modelName)
    {
        // Get the .csv file from the InProgress Directory
        string filePath = $"{modelName}/{fileName}";
        CloudFile cloudFile = _inProgressDir.GetFileReference(filePath);

        List<T> dataList = new List<T>();

        // Does the file exist?
        if (!cloudFile.Exists())
            return dataList;

        using (StreamReader reader = new StreamReader(cloudFile.OpenRead()))
        {
            IGpasData gpasData = (IGpasData)Activator.CreateInstance<T>();
            dataList = gpasData.ConvertToList<T>(reader);
        }

        return dataList;
    }

And that brings us back to ConvertToList.  The problem is here:

dataList.Add(dataItem);

Can not convert 'GpasFoo' to 'T'

Not sure how to work around this.


问:
how to insert more than 31000 characters into a varchar(64000) field in Teradata
I tried to insert more than 31000 characters using insert statement into a varchar(64000) field, but Teradata throws an error as - 3738: String is longer than 31000 characters.

insert into (1,'abc...….12312sdfsdf');--Second field is of length 31090

I can not change the field from varchar(64000) into clob. Please provide a solution to have a second field with 64000 characters.

答:
A single character literal cannot exceed 31000 characters, but a character expression or host variable (parameter) can be 64000 bytes. So you can split the text into two literals and concatenate in the INSERT:

```<code>INSERT INTO table VALUES(1,'first part'||'second part');
</code>```

The syntax and details for supplying host variables / parameters would depend on which client you are using.

Body:
A single character literal cannot exceed 31000 characters, but a character expression or host variable (parameter) can be 64000 bytes. So you can split the text into two literals and concatenate in the INSERT:

INSERT INTO table VALUES(1,'first part'||'second part');

The syntax and details for supplying host variables / parameters would depend on which client you are using.


问:
Differences clear function of gridview
How can I clear data in a GridView?  is appeared

```<code>     GridView1.DataSource = null;
     GridView1.DataBind();
</code>```

I want to Know difference of

```<code>     GridView1.Clear();
</code>```

two function of  GridView1.Clear()  and GridView1.DataSource = null.I have no error which clear  of syntax of GridView1.Clear().

```<code>Public Sub Clear()
    Data.Dispose()
    Data = New DataSet
    MyBase.DataSource = Nothing
End Sub
</code>```

I wrote new class of gridview  Inherits System.Windows.Forms.DataGridView.Vb class called  my clear function in its class.Sorry,gridview.Clear() is not have.

答:
If you want to delete the rows of a gridview
you can use this:
Gridview1.Rows.Clear();</p>

ContentLicense: CC BY-SA 3.0

Title: Differences clear function of gridview

Body:
How can I clear data in a GridView?  is appeared

     GridView1.DataSource = null;
     GridView1.DataBind();

I want to Know difference of

     GridView1.Clear();

two function of  GridView1.Clear()  and GridView1.DataSource = null.I have no error which clear  of syntax of GridView1.Clear().

Public Sub Clear()
    Data.Dispose()
    Data = New DataSet
    MyBase.DataSource = Nothing
End Sub

I wrote new class of gridview  Inherits System.Windows.Forms.DataGridView.Vb class called  my clear function in its class.Sorry,gridview.Clear() is not have.


问:
Can't understand &(sizeof 2[a])[a]
I've got some code that outputs bcdefg and I can't understand why.

I google searched but I couldn't find anything.

```<code>const char a[]="Abcdefg";
printf("%s",&(sizeof 2[a])[a]);
</code>```

Why is the output bcdefg, and not Abcdefg?

答:
If you break down each segment:

&(sizeof 2[a])[a]

2[a] = a[2].  a[2] = a char element at position 2

sizeof(char) = 1

(1)[a] = a[1]

&(a[1]) = a reference to the 'b' in "bcdefg"
```

printf() then prints the char array starting with b and ending at the null terminator

Title: Can't understand &(sizeof 2[a])[a]

Body:
I've got some code that outputs bcdefg and I can't understand why.

I google searched but I couldn't find anything.

const char a[]="Abcdefg";
printf("%s",&(sizeof 2[a])[a]);

Why is the output bcdefg, and not Abcdefg?

Body:
If you break down each segment:

&(sizeof 2[a])[a]

2[a] = a[2].  a[2] = a char element at position 2

sizeof(char) = 1

(1)[a] = a[1]

&(a[1]) = a reference to the 'b' in "bcdefg"

printf() then prints the char array starting with b and ending at the null terminator


问:
How can i reset my id counter to 1 in phpmyadmin?
So, I have a database with 10 users in it in which each one of them has an id from 1 to 10. If I delete those 10 users from the database and then create a new user, the new user will start with the id of 11 and not 1. Any Ideas?

答:
Instead of using the DELETE statment (which will keep your auto incrementing indexes, and start them from the last INSERTED index) use TRUNCATE

```<code>TRUNCATE TABLE database_name.table_name;
</code>```

TRUNCATE REFERENCE

Any AUTO_INCREMENT value is reset to its start value. This is true even for MyISAM and InnoDB, which normally do not reuse sequence values.

Body:
Instead of using the DELETE statment (which will keep your auto incrementing indexes, and start them from the last INSERTED index) use TRUNCATE

TRUNCATE TABLE database_name.table_name;

TRUNCATE REFERENCE

Any AUTO_INCREMENT value is reset to its start value. This is true even for MyISAM and InnoDB, which normally do not reuse sequence values.


问:
Is there a way to integrate S3 Bucket with Appsync without using an extra AWS Lambda for checking authorization?
My Stack:

AWS Appsync + AWS Lambda(to Mongodb) + custom OpenId-Connect Provider(also Lambda)

What I am trying to achieve

I would like to store some of the user data to the S3 bucket. I am hoping I can access the stored data directly from appsync, instead of calling a lambda for authorization checking every time I have to access the data.

There are mainly two types of information I want to store. For instance, userPicture and userSecret.

userPictures,that can be accessed by anyone.

As there are not access control checking, this part can be achieved by issuing a presignedUrl. No further authorization has to be done.

userSecret, that can be accessed by oneself and all admins

Here comes the part that I am trying to avoid calling another lambda just for authorization check. I already have the userId and role stored in the $context.identity, but still haven't figured out a way to actually perform the checking.

Is there a way I can avoid the lambda overhead?

答: AppSync recently launched support for multiple Authorization Providers. So for example you can secure your userSecret with an OpenID Connect provider and your userPictures by using an Api key. Does that satisfy your use case?</p>


问:
How to change the y-axis for a multivariate GAM model from smoothed to actual values?
I am using multivariate GAM models to learn more about fog trends in multiple regions. Fog is determined by visibility going below a certain threshold (< 400 meters). Our GAM model is used to determine the response of visibility to a range of meteorological variables.

However, my challenge right now is that I'd really like the  y-axis to be the actual visibility observations rather than the centered smoothed. It is interesting to see how visibility is impacted by the covariates relative to the mean visibility in that location, but it's difficult to compare this for multiple locations where the mean visibility is different (and thus the 0 point in which visibility is enhanced or diminished has little comparable meaning).

In order to compare the results of multiple locations, I'm trying to make the y-axis actual visibility observations, and then I'll put a line at the visibility threshold we're interested in looking at (400 m)
to evaluate what the predictor variables values are like below that threshold (eg what temperatures are associated with visibility below 400 m).

I'm still a beginner when it comes to GAMs and R in general, but I've figured out a few helpful pieces so far.

Helpful things so far:

Attempt 1. how to extract gam fit for each variable in model
Extracting data used to make a smooth plot in mgcv

Attempt 2. how to use predict function to reconstruct a univariable model
http://zevross.com/blog/2014/09/15/recreate-the-gam-partial-regression-smooth-plots-from-r-package-mgcv-with-a-little-style/

Attempt 3. how to get some semblance of a y-axis that looks like visibility observations using "fitted" -- though I don't think this is
the correct approach since I'm not taking the intercept into account
http://gsp.humboldt.edu/OLM/R/05_03_GAM.html

simulated data

```<code>install.packages("mgcv") #for gam package
require(mgcv)
install.packages("pspline")
require(pspline)

#simulated GAM data for example
dataSet <- gamSim(eg=1,n=400,dist="normal",scale=2)
visibility <- dataSet[[1]]
temperature <- dataSet[[2]]
dewpoint <- dataSet[[3]]
windspeed <- dataSet[[4]]

#Univariable GAM model
gamobj <- gam(visibility ~  s(dewpoint))
plot(gamobj, scale=0, page=1, shade = TRUE, all.terms=TRUE, cex.axis=1.5, cex.lab=1.5, main="Univariable Model: Dew Point")
summary(gamobj)
AIC(gamobj)
abline(h=0)
</code>```

Univariable Model of Dew Point
https://imgur.com/1uzP34F

ATTEMPT 2 -- predict function with univariable model, but didn't change y-axis

```<code>#dummy var that spans length of original covariate
maxDP <-max(dewpoint)
minDP <-min(dewpoint)
DPtrial.seq <-seq(minDP,maxDP,length=3071)
DPtrial.seq <-data.frame(dewpoint=DPtrial.seq)

#predict only the DP term
preds <- predict(gamobj, type="terms", newdata=DPtrial.seq, se.fit=TRUE)

#determine confidence intervals
DPplot <-DPtrial.seq$dewpoint
fit <-preds$fit
fit.up95 <-fit-1.96*preds$se.fit
fit.low95 <-fit+1.96*preds$se.fit

#plot
plot(DPplot, fit, lwd=3,
 main="Reconstructed Dew Point Covariate Plot")

#plot confident intervals
polygon(c(DPplot, rev(DPplot)),
    c(fit.low95,rev(fit.up95)), col="grey",
    border=NA)

lines(DPplot, fit,  lwd=2)
rug(dewpoint)
</code>```

Reconstructed Dew Point Covariate Plot
https://imgur.com/VS8QEcp

ATTEMPT 3 -- changed y-axis using "fitted" but without taking intercept into account

```<code>plot(dewpoint,fitted(gamobj), main="Fitted Response of Y (Visibility) Plotted Against Dew Point")
abline(h=mean(visibility))
rug(dewpoint)
</code>```

Fitted Response of Y Plotted Against Dew Point https://imgur.com/RO0q6Vw

Ultimately, I want a horizontal line where I can investigate the predictor variable relative to 400 meters, rather than just the mean of the response variable. This way, it will be comparable across multiple sites where the mean visibility is different. Most importantly, it needs to be for multiple covariates!

Gavin Simpson has explained the method in a couple of posts but unfortunately, I really don't understand how I would hold the mean of the other covariates constant as I use the predict function:

Changing the Y axis of default plot.gam graphs

Any deeper explanation into the method for doing this would be super helpful!!!

答:
I'm not sure how helpful this will be as your Q is a little more open ended than we'd typically like on SO, but, here goes.

Firstly, I think it would help to think about modelling the response variable, which I assume is currently visibility. This is going to be a continuous variable, bounded at 0 (perhaps the data never reach zero?) which suggests modelling the data as conditionally distributed either

gamma (family = Gamma(link = 'log')) for visibility that never takes a value of zero.

Tweedie (family = tw()) for data that do have zeroes.

An alternative approach would be to model the occurrence of fog; if this is defined as an event <400m visibility then you could turn all your observations into 0/1 values for being a fog event or otherwise. Then you'd model the data as conditionally distributed Bernoulli, using family = binomial().

Having decided on a modelling approach, we need to model the response. This should be done using a multiple regression type of approach, with a GAM including multiple predictors. This way you get to estimate the effect of each potential predictor variable on the response while controlling for the effects of the other predictors. If you just do this using a single predictor at a time, say dewpoint, that variable could well "explain" variation in the data that might be due to another predictor, windspeed say, and you wouldn't know it.

Furthermore, there may well be interactions between predictors that you'll want to control for if they exist, which can only be done in

Then, to finally get to the crux of your problem, having fitted the multi-predictor model to "explain" visibility, you will need to predict from the model for sets of likely conditions. To look at how the visibility varies with dewpoint in a model where other predictor variables have effects, you need to fix the other variables at some reasonable values; one option is to set them to their mean (or modal value in the case of any factor predictor variables), or some  other value indicative of typically values for that variable. You'll have to use your domain knowledge for this.

If you have interactions in the model, then you'll need to vary the two variables in the interaction, whilst holding all other variable fixed at some values.

Let's assume you don't have interactions and are interested in dewpoint but the model also includes windspeed. The mean windspeed for the values used to fit the model can be found from the cmX component of the fitted model. Of you could just calculate this from the observed windpseed values or set it to some known number you want to use. Denote the fitted by m, and the data frame with your data in it by df, then we can create new data to predict at over the range of dewpoint, whilst holding windspeed fixed.

```<code>mn.windspd <- m$cmX['windspeed']
## or
mn.windspd <- with(df, mean(windspeed))
## or set it some some value
mn.windspd <- 10 # say
</code>```

Then you can do

```<code>preddata <- with(df,
                 expand.grid(dewpoint = seq(min(dewpoint),
                                            max(dewpoint),
                                            length = 300),
                             windspeed = mn.windspd))
</code>```

Then you use this to predict from the fitted model:

```<code>pred <- predict(m, newdata = preddata, type = "link", se.fit = TRUE)
pred <- as.data.frame(pred)
</code>```

Now we want to put these predictions back on to the response scale, and we want a confidence interval so we have to create that first before back transforming:

```<code>ilink <- family(m)$linkinv
pred <- transform(pred,
                  Fitted = ilink(fit),
                  Upper  = ilink(fit + (2 * se.fit)),
                  Lower  = ilink(fit - (2 * se.fit)),
                  dewpoint = preddata = dewpoint)
</code>```

Now you can visualised the effect of dewpoint on the response whilst keeping windspeed fixed.

In your case, you will have to extend this to keeping temperature constant also, but that is done in the same way

```<code>mn.windspd <- m$cmX['windspeed']
mn.temp <- m$cmX['temperature']
preddata <- with(df,
                 expand.grid(dewpoint = seq(min(dewpoint),
                                            max(dewpoint),
                                            length = 300),
                             windspeed = mn.windspd,
                             temperature = mn.temp))
</code>```

and then follow the steps above to do the prediction.

For one or two variables varying I have a function data_slice() in my gratia package which will do the above expand.grid() stuff for you so you don't have to  specify the mean values of the other covariates:

```<code>preddata <- data_slice(m, 'dewpoint', n = 300)
</code>```

technically this finds the value in the data closest to the median value (for the covariates not varying). If you want means, then do

```<code>fixdf <- data.frame(windspeed = mn.windspd, temperature = mn.temp)
preddata <- data_slice(m, 'dewpoint', data = fixdf, n = 300)
</code>```

If you have an interaction, say between dewpoint and windspeed then you need to vary two variables. This is pretty easy again with expand.grid():

```<code>mn.temp <- m$cmX['temperature']
preddata <- with(df,
                 expand.grid(dewpoint = seq(min(dewpoint),
                                            max(dewpoint),
                                            length = 100),
                             windspeed = seq(min(windspeed),
                                             max(windspeed),
                                             length = 300),
                             temperature = mn.temp))
</code>```

This will create a 100 x 100 grid of values of the covariates to predict at, whilst holding temperature constant.

For data_slice() you'd need to do:

```<code>fixdf <- data.frame(temperature = mn.temp)
preddata <- data_slice(m, 'dewpoint', 'windpseed',
                       data = fixdf, n = 300)
</code>```

And extending this on to more covariates you want to vary, is also easy following this pattern with expand.grid(); I have yet to implement more than 2 variables varying in data_slice.

Body:
I am using multivariate GAM models to learn more about fog trends in multiple regions. Fog is determined by visibility going below a certain threshold (< 400 meters). Our GAM model is used to determine the response of visibility to a range of meteorological variables.

However, my challenge right now is that I'd really like the  y-axis to be the actual visibility observations rather than the centered smoothed. It is interesting to see how visibility is impacted by the covariates relative to the mean visibility in that location, but it's difficult to compare this for multiple locations where the mean visibility is different (and thus the 0 point in which visibility is enhanced or diminished has little comparable meaning).

In order to compare the results of multiple locations, I'm trying to make the y-axis actual visibility observations, and then I'll put a line at the visibility threshold we're interested in looking at (400 m)
to evaluate what the predictor variables values are like below that threshold (eg what temperatures are associated with visibility below 400 m).

I'm still a beginner when it comes to GAMs and R in general, but I've figured out a few helpful pieces so far.

Helpful things so far:

Attempt 1. how to extract gam fit for each variable in model
Extracting data used to make a smooth plot in mgcv

Attempt 2. how to use predict function to reconstruct a univariable model
http://zevross.com/blog/2014/09/15/recreate-the-gam-partial-regression-smooth-plots-from-r-package-mgcv-with-a-little-style/

Attempt 3. how to get some semblance of a y-axis that looks like visibility observations using "fitted" -- though I don't think this is
the correct approach since I'm not taking the intercept into account
http://gsp.humboldt.edu/OLM/R/05_03_GAM.html

simulated data

install.packages("mgcv") #for gam package
require(mgcv)
install.packages("pspline")
require(pspline)

#simulated GAM data for example
dataSet <- gamSim(eg=1,n=400,dist="normal",scale=2)
visibility <- dataSet[[1]]
temperature <- dataSet[[2]]
dewpoint <- dataSet[[3]]
windspeed <- dataSet[[4]]

#Univariable GAM model
gamobj <- gam(visibility ~  s(dewpoint))
plot(gamobj, scale=0, page=1, shade = TRUE, all.terms=TRUE, cex.axis=1.5, cex.lab=1.5, main="Univariable Model: Dew Point")
summary(gamobj)
AIC(gamobj)
abline(h=0)

Univariable Model of Dew Point
https://imgur.com/1uzP34F

ATTEMPT 2 -- predict function with univariable model, but didn't change y-axis

#dummy var that spans length of original covariate
maxDP <-max(dewpoint)
minDP <-min(dewpoint)
DPtrial.seq <-seq(minDP,maxDP,length=3071)
DPtrial.seq <-data.frame(dewpoint=DPtrial.seq)

#predict only the DP term
preds <- predict(gamobj, type="terms", newdata=DPtrial.seq, se.fit=TRUE)

#determine confidence intervals
DPplot <-DPtrial.seq$dewpoint
fit <-preds$fit
fit.up95 <-fit-1.96*preds$se.fit
fit.low95 <-fit+1.96*preds$se.fit

#plot
plot(DPplot, fit, lwd=3,
 main="Reconstructed Dew Point Covariate Plot")

#plot confident intervals
polygon(c(DPplot, rev(DPplot)),
    c(fit.low95,rev(fit.up95)), col="grey",
    border=NA)

lines(DPplot, fit,  lwd=2)
rug(dewpoint)

Reconstructed Dew Point Covariate Plot
https://imgur.com/VS8QEcp

ATTEMPT 3 -- changed y-axis using "fitted" but without taking intercept into account

plot(dewpoint,fitted(gamobj), main="Fitted Response of Y (Visibility) Plotted Against Dew Point")
abline(h=mean(visibility))
rug(dewpoint)

Fitted Response of Y Plotted Against Dew Point https://imgur.com/RO0q6Vw

Ultimately, I want a horizontal line where I can investigate the predictor variable relative to 400 meters, rather than just the mean of the response variable. This way, it will be comparable across multiple sites where the mean visibility is different. Most importantly, it needs to be for multiple covariates!

Gavin Simpson has explained the method in a couple of posts but unfortunately, I really don't understand how I would hold the mean of the other covariates constant as I use the predict function:

Changing the Y axis of default plot.gam graphs

Any deeper explanation into the method for doing this would be super helpful!!!

Body:
I'm not sure how helpful this will be as your Q is a little more open ended than we'd typically like on SO, but, here goes.

Firstly, I think it would help to think about modelling the response variable, which I assume is currently visibility. This is going to be a continuous variable, bounded at 0 (perhaps the data never reach zero?) which suggests modelling the data as conditionally distributed either

gamma (family = Gamma(link = 'log')) for visibility that never takes a value of zero.

Tweedie (family = tw()) for data that do have zeroes.

An alternative approach would be to model the occurrence of fog; if this is defined as an event <400m visibility then you could turn all your observations into 0/1 values for being a fog event or otherwise. Then you'd model the data as conditionally distributed Bernoulli, using family = binomial().

Having decided on a modelling approach, we need to model the response. This should be done using a multiple regression type of approach, with a GAM including multiple predictors. This way you get to estimate the effect of each potential predictor variable on the response while controlling for the effects of the other predictors. If you just do this using a single predictor at a time, say dewpoint, that variable could well "explain" variation in the data that might be due to another predictor, windspeed say, and you wouldn't know it.

Furthermore, there may well be interactions between predictors that you'll want to control for if they exist, which can only be done in

Then, to finally get to the crux of your problem, having fitted the multi-predictor model to "explain" visibility, you will need to predict from the model for sets of likely conditions. To look at how the visibility varies with dewpoint in a model where other predictor variables have effects, you need to fix the other variables at some reasonable values; one option is to set them to their mean (or modal value in the case of any factor predictor variables), or some  other value indicative of typically values for that variable. You'll have to use your domain knowledge for this.

If you have interactions in the model, then you'll need to vary the two variables in the interaction, whilst holding all other variable fixed at some values.

Let's assume you don't have interactions and are interested in dewpoint but the model also includes windspeed. The mean windspeed for the values used to fit the model can be found from the cmX component of the fitted model. Of you could just calculate this from the observed windpseed values or set it to some known number you want to use. Denote the fitted by m, and the data frame with your data in it by df, then we can create new data to predict at over the range of dewpoint, whilst holding windspeed fixed.

mn.windspd <- m$cmX['windspeed']
## or
mn.windspd <- with(df, mean(windspeed))
## or set it some some value
mn.windspd <- 10 # say

Then you can do

preddata <- with(df,
                 expand.grid(dewpoint = seq(min(dewpoint),
                                            max(dewpoint),
                                            length = 300),
                             windspeed = mn.windspd))

Then you use this to predict from the fitted model:

pred <- predict(m, newdata = preddata, type = "link", se.fit = TRUE)
pred <- as.data.frame(pred)

Now we want to put these predictions back on to the response scale, and we want a confidence interval so we have to create that first before back transforming:

ilink <- family(m)$linkinv
pred <- transform(pred,
                  Fitted = ilink(fit),
                  Upper  = ilink(fit + (2 * se.fit)),
                  Lower  = ilink(fit - (2 * se.fit)),
                  dewpoint = preddata = dewpoint)

Now you can visualised the effect of dewpoint on the response whilst keeping windspeed fixed.

In your case, you will have to extend this to keeping temperature constant also, but that is done in the same way

mn.windspd <- m$cmX['windspeed']
mn.temp <- m$cmX['temperature']
preddata <- with(df,
                 expand.grid(dewpoint = seq(min(dewpoint),
                                            max(dewpoint),
                                            length = 300),
                             windspeed = mn.windspd,
                             temperature = mn.temp))

and then follow the steps above to do the prediction.

For one or two variables varying I have a function data_slice() in my gratia package which will do the above expand.grid() stuff for you so you don't have to  specify the mean values of the other covariates:

preddata <- data_slice(m, 'dewpoint', n = 300)

technically this finds the value in the data closest to the median value (for the covariates not varying). If you want means, then do

fixdf <- data.frame(windspeed = mn.windspd, temperature = mn.temp)
preddata <- data_slice(m, 'dewpoint', data = fixdf, n = 300)

If you have an interaction, say between dewpoint and windspeed then you need to vary two variables. This is pretty easy again with expand.grid():

mn.temp <- m$cmX['temperature']
preddata <- with(df,
                 expand.grid(dewpoint = seq(min(dewpoint),
                                            max(dewpoint),
                                            length = 100),
                             windspeed = seq(min(windspeed),
                                             max(windspeed),
                                             length = 300),
                             temperature = mn.temp))

This will create a 100 x 100 grid of values of the covariates to predict at, whilst holding temperature constant.

For data_slice() you'd need to do:

fixdf <- data.frame(temperature = mn.temp)
preddata <- data_slice(m, 'dewpoint', 'windpseed',
                       data = fixdf, n = 300)

And extending this on to more covariates you want to vary, is also easy following this pattern with expand.grid(); I have yet to implement more than 2 variables varying in data_slice.


问:
How to display an image full size with ImageView?
I have problem when I try to display the image with full size.
I tried to display image with wrap_content. But it is smaller real image.

```<code><ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/screen1_logo" />
</code>```

I go to the properties of this image and see some information: dimentions 211 x 74. Then I try to display image with fixed width and height. I see it's bigger.

```<code><ImageView
        android:layout_width="211px"
        android:layout_height="74px"
        android:src="@drawable/screen1_logo" />
</code>```

is it right way? do we must to fix size to display image with full size?
Please help me to explain and resolve this problem.

答:
Try adding in xml file (ImageView properties) android:adjustViewBounds="true".</p>

```<code><ImageView
    android:id="@+id/photoViewer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
</code>```

Body:
I have problem when I try to display the image with full size.
I tried to display image with wrap_content. But it is smaller real image.

<ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/screen1_logo" />

I go to the properties of this image and see some information: dimentions 211 x 74. Then I try to display image with fixed width and height. I see it's bigger.

<ImageView
        android:layout_width="211px"
        android:layout_height="74px"
        android:src="@drawable/screen1_logo" />

is it right way? do we must to fix size to display image with full size?
Please help me to explain and resolve this problem.

Body:
Try adding in xml file (ImageView properties) android:adjustViewBounds="true".

<ImageView
    android:id="@+id/photoViewer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"


问:
Using R, if values in either variable A or B are 'NA', to then use values in variables C and D instead?
I am trying to calculate the mean of two variables and outputting it as a new variable. For example:

```<code>data$Average <- (data$Visit1 + data$Visit2) / 2
</code>```

However, there are NA values for both 'Visit 1' and 'Visit 2' variables across the dataset.

How can I write some code such that I am able to identify if NA values exist in either Visit 1 or 2, then I would use the corresponding row's values but for 'Visit 3' and 'Visit 4' instead, to calculate the average? And if 'Visit 3' and 'Visit 4' values were NA, to use 'Visit 5' and 'Visit 6' instead?

Basically, the priority is to use Visits 1 and 2 data, but if those do not exist (NA), to use   Visits 3 and 4, and finally, last priority being Visits 5 and 6.

Many thanks!

答:
Here's another option for your edited question using dplyr::case_when to filter through the possibilities:

```<code>library(dplyr)

data$Average <- case_when(!is.na(data$Visit1) & !is.na(data$Visit2) ~
                                        (data$Visit1 + data$Visit2) / 2,
                          !is.na(data$Visit3) & !is.na(data$Visit4) ~
                                        (data$Visit3 + data$Visit4) / 2,
                          TRUE ~ (data$Visit1 + data$Visit2) / 2)
</code>```

This is like ifelse but can handle more than 2 cases.

Body:
I am trying to calculate the mean of two variables and outputting it as a new variable. For example:

data$Average <- (data$Visit1 + data$Visit2) / 2

However, there are NA values for both 'Visit 1' and 'Visit 2' variables across the dataset.

How can I write some code such that I am able to identify if NA values exist in either Visit 1 or 2, then I would use the corresponding row's values but for 'Visit 3' and 'Visit 4' instead, to calculate the average? And if 'Visit 3' and 'Visit 4' values were NA, to use 'Visit 5' and 'Visit 6' instead?

Basically, the priority is to use Visits 1 and 2 data, but if those do not exist (NA), to use   Visits 3 and 4, and finally, last priority being Visits 5 and 6.

Many thanks!

Body:
Here's another option for your edited question using dplyr::case_when to filter through the possibilities:

library(dplyr)

data$Average <- case_when(!is.na(data$Visit1) & !is.na(data$Visit2) ~
                                        (data$Visit1 + data$Visit2) / 2,
                          !is.na(data$Visit3) & !is.na(data$Visit4) ~
                                        (data$Visit3 + data$Visit4) / 2,
                          TRUE ~ (data$Visit1 + data$Visit2) / 2)

This is like ifelse but can handle more than 2 cases.


问:
Async issues with geolocation and react-google-maps
I'm implementing google maps directions in a react component and am struggling using navigator.geolocation as the starting location. Warning: I'm rather new to React and Node.

I have two main methods which are invoked within ComponentDidMount, setStartLoc() which sets the starting location to the navigator.geolocation result (if the user permits, otherwise it defaults to just a hardcoded location), and GetDirections() which queries the maps api for directions from the start-location to the destination-location (function arguments).

componentDidMount() {
    //get geoLocation and print
    const startLoc = this.setStartLoc();
    console.log(startLoc);

    //set destination
    const destinationLoc =  "50.927884,  -1.299285";

    //get directions once start location returned
    startLoc.then(function(startLoc){
      this.getDirections(startLoc,destinationLoc).bind(this)
    })
  }
```

<p>I've had issues with the asynchronous <strong>getDirections</strong> function, querying for directions before the user has a chance to permit their geolocation which results in an error returned from the api.</p>

<p>I've tried forcing <strong>getDirections</strong> to wait by implementing <strong>setStartLoc</strong> as a promise and using <em>.then</em>:</p>

```  setStartLoc = function() {
    return new Promise(function(resolve,reject){
        if (navigator && navigator.geolocation) {
            console.log("in navigator");
            //get browser geolocation:
            navigator.geolocation.getCurrentPosition(pos => {
                resolve(pos.coords.latitude + ", " + pos.coords.longitude);
            });
        }
        else{
            //hardcoded alternative start location
            resolve("50.927044, -1.299964");
        }
    })
  }
```

<p>As far as im aware this works in forcing <strong>getDirections</strong> to wait but it does result in the following typeError.</p>

<p>"TypeError: <em>this</em> is undefined"</p>

<p>Where <em>this</em> is referring to the following code in <strong>componentDidMount</strong>.</p>

```  this.getDirections(startLoc,destinationLoc).bind(this)
```

I've naturally tried adding/removing the bind but the same error occurs.

Could anyone please help with this error either by correcting the shown code or by suggesting an alternative method to making the getDirection() function wait to query once the setStartLoc() has returned?

The getDirections method and the rest of the component code was taken from this guide where full code for the component can be seen here - to which I've added setStartLoc and changed componentDidMount as shown.

Thank you in advance for any help!

Max

答:
You were on the right track by promisifying the setStartLoc function, but, to get around your scoping issue, @Jaromanda X is absolutely right, you'll need an arrow function here.

With arrow functions, you could do something like this in your componentDidMount:

```<code>startLoc.then(startLoc => {
   this.getDirections(startLoc, destinationLoc);
});
</code>```

Where startLoc is defined something like this:

```<code>setStartLoc = () => {
    return new Promise(function(resolve, reject) {
      if (navigator && navigator.geolocation) {
        console.log("in navigator");
        //get browser geolocation:
        return navigator.geolocation.getCurrentPosition(
          pos => {
            return resolve(pos.coords.latitude + ", " + pos.coords.longitude);
          },
          err => resolve("50.927044, -1.299964") // if user refuses access, return with default
        );
      }
      return resolve("50.927044, -1.299964");
    });
  };

</code>```

For completeness, here is a codesanbox

Body:
I'm implementing google maps directions in a react component and am struggling using navigator.geolocation as the starting location. Warning: I'm rather new to React and Node.

I have two main methods which are invoked within ComponentDidMount, setStartLoc() which sets the starting location to the navigator.geolocation result (if the user permits, otherwise it defaults to just a hardcoded location), and GetDirections() which queries the maps api for directions from the start-location to the destination-location (function arguments).

componentDidMount() {
    //get geoLocation and print
    const startLoc = this.setStartLoc();
    console.log(startLoc);

    //set destination
    const destinationLoc =  "50.927884,  -1.299285";

    //get directions once start location returned
    startLoc.then(function(startLoc){
      this.getDirections(startLoc,destinationLoc).bind(this)
    })
  }

I've had issues with the asynchronous getDirections function, querying for directions before the user has a chance to permit their geolocation which results in an error returned from the api.

I've tried forcing getDirections to wait by implementing setStartLoc as a promise and using .then:

  setStartLoc = function() {
    return new Promise(function(resolve,reject){
        if (navigator && navigator.geolocation) {
            console.log("in navigator");
            //get browser geolocation:
            navigator.geolocation.getCurrentPosition(pos => {
                resolve(pos.coords.latitude + ", " + pos.coords.longitude);
            });
        }
        else{
            //hardcoded alternative start location
            resolve("50.927044, -1.299964");
        }
    })
  }

As far as im aware this works in forcing getDirections to wait but it does result in the following typeError.

"TypeError: this is undefined"

Where this is referring to the following code in componentDidMount.

  this.getDirections(startLoc,destinationLoc).bind(this)

I've naturally tried adding/removing the bind but the same error occurs.

Could anyone please help with this error either by correcting the shown code or by suggesting an alternative method to making the getDirection() function wait to query once the setStartLoc() has returned?

The getDirections method and the rest of the component code was taken from this guide where full code for the component can be seen here - to which I've added setStartLoc and changed componentDidMount as shown.

Thank you in advance for any help!

Max

Body:
You were on the right track by promisifying the setStartLoc function, but, to get around your scoping issue, @Jaromanda X is absolutely right, you'll need an arrow function here.

With arrow functions, you could do something like this in your componentDidMount:

startLoc.then(startLoc => {
   this.getDirections(startLoc, destinationLoc);
});

Where startLoc is defined something like this:

setStartLoc = () => {
    return new Promise(function(resolve, reject) {
      if (navigator && navigator.geolocation) {
        console.log("in navigator");
        //get browser geolocation:
        return navigator.geolocation.getCurrentPosition(
          pos => {
            return resolve(pos.coords.latitude + ", " + pos.coords.longitude);
          },
          err => resolve("50.927044, -1.299964") // if user refuses access, return with default
        );
      }
      return resolve("50.927044, -1.299964");
    });
  };

For completeness, here is a codesanbox


问:
AVAudioRecorder Play the Recorded Item While Recording
Is there a way I can play the file that is BEING RECORDED during the recording? That is, when the user plugs in an earphone with mic, he is able to hear his own voice through the earphone as he speaks into the mic, which means there is no looping to be worried about.

P.S. If AVAudioRecorder cannot achieve this, is there any way of doing this with AudioKit? If so, please tell me how.

答:
My guess is you would have to use an AVCaptureSession to do this.

You can grab input from AVCaptureDeviceInput.

And then you can use AVCaptureOutputAudioDataOutput, which provides access to audio sample buffers as they are recorded.

```<code>extension RecordingViewController: AVCaptureAudioDataOutputSampleBufferDelegate {
    func captureOutput(_ output: AVCaptureOutput,
                       didOutput sampleBuffer: CMSampleBuffer,
                       from connection: AVCaptureConnection) {
        output.connection(with: AVMediaType(rawValue: AVAudioSessionPortBuiltInSpeaker))
    }
}
</code>```

Edit: It might be simpler and cleaner to implement this with AudioKit, however. Code would be something like this:

```<code>let microphone = AKMicrophone()
let mixer = AKMixer(microphone)
let booster = AKBooster(mixer, gain: 0)
AudioKit.output = booster

microphone.start()
do {
    try AudioKit.start()
} catch {
    print("AudioKit boot failed.")
}
</code>```

Body:
My guess is you would have to use an AVCaptureSession to do this.

You can grab input from AVCaptureDeviceInput.

And then you can use AVCaptureOutputAudioDataOutput, which provides access to audio sample buffers as they are recorded.

extension RecordingViewController: AVCaptureAudioDataOutputSampleBufferDelegate {
    func captureOutput(_ output: AVCaptureOutput,
                       didOutput sampleBuffer: CMSampleBuffer,
                       from connection: AVCaptureConnection) {
        output.connection(with: AVMediaType(rawValue: AVAudioSessionPortBuiltInSpeaker))
    }
}

Edit: It might be simpler and cleaner to implement this with AudioKit, however. Code would be something like this:

let microphone = AKMicrophone()
let mixer = AKMixer(microphone)
let booster = AKBooster(mixer, gain: 0)
AudioKit.output = booster

microphone.start()
do {
    try AudioKit.start()
} catch {
    print("AudioKit boot failed.")
}


答:
A nice way to do multiple ifelse statements is with case_when

```<code>library(dplyr)

dat <- dat %>%
   mutate(
     average = case_when(
     !is.na(Visit1) & !is.na(Visit2) ~ (Visit1 + Visit2)/2,
     !is.na(Visit3) & !is.na(Visit4) ~ (Visit3 + Visit4)/2,
     !is.na(Visit5) & !is.na(Visit6) ~ (Visit5 + Visit6)/2,
     TRUE ~ NA
     )
   )
</code>```

The advantages to this approach are: a) uses tidyverse and applicable to many cases of multiple ifelse statements, b) is easy to understand when you come back to the code later, c) use NA if some rows do not satisfy your conditions (i.e., TRUE ~ NA)

More documentation on case_when is here: https://dplyr.tidyverse.org/reference/case_when.html

Title: Using R, if values in either variable A or B are 'NA', to then use values in variables C and D instead?

Body:
A nice way to do multiple ifelse statements is with case_when

library(dplyr)

dat <- dat %>%
   mutate(
     average = case_when(
     !is.na(Visit1) & !is.na(Visit2) ~ (Visit1 + Visit2)/2,
     !is.na(Visit3) & !is.na(Visit4) ~ (Visit3 + Visit4)/2,
     !is.na(Visit5) & !is.na(Visit6) ~ (Visit5 + Visit6)/2,
     TRUE ~ NA
     )
   )

The advantages to this approach are: a) uses tidyverse and applicable to many cases of multiple ifelse statements, b) is easy to understand when you come back to the code later, c) use NA if some rows do not satisfy your conditions (i.e., TRUE ~ NA)

More documentation on case_when is here: https://dplyr.tidyverse.org/reference/case_when.html


问:
AWS CLI: disable distribution
As far as I have understood, disabling a cloudfront distribution means updating it's status and is necessary to be able to delete it.

Given the very sparse documentation of the AWS CLI, I am looking for a minimal example of how to do that update using just the CLI.

答:
Here is entire script for disabling distribution automatically:

```<code>    id=E234343434

    tmpfile=$(mktemp /tmp/script.XXXXXX)
    tmpfile2=$(mktemp /tmp/script.XXXXXX)
    aws cloudfront get-distribution-config --id $id | \
       jq .DistributionConfig.Enabled=false > $tmpfile
    jq -r .DistributionConfig $tmpfile > $tmpfile2
    aws cloudfront update-distribution --id $id \
        --if-match $(jq .ETag $tmpfile -r) \
        --distribution-config file://$tmpfile2
    rm $tmpfile $tmpfile2
</code>```

and to delete:

```<code>aws cloudfront delete-distribution --id $id --if-match \
  $(aws cloudfront get-distribution-config --id $id | jq .ETag -r)
</code>```

Title: AWS CLI: disable distribution

Body:
Here is entire script for disabling distribution automatically:

    id=E234343434

    tmpfile=$(mktemp /tmp/script.XXXXXX)
    tmpfile2=$(mktemp /tmp/script.XXXXXX)
    aws cloudfront get-distribution-config --id $id | \
       jq .DistributionConfig.Enabled=false > $tmpfile
    jq -r .DistributionConfig $tmpfile > $tmpfile2
    aws cloudfront update-distribution --id $id \
        --if-match $(jq .ETag $tmpfile -r) \
        --distribution-config file://$tmpfile2
    rm $tmpfile $tmpfile2

and to delete:

aws cloudfront delete-distribution --id $id --if-match \
  $(aws cloudfront get-distribution-config --id $id | jq .ETag -r)


问:
Web3J to generate Java wrapper automatically
I am trying to generate the wrapper on fly using web3j and invoking a method on the generated class using reflection. But I am getting classNotFound exception on the first try. (When I stop the server and re-run, it works because the class was already present)

Does java support generating class on fly (when server is running) ?

```<code> private void createContractClass(String contractFileNameWithoutExtension) {
        try {
            String command = "web3j solidity generate -b " +
                    contractLocation+contractFileNameWithoutExtension+".bin  -a "+contractLocation+contractFileNameWithoutExtension+".abi" +
                    " -o "+sourceCodeLocation+" -p generated";
            LOG.info("Executing {}", command);
            Process p = Runtime.getRuntime().exec(command);
            int exitCode = p.waitFor();
            if(exitCode != 0) {
                LOG.error("Error {}", p.getOutputStream());
            }
        } catch(IOException | InterruptedException ex) {
            ex.printStackTrace();
            throw new FormatException(ValidationMessages.FAILED_TO_DEPLOY_CONTRACT);
        }
    }

 private String invokeDeployment(String password, String walletFileName, String contractFileName) {
        try {
            Credentials credentials = WalletUtils.loadCredentials(password, walletLocation + "/" + walletFileName);
            Class classz = Class.forName("generated."+ StringUtils.capitalize(contractFileName));
            Method method = classz.getDeclaredMethod("deploy", Web3j.class, Credentials.class, BigInteger.class, BigInteger.class);

            RemoteCall<?> invoke = (RemoteCall<?>)method.invoke(classz, web3JClient.getClient(), credentials, DefaultGasProvider.GAS_PRICE, DefaultGasProvider.GAS_LIMIT);
            Contract contract = (Contract)invoke.send();
            return contract.getContractAddress();
        } catch (Exception e){
            e.printStackTrace();
            throw new FormatException(ValidationMessages.FAILED_TO_DEPLOY_CONTRACT);
        }
    }
</code>```

答: It was because, the class was not loaded in the class path. Compile and load the class to the class path resolved the issue.</p>

Body:
I am trying to generate the wrapper on fly using web3j and invoking a method on the generated class using reflection. But I am getting classNotFound exception on the first try. (When I stop the server and re-run, it works because the class was already present)

Does java support generating class on fly (when server is running) ?

 private void createContractClass(String contractFileNameWithoutExtension) {
        try {
            String command = "web3j solidity generate -b " +
                    contractLocation+contractFileNameWithoutExtension+".bin  -a "+contractLocation+contractFileNameWithoutExtension+".abi" +
                    " -o "+sourceCodeLocation+" -p generated";
            LOG.info("Executing {}", command);
            Process p = Runtime.getRuntime().exec(command);
            int exitCode = p.waitFor();
            if(exitCode != 0) {
                LOG.error("Error {}", p.getOutputStream());
            }
        } catch(IOException | InterruptedException ex) {
            ex.printStackTrace();
            throw new FormatException(ValidationMessages.FAILED_TO_DEPLOY_CONTRACT);
        }
    }

 private String invokeDeployment(String password, String walletFileName, String contractFileName) {
        try {
            Credentials credentials = WalletUtils.loadCredentials(password, walletLocation + "/" + walletFileName);
            Class classz = Class.forName("generated."+ StringUtils.capitalize(contractFileName));
            Method method = classz.getDeclaredMethod("deploy", Web3j.class, Credentials.class, BigInteger.class, BigInteger.class);

            RemoteCall<?> invoke = (RemoteCall<?>)method.invoke(classz, web3JClient.getClient(), credentials, DefaultGasProvider.GAS_PRICE, DefaultGasProvider.GAS_LIMIT);
            Contract contract = (Contract)invoke.send();
            return contract.getContractAddress();
        } catch (Exception e){
            e.printStackTrace();
            throw new FormatException(ValidationMessages.FAILED_TO_DEPLOY_CONTRACT);
        }
    }


问:
Create an intermediary commit in history
I've been tasked with replicating our centralized VCS solution onto a git repository. My plan is to use a Jenkins pipeline to absorb commits from the CVCS, pull latest from git, then commit those changes onto git and push.

The goal is to capture every commit as it comes in, for which I'm using a webhook from the CVCS that passes relevant information to the pipeline to identify the particular commit in history. For the purposes of this question, assume all development is done in one unending line with no branches.

My concern is the asynchronous nature of a webhook call. If I have commit 1000 synced to git, then commits 1001 and 1002 both come in nearly simultaneously, the Jenkins pipeline might run 1002 before 1001. That in itself isn't a huge problem (I intend to include a text file on the git repo to keep track of the last synced CVCS commit), but it would be nice if I could handle 1001 in a more intelligent way than to discard it.

Is there any way to create an intermediary commit in git history? Something like:

```<code>git checkout HEAD~1
git checkout -b some_temp_branch
git commit -m "This is commit 1001"
git checkout master
git merge some_temp_branch -Xtheirs         # or something?
</code>```

where the resulting history goes from:

```<code>1002
 |
1000
</code>```

to

```<code>1002
 |
1001
 |
1000
</code>```

with no change in the working directory?

答:
Whether you are converting to Git, or want to create a mirror of your repository in Git, these tools already exist. How well developed they are depends on the VCS. For example, git-svn is very well developed; Git can act as a two-way mirror of Subversion. Others are available online, look for <vcs name>2git. For example, if you're using CVS you'd look for cvs2git or git cvsimport.

If a Git migration tool does not already exist for your VCS you can build one by turning your commits into a format suitable for git fast-import. Rather than involving Jenkins you would use the normal VCS client to retrieve the latest commits; there would then be no order issue.

If the intention is to convert to Git I would recommend inverting your process. Which approach is best depends on your situation, but generally it's better to use Git as the leader repository and your old VCS as the follower. Git is very flexible and powerful. It can adapt to how you were using your old VCS, but your old VCS cannot adapt to Git. Making Git the follower restricts you to using version control the same as you are now; what's the point of converting?

If Git is the leader, Git can act in a centralized mode as you're used to. Be sure to only commit to master and always put your local commits on the tip of master using git pull --rebase before pushing. Meanwhile you can explore Git's new features; for example, feature branches. If it's necessary to keep the old VCS around create a read-only mirror by mirroring commits to the master branch into the old VCS.

Body:
I've been tasked with replicating our centralized VCS solution onto a git repository. My plan is to use a Jenkins pipeline to absorb commits from the CVCS, pull latest from git, then commit those changes onto git and push.

The goal is to capture every commit as it comes in, for which I'm using a webhook from the CVCS that passes relevant information to the pipeline to identify the particular commit in history. For the purposes of this question, assume all development is done in one unending line with no branches.

My concern is the asynchronous nature of a webhook call. If I have commit 1000 synced to git, then commits 1001 and 1002 both come in nearly simultaneously, the Jenkins pipeline might run 1002 before 1001. That in itself isn't a huge problem (I intend to include a text file on the git repo to keep track of the last synced CVCS commit), but it would be nice if I could handle 1001 in a more intelligent way than to discard it.

Is there any way to create an intermediary commit in git history? Something like:

git checkout HEAD~1
git checkout -b some_temp_branch
git commit -m "This is commit 1001"
git checkout master
git merge some_temp_branch -Xtheirs         # or something?

where the resulting history goes from:

1002
 |
1000

to

1002
 |
1001
 |
1000

with no change in the working directory?


问:
JavaScript key undefined in IE over SSL
I'm working on a web app that works fine inside the LAN using HTTP but when I test it remotely over SSL it fails for IE (7 & 8). Firefox, Camino and Safari all work perfectly.

I've tracked down the issue to key being undefined. The relevant bit of code is:

```<code>function showResult(e,str,num) {
    var key = (window.Event) ? e.which : e.keyCode;

    if(((key > 32 && key < 58) || (key > 64 && key < 91) || (key > 95 && key < 123)) && (str.length >= num)) {
</code>```

Any clues as to why key would be undefined for IE over SSL but works fine over HTTP? Better yet, can someone tell me how to overcome this problem? FWIW, I don't need to support IE prior to version 7.

Update:

There was an answer which suggested replacing

```<code>var key = (window.event) ? e.which : e.keyCode;
</code>```

with

```<code>var key;
e = e || window.event;
key = e.keyCode || e.which;
</code>```

That works. The problem now is that I can't accept that answer because it has been deleted.

答:
Ok, I'll re-add that answer, even though it wasn't mine, and add some info along the way so you can feel OK with accepting it even if it wasn't my answer from the beginning.

So, the suggested code is this:

```<code>var key;
e = e || window.event;
key = e.keyCode || e.which;
</code>```

What does || do? For one it's the logical or operator, which would return true if one of the sides evaluate to boolean true.

It also has another use in JS. If you give it two arguments and the first one is undefined, it will return the second argument. This means, in the code above, that if e is undefined, you instead get window.event which is IE's traditional event object.

Same thing goes with e.keyCode || e.which whichever one exists is used. So in the end, you're likely to end up with a valid key code on various browsers. All is good in wonderland.

But wait, doesn't your original code do something similar?

```<code>var key = (window.Event) ? e.which : e.keyCode;
</code>```

Uh-huh. What's that? JavaScript is case-sensisitive, so window.Event is not the same as  window.event in the code above. window.event is IE's traditional event object that you would use get information about the event that occurred, while window.Event (which you can see from the initial capital letter) is a constructor, or more specifically in this case an interface.

Point is, in that code it was used to detect Mozilla. If it exists, choose e.which (One of the places where Mozilla stores the key code) otherwise go for e.keyCode where IE would store the key code.

However, this is based on the flawed assumption that IE doesn't have the window.Event constructor defined. It does have it defined as of IE8, at least. This means that e.which is chosen over e.keyCode, on newer versions of IE. e.which has never and will never be supported in IE. That's why key ends up being undefined.

But, uhm, why does it differ between encrypted and unencrypted connections? That's a good question. While I cannot know for sure without access to your development environment, I would assume it has something with IE's compatibility modes.

IE has historically (over the last 10 years) been the most quirky and non-standard browser around. This has lead to people a) programming ignorantly according to IE's standards b) creating workarounds for IE's behaviors. If MS just made IE standards-compliant, that would break many pages which rely on IE's quirky behavior in one way or the other. Microsoft has ackowledged this by making IE8+ emulate older versions of IE so pages won't break, unless told otherwise.

I can only assume that, for whatever reason, in your test environment the page ends up running in the "IE7" mode which might not have the window.Event constructor/interface defined. This would make your old code use e.keyCode which is ey OK. Then perhaps in your production environment, or maybe just because of the encrypted connection (only ghawd knows what MS is doing) you end up with a newer IE mode so window.Event is actually defined and e.which is chosen. This makes IE a cheeky monkey.

Bottom line: Use the new code.

Title: JavaScript key undefined in IE over SSL

Body:
I'm working on a web app that works fine inside the LAN using HTTP but when I test it remotely over SSL it fails for IE (7 & 8). Firefox, Camino and Safari all work perfectly.

I've tracked down the issue to key being undefined. The relevant bit of code is:

function showResult(e,str,num) {
    var key = (window.Event) ? e.which : e.keyCode;

    if(((key > 32 && key < 58) || (key > 64 && key < 91) || (key > 95 && key < 123)) && (str.length >= num)) {

Any clues as to why key would be undefined for IE over SSL but works fine over HTTP? Better yet, can someone tell me how to overcome this problem? FWIW, I don't need to support IE prior to version 7.

Update:

There was an answer which suggested replacing

var key = (window.event) ? e.which : e.keyCode;

with

var key;
e = e || window.event;
key = e.keyCode || e.which;

That works. The problem now is that I can't accept that answer because it has been deleted.

Body:
Ok, I'll re-add that answer, even though it wasn't mine, and add some info along the way so you can feel OK with accepting it even if it wasn't my answer from the beginning.

So, the suggested code is this:

var key;
e = e || window.event;
key = e.keyCode || e.which;

What does || do? For one it's the logical or operator, which would return true if one of the sides evaluate to boolean true.

It also has another use in JS. If you give it two arguments and the first one is undefined, it will return the second argument. This means, in the code above, that if e is undefined, you instead get window.event which is IE's traditional event object.

Same thing goes with e.keyCode || e.which whichever one exists is used. So in the end, you're likely to end up with a valid key code on various browsers. All is good in wonderland.

But wait, doesn't your original code do something similar?

var key = (window.Event) ? e.which : e.keyCode;

Uh-huh. What's that? JavaScript is case-sensisitive, so window.Event is not the same as  window.event in the code above. window.event is IE's traditional event object that you would use get information about the event that occurred, while window.Event (which you can see from the initial capital letter) is a constructor, or more specifically in this case an interface.

Point is, in that code it was used to detect Mozilla. If it exists, choose e.which (One of the places where Mozilla stores the key code) otherwise go for e.keyCode where IE would store the key code.

However, this is based on the flawed assumption that IE doesn't have the window.Event constructor defined. It does have it defined as of IE8, at least. This means that e.which is chosen over e.keyCode, on newer versions of IE. e.which has never and will never be supported in IE. That's why key ends up being undefined.

But, uhm, why does it differ between encrypted and unencrypted connections? That's a good question. While I cannot know for sure without access to your development environment, I would assume it has something with IE's compatibility modes.

IE has historically (over the last 10 years) been the most quirky and non-standard browser around. This has lead to people a) programming ignorantly according to IE's standards b) creating workarounds for IE's behaviors. If MS just made IE standards-compliant, that would break many pages which rely on IE's quirky behavior in one way or the other. Microsoft has ackowledged this by making IE8+ emulate older versions of IE so pages won't break, unless told otherwise.

I can only assume that, for whatever reason, in your test environment the page ends up running in the "IE7" mode which might not have the window.Event constructor/interface defined. This would make your old code use e.keyCode which is ey OK. Then perhaps in your production environment, or maybe just because of the encrypted connection (only ghawd knows what MS is doing) you end up with a newer IE mode so window.Event is actually defined and e.which is chosen. This makes IE a cheeky monkey.

Bottom line: Use the new code.


问:
Find possible words that can be made from 7 random letters
I'm trying to build a program in Python that, given a list of words and a list of letters, can determine what words can be made with 7 random letters from the list of letters.

I have code that shows all possible words from the list that are possible with all the letters in the list, but I need to change it to choose 7 random letters from the list, then see which words from the list are possible to make with those chosen letters.

Here's the code that uses all letters and determines what words are possible with them:

```<code>from collections import Counter

words = ['hi','how','are','you']
letters = ['h','i','b', 'a','r','e', 'l', 'y', 'o', 'u', 'x', 'b']

# For every word
for word in words:

    # Convert the word into a dictionary
    dict = Counter(word)
    flag = 1

    # For every letter in that word
    for key in dict.keys():

        # If that letter is not in the list of available letters, set the flag to 0
        if key not in letters:
            flag = 0

    # If the flag remains 1 (meaning all letters in the word are in the letters list), then print the word.
    if flag == 1:
        print(word)
</code>```

Right now, it correctly prints the possible words with all the letters, but I want it to choose 7 random letters from the list and print what words are possible.

答:
You can use Counter from collection to check for the ability to make each word out of the letters:

```<code>words   = ['hi','how','are','you']
letters = ['h','i','b', 'a','r','e', 'l', 'y', 'o', 'u', 'x', 'b']

from random      import sample
from collections import Counter

letterCounts  = Counter(sevenLetters)
wordCounts    = { word:Counter(word) for word in words }

sevenLetters  = sample(letters,7)
possibleWords = [ word for word,wc in wordCounts.items() if not wc-letterCounts ]

print(sevenLetters,possibleWords)
# ['x', 'u', 'h', 'b', 'a', 'i', 'b'] ['hi']
</code>```

Note: using Counter() covers the cases where the same letter is needed more than once (ex:baby).

Body:
I'm trying to build a program in Python that, given a list of words and a list of letters, can determine what words can be made with 7 random letters from the list of letters.

I have code that shows all possible words from the list that are possible with all the letters in the list, but I need to change it to choose 7 random letters from the list, then see which words from the list are possible to make with those chosen letters.

Here's the code that uses all letters and determines what words are possible with them:

from collections import Counter

words = ['hi','how','are','you']
letters = ['h','i','b', 'a','r','e', 'l', 'y', 'o', 'u', 'x', 'b']

# For every word
for word in words:

    # Convert the word into a dictionary
    dict = Counter(word)
    flag = 1

    # For every letter in that word
    for key in dict.keys():

        # If that letter is not in the list of available letters, set the flag to 0
        if key not in letters:
            flag = 0

    # If the flag remains 1 (meaning all letters in the word are in the letters list), then print the word.
    if flag == 1:
        print(word)

Right now, it correctly prints the possible words with all the letters, but I want it to choose 7 random letters from the list and print what words are possible.

Body:
You can use Counter from collection to check for the ability to make each word out of the letters:

words   = ['hi','how','are','you']
letters = ['h','i','b', 'a','r','e', 'l', 'y', 'o', 'u', 'x', 'b']

from random      import sample
from collections import Counter

letterCounts  = Counter(sevenLetters)
wordCounts    = { word:Counter(word) for word in words }

sevenLetters  = sample(letters,7)
possibleWords = [ word for word,wc in wordCounts.items() if not wc-letterCounts ]

print(sevenLetters,possibleWords)
# ['x', 'u', 'h', 'b', 'a', 'i', 'b'] ['hi']

Note: using Counter() covers the cases where the same letter is needed more than once (ex:baby).


问:
Using Spark Scala in EMR to get S3 Object size (folder, files)
I am trying to get the folder size for some S3 folders with scala  from my command line EMR.

I have JSON data stored as GZ files in S3. I find I can count the number of JSON records within my files:

```<code>spark.read.json("s3://mybucket/subfolder/subsubfolder/").count
</code>```

But now I need to know how much GB that data accounts for.

I am finding options to get the size for distinct files, but not for a whole folder all up.

答:
I am finding options to get the size for distinct files, but not for a
  whole folder all up.

Solution :

Option1:

Get the s3 access by FileSystem

```<code>    val fs = FileSystem.get(new URI(ipPath), spark.sparkContext.hadoopConfiguration)
</code>```

Note :

1)  new URI is important other wise it will connect to
  hadoop file system path instread of s3 file system(object store :-)) path . using new URI you are giving scheme s3://
  here.

2) org.apache.commons.io.FileUtils.byteCountToDisplaySize will
  give display sizes of file system in GB MB etc...

```<code>      /**
    * recursively print file sizes
    *
    * @param filePath
    * @param fs
    * @return
    */
@throws[FileNotFoundException]
@throws[IOException]
  def getDisplaysizesOfS3Files(filePath: org.apache.hadoop.fs.Path, fs: org.apache.hadoop.fs.FileSystem): scala.collection.mutable.ListBuffer[String] = {
    val fileList = new scala.collection.mutable.ListBuffer[String]
    val fileStatus = fs.listStatus(filePath)
    for (fileStat <- fileStatus) {
      println(s"file path Name : ${fileStat.getPath.toString} length is  ${fileStat.getLen}")
      if (fileStat.isDirectory) fileList ++= (getDisplaysizesOfS3Files(fileStat.getPath, fs))
      else if (fileStat.getLen > 0 && !fileStat.getPath.toString.isEmpty) {
        println("fileStat.getPath.toString" + fileStat.getPath.toString)
        fileList += fileStat.getPath.toString
        val size = fileStat.getLen
        val display = org.apache.commons.io.FileUtils.byteCountToDisplaySize(size)
        println(" length zero files \n " + fileStat)
        println("Name    = " + fileStat.getPath().getName());
        println("Size    = " + size);
        println("Display = " + display);
      } else if (fileStat.getLen == 0) {
        println(" length zero files \n " + fileStat)

      }
    }
    fileList
  }
</code>```

based on your requirement, you can modify the code... you can sum up all the distinct files.

Option 2 : Simple and crispy using getContentSummary

```<code>implicit val spark = SparkSession.builder().appName("ObjectSummary").getOrCreate()
  /**
    * getDisplaysizesOfS3Files
    * @param path
    * @param spark [[org.apache.spark.sql.SparkSession]]
    */
  def getDisplaysizesOfS3Files(path: String)( implicit spark: org.apache.spark.sql.SparkSession): Unit = {
    val filePath = new org.apache.hadoop.fs.Path(path)
    val fileSystem = filePath.getFileSystem(spark.sparkContext.hadoopConfiguration)
    val size = fileSystem.getContentSummary(filePath).getLength
    val display = org.apache.commons.io.FileUtils.byteCountToDisplaySize(size)
    println("path    = " + path);
    println("Size    = " + size);
    println("Display = " + display);
  }
</code>```

Note : Any option showed above will work for
local or
hdfs or
s3
as well

Body:
I am trying to get the folder size for some S3 folders with scala  from my command line EMR.

I have JSON data stored as GZ files in S3. I find I can count the number of JSON records within my files:

spark.read.json("s3://mybucket/subfolder/subsubfolder/").count

But now I need to know how much GB that data accounts for.

I am finding options to get the size for distinct files, but not for a whole folder all up.

Body:
I am finding options to get the size for distinct files, but not for a
  whole folder all up.

Solution :

Option1:

Get the s3 access by FileSystem

    val fs = FileSystem.get(new URI(ipPath), spark.sparkContext.hadoopConfiguration)

Note :

1)  new URI is important other wise it will connect to
  hadoop file system path instread of s3 file system(object store :-)) path . using new URI you are giving scheme s3://
  here.

2) org.apache.commons.io.FileUtils.byteCountToDisplaySize will
  give display sizes of file system in GB MB etc...

      /**
    * recursively print file sizes
    *
    * @param filePath
    * @param fs
    * @return
    */
@throws[FileNotFoundException]
@throws[IOException]
  def getDisplaysizesOfS3Files(filePath: org.apache.hadoop.fs.Path, fs: org.apache.hadoop.fs.FileSystem): scala.collection.mutable.ListBuffer[String] = {
    val fileList = new scala.collection.mutable.ListBuffer[String]
    val fileStatus = fs.listStatus(filePath)
    for (fileStat <- fileStatus) {
      println(s"file path Name : ${fileStat.getPath.toString} length is  ${fileStat.getLen}")
      if (fileStat.isDirectory) fileList ++= (getDisplaysizesOfS3Files(fileStat.getPath, fs))
      else if (fileStat.getLen > 0 && !fileStat.getPath.toString.isEmpty) {
        println("fileStat.getPath.toString" + fileStat.getPath.toString)
        fileList += fileStat.getPath.toString
        val size = fileStat.getLen
        val display = org.apache.commons.io.FileUtils.byteCountToDisplaySize(size)
        println(" length zero files \n " + fileStat)
        println("Name    = " + fileStat.getPath().getName());
        println("Size    = " + size);
        println("Display = " + display);
      } else if (fileStat.getLen == 0) {
        println(" length zero files \n " + fileStat)

      }
    }
    fileList
  }

based on your requirement, you can modify the code... you can sum up all the distinct files.

Option 2 : Simple and crispy using getContentSummary

implicit val spark = SparkSession.builder().appName("ObjectSummary").getOrCreate()
  /**
    * getDisplaysizesOfS3Files
    * @param path
    * @param spark [[org.apache.spark.sql.SparkSession]]
    */
  def getDisplaysizesOfS3Files(path: String)( implicit spark: org.apache.spark.sql.SparkSession): Unit = {
    val filePath = new org.apache.hadoop.fs.Path(path)
    val fileSystem = filePath.getFileSystem(spark.sparkContext.hadoopConfiguration)
    val size = fileSystem.getContentSummary(filePath).getLength
    val display = org.apache.commons.io.FileUtils.byteCountToDisplaySize(size)
    println("path    = " + path);
    println("Size    = " + size);
    println("Display = " + display);
  }

Note : Any option showed above will work for
local or
hdfs or
s3
as well


问:
How do I make a sorted list of items based on multiple list of sorted items?
I am trying to get a sorted master list from smaller lists. This can be in python or R.

in R, I have

```<code>l1<-c("a","c","d")
l2<-c("a","b","e")
l3<-c("a","c","e")
l4<-c("a","b","c","e")
l5<-c("b","c","d")

m<-unique(c(l1,l2,l3,l4,l5))
</code>```

The output I was expecting is a,b,c,d,e.

In python

```<code>l1=["a","c","d"]
l2=["a","b","e"]
l3=["a","c","e"]
l4=["a","b","c","e"]
l5=["b","c","d"]
</code>```

expected result is ["a","b","c","d","e"]
I started with making a set and going over each list and checking with index but it started getting complex.

Thank you for help.

EDIT:
I think I confused you with having a alphabetically sorted list. Those list items could be random. the real order may not be a,b,c,d,e.

```<code>l1=["e","a"]
l2=["e","b","d"]
l3=["b","d","a"]
</code>```

in this case expected order is ["e","b","d","a"] not ["a","b","d","e"]

for more clarity, think about few people trying to name US states from east to west.

```<code>person 1 says, Florida, Louisiana, Nevada,California.
person 2 says  Alabama, Mississippi, Louisiana, new Mexico, Nevada
person 3 says Florida, Alabama, Texas, New Mexico, California
person 4 says Alabama, Mississippi, Texas, Nevada
person 5 says Mississippi Louisiana, Nevada
</code>```

and I am trying to get right order from information above.

So, here, We will start with Florida, Louisiana, Nevada, California. Now, adding second, it would be, (Alabama, Florida),Louisiana,New Mexico, Nevada, California. Adding 3rd (breaks the tie of Alabama/Florida),Florida, Alabama, Louisiana, Texas, New Mexico, Nevada, California and adding 4th makes Florida, Alabama, (Mississippi/Louisiana), Texas, New Mexico, Nevada, California. adding 5th breaks tie of Mississippi and Louisiana.

答:
Here is an approach in R that turns the vectors into a directed acyclic graph using tidygraph, and then uses node_topo_order to derive the implied node order. Using your example states from east to west:

l1 <- c("Florida", "Louisiana", "Nevada", "California")
l2 <- c("Alabama", "Mississippi", "Louisiana", "New Mexico", "Nevada" )
l3 <- c("Florida", "Alabama", "Texas", "New Mexico", "California")
l4 <- c("Alabama", "Mississippi", "Texas", "Nevada")
l5 <- c("Mississippi", "Louisiana", "Nevada")

library(tidyverse)
library(tidygraph)
ew_graph <- list(l1, l2, l3, l4, l5) %>%
  map_dfr(~tibble(east = ., west = lead(.))) %>% # turn vectors into edge table
  filter(!is.na(west)) %>%
  as_tbl_graph()

ew_graph %>%  # Now we can order nodes and extract their names as output
  arrange(node_topo_order()) %>%
  pull(name)
#> [1] "Florida"     "Alabama"     "Mississippi" "Louisiana"   "Texas"
#> [6] "New Mexico"  "Nevada"      "California"
```

<p>Note that there can be more than one correct order and this will only return one of them. If we want, we can also plot the graph to see the relationships more clearly, which reveals that in this data we have a tie between Louisiana and Texas (you can't trace from one to the other), which you missed in constructing the example. It just happens that we have them in the "true" order. If you need to define a separate way of breaking ties, this approach will require some hacking.</p>

<pre class="lang-r prettyprint-override"><code>library(ggraph)
ggraph(ew_graph) +
  geom_node_label(aes(label = name)) +
  geom_edge_link(
    mapping = aes(start_cap = label_rect(node1.name),
                  end_cap = label_rect(node2.name)),
    arrow = arrow(length = unit(4, 'mm'))
  )
</code>```

Created on 2019-05-28 by the reprex package (v0.3.0)

Body:
I am trying to get a sorted master list from smaller lists. This can be in python or R.

in R, I have

l1<-c("a","c","d")
l2<-c("a","b","e")
l3<-c("a","c","e")
l4<-c("a","b","c","e")
l5<-c("b","c","d")

m<-unique(c(l1,l2,l3,l4,l5))

The output I was expecting is a,b,c,d,e.

In python

l1=["a","c","d"]
l2=["a","b","e"]
l3=["a","c","e"]
l4=["a","b","c","e"]
l5=["b","c","d"]

expected result is ["a","b","c","d","e"]
I started with making a set and going over each list and checking with index but it started getting complex.

Thank you for help.

EDIT:
I think I confused you with having a alphabetically sorted list. Those list items could be random. the real order may not be a,b,c,d,e.

l1=["e","a"]
l2=["e","b","d"]
l3=["b","d","a"]

in this case expected order is ["e","b","d","a"] not ["a","b","d","e"]

for more clarity, think about few people trying to name US states from east to west.

person 1 says, Florida, Louisiana, Nevada,California.
person 2 says  Alabama, Mississippi, Louisiana, new Mexico, Nevada
person 3 says Florida, Alabama, Texas, New Mexico, California
person 4 says Alabama, Mississippi, Texas, Nevada
person 5 says Mississippi Louisiana, Nevada

and I am trying to get right order from information above.

So, here, We will start with Florida, Louisiana, Nevada, California. Now, adding second, it would be, (Alabama, Florida),Louisiana,New Mexico, Nevada, California. Adding 3rd (breaks the tie of Alabama/Florida),Florida, Alabama, Louisiana, Texas, New Mexico, Nevada, California and adding 4th makes Florida, Alabama, (Mississippi/Louisiana), Texas, New Mexico, Nevada, California. adding 5th breaks tie of Mississippi and Louisiana.

Body:
Here is an approach in R that turns the vectors into a directed acyclic graph using tidygraph, and then uses node_topo_order to derive the implied node order. Using your example states from east to west:

l1 <- c("Florida", "Louisiana", "Nevada", "California")
l2 <- c("Alabama", "Mississippi", "Louisiana", "New Mexico", "Nevada" )
l3 <- c("Florida", "Alabama", "Texas", "New Mexico", "California")
l4 <- c("Alabama", "Mississippi", "Texas", "Nevada")
l5 <- c("Mississippi", "Louisiana", "Nevada")

library(tidyverse)
library(tidygraph)
ew_graph <- list(l1, l2, l3, l4, l5) %>%
  map_dfr(~tibble(east = ., west = lead(.))) %>% # turn vectors into edge table
  filter(!is.na(west)) %>%
  as_tbl_graph()

ew_graph %>%  # Now we can order nodes and extract their names as output
  arrange(node_topo_order()) %>%
  pull(name)
#> [1] "Florida"     "Alabama"     "Mississippi" "Louisiana"   "Texas"
#> [6] "New Mexico"  "Nevada"      "California"

Note that there can be more than one correct order and this will only return one of them. If we want, we can also plot the graph to see the relationships more clearly, which reveals that in this data we have a tie between Louisiana and Texas (you can't trace from one to the other), which you missed in constructing the example. It just happens that we have them in the "true" order. If you need to define a separate way of breaking ties, this approach will require some hacking.

library(ggraph)
ggraph(ew_graph) +
  geom_node_label(aes(label = name)) +
  geom_edge_link(
    mapping = aes(start_cap = label_rect(node1.name),
                  end_cap = label_rect(node2.name)),
    arrow = arrow(length = unit(4, 'mm'))
  )

Created on 2019-05-28 by the reprex package (v0.3.0)


问:
Is this class Threadsafe?
Hi

Is the class below threadsafe?

```<code>class ImmutablePossiblyThreadsafeClass<K, V> {

    private final Map<K, V> map;

    public ImmutablePossiblyThreadsafeClass(final Map<K, V> map) {
        this.map = new HashMap<K, V>();

        for (Entry<K, V> entry : map.entrySet()) {
            this.map.put(entry.getKey(), entry.getValue());
        }
    }

    public V get(K key) {
        return this.map.get(key);
    }
}
</code>```

答:
I think it is thread-safe the way it is. You've created (effectively) a copy of the input map and are doing only read-only operations on it. I've sort of always held the belief that immutable classes are inherently thread-safe. But be aware that unless your K and V classes are immutable, you could still run into thread safety issues.

You could also replace it with:

```<code>Map<K, V> m = Collections.unmodifiableMap(Collections.synchronizedMap(map));
</code>```

Title: Is this class Threadsafe?

Body:
Hi

Is the class below threadsafe?

class ImmutablePossiblyThreadsafeClass<K, V> {

    private final Map<K, V> map;

    public ImmutablePossiblyThreadsafeClass(final Map<K, V> map) {
        this.map = new HashMap<K, V>();

        for (Entry<K, V> entry : map.entrySet()) {
            this.map.put(entry.getKey(), entry.getValue());
        }
    }

    public V get(K key) {
        return this.map.get(key);
    }
}

Body:
I think it is thread-safe the way it is. You've created (effectively) a copy of the input map and are doing only read-only operations on it. I've sort of always held the belief that immutable classes are inherently thread-safe. But be aware that unless your K and V classes are immutable, you could still run into thread safety issues.

You could also replace it with:

Map<K, V> m = Collections.unmodifiableMap(Collections.synchronizedMap(map));