问:
Different results with observed=True/False in pandas groupby
I am getting different results when using groupby in pandas after using observed=True/False in a dataframe with a categorical column with intervals. In principle, I believe I should have exactly the same results.

As an example, let's assume the following dataframe:

    df_testing = pd.DataFrame({"a": ["good", "good", "good", "bad", "good", "good", "bad", "good",
                                "good", "good"],
                          "b": [1, 1, 2, 2, 3, 4, 5, 6, 11111, -5455]})
```

<p>I transform the "b" column so to group the values into different intervals. I also force the "a" column to be categorical:</p>

<pre class="lang-py prettyprint-override"><code>    df_testing["a"] = df_testing["a"].astype("category")
    df_testing["b"] = pd.cut(df_testing["b"], [-9999, 0, 2, 5, 1e99], right=True)
</code>```

If I put the observed = False, the results are correct:

    In[310]: df_testing.groupby(by="b", observed=False)["a"].value_counts()

    Out[310]:
    b               a
    (-9999.0, 0.0]  good    1
    (0.0, 2.0]      good    3
                    bad     1
    (2.0, 5.0]      good    2
                    bad     1
    (5.0, 1e+99]    good    2
    Name: a, dtype: int64
```

<p>But for observed = True:</p>

<pre class="lang-py prettyprint-override"><code>    In[311]: df_testing.groupby(by="b", observed=True)["a"].value_counts()

    Out[311]:
    b               a
    (0.0, 2.0]      good    1
    (2.0, 5.0]      good    3
                    bad     1
    (5.0, 1e+99]    good    2
                    bad     1
    (-9999.0, 0.0]  good    2
    Name: a, dtype: int64
</code>```

As you can see, the counting is the same... But the labels for the b column in the second case are wrong!

I am using pandas v0.24.2 (latest stable version)

答:
This is a bug in pandas that has been fixed for the upcoming 0.25.0 release:</p>

```<code>In [1]: import pandas as pd; pd.__version__
Out[1]: '0.25.0.dev0+596.g20d0ad159a'

In [2]: df_testing = pd.DataFrame({"a": ["good", "good", "good", "bad", "good", "good",
   ...:                                  "bad", "good", "good", "good"],
   ...:                            "b": [1, 1, 2, 2, 3, 4, 5, 6, 11111, -5455]})

In [3]: df_testing["a"] = df_testing["a"].astype("category")

In [4]: df_testing["b"] = pd.cut(df_testing["b"], [-9999, 0, 2, 5, 1e99], right=True)

In [5]: df_testing.groupby(by="b", observed=False)["a"].value_counts()
Out[5]:
b               a
(-9999.0, 0.0]  good    1
(0.0, 2.0]      good    3
                bad     1
(2.0, 5.0]      good    2
                bad     1
(5.0, 1e+99]    good    2
Name: a, dtype: int64

In [6]: df_testing.groupby(by="b", observed=True)["a"].value_counts()
Out[6]:
b               a
(-9999.0, 0.0]  good    1
(0.0, 2.0]      good    3
                bad     1
(2.0, 5.0]      good    2
                bad     1
(5.0, 1e+99]    good    2
Name: a, dtype: int64
</code>```

Body:
I am getting different results when using groupby in pandas after using observed=True/False in a dataframe with a categorical column with intervals. In principle, I believe I should have exactly the same results.

As an example, let's assume the following dataframe:

    df_testing = pd.DataFrame({"a": ["good", "good", "good", "bad", "good", "good", "bad", "good",
                                "good", "good"],
                          "b": [1, 1, 2, 2, 3, 4, 5, 6, 11111, -5455]})

I transform the "b" column so to group the values into different intervals. I also force the "a" column to be categorical:

    df_testing["a"] = df_testing["a"].astype("category")
    df_testing["b"] = pd.cut(df_testing["b"], [-9999, 0, 2, 5, 1e99], right=True)

If I put the observed = False, the results are correct:

    In[310]: df_testing.groupby(by="b", observed=False)["a"].value_counts()

    Out[310]:
    b               a
    (-9999.0, 0.0]  good    1
    (0.0, 2.0]      good    3
                    bad     1
    (2.0, 5.0]      good    2
                    bad     1
    (5.0, 1e+99]    good    2
    Name: a, dtype: int64

But for observed = True:

    In[311]: df_testing.groupby(by="b", observed=True)["a"].value_counts()

    Out[311]:
    b               a
    (0.0, 2.0]      good    1
    (2.0, 5.0]      good    3
                    bad     1
    (5.0, 1e+99]    good    2
                    bad     1
    (-9999.0, 0.0]  good    2
    Name: a, dtype: int64

As you can see, the counting is the same... But the labels for the b column in the second case are wrong!

I am using pandas v0.24.2 (latest stable version)

Body:
This is a bug in pandas that has been fixed for the upcoming 0.25.0 release:

In [1]: import pandas as pd; pd.__version__
Out[1]: '0.25.0.dev0+596.g20d0ad159a'

In [2]: df_testing = pd.DataFrame({"a": ["good", "good", "good", "bad", "good", "good",
   ...:                                  "bad", "good", "good", "good"],
   ...:                            "b": [1, 1, 2, 2, 3, 4, 5, 6, 11111, -5455]})

In [3]: df_testing["a"] = df_testing["a"].astype("category")

In [4]: df_testing["b"] = pd.cut(df_testing["b"], [-9999, 0, 2, 5, 1e99], right=True)

In [5]: df_testing.groupby(by="b", observed=False)["a"].value_counts()
Out[5]:
b               a
(-9999.0, 0.0]  good    1
(0.0, 2.0]      good    3
                bad     1
(2.0, 5.0]      good    2
                bad     1
(5.0, 1e+99]    good    2
Name: a, dtype: int64

In [6]: df_testing.groupby(by="b", observed=True)["a"].value_counts()
Out[6]:
b               a
(-9999.0, 0.0]  good    1
(0.0, 2.0]      good    3
                bad     1
(2.0, 5.0]      good    2
                bad     1
(5.0, 1e+99]    good    2
Name: a, dtype: int64


来源: stackexchange

问:
What prefix to use when indexing SecureRandom string column
I have a DB column (type varchar(255)) that stores a URL-safe base 64 string produced by SecureRandom.urlsafe_base64. The method call uses the default values, so the result should be 16 bytes or 22 characters in length.

The base64 value is used to look up records when a user hits the site, to mask DB IDs. I need an index for this column because of this lookup, however I don't want to index the entire column because that could be inefficient storage-wise.

What's the best approach to determining the optimal index prefix to use in this case? What I'm thinking right now is something like this:

Create sample data with about 100k records to simulate production data

Add an index for the base 64 column with a prefix (say 8 characters)

Run EXPLAIN on a lookup on the base 64 column to see how many
records need to be inspected

Adjust the index up or down and re-do step 3.

Choose a prefix size that balances (a) storage requirements and (b) number of records returned with matching hits.

The issue here is that I know SecureRandom produces unique base 64 strings, but I'm not sure how unique they are. Eg, out of 100k records, if I use a prefix of 8 characters, will that prefix be shared by 10 records or 100?

Some more specific questions about my approach:

Is 100k records enough of a sample to pick a good prefix size?

If I did apply an index without using a prefix, is my suspicion that this is bad storage-wise correct?

What's a reasonable number of records to expect to have to query the table directly, and still benefit from an index?

Notes:

My database is MySQL (actually Percona)

SecureRandom comes from Ruby

I'm assuming that the URL-safe feature of SecureRandom isn't changing the uniqueness characteristics of the base 64 output.

答:
This is just a random number, correct?  Not encryption.

Do not use a prefix; although it will shrink the index size some, it will invalidate the usage of the index in many cases.  True, 22 bytes is longer than an 8-char string or a 4-byte INT.  But the downside of failure to use the index is worse.

The default of 16 (22) is sufficient to make the random string unique enough to avoid accidental collisions.

Don't say VARCHAR(255) if the max is 22.  Say CHAR(22) if fixed length or VARCHAR(22) if you allow the user to choose a length up to 16.

Do say CHARACTER SET ascii COLLATE ascii_bin for the column.  This will avoid (1) overhead of utf8 and (2) the mistake of case folding.

If you are going to have an index on a billion of these items, then there will be significant performance issues, as discussed here (though in a different context).  A million rows is probably not an issue -- it depends on when the index becomes bigger than can be cached in RAM in the buffer_pool.

(If I recall the formula correctly, for 8 chars as you describe, there would be one chance in 300K that an index with 300K entries would include a duplicate.  But that's not the issue.)

ContentLicense: CC BY-SA 4.0


问:
Xamarin.Forms Prism App.xaml can be binded to a ViewModel?
I am building a Xamarin.Forms application with Prism, it shows what Bluetooth devices are connected.

In this application, I need a footer in all the pages showing the connection status of the device.
To do that, I’ve made a static class called Pen with the property IsConnected updated from the BleService.

I also need a button in the footer that is visible when the Pen is disconnected and when it is pressed I can call the Connect method of the BleService.

For the footer, I’ve made a ControlTemplate in the App.xaml containing a button.

Now, I’m using Prism and I don’t know how to bind the button to a ViewModel (App.xaml does not have a ViewModel) in order to call the Connect Command of the BleService.

答:
The App.xaml and App.aml.cs files contain the logic required to configure a Prism application and navigate to the root page on startup.

From the documentation

So you cant create a ViewModel for this as it's not a View and this would break the MVVM pattern. Prism is an MVVM framework, that works by loading Views (And their ViewModels) through a Navigation Service. So this approach for a "global" view is not going to work with binding.

Creating a control template is not going to fix this either, it just allows you to have a reusable template defined in the app.xaml.

What I would suggest is to either:

Add this footer to all views, which is a bit of an overhead.

OR

Show a popup when the BleService changes rather than having a footer view.

Another option would be to create some custom renders. You can do this natively on iOS adding the View to the RootViewController. For the other platforms, I'm not too sure, so you could go down some custom renderer road to try to get it with a footer but I don't think it is worth the headache.

Again not ideal solutions but some platforms can control this very well so I can see why Xamarin.Forms doesn't have this, built in.


问:
Look for specific number in list and echo that lines containing that number
I have to get a output of a list that only contain ports like :8080, not any other ports so the php has to check if the website contains any lines with :8080 port, and if so you will have to print the whole line. </p>

```<code>$url = "https://proxymagic.cc/Home/FreeProxies?accesskey=key";
$str = file_get_contents($url);
$lines = explode('
', $str);
foreach($lines as $line) {
  if (stripos($line, '8080') !== false) {
    $line = $output;
  }

echo $output;

}
</code>```

答:
Use file() instead of file_get_contents().

Use array_filter, as Jon Gauthier demonstrates in his answer to a related question:

```<code>array_filter($array, function($el) use ($search_text) {
   return ( strpos($el['text'], $search_text) !== false );
});
</code>```

Summary:

```<code>$url = "https://proxymagic.cc/Home/FreeProxies?accesskey=key";
$lines = file($url);

$needle = ':8080';
$lines = array_filter($lines, function($line) use ($needle) {
   return false !== strpos($line, $needle);
});

print_r($lines);
</code>```

Body:
I have to get a output of a list that only contain ports like :8080, not any other ports so the php has to check if the website contains any lines with :8080 port, and if so you will have to print the whole line.

$url = "https://proxymagic.cc/Home/FreeProxies?accesskey=key";
$str = file_get_contents($url);
$lines = explode('
', $str);
foreach($lines as $line) {
  if (stripos($line, '8080') !== false) {
    $line = $output;
  }

echo $output;

}

Body:
Use file() instead of file_get_contents().

Use array_filter, as Jon Gauthier demonstrates in his answer to a related question:

array_filter($array, function($el) use ($search_text) {
   return ( strpos($el['text'], $search_text) !== false );
});

Summary:

$url = "https://proxymagic.cc/Home/FreeProxies?accesskey=key";
$lines = file($url);

$needle = ':8080';
$lines = array_filter($lines, function($line) use ($needle) {
   return false !== strpos($line, $needle);
});

print_r($lines);


问:
screen doesn't show, Console.ReadLine() in Haskell?
I'm using GHC 8.6.5 in Windows 10.

I'm a beginner in Haskell, but have some C# experience. When I try
main = putStrLn "Hello, World!" and I compile, the program shows the screen only for a moment. I want something like Console.ReadLine() in C# so that the screen will remain until I press enter. What should I do?

I have searched the net for 2 hours in vain.

```<code>main = putStrLn "Hello, World!"
</code>```

答:
getLine reads a whole line (returning that as a string, but you don't need that).

```<code>main = do
   putStrLn "Hello, World!"
   getLine
</code>```

In this way, you'll need to press enter to exit the program.

Body:
I'm using GHC 8.6.5 in Windows 10.

I'm a beginner in Haskell, but have some C# experience. When I try
main = putStrLn "Hello, World!" and I compile, the program shows the screen only for a moment. I want something like Console.ReadLine() in C# so that the screen will remain until I press enter. What should I do?

I have searched the net for 2 hours in vain.

main = putStrLn "Hello, World!"

Body:
getLine reads a whole line (returning that as a string, but you don't need that).

main = do
   putStrLn "Hello, World!"
   getLine

In this way, you'll need to press enter to exit the program.


问:
JavaScript JSON API Parser w/ Default Values?
When consuming an external JSON API I expect certain values but I need to guard against bad inputs since I cannot control the external API.

Writing a local parser class is an option but seems pretty tedious. I need to check that the properties exist and that they are the right type (e.g. that the API did not return a string instead of an array or object) and this quickly adds up to a decent chunk of code.

Are there any standard ways of dealing with this? Any popular libraries? I tried a Google search but could not find anything.

答:
RunScope (www.runscope.com) and Assertible (www.assertible.com) seem like a good commercial alternative to do what you are looking for, i.e. validate or guard against bad inputs from your API Call result at a Test Level.

From a Programmatic point of view, however, you could use a Library called 'REST-Assured' to do the JSON Schema validation against API Call Output.

The following article elaborates with a detailed coding example for performing JSON Schema Validation against JSON/REST API call

https://www.baeldung.com/rest-assured-json-schema


问:
How to automatically update filter and/or sort order on CollectionViewSource, when an individual item's property changes?
Ok, so this question is related to Windows Phone 7/Silverlight (updated WP7 Tools, Sept 2010), specifically filtering an underlying ObservableCollection<T>.

In mucking about with the WP7 template Pivot control application, I've run into an issue whereby changing an underlying item in an ObservableCollection<T>, does not result in the on-screen ListBox being updated. Basically, the sample app has two pivots, the first directly bound to the underlying ObservableCollection<T>, and the second bound to a CollectionViewSource (i.e., representing a filtered view on the underlying ObservableCollection<T>).

The underlying items that are being added to the ObservableCollection<T> implement INotifyPropertyChanged, like so:

```<code>public class ItemViewModel : INotifyPropertyChanged
{
    public string LineOne
    {
        get { return _lineOne; }
        set
        {
            if (value != _lineOne)
            {
                _lineOne = value;
                NotifyPropertyChanged("LineOne");
            }
        }
    } private string _lineOne;

    public string LineTwo
    {
        get { return _lineTwo; }
        set
        {
            if (value != _lineTwo)
            {
                _lineTwo = value;
                NotifyPropertyChanged("LineTwo");
            }
        }
    } private string _lineTwo;

    public bool IsSelected
    {
        get { return _isSelected; }
        set
        {
            if (value != _isSelected)
            {
                _isSelected = value;
                NotifyPropertyChanged("IsSelected");
            }
        }
    } private bool _isSelected = false;

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
</code>```

Then, in the main class, a data collection is concocted (list reduced for brevity, also note that unlike other items, three of the LoadData() entries have IsSelected == true):

```<code> public class MainViewModel : INotifyPropertyChanged
 {
  public MainViewModel()
  {
   this.Items = new ObservableCollection<ItemViewModel>();
  }

  public ObservableCollection<ItemViewModel> Items { get; private set; }

  public bool IsDataLoaded
  {
   get;
   private set;
  }

   public void LoadData()
  {
   this.Items.Add(new ItemViewModel() { LineOne = "runtime one", IsSelected = true, LineTwo = "Maecenas praesent accumsan bibendum" });
   this.Items.Add(new ItemViewModel() { LineOne = "runtime two", LineTwo = "Dictumst eleifend facilisi faucibus" });
   this.Items.Add(new ItemViewModel() { LineOne = "runtime three", IsSelected = true, LineTwo = "Habitant inceptos interdum lobortis" });
   this.Items.Add(new ItemViewModel() { LineOne = "runtime four", LineTwo = "Nascetur pharetra placerat pulvinar" });
   this.Items.Add(new ItemViewModel() { LineOne = "runtime five", IsSelected = true, LineTwo = "Maecenas praesent accumsan bibendum" });
   this.Items.Add(new ItemViewModel() { LineOne = "runtime six", LineTwo = "Dictumst eleifend facilisi faucibus" });
   this.IsDataLoaded = true;
  }

  public event PropertyChangedEventHandler PropertyChanged;
  public void NotifyPropertyChanged(String propertyName)
  {
   if (null != PropertyChanged)
   {
    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
   }
  }
 }
</code>```

In the MainPage.xaml file, the first Pivot has its ItemSource based directly on the ObservableCollection<T> list. Within the second Pivot, the on-screen ListBox has its ItemSource Property set to a CollectionViewSource, whose underlying source is based on the ObservableCollection<T> populated in LoadData() above.

```<code><phone:PhoneApplicationPage.Resources>
    <CollectionViewSource x:Key="IsSelectedCollectionView" Filter="CollectionViewSource_SelectedListFilter">
    </CollectionViewSource>
</phone:PhoneApplicationPage.Resources>

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
    <!--Pivot Control-->
    <controls:Pivot Title="MY APPLICATION">
        <!--Pivot item one-->
        <controls:PivotItem Header="first">
            <!--Double line list with text wrapping-->
            <ListBox x:Name="FirstListBox" Margin="0,0,-12,0" ItemsSource="{Binding Items}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                      <StackPanel Margin="0,0,0,17" Width="432">
                          <TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                          <TextBlock Text="{Binding LineTwo}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                      </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </controls:PivotItem>

        <!--Pivot item two-->
        <controls:PivotItem Header="second">
            <!--Triple line list no text wrapping-->
            <ListBox x:Name="SecondListBox" Margin="0,0,-12,0" ItemsSource="{Binding  Source={StaticResource IsSelectedCollectionView}}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Margin="0,0,0,17">
                                <TextBlock Text="{Binding LineOne}" TextWrapping="NoWrap" Margin="12,0,0,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                                <TextBlock Text="{Binding LineThree}" TextWrapping="NoWrap" Margin="12,-6,0,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
        </controls:PivotItem>
    </controls:Pivot>
</Grid>

<!--Sample code showing usage of ApplicationBar-->
<phone:PhoneApplicationPage.ApplicationBar>
    <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
        <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1" Click="ApplicationBarIconButton_Click"/>
        <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
        <shell:ApplicationBar.MenuItems>
            <shell:ApplicationBarMenuItem Text="MenuItem 1"/>
            <shell:ApplicationBarMenuItem Text="MenuItem 2"/>
        </shell:ApplicationBar.MenuItems>
    </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
</code>```

Note that in the MainPage.xaml.cs, the Filter attribute on the CollectionViewSource in the Resources section above is assigned a filter handler, which sifts through those items that have IsSelected set to true:

```<code>public partial class MainPage : PhoneApplicationPage
{
    public MainPage()
    {
        InitializeComponent();
        DataContext = App.ViewModel;
        this.Loaded += new RoutedEventHandler(MainPage_Loaded);
    }

    private void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        if (!App.ViewModel.IsDataLoaded)
        {
            App.ViewModel.LoadData();
            CollectionViewSource isSelectedListView = this.Resources["IsSelectedCollectionView"] as CollectionViewSource;
            if (isSelectedListView != null)
            {
                isSelectedListView .Source = App.ViewModel.Items;
            }
        }
    }

    private void CollectionViewSource_SelectedListFilter(object sender, System.Windows.Data.FilterEventArgs e)
    {
        e.Accepted = ((ItemViewModel)e.Item).IsSelected;
    }

    private void ApplicationBarIconButton_Click(object sender, EventArgs e)
    {
        ItemViewModel item = App.ViewModel.Items[App.ViewModel.Items.Count - 1];
        item.IsSelected = !item.IsSelected;
    }
}
</code>```

Also note that immediately after loading up the data, I obtain the CollectionViewSource and set its data source as the ObservableCollection<T> list, in order that there is base data upon which the filtering can take place.

When application loads, the data is displayed as expected, with those items in the ObservableCollection<T> which have IsSelected true, being displayed in the second Pivot:

You'll notice that I've uncommented the Application Bar Icons, the first of which toggles the IsSelected property of the last item in the ObservableCollection<T> when clicked (see the last function in MainPage.xaml.cs).

Here is the crux of my question - when I click the applicable bar icon, I can see when the last item in the list has its IsSelected property set to true, howoever the second Pivot does not display this changed item. I can see that the NotifyPropertyChanged() handler is being fired on the item, however the collection is not picking up this fact, and hence the list box in Pivot 2 does not change to reflect the fact that there should be a new item added to the collection.

I'm pretty certain that I'm missing something quite fundamental/basic here, but failing that, does anyone know the best way to get the collection and it's underlying items to play happily together?

I suppose this problem also applies to sorting as well as filtering ((in the sense that if a CollectionViewSource is based on sorting, then when a property of an item that is used in the sort changes, the sort order of the collection should reflect this as well))

答:
I had to handle this problem and although the 'Refresh()' solution works well, it is quite long to execute because its refreshes the whole list just for one item property changed event. Not very good. And in a scenario of real time data entering the collection every 1 seconds, I let you imagine the result in user experience if you use this approach :)

I came up with a solution which base is : when adding an item to collection wrapped in a collectionview, then the item is evaluated by the filter predicate and, based on this result, displayed or not in the view.

So instead of calling refresh(), I came up simulating an insert of the object that got its property updated. By simulating the insert of the object, it is going to be automatically evaluated by the filter predicate without need to refresh the whole list with a refresh.

Here is the code in order to do that :

The derived observable collection :

```<code>namespace dotnetexplorer.blog.com
{
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

/// <summary>
/// Derived class used to be able to manage filter application when a collection item property changed
///   whithout having to do a refresh
/// </summary>
internal sealed class CustomObservableCollection : ObservableCollection<object>
{
    /// <summary>
    ///   Initializes a new instance of the <see cref = "CustomObservableCollection " /> class.
    /// </summary>
    public CustomObservableCollection ()
    {
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="CustomObservableCollection "/> class.
    /// </summary>
    /// <param name="source">
    /// The source.
    /// </param>
    public CustomObservableCollection (IEnumerable<object> source)
        : base(source)
    {
    }

    /// <summary>
    /// Custom Raise collection changed
    /// </summary>
    /// <param name="e">
    /// The notification action
    /// </param>
    public void RaiseCollectionChanged(NotifyCollectionChangedEventArgs e)
    {
        OnCollectionChanged(e);
    }
}
}
</code>```

And there is the code to use when receiveing item property changed event where substitute source is a CustomObservableCollection :

```<code>        private void ItemPropertyChanged(object sender, PropertyChangedEventArgs e)
    {

                // To avoid doing a refresh on a property change which would end in a very hawful user experience
                // we simulate a replace to the collection because the filter is automatically applied in this case
                int index = _substituteSource.IndexOf(sender);

                var argsReplace = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace,
                                                                       new List<object> { sender },
                                                                       new List<object> { sender }, index);
                _substituteSource.RaiseCollectionChanged(argsReplace);
            }

        }
    }
</code>```

Hope this will help !

ContentLicense: CC BY-SA 2.5

Body:
Ok, so this question is related to Windows Phone 7/Silverlight (updated WP7 Tools, Sept 2010), specifically filtering an underlying ObservableCollection<T>.

In mucking about with the WP7 template Pivot control application, I've run into an issue whereby changing an underlying item in an ObservableCollection<T>, does not result in the on-screen ListBox being updated. Basically, the sample app has two pivots, the first directly bound to the underlying ObservableCollection<T>, and the second bound to a CollectionViewSource (i.e., representing a filtered view on the underlying ObservableCollection<T>).

The underlying items that are being added to the ObservableCollection<T> implement INotifyPropertyChanged, like so:

public class ItemViewModel : INotifyPropertyChanged
{
    public string LineOne
    {
        get { return _lineOne; }
        set
        {
            if (value != _lineOne)
            {
                _lineOne = value;
                NotifyPropertyChanged("LineOne");
            }
        }
    } private string _lineOne;

    public string LineTwo
    {
        get { return _lineTwo; }
        set
        {
            if (value != _lineTwo)
            {
                _lineTwo = value;
                NotifyPropertyChanged("LineTwo");
            }
        }
    } private string _lineTwo;

    public bool IsSelected
    {
        get { return _isSelected; }
        set
        {
            if (value != _isSelected)
            {
                _isSelected = value;
                NotifyPropertyChanged("IsSelected");
            }
        }
    } private bool _isSelected = false;

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Then, in the main class, a data collection is concocted (list reduced for brevity, also note that unlike other items, three of the LoadData() entries have IsSelected == true):

 public class MainViewModel : INotifyPropertyChanged
 {
  public MainViewModel()
  {
   this.Items = new ObservableCollection<ItemViewModel>();
  }

  public ObservableCollection<ItemViewModel> Items { get; private set; }

  public bool IsDataLoaded
  {
   get;
   private set;
  }

   public void LoadData()
  {
   this.Items.Add(new ItemViewModel() { LineOne = "runtime one", IsSelected = true, LineTwo = "Maecenas praesent accumsan bibendum" });
   this.Items.Add(new ItemViewModel() { LineOne = "runtime two", LineTwo = "Dictumst eleifend facilisi faucibus" });
   this.Items.Add(new ItemViewModel() { LineOne = "runtime three", IsSelected = true, LineTwo = "Habitant inceptos interdum lobortis" });
   this.Items.Add(new ItemViewModel() { LineOne = "runtime four", LineTwo = "Nascetur pharetra placerat pulvinar" });
   this.Items.Add(new ItemViewModel() { LineOne = "runtime five", IsSelected = true, LineTwo = "Maecenas praesent accumsan bibendum" });
   this.Items.Add(new ItemViewModel() { LineOne = "runtime six", LineTwo = "Dictumst eleifend facilisi faucibus" });
   this.IsDataLoaded = true;
  }

  public event PropertyChangedEventHandler PropertyChanged;
  public void NotifyPropertyChanged(String propertyName)
  {
   if (null != PropertyChanged)
   {
    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
   }
  }
 }

In the MainPage.xaml file, the first Pivot has its ItemSource based directly on the ObservableCollection<T> list. Within the second Pivot, the on-screen ListBox has its ItemSource Property set to a CollectionViewSource, whose underlying source is based on the ObservableCollection<T> populated in LoadData() above.

<phone:PhoneApplicationPage.Resources>
    <CollectionViewSource x:Key="IsSelectedCollectionView" Filter="CollectionViewSource_SelectedListFilter">
    </CollectionViewSource>
</phone:PhoneApplicationPage.Resources>

<Grid x:Name="LayoutRoot" Background="Transparent">

    <controls:Pivot Title="MY APPLICATION">

        <controls:PivotItem Header="first">

            <ListBox x:Name="FirstListBox" Margin="0,0,-12,0" ItemsSource="{Binding Items}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                      <StackPanel Margin="0,0,0,17" Width="432">
                          <TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                          <TextBlock Text="{Binding LineTwo}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                      </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </controls:PivotItem>

        <controls:PivotItem Header="second">

            <ListBox x:Name="SecondListBox" Margin="0,0,-12,0" ItemsSource="{Binding  Source={StaticResource IsSelectedCollectionView}}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Margin="0,0,0,17">
                                <TextBlock Text="{Binding LineOne}" TextWrapping="NoWrap" Margin="12,0,0,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                                <TextBlock Text="{Binding LineThree}" TextWrapping="NoWrap" Margin="12,-6,0,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
        </controls:PivotItem>
    </controls:Pivot>
</Grid>

<phone:PhoneApplicationPage.ApplicationBar>
    <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
        <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1" Click="ApplicationBarIconButton_Click"/>
        <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
        <shell:ApplicationBar.MenuItems>
            <shell:ApplicationBarMenuItem Text="MenuItem 1"/>
            <shell:ApplicationBarMenuItem Text="MenuItem 2"/>
        </shell:ApplicationBar.MenuItems>
    </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>

Note that in the MainPage.xaml.cs, the Filter attribute on the CollectionViewSource in the Resources section above is assigned a filter handler, which sifts through those items that have IsSelected set to true:

public partial class MainPage : PhoneApplicationPage
{
    public MainPage()
    {
        InitializeComponent();
        DataContext = App.ViewModel;
        this.Loaded += new RoutedEventHandler(MainPage_Loaded);
    }

    private void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        if (!App.ViewModel.IsDataLoaded)
        {
            App.ViewModel.LoadData();
            CollectionViewSource isSelectedListView = this.Resources["IsSelectedCollectionView"] as CollectionViewSource;
            if (isSelectedListView != null)
            {
                isSelectedListView .Source = App.ViewModel.Items;
            }
        }
    }

    private void CollectionViewSource_SelectedListFilter(object sender, System.Windows.Data.FilterEventArgs e)
    {
        e.Accepted = ((ItemViewModel)e.Item).IsSelected;
    }

    private void ApplicationBarIconButton_Click(object sender, EventArgs e)
    {
        ItemViewModel item = App.ViewModel.Items[App.ViewModel.Items.Count - 1];
        item.IsSelected = !item.IsSelected;
    }
}

Also note that immediately after loading up the data, I obtain the CollectionViewSource and set its data source as the ObservableCollection<T> list, in order that there is base data upon which the filtering can take place.

When application loads, the data is displayed as expected, with those items in the ObservableCollection<T> which have IsSelected true, being displayed in the second Pivot:

You'll notice that I've uncommented the Application Bar Icons, the first of which toggles the IsSelected property of the last item in the ObservableCollection<T> when clicked (see the last function in MainPage.xaml.cs).

Here is the crux of my question - when I click the applicable bar icon, I can see when the last item in the list has its IsSelected property set to true, howoever the second Pivot does not display this changed item. I can see that the NotifyPropertyChanged() handler is being fired on the item, however the collection is not picking up this fact, and hence the list box in Pivot 2 does not change to reflect the fact that there should be a new item added to the collection.

I'm pretty certain that I'm missing something quite fundamental/basic here, but failing that, does anyone know the best way to get the collection and it's underlying items to play happily together?

I suppose this problem also applies to sorting as well as filtering ((in the sense that if a CollectionViewSource is based on sorting, then when a property of an item that is used in the sort changes, the sort order of the collection should reflect this as well))

Body:
I had to handle this problem and although the 'Refresh()' solution works well, it is quite long to execute because its refreshes the whole list just for one item property changed event. Not very good. And in a scenario of real time data entering the collection every 1 seconds, I let you imagine the result in user experience if you use this approach :)

I came up with a solution which base is : when adding an item to collection wrapped in a collectionview, then the item is evaluated by the filter predicate and, based on this result, displayed or not in the view.

So instead of calling refresh(), I came up simulating an insert of the object that got its property updated. By simulating the insert of the object, it is going to be automatically evaluated by the filter predicate without need to refresh the whole list with a refresh.

Here is the code in order to do that :

The derived observable collection :

namespace dotnetexplorer.blog.com
{
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

/// <summary>
/// Derived class used to be able to manage filter application when a collection item property changed
///   whithout having to do a refresh
/// </summary>
internal sealed class CustomObservableCollection : ObservableCollection<object>
{
    /// <summary>
    ///   Initializes a new instance of the <see cref = "CustomObservableCollection " /> class.
    /// </summary>
    public CustomObservableCollection ()
    {
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="CustomObservableCollection "/> class.
    /// </summary>
    /// <param name="source">
    /// The source.
    /// </param>
    public CustomObservableCollection (IEnumerable<object> source)
        : base(source)
    {
    }

    /// <summary>
    /// Custom Raise collection changed
    /// </summary>
    /// <param name="e">
    /// The notification action
    /// </param>
    public void RaiseCollectionChanged(NotifyCollectionChangedEventArgs e)
    {
        OnCollectionChanged(e);
    }
}
}

And there is the code to use when receiveing item property changed event where substitute source is a CustomObservableCollection :

        private void ItemPropertyChanged(object sender, PropertyChangedEventArgs e)
    {

                // To avoid doing a refresh on a property change which would end in a very hawful user experience
                // we simulate a replace to the collection because the filter is automatically applied in this case
                int index = _substituteSource.IndexOf(sender);

                var argsReplace = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace,
                                                                       new List<object> { sender },
                                                                       new List<object> { sender }, index);
                _substituteSource.RaiseCollectionChanged(argsReplace);
            }

        }
    }

Hope this will help !

ContentLicense: CC BY-SA 3.0


问:
Check if element is occurring very first time in python list
I have a list with values occurring multiple times. I want to loop over the list and check if value is occurring very first time.

For eg: Let's say I have a one list like ,

L = ['a','a','a','b','b','b','b','b','e','e','e'.......]

Now, at every first occurrence of element, I want to perform some set of tasks.

How to get the first occurrence of element?

Thanks in Advance!!

答:
You can use unique_everseen from itertools recipes.

This function returns a generator which yield only the first occurence of an element.

Code

```<code>from itertools import filterfalse

def unique_everseen(iterable, key=None):
    "List unique elements, preserving order. Remember all elements ever seen."
    # unique_everseen('AAAABBBCCDAABBB') --> A B C D
    # unique_everseen('ABBCcAD', str.lower) --> A B C D
    seen = set()
    seen_add = seen.add
    if key is None:
        for element in filterfalse(seen.__contains__, iterable):
            seen_add(element)
            yield element
    else:
        for element in iterable:
            k = key(element)
            if k not in seen:
                seen_add(k)
                yield element
</code>```

Example

```<code>lst = ['a', 'a', 'b', 'c', 'b']

for x in unique_everseen(lst):
    print(x) # Do something with the element
</code>```

Output

```<code>a
b
c
</code>```

The function unique_everseen also allows to pass a key for comparison of elements. This is useful in many cases, by example if you also need to know the position of each first occurence.

Example

```<code>lst = ['a', 'a', 'b', 'c', 'b']

for i, x in unique_everseen(enumerate(lst), key=lambda x: x[1]):
    print(i, x)
</code>```

Output

```<code>0 a
2 b
3 c
</code>```

Body:
You can use unique_everseen from itertools recipes.

This function returns a generator which yield only the first occurence of an element.

Code

from itertools import filterfalse

def unique_everseen(iterable, key=None):
    "List unique elements, preserving order. Remember all elements ever seen."
    # unique_everseen('AAAABBBCCDAABBB') --> A B C D
    # unique_everseen('ABBCcAD', str.lower) --> A B C D
    seen = set()
    seen_add = seen.add
    if key is None:
        for element in filterfalse(seen.__contains__, iterable):
            seen_add(element)
            yield element
    else:
        for element in iterable:
            k = key(element)
            if k not in seen:
                seen_add(k)
                yield element

Example

lst = ['a', 'a', 'b', 'c', 'b']

for x in unique_everseen(lst):
    print(x) # Do something with the element

Output

a
b
c

The function unique_everseen also allows to pass a key for comparison of elements. This is useful in many cases, by example if you also need to know the position of each first occurence.

Example

lst = ['a', 'a', 'b', 'c', 'b']

for i, x in unique_everseen(enumerate(lst), key=lambda x: x[1]):
    print(i, x)

Output

0 a
2 b
3 c


问:
Matrix multiplication (element-wise) from numpy to Pytorch
I got two numpy arrays (image and and environment map),

```<code>MatA
MatB
</code>```

Both with shapes (256, 512, 3)

When I did the multiplication (element-wise) with numpy:

```<code>prod = np.multiply(MatA,MatB)
</code>```

I got the wanted result (visualize via Pillow when turning back to Image)

But when I did it using pytorch, I got a really strange result(not even close to the aforementioned).

I did it with the following code:

```<code>MatATensor = transforms.ToTensor()(MatA)
MatBTensor = transforms.ToTensor()(MatB)

prodTensor = MatATensor * MatBTensor
</code>```

For some reasons, the shape for both MatATensor and MatBtensor is

```<code>torch.Size([3, 256, 512])
</code>```

Same for the prodTensor too.
When I tried to reshape to (256,512,3), I got an error.

Is there a way to get the same result?

I am new to pytorch, so any help would be appreciated.

答:
I suggest you use torch.from_numpy, which will easily convert your ndarrays to torch tensors. As in:

```<code>In[1]: MatA = np.random.rand(256, 512, 3)
In[2]: MatB = np.random.rand(256, 512, 3)

In[3]: MatA_torch = torch.from_numpy(MatA)
In[4]: MatB_torch = torch.from_numpy(MatB)

In[5]: mul_np = np.multiply(MatA, MatB)
In[6]: mul_torch = MatA_torch * MatB_torch

In[7]: torch.equal(torch.from_numpy(mul_np), mul_torch)
Out[7]: True

In[8]: mul_torch.shape
Out[8]: torch.Size([256, 512, 3])
</code>```

If you want it back to numpy, just do:

```<code>mul_torch.numpy()
</code>```

Body:
I got two numpy arrays (image and and environment map),

MatA
MatB

Both with shapes (256, 512, 3)

When I did the multiplication (element-wise) with numpy:

prod = np.multiply(MatA,MatB)

I got the wanted result (visualize via Pillow when turning back to Image)

But when I did it using pytorch, I got a really strange result(not even close to the aforementioned).

I did it with the following code:

MatATensor = transforms.ToTensor()(MatA)
MatBTensor = transforms.ToTensor()(MatB)

prodTensor = MatATensor * MatBTensor

For some reasons, the shape for both MatATensor and MatBtensor is

torch.Size([3, 256, 512])

Same for the prodTensor too.
When I tried to reshape to (256,512,3), I got an error.

Is there a way to get the same result?

I am new to pytorch, so any help would be appreciated.

Body:
I suggest you use torch.from_numpy, which will easily convert your ndarrays to torch tensors. As in:

In[1]: MatA = np.random.rand(256, 512, 3)
In[2]: MatB = np.random.rand(256, 512, 3)

In[3]: MatA_torch = torch.from_numpy(MatA)
In[4]: MatB_torch = torch.from_numpy(MatB)

In[5]: mul_np = np.multiply(MatA, MatB)
In[6]: mul_torch = MatA_torch * MatB_torch

In[7]: torch.equal(torch.from_numpy(mul_np), mul_torch)
Out[7]: True

In[8]: mul_torch.shape
Out[8]: torch.Size([256, 512, 3])

If you want it back to numpy, just do:

mul_torch.numpy()


问:
How to extract top-level domain name (TLD) from URL
how would you extract the domain name from a URL, excluding any subdomains?

My initial simplistic attempt was:

```<code>'.'.join(urlparse.urlparse(url).netloc.split('.')[-2:])
</code>```

This works for http://www.foo.com, but not http://www.foo.com.au.
Is there a way to do this properly without using special knowledge about valid TLDs (Top Level Domains) or country codes (because they change).

thanks

答:
In Python I used to use tldextract until it failed with a url like www.mybrand.sa.com parsing it as subdomain='order.mybrand', domain='sa', suffix='com'!!

So finally, I decided to write this method

IMPORTANT NOTE: this only works with urls that have a subdomain in them. This isn't meant to replace more advanced libraries like tldextract

```<code>def urlextract(url):
  url_split=url.split(".")
  if len(url_split) <= 2:
      raise Exception("Full url required with subdomain:",url)
  return {'subdomain': url_split[0], 'domain': url_split[1], 'suffix': ".".join(url_split[2:])}
</code>```

Body:
how would you extract the domain name from a URL, excluding any subdomains?

My initial simplistic attempt was:

'.'.join(urlparse.urlparse(url).netloc.split('.')[-2:])

This works for http://www.foo.com, but not http://www.foo.com.au.
Is there a way to do this properly without using special knowledge about valid TLDs (Top Level Domains) or country codes (because they change).

thanks

Body:
In Python I used to use tldextract until it failed with a url like www.mybrand.sa.com parsing it as subdomain='order.mybrand', domain='sa', suffix='com'!!

So finally, I decided to write this method

IMPORTANT NOTE: this only works with urls that have a subdomain in them. This isn't meant to replace more advanced libraries like tldextract

def urlextract(url):
  url_split=url.split(".")
  if len(url_split) <= 2:
      raise Exception("Full url required with subdomain:",url)
  return {'subdomain': url_split[0], 'domain': url_split[1], 'suffix': ".".join(url_split[2:])}


问:
How to fix "Error creating bean with name"
My website is/was working just fine. But I am being forced to upgrade the OS of the servers it runs on, because the newer version of a piece of software I need to upgrade, won't run on the old OS. So, now I am in dependency hell.

So, on a test server, I've upgraded the OS from Ubuntu 14.04 to 18.04.1 . I was also forced to use a later version of Java from:

```<code>echo $JAVA_HOME
/usr/lib/jvm/java-7-oracle
</code>```

to

```<code>echo $JAVA_HOME
/usr/lib/jvm/java-11-openjdk-amd64/
</code>```

And the version of Tomcat from 7 to 8.

Having fiddled a bit (I started this in January, but have only just been able to get back to it, so I can't remember what exactly, though can search the history for clues if required), I can build my application OK, but it fails at the final hurdle:

BUILD SUCCESSFUL Total time: 19 seconds Using CATALINA_BASE:

  /var/lib/tomcat8 Using CATALINA_HOME:   /usr/share/tomcat8 Using
  CATALINA_TMPDIR: /var/lib/tomcat8/temp Using JRE_HOME:

  /usr/lib/jvm/java-11-openjdk-amd64/ Using CLASSPATH:

  /usr/share/tomcat8/bin/bootstrap.jar:/usr/share/tomcat8/bin/tomcat-juli.jar
  Using CATALINA_PID:    /root/catalina.pid Tomcat started.
  --2019-05-28 16:10:16--  http://localhost:8080/ Resolving localhost (localhost)... 127.0.0.1 Connecting to localhost
  (localhost)|127.0.0.1|:8080... connected. HTTP request sent, awaiting
  response... 404 2019-05-28 16:10:16 ERROR 404: (no description).

Further investigation reveals:

```<code>less /var/log/tomcat8/catalina.out
</code>```

2019-05-28 16:10:06.418 UTC [localhost-startStop-1] ERROR
  org.springframework.web.context.ContextLoader - Context initialization
  failed org.springframework.beans.factory.BeanCreationException: Error
  creating bean with name 'embedController' defined in ServletContext
  resource [/WEB-INF/playground-servlet.xml]: Cannot resolve reference
  to bean 'homeController' while settin g bean property
  'homeController'; nested exception is
  org.springframework.beans.factory.BeanCreationException: Error
  creating bean with name 'homeController' defined in ServletContext
  resource [/WEB-INF/playground-servlet.xml]: Cannot re solve reference
  to bean 'execService' while setting bean property 'execService';
  nested exception is
  org.springframework.beans.factory.BeanCreationException: Error
  creating bean with name 'execService': Invocation of init method
  failed; nested exception is javax.ws.rs.ProcessingException:
  java.net.ConnectException: Connection refused (Connection refused)
          at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:326)
  ~[spring-beans-3.2.8.RELEASE.jar:3.2.8.RELEASE]

It should be abundantly clear by now that I don't have much clue what I'm doing. I have spent the afternoon doing lots of googling and looking at lots of Stack Overflow answers, but nothing seems relevant.

答:
that error comes from Spring. For Java 11 support, you'd also need to upgrade Spring to the latest 5.x version (currently 5.2.x).

You may encounter various compatibility issues when upgrading an older environment to Java 11. Unless you absolutely need to upgrade, you may consider staying on a previous version. Java 8 is still being supported.

Body:
My website is/was working just fine. But I am being forced to upgrade the OS of the servers it runs on, because the newer version of a piece of software I need to upgrade, won't run on the old OS. So, now I am in dependency hell.

So, on a test server, I've upgraded the OS from Ubuntu 14.04 to 18.04.1 . I was also forced to use a later version of Java from:

echo $JAVA_HOME
/usr/lib/jvm/java-7-oracle

to

echo $JAVA_HOME
/usr/lib/jvm/java-11-openjdk-amd64/

And the version of Tomcat from 7 to 8.

Having fiddled a bit (I started this in January, but have only just been able to get back to it, so I can't remember what exactly, though can search the history for clues if required), I can build my application OK, but it fails at the final hurdle:

BUILD SUCCESSFUL Total time: 19 seconds Using CATALINA_BASE:

  /var/lib/tomcat8 Using CATALINA_HOME:   /usr/share/tomcat8 Using
  CATALINA_TMPDIR: /var/lib/tomcat8/temp Using JRE_HOME:

  /usr/lib/jvm/java-11-openjdk-amd64/ Using CLASSPATH:

  /usr/share/tomcat8/bin/bootstrap.jar:/usr/share/tomcat8/bin/tomcat-juli.jar
  Using CATALINA_PID:    /root/catalina.pid Tomcat started.
  --2019-05-28 16:10:16--  http://localhost:8080/ Resolving localhost (localhost)... 127.0.0.1 Connecting to localhost
  (localhost)|127.0.0.1|:8080... connected. HTTP request sent, awaiting
  response... 404 2019-05-28 16:10:16 ERROR 404: (no description).

Further investigation reveals:

less /var/log/tomcat8/catalina.out

2019-05-28 16:10:06.418 UTC [localhost-startStop-1] ERROR
  org.springframework.web.context.ContextLoader - Context initialization
  failed org.springframework.beans.factory.BeanCreationException: Error
  creating bean with name 'embedController' defined in ServletContext
  resource [/WEB-INF/playground-servlet.xml]: Cannot resolve reference
  to bean 'homeController' while settin g bean property
  'homeController'; nested exception is
  org.springframework.beans.factory.BeanCreationException: Error
  creating bean with name 'homeController' defined in ServletContext
  resource [/WEB-INF/playground-servlet.xml]: Cannot re solve reference
  to bean 'execService' while setting bean property 'execService';
  nested exception is
  org.springframework.beans.factory.BeanCreationException: Error
  creating bean with name 'execService': Invocation of init method
  failed; nested exception is javax.ws.rs.ProcessingException:
  java.net.ConnectException: Connection refused (Connection refused)
          at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:326)
  ~[spring-beans-3.2.8.RELEASE.jar:3.2.8.RELEASE]

It should be abundantly clear by now that I don't have much clue what I'm doing. I have spent the afternoon doing lots of googling and looking at lots of Stack Overflow answers, but nothing seems relevant.


问:
Programmatically disabling Chrome auto-fill
There’s some discussion about this around the internet (especially here on stack overflow), but quite a lot of it dates from around 2015, so I was wondering if anyone had any recent experience.

We use a JavaScript widget to power type-ahead search functionality on web forms, but this UI is consistently overlaid with the Chrome autofill.

While autocomplete=“off” used to work, Chrome seems to ignore this value now, and show the UI anyway. The only thing I can find that works with Chrome 66/67 on OSX is an invalid value, such as autocomplete=“blah”. This seems way too sketchy though, and we have experimented with this before and it gets ignored in certain situations/Chrome versions.

Has anyone fond a reliable way to turn this off using HTML/Javascript?

As a side note - it looks like even Google can’t turn it off when needed, as their own Google maps type-ahead widget gets overlaid by the Chrome autofill. This can be seen on most Shopify stores.

答:
At this time (May, 2019) only autocomplete="new-password" is working well.

It can do programmatically with jQuery for example :

```<code>$('form, input, select').attr('autocomplete', 'new-password');
</code>```

Body:
At this time (May, 2019) only autocomplete="new-password" is working well.

It can do programmatically with jQuery for example :

$('form, input, select').attr('autocomplete', 'new-password');


问:
Unable to get DatabaseMetaData for Procedure from JDBC
I have 2 issues:
 I'm unable to get Procedure MetaData via JDBC
1.) if my procedure is inside a Package
2.) if the procedure is outside a package , but user is a non-schema owner , but has access to the Procedure

For (1) I referred to this  link , but didn't work for me even when using Schema owner as the program user

Here's what my code snippet looks like:

```<code> DatabaseMetaData metadata = conn.getMetaData();

        ResultSet rs = metadata.getProcedureColumns(
                    "PKG_TEST",
                    "ENTITLE",
                    "getUser",
                "%"); // Tried with null instead of % as well
</code>```

Where getUser is a Procedure within PKG_TEST in Schema ENTITLE

The only thing that works for me is below:

```<code> ResultSet rs = metadata.getProcedureColumns(
                conn.getCatalog(),
                null,
                "getUser1",
           null);
</code>```

Where getUser1 is a Procedure outside any package but in the schema ENTITLE and the program logs into the database as ENTITLE

Are there any permissions required to access Database metadata of a schema?

Note that in the example above , it does not work if I login as a DB user that has access to the procedure , but is not schema owner.

答:
Oracle stores names in uppercase</p>

```<code>String p = "getUser1";

ResultSet rs = metadata.getProcedureColumns(
               conn.getCatalog(),
               null,
               p.toUpperCase(),
               null);
</code>```

Body:
I have 2 issues:
 I'm unable to get Procedure MetaData via JDBC
1.) if my procedure is inside a Package
2.) if the procedure is outside a package , but user is a non-schema owner , but has access to the Procedure

For (1) I referred to this  link , but didn't work for me even when using Schema owner as the program user

Here's what my code snippet looks like:

 DatabaseMetaData metadata = conn.getMetaData();

        ResultSet rs = metadata.getProcedureColumns(
                    "PKG_TEST",
                    "ENTITLE",
                    "getUser",
                "%"); // Tried with null instead of % as well

Where getUser is a Procedure within PKG_TEST in Schema ENTITLE

The only thing that works for me is below:

 ResultSet rs = metadata.getProcedureColumns(
                conn.getCatalog(),
                null,
                "getUser1",
           null);

Where getUser1 is a Procedure outside any package but in the schema ENTITLE and the program logs into the database as ENTITLE

Are there any permissions required to access Database metadata of a schema?

Note that in the example above , it does not work if I login as a DB user that has access to the procedure , but is not schema owner.

Body:
Oracle stores names in uppercase

String p = "getUser1";

ResultSet rs = metadata.getProcedureColumns(
               conn.getCatalog(),
               null,
               p.toUpperCase(),
               null);


问:
Simple CSV file error when reading
I am new to Python programming, I want to know little bit more about the internal working of python, why my code giving this error:

Code:

```<code>with open('countries.csv','r') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
        print(row[1])
</code>```

ERROR:

```<code>Traceback (most recent call last):
2
  File "/DS/main.py", line 12, in <module>
china
    print(row[1])
china
IndexError: list index out of range
germany
us
japan
india
us
china
india
japan

Process finished with exit code 1
</code>```

My CSV file is separated by a simple comma (delimiter), it shows the correct output but with mentioned error?

答:
After @Hai Vu:
2nd approach could be:

code:

with open('countries.csv','r')  as  csvfile:

```<code>    readCSV = csv.DictReader(csvfile, delimiter=',')
    for row in readCSV:
        print(row)
</code>```

Title: Simple CSV file error when reading

Body:
I am new to Python programming, I want to know little bit more about the internal working of python, why my code giving this error:

Code:

with open('countries.csv','r') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
        print(row[1])

ERROR:

Traceback (most recent call last):
2
  File "/DS/main.py", line 12, in <module>
china
    print(row[1])
china
IndexError: list index out of range
germany
us
japan
india
us
china
india
japan

Process finished with exit code 1

My CSV file is separated by a simple comma (delimiter), it shows the correct output but with mentioned error?

Body:
After @Hai Vu:
2nd approach could be:

code:

with open('countries.csv','r')  as  csvfile:

    readCSV = csv.DictReader(csvfile, delimiter=',')
    for row in readCSV:
        print(row)


问:
How to determine the correct way to update a DAC field?
I'm having some trouble determining when to use the e.Row properties or cache.SetValue/SetValueExt when updating the value of a record. In the T200 course, there's a portion from the pdf that says,

To update a field that is defined after the current field in the data
  access class, use the properties of the e.Row data record as follows.

```<code>ShipmentLine line = e.Row as ShipmentLine;
...
line.Description
</code>```

To update a field that is defined before the current field in the data
  access class, use the SetValueExt<>() method:

```<code>sender.SetValueExt<ShipmentLine.ProductID>(e.Row, GiftCardID);
</code>```

In these cases, what are they referring to when they say "before" and "after"? If my DAC field declaration is in the order below:

```<code>Field1 {get;set;}
Field2 {get;set;}
Field3 {get;set;}
</code>```

Do they literally mean, if I'm in "Field2.FieldUpdated()" I would have to update Field1 and Field3 like so?

```<code>sender.SetValueExt<ShipmentLine.Field1>(e.Row, "X");
line.Field3 = "X";
</code>```

Also, are there some hard rules on when to use which method? I.e. if in a RowUpdated event, use "X", if in a FieldUpdated event, use "Y".

答:
You will encounter mainly three types of assignment:

```<code> 1. record.Field = "value"
 2. cache.SetValue<T>
 3. cache.SetValueExt<T>
</code>```

Method 1 and 2 are essentially the same. They assign the value without raising events. The practical difference is that with method 2 you can avoid a cast.

```<code>// 1. With cast
ShipmentLine line = e.Row as ShipmentLine;

if (line != null)
{
    line.Description = "value";
}

// 2. Without cast
sender.SetValue<ShipmentLine.description>(e.Row, "value");
</code>```

SetValueExt should be used when you want the assignment to raise FieldUpdated events. Many fields on UI are raising events when user changes their value. If user changes the quantity fields of a sales order it will raise FieldUpdated events which will in turn re-compute the total price.

If you were to change the quantity programmatically from code you should use SetValueExt as it will ensure re-computing the total cost using the events system. It is especially useful when you try to replicate what a user would do on screen since UI controls all raise events by default.

Body:
I'm having some trouble determining when to use the e.Row properties or cache.SetValue/SetValueExt when updating the value of a record. In the T200 course, there's a portion from the pdf that says,

To update a field that is defined after the current field in the data
  access class, use the properties of the e.Row data record as follows.

ShipmentLine line = e.Row as ShipmentLine;
...
line.Description

To update a field that is defined before the current field in the data
  access class, use the SetValueExt<>() method:

sender.SetValueExt<ShipmentLine.ProductID>(e.Row, GiftCardID);

In these cases, what are they referring to when they say "before" and "after"? If my DAC field declaration is in the order below:

Field1 {get;set;}
Field2 {get;set;}
Field3 {get;set;}

Do they literally mean, if I'm in "Field2.FieldUpdated()" I would have to update Field1 and Field3 like so?

sender.SetValueExt<ShipmentLine.Field1>(e.Row, "X");
line.Field3 = "X";

Also, are there some hard rules on when to use which method? I.e. if in a RowUpdated event, use "X", if in a FieldUpdated event, use "Y".

Body:
You will encounter mainly three types of assignment:

 1. record.Field = "value"
 2. cache.SetValue<T>
 3. cache.SetValueExt<T>

Method 1 and 2 are essentially the same. They assign the value without raising events. The practical difference is that with method 2 you can avoid a cast.

// 1. With cast
ShipmentLine line = e.Row as ShipmentLine;

if (line != null)
{
    line.Description = "value";
}

// 2. Without cast
sender.SetValue<ShipmentLine.description>(e.Row, "value");

SetValueExt should be used when you want the assignment to raise FieldUpdated events. Many fields on UI are raising events when user changes their value. If user changes the quantity fields of a sales order it will raise FieldUpdated events which will in turn re-compute the total price.

If you were to change the quantity programmatically from code you should use SetValueExt as it will ensure re-computing the total cost using the events system. It is especially useful when you try to replicate what a user would do on screen since UI controls all raise events by default.


问:
How to initialize 3 dimension vector with the size in C++
For example, if we initialize vector<vector<vector<double>>> f, the dimension in each direction is not specified. So, I am wondering, what command should we insert to make f have size of [3][4][5]. Do we use new or something else?

Thanks for helping me out!

答:
```<code>std::vector<std::vector<std::vector<double>>> f(3, std::vector<std::vector<double>>(4, std::vector<double>(5,0.0)));
</code>```

For anyone who wants it without namespace std.  Also the values in this f[3][4][5] are initialised to 0.0 which might be useful.

Body:
std::vector<std::vector<std::vector<double>>> f(3, std::vector<std::vector<double>>(4, std::vector<double>(5,0.0)));

For anyone who wants it without namespace std.  Also the values in this f[3][4][5] are initialised to 0.0 which might be useful.


问:
Get Explorer's children in batch
I created folder shortcuts  for my taskbar and I would like them to stop launching a new explorer every time

So I decided to create a batch script, howover I can not get the kids from explorer.exe

```<code>@echo off
pushd
tasklist /nh /fi "imagename eq explorer.exe C:\Users\danil\Desktop\ISO" | find /i "explorer.exe C:\Users\danil\Desktop\ISO" > nul ||(start explorer.exe C:\Users\danil\Desktop\ISO)
</code>```

答:
You cannot check the opening folders by checking the command line options, because the arguments stay the same across the whole lifetime of the process even after you changed to some other folders in that window. You need to use scriptable shell objects to get the current address.

Here's a PowerShell script to open a folder if it's not already opened in explorer

$folder = 'C:\Users\danil\Desktop\ISO'
$folderOpened = $false
foreach ($w in (New-Object -ComObject Shell.Application).Windows()) {
    if ($w.LocationURL -ieq ([uri]$folder).AbsoluteUri) {
        $folderOpened = $true; break
    }
}
if (-not $folderOpened) { Invoke-Item $folder } # or start $folder
```
<p>Below is an equivalent hybrid batch-jscript snippet</p>
<pre class="lang-js prettyprint-override"><code>@if (@CodeSection == @Batch) @then

@echo off
cscript //e:jscript //nologo "%~f0" %*
exit /b

@end

// JScript Section

var objShell = new ActiveXObject("shell.application");
var objShellWindows;
objShellWindows = objShell.Windows();

if (objShellWindows != null)
{
    // the folder you want to open
    var folder = "file:///C:/Users/danil/Desktop/ISO";
    var folderOpened = 0;
    for (var objEnum = new Enumerator(objShellWindows);
             !objEnum.atEnd(); objEnum.moveNext())
    {
        if (folder == objEnum.item().LocationUrl)
        {
            folderOpened = 1;
            break;
        }
    }
    if (!folderOpened) // open the folder if it's not already opened
        objShell.Explore(folder); // or objshell.Open(folder)
}
</code>```

Each explorer window is represented by an InternetExplorer object that can be retrieved from the Shell.Windows() collection. You need to use a file URI scheme instead of a normal Windows path, but it works. Of course you can even further change it to switch to the folder window if it's being opened. You can also use VBS or any other languages that support scriptable shell objects

Update:

You can avoid the file URI scheme by changing objEnum.item().LocationUrl to objEnum.item().Document.Folder.Self.Path

In the PowerShell version above it means changing

```<code>if ($w.LocationURL -ieq ([uri]$folder).AbsoluteUri) {
</code>```

to

```<code>if ($w.Document.Folder.Self.Path -ieq $folder) {
</code>```

Title: Get Explorer's children in batch

Body:
I created folder shortcuts  for my taskbar and I would like them to stop launching a new explorer every time

So I decided to create a batch script, howover I can not get the kids from explorer.exe

@echo off
pushd
tasklist /nh /fi "imagename eq explorer.exe C:\Users\danil\Desktop\ISO" | find /i "explorer.exe C:\Users\danil\Desktop\ISO" > nul ||(start explorer.exe C:\Users\danil\Desktop\ISO)

Body:
You cannot check the opening folders by checking the command line options, because the arguments stay the same across the whole lifetime of the process even after you changed to some other folders in that window. You need to use scriptable shell objects to get the current address.

Here's a PowerShell script to open a folder if it's not already opened in explorer

$folder = 'C:\Users\danil\Desktop\ISO'
$folderOpened = $false
foreach ($w in (New-Object -ComObject Shell.Application).Windows()) {
    if ($w.LocationURL -ieq ([uri]$folder).AbsoluteUri) {
        $folderOpened = $true; break
    }
}
if (-not $folderOpened) { Invoke-Item $folder } # or start $folder

Below is an equivalent hybrid batch-jscript snippet

@if (@CodeSection == @Batch) @then

@echo off
cscript //e:jscript //nologo "%~f0" %*
exit /b

@end

// JScript Section

var objShell = new ActiveXObject("shell.application");
var objShellWindows;
objShellWindows = objShell.Windows();

if (objShellWindows != null)
{
    // the folder you want to open
    var folder = "file:///C:/Users/danil/Desktop/ISO";
    var folderOpened = 0;
    for (var objEnum = new Enumerator(objShellWindows);
             !objEnum.atEnd(); objEnum.moveNext())
    {
        if (folder == objEnum.item().LocationUrl)
        {
            folderOpened = 1;
            break;
        }
    }
    if (!folderOpened) // open the folder if it's not already opened
        objShell.Explore(folder); // or objshell.Open(folder)
}

Each explorer window is represented by an InternetExplorer object that can be retrieved from the Shell.Windows() collection. You need to use a file URI scheme instead of a normal Windows path, but it works. Of course you can even further change it to switch to the folder window if it's being opened. You can also use VBS or any other languages that support scriptable shell objects

Update:

You can avoid the file URI scheme by changing objEnum.item().LocationUrl to objEnum.item().Document.Folder.Self.Path

In the PowerShell version above it means changing

if ($w.LocationURL -ieq ([uri]$folder).AbsoluteUri) {

to

if ($w.Document.Folder.Self.Path -ieq $folder) {


问:
Redis sentinel vs clustering
I understand redis sentinel is a way of configuring HA (high availability) among multiple redis instances. As I see, there is one redis instance actively serving the client requests at any given time. There are two additional servers are on standby (waiting for a failure to happen, so one of them can be in action again).

Is it waste of resources?

Is there a better way of using full use of the resources available?

Is Redis clustering an alternative to Redis sentinel?

I already looked up redis documentation for sentinel and clustering, can somebody having experience explain please.

UPDATE

OK. In my real deployment scenario I have two servers dedicated for redis. I have another server my Jboss server is running. The application running in Jboss is configured to connect to redis master server(M).

Failover scenario

Ideally, I think when Master cache server fails (either Redis process goes down or machine failure) the application in Jboss needs to connect to Slave cache server. How would I configure the redis servers to achieve this?

```<code>+--------+          +--------+
| Master  |---------| Slave  |
|         |         |        |
+--------+          +--------+

Configuration: quorum = 1
</code>```

答:
Redis Sentinel performs the failover promoting replicas when they see a master is down. You typically want an odd number of sentinel nodes. For the example of one master and one replica, 3 sentinels should be used so there can be a consensus on the decision. Ideally the 3rd sentinel is on a 3rd server so the decision is not skewed (depending on failure). Sentinel takes care of changing the master/replica config settings on your nodes so that promotion and syncing occurs in the correct order and you don’t overwrite data by bringing on an old failed master that now contains older data.

Once you have your sentinel nodes set up to perform failovers, you need to ensure you are pointing to the correct instance. See an example of HAProxy configuration for this. HAProxy performs health checks and will point to the new master if a failure occurs.

Clustering will allow you to scale horizontally and can help handle high loads. It does take a bit of work to set up and configure up front.

There is an open source fork of Redis, “KeyDB” that has eliminated the need for sentinel nodes with an active-replica option. This allows the replica node to accept reads and writes. When a failover occurs HAProxy stops reads/writes with the failed node and just uses the remaining active node which is already sync’d. Timestamping enables the failed nodes to rejoin automatically and resync without losing data when they come back online. Setup is simple and for higher traffic you don’t need special upfront setup to direct reads to the replica node and read/writes to the master. See example of active replication here. KeyDB is also multi-threaded which for some applications might be an alternative to clustering, but really depends on what your needs are.

There is also an example of setting up clustering manually and with the create-cluster tool. These are the same steps if you are using Redis (replace 'keydb' with 'redis' in instruction)

Title: Redis sentinel vs clustering

Body:
I understand redis sentinel is a way of configuring HA (high availability) among multiple redis instances. As I see, there is one redis instance actively serving the client requests at any given time. There are two additional servers are on standby (waiting for a failure to happen, so one of them can be in action again).

Is it waste of resources?

Is there a better way of using full use of the resources available?

Is Redis clustering an alternative to Redis sentinel?

I already looked up redis documentation for sentinel and clustering, can somebody having experience explain please.

UPDATE

OK. In my real deployment scenario I have two servers dedicated for redis. I have another server my Jboss server is running. The application running in Jboss is configured to connect to redis master server(M).

Failover scenario

Ideally, I think when Master cache server fails (either Redis process goes down or machine failure) the application in Jboss needs to connect to Slave cache server. How would I configure the redis servers to achieve this?

+--------+          +--------+
| Master  |---------| Slave  |
|         |         |        |
+--------+          +--------+

Configuration: quorum = 1


问:
Animate UICollectionView contentInset adjustment
Is there any way to animate the invalidateLayout() call on a UICollectionView layout?  I have a collectionView with a custom layout, and a view that is animating on top of that collectionView.  I would like to animate the repositioning of the cells in the collection view (much like a default UIScrollView implementation would do), but calling invalidateLayout() during an animation block repositions the cells with a fade animation, rather than an interpolation of the frames.

Is there any way to dictate the type of animation used in the layout change?

答: I finally found the answer, and hope this helps someone else.  The trick was simply to call collectionView.performBatchUpdtes(nil, completion: nil) inside the animation block.


问:
circle with dash lines uiview
I am trying to make a circle with dash lines. I am able to make lines in rectangle but I don't know how to make these in circle. Here is answer I got but it's in Objective-C: UIView Draw Circle with Dotted Line Border

Here is my code which makes a rectangle with dashed lines.

```<code>func addDashedBorder() {
    let color = UIColor.red.cgColor

    let shapeLayer:CAShapeLayer = CAShapeLayer()
    let frameSize = self.frame.size
    let shapeRect = CGRect(x: 0, y: 0, width: frameSize.width, height: frameSize.height)

    shapeLayer.bounds = shapeRect
    shapeLayer.position = CGPoint(x: frameSize.width/2, y: frameSize.height/2)
    shapeLayer.fillColor = UIColor.clear.cgColor
    shapeLayer.strokeColor = color
    shapeLayer.lineWidth = 2
    shapeLayer.lineJoin = CAShapeLayerLineJoin.round
    shapeLayer.lineDashPattern = [6,3]
    shapeLayer.path = UIBezierPath(roundedRect: shapeRect, cornerRadius: 5).cgPath

    self.layer.addSublayer(shapeLayer)
}
</code>```

答:
Draw the path using the circle path variant of UIBezierPath

```<code>shapeLayer.path = UIBezierPath(arcCenter: CGPoint(x: frame.size.width * 0.5, y: frame.size.height * 0.5), radius: frame.size.width * 0.5, startAngle: 0, endAngle: .pi * 2, clockwise: true)
</code>```

Title: circle with dash lines uiview

Body:
I am trying to make a circle with dash lines. I am able to make lines in rectangle but I don't know how to make these in circle. Here is answer I got but it's in Objective-C: UIView Draw Circle with Dotted Line Border

Here is my code which makes a rectangle with dashed lines.

func addDashedBorder() {
    let color = UIColor.red.cgColor

    let shapeLayer:CAShapeLayer = CAShapeLayer()
    let frameSize = self.frame.size
    let shapeRect = CGRect(x: 0, y: 0, width: frameSize.width, height: frameSize.height)

    shapeLayer.bounds = shapeRect
    shapeLayer.position = CGPoint(x: frameSize.width/2, y: frameSize.height/2)
    shapeLayer.fillColor = UIColor.clear.cgColor
    shapeLayer.strokeColor = color
    shapeLayer.lineWidth = 2
    shapeLayer.lineJoin = CAShapeLayerLineJoin.round
    shapeLayer.lineDashPattern = [6,3]
    shapeLayer.path = UIBezierPath(roundedRect: shapeRect, cornerRadius: 5).cgPath

    self.layer.addSublayer(shapeLayer)
}

Body:
Draw the path using the circle path variant of UIBezierPath

shapeLayer.path = UIBezierPath(arcCenter: CGPoint(x: frame.size.width * 0.5, y: frame.size.height * 0.5), radius: frame.size.width * 0.5, startAngle: 0, endAngle: .pi * 2, clockwise: true)


问:
How is size of record size set for each record in kafka partition?
I want to know if each record within a kafka topic occupies same size irrespective actual size of the message?

I am aware of the following property -> max.message.bytes=1000012, this will make size of a record upto 1000012 bytes.

My question is if incoming record is less than these bytes , what will be the actual size of the space which is set aside for it?

答:
In Kafka, the broker appends to disk pretty much exactly what the producer has sent.

However as you hinted, the producer wraps the message into a record and a batch.

The full description of these data structures can be found in the documentation.

Apart for very small messages, the disk space occupied by a message depends mostly of its size as the Kafka overhead per message is relatively small.

For example, for a single message of 10 bytes without a key or headers, the size on disk will be 78 bytes. That may seem like a large overhead but as it is almost constant regardless of the message size, if the payload was 1024 bytes, the total disk occupied would only be 1094 bytes.

If you take batching into account, then the overhead diminishes even further as the batch header (~50 bytes) will be used for several records.


问:
Is there a way to pd.cut a scalar?
I am looking for the equivalent of pd.cut but for a scalar?

I would like to do this:

```<code>bins = [0, 5, 10, 15, 20, 25, 30, 40, 50, 100, 150]
pd.cut(43, bins=bins)
</code>```

But currently that throws a value error:

```<code>ValueError: Input array must be 1 dimensional
</code>```

It can be fixed adding []:

```<code>pd.cut([43], bins=bins)
</code>```

Which outputs:

```<code>[(40, 50]]
Categories (10, interval[int64]): [(0, 5] < (5, 10] < (10, 15] < (15, 20] ... (30, 40] < (40, 50] < (50, 100] < (100, 150]]
</code>```

But just wondering if there is a better way to go from scalar to interval in Python in general or with Pandas?

答:
If you just want to get the associated Interval object you can construct an IntervalIndex and filter with get_loc:

```<code>In [2]: bins = [0, 5, 10, 15, 20, 25, 30, 40, 50, 100, 150]

In [3]: ii = pd.IntervalIndex.from_breaks(bins)

In [4]: ii[ii.get_loc(43)]
Out[4]: Interval(40, 50, closed='right')
</code>```

Title: Is there a way to pd.cut a scalar?

Body:
I am looking for the equivalent of pd.cut but for a scalar?

I would like to do this:

bins = [0, 5, 10, 15, 20, 25, 30, 40, 50, 100, 150]
pd.cut(43, bins=bins)

But currently that throws a value error:

ValueError: Input array must be 1 dimensional

It can be fixed adding []:

pd.cut([43], bins=bins)

Which outputs:

[(40, 50]]
Categories (10, interval[int64]): [(0, 5] < (5, 10] < (10, 15] < (15, 20] ... (30, 40] < (40, 50] < (50, 100] < (100, 150]]

But just wondering if there is a better way to go from scalar to interval in Python in general or with Pandas?

Body:
If you just want to get the associated Interval object you can construct an IntervalIndex and filter with get_loc:

In [2]: bins = [0, 5, 10, 15, 20, 25, 30, 40, 50, 100, 150]

In [3]: ii = pd.IntervalIndex.from_breaks(bins)

In [4]: ii[ii.get_loc(43)]
Out[4]: Interval(40, 50, closed='right')


问:
How to Capture text for multiple divs with same data attribute?
Multiple divs contains same html5 data attribute

with same values. Inside each divs are b and p tags with same html 5 data attributes and same values.

requirement is to capture each div values in this format.   and so on. Example  result---
 T1 title1  D1 description1 |  T2 title2  D2 description2 |  T3 title3  D3 description3

Here is the div code

```<code><div data-v-46636d4a="" class="panel_2N_jF">
  <div data-v-46636d4a="" data-purpose="selected-prescription">
    <b data-v-46636d4a="" data-purpose="prescription-name">T1 title1</b>
    <p data-v-46636d4a="" data-purpose="prescription-description">D1 description1</p>
  </div>
  <div data-v-46636d4a="" data-purpose="selected-prescription">
    <b data-v-46636d4a="" data-purpose="prescription-name">T2 title 2</b>
    <p data-v-46636d4a="" data-purpose="prescription-description">D2 description2</p>
  </div>
  <div data-v-46636d4a="" data-purpose="selected-prescription">
    <b data-v-46636d4a="" data-purpose="prescription-name">T3 title3</b>
    <p data-v-46636d4a="" data-purpose="prescription-description">D3 description3</p>
  </div>
</div>
</code>```

I have used simple  this code:

```<code>console.log(
  document.querySelector("body > div:nth-child(1) > div:nth-child(1) > b").textContent + ' '
+ document.querySelector("body > div:nth-child(1) > div:nth-child(1) > p").textContent + ' ' + '|'
+ document.querySelector("body > div:nth-child(1) > div:nth-child(2) > b").textContent + ' '
+ document.querySelector("body > div:nth-child(1) > div:nth-child(2) > p").textContent + ' ' + '|'
+ document.querySelector("body > div:nth-child(1) > div:nth-child(3) > b").textContent + ' '
+ document.querySelector("body > div:nth-child(1) > div:nth-child(3) > p").textContent
);
</code>```

But what is the ideal way to do this for multiple divs.

答:
Check out document.querySelectorAll on MDN. It returns a NodeList of elements, which you can then turn into an array and iterate over (either with Array.from or the spread operator, i.e. new Array(...nodeList).

It looks like your data is like this:

```<code><body>
  <div data-my-attr="value">
    <b>Tag Text 1</b>
    <p>Paragraph text 1</p>
  </div>
  <div data-my-attr="value">
    <b>Tag Text 2</b>
    <p>Paragraph text 2</p>
  </div>
  ... <!-- more divs -->
</body>
</code>```

And the output you want is: Tag Text 1 Paragraph text 1 | Tag Text 2 Paragraph Text 2.

You can get all the divs with a given data attribute by using this function call:

const divs = document.querySelectorAll('div[data-my-attr]').
Then iterate over them:

```<code>const texts = [...divs].map(div => {
  return `${div.querySelector('b').textContent} ${div.querySelector('p').textContent`
})
const result = texts.join('|')
</code>```

Body:
Multiple divs contains same html5 data attribute

with same values. Inside each divs are b and p tags with same html 5 data attributes and same values.

requirement is to capture each div values in this format.   and so on. Example  result---
 T1 title1  D1 description1 |  T2 title2  D2 description2 |  T3 title3  D3 description3

Here is the div code

    T1 title1

D1 description1

    T2 title 2

D2 description2

    T3 title3

D3 description3

I have used simple  this code:

console.log(
  document.querySelector("body > div:nth-child(1) > div:nth-child(1) > b").textContent + ' '
+ document.querySelector("body > div:nth-child(1) > div:nth-child(1) > p").textContent + ' ' + '|'
+ document.querySelector("body > div:nth-child(1) > div:nth-child(2) > b").textContent + ' '
+ document.querySelector("body > div:nth-child(1) > div:nth-child(2) > p").textContent + ' ' + '|'
+ document.querySelector("body > div:nth-child(1) > div:nth-child(3) > b").textContent + ' '
+ document.querySelector("body > div:nth-child(1) > div:nth-child(3) > p").textContent
);

But what is the ideal way to do this for multiple divs.

Body:
Check out document.querySelectorAll on MDN. It returns a NodeList of elements, which you can then turn into an array and iterate over (either with Array.from or the spread operator, i.e. new Array(...nodeList).

It looks like your data is like this:

<body>
  <div data-my-attr="value">
    <b>Tag Text 1</b>
    <p>Paragraph text 1</p>
  </div>
  <div data-my-attr="value">
    <b>Tag Text 2</b>
    <p>Paragraph text 2</p>
  </div>
  ... <!-- more divs -->
</body>

And the output you want is: Tag Text 1 Paragraph text 1 | Tag Text 2 Paragraph Text 2.

You can get all the divs with a given data attribute by using this function call:

const divs = document.querySelectorAll('div[data-my-attr]').
Then iterate over them:

const texts = [...divs].map(div => {
  return `${div.querySelector('b').textContent} ${div.querySelector('p').textContent`
})
const result = texts.join('|')


问:
What does crossover index of 0.25 means in Genetic algorithm for real encoding?
I am familiar with crossover and mutation indexes in binary representation but in real encoding, I came across with several articles in which crossover index and mutation index are used as parameter values.

For example, we have population size of 300 and 30 decision variables then what does crossover index = 0.25 means?

Also confused about the mutation index of 100+current generation number.

答:
Crossover index

A number of real-coded crossover operators have been developed that create two children solutions from two parent solutions.

Maybe the papers you're reading use the Simulated Binary Crossover (SBX).

For this operator the crossover index (η) is a non-negative real parameter. A large value of η gives a higher probability for creating near parent solutions and a small value of η allows distant solutions to be selected as children solutions.

The step by step procedure for SBX algorithm is:

Choose a random number u ∈ [0; 1[.

Calculate βq:

Compute children solutions using these equations:

Here Xi(1, t+1) and Xi(2, t+1) are the children obtained from two parents Xi(1, t) and Xi(2, t).

A possible implementation in C is here (also take a look at Simulated Binary Crossover (SBX) crossover operator in Scala genetic algorithm (GA) library and Simulated Binary Crossover (SBX) crossover operator example).

So the probability distribution for creating children solutions of continuous variables when η=2 / η=5 is:

Parents are marked with o and you can see how a larger value gives higher probability for creating near-parent solutions.

The reference paper for SBX is:

Simulated Binary Crossover for Continuous Search Space

Kalyanmoy Deb, Ram Bhushan Agrawal

1995 (PDF here)

Mutation index

The mutation index (ηₘ) is (probably) a parameter of the polynomial mutation operator suggested by Deb and Agrawal (1999).

ηₘ induces an effect of a perturbation of O((b – a) / ηₘ) in a variable, where a and b are lower and upper bounds of the variable.

Then it's reasonable to use a larger ηₘ for subsequent generations.


问:
How can I select only unordered factors from my data frame?
I cannot find a simple way to select only columns of nominal (unordered factor) variables from a data frame.

I've tried using the expression "!is.ordered" to grab factor variables that are not ordered, but that throws an error.

```<code>df=mtcars
df$am=as.factor(df$am)
df$carb=as.ordered(df$carb)
df2=df[,lapply(df,(is.factor & !is.ordered))]
</code>```

Error in !is.ordered : invalid argument type

This doesn't work either:

```<code>df=mtcars
df$am=as.factor(df$am)
df$carb=as.ordered(df$carb)
df2=df[,sapply(df, is.factor)]
ords=unlist(lapply(df2,is.ordered))
df2=df2[,-ords]
names(df2)
</code>```

NULL

答:
```<code>df[,sapply(df, function(x) is.factor(x) & !is.ordered(x)), drop = FALSE]

dplyr::select_if(df, ~ !is.ordered(.) & is.factor(.)) # Extended by is.factor of utubun
</code>```

From readability statepoint if would prefer the dplyr function, but it´s always nice to have a base R alternative.

Body:
I cannot find a simple way to select only columns of nominal (unordered factor) variables from a data frame.

I've tried using the expression "!is.ordered" to grab factor variables that are not ordered, but that throws an error.

df=mtcars
df$am=as.factor(df$am)
df$carb=as.ordered(df$carb)
df2=df[,lapply(df,(is.factor & !is.ordered))]

Error in !is.ordered : invalid argument type

This doesn't work either:

df=mtcars
df$am=as.factor(df$am)
df$carb=as.ordered(df$carb)
df2=df[,sapply(df, is.factor)]
ords=unlist(lapply(df2,is.ordered))
df2=df2[,-ords]
names(df2)

NULL

Body:
df[,sapply(df, function(x) is.factor(x) & !is.ordered(x)), drop = FALSE]

dplyr::select_if(df, ~ !is.ordered(.) & is.factor(.)) # Extended by is.factor of utubun

From readability statepoint if would prefer the dplyr function, but it´s always nice to have a base R alternative.