Material Table not updating table data after mutation

Question: When a user adds additional information, a mutation is made to the database adding the new info, then the local state is updated, adding the new information to the lead.
My mutation and state seem to get updated fine, the issue seems to be that the state of the Material Table component does not match its 'data' prop. I can see in the React Dev tools that the state was updated in the parent component and is being passes down, the table just seems to be using stale data until I manually refresh the page.
I will attach images of the React Devtools as well as some code snippets. Any help would be much appreciated.
Devtools Material Table data prop:
Devtools Material Table State
Material Table Parent Component:
const Leads = () => {

    const [leadState, setLeadState] = useState({});
    const [userLeadsLoaded, setUserLeadsLoaded] = React.useState(false);
    const [userLeads, setUserLeads] = React.useState([]);
    const { isAuthenticated, user, loading } = useAuth()

    const [
        createLead,
        { data,
            // loading: mutationLoading,
            error: mutationError },
    ] = useMutation(GQL_MUTATION_CREATE_LEAD);

    const params = { id: isAuthenticated ? user.id : null };

    const {
        loading: apolloLoading,
        error: apolloError,
        data: apolloData,
    } = useQuery(GQL_QUERY_ALL_LEADS, {
        variables: params,
    });

    useEffect(() => {
        if (apolloData) {
            if (!userLeadsLoaded) {
                const { leads } = apolloData;
                const editable = leads.map(o => ({ ...o }));
                setUserLeads(editable);
                setUserLeadsLoaded(true);
            };
        }
    }, [apolloData])

    if (apolloLoading) {
        return (
            <>
                <CircularProgress variant="indeterminate" />
            </>
        );
    };

    if (apolloError) {
        console.log(apolloError)
        //TODO: Do something with the error, ie default user?
        return (

Oh no, there was a problem. Try refreshing the app.

{apolloError.message}

        );
    };
    return (
        <>
            <Layout leadState={leadState} setLeads={setUserLeads} leads={userLeads} setLeadState={setLeadState} createLead={createLead}>
                {apolloLoading ? (<CircularProgress variant="indeterminate" />) : (<LeadsTable leads={userLeads} setLeads={setUserLeads} />)}
            </Layout>
        </>
    )
}

export default Leads

Handle Submit function for adding additional information:
const handleSubmit = async (event) => {
        event.preventDefault();

        const updatedLead = {
            id: leadState.id,
            first_name: leadState.firstName,
            last_name: leadState.lastName,
            email_one: leadState.email,
            address_one: leadState.addressOne,
            address_two: leadState.addressTwo,
            city: leadState.city,
            state_abbr: leadState.state,
            zip: leadState.zipCode,
            phone_cell: leadState.phone,
            suffix: suffix,
            address_verified: true
        }
        const { data } = await updateLead({
            variables: updatedLead,
            refetchQueries: [{ query: GQL_QUERY_GET_USERS_LEADS, variables: { id: user.id } }]
        })
        const newLeads = updateIndexById(leads, data.updateLead)
        console.log('New leads before setLeads: ', newLeads)
        setLeads(newLeads)
        // setSelectedRow(data.updateLead)
        handleClose()
    };

Material Table Component:
const columnDetails = [
  { title: 'First Name', field: 'first_name' },
  { title: 'Last Name', field: 'last_name' },
  { title: 'Phone Cell', field: 'phone_cell' },
  { title: 'Email', field: 'email_one' },
  { title: 'Stage', field: 'stage', lookup: { New: 'New', Working: 'Working', Converted: 'Converted' } },
  { title: 'Active', field: 'active', lookup: { Active: 'Active' } },

];

const LeadsTable = ({ leads, setLeads }) => {
  const classes = useStyles();
  const { user } = useAuth();
  const [isLeadDrawerOpen, setIsLeadDrawerOpen] = React.useState(false);
  const [selectedRow, setSelectedRow] = React.useState({});

  const columns = React.useMemo(() => columnDetails);

  const handleClose = () => {
    setIsLeadDrawerOpen(!isLeadDrawerOpen);
  }
  console.log('All leads from leads table render: ', leads)
  return (
    <>
      <MaterialTable
        title='Leads'
        columns={columns}
        data={leads}
        icons={tableIcons}
        options={{
          exportButton: false,
          hover: true,
          pageSize: 10,
          pageSizeOptions: [10, 20, 30, 50, 100],
        }}
        onRowClick={(event, row) => {
          console.log('Selected Row:', row)
          setSelectedRow(row);
          setIsLeadDrawerOpen(true);
        }}
        style={{
          padding: 20,
        }}
      />
      <Drawer
        variant="temporary"
        open={isLeadDrawerOpen}
        anchor="right"
        onClose={handleClose}
        className={classes.drawer}
      >
        <LeadDrawer onCancel={handleClose} lead={selectedRow} setLeads={setLeads} setSelectedRow={setSelectedRow} leads={leads} />

      </Drawer>
    </>
  );
};

export default LeadsTable;

Answer: Try creating an object that contains refetchQueries and awaitRefetchQueries: true. Pass that object to useMutation hook as a 2nd parameter. See example below:
const [
        createLead,
        { data,
            loading: mutationLoading,
            error: mutationError },
    ] = useMutation(GQL_MUTATION_CREATE_LEAD, {
  refetchQueries:  [{ query: GQL_QUERY_GET_USERS_LEADS, variables: { id: user.id } }],
  awaitRefetchQueries: true,
});

Manually updating cache. Example blow is adding a new todo. In your case you can find and update the record before writing the query.
const updateCache = (cache, {data}) => {

    // Fetch the todos from the cache
    const existingTodos = cache.readQuery({
      query: GET_MY_TODOS
    });
    // Add the new todo to the cache (or find and update an existing record here)
    const newTodo = data.insert_todos.returning[0];
    cache.writeQuery({
      query: GET_MY_TODOS,
      data: {todos: [newTodo, ...existingTodos.todos]}
    });
  };
  const [addTodo] = useMutation(ADD_TODO, {update: updateCache});

Comment: So I believe what you were looking at was the creteLead mutation, where the issue I'm having is in the updateLead mutation which is invoked in the handleSubmit function. I was trying something similar there passing the refetchQueries object as the second parameter when I invoke the function, but I tried something closer to what you suggested where I pass it in when I call the hook, but I had no luck there either.
Comment: What I tried instead:

```
const [
        updateLead,
        { data, loading: mutationLoading, error: mutationError },
    ] = useMutation(GQL_MUTATION_UPDATE_LEAD, {
        refetchQueries: [{ query: GQL_QUERY_GET_USERS_LEADS, variables: { id: user.id } }],
        awaitRefetchQueries: true,
    });
```
Comment: Same principal, It's best to pass the refetchqueries inside the use mutation hook.  2nd option would be to manually update the cache. Hope this solved the problem.
Comment: I will try manually updating the cache next, thanks Ray.
Comment: I added a snippet on how to manually update the cache above.


what is the value of angle $a$

Question:
what is the value of angle   $a$

Comment: I have a brute force answer, but the work is awful. I'm probably missing some cool trick that makes it easy. At least the answer is nice.
Comment: @prayer smith -- (1) Where is the problem from?; (2) Have you tried anyhing? If so, can you show your work up to the point where you got stuck?
Comment: This is a good question. Who is gave -1 ? I will give +1 and I will try for a synthetic solution.
Comment: Is this homework?
Comment: Not me. -1 is probably due to the fact that OP did not show any effort.
Comment: Your question was put on hold, the message above (and possibly comments) should give an explanation why. (In particular, [this link](https://math.meta.stackexchange.com/a/9960) might be useful.) You might try to edit your question to address these issues. Note that the next edit puts your post in the review queue, where users can vote whether to reopen it or leave it closed. (Therefore it would be good to avoid minor edits and improve your question as much as possible with the next edit.)
Answer: Answer: $\angle FED = \alpha = 54^\circ .$ In the solution, we will use $\cos 54^\circ = \sqrt{\dfrac{5-\sqrt 5}{8}} $ and $\cos 36^\circ = \dfrac{\sqrt 5 -1}{4}$.

Without loss of generality we assume that $|AD|=|AF|=2.$ By $\cos 36^\circ = \dfrac{\sqrt 5 -1}{4}$ and cosine theorem in $\triangle ADF$, we find $|DF|=\sqrt 5 -1.$ Let's say that $|DE|=x, |EB|=y, |FE|=z.$ By power of point $D$ with respect to the circle:
$$x(x+y)=2^2 \tag{1}$$
On the other hand, by cosine theorem in $\triangle ADB$: $(x+y)^2=2^2+4^2-2\cdot 2\cdot 4 \cdot \cos 36^\circ$ and
$$ (x+y)^2=16-4\sqrt 5 \tag{2} $$
If we solve $(1)$ and $(2)$ together we can find
$$x=\dfrac 2{\sqrt{4 - \sqrt5}}, \   \  \ y=\dfrac {2(3-\sqrt 5)}{\sqrt{4 - \sqrt5}} \tag{3} $$
Now, let's apply Stewart's theorem in $\triangle BDF$: $z^2 =\dfrac{x\cdot |BF|^2 + y\cdot |DF|^2}{x+y}-xy$ and we can calculate $z$, by exhausting operations.
In last step, $\angle DEF = \alpha$ and $\cos\alpha = \dfrac{x^2+z^2-|DF|^2}{2 x z}$ and therefore $\cos \alpha = \sqrt{\dfrac{5-\sqrt 5}{8}} \implies \alpha = 54^\circ $.
Answer: (We are grateful to Suleyman Soyler for this solution, he is a math teacher.)
By power of $D$ w.r.t given circle: $|DC|^2=|DE|\cdot |DB|$ and $|AD|=|DC|$. Therefore
$$|AD|^2=|DE|\cdot |DB|$$
implies $\triangle ADB \sim \triangle EDA$ (by side-angle-side). Hence we find that
$$ \angle ABD = \angle EAD \tag{1}$$
$$ \angle DEA = \angle DAB = 36^\circ \tag{2}$$
Now, let's draw circumcircle of $\triangle AED$ and ray $[DF$. Let's say intersection of them $K$. By inscribed angles
$$ \angle EAD = \angle EKD \tag{3}$$
and
$$ \angle DKA = \angle DEA = 36^\circ \tag{4}$$
By sum of interior angles of $\triangle ADK$, we find $\angle KAF =36^\circ$ and therefore $|FB|=|FA|=|FK|$. Then
$$\angle FBK = \angle FKB = 54^\circ \tag{5}$$
On the other hand, $(1)$ and $(3)$ implies that $KFEB$ a cyclic quadrilateral. Thus $$ \angle DEF = \angle FKB = 54^\circ $$
Comment: Bravo Suleyman Soyler! Such an elegant solution!
Comment: Glad to see someone can prove it via geometry.

what is the value of angle $a$


Deleting modules from the database that aren't on the filesystem

Question: How do I remove modules that are referenced to in the database but have already been deleted from the filesystem?
This occurred because I had extra modules in my staging site, and I pushed the db to production. I would like to know how to clear out any modules from the db that aren't found on the filesystem.
Comment: Delete the related line in the system table, make sure you've cleared up any tables that the module installed (check the `hook_schema()` implementation in the .install file for details). Then clear the caches. That should be all you need to do, but always test on a dev copy first
Answer: Try the Schema module.
This module compares the live database structure with the schema structure declared by all enabled modules, reporting
on any missing or incorrect tables

The Schema module provides additional Schema-related functionality not provided by the core Schema API that is useful for module developers. Currently, this includes:

Schema documentation: hyperlinked display of the schema's embedded    documentation explaining what each table and field is for.
Schema structure generation: the module examines the live database    and creates Schema API data structures for all tables that match the
  live database.
Schema comparison: the module compares the live database structure    with the schema structure declared by all enabled modules, reporting
  on any missing or incorrect tables.
Comment: Thank you! Schema > Compare > Extra showed me a bunch of `feeds_*` tables, which I dropped. Then I could clear the performance cache without getting a PHP error from missing modules.


MySQL: sum session times via students, via program

Question: I have the following:
Programs     Students      Sessions
--------     ----------    ----------
id           id            id
name         program_id    student_id
...          ...           time_in
                           time_out

Programs have many students, students have many sessions. Each session has a duration, delineated by time_in and time_out.
What I want is a summary of total hours students spent in sessions by program. So an example result would look like:
Results:

Program Name       hours
-----------        ---------
Nursing            300 hours
Pharmacy           467 hours
PT                 598 hours

After much scrounging, I have the following query:
SELECT programs.name, a.session_sum FROM(
    SELECT students.program_id AS stu_id,
    TIME_TO_SEC(TIMEDIFF(time_out, time_in)) AS session_sum
    FROM students INNER JOIN sessions
    ON students.id = sessions.student_id
    GROUP BY students.program_id
) AS a, programs
WHERE programs.id = a.stu_id

It works...sort of. Unfortunately, it only seems to work for the first session found for a given student and not all the sessions that student might have.
I'm at the edge of my MySQL knowledge getting this far. What am I missing? Should I scrap this query and go in an entirely different direction? I can also resort to looping in Rails, but I'd like to figure out a way to do things with a single query.
Thanks for any help!
Answer: In your inner SQL Statement:
SELECT students.program_id AS stu_id,
TIME_TO_SEC(TIMEDIFF(time_out, time_in)) AS session_sum
FROM students INNER JOIN sessions
ON students.id = sessions.student_id
GROUP BY students.program_id

You GROUP BY students.program_id but you don't give any aggregate function for your session_sum and this case MY SQL's default behaviour is that you only get the first result (Remark: MS SQL Server would throw an error in such a case).
So
SELECT students.program_id AS stu_id,
SUM(TIME_TO_SEC(TIMEDIFF(time_out, time_in))) AS session_sum
FROM students INNER JOIN sessions
ON students.id = sessions.student_id
GROUP BY students.program_id

should work. But I don't have a MY SQL at the hand right now to try. And by the way, to do it in really a single query, this should work, too (but I can't try right now).
SELECT
   programs.name,
   SUM(TIME_TO_SEC(TIMEDIFF(sessions.time_out, sessions.time_in))) AS session_sum
FROM
   programs LEFT JOIN students ON programs.id = students.program_id
            INNER JOIN sessions ON students.id = sessions.student_id
GROUP BY programs.id

Comment: Thanks! This was the problem. Oh, so obvious after it's been pointed out.
Answer: Why does the Students table contain program ID? The students table is primarily meant to carry information about students. What you're doing seems to also be capturing relationship between students and programs in the same table, which brings me to the question if a student is only in one program for life.
Instead, why not have the program ID in sessions table? This way you wont need to join 3 tables. I would happily associate a session with a program ID.


Python / Django render_to_string outpu rendered as text with html entities

Question: I am porting a python / Django application from:
Django==1.5.1
python version 2.6.6
to
Django==3.2
python version 3.6.8
The issue that I am having is that I have a section of code that renders to string, a particular HTML template, and then adds that to list to output elsewhere.
The actual code that produces this HTML is:
class AccountAdmin(SmarterModelAdmin):
   list_display = ('username', 'files_url', 'teamMembers', 'roleMemberships', 'safetyLimit', 'admin_quota', 'manager', 'enabled', 'path')
   list_filter = ['enabled', 'manager', 'accountType']
   search_fields = ['username']
   inlines = [RolesInline]
   valid_lookups = (
      'members__member__username',
      'teams__role__username',
   )

      roles = Account.objects.filter(teams__member=account).order_by('username')
      roleList = []
      for role in roles:
         link = '/admin/files/account/?teams__role__username=' + role.username
#        mylink = ''+role.username+''
#        linkText = format_html(mylink,myurl=link)
         linkText = render_to_string('common/contact.html', context={'URL': link, 'contact': role})
         roleList.append(linkText)
      return ', '.join(roleList)
   roleMemberships.short_description='Roles'
   roleMemberships.allow_tags=True```

I have added in a logging.warn to validate what comes out of the render_to_string, and it is straight HTML.
The commented out lines were something that I tried that fixed a similar issue.
common/contact.html  is:
{{ contact.username }}

However, on the final render,  It comes out like this:
abaumann,
abaumann
which when run through a browser looks like this:

I haven't been able to find anything that references this particular issue.  I have everything else working in the admin section of the django application, and I am at a loss for why it is not  rendering as expected.
Please let me know if there is anything else that you need to see.
Answer: I was on the right track, just had an issue with implementation.
Django backend tries to make everything secure.  As such,  the generated HTML from my template was not considered safe.  So,  using the mark_safe() function was the key.  But where to place it was the challenge.
Anytime the variable containing the HTML is modified IN ANY WAY, the "safe" flag is removed, and the variable is passed through encoding to render any HTML as text.
So the fixe is, on this line:
return ', '.join(roleList)
change it to:
return mark_safe(', '.join(roleList))
My mistake was placing the mark_safe on the linkText variable either at the time the vaiable was set, or when it was appended to the roleList.


Basis of a basis-linear algebra?

Question: Usually when we say that $v_1$ and $v_2$ are basis we imply that they are linearly independent and span the space. We by default denote $v_1$ and $v_2$ in $i$-$j$ basis. Then how is $i$-$j$ basis defined in the first place?
Comment: Wrong place to ask such a question. You should head to Mathematics.SE.
Comment: The notation $i,j$ is use, I think, on a $2$-dimensional space that already has two preferential directions that are linearly independent. For example, in $\mathbb{R}^2$, the space of pairs of real numbers, considered as a vector space with real coefficients, there are two directions, those of $(1,0)$ and $(0,1)$, that people often take a preferential. It is, of course, a choice. In an abstract vector space, there may not be a reason any pair of directions is preferential.
Answer: We do not by default use any $i-j$ basis, no matter what that actually means. A vector space is a set of things called vectors together with an algebraic structure satisfying the axioms of a vector space. Vectors can be anything in the world, including tuples like $(x,y)$, functions, matrices, or a bunch of elephants. In any such vector space, a basis is any set of linearly independent vectors that also span the entire space. Just like a vector has no $i-j$ form, nor does a basis have such a form. In some vector spaces vectors are either directly given as tuples (e.g., $\mathbb R^3$), or can be uniquely represented as tuples once a basis is chosen. If your question is how to find the coordinates vector of a given vector in a given basis, the answer is that it is pretty much immediate from the definition of a basis; spanning means coordinates exist, and linear independence means the coordinates are unique.

Basis of a basis-linear algebra?


C# XML Serilization & Appending new data

Question: I am trying to create an XML file with the following structure

However, this is what I get (one line):
<?xml version="1.0" encoding="utf-8"?><root><OFBM time="9:15" date="22.06.2016"><folder>file:///C:/Program Files (x86)/Cisco/Cisco AnyConnect Secure Mobility Client</folder><folder>file:///C:/Arduino223</folder></OFBM></root>

My code:
public void CreateXML()
        {
            XmlWriter writer = XmlWriter.Create(@"C:\Users\Alek\Dropbox\D\Debug\product.xml");

            writer.WriteStartDocument();

            writer.WriteComment("This file is generated by the program.");

            writer.WriteStartElement("root");
            writer.WriteStartElement("OFBM");
            writer.WriteAttributeString("time", "9:15");
            writer.WriteAttributeString("date", "22.06.2016");
            writer.WriteElementString("folder", @"file:///C:/Program Files (x86)/Cisco/Cisco AnyConnect Secure Mobility Client");
            writer.WriteElementString("folder", @"file:///C:/Arduino223");
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();
            writer.Close();
        }

How do I also append new data? Instead of creating a new XML each time.
Comment: Look this article - http://stackoverflow.com/questions/4094180/indentation-and-new-line-command-for-xmlwriter-in-c-sharp. There is a solution for adding indentation. Second issue: http://stackoverflow.com/questions/20922835/appending-an-existing-xml-file-with-xmlwriter - how to  add xml to existing file.
Answer: Add some settings to achieve the indent you are trying to accomplish in your XML file:
 //I added this part (XmlWriterSettings):
 XmlWriterSettings mySettings = new XmlWriterSettings();
 mySettings.Indent = true;
 mySettings.IndentChars = ("    ");
 mySettings.CloseOutput = true;
 mySettings.OmitXmlDeclaration = true;

//Added the settings you intialize and set on your XmlWriter:
 XmlWriter writer = XmlWriter.Create(@"C:\Users\Alek\Dropbox\D\Debug\product.xml", mySettings);

 writer.WriteStartDocument();

 writer.WriteComment("This file is generated by the program.");

 writer.WriteStartElement("root");
 writer.WriteStartElement("OFBM");
 writer.WriteAttributeString("time", "9:15");
 writer.WriteAttributeString("date", "22.06.2016");
 writer.WriteElementString("folder", @"file:///C:/Program Files (x86)/Cisco/Cisco AnyConnect Secure Mobility Client");
 writer.WriteElementString("folder", @"file:///C:/Arduino223");
 writer.WriteEndElement();
 //writer.WriteEndElement();
 //writer.WriteEndDocument();
  writer.Flush();
  writer.Close();

To append new data without recreating the file, you can do this:
 XDocument doc = XDocument.Load(@"C:\Users\Alek\Dropbox\D\Debug\product.xml");
 XElement school = doc.Element("root");
 school.Add(new XElement("OFBM",
                   new XElement("folder", "file:///C:/Arduino223")));
 doc.Save(@"C:\Users\Alek\Dropbox\D\Debug\product.xml");


Spelling suggestions for MultiAutoCompleteTextView in Android?

Question: I'm trying to bring up spelling suggestions for words when the user is typing in a MultiAutoCompleteTextView widget, like how Android will give you word suggestions as you type in their messaging app. I've set android:inputType="textAutoCorrect" but there are still no spelling suggestions appearing. Does anyone know a way to do this?
Answer: I haven't tried implementing a solution from these recent findings yet, but the constant textAutoCorrect under http://developer.android.com/reference/android/widget/TextView.html#attr_android:inputType may prove useful. Also, look into the Android Spelling Checker framework here http://developer.android.com/guide/topics/text/spell-checker-framework.html.
Answer: here's my solution
<code>edit.setRawInputType(edit.getInputType() & ~EditorInfo.TYPE_TEXT_FLAG_AUTO_COMPLETE);
</code>
this shows me words at the top of the [google] keyboard as I type just like in a normal EditText
and my custom Tokeniser and Adapter still show me stuff!


How to test qualities about a number's prime factorization in maple?

Question: I'm trying to write a code to test the following properties of a composite number $n$. To be more specific, for a given $n$ from (say) 200-300, I want to check if $n$ satisfies any of the following:
If $n$ has 3 or more odd prime factors
If $n=2^rp^{\alpha}$ where $r>2$
If $n=2^rp^{\alpha}q^{\beta}$ where $r>1$, or
If $n=2p^{\alpha}q^{\beta}$ where $p\equiv q\equiv 1\pmod{4}$
Where $p, q$ are odd prime numbers and $\alpha, \beta>1$.
I'm still pretty new to Maple, so any tips or pointers you can give would be much appreciated. I figure some kind of for loop would be best but I have no idea how I would do the conditional for those.
Comment: You might like to use $\mathtt{ifactor()}$, which mostly plays nicely with $\mathtt{op()}$.
Answer: The following procedure does all four of the tests you mentioned, returning a list of four booleans.
<code>Tests:= proc(n::integer)
local F:= ifactors(n)[2];
      if n::{prime, nonpositive, identical(1)} then
           [false$4]
      else
           [nops(F) > 3 or F[1,2] > 2 and nops(F) > 2,
            nops(F)=2 and F[1,2] > 2 and F[2,2] > 1,
            nops(F)=3 and F[1,2] > 1 and F[2,2] > 1 and F[3,2] > 1,
            nops(F)=3 and F[1,2] = 1 and irem(F[2,1], 4) = 1 and F[2,2] > 1
                                     and irem(F[3,1], 4) = 1 and F[3,2] > 1
           ]
      end if
 end proc:
</code>
Answer: Below is a Maple procedure for checking the second  case ($n=2^rp^{\alpha}$ where $r>2$)
<code>    IFak:=proc(n)  local S,R: R:="FALSE": S:=[op(ifactors(n)[2])]:
 if nops(S)=2 and S[1][1]=2 and S[1][2]>2  then R:="TRUE" end if:
 R
  end proc;
</code>
Now, IFak(100) gives "FALSE" and IFak(24) gives "TRUE".
You may write something like and for next cases.
Comment: Maple has predefined boolean constants **false** and **true**. Replacing these with string values is really crude.
Comment: yes, your procedure is very elegant but  incorrect :)  for example Tests(24) gives [false, false, false, false]
Comment: Note that the OP has said that for all tests $\alpha,\beta > 1$. Thus, 24 fails all the tests.
Comment: Oh sorry, You are right


Complex Types support in Azure Search .Net SDK

Question: I want to know whether complex types are supported in the azure search sdk for .net?
I already know that the azure team is providing a private preview of this feature. But the last information I have is that it can be accessed only directly via REST Api. Is there a .net sdk support for the same?
Answer: Complex Types is now generally available and is supported in version 9 of the Azure Search .NET SDK
Comment: Thanks @Bruce for that prompt response. Eagerly waiting for that release.


Comparação de inteiros em Java

Question: Integer valor = 127;
Integer valor2 = 127;

System.out.printIn(valor == valor2);

Output: true

Integer valor = 128;
Integer valor2 = 128;

System.out.printIn(valor == valor2);

Output: false

Porque isso acontece?
Comment: Acredito que tenha a ver com algum autoboxing que essa classe realiza internamente. Posso chutar que, por ter 127, ela faz autoboxing para byte, o que justifica o resultado da primeira retornar true. Já na segunda, parece que não ocorre autoboxing nem pra int, e acaba por comparar objetos, o que justifica o resultado false, já que objetos não podem ser comparados corretamente com `==`
Answer: A classe Integer faz parte do pacote de classe Wrapper e não de tipo primitivo de dados, como o int. O que chamamos de Wrapper é uma classe que representa um tipo primitivo. Por exemplo o Wrapper de int é o Integer. Perceba que o Wrapper começa com letra maiúscula, ele segue a mesma nomenclatura que qualquer outra classe, pois ele também é uma.
Por tal motivo o operador == não é recomendado para comparar valores da classe Integer. Para isso usamos outro comparador, o equals.
Exemplo 1:
Integer valor = 127;
Integer valor2 = 127;

System.out.println(valor == valor2); // Saída: true
System.out.println(Objects.equals(valor, valor2)); // Saída: true

Perceba que nos dois casos retorna true.
Agora vejamos outro exemplo:
Integer valor3 = 128;
Integer valor4 = 128;

System.out.println(valor3 == valor4); // Saída: false
System.out.println(Objects.equals(valor3, valor4)); // Saída: true

Mas a pergunta é: por que o == funciona para o Integer 127 e não para o Integer 128?
A resposta é simples: A JVM está armazenando valores inteiros em cache. Portanto o == funciona apenas para números entre -128 e 127. Este cache é mencionado na documentação do método valueOf, que, a primeira impressão é chamado internamente quando se faz autoboxing, um recurso que automaticamente converte tipos primitivos em seu wrapper equivalente.
Observação: Caso utilizasse o tipo primitivo de dados int uma simples comparação utilizando o comparador == funcionaria.
int valorTeste = 128;
int valorTeste2 = 128;

System.out.println(valorTeste == valorTeste2); // Saída: true

Comment: É interessante notar que este cache é mencionado [na documentação do método `valueOf`](https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#valueOf(int)), que [parece ser o método chamado internamente quando se faz autoboxing](https://stackoverflow.com/q/31445024)
Comment: Exatamente @hkotsubo, bem lembrado.
Answer: Integer valor = 127;

cria um Objeto da classe Integer que representa o respectivo inteiro, ao contrário de
int i = 127;

que apenas cria um primitivo.
O operador == serve apenas para testar se se trata da mesma instância, não compara o conteúdo das instâncias. Portanto normalmente irá retornar false para duas instâncias de Integer mesmo que representem o mesmo valor.
A classe Integer possui um mecanismo para armazenar uma certa quantidade de instâncias (cache) para evitar a criação de novas instâncias repetidas. Normalmente esse cache é especificado para armazenar os inteiros entre -128 e 127 (documentação). O valor máximo pode ser alterado através da propriedade java.lang.Integer.IntegerCache.high. O mínimo é fixo em -128. O compilador usa esse método para a criação (ou conversão de int) do Integer.
Então se usar
Integer valor = 127;

O compilador está traduzindo isso para algo semelhante à
Integer valor = Integer.valueOf(127);

e acaba usando a instância armazenada no cache, também para
Integer valor2 = 127;

e o resultado de valor == valor2 vai ser true pois se trata da mesma instância.
Se por exemplo fizer
Integer valor2 = new Integer(127);

estará criando uma nova instância e o resultado de valor == valor2 vai ser false.
Obs: semelhante acontece na classe String onde os valores constantes são internalizados, ver método intern(). Exemplo: new String("abc") != "abc"
Answer: Primeiro é importante entender que Integer é uma classe em java, enquanto int é um tipo primitivo.
Desta forma, ao usar Integer valor está criando um objeto. Isso serve, por exemplo, para fazer operações que um tipo primitivo não pode fazer, como conter nulos ou servir de chave para um objeto HashMap, que requer um objeto como chave.
Assim, a sua comparação se fosse feita usando int, retornaria true:
int valor-18;
int valor2 = 128;
System.out.println(valor == valor2);

Por que a diferença com Integer, sendo que com 127 retornar true?
Lendo essa resposta do SO: https://stackoverflow.com/a/1700109/4730201
É a apresentada a documentação do java: http://java.sun.com, que cita, em tradução livre:

Se o valor p sendo boxed for verdadeiro, falso, um byte, um
  caractere no intervalo \ u0000 a \ u007f ou um int ou um número curto
  entre -128 e 127, então faz r1 e r2 serem os resultados de quaisquer
  duas conversões de boxing  p. É sempre o caso que r1 == r2

Lembrando que boxing é o processo de conversão de um tipo de valor (value type) para um objeto de um classe, ou para o tipo de interface de uma classe, por exemplo, converter um tipo primitivo int para uma classe Integer.
Em resumo, o problema da diferença nos valores 128, é que até 127, o java faz esse boxing, resultando em dois objetos iguais, e o valor 128 ou mais se comporta diferente, gerando dois objetos não necessariamente iguais.
Se o boxing não for automático, mesmo com o valor 127 resultará em objetos diferentes, teste assim:
Integer valor = new Integer(127);
Integer valor2 = new Integer(127);

Como Integer gera um objeto, se você fizer assim, retornará true:
Integer valor = new Integer(128);
Integer valor2 = valor;

Montei um fiddle com esses testes: jdoodle.com/a/DAM

Comparação de inteiros em Java


Como fazer um select no formato de data?

Question: Como faço para realizar um select utilizando datas no SQL server 2008?
Quando eu faço uma busca com esse select:
select * from NOME_TABELA where DATA_FISCAL between '2016-05-01' and '2016-05-11'

O resultado são 12 registros inclusive um registro tem a informação da data como "2016-05-11 00:00:00.000".
Quando faço o select:
" select * from NOME_TABELA where DATA_FISCAL like '2016-05-11 00:00:00.000' "

ou
" select * from NOME_TABELA where DATA_FISCAL = '2016-05-11 00:00:00.000' "

O SQL não encontra nenhum registro.
Outra dúvida que me intriga, é que quando vou dar um insert nesta tabela, aparece este erro.
"Falha ao converter data e/ou hora da cadeia de caracteres."
Alguém sabe o motivo deste erro? e como resolver ?
Comment: Bem vindo ao SOPT. Teria como [edit] sua pergunta, e colocar a estrutura da tabela?
Answer: Tenta assim:
Primeiro faça um select para verificar se o formato é date ou datetime.
Em seguida use os seus parâmetros dessa maneira -
//Use o seu formato de data
SELECT * FROM NOME_TABELA
WHERE DATA_FISCAL BETWEEN #07/04/1996# AND #07/09/1996#;

Fonte
Answer: Respondendo, o erro no insert se dá porque você tenta informar a data num formato não esperado pelo banco, e o resultado inesperado no select se dá pelo mesmo motivo.
Explicando: o formato da data como string aceito pelo SQL Server, por padrão, depende da linguagem do usuário que se está usando para conectar ao banco, a qual é definida, por padrão, de acordo com a  linguagem de instalação do próprio SQL Server, a qual é definida, por padrão, de acordo com a linguagem do sistema operacional.
Mas a linguagem do usuário pode ser sobrescrita também por configurações da sessão, e o próprio formato de data aceita como string também pode ser sobrescrito por configurações da sessão.
Quando você acha que encontrou o formato correto e passa a utilizá-lo por todo o seu código, eventualmente este código vai rodar em um ambiente ligeiramente diferente e vai passar a dar erro, ou pior: vai passar a trazer resultados incorretos ou salvar no banco informações incorretas.
A boa prática é utilizar parâmetros para informar datas ou qualquer outro valor nos comandos SQL, isso evita este e outros problemas em potencial.
Se precisar informar a data como string, sem utilizar parâmetros, informe-a em um dos dois formatos universalmente aceitos pelo SQL Server independentemente de configurações. A saber:

yyymmdd para data,
ou yyyy-mm-ddThh:mm:ss[.mmm] para data e hora.

Isso vai servir tanto para insert quanto para select ou qualquer outro comando.
Obs: estes dois formatos são padrão ISO.
No seu caso, o select informando data e hora ficaria assim:
select * from NOME_TABELA where DATA_FISCAL = '2016-05-11T00:00:00.000'


Como pegar parametro de um POST em aplicação REST

Question: Iniciei uma aplicação Rest e tenho uma classe java createPost
<code>    POST
    @Path("/post/")
    @Produces(MediaType.APPLICATION_JSON)
    public static String createPost(@FormParam("loopID") String loopID) throws IOException {
         ConnectDAO dao = ConnectDAO();
         Post post = new Post();
         post.setLoopId(loopID);
    }
</code>
Onde ele vai receber por parametro esse "valor".
No javascript tenho que criar um serviço já que utilizo angular chamado createPost, e ele vai fazer o "POST".
<code>createPost: function(loopID) {
            if (!loopID) {
                hyatt.ui.view.alert('There is no file to upload. Please choose a file.');
            } else {
                return http({
                    method: 'POST',
                    url: url + '/post/',
                    headers: {
                        'Content-Type': undefined
                    },
                    transformRequest: formData
                });
            }
            // This forces the break of execution.
            throw new Error('The UploadService service needs a file and this file cannot be a video');

        },
</code>
Acima estou recebendo por parametro o valor que quero passar para o JAVA.Como posso estar fazendo isso? tentei com o @FormParam mas não deu certo.
Obs: Não estou podendo no momento pegar via @PathParam já que a url chama um serviço externo.
Quem puder ajudar agradeço.
Answer: Tente assim.
<code>@POST
@Path("/post/")
@Produces("application/json ; charset=UTF-8")
public String createPost(String loopId){
    ConnectDAO dao = ConnectDAO();
    Post post = new Post();
    post.setLoopId(loopID);
    return "";
}
</code>
Comment: restornou pra mim o seguinte erro: 204 - No Content. Acredito que tenha resolvido no sentido de pegar o parametro...
Comment: Vc debugou o codigo java pra ver se o método chegou a ser executado e se o parametro loopId foi preenchido?
Comment: sim! de qualquer forma consegui assim no JS
var data = '&idLoop=' + idLoop + '&title=' + title + '&folderID=' + folderID;
                return http.post(urlYapmo + '/post/' +
                    hyatt.date.getParameterTimeStamp(), data);


.htaccess , How to redirect any .jpg page to its attachment page (HTML)

Question: I want to redirect

www.domain.com/images/Apple.jpg

to

www.domain.com/Apple.html

Here is the Live working example, This is what i exactly want
http://more-sky.com/data/out/2/IMG_21306.jpg
How it redirect to its parent page i.e (http://more-sky.com/WDF-21306.html)
Comment: do you have a set of records, that `this image` should be redirected to `that page`?
Answer: Assuming you have mod_rewrite enabled in your Apache:
RewriteEngine On
RewriteBase /
RewriteRule ^images/([^\.]+)\.(jpe?g|png) /$1.html  [QSA,L]

Edit: as noticed by Wh1T3h4Ck5, QSA (Query String Append) flag would be optional, as well as L (Last) would depend on the context and other rules present. Please, read about RewriteRule flags for more information.

To clarify: this solution assumes that an HTML document with the name of the image already exists. Something like:

        <title>Apple</title>

This is not an optimal solution for a large number of images, but it seemed to fit OP's case.
Comment: `QSA` appends the query string (if any) to the redirected URL. You're right on pointing this out. It should be totally optional.
Comment: Yeah, I know that. Asked just because `/$1.html` replacement has no GET parameters to be appended and seems logical to me that QSA directive does nothing in this rule ;) Anyway, answer is correct despite QSA.
Comment: Thanks for your help, but this method also changing the extension of image file(image.jpg to image.html).. and image is not showing in the webpage
Comment: Well @Rohit, you obviously need to create an HTML page with the same name and `.html` extension for this to work. But please, read again your own question. You asked to redirect `www.domain.com/images/Apple.jpg` to `www.domain.com/Apple.html`.
Comment: @jordi nebot , i really appreciate your help,but i am getting broken image path on my webpage, after putting your code.

Let me directly come to the point what i exactly want.
I want user to redirect to my web page when they click on VIEW FULL IMAGE on Google image Search.What i am getting right now , is user is directly falling to the jpg page.If you think there's any other way to do this , then please correct me
Comment: @Rohit I'd suggest you to edit your question adding more information on what you really want to achieve to see if me or anybody else can come up with a better solution... :)


Feature selection using RFE in SVM kernel (other than linear eg rbf, poly etc)

Question: At this link, there is an example of finding feature ranking using RFE in SVM linear kernel.
If I want to check feature ranking in other SVM kernel (eg. rbf, poly etc).How to do it?
I have changed the kernel in the code from SVR(kernel="linear") to SVR(kernel="rbf"),
from sklearn.datasets import make_friedman1
from sklearn.feature_selection import RFE
from sklearn.svm import SVR
X, y = make_friedman1(n_samples=50, n_features=10, random_state=0)
estimator = SVR(kernel="linear")
selector = RFE(estimator, 5, step=1)
selector = selector.fit(X, y)
selector.ranking_

and then I get this error
ValueError: coef_ is only available when using a linear kernel

Question: How to check feature ranking in other SVM kernels eg rbf, poly etc?
Answer: To use RFE, it is a must to have a supervised learning estimator which attribute coef_ is available, this is the case of the linear kernel. The error you are getting is because coef_ is not for SVM using kernels different from Linear. It is in RFE documentation
A walk-around solution is presented in Feature selection for support vector machines with RBF kernel by Quanzhong Liu et. al.
Comment: I dont understand what was the the point of giving -ve points on this question. I am clearly asking how to check feature ranking in other SVM kernels (which are not linear)


Custom HTTP handler configuration fail

Question: I am developing an ASP .Net website.
I have created a custom HTTP handler to respond to requests aiming resources with .videoImage extension.
Here are the first lines of the file corresponding to my handler :
<%@ WebHandler Language="C#" Class="CompleteSubtitles.VideoImage" %>

using System;
using System.Web;
using System.IO;
using SubtitleSounds.DataManagement;

namespace CompleteSubtitles
{
    public class VideoImage : IHttpHandler
    {
        ...
    }
}

The handler file is located in a subfolder of the website root folder.
I have configured my handler in my website root web.config file as follows :
<configuration>
    <system.web>
        ...

        <httpHandlers>
          <add verb="*" path="*.videoImage" type="CompleteSubtitles.VideoImage" />
        </httpHandlers>
    </system.web>
</configuration>

I have got an ASP .Net error message when loading a page informing me that the loading of CompleteSubtitles.VideoImage type failed.
Does anyone know why ?
Any help will be greatly appreciated.
Comment: in the 'type' attribute, you need to give Assembly name and then type name probably like this for your case `type="CompleteSubtitles, CompleteSubtitles.VideoImage"`
Answer: Whitout the exact error message I can't be sure, but when from my experience with handlers and Web Forms, this works:

Your Handler can't be a simple VB/CS file, even if you place it on APP_CODE folder (never worked with me). You need place in a DLL, I always use a separate Class Library for that.
If the host uses IIS 7, system.web/httpHandlers don't work, you need to add system.webServer. I keep both just in case. Here is a sample (bNet.Ferramentas is my DLL file):

>
<system.web>
    <httpHandlers>
        <add verb="*" path="sitemap.ashx" type="bNet.Ferramentas.SiteMapHandler, bNet.Ferramentas" />
    </httpHandlers>
</system.web >

<system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <handlers>
        <add verb="*" name="bnetSitemap" path="sitemap.ashx" type="bNet.Ferramentas.SiteMapHandler, bNet.Ferramentas"/>
    </handlers>
</system.webServer>

Answer: you have to specify the full name of the class (HttpHanlder) as follows:
 <configuration>
 <system.webServer>
 <handlers>
    <add verb="*" path="*.sample" name="HelloWorldHandler" type="HelloWorldHandler"/>
 </handlers>
 </system.webServer>
 </configuration>

For more click here
Hope it will help.
Answer: Here is how I have solved my problem :
I have created a class representing my handler in my App_Code folder.
My handler file's extension is ".cs" and not ".ashx".
The declaration of my handler in my web.config is :
<httpHandlers>
  <add verb="*" path="*.videoImage" type="CompleteSubtitles.VideoImageHandler" />
</httpHandlers>

Custom HTTP handler configuration fail


Validation loss increases while Training loss decrease

Question: I am training a model and the accuracy increases in both the training and validation sets. I am using a pre-trained model as my dataset is very small.
I am not sure why the loss increases in the finetuning process for the validation:

while when training from scratch, the loss decreases similar to the training:

I add the accuracy plots as well here:
Fine tuning accuracy:

Training from scratch accuracy:

The model used in the pretraining did not have all the classes/nor exact patterns in the training set. Does that explain why finetuning did not enhance the accuracy and that training from scratch has a little bit enhancement compared to finetuning?
Extra Information:
I am using C3D model, which first divides one video into several "stacks" where one stack is a part of the video composed of 16 frames. I am trying to learn actions from videos.
The C3D model consists of 5 convolutional layers and 3 fully connected layers: https://arxiv.org/abs/1412.0767
Pretraining dataset: 11 classes, with 6646 videos divided into 94069 stacks
Training dataset: 18 classes (with 11 "almost similar" classes to the pretraining), and 657 videos divided into 6377 stacks
In the fine tuning, I do not freeze any layers as the videos in the training are in different places compared to the videos in the dataset used for the pretraining, and are visually different than the pretraining videos. I am trying next to train the model with few neurons in the fully connected layer.
Learning rate starts with lr = 0.005 and is decreased after step 4, 8, 12 by 10, 100, 1000 respectively in both the pretraining and the fine-tuning phases
Comment: Looks like pre-trained model is already better than what you get by training from scratch. It would be useful to see the confusion matrices in validation at the beginning and end of training for each version
Comment: I am using C3D model which is trained on videos rather than images, I have added the required information in the question, thanks for pointing to the missing information. I am trying next to use a lighter model, with two fully connected layer instead of 3 and to use 512 neurons in the first, while the other layer contains the number of classes (dropped in the finetuning)
Comment: Why do u mention that the pre-trained model is better? The accuracy achieved by the training from scratch is better than the accuracy with finetuning.
Comment: Looks like you are overfitting the pre-trained model during the fine tuning. How many images do you have? Do you only train a fully connected layer (they are the one with most parameters)? Also, in my experience, and I think it is common practice that you'd want a pretty small learning rate when fine tuning using a pretrained model. As your validation error shoots up and training goes down, it may be that the learning rate is too large.
Answer: This is a case of overfitting. The training loss will always tend to improve as training continues up until the model's capacity to learn has been saturated. When training loss decreases but validation loss increases, your model has reached the point where it has stopped learning the general problem and started learning the data.
You said you are using a pre-trained model? Well, it's likely that this pretrained model was trained with early stopping: the network parameters from the specific epoch which achieved the lowest validation loss were saved and have been provided for this pretrained model. If this is the case (which it likely is), it means any further fine-tuning will probably make the network worse at generalizing to the validation set, since it has already achieved the best generalization.
The reason you don't see this behaviour of validation loss decreasing after $n$ epochs when training from scratch is likely an artefact from the optimization you have used. The training rate has decreased over time so any effects of overfitting are mitigated when training from scratch. When fine-tuning the pre-trained model the optimizer starts right at the beginning of your training rate schedule, so starts out with a high training rate causing your loss to decrease rapidly as it overfits the training data, and conversely, the validation loss rapidly increases.
Since you said you are fine-tuning with new training data, I'd recommend trying a much lower training rate (e.g. 0.0005) and less aggressive training schedule, since the model could still learn to generalize better to your visually different new training data while retaining good generalization properties from pre-training on its original dataset.


Como extrair um texto específico de uma string via JavaScript

Question: Dúvida relacionada a como retirar somente uma parte do texto que fica depois das minhas tags p: e r:
Estou desenvolvendo uma aplicação similar ao chatbot simsimi e por questões de teste estou armazenando as perguntas e respostas numa var dentro do script. Para que o bot possa aprender perguntas e respostas novas, estou tentando criar um comando onde o usuário mandaria a pergunta e a resposta através do uso das tags p: e r: como no exemplo abaixo:
Exemplo:
var text = "p: pergunta r: resposta"

Para isso eu teria que pegar somente o conteúdo dentro as minhas tags mas não sei como fazer isso. Via .indexOf() eu até tentei mas não sei como limitar o que ele pega até o começo da próxima tag.
Answer: para a resposta
text.substr((text.search(/r:/)+2))

para a pergunta
text.substr((text.search(/p:/)+2),(text.search(/r:/)-2))

Comment: isso se for como no exemplo, se for em um cenário onde não sabe como vai chegar (com palavras antes do p:) o melhor é ir recortando a string
Comment: pra resolver isso eu criei um if onde só seria aceito se a ordem fosse p: e r: respectivamente, onde o p: teria que estar no começo da mensagem.
Answer: Recomendo que use 2 inputs, um para a pergunta e outro para a resposta. Temos que sempre esperar que o usuário vai fazer da maneira errada, ou seja, não botar o "p:" e o "r:" da maneira correta, se colocar.
Crie 2 inputs e depois pegue o que foi digitado neles.
Aqui tem exemplos caso você queira usar JS: https://stackoverflow.com/questions/11563638/javascript-how-to-get-value-of-text-input-field
E aqui usando JQuery:
var text= $('#input_name').val();
Comment: Sim, o problema é que a pergunta e a resposta seria envida no input onde são enviadas as mensagens
Answer: Podes usar uma RegExp para extrair esses pares pergunta/resposta.
Poderia ser algo assim:

var texto = "p: perguntaA r: respostaA p: perguntaB r: respostaB p: perguntaC r: respostaC";
var regex = /((\w:\s?)([^:]+)\s(\w:\s?)([^:]+)(\s|$))/g;
var pares = [], match;
while (match = regex.exec(texto)) {
    pares.push({
        p: match[3],
        r: match[5]
    });
}
console.log(pares);

Podes ver a regex em funcionamento aqui: https://regex101.com/r/I8yCcg/1 mas a ideia é:

um "super grupo" de captura, com dois grupos iguais dentro
(\w:\s?) refere-se a p: ou r:
([^:]+) quer dizer "algo exceto ;"
e depois desse grupo \s ou $ para garantir que há um espaço ou final de linha antes do proximo match.


if c3p0 is configured, will connection.pool_size be ignored

Question: I am using hibernate with c3p0 for connection management. In my hibernate configuration I have both connection.pool_size and hibernate.c3p0.max_size configured (with different numbers).
As per my knowledge when I am using c3p0, my default hibernate connection management will not be in use.
But I am not sure whether hibernate.c3p0.max_size is the c3p0 replacement of connection.pool_size of default hibernate pool size setting variable.
So, my query is - Can I remove connection.pool_size from the configuration?
If I have both in my configuration file, will connection.pool_size be completely ignored or will it be used for any other purpose ?
Answer: Once you defined configuration for c3po with session factory.
No need of hibernate default settings. You should remove it to avoid confusion.
Your session factory configuration looks like :
   <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
 <session-factory>
  <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
  <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:ABCXYZ</property>
  <property name="hibernate.connection.username">myuser</property>
  <property name="hibernate.connection.password">password</property>
  <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
  <property name="hibernate.default_schema">SCHEMA_A</property>
  <property name="show_sql">true</property>

  <property name="hibernate.c3p0.min_size">5</property>
  <property name="hibernate.c3p0.max_size">20</property>
  <property name="hibernate.c3p0.timeout">300</property>
  <property name="hibernate.c3p0.max_statements">50</property>
  <property name="hibernate.c3p0.idle_test_period">3000</property>

  <mapping class="com.ABCXYZ.user.DBUser"></mapping>
</session-factory>
</hibernate-configuration>

Better you remove that connection.pool_size to make configuration settings clear to understand.
Answer: You should actually remove the hibernate.connection.pool_size from your configurations as it's already being replaced by cp30's specific settings - refer to the doc

Hibernate's own connection pooling algorithm is however quite rudimentary. It is intended to help you get started and is not intended for use in a production system or even for performance testing. You should use a third party pool for best performance and stability. Just replace the hibernate.connection.pool_size property with connection pool specific settings. This will turn off Hibernate's internal pool. For example, you might >like to use C3P0.

reference - https://docs.jboss.org/hibernate/stable/core.old/reference/en/html/configuration-hibernatejdbc.html
Update
I have read that Hikari CP offers better performance than C3P0 and other DB connection pool tools, and Hikari is natively supported by spring boot, so you'll be just fine with the defaults. However, if you ever want to tweak the defaults to cater for your particular use case, you can do so.
https://github.com/brettwooldridge/HikariCP
For how to configure Hikari parameters in your application.properties, check : https://gist.github.com/rhamedy/b3cb936061cc03acdfe21358b86a5bc6


Como transformar um arquivo python em executável?

Question: Se eu criar um aplicativo em python e quiser que outras pessoas possam usá-lo, as outras pessoas vão ter que instalar o python pra poder rodar ele ou existe uma forma de tornar  ele executável? E pra criar um instalador pra esse aplicativo preciso aprender outra linguagem?
Comment: Cara, da uma olhada neste tópico e veja se te ajuda: https://pt.stackoverflow.com/questions/13806/como-criar-um-executavel-exe-em-python
Comment: Ajuda sim, valeu.
Answer: Você pode usar o cython também. Existe algum ganho de perfomace.
cython main_script.py --embed

Porém acho que a solução mais simples é utilizar o PyInstaller, funciona no Windows, Linux e MacOS. Ele cria um pacote com tudo incluso(python, libs, e o executavel).
pyinstaller main_script.py


document.ready called again when loading template using underscore.js

Question: I just tried to use underscore.js for loading the template, but when i click on login button and fire a request to server then after retrieval of response when i try to remove one template and show other, after that document.ready() is called again resulting in re-rendering of login page.
I am confused what is causing this to happen, any help is appreciated.
Here is javscript code:
$(document).ready(function(){
    /* Login-Workflow-start*/
        var loginTemplate = _.template($("#login-template").html());
        $("#main-content").html(loginTemplate);

        $("#btn_login").on('click', function(){
          // when login is clicked.
          var formData = $("#form_login").serialize()
          console.log(formData);
          var req= $.ajax({
              type: "POST",
              url: "http://localhost:3002/login",
              data: {
                "username": $('[name="username"]').val(),
                "password": $('[name="password"]').val()
              },
              success: function(responseData,textStatus,jqXhr){
                console.log("Response: " + responseData);
                // show welcome page now
                var welcomeTemplate = _.template($("#welcome-template").html());
                $("#main-content").html(welcomeTemplate);
              },
              error: function(){
                console.log("Error!!!");
              }
            });
        });
    /* Login-Workflow-end*/
});

update:
welcome-template looks like following:

Login template looks like following:

UPDATE2
In case some one want to see the entire/part(s) code for analysis then please visit at this repo on bitbucket
Comment: Event Delegation? any link or an example will be more helpful to learn and implement the stuff.
Comment: You need to show the page(s) HTML to show what is going on. Does the second loaded page have any JS in it?
Comment: give me a minute, i will update question. Well second page doesn't have any js code yet. its normal html made using bootstrap.
Comment: @TrueBlueAussie added the html pages.
Comment: if the ready function is inside the template your loading it it will execute as soon as its ready
Comment: @johnSmith ready function is under html, also these templates are under html as it is, till now i haven't re factored them.
Comment: @johnSmith  added repository link in post for details
Answer: Got the solution,
Culprit was form tag, upon clicking the submit button, form tag was firing a request too of its own, this was leading to document.ready getting re-called.
Solution: convert form tag to div, since we are handling the request creation by ourselves.
Comment: So this `document.ready` isn't `called again`, the page actually refresh, meaning it is ready a second time. The standard way to avoid this is to call `.preventDefault()` on the button click event.
Comment: @loganfsmyth Thanks for letting me know the standard way,


How to display programmaticaly default user picture?

Question: I want to display programmatically default user picture if the user is not uploaded any. For that I have written code php code but its displaying user uploaded user picture correctly but not the one default when user doesn't upload his/her own. How to do that? help me.
$user = user_load($user->uid);
$account = user_load($user->uid);
print theme(
    'image_style',
    array( 'account' =>$user,
      'style_name' => 'x-small',
     // 'path' =>!empty($user->picture->uri)?$user->picture->uri:variable_get('user_picture_default'),
      'path' =>!empty($user->picture->uri)?$user->picture->uri:variable_get('user_picture_default',''),
      'attributes' => array(
        'class' => 'avatar'
      )
    )
  );

Answer: Its very straight forward. If you want to add a default picture then go ahead and add the picture from the admin page as the other answer suggested. Programmatically,
The default picture is loaded via the variable user_picture_default.
So you can do,
$user = user_load($uid);
print theme('image_style', array('path' => $user->picture->uri, 'style_name' => '<image-style>'));

if (!empty($user->picture)) {
  print theme('image_style', array('path' => $user->picture->uri, 'style_name' => 'CUSTOM_STYLE'));
}
else {
  $custom_default_image_path = 'public://default_image.png';
  print theme('image_style', array('path' => variable_get('user_picture_default', $custom_default_image_path), 'style_name' => 'CUSTOM_STYLE'));
}

However, you can also make the default image theme dependent/independent via the hook
Answer: Why programmatically drupal provide this functionality out of the box.
Go to Configuration >> People >> Account Settings. Under personalisation section you can provide the url of the default picture and can select the image style to use for the picture.
Answer: First you need to assign a default picture path in Account settings in the drupal admin as described by Mutant Mahesh .
Then you can use this code to print picture anywhere in your code
<?php
global $user;
  $user = user_load($user->uid);
  print theme('user_picture', array('account' =>$user));
?>

Hope it will work for you.


Connect Pentaho to MS SQL Server (Native)

Question: I am very new in using PENTAHO. I need to connect this software to MS SQL Server (Native). I already try to connect it many time but every time I test the connection, failed.
This is the error message that occur every time I test the connection.
Error connecting to database [sutera] :
org.pentaho.di.core.exception.KettleDatabaseException:
Error occured while trying to connect to the database

Error connecting to database: (using class

com.microsoft.sqlserver.jdbc.SQLServerDriver)
The TCP/IP connection to the host 172.16.1.133, port 1433 has failed. Error: "connect timed out. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".

org.pentaho.di.core.exception.KettleDatabaseException:
Error occured while trying to connect to the database

Error connecting to database: (using class com.microsoft.sqlserver.jdbc.SQLServerDriver)
The TCP/IP connection to the host 172.16.1.133, port 1433 has failed. Error: "connect timed out. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".

at org.pentaho.di.core.database.Database.normalConnect(Database.java:368)
at org.pentaho.di.core.database.Database.connect(Database.java:317)
at org.pentaho.di.core.database.Database.connect(Database.java:279)
at org.pentaho.di.core.database.Database.connect(Database.java:269)
at org.pentaho.di.core.database.DatabaseFactory.getConnectionTestReport(DatabaseFactory.java:86)
at org.pentaho.di.core.database.DatabaseMeta.testConnection(DatabaseMeta.java:2464)
at org.pentaho.ui.database.event.DataHandler.testDatabaseConnection(DataHandler.java:533)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.pentaho.ui.xul.impl.AbstractXulDomContainer.invoke(AbstractXulDomContainer.java:329)
at org.pentaho.ui.xul.swing.tags.SwingButton$OnClickRunnable.run(SwingButton.java:58)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.WaitDispatchSupport$2.run(Unknown Source)
at java.awt.WaitDispatchSupport$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.WaitDispatchSupport.enter(Unknown Source)
at java.awt.Dialog.show(Unknown Source)
at java.awt.Component.show(Unknown Source)
at java.awt.Component.setVisible(Unknown Source)
at java.awt.Window.setVisible(Unknown Source)
at java.awt.Dialog.setVisible(Unknown Source)
at org.pentaho.ui.xul.swing.tags.SwingDialog.show(SwingDialog.java:234)
at org.pentaho.reporting.ui.datasources.jdbc.ui.XulDatabaseDialog.open(XulDatabaseDialog.java:256)
at org.pentaho.reporting.ui.datasources.jdbc.ui.ConnectionPanel$EditDataSourceAction.actionPerformed(ConnectionPanel.java:162)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.WaitDispatchSupport$2.run(Unknown Source)
at java.awt.WaitDispatchSupport$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.WaitDispatchSupport.enter(Unknown Source)
at java.awt.Dialog.show(Unknown Source)
at java.awt.Component.show(Unknown Source)
at java.awt.Component.setVisible(Unknown Source)
at java.awt.Window.setVisible(Unknown Source)
at java.awt.Dialog.setVisible(Unknown Source)
at org.pentaho.reporting.libraries.designtime.swing.CommonDialog.performEdit(CommonDialog.java:156)
at org.pentaho.reporting.ui.datasources.jdbc.ui.JdbcDataSourceDialog.performConfiguration(JdbcDataSourceDialog.java:759)
at org.pentaho.reporting.ui.datasources.jdbc.JdbcDataSourcePlugin.performEdit(JdbcDataSourcePlugin.java:67)
at org.pentaho.reporting.designer.core.actions.report.AddDataFactoryAction.actionPerformed(AddDataFactoryAction.java:79)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: org.pentaho.di.core.exception.KettleDatabaseException:
Error connecting to database: (using class com.microsoft.sqlserver.jdbc.SQLServerDriver)
The TCP/IP connection to the host 172.16.1.133, port 1433 has failed. Error: "connect timed out. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".

at org.pentaho.di.core.database.Database.connectUsingClass(Database.java:504)
at org.pentaho.di.core.database.Database.normalConnect(Database.java:352)
... 122 more
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host 172.16.1.133, port 1433 has failed. Error: "connect timed out. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:170)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:104
         9)
at
    com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:833)
at
    com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:716)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:841)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at org.pentaho.di.core.database.Database.connectUsingClass(Database.java:478)
... 123 more

Can anyone help me to solve this problem? I really need your help. TQ
Comment: To connect to SQL Server in native mode, Java must be able to find the sqlauth.dll file. I solved this problem by modifying the spoon.bat file and providing the path to the sqlauth.dll file there. I think there's a much better way to do that, but I haven't tried it yet. Are you sure the host and port are correct?
Comment: Yes, I'm sure the host & port are correct. Can you show me how you solve this problem? I'm very new in using this software. I just learned it for about 3 days.
Answer: Integrated Security SetupTo connect to database with integrated security, following steps should be done:Download Microsoft JDBC Drivers 4.2 on https://www.microsoft.com/en-us/download/details.aspx?id=11774Unzip the package in a temp directoryClose SpoonCopy '<temp directory>\sqljdbc_4.2\enu\auth\x86\sqljdbc_auth.dll' to 'C:\Program Files (x86)\Java\jre1.8.0_73\bin' (32-bit)Copy '<temp directory>\sqljdbc_4.2\enu\sqljdbc42.jar' to '<Kettle installation folder>\data-integration\lib'Open SpoonTest connection in spoonDelete temp directoryIf you upgrade Kettle, Java, JDBC Driver to a higher version, you have to repeat the above steps
Comment: Installing Java in a version independent folder prevents to copy the file for each java update
Comment: Works also with Microsoft JDBC Driver for SQL Server 6 and 8, only folder structure is a little different. https://learn.microsoft.com/en-us/sql/connect/jdbc/download-microsoft-jdbc-driver-for-sql-server?view=sql-server-ver15
Comment: I dont believe, 'sqljdbc_auth.dll' needs to go to 'C:\Program Files (x86)\Java\jre1.8.0_73\bin'. I believe it needs to got to the folder of Pentaho, that is: '...\data-integration\libswt\win64'.
Answer: Here is what worked for me to connect Pentaho PDI to a Microsoft SQL Server:

Download the official Microsoft JDBC Driver for SQL Server (v4.0) from here.
It comes bundled with two jar files, copy the file "sqljdbc4.jar" into your path data-integration\lib.
Restart Pentaho and try the connection MS SQL server(Native) again.

Note: I used this method for Pentaho PDI (Kettle), but it should work for the rest of products of the suite.
Note2: Even if you are using windows you can download the ".tar.gz" file instead of the ".exe" because you are using it just for Pentaho.
Answer: Adding my solution because none of the above match exactly.
I got the sqljdbc_auth.dll as was suggested in this article and just put it into the
data-integration\libswt\win64 directory and started Spoon. I was able to authenticate after that.
Answer: I dont know whether you have followed these steps or not.but what i want to mention ::
1>first add jdbc driver for ms sql server add in tomcat->lib.
sqljdbc4.jar
find the below image

I hope it is helpful.
Comment: I already follow like what you show. However, it still failed to connect. Do you have any other suggestion?
Comment: Do I need to install tomcat in server too?
Answer: For SQL Server's "Inregratedsecurity" authentication feature to work you must copy the sqljdbc_auth.dll to the BA server's "~\tomcat\bin" location and restart the BA server.
Hope this'll help!
Answer: add "sqljdbc41.jar" to \Pentaho\java\bin
and "sqljdbc_auth" (Diff for 32-bit and 64-bit version) to \Pentaho\design-tools\data-integration\lib
Answer: Just wrestled with this on my Mac.
By default the "root" directory when you unzip the archive is data-integration. I moved it over to a directory called "Pentaho" -- it's a small thing, but not exactly clear -- you want the JDBC driver in the lib sub-directory off of whatever ROOT directory you are using for Pentaho.
I used the latest JDBC driver -- sqljdbc42.jar -- works just fine -- worked just fine with the mariadb-java-client-2.1.0.jar for Aurora DB on AWS RDS.
Answer: Alright, this kept me busy for a nasty little while. This is what worked for me:
System info:

win10
PDI community edition Version 9.4
SQL Server 2019
Java install 1: jre1.8.0_361
Java install 2: jdk1.8.0_361

Download:

Driver bundle "sqljdbc_6.0.8112.200_enu.tar.gz"
from source https://www.microsoft.com/en-us/download/details.aspx?id=11774
(over time the exact name will change. the url seems to stay the same)

Actions:

shut down pentaho/spoon
unpack file "sqljdbc_6.0.8112.200_enu.tar.gz". this gets you a folder with subfolders and various files in them.
copy this file:
"sqljdbc_6.0.8112.200_enu.tar\sqljdbc_6.0\enu\auth\x64\sqljdbc_auth.dll"
to this location:
"...\data-integration\libswt\win64"
copy this file:
"sqljdbc_6.0.8112.200_enu.tar\sqljdbc_6.0\enu\jre8\sqljdbc42.jar"
to this location:
"...\data-integration\lib"
start up spoon

I hope this does it for You..
Thanks to the other posters.
Answer: Follow Bart VdA's steps, then go to Pentaho, file, new, transformation. Go to the preview tab, select transformation, click connections, new connection. Enter the default sql server settings as shown in the image below.


Quais as vantagens e desvantagens de utilizar transação explicita

Question: Além da segurança qual a vantagem de usar transação explícita? Em quais situações é recomendado?
Answer: Transações implícitas
Neste modo, o servidor irá garantir que todas as operações sejam realizadas dentro de uma transação.
Em geral são usadas para que o desenvolvedor não precise se preocupar com a lógica de tratamento transacional e tenha que repeti-la em por todo o código.
Porém, a tendência é a degradação do desempenho, pois transações são mais "caras" do ponto de vista computacional já que o banco de dados precisa realizar várias verificações adicionais e há maior probabilidade de ocorrerem locks e deadlocks em que o desenvolvedor consiga prever.
Enfim, a vantagem é em relação à facilidade de desenvolvimento ao custo do desempenho e de possíveis locks.
Transações Explícitas
Deixa a cargo do desenvolvedor iniciar e encerrar transações através de comandos:

BEGIN TRANS
COMMIT TRANS
ROLLBACK TRANS

A desvantagem é que o desenvolvedor precisa repetir a lógica de controle em todos os pontos onde transações forem necessários.
Por outro lado, isso permite um ajuste fino do desempenho, minimizando uso de transações onde não é necessário, locks e deadlocks, além de possibilitar ao desenvolvedor analisar os casos de possíveis problemas de concorrência.
Recomendações
Minha recomendação é usar sempre transações explícitas delimitando manualmente onde elas serão necessárias.
Quando o código por si só realiza operações atômicas, transações não são necessárias. Por exemplo, quando se faz apenas um comando INSERT ou UPDATE ou SELECT.
No entanto, quando há uma sequência de operações, então deve-se iniciar uma transação sempre englobando a menor quantidade de código possível, de forma a aumentar a eficiência do sistema de banco de dados para processar requisições concorrentes.
Answer: Vantagens:

Segurança em operações que envolvam várias alterações em diferentes elementos de dados, evitando que dados sejam inseridos incompletos por falhas sistêmicas;
Em caso de falha física do equipamento, torna possível a restauração do banco de dados de forma segura até o último checkpoint transacional válido;
Permite propagação correta de dados, em caso de sistemas multiusuários, organizando alterações concorrentes.

Desvantagens:

Mais lento que uma operação simples de persistência;
Pode causar deadlocks;
Transações exclusivas podem indisponibilizar o banco de dados para outros usuários, que não estejam dentro do escopo da transação, consequentemente tornando o sistema lento se as operações dentro de uma transação também forem.

Recomendado usar:

Em operações que envolva persistência de múltiplas entidades;
Em sistemas que ocorram alterações nas informações em tempo real, e estas alterações sejam propagadas para vários usuários ao mesmo tempo.


ProGuard Obfuscation Jar file & DB Connection Strings

Question: I have DB Connection Strings in code like a ;
<code>String connectionUrlForSomeCompany = "jdbc:sqlserver://192.168.131.10;" + "databaseName=CompanyName;" + "user=" + "test.user" + ";" + "password=" + "testuserpassword" + ";";
</code>
I plan distrubute this application but detect some vulnerability about Strings. When user extract jar via Winrar, all classes extracted and readable. And some classes shown above DB Connection string.
I have try Obfuscate the Jar file via ProGuard. Class names changed but contents not encrypted. So DB Connection String can find, if user try read all classes.
How can i prevent this?
Regards.
Answer: Obfuscation can and should only obfuscate the class name, variable name, package name, etc. If it obfuscate the content string, how do you expect to get the original value when your program needs them?
Obfuscation is NOT encryption.
There's no way proguard can help you in this case.
Letting the client to connect to your database is never a good idea. You can try separate the connection string to a config file, and only give the config file to you people you trust.


Small Ensemble Management

Question: I recently started a brass quintet at my university. I've been managing it more or less myself, and while I'm trying hard to do it right, I'm sure there are things that I'm missing.
We are all students in other fields (no music majors). That said, we play well enough (a grad student in the group did his undergrad in music performance) to get paid for gigs. However, the main aim of the group is to play music and enjoy doing so; we're not looking to make lots of money or start a world tour. Which brings me to my question.
Essentially, what are the most important practices for managing a small ensemble? This boils down to two main questions:

Mentorship: Should I seek out an experienced musician to give the group guidance (musical and otherwise)? How many mentors is best? How/where can I find such mentors?
Networking: How/where should I get word out about my group?

Additionally, if there is something I left out I'd love to hear about it. Thanks.
Answer: Mentorship: If you want to be a performing musician, then your teachers should be other performing musicians--your current and future peers.  Teachers in an academic setting don't always know how to perform.  I've had a stable regularly performing band for about twenty years now. Everything I learned about being a performing musician I learned from other performing musicians.  How do you find them?  They're playing all around you. Go to local gigs of bands playing the music you want to play.  Chat with them on their breaks, maybe buy them a beer and ask them questions.  Most musicians are happy to share info if they like you and they trust you.  If you're thinking about touring, buy Hal Galper's book "The Touring Musician."
Networking: You start with people who like you and your band members.  You start with your friends.  Every time you play a bar gig, you invite all of your friends--in person works better than social media, but I've had some success with Facebook and MailChimp.  Get your friends to invite their friends.  Make it a party, make it a scene.  Successful performing musicians these days--unless they have musical superpowers--make a splash by making a community.  Bookers notice communities, bookers want butts in seats.
Answer: Particularly in a University setting, you should contact the director of the school's band or orchestra. There are likely many, many small events in the school calendar where a music ensemble would be very welcome. Any sort of small reception or lunch can be made more inviting with a musical group. I was in my school's Jazz Combo which was one of a few "ensemble classes" that were set up primarily to provide groups to play at school functions. The Afro-Cuban Ensemble was also very much in demand. If the dean calls the head of the music department and the head calls the band director, he'll have your group in the rolodex.
Depending on the details of the school bureaucracy, the music department's Office Admin might be the person to talk to.

Small Ensemble Management


Use custom form submit function inside class

Question: In my controller I am loading a form and including one custom submit handler. However the submit function is not called. I think that it's either not found or not accessible as it is inside the controller class:
class MyController extends ControllerBase {

  //...

  public function myProfile() {
    $user = User::load($this->currentUser->id());
    $form = $this->entityFormBuilder()->getForm($user);

    $form['actions']['submit']['#submit'][] = '::_custom_submit_form';
  }

  protected function _custom_submit_form(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
    $form_state->setRedirect('<front>');
  }

}

If I use hook_form_alter it works, but I don't want to use a hook as I can directly manipulate the form.
Comment: I'd say this submit function needs to be public. `public function _custom_submit_form`.
Comment: doesn't work with public
Answer: To understand why your code is not working, you need to understand how the form API works:

The form definition is called
Any form alter hooks are executed, altering the form
The render array is cached
The form is rendered as HTML and sent to the browser
The user submits the form
Drupal checks that the submitted values match the cached form. If values do not match, the values are rejected by Drupal's security system

When you call this code:
$form = $this->entityFormBuilder()->getForm($user);

The caching mentioned in step 3 above has already happened. So when you do this after that line of code:
$form['actions']['submit']['#submit'][] = '::_custom_submit_form';

The submit handler is never called, since the form was cached before this line of code, and therefore your submit handler does not make it into the cache.
To add a submit handler to a form, you need to implement hook_form_alter() (or a variation), which will add the handler before the caching happens. Or, you can extend the form class you are working with, add your own ::submitForm() handler, then call the parent submit from that.
Comment: thank you for the very good and comprehensible explanation. It's a pitty that these kind of things are not documented in this way.
Comment: so if I don't want the custom submit handler added always to the form but only in one special case, the only possibility is to extend the form class?
Comment: Or you can use hook_form_alter() and add a logic check.
Comment: Thanks @Jaypan!


php-fpm and nginx super fast and timeout responses together

Question: I'm having some problem with my server:
Ubuntu 12.04
nginx 1.4.6
2GB RAM
OpenVZ VPS

I increased php-fpm settings as the default one was very low. To something like this:
pm.max_children = 48
pm.start_servers = 10
pm.min_spare_servers = 10
pm.max_spare_servers = 15
pm.max_requests = 100

and I got super fast responses but 4 out of 10 times, when I click on a link, it times out or get a very late response. If it is working and seems to be one of those long ones and I click on it again, I may get a fast response or one of those delays again.
Anyway, I reduced the number of servers to something like this:
pm.max_children = 48
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 10
pm.max_requests = 100

and now the server is working OK. I don't get a super fast response, but all connections are good and reasonable. I get responses in 1 second max.
I don't think those first numbers are high for my machine. (this is now the current situation with the site working)
total       used       free     shared    buffers     cached
Mem:          2048        604       1443          0          0        112
-/+ buffers/cache:        491       1556
Swap:          256          0        256

When it was acting weird, I remember "-/+ buffers/cache" row was about the same but the first row "Mem", was showing like 1600 used.
There's only ONE website on this server which at its busy hours gets 60-90 visits in 15 minutes period. (hardly 30 requests at the same time?)
I'm curious why higher numbers which seem to have no problem with my server resources, causes such behaviour. Also I'd like to get those super fast responses.
This is my nginx config if someone is interested. (removed the unnecessary lines like document root and which port it is listening etc etc)
user www-data www-data;
worker_processes  2;

events
{
        worker_connections 1024;
}

http
{

    include       mime.types;
    default_type  application/octet-stream;

    #access_log off;
    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     off;
    keepalive_timeout  5;
    client_max_body_size 10M;
    server_tokens off;

    gzip  on;
    gzip_comp_level 2;
    gzip_proxied any;
    gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    server
    {
            location ~ \.php$
            {
                    fastcgi_pass 127.0.0.1:9000;
                    fastcgi_index index.php;
                    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                    include /etc/nginx/fastcgi_params;
            }

     }
}

Comment: Go back to the default php-fpm settings. You don't have enough traffic to justify raising it without reason.
Comment: It is a fast growing website and I think this is good thing that happened. Because even if I go back and later on I want to upgrade I may bump into this issue again.
Answer: Have you validated that you backend server which listens 9000 and it is responding timely manner? if not tweak it your backend server.
have you looked at timeout params ref https://stackoverflow.com/questions/561946/how-do-i-prevent-a-gateway-timeout-with-fastcgi-on-nginx
Otherwise, Enable debug option in nginx and refer log files for some clue. http://nginx.org/en/docs/debugging_log.html


Summarize rows by other column values - Countif in Python/Pandas

Question: I have a df with this dataset:
customer action    date
1049381  share    9/29/2017
1049381  level_up 10/6/2017
105460   share    9/22/2017
105460   share    9/23/2017
105668   level_up 9/8/2017
105668   share    9/8/2017
105668   level_up 9/18/2017
105668   share    9/18/2017
105668   share    9/20/2017
905669   share    9/25/2017
905669   level_up 9/25/2017

I want to count (summarize) those cases when the user made "level_up" and "share" on the same day(s). Like this:
customer  share_wth_level_up
1049381         0
105460          0
105668          2
905669          1

I have started with pandas but I cannot found the solution, because it doesn't provide a summarized df for every row (unique).
df.groupby(['customer','date']).size().value_counts()

result
Answer: One solution is to use GroupBy + nunique and test for length equal to 2. Then use GroupBy + sum to total those instances.
df_grp = df.groupby(['customer', 'date'])['action'].nunique() == 2
res = df_grp.groupby('customer').sum().astype(int)

print(res)

customer
105460     0
105668     2
905669     1
1049381    0
Name: action, dtype: int32

Answer: Using duplicated filter the df firstly , then we groupby customer and date, to check all many unique value in action.
 s=df[df.groupby('customer').date.apply(pd.Series.duplicated,keep=False)].groupby(['customer','date']).action.nunique()
(s[s==2]//2).sum(level=0).reindex(df.customer.unique(),fill_value=0)
Out[166]:
customer
1049381    0
105460     0
105668     2
905669     1
Name: action, dtype: int64


How do I update the URL after changing the hosting of a website?

Question: Our website recently changed hosts. After updating the named servers the home page shows but all links in the page take you to 404 error.  The new host advises that the URL links need to be refreshed.
Comment: Did you cleared the caches?
Answer: There are a couple of possibilities when this happens. The first is to make sure that you still have an .htaccess file, as moving between hosts sometimes doesn't include the hidden files.
If you do have it, depending on the server, the Symlinks may need to be changed. For instance my Ubuntu server needs the .htaccess file to be changed to this in order for it to work
<code>#Options +FollowSymLinks // Commented out
Options +SymLinksIfOwnerMatch
</code>
The next possibility is that the DNS propagation hasn't finished yet. It can take up to 72 hours with some hosts before everything is fully refreshed and pointing the right location.


extends Component vs extends React.Component

Question: What's the difference between
type Props = {};
export default class App extends Component<Props>

And
export default class App extends React.Component

Both ways of extending seem to work without any errors.
Answer: <Props> part is just Flow feature for type checking. It will be ignored if you haven't integrated Flow.
Component and React.Component is the same class from react package, a difference only in importing:
import React from 'react';
export default class App extends React.Component

import React, { Component } from 'react';
export default class App extends Component


Lilypond: output to iPhone and iPad

Question: How can I generate pdf outputs that fit best on iPad and iPhone?
I could not find the right paper sizes in Lilypond's predefined paper sizes
Answer: It seems like there is a public variable called paper-alist which contains all the recognized paper sizes, along with their dimensions. You can easily add your own paper type as per the Lilypond docs about paper size:
#(set! paper-alist (cons '("my size" . (cons (* 15 in) (* 3 in))) paper-alist))

\paper {
  #(set-paper-size "my size")
}

(This adds a paper size called my size which is 15 inches wide and 3 inches high.) Besides the unit of in, you can also use cm and mm.

Lilypond: output to iPhone and iPad


Definir um valor para o select do Material Design Lite

Question: Eu estou tentando inserir alguns dados no select do meu formulario, estou usando mdl-select
O problema é que estou tentando inserir um valor mas simplesmente não funciona.
Gostaria que o valor buscado no banco de dados fosse inserido no select. o que esta errado?

<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://code.getmdl.io/1.3.0/material.indigo-pink.min.css">

<link rel="stylesheet" href="http://creativeit.github.io/getmdl-select/getmdl-select.min.css">

        <input type="text" value="" class="mdl-textfield__input" id="sample1" readonly>
        <input type="hidden" value="" name="sample1">
        Country

Germany

Belarus

Russia

Comment: Note que o valor RUS some ao clicar em outra parte da tela.
Comment: Não deu para entender, está funcionando normal.
Comment: O valor esperado para RUS é  Russia, alem disso o valor some da tela. Analisando no Dev Tools do Chrome é possivel ver que a tag value fica vazia.
Answer: Para pré selecionar uma opção é necessário usar o atributo data-selected, você pode adiciona-lo diretamente ao HTML:

Russia

Ou pelo jQuery:
$('[data-val="RUS"]').attr('data-selected', true)

Exemplo funcionando:

<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://code.getmdl.io/1.3.0/material.indigo-pink.min.css">

<link rel="stylesheet" href="http://creativeit.github.io/getmdl-select/getmdl-select.min.css">

    <input type="text" value="" class="mdl-textfield__input" id="sample1" readonly>
    <input type="hidden" value="" name="sample1">
    Country

Germany

Belarus

Russia

Ainda é necessário manter a linha que seta o valor:
$('#sample1').val('RUS')

Se não, apesar de visualmente selecionado, o valor do input será uma string vazia


How do you get the text inside only the  tag using requests.get()?

Question: I have tried using :
<code>from json import loads
from requests import get

text_inside_body_tag = loads(get('https://Clicker-leaderboard.ge1g.repl.co').content)
</code>
But it either gives an error about loads using bytes object or when I remove 'loads' , it returns the whole html code while I only want the code in the  tag.
Could anyone help me?
Comment: Have a look at `BeautifulSoup` (bs4).
Comment: Thank you! I will look into BeautifulSoup.
Answer: BeautifulSoup (bs4) is a great module to work with HTML data.
<code># Import required modules
from bs4 import BeautifulSoup as bs4
import json
import requests

# Retrieve page content
html = requests.get("your url").content
# Create BS4 object to handle HTML data
soup = bs4(html, "lxml")

# Extract text from body tag and remove \n, \s and \t
body = soup.find("body").text.strip()
# Create dictionary from extracted data
data = json.loads(body)
</code>


Is there a GZIP merger that merges two GZIP files without decompressing them?

Question: Let's say there's a.gz, and b.gz.
$ gzip_merge a.gz b.gz -output c.gz
I'd like to have this program. Of course,
$ cat a.gz b.gz > c.gz
doesn't work. Because the final DEFLATE block of a.gz has BFINAL, and the GZIP header of b.gz. (Refer to RFC1951, RFC1952) But if you unset BFINAL, throw away the second GZIP header and walk through the byte boundaries of the second gzip file, you can merge it.
In fact, I thought of writing an open source program for this matter, but didn't know how to publish it. So I asked the Joel to be my program manager, and I walked him through my explanation and defense, he finally understood what I wanted to do, but said he was too busy. :(
Of course, I could write one myself and try my way to publish it. But I can't do this alone because my day work belongs to the property of my employer.
Is there any volunteers? We could work as programmer(me), publisher(you) or programmer(you), publisher(me). All I need is some credit. I once implemented a Universal Decompressor Virtual Machine described in RFC3320. So I know this is feasible.
OR, you could point me to THAT program. It would be very useful for managing log files like merging 365 (day) gzipped log files to one. ;)
Thanks.
Comment: If you have an MVC framework and want to speed up the delivery of a header template, a dynamic-content template, and a footer template, you'd choose compression. If you compress a header template and a footer template in advance, it could speed up things.
Comment: Just out of curiosity, *why* would you want to do this? If you just want to represent something as a single file, why not just tar it up?
Answer:
Of course, cat a.gz b.gz > c.gz doesn't work.

Actually, it works just fine.  I just tested it.  It's even documented (sort of) in the gzip man page.
   Multiple  compressed  files  can  be concatenated. In this case, gunzip
   will extract all members at once. For example:

         gzip -c file1  > foo.gz
         gzip -c file2 >> foo.gz

   Then

         gunzip -c foo

   is equivalent to

         cat file1 file2

Comment: Oh, it works like a charm! Thank you! I suppose it all works with a Perl implementation like PerlIO::gzip.
Comment: For creating the files, I would expect no problem.  For reading them, in the worst case you could use a loop or shell out to zcat/gunzip.
Answer: You could also:
zcat a.gz b.gz > c.txt && gzip c.txt

as long as your Linux/Unix distribution has zcat built in, which most of them do (and you could install it for the ones that do not.)
Alternatively:
zcat a.gz b.gz | gzip -c > c.txt.gz

Comment: This would be my solution when starting with files already compressed. The solution from Andru Luvisi starts with uncompressed files.