Android Dynamic Feature modules with Dagger Hilt

Question: I have built a Dynamic feature module sample with Fragments, sub components and dependent components based on plaid app, if you wish to check out here is the link. Now, i'm trying to convert it to Dagger Hilt using the official android document.
In core module which is the library module, app module and dynamic feature modules depend on
@Singleton
@Component(modules = [CoreModule::class])
interface CoreComponent {

    /*
        Provision methods to provide dependencies below to components that depends on
        CoreComponent
     */
    fun coreDependency(): CoreDependency

    fun coreCameraDependency(): CoreCameraDependency

    fun corePhotoDependency(): CorePhotoDependency

    fun coreActivityDependency(): CoreActivityDependency

    @Component.Factory
    interface Factory {
        fun create(@BindsInstance application: Application): CoreComponent
    }

}

and it's module
@Module(includes = [CoreProvideModule::class])
abstract class CoreModule {
    @Binds
    abstract fun bindContext(application: Application): Context
}

@Module
object CoreProvideModule {

    @Singleton
    @Provides
    fun provideCoreDependency(application: Application) = CoreDependency(application)

    @ActivityScope
    @Provides
    fun provideCoreActivityDependency(context: Context) = CoreActivityDependency(context)

    @Provides
    fun provideCoreCameraDependency(): CoreCameraDependency = CoreCameraDependency()

    @Provides
    fun provideCorePhotoDependency(): CorePhotoDependency = CorePhotoDependency()

}

How is CoreComponent migrated? Do provision methods still stay and i only change
@Singleton
@DefineComponent

or
@Singleton
@DefineComponent(parent = ApplicationComponent.class)

for CoreModule i guess i only change
@EntryPoint
@InstallIn(CoreComponent::class)

or is this for adding provision methods in CoreComponent?
How do i create sub-component in app module?
If anyone has a sample with dynamic feature fragments and hilt, or tutorial to build, it would be more than welcome. I'm just working on it at the moment, if i figure it out i would post an answer
Answer: I finally figured it out.
For an app structure
FeatureCamera  FeaturePhotos  (Dynamic Feature Modules)
|         |    |
|         ----App
|              |
core(android-library)

Camera dynamic feature module dependencies from core module, Photo dynamic feature module dependencies from app.
First create a CoreModule in library module
@InstallIn(ApplicationComponent::class)
@Module
class CoreModule {

    @Singleton
    @Provides
    fun provideCoreDependency(application: Application) = CoreDependency(application)

    @Provides
    fun provideCoreActivityDependency(context: Application) = CoreActivityDependency(context)

    @Provides
    fun provideCoreCameraDependency(): CoreCameraDependency = CoreCameraDependency()

    @Provides
    fun provideCorePhotoDependency(): CorePhotoDependency = CorePhotoDependency()
}

An interface with @EntryPoint is required to with provision methods defined in this interface, if you don't define a method for that dependency you cannot inject it even though there is a @Provides method for it. These are mock dependencies that take application or context as only parameter.
@EntryPoint
@InstallIn(ApplicationComponent::class)
interface CoreComponent {

    /*
        Provision methods to provide dependencies to components that depend on this component
     */
    fun coreDependency(): CoreDependency

    fun coreActivityDependency(): CoreActivityDependency

    fun coreCameraDependency(): CoreCameraDependency

    fun corePhotoDependency(): CorePhotoDependency

}

In camera dynamic feature module, create another module for the dependency based inside of this dynamic feature module.
@InstallIn(FragmentComponent::class)
@Module(includes = [CameraBindModule::class])
class CameraModule {

    @Provides
    fun provideCameraObject(context: Context) = CameraObject(context)
}

@InstallIn(FragmentComponent::class)
@Module
abstract class CameraBindModule {
    @Binds
    abstract fun bindContext(application: Application): Context
}

And component to inject dependencies to Fragments or Activities in this DFM.
@Component(
        dependencies = [CoreComponent::class],
        modules = [CameraModule::class]
)
interface CameraComponent {

    fun inject(cameraFragment1: CameraFragment1)
    fun inject(cameraFragment2: CameraFragment2)

    fun inject(cameraActivity: CameraActivity)

    @Component.Factory
    interface Factory {
        fun create(coreComponent: CoreComponent, @BindsInstance application: Application): CameraComponent
    }

}

If injected to Activity call in `onCreate()`

      DaggerCameraComponent.factory().create(
                EntryPointAccessors.fromApplication(
                        applicationContext,
                        CoreComponent::class.java
                ),
                application
        )
                .inject(this)

For injecting to Fragment call in `onCreate()`

    DaggerCameraComponent.factory().create(
            EntryPointAccessors.fromApplication(
                    requireActivity().applicationContext,
                    CoreComponent::class.java
            ),
            requireActivity().application
    )
            .inject(this)

The trick is here to get dependency interface annotated with `@EntryPoint`
using `EntryPointAccessors.fromApplication()`

Also created Activity based dependencies in app module

**MainActivityModule.kt**

    @InstallIn(ActivityComponent::class)
    @Module(includes = [MainActivityBindModule::class])
    class MainActivityModule {

        @Provides
        fun provideToastMaker(application: Application) = ToastMaker(application)

        @ActivityScoped
        @Provides
        fun provideMainActivityObject(context: Context) = MainActivityObject(context)

    }

    @InstallIn(ActivityComponent::class)
    @Module
    abstract class MainActivityBindModule {

        @Binds
        abstract fun bindContext(application: Application): Context

    }

And only intend to inject these dependencies to Photos dynamic feature module so named it as `PhotoDependencies`

    @EntryPoint
    @InstallIn(ActivityComponent::class)
    interface PhotoModuleDependencies {

        fun toastMaker(): ToastMaker

        fun mainActivityObject(): MainActivityObject
    }

In Photos dynamic feature module create dagger module named `PhotoModule`

    @InstallIn(FragmentComponent::class)
    @Module(includes = [PhotoBindModule::class])
    class PhotoModule {

        @Provides
        fun providePhotoObject(application: Application): PhotoObject = PhotoObject(application)

    }

    @InstallIn(FragmentComponent::class)
    @Module
    abstract class PhotoBindModule {
        @Binds
        abstract fun bindContext(application: Application): Context
    }

And component

    @Component(
            dependencies = [PhotoModuleDependencies::class],
            modules = [PhotoModule::class]
    )
    interface PhotoComponent {

        fun inject(photosFragment1: PhotoFragment1)
        fun inject(photosFragment2: PhotoFragment2)

        @Component.Factory
        interface Factory {
            fun create(photoModuleDependencies: PhotoModuleDependencies,
                       @BindsInstance application: Application): PhotoComponent
        }
    }

And inject to fragments with

    DaggerPhotoComponent.factory().create(
            EntryPointAccessors.fromActivity(
                    requireActivity(),
                    PhotoModuleDependencies::class.java
            ),
            requireActivity().application
    )
            .inject(this)

The trick here is to get `EntryPointAccessors.fromActivity` instead of fromApplication.

You can check out [this link][1] if you wish to experiment yourself.

If you wish to add `ViewModel` to dynamic feature modules with hilt you can check out my answer [here][2].

  [1]: https://github.com/SmartToolFactory/Dagger2-Tutorials/tree/master/Tutorial10-1DFM-DaggerHilt
  [2]: https://stackoverflow.com/questions/63671489/how-to-create-viewmodel-in-dynamic-feature-module-with-dagger-hilt

Comment: any idea how to inject to a viewmodel in DFM using hilt?
Comment: @YouQi, you can check out this [link](https://stackoverflow.com/questions/63671489/how-to-create-viewmodel-in-dynamic-feature-module-with-dagger-hilt) also edited answer


Prove: if $px^2 - (p+q)x + p = 0$ has an integer root, $px^2 + qx + p^2 - q = 0$ has an integer root too, for coprime $p,q \in \mathbb{N}$

Question: For $\mathbf{coprime}$ $p,q \in \mathbb{N}$, how can I prove that:
if equation
$$px^2 - (p+q)x + p = 0$$
has a root $x \in \mathbb{Z}$,
$$px^2 + qx + p^2 - q = 0$$
has an integer root as well?
Couldn't get a working idea.
Comment: use https://en.wikipedia.org/wiki/Rational_root_theorem
Comment: What is the source of the problem. Observe the expression of the discriminant of the second
Comment: @EmilioNovati It's not True . Try $p =2 , q= 3$
Comment: Sorry. Mi mistake!
Answer: In this answer, $p$ and $q$ are coprime integers such that $q\geq 0$ (and $p$ can be zero or negative).  We want to show that if $px^2-(p+q)x+p=0$ has an integer solution, then so does $px^2+qx+p^2-q=0$.
If $x=t$ is an integer root of $px^2-(p+q)x+p=0$, then
$$t(p+q-pt)=p.$$
If $t=0$, then $p=0$.  Because $\gcd(p,q)=1$, this means $q=1$.  Then, the equation $px^2+qx+p^2-q=0$, which is equivalent to $x-1=0$, has an integer solution $x=1$.
From now on, suppose that $t\neq 0$.  That is, $p=st$ for some integer $s$.  Hence,
$$p+q-pt=\frac{p}{t}=s.$$
Therefore,
$$q=s+p(t-1)=s+st(t-1)=s(t^2-t+1).$$
Therefore, $s$ divides $p$ and $q$, which implies $s=\pm1$ as $p$ and $q$ are coprime.  Since $q\geq 0$, we get $s=1$.
Therefore, $p=t$ and $q=t^2-t+1$.  Hence, $$px^2+qx+p^2-q=tx^2+(t^2-t+1)x+\big(t^2-(t^2-t+1)\big)$$ so that
$$px^2+qx+p^2-q=tx^2+(t^2-t+1)x+(t-1)=(tx+1)(x+t-1).$$
That is, $x=1-t$ is an integer solution to the quation $px^2+qx+p^2-q=0$.
The condition $q\geq 0$ is necessary.  Note that $p=-2$ and $q=-3$ satisfies the condition that $px^2-(p+q)x+p=0$ has an integer solution, but $px^2+qx+p^2-q=0$ does not have an integer solution.
Comment: I know that.  I simply allow $p$ to be any integer, whereas $q$ must remain non-negative.
Comment: i'm so sorry if that wasn't clear, but i thought that $p,q \in \mathbb{N}$ says that both $p$ and $q$ would be natural
Comment: i've read the solution for a few times. could you please elaborate a bit about the step from $(tx+1)(x+t-1)$ to stating that $x=1-t$ is an integer solution to $px^2+qx+p^2−q=0$? thank you in advance
Comment: Well, if $(tx+1)(x+t-1)=0$, then what are possible values of $x$?
Comment: i see. what about the $x= \frac{-1}{t}$, though? why is it left out?
Comment: The question is whether there is an integer root.  I found one.  There is no need to look at the root $x=-1/t$.
Comment: yeah, completely missed that. guess i should finally get some sleep haha. thank you  a lot!
Answer: From the first equation, the product of the roots is 1. So they are both 1 or both -1.        If they are both 1, substituting x=1 into the first equation gives q=p.                        Since p and q are co-prime, p=1,q=1 or p=-1,q=-1                                              If they are both -1, substituting x=-1 into the first equatio gives q=-3p.                  Since p and q are co-prime, p=1, q=-3 or p=-1, q=3                                           In all four cases for p and q, substituting the values for p and q into the second equation gives solutions for x that are integers.
Comment: If we consider $p=2,q=3$ , we get $f(x) = 2x^2 -5x + 2 $ , which has  the integer solution as $x= 2$ which is clearly $\ne \pm1$
Comment: Moreover , $p,q \in \mathbb N.$
Comment: this doesnt follow the given requirements for $p, q$


Please help to convert SQL-query to Elasticsearch-query

Question: How to convert SQL-query like this
select IndexA.* from IndexA where IndexA.NAME like ('%stics%') and IndexA.ID in (1,2,3,4)

in Elasticsearch-query(v6.x) ?
Answer: Like this:
POST /IndexA/_search -d '
{
"query" : {
 "bool" : {
 "must" : [
    {
        "wildcard" : {
                "name" : "*stics*"
        }
    },
    {
        "terms" : {
            "_id" : ["1", "2", "3"]
        }
     }
   ]
  }
 }
}'

Answer: In ES7, this can be done simply as follows:
curl -H "Content-Type:application/json" -XPOST 127.0.0.1:9200/_xpack/sql/translate?pretty -d '{"query" : "select IndexA.* from IndexA where IndexA.NAME like ('%stics%') and IndexA.ID in (1,2,3,4)"}'


r Loop por condição em script

Question: É possível fazer um loop, a partir desses dados?
library(dplyr)

mes <- c("jan","fev","fev","mar","jan","nov","mar","mar","mar","jan","nov","maio","maio","maio","nov","nov","mar","jan","mar","jan")
a <- c(32.3,32.7,32.6,33.1,33.0,33.5,33.4,33.4,34.9,32.6,33.1,33.0,33.5,33.4,32.6,33.1,33.0,33.5,33.4,12)
b <- c(19.2,19.2,19.6,19.7,19.7,19.9,20.0,20.0,20.4,32.6,33.1,33.0,33.5,33.4,32.6,33.1,33.0,33.5,33.4,50)
c <- c(14.7,15.0,15.6,16.2,16.4,17.0,17.7,18.3,19.1,32.6,33.1,33.0,33.5,33.4,32.6,33.1,33.0,33.5,33.4,32)
d <- c(24.2,24.3,24.7,25.0,25.5,26.4,26.7,67.1,27.6,32.6,33.1,33.0,33.5,33.4,32.6,33.1,33.0,33.5,33.4,10)
e <- c(24.2,24.3,14.7,25.0,25.5,26.4,26.7,27.1,27.6,32.6,33.1,33.0,33.5,33.4,32.6,33.1,33.0,33.5,33.4,87)
f <- c(24.2,94.3,24.7,25.0,25.5,26.4,86.7,27.1,27.6,32.6,33.1,33.0,33.5,33.4,32.6,33.1,33.0,33.5,33.4,40)
g <- c(24.2,24.3,64.7,25.0,25.5,26.4,26.7,27.1,27.6,32.6,33.1,33.0,33.5,33.4,32.6,33.1,33.0,33.5,33.4,80)
h <- c(24.2,24.3,84.7,25.0,25.5,26.4,26.7,27.1,27.6,32.6,33.1,33.0,33.5,33.4,32.6,33.1,33.0,33.5,33.4,30)
i <- c(24.2,24.3,24.7,25.0,25.5,26.4,26.7,27.1,27.6,32.6,33.1,33.0,33.5,33.4,32.6,33.1,33.0,33.5,33.4,10)
temp <- data.frame(mes,a,b,c,d,e,f,g,h,i)
y <- c(1, 3, 5, 7,8,11,13,17,23)

Depois faço o agrupamento para obter o numero de observações n e as médias de toda a amostra:

temp.group <- temp %>%
  group_by(mes)%>%
  summarise(n = length(mes))

temp.g.mean <- temp %>%
  group_by(mes) %>%
  summarise_all((mean))

Depois disso, transforma-se em log e calcul-se a regressão:
log_temp <- apply(temp.g.mean[-1], 2, log)

model_list <- apply(log_temp, 1, function(x) lm(x ~ y))

coef_list <- t(sapply(model_list, coef))

model_smry <- lapply(model_list, summary)

R2_list <- sapply(model_smry, '[[', 'r.squared')
pval_list <- t(sapply(model_smry, function(LM){
  LM[['coefficients']][, 4]
}))

Dai, calculo uma taxa de aumento e crio uma equação:
coef_list <- data.frame(coef_list)
coef_list$r <- (exp(coef_list$y)-1)

string1 <- "mesf = mesi *"
string3<- "^t"

eq <- data.frame(coef_list$r)
eq$r1 <- eq[,1]+1
eq$s1 <- string1
eq$s2 <- round(eq[,2],digits = 5)
eq$s3 <- string3
eq$eq <- paste(eq$s1,eq$s2,eq$s3)

Por fim, junto tudo em um data.frame
temp.g.mean <- round(temp.g.mean[-1],digits = 1)
R2_list <- data.frame(round(R2_list, digits = 3))
pval_list <- data.frame(pval_list)
coef_list <- round(coef_list, digits = 5)

result <- data.frame(temp.group[,1], temp.g.mean, n = temp.group$n,
                     eq = eq$eq,
                     R2 = R2_list[,1],
                     pvalor = pval_list$y, a = coef_list[,1],
                     b = coef_list[,2], r = coef_list[,3])

Eu gostaria de fazer um loop desses passos, utilizando o data.frame temp, só que variando os números de coluna, com a condição de sempre calcular as colunas juntas em no minimo 3, e no máximo o número de colunas existentes.
o intuito de fazer isso é poder salvar no modelo da tabela results sempre os meses que teriam um mínimo de observações n=4
tem como ir fazendo esses loops e salvando no results um por um os dados retirados dessas análises, com a condição de tem no mínimo n=4 ?
Se puderem me dar um norte, já fico extremamente grato.
Comment: Não estou a perceber se quer correr regressões com todos os segmentos de 3 ou mais colunas consecutivas ou se quer começar com as 3 primeiras colunas e ir daí para a frente.
Answer: Isto está longe de ser uma resposta completa mas se quer ficar só com os meses com 4 ou mais linhas, pode começar por este código.
library(tidyverse)

temp.group <- temp %>%
  group_by(mes) %>%
  summarise(n = n()) %>%
  filter(n >= 4)

temp.g.mean <- temp %>%
  group_by(mes) %>%
  inner_join(temp.group, by = 'mes') %>%
  summarise_all((mean))

O data.frame final temp.g.mean tem só os meses com 4 ou mais linhas. Isso é feito com a função filter. Depois pode processar os dados como na pergunta.

r Loop por condição em script


ios - retrive selection field in Form by hyperoslo

Question: Link for Hyperoslo/Form : https://github.com/hyperoslo/Form#faq
[
  {
    "id":"group-id",
    "title":"Group title",
    "sections":[
      {
        "id":"section-0",
        "fields":[
          {
            "id":"employment_type",
            "title":"Employment type",
            "type":"select",
            "size":{
              "width":30,
              "height":1
            },
            "values":[
              {
                "id":0,
                "title":"Part time",
                "default":true

              },
              {
                "id":1,
                "title":"Full time"

              }
            ]
          }

        ]
      }
    ]
  }
]

the above code for design ui by json file.
I am facing issue on, i am unable to retrive data from type : "select" . I need, which option is selected in select field.
Comment: I don't understand what is your issue. You have a JSON object, you know the value that you need to grab, and how to grab values from a JSON object has already been explained *so many times*... So, what is your problem *exactly*, supposing you actually have one?
Answer: In your class derived from FORMViewController you can try to call the following code:
let selectedItemId = self.dataSource.values["employment_type"] as! Int
print(selectedItemId)

In console you will see id of selected item (0 or 1 in your case).


interpreting intercept in lme4 when having within and between subjects variables and interaction

Question: I am testing the effect of priority (which is a continuous within subjects factor) and viewing condition (which is a factor and a between subjects factor with 2 levels) on error. So the condition factor was coded with 0 being the free viewing condition and 1 being the fixed viewing condition. I am also investigating the interaction between priority and viewing condition. I used the following code:
Mag2 = lmer(Error ~ Priority*Condition + (1|Subject), data = Magnitude, REML = FALSE)
anova(Mag2)

And I get the following output about fixed effects:
Fixed Effects:
            (Intercept)                 Priority           Conditionfixed
                75.9227                  -0.4155                  -9.4225

Priority:Conditionfixed 0.2342

I know that since condition is a factor with two levels and I coded it in the above way, the (Intercept) refers to free condition while Conditionfixed refers to fixed condition. So I know by looking at the averages that I have more error in the fixed condition than in the free so i would expect the Conditionfixed value to be positive. But here is negative? Why is that? Am I interpreting it in a wrong way?
Thank you
Comment: ***Correction***: So the condition factor was coded with 1 being the free viewing condition and 2 being the fixed viewing condition.
Answer: Which averages are you comparing, exactly? More specifically, with which Reputation values?
Assuming free viewing is the reference condition, I think the answer may be in the interaction term: for each increment of Priority with 1 unit, there is a decrement of -0.4155 for the free viewing condition, while the decrement is limited to -0.4155 + 0.2342 = -0.1813 for the fixed viewing condition. So provided Reputation scores are sufficiently high, this difference could more than compensate the between condition intercept.
Comment: (+1) Nice answer. I have a few comments that I'll put in an answer..
Answer: This is more of a comment/expansion on the excelent answer by @KrisBae which I recommend you accept. This model has the following features:

Global intercept: 75.9

This is the expected value of Error when Priority is 0, in Free Condition group.

Main effect for Priority: -0.4155

This is expected change in Error for a 1 unit change in Priority (ie. the slope for Priority) in the Free Condition group.

Main effect for Condition: -9.4225

This is expected difference in Error between Condition = Free and Condition = Fixed, when when Priority is 0

The interaction term Priority:Condition: 0.2342

This is the expected difference in the slope for Priority between the two  Condition s), so that the slope for those in the Fixed Condition group is -0.4155 + 0.2342 = 0.1813

It might make more sense for your interpretation if you centre the Period variable (depending on it's range - I guess it might be centred or otherwise transformed already). If you centre it, say at the grand mean, then rather than the other estimates being conditional on Period being zero, they are conditional on Period being at it's mean.
Comment: Please take your time to read through @Robert Long s crystal clear explanation. E.g., the intercept value does not reflect the average for your free viewing condition disregarding priority (unless you centered it), it reflects the pedicted value in that condition when priority = 0.


Swift: Load Data from Realm Duplicate Sections in TableView?

Question: I've been trying to solve this for a couple of days now. Whenever I add a new contact and load it from Realm, a new section is created with the same header. Every section has only one row and cannot accommodate more. I'm trying to accomplish functionality like the contacts app on the phone. I want every section to hold as many names (rows) as possible with unique names.
Here's my code:
class ArtistNamesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var contacts: [Contact] = []
var contactsArray = [[Contact]]()

override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self

        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(didTapAdd))

        loadArtists()
    }

func setupContacts() {
        let groupedContacts = Dictionary(grouping: contacts) { (contact) -> Character in
            return contact.name.first!
        }
        let keys = groupedContacts.keys.sorted()
        keys.forEach { (key) in
            contactsArray.append(groupedContacts[key]!)
        }

    }
}

//MARK: - Realm Methods

func uploadContacts(contactName: String, contactNumber: String) {

    let contact = ContactRealm()
    contact.artistName = contactName
    contact.artistPhoneNumber = contactNumber

    let realm = try! Realm()
    try! realm.write {
        realm.add(contact)
    }
}

func loadArtists(){

    let contactsRealm = realm.objects(ContactRealm.self)

    for contact in contactsRealm {
        if let name = contact.artistName,
           let number = contact.artistPhoneNumber {
            self.contactsArray.append([Contact(name: name, number: number)])
        }
    }

    self.tableView.reloadData()
}

//MARK: - TableView Methods

func numberOfSections(in tableView: UITableView) -> Int {
    return contactsArray.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return contactsArray[section].count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "ArtistNamesCell", for: indexPath) as! ContactsTableViewCell
    cell.configureName(with: contactsArray[indexPath.section][indexPath.row].name)
    cell.configureNumber(with: contactsArray[indexPath.section][indexPath.row].number)
    return cell
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    let title = String(contactsArray[section].first?.name.prefix(1) ?? "")
    return title
}

//MARK: - Segue

@IBAction func goToContacts(segue: UIStoryboardSegue) {

    guard let viewController = segue.source as? AddArtistViewController else { return }
    guard let name = viewController.name.text,
          let phoneNumber = viewController.phoneNumber.text else {
              return
          }

    contactsArray = []
    let contact = Contact(name: name, number: phoneNumber)
    contacts.append(contact)
    uploadContacts(contactName: name, contactNumber: phoneNumber)
    setupContacts()
    tableView.reloadData()
}
}

ContactRealm Class
class ContactRealm: Object {
    @objc dynamic var artistName: String?
    @objc dynamic var artistPhoneNumber: String?
}

Contact Class
import Foundation

struct Contact {
    let name: String
    let number: String
}

Thank you in advance.
Answer:
Because you are returning multiple sections in
func numberOfSections(in tableView: UITableView) -> Int {
   return contactsArray.count // HERE ITS RETURNING THE A, B , C , D MULTIPLE TIMES.
}

Solution 1: // Bit Complex

Return the unique Alphabet Set/ Array from

setArray.append(String(contactsArray[section].first?.name.prefix(1) ?? ""))

return this setArray.count in numberOfSections.

in numberOfRowsInSection filter out array with items and return which are present with Alphabet. e.g let rowsCount = contactArray.filter({$0.name. prefix(1) == setArray[section] })

return row which belongs this group in cellForRowAt.let contact = contactArray.filter({$0.name. prefix(1) == setArray[indexpath.section]})[indexpath.row] as? Contact

Solution 2:

Create Unique dictionary that maintains Alphabet and contact objects.
e.g let contactDict = [ 'A': [Contact(name: name, number: number), Contact(name: name, number: number)]], 'B': [Contact(name: name, number: number), Contact(name: name, number: number)]] // This list you can update dynamically once you received contact from database.

return this Array(contactDict.keys).count in numberOfSections.

in numberOfRowsInSection return let rowsCount = contactsDict[Array(contactsDict.keys).sorted()[section]].count

return row which belongs this group in cellForRowAt.let contact = contactsDict[Array(contactsDict.keys).sorted()[section]][indexpath.row]
Comment: Hello. What do you mean by the fourth point in solution 1 "return row..."? Do I copy and paste the code in cellForRowAt? I'm already returning cell, how do I return row?
Comment: Yes return cell with content that's it
Comment: `setArray` is an empty array of type `String`, right? I created it like this: `var setArray = [String]()`
Answer: It was solved using this extension:
public enum SortingOrder {
    case ascending
    case descending
}

public extension Sequence {
    func group<K: Hashable & Comparable>(
        by keyForValue: (Element) -> K,
        sortOrder: SortingOrder = .ascending) -> [[Element]] {
        Dictionary(grouping: self, by: keyForValue)
            .sorted { sortOrder == .ascending ? $0.key > $1.key : $0.key < $1.key }
            .map { $0.value }
    }
}

And adding...
contactArray = contacts.group(by: { $0.name.first ?? "-" }, sortOrder: .descending) to loadArtists()
Also, setupContacts() has been changed to:
contactArray = contacts.group(by: { $0.name.first ?? "-" }, sortOrder: .descending)


"Experimental modules found: Place blocks" but I am using core 8.6

Question: I am using Drupal 8.6.2. When I open the status report I see the following warning:

Experimental modules found: Place Blocks. Experimental modules are provided for testing purposes only. Use at your own risk.

I researched this issue and found that place blocks has been part of Drupal Core since 8.2.
When I check the list of installed modules then I see no reference to a module named place blocks or block-place. I also ran drush pm-list and grepped for place and block. Is it possible that my database contains some outdated dangling reference to this module? How can I clean up my Drupal installation and get rid of this warning?
Answer: The module is present since Drupal 8.2, but since Drupal 8.4 it has been marked hidden. That explains why you get the error, but you don't find the module: The module was enabled before updating to Drupal 8.4 or later, and it is still installed; once Drupal was updated to any version after 8.4, the module has been just marked as hidden (without changing its status of installed module).
If you look at the content of the block_place.info.yml file (core/modules/block_place/block_place.info.yaml), you will notice the following lines.
package: Core (Experimental)
version: VERSION
core: 8.x
hidden: true

If you remove the hidden: true line, and visit admin/modules to install a module that you then uninstall, you will see that module listed in /admin/modules/uninstall, and you will be able to uninstall it.
Installing and uninstalling a module is done just to force Drupal to clear the cache it uses to keep the list of found modules. In this case, Drupal will notice the module is not marked anymore as hidden, and it will show it again.
Comment: Thanks that worked. Btw. path to the file is `core/modules/block_place/block_place.info.yml`.
Comment: I should have added that to the answer, to make it easier to find it.


Non determinant -class operators satisfing $A^2+I=0$

Question: Let  $H$  be  a  complex Hilbert space and  $A\in B(H)$ satisfies $A^2+I=0$. Does this implies that $A$ can be written in the form $A=\lambda I +T$ where $T$ is a trace classe operator  and $\lambda$ is  a complex scalar?
Answer: I think not: Define $A:l^2\to l^2$ by
$$
Ax = i  (x_1,-x_2,x_3,-x_4,\dots).
$$
Then $A^2=-I$, but $A-\lambda I$ does not have finite trace for all $\lambda$.


Cookie Id of one user copied from one browser to another browser - its hijacked - how to solve this issue

Question: I am logging as a user1 in chrome browser with valid credentials and I am logging as a user2 in IE browser. I am taking user2's cookie id from IE and pasting it in user1's Cookie id i.e (just deleting user1's cookie id and replaced with user2's cookie id). Now I am able to get the user2's account detail in chrome browser. Is there a way how to solve this issue?
Even if we manually copy the cookie from one browser to another browser - It should not hijack the user details of another browser.
Comment: Can you provide some more information? e.g. how you create the cookies... On the [ask] page you get some tips to increase your question.
Answer: Make sure you use the HttpOnly flag on your cookies, use data such as client ip and perhaps user agent to provide some variables to check against at the server, if something has changed then have them re-authenticate and only transmit over https.
Edit: Also if you are doing this on the same machine with the same user account remember that this isn't the best test of your web applications security, if someone got on my user account the browser would already have tonnes of remembered passwords for websites that a nefarious user could use to log in as me. If you transmit over https then data between client and server is encrypted and the risk is minimal for interception, if the hacker can get into you machine it becomes a whole new ball game


Prove that if $f$ is continuous then $\Vert f\Vert$ is continuous. Is the converse true?

Question: I need help with this problem:

Given a function $f:D\subset\mathbb{R}\rightarrow\mathbb{R}^n$, consider the function $\Vert f\Vert :D\subset\mathbb{R}\rightarrow\mathbb{R}$ where $\Vert f\Vert (t) = \Vert f(t)\Vert, t\in\mathbb{R}$. Prove that if g is continuous the $\Vert f\Vert$ is continuous. Is the converse true?

I don't understand this problem. I think that the function $\Vert f\Vert :D\subset\mathbb{R}\rightarrow\mathbb{R}$ takes the norm of every number plugged in, and we get the same result that the function $\Vert f(t)\Vert$ would give us, which is taking the norm of the function. How do I prove this problem? I tried to use limits from both sides, but I don't have any function to apply them to.
Comment: $f(x)=\boldsymbol 1_{\mathbb Q}(x)-\boldsymbol 1_{\mathbb R\setminus \mathbb Q}(x)$ is discontinuous everywhere but $|f|$ is continuous.
Answer: Hint: By the reverse triangle inequality we know that $|(||x||-||y||)|\leq ||x-y||$ for all $x,y\in\mathbb{R^n}$. Using this you can prove that a norm is continuous, and then $||f||$ will be just a composition of continuous functions.
The converse is obviously false even if $n=1$. Just take the function $D:\mathbb{R}\to\mathbb{R}$ to be $D(x)=1$ if $x\in\mathbb{Q}$, otherwise $D(x)=-1$. This function is nowhere continuous, but $|D|$ is just a constant.
Comment: Maybe it will help to write $f(x)=\|x\|.$ Then, $f$ is continuous at $x_0$ iff for all $\epsilon>0$ there is a $\delta>0$ such that if $\|x-x_0\|<\delta$, then $|f(x)-f(x_0)|<\epsilon.$ Now unpack this and you'll see the reverse triangle inequality is exactly the condition you need for this to hold. i.e. take $\delta=\epsilon. $ It shows in fact, that the continuity is uniform.
Comment: I don't understand why $|(||x||-||y||)|\leq ||x-y||$ implies that the norm is continuous.
Comment: Let $\epsilon>0$. Then you can take $\delta=\epsilon$ and you get that if $||x-y||<\delta$ then $|(||x||-||y||)|\leq ||x-y||<\delta=\epsilon$. So the norm is even uniformly continuous.
Comment: And how do I use that to prove that the norm is continuous?
Comment: If a function is uniformly continuous then of course it is continuous. There is nothing left to prove.
Comment: Oh, I see thanks.
Comment: Thanks for the clarification @Matematleta.
Answer: For the first part, note that $\|x-y\|\ge |\|x\|-\|y\||$ so the norm $\|\cdot \|:\mathbb R^n\to \mathbb R$ is continuous. And now, since $g$ is also continuous, so is $\|g\|$, being a composition of continuous functions.
For the second part, take $ g(x) = \begin{cases}
          -1 & x< 0 \\
          1 & x\ge 0 \\
       \end{cases}$
Then, $|g|$ is continuous but $g$ is not.
Comment: Why $\|x-y\|\ge |\|x\|-\|y\||$ implies thaat the norm $\|\cdot \|:\mathbb R^n\to \mathbb R$ is continuous?


Difference between Base, Schema, and Migration in ActiveRecord

Question: I am currently trying to use the activerecord gem in Ruby but I'm quite confused on how to use the gem. What's the difference of ActiveRecord::Base, ActiveRecord::Schema and ActiveRecord::Migration? And when do I use them?
Comment: @Sergio No im not familiar with rails. Im currently using activerecord with only ruby. Im not using rails since Im not working on a website.
Answer: To sum up : AR::Base is the skeleton for a model. Your models usually are classes that derives from this one. AR::Schema is, well, for the schema of your db. You can see what it looks like in the file db/schema.rb. As for AR::Migration, it's in the name too : it's the skeleton for the migration files that you can find in db/migrate. You can check the rails guides & api reference to have more information on them.
Comment: I agree with the answer but I'm not sure the paths given are valid outside a rails context.
Comment: You're right. Didn't notice the question was about "pure" ruby.
Comment: I think the path wont matter to me. what i'm after is just for the meaning and when to use it.


Finding transfer function based on Nyquist Diagram

Question: I have a Nyquist Diagram that looks like this:

The angular frequency at point A is $\sqrt 2$ rad/s. Now I would like to derive the transfer function based on the diagram. Now, I am given the choices of the open-loop transfer function as below:
\begin{align}
  (a)L(s) = \dfrac{s+k}{s(s^2+as+b)}\\
  (b)L(s) = \dfrac{k}{s(s^2+as+b)}\\
  (c)L(s) = \dfrac{s+k}{s^2+as+b}\\
  (d)L(s) = \dfrac{k}{s^2+as+b}
\end{align}
Currently, as far as I know, the place on the real-axis with the number 3 should have $\omega$ = 0. If I replace $L(s)$ with $L(j\omega)$ and put in $\omega$ with 0, then options (a) and (b) will not satisfy because the results of them will be infinity. However, I am not certain how to derive the full transfer function in a standard way and find k, a, b. I have tried to use (c) and (d) and replace $\omega$ with $\sqrt 2$, but it failed either.
This is how I did when trying to get the transfer function based on option (c) and (d):
\begin{equation}
  L(j\sqrt 2) = \dfrac{k}{(b-2)+ja\sqrt 2} * \dfrac{(b-2)-ja\sqrt 2}{(b-2)-ja\sqrt 2} = \dfrac{k[(b-2)-ja\sqrt 2]}{(b-2)^2 + 2a^2} = 0 - j\dfrac{3}{4}\sqrt 2
\end{equation}
\begin{equation}
   L(j\sqrt 2) = \dfrac{k + j\sqrt 2}{(b-2)+ja\sqrt 2} *  \dfrac{(b-2)-ja\sqrt 2}{(b-2)-ja\sqrt 2} = \dfrac{[k + j\sqrt 2][(b-2)-ja\sqrt 2]}{(b-2)^2 + 2a^2} = \dfrac{k(b-2) + 2a - j(k-b+2)\sqrt 2}{(b-2)^2 + 2a^2} = 0 - j\dfrac{3}{4}\sqrt 2
\end{equation}
From option (d), it looks like k, a, and b should satisfy the following equations:
\begin{equation}
  \begin{cases}
    k(b-2) + 2a = 0\\
    (b-2)^2 + 2a^2 = 4\\
    k-b = 1
  \end{cases}
\end{equation}
With option (c), this seems not to make sense because I think a could not be an imaginary number in this case. As for option (d), the equation, there seems no value pairs meet at the same time..., What did I miss?
Please help me with this question. Thank you for any advice.
Comment: You have correctly eliminated option (a) and (b). But have you also attempted to eliminate either option (c) or (d) as well? And can you also show your attempt at solving for the transfer function in case of option (d)?
Comment: Thank you for your comment. Yes, I have reviewed the options (c) and (d). Would you mind seeing if there is anything wrong with my calculation?
Comment: By equating two fractions with each other it is not required that each numerator and denominator are equal. Namely, when multiplying both the numerator and the denominator by the same coefficient it can still be the case that the two fractions are equal. Instead try to equate the two fractions directly to each other.
Comment: Also don't forget to use $L(0)=3$.
Comment: For option (d) you need $b-2=0$ not $k(b-2)+2a=0$.
Comment: Thank you for advices. I will check the equation one more time.
Answer: With options (a) and (b),
$$
    \begin{align*}
        L_a(jω) & = \dfrac{jω + k}{jω(-ω^2 + jaω + b)}, L_b(jω) = \dfrac{k}{jω(-ω^2 + jaω + b)} \\
        L_a(j0) & = ∞, L_b(jω) = ∞ \\
    \end{align*}
$$
Therefore, options (a) and (b) do not correspond to the graph.
With option (c) and (d),
$$
    \begin{align*}
        L_c(jω) & = \dfrac{jω + k}{(b-ω^2) + jaω} * \dfrac{(b-ω^2) - jaω}{(b-ω^2) - jaω} = \dfrac{(jω + k)(b-ω^2-jaω)}{(b-ω^2)^2 + (aω)^2} \\
        L_d(jω) & = \dfrac{k}{(b-ω^2) + jaω} * \dfrac{(b-ω^2) - jaω}{(b-ω^2) - jaω} = \dfrac{k(b-ω^2-jaω)}{(b-ω^2)^2 + (aω)^2} \\
    \end{align*}
$$
With $ω = \sqrt 2, L_c(j\sqrt 2)$ has a real part, hence the possible solution is (d)
Based on option (d)
$$
    \begin{align*}
        L_d(jω) & = \dfrac{k}{(b-ω^2) + jaω} * \dfrac{(b-ω^2) - jaω}{(b-ω^2) - jaω} = \dfrac{k(b-ω^2-jaω)}{(b-ω^2)^2 + (aω)^2} \\
        L_d(j0) & = 3 = \dfrac{k}{b} \\
        L_d(j\sqrt 2) & = 0 - j\dfrac{3}{4}\sqrt 2 = \dfrac{k(b-2-ja\sqrt 2)}{(b-2)^2 + 2a^2}
    \end{align*}
$$
Therefore,
$$
    \begin{align*}
        \begin{cases}
            k = 3b \\
            b - 2 = 0 \\
            \dfrac{-jka\sqrt 2}{2a^2} = -j\dfrac{3}{4}\sqrt 2
        \end{cases}
    \end{align*}
$$
$$
    b = 2, k = 6, a = 4
$$


How to use connect-proxy on CygWin

Question: I am trying to run a node.js program behind a corporate firewall. However, I am unable to explicitly tell node.js which proxy to use and therefore all my external connections time out.
I read on a some post that I could use connect-proxy as an HTTP proxy for my tunneling needs, but I have no idea how to actually use it.
I want to run the following:
$ node program.js
using connect-proxy.
The only command I was able to get so far is this:
$ connect-proxy -H myproxy.com:8083 google.com
GET
HTTP/1.0 302 Found
Location: http://www.google.com/
...
Answer: Before going further it is worth trying the environment variable that a number of other languages and tools support.
export http_proxy=http://proxyserver:port
Often they use port 8080 but check the Javascript in the PAC file loaded by your browser to be sure.
If that produces a different result but still doesn't connect, you probably need to do NTLM auth with the proxy and the only way I know to do this is to run NTLMAPS before running your app. If you are really interested in getting this working transparently, then porting NTLMAPS to Javascript should do the trick.

How to use connect-proxy on CygWin


Why does list syntax after a header treat the contents as code?

Question: I am relatively new to markdown but when I type in the following:
<code># Here is a header
    * here is some stuff about the header
    * some more stuff
</code>
And this gives me something like:

when I put it into github.
Why is this happening?
Comment: because you put 4 spaces in front of the asterisks. Remove the spaces
Answer: Because you put 4 spaces in front of the asterisks. 4 leading spaces at the current indent level = code block. Remove the spaces (or put less then 4).
It's the same on stackoverflow
<code># Here is a header
    * here is some stuff about the header
    * some more stuff
</code>
produces
Here is a header
<code>* here is some stuff about the header
* some more stuff
</code>
where as
<code># Here is a header
* here is some stuff about the header
* some more stuff
</code>
produces
Here is a header

here is some stuff about the header
some more stuff

as does
<code># Here is a header
   * here is some stuff about the header
   * some more stuff
</code>
--
Here is a header

here is some stuff about the header
some more stuff


Hypothesis testing for the sum of 100 tosses of a fair die

Question: Suppose you have a fair 6-sided die. You toss the die 100 times and sum up the values. I claim that the sum of the 100 tosses is 400. Will you believe me?
I don't know of a good way to compute the probability of the sum of 100 tosses being 400. The only exact way that I know of is using generating functions, but I'm not well versed in that area. An approximate method is to use CLT and integrate a normal distribution with a continuity correct factor. A monte carlo simulation with 1 million trials shows that the probability is about 0.0003. Let's just pretend this is the exact result since that's not the focus of this question.
I would like to apply hypothesis testing to this problem, but I'm kind of confused on what the null and alternate hypothesis should be. In the few times that I've worked with hypothesis testing, it was to test against the null hypothesis that the mean of the distribution is true, which in this case is 350. But it seems we're not testing means here. We're testing whether my claim that the sum of 100 tosses is 400 is believable.
In my case should the null hypothesis be $H_0 : Y = X_1 + \ldots + X_{100} = 400$ where $X_i \sim U( 1, 6)$. If so, then the alternate hypothesis is $H_a: Y \neq 400$. If this is the case, then my p-value is going to be almost 1 since there's a very small probability that we're going to see $Y = 400$. In the times that I've used hypothesis testing, we want a smaller value to reject a null hypothesis. But with the way things are defined here, it seems we'd want a higher p-value. The other issue is I don't actually appear to be conducting any experiments for my test statistic, $Y$. All I'm doing is saying that there's a tiny chance $Y = 400$, hence we should reject the null hypothesis.
So, I guess the questions that I have are:
(1) What should the null and alternate hypothesis be here?
(2) What does the p-value in this case represent, and do we desire a smaller or higher p-value in this situation? (I think the answer is dependent on the answer to (1)).
(3) This is probably a dumb question, but my proposal above doesn't actually involve computing a value for the test statistic, $Y$. Like, e.g., in another hypothesis test we could claim that $H_0: \mu_{Y} = 350$ and $H_a: \mu_Y \neq 350$ and observe that $Y = 400$, and then use this $Y = 400$ to compute our p-value using a two-tailed test. Do we need some realization of $Y$ for the current problem?
Answer: Two useful approximations:
(1) A normal approximation to the sum of 100 rolls of a die based on $\mu = 3.5, \sigma^2 = 2.916667$ for one roll should give a useful answer.
[Related Q&A.]
(2) A simulation in R for the sum $S_{100}$ of 100 rolls gives the following results, showing that a sum as large as 400 would be very unlikely.
set.seed(513)
s = replicate(10^6, sum(sample(1:6, 100, rep=T)))
summary(s)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
    271     338     350     350     362     431
mean(s >= 400)
[1] 0.001821

A histogram of a million simulated results illustrates that $P(S_{100} \ge 400)$ is small, and
that a normal approximation is useful.
hist(s, prob=T, br=30, col="skyblue2", main="Sum of 100 Rolls of a Die")
 abline(v = 400, lwd=2, lty="dotted")
 curve(dnorm(x, mean(s), sd(s)), add=T, col="orange", lwd=2)

Comment: Hmm, but I think this hypothesis test that you did is asking the question if the sum of 100 tosses is significantly *more than* 350 or not? In other words, $H_0: S_{100} = 350$ and $H_a: S_{100} > 350$?
Comment: I purposely did not frame my answer as a test of hypothesis. Strictly speaking the hypothesis should come before the observation. "I got 400. Will you believe me?" does not lead to a proper test. I don't doubt you could have gotten 400<600, if you say so. (I might wonder if your die is fair and randomly tossed.) In my simulation of a million 100-roll experiments I got max 431. and exactly 334 times in a million I got exactly score 400. All I'm saying is someone who's contemplating doing 100 careful rolls of a fair die has a very small chance of getting a score 400 or more


How to unset environment variables from script

Question: if I need to unset an environment variable from script it used to be "sourced" like
. ./myscript.sh

or
source ./myscript.sh

But how do i make this work, if i launch this script from supervisord?
[program:myapp]
command=source /usr/bin/myscript.sh
...

does not work.
Simple example script (myscript.sh):
#!/bin/bash
unset -v SOME_VAR

That's all ...
Comment: sure execution works fine, but 'unset' of environment vars doesn't
Comment: Why don't you post the content of you script, starting with the missing interpreter
Comment: I bet `command=/usr/bin/bash -c "source /usr/bin/myscript.sh"` works.
Answer: You wouldn't generally /run/ a script by using source, instead you'd make it executable and then run it using it's path.
However, to specifically unset a variable just use unset:
$ TEST=foo
$ echo $TEST
foo
$ unset TEST
$ echo $TEST

$

Comment: Sorry, but this is not what I asked for. I know how to unset vars from command-line. Instead I am looking for a way to unset vars from within a script. Therefore the . (or source) is normally the best practice.
Comment: Sorry, Jacob but this is not true!
Comment: It's the same thing...
Answer: It works for me.
root@localhost:~# cat set.sh
hello1=world1
hello2=world2

root@localhost:~# cat unset.sh
unset hello1
unset hello2

root@localhost:~# . set.sh
root@localhost:~# env | grep ^hello
hello1=world1
hello2=world2
root@localhost:~# . unset.sh
root@localhost:~# env | grep ^hello
root@localhost:~#


Installing python development package on RHEL

Question: I have been trying to install python development package on my red hat machine but not able to find the package any where.
My server is a Red Hat Enterprise Linux Server release 6.8 (Santiago)( x86_64 GNU/Linux) with python 2.6.6 on-board
The repo list dose not include the required package and i have no access to alter it also.
I have tried doing a "yum install python-devel" and "yum search .." but have not been successful.
From where can i grab hold of this package? Any external link available?
Answer: 1.-> yum install epel-release
After that ;
<code>[root@srv1 ~]# yum search python-devel

Loaded plugins: fastestmirror, priorities
Loading mirror speeds from cached hostfile
 * base: mirror.daniel-jost.net
 * epel: mirror.daniel-jost.net
 * epel-debuginfo: mirror.daniel-jost.net
 * epel-source: mirror.daniel-jost.net
 * extras: mirror.daniel-jost.net
 * remi-safe: mirror.23media.de
 * updates: mirror.daniel-jost.net
============================================================= N/S matched: python-devel ==============================================================

+dbus-python-devel.i686 : Libraries and headers for dbus-python
+dbus-python-devel.x86_64 : Libraries and headers for dbus-python
+gstreamer-python-devel.i686 : Headers for developing programs that will use gstreamer-python
+gstreamer-python-devel.x86_64 : Headers for developing programs that will use gstreamer-python
+nemo-python-devel.x86_64 : Python bindings for Nemo
+ ---->>>>> python-devel.x86_64 : The libraries and header files needed for Python development
+qscintilla-python-devel.noarch : Development files for QScintilla PyQt4 bindings
+wxPython-devel.x86_64 : Development files for wxPython add-on modules
</code>


How to address nonlinearities among covariates when modelling?

Question: Generally, it is recommended to drop one variable from modelling when we found any collinearity among two variables.
But what when two or more independent variables have nonlinear relationship. Can they all be used in same model as independent variable?
Answer:
Generally, it is recommended to drop one variable from modelling when we found any collinearity among two variables.

Recommended by whom exactly?  Collinearity between explanatory variables is a ubiquitous part of statistical modelling, and it is not generally the case that you would drop a variable just because of collinearity with another variable.  Indeed, if the advice you have stated were to be followed, we would never use more than one explanatory variable in any model.
Regression models allow you to have two or more explanatory variables that have non-zero collinearity.  So long as the set of explanatory variables are linearly independent you still get an identifiable linear model and you can estimate the relationships between the explanatory variables and the response.  Collinearity will have the effect of inflating the standard error of your estimators, and in cases where explanatory variables are strongly collinear, it is difficult to separate their "effects" and thus, the standard errors are inflated a lot.  This is not fatal to the model, and you will still often get reasonable predictions from your model in these cases.  It just means that it is difficult to demarcate the "effects" of variables that are strongly collinear.
Answer: Correlation among regressors is almost always the case when working with economic variables. It only becomes problematic when the variables are perfectly collinear, i.e. one regressor can be written as a linear combination of other regressors, meaning $(X'X)$ is is singular so $(X'X)^{-1}$ does not exist. If the variables are related in a nonlinear fashion, e.g. one regressor is another regressor squared, the collinearity problem is not an issue.
Comment: Okay, thank you a lot, are there any literature that deals with it in details? Because, correlation doesn't say about relationship, it may happen that there is nice correlation but strong curvilinear relationship. Wouldn't it affect the regression coefficients?


Python Modular Inheritance

Question: I'm trying to do the following:
File 1:
  class x:
    def somefunc(self):
      # Some code
      ect...

File 2:
  import File 1
  # Inherits x
  class y(File1.x):
      # Some code
      ect...

But this raises an error:

"name 'x' is not defined"

Edit: Changed x to File1.x.
Still not working
Comment: You'll need to give us the proper exception here; you will no longer have a `NameError` in your edited code.
Comment: @Tracing This is a bit of a wild guess, but maybe you are having problems because of spaces in your file names? See my answer below, I edited it to include some more information about this.
Answer: You imported the module into your namespace; x is an attribute of the module:
import modulename

class y(modulename.x):

Alternatively, use the from modulename import syntax to bind objects from a module into your local namespace:
from modulename import x

class y(x):

Answer: You need to do from file1 import x or class y(file1.x) to make this work.
EDIT: Make sure you have no spaces in your file names. Maybe it's just a typo in your question, but at the top of File2 you are saying import File 1 instead of import File1. If the name of your Python module corresponding to File1 does indeed contain one or more spaces, you should remove them (or replace them with underscores), both in the file name and the import statement. As explained in the accepted answer to this question, file names are used as identifiers for imported modules, and Python identifiers can't contain spaces.

Python Modular Inheritance


Caching in Spark

Question: A function is defined to transform an RDD. Therefore, the function is called once for each element in the RDD.
The function needs to call an external web service to look up reference data, passing as a parameter data from the current element in the RDD.
Two questions:

Is there an issue with issuing a web service call within Spark?
The data from the web service needs to be cached. What is the best way to hold (and subsequently reference) the cached data? The simple way would be to hold the cache in a collection with the Scala class which contains the function being passed to the RDD. Would this be efficient, or is there a better approach for caching in Spark?

Thanks
Answer: There isn't really any mechanism for "caching" (in the sense that you mean). Seems like the best approach would be to split this task into two phases:

Get the distinct "keys" by which you must access the external lookup, and perform the lookup once for each key
Use this mapping to perform the lookup for each record in the RDD

I'm assuming there would potentially be many records accessing the same lookup key (otherwise "caching" won't be of any value anyway), so performing the external calls for the distinct keys is substantially faster.
How should you implement this?

If you know this set of distinct keys is small enough to fit into your driver machine's memory:

map your data into the distinct keys by which you'd want to cache these fetched values, and collect it, e.g. : val keys = inputRdd.map(/* get key */).distinct().collect()
perform the fetching on driver-side (not using Spark)
use the resulting Map[Key, FetchedValues] in any transformation on your original RDD - it will be serialized and sent to each worker where you can perform the lookup. For example, assuming the input has records for which the foreignId field is the lookup key:
val keys = inputRdd.map(record => record.foreignId).distinct().collect()
val lookupTable = keys.map(k => (k, fetchValue(k))).asMap
val withValues = inputRdd.map(record => (record, lookupTable(record.foreignId)))

Alternatively - if this map is large (but still can fit in driver memory), you can broadcast it before you use it in RDD transformation - see Broadcast Variables in Spark's Programming Guide

Otherwise (if this map might be too large) - you'll need to use join if you want keep data in the cluster, but to still refrain from fetching the same element twice:
val byKeyRdd = inputRdd.keyBy(record => record.foreignId)
val lookupTableRdd = byKeyRdd
  .keys()
  .distinct()
  .map(k => (k, fetchValue(k))) // this time fetchValue is done in cluster - concurrently for different values
val withValues = byKeyRdd.join(lookupTableRdd)

Comment: Thanks Tzach. Presumably the best place to hold the cache (driver side) would be in a scala object. To clarify, are you saying that if the cache is large, prefer a broadcast variable because otherwise there would be a performance overhead in sending the cache to each transformation mapping function? Note that the cache will not be static, but will grow with the processing of different requests (each request will be processed as a separate RDD). Would the the broadcast variable still be a better approach (for a large cache) despite the overhead of constantly updating the broadcast variable?
Comment: Oh - I see. Yes, broadcast is only better because it gives you control of when the variable is sent - if you have to re-send it over and over anyway, then it probably wouldn't be of much value.
Comment: Do you think the overhead of sending a potentially large cache to the RDD could be an issue? It would likely only be sent once for the processing of each RDD.
Comment: It could - depending on its size, and on your expectation... e.g. if your jobs takes 30 minutes, this serialization would probably be negligible. If it takes 40 seconds, then by comparison this might be significant.
Comment: With the second answer Tzach (data stored on the cluster) - would that allow for one global cache which all nodes would be writing to and reading from ?
Comment: Not really - all nodes would hold partitions of that "cache", which won't behave like a cache at all, but like an RDD that you create _once_ and then _join_ however many times you want. So if an item from this cache is needed by a record in another node, that would involve a shuffle operation.
Comment: would that shuffle happen automatically?
Comment: of course. It's a join, read more about Spark joins [here](http://spark.apache.org/docs/latest/programming-guide.html#shuffle-operations)

Caching in Spark


Why is kernel said to be in process address space?

Question: This might be a silly question but it just popped up in my mind. All the text about process address space and virtual memory layout mentions that the process address space has
space reserved for kernel. For e.g. on 32 bit systems the process address space is 4GB of which 1 GB is reserved for kernel in Linux (Might be different on other OS).
I am just wondering why kernel is said to be in the process address space when a process cannot address the kernel directly. Why don't we say that the kernel has a separate address space than a process and why can't we have a different page table for kernel itself which is separate from the page tables of the processes?
Comment: I was just giving an example that some space is reserved for kernel. The specifics are not as important to the question.
Comment: Can you identify one or more specific URLs which describe how the process address space has space reserved for the kernel?  It certainly isn't universal; I'm tolerably certain that there are 32-bit systems (not necessarily Linux-based) using more than 3 GiB of shared memory in a single process, which doesn't leave 1 GiB for the kernel.
Answer: When the process makes a system call, we don't need to switch the page tables (from process address space page table to kernel address space page table) for servicing the system call (which should be done only in kernel mode). This is said to be that the kernel is running in the process context.
Some kernel events which won't run in process context will load the page tables only for kernel.
Got it ?
Comment: One update to this since things have changed a little since this question was posted and I just stumbled across it, so others may too.  :)  What's described in this answer is not entirely the case anymore.  A vulnerability called MELTDOWN was discovered, which resulted in the ability to access kernel pages in the page table.  This resulted in the kernel switching to using two page tables for each process, one with the kernel mapped (for syscalls) and one without (regular process space).  Depending on the CPU, the TLB may get automatically flushed now, or just entries related to that process.
Comment: ok. but how badly switching pages will affect performance. also according to https://flylib.com/books/en/188.8.131.52/1/ OSX does exactly that. "Mac OS X does not map the kernel into each user address space, and therefore each user/kernel transition (in either direction) requires an address space switch" and it doesn't seem extra slow to me.
Comment: TLB (Transition Lookaside Buffer) would get reset for every page-table switch, and that's a lot of waste time.


How can i display an error message in Java

Question: I am a beginner, and I have to write a code that calculates the area and circumference from the radius the user gives me. that part was easy, but the teacher also wanted me to only accept valid integers and have the user re-enter a value when his input is incorrect ...
so i was able to have it display the error message for every case except when they enter an negative integer:
 import java.util.Scanner;
 public class GetCircle
 {
     public static void main(String []args)
     {

    Scanner input = new Scanner(System.in);
     int radius;
     do {
    System.out.print("Please enter size of radius (Must be integer): ");
    while (!input.hasNextInt()) {
        System.out.print("That is not a valid number, please try again :");
        input.next();
    }

    radius = input.nextInt();

    } while (radius < 0);

    System.out.println("The radius is: " + radius);

    }
}

and so this is what I get as a result:

Please enter size of radius (Must be integer): aaa
That is not a valid number, please try again :4.5555
That is not a valid number, please try again :-5
Please enter size of radius (Must be integer): -5
Please enter size of radius (Must be integer): -8.777
That is not a valid number, please try again :-4
Please enter size of radius (Must be integer):

** so whenever its a negative integer it doesn't display the error message...
how can i fix this please?
Comment: PLEASE DON'T SHOUT. Also, please format your question so we can optimally assist you.
Comment: Hint: `-5` is OK regarding `hasNextInt`
Comment: i realize that, and i catch that error by going while ( radius < 0) at the bottom, but i want it to display the error message, and I am stuck. I don't know how to do it . i thought about using : while(!input.hasNextInt() || radius < 0) but that didnt work either.
Answer: There are many ways to do it, but the simplest for you as a beginner would be to add an if statement in between your assignment to radius and the end of your outer while loop:
 if (radius < 0) {
    System.out.print("The radius must not be negative, please try again :");
 }

Comment: this doesn't work because you get :

That is not a valid number, please try again :-5
The radius must not be negative, please try again :Please enter size of radius (Must be integer):

so it repeats two messages now
Comment: Then just move your first System.out.print() call up before the 'do', so it is outside of the outer loop.
Comment: do you know how to use the same principle with JOptionPane?
Comment: I'm not familiar with that.
Answer: If I got your question right, then you don't need to use !input.hasNextInt(). This is how you do it.
import java.util.Scanner;

public class GetCircle {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
    int radius = 0;
    System.out.print("Please enter size of radius (Must be integer): ");
    while (true){
        if(input.hasNextInt()){
        radius = input.nextInt();
        if (radius < 0){
            System.out.println("That is not a valid number, please try again :");
            System.out.println("Please enter size of radius (Must be integer): ");
        }   else {
        System.out.println("The radius is: " + radius);
        }
        } else {
            System.out.println("That is not a valid number, please try again :");
            System.out.println("Please enter size of radius (Must be integer): ");
            input.next();

        }
    }
}
}

Comment: We still haven't learned to use try and catch yet and so I cannot use it ..
Comment: Please enter size of radius (Must be integer): -5
The radius is: -5

so it didnt show the error message...
Comment: but now when the user types 4.5, or a , something that is not an integer the program just exits....
Comment: if you look at the original code, it catches all the errors. so if the user types R, or 7.5 or -4 it makes him enter again. But when it is a negative integer, i cant get it to display the error message. that is the only thing that is wrong with it...
Comment: i did, but i have less than 15 reputation...

may i ask a couple questions?
Comment: @Brow if the answer works for you, could you consider accepting and upvoting the answer? thanks


efficient way to apply content permissions

Question: My site has content type "Department News". There are 10 departments, each one should have permission to create and edit its own "Department News", everybody can view everything.
How to implement it?
My idea is to create 10 roles - one for each department - and the only difference between them will be Create and Edit access to a specific content type.
Then create 10 identical content types, just named differently - and configure permission for the Department-Content matrix.
The question: is it possible to implement it somehow more efficiently, at least without creating 10 content types which are identical, only for the purpose of assigning permissions to them. Best would be also to get rid of the 10 roles and e.g. check additional permission for "Department News" based on a field in user profile.
Duplicating almost identical roles and content types is a burden from maintenance point of view.
Answer: Have you tried looking into Organic Groups?  It seems like the PERFECT solution to your problem.  It'll require a bit of work to set up, but the benefits will FAR outweigh the work.
You could have each department set up as a group, and then users can be assigned to those groups.  You can then set permissions that allow users to create content but only for their specific group (using OG means that this can be ONE content type).
You'd only need one user role (an organic groups user..you could also have a department admin per department should you want).
You can also do some more awesome stuff from here on out.  Just as an example, these departments could then view their own department content - but you can also display all posts from all groups.
There are some REALLY good videos at Modules Unraveled


How to convert Java List into an Oracle ARRAY to enter as a param in an Oracle Stored procedure?

Question: I have the following list of Person Objects:
List<Person> persons;

The Person object is as follows:
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class Person
{
    @XmlElement
    private int id;
    @XmlElement
    private String name;

    public Person(){

    }

}

A Stored Procedure I am invoking takes a list of Person Objects as an Oracle ARRAY.
How can I convert my list of Java Objects into an Oracle ARRAY in order to pass it into the Stored Procedure?
I have seen the CallableStatement.setArray() method, but I do not know how to convert my Java List into an Oracle ARRAY first.
Comment: Also duplicate of http://stackoverflow.com/questions/3626061/how-to-call-oracle-stored-procedure-which-include-user-defined-type-in-java
Answer: You can create an sql Array using the method createARRAY.

Creates an ARRAY object with the given type name and elements.

Here a simple example
    String[] array = new String[]{"a", "b", "c"};
    oracle.jdbc.OracleDriver ora = new oracle.jdbc.OracleDriver();
    java.sql.Connection conn = ora.defaultConnection();
    oracle.jdbc.OracleConnection oraConn = (oracle.jdbc.OracleConnection) conn;
    java.sql.Array sqlArray = oraConn.createARRAY("typeName", array);

If you start from a List of Object
List<Object> list = ...
...
Object[] array = list.toArray();
oracle.jdbc.OracleDriver ora = new oracle.jdbc.OracleDriver();
java.sql.Connection conn = ora.defaultConnection();
oracle.jdbc.OracleConnection oraConn = (oracle.jdbc.OracleConnection) conn;
java.sql.Array sqlArray = oraConn.createARRAY("typeName", array);

Comment: Thanks, although will this work for an ArrayList of Objects ?
Comment: And also, what would the "typeName" be in my case, can you please show a complete example?
Comment: @java123999 I edited my answer to show how to use a List. The parameter type name is defined as: typeName - the name of the SQL type of the created object
Comment: Thanks for edit. So in my case what would the typeName be for a list of Person Objects?
Comment: Is the sql type. I don't know what name you used, probably Person?
Comment: ah ok, the SQL type that is stated in the Stored Procedure?


Is it possible to use TypeScript with 'aws-sdk-mock'

Question: I'm writing unit tests for a serverless application in TypeScript, and I'd like to mock the AWS SDK.
Unfortunately I have not found many existing type definitions for popular AWS mocking projects. In particular I'd like to use the aws-sdk-mock library, but without its type definitions I can't.
Theoretically I'd like to be able to do something like:
<code>import 'jest';
import * as sinon from 'sinon';
import * as _ from 'lodash';
import { handler } from '../lib/lambda';
import AWSMock from 'aws-sdk-mock';
import { PutItemInput } from 'aws-sdk/clients/dynamodb';

const mockData: DataType = {
   // ...some fields
};

describe('create data lambda tests', () => {

  afterEach(() => {
    sinon.restore();
    AWSMock.restore();
  });

  it('returns a success response on creation', () => {
    AWSMock.mock('DynamoDB.DocumentClient', 'put', (params: PutItemInput, callback: any) => {
      return callback(null, 'Successful creation');
    });

    const mockGatewayEvent: any = {
      headers: {
        Authorization: // some JWT
      },
      body: _.clone(mockData)
    };

    handler(mockGatewayEvent).then((createdData: DataType) => {
      expect(createdData.id).toBeDefined();
      expect(createdData.id.length).toBeGreaterThan(0);
    }, () => {
      fail('The create request should not have failed');
    });
  });
});
</code>
Answer: Here's how we got it working with jest. This tests a lambda function that makes calls to Dynamo using the DynamoDB.DocumentClient.
The warnings about importing the aws-sdk-mock ts definitions go away for me if the file is called *.test.ts or *.spec.ts.
<code>// stubbed.test.ts

// this line needs to come first due to my project's config
jest.mock("aws-sdk");

import * as AWS from "aws-sdk-mock";
import { handler } from "../index";
// these next two are just test data
import { mockDynamoData } from "../__data__/dynamo.data";
import { mockIndexData } from "../__data__/index.data";

describe("Stubbed tests", () => {
  it("should return correct result when Dynamo returns one slice", async () => {
    expect.assertions(2);
    const mockQuery = jest.fn((params: any, cb: any) =>
      cb(null, mockDynamoData.queryOneSlice)
    );
    AWS.mock("DynamoDB.DocumentClient", "query", mockQuery);
    // now all calls to DynamoDB.DocumentClient.query() will return mockDynamoData.queryOneSlice

    const response = await handler(mockIndexData.handlerEvent, null, null);

    expect(mockQuery).toHaveBeenCalled();
    expect(response).toEqual(mockIndexData.successResponseOneSlice);

    AWS.restore("DynamoDB.DocumentClient");
  });
});
</code>


Likelihood Function for the Uniform Density. $ (\theta-1,\theta+1)$

Question: Let the random variables $X_1,X_2,\ldots,X_n$  iid $U[\theta-1\,,\theta+1]$.
So the likelihood function therefore has the form:
$$L(\theta\mid X)=\prod_{i=1}^nf(X_i\mid \theta)=\frac{1}{2^n}I(X_1, \ldots , X_n \in [\theta-1\,,\theta+1])\text{ ?}$$
It is correct ?
Answer: It is correct, but can be further simplified by considering the order statistics of the sample.  That is to say, if $$\boldsymbol x = (x_1, \ldots, x_n)$$ is the sample, then consider $$x_{(1)} = \min_i x_i, \quad x_{(n)} = \max_i x_i,$$ the first and last order statistics which are equal to the smallest and largest observations in the sample, respectively.  Then the condition $$\mathbb 1(x_1, \ldots, x_n \in [\theta - 1, \theta + 1])$$ is equivalent to the simpler condition $$\mathbb 1(\theta - 1 \le x_{(1)} \le x_{(n)} \le \theta + 1),$$ because if the smallest observation is at least $\theta - 1$ and the largest is at most $\theta + 1$, then all of the other observations must also fall within the stated interval.  This also shows that a sufficient statistic for $\theta$ is the ordered pair $(x_{(1)}, x_{(n)})$, meaning that in the case of $n > 2$, we can achieve data reduction by discarding all but the minimum and maximum observations.
Comment: thank heropup, is possible draws the likelihood ?
Answer: Your likelihood function is correct.


Get column name where a condition is met after summing upto n columns

Question: I have the below data which contains the monthly sales by different salespeople
df_monthofsuccess
M1_Sales    M2_Sales    M3_Sales    M4_Sales    M5_Sales    M6_Sales
15000       16435       12144       55536       75260       15002
35853       41020       66689       0           51495       11725
2500        24600       0   0       3000        0           12445
80654       0           50625       275946      37320       43000
21578       40000       0   0       20000       0   0       20000

I want to find out the month in which they reach 1,00,000 and capture that month in a separate variable as shown below
M1_Sales    M2_Sales    M3_Sales    M4_Sales    M5_Sales    M6_Sales Month_Target
15000       16435       12144       55536       75260       15002       M5
35853       41020       66689       0           51495       11725       M3
2500        24600       0   0       3000        0           12445       FALSE
80654       0           50625       275946      37320       43000       M3
21578       40000       0   0       20000       0   0       20000       M6

I have tried with the below code:
df_success <- data.frame()
for (i in (1:nrow(df_monthofsuccess))){
  #i = 9
  x <- df_monthofsuccess[i,]
  ape_tot = 0
  month = 'FALSE'
  for (j in (2:ncol(x))){
    #j = 2
    ape_tot = ape_tot + x[,j]
    if (ape_tot > 100000) month = names(x)[j]
    x$monthofSuccess <- month
    next
  }
  df_success <- rbind(df_success,x)
}

However, this does not give the intented output and is slow.
Can anyone help me in getting the desired result?
Answer: We can use apply to loop over the rows in base R, get the index of column where the cumulative sum of the the row is greater than 1e5, extract the names of the first element
df1$Month_Target <- apply(df1, 1, FUN = function(x) sub("_Sales", "",
         names(which(cumsum(x) >1e5)[1])))
df1$Month_Target
#[1] "M5" "M3" NA   "M3" "M6"

NOTE: No packages used.  Only base R

Or using a vectorized approach with matrixStats
library(matrixStats)
m1 <- rowCumsums(as.matrix(df1))
substr(names(df1), 1, 2)[max.col(m1 > 1e5, "first") * NA^!(rowSums(m1 > 1e5))]
#[1] "M5" "M3" NA   "M3" "M6"

Or using tidyverse without doing any reshaping
library(tidyverse)
df1 %>%
    mutate(Month_Target = pmap(., ~
         names(which(cumsum(c(...)) >1e5)[1])) %>%
            str_remove("_Sales"))
#  M1_Sales M2_Sales M3_Sales M4_Sales M5_Sales M6_Sales Month_Target
#1    15000    16435    12144    55536    75260    15002           M5
#2    35853    41020    66689        0    51495    11725           M3
#3     2500    24600        0     3000        0    12445         <NA>
#4    80654        0    50625   275946    37320    43000           M3
#5    21578    40000        0    20000        0    20000           M6

data
df1 <- structure(list(M1_Sales = c(15000L, 35853L, 2500L, 80654L, 21578L
), M2_Sales = c(16435L, 41020L, 24600L, 0L, 40000L), M3_Sales = c(12144L,
66689L, 0L, 50625L, 0L), M4_Sales = c(55536L, 0L, 3000L, 275946L,
20000L), M5_Sales = c(75260L, 51495L, 0L, 37320L, 0L), M6_Sales = c(15002L,
11725L, 12445L, 43000L, 20000L)), class = "data.frame", row.names = c(NA,
-5L))

Comment: Hello @akrun.. This code gives error, replacement has 0 rows, data has 5.
Answer: A tidyverse approach could be to gather the data to long format, group_by each row find the month_name when the cumulative values of sales reaches 100000 and spread it back to wide format.
library(dplyr)
library(tidyr)

df %>%
  mutate(row = row_number()) %>%
  gather(month, sales, -row) %>%
  mutate(month_name = sub("_.*", "", month)) %>%
  group_by(row) %>%
  mutate(target = month_name[which(cumsum(sales) > 100000)[1]]) %>%
  select(-month_name) %>%
  spread(month, sales) %>%
  ungroup() %>%
  select(-row)

#  target M1_Sales M2_Sales M3_Sales M4_Sales M5_Sales M6_Sales
#  <chr>     <int>    <int>    <int>    <int>    <int>    <int>
#1 M5        15000    16435    12144    55536    75260    15002
#2 M3        35853    41020    66689        0    51495    11725
#3 NA         2500    24600        0     3000        0    12445
#4 M3        80654        0    50625   275946    37320    43000
#5 M6        21578    40000        0    20000        0    20000


MATLAB rgb2hsv() vs. OpenCV cvtColor()

Question: Has anyone else noticed that the outputs of MATLAB's rgb2hsv() and OpenCV's cvtColor() (with the argument thereof being CV_BGR2HSV) appear to be calculated slightly differently?
For one, MATLAB's function maps any image input to the [0,1] interval, while OpenCV maintains the same interval of the input (i.e. an image with pixels in [0,255] in RGB keeps the same [0,255] interval in HSV).
But more importantly, when normalizing the cvtColor() output (e.g. mat = mat / 255), the values are not quite the same.
I couldn't find anything in the documentations about the specific formulas they use. Can anyone please shed some light on these differences?
Comment: Just out of curiosity, how big are the deviations you get?
Comment: Pretty minor...but enough that it's evident. We're talking like .04513 vs. .03777. It's enough that it could make a difference in some CV applications but not enough for basic things.
Answer: For OpenCV the formula is right there in the document you point to. For Matlab, have a look here http://www.mathworks.com/matlabcentral/newsreader/view_thread/269237:

Just dive into the code - they gave it to you. Just put the cursor on
  the function rgb2hsv() in your code and type control-d.
Comment: Woops you're right about the OpenCV one...I didn't scroll far enough

MATLAB rgb2hsv() vs. OpenCV cvtColor()


How to add a row to a pandas dataframe with interval indices

Question: How can you add a row to a dataframe in pandas with interval indices in both the column and index?
I am trying to define the total counts in a crosstable in pandas defined as:
import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.rand(500,2))
bins = np.linspace(0, 1, 11)
df['bins_0'] = pd.cut(df[0], bins=bins)
df['bins_1'] = pd.cut(df[1], bins=bins)
# Define crosstable
ct = pd.crosstab(df['bins_0'], df['bins_1'])
# define sumrow
sumrow = ct.sum(axis=0)

My question is: how can you include a row in such a table?
What I tried so far:
Using '.loc' to add a row does not work.
ct.loc['total'] = sumrow
results in
TypeError: cannot append a non-category item to a CategoricalIndex
Trying to reset the index also did not work in this case, since using 'ct.reset_index()'
resulted in TypeError: cannot insert an item into a CategoricalIndex that is not already an existing category
In the end I would also add a column with the sums per row, but I suppose that would require the same procedure as adding a row.
Answer: First idea is use margins in crosstab:
ct = pd.crosstab(df['bins_0'], df['bins_1'], margins=True, margins_name='Total')

Your solution -  if need new column Total add categories to columns and axis=1 to sum:
ct = pd.crosstab(df['bins_0'], df['bins_1'])
ct.columns = ct.columns.add_categories('Total')
ct['Total'] = ct.sum(axis=1)

Or add DataFrame.loc if need new Total row:
ct = pd.crosstab(df['bins_0'], df['bins_1'])
ct.index= ct.index.add_categories('Total')
ct.loc['Total'] = ct.sum(axis=0)

Comment: Also, I edited the question because I made a mistake not to use `.loc` as I had written `ct.index= ct.index.add_categories('Total')
ct['Total'] = ct.sum(axis=0)`
Comment: Thanks, the margins function on crosstab works great.


ID do componente não aparece na MainActivity

Question: O problema é o seguinte, eu adiciono um componente na minha ActivityMain, um botão por exemplo, mas quando vou fazer o findviewById(ID) do componente, não aparece o ID dele na MainActivity, estava funcionando normal antes, mas agora fui add um novo botão e não aparece mais o ID dele, ele aparece na ActivityMain, coloco um ID nele normal, mas na hora de fazer o findviewById, o ID não aparece. Estou tendo esse problema pela segunda, da primeira vez eu resolvi, mas tive que criar outro projeto, copiar todos os código pro outro projeto e aí voltou ao normal e consegui add o componente, mas não queria ter que fazer isso pela segunda vez, gostaria de entender o pq disso estar acontecendo.
Answer: O problema mais provável é que você não está definido o atributo de ID da forma correta ou ele não está no arquivo R.

Primeiro verifique se você está definindo o atributo id do componente:
<code><Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/button" />
</code>

Se sim faça o build do projeto para que as referências sejam adicionadas no arquivo R
Answer: O problema era que o Android Studio não tava meio que atualizando os componentes que eu colocava, eu continuei os trabalhos no código até que uma hora o android studio deu uma pequena taravada, aí resolvi procurar pelo ID do componente e ele tava lá, na verdade, o problema não deve ter sido nem do Android Studio em si, mas sim com meu notebook que tava sobrecarregado com várias abas no chrome abertas também.


Como extraio a quantidade de pedidos com 5 itens diferentes? Possuo essa query porém está saindo um resultado diferente do esperado

Question: SELECT DISTINCT(pedidos.id_pedidos),produtos.nome_produto
FROM pedidos
    INNER JOIN itens_dopedido ON itens_dopedido.id_pedido=pedidos.id_pedidos
    INNER JOIN produtos ON produtos.id_produto=itens_dopedido.id_produto
WHERE nome_produto = (SELECT (COUNT(DISTINCT(id_pedido)) >= 5));

Estão saindo resultados com 4 pedidos diferentes e 1. Alguém pode me explicar o que está acontecendo? Porque eu não entendi.
Comment: Qual o objetivo da sua `query`?
Comment: Considere utilizar a cláusula `HAVING`. É o tipico problema para o qual ela se aplica.
Answer: Se você quer apenas a quantidade de pedidos que tem mais de 5 itens, basta usar uma subquery que conte a quantidade de itens_dopedido e que seja comparada com o número que você quer:
SELECT COUNT(1) AS quantidade
  FROM pedidos p
 WHERE (SELECT COUNT(1)
          FROM itens_dopedido i
         WHERE i.id_pedido = p.id_pedidos) >= 5

Comment: Vou fazer aqui, muito obrigada :)
Comment: @vivianearaujo caso a resposta tenha lhe atendido não esqueça de marcá-la como correta, assim pessoas com dúvidas similares podem ser beneficiadas pela sua pergunta.
Answer: Na verdade você está fazendo algumas coisas erradas, no where você está comparando nome_produto uma soma aleatório. Você precisa utilizar também a tabela itens_do_pedido, eu fiz um exemplo bem simples com um subselect, eu só não sei na sua tabela qual é o "id_item_pedido", segue exemplo como o select deveria ser feito:
SELECT pedidos, nome_produto, qtd_itens_diferentes

  FROM(

    SELECT ped.id_pedidos, p.nome_produto,
    COUNT(itp.id_item_pedido) AS qtd_itens_diferentes

    FROM pedidos ped

        INNER JOIN itens_dopedido itp
            ON itp.id_pedido = ped.id_pedidos

        INNER JOIN produtos p
            ON p.id_produto = itp.id_produto

    GROUP BY ped.id_pedidos, p.nome_produto
  )

WHERE qtd_itens_diferentes > 5

Comment: No lugar do SUM não deveria ser COUNT?
Comment: Isso mesmo rs, ajustado, espero que tenha te ajudado.


Doctrine annotations for custom field formatters in Drupal 8

Question: I am trying to port my Drupal 7 module, Views Hybrid, to Drupal 8. It provides a custom field formatter. In order to provide the field formatter, I am using the Doctrine annotation roughly following this tutorial provided by realize.be. However, I am getting PHP exceptions:
Uncaught PHP Exception Doctrine\Common\Annotations\AnnotationException:
"[Syntax Error] Expected PlainValue, got '>'
at position 168 in class
Drupal\views_hybrid\Plugin\Field\FieldFormatter\HybridFormatter."
at ~/core/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationException.php line 41

If I remove the double quotes from the keys inside the settings array then I get a different error:
Uncaught PHP Exception Doctrine\Common\Annotations\AnnotationException:
"[Semantical Error] Couldn't find constant items_count, class Drupal\views_hybrid\Plugin\Field\FieldFormatter\HybridFormatter."
at ~/core/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationException.php line 52

/**
 * Plugin implementation of the 'views_hybrid' formatter.
 *
 * @FieldFormatter(
 *   id = "views_hybrid",
 *   label = @Translation("Views hybrid"),
 *   field_types = {"entity_reference"},
 *   settings = {
 *      "items_count" => "5",
 *      "view" => "frontpage",
 *      "display" => "linked_title_only",
 *      "classes" => {},
 *      "view_mode" => "teaser",
 *      "arguments" => "",
 *   },
 * )
 */

I am not clear about the syntax that Doctrine expects for these nested arrays, or even on whether Drupal 8 expects the settings array to be included in the Doctrine annotations block when declaring a new field formatter, and my usually debugging techniques aren't very useful for debugging annotations since they're not exactly code. What is the expected syntax and content of a Doctrine annotation for defining a field formatter in Drupal 8?
Answer: I confused PHP array syntax for annotation syntax. Those => should be = and the trailing commas need to be removed from lines that are the last in their set. So it should be:
    /**
     * Plugin implementation of the 'views_hybrid' formatter.
     *
     * @FieldFormatter(
     *   id = "views_hybrid",
     *   label = @Translation("Views hybrid"),
     *   field_types = {"entity_reference"},
     *   settings = {
     *      "items_count" = "5",
     *      "view" = "frontpage",
     *      "display" = "linked_title_only",
     *      "classes" = {},
     *      "view_mode" = "teaser",
     *      "arguments" = ""
     *   }
     * )
     */

Comment: I reckon more than a few people will make this tiny mistake as D8 picks up speed, having a canonical question/answer about is is a good idea IMO. @betherwisser if you could maybe flesh the answer out a tiny bit with the 'why' (annotation syntax is a different beast to php array syntax, yada yada) I think it'd be even more helpful
Comment: I have edited to include your suggestion.


Como formatar valores monetários com C++?

Question: Gostaria de apresentar valores formatados como moeda, com separadores de milhares e centavos.
Gostaria que, por exemplo, 56000/12 apresentasse 4.666,67. Eu consigo apresentar 4,666.67. Teria algum jeito de trocar o . (ponto) pela , (virgula)?
Se quiserem, esse é o código que uso (as 11 primeiras entradas são de 5000 e a 12º é de 1000):
#include <iostream>
#include <iomanip>
#include <locale.h>
using namespace std;

class BRL : public numpunct<char>
{
    protected:

    virtual char do_thousands_sep() const
    {
        return ',';
    }

    virtual std::string do_grouping() const
    {
        return "\03";
    }
};

int menor5000;
float stop,media,mes,tot,var;
int main(int argc, char* argv[])
{
    setlocale(LC_ALL, "portuguese");

    cout <<"\n\t\tExercício 5\n";
    locale br(locale(), new BRL());
    cout.imbue(br);

    for (menor5000,mes=1,stop=0;stop<12;stop++,mes++)
    {
        cout<<"Digite o valor da venda no " << mes <<"º mês: ";
        cin>>var;
        if (var < 5000)
        menor5000++;
        tot = tot + var;
    }

media = tot / 12;
cout<<"\n\nA media de vendas mensais em 2013 é de R$ " << setiosflags (ios::fixed) << setprecision(2) << media << "\n";
cout << "\nForam realizadas " << menor5000 << " vendas menores que R$ 5,000.00.";

return 0;
system ("pause");
}

Comment: Isto te responde? http://pt.stackoverflow.com/questions/9810/formatar-decimal-com-virgula-e-milhar-com-ponto
Answer: Ora, você praticamente tem a resposta você mesmo. Basta modificar a classe que herda de numpunct<char>. Implemente o do_decimal_point para retornar uma vírgula e faça o do_thousands_sep retornar um ponto. Aqui um exemplo:
#include <locale.h>
#include <iostream>
#include <iomanip>

using namespace std;

class CurrencyLocale : public numpunct<char> {
    virtual char do_thousands_sep() const   { return '.'; }
    virtual char do_decimal_point() const   { return ','; }
    virtual std::string do_grouping() const { return "\03"; }
};

int main() {
    locale currency(locale(), new CurrencyLocale());
    cout.imbue(currency);

    cout << fixed << setprecision(2) << 5654214.54 << endl;
}

Resultado é:
5.654.214,54

Answer: Existe um método chamado Replace(), você pode trabalhar ele em conjunto com o Find(). Segue um pequeno exemplo de como utilizar:
Alterei para responder ao Maniero
#include <iostream>
#include <string>

int main ()
{
  using namespace std;

  string str;

  str = "5,000.00";

  str.replace(str.find(','),1,"@");
  str.replace(str.find('.'),1,",");
  str.replace(str.find('@'),1,".");

  cout << ("%s",str);

  return 0;
}

Comment: É, a pergunta estava mal feita, é normal quando o OP não entende exatamente o problema que ele está tentando resolver, aí a gente tem que tentar suprir esta deficiência.
Comment: Ok, eu vi como muda o ponto para a vírgula. E como você vai mudar vírgula para o ponto?
Comment: [edit]e sua reposta para colocar o exemplo.
Comment: Está na resposta o exemplo.
Comment: Não precisa do crédito não. Até porque você alterou para a resposta não ficar errada. Do jeito que estava, resultava em "5,000,00".
Comment: Levei como base a pergunta: **eu consigo apresentar 4,666.67 teria algum jeito de trocar o . (ponto) pela , (virgula)?**. Obrigado pela observação @bigown
Comment: -1. Isso não vai funcionar com valores que possuam mais de um separador decimal, como `1,000,000.00`. A solução está em manipular o locale para gerar o valor já no formato certo, em vez de mudar depois. Além disso, o que você pensa que `cout << ("%s",str);` faz? O literal antes da vírgula é ignorado completamente.
Comment: @GuilhermeBernal Se a solução é manipular o locale, a pergunta é duplicata da que o bigown linkou lá em cima?
Comment: @bfavaretto Não havia lido o comentário dele. Bom... ele parece querer o formato americano e não o brasileiro como na pergunta, mas a ideia é exatamente a mesma, mudando só alguns parâmetros como mencionei na resposta. Acho que dá para considerar duplicata sim.
Comment: @GuilhermeBernal Ou talvez não, porque com qt a resposta mais votada lá parece mais simples (não sei C++ para julgar direito). E aqui a pergunta é sem qt.
Comment: @GuilhermeBernal ainda tinha esquecido deste detalhe. Eu já não tinha gostado da solução :)


How to generate a series of points in a convex set?

Question: For an arbitrary convex set, may be a polyhedron or an ellipsoid how can I generate N uniformly distributed points inside?
Comment: What do you mean by uniformly distributed?
Answer: Generate points uniformly in a rectangle that contains the set and keep only those that are in the set.
Comment: Thanks a lot! and any methodology to solve this problem to arbitrary convex set, such as a convex hull of a set of points.


Create language modal(dialog)

Question: I'm trying to create a language modal for Drupal 8.
I only want to show the modal if:

Language paramater is not present in URL
Language cookie is set by previous selection.

I don't really know what the best method is to create this.
I started by creating a hidden ajax button which gets clicked by custom js if the language parameter is not present in the URL and the language cookie is not set:
namespace Drupal\dms_theme\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\OpenModalDialogCommand;
use Drupal\lang_dropdown\Form\LanguageDropdownForm;
use Drupal\Core\Url;
use Drupal\Core\Language\Language;

/**
 * Class LangswitchModal.
 *
 * @package Drupal\dms_theme\Form
 */
class LangswitchModal extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'dms_theme_langswitch';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['#attached']['library'][] = 'core/drupal.dialog.ajax';

    $form['actions']['#type']  = 'actions';
    $form['actions']['submit'] = [
      '#type'  => 'submit',
      '#value' => $this->t('Switch language'),
      '#ajax'  => [
        'callback' => '::open_modal',
      ],
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

  }

  public function open_modal(&$form, FormStateInterface $form_state) {
    // Get active domain
    $domain_negotiator = \Drupal::service('domain.negotiator');

    $form = \Drupal::formBuilder()->getForm('Drupal\dms_theme\Form\LangswitchForm');

    // Build array
    $build = [];

    $build[] = [
      '#theme' => 'langswitch_modal',
      '#title' => 'Welcome',
      '#form' => render($form),
    ];

    $render  = \Drupal::service('renderer')->renderPlain($build);

    // Dialog
    $options = [
      'dialogClass' => 'popup-languageswitcher-modal',
      'width'       => '100%',
    ];

    $response = new AjaxResponse();
    $response->addCommand(new OpenModalDialogCommand(NULL, $render, $options));

    return $response;
  }
}

The modal is opened correctly and the content and form is printed.
Logic of the form:
namespace Drupal\dms_theme\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\OpenModalDialogCommand;
use Drupal\lang_dropdown\Form\LanguageDropdownForm;
use Drupal\Core\Url;
use Drupal\Core\Language\Language;

/**
 * Class LangswitchForm.
 *
 * @package Drupal\dms_theme\Form
 */
class LangswitchForm extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'dms_theme_langswitchform';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    // Service
    $domain_negotiator = \Drupal::service('domain.negotiator');

    // Language options
    $language_options = [];
    foreach(domain_locale_custom_get_domains_languages()[$domain_negotiator->getActiveId()] as $domain_language) {
      $language_options[$domain_language->getId()] = $domain_language->getName();
    }

    $form['languages'] = [
      '#type' => 'select',
      '#title' => t('Please indicate from which location you are connecting:'),
      '#options' => $language_options,
      '#required' => TRUE,
    ];

    $form['actions'] = [];
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => t('Submit button'),
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

  }
}

Now i can't submit the form correctly. It redirects to urls like /form_action_p_pvdeGsVG5zNF_XLGPTvYSKCf43t8qZYSwcfZl2uzM.
I geuss this is caused by the ajax callback url, which is used by some form parameters. But don't really know how to solve this.
Thnx.
Other question:
What are in general the best practice to show a language modal.
If language is not is in URL? If language is not set in Cookie? Or the combination described above?
Answer: You render early twice, which prevents that any attached metadata has a chance to get into the response. You can try without rendering or if you need to render, then attach the metadata to the response like in this example:
ModalRenderer::renderResponse
  public function renderResponse(array $main_content, Request $request, RouteMatchInterface $route_match) {
    $response = new AjaxResponse();

    // First render the main content, because it might provide a title.
    $content = drupal_render_root($main_content);

    // Attach the library necessary for using the OpenModalDialogCommand and set
    // the attachments for this Ajax response.
    $main_content['#attached']['library'][] = 'core/drupal.dialog.ajax';
    $response->setAttachments($main_content['#attached']);

    // If the main content doesn't provide a title, use the title resolver.
    $title = isset($main_content['#title']) ? $main_content['#title'] : $this->titleResolver->getTitle($request, $route_match->getRouteObject());

    // Determine the title: use the title provided by the main content if any,
    // otherwise get it from the routing information.
    $options = $request->request->get('dialogOptions', []);

    $response->addCommand(new OpenModalDialogCommand($title, $content, $options));
    return $response;
  }

Additional to the example you might need to add the attachments of the form too:
$response->addAttachments($form['#attached']);

"Other question: What are in general the best practice to show a language modal. If language is not is in URL? If language is not set in Cookie? Or the combination described above?"
There is also a server side approach. You can add some kind of overlay with the form before you deliver the page.

Create language modal(dialog)


Remove default installation profiles with drupal core

Question: Is there a way to remove the core installation profiles that come with drupal without the need of hacking core? i.e. is there a hook that can run to alter this list?
The reason why this is required is that we are making a custom install profile for use with aegir, and do not want the default core drupal 'minimal' and 'standard' install profiles as options for the users to create new sites.
Perhaps this could be a hook in aegir?
Has anyone found an elegant solution to this?
Comment: I have found no hook for that, I think you have not many choice but remove the minimal and standard folders from /profiles
Comment: Agreed... I've always just manually edited or deleted the profiles as necessary. This is certainly a weakness of Aegir as it currently works.
Answer: You need to add exclusive = 1 line in the .info file of your profile.
Answer: Build a simple module for your Aegir instance that implements hook_form_alter() to hide the Standard/Minimal profile options.
Answer: This is how I did it and let me know please if there is smarter way.

From the core I learned that the install is using maintenance theme or 'seven' if none defined. So I created a new 'my_install' sub-theme. Seven theme is its base theme. I replaced the logo.png with my site's logo, created template.php and added hook_form_alter() to it to unset the Standard and Minimum profiles:
function my_install_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'install_select_profile_form') {
    $form['profile']['My profile']['#attributes'] = array('checked' => 'checked');
    unset($form['profile']['Standard']);
    unset($form['profile']['Minimal']);
  }
}

Now to make install to use my_install theme I changed settings.php and default.settings.php by adding to both
$conf['maintenance_theme'] = 'my_install';

that is it.