text:
Q: MySQL Query Slow with ORDER BY So I'm having a problem when I add in ORDER BY date_last_access DESC the whole query slows down to 3secs and without it's about 0.2secs, why is it running so slow and how can I change the query to run faster?
There are also indexes on all the tables and fields used.
Users: 1+ million records
Likes: 5+ million records (over 1 billion in production)
Tables will be growing really fast once in production.
QUERY
SELECT
    id,
    sid,
    first_name,
    date_birth,
    location,
    date_created,
    date_last_access,
    (3956 * 2 * ASIN(
        SQRT(
            POWER(
                SIN(
                    ({LAT} - latitude) * pi() / 180 / 2
                ),
                2
            ) + COS({LAT} * pi() / 180) * COS(latitude * pi() / 180) * POWER(
                SIN(
                    ({LON} - longitude) * pi() / 180 / 2
                ),
                2
            )
        )
    )) AS distance
FROM
    users
WHERE
    `id` != {UID} AND
    `gender` = {GEND} AND
    `date_birth` BETWEEN {DOB_MIN} AND {DOB_MAX} AND
    `status` = 'active' AND
    (SELECT COUNT(*) FROM likes WHERE likes.judged_user = users.id AND likes.user_id = {UID}) = 0
HAVING distance <= {DIST}
ORDER BY date_last_access DESC
LIMIT {ROWS}

EXPLAIN
1   PRIMARY users   ref PRIMARY,Index_2,discovery,index_1   index_1 2   const   226184  Using index condition; Using where; Using filesort

2   DEPENDENT SUBQUERY  likes   eq_ref  PRIMARY,index_1,index_2 PRIMARY 16  const,hello.users.id    1   Using index

INDEXES
LIKES - user_id, judged_user - NORMAL - BTREE
USERS - id, gender, date_birth, status, date_last_access - NORMAL - BTREE
When I order by id instead of date_last_access it seems to run much faster, could it be cause date_last_access is a datetime format?

A: First try run a EXPLAIN of your query. This will show you what fields and operations are slowing your query. Then try to make joins with indexed columns and filter you resultset with more specific values.

A: Simplyfying the subquery could be a better way to avoid extra processing time (COUNT):
(SELECT COUNT(*) FROM likes WHERE likes.judged_user = users.id AND likes.user_id = {UID}) = 0

could change to
(SELECT 1 FROM likes WHERE likes.judged_user = users.id AND likes.user_id = {UID} limit 1) IS NULL

Avoiding a subquery could be the best way to improve the performance of the query. You could check what options could be better for your case (an index for likes.user_id is required in this case)
FROM
    users
LEFT JOIN (
SELECT distinct judged_user FROM likes WHERE likes.user_id = {UID}
) l ON l.judged_user=users.id
WHERE
    `id` != {UID} AND
    `gender` = {GEND} AND
    `date_birth` BETWEEN {DOB_MIN} AND {DOB_MAX} AND
    `status` = 'active' AND
    l.judged_user is NULL

A: You should phrase the FROM clause as:
WHERE `id` <> {UID} AND
      `gender` = {GEND} AND
      `date_birth` BETWEEN {DOB_MIN} AND {DOB_MAX} AND
      `status` = 'active' AND
       NOT EXISTS (SELECT 1 FROM likes l WHERE l.judged_user = users.id AND l.user_id = {UID})
HAVING distance <= {DIST}

For this query, you can try two indexes:

*

*LIKES(judged_user, user_id)

*

*USERS(Gender, status, date_birth, id)


text:
Q: Values not being defined when outputted in Python MySQL? Aided by a previously asked question see (How to correctly convert SQL rows to columns?), I'm attempting to apply to a MySQL connector script written in python. As seen by @Popeye 's fiddle http://sqlfiddle.com/#!9/997790/25, my attempt at returning all rows now with a null value for Person C but its output is seen below
import mysql.connector

db = mysql.connector.connect(
     host="localhost",
     port="3307",
     user="root",
     passwd="#",
     database="#"
  )
mycursor = db.cursor()
mycursor.execute("CREATE TABLE trial157 (Name TEXT(13) NULL , A1 TEXT(13) NULL ,  A2 TEXT(13) NULL ,  A3 TEXT(13) NULL ,  A4 TEXT(13) NULL)")

sql = "INSERT INTO trial157(Name, A1, A2, A3, A4) VALUES (%s, %s, %s, %s, %s)"
val = [
  ('Person A', 'C', 'T', 'R', 'S'),
  ('Person B', 'M', 'V', 'R', 'S'),
  ('Person C', 'M', None , 'R', 'C')
]
mycursor.executemany(sql, val)
db.commit()

mycursor.execute("SELECT * From (select name_new, max(case when name = 'PersonA' then A end) as PersonA, max(case when name = 'PersonB' then A end) as PersonB, max(case when name = 'PersonC' then A end) as PersonC from (select name, 'A1' name_new, A1 A from trial157 union all select name, 'A2' name_new, A2 A from trial157 union all select name, 'A3' name_new, A3 A from trial157 union all select name, 'A4' name_new, A4 A from trial157 ) t group by name_new) t where PersonC is null")
myresult = mycursor.fetchall()
for x in myresult:
  print(x)

 - OUTPUT
('A1', None, None, None)
('A2', None, None, None)
('A3', None, None, None)
('A4', None, None, None)
[Finished in 1.2s]

A: You have used wrong names.(in case expression names are used without space. You should use it exactly same as per val)
Replace
max(case when name = 'PersonA' then A end) as PersonA

With
max(case when name = 'Person A' then A end) as PersonA

See the space between Person and A in name. Do the same for Person B and Person C

redpajama_set_name: RedPajamaStackExchange


text:
Q: Return the First and Last Elements in a List Python, why does this method not work? I'm supposed to create a function that takes a list of elements and return the first and last elements as a new list.
def first_last(lst)
    return lst[0, -1]
TypeError: list indices must be integers, not tuple

Why does this not work? I looked at the answer. It's
def first_last(lst):
    return [lst[0], lst[-1]]

I don't get it, can someone explain?

A: In Python, there exists no such list indexing syntax. Just because it makes sense to you does not mean it's valid Python code. Since no such syntax exists for standard Python lists, to Python, it looks like you are trying to use the tuple literal 0, -1 (which is equivalent to (0, -1)) as a single index to your list. Python lists do not support indexing via tuples, therefore the error.

A: You can do two things with that bracket notation. You can retrieve one element:
return lst[0]  # the 0th element of the list

or you can retrieve a continuous slice of elements:
return lst[1:9:2]  # a sublist containing every 2nd element from index 1 until 9
                   #   so, incides 1, 3, 5, and 7.

The "solution" here is extracting the 0th element and the last element of the original list, and putting them in a new list. Technically, you could use a list slice to do this, by giving it a "step size" equal to the length of the list minus one:
return lst[::len(lst) - 1]

but that's less clear to look at than the solution you've been given.

Importantly, there are some classes in third-party libraries (e.g. numpy.array) that do let you use array[2, 3] syntax. This is not a base language feature, and it's accomplished by overriding the method that gets called when you use bracket notation to access something on the object, to make it not return an error when you put in the tuple (2, 3). In the case of np.array, it's to make it more familiar to mathematicians - array[2, 3] functions similarly to array[2][3].


text:
Q: Java Thread suspend does not work This is a sample code using java thread. The problem is that I cannot suspend the thread.
public class RunnableDemo implements Runnable {
   public Thread t;
   private String threadName;
   boolean suspended = false;
   RunnableDemo( String name){
       threadName = name;
       System.out.println("Creating " +  threadName );
   }
   public void run() {
      System.out.println("Running " +  threadName );
      try {
         while (true) {
            System.out.println("Thread: " + threadName + ", " + i);
            // Let the thread sleep for a while.
            Thread.sleep(300);
            synchronized(this) {
               while(suspended) {
                  wait();
               }
            }
        }
     } catch (InterruptedException e) {
         System.out.println("Thread " +  threadName + " interrupted.");
     }
     System.out.println("Thread " +  threadName + " exiting.");
   }
   public void start ()
   {
      System.out.println("Starting " +  threadName );
      if (t == null)
      {
         t = new Thread (this, threadName);
         t.start ();
      }
   }
   public void suspend() {
      suspended = true;
   }

The suspend function below does not work:
RunnableDemo R1 = new RunnableDemo( "Thread-1");
R1.start();
R1.suspend();  // This line does not work and thread continues ...

In fact the thread does not see the new value for suspended when I call suspend() . Any idea ?

A: Declare suspended as volatile:
volatile boolean suspended = false;

A: It is not absolutely clear to me what you are trying to achieve, but here are a few observations and tips that may help:

*

*By the time the main thread has completed the suspend() method, the second thread, "Thread-1", has probably not even entered the loop. You will see this if you add more debug output in suspend() and before and after wait(). The opposite could also be true, and you would reach the call to wait() before suspend is set to true. Then, once wait() is called, you won't see any more updates in "Thread-1" since it is waiting.

*The wait() method will never return. To achieve this, synchronize on the same object that you synchronize on when calling wait(), i.e. R1 in the main method and call notifyAll() on this object. You can also do that inside your suspend() method.

*This approach makes methods like Thread.sleep() pretty much unnecessary. Calling wait() is no active waiting and won't consume significant resources.

In short, change your suspend-method to
void suspend() {
   suspended = true;
   synchronized (this) {
       this.notifyAll();
   }
}

This will change how your program behaves and make it more clear what happens. But either way, it does not seem like "suspend" is a good name for what the method does, before or after, but if you experiment with this you may get a better understanding of how you can achieve what you want to achieve.

A: Using wait() might not work as you expect. The wait/notify is built as a simple implementation of the sempaphore (as an object instance wide monitor) context of concurrent programming.
Calling notify() from any other thread not locking the monitor of the thread object instance won't work, see Java Wait and Notify: IllegalMonitorStateException
You can easily achieve your goal by checking the suspended flag regularly, and just wait a bit if your thread shall not do anything:
public class SuspendDemo implements Runnable {
    public Thread t;
    private final String threadName;
    boolean suspended = false;
    SuspendDemo(final String name){
        threadName = name;
        System.out.println("Creating " +  threadName );
    }

    @Override
    public void run() {
        System.out.println("Running " +  threadName );
        try {
            while (true) {
                System.out.println("Thread: " + threadName);
                // Let the thread sleep for a while.
                Thread.sleep(300);
                while(suspended) {
                    System.out.println("suspended");
                    Thread.sleep(50);
                }
            }
        } catch (final InterruptedException e) {
            System.out.println("Thread " +  threadName + " interrupted.");
        }
        System.out.println("Thread " +  threadName + " exiting.");
    }

    public void start () {
        System.out.println("Starting " +  threadName );
        if (t == null) {
            t = new Thread (this, threadName);
            t.start ();
        }
    }

    void suspend() {
        suspended = true;
    }

    void resume() {
        suspended = false;
        notify();
    }

    public static void main(final String[] args) throws InterruptedException  {
        final SuspendDemo R1 = new SuspendDemo( "Thread-1");
        R1.start();
        R1.suspend();
        Thread.sleep(500);
        R1.resume();
    }
}

Resulting:
> Creating Thread-1
> Starting Thread-1
> Running Thread-1
> Thread: Thread-1
> suspended
> suspended
> suspended
> suspended
> Thread: Thread-1
> Thread: Thread-1
> Thread: Thread-1

The suspend flag is a native boolean, so writing it is an atomic operation, therefore you can go without synchronization on anything.
I suggest to fully understand concurrent programming and the monitor concurrency model what Java uses, before using synchronized. I found using synchronize(this) blocks hard to maintain and prone to mistakes.
The next example solves the problem with wait() and notifyAll(). Note the use of synchronized methods for locking the monitor of the thread object instead of the synchronize(this) blocks.
public class SuspendDemo implements Runnable {
    public Thread t;
    private final String threadName;
    boolean suspended = false;
    SuspendDemo(final String name){
        threadName = name;
        System.out.println("Creating " +  threadName );
    }

    @Override
    public void run() {
        System.out.println("Running " +  threadName );
        try {
            while (true) {
                System.out.println("Thread: " + threadName);
                // Let the thread sleep for a while.
                Thread.sleep(300);
                work();
            }
        } catch (final InterruptedException e) {
            System.out.println("Thread " +  threadName + " interrupted.");
        }
        System.out.println("Thread " +  threadName + " exiting.");
    }

    synchronized protected void work() throws InterruptedException {
        while(suspended) {
            System.out.println("suspended");
            wait();
            System.out.println("resumed");
        }
    }

    public void start () {
        System.out.println("Starting " +  threadName );
        if (t == null) {
            t = new Thread (this, threadName);
            t.start ();
        }
    }

    synchronized void suspend() {
        suspended = true;
        notifyAll();
    }

    synchronized void resume() {
        suspended = false;
        notifyAll();
    }

    public static void main(final String[] args) throws InterruptedException  {
        final SuspendDemo R1 = new SuspendDemo( "Thread-1");
        R1.start();
        R1.suspend();
        Thread.sleep(500);
        R1.resume();
    }
}


text:
Q: How to detect the player's position in a grid? I'm working on a tactical RPG and I have an empty game object called Map which is composed by 36 tiles.
The Map object has this script attached to it:
using System.Collections.Generic;
using UnityEngine;

public class GridMap : MonoBehaviour
{
    private GameObject[] MyVector = new GameObject[36];
    public GameObject[,] GridMatrix = new GameObject[6,6];

    public GameObject Map;

    // Start is called before the first frame update
    void Awake()
    {
        for (int i = 0; i < 36; i++)
        {
            try
            {
                MyVector[i] = Map.transform.GetChild(i).gameObject;
            }
            catch
            {
                print("Something is wrong!");
            }
        }
    }

    void Start()
    {
        int Counter = 0;

        for (int i = 0; i < 6; i++)
        {
            for (int j = 0; j < 6; j++)
            {
                GridMatrix[i, j] = MyVector[Counter];
                Counter;
            }
        }
    }

    // Update is called once per frame
    void Update()
    {

    }
}

Each tile has this script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Tile : MonoBehaviour
{
    public bool Current = false;
    public bool Selectable = false;

    public bool MouseOver = false;
    public bool Clicked = false;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }

    void OnMouseOver()
    {
        MouseOver = true;
        if (Clicked == false)
        {
            GetComponent<Renderer>().material.color = Color.red;

        }

        if (Input.GetMouseButton(0))
        {
            Clicked = true;
            GetComponent<Renderer>().material.color = Color.green;

        }
    }

    void OnMouseExit()
    {
        MouseOver = false;
        Clicked = false;

        GetComponent<Renderer>().material.color = Color.white;
    }

    private void OnTriggerStay(Collider other)
    {
        if (other.tag == "Player")
        {
            GetComponent<Renderer>().material.color = Color.magenta;
            Current = true;

        }

    }

    private void OnTriggerExit(Collider other)
    {
        GetComponent<Renderer>().material.color = Color.white;
        Current = false;

    }

}

I'd like to do detect in which tile the player is, for instace: if the player is in the tile whose position in the matrix is 0,0, I'd like to show in the console something like this: "The player is in the position 0,0'.
I've created this script and attached it to the player in order to get its position in the map(grid), however is not working:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GetPlayerPosition : MonoBehaviour
{
    private GameObject mapgrid;
    GridMap map;

    float tileX, tileY;
    // Start is called before the first frame update
    void Start()
    {

        mapgrid = GameObject.FindGameObjectWithTag("map");
        map = mapgrid.GetComponent<GridMap>();

        for (int i = 0; i < 6; i++)
        {
            for (int j = 0; j < 6; j++)
            {
                Tile tile = map.GridMatrix[i, j].GetComponent<Tile>();

                if (tile.Current)
                {
                    tileX = i;
                    tileY = j;

                    Debug.Log(tileX);
                    Debug.Log(tileY);
                }
            }
        }
    }

    // Update is called once per frame
    void Update()
    {

    }
}

What could I do guys to solve this?
Print of the project. The black cylinder is the player

A: *

*First I would make the type
private Tile[] MayVector = new Tile[36];
public Tile[,] GridMatrix = new Tile[6,6];

so you can avoid all the expensive GetComponent<Tile> calls and make them only once.

*I would also rather use OnTriggerEnter which is called only once instead of OnTriggerStay which is called every frame. And OnMouseEnter for the first part of your OnMouseOver since it is enough to set things only once.

*Instead of using GameObject.FindGameObjectWithTag and GetComponent I would also use FindObjectOfType or even better (if possible) reference everything already via the Inspector!

*Also you should fill the GridMatrix array already in Awake. Otherwise the Start of GetPlayerPosition might be executed before the Start method of GridMap so trying to access the array at a moment when it wasn't filled yet and only contains null elements → NullReferenceException!
My general thumb rule for Awake and Start:

*

*Awake: Do everything that doesn't depend on others, set your own references and values, use GetComponent etc to find scene references

*Start: Do things that depend on your references already being set. Like access fields of others etc.

*Then I would make the whole thing more event driven and not make the player all the time loop through the tiles but rather let the tiles set the players position:

So something like
public class Tile : MonoBehaviour
{
    // maybe not needed anymore
    public bool Current = false;

    public bool Selectable = false;

    public bool MouseOver = false;
    public bool Clicked = false;

    // Will be assigned by the GridMap script
    // so every tile will know it's own position
    // you could also assign this via the Inspector
    // but maybe a lot overhead for 36 tiles ;)
    public Vector2Int TilePosition;

    // The player will register itself here
    // you could even already reference this via the Inspector
    // select all tiles and simply drag the player object here
    public GetPlayerPosition getPlayerPosition;

    // would even be better to already reference this via the Inspector
    // but maybe a bit overhead for 36 tiles
    // except this is a prefab (what it probably should be ;) )
    [SerializeField] private Renderer renderer;

    private void Awake ()
    {
        // do this only once
        if(!renderer) renderer = GetComponent<Renderer>();
    }

    private void OnMouseEnter()
    {
        // do this only once .. no need to do it every frame
        MouseOver = true;
        renderer.material.color = Color.red;
    }

    private void OnMouseOver()
    {
        // skip further API calls if already Clicked
        if(Clicked) return;

        if (Input.GetMouseButton(0))
        {
            Clicked = true;
            renderer.material.color = Color.green;
       }
    }

    private void OnMouseExit()
    {
        MouseOver = false;
        Clicked = false;

        renderer.material.color = Color.white;
    }

    private void OnTriggerEnter(Collider other)
    {
        // do all these only once .. no need to do it every frame

        if(other.tag != "Player") return;

        renderer.material.color = Color.magenta;
        // Tell the player it's current position
        playerPosition.GridPosition = TilePosition;
        // probably not needed anymore
        Current = true;
    }

    private void OnTriggerExit(Collider other)
    {
        if(other.tag != "Player") return;

        renderer.material.color = Color.white;
        // probably not needed anymore
        Current = false;
    }
}

Then in the GridMap tell the Tile their position
public class GridMap : MonoBehaviour
{
    // just for clean coding reasons
    public const int GRID_WIDTH = 6;
    public const int GRID_HEIGHT = 6;

    public Transform Map;

    public Tile[,] GridMatrix = new Tile[GRID_WIDTH, GRID_HEIGHT];
    private Tile[] MyVector = new Tile[GRID_WIDTH * GRID_HEIGHT];

    // I would move it all to Awake
    //   1. it is possible so why wait until Start
    //   2. Makes also sure that the Start method of GetPlayerPosition
    //      will definitely be executed after all this
    //      => makes sure the array will actually be filled already!
    private void Awake()
    {
        for (int i = 0; i < GRID_WIDTH * GRID_HEIGHT; i++)
        {
            try
            {
                MyVector[i] = Map.GetChild(i).GetComponent<Tile>();
            }
            catch
            {
                print("Something is wrong!");
                return;
            }
        }

        for (var x = 0; x < GRID_WIDTH; x++)
        {
            for (var y = 0; y < GRID_HEIGHT; y++)
            {
                // This depends of course on how you arranged the tiles in the hierarchy
                GridMatrix[x, y] = MyVector[x * GRID_WIDTH + y];
                GridMatrix[x, y].TilePosition = new Vector2Int (x, y);
            }
        }
    }
}

And now all that's left is the player having to register to the Tiles:
public class GetPlayerPosition : MonoBehaviour
{
    // would be better if you already reference this via the Inspector
    [SerilaizeField] private GridMap map;

    // Will be assigned when entering a Tile
    // since you want integer values I would also store them as such
    // and Vector2Int is already made for storing a value pair x and y ;)
    public Vector2Int GridPosition;

    private void Awake()
    {
        if(!map) map = FindObjectOfType<GridMap>();
    }

    private void Start()
    {
        // if you don't have to know the indices within the loop
        // you can iterate a multidimensional array simply via foreach
        // because internally it is actually saved and treated as flat array
        foreach (var tile in map.GridMatrix)
        {
            tile.playerPosition = this;
        }
    }
}


text:
Q: How to schedule multiple tasks with ExecutorService I have three independend tasks to be executed at every 1 minute.
Here I have developed two options.
Option1
ScheduledExecutorService service1 = Executors.newScheduledThreadPool(1);
ScheduledExecutorService service2 = Executors.newScheduledThreadPool(1);
ScheduledExecutorService service2 = Executors.newScheduledThreadPool(1);
service1.scheduleAtFixedRate(new task1(), 0, 60, TimeUnit.SECONDS);
service2.scheduleAtFixedRate(new task2(), 0, 60, TimeUnit.SECONDS);
service3.scheduleAtFixedRate(new task3(), 0, 60, TimeUnit.SECONDS);

Option2
ScheduledExecutorService service = Executors.newScheduledThreadPool(3);
service.scheduleAtFixedRate(new task1(), 0, 60, TimeUnit.SECONDS);
service.scheduleAtFixedRate(new task2(), 0, 60, TimeUnit.SECONDS);
service.scheduleAtFixedRate(new task3(), 0, 60, TimeUnit.SECONDS);

My question is which option is preferred?  Does Option1 consume more system resources?

A: I don't know the implementation details to give an exact answer as to which usage would consume more system resources.  But the second option is definitely the way which is supported by Javadoc, tutorials, etc.  There are some reasons which I can point out.  First, the ScheduledExecutorService interface exposes the ability to schedule multiple tasks with a single service.
There is also a reason to not use the first option.  You were trying to schedule three tasks using three separate executors.  But there is no guarantee that all three tasks would begin at the same time, but the executors are different and could be spun up at different times.  On the other hand, if you used a single executor service the three tasks should be started concurrently.


text:
Q: Converting to base64 changes numpy array I have a service which takes input a base64 image, converts it to numpy array, performs some operation on it and then converts it back to base64 image, which is sent as output. But the numpy array gets modified during the conversion to base64.
I have created an example to demonstrate the problem.
import base64
from io import BytesIO
import numpy as np
from PIL import Image

img = np.array([[[1, 2, 3], [1, 2, 3]], [[1, 2, 3], [1, 2, 3]]]
               ).astype(np.uint8)
print(img)
image_format = 'JPEG'

pil_img = Image.fromarray(img)
buff = BytesIO()
pil_img.save(buff, format=image_format)
encrypted_image = base64.b64encode(buff.getvalue()).decode('utf-8')

base64_decoded = base64.b64decode(encrypted_image)
img2 = Image.open(BytesIO(base64_decoded))
img2 = np.array(img2)

print(img2)

Output:
[[[1 2 3]
  [1 2 3]]

 [[1 2 3]
  [1 2 3]]]
[[[1 2 4]
  [1 2 4]]

 [[1 2 4]
  [1 2 4]]]

The last column of the img2 is different to that of img but I expected it to be the same.
Is the conversion to base64 wrong? If in what format should I take the input for my service?

A: Notice that you are not simply doing base64 encoding/decoding.
Instead of x == base64.decode(base64.encode(x)) you are doing
base64.decode(base64.encode(jpeg.deflate(jpeg.compress(x)))) == x
The part of base64 is bit accurate, but the part of JPEG compression will give only an approximation. In other words is not tru that jpeg.deflate(jpeg.compress(x)) always equals x.


text:
Q: How can I retain Vim undo history but disallow hiding modified buffers? Similar to this question, I want to keep my undo history when I change buffers. However, if I use set hidden, vim no longer prompts me when I switch from a buffer with changes.
How can I retain the unsaved buffer prompt but also retain my undo history?

A: You can use the new persistent undo feature in Vim 7.3.
set undodir=~/.vim/undodir
set undofile

For details, see the documentation at
:help persistent-undo


text:
Q: MATLAB: Verify if a SSH tunnel to AWS is open. I'm using MATLAB to connect to a database hosted in AWS (using the database toolbox). In order to do that, I supply the URL of the database as a local port and create a SSH tunnel to the AWS host.
The issue is that this tunnel needs to be created in order for the code to run. If it is not, no error message is generated but MATLAB gets hung and needs to be killed. I would like to deploy this code to users who will not be able to troubleshoot if this tunnel is missing.
My question is: is there a way to check for a local port opening in MATLAB? How would I check if the tunnel is setup?

A: Since you are using the Database Toolbox, you might want to use the logintimeout function. As the documentation says:

Note   If you do not specify a value for logintimeout and the MATLAB session cannot establish a database connection, your MATLAB
  session may freeze.

And you would wrap your code inside a try/catch block

A: I am not familiar with Matlab's TCP objects, but there is a system command that executes a program, returning its exit code (see its documentation).  So what would probably do the job is a small program or script (as portable as needed), that tries to connect to the local port.
Alternatively, the small program/script could actually open (or re-open) the tunnel and return 0 on success. (This possibly adds the problem of how Matlab handles forking processes, I don't know how it handles that.)
There probably is some way to do the check if open and re-open if not-housekeeping via Matlab, but I have no clue.


text:
Q: Microsoft SQL Server 2008 R2 Service Pack 1 (KB2528583) won't install When I tried to update using Windows Update, it just "hangs" at the "Installing" step.
When I downloaded the patch and ran it myself, a console screen just pops up and disappears, and does nothing.
EDIT:
I just installed it, but after checking for updates, it's still on the list. When I tried reinstalling it, I get error 84C4000B. I deleted the files in the downloaded install folder, rebooted my PC and tried again. It's now stuck again.
I'm now trying another patch. This time its the Security Update for SQL Server 2008 R2 (KB2494088), and in my logs, it's been stuck in "Updates to install = 1", just like it did for SP1.
2011-12-03  07:03:59:010     536    14cc    Agent     *   Title = Security Update for SQL Server 2008 R2 (KB2494088)
2011-12-03  07:03:59:010     536    14cc    Agent     *   UpdateId = {2212D902-7C3B-408E-96A3-ECE4D00BFAE6}.100
2011-12-03  07:03:59:010     536    14cc    Agent     *     Bundles 1 updates:
2011-12-03  07:03:59:010     536    14cc    Agent     *       {13017DB1-F976-46E7-B465-8BA20E93DEE0}.100
2011-12-03  07:03:59:029     536    1114    AU  Getting featured update notifications.  fIncludeDismissed = true
2011-12-03  07:03:59:029     536    1114    AU  No featured updates available.
2011-12-03  07:04:09:543     536    14cc    DnldMgr Preparing update for install, updateId = {13017DB1-F976-46E7-B465-8BA20E93DEE0}.100.
2011-12-03  07:04:10:447    5100    398 Misc    ===========  Logging initialized (build: 7.5.7601.17514, tz: +1100)  ===========
2011-12-03  07:04:10:447    5100    398 Misc      = Process: C:\Windows\system32\wuauclt.exe
2011-12-03  07:04:10:448    5100    398 Misc      = Module: C:\Windows\system32\wuaueng.dll
2011-12-03  07:04:10:447    5100    398 Handler :::::::::::::
2011-12-03  07:04:10:448    5100    398 Handler :: START ::  Handler: Command Line Install
2011-12-03  07:04:10:448    5100    398 Handler :::::::::
2011-12-03  07:04:10:448    5100    398 Handler   : Updates to install = 1

A: Please see http://support.microsoft.com/kb/906602. This could be one of many things. That wizard will assist you with some possible solutions. In order to troubleshoot this type of issue we would need the exact error.

A: You can only install the patches on SQL Server 2008 R2. Be careful, if it's not an SQL Server 2008 R2, you can't update the patches.

A: I uninstalled SQL 2008 R2 and reinstalled with default settings (as customizing them resulted in other problems). I was able to update it to SP1 without any more issues.


text:
Q: Using set-url for a remote repository did not change when entering command git remote -v I've been trying to switch my git settings from ssh to https and found that I could do that through set-url; however, when I tried to switch my remote url from ssh to https, it did not work. Here is the situation:
>git remote set-url origin [url format]
>git remote -v
origin [ssh format] (fetch)
origin [ssh format] (pull)

Does this have something to do with other ssh settings in git?
I tried set-url, changing my username, but the remote repository remained in ssh form and did no change to https.

A: Try and confirm the change actually took place by editing directly your repo/.git/config file.
If the config file itself has not changed after your git remote set-url origin command, then, as commented, look for an insteadOf setting which would force Git to use SSH insteadOf HTTPS.


text:
Q: tensorflow cost is nan when training a autoencoder I am training a autoencoder with tensorflow ,but the cost is nan ,i modify the learning rate and the optimizer, but it not works.i search some result shows that decreasing the lr may help it ,but I change the lr to 0.00001 ,it also wont work .
here is my parameters  code :
learning_rate =0.00001
training_epochs = 2
batch_size = 900
display_step =1
examples_to_show = 10
nextbatch = 0
#network parameters
n_input = 500
# tf Graph input
X = tf.placeholder("float",[None,n_input])

# hidden layer setting
n_hidden_1 = 400 # 1st layer num features
n_hidden_2 = 300 # 2nd layer num features
n_hidden_3 = 200 # 3nd layer num features
n_hidden_4 = 100 # 4nd layer num features
weights = {
    'encoder_h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])),    # 500 * 400
    'encoder_h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])), # 400 * 300
    'encoder_h3': tf.Variable(tf.random_normal([n_hidden_2, n_hidden_3])), # 300 * 200
    'encoder_h4': tf.Variable(tf.random_normal([n_hidden_3, n_hidden_4])), # 200 * 100

    'decoder_h1': tf.Variable(tf.random_normal([n_hidden_4, n_hidden_3])), # 100 * 200
    'decoder_h2': tf.Variable(tf.random_normal([n_hidden_3, n_hidden_2])), # 200 * 300
    'decoder_h3': tf.Variable(tf.random_normal([n_hidden_2, n_hidden_1])), # 300 * 400
    'decoder_h4': tf.Variable(tf.random_normal([n_hidden_1, n_input])),    # 400 * 500
}
biases = {
'encoder_b1': tf.Variable(tf.random_normal([n_hidden_1])),  # 400
'encoder_b2': tf.Variable(tf.random_normal([n_hidden_2])),  # 300
'encoder_b3': tf.Variable(tf.random_normal([n_hidden_3])),  # 200
'encoder_b4': tf.Variable(tf.random_normal([n_hidden_4])),  # 100

'decoder_b1': tf.Variable(tf.random_normal([n_hidden_3])),  # 200
'decoder_b2': tf.Variable(tf.random_normal([n_hidden_2])),  # 300
'decoder_b3': tf.Variable(tf.random_normal([n_hidden_1])),  # 400
'decoder_b4': tf.Variable(tf.random_normal([n_input])),     # 500
}

# Building the encoder
def encoder(x):
    print ("i am encoder")
    layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x,
weights['encoder_h1']),biases['encoder_b1']))
    layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1,
weights['encoder_h2']),biases['encoder_b2']))
    layer_3 = tf.nn.sigmoid(tf.add(tf.matmul(layer_2,
weights['encoder_h3']),biases['encoder_b3']))
    layer_4 = tf.nn.sigmoid(tf.add(tf.matmul(layer_3,
weights['encoder_h4']),biases['encoder_b4']))
    return layer_4

# Building the decoder
def decoder(x):
    layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x,
weights['decoder_h1']),biases['decoder_b1']))
    print("layer1:",layer_1)
    layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1,
weights['decoder_h2']),biases['decoder_b2']))
    print("layer2:",layer_2)
    layer_3 = tf.nn.sigmoid(tf.add(tf.matmul(layer_2,
weights['decoder_h3']),biases['decoder_b3']))
    print("layer3:",layer_3)
    layer_4 = tf.nn.sigmoid(tf.add(tf.matmul(layer_3,
weights['decoder_h4']),biases['decoder_b4']))
    print("layer4:",layer_4)
    return layer_4

def normalize(x):
    amin,amax = x.min(),x.max()
    x = (x-amin)/(amax - amin)
    return x
def main():
# Construct model
encoder_op = encoder(X)
print ("encoder_op:",encoder_op)
decoder_op = decoder(encoder_op)
print ("decoder_op:",decoder_op)
# Prediction
y_pred = decoder_op
# Targets (Labels) are the input data.
y_true = X

# Define loss and optimizer, minimize the squared error
#cost = tf.reduce_mean(tf.pow(y_true - y_pred, 2))
#cost =tf.reduce_sum(tf.pow((y_true - y_pred),2))
cost = tf.reduce_mean(tf.squared_difference(y_true, y_pred))
#optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)
#optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
optimizer = tf.train.AdagradOptimizer(learning_rate).minimize(cost)

# Launch the graph
with tf.Session() as sess:
    if int((tf.__version__).split('.')[1]) < 12 and int((tf.__version__).split('.')[0]) < 1:
        init = tf.initialize_all_variables()
    else:
        init = tf.global_variables_initializer()
    sess.run(init)
    lenx_train = len(loadvector("x","train"))
    total_batch = int(lenx_train/batch_size)

    # Training cycle
    for epoch in range(training_epochs):
        # Loop over all batches
        for i in range(total_batch):
            batch_xs, batch_ys = get_next_batch(batch_size)  # max(x) = 1, min(x) = 0
            # Run optimization op (backprop) and cost op (to get loss value)
            op,c = sess.run([optimizer, cost], feed_dict={X: batch_xs})
        # Display logs per epoch step
        if epoch % display_step == 0:
            print("Epoch:", '%04d' % (epoch+1),"cost=",c)
            print("op:", '%04d' % (epoch+1),"op=",op)
print("Optimization Finished!")
if __name__ == "__main__":
    x_train= loadvector("x","train")
    #x_train = scale(x_train)
    x_train = normalize(x_train)
    y_train = loadvector("y", "train")
    main()

here is the result when i running it
Epoch: 0001 cost= 0.373359
op: 0001 op= None
Epoch: 0002 cost= nan
op: 0002 op= None
Optimization Finished!

A: It is hard to tell for sure without the data, but a possible reason is your implementation of normalize(). If you happened to have a tensor of equal values, you will get a bunch of nans from normalize().
In general, you can take a look at the second answer in How does one debug NaN values in TensorFlow? that suggests to use tf.add_check_numerics_ops(). This should catch numeric issues in most cases pretty quickly. Also, you can take a look at tfdbg.


text:
Q: Should we use MS Team Foundation Server if we are not using VS? We are about to roll out a project management system and we came across TFS. It seems pretty powerful as it can integrate version control system and project management tools into one server. But we are not a visual studio shop. Our development is done with Java/Php/Obj-C. Is TFS still useable without VS?

A: The key difference between TFS and the rest is the highly integrated nature of Source Control with Worth Item tracking and the fact that you have a data warehouse on top of both.
In terms of raw source control features, TFS and Subversion compare well.  Obvioulsy, TFS has the benefit of talking to SQL Server 2005, meaning that any SQL tools and knowledge can be used to backup the database etc,  off the top of my head - Subversion lacks the shelving, the caching proxy and and lightwieght labelling features but does have a better offline story than TFS V1.
TFS, Subversion and things like Vault are great, modern, Source Control systems.  You'll notice I left VSS out of that list :-).  While the Source Control features of TFS are very good - it does much, much more stuff than version control...
You can also take a look at some other studies have been carried out.  But then again, just because other people have decided to adopt the product doesn't mean that you have to.  There is plenty of competition in the developer tools market for you to get the tools you need at a price that you can afford.
In terms of convincing your management, then an explaination of the integration between work item tracking and source control along with a cost comparison of buying the equivalent (not integrated) products for your organisation should do the trick.

A: I wouldn't wish TFS on my worst enemy.  I've only ever used it for source control and have no experience with its other facilities, but it has caused me immense grief.  TFS source control facilities are cumbersome, complicated, incomplete, error prone, failure prone and generally difficult to use.  I only use it because I'm forced to at work.  Even if project management tools were totally awesome (which I gather they're not) I wouldn't want to use them because of the pain TFS source control regularly causes me.
There are plenty of more or less reasonable analyses of this tool on the web (in addition to all the hate filled diatribes).  Here are a few from nearby:
Should we migrate from svn to Team Foundation Server 2010?
Experience with SVN vs. Team Foundation Server?
TFS vs SVN

A: With the languages you mention, you might look into using Jazz.  http://www.jazz.net


text:
Q: How to display date in RDLC in another language I want to display date in RDLC(Report Definition Language Client-side) as १०/०७/२०१० instead 10/07/2010. How can i do this?

A: I believe it is not possible - at least not without effort on your side. Native digits are defined in .NET FW like:
var culture = new CultureInfo("km-KH");
for (var i = 0; i < culture.NumberFormat.NativeDigits.Length; i++) {
     Console.Out.WriteLine(culture.NumberFormat.NativeDigits[i]);
}

BUT native digits formatting is not supported (yet).
Reference:
http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.digitsubstitution.aspx

The DigitSubstitution property is reserved for future use. Currently,
  it is not used in either parsing or formatting operations for the
  current NumberFormatInfo object.

You still can manage to do it on your own - i.e. add Report->Report properties->Code
Function Khmer(value)
  value = Replace(value,"0","០")
  value = Replace(value,"1","១")
  value = Replace(value,"2","២")
  value = Replace(value,"3","៣")
  value = Replace(value,"4","៤")
  value = Replace(value,"5","៥")
  value = Replace(value,"6","៦")
  value = Replace(value,"7","៧")
  value = Replace(value,"8","៨")
  value = Replace(value,"9","៩")
  Khmer =  value
End Function

Note - report code uses vb syntax (always) - dunno why.
And in your report add expression for the field you want to encode to Khmer native digits:
=Code.Khmer(Fields!DateCreated.Value)


text:
Q: Is it possible to call a different class when a Button is clicked So I have a window with a JButton called "start". What I would like to happen is when that button is clicked it will run a separate class I have already created.  Is this possible and if so how would I go about doing it?
Thanks for all your help
Here's what I have so far
JButton start = new JButton ("Play");
frame.add(start);
start.addActionListener(//not sure what goes here, i would like to call other class here)

By the way, the other class being called is falling in case that matters

A: You(your class) can implement ActionListener to handle when the button is clicked.
public class YourClass implements ActionListener {

  public YourClass() {
    JButton start = new JButton ("Play");
    frame.add(start);
    start.addActionListener(this);
  }

  public void actionPerformed(ActionEvent arg0) {
    // Call other class
  }

}

A: There are many ways to do this.  If you want to use another class that has already implemented ActionListener then you can absolutely create an object of that class and then insert is as such:
//Another class that you're already created is named MyActionListener
// and implements ActionListener for this example

MyActionListener mal = new MyActionListener();
JButton start = new JButton ("Play");
frame.add(start);
start.addActionListener(mal);

However, this might not make any sense because you need to reference variables that are members of the current class's instance. In that case you want the current class to implement ActionListener as such:
class MyClass implements ActionListener {

    public MyClass() {
        JButton start = new JButton ("Play");
        frame.add(start);
        start.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //do whatever you need to do here
    }
}

The this keyword means that you are referring to the current instance of this class.  Therefore it will use the actionPerformed method that you implemented within this class.  You can instantiate an object of your other class within the actionPerformed method and then make any calls on that object as usual or if it's a member of this class, just call the member's function directly.  Additionally, if your referring to static methods on that second class you can also calls those directly within the actionPerformed method.
There is one other option, which is to use an anonymous inner class.  This is usually used if you want to have multiple actionPerformed methods within a single class that aren't shared between different components' event handler's registrations (ie. just use it for this single start button).  Here's how to use that method:
JButton start = new JButton ("Play");
frame.add(start);
start.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        //do whatever you want here for whenever start is clicked
    }
 });

As with the other options, you can also instantiate a second class in the actionPerformed method here or call a static method from another class.  However, if you were planning on calling a method that is on a variable that is a member of the outer class (MyClass in this case), then you need to use this syntax this.MyOtherClass.method().  The reason being is that the this keyword in this case provides you with access to the outer class inside of the anonymous inner class.


text:
Q: Convert seconds since epoch to days since 0001-01-01 UTC in Python I have a time in Python that's represented as seconds since the unix epoch. However Matplotlib wants days since 0001-01-01 UTC (http://matplotlib.org/api/dates_api.html).
How can I convert seconds since the unix epoch to days since 0001-01-01 UTC?

A: A little more thorough reading of the documentation shows matplotlib.dates.epoch2num


text:
Q: jQuery: CSS Selectors How would I use jQuery CSS Selectors to find for instance:
css class name: "Text" which is a div inside of dom object "wrapper"?

A: $('#wrapper div.Text') or $('#wrapper .Text'), depending on how specific you want to be.

A: To add to nezroy's answer: $('#wrapper > div.Text') if you only want elements directly inside the wrapper and not all that might be nested inside.

A: either
$('#wraper .text')

or
$('.text', $('#wraper'))

will work well with same results (even tho, i guess the second option is faster)


text:
Q: How to Prevent/Manage Stripe Webhook Sending Invoice for $0 on Trial Signup? I'm working on an app that comes with a 14 day trial for free.
To handle payments, I'm using Stripe and listening for webhooks so I can perform functions on the backend when events happen.
One thing I've noticed, though, is that Stripe is sending me invoice data with an amount charged of $0 for the trial period. So, if a customer signs up, they get an invoice from Stripe for $0 (I have my webhook setup to fire off an email for each invoice I receive).
This isn't terrible, but from a UX perspective, I'd like to avoid the shock of getting an immediate invoice when someone is expecting a trial (even if that invoice is for $0).
I've considered just checking the data Stripe sends over and filtering out $0 invoices, but if I offer a discount or something, this doesn't seem like the best way.
Any thoughts/notes on how to implement this better?

A: A couple options here:

*

*When you create the customer/subscription, the API returns both the customer and the subscription data to you in its response. You can use data from either or both of these to filter intelligently. Of particular interest:

*

*current_period_start: This will also be the timestamp of the invoice.

*trial_end: Until this timestamp, any invoice including a subscription is for a trial.

*customer: If you don't like the others, you can always query the customer record when processing a $0 invoice. Customers in their trial period have a status of trialing.

*If you're sending the email on invoice.created events, only the initial subscription invoice is created as closed. All other subscription invoices are open when Stripe creates them. (This is so you can make adjustments before the invoice is processed.) An invoice that's both $0 and closed has a high probability of being a trial—100%, in fact, if you're not otherwise creating already-closed invoices.

A: Try to listen for charges events. The event charge.succeeded Only fired when user has ben charge successfully.


text:
Q: Adding share button on fancybox 3 Share buttons are not present by default, and I'm wondering how to add it. Is there any other method than this?
$('[data-fancybox="XXXXX"]').fancybox({
    buttons : [
        'share',
        'fullScreen',
        'close'
    ]
})

I'm on Tumblr, and using a Tumblr tag for data-fancybox (i.e. data-fancybox="{PostID}"), but using {PostID} for XXXXX in the code above doesn't work.
Thank you in advance for your help.

A: Basically, there are two methods:
1) Use $( selector ).fancybox( options ); to select your links and to apply your custom options.
Demo - https://codepen.io/fancyapps/pen/vzoPMB?editors=1010
Note: You can use any selector, it is not necessary to use '[data-fancybox="XXXXX"]', you could use, for example, '.imglist a', demo - https://codepen.io/anon/pen/YJaOVo?editors=1010
2) Simply change defaults, for example:
$.fancybox.defaults.buttons = [
  'slideShow',
  'share',
  'zoom',
  'fullScreen',
  'close'
];


text:
Q: PIL save using file object without filename save() has a param fp: A filename (string), pathlib.Path object or file object. If I use file object,what is the filename ? I am inconvenient to test this,please help me!
s = io.BytesIO()
pi = Image.frombytes(mode=i.mode, size=i.size, data=i.data)
pi.save(s, format="jpeg")

A: Does this help?
from io import BytesIO
from PIL import Image, ImageDraw

image = Image.new("RGB", (300, 50))
draw = ImageDraw.Draw(image)
draw.text((0, 0), 'BytesIO')

byte_io = BytesIO()

image.save(byte_io, 'PNG')
with open('test.png', 'wb') as outfile:
    outfile.write(byte_io.getvalue())

You still have to do something with the stream like writing to a file after declaring it.
If you want to save the bytes for something:
with io.BytesIO() as output:
    image.save(output, 'PNG')
    content = output.getvalue()


text:
Q: Upload a file in Reactjs I have made a input field which allows user to upload a image.
       class Form extends Component<{}, IState> {
          constructor(Props) {
            super(Props);
            this.state = {
              image: ""
            };

         handleImageChange (e) {
          let files = e.target.files;
          let reader = new FileReader();
          reader.readAsDataURL(files[0]);
          reader.onload = (e) => {
          console.log(e.target?.result);
          this.setState({image: e.target?.result})
                }
              }

        render() {
         return (
           <input type = "file" onChange = {(e)=> this.handleImageChange(e)} />
      <img id="target" src={this.state.image}
)}

I am able to see the uploaded image in my current Form Page. But I want to see the uploaded image in some other page. How will I do that? Do I need to use Props? If yes, how will I implement it. Any help would be appreciated.


text:
Q: SQL dynamically create stored procedures? I am trying to create a script to create/setup a group of stored procedures that will all be fairly similar.
So I am trying to loop through this code, changing the @DATABASE_NAME and @TableName when needed.
/* Start loop */
    DECLARE @create_stored_procedure nvarchar(max)
    SET @create_stored_procedure = N'
       USE [' + @DATABASE_NAME + ']
       CREATE PROCEDURE [dbo].[sproc_imp_' + @TableName + ']
       AS
       BEGIN
           PRINT(''doing something'')
       END'
    EXEC sp_executesql @statement = @create_stored_procedure
/* End loop */

But I am getting errors saying

'CREATE/ALTER PROCEDURE' must be the first statement in a query batch.

or

'CREATE/ALTER PROCEDURE' does not allow specifying the database name as a prefix to the object name.

All the solutions online suggest using GO, but that won't work in dynamic SQL.
Does anyone know a possible solution for SQL Server 2005?

A: I wouldn't call the solution intuitive, but apparently this works. I prefer the look of this one though.

A: Try with spiting USe DB and create procedure. Like this
DECLARE @create_store_procedure nvarchar(max)

SET @create_store_procedure = N'
   USE [' + @DATABASE_NAME + '] '
EXEC sp_executesql @statement = @create_store_procedure

SET @create_store_procedure = N'
   CREATE PROCEDURE [dbo].[sproc_imp_' + @TableName + ']
   AS
   BEGIN
       PRINT(''doing something'')
   END '

 EXEC sp_executesql @statement = @create_store_procedure

This is working perfectly for me

A: I tried Nithesh's answer and that didn't work for me it ended up creating the store procedure in the master table. Zec's answer worked. Creating a dynamic query inside my dynamic query.
DECLARE @create_store_procedure nvarchar(max)
DECLARE @use_db nvarchar(max)

SET @create_store_procedure = N'
   CREATE PROCEDURE [dbo].[sproc_imp_' + @TableName + ']
   AS
   BEGIN
       PRINT(''doing something'')
   END '

SET @use_db = N'
USE [' + @DATABASE_NAME + ']
DECLARE  @sub_create_store_procedure nvarchar(max)
SET @sub_create_store_procedure = ''' + REPLACE(@create_store_procedure, CHAR(39), CHAR(39) + CHAR(39)) + '''
EXEC sp_executesql @statement = @sub_create_store_procedure
'

 EXEC sp_executesql @statement = @use_db


text:
Q: Run command on brightness change as the brightness selected doesn't change my effective brightness, but xbacklight does, I just wanted to sync them.
So, I would like to run xbacklight on login, in order so adjust to the saved state, and also run xbacklight with the current selected brightness, whenever it changes.
Is there a way?
Thank you.


text:
Q: Monodromy along strata of a pushforward Work with complex varieties and constructible sheaves on the complex analytic site. All functors will be tacitly derived.
Let $X$ be a variety acted upon by a connected linear algebraic group. Let $X = \bigsqcup X_i$ be the orbit decomposition (assumed to be finite). Let $f_i\colon X_i \hookrightarrow X$ be the inclusion. If you wish, assume each $f_i$ is affine.
Let $\underline{\mathbb{C}}$ denote the constant (rank $1$) sheaf. Consider the cohomology sheaves $H^k(f_j^*f_{i*}\underline{\mathbb{C}})$. These are certainly semi simple local systems on $X_j$ (by equivariance). Without imposing any further conditions, is there any reason these will be the trivial local system (of some rank)? In particular, I don't want to impose any conditions on the isotropy groups of points.
Generalizing, one could replace $\underline{\mathbb{C}}$ by an arbitrary equivariant local system and ask for constraints on the monodromy of the analogous restriction. Apart from the fact that one gets equivariant local systems this way, are there any other constraints?


text:
Q: How to read Schema from the config table and attach to Pyspark DataFrame? I have 50 tables in my on premise server. I want to migrate those 50 tables from on-premise to delta table in data bricks. But every table has specific schema defined but i need to design the single adf pipeline to move those fifty tables from on-premises to delta table.
How to attach the schema to the data frame at the run time based on the table name ?

A: I would use mapping data flows for this scenario:

*

*Create a static table/file with the list of your tables.

*Add a for each loop in ADF pipeline.

*Within foreach, create a mapping data flow.

*As a source provide your on-prem database (schema will be detected automatically).

*Delta table as a destination (sink).

Mapping data flows are spark-based, therefore you can see in "projection" tab that it is translated to the spark type already.


text:
Q: SQL Case Statement I'm trying to write a SQL Query for the below. I have list of email address in table. I need to pull Subscriber key associated to that email address from another table if that email address is not present I want email address as subscriber key. How to write SQL for this? Any help will be highly appreciated.
Table1
    **| Email ID**
    | TOM@gmail
    | KIM@gmail
    | JIM@gmail
    | RIM@gmail
    | SIM@gmail

Table2
    Subscriber key  | Email ID**
    100222221       | TOMMOT@gmail
    103333332       | KIM@gmail
    104444443       | JIMMIJ@gmail

Output:
Subscriber key  | Email ID**
    100222221   | TOMMOT@gmail
    103333332   | KIM@gmail
    104444443   | JIMMIJ@gmail
    RIM@gmail   | RIM@gmail
    SIM@gmail   | SIM@gmail

A: Try this -
SELECT
CASE WHEN SubscriberKey IS NULL
THEN [Email ID]
ELSE SubscriberKey
END AS SubscriberKey,
[Email ID]
FROM
(SELECT
de2.SubscriberKey,
de1.[Email ID]
FROM
[TABLE1] de1
LEFT JOIN [TABLE2] de2
on de1.[Email ID] = de2.[Email ID]
) AS ANSWER


text:
Q: Cannot use Math template in TeXnicCenter First of all, I just want to thanks the people of TeXnicCenter... great application...
I am trying to use the math template in TeXnicCenter, When I compile it, it says missing \begin{document}. After googling, I realised that the enconding is the problem. The recommendation is to change the BOM. I cannot find this option in the editor, the question is how to make this work again.
The extange thing is that it fails only using the template. If I type my own code, there is not problem.

A: I'll guess you use Windows? I think the problem is that you use UTF-8 with BOM because you are under MS Windows.
Try the following. Open the file you want to compile in TeXnicCenter then go to File->Save As. In the following Save-Dialog at the bottom you will find a drop down menu with the encoding for the file. Select UTF-8 (without the BOM).
Then try to compile it. BTW, which encoding did you use when you write your own files?


text:
Q: Getopt::Long - How to get script arguments that are not options Taken from the manual page Getopt::Long:
The code is:
use Getopt::Long;
my $data   = "file.dat";
my $length = 24;
my $verbose;
GetOptions ("length=i" => \$length,    # numeric
            "file=s"   => \$data,      # string
            "verbose"  => \$verbose)   # flag
or die("Error in command line arguments\n");

And I execute the script thus:
./myscript.pl --verbose filename.txt

How do I get hold of the argument filename.txt?

A: Non-options are left in @ARGV.
This means you can simply use <> to read from them if they're file names.

A: Non option arguments are simply left in @ARGV.
our $argument = shift @ARGV;

There is a more concise way to use Getopt though.  By specifying a hash reference as the first argument, all of the options are assigned to that hash.  It's a great way to keep all of the options together in one place.  The option spec can be put into a qw list.
use strict;
use diagnostics;
use Getopt::Long;

## %options must be declared seperately because it is referenced in its own definition
our %options;
%options = (
  # set default debug level
  debug => 0,
  # set default file name
  file => "file.dat",
  # option with multi-effects, setting debug and verbose at once
  quiet => sub { @options{qw/debug verbose/} = (0, 0); },
  loud  => sub { @options{qw/debug verbose/} = (999, 1); },
);

GetOptions(\%options, qw/debug+ verbose! file=s length=o quiet loud/);

our $argument = shift @ARGV;
die "missing first argument\n" unless defined $argument;

print "Starting program $0 on $argument\n" if $options{verbose};
if ($options{debug} >= 2) {
  ## Load this module only if we need it, but you must guarantee it's there or trap the error with eval{}
  require Data::Dump;
  printf "Dumping options hash\n%s\n", Data::Dump::pp(\%options);
}

A: To quote the original question:
Taken from the manual page: Getopt::Long

Mixing command line option with other arguments
Usually programs take command line options as well as other arguments,
for example, file names. It is good practice to always specify the
options first, and the other arguments last. Getopt::Long will,
however, allow the options and arguments to be mixed and 'filter out'
all the options before passing the rest of the arguments to the
program. To stop Getopt::Long from processing further arguments,
insert a double dash -- on the command line:
--size 24 -- --all
In this example, --all will not be treated as an option, but passed to
the program unharmed, in @ARGV.


text:
Q: Best way to change name Sum Sq in anova table I am changing the Sq sum column in an anova table to present the percentage explained instead.
I have changed the numbers but cannot change the name to Percent. Can anyone help? Thanks

A: Sum Sq is the second column in the Anova table, which is a data.frame.  It can be changed simply with:
colnames(df)[2]<-'Percent'


text:
Q: typescript - How can i get object class instance? I have an object of class classA which was inherited from class classB
My question is: how do i know if my object was inherited from classB
I tried:
Object instanceof classB
But it's not working!
when i am ading instanceof to any ts file
im getting
0x800a138f - Microsoft JScript runtime error: Unable to get value of the property 'prototype': object is null or undefined
var __extends = this.__extends || function (d, b) {
    function __() { this.constructor = d; }
    __.prototype = b.prototype; //exception occurs in this line
    d.prototype = new __();
};

A: One cause of this error is if the class you are deriving from comes after the derived class in your file.
This (to mangle Ryan's answer):
class B extends A {
}

class A {
}

var x = new B();
console.log(x instanceof A); // Error

... causes precisely the error you describe in the Playground.
I think there is already a work item for this on codeplex, but it's late, and I haven't checked...

A: This generally does work:
class A {
}

class B extends A {
}

var x = new B();
console.log(x instanceof A); // true


text:
Q: List of people who meet specific conditions I need a list of customers who bought products in a given year for a total amount of more than 80% of the largest total amount in a given year. I have a list of all persons and their total amount in each year, but I don't know how can I filter it. I tried to use MAX function and use the highest SUM but it doesn't work or I do it wrong.
My current query is:
SELECT (p.LastName + ' ' + p.FirstName) AS 'Customer', s.CustomerID AS 'CustomerId',    YEAR(s.OrderDate) AS Rok, SUM(s.SubTotal) AS 'Amount', COUNT(*) AS 'number of purchases'

FROM Sales.SalesOrderHeader s JOIN Sales.Customer c ON s.CustomerID = c.CustomerID
JOIN Person.Person p ON c.PersonID = p.BusinessEntityID

GROUP BY s.CustomerID,  YEAR(s.OrderDate), p.LastName, p.FirstName

ORDER BY  s.CustomerID, YEAR(s.OrderDate);

A: If I understand your question correctly it should be okay:
with cte as (
SELECT
    (p.LastName + ' ' + p.FirstName) AS 'Customer', s.CustomerID AS 'CustomerId',    YEAR(s.OrderDate) AS Rok, SUM(s.SubTotal) AS 'Amount', COUNT(*) AS 'number of purchases'
FROM Sales.SalesOrderHeader s JOIN Sales.Customer c ON s.CustomerID = c.CustomerID
JOIN Person.Person p ON c.PersonID = p.BusinessEntityID
GROUP BY s.CustomerID,  YEAR(s.OrderDate), p.LastName, p.FirstName
), Max80Percent as
(
    select
         *
        ,0.8 * MAX(Amount) OVER (PARTITION BY Rok) as Threshold
    from cte
)
select
    *
from Max80Percent
where Amount > Threshold

A: You can use a sub query to filter
SELECT * FROM (SELECT (p.LastName + ' ' + p.FirstName) AS 'Customer',
s.CustomerID AS 'CustomerId',    YEAR(s.OrderDate) AS Rok,
SUM(s.SubTotal) AS 'Amount', COUNT(*) AS 'number of purchases'
FROM Sales.SalesOrderHeader s JOIN Sales.Customer c
ON s.CustomerID = c.CustomerID
JOIN Person.Person p ON c.PersonID = p.BusinessEntityID
GROUP BY s.CustomerID,  YEAR(s.OrderDate), p.LastName, p.FirstName
ORDER BY  s.CustomerID, YEAR(s.OrderDate)) z
WHERE z.Amount>80;

The logic you need to implement can written in where clause this is a sample filter that I have used


text:
Q: Using JWT along with refresh tokens in Spring OAuth2 Consider we want to use JWT along with refresh tokens in Spring.
We have to use JWTTokenStore, right?
But if JWTTokenStore isn't a real database and doesn't store anything, where and how should we store refresh tokens?
(We prefer to store refresh tokens in database rather than store them in memory)

A: Refresh tokens are JWTs by default if the access tokens are. To change that you would have to modify the token services (possibly a custom TokenEnhancer would do it, and maybe also a custom TokenStore). Why not go with the default?

A: I faced the same problem. The explanation for me is that the tokens was set in the cookies.(No sessions on client side). And I don't want that my refresh token be used like an access token. (Modifying the access token cookie with the value of the refresh token)


text:
Q: Kotlin Map: why is there no toHashMap()? In Kotlin, Map class has toLinkedMap() and toSortedMap() extension methods.
But why is there no toHashMap() method? In fact, the resulting Map implementation of many stdlib methods is LinkedHashMap, but casting it to HashMap in my code would make me rely on the implementation which is bad.
Introducing such a method would keep developers safe from digging into the implementation, and with the current implementation it would just perform the cast.
My use case is:
val matchesInClass: HashMap<MessageClass, HashMap<Int, Int>>

//...

for ((cl, matches) in matchesInClass) {
    matchesInClass[cl] = matches.filterKeys { it !in banned } //error: not a HashMap
}

When I use HashMap(matches.filterKeys { it !in banned }) it leads to overhead of creating a new map, which I'd be happy to avoid.
So, is it by design?

A: Why? is hard to answer, but searching for or adding a ticket to Kotlin issue tracker will get you that answer as well as update you when the status changes. It does seem that toHashMap is missing since toHashSet is there (as-of 1.0 BETA 4).
Note: that LinkedHashMap is the default because kotlin wants to maintain order of elements as you go from List to Set to List to Map to List and be sure that things are still in their relative order.
Regardless, In the meantime, here is a custom toHashMap for you:
public fun <K, V> Iterable<Pair<K, V>>.toHashMap(): Map<K, V>
    = HashMap<K, V>(collectionSizeOrNull() ?: 16).apply { putAll(this@toHashMap) }

You can make it better by using logic similar to the toMap function for picking the best initial map size, or don't set it and trust the default growth implementation.
Kotlin is built to easily be extended.  If everything is placed into the stdlib then people on constrainted devices such as Android will complain it is too big and really most things people ask for that are small oddities are a few lines of code to put into your own custom lib.

A: I agree that this place in stdlib is inconvenient. But how do you want such a function toHashMap() to be implemented?
The simplest implementation makes a cast as HashMap if the underlying map is a HashMap, and convert it otherwise. So the performance of that code will dramatically change in case of changing the implementation. That's bad.
So, IMHO, I prefer unsafe cast which fails if stdlib changes in a strange way and alerts me of that.
Anyway you are always welcome to report this issue as a ticket in YouTrack  http://youtrack.jetbrains.com/issues/KT. You gave a prety good description of the problem here, so it shouldn't take long.

A: It's not an answer to your original question, but an alternative solution to your use case.
Here you're trying to filter HashMap<Int, Int> and assign it back to the map it was taken from:

matchesInClass[cl] = matches.filterKeys { it !in banned } //error: not a HashMap

If you have no requirement to keep matches HashMap intact, you could filter it in-place by invoking removeAll function on its keys mutable set:
val matchesInClass: HashMap<MessageClass, HashMap<Int, Int>>

//...

for (matches in matchesInClass.values) {
    matches.keys.removeAll { it in banned } // notice the inverted condition
}

It would be more efficient than creating a filtered copy.
Another alternative would be to use Map.filterTo provided with an empty map of the required type:
for (entry in matchesInClass) {
    entry.setValue(entry.value.filterTo(HashMap()) { it !in banned })
}

A: How about just filter it like this:
val filtered = matchesInClass.mapValues { it.value.filterKeys { it !in banned } }


text:
Q: ASUS ROG GL553VE (aka FX53VE-MS74) External Monitor Connection(s) When I attach a 3840x1080 monitor capable of 144Hz through HDMI, all I am getting is 60Hz.
There is a USB 3.1 Gen 1 Type-C port, but I cannot confirm that DisplayPort functionality is implemented.
Is there a way to get 144Hz signal out of the GL553VE, given that the Nvidia GeForce 1050Ti supports it?


text:
Q: Text on image looks blurry after scaling image down using Aspose.Words.Saving.ImageSaveOptions We have a problem in our system. We extract images from doc file and perform scaling up and down if the size of the extracted image is not as we want. Image looks good after scaling but the text that lies inside the image looks a little bit blurry or not in the same quality (sharpness) as in the image before scaling. Any ideas that can help to improve the text sharpness will be most appreciated.Thank you wonderful people!
Our scaling function looks like this:
const float highScalingFactor = 0.58F; // default scaling of large images - based on experience

const float lowScalingFactor = 0.44F; // default scaling of small images - based on experience

const int maxWidth = 1100;
const int maxHeight = 2500;

float formulaScale = 0.9F;

//calling scaling function here
var lImgStream = ScaleImage(shape, lowScalingFactor * formulaScale, maxWidth, maxHeight, "png");

private static MemoryStream ScaleImage(Shape shape, float scale, int maxWidth, int maxHeight, string extension)
{
    var resolution = 300F;

    ShapeRenderer r = new ShapeRenderer(shape);
    var rect = r.GetBoundsInPixels(scale, resolution);

    if (rect.Width > maxWidth)
    {
        scale = 1 - (rect.Width * scale - maxWidth) / rect.Width;
    }

    if (rect.Height > maxHeight)
    {
        scale = 1 - (rect.Height * scale - maxHeight) / rect.Height;
    }

    ImageSaveOptions imageOptions = new ImageSaveOptions(FileFormatUtil.ExtensionToSaveFormat(extension))
    {
        SaveFormat = SaveFormat.Png,
        Scale = scale,
        Resolution = resolution,
        UseHighQualityRendering = true
    };

    // sharpen formulas
    if (shape.ImageData.ImageType == ImageType.Wmf)
        imageOptions.ImageContrast = 0.55f;

    var output = new MemoryStream();
    r.Save(output, imageOptions);
    output.Seek(0, SeekOrigin.Begin);
    return output;
}

We tried the above scaling algorithm with successful output in terms of image sharpness except text on the image. The text looks blurry.

A: Try using SaveOptions.UseAntiAliasing to improve the quality of a rendered image.


text:
Q: mysql select one row if no row found with an order SELECT
'1' AS SITE
, 'test_id' AS ID
, IFNULL(PREV_DATA, 60) AS PREV_DATA
, IFNULL(PREV_DATA + 2, 60 + 2) AS DATA
FROM 'TABLE'
WHERE ID = 'test_id'
ORDER BY CREATE_DATE DESC
LIMIT 1;

I want only one row even if no row found.
And order is very important.
I tried COUNT(1) or MAX(1), but the result doesn't ordered by create_date...
Please help.
-----ADD SAMPLE DATA

+------+---------+-----------+------+---------------------+
| SITE |   ID    | PREV_DATA | DATA |     CREATE_DATE     |
+------+---------+-----------+------+---------------------+
|    1 | test_id |        60 |   62 | 2016-03-21 00:00:00 |
|    1 | test_id |        62 |   64 | 2016-03-21 10:00:00 |
|    1 | test_22 |        60 |   62 | 2016-03-24 00:00:00 |
+------+---------+-----------+------+---------------------+
I want below data when the ID is 'test_id'.
+------+---------+-----------+------+
| SITE |   ID    | PREV_DATA | DATA |
+------+---------+-----------+------+
|    1 | test_id |        64 |   66 |
+------+---------+-----------+------+
And I want below data when the id is 'test33333'
+------+-----------+-----------+------+
| SITE |   ID      | PREV_DATA | DATA |
+------+-----------+-----------+------+
|    1 | test33333 |        60 |   62 |
+------+-----------+-----------+------+

A: look into UNION.. so you adjoin an additional predefined or "blank" row but with the limit 1, it only shows when another row isn't found
something like this (untested):
SELECT
'1' AS SITE
, 'test_id' AS ID
, IFNULL(PREV_DATA, 60) AS PREV_DATA
, IFNULL(PREV_DATA + 2, 60 + 2) AS DATA
, CREATE_DATE
FROM 'TABLE'
WHERE ID = 'test_id'
union
SELECT
'0' AS SITE
, '' AS ID
, '' AS PREV_DATA
, '' AS DATA
, DATE_SUB(NOW(), INTERVAL -300 YEAR) as CREATE_DATE

ORDER BY CREATE_DATE DESC
LIMIT 1;


text:
Q: Django - How to implement a search box I'm struggling to implement a search bar and could really do with a few suggestions on how it could be done. I'm quite a novice, so please bear with me. My difficulties lie more with the general understanding of what needs to be built than with a specific line of code and somehow that's making it even harder for me to find an answer.
In summary, I have:
Django Models:

*

*Recipe

*Tags (M2M with Recipe)

*Category (ForeignKey with Recipe)

*etc.

I've written Graphene schemas to query the database (Postgres) by Tags, Categories and numerous Recipe attributes (e.g.: Recipe name). You can filter the recipes displayed on the frontend with those queries, by clicking on e.g. a certain category name.
What I'd however also like to have, is a search box where a user can enter a keyword of their choice on the frontend, and get back all the recipes that have something matching that keyword.
I don't know what or how to build something like that. To my novice mind, it seems like a search box of that sort would essentially be a query where I don't know what the filter is in advance, i.e. "fresh" as an input could be a tag, category, part of a recipe name, etc... and I'd want all those matching results back - ideally without the search taking an exaggerated amount of time.
How should I go about building something like this? I've built the search box (React/Tailwind), so it's really all about what the backend needs to provide when a user types a keyword and clicks search.
Thank you for your help! :)
The following code shows my models -> The Recipe model is however quite large (not all code included), so any tips on how to keep the search speed down are also very appreciated.
class Recipe(models.Model):
..
..
..
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=50)
    description = models.TextField(max_length=512, default='')

    tags = models.ManyToManyField('tag.Tag')
    category = models.ForeignKey(
        'tag.Category',
        related_name='recipe_category',
        on_delete=models.SET_NULL,
        null=True,
    )
..
..
..

class Tag(models.Model):

    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=50)

    def __str__(self):
        return self.name

class Category(models.Model):

    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=50)

    def __str__(self):
        return self.name

Edit: Code added

A: So, based on the search term, you are trying to query the category table, tags table, and recipes table. here icontains is a Django-related ORM utility that is equivalent to ILIKE in SQL.
from django.db.models import Q

# in your mutation
search_term = "from your graphene mutation"
categories = Category.objects.filter(name__icontains=search_term).values_list(
    "id", flat=True
)
tags = Tag.objects.filter(name__icontains=search_term).values_list("id", flat=True)
# here the Q object is used to build multiple or conditions
# the final query as response to your frontend
recipies = Recipe.objects.filter(Q(name__icontains=search_term) | Q(description__icontains=search_term) | Q(tags__in=tags) | Q(category__in=categories))

Notice the number of queries here, since we are querying multiple tables, the response could be slow depending on the size of the data in the database. By proper indexing on the database, you can easily overcome this, or you can even use something like full-text search engines like elastic search, etc.


text:
Q: Galaxy tab not showing up as a Drive in Windows XP I just installed Android Ice Cream Sandwich. But I cant see a Drive! USB seems to show up but no Drive to access the files in Windows Explorer.
I can see a USB device ID and it is showing as SAMSUNG Mobile but there is no drive for me to explore .
I was planning to put the original HONEYCOMB zip and reinstall again. Can anyone please let me know what to do ?
Can I transfer a Zip file to Galaxy Tab's SD card somehow? I am trying to reinstall the original ROM.

A: This is because since honeycomb Google have decided to use the MTP protocol to handle PC to Tablet file transfers as it allows them to obfuscate the internal file system on the device and fix one of the most glaring of the early problems with Android, namely that the space for applications and user data was fixed and one could be unable to install an application yet have plenty of space left to put your own personal data.
This means that you either need some special drivers which may be available from Microsoft or you manufacturer.  You will also not have a drive letter for your device as it is no longer an actual drive as far as the computer can see.
Some manufacturers still allowed their devices to be used as USB mass storage (hard drive) devices, but it appears that Google has gotten its way.
You may need to install the Media Transfer Protocol Porting Kit to allow you to see and transfer files to the device.You also may need to get the latest version of Windows Media Player, but I don't know for sure on that one.
From XDA

MTP is a big improvement over USB mass storage — for devices with lots of internal memory, a manufacturer no longer needs to come up with some hard partition between the USB mass storage and internal storage. Instead, they are all in one partition, with MTP providing access to the directory of media files that would normally be available through USB mass storage.This means there is no longer a need for apps on SD card for such devices, because what used to be the "internal SD card" is in the same partition as where applications are stored. The storage on your device can be used for either applications or media, depending on what you want to put on it. You aren't stuck with how much space the manufacturer decided to leave for the two areas.


text:
Q: How to overwrite get method in generic RetrieveAPIView in django rest framework to filter the results I have an API that can list several buildings. Each building belongs to several building groups and each building group contains several buildings.
I want to show single fields of one building group. More specifically I want to show all buildings of one building group in my RetrieveAPIView.
I can list a single BuildingGroup instance using the generic view like so:
class BuildingGroupRetrieveAPIView(RetrieveAPIView):
    serializer_class = BuildingGroupSerializer
    queryset = BuildingGroup.buildings.all()

I assume that I can overwrite the get method to only display a single field of that retrieved object. Specifically I want to display all the objects that are in my many to many relation. Or better to say, I want to retrieve all the complete data within my m2m relation.
Here are my models:
class Building(models.Model):
    name  = models.CharField(max_length=120, null=True, blank=True)

    def __str__(self):
        return self.name

class BuildingGroup(models.Model):
    description           = models.CharField(max_length=500, null=True, blank=True)
    buildings             = models.ManyToManyField(Building, default=None, blank=True)

I tried this without success:
 def get(self):
        building_group = BuildingGroup.objects.get(id='id')
        qs = building_group.buildings.all()
        return qs

my serializer
class BuildingGroupSerializer(serializers.ModelSerializer):

    class Meta:

        model = BuildingGroup

        fields = (
            'description',
             .....
        )

I can attach a screenshot to be more clear.
Any help is highly appreciated. Thanks in advance
Here is my full view:

class BuildingGroupAPIView(ListAPIView):

    permission_classes          = [permissions.IsAdminUser]
    authentication_classes      = [SessionAuthentication]

    serializer_class = BuildingGroupSerializer
    passed_id = None

    def get_queryset(self):
        qs = BuildingGroup.objects.all()
        query = self.request.GET.get('q')
        if query is not None:
            qs = qs.filter(name=query)
        return qs

class BuildingGroupRetrieveAPIView(RetrieveAPIView):
    serializer_class = BuildingGroupSerializer
    queryset = BuildingGroup.buildings.all()

    def get(self):
        building_group = BuildingGroup.objects.get(id='id')
        qs = building_group.buildings.all()
        return qs


text:
Q: mysql select count of distinct rows based on newest date and insert into another table As the subject states, I have a table with multiple rows per user and I need to get a count of all rows for each user with the data from the row with the oldest date AND then insert one new row, with the count value, into another table.
I am importing an attendance list CSV file into a temporary table...which is working fine.  But now I need to process all of the individual records into a single summary record to be added to the final table.
tempTable:
id   email      tDate        cValue  col4    col5   col6
==========================================================
1    a@a.com    2021-01-01    1      foo     bar    foobar
2    b@b.com    2021-01-02    1      bar     foo    barfoo
3    a@a.com    2021-02-01    1      foo     bar    foobar
4    c@c.com    2021-01-15    1      bah     hab    bahhab
5    d@d.com    2021-02-15    1      hab     bah    habbah
5    b@b.com    2021-03-01    1      bar     foo    barfoo
6    a@a.com    2021-04-01    1      foo     bar    foobar
7    d@d.com    2021-03-01    1      hab     bah    habbah

newTable (with newest date)
id   email      tDate        cValue  col4    col5   col6
==========================================================
1    a@a.com    2021-04-01    3      foo     bar    foobar
2    b@b.com    2021-03-01    2      bar     foo    barfoo
3    c@c.com    2021-01-15    1      bah     hab    bahhab
4    d@d.com    2021-03-01    2      hab     bah    habbah

I think the below works (I have tested the select part, but not the full insert yet), but I don't know how to process the GROUP BY email based on tDate being the oldest or newest.  I haven't decided yet where the oldest or newest data should be the final record - but I still need to know how to grab by the date.
INSERT INTO newTable (email,tDate,cValue,col4,col5,col6)
SELECT
   email,
   tDate,
   COUNT(*) as  tValue,
   col4,
   col5,
   col6
FROM tempTable
GROUP BY email ;

When I do an ORDER BY tDate DESC - it is simply ordering the output, not actually ordering the records before the GROUP BY.

A: As Luuk has already explained, this is a non-deterministic query and you should read about MySQL Handling of GROUP BY. ONLY_FULL_GROUP_BY is a good thing and should be enabled.
The idea is to get the MAX tDate and COUNT per email and then use that to join back to the original table values -
SELECT t1.email, t1.tDate, t2.cnt AS cValue, t1.col4, t1.col5, t1.col6
FROM tempTable t1
JOIN (
    SELECT email, MAX(tDate) AS maxDate, COUNT(*) as cnt
    FROM tempTable
    GROUP BY email
) t2 ON t1.email = t2.email AND t1.tDate = t2.maxDate;

Alternatively, if using MySQL 8 you can use window functions, specifically ROW_NUMBER() to assign row numbers per partition ordered by tDate descending and then select where row_number = 1
SELECT email, tDate, cValue, col4, col5, col6
FROM (
    SELECT
        email,
        tDate,
        COUNT(*) OVER (PARTITION BY email) AS cValue,
        col4,
        col5,
        col6 ,
        ROW_NUMBER() OVER (PARTITION BY email ORDER BY tDate DESC) AS rn
    FROM tempTable
) t
WHERE t.rn = 1
ORDER BY email ASC;

A: Kudos to both @nnichols and @Luuk for clueing me into the use of MIN/MAX in the query.  I should have thought of it myself but...well, didn't.  lol.
Just using the MIN or MAX gave me what I was looking for....but with a caveat.  See caveat noted below.  In my case though, I don't have to worry about the caveat so simply doing the MIN/MAX in my current query is all I needed.
INSERT INTO newTable (email,tDate,cValue,col4,col5,col6)
SELECT
   email,
   MAX(tDate),
   COUNT(*) as  tValue,
   col4,
   col5,
   col6
FROM tempTable
GROUP BY email ;

Outputted:
newTable (with newest date)
id   email      tDate        cValue  col4    col5   col6
==========================================================
1    a@a.com    2021-04-01    3      foo     bar    foobar
2    b@b.com    2021-03-01    2      bar     foo    barfoo
3    c@c.com    2021-01-15    1      bah     hab    bahhab
4    d@d.com    2021-03-01    2      hab     bah    habbah

Or with MIN:
newTable (with newest date)
id   email      tDate        cValue  col4    col5   col6
==========================================================
1    a@a.com    2021-01-01    3      foo     bar    foobar
2    b@b.com    2021-01-02    2      bar     foo    barfoo
3    c@c.com    2021-01-15    1      bah     hab    bahhab
4    d@d.com    2021-02-15    2      hab     bah    habbah

CAVEAT: the MAX(tDate) being selected does not mean the rest of the columns being selected are also from the same row.  I saw instances where if col4 for the same email address had different values, that the MIN/MAX(tDate) returned the same col4 value even though the returned tDate was different each time.
If I truly needed all the data from the same tDate row (MIN or MAX), then I would need to do query more like @nnichols answered with.


text:
Q: Unsupported config option for services service: 'chrome' I am trying to setup a selenium grid with docker using this blog
But when i try to run docker-compose up -d
It is giving me error as: Unsupported config option for services service: 'chrome'
 my docker-compose.yml is
version: "3"
services:
  selenium-hub:
    image: selenium/hub:3.141.59-yttrium
    container_name: selenium-hub
    ports:
      - "4444:4444"
  chrome:
    image: selenium/node-chrome:3.141.59-yttrium
    volumes:
      - /dev/shm:/dev/shm
    depends_on:
      - selenium-hub
    environment:
      - HUB_HOST=selenium-hub
      - HUB_PORT=4444
  firefox:
    image: selenium/node-firefox:3.141.59-yttrium
    volumes:
      - /dev/shm:/dev/shm
    depends_on:
      - selenium-hub
    environment:
      - HUB_HOST=selenium-hub
      - HUB_PORT=4444

Docker version shows :
Client: Docker Engine - Community
 Version:           19.03.5
 API version:       1.39 (downgraded from 1.40)
 Go version:        go1.12.12
 Git commit:        633a0ea838
 Built:             Wed Nov 13 07:29:52 2019
 OS/Arch:           linux/amd64
 Experimental:      false

A: I tried to run your docker-compose.yml file and it works without any issues.
Possible reasons for your issue could be

*

*incorrect yaml format or incorrect indentation

*outdated docker-compose version in which case you need to upgrade to a more recent version to use the version 3 format configuration files

my docker compose configuration -
[root@allselenium ~]# docker-compose version
 docker-compose version 1.18.0, build 8dd22a9
 docker-py version: 2.6.1
 CPython version: 3.6.8
 OpenSSL version: OpenSSL 1.0.2k-fips  26 Jan 2017

A: This error usually occurs when you try to use a compose file option that is incompatible with the engine version you are trying to run against.
Please find more information on this here:
https://success.docker.com/article/error-unsupported-config-option-for-services
And here is the compatibility matrix:
https://docs.docker.com/compose/compose-file/compose-versioning/#compatibility-matrix
According to this you should have Compose File format of 3.8


text:
Q: SELECT Unique rows from Datagridview using LINQ I am trying to SELECT ALL rows\columns from a datagridview where the first column is unique using LINQ.
Datagridview:
1     Blue     1111
1     Blue     1111
2     Green    1234
3     Orange   3211
2     Green    1234
4     Red      2222

Trying to get this Output:
1     Blue     1111
2     Green    1234
3     Orange   3211
4     Red      2222

I was able to use the following code which pulls all unique records from the first column but I am not sure how to get the remaining columns:
        Dim unique() As String = (From row As DataGridViewRow In dgvMaestro.Rows.Cast(Of DataGridViewRow)() _
         Where Not row.IsNewRow _
         Select CStr(row.Cells(0).Value)).Distinct.ToArray

        For Each a As String In unique
            Debug.Print(a)
        Next

Output:
1
2
3
4

Thanks

A: First you need import 2 namespaces for distinct with linq to use AsEnumerable() method in DataTables and Field() method in DataRows
Imports System.Data.DataTableExtensions
Imports System.Data.DataRowExtensions

Declare new datatable
Dim NewTbl As New System.Data.DataTable

Add columns in you scenario
NewTbl.Columns.Add("ID", GetType(Integer))
NewTbl.Columns.Add("Color", GetType(String))
NewTbl.Columns.Add("Value", GetType(Integer))

Linq use Table 'TblValues' as you datasource
Dim results = (From row in TblValues.AsEnumerable() select col1 = row(Of Integer)("ID"), col2 = row(Of String)("Color"), col3 = row(Of Integer)("Value")).Distinct().ToList()

Iterate elements in results with for each, the reason is because results is an object  datarow collection and isn't convert auto to table
For each r in results
  Dim Row as System.Data.DataRow = NewTbl.NewRow
  Row("ID") = r.col1
  Row("Color") = r.col2
  Row("Value") = r.col3
  NewTbl.Rows.Add(Row)
next

Now you have a DataTable 'NewTbl' with distinct values inside

A: The best way to solve this is to write a comparer to compare the entire row.
Dim noduplicates = dgvMaestro.Rows.Cast(Of DataGridViewRow).Distinct(New RowComparer())

There are some examples of comparers on msdn


text: Q: Formatting for Appendix only I was wondering if there is an easy way to style and format the appendix different from the thesis. Precisely, I use less width and larger font-size for thesis. The appendix mostly contains proofs and hence, I want wider width and small font-size. I want them attached as one pdf and not distinct pdfs. Any idea how to do that?


text:
Q: Postgresql to Oracle Sql for timezone I have a sql running on Postgresql as follows:
select *
 from table1
where ts >= '2016-12-08 23:01:48 CST'::timestamp with timezone

Where the datetime value will be given by the user.
I need to run this query in Oracle as well, but I am finding it hard to find an exact function in Oracle.
I tried the following in Oracle 12c:
select to_timestamp_tz('2016-12-08 23:01:48','yyyy-mm-dd hh24:mi:ss') as c FROM dual;

But its giving result as:

12/08/2016 23:01:48  GMT-06:00

Any advice, please.
Thanks.

A: You must include the timezone region in your timestamp, for example like this:
SELECT TO_TIMESTAMP_TZ('2016-12-08 23:01:48 CST','yyyy-mm-dd hh24:mi:ss TZR') AS c
FROM dual;

or you can also write
SELECT TO_TIMESTAMP('2016-12-08 23:01:48','yyyy-mm-dd hh24:mi:ss') AT TIME ZONE 'CST' AS c
FROM dual;


text:
Q: Generator function not executed PHP In the following code, I don't get 'handled' on my output. I see the filehandle a resource, the file gets opened en the contructor of FqReader is called, I checked all that. But with execution of FqReader::getread() I don't see output and the returned array is empty. The first while loop also does not get exectuted when I put while(1) instead of the logic test as in the code now.
<?php

class FastqFile {
    function __construct($filename) {
        if (substr($filename, -3, 3) == '.gz') {
            $this->handle = gzopen($filename, 'r');
            return $this->handle;
        }
        else
            $this->handle = fopen($filename, 'r');
            return $this->handle;
    }
}

class FqReader {
    function __construct($file_handle) {
        $this->handle = $file_handle;
    }
    function getread() {
        while ($header = fgets($this->handle) !== false) {
            echo "handled";
            $bases = fgets($this->handle);
            $plus = fgets($this->handle);
            $scores = fgets($this->handle);
            yield array($header, $plus, $scores);
        }
    }
}

$filename = $argv[1];
$file_handle = new FastqFile($filename);
var_dump($file_handle);
$reader = new FqReader($file_handle);
var_dump($reader->getread());

It outputs:
object(FastqFile)#1 (1) {
  ["handle"]=>
  resource(5) of type (stream)
}
object(Generator)#3 (0) {
}

A: $file_handle is a FastqFileinstance. Then you pass that object to fgets(), but you need to pass that object's handle to fgets(). For instance:
class FqReader {
    function __construct($file_handle) {
        $this->handle = $file_handle->handle;
    }
    function getread() {
        while ($header = fgets($this->handle) !== false) {
            echo "handled";
            $bases = fgets($this->handle);
            $plus = fgets($this->handle);
            $scores = fgets($this->handle);
            yield array($header, $plus, $scores);
        }
    }
}

The usage of yield was not showing you that error.

A: Exactly, this works like a charm:
(using a function to open file, not a class)

function openfq($filename)
{
    if (substr($filename, -3, 3) == '.gz') {
        $handle = gzopen($filename, 'r');
        return $handle;
    }
    else
        $handle = fopen($filename, 'r');
        return $handle;
}

class FqReader {
    function __construct($file_handle) {
        $this->handle = $file_handle;
    }
    function getread() {
        while (($header = fgets($this->handle)) !== false) {
            echo "handled";
            $bases = fgets($this->handle);
            $plus = fgets($this->handle);
            $scores = fgets($this->handle);
            yield array($header, $bases, $scores);
        }
    }
}

$filename = $argv[1];
$file_handle = openfq($filename);
var_dump($file_handle);
$reader = new FqReader($file_handle);
var_dump($reader->getread());
foreach($reader->getread() as $read) {
    var_dump($read);
}


text:
Q: How to sort List> by key1 descending order and key2 ascending order if key1 has same values I have a List
List<Map<String,String>> list = new ArrayList<>();
            Map<String,String> map = new HashMap<>();
            Map<String,String> map1 = new HashMap<>();
            Map<String,String> map2 = new HashMap<>();
            map.put("productNumber", "107-001");
            map1.put("productNumber", "108-001");
            map2.put("productNumber", "109-001");
            map.put("price", "1.99");
            map1.put("price", "1.02");
            map2.put("price", "1.99");
            list.add(map);
            list.add(map1);
            list.add(map2);

I sort it by price and revers result
formattedResult = list.stream().sorted(Comparator.comparing(m -> Double.parseDouble(m.get("price")))).collect(Collectors.toList());
            Collections.reverse(formattedResult);

The result of this :
**price  productNumber**
1.99   109-001
1.99   107-001
1.02   108-001

I want to sort it like
**price  productNumber**
    1.99   107-001
    1.99   109-001
    1.02   108-001

If prices equal - compare by product number, end first should be productNumber with a lower value.
Please help!

A: You don't need to reverse result if you use the right comparator (and you can chain comparators, too):
    final Comparator<Map<String, String>> byPrice = Comparator.comparing(m -> Double.parseDouble(m.get("price")), Comparator.reverseOrder());

    formattedResult = list.stream().sorted(byPrice.thenComparing(m -> m.get("productNumber"))).collect(Collectors.toList());

A: It's easier if you build your Comparator in two steps. So try this and print them out.  It works like this.

*

*first it sorts the price in reversed order.

*Then t sorts the product number in regular order for equal prices.

    Comparator<Map<String, String>> comp = Comparator
        .comparing(m ->Double.parseDouble(m.get("price"))
                         ,Comparator.reverseOrder());
    comp = comp.thenComparing(m -> m.get("productNumber"));

Apply it like so.
        List<Map<String, String>> formattedResult =
                list.stream().sorted(comp)
                        .collect(Collectors.toList());

        formattedResult.forEach(m -> System.out.println(
                m.get("price") + " : " + m.get("productNumber")));

This prints
1.99 : 107-001
1.99 : 109-001
1.02 : 108-001

A: You can create reverse comparator for productNumber
 Comparator<Map<String, String>> productNumberReversed =  Comparator.comparing((Map<String, String> mp) -> mp.get("productNumber")).reversed();

And then add it sort method comparator using thenComparing
formattedResult = list.stream()
           .sorted(Comparator.comparing((Map<String, String> m) -> Double.parseDouble(m.get("price"))).thenComparing(productNumberReversed)).collect(Collectors.toList());
    Collections.reverse(formattedResult);


text:
Q: how to stop jquery hover slidetoggle queuing I am trying to create a horizontal menu, which when hovered, expands upwords.
The problem is when you hover over them all quickly, they have a horrible jumping effect which  looks nasty. How can I prevent this, and also is there a better way to write this function?
 $(".toggle_hov").hover(function () {
    $(this).find(".toggle_cont").stop().slideToggle();
    $('.toggle_hov.active').not(this).removeClass('active');
    $(this).toggleClass('active');
    var $this = $(this).parent().find('.toggle_cont');
    $(".toggle_cont").not($this).slideUp();
  });

many thanks
http://jsfiddle.net/Js5Z5/1

A: I would suggest you use some delay in hover this way it will only open slide up the many if user hovers for given time. it wont slide open if you quickly move over it.
 var delay=500, setTimeoutConst;
    $(".toggle_hov").hover(function () {
        that = $(this);
         setTimeoutConst = setTimeout(function () {
            $(that).find(".toggle_cont").slideToggle();
            $('.toggle_hov.active').not(this).removeClass('active');
            $(that).toggleClass('active');
            var $this = $(this).parent().find('.toggle_cont');
            $(".toggle_cont").not($this).slideUp();
          }, delay)
    }, function(){
         clearTimeout(setTimeoutConst );

      });

Here's jsfiddle