问:
Can`t round the corners of the button
I wrote the code below, but my ImageButton still has sharp corners. Maybe I don`t know something, if you know that, please, tell me.

My View

```<code><ImageButton
    android:id="@+id/bottle"
    android:src="@drawable/bottle"
    android:background="@drawable/rouncorners"
    android:layout_width="100dp"
    android:layout_height="100dp"/>
</code>```

Drawable xml file :

```<code><?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="10dp"/>
</shape>
</code>```

答:
I had a similar issue (it was with a Button), I had created a drawable shape with corners, stroke, solid etc but although I could see the button correctly displayed in the Preview, I wouldn't see it in the actual app. I haven't figured out why, but the solution was to set the drawable as the background of the button programmatically like so
 someButton.setBackgroundResource(R.drawable.button_rounded_corners);

Title: Can`t round the corners of the button

Body:
I wrote the code below, but my ImageButton still has sharp corners. Maybe I don`t know something, if you know that, please, tell me.

My View

<ImageButton
    android:id="@+id/bottle"
    android:src="@drawable/bottle"
    android:background="@drawable/rouncorners"
    android:layout_width="100dp"
    android:layout_height="100dp"/>

Drawable xml file :

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="10dp"/>
</shape>


来源: stackexchange

问:
How do I specify local .gem files in my Gemfile?
I have a couple of gem files which I install via gem install xx.gem.  Can I tell Bundler to use them?  Or do I have to specify the source path?

答: By default Bundler will check your system first and if it can't find a gem it will use the sources specified in your Gemfile.</p>

ContentLicense: CC BY-SA 3.0


问:
CarPlay with Apple Enterprise Developer Program
I was considering developing an in-house iOS application that supports CarPlay. However the problem is that when I am trying to apply for CarPlay entitlement with this link: https://developer.apple.com/contact/carplay/ on my enterprise account it gives me an information that I need to be a member of Apple Developer Program.

So I understand that as a member of Apple Enterprise Developer Program I am not able to develop for CarPlay?

答:
1 . in-house iOS application for you need to purchase enterprise developer account.

https://developer.apple.com/programs/enterprise/($299)

ContentLicense: CC BY-SA 4.0


问:
Checking two columns duplicate in Doctrine2
I wrote an API call that extracts .json file in MainTable.

In LangTable I have 3 languages and I am extracting the json data for every language.

Json data has key and locale field to populate.

To escape duplicate there should be no duplicated key<->locale values, so that should be checked for each key.

How to check that before persisting the database?

Code below works. Now I need to add logic I mentioned.

Can anybody help?

```<code>    $file = file_get_contents('translation.json');
    $jsonData = json_decode($file, true);

    $findLanguage = $this->getLangTableRepository()->findAll();

    foreach ( $findLanguage as $locale) {
        foreach ($jsonData as $data) {
            $newTranslation = new MainTable();
            $newTranslation->setKey($data);
            $newTranslation->setLocale($locale->getLocale());
            $this->em->persist($newTranslation);
        }
    }

    $this->em->flush();

    dump('done!');die;
</code>```

答:
You can use

```<code>use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
</code>```

UniqueEntity to add a constraint about being unique like so :

```<code>/**
 * @ORM\Table(name="entity")
 * @ORM\Entity(repositoryClass="App\MainBundle\Entity\EntityRepository")
 * @UniqueEntity(fields={"attributeA"}, message="Attribute A already exists
 */
</code>```

Body:
I wrote an API call that extracts .json file in MainTable.

In LangTable I have 3 languages and I am extracting the json data for every language.

Json data has key and locale field to populate.

To escape duplicate there should be no duplicated key<->locale values, so that should be checked for each key.

How to check that before persisting the database?

Code below works. Now I need to add logic I mentioned.

Can anybody help?

    $file = file_get_contents('translation.json');
    $jsonData = json_decode($file, true);

    $findLanguage = $this->getLangTableRepository()->findAll();

    foreach ( $findLanguage as $locale) {
        foreach ($jsonData as $data) {
            $newTranslation = new MainTable();
            $newTranslation->setKey($data);
            $newTranslation->setLocale($locale->getLocale());
            $this->em->persist($newTranslation);
        }
    }

    $this->em->flush();

    dump('done!');die;

Body:
You can use

use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

UniqueEntity to add a constraint about being unique like so :

/**
 * @ORM\Table(name="entity")
 * @ORM\Entity(repositoryClass="App\MainBundle\Entity\EntityRepository")
 * @UniqueEntity(fields={"attributeA"}, message="Attribute A already exists
 */


问:
A memory-efficient SHA1 implementation
I'm working with a very restrictive embedded processor, which only has 128 bytes of ram. I'd like to implement SHA1 on it. RFC3174 describes, in 'method 2', a way of implementing SHA1 that doesn't require allocating an array of 80 32-bit words (which, at 320 bytes, is obviously not practical), and seems like it ought to be usable on my processor. I'm unable to find any implementations of 'method 2', though, and the sample code in the RFC only implements the default method.

Is anyone aware of a memory-efficient implementation of SHA1 in C or C++?

答:
You should be able to quickly adapt the method 1 source to method 2. The function to change is Sha1ProcessMessageBlock() in method 1. Initialize w[0:15] from message, then do a loop of 0 to 79, where you only do w[] manipulation after iteration 16, and temp calculation depends on ts value (0-19 uses one, 20-39 uses another, etc). The important thing to remember is using index%16 or index & 0x0f whenever you are addressing the w[] array.

A quick modification would be something like this (double check all accesses to w to make sure I haven't missed the t & 0x0f):

```<code>void SHA1ProcessMessageBlock(SHA1Context *context)
{
    const uint32_t K[] =    {       /* Constants defined in SHA-1   */
                            0x5A827999,
                            0x6ED9EBA1,
                            0x8F1BBCDC,
                            0xCA62C1D6
                            };
    int           t;                 /* Loop counter                */
    uint32_t      temp;              /* Temporary word value        */
    uint32_t      W[16];             /* Word sequence               */
    uint32_t      A, B, C, D, E;     /* Word buffers                */

    /*
     *  Initialize the first 16 words in the array W. You can move this to your
     *  context.
     */
    for(t = 0; t < 16; t++)
    {
        W[t] = context->Message_Block[t * 4] << 24;
        W[t] |= context->Message_Block[t * 4 + 1] << 16;
        W[t] |= context->Message_Block[t * 4 + 2] << 8;
        W[t] |= context->Message_Block[t * 4 + 3];
    }

    A = context->Intermediate_Hash[0];
    B = context->Intermediate_Hash[1];
    C = context->Intermediate_Hash[2];
    D = context->Intermediate_Hash[3];
    E = context->Intermediate_Hash[4];

    for(t = 0; t < 80; t++) {
        if (t >= 16) {
            W[t&0xf] = SHA1CircularShift(1,W[(t-3)&0xf] ^ W[(t-8)&0xf] ^ W[(t-14)&0xf] ^ W[t&0xf]);

        }

        if (t<20) {
            temp =  SHA1CircularShift(5,A) +
                    ((B & C) | ((~B) & D)) + E + W[t&0xf] + K[0];
        }
        else if (t<40) {
            temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t&0xf] + K[1];
        }
        else if (t < 60) {
            temp = SHA1CircularShift(5,A) +
                   ((B & C) | (B & D) | (C & D)) + E + W[t&0xf] + K[2];
        }
        else {
            temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t&0xf] + K[3];
        }
        E = D;
        D = C;
        C = SHA1CircularShift(30,B);
        B = A;
        A = temp;
    }

    context->Intermediate_Hash[0] += A;
    context->Intermediate_Hash[1] += B;
    context->Intermediate_Hash[2] += C;
    context->Intermediate_Hash[3] += D;
    context->Intermediate_Hash[4] += E;

    context->Message_Block_Index = 0;
}
</code>```

There are still savings to be made: get rid of W[] array on stack and put it in context pre-initialized with the data you get.

Also, you need a lot of pre-processing before calling this function. For example, if all your messages are less than 55 bytes, you can put it in W array, add padding, and process immediately. If not, you'll have to call process twice: first with your partially padded input, and again with the rest of the pad, etc. That sort of thing would be very application specific, and I doubt you'll be able to find the code to do it for you.

By the way, the code above is a straight adaptation from the type 1 source from your link. You can probably squeeze a bit more out of it if you try to optimize it further.

I couldn't think of a way to get any savings on the intermediate hash, so you will need a total of 108 bytes for this (109 if counter is also in RAM), and 24 of which is local to this function, and can be reused in other places - so long as they are also temporary. So it is very hard to do what you want to do.

EDIT: If all your messages are less than 55 bytes, you can save another 20 bytes in your context by getting rid of the intermediate_hash[] storage. Simply initialize A-E from the constants, and add the constants at the end. Finally, instead of storing them in a separate variable, overwrite your input when this function ends.

Title: A memory-efficient SHA1 implementation

Body:
You should be able to quickly adapt the method 1 source to method 2. The function to change is Sha1ProcessMessageBlock() in method 1. Initialize w[0:15] from message, then do a loop of 0 to 79, where you only do w[] manipulation after iteration 16, and temp calculation depends on ts value (0-19 uses one, 20-39 uses another, etc). The important thing to remember is using index%16 or index & 0x0f whenever you are addressing the w[] array.

A quick modification would be something like this (double check all accesses to w to make sure I haven't missed the t & 0x0f):

void SHA1ProcessMessageBlock(SHA1Context *context)
{
    const uint32_t K[] =    {       /* Constants defined in SHA-1   */
                            0x5A827999,
                            0x6ED9EBA1,
                            0x8F1BBCDC,
                            0xCA62C1D6
                            };
    int           t;                 /* Loop counter                */
    uint32_t      temp;              /* Temporary word value        */
    uint32_t      W[16];             /* Word sequence               */
    uint32_t      A, B, C, D, E;     /* Word buffers                */

    /*
     *  Initialize the first 16 words in the array W. You can move this to your
     *  context.
     */
    for(t = 0; t < 16; t++)
    {
        W[t] = context->Message_Block[t * 4] << 24;
        W[t] |= context->Message_Block[t * 4 + 1] << 16;
        W[t] |= context->Message_Block[t * 4 + 2] << 8;
        W[t] |= context->Message_Block[t * 4 + 3];
    }

    A = context->Intermediate_Hash[0];
    B = context->Intermediate_Hash[1];
    C = context->Intermediate_Hash[2];
    D = context->Intermediate_Hash[3];
    E = context->Intermediate_Hash[4];

    for(t = 0; t < 80; t++) {
        if (t >= 16) {
            W[t&0xf] = SHA1CircularShift(1,W[(t-3)&0xf] ^ W[(t-8)&0xf] ^ W[(t-14)&0xf] ^ W[t&0xf]);

        }

        if (t<20) {
            temp =  SHA1CircularShift(5,A) +
                    ((B & C) | ((~B) & D)) + E + W[t&0xf] + K[0];
        }
        else if (t<40) {
            temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t&0xf] + K[1];
        }
        else if (t < 60) {
            temp = SHA1CircularShift(5,A) +
                   ((B & C) | (B & D) | (C & D)) + E + W[t&0xf] + K[2];
        }
        else {
            temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t&0xf] + K[3];
        }
        E = D;
        D = C;
        C = SHA1CircularShift(30,B);
        B = A;
        A = temp;
    }

    context->Intermediate_Hash[0] += A;
    context->Intermediate_Hash[1] += B;
    context->Intermediate_Hash[2] += C;
    context->Intermediate_Hash[3] += D;
    context->Intermediate_Hash[4] += E;

    context->Message_Block_Index = 0;
}

There are still savings to be made: get rid of W[] array on stack and put it in context pre-initialized with the data you get.

Also, you need a lot of pre-processing before calling this function. For example, if all your messages are less than 55 bytes, you can put it in W array, add padding, and process immediately. If not, you'll have to call process twice: first with your partially padded input, and again with the rest of the pad, etc. That sort of thing would be very application specific, and I doubt you'll be able to find the code to do it for you.

By the way, the code above is a straight adaptation from the type 1 source from your link. You can probably squeeze a bit more out of it if you try to optimize it further.

I couldn't think of a way to get any savings on the intermediate hash, so you will need a total of 108 bytes for this (109 if counter is also in RAM), and 24 of which is local to this function, and can be reused in other places - so long as they are also temporary. So it is very hard to do what you want to do.

EDIT: If all your messages are less than 55 bytes, you can save another 20 bytes in your context by getting rid of the intermediate_hash[] storage. Simply initialize A-E from the constants, and add the constants at the end. Finally, instead of storing them in a separate variable, overwrite your input when this function ends.


问:
Exit code -11 after installation of Vim auto completion plugin YouCompleteMe with MacVim (Vim version 8.1)
I'd like to install the auto completion plugin YouCompleteMe for Vim.

I followed the instructions of the installation guide at https://github.com/Valloric/YouCompleteMe/blob/master/README.md#full-installation-guide

I chose the simpler option without semantic completion support for C-family languages.

Executing
vim --version
returns

```<code>VIM - Vi IMproved 8.1 (2018 May 18, compiled Feb 19 2019 12:07:03)
macOS version
Included patches: 1-950
Compiled by Homebrew
Huge version with MacVim GUI.  Features included (+) or not (-):
+acl               -farsi             +mouse_sgr         -tag_any_white
+arabic            +file_in_path      -mouse_sysmouse    -tcl
+autocmd           +find_in_path      +mouse_urxvt       +termguicolors
+autochdir         +float             +mouse_xterm       +terminal
-autoservername    +folding           +multi_byte        +terminfo
+balloon_eval      -footer            +multi_lang        +termresponse
+balloon_eval_term +fork()            -mzscheme          +textobjects
+browse            +fullscreen        +netbeans_intg     +textprop
++builtin_terms    -gettext           +num64             +timers
+byte_offset       -hangul_input      +odbeditor         +title
+channel           +iconv             +packages          +toolbar
+cindent           +insert_expand     +path_extra        +transparency
+clientserver      +job               +perl              +user_commands
+clipboard         +jumplist          +persistent_undo   +vartabs
+cmdline_compl     +keymap            +postscript        +vertsplit
+cmdline_hist      +lambda            +printer           +virtualedit
+cmdline_info      +langmap           +profile           +visual
+comments          +libcall           -python            +visualextra
+conceal           +linebreak         +python3           +viminfo
+cryptv            +lispindent        +quickfix          +vreplace
+cscope            +listcmds          +reltime           +wildignore
+cursorbind        +localmap          +rightleft         +wildmenu
+cursorshape       +lua               +ruby              +windows
+dialog_con_gui    +menu              +scrollbind        +writebackup
+diff              +mksession         +signs             -X11
+digraphs          +modify_fname      +smartindent       -xfontset
+dnd               +mouse             +startuptime       +xim
-ebcdic            +mouseshape        +statusline        -xpm
+emacs_tags        +mouse_dec         -sun_workshop      -xsmp
+eval              -mouse_gpm         +syntax            -xterm_clipboard
+ex_extra          -mouse_jsbterm     +tag_binary        -xterm_save
+extra_search      +mouse_netterm     +tag_old_static
   system vimrc file: "$VIM/vimrc"
     user vimrc file: "$HOME/.vimrc"
 2nd user vimrc file: "~/.vim/vimrc"
      user exrc file: "$HOME/.exrc"
  system gvimrc file: "$VIM/gvimrc"
    user gvimrc file: "$HOME/.gvimrc"
2nd user gvimrc file: "~/.vim/gvimrc"
       defaults file: "$VIMRUNTIME/defaults.vim"
    system menu file: "$VIMRUNTIME/menu.vim"
  fall-back for $VIM: "/Applications/MacVim.app/Contents/Resources/vim"
Compilation: clang -c -I. -Iproto -DHAVE_CONFIG_H -DFEAT_GUI_MACVIM -Wall -Wno-unknown-pragmas -pipe  -DMACOS_X -DMACOS_X_DARWIN  -g -O2 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1
Linking: clang   -L. -L.  -L/usr/local/lib -o Vim -framework Cocoa -framework Carbon       -lm  -lncurses -liconv -framework AppKit  -L/usr/local/opt/lua/lib -llua5.3 -fstack-protector  -L/System/Library/Perl/5.18/darwin-thread-multi-2level/CORE -lperl  -L/usr/local/opt/python/Frameworks/Python.framework/Versions/3.7/lib/python3.7/config-3.7m-darwin -lpython3.7m -framework CoreFoundation  -framework Ruby
</code>```

I added the line

Plugin 'Valloric/YouCompleteMe'

to my .vimrc, afterwards the Vim command

:PluginInstall

executes without errors.

If I then start Vim the following error message is displayed:

The ycmd server SHUT DOWN (restart with ':YcmRestartServer'). Unexpected exit code -11. Type ':YcmToggleLogs ycmd_62275_stderr_menu0zqa.log' to check the logs.

The here mentioned log file is empty. Does anyone know what's the problem here?

I know there are a lot of other questions on StackOverflow and Github regarding very similar problems. But since I've spent the last 3 hours going through these and trying to get the plugin to work without success, I'd be happy if this won't be marked as duplicate and am very grateful for every response.

答:
https://valloric.github.io/YouCompleteMe/#faq

YCM does not work with my Anaconda Python setup

Anaconda is often incompatible with the pre-built libclang used by YCM and therefore is not supported. The recommended way to solve this is to run /path/to/real/python install.py (for example /usr/bin/python install.py).

If you want completion in Anaconda projects, point the interpreter_path option in your .ycm_extra_conf.py file to the path of your Anaconda Python e.g.

```<code>def Settings(**kwargs):
  return {
    'interpreter_path': '/path/to/anaconda/python'
  }
</code>```

See the Python Semantic Completion section for more details.

Body:
I'd like to install the auto completion plugin YouCompleteMe for Vim.

I followed the instructions of the installation guide at https://github.com/Valloric/YouCompleteMe/blob/master/README.md#full-installation-guide

I chose the simpler option without semantic completion support for C-family languages.

Executing
vim --version
returns

VIM - Vi IMproved 8.1 (2018 May 18, compiled Feb 19 2019 12:07:03)
macOS version
Included patches: 1-950
Compiled by Homebrew
Huge version with MacVim GUI.  Features included (+) or not (-):
+acl               -farsi             +mouse_sgr         -tag_any_white
+arabic            +file_in_path      -mouse_sysmouse    -tcl
+autocmd           +find_in_path      +mouse_urxvt       +termguicolors
+autochdir         +float             +mouse_xterm       +terminal
-autoservername    +folding           +multi_byte        +terminfo
+balloon_eval      -footer            +multi_lang        +termresponse
+balloon_eval_term +fork()            -mzscheme          +textobjects
+browse            +fullscreen        +netbeans_intg     +textprop
++builtin_terms    -gettext           +num64             +timers
+byte_offset       -hangul_input      +odbeditor         +title
+channel           +iconv             +packages          +toolbar
+cindent           +insert_expand     +path_extra        +transparency
+clientserver      +job               +perl              +user_commands
+clipboard         +jumplist          +persistent_undo   +vartabs
+cmdline_compl     +keymap            +postscript        +vertsplit
+cmdline_hist      +lambda            +printer           +virtualedit
+cmdline_info      +langmap           +profile           +visual
+comments          +libcall           -python            +visualextra
+conceal           +linebreak         +python3           +viminfo
+cryptv            +lispindent        +quickfix          +vreplace
+cscope            +listcmds          +reltime           +wildignore
+cursorbind        +localmap          +rightleft         +wildmenu
+cursorshape       +lua               +ruby              +windows
+dialog_con_gui    +menu              +scrollbind        +writebackup
+diff              +mksession         +signs             -X11
+digraphs          +modify_fname      +smartindent       -xfontset
+dnd               +mouse             +startuptime       +xim
-ebcdic            +mouseshape        +statusline        -xpm
+emacs_tags        +mouse_dec         -sun_workshop      -xsmp
+eval              -mouse_gpm         +syntax            -xterm_clipboard
+ex_extra          -mouse_jsbterm     +tag_binary        -xterm_save
+extra_search      +mouse_netterm     +tag_old_static
   system vimrc file: "$VIM/vimrc"
     user vimrc file: "$HOME/.vimrc"
 2nd user vimrc file: "~/.vim/vimrc"
      user exrc file: "$HOME/.exrc"
  system gvimrc file: "$VIM/gvimrc"
    user gvimrc file: "$HOME/.gvimrc"
2nd user gvimrc file: "~/.vim/gvimrc"
       defaults file: "$VIMRUNTIME/defaults.vim"
    system menu file: "$VIMRUNTIME/menu.vim"
  fall-back for $VIM: "/Applications/MacVim.app/Contents/Resources/vim"
Compilation: clang -c -I. -Iproto -DHAVE_CONFIG_H -DFEAT_GUI_MACVIM -Wall -Wno-unknown-pragmas -pipe  -DMACOS_X -DMACOS_X_DARWIN  -g -O2 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1
Linking: clang   -L. -L.  -L/usr/local/lib -o Vim -framework Cocoa -framework Carbon       -lm  -lncurses -liconv -framework AppKit  -L/usr/local/opt/lua/lib -llua5.3 -fstack-protector  -L/System/Library/Perl/5.18/darwin-thread-multi-2level/CORE -lperl  -L/usr/local/opt/python/Frameworks/Python.framework/Versions/3.7/lib/python3.7/config-3.7m-darwin -lpython3.7m -framework CoreFoundation  -framework Ruby

I added the line

Plugin 'Valloric/YouCompleteMe'

to my .vimrc, afterwards the Vim command

:PluginInstall

executes without errors.

If I then start Vim the following error message is displayed:

The ycmd server SHUT DOWN (restart with ':YcmRestartServer'). Unexpected exit code -11. Type ':YcmToggleLogs ycmd_62275_stderr_menu0zqa.log' to check the logs.

The here mentioned log file is empty. Does anyone know what's the problem here?

I know there are a lot of other questions on StackOverflow and Github regarding very similar problems. But since I've spent the last 3 hours going through these and trying to get the plugin to work without success, I'd be happy if this won't be marked as duplicate and am very grateful for every response.

Body:
https://valloric.github.io/YouCompleteMe/#faq

YCM does not work with my Anaconda Python setup

Anaconda is often incompatible with the pre-built libclang used by YCM and therefore is not supported. The recommended way to solve this is to run /path/to/real/python install.py (for example /usr/bin/python install.py).

If you want completion in Anaconda projects, point the interpreter_path option in your .ycm_extra_conf.py file to the path of your Anaconda Python e.g.

def Settings(**kwargs):
  return {
    'interpreter_path': '/path/to/anaconda/python'
  }

See the Python Semantic Completion section for more details.


问:
Will flip an image change the image dimension
I have an image and its mask picked from a competition hosted in kaggle. The shape of the image is (512,512,3) and the mask is (512,512,1). After applying a function(flipping) on an image, the shape remains the same. However, before applying the operation when I try to access the mask such as (print mask[:,:,0]), I get a matrix,

```<code>  [[0. 0. 0. ... 0. 0. 0.]
   [0. 0. 0. ... 0. 0. 0.]
   [0. 0. 0. ... 0. 0. 0.]
   ...
   [0. 0. 0. ... 0. 0. 0.]
   [0. 0. 0. ... 0. 0. 0.]
   [0. 0. 0. ... 0. 0. 0.]]
</code>```

but after applying the operation, and try to access the mask (print mask[:,:,0]), I get the following error

```<code> Traceback (most recent call last):
 File "Augmentation.py", line 94, in <module>
 plot_img_and_mask_transformed(img,mask,img_flip,mask_flip)
 File "Augmentation.py", line 36, in plot_img_and_mask_transformed
   print(mask_tr[:,:,0])
 IndexError: too many indices for array
</code>```

The function I applied was

```<code> def random_flip(img,mask,u=0.5):

   if np.random.random() < u :
      img = cv.flip(img,0)
      mask = cv.flip(mask,0)
   return img, mask

img, mask = get_image_and_mask(img_id)
img_tr,mask_tr = random_flip(img,mask)
plot(img,mask,img_tr,mask_tr)
</code>```

The shape of the image and the mask before flipping

```<code>  ((512, 512, 3), (512, 512, 1))
</code>```

The shape of the image and the mask after flipping

```<code>  ((512, 512, 3), (512, 512))
</code>```

Can someone help me out what's happening behind the scenes?

CODE

```<code>def get_image_and_mask(img_id):
  img = image.load_img(join(data_dir,'train','%s.jpg' % img_id),target_size=(input_size,input_size))
  img = image.img_to_array(img)
  mask = image.load_img(join(data_dir,'train_masks','%s_mask.gif' % img_id), grayscale=True,target_size=(input_size,input_size))
  mask = image.img_to_array(mask)
  img,mask = img / 255., mask/ 255.
  return img, mask

def plot_img_and_mask(img,mask):
  fig, axs = plt.subplots(ncols=2, figsize=(10,5),sharex=True,sharey=True)
  axs[0].imshow(img)
  axs[1].imshow(mask[:,:,0])
  for ax in axs:
     ax.set_xlim(0,input_size)
     ax.axis('off')
  fig.tight_layout()
  plt.show()

def plot_img_and_mask_transformed(img, mask, img_tr, mask_tr):
 fig, axs=plt.subplots(ncols=4,figsize=(16,4),sharex=True,sharey=True)
 axs[0].imshow(img)

 axs[1].imshow(mask[:,:,0])
 print(mask[:,:,0])
 print(mask_tr[:,:,0])
 axs[2].imshow(img_tr)
 axs[3].imshow(mask_tr)

 for ax in axs:
    ax.set_xlim(0,input_size)
    ax.axis('off')

 fig.tight_layout()
 plt.show()

def random_flip(img,mask,u=0.5):
  # Why do we have to check less than u
  if np.random.random() < u :
     img = cv.flip(img,0)
     mask = cv.flip(mask,0)
  return img, mask

def rotate(x,theta,row_axis=0,col_axis=1,channel_axis=2,fill_mode='nearest',cval=0):
 rotation_matrix = np.array([
  [np.cos(theta),-np.sin(theta),0],
  [np.sin(theta),np.cos(theta),0],
  [0,0,1]
 ])

 h, w = x.shape[row_axis], x.shape[col_axis]
 transform_matrix = image.transform_matrix_offset_center(rotation_matrix,h,w)
 x = image.apply_transform(x,transform_matrix,channel_axis,fill_mode,cval)
 return x

def random_rotate(img, mask, rotate_limit=(-20,20), u=0.5):
   if np.random.random() <  u:
     theta = np.pi/ 180 * np.random.uniform(rotate_limit[0], rotate_limit[1])
     img = rotate(img,theta)
     mask = rotate(mask,theta)
   return img, mask

if __name__== '__main__':

  input_size = 512
  data_dir = '../data/carvana-image-masking-challenge'
  np.random.seed(1987)

  df_train = pd.read_csv(join(data_dir,'train_masks.csv'),usecols=['img'])
  df_train['img_id']=df_train['img'].map(lambda s:s.split('.')[0])
  df_train.head(3)

 img_ids=df_train['img_id'].values
 np.random.shuffle(img_ids)
 img_id=img_ids[0]
 img,mask=get_image_and_mask(img_id)
 print((img.shape,mask.shape))
 plot_img_and_mask(img,mask)

 img_flip,mask_flip = random_flip(img,mask,u=1)
 print((img_flip.shape,mask_flip.shape))
 plot_img_and_mask_transformed(img,mask,img_flip,mask_flip)
</code>```

OUTPUT

```<code>    Using TensorFlow backend.
    C:\Users\JamesJohnson\AppData\Local\Programs\Python\Python35\lib\site- packages\keras_preprocessing\image.py:492: UserWarning: grayscale is deprecated. Please use color_mode = "grayscale"
   warnings.warn('grayscale is deprecated. Please use '
  > ((512, 512, 3), (512, 512, 1))
  > ((512, 512, 3), (512, 512))
  [[0. 0. 0. ... 0. 0. 0.]
   [0. 0. 0. ... 0. 0. 0.]
   [0. 0. 0. ... 0. 0. 0.]
   ...
   [0. 0. 0. ... 0. 0. 0.]
   [0. 0. 0. ... 0. 0. 0.]
   [0. 0. 0. ... 0. 0. 0.]]
 Traceback (most recent call last):
 File "Augmentation.py", line 94, in <module>
 plot_img_and_mask_transformed(img,mask,img_flip,mask_flip)
 File "Augmentation.py", line 36, in plot_img_and_mask_transformed
 print(mask_tr[:,:,0])
 IndexError: too many indices for array
</code>```

答:
It looks like OpenCV dumps the singleton dimension when you flip the mask. You'll need to reintroduce it after you flip.

```<code>mask_flip = mask_flip[..., None]
</code>```

A more convenient way is to modify your method so the mask is returned with the singleton dimension after you flip in case you lose it. This way you don't have to do this every time you flip and the method takes care of that instead.

```<code>def random_flip(img,mask,u=0.5):
    # Why do we have to check less than u
    if np.random.random() < u:
        img = cv.flip(img,0)
        mask = cv.flip(mask,0)
        if len(mask.shape) == 2:
            mask = mask[..., None]
        return img, mask
</code>```

BTW as a minor note, you have a comment that asks why you have to check for less than u in the method.   Remember that the np.random.random method generates a value between 0 and 1 uniformly.  Suppose you chose u = 0.3.  This means that there is a 30% chance that you will choose a value between between 0 and 0.3 and a 70% chance that you will choose a value between 0.3 and 1.  Loosely, this means that if u = 0.3, there is a 30% chance that the if condition is run and you thus flip the image and mask.  Therefore, the u controls the probability that a flip of the image and mask will happen.

Body:
I have an image and its mask picked from a competition hosted in kaggle. The shape of the image is (512,512,3) and the mask is (512,512,1). After applying a function(flipping) on an image, the shape remains the same. However, before applying the operation when I try to access the mask such as (print mask[:,:,0]), I get a matrix,

  [[0. 0. 0. ... 0. 0. 0.]
   [0. 0. 0. ... 0. 0. 0.]
   [0. 0. 0. ... 0. 0. 0.]
   ...
   [0. 0. 0. ... 0. 0. 0.]
   [0. 0. 0. ... 0. 0. 0.]
   [0. 0. 0. ... 0. 0. 0.]]

but after applying the operation, and try to access the mask (print mask[:,:,0]), I get the following error

 Traceback (most recent call last):
 File "Augmentation.py", line 94, in <module>
 plot_img_and_mask_transformed(img,mask,img_flip,mask_flip)
 File "Augmentation.py", line 36, in plot_img_and_mask_transformed
   print(mask_tr[:,:,0])
 IndexError: too many indices for array

The function I applied was

 def random_flip(img,mask,u=0.5):

   if np.random.random() < u :
      img = cv.flip(img,0)
      mask = cv.flip(mask,0)
   return img, mask

img, mask = get_image_and_mask(img_id)
img_tr,mask_tr = random_flip(img,mask)
plot(img,mask,img_tr,mask_tr)

The shape of the image and the mask before flipping

  ((512, 512, 3), (512, 512, 1))

The shape of the image and the mask after flipping

  ((512, 512, 3), (512, 512))

Can someone help me out what's happening behind the scenes?

CODE

def get_image_and_mask(img_id):
  img = image.load_img(join(data_dir,'train','%s.jpg' % img_id),target_size=(input_size,input_size))
  img = image.img_to_array(img)
  mask = image.load_img(join(data_dir,'train_masks','%s_mask.gif' % img_id), grayscale=True,target_size=(input_size,input_size))
  mask = image.img_to_array(mask)
  img,mask = img / 255., mask/ 255.
  return img, mask

def plot_img_and_mask(img,mask):
  fig, axs = plt.subplots(ncols=2, figsize=(10,5),sharex=True,sharey=True)
  axs[0].imshow(img)
  axs[1].imshow(mask[:,:,0])
  for ax in axs:
     ax.set_xlim(0,input_size)
     ax.axis('off')
  fig.tight_layout()
  plt.show()

def plot_img_and_mask_transformed(img, mask, img_tr, mask_tr):
 fig, axs=plt.subplots(ncols=4,figsize=(16,4),sharex=True,sharey=True)
 axs[0].imshow(img)

 axs[1].imshow(mask[:,:,0])
 print(mask[:,:,0])
 print(mask_tr[:,:,0])
 axs[2].imshow(img_tr)
 axs[3].imshow(mask_tr)

 for ax in axs:
    ax.set_xlim(0,input_size)
    ax.axis('off')

 fig.tight_layout()
 plt.show()

def random_flip(img,mask,u=0.5):
  # Why do we have to check less than u
  if np.random.random() < u :
     img = cv.flip(img,0)
     mask = cv.flip(mask,0)
  return img, mask

def rotate(x,theta,row_axis=0,col_axis=1,channel_axis=2,fill_mode='nearest',cval=0):
 rotation_matrix = np.array([
  [np.cos(theta),-np.sin(theta),0],
  [np.sin(theta),np.cos(theta),0],
  [0,0,1]
 ])

 h, w = x.shape[row_axis], x.shape[col_axis]
 transform_matrix = image.transform_matrix_offset_center(rotation_matrix,h,w)
 x = image.apply_transform(x,transform_matrix,channel_axis,fill_mode,cval)
 return x

def random_rotate(img, mask, rotate_limit=(-20,20), u=0.5):
   if np.random.random() <  u:
     theta = np.pi/ 180 * np.random.uniform(rotate_limit[0], rotate_limit[1])
     img = rotate(img,theta)
     mask = rotate(mask,theta)
   return img, mask

if __name__== '__main__':

  input_size = 512
  data_dir = '../data/carvana-image-masking-challenge'
  np.random.seed(1987)

  df_train = pd.read_csv(join(data_dir,'train_masks.csv'),usecols=['img'])
  df_train['img_id']=df_train['img'].map(lambda s:s.split('.')[0])
  df_train.head(3)

 img_ids=df_train['img_id'].values
 np.random.shuffle(img_ids)
 img_id=img_ids[0]
 img,mask=get_image_and_mask(img_id)
 print((img.shape,mask.shape))
 plot_img_and_mask(img,mask)

 img_flip,mask_flip = random_flip(img,mask,u=1)
 print((img_flip.shape,mask_flip.shape))
 plot_img_and_mask_transformed(img,mask,img_flip,mask_flip)

OUTPUT

    Using TensorFlow backend.
    C:\Users\JamesJohnson\AppData\Local\Programs\Python\Python35\lib\site- packages\keras_preprocessing\image.py:492: UserWarning: grayscale is deprecated. Please use color_mode = "grayscale"
   warnings.warn('grayscale is deprecated. Please use '
  > ((512, 512, 3), (512, 512, 1))
  > ((512, 512, 3), (512, 512))
  [[0. 0. 0. ... 0. 0. 0.]
   [0. 0. 0. ... 0. 0. 0.]
   [0. 0. 0. ... 0. 0. 0.]
   ...
   [0. 0. 0. ... 0. 0. 0.]
   [0. 0. 0. ... 0. 0. 0.]
   [0. 0. 0. ... 0. 0. 0.]]
 Traceback (most recent call last):
 File "Augmentation.py", line 94, in <module>
 plot_img_and_mask_transformed(img,mask,img_flip,mask_flip)
 File "Augmentation.py", line 36, in plot_img_and_mask_transformed
 print(mask_tr[:,:,0])
 IndexError: too many indices for array

Body:
It looks like OpenCV dumps the singleton dimension when you flip the mask. You'll need to reintroduce it after you flip.

mask_flip = mask_flip[..., None]

A more convenient way is to modify your method so the mask is returned with the singleton dimension after you flip in case you lose it. This way you don't have to do this every time you flip and the method takes care of that instead.

def random_flip(img,mask,u=0.5):
    # Why do we have to check less than u
    if np.random.random() < u:
        img = cv.flip(img,0)
        mask = cv.flip(mask,0)
        if len(mask.shape) == 2:
            mask = mask[..., None]
        return img, mask

BTW as a minor note, you have a comment that asks why you have to check for less than u in the method.   Remember that the np.random.random method generates a value between 0 and 1 uniformly.  Suppose you chose u = 0.3.  This means that there is a 30% chance that you will choose a value between between 0 and 0.3 and a 70% chance that you will choose a value between 0.3 and 1.  Loosely, this means that if u = 0.3, there is a 30% chance that the if condition is run and you thus flip the image and mask.  Therefore, the u controls the probability that a flip of the image and mask will happen.


问:
Convert object in JSON to map in Java
Been trying to convert an object in JSON to java using Gson but many things do not match up.
Before the object was an int, now it's converted into an object.
This is part of the JSON (I need only damage to get converted):

I've tried most of the guides online but still not helpful at all.

```<code>[
  {
    "username": "lol",
    "value": [
      "one","two"
    ],
    "Power": [
      {
        "type": "Damage",
        "damage": {"first":2, "second":0,"third":0}
}]}]
</code>```

答:
To convert your Text into an Hashmap, use this.

String yourJson = "insert your JSON here";
Type type = new TypeToken<List<Map<String, Object>>>() {}.getType();
List<Map<String, Object>> parsedJson = new Gson().fromJson(yourJson, type);

```
<p>now you can access it via:</p>
```Map<String, Object> user = (Map<String, Object>) parsedJson.get(0);
List<Map<String, Object>> userPowers = (List<Map<String, Object>>) user.get("Power");
Map<String, Object> usersFirstPower = userPowers.get(0);
Map<String, Object> firstPowerDamage = (Map<String, Object>) usersFirstPower.get("damage");
System.out.println(firstPowerDamage.get("first").toString());
```
<h2>Edit</h2>
<p>This is another way to use Gson:</p>
<pre class="lang-java prettyprint-override"><code>String yourJson = "...";
JsonArray map = (JsonArray) new JsonParser().parse(yourJson);
System.out.println(map.get(0).getAsJsonObject().get("Power").getAsJsonArray().get(0).getAsJsonObject().get("damage"));
</code>```

Title: Convert object in JSON to map in Java

Body:
Been trying to convert an object in JSON to java using Gson but many things do not match up.
Before the object was an int, now it's converted into an object.
This is part of the JSON (I need only damage to get converted):

I've tried most of the guides online but still not helpful at all.

[
  {
    "username": "lol",
    "value": [
      "one","two"
    ],
    "Power": [
      {
        "type": "Damage",
        "damage": {"first":2, "second":0,"third":0}
}]}]

Body:
To convert your Text into an Hashmap, use this.

String yourJson = "insert your JSON here";
Type type = new TypeToken<List<Map<String, Object>>>() {}.getType();
List<Map<String, Object>> parsedJson = new Gson().fromJson(yourJson, type);

now you can access it via:

Map<String, Object> user = (Map<String, Object>) parsedJson.get(0);
List<Map<String, Object>> userPowers = (List<Map<String, Object>>) user.get("Power");
Map<String, Object> usersFirstPower = userPowers.get(0);
Map<String, Object> firstPowerDamage = (Map<String, Object>) usersFirstPower.get("damage");
System.out.println(firstPowerDamage.get("first").toString());

Edit

This is another way to use Gson:

String yourJson = "...";
JsonArray map = (JsonArray) new JsonParser().parse(yourJson);
System.out.println(map.get(0).getAsJsonObject().get("Power").getAsJsonArray().get(0).getAsJsonObject().get("damage"));


问:
ISNumeric Alternatives for Oracle
I want to see what value is higher A or B. They are strings but most strings only contain Numbers. i need to convert those numbers so i can use the > and < operators.

They don't want me to create a function on "their" database. So that's not an option. I tried Lukasz Szozda his approach on "Safe" TO_NUMBER() but that just gives me "Missing Right Parenthesis"

I tried https://docs.oracle.com/cd/E51367_01/financialsop_gs/FADRA/ch09s05s53.html
No succes either.

And i tried with REGEX but no luck either.

```<code>SELECT *
FROM Table
WHERE
(
 TO_NUMBER('T' DEFAULT 0 ON CONVERSION ERROR)
 >
 TO_NUMBER('5' DEFAULT 0 ON CONVERSION ERROR)
)
</code>```

```<code>SELECT *
FROM Table
WHERE
(
 TO_NUMBER('7' DEFAULT 0 ON CONVERSION ERROR)
 >
 TO_NUMBER('5' DEFAULT 0 ON CONVERSION ERROR)
)
</code>```

The first bit of code should return nothing, the second the entire table.
But all i get is "Missing Right Parenthesis"

答:
As noted in Lukasz's answer on the question you linked to, the on conversion error syntax was added in 12cR2, so you cannot use it in 11gR2 - that is the cause of the error you are getting.

The regular expression method described in that question works though:

```<code>SELECT *
FROM dual
WHERE
(
 COALESCE(TO_NUMBER(REGEXP_SUBSTR('T', '^\d+')), 0)
 >
 COALESCE(TO_NUMBER(REGEXP_SUBSTR('5', '^\d+')), 0)
);

no rows selected

SELECT *
FROM dual
WHERE
(
 COALESCE(TO_NUMBER(REGEXP_SUBSTR('7', '^\d+')), 0)
 >
 COALESCE(TO_NUMBER(REGEXP_SUBSTR('5', '^\d+')), 0)
);

D
-
X
</code>```

Title: ISNumeric Alternatives for Oracle

Body:
I want to see what value is higher A or B. They are strings but most strings only contain Numbers. i need to convert those numbers so i can use the > and < operators.

They don't want me to create a function on "their" database. So that's not an option. I tried Lukasz Szozda his approach on "Safe" TO_NUMBER() but that just gives me "Missing Right Parenthesis"

I tried https://docs.oracle.com/cd/E51367_01/financialsop_gs/FADRA/ch09s05s53.html
No succes either.

And i tried with REGEX but no luck either.

SELECT *
FROM Table
WHERE
(
 TO_NUMBER('T' DEFAULT 0 ON CONVERSION ERROR)
 >
 TO_NUMBER('5' DEFAULT 0 ON CONVERSION ERROR)
)

SELECT *
FROM Table
WHERE
(
 TO_NUMBER('7' DEFAULT 0 ON CONVERSION ERROR)
 >
 TO_NUMBER('5' DEFAULT 0 ON CONVERSION ERROR)
)

The first bit of code should return nothing, the second the entire table.
But all i get is "Missing Right Parenthesis"

Body:
As noted in Lukasz's answer on the question you linked to, the on conversion error syntax was added in 12cR2, so you cannot use it in 11gR2 - that is the cause of the error you are getting.

The regular expression method described in that question works though:

SELECT *
FROM dual
WHERE
(
 COALESCE(TO_NUMBER(REGEXP_SUBSTR('T', '^\d+')), 0)
 >
 COALESCE(TO_NUMBER(REGEXP_SUBSTR('5', '^\d+')), 0)
);

no rows selected

SELECT *
FROM dual
WHERE
(
 COALESCE(TO_NUMBER(REGEXP_SUBSTR('7', '^\d+')), 0)
 >
 COALESCE(TO_NUMBER(REGEXP_SUBSTR('5', '^\d+')), 0)
);

D
-
X


问:
update a column when multiple records of another columns of same table matches
I have table as below:

```<code>            Input table:
            RequestNumber            TrackName         Date
            -----------------------------------------------------------
            02209                    Middle         2017-05-28 00:00:00
            0263                     Middle         2017-06-29 00:00:00
            0633                     Middle         2017-09-10 00:00:00
            0762                     Back           2017-06-23 00:00:00
            0762                     Front          2017-06-23 00:00:00
            0762                     Middle         2017-06-23 00:00:00
            01839                    Middle         2017-03-09 00:00:00
</code>```

I need to check, if "RequestNumber" and "Date" is same for "TrackName".
if multiple reacords of "RequestNumber" and "Date" matches then I should update the "TrackName" column as "All three" as below
output example (in this example there are 3 records matching)

```<code>            Output table:
            RequestNumber            TrackName         Date
            -----------------------------------------------------------
            02209                    Middle         2017-05-28 00:00:00
            0263                     Middle         2017-06-29 00:00:00
            0633                     Middle         2017-09-10 00:00:00
            0762                     All three      2017-06-23 00:00:00
            01839                    Middle         2017-03-09 00:00:00
</code>```

To get the above desigred output this is the SQL I have tried. Howveer, its updating all TrackName to All three.

```<code>            UPDATE a
            SET a.[TrackName] = 'All three'
            FROM Table1 as a
            INNER JOIN
            (SELECT [RequestNumber], row_number() OVER (ORDER BY [RequestNumber] DESC) as rowNumber
            FROM Table1 ) drRowNumbers ON drRowNumbers.[RequestNumber] = a.[RequestNumber] and drRowNumbers.[Date] = a.[Date]
</code>```

Hope I have explained this correctly. What am I doing worng? Any query to fix this please?

Note: The records are coming dynamically so can not be hardcoded(if any).

Thanks.

答:
This is not an just an update, it's also a delete.

This means you must have a two step process - one to update relevant records and the next to delete the duplicates created by the first step.

This can be done using a couple of common table expressions that use window functions such as count() over() and row_number() over(), when both steps are joined inside a transaction.

First, create and populate sample table (Please save us this step in your future questions):

```<code>DECLARE @T AS TABLE
(
    RequestNumber int,
    TrackName varchar(10),
    [Date] datetime2
);

INSERT INTO @T (RequestNumber, TrackName, Date) VALUES
(02209, 'Middle', '2017-05-28 00:00:00'),
(0263, 'Middle', '2017-06-29 00:00:00'),
(0633, 'Middle', '2017-09-10 00:00:00'),
(0762, 'Back', '2017-06-23 00:00:00'),
(0762, 'Front', '2017-06-23 00:00:00'),
(0762, 'Middle', '2017-06-23 00:00:00'),
(01839, 'Middle', '2017-03-09 00:00:00');
</code>```

Next, start a try block and a transaction:

```<code>BEGIN TRY
BEGIN TRANSACTION;
</code>```

Then, identify and update the relevant records:

```<code>WITH CTE AS
(
    SELECT  RequestNumber,
            TrackName,
            Date,
            COUNT(TrackName) OVER(PARTITION BY RequestNumber, Date) As Cnt
    FROM @T
)

UPDATE CTE
SET TrackName = 'All Three'
WHERE Cnt = 3;
</code>```

Next, delete the duplicates:

```<code>WITH CTE AS
(
    SELECT  RequestNumber,
            TrackName,
            Date,
            ROW_NUMBER() OVER(PARTITION BY RequestNumber, Date ORDER BY TrackName) As Rn
    FROM @T
)

DELETE
FROM CTE
WHERE Rn > 1;
</code>```

Commit the transaction and close the try block:

```<code>COMMIT TRANSACTION;
END TRY
</code>```

Use a catch block to rollback the transaction:

```<code>BEGIN CATCH
    IF @@TRANCOUNT > 0
        ROLLBACK TRANSACTION

    -- you might want to print the result of ERROR_MESSAGE() here...
END CATCH
</code>```

And finally, select to see the changes:

```<code>SELECT  RequestNumber,
        TrackName,
        Date
FROM @T
</code>```

Results:

```<code>RequestNumber   TrackName   Date
2209            Middle      28.05.2017 00:00:00
263             Middle      29.06.2017 00:00:00
633             Middle      10.09.2017 00:00:00
762             All Three   23.06.2017 00:00:00
1839            Middle      09.03.2017 00:00:00
</code>```

You can see a live demo on rextester (minus the transaction part which isn't allowed there and the try...catch which isn't relevant without the transaction anyway)

Body:
I have table as below:

            Input table:
            RequestNumber            TrackName         Date
            -----------------------------------------------------------
            02209                    Middle         2017-05-28 00:00:00
            0263                     Middle         2017-06-29 00:00:00
            0633                     Middle         2017-09-10 00:00:00
            0762                     Back           2017-06-23 00:00:00
            0762                     Front          2017-06-23 00:00:00
            0762                     Middle         2017-06-23 00:00:00
            01839                    Middle         2017-03-09 00:00:00

I need to check, if "RequestNumber" and "Date" is same for "TrackName".
if multiple reacords of "RequestNumber" and "Date" matches then I should update the "TrackName" column as "All three" as below
output example (in this example there are 3 records matching)

            Output table:
            RequestNumber            TrackName         Date
            -----------------------------------------------------------
            02209                    Middle         2017-05-28 00:00:00
            0263                     Middle         2017-06-29 00:00:00
            0633                     Middle         2017-09-10 00:00:00
            0762                     All three      2017-06-23 00:00:00
            01839                    Middle         2017-03-09 00:00:00

To get the above desigred output this is the SQL I have tried. Howveer, its updating all TrackName to All three.

            UPDATE a
            SET a.[TrackName] = 'All three'
            FROM Table1 as a
            INNER JOIN
            (SELECT [RequestNumber], row_number() OVER (ORDER BY [RequestNumber] DESC) as rowNumber
            FROM Table1 ) drRowNumbers ON drRowNumbers.[RequestNumber] = a.[RequestNumber] and drRowNumbers.[Date] = a.[Date]

Hope I have explained this correctly. What am I doing worng? Any query to fix this please?

Note: The records are coming dynamically so can not be hardcoded(if any).

Thanks.

Body:
This is not an just an update, it's also a delete.

This means you must have a two step process - one to update relevant records and the next to delete the duplicates created by the first step.

This can be done using a couple of common table expressions that use window functions such as count() over() and row_number() over(), when both steps are joined inside a transaction.

First, create and populate sample table (Please save us this step in your future questions):

DECLARE @T AS TABLE
(
    RequestNumber int,
    TrackName varchar(10),
    [Date] datetime2
);

INSERT INTO @T (RequestNumber, TrackName, Date) VALUES
(02209, 'Middle', '2017-05-28 00:00:00'),
(0263, 'Middle', '2017-06-29 00:00:00'),
(0633, 'Middle', '2017-09-10 00:00:00'),
(0762, 'Back', '2017-06-23 00:00:00'),
(0762, 'Front', '2017-06-23 00:00:00'),
(0762, 'Middle', '2017-06-23 00:00:00'),
(01839, 'Middle', '2017-03-09 00:00:00');

Next, start a try block and a transaction:

BEGIN TRY
BEGIN TRANSACTION;

Then, identify and update the relevant records:

WITH CTE AS
(
    SELECT  RequestNumber,
            TrackName,
            Date,
            COUNT(TrackName) OVER(PARTITION BY RequestNumber, Date) As Cnt
    FROM @T
)

UPDATE CTE
SET TrackName = 'All Three'
WHERE Cnt = 3;

Next, delete the duplicates:

WITH CTE AS
(
    SELECT  RequestNumber,
            TrackName,
            Date,
            ROW_NUMBER() OVER(PARTITION BY RequestNumber, Date ORDER BY TrackName) As Rn
    FROM @T
)

DELETE
FROM CTE
WHERE Rn > 1;

Commit the transaction and close the try block:

COMMIT TRANSACTION;
END TRY

Use a catch block to rollback the transaction:

BEGIN CATCH
    IF @@TRANCOUNT > 0
        ROLLBACK TRANSACTION

    -- you might want to print the result of ERROR_MESSAGE() here...
END CATCH

And finally, select to see the changes:

SELECT  RequestNumber,
        TrackName,
        Date
FROM @T

Results:

RequestNumber   TrackName   Date
2209            Middle      28.05.2017 00:00:00
263             Middle      29.06.2017 00:00:00
633             Middle      10.09.2017 00:00:00
762             All Three   23.06.2017 00:00:00
1839            Middle      09.03.2017 00:00:00

You can see a live demo on rextester (minus the transaction part which isn't allowed there and the try...catch which isn't relevant without the transaction anyway)


问:
Rails 5 Filter chain halted as :set_product rendered or redirected error when indexing all products
I'm running a Rails api-only app with a product model. I've created products on the backend with activeadmin. When using postman to view individual products with a corresponding product ID, it returns the json data fine but when I try to view all product at: api/v1/products

I get this error:

Started GET "/api/v1/products/" for ::1 at 2019-05-28 11:12:47 +0100
  Processing by Api::V1::ProductsController#index as JSON
    Parameters: {"product"=>{}}
  Filter chain halted as :set_product rendered or redirected
  Completed 404 Not Found in 5ms (Views: 0.2ms | ActiveRecord: 4.9ms)

Here's my controller:

```<code>module Api
  module V1
    class ProductsController < Api::V1::ApiController
      # do not authenticate users before viewing product listing or individual products
      before_action :authenticate_user!, except: %i[index show]
      before_action :set_product, only: %i[index show]

      # GET /product
      def index
        product = Product.all.order('id')
        render json: { success: true, data: product }, status: :ok
      end

      # GET /products/:id
      def show
        @product = Product.find(params[:id])
        render json: { success: true, data: @products }, status: :ok
      end

      private

      def set_product
        @products = Product.find(params[:id])
        rescue StandardError => error
        render json: { success: false, error: error }, status: 404
      end

      def product_params
        params.require(:product).permit(:title, :release_date, :release_time, :description, :price, :brand)
      end

    end
  end
end
</code>```

Here are my api views:

_product.json.jbuilder

```<code>json.id               product.id
json.title            product.title
json.description      product.description
json.release_date     product.release_date
json.release_time     product.release_time
json.price            product.price
json.brand            product.brand
</code>```

index.json.jbuilder

```<code>json.array! @products, partial: 'products/product', as: :product

</code>```

show.json.jbuilder

```<code>json.product do
  json.partial! 'product'
end
</code>```

routes

```<code>namespace :api do
    namespace :v1, defaults: { format: :json } do
      devise_scope :user do
        get :status, to: 'api#status'
        resource :user, only: %i[update show] do
          get :profile
        end
      end
      resources :products, only: [:index, :show]
    end

  end
</code>```

答:
convert

```<code>module Api
  module V1
    class ProductsController < Api::V1::ApiController
      # do not authenticate users before viewing product listing or individual products
      before_action :authenticate_user!, except: %i[index show]
      before_action :set_product, only: %i[index show]

      # GET /product
      def index
        product = Product.all.order('id')
        render json: { success: true, data: product }, status: :ok
      end

      # GET /products/:id
      def show
        @product = Product.find(params[:id])
        render json: { success: true, data: @products }, status: :ok
      end

      private

      def set_product
        @products = Product.find(params[:id])
        rescue StandardError => error
        render json: { success: false, error: error }, status: 404
      end

      def product_params
        params.require(:product).permit(:title, :release_date, :release_time, :description, :price, :brand)
      end

    end
  end
end
</code>```

to

```<code>module Api
  module V1
    class ProductsController < Api::V1::ApiController
      # do not authenticate users before viewing product listing or individual products
      before_action :authenticate_user!, except: %i[index show]
      before_action :set_product, only: %i[show]

      # GET /product
      def index
        @products = Product.all.order('id')
        render json: { success: true, data: @products }, status: :ok
      end

      # GET /products/:id
      def show
        if @product.present?
          render json: { success: true, data: @product }, status: :ok
        else
           render json: { success: false }, status: :not_found
        end

      end

      private

      def set_product
        @product = Product.find(params[:id])
      end

      def product_params
        params.require(:product).permit(:title, :release_date, :release_time, :description, :price, :brand)
      end

    end
  end
end
</code>```

Body:
I'm running a Rails api-only app with a product model. I've created products on the backend with activeadmin. When using postman to view individual products with a corresponding product ID, it returns the json data fine but when I try to view all product at: api/v1/products

I get this error:

Started GET "/api/v1/products/" for ::1 at 2019-05-28 11:12:47 +0100
  Processing by Api::V1::ProductsController#index as JSON
    Parameters: {"product"=>{}}
  Filter chain halted as :set_product rendered or redirected
  Completed 404 Not Found in 5ms (Views: 0.2ms | ActiveRecord: 4.9ms)

Here's my controller:

module Api
  module V1
    class ProductsController < Api::V1::ApiController
      # do not authenticate users before viewing product listing or individual products
      before_action :authenticate_user!, except: %i[index show]
      before_action :set_product, only: %i[index show]

      # GET /product
      def index
        product = Product.all.order('id')
        render json: { success: true, data: product }, status: :ok
      end

      # GET /products/:id
      def show
        @product = Product.find(params[:id])
        render json: { success: true, data: @products }, status: :ok
      end

      private

      def set_product
        @products = Product.find(params[:id])
        rescue StandardError => error
        render json: { success: false, error: error }, status: 404
      end

      def product_params
        params.require(:product).permit(:title, :release_date, :release_time, :description, :price, :brand)
      end

    end
  end
end

Here are my api views:

_product.json.jbuilder

json.id               product.id
json.title            product.title
json.description      product.description
json.release_date     product.release_date
json.release_time     product.release_time
json.price            product.price
json.brand            product.brand

index.json.jbuilder

json.array! @products, partial: 'products/product', as: :product

show.json.jbuilder

json.product do
  json.partial! 'product'
end

routes

namespace :api do
    namespace :v1, defaults: { format: :json } do
      devise_scope :user do
        get :status, to: 'api#status'
        resource :user, only: %i[update show] do
          get :profile
        end
      end
      resources :products, only: [:index, :show]
    end

  end

Body:
convert

module Api
  module V1
    class ProductsController < Api::V1::ApiController
      # do not authenticate users before viewing product listing or individual products
      before_action :authenticate_user!, except: %i[index show]
      before_action :set_product, only: %i[index show]

      # GET /product
      def index
        product = Product.all.order('id')
        render json: { success: true, data: product }, status: :ok
      end

      # GET /products/:id
      def show
        @product = Product.find(params[:id])
        render json: { success: true, data: @products }, status: :ok
      end

      private

      def set_product
        @products = Product.find(params[:id])
        rescue StandardError => error
        render json: { success: false, error: error }, status: 404
      end

      def product_params
        params.require(:product).permit(:title, :release_date, :release_time, :description, :price, :brand)
      end

    end
  end
end

to

module Api
  module V1
    class ProductsController < Api::V1::ApiController
      # do not authenticate users before viewing product listing or individual products
      before_action :authenticate_user!, except: %i[index show]
      before_action :set_product, only: %i[show]

      # GET /product
      def index
        @products = Product.all.order('id')
        render json: { success: true, data: @products }, status: :ok
      end

      # GET /products/:id
      def show
        if @product.present?
          render json: { success: true, data: @product }, status: :ok
        else
           render json: { success: false }, status: :not_found
        end

      end

      private

      def set_product
        @product = Product.find(params[:id])
      end

      def product_params
        params.require(:product).permit(:title, :release_date, :release_time, :description, :price, :brand)
      end

    end
  end
end


问:
Load file in rspec before block VS load them once
Today I'm tryng to speed up my tests suite. My application is basically a big integrator between systems so most of my tests are using Savon mocks like this

RSpec.describe MyClass do

  describe 'a function which sends a SOAP request'do
    before do
      savon.mock!
      savon.expects(action).returns(File.read("spec/fixtures/somefile.xml"))
    end

    after { savon.unmock! }

    it 'checks something'
    it 'checks something else'
    it 'checks something more'
    it 'checks something different'
  end
end
```

<p>Obviously most of those tests are quite slow as they are loading a file. Moreover sometimes these mocks are inside nested contexts in order to combine multiple shared examples which increases the amount of loads.
Thinking to speed up some of these tests I tried to reduce the number of file loads moving them outside the <code>before</code> block. like this </p>

<pre class="lang-rb prettyprint-override"><code>RSpec.describe MyClass do

  describe 'a function which sends a SOAP request'do
    the_file = File.read("spec/fixtures/somefile.xml")

    before do
      savon.mock!
      savon.expects(action).returns(the_file)
    end

    after { savon.unmock! }

    it 'checks something'
    it 'checks something else'
    it 'checks something more'
    it 'checks something different'
  end
end
</code>```

Indeed, the speed does not change; I have blocks of 96 tests with multiples, nested contexts and checks and I haven't gained not even 0.01 seconds. So my questions are:

I supposed the before block loads the file for each it, am I
right?

Does Rspec or Savon have some kind of cache?

How can I track the number of times I'm really loading my example file?

Thank you!

答:
Perhaps you should look into the hooks order and specify something that suits you better like before (:suite) or before (:context). Depending the one you use it will be executed

https://relishapp.com/rspec/rspec-core/docs/hooks/before-and-after-hooks

Body:
Today I'm tryng to speed up my tests suite. My application is basically a big integrator between systems so most of my tests are using Savon mocks like this

RSpec.describe MyClass do

  describe 'a function which sends a SOAP request'do
    before do
      savon.mock!
      savon.expects(action).returns(File.read("spec/fixtures/somefile.xml"))
    end

    after { savon.unmock! }

    it 'checks something'
    it 'checks something else'
    it 'checks something more'
    it 'checks something different'
  end
end

Obviously most of those tests are quite slow as they are loading a file. Moreover sometimes these mocks are inside nested contexts in order to combine multiple shared examples which increases the amount of loads.
Thinking to speed up some of these tests I tried to reduce the number of file loads moving them outside the before block. like this

RSpec.describe MyClass do

  describe 'a function which sends a SOAP request'do
    the_file = File.read("spec/fixtures/somefile.xml")

    before do
      savon.mock!
      savon.expects(action).returns(the_file)
    end

    after { savon.unmock! }

    it 'checks something'
    it 'checks something else'
    it 'checks something more'
    it 'checks something different'
  end
end

Indeed, the speed does not change; I have blocks of 96 tests with multiples, nested contexts and checks and I haven't gained not even 0.01 seconds. So my questions are:

I supposed the before block loads the file for each it, am I
right?

Does Rspec or Savon have some kind of cache?

How can I track the number of times I'm really loading my example file?

Thank you!


问:
XPath not locating element
What is wrong with my path to select the following:

```<code><label class="form-control-label" for="profile_form_state">State</label>
</code>```

Xpath:

```<code>xpath = '//label[ends-with(@for, "_state")]'
</code>```

I am using rspec and capybara

```<code> expect(rendered).to have_xpath(xpath)
</code>```

Error:

```<code>   xmlXPathCompOpEval: function ends-with not found
</code>```

答:
Try the following Xpath.</p>

```<code>xpath = '//label[contains(., "State")]'
</code>```

Title: XPath not locating element

Body:
What is wrong with my path to select the following:

State

Xpath:

xpath = '//label[ends-with(@for, "_state")]'

I am using rspec and capybara

 expect(rendered).to have_xpath(xpath)

Error:

   xmlXPathCompOpEval: function ends-with not found

Body:
Try the following Xpath.

xpath = '//label[contains(., "State")]'


问:
Check Box Value Not Working in Oracle Forms
I have check box on oracle forms

Value when checked = 'Y'

Value when unchecked = 'N'

By default value unchecked

I create button and write code when check box value 'N' the show message. But when I pressed the button then did not show message. When I changed the value to 'Y' then show message

CODE:

```<code>IF :WE_GROUP.CHECK_BOX_HOF = 'N' THEN
 MESSAGE('PLEASE CHECK THE BOX');
 MESSAGE(' ');
END IF;
</code>```

答:
Forms - as far as I can remember - behaves strange regarding the default checkbox value. Although you say that (if unchecked) it is equal to N, it is actually empty (null). I'd suggest you to try to set the default value upon form startup, for example in WHEN-NEW-FORM-INSTANCE trigger, e.g.

```<code>:we_group.check_box_hof := 'N';
</code>```

Alternatively/aditionally, in a trigger whose code you posted, use NVL function as

```<code>if nvl(:we_group.check_box_hof, 'N') = 'N' then
   ...
end if;
</code>```

Body:
I have check box on oracle forms

Value when checked = 'Y'

Value when unchecked = 'N'

By default value unchecked

I create button and write code when check box value 'N' the show message. But when I pressed the button then did not show message. When I changed the value to 'Y' then show message

CODE:

IF :WE_GROUP.CHECK_BOX_HOF = 'N' THEN
 MESSAGE('PLEASE CHECK THE BOX');
 MESSAGE(' ');
END IF;

Body:
Forms - as far as I can remember - behaves strange regarding the default checkbox value. Although you say that (if unchecked) it is equal to N, it is actually empty (null). I'd suggest you to try to set the default value upon form startup, for example in WHEN-NEW-FORM-INSTANCE trigger, e.g.

:we_group.check_box_hof := 'N';

Alternatively/aditionally, in a trigger whose code you posted, use NVL function as

if nvl(:we_group.check_box_hof, 'N') = 'N' then
   ...
end if;


问:
Run callback if/when image loaded
I want to know if looking at an image's height property is a reliable way to see if it has already loaded.

Let's say I've got a reference to an image, like this:

```<code>var img = document.getElementById('myImg');
</code>```

If I want to run some code that relies on the image being loaded, I can put it in a callback, and run img.onload = myCallback;.

But if the image has already loaded before this code runs, then the callback won't execute.

This is one possible solution:

```<code>if (img.height && img.height > 0) {
    myCallback();
}
else {
    img.onload = myCallback;
}
</code>```

In other words: run myCallback now if the image is already loaded, otherwise wait until it's loaded.

But I have a feeling this code might be fragile... Will it work in all cases? Is there a safer or more elegant way of doing this?

答:
You're looking for the complete property.  However, I don't think this works reliably in all Firefox versions, so it's good to add your height check as backup:

```<code>if (img.complete) {
    myCallback();
} else if (img.height && img.height > 0) {
    myCallback();
} else {
    img.onload = myCallback;
}
</code>```

Title: Run callback if/when image loaded

Body:
I want to know if looking at an image's height property is a reliable way to see if it has already loaded.

Let's say I've got a reference to an image, like this:

var img = document.getElementById('myImg');

If I want to run some code that relies on the image being loaded, I can put it in a callback, and run img.onload = myCallback;.

But if the image has already loaded before this code runs, then the callback won't execute.

This is one possible solution:

if (img.height && img.height > 0) {
    myCallback();
}
else {
    img.onload = myCallback;
}

In other words: run myCallback now if the image is already loaded, otherwise wait until it's loaded.

But I have a feeling this code might be fragile... Will it work in all cases? Is there a safer or more elegant way of doing this?

Body:
You're looking for the complete property.  However, I don't think this works reliably in all Firefox versions, so it's good to add your height check as backup:

if (img.complete) {
    myCallback();
} else if (img.height && img.height > 0) {
    myCallback();
} else {
    img.onload = myCallback;
}


问:
jQuery ajax not keeping order of array
I am converting a comma seperated string of ids and running each one through a simple WordPress ajax call. Like this....

JS

```<code>ids = '575,570,579';

var ids_array = ids.split(',');

ids_array.forEach(function(ids_array_item) {

    $.ajax({
        type   : 'POST',
        url    : 'myurl',
        data   : {
            action   : 'get_custom_content',
            customid : ids_array_item
        },
        success : function( response ) {

            if (response != '') {

                console.log('response : ' + response);

            }

        }

    });

});
</code>```

PHP

```<code>add_action('wp_ajax_get_custom_content', 'get_custom_content');
add_action('wp_ajax_nopriv_get_custom_content', 'get_custom_content');
function get_custom_content() {

    $customid = $_POST['customid'];
    echo $customid;
    die();

}
</code>```

I am expecting to get this back...

```<code>response : 575
response : 570
response : 579
</code>```

But I get this instead...

```<code>response : 575
response : 579
response : 570
</code>```

Does anybody have any idea why the order is being mangled when it is run through the ajax call?

答:
Ajax is asynchronous. Multiple HTTP requests to the same endpoint aren't guaranteed to get their responses in the same order as the requests were sent.

If order matters, then wait until all the responses are gathered (by putting the promise returned by $.ajax into Promise.all) before looping over them and logging them.

Alternatively, write an HTTP endpoint that can take multiple IDs in one go and return an array of results.

Title: jQuery ajax not keeping order of array

Body:
I am converting a comma seperated string of ids and running each one through a simple WordPress ajax call. Like this....

JS

ids = '575,570,579';

var ids_array = ids.split(',');

ids_array.forEach(function(ids_array_item) {

    $.ajax({
        type   : 'POST',
        url    : 'myurl',
        data   : {
            action   : 'get_custom_content',
            customid : ids_array_item
        },
        success : function( response ) {

            if (response != '') {

                console.log('response : ' + response);

            }

        }

    });

});

PHP

add_action('wp_ajax_get_custom_content', 'get_custom_content');
add_action('wp_ajax_nopriv_get_custom_content', 'get_custom_content');
function get_custom_content() {

    $customid = $_POST['customid'];
    echo $customid;
    die();

}

I am expecting to get this back...

response : 575
response : 570
response : 579

But I get this instead...

response : 575
response : 579
response : 570

Does anybody have any idea why the order is being mangled when it is run through the ajax call?


问:
add a class to the li element or the a?
I have a standard list of links:

```<code><li><a href="">one</a></li>
<li><a href="">two</a></li>
</code>```

If I want to add an extra class to one of the list items, should I add the class to the li or the a element? What is best practice?

Many thanks!

Cameron

答:
If you're styling the list item, such as in creating a menu, then add the class to the list item.  If you want to change the appearance of the link, add it to the anchor tag.

There's really no one right answer.  You should style the one that makes the most sense semantically.  In many cases you may need to style both the list item and the anchor, in which case you should add the class to the li tag and define your styles for both.

```<code>li.myclass {
    /* li styles */
}

li.myclass a {
    /* link styles */
}
</code>```

This is pretty much what alex suggested. The second style targets only those links wrapped in list items of your class.

OwnerDisplayName: Cameron

ContentLicense: CC BY-SA 2.5

Title: add a class to the li element or the a?

Body:
I have a standard list of links:

one

two

If I want to add an extra class to one of the list items, should I add the class to the li or the a element? What is best practice?

Many thanks!

Cameron

Body:
If you're styling the list item, such as in creating a menu, then add the class to the list item.  If you want to change the appearance of the link, add it to the anchor tag.

There's really no one right answer.  You should style the one that makes the most sense semantically.  In many cases you may need to style both the list item and the anchor, in which case you should add the class to the li tag and define your styles for both.

li.myclass {
    /* li styles */
}

li.myclass a {
    /* link styles */
}

This is pretty much what alex suggested. The second style targets only those links wrapped in list items of your class.

OwnerDisplayName: Bill the Lizard

LastEditorDisplayName: Bill the Lizard


问:
Hive partitioned table reads all the partitions despite having a Spark filter
I'm using spark with scala to read a specific Hive partition. The partition is year, month, day, a and b

scala> spark.sql("select * from db.table where year=2019 and month=2 and day=28 and a='y' and b='z'").show

But I get this error:

org.apache.spark.SparkException: Job aborted due to stage failure: Task 236 in stage 0.0 failed 4 times, most recent failure: Lost task 236.3 in stage 0.0 (TID 287, server, executor 17): org.apache.hadoop.security.AccessControlException: Permission denied: user=user, access=READ, inode="/path-to-table/table/year=2019/month=2/day=27/a=w/b=x/part-00002":user:group:-rw-rw----

As you can see, spark is trying to read a different partition and I don't have permisions there.

It shouldn't be, because I created a filter and this filter is my partition.

I tried the same query with Hive and it's works perfectly (No access problems)

Hive> select * from db.table where year=2019 and month=2 and day=28 and a='y' and b='z';

Why is spark trying to read this partition and Hive doesn't?

There is a Spark configuration that am I missing?

Edit: More information

Some files were created with Hive, others were copied from one server and pasted to our server with different permissions (we can not change the permissions), then they should have refreshed the data.

We are using:
cloudera 5.13.2.1
hive 1.1.0
spark 2.3.0
hadoop 2.6.0
scala 2.11.8
java 1.8.0_144

Show create table

```<code>|CREATE EXTERNAL TABLE Columns and type
PARTITIONED BY (`year` int COMMENT '*', `month` int COMMENT '*', `day` int COMMENT '*', `a` string COMMENT '*', `b` string COMMENT '*')
ROW FORMAT SERDE 'org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe'
WITH SERDEPROPERTIES (
 'serialization.format' = '1'
)
STORED AS
 INPUTFORMAT 'org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat'
 OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat'
LOCATION 'hdfs://path'
TBLPROPERTIES (
 'transient_lastDdlTime' = '1559029332'
)
|
</code>```

答:
A parquet hive table in Spark can use following 2 read flows -

Hive flow -
This will be used when spark.sql.hive.convertMetastoreParquet is set to false. For partitioning pruining to work in this case, you have to set spark.sql.hive.metastorePartitionPruning=true.

spark.sql.hive.metastorePartitionPruning: When true, some predicates
  will be pushed down into the Hive metastore so that unmatching
  partitions can be eliminated earlier. This only affects Hive tables
  not converted to filesource relations (see
  HiveUtils.CONVERT_METASTORE_PARQUET and
  HiveUtils.CONVERT_METASTORE_ORC for more information

Datasource flow - This flow by default has partition pruning turned on.

Body:
I'm using spark with scala to read a specific Hive partition. The partition is year, month, day, a and b

scala> spark.sql("select * from db.table where year=2019 and month=2 and day=28 and a='y' and b='z'").show

But I get this error:

org.apache.spark.SparkException: Job aborted due to stage failure: Task 236 in stage 0.0 failed 4 times, most recent failure: Lost task 236.3 in stage 0.0 (TID 287, server, executor 17): org.apache.hadoop.security.AccessControlException: Permission denied: user=user, access=READ, inode="/path-to-table/table/year=2019/month=2/day=27/a=w/b=x/part-00002":user:group:-rw-rw----

As you can see, spark is trying to read a different partition and I don't have permisions there.

It shouldn't be, because I created a filter and this filter is my partition.

I tried the same query with Hive and it's works perfectly (No access problems)

Hive> select * from db.table where year=2019 and month=2 and day=28 and a='y' and b='z';

Why is spark trying to read this partition and Hive doesn't?

There is a Spark configuration that am I missing?

Edit: More information

Some files were created with Hive, others were copied from one server and pasted to our server with different permissions (we can not change the permissions), then they should have refreshed the data.

We are using:
cloudera 5.13.2.1
hive 1.1.0
spark 2.3.0
hadoop 2.6.0
scala 2.11.8
java 1.8.0_144

Show create table

|CREATE EXTERNAL TABLE Columns and type
PARTITIONED BY (`year` int COMMENT '*', `month` int COMMENT '*', `day` int COMMENT '*', `a` string COMMENT '*', `b` string COMMENT '*')
ROW FORMAT SERDE 'org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe'
WITH SERDEPROPERTIES (
 'serialization.format' = '1'
)
STORED AS
 INPUTFORMAT 'org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat'
 OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat'
LOCATION 'hdfs://path'
TBLPROPERTIES (
 'transient_lastDdlTime' = '1559029332'
)
|


问:
Adding missing date rows to a BigQuery Table
I have a table where 1 of the rows is an integer that represents the rows time. Problem is the table isn't full, there are missing timestamps.

I would like to fill missing values such that every 10 seconds there is a row. I want the rest of the columns to be nuns (later I'll forward fill these nuns).

10 secs is basically 10,000.
If this was python the range would be:

```<code>   range(
      min(table[column]),
      max(table[column]),
      10000
   )
</code>```

答:
You can use arrays.  For numbers, you can do:

```<code>select n
from unnest(generate_array(1, 1000, 1)) n;
</code>```

There are similar functions for generate_timestamp_array() and generate_date_array() if you really need those types.

Body:
I have a table where 1 of the rows is an integer that represents the rows time. Problem is the table isn't full, there are missing timestamps.

I would like to fill missing values such that every 10 seconds there is a row. I want the rest of the columns to be nuns (later I'll forward fill these nuns).

10 secs is basically 10,000.
If this was python the range would be:

   range(
      min(table[column]),
      max(table[column]),
      10000
   )

Body:
You can use arrays.  For numbers, you can do:

select n
from unnest(generate_array(1, 1000, 1)) n;

There are similar functions for generate_timestamp_array() and generate_date_array() if you really need those types.


问:
No tray icon after run compiled program
When I run program from Qt Creator icon is visible in tray, but when I run compiled program from "relased" folder I see process of application but no icon in tray </p>

```<code>QSystemTrayIcon * tray = new QSystemTrayIcon( QIcon( "ok.png" ), this );
tray->setContextMenu( trayIconMenu );
tray->show();
tray->setVisible( true );
</code>```

答:
It looks like your image file ok.png was not deployed properly.
I would avoid the recommendation of using an absolute path.
Please introduce yourself with the Qt-ressource-system: it adds a layer ob indirection to manage ressources like graphics, sound files, .. and Qt will take care of proper deployment: https://doc.qt.io/qt-5/resources.html</p>

Title: No tray icon after run compiled program

Body:
When I run program from Qt Creator icon is visible in tray, but when I run compiled program from "relased" folder I see process of application but no icon in tray

QSystemTrayIcon * tray = new QSystemTrayIcon( QIcon( "ok.png" ), this );
tray->setContextMenu( trayIconMenu );
tray->show();
tray->setVisible( true );


问:
Taking too much time with large excel file with python
I have an excel file with 4.95 MB. I am trying to read this file with openpyxl
and perform some excel formula in this file. It is working great with small files. But as Soon as I started working with the large file. It takes too much time as if seems like it moves to an infinite Condition. Kindly let me know with some approach or what modification should I do in this piece of code.</p>

```<code>import openpyxl
import os
import xlrd
import xlsxwriter
#reading the file i.e. 4.5 Mb
wb = openpyxl.load_workbook(r"type_example.xlsx")
Sheet = wb.get_sheet_by_name('sheet1')

for row,cellObj in enumerate(Sheet["C"],1):
    cellObj.value='=IF(OR(B{0}="J", B{0} ="U", B{0} ="s"),"s", IF(OR(B{0} ="I", B{0} ="M", B{0} ="N", B{0}="O", B{0} ="Q", B{0} ="U", B{0} ="V"), "J", "des"))'.format(row)
    Sheet.cell(row=1, column=3).value = 'Dt'
    #path to save file
    wb.save(r'\Documents\test_1.xlsx')
</code>```

答:
I had a quick look around and found that the issue may be because you're going cell-by-cell.

It seems that openpyxl will basically do a linear search through the entire document to find your desired cell if you do things like that (which is not ideal), and you need to use a generator if you want to speed it up.

It doesn't look too hard, but I'm not particularly skilled in python. So, rather than try to explain the concept myself, I'll leave you this:

https://blog.davep.org/2018/06/02/a_little_speed_issue_with_openpyxl.html

Hope it helps.

Body:
I have an excel file with 4.95 MB. I am trying to read this file with openpyxl
and perform some excel formula in this file. It is working great with small files. But as Soon as I started working with the large file. It takes too much time as if seems like it moves to an infinite Condition. Kindly let me know with some approach or what modification should I do in this piece of code.

import openpyxl
import os
import xlrd
import xlsxwriter
#reading the file i.e. 4.5 Mb
wb = openpyxl.load_workbook(r"type_example.xlsx")
Sheet = wb.get_sheet_by_name('sheet1')

for row,cellObj in enumerate(Sheet["C"],1):
    cellObj.value='=IF(OR(B{0}="J", B{0} ="U", B{0} ="s"),"s", IF(OR(B{0} ="I", B{0} ="M", B{0} ="N", B{0}="O", B{0} ="Q", B{0} ="U", B{0} ="V"), "J", "des"))'.format(row)
    Sheet.cell(row=1, column=3).value = 'Dt'
    #path to save file
    wb.save(r'\Documents\test_1.xlsx')


问:
Does a Python-like virtualenv exist in Julia?
Is there a Python-like virtualenv environment simulator for Julia where one can do development in a local, virtual environment?</p>

答:
Nowadays Julia has this built into its package manager. They're called environments. It boils down to this:

Enter the package mangement REPL by hitting ]

Use the command activate $dir to switch to the environment described in $dir

Use the command instantiate to install the packages described in the environment


问:
django : Change default value for an extended model class
I posted a similar question a while earlier, but this one is different. I have a model structure of related classes like:

```<code>class Question(models.Model):
     ques_type = models.SmallIntegerField(default=TYPE1, Choices= CHOICE_TYPES)

class MathQuestion(Question):
     //Need to change default value of ques_type here
     // Ex: ques_type = models.SmallIntegerField(default=TYPE2, Choices= CHOICE_TYPES)
</code>```

I want to change the default value of ques_type in the derived class. How should i accomplish this?

答:
Examples above are for proxy models. If you need to change default for model inherited from non-abstract base model you can do following:

from django.db import models

class Base(models.Model):
    field_name = models.CharField(...)

class Child(Base):
    def __init__(self, *args, **kwargs):
        kwargs['field_name'] = kwargs.get('field_name') or 'default value'
        super().__init__(*args, **kwargs)
```

Which will set default if it wasn't passed directly on Model(...) or Model.objects.create(...).

Body:
I posted a similar question a while earlier, but this one is different. I have a model structure of related classes like:

class Question(models.Model):
     ques_type = models.SmallIntegerField(default=TYPE1, Choices= CHOICE_TYPES)

class MathQuestion(Question):
     //Need to change default value of ques_type here
     // Ex: ques_type = models.SmallIntegerField(default=TYPE2, Choices= CHOICE_TYPES)

I want to change the default value of ques_type in the derived class. How should i accomplish this?

Body:
Examples above are for proxy models. If you need to change default for model inherited from non-abstract base model you can do following:

from django.db import models

class Base(models.Model):
    field_name = models.CharField(...)

class Child(Base):
    def __init__(self, *args, **kwargs):
        kwargs['field_name'] = kwargs.get('field_name') or 'default value'
        super().__init__(*args, **kwargs)

Which will set default if it wasn't passed directly on Model(...) or Model.objects.create(...).


问:
Why to put a property variable in the constructor
From other languages I am used to code a class property and afterwards I can access this without having it in the constructor like

```<code>Class MyClass:
    def __init__(self):
        self._value = 0
    @property
    my_property(self):
        print('I got the value:' & self._value)
</code>```

In almost every example I worked through the property variable was in the constructor self._value like this

```<code>Class MyClass:
    def __init__(self, value = 0):
        self._value = value
</code>```

To me this makes no sence since you want to set it in the property. Could anyone explain to me what is the use of placing the value variable in the constructor?

答:
I'm guessing you're coming from a language like C++ or Java where it is common to make attributes private and then write explicit getters and setters for them? In Python there is no such thing as private other than by convention and there is no need to write getters and setters for a variable that you only need to write and read as is. @property and the corresponding setter decorators can be used if you want to add additional behaviour (e.g. logging acccess) or you want to have pseudo-properties that you can access just like real ones, e.g. you might have a Circle class that is defined by it's radius but you could define a @property for the diameter so you can still write circle.diameter.

More specifically to your question: You want to have the property as an argument of the initializer if you want to set the property at the time when you create the object. You wouldn't want to create an empty object and then immediately fill it with properties as that would create a lot of noise and make the code less readable.

Just an aside: __init__ isn't actually a constructor. The constructor for Python objects is __new__ and you almost never overwrite it.

Body:
From other languages I am used to code a class property and afterwards I can access this without having it in the constructor like

Class MyClass:
    def __init__(self):
        self._value = 0
    @property
    my_property(self):
        print('I got the value:' & self._value)

In almost every example I worked through the property variable was in the constructor self._value like this

Class MyClass:
    def __init__(self, value = 0):
        self._value = value

To me this makes no sence since you want to set it in the property. Could anyone explain to me what is the use of placing the value variable in the constructor?


问:
Creating address_list array in phpBB?
Can someone explain how address_list works in phpBB? I'm attempting to create a small function for automatically inserting private messages and think I have it up to this point:

We'll say my current user array looks like this:

$users = array('100','150','77','94')

where each number is a user's ID.

current address_list looks like this:

'address_list'      => array ('u' => array(2 => 'to'))

Yes, it has been taken directly from http://wiki.phpbb.com/Using_phpBB3%27s_Basic_Functions#1.4.7._Inserting_Posts_and_Private_Messages

As far as I can tell, the explanation is telling me that it uses a two-dimensional array, but I don't need to send to groups, and I'm not even sure how to stick a two-dimensional array into that equation. All I want to do is send to the first userid on that list, and the BCC all the others.

Then again, phpBB's documentation has always been near-impossible for me to follow.

Any and all help is appreciated.

答:
The format is as follows:

```<code>'address_list' => array(
    'u' => array(2 => 'to', 3 => 'bcc'),
    'g' => array(40 => 'to', 41 => 'bcc'),
)
</code>```

u contains a mapping of user_id => recipient_type.

g contains a mapping of group_id => recipient_type.

A recipient type can be either to or bcc.

This example will send the PM to user 2 and group 40, and also send a BCC to user 3 and group 41.

Title: Creating address_list array in phpBB?

Body:
The format is as follows:

'address_list' => array(
    'u' => array(2 => 'to', 3 => 'bcc'),
    'g' => array(40 => 'to', 41 => 'bcc'),
)

u contains a mapping of user_id => recipient_type.

g contains a mapping of group_id => recipient_type.

A recipient type can be either to or bcc.

This example will send the PM to user 2 and group 40, and also send a BCC to user 3 and group 41.


问:
Access a Java variable from Jenkins
I am trying to find out if I could get the value of a variable declared inside a Java program or maven-plugin and store it in a Jenkins environment variable.

This is because the Jenkins file has to create a new git branch from dev and call it Release-9.0.86 for example but the version number is inside a pom.xml.

I have already written a maven-plugin that retrieves the version from the pom.xml an writes it into another XML file, but I need to know if I could send the version to Jenkins to handle it.

答:
You could perhaps create a properties file instead of a xml file with the needed information. Example contents of created branch-name.properties file:

```<code>NEW_BRANCH_NAME=Release-9.0.86
</code>```

Then you can add a build step "Inject environment variables" after the one that produces the properties file, and configure that to read the properties file you just created.

```<code>Properties File Path: branch-name.properties
</code>```

After that you can use the environment variables as usual in the jenkins build, for example a "Execute shell" build step that creates the branch:

git branch ${NEW_BRANCH_NAME}
```

<p>To then push the newly created branch you could add a "Git Publisher" <strong>post-build action</strong>.</p>

```Branch to push: ${NEW_BRANCH_NAME}
```

Please note that it might be useful to configure Git Publisher to "Push Only If Build Succeeds" i.e. branch will not be created if the build fails for any reason.

Title: Access a Java variable from Jenkins

Body:
You could perhaps create a properties file instead of a xml file with the needed information. Example contents of created branch-name.properties file:

NEW_BRANCH_NAME=Release-9.0.86

Then you can add a build step "Inject environment variables" after the one that produces the properties file, and configure that to read the properties file you just created.

Properties File Path: branch-name.properties

After that you can use the environment variables as usual in the jenkins build, for example a "Execute shell" build step that creates the branch:

git branch ${NEW_BRANCH_NAME}

To then push the newly created branch you could add a "Git Publisher" post-build action.

Branch to push: ${NEW_BRANCH_NAME}

Please note that it might be useful to configure Git Publisher to "Push Only If Build Succeeds" i.e. branch will not be created if the build fails for any reason.


问:
Create multiple background button
I'm trying to do a button that have a double background, one that contains an icon (with fixed width) and the rest is normal text.

What I got for now is:

.button {
  position: relative;
  display: inline-block;
  background-color: red;
  height: 50px;
  line-height: 50px;
  padding-left: 30px;
}

span {
  background-color: blue;
  position: absolute;
  left: 0;
  width: 30px;
  text-align: center;
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><link href="https://use.fontawesome.com/releases/v5.8.2/css/all.css" rel="stylesheet"/>

<div class="button">
  <span>x</span> Test
</div>

<div class="button">
  <span><i class="fas fa-address-card"></i></span> Test
</div></code>```

The problem of that method is that a 1px border is shown at the bottom and seems not very stable as method. How can I achieve this effect cleanly? Thanks

答:
Set the height of the span to 100%

.button {
  position: relative;
  display: inline-block;
  background-color: red;
  height: 50px;
  line-height: 50px;
  padding-left: 30px;
}

span {
  background-color: blue;
  position: absolute;
  left: 0;
  height:100%;
  width: 30px;
  text-align: center;
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><link href="https://use.fontawesome.com/releases/v5.8.2/css/all.css" rel="stylesheet"/>

<div class="button">
  <span>x</span> Test
</div>

<div class="button">
  <span><i class="fas fa-address-card"></i></span> Test
</div></code>```

Title: Create multiple background button

Body:
I'm trying to do a button that have a double background, one that contains an icon (with fixed width) and the rest is normal text.

What I got for now is:

.button {
  position: relative;
  display: inline-block;
  background-color: red;
  height: 50px;
  line-height: 50px;
  padding-left: 30px;
}

span {
  background-color: blue;
  position: absolute;
  left: 0;
  width: 30px;
  text-align: center;
}

<link href="https://use.fontawesome.com/releases/v5.8.2/css/all.css" rel="stylesheet"/>

  x Test

   Test

The problem of that method is that a 1px border is shown at the bottom and seems not very stable as method. How can I achieve this effect cleanly? Thanks

Body:
Set the height of the span to 100%

.button {
  position: relative;
  display: inline-block;
  background-color: red;
  height: 50px;
  line-height: 50px;
  padding-left: 30px;
}

span {
  background-color: blue;
  position: absolute;
  left: 0;
  height:100%;
  width: 30px;
  text-align: center;
}

<link href="https://use.fontawesome.com/releases/v5.8.2/css/all.css" rel="stylesheet"/>

<div class="button">
  <span>x</span> Test
</div>

<div class="button">
  <span><i class="fas fa-address-card"></i></span> Test
</div>


问:
Best UML designing tool in Linux?
Urgently want to know best UML designing tool in Linux?
Which Also Support ERD
Is it Possible to use in NetBEANs IDE 6.9.1</p>

答:
Almost all UML tools are Linux compliant.
Try Papurus with Eclipse or BOUML which are free and open source. RSA is pretty good as well as Omondo but not free.</p>

Title: Best UML designing tool in Linux?