Git how to rename a folder/package remotely?

Question: I accidentally named my package the wrong thing and I pushed it to github before I noticed. I renamed it locally, but when I push it again it hasn't tracked the changes made since it was just a couple of capitalization errors. How do I fix the remote capitalization errors? I mean its not a huge deal, but I would prefer to maintain consistency.
Comment: It is not clear what was not named correctly. What "package" are you talking about?
Answer: Depending on the OS you are on, the change in the casing of a path may not be picked up locally - in Linux it is. A way around it is to
mv somepath/filename temp
git add -A
git commit -m "changing the case of a dir"

mv temp SomePath/FileName
git add -A
git commit --amend -m "changed somepath/filename to SomePath/FileName"

You can also just try
git mv -f somepath/filename SomePath/FileName
git commit -m "changed somepath/filename to SomePath/FileName"

But you will need to not be in that directory for this to work and may need to git reset --hard from the root of the repository if on Windows or OSX to force the working directory to be updated.
Answer: the manpage of git remote says
git remote rename <old> <new>

Comment: And what do I enter for old/new? I don't think I can use my local directories can i?
Comment: old is the old name on your remote repository new in the name you want it suits


List runtime-only changes in firewalld

Question: Is there a way to list runtime-only (i.e. no --permanent) changes in firewalld? I would like to see a diff in my configuration to make sure no change will get lost in case of a --reload.
Answer: as far as I can see no tool that offers you that possibility, but you can create it yourself.
I add a runtime acl to port 54/tcp:
# firewall-cmd --add-port=54/tcp

Then I can save the runtime rules to a file:
# firewall-cmd --list-all > /tmp/runtime_rules

And I save the permanent rules to another file:
# firewall-cmd --list-all --permanent > /tmp/permanent_rules

Finally I use diff to compare both
# diff /tmp/runtime_rules /tmp/permanent_rules
1c1
< FedoraWorkstation (active)
---
> FedoraWorkstation
4c4
<   interfaces: wlp2s0
---
>   interfaces:
7c7
<   ports: 1025-65535/udp 1025-65535/tcp 54/tcp
---
>   ports: 1025-65535/udp 1025-65535/tcp

And there you have the port I added on the runtime rules, not on the permanent one.
Comment: This is what I ended up doing. Thanks for the detailed answer!
Comment: This seems to only list the default zone. For example, if I run `firewall-cmd --zone=trusted --add-source=192.0.2.1/32 --permanent`, that doesn't show up in `firewall-cmd --list-all --permanent` which for me lists the `public` zone my intefrace is in.

Something to be aware of if you use multiple zones
Answer: I'd like to propose a one-liner alternative usable in a bash shell:
diff -u <(firewall-cmd --list-all --permanent) <(firewall-cmd --list-all)

--- /dev/fd/63  2022-02-20 14:05:38.106385643 +0000
+++ /dev/fd/62  2022-02-20 14:05:38.110385485 +0000
@@ -3,7 +3,7 @@
   icmp-block-inversion: no
   interfaces: eno0
   sources:
-  services: dhcpv6-client ssh
+  services: dhcpv6-client http https ssh
   ports:
   protocols:
   forward: no

Lines prefixed with - are in the permanent config. Those with + are  in the live configuration.

List runtime-only changes in firewalld


Python, writing a very basic alarm clock

Question: I've just written a very basic alarm clock in python to warn me when some food is cooked at 2:15pm, but I've read somewhere that time.sleep loses time when other programs are being processed, what should I use instead?
My current code, it's really basic -
import time
import winsound
time.sleep(370800)
winsound.Beep(1000,10000)

Comment: You could periodically check the time and if it's 2.15pm, beep!
Answer: Do periodic checks. The code below should be ok. You can tweak timeout variable to your needs:
timeout = 10
target = time.time() + 370800

while True:
    diff = target - time.time()
    if diff < timeout:
        if diff > 0:  # highly unlikely that diff will go below 0 but just in case
            time.sleep(diff)
        break
    else:
        time.sleep(timeout)

winsound.Beep(1000,10000)


Proof applying density.

Question:
(Exercise 1.2.8 in Lebl) Show that for any two real numbers $x$ and $y$ such that $x < y$, we have an irrational number s such that $x < s < y$.
Hint: Apply the density of $\mathbb Q$.

I'm just learning to write proofs and can't get anywhere.
Comment: No, the exercise asks for irrational number.... Should be the same?
Comment: I guess you mean rational?
Comment: That's weird, but just replace in my answer $\mathbb Q$ with $\mathbb R - \mathbb Q$
Comment: This has to be a duplicate...
Answer: I'll assume that we've already shown that $\sqrt{2}$ is irrational. Now since $\frac{x}{\sqrt 2},\frac{y}{\sqrt 2} \in \mathbb R$ and $\frac{x}{\sqrt 2} < \frac{y}{\sqrt 2}$, we know by the density of $\mathbb Q$ that there exists some rational $q \in (\frac{x}{\sqrt 2},\frac{y}{\sqrt 2})$. Without loss of generality, notice that we may assume that $q$ is nonzero (for if $q = 0$, then we can just apply the same density argument to find some rational nonzero $q' \in (0, \frac{y}{\sqrt 2})$.
Now since $\frac{x}{\sqrt 2} < q < \frac{y}{\sqrt 2}$, observe that $x < q\sqrt 2 < y$. Notice that $q\sqrt 2$ is irrational (otherwise, if $q\sqrt{2} \in \mathbb Q$, then since $q$ is a nonzero rational number, we know that $\frac{q\sqrt 2}{q} = \sqrt{2}$ is also rational, a contradiction). Hence, we may take $s = q\sqrt 2$. $~~\blacksquare$
Comment: Thanks, now I'm goning to analyze this to really get the idea!! Thank you so much!
Answer: We can assume that $x > 0$. Since $x < y$, $y - x > 0$. Then since both $1$ and $y -x$ are positive real numbers, there exists a natural number $q$ such that $(y - x)q > 1$ by the Archimedian property. By the same principle, the set $S = \{n \in \mathbb{N}: \frac{n}{q} \geq y\} \neq \emptyset$ hence there exists a smallest $m > 1$ (since $ y > (y - x) > \frac{1}{q}$ ) such that $x + \frac{1}{q} < y \leq \frac{m}{q} \implies \frac{m-1}{q} \in (x, y)$.
Since $\frac{y - \frac{m-1}{q}}{2} > 0$, by the Archimedian Property there exists $n \in \mathbb{N}$ such that $\frac{y - \frac{m-1}{q}}{2} > \frac{1}{n}$. Then we have $x < \frac{m -1}{q} + \frac{\sqrt{2}}{n} < \frac{m -1}{q} + \frac{\sqrt{4}}{n} < y$. Now check that $s = \frac{m -1}{q} + \frac{\sqrt{2}}{n}$ is an irrational number sitting in $(x, y)$.
Answer: Let $x,y$ be real numbers s.t. $x < y$.
Define $ y-x=\epsilon >0$, then by the density of the rationals on the real line there exists a rational $$q \in (\frac{x+y}{2} - \frac{\epsilon}{4} , \frac{x+y}{2} + \frac{\epsilon}{4})\subset (x,y)$$ therefore $x < q < y$ as needed

Proof applying density.


How to do localization?

Question: im really neww to the term localization, can any one of you expound, i've been searching for good references. can anyone of you provide me some good references or can anyone of you explain about this matter to give me more insights
Comment: That's a rather vague question. What's your objective? If it's just to learn the meaning of the term, why have web searches been insufficient? If it's to implement it in the context of a particular technology or framework, we'd probably need to know which ones.
Comment: in the context of implementing this on a certain tech or framework, i really don't know the process even the basic, can't find any good source
Comment: Based on your other questions with the res/values/strings.xml and the other locales come e.g. in values-de, values-es and so on.
Comment: hmmm dats on android, but what is localization really? an overview would be an headsup and some pointers would be good
Answer: Basically you just need to create a seperate strings.xml file for every language you want to support.
Your fallback language is in the directory res/values. Your german translation would be in res/values-de, the spanish one in res/values-es and so on.
Read also the official documentation about that.
Comment: so u mean every variable that is declared in strings with its equivalent word say in en
Comment: Your comment seems to be incompleated but yes, if you have 3 versions of a text variable e.g. `R.string.welcome` `getResources().getString(R.string.welcome)` would return on an english device *"Welcome"* and on a german device *"Willkommen"*.

How to do localization?


How to add the image url in the head tag

Question: Do youknow how to add the image url to the head tag?
This is what I've tried so far
Example
<code><head>
     <meta property="og:image" content="  {{ file_create_url(node.field_image.0.entity.uri.value) }}        "/>
     <meta property="og:image:secure_url" content="  {{  file_create_url(node.field_image.0.entity.uri.value)  }} " />
</head>
</code>
Thanks!
Answer: Or possibly a better solution - use the metatag module and you can customise this through the CMS globally or for specific pages. "og" stands for Open Graph so you just need to pop your data in the relevant open graph section
https://www.drupal.org/project/metatag
Answer: Probably the best place to do this is in a node preprocess function and then attach it for the render array. How it could be:
<code>/**
 * Implements hook_preprocess_node().
 */
function themename_preprocess_node(&$variables) {
  // Load the image field values from the node.
  $image_field = $variables['node']->get('field_image')->getValue();
  // Get the first item of the field values.
  $image_field = reset($image_field);
  // Load file by the file id.
  $image = file_load($image_field['target_id']);

  // Create the actual HTML by creating a render array.
  $og_image = [
    '#type' => 'html_tag',
    '#tag' => 'meta',
    '#attributes' => [
      'property' => 'og:image',
      'content' => $image->toUrl(),
    ],
  ];

  $variables['#attached']['html_head'][] = [$og_image, 'og:image'];
}
</code>
Comment: That is for Drupal 8
Comment: I need it for drupal 8, do you know what it would be for drupal 8 please?


django / filter the fields of a form and have the error 'int' object has no attribute '_meta'

Question: I use django-cities-light for a travel website and I would like to filter the cities in the fields ville_de_depart and ville_destination in the newBookingForm by trip.depart and trip.destination.
I tried to pass the trip object in the instance of newBookingForm. I override the __init__ and I took the value of the depart and destination, I succeeded in filtering the fields but I could no longer save the newBooking, the view redirect to the alltrip page with no error but no new booking is added to the database.
I tried to replace the trip by the slug which is the same value as the id and it shows me this error
'int' object has no attribute '_meta'

models.py
class trip(models.Model):
    depart = models.ForeignKey(default='',to=Country,on_delete=models.CASCADE,related_name='depart')
    destination = models.ForeignKey(default='',to=Country,on_delete=models.CASCADE)
    date_de_depart = models.DateField(default='')
    prix_kg = models.PositiveIntegerField(default='')
    collecte = models.BooleanField(default=False,null=False,help_text='' )
    creation_date = models.DateTimeField(auto_now=True)
    author = models.ForeignKey(to=settings.AUTH_USER_MODEL,on_delete=models.CASCADE,default='')

    slug = models.SlugField(max_length=100, default='' )

    #jouter slug
    def save(self, *args , **kwargs):
        super(trip, self).save(*args , **kwargs)
        if not self.slug:
            self.slug = self.id
            self.save()

    def __str__(self):
        return f'{self.id} {self.author} '

class Booking(models.Model):
    trip = models.ForeignKey(trip,on_delete=models.CASCADE, default='',)
    author =  models.ForeignKey(to=settings.AUTH_USER_MODEL,on_delete=models.CASCADE,default='')
    creation_date = models.DateTimeField(auto_now=True)
    ville_de_depart = models.ForeignKey(City,on_delete=models.CASCADE,default='')
    slug = models.SlugField(max_length=100, default='' )
    # ville_depart = models.ForeignKey(default='',to=City,on_delete=models.CASCADE,related_name='ville_dep')
    sender_phone = PhoneNumberField(blank=True)
    receiver_phone = PhoneNumberField()
    ville_destination = models.ForeignKey(default='',to=City,on_delete=models.CASCADE,related_name='ville_dest')

    #jouter slug
    def save(self, *args , **kwargs):
        super(Booking, self).save(*args , **kwargs)
        if not self.slug:
            self.slug = self.id
            self.save()

    def __str__(self):
        return str(self.trip.author)

views.py
def detailsTrip(request, slug):
    trip = get_object_or_404(models.trip,slug=slug)
    auth = trip.author

    bookingForm = newBookingForm(instance=slug)
    context = {'trip': trip, 'auth': auth, 'form': bookingForm}

    if request.method == 'POST':
        form = newBookingForm(request.POST , instance=slug )

        if request.user.is_authenticated:
            if form.is_valid():
                trip = get_object_or_404(models.trip,slug=slug)

                Booking = form.save(commit=False)
                Booking.trip_id= trip.id
                Booking.author_id = request.user.id
                Booking = form.save()

                return redirect('/alltrips')
            else:
                trip = get_object_or_404(models.trip,slug=slug)
                auth = trip.author
                bookingForm = newBookingForm()

                context = {'trip': trip, 'auth': auth, 'form': bookingForm}
                return render(request, 'detailstrip.html', context)
        else:
            return render (request, 'notFound.html')

    return render(request,'detailstrip.html', context , )

forms.py
class newBookingForm(forms.ModelForm,):
    def __init__(self,*args,**kwargs):
        # capture the instance : Slug
        slug = kwargs.get('instance')

        # capture current trip
        Trip = get_object_or_404(trip, id=slug)

        # Filter cities field by the instance : trip.depart  / trip.destination
        super(newBookingForm, self).__init__(*args,**kwargs)
        self.fields['ville_de_depart'].queryset = City.objects.filter(country_id=Trip.depart)
        self.fields['ville_destination'].queryset = City.objects.filter(country_id=Trip.destination)

    class Meta:
            model = Booking
            fields = ['ville_de_depart','ville_destination']
            exclude = ['sender_phone','receiver_phone']

Comment: Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example).
Answer: solution
#view.py
def detailsTrip(request, slug):

trip = get_object_or_404(models.trip,slug=slug)
auth = trip.author
#add the trip to the form
bookingForm = newBookingForm(trip=trip)

context = {'trip': trip, 'auth': auth, 'form': bookingForm}

if request.method == 'POST':

    #add the object trip to the form here too

    form = newBookingForm(trip,request.POST )

    if request.user.is_authenticated:

        if form.is_valid():
            trip = get_object_or_404(models.trip,slug=slug)

            Booking = form.save(commit=False)
            Booking.trip_id= trip.id
            Booking.author_id = request.user.id
            Booking = form.save()

            return redirect('/alltrips')
        else:
            trip = get_object_or_404(models.trip,slug=slug)
            auth = trip.author
            bookingForm = newBookingForm()

            context = {'trip': trip, 'auth': auth, 'form': bookingForm}

            return render(request, 'detailstrip.html', context)
    else:
        return render (request, 'notFound.html')

return render(request,'detailstrip.html', context , )

form.py
class newBookingForm(forms.ModelForm,):
class Meta:

        model = Booking
        fields = ['ville_de_depart','ville_destination']
        exclude = ['sender_phone','receiver_phone']

def __init__(self,trip,*args,**kwargs):

        # Filter cities field by trip.depart  / trip.destination
        super(newBookingForm, self).__init__(*args,**kwargs)
        self.fields['ville_de_depart'].queryset = City.objects.filter(country=trip.depart)
        self.fields['ville_destination'].queryset = City.objects.filter(country=trip.destination)

Comment: when you call form.save(commit=False) it will return an object that hasn't yet been saved to the database. the commit keyword means to apply data to the database. You may use this method if you want to do custom processes on the object before saving it, as you do in your code above.
Answer: As long as you use many-to-many fields, you need to call form.save_m2m() after saving the model instance. or you can just call form.save() without commit=False keyword argument.
Here are some quotes from Django documentation:

This save() method accepts an optional commit keyword argument, which accepts either True or False. If you call save() with commit=False, then it will return an object that hasn't yet been saved to the database.

Another side effect of using commit=False is seen when your model has a many-to-many relation with another model. If your model has a many-to-many relation and you specify commit=False when you save a form, Django cannot immediately save the form data for the many-to-many relation.

read this documentation about the save method


Provig that the number $z_n$ of zero element of $A^{-1} $ satisfies $z_n\le n^2-2n$

Question: Let A be a $n\times n$ matrix $n\ge 2$ symmetric invertible with real positive entries.

Then show that the number $z_n$ of zero element of $A^{-1} $ satisfies
  $$z_n\le n^2-2n$$
  How many zero elements are in the inverse of the $n\times n$ matrix A given by
  $$A=\begin{pmatrix} 1&1&1&1&\cdots&1
\\1&2&2&2&\cdots&2
\\1&2&1&1&\cdots&1\\
\cdots &\cdots&\cdots&\cdots&\cdots&\cdots
\\1&2&1&2&\cdots&\cdots\end{pmatrix}$$

The try to use the multiplication formula for matrices which given by as follow
$$(AB)_{ij} = \sum_{k=1}^{n}a_{ik}b_{kj}$$
Then taking $B= A^{-1} = (b)_{i,j}$ we have the system,
$$ (I_n)_{ij}=(AB)_{ij} = \sum_{k=1}^{n}a_{ik}b_{kj} =\delta_{ij}$$
where $\delta_{ij}$ is the Kronecker symbol.

From here I don't how to get further does any one ha an idea?

for the second question the number of zeros elements seems to be $n^2-2n$ which prove that the inequality $z_n\le n^2-2n$ is sharp. see here
Answer: Suppose to the contrary that $z_n \geq n^2 - 2n + 1$. Suppose $A^{-1} = (b_{ij})$. Then by pigeon hole principle there is a column with at least $n-1$ zeros, without loss of generality let it be the first column. This means that there is an $m$ such that $b_{i1} = 0$ for all $i \neq m$. Then the $m1$-minor of $A^{-1}$ has a zero column hence has zero determinant, which implies $A$ has a zero entry, contradiction.
Comment: what is pigeon principle
Comment: It is simply statements in the flavor: "if 5 pigeons go into 4 holes, at least one hole contains 2 or more pigeons."


Complex folder structure using MVC 4 Views

Question: I have a very large system divided by folders, like that:
<code>Folder1 - Folder2 - Folder3 - View1/View2
Folder1 - Folder2 - Folder4 - View3
Folder1 - Folder2 - Folder5 - View4
Folder1 - Folder6 - Folder7 - View5/View6/View7
...
</code>
I´d like to know if it possible to create that folder organization with MVC 4... Areas just give 1 sub nivel...
Thanks
Answer: You could write a custom view engine which allows you to override the default views locations:

http://weblogs.asp.net/imranbaloch/archive/2011/06/27/view-engine-with-dynamic-view-location.aspx
http://blog.thekfactor.info/posts/asp-net-mvc-custom-view-engines-using-the-razor-view-engine-as-the-base/
Comment: So, Is that the only option? I have more than 100 folders... Am I supposed to mapping all of then in the custom view engine?
Comment: Yes, that's what you are supposed to do. Alternatively if you follow the standard conventions you won't need to write custom view engines.
Comment: How is it even possible to mantain a huge project organized with standard conventions? In my case, i will have 5 Areas, and then in Area 1, all Views will be grouped in the same level (about 100)...
Comment: 100 Views in the same level??????? You have 100 actions in a single controller? Just organize your controllers better - by resources. As you know, each view is placed in a subfolder with the name of the controller hosting the action that is serving this view.
Comment: No, I mean Controllers... In Area I have about 80 controllers... In my old Web Forms project I have organized with folder, sub-folders... But with MVC all in same folder...
Comment: You could place controllers wherever you want. ASP.NET MVC doesn't care. It is loading them at runtime by type from the AppDomain. It absolutely doesn't matter where your `XXXController.cs` file lives. They could even be placed in separate assemblies and logocally organized. What's important is the location of your views. But as I already said in my previous comment the location of the view is bound to the name of the controller. So if you have 5, 6 actions in a controller (which sounds reasonable) you will have at most 5 views in the corresponding folder.


Somar valores de inputs dinâmicos

Question: Preciso exibir o total de um "item valor x quantidade" em um paragrafo de um formulário dinâmico, esse formulário tem um botão que ao clicar cria outra div para preencher.
Consegui fazer com a div fica pegar o valor e mostrar mas quando vou para div criada dinamicamente não aparece.

function calculo() {

  $('.totaldoValor2').each(function(i) {
    var valor = parseInt(document.querySelector(".valorUnitario").value, 10)
    var unidade = parseInt(document.querySelector(".qtd").value, 10)
    var total = valor * unidade;
    console.log(total);

    document.querySelector('.totaldoValor2').innerHTML = total

  })

}

  <button type="button" class="remove"></button>

  <select name="" id="">
    <option value="">Hospedagem</option>
    <option value="">Pedágio</option>
    <option value="">Alimentação</option>
    <option value="">Combustivel</option>
    <option value="">Outra</option>
  </select>
  <input type="text" name="">
  <input type="text" name="valorUnitario" class="valorUnitario" onblur="calculo()">
  <input type="text" name="qtd" class="qtd" onblur="calculo()">

Answer: Consegui resolver.
function teste(){

    $('.linha').each(function (){
        console.log(this);

        // pega os valores dos inputs

        var qtd = $(this).find('input[name=quantidade]').val()
        var val = $(this).find('input[name=valor_unitario]').val();
        var total = parseFloat(qtd) * parseFloat(val);

        //tratamento do NaN

        if(isNaN(total)){
            $(this).find('input[name=valor_total]').val(" ").html(" ")
        }else{
            $(this).find('input[name=valor_total]').val(total).html(total)
        }

    });

    // validação input
    $('.linha').each(function (){
        console.log(this);

        var total1 =  $(this).find('input[name=valor_total]').val()

        if(total1 >= 200){
            $(this).addClass( "selected" ).hasClass("linha");

        }
    });
}

Comment: @Augusto Vasques poderia me explicar o motivo de retirar o "Boa tarde!" , isso atrapalha em alguma coisa na pesquisa?

Desculpe a pergunta mas sou novo aqui
Comment: Sim posso explicar. Porque não somos uma rede social ou fórum ou grupo de discussão. O Stack Overflow é um site de perguntas e resposta técnicas sobre linguagem de programação e nosso [código de conduta](https://pt.stackoverflow.com/help/behavior) estabelece que nas publicações não devem ser feitos cumprimentos, agradecimentos, ser feito o uso de assinaturas e  frases de efeito.
Comment: É um comportamento já [estabelecido pela comunidade](https://pt.meta.stackoverflow.com/questions/846/sauda%c3%a7%c3%b5es-e-agradecimentos), pode parecer que não mas o caminho entre  [*Boa tarde* para um *Fala ai seus locos* ou *E ai bando de FDPs* é curto](https://pt.meta.stackoverflow.com/questions/44/teoria-das-janelas-partidas). Fora o fato que é num site de alcance mundial cumprimentar pelo horário é  ruído comunicação temos usuários em todos os fusos horários, bom dia para um é boa noite para outro.
Comment: @AugustoVasques Entendi, obrigado.

Somar valores de inputs dinâmicos


D7 Services node view mode

Question: I am using the services module. When I call domain.com/json/node/3 is it possible to ask for a specific view mode of the node ?
Something like this ?
domain.com/json/node/teaser/3
Thanks!
Answer: Nope, Drupal Services just return the node object by default. Looking at the end points in node_resource.inc, it would seem it would not be hard to build thou.
Comment: Maybe afterwards I should write some docs for services. It is a good and important module, but the documentation could use a lot of work.
Comment: If youre building it try using the services output alter hook. http://drupalcode.org/project/services.git/blob/refs/heads/7.x-3.x:/docs/services.alter.api.php#l29
Comment: Perfect! I did not see that function. This is just what I need.
Comment: looking forward to the added documentation

D7 Services node view mode


WPF :-Assembly reference issue(Are you missing an assembly reference)

Question: I have one WPF project which is referring to another WPF project within the same solution.When I include namespace in first project in xaml file or its .cs file while compile time it shows the error like"Are you missing an assembly reference of project 2" .
Comment: Open the output window during the build, what is the first error you see ?
Comment: The type or namespace name 'XYZ' does not exist in the namespace 'ABC' (are you missing an assembly reference?)
Comment: Compile project A, Project B has a reference to project A right ? after project A is compiled successfully, Compile project B.
Answer: You Just Need to Set your Build Order Properly.
Your Ideal Build Order should be like this
1) Independent Project(project 2 in your case)
2) Dependent Project(project 1 in your case)
Second thing, to consider is that there is no cyclic reference.


How to remove multiple strings from a list by letter?

Question: I am trying to remove multiple strings from a list without the letter "a".
I have tried to use a function to remove every word without the letter "a" in my list
myList = ['advertisement', 'start', 'clever', 'billowy', 'melted', 'charge', 'longing', 'disgusting', 'phobic', 'carry', 'chew', 'big', 'mist', 'warn', 'faint']

def new_list(myList):
    for word in myList:
        if 'a' not in word:
            myList.remove(word)
    return myList

print(new_list(myList))

>>> ['advertisement', 'start', 'billowy', 'charge', 'disgusting', 'carry', 'big', 'warn', 'faint']

I expected it to remove all words without the letter "a" in it, but it is still outputting the words "billowy", "disgusting" and "big".
Comment: Possible duplicate of [Removing from a list while iterating over it](https://stackoverflow.com/questions/6500888/removing-from-a-list-while-iterating-over-it)
Answer: You're trying to change the list at the same time as modifying it. Try creating a new list with the filtered objects instead.
mylist = [x for x in mylist if 'a' in x]

See more methods here: How to remove items from a list while iterating?
Answer: Personally, I think,
Best practice is creating new list and returning it, than removing elements from existing list.
so,
def new_list(myList):
    newlist = []
    for word in myList:
        if 'a' in word:
            newlist.append(word)
    return newlist

Answer: You can do it by initializing new list in your function.
myList = ['advertisement', 'start', 'clever', 'billowy', 'melted', 'charge', 'longing', 'disgusting', 'phobic', 'carry', 'chew', 'big', 'mist', 'warn', 'faint']

def new_list(myList):
    l = []
    for word in myList:
        if 'a' in word:
            l.append(word)
    return l

print(new_list(myList))

Answer: You cannot modify a list you are currently iterating, try making another copy of the list. Try using slice operator to make a copy of the list.
for word in myList[:]:
    if 'a' not in word:
        myList.remove(word)
return myList


How to redefine c++ pointer function?

Question: I have code from BASS lib.
#ifndef BASSDEF
#define BASSDEF(f) WINAPI f
#else
#define NOBASSOVERLOADS
#endif

HSAMPLE BASSDEF(BASS_SampleLoad)(BOOL mem, const void *file, QWORD offset, DWORD length, DWORD max, DWORD flags);

I need redefine BASSDEF to call dlsym function. How can i do this?
Update:
I using this on Android NDK (Linux) i loaded bass module via function dlopen and i need to make all functions point (here is original header file of bass lib https://pastebin.com/Z2Ty9UsY ) to this loaded module via dlsym function. I need this to call all functions (from JNI inside bass.so) module easily.
Comment: `BASSDEF` is not a function, and neither it nor `BASS_SampleLoad` is a pointer. Please be more specific about what you want to accomplish and what the context is.
Comment: Please elaborate on what you mean by "dlsym function".
Comment: @SebastianRedl https://linux.die.net/man/3/dlsym
Comment: @LightnessRaceswithMonica I know `dlsym` in Linux. But this is Windows. Also, "dlsym function" is not a well-defined term. Neither is "GetProcAddress function", the Windows translation.
Comment: @SebastianRedl _"But this is Windows"_ Where does the question say that?
Comment: @SebastianRedl _"Also, 'dlsym function' is not a well-defined term"_ Come on, that's _really_ stretching credulity.
Comment: I using this on Android NDK (Linux) i loaded bass module via function dlopen and i need to make all functions point to this loaded module via dlsym function.
Comment: Description updated.
Comment: @SebastianRedl take a look at BASS lib description and provided header. BASS is cross-platform. For example in linux impl WINAPI redefined as nothing to be just a placeholder. So, it only looks like Windows-only!
Comment: @LightnessRaceswithMonica "Come on, that's really stretching credulity" - No, I genuinely have no idea what "call dlsym function" means. Is the OP just confused about DLLs vs runtime linking (as opposed to load-time) and means "import from DLL"? (E.g. redefine `BASSDEF` to invoke the corerct `__declspec(dllimport)` magic?) Or do they want the function declaration to turn into a function pointer global that is initialized via `dlsym/GetProcAddress`? It could be either, or something else. "dlsym function" is not a defined term that would clarify this situation.
Answer: Actually, BASSDEF is not a function. It's macro which is known at compile time. So let's unwrap it ourselves:
HSAMPLE WINAPI BASS_SampleLoad(BOOL mem, const void *file, QWORD offset, DWORD length, DWORD max, DWORD flags);

Whoa, just function declaration here. Now "WINAPI" is basicly __stdcall call convention (Microsoft-specific). But, looking to BASS header you provided one can find for non-WIN32 systems:
#define WINAPI

Basicly, under Linux it's just a placeholder which expands to nothing. Now function declaration looks like this:
HSAMPLE BASS_SampleLoad(BOOL mem, const void *file, QWORD offset, DWORD length, DWORD max, DWORD flags);

What's next? You would like to find this function in some shared library via dlsym?
I assume you wants something like this:
// Declare a function pointer in C++11 style
using BASS_SampleLoad_FuncPtr = std::add_pointer<decltype(BASS_SampleLoad)>::type;
// Open library you wants
void* soHandle = dlopen("your_lib_here.so", RTLD_LAZY);
// Error check!
if (nullptr == soHandle) {
    // Fail here
}
// Finally, get pointe to function!
BASS_SampleLoad_FuncPtr BASS_SampleLoad = reinterpret_cast<BASS_SampleLoad_FuncPtr>(dlsym(soHandle, "BASS_SampleLoad"));
// Error check!
if (nullptr == BASS_SampleLoad) {
    // Fail here
}
// Now only here it's safe to call "BASS_SampleLoad" with required params
auto sample = BASS_SampleLoad(...);

...

// Don't forget to close lib
dlclose(soHandle);

Please, NOTE!
Provided code is not tested and might contain errors. And, it's C++11 standard.
Also, for C++14 and higher replace 'std::add_pointer<...>::type' with 'std::add_pointer_t<...>'
P.S. this code valid because BASS is cross-platform library and all WinAPI-look-a-like stuff (WINAPI, QWORD, BOOL, DWORD, etc.) is defined for Linux in BASS header
Comment: It works only if use using decltype, thanks for the answer.

BASS_SampleLoad_FuncPtr = std::add_pointer::type;

How to redefine c++ pointer function?


Proportional hazards assumption Cox's model vs AFT

Question: I'm modelling survival data with Cox's model and AFT model (weibull) with only treatment as dependent variable.
From the KM plot of the survival probability i saw that there isn't a different trend by the two groups and that the distance between the two curves rests constantly over time. (the two curves cross each other but rest "crossing" all the follow-up time) That suggest to us that the PH assumption is met for the treatment variable.
For testing the proportional hazards assumption in another way I add a time dependent variable for the treatment in the Cox's model, and it results not significant, so seems the assumption is met for the treatment variable.
The problem is that if I try to add the same time dependent variable to AFT weibull model the variable result significant and the GOF of the model result incremented.
I have to make a confrontation between the two models so, I consider the assumption met and use only the treatment variable for all the two models or I add the time dependent variable?
Answer: You are doing a lot of hypothesis testing, which is generally not a good way to decide on a model. Ideally, a single model would be pre-specified or you could do model averaging between candidate models. The described data-dependent way of picking a model will invalidate inference from the model that is picked at the end.
In general, it may be that the parametric model is more powerful than the semi-parametric model. Alternatively, it could be that the Weibull model does not fit the data all that well, but that adding the interaction makes the model a little more flexible so that it fits the data better. In that case, you may decide that it's more of an artifact of forcing a Weibull model on the data.
One way of getting an approximate idea of what is going on would be to plot the survivor curves (or CDFs) from all models (Cox with and without time interaction, as well as Weibull with and without time interaction) together with the Kaplan-Meier curves for each treatment group. This will tell you whether one of these models is fitting the data terribly.
Comment: Compare the survivor curves it's a good idea. Thank you, for your answear!


Is the boundary of (countable) intersction of open sets included in the closure of the union of the boundaries?

Question: Let $\{ U_i \}$ be a family of open sets. From the example of $U_i = (-1/i, 1/i) \subset \mathbb{R}$ for $i \in \mathbb{N}$, I know that it's not true that $\partial( \bigcap\limits_i U_i) \subseteq \bigcup\limits_i \partial(U_i) $. However, I've been trying to prove that
$$ \partial(\bigcap\limits_i  U_i) \subseteq \overline{\bigcup\limits_i \partial(U_i) } $$
In other examples I could think of, this is also true.
My attempt: Let $z \not\in \overline{\bigcup\limits_i \partial(U_i) }$. Then there exists $U$ a neighborhood of $z$ such that $U$ doesn't intersect any $\partial(U_i)$.
If there is some $i$ such that $z \not\in U_i$, then a whole neighborhood of $z$ doesn't intersect $U_i$, so a whole neighborhood of $z$ doesn't intersect $\bigcap\limits_i  U_i$, so $z \not\in \overline{\bigcap\limits_i  U_i}$, so $z \not\in \partial(\bigcap\limits_i  U_i)$.
So we can suppose $U \cap U_i \neq \emptyset$ and $U \cap \partial(U_i) = \emptyset$ for all $i$.
I would like to conclude that $z \in int(\bigcap\limits_i  U_i)$. Is this possible in general, or, if not, is there a counterexample? How about with some assumptions on the space? For example, I'm thinking if the space has a basis of connected open sets, then it would be done if I could conclude that a constant connected component of $U$ is included in each $U_i$, since connected components are open. But is it true that, as intuition has it, the boundary "separates" a set into connected components?
Also, I suspect countability of the family is not necessary, unless Baire's theorem somehow appears, but I'd be surprised.
Answer: Assuming that your space is locally connected, that is, that every point has a neighborhood basis of connected open sets, the answer is yes and your argument works.  Indeed, if your set $U$ is taken to be connected, then the fact that $U\cap \partial U_i$ is empty implies that either $U\subseteq U_i$, or $U\subseteq U_i^c$ (complement).  Therefore your assumption that $U\cap U_i$ is not empty for every $i$ leads to $U\subseteq U_i$, for every $i$, so you are done!
On the other hand, if your space fails to be locally connected, there are counter-examples: consider the ternary Cantor set $C\subseteq [0,1]$, and let $U_n= [0,3^{-n}]\cap C$.  It is easy to see that each $U_n$ is an open set with empty boundary, so $\cup_n\partial U_n$ is empty but $\cap_n U_n$ is the singleton $\{0\}$, whose boundary coincides with itself.
Comment: The whole space decomposes as the disjoint union $\text{int}(U_i) \cup \partial U_i\cup\text{ext}(U_i)$, where ext is the interior of the complement.  If $U$ doesn't meet the boundary then $$U=(U\cap \text{int}(U_i)
)\cup(U\cap\text{ext}(U_i))
$$ hence a  partition into disjoint open sets.  Since $U$ is connected, one of these is empty.
Comment: Thank you, could you elaborate on why the boundary of each $U_i$ puts connected open sets either on one "side" or another of it, in the local connectedness assumption?


D7 Related Nodes within a Content Type

Question: Please forgive me if this is a total noob question, but here it is anyway.
I have a content type 'person'. One of the fields I want to add to the 'person' is a relatives field and I want it to either be an autocomplete or a drop down list. I'm pretty sure I can figure that part out. The question is, how can I populate the drop down list (or the autocomplete) with the names of all 'person' content that is already created?
Also, I am using the persons full name for my title field.
I'm also running drupal 7 and I've installed the CTools and Views modules.
EDIT:
After some more thought on this, what I'm really trying to do is have a field that links to a related content node. That way if a person is ever deleted they would also be removed from those who claim to be related to them.
Does drupal have a way to do this? I have so many ideas on how to use drupal, but most of them involve relating nodes to one another in a heirarchal format.
Answer: I would look at the references module, this will allow you to manually set relationships between your "people".
Comment: I believe this should need to tweak manually..., for autocomplete structure format like tags.
Comment: You have an auto complete widget available by default, you can specify which content types to limit the selection to, so in this case James would limit it to only "people" nodes...
Comment: Thanks a ton for helping me find the references module. It also led me to the related modules 'Entity Reference' and 'Relation' which might also fit what I'm looking for.

D7 Related Nodes within a Content Type


Inequality involving slightly larger base and slightly smaller exponent

Question: I recently stumbled over a problem I am not able to solve. I want to show that
$$
(a+1-b)\cdot(a+1)^{b-1}\leq a^b,\quad a,b \in \mathbb{N}_{++}, a\geq b
$$
According to Wolfram Alpha this seems to be true, however I am failing to prove this.
What I tried to do is to take the log and use some inequality on the log ($\log(x+1)\leq x$). However. this bound seems to be too wide. I get
$$
ab-b \leq b\log a
$$
but from there I'm not able to find an upper bound for $b\log a$ that satisfies the inequality.
Does someone have some hints on other inequalities to look into? (No homework, real problem). Thanks!
Answer: $$\iff \frac{a+1-b}{a+1}\leq \left(\frac{a}{a+1} \right)^b\iff 1-\frac{b}{a+1}  \leq \left(1-\frac{1}{a+1}  \right)^b$$
Let's study the function $f(x)=(1-x)^b+bx-1$ for $b\in\mathbb{N}_{++}$ and $x>0$.
We have   $f'(x)=b(1-(1-x)^{b-1})\geq 0$  for $x>0$ and $b \geq 1$.
Then $f(x)$ is increasing function and $f(x) \geq f(0) = 0$. (Q.E.D)
Comment: Thank you, very nice solution
Comment: Hi @NN2, could you drop me an email at firstname.lastname@example.com if you like? We would like to acknowledge your help if you're interested.
Comment: @Leonard Thank you, but don' worry. If necessary (e.g. for a publication that requires this solution), you can delete the question and use the solution.
Comment: Thanks, we didn't end up using this proof, but we still wanted to ask :)
Comment: @Leonard You're welcome! :)


Is there a control for Java Swing that is similar to listview in .NET?

Question: I am looking for a control in Java Swing that is similar to this one in .NET.
Answer: Also consider Outline, discussed here.
Answer: in Swing use JTree along with JCheckBox for what you are looking for.
Use the JTree along with the JTable. See http://java.sun.com/products/jfc/tsc/articles/treetable1/index.html for reference
Comment: What about group and multiple columns? The control can group items and has multiple colums like the image you see. I think JTree can not do it.
Comment: ofcourse you need `JTable`. Thought, that was evident from your table structure.


Views' taxonomy term page by content type

Question: I have two regular content type Article & Blog, both share a term reference field field_tags. When I use Taxonomy term view that come with Views module, it display tags term in both content type.
For example, there are two Article nodes tag as health and three Blog nodes tag as health. Then if I use Taxonomy term view to display health term, it will display five nodes: two Article nodes and three Blog nodes.
Is there a way to separate the display result by content type? For example, if user click on health term on an Article node, Taxonomy term view will only display two Article nodes and if user click on health term on a Blog node, Taxonomy term view will show three Blog nodes.
Prefer not to use an extra module or adding different tag field for Blogs content type. Thank you for helping!
Answer: in the view you can filter by your desired content type.
just add the filter criteria named Content: type
Comment: Thank you for reply. But if I add filter criteria for Content: type (Article) then when user click on term link on Blog node, nothing will show.
Answer: You can achieve that by using Contextual filters in your view. Filter by Content:type and then provide the value in the URL.


Add link to call by Skype in a message - relative reference

Question: I have rules' module and in the last part I set an action to show a message. In that message I want to add a link to call one user by Skype, I am using this:
     $message = "Click to call to your student";

     echo $message;

That only works in php (You can try it http://phptester.net/).
But the problem is that Drupal is using that reference as a relative url so when I click it, it takes me to "http://www.example.com/yyyy/echo123?call". How can I do to do a reference to Skype?.
Thank you so much for your help.
Answer: your link contains "skype" protocol that is filtered out by filter_xss_bad_protocol() Drupal core function.
You can install Filter Protocols module which allows you to configure allowed protocols. Skype protocol is not included in configuration by default, you'll have to add it yourself - then your skype link will work.
Comment: Thank you so much for your help, I didn't know that  :).
Answer: I don't really know about the skype stuff so this might not be ideal but can't you use:
    $message = "Click to call to your student";
That should still open up in skype if you've got it as the default. Also means people without skype e.g. mobile phone users can click it directly and make a call via their phone


Github api Publish a draft release

Question: I have draft release automatically created with github actions.
We can access its url or any other variable.
How can I publish it using github api is there any endpoint or something?
Comment: I don't think this answers your question, but it's related — here's a link to a Python script I have that uses the GitHub API to publish a non-draft release. It might be helpful. https://github.com/Samasaur1/DiceKit/blob/ce0d45142b0d7ea8089c6f6a2db10d5292f1e4ec/scripts/release.py#L32-L50
Answer: I don't have an answer for how to do it using curl but
This github action can do it for you-
Publish Release

Github api Publish a draft release


I want to make a popup which does not allows user to close browser until popup closes

Question: I want to make popup which freezes the browser until the popup is closed. But I am stuck at a point since on closing the browser, it is asking the user to stay or leave but when a user clicks on leave it shuts down. I instead want a that when a user clicks the leave option it either reload the site or do nothing, also on doing the same for second time Chrome is giving alert to prevent any additional dialogue which I want to disable or it should do nothing. Until the popup is closed. So at last users should not be able to close their browser from a closing button of browser or by going back or replacing the URL. It should freeze until the requirement of the popup is fulfilled. Thank you and please help.
Comment: Only chance is to write your own browser. :) Noone wants popups my friend! Really noone.
Answer: You can't force the users to stay on your site as the browser doesn't allow this (think of a malicious site forcing you to stay there). Your approach is probably the best you can do. Show a dialog that says something like. "Please wait while exiting..." or similar.
If you want to hide the "Block additional dialogs from this page" thing you can create your own dialog. Just a div with position: fixed and display: none and then onBeforeUnload show it with display: block.
Here is more on that: Stackoverflow.com
Comment: Thankyou so much for your answer.
Comment: Glad I could help. You may upvote and accept it as answer so that others can see what worked for you. If you need further help just comment :)
Comment: My problem is still not solved. I want to disable back button when my popup appears. Its is a alert box which appears and i want to disable the feature to close the browser unless the popup which is a JavaScript alert box closes.
Comment: You didn't read my answer well. You **can't** stop the browser from closing. If the user wants to leave, he leaves. Browsers don't allow Javascript to interrupt that. Think of a site that blocks you from closing the browser but you as user can't turn it off. This would be a huge security problem.
Answer: In that case you can use bootstrap popup:
like this:
https://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_modal&stacked=h
and on the click event on close button you can add new alert or some other popup..
and closing this type of popup box cannot reflect other data..
It might helps you.!
Comment: Thankyou for your answer. Can you provide me a code to disable close button of browser when popup opens
Comment: How can i prevent user from closing brower when my popup appears. It is a JavaScript alert box. I want that the user cannot close the brower using close button when the box appears.
Comment: Actually we can not detect browser event..this is not in our control..but as I suggested you in my answer you can use a model popup..
Comment: Is there any way i can replace onbeforeunload default dialog box and add custom box which can then prevent user from leaving?


Feature selection with Decision Tree

Question: I'm supposed to perform feature selection of my dataset (independent variables: some aspects of a patient, target varibale: patient ill or not) using a dcision tree. After that with the features selected I've to implement a different ML model.
My doubt is: when I'm implementing the decison tree is it necessary having a train and a test set or just fit the model on the whole data?
Answer: it's necessary to split the dataset into train-test because otherwise you will measure the performance with data used in training and could end up into over-fitting.
Over-fitting is where the training error constantly decrease but the generalization error increase, where by generalization error is intended as the ability of the model to classify correctly new (never seen before) samples.
Comment: yes by using ""feature_importances_" (from sklearn I suppose) you will obtain the list of importance of each feature, from there you can select the most important set of features and train a new model with only the selected subset.

Hope that help
Comment: ok thanks, you're right!! So at the end, after the split and fit I've to use ""feature_importances_"" in order to understand which variables are important (these feattures will be the features of the main model), right?

Feature selection with Decision Tree


What will be resumption order when notifyAll() calls?

Question: static Integer sync = 1;

static void m() throws Exception {
    synchronized (sync) {
        System.out.println(Thread.currentThread().getName());
        sync.wait(1000L);
        System.out.println(Thread.currentThread().getName());

    }
}

static void mthd() throws Exception {
    synchronized (sync) {
        System.out.println("Notifying...");
        sync.notify();
    }
}

public static void main(String[] args) throws Exception {
    Runnable r = new Runnable() {
        @Override
        public void run() {
            try {
                m();
            } catch (Exception ex) {
                Logger.getLogger(IO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    };
    Runnable t = new Runnable() {
        @Override
        public void run() {
            try {
                m();
            } catch (Exception ex) {
                Logger.getLogger(IO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    };
    Runnable g = new Runnable() {
        @Override
        public void run() {
            try {
                mthd();

    } catch (Exception ex) {
                Logger.getLogger(IO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    };
    Thread th1 = new Thread(r);
    Thread th2 = new Thread(t);
    Thread th3 = new Thread(g);
    th1.start();
    th2.start();
    th3.start();
}

The code produce the following output:
Thread-0
Thread-1
Notifying...
Thread-0
Thread-1

That is, we have the same order as the order of adding to wait-set or it doesn't specified?
Comment: What makes you think it might be deterministic?
Comment: @wallyk I don't know. I just ask is it true...
Comment: Threading is inherently non-deterministic. You shouldn't rely on the ordering of execution of threads
Comment: `notify` just marks the threads as able to be run. It's up to the scheduler which ones to run when. Thus, it's unspecified.
Comment: By the way, you shouldn't synchronize on an autoboxed object, as some of them are cached; this can lead to subtle deadlocks between different parts of your application that just happen to be synchronizing on the same object.  String literals (which get interned) also have this problem.  Just do `Object lock = new Object();` instead.
Answer: The notifying order isn't specified so any of the threads can execute at that time. This is all up to the thread scheduler to when each one should execute.
Answer: From the notify() documentation:

If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation.

And notifyAll() documentation:

The awakened threads will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened threads enjoy no reliable privilege or disadvantage in being the next thread to lock this object.

So not only do waiting threads not awake in order, a thread that wasn't previously waiting but wanted the lock for another reason (say, calling a synchronized method) might get the lock first.


Estimate $E[X_1 | X_1>X_2>\cdots>X_k]$ with simulation

Question: Suppose Random variables $(X_1,X_2,\cdots,X_k)$ are mutually independent, but not identically distributed. I want to estimate $E[X_1|X_1>X_2>\cdots>X_k]$ with simulation. I am wondering if what I do is correct or not.
$$E[X_1|X_1>X_2>\cdots>X_k] = \frac{\int x_1 1\{x_1>x_2>\cdots>x_k\} dF(x)}{\int 1\{x_1>x_2>\cdots>x_k\} dF(x)},$$
where $F(x) = \prod_{i=1}^k F_i(x_i)$ is distribution of $X = (X_1,X_2,\cdots,X_k)$. I attempt to generate random variables $x^s_1,\cdots,x^s_k$ such that $x^s_1>x^s_2>\cdots>x^s_k$ using GHK algorithm. Superscirpt $s$ denotes simulation s.
First I attempted to estimate the conditional expectation by
$$\hat{E}[X_1|X_1>X_2>\cdots>X_k] = \frac{\sum_s x_1^s GHK(x^s)}{\sum_s GHK(x^s)}, $$
where $GHK(\cdot)$ is a weigting function that is used for GHK algorithm.
The above estimator works fine when $k$ is small, but when $k$ gets larger, what I have
$$\hat{E}[X_1|X_1>X_2>\cdots>X_k] = \frac{\sum_s x_1^s GHK(x^s)}{\sum_s GHK(x^s)} \approx x^{s^*}_1 $$
for some $s^*$. This means that
$$\frac{GHK(x^{s^*})}{\sum_s GHK(x^s)} \approx 1.$$
I highly doubt that $x^{s^*}_1$ is a reasonable estimation of the conditional expectation. So What I did to estimate the conditional expectation is that I repeat $R$ times and obtain a set of $(x^{s^*_1}_1,\cdots,x^{s^*_r}_1,\cdots,x^{s^*_R}_1)$. Then, define
$$\hat{E}[X_1|X_1>X_2>\cdots>X_k] = \frac{1}{R}\sum_r x^{s^*_r}_1.$$
Ultimately, I wonder what I did to estimate the conditional expectation is correct. Otherwise is there anyway to estimate the conditional expectation with reasonable accuracy?
Answer: I have never heard of this algorithm by Geweke et al. but since it is a special case of importance sampling the fact that a single weight takes all the mass is indicative that the importance function is poorly suited for the target.  Given that the integral is over the set$$\mathfrak H=\{x\in\mathbb R^k;~x_1>x_2>\cdots>x_k\}$$the importance function $G$ should be concentrated on that set, i.e., $G(\mathfrak H)=1$, in which case$$\mathbb E[X_1|X_1>X_2>\cdots>X_k] \approx
\sum_{s=1}^S x_1^s \dfrac{\text dF}{\text dG}(x^s)\Big/\sum_{s=1}^S \dfrac{\text dF}{\text dG}(x^s)$$
Here is a toy implementation in R, using the product of Normal $\prod_{i=1}^k\mathcal{N}(i,1/i^2)$ as the target and the product of Normal $\mathcal N(1,1)\times\prod_{i=2}^k\mathcal N^-(x_{i-1},1/i)$ as importance distribution (where $\mathcal N^-(x_{i-1},1/i)$ denotes the half-normal truncated to the left of its mean, $x_{i-1}$):
<code># target density
tgt=function(x,k=length(x)) sum(dnorm(x,mean=1:k,sd=1/(1:k),log=TRUE))
# importance density
imp=function(x,k=length(x)) dnorm(x[1],mean=1,log=TRUE)+sum(
    dnorm(x[2:k],mean=x[-k],sd=1/(2:k),log=TRUE))
# importance simulation with S iid k-dim vectors
rtr=function(S,k){
  X=matrix(0,S,k)
  X[,1]=rnorm(S)+1
  for(i in 2:k)X[,i]=X[,i-1]-abs(rnorm(S,sd=1/i))#truncated Normal
  X}
# importance weight
wt=function(X){
  w=1:(dim(X)[1])
  for(i in 1:length(w))w[i]=tgt(X[i,])-imp(X[i,])
  w}
# mean approximation
mn=function(S,k=2){
  w=wt(X<-rtr(S,k))
  print(c(sum(X[,1]*(p<-exp(w-max(w))))/sum(p),
           round(sum(p)^2/sum(p^2))),digits=4) #ESS
}
# verification for k=2
vf=function(S){
  X=rbind(rnorm(S)+1,rnorm(S)/2+2)
  sum(X[1,X[1,]>X[2,]])/sum(X[1,]>X[2,])}
</code>
While the importance approximation is variable (as shown by the effective sample size check), it returns a value in the same range as the sheer Monte Carlo approximation
<code>> vf(1e7)
[1] 2.289312
> mn(1e7,2)
[1] 2.276 48689
</code>
The attempt running R independent importance runs and taking
$$\mathbb E[X_1|X_1>X_2>\cdots>X_k] \approx \frac{1}{R}\sum_r x^{s^*_r}_1$$is incorrect because the take-it-all realisations $x^{s^*_r}$ are not realisations from the target distribution.


Resource mailbox rejecting meetings in the past

Question: In our organization we use room calendars to track overtime and leave, which gets tallied up every two weeks for payroll. We've just recently migrated from Zimbra to Exchange 2013, and have hit a snag with this workflow.
Often -- especially if someone is out sick -- people will not put their leave or overtime on the calendar right away, much less ahead of time. This means they're putting their leave on retroactively, usually when the reminder email to do so is sent out.
Still new to Exchange, we were surprised this week to discover that resources automatically reject meeting requests in the past. That's fine, that's probably what we want for all the "real" rooms we have, but it's not what we want for these calendars.
How can we turn this feature off for these calendars? I've dug through every calendar-related PowerShell command I could find to no avail. Making everyone an editor on these calendars is not an option; they need to send invites to the calendar. Is our only option to adopt a different workflow?
Edit: Here's the full calendar processing configuration for one of the relevant calendars:
<code>PS > Get-CalendarProcessing ins.calendar | fl

AutomateProcessing                  : AutoAccept
AllowConflicts                      : True
BookingWindowInDays                 : 730
MaximumDurationInMinutes            : 0
AllowRecurringMeetings              : True
EnforceSchedulingHorizon            : False
ScheduleOnlyDuringWorkHours         : False
ConflictPercentageAllowed           : 0
MaximumConflictInstances            : 0
ForwardRequestsToDelegates          : True
DeleteAttachments                   : True
DeleteComments                      : True
RemovePrivateProperty               : True
DeleteSubject                       : False
AddOrganizerToSubject               : False
DeleteNonCalendarItems              : True
TentativePendingApproval            : True
EnableResponseDetails               : True
OrganizerInfo                       : True
ResourceDelegates                   : {}
RequestOutOfPolicy                  : {}
AllRequestOutOfPolicy               : False
BookInPolicy                        : {}
AllBookInPolicy                     : True
RequestInPolicy                     : {}
AllRequestInPolicy                  : False
AddAdditionalResponse               : False
AdditionalResponse                  :
RemoveOldMeetingMessages            : True
AddNewRequestsTentatively           : True
ProcessExternalMeetingMessages      : False
RemoveForwardedMeetingNotifications : False
MailboxOwnerId                      : domain/Resources/ins.calendar
Identity                            : domain/Resources/ins.calendar
IsValid                             : True
ObjectState                         : Changed
</code>
Answer: Since the bookingwindowindays can only be between 0-1080. I don't think what you are looking for is possible with resource mailboxes.  While there are probably better solutions outside of Exchange for what you are looking for that's a whole other can of worms.  Perhaps this mailbox could be a shared calendar that someone from HR manages.
The AllRequestOutOfPolicy parameter specifies whether to allow all users to submit out-of-policy requests. Valid input for this parameter is $true or $false. The default value is $false.
Out-of-policy requests are subject to approval by a resource mailbox delegate.
Have you tried setting that (AllRequestOutOfPolicy) to $true and set HR as the resource mailbox delegate?
Comment: Unfortunately we have tried setting AllRequestOutOfPolicy to true with a delegate assigned; the request is still immediately rejected, and the delegate receives no notification of any kind.
Comment: "Perhaps this mailbox could be a shared calendar that someone from HR manages." This was the magic sauce that made it all work! Using a shared mailbox rather than a resource, and assigning a delegate who receives forwarded calendar invites, lets us use these calendars in the way we want to. Assigning someone as an Editor of the calendar would work as well, but they'd have to check events on the calendar directly, rather than getting a notice each time a new one is added. Thank you!
Answer: The purpose of a Resource mailbox is to book a "Resource" for current or future use, such as a conference room, projector, etc. Hence the design and limitations of booking the past.
What you are looking for is more for what a Public Folder calendar or SharePoint calendar can handle, or perhaps a Shared mailbox calendar (as PHLiGHT points out).
If you have SharePoint, that would be my recommendation on the way to go, since you can include workflow for approvals of vacation time, or get alerts when someone puts something on the calendar, etc.
Comment: We do not have SharePoint at this time (there's always some rumblings that it's coming, but my department is the one that would set it up and no one here wants it...), but somehow I overlooked @PHLiGHT's Shared mailbox suggestion. I'll have to do some testing of that, thanks for pointing that out!


Olá Mundo no React.JS não funciona!

Question: Estou tentando printar um "Olá Mundo!" no React.JS
Vejam meu código, simplesmente não funciona:
<!DOCTYPE html>

    <meta charset="UTF-8">

Já tentei várias vezes, nunca funciona! O que há de errado aí!?!?
Comment: o react.js está mesmo nessa pasta library?
Comment: Puxa os arquivos da CDN, se funcionar é pq o caminho aí dos arquivos estão errados.
Comment: Não, não! O caminho está certo. Os arquivos .js estão armazenados na library sim. Nem por CDN esse tal de React não funciona! Já tentei assim também!
Answer: Creio que o erro esteja no Babel, ele é um pré processador de javascript, não acredito que se possa usá-lo via cdn, ele deve ser configurado internamente, no snippet do jsfiddle é possível habilitar o Babel, veja funcionando...

Olá Mundo no React.JS não funciona!


Schur decomposition applications

Question: I know that Schur decomposition is important in matrix theory and linear algebra.
I am doing a research and wondering:

Why is it that important?
What are some applications of it outside the math world like in engineering, computer science etc. ?
Comment: Do check your title for typos, please.
Comment: Have you searched in google "Schur decomposition"?
Comment: https://en.wikipedia.org/wiki/Schur_decomposition
Answer: The Schur decomposition $A = USU^T$ of a dense matrix $A$ can be obtained using a sequence of orthogonal similarity transformations. This allows us to obtain the eigenvalues of the matrix $A$ in a numerically reliable manner. Moreover, the eigenvectors of $A$ can be computed by substitution and backtransformation from the (quasi) triangular matrix $S$. It is possible to prevent overflow in the substitution, making the entire process remarkably robust.
A large number of physical systems can be modeled using linear time-invariant dynamical systems
\begin{align}
\dot{x} &= A x + B u(t) \\
y &= Cx + Du(t),
\end{align}
where $x$ describes the state of the system, $u$ is the control and we are primarily interested in the output $y$. A large scale example is a weather model, where the state vector $x$ includes the pressure, temperature, humidity and wind velocity at every grid point. Typically, the dimension is so large that direct integration is too time consuming. The successful application of model reduction by balanced truncation hinges on the matrix $A$ being (asymptotically) stable. Computing the all eigenvalues is the only way to be sure.
Buildings are often modeled using a system of ordinary differential equation of the type $$M \ddot{x} + D \dot{x} + Kx = F(t)$$
Here $x$ is a vector of displacements of nodes in the building and $F$ is an external force, say, an Earth quake. It is not uncommon that the damping matrix $D$ is a linear combination of the mass matrix $M$ and the stiffness matrix $K$. In order to understand the response of the building to the external force, one needs to solve the generalize eigenvalue problem $$Mv = \lambda K x.$$ There is a generalized Schur form which is relevant here.
Dense eigensolvers are used as kernels for sparse codes such as subspace iteration with Ritz acceleration or for the construction of preconditioners for sparse linear systems.
Answer: Why look outside the math world? Here is simple-enough multi-variable calculus
problem that required the Schur decomposition.
Here is an application from Statistics, one that is applied in the derivation of the Kalman filter.
See the comments on that at the end of Jean Marie's answer here.

A complementary view ( is to interpret matrix A as a covariance matrix
  of a certain multivariate normal random variable in  $\mathbb{R^3}$
  and B as the covariance matrix associated to a marginal distribution
  in $\mathbb{R^2}$

And needless to say, some others from dynamical systems and control ( already covered by Carl Christian's answer)

Schur decomposition applications


Eigenvalues relation between matrices that have "similar" rows

Question: Let $A$ be an $n\times n$ matrix whose eigenvalues have all negative real part. Then we construct the matrix $B$ by multiplying each row of $A$ with a positive number, i.e. $(B)_{ij} = a_i (A)_{ij}$ and $a_i>0$.
Do we know that all the eigenvalues of $B$ have negative real parts?
I got to this equation by looking at linear ODEs and it seems obvious that if the system $\dot x = Ax$ is stable then $\dot x=Bx$ is also stable.
I thought that Gershgorin circle theorem can help, but clearly it cannot give a definite general answer, a disc for matrix $A$ can have $0$ in its interior and then it cannot really tell us anything about $B$.
Answer: No, not necessarily.

For example, let
$$
A
=
\begin{pmatrix}
1 & 1 \\
-3 & -2
\end{pmatrix}
$$
Then the characteristic polynomial of $A$ is $x^2+x+1$, whose roots have negative real parts.

But if we multiply the rows of $A$ by $3$ and $1$, respectively, we get the matrix
$$
B
=
\begin{pmatrix}
3 & 3 \\
-3 & -2
\end{pmatrix}
$$
whose characteristic polynomial is $x^2-x+3$, for which the roots have positive real parts.


Can not change color of UISearchBar?

Question: I'm trying to change the background-, tint-, and barTintColor of my searchBar... I've tried so many different ways but none of them worked. I even tried setting my GlobalTintColor to the yellow I want.
Current Code:
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    //UISearchController
    searchController.searchBar.delegate = self
    self.searchController.delegate = self
    searchController.searchResultsUpdater = self
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.dimsBackgroundDuringPresentation = false
    searchController.searchBar.sizeToFit()
    searchController.searchBar.barTintColor = UIColor(red:1.00, green:0.96, blue:0.02, alpha:1.0)
    searchController.searchBar.tintColor = UIColor(red:1.00, green:0.96, blue:0.02, alpha:1.0)
    searchController.searchBar.backgroundColor = UIColor(red:0.36, green:0.39, blue:0.40, alpha:1.0)
    searchController.searchBar.placeholder = "Search for Users"

    if #available(iOS 9.1, *) {
        searchController.obscuresBackgroundDuringPresentation = false
    } else {
        // Fallback on earlier versions
    }

    if #available(iOS 11.0, *) {
        navigationItem.searchController = searchController
    } else {
        // Fallback on earlier versions
    }

    if #available(iOS 11.0, *) {
        self.navigationItem.searchController = self.searchController
        self.navigationItem.hidesSearchBarWhenScrolling = false
    } else {
        tableView.tableHeaderView = searchController.searchBar
    }
    self.navigationItem.titleView?.tintColor = UIColor.yellow
}

Do I have anything set wrong?
Answer: Do it this way:
func SearchBarAppearence() {
    searchController.searchBar.tintColor = .black
    searchController.searchBar.setValue("text of button", forKey:"_cancelButtonText")
    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.white]
    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).attributedPlaceholder = NSAttributedString(string: "text of searchbar", attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedStringKey.font.rawValue: UIFont(name: IRAN_SANS_MOBILE_FONT, size: 14)!]
    UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSAttributedStringKey.font : UIFont(name: IRAN_SANS_MOBILE_FONT, size: 14)!], for: .normal)
}

Comment: the code is tested can i see the whole viewcontroller
Comment: this didn't made any difference
Comment: well, I got it working by creating a complete new searcher - don't know where the error was, although thank you for your help

Can not change color of UISearchBar?


how to remove duplicate Element from Arraylist in java

Question: <code>    { "744101", "744101","744101", "744102",744102","744102","744102","744102","744103","744103"}
      List<String> list2=new new ArrayList<String>();  //
           Arrays.sort(iArr);
                            for(int k=0;k<iArr.length;k++) {
                                list2.add(String.valueOf(iArr[k]));
                            }

   List li2 = new Array List(new HashSet(list2));
</code>
I'm unable to get result while trying to Sort Array list. Please correct me.
Comment: It's not clear if you want just to remove duplicates, or to both remove duplicates and sort the output. Your current code sorts the input array, but adding the elements to the HashSet destroys the order (while eliminating the duplicates).
Comment: my current code is Sorted but duplicate element coming i dont want duplicate
Answer: The TreeSet both sorts the elements and removes the  duplicates.
<code>    String[] array = { "744101", "744101","744101", "744102","744102","744102","744102","744102","744103","744103"};
    List<String> list = new ArrayList<>(new TreeSet<>(Arrays.asList(array)));

    list.forEach((element)->{
        System.out.println(element);
    });
</code>
Answer: Try this:
<code>   List<String> list = new ArrayList<>();
    Set<String> set = new HashSet<>();
    hash.addAll(list);
    list.clear();
    list.addAll(hash);
</code>
And than sort list if you want.
Answer: As Eran mentioned, you current implementation "shuffles" the list due to HashSet implementation being used, as this Set implementation doesn't retain the order. Try using LinkedHashSet instead. As mentioned in javadoc it avoids overheads related to TreeSet.
Code would be something like this
<code> String[] arrayToProcess = { "744101", "744101","744101", "744102","744102","744102","744102","744102","744103","744103"};

 //creates array and sorts the list
 List<String> sortedList = Arrays.asList(arrayToProcess);
 Collections.sort(sortedList);

 //removes duplicates the list, while retaining order of sorted list
 Set<String> uniqueNumbers = new LinkedHashSet<String>();
 uniqueNumbers.addAll(sortedList);
</code>
Note the implementation of Set being used is LinkedHashSet. Also this snippet makes two copies of the array so if array size is huge, I wouldn't suggest using it.
I would suggest you look up the implementations of collections in java. Because each of them has their own strengths and weaknesses:


Reduce numbers of queries in OpenTSDB

Question: I used opentsdb to save my time series data. Of each data point input, I must get 20 value of data points before. But, I have a large numbers of metrics, I can not call query opentsdb api too many times. How can I do to reduce numbers of query from openTSDB?
Answer: As far as I know you can't aggregate different metrics into one single result. But I would suggest two solutions:

You can put multiple metrics queries in one call. If you use HTTP
API endpoint you can do something like this:
http://otsdb:4242/api/query?start=15m-ago&m=avg:metric1{tag1=a}&m=avg:metric2{tag2=b}
You get the results for all queries with the same start(end) dates/times. But with multiple metrics don't forget that it will take longer time...
Redefine your time series.I don't know any details about your data, but if you're going to store and use data you should also think about usage - What queries am I going to use? How often? What would be the most common access to the data? And so on...
That's also what's advised from OpenTSDB documentation [1]:

Cardinality also affects query speed a great deal, so consider the queries you will be performing frequently and optimize your naming schema for those.

So, I would suggest to use tags to overcome this issue of multiple metrics. But as I mentioned I don't know your schema, but OpenTSDB is much more powerful with tags - there are many examples and also filtering options as well.

Edit 1:
From OpenTSDB 2.3 version there is also expression api: http://opentsdb.net/docs/build/html/api_http/query/exp.html
You should be able to handle multiple metric queries together (but I've never used that for any query).

Reduce numbers of queries in OpenTSDB


How can I edit a pinmux for BeagleBone Black on linux kernel 3.17?

Question: I can take a peek at /sys/kernel/debug/pinctrl/44e10800.pinmux/pins and see the pin I am interested in:
pin 38 (44e10898.0) 00000037 pinctrl-single

This corresponds to GPIO[2]4, or P8.10.
I'm using Fedora 21, with kernel 3.17.7-300.fc21.armv7hl.
I would like to change the mux to 0x27. In words, this would configure the pin to have an internal pull-down resistor (before, it was pull up).
The desired output would be:
pin 38 (44e10898.0) 00000027 pinctrl-single

What can be done?
Note: There is no /sys/devices/bone_capemgr.*, as this is not on Angstrom. The typical DTO approach exports the changes by modifying the cape manager. This is not an option.
Edit: Following exploration from tad's line of thought, I did:
dtc -I dtb -O dts -o ~/am335x-boneblack.dts /boot/dtb-3.17.7-300.fc21.armv7hl/am335x-boneblack.dtb. I edited this file to have:
...
    pinmux@44e10800 {
        ...

        example {
            pinctrl-single,pins = <0x898 0x27>;
        };
    };
...

Then, I compiled it again with dtc, stuck it in /boot/dtb-3.17.7-300.fc21.armv7hl/, and rebooted. However, nothing changed. What's happening?
Edit:
As indicated by Charles Steinkuehler, the 0x800 needs to be subtracted from the offset, and "something" needs to reference "example".
If I add 0x098 0x27 to my entry for user_leds_s0, the desired behavior is observed:
...
    user_leds_s0 {
        pinctrl-single,pins = <0x54 0x7 0x58 0x17 0x5c 0x7 0x60 0x170 0x098 0x27>;
        linux,phandle = <0x3f>;
        phandle = <0x3f>;
    };
...

Now, this is all fine and gets me where I need to go. However, that pin isn't really a user_led. It should be in a separate field of some kind. So, what is the "something" I need to do to get the "example" field or similar to work?
Comment: AFAIK the concept of overlays were removed. You need to manipulate the original .dts file in order to get things done. FYI, this is not related to the flavor of the root file system(in your case, Angstrom). This entirely depends on the kernel version.
Comment: Pardon my ignorance; Can you elaborate a little more on what would be required, supposing that I found the dts file Fedora uses?
Comment: Did you edit the "example" pin definition or add it?  If you added this, I see two problems.  First, something somewhere needs to reference the "example" pin configuration or it will have no effect.  Second, your pinmux register offsets is 0x800 off from what I would expect (ie: 0x898 pinmux register offset turns into 0x098 in the device tree, since the AM335x pinmux driver adds 0x800 for you).  If this is an entry you edited, I am unfamiliar with the device tree naming conventions for your particular kernel.
Comment: I added the "example" pin definition. Is it better to modify an existing definition? Which one? I wasn't able to get it to work by adding `0x98 0x27` to one of them, nor renaming `example` to `pinmux_P8_07_default_pin`. The decompiled dtb looks to be identical to https://github.com/torvalds/linux/blob/bfe01a5ba2490f299e1d2d5508cbbbadd897bbe9/arch/arm/boot/dts/am335x-boneblack.dts after the includes. Notably, I don't see `0x98` anywhere, so I don't know how it is being set to `0x37` in the first place.
Answer: I believe the cape manager stuff has not been ported past the 3.8 kernel.  Not sure if it is planned to be or not, but in the mean time, you can edit the flat device tree instead.  The easiest way I have found (and I'm still looking for the best way) is to grab the repository at
https://github.com/RobertCNelson/dtb-rebuilder
In the src/arm directory, edit the am335x-bone-common-pinmux.dtsi file.  In it, you can search for "P8_10_default_pin:".  This section tell the pinmux to use 0x37 as its default setting.  Change this to 0x27 and save.
Now, build the new compiled device tree (.dtb) file by running make.  On ubuntu, "make install" puts all of the files in the correct place.  I'm not sure on fedora where they go, but digging around in /boot/ should prove interesting.  On ubuntu, the correct spot is
/boot/dtbs/`uname -r`/

In my case, I am running 3.14.26-ti-r43.  After reboot (and exporting the pin), the above change enables the pulldown on the receiver and the open state (value) reads as 0.
root@arm:~# grep 898 /sys/kernel/debug/pinctrl/44e10800.pinmux/pins
pin 38 (44e10898.0) 00000027 pinctrl-single

root@arm:~# echo 36 > /sys/class/gpio/export
root@arm:~# cat /sys/class/gpio/gpio36/value
0

I'm sure there is a more elegant way to accomplish the goal, but this works for me.
Comment: The repos and method you provide is only for 3.14. On the other hand, this has been helpful since I see the .dtb files in `/boot/dtbs/$(uname -r)/`. It seems like I need to modify the .dts(i) file for my specific kernel, but I can't seem to figure out the final step: https://github.com/torvalds/linux/blob/bfe01a5ba2490f299e1d2d5508cbbbadd897bbe9/arch/arm/boot/dts/am335x-bone-common.dtsi . Any idea how to edit P8.10 in that file (or one of the other bone files?)
Comment: There is also a branch in the repo for 3.19.  You could take a look at that one too.  My guess is that the files aren't materially different, certainly not for the pin you are looking at which doesn't appear to have any particularly tricky other uses.  At the very least, you could have a look at what the makefile is doing and duplicate it in the mainline kernel source tree.  If I have a chance I'll pull down a fedora image and poke around a bit.
Comment: I assigned the bounty, but am still looking for a more complete answer. I used `dtc` to get the `am335x-boneblack.dts` file and added a new group under the `pinmux@44e10800` header that looks like `example { pinctrl-single,pins = <0x898 0x27>;};`, but this does not seem to get picked up.
Answer: If you're using Robert Nelson's kernels (or the ones from BeagleBoard), the pinmux should be under the control of a pinmux-helper driver which allows for run-time setting of the pinmux registers to various predefined values.  Look for the file /sys/devices/ocp./P8_10_pinmux./state.  If this exists, you can change the pin to an input with pull-down by:
# echo gpio_pd > /sys/devices/ocp.*/P8_10_pinmux.*/state

...or if you're running one of RCN's Debian builds or have installed my universal overlay you can just:
$ config-pin P8.10 in-

The overlay (for 3.8.13 kernels) and config-pin utility (for any kernel with similar pinmux-helper device-tree entries) can be found here: https://github.com/cdsteinkuehler/beaglebone-universal-io
Comment: This will not work for my kernel. As stated in the original question, the kernel is `3.17.7-300.fc21.armv7hl` provided by Fedora.


Fastest way of getting OpenGraph metatags from HTML?

Question: Beautiful Soup is not efficient enough, so I'm trying to use pure lxml. However, the lxml.html.fromstring is buggy (it suddenly eats 100% RAM after some time), so I need to do it some other way (not with fromstring).
Could I use any of the other modules in the API?  I can't figure it out, and there are surprisingly close to nil examples out there on the internets.
This is what I'm doing now, but as I said, I need to replace the fromstring:
        mySearchTree = fromstring(data)
        metas = {}
        n = -1
        for a in mySearchTree.cssselect('meta'):
            n += 1
            metas[n] = {}
            for b in a.items():
                metas[n][b[0]] = b[1]
        y = 0
        tag = []
        for m in metas:
            if 'property' in metas[m] and 'content' in metas[m]:
                if 'og:' in metas[m]['property']:
                    y += 1
                    tag.append({metas[m]['property'] : metas[m]['content']})

        for x in tag:
            for y in x:
                #print '%s ==> %s' % (y, x[y])
                self.rj[y] = x[y]

Any pointers greatly appreciated!
Comment: Take a look at `etree.iterparse`. If used correctly, it works well. I parsed a 10GB XML file in like 20mb of RAM.
Comment: Does it work with HTML as well?
Answer: As OpenGraph tags are usually at the beginning of HTML document, you could read and parse only part of the input file. Probability the HEAD part will be huge is very small I think (although your research can lead to a different opinion, depends on your sample).

Setup chunk size (e.g. 1024 bytes).
Read input file chunk by chunk (something like stream.read(1024)) until there is  (or , or , etc.) present in the buffer. Decide on limit - number of bytes to read at maximum in case the input is corrupted and has no header or so, so you can give up soon enough you consume a lot of memory.
Use lxml.html parser to read the fragment you have buffered (it's not valid, but it does not matter, lxml can deal with it and it won't affect our goal).
Now, you have lxml DOM object with head of the HTML file. It was created efficiently and with no risk of excessive memory consumption. You can do any searches, extractions, etc. I'd use xpath, but feel free to use your funky DOM tickling code you have above in your question.


How to modify admin pages?

Question: Everytime I log in into the Drupal site there is a resume of the posts that I have written. I would like to modify it and show all the accumulated views of each user post.
I plan to use the Views UI to make the query but I am not sure which file modify to change the admin site.
Thanks.
Answer: If you are meaning the Drupal dashboard (admin/dashboard), there is a documentation page on how to customize the dashboard.
If you want to go further on the customization of the dashboard, there are modules like Homebox that can be useful.

How to modify admin pages?


What is asymptotic error?

Question:
Ng, A.Y., and Jordan, M.I. (2001). On Discriminative vs. Generative classifiers: A comparison of logistic regression and naive Bayes. Advances in Neural Information Processing Systems, 14, pp. 841-8, MIT Press.

In the above paper, the authors mentioned "asymptotic error". Can anyone explain a bit about this?
For example, the abstract of the paper includes:

Discriminative learning has lower asymptotic error, a generative classifier may also approach its higher asymptotic error much faster.

What is the exact definition of "Asymptotic Error"?
Comment: Please add whatever context is necessary to understand & answer your question into the text of the question itself.  Eg, provide a full citation for the paper, & quote the context in which the term is used. People aren't going to want to download & read a paper so that they can answer your question for you, & we want this thread to be informative in the future even if the link goes dead.
Comment: I have added some lines from the paper. Is it understandable ?
Answer: It means the error of a method when you run entire population through it. It's a useful measure of the method as it tells you what's the best you could get from a method. Also, you want to know how quickly the method converges to the asymptotic error, because you can't really run the population in most cases.
Answer: What it means is just the error to which the algorithm is asymptotic. Suppose we have an error that is the limiting error that an algorithm can achieve after a number of iterations, no matter how many. The error for the $i^{th}$ iteration is then (typically) larger than the error associated with a limiting number of iterations. The text is comparing a larger terminal error that is quickly achieved for fewer iterations with a smaller terminal error that takes more iterations to achieve.
A problem with this is that the terminal error may be only relatively constant, such that the language used is inexact. In the quote, "lower" means smaller absolute error, and "higher" means larger absolute error.

What is asymptotic error?


URL contains a number after node/nid: why?

Question: I have just inherited a site to maintain.
The site has many pages ending with /node/$nid. I notice that appending /x to the end of this URL seems to change nothing. That is, if I browse to http://example.com/node/123/1, I get the same result as if I browse to http://example.com/node/123/2.
What are those numbers typically used for? I notice that in my sidebar, it seems to be sending people to the /2 version.
Answer: Drupal's path-matching is greedy. The path node/%nid will match anything with that string at the start, that has a separator after it. e.g

node/%nid/1
node/%nid/giraffe
node/%nid/barack-obama
node/%nid/this/is/just/too/much

The 'extra' path segments are passed on as extra parameters to the item's page callback, whether it wants/uses them or not.
The page that you see at node/%nid/1 isn't different at all from the standard node/%nid page.
You'd do well to keep those URLs out of the page source at all costs, or make sure you have an appropriate canonical URL set, as Google will consider them duplicate content otherwise.
Answer: That is the behavior of Drupal: When a module doesn't define a menu path matching the URL passed to Drupal, Drupal uses the closer menu path defined by a module. That behavior is caused by menu_get_item(), which contains the following code.
$parts = array_slice($original_map, 0, MENU_MAX_PARTS);
$ancestors = menu_get_ancestors($parts);
$router_item = db_query_range('SELECT * FROM {menu_router} WHERE path IN (:ancestors) ORDER BY fit DESC', 0, 1, array(':ancestors' => $ancestors))->fetchAssoc();

In your case, the closer menu path matching the URL you are using is node/%nid, which shows the node content. If you would use http://example.com/node/123/edit, the matching path would be node/%nid/edit, which shows the node edit form.
A module implementing hook_menu_get_item_alter() could easily change that behavior, since menu_get_item() contains the following code.
if ($router_item) {
  // Allow modules to alter the router item before it is translated and
  // checked for access.
  drupal_alter('menu_get_item', $router_item, $path, $original_map);

  $map = _menu_translate($router_item, $original_map);
  $router_item['original_map'] = $original_map;
  if ($map === FALSE) {
    $router_items[$path] = FALSE;
    return FALSE;
  }
  if ($router_item['access']) {
    // …
  }

  // …
}


What to use to generate pdf documents that will incorporate dynamically generated barcodes (Java)?

Question: My requirements ask for generating pdf documents that contain both arbitrary text and barcodes. I have related question that addresses pdf generation part, but here I'd like to know about how to incorporate barcode in pdf in Java.
So far, I've found clear explanation on how barcode4j does it with Apache FOP: Instructions for the Apache FOP extension
But it looks that XSL-FO is not primary option for my requirements as I prefer to go with pdf forms (using iText or PDFBox or similar). Again, this is not final yet.
Do you use images or fonts for barcode in pdf? What dependencies besides pdf API should I expect (fonts, libraries)?
Answer: I succeeded in adding barcodes to PDFs using PDFBox and Barbecue. Barbecue offers the Output interface to draw barcodes yourself. I implemented this interface in a such a way that drawBar() translates to calls to PDPageContentStream.fillRect().
Adding a barcode to the PDF now comes down to:
Barcode barcode = BarcodeFactory.createCode128(text);
barcode.output(new PDFBoxOutput(pageContentStream, startX, startY, height));

The PDFBoxOutput class looks like this:
import java.awt.Color;
import java.io.IOException;

import net.sourceforge.barbecue.output.LabelLayout;
import net.sourceforge.barbecue.output.Output;
import net.sourceforge.barbecue.output.OutputException;

import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;

public class PDFBoxOutput implements Output {

    /** The widths and heights from Barbecue are multipplied with this scalar to get the widths and heights for PDFBox. */
    public final static float SCALAR = 0.5f;

    private final PDPageContentStream stream;
    private final float startX;
    private final float startY;
    private final float height;
    private boolean toggleDrawingColor;

    PDFBoxOutput(PDPageContentStream stream, float startX, float startY, float height) {
        this.stream = stream;
        this.startX = startX;
        this.startY = startY;
        this.height = height;
    }

    @Override
    public void beginDraw() throws OutputException {}

    @Override
    public int drawBar(int x, int y, int width, int height, boolean paintWithForegroundColor) throws OutputException {
        if (paintWithForegroundColor == !toggleDrawingColor) {
            try {
                stream.setLineWidth(0.0f);
                stream.setStrokingColor(Color.BLACK);
                stream.fillRect(startX + SCALAR * x, startY - SCALAR * y, SCALAR * width, this.height);
                stream.stroke();
            } catch (IOException e) {
                throw new OutputException(e);
            }
        }
        return width;
    }

    @Override
    public int drawText(String text, LabelLayout layout) throws OutputException {
        return 0;
    }

    @Override
    public void endDraw(int width, int height) throws OutputException {}

    @Override
    public void paintBackground(int x, int y, int width, int height) {}

    @Override
    public void toggleDrawingColor() {
        toggleDrawingColor = !toggleDrawingColor;
    }

}

Comment: It works. At first, the barcode was generated, but it would not scan. There are 2 possible fixes: 1) Change SCALAR to 1, but the barcodes are HUGE (and ugly), or 2) Change the DPI in Adobe settings (or equivalent): Edit -> Preferences -> Page Display -> Resolution -> Custom resolution at 300 pixels/inch). Mine was at 96.
Comment: I know this isn't the purpose of comments but thank you so much for this, this worked so perfectly for me.
Answer: For generating barcodes in a pdf i would strongly recommend iText to you. If you use maven you can add these dependencies and you can start:
    <dependency>
        <groupId>com.lowagie</groupId>
        <artifactId>itext</artifactId>
        <version>2.0.7</version>
    </dependency>
    <dependency>
        <groupId>bouncycastle</groupId>
        <artifactId>bcmail-jdk14</artifactId>
        <version>136</version>
    </dependency>
    <dependency>
        <groupId>bouncycastle</groupId>
        <artifactId>bcprov-jdk14</artifactId>
        <version>136</version>
    </dependency>

To generate a barcode only a few lines of code are needed:
    Barcode128 code128 = new Barcode128();
    code128.setCodeType(Barcode128.CODE128);
    code128.setCode(new Long(1234559690234234);
    Chunk chunk = new Chunk(code128.createImageWithBarcode(cb, null, null),
            200, -30);
    Paragraph p = new Paragraph(chunk);

Add the paragraph to the document and voila, there you go. A good tutorial can be found here:
IText Example
Answer: I'd use a barcode image generator, then embed that in an HTML documetn I converted to PDF.
Check out this library for rendering XHTML as a PDF. Use barcode4j to render barcodes as images, as you originally planned.
Comment: however this is not free: it requires itext that would require commercial license in my case.
Comment: It's GPL3. It would not require a commercial license, unless you require idemnification.
Answer: If you are prepared to relax your PDF generation requirements to using a non-Java tool, you might find the following useful:

Layout your page templates using HTML/CSS/JS with placeholders for the barcodes.
Use Barcode4J to output SVG and then put that into the template.
Render the page using the wkhtmltopdf command-line tool. wkhtmltopdf uses WebKit under the hood so it gives you good control of the PDF layout using HTML/CSS.


How do I compute maximum number of questions needed to guess a number.

Question: In the classical number guessing game, you guess the number from 1 to 100. The answers, if you do not get the correct number, state whether your guess is higher or lower. In this setup, using optimal binary search strategy, you need at maximum 7 tries to guess the number (the last try is the try stating the correct number).
What do I ask is how do I compute the number of necessary tries for games with different range of possible numbers. In the original game, we have 100 different numbers (N=100) and 7 tries is enough in the worst case (X=7).
How do I compute X for any N?
My guess using some simple logic (I am not a mathematician) is something like X=log2(N), rounded up, but it fails for e.g. N = 2, because the result is 1, but I need 2 tries to get the number if I am not lucky.
What is the correct formula?
Comment: log2 seems right ... maybe add or subtract 1 somewhere? Combined with rounding down rather than rounding up? Just try some more numbers (3,4,5 ..) and see where the boundaries are.
Answer: It might be easier to flip this around and ask: What's the largest range you can solve using $X$ questions? It's straightforward to see that, using a bisection strategy, the maximum value of $N$ where you can win is $N = 2^{X} - 1$.
So, for a given $N$, the number of questions you need is the integer $X$ satisfying:
$$2^{X - 1} - 1 < N \leq 2^{X} - 1$$
$$X - 1 < \log_{2}(N + 1) \leq X$$
Meaning:
$$X = \text{ceil}(\log_2(N + 1))$$


Can we apply the following derivatives to $f(y_1)$ depending on Leibniz Rule?

Question: Can we apply the following derivatives to $f(y_1)$ as below depending on Leibniz  Rule?
$$f(y)= \lim_{u\to \infty}{\frac{g(y)-\int_{1}^{u}{\frac{x-\lfloor x \rfloor}{x^{1+y}}dx}}{h(y)-\int_{1}^{u}{\frac{x-\lfloor x \rfloor}{x^{2-y}}dx}}}$$
Now let us supose that the limit results of  $f(y_1)$ will look as $0/0$. Thus by this advantage, can we apply L'Hopital rule to the right side with respect to the parameter $u$?
$$f(y_1)= \lim_{u\to \infty}{\frac{{\frac {d} {du}}(g(y_1)-\int_{1}^{u}{\frac{x-\lfloor x \rfloor}{x^{1+y_1}}dx)}}{{\frac {d} {du}}(h(y_1)-\int_{1}^{u}{\frac{x-\lfloor x \rfloor}{x^{2-y_1}}dx)}}}$$
$$f(y_1)= \lim_{u\to \infty}{\frac{{\frac{u-\lfloor u \rfloor}{u^{1+y_1}}}}{{\frac{u-\lfloor u \rfloor}{u^{2-y_1}}}}}$$
$$ f(y_1)=\lim_{u\to \infty}\frac{u^{2-y_1}}{u^{1+y_1}}$$
Is my way correct ?
Comment: The question keeps changing and I cannot continue to update my answer. Please ask another question instead of changing it
Comment: @Federico , Sorry, but this last shape, if I can take your comment, I will be glad.
Answer:
Old version. Your first formula doesn't make much sense: the expression
  $$
\lim_{u\to\infty} \int_1^u g(x)\,dx
$$
  is a constant, usually denoted as $\int_1^\infty g(x)\,dx$, not a function of $u$; therefore in this case its derivative would be $0$.

If instead you mean
$$
f(u) = \int_1^u g(x)\,dx,
$$
then the Fundamental Theorem of Calculus tells you that
$$
f'(u) = g(u)
$$
for all $u$ at which $g$ is continuous; in your case $u\in\mathbb R\setminus\mathbb Z$.
At $u\in\mathbb Z$, your function $f$ admits left and right derivatives equal to the left and right limits $\lim_{s\to u^\pm}g(s)$.
Comment: Oh, *that* Leibniz rule! I thought you were referring to the [other one](https://en.wikipedia.org/wiki/Product_rule).
Comment: I added some details. You just need the FToC in this case, no need for differentiation under integral sign


How to update/insert data into the table from pipe separated .csv file

Question: I have imported data from .csv file to temp table and it is like
ID          DEPARTMENT      DESIGNATION       LOCATION     REGION       STATE

Test19768   Barclays|Citi Bank|Ireland      developer   NULL    NULL    NULL

and have to update/insert in EMp table like
ID          DEPARTMENT      DESIGNATION   LOCATION        REGION        STATE
Test19768   Barclays        developer       NULL            NULL        NULL
Test19768   Citi Bank|      NULL            NULL            NULL        NULL
Test19768   Ireland         NULL            NULL            NULL        NULL

Need to insert multiple pipe separated value for each user ID. we can't use STRING_SPLIT as we have version 14.
Answer: check this
BULK INSERT TempTable
    FROM 'C:\CSVData\your.csv'
    WITH
    (
        FIRSTROW = 2,
        FIELDTERMINATOR = '|',  --CSV field delimiter
        ROWTERMINATOR = '\n',   --Use to shift the control to next row
        ERRORFILE = 'C:\CSVDATA\ErrorRows.csv',
        TABLOCK
    )

Related Post
Comment: FIELDTERMINATOR  is comma by actual data is pipe separated and hence need to split that data and insert into another table