Página virtual - algoritmo Frame allocation

Question: Galera, alguém pode me explicar esse algoritmo de Frame allocation?
Já quebrei a cabeça tentando entender o que cada função faz mas não consigo entender o sentido.
Link do tutorial do código abaixo: http://www.jamesmolloy.co.uk/tutorial_html/6.-Paging.html
<code>// A bitset of frames - used or free.
u32int *frames;

u32int nframes;

// Defined in kheap.c

extern u32int placement_address;

// Macros used in the bitset algorithms.
#define INDEX_FROM_BIT(a) (a/(8*4))

#define OFFSET_FROM_BIT(a) (a%(8*4))

// Static function to set a bit in the frames bitset
static void set_frame(u32int frame_addr)

{
   u32int frame = frame_addr/0x1000;

   u32int idx = INDEX_FROM_BIT(frame);

   u32int off = OFFSET_FROM_BIT(frame);

   frames[idx] |= (0x1 << off);
}

// Static function to clear a bit in the frames bitset
static void clear_frame(u32int frame_addr)

{
   u32int frame = frame_addr/0x1000;

   u32int idx = INDEX_FROM_BIT(frame);

   u32int off = OFFSET_FROM_BIT(frame);

   frames[idx] &= ~(0x1 << off);

}

// Static function to test if a bit is set.
static u32int test_frame(u32int frame_addr)

{
   u32int frame = frame_addr/0x1000;

   u32int idx = INDEX_FROM_BIT(frame);

   u32int off = OFFSET_FROM_BIT(frame);

   return (frames[idx] & (0x1 << off));

}

// Static function to find the first free frame.
static u32int first_frame()

{
   u32int i,    j;
   for (i = 0; i < INDEX_FROM_BIT(nframes); i++)
   {

       if (frames[i] != 0xFFFFFFFF) // nothing free, exit early.

       {
           // at least one bit is free here.
           for (j = 0; j < 32; j++)
           {

               u32int toTest = 0x1 << j;

               if ( !(frames[i]&toTest) )
               {
                   return i*4*8+j;
               }
           }
       }
   }
}
</code>
Answer: Eu comentei o código para você.
O programa procura o primeiro bit livre duma tabela com 1 e 0.
<code>// A bitset of frames - used or free.
u32int *frames; // Tabela de frames de 32 bits, tem que alocar

u32int nframes; // Numero de tabela (tamanho da tabela)

// Defined in kheap.c

extern u32int placement_address; // ?

// Macros used in the bitset algorithms.
#define INDEX_FROM_BIT(a) (a/(8*4))

#define OFFSET_FROM_BIT(a) (a%(8*4))

// Indexo do array: qual é o index do bit 35 por exemplo ?
// Index  = 35 / 32 = 1
// Offset = 35 % 32 = 3
// Para ter o bit 35, você tem que buscar ao indexo 1 da tabela e ler o bit 3
// Tabela: XXXXXXXX XXXX[X]XXX XXXXXXXX XXXXXXXX XXXXXXXX ...
// Index:  0        1          2        3        4
// Offset                3 210

// Static function to set a bit in the frames bitset
static void set_frame(u32int frame_addr)

{
   // dividir por 0x1000 = 4096 = 2^12
   // significa: frame/addr >> 12 (dado é situado depois dos 12 primeiros bits)
   u32int frame = frame_addr/0x1000; // frame tem o bit para procurar.

   u32int idx = INDEX_FROM_BIT(frame); // Obter o index do bit

   u32int off = OFFSET_FROM_BIT(frame); // Obter o offset do bit

   frames[idx] |= (0x1 << off); // Colocar 1 no bit na tabela
}

// Static function to clear a bit in the frames bitset
static void clear_frame(u32int frame_addr)

{
   // Igual, mas esta vez para colocar um 0 no bit da tabela
   u32int frame = frame_addr/0x1000;

   u32int idx = INDEX_FROM_BIT(frame);

   u32int off = OFFSET_FROM_BIT(frame);

   frames[idx] &= ~(0x1 << off); // Colocar 0 no bit da tabela

}

// Static function to test if a bit is set.
static u32int test_frame(u32int frame_addr)

{
   // Igual, mas esta vez para obter o valor do bit
   u32int frame = frame_addr/0x1000;

   u32int idx = INDEX_FROM_BIT(frame);

   u32int off = OFFSET_FROM_BIT(frame);

   return (frames[idx] & (0x1 << off)); // Obter o valor do bit

}

// Static function to find the first free frame.
static u32int first_frame()

{
   u32int i,    j;
   for (i = 0; i < INDEX_FROM_BIT(nframes); i++) // Ler a tabela enterra (até o ultimo indexo)
   {
       // Se cada bit esta utilizado (1), não é útil testar este indexo, passar por o próximo diretamente
       if (frames[i] != 0xFFFFFFFF) // nothing free, exit early.

       {
           // at least one bit is free here.
           // Eu espero que é verdade, senão, o programa nunca vá retornar alguma cosa...
           for (j = 0; j < 32; j++) // Por cada bit do indexo
           {

               u32int toTest = 0x1 << j; // Fazer um mascara para isolar o bit
               // Isolar o bit: se o valor é > 0 (bit = 1, então o bit esta livre)
               if ( !(frames[i]&toTest) )
               {
                   // Retornar um inteiro:
                   // - A partir do bit 6 (2^6 = 32 = 4*8), terá o indexo
                   // - Nos outros bits (0 até 5) terão o offset
                   // Exemplo com 35 (indexo = 1, offset = 3):
                   // Retornado:     00000000 00000000 00000000 01   000011
                   // Numero do bit:                             6        0
                   // Valor:                                     1        3
                   //                < --------- index --------- > | offset
                   return i*4*8+j;
               }
           }
       }
   }
}
</code>


Shapiro-Wilk test on transformed data

Question: I used RStudio to perform Shapiro-Wilk test for normality. In the following data set calling it $D$, I have performed Shapiro-Wilk test for normality with p-value=0.7698.
34.681 26.291 33.280 36.169 41.471 31.528 25.502 43.211 35.330 30.447
If I consider log transform of data set $D$(i.e. take log on each element of $D$), I perform Shapiro-Wilk test on the transformed data set $log(D)$ with p-value=0.7707.
In particular, I have both $D$ and $log(D)$ data set following normal distribution as Shapiro-Wilk test did not reject either one of them.
$Q$: How should I resolve such contradiction? Note data $D$ and data $log(D)$ cannot both be normal.
Comment: There's no contradiction in these results: they merely show that the logarithm does little to change the distribution of a set of data with a small CV.
Answer: This is an excellent example of a non-rejection not implying that the null hypothesis is true, and I will use this the next time someone asserts that.
You resolve the contradiction by noting that you only have ten observations, so the test has limited power to detect non-normality. Neither the data nor the log of the data look distinctly non-normal, but this does not mean that they are normal.
In each case, the test is shrugging its shoulders: "I can't tell, folks; I can't tell."
Comment: So is there anyway out of this dilemma?
Comment: There is no dilemma.  We should back up: what you are doing is similar to procedures that are known to be invalid: that is what we should be concerned about.  What statistical problem do you hope your testing will solve?
Comment: @whuber I mean do I use a different test to do hypothesis testing? If so which one? That is basically my question. I only know Shapiro-Wilk testing for normality. According to wiki, it is best for overall performance.
Comment: @whuber Essentially, I want to test whether the data is following normal distribution which will tell me what estimators I should use.
Comment: That's usually not a good idea: instead, you should choose your estimators based on what you want to estimate and on your model of the data.

Shapiro-Wilk test on transformed data


Is there a way to force some block caching despite system block caching option is disabled?

Question: In /admin/config/development/performance settings page, my "cache blocks" options is disabled due to "Block caching is inactive because you have enabled modules defining content access restrictions."
But I absolutely need to globally cache some blocks (in particular some superfish blocks because they recreates menu items on every page load if not cached).
I've already tried Block Cache Alter module, but without success, probably because this module can works only if system "cache block" option is enabled (see: https://drupal.org/node/1954224 )
Thank you for help
Answer: With latest Drupal 7.33 release there is a new setting variable now:
/**
 * Block caching:
 *
 * Block caching may not be compatible with node access modules depending on
 * how the original block cache policy is defined by the module that provides
 * the block. By default, Drupal therefore disables block caching when one or
 * more modules implement hook_node_grants(). If you consider block caching to
 * be safe on your site and want to bypass this restriction, uncomment the line
 * below.
 */

$conf['block_cache_bypass_node_grants'] = TRUE;

Setting the above to TRUE the "cache blocks" option becomes checkable.
Answer: block_form_system_performance_settings_alter() is where that code lives. Looking into it you can force this by running variable_set('block_cache', TRUE); once or by adding $conf['block_cache'] = TRUE; to your settings.php file (near the bottom). I would opt for the settings.php method.
Core Issue for this as well: https://drupal.org/node/1930960 (Now in 7.33). See MXT's answer on how to use it.
Note that by doing this bad things might happen. You have been warned.


About $|\operatorname{Sym}(\Omega)|$ when $\Omega$ is an infinite set.

Question: Here is a problem:

Show that if $\Omega$ is an infinite set, then $|\operatorname{Sym}(\Omega)|=2^{|\Omega|}$.

I have worked on a problem related to a group that is $S=\bigcup_{n=1}^{\infty } S_n$. Does it make sense we speak about the relation between $S$ and $\operatorname{Sym}(\Omega)$ when $\Omega$ is an infinite set. Moreover, I know that  $|\operatorname{Sym}(\Omega)|=|\Omega|^{|\Omega|}$. How we can reach from $|\Omega|^{|\Omega|}$ to $2^{|\Omega|}$. Thanks.
Comment: We can have two infinite sets $A$ and $B$, with $A$ a proper subset of $B$, but with $A$ and $B$ of the same cardinality. for example (Galileo) let $B$ be the integers, and $A$ the even integers.
Comment: If $\Omega$ is infinite, the two have the same cardinality.
Comment: @AndréNicolas: Isn't Sym$(\Omega)\subset S$?
Answer: $\textbf{Lemma}$ : For cardinals, $2 \leq \kappa \leq \lambda$ where $\lambda$ is infinite, then $\kappa^\lambda = 2^\lambda$.
It is clear that $2^\lambda \leq \kappa^\lambda$. However $\kappa^\lambda \leq (2^\kappa)^\lambda = 2^{\kappa \cdot \lambda} = 2^{\lambda}$, where $\lambda \cdot \kappa = \text{max}\{\lambda, \kappa\}$, which can be proved.
Now taking $\kappa = \lambda = |\Omega|$ in the lemma, you have $2^{|\Omega|} = |\Omega|^{|\Omega|}$.
Answer: You have $|\Omega|^{|\Omega|} \geq 2^{|\Omega|}$, since $\Omega$ is infinite. Then, $|\Omega| < 2^{|\Omega|}$ (Cantor's theorem) so $|\Omega|^{|\Omega|} \leq (2^{|\Omega|})^{|\Omega|}=2^{|\Omega|.|\Omega|}=2^{|\Omega|}$, since $\Omega$ is infinite.


(don't ?) use JavaScriptSerializer to convert xml file (of unknown schema) to json in c#

Question: Is JavascriptSerializer the "tool" to convert an xml file (of unknown schema) into a json string ?
There are some threads here dealing about how to convert xml to json in c#.
And some recommended dedicated solutions (http://www.phdcc.com/xml2json.htm)
But in those threads there are always one suggesting using JavaScriptSerializer. But there is never clear explanation on how to do it. One always elude it or start with an object instead of an xml.
To make it clear :
I don't look after having my xml turned into objects.
If I can, I'd prefer to avoid it.
XML => Json would please me more than XML => objects => Json.
But everybody is telling don't reinvent wheel use JavaScriptSerializer. But I don't feel like this is the way to go. Setting up objects from xml looks like a terrible task (strongly typing).
So my question is :
Should I stay with the quick (but "dirty") methods described in http://www.phdcc.com/xml2json.htm
Or
Could I use JavascriptSerializer even if I don't know the schema of the xml ?
If so please fill in the gaps/modify the following code
namespace ExtensionMethods {
    public static class JSONHelper
    {
        public static string ToJSON(this XmlDocument doc)
        {
            object obj = get_An_Object_From_My_XML_Without_Too_Much_Hassle_Like_Having_To_Deal_With_Strongly_Type(doc); // how to do that ???
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Serialize(obj);
        }
    }
}

using ExtensionMethods;
...
XmlDocument mydoc = new XmlDocument(@"c:\test.xml");
Response.write(mydoc.ToJSON());

Comment: http://blogs.msdn.com/b/shahpiyush/archive/2007/04/12/2103116.aspx
Comment: @Lucero. Saw that one. It definitively looks like JavascriptSerializer is not the way to go for what I want. This is  so much extra work all I want is to get my data structure (xml file) turned into another (json).
Answer: I think you could use json.net in order to receive a unknown xml into a json object:
string xml = @"<?xml version=""1.0"" standalone=""no""?>
<root>
  <person id=""1"">
  <name>Alan</name>
  <url>http://www.google.com</url>
  </person>
  <person id=""2"">
  <name>Louis</name>
  <url>http://www.yahoo.com</url>
  </person>
</root>";

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

string jsonText = JsonConvert.SerializeXmlNode(doc);
//{
//  "?xml": {
//    "@version": "1.0",
//    "@standalone": "no"
//  },
//  "root": {
//    "person": [
//      {
//        "@id": "1",
//        "name": "Alan",
//        "url": "http://www.google.com"
//      },
//      {
//        "@id": "2",
//        "name": "Louis",
//        "url": "http://www.yahoo.com"
//      }
//    ]
//  }
//}


Convert AWS CLI command into Python AWS Lambda function

Question: I'm using an AWS CLI command to extract results for reporting and I must create a lambda that does the same job but is facing many problems with syntax errors
The current AWS CLI command:
$ aws ec2 describe-instances \
    --query 'Reservations[].Instances[].[Tags[?Key==`platform`]|[0].Value, Tags[?Key==`resource-version`]|[0].Value]| sort_by(@, &[0])' \
    --output table \
    --filter Name=tag:platform,Values=aip,mmt,pame --profile prod | uniq

Could you help me to make the same code with python please?
Comment: I think what you are looking for is the AWS Python SDK (https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/index.html).
If you scroll through the documentation, or crtl+f for `describe instances` you will eventually find this resource that might help you.
If you already tried running the same command with Python, and you did get errors, please post the python code and the errors, otherwise no one will be able to hlep you.
Comment: Can you please explain, in words, what is the output you are seeking? It seems to be that you want the `platform` and `resource-version` tags for instances where the `platform` has a value that is one of `aip`, `mmt` and `pame`. Is that correct? Don't you want the Instance ID as well?
Comment: No i obtained this result : Deployed versions are :
mmt 1.104.12
mmt 1.104.15
mmt 1.104.15
mmt 1.112.0-SNAPSHOT
mmt 1.104.15
mmt 1.104.12
aip 1.112.0-SNAPSHOT
mmt 1.112.0-SNAPSHOT
mmt 1.104.15
mmt 1.104.12
aip 1.112.0-SNAPSHOT
aip 1.112.0-SNAPSHOT
mmt 1.112.0-SNAPSHOT
Answer: Now i can print this result :
Deployed versions are :
mmt 1.104.12
mmt 1.104.15
mmt 1.104.15
mmt 1.112.0-SNAPSHOT
mmt 1.104.15
mmt 1.104.12
aip 1.112.0-SNAPSHOT
mmt 1.112.0-SNAPSHOT
mmt 1.104.15
mmt 1.104.12
aip 1.112.0-SNAPSHOT
aip 1.112.0-SNAPSHOT
mmt 1.112.0-SNAPSHOT
Using this cide `import boto3
def instance_list(tagkey, tagvalue):
    # When passed a tag key, tag value this will return a list of InstanceIds that were found.
ec2client = boto3.client('ec2')

response = ec2client.describe_instances(
     Filters=[
      {'Name':'tag:platform', 'Values':["mmt","pame","aip"]}
]
)
name = []
instancelist= []
print ("Deployed versions are :")
for reservation in (response["Reservations"]):
    for instance in reservation["Instances"]:
       for tag in instance['Tags']:
           if tag['Key'] == 'platform':
               name.append(tag["Value"])
           if tag['Key'] == 'resource-version':
               instancelist.append(tag["Value"])
a  = [name + instancelist]
for x, y in zip(name, instancelist):
    print (x, y)

`
Am trying to delete duplicated line and send result by mail or slack; Any help please ?


run anonymous function (IIFE) on unittest using jest

Question: I have file index with anonymous function or IIFE like this below
<code>(async()=> todo list logic)()
</code>
I want to call this anonymous function on my test case,
is there any way to run this anonymous func on my unit test case?
or is there any way to call this function using typescript code?
thanks in advance
Comment: In IIFE automatically runs just because it's there. What exactly do you want to call on it separately?
Comment: i want to create unittest of it, so i have to run the file when i run my unittest, the problem is on ther, how do i run IIFE file @tevemadar
Comment: The whole idea of an IIFE is that it is _Immediately Invoked_. This smells like an XY problem.
Comment: actually i can use like marco said below, but it seems i need to do a little refactor of the code which prohibited for me to touch the code, but i will discuss to my lead about that
Answer: No, you can not call it explicitly from your tests. I recommend creating a pure function and an additional IFFE wrapper. This way you can import your pure function into your tests.
<code>// foo.js
function foo() {}

export default foo
</code>
<code>// foo-wrapper.js

(() => {
  foo()
})()
</code>
<code>// foo.test.js

import foo from './foo.js'
</code>
Comment: No, in both cases `foo` is executed once. Once in your tests and once immediately on runtime.
Comment: i have tried this once before, but the problem is , it seems i need to do a little refactor code there which i prohibited for me to touch the code, but maybe i can discuss this to my lead, thank you marco
Comment: and the downside is, it will run twice for foo() , if i call foo() on my unittest, it will run the IFFE and foo()


Question concerning a minimal faithful left ideal in an artin algebra

Question: Let $B$ be an artin algebra an suppose there is a faithful projective-injective left $B$-module. Moreover, there is a minimal faithful left ideal $Be$ for some idempotent $e\in B$.
1) What does minimal mean? Minimal with respect to what?
2) Does $Be$ have to be indecomposable?
Thanks in advance.
Comment: What do you mean "moreover"? Assume moreover that there is such an ideal? Since you say this after several hypotheses, it sounds maybe like you want it to be a consequence of those hypotheses (but the wording disguises that.) It would be good to give the source of this problem, or better yet a link to somewhere we can read it in print.
Comment: It concerns Theorem 2.10 in http://www.sciencedirect.com/science/article/pii/S002186930098726X#
Answer:
Apparently, "minimal faithful" is meant to mean "minimal among faithful ideals." Let $R$ be some interesting commutative quasi-Frobenius ring (say, $\Bbb Z/(36)$ ). In such a ring, nontrivial ideals have nontrivial annihilators due to the double-annihilator property. This means that the entire ring is a minimal faithful ideal (actually, the minimum faithful ideal) of the ring.
Moreover, the same ring is a faithful projective-injective module over itself, but it is not an irreducible module.
Comment: Thank you very much for your answer!


Como recuperar os dados vindos do model?

Question: Estou usando roteamento com DRouter e sempre preciso pegar os dados de tal loja pelo slug, para isso preciso fazer 1 select em todas as rotas.
Mas não sei como pegar o id, nome e slug vindos lá do SistemaModel.php e por ali na index.php usando a variável $dados.
Diz que a variável $dados que não existe e que não é um objeto.
index.php
$app = new DRouter\App();
$app->render->setViewsFolder(__DIR__.'/App/Views/');
require 'App/Controllers/SistemaController.php';

/* Login */
$app->get('/:slug/admin', function($slug){
    SistemaController::dadosLoja($slug);
    $this->render->load('lojista/loj_login.php', [
        'id_loja' => $dados->id,
        'nome_loja' => $dados->nome,
        'slug_loja' => $dados->slug
    ]);
});

SistemaController.php
require dirname(__FILE__) . '/../Models/SistemaModel.php';
class SistemaController
{
    public static function dadosLoja($slug){
        $dados = SistemaModel::slug($slug);
        if($dados != false){
            return $dados;
        }
    }
}

SistemaModel.php
class SistemaModel
{
    public static function slug($slug){
        $dados = DB::conn()->prepare("SELECT * FROM `admin_lojas` WHERE `slug` = :slug");
        $dados->bindValue(':slug', $slug, PDO::PARAM_STR);
        $dados->execute();
        if(count($dados) == 1){
            return $dados;
        }
    }
}

DB.php
class DB
{
    private static $conn;
    public function __construct(){}

    public static function conn(){
        try {
            if(is_null(self::$conn)){
                self::$conn = new PDO('mysql:host='.HOST.';dbname='.BD.'',''.USER.'',''.PASS.'',
                array(
                    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                    PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
                ));
            }
            return (self::$conn) ? self::$conn : false;
        } catch(PDOException $e) {
            die("Error");
        }
    }
}

Comment: Está usando composer-autoloader? Realmente é necessário usar `require`? Quero dizer se usa composer-autoloader não seria mais facil configurar os namespaces no composer.json? Posta a classe `DB::conn`, não tem como saber se é um framework ou se foi vc quem criou.
Comment: @GuilhermeNascimento estou usando o DRouter para roteamento
Answer: Isso está errado:
if(count($dados) == 1){
    return $dados;
}

O $dados é um PDOStatement, posso estar enganado, mas ele só itera com um foreach, ele não conta os resultados, para isso você deve fazer isso:
$dados->execute();

$count = $dados->rowCount();

if($dados > 0){

Claro que se eu entendi o seu código, agora isso também me parece errado:
    return $dados;

Você está retornando o objeto e não os dados, para isso é necessário $dados->fetchAll(); (retorna multiplas linhas)
Deve ficar tudo assim:
class SistemaModel
{
    public static function slug($slug){
        $dados = DB::conn()->prepare("SELECT * FROM `admin_lojas` WHERE `slug` = :slug");
        $dados->bindValue(':slug', $slug, PDO::PARAM_STR);
        $dados->execute();

        if($dados->rowCount() > 0){
            return $dados->fetchAll();
        }
    }
}

Se slug retorna apenas um item, então acredito que o correto seria $dados->fetch(PDO::FETCH_ASSOC):
class SistemaModel
{
    public static function slug($slug){
        $dados = DB::conn()->prepare("SELECT * FROM `admin_lojas` WHERE `slug` = :slug");
        $dados->bindValue(':slug', $slug, PDO::PARAM_STR);
        $dados->execute();

        if($dados->rowCount() > 0){
            return $dados->fetch(PDO::FETCH_ASSOC);
        }
    }
}

Documentação
É altamente recomendável que leia a documentação e evite escrever códigos aleatoriamente sem saber o que cada função faz, links:

http://php.net/manual/en/class.pdostatement.php
http://php.net/manual/en/pdostatement.rowcount.php
http://php.net/manual/en/pdostatement.fetchall.php
http://php.net/manual/en/pdostatement.fetch.php
http://php.net/manual/en/pdostatement.execute.php


Can I get consistent CSS colors across browsers?

Question: I'm testing a new site, and I have a div with
<code>background-color: #bbf6bb;
</code>
That seems innocuous enough to me. And yet, on my MacBook Pro, the color looks very different in Firefox 3.6 vs. Safari 4. In Safari, it's the color I'd expect from the hex value: a pale green. In Firefox, there's a definite bluish tint, making the color turquoise.
I'm aware of color inconsistencies that result from different treatment of images across browsers, but in pure CSS? Really? I'm guessing that Firefox trying to correct for my display in hopes of delivering better consistency with print, but I'd much rather have my site look the same hue to my users regardless of their choice of browser. Any ideas? Can someone confirm that Firefox is the culprit here?
[Update: This seems to have been a fluke. Specifically, it's a narrow issue with Firefox—see my answer below. I'm puzzled, but relieved.]
Comment: Does your style tag have the "media=screen" attribute?
Comment: As I said in the comment to my answer, can you create a screen shot of this? This is really strange.
Comment: I wish I'd taken a screenshot at the time, because now I find myself unable to reproduce the issue. I'm sure it was there...
Answer:
but I'd much rather have my site look the same hue to my users regardless of their choice of browser.

That's just the thing - without colour correction, it won't look the same to different users, because not every monitor has the exact same colour response.
Comment: I don't think you read my question closely. This isn't about different monitors; I'm talking about Firefox and Safari, on the same display, with the same system settings.
Comment: Safari doesn't perform colour-correction on CSS and images without colour profiles, while Firefox assumes sRGB for those. The end result is that Firefox users get roughly the same colours across all monitors, while Safari users get completely different colours depending on what monitor they're on.
Answer: I've isolated the issue! It's definitely a Firefox 3.6 bug (running on Mac OS 10.6). Seems to have something to do with having a large number of tabs open. If I create a new tab and go to the page, I get the slightly off colors (blue-green instead of just green). If I create a new window and go to the same page, the colors are accurate. And yes, this still occurs if I refresh both instances.
[Edit: Screenshot got misplaced, but I stand by this answer.]
Comment: Actually i don't know how you took you screen shots but the color difference is also apparent in the browser themes, so i question this test. ;)
Answer: color managent color profiles not only changes the images (that can save those) but also the rgb colors.
Comment: OK, right, but differently in Firefox vs. Safari? I need a more specific answer.
Answer: This has nothing to do with the browser, more with the user's monitor's colour correction. Every monitor has slight differences in how they show colours; additionally, the background lighting around the monitor plays a role, as well (warm daylight vs. usually colder inside lights etc. etc).
Sadly, there's nothing really you can do. You could embed a colour profile into your graphics, which is how design professionals do it when sending data to print, but that will provide halfway decent results only for the minuscule number of (graphic designers') monitors that has undergone colour calibration and is properly set up - and I think IE won't parse colour profiles at all.
Comment: No, this has everything to do with the browser. Read the question.
Comment: @Trevor: Sorry, I didn't read your question closely enough. Now that's really odd. Can you create a screen shot showing two such inconsistencies? It would be interesting to take a look at the actual colour values.
Comment: If you don't give your images a colour profile the browser should understand them as sRGB (therefore, I always do "convert to sRGB" and strip the colour profiles when doing "save for web" in Photoshop).


PHP Scheduled Task Security

Question: To piggyback off of this article, How to schedule the execution of a PHP script on the server side?, what concerns should I have for the user account that is selected to run the script?
For example, if I use the administrator account and I need to change the password of the administrator account, what happens to my scripts?  Is there a way to have the scripts run on Windows and not have to worry about user account password changes?  Are there other security concerns by running PHP scripts in this manner that I need to be aware of?
Is there a way to make it run on the local system without needing to be an admin user and call php?
Comment: It depends what your scheduled task does. If it's just executing some standard business logic from your web app on a scheduled basis and it's just touching your app's database (and maybe writing some files somewhere) then you don't need to run as administrator or your login.
Answer: Typically scheduled tasks with work if a password is updated, as it links into the user account.
However, its not a good idea to create tasks under random users, rule of thumb is typically organize / create users specifically for a scheduled task.  I know internally for domain accounts, we (at my company) have specific accounts that we use for backups, others for scheduled tasks, etc.
That way you separate logic and security layer (backup user isn't full admin, nor is a task running user capable of say touching databases, etc;)
Comment: Would running it under the "NT AUTHORITY\LOCAL SERVICE" user account be a better choice?

PHP Scheduled Task Security


Django get the current email of the user by default in a different serializer based on the selected "userid"

Question: I was wondering what the correct way is to get the current email  of the user by default in a different serializer based on the selected "userid".
I have tried many examples from the ModelSerializer docs but without success.
serializers.py
from rest_framework import serializers
from ticker.models import Ticker
from users.models import NewUser
from rest_framework.permissions import IsAuthenticated
from alerts.models import SectionAlerts
from users.serializers import UserlistSerializer
from rest_framework.fields import CurrentUserDefault

class TickerSerializer(serializers.ModelSerializer):
    class Meta:
        model = Ticker
        fields = "__all__"

class UserlistSerializer(serializers.ModelSerializer):
    class Meta:
        model = NewUser
        fields = "__all__"

class AlertsSerializer(serializers.ModelSerializer):
    ticker = TickerSerializer(read_only=True)
    email = UserlistSerializer(read_only=True)
    ticker_id = serializers.SlugRelatedField(
    queryset=Ticker.objects.all(), source="ticker", slug_field='crypto', write_only=True
    )
    class Meta:
        model = SectionAlerts
        fields = "__all__"

models.py
from django.db import models
from ticker.models import Ticker
from django.conf import settings
from import_export.resources import ModelResource
from import_export.fields import Field
from users.models import NewUser
from django.core.mail import EmailMessage

class SectionAlerts(models.Model):
    id = models.AutoField(primary_key=True)  # auto increment field
    valuenow = models.FloatField(null=True, blank=True, default=None)
    valuealarm = models.FloatField(null=True, blank=True, default=None)
    user = models.CharField(max_length = 40,blank=True, null=True)
    userid = models.ForeignKey(
        NewUser, related_name='userid',
        blank=True,
        null=True,
        on_delete=models.CASCADE,
    )
    email = models.ForeignKey(NewUser, blank=True, null=True, on_delete=models.CASCADE)
    ticker = models.ForeignKey(Ticker, blank=True, null=True, on_delete=models.CASCADE)

The goal: anytime that the "Current Value" of a crypto is lower than the previously set "Value Alert", the backend is sending an email notification to the user.
The problem: at the moment all users receive that email notification, while just each individual logged in user that created
their own alerts should receive it.
Question: how can i add just the email of the user that created the individual alert?

views.py
from email import message
from rest_framework import generics
from rest_framework import status
from .serializers import AlertsSerializer, TickerSerializer, UserlistSerializer
from users.serializers import CustomUserSerializer
from .models import SectionAlerts, Ticker, NewUser
import requests
import logging
from itertools import chain
from importlib import reload
import sys
import csv
from django.http import HttpResponse
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.settings import api_settings
from rest_framework.response import Response
from .utils import Util
from django.urls import reverse
from django.shortcuts import redirect

class CreateSectionAlerts(generics.CreateAPIView):
    def perform_create(self, serializer):
        serializer.save(user=self.request.user)
        print("New alert is saved")
    serializer_class = AlertsSerializer

class ListSectionAlerts(generics.ListAPIView):
    queryset = SectionAlerts.objects.all()
    serializer_class = AlertsSerializer

    # ------- EMAIL STARTS HERE
    def get_queryset(self):
        print("****ALERTS START get_querysetdavid")
        queryset = SectionAlerts.objects.filter(userid=self.kwargs["pk"])

        for b in SectionAlerts.objects.filter(userid=self.kwargs["pk"]):
            # print("b---Print just mail", b.email)
            print("b---Print SectionAlerts", b.id, b.valuenow, b.userid, b.ticker.c_0)

            for c in NewUser.objects.all():
                if b.ticker.c_0 < b.valuealarm:
                    print("Alert is achieved and must be sent")
                    email_body = 'Hi ' + c.user_name + ', you received this message because the value of the crypto ' + str(b.ticker) + ' is now ' + str(b.ticker.c_0) +'€.' + ' and reached the alert value of ' + str(b.valuealarm) +'€.' + '\n'+ 'This email confirms this event. \n' + 'Click here https://www.cryptothlon.com to signin in your account and know more. \n' +'\n'+ 'Your Cryptothlon Team'
                    data={'email_body': email_body, 'to_email': c.email,
                        'email_subject': 'Crypto alert'}
                    Util.send_email(data)
                    print("Email sent")
                    try:
                        record = SectionAlerts.objects.get(id = b.id )
                        record.delete()
                        print("Record deleted successfully!")
                    except:
                        print("Record doesn't exists")
                    # email method ends here
        return queryset
    # ------- EMAIL ENDS HERE

class DeleteSectionAlerts(generics.RetrieveDestroyAPIView):
    queryset = SectionAlerts.objects.all()
    serializer_class = AlertsSerializer

Comment: The screenshot shows a post request for a `SectionAlert` object, `email` is null because you haven't defined a `NewUser` when creating your `SectionAlert`, hence the `email` field contains `null`.
Answer: You can use SerializerMethodField. It will allow you to create a custom field in the serializer. By default SerializerMethodField looks for a method get_<field name>, and performs the according logic:
from users.models import NewUser

class AlertsSerializer(serializers.ModelSerializer):
    ...
    email = serializers.SerializerMethodField()

    class Meta:
        model = SectionAlerts
        fields = "__all__"

    def get_email(self, obj):
        user_id = self.initial_data['userid']  # get the `userid` from the request body
        user = NewUser.objects.get(pk=user_id)  # fetch the user from DB
        return UserlistSerializer(instance=user).data

Comment: Excellent valuable insight. This works perfectly
Comment: Note that it may not be that performant because you're making extra queries. OK if it's not many, but if there are a lot you may find this really slows things down ...
Comment: @michjnich assuming that the OP uses the serializer only once after POST request on `api/sectionalerts/create` it should not cause many troubles and "slow things down".
Comment: No, but I wouldn't like to make that assumption :) It's not wrong, I just thought it worth pointing out there are potential side effects to doing it like that.
Comment: @Ersain do you have any suggestion on why do i receive the error ` 'AlertsSerializer' object has no attribute 'initial_data' ` when i try to call it in the frontend?
Comment: @David are you using the serializer for things other than object creation/update? If so, you won't be able to use `initial_data` property, describe what you are trying to do, and I will edit my post
Comment: @Ersain thanks, i have just added the "goal", the "problem", and the main "question" to the post. I am looking forward to hearing your comments.
Comment: @Ersain i created a new post to try to solve this problem. Do you have any further suggestions? https://stackoverflow.com/questions/72837956/django-add-just-the-email-of-the-user-that-created-an-individual-alert
Answer: You don't show the NewUser model so not sure exactly what field you need, but you can use dot notation for the source of a serializer field.
From DRF docs:

The name of the attribute that will be used to populate the field. May be a method that only takes a self argument, such as URLField(source='get_absolute_url'), or may use dotted notation to traverse attributes, such as EmailField(source='user.email').

See this section of the docs: https://www.django-rest-framework.org/api-guide/fields/#core-arguments
Comment: Note that the `email` field is a foreign key relationship to `NewUser` table, and you cannot use primitive data types to serialize the whole object, but in case if you are referencing some field from the object, then you will make extra query to fetch the needed user under the hood, which according to your words can make things slow down
Comment: I seem to have offended you by pointing that out. Not sure why? With this solution you would set the queryset for the view(set) and use `select_related`, which would then pull in the `NewUser` record as part of the same query. Your solution explicitly calls another query for each record. As I say, not a problem if you have a small queryset, but if you need it to scale, not idea.
Comment: @michjnich thanks for your contribution. In fact I didn't manage yet to achieve my ultimate goal completely. I edited the post to make it more clear. The problem: at the moment all users receive that email notification, while just each individual logged in user that created their own alerts should receive it.
Question: how can i add just the email of the user that created the individual alert?
Do you have any thoughts on how to solve it?
Comment: @michjnich i created a new post to try to solve this problem. Do you have any further suggestions? https://stackoverflow.com/questions/72837956/django-add-just-the-email-of-the-user-that-created-an-individual-alert
Comment: Only on mobile right now, but will take a look later on if nobody else has answered by then...


JAVASCRIPT: Ordenar lista HTML por nome e por idade

Question: Boa noite,
Preciso ordenar uma lista de frases em HTML por nome do usuário ou pela idade, conforme o botão que for clicado. Como posso fazer isso?
Segue abaixo, uma versão mais clara do código:

    <title>JS</title>

        <button onClick="orderName()">Ordenar por nome</button>
        <button onClick="orderIdade()">Ordenar por idade</button>

João é homem e possui 23 anos"

Ana é mulher e possui 19 anos"

Ricardo é homem e possui 32 anos"

Fernanda é mulher e possui 25 anos"

ATUALIZAÇÃO:
Caso necessário, essa é a versão completa.
As frases são montadas a partir do array users (como é visto no link acima). Consegui fazer os filtros funcionarem, então optei por reduzir o código, pra facilitar.
Como preciso que a ordenação funcione junto com os filtros, entendi que talvez fosse necessário pegar a lista a partir do HTML, pra manter os filtros ao ordenar (nas minhas tentativas, se eu pegasse a partir do array, teria que limpar a lista, o que eliminaria o filtro).
Comment: Da onde se monta essa lista?
Comment: Alterei a pergunta explicando melhor como a lista é obtida, @VirgilioNovic
Answer: Usando .map(), .sort() e .filter() você consegue ordenar. Veja que no caso da idade foi preciso usar expressão regular com .match() para pegar apenas o valor numérico da idade (não pode haver outro número no texto senão a idade!):

function orderName() {
   var list = document.querySelector("#lista-usuarios"); // seleciona a UL
   var list_lis = list.querySelectorAll("li"); // seleciona as LIs
   var nova_list = ''; // crio uma variável vazia que será usada para construir uma nova lista ordenada
   [].map.call(list_lis, function(a){ // mapeio as LIs retornando o texto
      return a.textContent;
   }).sort().filter(function(a){ // ordeno em ordem alfabética e retorno o texto ordenado
      nova_list += '
'+a+'
'; // concateno o texto construindo uma nova lista
   });

   list.innerHTML = nova_list; // substituo o conteúdo da UL pelos novos LIs ordenados

   console.log("Ordenado por nome");
}

function orderIdade() {
   var list = document.querySelector("#lista-usuarios");
   var list_lis = list.querySelectorAll("li");
   var nova_list = '';
   [].map.call(list_lis, function(a){
      return a.textContent.match(/\d+/); // aqui eu seleciono apenas a idade
   }).sort().filter(function(a){ // ordeno em ordem numérica (o .sort() faz isso automaticamente)
      nova_list += '
'+a.input+'
'; // concateno o texto
   });

   list.innerHTML = nova_list;

   console.log("Ordenado por idade");
}

  <button onClick="orderName()">Ordenar por nome</button>
  <button onClick="orderIdade()">Ordenar por idade</button>

João é homem e possui 23 anos"

Ana é mulher e possui 19 anos"

Ricardo é homem e possui 32 anos"

Fernanda é mulher e possui 25 anos"

Answer:

function getNumber(str) {
 return +str.textContent.match(/\d/gi).join('')
}

function orderIdade() {
  const list = document.querySelector("#lista-usuarios");
  const listChildren = [...list.children];
  const numbers = listChildren.map(item => getNumber(item));
  const listOrder = numbers
    .sort()
    .map(number => listChildren.filter(item => getNumber(item) === number));
   list.innerHTML = listOrder.map(item => `<li>${item[0].textContent}</li>`).join('');
}

function orderNome() {
  const list = document.querySelector("#lista-usuarios");
  const listChildren = [...list.children]
   .map(item => item.textContent)
    .sort();
  list.innerHTML = listChildren.map(item => `<li>${item}</li>`).join('');
}

João é homem e possui 23 anos

Ana é mulher e possui 19 anos

Ricardo é homem e possui 32 anos

Fernanda é mulher e possui 25 anos

<button class='btn' onClick="orderNome()">Ordenar pelo nome</button>
<button class='btn' onClick="orderIdade()">Ordenar pela idade</button>
Comment: Ficou bem legal usando ES6.
Comment: Então... eu não entendi bem o `.match(/\d/gi)` na função `getNumber()`. Qual o papel das barras, do `d` e do `gi`?
Comment: Então meu amigo, usamos uma expressão regular para pegar somente os números de uma String. O ***//*** é uma expressão regular no seu formato literal. o ***\d*** indica que eu quero apenas números. O ***gi*** nesse caso não faz muita diferença, ele faz mas sentido com textos. Você colocar ***\d+*** é mais indicado, o ***+*** ai indica pelo 1 ou mais vezes.
Vou deixar esses dois links que vão te ajudar a entender melhor essas dúvidas.
https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Guide/Regular_Expressions
https://www.devmedia.com.br/expressoes-regulares-em-javascript/37015


CSS background (gradient) transition not working

Question: The CSS transition on my a tag isn't fading in and out, its just like there's no transition at all! I tried using transition: background 300ms ease-in-out; too but still no luck
CSS:
.FeatureRow a{
    padding: 10px;
    display: block;
    width: 260px;
    height: auto;
    border-radius: 5px;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    -webkit-box-shadow: 0px 4px 8px #f5f5f5;
    -moz-box-shadow: 0px 4px 8px #f5f5f5;
    box-shadow: 0px 4px 8px #f5f5f5;
    background-image: -moz-linear-gradient(top,#fbfbfb,#ffffff);
    background-image: -webkit-gradient(linear,0 0,0 100%,from(#fbfbfb),to(#ffffff));
    background-image: -webkit-linear-gradient(top,#fbfbfb,#ffffff);
    background-image: -o-linear-gradient(top,#fbfbfb,#ffffff);
    background-image: linear-gradient(to bottom,#fbfbfb,#ffffff);
    background-repeat: repeat-x;
    transition: all 300ms ease-in-out;
    -webkit-transition: all 300ms ease-in-out;
    -moz-transition: all 300ms ease-in-out;
    -ms-transition: all 300ms ease-in-out;
    -o-transition: all 300ms ease-in-out;
}
.FeatureRow a:hover{
    background-image: -moz-linear-gradient(top,#C00,#C0F);
    background-image: -webkit-gradient(linear,0 0,0 100%,from(#C00),to(#C0F));
    background-image: -webkit-linear-gradient(top,#C00,#C0F);
    background-image: -o-linear-gradient(top,#C00,#C0F);
    background-image: linear-gradient(to bottom,#C00,#C0F);
}

Answer: As Adrift says, this is not supported; you have to simulate it.
This is the CSS
.FeatureRow {
    padding: 10px;
    display: block;
    position: relative;
    width: 260px;
    height: 70px;
    border-radius: 5px;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    background-image: linear-gradient(0deg,white,gray);
}

.FeatureRow:after {
    content: '';
    position: absolute;
    left: 0px;
    top: 0px;
    right: 0px;
    bottom: 0px;
    background-image: linear-gradient(0deg,red,magenta);
    opacity: 0;
    transition: all 3s ease-in-out;
}

.FeatureRow:hover:after {
    opacity: 1;
}

We are overlaying your div with a pseudo element. Each one has a gradient. The pseudo element has opacity set to 0; when you hover it you change the opacity to 1; and this property can be transitioned.
demo
Comment: @SethB it does work, if the problem you are having is the text being hidden behind a background, make sure you put **z-index: 1** on your btn, and **z-index: -1** on your :after
Comment: appreciate it man this worked for me!
Comment: This does not work for buttons unfortunately
Answer: As noted in other answers, it is not possible to animate a gradient. It is however possible to animate background-color. Knowing this, you can layer a transparent gradient on top of a background color, which will allow you to animate one of the colors in your gradient.
To do this, choose the color you want to animate, set it as the background-color, and put rgba(0,0,0,0) (transparent) in its place.
Then all you have to do is on :hover or with javascript change  background-color to your desired value

This will only work to transition one color of your gradient. But I think it's still a decent option to transition a gradient

.squareTile {
 background: radial-gradient(#203244eb, rgba(0,0,0,0), #203244eb);
 background-color: #3596ff;
 border-radius: 10px;
  width: 100px;
 height: 100px;
 border: 1px solid black;
 cursor: pointer;
 transition: 1s;
}

.squareTile:hover {
 background-color: #77c577;
}

Comment: This is actually quite a clever answer. It won't work in very situation of course, but way to think outside the box!
Answer: Using gradients for animations (or transitions) is unfortunately not supported by any browser at the moment. Take a look at this list in the Transitions module for a list of all current animatible properties
Comment: Is it possible to use a background image and then use a transition to "overwrite" the image with just background-color?
Comment: You unfortunately can't do that either because background-images aren't animatible. Take a look at the list in my answer.
Comment: Great.. this proves that all these new CSS3 tricks are becoming as complex as useless.
Answer: animating a background gradient is possible, as mentioned, and there's a quite good tool currently up to let you play with the feature:
https://www.gradient-animator.com/
example output:
.css-selector {
    background: linear-gradient(270deg, #8ccebd, #146e57);
    background-size: 400% 400%;

    animation: AnimationName 5s ease infinite;
}

@keyframes AnimationName {
    0%{background-position:0% 50%}
    50%{background-position:100% 50%}
    100%{background-position:0% 50%}
}


Youtube API : Upload videos to a specific channel

Question: I'm using PHP (CodeIgniter framework).
The project I'm working on will let users upload videos to server. It is required that the video is also uploaded to Youtube under a specific account (the site's). I'm aware that this is not the recommended practice but it's the ideal way to go since most of the users don't have gmail account.
I've been reading Youtube's documentation, but got confused real quick. Can anyone explain the step-by-step to achieve what I want to do? From what I'm thinking, I need a long-lived access token for my site and when user finishes uploading, I will call Youtube API with this long-lived access token to upload it to my site's Youtube channel. But I don't know whether Youtube API supports this or not.
I've found this library : CI Youtube Library but from what I read, this library also requires my web user to log in with their Youtube account, so I guess it's not what I'm looking for.
Thanks for the help!
Answer:
The suggested way is to have your users upload with their account and you moderate what goes into your account. Otherwise they may upload an offensive, illegal video and you may have to get accounted for that. It's not any different than sharing your password publicly.
There are also quota limits for upload and you will hit those limits really fast, if you try to upload from same account.
They won't need "gmail" account, by using authentication, they will just link any of their accounts to a Google account really fast.
There are no long lived tokens for security reasons. In case of an unwanted access, short lived tokens are more secure.

You can get all the rest of information at "https://developers.google.com/youtube/v3/getting-started"
Answer: Because your users are uploading to YOUR Youtube account, they shouldn't need to login to yours.  If you authenticate your site's youtube account, that is the only token they should need.


Organização de portfolio por categorias

Question: Analisando o MODELO, gostaria de alguma sugestão simples para classificar estes blocos por categorias dinâmicamente com os mesmos efeitos já existentes.
Possuo uma uma tabela que salva o id do "produto" e os respectivos ids das categorias relacionadas a este produto.

categorias_produtos
  catp_id int(11) Não
  catp_pro_id int(11) Sim     NULL
  catp_cat_id varchar(255)    Sim     NULL
DADOS da tabela:

Protótipo PHP (necessita solução):
Lembrando que os ids das categorias são processados em um array e separados por vírgula no banco de dados utilizando a função explode.

Todas categorias

                <?php $selCategoria = $conn->prepare("SELECT * FROM cat ORDER BY cat_nome ASC");
                $selCategoria->execute();

                if($selCategoria->rowCount() > 0):
                while($rowCategoria = $selCategoria->fetch(PDO::FETCH_OBJ)): ?>

<a href="#" data-filter=".<?php echo $rowCategoria->cat_id; ?>"><?php echo $rowCategoria->cat_nome; ?>

                <?php endwhile; endif; ?>

                <?php $selProdutos = $conn->prepare("SELECT p.pro_id, p.pro_nome, p.pro_status, p.pro_valor, p.pro_destaque, p.pro_brevedescricao, fp.posicao, fp.arquivo, cp.catp_cat_id
                                                    FROM produtos p
                                                    LEFT JOIN fotos_produtos fp ON p.pro_id = fp.id_produto AND fp.posicao = ?
                                                    RIGHT JOIN produtos ON p.pro_status = ?
                                                    LEFT JOIN categorias_produtos cp ON cp.catp_pro_id = p.pro_id
                                                    GROUP BY p.pro_id
                                                    ORDER BY rand() ");
                $selProdutos->execute(array(0,1));
                while($rowProduto = $selProdutos->fetch(PDO::FETCH_OBJ)): ?>
                    <div class="project-post <?php echo $rowProduto->catp_cat_id; ?>">

                            <img alt="" src="<?php echo URL; ?>administracao/imagens/produtos/tamanho1_<?php echo $rowProduto->arquivo; ?>">

<?php echo $rowProduto->pro_nome; ?>

<?php echo $rowProduto->pro_brevedescricao; ?>

                <?php endwhile; ?>

Estrutura HTML:
Ignore a origem das classes nesta estrutura. Pode-se observar que o nome da categoria é processado dentro da tag data-filter, no PHP montei algo semelhante com o ID das categorias separado por vírgula no data-filter.

Todas categorias

Categoria 1

Categoria 2

Categoria 3

Cras ornare tristique

Vivamus vestibulum nulla nec ante.

Cras ornare tristique

Vivamus vestibulum nulla nec ante.

Cras ornare tristique

Vivamus vestibulum nulla nec ante.

Cras ornare tristique

Vivamus vestibulum nulla nec ante.

Cras ornare tristique

Vivamus vestibulum nulla nec ante.

Cras ornare tristique

Vivamus vestibulum nulla nec ante.

Comment: com números e virgulas na classe não vai funcionar, crie uma palavra chave para cada categoria e separe-as com espaço e não virgula... a proposito essa sua estrutura de tabelas esta meio estranha, para concatenar os IDs de categorias assim, não precisaria ser em uma tabela separada, poderia ser um campo na mesma tabela produtos...
Comment: A princípio está assim porque sua utilização era diferenciada anteriormente. No caso algum tempo atrás desenvolvi algo parecido porém, sem êxito, onde utilizei uma função `in_array`, para verificar se há determinado número no `data-filter`. Pensei em algo assim, desenvolvi a lógica, porém sem sucesso na prática. A princípio acho que deve existir alguma maneira utilizando os ids, caso contrário irei redesenvolver esta parte.
Answer: Se você usar uma estrutura de tabelas como no modelo abaixo, acho que você consegue consultar facilmente todas as categorias do produto e obter a classe de cada uma...
Tabela categorias:
CREATE TABLE IF NOT EXISTS `categorias` (
  `id_categoria` int(11) NOT NULL AUTO_INCREMENT,
  `nome_categoria` varchar(255) NOT NULL,
  `classe_categoria` varchar(25) NOT NULL,
  PRIMARY KEY (`id_categoria`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;

Tabela produtos:
CREATE TABLE IF NOT EXISTS `produtos` (
  `id_produto` int(11) NOT NULL AUTO_INCREMENT,
  `nome_produto` varchar(255) NOT NULL,
  `descricao_produto` text NOT NULL,
  PRIMARY KEY (`id_produto`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;

Tabela produtos para categorias:
CREATE TABLE IF NOT EXISTS `produtos_para_categorias` (
  `id_produto` int(11) NOT NULL,
  `id_categoria` int(11) NOT NULL,
  PRIMARY KEY (`id_produto`,`id_categoria`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Um exemplo de consulta para o produto ID 1:
SELECT *
FROM `produtos_para_categorias`
LEFT JOIN `categorias`
USING ( id_categoria )
WHERE id_produto =1
LIMIT 0 , 30

Organização de portfolio por categorias


Wilcard binding in IIS 7.5

Question: Scenario
Domain is mydomain.com
Website host IP is 184.108.40.206
I have setup the DNS for *.mydomain.com to point to 188.8.131.52
Requirement
I need to route incoming requests for *.mydomain.com to a single site in IIS 7.5
e.g. site1.mydomain.com, site2.mydomain.com must all be directed (bound) to the same site in IIS 7.5
Answer: Suppose you're using Microsoft DNS:
Create an A-record in the zone mydomain.com with the value "mydomain.com" and the IP 220.127.116.11. Will show up in the MMC as (same as parent folder).
This will redirect any undefined subdomains to the default web site running on the IIS.
Comment: This is what I have done so far, now my problem is routing those requests that arrive at IIS running on 18.104.22.168 to the same site i.e. subdomain.mydomain.com AND subdomain2.mydomain.com AND subdomain3.mydomain.com

Wilcard binding in IIS 7.5


Dúvida em relação a modelagem

Question: Boa noite, tenho que fazer um banco de dados para uma simples loja de vendas online, mas estou num dilema de quais tabelas criar e gostaria de opinião de vocês.
No caso, eu criei as tabelas:

Cliente
Produtos
Compra

No caso,a compra iria unir as duas, com id do cliente e do produto, além de outros campos.
Estou certo ou poderia fazer de maneira diferente?
Answer: Voce precisa fazer um relacionamento Many to Many em produtos e clientes, esse relacionamento resulta em uma tabela produto_cliente, com uma chave estrangeira de produtos e clientes. A necessidade disso é que vários clientes podem ter comprado um mesmo produto, consequentemente o contrário, um único produto está relacionado a vários usuários (isso é útil caso você precise saber o histórico de compras de um cliente, ou de um produto)
A tabela compras eu mudaria para pedidos, guardando o id do cliente e o id do endereço de entrega (caso seu sistema suporte vários endereços de um mesmo cliente), a chave primária seria o ID de compra, nessa tabela você guarda outras coisas essenciais como valor total, tipo de entrega, data de compra, essas informações.
Por fim outra tabela de nome compras_produto, onde eu guardo por meio de chave estrangeira de pedidos, e produto suas respectivas chaves primárias, nessa tabela incluo quantidade, preço unitário e data, talvez outras informações caso necessário.
Por desconhecer totalmente o domínio da sua aplicação, alguns relacionamentos One to Many e One to One são necessários, mas trocando em miúdos, essa é uma das alternativas que eu adotaria, um banco de dados bem modelado contribui fortemente com o desempenho de sua aplicação, já que um dos processos mais custosos é justamente a realização de consultas.
Comment: Tipo, quando o usuário entra na página ele vê os produtos e seleciona os que ele deseja, indo para o carrinho, com isso ele seleciona a quantidade,insere estado/cidade etc. Na hora de finalizar o pedido ele seleciona se quer logar ou criar um cadastro. Depois de logado ele finaliza o pedido e vai para outra página que mostra os pedidos dele.
Comment: Eu tinha feito como vc disse dos relacionamentos, mas eu tava apanhando muito com as tabelas e nao estava sabendo como inserir e como fazer os joins, entao queria optar por essa escolha de um banco simples.
Comment: Seu banco de dados pode ate ser simples, porem você não vai conseguir fugir dos relacionamentos ou de um join monstruoso para juntar todos os dados que você precisa, por isso a importância dos relacionamentos M2M, O2O e O2M. É preciso acima de tudo criar um balanço, não complexo de mais e nem simples de mais. Como diz o ditado, o barato sai caro.
Comment: Pedidos tambem nao teria relacionamento n-n com cliente?
Comment: Pedidos e clientes e One to Many, um cliente pode ter vários pedidos, mas um pedido está relacionado apenas a um cliente. Caso tenha dúvidas sobre relacionamentos abra outra pergunta.

Dúvida em relação a modelagem


Output one value into an array from an array of Objects containing value pairs

Question: Ive been trying combinations but can't figure this out. I am trying to pull out an array containing just the values from a value pair within an array containing objects.
INPUT
const myUsers = [
    { id: '1', name: 'bob' },
    { id: '56', name: 'bert' },
    { id: '32', name: 'Jenny' }
]

REQUIRED OUTPUT
[1,56,32]

CURRENT CODE
const idRecordsOnly = myUsers.map(item => {
    const container = {};
    container[item.id] = container[item.id]
    return container;
})

console.log(idRecordsOnly)

Comment: `const idRecordsOnly = myUsers.map(item =>item.id)` , just return id
Answer: Thanks Aurabliss! the answer is below
const idRecordsOnly = myUsers.map(item =>item.id)

Comment: You can also utilize [destructuring](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) there too. For instance: `const idRecordsOnly = myUsers.map(({ id }) => id);`


É possível alterar uma String de um método em tempo de execução?

Question: Se eu tiver tiver uma String fixa dentro de um método é possível ela ser alterada usando reflexão ou algo do gênero?
Exemplo:
public static void main(String[] args) {
    String nome = getName();
    boolean valid = isValidName(nome);
    if (valid) {
        System.out.print("Seu nome é valido!");
    } else {
        System.out.println("Seu nome é inválido!");
    }
}

Eu precisaria alterar a mensagem "Seu nome é inválido!" para "Nome invalido." isso em tempo de execução, ou seja, após compilar e executar o programa eu precisaria editar essa mensagem.
Estou trabalhando com uma API que esta em inglês, o cliente pediu para min traduzi-la 100%. A API possui mensagens configuráveis, onde você pode simplesmente ir no arquivo lang.txt e trocar as mensagens, porém existem algumas mensagens que não estão disponíveis nesse arquivo lang.txt então eu tive que edita-las usando reflexão, porém me deparei com algumas mensagens que estão "fixas" (não sei se esse é o termo correto) e eu precisaria editar essas mensagens... essas mensagens seguem o exemplo acima.
Comment: Alterar como? Não tá muito claro o que pretende fazer
Comment: Quando? Clique em [edit] e explique o que voce está fazendo, o que está tentando desenvolver e não está conseguindo. Sua duvida está superficial demais. Explicando nesses detalhes que te falei, fica mais facil entender.
Comment: Quero trocar a mensagem "Seu nome é inválido!" para "Nome não valido" por exemplo.
Comment: @Articuno fiz uma edição do post agora explicando melhor o caso, espero que entenda... eu sinceramente acho que isso que estou tentando fazer é impossível... já trabalhei algumas vezes com reflection mas acho que nem tudo nessa vida é possível ><
Comment: as chamadas de mensagem são sempre com `System.out`? Ou foi um exemplo demonstrativo
Comment: Foi um exemplo demonstrativo... Acabei de estudar e fazer MUITOS testes com refleciton e methods, e acho que isso que estou tentando fazer é impossível :(
Comment: Acho que a unica "maneira" de se fazer isso é tentar interpretar o método que envia a mensagem para o usuário, como a API é uma API de um jogo, acho que não deve ser muito difícil interpretar os pacotes que são enviados no chat do jogador.
Answer: Você não precisa necessariamente usar reflection para isso, você pode criar sua própria API.
Já precisei fazer isso, com vários jars antigos e sem suporte cujo código fonte ninguém possuía, geralmente internos, mas também já aconteceu de precisar alterar um jar de terceiros, que nem o site existia mais.
Usa o programa jd-gui (ou qualquer descompilador que lhe agrade) para ver todo o código fonte do seu jar.
Cria um novo projeto na sua IDE e copia os códigos do descompilador para lá, faz as suas modificações e exporta para um novo arquivo.
Acredito que dessa forma é até mais fácil, pode até alterar de forma a fazer todas as strings serem carregadas de um properties.
Comment: Infelizmente o JAR que eu tenho pesa 18MB e ele não é opensource, trabalhar com engenharia reversa neste casso seria praticamente impossível... ia ser messes de trabalho gastos para editar 4 ou 5 frases.
Comment: Não recomendo usar jd-gui... ele é de longe um dos piores decompiladores... recomendo luytem ou ainda se preferir use um online http://www.javadecompilers.com/ (recomendo)


Verilog - Assigning value to a reg twice in a single always block

Question: I am working on a counter that counts the number of bits high in an input bit-stream of variable width. The code is as follows:
module counter(i_clk, i_arst, i_data, o_done, o_cnt);

    // ---------------------------------------- SIGNALS ---------------------------------------- //
    // Parameters
    parameter                   OUT_WIDTH;      // Width of the output in bits
    parameter                   IN_WIDTH;       // Number of bits in an input bit stream

    // Input
    input   wire                i_clk;          // Clock
    input   wire                i_arst;         // Active high asynchronous reset
    input   wire                i_data;         // Input data

    // Outputs
    output  reg                 o_done;         // Output done bit
    output  reg[OUT_WIDTH-1:0]  o_cnt;          // WIDTH-bit output counter

    // Internal signals
    integer                     index;          // Bit index for the input
    reg[OUT_WIDTH-1:0]          r_cnt_tmp;      // Temporary counter for assignment

    // ---------------------------------------- LOGIC ---------------------------------------- //
    // Combinational logic
    always @(*) begin
        o_cnt = r_cnt_tmp;
    end

    // Sequential logic
    always @(posedge i_clk or posedge i_arst) begin

        // Reset the counter
        if(i_arst) begin
            r_cnt_tmp   <= {OUT_WIDTH{1'b0}};
            o_done      <= 1'b0;
            index       <= 0;
        end
        else begin
            // When a new bit stream arrives
            if(index == 0) begin
                r_cnt_tmp   <= {OUT_WIDTH{1'b0}};       // Reset the output data
                o_done      <= 1'b0;                    // Data is now invalid because it is a new bit stream
                                                        // It only happens after a reset or a valid data output
            end

            // If bit is set
            if(i_data == 1'b1) begin
                r_cnt_tmp <= r_cnt_tmp + 1; // Count up
            end

            index <= index + 1;             // Increment the index

            if(index == IN_WIDTH) begin     // The input has been completely looped over
                o_done  <= 1'b1;            // Data is now valid for the output
                index   <= 0;               // Reset the index
            end
        end
    end
endmodule

Once I am done with the current bit-stream, I set the o_done signal to inform that the output data is valid and I reset the variable index to start with a new bit-stream. Then at the next clock rising edge, I reset the o_done signal and the counter value andstart counting again.
The problem I have is that my counter does not always reset. Since the signal takes its value only at the end of the block, if the first bit of the bit-stream is high, then it is not reset.
I would like to reset and start counting again in the same clock cycle because I do not want more latency and I have continuous bit-streams arriving on the input.
Is there a solution to avoid this issue ?
Thanks for your help.
Comment: I am struggling to understand what you **exactly** want where. you talk about `both assignments` . All assignments are in the same block.  This one: `o_cnt = r_cnt_tmp;` is redundant. If I understand what you want you 1/ need a extra copy of your bit counter to output. 2/ You need a to take into account that a bit may arrive whilst you want to restart the count. That needs a more complex if statement to cope with the extra condition of a bit arriving or not.
Answer: I think that your issue is in these statements:
           // When a new bit stream arrives
            if(index == 0) begin
                r_cnt_tmp   <= {OUT_WIDTH{1'b0}};       // Reset the output data
                o_done      <= 1'b0;                    // Data is now invalid because it is a new bit stream
                                                        // It only happens after a reset or a valid data output
            end

            // If bit is set
            if(i_data == 1'b1) begin
                r_cnt_tmp <= r_cnt_tmp + 1; // Count up
            end

Supposedly that you have index == 0 and i_data == 1. Now, the r_cnt_tmp value will be scheduled to become 0 after the block is executed, because you use the non-blocking assignment there.
The next block will try to increment the value, but it will use an old value of the counter and will happily override the previously scheduled reset value. Most likely it will not be '1' or '0'.
One way to re-write it is the following. In the example the initial value of the counter will become '1' if i_data is '1' while index is 0.
           // When a new bit stream arrives
            if(index == 0) begin
                r_cnt_tmp   <= i_data;       // Reset the output data to '0' or '1' depending on i_data
                o_done      <= 1'b0;                    // Data is now invalid because it is a new bit stream
                                                        // It only happens after a reset or a valid data output
            end
            else if(i_data == 1'b1) begin   // you definitely need this 'else'
                r_cnt_tmp <= r_cnt_tmp + 1; // Count up
            end

Sorry, i have not tested it, in particular when i_data is 'x'.
You might need different requirements for the initial condition. So, you can write it in your own way.


Conflict between javascript codes

Question: I'm creating a web application in which the main page is a timeline, and this timeline should update its content automatically. I am using the setTimeOut function of JQuery to update the timeline every x seconds.
But I also have another JavaScript code to show hidden elements of that item when clicking on the div.
The two code work perfectly when alone, except when I try to use both on the page. The error is as follows After the timeline is updated by setTimeOut, the second code (clicking on the div to show / hide elements) no longer works. I tried to solve it in several ways and I could not and also I did not find solutions. Does anyone know what can it be? I also accept tips on how to upgrade my timeline, such as updating only when there are new item entries, instead of every x seconds.
setTimeout("my_function();", 9000);
    function my_function() {
      $('#timeline').load(location.href + ' #timeline')
    }

$(document).ready(function () {
      var itemsDivs = document.querySelectorAll('.timeline-item');
      itemsDivs.forEach(function (itemsDiv) {

        itemsDiv.addEventListener('click', function () {
          var itemId = this.getAttribute('item-id')
          var display = document.getElementById('comment-form-' + itemId).style.display
          if (display == 'none')
            document.getElementById('comment-form-' + itemId).style.display = 'block'
          else
            document.getElementById('comment-form-' + itemId).style.display = 'none'
        })
      })
    })

          {% for item in items %}

              {{item.data.strftime('%c')}}

{{item.analista}} recomenda {{item.ativo}} a R${{item.preco}}.

Fonte:{{item.fonte}}

              {{item.coments}}
Data Alvo: {{item.dataalvo}}

          {% endfor %}

Comment: you are binding click eventlistener on every element pretty much manually that is why when the items reload the listeners are gone with them. Quickest solution would be to move your code (adding listener) to a function and call it again every time you reload items. Of course there are better and more sophisticated answers but mine is just the quickest fix to given problem without rewriting a lot.
Answer: You must re-register your event listeners.
Event listeners in Javascript do not attach to a selector, but to the actual elements themselves. document.querySelectorAll('.timeline-item') selects all DOM elements that currently have the .timeline-item selector. After 9 seconds (per your setTimeout function), you delete all your .timeline-items and get new up-to-date ones (load() deletes all inner HTML and replaces it with the new HTML)
If you wish for these newly added .timeline-items to have event listeners, then you need to add them. Something like this might work for you:
setTimeout(function() {
  // Register event listeners again after the load is complete
  $('#timeline').load(location.href + ' #timeline', function() {
    registerEventListeners();
  });
}, 9000);

// Register event listeners once the document is finished loading
$(document).ready(function () {
  registerEventListeners();
});

/**
 * Registers event listeners to all `.timeline-item` elements.
 */
function registerEventListeners() {
  var itemsDivs = document.querySelectorAll('.timeline-item');
  itemsDivs.forEach(function (itemsDiv) {

    itemsDiv.addEventListener('click', function () {
      var itemId = this.getAttribute('item-id')
      var display = document.getElementById('comment-form-' + itemId).style.display
      if (display == 'none')
        document.getElementById('comment-form-' + itemId).style.display = 'block'
      else
        document.getElementById('comment-form-' + itemId).style.display = 'none'
    })
  })
}

Comment: this way it worked, but it seems that the settimeout only runs once
Comment: @PedroLyra `setTimeout` is designed to only run once. If you want your code to repeat then use `setInterval` instead.
Answer: Perhaps, the reason is that: your div event is attached in document.ready , after x seconds over,  my_function is executed , the original item divs is removed from the page , and the new item divs  has not been attached event .
so, bind the div event in  $('#timeline').loaded  event.

Conflict between javascript codes


Problema com acentuação ao executar script python direto do C#

Question: Estou usando isso pra rodar o código:
processo.StartInfo = new System.Diagnostics.ProcessStartInfo
{
    FileName = @"\Python27\python.exe",
    Arguments = arquivoResposta,
    UseShellExecute = false,
    CreateNoWindow = true,
    WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
    RedirectStandardOutput = true,
    RedirectStandardError = true,
    RedirectStandardInput = true
};

Aqui um exemplo da saída correta:
Média mensal de atendimentos: 13.17
----
Mês 5: 17
Mês 7: 14
Mês 8: 14
Mês 11: 23
Mês 12: 30

O que está saindo:
Média mensal de atendimentos: 13.17

----

Mês 5: 17

Mês 7: 14

Mês 8: 14

Mês 11: 23

Mês 12: 30

Alguém tem uma solução? Tentei usar também o output.normalize(), mas ainda nada.
Comment: Edite o script em python (arquivoResposta) colocando logo na primeira linha `# -*- coding: utf-8 -*-` e veja se alterou o resultado. E o que acontece se você executar o mesmo código diretamente pelo cmd?
Answer: O que acontece é que quando você executa um script Python direto pelo command, ele pode detectar a codificação do terminal (que é "cp-852") e codificar a saída de acordo.
Quando você roda a partir de outro processo, o Python não tem como adivinhar a codificação do terminal, e usa utf-8 por padrão. Isso se for Python3, ou se você usou strings Unicode de forma correta, ou próximo do correto em Python2 - senão pode ser o caso de você estar usando utf-8 hardcoded mesmo.
Quando você tenta exibir texto acentuado, codificado em utf-8 numa saída latin-1, a acentuação é exibida errada da forma que você postou (cada caractére acentuado se desdobra em dois, sendo que o primeiro é um "Ã" )
Primeiro você tem que entender o que está acotnecendo quando trata de codificação de acentos. Não adianda pergunta,r copiar e colar respostas, ou tentar ficar alterando parametros para  "encode" e "decode" ao acaso.
Esse artigo tem uma boa introdução.
Se o seu programa em Python2 está usando byte-strings, então ele está hard-coded para utf-8 mesmo  -  e se você executa-lo direto do command vai ver a acentuação errada, mas de outro jeito.
ENtão, segunda coisa - tente usar Python 3.6.0 ou 3.5 em vez de Python 2.7-  não há sentido usar uma versão tao antiga do Python em um projeto novo, e a maior mudança do Python2 para Python3 é justamente o tratamento automático da codificação de acentos em alguns dos casos.
Se o problema persistir, é por que, como eu disse no primeiro parágrafo, não tem como realmente o Python adivinhar a codificação da sua saída a partir dos dados que o C# envia para ele no redirecionamento. Nesse caso, você deverá forçar a codificação para latin-1. É bem pouco provável que esse seja o caso, por que é bem difícil forçar o "print" do Python a usar outra codificação para o stdout que não seja a que está lá. O que eu acredito que seja o seu caso, como em Python2, é você ter usado byte-strings (isso é, sem o prefixo u"..." em Python2) e etar com seu arquivo código fonte hardcoded em utf-8. Ao passar a usar Python3, a coisa se resolve.
Senão, outra forma de resolver é salvar seu arquivo.py com a codificação "latin1" diretamente. Nesse caso,a sa alteraçẽos devm ser feitas apenas no seu editor de textos de programação, e a primeira ou segunda linha que indica a codificação do arquivo deve ser mudade de "# coding:utf-8" para  "# coding: latin1" . Seu programa funcionará sem alterações, mas você vai perder muitas das ferramentas para processamento de texto de forma correta - por isso a dica para mudar para Python 3.


Multiple relations like join tables (neo4j)

Question: How do I express the following in neo4j?
match or create user bob; bob works at studio; while at studio, he's allowed to doodle; while at studio, he's also allowed to type.
Here's what I have:
MERGE (u:user {name:'bob'})
MERGE (c:company {name: 'studio'})
MERGE (u)-[:works_at]->(c)-[:allowed_to]->(p:permission {name:'doodle'})
MERGE (u)-[:works_at]->(c)-[:allowed_to]->(p:permission {name:'type'})

This doesn't work as permission becomes a relation of company.
Also, is it possible to chain relations such that:
MERGE work=(u)-[:works_at]->(c)
CREATE (work)-[:allowed_to]->(p:permission {name:'doodle'})
CREATE (work)-[:allowed_to]->(p:permission {name:'type'})

where you assign a relation to a variable to continue it later on in another query?
Comment: Can you close the 2 other identical questions that you created?
Comment: Stackoverflow was bugging out. Wasn't me. I didn't realize it created others. I closed them. Thanks for making me aware of it.
Answer: How about modelling it so the company grants the permission? Something like this...
MERGE (u:user {name:'bob'})
MERGE (c:company {name: 'studio'})
MERGE (u)-[:works_at]->(c)
MERGE (u)-[:allowed_to]->(p1:permission {name:'doodle'})<-[:GRANTS]-(c)
MERGE (u)-[:allowed_to]->(p2:permission {name:'type'})<-[:GRANTS]-(c)
RETURN *

You can't really refer to objects via identifiers/variables you have created previously in other queries. You would have to re-match or merge those previously created objects in your new query.
Part 2 could be modelled something like this..
MERGE (u:user {name:'bob'})
MERGE (c:company {name: 'studio'})
MERGE (u)-[:DOES]->(work:Work {start_date: timestamp()} )-[:AT]->(c)
CREATE (work)-[:allowed_to]->(p:permission {name:'doodle'})
CREATE (work)-[:allowed_to]->(p:permission {name:'type'})

Answer: As an alternate, if you never need to lookup all users with a certain permission at a company, you could maintain a collection of permissions as relationship properties.
MERGE (u:user {name:'bob'})
MERGE (c:company {name: 'studio'})
MERGE (u)-[r:works_at]->(c)
SET r.permissions = ['doodle', 'type']


Extract UTF-uncoded binary data from JSON using jq

Question: Say I have a JSON with a 0xb7 byte encoded as a UTF codepoint:
{"key":"_\u00b7_"}

If I extract the value of the "key" with jq it keeps the utf8 encoding of this byte which is "c2 b7":
$ echo '{"key":"_\u00b7_"}' | ./jq '.key' -r | xxd
0000000: 5fc2 b75f 0a                             _.._.

Is there any jq command that extracts the decoded "5f b7 5f" byte sequence out of this JSON?
I can solve this with extra tools like iconv but it's a bit ugly:
$ echo '{"key":"_\u00b7_"}' | ./jq '.key' -r \
      | iconv -f utf8 -t utf32le \
      | xxd -ps | sed -e 's/000000//g' | xxd -ps -r \
      | xxd
0000000: 5fb7 5f0a                                _._.

Answer: def hx:
  def hex: [if . < 10 then 48 + . else  55 + . end] | implode ;
  tonumber | "\(./16 | floor | hex)\(. % 16 | hex)";

{"key":"_\u00b7_"} | .key | explode | map(hx)

produces:
["5F","B7","5F"]

"Raw Bytes" (caveat emptor)
Since jq only supports UTF-8 strings, you would have to use some external tool to obtain the "raw bytes". Maybe this is closer to what you want:
jq -nrj '{"key":"_\u00b7_"} | .key' | iconv -f utf-8 -t ISO8859-1

This produces the three bytes.
And here's an iconv-free solution:
jq -nrj '{"key":"_\u00b7_"} | .key' | php -r 'print utf8_decode(readline());'

Comment: Thank you! The "explode" operator performs the actual "utf8 => codepoint number" decoding, after that there's a number of ways to proceed with this data.

However, from the jq sources it seems that currently there's no way to obtain the actual "5f b7 5f" byte sequence (I mean just 3 bytes, not a hex-encoded string) as an output as jq always ensures that it's dealing with a correct utf8 byte sequence. Therefore the binary output should always have some kind of encoding (utf8, hex, base64, uri etc).
Answer: Alternate
Addressing the character encoding scenario outside of jq:
Though you didn't want extra tools, iconv and hexdump are indeed readily available - I for one frequently lean on iconv when I require certain parts of a pipeline to be completely known to me, and hexdump when I want control of the formatting of the representation of those parts.
So an alternative is:
jq -njr '{"key":"_\u00b7_"} | .key' | iconv -f utf8 -t UTF-32LE | hexdump -ve '1/1 "%.X"'

Result:
5FB75F


Modify PHP_Beautifier in Vim to not strip empty lines

Question: Just finished incorporating PHP_Beautifier into Vim and the fact that it removes whitespace irks me. Apparently it's a bug since 2007. There is a hack to fix this problem, but it leads to other problems. Instead I decided to use a round about method.
First Convert multiple blank lines to a single blank line via the command  as suggested here
:g/^\_$\n\_^$/d

Next Convert all blank lines to something unique like so (make sure it does not get changed during beautification)
:%s/^[\ \t]*\n/$x = 'It puts the lotion on the skin';\r/ge

Next Call PHP_Beautifier like so
:% ! php_beautifier --filters "ArrayNested() IndentStyles(style=k&r) NewLines(before=if:switch:foreach:else:T_CLASS,after=T_COMMENT:function)"<CR>

Finally Change all unique lines back to empty lines like so
:%s/$x='It puts the lotion on the skin';//ge

All four work when I tested them independently. I also have the third step mapped to my F8 key like so
map <F8> :% ! php_beautifier --filters "ArrayNested() IndentStyles(style=k&r) NewLines(before=if:switch:foreach:else:T_CLASS,after=T_COMMENT:function)"<CR>

But when I try to string the commands together via the pipe symbol, like so (I padded the pipes with whitespace to better show the different commands)
map <F8> :g/^\_$\n\_^$/d    |    %s/^[\ \t]*\n/$x = 'It puts the lotion on the skin';\r/ge      |      % ! php_beautifier --filters "ArrayNested() IndentStyles(style=k&r) NewLines(before=if:switch:foreach:else:T_CLASS,after=T_COMMENT:function)"     |     %s/$x = 'It puts the lotion on the skin';//ge<CR>

I get the following error

Error detected while processing /home/xxx/.vimrc:
line  105:
E749: empty buffer

E482: Can't create file /tmp/vZ6LPjd/0
Press ENTER or type command to continue

How do I bind these multiple commands to a key, in this case F8.

Thanks to ib's answer, I finally got this to work. If anyone is having this same problem, just copy this script into your .vimrc file
func! ParsePHP()
    :exe 'g/^\_$\n\_^$/d'
    :%s/^[\ \t]*\n/$x = 'It puts the lotion on the skin';\r/ge
    :exe '%!php_beautifier --filters "ArrayNested() IndentStyles(style=k&r)"'
    :%s/$x = 'It puts the lotion on the skin';//ge
endfunc

map <F8> :call ParsePHP()<CR>

Answer: For some Ex commands, including :global and :!, a bar symbol (|) is
interpreted as a part of a command's argument (see :help :bar for the full
list).  To chain two commands, the first of which allows a bar symbol in its
arguments, use the :execute command.
:exe 'g/^\_$\n\_^$/d' |
\   %s/^[\ \t]*\n/$x = 'It puts the lotion on the skin';\r/ge |
\   exe '%!php_beautifier --filters "ArrayNested() IndentStyles(style=k&r) NewLines(before=if:switch:foreach:else:T_CLASS,after=T_COMMENT:function)"' |
\   %s/$x = 'It puts the lotion on the skin';//ge

Comment: There's a syntax error in your example (no matching `"` for `"ArrayNested()`), and am having a hard time trying to make it work
Comment: Problem has to do with mapping to the  key. When I copy paste the above code into vim (and add `"`) it works.
Comment: Also, `'g/^\_$\n\_^$/d'` complains if no patterns are found\
Comment: @puk:  I just copied your commands for the matter of illustration of using `:exe` to chain `|`-sensitive commands.  Debugging your original commands is a separate issue.
Comment: @puk:  The `:global` command does not cause any errors if there is no matching lines.  What kind of "complaint" do you get exactly?  Does it occur when you manually run that `:global` command?
Comment: I actually fixed (if you read the modified section of my question). The problem was in the `map `. It didn't work, so I moved your answer into a function, and instead did `map  :call ParsePHP()`. Thanks for your help.


Proof of when is $A=X^TX$ invertible?

Question: Say we have an $n\times m$ matrix $X$. What are the specific properties that $X$ must have so that $A=X^TX$ invertible?
I know that when the rows and columns are independent, then matrix $A$ (which is square) would be invertible and would have a non-zero determinant. However, what confuses me is, what sort of conditions must we have on each row of $X$ such that $A$ would be invertible.
It would be very nice to have a solution of the form:

when $n > m$ then $X$ must have...
when $n < m$ then $X$ must have...
when $n = m$ then $X$ must have...

I think in the 3rd case we just need $X$ to be invertible but I was unsure of the other two cases.
Comment: Closely related questions: [If $A^TA$ is invertible, then $A$ has linearly independent column vectors](http://math.stackexchange.com/q/1181271/72968) and conversely [Why is $A^TA$ invertible if $A$ has independent columns?](http://math.stackexchange.com/q/1840801/72968). For a statistical application, see [What is an example of perfect multicollinearity?](http://stats.stackexchange.com/q/221902/22228).
Comment: I am specifically interested when $n \neq m$
Comment: Hint: What happens if $X$ has full row rank?
Comment: This question is crucially missing any hypothesis about the field over which matrices are considered. The case of complex matrices is very different from the case of real matrices, and fields of nonzero characteristic are quite different again.
Comment: If $n=m$, it is enough to check that $\det(X) \not=0$
Comment: full row rank means when all the rows of X have a pivot?
Comment: Consider $X$ as a linear operator and look what happens to the rank of the basis $(v_1,...,v_n)$ when $X$ maps it to $R^m$ and then $X^T$ maps it back to $R^n$.
Comment: @Nitish when it has a full row rank does it mean it has all independent rows, right?
Comment: $X^T X$ is invertible if and only if the columns of $X$ are linearly independent.  A necessary condition for that is that $X$ must have at least as many rows as columns. $\qquad$
Answer: Precisely when the rank of $X$ is $m$ (which forces $n\geq m$).
The key observation is that for $v\in\mathbb R^m$, $Xv=0$ if and only if $X^TXv=0$. For the non-trivial implication, if $X^TXv=0$, then $v^TX^TXv=0$, that is $(Xv)^TXv=0$, which implies that $Xv=0$.
If the rank of $X$ is $m$, this means that $X$ is one-to-one when acting on $\mathbb R^m$. So by the observation, $X^TX$ is one-to-one, which makes it invertible (as it is square).
Conversely, if the rank of $X$ is less than $m$, there exists $v\in\mathbb R^m$ with $Xv=0$. Then $X^TXv=0$, and $X^TX$ cannot be invertible.
Comment: Which is a different way of saying that $X$ has a left inverse. Actually, if $X^TX$ is invertible, then $(X^TX)^{-1}X^T$ is a left inverse of $X$ and is exactly the Moore-Penrose pseudoinverse of $X$.
Comment: Yes, properly one needs to use the adjoint and not the transpose. But usually a question that is only tagged "linear algebra" and doesn't clarify it, is considering the real case.
Comment: Wouldn't the proof need to change if the matrices are complex? Then $(Xv)^T(Xv)=0$ does not imply $Xv =0$.
Answer: It is true if and only if:
$m\le n$ and Rank$\,(X)=m$.
Assume that $m\le n$ and Rank$\,(X)=m$, and let $X^TXu=0$, for some $u\in\mathbb R^m$. We need to show that $u=0$.
We have also that
$$
0=(X^TXu,u)=(Xu,Xu),
$$
and thus $Xu=0$. But as Rank$\,(X)=m$, this implies that $u=0$. (Otherwise, the columns of $X$ would be linearly dependent, and hence its rank less than $m$.)
Assume that $X^TX\in\mathbb R^{m\times m}$ is invertible. Then $m=$Rank$\,(X^TX)\le$Rank$\,(X)\le \min\{m,n\}$. Thus $\min\{m,n\}=m$, Rank$\,(X)=m$, and $m\le n$.
Comment: why do we need to show u = 0? I am a little confused on your notation for $0 = (XTXu, u) = (Xu , Xu)$
Comment: It is really $0=(X^T X u, u) = (Xu, Xu)$. Just a typo
Comment: Correct. I fixed it.
Comment: Wouldn't the proof need to change if the matrices are complex? Then $(Xv)^T(Xv)=0$ does not imply $Xv =0$.

Proof of when is $A=X^TX$ invertible?


What is the minimum action required in order to have access to a newly authorized server folder

Question: I hope I'm at the right place (right stack exchange website). If not please tell me the right one.
I got new rights on a folder on a Windows server by being added to Active Directory Group for which I didn't had access.
But although I had rights, I was not able to access the folder. I had to logout/login in order for me to have access.
Why so? If it is the server that authorize me according to who I am, then why rebooting my machine (or logoff/login) is required to have access to that folder?
What is the minimum action to do to have access to that folder? Do we absolutely have to logoff/login ?
Answer: A old text, but please see How Security Groups are Used in Access Control as it explain the process. The token is refreshed when your logoff/login happen
When a user or group is given permission to access a resource, such as a printer or a file share, the SID of the user or group is added to the access control entry (ACE) defining the granted permission in the resource's discretionary access control list (DACL). In Active Directory Domain Services, each object has an nTSecurityDescriptor attribute that stores a DACL defining the access to that particular object or attributes on that object. For more information about setting access control on objects in Active Directory Domain Services, see Controlling Access to Objects in Active Directory Domain Services.
When a user logs on to a Windows 2000 domain, the operating system generates an access token. This access token is used to determine which resources the user may access. The user access token includes the following data:
User SID.
SIDs of all global and universal security groups that the user is a member of.
SIDs of all nested global and universal security groups.
Every process executed on behalf of this user has a copy of this access token.
When the user attempts to access resources on a computer, the service through which the user accesses the resource will impersonate the user by creating a new access token based on the access token created at user logon time. This new access token will also contain the following SIDs:
SIDs for all domain local groups in the target domain that the user is a member of.
SIDs for all machine local groups on the target computer that the user is a member of.
The service uses this new access token to evaluate access to the resource. If a SID in the access token appears in any ACEs in the DACL, the service gives the user the permissions specified in those ACEs.
Answer:
But although I had rights, I was not able to access the folder. I had
  to logout/login in order for me to have access.

That is the expected behavior.

What is the minimum action to do to have access to that folder? Do we
  absolutely have to logoff/login?

AFAIK, yes.
Comment: Although your answer is exactly what I expected, I chosen yagmoth555 answer because it give me more information on the reason why and help me better understand the mechanism. But thank you very much. Thumbs up!
Comment: Glad to help...
Answer: Just as additional information...
From Perphenazine at ars technica

Depends on what you mean by
  'permissions'. Do you mean NTFS permissions? If so, they do take
  effect immediately. Do you mean permissions against an AD object? That
  requires a replication interval (a few minutes). Do you mean you
  changed group membership? That always requires a logoff/logon as group
  membership is appended to the kerberos ticket received at
  authentication.


filtering by several values at once

Question: please tell me how to use the filter to catch several values at once, provided that the value that we are looking for is dynamic

var tags = ['cat', 'animal'] //this will be a dynamic value the user will enter

var list = [
{
  tags: 'cat, kitty, big cat'
},
{
  tags: 'pet, animal, big cat'
},
{
  tags: 'tiger, kitty, big cat'
},
{
  tags: 'cat, kitty, animal, big cat'
}];

list.filter(item => {
  return item.tags.includes(/* need to filter only those where there are both values from the array tags */)
})
Comment: @epascarello no, I want to use it in the project, here I just made an example for easier so as not to throw a lot of code from the project here
Comment: so the code should be `return tags.every(..your includes logic..)`
Comment: @David I don't know how to do this with multiple dynamic values, I only know how to filter with static values even if there are several, but I don't know with dynamic
Comment: @epascarello And if the array "tags" is dynamic how to do it through every?
Comment: um, every loops over it.... so if it is dynamic it will use what is there.
Comment: @epascarello So every will return only true or false, and I need a filtered array
Answer: If you want to and the input tags; i.e. only show the list items that match all the tags, then use
list.filter(item => tags.every(tag => item.tags.includes(tag));

On the other hand, if you want to or the input tags; i.e. show the list items that match any of the tags, then use
list.filter(item => tags.some(tag => item.tags.includes(tag));

Comment: Don't work, what is the "tag" maybe "tags" do you mean? and i got error "Unexpected token, expected "," " on second "=>"
Comment: Removed the extra `)`.

filtering by several values at once