text:
Q: Why does discarding an EntityFramework.FirstOrDefaultAsync() query or even a OracleCommand.ExecuteNonQueryAsync behave just like I awaited it? For it to actually become fire and forget as I expect it to. I'll need to call it inside a Task.Run(() => DbCallTestCommand()) instead which seems a bit unnecessary to me?
This is an example code that reproduced the issue in LINQPad:
void Main()
{
    "Main start!".Dump();

    _ = DbCallTestCommand();

    "Main Done!".Dump();
}

//Makes no sense to really execute this as a fire and forget but I included it because Im guessing that EF also uses DbCommand behind the scenes.

async Task DbCallTestEF()
{
    var db = new EventContext(this.connectString);

    var query = db.TBLEVENT.Where(t => t.STARTDATE > DateTime.Now.AddDays(-65));

    var result = await query.ToListAsync(); //Takes about 10 seconds to run in total

    "DbCallTestEF done".Dump();
}

async Task DbCallTestCommand()
{
    using var connection = new OracleConnection(this.connectString);
    await connection.OpenAsync();

    using var command = connection.CreateCommand();
    command.CommandText = "UPDATE_STATISTICS"; //<- long running statistic calculation job that the client shouldn't need to wait for.
    command.CommandType = CommandType.StoredProcedure;

    await command.ExecuteNonQueryAsync();

    "DbCallTestCommand done".Dump();
}

Result:
Main Start!
DbCallTestCommand!
Main Done!
The expected result here (imho) is that the main method should complete BEFORE the discarded method. Because I didn't use an await on DbCallTestCommand() but that is not what happens here.
However if I instead discard a method that simply just awaits a Task.Delay. method. Then it works as expected. Main method completes before discarded method.
See here:
void Main()
{
    "Main start!".Dump();

    _ = TaskDelayTest();

    "Main Done!".Dump();
}

async Task TaskDelayTest()
{
    await Task.Delay(10000);

    "TaskDelayTest done!".Dump();
}

Result: (which is the expected result for discarding a task):
Main Start!
Main Done!
TaskDelayTest done!
I'm quite stumped by this and I really think both discards should behave the same (i.e to NOT wait for the methods completion before continuing). So I'm wondering if anyone knows the reason for this and if this is indeed the correct behaviour?

A:
The discard still doesn't behave as expected.

It behaves as it should. Specifically, it doesn't affect program flow at all. You can remove the discard assignment and observe the same behavior.
The Main() function continues to execute while the Task created when you invoke DbCallTestCommand() continues to execute.

which is the expected result for discarding a task

You don't discard the task with that syntax. You tell the compiler that you don't care to track whatever is returned from the function call (task in this case) in a variable. The task is still created and continues to execute until completion.
You can see that without a dependency on your database with the following modification (includes LinqPad's .Dump()):
// The behavior is the same if you change this to
// void Main()
Task Main()
{
    "Main start!".Dump();

    // Same as:
    // DbCallTestCommand();
    _ = DbCallTestCommand();

    "Main Done!".Dump();

    // To force the program to exit immediately, uncomment this:
    // Environment.Exit(0);
    return Task.CompletedTask;
}

async Task DbCallTestCommand()
{
    "DbCallTestCommand() starting".Dump();
    for (int i = 0; i < 3; i++)
    {
        $"DbCallTestCommand() {i}".Dump();
        await Task.Delay(1000);
    }

    "DbCallTestCommand() ending".Dump();
}

Output:

Main start!
DbCallTestCommand() starting
DbCallTestCommand() 0
Main Done!
DbCallTestCommand() 1
DbCallTestCommand() 2
DbCallTestCommand() ending

You will also have compiler warning BC42358 informing you of what's going on:

Because this call is not awaited, execution of the current method
continues before the call is completed

A: After seeing a comment on a tweet about the upcoming ODP.NET 7 release. Where they were asking about if this release will finally come with "true async". I started doing some digging.
As it turns out. No async method in OracleCommand is truly async. They're apparently fake. Which I'm guessing is the most likely reason why "fire and forget" using the _ discard operator simply doesn't work with OracleCommand executes.
True async is however something that will be added in another ODP.NET release version, due sometime in 2023. See: https://github.com/oracle/dotnet-db-samples/issues/144 for more info.

A: Because Oracle driver is not actually "truly" async. See more here.
If for some reason you need fire-and-forget functionality as a quick fix you can wrap the call into Task.Run.
Also note that your example code is not guaranteed to wait for the completion of the fire-and-forget task at all, so depending on how do you run it the "truly" async version can miss any output from the DbCallTestCommand after first await.


text:
Q: use request in form class in django I have a form just like this:
class addMeal(forms.Form):

    name = forms.CharField(max_length=40,widget=forms.TextInput(attrs={'class':'form-control','placeholder':'نام وعده'}))
    foods = forms.ModelMultipleChoiceField(queryset=Food.objects.filter(user=1),widget=forms.SelectMultiple(attrs={'class':'form-control'}))
    class Meta:
            model = Meals

i need write a queryset to get user id with request(see the queryset=Food.objects.filter(user=1) )
what should i do to fix it?

A: You override the constructor:
class MealForm(forms.ModelForm):
    name = forms.CharField(
        max_length=40,
        widget=forms.TextInput(attrs={'class':'form-control','placeholder':'نام وعده'})
    )
    foods = forms.ModelMultipleChoiceField(
        queryset=Food.objects.none(),
        widget=forms.SelectMultiple(attrs={'class':'form-control'})
    )

    def __init__(self, *args, **kwargs, user=None):
        super().__init__(*args, **kwargs)
        if user is not None:
            self.fields['foods'].queryset = Food.objects.filter(user=user)

    class Meta:
            model = Meals
In this case you should also inherit from ModelForm instead of Form. In the view, you then pass the user to the view:
from django.contrib.auth.decorators import login_required

@login_required
def my_view(request):
    MealForm(user=request.user)
    # …

Note: You can limit views to a view to authenticated users with the
@login_required decorator [Django-doc].

redpajama_set_name: RedPajamaStackExchange


text:
Q: Can I replicate all dlls in Global assembly cache from one system to the other? I need to have the same exact dlls on GAC as of my team mates system, is there any way by which I can do this? Any way to import the GAC from one system to the other instead of adding them one by one?
We have the same system configuraion (64 bit, windows 7 )


text:
Q: i am having a problem reading out a data from firebase collection using react and typescript I am able to get all the documents
but i need to get the datas i want and that is where my problem is.i want to query to the point of getting the _fl_meta
my code
firebase


text:
Q: DropDownList value into Linq query where clause I am facing following problem. I want a search field with a DropDowList next to it, where user can pick an item for which he wants to search. I have to make it with this LINQ code not just a SQL query.
Here's my code:
var Metadata = from m in db.Metadatas
    join mm in db.Multimedias
    on m.multimediaID equals mm.multimediaID
    where (m.   { Here would i have the selected value from the dropdownlist. } .ToString().Contains(textboxvalue) ||
           mm.  { Here would i have the selected value from the dropdownlist. } .ToString().Contains(textboxvalue))
           && mm.filetype.ToString().Contains(radiobuttonvalue)

I want to put something like: "Dropdownlist.selectedvalue" into the area { Here would i have the selected value from the dropdownlist. }
I hope you guys understand my idea and problem.

A: I am not sure why you are doing join on in your linq statement.  That is not necessary if you Linq already knows about the relationship between the objects because there is a foreign key relationship setup in the database.
What you can do is this:
var Metedata = db.Metadatas;

switch(Dropdownlist.selectedvalue)
{
  case "one":
    Metadata = Metadata.Where(m => m.{selected value field}.Contains(textboxvalue));
    break;

  case "two":
    Metadata = Metadata.Where(m => m.{selected value field}.Contains(textboxvalue));
    break;

  //More use cases
}

I am not sure what you will be selecting out of the list when you are done, but if it involves related objects (e.g. Multimedias) then you might want to look into DataLoadOptions (LinqToSQL) or .Include() (EntityFrameworks).

A: Just change your sql query:
if(supportgrp.SelectedItem.Text == "All")
sql ="SELECT * FROM QlyData where Date >='" + txtstartdate.Text + "' and Date<='" + txtenddate.Text + "'";
else
sql ="SELECT * FROM QlyData where Date >='" + txtstartdate.Text + "' and Date<='" + txtenddate.Text + "' and suppgrp = '" + supportgrp.Text + "'";


text:
Q: Django: How to access original (unmodified) instance in post_save signal I want to do a data denormalization for better performance, and put a sum of votes my blog post receives inside Post model:
class Post(models.Model):
    """ Blog entry """
    author          = models.ForeignKey(User)
    title           = models.CharField(max_length=255)
    text            = models.TextField()
    rating          = models.IntegerField(default=0) # here is the sum of votes!

class Vote(models.Model):
    """ Vote for blog entry """
    post            = models.ForeignKey(Post)
    voter           = models.ForeignKey(User)
    value           = models.IntegerField()

Ofcourse, I need to keep Post.rating value actual. Nornally I would use database triggers for that, but now I've decided to make a post_save signal (to reduce database process time):
# vote was saved
@receiver(post_save, sender=Vote)
def update_post_votes(sender, instance, created, **kwargs):
    """ Update post rating """
    if created:
        instance.post.rating += instance.value
        instance.post.save()
    else:
        # if vote was updated, we need to remove the old vote value and add the new one
        # but how...?

How can I access the instance value before it was saved? In database triggers, i would have OLD and NEW predefines for this, but is there something like this in post_save signals?
UPDATE
The solution based on Mark's the answer:
# vote was saved
@receiver(pre_save, sender=Vote)
def update_post_votes_on_save(sender, instance, **kwargs):
    """ Update post rating """
    # if vote is being updated, then we must remove previous value first
    if instance.id:
        old_vote = Vote.objects.get(pk=instance.id)
        instance.post.rating -= old_vote.value
    # now adding the new vote
    instance.post.rating += instance.value
    instance.post.save()

A: I believe post_save is too late to retrieve the unmodified version. As the name implies the data has already been written to the db at that point. You should use pre_save instead. In that case you can retrieve the model from the db via pk: old = Vote.objects.get(pk=instance.pk) and check for differences in the current instance and the previous instance.

A: This is not an optimal solution, but it works.
@receiver(pre_save, sender=SomeModel)
def model_pre_save(sender, instance, **kwargs):
    try:
        instance._pre_save_instance = SomeModel.objects.get(pk=instance.pk)
    except SomeModel.DoesNotExist:
        instance._pre_save_instance = instance

@receiver(signal=post_save, sender=SomeModel)
def model_post_save(sender, instance, created, **kwargs):
    pre_save_instance = instance._pre_save_instance
    post_save_instance = instance

A: You can use the FieldTracker from django-model-utils: https://django-model-utils.readthedocs.io/en/latest/utilities.html#field-tracker


text:
Q: Is it a good practice to enable auto-commit in sqlite python all the time? In sqlite python, one has to run commit() to make sure a SQL statement is executed if auto-commit is not enabled. Auto-commit is enabled by doing something like this sqlite3.connect('sqlitedb.db', isolation_level=None)
Is it a good practice to enable auto-commit all the time? THis is to avoid bugs that can happen when one forgets to run commit().
What are some situations, if any, that auto-commit is better to be disabled?
I am using sqlite3 and python v3.6

A: Autocommit should be disabled if multiple operations belong together logically to ensure that not only some of them are executed (atomicity).
Additionally it should be disabled if multiple operations are done consecutively in a short period of time for performance reasons.
For databases with concurrent access from different threads/processes additional consistency considerations apply but this usage is unlikely for Sqlite.


text:
Q: SQL programming involving combining columns to tables but not with join Suppose I have  columns 1, 2, and 3. Also suppose columns 2 and 3 are in the same table but column 1 is not. Finally suppose column 1 and 2 tell the same information (order number),but aren't necessarily in the same ordering, and column 3 holds the price for that order number. How do I add a column to the table 1 is in that copies the information from column 3 but keeps the order number aligned with the price?
Is there a command or do I have to write a function for this?

A: Table1:
---Column1 (order number)

Table2:
---Column2 (order number)
---Column3 (price)

SELECT
   T1.Column1,
   T2.Column3
FROM
   Table1 T1

   INNER JOIN Table2 T2
   ON T1.Column1 = T2.Column2

(the join keeps the order number aligned with the price)


text:
Q: Continuously watch for a condition to be true without render in ReactJs In my react project, I have a requirement where I have to check for a condition continuously and call a function as soon as the condition becomes true.
The condition looks like this:
var El = document.activeElement;
if(document.getElementById('myInputField').getAttribute('aria-invalid') === 'true'
   && !(document.getElementById('myInputField') === El) {

       // function call

}

How can this be achieved in ReactJs?
I'm okay to use jquery/javascript too.
Additional details:
I'm using an Input field from an external project. The input field checks the provided input and alter it's own aria-invalid attribute.
If the user submits the form with invalid input, I want to check if aria-invalid is true and focus is not on input field.

A: The example below shows the correct and incorrect way to manipulate the DOM (in the context of React):
// -> Imports
import React, { useEffect, useState } from "react";

// -> Element Ids
const subHeading = "sub-heading";

// -> DOM Manipulator
// -> Don't do this, opt for conditional style instead
// -> Check h1 element for example
function sideEffect() {
  document.getElementById(subHeading).style.color = "red";
}

// -> Time Constant
const fiveSeconds = 5000;

export default function App() {
  // -> State
  const [readCond, writeCond] = useState(false);
  // -> Side Effects
  // -> Change readCond to true after five second pause
  useEffect(() => {
    setTimeout(() => writeCond(true), fiveSeconds);
  }, []);
  // -> Trigger DOM manipulation when readCond is true
  useEffect(() => {
    if (readCond) sideEffect();
  }, [readCond]);
  return (

React Way - Good

DOM Way - Bad

  );
}

Working sandbox


text:
Q: Using AngularJS Values in JavaScript Function I have a function that calls some code using the Jcrop API that takes in some values to set aspect ratio. However, I need those values to come from a data connection for which I'm using Angular. So I need something like this:

But I get this error when trying to do it that way. If I do it this way:

Then valueToSet actually evaluates, but I can't call it this way as setAspectRatio() isn't defined in any controller (it's a script on my page itself). This is mainly because it uses Jcrop and I didn't want to mix Jcrop with my Angular controller.
How can I accomplish what I'm trying to do here?

A: You don't need the {{}} around the variable name. ng-click=setAspectRatio(valueToSet) should work.


text:
Q: Возвращение нескольких значений функции(1 курс, с++) int read()
{
   int a, b, n;
   double step;
   cout << "Enter a and b: ";
   cin >> a >> b;
   cout << "Enter n > 1: " << endl;
   cin >> n;
   while (n <= 1)
   {
      cout << "Error! Enter n > 1" << endl;
      cin >> n;
   }
   cout << "Enter step: " << endl;
   cin >> step;
   while (step == 0)
      {
          cout << "Error! Enter step != 0" << endl;
          cin >> step;
      }
   //Как мне вернуть все эти значения//
 }

Задание создать функцию для считывания данных, как вернуть все эти значения для использования в другой функции, если return возвращает только одно?

A: Вернуть несколько значений функция не может по определению, однако она может сохранить эти значения через переданные ей указатели или ссылки. Например, через ссылки так:
void read(int& a, int& b, int& n, double& step)
{
   cout << "Enter a and b: ";
   cin >> a >> b;
   cout << "Enter n > 1: " << endl;
   cin >> n;
   while (n <= 1)
   {
      cout << "Error! Enter n > 1" << endl;
      cin >> n;
   }
   cout << "Enter step: " << endl;
   cin >> step;
   while (step == 0)
      {
          cout << "Error! Enter step != 0" << endl;
          cin >> step;
      }
 }

Вызов будет выглядеть так:
   int a, b, n;
   double step;

   read(a,b,n,step);
   //a,b,n и step теперь содержат нужные значения

Также можно запихнуть значения в структуру и возвращать структуру (об этом написали в комментариях), либо передавать указатель/ссылку на структуру в качестве параметра - результат будет тот же.


text:
Q: What is the difference between RSocket + TCP and RSocket + WebSocket? I am completely new to RSocket.
I read the FAQ and the motiviations page (and skimmed the Protocol page) and understand that RSocket can be used on top of TCP, WebSocket and Aeron. But what I don't understand is what the differences are between using RSocket with these transports because all interaction-models can be used with each transport.
I am personally interested in using RSocket channel to enable bi-directional communication but don't know which transport I should use.
For example what are the differences between RSocket (channel) + TCP and RSocket (channel) + WebSocket?
I couldn't find an answer anywhere, so I was hoping someone here could help me out.
Ty in advance.

A: RSocket provides a common programming interface to multiple transports. You can choose the transport based on the qualities of service the transport provides. For example, if you require ease of firewall traversal then choose WebSocket, if you require low-latency and high-throughput transfer choose Aeron. All things are relative. Aeron can traverse firewalls but configuration is more specialised and WebSocket can give reasonable performance but it is not in the same category as Aeron.
Many other factors come into play so you need to understand the underlying transports with the qualities they provide and match these up against your requirements.

A: RSocket let's you program across platforms (JS, iOS, Android, C++ Server) with a single reactive network programming model. Cleanly supporting common reactive operations from frameworks like RxJava (Observable, Flowable, Single, Maybe, Completable).
The underlying transport is an implementation detail.  But it's a critical implementation detail as between a mobile and a server hosted in GCP, WebSocket may be the only viable option.  While in a datacenter you may opt for Aeron or TCP depending on your requirements.
Whatever you choose, you can write against the same higher level model of reactive network operations.  If you know you just need say Aeron for a single server to server operation, you may not need RSocket, you could program directly against Aeron.  RSocket is giving you this abstraction above it.


text:
Q: Call another epic inside one epic RXJS I'm quite new to RXJS and development in general. I started working with rxjs recently and I found myself stuck with the following issue and I would appreciate some help/ guidance and some explanation please.
    export const updateSomethingEpic1 = (action$) =>
 action$
    .ofType('UPDATE_SOMETHING')
    .switchMap(({ result }: { result }) =>
      //SOME API CALL
        .map(({ response }) => updateSomethingSuccess(response))

         **make call to second epic**

        .catch(err => updateSomethingError(err)),
    );

      //My second epic

        export const updateSomethingEpic2 = (action$) =>
 action$
    .ofType('UPDATE_SOMETHING2')
    .switchMap(({ result }: { result }) =>
      //SOME API CALL
        .map(({ response }) => updateSomethingSuccess2(response))
        .catch(err => updateSomethingError2(err)),
    );

My question is how would I make a call to my second epic after my first epic has called the api and made a successful request. Want to make a call in the first epic after updateSomethingSuccess action, which adds response to the store and then call the second api afterwards.

A: Just do return an action.
Code from your sample
.map(({ response }) => updateSomethingSuccess(response) **<--- make call to second epic**)

use
.map(({ response }) => updateSomethingSuccess(response) )

where updateSomethingSuccess(response) is action creator like following
function updateSomethingSuccess(response) {
   return { type: 'UPDATE_SOMETHING2', payload: response}; // HERE
}

Then your epic 2 with ofType UPDATE_SOMETHING2 will be performed.
It's dead simple. You have to modify for your needs


text:
Q: Como dividir elementos de um array usando o laço For? Resumo do Programa
Bom dia, estou desenvolvendo uma calculadora que pede ao usuário a quantidade de números que serão calculados,  a operação e os números em questão. Para guardar os valores, utilizo um array. O código que calcula os números do array é:
O Código
for (i = 0; i < arrayLength; i++) { result /= num[i]; }
Onde arrayLength é o tamanho do array, e "result" é uma variável do tipo double declarada com o valor 0.
Problema
*Supondo que tenhamos um array unidimensional com 2 posições cujos valores sejam num[0] = 120 e num[1] = 5*
O problema é que quando a divisão é executada, o que acaba sendo calculado é: (0 / 120) / 5
Conclusão
Gostaria de saber como percorrer pelo meu array "num" e dividir os valores contidos nele de um modo eficiente. E se possível uma maneira de reduzir o tamanho código.
Código Completo
static void Main(string[] args)
    {

        int arrayLength, loopCounter = 0;
        int numCounter = 1;
        int i = 0;

        double result = 0;
        string operation = "";

        Console.WriteLine("Digite a quantidade de números a serem calculados");

        arrayLength = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("\nEscolha a operação:\n\n1: Soma\n2: Subtração\n3: Multiplicação\n4: Divisão (Temp. Desativado)\n");
        operation = Console.ReadLine();

        Console.WriteLine("");

        double[] num = new double[arrayLength];

        while (loopCounter < arrayLength)
        {
            Console.Write("Digite o {0}º valor: ", numCounter);
            num[loopCounter] = Convert.ToDouble(Console.ReadLine());

            numCounter++;
            loopCounter++;

        }

        switch (operation.ToUpper())
        {
            case "1":
                for (i = 0; i < arrayLength; i++)
                {
                    result += num[i];
                }
            break;

            case "2":
                for ( i = 0; i < arrayLength; i++)
                {
                    result -= num[i];
                }
            break;

            case "3":
            result = 1;
                for (i = 0; i < arrayLength; i++)
                {
                    result *= num[i];
                }
            break;

            case "4":
                for (i = 0; i < arrayLength; i++)
                {
                    result /= num[i];
                }
            break;

            default:
            Console.WriteLine("Operação Inválida.");
            break;
        }

        Console.Write("\nO resultado é: ");

        Console.WriteLine(Math.Round(result,2));
    }

A: Você pode iniciar o result com a primeira posição do array, e o loop começar a partir da segunda, apenas uma sugestão:
 case "4":
 {
     result = num[0];
     for (i = 1; i < arrayLength; i++)
         result /= num[i];
 }
 break;

A: Seu código tem uma caracterísca muito comum em código acadêmicos. São pensados de forma a processar estados matematicos. Tente enxergar como é que você, como humano, faria esse cálculo, e depois rescreva.
Exemplo: Calcule a soma dos 3 números: 1, 2 e 3. Vc não começa assim:

Zero + 1 = 1, 1 + 2 = 3, 3 + 3 = 6

Vc simplesmente faz:

1 + 2 = 3, 3 + 3 = 6

Agora, passe a maneira que você realiza o calculo para códigos:
result = num[0]; // começa já no primeiro número.
for (int i = 1; i < num.Length; i++)
     result += num[i];


text:
Q: Unable to retrieve data from my database with spring I'm trying to save a list of objects that have others objects as attributes using save() method of Spring   CrudRepository.
My service is as below
@Service
@Transactional
public class DBDataManipulatorServiceImpl implements DBDataManipulatorService {

@Autowired
private AircraftRepository aircraftRepository;
@Autowired
private WorkPackageRepository workPackageRepository;
@Autowired
private MyJobRepository myJobRepository;

@Override
@Transactional
public List<MyJob> saveToDataBase(List<MyJob> jobs) {

    List<MyJob> newJobs = new ArrayList<MyJob>();
    MyJob myJob = null;

    for (MyJob job : jobs) {
        myJob = myJobRepository.save(job);
        newJobs.add(myJob);
    }

    return newJobs;
} ...

and myJob class is:
@Entity
@Table(name="job", uniqueConstraints=@UniqueConstraint(columnNames={"jobId"}))
public class MyJob implements Serializable {

@Id
@Column(name = "jobId")
private String jobId;
    private String jobStatus;

    @ManyToOne()
private Aircraft aircraft;

@ManyToOne()
private WorkPackage workPackage;
....

And Aircraft class is:
@Entity
@Table(name="aircraft", uniqueConstraints=@UniqueConstraint(columnNames={"aircraftId"}))
public class Aircraft implements Serializable{

@Id
@Column(name = "aircraftId")
private String aircraftId;

@Column (name = "TYPE")
private String type;

@Column (name = "MODEL")
private String model;

And when I execute the following test :
@Test
public void testSaveToDataBaseWithFiles() {

HTMLDataExtractorServiceImpl htmlDataExtractorService = new    HTMLDataExtractorServiceImpl();
    Map<String, MyJob> jobsMap = htmlDataExtractorService.parsePage("/Report.html");
    List<MyJob> jobs = new ArrayList<MyJob>(jobsMap.values());

    List<MyJob> myjobs =  dbDataManipulatorService.saveToDataBase(jobs);

    jobsMap = myJobsListToMyJobsMap(myjobs);

    MyJob myJob = jobsMap.get("20316408");

    assertEquals("SUCCESS",myJob.getJobStatus());
    assertEquals("50005", myJob.getAircraft().getAircraftId());
    assertEquals("WP6_27", myJob.getWorkPackage().getWorkPackageId());

}

I get the following error:
org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find com.domain.Aircraft with id 50004; nested exception is javax.persistence.EntityNotFoundException: Unable to find com.domain.Aircraft with id 50004

I understand that there is a problem with the save method, but I don't know how to solve it.  Can anyone help, please?

A: Hard to guess, since we don't know what you get from the
Map<String, MyJob> jobsMap = htmlDataExtractorService.parsePage("/Report.html");

but my guess would be the missing cascading configuration. Your jobsMap seems to contain a MyJob object associated with an Aircraft of id 5004, however this Aircraft seems not to be persisted previously. If you mark the association with
@ManyToOne(cascade = CascadeType.ALL)

or
@ManyToOne(cascade = CascadeType.PERSIST)

JPA should persist the Aircraft automatically for you.


text:
Q: Copy ALL sites from remote server to local server Ubuntu & Filezilla Hey all,
I'm new to linux and still learning and I've searched and haven't been able to find the exact same issue anywhere......
I have a CentOS remote server hosting approximately 60 websites. Some just html, others custom scripts, and mostly wordpress.
I want to backup ALL the sites to my local Ubuntu 16.04.1 lamp stack server.
I am using Filezilla and I'm assuming it's not downloading the the /var/www folder because of permissions, but again, I'm new and don't know much.
Questions:
Do I need to change some settings in Filezilla?
Can I download them with terminal but in bulk?
I do understand I will have to grab all the databases and install them locally, separately, but are there any programs that could help automate this process, paid or free?


text:
Q: Tem como usar uma mesma consulta e definir dois "ORDER BY" diferente? Uso o seguinte trecho para uma consultado ao banco de dados
$cs1 = $pdo->query("SELECT TITULO, URL, IMAGEM , TEMPO FROM post ORDER BY CLIQUES DESC LIMIT 5")->fetchAll();

Gostaria então de explorar mais essa consulta com o intuito de usar menos memoria possível.
É o seguinte, na home page do site estou usando duas consultas distintas
$cs1 = $pdo->query("SELECT TITULO, URL, IMAGEM , TEMPO FROM post ORDER BY CLIQUES DESC LIMIT 5")->fetchAll();

$cs2 = $pdo->query("SELECT TITULO, URL, IMAGEM , TEMPO FROM post ORDER BY VOTOS DESC LIMIT 5")->fetchAll();

Como pode se ver a unica coisa que muda entre as consultas é a coluna citada no ORDER BY, onde em um a coluna citada do ORDER BY é CLIQUES e na outra VOTOS.
Na minha visão isso é um desperdício de recursos, já que ambas as consultadas são exibidas na mesma pagina. Existe então alguma maneira de fazer uma unica consulta porem determinar diferentes colunas no ORDER BY ? Algo tipo assim :
<?php
    $colunas = ID, VOTOS, CLIQUES, CATEGORIA;

    $csu = $pdo->query("SELECT TITULO, URL, IMAGEM , TEMPO FROM post ORDER BY $colunas DESC LIMIT 5")->fetchAll();

?>

Ai tipo, a cada vez que atingir o limite de 5, pular pro próximo termo citado na variável $colunas

A: Vejo duas soluções possíveis para esse problema:

*

*Continuar usando duas consultas distintas e criar índices nas colunas CLIQUES e VOTOS. Ao usar índices o seu sistema gerenciador de banco de dados passará a trazer ambas as consultas mais rapidamente.

*Trazer todo o resultset e ordenar com PHP. Dessa forma você não precisa criar os índices e pode ter sempre a mesma consulta.
Ordenação pela coluna CLIQUES:
usort($resultset, function($a, $b) {
    return $a['CLIQUES'] > $b['CLIQUES'];
});

Ordenação pela coluna VOTOS:
usort($resultset, function($a, $b) {
    return $a['VOTOS'] > $b['VOTOS'];
});

A: Bem ivan veloso, quanto a questão dos recursos, fazer estas consultas separadamente, não será tão custoso, pois o MySQL trabalha com Query Cache e desta forma, antes das query's serem executadas, elas serão comparadas com os resultados armazenados no cache, e caso o resultado já exista não será necessário uma nova consulta. Para você verificar se esta funcionalidade está habilitada em seu MySQL você pode executar o comando:
SHOW VARIABLES LIKE 'have_query_cache';

Quanto ao seu problema, caso você queira fazer um retorno apenas, para suas várias consultas com ordenações diferentes, você pode utilizar o recurso do UNION ALL. No entanto o retorno estará agrupado e você terá que tratá-lo no PHP. Sua query ficaria mais ou menos assim:
(SELECT TITULO, URL, IMAGEM , TEMPO FROM post ORDER BY CLIQUES DESC LIMIT 5) as postcliques
UNION ALL
(SELECT TITULO, URL, IMAGEM , TEMPO FROM post ORDER BY VOTOS DESC LIMIT 5) as postvotos

Neste caso, se você possuir mais de 5 registros na tabela post, o retorno será de 10 registros, os 5 primeiros ordenados pelos cliques e os 5 últimos ordenados pelos votos.


text:
Q: How to check if a variable is equal to one string or another string? if var is 'stringone' or 'stringtwo':
    dosomething()

This does not work! I have a variable and I need it to do something when it is either of the values, but it will not enter the if statement. In Java if (var == "stringone" || "stringtwo") works. How do I write this in Python?

A: if var == 'stringone' or var == 'stringtwo':
    dosomething()

'is' is used to check if the two references are referred to a same object. It compare the memory address.
Apparently, 'stringone' and 'var' are different objects, they just contains the same string, but they are two different instances of the class 'str'. So they of course has two different memory addresses, and the 'is' will return False.

A: Two separate checks. Also, use == rather than is to check for equality rather than identity.
 if var=='stringone' or var=='stringtwo':
     dosomething()

A: if var == 'stringone' or var == 'stringtwo':
    do_something()

or more pythonic,
if var in ['string one', 'string two']:
    do_something()

A: This does not do what you expect:
if var is 'stringone' or 'stringtwo':
    dosomething()

It is the same as:
if (var is 'stringone') or 'stringtwo':
    dosomething()

Which is always true, since 'stringtwo' is considered a "true" value.
There are two alternatives:
if var in ('stringone', 'stringtwo'):
    dosomething()

Or you can write separate equality tests,
if var == 'stringone' or var == 'stringtwo':
    dosomething()

Don't use is, because is compares object identity.  You might get away with it sometimes because Python interns a lot of strings, just like you might get away with it in Java because Java interns a lot of strings.  But don't use is unless you really want object identity.
>>> 'a' + 'b' == 'ab'
True
>>> 'a' + 'b' is 'abc'[:2]
False # but could be True
>>> 'a' + 'b' is 'ab'
True  # but could be False

A: for a in soup("p",{'id':'pagination'})[0]("a",{'href': True}):
        if createunicode(a.text) in ['<','<']:
            links.append(a.attrMap['href'])
        else:
            continue

It works for me.


text:
Q: Calling Mailgun Rest API from Oracle APEX I  use Oracle Apex 19 and need to make a procedure to send mail using Mailgun rest API.
if I write on console the next curl code it work fine:
curl --location --request POST 'https://api.mailgun.net/v3/example.com.ar/messages' \
--header 'Authorization: Basic <<my-secrect-api-code>>' \
--form 'from=postmaster@example.com.ar' \
--form 'to=test@gmail.com' \
--form 'subject=Test' \
--form 'template=mytemplatename'

In postman also work correctly:

How I said,  I need translate this code to APEX PL/SQL , I tried
    declare
      -- Local variables here
      i      integer;
      l_resp clob;
      g_url  varchar2(300) := 'https://api.mailgun.net/v3/example.com.ar/messages';

    begin
      -- Test statements here

      apex_web_service.g_request_headers(1).name := 'Authorization';
      apex_web_service.g_request_headers(1).Value := 'Basic my-secret-code';

      l_resp := apex_web_service.make_rest_request(p_url         => g_url,
                                                   p_http_method => 'POST',
                                                   p_parm_name  => apex_util.string_to_table('from:to:subject:template',':'),
                                                   p_parm_value => apex_util.string_to_table('postmaster@example.com.ar:test@gmail.com:helo:mytemplate',':'),
                                                   p_wallet_path =>'file:/backup/wallet',
                                                   p_wallet_pwd  => null);

    dbms_output.put_line(L_resp);

end;

when call the pl/sql procedure I allways get the error:
{
  "message": "'from' parameter is missing"
}

It sound like FORM parameters is not send, but I dont know how to make this request with APEX_WEB_SERVICE.
Can you help me?

A: Adding the parameters "Content-Type" I've solved the problem:
apex_web_service.g_request_headers(2).name := 'Content-Type';
apex_web_service.g_request_headers(2).value := 'application/x-www-form-urlencoded';


text:
Q: Android - Display URLs in Webview from List with Different Times one after each other I have a List and I need to Display all websites from this list in a Loop one after each other in a WebView. Between switching from one website to the other one I have also a individual time which is also in a other list with the same size.
But if I try to display the site in a WebView it sometimes skips a website and the times are not accurate. Can someone improve my code please and tell me where the misstake is?
public class WebshowActivity extends AppCompatActivity {
private List<String> htmlSites = new ArrayList<>();
private List<Integer> timeForSite = new ArrayList<>();
private int counter = 0;
WebView webView;
boolean firstCall = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_webshow);
    //Fullscreen settings
    View decorView = getWindow().getDecorView();
    int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
    decorView.setSystemUiVisibility(uiOptions);
    //implement list from utils
    timeForSite.add(10);//this is the 0th index and it's needed for the first URL load which is emepty
    for (int i = 0; i < Utilities.list.size(); i++) {
        htmlSites.add(Utilities.list.get(i).getPath());
        timeForSite.add(Utilities.list.get(i).getDuration());
    }
    //WebView
    webView = (WebView) findViewById(R.id.webview);
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webView.loadUrl("");
    webView.setWebViewClient(new WebViewClient() {
        public void onPageFinished(WebView view, String url) {
            Toast.makeText(WebshowActivity.this, "Time for this Website: " + timeForSite.get(counter) + "ms", Toast.LENGTH_SHORT).show();
            final Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    webView.loadUrl(htmlSites.get(counter));
                    counter++;
                    if (firstCall == true) {
                        timeForSite.remove(0);
                        firstCall = false;
                    } else {
                        counter++;
                    }
                    if (counter == htmlSites.size()) {
                        counter = 0;
                    }
                }
            }, timeForSite.get(counter));
        }
    });
}

}
The Lists are in a other Class but I think the error is inside the onPageFinished Method.

A: UPDATE: To make scheduling more precise using Timer.
Here is an example:
public class MainActivity extends AppCompatActivity {
    //mock
    private static class SomeComplicatedObject {
        private final String mPath;
        private final Integer mDuration;

        public SomeComplicatedObject(final String path, final int duration){
            mPath = path;
            mDuration = duration;
        }

        String getPath() { return mPath; }
        Integer getDureation() { return mDuration;    }
    }

    private static final List<SomeComplicatedObject> complicatedList = new ArrayList<>();
    static {
        complicatedList.add(new SomeComplicatedObject("http://google.com", 1000));
        complicatedList.add(new SomeComplicatedObject("http://facebook.com", 2000));
        complicatedList.add(new SomeComplicatedObject("http://google.com", 3000));
        complicatedList.add(new SomeComplicatedObject("http://twitter.com", 3000));
    }

    private static final String TAG = "MainActivity";
    private final ArrayList<Pair<String, Integer>> mSitesToDisplay = new ArrayList<>();
    private final Timer mTimer = new Timer("LoadNext");
    private final Handler mHandler = new Handler();
    private WebView mWebView = null;
    private int mIndex = -1;

    private void fillSitesToDisplay(List<SomeComplicatedObject> list) {
        mIndex = -1;
        mSitesToDisplay.clear();
        for (SomeComplicatedObject item : list) {
            mSitesToDisplay.add(new Pair<>(item.getPath(), item.getDureation()));
        }
    }

    private Pair<String, Integer> getNext() {
        mIndex = (mIndex+1) % mSitesToDisplay.size();
        return mSitesToDisplay.get(mIndex);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fillSitesToDisplay(complicatedList);
        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);

        mWebView = (WebView)findViewById(R.id.webview);

        //initial start delayed by 1 sec
        mTimer.schedule(createTimerTask(), TimeUnit.SECONDS.toMillis(1));
    }

    private Runnable mDisplayNextPage = new Runnable() {
        @Override
        public void run() {
            final Pair<String, Integer> pathTimePair = getNext();
            Log.e(TAG, "DisplayNext::run :  Start loading next page : " + pathTimePair.first);
            mWebView.setWebViewClient(new WaitPageLoadedClient(mTimer, createTimerTask(), pathTimePair.second));
            mWebView.loadUrl(pathTimePair.first);
        }
    };

    private TimerTask createTimerTask() {
        return new TimerTriggered(mHandler, mDisplayNextPage);
    }

    //simple utility class for notifying timer triggered event
    //on UI thread.
    private static class TimerTriggered extends TimerTask {
        Runnable mOnTriggered;
        Handler mHandler;

        protected TimerTriggered(Handler handler, Runnable onTriggered) {
            super();
            mHandler = handler;
            mOnTriggered = onTriggered;
        }

        @Override
        public void run() {
            //Timer runs int it's own thread so we need to
            //pass execution to MainThread for accessing WebView.
            mHandler.post(mOnTriggered);
        }
    }

    private static class WaitPageLoadedClient extends WebViewClient {
        private final TimerTask mRunOnFinished;
        private final Timer mTimer;
        private final Integer mDelay;

        public WaitPageLoadedClient(Timer timer, TimerTask toRunOnFinished, Integer delay) {
            mRunOnFinished = toRunOnFinished;
            mTimer = timer;
            mDelay = delay;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            Log.e(TAG, "onPageFinished: Schedule timer for " + mDelay);
            mTimer.schedule(mRunOnFinished, mDelay);
        }
    }
}

It will cycle forever.


text:
Q: Find localized Windows strings I need to find some strings that the current version of Windows is using.  For example, when I create a new folder, it is initially named "New Folder" on English Vista.  I need to programmatically find what that folder would be named on any language and version of Windows that I might be running on.
Anyone have any ideas how to do that?
Thanks Morinar -- I just stumbled upon that article too.  Unfortunately, the stringID doesn't appear to be constant -- it's 30396 on my Vista, which isn't the same as what they show for XP.  So it would appear MS didn't keep it stable.
EDIT: Looks like this isn't possible...?  This apps runs on computers in Germany, The Netherlands, France, Spain, Brazil, Mexico, Vietnam, Taiwan, China, Japan, South Korea, India, Israel, Hungary ...  You get the idea.  It will take a very long time to install all the different language packs and find out what 'New Folder' is in every language.
Perhaps the best option is to default to "New Folder" and make the user change that value if they want to.  I just prefer to have the software figure out as much as it can and spare the user from configuring _yet_another_setting_.

A: Shamelessly cribbed from http://blogs.msdn.com/oldnewthing/archive/2004/01/30/65013.aspx. This is mostly correct but if there is a resource string of "New Folder something else" it will match that:
LPCWSTR FindStringResourceEx(HINSTANCE hinst,
    UINT uId, UINT langId)
{
    // Convert the string ID into a bundle number
    LPCWSTR pwsz = NULL;
    HRSRC hrsrc = FindResourceEx(hinst, RT_STRING,
        MAKEINTRESOURCE(uId / 16 + 1),
        langId);
    if (hrsrc) {
        HGLOBAL hglob = LoadResource(hinst, hrsrc);
        if (hglob) {
            pwsz = reinterpret_cast<LPCWSTR>
                (LockResource(hglob));
            if (pwsz) {
                // okay now walk the string table
                for (int i = 0; i < (uId & 15); i++) {
                    pwsz += 1 + (UINT)*pwsz;
                }

                pwsz+= 1;
            }
        }
    }
    return pwsz;
}

UINT FindResourceStringId(HMODULE resource_handle, LPCWSTR string, UINT langId)
{
    UINT resource_id= -1;

    for (int i= 0; i<65536; ++i)
    {
        LPCWSTR resource_string= FindStringResourceEx(resource_handle, i, langId);

        if (resource_string && wcsncmp(resource_string, string, wcslen(string))==0)
        {
            resource_id= i;
        }
    }

    return resource_id;
}

int main()
{
    HMODULE shell_handle= LoadLibraryW(L"shell32.dll");
    UINT new_folder_id= FindResourceStringId(shell_handle, L"New Folder", 0x409); // look for US English "New Folder" resource id.
}

A: This is not easy.  These strings are private data for Windows Explorer, and as such they can (and probably do) change between releases.  You can hack something up where you do a lot of version checking and read the appropriate resource string, but that seems like a losing battle.  What are you trying to accomplish?

A: Unsure if there is a more elegant way or not (I couldn't seem to find one), but those strings are stored in %windir%\System32\Shell32.dll.  Theoretically, you could merely read that file in and extract the appropriate strings.
Seems a bit hacky-ish, but should get the job done.  Here's a link to an article that discusses where they live in said file:
http://www.askvg.com/customize-new-folder-and-new-shortcut-text-in-windows-xp/
Seems like there could or even should be an interface to them via the Windows API, but trolling through the documentation I couldn't find one. Perhaps you'll have more luck.

A: If you want to handle 80% of the cases, you can start with "New Folder".
As I guess you're in an enterprise environment, you can get the folder names back into a storage, then after a week (or any time) you get through the names; update your app; release the new version which will please the users. (then later publish the results here)
You can preemptively test your app on a range of platform you're suspecting the users to use. to get a first series of folders names.
This will avoid the problem of dealing with code specific with each of the platform you're looking at.
EDIT
Well I get a second thought about that, I guess you might want to warn the user about that "New Folder" if it wasn't rename after some time (say a minute)? then I guess you would need to add a list and a timer ...


text:
Q: Why do Shapefiles and GeoJSON behave differently in GDAL Python? I have a test.shp. I loop over the file, buffer the features, and write the buffers to a new file. I tested writing the buffers to a  Shapefile and to a GeoJSON. If I try to use the new buffer layer afterwards, for example to get a feature count, I get a number for the Shapefile 90 features, but not for the GeoJSON 0 features. What is the reason for that?
The code looks like this
import ogr
test = ogr.Open('test.shp', 0)
lyrTest = test.GetLayer()

For the Shapefile
shpdriver = ogr.GetDriverByName('ESRI Shapefile')
ds = shpdriver.CreateDataSource('Buffer.shp')
lyrBuffer = ds.CreateLayer('buffer', geom_type=ogr.wkbPolygon)
featureDefn = lyrBuffer.GetLayerDefn()

featureTest = lyrTest.GetNextFeature()
while featureTest:
  geomTest = featureTest.GetGeometryRef()
  geomBuffer = geomTest.Buffer(250)
  outFeature = ogr.Feature(featureDefn)
  outFeature.SetGeometry(geomBuffer)
  lyrBuffer.CreateFeature(outFeature)
  outFeature.Destroy()
  featureTest = lyrTest.GetNextFeature()

print lyrBuffer.GetFeatureCount()

For the GeoJSON
GeoJSONdriver = ogr.GetDriverByName('GeoJSON')
ds = GeoJSONdriver.CreateDataSource('Buffer.geojson')
lyrBuffer = ds.CreateLayer('Buffer.geojson', geom_type=ogr.wkbPolygon)
featureDefn = lyrBuffer.GetLayerDefn()

featureTest = lyrTest.GetNextFeature()
while featureTest:
  geomTest = featureTest.GetGeometryRef()
  geomBuffer = geomTest.Buffer(250)
  outFeature = ogr.Feature(featureDefn)
  outFeature.SetGeometry(geomBuffer)
  lyrBuffer.CreateFeature(outFeature)
  outFeature.Destroy()
  featureTest = lyrTest.GetNextFeature()

print lyrBuffer.GetFeatureCount()

A: Gross, but:
del lyrBuffer
del ds  # flushes Buffer.geojson to disk
ds = GeoJSONDriver.Open('Buffer.geojson', 0)
lyrBuffer = ds.GetLayerByIndex(0)
print lyrBuffer.GetFeatureCount()

From what I can tell, OGRGeoJSONDataSource.CreateLayer() returns a OGRGeoJSONWriteLayer, not a OGRGeoJSONLayer (which is what you get when opening an existing file). The former doesn't implement GetFeatureCount() (or much other useful stuff), and the datasource doesn't (a) actually flush the file to disk until it's deleted (ignoring SyncToDisk())or (b) expose any of the private methods that do flush it or enable proper re-reading of the layer.
By contrast, the Shapefile driver uses a single layer class for both reading and writing, and changes are propagated much better when you're editing.
Normally Fiona is a nicer python-based interface to OGR, but in this case it still requires reloading the file:
import fiona

with fiona.open('test.shp', 'r') as source:
    with fiona.open('Buffer.geojson', 'w',
                    crs=source.crs,
                    driver="GeoJSON",
                    schema=source.schema) as sink:

        for f in source:
            sink.write(f)

with fiona.open('Buffer.geojson', 'r') as sink:
    print len(sink)


text:
Q: change function to return entire column The function returns a single state and population but I need to to return all the populations in a column but skip over the first 6 rows
def findpop(state=None):
    f = open(getMediaPath("population_state_reduced (2).csv"), "rt")
    index = 1
    for line in f:
        if index > 3:
            parts = line.split(',')
            if state is None:
                return [(parts[4], int(parts[5]))]
            else:
                for line in f:
                    if parts[4] == state.capitalize():
                        return int(parts[5])
        index += 1

print findpop()

A: If I understood you correct this is exactly what you want:
import csv

def findpop(state=None):
    res = []
    with open(getMediaPath("population_state_reduced (2).csv")) as f:
        reader = csv.reader(f)
        for i, line in enumerate(reader):
            if i > 5: # skip first 6 rows
                if state is None:
                    res.append((line[4], int(line[5])))
                else:
                    if line[4] == state.capitalize():
                        return int(line[5])
    return res


text:
Q: problem in spawning Xvfb, server already running I'm trying to run 2 cypress docker container in parallel using node js spawn
docker run -v path/to/ws:/e2e -w /e2e --network host cypress/included:8.0.0 --spec 'cypress/integration/cytorus-cache/1.feature'

docker run -v path/to/ws:/e2e -w /e2e --network host cypress/included:8.0.0 --spec 'cypress/integration/cytorus-cache/2.feature'

It fails with following error;
Xvfb exited with a non zero exit code.

There was a problem spawning Xvfb.

This is likely a problem with your system, permissions, or installation of Xvfb.

----------

Error: _XSERVTransSocketUNIXCreateListener: ...SocketCreateListener() failed

_XSERVTransMakeAllCOTSServerListeners: server already running
(EE)
Fatal server error:
(EE) Cannot establish any listening sockets - Make sure an X server isn't already running(EE)

So basically, if I run 4 parallel processes, 1 of them connects to X11 server and 3 gets error.
How it can be handlled? I've already tried setting unset DISPLAY as Cypress runs in headless mode by default. But it didn't help.

A: I encountered the same issue when running cypress tests in parallel.
The fix for me is to remove --network host from your command to avoid port conflicts. You may need to find another approach to access the services on your host.
For instance, you can use 172.17.0.1 to access the host on Docker for Linux.

A: As per cypress docker documentation,

When running on Linux, Cypress needs an X11 server; otherwise it spawns its own X11 server during the test run. When running several Cypress instances in parallel, the spawning of multiple X11 servers at once can cause problems for some of them. In this case, you can separately start a single X11 server and pass the server's address to each Cypress instance using DISPLAY variable.

Since the X11 server was already running on the server, I just spawned Xvfb :99 &, then set the address export DISPLAY=:99. And passed the DISPLAY environment variable to the docker cypress container.


text:
Q: How to add namespace to xml intellisense in Visual Studio I have below schema:
Main XSD:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:firstNamespace="firstNamespace" elementFormDefault="qualified" targetNamespace="mainNamespace" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:import schemaLocation=".\firstChild.xsd" namespace="firstNamespace" />
    <xs:element name="firstElement">
        <xs:complexType>
            <xs:group ref="firstNamespace:group" />
        </xs:complexType>
    </xs:element>
    <xs:element name="secondElement" type="xs:date" />
</xs:schema>

Child xsd (with different namespace):
    <?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:tns0="firstNamespace" elementFormDefault="qualified" targetNamespace="firstNamespace" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="firstElement" type="xs:date" />
    <xs:element name="secondElement" type="xs:string" />
    <xs:group name="group">
        <xs:choice>
            <xs:element ref="tns0:firstElement" />
            <xs:element ref="tns0:secondElement" />
        </xs:choice>
    </xs:group>
</xs:schema>

When i add this schema to Visual Studio, i've got next thing:
Screenshot from MS Visual Studio
Elements namespaces doesn't appear before elements name. For example:
<firstElement xmlns="mainNamespace">
  <firstElement xmlns="firstNamespace"/>
</firstElement>

But i need to see namespaces and be able to select elements by namespaces. Example:
<mainNamespace:firstElement>
  <firstNamespace:firstElement/>
</mainNamespace:firstElement>

Can someone explain me why namespaces doesn't appear in Visual Studio or give example with right realization.
Many thanks.


text:
Q: iOS app crashes randomly because of bad install I've had a fairly large app (50mb) in the App Store for several years now with many updates and thousands of users. Before each release I do a lot of testing, analyzing and beta-testing on all sorts of devices and situations before releasing an update.
Every once in a while I get a horrific bug report that says the app is crashing ALL the time and is completely unusable. What seems to fix this EVERY TIME is telling the user to delete the app, power cycle the device and reinstall the app while under wi-fi.
This means to me that under some unknown circumstances to me, my app is being installed badly. And this has been happening through iOS 4, 5, 6 and now 7.
Is this happening to anyone else? Any known causes or fixes besides my reinstall fix?
To make matters worse, this shows up in a few bad reviews that say the app crashes all the time. All the other reviews are 5 stars! I hate getting blamed for a bad install.
Thanks!

A: Shot in the dark here, but could it be related to wireless connectivity (ie an API timing out)? Very hard to say without more details.


text:
Q: MongoDB aggregate chain from matches My targred is to exclude records which are already mateched from previous match aggregation in MongoDb.
In first match I need to fetch records from two countries: United States and Germany.
In second match I need to exclude records from given country, for example United States AND level of "Begginer", but keep other records from United States and Germany. How is that possible?
Example Data:
[
  {
    country: 'United States',
    personName: 'John',
    level: 'Average',
  },
  {
    country: 'United States',
    personName: 'Rina',
    level: 'Begginer',
  },
  {
    country: 'Germany',
    personName: 'John',
    level: 'Average',
  }
]

First match (correct):
{
  country: {$in: ['United States', 'Germany']}
}

Second match (wrong):
{
  $and: [
    { level: { $ne: 'Begginer'} },
    { country: { $eq: 'United States' } },
  ]
}

I do not want to add countries array again in second match phase.

A: You'll need an $or so that you keep all records that are not 'United States':
db.stuff.aggregate([
  { $match: {
      country: {$in: ['United States', 'Germany'] }
    }
  },
  { $match: {
      $or: [
        { country: { $ne: 'United States' } },
        { country: { $eq: 'United States' }, level: { $ne: 'Begginer'} }
      ]
    }
  }
]);

You can then just combined the 2 matches in to one.
db.stuff.aggregate([
  { $match: {
      country: {$in: ['United States', 'Germany'] },
      $or: [
        { country: { $ne: 'United States' } },
        { country: { $eq: 'United States' }, level: { $ne: 'Begginer'} }
      ]
    }
  }
]);

Both these queries will output:
{
        "_id" : ObjectId("5e999bdee125b211df7f361a"),
        "country" : "United States",
        "personName" : "John",
        "level" : "Average"
}
{
        "_id" : ObjectId("5e999bdee125b211df7f361c"),
        "country" : "Germany",
        "personName" : "John",
        "level" : "Average"
}

https://mongoplayground.net/p/oXBqAzBHzUr


text:
Q: Execution plan of es search? I want to use Elasticsearch 5's profile api to find the search execution plan.
Here are my two different querys:
1:
"query": {
    "bool": {
        "should": [
            {
                "term": {
                    "field1": "f1"
                }
            },
            {
                "range": {
                    "field3": {
                        "gt": "1"
                    }
                }
            }
        ]
    }
}

2:
"query": {
    "bool": {
        "filter": [
            {
                "term": {
                    "field1": "f1"
                }
            },
            {
                "range": {
                    "field3": {
                        "gt": "1"
                    }
                }
            }
        ]
    }
}

The difference between the two queries is just that query1 uses should and query2 use filter.
But the children profile results of two queries are the same.
Such as:
"children": [
{
    "type": "BooleanQuery",
    "description": "field1:f1 field3:[2 TO 9223372036854775807]",
    "time": "0.06531500000ms",
    "breakdown": {
        "score": 0,
        "build_scorer_count": 0,
        "match_count": 0,
        "create_weight": 65314,
        "next_doc": 0,
        "match": 0,
        "create_weight_count": 1,
        "next_doc_count": 0,
        "score_count": 0,
        "build_scorer": 0,
        "advance": 0,
        "advance_count": 0
    },
    "children": [
        {
            "type": "TermQuery",
            "description": "field1:f1",
            "time": "0.05287500000ms",
            "breakdown": {
                "score": 0,
                "build_scorer_count": 0,
                "match_count": 0,
                "create_weight": 52874,
                "next_doc": 0,
                "match": 0,
                "create_weight_count": 1,
                "next_doc_count": 0,
                "score_count": 0,
                "build_scorer": 0,
                "advance": 0,
                "advance_count": 0
            }
        },
        {
            "type": "",
            "description": "field3:[2 TO 9223372036854775807]",
            "time": "0.001556000000ms",
            "breakdown": {
                "score": 0,
                "build_scorer_count": 0,
                "match_count": 0,
                "create_weight": 1555,
                "next_doc": 0,
                "match": 0,
                "create_weight_count": 1,
                "next_doc_count": 0,
                "score_count": 0,
                "build_scorer": 0,
                "advance": 0,
                "advance_count": 0
            }
        }
    ]
}]

Does ElasticSearch create multiple threads, then each thread executes one filter item and then the results are merged in the end?
But in my mind,i think es will use a filter pipeline,execute filter items one by one.Such as elasticsearch-order-of-filters-for-best-performance says

In case 1, the execution will be slower because all documents from the past month will need to go through filter A first, which is not cached.
In case 2, you first filter out all the documents without the type XYZ, which is fast because filter B is cached. Then the documents that made it through filter B can go through filter A. So even though filter A is not cached, the execution will still be faster since there are mush less documents left in the filter pipeline.

How es to execute multiple filter item?


text:
Q: elisp code clobbering a buffer, instead of saving off elsewhere... why? I'm having some difficulties when trying to set something up that saves some persistent state, so that I can use the data between emacs invocations.
Using as a starting point some code from another question, I came up with the following little code snippet for something I'm wanting to do:
(defmacro with-output-to-file (path &rest body)
  "record output of commands in body to file"
  `(save-excursion
     (let* ((buf (find-file-noselect ,path))
            (standard-output buf))
       (set-buffer buf)
       (erase-buffer)
       ,@body
       (save-buffer)
       (kill-buffer))))

I then have a function that uses this, like:
(defun my-save-some-data ()
  (with-output-to-file my-data-save-file
                       (prin1 my-data)))

EDIT: These both follow code like the following (previously, these were both setq; thanks to a comment from @phils for inspiring me to switch them to devfar and defcustom):
; note: my actual variable names (and filename value) are different;
; changed for example sake:
(defvar my-data (make-hash-table :test 'equal) "Data for a thing")
(defcustom my-data-save-file "~/tmp/my-data.el" "File to save my data to")

(Note: I also have a function to read the data back in, which happens automatically at load time, or on demand.)
I've set that up to run in a few circumstances (maybe too many?  maybe poor choices?  Anyway, this is what I set up):
(add-hook 'auto-save-hook 'my-save-some-data)
(add-hook 'kill-emacs-hook 'my-save-some-data)
(add-hook 'post-gc-hook 'my-save-some-data)

Most of the time, this works fine.  However, every once in a while, I'm getting a problem where the data gets written to one of my previously-open buffers (killing all previous content there!), and then that buffer gets killed, with the saved changes.
Suffice it to say, this is highly annoying, as the buffer where this happens is frequently somewhere where I've been doing some work, and not necessarily checked it in yet.
I tried altering the macro above, replacing from (set-buffer buf) on with:
       (with-current-buffer buf ; because set-buffer wasn't working??
         (erase-buffer)
         ,@body
         (if (eq buf (current-buffer))
             (progn
               (save-buffer)
               (kill-buffer))
           (message "buffer changed?!"))))))

This has somehow managed to cause it to append to the buffer, instead of overwriting it... so my if statement does seem to be working to some degree... however I don't see the message in my *Messages* buffer, so... I'm not quite sure what's going on.
One thing I think I've noticed (though it's hard to be certain, since I may not be actively paying attention when this happens) is that this happens in a not-then-currently-active buffer, rather than a buffer I'm currently editing.
So, the questions:

*

*Am I doing something wrong here?

*Are there other/better ways of doing this?

*Are there standard ways to save state in a programatic way, that I could be using? (I poked around a bit in apropos, but failed to find anything... though perhaps I just don't know what to look for.)

*What can I do to help myself track this down? (is there a way I can set breakpoints or something?)

*Are there other protections I could use in code like this?

Any other thoughts welcome.  I'm adding some more (message) forms in hopes of getting more debugging info in the mean time.
UPDATE: I've figured out that this only happens with the post-gc-hook.  I don't know if my variables were somehow getting clobbered (and perhaps switching to defvar and defcustom will solve that?), or if there's some sort of obscure bug in the post-gc-hook processing...  checking for reproducing the test-case with this latest change.

A: You can indeed set breakpoints, an easy way to do this is to put (edebug) in the place where you want to break. Then you can use, n for next, SPC for step, and e to eval. You can read more about edebug here.
So you can set a conditional breakpoint as a protection/warning, like this, before your call to (set-buffer):
(when (get-file-buffer my-data-save-file)
 (read-from-minibuffer
   (format "Warning: %s is already being visited by a buffer, contents will be overwritten! Entering edebug" my-data-save-file))
   (edebug))

This will warn you and then enter the debugger if a file you are visiting in some buffer is about to be overwritten by your macro, where you can inspect what is going on.
Here is part the docstring of find-file-no-select:
Read file FILENAME into a buffer and return the buffer.
If a buffer exists visiting FILENAME, return that one, but
verify that the file has not changed since visited or saved.

My guess is that the my-data-save-file is already being visited by a buffer, so that is the buffer that is returned (and subsequently overwritten). But you can really find out what is happening with (edebug).

A: Just a quick reply to some of what you said. Your message never appears probably because you test whether the buffer of with-current-buffer is the current-buffer, which it always is, unless body changes the current buffer.
But you are right to use with-current-buffer instead of save-excursion followed by set-buffer.
As for other ways: why not put your data in a temporary buffer and then use write-file or append-to-fileor write-region?
FWIW, I tried your code briefly and saw no problem. But I just tried a simple (prin1 (symbol-function 'my-save-some-data)) for the body and a constant file name for the file.  I tried with pre-existing file or not, and with pre-existing buffer or not, and with pre-existing unsaved modified buffer or not.
Are you testing with the interpreted code (e.g., macro present) or byte-compiled code?


text:
Q: Is it possible to have two separate listings? I have defined two listings environments, one for Python and other for Bash, both have different settings, this is a minimal example of what i'm using:
\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}

% Some definitions to use color
% Default fixed font does not support bold face
\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{9} % for bold
\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{9}  % for normal

% Custom colors
\usepackage{color}
\definecolor{deepblue}{rgb}{0,0,0.5}
\definecolor{deepred}{rgb}{0.6,0,0}
\definecolor{deepgreen}{rgb}{0,0.5,0}

\usepackage{listings}

% Python style for highlighting
\newcommand\pythonstyle{\lstset{
language=Python,
basicstyle=\footnotesize,
otherkeywords={self},
tabsize=1,
keywordstyle=\ttb\color{deepblue},
emph={__init__, update_port},
emphstyle=\ttb\color{deepred},
stringstyle=\ttb\color{deepgreen},
frame=single,
showstringspaces=false,
float=htpb,
numbers=left,
captionpos=b,
numbersep=5pt,
linewidth=0.95\linewidth,
xleftmargin=0.05\linewidth,
breaklines=true
breakwhitespace=false,
}}

% Python environment
\lstnewenvironment{python}[1][]
{
\pythonstyle
\lstset{#1}
}
{}

% Bash style for highlighting
\newcommand\bashstyle{
   \lstset{
  language=bash,
  keywordstyle=\sffamily\ttm,
  basicstyle=\sffamily\ttm,
  numbersep=5pt,
  frame=tb,
  columns=fullflexible,
  backgroundcolor=\color{yellow!20},
  linewidth=0.95\linewidth,
  xleftmargin=0.05\linewidth,
  breaklines=true,
  captionpos=b
}}

% Bash environment
\lstnewenvironment{bash}[1][]
{
\bashstyle
\lstset{#1}
}
{}

\begin{document}

\lstlistoflistings

\section{test}

\begin{python}[caption=python example]
class MyClass(Yourclass):
    def __init__(self, my, yours):
        bla = '5 1 2 3 4'
        print bla
\end{python}

\begin{bash}[caption=bash example]
$ sudo yum update -y
$ sudo yum install -y https://rdo.fedorapeople.org/rdo-release.rpm
$ sudo yum install packstack
\end{bash}

\end{document}

The result is like this:

As we can see both fall under the same Listings, however it is desirable to have two different listings for both of this styles, they should have separate lists, with separate index names and separate numbering.
For example i should have a "Python code 1: python example" and one "bash code 1: bash example", i should also have a list of python codes and a list of bash codes.
Is it possible to do it? If so how can it be done?
Edited to be more clear.

A: You can try this code and let me know if there is a problem.
I combined information from here for the counters and here for the two different lists.
\documentclass{article}
\usepackage{regexpatch}% http://ctan.org/pkg/regexpatch
\usepackage{listings}% http://ctan.org/pkg/listings

\usepackage{xcolor}

% Some definitions to use color
% Default fixed font does not support bold face
\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{9} % for bold
\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{9}  % for normal

% Custom colors
\usepackage{color}
\definecolor{deepblue}{rgb}{0,0,0.5}
\definecolor{deepred}{rgb}{0.6,0,0}
\definecolor{deepgreen}{rgb}{0,0.5,0}

\newcounter{python}
\newcounter{bash}

\makeatletter
% --------------------------------------- Python
\newcommand{\lstlistpythonname}{List of Python Codes}
\lst@UserCommand\lstlistofpython{\bgroup
    \let\contentsname\lstlistpythonname
    \let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{lor}}%
    \tableofcontents \egroup}
\lstnewenvironment{python}[1][]{%
  \renewcommand{\lstlistingname}{Python Code}%
  \let\c@lstlisting=\c@python
  \let\thelstlisting=\thepython
  \xpatchcmd*{\lst@MakeCaption}{lol}{lor}{}{}%
  \lstset{language=Python,
  basicstyle=\footnotesize,
  otherkeywords={self},
  tabsize=1,
  keywordstyle=\ttb\color{deepblue},
  emph={__init__, update_port},
  emphstyle=\ttb\color{deepred},
  stringstyle=\ttb\color{deepgreen},
  frame=single,
  showstringspaces=false,
  float=htpb,
  numbers=left,
  captionpos=b,
  numbersep=5pt,
  linewidth=0.95\linewidth,
  xleftmargin=0.05\linewidth,
  breaklines=true
  breakwhitespace=false,
  #1}}
  {}
% --------------------------------------- Bash
\newcommand{\lstlistbashname}{List of Bash Codes}
\lst@UserCommand\lstlistofbash{\bgroup
    \let\contentsname\lstlistbashname
    \let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{lop}}%
    \tableofcontents \egroup}
\lstnewenvironment{bash}[1][]{%
  \renewcommand{\lstlistingname}{Bash Code}%
  \let\c@lstlisting=\c@bash
    \let\thelstlisting=\thebash
  \xpatchcmd*{\lst@MakeCaption}{lol}{lop}{}{}%
  \lstset{language=bash,
    keywordstyle=\sffamily\ttm,
    basicstyle=\sffamily\ttm,
    numbersep=5pt,
    frame=tb,
    columns=fullflexible,
    backgroundcolor=\color{yellow!20},
    linewidth=0.95\linewidth,
    xleftmargin=0.05\linewidth,
    breaklines=true,
    captionpos=b,
    #1}}
  {}
\makeatother
\begin{document}

\lstlistofpython
\lstlistofbash
%\lstlistofpseudocode
\begin{bash}[caption=bash example]
$ sudo yum update -y
$ sudo yum install -y https://rdo.fedorapeople.org/rdo-release.rpm
$ sudo yum install packstack
\end{bash}
\begin{python}[caption=python example]
class MyClass(Yourclass):
    def __init__(self, my, yours):
        bla = '5 1 2 3 4'
        print bla
\end{python}
\begin{python}[caption=python example]
class MyClass(Yourclass):
    def __init__(self, my, yours):
        bla = '5 1 2 3 4'
        print bla
\end{python}
\begin{bash}[caption=bash example]
$ sudo yum update -y
$ sudo yum install -y https://rdo.fedorapeople.org/rdo-release.rpm
$ sudo yum install packstack
\end{bash}

\begin{python}[caption=python example]
class MyClass(Yourclass):
    def __init__(self, my, yours):
        bla = '5 1 2 3 4'
        print bla
\end{python}
\begin{python}[caption=python example25]
class MyClass(Yourclass):
    def __init__(self, my, yours):
        bla = '5 1 2 3 4'
        print bla
\end{python}

\end{document}


text:
Q: Find a pattern (of a child tag) and replace the entire parent tag at XML files, without using sed tool Is there a way to find a pattern (of a child tag)
and replace the entire parent tag, using regular expressions?
I'm working from a Linux server without a graphics environment.
I have XML like:
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
  <book category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="CHILDREN">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book category="WEB">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>

I need a shell script that finds the pattern:
<author>J K. Rowling</author>

then replace its complete block:
  <book category="CHILDREN">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>

with:
  <book category="CHILDREN">
    <title lang="en">Hamlet</title>
    <author>William Shakespeare</author>
  </book>

to finally get:
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
  <book category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="CHILDREN">
    <title lang="en">Hamlet</title>
    <author>William Shakespeare</author>
  </book>
  <book category="WEB">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>

with something like <book*<author>J K. Rowling</author>*</book>,
where * is a wildcard for all text or code between <book and <author>...
I have an idea, using Perl, contemplating these logic steps:

*

*Search the line number where the pattern is

*Identify the line number of parent block open and close  tags

*Remove all this content, inside these lines.

*Add the new block inside these lines

But, it is possible, I prefer the first approach.

A: My preferred approach tends to be to use xmlstarlet to manipulate XML data. We declare an xmlstarlet variable $book to reference the subtree that we need to edit
xmlstarlet <682660.xml ed                                               \
    --var book '//book[author="J K. Rowling"]'                          \
    --update '$book' --value ''                                         \
    --update '$book/@category' --value 'CHILDREN'                       \
    --subnode '$book' --type 'elem' --name 'title'  --value 'Hamlet'    \
    --subnode '$book/title' --type attr --name 'lang' --value 'en'      \
    --subnode '$book' --type 'elem' --name 'author' --value 'William Shakespeare'

Output
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
  <book category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="CHILDREN">
    <title lang="en">Hamlet</title>
    <author>William Shakespeare</author>
  </book>
  <book category="WEB">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>

You could also just delete the relevant <book/> subtree and append a new one, but that might break sequential processing so I didn't do that here.

A: When working with structured document formats, use tools explicitly made for working with these.  Regular expressions are mainly for matching text, and an XML document is not really text but rather data structured in a particular way (and newlines etc. are not always significant).  Likewise, sed is a tool for working with lines of text, and this is also not what XML is in general.
Using xq from https://kislyuk.github.io/yq/
xq -x '.book as $new | input |
    (
        .bookstore.book[] |
        select(.author == "J K. Rowling")
    ) |= $new' insert.xml file.xml

This uses xq to convert both your bookstore XML and the element that you want to insert (in insert.xml) into JSON.  It then applies a particular jq expression to the generated JSON document to extract each entry of the .bookstore.book array.  Each element in that array that has an .author field equal to exactly J K. Rowling is then replaced with the element read from insert.xml.
In more detail: We read the contents of the new .book object into an internal variable called $new and then proceed to get the main document by calling input.  The select() statement acts on each individual element of the .bookstore.book array and pulls out the ones with a particular author.  The result of this is a number of "paths" to these matching book entries.  These are updated using |= (the update operator) to the $new value created earlier.
If you want to provide the new XML on the command line instead of via a file, then use a here-document:
xq -x '.book as $new | input |
    (
        .bookstore.book[] |
        select(.author == "J K. Rowling")
    ) |= $new' - file.xml <<'NEW_XML'
<book category="CHILDREN">
  <title lang="en">Hamlet</title>
  <author>William Shakespeare</author>
</book>
NEW_XML

Note that the input filename insert.xml was replaced by a dash on the command line.
The result, given the data in your question, would be
<bookstore>
  <book category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="CHILDREN">
    <title lang="en">Hamlet</title>
    <author>William Shakespeare</author>
  </book>
  <book category="WEB">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>

The xq utility can do in-place editing, if you use its --in-place (or -i) option.

For reference, xq is translating your XML to the following internal JSON representation, which is then handled by jq:
{
  "bookstore": {
    "book": [
      {"@category":"COOKING","title":{"@lang":"en","#text":"Everyday Italian"},"author":"Giada De Laurentiis","year":"2005","price":"30.00"},
      {"@category":"CHILDREN","title":{"@lang":"en","#text":"Harry Potter"},"author":"J K. Rowling","year":"2005","price":"29.99"},
      {"@category":"WEB","title":{"@lang":"en","#text":"Learning XML"},"author":"Erik T. Ray","year":"2003","price":"39.95"}
    ]
  }
}

The data to insert would be converted to something equivalent of
{
  "book": {
    "@category": "CHILDREN",
    "title": {
      "@lang": "en",
      "#text": "Hamlet"
    },
    "author": "William Shakespeare"
  }
}


text:
Q: What is the difference between Canned Policy and Custom Policy? I am looking for an answer at a conceptual level. So please refrain from simply providing a link to Aws documentation as an answer.
It is how a canned policy is generated by boto
@staticmethod
def _canned_policy(resource, expires):
    """
    Creates a canned policy string.
    """
    policy = ('{"Statement":[{"Resource":"%(resource)s",'
              '"Condition":{"DateLessThan":{"AWS:EpochTime":'
              '%(expires)s}}}]}' % locals())
    return policy

And it is how custom policy is generated by the same library
@staticmethod
def _custom_policy(resource, expires=None, valid_after=None, ip_address=None):
    """
    Creates a custom policy string based on the supplied parameters.
    """
    condition = {}
    # SEE: http://docs.amazonwebservices.com/AmazonCloudFront/latest/DeveloperGuide/RestrictingAccessPrivateContent.html#CustomPolicy
    # The 'DateLessThan' property is required.
    if not expires:
        # Defaults to ONE day
        expires = int(time.time()) + 86400
    condition["DateLessThan"] = {"AWS:EpochTime": expires}
    if valid_after:
        condition["DateGreaterThan"] = {"AWS:EpochTime": valid_after}
    if ip_address:
        if '/' not in ip_address:
            ip_address += "/32"
        condition["IpAddress"] = {"AWS:SourceIp": ip_address}
    policy = {"Statement": [{
                 "Resource": resource,
                 "Condition": condition}]}
    return json.dumps(policy, separators=(",", ":"))

To my mind, a canned policy is essentially a custom policy but with fewer attributes.
If it is a correct observation, then why the need for two different policies?

A: Yes, a canned policy can convey only a specific subset of the attributes of a custom policy, but the distinction between the two is more significant.
When you use a canned (pre-defined) policy, the contents of the resulting canned policy document are so deterministic and predictable -- from the elements of the request, itself -- that the policy document doesn't even need to be sent to CloudFront along with the request.
Instead, it's generated locally so that you can sign it, but then it's discarded.  The server generates the identical document based on the request parameters, and validates the signature.
By contrast, with a custom policy, the policy document itself is sent with the request, base-64 encoded, in &Policy= in the URL.  This makes the URL longer, since the policy document has to be sent along, but the policy document itself is now allowed to contain elements that can't be simply extrapolated from the request by simple examination.
Canned policies, then, are (at least to some extent) more "lightweight" -- shorter URLs mean fewer bytes included in the request, and somewhat less processing needed to use them, but they have less flexibility than custom policies.
Comparison matrix:
Using signed URLs @ docs.aws.amazon.com


text:
Q: pandas: groupby + store in another dataframe I asked a similar question last week and now I have a similar issue, but I cannot convert the answer I received in this case.
Basically, I have a dataframe called comms which looks like this:
articleID   Material    commentScore
 1234         News          0.75
 1234         News          -0.1
 5678         Sport         1.33
 5678         News          0.75
 5678        Fashion        0.02
 7412       Politics        -3.45

and another dataframe called arts and it looks like this:
articleID   wordCount      byLine
 1234         1524          John
 5678         9824          Mary
 7412         3713          Sam

I would like to simply count how many comms there are for each articleID, and store this number in a new column of the arts dataframe named commentNumber.
I think I have to use groupby, count() and maybe merge, but I can't figure out why.
Expected output
articleID   wordCount      byLine    commentNumber
 1234         1524          John         2
 5678         9824          Mary         3
 7412         3713          Sam          1

Thanks in advance!
Andrea

A: Use groupby() then count() on one column. At last, map the result with articleID columns of arts.
arts['commentNumber'] = arts['articleID'].map(comms.groupby('articleID')['Material'].count())

print(arts)

   articleID  wordCount byLine  commentNumber
0       1234       1524   John              2
1       5678       9824   Mary              3
2       7412       3713    Sam              1

A: Use Series.map with Series.value_counts:
arts['commentNumber'] = arts['articleID'].map(comms['articleID'].value_counts())
print (arts)
   articleID  wordCount byLine  commentNumber
0       1234       1524   John              2
1       5678       9824   Mary              3
2       7412       3713    Sam              1

Alternative:
from collections import Counter
arts['commentNumber'] = arts['articleID'].map(Counter(comms['articleID']))


text:
Q: re: windows xp like web app uses jquery and doesnt work I happen to be messing with JQuery lately, and I decided to make something like win xp where you have windows that you can: minimize, close, and maximize. And when minimized the window should be marked in a taskbar as minimized.
My code: http://codepen.io/julian-a-avar/pen/xbaRJz
And snippet for those of you interested:

$(function() {
  var window = ".window";
  var head = ".head";
  var close = ".close";
  var maximize = ".maximize";
  var minimize = ".minimize";
  var app = ".app";
  var w1 = $(".w1").clone();
 var w2 = $(".w2").clone();

  $(window).draggable({handle:head}).resizable();

  $(window).focus(function() {
    $(this).css({
      "background-color": "#000000",
      "z-index": "1"
    });
  });
  $(head).focus(function() {
    $(this).parent().css({
      "background-color": "#000000",
      "z-index": "1"
    });
  });
  $(window).blur(function() {
    $(this).css({
      "background-color": "#444444",
      "z-index": "0"
    });
  });

  $(close).click(function() {
    $(this).parents(".window").remove();
    alert("Closing window");
  });

  $(maximize).click(function() {
  if($(this).parents(".window").css("width") == "100vw" && $(this).parents(".window").css("height") == "100vh") {
      $(this).parents(".window").css({
        "top": "0",
        "left": "0",
        "width": "500px",
        "height": "200px"
      });
    } else {
      $(this).parents(".window").css({
        "top": "0",
        "left": "0",
        "width": "100vw",
        "height": "100vh"
      });
    }
  });

  $(minimize).click(function() {
    var element = $(this).parents(".window")[0];
    $(this).parents(".window").css({
      "display": "none"
    });
    if(element == $(".w1")[0]) {
      $(".app1").css({
        "background-color": "#aaaaaa"
      });
    } else if(element == $(".w2")[0]) {
      $(".app2").css({
        "background-color": "#aaaaaa"
      });
    }
  });

  $(".app1").click(function() {
    if($(".w1").length) {
      if($(".w1").css("display") == "none") {
        $(".w1").css({
          "display": "block"
        });
        $(".app1").css({
          "background-color": "#ffffff"
        });
      } else {
        alert("app already exists");
      }
    } else {
      alert("making new app");
      $("body").append(w1);
      newapp();
    }
  });

  $(".app2").click(function() {
    if($(".w2").length) {
      if($(".w2").css("display") == "none") {
        $(".w2").css({
          "display": "block"
        });
        $(".app2").css({
          "background-color": "#ffffff"
        });
      } else {
        alert("app already exists");
      }
    } else {
      alert("making new app");
      $("body").append(w2);
      newapp();
    }
  });

  function newapp() {
   $(window).draggable({handle:head}).resizable();

   $(window).focus(function() {
     $(this).css({
       "background-color": "#000000",
       "z-index": "1"
     });
   });

    $(head).focus(function() {
     $(this).parent().css({
       "background-color": "#000000",
       "z-index": "1"
     });
   });

    $(window).blur(function() {
     $(this).css({
       "background-color": "#444444",
       "z-index": "0"
     });
   });

   $(close).click(function() {
     $(this).parents(".window").remove();
      alert("Closing window");
   });

    $(minimize).click(function() {
     var element = $(this).parents(".window")[0];
     $(this).parents(".window").css({
       "display": "none"
     });
     if(element == $(".w1")[0]) {
       $(".app1").css({
         "background-color": "#aaaaaa"
       });
     } else if(element == $(".w2")[0]) {
       $(".app2").css({
         "background-color": "#aaaaaa"
       });
     }
   });

    $(maximize).click(function() {
  if($(this).parents(".window").css("width") == "100vw" && $(this).parents(".window").css("height") == "100vh") {
       $(this).parents(".window").css({
         "top": "0",
         "left": "0",
         "width": "500px",
         "height": "200px"
       });
     } else {
       $(this).parents(".window").css({
         "top": "0",
         "left": "0",
         "width": "100vw",
         "height": "100vh"
       });
     }
   });
  }
});
* {
  padding: 0;
  margin: 0;
}

body {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}

#taskbar {
  width: 100vw;
  height: 2em;
  background-color: #000000;
  position: absolute;
  left: 0;
  bottom: 0;
  z-index: 2;
}
/*#start_menu {
  background-color: #ffffff;
  width: 2em;
  height: 2em;
}*/
/*#menu {
  height: 150px;
  width: 100px;
  background-color: #aaaaaa;
  position: absolute;
  z-index: 7;
  left: 0;
  bottom: 2em;
  display: none;
}*/

.app {
  height: 2em;
  width: 2em;
  color: #000000;
  background-color: #ffffff;
  float: left;
  cursor: pointer;
  border: solid #000000 1px;
}

.window {
  width: 500px;
  min-width: 275px;
  max-width: 100vw;
  height: 200px;
  min-height: 125px;
  max-height: calc(100vh - 2em);
  background-color: #444444;
  color: #ffffff;
  z-index: 0;
  border: 1px #000000 solid;
  position: absolute;
  top: 0;
  left: 0;
}

.head {
  background-color: #ffffff;
  color: #000000;
}

.title {
  text-align: center;
  cursor: default;
}

.buttons {
  position: relative;
}

.close {
  position: absolute;
  right: .1em;
  top: -1.1em;
  background-color: #aa0000;
  height: 1em;
  width: 1em;
  border-radius: 100%;
  cursor: pointer;
}

.maximize {
  position: absolute;
  right: 1.4em;
  top: -1.1em;
  background-color: #dddd00;
  height: 1em;
  width: 1em;
  border-radius: 100%;
  cursor: pointer;
}

.minimize {
  position: absolute;
  right: 2.6em;
  top: -1.1em;
  background-color: #0000dd;
  height: 1em;
  width: 1em;
  border-radius: 100%;
  cursor: pointer;
}

.content {
  height: 100%;
  padding: 8px;
  font-size: .8em;
  text-align: justify;
  overflow-y: auto;
  overflow-x: hidden;
}

W1

W2

Window 1

      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse tristique massa ipsum, eu posuere tellus interdum vel. Nullam quis lectus feugiat, malesuada enim ac, malesuada sapien. Cras elit tellus, consequat id justo non, convallis interdum nisi. Sed pulvinar, ipsum in imperdiet malesuada, lorem turpis gravida urna, consectetur auctor leo lectus et est. Nunc in pulvinar nulla, in pharetra dolor. Nunc accumsan felis interdum commodo pretium. Praesent rutrum neque et orci congue imperdiet. Fusce et gravida dui. Donec gravida in mauris vitae aliquam. Quisque semper est ut orci lacinia facilisis.

Window 2

      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse tristique massa ipsum, eu posuere tellus interdum vel. Nullam quis lectus feugiat, malesuada enim ac, malesuada sapien. Cras elit tellus, consequat id justo non, convallis interdum nisi. Sed pulvinar, ipsum in imperdiet malesuada, lorem turpis gravida urna, consectetur auctor leo lectus et est. Nunc in pulvinar nulla, in pharetra dolor. Nunc accumsan felis interdum commodo pretium. Praesent rutrum neque et orci congue imperdiet. Fusce et gravida dui. Donec gravida in mauris vitae aliquam. Quisque semper est ut orci lacinia facilisis.

But you see, I happen to find the following problems in my code:

*

*When clicked in a window, the window does not increment z-index

*I'm having a lot of trouble making a maximize button. The windows do maximize, but they don't unmaximize

*When dragging a window to the right, a scrollbar appears at the bottom, so you can scroll, as the browser window scroll by itself. This is a non wanted effect

The code happens to be so complicated for my brain, that I could not identify where exactly the problems are. What I do know, is that the problems are between line 42 and line 124 of the js column(codepen). As you can see it is a wide range.
Sorry I can't do any better, I just don't know where the problem is.
Thanks!

A: Maximize/Restore not working:
Your maximize/restore didn't work because your if/else should be vice versa. The below code should work. Restored windows has got 500px & 200px size, so when you click maximize it should go to 100vw & 100vh isn't it?
$(maximize).click(function() {
        if($(this).parents(".window").css("width") == "500px" && $(this).parents(".window").css("height") == "200px") {
      $(this).parents(".window").css({
        "top": "0",
        "left": "0",
        "width": "100vw",
        "height": "100vh"
      });
    } else {
      $(this).parents(".window").css({
        "top": "0",
        "left": "0",
        "width": "500px",
        "height": "200px"
      });
    }
  });

Window focus problem:
Div element doesn't need to get focus unless it contains any form elements like select, input or links etc. So by default focus will not get fired for div elements. So by that theory your jquery function for focus/blur will never get fired,
 $(window).focus(function() {
    $(this).css({
      "background-color": "#000000",
      "z-index": "1"
    });
  });
  $(head).focus(function() {
    $(this).parent().css({
      "background-color": "#000000",
      "z-index": "1"
    });
  });
  $(window).blur(function() {
    $(this).css({
      "background-color": "#444444",
      "z-index": "0"
    });
  });

but there is a way to make it work, just add tabindex attribute to your div elements like below with any tabindex value which will make your div element receive focus.

Dragging out to right is wrong:
Your problem about dragging window to the right can be fixed with a small option with "draggable",
$(window).draggable({containment: "parent",handle:head}).resizable();

with containment: "parent", your div element dragging is limited within the parent, will not go outside it and you will not see scrollbar anymore.
Update #1:
For focus not working on head div element click:
I think this is how draggable() jquery call is programmed which makes focus event suppressed for some reason. There were many bugs reported to bugs.jqueryui.com about this for different elements. Back to your problem on window focus, I believe after above solution your issue is only while clicking on title of the window. Because I have checked clicking on body of the window and it works fine. For your title we can do a workaround by not using draggable() instead using a manual drag code as explained below,
Considering your window part of html like below,

Window 1

      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse tristique massa ipsum, eu posuere tellus interdum vel. Nullam quis lectus feugiat, malesuada enim ac, malesuada sapien. Cras elit tellus, consequat id justo non, convallis interdum nisi. Sed pulvinar, ipsum in imperdiet malesuada, lorem turpis gravida urna, consectetur auctor leo lectus et est. Nunc in pulvinar nulla, in pharetra dolor. Nunc accumsan felis interdum commodo pretium. Praesent rutrum neque et orci congue imperdiet. Fusce et gravida dui. Donec gravida in mauris vitae aliquam. Quisque semper est ut orci lacinia facilisis.

Use onmouseup, onmousedown, onmousemove events to do a manual move of the window, a typical programmatic way of doing it in whatever programming we always do.
//  $(window).draggable({containment: "parent",handle:head}).resizable();

    $(document).ready(function() {
        // the below $mywindow is important as that starts and stops the drag operation
        var $mywindow = null;
        var xdiff=0;
        var ydiff=0;
        $(document.body).on("mousemove", function(e) {

            if ($mywindow) {   // see if drag started on our element
                if($mywindow.attr("class")==="head"){  // see if whether it is actually our head element for dragging
                    // and by the way we have to drag our whole window not just head, so .parent()
                    $mywindow.parent().offset({
                        top: e.pageY - ydiff,
                        left: e.pageX - xdiff
                    });
                }
            }
        });
        $(document.body).on("mousedown", "div", function (e) {
            // level up to head as we are clicking on title element
            $mywindow = $(e.target).parent();
              xdiff=e.pageX -$mywindow.offset().left;
              ydiff=e.pageY - $mywindow.offset().top;
        });

        $(document.body).on("mouseup", function (e) {
            xdiff=0;
            ydiff=0;

            // make it null so that the above if($mywindow) will fail on mousemove
            $mywindow = null;
        });
    });

Screenshot:

By they way, consider not using $(window) for your window object as that is also referring to global window object, instead try using something like $(hwnd) ;) I am basically a VC++ guy. No problem as of now, but the problem and confusion will be when you try to use the actual $(window) object. for eg: to check the boundary of the window to limit your dragging etc. Good Luck!