Ruby Enumerable: get the first which is not nil, but the result must be what's inside the block

Question: The Enumerable#find method works by evaluating until the it finds an element which matches the condition in the block. Is there something similar for returning the first time that the block is not evaluated to nil? imagining one would have a collection of hashes:
value = nil
options.each do |o|
  break if value = o[:desired]
end
value ||= DEFAULT

isn't there a method which already accomplishes this?
No point in making a lot of transformations to the collection, i'd like to minimize the number of allocations, so any solution which allocates a new Array will not be good for me.
Comment: Not sure if that is what you re asking but doesn't just negating the `nil?` check do your bidding?
Answer: find method will work for finding first element which has :desired key with minimum iterations.
I think you wish to get the value of desired key from the block instead of element itself - there is no method in Enumerable that behaves like a mixture of find and map  - you will have to use the outer variable to which value is assigned inside the block as shown below.
options = [{foo: 1}, {desired: 2}, {bar: 3}]

value = nil
options.find do |o|
  value = o[:desired]
  break if value
end

p value
#=> 2

It more or less looks like your code, which should also work just fine.
Below is one way which you can use if you want to use Enumerable methods, but it will iterate over all elements.
p value = options.map { |o| value = o[:desired] }.compact.first

Comment: I think he wants method to find first that has `:desired` key
Comment: @SteveTurczyn  I understood what he is asking now - thanks for pointing out the flaw with my earlier answer.
Answer: As of Ruby 2.0, this can be accomplished by combining #map and #find with lazy enumerables:
value = options.lazy.map { |o| o[:desired] }.find { |x| !x.nil? } # or find(&:present?) with ActiveSupport
value ||= DEFAULT

Answer: You can use reduce:
value = options.reduce(nil){|memo, entry|  memo || entry[:desired]  } || DEFAULT

Comment: won't it parse the options collection until the end? this is what I wanted to avoid. `#find` will stop evaluation as soon as it has a match.
Comment: Correct, it's possible to break with a value though - see my answer
 https://stackoverflow.com/a/57630623/2993768
Answer: This came up for me today: I think we can use break with a value from reduce
treasure = [1,2,3].reduce(nil) do |memo, value|
  break memo if memo
  foo(value)
end

Answer: How about
options.reduce{ |_,o|
   break o if o[:desired]
   DEFAULT
}

or
catch do |tag|
   options.each{ |_,o| o[:desired] and throw tag, o }
   DEFAULT
end

The latter allows for recursion.
Comment: Would you mind editing your answer to include a brief explanation of how this differs from the previous existing answers?


Where should I put the time-invariant variables in a glmm.PQL function to perform a time-dependent negative binomial regression?

Question: Recently I have processed and cleaned a data for the aim of application of a negative binomial regression.
First, I tried to use the function glm.nb of package MASS in R and I had a problem with ensuring that the model will realize the data are for one unique participant (possible correlations in a group of observations).
Then, I realized that I can use glmmPQL of package MASS or glmer of package lme4 and use the family negative binomial in it's family link.
The question is I would like to know in which part of the model I can embed the offset (logarithm of the number of days of treatment) also how should I insert the time-constant observations for an id (such as gender and baseline age in the df)?
My latest attempt was:
(glmmPQL (event ~ treatment + offset (log(person.time)) ,
random= list (id=~1, gender=~1, baseline.age=~1),
family= negative.binomial (theta=1.75), data=df ))

which faced with a memory-related error (probably because of the wrong code).
data example:
df<-data.frame(id=rep(1:3,each=4),treatment=sample(c(0,1),12,replace = T),
event=sample(c(0,1),12,replace = T),
person.time=sample(c(15,31,30),12,replace = T),
age=rep(c(65,58,74),each=4),gender=rep(c("m","f","m"),each=4))

Answer: I would like to answer my own question here, so that might be useful for some researchers in future.
First of all, I realized that lme4 package might be a better option for mixed-effects model compared to glmPQL from MASS package.
Second, I realized that when we use a glmer model on a data that we are interested in the calculation of a rate ratio such as incidence rate ratio (as in our example) we need to use a time period as in our offset argument and in case one is interested in analysis of time series with binary outcome variable one should use the duration between two observations in a row instead of a cumulative sum of those periods (so, this was the main problem I faced with when I used glmer fucntion of lme4 package).
Inclusion of arguments within the model function, such as those used in Survival package ("tstart" and "tstop" for Surv function), for simplifying the model formula for researchers with other backgrounds and less familiarity with R will be very useful.
The code to run the model on sample data above:
tdpm<-glmer(event ~ treatment + age + gender + (1|id), offset=(log(person.time)), data = df ,verbose = T, family = "poisson")

To test the correct person-time we used, we can calculate the rates manually and compare that to the exponentiation of rate ratio in further step;
(sum(df$event[df$treatment==1])/sum(df$person.time[df$treatment==1]))/(sum(df$event[df$treatment==0])/sum(df$person.time[df$treatment==0]))
exp(fixef(tdpm))[2]

However, for reasons that you can find in methods of analysing the rates and also other more sophisticated methods developed by Robins and Hernan etal. and published in chapters 12, 19, and 21 of their Causal Inference What If book, we need to add an indicator in the model to let the model realizes the order of the consecutive person-times (or adding the next person-time to the previous ones).
Comment: `glmmTMB` is another, slightly more flexible option


How can i show running app icon in listview using ActivityManager and PackageManager?

Question: I'm trying to making a app that..
-shows currently running app icons in listview
-click item on listview that shows running app icons to switch
But i don't know how to do these, and i already googled a lot
but i found that i should use ActivityManager.RunningTaskinfo and Packagemanager to do these
Cloud you help me to do these?
How can i show running app icons in listview and give listview click event(such as OnItemClick)that switch to app which is clicked on listview?
[LeftSidePanel.java]
package kr.hybdms.sidepanel;

import java.util.ArrayList;
import java.util.List;

import kr.hybdms.sidepanel.PanelArrayAdapter;
import kr.hybdms.sidepanel.R;

import kr.hybdms.sidepanel.util.SystemUiHider;

import android.app.Activity;
import android.content.pm.PackageInfo;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 *
 * @see SystemUiHider
 */
public class LeftSidePanel extends Activity implements
OnItemClickListener {
ListView listView;
List<PanelItemDetail> rowItems;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_left_side_panel);

List<PackageInfo> packs = getPackageManager().getInstalledPackages(10);
Drawable[] images = new Drawable[packs.size()];
for(int i=0;i<packs.size();i++) {
    PackageInfo p = packs.get(i);
    images[i]= p.applicationInfo.loadIcon(getPackageManager());
 }

rowItems = new ArrayList<PanelItemDetail>();
listView = (ListView) findViewById(R.id.panelcontents);
PanelArrayAdapter adapter = new PanelArrayAdapter(this,
        R.layout.panelrow, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
    long id) {
Toast toast = Toast.makeText(getApplicationContext(),
    "Item " + (position + 1) + ": " + rowItems.get(position),
    Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
}

[PanelArrayAdapter.java]
package kr.hybdms.sidepanel;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;

public class PanelArrayAdapter extends ArrayAdapter<PanelItemDetail> {
    Context context;
    public PanelArrayAdapter(Context context, int resourceId,
            List<PanelItemDetail> items) {
        super(context, resourceId, items);
        this.context = context;
    }
    /*private view holder class*/
    private class ViewHolder {
        ImageView imageView;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        PanelItemDetail rowItem = getItem(position);
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.panelrow, null);
            holder = new ViewHolder();
            holder.imageView = (ImageView) convertView.findViewById(R.id.appicon);
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();
        holder.imageView.setImageDrawable(rowItem.getImageId());
        return convertView;
    }
}

[PanelItemDetail.java]
package kr.hybdms.sidepanel;

import android.graphics.drawable.Drawable;

public class PanelItemDetail {
    private Drawable imageId;
    public PanelItemDetail(Drawable images) {
        this.imageId = images;
    }
    public Drawable getImageId() {
        return imageId;
    }
    public void setImageId(Drawable imageId) {
        this.imageId = imageId;
    }
}

[panelrow.xml]
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

<ImageView
    android:id="@+id/appicon"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_margin="10dp" />

    </RelativeLayout>

[activity_left_side_panel.xml]
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LeftSidePanel" >

    <ImageView android:id="@+id/transparentbackground"
        android:src="@drawable/detector"
        android:adjustViewBounds="true"
        android:gravity="center_vertical"
        android:scaleType="fitXY"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_x="0dp"
        android:layout_y="0dp"
        android:layout_marginLeft="100dp"/>

    <ImageView
        android:id="@+id/panelbackground"
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:scaleType="fitXY"
        android:src="@drawable/panelbg" />

    <ListView
        android:id="@+id/panelcontents"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/panelbackground"
        android:layout_centerVertical="true"
        android:listSelector="@drawable/panel_item_bg"
        android:divider="#000000" >

    </ListView>

</RelativeLayout>

Answer: Try going through this tutorial it may help you
here!
Comment: Thanks. Now i know switch to corresponding app when click item. But what about showing running app icons in listview?


Java travando no windows server

Question: Pessoal estou com um problema, tenho uma app rodando no server 2012 ,o problema é q simplesmente depois de uns 2 dias o java trava e nao me da nenhum erro , tenho q finalizar o java e abrir novamente ai ele volta a funcionar , estou usando o glassfish , alguem ja viu esse erro ?
Comment: Não sou expert em Java EE. Mas você poderia por mais algumas informações sobre o sistema. Por exemplo, se usa alguma framework, qual banco de dados, versão do GlassFish... Mais uma coisa, já tentou com Tomcat?
Comment: bom , banco de dados firebird , uso hibernate jpa e jsf
Comment: Qual a versão do GlassFlish? Já tentou com Tomcat?
Comment: to usando o glassfish 4 , nao testei com o tomcat ainda
Comment: Qual a configuração da maquina utilizada?
Comment: é um i7 com 16 gb de memoria
Comment: Cara se a sugestão do @PedroSoares não funcionar, acredito que você irá precisar colher um pouco mais de dados sobre esse problema. Conheço pouco do Glassfish mas, se lembro bem, ele tem um console de administração com ferramentas para monitoramento. Acho que pode ser uma ferramenta legal para entender o que está acontecendo.
Comment: O primeiro passo é você monitorar o que está acontecendo na aplicação durante o travamento. É um crash da JVM, isto é, processo finaliza do nada? A aplicação simplesmente para de responder? Quanta memória ela está consumindo no momento do travamento? Você pode usar ferramentas como JVisualVM que vêm no JDK para monitorar a aplicação em tempo real. Extraia um *thread dump*, isto é, uma imagem dos stack traces de todas as threads executando no momento do travamento.
Comment: Enfim, você precisa iniciar uma investigação e não ficar chutando o problema. Isso pode ser causado por qualquer atualização que alguém fez no sistema, atualização em alguma biblioteca, aumento abrupto dos dados carregados em memória (talvez algum usuário tenha importado milhares de registros novos no banco) e assim por diante. Isso ocorre quando alguém executa alguma função específica?
Answer: Não sou expert em Java EE, mas pelo que andei lendo pela internet, algumas versões do GlassFish acontece exatamente o mesmo problema, mas ninguém sabe o porque. Então vou resumir os passos que encontrei na internet e verifique se irá funcionar no seu caso.
01) Atualize seu GlassFish, se for a ultima versão, volte para uma anterior.
02) Verifique se possui mais de 2gb de memória RAM, algumas pessoas relatam problema com 2gb de RAM com GlassFish no Windows 7/Server.
03) Comesse a usar o Tomcat.
04) Verifique como estão as rotinas do software caso seja de desenvolvimento seu, existem boas praticas que evitam esse problema, como por exemplo, não fazer consulta ao banco em GETs e SETs.

Foi só isso que achei na internet, como disse antes, não sou expert em Java, só estou querendo ajudar.
Comment: obrigado pela ajuda, amanha vou testar e ja retorno o resultado

Java travando no windows server


Lint Staged does not access subfolders

Question: I'm running the npx lint-staged command and noticed that if I have the following tree:
src
 /sub-folder-x
  - file-x
 /sub-folder-y
  - file-y

The eslint and prettier cannot find files in subfolders.
How do I get the stage to run on all subfolders and files of it?

.lintstagedrc.json
{
    "src/**/*.{js,jsx,ts,tsx}": ["prettier --check", "eslint"]
}

✖ prettier --check:
[warn] src/App.test.tsx
[warn] src/App.tsx
[warn] src/index.tsx
[warn] src/reportWebVitals.ts
[warn] Code style issues found in the above file(s). Forgot to run Prettier?

✖ eslint:
/POC/uber-web/src/App.test.tsx
/POC/uber-web/src/App.tsx
/POC/uber-web/src/index.tsx
/POC/uber-web/src/reportWebVitals.ts

Answer: It seems like your file pattern string in .lintstagedrc.json is wrong. Try remove dot before bracket and put it before each file extension definition like this:
{
   "src/**/*{.js,.jsx,.ts,.tsx}": ["prettier --check", "eslint"]
}

Answer: If you are using lint-staged along with husky, you can simply change the directory in the git-hook.
cd ./client && npx lint-staged

And the package.json should be
"lint-staged": {
  "*.{js,jsx,ts,tsx}": [
    "prettier --write",
    "eslint -c .eslintrc.json --fix"
  ]
}

Lint Staged does not access subfolders


SoftKeyboard overlaps over EditText - ConstraintLayout

Question: I created a Registration form using ConstraintLayout.
This contains vertical stack of EditText fields.
The top three EditTexts are displaying fine and I can type them while the SoftKeyboard is visible. However, for other EditText fields, they completely got hidden behind the SoftKeyboard.
Following is my Layout XML file in which I'm facing this issue.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rootView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical"
    android:isScrollContainer="true"
    android:verticalScrollbarPosition="left"
    android:fitsSystemWindows="false"
    tools:context="com.RegisterWithEmailActivity">

    <EditText
        android:id="@+id/edtRegFirstName"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_marginTop="108dp"
        android:background="@drawable/shape_edittext"
        android:ems="10"
        android:hint="@string/hintName"
        android:inputType="text"
        android:textAlignment="center"
        android:textColor="@color/textColor"
        android:textColorHint="@color/textColor"
        android:textSize="20sp"
        app:layout_constraintHorizontal_bias="0.504"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/edtRegUserName"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/shape_edittext"
        android:ems="10"
        android:hint="Username"
        android:inputType="text"
        android:textAlignment="center"
        android:textColor="@color/textColor"
        android:textColorHint="@color/textColor"
        android:textSize="20sp"
        app:layout_constraintHorizontal_bias="0.504"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/edtRegFirstName" />

    <EditText
        android:id="@+id/edtRegPassword"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/shape_edittext"
        android:ems="10"
        android:hint="@string/hintPassword"
        android:inputType="textPassword"
        android:textAlignment="center"
        android:textColor="@color/textColor"
        android:textColorHint="@color/textColor"
        android:textSize="20sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/edtRegUserName" />

    <EditText
        android:id="@+id/edtStreetAddress1"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/shape_edittext"
        android:ems="10"
        android:hint="@string/hintStreetAdd1"
        android:inputType="text"
        android:textAlignment="center"
        android:textColor="@color/textColor"
        android:textColorHint="@color/textColor"
        android:textSize="20sp"
        app:layout_constraintHorizontal_bias="0.504"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/edtRegPassword" />

    <EditText
        android:id="@+id/edtStreetAddress2"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/shape_edittext"
        android:ems="10"
        android:hint="@string/hintStreetAdd2"
        android:inputType="text"
        android:textAlignment="center"
        android:textColor="@color/textColor"
        android:textColorHint="@color/textColor"
        android:textSize="20sp"
        app:layout_constraintHorizontal_bias="0.504"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/edtStreetAddress1" />

    <EditText
        android:id="@+id/edtCity"
        android:layout_width="109dp"
        android:layout_height="36dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/shape_edittext"
        android:ems="10"
        android:hint="@string/hintCity"
        android:inputType="text"
        android:textAlignment="center"
        android:textColor="@color/textColor"
        android:textColorHint="@color/textColor"
        android:textSize="20sp"
        app:layout_constraintHorizontal_bias="0.247"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/edtStreetAddress2" />

    <EditText
        android:id="@+id/edtState"
        android:layout_width="113dp"
        android:layout_height="36dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/shape_edittext"
        android:ems="10"
        android:hint="@string/hintState"
        android:inputType="text"
        android:textAlignment="center"
        android:textColor="@color/textColor"
        android:textColorHint="@color/textColor"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.116"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintStart_toEndOf="@+id/edtCity"
        app:layout_constraintTop_toBottomOf="@+id/edtStreetAddress2" />

    <EditText
        android:id="@+id/edtZipCode"
        android:layout_width="110dp"
        android:layout_height="36dp"
        android:layout_marginEnd="188dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/shape_edittext"
        android:ems="10"
        android:hint="@string/hintZipCode"
        android:inputType="text"
        android:textAlignment="center"
        android:textColor="@color/textColor"
        android:textColorHint="@color/textColor"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/edtCity" />

    <EditText
        android:id="@+id/edtGender"
        android:layout_width="112dp"
        android:layout_height="36dp"
        android:layout_marginEnd="60dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/shape_edittext"
        android:ems="10"
        android:hint="@string/hintGender"
        android:inputType="text"
        android:textAlignment="center"
        android:textColor="@color/textColor"
        android:textColorHint="@color/textColor"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.875"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintStart_toEndOf="@+id/edtZipCode"
        app:layout_constraintTop_toBottomOf="@+id/edtState" />

    <EditText
        android:id="@+id/edtRegEmailAddress"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/shape_edittext"
        android:ems="10"
        android:hint="Email Address"
        android:inputType="text"
        android:textAlignment="center"
        android:textColor="@color/textColor"
        android:textColorHint="@color/textColor"
        android:textSize="20sp"
        app:layout_constraintHorizontal_bias="0.504"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/edtGender" />

    <Button
        android:id="@+id/btnRegisterWithEmail"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:layout_marginTop="12dp"
        android:backgroundTint="@color/colorLoginButton"
        android:elevation="@android:dimen/app_icon_size"
        android:gravity="center"
        android:padding="10dp"
        android:text="REGISTER"
        android:textColor="@color/white"
        android:textSize="20sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/edtRegEmailAddress" />

</android.support.constraint.ConstraintLayout>

This is AndroidManifest.xml snippet with some tweaks applied for SoftKeyboard.
<activity
            android:name=".RegisterWithEmailActivity"
            android:isScrollContainer="false"
            android:screenOrientation="portrait"
            android:theme="@style/Theme.AppCompat.NoActionBar"
            android:windowSoftInputMode="adjustResize|adjustPan"></activity>

Comment: Don't use `adjustResize` and `adjustPan` together. Try with `adjustPan` only.
Comment: Possible duplication? https://stackoverflow.com/q/33109976/8713068.
In the accepted answer there is a suggestion to adjust `android:windowSoftInputMode` in AndroidManifest and add `ScrollView` in your layout.
Comment: I already tried with "adjustPan" also. But it's not working in my case.
Comment: Put Constraint Layout inside a ScrollView and set windowSoftInputMode to adjustResize only
Answer:
Try using Scrollview with only one child layout. and then place your edit text's and buttons under that child layout.


Remove lines containing only 2-3 words starting with a string

Question: I need to remove lines having just 2-3 words starting with say
hi/Hi/Hello/hello
Example
hi Matt
I have tried using the following code
dropcols = ['Hi','hi','Hello']
dataextract = dataextract[~dataextract['text'].str.contains('|'.join(dropcols))]

But this would remove relevant lines like
for example - 'hi Matt, did you get my email'
And I only need to remove the line if it has
'hi Matt'
Answer: To match the first 2 or 3 words which start with hi/Hi/Hello/hello, you might use this pattern which you can remove from the string:
^[hH](?:i|ello)(?: \w+){1,2}

Explanation

^ Start of string
[hH] Match h or H
(?:i|ello) Match i or ello
(?: \w+){1,2} Repeteat 1 - 2 times matching a space and 1+ word characters

Regex demo
If you want to match all non whitespace characters, you could use \S+ instead of \w+
Answer: This expression,
^(?=.*\b(?:hi|hello)\b).*$[\r?\n]

with re.sub might be an option.
import re

regex = r"^(?=.*\b(?:hi|hello)\b).*$[\r?\n]"

test_str = """
hi alice
some other words

Hi bob
some other words

Hello alice
some other words

hello bob
some other words

hi Matt
some other words
"""

subst = ""

print(re.sub(regex, subst, test_str, 0, re.MULTILINE | re.IGNORECASE))

Output
some other words

some other words

some other words

some other words

some other words

The expression is explained on the top right panel of this demo if you wish to explore/simplify/modify it.


How to move views pager to different region as a block?

Question: I created a page view and results are showing up in main page content block. Now i need to move the pager attached with view to different region.
I am not sure how to achieve this. Please suggest any preprocess or backend feature which can do this?
Comment: CSS absolute positioning is one option.
Comment: @NoSssweat is there any Drupal way, any preprocess or something?
Answer: The easiest is probably the comment from @No SSsweat
Another easy option is to place the view in each region and hide the pager/content using CSS depending on region.
The javascript option may be possible depending if you have anything already in place, as you can move the DOM pager element programmatically.
The neater solution is to use some php code, the drupal way, probably someone has already done this for you, so look for a drupal contributed module related to view and pager or paging, and layout, those would probably be my keywords for a search.
Comment: If using CSS the best method is CSS Grid, you can shuffle elements pretty creatively with it. It can be difficult to make absolute positioning pass some accessibility tests. Depending on the situation with other content around/in-between the content and bottom content regions you might even do it with Flexbox (much easier than Grid) and that supports older browsers too.


VPN trace route

Question: I am inside an Active Directory (AD) domain and trying to trace route to another AD domain at a remote site, but supposedly connected by VPN in between.
the local domain can be accessed at 192.168.3.x and the remote location 192.168.2.x. When I do a tracert, I am suprised to see that the results did not show the intermediate ISP nodes. If I used the public IP of the remote location, then a normal tracert going through every intermediate node would show.
<code>1    <1 ms    <1 ms    <1 ms  192.168.3.1
2     1 ms    <1 ms    <1 ms  192.168.3.254
3     7 ms     7 ms     7 ms  212.31.2xx.xx
4   197 ms   201 ms   196 ms  184.108.40.206xx
5   201 ms   201 ms   210 ms  vacc27.norwich.vpn-acc.bt.net [188.8.131.52]
6   209 ms   209 ms   209 ms  81.146.xxx.xx
7   209 ms   209 ms   209 ms  COMPANYDOMAIN [192.168.2.6]
</code>
Can someone explain how does this VPN tunnelling works? Does this mean VPN is technically faster than without?
Comment: I suggest taking a few moments to read up on VPN concepts, which will explain this and many other questions you may have.
Comment: @John. I tried to, and on many other occassions as well. But most text require many other advanced concepts and it's hard to following along given my limited understanding.
Comment: in a nutshell: A VPN comprises two layers of traffic. The outer layer does all the routing and knows about the nodes it passes through. The inner, secure, layer knows only the two endpoints and doesn't even see anything else along the way.
Answer: VPNs use tunneling to get packets from one end to the other. You wont see those hops . If you want to locate a problem router or failed router to the end point you must route your icmp packets outside the tunnel using the wan interface.
VPN is a bit of a broad term, it can use many types of encapsulation. Most commonly PPTP (point to point tunneling protocol) is used. More often than not with the use of IPSEC to secure the transmission.
(Avoid adding a PBR by just doing a traceroute from the router/firewall that the VPN is established from)
Comment: the word "encapsulation" suddenly made everything seem much clearer. thanks for the hint.
Comment: I remember none of this really made a lot of sense for me until ccna. If you are interested there are some really good tutoring videos from 'train signal' that make it easy to understand.
Answer: If there is a site to site VPN setup between your two locations a trace route to the remote site will show less hops then a trace route to the Public IP of the remote site because the first trace route is not showing the nodes that the site to site is doing for you
It does not necessairly mean your VPN connection will make it any faster, in reality with the overhead of the VPN it can sometimes be slower but performance will depend on the type of VPN connection and speeds of all connections involved.

VPN trace route


How to evaluate $\int_{-\infty}^\infty \frac{\cos x}{\cosh x}\,\mathrm dx$ by hand

Question: How can I evaluate$$\int_{-\infty}^\infty \frac{\cos x}{\cosh x}\,\mathrm dx\quad\text{ and }\quad\int_0^\infty\frac{\sin x}{e^x-1}\,\mathrm dx\,?$$
Thanks in advance.
Comment: @TymaGaidash This question was asked 9 years ago.
Comment: Please add context and any research about the problem. Did you read [How to ask a good question](https://math.meta.stackexchange.com/questions/9959/how-to-ask-a-good-question)?
Answer: For the second one,
$$ \begin{align*}
\int_{0}^{\infty} \frac{\sin x}{e^x - 1} \; dx
&= \int_{0}^{\infty} \frac{\sin x \, e^{-x}}{1 - e^{-x}} \; dx \\
&= \int_{0}^{\infty} \left( \sum_{n=1}^{\infty} \sin x \, e^{-nx} \right) \; dx \\
&\stackrel{\ast}{=} \sum_{n=1}^{\infty} \int_{0}^{\infty} \sin x \, e^{-nx} \; dx \\
&= \sum_{n=1}^{\infty} \frac{1}{1+n^2},
\end{align*}$$
where the starred identity is justified by the following formula
$$ \begin{align*}
\int_{0}^{\infty} \frac{\sin x}{e^x - 1} \; dx
&= \int_{0}^{\infty} \left( \frac{1 - e^{-Nx}}{1 - e^{-x}} e^{-x} + \frac{e^{-Nx}}{e^x - 1} \right) \sin x \; dx \\
&= \sum_{n=1}^{N} \int_{0}^{\infty} \sin x \, e^{-nx} \; dx + \int_{0}^{\infty} \frac{\sin x \, e^{-Nx}}{e^x - 1} \; dx,
\end{align*}$$
together with the dominated convergence theorem. Now the resulting infinite summation can be evaluated in numerous ways. For example, exploiting identities involving the digamma function,
$$ \sum_{n=1}^{\infty} \frac{1}{1+n^2} = \frac{1}{2i} \sum_{n=1}^{\infty} \left( \frac{1}{n-i} - \frac{1}{n+i} \right) = \frac{\psi_0(1+i) - \psi_0(1-i)}{2i} = -\frac{1}{2} + \frac{\pi}{2} \coth \pi. $$
Similar techniques apply to the first integral.
Comment: Or, there's always the [partial fraction series for the hyperbolic cotangent](http://dlmf.nist.gov/4.36.E3) (which can be derived by logarithmically differentiating the infinite product for hyperbolic sine).
Comment: Hmmm...shouldn't it be $$-\frac{1}{2}+\frac{\pi}{2}\coth\pi\,\,?$$I mean, otherwise it is far and away from the approximations WA gives...although I didn't succeeded to obtain the value in WA with the upper limit infinity.
Comment: @DonAntonio: You're right!
Comment: @J. M.: good point (+1)
Comment: @sos440: hi (sister here). Do you know a nice way to prove that $\int_{0}^{\infty} \frac{\sin x \, e^{-nx}}{e^x - 1} \; dx$ tends to $0$ when $n\to\infty$?
Answer: For the first one for example by using the rectangular contour $-R\to +R \to +R+2\pi i \to -R+2\pi i \to -R$
and since there are two simple poles from $\cosh(z)$ at $z=\frac {\pi i}2$ and $z=\frac {3\pi i}2$ of values $-ie^{-\frac {\pi i}2}$ and $ie^{-\frac {3\pi i}2}$ we have :
$$2\pi i\ \left(\rm{Res}\left(\frac {e^{iz}}{cosh(z)}; \frac {\pi i}2\right)+\rm{Res}\left(\frac {e^{iz}}{cosh(z)}; \frac {3\pi i}2\right)\right)=2\pi i \left(-ie^{-\frac {\pi}2}+ie^{-\frac {3\pi}2}\right)$$
so that the integral over the rectangular contour of $\dfrac {e^{i x}}{\cosh(x)}$ will be :
$$\int_{-R}^R \frac {e^{i x}}{\cosh(x)}\;dx+\int_0^{2\pi} \frac {e^{i R-y}}{\cosh (R+iy)}\;dy+\int_R^{-R} \frac {e^{-2\pi+i x}}{\cosh(2\pi i+x)}\;dx+\int_{2\pi}^0 \frac {e^{-i R-y}}{\cosh (-R+iy)}\;dy=2\pi \left(e^{-\frac {\pi}2}-e^{-\frac {3\pi}2}\right)$$
I'll let you prove that the two integrals in $y$ disappear as $R\to\infty$ so that only remains :
$$\lim_{R\to \infty} \int_{-R}^R \frac {e^{i x}}{\cosh(x)}\;dx-\lim_{R\to \infty}\int_{-R}^R \frac {e^{-2\pi-i x}}{\cosh(2\pi i-x)}\;dx=2\pi \left(e^{-\frac {\pi}2}-e^{-\frac {3\pi}2}\right)$$
it is easy to show that $\ \cosh(2\pi i+x)=\cosh(x)$ (use exponential notation)
so that :
$$(1-e^{-2\pi})\int_{-\infty}^\infty \frac {e^{i x}}{\cosh(x)}\;dx=2\pi \left(e^{-\frac {\pi}2}-e^{-\frac {3\pi}2}\right)$$
and the result (keeping the real part) :
$$\int_{-\infty}^\infty \frac {\cos x}{\cosh x}\;dx=2\pi \frac{e^{-\frac {\pi}2}}{1+e^{-\pi}}=\frac{\pi}{\cosh \frac{\pi}2}$$
(the second one may be solved the same way...)
Comment: I prefer real analysis methods to complex ones because of my personal taste, but I also agree that complex method is undisputedly the most general and elegant method to evaluate these kinds of integrals or summations, in the sense that this often shows the glimpse of the true nature behind them. +1, of course!
Comment: I just knew contours could be used (as with a lot of these sorts of integrals), but I got stuck picking the right one. +1, of course.
Comment: @J.M.: I must admit that it is rather *laborious* this way (I am much more fond of derivation under the integral and so on...)
Comment: @sos440: with clearly the advantage of speed here! :-) (when other methods exist they are often more direct and thus faster than complex integration...). Your nice derivation got my vote too!
Answer: Note
\begin{align}
I(a)=&\int_{0}^\infty \frac{\cosh ax}{\cosh x}\,dx\\
\overset{t=e^{-x}}=&\int_0^1 \frac{t^a}{1+t^2}dt
 + \int_0^1 \frac{t^{-a}}{1+t^2}\overset{t\to1/t}{dt}
=\int_0^\infty \frac{t^a}{1+t^2}dt \\
=& \frac{1}{2} \Gamma \left( \frac{1+a}{2} \right) \Gamma \left(\frac{1-a}{2} \right)
=\frac\pi2\sec\frac{a\pi}2
\end{align}
Then
$$\int_{-\infty}^\infty \frac{\cos x}{\cosh x}\,dx
= 2I(i)=\pi\>\text{sech}\frac\pi2
$$
Comment: The step where you get $\frac\pi2\sec\left(\frac{a\pi}2\right)$ is not clear without any explanation. Also using imaginary values for $a$ requires verification that the contours involved do not contain any singularities.
Comment: The edit to the title of the question was very minor for a question that is 9 years old.
Comment: You may comment on the step leading to the explicit form in terms of the secant function.
Answer: Let us start from the Weierstrass product for the sine function:
$$ \sin(x) = x \prod_{n\geq 1}\left(1-\frac{x^2}{\pi^2 n^2}\right)\tag{1} $$
This identity over the real line can be proved by exploiting Chebyshev polynomials, but it holds over $\mathbb{C}$ and it is equivalent to
$$ \sinh(x) = x \prod_{n\geq 1}\left(1+\frac{x^2}{n^2\pi^2}\right).\tag{1'} $$
If we apply $\frac{d}{dx}\log(\cdot)$ (the logarithmic derivative) to both sides of $(1)$ we end up with
$$ \cot(x)-\frac{1}{x} = \sum_{n\geq 1}\frac{2x}{x^2-n^2 \pi^2} \tag{2}$$
$$ \coth(x)-\frac{1}{x} = \sum_{n\geq 1}\frac{2x}{x^2 + n^2 \pi^2} \tag{2'}$$
and by expanding the main term of the RHS of $(2)$ as a geometric series we have
$$ \frac{1-x\cot x}{2} = \sum_{n\geq 1}\frac{\frac{x^2}{n^2 \pi^2}}{1-\frac{x^2}{n^2\pi^2}} =\sum_{n\geq 1}\sum_{m\geq 1}\left(\frac{x^2}{n^2 \pi^2}\right)^m = \sum_{m\geq 1}x^{2m}\frac{\zeta(2m)}{\pi^{2m}} \tag{3}$$
$$ \frac{1-x\coth x}{2} = \sum_{m\geq 1}(-1)^m x^{2m}\frac{\zeta(2m)}{\pi^{2m}}\tag{3'} $$
$(3')$ alone is enough to prove that $\zeta(2m)$ is always a rational multiple of $\pi^{2m}$, related to a coefficient of the Maclaurin series of $\frac{z}{e^z-1}=-\frac{z}{2}+\frac{z}{2}\coth\frac{z}{2}$, i.e. to a Bernoulli number, but that is not the point here.
Let us deal with the second integral:
$$ \int_{0}^{+\infty}\frac{\sin z}{e^z-1}\,dz = \sum_{m\geq 1}\int_{0}^{+\infty}\sin(z) e^{-mz}\,dz = \sum_{m\geq 1}\frac{1}{m^2+1} $$
can be evaluated through $(2')$ by picking $x=\pi$. In a similar fashion
$$ \int_{0}^{+\infty}\frac{\cos z}{e^z+e^{-z}}\,dz = \sum_{m\geq 0}(-1)^m\int_{0}^{+\infty}\cos(z)e^{-(2m+1)z}\,dz = \sum_{m\geq 0}(-1)^m \frac{(2m+1)}{(2m+1)^2+1} $$
can be computed from the Weierstrass product of the cosine function,
$$ \cos(x) = \prod_{m\geq 0}\left(1-\frac{4x^2}{(2m+1)^2\pi^2}\right)\tag{4} $$
$$ \cosh(x) = \prod_{m\geq 0}\left(1+\frac{4x^2}{(2m+1)^2\pi^2}\right)\tag{4'} $$
leading by logarithmic differentiation to
$$ \sum_{m\geq 0}\frac{x}{x^2+(2m+1)^2} = \frac{\pi}{4}\tanh\left(\frac{\pi x}{2}\right)\tag{5} $$
and also to
$$ \sum_{m\geq 0}\frac{(-1)^m(2m+1)}{x^2+(2m+1)^2} = \frac{\pi}{4}\operatorname{sech}\left(\frac{\pi x}{2}\right).\tag{6}$$
Comment: May I ask how you got $(-1)^m(2m+1)$ in equation $(6)$?
Answer: Second integral
$$
\begin{aligned}
\int_0^{\infty} \frac{\sin x}{e^x-1} d x&=\int_0^{\infty} \frac{e^{-x} \sin x}{1-e^{-x}} d x \\
& =\sum_{n=0}^{\infty} \int_0^{\infty} e^{-(n+1) x} \sin x d x \\
& =\sum_{n=0}^{\infty} \frac{1}{(n+1)^2+1} \\
& =\sum_{n=1}^{\infty} \frac{1}{n^2+1}
\end{aligned}
$$
To get rid of the square of $n$, we resolve it into two fractions and then reunite them as
$$
\begin{aligned}
\int_0^{\infty} \frac{\sin x}{e^x-1} d x & =\frac{1}{2 i} \sum_{n=1}^{\infty}\left(\frac{1}{n-i}-\frac{1}{n+i}\right) \\
& =\frac{1}{2 i} \sum_{n=1}^{\infty}\left(\frac{1}{n-i}+\frac{1}{-n-i}\right) \\
& =\frac{1}{2 i}\left(\sum_{n=-\infty}^{\infty} \frac{1}{n-i}+\frac{1}{i}\right)
\end{aligned}
$$
Using the result$$
\pi \cot (\pi z)=\lim _{N \rightarrow \infty} \sum_{k=-N}^N \frac{1}{z+k} \text {, where } z \notin \mathbb{Z}
$$
We can now conclude that
$$
\begin{aligned}
\int_0^{\infty} \frac{\sin x}{e^x-1} d x & =\frac{1}{2 i}\left[\pi \cot (-i \pi)+\frac{1}{i}\right] \\
& =-\frac{1}{2}+\frac{\pi}{2} \operatorname{coth} \pi
\end{aligned}
$$


Python pandas unindo planilhas que estão dentro de um array

Question: Comunidade, eu tenho um diretório que contém várias planilhas ".xls" e gostaria de unir essas planilhas em um único DataFrame e como bônus exportar esse novoDataFrame em ".xlsx".
Estou usando o for para percorrer o diretório em busca dos arquivos ".xls" salvo eles na variável files_xlsx e gostaria de juntar (merge) dessas planilhas, segue o código abaixo:
from numpy import append
import pandas as pd
import os
from openpyxl.workbook import Workbook
path = r'/Users/danilobrun/Downloads/temp/pandasimovelacao'
files = os.listdir(path)

files_xlsx = [path + '/' + f for f in files if f[-3:] == 'xls']
print(files_xlsx)
for f in files_xlsx:
    data = pd.read_excel(f)
    df = pd.merge(data, data, how = 'outer')

print(f'Meu DataFrame completo {df}')

Comment: Por favor, edite a pergunta para limitá-la a um problema específico com detalhes suficientes para identificar uma resposta adequada.
Comment: Caso a resposta fornecida tenha resolvido o problema apresentado, considere por favor clicar no botão ✔ para aceitar a resposta, por favor.
Answer: *Resposta adaptada e traduzida do Stack Overflow em inglês*
Import multiple excel files into python pandas and concatenate them into one dataframe
Um exemplo usando 5 arquivos Excel idênticos que são anexados ("appended") sequencialmente.
(1) Imports
import os
import pandas as pd

(2) Listando arquivos:
>>> caminho_atual = os.getcwd()
>>> arquivos = os.listdir(caminho_atual)
>>> print(arquivos)
['.DS_Store',
 '.ipynb_checkpoints',
 '.localized',
 'Screen Shot 2013-12-28 at 7.15.45 PM.png',
 'test1 2.xls',
 'test1 3.xls',
 'test1 4.xls',
 'test1 5.xls',
 'test1.xls',
 'Untitled0.ipynb',
 'Werewolf Modelling',
 '~$Random Numbers.xlsx']

(3) Filtrar por arquivos 'xls':
>>> arquivos_xls = [arquivo for arquivo in arquivos if arquivo[-3:] == 'xls']
>>> print(arquivos_xls)
['test1 2.xls', 'test1 3.xls', 'test1 4.xls', 'test1 5.xls', 'test1.xls']

(4) Inicializar dataframe vazio:
df = pd.DataFrame()

(5) Iterar sobre lista de arquivos para anexá-los ("append") ao dataframe vazio:
for arquivo in arquivos_xls:
    dados = pd.read_excel(arquivo, 'Sheet1')
    df = df.append(dados)

Saída
xlsx
df.to_excel('dataframe_mesclado.xlsx', engine='xlsxwriter')

stdout
>>> print(df)
  Result  Sample
0      a       1
1      b       2
2      c       3
3      d       4
4      e       5
5      f       6
6      g       7
7      h       8
8      i       9
9      j      10
0      a       1
1      b       2
2      c       3
3      d       4
4      e       5
5      f       6
6      g       7
7      h       8
8      i       9
9      j      10
0      a       1
1      b       2
2      c       3
3      d       4
4      e       5
5      f       6
6      g       7
7      h       8
8      i       9
9      j      10
0      a       1
1      b       2
2      c       3
3      d       4
4      e       5
5      f       6
6      g       7
7      h       8
8      i       9
9      j      10
0      a       1
1      b       2
2      c       3
3      d       4
4      e       5
5      f       6
6      g       7
7      h       8
8      i       9
9      j      10

OBS: Pode ser necessário ter que instalar o módulo xlsxwriter para escrever o arquivo "dataframe_mesclado.xlsx". Para isso basta executar o seguinte comando do pip:
python -m pip install -U xlsxwriter

EDIT: Conforme comentado pelo @AugustoVasques, a partir da versão 1.4.0 do pandas o método pandas.DataFrame.append foi depreciado portanto o método recomendado agora é pd.concat:
* Revisão dos passos (4) e (5) *
df = pd.concat(pd.read_excel(arquivo, 'Sheet1') for arquivo in arquivos_xls)


How to install drush 8 properly on Debian 8?

Question: Finding hundreds of articles and documents about how to install drush and none of it explains where exactly and how properly I got a bit lost.
I've got a Debian 8 system with a root user, NGinx and a ssh user.
Now I'd like to go with drush 8 to be ready for Drupal 8 soon.
But I don't feel like installing git on my server - is this common practice? I usually think the fewer software the better?
So - any chance of installing drush without having git?
Next thing that all instructions seem to forget:
As which user do I need to install drush and composer? (I did it now as the ssh user with the result that root can't use drush).
And eventually: Which directory would be correct to have drush installed? /usr/bin/ ? With a symlink? Or directly there?
Does the NGinx user (www-data) need to be able to use drush? (I guess yes for drush pm-update?)
Very lost here - hope someone can shed some light.
Answer: (Updated to fix links and clarify Drush 8 vs 9.)
Debian still (as of February 8th 2018) comes with Drush 5.10 which is completely unacceptable.
There is no reason to use Git to download Drush. Either

If you want the old Drush 8 (that works for Drupal 8.3 and older, including Drupal 7) download a single PHAR file using the the straight forward instructions on http://docs.drush.org/en/8.x/install/ or
If you want the current Drush 9, install a site-local Drush (with Composer) and Drush Launcher (as a PHAR file) as described on http://docs.drush.org/en/master/install/
Comment: Please comment why you downvote. In this case, I assume it was because the links in my answer was dead - updated now.
Comment: Voting is anonymous by design, people don't tend to leave comments when they up or down vote as historically people get harassed and abused when they do
Comment: Too bad (about the harassment/abuse). It would be very useful to know - so you can improve the answer.
Comment: Yeah it's a shame, human nature or something I guess. In an ideal world everyone would be honest, just vote on the content, and not take downvotes personally. Then we could force/encourage people to explain what they objected to. Reality is somewhat different though. FWIW I can't think of a single thing wrong with either version of the answer, except the broken links. If you get the odd random downvote on an answer don't give it too much thought, if you start getting a few on the same one that's the time to think about it
Comment: It's actually pretty nice to use Git to download Drush. You can very [easily check out different release tags](https://linode.com/docs/websites/cms/drush-drupal/), `composer install`, done :)
Answer: [update] If you are happy with an older version (Drush 5.10) then you could install from Debian repos.
sudo apt-get update && sudo apt-get install drush

Note that git is a suggested package but it shouldn't auto install by default. Reference: https://packages.debian.org/jessie/drush
If Drush 5 is too old then you can always re-evaluate your options then...
What version control do you use for your server/CMS/etc? If you don't use version control then I would strongly argue that you should just learn how to use git. IMO it is the most awesome vc system that I have ever used... I would say that using git (let alone installing it) is pretty common dev practice!
Regardless there are possibly ways to install drush without installing git but you could just install git then remove it again after if you don't want it!? AFAIK drush now requires composer to complete the installation and I'm pretty sure that needs git too. So perhaps just bite the bullet...
So my suspicion is that even if it's possible then it will be a lot more painful and a lot more mucking around...
For the record this is how I install drush (latest dev version) then use it to install drupal8:
SRC=/usr/local/src
WEBROOT=/var/www/drupal
# install composer
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer

# install drush dev-master
git clone --depth 1 https://github.com/drush-ops/drush.git $SRC/drush
cd $SRC/drush
composer install
ln -s $SRC/drush/drush /usr/local/bin/drush
ln -s $SRC/drush/drush.complete.sh /etc/bash_completion.d/drush

mkdir -p /etc/drush
cat > /etc/drush/drushrc.php << EOF
<?php
// by default use the drupal root directory
\$options['r'] = '$WEBROOT';
EOF

# download latest drupal8 and install
drush dl drupal-8 --destination=$(dirname $WEBROOT)
mv $(dirname $WEBROOT)/drupal-8* $WEBROOT

Comment: Hmm .. that would work I guess. How ever, since I'm using Debian I kind of got used to the stable but a bit old packages.
Throwing git onto a server and getting things from the latest dev .. is this a safe practice?
Comment: TBH I hadn't even considered that. I will update my answer. Debian stable is awesome (I'm still using Wheezy on my main laptop and have been using Jessie server a fair bit lately). But Debian do not (yet?) have a composer package. They do however have a 'drush" package!

Getting source from GitHub does not always mean "bleeding edge". E.g. Drush use their master branch for development release but bleeding edge development is not merged unless it passes tests first. Obviously there is still a chance that it might be buggy; but I've seen plenty of buggy packages in Debian stable too!


What VMWare software is necessary to run a clustered Exchange environment

Question: It's budget time at my agency, and we're planning to move to Exchange 2010 next year.  We are a small shop, so we just want to run a two-server cluster of Exchange, providing fail-over safety if one server has an issue.
We run this scenario now, however, next year we'd like to move this to a virtual environment.  What VMWare software is necessary for this to work?
Currently we own a VMWare Infrastructure 3 Foundation license, and run ESXi on our VM host.  I would imagine that we have all we need there.  We could run two VMs with Windows 2008 Enterprise, and from the Windows and Exchange server's perspective, clustering should work the same.  I think.
Can anyone else confirm, or shed more light on the topic?  Thanks!
Answer: So Exchange IS supported on SVVP qualified virtualization platforms which includes HyperV and VMWare VI.
Exchange 2010 however has a completely new high availability model which is not at all traditional clustering. I would encourage you to research this (specifically DAGs) and how they work when considering your VMWare deployment here.
Additionally DAGs are not supported with VMWare HA (as in doesn't work).
Thanks,
Brian Desmond
Active Directory MVP
Answer: Not sure I entirely see what you are after.  But if you set up a cluster of two VMs on the same machine, that kind of defeats much of the purpose of clustering.  You will be adding the overhead of two virtualized OSes, and if the actual machine fails you will lose both.
As far as witch VMware product is needed, it really just depends on what features you want.  I have Exchange 2003 running perfectly stable on VMware server 1.x with a Linux as the host machine.
Comment: You make a good point.  We should probably run the exchange server VMs on separate VM hosts as well.

But even if they are on the same box, the idea would be to a) have fail-over protection of a software crash of one server, and b) allow us to patch the individual Exchange servers without taking down Exchange entirely.

We also use Mimecast, so if both servers did go down we could  fail-over to the cloud to get our mail, in case that helps explain our position coming into this.
Comment: Right, that would be the advantage.  As far as patching goes, the main advantage I think is the ability to take a snapshot before you do apply the patches.  VMWare server supports snapshots, but only one.  So you might want something that supports multiple snapshots.
Comment: I think that is one snapshot per vm, but I might be wrong at the one
Answer: Where I used to work employed VMware ESX and VMotion to handle their AD DCs and Exchange servers.
When implemented, there were 6 physical servers, 5 fully used at any given time.
Each of the pair of DCs took nearly a full server's capacity. Likewise, each of the two Exchange servers used nearly a full server's resources.
All VMs lived on a SAN.
By turning-on VMotion, in the event that any physical server had load issues, VMware would move the server from the failing machine to a lightly-loaded one.
It works great - handling several thousand active users.
Answer: are you wanting to put both servers on the same host computer? I would suggest getting another small server put 1 VM on each and look into VM Ware's high availability options so if 1 box dies it will fail it over to the other.
Answer: One thing to keep in mind is that unless something had changed, Microsoft didn't support Exchange in a virtualized environment. I don't know if that factors into your support plans. This may have changed now that MS is pushing Hyper-V, though.
Here's a whitepaper right from VMWare on best practices with hosting an Exchange server.
EDIT: more info on support from MS and virtualized environments is here.
Comment: +1 That is a good thing to know that it is not supported.  Won't stop me from doing it, but I will remember not to mention it if I ever try to get support ;-)
Comment: since when has it not been supported? I know a lot of companies and universities that are running Exchange in a virtutalized environment with full support from MS.
Comment: the article you cite is for Windows 2k8. My experience with VMware and Exchange was with 2k3.
Comment: and the link you provide also goes to here: http://www.windowsservercatalog.com/svvp.aspx - which clearly states what VMware is a partner.
Comment: @warren: Then the situation had changed...I can tell you that back when I was looking at this type of virtualization before Exchange was *not* a supported configuration to use.
Comment: @warren: article online dated april 2009, found http://windowsitpro.com/article/articleid/101817/exchange-server-virtualization-is-it-time.html , saying that: It's hard to believe it's been over half a year since Microsoft announced its official support policy for running Microsoft Exchange Server in a virtual environment.

So it it wasn't "official" until fairly recently. It had been done, but I said, quote, One thing to keep in mind is that unless something had changed, Microsoft didn't support Exchange in a virtualized environment.
Comment: As a further thanks for the downvote...check out these links...since I'm not making up that it is fairly recent that you were able to get decent support (TECH SUPPORT) for running Exchange in VMWare...
http://www.vcritical.com/2009/04/nice-windows-vm-youve-got-there/

http://ryanlrussell.blogspot.com/2009/04/hey-you-get-off-of-that-cloud.html
Comment: it was a fully supported configuration as of summer 2006 when the university I attended replaced their Exchange infrastructure
Comment: http://bink.nu/news/microsoft-changes-its-support-for-vmware.aspx && http://www.dell.com/downloads/global/solutions/Virtualizing_Exchange2003.pdf && http://www.microsoft.com/downloads/details.aspx?familyid=77B6D819-C7B3-42D1-8FBB-FE6339FFA1ED&displaylang=en
Comment: @Warren: Supported as in works, or supported as in call support and they don't shudder when you say your hardware is virtual? I posted links supporting what I said and I had someone working with MS Support TELL ME that they didn't support that configuration officially. So unless you're saying I'm lying, I'm telling you that there were until recently (And in the links it reiterates that it happened to people) that you could be told *WE DON'T SUPPORT THAT*. It worked. People were doing it. But MS DIDN'T NECESSARILY LIKE IT UNTIL RELATIVELY RECENTLY.
Answer: I currently host an Exchange 2007 cluster under VMware without issue.
You'll need some sort of shared storage that can be presented over iSCSI for the Quorum drive of the cluster.  The other disks which hold the Exchange databases can be local disks to the VM.
Answer: @Kyle
The benefit Ken would gain from running two instances on a single physical box would be in case the operating system inside one of the vms went nutty. Ive seen exchange poo itself so many times in the past it sounds like something I would do.
@Ken
As for what you would need.
You would need the High Availability (HA) or Fault Tolerant (FT) ability in VMware (depending on how you want your redundancy. FT runs two instances in lock step, HA just fails over.
Comment: You wouldn't need HA or FT for the scenario he described. He only has one VM host, buying HA or FT would b useless wtha single host.


How to draw growth rate line in dc.js barChart?

Question: I have Statewise, sectorwise(Agriculture, Manufacturing, Mining etc.) & yearwise GDP data of India. I have created a dashboard that can be found at India GDP. Now in the barchart I want to draw a line indicating growth rate at each year. I think it can be done by composite chart but I don't know how to calculate dynamic growth rates each time a filter is applied. Can anybody provide a guidance.
Comment: It's a good question. I am torn between figuring out how to do it with a fake group, and just advising the use of a renderlet.
Comment: Thanks for the reply...I have just started coding, will explore renderlet. If you can provide any guidance on how to use renderlet for this specific task, I will be grateful. Also, I do think this feature should be standard in dc.js, it is a very common graph used in business analysis.
Answer: You are right that there should be a standard way to do this.  The problem is that it doesn't fit into dc.js's way of dealing with data through crossfilter, and there are any number of graphical objects you might want to overlay on a chart.
So, for now we have renderlets.
The idea of using a renderlet is to write a function that drops into lower-level d3 code once the chart has rendered.  You might search around for the exact d3 code you want, but here is something which adds a single red line on top of the bars:
<code>.on('renderlet', function(chart) {
    var extra_data = [{x: chart.x().range()[0], y: chart.y()(10)},
                      {x: chart.x().range()[1], y: chart.y()(70)}];
    var line = d3.svg.line()
        .x(function(d) { return d.x; })
        .y(function(d) { return d.y; })
        .interpolate('linear');
    var path = chart.select('g.chart-body').selectAll('path.extra').data([extra_data]);
    path.enter().append('path').attr('class', 'extra').attr('stroke', 'red');
    path.attr('d', line);
});
</code>
This draws the line from the left side to the right side, from Y value 10 to Y value 70, using the x and y scales to obtain the SVG coordinates.
I created a bar with extra line example.
Hopefully this will get you started!
Comment: Is there a way to give this line a label/text/title that flows along the length of the line something that says for e.g. `global average` ?
Comment: Hi @coding_idiot, sure; I've added it to the example linked above.


PowerShell script is NOT displaying any output on the console

Question: I have the below code snippet which runs from a C# file in the backend.
using (Process process = new Process())
{
    process.StartInfo.FileName = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe";
    String cmd = "C:\\Users\\xyz\\AppData\\Local\\Temp\\2\\Demo.ps1";
    process.StartInfo.Arguments = cmd;
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardOutput = false;
    process.StartInfo.RedirectStandardError = true;
    StringBuilder error = new StringBuilder();

    process.Start();

    while (true)
    {
        bool hasExited = process.HasExited;

        String errorStr = null;
        while ((errorStr = process.StandardError.ReadLine()) != null)
            error.AppendLine(errorStr);

        if (hasExited)
            break;

        Thread.Sleep(100);
    }

    if (process.ExitCode != 0 || error.Length > 0)
        throw new Exception(error.ToString());
}

Demo.ps1
write-host "Working Properly now! "

I want the string to be printed in the same PowerShell window which is open, but somehow it's not showing any output to the opened PowerShell window. If I redirect output to a text file then it is clearly visible.
Comment: If you want to run PowerShell code from C#, a better option (especially if you want easy access to the output) is to use the [PowerShell Class](https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.powershell?view=powershellsdk-7.4.0)
Comment: Three bogus answers, with at least one generated by [ChatGPT](https://meta.stackoverflow.com/questions/421831/temporary-policy-chatgpt-is-banned)? If that is the case, it is a really sad state of Stack Overflow.
Answer: This is a better approach in my opinion. It is avoiding the hassle of trying to manage by hand the output (std/err).
There is a functionality directly in C#. It is simplifying the interaction and easier for debugging as well.
try
{
    ScriptBuilder script = new ScriptBuilder();
    script.LoadScript(File.ReadAllText(@"some.ps1"));
    script.AddParameters(new Dictionary<string, string>
    {
        {"param1", "param1" }
    });
    script.Run();
    System.Console.WriteLine("Hit any key to exit.");
    System.Console.ReadKey();
}
catch (Exception ex)
{
    System.Console.WriteLine(ex.Message);
}

This is a simple wrapper used
public class ScriptBuilder
{
    private readonly PowerShell powerShellScript;

    public ScriptBuilder()
    {
        powerShellScript = PowerShell.Create();
    }

    public void LoadScript(string script)
    {
        powerShellScript.AddScript(script);
    }

    public void AddParameters(Dictionary<string, string> parameters)
    {
        foreach (var parameter in parameters)
        {
            powerShellScript.AddParameter(parameter.Key, parameter.Value);
        }
    }

    public void Run()
    {
        var output = new PSDataCollection<PSObject>();
        output.DataAdded += Output_DataAdded;

        powerShellScript
            .BeginInvoke<PSObject, PSObject>(null, output)
            .AsyncWaitHandle
            .WaitOne();

        powerShellScript.Invoke();
    }

    private static void Output_DataAdded(object? sender, DataAddedEventArgs e)
    {
        if (sender == null)
        {
            return;
        }
        PSObject newRecord = ((PSDataCollection<PSObject>)sender)[e.Index];
        Console.WriteLine(newRecord);
    }
}

This example is passing arguments to the script and executing the script.
These are some links which could help with further extending the code:
powershell-host
system.management.automation.powershell
PowerShell output
Comment: @mklement0 Thanks for pointing out. I forgot there was a wrapper. It was an old project. Answer updated.
Comment: How does this answer the question? It doesn't seem to be tailored to the question at all.
Comment: @PeterMortensen it shows a better approach. Facilitating the work with the output, replacing the raw string with objects. At least it is an alternative.
Answer: The behavior you're observing is because you're running PowerShell as a separate process with different input and output streams, and the output produced by your PowerShell script is not automatically displayed in the parent console window. When you run PowerShell in this way, you need to explicitly handle the redirection of output if you want to see it in the parent console.
Remove the line process.StartInfo.RedirectStandardOutput = false; from your code. This line tells the Process to not redirect the standard output.
Modify your Demo.ps1 script to use the Write-Output cmdlet instead of Write-Host.
Write-Output "Working Properly now !"

Modified code
using (Process process = new Process())
{
    process.StartInfo.FileName = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe";
    String cmd = "C:\\Users\\xyz\\AppData\\Local\\Temp\\2\\Demo.ps1";
    process.StartInfo.Arguments = cmd;
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardError = true;
    StringBuilder error = new StringBuilder();

    process.Start();

    while (true)
    {
        bool hasExited = process.HasExited;

        String errorStr = null;
        while ((errorStr = process.StandardError.ReadLine()) != null)
            error.AppendLine(errorStr);

        if (hasExited)
            break;

        Thread.Sleep(100);
    }

    if (process.ExitCode != 0 || error.Length > 0)
        throw new Exception(error.ToString());
}

Comment: Somehow this is not working. I think your explanation is correct though. Actually I want to login to another VM from this script. Now in logs it is showing logged in but on console I cannot interact with that VM. How can I open new terminal
Comment: @utkarsh Kendre: It looks like [ChatGPT](https://meta.stackoverflow.com/questions/421831/temporary-policy-chatgpt-is-banned) (or similar) output.


'sh: 1: Syntax error: Unterminated quoted string ' in Ruby launching a bash script

Question: I have the following line in my Rails app:
pid = Process.spawn("casperjs #{path_to_file} '#{params[:page][:url]}' '#{Rails.root}/tmp/' --ignore-ssl-errors=yes", :out => pipe_cmd_out, :err => pipe_cmd_out)

If params[:page][:url] contains a single quote, like for example this URL: http://www.degree33surfboards.com/surf-gear/ultimate-9'-epoxy-sand-.html
I get this error:
sh: 1: Syntax error: Unterminated quoted string

How can I avoid that to happen?
Answer: Use the argument-list form of Process.spawn instead of trying to quote things for the shell.
Process.spawn(
  "casperjs", path_to_file, params[:page][:url],
  "#{Rails.root}/tmp/", "--ignore-ssl-errors=yes",
  :out => pipe_cmd_out,
  :err => pipe_cmd_out
)


Reasonable hyperparameter range for Latent Dirichlet Allocation?

Question: What are good ranges for the hyperparameters $\alpha$ and $\beta$ (explained well here) in LDA?
I appreciate hyperparameter tuning always depends on the use case, data, content of documents etc., but is there any general rule or heuristic to choose these hyperparameters for LDA?
Additional Info
For extra info on my particular use case and data (although I'd like a generalizeable answer if possible):

29 documents with an average length of 5,177 words (after parsing). This number of documents is expected to grow to between 50-200.
3,500 unique words (after parsing and keeping the top 3,500 words by frequency)
155,309 total words (again, after parsing)
All documents are finance related, and more specifically investment outlook whitepapers. So there isn't a lot of "variety" between documents

This is quite a small dataset, but I think there's enough words and structure in each document to train an LDA model (if not, please let me know).
Answer: Choice of $\alpha$ and $\beta$ is indeed tricky, since it impacts the topic modeling results. The Gibbs sampling paper by Griffiths et al. gives some insight into this:

The value of $\beta$ thus affects the granularity of the model: a
  corpus of documents can be sensibly factorized into a set of topics at
  several different scales, and the particular scale assessed by the
  model will be set by $\beta$. With scientific documents, a large value
  of $\beta$ would lead the model to find a relatively small number of
  topics, perhaps at the level of scientific disciplines, whereas
  smaller values of $\beta$ will produce more topics that address
  specific areas of research.

Eventually for scientific documents, the authors chose the following hyper-parameters, $\beta=0.1$ and $\alpha=50/T$. But they had a corpus of around $28K$ documents and a vocabulary of $20K$ words, and they tried several different values of $T: [50, 100, 200, 300, 400, 500, 600, 1000]$.

Regarding your data. I have no experience with analyzing financial text data, but for the choice of
$\alpha$ and $\beta$, I would ask myself the following questions:

Given my word vocabulary, do I expect my resultant topics to be sparse? For most cases, this is true. Hence, typically the topic prior is chosen to be sparse with $\beta < 1$.
Given the topics, do I expect the distribution of topics in each document to be sparse? That is, each document only represents a few topics. If yes, then $\alpha < 1$.

Answering the above questions may not be straight-forward with limited knowledge of the data. Since you have limited data, I would choose multiple values of $\alpha$ and $\beta$ - ranging from sparse to non-sparse priors - and find which one suits the dataset by computing the perplexity over some hold-out data. To put it more concretely:

Choose $\alpha_m$ from $[0.05, 0.1, 0.5, 1, 5, 10]$
Choose $\beta_m$ from $[0.05, 0.1, 0.5, 1, 5, 10]$
Run topic modeling on training data, with $(\alpha_m, \beta_m)$ pair
Find model perplexity on hold-out test data
Choose the value of $\alpha_m$ and $\beta_m$ with the minimum perplexity

Resources:

Latent Dirichlet Allocation: Original paper talks about perplexity.
Finding Scientific topics: Paper discussed above.
Rethinking LDA: Why priors matter: Paper discussing how prior structure affects topic modeling results.
Comment: @killerT2333 Perplexity is a measure of log-likehood of the data. It tells you how well the model fits to the given data. You can read more about it in the original LDA paper. Seems like the gensim library has a [`log_perplexity`](https://radimrehurek.com/gensim/models/ldamodel.html#gensim.models.ldamodel.LdaModel.log_perplexity) method which you can use.
Comment: +1 detailed answer, thanks. How does one measure model perplexity - what is the perplexity? Is it possible to measure this using the gensim library for python?


How to authenticate a user when embedding Google Calendar in external website without making the calendar public

Question: I'm looking for the best way to authenticate a user and give him access to a Google calendar embedded in an external website; I cannot share the calendar with the world because it contains private events.
My solution is to use the same authentication for consuming Google APIs (please my code below); once the user logs in Google account (part of the authentication), and accept permissions, he is basically logged in to Google and he can see the (private) events in the Google Calendar.
Is there a better way to give the user access to the embedded Google calendar (without sharing the calendar with the world)?
My code:
<?php
require_once 'src/Google_Client.php';
require_once 'src/contrib/Google_CalendarService.php';
session_start();

$client = new Google_Client();
$client->setApplicationName("Calendar Project");

$client->setClientId('XXXX');
$client->setClientSecret('XXX');
$client->setRedirectUri('XXX');
$client->setDeveloperKey('XXX');
$cal = new Google_CalendarService($client);
if (isset($_GET['logout']))
{
  unset($_SESSION['token']);
}

if (isset($_GET['code']))
{
  $client->authenticate($_GET['code']);
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}

if (isset($_SESSION['token']))
{
  $client->setAccessToken($_SESSION['token']);
}

if ($client->getAccessToken()) {
    ?><?php
$_SESSION['token'] = $client->getAccessToken();
} else {
  $authUrl = $client->createAuthUrl();
  print "Access the calendar here";
}
?>

Thanks!
Answer: Yes... You will have to make it have shared access with few GMail accounts, so that they can login and view the calendar.. This option is available under Calendar settings.


How does a ice-hockey team's strategy differ playing 3-on-3 rather than 5-on-5?

Question: In 2015, the NHL Board of Governors approved having overtime periods played in a 3-on-3 format (as opposed to 5-on-5 during regulation), which has led to a noticeable scoring boost (due to the increase of shots and shooting percentage). Furthermore, the NHL All-Star game was reformatted into a series of 3-on-3 "mini games."
How does a team's strategy differ playing 3-on-3 rather than 5-on-5? There is an increase in scoring, but the goalie has to adjust to more shots being taken, there isn't much of an opportunity to create traffic in front of the net, and with less players, their endurance becomes more of a factor.
Answer: 3 on 3 is still fairly new to the NHL, so I would imagine strategy is still well in development - teams have played tens of minutes at most of 3 on 3 outside of practice.
However, from the rest of the world, 3 on 3 is fairly common in lower level games, and some of the strategy there clearly makes it to the NHL game.  Most of the differences arise from the larger spaces on the ice, and higher relevance of each individual player.

Triangle formation on defense: This is the same as the 5 on 3 defense, really.  One guy between each faceoff circle and the goal, and then the third 'point' around mid-ice in your zone.  Cuts off passing lanes very effectively.
Triangle formation on offense: Basically the same as the defense's triangle.  One guy back, two guys forward.  When a shot happens, whomever is on the opposite side needs to crash the net hard when that occurs to get the rebound.
One defender and two forwards: This is somewhat of a result of the triangle formation, but it's a good general approach even if you use a different formation.  One player needs to play closer to the blue line, behind any of the defending team's players, to avoid breakaways.  Breakaways will happen constantly in 3 on 3, so that player needs to be fast - and maybe not a "D" man (someone like Jonathan Toews for example is a great choice - fast, hard skater, big presence to prevent breakaways).
Forechecking can be even more important.  When there are only three defenders, taking one of them out during the rush can be critical - even a few seconds of 3 on 2 can give you a score.  It also can be critical to prevent breakaways.
Frequent line changes are a must, and getting clean changes off.  This keeps your players fresh, and avoids bad breakaways.

These are a combination of my limited experience watching 3 on 3 NHL hockey, and reading some articles on amateur 3 on 3 strategies.  Some of those don't apply to the NHL (smaller ice, for example, is of course off the table), but there is interesting information there nonetheless.  This article is the one I most used to source this.
Comment: +1 1) Thanks for going beyond the NHL. 2) What would you consider "lower level" games? I watch NHL occasionally (albeit not recently enough to catch this 3-on-3 phenomenon), but I have been to a handful of other hockey games (AHL and NAHL) and am looking to attend a collegiate game sometime this season.
Comment: By lower level I mostly mean semi-pro or amateur.


Converting Crystal to SSRS: How to use "if" in where clause?

Question: I am manually converting an existing Crystal Report to SSRS.  In the CR selection formula, part of the formula includes the following:
(if {?SubStatus}="O" then {SLHD.Status}<2 else
if {?SubStatus}="C" then {SLHD.Status}=2 else 1=1)

NOTE:  ?SubStatus is a parameter.
How do I code this in an SQL WHERE clause so that I can use it in SSRS?  Thanks.
Answer: If I recall correctly, this is where the Crystal report will filter the data for SLHD.Status < 2 when the SubStatus parameter is O but if the SubStatus Parameter is C then the SLHD.Status must be 2.
I think it would actually be easier to use a CASE statement in this case. The CASE is similar to an IF but with multiple possibilities without nesting IFs.
WHERE CASE WHEN @SubStatus NOT IN ('O', 'C') THEN 1
           WHEN @SubStatus = 'O' AND SLHD.Status < 2 THEN 1
           WHEN @SubStatus = 'C' AND SLHD.Status = 2 THEN 1
           ELSE 0 END = 1


Parse through a string php and replace substrings

Question: I have a string, in PHP and the string has occurrences of the pattern %%abc%%(some substring)%%xyz%%
There are multiple occurrences of such substrings within the master string.
Each of these occurrences need to be replaced with a string from within an array
array('substring1','substring2','substring3','substring4') depending upon the response of a function() which returns back a integer between 1 to 4.
I am not able to figure out an efficient way to do this.
Answer: This is a situation that calls for preg_replace_callback:
// Assume this already exists
function mapSubstringToInteger($str) {
    return (strlen($str) % 4) + 1;
}

// So you can now write this:
$pattern = '/%%abc%%(.*?)%%xyz%%/';
$replacements = array('r1', 'r2', 'r3', 'r4');
$callback = function($matches) use ($replacements) {
    return $replacements[mapSubstringToInteger($matches[1])];
};

preg_replace_callback($pattern, $callback, $input);

Comment: Note that $pattern is a regex string, so if you change $pattern make sure you escape any regex characters
Answer: Use preg_replace_callback(), like this:
preg_replace_callback( '#%%abc%%(.*?)%%xyz%%#', function( $match) {
    // Do some logic (with $match) to determine what to replace it with
    return 'replacement';
}, $master_string);


average marginal effect AME vs. average partial effect APE

Question: Say I have a binary logistic regression model:
model1 : logit = b0 + b1 *age + b2 *gender_f + b3 *race_f
with age being continuous and race_f and gender_f being two-level factors. The margins function in R (or equivalently the margins command in Stata) can be used to estimate AME's for the three IV's. There also exists a so called APE, which for "age" can be obtained, following Mood (Logistic regression: why we cannot do what we think we can do, and what we can do about it, 2009), by averaging the marginal effects of age, dydx_age = b1 * p * (1-p), across the distribution of other variables at different given values of age; here p denotes the predicted probability of a person.
I'm stuck with how to calculate this APE measure. Roughly, I can think of two ways.

Select all people with age=20 in my dataset, and calculate the average dydx_age value for this subset.

With "margins", the "at" option can be used, as in R's:  margins(model1, at=list(age=20)). Stata has a similar option. This at= option actually constructs a new dataset, equal to the original data, except for age=20 now for ALL respondents in the new dataset. Then, for each person in this new dataset p is calculated (based on age=20, and gender and race as observed) and then the average of all dydx_age values is obtained. This average is another way I can think of, to calculate the APE. I would never have considered this way of calculating APE, had I not read about at= in T. Leeker's manual of margins and in R. Williams's paper on Stata's margins.

Not many texts could I find in which AME and APE are clearly contrasted, only the one mentioned by Carina Mood. Wooldridge discusses APE but reading this text is not simple, at least for me. On some internet pages the terms AME and APE seem to be used as synonyms, improperly given Mood's explanation.
As to the at= option, more in general, I would like to find an explanation of when to use this instead of working with subgroups. Reading the many examples about using margins in Stata or R, it seems so "obvious" to use at=, that the subgroups options seems " wrong" anyway. Is this indeed wrong, or "stupid"? The fictitious data, as generated by at=, seems "wrong" to, in the sense of not leading to a random sample of 20 year old people from the population.
Thanks for any help or reference!!
After receiving a response from Tom, about an explanation of what an average partial effect is by Kjetil B. Halvorsen, see:
What are average partial effects?
I added the following:
I happened to read this explanation just before posting my question. As you will have noticed, the two terms are used as synonyms there. In https://cran.r-project.org/web/packages/margins/vignettes/Introduction.html the two terms are also used but with a different meaning: AME refers (in case of logit link) to the linear predictor scale, whereas APE is reserved for the response scale. This is quite different from the way AME is mostly used (e.g. in R. Williams's Stata paper) namely to express change on the response scale.
But apart from this, the explanation you pointed to is much in line with Wooldridge's text, as far as I can see. Also, it seems to suggest (but correct me please if I'm wrong) that calculating the average partial effect APE boils down to taking the average of the derivative (dydx_age in R's margins terms) over the SUBGROUP in the original sample with age=20, or age=21, 22 etc. Or to put it differently: APE is AME calculated over a subgroup. In Stata this would mean using option "over=" And this is definitely NOT the same as using at=.
In Richard Williams's text, "at" is used as the more or less "obvious" way to go, if you want to compare the effect of e.g. race across different ages. But now I believe that this is less obvious than I first thought. In case of an experiment and a completely balanced dataset, I guess the two methods of calculating APE for age=20 would lead to the same estimate (though probably different std. errors). But given that the data in Richard Williams's examples are observational (I think) wouldn't it be at least equally plausible to use method 1 described above?
B.t.w., using margins in R would not produce a std. error for the average dydx_age at age=20 obtained for the subgroup method; only for the at=list(age=20) method, there would be a std. error. I'm not sure if Stata produces a std. error for a subgroup when using over=.
Further, the cplot(.., what="effect") function in R produces at= like results. As far as I can see now, it would be nice if cplot could also generate subgroup results (which of course can easily be generated, knowing that the dydx_age etc. columns are available in a data frame, made by margins).
Comment: With regard to your reaction to my comment, which is now posted here as an answer: https://stats.stackexchange.com/questions/12313/what-are-average-partial-effects. I have the feeling that you are mixing up a couple of things (but it is hard for me to exactly figure out what). If many people do not make a clear distinction between AME and APE (and even Wooldridge does not), you have to clearer explain what you want and what you are aiming for. I think it is also more fruitful to think about what you want to achieve instead what name you want to give to what you are doing.
Comment: Have you checked if any other questions on CrossValidated tackle this topic? https://stats.stackexchange.com/questions/12313/what-are-average-partial-effects
Comment: Does this answer your question? [What are average partial effects?](https://stats.stackexchange.com/questions/12313/what-are-average-partial-effects)
Comment: Sorry Tom, I didn't notice I was typing at the wrong place. You are right that what I want to achieve should be most relevant. The problem is that I only recently started looking at marginal effects as a useful tool in case of logistic regression. And then I ran into this problem of not knowing which of the two average effects to use in which situation. Hence my questions. Thanks for taking the time to answer!!
Answer: This is probably unsettled terminology, especially once you start crossing disciplinary boundaries, but according to Baby Wooldridge (7e), APE and AME are synonyms:

There are at least three calculations that you can do that correspond to different thought experiments about what happens to $\Pr(Y=1 \vert X, age)$ when age:

goes from observed $age$ to $age+1$ (most common)
goes from $20$ to $21$ (i.e., pretend everyone is 20 in your data, less common)
when $age$ goes from $age$ to $age+1$ for all the folks who are 20 in your data (fairly uncommon, equivalent to (2), but for a subset of the data)

The only difference between (1) and (3) is the sample over which the average is computed. The latter is occasionally interesting, but usually, you would do something like the effect of age for women rather than the effect of age for 20-years-olds.
(2) used to be common, but typically you would also set the other covariates to some interesting value (like means, medians, modes, or something like an 18-year-old male HS graduate). You can think of this as the effect of age for an "interesting" or "representative" person.
It's hard to recommend which one to use since we don't know what you have in mind. However, (1) is probably a reasonable default.
I am using effect here, but these are associations or correlations.
Comment: Thanks Dimitriy for your comment, things are getting a bit more clear for me. Best regards, Ben.


Angular js: Display an iframe in a pop up and see an empty frame

Question: I am building an angularJs app.
When i am clicking on a photo i would like that a modal screen will be shown with a player for playing a YouTube clip.
When i type it hardcoded everything running fine.
However when i get it from the server the iframe is empty.
In the controller

$scope.openVideoPlayerPopup = function (videoUrl) {
        var modalInstance = $modal.open({
            templateUrl: 'common/partials/videoPlayerModalScreen.html',
            controller: 'VideoPlayerModalCtrl',
            resolve: {
                url: function () {
                  return videoUrl;
                }
            }
        });

        modalInstance.result.then(function () {

        }, function () {
            console.log('Modal dismissed at: ' + new Date());
        });
    };

VideoPlayerModalController:
elbitApp.controller('VideoPlayerModalCtrl', ['$scope', '$modalInstance', 'url',
                                     function ($scope, $modalInstance,url) {

    $scope.givenUrl =  url;

    $scope.ok = function () {
        $modalInstance.close();
    };

    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
}]);

videoPlayerModalScreen.html:

Video Player

    <button class="btn btn-primary" ng-click="ok()">OK</button>
    <button class="btn btn-warning" ng-click="cancel()">Cancel</button>

If we put instead{{givenurl}},
ng-src="https://www.youtube.com/embed/3O1_3zBUKM8"
It will run fine...
What am i doing wrong?
Comment: Are you using dependency injection to pass in the url? Could you maybe make it into a plnkr ?
Comment: http://jsfiddle.net/Hx7B9/2/ something similar...
Comment: Yes this is somewhat similar. To find a solution for your problem it would be nice though if you could reproduce your problem with a jsfiddle ...
Answer: I found the problem.
I am using iframe. Angular requires to use $sce when we are using iframe.
The solution:
app.controller('VideoPlayerModalCtrl', ['$scope', '$modalInstance', '$sce', 'url',
                                     function ($scope, $modalInstance, $sce, url) {

    $scope.givenUrl =  $sce.trustAsResourceUrl(url);

    $scope.ok = function () {
        $modalInstance.close();
    };

    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
}]);

Instead of $scope.givenUrl = url replace it in $scope.givenUrl = $sce.trustAsResourceUrl(url).
Don't forget to add $sce as a dependency injection.
Comment: Nice to see angular handles security issues :)
Comment: @Aviade, can you add a jsfiddle for the above solution
Answer: I have also ran into this recently. Here is my solution using a filter:
Filter:
.filter('trustAsResourceUrl', ['$sce', function($sce) {
    return function(val) {
        return $sce.trustAsResourceUrl(val);
    };
}]);

In the HTML:


FCM MessagingOptions restrictedPackageName implementation

Question: We have 2 Android apps in one Firebase project container, the first app has FCM implementation on Cloud Function and it works well, but the second app will received this notification as well so using restricted_package_name in messaging option is what we are looking for. However the question is, do we need to update the app as well or just the function in Cloud Function? Thus everything related to filtering of push notif. will be handled by its API and all we need is to provide the package name for the target app.
According to documentation of restrictedPackageName

The package name of the application which the registration tokens must match in order to receive the message.

Do we need to handle token as well? Do we need to update the code in the app or just add the option in the function with NodeJs?
    const options = { priority: 'high' };

exports.announcement = functions.firestore.document("Announcement/{doc_id}").onCreate((added) => {

    const title = added.data().title;
    const description = added.data().description;
    const bigImage = added.data().bigImage;
    const link = added.data().link;
    const environment = added.data().environment;

    const payload = {
        data: {
            title: title,
            description: description,
            bigImage: bigImage,
            link: link,
            environment: environment,
        }
    };

    return admin.messaging().sendToTopic("Announcement", payload, options)
        .then(val => {
            console.log("Success!\n" + JSON.stringify(payload), val);
            return true;
        })
        .catch(error => {
            console.error("Failed to send notification.", error);
            return true;
        });

});

Comment: Can you please provide the cloud function which you are using and also if possible, part of the code where you implemented ?
Answer: From Firebase side it is not recommended to use same project for 2 different applications, which was also mentioned in this blog. If you want to use restricted_package_name you need to update the code so that the object sends it in notification. You can find a small example here.
Comment: @WytrzymałyWiktor good day first, the question is about Android not a NodeJs app so the sample is not relevant to the question. Second I never read any word "not recommended" on the given blog, neither in any Firebase article and SO question I read so far, I forgot to mention that these apps is the same and all we want to know is whether we need to change only the Cloud Functions or the app code as well. Third you revised the question which is totally not readable and can result to more confusion.


How to detect Social Network and Redirect to PHP to generate Metatags

Question: I have a site that is hosted at
example: http://lab.example.com/app
When a social network bot such as facebook/twitter tries too scrape this site, I need it to redirect to a php file which generates the correct meta tags.  I tried to modify the HTACCESS in several ways, but after several research, I can't get the Facebook/Twitter debugger to read it.
.HTACCESS
RewriteEngine On
RewriteBase /

RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . app/index.html [L]

# allow social media crawlers to work by redirecting them to a server-rendered static version on the page
RewriteCond %{HTTP_USER_AGENT} (facebookexternalhit/[0-9]|Twitterbot|Pinterest|Google.*snippet)
RewriteRule album/(\d*)$ http://lab.example.com/app/opengraph.php [P]

OPENGRAPH.PHP  TEST
<?php

makePage();

function makePage() {
    ?>
    <!DOCTYPE html>

            <meta property="og:title" content="Title Test" />
            <meta property="og:description" content="Description Test" />
            <meta property="og:image" content="http://68.media.tumblr.com/784fb357523d75590261ba6c5c19e6e7/tumblr_o8punbn6Qo1st5lhmo1_1280.jpg" />

    <?php
}

?>

Hoping someone experienced with HTACCESS can help me out
Comment: Generate those meta tags for every request, safest way. You can't predict which user-agent the existing and future bots will use.
Comment: And Google really, really, **really** frowns on people showing something different to their bot than to regular users.
Comment: You should be including the OG Metatags on __every__ pagwe, regardless of who it is requesting it.
Comment: What do you mean to generate the meta tags for every request? I am trying to avoid using any prerender services at the moment - Since Facebook/Twitter won't read metatags generated by javascript after page load, it's why i'm trying to go this route.  I'll probably remove Google anyways, since Google works better with Single Page Applications.
Comment: The OG Metatags will be on every page on Index.html, it is just I need it to be different when facebook/twitter tries to scrape it.
Answer: To provide an answer, not what you asked, but what you are trying to achieve.
"The OG Metatags will be on every page on Index.html, it is just I need it to be different when facebook/twitter tries to scrape it."
Twitter has its own set of og tags ..
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="">
<meta name="twitter:description" content="">
<meta name="twitter:image" content="">

As for facebook, all you need to do, is detect the useragent.
<?php
if(strpos($_SERVER['HTTP_USER_AGENT'],'facebookexternalhit')!==false){
 //Do Facebook Og
}
else{
 // Do basic Og
}
?>


C Program returns incorrect result

Question: I'm making a program to calculate flowrate but everytime you enter in the variables when the program is excecuted, it returns the wrong answer. I got the formula for calculating flow rate from this website "http://www.flowmeterdirectory.com/flowcalculator.php", i'm using it as a a reference to check my program's results but they never work.
#include <stdio.h>
#include <math.h>
#include "PI.h"

int flowRateFormula(int,int);   //Finally, finds out the flow rate
int square(int);

int square(int x)
{
    x=pow(x,2);

    return x;
}

int flowRateFormula(int pipeDiameter,int velocity)
{
    int integer3=0.25*(PI*square(pipeDiameter)*velocity);

    return integer3;
}

int main()
{
    int flowRate;
    int velocity;
    int pipeDiameter;

    printf("Enter velocity of liquid(in./s):");
    scanf("%d",&velocity);
    printf("Velocity=%d in/s.\n",velocity);

    printf("Enter pipe diameter(inches):");
    scanf("%d",&pipeDiameter);
    printf("Pipe Diameter=%d inches.\n",pipeDiameter);

    printf("Applying formula for Flow Rate.........\n");
    flowRate=flowRateFormula(pipeDiameter,velocity);

    printf("Pipe Diameter=%d inches.\n",pipeDiameter);

    printf("Velocity of liquid=%d in/s.\n",velocity);

    printf("Thus, the flow rate=%d ft/s.\n",flowRate);

    return 0;
}

Comment: Ah, got it. All of your other questions have had the "homework" tag, so you're trying to get us to do your work for you without anyone knowing it's for school. Nice try - didn't work. :)
Comment: Why, oh why, do people use `pow(x,2)` to square a number?
Comment: ...throw it in a debugger and see what happens?
Comment: Why, oh why, do people use the Homework tag lol. Man thats classic...
Answer: It's most likely because of the integer cast. Depending on your inputs, the results can be close and appear to have the same result because of rounding during the integer cast. Use double in your function instead.
Answer: You're going to get a lot of error due to the fact that you're using int everywhere. float or double would be a better choice. This alone could explain why you're getting the same results despite entering different values. For example, int flow=0.25*x would give the same value for flow for all x where x > -4 and x < 4.
Answer: To get accurate result, you need float or double type.
Please try this:
#include <stdio.h>
#include <math.h>

double PI = 3.14;

double flowRateFormula(float, float);

double flowRateFormula(float pipeDiameter, float velocity)
{
    double p = pow(pipeDiameter, 2);
    double result = 0.25 * PI * p * velocity;
    return result;
}

int main()
{
    double flowRate;
    float velocity;
    float pipeDiameter;

    printf("Enter velocity of liquid(in./s):");
    scanf("%f",&velocity);
    printf("Velocity=%f in/s.\n",velocity);

    printf("Enter pipe diameter(inches):");
    scanf("%f",&pipeDiameter);
    printf("Pipe Diameter=%f inches.\n",pipeDiameter);

    printf("Applying formula for Flow Rate.........\n");
    flowRate=flowRateFormula(pipeDiameter,velocity);

    printf("Pipe Diameter=%f inches.\n",pipeDiameter);
    printf("Velocity of liquid=%f in/s.\n",velocity);
    printf("Thus, the flow rate=%f ft/s.\n",flowRate);

    return 0;
}

Output:
$ ./a.out
Enter velocity of liquid(in./s):10
Velocity=10.000000 in/s.
Enter pipe diameter(inches):20
Pipe Diameter=20.000000 inches.
Applying formula for Flow Rate.........
Pipe Diameter=20.000000 inches.
Velocity of liquid=10.000000 in/s.
Thus, the flow rate=3140.000000 ft/s.
$
$
$
$
$ ./a.out
Enter velocity of liquid(in./s):12.34
Velocity=12.340000 in/s.
Enter pipe diameter(inches):435.345
Pipe Diameter=435.345001 inches.
Applying formula for Flow Rate.........
Pipe Diameter=435.345001 inches.
Velocity of liquid=12.340000 in/s.
Thus, the flow rate=1835912.361516 ft/s.
$

Hope this helps!
Answer: You are doing floating point arithmetic and returning the result to int.  I would change variables to floats or doubles were needed.

C Program returns incorrect result