text:
Q: Find which page an address belongs to I could find out the address of a buffer in memory trivially as follows :-
int main() {
  char buffer[100];
  printf("%p\n, buffer);
}

Is there any way I could print out the address of the page the buffer belongs to?
[EDIT]
I'm on a Linux machine, Ubuntu to be exact. I'm curious about both 32 and 64 bit versions.

A: There seems to be no system call provided by the Linux where we can get such information. However I think we can get the information(/proc directory) by following steps:
Get the buffer address assume 0x7fffffffe0b0(attach with gdb so that program would pause)
Execute the command(cat /proc/3586/maps)assume PID = 3586
Execute the command(cat /proc/3586/numa_maps)

Now following buffer address belongs to the [stack] segments below.
mantosh@mantosh4u:/proc/3586$ cat maps
00400000-00401000 r-xp 00000000 08:03 5116562                            /home/mantosh/practice/bakwas
00600000-00601000 r--p 00000000 08:03 5116562                            /home/mantosh/practice/bakwas
00601000-00602000 rw-p 00001000 08:03 5116562                            /home/mantosh/practice/bakwas
7ffff7204000-7ffff72ff000 r-xp 00000000 08:03 23336063                   /lib/x86_64-linux-gnu/libm-2.15.so
7ffff72ff000-7ffff74fe000 ---p 000fb000 08:03 23336063                   /lib/x86_64-linux-gnu/libm-2.15.so
7ffff74fe000-7ffff74ff000 r--p 000fa000 08:03 23336063                   /lib/x86_64-linux-gnu/libm-2.15.so
7ffff74ff000-7ffff7500000 rw-p 000fb000 08:03 23336063                   /lib/x86_64-linux-gnu/libm-2.15.so
7ffff7500000-7ffff76b5000 r-xp 00000000 08:03 23334413                   /lib/x86_64-linux-gnu/libc-2.15.so
7ffff76b5000-7ffff78b5000 ---p 001b5000 08:03 23334413                   /lib/x86_64-linux-gnu/libc-2.15.so
7ffff78b5000-7ffff78b9000 r--p 001b5000 08:03 23334413                   /lib/x86_64-linux-gnu/libc-2.15.so
7ffff78b9000-7ffff78bb000 rw-p 001b9000 08:03 23334413                   /lib/x86_64-linux-gnu/libc-2.15.so
7ffff78bb000-7ffff78c0000 rw-p 00000000 00:00 0
7ffff78c0000-7ffff78d5000 r-xp 00000000 08:03 23331018                   /lib/x86_64-linux-gnu/libgcc_s.so.1
7ffff78d5000-7ffff7ad4000 ---p 00015000 08:03 23331018                   /lib/x86_64-linux-gnu/libgcc_s.so.1
7ffff7ad4000-7ffff7ad5000 r--p 00014000 08:03 23331018                   /lib/x86_64-linux-gnu/libgcc_s.so.1
7ffff7ad5000-7ffff7ad6000 rw-p 00015000 08:03 23331018                   /lib/x86_64-linux-gnu/libgcc_s.so.1
7ffff7ad6000-7ffff7bbc000 r-xp 00000000 08:03 1573341                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.18
7ffff7bbc000-7ffff7dbb000 ---p 000e6000 08:03 1573341                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.18
7ffff7dbb000-7ffff7dc3000 r--p 000e5000 08:03 1573341                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.18
7ffff7dc3000-7ffff7dc5000 rw-p 000ed000 08:03 1573341                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.18
7ffff7dc5000-7ffff7dda000 rw-p 00000000 00:00 0
7ffff7dda000-7ffff7dfc000 r-xp 00000000 08:03 23336064                   /lib/x86_64-linux-gnu/ld-2.15.so
7ffff7fd2000-7ffff7fd7000 rw-p 00000000 00:00 0
7ffff7ff8000-7ffff7ffb000 rw-p 00000000 00:00 0
7ffff7ffb000-7ffff7ffc000 r-xp 00000000 00:00 0                          [vdso]
7ffff7ffc000-7ffff7ffd000 r--p 00022000 08:03 23336064                   /lib/x86_64-linux-gnu/ld-2.15.so
7ffff7ffd000-7ffff7fff000 rw-p 00023000 08:03 23336064                   /lib/x86_64-linux-gnu/ld-2.15.so
7ffffffde000-7ffffffff000 rw-p 00000000 00:00 0                          [stack]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]

We can see the output from the last command mention above(numa_maps)
mantosh@mantosh4u:/proc/3586$ cat numa_maps
00400000 default file=/home/mantosh/practice/bakwas anon=1 dirty=1 N0=1
00600000 default file=/home/mantosh/practice/bakwas anon=1 dirty=1 N0=1
00601000 default file=/home/mantosh/practice/bakwas anon=1 dirty=1 N0=1
7ffff7204000 default file=/lib/x86_64-linux-gnu/libm-2.15.so mapped=16 mapmax=52 N0=16
7ffff72ff000 default file=/lib/x86_64-linux-gnu/libm-2.15.so
7ffff74fe000 default file=/lib/x86_64-linux-gnu/libm-2.15.so anon=1 dirty=1 N0=1
7ffff74ff000 default file=/lib/x86_64-linux-gnu/libm-2.15.so anon=1 dirty=1 N0=1
7ffff7500000 default file=/lib/x86_64-linux-gnu/libc-2.15.so anon=1 dirty=1 mapped=63 mapmax=111 N0=63
7ffff76b5000 default file=/lib/x86_64-linux-gnu/libc-2.15.so
7ffff78b5000 default file=/lib/x86_64-linux-gnu/libc-2.15.so anon=4 dirty=4 N0=4
7ffff78b9000 default file=/lib/x86_64-linux-gnu/libc-2.15.so anon=2 dirty=2 N0=2
7ffff78bb000 default anon=3 dirty=3 N0=3
7ffff78c0000 default file=/lib/x86_64-linux-gnu/libgcc_s.so.1 mapped=3 mapmax=22 N0=3
7ffff78d5000 default file=/lib/x86_64-linux-gnu/libgcc_s.so.1
7ffff7ad4000 default file=/lib/x86_64-linux-gnu/libgcc_s.so.1 anon=1 dirty=1 N0=1
7ffff7ad5000 default file=/lib/x86_64-linux-gnu/libgcc_s.so.1 anon=1 dirty=1 N0=1
7ffff7ad6000 default file=/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.18 mapped=90 mapmax=20 N0=90
7ffff7bbc000 default file=/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.18
7ffff7dbb000 default file=/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.18 anon=8 dirty=8 N0=8
7ffff7dc3000 default file=/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.18 anon=2 dirty=2 N0=2
7ffff7dc5000 default anon=3 dirty=3 N0=3
7ffff7dda000 default file=/lib/x86_64-linux-gnu/ld-2.15.so anon=1 dirty=1 mapped=27 mapmax=106 N0=27
7ffff7fd2000 default anon=5 dirty=5 N0=5
7ffff7ff8000 default anon=3 dirty=3 N0=3
7ffff7ffb000 default
7ffff7ffc000 default file=/lib/x86_64-linux-gnu/ld-2.15.so anon=1 dirty=1 N0=1
7ffff7ffd000 default file=/lib/x86_64-linux-gnu/ld-2.15.so anon=2 dirty=2 N0=2
7ffffffdd000 default stack anon=2 dirty=2 N0=2

The more detailed information regarding each line entry can be found in proc and numa  documentation. However our buffer belongs to the last entry which contains the information about the page and the base address and some other useful information(like dirty status of page and all). Hope this might be useful.


text:
Q: FastAPI Upload to S3 I've been scratching my head on this for days now. I still couldn't solve the problem. Basically, I just wanted to put a CSV file in a LocalStack S3, and I can't get it working.
Here's the snippet of my code:
api.py
from files import s3, AWS_S3_BUCKET_NAME, upload_file_to_bucket

@router.post('/api/customer/generate/upload',
                 name='Upload CSV to AWS S3 Bucket',
                 status_code=201)
    async def post_upload_user_csv(file_obj: UploadFile = File(...)):
        upload_obj = upload_file_to_bucket(s3_client=s3(),
                                           file_obj=file_obj.file,
                                           bucket=AWS_S3_BUCKET_NAME,
                                           folder='CSV',  # To Be updated
                                           object_name=file_obj.filename)
        if upload_obj:
            return JSONResponse(content="Object has been uploaded to bucket successfully",
                                status_code=status.HTTP_201_CREATED)
        else:
            raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
                                detail="File could not be uploaded")

files.py
import os
import boto3
import logging

from botocore.client import BaseClient
from botocore.exceptions import ClientError

AWS_ACCESS_KEY_ID = os.getenv('POSTGRES_HOST')
AWS_SECRET_KEY = os.getenv('AWS_SECRET_KEY')
AWS_S3_BUCKET_NAME = os.getenv('AWS_S3_BUCKET_NAME')

def s3() -> BaseClient:
    client = boto3.client(service_name='s3',
                          aws_access_key_id=AWS_ACCESS_KEY_ID,
                          aws_secret_access_key=AWS_SECRET_KEY,
                          endpoint_url='http://localhost:4566/')  # Use LocalStack Endpoint

    return client

def upload_file_to_bucket(s3_client, file_obj, bucket, folder, object_name=None):
    """Upload a file to an S3 bucket

    :param s3_client: S3 Client
    :param file_obj: File to upload
    :param bucket: Bucket to upload to
    :param folder: Folder to upload to
    :param object_name: S3 object name. If not specified then file_name is used
    :return: True if file was uploaded, else False
    """
    # If S3 object_name was not specified, use file_name
    if object_name is None:
        object_name = file_obj

    # Upload the file
    try:
        # with open("files", "rb") as f:
        s3_client.upload_fileobj(file_obj, bucket, f"{folder}/{object_name}")
    except ClientError as e:
        logging.error(e)
        return False
    return True

The problem is that s3_client needs to open the file in binary mode first before I can upload it to s3 bucket. However, this can't be done directly and the file needs to be saved temporarily on the FastAPI server but I really don't want to do that for obvious reasons.
Any help will be much appreciated. Thank you in advance!

A: Can you show the error first?
Bassicly. I have done it before. Upload directly a file from the front-end to aws-s3. Can you try to add ContentType to upload_fileobj. Here is my code
 content_type = mimetypes.guess_type(fpath)[0]
 s3.Bucket(bucket_name).upload_fileobj(Fileobj=file, Key=file_path,
                                          ExtraArgs={"ACL": "public-read",
                                                     "ContentType": content_type})

another way. You should try to convert files to io.BytesIO
 def s3_upload(self, file, file_path, bucket_name, width=None, height=None, make_thumb=False, make_cover=False):
    s3 = boto3.resource(service_name='s3')
    obj = BytesIO(self.image_optimize_from_buffer(file, width, height, make_thumb, make_cover))
    s3.Bucket(bucket_name).upload_fileobj(Fileobj=obj, Key=file_path,
                                          ExtraArgs={"ACL": "public-read", "ContentType": file.content_type})
    return f'https://{bucket_name}.s3.amazonaws.com/{file_path}'

redpajama_set_name: RedPajamaStackExchange


text:
Q: How do I check in JAVA if a string is not equal to? How do I check in Java if a string is not equal to "ABC"?
I know that to check if it is equal we can type string.equals("ABC") but how do I do it when I want to check if it is not equal?

A: Simply negate the result of equals:
!string.equals("ABC")
String.equals returns a boolean value, to get the inverse of any boolean value, use the ! operator:
boolean t = true; // t will be true
boolean f = !t; // f will be false

A: Negate the returning value from string.equals(TestString):
!string.equals("ABC")

You better have a look at Logical Operators in Java

A: You can simply write it like this.
!string.equals("ABC")


text:
Q: Create shortcut icon to deploy project VS I  want to create a setup for project. So, I've read this article Here.
Now , I want to add a shortcut icon to Desktop, so , when i add the icon like in the following steps it doesn't appear in the dialog box Current icon and then i can't clik to the button OK

Please help.Thanks


text:
Q: Issue with Appium Environment Setup in Mac OS Working in latest version of IOS.
Anyone provide steps to run and record the script in latest version appium IOS

A: To Setup

*

*You need to download Appium's latest version from here

*Just drag and drop the appium file to Applications folder

*Start it just with double click from applications

To work with Appium You can watch tutorial videos on YouTube. One is my own


text:
Q: Kafka->S3->Oozie->Hadoop, Kafka writing to a S3 location, oozie read this folder every 15 mins and push the new files to hadoop The above title describes the workflow and I want to validate the design here, plus discuss how to manage the updates coming to shared S3 folder.
As oozie is consumer of data, and kafka is producer of data here, how do I maintain which files have already been consumed and which files are yet to be consumed, given that the producer of data is continuously writing to the location.
Are there any atomicity features provided by Amazon-s3 to maintain this?
Update Note : Kafka cannot directly write to HDFS because of network connectivity constraints.

A: I think you're making life hard for yourself. You can just use Kafka Connect (which is part of Kafka) and the HDFS connector or S3 connector.

A: You can use Oozie coordinator here. The oozie cordinator can poll continuously for new files and once a file has arrived, it can trigger a oozie workflow to copy copy the data. The file names/folders you can create based on the format ${baseFsURI}/${YEAR}/${MONTH}/${DAY}/${HOUR}/${MINUTE}. It will help oozie cordinator to identify the new files that has arrived.
More information is here : https://oozie.apache.org/docs/3.1.3-incubating/CoordinatorFunctionalSpec.html
To set up oozie cordinator to poll for s3 files use : https://community.cloudera.com/t5/Batch-Processing-and-Workflow/Getting-Oozie-Coordinator-datasets-working-with-S3-after-a-lost/td-p/27233


text:
Q: Copying a part of string to another string I have a string of words that have word separator between them, in another string, I wanna put the words of the main string but one by one, compare the word to some other string and put it into an array of strings.
So basically I have
1) an array of words, and
2) a string of words
 that are in the form (word-word-word...), and what I need to do is check if one of these words is in the array of words. If yes, then, delete it.
How I'm going to do that is by defining a new 'char' type variable, walking through my main sentence and as long as I haven't reached the word separator, I add the word to the new string. Using another function, I check if this one-word string exists in the array. If no, I add the word to a temp array, then free the string and repeat until the end of the sentence. but I can't seem to do it right (I have just started studying this subject of chars, arrays of chars and so)
void delete_words(char *words[],int n, char *sentence ){
int m=strlen(sentence);
char tmp[m];
char * str=(char*)malloc(sizeof(char)*(MAX_LEN));
int i=0,end=0,k,z;
for(k=0; k<m; k++)
{
    z=0;
    while(*sentence && sentence[i]!= WORD_SEPERATOR)
    {
        str[z]=sentence[i];
        i++;
        z++;
    }

    if(is_string_in_array(words,n,str)==false)
    {
        int len=strlen(str);
        strcpy(tmp+k,str);
        end=end+len;
        tmp[end]=WORD_SEPERATOR;
        free(str);
    }}

I do realize that this is completely wrong and I can find some of the mistakes, but I don't know how to fix it because as I said, it's my first homework on the subject. will be really thankful for help!

A: Here select_next_word will iterate through string and tries to extract words one by one then we check it against the array, if it does not exist, we add it to destination string
#include <stdio.h>
#include <String.h>

void select_next_word(char **buffer, char **next_word)
{
    *next_word = *buffer;
    while(**buffer != 0)
    {
        if(**buffer == '-')
        {
            **buffer = 0;
             (*buffer)++;
            break;
        }

        (*buffer)++;
    }
}

int main()
{
    #define CHECK_ARR_SIZE  2
    char check_arr[CHECK_ARR_SIZE][32] = {"this", "test"}; //make second dim large enough
    char test[] = "this-is-a-test-string";
    char dest[32]; //be sure it is large enough
    char *next_word;
    char *temp = test;

    while(1)
    {
        select_next_word(&temp, &next_word);
        int i = 0;
        int word_found = 0;
        for( ; i < CHECK_ARR_SIZE ;i++)
        {
            if(strcmp(next_word, check_arr[i]) == 0)
            {
                word_found = 1;
                break;
            }
        }

        if(!word_found)
        strcat(dest, next_word);
        if(*temp == 0)
         break;
    }

    printf(dest);
}


text:
Q: Get sum of positive values and sum of negative values I have a DataFrame as follows.
d = {}
d['Model'] = ['M1', 'M2'] * 4 * 3 * 5
d['Support'] = ['S1', 'S1', 'S2', 'S2'] * 2 * 3 * 5
d['Group'] = ['G1', 'G2', 'G2'] * 2 * 4 * 5
d['Case'] = ['C11', 'C21', 'C22', 'C31', 'C32'] * 2 * 4 * 3
val1 = []
val2 = []
random.seed(0)
for i in range (2*4*3*5):
    val1.append(random.randrange(-10, 11))
    val2.append(random.randrange(-10, 11))
d['val1'] = val1
d['val2'] = val2
df = pd.DataFrame(d)

I am looking for the maximum value of the sum of the positive values for each Support, Group and Case and with reference to the Model and the sum of the minimum values of the sum of the negative values.
This is my attempt:
df1 = df.groupby(['Model', 'Support', 'Group', 'Case'])[['val1', 'val2']].sum()

df2 = df1.groupby(['Model', 'Support', 'Group', 'Case']).agg([
    ('max' , lambda x : x[x > 0].sum()),
    ('min' , lambda x : x[x < 0].sum())
])

df3 = df2.groupby(['Support', 'Group', 'Case']).agg([
    ('max' , max),
    ('Model', lambda x: x.idxmax()[0]),
    ('min' , min),
    ('Model', lambda x: x.idxmin()[0]),
])

(See edition of 14 Jan 2021)

The results of the last DataFrame df3 are fine, but this is not the output format I want.
I need to filter the last DataFrame df3 to get the results this way:

Edited 14 Jan 2021
Reviewing the @aneroid results I have seen that the df3 values are not as expected.
For Support S1, Group G1, Case C11 this will be the result:
df_M1S1G1_1 = df.loc[df['Model'] == 'M1']
df_M1S1G1_2 = df_M1S1G1_1.loc[df['Support'] == 'S1']
df_M1S1G1_3 = df_M1S1G1_2.loc[df['Group'] == 'G1']

    Model   Support Group   Case    val1    val2
0   M1      S1      G1      C11     2       3
12  M1      S1      G1      C22     -8      0
24  M1      S1      G1      C32     5       0
36  M1      S1      G1      C21     7       -4
48  M1      S1      G1      C31     -6      -9
60  M1      S1      G1      C11     10      0
72  M1      S1      G1      C22     10      -4
84  M1      S1      G1      C32     -8      -3
96  M1      S1      G1      C21     -5      0
108 M1      S1      G1      C31     -9      7

df_M2S1G1_1 = df.loc[df['Model'] == 'M2']
df_M2S1G1_2 = df_M2S1G1_1.loc[df['Support'] == 'S1']
df_M2S1G1_3 = df_M2S1G1_2.loc[df['Group'] == 'G1']

    Model   Support Group   Case    val1    val2
9   M2      S1      G1      C32     -2      7
21  M2      S1      G1      C21     -10     -8
33  M2      S1      G1      C31     -1      7
45  M2      S1      G1      C11     9       -2
57  M2      S1      G1      C22     -8      0
69  M2      S1      G1      C32     10      7
81  M2      S1      G1      C21     -10     7
93  M2      S1      G1      C31     5       8
105 M2      S1      G1      C11     -6      7
117 M2      S1      G1      C22     -8      -10

Therefore:
0  M1 S1 G1 C11 val1 = 2  val2 = 3
60 M1 S1 G1 C11 val1 = 10 val2 = 0

val1_sum_pos = 12
val1_sum_neg = 0

val2_sum_pos = 3
val2_sum_neg = 0

45  M2 S1 G1 C11 val1 = 9  val2 = -2
105 M2 S1 G1 C11 val1 = -6 val2 = 7

val1_sum_pos = 9
val1_sum_neg = -6

val2_sum_pos = 7
val2_sum_neg = -2

And as a result:
                                                      val1
     val2
                            max  Model_max  min  Model_min  max  Model_max  min  Model_min
    Support  Group  Case
    S1       G1     C11     12   M1         -6   M2         7     M2        -2
M2

These results are in line with the solution proposed by @aneroid.

A: Here's a different approach from my previous answer. Requires a little more "prep-code" but fewer steps to massage the dataframe afterwards.
As before, this is based on starting at your step2 (skipping step1):
df1 = df.groupby(['Model', 'Support', 'Group', 'Case']).agg([
    ('sum_pos', lambda x: x[x > 0].sum()),
    ('sum_neg', lambda x: x[x < 0].sum())
])

First, a template dataframe for the results:
res = pd.DataFrame(
    columns=pd.MultiIndex.from_product(
        [('val1', 'val2'),
         ('sum_pos_max', 'Model_max', 'sum_neg_min', 'Model_min'),
]))

This function will be used for apply (again), with the difference that we'll first create an empty DataFrame with the column structure needed. (And optionally, can be modified to use with a for group in grouped loop, without using apply, where each record is appended to it.)
def model_minmax_opt(gr):
    # start with a deep copy of the result template
    tf = res.copy(deep=True)
    for val in ['val1', 'val2']:
        max_pos = gr[(val, 'sum_pos')].idxmax()
        min_neg = gr[(val, 'sum_neg')].idxmin()
        tf.loc[0, [(val, 'sum_pos_max'), (val, 'Model_max')]] = gr.loc[max_pos, [(val, 'sum_pos'), ('Model', '')]].values
        tf.loc[0, [(val, 'sum_neg_min'), (val, 'Model_min')]] = gr.loc[min_neg, [(val, 'sum_neg'), ('Model', '')]].values
    return tf

Then create a grouping and apply it:
group = df1.reset_index().groupby(['Support', 'Group', 'Case'])
df2 = group.apply(model_minmax_opt)
df2.reset_index(level=3, drop=True, inplace=True)  # get rid of the added 0-index

Result df2, same as the other one, with better column ordering:
                                                          val1                                        val2
                   sum_pos_max Model_max sum_neg_min Model_min sum_pos_max Model_max sum_neg_min Model_min
Support Group Case
S1      G1    C11           12        M1          -6        M2           7        M2          -2        M2
              C21            7        M1         -20        M2           7        M2          -8        M2
              C22           10        M1         -16        M2           0        M1         -10        M2
              C31            5        M2         -15        M1          15        M2          -9        M1
              C32           10        M2          -8        M1          14        M2          -3        M1
        G2    C11            9        M1         -23        M1          14        M1          -9        M1
              C21           22        M1         -13        M2          21        M1          -9        M1
              C22           24        M2          -6        M1          14        M2         -12        M2
              C31           30        M2         -28        M1          28        M2          -6        M1
              C32           11        M2         -10        M2          11        M2         -16        M1
S2      G1    C11            9        M1          -8        M1           1        M2         -15        M1
              C21            6        M1          -9        M1          15        M2         -13        M1
              C22           11        M2          -3        M1           0        M1          -8        M2
              C31            5        M1          -7        M2           4        M1          -5        M1
              C32           17        M1           0        M1           0        M1         -13        M2
        G2    C11           20        M1         -10        M2          11        M1         -14        M1
              C21            5        M1         -19        M1           6        M2         -18        M2
              C22           11        M1         -23        M2          24        M1         -15        M2
              C31           26        M1         -22        M2          10        M2         -12        M1
              C32           11        M2         -20        M1          13        M2         -11        M1

A: Here's another two solutions, which are more comparable to your original code and final dataframe in your post.
The first step has a slight modification to the order of the groupby. The minmax() function determines whether it's been given a column of positive or negative sums; and applies idxmin/idxmax as required. And whether the return should be the value or the Model, is passed in as a parameter (y). Using agg can generate additional columns under the existing column index, as another level. This gives the same format as the one in your post.
df1 = df.groupby(['Support', 'Group', 'Case', 'Model']).agg([
    ('sum_pos', lambda x: x[x > 0].sum()),
    ('sum_neg', lambda x: x[x < 0].sum())
])

# Model is the last column of the index, so `-1`
def minmax(x, y):
    """x is the series, y='val' or 'Model'"""
    min = x.min()
    if min >= 0:  # or check x.name
        # postive column, return max's; or both vals are 0
        max_pos = x.idxmax()
        return x[max_pos] if y == 'val' else max_pos[-1]
    # negative column, return min's
    return min if y == 'val' else x.idxmin()[-1]

# use `minmax` to generate columns in an aggregate:
df1.groupby(['Support', 'Group', 'Case']).agg([
    ('val', lambda x: minmax(x, 'val')),
    ('Model', lambda x: minmax(x, 'Model')),
])

Result is the same as my previous two answers, with an identical structure to your post (3-level-index columns):
                                          val1                        val2
                         sum_pos       sum_neg       sum_pos       sum_neg
                       val Model     val Model     val Model     val Model
Support Group Case
S1      G1    C11       12    M1      -6    M2       7    M2      -2    M2
              C21        7    M1     -20    M2       7    M2      -8    M2
              C22       10    M1     -16    M2       0    M1     -10    M2
              C31        5    M2     -15    M1      15    M2      -9    M1
              C32       10    M2      -8    M1      14    M2      -3    M1
        G2    C11        9    M1     -23    M1      14    M1      -9    M1
              C21       22    M1     -13    M2      21    M1      -9    M1
              C22       24    M2      -6    M1      14    M2     -12    M2
              C31       30    M2     -28    M1      28    M2      -6    M1
              C32       11    M2     -10    M2      11    M2     -16    M1
S2      G1    C11        9    M1      -8    M1       1    M2     -15    M1
              C21        6    M1      -9    M1      15    M2     -13    M1
              C22       11    M2      -3    M1       0    M1      -8    M2
              C31        5    M1      -7    M2       4    M1      -5    M1
              C32       17    M1       0    M1       0    M1     -13    M2
        G2    C11       20    M1     -10    M2      11    M1     -14    M1
              C21        5    M1     -19    M1       6    M2     -18    M2
              C22       11    M1     -23    M2      24    M1     -15    M2
              C31       26    M1     -22    M2      10    M2     -12    M1
              C32       11    M2     -20    M1      13    M2     -11    M1

An even shorter solution but generates a tuple of (min/max val, Model) instead of having them in different columns:
df1 = df.groupby(['Support', 'Group', 'Case', 'Model']).agg([
    ('sum_pos', lambda x: x[x > 0].sum()),
    ('sum_neg', lambda x: x[x < 0].sum())
])

df1.groupby(['Support', 'Group', 'Case']).agg({
    ('val1', 'sum_pos'): lambda x: (x.max(), x.idxmax()[-1]),
    ('val1', 'sum_neg'): lambda x: (x.min(), x.idxmin()[-1]),
    ('val2', 'sum_pos'): lambda x: (x.max(), x.idxmax()[-1]),
    ('val2', 'sum_neg'): lambda x: (x.min(), x.idxmin()[-1]),
})

Result sample:
                                   val1                 val2
                     sum_pos    sum_neg   sum_pos    sum_neg
Support Group Case
S1      G1    C11   (12, M1)   (-6, M2)   (7, M2)   (-2, M2)
              C21    (7, M1)  (-20, M2)   (7, M2)   (-8, M2)
              C22   (10, M1)  (-16, M2)   (0, M1)  (-10, M2)
              C31    (5, M2)  (-15, M1)  (15, M2)   (-9, M1)
...

A: You've said "looking for the maximum value of the sum of the positive values" and "sum of the minimum values of the sum of the negative values" but then in your first step, you've applied sum() to the entire groupby without distinguishing between +ve or -ve values. To me, your second step df2 = df1.groupby(...).agg(...) should actually be your first step.
Setting up df as per your original code, then:
# doing your 2nd step as the first step
df1 = df.groupby(['Model', 'Support', 'Group', 'Case']).agg([
    ('sum_pos', lambda x: x[x > 0].sum()),
    ('sum_neg', lambda x: x[x < 0].sum())
])

# Btw, some sum's are `0` in `df1`

# stacking `val1` and `val2` into a column
df2 = df1.stack(level=0)
df2.index.names = df2.index.names[:-1] + ['val']

Create a function which will calculate the model associated with the "max of positive" and "min of negative" values, per val and include that value in the return:
def model_minmax(gr):
    """modifies both rows of the group object passed in"""
    gr[['sum_pos_max', 'Model_max']] = gr.loc[gr['sum_pos'].idxmax(), ['sum_pos', 'Model']]
    gr[['sum_neg_min', 'Model_min']] = gr.loc[gr['sum_neg'].idxmin(), ['sum_neg', 'Model']]
    return gr

Create the groups and apply() the function above:
group = df2.reset_index().groupby(['Support', 'Group', 'Case', 'val'])
df3 = group.apply(model_minmax)
# sort values and drop every alternate row
df3 = df3.sort_values(['Support', 'Group', 'Case', 'val', 'Model'])[::2]

What df3.head(4) looks like:
   Model Support Group Case   val  sum_neg  sum_pos  sum_pos_max Model_max  sum_neg_min Model_min
0     M1      S1    G1  C11  val1        0       12           12        M1           -6        M2
1     M1      S1    G1  C11  val2        0        3            7        M2           -2        M2
2     M1      S1    G1  C21  val1       -5        7            7        M1          -20        M2
3     M1      S1    G1  C21  val2       -4        0            7        M2           -8        M2

Drop the 'Model', sum_neg', and 'sum_pos' columns and some steps to get the data in a similar format to the one you have:
df3.drop(['Model', 'sum_neg', 'sum_pos'], axis=1, inplace=True)

df4 = df3.pivot(index=['Support', 'Group', 'Case'],
                columns='val',
                values=['sum_pos_max', 'Model_max', 'sum_neg_min', 'Model_min'],
)
df4 = df4.swaplevel(0, 1, axis=1).sort_index(axis=1, level=0)

Result:
               val                                        val1                                        val2
                   Model_max Model_min sum_neg_min sum_pos_max Model_max Model_min sum_neg_min sum_pos_max
Support Group Case
S1      G1    C11         M1        M2          -6          12        M2        M2          -2           7
              C21         M1        M2         -20           7        M2        M2          -8           7
              C22         M1        M2         -16          10        M1        M2         -10           0
              C31         M2        M1         -15           5        M2        M1          -9          15
              C32         M2        M1          -8          10        M2        M1          -3          14
        G2    C11         M1        M1         -23           9        M1        M1          -9          14
              C21         M1        M2         -13          22        M1        M1          -9          21
              C22         M2        M1          -6          24        M2        M2         -12          14
              C31         M2        M1         -28          30        M2        M1          -6          28
              C32         M2        M2         -10          11        M2        M1         -16          11
S2      G1    C11         M1        M1          -8           9        M2        M1         -15           1
              C21         M1        M1          -9           6        M2        M1         -13          15
              C22         M2        M1          -3          11        M1        M2          -8           0
              C31         M1        M2          -7           5        M1        M1          -5           4
              C32         M1        M1           0          17        M1        M2         -13           0
        G2    C11         M1        M2         -10          20        M1        M1         -14          11
              C21         M1        M1         -19           5        M2        M2         -18           6
              C22         M1        M2         -23          11        M1        M2         -15          24
              C31         M1        M2         -22          26        M2        M1         -12          10
              C32         M2        M1         -20          11        M2        M1         -11          13

Note that these values will differ from yours due to your first step, as mentioned above. If you're sure those are correct, it can be added before the one's I've given.


text:
Q: scala how to reduce while n > 0 I am wondering if there's a way to deal with a while (n > 0) loop in a more functional way, I have a small Scala app that counts the number of digits equal to K from a range from 1 to N:
for example 30 and 3 would return 4 [3, 13, 23, 30]
object NumKCount {

  def main(args: Array[String]): Unit = {
    println(countK(30,3))
  }

  def countKDigit(n:Int, k:Int):Int = {
      var num = n
      var count = 0
      while (num > 10) {
        val digit = num % 10
        if (digit == k) {count += 1}
        num = num / 10
      }
      if (num == k) {count += 1}
      count
  }

  def countK(n:Int, k:Int):Int = {
    1.to(n).foldLeft(0)((acc, x) => acc + countKDigit(x, k))
  }
}

I'm looking for a way to define the function countKDigit using a purely functional approach

A: First expand number n into a sequence of digits
def digits(n: Int): Seq[Int] = {
  if (n < 10) Seq(n)
  else digits(n / 10) :+ n % 10
}

Then reduce the sequence by counting occurrences of k
def countKDigit(n:Int, k:Int):Int = {
  digits(n).count(_ == k)
}

Or you can avoid countKDigit entirely by using flatMap
def countK(n:Int, k:Int):Int = {
  1.to(n).flatMap(digits).count(_ == k)
}

A: Assuming that K is always 1 digit, you can convert n to String and use collect or filter, like below (there's not much functional stuff you can do with Integer):
def countKDigit(n: Int, k: Int): Int = {
  n.toString.collect({ case c if c.asDigit == k => true }).size
}

or
def countKDigit(n: Int, k: Int): Int = {
  n.toString.filter(c => c.asDigit == 3).length
}

E.g.
scala> 343.toString.collect({ case c if c.asDigit == 3 => true }).size
res18: Int = 2

scala> 343.toString.filter(c => c.asDigit == 3).length
res22: Int = 2

A: What about the following approach:
scala> val myInt = 346763
myInt: Int = 346763

scala> val target = 3
target: Int = 3

scala> val temp = List.tabulate(math.log10(myInt).toInt + 1)(x => math.pow(10, x).toInt)
temp: List[Int] = List(1, 10, 100, 1000, 10000, 100000)

scala> temp.map(x => myInt / x % 10)
res17: List[Int] = List(3, 6, 7, 6, 4, 3)

scala> temp.count(x => myInt / x % 10 == target)
res18: Int = 2

A: Counting the occurrences of a single digit in a number sequence.
def countK(n:Int, k:Int):Int = {
  assert(k >= 0 && k <= 9)
  1.to(n).mkString.count(_ == '0' + k)
}

If you really only want to modify countKDigit() to a more functional design, there's always recursion.
def countKDigit(n:Int, k:Int, acc: Int = 0):Int =
  if (n == 0) acc
  else countKDigit(n/10, k, if (n%10 == k) acc+1 else acc)


text:
Q: ads not showing with AdMob GMA SDK 7.64 I have updated my new app (on IOS) with the SDK 7.69 version following the message in my google AdMob account "Some of your iOS apps require a GMA SDK update". The app update was accepted on the App Store, including the new apple appTrackingTransparency.
However, the ads (banner and reward ads) do not show, even though the configuration works perfectly with the test_IDs. Also, the ads still show just fine with my older app, which is running ads on GMA SDK 7.61.
The message still appears on top of my AdMob account, saying some of my apps require an update to SDK 7.64 or later.
Do I have to update my other app to the new SDK for the ads on my newer app to work? I don't think that makes sense tho because the apps should not be connected. I have performed the update about 3 days ago, do I just have to wait longer?
If anyone is stuck with the same problem or knows how to get the ads to show, I would really appreciate help. (I know this is not code question, but I don't know where to ask this)

A: I have still SDK 7.59 from outdated plugin. Recently I published first time Nativescript apps with admob plugin - https://github.com/EddyVerbruggen/nativescript-admob - which is outdated. These app were serving via Google AdMob interstitials for 3 days (very low traffic about 200 hits), then I have received automated template email from Google (Subject: Temporary ad serving limit placed on your AdMob account - "We are working hard... blah blah" - )


text:
Q: Read out an XML file and search for a specific word. Then put this word on a page. Code in PHP I have to read out an XML file and I have to search for 0GEW903KA. This code must then be on one page. I can't get it to read the XML file. With a .txt, I can already read the code, but it always outputs the entire line and not just the word. Can you help me please.
sheet.info
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" sheetInfoVersion="1.0" infoType="SheetInfo">
<uid>10000005-e59b-4723-a941-8d5942ff1673</uid>
<sheetType hasTemporaryMOP="false" regularMOPLocation="">ALS</sheetType>
<revision />
<timestamps>
    <created>2019-08-26 12:27:30</created>
    <lastWrite>2019-08-26 12:27:30</lastWrite>
    <lastAccess>2019-08-26 12:27:30</lastAccess>
</timestamps>
<sheetName />
    <idCode>0GEW903KA</idCode>
    <versionInfo>
        <comment>initial revision</comment>
        <date>2019-08-26 12:27:30</date>
        <increment>0</increment>
        <user>LA2TERMSERV/html_eng1</user>
        <version>1</version>
        <state>Undefined</state>
        <creationToolName>HTML_Editor</creationToolName>
        <creationToolVersion>2.6.0</creationToolVersion>
        <creationToolLibVersion>HTML 2.6.0</creationToolLibVersion>
    </versionInfo>
</configuration>

my current mess code
my attempt to read xml files
$z = new XMLReader;
$z->open('C:/xampp/htdocs/Flutter/Probe/sheet.xml');

$doc = new DOMDocument;

// move to the first <product /> node
while ($z->read() && $z->name !== 'idCode');

// now that we're at the right depth, hop to the next <product/> until the end of the tree
while ($z->name === 'idCode') {
    // either one should work
    //$node = new SimpleXMLElement($z->readOuterXML());
    $node = simplexml_import_dom($doc->importNode($z->expand(), true));

    // now you can use $node without going insane about parsing
    echo $node;
}

A: The code does not match the XML example (here is not "product" element) and the XML does not look like it would need XMLReader (large xml file).
So with just DOM + Xpath:
$document = new DOMDocument;
$document->loadXML($xmlString);
$xpath = new DOMXpath($document);

var_dump($xpath->evaluate('string(/configuration/idCode)'));

Output:
string(9) "0GEW903KA"

Xpath allows you to fetch nodes and values from DOM using expressions. The expression here is:

*

*Fetch the "configuration" document element node.../configuration

*... fetch the "idCode" child elements .../configuration/idCode

*... cast the first found node to a stringstring(/configuration/idCode)
Your example code misses the actual string value read and it is an endless loop because it does not move to the next node. Here is a fixed example with the different possibilities after you navigate to the "idCode"element node:
$reader = new XMLReader;
//$reader->open('C:/xampp/htdocs/Flutter/Probe/sheet.xml');
$reader->open('data:text/xml;base64,'.base64_encode($xmlString));

$document = new DOMDocument;
$xpath = new DOMXpath($document);

while ($reader->read() && $reader->localName !== 'idCode');

while ($reader->localName === 'idCode') {
    // expand idCode into prepared document
    $node = $reader->expand($document);
    // importing into SimpleXML is an additional step
    $idCode = simplexml_import_dom($node);

    var_dump(
      [
          // direct read without expand into DOM
          'XMLReader::readString' => $reader->readString(),
          // text content of the expanded node
          'DOMNode::$textContent' => $node->textContent,
          // xpath expression using expanded node as context
          'Xpath expression' => $xpath->evaluate('string(.)', $node),
          // cast the imported SimpleXMLElement instance
          'SimpleXMLElement' => (string)$idCode
      ]
    );

    // look for a sibling "idCode"
    $reader->next('idCode');
}

Output:
array(4) {
  ["XMLReader::readString"]=>
  string(9) "0GEW903KA"
  ["DOMNode::$textContent"]=>
  string(9) "0GEW903KA"
  ["Xpath expression"]=>
  string(9) "0GEW903KA"
  ["SimpleXMLElement"]=>
  string(9) "0GEW903KA"
}


text:
Q: SourceSansPro: tabular numbers in a beamer presentation I'm displaying some data with pgfplotstable in a beamer presentation for my history lesson.
The main font is SourceSansPro. I'm using it with \usepackage[semibold]{sourcesanspro} with default lining numbers for normal text.
But I'd like to use the tabular numbers in the tables. It appears I cannot change the math mode font to use SourceSansPro-TLF whitch is the variation with monospaced numbers. On the following example, it works only for string type columns.
\documentclass{beamer}
\usepackage{pgfplotstable}
\usepackage{sourcesanspro}
\usepackage[T1]{fontenc}
\begin{document}
Louis XIV was born in 1638.

\newcolumntype{i}{>{\fontfamily{SourceSansPro-TLF}\selectfont}r}
\pgfplotstabletypeset[%
fixed,
column type=i,
columns/A/.style={string type},
]{%
A      B      C
123121 121121 718121
456324 314123 192322
789425 141425 212422
101121 161121 223121
}%

He died in 1715.
\end{document}

The result is looking like this:

I'd like tabular numbers of SourceSansPro like in the first column, and thousand separators too, like in the math mode columns.

A: With unicode-math, you can \addfontfeature in text mode and select tabular figures in math mode.  Note that the sourcesanspro package defaults to using OTF on some engines!
\documentclass{beamer}
\usepackage{pgfplotstable}
\usepackage{unicode-math}
\usepackage{sourcesanspro}

\pgfplotsset{compat=1.16}

\setmathfont{Fira Math} % Or your preferred default.
\setmathfont[range=up]{SourceSansPro-Regular.otf}
\setmathfont[range=it]{SourceSansPro-RegularIt.otf}
\setmathfont[range=bfup]{SourceSansPro-Bold.otf}
\setmathfont[range=bfit]{SourceSansPro-BoldIt.otf}

\begin{document}
Louis XIV was born in 1638.

% For most fonts, you would select the font feature Numbers=Tabular (tnum).
% Source Sans Pro is unusual in that it does not support the tnum OpenType
% feature, but makes it the default.  Selecting it will still work, but
% turning the pnum feature off instead suppresses numerous spurious error
% messages.
\newcolumntype{i}{>{\addfontfeature{RawFeature = -pnum}}r}
\pgfplotstabletypeset[%
fixed,
column type=i,
columns/A/.style={string type},
]{%
A      B      C
123121 121121 718121
456324 314123 192322
789425 141425 212422
101121 161121 223121
}%

He died in 1715.
\end{document}

If you want to be able to switch between tabular and proportional numbers in math mode, what you want to do is set up a tabular math version.  Unfortunately, as of March 2019, the range= option of \setmathfont does not work properly with math versions.
To briefly repeat what I said in the comment, you would normally add the feature Numbers=Tabular, or tnum.  Source Sans Pro is unusual in that it does not support that feature, and you select tabular numbers by turning the pnum feature off.  (Numbers = Tabular still works, but gives you many spurious error messages.)


text:
Q: Time homogeneous Markov chain with random times A continuous time homogeneous Markov chain $X_t$ over a finite state space $\{ 1, \dots, n \}$ satisfies the property
$$P(X(s+t) = j \mid X(t) = i) = P(X(s) = j \mid X(0) = i).$$
If $S$ and $T$ are random times is it then true that
$$P(X(S+T) = j \mid X(T) = i) = P(X(S) = j \mid X(0) = i).$$
Edit: See the comments for a nice counterexample in the case $S$ deterministic and $T$ random but not stopping time (for discrete time Markov chains). Now what happens if we restrict $T$ to be a stopping time?


text:
Q: Angular scope deleted on splice When trying to remove an item from a list using splice, my entire list is destroyed? what could be cause it?
HTML:

          {{pane.title}}
          x

      +

JS
.controller('editCtrl', ['$scope', function($scope){

    $scope.panes = [];
    var ctrl  = this;
    /*===========Select===========*/
    $scope.select = function(pane) {
      angular.forEach($scope.panes, function(pane) {
        pane.selected = false;
      });
      pane.selected = true;
    };
    /*==========Add-Pane=========*/
    this.addPane = function(pane) {
      if ($scope.panes.length === 0) {
        $scope.select(pane);
      }
      $scope.panes.push(pane);
    };
    /*===========Create===========*/
    $scope.createPane = function() {
        var pane = {
            title: 'untitled',
            content: 'Scope:'+$scope.$id
        }
        ctrl.addPane(pane);
    };
    /*===========Close===========*/
    $scope.close = function(pane) {
        var idx = $scope.panes.indexOf(pane);
        //$scope.panes.splice(pane, 1);
        if (idx != -1) $scope.panes.splice(idx, 1);
    }
}]);

in particular I am looking at the close method, The rest work fine.

A: I think your list isn't destroyed by anything.
But the href="" on those  tags make the entire page to be refreshed when clicking the x icon.
Try removing those href like this:

          {{pane.title}}
          x

      +

Hope this helps.

A: You're already passing the index of the pane you want to remove when you send $index as the parameter to your close($index) function.
Thus your close function should look like this:
$scope.close = function(i){
    var removedPane = $scope.panes.splice(i,1);
    // do something here with the "removedPane" if you want to
};

This line
var idx = $scope.panes.indexOf(pane);

is trying to find the index of the index being sent to the function, thus idx is most likely undefined.
EDIT
You need to go back to calling close like this:
`ng-click="close($index)"`


text:
Q: DialogFlow from AsyncTask to RX Im looking for a way to manage a Observable over a Asynctask in the request from the API of DialogFlow (AI.API from Google https://dialogflow.com)
Right now the code is everything in Kotlin
The normal request is the next one
val task = object : AsyncTask<String, Void, AIResponse>() {
        private var aiError: AIError? = null

        override fun doInBackground(vararg params: String): AIResponse? {
            val request = AIRequest()
            val query = params[0]
            val event = params[1]

            if (!TextUtils.isEmpty(query))
                request.setQuery(query)
            if (!TextUtils.isEmpty(event))
                request.setEvent(AIEvent(event))
            val contextString = params[2]
            var requestExtras: RequestExtras? = null
            if (!TextUtils.isEmpty(contextString)) {
                val contexts = listOf(AIContext(contextString))
                requestExtras = RequestExtras(contexts, null)
            }

            try {
                return aiDataService?.request(request, requestExtras)
            } catch (e: AIServiceException) {
                aiError = AIError(e)
                return null
            }

        }

        override fun onPostExecute(response: AIResponse?) {
            if (response != null) {
                aiApiSuccess (response)
            } else {
                view.onError(R.string.natural_language_error)
            }
        }
    }
    task.execute(requestString, "", "")

I don't know if I doing wrong the request
Right now
return Observable.fromCallable<AIResponse> {
        val params = arrayOfNulls<String>(3)
        val request = AIRequest()
        val query = params[0]
        val event = params[1]

        if (!TextUtils.isEmpty(query))
            request.setQuery(requestString)
        if (!TextUtils.isEmpty(event))
            request.setEvent(AIEvent(event))
        val contextString = params[2]
        var requestExtras: RequestExtras? = null
        if (!TextUtils.isEmpty(contextString)) {
            val contexts = listOf(AIContext(contextString))
            requestExtras = RequestExtras(contexts, null)
        }

        try {
            aiDataService.request(request, requestExtras)
        } catch (e: AIServiceException) {
            Log.i("Error", "Error")
        }
            AIResponse()
        }
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())

It make the request but all the objects are null always
that doesn't happen with the Asynctask


text:
Q: Sql HierarchyId How do I get the last descendants? Using t-sql hierarchy Id how do I get all of the rows that have no children (that is the last decendants)?
Say my table is structured like this:
 Id,
 Name,
 HierarchyId

And has these rows:
1, Craig, /
2, Steve, /1/
3, John, /1/1/

4, Sam, /2/
5, Matt, /2/1/
6, Chris, /2/1/1/

What query would give me John and Chris?

A: Perhaps there are better ways but this seams to do the job.
declare @T table
(
  ID int,
  Name varchar(10),
  HID HierarchyID
)

insert into @T values
(1, 'Craig', '/'),
(2, 'Steve', '/1/'),
(3, 'John', '/1/1/'),
(4, 'Sam', '/2/'),
(5, 'Matt', '/2/1/'),
(6, 'Chris', '/2/1/1/')

select *
from @T
where HID.GetDescendant(null, null) not in (select HID
                                            from @T)

Result:
ID          Name       HID
----------- ---------- ---------------------
3           John       0x5AC0
6           Chris      0x6AD6

Update 2012-05-22
Query above will fail if node numbers is not in an unbroken sequence. Here is another version that should take care of that.
declare @T table
(
  ID int,
  Name varchar(10),
  HID HierarchyID
)

insert into @T values
(1, 'Craig', '/'),
(2, 'Steve', '/1/'),
(3, 'John', '/1/1/'),
(4, 'Sam', '/2/'),
(5, 'Matt', '/2/1/'),
(6, 'Chris', '/2/1/2/') -- HID for this row is changed compared to above query

select *
from @T
where HID not in (select HID.GetAncestor(1)
                  from @T
                  where HID.GetAncestor(1) is not null)

A: Since you only need leafs and you don't need to get them from a specific ancestor, a simple non-recursive query like this should do the job:
SELECT * FROM YOUR_TABLE PARENT
WHERE
    NOT EXISTS (
        SELECT * FROM YOUR_TABLE CHILD
        WHERE CHILD.HierarchyId = PARENT.Id
    )

In plain English: select every row without a child row.
This assumes your HierarchyId is a FOREIGN KEY towards the Id, not the whole "path" as presented in your example. If it isn't, this is probably the first thing you should fix in your database model.
--- EDIT ---
OK, here is the MS SQL Server-specific query that actually works:
SELECT * FROM YOUR_TABLE PARENT
WHERE
    NOT EXISTS (
        SELECT * FROM YOUR_TABLE CHILD
        WHERE
            CHILD.Id <> PARENT.Id
            AND CHILD.HierarchyId.IsDescendantOf(PARENT.HierarchyId) = 1
    )

Note that the IsDescendantOf considers any row a descendant of itself, so we also need the CHILD.Id <> PARENT.Id in the condition.

A: Hi I use this one and works perfectly for me.
CREATE TABLE [dbo].[Test]([Id] [hierarchyid] NOT NULL,  [Name] [nvarchar](50) NULL)
DECLARE @Parent AS HierarchyID = CAST('/2/1/' AS HierarchyID) -- Get Current Parent
DECLARE @Last AS HierarchyID
SELECT @Last = MAX(Id) FROM Test WHERE Id.GetAncestor(1) = @Parent -- Find Last Id for this Parent

INSERT INTO Test(Id,Name) VALUES(@Parent.GetDescendant(@Last, NULL),'Sydney') -- Insert after Last Id


text: Q: Es posible enviar credenciales como tokens desde angular de forma segura? Tengo una duda que quisiera que me puediera aclarar alguien con experiencia en Angular. Ante todo se que es un framework de frontend y se supone que toda la seguridad se debe dejar a cargo del backend, pero ando buscando opciones antes de sumergirme a hacer algo en el backend, ya que por los momentos solo se algo de frontend. Es posible enviar credenciales de acceso a una API desde Angular de forma segura sin que alguien pueda interceptarlos? estoy tratando de conectarme a una API de un Software CRM y en primera instancia no pude por que me bloquean la peticion a traves de CORS, me comnunique con ellos y me dijeron que las peticiones debian hacerse desde mi backend y sobre todo lo dicen es por la seguridad, entonces esta es mi gran pregunta, estuve buscando y no encontre algo que me permita en angular enviar credenciales de forma segura y por esa razon recurro a este medio para confirmar. Estaria agradecido con sus opiniones y sugerencias. gracias de antemano!


text:
Q: Google API connection fails after cloning from Git to another environment We are a team of two trying to build an android app that uses the Google Fit API. The problem we're running into is that only the computer that originally built the project is able to successfully run the app on a device and connected it to Google Fit.
We can get the app working fine on one computer, where it will prompt the user for permission and also the Google account to use and then it will successfully connect and pull the steps data.
Then we will create a git repository with this working project and clone it to another computer. But the problem is when we then try to run that project from the second environment the connection always fails. It always returns statusCode=CANCELED:
Google Play services connection failed. Cause: ConnectionResult{statusCode=CANCELED, resolution=null, message=null}
The device just says the result.getErrorMessage() is null which isn't really telling us much.
We're trying to figure out what part of checking this app in and trying to then pull and work on it on another machine causes this. Is there some kind of log in key or setting, which is not versioned for apps, that need to be transferred/set/configured?
We've built it and gotten it working on two different starting environments, but pulling it from any other computer just doesn't build into a working app. Any thoughts would be greatly appreciated.

A: Not sure why it was voted down but after some juggling of different phrases and looking up similar problems for other APIs we finally found our solution elsewhere so I'll leave it here.
We had to copy over the debug.keystore file from the originating computer to any other computer that wants to work on it. The file will be in the computer's user directory, somewhere around
C:\Users\Owner\.android, which isn't versioned with the project but necessary to authenticate with google apis apparently.


text: Q: How to stop Windows Update deleting the downloaded update files after failed installation When Windows Update failed to install an Insider Build, it freaking deleted the 3 gig file(s) that took so long to download. How do I stop this from happening again?


text:
Q: Why did these uniforms have that symbol? In episodes 2 and 3 of Andor,

 a flashback to Cassian Andor's youth shows a freighter crashing on his homeworld of Kenari. Some crew stagger out, with clearly visible Separatist/CIS insignia on their arms, and one of them is killed by blowguns:

 However, later, Maarva says that Cassian and the rest of his people won't be safe, since they just killed a "Republic officer".

Has this been officially addressed? Is this just a costuming error, the result of rewrites, or something else?

A: This is addressed by the starwars.com Episode Guide for Episode 3:

The flashbacks in this episode occur during the later years of the Republic, prior to the start of the Clone Wars, with Maarva and Clem worrying about an incoming Republic frigate interrupting their salvage operation. The dead crewers aboard the transport corsair wear uniforms with a symbol closely related to the eventual Separatist Alliance. Travel to Kenari will later be restricted by the Empire due to environmental disaster.
(source: https://www.starwars.com/series/andor/andor-season-1-episode-3-episode-guide, Trivia Gallery image 1, emphasis added)

The implication is that the faction that the crew belongs to will eventually become the Separatist Alliance/CIS, but hasn't yet seceded from the Republic.


text:
Q: fast infix search and count on a 40 million rows table postgresql I'm new to database administration, but I need to create a database view, while the db admin requires it to run in 5 mins or less. My database is PostgreSQL 9.1.1 on RedHat4.4 linux 64-bit. I'm unsure about the hardware specifications. One of the tables is 40million rows. From the table, I have a column of directory paths from which I must group by about 20 string patterns and count its occurrences. The string pattern requires infix search, as it could be somewhere in the middle or end of the path. The string pattern also has a priority, as in when %str1% then 'str1', when %str2% then 'str2', and both str1, str2, str3, etc can occur on the same path, i.e.
path
/usr/myblock/str1/str2
/usr/myblock/something/str2
/usr/myblock/str1/something/str3

What I did so far was to build a table out of CASE statements then join it back to the original table by LIKE, then SELECT id, pattern, count(pattern). The query runtime was terrible, taking 5mins to retrieve from 5.5K rows. My query looks like this:
WITH a AS (
SELECT CASE
WHEN path ~ '^/usr/myblock/(.*)str1(.*)' THEN 'str1'
WHEN path ~ '^/usr/myblock/(.*)str2$' THEN 'str2'
WHEN path ~ '^/usr/myblock/(.*)str3$' THEN 'str3'
.... --multiple other case conditions
WHEN path ~ '^/usr/myblock/' THEN 'others'
ELSE 'n/a'
END as flow
FROM mega_t WHERE left(path,13)='/usr/myblock/' limit 5)
SELECT id, a.flow, count(*) AS flow_count FROM a
JOIN mega_t ON path LIKE '%' || a.flow || '%'
WHERE (some_conditions) AND to_timestamp(test_runs.created_at::double precision)
> ('now'::text::date - '1 mon'::interval) --collect last 1 month's results only
GROUP BY id, a.flow;

My expected output for that simple case would be:
id | flow | flow_count
1  | str1 | 2
2  | str2 | 1

What is a better way to search for substrings like this and count occurrences? I can't use ts_stat, nor 'SELECT count(path) WHERE path LIKE %str1%' because of the if-else priority it needs. I read about creating trigram indexes, but I think that is overkill for my patterns. I hope this question is clear and useful. Another thing I should add is that the 40million rows table is updated frequently every few seconds or minutes while the view will be accessed every eight hours daily.


text:
Q: generics paraemeterised definition in inteface and implementation class rationale I'm unable to understand the concept behind the subtyping / interface implementation in relation to generics. Please explain.
I had the below structure:
`ICommand -> ProducerConsumerCommand`

IProducerConsumer -> SimpleProducerConsumer

My understanding is that any concrete class implementing IProducerConsumer must hold objects of type T which is the subclass of Number and ICommand interface.
IProducerConsumer<T extends Number,ICommand>
Now I want to write a implementation class SimpleProducerConsumer,
Now my intent is that I want to tell that SimpleProducerConsumer can hold any type parameters as defined in IProducerConsumer type parameters as below:
public class SimpleProducerConsumer<T,ICommand> implements Runnable,
        IProducerConsumer<T,ICommand> {

It dosen't work, it says as below:
SimpleProducerConsumer<T,ICommand> - ICommand complains (warning) that it is getting hidden.
IProducerConsumer<T,ICommand> - compiler errors in T saying as below

*Bound mismatch: The type T is not a valid substitute for the bounded parameter  of the type IProducerConsumer<T,ICommand>*
So, My first question is why can't I define the same way as interface in concrete class.
what is happening under the hoods.
if I can define
 public class test<ICommand> {}

why can't I do the same while implementing, confusing.
Now I define as below
public class SimpleProducerConsumer<T,ProducerConsumerCommand> implements Runnable,
        IProducerConsumer<T,ICommand> {

Still T in IProducerConsumer complains.
If I change as below:
public class SimpleProducerConsumer<T,ProducerConsumerCommand> implements Runnable,
        IProducerConsumer<T extends Number,ICommand> {

extends complains saying to remove the token.
Now i do like this as below:
public class SimpleProducerConsumer<T extends Number,ProducerConsumerCommand> implements Runnable,
        IProducerConsumer<T extends Number,ICommand> {

Still extends in IProducerConsumer dosen't work.
Now
public class SimpleProducerConsumer<T extends Number,ProducerConsumerCommand> implements Runnable,
        IProducerConsumer<T,ICommand> {

Yes now compiler is happy. Now I'm completely lost, what does above thing mean. SimpleProducerConsumer contains objects of type T which are subclass of Number and ProducerCommand object makes sense, but IProducerConsumer holds objects of Type T ??? what is this ?
What is the rationale behind this. Can someone please expalin.

A: You have two problems here:

*

*As it is declared now in IProducerConsumer<T extends Number,ICommand>, ICommand is not referencing your ICommand interface, but is instead just another generic type parameter declaration like T. Therefore you get the warning, that this generic type parameter is hiding your ICommand interface. You must declare your IProducerConsumer like so:
public interface IProducerConsumer<N extends Number, C extends ICommand>

*You get a Bound mismatch for T, because T does not necessarily extend Number, as required by the IProducerConsumer interface. To fix this, you must declare your SimpleProducerConsumer like so:
public class SimpleProducerConsumer<N extends Number, C extends ICommand> implements Runnable, IProducerConsumer<N,C>


text:
Q: Postfix not connecting to postgres pool with local ip address I have a postfix e-mail server that is contacting a database to query and verify e-mail addresses etc. It connects using pgbouncer to cycle through connections, however my problem was that the 'bouncer' is located on the database end so connections to and from the database are unencrypted and could potentially be a security issue.
I installed a local instance of pgbouncer on the mail server along with stunnel to at least give the two the chance to instigate an SSL connection between the two, however for some infurating reason, postfix, through sentimentality, perniciousness or just because it is plain stubborn refuses to comply with the switchover. Here is an example of one of the virtual alias lookup files located in the postfix directory:
# -------
# p-alias
# -------
hosts=10.0.0.123
port=6432
dbname=mail
user=user1
password=password
query=SELECT destination FROM v_alias WHERE source = '%s'

to test it, you can query a test e-mail address like so
console:~$ postmap -q test@testy.net pgsql:/etc/postfix/p-alias

and the result:
tester.mc_testy_tester@test_email_address.com

all fine and good, now to create a nearly identical config that points to the local pooler
console:~$ cp /etc/postfix/p-alias /etc/postfix/p-alias2
console:~$ vi /etc/postfix/p-alias2

change the ip to localhost:
# -------
# p-alias2
# -------
hosts=localhost
port=6432
dbname=mail
user=user1
password=password
query=SELECT destination FROM v_alias WHERE source = '%s'

pose it the same command:
console:~$ postmap -q test@testy.net pgsql:/etc/postfix/p-alias2

and it spits out the following error
postmap: warning: connect to pgsql server localhost: could not connect to server: Connection refused??
Is the server running on host "localhost" (::1) and accepting??
TCP/IP connections on port 5432??
could not connect to server: Connection refused??
Is the server running on host "localhost" (127.0.0.1) and accepting??
TCP/IP connections on port 5432??
postmap: fatal: table pgsql:/etc/postfix/p-alias2: query error: Operation now in progress

The two bouncers are all but identical, the pgbouncer.ini files are the same, as is the userlist.txt, the only noticable difference being their physical location and connection. Because I do not have psql client installed on the e-mail server (trying to install through linux distros tried to add far too many dependencies for my liking), I instead resorted to mimicking the queries with python to check that I hadn't missed anything silly:
import psycopg2

conn = psycopg2.connect( "host='10.0.0.123' port='6432' dbname=mail user='user1' password='password'" )
dbh = conn.cursor()
dbh.execute( "SELECT destination FROM v_alias WHERE source = 'test@testy.net'" )
dbh.fetchone()

# Result:
# ('tester.mc_testy_tester@test_email_address.com',)

conn.commit()
conn.close()

# -----------------------------------
# All is tickety boo, as it should be
# now let's repeat with the local ip
# -----------------------------------

conn = psycopg2.connect( "host='localhost' port='6432' dbname=mail user='user1' password='password'" )
dbh = conn.cursor()
dbh.execute( "SELECT destination FROM v_alias WHERE source = 'test@testy.net'" )
dbh.fetchone()

# Result:
# ('tester.mc_testy_tester@test_email_address.com',)

conn.commit()
conn.close()

both worked without a hitch, so as far as I can tell this is working and there should be no problems. Postfix clearly thinks otherwise.
So then I assumed that perhaps it was the stunnel that was the problem, here is the config:
client = yes
pid = /var/run/stunnel.pid
[pgbouncer]
        protocol = pgsql
        accept = 6433
        connect = 10.0.0.123:5432

I bypass the pgbouncer and query the local stunnel
# -------
# p-alias3
# -------
hosts=127.0.0.1
port=6433
dbname=mail
user=user1
password=password
query=SELECT destination FROM v_alias WHERE source = '%s'

postmap refuses to comply
console:~$ postmap -q test@testy.net pgsql:/etc/postfix/p-alias3
postmap: warning: connect to pgsql server 127.0.0.1: could not connect to server: Connection refused??
Is the server running on host "127.0.0.1" and accepting??
TCP/IP connections on port 5432??
postmap: fatal: table pgsql:/etc/postfix/p-alias3: query error: Operation now in progress

no luck.
For the sake of completeness here is a fair chunk of the pgbouncer.ini
[databases]

; ...
;mail = host=localhost port=6433 dbname=mail
mail = host=10.0.0.123 port=5432 dbname=mail

[pgbouncer]

listen_addr = *
listen_port = 6432

; ...

auth_type = md5

auth_file = /etc/pgbouncer/userlist.txt

pool_mode = transaction

server_reset_query = DISCARD ALL

server_check_query = SELECT 1

server_check_delay = 30

; ...

I try pgbouncer with stunnel, or without and straight to the database and postfix refuses to accept there is a localhost, it works with python, and fails with postmap.
Clearly it just doesn't like localhost.
So, Postfix, what do you want from me?!!!
(Postfix is version 2.11.0)

A: After some discussion on irc, it was pointed out that Postfix does not appear to be honoring the port= setting for local connections - the error message was clearly attempting to speak to port 5432, so changing the pgbouncer listen port to 5432 fixed the problem.
Thanks -Myon!
(it's always so simple once you know the answer...)

A: Based on the manpage here:
http://www.postfix.org/pgsql_table.5.html
The optional port needs to be specified in the hosts fields with the hostname:port syntax:

  hosts  The hosts that Postfix will try to connect to  and  query  from.
          Specify unix: for UNIX-domain sockets, inet: for TCP connections
          (default).  Example:
              hosts = host1.some.domain host2.some.domain:port
              hosts = unix:/file/name

An independant port field does not seem to exist.


text:
Q: Pixi.js v5 - apply alpha to displacement map I'm scaling a displacement map on click but would like that map to fade away once it reaches almost full scale. The idea is that the filter should be non-existent after a couple of seconds.
const app = new PIXI.Application({
  view: document.querySelector("#canvas"),
  width: 512,
  height: 512
});

const logo   = PIXI.Sprite.fromImage("https://unsplash.it/600");
const displacement = PIXI.Sprite.fromImage("https://images.unsplash.com/photo-1541701494587-cb58502866ab?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80");
const filter = new PIXI.filters.DisplacementFilter(displacement);

logo.anchor.set(0.5);
logo.position.set(256);
logo.interactive = true;

displacement.anchor.set(0.5);
displacement.position.set(256);
displacement.scale.set(0.05)
displacement.alpha = 1

app.stage.filterArea = app.screen;
app.stage.filters = [filter];
app.stage.addChild(logo, displacement);

app.ticker.add(function() {
  displacement.scale.x += 0.05
  displacement.scale.y += 0.05
  if (displacement.scale.x > 10) app.ticker.stop()
});

logo.on('mousedown', function() {
  displacement.scale.set(0.05)
  app.ticker.start()
});

Here's what I have so far:
https://codepen.io/mariojankovic/pen/pojjNae?editors=0111

A: I've only just started looking at Pixi but I think you want to use the scale property of the displacement filter.  This value says how far to shift. Reducing this value to 0 will lessen its effect to none.
https://pixijs.download/dev/docs/PIXI.filters.DisplacementFilter.html
https://pixijs.download/dev/docs/PIXI.filters.DisplacementFilter.html#scale

The way it works is it uses the values of the displacement map to look
  up the correct pixels to output. This means it's not technically
  moving the original. Instead, it's starting at the output and asking
  "which pixel from the original goes here". For example, if a
  displacement map pixel has red = 1 and the filter scale is 20, this
  filter will output the pixel approximately 20 pixels to the right of
  the original.

https://codepen.io/PAEz/pen/BaoREwv
const app = new PIXI.Application({
  view: document.querySelector("#canvas"),
  width: 512,
  height: 512
});

const logo = PIXI.Sprite.fromImage("https://unsplash.it/600");
const displacement = PIXI.Sprite.fromImage(
  "https://images.unsplash.com/photo-1541701494587-cb58502866ab?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80"
);
const filter = new PIXI.filters.DisplacementFilter(displacement);

logo.anchor.set(0.5);
logo.position.set(256);
logo.interactive = true;

displacement.anchor.set(0.5);
displacement.position.set(256);
displacement.scale.set(0.0);
displacement.alpha = 1;

app.stage.filterArea = app.screen;
app.stage.filters = [filter];
app.stage.addChild(logo, displacement);

const displacementScaleFrom = 0.05;
const displacementScaleTo = 10 ;
const displacementStep = 0.05;

const filterScaleFrom = 20;// the default value for the filter is 20
const filterStep = filterScaleFrom / ((displacementScaleTo-displacementScaleFrom) / displacementStep);

app.ticker.add(function () {
  displacement.scale.x += displacementStep;
  displacement.scale.y += displacementStep;
  filter.scale.x -= filterStep;
  filter.scale.y -= filterStep;
  if (displacement.scale.x >= displacementScaleTo) {
    app.ticker.stop();
    filter.scale.x = 0;
    filter.scale.y = 0;
  }
});

logo.on("mousedown", function () {
  displacement.scale.set(displacementScaleFrom);
  filter.scale.x = filterScaleFrom;
  filter.scale.y = filterScaleFrom;
  app.ticker.start();
});


text:
Q: Show that the torsion of a connection is an element in $\Omega^2(M,TM)$ Consider a connection $\nabla$ on the tangent bundle $TM$ some manifold $M$.
For a such a conneciton the torsion is defined as $T(X,Y)=\nabla_X Y -\nabla_Y X -[X,Y]$.
I want to show that $T\in \Omega^2(M,TM)$.
That is to show that $T$ is a $2$ form that takes values in the tangent bundle.
It's clearly anti-symmetric in $X$ and $Y$. We also must show that it is multi linear but this is also clear.
Individually $\nabla_X Y$,$\nabla_Y X$, and $[X,Y]$ take values in $TM$ but why does there sum also take values? Is this because, at any point $p$ in $M$ $\nabla_X Y$,$\nabla_Y X$, and $[X,Y]$ will all take values in $T_pM$ which is a vector space?

A: Yes, that's right. But the crucial thing to show you have a well-defined tensor field is that it's multilinear not over the scalars but over the $C^\infty$ functions. That's why the Lie bracket term has to be there.


text:
Q: How do you solve speed questions while only knowing the time?
David's father picks him up from school every evening at 6 pm. One day, David was dismissed early at 5 pm. He walked home taking the same route that his father usually drives. When he met his father along the way, he boarded the car and returned home 50 minutes earlier than usual. Given that his father drove at a constant speed and planned to reach the school at 6 pm sharp, how long, in minutes, had he walked before he as picked up by his father?

In this question, we are only given the time which is 6 pm, 5 pm and 50 minutes earlier. From here, I have completely no idea on how to continue doing this. Please help.
This question is from the 2004 Singapore Mathematical Olympiad for Primary Schools

A: Let:

*

*$X$ be the time duration of a one-way trip to school by car

*$Y$ be the time duration of the one-way trip by car before interception

*$Z$ be the time duration David has walked before interception (what we are asked to solve for)

So by these definitions, David's Dad always leaves at 6h00-X and he and David normally arrive at home at 6h00+X. Now, the new time of arrival is: $$t_{a}= 6h00-X+2Y =6h00+X-50 \text{ min}$$ From which we cannot explicitly tell what $X$ and $Y$ are, but we infer the difference:
$$X-Y = 25\text{ min}$$
Now the time of interception is:
$$t_{i}= 5h00 + Z =6h00-X+ Y$$
From which we solve : $Z= 35 \text{ min}$

A: David started walking at $5$ pm.
By walking he saves $50$ minutes of journey by car.
Lets say that David and the car intercepted at point $X$ somewhere between $H$ and $S$. Now distance from $X$ to $S$ and then again to $X$ (kind of a $2$-way trip) was saved. This is equivalent to $50$ minutes of car time. A one-way trip from $S$ to $X$ is $25$ minutes of car time. Therefore the car travelled for $60-25=35$ minutes and David walked for $35$ minutes.


text:
Q: Solaris 11 randomly crashed I have a Solaris 11 machine that randomly crashed this morning. After physically restarting the machine, I noticed that all of the drives were marked with a Sense Key: Soft_Error both in dmesg and in /var/adm/messages.
Since all the drives on the machine were tagged with the same Soft Error, does this mean that the HBA is faulty? Anyone have any ideas/suggestions?
root@solaris-machine:/var/log# iostat -E
sd0       Soft Errors: 1 Hard Errors: 0 Transport Errors: 0
Vendor: ATA      Product:       Revision: SN02 Serial No:
Size: 500.11GB <500107862016 bytes>
Media Error: 0 Device Not Ready: 0 No Device: 0 Recoverable: 1
Illegal Request: 12 Predictive Failure Analysis: 0
sd2       Soft Errors: 1 Hard Errors: 0 Transport Errors: 0
Vendor: ATA      Product:      Revision: 0004 Serial No:
Size: 3000.59GB <3000592982016 bytes>
Media Error: 0 Device Not Ready: 0 No Device: 0 Recoverable: 1
Illegal Request: 0 Predictive Failure Analysis: 0
sd4       Soft Errors: 1 Hard Errors: 0 Transport Errors: 0
Vendor: ATA      Product:      Revision: 0004 Serial No:
Size: 3000.59GB <3000592982016 bytes>
Media Error: 0 Device Not Ready: 0 No Device: 0 Recoverable: 1
Illegal Request: 0 Predictive Failure Analysis: 0
sd5       Soft Errors: 1 Hard Errors: 0 Transport Errors: 0
Vendor: ATA      Product:      Revision: 0004 Serial No:
Size: 3000.59GB <3000592982016 bytes>
Media Error: 0 Device Not Ready: 0 No Device: 0 Recoverable: 1
Illegal Request: 0 Predictive Failure Analysis: 0

Jan 23 10:45:02 solaris-machine scsi: [ID 107833 kern.warning] WARNING: /scsi_vhci/disk@g5000c5004dfae642 (sd4):
Jan 23 10:45:02 solaris-machine      Error for Command: <undecoded cmd 0xa1>    Error Level: Recovered
Jan 23 10:45:02 solaris-machine scsi: [ID 107833 kern.notice]        Requested Block: 0                         Error Block: 0
Jan 23 10:45:02 solaris-machine scsi: [ID 107833 kern.notice]        Vendor: ATA                                Serial Number:
Jan 23 10:45:02 solaris-machine scsi: [ID 107833 kern.notice]        Sense Key: Soft_Error
Jan 23 10:45:04 solaris-machine scsi: [ID 107833 kern.warning] WARNING: /scsi_vhci/disk@g5000c5004dfc8db2 (sd2):
Jan 23 10:45:04 solaris-machine      Error for Command: <undecoded cmd 0xa1>    Error Level: Recovered
Jan 23 10:45:04 solaris-machine scsi: [ID 107833 kern.notice]        Requested Block: 0                         Error Block: 0
Jan 23 10:45:04 solaris-machine scsi: [ID 107833 kern.notice]        Vendor: ATA                                Serial Number:
Jan 23 10:45:04 solaris-machine scsi: [ID 107833 kern.notice]        Sense Key: Soft_Error
Jan 23 10:45:04 solaris-machine scsi: [ID 107833 kern.notice]        ASC: 0x0 (<vendor unique code 0x0>), ASCQ: 0x1d, FRU: 0x0
Jan 23 10:45:04 solaris-machine scsi: [ID 107833 kern.warning] WARNING: /scsi_vhci/disk@g5000c5004dfd4ce3 (sd5):
Jan 23 10:45:04 solaris-machine      Error for Command: <undecoded cmd 0xa1>    Error Level: Recovered
Jan 23 10:45:04 solaris-machine scsi: [ID 107833 kern.notice]        Requested Block: 0                         Error Block: 0
Jan 23 10:45:04 solaris-machine scsi: [ID 107833 kern.notice]        Vendor: ATA                                Serial Number:
Jan 23 10:45:04 solaris-machine scsi: [ID 107833 kern.notice]        Sense Key: Soft_Error
Jan 23 10:45:04 solaris-machine scsi: [ID 107833 kern.notice]        ASC: 0x0 (<vendor unique code 0x0>), ASCQ: 0x1d, FRU: 0x0
Jan 23 10:45:07 solaris-machine scsi: [ID 107833 kern.warning] WARNING: /pci@0,0/pci15d9,664@1f,2/disk@0,0 (sd0):
Jan 23 10:45:07 solaris-machine      Error for Command: <undecoded cmd 0xa1>    Error Level: Recovered
Jan 23 10:45:07 solaris-machine scsi: [ID 107833 kern.notice]        Requested Block: 0                         Error Block: 0
Jan 23 10:45:07 solaris-machine scsi: [ID 107833 kern.notice]        Vendor: ATA                                Serial Number:
Jan 23 10:45:07 solaris-machine scsi: [ID 107833 kern.notice]        Sense Key: Soft_Error
Jan 23 10:45:07 solaris-machine scsi: [ID 107833 kern.notice]        ASC: 0x0 (no additional sense info), ASCQ: 0x0, FRU: 0x0

A: Where I look when a Solaris 11 server crashes:

*

*dmesg | less

*

*have a careful read of this, there are clues here, or if the dmesg buffer has cycled, look at the /var/adm/messages log in play at the time of the crash.

*fmadm faulty

*

*this will show any devices the fault manager has deemed faulty, including disks and hbas, and NIC, etc.

*fmdump -eV

*

*this will show you any errors from the system, recognized by the fault management system, regardless of whether they resulted in a piece of hardware being marked faulty.

*if the disks in question are part of a zpool, then run zpool scrub on that pool, and then look at zpool status and see if you're getting checksum errors that could indicate a problem with the HBA or disks.

*make sure that the dump config is set to capture crash dumps, by running dumpadm.  the output of this should show you that savecore is enabled, and crashes will be left in /var/crash.  If your system left a crashdump in /var/crash from this morning's crash, then you can look at the pstack of the crash, and possibly get some indicators as to root cause.

Those steps should get you closer to finding out why your system crashed.

A: [No Solaris expert anymore, but know my way around hardware with Linux]
If it was one drive, I'd suspect that one is failing. But all of them is extra fishy. Trouble with the controller perhaps?
Most crashes I've seen recently were due to CPU overheating (bad fan, dirt, clogged air circulation), close second hangs due to graphics drivers (particularly nVidia). Some trouble spots in times past were brownouts, or even micro blackouts.

A: Small numbers of soft errors can be safely ignored.  Solaris is just pedantic about recording and reporting everything that other OS'es don't even notice.
Monitor the error count (It gets reset on reboot) and check for patterns.
Also be aware that soft errors on one device have a tendency to cause the count on other drives on the same bus to also increment.  You will also have a clear winner though, one drive with many times more errors than the others.
I've never found drive error counts (soft or hard errors) to be indicative of HBA failures, but that may just be me.  HBA failures typically show up as other messages, eg mpt errors.


text:
Q: How to make a line of code execute only after the time interval of the animation block? I have put a line of code inside the UIAnimation block. And the next line comes after the animation block. I want the second line only to be executed after the time interval of the first animation block.
Eg:-
[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:0.5];
//First line comes here
 [self.view addSubview:subView];
 [UIView commitAnimations];

Then the 2nd line
[subView2 removeFromSuperView];

I want the 2nd line only to be executed after the 0.5 second interval of the animation action. Is it possible to do like this?

A: You can set a delegate for animation and remove subview in its method:
...
[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:0.5];

[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
[UIView setAnimationDelegate:self];

//First line comes here
[self.view addSubview:subView];
[UIView commitAnimations];
...

Then in delegate's animation did stop handler remove your 2nd subview:
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context{
    if ([finished boolValue])
        [subView2 removeFromSuperview];
}


text:
Q: Scala equivalent to Python generators? Is it possible to implement in Scala something equivalent to the Python yield statement where it remembers the local state of the function where it is used and "yields" the next value each time it is called?
I wanted to have something like this to convert a recursive function into an iterator. Sort of like this:
# this is python
def foo(i):
  yield i
  if i > 0:
    for j in foo(i - 1):
      yield j

for i in foo(5):
  print i

Except, foo may be more complex and recurs through some acyclic object graph.
Additional Edit:
Let me add a more complex example (but still simple):
I can write a simple recursive function printing things as it goes along:
// this is Scala
def printClass(clazz:Class[_], indent:String=""): Unit = {
  clazz match {
    case null =>
    case _ =>
      println(indent + clazz)
      printClass(clazz.getSuperclass, indent + "  ")
      for (c <- clazz.getInterfaces) {
        printClass(c, indent + "  ")
      }
  }
}

Ideally I would like to have a library that allows me to easily change a few statements and have it work as an Iterator:
// this is not Scala
def yieldClass(clazz:Class[_]): Iterator[Class[_]] = {
  clazz match {
    case null =>
    case _ =>
      sudoYield clazz
      for (c <- yieldClass(clazz.getSuperclass)) sudoYield c
      for (c <- clazz.getInterfaces; d <- yieldClasss(c)) sudoYield d
  }
}

It does seem continuations allow to do that, but I just don't understand the shift/reset concept. Will continuation eventually make it into the main compiler and would it be possible to extract out the complexity in a library?
Edit 2:
check Rich's answer in that other thread.

A: To do this in a general way, I think you need the continuations plugin.
A naive implementation (freehand, not compiled/checked):
def iterator = new {
  private[this] var done = false

  // Define your yielding state here
  // This generator yields: 3, 13, 0, 1, 3, 6, 26, 27
  private[this] var state: Unit=>Int = reset {
    var x = 3
    giveItUp(x)
    x += 10
    giveItUp(x)
    x = 0
    giveItUp(x)
    List(1,2,3).foreach { i => x += i; giveItUp(x) }
    x += 20
    giveItUp(x)
    x += 1
    done = true
    x
  }

  // Well, "yield" is a keyword, so how about giveItUp?
  private[this] def giveItUp(i: Int) = shift { k: (Unit=>Int) =>
    state = k
    i
  }

  def hasNext = !done
  def next = state()
}

What is happening is that any call to shift captures the control flow from where it is called to the end of the reset block that it is called in.  This is passed as the k argument into the shift function.
So, in the example above, each giveItUp(x) returns the value of x (up to that point) and saves the rest of the computation in the state variable.  It is driven from outside by the hasNext and next methods.
Go gentle, this is obviously a terrible way to implement this.  But it best I could do late at night without a compiler handy.

A: Scala's for-loop of the form for (e <- Producer) f(e) translates into a foreach call, and not directly into iterator / next.
In the foreach we don't need to linearize objects' creations and have them in one place, as it would be needed for iterator's next. The consumer-function f can be inserted multiple times, exactly where it is needed (i.e. where an object is created).
This makes implementation of use cases for generators simple and efficient with Traversable / foreach in Scala.

The initial Foo-example:
case class Countdown(start: Int) extends Traversable[Int] {
    def foreach[U](f: Int => U) {
        var j = start
        while (j >= 0) {f(j); j -= 1}
    }
}

for (i <- Countdown(5))  println(i)
// or equivalent:
Countdown(5) foreach println

The initial printClass-example:
  // v1 (without indentation)

  case class ClassStructure(c: Class[_]) {
    def foreach[U](f: Class[_] => U) {
      if (c eq null) return
      f(c)
      ClassStructure(c.getSuperclass) foreach f
      c.getInterfaces foreach (ClassStructure(_) foreach f)
    }
  }

  for (c <- ClassStructure(<foo/>.getClass)) println(c)
  // or equivalent:
  ClassStructure(<foo/>.getClass) foreach println

Or with indentation:
  // v2 (with indentation)

  case class ClassWithIndent(c: Class[_], indent: String = "") {
    override def toString = indent + c
  }
  implicit def Class2WithIndent(c: Class[_]) = ClassWithIndent(c)

  case class ClassStructure(cwi: ClassWithIndent) {
    def foreach[U](f: ClassWithIndent => U) {
      if (cwi.c eq null) return
      f(cwi)
      ClassStructure(ClassWithIndent(cwi.c.getSuperclass, cwi.indent + "  ")) foreach f
      cwi.c.getInterfaces foreach (i => ClassStructure(ClassWithIndent(i, cwi.indent + "  ")) foreach f)
    }
  }

  for (c <- ClassStructure(<foo/>.getClass)) println(c)
  // or equivalent:
  ClassStructure(<foo/>.getClass) foreach println

Output:
class scala.xml.Elem
  class scala.xml.Node
    class scala.xml.NodeSeq
      class java.lang.Object
      interface scala.collection.immutable.Seq
        interface scala.collection.immutable.Iterable
          interface scala.collection.immutable.Traversable
            interface scala.collection.Traversable
              interface scala.collection.TraversableLike
                interface scala.collection.generic.HasNewBuilder
                interface scala.collection.generic.FilterMonadic
                interface scala.collection.TraversableOnce
                  interface scala.ScalaObject
                interface scala.ScalaObject
              interface scala.collection.generic.GenericTraversableTemplate
                interface scala.collection.generic.HasNewBuilder
                interface scala.ScalaObject
              interface scala.ScalaObject
            interface scala.collection.generic.GenericTraversableTemplate
              interface scala.collection.generic.HasNewBuilder
              interface scala.ScalaObject
            interface scala.collection.TraversableLike
              interface scala.collection.generic.HasNewBuilder
              interface scala.collection.generic.FilterMonadic
              interface scala.collection.TraversableOnce
                interface scala.ScalaObject
              interface scala.ScalaObject
            interface scala.Immutable
            interface scala.ScalaObject
          interface scala.collection.Iterable
            interface scala.collection.Traversable
              interface scala.collection.TraversableLike
                interface scala.collection.generic.HasNewBuilder
                interface scala.collection.generic.FilterMonadic
                interface scala.collection.TraversableOnce
                  interface scala.ScalaObject
                interface scala.ScalaObject
              interface scala.collection.generic.GenericTraversableTemplate
                interface scala.collection.generic.HasNewBuilder
                interface scala.ScalaObject
              interface scala.ScalaObject
            interface scala.collection.generic.GenericTraversableTemplate
              interface scala.collection.generic.HasNewBuilder
              interface scala.ScalaObject
            interface scala.collection.IterableLike
              interface scala.Equals
              interface scala.collection.TraversableLike
                interface scala.collection.generic.HasNewBuilder
                interface scala.collection.generic.FilterMonadic
                interface scala.collection.TraversableOnce
                  interface scala.ScalaObject
                interface scala.ScalaObject
              interface scala.ScalaObject
            interface scala.ScalaObject
          interface scala.collection.generic.GenericTraversableTemplate
            interface scala.collection.generic.HasNewBuilder
            interface scala.ScalaObject
          interface scala.collection.IterableLike
            interface scala.Equals
            interface scala.collection.TraversableLike
              interface scala.collection.generic.HasNewBuilder
              interface scala.collection.generic.FilterMonadic
              interface scala.collection.TraversableOnce
                interface scala.ScalaObject
              interface scala.ScalaObject
            interface scala.ScalaObject
          interface scala.ScalaObject
        interface scala.collection.Seq
          interface scala.PartialFunction
            interface scala.Function1
              interface scala.ScalaObject
            interface scala.ScalaObject
          interface scala.collection.Iterable
            interface scala.collection.Traversable
              interface scala.collection.TraversableLike
                interface scala.collection.generic.HasNewBuilder
                interface scala.collection.generic.FilterMonadic
                interface scala.collection.TraversableOnce
                  interface scala.ScalaObject
                interface scala.ScalaObject
              interface scala.collection.generic.GenericTraversableTemplate
                interface scala.collection.generic.HasNewBuilder
                interface scala.ScalaObject
              interface scala.ScalaObject
            interface scala.collection.generic.GenericTraversableTemplate
              interface scala.collection.generic.HasNewBuilder
              interface scala.ScalaObject
            interface scala.collection.IterableLike
              interface scala.Equals
              interface scala.collection.TraversableLike
                interface scala.collection.generic.HasNewBuilder
                interface scala.collection.generic.FilterMonadic
                interface scala.collection.TraversableOnce
                  interface scala.ScalaObject
                interface scala.ScalaObject
              interface scala.ScalaObject
            interface scala.ScalaObject
          interface scala.collection.generic.GenericTraversableTemplate
            interface scala.collection.generic.HasNewBuilder
            interface scala.ScalaObject
          interface scala.collection.SeqLike
            interface scala.collection.IterableLike
              interface scala.Equals
              interface scala.collection.TraversableLike
                interface scala.collection.generic.HasNewBuilder
                interface scala.collection.generic.FilterMonadic
                interface scala.collection.TraversableOnce
                  interface scala.ScalaObject
                interface scala.ScalaObject
              interface scala.ScalaObject
            interface scala.ScalaObject
          interface scala.ScalaObject
        interface scala.collection.generic.GenericTraversableTemplate
          interface scala.collection.generic.HasNewBuilder
          interface scala.ScalaObject
        interface scala.collection.SeqLike
          interface scala.collection.IterableLike
            interface scala.Equals
            interface scala.collection.TraversableLike
              interface scala.collection.generic.HasNewBuilder
              interface scala.collection.generic.FilterMonadic
              interface scala.collection.TraversableOnce
                interface scala.ScalaObject
              interface scala.ScalaObject
            interface scala.ScalaObject
          interface scala.ScalaObject
        interface scala.ScalaObject
      interface scala.collection.SeqLike
        interface scala.collection.IterableLike
          interface scala.Equals
          interface scala.collection.TraversableLike
            interface scala.collection.generic.HasNewBuilder
            interface scala.collection.generic.FilterMonadic
            interface scala.collection.TraversableOnce
              interface scala.ScalaObject
            interface scala.ScalaObject
          interface scala.ScalaObject
        interface scala.ScalaObject
      interface scala.xml.Equality
        interface scala.Equals
        interface scala.ScalaObject
      interface scala.ScalaObject
    interface scala.ScalaObject
  interface scala.ScalaObject
  interface java.io.Serializable

A: While Python generators are cool, trying to duplicate them really isn't the best way to go about in Scala. For instance, the following code does the equivalent job to what you want:
def classStream(clazz: Class[_]): Stream[Class[_]] = clazz match {
  case null => Stream.empty
  case _ => (
    clazz
    #:: classStream(clazz.getSuperclass)
    #::: clazz.getInterfaces.toStream.flatMap(classStream)
    #::: Stream.empty
  )
}

In it the stream is generated lazily, so it won't process any of the elements until asked for, which you can verify by running this:
def classStream(clazz: Class[_]): Stream[Class[_]] = clazz match {
  case null => Stream.empty
  case _ => (
    clazz
    #:: { println(clazz.toString+": super"); classStream(clazz.getSuperclass) }
    #::: { println(clazz.toString+": interfaces"); clazz.getInterfaces.toStream.flatMap(classStream) }
    #::: Stream.empty
  )
}

The result can be converted into an Iterator simply by calling .iterator on the resulting Stream:
def classIterator(clazz: Class[_]): Iterator[Class[_]] = classStream(clazz).iterator

The foo definition, using Stream, would be rendered thus:
scala> def foo(i: Int): Stream[Int] = i #:: (if (i > 0) foo(i - 1) else Stream.empty)
foo: (i: Int)Stream[Int]

scala> foo(5) foreach println
5
4
3
2
1
0

Another alternative would be concatenating the various iterators, taking care to not pre-compute them. Here's an example, also with debugging messages to help trace the execution:
def yieldClass(clazz: Class[_]): Iterator[Class[_]] = clazz match {
  case null => println("empty"); Iterator.empty
  case _ =>
    def thisIterator = { println("self of "+clazz); Iterator(clazz) }
    def superIterator = { println("super of "+clazz); yieldClass(clazz.getSuperclass) }
    def interfacesIterator = { println("interfaces of "+clazz); clazz.getInterfaces.iterator flatMap yieldClass }
    thisIterator ++ superIterator ++ interfacesIterator
}

This is pretty close to your code. Instead of sudoYield, I have definitions, and then I just concatenate them as I wish.
So, while this is a non-answer, I just think you are barking up the wrong tree here. Trying to write Python in Scala is bound to be unproductive. Work harder at the Scala idioms that accomplish the same goals.

A: Another continuations plugin based solution, this time with a more or less encapsulated Generator type,
import scala.continuations._
import scala.continuations.ControlContext._

object Test {

  def loopWhile(cond: =>Boolean)(body: =>(Unit @suspendable)): Unit @suspendable = {
    if (cond) {
      body
      loopWhile(cond)(body)
    } else ()
  }

  abstract class Generator[T] {
    var producerCont : (Unit => Unit) = null
    var consumerCont : (T => Unit) = null

    protected def body : Unit @suspendable

    reset {
      body
    }

    def generate(t : T) : Unit @suspendable =
      shift {
        (k : Unit => Unit) => {
          producerCont = k
          if (consumerCont != null)
            consumerCont(t)
        }
      }

    def next : T @suspendable =
      shift {
        (k : T => Unit) => {
          consumerCont = k
          if (producerCont != null)
            producerCont()
        }
      }
  }

  def main(args: Array[String]) {
    val g = new Generator[Int] {
      def body = {
        var i = 0
        loopWhile(i < 10) {
          generate(i)
          i += 1
        }
      }
    }

    reset {
      loopWhile(true) {
        println("Generated: "+g.next)
      }
    }
  }
}

A: Dsl.scala is what you are looking for.
Suppose you want to create a random number generator. The generated numbers should be stored in a lazily evaluated infinite stream, which can be built with the help of our built-in domain-specific keyword Yield.
import com.thoughtworks.dsl.keys.Yield
def xorshiftRandomGenerator(seed: Int): Stream[Int] = {
  val tmp1 = seed ^ (seed << 13)
  val tmp2 = tmp1 ^ (tmp1 >>> 17)
  val tmp3 = tmp2 ^ (tmp2 << 5)
  !Yield(tmp3)
  xorshiftRandomGenerator(tmp3)
}

Other examples can be found in the Scaladoc.


text:
Q: How to use os.time () to know current time in script? Basically, How can I use os.time() and os.difftime() or possibly os.clock() to find out my current time that it has been running at a certain part of the script.
So at 10 minutes I am trying to run a command and at 20 mins I am trying to run another command all the way up to 40 mins in 10 min intervals and then I'm trying to reset back to 0 and restart.
Any ideas?

A: What is your problem? It's all described in the Lua reference manual.
http://www.lua.org/manual/5.3/manual.html#pdf-os.time
os.time([table]), if called without an argument returns the current time.
os.difftime(t2, t1) returns the time difference t2-t1 in seconds.
So simply store your start time and then frequently check if the time difference is >= 600. then reset your start time and do the same again.
You can also use os.clock()
In any case you have to stop what you are doing from time to time and check if the time has come to run your function. So you have to split your sequential code into interruptable bits. If you don't do anything between those 10 minute marks you can simply check the time in a simple loop.
There are plenty of Lua libraries (e.g. wxLua) that provide convenient timer and timer event functions. If you don't want to implement it yourself.

A: You could have your script store initial time use that value later to find time since script started, eg:
start_time = os.time()

function sleep(n)  -- seconds
  local t0 = os.clock()
  while os.clock() - t0 <= n do end
end

-- Write script logic here...
-- ...
sleep(5)
-- ...
-- You can later find how much time passed since beginning of execution by calling os.time() again
print(os.time()-start_time.." seconds since start of script execution...")