Erro ao tentar criar uma conexão com o banco de dados (MySQL)

Question: Tentei fazer uma conexão com o banco de dados MySQL usando AsyncTask, mas está dando erro na classe responsável por pela conexão, a MysqlConnect:

The constructor Log() is not visible

Estou seguindo este tutorial e meu problema está no seguinte trecho da MysqlConnect:
public MysqlConnect(Context context) {
    this.context = context;
    log = new Log();
}

Minha MysqlConnect.java:
package br.com.alerts;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

public class MysqlConnect extends AsyncTask<Void, Void, Boolean> {

    private static final String url = "jdbc:mysql://mysql.hostinger.com.br/u259053814_table";
    private static final String user = "root";
    private static final String pass = "root";
    private Connection con;
    private Log log;
    private Context context;
    private ProgressDialog dialog;

    public MysqlConnect(Context context) {
        this.context = context;
        log = new Log();
    }
    public Boolean isConected() {
        try
        {
            if (con == null)
            {
                return false;
            } else {
                return (!this.con.isClosed());
            }
        } catch (SQLException e) {
            return false;
        }
    }
    public String getLog() {
        return log.toString();
    }
    public boolean connect() {
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection(url, user, pass);
            log.add("Conectado com sucesso!");
        } catch(Exception e) {
            e.printStackTrace();
        }
        return isConected();
    }
    public void disconnect() {
        try {
            con.close();
            con.isClosed();
            log.add("Desconectado!");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(this.context);
        dialog.setMessage("Aguarde... conectando ao banco de dados...");
        dialog.show();
    }
    @Override
    protected Boolean doInBackground(Void... params) {
        connect();
        return isConected();
    }
    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        dialog.dismiss();
    }
}

Answer: Você está usando log = new Log(), mas esta classe do android não tem construtor disponível. O correto e utilizar os métodos estáticas da classe Log:
Log.d("tag","algum log"); // Debug
Log.e("tag","algum log"); // Error
Log.w("tag","algum log"); // Warning
Log.v("tag","algum log"); // Verbose
Log.i("tag","algum log"); // Info

Seu código:
public class MysqlConnect extends AsyncTask<Void, Void, Boolean> {

    private static final String url = "jdbc:mysql://mysql.hostinger.com.br/u259053814_table";
    private static final String user = "root";
    private static final String pass = "root";
    private Connection con;
    //private Log log;
    private Context context;
    private ProgressDialog dialog;

    public MysqlConnect(Context context) {
        this.context = context;
        //log = new Log();
    }

    //public String getLog() {
    //    return log.toString();
    //}
    public boolean connect() {
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection(url, user, pass);
            //log.add("Conectado com sucesso!");
            Log.d("","Conectado com sucesso!");
        } catch(Exception e) {
            e.printStackTrace();
        }
        return isConected();
    }
    public void disconnect() {
        try {
            con.close();
            con.isClosed();
            //log.add("Desconectado!");
            Log.d("","Desconectado!");
        } catch (SQLException e) {
            e.printStackTrace();
        }
   }

   /// ...

}

Comment: mas como adapto ao meu codigo?
Comment: remove ou comenta as linha `log = new Log();` , `private Log log;` e a função `getLog` aonde esta usando a sua variável `log` passa a usar `Log.d("","a sua mensagem");`
Comment: Vou testar aqui e dou um parecer do resultado..


Kubernetes cloudnativepg operator local/nfs backups

Question: I wanted to test the cloudnativepg operator for a local cluster and would like to save the backups on a local dir. The problem is that the documentation only has information regarding cloud solutions https://cloudnative-pg.io/documentation/1.20/backup_recovery/ and I can't seem to make it work with directories. I know it is somewhat of a long shot since not many people use this operator. I was thinking of something like this:
<code>62 │ backup:
63 │ │ barmanObjectStore:
64 │ │ │ destinationPath: /data
65 │ │ │ wal:
66 │ │ │ │ compression: gzip
67 │ │ │ │ encryption: AES256
68 │ │ │ data:
69 │ │ │ │ compression: gzip
70 │ │ │ │ encryption: AES256
71 │ │ │ │ immediateCheckpoint: false
72 │ │ │ │ jobs: 2
</code>
Answer: you could deploy minIO locally and then push the backups there. You can use this link on how to deploy minio locally and then use the manifest provided by CloudnativePG to push and store the backups there.
manifest_img


Colocar validação em Identity do Asp Net MVC

Question: Tentei localizar essa duvida no forum e não consegui encontrar. Tenho uma aplicação MVC Asp Net que utiliza para o login o Entity Framework com Identity. Meu cadastro de cliente funciona perfeitamente, mas não estou conseguindo validar dois campos: Username e Login. Tentei localizar onde eles são chamados, mas pelo o que li na net até agora, ele fica "encapsulado" e não consegui colocar as validações neles. GOstaria de colocar uma validação como essa neles:
<code>[Required]
    [StringLength(10, MinimumLength = 6)]
    public string Senha { get; set; }
</code>
O problema é que não estou conseugindo encontrar Username e Login. Não consegui encontrar o AccountViewModels.cs que me indicaram na net. Alguem pode me ajudar por favor? Gostaria apenas de validar os campos de Username e Login para que não seja possivel salvar o cadastro com eles em branco, como não encontrei a localização deles, não consigo validar e a maneira mais facil que encontrei foi como informei no código acima.
Comment: se você salva ele certamente esta passando o valor para eles em sua action ... só dar um F12 em cima da classe.
Answer: Para registrar um usuário, o identity no aspnet mvc 5 utiliza a classe RegisterViewModel que esta no arquivo AccountViewModels.cs dentro da pasta Models.
<code>public class RegisterViewModel
{
    [Required]
    [EmailAddress]
    [Display(Name = "Email")]
    public string Email { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
}
</code>
Esta é a classe recebe o email, a senha e a confirmação de senha da tela de registro. Aqui você pode customizar as validações a vontade.
Comment: Tentei encontrar esse arquivo AccountViewModels.cs  no meu projeto na pasta Models e não encontrei. Sabe se ele pode ter outro nome? Procurei em todo o projeto aqui e não localizei esse arquivo da Model nem a classe RegisterViewModel
Comment: Qual versão do .net esta usando? Esta usando o identity que tem no template?
Comment: seria essa  versão, certo?
Comment: Ta bem estranho não ter esses arquivos. Pode subir uma screenshot das pastas de model e controller do seu projeto para eu ver como esta?
Comment: subi a imagem das pastas aqui https://uploaddeimagens.com.br/imagens/pastas-jpg--3
Comment: Pelos seus arquivos vc nao ta usando o identity que vem no template do aspnet mvc 5. Voce implementou seu proprio sistema de login?
Comment: Isso, na verdade segui um curso que ensinava afazer esse sistema de login. O curso já encerrou e vi que ficaram alguns pontos pendentes, como essas validações e não estou mais conseguindo entrar em contato com eles.
Comment: Então pra responder sua pergunta teria que ter acesso ao seu codigo de registro de usuário (controller e viewmodel) pra poder te ajudar. Usando o identity padrão do aspnet mvc a resposta é a que dei mesmo.


SQL Retention Rates

Question: I am trying to construct a rolling retention measure but am having troubles figuring how to do it in redshift.
I have defined retention as the intersection between two sets of users. The first a cohort of distinct user ids who 90 days from todays date had been active at least once 30 days from that date (between 90 and 120 days from today). The second is the number of those users who were active in the last 30 days from today.
Retention = Todays 30 day active users who were in original cohort 90 days ago / 30 day active suers 90 days ago
My sessions table looks like this:

id
created_date

1
2021-03-04

1
2021-01-01

1
2020-12-15

2
2021-02-17

The only way I can seem to do this is as follows:

Create a temple table and insert into for todays date.

with t1 as (
    select distinct customer_id id
    from sessions
    and created_date >= dateadd('day', -29, current_date)
    )
, t2 as (
    select distinct customer_id id
    from sessions
    and created_date <= dateadd('day', -89, current_date)
    and created_date >= dateadd('day', -119, current_date)
    )
select current_date,
    count(t1.id) as original,
    count(t2.id) as current,
    round(cast(count(t2.id) as float) / cast(count(t1.id) as float), 2) as ratio
into temp table temp1
from t1
left join t2
on t1.id = t2.id

Run an insert statement into the temp table multiple times subtracting one day from current date in each query

insert into temp1
with t1 as (
    select distinct customer_id id
    from sessions
    and created_date >= dateadd('day', -29, current_date-1)
    )
, t2 as (
    select distinct customer_id id
    from sessions
    and created_date <= dateadd('day', -89, current_date-1)
    and created_date >= dateadd('day', -119, current_date-1)
    )
select current_date-1,
    count(t1.id) as original,
    count(t2.id) as current,
    round(cast(count(t2.id) as float) / cast(count(t1.id) as float), 2) as ratio
from t1
left join t2
on t1.id = t2.id

Obtain this table with a daily retention rate for all days so far in 2021

The column original is the user cohort of 30 day active users 90 days ago from the reference date.
The current column is the number of users from the cohort in the original column that are 30 day active users at the reference date.
Step 1 returns only the first row 2021-03-05 and step 2 gives me the other row.

date
original
current
ratio

2021-03-05
100
70
0.7

2021-03-04
100
60
0.6

This process obviously is very inefficient and I am trying to figure out whether there is faster, easier way to do it? The issue is I need to compare a distinct user cohort from 3 months ago and then see today how many of those users from the cohort are still active.
All hep will be greatly appreciated!
Comment: I don't follow the question.  It is all about "today's date", but then you are talking about historical dates as well.  And then there is this statement:  "who 90 from todays date who had been active at least once in the last 30 days".  90 days or 30 days?
Comment: Hi Gordon, at todays date (5 March 2021) I need to look 90 days into the past and find the number of users at that point (6 December 2020) how many were 30 day active users from that date. Lets say there are 100 at that date, I then want to know how many of that cohort are 30 day actives at todays date. Lets say there are 70, so 70%. I have a working query for this but I also need yesterday and the day. Basically the calculation needs to be repeated everyday for the last year.
Comment: Does that help?
Answer: If you want to get the number of users for 30 days today and 90 days ago for each date, the query is:
with t1 as (
  select
    s2.created_date,
    count(distinct customer_id id) as cnt30
  from sessions s1 inner join
    (select distinct created_date from sessions) s2
    on dateadd('day', -29, s2.created_date)<=s1.created_date
       and s1.created_date<=s2.created_date
  group by s2.created_date
)
select a1.current_date,
  a1.cnt30 as original,
  a2.cnt32 as current,
  round(cast(a2.cnt30) as float) / cast(count(a1.cnt30) as float), 2) as ratio
from t1 as a1 inner join t1 as a2
  on dateadd('day', -89, a1.created_date)=a2.created_date
order by 1

Using the subquery in the select-list, the query is:
with t1 as (
  select
    s2.created_date,
    (select count(distinct s1.customer_id) from sessions s1
     where dateadd('day', -29, s2.created_date)<=s1.created_date
       and s1.created_date<=s2.created_date) as cnt30
  from
    (select distinct created_date from sessions) s2
)
select a1.current_date,
  a1.cnt30 as original,
  a2.cnt32 as current,
  round(cast(a2.cnt30) as float) / cast(count(a1.cnt30) as float), 2) as ratio
from t1 as a1 inner join t1 as a2
  on dateadd('day', -89, a1.created_date)=a2.created_date
order by 1

First, use joins and subqueries to calculate the number of unique IDs for the last 30 days on each date.
Next, join the same tables and output the number of unique IDs on the current day and 90 days ago.
Note taht I've never used redshift, so I'll write this based on your query and common SQL syntax. I hope my answer helps you.

SQL Retention Rates


Query to use in backend to retrieve all contacts with recent sent date less than or equal to some date

Question: In my task I want a query to retrieve all contacts with recent sent date, on the condition that it is less than or equal to some fixed date.
Actually I have one table name XXX which contains email, name, and sentdate.
Each year from 2006 to 2012, the mail is sent to each email contact. For some contacts still the mail is sent for every year from 2006 to 2012. For some contacts the mail has not been sent after 2007. So some contacts have received mail more recently than others.
I need a query to retrieve all the contacts with last sent date less than or equal to some date (for example, "01-01-2010").
Answer: SELECT * FROM `xxx` WHERE `last_sent_date`<='some_date'

Comment: Thanks for your response. But i need backend query for retrieving all contacts which has last send email date of each contacts which is <='01-01-2010'.  For Eg: If Subha has received two mails with two different date as '12-09-2009' in one mail and '24-08-2007' in another mail.For her 12-09-2009 is the recent received email send date. Like this i need the email contacts of a person who had received last send email date.Like this i need to retrieve all contacts from one table from using this condition which is <=01-01-2010. Can you help me.....
Answer: Try below :
  SELECT * FROM xxx WHERE sent_date<='01-01-2010'

Comment: Thanks for your response.But i need backend query for retrieving all contacts which has last sent email date which is <='01-01-2010'.


A polynomial rep. for $\frac{1}{m!} \sum_{k=0}^n (-1)^{k+m} \frac{k^n}{k+1} \binom{m}{k}$

Question: Let the generalized Bernoulli polynomials be defined by their generating function
$$  \sum_{n=0}^\infty \frac{B_n^{(s)}(x)}{n!} t^n = \Big(\frac{t}{e^t-1}\Big)^s\ e^{xt} $$
The Stirling numbers of the second kind can be written in terms of the Norlund polynomials $(B_n^{(s)}(0))$  as
$$ S_2(n,m)=\frac{1}{m!} \sum_{k=0}^n (-1)^{k+m} k^n \binom{m}{k} = \binom{n}{m} B_{n-m}^{\ (-m)}(0)$$
How can it be proved that the sum in the title has a generalized Bernoulli polynomial representation
$$ \frac{1}{m!} \sum_{k=0}^n (-1)^{k+m}  \frac{k^n}{k+1} \binom{m}{k}
= \binom{n}{m+1} B_{n-(m+1)}^{(-(m+1))}(-1) + \frac{(-1)^{n+m}}{(m+1)!}  \quad ? $$
The proposed identity has been verified for many $(n,m),$ with n>m>0.  If you present a proof, is there a way to extend it to denominators other than $k+1$ within the summation on the left hand side?
Answer: We seek to show that with $n\gt m\gt 0$
$$\frac{1}{m!} \sum_{k=0}^n (-1)^{k+m} \frac{k^n}{k+1}
{m\choose k}
\\ = {n\choose m+1} (n-m-1)! [t^{n-m-1}]
\frac{\exp(-t)}{t^{m+1}} (\exp(t)-1)^{m+1}
+ \frac{(-1)^{n+m}}{(m+1)!}.$$
The LHS is
$$\frac{1}{(m+1)!} \sum_{k=0}^n (-1)^{k+m} k^n
{m+1\choose k+1}.$$
We get for the coefficient extractor on the RHS
$${n\choose m+1} (n-m-1)! [t^n]
\sum_{k=0}^{m+1} {m+1\choose k} (-1)^{m+1-k}
\exp((k-1)t)
\\ = \frac{1}{(m+1)!}
\sum_{k=0}^{m+1} {m+1\choose k} (-1)^{m+1-k} (k-1)^n
\\ = \frac{1}{(m+1)!}
\sum_{k=-1}^{m} {m+1\choose k+1} (-1)^{m-k} k^n.$$
Restoring the extra term on the RHS yields
$$\frac{1}{(m+1)!}
\sum_{k=-1}^{m} {m+1\choose k+1} (-1)^{m-k} k^n
+ \frac{(-1)^{n+m}}{(m+1)!}
\\ = \frac{1}{(m+1)!}
\sum_{k=0}^{m} {m+1\choose k+1} (-1)^{m-k} k^n.$$
The LHS is the same as the RHS except for the upper limit of the sum
being $n$ instead of $m.$ Note however that ${m+1\choose k+1} =
(m+1)^{\underline{k+1}}/(k+1)! = 0$ when $k\gt m$ so we may lower the
limit  to $m$ (here we have used $n\gt m.$) This concludes the argument.
Comment: Nice proof.  It appears the $k+1$ in the denominator is absorbed into a binomial by your method, so it is unlikely that a $k+2$ (or another shift) will work.


Passing variable in Group By in SQL Server

Question: How can I pass variable in Group BY expression.Here is my code.
and from @Varientsku I want to pass column name
Create Proc spIsUnique
@varientsku nvarchar(max) output
As
Begin
select @varientsku,
IIf (count(*)>1,'Dublicate','True') as Total from product group by
@varientsku
End.

and I get this error.
Each GROUP BY expression must contain at least one column that is not an outer reference.
Help me to solve this
Comment: There's a very long list of duplicates, I just added 3 but searching for `[sql-server] Parameterize identifiers` yields a very long list of questions and answers.
Answer: You need dynamic sql
      EXEC ('select '+@varientsku+',
                    IIf (count(*)>1,''Duplicate'',''True'') as Total
            from product
            group by '+@varientsku)

Note : Make sure @varientsku is sanitized, this code is vulnerable to sql injection
Comment: @TT. True, but that would depend on who is calling this proc.
Comment: SQL injection alert
Comment: @TT. True but there is not other way you can get this done
Comment: @TT. Hope its fine now ;)
Comment: Sure, if the length of the contents of @varientsku is 128 characters or less.
Comment: if there are multiple columns, it's possible to split them and check every column in schema
Comment: @TT. - If `@varientsku` isn't a column name, dynamic sql isn't going execute. Or what am i missing here
Comment: the only problem is expressions... like CASE WHEN ... END
Comment: @Anton - If you are sure that `@varientsku` is sanitized then you can remove the `if` condition and `quotename` and execute the sql directly
Comment: My opinion is that what is asked in the question is the wrong way to go about this. Perhaps if the "columns" or "expressions" were in a table variable, then this could be done somewhat sensibly. In any case, QUOTENAME takes as parameter values of type SYSNAME, which is equivalent to NVARCHAR(128). Expressions longer than that, would be clipped to 128 characters.
Comment: @Anton - From where `@varientsku` variable is generated. Edit : Lol I thought you are the OP
Comment: @TT. - Got it.. Quotename cannot be used, as per the previous comment from OP, even case expression can be passed..
Comment: Edited:- I want to passed column name from @varientsku


Launching mysql server: same permissions for root and for user

Question: have been directed here from stackoverflow here, am reposting the question and adding my.cnf at the end of a post.
so far in my 10+ years experience with linux, all the permission problems I've ever encountered, have been successfully solved with chmod -R 777 /path/where/the/problem/has/occured (every lie has a grain of truth in it :)
This time the trick doesn't work, so I'm turning to you for help.
I'm compiling mysql server from scratch with zc.buildout (www . buildout . org). I do launch it by executing /home/toinbis/.../parts/mysql/bin/mysqld_safe, this works. The thing is that i'll be launching this from within supervisor (supervisord . org) script, and when used on the deployment server, it'll need it to be launched with root permissions(so that nginx server, launched with the same script, would have access to 80 port). The problem is that sudo /home/toinbis/.../parts/mysql/bin/mysqld_safe, fails, generating the error, posted bellow, in mysql error log (apache and nginx works as expected).
http://lists.mysql.com/mysql/216045 suggests, that "there are two errors: A
missing table and a file system that mysqld doesn't have access to". Mysqldatadir and all the mysql server binary files has 777 permissions, talbe mysql.plugin does exist and has 777 permissions (why Can't open the mysql.plugin table?), "sudo touch mysql_datadir/tmp/file" does create file (why  Can't create/write to file /home/toinbis/.../runtime/mysql_datadir/tmp/ib4e9Huz?).
chgrp -R mysql mysql_datadir and  adding "root, toinbis, mysql" users to mysql group ( cat /etc/group | grep mysql  outputs mysql:x:124:root,toinbis,mysql)
 has no effect - when i launch it as a casual user, it starts, when as a root - it fails.
Does mysql server, even started as root, tries to operate as other, let's say, 'mysql' user? but even in that case, adding mysql user to mysql group and making all the mysql_datadirs files belong to mysql group should make things work smoothly.
I do know that it might be a better idea to simply to launch one the nginx as root and mysql - as just a user, but this error irritated me enough so to devote enough energy so not to only "make things work", but to also make things work exactly as i wanted it initially, so to have a proof of concept that it's possible.
and this is the generated error:
091213 20:02:55 mysqld_safe Starting mysqld daemon with databases from /home/toinbis/.../runtime/mysql_datadir
/home/toinbis/.../parts/mysql/libexec/mysqld: Table 'plugin' is read only
091213 20:02:55 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it.
/home/toinbis/.../parts/mysql/libexec/mysqld: Can't create/write to file '/home/toinbis/.../runtime/mysql_datadir/tmp/ib4e9Huz' (Errcode: 13)
091213 20:02:55  InnoDB: Error: unable to create temporary file; errno: 13
091213 20:02:55 [ERROR] Plugin 'InnoDB' init function returned error.
091213 20:02:55 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
091213 20:02:55 [ERROR] Can't start server : Bind on unix socket: Permission denied
091213 20:02:55 [ERROR] Do you already have another mysqld server running on socket: /home/toinbis/.../runtime/var/pids/mysql.sock ?
091213 20:02:55 [ERROR] Aborting

091213 20:02:55 [Note] /home/toinbis/.../parts/mysql/libexec/mysqld: Shutdown complete

091213 20:02:55 mysqld_safe mysqld from pid file /home/toinbis/.../runtime/var/pids/mysql.pid ended

My my.cnf (the basedir and datadir(including tempdir) have chmod -R 777 permissions) :
[client]
socket          = /home/toinbis/.../runtime/var/pids/mysql.sock
port            = 8002

[mysqld_safe]
socket          = /home/toinbis/.../runtime/var/pids/mysql.sock
nice            = 0

[mysqld]
#
# * Basic Settings
#

socket          = /home/toinbis/.../runtime/var/pids/mysql.sock
port            = 8002
pid-file        = /home/toinbis/.../runtime/var/pids/mysql.pid
basedir         = /home/toinbis/.../parts/mysql
datadir         = /home/toinbis/.../runtime/mysql_datadir
tmpdir          = /home/toinbis/.../runtime/mysql_datadir/tmp
skip-external-locking
bind-address            = 127.0.0.1
log-error =/home/toinbis/.../runtime/logs/mysql_errorlog

#
# * Fine Tuning
#
key_buffer              = 16M
max_allowed_packet      = 32M
thread_stack            = 128K
thread_cache_size       = 8
myisam-recover          = BACKUP
#max_connections        = 100
#table_cache            = 64
#thread_concurrency     = 10

#
# * Query Cache Configuration
#
query_cache_limit       = 1M
query_cache_size        = 16M

#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
#log            = /home/toinbis/.../runtime/logs/mysql_logs/mysql.log
#
# Error logging goes to syslog. This is a Debian improvement :)
#
# Here you can see queries with especially long duration
#log_slow_queries       = /home/toinbis/.../runtime/logs/mysql_logs/mysql-slow.log
#long_query_time = 2
#log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or for replication.
#server-id               = 1
#log_bin                 = /home/toinbis/.../runtime/mysql_datadir/mysql-bin.log
#binlog_format           = ROW
#read_only               = 0
#expire_logs_days        = 10
#max_binlog_size         = 100M
#sync_binlog             = 1
#binlog_do_db           = include_database_name
#binlog_ignore_db       = include_database_name

#
# * InnoDB
#
innodb_data_file_path = ibdata1:10M:autoextend
innodb_buffer_pool_size=64M
innodb_log_file_size=16M
innodb_log_buffer_size=8M
innodb_flush_log_at_trx_commit=1
innodb_file_per_table
innodb_locks_unsafe_for_binlog=1

[mysqldump]
quick
quote-names
max_allowed_packet      = 32M

[mysql]
#no-auto-rehash # faster start of mysql but no tab completion

[isamchk]
key_buffer              = 16M

Any ideas much appreciated!
regards,
to
P.S. sorry for messy hyperlinks, it's my first post and anti-spam feature of SF doesn't allow to post them properly :)
Answer: First off?  Don't use 777 for this.  Don't.  Daemons don't run as root for a reason (monstrous security problem if any of them have a buffer overflow -- if anybody manages to get into your website via SQL injection or other, they've just rooted your box).  Use a wrapper script to drop the privileges after you open the port.  Nginx doesn't need to be run as root either.
Secondly, your problem appears to be the paths.  Not permissions.  Unless you've substituted the actual path with /.../ everywhere in this post, that's your problem.  If you did, please update the question with the full path.
Comment: Phresus, cheers. Yes, i've changed the path, which is similar to "/home/toinbis/Desktop/programming/pamella-anderson-photo-galery-project-name/buildout/runtime/" with what you can see:) As the folder of the path contains my not-yet-realeased project's name, i've prefered not to unveil it. So i feel it's not the path problem(as it's all working flawlessly with a casual user).
Agree with your opinion, the 777 is just an extreme way of trying to trace the root of a problem... Will try to refresh my bash knowledge and write decent wrapper script... cheers once again!
Comment: Well, that clears that up.  It may be helpful to update the question with `ls -l /path/to/your/mysql/root` (make that `ls -lZ` if you have SElinux enabled).  If it's 777, SElinux seems like the obvious culprit (/var/log/audit/audit.log).
Answer: Silly question first. Are you sure there isn't another mysql running? Read-only errors despite sane permissions (and despite 777) are almost always a sign that files are locked by another process. You can use lsof to find out if something else is messing with your files:
$ lsof /home/toinbis/.../runtime/var/pids/mysql.sock

or
$ sudo lsof | grep mysqld

If you are still getting the error try the above and if their are any matches they are what is to blame.
Failing that, do a fresh build and never ever start as a non-root user. Does that make any difference?
If you want to cheat, create a wrapper script using buildout_script (http://pypi.python.org/pypi/buildout_script/0.2a1)
It takes a template Something like:
#! /bin/sh
sudo -u mysql_or_your_username_here {buildout[bin-directory]}/mysqld_safe $@

That way mysqld_safe never runs as root, and you avoid this error. I find this distasteful, but your entire setup is already something i'd never ever do so I think it would be in good company! :)
I believe Gentoo and a system-level config tool like puppet, chef-solo, or kokki would suit you better though.
Answer: Is your home directory mounted across NFS, mounted with config which is squashing root privileges down to "nobody"?  If so, start as another user, or don't store the data in NFS, or export the NFS share without root_squash.
Comment: Phill, thanks for replying! No, it's the same partition, all files in one subdirectory, on good not-so-old ext4 filesystem;)
Comment: Just read the Q more thoroughly: yes, mysqld_safe drops privileges to the mysql user (unless overriden with -o/--user); generally, running commands as root needlessly is dangerous, so the start-up wrapper script drops privileges.  This lets you have one startup script run as root for all the tools, but still drop privileges very easily.
Answer:
I'm compiling mysql server from scratch with zc.buildout (www . buildout . org).

It looks like you've got into a mess! :)
Is there a specific reason why you are compiling MySQL server from source? I found this MySQL "buildout" script, which may be related to what you're doing: It seems to download MySQL server and a package to give MySQL support to Python, then compile and install these in non standard locations.
In any case, MySQL specifically recommend against compiling from source, as they provide their own optimised binary packages for various different platforms.
If you are deploying to a nice package managed system, then your "buildout" script need only invoke the package manager of the target system. This will give you a vendor recommended and approved installation (no chmod -R 777 necessary!) in a couple of commands. For a RHEL/CentOS system, the procedure is roughly:
yum install mysql-server (MySQL-python?)
[edit /etc/my.cnf]
chkconfig mysqld on && service mysqld start
[lock down the server by adjusting the grants]

See how easy, repeatable that is :). Unless you have a very good reason, using your system's built in package management is  definitely preferable, compared to non-standard install locations and chmod -R 777 games.
Comment: Lockie, cheers for devoting your time! I compile it from source for couple reasons. First, feelings-based,I honestly feel power doing that. I guess that's hacker's way to do it, and i like it:) Second, pragmatical reasons based, i do want to have latest innodb plugin installed and try out tokudb plugin. Thirdly, i guess that's the real loosly-coupling, when you lamp stack is linux-distribution-independent.
I'll update my post with zc.buildout config i use;)
Comment: BTW, i was honestly thinking myself "this will get me into mess", but now i've almost made it all work, except the problem being discussed, it's a really nice LAMP setup ("custom" mysql, php, apache, nginx, supervisor, phpmyadmin, wordpress, django, djangosphinx..., all the system being built with one command. Whenever i finish setting it up, i'll share all the buildout.cfg...
Comment: No, it's not a very nice setup - since you solve all your permission issues by giving the world write access to any file or folder. I suggest that you use packages from your operating system, and then modify config files to make it work for you.
Comment: Using 777 permission is just a debugging tool, which says nothing about the beauty of the system. The discussion of what is more valuable - increased flexibility, which you get by having all the lamp stack "customly" compiled, or increased security, which you get by using OS packages, is worth the separate discussion, not to be performed in comments. The answer is probably "if you know what you are doing, go for customized setup, otherwise stick to OS packages". But i personally prefer "don't know something? learn it", not "don't know something? don't do that" attitude...


AngularJS how to get multiple values on drop-down menu change value

Question: I have a small angularjs function that gets the meeting id value of a drop down menu on its change event. The meeting name is used as the display value.
The values of the drop down come from an array that is populated on page load.
I now need to also get 2 more values from the array to later use in the code.
I am trying to add the variable assignment sort of following the same way that I did for meeting id, but I am getting nowhere, the values are not assigned.
This is the function:
$scope.GetValue = function (meeting) {

    $scope.meetingId = $scope.selectedMeeting;

    $scope.meetingName = $.grep($scope.MeetingList, function (meeting) {

        // These are the 2 new variables
        meeting.MeetingDate == $scope.meetingDate;
        meeting.MeetingTitle == $scope.meetingTitle;

        return meeting.MeetingID == $scope.meetingId;
    })[0].MeetingName;

    console.log("Selected MeetingID: " + $scope.meetingId + "\nSelected Meeting Date - Title: " + $scope.meetingName + + "\nSelected Meeting Title: " + $scope.meetingTitle + "\nSelected Meeting Date: " + $scope.meetingDate);    $scope.GetValue = function (meeting) {

    $scope.meetingId = $scope.selectedMeeting;

    $scope.meetingName = $.grep($scope.MeetingList, function (meeting) {

        // These are the 2 new variables
        meeting.MeetingDate == $scope.meetingDate;
        meeting.MeetingTitle == $scope.meetingTitle;

        return meeting.MeetingID == $scope.meetingId;
    })[0].MeetingName;

    console.log("Selected MeetingID: " + $scope.meetingId + "\nSelected Meeting Date - Title: " + $scope.meetingName + + "\nSelected Meeting Title: " + $scope.meetingTitle + "\nSelected Meeting Date: " + $scope.meetingDate);
};
};

This is the HTML, I am including it just to show how I get the values in the drop-down
                    Current Available Meetings
                    <select ng-model="selectedMeeting" ng-change="GetValue()" ng-disabled="form.$invalid || !email" >
                        <option ng-repeat="meeting in MeetingList" value="{{meeting.MeetingID}}">Meeting: {{meeting.MeetingName}}</option>
                        <option value="">--Select Meeting--</option>
                    </select>

COuld it be possible to get some help to make this addition correctly?
Thank you,
Erasmo.
Answer: I think there is an issue with the usage of $.grep in your code.
It can not modify the element parameter (here meeting), but it will filter and return the matched elements.
Also not sure if you aren't mis-spelling variable names (case-sensitive in JS !!) MeetingName, MeetingTitle,, MeetingDate and MeetingID. In your scope they all have initial lower-case.
You can use JavaScripts filter() method on your array to get the selected meeting.

angular.module('myApp', [])
 .controller('ExampleController', ['$scope', function($scope)  {
    $scope.meetings = [
    { id: 1, title: 'First', date: '2020-09-01' },
    { id: 2, title: 'Second', date: '2020-09-02' },
    { id: 3, title: 'Third', date: '2020-09-03' }
    ];

    $scope.meetingIdSelected = $scope.meetings[0].id;

    $scope.pickMeeting = function() {
      console.log("selected",$scope.meetingIdSelected);
      $scope.selectedMeeting = $scope.meetings.filter(function (m) {
        return m.id === $scope.meetingIdSelected;
      })[0];
      console.log("found",$scope.selectedMeeting);
        };
}]);

  <select ng-model='meetingIdSelected' ng-change="pickMeeting()" required ng-options='m.id as m.title for m in meetings'></select>

  selected:
  <tt>{{selectedMeeting}}</tt>


Radius of convergence of $x$

Question: What is the radius of convergence of
$f(x)=x$?
I have found the taylor series of x to be just = $1 + (x-1)$ , but then how can I use the ratio test to determine the radius? Or is there another way?
Comment: all Real numbers. Polynomials are analytic everywhere.
Answer: If the Taylor series terminates, so that the function is just a polynomial, there's no need to use an indirect test to find the radius of convergence. Clearly the series converges for all real numbers, because it is just a finite sum.

Radius of convergence of $x$


Definition of an indefinite integral

Question: I am learning integration right now and I was wondering if there is something like the definition of a derivative but for an integral. So, to find a derivative we can do the following:
$$f'(x) = \lim_{h \rightarrow 0} \frac{f(x+h) - f(x)}{h}$$
Is there something like that for an integral?
Thanks!
Comment: But is that not a definite integral? What's the difference?
Comment: So does that mean that when I am asked to find the indefinite integral i.e. $\int \cos \theta$, the question is really asking me to find a definite integral from $\int_0^x \cos \, \theta$?
Comment: The formal definition of definite integral in your course will involve a limit of Riemann sums. The formal definition of indefinite integral will involve finding the functions whose derivative is $\dots$. **Very** different, but it turns out related through the Fundamental Theorem of Calculus.
Answer: You can write it slightly differently, but this is often taken as the defintion of the definite integral:
$$
\int_{a}^{b} f(x) \; dx = \lim_{n\to \infty} \sum_{i=1}^{n}f(x_i)\Delta x
$$
Here $\Delta x = \frac{b-a}{n}$ and $x_i = a + i\Delta x$.
To understand this definition better, you could take a look at this Wikipedia article.
There is a relation between the indefinite integral and the definite integral. The Fundamental Theorem of Calculus says that (assuming that $f$ is integrable)
$$
\int_a^b f(t)\; dt = F(b) - F(a)
$$
where $F$ is any antiderivative (i.e. $ F'(x) = f(x)$). Now given constant $a$ that would mean that
$$
F(x) = \int_a^x f(t)\; dt + F(a).
$$

Definition of an indefinite integral


Condição no MySql para valor especifico de unidade de medida

Question: Tenho uma view no BD onde preciso trazer um quantidade total de KG/LT de produtos esperados para recebimento na empresa, preciso incluir uma condição que quando a unidade for TON ou TN ele multiplique por 1000, pois quando é tonelada, ele calcula como se fosse unitário normal, essas informações são retiradas de XML, é possível?
Segue a consulta atual:
SELECT SUM(i_prod_quant) as QUANT, pha_table.pha_emp as EMP FROM pha_table
            INNER JOIN phs_table ON pha_table.pha_parceiro_cnpj = phs_table.phs_parceiro_cnpj
            INNER JOIN nfe_table ON nfe_table.nfe_chave = phs_table.phs_nfe_chave
            INNER JOIN i_nfe_table ON i_nfe_table.i_nfe_chave = nfe_table.nfe_chave GROUP BY EMP

Comment: Ulisses, pelo que entendi o campo "i_prod_quat" possui a informação referente à quantidade do produto, mas haveria um campo que informa qual é a unidade relacionada à quantidade do produto? Se sim, qual seria esse campo? Você pode incluir na sua pergunta um esquema da sua pha_table?
Answer: No seu caso basta fazer uso de uma condição com o IF no seu select que dará certo, o IF deverá ver a unidade de medida para fazer a multiplicação.
SELECT SUM(IF(UNIDADE='TON', i_prod_quant*1000, i_prod_quant)) as QUANT, pha_table.pha_emp as EMP FROM pha_table
            INNER JOIN phs_table ON pha_table.pha_parceiro_cnpj = phs_table.phs_parceiro_cnpj
            INNER JOIN nfe_table ON nfe_table.nfe_chave = phs_table.phs_nfe_chave
            INNER JOIN i_nfe_table ON i_nfe_table.i_nfe_chave = nfe_table.nfe_chave GROUP BY EMP

Você tmbém pode ter o mesmo efeito utizando o CASE
SELECT SUM(CASE WHEN UNIDADE='TON' THEN i_prod_quant*1000 ELSE i_prod_quant END) as QUANT, pha_table.pha_emp as EMP FROM pha_table
            INNER JOIN phs_table ON pha_table.pha_parceiro_cnpj = phs_table.phs_parceiro_cnpj
            INNER JOIN nfe_table ON nfe_table.nfe_chave = phs_table.phs_nfe_chave
            INNER JOIN i_nfe_table ON i_nfe_table.i_nfe_chave = nfe_table.nfe_chave GROUP BY EMP

Comment: E se eu precisar com mais de uma opção, tentei colocar mas está dando erro!
Comment: Consegui! obrigado pela resposta!


How to use Lemma of Urysohn ??

Question: Let U, V be two non-empty, closed, disjoint subsets of a metric space X. How can I prove that there is a
continuous function $f : X → [0, 1] $ with $f|_U = 1 $and $f|_V = 0.$??I am not sure how to use  Urysohn's lemma in this. Or at least any hints, explanations, how to begin would be wonderful!
Comment: This answers your question: [Proving every metric space is normal](https://math.stackexchange.com/questions/3208752/proving-every-metric-space-is-normal). [Urysohn](https://en.wikipedia.org/wiki/Urysohn%27s_lemma) is overkill for metric spaces.
Comment: Why are you unsure about using Urysohn's lemma? This is almost exactly the conclusion of the Urysohn lemma
Comment: To be fair never used it so that is why I'm not sure how to do this
Comment: To elaborate, Urysohn is overkill for metric spaces because you're already given a real valued function you can manipulate - the metric itself! To say anymore would be to give the game away. You should add your thoughts anyhow
Answer: Let U, V be two non-empty, closed, disjoint subsets of a metric space $(X,d)$. You can show that all metric spaces are normal spaces. Define
\begin{equation*}
f: X \to \mathbb{R}, \
x \mapsto \frac{d(x,U)}{d(x,U) + d(x,V)}.
\end{equation*}
where $d(x,U)=$$inf${$d(x,u),u\in U$} and $d(x,U)=0\iff x\in U$,
$d(x,V)=$$inf${$d(x,v),v\in V$} and $d(x,V)=0\iff x\in V$
It is easy to prove that $f$ is continuous and conclude that every metric space is normal. Now Uryshon states that a topological space
X is normal if and only if, for any U,V two non-empty closed disjoint subsets
$\exists f:X\to[0,1]$ with$f(U)=0,f(V)=1$. And that is what you want to show. $\square$

How to use Lemma of Urysohn ??


Uniform distribution with random on iris

Question: This is the code I have to randomly select 30 data points uniformly. The part that confuses me is why we are checking if random.random() <= p ? Can anyone explain?
from sklearn import datasets
import random
iris = datasets.load_iris()
d = iris.data

# sample 30 points uniform randomly from 150 points dataset
n = 150
m = 30
p = m/n

lst = []
for i in range(0, n):
    if random.random() <= p:
        lst.append(d[i,:])

Comment: This is a maths problem, not a programming issue. It's setting a threshold of acceptance on whether or not you're going to pick a particular value. You have 150 samples, you want 30, as you traverse your samples you want a 30/150 probability of adding it to your set.
Comment: I'd be curious, according to the author of the code is the goal to select exactly 30 elements, or select approximately 30 elements? This will rarely give you exactly 30 (~8% of the time).
Answer: So p represents the probability of an element being selected.
As there are 150 total elements, and 30 elements need selecting, the probability of selecting one element is 30/150. This is set to p.
Each element is then iterated over and if the result of random.random() (between 0 and 1) is greater than p, than that element is selected (I assume this; I do not fully know your dataset).
On average, this should give about 30 elements.
Comment: Note that this is not adding to the *explanation* of the code, but I'd like to point out that this is not a reliable way for selecting `30` elements at all. On average, `30` elements will be selected, but on one specific run, `28` or `34` whatever may be chosen which may not be desired. A better method would be to use `random.randint` to select `30` random indexes between `0` and `150` and use these to select *exactly* 30 elements.
Comment: I was considering this too. I don't think there's anything inherently more uniform by the approach the OP is using?
Comment: I was going to say something similar to this. This is a great way to _not_ select exactly 30 elements. In my testing, running this code >100,000 times, you get exactly 20% of the elements ~8% of the time.
Comment: @G.Anderson Nice


Limitar número num input

Question: Gostaria de saber como limitar num input o número máximo digitado. Por exemplo: quero que o maior número que possa ser digitado seja 42, ou seja, se o usuário tentar digitar 43: ou apareceria apenas o quatro, ou, ao digitar o 4, exibiria o 2, pois o limite é 42, e o mínimo é 1.
O resultado não precisa ser necessariamente este que falei, só preciso que ela não consiga digitar o número 43 em diante. Se possível que ela não consiga nem usando CTRL + V kkk, mas qualquer progresso já me ajuda, segue o código que estou utilizando no momento com o mesmo exemplo, mas não está funcionando.
<input type="number" min="1" max="42">

Comment: Seu campo está em um formulário? Dentro de um .
Answer: Esse funcionalidade de digitar o 4 aparecer o 2 pode ser dispensável pois o usuário pode ter a intenção de digitar 41 e isso irá atrapalhar em vez de ajudar.
Mas você pode alterar o valor do campo para o mínimo caso seja digitado um valor menor que o min, ou para o valor máximo se tentar entrar um valor maior que o max.
Você pode usar o evento oninput e chamar uma função passando o elemento como parâmetro com this. O oninput detecta qualquer alteração no campo, mesmo colando.
Seria interessante também já iniciar o campo com o valor mínimo colocando o atributo value="1":

function checa(e){
   const mi = +e.min;
   const ma = +e.max;
   const va = e.value;
   if(va < mi){
      e.value = mi;
   }else if(va > ma){
      e.value = ma;
   }
}
<input oninput="checa(this)" value="1" type="number" min="1" max="43">
Comment: Obrigado Sam! Era dessa forma que eu queria! Eu já tinha encontrado uma função, mas como tava dentro de uma tabela estava tendo uns problemas, mas a sua funcionou exatamente à outra função, mas sem problemas. Abraço.
Answer: E se o valor máximo for 10, e eu precisar digitar, por exemplo: o número 6? não conseguirei.

function checa(e){
   const mi = +e.min;
   const ma = +e.max;
   const va = e.value;
   if(va < mi){
      e.value = mi;
   }else if(va > ma){
      e.value = ma;
   }
}
<input oninput="checa(this)" value="1" type="number" min="1" max="10">

Limitar número num input


defmulti não localiza defmethod em outr namespace

Question: Criei um projeto em clojure para validar a feature de defmulti com implementações de defmethod em outros namespace. No entanto ao executar o projeto o dispatcher do defmulti não localiza a implementação correta.
url do projeto no git

Implementação

(ns multi-metodos.executar)

(defmulti run :tipo)

(ns multi-metodos.sms
  (:require [multi-metodos.executar :as exe]))

(defmethod exe/run :default [_]
  (println "default"))

(ns multi-metodos.notificacao
  (:require [multi-metodos.executar :as exe]))

(defmethod exe/run :notificacao [_]
  (println "Enviando notificacao"))

(ns multi-metodos.email
  (:require [multi-metodos.executar :as exe]))

(defmethod exe/run :email [_]
  (println "Enviando email"))

(ns multi-metodos.core
  (:require [multi-metodos.executar :as exe]))

(defn -main []
  (println "Ola")
  (exe/run {:tipo :email}))

erro gerado:

Exception in thread "main" Syntax error compiling at (C:\Users\guilherme.angelico\AppData\Local\Temp\form-init7125751530945241762.clj:1:121).
    at clojure.lang.Compiler.load(Compiler.java:7647)
    at clojure.lang.Compiler.loadFile(Compiler.java:7573)
    at clojure.main$load_script.invokeStatic(main.clj:452)
    at clojure.main$init_opt.invokeStatic(main.clj:454)
    at clojure.main$init_opt.invoke(main.clj:454)
    at clojure.main$initialize.invokeStatic(main.clj:485)
    at clojure.main$null_opt.invokeStatic(main.clj:519)
    at clojure.main$null_opt.invoke(main.clj:516)
    at clojure.main$main.invokeStatic(main.clj:598)
    at clojure.main$main.doInvoke(main.clj:561)
    at clojure.lang.RestFn.applyTo(RestFn.java:137)
    at clojure.lang.Var.applyTo(Var.java:705)
    at clojure.main.main(main.java:37)
Caused by: java.lang.IllegalArgumentException: No method in multimethod 'run' for dispatch value: :email
    at clojure.lang.MultiFn.getFn(MultiFn.java:156)
    at clojure.lang.MultiFn.invoke(MultiFn.java:229)
    at multi_metodos.core$_main.invokeStatic(core.clj:6)
    at multi_metodos.core$_main.invoke(core.clj:4)
    at clojure.lang.Var.invoke(Var.java:380)
    at user$eval140.invokeStatic(form-init7125751530945241762.clj:1)
    at user$eval140.invoke(form-init7125751530945241762.clj:1)
    at clojure.lang.Compiler.eval(Compiler.java:7176)
    at clojure.lang.Compiler.eval(Compiler.java:7166)
    at clojure.lang.Compiler.load(Compiler.java:7635)
    ... 12 more

Answer: IIsso foi traduzido usando o Google Translate. Peço desculpas se está quebrado Português
Você precisa garantir que o multi-metodos.email é carregado em algum momento. O arquivo de email nunca é carregado, então (defmethod exe/run :email [_] . . .) nunca é executado.
Você precisará importar o arquivo de email para que isso funcione. Admito que não uso multi-métodos com muita frequência, então não sei a melhor maneira de configurar isso. No entanto, ele só pode ser remediado importando o arquivo de email:
(ns multi-metodos.core
  (:require [multi-metodos.executar :as exe]
            [multi-metodos.email]))  ; Verifique se está carregado

(defn -main []
  (println "Ola")
  (exe/run {:tipo :email}))


vba Word how to get sentences in a table

Question: I have a Word document, with the first two sentences as normal text and sentences three and four in a table (in one cell):
My first sentence. My second sentence.

My third sentence. My fourth sentence.

My code is the following:
Option Explicit
Sub test()
    Dim sentence As Variant
    Dim i As Long: i = 0

    Selection.Expand wdSentence
    Debug.Print "--------->" & ActiveDocument.ActiveWindow.Selection.Sentences.Count
    Debug.Print "selection: " & ActiveDocument.ActiveWindow.Selection
    For Each sentence In ActiveDocument.ActiveWindow.Selection.Sentences
        i = i + 1
        Debug.Print i & " sentence: " & sentence
    Next

End Sub

If I select the first two sentences, the debug output is correct:
--------->2
selection: My first sentence. My second sentence.

1 sentence: My first sentence.
2 sentence: My second sentence.

If I select the two sentences in the table, the debug output is strange (or wrong?):
--------->2
selection: My third sentence. My fourth sentence.

1 sentence: My third sentence.

Why is the output of the table content different from normal text? How can I get the same result for the table content as for the normal text?
Comment: The fundamental problem is that VBA has no idea what a grammatical sentence is. For example, consider the following: "Mr. Smith spent $1,234.56 at Dr. John's Grocery Store, to buy: 10.25kg of potatoes; 10kg of avocados; and 15.1kg of Mrs. Green's Mt. Pleasant macadamia nuts." For you and me, that would count as one sentence; for VBA it counts as 5 sentences.
Comment: Absolutely correct! That's why I corrected that in my macro.
In this case, however, part of the selection is not displayed. A piece of text is simply left out. It could be a VBA bug. I also checked it with n x m tables. VBA has problems with more than one sentence in a cell. In such case, Sentence.Information (wdWithInTable) indicates that the 2nd sentence is not contained in the table.
Answer: Tables bring a another whole dimension of complexity to what Word recognizes as a sentence. Paragraph, end of cell marks, and end of row markers all factor into the confusion of what constitutes a sentence to VBA.
Here is some code that should work, but I can't claim with 100% certainty that it will work in all situations. In other words, I know it can be improved upon, but it should give you a good start for your own debugging sessions.
Sub ParseBySentence()
    Dim doc As Word.Document
    Dim i As Long, s As Long, para As Word.Paragraph
    Dim rng As Word.Range, sRng As Word.Range

    Application.ScreenUpdating = False
    On Error Resume Next
    Set doc = ActiveDocument
    For i = 1 To doc.Paragraphs.Count
        Set para = doc.Paragraphs(i)
        If para.Range.Information(wdWithInTable) Then
            Set rng = para.Range
            Do While Asc(rng.Characters.Last) = 13
                rng.MoveEnd unit:=wdCharacter, Count:=-1
            Loop
            If rng.Text = vbNullString Or _
                Asc(rng.Text) = 13 Then
                'do nothing
            Else
                For s = 1 To rng.Sentences.Count
                    Set sRng = rng.Sentences(s)
                    Do While Asc(sRng.Characters.Last) = 13
                        sRng.MoveEnd unit:=wdCharacter, Count:=-1
                    Loop
                    sRng.Select
                    Debug.Print Selection.Text
                    Selection.Collapse Word.WdCollapseDirection.wdCollapseEnd
                Next
            End If
        End If
    Next
    Selection.HomeKey unit:=wdStory
    Application.ScreenUpdating = True
    MsgBox "Action Complete"
End Sub

Answer: @Rich Michaels: Thanks for the hint with the paragraphs. I made a fast and minimalistic macro that also works in tables:
Sub Par2Sen()
    Dim s0, s1, smax As Long
    Dim para As Word.Paragraph
    Dim r() As Byte

    For Each para In ActiveDocument.Paragraphs
        r = para.Range
        Debug.Print "> Paragraph= " & para.Range;
        smax = UBound(r) - 1
        s0 = 0
        s1 = 0
        Do Until s1 > smax
            If r(s1) = 46 Or s1 = smax Then
                Debug.Print "-> Sen from" & (s0 / 2) + 1 & " to " & (s1 / 2) + 1 & MidB$(r, s0 + 1, s1 - s0 + 3)
                s0 = s1 + 2
            End If
            s1 = s1 + 2
        Loop
    Next
End Sub


Batter delay of game

Question: Why is the batter allowed to wander in and out of the box? Doesn't this amount to delay of the game?
The pitcher has 12 seconds to throw the ball after he receives it from the catcher. Apparently he has to wait until the batter feels like getting ready to hit the ball. The umpire has control of this situation.  He can say "ball" if everybody takes too long.
Today I watched a batter walk 10 feet away from the box after swinging at a pitch. The catcher is supposed to throw the ball back to the pitcher as soon as possible. There is a baseball rule book. What good is that?
Answer: A batter is not permitted to wander in and out of the box without requesting the umpire to call "Time" (rule 6.02(b)). A comment on the rule goes on to urge umpires not to grant excessive requests, since the batter is there only to bat.
If the batter leaves the box without requesting time, rule 6.02(c) allows the umpire to call a strike. Ordinarily umpires are hesitant to do so without warning the batter first. If the batter still does not enter the box, the umpire can continue the procedure until the batter has struck out.
This is not just theoretical; in 2013 the minor-league player Vinnie Catricala took the first pitch for a strike, stepped out of the box to argue the call, and wound up striking out without getting another pitch.
Comment: Is the batter still in the box if after each pitch he turns his back on the plate and plays with his gloves and checks to see if his shirt is buttoned and adjusts his hat  before he gets set to receive a pitch?
Comment: @charlescobb: That would fall into the umpire's discretion. I suspect most umpires don't want to warn every single batter, just like NBA referees always let a certain amount of contact go before calling a foul.
Comment: @charlescobb *Unwritten* rule when the batter does this they a reasonable amount of time to do so.
Answer: 6.02 b) begins by saying while the pitcher is in the set position or in the windup.  If this is not the case and there is a delay in the action, ie between pitches the batter may step out (in the comments part of 6.02 b).  Also the batter is allowed to take time to receive signs from the coach.  If the umpire chooses to ask the player to step in the box, then 6.02 c) may be applied.
The MLB differs from most other levels of play on this rule. In the MLB there is little to no concern over pace of play. It is rare to see an umpire warn a batter for taking his time between pitches.
One thing to consider about baseball is most calls are judgement calls.  The rule book is vague.  Things from the strike zone to what constitutes a delay are at the umpires discretion.  The MLB ran into this earlier in 2014 with the "transfer" rule (2.0).
The are no penalties or fouls in baseball so it is often understood that umpires are seen as moderators instead of officials.  If teams play slow, they play slow and vice verse for fast paced teams.
The Yankees and Red Sox are notorious for playing slow, and when they meet up games may exceed 4 hours.
In lower levels there is much more emphasis on this rule. Often at the high school levels and below, hitters are asked to keep one foot in the box while receiving signs from the coach.  In college (NCAA) there is a time limit or pitch clock between innings and pitches.

Batter delay of game


Why is this 2d array pushing values when it shouldn't?

Question: EDITED: Thanks for pointing out my oblivious mistake with tempRndNumber being reset in the inner loop.
I'm still seeing "," characters show up in the array though.
I want to create a 2d array that only gets populated when a random number meets a particular criteria (rnd >= 7). But the following code populates the 2d array with a combination of "," and the numbers that meets the criteria.
var tempAllRndNumbers = [];
    for (var i = 0; i < 10; i++) {
        for (var j = 0; j < 10; j++) {
            var tempRndNumber = [];
            var rndNumber = Math.floor(Math.random() * 10);
            if (rndNumber >= 7) {
                tempRndNumber.push(rndNumber);
            }
        }
        tempAllRndNumbers.push(tempRndNumber);
    }

tempAllRndNumbers should only be populated with numbers 7 and above, right? But instead, I'm getting a 2D array full of "," and numbers 7 and above.
Comment: You reset the `tempRndNumber` array every time in the inner loop.
Comment: @Juhana That's intentional, note there are two array variables.
Comment: What do you mean you're seeing "," characters? How are you outputting the array?
Comment: *"I want to create a 2d array"* (...) *"should only be populated with numbers 7 and above, right? But instead, I'm getting a 2D array"*.. the post seems a little contraditory...
Comment: @Nit I don't think so. Then the inner loop wouldn't make any sense because only the last iteration has any effect.
Comment: I updated my answer with a fiddle to show that there are no commas in the output
Comment: @jessikwa:  console.log(tempAllRndNumbers)
Comment: that's your problem. Look at the fiddle in my answer.
Answer: I don't think you want to reset tempRndNumber in each iteration of your inner loop like you are. Try this:
                    var tempAllRndNumbers = [];
                    for (var i = 0; i < 10; i++) {
                        var tempRndNumber = [];
                        for (var j = 0; j < 10; j++) {
                            var rndNumber = Math.floor(Math.random() * 10);
                            if (rndNumber >= 7) {
                                tempRndNumber.push(rndNumber);
                            }
                        }
                        tempAllRndNumbers.push(tempRndNumber);
                    }

UPDATE:
You have to loop back through both arrays to get the proper output, see here:
http://jsfiddle.net/jessikwa/1fbq0woo/
Comment: No problem, friend. Don't forget to select an answer if your issue is solved :)
Answer: Since you reset tempRndNumber to an empty array on each iteration of the j loop, it will only contain a number if the last iteration was >= 7.  Move the initialization outside of the innermost loop:
var tempAllRndNumbers = [];
    for (var i = 0; i < 10; i++) {
        var tempRndNumber = [];
        for (var j = 0; j < 10; {
            var rndNumber = Math.floor(Math.random() * 10);
            if (rndNumber >= 7) {
                tempRndNumber.push(rndNumber);
            }
        }
        tempAllRndNumbers.push(tempRndNumber);
    }

Answer: You have no check to see if the tempRndNumber array has any values, so you are pushing an empty array into the array tempAllRndNumbers. That's why you have the ','s, you have indexes with no values.
Comment: No, it only proves that if there is a value that is 7 or greater it will be pushed, if the loop generates no values of 7+, tempRndNumber will be empty when you push it to tempAllRndNumbers, so if it was on the first loop for example, tempAllRndNumbers[0] === [ ]
Comment: But doesn't the if (rndNumber >=7) prove that there is a value to be pushed?
Comment: While this wasn't the main issue it is a good point. When pushing to `tempAllRndNumbers` I'd first do a check that says `if (tempRndNumber && tempRndNumber.length) { tempAllRndNumbers.push(tempRndNumber); }`


ssh remote command using password-less sudo fails

Question: Logged on as bryan@localserver, I'm trying to perform a backup using rsync on a remote host bryan@remoteserver.
I need the rsync connection to elevate privileges on the remote host using --rsync-path='sudo rsync', but I'm having problems, so have decided to simplify matters and just try to get the following working first:
bryan@localserver # ssh bryan@remoteserver 'sudo /usr/bin/whoami'

Once, I've cracked this, I should be able to get rsync working.

What I've done so far:
I've got ssh configured so that bryan@localserver can ssh to bryan@remoteserver using key instead of a password.
bryan@localserver # ssh bryan@remoteserver
bryan@remoteserver #

I've also configured bryan@remoteserver to be able to run /usr/bin/whoami as sudo, without a password in the sudoers file on remoteserver:
bryan   ALL=(ALL) NOPASSWD: /usr/bin/whoami

This works fine:
bryan@remotehost # sudo whoami
root

But from the local server:
bryan@localhost # ssh bryan@remoteserver 'sudo /usr/bin/whoami'
sudo: no tty present and no askpass program specified
Sorry, try again.
sudo: no tty present and no askpass program specified
Sorry, try again.
sudo: no tty present and no askpass program specified
Sorry, try again.
sudo: 3 incorrect password attempts

My complete sudoers file (minus the comments and blank lines) is below:
Defaults env_reset
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
Defaults !requiretty
root    ALL=(ALL:ALL) ALL
bryan   ALL=(ALL) NOPASSWD: /usr/bin/whoami
%admin  ALL=(ALL) ALL
%sudo   ALL=(ALL:ALL) ALL

ssh -t from localserver solves the 'no tty present' error, but still prompts me for bryan@remotehost's password.
Can anyone please explain what the problem is?
localserver is Ubuntu 10.04 x64, remoteserver is Ubuntu 12.04 x64.
Answer: Are you a member of the sudo group or admin group?  From the sudoers man page:

When multiple entries match for a user, they are applied in order.
  Where
         there are multiple matches, the last match is used (which is not
         necessarily the most specific match).

You can move your bryan   ALL=(ALL) NOPASSWD: /usr/bin/whoami line to the bottom so it is the last match.  This should keep you from being prompted for a password.


How do I get the new index of a moved column in a wxGrid?

Question: I'm using wxWidgets 3.1.0 and I'm developing a Windows app in C++.
I'm using the base wxGrid and I've enabled column re-ordering via dragging them with the mouse (EnableDragColMove(true)). My problem now is that I need to get the moved column's new position/index after the column has been dragged to its new position.
Unfortunately, I could not find a way to do that from the available APIs.
I've tried catching the wxGridEvent wxEVT_GRID_COL_MOVE then using GetCol() and GetColPos() to check the column's new index:
gridDataList->Bind(wxEVT_GRID_COL_MOVE, &FormData::OnList_ColumnMove, this);

...

void FormData::OnList_ColumnMove(wxGridEvent& event)
{
    int movedCol = event.GetCol();
    int movedColPos = gridDataList->GetColPos(movedCol );

    ...
}

But it seems the event is triggered BEFORE the column is actually moved, so GetColPos() will still return the current column index, NOT the new index.
There seems to be no event to catch AFTER the column is moved.
My current solutions/workarounds are to:

Manually handle the column movement after catching the wxEVT_GRID_COL_MOVE event (as suggested in the wxWidgets docs) so that I can properly track the before and after index of the moved column.
Manually trigger a callback or a timer event after the column moves to its new position, similar to a wxPython workaround suggested in another SO post.

Though, I would like to know if there is a cleaner, less complicated way without resorting to the workarounds above.
Any suggestions are appreciated.
Comment: How about `EVT_HEADER_BEGIN_REORDER(id, func)` and `EVT_HEADER_END_REORDER(id, func)`
Comment: @macroland I tried them but the [wxHeaderCtrl](http://docs.wxwidgets.org/3.1.0/classwx_header_ctrl.html) events are not fired. I tried binding+catching them in my wxGrid code (similar to the wxEVT_GRID_COL_MOVE) and also inside a custom wxGrid implementation.
Answer: Yes, this wxEVT_GRID_COL_MOVE is generated before moving the column because it can be vetoed, preventing the move from happening. And it's true that it would be convenient if it carried the new column position, but unfortunately currently it doesn't (it would be simple to fix this and any patches doing this would be welcome!).
The standard workaround of using CallAfter() to execute your code at later time should work just fine without changing wxWidgets however. I.e., assuming you use C++11, you should be able to just write
void FormData::OnList_ColumnMove(wxGridEvent& event)
{
     const int movedCol = event.GetCol();
     CallAfter([movedCol]() {
          int movedColPos = gridDataList->GetColPos(movedCol);
          ...
     });
}

Comment: This will capture by copy the whole `wxGridEvent` object; is that wise? It's not that small a class...
Comment: Personally I think clarity is more important, especially in a quick example, but you can, of course, extract just the column from the `event` object and capture just it if you prefer.
Comment: This seems to be the best workaround for now. A minor edit to the answer is to move `event.GetCol()` outside of `CallAfter` to fix the compile error. Otherwise, good. Thanks.
Comment: Applied, also addressing the previous comment, thanks.


$\lim\limits_{x\to 0} \cos\left(\frac{1}{\sqrt[3]{x}}\right)$

Question: I can't seem to find a way to solve $$\lim\limits_{x\to 0} \cos\left(\frac{1}{\sqrt[3]{x}}\right)$$
 Is there really any way to solve it, or does it just not exist?
Answer: This limit does not exist.
You can simply take the particular subsequence:
\begin{equation}
x_n = \frac{1}{(2\pi n)^3}
\end{equation}
and
\begin{equation}
x_n = \frac{1}{(\pi n)^3},  n\ \text{odd}
\end{equation}
so that they do not converge to the same value.


Estou a tentar mover o meu texto para o canto inferior direito mas não consigo

Question: O meu  código:

Lucas Sintra

Comment: Lucas coloque o código completo do seu HTML e do CSS que está usando também!
Comment: Tenta: `Lucas Sintra`
Comment: Não é recomendado usar a tag ``, está obsoleta e pode ser excluída a qualquer momento dos navegadores. **[MDN](https://developer.mozilla.org/pt-BR/docs/Web/HTML/Element/font)**
Answer: Na sua
 adicione o stributo style e a propriedade text-align: right;
Deixei o fundo da
 como cinza só para permitir a visualização na resposta.

  Lucas Sintra

Answer: Fiz esse modelinho bem simples só para te ajudar a entender o código, se tiver alguma dúvida e só comentar que te ajudo. Clique em Executar para ver funcionando!

html, body {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    }
.texto {
    position: absolute;
    right: 0;
    bottom: 0;
    margin: 0 1rem 1rem 0;
}
.texto p {
    line-height: 0;
    color: red;
    font-size: 2rem;
    font-weight: bold;
    font-family: Arial, Helvetica, sans-serif;
}

Lucas Sintra


Why is $(X,Y)$ not a projective $\mathbb{Z}[X,Y]$-module?

Question: This was an exam problem I had which stumped me. The question was to prove that the ideal generated by $X$ and $Y$ in $\mathbb{Z}[X,Y]$ is not a projective $\mathbb{Z}[X,Y]$-module.
I was trying to exhibit an exact sequence with fourth term $(X,Y)$  which did not split, but hit a dead end.
Comment: With some heavy machinery, you can prove that the only projective/invertible ideals in $\mathbb{Z}[x,y]$ are principal ideals. See this post http://math.stackexchange.com/questions/76957/modules-over-kx-y
Comment: @PrahladVaidyanathan, that post is about $k[x,y]$ with $k$ a field.
Comment: @MarianoSuárez-Alvarez : Read the "Edit" portion of the answer. It refers to UFDs in general.
Answer: Let $R=\mathbb Z[X,Y]$ and $I=(X,Y)$. There is a short exact sequence of left $R$-modules $$0\to R\xrightarrow{f} R\oplus R\xrightarrow{g}I\to 0$$ with $f(a)=(Ya,-Xa)$ and $g(a,b)=aX+bY$ for all $a$, $b\in R$.
Check that it doesn't split.
Comment: Thanks, I tried this: Let $s\colon R\oplus R\to R$ be such that $sf=1_R$. Suppose $s(1,0)=p$ and $s(0,1)=q$ for some polynomials $p$ and $q$. Then $s(Y,0)=pY$ and $s(0,-X)=-qX$ since $s$ is a module homomorphism. Then $s(Y,-X)=pY-qX=sf(1)=1$. But this is impossible since $pY-qX$ has zero constant term. Is this correct?
Answer: Let's write $R=\mathbf Z[X,Y]$, $I=(X,Y)$. Two strategies:
First strategy: Let $M$ be the free $R$-module of rank $2$, with generators $u$ and $v$. Consider the exact sequence
$$0 \to K \to M \to I \to 0 $$
where $u \mapsto X$, $v \mapsto Y$, and $K$ is the kernel. Prove that $K$ is free of rank $1$, generated by $Yu-Xv$. Then prove that the sequence is not split on the left. (In a splitting $M\to K$, where could $u$ and $v$ go? Look at degrees of polynomials...)
Second strategy: Prove that $I$ is not flat. Can you find an ideal $J\subseteq R$ such that the natural map $J\otimes_R I \to I$ is not injective?
Comment: Thanks Bruno, is this idea like this: The generated module by $Yu-Xv$ is in the kernel. On the other hand, suppose $pu+qv$ is in the kernel. Then $pX+qY=0$. Then $pX=-qY$, so I think I can write $p=p'Y$ and $q=q'X$ for polynomials $p'$ and $q'$. Then $p'YX=-q'XY$, so $p'=-q'$. But then $pu+qv=p'Yu+q'Xv=p'Yu-p'Xv=p'(Yu-Xv)$? Then I think the rest follow by my comment on Mariano's answer.
Answer: It is not flat because $I\otimes I\to I$ not injective because $X\otimes Y-Y\otimes X\neq0$ but $XY-YX=0$. For $X\otimes Y-X\otimes Y\neq0$ I hope the argument here works well for $\mathbb{Z}$ instead of $k$.


Cluster Boostrap with Unequally Sized Clusters

Question: I need to perform a bootstrap for variance estimation on a GEE model for clustered data that I am analyzing.  I understand that I need to use a clustered bootstrap for this, which is pretty much the same thing as the usual nonparametric bootstrap, only the clusters are sampled rather than the individual observations within a cluster.  I've read a few articles on this and the articles always assume clusters of equal size.  Can the clustered bootstrap be modified for clusters of unequal size?  If so, how is this done?  In the usual bootstrap, resampling with replacement is performed until the dataset is the same size of the original dataset.  How do we perform a cluster bootstrap with unequal sized clusters such that the $i$th bootstrap sample is the same size as the original dataset?  I could imagine a situation where the cluster sizes are different such that it might be impossible to obtain datasets of the same size as the original dataset if we sample clusters, stead of elements.
Lastly is there a procedure in SAS or R that will perform the cluster bootstrap?
Answer: This is explained quite nicely in Sherman and leCessie's paper, "A comparison between bootstrap methods and generalized estimating equations for correlated outcomes in generlized linear models."  On page 905, they note:
"If as often may be the case, there are blocks of different sizes, then the algorithm can be modified as follows: let $m_i$, denote the number of blocks of size $i$, $i = 1,. . . ,I$. For each $i$ resample $m_i$ times  with  replacement from the $m_i$ blocks and compute $\hat{\beta}^*$ from the $n = \sum_{i=1}^Iim_i$ resampled  observations. This conditioning on block size guarantees  a total resarnple size
equal to the original sample size,  making the bootstrap replicates  "comparable".  If,
however, $I$ is  large it may be more attractive to resample $m$ times from the entire set of blocks.
We will call this the "All  Block" bootstrap. This algorithm gives a random total sample size, $n^*$, say,
which makes the replicates less comparable. A reasonable approach to make them more comparable is  to let the replicate be $(n^*/n)^{1/2}\hat{\beta}^*$, as suggested by Efron and Tibshirani (1993, p.101) for a whole block bootstrap in the time series setting."
REFERENCES:
Michael Sherman & Saskia le Cessie (1997) A comparison between
bootstrap methods and generalized estimating equations for correlated outcomes in
generalized linear models, Communications in Statistics - Simulation and Computation, 26:3,
901-925, DOI: 10.1080/03610919708813417
Comment: Do you know if this has been implemented in R?
Comment: I'm sure there must be one in R, but I'm not familiar with them. That being said, it should be quite simple to implement your own routine with a few lines of code.  You might also consider reaching out to the R user group and asking them which packages might contain blocked bootstrap functions.
Answer: I wrote something in R for my own use, based on the quote from Sherman and Cessie (1997) in StatsStudent's answer.
It implements bootstrap replicates on clustered data with clusters of different sizes.
It makes sure that clusters sampled more than once (due to replacement) are treated as distinct clusters within bootstrap samples (especially important in estimations involving fixed-effects along the cluster dimension).
Finally, note that this approach allows to compute the one-way cluster bootstrap standard error of any statistic computed via a custom, user-provided function. In this respect, it is more flexible than sandwich::vcovBS or sandwich::vcovCL which accept only some fitted model objects as input.
EDIT (improvement)
The Sherman and Cessie (1997) approach that draws clusters from pools of same-size clusters is incorrect I think. More precisely, the same-size-cluster pools are systematically smaller than the full pool of clusters. Therefore, this approach produces standard errors biased towards zero. The most extreme case being when every cluster is of different size (e.g. clustering on year level and no cross-section has the same size). In this case, the same cluster gets sampled for every size, all bootstrap replicates are thus identical and the standard error is exactly zero. I leave my old code implementing this approach at the end of my answer.
The better solution is to draw in the full pool of clusters such that the replicate data set has, on average, the same size as the original data set.
library(boot)
library(sandwich)

data("PetersenCL", package = "sandwich")

# construct an unbalanced panel with different numbers of firms every year.
datalist <- list()
for(yr in 1:7){
  datalist[[yr]] <- subset(PetersenCL, (firm %in% c(1:(2*yr)) & year == yr))
}
data <- bind_rows(datalist)

cluster_var <- "year"

cluster_names = unique(data[,cluster_var])

# get the different cluster sizeS. This is necessary to cluster bootstrapping with clusters of different sizes.
sizes <- table(data[,cluster_var])

# don't bother the size of every cluster. Use the average cluster size only.
avg_cl_size <- mean(sizes)
# sample size of original data
N <- nrow(data)
# Number of draws, for the bootstrap replicate data sets to be as large as original data, on average.
n_draws <- N/avg_cl_size

ran.gen_cluster <- function(original_data, arg_list){

  cl_boot_dat <- NULL

  # for later: non-unique names of clusters (repeated when there is more than one obs. in a cluster)
  nu_cl_names <- as.character(original_data[,cluster_var])

  # sample, in the vector of names of clusters, N/avg_cl_size
  sample_cl <- sample(x = cluster_names,
                      size = n_draws,
                      replace = TRUE)

  # because of replacement, some names are sampled more than once
  # we need to give them a new cluster identifier, otherwise a cluster sampled more than once
  # will be "incorrectly treated as one large cluster rather than two distinct clusters" (by the fixed effects) (Cameron and Miller, 2015)
  sample_cl_tab <- table(sample_cl)

  for(n in 1:max(sample_cl_tab)){ # from 1 to the max number of times a name was sampled bc of replacement
    # vector to select obs. that are within the sampled clusters.
    names_n <- names(sample_cl_tab[sample_cl_tab == n])
    sel <- nu_cl_names %in% names_n

    # select data accordingly to the cluster sampling (duplicating n times observations from clusters sampled n times)
    clda <- original_data[sel,][rep(seq_len(sum(sel)), n), ]

    #identify row names without periods, and add ".0"
    row.names(clda)[grep("\\.", row.names(clda), invert = TRUE)] <- paste0(grep("\\.", row.names(clda), invert = TRUE, value = TRUE),".0")

    # add the suffix due to the repetition after the existing cluster identifier.
    clda[,cluster_var] <- paste0(clda[,cluster_var], sub(".*\\.","_",row.names(clda)))

    # stack the bootstrap samples iteratively
    cl_boot_dat <- rbind(cl_boot_dat, clda)
  }
  return(cl_boot_dat)
}

#the returned data ARE NOT the same dimension as input data. It only has the same dimension on average over bootstrap replicates.
test_boot_d <- ran.gen_cluster(original_data = data)
dim(test_boot_d)
dim(data)

# test new clusters are not duplicated (correct if anyDuplicated returns 0)
base::anyDuplicated(test_boot_d[,c("firm","year")])

# custom estimation function
est_fun <- function(est_data){

  est <- lm(as.formula("y ~ x"), est_data)

  # statistics we want to evaluate the variance of:
  return(est$coefficients)
}

# Run the bootstrap (see ?boot::boot for more details on the arguments)
set.seed(1234)
boot(data = data,
     statistic = est_fun,
     ran.gen = ran.gen_cluster,
     mle = list(), # this argument cannot be left empty
     sim = "parametric",
     parallel = "no",
     R = 400)

Test that it computes the same standard error as sandwich::vcovBS and compare with the asymptotic solution of sandwich::vcovCL
set.seed(1234)
sdw_bs <- vcovBS(lm(as.formula("y ~ x"), data), cluster = ~year, R=400, type = "xy")#
sqrt(sdw_bs["x","x"])

sdw_cl <- vcovCL(lm(as.formula("y ~ x"), data), cluster = ~year)
sqrt(sdw_cl["x","x"])

OLDER, FLAWED APPROACH
library(boot)
library(sandwich)

## Make some necessary objects
# unbalanced panel data
data("PetersenCL", package = "sandwich")
data <- subset(PetersenCL, !(firm %in% c(1:10) & year == 10))

cluster_var <- "firm"

# get the different cluster sizeS. This is necessary to cluster bootstrapping with clusters of different sizes.
sizes <- table(data[,cluster_var])
u_sizes <- sort(unique(sizes))

# names and numbers of clusters of every sizes
cl_names <- list()
n_clusters <- list()
for(s in u_sizes){
  cl_names[[s]] <- names(sizes[sizes == s])
  n_clusters[[s]] <- length(cl_names[[s]])
}

par_list <- list(unique_sizes = u_sizes,
                 cluster_names = cl_names,
                 number_clusters = n_clusters)

## Design the bootstrap sampling function.
# It will tell boot::boot how to sample data at each replicate
    ran.gen_cluster_bysize <- function(original_data, arg_list){

  cl_boot_dat <- NULL

  # non-unique names of clusters (repeated when there is more than one obs. in a cluster)
  nu_cl_names <- as.character(original_data[,cluster_var])

  for(s in arg_list[["unique_sizes"]]){
    # sample, in the vector of names of clusters of size s, as many draws as there are clusters of that size, with replacement
    sample_cl_s <- sample(arg_list[["cluster_names"]][[s]],
                          arg_list[["number_clusters"]][[s]],
                          replace = TRUE)

    # because of replacement, some names are sampled more than once
    sample_cl_s_tab <- table(sample_cl_s)

    # we need to give them a new cluster identifier, otherwise a cluster sampled more than once
    # will be "incorrectly treated as one large cluster rather than two distinct clusters" (by the fixed effects) (Cameron 2015)

    for(n in 1:max(sample_cl_s_tab)){ # from 1 to the max number of times a name was sampled bc of replacement
      # vector to select obs. that are within the sampled clusters.
      names_n <- names(sample_cl_s_tab[sample_cl_s_tab == n])
      sel <- nu_cl_names %in% names_n

      # select data accordingly to the cluster sampling (duplicating n times observations from clusters sampled n times)
      clda <- original_data[sel,][rep(seq_len(sum(sel)), n), ]

      #identify row names without periods, and add ".0"
      row.names(clda)[grep("\\.", row.names(clda), invert = TRUE)] <- paste0(grep("\\.", row.names(clda), invert = TRUE, value = TRUE),".0")

      # add the suffix due to the repetition after the existing cluster identifier.
      clda[,cluster_var] <- paste0(clda[,cluster_var], sub(".*\\.","_",row.names(clda)))

      # stack the bootstrap samples iteratively
      cl_boot_dat <- rbind(cl_boot_dat, clda)
    }
  }
  return(cl_boot_dat)
}

#test that the returned data are the same dimension as input
test_boot_d <- ran.gen_cluster_bysize(original_data = data,
                               arg_list = par_list)
dim(test_boot_d)
dim(data)
# test new clusters are not duplicated (correct if anyDuplicated returns 0)
base::anyDuplicated(test_boot_d[,c("firm","year")])

## Custom ("black-box") estimation function
est_fun <- function(est_data){

  est <- lm(as.formula("y ~ x"), est_data)

  # statistics we want to evaluate the variance of:
  return(est$coefficients[2])
}

# see ?boot::boot for more details on these arguments
boot(data = data,
      statistic = est_fun,
      ran.gen = ran.gen_cluster_bysize,
      mle = par_list,
      sim = "parametric",
      parallel = "no",
      R = 200)

With this clustering structure, the result is close to the asymptotic solution (available for unbalanced data, but only for lm or glm objects):
sdw_cl <- vcovCL(lm(as.formula("y ~ x"), data), cluster = ~firm)
sqrt(sdw_cl["x","x"])

But it does not replicate the result from vcovBS, even for balanced data:
## compare with sandwich::vcovBS, on balanced panel data
set.seed(1234)
custom_bs <- boot(data = PetersenCL,
                  statistic = est_fun,
                  ran.gen = ran.gen_cluster_bysize,
                  mle = par_list,
                  sim = "parametric",
                  parallel = "no",
                  R = 200)
sd(custom_bs$t[,2])

set.seed(1234)
sdw_bs <- vcovBS(lm(as.formula("y ~ x"), PetersenCL), cluster = ~firm, R=200)#
sqrt(sdw_bs["x","x"])

Comment: Great.  Have you seen this too?  https://search.r-project.org/CRAN/refmans/icenReg/html/ir_clustBoot.html
Comment: I did not know about it, thanks. I am not sure how it addresses the unbalanced data problem though. And like sandwich::vcovBS, it seems to be constraining regarding the input estimate.


Making a JTree leaf appear as an empty directory and not a file

Question: I'm learning my way around Java JTree's but I have this little problem I can't figure out.
Using a tutorial I found on Oracle's site, I've mimicked a tree structure they demonstrate. The problem is, I want the Folder "websites" to be an empty folder, but JTree is displaying it as though it is a file. How can I tell the JTree that "websites" is in fact an empty folder?
UPDATE
I've started working on a simple 'contact manager'. What I want to do basically it make the name look like folders (since I want to add info to each of them), but without adding stuff. i.e. Some might not have any info in them, but I would still like them to look like folders.

Code for the Browser class:
import java.awt.Component;
import java.io.File;
import java.util.Iterator;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

public class Browser extends JFrame implements javax.swing.tree.TreeModel {

    private JTree tree;
    ManagerOfContacts mngrOfContacts;

    public Browser() {

        //Generates the ManagerOfContacts and associated Contacts
        Driver driver = new Driver();
        mngrOfContacts = driver.getManagerOfContacts();
        //---------------------------\\

        DefaultMutableTreeNode contacts = new DefaultMutableTreeNode("Contacts");
        createNodes(contacts);
        tree = new JTree(contacts);

        JScrollPane treeView = new JScrollPane(tree);

        add(treeView);

        setSize(400,400);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    public void createNodes(DefaultMutableTreeNode top){
        DefaultMutableTreeNode contactName;

        Iterator<Contact> contactItr = mngrOfContacts.getIterator();
        while(contactItr.hasNext()){
            contactName = new DefaultMutableTreeNode(contactItr.next());
            top.add(contactName);
        }
    }

    public static void main(String[] args) {
        Browser browsr = new Browser();
    }

}

Comment: have a look at `allowsChildren`
Comment: Initially, I also started out with Java and later decided that if the tree could be displayed in a browser in realtime without any browser add-ons, plug-ins or extensions, then it could be used on a smartphone, tablet or desktop and be a lot more useful.

At the same time, I thought making it usable from any programming language that supported sockets would make it even more useful. And,

The end result is here and now we are using hierarchical structure to create web/cloud based realtime apps.

Disclosure: I work for Accord Software, Inc. (accord.com)
Comment: You have to "play" with the TreeCellRenderer. Would be cool if you could post some code so that we could guide you correctly.
Comment: Where should I look for "allowsChildren"?
Comment: Is the goal to create a hierarchical folder/document type tree as quickly and easily as possible using any programming language, including Java, or is it to do it specifically using Java JTree?
Comment: @ArunTaylor The goal is to be using a JTree, but in general, can you suggest a better method?
Comment: @Imray I forgot the link -- it is ActiveML.com. Disclosure: I work for Accord Software, Inc. (accord.com)
Answer: This is how you create your own TreeCellRenderer which tells your JTree whatever you want. Since you are using a default tree model with DefaultMutableTreeNode as nodes you have to extract their user object and decide what to paint based on that. Note that the default renderer is just an extended JLabel, which is why you can use setIcon(...), setText(...), etc. inside it.
The reason why your leaf nodes are painted with file icons is probably that the default renderer chooses the icon based on DefaultMutableTreeNode.getAllowsChildren() similarily to how I use Contact.isSomeProperty().
import java.awt.Component;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;

public class Browser extends JFrame {

    private JTree tree;
    //ManagerOfContacts mngrOfContacts;

    public Browser() {

        //Generates the ManagerOfContacts and associated Contacts
        //Driver driver = new Driver();
        //mngrOfContacts = driver.getManagerOfContacts();
        //---------------------------\\

        DefaultMutableTreeNode contacts = new DefaultMutableTreeNode("Contacts");
        createNodes(contacts);
        tree = new JTree(contacts);

        // use your own renderer (!)
        tree.setCellRenderer(new MyTreeCellRenderer());

        JScrollPane treeView = new JScrollPane(tree);

        add(treeView);

        setSize(400,400);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    public final void createNodes(DefaultMutableTreeNode top){
        DefaultMutableTreeNode contactName;

        // dummies
        List<Contact> contacts = new ArrayList<Contact>();
        contacts.add(new Contact("Me", true));
        contacts.add(new Contact("You"));

        Iterator<Contact> contactItr = contacts.iterator();
        while(contactItr.hasNext()){
            contactName = new DefaultMutableTreeNode(contactItr.next());
            top.add(contactName);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Browser browsr = new Browser();
            }
        });

    }

    // dummy
    private static class Contact {

        private boolean someProperty;
        private String name;

        public Contact(String name) {
            this(name, false);
        }

        public Contact(String name, boolean property) {
            this.someProperty = property;
            this.name = name;
        }

        public boolean isSomeProperty() {
            return someProperty;
        }

        public String getName() {
            return name;
        }

        @Override
        public String toString() {
            return name;
        }
    }

    // this is what you want
    private static class MyTreeCellRenderer extends DefaultTreeCellRenderer {

        @Override
        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
            super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);

            // decide what icons you want by examining the node
            if (value instanceof DefaultMutableTreeNode) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
                if (node.getUserObject() instanceof String) {
                    // your root node, since you just put a String as a user obj
                    setIcon(UIManager.getIcon("FileView.computerIcon"));
                } else if (node.getUserObject() instanceof Contact) {
                    // decide based on some property of your Contact obj
                    Contact contact = (Contact)  node.getUserObject();
                    if (contact.isSomeProperty()) {
                        setIcon(UIManager.getIcon("FileView.hardDriveIcon"));
                    } else {
                        setIcon(UIManager.getIcon("FileChooser.homeFolderIcon"));
                    }
                }
            }

            return this;
        }

    }
}

This should get you started. You should read more about this in a JTree tutorial.
Comment: Works great ! Thank you :)

By the way, to get the list of available icons : System.out.println("Default icons : "+UIManager.getDefaults());
Comment: Awesome answer - thanks! Can you explain your main method
        `SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Browser browsr = new Browser();`
Comment: Is the class `MyTreeCellRenderer` supposed to be inside the main class?
Comment: @Imray Check this [question](http://stackoverflow.com/questions/3018165/why-do-people-run-java-guis-on-the-event-queue) for an answer to your first question. You can put the renderer class anywhere you want like any other class.
Answer: Configure the treeModel to query the askAllowsChildren property of its node to decide whether it should regarded as a folder (even without having children)
DefaultMutableTreeNode root = new DefaultMutableTreeNode("I'm the root");
// node allowsChildren - that's the default
root.add(new DefaultMutableTreeNode("folder always"));
// node doesn't accept children
root.add(new DefaultMutableTreeNode("folder never", false));
// model configured to query the node's allowsChildren property
DefaultTreeModel model = new DefaultTreeModel(root, true);


R keras package: Python module tensorflow.contrib.keras.python.keras was not found

Question: Windows 7, Python 3.6 (64-bit), RStudio 1.0.143, R 3.4.0
I'm getting this error message even though it's got the correct path to tensorflow package:
> library(keras)
> data <- dataset_mnist()
Error: Python module tensorflow.contrib.keras.python.keras was not found.

Detected Python configuration:

python:         C:\Users\mikzhi\AppData\Local\Programs\Python\Python36\\python.exe
libpython:      C:/Users/mikzhi/AppData/Local/Programs/Python/Python36/python36.dll
pythonhome:     C:\Users\mikzhi\AppData\Local\Programs\Python\Python36
version:        3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)]
Architecture:   64bit
numpy:          C:\Users\mikzhi\AppData\Local\Programs\Python\Python36\lib\site-packages\numpy
numpy_version:  1.13.1
tensorflow:     C:\Users\mikzhi\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow

Switching to Python 3.5.3 doesn't help:
> data <- dataset_mnist()
Error: Python module tensorflow.contrib.keras.python.keras was not found.

Detected Python configuration:

python:         C:\Users\mikzhi\AppData\Local\Programs\Python\Python35\python.exe
libpython:      C:/Users/mikzhi/AppData/Local/Programs/Python/Python35/python35.dll
pythonhome:     C:\Users\mikzhi\AppData\Local\Programs\Python\Python35
version:        3.5.3 (v3.5.3:1880cb95a742, Jan 16 2017, 16:02:32) [MSC v.1900 64 bit (AMD64)]
Architecture:   64bit
numpy:          C:\Users\mikzhi\AppData\Local\Programs\Python\Python35\lib\site-packages\numpy
numpy_version:  1.13.1
tensorflow:     C:\Users\mikzhi\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow

python versions found:
 C:\Users\mikzhi\AppData\Local\Programs\Python\Python35\python.exe
 C:\Users\mikzhi\AppData\Local\Programs\Python\Python35\\python.exe

Comment: Try python3.5. Keras is compatible with Python 2.7-3.5 only at this stage. https://keras.io/
Comment: Am getting the same error. Not sure why it claims to have found two versions (Python35\python.exe and Python35\\python.exe).
Comment: Take a look at this issue in the keras Github repo:https://github.com/rstudio/keras/issues/37
Answer: I've managed to fix the problem by running pip3 install --upgrade tensorflow https://www.tensorflow.org/install/install_windows
Perhaps install_tensorflow() hadn't done all that was needed.


Positioning the sound in 3D using VST plugins

Question: I've listened to a lot of stereo music that when listened to through good headphones sounds quadrophonic. For instance Pink Floyd's The wall seems so 3D even in stereo.
How to place my instruments and work with them in 3D using VST plugins?
Comment: I have no idea about placement _behind_ a listener, but for front-back placement in _front_ of the listener, check out the free [Proximity](http://www.tokyodawn.net/proximity/) plugin -- it's designed to encapsulate all the different sonic elements that go into making a sound appear closer or farther away. I use it mostly on lead vocals.
Comment: https://en-us.sennheiser.com/ambeo-orbit
Answer: The 3D placement is just playing with EQ, volume and reverb.
This wont give you a perfect 3D effect but it will work. In general remember those rules.
EQ: The farther the sound the less high frequencies it will have (so maybe use a low pass filter)
REVERB: the farther the sound the more wet signal you'll ear, less early reflections and less pre-delay (when the signal is closer you have more ms of pre-delay)
VOLUME: now that's an easy one, when the sound is far away you have of course less volume.
Hope this helps a little bit.
Comment: that's close to impossible, make a sound coming below and above is one of the most difficult things to do without having more speakers. For this nice 3D effects https://www.youtube.com/watch?v=IUDTlvagjJA you have to use a special microphone.
Comment: I didn't mean only far or near. But also below or above, behind or at the sides. Sometimes the sound is all around you.
Comment: This is marvelous. Amazing. That's what I want.
Comment: check out the 3Dio microphone
Answer: In short, filter your signal through modifications of the frequency spectra to match notch shapes of HRTFs, introduce interaural time-delay for left-right distinction, and use geometry model (e.g. room) to add in early-reflections/reverb to simulate depth. We did all this in a recently released 3D panner/mixer/reverb plugin. Muze: 3D Binaural Reverb/Mixer/Panner VST/AU