问:
Open MPI with icc: "Open MPI requires a C99 compiler"
If I run

```<code>../configure CC=icc CXX=icpc F77=ifort FC=ifort
</code>```

the configuring stops with

```<code>============================================================================
== Compiler and preprocessor tests
============================================================================

*** C compiler and preprocessor
checking for gcc... (cached) icc
checking whether we are using the GNU C compiler... (cached) yes
checking whether icc accepts -g... (cached) yes
checking for icc option to accept ISO C89... (cached) none needed
checking whether icc understands -c and -o together... (cached) yes
checking for icc option to accept ISO C99... unsupported
configure: WARNING: Open MPI requires a C99 compiler
configure: error: Aborting.
</code>```

I'm trying to build Open MPI 3.0.0 from source. I'm using the Intel compilers version 18.0.0 20170811. The environment is Arch Linux.

Why is Open MPI having a problem with icc not being C99 compliant?

答:
There seems to be a problem with the latest ICC if used in combination with glibc 2.26. The problem is still present with version 18.0.0. For now, the fix is to include the flag -D_Float128=__float128:

```<code>../configure CC=icc CXX=icpc FC=ifort CFLAGS="-D_Float128=__float128"
</code>```

Body:
If I run

../configure CC=icc CXX=icpc F77=ifort FC=ifort

the configuring stops with

============================================================================
== Compiler and preprocessor tests
============================================================================

*** C compiler and preprocessor
checking for gcc... (cached) icc
checking whether we are using the GNU C compiler... (cached) yes
checking whether icc accepts -g... (cached) yes
checking for icc option to accept ISO C89... (cached) none needed
checking whether icc understands -c and -o together... (cached) yes
checking for icc option to accept ISO C99... unsupported
configure: WARNING: Open MPI requires a C99 compiler
configure: error: Aborting.

I'm trying to build Open MPI 3.0.0 from source. I'm using the Intel compilers version 18.0.0 20170811. The environment is Arch Linux.

Why is Open MPI having a problem with icc not being C99 compliant?

Body:
There seems to be a problem with the latest ICC if used in combination with glibc 2.26. The problem is still present with version 18.0.0. For now, the fix is to include the flag -D_Float128=__float128:

../configure CC=icc CXX=icpc FC=ifort CFLAGS="-D_Float128=__float128"


来源: stackexchange

问:
use explicit type to limit visibility of method in object to the module defining it
I have a coordinate "class" (implemented using a factory function returning an object literal) in OCaml and am trying to figure out how to restrict the visibility of a method on that object to the module it was defined in.

Here's the source file

```<code>(* coordinate.ml *)
type coordinate = < get_x : int; get_y : int >

let create_coord ~x ~y = object
  val x = x
  val y = y
  method get_x = x
  method get_y = y
  (* hide this method from public interface *)
  method debug_print = Printf.printf "(x=%d, y=%d)\n"
end
</code>```

and here's the interface

```<code>(* coordinate.mli *)
(* the type synonym coordinate is compatible with the "real"
 * type, but does not expose the debug_print method *)
type coordinate = < get_x : int; get_y : int >

val create_coord : x:int -> y:int -> coordinate
</code>```

My intent was to allow the object returned by create_coord to have all of methods visible inside the body of the Coordinate module. The only reason the coordinate type alias is repeated in the .ml file is to satisfy the compiler/allow it to be used in the .mli.

However, I want to prevent consumers of this module from using the debug_print method. I figured that, "since OCaml supports structural typing", an object type with strictly fewer methods would be a "compatible" type for the purposes of type ascription.

However, when I attempt to compile the files I get the following error:

```<code>$ ocamlc coordinate.mli
$ ocamlc coordinate.ml
File "coordinate.ml", line 1:
Error: The implementation coordinate.ml
       does not match the interface coordinate.cmi:
       Values do not match:
         val create_coord :
           x:'a ->
           y:'b ->
           < debug_print : int -> int -> unit; get_x : 'a; get_y : 'b >
       is not included in
         val create_coord : x:int -> y:int -> coordinate
       File "coordinate.ml", line 3, characters 4-16: Actual declaration
Exit 2
</code>```

Is there a way to limit the visibility of debug_print outside of Coordinate while making it freely accessible inside?

答:
You can make some of this work with an explicit coercion.  A minimal example:

```<code>type t = < x : int; y : int >
let make x y : t =
  let o =
    object
      method x = x
      method y = y
      method z = `Not_exposed
    end
  in
  (o :> t)

(* val make : int -> int -> t = <fun> *)
</code>```

That still hides z inside of the module.  You can get a bit further by shadowing make:

```<code>module M : sig
  type t = < x : int; y : int >
  val make : int -> int -> t
end = struct
  type t = < x : int; y : int >

  let make x y = object
    method x = x
    method y = y
    method z = `Not_exposed
  end

  let f o =
    (* Access a method not exposed outside of the module *)
    assert (o#z = `Not_exposed)

  (* Shadow the make function, coercing the result into type t *)
  let make x y =
    (make x y :> t)
end
</code>```

but this will not allow you to expose f as t -> unit as t does not have a z method.

You could also expose two types, keeping the full object type abstract outside of the module's context and providing a function to coerce/convert to the exposed, more restricted type:

```<code>module M : sig
  type t = < x : int; y : int >
  type opaque

  val make : int -> int -> opaque

  val f : opaque -> unit

  val of_opaque : opaque -> t
end = struct
  type t = < x : int; y : int >
  type opaque = < t; z : [`Not_exposed] >

  let make x y = object
    method x = x
    method y = y
    method z = `Not_exposed
  end

  let f (o : opaque) =
    assert (o#z = `Not_exposed)

  let of_opaque (o : opaque) : t =
    (o :> t)
end
</code>```

ContentLicense: CC BY-SA 3.0

Body:
I have a coordinate "class" (implemented using a factory function returning an object literal) in OCaml and am trying to figure out how to restrict the visibility of a method on that object to the module it was defined in.

Here's the source file

(* coordinate.ml *)
type coordinate = < get_x : int; get_y : int >

let create_coord ~x ~y = object
  val x = x
  val y = y
  method get_x = x
  method get_y = y
  (* hide this method from public interface *)
  method debug_print = Printf.printf "(x=%d, y=%d)\n"
end

and here's the interface

(* coordinate.mli *)
(* the type synonym coordinate is compatible with the "real"
 * type, but does not expose the debug_print method *)
type coordinate = < get_x : int; get_y : int >

val create_coord : x:int -> y:int -> coordinate

My intent was to allow the object returned by create_coord to have all of methods visible inside the body of the Coordinate module. The only reason the coordinate type alias is repeated in the .ml file is to satisfy the compiler/allow it to be used in the .mli.

However, I want to prevent consumers of this module from using the debug_print method. I figured that, "since OCaml supports structural typing", an object type with strictly fewer methods would be a "compatible" type for the purposes of type ascription.

However, when I attempt to compile the files I get the following error:

$ ocamlc coordinate.mli
$ ocamlc coordinate.ml
File "coordinate.ml", line 1:
Error: The implementation coordinate.ml
       does not match the interface coordinate.cmi:
       Values do not match:
         val create_coord :
           x:'a ->
           y:'b ->
           < debug_print : int -> int -> unit; get_x : 'a; get_y : 'b >
       is not included in
         val create_coord : x:int -> y:int -> coordinate
       File "coordinate.ml", line 3, characters 4-16: Actual declaration
Exit 2

Is there a way to limit the visibility of debug_print outside of Coordinate while making it freely accessible inside?

Body:
You can make some of this work with an explicit coercion.  A minimal example:

type t = < x : int; y : int >
let make x y : t =
  let o =
    object
      method x = x
      method y = y
      method z = `Not_exposed
    end
  in
  (o :> t)

(* val make : int -> int -> t = <fun> *)

That still hides z inside of the module.  You can get a bit further by shadowing make:

module M : sig
  type t = < x : int; y : int >
  val make : int -> int -> t
end = struct
  type t = < x : int; y : int >

  let make x y = object
    method x = x
    method y = y
    method z = `Not_exposed
  end

  let f o =
    (* Access a method not exposed outside of the module *)
    assert (o#z = `Not_exposed)

  (* Shadow the make function, coercing the result into type t *)
  let make x y =
    (make x y :> t)
end

but this will not allow you to expose f as t -> unit as t does not have a z method.

You could also expose two types, keeping the full object type abstract outside of the module's context and providing a function to coerce/convert to the exposed, more restricted type:

module M : sig
  type t = < x : int; y : int >
  type opaque

  val make : int -> int -> opaque

  val f : opaque -> unit

  val of_opaque : opaque -> t
end = struct
  type t = < x : int; y : int >
  type opaque = < t; z : [`Not_exposed] >

  let make x y = object
    method x = x
    method y = y
    method z = `Not_exposed
  end

  let f (o : opaque) =
    assert (o#z = `Not_exposed)

  let of_opaque (o : opaque) : t =
    (o :> t)
end


问:
div floats to next line
I have a prototype lightbox that will float the second element if the text within it exceeds 28 characters. What am I doing wrong??

This is my current CSS and HTML

// Parent Div
.lightbox-item {
  z-index: 8200;
  top: 50vh;
  left: 50vw;
  width: auto;
  max-height: 650px;
  height: auto;
  display: inline-block;
  background: #fff;
  position: fixed;
}
// First Child div
.lightbox-item .avatarBox {
  height: 155px;
  width: 155px;
  position: relative;
  float: left;
  padding: 0;
  background: green;
}
// Second Child div
.lightbox-item .infoBox {
  padding: 10px 5px 0 0;
  height: auto
  position: relative;
  width: auto;
  float: left;
  background: orange;
}
// Third Child div
.lightbox-item .person-info-wrap {
  width: 100%;
  display: block !important;
  position: relative;
  z-index: 5;
  height: auto;
  clear: left;
}

// Secondary elements nested inside 2nd & 3rd Child div
.lightbox-item .infoBox .gallery-member_fullname{margin: 0 auto;line-height:1em;text-align:left;}
.lightbox-item .infoBox .gallery-member_title{margin: 0 auto;line-height:1em;text-align:left;}
.lightbox-item .infoBox .gallery-member_company{margin: 0 auto;line-height:1em;text-align:left;}
.lightbox-item .infoBox .gallery-company_location{margin: 0 auto;line-height:1em;text-align:left;}

.lightbox-item .person-info {
  padding: 10px 0 25px 0;
  margin: 0 auto;
  background-color: red;
  color: #fff;
  width: auto;
  height: auto;
  min-height: 275px;
}

.lightbox-item .person-info h3 {
  text-transform: uppercase;
  font-size: 1.5em;
  font-weight: 600;
  font-family: "Lato", sans-serif;
  line-height: 30px;
  width: 320px;
  margin: 0 auto;
  text-align: center;
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code>      <div class="lightbox-item" style="opacity: 1; transform: matrix(1, 0, 0, 1, -224.5, -226);">
      <div class="avatarBox"></div>
      <div class="infoBox">
        <h2 class="gallery-member_fullname">Bill Gates</h2>
        <div class="gallery-member_title">Founder</div>
        <div class="gallery-member_company">Bill and Melinda Gates Found</div>
        <div class="gallery-company_location">Nashville, TN 28277
          <br>United States</div>
      </div>
      <div class="person-info-wrap" style="opacity: 1;">
        <div class="person-info">
          <h3>Active committees</h3>
          <ul class="subList">
            <li class="subList-item"></li>
          </ul>
        </div>
      </div>
    </div></code>```

I have the first 2 divs floating to the left and then the 3rd element is set to block and clearing left. The result I'm trying to achieve is so that if the text expands in the 2nd child element the parent container ( lightbox-item) should expand as well.

THE REAL ISSUE

Sorry, I don't believe I made my problem clear. The second child element(infoBox) floats to the next line when the viewport size decrease below 768px I believe. The parent element expands as expect if viewport size is greater than ~768px.

答:
    .lightbox-item {
      z-index: 8200;
      top: 50vh;
      left: 50vw;
      width: auto;
      height: auto;
      display: inline-block;
      background: #fff;
      position: fixed;
    }```
<pre class="snippet-code-html lang-html prettyprint-override"><code><div class="lightbox-item" style="opacity: 1; transform: matrix(1, 0, 0, 1, -224.5, -226);">
       <div class="avatarBox"></div>
        <div class="infoBox">
          <h2 class="gallery-member_fullname">Bill Gates</h2>
          <div class="gallery-member_title">Founder</div>
          <div class="gallery-member_company">Bill and Melinda Gates Found</div>
          <div class="gallery-company_location">Nashville, TN 28277<br>United States</div>
        </div>
        <div class="person-info-wrap" style="opacity: 1;">
          <div class="person-info">
            <h3>Active committees</h3>
          </div>
        </div>
      </div></code>```

Soooooo....

After some tinkering and 2 cups of coffee later. I changed the parent div from display: inline-block to display: inline-table See the change below.

```<code>.lightbox-item {
  z-index: 8200;
  top: 50vh;
  left: 50vw;
  width: auto;
  height: auto;
  display: inline-table;
  background: #fff;
  position: fixed;
}
</code>```

AND Voila!

My lightbox works as I am expecting. I have other media queries to handle how the children divs stack on mobile.

I'm still open to other suggestions but this solved my problem the quickest without having to resort to restructuring the layout ( FlexBox).

Thanks for your input.

Title: div floats to next line

Body:
I have a prototype lightbox that will float the second element if the text within it exceeds 28 characters. What am I doing wrong??

This is my current CSS and HTML

// Parent Div
.lightbox-item {
  z-index: 8200;
  top: 50vh;
  left: 50vw;
  width: auto;
  max-height: 650px;
  height: auto;
  display: inline-block;
  background: #fff;
  position: fixed;
}
// First Child div
.lightbox-item .avatarBox {
  height: 155px;
  width: 155px;
  position: relative;
  float: left;
  padding: 0;
  background: green;
}
// Second Child div
.lightbox-item .infoBox {
  padding: 10px 5px 0 0;
  height: auto
  position: relative;
  width: auto;
  float: left;
  background: orange;
}
// Third Child div
.lightbox-item .person-info-wrap {
  width: 100%;
  display: block !important;
  position: relative;
  z-index: 5;
  height: auto;
  clear: left;
}

// Secondary elements nested inside 2nd & 3rd Child div
.lightbox-item .infoBox .gallery-member_fullname{margin: 0 auto;line-height:1em;text-align:left;}
.lightbox-item .infoBox .gallery-member_title{margin: 0 auto;line-height:1em;text-align:left;}
.lightbox-item .infoBox .gallery-member_company{margin: 0 auto;line-height:1em;text-align:left;}
.lightbox-item .infoBox .gallery-company_location{margin: 0 auto;line-height:1em;text-align:left;}

.lightbox-item .person-info {
  padding: 10px 0 25px 0;
  margin: 0 auto;
  background-color: red;
  color: #fff;
  width: auto;
  height: auto;
  min-height: 275px;
}

.lightbox-item .person-info h3 {
  text-transform: uppercase;
  font-size: 1.5em;
  font-weight: 600;
  font-family: "Lato", sans-serif;
  line-height: 30px;
  width: 320px;
  margin: 0 auto;
  text-align: center;
}

Bill Gates

Founder

Bill and Melinda Gates Found

Nashville, TN 28277

United States

Active committees

I have the first 2 divs floating to the left and then the 3rd element is set to block and clearing left. The result I'm trying to achieve is so that if the text expands in the 2nd child element the parent container ( lightbox-item) should expand as well.

THE REAL ISSUE

Sorry, I don't believe I made my problem clear. The second child element(infoBox) floats to the next line when the viewport size decrease below 768px I believe. The parent element expands as expect if viewport size is greater than ~768px.

Body:
    .lightbox-item {
      z-index: 8200;
      top: 50vh;
      left: 50vw;
      width: auto;
      height: auto;
      display: inline-block;
      background: #fff;
      position: fixed;
    }

<div class="lightbox-item" style="opacity: 1; transform: matrix(1, 0, 0, 1, -224.5, -226);">
       <div class="avatarBox"></div>
        <div class="infoBox">
          <h2 class="gallery-member_fullname">Bill Gates</h2>
          <div class="gallery-member_title">Founder</div>
          <div class="gallery-member_company">Bill and Melinda Gates Found</div>
          <div class="gallery-company_location">Nashville, TN 28277<br>United States</div>
        </div>
        <div class="person-info-wrap" style="opacity: 1;">
          <div class="person-info">
            <h3>Active committees</h3>
          </div>
        </div>
      </div>

Soooooo....

After some tinkering and 2 cups of coffee later. I changed the parent div from display: inline-block to display: inline-table See the change below.

.lightbox-item {
  z-index: 8200;
  top: 50vh;
  left: 50vw;
  width: auto;
  height: auto;
  display: inline-table;
  background: #fff;
  position: fixed;
}

AND Voila!

My lightbox works as I am expecting. I have other media queries to handle how the children divs stack on mobile.

I'm still open to other suggestions but this solved my problem the quickest without having to resort to restructuring the layout ( FlexBox).

Thanks for your input.


问:
Tracking using hybris plugin
I want to perform ecommerce tracking in a hybris site. Will it be possible for non web developers to do it? I want to track add to cart, remove from cart events. I want this to happen using Google analytics and Google tag manager.</p>

答:
The following extension uses Google Tag Manager. It also includes instructions around the implementation as well as the GTM container that can be imported to your Google Tag Manager account.

https://www.hybris.com/en/products/extensions/google-analytics

Title: Tracking using hybris plugin


问:
Drawing weighted, directional graphs in Unity
I am attempting to build a project that allows users to build a flow network and then solve for the maximum flow of the network using Unity. I thought that a graph drawing library would already exist but I'm unable to find it.

As I am relatively new to Unity, my questions are:

Is there a library for drawing weighted, directional graphs in Unity?

If such a library doesn't exist, can anyone recommend some useful tutorials that deal with drawing simple 2d structures (nodes and edges of a graph) in Unity?

答:
If you're new to Unity; I suggest that you look into the Line renderer first, it's a relatively simple interface for drawing weighted graphs.

But, if you wanted to draw primitive lines, you could look at OpenGL lines.

This tutorial also might also be useful in developing intuitive graphs, mind you it doesn't use the Line renderer but the same principles when dealing with Vectors still apply.

I've also used Vectrosity for rapid prototyping (NOTE: I'm in no way tethered to this product).


问:
Events for adding/removing items in a listbox c#.NET
I have a listbox control that has items dynamically added to and manually removed from (due to 'remove item' button). When the number of items is changed, I would like to update other parts of the user interface - namely a caption that says 'You must choose some files.' and an item count caption.

How can an event handler or effectively an event handler be added to fire when the number of items is changed - e.g. an ItemAdded or ItemRemoved or ItemsChanged

Note: This is nothing to do with the user selecting items in the listbox.

Thanks very much!

答:
Create a method to which you can do what you want with the     ListBox Item.
for example:

my program receives data feeds from a server upon request or request for stream.
In my winform1.cs my control for adding data to a list box is as followed.

```<code>public void AddData(string data)
    {
        if (this.ResponseData.InvokeRequired)
            BeginInvoke(new AddDataDelegate(AddData), new object[] { data });
        else
        {
            this.ResponseData.Items.Insert(0, data);
            DataDistro();

        }

    }
</code>```

DataDistro Is what I called My Method for doing work with the new data. Also Note by inserting at index value 0, the new item will Always be on top.

If you are using winForm this is a lot easier, Also, Because the adding of an item is handled by a delegate, the main thread is still open.  If your not using a method that is adding all the data to the listbox this will not work. And using the bindingsource method mentioned above would be the next best thing.

here is an example of my DataDistro method: My response strings look like this: [Q],ATQuoteLastPrice,value
[B],datetime,open,high,low,close,volume

```<code>private void DataDistro()
{
    string data = ListBox.Items[0].ToString();
    string[] split = data.Split(new string[] {","}, stringsplitoptions.None);
    if(spit[0] == "[Q]")
    {
        //do some work
    }
    if(split[0] == "[B]")
    {
         //Do some work
    }
}
</code>```

In your case you would call your method at end of the remove item button click. I would also suggest to make a delegate, or backgroundWorker if the work is extensive. As Calling from a button click Event will be handled by UI Thread.

Everytime the AddData method is called upon, the DataDistro method is also called after data is added.

Body:
Create a method to which you can do what you want with the     ListBox Item.
for example:

my program receives data feeds from a server upon request or request for stream.
In my winform1.cs my control for adding data to a list box is as followed.

public void AddData(string data)
    {
        if (this.ResponseData.InvokeRequired)
            BeginInvoke(new AddDataDelegate(AddData), new object[] { data });
        else
        {
            this.ResponseData.Items.Insert(0, data);
            DataDistro();

        }

    }

DataDistro Is what I called My Method for doing work with the new data. Also Note by inserting at index value 0, the new item will Always be on top.

If you are using winForm this is a lot easier, Also, Because the adding of an item is handled by a delegate, the main thread is still open.  If your not using a method that is adding all the data to the listbox this will not work. And using the bindingsource method mentioned above would be the next best thing.

here is an example of my DataDistro method: My response strings look like this: [Q],ATQuoteLastPrice,value
[B],datetime,open,high,low,close,volume

private void DataDistro()
{
    string data = ListBox.Items[0].ToString();
    string[] split = data.Split(new string[] {","}, stringsplitoptions.None);
    if(spit[0] == "[Q]")
    {
        //do some work
    }
    if(split[0] == "[B]")
    {
         //Do some work
    }
}

In your case you would call your method at end of the remove item button click. I would also suggest to make a delegate, or backgroundWorker if the work is extensive. As Calling from a button click Event will be handled by UI Thread.

Everytime the AddData method is called upon, the DataDistro method is also called after data is added.


问:
How can you get indexes of visible rows for an NSOutlineView?
How can you get a indexes of visible rows for an NSOutlineView?
I need to know which level and which rows are visible.

[EDIT]
What I'm actually looking for is an NSOutlineView equivalent to CocoaTouch/UITableView - (NSArray *)indexPathsForVisibleRows

答:
I changed my datasource to return an NSIndexPath for outlineView:child:ofItem:.
This way I could use [outlineview rowAtPoint:point] (and similar) to get the NSIndexPath.

This change required me to make a set of these indexPaths so they wouldn't get released until I don't need them. Also, all the other code which normally expected a model object now needs to lookup the model object from the index path. In my case this was efficient.

ContentLicense: CC BY-SA 2.5


问:
Get duplicates based on property using linq
I have a class say

```<code>public class Sample
{
    public string property;
    public List<string> someListProperty;
    public string someOtherPropery;
}
</code>```

Now I have a object which is List<Sample>, I need to iterate over this collection and find out items which are having same values for property and someListProperty fields.

I tried this:

```<code>var listSample = List<Sample>()
var result = listSample.GroupBy(x => new { x.property, x.someListProperty})
                       .Where(x => x.Count() > 1).ToList();
</code>```

But this doesn't seem to work, any pointer would be greatly appreciated.

答:
Adapt your linq like this:

```<code>    static void Main(string[] args)
    {
        var samples = new List<Sample>
        {
            new Sample("p1", "aaa,bbb,ccc,ddd"),
            new Sample("p1", "bbb,ccc,xxx"),
            new Sample("p2", "aaa,bbb,ccc"),
            new Sample("p1", "xxx")
        };

        var grp = samples.GroupBy(b => b.property)
            .Where(a => a.Key == "p1")
            .SelectMany(s => s.ToList())
            .Where(b => b.someListProperty.Contains("ccc"));

        foreach (var g in grp)
            System.Console.WriteLine(g.ToString());

        System.Console.ReadLine();
    }

    private class Sample
    {
        public string property;

        public List<string> someListProperty;

        public string someOtherPropery;

        public Sample(string p, string props)
        {
            property = p;
            someListProperty = props.Split(',').ToList();
            someOtherPropery = string.Concat(from s in someListProperty select s[0]);
        }

        public override string ToString()
        {
            return $"{property} - {string.Join(", ", someListProperty)} -"
                       + $" ({someOtherPropery})";
        }
    }
</code>```

Misread - I read you wanted that "some" of the someListProperty should be in for the ones you want to filter from the grouping.

You could always group like this to make it happen:

```<code>var grp = samples
    // choose a joiner thats not in your somePropertyList-data
    .GroupBy(b => $"{b.property}:{string.Join("|", b.someListProperty)}")
    .Where(g => g.Skip(1).Any())
    .SelectMany(s => s.ToList())
</code>```

But be aware that this is kindof hackish and depends on your data. You essentially group over all that is interesting for you and choose all that have multiple results.

Body:
I have a class say

public class Sample
{
    public string property;
    public List<string> someListProperty;
    public string someOtherPropery;
}

Now I have a object which is List<Sample>, I need to iterate over this collection and find out items which are having same values for property and someListProperty fields.

I tried this:

var listSample = List<Sample>()
var result = listSample.GroupBy(x => new { x.property, x.someListProperty})
                       .Where(x => x.Count() > 1).ToList();

But this doesn't seem to work, any pointer would be greatly appreciated.

Body:
Adapt your linq like this:

    static void Main(string[] args)
    {
        var samples = new List<Sample>
        {
            new Sample("p1", "aaa,bbb,ccc,ddd"),
            new Sample("p1", "bbb,ccc,xxx"),
            new Sample("p2", "aaa,bbb,ccc"),
            new Sample("p1", "xxx")
        };

        var grp = samples.GroupBy(b => b.property)
            .Where(a => a.Key == "p1")
            .SelectMany(s => s.ToList())
            .Where(b => b.someListProperty.Contains("ccc"));

        foreach (var g in grp)
            System.Console.WriteLine(g.ToString());

        System.Console.ReadLine();
    }

    private class Sample
    {
        public string property;

        public List<string> someListProperty;

        public string someOtherPropery;

        public Sample(string p, string props)
        {
            property = p;
            someListProperty = props.Split(',').ToList();
            someOtherPropery = string.Concat(from s in someListProperty select s[0]);
        }

        public override string ToString()
        {
            return $"{property} - {string.Join(", ", someListProperty)} -"
                       + $" ({someOtherPropery})";
        }
    }

Misread - I read you wanted that "some" of the someListProperty should be in for the ones you want to filter from the grouping.

You could always group like this to make it happen:

var grp = samples
    // choose a joiner thats not in your somePropertyList-data
    .GroupBy(b => $"{b.property}:{string.Join("|", b.someListProperty)}")
    .Where(g => g.Skip(1).Any())
    .SelectMany(s => s.ToList())

But be aware that this is kindof hackish and depends on your data. You essentially group over all that is interesting for you and choose all that have multiple results.


问:
Weird crashing program while debug runs smoothly (Eclipse C++)
I'm writing a program for my algorithmic math class at university and I'm using Win 7 (x64), Eclipse Oxygen.1a Release (4.7.1a) with MinGW 6.3.0.

Whenever I build and run the program it crashes with windows claiming 'Abgabe3.exe stopped working' but when trying to find the problem using the debugger and breakpoints I step trough the whole program and it finishes without errors...

I stripped everything not used by the problematic function and copied everything into a seperate file and the exact problem occurs.
Maybe somebody has a clue what happened at my side. ^^

```<code>#include <math.h>       /* pow, sqrt */
#include <iostream>     /* cin, cout */
#include <new>          /* new */
#include <string>       /* string */
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */

using namespace std;

void NORM(double* res, double* x, int n){
    res[0] = 0.0;
    for(int i = 0; i < n; i++){
        res[0] += pow(x[i], 2);
    }
    res[0] = sqrt(res[0]);
}

void initRand(double* x, int n){
    srand (time(NULL) * rand());
    for(int i = 0; i < n; i++){
        x[i] = (((double) rand()) / ((double) RAND_MAX));
    }
}

void createArray(double* &x, int n){
    if (n > 0){
        x = new double[n];
        initRand(x, n);
    }
}

void printArray(double* x, int n){
    if (x != NULL){
    cout<<"(\n";
    for(int i = 0; i < n; i++){
        if(i+1 == n) cout<<x[i];
        else if ((i % 5) == 0) cout<<x[i];
        else if ( ((i+1) % 5) == 0 ){
            cout<<", "<<x[i]<<"\n";
        }
        else {
            cout<<", "<<x[i];
        }
    }
    cout<<"\n)\n";
    }
    else cout<<"\nError: pointer = NULL\n";
}

unsigned long long int bin(unsigned int n, unsigned int k){
    unsigned long long res = 1;
    if(k == 0) return 1;
    else if( n >= k){
        for(unsigned long long int i = 1; i <= k; i++){
            res *= (n + 1 - i) / i;
        }
    }
    else return 0;
    return res;
}

void newArray(double** x, unsigned int v, unsigned int n){
    for(unsigned int i = 0; i < v; i++){
        double* ptr = x[i];
        createArray(ptr,n);
        x[i] = ptr;
    }
}

void experiment(double** vektorArray){
    unsigned int n = 10, v = 20;
    cout<<"Dimension n = "<<n<<"\nAnzahl Versuche v = "<<v<<endl;
    //Erstellen der Vektoren
    cout<<"Erstellen - starte\n";

    vektorArray = new double*[n];
    newArray(vektorArray, v, n);

    cout<<"Erstellen - fertig\n";

    for(unsigned int i = 0; i < v; i++){
        if(i%10 == 0) printArray(vektorArray[i], n);
    }
}
int main(int argc, char** argv){
    double** vektorArray = NULL;
    experiment(vektorArray);
    return 0;
}
</code>```

答:
```<code>vektorArray = new double*[n];
</code>```

created an array of size n, but

```<code>void newArray(double** x, unsigned int v, unsigned int n)
{
    for (unsigned int i = 0; i < v; i++)
    {
        double* ptr = x[i];
        createArray(ptr, n);
        x[i] = ptr;
    }
}
</code>```

and

```<code>for (unsigned int i = 0; i < v; i++)
{
    if (i % 10 == 0)
        printArray(vektorArray[i], n);
}
</code>```

index that array with v. Looks like you got your variables crossed. Strongly recommend giving variables better, more descriptive names to help make this more obvious.

ContentLicense: CC BY-SA 4.0

Body:
I'm writing a program for my algorithmic math class at university and I'm using Win 7 (x64), Eclipse Oxygen.1a Release (4.7.1a) with MinGW 6.3.0.

Whenever I build and run the program it crashes with windows claiming 'Abgabe3.exe stopped working' but when trying to find the problem using the debugger and breakpoints I step trough the whole program and it finishes without errors...

I stripped everything not used by the problematic function and copied everything into a seperate file and the exact problem occurs.
Maybe somebody has a clue what happened at my side. ^^

#include <math.h>       /* pow, sqrt */
#include <iostream>     /* cin, cout */
#include <new>          /* new */
#include <string>       /* string */
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */

using namespace std;

void NORM(double* res, double* x, int n){
    res[0] = 0.0;
    for(int i = 0; i < n; i++){
        res[0] += pow(x[i], 2);
    }
    res[0] = sqrt(res[0]);
}

void initRand(double* x, int n){
    srand (time(NULL) * rand());
    for(int i = 0; i < n; i++){
        x[i] = (((double) rand()) / ((double) RAND_MAX));
    }
}

void createArray(double* &x, int n){
    if (n > 0){
        x = new double[n];
        initRand(x, n);
    }
}

void printArray(double* x, int n){
    if (x != NULL){
    cout<<"(\n";
    for(int i = 0; i < n; i++){
        if(i+1 == n) cout<<x[i];
        else if ((i % 5) == 0) cout<<x[i];
        else if ( ((i+1) % 5) == 0 ){
            cout<<", "<<x[i]<<"\n";
        }
        else {
            cout<<", "<<x[i];
        }
    }
    cout<<"\n)\n";
    }
    else cout<<"\nError: pointer = NULL\n";
}

unsigned long long int bin(unsigned int n, unsigned int k){
    unsigned long long res = 1;
    if(k == 0) return 1;
    else if( n >= k){
        for(unsigned long long int i = 1; i <= k; i++){
            res *= (n + 1 - i) / i;
        }
    }
    else return 0;
    return res;
}

void newArray(double** x, unsigned int v, unsigned int n){
    for(unsigned int i = 0; i < v; i++){
        double* ptr = x[i];
        createArray(ptr,n);
        x[i] = ptr;
    }
}

void experiment(double** vektorArray){
    unsigned int n = 10, v = 20;
    cout<<"Dimension n = "<<n<<"\nAnzahl Versuche v = "<<v<<endl;
    //Erstellen der Vektoren
    cout<<"Erstellen - starte\n";

    vektorArray = new double*[n];
    newArray(vektorArray, v, n);

    cout<<"Erstellen - fertig\n";

    for(unsigned int i = 0; i < v; i++){
        if(i%10 == 0) printArray(vektorArray[i], n);
    }
}
int main(int argc, char** argv){
    double** vektorArray = NULL;
    experiment(vektorArray);
    return 0;
}

Body:
vektorArray = new double*[n];

created an array of size n, but

void newArray(double** x, unsigned int v, unsigned int n)
{
    for (unsigned int i = 0; i < v; i++)
    {
        double* ptr = x[i];
        createArray(ptr, n);
        x[i] = ptr;
    }
}

and

for (unsigned int i = 0; i < v; i++)
{
    if (i % 10 == 0)
        printArray(vektorArray[i], n);
}

index that array with v. Looks like you got your variables crossed. Strongly recommend giving variables better, more descriptive names to help make this more obvious.


问:
MariaDB database COLLATE change query
I have 3 queries to change the COLLATION. They should work but they does not. Looks like only [!varchar] columns by 2nd query are affected. Can anybody tell what is the problem there? I need to convert from utf8_general_ci to utf8_bin.
I get results like:

```<code>ALTER TABLE AO_21F425_MESSAGE_MAPPING_AO MODIFY MESSAGE_ID varchar CHARACTER SET UTF8 COLLATE utf8_bin NOT NULL;
ALTER TABLE AO_21F425_MESSAGE_MAPPING_AO MODIFY USER_HASH varchar CHARACTER SET UTF8 COLLATE utf8_bin NOT NULL;
ALTER TABLE AO_38321B_CUSTOM_CONTENT_LINK MODIFY LINK_URL varchar CHARACTER SET UTF8 COLLATE utf8_bin;
</code>```

and so on... but no changes in DB.

1) Change columns with type VARCHAR

```<code>SELECT CONCAT('ALTER TABLE `', table_name,
        '` MODIFY `', column_name, '` ', DATA_TYPE, '(', CHARACTER_MAXIMUM_LENGTH,
        ') CHARACTER SET UTF8 COLLATE utf8_bin',
        (CASE WHEN IS_NULLABLE = 'NO' THEN ' NOT NULL' ELSE '' END), ';')
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'my_database_name'
AND DATA_TYPE = 'varchar'
AND
(
    CHARACTER_SET_NAME != 'utf8'
    OR
    COLLATION_NAME != 'utf8_bin'
);
</code>```

2) Change columns with other than VARCHAR type

```<code>SELECT CONCAT('ALTER TABLE `', table_name,
    '` MODIFY `', column_name, '` ', DATA_TYPE,
    ' CHARACTER SET UTF8 COLLATE utf8_bin',
    (CASE WHEN IS_NULLABLE = 'NO' THEN ' NOT NULL' ELSE '' END), ';')
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'my_database_name'
AND DATA_TYPE != 'varchar'
AND
(
    CHARACTER_SET_NAME != 'utf8'
    OR
    COLLATION_NAME != 'utf8_bin'
);
</code>```

3) Change table level

```<code>SELECT CONCAT('ALTER TABLE ',  table_name, ' CHARACTER SET utf8 COLLATE utf8_bin;')
FROM information_schema.TABLES AS T, information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` AS C
WHERE C.collation_name = T.table_collation
AND T.table_schema = 'my_database_name'
AND
(
    C.CHARACTER_SET_NAME != 'utf8'
    OR
    C.COLLATION_NAME != 'utf8_bin'
);
</code>```

答:
Beware; the ALTER may not be what you want!

Are you coming from charset latin1 and the data is correctly encoded as latin1 now?  Then you need ALTER TABLE ... CONVERT TO ...

Are you coming from latin1, but the bytes were utf8?  Then you need two alters.

What is your exact situation?

After determining what you really need, please provide the resulting ALTERs for us to critique.

These links may help:

Diagnosis, Best practice, Common problems: Trouble with UTF-8 characters; what I see is not what I stored

Fixes: http://mysql.rjweb.org/doc.php/charcoll#fixes_for_various_cases

more

In

```<code>ALTER  TABLE AO_21F425_MESSAGE_MAPPING_AO MODIFY MESSAGE_ID
      varchar CHARACTER SET UTF8 COLLATE utf8_bin NOT NULL;
</code>```

you must specify the size of the varchar.

Title: MariaDB database COLLATE change query

Body:
I have 3 queries to change the COLLATION. They should work but they does not. Looks like only [!varchar] columns by 2nd query are affected. Can anybody tell what is the problem there? I need to convert from utf8_general_ci to utf8_bin.
I get results like:

ALTER TABLE AO_21F425_MESSAGE_MAPPING_AO MODIFY MESSAGE_ID varchar CHARACTER SET UTF8 COLLATE utf8_bin NOT NULL;
ALTER TABLE AO_21F425_MESSAGE_MAPPING_AO MODIFY USER_HASH varchar CHARACTER SET UTF8 COLLATE utf8_bin NOT NULL;
ALTER TABLE AO_38321B_CUSTOM_CONTENT_LINK MODIFY LINK_URL varchar CHARACTER SET UTF8 COLLATE utf8_bin;

and so on... but no changes in DB.

1) Change columns with type VARCHAR

SELECT CONCAT('ALTER TABLE `', table_name,
        '` MODIFY `', column_name, '` ', DATA_TYPE, '(', CHARACTER_MAXIMUM_LENGTH,
        ') CHARACTER SET UTF8 COLLATE utf8_bin',
        (CASE WHEN IS_NULLABLE = 'NO' THEN ' NOT NULL' ELSE '' END), ';')
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'my_database_name'
AND DATA_TYPE = 'varchar'
AND
(
    CHARACTER_SET_NAME != 'utf8'
    OR
    COLLATION_NAME != 'utf8_bin'
);

2) Change columns with other than VARCHAR type

SELECT CONCAT('ALTER TABLE `', table_name,
    '` MODIFY `', column_name, '` ', DATA_TYPE,
    ' CHARACTER SET UTF8 COLLATE utf8_bin',
    (CASE WHEN IS_NULLABLE = 'NO' THEN ' NOT NULL' ELSE '' END), ';')
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'my_database_name'
AND DATA_TYPE != 'varchar'
AND
(
    CHARACTER_SET_NAME != 'utf8'
    OR
    COLLATION_NAME != 'utf8_bin'
);

3) Change table level

SELECT CONCAT('ALTER TABLE ',  table_name, ' CHARACTER SET utf8 COLLATE utf8_bin;')
FROM information_schema.TABLES AS T, information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` AS C
WHERE C.collation_name = T.table_collation
AND T.table_schema = 'my_database_name'
AND
(
    C.CHARACTER_SET_NAME != 'utf8'
    OR
    C.COLLATION_NAME != 'utf8_bin'
);

Body:
Beware; the ALTER may not be what you want!

Are you coming from charset latin1 and the data is correctly encoded as latin1 now?  Then you need ALTER TABLE ... CONVERT TO ...

Are you coming from latin1, but the bytes were utf8?  Then you need two alters.

What is your exact situation?

After determining what you really need, please provide the resulting ALTERs for us to critique.

These links may help:

Diagnosis, Best practice, Common problems: Trouble with UTF-8 characters; what I see is not what I stored

Fixes: http://mysql.rjweb.org/doc.php/charcoll#fixes_for_various_cases

more

In

ALTER  TABLE AO_21F425_MESSAGE_MAPPING_AO MODIFY MESSAGE_ID
      varchar CHARACTER SET UTF8 COLLATE utf8_bin NOT NULL;

you must specify the size of the varchar.


问:
Pull ssh-client container via docker-compose
I'm new to Docker and may not understand something.

I have an app container and a dockerfile.

I want to install an ssh-client into the app container.
I want to be able to run ssh command to reach some external ip from the app container.

The simplest way I know is to add

RUN apk update && apk add openssh-client bash

to my dockerfile, but I'd like to use some ready image, so all my containers could use the same one.

Is it possible to do so?

答:
If you search over dockerhub where all the public images reside, you will find that all the "ssh-client" popular images there are simply building on top of alpine and installing openssh-client exactly that way you described it.

So there is no obvious benefit in using those existing images. Just install the ssh-client via:

```<code>RUN apk update && apk-install openssh-client
</code>```

Body:
If you search over dockerhub where all the public images reside, you will find that all the "ssh-client" popular images there are simply building on top of alpine and installing openssh-client exactly that way you described it.

So there is no obvious benefit in using those existing images. Just install the ssh-client via:

RUN apk update && apk-install openssh-client


问:
When using Virtual Documents how can a template be used with different report parameters?
I'm using a virtual document structure generated by a script to create a document from EA, and I'm trying to use the same template fragment several times with different elements and different headings.

For example, I have an element which describes the input data to one program, and the output data to another program, so I can't really store the information in the element I'm documenting.

Where it is the input, I want one  heading (and similar references within the template), and where it is output I want different values for the headings.

I've tried using the ReportTitle tagged value in the individual <model document> element, but this appears to be ignored and only the <report package> value is used throughout (which makes me wonder why they are there in the first place).

While I could create multiple templates all referring to the same fragment and hard-code the different headings, but that is messy, and as I already have fragments within fragments so it could result in a lot of almost identical templates and fragments. Variables that I can set for each <model document> would be much preferable.

Has anyone got a better approach than this? Thanks!

答:
I don't think there's an easy solution.

If there is a way to determine based on the element, package, or diagram ID whether you should use one title or the other then you could use a script or SQL fragment to return the correct title.

If that is not the case I guess the only possibility is to hardcode the different titles in your templates.
In order to avoid too much duplication you could create a template with only the title and use that on a model document. Since you are generating the modeldocuments by a script anyway that doesn't cost any user time.


问:
VIM navigate tab in Mac OS
I recently started using OSX 10.8.4, earlier to this I was a Windows user. I installed a Ubuntu 12.04 VM my mac OS. I'm wondering if someone can assist me how to "navigate tabs in VIM" from inside Ubuntu VM. Hovering the mouse over "Tabs" option list commands as: NextTab: Control+PageDown, PreviousTab:Control+PageUp but Mac Pro does not have any PageUp or PageDown buttons.

答:
You can use: gt and gT

(based on the comment of FDinoff which IMO could be upgraded to answer)

Title: VIM navigate tab in Mac OS


问:
Amazon AWS ELB without public address on servers behind it
I currently have 3 servers sitting behind a ELB on AWS.

Each of these EC2 instance sit in 3 separate availability zones.

I use the ELB for

SSL Termination

Distribute Load

I have already configured a VPN to access the EC2 Instance for SSH access however I cannot get the ELB to work when I remove the public addresses from the EC2 containers...

I assumed that I could have them allow traffic only on port 80 (443 terminated on ELB) from the ELB sec group, which would mean I wouldn't need Ext IPs as ELB connects directly to them?

I assume i would need to also setup NAT for them to be able to externally access?

Are ELBs not within a subnet?

Tried all variations coming to conclusion they need public IPs but just restrict what has access?

Many thanks in advance!

答: I had a concern of this getting exposed to public and that can be exploited by DDoS. I had to front it with API Gateway and then trust in VPC using HAProxy more details are here. http://knowmg.blogspot.com/2017/11/why-do-i-need-haproxy-in-aws-stack.html</p>


问:
Using a JavaScript file into Ionic 3
I have a problem in my project and I set it using JavaScript, I want to know how can I use a JS file in an Ionic 3 project?

thanks.

答: You can include your JS file in src/index.html and then use its methods/properties in TS file by declaring it as a variable by using declare keyword as Patrick Odum in the above answer told.</p>

Title: Using a JavaScript file into Ionic 3


问:
CouchDB - prevent unauthorized reads
CouchDB has a mechanism in place to prevent unauthorized writes.

Can it also prevent unauthorized reads?

答:
Yes, CouchDB can prevent unauthorized reads. Unfortunately, it is slightly less straightforward.

Imagine a secret auction application. You bid $20 and I bid $10; each bid in a couch document. Couch lets us read our own bid documents but no others. However, there is a map-reduce view showing the average. I load the view and see that the average is $15, so I conclude that your bid is $20 and I have broken the security policy. View output can leak some or all of a document's information. It is not feasible to enforce security at the document level. That is why read access is at the database level.

I know, it sucks. But that is the only correct, scalable answer.

This is part of the reason the Couch philosophy is to create many databases—even one (or more!) per user. Read permission to a database is set in the readers value of the database _security object. (Note, the field readers was renamed to members in CouchDB trunk because it also specifies who may write to the DB.)

The technique works like this:

Create a database for each user. It will hold all documents the user may read. Add the user (or the user's role) to the _security object.

In the master database, create a filter function which implements the read policy. (It could share code with validate_doc_update.)

Replicate from the master database to the user's database with ?filter=my_filter_function.

Allow the user to load (or replicate from) their database.

Of course, this is all for a pure Couch application, where users access Couch directly. If you have a middle layer (MVC controller, or just a reverse HTTP proxy), then you can enforce policy there, between the user and the couch. But be careful. For example, a _show function or a _rewrite rule might allow a user to load a view or document despite your policy.

Good luck!

Title: CouchDB - prevent unauthorized reads


问:
OID: value is hex-coded
I try to parse a X509-issuer-string.
For reasons I do not know some attributes are coded in OID.

E.g.

```<code>CN=TEST,DN=my.test.com,O=My State,C=MS,2.5.4.97=#130e414141505050
</code>```

My question is about the last one. I am perfectly fine that it is not named but with the OID.

But I have no idea how to interpret the value. I expect it to be 2-digit-hex per character. But what about the first 4 numbers? That would be some control-characters.

414141505050 would be the value I expect (AAAPPP).

But what is the meaning of the first 4 bytes (130e)??

I did not find any definition of the coding of a hex-value for an OID.

I faked the values for privacy - so if it is a form of checksum it will be corrupt.

Thanks for help!

答:
2.5.4.97 is the OID for organizationIdentifier (http://oid-info.com/get/2.5.4.97), which OpenSSL is printing as the OID because it doesn't have that in the OID to name map.

One presumes that the # is OpenSSL-ese for "this is the raw data, hex encoded".  The raw value is a DER encoded value.  0x13 identifies that this is a (primitive encoding) Printable String.  The next byte (0x0E) says that it is 14 bytes long.

Since your 414141505050 is only 6 bytes long, one assumes that you originally had a longer value.

Title: OID: value is hex-coded

Body:
I try to parse a X509-issuer-string.
For reasons I do not know some attributes are coded in OID.

E.g.

CN=TEST,DN=my.test.com,O=My State,C=MS,2.5.4.97=#130e414141505050

My question is about the last one. I am perfectly fine that it is not named but with the OID.

But I have no idea how to interpret the value. I expect it to be 2-digit-hex per character. But what about the first 4 numbers? That would be some control-characters.

414141505050 would be the value I expect (AAAPPP).

But what is the meaning of the first 4 bytes (130e)??

I did not find any definition of the coding of a hex-value for an OID.

I faked the values for privacy - so if it is a form of checksum it will be corrupt.

Thanks for help!


问:
How do I access a public page variable of the only page from the master page?
I have a page - only ONE page for the entire site:

```<code>using System;
using System.Text.RegularExpressions;
using System.IO;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class CMSPageViewer : System.Web.UI.Page
{
    public int myPageID;

    protected void Page_PreInit(object sender, EventArgs e)
    {
         // Get stuff from the database

         myPageID = 1;

         // Set the Page.MasterPageFile
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        // Fill in the page contents
        // This looks for and fills in asp:ContentPlaceHolder controls based on ID
    }
}
</code>```

And the master page:

```<code>using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Standard_Page : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int myID = (this.Page as CMSPageViewer).myPageID;
    }
}
</code>```

How can I access ID from the MasterPage that is used?

Casting the page doesn't seem to work. I get the following error:

```<code>Compiler Error Message: CS0246: The type or namespace name 'CMSPageViewer' could not be found (are you missing a using directive or an assembly reference?)
</code>```

答:
Define a variable in your master page. Give it proper getter/setters. In each page call the master.setter and "pass the variable into" the master page.

At the very least you're going about it wrong, but this is what you have to do.

Referencing "ASP.NET 3.5 Unleashed" http://www.amazon.com/dp/0672330113 section in Chapter 5 pages 255 - 260 for modifying Master properties based on the Page being interpreted.

Again, Referencing ASP.NET 3.5 Unleashed Chapter 6 which starts off:

Themes are different than Master Pages. A Master Page enables you to share content across multiple pages in a website. A Theme, on the other hand, enables you to control the appearance of the content.

So in other words, you're not supposed to use a Master Page as a Theme, the two work together.

Do you want me to keep going? I've got a few other books. I've done exactly what you're trying to do (modify the master based on the content) and at the time I knew it was a hack, but I did it because I had to. So what I'm telling you is what I did, which is what works.

Now please, again, tell me that what I'm suggesting can't be done. I'll go find some more references that will explain why you can't cast to a Page from within the Master.

For those looking to lay the smack down on my turning to a book for reference, note that that's how the big boys play, it's how the people who get the actual contracts play, and it's how you inform your boss. It's a book. That's what they're for. Additionally, Stephen Walther is not a nobody, he was a Senior Product Manager for the ASP.NET team. So he knows what the hell he's talking about. I would listen to his advice. Hell, I bought his book didn't I?

Body:
I have a page - only ONE page for the entire site:

using System;
using System.Text.RegularExpressions;
using System.IO;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class CMSPageViewer : System.Web.UI.Page
{
    public int myPageID;

    protected void Page_PreInit(object sender, EventArgs e)
    {
         // Get stuff from the database

         myPageID = 1;

         // Set the Page.MasterPageFile
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        // Fill in the page contents
        // This looks for and fills in asp:ContentPlaceHolder controls based on ID
    }
}

And the master page:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Standard_Page : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int myID = (this.Page as CMSPageViewer).myPageID;
    }
}

How can I access ID from the MasterPage that is used?

Casting the page doesn't seem to work. I get the following error:

Compiler Error Message: CS0246: The type or namespace name 'CMSPageViewer' could not be found (are you missing a using directive or an assembly reference?)


问:
Incorrect output in Depth First Search Implementation
Recently, I was looking at a code for Depth First Search in a website (https://brilliant.org/wiki/depth-first-search-dfs/). However their implementation is not completly correct.This is the code they posted

```<code>def depth_first_search(graph):
    visited, stack = set(), [root]
    while stack:
        vertex = stack.pop()
        if vertex not in visited:
            visited.add(vertex)
            stack.extend(graph[vertex] - visited)
    return visited
</code>```

As you can see, the logic they applied is correct but they used set operation which changes the output everytime the program runs.

This is my complete program

```<code>graph = {'A': {'B', 'S'}, 'B': {'A'}, 'C': {'S', 'F', 'D', 'E'},
     'D': {'C'}, 'E': {'H', 'C'}, 'F': {'C', 'G'}, 'G': {'S', 'F', 'H'},
     'H': {'G', 'E'}, 'S': {'A', 'G', 'C'}}

def depth_first_search(graph, root):
    visited, stack = set(), [root]
    while stack:
        vertex = stack.pop()
        if vertex not in visited:
            visited.add(vertex)
            stack.extend(graph[vertex] - visited)
    return visited

print(depth_first_search(graph, 'A'))
</code>```

Below are the outputs I get evertime I run the program

```<code>{'H', 'C', 'B', 'A', 'D', 'S', 'F', 'E', 'G'}
{'D', 'E', 'C', 'H', 'G', 'S', 'A', 'B', 'F'}
{'G', 'F', 'C', 'H', 'E', 'D', 'B', 'S', 'A'} and so on....
</code>```

The reason for using set especially makes sense for the below line of code as we want the stack to store only the vertices that are unexplored.

```<code>stack.extend(graph[vertex] - visited)
</code>```

So performing set difference operation achieves that objective.But it comes at a cost as mentioned above.So I tweaked the code a bit to avoid using set and make do with lists

```<code>graph = {'A': ['B', 'S'], 'B': ['A'], 'C': ['S', 'F', 'D', 'E'],
 'D': ['C'], 'E': ['H', 'C'], 'F': ['C', 'G'], 'G': ['S', 'F', 'H'],
  'H': ['G', 'E'], 'S': ['A', 'G', 'C']}

def depth_first_search(graph, root):
    visited, stack = [], [root]
    while stack:
        vertex = stack.pop()
        if vertex not in visited:
            visited.append(vertex)
            neighbours = graph[vertex]
            for neighbour in neighbours:
                # ensures we only add unexplored nodes to the stack
                if neighbour not in visited:
                    stack.append(neighbour)
    return visited

print(depth_first_search(graph, 'A'))
</code>```

But I get a wrong result

```<code>['A', 'S', 'C', 'E', 'H', 'G', 'F', 'D', 'B']
</code>```

The correct result must be

```<code>['A', 'B', 'S', 'C', 'D', 'E', 'H', 'G', 'F']
</code>```

What am I doing wrong?

答:
The answer you're getting is a valid DFS order for that graph. There seems to be an unwritten restriction that a node's neighbors must be traversed in alphabetical order. With that in mind, two things:

First, you're adding the neighbors of a node to the stack in the order in which they're defined. But when you pop() off the stack, you take the last item off the stack first. That means you're selecting your nodes in reverse order. That's easy enough to fix by reversing the order in which you iterate over the neighbors:

```<code>for neighbour in reversed(neighbours):
</code>```

Second, you haven't actually defined the nodes' neighbors in graph alphabetically. You either need to alphabetize the values of graph in the definition, or sort them before iterating:

```<code>for neighbour in reversed(sorted(neighbours)):
# or
for neighbour in sorted(neighbours, reverse=True):
</code>```

Body:
Recently, I was looking at a code for Depth First Search in a website (https://brilliant.org/wiki/depth-first-search-dfs/). However their implementation is not completly correct.This is the code they posted

def depth_first_search(graph):
    visited, stack = set(), [root]
    while stack:
        vertex = stack.pop()
        if vertex not in visited:
            visited.add(vertex)
            stack.extend(graph[vertex] - visited)
    return visited

As you can see, the logic they applied is correct but they used set operation which changes the output everytime the program runs.

This is my complete program

graph = {'A': {'B', 'S'}, 'B': {'A'}, 'C': {'S', 'F', 'D', 'E'},
     'D': {'C'}, 'E': {'H', 'C'}, 'F': {'C', 'G'}, 'G': {'S', 'F', 'H'},
     'H': {'G', 'E'}, 'S': {'A', 'G', 'C'}}

def depth_first_search(graph, root):
    visited, stack = set(), [root]
    while stack:
        vertex = stack.pop()
        if vertex not in visited:
            visited.add(vertex)
            stack.extend(graph[vertex] - visited)
    return visited

print(depth_first_search(graph, 'A'))

Below are the outputs I get evertime I run the program

{'H', 'C', 'B', 'A', 'D', 'S', 'F', 'E', 'G'}
{'D', 'E', 'C', 'H', 'G', 'S', 'A', 'B', 'F'}
{'G', 'F', 'C', 'H', 'E', 'D', 'B', 'S', 'A'} and so on....

The reason for using set especially makes sense for the below line of code as we want the stack to store only the vertices that are unexplored.

stack.extend(graph[vertex] - visited)

So performing set difference operation achieves that objective.But it comes at a cost as mentioned above.So I tweaked the code a bit to avoid using set and make do with lists

graph = {'A': ['B', 'S'], 'B': ['A'], 'C': ['S', 'F', 'D', 'E'],
 'D': ['C'], 'E': ['H', 'C'], 'F': ['C', 'G'], 'G': ['S', 'F', 'H'],
  'H': ['G', 'E'], 'S': ['A', 'G', 'C']}

def depth_first_search(graph, root):
    visited, stack = [], [root]
    while stack:
        vertex = stack.pop()
        if vertex not in visited:
            visited.append(vertex)
            neighbours = graph[vertex]
            for neighbour in neighbours:
                # ensures we only add unexplored nodes to the stack
                if neighbour not in visited:
                    stack.append(neighbour)
    return visited

print(depth_first_search(graph, 'A'))

But I get a wrong result

['A', 'S', 'C', 'E', 'H', 'G', 'F', 'D', 'B']

The correct result must be

['A', 'B', 'S', 'C', 'D', 'E', 'H', 'G', 'F']

What am I doing wrong?

Body:
The answer you're getting is a valid DFS order for that graph. There seems to be an unwritten restriction that a node's neighbors must be traversed in alphabetical order. With that in mind, two things:

First, you're adding the neighbors of a node to the stack in the order in which they're defined. But when you pop() off the stack, you take the last item off the stack first. That means you're selecting your nodes in reverse order. That's easy enough to fix by reversing the order in which you iterate over the neighbors:

for neighbour in reversed(neighbours):

Second, you haven't actually defined the nodes' neighbors in graph alphabetically. You either need to alphabetize the values of graph in the definition, or sort them before iterating:

for neighbour in reversed(sorted(neighbours)):
# or
for neighbour in sorted(neighbours, reverse=True):


问:
T-SQL Script current user database permissions
Occasionally I have to restore DB's from our production SQL server to the test SQL instance. When the database has been restored, we manually restore the correct access permissions (e.g. DB owner/ reader/ writer) for the restored database. This process works fairly well, except for having to manually screenshot the permissions before the restore and then reapply them from the image taken.

Is there an easy way to use T-SQL to store CURRENT permissions for a user BEFORE the database restore and then reapply those same permissions once the restoration is complete?

答:
Here is a modified version of the script @Fabian wrote so that I could also script out the permissions for stored procedures. Also adds QUOTENAME so the appropriate things are in brackets.</p>

```<code>SELECT
    dp.permission_name collate latin1_general_cs_as    AS Permission,
    t.TABLE_SCHEMA + '.' + o.name AS TableName,
    rt.ROUTINE_SCHEMA + '.' + o.name AS ProcedureName,
    dpr.name AS Username
    , 'GRANT ' + dp.permission_name collate latin1_general_cs_as
        + ' ON '
        + QUOTENAME(CASE WHEN t.TABLE_SCHEMA IS NOT NULL THEN t.TABLE_SCHEMA ELSE rt.ROUTINE_SCHEMA END )
        + '.'
        + QUOTENAME(o.name)
        + ' TO '
        +  QUOTENAME(dpr.name)
FROM sys.database_permissions AS dp
INNER JOIN sys.objects AS o ON dp.major_id=o.object_id
INNER JOIN sys.schemas AS s ON o.schema_id = s.schema_id
INNER JOIN sys.database_principals AS dpr ON dp.grantee_principal_id=dpr.principal_id
LEFT outer JOIN INFORMATION_SCHEMA.TABLES t ON  TABLE_NAME = o.name
LEFT OUTER JOIN INFORMATION_SCHEMA.ROUTINES rt ON rt.ROUTINE_NAME = o.name
WHERE dpr.name NOT IN ('public','guest')
ORDER BY Permission, TableName, ProcedureName, Username
</code>```

Body:
Here is a modified version of the script @Fabian wrote so that I could also script out the permissions for stored procedures. Also adds QUOTENAME so the appropriate things are in brackets.

SELECT
    dp.permission_name collate latin1_general_cs_as    AS Permission,
    t.TABLE_SCHEMA + '.' + o.name AS TableName,
    rt.ROUTINE_SCHEMA + '.' + o.name AS ProcedureName,
    dpr.name AS Username
    , 'GRANT ' + dp.permission_name collate latin1_general_cs_as
        + ' ON '
        + QUOTENAME(CASE WHEN t.TABLE_SCHEMA IS NOT NULL THEN t.TABLE_SCHEMA ELSE rt.ROUTINE_SCHEMA END )
        + '.'
        + QUOTENAME(o.name)
        + ' TO '
        +  QUOTENAME(dpr.name)
FROM sys.database_permissions AS dp
INNER JOIN sys.objects AS o ON dp.major_id=o.object_id
INNER JOIN sys.schemas AS s ON o.schema_id = s.schema_id
INNER JOIN sys.database_principals AS dpr ON dp.grantee_principal_id=dpr.principal_id
LEFT outer JOIN INFORMATION_SCHEMA.TABLES t ON  TABLE_NAME = o.name
LEFT OUTER JOIN INFORMATION_SCHEMA.ROUTINES rt ON rt.ROUTINE_NAME = o.name
WHERE dpr.name NOT IN ('public','guest')
ORDER BY Permission, TableName, ProcedureName, Username


问:
order by absolute values of one column while keeping equal values of another column together in R
Example data.frame:

```<code>df <- data.frame(study = c('A', 'A', 'B', 'C', 'D', 'D'),
                 similarity = c(0.61, 0.58, -0.605, 0.4, 0.607, 0.3))

df
   study similarity
1     A      0.610
2     A      0.580
3     B     -0.605
4     C      0.400
5     D      0.607
6     D      0.300
</code>```

I want to order by absolute similarity, but keeping equal values of study together. For the above example the result should be:

```<code>   study similarity
1     A      0.610
2     A      0.580
5     D      0.607
6     D      0.300
3     B     -0.605
4     C      0.400
</code>```

答:
data.table solution:

```<code>library(data.table)
setDT(df)[order(-abs(similarity)), .SD, by = study]
</code>```

Result:

```<code>   study similarity
1:     A      0.610
2:     A      0.580
3:     D      0.607
4:     D      0.300
5:     B     -0.605
6:     C      0.400
</code>```

Body:
Example data.frame:

df <- data.frame(study = c('A', 'A', 'B', 'C', 'D', 'D'),
                 similarity = c(0.61, 0.58, -0.605, 0.4, 0.607, 0.3))

df
   study similarity
1     A      0.610
2     A      0.580
3     B     -0.605
4     C      0.400
5     D      0.607
6     D      0.300

I want to order by absolute similarity, but keeping equal values of study together. For the above example the result should be:

   study similarity
1     A      0.610
2     A      0.580
5     D      0.607
6     D      0.300
3     B     -0.605
4     C      0.400

Body:
data.table solution:

library(data.table)
setDT(df)[order(-abs(similarity)), .SD, by = study]

Result:

   study similarity
1:     A      0.610
2:     A      0.580
3:     D      0.607
4:     D      0.300
5:     B     -0.605
6:     C      0.400


问:
Git dummy needs help on Windows
Admittedly, I'm new to Git.  I know... welcome to the century right!  But here's my issue...

I have a repo containing an Angular app.  The app happens to contain an MP4 video file that exceeds the 100mb GitHub limit.  (Before you recommend that the video shouldn't be local to the app... trust me, it needs to be in this particular case.)  My initial attempt to push the repo to GitHub failed due to the size of that one file.

So then I did something stupid...
I reverted the commit, thinking that it would simply revert back to the state that it was in prior to me attempting to push to GitHub.  But what it ACTUALLY did was restore my repo (entire app) to the original state that it was in when it was created by the Angular CLI... so it was just a new, blank app.

So I deleted the repo, restored my code from a backup, and started over.  This time, I added an exclusion for .mp4 to my .gitignore file.  I was confident that that exclusion would prevent git from trying to push the video up with the commit.  It did not.  So I'm basically in the same boat as yesterday.  I'm in the middle of an unsuccessful push.  The offending video file is in my History and I don't understand why it wasn't excluded, or what I should do to recover.

I am using GitHub Desktop for Windows.  And I have Git version 2.15.0 installed.

I understand that GitHub Desktop may not provide the solution that I need, as it seems to only provide a limited subset of git functionality.  I'm very comfortable working from a command line to do whatever needs to be done.  I'm just not sure what that is.  When attempting to search for solutions to similar scenarios, the responses didn't seem highly relevant, or didn't indicate that they would definitely resolve the issue.

Here is my .gitignore file

```<code># compiled output
/dist
/tmp
/out-tsc

# dependencies
/node_modules

# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace

# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

# misc
/.sass-cache
/connect.lock
/coverage
/libpeerconnection.log
npm-debug.log
testem.log
/typings

# e2e
/e2e/*.js
/e2e/*.map

# System Files
.DS_Store
Thumbs.db

# Video file exclusion
.mp4
</code>```

答:
Lets start by clearing up a misconception.

git revert did not delete anything from your repo.  If there was no detailed history lost by deleting the repo and starting over, that's fine; but the revert is not a reason to do that.  What revert does is to create one or more patches to "undo" the changes from one or more old commits, and either create a new commit to each patch (the default) or apply all of the patches to the work-in-progress.  Though the post-revert state may have appeared to present an empty work tree, the history was still there (as you would see with git log, for example).  This history is the primary reason for using source control, so it's important to understand the difference between an empty work tree and an empty repo.

Immediately after the revert command, you could've undone the revert with a reset, such as

```<code>git reset --hard HEAD^
</code>```

Even if you did other things in between, you'd be able to safely undo the revert as long as you hadn't successfully pushed anything.  (The exact guideline would be more nuanced, but this is a good starting point for understanding.)  The only trick would be finding a "name" of the original commit to which you want to return.

That said, if you had a backup of the code and the repo had no valuable history, starting with a fresh repo isn't a bad solution to the problem that a large file was already committed to the existing repo.  There are ways to clean up an existing repo and get rid of its large files, but those ways are a bit involved; so in this case a fresh start is reasonable.

But lastly you need to make sure you understand how git's "ignore rule" mechanism works.  Each ignore rule is a pattern that may match one or more paths, and any untracked file at such a path is ignored.  A file is "untracked" if there is no entry for that path in the index.  Once a file has been staged (with git add) it is in the index, where it typically remains.  (When you commit, the file is also added to the database; it's quite hard to remove a file from the database.)

So what you want to do is, as you recreate the repo, before doing any git add commands, create your .gitignore.  Also make sure it contains a path that matches your file.  As discussed elsewhere .mp4 will not match "all MP4 files"; that would be *.mp4

Title: Git dummy needs help on Windows

Body:
Admittedly, I'm new to Git.  I know... welcome to the century right!  But here's my issue...

I have a repo containing an Angular app.  The app happens to contain an MP4 video file that exceeds the 100mb GitHub limit.  (Before you recommend that the video shouldn't be local to the app... trust me, it needs to be in this particular case.)  My initial attempt to push the repo to GitHub failed due to the size of that one file.

So then I did something stupid...
I reverted the commit, thinking that it would simply revert back to the state that it was in prior to me attempting to push to GitHub.  But what it ACTUALLY did was restore my repo (entire app) to the original state that it was in when it was created by the Angular CLI... so it was just a new, blank app.

So I deleted the repo, restored my code from a backup, and started over.  This time, I added an exclusion for .mp4 to my .gitignore file.  I was confident that that exclusion would prevent git from trying to push the video up with the commit.  It did not.  So I'm basically in the same boat as yesterday.  I'm in the middle of an unsuccessful push.  The offending video file is in my History and I don't understand why it wasn't excluded, or what I should do to recover.

I am using GitHub Desktop for Windows.  And I have Git version 2.15.0 installed.

I understand that GitHub Desktop may not provide the solution that I need, as it seems to only provide a limited subset of git functionality.  I'm very comfortable working from a command line to do whatever needs to be done.  I'm just not sure what that is.  When attempting to search for solutions to similar scenarios, the responses didn't seem highly relevant, or didn't indicate that they would definitely resolve the issue.

Here is my .gitignore file

# compiled output
/dist
/tmp
/out-tsc

# dependencies
/node_modules

# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace

# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

# misc
/.sass-cache
/connect.lock
/coverage
/libpeerconnection.log
npm-debug.log
testem.log
/typings

# e2e
/e2e/*.js
/e2e/*.map

# System Files
.DS_Store
Thumbs.db

# Video file exclusion
.mp4

Body:
Lets start by clearing up a misconception.

git revert did not delete anything from your repo.  If there was no detailed history lost by deleting the repo and starting over, that's fine; but the revert is not a reason to do that.  What revert does is to create one or more patches to "undo" the changes from one or more old commits, and either create a new commit to each patch (the default) or apply all of the patches to the work-in-progress.  Though the post-revert state may have appeared to present an empty work tree, the history was still there (as you would see with git log, for example).  This history is the primary reason for using source control, so it's important to understand the difference between an empty work tree and an empty repo.

Immediately after the revert command, you could've undone the revert with a reset, such as

git reset --hard HEAD^

Even if you did other things in between, you'd be able to safely undo the revert as long as you hadn't successfully pushed anything.  (The exact guideline would be more nuanced, but this is a good starting point for understanding.)  The only trick would be finding a "name" of the original commit to which you want to return.

That said, if you had a backup of the code and the repo had no valuable history, starting with a fresh repo isn't a bad solution to the problem that a large file was already committed to the existing repo.  There are ways to clean up an existing repo and get rid of its large files, but those ways are a bit involved; so in this case a fresh start is reasonable.

But lastly you need to make sure you understand how git's "ignore rule" mechanism works.  Each ignore rule is a pattern that may match one or more paths, and any untracked file at such a path is ignored.  A file is "untracked" if there is no entry for that path in the index.  Once a file has been staged (with git add) it is in the index, where it typically remains.  (When you commit, the file is also added to the database; it's quite hard to remove a file from the database.)

So what you want to do is, as you recreate the repo, before doing any git add commands, create your .gitignore.  Also make sure it contains a path that matches your file.  As discussed elsewhere .mp4 will not match "all MP4 files"; that would be *.mp4


问:
Add logic in admin panel in Django
I have two models, the second model is FK from my first model like this:

```<code>class Model0(models.Model):
    work = models.FloatField(blank=True, null = True)

class Model1(models.Model):
    class Meta:
    unique_together = (('lat', 'lng'),)
    lng = models.FloatField(blank=True, null=True)
    lat = models.FloatField(blank=True, null=True)

class Model2(models.Model):
    idWork    = models.ForeignKey('Model0', on_delete=models.CASCADE)
    idSTFolio = models.OneToOneField('Model1', on_delete=models.CASCADE)
</code>```

I use admin panel to add my data, I need that when I add a second (Model0, Model1) with others values to Model1, in my Model2 this association was created automatically like this:

for example:

Model2 : (Model0, Model1)

Id 1    : (1.5, (2.5, 3.0)), 1.5 is 'Work' from Model0 and (2.5, 3.0) lat and lng in Model1

I need something like this...in this moment I create other Model1 with these values (2.5, 4.0), so like lat: 2.5 has already been added in Model2 I need that in my Model2 add other row like this automatically :

Id 1   : (1.5, (2.5, 3.0))
 Id 2   : (1.5, (2.5, 4.0))

Any idea if it is possible to modify the admin panel for what I need?
In this moment I must add this association in my Model2 page in admin panel, but It not is my idea.

答:
I just googled for multi-level inlines and found django-nested-inline package.

It seems promising to me for your purpose. I have found this in many stackoverflow answers as well:

https://stackoverflow.com/a/43719490/4772392

https://stackoverflow.com/a/18829259/4772392

https://stackoverflow.com/a/26575394/4772392

I have never tried this package but I think, this will work for you.
Please let me know if it works for you.

Title: Add logic in admin panel in Django

Body:
I have two models, the second model is FK from my first model like this:

class Model0(models.Model):
    work = models.FloatField(blank=True, null = True)

class Model1(models.Model):
    class Meta:
    unique_together = (('lat', 'lng'),)
    lng = models.FloatField(blank=True, null=True)
    lat = models.FloatField(blank=True, null=True)

class Model2(models.Model):
    idWork    = models.ForeignKey('Model0', on_delete=models.CASCADE)
    idSTFolio = models.OneToOneField('Model1', on_delete=models.CASCADE)

I use admin panel to add my data, I need that when I add a second (Model0, Model1) with others values to Model1, in my Model2 this association was created automatically like this:

for example:

Model2 : (Model0, Model1)

Id 1    : (1.5, (2.5, 3.0)), 1.5 is 'Work' from Model0 and (2.5, 3.0) lat and lng in Model1

I need something like this...in this moment I create other Model1 with these values (2.5, 4.0), so like lat: 2.5 has already been added in Model2 I need that in my Model2 add other row like this automatically :

Id 1   : (1.5, (2.5, 3.0))
 Id 2   : (1.5, (2.5, 4.0))

Any idea if it is possible to modify the admin panel for what I need?
In this moment I must add this association in my Model2 page in admin panel, but It not is my idea.


问:
Spark Structured streaming- Using different Windows for different GroupBy Keys
Currently i have following table after reading from a Kafka topic via spark structured streaming

```<code>key,timestamp,value
-----------------------------------
key1,2017-11-14 07:50:00+0000,10
key1,2017-11-14 07:50:10+0000,10
key1,2017-11-14 07:51:00+0000,10
key1,2017-11-14 07:51:10+0000,10
key1,2017-11-14 07:52:00+0000,10
key1,2017-11-14 07:52:10+0000,10

key2,2017-11-14 07:50:00+0000,10
key2,2017-11-14 07:51:00+0000,10
key2,2017-11-14 07:52:10+0000,10
key2,2017-11-14 07:53:00+0000,10
</code>```

I would like to use different windows for each of the keys and perform aggregation

for example

key1 would be aggregated on window of 1 minute to yield

```<code>key,window,sum
------------------------------------------
key1,[2017-11-14 07:50:00+0000,2017-11-14 07:51:00+0000],20
key1,[2017-11-14 07:51:00+0000,2017-11-14 07:52:00+0000],20
key1,[2017-11-14 07:52:00+0000,2017-11-14 07:53:00+0000],20
</code>```

key2 would be aggregated on window of 2 minutes to yield

```<code>key,window,sum
------------------------------------------
key2,[2017-11-14 07:50:00+0000,2017-11-14 07:52:00+0000],20
key2,[2017-11-14 07:52:00+0000,2017-11-14 07:54:00+0000],20
</code>```

Currently i do the following

```<code>var l1 = List(List(key1,"60 seconds"),List(key2,"120 seconds"))
l1.foreach{list =>

    val filtered_df = df.filter($"key" === list(0))

    val windowedPlantSum = filtered_df
        .withWatermark("timestamp", "120 minutes")
        .groupBy(
          window($"timestamp", list(1)),
          $"key"
        )
        .agg(sum("value").alias("sum")

    //start the stream
}
</code>```

The above approach starts 2 separate streams. In my case there are 200 such keys which starts 200 streams which fails due to memory issue.

Is there any way to specify window based on Keys in Spark structured streaming or is there any other approaches?

答:
I guess you have to use mapGroupsWithState to only manage one query

From slides 28 : https://www.slideshare.net/databricks/arbitrary-stateful-aggregations-using-structured-streaming-in-apache-spark

And also:

Arbitrary Stateful Processing in Apache Spark’s Structured Streaming

Deep dive stateful stream processing

Official documentation

Body:
Currently i have following table after reading from a Kafka topic via spark structured streaming

key,timestamp,value
-----------------------------------
key1,2017-11-14 07:50:00+0000,10
key1,2017-11-14 07:50:10+0000,10
key1,2017-11-14 07:51:00+0000,10
key1,2017-11-14 07:51:10+0000,10
key1,2017-11-14 07:52:00+0000,10
key1,2017-11-14 07:52:10+0000,10

key2,2017-11-14 07:50:00+0000,10
key2,2017-11-14 07:51:00+0000,10
key2,2017-11-14 07:52:10+0000,10
key2,2017-11-14 07:53:00+0000,10

I would like to use different windows for each of the keys and perform aggregation

for example

key1 would be aggregated on window of 1 minute to yield

key,window,sum
------------------------------------------
key1,[2017-11-14 07:50:00+0000,2017-11-14 07:51:00+0000],20
key1,[2017-11-14 07:51:00+0000,2017-11-14 07:52:00+0000],20
key1,[2017-11-14 07:52:00+0000,2017-11-14 07:53:00+0000],20

key2 would be aggregated on window of 2 minutes to yield

key,window,sum
------------------------------------------
key2,[2017-11-14 07:50:00+0000,2017-11-14 07:52:00+0000],20
key2,[2017-11-14 07:52:00+0000,2017-11-14 07:54:00+0000],20

Currently i do the following

var l1 = List(List(key1,"60 seconds"),List(key2,"120 seconds"))
l1.foreach{list =>

    val filtered_df = df.filter($"key" === list(0))

    val windowedPlantSum = filtered_df
        .withWatermark("timestamp", "120 minutes")
        .groupBy(
          window($"timestamp", list(1)),
          $"key"
        )
        .agg(sum("value").alias("sum")

    //start the stream
}

The above approach starts 2 separate streams. In my case there are 200 such keys which starts 200 streams which fails due to memory issue.

Is there any way to specify window based on Keys in Spark structured streaming or is there any other approaches?


问:
Update database after duration (Ruby on Rails)
I have a cycle model with two fields: duration (string) and completed (boolean).  When a user creates a cycle, they enter the duration (lets say 30 minutes) and the cycle is set to not complete (boolean 0). How do I update that database entry after the cycle duration (30 minutes) to mark the cycle as complete (boolean 1)?  Is there a way to handle this with ruby/rails code, or do I have to execute a javascript function?

The goal is to be able to find and display all completed cycles using Cycle.all(:conditions..) and call the SQL database.  I wrote a "complete?" method in the cycle model that compares the age of the cycle to the duration, but this is useless for SQL find methods.

What's the best way to tackle this?  Thanks!

答:
Define a rake task that runs something like…

```<code>desc "Expire old cycles"
task :cron => :environment do
  expired = Cycle.all :conditions => ["expiration < ?", DateTime.now]
  expired.each { |c| c.expire! }
end
</code>```

Where c#expire! is a method that'll mark it as expired in the database. Then setup rake cron to run every N minutes via a cronjob.

If you're comfortable doing this in SQL, you can optimize this by writing a query to do UPDATE cycles SET complete = 1 WHERE expiration < NOW();.

Body:
Define a rake task that runs something like…

desc "Expire old cycles"
task :cron => :environment do
  expired = Cycle.all :conditions => ["expiration < ?", DateTime.now]
  expired.each { |c| c.expire! }
end

Where c#expire! is a method that'll mark it as expired in the database. Then setup rake cron to run every N minutes via a cronjob.

If you're comfortable doing this in SQL, you can optimize this by writing a query to do UPDATE cycles SET complete = 1 WHERE expiration < NOW();.


问:
Changing Port on HUG
Is it possible to change the port number on HUG for python?
I have the following sample of what I'm trying to do.
The API defaults to port 8000 but I want to be able to set it manually. </p>

```<code>@hug.post()
def receive_json(request):
    request=str(request)
    print("Hello! Glad you're here!")
    print(request)
    process=os.system("echo "+'"'+request+'"')
</code>```

答:
If you are using the hug command line script to start your app, the -p / --port option will get you there, as in hug --port 5000 -f app.py:

```<code>$ hug --help
usage: hug [-h] [-v] [-f FILE] [-m MODULE] [-p PORT] [-n] [-ma] [-i INTERVAL]
           [-c COMMAND]

Hug API Development Server

optional arguments:
  -h, --help            show this help message and exit
  -v, --version         show program's version number and exit
  -f FILE, --file FILE  file
  -m MODULE, --module MODULE
                        module
  -p PORT, --port PORT  A Whole number
  -n, --no_404_documentation
                        Providing any value will set this to true
  -ma, --manual_reload  Providing any value will set this to true
  -i INTERVAL, --interval INTERVAL
                        A Whole number
  -c COMMAND, --command COMMAND
                        command
</code>```

Title: Changing Port on HUG

Body:
Is it possible to change the port number on HUG for python?
I have the following sample of what I'm trying to do.
The API defaults to port 8000 but I want to be able to set it manually.

@hug.post()
def receive_json(request):
    request=str(request)
    print("Hello! Glad you're here!")
    print(request)
    process=os.system("echo "+'"'+request+'"')

Body:
If you are using the hug command line script to start your app, the -p / --port option will get you there, as in hug --port 5000 -f app.py:

$ hug --help
usage: hug [-h] [-v] [-f FILE] [-m MODULE] [-p PORT] [-n] [-ma] [-i INTERVAL]
           [-c COMMAND]

Hug API Development Server

optional arguments:
  -h, --help            show this help message and exit
  -v, --version         show program's version number and exit
  -f FILE, --file FILE  file
  -m MODULE, --module MODULE
                        module
  -p PORT, --port PORT  A Whole number
  -n, --no_404_documentation
                        Providing any value will set this to true
  -ma, --manual_reload  Providing any value will set this to true
  -i INTERVAL, --interval INTERVAL
                        A Whole number
  -c COMMAND, --command COMMAND
                        command


问:
Error 404 when i try to add sharepoint lists.asmx web references
I have a Word document 2013 VisualStudio solution, and I tried to add a web reference "_vti_bin / search.asmx", but it shows me an error message "Error downloading" http: // [web path] / _ vti_bin / ListData.svc request error with the following HTTP 404 steps: Not Found".
I'm use Sharepoint 2010.
I tried that, becouse i need attach my word document to an item adding by my code.
Please apologize my english. And thanks in advance for any help.

答:
Looking over there, I found the answer to my problem. It is necessary to put the port of the central administration so that the web service in the VS2010 can be recognized.
Like http://[webpath]:10000/_ vti_bin /search.asmx


问:
In Spark 2.0, jdbc dataframes schema is automatically applied as nullable = false
For jdbc dataframes if I specify a custom query like

```<code>(select * from table1 where col4 > 10.0) AS table1
</code>```

then schema for all columns turns out to be nullable = false

```<code>col1: string (nullable = false)
col2: string (nullable = false)
col3: string (nullable = false)
col4: float (nullable = false)
</code>```

This causes null pointer exception when I use custom queries and the resultset contains any null value. I also tried to transform schema programatically but it still fails because of spark lineage, as original dataframe has the restricted schema irrespective of what schema transformed dataframe has.

I found a workaround for this. If I just provide the table name and then provide the select and where clause

sqlContext.read.jdbc(url, table1, dconnectionProperties).
              select("col1", "col2", "col3", "col4").
              where(s"col4 < 10.0")

the schema is correctly(or I how I want) inferred as

```<code>col1: string (nullable = true)
col2: string (nullable = true)
col3: string (nullable = true)
col4: float (nullable = true)
</code>```

But I wanted to use the custom queries as my queries has some join and aggregations which I want to be pushed down to database to execute.

This started showing up after we moved to spark 2.0.x prior to that this was working fine

答:
This issue is related to Teradata JDBC driver. This problem is discussed at https://community.teradata.com/t5/Connectivity/Teradata-JDBC-Driver-returns-the-wrong-schema-column-nullability/m-p/76667/highlight/true#M3798.

The root cause is discussed on the first page. And the solution is on the third page.

People from Teradata said they fixed the issue in the 16.10.* driver with a MAYBENULL parameter, but I am still seeing an undeterministic behaviour.

Here is a similar discussion
https://issues.apache.org/jira/browse/SPARK-17195

Body:
For jdbc dataframes if I specify a custom query like

(select * from table1 where col4 > 10.0) AS table1

then schema for all columns turns out to be nullable = false

col1: string (nullable = false)
col2: string (nullable = false)
col3: string (nullable = false)
col4: float (nullable = false)

This causes null pointer exception when I use custom queries and the resultset contains any null value. I also tried to transform schema programatically but it still fails because of spark lineage, as original dataframe has the restricted schema irrespective of what schema transformed dataframe has.

I found a workaround for this. If I just provide the table name and then provide the select and where clause

sqlContext.read.jdbc(url, table1, dconnectionProperties).
              select("col1", "col2", "col3", "col4").
              where(s"col4 < 10.0")

the schema is correctly(or I how I want) inferred as

col1: string (nullable = true)
col2: string (nullable = true)
col3: string (nullable = true)
col4: float (nullable = true)

But I wanted to use the custom queries as my queries has some join and aggregations which I want to be pushed down to database to execute.

This started showing up after we moved to spark 2.0.x prior to that this was working fine