问:
Spring property Injection using annotations
I want to configure below type of bean initialization to be performed by annotation.

Below is sample bean configuration in xml type and want to configure this kind of bean using annotations.

```<code><bean id="Animal" class="aaa.type.Animal">
    <property name="Animal" value="${Animal}" />
    <property name="AnimalFamily" >
        <bean class="aaa.type.AnimalFamily">
            <property name="AnimalCharactertitic">
                <list>
                    <bean class="aaa.type.AnimalColor">
                        <property name="name" value="Color" />
                        <property name="value" value="${color}" />
                    </bean>
                    <bean class="aaa.type.AnimalType">
                        <property name="name" value="Animal Type" />
                        <property name="value" value="${AnimalType}" />
                    </bean>
                </list>
            </property>
        </bean>
</property>
</bean>
</code>```

答:
You can do something like this:</p>

```<code>@Configuration
public class MyConfigurationClass {
    @Bean
    public AnimalCharactertitic animalColor(@Value("${color}") String color) {
        AnimalCharactertitic animalCharactertitic = new AnimalCharactertitic();
        animalCharactertitic.setName("Color");
        animalCharactertitic.setValue(color);
        return animalCharactertitic;
    }

    @Bean
    public AnimalCharactertitic animalType(@Value("${AnimalType}") String animalType) {
        AnimalCharactertitic animalCharactertitic = new AnimalCharactertitic();
        animalCharactertitic.setName("Animal Type");
        animalCharactertitic.setValue(animalType);
        return animalCharactertitic;
    }

    @Bean
    public AnimalFamily animalFamily(@Autowired AnimalCharactertitic animalColor,
         @Autowired AnimalCharactertitic animalType) {

        AnimalFamily animalFamily = new AnimalFamily();

        List<AnimalCharactertitic> animalCharactertitics = new ArrayList<>();
        animalCharactertitics.add(animalColor);
        animalCharactertitics.add(animalType);

        animalFamily.setAnimalCharactertitic(animalCharactertitics);

        return animalFamily;
    }

    @Bean
    public Animal animal(@Value("${Animal}") String animal, @Autowired AnimalFamily animalFamily) {

        Animal animal = new Animal();
        animal.setAnimal(animal);
        animal.setAnimalFamily(animalFamily);

        return animal;
    }
}
</code>```

Body:
I want to configure below type of bean initialization to be performed by annotation.

Below is sample bean configuration in xml type and want to configure this kind of bean using annotations.

<bean id="Animal" class="aaa.type.Animal">
    <property name="Animal" value="${Animal}" />
    <property name="AnimalFamily" >
        <bean class="aaa.type.AnimalFamily">
            <property name="AnimalCharactertitic">
                <list>
                    <bean class="aaa.type.AnimalColor">
                        <property name="name" value="Color" />
                        <property name="value" value="${color}" />
                    </bean>
                    <bean class="aaa.type.AnimalType">
                        <property name="name" value="Animal Type" />
                        <property name="value" value="${AnimalType}" />
                    </bean>
                </list>
            </property>
        </bean>
</property>
</bean>

Body:
You can do something like this:

@Configuration
public class MyConfigurationClass {
    @Bean
    public AnimalCharactertitic animalColor(@Value("${color}") String color) {
        AnimalCharactertitic animalCharactertitic = new AnimalCharactertitic();
        animalCharactertitic.setName("Color");
        animalCharactertitic.setValue(color);
        return animalCharactertitic;
    }

    @Bean
    public AnimalCharactertitic animalType(@Value("${AnimalType}") String animalType) {
        AnimalCharactertitic animalCharactertitic = new AnimalCharactertitic();
        animalCharactertitic.setName("Animal Type");
        animalCharactertitic.setValue(animalType);
        return animalCharactertitic;
    }

    @Bean
    public AnimalFamily animalFamily(@Autowired AnimalCharactertitic animalColor,
         @Autowired AnimalCharactertitic animalType) {

        AnimalFamily animalFamily = new AnimalFamily();

        List<AnimalCharactertitic> animalCharactertitics = new ArrayList<>();
        animalCharactertitics.add(animalColor);
        animalCharactertitics.add(animalType);

        animalFamily.setAnimalCharactertitic(animalCharactertitics);

        return animalFamily;
    }

    @Bean
    public Animal animal(@Value("${Animal}") String animal, @Autowired AnimalFamily animalFamily) {

        Animal animal = new Animal();
        animal.setAnimal(animal);
        animal.setAnimalFamily(animalFamily);

        return animal;
    }
}


来源: stackexchange

问:
Why code doesn't Go past the firevents ("Onchange") if popup comes after
I am trying to enter value in the text box and then fire event "onchange", which works. But after that I get a popup and code doesnt move past the onchnage code, so I cant click on the 'ok' button of popup.

```<code><INPUT onchange=javascript:AcctId_ONCHANGE(); id=debAcctId class=textfieldfont style="WIDTH: 150px" maxLength=16 name=nefrtg.debAcctId fdt="String" enabled>

<INPUT onclick="javascript:return nefrtg_det_ONCLICK14(this,this);" id=Go class=sbutton type=button value=Go name=Go ?>
</code>```

Is there a way to click on that button on same line of code or is there any other method?

```<code>ie.Document.frames(0).frames(0).frames(0).Document.getElementById("debAcctId").Value = strDebAcc

ie.Document.frames(0).frames(0).frames(0).Document.getElementById("debAcctId").FireEvent ("onchange")

    While ie.Busy Or ie.ReadyState <> READYSTATE_COMPLETE
        DoEvents
        If ie.Busy Then
            DoEvents
            hWND = FindWindow(vbNullString, "Message from webpage")
            If hWND <> 0 Then childHWND = FindWindowEx(hWND, ByVal 0&, "Button", "OK")
            If childHWND <> 0 Then SendMessage childHWND, BM_CLICK, 0, 0
        End If
    Wend
</code>```

答:
I understand that your VBA code gets stuck when Alert message gets displayed on the page.

I tried to find a solution or a workaround but did not get any working examples.

I found in one old thread that user needs to create an instance of IE from another workbook and find the msg window and close it. I personally think that it is not a practical solution. Other than that I did not get any solution for VBA.

In further search, I found that if you are available to use the Selenium web driver to automate the IE browser then there is a solution for this issue.

To click on the 'OK' button of the alert using Selenium:

```<code>driver.switchTo().alert().accept();
</code>```

To click on the 'Cancel' button of the alert using Selenium:

```<code>driver.switchTo().alert().dismiss();
</code>```

Reference:

Alert & Popup Window Handling in Selenium WebDriver

So you can think about this approach as an alternative for your VBA approach.

ContentLicense: CC BY-SA 4.0

Body:
I am trying to enter value in the text box and then fire event "onchange", which works. But after that I get a popup and code doesnt move past the onchnage code, so I cant click on the 'ok' button of popup.

<INPUT onchange=javascript:AcctId_ONCHANGE(); id=debAcctId class=textfieldfont style="WIDTH: 150px" maxLength=16 name=nefrtg.debAcctId fdt="String" enabled>

<INPUT onclick="javascript:return nefrtg_det_ONCLICK14(this,this);" id=Go class=sbutton type=button value=Go name=Go ?>

Is there a way to click on that button on same line of code or is there any other method?

ie.Document.frames(0).frames(0).frames(0).Document.getElementById("debAcctId").Value = strDebAcc

ie.Document.frames(0).frames(0).frames(0).Document.getElementById("debAcctId").FireEvent ("onchange")

    While ie.Busy Or ie.ReadyState <> READYSTATE_COMPLETE
        DoEvents
        If ie.Busy Then
            DoEvents
            hWND = FindWindow(vbNullString, "Message from webpage")
            If hWND <> 0 Then childHWND = FindWindowEx(hWND, ByVal 0&, "Button", "OK")
            If childHWND <> 0 Then SendMessage childHWND, BM_CLICK, 0, 0
        End If
    Wend

Body:
I understand that your VBA code gets stuck when Alert message gets displayed on the page.

I tried to find a solution or a workaround but did not get any working examples.

I found in one old thread that user needs to create an instance of IE from another workbook and find the msg window and close it. I personally think that it is not a practical solution. Other than that I did not get any solution for VBA.

In further search, I found that if you are available to use the Selenium web driver to automate the IE browser then there is a solution for this issue.

To click on the 'OK' button of the alert using Selenium:

driver.switchTo().alert().accept();

To click on the 'Cancel' button of the alert using Selenium:

driver.switchTo().alert().dismiss();

Reference:

Alert & Popup Window Handling in Selenium WebDriver

So you can think about this approach as an alternative for your VBA approach.


问:
How to split a dataframe into two based on data from another dataframe (before and after some action) in pandas?
I need to split a dataframe with posts using data of date and time of registration into two dataframes, where I will have data of posts before and after registration.

A dataframe with posts:

```<code>posts = {'Login':['User1', 'User1', 'User2', 'User3', 'User3'], 'Posted':['13.12.2018 07:12', '13.05.2019 00:00', '13.05.2019 12:32', '13.06.2019 00:06', '01.07.2019 14:43']}
df_posts = pd.DataFrame(posts)
</code>```

A dataframe with dates of registration:

```<code>registr_date = {'Login':['User1', 'User2', 'User3'], 'Registered':['14.12.2018 12:07', '17.05.2019 14:01', '20.06.2019 15:00']}
df_registr = pd.DataFrame(registr_date)
</code>```

I changed the format of columns with time and date:

```<code>df_posts['Posted']= pd.to_datetime(df_posts['Posted'])
df_registr['Date_of_registration']= pd.to_datetime(df_registr['Date_of_registration'])
</code>```

I need to get the following output.
Posts before registration:

```<code>    Login   Posted_before
0   User1   13.12.2018 07:12
1   User2   13.05.2019 12:32
2   User3   13.06.2019 00:06
</code>```

Posts after registration:

```<code>    Login   Posted_after
0   User1   13.05.2019 00:00
1   User3   01.07.2019 14:43
</code>```

How I should split the dataframe with posts?

答:
```<code> df_posts['Posted'] = pd.to_datetime(df_posts['Posted'], format='%d.%m.%Y
 %H:%M')

 df_registr['Registered'] = pd.to_datetime(df_registr['Registered'], format='%d.%m.%Y
 %H:%M')

 df_merge = pd.merge(df_posts, df_registr, on="Login")
 df_before = df_merge.loc[df_merge['Registered'] > df_merge['Posted']]
 df_after = df_merge.loc[df_merge['Registered'] <= df_merge['Posted']]
</code>```

Body:
I need to split a dataframe with posts using data of date and time of registration into two dataframes, where I will have data of posts before and after registration.

A dataframe with posts:

posts = {'Login':['User1', 'User1', 'User2', 'User3', 'User3'], 'Posted':['13.12.2018 07:12', '13.05.2019 00:00', '13.05.2019 12:32', '13.06.2019 00:06', '01.07.2019 14:43']}
df_posts = pd.DataFrame(posts)

A dataframe with dates of registration:

registr_date = {'Login':['User1', 'User2', 'User3'], 'Registered':['14.12.2018 12:07', '17.05.2019 14:01', '20.06.2019 15:00']}
df_registr = pd.DataFrame(registr_date)

I changed the format of columns with time and date:

df_posts['Posted']= pd.to_datetime(df_posts['Posted'])
df_registr['Date_of_registration']= pd.to_datetime(df_registr['Date_of_registration'])

I need to get the following output.
Posts before registration:

    Login   Posted_before
0   User1   13.12.2018 07:12
1   User2   13.05.2019 12:32
2   User3   13.06.2019 00:06

Posts after registration:

    Login   Posted_after
0   User1   13.05.2019 00:00
1   User3   01.07.2019 14:43

How I should split the dataframe with posts?


问:
Init once with lambda function pointer, self-modifying (C++)
I was wondering if it's possible to create inline lambda initialization function, so after initialization it would replace itself with dummy initialization. Some sort of lazy initialization algorithm with as less pain as possible. Here is some pseudo code what I would like to achieve:

```<code>SomeData* data = nullptr;
auto initF = [&]
{
    data = initData();
    initF = [&] {};
};
</code>```

so in for loop it would look like this:

```<code>for(int i = 0; i < count; i++) {
     initF();
     data[i] = ....;
}
</code>```

Basically if count is 0 - initF would not be ever called, but once called - pointer becomes initialized and can be used after that.

答:
Use a smart pointer to a closure.

```<code>std::shared_ptr<std::function<void(void)>> initf;
</code>```

Then use std::call_once to initialize it (with some lambda expression), or some static constructor initializing it, e.g.

```<code>//untested
initf.reset (& [&] () { initf = [&]() {
   auto oldinitf=initf; static int cnt;
   cnt++;
   std::cout << "cnt=" << cnt << std::endl;
   if (oldinitf)
      (oldinitf.get()) ();
});
</code>```

Read also about the Y fix-point combinator in the untyped  λ-calculus.

Queinnec's book Lisp in Small Pieces (and Rice's theorem) is related to your question.

Observe that circular references in memory require an indirection at least (so are unfriendly to reference counting approaches).

On Linux you could generate some similar C++ code at runtime (using raw function pointers), compile it into a temporary plugin, and then use dlopen with dlsym (I did that several years ago in GCC MELT, now obsolete). See C++ dlopen minihowto and look into RefPerSys as an example (of a C++ program generating then running C++ code). Look also into JIT compiling libraries such as libgccjit, asmjit, etc...

Observe that such tricks are easier with SBCL, once you made the efforts of learning Common Lisp. SBCL is generating machine code at most REPL interactions.

Body:
I was wondering if it's possible to create inline lambda initialization function, so after initialization it would replace itself with dummy initialization. Some sort of lazy initialization algorithm with as less pain as possible. Here is some pseudo code what I would like to achieve:

SomeData* data = nullptr;
auto initF = [&]
{
    data = initData();
    initF = [&] {};
};

so in for loop it would look like this:

for(int i = 0; i < count; i++) {
     initF();
     data[i] = ....;
}

Basically if count is 0 - initF would not be ever called, but once called - pointer becomes initialized and can be used after that.

Body:
Use a smart pointer to a closure.

std::shared_ptr<std::function<void(void)>> initf;

Then use std::call_once to initialize it (with some lambda expression), or some static constructor initializing it, e.g.

//untested
initf.reset (& [&] () { initf = [&]() {
   auto oldinitf=initf; static int cnt;
   cnt++;
   std::cout << "cnt=" << cnt << std::endl;
   if (oldinitf)
      (oldinitf.get()) ();
});

Read also about the Y fix-point combinator in the untyped  λ-calculus.

Queinnec's book Lisp in Small Pieces (and Rice's theorem) is related to your question.

Observe that circular references in memory require an indirection at least (so are unfriendly to reference counting approaches).

On Linux you could generate some similar C++ code at runtime (using raw function pointers), compile it into a temporary plugin, and then use dlopen with dlsym (I did that several years ago in GCC MELT, now obsolete). See C++ dlopen minihowto and look into RefPerSys as an example (of a C++ program generating then running C++ code). Look also into JIT compiling libraries such as libgccjit, asmjit, etc...

Observe that such tricks are easier with SBCL, once you made the efforts of learning Common Lisp. SBCL is generating machine code at most REPL interactions.


问:
Powermock fails tests
I wanted to replace my JUnitRunner by Powermockrunnner (in order to further use features of Powermock that simple Mockito does not have).

So I added the dependencies, replaced the runner in my test class, and added the @PrepareForTest annotation and, before writing any further test code, ran my existing tests to validate them.

Of ~25 tests, 5 or 6 failed with apparently functional reasons (I mean they failed on actual failures in the run code, not on some infrastructural errors).

In a normal state, these tests are 100% consistent and non-flaky.

How can such a change fail fully working tests?

答:
Though this is a old post, I had a similar experience recently and came across the dependency matrix between PowerMock and Mockito.

When we are using both Mockito and PowerMock dependencies in our Springboot project, We need to be cautious about the versions of the PowerMock and Mockito we are using.

Here is the dependency matrix for the same.

Usually Spring boot starter test comes with a version of Mockito. In my case, I had to exclude the Mockito dependency out of starter and add the Mockito version that works well with the latest PowerMock release. Would be happy if this helps anyone.

ContentLicense: CC BY-SA 3.0

Title: Powermock fails tests


问:
how to display alert when networking connection is disable?
i am new to iphone in my app i am connecting to database through web services it working correctly,but i want to displat alert  when network connection is disable.please any help me how to alert when net connection is disable,
  thanks in advance.</p>

答: Use UIAlertView  to display alerts</p>

Body: Use UIAlertView  to display alerts


问:
Java timezone conversion
I have a String, representing time in UTC.
I need to convert it to long representing milliseconds since midnight at EST, considering daylight saving times.
For example, in January, the offset is 5 hours, but in June 4 hours.

However, the code below displays the same offset of 5 hours for both June and January.
The variable tzOffset = -18000000 (=-5 hours), regardless the date month.

Please advise,

Thanks!

```<code>package TimeConversion;

import java.text.SimpleDateFormat;

import java.util.*;

public class TimeConversion {

    public static void main(String[] args) throws Exception {

        String utcTime = "20100101120000000";
        SimpleDateFormat sdfIn = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        sdfIn.setTimeZone(TimeZone.getTimeZone("UTC"));
        long utcMillis = sdfIn.parse(utcTime).getTime();
        long tzOffset = TimeZone.getTimeZone("EST").getOffset(utcMillis);
        long estMillis = utcMillis + tzOffset;
        long estMillisSinceMidnight = estMillis % 86400000;
        System.out.println("utcTime = " + utcTime + "\nestMillisSinceMidnight = " + estMillisSinceMidnight + "(" + 24.0 * estMillisSinceMidnight / 86.4e6 + ")");
    }

}
</code>```

答:
tl;dr

```<code>Duration                                                     // Represent a span-of-time not attached to the timeline, on a scale of hours-minutes-seconds.
.between(                                                    // Instantiate a `Duration` by calculating time elapsed between a pair of moments.
    LocalDateTime.parse(                                     // Represent a date with time-of-day but lacking the context of a time zone or offset-from-UTC.
        "20100101120000000" ,                                // Tip: Use standard ISO 8601 formats instead of inventing your own.
        DateTimeFormatter.ofPattern( "uuuuMMddHHmmssSSS" )
    )                                                        // Returns a `LocalDateTime` object.
    .atZone(                                                 // Place this date-with-time in the context of a particular time zone to determine a moment, a specific point on the timeline.
        ZoneId.of( "America/Montreal" )                      // Never use 2-4 character pseudo-zones such as `EST` or `EDT`. Real time zone names are in `Continent/Region` format.
    )                                                        // Returns a `ZonedDateTime` object.
    .toLocalDate()                                           // Extracts the date-only portion from a `ZonedDateTime` object.
    .atStartOfDay(                                           // Determine the first moment of the day on that date as seen in a particular time zone.
        ZoneId.of( "America/Montreal" )
    )                                                        // Returns a `ZonedDateTime` object, rather than altering original. (immutable objects)
    ,
    LocalDateTime.parse(
        "20100101120000000" ,
        DateTimeFormatter.ofPattern( "uuuuMMddHHmmssSSS" )
    )
    .atZone(
        ZoneId.of( "America/Montreal" )
    )
)                                                             // Returns a `Duration`.
.toMillis()                                                   // Returs a `long` integer.
</code>```

Beware of possible data loss as any microseconds or nanoseconds are not accounted for with a coarser count of milliseconds.

ISO 8601

String utcTime = "20100101120000000";

By the way, rather than invent your own formats, use the standard ISO 8601 formats for exchanging date-time values as text. The standard was invented for that very purpose.

java.time

Define a formatting pattern to match your input.

```<code>String input = "20100101120000000";
DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuuMMddHHmmssSSS" ) ;
</code>```

Parse as a LocalDateTime object, because your input lacks the context of a time zone or offset-from-UTC.

```<code>LocalDateTime ldt = LocalDateTime.parse( input , f ) ;
</code>```

If you know for certain the intended time zone for that date and time-of-day, apply a ZoneId to get a ZonedDateTime.

I am guessing by EDT you mean east coast time somewhere North America or the Caribbean. That could be any number of various time zones such as America/New_York, America/Louisville, and so on. I arbitrarily guessed America/Montreal for my code example here.

Never use the 2-4 letter abbreviation such as EDT or EST or IST as they are not true time zones, not standardized, and not even unique(!). Specify a proper time zone name in the format of Continent/Region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland.

```<code>ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = ldt.atZone( z ) ;
</code>```

You want to get elapsed time since the first moment of the day as seen in that time zone, until this ZonedDateTime object we just determined.

Let java.time determine the first moment of the day. In some zones on some dates, the day may not start at 00:00.

```<code>LocalDate ld = zdt.toLocalDate() ;
ZonedDateTime zdtStartOfDay = ld.atStartOfDay( z ) ;
</code>```

Calculate elapsed time as a Duration. Feed inputs as Instant objects, which represent the same moment as our ZonedDateTime objects but are adjusted to UTC (an offset of zero hours-minutes-seconds).

```<code>Duration d = Duration.between(
    zdtStartOfDay.toInstant() ,
    zdt.toInstant()
);
</code>```

Apparently you want to represent that span-of-time as a count of milliseconds. Beware of possible data-loss, as the ZonedDateTime & Instant classes resolve to the finer nanoseconds.

```<code>long millis = d.toMillis() ;  // Report entire span-of-time as a count of milliseconds. Beware: possible data loss as any microseconds and nanoseconds are ignored.
</code>```

See this code run live at IdeOne.com. We get a result of 12 hours.

d.toString(): PT12H

millis: 43200000

ContentLicense: CC BY-SA 2.5

Title: Java timezone conversion

Body:
I have a String, representing time in UTC.
I need to convert it to long representing milliseconds since midnight at EST, considering daylight saving times.
For example, in January, the offset is 5 hours, but in June 4 hours.

However, the code below displays the same offset of 5 hours for both June and January.
The variable tzOffset = -18000000 (=-5 hours), regardless the date month.

Please advise,

Thanks!

package TimeConversion;

import java.text.SimpleDateFormat;

import java.util.*;

public class TimeConversion {

    public static void main(String[] args) throws Exception {

        String utcTime = "20100101120000000";
        SimpleDateFormat sdfIn = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        sdfIn.setTimeZone(TimeZone.getTimeZone("UTC"));
        long utcMillis = sdfIn.parse(utcTime).getTime();
        long tzOffset = TimeZone.getTimeZone("EST").getOffset(utcMillis);
        long estMillis = utcMillis + tzOffset;
        long estMillisSinceMidnight = estMillis % 86400000;
        System.out.println("utcTime = " + utcTime + "\nestMillisSinceMidnight = " + estMillisSinceMidnight + "(" + 24.0 * estMillisSinceMidnight / 86.4e6 + ")");
    }

}

Body:
tl;dr

Duration                                                     // Represent a span-of-time not attached to the timeline, on a scale of hours-minutes-seconds.
.between(                                                    // Instantiate a `Duration` by calculating time elapsed between a pair of moments.
    LocalDateTime.parse(                                     // Represent a date with time-of-day but lacking the context of a time zone or offset-from-UTC.
        "20100101120000000" ,                                // Tip: Use standard ISO 8601 formats instead of inventing your own.
        DateTimeFormatter.ofPattern( "uuuuMMddHHmmssSSS" )
    )                                                        // Returns a `LocalDateTime` object.
    .atZone(                                                 // Place this date-with-time in the context of a particular time zone to determine a moment, a specific point on the timeline.
        ZoneId.of( "America/Montreal" )                      // Never use 2-4 character pseudo-zones such as `EST` or `EDT`. Real time zone names are in `Continent/Region` format.
    )                                                        // Returns a `ZonedDateTime` object.
    .toLocalDate()                                           // Extracts the date-only portion from a `ZonedDateTime` object.
    .atStartOfDay(                                           // Determine the first moment of the day on that date as seen in a particular time zone.
        ZoneId.of( "America/Montreal" )
    )                                                        // Returns a `ZonedDateTime` object, rather than altering original. (immutable objects)
    ,
    LocalDateTime.parse(
        "20100101120000000" ,
        DateTimeFormatter.ofPattern( "uuuuMMddHHmmssSSS" )
    )
    .atZone(
        ZoneId.of( "America/Montreal" )
    )
)                                                             // Returns a `Duration`.
.toMillis()                                                   // Returs a `long` integer.

Beware of possible data loss as any microseconds or nanoseconds are not accounted for with a coarser count of milliseconds.

ISO 8601

String utcTime = "20100101120000000";

By the way, rather than invent your own formats, use the standard ISO 8601 formats for exchanging date-time values as text. The standard was invented for that very purpose.

java.time

Define a formatting pattern to match your input.

String input = "20100101120000000";
DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuuMMddHHmmssSSS" ) ;

Parse as a LocalDateTime object, because your input lacks the context of a time zone or offset-from-UTC.

LocalDateTime ldt = LocalDateTime.parse( input , f ) ;

If you know for certain the intended time zone for that date and time-of-day, apply a ZoneId to get a ZonedDateTime.

I am guessing by EDT you mean east coast time somewhere North America or the Caribbean. That could be any number of various time zones such as America/New_York, America/Louisville, and so on. I arbitrarily guessed America/Montreal for my code example here.

Never use the 2-4 letter abbreviation such as EDT or EST or IST as they are not true time zones, not standardized, and not even unique(!). Specify a proper time zone name in the format of Continent/Region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland.

ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = ldt.atZone( z ) ;

You want to get elapsed time since the first moment of the day as seen in that time zone, until this ZonedDateTime object we just determined.

Let java.time determine the first moment of the day. In some zones on some dates, the day may not start at 00:00.

LocalDate ld = zdt.toLocalDate() ;
ZonedDateTime zdtStartOfDay = ld.atStartOfDay( z ) ;

Calculate elapsed time as a Duration. Feed inputs as Instant objects, which represent the same moment as our ZonedDateTime objects but are adjusted to UTC (an offset of zero hours-minutes-seconds).

Duration d = Duration.between(
    zdtStartOfDay.toInstant() ,
    zdt.toInstant()
);

Apparently you want to represent that span-of-time as a count of milliseconds. Beware of possible data-loss, as the ZonedDateTime & Instant classes resolve to the finer nanoseconds.

long millis = d.toMillis() ;  // Report entire span-of-time as a count of milliseconds. Beware: possible data loss as any microseconds and nanoseconds are ignored.

See this code run live at IdeOne.com. We get a result of 12 hours.

d.toString(): PT12H

millis: 43200000


问:
Text lowercase after capitalization is possible?
My database give mixed font styles, for Example:

" Hi THis is TEST teXT "

I want to capitalization this type of fonts. I found a way, first using lowercase and then ::first-letter to be capitalized. This give the first letter only capital.

But I want capitalization in every first word, is there any way?

答:
In Javascript:

```<code>function titleCase(str) {
var splitStr = str.toLowerCase().split(' ');
for (var i = 0; i < splitStr.length; i++) {
   // You do not need to check if i is larger than splitStr length, as your for does
       that for you
   // Assign it back to the array
    splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
}
// Directly return the joined string
return splitStr.join(' ');
}

document.write(titleCase("I'm a little tea pot"));
</code>```

Please see Capitalize First Letter Of Each Word In A String - JavaScript

Body:
In Javascript:

function titleCase(str) {
var splitStr = str.toLowerCase().split(' ');
for (var i = 0; i < splitStr.length; i++) {
   // You do not need to check if i is larger than splitStr length, as your for does
       that for you
   // Assign it back to the array
    splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
}
// Directly return the joined string
return splitStr.join(' ');
}

document.write(titleCase("I'm a little tea pot"));

Please see Capitalize First Letter Of Each Word In A String - JavaScript


问:
click events not working with current version of aframe [1.0.3]
I am exploring in Augmented reality using A-Frame and ar.js in that I am currently working with interactions  part of 3d-models or primitives.My requirement is on every click of the object or primitive it has to increase its size.But the click events are not working with the current version of A-frame.

Code link for click events click .

A-Frame latest version cdn link.

My code  glitch

答:
You need to include the cursor component and specify which objects will respond to the raycast, (in this case #button) and set this in the opening scene tag: ie.

```<code><a-scene  cursor="rayOrigin: mouse" raycaster="objects: #button">
</code>```

And set the "object" id (in our case '#button') in the script ie.

```<code><script>
        var toggleEl = document.querySelector('#button')
        toggleEl.addEventListener('click', function (evt) {
            toggleEl.emit("clicked");
        });
    </script>
</code>```

and finally trigger the event in the actual entity animation to respond to the emitted string in our case "clicked"
startEvents: clicked (this used to be begin = "clicked")

In this case the id of your primitive or object will be set to id = "button"

Body:
You need to include the cursor component and specify which objects will respond to the raycast, (in this case #button) and set this in the opening scene tag: ie.

<a-scene  cursor="rayOrigin: mouse" raycaster="objects: #button">

And set the "object" id (in our case '#button') in the script ie.

<script>
        var toggleEl = document.querySelector('#button')
        toggleEl.addEventListener('click', function (evt) {
            toggleEl.emit("clicked");
        });
    </script>

and finally trigger the event in the actual entity animation to respond to the emitted string in our case "clicked"
startEvents: clicked (this used to be begin = "clicked")

In this case the id of your primitive or object will be set to id = "button"


问:
Split a String into an array in Swift?
Say I have a string here:

```<code>var fullName: String = "First Last"
</code>```

I want to split the string base on white space and assign the values to their respective variables

```<code>var fullNameArr = // something like: fullName.explode(" ")

var firstName: String = fullNameArr[0]
var lastName: String? = fullnameArr[1]
</code>```

Also, sometimes users might not have a last name.

答:
This gives an array of split parts directly

```<code>var fullNameArr = fullName.components(separatedBy:" ")
</code>```

then you can use like this,

```<code>var firstName: String = fullNameArr[0]
var lastName: String? = fullnameArr[1]
</code>```

Title: Split a String into an array in Swift?

Body:
Say I have a string here:

var fullName: String = "First Last"

I want to split the string base on white space and assign the values to their respective variables

var fullNameArr = // something like: fullName.explode(" ")

var firstName: String = fullNameArr[0]
var lastName: String? = fullnameArr[1]

Also, sometimes users might not have a last name.

Body:
This gives an array of split parts directly

var fullNameArr = fullName.components(separatedBy:" ")

then you can use like this,

var firstName: String = fullNameArr[0]
var lastName: String? = fullnameArr[1]


问:
Application Insights User ID from ASP.NET Core identity
I am using .NET Core 3.1. I want to populate the User ID property in Application Insights with the ASP.NET Core identity username. I got this article by Microsoft, but it is for the full .NET Framework. I also tried the following code given here (I changed claim to Identity)

```<code>public class TelemetryEnrichment : TelemetryInitializerBase
{
    public TelemetryEnrichment(IHttpContextAccessor httpContextAccessor) : base(httpContextAccessor)
    {
    }

    protected override void OnInitializeTelemetry(HttpContext platformContext, RequestTelemetry requestTelemetry, ITelemetry telemetry)
    {
        telemetry.Context.User.AuthenticatedUserId =
            platformContext.User?.Identity.Name ?? string.Empty;
    }
}
</code>```

but don't know how to include it in the pipeline. I tried services.AddSingleton<ITelemetryInitializer, TelemetryEnrichment>(); but it doesn't help. Application Insights is still showing its own User ID.

Startup.cs

```<code>public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(
            Configuration.GetConnectionString("DefaultConnection")));

    services.AddIdentity<ApplicationUser, ApplicationRole>(
        options => {
            options.Stores.MaxLengthForKeys = 128;
            options.User.RequireUniqueEmail = true;
            options.Password.RequiredLength = 8;
        })
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultUI()
        .AddDefaultTokenProviders()
        .AddRoles<ApplicationRole>()
        .AddSignInManager<ApplicationSignInManager>();

    services.AddControllersWithViews()
        .AddNewtonsoftJson()
        .AddRazorRuntimeCompilation();

    services.AddRazorPages();

    services.AddMvc(mvcOptions =>
    {
        mvcOptions.ModelBinderProviders.Insert(0, new ModelBinderProvider());
    })
        .AddRazorPagesOptions(options =>
        {
            options.Conventions.AddAreaPageRoute("Identity", "/Account/Login", "");
        })
        .SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

    services.AddAntiforgery(options => options.HeaderName = "XSRF-TOKEN");

    services.AddFlashMessage();

    services.AddSingleton<IEmailUtility, EmailUtility>();
    services.Configure<EmailSettings>(Configuration.GetSection("EmailSettings"));
    services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));

    services.AddApplicationInsightsTelemetry();

    services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
    services.AddSingleton<ITelemetryInitializer, TelemetryEnrichment>();

    var mappingConfig = new MapperConfiguration(mc =>
    {
        mc.AddProfile(new MappingProfile());
    });
    var mapper = mappingConfig.CreateMapper();
    services.AddSingleton(mapper);
}
</code>```

答:
Answering my own question for completeness. The code given in the question works perfectly fine. I will just put it here again.

To get the ASP.NET Core identity username in your Application Insights, you need a custom telemetry initializer Please refer to original answer by @PeterBons here

```<code>public class TelemetryEnrichment : TelemetryInitializerBase
{
    public TelemetryEnrichment(IHttpContextAccessor httpContextAccessor) : base(httpContextAccessor)
    {
    }

    protected override void OnInitializeTelemetry(HttpContext platformContext, RequestTelemetry requestTelemetry, ITelemetry telemetry)
    {
        telemetry.Context.User.AuthenticatedUserId =
            platformContext.User?.Identity.Name ?? string.Empty;
    }
}
</code>```

Then you need to register it in your Startup.cs ConfigureServices as below:

```<code>public void ConfigureServices(IServiceCollection services)
{
    ...
    ...
    services.AddApplicationInsightsTelemetry();

    services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
    services.AddSingleton<ITelemetryInitializer, TelemetryEnrichment>();
    ...
    ...
}
</code>```

Remember to put AddApplicationInsightsTelemetry() above the AddSingleton()

Once this is set up, the identity username is displayed under Auth User ID property in Application Insights.

-- Update --

The identity username is now displayed under Auth Id instead of Auth User ID as stated above.

Body:
I am using .NET Core 3.1. I want to populate the User ID property in Application Insights with the ASP.NET Core identity username. I got this article by Microsoft, but it is for the full .NET Framework. I also tried the following code given here (I changed claim to Identity)

public class TelemetryEnrichment : TelemetryInitializerBase
{
    public TelemetryEnrichment(IHttpContextAccessor httpContextAccessor) : base(httpContextAccessor)
    {
    }

    protected override void OnInitializeTelemetry(HttpContext platformContext, RequestTelemetry requestTelemetry, ITelemetry telemetry)
    {
        telemetry.Context.User.AuthenticatedUserId =
            platformContext.User?.Identity.Name ?? string.Empty;
    }
}

but don't know how to include it in the pipeline. I tried services.AddSingleton<ITelemetryInitializer, TelemetryEnrichment>(); but it doesn't help. Application Insights is still showing its own User ID.

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(
            Configuration.GetConnectionString("DefaultConnection")));

    services.AddIdentity<ApplicationUser, ApplicationRole>(
        options => {
            options.Stores.MaxLengthForKeys = 128;
            options.User.RequireUniqueEmail = true;
            options.Password.RequiredLength = 8;
        })
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultUI()
        .AddDefaultTokenProviders()
        .AddRoles<ApplicationRole>()
        .AddSignInManager<ApplicationSignInManager>();

    services.AddControllersWithViews()
        .AddNewtonsoftJson()
        .AddRazorRuntimeCompilation();

    services.AddRazorPages();

    services.AddMvc(mvcOptions =>
    {
        mvcOptions.ModelBinderProviders.Insert(0, new ModelBinderProvider());
    })
        .AddRazorPagesOptions(options =>
        {
            options.Conventions.AddAreaPageRoute("Identity", "/Account/Login", "");
        })
        .SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

    services.AddAntiforgery(options => options.HeaderName = "XSRF-TOKEN");

    services.AddFlashMessage();

    services.AddSingleton<IEmailUtility, EmailUtility>();
    services.Configure<EmailSettings>(Configuration.GetSection("EmailSettings"));
    services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));

    services.AddApplicationInsightsTelemetry();

    services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
    services.AddSingleton<ITelemetryInitializer, TelemetryEnrichment>();

    var mappingConfig = new MapperConfiguration(mc =>
    {
        mc.AddProfile(new MappingProfile());
    });
    var mapper = mappingConfig.CreateMapper();
    services.AddSingleton(mapper);
}

Body:
Answering my own question for completeness. The code given in the question works perfectly fine. I will just put it here again.

To get the ASP.NET Core identity username in your Application Insights, you need a custom telemetry initializer Please refer to original answer by @PeterBons here

public class TelemetryEnrichment : TelemetryInitializerBase
{
    public TelemetryEnrichment(IHttpContextAccessor httpContextAccessor) : base(httpContextAccessor)
    {
    }

    protected override void OnInitializeTelemetry(HttpContext platformContext, RequestTelemetry requestTelemetry, ITelemetry telemetry)
    {
        telemetry.Context.User.AuthenticatedUserId =
            platformContext.User?.Identity.Name ?? string.Empty;
    }
}

Then you need to register it in your Startup.cs ConfigureServices as below:

public void ConfigureServices(IServiceCollection services)
{
    ...
    ...
    services.AddApplicationInsightsTelemetry();

    services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
    services.AddSingleton<ITelemetryInitializer, TelemetryEnrichment>();
    ...
    ...
}

Remember to put AddApplicationInsightsTelemetry() above the AddSingleton()

Once this is set up, the identity username is displayed under Auth User ID property in Application Insights.

-- Update --

The identity username is now displayed under Auth Id instead of Auth User ID as stated above.


问:
How do I properly nest this If statement
I'm capturing user input but restricting them between 1-100. I'm having some difficulty nesting this if statement correctly.
Goal:
user can input any amount between 1-100
if not, it sets automatically to 0.
I print the results at the end</p>

```<code>    // if statement to restrict use to 1-100
    hatNumber = in.readInt("How many beaver hats would you like?: \n");
    if (hatNumber <= 0 && hatNumber > 100) {
        System.out.println("Hats = " + hatNumber);
    } else {
        hatNumber = 0;
    }
    photoNumber = in.readInt("How many photographs would you like?: \n");
    if (photoNumber <= 0 && photoNumber > 100) {
        System.out.println("Photographs = " + photoNumber);
    } else {
        photoNumber = 0;

        jarNumber = in.readInt("How many Jars of maple syrup?: \n");

    if (jarNumber <= 0 && jarNumber > 100) {
            System.out.println("Jars = " + jarNumber);

        } else {
            jarNumber = 0;
            {

                System.out.println("Hats = " + hatNumber);
                System.out.println("Photographs = " + photoNumber);
                System.out.println("Jars = " + jarNumber);
</code>```

答:
you're missing a close brace on the line after  </p>

```<code>photoNumber = 0;
</code>```

Body:
I'm capturing user input but restricting them between 1-100. I'm having some difficulty nesting this if statement correctly.
Goal:
user can input any amount between 1-100
if not, it sets automatically to 0.
I print the results at the end

    // if statement to restrict use to 1-100
    hatNumber = in.readInt("How many beaver hats would you like?: \n");
    if (hatNumber <= 0 && hatNumber > 100) {
        System.out.println("Hats = " + hatNumber);
    } else {
        hatNumber = 0;
    }
    photoNumber = in.readInt("How many photographs would you like?: \n");
    if (photoNumber <= 0 && photoNumber > 100) {
        System.out.println("Photographs = " + photoNumber);
    } else {
        photoNumber = 0;

        jarNumber = in.readInt("How many Jars of maple syrup?: \n");

    if (jarNumber <= 0 && jarNumber > 100) {
            System.out.println("Jars = " + jarNumber);

        } else {
            jarNumber = 0;
            {

                System.out.println("Hats = " + hatNumber);
                System.out.println("Photographs = " + photoNumber);
                System.out.println("Jars = " + jarNumber);

Body:
you're missing a close brace on the line after

photoNumber = 0;


问:
How do I set a mock date in Jest?
I'm using moment.js to do most of my date logic in a helper file for my React components but I haven't been able to figure out how to mock a date in Jest a la sinon.useFakeTimers().

The Jest docs only speak about timer functions like setTimeout, setInterval etc but don't help with setting a date and then checking that my date functions do what they're meant to do.

Here is some of my JS file:

var moment = require('moment');

var DateHelper = {

  DATE_FORMAT: 'MMMM D',
  API_DATE_FORMAT: 'YYYY-MM-DD',

  formatDate: function(date) {
    return date.format(this.DATE_FORMAT);
  },

  isDateToday: function(date) {
    return this.formatDate(date) === this.formatDate(moment());
  }
};

module.exports = DateHelper;
```
<p>and here is what I've set up using Jest:</p>
<pre class="lang-js prettyprint-override"><code>jest.dontMock('../../../dashboard/calendar/date-helper')
    .dontMock('moment');

describe('DateHelper', function() {
  var DateHelper = require('../../../dashboard/calendar/date-helper'),
      moment = require('moment'),
      DATE_FORMAT = 'MMMM D';

  describe('formatDate', function() {

    it('should return the date formatted as DATE_FORMAT', function() {
      var unformattedDate = moment('2014-05-12T00:00:00.000Z'),
          formattedDate = DateHelper.formatDate(unformattedDate);

      expect(formattedDate).toEqual('May 12');
    });

  });

  describe('isDateToday', function() {

    it('should return true if the passed in date is today', function() {
      var today = moment();

      expect(DateHelper.isDateToday(today)).toEqual(true);
    });

  });

});
</code>```

Now these tests pass because I'm using moment and my functions use moment but it seems a bit unstable and I would like to set the date to a fixed time for the tests.

Any idea on how that could be accomplished?

答:
This is how I mocked my Date.now() method to set the year to 2010 for my test

```<code>jest
  .spyOn(global.Date, 'now')
  .mockImplementationOnce(() => new Date(`2010`).valueOf());
</code>```

Title: How do I set a mock date in Jest?

Body:
I'm using moment.js to do most of my date logic in a helper file for my React components but I haven't been able to figure out how to mock a date in Jest a la sinon.useFakeTimers().

The Jest docs only speak about timer functions like setTimeout, setInterval etc but don't help with setting a date and then checking that my date functions do what they're meant to do.

Here is some of my JS file:

var moment = require('moment');

var DateHelper = {

  DATE_FORMAT: 'MMMM D',
  API_DATE_FORMAT: 'YYYY-MM-DD',

  formatDate: function(date) {
    return date.format(this.DATE_FORMAT);
  },

  isDateToday: function(date) {
    return this.formatDate(date) === this.formatDate(moment());
  }
};

module.exports = DateHelper;

and here is what I've set up using Jest:

jest.dontMock('../../../dashboard/calendar/date-helper')
    .dontMock('moment');

describe('DateHelper', function() {
  var DateHelper = require('../../../dashboard/calendar/date-helper'),
      moment = require('moment'),
      DATE_FORMAT = 'MMMM D';

  describe('formatDate', function() {

    it('should return the date formatted as DATE_FORMAT', function() {
      var unformattedDate = moment('2014-05-12T00:00:00.000Z'),
          formattedDate = DateHelper.formatDate(unformattedDate);

      expect(formattedDate).toEqual('May 12');
    });

  });

  describe('isDateToday', function() {

    it('should return true if the passed in date is today', function() {
      var today = moment();

      expect(DateHelper.isDateToday(today)).toEqual(true);
    });

  });

});

Now these tests pass because I'm using moment and my functions use moment but it seems a bit unstable and I would like to set the date to a fixed time for the tests.

Any idea on how that could be accomplished?

Body:
This is how I mocked my Date.now() method to set the year to 2010 for my test

jest
  .spyOn(global.Date, 'now')
  .mockImplementationOnce(() => new Date(`2010`).valueOf());


答:
You can use the following if you strictly want to do it in style.css file:

```<code>selector{
text-transform: capitalize;
}
</code>```

Another option is :

```<code>{{ textToBeConverted | uppercase }}
</code>```

Thanks !

Title: Text lowercase after capitalization is possible?

Body:
My database give mixed font styles, for Example:

" Hi THis is TEST teXT "

I want to capitalization this type of fonts. I found a way, first using lowercase and then ::first-letter to be capitalized. This give the first letter only capital.

But I want capitalization in every first word, is there any way?

Body:
You can use the following if you strictly want to do it in style.css file:

selector{
text-transform: capitalize;
}

Another option is :

{{ textToBeConverted | uppercase }}

Thanks !


问:
How to find which parts of C# code may throw exceptions
Is there a way to make Visual Studio tell me which parts of my code may throw exceptions? Something like the compiler warnings that can be suppressed, but instead it warns me that some line may throw an exception if it isn't surrounded by a try and catch.

答:
No, there is no such functionality in VS.

C# (and .Net in general) does not support any kind of annotation to specify "method throws an interesting exception" (unlike Java, see Why are Exceptions not Checked in .NET?). As result there is no way to have general tool that will let you know that some "interesting" exception ( IOException as you've suggested) can be thrown from any given method.

In some cases VS/C# compiler can help. I.e. in C# 8 NRE actually has partial special treatment - "nullable reference type" which lets compiler to know if code expects null or not and warn at compile time if null passed where it is not supposed to.

Note: XML documentation (which is mentioned in comments as source of intellisense tooltips) is not part of code and there is no requirements for it to be correct/complete. Wast majority of the code does not have any useful XML documentation and documentation is not necessary available at compile time. So while it may serve as basis for some tool I don't believe VS is using it for any code highlighting.


问:
Python3.0:-the variable which is assigned inside a path function is not readable outside it
I am trying to create function which takes the path and then store it in a variable but the variable which is assigned inside a function is not readable outside it

```<code>def source_func(path):
    source_path=path
    print("source path is:",source_path)

source_func("C:/Users/Tools Lab1/Downloads/RBI")
source_path
</code>```

Actual Output:

function main.source_path(path)

Expected Output:

C:/Users/Tools Lab1/Downloads/RBI

答:
The function should return the value, then you can assign it to the variable.</p>

```<code>def source_func(path):
    source_path=path
    print("source path is:",source_path)
    return source_path

source_path = source_func("C:/Users/Tools Lab1/Downloads/RBI")
</code>```

Body:
I am trying to create function which takes the path and then store it in a variable but the variable which is assigned inside a function is not readable outside it

def source_func(path):
    source_path=path
    print("source path is:",source_path)

source_func("C:/Users/Tools Lab1/Downloads/RBI")
source_path

Actual Output:

function main.source_path(path)

Expected Output:

C:/Users/Tools Lab1/Downloads/RBI

Body:
The function should return the value, then you can assign it to the variable.

def source_func(path):
    source_path=path
    print("source path is:",source_path)
    return source_path

source_path = source_func("C:/Users/Tools Lab1/Downloads/RBI")


问:
Get output of the last 4 layers of BERT (Tensorflow)
I was going through Andreas Poyiatzis' article posted on towardsdatascience.com. Below is the link for the article.

https://towardsdatascience.com/nlp-extract-contextualized-word-embeddings-from-bert-keras-tf-67ef29f60a7b#--responses

The embeddings generated are through the use of TPU. But I want to run it on my local machine, which does not support TPU. Can someone let me know how can I run the same code on the CPU? Your help will be appreciated. Thanks!

答:
Following the link, the code says if no TPU is available, Estimator falls back to CPU or GPU. Just make sure you set use_tpu = False everywhere it shows up:

```<code>  # If TPU is not available, this will fall back to normal Estimator on CPU
  # or GPU.
  estimator = tf.contrib.tpu.TPUEstimator(
      use_tpu=False,
      model_fn=model_fn,
      config=run_config,
      predict_batch_size=BATCH_SIZE,
      train_batch_size=BATCH_SIZE)
</code>```

Also directly above that in the model_fn_builder call. Like this:

```<code>model_fn = model_fn_builder(
      bert_config=bert_config,
      init_checkpoint=INIT_CHECKPOINT,
      layer_indexes=layer_indexes,
      use_tpu=False,
      use_one_hot_embeddings=True)
</code>```

Body:
Following the link, the code says if no TPU is available, Estimator falls back to CPU or GPU. Just make sure you set use_tpu = False everywhere it shows up:

  # If TPU is not available, this will fall back to normal Estimator on CPU
  # or GPU.
  estimator = tf.contrib.tpu.TPUEstimator(
      use_tpu=False,
      model_fn=model_fn,
      config=run_config,
      predict_batch_size=BATCH_SIZE,
      train_batch_size=BATCH_SIZE)

Also directly above that in the model_fn_builder call. Like this:

model_fn = model_fn_builder(
      bert_config=bert_config,
      init_checkpoint=INIT_CHECKPOINT,
      layer_indexes=layer_indexes,
      use_tpu=False,
      use_one_hot_embeddings=True)


问:
How to load local file using sc.textFile in spark?
I've been trying to load local file using sc.textFile()in spark.

I already read [question]:How to load local file in sc.textFile, instead of HDFS

I have local file in /home/spark/data.txt on Centos 7.0

When I use val data = sc.textFile("file:///home/spark/data.txt").collect, I got a error as below.

16/12/27 12:15:56 WARN TaskSetManager: Lost task 0.0 in stage 5.0 (TID
  36,): java.io.FileNotFoundException: File file:/home/spark/data.txt does not
  exist
          at org.apache.hadoop.fs.RawLocalFileSystem.deprecatedGetFileStatus(RawLocalFileSystem.java:609)
          at org.apache.hadoop.fs.RawLocalFileSystem.getFileLinkStatusInternal(RawLocalFileSystem.java:822)
          at org.apache.hadoop.fs.RawLocalFileSystem.getFileStatus(RawLocalFileSystem.java:599)
          at org.apache.hadoop.fs.FilterFileSystem.getFileStatus(FilterFileSystem.java:421)
          at org.apache.hadoop.fs.ChecksumFileSystem$ChecksumFSInputChecker.(ChecksumFileSystem.java:140)
          at org.apache.hadoop.fs.ChecksumFileSystem.open(ChecksumFileSystem.java:341)
          at org.apache.hadoop.fs.FileSystem.open(FileSystem.java:767)
          at org.apache.hadoop.mapred.LineRecordReader.(LineRecordReader.java:109)
          at org.apache.hadoop.mapred.TextInputFormat.getRecordReader(TextInputFormat.java:67)
          at org.apache.spark.rdd.HadoopRDD$$anon$1.(HadoopRDD.scala:246)
          at org.apache.spark.rdd.HadoopRDD.compute(HadoopRDD.scala:209)
          at org.apache.spark.rdd.HadoopRDD.compute(HadoopRDD.scala:102)
          at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:319)
          at org.apache.spark.rdd.RDD.iterator(RDD.scala:283)
          at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:38)
          at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:319)
          at org.apache.spark.rdd.RDD.iterator(RDD.scala:283)
          at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:70)
          at org.apache.spark.scheduler.Task.run(Task.scala:85)
          at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:274)
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
          at java.lang.Thread.run(Thread.java:745)

16/12/27 12:15:56 ERROR TaskSetManager: Task 0 in stage 5.0 failed 4
  times; aborting job org.apache.spark.SparkException: Job aborted due
  to stage failure: Task 0 in stage 5.0 failed 4 times, most recent
  failure: Lost task 0.3 in stage 5.0 (TID 42,):
  java.io.FileNotFoundException: File file:/home/spark/data.txt does not exist
          at org.apache.hadoop.fs.RawLocalFileSystem.deprecatedGetFileStatus(RawLocalFileSystem.java:609)
          at org.apache.hadoop.fs.RawLocalFileSystem.getFileLinkStatusInternal(RawLocalFileSystem.java:822)
          at org.apache.hadoop.fs.RawLocalFileSystem.getFileStatus(RawLocalFileSystem.java:599)
          at org.apache.hadoop.fs.FilterFileSystem.getFileStatus(FilterFileSystem.java:421)
          at org.apache.hadoop.fs.ChecksumFileSystem$ChecksumFSInputChecker.(ChecksumFileSystem.java:140)
          at org.apache.hadoop.fs.ChecksumFileSystem.open(ChecksumFileSystem.java:341)
          at org.apache.hadoop.fs.FileSystem.open(FileSystem.java:767)
          at org.apache.hadoop.mapred.LineRecordReader.(LineRecordReader.java:109)
          at org.apache.hadoop.mapred.TextInputFormat.getRecordReader(TextInputFormat.java:67)
          at org.apache.spark.rdd.HadoopRDD$$anon$1.(HadoopRDD.scala:246)
          at org.apache.spark.rdd.HadoopRDD.compute(HadoopRDD.scala:209)
          at org.apache.spark.rdd.HadoopRDD.compute(HadoopRDD.scala:102)
          at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:319)
          at org.apache.spark.rdd.RDD.iterator(RDD.scala:283)
          at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:38)
          at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:319)
          at org.apache.spark.rdd.RDD.iterator(RDD.scala:283)
          at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:70)
          at org.apache.spark.scheduler.Task.run(Task.scala:85)
          at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:274)
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
          at java.lang.Thread.run(Thread.java:745)

Driver stacktrace:   at
  org.apache.spark.scheduler.DAGScheduler.org$apache$spark$scheduler$DAGScheduler$$failJobAndIndependentStages(DAGScheduler.scala:1450)
  at
  org.apache.spark.scheduler.DAGScheduler$$anonfun$abortStage$1.apply(DAGScheduler.scala:1438)
  at
  org.apache.spark.scheduler.DAGScheduler$$anonfun$abortStage$1.apply(DAGScheduler.scala:1437)
  at
  scala.collection.mutable.ResizableArray$class.foreach(ResizableArray.scala:59)
  at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:48)
  at
  org.apache.spark.scheduler.DAGScheduler.abortStage(DAGScheduler.scala:1437)
  at
  org.apache.spark.scheduler.DAGScheduler$$anonfun$handleTaskSetFailed$1.apply(DAGScheduler.scala:811)
  at
  org.apache.spark.scheduler.DAGScheduler$$anonfun$handleTaskSetFailed$1.apply(DAGScheduler.scala:811)
  at scala.Option.foreach(Option.scala:257)   at
  org.apache.spark.scheduler.DAGScheduler.handleTaskSetFailed(DAGScheduler.scala:811)
  at
  org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.doOnReceive(DAGScheduler.scala:1659)
  at
  org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.onReceive(DAGScheduler.scala:1618)
  at
  org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.onReceive(DAGScheduler.scala:1607)
  at org.apache.spark.util.EventLoop$$anon$1.run(EventLoop.scala:48)

  at
  org.apache.spark.scheduler.DAGScheduler.runJob(DAGScheduler.scala:632)
  at org.apache.spark.SparkContext.runJob(SparkContext.scala:1871)   at
  org.apache.spark.SparkContext.runJob(SparkContext.scala:1884)   at
  org.apache.spark.SparkContext.runJob(SparkContext.scala:1897)   at
  org.apache.spark.SparkContext.runJob(SparkContext.scala:1911)   at
  org.apache.spark.rdd.RDD$$anonfun$collect$1.apply(RDD.scala:893)   at
  org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
  at
  org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:112)
  at org.apache.spark.rdd.RDD.withScope(RDD.scala:358)   at
  org.apache.spark.rdd.RDD.collect(RDD.scala:892)   ... 48 elided Caused
  by: java.io.FileNotFoundException: File file:/home/spark/data.txt does not exist
  at
  org.apache.hadoop.fs.RawLocalFileSystem.deprecatedGetFileStatus(RawLocalFileSystem.java:609)
  at
  org.apache.hadoop.fs.RawLocalFileSystem.getFileLinkStatusInternal(RawLocalFileSystem.java:822)
  at
  org.apache.hadoop.fs.RawLocalFileSystem.getFileStatus(RawLocalFileSystem.java:599)
  at
  org.apache.hadoop.fs.FilterFileSystem.getFileStatus(FilterFileSystem.java:421)
  at
  org.apache.hadoop.fs.ChecksumFileSystem$ChecksumFSInputChecker.(ChecksumFileSystem.java:140)
  at
  org.apache.hadoop.fs.ChecksumFileSystem.open(ChecksumFileSystem.java:341)
  at org.apache.hadoop.fs.FileSystem.open(FileSystem.java:767)   at
  org.apache.hadoop.mapred.LineRecordReader.(LineRecordReader.java:109)
  at
  org.apache.hadoop.mapred.TextInputFormat.getRecordReader(TextInputFormat.java:67)
  at org.apache.spark.rdd.HadoopRDD$$anon$1.(HadoopRDD.scala:246)
  at org.apache.spark.rdd.HadoopRDD.compute(HadoopRDD.scala:209)   at
  org.apache.spark.rdd.HadoopRDD.compute(HadoopRDD.scala:102)   at
  org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:319)   at
  org.apache.spark.rdd.RDD.iterator(RDD.scala:283)   at
  org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:38)
  at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:319)

  at org.apache.spark.rdd.RDD.iterator(RDD.scala:283)   at
  org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:70)

  at org.apache.spark.scheduler.Task.run(Task.scala:85)   at
  org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:274)
  at
  java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
  at
  java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
  at java.lang.Thread.run(Thread.java:745)

Apparently there is a file in this path. If I use wrong path, then the error is like below.

```<code> val data = sc.textFile("file:///data.txt").collect
</code>```

org.apache.hadoop.mapred.InvalidInputException: Input path does not
  exist: file:/data.txt   at
  org.apache.hadoop.mapred.FileInputFormat.singleThreadedListStatus(FileInputFormat.java:287)
  at
  org.apache.hadoop.mapred.FileInputFormat.listStatus(FileInputFormat.java:229)
  at
  org.apache.hadoop.mapred.FileInputFormat.getSplits(FileInputFormat.java:315)
  at org.apache.spark.rdd.HadoopRDD.getPartitions(HadoopRDD.scala:200)

  at org.apache.spark.rdd.RDD$$anonfun$partitions$2.apply(RDD.scala:248)
  at org.apache.spark.rdd.RDD$$anonfun$partitions$2.apply(RDD.scala:246)
  at scala.Option.getOrElse(Option.scala:121)   at
  org.apache.spark.rdd.RDD.partitions(RDD.scala:246)   at
  org.apache.spark.rdd.MapPartitionsRDD.getPartitions(MapPartitionsRDD.scala:35)
  at org.apache.spark.rdd.RDD$$anonfun$partitions$2.apply(RDD.scala:248)
  at org.apache.spark.rdd.RDD$$anonfun$partitions$2.apply(RDD.scala:246)
  at scala.Option.getOrElse(Option.scala:121)   at
  org.apache.spark.rdd.RDD.partitions(RDD.scala:246)   at
  org.apache.spark.SparkContext.runJob(SparkContext.scala:1911)   at
  org.apache.spark.rdd.RDD$$anonfun$collect$1.apply(RDD.scala:893)   at
  org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
  at
  org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:112)
  at org.apache.spark.rdd.RDD.withScope(RDD.scala:358)   at
  org.apache.spark.rdd.RDD.collect(RDD.scala:892)

I don't know why it doesn't work.

Any ideas?

答:
The Problem is our local is different from spark local. so when you run your pyspark, it's mandatory to mention your code must be run in your local machine, especially when you use AWS EC2. So simply run
  ./pyspark --master local[n]
after that your local and spark local will be the same.....
don't forget to use(file:///....)</p>

Body:
I've been trying to load local file using sc.textFile()in spark.

I already read [question]:How to load local file in sc.textFile, instead of HDFS

I have local file in /home/spark/data.txt on Centos 7.0

When I use val data = sc.textFile("file:///home/spark/data.txt").collect, I got a error as below.

16/12/27 12:15:56 WARN TaskSetManager: Lost task 0.0 in stage 5.0 (TID
  36,): java.io.FileNotFoundException: File file:/home/spark/data.txt does not
  exist
          at org.apache.hadoop.fs.RawLocalFileSystem.deprecatedGetFileStatus(RawLocalFileSystem.java:609)
          at org.apache.hadoop.fs.RawLocalFileSystem.getFileLinkStatusInternal(RawLocalFileSystem.java:822)
          at org.apache.hadoop.fs.RawLocalFileSystem.getFileStatus(RawLocalFileSystem.java:599)
          at org.apache.hadoop.fs.FilterFileSystem.getFileStatus(FilterFileSystem.java:421)
          at org.apache.hadoop.fs.ChecksumFileSystem$ChecksumFSInputChecker.(ChecksumFileSystem.java:140)
          at org.apache.hadoop.fs.ChecksumFileSystem.open(ChecksumFileSystem.java:341)
          at org.apache.hadoop.fs.FileSystem.open(FileSystem.java:767)
          at org.apache.hadoop.mapred.LineRecordReader.(LineRecordReader.java:109)
          at org.apache.hadoop.mapred.TextInputFormat.getRecordReader(TextInputFormat.java:67)
          at org.apache.spark.rdd.HadoopRDD$$anon$1.(HadoopRDD.scala:246)
          at org.apache.spark.rdd.HadoopRDD.compute(HadoopRDD.scala:209)
          at org.apache.spark.rdd.HadoopRDD.compute(HadoopRDD.scala:102)
          at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:319)
          at org.apache.spark.rdd.RDD.iterator(RDD.scala:283)
          at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:38)
          at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:319)
          at org.apache.spark.rdd.RDD.iterator(RDD.scala:283)
          at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:70)
          at org.apache.spark.scheduler.Task.run(Task.scala:85)
          at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:274)
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
          at java.lang.Thread.run(Thread.java:745)

16/12/27 12:15:56 ERROR TaskSetManager: Task 0 in stage 5.0 failed 4
  times; aborting job org.apache.spark.SparkException: Job aborted due
  to stage failure: Task 0 in stage 5.0 failed 4 times, most recent
  failure: Lost task 0.3 in stage 5.0 (TID 42,):
  java.io.FileNotFoundException: File file:/home/spark/data.txt does not exist
          at org.apache.hadoop.fs.RawLocalFileSystem.deprecatedGetFileStatus(RawLocalFileSystem.java:609)
          at org.apache.hadoop.fs.RawLocalFileSystem.getFileLinkStatusInternal(RawLocalFileSystem.java:822)
          at org.apache.hadoop.fs.RawLocalFileSystem.getFileStatus(RawLocalFileSystem.java:599)
          at org.apache.hadoop.fs.FilterFileSystem.getFileStatus(FilterFileSystem.java:421)
          at org.apache.hadoop.fs.ChecksumFileSystem$ChecksumFSInputChecker.(ChecksumFileSystem.java:140)
          at org.apache.hadoop.fs.ChecksumFileSystem.open(ChecksumFileSystem.java:341)
          at org.apache.hadoop.fs.FileSystem.open(FileSystem.java:767)
          at org.apache.hadoop.mapred.LineRecordReader.(LineRecordReader.java:109)
          at org.apache.hadoop.mapred.TextInputFormat.getRecordReader(TextInputFormat.java:67)
          at org.apache.spark.rdd.HadoopRDD$$anon$1.(HadoopRDD.scala:246)
          at org.apache.spark.rdd.HadoopRDD.compute(HadoopRDD.scala:209)
          at org.apache.spark.rdd.HadoopRDD.compute(HadoopRDD.scala:102)
          at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:319)
          at org.apache.spark.rdd.RDD.iterator(RDD.scala:283)
          at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:38)
          at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:319)
          at org.apache.spark.rdd.RDD.iterator(RDD.scala:283)
          at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:70)
          at org.apache.spark.scheduler.Task.run(Task.scala:85)
          at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:274)
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
          at java.lang.Thread.run(Thread.java:745)

Driver stacktrace:   at
  org.apache.spark.scheduler.DAGScheduler.org$apache$spark$scheduler$DAGScheduler$$failJobAndIndependentStages(DAGScheduler.scala:1450)
  at
  org.apache.spark.scheduler.DAGScheduler$$anonfun$abortStage$1.apply(DAGScheduler.scala:1438)
  at
  org.apache.spark.scheduler.DAGScheduler$$anonfun$abortStage$1.apply(DAGScheduler.scala:1437)
  at
  scala.collection.mutable.ResizableArray$class.foreach(ResizableArray.scala:59)
  at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:48)
  at
  org.apache.spark.scheduler.DAGScheduler.abortStage(DAGScheduler.scala:1437)
  at
  org.apache.spark.scheduler.DAGScheduler$$anonfun$handleTaskSetFailed$1.apply(DAGScheduler.scala:811)
  at
  org.apache.spark.scheduler.DAGScheduler$$anonfun$handleTaskSetFailed$1.apply(DAGScheduler.scala:811)
  at scala.Option.foreach(Option.scala:257)   at
  org.apache.spark.scheduler.DAGScheduler.handleTaskSetFailed(DAGScheduler.scala:811)
  at
  org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.doOnReceive(DAGScheduler.scala:1659)
  at
  org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.onReceive(DAGScheduler.scala:1618)
  at
  org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.onReceive(DAGScheduler.scala:1607)
  at org.apache.spark.util.EventLoop$$anon$1.run(EventLoop.scala:48)

  at
  org.apache.spark.scheduler.DAGScheduler.runJob(DAGScheduler.scala:632)
  at org.apache.spark.SparkContext.runJob(SparkContext.scala:1871)   at
  org.apache.spark.SparkContext.runJob(SparkContext.scala:1884)   at
  org.apache.spark.SparkContext.runJob(SparkContext.scala:1897)   at
  org.apache.spark.SparkContext.runJob(SparkContext.scala:1911)   at
  org.apache.spark.rdd.RDD$$anonfun$collect$1.apply(RDD.scala:893)   at
  org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
  at
  org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:112)
  at org.apache.spark.rdd.RDD.withScope(RDD.scala:358)   at
  org.apache.spark.rdd.RDD.collect(RDD.scala:892)   ... 48 elided Caused
  by: java.io.FileNotFoundException: File file:/home/spark/data.txt does not exist
  at
  org.apache.hadoop.fs.RawLocalFileSystem.deprecatedGetFileStatus(RawLocalFileSystem.java:609)
  at
  org.apache.hadoop.fs.RawLocalFileSystem.getFileLinkStatusInternal(RawLocalFileSystem.java:822)
  at
  org.apache.hadoop.fs.RawLocalFileSystem.getFileStatus(RawLocalFileSystem.java:599)
  at
  org.apache.hadoop.fs.FilterFileSystem.getFileStatus(FilterFileSystem.java:421)
  at
  org.apache.hadoop.fs.ChecksumFileSystem$ChecksumFSInputChecker.(ChecksumFileSystem.java:140)
  at
  org.apache.hadoop.fs.ChecksumFileSystem.open(ChecksumFileSystem.java:341)
  at org.apache.hadoop.fs.FileSystem.open(FileSystem.java:767)   at
  org.apache.hadoop.mapred.LineRecordReader.(LineRecordReader.java:109)
  at
  org.apache.hadoop.mapred.TextInputFormat.getRecordReader(TextInputFormat.java:67)
  at org.apache.spark.rdd.HadoopRDD$$anon$1.(HadoopRDD.scala:246)
  at org.apache.spark.rdd.HadoopRDD.compute(HadoopRDD.scala:209)   at
  org.apache.spark.rdd.HadoopRDD.compute(HadoopRDD.scala:102)   at
  org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:319)   at
  org.apache.spark.rdd.RDD.iterator(RDD.scala:283)   at
  org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:38)
  at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:319)

  at org.apache.spark.rdd.RDD.iterator(RDD.scala:283)   at
  org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:70)

  at org.apache.spark.scheduler.Task.run(Task.scala:85)   at
  org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:274)
  at
  java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
  at
  java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
  at java.lang.Thread.run(Thread.java:745)

Apparently there is a file in this path. If I use wrong path, then the error is like below.

 val data = sc.textFile("file:///data.txt").collect

org.apache.hadoop.mapred.InvalidInputException: Input path does not
  exist: file:/data.txt   at
  org.apache.hadoop.mapred.FileInputFormat.singleThreadedListStatus(FileInputFormat.java:287)
  at
  org.apache.hadoop.mapred.FileInputFormat.listStatus(FileInputFormat.java:229)
  at
  org.apache.hadoop.mapred.FileInputFormat.getSplits(FileInputFormat.java:315)
  at org.apache.spark.rdd.HadoopRDD.getPartitions(HadoopRDD.scala:200)

  at org.apache.spark.rdd.RDD$$anonfun$partitions$2.apply(RDD.scala:248)
  at org.apache.spark.rdd.RDD$$anonfun$partitions$2.apply(RDD.scala:246)
  at scala.Option.getOrElse(Option.scala:121)   at
  org.apache.spark.rdd.RDD.partitions(RDD.scala:246)   at
  org.apache.spark.rdd.MapPartitionsRDD.getPartitions(MapPartitionsRDD.scala:35)
  at org.apache.spark.rdd.RDD$$anonfun$partitions$2.apply(RDD.scala:248)
  at org.apache.spark.rdd.RDD$$anonfun$partitions$2.apply(RDD.scala:246)
  at scala.Option.getOrElse(Option.scala:121)   at
  org.apache.spark.rdd.RDD.partitions(RDD.scala:246)   at
  org.apache.spark.SparkContext.runJob(SparkContext.scala:1911)   at
  org.apache.spark.rdd.RDD$$anonfun$collect$1.apply(RDD.scala:893)   at
  org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
  at
  org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:112)
  at org.apache.spark.rdd.RDD.withScope(RDD.scala:358)   at
  org.apache.spark.rdd.RDD.collect(RDD.scala:892)

I don't know why it doesn't work.

Any ideas?


问:
How to send Image and Text with JQuery AJAX
My Code like below, that is a form to input a Text and Image.

```<code><div id="encrypt_function" style="display:none">
        <div class="form-group">
            <label for="gambar">Gambar:</label>
            <input type="file" accept=="image/*" name="gambar" id="gambar">
        </div>
        <div class="form-group">
            <label for="plaintext">Plaintext:</label>
            <input type="text" class="form-control" placeholder="Message to Encryption" id="plaintext">
        </div>
        <button type="submit" class="btn btn-primary" id="submit_encrypt">Submit</button>
        <div class="form-group" id="pk_result" style="display:none">
            <label for="Public Key Result">Public Key</label>
            <textarea class="form-control" id="pkvalue" rows="3"></textarea>
        </div>
    </div>
</code>```

Then the Ajax JQuery script like below, I Wanted to send Image and Text for 1 Ajax Syntax.

```<code>$('#submit_encrypt').click(function(){
                var plaintext = $('#plaintext').val();
                var file_data = $('#gambar').prop('files')[0];
                var gambar = new FormData();
                gambar.append('file', file_data);
                $.ajax({
                    url: 'encrypt.php', // point to server-side controller method
                    dataType: 'text', // what to expect back from the server
                    cache: false,
                    contentType: false,
                    processData: false,
                    data: {gambar, 'plaintext' : plaintext},
                    type: 'POST',
                    beforeSend: function(){

                    },
                    success: function (data) {
                        $("#pk_result").html(data);
                        $("#pk_result").show();
                    }
                });

            });
</code>```

Then in encrypt.php is like

```<code>$img_tmp = $_FILES['gambar']['tmp_name'];
$img_name = $_FILES['gambar']['name'];
move_uploaded_file($img_tmp, 'Gambar/'.$img_name);
</code>```

But it doesn't work for upload the Image in local directory for test. How does the right codes?

The error is

```<code>Notice: Undefined index: gambar in C:\xampp\htdocs\Tugas Akhir\encrypt.php on line 7

Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\Tugas Akhir\encrypt.php on line 7

Notice: Undefined index: gambar in C:\xampp\htdocs\Tugas Akhir\encrypt.php on line 8

Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\Tugas Akhir\encrypt.php on line 8
</code>```

答:
I believe you could do it like this :

```<code>$( 'form' ).submit(function ( e ) {
    var data;

var fd = new FormData();
fd.append( 'file', input.files[0] );

$.ajax({
  url: 'http://example.com/script.php',
  data: fd,
  processData: false,
  contentType: false,
  type: 'POST',
  success: function(data){
    alert(data);
  }
});
e.preventDefault();
</code>```

});

Setting processData to false lets you prevent jQuery from automatically transforming the data into a query string. See the docs for more info.
Setting the contentType to false is imperative, since otherwise jQuery will set it incorrectly.

Body:
My Code like below, that is a form to input a Text and Image.

            Gambar:
            <input type="file" accept=="image/*" name="gambar" id="gambar">

            Plaintext:
            <input type="text" class="form-control" placeholder="Message to Encryption" id="plaintext">

        <button type="submit" class="btn btn-primary" id="submit_encrypt">Submit</button>

            Public Key
            <textarea class="form-control" id="pkvalue" rows="3"></textarea>

Then the Ajax JQuery script like below, I Wanted to send Image and Text for 1 Ajax Syntax.

$('#submit_encrypt').click(function(){
                var plaintext = $('#plaintext').val();
                var file_data = $('#gambar').prop('files')[0];
                var gambar = new FormData();
                gambar.append('file', file_data);
                $.ajax({
                    url: 'encrypt.php', // point to server-side controller method
                    dataType: 'text', // what to expect back from the server
                    cache: false,
                    contentType: false,
                    processData: false,
                    data: {gambar, 'plaintext' : plaintext},
                    type: 'POST',
                    beforeSend: function(){

                    },
                    success: function (data) {
                        $("#pk_result").html(data);
                        $("#pk_result").show();
                    }
                });

            });

Then in encrypt.php is like

$img_tmp = $_FILES['gambar']['tmp_name'];
$img_name = $_FILES['gambar']['name'];
move_uploaded_file($img_tmp, 'Gambar/'.$img_name);

But it doesn't work for upload the Image in local directory for test. How does the right codes?

The error is

Notice: Undefined index: gambar in C:\xampp\htdocs\Tugas Akhir\encrypt.php on line 7

Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\Tugas Akhir\encrypt.php on line 7

Notice: Undefined index: gambar in C:\xampp\htdocs\Tugas Akhir\encrypt.php on line 8

Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\Tugas Akhir\encrypt.php on line 8

Body:
I believe you could do it like this :

$( 'form' ).submit(function ( e ) {
    var data;

var fd = new FormData();
fd.append( 'file', input.files[0] );

$.ajax({
  url: 'http://example.com/script.php',
  data: fd,
  processData: false,
  contentType: false,
  type: 'POST',
  success: function(data){
    alert(data);
  }
});
e.preventDefault();

});

Setting processData to false lets you prevent jQuery from automatically transforming the data into a query string. See the docs for more info.
Setting the contentType to false is imperative, since otherwise jQuery will set it incorrectly.


答:
When you get notified by your delegate/Observer for the network disable state, you could show message in UIAlertView.

```<code> //show an alert to let the user know that they can't connect...
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Network Status" message:@"Network is not available. Please try again later." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
        [alert release];
</code>```

you could also use the Reachability API's to know the state of network connection.

iPhone SDK: Testing Network Reachability

Title: how to display alert when networking connection is disable?

Body:
i am new to iphone in my app i am connecting to database through web services it working correctly,but i want to displat alert  when network connection is disable.please any help me how to alert when net connection is disable,
  thanks in advance.

Body:
When you get notified by your delegate/Observer for the network disable state, you could show message in UIAlertView.

 //show an alert to let the user know that they can't connect...
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Network Status" message:@"Network is not available. Please try again later." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
        [alert release];

you could also use the Reachability API's to know the state of network connection.

iPhone SDK: Testing Network Reachability


问:
How do you get the size of array that is passed into the function?
I am trying to write a function that prints out the elements in an array. However when I work with the arrays that are passed, I don't know how to iterate over the array.

```<code>void
print_array(int* b)
{
  int sizeof_b = sizeof(b) / sizeof(b[0]);
  int i;
  for (i = 0; i < sizeof_b; i++)
    {
      printf("%d", b[i]);
    }
}
</code>```

What is the best way to do iterate over the passed array?

答:
Use variable to pass the size of array.
int sizeof_b = sizeof(b) / sizeof(b[0]); does nothing but getting the pre-declared array size, which is known, and you could have passed it as an argument; for instance, void print_array(int*b, int size). size could be the user-defined size too.

int sizeof_b = sizeof(b) / sizeof(b[0]); will cause redundant iteration when the number of elements is less than the pre-declared array-size.

Body:
I am trying to write a function that prints out the elements in an array. However when I work with the arrays that are passed, I don't know how to iterate over the array.

void
print_array(int* b)
{
  int sizeof_b = sizeof(b) / sizeof(b[0]);
  int i;
  for (i = 0; i < sizeof_b; i++)
    {
      printf("%d", b[i]);
    }
}

What is the best way to do iterate over the passed array?


问:
Laravel Distinct Count
Any way to make this query work using laravel? DB::raw or Eloquent usage doesn't matter.

```<code>SELECT count(DISTINCT name) FROM tablename;
</code>```

Here's what i've tried but cannot get the proper output:

```<code>EloquentTableName::select(DB::raw('count(DISTINCT name) as name_count'))->get();
</code>```

This returns something like this and i'd like to fix that:

```<code>([{"name_count":"15"}])
</code>```

I just want to get count 15.

答:
You may simply do the following:</p>

```<code>Tablename::distinct()->count('name');
</code>```

Title: Laravel Distinct Count

Body:
Any way to make this query work using laravel? DB::raw or Eloquent usage doesn't matter.

SELECT count(DISTINCT name) FROM tablename;

Here's what i've tried but cannot get the proper output:

EloquentTableName::select(DB::raw('count(DISTINCT name) as name_count'))->get();

This returns something like this and i'd like to fix that:

([{"name_count":"15"}])

I just want to get count 15.

Body:
You may simply do the following:

Tablename::distinct()->count('name');


问:
On the issues of scaling Firebase Backend
I am starting development on a backend system. It will be supporting cross platform mobile apps. The backend has many functionalities which, prompted me to divide the backend into 8 services (deployed as Cloud Functions) and exposed as a REST API for clients. Each of the deployed function will be using firestore and will strictly query collections relating to that particular service. Inter service communication over HTTP is strictly prohibited and all such communications have been restricted to Cloud Messaging.

Now, I have been told recently that Firebase will be a "bottleneck" and will not be able to handle scaling. I am pretty confident that scaling will not be an issue since, scaling a service will simply mean multiplying the number of deployed function instance. The same goes for database (Firestore). From a general perspective, it is safe for me say that Firebase will be scalable? I know there are lot of challenges and it's a broad question. However, such challenges will even exist when I go for my own setup on a VPS. So, to clarify my question: from the perspective of a backend that have many services developed as REST APIs. is Firebase a scalable option? Any suggestions, references or, guidelines will be highly appreciated.

答:
What you're asking about is not really about "Firebase" (which is a mobile application development platform), but more about Cloud Functions, which is a scalable serverless Google Cloud product.  Firebase just adds tools and APIs on top of the core Cloud Functions product.  Otherwise, the code deployed by the Firebase CLI behaves exactly the same as if you deployed it using Google Cloud tools.  Read more about the relationship between Firebase and Google Cloud with respect to Cloud Functions in this article.

The same is true for Firestore, which is also a Google Cloud product.  It scales massively, and you can read about its known limitations.  Read a similar blog about the differences between Firebase and Google Cloud with respect to Firestore.

There is a good amount of documentation about how Cloud Functions scales.  For HTTP type functions, your limits are based on the volume of of data you are sending out of your function instances.  You can read about the limits in the Cloud documentation.  Practically speaking, I've never heard of anyone exceeding the limits of Cloud Functions for the sort of work that it's meant to do (which is to say, work that is time-boxed to the max configurable 9 minute limit, at the memory limits of the class of machine being used).

Again, none of this has to do with Firebase or of any of its other, many, products.  All you need to understand is the behavior of the individual products you want to use, both of which are Google Cloud products.

Whoever told you that you'd have problems scaling with "Firebase" was probably referring to the product Firebase Realtime Database, which does have limitations that require the use of sharding if you want to scale up massively.  Firestore does not have that limitation - you don't have to do anything to scale it.


答:
Angular has a inbuilt pipe for this

```<code>{{name | titlecase}}
</code>```

link

https://angular.io/api/common/TitleCasePipe

Body:
Angular has a inbuilt pipe for this

{{name | titlecase}}

link

https://angular.io/api/common/TitleCasePipe


问:
Using thor for complex command line tool
i want to create a command line tool in Ruby using Thor. This tool should be packaged as a gem so that it is easily installed and uninstalled.

Creating and publishing the gem, I have done. I also created several Thor scripts which also work. However, I do not know how to combine them.

My aim is to be able to call my tool the following way:
mytool task param --options
mytool taskgroup:task param --options

I know how to make one Thor script to be executable. However, how do I make a bunch of thor scripts accessible throw one command?

答:
According to the relevant Gem documentation, you could specify (in your .gemspec):

```<code>spec.executables = ['bin/foo', 'bin/bar']
spec.default_executable = 'bin/bar'
</code>```

and have your gem install a bunch of executables (foo and bar). Or you write a wrapper for all your Thor scripts and specify:

```<code>spec.executables = ['bin/wrapper']
</code>```

and have your gem install only one executable (wrapper).

Body:
According to the relevant Gem documentation, you could specify (in your .gemspec):

spec.executables = ['bin/foo', 'bin/bar']
spec.default_executable = 'bin/bar'

and have your gem install a bunch of executables (foo and bar). Or you write a wrapper for all your Thor scripts and specify:

spec.executables = ['bin/wrapper']

and have your gem install only one executable (wrapper).


问:
How can I calculate delta at dataframe?
```<code>A   B   C   Delta
**1 Jan 10  0**
1   Feb 20  10
1   Mar 40  30
**2 Jan 10  0**
2   Feb 30  20
2   Mar 20  10
2   Oct 40  30
**3 Jan 10  0**
3   Feb 20  10
3   Mar 30  20
3   Oct 40  30
3   Dec 50  40
</code>```

how can I calculate delta column?

I couldn't find it anywhere.
Please let me know. how to calculate

答:
Subtract column C by Series.sub with repeated first values per groups by GroupBy.transform and GroupBy.first:

```<code>df['Delta'] = df['C'].sub(df.groupby('A')['C'].transform('first'))
print (df)
    A    B   C  Delta
0   1  Jan  10      0
1   1  Feb  20     10
2   1  Mar  40     30
3   2  Jan  10      0
4   2  Feb  30     20
5   2  Mar  20     10
6   2  Oct  40     30
7   3  Jan  10      0
8   3  Feb  20     10
9   3  Mar  30     20
10  3  Oct  40     30
11  3  Dec  50     40
</code>```

Detail:

```<code>print (df.groupby('A')['C'].transform('first'))
0     10
1     10
2     10
3     10
4     10
5     10
6     10
7     10
8     10
9     10
10    10
11    10
Name: C, dtype: int64
</code>```

Title: How can I calculate delta at dataframe?

Body:
A   B   C   Delta
**1 Jan 10  0**
1   Feb 20  10
1   Mar 40  30
**2 Jan 10  0**
2   Feb 30  20
2   Mar 20  10
2   Oct 40  30
**3 Jan 10  0**
3   Feb 20  10
3   Mar 30  20
3   Oct 40  30
3   Dec 50  40

how can I calculate delta column?

I couldn't find it anywhere.
Please let me know. how to calculate

Body:
Subtract column C by Series.sub with repeated first values per groups by GroupBy.transform and GroupBy.first:

df['Delta'] = df['C'].sub(df.groupby('A')['C'].transform('first'))
print (df)
    A    B   C  Delta
0   1  Jan  10      0
1   1  Feb  20     10
2   1  Mar  40     30
3   2  Jan  10      0
4   2  Feb  30     20
5   2  Mar  20     10
6   2  Oct  40     30
7   3  Jan  10      0
8   3  Feb  20     10
9   3  Mar  30     20
10  3  Oct  40     30
11  3  Dec  50     40

Detail:

print (df.groupby('A')['C'].transform('first'))
0     10
1     10
2     10
3     10
4     10
5     10
6     10
7     10
8     10
9     10
10    10
11    10
Name: C, dtype: int64


问:
CSS Responsive image "shows up fully" on-screen no-stretch background with precise text placement

Testing

```

I'm trying to have an image fully show up based on the size of a screen, and to have text ("Testing" in the textbox class) show up in a precise designated area in the image, as shown above.

Trying to get the above to work with this codepen, but I am defeated to admit that after an hour of fiddling with css, I am nowhere close.

It is pretty frustrating that css doesn't seem to work as expected, where the image doesn't seem to want to nest to full height etc.

答:
I would like to suggest if you add image using img HTML tag you have better control on image in relation with "Testing" text. Please check below my snippet. You can adjust position of "Testing" by "top" position on ".textbox" class :

.container{
        min-height: 100%;
        margin: 0;
        padding: 0;
      }
      .fullscreen{
        width: auto;
        height: 100%;
        margin: 0;
        padding: 0;
        position:relative;
      }

      .textbox{
        position:absolute;
        top:55%;
        left: 50%;
        -webkit-transform: translateX(-50%);
        transform: translateX(-50%);
        z-index:3;
        text-align:center;
      }```
<pre class="snippet-code-html lang-html prettyprint-override"><code><div class="container">
  <div class="fullscreen">
    <img src="http://print.drawmaticar.com/preview.jpg"   style="width:100%;"/>

    <div class="textbox">Testing</div>

  </div>
</div></code>```

Body:
Testing

I'm trying to have an image fully show up based on the size of a screen, and to have text ("Testing" in the textbox class) show up in a precise designated area in the image, as shown above.

Trying to get the above to work with this codepen, but I am defeated to admit that after an hour of fiddling with css, I am nowhere close.

It is pretty frustrating that css doesn't seem to work as expected, where the image doesn't seem to want to nest to full height etc.

Body:
I would like to suggest if you add image using img HTML tag you have better control on image in relation with "Testing" text. Please check below my snippet. You can adjust position of "Testing" by "top" position on ".textbox" class :

.container{
        min-height: 100%;
        margin: 0;
        padding: 0;
      }
      .fullscreen{
        width: auto;
        height: 100%;
        margin: 0;
        padding: 0;
        position:relative;
      }

      .textbox{
        position:absolute;
        top:55%;
        left: 50%;
        -webkit-transform: translateX(-50%);
        transform: translateX(-50%);
        z-index:3;
        text-align:center;
      }

<div class="container">
  <div class="fullscreen">
    <img src="http://print.drawmaticar.com/preview.jpg"   style="width:100%;"/>

    <div class="textbox">Testing</div>

  </div>
</div>


问:
Using SQL to exclude a word that comes after a phrase
This issue might be a bit too complicated for SQL to handle, but since I have pretty intermediate SQL skills perhaps someone more well-versed in SQL than me will know how to fix the below issue.

I am looking to isolate observations that mention a keyword that occur after a phrase. An example would be like below: For variable FREE_TEXT I want to isolate where the word orange is said AFTER the phrase Excluding This:

FREE_TEXT=
"Including This:

The best fruit is an apple

Excluding This:

The worst fruit is an orange"

Technically I could just do where FREE_TEXT not like '%orange%' but that is not what I am looking for. I am looking to specifically isolate for when a word is said AFTER the phrase Excluding This.

Anyone have ideas?

答:
If I understand your sample data correct, this following logic should help you getting your expected output-

DEMO HERE

```<code>WITH your_table (FREE_TEXT)
AS
(
    SELECT 'T Including This: The best fruit is an apple' UNION ALL
    SELECT 'Including This: The best fruit is an orange' UNION ALL
    SELECT 'Excluding This: The best fruit is an apple' UNION ALL
    SELECT 'T Excluding This: The worst fruit is an orange'
)

SELECT *
FROM your_table
WHERE strpos(FREE_TEXT, 'Excluding This:') = 0
OR strpos(FREE_TEXT, 'Excluding This:') > strpos(FREE_TEXT, 'orange')
</code>```

Body:
If I understand your sample data correct, this following logic should help you getting your expected output-

DEMO HERE

WITH your_table (FREE_TEXT)
AS
(
    SELECT 'T Including This: The best fruit is an apple' UNION ALL
    SELECT 'Including This: The best fruit is an orange' UNION ALL
    SELECT 'Excluding This: The best fruit is an apple' UNION ALL
    SELECT 'T Excluding This: The worst fruit is an orange'
)

SELECT *
FROM your_table
WHERE strpos(FREE_TEXT, 'Excluding This:') = 0
OR strpos(FREE_TEXT, 'Excluding This:') > strpos(FREE_TEXT, 'orange')