Download file using WorkManager

Question: I am working/trying with download a file with pause and resume functionality using WorkManager with MVVM.
Here I am seeking pause/resume and download percentage progress update with use of WorkManager. So I am sharing my classes here.
MainActivity.kt
class MainActivity : AppCompatActivity() {

    lateinit var downloadViewModel : DownloaderViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //setContentView(R.layout.activity_main)
        val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this,R.layout.activity_main)
        downloadViewModel = ViewModelProviders.of(this).get(DownloaderViewModel::class.java)
        binding.viewmodel = downloadViewModel
        download_button.setOnClickListener({
                startDownload()
        })

        cancel_button.setOnClickListener({
            downloadViewModel.cancelDownloadWork(Constants.TAG_OUTPUT)
            WorkUtils.deleteFile(WorkUtils.getIsbn(Constants.TAG_OUTPUT))
        })

        downloadViewModel.mSavedWorkStatus.observe(this, Observer {

            it?.let {

            }
            if(it?.size!! > 0){
                val workStatus = it.first()
                val workState =  workStatus?.state
                downloadViewModel.updateDownloadWorkState(workState.toString())
                WorkUtils.makeStatusNotification(workState.toString(),this.applicationContext)
            }
        })

        pause_button.setOnClickListener({
            downloadViewModel.cancelDownloadWork(Constants.TAG_OUTPUT)
            val pausedAt = WorkUtils.getFileSize(WorkUtils.getIsbn(DOWNLOAD_URL))
            Log.d("paused at","paused at $pausedAt")
        })

        resume_button.setOnClickListener({
            val resumeFrom = WorkUtils.getFileSize(WorkUtils.getIsbn(DOWNLOAD_URL))
            Log.d("paused at","resumed at ${resumeFrom+1}")
            startDownload()
        })

    }

    fun startDownload() {
        downloadViewModel.downloadUrl =  DOWNLOAD_URL
        downloadViewModel.makeDownloadRequest()
    }

    fun isFileExists(url : String) : Boolean {

        return File(WorkUtils.getAbsolutePath(WorkUtils.getIsbn(url))).exists()

    }
}

Constants.kt
class Constants {
    companion object {

        val DOWNLOAD_URL = "download_url"
        // Notification Channel constants

        // Name of Notification Channel for verbose notifications of background work
        val VERBOSE_NOTIFICATION_CHANNEL_NAME: CharSequence = "Verbose WorkManager Notifications"
        var VERBOSE_NOTIFICATION_CHANNEL_DESCRIPTION = "Shows notifications whenever work starts"
        val NOTIFICATION_TITLE: CharSequence = "WorkRequest Starting"
        val CHANNEL_ID = "VERBOSE_NOTIFICATION"
        val NOTIFICATION_ID = 1

        // The name of the image manipulation work
        internal val IMAGE_MANIPULATION_WORK_NAME = "image_manipulation_work"

        // Other keys
        val OUTPUT_PATH = "blur_filter_outputs"
        val KEY_DOWNLOAD_URL = "KEY_DOWNLOAD_URL"
        internal val TAG_OUTPUT = "OUTPUT"

        val DELAY_TIME_MILLIS: Long = 3000

        // Ensures this class is never instantiated
        private fun Constants() {}
    }
}

DownloaderViewModel.kt
To make download request and notify the UI using binding. Cancelling the download(work) using WorkManager instance.
class DownloaderViewModel : ObservableViewModel() {
    var  mWorkManager : WorkManager = WorkManager.getInstance()

    var   mSavedWorkStatus: LiveData<List<WorkStatus>>
    @Bindable
    var currentDownloadState : MutableLiveData<String> = MutableLiveData()
    init {
        mSavedWorkStatus = mWorkManager.getStatusesByTagLiveData(Constants.TAG_OUTPUT)
    }

    lateinit var  downloadUrl: String
    /**
     * Creates the input data bundle which includes the Uri to operate on
     * @return Data which contains the Image Uri as a String
     */
    private fun createInputData(): Data {
        val builder = Data.Builder()
        if (downloadUrl != null) {
            builder.putString(KEY_DOWNLOAD_URL, downloadUrl)
        }
        return builder.build()
    }

    fun makeDownloadRequest() {

        // Create charging constraint
        val constraints = Constraints.Builder()
                .setRequiredNetworkType(NetworkType.CONNECTED)
                .setRequiresCharging(true)
                .build()

        // Add WorkRequest to download the epub to the filesystem
        val save = OneTimeWorkRequest.Builder(DownloadWorker::class.java)
                .setConstraints(constraints)
                .setInputData(createInputData())
                .addTag(TAG_OUTPUT)
                .build()
        mWorkManager.enqueue(save)
    }

    fun cancelDownloadWork(tag : String) {
        mWorkManager.cancelAllWorkByTag(tag)
    }

    fun getDownloadWorkState()  {
        getOutputStatus()
    }

    fun updateDownloadWorkState(state: String){
        currentDownloadState.value = state
        notifyPropertyChanged(BR.currentDownloadState)
    }

    internal fun getOutputStatus(): LiveData<List<WorkStatus>> {
        return mWorkManager.getStatusesByTagLiveData(TAG_OUTPUT)
    }
}

Worker class : DownloadWorker.kt
This class used to start the download work . As the server supports range request, every OneTimeWorkRequest started with this work range to download the file.
class DownloadWorker(context : Context,workerParameters: WorkerParameters) : Worker(context,workerParameters) {
    override fun doWork(): Result {

        val applicationContext = applicationContext

        // Makes a notification when the work starts and slows down the work so that it's easier to
        // see each WorkRequest start, even on emulated devices

        lateinit var result : Result
        val inputData : String? = inputData.getString(KEY_DOWNLOAD_URL)
        inputData?.let {
             result =  downloadFile(inputData)

        }
        return result
    }

    fun downloadFile(url : String) : Result {
        var input: InputStream? = null
        var output: OutputStream? = null
        var connection: HttpURLConnection? = null
        lateinit var result : Result

        val isbnFileName = WorkUtils.getIsbn(url);
        val filePathToWrite = WorkUtils.getAbsolutePath(isbnFileName)

        //val path = "${Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).path}${File.separator}${"file.zip"}"
        Log.d("Download file path :","Download file path :"+filePathToWrite)
        val targetFile = File(filePathToWrite)
        if(!targetFile?.exists()){
            targetFile.createNewFile()
        }

        try {
            val urlConnection = URL(url)
            connection = urlConnection.openConnection() as HttpURLConnection
            val range = WorkUtils.getFileSize(isbnFileName)+1
            connection.setRequestProperty("Range","bytes=${range}-")
            Log.d("Range","Request header Range is $range")
            connection.connect()

            // expect HTTP 200 OK, so we don't mistakenly save error report
            // instead of the file
            if (connection.responseCode != HttpURLConnection.HTTP_OK) {
                Log.e("","Server returned HTTP ${connection.responseCode} ${connection.responseMessage}")
                result =  Result.FAILURE
            }

            // download the file

            input = connection.inputStream

            input?.let {
                output = FileOutputStream(targetFile, false)

                val data = ByteArray(1024 * 4)
                var count: Int

                do {
                    count = input.read(data)
                    if (count != 1) {
                        output!!.write(data, 0, count)

                    } else {
                        break
                    }

                } while (count != -1)
            }

            result = Result.SUCCESS

        } catch (e: Exception) {
            Log.e("Exception occured:",e.message)
            result =  Result.FAILURE
        } finally {
            try {
                output?.close()
                input?.close()
                connection?.disconnect()
            } catch (e: IOException) {
                Log.e("Exception occured:",e.message)
            }
        }
        return result
    }
}

Utilities used in this sample
Utils.kt
class WorkUtils {

    companion object {
        private val TAG = WorkUtils::class.java.getSimpleName()

    fun makeStatusNotification(message: String, context: Context) {

        // Make a channel if necessary
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // Create the NotificationChannel, but only on API 26+ because
            // the NotificationChannel class is new and not in the support library
            val name = Constants.VERBOSE_NOTIFICATION_CHANNEL_NAME
            val description = Constants.VERBOSE_NOTIFICATION_CHANNEL_DESCRIPTION
            val importance = NotificationManager.IMPORTANCE_HIGH
            val channel = NotificationChannel(Constants.CHANNEL_ID, name, importance)
            channel.description = description

            // Add the channel
            val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

            notificationManager?.createNotificationChannel(channel)
        }

        // Create the notification
        val builder = NotificationCompat.Builder(context, Constants.CHANNEL_ID)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(Constants.NOTIFICATION_TITLE)
                .setContentText(message)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setVibrate(LongArray(0))

        // Show the notification
        NotificationManagerCompat.from(context).notify(Constants.NOTIFICATION_ID, builder.build())
    }

        /**
         * Method for sleeping for a fixed about of time to emulate slower work
         */
        fun sleep() {
            try {
                Thread.sleep(Constants.DELAY_TIME_MILLIS, 0)
            } catch (e: InterruptedException) {
                Log.d(TAG, e.message)
            }

        }

        fun getIsbn(url: String) : String {
            return url.substring(url.lastIndexOf("/")+1)
        }

        fun getAbsolutePath(isbnFileName: String): String {
            return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString()+"/"+isbnFileName+".zip";
        }

        fun getFileSize(isbn:String) : Long {

            val file: File = File(getAbsolutePath(isbn))
            if(!file?.exists())
            {
                return 0
            }else{
                return file?.length()
            }
        }

        fun deleteFile(isbn: String) {
            val file = File(getAbsolutePath(isbn))
            if(file.exists() && file.isFile) {
                file.delete()
            }
        }

    }
}

For the UI part with databinding
<?xml version="1.0" encoding="utf-8"?>
<layout
    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"
    >
    <data>

        <import type="com.pkonf.downloadebook.viewmodels.DownloaderViewModel" />

        <variable
            name="viewmodel"
            type="com.pkonf.downloadebook.viewmodels.DownloaderViewModel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

        <TextView
            android:id="@+id/download_status"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:text="@{viewmodel.currentDownloadState}"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="368dp"
            android:layout_height="50dp"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:orientation="horizontal"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">

            <Button
                android:id="@+id/pause_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="pause_button"
                tools:text="Pause" />

            <Button
                android:id="@+id/resume_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="resume_button"
                tools:text="Resume" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="377dp"
            android:layout_height="157dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="28dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:gravity="center_horizontal|center_vertical"
            android:orientation="vertical"
            app:layout_constraintBottom_toTopOf="@+id/linearLayout"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/download_status"
            app:layout_constraintVertical_bias="0.95">

            <Button
                android:id="@+id/download_button"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="-1dp"
                android:text="Download"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="@id/linearLayout2"
                app:layout_constraintTop_toTopOf="@+id/linearLayout2" />

            <Button
                android:id="@+id/cancel_button"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginBottom="8dp"
                android:text="Cancel"
                app:layout_constraintBottom_toBottomOf="@+id/linearLayout2"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/download_button" />

        </LinearLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

I have tried with - starting the download - pausing the download (here pausing means cancelling the current OneTimeWorkRequest) and resuming by creating new OneTimeWorkRequest and start/resume with downloaded file length+1 as bytes range in request header.
But the cancel is not working in my case. Don't know what I am doing wrong.
And my questions are,
1. Can we do pause and resume download using WorkManager?
2. Can we update progress bar using WorkManager?
3. Ref : link is this is the only way to update the progress bar?
Answer: Try an object lock like this:
private Object lock = new Object();
new Thread() {
        @Override
        public void run() {
            synchronized (lock) {
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            callApi();
        }
    }.start();

The task will hang forever until user click resume button:
 synchronized (lock) {
                    lock.notify(); // Will wake up lock.wait()
                }

Comment: for this case, you might wanna to check this answer instead: https://stackoverflow.com/questions/50403013/asynchronous-worker-in-android-workmanager
Answer: You can do this, override the onStopped() method of DownloadWorker class and then execute, connection.disconnect()
Something like this:
class DownloadWorker(context : Context,workerParameters: WorkerParameters) : Worker(context,workerParameters) {
    private val connection: HttpUrlConnection? = null

    override fun doWork(): Result {
        connection = ...

        ...
    }

    override fun onStopped() {
        super.onStopped()
        connection.disconnect()
    }
}

Download file using WorkManager


Problem at proof of Cartan's theorem about the relation between metric and curvature in do Carmo's book

Question: I'm reading DoCarmo's book, Riemannian Geometry and i don't understand something. At page 157, Cartan's theorem.
My question is, why can we take a Jacobi field $J$ in such a way that $J(0)=0$ and $J(l)=v?$
Comment: Any time you have a second-order differential equation on an interval, you can *either* specify the value and derivative at a single point *or* specify the values at two different points of the domain. We're just used to doing the former all the time :)
Comment: @ Ted Shifrin: What you are saying is not true. Just look at the Jacobifields on $S^2$ when they reach the antipodal point.
Comment: Ted's claim needs some qualifying to become a theorem, but I'd say it's morally/generically true. In the case of the Jacobi equation, linearity tells us that it's true so long as the points $p,q$ are not conjugate, which is the case here since $V \ni q$ is a normal neighbourhood of $p.$
Comment: @TedShifrin ohh I didn't know that. I know that I can specify the value and the derivative at a point but i didn't know the second method. Can you give me some reference on this topic? something that i could read about? Thanks
Answer: A Jacobi field $J$ with initial conditions $J(0)=0$ and $J'(0)=w$ along the geodesic $\gamma(t)=exp_p(tv)$ can explicitly be written as
\begin{equation}
J(t)=(d exp_p)_{tv}(tw).
\end{equation}
For small enough $t$ the differential of the exponential map at $tv$ is bijective. So for any $\nu \in T_{\gamma(t)}M$ there is some $u \in T_pM$ with
\begin{equation}
(d exp_p)_{tv}(u)=\nu.
\end{equation}
Then the Jacobi field with initial condition $J(0)=0$ and $J'(0)=\frac{u}{t}$ is the one you're looking for.


want to get a row from cursor

Question: I want to get a row and this row have 2 columns which named username and password my code is here:
String selection = PROFILE_COLUMN_USERNAME + " = '" + userName+ "' AND " +PROFILE_COLUMN_PASSWORD + " = '" + password + "'";
Cursor cursor = database.query(TABLE_NAME, new String[] {PROFILE_COLUMN_USERNAME, PROFILE_COLUMN_PASSWORD }, selection,null, null, null, null);

I got a sample data like username = erdem and password= c on my db but when i write this sample and write to get username  like this:
String s =cursor.getString(cursor.getColumnIndex(PROFILE_COLUMN_USERNAME));

it doesn't work. Why?
Answer: Use moveToFirst, read the documentation.
Answer: Try something like below : Here "path_on_server" is the name of the column in the DB table whose value is being extracted. You can change it to your table's column name.
        String query = "some query";
        Cursor c = newDB.rawQuery(query, null);

        if (c != null) {
            if (c.moveToFirst()) {
                do {
                    mPathOnServer = c.getString(c
                            .getColumnIndex("path_on_server"));
                } while (c.moveToNext());
            }
        }
    } catch (SQLiteException se) {
        Log.e(getClass().getSimpleName(),
                "Could not extract information from DB");
    }

Comment: A little tip: the two `if`s are not necessary just use `if( c != null && c.moveToFirst() ){ ... }`, if is c is null moveToFirst won't be called.
Comment: i m new abou android. Thanks for reply but mPathOnServer means?
Comment: Its just the name of a variable wherein I am storing the value of a column from one of the tables in my app's database..
Comment: if PROFILE_COLUMN_USERNAME is your actual column name in the table put that in double quotes when you do the getColumnIndex
Comment: it worked thanks. i found my mistake. want to rate your answer but my reputation doesn't allow this:)
Comment: Just accept it as answer... Use the tick symbol across my answer
Answer: Try this way...
Cursor cursor = null;

try {
    String queryString = "SELECT * FROM Table_Name";

    cursor = myDatabase.rawQuery(queryString, null);
            if (cursor != null && cursor.moveToFirst()) {

                do {
                    String nextUser = new String(cursor.getString(cursor
                            .getColumnIndex("driver_fname")));

                } while (cursor.moveToNext());

            }
        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.deactivate();
                cursor.close();
                cursor = null;
            }
            if (myDatabase != null) {
                myDatabase.close();
            }
        }

Comment: i can list my data using SELECT * FROM Table_Name query but when i try to my query at question it doesnt work. may be a mistake at my query?

want to get a row from cursor


Polynomial restricts to zero on a hyperplane implies divisibility

Question: $F$ is a finite field and $p \in F[x_1, x_2, \ldots ,x_n]$ is a polynomial
If $p$ restricts to $0$ on a hyperplane $q = a_0 + a_1 x_1 + \cdots + a_n x_n = 0$ in $F^n$ then does it follow that $q$ divides $p$?
By 'restricts to $0$' I mean if we parameterise $q$ by $n-1$ paramteters $t_i$ and compute the restriction of $p$ to $q$ (i.e. a polynomial in $F[t_1, \ldots, t_{n-1}]$ where $t_i$ are our parameters) this restriction is the zero polynomial.
Comment: What does "being a polynomial on $\,\Bbb F^n\,$" mean?
Comment: @DonAntonio Sorry, I meant that it is a polynomial with $n$ variables and coefficient in $F$
Comment: Please edit your question, as then you meant $\,p\in\Bbb F[X_1,...,X_n]\,$
Comment: I edited it, hope it makes sense now =)
Answer: Suppose $n > 1$.
Consider the polynomial $p(x_1, \dots, x_n) = x_1^2 - x_{1}$ over the field $F = \mathbf{F}_{2}$. (So I might have written $-1 = 1$.)
Clearly $p$ evaluates to zero on any element of $F^{n}$. But $p$ is not divisible by, say, $q = x_2$.

The above was a solution to the problem as I understood it by reading a previous version, assuming that $p$ evaluates to zero on the hyperplane.
In the current formulation, I believe the answer to be positive.
Suppose WLOG $a_{n} \ne 0$, and do a change of variables so that the hyperplane is $q = x_{n} = 0$. (And the hyperplane is parametrized by $x_1, \dots, x_{n-1}$ in the obvious way.)
Now consider $p$ as a polynomial $p' \in K[x_{n}]$ in the variable $x_n$, with coefficients in $K = F[x_1, \dots, x_{n-1}]$.
If I understand correctly the assumption, this says that $p'(0) = 0$, that is, $p'$ has no constant term when regarded as a polynomial in $K[x_{n}]$, that is, it is a multiple of $q = x_{n}$, as required.
Comment: The hyperplane $q=x_2$ is parametrised by $(0,0) + t(1,0)$ so the restriction to $q$ is $p((0,0) + t(1,0)) = t^2 - t \neq 0$ i.e. is not the _zero polynomial_. I don't know if there is a better term for what I mean by "restricts to $0$" in the question?
Comment: I added to my question to explain what I meant by restricts to $0$
Comment: @DonAntonio, OP means zero as a polynomial, not just as a function.
Comment: @user65496, please check my edited answer and let me know whether it is ok.
Comment: @Andreas Yes this resolves my question, thank you for taking the time! I'm sorry for the confusing question, I did not think it was difficult but I'm not very familiar with the language


Bassoon line in Pink Floyd song

Question: I played bassoon all through high school and was lured away by rock and roll.  Someday I'd like to take it up again and probably will.
My question concerns the bassoon line in the Pink Floyd song, "Is There Anybody Out There?".  When the nylon string guitar starts its arpeggios, there is what I presume to be a bassoon playing a quarter note pattern that starts on an A and shoots up a couple of octaves.  It sound like an embouchure thing, but I'm not even sure if it's an actual bassoon or something else.  Like all other things completely unfair to bassoonists, the internet has been of no help so far!  Hopefully someone here will give a listen.
Thanks,
Trevcda
Answer: Welcome to Music P&T.
I'm hearing -

I'm not hearing it shoot up two octaves at all. There are violin harmonics up there but I don't think the bassoon goes so high. Did they re-record it? I'm listening to the 2011 remastered version.
Comment: That's ok for bassoon.  The bassoon solo that opens Stravinsky's 'Rite of Spring' starts on the C above that A, and even goes up to the D.   Every bassoonist considers ability to play it a necessary professional requirement, just as every clarinettist has to manage the opening glissando of 'Rhapsody in Blue'.  (But it's a synth.)
Comment: Yeah, it DOES sound like tenor clef on the recording, doesn't it?  :-)
Comment: @Laurence Payne I know. It's fine. But the OP said it shot up TWO octaves. I know the Rite of Spring very well indeed. I know the range of a bassoon and that bassonists need to be able to play it, scary though it is to start the whole kaboodle off. AND I know about the clarinet in Rhapsody in Blue. BUT if it's a synth it's a remarkably good one for its time.
Comment: That's what I hear but it sounds an octave up, the first note is a 6th above middle C.
Comment: How strange, John. I just can't hear that. I CAN hear it where I said. I've checked. And I can hear other instruments up there - violins and a synth - but not the bassoon. Is it the 2011 re-mastered version you're listening to, btw? I wonder if they got Brian Gulland to play it. We need a record-sleeve!
Comment: @Laurence Payne This was before yer Akais and yer Roland S-750s. I can't think of a synth that was up to it back then. And anyway if it's not a bassoon how come it's in the tenor clef?  ;-)
Comment: @OldBrixtonian I listened on iTunes, it doesn't mention if it's the remastered version but the line starts at 1:26, right? I compared it to an A above middle C on my keyboard.
Comment: I was listening to the album version.  It does start around 1:26 and is on the 2 and 3 of each measure in the first phrase only.  I believe Old Brix has got the location of what I'm talking about.  I do hear a violin riding the upper note, but I swear that's a bassoon making that jump.  A compressed music source maybe deleting some of that.  In any case, thanks for jumping in and giving it a listen.
Comment: It was good to hear that song.
Comment: There's a very characteristic synth portamento in that sound.  Don't think a bassoon would do that.
Comment: Oh do leave off!

Bassoon line in Pink Floyd song


Is there any reference justificating a P<0.10 as a cutoff for likelihood-ratio tests?

Question: I am conducting log-likelihood-ratio tests to examine the goodness of fit of two competing logistic regression models. The test is the traditional log-likelihood-ratio test, in which I compare the ratio of the two log-likelihoods to identify the "best" model between two nested models.
I have used 10%  as a cutoff (pre-specified threshold); when P < 0.10, there is evidence that the more complex model fits the data "better"  than the null model without the additional covariate. I haven't used a P <0.05 because my dataset is relatively small.
Is there any empirical evidence/recommendation that using a P<0.10 is a good thing when the statistical power is low? Any references/suggestions are remarkably welcome.
Comment: I am not sure the ratio of two log-likelihoods or two log-likelihood-ratios makes much sense.  The ratio of two likelihoods or two likelihood-ratios might, and so too might the difference of two log-likelihoods or two log-likelihood-ratios.  Perhaps I have misunderstood the question
Answer: No, there is no evidence that this is a good approach. Building models based on whether some significance test has $p \leq 0.05$ or $\leq 0.1$ associated with it is problematic. For a start, it invalidates naive post-selection inference (i.e. just using the "selected" model and treating coefficients and hypothesis test results from this model, as if it had been prespecified). It is also not a particularly good approach for building a good predictive model, either. The particular p-value threshold for this is kind of irrelevant for this question, but even the common usage of 0.05 is pretty arbitrary in the first place (and some have argued that often lower thresholds would be desirable).
Comment: You're implying that testing is a good idea.  Interactions usually need to be pre-specified and if you spend degrees of freedom on them you need to keep those terms in the model.  A better alternative is to use a Bayesian model and put prior information on interaction effects.  That way you don't need to think in a binary fashion and can have interactions "half in" and "half out" of the model.
Comment: Thank you. I am actually testing for interactions. Does the same apply?


Rules for cancelling fractions with exponents

Question: I have an expression that I need to simplify, I know the answer (wolframalpha) but I'm not sure of the rule that gets me there.
$\dfrac{(\alpha) X_1^{\alpha -1} X_2^{1-\alpha}}{(1-\alpha)X_1^\alpha X_2^{-\alpha}}$
Basically I know that on the $X_1$ side of the fraction it cancels down to $\frac{1}{x_1}$ and the $X_2$ side cancels to $\frac{x_2}{1}$ leaving me with the alphas still outside and $\frac{X_2}{X_1}$.
But I don't know the exponent rule that allows me to do this cancellation. I just want to be sure I understand it fully.
Thanks.
Edit: Thank you very much to both of you! You've really helped with an upcoming exam. This is my first time posting here, I will fill out my profile and hopefully I can help other people from here on in! Thanks again!
Comment: If I say that (X^a) (X^b) = X^(a+b) or (X^a) / (X^b) = (X^a) (X^(-b)) = X^(a-b), does this remember something to you ? If not, just post and I shall try something else.
Comment: @ClaudeLeibovici, post an answer and elaborate more.   StephenB looks like you received the answer you were looking for, please accept an answer
Comment: @StephenB. What I said is almost identical to what other said better in the answer. I was trying to find a way to refresh your memory with the simplest rules. If there is anything specific you would like me to clarify, just precise it. Cheers.
Answer: You are correct.
The following holds.

$$\frac{x^a}{x^b}=x^{a-b}.$$

In your example,
$$\frac{x_1^{\alpha-1}}{x_1^\alpha}=x_1^{(\alpha-1)-\alpha}=x_1^{-1}=\frac {1}{x_{1}}.$$
$$\frac{x_2^{1-\alpha}}{x_2^{-\alpha}}=x_2^{(1-\alpha)-(-\alpha)}=x_2^{1-\alpha+\alpha}=x_2^1=x_2.$$
Comment: I hope you don't mind if I edited it kindly. ;-)
Comment: No, I don't. It's better:)
Answer: $$X_2^{1-\alpha}=\frac{1}{X_2^{\alpha -1}}$$
also $$X_2^{-\alpha}=\frac{1}{X_2^\alpha}$$
also use the property $$x^ay^a=(xy)^a$$
you get
$$\frac{\alpha}{1-\alpha}\frac{(\frac{X_1}{X_2})^{\alpha-1}}{(\frac{X_1}{X_2})^\alpha}$$
Answer: I assume that $X_1\neq 0,~X_2\neq 0$ and $\alpha\neq 1$.
$$\frac{\alpha X_1^{\alpha -1} X_2^{1-\alpha}}{(1-\alpha)X_1^{\color{red}{\alpha}} X_2^{\color{blue}{-\alpha}}}\longrightarrow\frac{\alpha X_1^{(\alpha -1)-\color{red}{\alpha}} X_2^{{1-\alpha}-(\color{blue}{-\alpha})}}{(1-\alpha) }=\frac{\alpha}{(1-\alpha)}X_1^{-1}X_2^{1}=\frac{\alpha X_2}{(1-\alpha) X_1} $$
Comment: I'd set $X_1>0$ and $X_2>0$, if $\alpha$ is not known to be an integer.
Comment: @egreg: I think this question is arisen in probability. Isn't it?


Let $L$ be the splitting field of $f(x) = x^3 + 10x + 15$ over $\mathbb{Q}$, and $a_1$ (real), $a_2, a_3$ (complex conjugates) the roots of $f$

Question: For $H$ = {$a_1$, $a_2$, $a_3$}, let $S_H$ be the symmetric group on $H$.
Identifying $Gal(L/\mathbb{Q})$ with a subgroup of $S_H$, how do we know if the transposition ($a_2$ $a_3$) the same as complex conjugation?
Comment: Just because knowing $a_1$ is real then the two complex roots are complex conjugates
Comment: For example, if $a_2 = 1+i$ and $a_3 = a-i$, then the transposition $(a_2 a_2)$ will map $1+i$ to $1-i$ and map $1-i$ to $1+i$.
Answer: First you should think about what you mean by 'it is the same as complex conjugation', so that the proof becomes evident. If $\sigma \in H$ is complex conjugation this means that $\sigma=\tau|_{\Omega}$, where $\tau: \mathbb{C} \rightarrow \mathbb{C}$ is complex conjugation, and $\Omega \subset \mathbb{C}$ is the splitting field of $f$. Now, a field homomorphism $\varphi: \Omega \rightarrow \mathbb{C}$ is completely determined by the images of $a_1$, $a_2$ and $a_3$. $\tau|_{\Omega}$ clearly is such a homomorphism, and even more clearly $(a_2 a_3)$ is such a homomorphism (by assumption). $\tau(a_1)=a_1=(a_2 a_3) (a_1)$, $\tau(a_2)=\overline{a_2}=a_3=(a_2 a_3) (a_2)$ and $\tau(a_3)=a_2=(a_2 a_3) (a_ 3)$, so both homomorphisms coincide on $\{a_1, a_2, a_3 \}$, so they must be the same.


Compilar diretivas a partir do Controller

Question: Possuo uma tabela que é renderizada dinamicamente quando se passa um array de dados pra ela.
Eu tenho acesso aos dados de cada "row" da seguinte forma:
{
    "data": function (data, type, val, meta) {

        return "
" + data.Valor + "
";
    }
}

Eu consigo colocar um trecho html ali, como está demonstrado, mas eu gostaria de colocar um progress-bar, e como no sistema já tenho o 'angular-ui' referenciado, queria usar a diretiva dele, mas colocar ali diretamente um
<uib-progressbar class="progress-striped active" max="200" value="166" type="danger">166 / 200</uib-progressbar>
ou compilar usando o $compiler do angularjs não está funcionando.
Como posso proceder?
Answer: Bem, vou deixar um exemplo com o $compile mesmo, dê uma olhada.
Exemplo com JSON e ngRepeat:

var app = angular.module('app', ["ngAnimate","ui.bootstrap"]);

app.directive('compile', function ($compile) {
  return {
    restrict: 'A',
    link: function ($scope, $element, $attrs) {
      var html = $attrs.compile;
      $scope.$watch($attrs.compile, function(html) {
       $element.html(html)
        $compile($element.contents())($scope);
      });
    }
  };
});

app.controller('compileProgressBar', function($scope) {
  $scope.createProgressBar = function(value, max, type){
    return '<uib-progressbar class="progress-striped active" max="'+ max + '" value="'+ value + '" type="'+type+'">'+ value + ' / '+ max + '</uib-progressbar>'
  }
  $scope.empresas = [
   {name: "Empresa 1", progress: 80, typeBar: "sucess"},
   {name: "Empresa 2", progress: 20, typeBar: "danger"},
   {name: "Empresa 3", progress: 50, typeBar: "warning"}
  ];
})
.progress {
  height: 20px;
}
.progress i{
  line-height: 20px;
}
.progress-bar {
  transition: width 1s ease-in-out;
}

    <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">

Compile dynamic uiProgressBar in ngRepeat

    {{empresa.name}}

Explicando...
Javascript
Crio a progressBar a partir de uma diretiva. Então, primeiramente crio a diretiva chamada compile. Ela será aplicada ao elemento que se tornará a progressBar.
No objeto de retorno da diretiva, a restrinjo a parâmetro (restrict: 'A'). Assim ela só poderá ser usada como atributo de uma tag, como uso nos exemplos, e não <compile></compile>. O que pode ser mudado, mas alteraria também o Controller.
No link, função dentro do objeto para manipular o DOM, atribuo os argumentos $scope, o $element e $attrs.
Dentro da função, uso o $scope.$watch, uma função que "escuta" por mudanças em algo. No caso, escuta por mudanças na diretiva compile e a cada mundaça executa o callback.
No callback uso .html() para modificar o conteúdo da div a qual está a diretiva. O novo conteúdo terá o que virá a ser passado dentro do atributo compile, no exemplo, é a variável html que recebe $attrs.compile.
E por último, uso o $compile mesmo, que recebe como argumento o conteúdo  do elemento ($element.contents()). Passando também o $scope para a função.
Obs.: No seu caso, é importante a injeção da dependência do ["ui.bootstrap"].
HTML
Neste exemplo passo uma função que retorna a uma barra com valores modificados por seus argumentos, como no seu exemplo. Os argumentos da função, no caso, são dados vindos do próprio JSON percorrido no ngRepeat, no caso empresa.progress e empresa.typeBar. Sei que a uiProgressBar poderia vir no próprio HTML, mas como o intuito é mostrar o uso do $compile, a deixo sendo retornada como string na função.
Qualquer problema em relação a implementação peço que avise, para tentarmos resolver.
Espero ter ajudado. Abraço.


Different permissions after node publishing

Question: I have a role (Creators) where users can create/edit/delete nodes of some content types but they can't publish it, publication is done by other role (Publishers), I'm using Workbench and Workbench Moderation modules.
Now I need to disable Creators role to edit/delete content (even their own) after someone from Publishers role published node, so if user from Creators role create node and user from Publishers role publish that node, than node creator from Creators node can't edit or delete it.
How can I achieve something like this?
Answer: I resolve this with small custom module. I checking user role as well because I need to apply this only on one role.
There is my code:
/**
 * Implements hook_node_access().
 */
function MODULE_node_access($node, $op, $account = NULL) {
    global $user;
  if (
    // The $node argument can be either a $node object or a machine name of
    // node's content type. It is called multiple times during a page load
    // so it is enough if you perform the check once you get the object.
      is_object($node) && array_key_exists(6, $user->roles) &&
      // Operation on which you want to act: "create", "delete", "update", "view".
      ($op == 'update' || $op == 'delete')
  ) {
    // Check if the node is published.
    if ($node->status == 1) {
      return NODE_ACCESS_DENY;
    }
  }
}

My role's ID is 6, so I used this part array_key_exists(6, $user->roles) because of that.


how to generate Normally distributed random number?

Question: I am looking for a function that can generate Normally distributed random numbers. I came to know about bux-muller transform but I didn't understood it completely what it is doing. Thus it would be good if some one can suggest how to generate Normally distributed random numbers?
Comment: Have you seen the description in [Wikipedia](http://en.wikipedia.org/wiki/Normal_distribution#Generating_values_from_normal_distribution)? If you do not want to use $\sin\,$ and $\cos,\,$ you can use the Marsaglia polar method.
Answer: So box muller transform can generate normally distributed random numbers from uniformly distributed random number.
I found my answer here:
http://www.design.caltech.edu/erik/Misc/Gaussian.html
http://www.random.org/gaussian-distributions/


How to pause Nest.js Kafka consumer

Question: I am using Kafka.js in a Nest.js project.
This is how I initialise KafkaClient:

@Module({
...
providers: [{
      provide: 'KAFKA_CLIENT',
      useFactory: async (configService: KafkaClientConfigService) => {

        const kafkaOptions = configService.getKafkaOptions();
        return ClientProxyFactory.create(kafkaOptions);
      },
      inject: [KafkaClientConfigService],
    },
  ]
...
})

Now I inject KafkaClient to my Controller, and I wish to consume messages with intervals.
While there is a way to do it with Kafka.js using consumer.pause() I could not find any reference to such option in KafkaClient.
Is there any option to do so either by pausing or by throttling the consumer?
Comment: Can you please add your subscriber code here?
Comment: Have you tried asking on their Github as an issue?
Answer: The ClientKafka has a protected consumer property. On the consumer itself you can call pause() and resume().
To use these methods I'm currently extending the ClientKafka with a custom class and use it as provider instead.
import { ClientKafka, KafkaOptions } from '@nestjs/microservices'

export class KafkaClient extends ClientKafka {
  pause() {
    this.consumer.pause()
    // ...
  }
}

Make sure to use the custom class as provider in your app.module.ts:
@NgModule({
  providers: [
    { provide: ClientKafka, useClass: KafkaClient },
  ]
})

Or use useFactory to inject dependencies too.
Comment: I am gettting an error `TypeError: this.kafkaClient.pause is not a function`.
Comment: Make sure to use the `KafkaClient` as provider. Updated my answer to reflect this.

How to pause Nest.js Kafka consumer


How to put number in google sheet query

Question: I want to do a query in google sheet something like:
<code>=query({BY6:CH29},"
select Col2,sum(Col5*Col9*{144}/Col4),sum(Col5*Col10*{144}/Col4),sum(Col7*Col9*{144}/Col4),sum(Col7*Col10*{144}/Col4)
where Col2 is not null
group by Col2
order by Col1
",1)
</code>
How do I do it? I think the number 144 made the formula error
the query return #VALUE!
Unable to parse query string for Function QUERY parameter 2: PARSE_ERROR: Encountered " "" " "" at line 2, column 21. Was expecting: ")" ...
Comment: What is `144` here? Share a sample workbook and show your desired output.
Comment: 1 gross = 12 dozen
Comment: 1 dozen = 12 pieces, so 1 gross = 144 pcs, I try to convert the data to pieces, the data is a fraction to gross
Comment: = query({BY6:CH29}," select Col2,sum(Col5*Col9*{144}/Col4),sum(Col5*Col10*{144}/Col4),sum(Col7*Col9*{144}/Col4),sum(Col7*Col10*{144}/Col4) where Col2 is not null group by Col2 order by Col1 ",1)

all the asterisk is gone :')
Answer: Try this:
<code>=query({BY6:CH29},"
select Col1, Col2,SUM(Col5)*SUM(Col9)*144/SUM(Col4),SUM(Col5)*SUM(Col10)*144/SUM(Col4),sum(Col7)*SUM(Col9)*144/SUM(Col4),sum(Col7)*SUM(Col10)*144/SUM(Col4)
where Col2 is not null
group by Col2,Col1
order by Col1
",1)
</code>
Comment: I added Col1 to first selection and grouping, you may choose another aggregation option
Comment: thank you, so cannot put formula inside SUM() yes?
Comment: it works :thumbup:
Comment: You should use it column by column, yes. I'm glad it works!
Comment: By the way, welcome to StackOverflow, consider marking the question as answered. Good luck!

How to put number in google sheet query


Can't save blob to indexedDB

Question: I'm having trouble saving blob in IndexedDB, and only with blob.
If I save something else (like image as base64), everything works fine.
But with blob, there is simply empty object property saved.
Screenshot from console:

Code:
//prepared blob...
var openRequest = indexedDB.open("testDB",1);

openRequest.onupgradeneeded = function(e) {
    var thisDB = e.target.result;

    if(!thisDB.objectStoreNames.contains("stash")) {
        thisDB.createObjectStore("stash");
    }
}

openRequest.onsuccess = function(e) {
    db = e.target.result;

    var transaction = db.transaction(["stash"],"readwrite");
    var store = transaction.objectStore("stash");
    var tID = Date.now();

    var obj = {
        bl:blob,
        created:tID
    }
    console.log(obj);
    //add it
    var request = store.add(obj, tID);
    request.onerror = function(e) {
        console.log("Error",e.target.error.name);
    }
    request.onsuccess = function(e) {
        console.log("success");
    }
}
openRequest.onerror = function(e) {
    //....
}

I also tried to save only blob (not wrapped as obj property), it's the same.
I can save blob to HDD, and if I console log my obj, I get:

So I guess, blob is valid, and problem is in adding it to indexedDB. I'm new to blob/indexedDB, and probably doing some noob mistake.
Can someone please advise, what am I doing wrong?
PS: no error messages at all
Comment: [The state of binary data in the browser](https://github.com/nolanlawson/state-of-binary-data-in-the-browser)

>IndexedDB has many Blob bugs in Chrome.
Comment: @wOxxOm ok, tnx for pointing that out
Comment: The Chrome developer console may not be showing Blobs when inspecting the stores/indexes. Have you tried writing code to read the value back out and inspect it programatically?
Comment: @JoshuaBell no, when I tried to read it (calling it from code, and do something with it) it was empty too...if that's what you are asking...some changes were made to chromium in the meantime, maybe they fixed this too...I can't test it anymore
Answer: Very old question, but there is support now for saving Blobs in IndexedDb:

https://developers.google.com/web/updates/2014/07/Blob-support-for-IndexedDB-landed-on-Chrome-Dev

// Create an example Blob object
var blob = new Blob(['blob object'], {type: 'text/plain'});

try {
    var store = db.transaction(['entries'], 'readwrite').objectStore('entries');

    // Store the object
    var req = store.put(blob, 'blob');
    req.onerror = function(e) {
        console.log(e);
    };
    req.onsuccess = function(event) {
        console.log('Successfully stored a blob as Blob.');
    };
} catch (e) {
    var reader = new FileReader();
    reader.onload = function(event) {
        // After exception, you have to start over from getting transaction.
        var store = db.transaction(['entries'], 'readwrite').objectStore('entries');

        // Obtain DataURL string
        var data = event.target.result;
        var req = store.put(data, 'blob');
        req.onerror = function(e) {
            console.log(e);
        };
        req.onsuccess = function(event) {
            console.log('Successfully stored a blob as String.');
        };
    };
    // Convert Blob into DataURL string
    reader.readAsDataURL(blob);
}

As of posting this, the referenced Document was last updated on: Last updated 2019-03-20 UTC.
Answer: You can convert Blob or File object to ArrayBuffer object or binarystring and then save it. Convert it back to Blob after you read from indexedDB.
//prepared blob...
blobToBlob2(blob, saveBlob2);

function blobToBlob2(blob, callback){
    var reader = new FileReader();
    reader.readAsArrayBuffer(blob);
    reader.onload = function(e) {
        callback({
            buffer: e.target.result,
            type: blob.type
        });
    };
}

function blob2ToBlob(blob2){
    return new Blob([blob2.buffer],{type:blob2.type});
}

function saveBlob2(blob2){
    //..... code
    var obj = {
        bl:blob2,
        created:tID
    }
    var request = store.add(obj, tID);
    //..... code
}

Comment: I'm completely out of this project right now, but planing to go back to it when i found time (it was half-year ago). The whole point of blob-indexedDB thing was to save large images (SS), instead of using dataURIs to chrome.storage. Thanx

Can't save blob to indexedDB


How to access field result from returned controller in MVC .net core

Question: Whilst my controller functions correctly, I am getting a syntax error in my unit test on the "Assert" line for .Name

IAction result does not contain a definition for "Name"...

If I hover over "result" in debug mode I can see the data is in the result variable (Result > Model > Name). I've tried accessing it with Result.Model.Name but that's a syntax error also.
Unit test:
    [Fact]
    public async Task TestGetNameById()
    {
        string expectedName = "Component";

        using (var context = GetContextWithData())
        using (var controller = new AssetTypesController(context))
        {
            var result = await controller.Details(2);
            Assert.Equal(expectedName, result.Name);
        }
    }

Controller action:
   public async Task<IActionResult> Details(int? id)
    {
        if (id == null)
        {
            return NotFound();
        }

        var assetType = await _context.AssetType
            .SingleOrDefaultAsync(m => m.AssetTypeId == id);
        if (assetType == null)
        {
            return NotFound();
        }

        return View(assetType);
    }

Model
public class AssetType
{
    [DatabaseGenerated(databaseGeneratedOption: DatabaseGeneratedOption.Identity)]
    [Key]
    public int AssetTypeId { get; set; }
    [Required]
    public string Name { get; set; }
}

Answer: That's because IActionResult is not a type of AssetType.
Try something like this:
[Fact]
public async Task TestGetNameById()
{
    string expectedName = "Component";

    using (var context = GetContextWithData())
    {
        var controller = new AssetTypesController(context);
        var result = await controller.Details(2) as ViewResult;
        var assetType = (AssetType) result.ViewData.Model;
        Assert.Equal(expectedName, assetType.Name);
    }
}

Comment: Excellent, thank you. I don't quite understand why I couldn't do `AssetType assetType = result.ViewData.Model` - also do you have any idea why ReSharper gives me a warning for the "result" in result.ViewData.Model saying "possible System.NullReferenceException" ?
Comment: @egmfrs Probably because controller.Details(2) could be returned as null.
Comment: Changing to (ViewResult)await controller.Details(2); fixed the syntax warning, but then the unit test failed. I'll just leave it as it was.
Comment: Adding Assert.NotNull(result); directly after the var result line fixes the syntax warning.


XSLT moving items inside other tag at the same level

Question: I have this problem for xslt:
This is the input file:
  <root>

   <item/>
   … other n-1 <item/>

   <item/>
   … other m-1 <item/>
  </root>

so header and item are at the same level (/root).
it must be converted in something like:
<root2>
  <header2>
     <item2/>
     …<item2/> // the first n-items up
  </header2>
  <header2>
     <item2/>
     …<item2/> // the last m-items up
  </header2>
</root2>

so basically the first n-item must be moved in the first header while the second group of items must be moved in the second header. Any idea how to get this?
Thanks
Randomize
Comment: Good question, +1. A simpler and more efficient solution than the currently accepted one is possible.
Comment: Added also an XSLT 2.0 solution.
Comment: Added an explanation for each of the two solutions.
Answer: Sample XML:
<root>

    <item>1</item>
    <item>2</item>
    <item>3</item>

    <item>5</item>
    <item>6</item>
    <item>7</item>
</root>

XSLT using grouping:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:key name="k" match="item" use="count(preceding-sibling::header)"/>

    <xsl:template match="/">
        <root2>
            <xsl:apply-templates select="root/item[generate-id(.) = generate-id(key('k', count(preceding-sibling::header)))]" mode="a"/>
        </root2>
    </xsl:template>

    <xsl:template match="item" mode="a">
        <header2>
            <xsl:apply-templates select="key('k', count(preceding-sibling::header))"/>
        </header2>
    </xsl:template>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

Or simple particular XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:template match="/">
        <root2>
            <header2>
                <xsl:apply-templates select="root/item[count(preceding-sibling::header) = 1]"/>
            </header2>
            <header2>
                <xsl:apply-templates select="root/item[count(preceding-sibling::header) = 2]"/>
            </header2>
        </root2>
    </xsl:template>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

Both produce the same output:
<root2>
  <header2>
    <item>1</item>
    <item>2</item>
    <item>3</item>
  </header2>
  <header2>
    <item>5</item>
    <item>6</item>
    <item>7</item>
  </header2>
</root2>

Answer: This is a simpler more efficient XSLT 1.0 solution, than counting all preceding siblings:
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:key name="kFollowing" match="item"
  use="generate-id(preceding-sibling::header[1])"/>

 <xsl:template match="/*">
  <root>
   <xsl:apply-templates select="header"/>
  </root>
 </xsl:template>

 <xsl:template match="header">
  <heather2>
   <xsl:copy-of select="key('kFollowing', generate-id())"/>
  </heather2>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the following XML document:
<root>

    <item>1</item>
    <item>2</item>
    <item>3</item>

    <item>5</item>
    <item>6</item>
    <item>7</item>
</root>

the wanted, correct result is produced:
<root>
   <heather2>
      <item>1</item>
      <item>2</item>
      <item>3</item>
   </heather2>
   <heather2>
      <item>5</item>
      <item>6</item>
      <item>7</item>
   </heather2>
</root>

Explanation:
A key is defined in such a way that for a header any of the immediately-following item elements is matched by the generate-id() of this header.
II. XSLT 2.0 solution:
<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/*">
  <root>
   <xsl:for-each-group select="item"
        group-adjacent=
         "generate-id(preceding-sibling::header[1])">
     <header2>
      <xsl:sequence select="current-group()"/>
     </header2>
   </xsl:for-each-group>
  </root>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the same XML document (above), the same correct result is produced:
<root>
   <header2>
      <item>1</item>
      <item>2</item>
      <item>3</item>
   </header2>
   <header2>
      <item>5</item>
      <item>6</item>
      <item>7</item>
   </header2>
</root>

Explanation: Use of xsl:for-each-group, group-adjacent, current-group()


CentOS 5.7 keeps rebooting after fresh installation

Question: I have just installed CentOS 5.7 x86_64 on a new computer. The installation went on without any issues. But, after it finnished, the machine started to show an awkward behaviour: it restarts every time it tries to boot.
It happens after all the services have been started. The screen just goes black and it shows an error message from the monitor: Input not supported. And then it reboots.
I took a look at the logs, but I couldn't manage to find anything.
Any help?
Update
Before doing the hardware diagnosis, as pointed out, I decided to make some tests.
First, I changed the runlevel to 3, adding the 3 parameter at the end of the kernel command. Then, after logging in in text mode, I checked the xorg.conf file out for some problems regarding the screen resolution. There was nothing unexpected set. Well, if there had to be a problem with it, I couldn't start the X server at the command line, right? So, I typed startx and Gnome started! So, probably, it's not an issue with the screen resolution, I suppose.
Then I selected the Log Out root... Gnome menu option and something odd happened: the screen went black, the Input not supported monitor error message was displayed and the system rebooted. Yes, the same problem I was having while trying to boot!
After that, I decided to try yet another test: I removed the rhgb quiet parameters from the kernel command to see if some error would show up. Well, to my surprise, the boot went on without problems! The Gnome login screen showed up, I logged in and the session started. But then I selected the Shut Down... menu option and guess what? Same problem: black screen, same monitor error and the system rebooted. Yes, it rebooted, it did not shut down.
I repeated both of the tests and the behaviours were the same. I really don't know what's going on. It seems to be an issue regarding the changing of the screen mode or something like that.
Any ideas? Could this be a hardware problem? Or does it seem to be something regarding the system configuration?

As I couldn't afford to lose any more time on this issue I installed Debian on this machine.
I really would like to thank you guys who took time to answer the question and tried to help!
Since this question is not going to provide any help for someone who bumps into it in the future, should I delete it?
Answer: The error message "Input not supported" usually means that your computer is sending an incompatible display mode to your monitor.
At what stage of the boot process do you get that error?
Comment: Just right after the last daemon service, _smartd_, has started. Actually, I'm not quite sure if it really starts. I can see the message _Starting smartd_, but as soon as it appears, the screen goes black. Is there any way I could gather more information about this? Any log file I could take a look to find out more about the problem?
Comment: You should boot in [single-user mode](http://centos.org/docs/5/html/Installation_Guide-en-US/s1-rescuemode-booting-single.html) and look through */var/log/Xorg**. After that you can edit **or** create your *xorg.conf* file and configure it to use lower resolution or refresh rate (the problem could be caused by any of these).
Answer: Looks like you set resolution and/or refresh rate bigger than the monitor can handle. Boot to rescue mode, check your xorg.conf.
Answer: You'll want to do some hardware testing on that machine. What you're describing sounds almost entirely like hardware failure.
Test the hard drives and memory first (memtest86+ and probably the disk diagnostics from your manufacturer).
Answer: I agree with the hardware diagnosis. Without knowing much about your environment, I would venture a guess that heat could be an issue here.
Answer: Looks like you've narrowed it down to something that happens in your default run level but not in init level 3 so compare your init scripts for run level 3 with whatever your default is.
Answer: I took this from the CentOS forum so you might want to try it out:

Boot to runlevel 3, AKA text mode, by  1) interrupting GRUB by typing a
  character such as "space", 2) invoking edit mode by typing "e",  3)
  appending to the kernel line by typing "a", backspacing through "rhgb
  quiet" and appending " 3". 4) typing "b" to boot. Log in as
  root and run system-config-display.


find all holomorphic $f$ with $f^{(k)}(0)=0$ for $0\le k \le n-1$ and $|f^{(n)}(0)|=n!$

Question: Let $n\ge 1$ be an integer. Find all holomorphic functions $f : D\to D$ which satisy the conditions$f^{(k)}(0)=0$ for $0\le k \le n-1$ and $|f^{(n)}(0)|=n!$
I had a feeling that $az^n$ with $|a|=1$ will be all such fuctions, but I still cannot find a way to prove it. Any help is appreciated!Thank you very much.
Answer: If $f\colon D \to D$ is holomorphic with $f^{(k)}(0) = 0$ for $0 \leqslant k < n$ and $\lvert f^{(n)}(0)\rvert = n!$, then its Taylor series starts
$$f(z) = a_n z^n + \dotsb$$
with $\lvert a_n\rvert = 1$, so
$$g(z) = \frac{f(z)}{z^n}$$
is a holomorphic function on the unit disk with $\lvert g(0)\rvert = 1$. But
$$\lvert g(z)\rvert = \frac{\lvert f(z)\rvert}{\lvert z\rvert^n} \leqslant \frac{1}{\lvert z\rvert^n},$$
and therefore, taking the limit $\lvert z\rvert \to 1$, $\lvert g(z)\rvert \leqslant 1$. The maximum modulus principle now implies that $g$ is constant, $g \equiv a_n$, and hence $f(z) = a_nz^n$.
Comment: Why do we have $|f(z)| \le 1$? Also do you mean Liouville's Theorem, not the Maximum Modulus Principle?
Comment: @Daron: $|f(z)|\leq 1$ because the codomain of $f$ is $D$.  No, Max Mod Principle implies that $|g|$ cannot have a local max in $D$ unless $g$ is constant, so $|g(0)|=1$ and $|g(z)|\leq 1$ for all $z$ imply $g(z)$ is constant.
Comment: @Daron $D$ is the unit disk, $\{ z : \lvert z\rvert < 1\}$, and $f\colon D\to D$ is the setting. Liouville's theorem is concerned with entire functions, here we have a function defined only on the unit disk. The maximum modulus principle tells us that a holomorphic function $h \colon \Omega \to \mathbb{C}$, where $\Omega$ is a domain, such that $\lvert h\rvert$ has a local maximum, is constant.
Comment: Oh thanks, I misread the question. I thought we wanted $f^{(N)}(0) = N! ~\forall N \ge n$. Which cannot occur.


Upgrading a simple drupal 7 module to drupal 8?

Question: I am new to drupal and currently using version 8. I am trying to convert cricket feed widget module in drupal 7 to drupal 8 based on Community instructions.. But I am unable to understand the routing.yml files. I don't understand the parameters used in the routing.yml file to connect with the controllers. I checked out some of their examples..
I don't understand
what they mean by sub_name and
Path: Path to the page on site.How do I know that?
Is it that much difficult to convert a simple drupal 7 module to drupal 8?
Comment: I might avoid rhetorical questions in the future even if you find something frustrating. You may also find the drush plugin, [drupalmoduleupgrader](https://www.drupal.org/project/drupalmoduleupgrader) to be helpful in understanding the differences from Drupal 7 code and Drupal 8 code. My best practice is to always review and change my code manually because I often like to change my approach depending on new APIs that are available. However that tool has an "analyze" mode which allows you to see what it's output would be, and this is excellent for reviewing and learning.
Answer: I've spent the past few months doing a lot of D7 -> D8 upgrades. I haven't used sub_name (or even seen it), so I cannot comment on that, but as for the path, it's the same path you declared as the array key in your hook_menu definition - however note that with D8 you need to include the preceeding forward slash.
So if your D7 hook_menu() definition for a path was:
<code>$menu['path/to/something']
</code>
In D8 your path in the routing file would be:
<code>path: /path/to/something
</code>
Comment: `sub_name` in this example refers to giving the route an unique machine name (in addition to the best practice of prefixing the route name with the module name).


Is it possible to create a multi level (stacked) donut chart with variable diameter on the last stack

Question: Is it possible to create a spider chart or sunburst or stacked doughnut chart in Highcharts or another charting library that has fixed width on the outer level and variable length similar to this one:
https://www.dropbox.com/s/b13tj46svjpxrku/image.png?dl=0
To confirm, I need to indicate the last data set visually by length so if one bar is 5 a and one is 10 then the second bar is twice as long.
Comment: Please include a Minimal, Complete, and Verifiable example to your question. Otherwise it may be impossible to tell exactly where the issue lies.
Comment: Hi @Bob Fleming, That chart type is not supported by default in Highcharts. You can try to combine two charts: `column` series in `polar` chart and `sunburst`. Please check this example: https://jsfiddle.net/BlackLabel/sgvoxf37/
Comment: @ppotaczek that is great. I think this is just what we need. Thank you so much!
Comment: @Bob Fleming, I added the suggestion as an answer. If it was helpful you can accept it.
Answer: That chart type is not supported by default in Highcharts, but you can combine two charts: column series in polar chart and sunburst:
Highcharts.chart('container', {
    chart: {
        polar: true,
        type: 'column',
        ...
    },
    ...
});

Highcharts.chart('container2', {
    chart: {
        type: "sunburst",
        ...
    },
    ...
});

Live demo: https://jsfiddle.net/BlackLabel/sgvoxf37/


Structuring Redux state for complex relational domain model

Question: Consider the following typical CRUD application:
Data layer
Relational domain model with one-to-one, one-to-many and many-to-many relations between entities. Entities are persisted in relational database.
Service layer
Service layer provides a REST API handling GET, POST, PUT and DELETE methods on entities to perform business transactions.
Client state layer
Client state is managed by Redux state container. Redux stores representation of entities in the data layer and the pending transactions on them. Redux middleware is utilized for more hygienic and easier to reason about code.
Presentation layer
UI is implemented using React with client-side rendering. Components internal states are used for minor state management as "prelude" to initiating transactions with bigger brush (Redux actions).
Which is the best approach to structure the Redux state, reducers, actions and subscriptions for complex domain model? Of course minimizing the code duplication yet enabling loose coupling and independent actions on entities? Also, is it better to manage "unsaved changes" and validation logic on components internal states?
Comment: regarding state and reducers: read this [https://redux.js.org/recipes/structuring-reducers/normalizing-state-shape](https://redux.js.org/recipes/structuring-reducers/normalizing-state-shape)
Comment: Not sure why you think this is DDD related? Your data layer seems very CRUD-oriented the way you described it.
Answer: For managing relational domain in Redux, try normalized-reducer. It is a higher-order-reducer that takes a schema describing the relationships, and it returns a reducer, actions, and selectors that let you write/read accordingly. It also integrates easily with Normalizr and Redux Toolkit.


Adding the DragDropContext gives me an error about exporting the ES6 module

Question: So I started to integrate the react dnd library into my application, and the first thing I tried to do is add the DragDropContext with the Html5 backend.
When I add the attribute to the class I get this error:

Uncaught Error: Expected the backend to be a function or an ES6 module
  exporting a default function.

import React from 'react';
import Link from 'react-router-dom';
import { connect } from 'react-redux';
import { DragDropContext } from 'react-dnd';
import { HTML5Backend } from 'react-dnd-html5-backend';
import PropTypes from 'prop-types';

@DragDropContext(HTML5Backend)
class UserHowView extends React.Component {
  ...
  ..
}

const mapStateToProps = state => ({
 ...
});

export default connect(mapStateToProps)(userShowView);

What am I doing wrong so far?
One of the examples for the library has this:
@DragDropContext(HTML5Backend)
export default class Container extends Component {

But my definition and export are separate.
Answer: This library has a breaking change since v11.0.0 breaking changes:

from
import HTML5Backend from 'react-dnd-html5-backend
to
import { HTML5Backend } from 'react-dnd-html5-backend

If you are still encountering the issue, please check this issue in repo
and make sure you're not including DragDropContext in your render function
Answer: Remove the braces from the HTML5Backend import statement. Replace with the following:
import HTML5Backend from 'react-dnd-html5-backend';

This has resolved the issue for me.


Apagar a ultima linha de um arquivo txt

Question: Preciso apagar a ultima linha de um arquivo txt.
Encontrei a ultima linha assim:
with open('arquivo.txt', 'r') as content:
    line = content.readlines()
    last_line = line[len(line)-1]

Obs: pode alterar o modo read na função open.
Comment: Você precisa resolver isso necessariamente com Python?
Answer: Se for um arquivo muito grande, veja esta forma, ele lê o arquivo de trás pra frente em binário até encontrar o caractere da ultima linha. Depois o converte.
import os

with open('arquivo.txt', 'rb') as f:
   f.seek(-2, os.SEEK_END)
   while f.read(1) != b'\n':
      f.seek(-2, os.SEEK_CUR)
   print(f.readline().decode())

Answer: Para trabalhar com arquivos grandes essa é uma possível solução:
import os

with open('arquivo.txt', 'r+', encoding = "utf-8") as arquivo:

    # Move o ponteiro (similar a um cursor de um editor de textos) para o fim do arquivo.
    arquivo.seek(0, os.SEEK_END)

    # Pula o ultimo caractere do arquivo
    # No caso de a ultima linha ser null, deletamos a ultima linha e a penúltima
    pos = arquivo.tell() - 1

    # Lê cada caractere no arquivo, um por vez, a partir do penúltimo
    # caractere indo para trás, buscando por um caractere de nova linha
    # Se encontrarmos um nova linha, sai da busca
    while pos > 0 and arquivo.read(1) != "\n":
        pos -= 1
        arquivo.seek(pos, os.SEEK_SET)

    # Enquanto não estivermos no começo do arquivo, deleta todos os caracteres para frente desta posição
    if pos > 0:
        arquivo.seek(pos, os.SEEK_SET)
        arquivo.truncate()

Comment: Resposta original: https://stackoverflow.com/a/10289740/1452488
Answer: Se por acaso estivermos em sistemas *nix, uma solução seria
head -n -1 file > novo

... mas não usa Python.
Answer: Desta forma:
line = file.readlines()
line = lines[:-1]

Comment: Vale notar que isso carregará todo o conteúdo do arquivo em memória. Se estiver trabalhando com arquivos grandes, isso pode ser um problema sério.
Comment: Esse é um dos problemas, o arquivo e grande e preciso minimizar o uso dos recursos.

Apagar a ultima linha de um arquivo txt


How to show timestamps in short format in Jenkins Blue Ocean?

Question: Using Timestamper plugin 1.11.2 with globally enabled timestamps, using the default format, I get the following console output:
00:00:41.097  Some Message

In Blue Ocean the output shows like:
[2020-04-01T00:00:41.097Z] Some Message

How can I make it so that Blue Ocean uses the short timestamp format? The long format is somewhat unreadable and clutters the details view of the steps.
I've looked at the Pipeline Options too, but there is only the timestamps option which doesn't have a parameter to specify the format.
Note: This question isn't a dupe, because it asks for differences in time zone only.
Answer:
Edit:
⚠ Unfortunately this workaround doesn't work in the context of node, see JENKINS-59575. Looks like I have to finally get my hands dirty with plugin development, to do stuff like that in a supported way.
Anyway, I won't delete this answer, as the code may still be useful in other scenarios.

Original answer:
As a workaround, I have created a custom ConsoleLogFilter. It can be applied as a pipeline option, a stage option or at the steps level. If you have the timestamp plugin installed, you should disable the global timestamp option to prevent duplicate timestamps.
Typically you would define the low-level code in a shared library. Here is a sample that can be copy-pasted right into the pipeline script editor (you might have to disable Groovy sandbox):
import hudson.console.LineTransformationOutputStream
import hudson.console.ConsoleLogFilter
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets

pipeline{
    agent any
    /*
    options{
        // Enable timestamps for the whole pipeline, using default format
        //withContext( myTimestamps() )

        // Enable timestamps for the whole pipeline, using custom format
        //withContext( myTimestamps( dateFormat: 'HH:mm:ss', prefix: '', suffix: ' - ' ) )
    }
    */

    stages {
        stage('A') {
            options {
                // Enable timestamps for this stage only
                withContext( myTimestamps() )
            }
            steps {
                echo 'Hello World'
            }
        }
        stage('B') {
            steps {
                echo 'Hello World'

                // Enable timestamps for some steps only
                withMyTimestamps( dateFormat: 'HH:mm:ss') {
                    echo 'Hello World'
                }
            }
        }
    }
}

//----- Code below should be moved into a shared library -----

// For use as option at pipeline or stage level, e. g.: withContext( myTimestamps() )
def myTimestamps( Map args = [:] ) {
    return new MyTimestampedLogFilter( args )
}

// For use as block wrapper at steps level
void withMyTimestamps( Map args = [:], Closure block ) {
    withContext( new MyTimestampedLogFilter( args ), block )
}

class MyTimestampedLogFilter extends ConsoleLogFilter {
    String dateFormat
    String prefix
    String suffix

    MyTimestampedLogFilter( Map args = [:] ) {
        this.dateFormat = args.dateFormat ?: 'YY-MM-dd HH:mm:ss'
        this.prefix     = args.prefix ?: '['
        this.suffix     = args.suffix ?: '] '
    }

    @NonCPS
    OutputStream decorateLogger( AbstractBuild build, OutputStream logger )
            throws IOException, InterruptedException {

        return new MyTimestampedOutputStream( logger, StandardCharsets.UTF_8, this.dateFormat, this.prefix, this.suffix )
    }
}

class MyTimestampedOutputStream extends LineTransformationOutputStream {
    OutputStream logger
    Charset charset
    String dateFormat
    String prefix
    String suffix

    MyTimestampedOutputStream( OutputStream logger, Charset charset, String dateFormat, String prefix, String suffix ) {
        this.logger = logger
        this.charset = charset
        this.dateFormat = dateFormat
        this.prefix = prefix
        this.suffix = suffix
    }

    @NonCPS
    void close() throws IOException {
        super.close();
        logger.close();
    }

    @NonCPS
    void eol( byte[] bytes, int len ) throws IOException {
        def lineIn = charset.decode( java.nio.ByteBuffer.wrap( bytes, 0, len ) ).toString()
        def dateFormatted = new Date().format( this.dateFormat )
        def lineOut = "${this.prefix}${dateFormatted}${this.suffix}${lineIn}\n"
        logger.write( lineOut.getBytes( charset ) )
    }
}

Example output for stage "B":

Credits:
I got the idea from this answer.


Why is additivity assumption in additive models unrealistic? Is it more unrealistic than the assumption of Linearity?

Question: I have gone through the following questions regarding the assumptions of additive models.
Assumptions of additive model
What does the additive assumption mean?
However, I do not quite get the intuitive understanding of why additive assumptions are regarded as unrealistic.
And how unrealistic is it? (if it's quantifiable)
Is it more unrealistic than the assumption of Linearity ?
Can anybody please demonstrate with a simple real-world example?
Answer: Suppose our inputs are $x\in\mathbb R^p$ and we are trying to model $y\in\mathbb R$ using a function $f : \mathbb R^p\to\mathbb R$.
An additive model is of the form
$$
f(x) = \beta_0 + \sum_{i=1}^m\beta_ih_i(x_i)
$$
so we're saying that $f$, our potentially complicated function of a $p$-dimensional input, is built out of functions only of each coordinate separately.
Note that if $h_i(x_i) = x_i$ then this reduces to a standard linear model, so additive models generalize linear models.
This is restrictive because we're missing so many possible functions, like if $y = x_1x_2$ we won't be able to exactly capture that. Any function that needs to simultaneously consider multiple coordinates of $x$ at once won't be exactly model-able with an additive function.
Another way to see why this is so restrictive is that once we fix our basis functions $\{h_1,\dots,h_m\}$ we are only considering functions in the span of this set, and that'll be at most an $m$-dimensional subspace of the probably infinite dimension function space that could contain the "true" function, so in that sense we have almost no chance of the true function being in our thin slice through this space that we're considering.
Comment: This assumes a very narrow view of functions $f$; there is nothing stopping us having a function $f(x_1,x_2)$ and estimating that function via a tensor product of the marginal bases for $x_1$ and $x_2$ say. The issue of basis dimension is problematic, but i) advice is to set the dimensionality sufficiently large to include the expected wiggliness/true function, and ii) we could make do with an approximation to the true function.
Comment: @Alexis I think we're having the same discussion as whether or not $y = x_1 + x_1^2 + \varepsilon$ is a "linear model" and you're saying it is because it's linear in the parameters and I'm saying it's not a linear function of $x_1$ and we're both right.
Comment: @ReinstateMonica-G.Simpson I was thinking about when the true function would exactly be in the class of functions we are considering or not, that isn't helped by things like tensor products of marginal bases right? It'd be a good way to estimate a nonadditive function with an additive one but the true function still wouldn't be in our class of additive functions right?
Comment: –1 You write "…if $y=x_1x_2$ we won't be able to exactly capture that." But this is hogwash in additive models like multiple regressions where multiplicative interaction terms such as $x_1x_2$ are both trivially easy and commonplace to **add** to other right-hand terms.
Comment: @Alexis I don't think I understand your point. Sure we can approximate interactions with additive functions but a nonadditive function won't actually be in the function space in question so function-wise we're inconsistent at best
Comment: Variable $x_3 = x_1x_2$, now I can, for example: $y = \beta_3x_3 +\epsilon$, or $y = \beta_0 + \beta_1x_1 + \beta_2x_2 +\beta_3x_3 +\epsilon$, or $y = f_1(x_1,\theta_1) + f_2(x_2,\theta_2) +f_3(x_3,\theta_3) + \epsilon$, etc. Those are all additive models perfectly able to capture $x_1x_2$ in a variety of ways.
Comment: @Alexis sure that's additive in $(x_1, x_2, x_1x_2)$ but it's still not additive in the original variables which is what I'm talking about. I'm talking about what it means for a function to be additive, not if a non-additive function can be represented by an additive function if we change the domain. That's obviously also true but I think that's a separate point
Comment: How is that separate in any linear modeling context?
Comment: It is also an additive model.


Constructing gamma prior from Poisson

Question: So if we have a Poisson distribution with a rate lambda we know that the prior is a gamma with alpha,beta. But suppose we didn't know that the prior was a gamma. How would we do the derivation please?
I think it's pretty straightforward and I'm just not seeing it.
Comment: Your question doesn't make sense - priors for a parameter don't generally come from examining the density of the model for the data. Are you asking about a particular kind of prior, like a Jeffreys' prior, or maybe just about conjugate priors, where you would look at the form of the probability function? Please clarify.
Comment: Conjugate prior please. I was thinking about if you had a prob mass dist which did not appear on one of the standard tables
Answer: Consider the posterior as prior$\times$likelihood and we want the posterior to be of the same form as the prior, so the likelihood must combine with the prior in a way that leaves the prior unchanged in form, apart from the values of the parameters.
In the case of the Poisson, we can do this one by inspection.
Recall that the likelihood is a function of the parameter, not the data (the data are fixed - we condition on them):
$\mathcal{L}(\lambda;\mathbf{x})\propto \lambda^{S} \exp(-n\lambda)\propto \lambda^{(S+1)-1} \exp(-n\lambda)$
(where $S$ is an obvious function of the data). We can immediately see that the likelihood is (up to scaling constants) of the form of a gamma density in $\lambda$.
This suggests to us that perhaps a gamma density could be a conjugate prior, since that also consists of a power of $\lambda$ and $\exp(-k\lambda)$, both of which will combine when we take the product:
$p(\theta|\mathbf{x})\propto \mathcal{L}(\lambda;\mathbf{x})\propto \lambda^{S} \exp(-n\lambda)\cdot \lambda^{\alpha-1}\exp(-\lambda \theta)$
$\hspace{1.4cm}\propto \lambda^{(S+\alpha)-1} e^{-\lambda(n+\theta)}$
Often this is all that it takes - looking at the likelihood as a function of the parameter and (in simple cases) recognizing the form of density where products of such densities are scaled densities of the same kind (as is the case with normal or gammas). In other cases, you will instead need to recognize that there's some density which the likelihood combines with in a product which will also be of the same form as the prior (even though the likelihood itself perhaps isn't in that form).
For another example, see this discussion of a conjugate prior for the Laplace (in the sense that the posterior is in the same form as the prior).

Constructing gamma prior from Poisson


Pass Term object to theme

Question: In my controller, I have a function that gets a taxonomy term. If I pass the term name to my theme function ($current_type = $term->name;), that works.
$roots = $this->termStorage->loadTree(ProductsController::PRIMARY_CATEGORIES, 0, 1, FALSE);
$current_type = null;
foreach ($roots as $term){
  if($this->urlSafeTerm($term->name) == $category){
    // $current_type = $term->name;
    $current_type = Term::load($term->tid);
    break;
  }
}

$build = [
  '#theme' => 'product_category',
  '#type' => $current_type,
  ],
];

return $build;

In my hook_theme function:
'product_category' => [
  'template' => 'products/category',
  'variables' => [
    'type' => NULL,
],

But if I try to load the term object ($current_type = Term::load($term->tid);) and pass that, I gives me:
TypeError: Illegal offset type in Drupal\Core\Render\ElementInfoManager->getInfo() (line 79 of /app/web/core/lib/Drupal/Core/Render/ElementInfoManager.php)

Is it not possible to pass a Term entity like this or is there something else I'm missing and/or doing wrong?
Thanks!
Comment: Try a different template variable name, `#type` is used for render elements.
Answer: In a render array, #type is already a property used by Drupal, which cannot be used for other purposes from a theme hook. It's the machine name of the type of render/form element.
For example, in the following code, #type is what allows Drupal to understand that the first item is a detail element and the second one is a text field.
$form['author'] = array(
  '#type' => 'details',
  '#title' => >t('Author'),
);
$form['author']['name'] = array(
  '#type' => 'textfield',
  '#title' => t('Name'),
);

When Drupal finds #type in a render array, it will ask to the element plugin manager information about the element plugin to use. (See ElementInfoManager::getInfo().) With the wrong value, it throws the exception you are seeing.
The solution is replacing type with a string that isn't already used by Drupal. Render API overview (where the purpose of #type and #theme is explained) and the documentation for RenderElement show a list of properties already used by Drupal.
For example, replacing type with term_id should fix that error.
Comment: Thank you! I knew it had to be something simple I was overlooking.

Pass Term object to theme


hook_block_view called multiple times per request

Question: I am creating a module for Drupal 7 that implements the hook_block_view. I make a call to watchdog in the hook method and I noticed (while debugging) that the the watchdog method is firing three times per page refresh. Is this normal, or is it something I should be worried about. Since each call to the method makes a database query it seems that this could result in a performance hit.
Answer: It gets called everytime the block is rendered.  Truthfully, that is a bit wasteful since the params it is passed are the same.  Still, in the normal block system, each block only loads once (i.e. it can only be in one region).  This makes the logic forgivable, but yeah, it should be cached.  You can of course cache it yourself in your hook_block_view implementation.
Disclaimer: I am drunk.


jQuery AutoSuggest Plugin - preFill options not working

Question: I'm using this jQuery AutoSuggest Plugin.
It's pretty well documented, but there is a built-in option called preFill that doesn't seem to be working. A few people including myself have had this issue on their support forum.
We may just not be entering the option in correctly. There's not a good example in the documentation for how to enter in the option for preFill. Can anyone make sense of how this option should be formatted when it is defined?
Comment: Could you post the code you're using / the errors you're getting?
Answer: Solved it. Answer is on the plugin forum.


Is it possible to find such a $n\in \mathbb {Z^{+}}$, for given value of $\lambda;$ $\frac {2^{10+\lambda+n}-2^{10+\lambda}-144759}{3^{10}}<349525$

Question: I'm trying to solve a mathematical problem. I expressed the point where I was stuck with the modular arithmetic. Here is my problem;
Is it possible to find such a $n\in \mathbb {Z^{+}}$,  for given value of $\lambda,$

$$\frac {2^{10+\lambda+n}-2^{10+\lambda}-144759}{3^{10}}<349525$$
$$2^{10+\lambda+n}-2^{10+\lambda}-144759 \equiv 0 \pmod {3^{10}}$$
Where, $\lambda =1,2,3,...,39366.$

Is there a mathematical way to solve this problem?..I don't know. But, I tried only random value of $\lambda$ and I used discrete logarithm calculator. After a few unfortunate attempts, I gave up.
At least, is it possible to say the existence of such a value of $\lambda$ without making a calculation?
https://www.alpertron.com.ar/DILOG.HTM
Comment: $\lambda$ is given and $n$ is sought or the other way around?
Comment: @Ihf You are right..(sorry for bad English)
Comment: Which is one is given then? Your last paragraph seems to say that $\lambda$ is sought but the second paragraph  seems to say that $\lambda$ is given.
Comment: @Ihf  Please, can you edit my question?...$\lambda$ is given $1,2,3,4...$ and $ n$ is sought..
Comment: Unless I'm doing my math wrong $\lambda$ has an upper limit of $24$ doesn't it?  $ 2^{\lambda + 10}(2^n - 1) <349525*3^10 + 144759 < 2^{35}$ implies $\lambda + 10 \le 34$, right?
Comment: @fleablood Yes you are right..I've realized now.
Answer: \begin{align}
   2^{10+\lambda+n} - 2^{10+\lambda} - 144759 &\equiv 0 \pmod{3^{10}} \\
   1024 \cdot 2^{\lambda+n} - 1024 \cdot 2^{\lambda} - 144759
      &\equiv 0 \pmod{3^{10}} \\
   1024 \cdot 2^{\lambda}(2^n - 1)  &\equiv 144759 \pmod{3^{10}} \\
   2^n - 1  &\equiv 13116 \cdot 2^{-\lambda} \pmod{3^{10}} \\
   2^n  &\equiv 1 + 13116 \cdot 2^{-\lambda} \pmod{3^{10}} \\
\end{align}
Comment: Thank you so much.
Comment: Isn't $\lambda$ fixed but $n$ that needs to be solved for?
Comment: Is this based on $2$ being a primitive root mod $3^{10}$ and $1+13116 \cdot 2^{-\lambda}$ being invertible mod $3^{10}$ ?
Comment: @lhf : Yes. and by $2^{-\lambda}$, I mean $(2^\lambda)^{-1} \pmod{3^{10}}$
Answer: You can write the first as $$2^n-1\lt \frac {349525\cdot 3^{10}+144759}{2^{10+\lambda}}\\n \lt \log_2\left(349525\cdot 3^{10}+144759\right)-10-\lambda\\n\le24-\lambda $$
so there are very few $n$s to try.  If $\lambda$ is at all large there are not any.


nested function change variable in an outside function not working

Question: def some_func(a):
    def access_a():
        print(a)
    access_a()

outputs the value of a. However, if I want to change a in the nested function like this:
def some_func(a):
    def change_a():
        a += 1
        print(a)
    change_a()

it raises UnboundLocalError exception.
I know a is a nonlocal variable, but why can I access it without declaring nonlocal a?
Comment: This is the same thing as with `global`: you can access a global variable, but trying to modify it without `global var` will fail.
Comment: Regardless of any ways to work around this, reaching out like this and causing arbitrary side-effects is not good programming practice.
Comment: Have you tried `global a` in the functions?
Comment: ...so you shouldn't be doing it in the first place.
Answer: Python scoping rules 101:

a name bound in a function body is considered local unless explicitely declared global (Python 2.x and 3.x) or nonlocal (Python 3.x only). This holds true whereever the assignment happens in the function's body. Trying to read a local variable before it's bound is of course an error.
if a name is read but not bound in a function's body, it will be looked up in enclosing scopes (outer function(s) if any then global scope). NB: functions arguments are de facto local names so they will never be looked up in enclosing scopes.

Note that a += 1 is mainly a shortcut for a = a + 1, so in your example a is local (bound in the function's body and not explicitely declared global or nonlocal), but you  try to read it (the rhs of a = a+1) before it's bound.
In Python 3 you can solve this with a nonlocal statement:
>>> def outer(a):
...    def change():
...       nonlocal a
...       a += 1
...    print("before : {}".format(a))
...    change()
...    print ("after : {}".format(a))
...
>>> outer(42)
before : 42
after : 43

Python 2 doesn't have nonlocal so the canonical hack is to wrap the variable in a mutable container (typically a list but any mutable object would do):
>>> def outer(a):
...     _a = [a]
...     def change():
...         _a[0] += 1
...     print("before : {}".format(_a[0]))
...     change()
...     print ("after : {}".format(_a[0]))
...
>>> outer(42)
before : 42
after : 43

which is quite ugly to say the least.
Now while closures are quite handy, they are mostly the functional counterpart of objects : a way to share state between a set of functions while preserving encapsulation of this state, so if you find you have a need for a nonlocal variable perhaps a proper class might be a cleaner solution (though possibly not for your example that doesn't return the inner function but only uses it internally).
Answer: i have two solutions for you:
#first one:
# try with list, compound data types dict/list
def some_func(a):
    def change_a():
        a[0] += 1
        print(a[0])
    change_a()
some_func([1])
>>> 2

#second one
#reference pointer
from ctypes import *
def some_func_ctypes(a):
    def change_a():
      a[0] += 1
      print a.contents, a[0]
    change_a()

i = c_int(1)
pi = pointer(i)
some_func_ctypes(pi)

>>> c_int(2) 2

Comment: What the OP was asking was why he could _read_ a nonlocal variable but not _rebind_ it...
Comment: @bruno, dude you did a great explanation/answer, thx. should i explain it again? i guess its not more needed but a other solution would be great for the future readers...
Answer: When you use the += operator, there is an assignment of a new value to a. This turns a to a local in the eyes of the interpreter.


Calculating Distance between two coordinates using CLLocation is different on two phones?

Question: I am calculating distance between two coordinates using CoreLocation framework, one being current location of the user and the other coming from the server, both phones are showing a difference of approx. 25 meters in distance when the phones were right next to each other. I was using iPhone 4s and iPhone 5s, is this has something to do with the accuracy of GPS modules installed on both phones or something is wrong with my implementation?
<code>CLLocationDistance distance = [newLocation distanceFromLocation:userLocation];
</code>
Comment: Probably... Did you tried to have a look at the coordinates?
Answer: Neither a handheld GPS or smart phone receiver will give you an exact position. At best you'll get an accuracy of about 10 to 15 feet (3 to 5 meters). If you are under trees or surrounded by tall buildings then you can expect an accuracy of 20 to 30 feet (6 to 10 meters) or even worse. Only professional GPS devices as used by surveyors can give accuracy to under one inch (2cm)
https://support.groundspeak.com/index.php?pg=kb.page&id=673
The accuracy also depends on the kind of cellular/wifi network you are using for your phone. I have experienced that the accuracy of the locations obtained on wifi networks is far greater than when you are on cellular data.
Also when you get a location update in the following delegate of CLLocationManager:-
<code>- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
       fromLocation:(CLLocation *)oldLocation
{
}
</code>
Investigate the 'horizontalAccuracy' of the new location which you get (newLocation.horizontalAccuracy). It is the error in meters, which the coordinates might have.
<code>/*
 *  horizontalAccuracy
 *
 *  Discussion:
 *    Returns the horizontal accuracy of the location. Negative if the lateral location is invalid.
 */
@property(readonly, nonatomic) CLLocationAccuracy horizontalAccuracy;
</code>
Hope it helps.
Comment: I am checking horizontalAccuracy of updated location along with its timestamp. If location is cached, i simply discard it. But as far as i noticed, i didn't get a horizontal accuracy of less than 65.0, why so?
Comment: Apart from all the above factors which I mentioned (type of n/w, elevation and surroundings, city vs country side). Are you testing in India? Location services for iOS work just fine in US, Europe, Japan and some other locations. But even when I am testing in India, I rarely get horizontal accuracy less than 50. If you need to test your logic, try testing it with a GPX file.


Session on page load

Question: I'm building multiple language web and found problem. My language is changing depending on session variable and on first load the session is empty,only after refreshing the page it gets the right session variable. How to set the variable before the page load? This is the code :
<code>    session_start();

  $available_langs = array('en','rus');
    if ($_SESSION['lang']=='') {
       $_SESSION['lang'] = 'en';
   }
  if(isset($_GET['lang']) && $_GET['lang'] != ''){
    if(in_array($_GET['lang'], $available_langs))
    {
      $_SESSION['lang'] = $_GET['lang'];
    }
  }
  include('language/'.$_SESSION['lang'].'.php');
</code>
Comment: Change if ($_SESSION['lang']=='') to if (!isset($_SESSION['lang'])) does that help?
Comment: can you post your full script content ?
Comment: This code is works for me. Instead `include` i've just `var_dump` the variable, and the value of it was `en`
Comment: Jeroen de Jong, propably that was the problem.Thanks!
Answer: <code>  session_start();

  // Direct override beats session
  $lang = $_GET['lang'] ?: $_SESSION['lang'];
  $available_langs = array('en','rus');

  // If the requested language isn't available, or not provided, fall back to first
  if(!in_array($lang, $available_langs))
    $lang = $available_langs[0];

  // Store it in the session and include the template
  $_SESSION['lang'] = $lang;
  include 'language/'.$lang.'.php';
</code>

Session on page load