Does everything after my try statement have to be encompassed in that try statement to access variables within it?

Question: I'm learning java and one thing I've found that I don't like, is generally when I have code like this:
import java.util.*;
import java.io.*;

public class GraphProblem
{
    public static void main(String[] args)
    {
        if (args.length < 2)
        {
            System.out.println("Error: Please specify a graph file!");
            return;
        }

        FileReader in = new FileReader(args[1]);
        Scanner input = new Scanner(in);

        int size = input.nextInt();
        WeightedGraph graph = new WeightedGraph(size);

        for(int i = 0; i < size; i++)
        {
            graph.setLabel(i,Character.toString((char)('A' + i)));
        }

        for(int i = 0; i < size; i++)
        {
            for(int j = 0; j < size; j++)
            {
                graph.addEdge(i, j, input.nextInt());
            }
        }

        // .. lots more code

    }
}

I have an uncaught exception around my FileReader.
So, I have to wrap it in a try-catch to catch that specific exception.  My question is  does that try { }  have to encompass everything after that in my method that wants to use either my FileReader (in) or my Scanner (input)?
If I don't wrap the whole remainder of the program in that try statement, then anything outside of it can't access the in/input because it may of not been initialized or has been initialized outside of its scope.  So I can't isolate the try-catch to just say the portion that intializes the FileReader and close the try statement immediately after that.
So, is it the "best practice" to have the try statement wrapping all portions of the code that are going to access variables initialized in it?
Thanks!
Answer: If you are comfortable with not wrapping the code after the FileReader constructor, you can declare the FileReader outside of the try/catch block, like this:
FileReader fr = null;
try
{
    fr = new FileReader(args[1]);
}
catch (IOException e)
{
    // handle
}
// code that uses fr

This is a reasonable design, and I use it often.  Make sure you properly handle, in the following code, the possibility that fr is null (i.e. the constructor threw an exception).
Comment: The variable `fr` must be initialized - this won't compile unless the exception handling code is guaranteed to return.
Comment: Shouldn't you also have a finally to see if the fr is not null and call close on it?
Comment: @CoolBeans: under what circumstances would an exception be thrown *and* `fr` be not null?  The OP will need to manage the FileReader lifetime properly, of course, but that's outside the scope of this example.
Answer: This is not an issue with try/catch blocks as such. The issue is variable scoping and that you must have a try/catch block because of the checked exception and thus establish a new scope.
You also have another option - declare the checked exception(s) as throws from your method.
public static void main(String[] args) throws IOException {
    // ...code here...
}

This is perfectly legal for a main method.
If you do want to handle the exception, as you ought to in a larger program. You can define a specific try/catch around the problem block of code and use the variable outside of the scope by declare the variable outside of that scope as many have answered:
FileReader fr = null; // must be initialized here if the exception handling code
                      // does not exit the method
try {
    fr = new FileReader(fileName);
} catch (IOException ex) {
    // log, print, and/or return
    // if you return or exit here then subsequent code can assume that fr is valid
}

You can also put the move the code to another method that deals with the exception:
private static FileReader openReader(String fileName) {
    try {
        return new FileReader(fileName);
    } catch (IOException ex) {
        // log/print exception
        return null; // caller must then expect a null
        // or
        throw new RuntimeException(...); // throw a RuntimeException of some kind (may not be good practice either)
    }
}

You could also move the file processing code to another method. This may be better and allow you to more correctly follow the open/close in finally idiom:
FileReader fr = null;
try {
    fr = new FileReader(fileName);
    Scanner input = new Scanner(fr);

    processInput(input);
} catch (IOException ex) {
    // log/print exception
} finally {
    if (fr != null) {
        try {
            fr.close();
        } catch (IOException ex) {
            // empty
        }
    }
}

private static void processInput(Scanner in) throws IOException {
    // ...processing here
}

For the close part, you could use a third party library (Apache File Utils) or write a simple method to provide a static safe close method that doesn't throw exceptions.
You really should look at breaking up large methods into smaller units and this will often provide you with a much clearer way of handling the exceptions as well.
Comment: One drawback of this method is that the Java default `printStackTrace()` doesn't print nested exceptions, so you just see the last one (which is especially irritation about `SQLException`s when the first exception says "Connection failed. See the next exception for details". Use commons-lang's `ExceptionUtils` or your own method to dump the whole tree.
Comment: @Aaron: Which one of them? I present several and don't assume anything in what is done within the exception handler. I also state "log" as an option; logging APIs generally handle nested exceptions. I didn't reference printStackTrace either. Is this comment on the right answer?
Comment: You're implicitly using `printStackTrace()` if you write `void main(String[] args) throws IOException`.
Answer: No. You can declare it like this:
FileReader in = null;
Scanner input = null;

try {
   in = new FileReader(args[1]);
   input = new Scanner(in);
} catch(IOException ioe) {
   //
}

Answer: Yes and no. The Try initiates it's own local scope so when you do this:
try
{
   FileReader reader = new FileReader(args[1]);

   // Stuff
}
catch(Exception e) { }

The reader variable is only visible within the constraints of the try {} block. This is preferred behaviour. If you want to use it outside (in the case of file i/o it's not reccomended) you need to declare it outside of the try/catch. A proper example would be a flag:
boolean isValidFile = false;
try
{
   FileReader reader = new FileReader(args[1]);

   // Do Stuff

   isValidFile = true;
}
catch(Exception e)
{
   // Handle Errors
}

System.out.print("Valid File: ");
System.out.println(isValidFile);

Answer: If you are just learning, you can have your method throw Exception. It isn't a great way to handle exceptions however, if you are just learning, you probably want to focus more on the logic of what your code is doing. Once you are comfortable with that, then you can look into handling exceptions properly.
So your code should look like the following and you won't have to worry about try/catch statements:
public class GraphProblem {
    public static void main(String[] args) throws Exception {
        //your code
    }
}

Again, this is not a best practice but it allows you to focus on your business logic rather than error handling. Once you get comfortable with the business logic, then you can dig into exception handling which is a topic onto itself.
Answer: Local variables declared within a block are scoped to that block.

So, is it the "best practice" to have the try statement wrapping all portions of the code that are going to access variables initialized in it?

In general, you should try to minimize the scope of your variables to make them as narrow as possible. This is covered in Effective Java and Code Complete, among others.
This sort of code is a recipe for NullPointerExceptions:
FileReader in = null;
try {
  in = new FileReader(filename);
} catch(IOException e) {
  //handle error, but now what?
}
// Code that needs "in", but what if it is null
// because of a FileNotFoundException?
// This code is junk.

Virtually all local variable declarations should include an assignment.
FileReader in = new FileReader(filename);

If you follow this rule of thumb, you'll end up with better code.

// .. lots more code

If you have giant try/catch statements, it sounds like your methods are too big. This is a code organization issue rather than something to do with try/catch specifically.
  public static void main(String[] args) {
    if (args.length < 1) {
      System.out.println("Error: Please specify a graph file!");
      return;
    }
    try {
      processGraphFile(args[0]);
    } catch (IOException e) {
      // Deal with error
    }
  }

  private static void processGraphFile(String filename) throws IOException {
    FileReader in = new FileReader(filename);
    try {
      Scanner scanner = new Scanner(in);
      processGraph(scanner);
      if (scanner.ioException() != null) {
        throw scanner.ioException();
      }
    } finally {
      in.close();
    }
  }

  //other methods

As an aside:

Note that Java arrays start at index 0, not 1
The FileReader is convenient while learning, but should generally be avoided due to encoding issues; this is unlikely to be a problem until the code leaves your machine
Answer: You should definitely follow the pattern above.  If I remember right from my SCJP studying, the compiler doesn't try to optimize anything that is in a try block, so you should try-catch at the lowest level possible.
Comment: Is this a comment or an answer? And don't use "above". Answers have no canonical ordering.


Show that $\frac{\int_t^\infty (u-t)f(u)du}{S(t)}=\frac{\int_t^\infty S(u)du}{S(t)}$

Question:
Show that $$vmr(t)=\frac{\int_t^\infty
 (u-t)f(u)du}{S(t)}=\frac{\int_t^\infty S(u)du}{S(t)}$$ Suggestion: Use
  integration by parts knowing that $f(u)du=-dS(u)$

First some explanations,
Let $T$ a continous non-negative random variable that represents a failure time, where $S(t)=P(T\geq t)$ with $f(t)$ probability density function of $T$.
What I tried:
Let $a=(u-t)$ and $db=f(u)du$, then $da=du$ and $b=\int_t^\infty f(u)du=S(u)$
$$ab-\int_t^\infty b\space da$$
so
$$vmr(t)=\frac{\int_t^\infty (u-t)f(u)du}{S(t)}=\frac{1}{S(t)}\Big((u-t)S(u)-\int_t^\infty S(u)du\Big)$$
EDIT:
$(u-t)S(u)\Big|_t^\infty =0$  since $\lim_{u\rightarrow \infty}S(u)\rightarrow 0$ because $P(T\geq \infty)\rightarrow 0$.
But I still don't get the result because the $-$ signal.
$$vmr=-\frac{\int_t^\infty S(u)du}{S(t)}$$
I probably need to use the fact that $f(u)du=-dS(u)$ but I can't figure out what this $-dS(u)$ mean.
Comment: In your final line, it should be

$$\text{vmr}(t) = \frac{1}{S(t)} \left[ (t-u) S(u) \bigg \rvert_{t}^{\infty} - \int_{t}^{\infty} S(u) du \right]$$

and what can you say about $\lim_{u \to \infty} (t-u) S(u)$ using the definition of $S(t)$?
Answer: Let $I(t)=\int_t^\infty (t'-t)f(t')\,dt'$.  Integrating $I(t)$ by parts with $u=t'-t$ and $v=\int^{t'}f(t'')\,dt''$, so that $du=dt'$ and $dv=-S(t')$, yields
$$\begin{align}
I(t)&=\int_t^\infty (t'-t)f(t')\,dt'\\\\
&=\left.\left(-(t'-t)S(t')\right) \right|_t^\infty +\int_t^\infty S(t')\,dt'\\\\
&=\int_t^\infty S(t')\,dt'
\end{align}$$
where we used $\lim_{t'\to \infty}t'S(t')=0$.


I want to develop an music player app for android with multitasking so dat i can use differtent apps without closing music player......help out plzz

Question: I want to develop an music player app for android with multitasking so dat i can use differtent apps without closing music player......help out plzz..
Comment: What exactly is your question?
Comment: "Can u send me the codez?" :)
Answer: To have it to continue to play music in the background, you will need two components to your app.  An Activity component that will display a UI to control the music player, and a Service component which is playing the music.  The Activity component controls the Service component.
See here for more details:
http://developer.android.com/guide/topics/fundamentals.html


Deploy a process with multitenancy support using Activiti Explorer

Question: Since Activiti 5.15 added multitenancy support, i have used the Activiti API to deploy process with tenantId.
I want to know if there is a way to do that using Activiti Explorer ?
Answer: "No - Explorer is not multi-tenant aware". Joram Barrez, Activiti Core Developer


Barcode not printing correctly using JSBarcode library

Question: I am trying to print a generated barcode. I am using the JSBarcode library to generate the barcode.
On the print screen, visuality is good but when I print it, the result is 4 pages and just one page has the barcode, and the barcode is very little and at the bottom of the page.
My html code is:

      <svg id="barcode tekli"></svg>

My JS code is:
JsBarcode("#barcode", "1554466", {
    width:5,
    height:200,
    fontSize:50,
    displayValue: true
});

window.print();

window.open('', '_parent', '');

window.close();

My CSS is:
@media print {
    .tekli{
        -webkit-transform: scaleX(2);
        -o-transform: scaleX(2);
        -ms-transform: scaleX(2);
        transform: scaleX(2);
    }
    .page {
        width: 21cm;
        height: 29.7cm;
        margin: 0;
        border: initial;
        border-radius: initial;
        width: initial;
        min-height: initial;
        box-shadow: initial;
        background: initial;
        page-break-after: always;
    }
    .col-print-12 {
        width: 100%;
    }
}

Can you help me please?
Answer: Try this
$(document).ready(function(){
    JsBarcode("#barcode", "1554466",{
        width:5,
        height:200,
        fontSize:50,
        displayValue: true

    });

    window.print();

    window.open('', '_parent', '');

    window.close();
});

or to delete svg tag and replace with a simple div with id="barcode"
let divDOM = document.getElementById("barcode");
let svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
svg.setAttribute('jsbarcode-format', 'ean13')
svg.setAttribute('jsbarcode-value', '<?php echo $product["ean"]; ?>')
svg.className.baseVal = "barcode";
divDOM.appendChild(svg);
JsBarcode("#barcode","Smallest width",{
    width: 1,
    height: 25,
    lineColor: "#676a6c"
}).init();

Answer: Your code with minor changes in html/CSS working fine for me.
I have removed id tekli from svg (you can't have multiple Ids) and also removed .tekli style from CSS.

JsBarcode("#barcode", "1554466",{
    width:5,
    height:200,
    fontSize:50,
    displayValue: true

});

window.print();

window.open('', '_parent', '');

window.close();
   @media print {

        .page {
            width: 21cm;
            height: 29.7cm;
            margin: 0;
            border: initial;
            border-radius: initial;
            width: initial;
            min-height: initial;
            box-shadow: initial;
            background: initial;
            page-break-after: always;
              }
             .col-print-12 {
            width: 100%;
             }
              }

                <svg id="barcode"></svg>

You can also test it here
Comment: .page not used above example


Find Server from where to download Cerificate for Wi-Fi authentication

Question: I am connected to my office network. Would like my PDA to connect to office wi-fi too. However even with credentials it says I need a certificate to connect to network.
I have an option to download certificate in pda from a server, however it needs server name or location I guess. Is there a way I can figure that out from my windows account from my laptop, that which server would be dispensing cerificates for authentication?
Answer: You're probably best off asking your netadmin(s) for this information directly.


How to elegantly restrict a template argument to be a `>`?

Question: How to restrict a template argument of Wrapper to be a Wrapper<Any,MyArray<AnyT>> elegantly?

Don't break content assist (Visual Studio).
High readability.  Not use a hacky approach.
For some reasons, most solution love to hack.
Make it obvious at the very first line by using C++ syntax rule.   (not just green comment)

As far as I know, there are many solutions, but every solution doesn't meet the criteria.
Workaround 1 (template specialization, fail 1)
template<class T> class MyArray{};
template<class T,class T2> class Wrapper;
template<class T,class T2> class Wrapper<T,MyArray<T2>>{
    using Test=int;
};
class B{};
class C{};
int main() {
    Wrapper<C,MyArray> wrapper;
    return 0;
}

This code is modified from https://stackoverflow.com/a/43518221 (@max66).
Context clue / syntax highlighting of IDE will be confused.
In my case, it marks some correct types as error e.g. :-
class ShowError : public Wrapper<B,MyArray<C>>{
    Test n=0;  //<-- unknown "Test" (intellisense)
};

Workaround 2 (some hacky field/typedef, fail 2)
template<class T> class MyArray{
    public: using MyArrayT=T;
};
template<class T,class T2> class Wrapper{
    public: using myT=typename T2::MyArrayT;
    //^ assert at compile time
};

This idea come from a comment in https://stackoverflow.com/a/43518295 (@Jarod42)
The class declaration doesn't mention about MyArray, it just uses a hacky (less readable) way (MyArrayT) to enforce that T2 is MyArray.
Workaround 3 (base class, fail 2)
class MyArrayBase{};
template<class T> class MyArray : public MyArrayBase{  };
template<class T,class T2> class Wrapper{
    //check something around MyArrayBase *object = new T2();
    // or "is_base_of"
};

The code is modified from Restrict C++ Template Parameter to Subclass and C++ templates that accept only certain types.
It has same disadvantage as workaround 2.
It is not obvious for common user.
Workaround 4 (SNIFAE, fail 1)
By adding std::enable_if on the template class declaration (Wrapper), I can get a working hack.
Unfortunately, content assist hate it.
Reference
Here are the other links that I read :-

http://www.informit.com/articles/article.aspx?p=376878 (template template parameter)
restrict a template function, to only allow certain types (not related to template type as a parameter)
Answer: You can write a custom type trait is_specialization, as follows:
template<class Type, template<class...> class Template>
struct is_specialization
  : std::false_type {};
template<template<class...> class Template, class... TArgs>
struct is_specialization<Template<TArgs...>, Template>
  : std::true_type {};

Then you just need to static_assert that is_specialization is true for the given template argument:
template<class T,class T2>
class Wrapper {
    static_assert(is_specialization<T2, MyArray>::value, "T2 must be a specialization of MyArray");
};


Want to delete some custom user data while delete any user in Drupal8

Question: Is there any Hook available for user delete in Drupal8.
I have some custom data in a custom table and I want to delete those data while I delete any user in Drupal8. Can anybody help me out of this?
Answer: Use hook_entity_delete().
https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21entity.api.php/function/hook_entity_delete/8.6.x
function mymodule_entity_delete(Drupal\Core\Entity\EntityInterface $entity) {
  if ($entity->getEntityType() == 'user') {
     ...
  }
}


How do you allow users to create own menus?

Question: The idea is that a user should be able to create a menu to structure his uploaded content. How would you go about making this possible without giving the user access to edit other users menus?
Cheers,
Comment: As far as I know there is no out of the box solution for that, it would have to be custom coded. Someone else may know better though.
Comment: There is a Drupal 6 module [Bookmarks](https://www.drupal.org/project/bookmarks) for that from which you may draw inspiration. I see that they plan to integrate it into Advanced Menu.
Comment: Can't u use the core shortcut module somehow? That works on a per user base and allows adding and removing pages to your list.
Answer: Menu Admin Per Menu Module comes to solve this purpose!!

The Drupal 7 Menu Admin Per Menu module is a simple module that allows
  you to grant permissions for users to administer specific Drupal menus
  based on their role.
This module for instance would allow you to allow one role to modify
  the Main menu, while not allowing the role to modify a separate menu
  on your Drupal 7 website.

Watch this Screencast to learn about it : How to allow a specific role to administer the user menu on a Drupal site without giving them access to modify the Main menu
Comment: But this is for the global menus? OP wants a per-user menu.


How many numbers between 1-10000 cannot be divided by 2 or 3?

Question:
I know that $5000$ are divisible by $2$ and $3333$ are divisible by $3$, but this will include repeats.
I figured out that for numbers divisible by $3$, $1/3$ of them are divisible by $2$, so for the numbers divisible by $3$ that aren't divisible by $2$ will be $2222$.

$5000$ + $2222$ = $7222$ are divisible by $2$ or $3$, giving a final answer of $2778$.
What do you guys think $?$.
Comment: This is the principle of inclusion-exclusion.  Who's divisible by 2, 3, or both (6)?
Comment: For numbers divisible by $3$, as for integers in general, $\frac12$ of them are divisible by $2$.
Comment: @Randall Sorry, edited the question, should say "cannot"
Comment: Doesn't really matter.  If you know who can, you know who cannot.  Same question, really.
Comment: @Randall 10000 - (5000 + 3333) + 1666 = 3333 ?
Answer: Let $M_d = \{\text{integers } n \text{ divisible by } d \text{ such that } 0 < n \leq 10\,000 \}$. Then you're looking for the size of the complement of the set $M_2 \cup M_3$. But, as you've observed, these sets overlap. In fact,
$$
| M_2 \cup M_3 | = | M_2 | + | M_3 | - | M_2 \cap M_3 |.
$$
What about that last term? You want numbers divisible by $2$ and by $3$, namely those divisible by $6$. (In general, a number is divisible by integers $a$ and $b$ if and only if it's divisible by $\operatorname{lcm}(a, b)$.)
So, $M_2 \cap M_3 = M_6$, and
$$
| M_2 \cup M_3 | = | M_2 | + | M_3 | - | M_6 |.
$$
How big are these sets of multiples? As you've implicitly indicated in your question,
$$
| M_d | = \biggl\lfloor \frac{10\,000}{d} \biggr\rfloor,
$$
where $\lfloor x \rfloor$ is the floor function that rounds numbers down to the greatest integer $m$ satisfying $m \leq x$. Can you put this all together? Try it yourself before revealing the spoiler:

\begin{align} | (M_2 \cup M_3)^{\mathrm{c}} | &= 10\,000 - | M_2 \cup M_3 | \\ &= 10\,000 - \bigl( | M_2 | + | M_3 | - | M_6 | \bigr) \\ &= 10\,000 - \biggl\lfloor \frac{10\,000}{2} \biggr\rfloor - \biggl\lfloor \frac{10\,000}{3} \biggr\rfloor + \biggl\lfloor \frac{10\,000}{6} \biggr\rfloor \\ &= 10\,000 - 5000 - 3333 + 1666 \\ &= 3333\end{align}
Comment: Oh, nice catch, @N.F.Taussig! Thanks, it's corrected now.
Answer: How do you figure that "for numbers divisible by $3$, $1/3$ of them are divisible by $2$"? I get $1/2$.
You didn't say but I'm guessing that your "between" is inclusive, so that the number $1$ is included in the count. You could use the in-and-out formula $|A\cup B|=|A|+|B|-|A\cap B|$ but you don't have to.
Observe that, of any six consecutive integers, exactly two of them are divisible neither by $2$ nor by $3$. Now $10000=1666\cdot6+4$, i.e., $1666$ groups of six consecutive integers, plus four at the end. To make things easy look at the four numbers at the little end: $1$, $2$, $3$, $4$. Only one of them (namely $1$) is good, so the final answer is $1666\cdot2+1=\boxed{3333}$. If you're a Big-Endian, of the four numbers at the big end, $9997$, $9998$, $9999$, $10000$, only one of them (namely $9997$) is divisible by neither $2$ nor $3$, so you get the same answer.
Comment: Yes if you read my reply to Randall you would have seen I got the right answer bud, cheers


Add a condition to show menu link in YML

Question: how should I do if I need to show to a link menu based on a condition ?
This is my YML for my menu link :
double_vue_klesia.backuserswitch:
  title: 'Retour user précédent'
  menu_name: navigation-principale
  description: 'Switch back to your user account.'
  route_name: double_vue_klesia.backuser.switch
  weight: 1

I want to show this link only :
if (\Drupal::service('userswitch')->isSwitchUser())

This is in PHP, but I need this to be on YML (unless there a way to intercept it in php and hide it ?).
If my isSwtichUser is true (from my service), I want to show the link. But I can find a way to do it.
EDIT :
I'm using this module : https://www.drupal.org/project/userswitch
I "just" need to display a link to go back to the original user (link accessible from my frontend)
Comment: You show/hide menu links by allowing/denying access to the target route. So the first (now deleted) answer of @QuanLee is correct.
Comment: I tried was @QuanLee says, but it wasn't working :/
Comment: Can be another issue. You need to add cache data. Don't know the cache dependencies of the userswitch service, so until you know you can disable caching with `AccessResult::forbidden()->setCacheMaxAge(0)`. But better find the correct caching data for the service, with cache max age = 0 you disable a lot of caching on your page, which is bad for performance. Btw. you need to add caching also in your latest edit for a normal link if it depends on a condition.
Comment: My toolbar will still be not accessible, right, even with setCacheMaxAge(0) ? It there a way otherwise to create an "temporary toolbar" where there's only my link ?
Answer: you can add custom access on route_name: double_vue_klesia.backuser.switch
double_vue_klesia.backuser.switch:
  path: '/path-of-you'
  defaults:
    _form:  '\Drupal\my_module\Form\DemoForm'
    _title: 'title'
  requirements:
    _custom_access: '\Drupal\my_module\Controller\ApplicationController::accessCustom'

Inside \Drupal\my_module\Controller\ApplicationController:
 /**
   * Check user has permission.
   *
   * @return \Drupal\Core\Access\AccessResult
   */
  public function accessCustom() {
    if (if (\Drupal::service('userswitch')->isSwitchUser())) {
      return AccessResult::allowed();
    }
    return AccessResult::forbidden();
  }

Comment: I believe your answer is for routing.yml, I need for links.menu.yml. If i'm wrong, can you explain ?
Comment: ou can still go for the _custom_access, but you need a event subscriber to change the requirement of the existing route. Read this for a start: https://www.drupal.org/node/2187643
Comment: My link is still appearing on my front office every time.
Comment: @Quan this is the right approach so I've undeleted it
Comment: I see (but too late...) that I've already this method in my module, but for a toolbar and I don't have access to this when I switch my user


How to retrieve image URL from Media Bundle field in Twig Template

Question: I have created a custom media bundle using the Media Entity module.
Here is a screen grab of the media bundle:

I have created a Twig template media--image.twig.html
I am able to retrieve all of the field values via the content variable in Twig but I'm having difficulty retrieving the field_image URL.
Here is a snapshot of the output from {{ kint(content.field_image) }}:

How do I extract the URL?  I can see a target Id, so presumably I can load the entity in the preprocess function and pass through the variable but I would've expected the full target object to be available within the Twig template?
Answer: Image URL Formatter

This module adds a url formatter for image field. Then you can output
image url directly.

Enable the module, edit your image field, change the formatter to url.
Now {{ content.field_image }} will print the url instead of rendering the image.
Comment: Thanks, that works.  The only additional task was to override the `field--media--field-image--image.twig.html` template to remove the divs from the Image URL Formatter output.


"Unable to open the database file" (warning: Wide error)

Question: I cannot seem to get anything out of SQLite other than "Unable to open the database file" on IIS. I'm convinced SQLite's error messages are as brusque as Oracle's.

Pre-deployment in Visual Studio 2010/IIS Express I can both read and write to the file.
When I tried to read/write it with the same VS2010 project deployed to IIS7.5, all "create", "read" and "write" commands fail.
The same occurs when I deploy the database file through the project and try to read it.
I've given Full control access to App_Data and the database file to the following users: IIS_IUSRS, IUSRS, DefaultAppPool, and Everyone.

I've looked at:
SQLite problem "unable to open the database file" (The problem automatically went away for the user) and a number of other similar questions, most of which were solved by changing permissions, changing to a writable directory (App_Data should be writable, no?) or changing a relative path to an absolute one (which |DataDirectory| should resolve to).
<connectionStrings>
    <add name="sqlite" connectionString="Data Source=|DataDirectory|\datatables.sqlite;Version=3;" />
</connectionStrings>

Have I missed anything?
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>Unable to open the database file</ExceptionMessage>
<ExceptionType>System.Data.SQLite.SQLiteException</ExceptionType>
<StackTrace>
at System.Data.SQLite.SQLite3.Open(String strFilename, SQLiteConnectionFlags connectionFlags, SQLiteOpenFlagsEnum openFlags, Int32 maxPoolSize, Boolean usePool) at System.Data.SQLite.SQLiteConnection.Open() at AjaxSource.Models.Database.query(String sql, Dictionary`2 parameters) in D:\Tools\Dropbox\Projects\myprojects\AjaxSource\AjaxSource\Models\Database.cs:line 48 at AjaxSource.Models.aaDataModel..ctor() in D:\Tools\Dropbox\Projects\myprojects\AjaxSource\AjaxSource\Models\aaDataModel.cs:line 18 at AjaxSource.Controllers.API.TableDataController.Get() in D:\Tools\Dropbox\Projects\myprojects\AjaxSource\AjaxSource\Controllers\API\TableDataController.cs:line 15 at lambda_method(Closure , Object , Object[] ) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass13.<GetExecutor>b__c(Object instance, Object[] methodParameters) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments) at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)
</StackTrace>
</Error>

Answer: I had a problem opening Sqlite database in IIS

(0x80004005): unable to open database file unable to open database
  file

what helped me to solve the problem is changing the "Identity" of the application pool to "LocalSystem"
Application Pools -> DefaultAppPool (or another pool you are working with) -> Advanced Settings -> Identity -> LocalSystem
Hopefully it will save someone time...
Answer: We used to encounter this problem on the machines of our customers most often, and I have made a lot of investigation upon it and finally we've solved it.
First of all, you need to make sure that your application has read/write access to the database file as well as the folder containing the database file. In most situations, the check will fix the issue; but we are not among those fortunate ones.
In our case, where the application makes very highly concurrent access to the database, it's most probably related to the journal mode of the database, which usually is DELETE by default. That is to say, the rollback journal will be deleted once the corresponding transaction is committed or rolled back. On **nix systems, it's no error; but on Windows systems, it's another story, as can be seen below. On Windows systems, the problem could take place in the following scenario:

A journal file (say A) is created by a SQLite thread.
Another thread P tries to open A.
SQLite has finished its transaction and so deletes the journal file A.
But now thread P has handle for A, so SQLite enters the "Delete Pending" state.
SQLite starts another transaction and has to recreate the journal file (with the same name).
Windows reports ERROR_DELETE_PENDING error (see here for more information: http://blogs.msdn.com/b/oldnewthing/archive/2007/11/09/6001644.aspx)
SQLite report SQLITE_CANTOPEN error (that is, "Unable to open the database file").
P finally releases the file handle and A is deleted.

One solution is to use PERSIST or TRUNCATE journal mode instead of DELETE. See here for more details: http://www.sqlite.org/pragma.html#pragma_journal_mode
In this way, the rollback journal (.-journal) is not deleted at all, and thus we can get rid of the problem you said. I hope this helps.
Answer: Well I don't know if this will answer your question, but in my case I had a dummy error which is that I gave a wrong relatif URL to the database file when I copied my code from a commandline projet to the mvc one. Hope someone will find this useful.
Answer: It wasn't a permissions problem. The problem is that I didn't check that |DataDirectory| actually resolved to the AppData directory like it's supposed to.
On the IIS 7.5 server, |DataDirectory| resolves to C:\inetpub\wwwroot\AjaxSource\App_Data, but the actual AppData is directory is C:\inetpub\wwwroot\AjaxSource\bin\App_Data.
The errors I caught using Fiddler never mention any paths, but imply a permissions problem. I had one of my views display the resolved directory with:

@AppDomain.CurrentDomain.GetData("DataDirectory")

(Oddly, this isn't documented).


No context holder when use partitioner

Question: I'm quite new in Spring Batch and probably that's reason why i have such a question, but anyway.
I want to use partitioner so that it splits work between threads(user per thread). I followed book(the definitive guide to spring batch) and got
Caused by: java.lang.IllegalStateException: No context holder available for step scope
I tried various tutorials to solve it but they couldn't help.
There is code i use
@SpringBootApplication
@Slf4j
@Import(BatchConfig.class)
public class DemoApplication {

    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(DemoApplication.class, args);

        final JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
        final Job job = (Job) context.getBean("partitionerJob");
        log.info("Starting the batch job");
        try {
            final JobExecution execution = jobLauncher.run(job, new JobParametersBuilder().addString("userId", "123,456,789").toJobParameters());
            log.info("Job Status : {}", execution.getStatus());
        } catch (final Exception e) {
            e.printStackTrace();
            log.error("Job failed {}", e.getMessage());
        }
    }

}

@EnableBatchProcessing
@Slf4j
public class BatchConfig {

    @Autowired
    private StepBuilderFactory stepBuilderFactory;
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Bean
    public Job partitionerJob() {
        return this.jobBuilderFactory.get("partitionerJob")
                .start(masterStep())
                .build();
    }

    @Bean
    public Step masterStep() {
        return stepBuilderFactory.get("partitionerStep")
                .partitioner(slaveStep(null).getName(), userPartitioner(null))
                .partitionHandler(partitionHandler())
                .build();
    }

    @Bean
    public Step slaveStep() {
        return
          this.stepBuilderFactory.get("partStep").tasklet(tasklet(null)).build();
    }

    @Bean
    @StepScope
    public Tasklet tasklet(@Value("#{stepExecutionContext[userId]}") String userId) {
        return ((contribution, chunkContext) -> {
            log.info("Current thread=" + Thread.currentThread().getName() + " and i process userId=" + userId);
            return RepeatStatus.FINISHED;
        });
    }

    @Bean
    public TaskExecutorPartitionHandler partitionHandler() {
        TaskExecutorPartitionHandler partitionHandler = new TaskExecutorPartitionHandler();
        partitionHandler.setStep(slaveStep(null));
        partitionHandler.setTaskExecutor(taskExecutor());
        partitionHandler.setGridSize(3);
        return partitionHandler;
    }

    @Bean
    @StepScope
    public UserPartitioner userPartitioner(@Value("#{jobParameters[userId]}") String userId) {
        UserPartitioner userPartitioner = new UserPartitioner();
        userPartitioner.setUserList(Stream.of(userId.split(",")).collect(Collectors.toList()));
        return userPartitioner;
    }

    @Bean
    public TaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setMaxPoolSize(3);
        taskExecutor.setCorePoolSize(3);
        taskExecutor.setQueueCapacity(3);
        taskExecutor.afterPropertiesSet();
        return taskExecutor;
    }

    @Bean
    public JobRepository getJobRepository() throws Exception {
        JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
        factory.setDataSource(dataSource());
        factory.setTransactionManager(getTransactionManager());
        factory.afterPropertiesSet();
        return factory.getObject();
    }

    @Bean
    public DataSource dataSource() {
        EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
        return builder.setType(EmbeddedDatabaseType.HSQL)
                .addScript("classpath:org/springframework/batch/core/schema-drop-h2.sql")
                .addScript("classpath:org/springframework/batch/core/schema-h2.sql")
                .build();
    }

    @Bean
    public PlatformTransactionManager getTransactionManager() {
        return new ResourcelessTransactionManager();
    }

    @Bean
    public JobLauncher getJobLauncher() throws Exception {
        SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
        jobLauncher.setJobRepository(getJobRepository());
        jobLauncher.afterPropertiesSet();
        return jobLauncher;
    }

}

public class UserPartitioner implements Partitioner {

    private static final String PARTITION_KEY = "partition";

    private List<String> userList;

    public void setUserList(List<String> userList) {
        this.userList = userList;
    }

    @Override
    public Map<String, ExecutionContext> partition(int gridSize) {
        Map<String, ExecutionContext> partitionContext = new HashMap<>(gridSize);

        int i = 0;
        for (String userId: userList) {
            ExecutionContext context = new ExecutionContext();
            context.put("userId", userId);
            partitionContext .put(PARTITION_KEY + i, context);
            i++;
        }

        return partitionContext;
    }
}

On the internet i found lots of complaining along with solutions regarding that error, but often that error has different causes.
I tried to remove @StepScope and setAutoProxy(true), but no luck.
Probably i'm missing something but i don't see what exactly.
Here is full stack trace
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.4.RELEASE)

2020-02-11 13:53:46.474  INFO 28557 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication on sts with PID 28557 (/home/sts/IdeaProjects/demo/target/classes started by sts in /home/sts/IdeaProjects/demo)
2020-02-11 13:53:46.478  INFO 28557 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2020-02-11 13:53:47.352  INFO 28557 --- [           main] o.s.j.d.e.EmbeddedDatabaseFactory        : Starting embedded database: url='jdbc:hsqldb:mem:testdb', username='sa'
2020-02-11 13:53:47.780  WARN 28557 --- [           main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'partitionerJob' defined in class path resource [com/example/demo/BatchConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.batch.core.Job]: Factory method 'partitionerJob' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'masterStep' defined in class path resource [com/example/demo/BatchConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.batch.core.Step]: Factory method 'masterStep' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.slaveStep': Scope 'step' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No context holder available for step scope
2020-02-11 13:53:47.781  INFO 28557 --- [           main] o.s.j.d.e.EmbeddedDatabaseFactory        : Shutting down embedded database: url='jdbc:hsqldb:mem:testdb'
2020-02-11 13:53:47.799  INFO 28557 --- [           main] ConditionEvaluationReportLoggingListener :

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-02-11 13:53:47.804 ERROR 28557 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'partitionerJob' defined in class path resource [com/example/demo/BatchConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.batch.core.Job]: Factory method 'partitionerJob' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'masterStep' defined in class path resource [com/example/demo/BatchConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.batch.core.Step]: Factory method 'masterStep' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.slaveStep': Scope 'step' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No context holder available for step scope
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:656) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:484) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
    at com.example.demo.DemoApplication.main(DemoApplication.java:42) [classes/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.batch.core.Job]: Factory method 'partitionerJob' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'masterStep' defined in class path resource [com/example/demo/BatchConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.batch.core.Step]: Factory method 'masterStep' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.slaveStep': Scope 'step' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No context holder available for step scope
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:651) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    ... 18 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'masterStep' defined in class path resource [com/example/demo/BatchConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.batch.core.Step]: Factory method 'masterStep' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.slaveStep': Scope 'step' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No context holder available for step scope
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:656) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:484) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.resolveBeanReference(ConfigurationClassEnhancer.java:394) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:366) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at com.example.demo.BatchConfig$$EnhancerBySpringCGLIB$$3396cdcd.masterStep(<generated>) ~[classes/:na]
    at com.example.demo.BatchConfig.partitionerJob(BatchConfig.java:46) ~[classes/:na]
    at com.example.demo.BatchConfig$$EnhancerBySpringCGLIB$$3396cdcd.CGLIB$partitionerJob$0(<generated>) ~[classes/:na]
    at com.example.demo.BatchConfig$$EnhancerBySpringCGLIB$$3396cdcd$$FastClassBySpringCGLIB$$9cf674b6.invoke(<generated>) ~[classes/:na]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at com.example.demo.BatchConfig$$EnhancerBySpringCGLIB$$3396cdcd.partitionerJob(<generated>) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_201]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_201]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_201]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_201]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    ... 19 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.batch.core.Step]: Factory method 'masterStep' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.slaveStep': Scope 'step' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No context holder available for step scope
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:651) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    ... 42 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.slaveStep': Scope 'step' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No context holder available for step scope
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:368) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:35) ~[spring-aop-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:192) ~[spring-aop-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at com.sun.proxy.$Proxy47.getName(Unknown Source) ~[na:na]
    at com.example.demo.BatchConfig.masterStep(BatchConfig.java:53) ~[classes/:na]
    at com.example.demo.BatchConfig$$EnhancerBySpringCGLIB$$3396cdcd.CGLIB$masterStep$3(<generated>) ~[classes/:na]
    at com.example.demo.BatchConfig$$EnhancerBySpringCGLIB$$3396cdcd$$FastClassBySpringCGLIB$$9cf674b6.invoke(<generated>) ~[classes/:na]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at com.example.demo.BatchConfig$$EnhancerBySpringCGLIB$$3396cdcd.masterStep(<generated>) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_201]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_201]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_201]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_201]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    ... 43 common frames omitted
Caused by: java.lang.IllegalStateException: No context holder available for step scope
    at org.springframework.batch.core.scope.StepScope.getContext(StepScope.java:167) ~[spring-batch-core-4.2.1.RELEASE.jar:4.2.1.RELEASE]
    at org.springframework.batch.core.scope.StepScope.get(StepScope.java:99) ~[spring-batch-core-4.2.1.RELEASE.jar:4.2.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:356) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    ... 58 common frames omitted

Actually you can reproduce this error locally using just given code, just generate project via spring initializr with added spring-batch and lombok.
Any help is highly appreciated.
P.S.
Maven pom (probably will be useful)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath/>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Answer: It is not correct to add StepScope on a Step bean:
@Bean
@StepScope
public Step slaveStep(@Value("#{stepExecutionContext[userId]}") String userId) {
    return this.stepBuilderFactory.get("partStep").tasklet(((contribution, chunkContext) -> {
        log.info("Current thread=" + Thread.currentThread().getName() + " and i process userId=" + userId);
        return RepeatStatus.FINISHED;
    })).build();
}

Because this means that the Step bean should be created only when the Step is active.. but to be active, the step should be created first, but it should be created only when it is active.. So what you need to do is to extract the tasklet of the step as a Step scoped bean and configure the step with that tasklet. Here is a quick example:
@Bean
public Step slaveStep() {
    return this.stepBuilderFactory.get("partStep").tasklet(tasklet(null)).build();
}

@Bean
@StepScope
public Tasklet tasklet(@Value("#{stepExecutionContext[userId]}") String userId) {
    return (contribution, chunkContext) -> {
        log.info("Current thread=" + Thread.currentThread().getName() + " and i process userId=" + userId);
        return RepeatStatus.FINISHED;
    };
}

Comment: Thanks for explanation, it works now. The only question now why tasklet doesn't work(log lines don't appear in console) although job marked as "Completed". I understand that's diffirent question. If anwser is short i'd be glad to see it:)
Comment: Log level configured fine, i see logs with level "info" from other places. Programm even doesn't stop execution on breakpoint inside slave tasklet. I also modified programm so that it throws an exception inside tasklet. But job still finishes with status "Completed". Don't understand why :/
Comment: It's oke, i've figured out what's wrong. Other threads swallowed an exception that was thrown inside partitioner so that exception was not displayed on console. I'll edit provided code so that people from future can use it for experiments. Many thanks again to Mahmoud!

No context holder when use partitioner


Couldn't run loadjava on user schema to load dbwsclientws.jar dbwsclientdb102.jar

Question: I am trying to load oracle webservice client jars to my schema. I did set the PATH to inlcude:
/u01/app/oracle/product/10.2.0/db_1/bin
When I try to run loadjava as "loadjava -u myschema/myscehmapwd -r -v -f -genmissing dbwsclientws.jar dbwsclientdb102.jar"
I am getting error:
Exception in thread "main" java.lang.NoClassDefFoundError: oracle/aurora/server/tools/loadjava/LoadJavaMain. Does it mean that jvm is not setup on the box? How can I check if the jvm is enabled or not?
I am running it on Oracle 10g in UNIX environment.
Any help with the issue is greatly appreciated.
Answer: Look at the classpath in the documentation to ensure that you have all of the files mentioned in the classpath, such as $ORACLE_HOME/javavm/lib/aurora.zip


UICollectionViewController cells disappear on tap or scrolling

Question: I'm trying to add CollectionView on tabBarController
for grid collectionView
So I made a UICollectionViewController as another swift file
I added to ViewController(Navigation Controller) as UICollectionViewController.view
But cells disappear immediately when I tap the collectionView.

I think I did follow the guide and search for two days.. but I can't fix it...
some people are saying it is the problem of add a collectionView as a subview, but I don't get it
This is custom UICollectionView, MyFeedController.swift
import UIKit

class MyFeedController: BaseListController, UICollectionViewDelegateFlowLayout {

    let cellId = "cellId"

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView.backgroundColor = .purple

        collectionView.register(MyFeedCell.self, forCellWithReuseIdentifier: cellId)
        collectionView.delegate = self

        if let layout = collectionViewLayout as? UICollectionViewFlowLayout {
            layout.scrollDirection = .vertical
        }
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! MyFeedCell
        cell.backgroundColor = .red
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let screenSize: CGRect = UIScreen.main.bounds
        let screenWidth = screenSize.width
        return .init(width: (screenWidth - 4 - 4) / 3, height: (screenWidth - 4 - 4) / 3)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 2
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 2
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return .init(top: 2, left: 0, bottom: 2, right: 0)
    }
}

And It's my custom Cell, MyFeedCell.swift It's simple
//
//  MyFeedCollectionController.swift
//  My-Lord-Cat
//
//  Created by Cory Kim on 17/07/2019.
//  Copyright © 2019 Cory Kim. All rights reserved.
//

import UIKit

class MyFeedController: BaseListController, UICollectionViewDelegateFlowLayout {

    let cellId = "cellId"

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView.backgroundColor = .purple

        collectionView.register(MyFeedCell.self, forCellWithReuseIdentifier: cellId)
        collectionView.delegate = self

        if let layout = collectionViewLayout as? UICollectionViewFlowLayout {
            layout.scrollDirection = .vertical
        }
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! MyFeedCell
        cell.backgroundColor = .red
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let screenSize: CGRect = UIScreen.main.bounds
        let screenWidth = screenSize.width
        return .init(width: (screenWidth - 4 - 4) / 3, height: (screenWidth - 4 - 4) / 3)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 2
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 2
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return .init(top: 2, left: 0, bottom: 2, right: 0)
    }
}

So I added MyFeedController to MyPageController(which is a UIViewController)

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .white

        navigationController?.navigationBar.isHidden = true

        setupLayout()
        setupMyFeedGridCollectionView()
    }

    // other views are set on setupLayout()

    fileprivate func setupMyFeedGridCollectionView() {

        let myFeedController = MyFeedController()

        view.addSubview(myFeedController.view)
        myFeedController.view.anchor(top: infoStackView.bottomAnchor, leading: view.leadingAnchor, bottom: view.bottomAnchor, trailing: view.trailingAnchor, padding: .init(top: 10, left: 0, bottom: 0, right: 0))
    // anchor method is extension for autolayout
    }

Answer: I don't see where you're setting the collectionView's dataSource.  Set a breakpoint at this line to make sure it's getting called:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! MyFeedCell

Comment: Thanks for your answer!
I set a breakpoint as you said and it's called well. Also I specified `collectionView.dataSource = self` but it's same :(
Comment: Your view hierarchy looks like it could be leading to the problem.  You're currently adding the view of another view controller to 'MyPageController'?
Comment: yes! adding MyFeedController(UicollectionView) to MyPageController(UIViewController)


Proving $f(x)=(\cos(2\pi x),\sin(2\pi x)))$ is not closed and is the quotient mapping

Question:
$S^1$ is the subspace of $\mathbb{R}^2$ consisting of all $y\in\mathbb{R}^2$ such that $d(y,0)=1$, where $d$ is the Eucledian metric. Let $f:\mathbb{R}\to S^{1}$ be given by $f(x)=(\cos(2\pi x),\sin(2\pi x)))$. Show that $f$ is the quotient mapping but not a closed mapping. Is $f$ an open mapping?

I have no idea on how to solve this question. A closed set in $S_1$ is by the definition of subspace topological space the intersection of a closed subset of $\mathbb{R}^2$. But I cannot see how I can relate all these concepts to the quotient topology.
Question:
Can someone help me solve this question?
Thanks in Advance!
Comment: What does "$f$ is the quotient mapping" mean?
Comment: @JoséCarlosSantos It is the surjective mapping between one topological space and other topological space, the latter endowed with the quotient topology.
Answer: It is not a closed map, because$$S=\left\{n+\frac1n\,\middle|\,n\in\mathbb N\setminus\{1\}\right\}$$is closed, but $f(S)$ isn't ($(1,0)\in\overline{f(S)}\setminus f(S)$).
Comment: How do you conclude that $f(S)$ is not in $(1,0)$ and even if it is not. How can the distance surpass $1$ if $c^2(x)+\sin^2(x)=1$?
Comment: I understood **nothing** from your questions.
Comment: How do you know that $f(S)$ is not in $(1,0)$? Why (1,0)? If f(S)=(-1,0) then the distance to the origin (0,0) does not surpass $1$.
Comment: I did **not** write that $f(S)$ is not in $(1,0)$. It doesn't make sense! What I wrote was that $S$ is closed by $f(S)$ isn't.. And I proved that assertion using the fact that $(1,0)\in\overline{f(S)}\setminus f(S)$.
Comment: Finally I understood your argument! Thanks! However regarding the fact $f$ is a quotient mapping it is obvious $f$ is surjective and  continuous. But to conclude I would need to show $f$ is open mapping. How should I prove that for any arbitrary interval of $\mathbb{R}$?
Comment: Take $(a,b)\subset\mathbb R$. If $b-a>1$, then $f\bigl((a,b)\bigr)=S^1$, which is an open subset of $S^1$. Otherwise, $f\bigl((a,b)\bigr)$ is just an arc in $S^1$ (without endpoints), which is again an open subset of $S^1$.


.htaccess file in top directory overrides .htaccess in subdirectories

Question: I'm using different .htaccess files to control the website.
I have a .htaccess file in the public_html folder, and one in a folder called "Private".
The files were working perfectly, until I created ErrorDocument handlers in the top level .htaccess file. When I went to the "Private" folder, I didn't have to provide credentials...
Everything was working fine until I made the ErrorDocument handlers. What's the problem, and how can I solve it?
Answer: You can nullify ErrorDocument directives of parent .htaccess by using these directives in /private/.htaccess:
ErrorDocument 404 default
ErrorDocument 401 default
ErrorDocument 403 default

Comment: The private directory is still accessible without a password.
Comment: Can you post content of both .htaccess in your question.
Answer: The /Private directory is Password Protected after I deleted and added protection again...


what is the best way to skew in x direction to look like this?

Question:

.navbar{

 background-color: purple;
 width: 70%;
 height: 40px;
 transform: skewX(20deg);
 padding: 0px;
}

I am trying to make a navigation bar that looks exactly the same as in image attached below.In the image the bottom ends of the div are skewed in X direction but when i tried this both corners skewed in the same direction.How to skew the div to look like this?
Comment: You could use `::before` and `::after` pseudo elements to make those angled lines, skewing in opposite directions.
Answer: You could try clip-path property instead of transform.
Clippy is a handy tool for creating css clip-paths, take a look
Answer: Try this one:
clip-path: polygon(0 0, 100% 0%, 85% 81%, 13% 80%);

Apply it your div now
You can change the values according to your needs.
Hope it help you.
Comment: Are you trying to skew the upper most div which I see is the whole purple colored box? Or you want the navbar div to be skew from bottom left and bottom right to the inner side?

If you can share your code here, that would be easy for everyone to help you.
Comment: yes.i want to skew the bottom corners of the purple div inside like the above image.I attached the code.
Comment: use this @Wania Kanwal
```
clip-path: polygon(0 0, 100% 0%, 85% 81%, 13% 80%);
Comment: You can also change the values according to your needs


finding number of triangles inscribed in a circle

Question: How to find the number of acute angle and obtuse angled triangles that can be inscribed in a circle containing 'N' equally spaced points.
Comment: To clarify: You are given $N$ equally spaced points on the circle, and each vertex of the inscribed triangles must be one of those points, correct? What have you tried?
Answer: Hint
The total number of triangles that can be formed from $N$ points on the circle will be $\binom{N}{3}$. I'm assuming that you are looking for total number of acute and obtuse angled triangles. If that is the case then from this total number found above, subtract the ones which are right angled. For right angled think in terms of two points that lie on the diameter of a circle and the third on the circumference. Parity of $N$ will play some role.
If you are looking for separate count of acute and obtuse triangles then also think in terms of all 3 points lying on one side of a diameter as opposed two lying on one side and the third on the other.
Answer: Hint:  number the points from $1$ through $N$ around the circle.  Consider a triangle that includes point $1$ and two other points.  What can you say about the pairs of points that produce a right angle at point $1$?  What can you say about the pairs of points that produce an obtuse angle at point $1$?


Can a pitcher also be a designated hitter?

Question: Suppose an American League Team ended up with a good-hitting pitcher e.g Yovani Gallardo, or from an earlier era, Ken Brett, who had established this reputation in the National League.
Could such a pitcher be a "designated hitter" (dh) for himself? If yes, could he continue to "dh" after being relieved as a pitcher?
Comment: So if I understand correctly, the DH could go in to pitch and continue to hit. After he came off the mound, the pitchers would have to hit for themselves. Correct?
Answer: NO.
Rule 6.10(b)

2 It is not mandatory that a club designate a hitter for the pitcher, but failure to
  do so prior to the game precludes the use of a Designated Hitter for that club
  for that game.
10 Once the game pitcher bats for the Designated Hitter, such move shall terminate the Designated Hitter role for that club for the remainder of the game.
  The game pitcher may pinch-hit only for the Designated Hitter.
12 Once a Designated Hitter assumes a position on defense, such move shall terminate the Designated Hitter role for that club for the remainder of the game

These rules combine to indicate quite clearly that the Pitcher cannot be named the designated hitter for himself. Specifically 12 would be the clearest case, the DH cannot appear on defense, which is the primary role of the pitcher.
What a team could do if they had a pitcher with sufficient value as a DH though is the have him DH and then allow him to pitch, perhaps in a closer type role. Though rule 15 indicate that he would not be allowed to warm up in the bullpen:

15 The Designated Hitter may not sit in the bullpen unless serving as a catcher in
  the bullpen
Comment: The technique is actually called the phantom DH. http://www.baseball-reference.com/bullpen/designated_hitter
Comment: Earl Weaver, long time manager of the Orioles had a lot to do with the creation of this rule. After the DH was instituted, Weaver would set a pitcher as the DH and simply pinch hit for him when the DH would come up, usually substituting based on match ups.
Comment: @Jerrod interested in a source on that, do you happen to have one?
Comment: @TomAu the DH is *always* optional, you just can't get it back if you either sub out your DH or don't start with one. That said in MLB, 99% of pitchers are worse hitters than 99% of hitters, so there is rarely a circumstance where you'd want your pitcher hitting if you can avoid it.
Comment: So what happens once a Designated Hitter assumes a position on defense, [and] such move shall terminate the Designated Hitter role for that club for the remainder of the game?" Does the team then bat with eight hitters, or does something else happen?
Comment: No, the pitcher bats in the substituted player's batting position.
Comment: @waxeagle: Then you could get a pitcher (e.g. Yovani Gallardo) to bat that way through the "back door." But the downside is that any reliever would also have to bat for himself.
Comment: Then if I am understanding correctly, the DH could go in to pitch and continue to hit in the same spot in the lineup. But when he came off the mound, the new pitcher would have to hit for himself. Correct?
Comment: I love @Jerrod 's  comment I was looking for that.  It illustrates what a crappy convoluted rule this is, I hope somehow they can get rid of it.
Comment: @MarkMonforti it's *far, far* more likely to expand than disappear. It's a good bet the National League gets the DH in the next 5 years.
Comment: Man that would be awful.  I think I would get rid of my season tickets
Comment: @MarkMonforti Come have a chat! http://chat.stackexchange.com/rooms/2433/the-clubhouse


Relationship between ordinal and (semi) ordinal variable

Question: I have the following problem. I have conducted a survey about how people have consumed media in a certain way. The scale of responses was 1=never to 9=always. Also I have recorded exam scores for a specific exam for each of the respondents. These scores however are on a scale from 1.0 to 5.0; 1.0 being the best and they can take on the incremental values in between the two boundaries (increments of 0.1; i.e. 1.1,1.2,...,4.9,5.0).
I am now exploring whether there is a relationship between the survey results and the scores.
I am not sure which method best to use to establish if there's a relationship between the two. From other questions on here, I have seen that Spearman correlation and a chi squared test might work, but I am a bit concerned about the scale of the scores and I am not 100% sure whether one would even consider this truly ordinal data.
Thanks
Answer: Spearman correlation will work with variables that are at least ordinal.  That is, if you suspect your scores are really interval or quasi-continuous, there is no issue with using Spearman correlation for this kind of data.
Also, with an ordinal scale from 1 to 9, that's probably enough categories where respondents are treating it as interval in nature.  That is, they are probably treating it as numeric, understanding, for example, that a response of 4 is half-way between a response of 3 and of 5.  In this case, I would probably be okay with treating this variable as interval, and Pearson correlation, if other assumptions are met, would probably be okay.  If this item was rated as (never, sometimes, often, always), I would advise against this approach.


Executar rotinas em background

Question: Tenho uma rotina para ser executado ao abrir meu sistema uma vez por dia, coloco um panel com uma mensagem para o cliente ver que está sendo processado, mas agora está levando bastante tempo para ser executado.
Minha ideia é colocar em background, fiz testes com Thread mas ela trava meu sistema.
Qual a melhor forma de fazer isso sem a necessidade de travar o usuário?
Comment: quando você usa uma thread o seu sistema trava? talvez você esteja usando errado, pode descrever melhor qual seria essa operação?
Comment: Minha rotina executa uma stored procedure no banco de dados para calcular juros de documentos. Hoje eu coloco um panel e travo o sistema para o usuário até ela terminar, mas em alguns clientes demora um certo tempo. Então quero colocar em uma thread ou algo parecido, as dicas que achei de como usar thread não funciona bem no meu caso, o sistema trava e só volta quando finaliza o procedimento.
Comment: Parece que a solução realmente seria utilizar uma thread, uma outra alternativa seria o seu programa utilizar um programa externo pra fazer a operação e aguardar o resultado dele em uma arquivo de texto por exemplo, seria como uma thread, o seu problema então são as threads, se puder postar como você está usando elas e quais tutoriais seguiu.
Comment: A forma como fiz foi baseado em tutorial da net, criando uma classe Thread e colocando o procedimento dentro do método execute. Você teria um tutorial sobre threads?
Comment: tem esse aqui: [link](https://simasware.wordpress.com/2014/01/06/criando-threads-com-delphi/), é bom que antes de aplicar ao seu problema você teste em uma aplicação simples pra ver como funciona.
Comment: Se a thread congela o sistema, possivelmente você a acionou com o método execute. Tem que ser com start. Outro detalhe é que a thread não pode usar a mesma conexão do resto do programa com o banco de dados, tem que ser uma conexão só pra ela.
Answer: Utilize TTaks de processamento em paralelo.
procedure executarrotina;
begin
  vg_Tarefas.Add(TTask.Create(procedure ()
  var vEmail : TEmail;
  begin
      vEmail := TEmail.Create;
      try
        vEmail.Assunto := pAssunto;
        vEmail.Destinatarios := pDest;
        vEmail.Mensagem := pMens;
        vEmail.Enviar;
      finally
        FreeAndNil(pMens);
        FreeAndNil(vEmail);
      end;
  end));
  vg_Tarefas[vg_Tarefas.Count - 1].Start;
end;

declare a variável
var vg_Tarefas : TList<ITask>;

uses System.Generics.Collections;

Muito importante: Nunca utilize comandos que mostre avisos, atualize edit, nesta rotina. Se quer performance, não atualize tela. Se precisa atualizar tela, atualize algo numa tabela e dispare evento no banco, que será mais eficiente.
Neste exemplo, estou enviando email em background (paralelo).

Executar rotinas em background


The size of my reduced data set is greater than the original

Question: I have an original data set with a number of features N equal to 135 and a number of rows equal to 32000. The last column of the data set ( column 136 ) can take either -1 or 1 depending on the class of the row. Each of the features can take a binary value of 0 or 1 ( characteristic present or not).
The original data set looks like this:
<code>    V1    V1   V3   V4   V5  ............ V136
    0     1    0    0     1                 -1
    0     1    1    0     1                 -1
    1     0    1    1     0                  1
    .     .    .    .     .                  .
    .     .    .    .     .                  .
    .     .    .    .     .                  . ( row 32000)
</code>
I am required to use PCA to reduce the dimentionality of my data, and then apply SVM for classification. when I used PCA to cover 96% of the variance, I got a number of principal componenents equal to 54 ( which is good, since I reduced the number of features from 135 to 54), and then I computed the reduced data matrix which looks like this:
<code>    V1                V2                V3                 V4  .................  V54    Class
    1.89463604908794 -1.15458215287551 -0.577464752091365 -0.560850946997309               -1
   -0.170737500644998 0.190545093723431 0.451249831953581 1.44451854658279                 -1
    ......
    ......
</code>
When I run SVM on the original data set, SVM finishes quickly, however, when I run it on the reduced data set, it takes forever. A part of the problem is that the original data set was binary, and thus it takes less memory when loaded. However, because the reduced data set is composed of real number, it takes more memory. For comparison purposes, I also saved the reduced data set to disque, and I compared the size of the two files in terms of KB:
<code>    Original Data ( not reduced) : 8500 KB
    Reduced Data set             : 32200 KB
</code>
It looks like PCA reduced the number of features but it increased the total size in term of KB. Any ideas about how I can overcome this issue? Doesn't this throw away the original purpose of PCA ( reducing the dimentionality, and thus the size)?
Comment: What is the "original purpose" of PCA? I'm not sure there is one exactly. More importantly, what is your purpose in doing PCA? If it is just to save KB of memory & make the SVM run faster, then yes, you should skip it.
Comment: PCA reduces the number of features needed to describe your data. The size is a side effect.
Comment: Can you give me more information of your PCA part? which function you are using `prcomp`? and then how you cut off at 96% variance? A reproducible sample code maybe.. with anonymous variables names will be extremely helpful
Comment: Actually, I did not use prcomp. I implemented my own version of PCA following the exact steps of tha algorithm ( computing the covariance matrix, and then taking the first "m" eigenvectors that are needed to compute the final reduced data set)
Answer: You're confusing the dimensionality of your data set with its size. They get swapped all the time informally, but the difference matters here:

Your data set has a dimensionality of 32000 cases x 135 features.
The size of your data set is 8500 KB

Note that the units aren't compatible.
PCA reduced the dimensionality of your data from 32000 x 135 -> 32000 x 54 (2.5x, not too shabby). PCA is pretty agnostic to how the data is stored, and unfortunately, switching from boolean values (a smart program can fit 8 to a byte) to doubles (8 bytes/double) more than counteracts whatever savings you get from projecting your data onto a lower dimensional subspace.
If reducing the memory footprint of your data is the only reason you care, I'd just skip it. (It's only 8 Mb anyway!). However, if you're interested in feature selection, you could do a factor analysis and manually look for variables that are either poorly correlated with the target variable OR are highly correlated with many other variables. These could then be eliminated from your original data set. This might give you a modest reduction in its dimensionality, while keeping it as a collection of binary features.
Answer: i think ,you are questioning purpose of PCA by considering single case i.e. when your data set is having binary values.
Now assume that your data set is having real values instead of binary values then i m sure PCA will do its task perfectly i.e. reducing the dimensionality, and thus the size
In your case, if PCA is increasing size of data set then don't use it.


Tree analysis - CHAID

Question: I am running a decision tree analysis, and the same predictor that forms the first core branch, reappears as an another branch further down the tree.
Could somebody explain me how this is possible? Or how I should interpret this?
Thanks!
Answer: Decision trees make splits by finding partitions derived from the domain of covariates, so assuming you aren't using binary data this is not surprising behavior at all.
Consider a categorical variable, $x$, that has four levels, 'A', 'B', 'C', 'D'. The first split could be $x=A$. This means the 'False' portion of the split has $x$ which can still have values 'B', 'C' or 'D' so there are conceivably 2 more splits that can be made. In general for a categorical variable you can make $|x| - 1$ splits, where $|x|$ is the cardinality of the variable
In the case of continuous data, if all of your data points are unique you could make $n-1$ splits where $n$ is the number of data points.
Hope this clears it up a little for you.

Tree analysis - CHAID


Help in proving homeomorphic map for topological cylinder

Question: I have understood that a cylinder can be obtained from a square by gluing a pair of its opposite sides.
Mathematically if we take $X=[0, 1]$ X $[0,1]$ with equivalence relation ~ such that
$(s,0)$ ~ $(s,1)$ and $(s,t)$ ~ $(s,t)$ for $s,t$ $\neq$ $0,1$
Then $X$$/$~ is homeomorphic to $S^1$ X $I$ ,where $I=[0,1]$, by mapping $[(s,t)]$ to $(e^{2\pi is},t)$
I know that the continuity can be proved by observing that the composition map $fo\pi$, where $\pi$ is the map from $X$ to $X$$/$~ , is continuous.
MAIN PROBLEM: I am having a hard time in writing other mathematical details (bijection and continuity of inverse map) which are required to show the homeomorphism.
If someone could give me proper mathematical proof then it would be really helpful. Then i will write other construction (like cone, mobius band, torus) by myself.
Comment: I think you made a mistake either in the equivalence relation or in the definition of $\pi$ as $\pi(0,1) = (e^{2\pi i 0},1) = (e^{2\pi i 1},1) = \pi(1,1)$ but $(0,1)$ is not equivalent to $(1,1)$ which shows that $\pi$ is not injective.
Comment: @Lassadar That is not a mistake. The map $\pi$ is not injective because $[x]=[y]$ does not imply $x=y$ while defining the quotient topology, it is surjective. That is the definition.
Comment: sorry I meant the homeomorphism and not the projection, replace $\pi$ by $f$ in the comment above. This is then supposed to be injective.
The projection identifies the border of the square corresponding to the second variable but $f$ then "glues together" the border corresponding to the first variable.
Comment: @Lassadar It would be really helpful if you could provide me proper proof of it. Because the more i am reading it the more i am getting confused.
Answer: First redefine your homeomorphism $f:X/\sim \rightarrow S^1 \times I$, $[s,t] \mapsto (e^{2\pi i t},s)$ (notice that $s$ and $t$ are switched. For an intuitive reason why this change is necessary imagine gluing opposite sides of a square ($I\times I$) together to get a cylinder. Now in order for this to be injective (i.e. we don't loose any information) we need to identify the glued sides via the relation. In your proposed homeomorphism you didn't glue together the sides that were identified but the other ones which made your map not injective.
You managed to show that this map is continuous.
Now we get injectivity by applying the definitions:
$$f(s,t)=f(s',t') \Leftrightarrow (e^{2\pi i t}, s) = (e^{2\pi i t'}, s') \Leftrightarrow s=s', t = t' + k, k\in \mathbb{Z} \Rightarrow [s,t] \sim [s',t']$$
where the last implication holds because the only way to get $t=t'+k$ is if $t=1$ and $t'=0$ or the other way round. This shows injectivity.
To show surjectivity try to find an inverse function which you then also show to be continuous.
As a side note: there is more than one way to parametrize $S^1$. You chose the complex unit circle. This works but I think the proof (in particular the inverse function) would be a bit simpler if you chose to parametrize $S^1$ by $[0,2\pi)$ (or even $[0,1)$) which stands for the angle. This lets you skip the exponential function.
Comment: Thanks a lot. It was really helpful. And yeah you were right, i made the mistake in defining homeomorphism.


(Linear regression) Can I train and validate at the same time using the following approach?

Question:
In a lot of material I found online, training and validation seems to be an iterative process

For example, the regularized regression problem
$E = \|Xw - t\|_2^2 + \lambda \|w\|^2_2$
$X$ is data matrix, $w$ is weights of linear predictors, $t$ is targets.
Their algorithm seems to be,

First, we find $w^\star$ by minimizing $E$ for some $\lambda$,
Next, use $w^\star$ to find the error on validation set
Then solve  $w^\star$ again by minimizing $E$ for some different
$\lambda$,
Next, use $w^\star$ to find the error on validation set
$\vdots$
Pick the best performing $w^\star$ on the validation set.

What I have in the my code is to compute several weights $w^\star_k$ at the same time, and pick the best one based on validation set.

I want to perform training and validation in one shot

Find $w^\star_1, \ldots, w_N^\star$ ($N$ different predictors) by minimizing $E$ on training set for some $\lambda_1, \ldots, \lambda_N$ ($N$ different regularization constants),
Run all of $w^\star_1, \ldots, w_N^\star$ on validation set.
Pick best $w^\star_k$, $k \in \{1, \ldots, N\}$

Is this a proper way of choosing my hyperparameter?
Sorry if this seems to be a basic question. First time doing "validation"/hyperparam tuning.
Answer: All the calculations are the same, wether you are doing it sequentially or in one shot, so yes. Remember, if you want to asses the out of sample error you should keep out an untouched test set and evaluate the error on that. The error that you get on the validation set with the picked w will underestimate the true out of sample error, as this is was chosen specifically to minimize the error on the validation set
Answer: Do you use python? do the hyperparameter tuning by RandmizedSearchCV or GridSearchCV:
https://scikit-learn.org/stable/modules/grid_search.html
As you're doing Ridge regression https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.Ridge.html, Should be very straight forward.
RandmizedSearchCV or GridSearchCV learn different models given deterministic hyperparameters or a random approach of the hyper parameters. So you can pick your favorite lambdas and give it a go.
The output of the hyperparametersearch is (if I remember correctly) the loss calculated by cross validation (therefore CV at the end) for each model and the models itself.
Although this technique is learning one-by-one, for you it should feel parallel as you only run it once.


SQL - Removing Duplicate without 'hard' coding?

Question: Heres my scenario.
I have a table with 3 rows I want to return within a stored procedure, rows are email, name and id. id must = 3 or 4 and email must only be per user as some have multiple entries.
I have a Select statement as follows
SELECT
     DISTINCT email,
     name,
     id
from table
where
     id = 3
     or id = 4

Ok fairly simple but there are some users whose have entries that are both 3 and 4 so they appear twice, if they appear twice I want only those with ids of 4 remaining. I'll give another example below as its hard to explain.
Table -
Email              Name    Id
firstname.lastname@example.com   jimmy   4
email@example.com   brian   4
firstname.lastname@example.com   kevin   3
firstname.lastname@example.com   jimmy   3

So in the above scenario I would want to ignore the jimmy with the id of 3, any way of doing this without hard coding?
Thanks
Comment: Which RDBMS are you using (MySQL, Oracle etc...)? Please _always_ tag SQL questions with the tag as there may be some additional feature of the RDBMS that you are using that can help.
Comment: Sorry its SQL Server so T-SQL. Thanks for all the responses. Think the Max(id) should do the trick, much appreciated
Answer: SELECT
     email,
     name,
     max(id)
from table
where
     id in( 3, 4 )
group by email, name

Comment: You must mean MAX() from the OPs example.
Answer: Is this what you want to achieve?
SELECT Email, Name, MAX(Id) FROM Table WHERE Id IN (3, 4) GROUP BY Email;

Comment: Unless it's MySQL you *must* GROUP BY Name as well.
Comment: T-SQL has to have  GROUP BY Name too.  This also doesn't account for the same name having different email addresses, which is a possibility, given the table structure.
Answer: Sometimes using Having Count(*) > 1 may be useful to find duplicated records.
select * from table group by Email having count(*) > 1

or
select * from table group by Email having count(*) > 1 and id > 3.

The solution provided before with the select MAX(ID) from table sounds good for this case.
This maybe an alternative solution.
Answer: What RDMS are you using?  This will return only one "Jimmy", using RANK():
SELECT A.email, A.name,A.id
FROM SO_Table A
INNER JOIN(
SELECT
email, name,id,RANK() OVER (Partition BY name ORDER BY ID DESC) AS COUNTER
FROM SO_Table B
) X ON X.ID = A.ID AND X.NAME = A.NAME
WHERE X.COUNTER = 1

Returns:
 email          name    id
 ------------------------------
 firstname.lastname@example.com   jimmy   4
 email@example.com   brian   4
 firstname.lastname@example.com   kevin   3