Q: Is there an ExecutorService implementation with queue limit and an option to replace old queue members with new ones? I need an ExecutorService implementation that can limit how many Runnables can be queued. I would also like to be able to control what happens when new runnables are submitted while the queue is already full. Ideally I'd like to be able to select a strategy but just removing the runnable at the front of the queue and placing the new one at the end of the queue would suffice. I'm sure there must be something like that already implemented and I looked around but couldn't find any.

A: Use DiscardOldestPolicy :

A handler for rejected tasks that discards the oldest unhandled
  request and then retries execute, unless the executor is shut down, in
  which case the task is discarded.

and BlockingQueue with fixed capacity:
int fixedThreadNumber = 10;
int idleSeconds = 10;

BlockingQueue<Runnable> blockingQueue = new LinkedBlockingQueue<>(10);

ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
        fixedThreadNumber,
        fixedThreadNumber,
        idleSeconds, TimeUnit.SECONDS,
        blockingQueue, new ThreadPoolExecutor.DiscardOldestPolicy());

ExecutorService executor = threadPoolExecutor;


Q: JPA/Hibernate using shared id and cascade=all tries to save child before parent, causing foreign key violation I am using JPA with hibernate. I have a 1-to-1 parent child relationship (the child is optional), with the id shared between the two and a foreign key relationship on the child table. My entities look like this:
parent:
public class LogItemEntity {
...

    @OneToOne(cascade={CascadeType.ALL}, mappedBy = "logItem", orphanRemoval=true, optional=true)
    @PrimaryKeyJoinColumn(referencedColumnName="ral_id")
    private LogAdditionalRequirement additionalRequirement;
...
}

child:
public class LogAdditionalRequirement {
...
    @Id
    @GeneratedValue(generator = "foreign")
    @GenericGenerator(name = "foreign", strategy = "foreign", parameters = { @Parameter(name = "property", value = "logItem") })
    @Column(name = "ral_id")
    private Long id;

    @OneToOne(optional=false)
    @PrimaryKeyJoinColumn(referencedColumnName="id")
    private LogItemEntity logItem;
...
}

When inserting a new object, the id for the parent is generated from a sequence and the cascade operation copies it onto the child. But the sql insert for the child is placed in the action queue of the session before the sql insert for the parent, and so it fails with a constraint violation on the foreign key:
ERROR o.h.util.JDBCExceptionReporter - ERROR: insert or update on table "rar_log_additional_requirement" violates foreign key constraint "fk_rar_ral_id"
Detail: Key (ral_id)=(70150) is not present in table "ral_log".

So how can I make the insert of the parent happen first?
This must be a pretty common usage, so I assume I'm doing something wrong, but I don't see what it is. I originally had the mappedBy attribute on the child side. I think that's wrong, but swapping it round made no difference.

A: One solution could be to remove the cascade "cascade={CascadeType.ALL}"
More on this subject here


Q: Braintree for flutter web I have a broader question. Is there any possibility to use a brain tree for the flutter web? I was searching for an API on pub.dev but there was only one for Android & Ios, but not for Web.
If there is one do you have a link to an example project, I just don't seem to find anything about the detailed integration

A: You can use Braintree using API calls with their GraphQL client. Read the whole guide on how to make API calls on their website.
Making API Calls


Q: How to make an image fill the entire div on hover? I want an image to show up on the div when I hover it. The code I am using makes the image larger than the div, how can I contain it to the fill the size of the div and not leak?
Here is the CSS code:
  img{
    display: none;
    max-height: 50vh;
  }
  .effect:hover .text{
    display: none;
  }
  .effect:hover img{
    display: block;
  }

Here is the HTML code:

Studio Griot

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam pretium dui ultrices, ornare mauris in, ultricies sapien. Proin dictum urna quis mauris pharetra, sit amet aliquam diam suscipit. In congue.

Web Development

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam pretium dui ultrices, ornare mauris in, ultricies sapien. Proin dictum urna quis mauris pharetra, sit amet aliquam diam suscipit. In congue.

Data Visualisation

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam pretium dui ultrices, ornare mauris in, ultricies sapien. Proin dictum urna quis mauris pharetra, sit amet aliquam diam suscipit. In congue.

Incrediminds

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam pretium dui ultrices, ornare mauris in, ultricies sapien. Proin dictum urna quis mauris pharetra, sit amet aliquam diam suscipit. In congue.

Thanks For The Help

A: EDIT with what OP actually wanted.
Here is the image that takes up the full width and height without stretching. You can use object-fit: cover
Information on object-fit: https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit

img {
  display: none;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.effect:hover .text {
  display: none;
}

.effect:hover img {
  display: block;
}

h1.text {
  font-size: 14px;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" />

Studio Griot

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam pretium dui ultrices, ornare mauris in, ultricies sapien. Proin dictum urna quis mauris pharetra, sit amet aliquam diam suscipit. In congue.

Web Development

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam pretium dui ultrices, ornare mauris in, ultricies sapien. Proin dictum urna quis mauris pharetra, sit amet aliquam diam suscipit. In congue.

Data Visualisation

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam pretium dui ultrices, ornare mauris in, ultricies sapien. Proin dictum urna quis mauris pharetra, sit amet aliquam diam suscipit. In congue.

Incrediminds

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam pretium dui ultrices, ornare mauris in, ultricies sapien. Proin dictum urna quis mauris pharetra, sit amet aliquam diam suscipit. In congue.

Add width: 100% to the img. That will keep it 100% width of its parent. I changed your bootstrap markup to col-sm so you could see it when you run the snippet.
Added information for future visitors:
An img will always display as large as its own default/native size, unless you specify the width: 100% and make sure it is display: block, since img are inline by default.

img {
  display: none;
  width: 100%;
}

.effect:hover .text {
  display: none;
}

.effect:hover img {
  display: block;
}

h1.text {
  font-size: 14px;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" />

Studio Griot

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam pretium dui ultrices, ornare mauris in, ultricies sapien. Proin dictum urna quis mauris pharetra, sit amet aliquam diam suscipit. In congue.

Web Development

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam pretium dui ultrices, ornare mauris in, ultricies sapien. Proin dictum urna quis mauris pharetra, sit amet aliquam diam suscipit. In congue.

Data Visualisation

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam pretium dui ultrices, ornare mauris in, ultricies sapien. Proin dictum urna quis mauris pharetra, sit amet aliquam diam suscipit. In congue.

Incrediminds

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam pretium dui ultrices, ornare mauris in, ultricies sapien. Proin dictum urna quis mauris pharetra, sit amet aliquam diam suscipit. In congue.


Q: Efficient way to store form data? I have a web app that has a big and complex form (fields, checks, etc).
I hade using standard OS form controls because they have visual (styling) limits.
I have been basically creating spans with IDs and attaching class or custom data attributes. I later need to send this to a PHP script for insertion into a database. This has worked well in the past, but I'm having to do a lot of manual processing of fields, which I don't think is efficient... Is there a better way?
I currently do,
IF ($('#foo').hasClass('on')){bar=1}

...
Then I manually compose a POST string via
foo=bar&bla=blabla ...

Then lots more on the PHP side to create an insert SQL statement.
Seems like its inefficient if you have dozens of fields... But I hate standard FORM elements...
Any suggestions? .... Loops? Arrays?

A: You should use a form. But how to make it pretty?
With JavaScript you can have a kind of front-end for the form. Hide the form elements and have some JS that changes the form field values on interaction.
Search for pretty forms jquery in Google.

A: From what I understand is that you don't want to manually get the value of each element that has the class 'on'. You could use jQuery's each function looping through all the elements you need. Something like:
var query = "";

$(".on").each(function(i){
    if(i === 0) {
      query+="?"+$(this).attr('id')+"="+$(this).text();
    } else {
        query+="&"+$(this).attr('id')+"="+$(this).text();
    }
});

(see: http://jsfiddle.net/MkbrV/)
Is this something you are looking for?

A: You could use jQuery's serialize() method:
$('form .on').serialize();

jsFiddle example


Q: Prefetch all relationship instances without call .all() I prefetch_related my objects with the following code:
    objs = wm.ModelA.objects.prefetch_related(
    'ModelB__ModelC')

and i want to iterate all ModelB from all objs and i do it with the following way
            for o in objs:
                for t in o.ModelB.all():

I noticed that in every iteration of e.ModelB.all() it calls the database. Is there a way to avoid this and bring all MobelB from the begining ?


Q: OPC Foundation Tree structure I've been searching the web, but I can't figure out how to get a tree view of the items on an OPC server. I used the following code:
 using Opc.Da;
 using Server=Opc.Da.Server;
 using Factory=OpcCom.Factory;

 string urlstring = string.Format("opcda://{0}/{1}/{{{2}}}", _hostName, _serverName, serverid);
 Server s = new Server(new Factory(), new URL(urlstring));
 ItemIdentifier itemId = null;
 BrowsePosition position;
 BrowseFilters filters = new BrowseFilters() {BrowseFilter = browseFilter.item};
 BrowseElement[] elements = s.Browse(itemId, filters, out position);

A: You do not state what precisely does not work. However, the main problems is probably in the fact that you are using BrowseFilter = browseFilter.item. The nodes in the tree are either leaves (sometimes called items), or branches. Your code only asks for the leafs, under the root of the tree. There may be no items under the root whatsoever, and you need to obtain the branches as well, and then dwelve deeper into the branches, recursively.
Start by changing your code to use BrowseFilter = browseFilter.all. This should give you all nodes under the root. Then, call the Browse recursively for the branches (just branches, not items) you receive, using the item ID of each branch as the starting point for the new browse.


Q: How to use ClamAV Antivirus in AWS EC2 for scanning S3 Bucket object when any file uploaded to s3 bucket? I need to run ClamAV antivirus in EC2 instance that can help me to scan a virus for s3 bucket object when any object being uploaded to s3. Is their any blog or way that i can use?

A: You can use S3 VirusScan, which is a third-party open source tool.
Some of its features are:

*

*Uses ClamAV to scan newly added files on S3 buckets

*Updates ClamAV database every 3 hours automatically

*Scales EC2 instance workers to distribute workload

*Publishes a message to SNS in case of a finding

*Can optionally delete compromised files automatically

*Logs to CloudWatch Logs

A: If you want to implement the solution by yourself, you can use: https://aws.amazon.com/blogs/developer/virus-scan-s3-buckets-with-a-serverless-clamav-based-cdk-construct/


Q: Working with jsoncpp to automate outputing json data
I am trying to automate the process of outputing the broad disciplines of UC Berkeley's majors. I have shortened the code and the JSON data to make the issue clearer. It is stored in JSON format and I am using the jsoncpp api to work with the data. The json data is the following:
{ "Berkeley": {
"1" :
{
"Broad Discipline": "Agriculture & Natural Resources",
"College/School**": "Letters & Science",
"Major": "ENVIRONMENTAL ECONOMICS & POLICY",
"Admit GPA Range": "3.63 - 3.94",
"Admit Rate": "0.342105263",
"Enroll GPA Range": "3.65 - 3.94",
"Yield Rate": "0.730769231",
"Admits": "26",
"Apps": "76",
"Enrolls": "19"
},
"2" :
{
"Broad Discipline": "Agriculture & Natural Resources",
"College/School**": "Natural Resources",
"Major": "CONSERVATION & RESOURCE STUDIES",
"Admit GPA Range": "3.32 - 3.88",
"Admit Rate": "0.333333333",
"Enroll GPA Range": "3.30 - 3.90",
"Yield Rate": "0.818181818",
"Admits": "22",
"Apps": "66",
"Enrolls": "18"
}
}}

Prior to this section of code, root is a json::Value and holds all the json data shown above.
// for loop through school's broad disciplines
for (int i = 0; i < 2; i++)
{
  string element = "\"" + to_string(i + 1) + "\"";
  string broadDiscipline = (root["Berkeley"][element]["Broad Discipline"]).asString();
  cout << "Broad Discipline: " << broadDiscipline << endl;
}

I would expect it to output:
Broad Discipline: "Agriculture & Natural Resources"
Broad Discipline: "Agriculture & Natural Resources"

The actual result is:
Broad Discipline:
Broad Discipline:

I do not know what I am doing wrong. Also I can not find the documentation in jsoncpp for whatever function is called in a command like: root["Berkeley"].


Q: Queue messages not being moved to the poison queue I have a job that imports files into a system. Everytime a file is imported, we create a blob in azure and we send a message with instructions to a queue so that the data is persisted in SQL accordingly. We do this using azure-webjobs and azure-webjobssdk.
We experienced an issue in which after the messages failed more than 7 times, they didn't move to the poision queue as expected. The code is the following:
Program.cs
public class Program
{
    static void Main()
    {
        //Set up DI
        var module = new CustomModule();
        var kernel = new StandardKernel(module);

        //Configure JobHost
        var storageConnectionString = AppSettingsHelper.Get("StorageConnectionString");
        var config = new JobHostConfiguration(storageConnectionString) { JobActivator = new JobActivator(kernel), NameResolver = new QueueNameResolver() };
        config.Queues.MaxDequeueCount = 7;
        config.UseTimers();

        //Pass configuration to JobJost
        var host = new JobHost(config);
        host.RunAndBlock();
    }
}

Functions.cs
public class Functions
{
    private readonly IMessageProcessor _fileImportQueueProcessor;

    public Functions(IMessageProcessor fileImportQueueProcessor)
    {
        _fileImportQueueProcessor = fileImportQueueProcessor;
    }

    public async void FileImportQueue([QueueTrigger("%fileImportQueueKey%")] string item)
    {
        await _fileImportQueueProcessor.ProcessAsync(item);
    }

}

_fileImportQueueProcessor.ProcessAsync(item) threw an exception and the message's dequeue count was properly increased and re-processed. However, it was never moved to the poison-queue. I attached a screenshot of the queues with the dequeue counts at over 50.
After multiple failures the webjob was stuck in a Pending Restart state and I was unable to either stop or start and I ended up deleting it completely. After running the webjob locally, I saw messages being processed (I assumed that the one with a dequeue count of over 7 should've been moved to the poison queue).
Any ideas on why this is happening and what can be done to have the desired behavior.
Thanks,
Update
Vivien's solution below worked.
Matthew has kind enough to do a PR that will address this. You can check out the PR here.

A: Fred,
The FileImportQueue method being an async void is the source of your problem.
Update it to return a Task:
public class Functions
{
    private readonly IMessageProcessor _fileImportQueueProcessor;

    public Functions(IMessageProcessor fileImportQueueProcessor)
    {
        _fileImportQueueProcessor = fileImportQueueProcessor;
    }

    public async Task FileImportQueue([QueueTrigger("%fileImportQueueKey%")] string item)
    {
        await _fileImportQueueProcessor.ProcessAsync(item);
    }
}

The reason for the dequeue count to be over 50 is because when _fileImportQueueProcessor.ProcessAsync(item) threw an exception it will crash the whole process. Meaning the WebJobs SDK can't execute the next task that will move the message to the poison queue.
When the message is available again in the queue the SDK will process it again and so on.


Q: How to read long lines from child pocress' stdout in an efficient way? Note: This question has changed significantly since the first version, so some comments or answers could seem weird. Please, check the edit history if something seems weird.
I am launching a child process from a C# class library.
I am using Process.BeginOutputReadLine() to read the output/error in an asynchronous way. I thought it didn't work with very long lines, but the problem seems to be that it's not scalable. In my computer, a 128 kb line is processed instantly, a 512 kb line seems to take around one minute, 1 mb seems to take several minutes, and I've been around two hours waiting for a 10 mb line to be processed, and it was still working when I cancelled it.
It seems easy to fix reading directly from the StandardOutput and StandardError streams, but the data from those streams seems to be buffered. If I get wnough data from stdout to fill the buffer, and then some more data from stderr, I can't find a way to check if there's data pending in one of them, and if I try to read from stderr, it will hang forever.
Why is this happening, what am I doing wrong, and what's the right way to do this?
Some code samples to illustrate what I'm trying to achieve.
Program1:

    // Writes a lot of data to stdout and stderr
    static void Main(string[] args)
    {
        int numChars = 512 * 1024;
        StringBuilder sb = new StringBuilder(numChars);

        String s = "1234567890";

        for (int i = 0; i < numChars; i++)
            sb.Append(s[i % 10]);

        int len = sb.Length;
        Console.WriteLine(sb.ToString());
        Console.Error.WriteLine(sb.ToString());
    }

Program2:

    // Calls Program1 and tries to read its output.
    static void Main(string[] args)
    {
       StringBuilder sb = new StringBuilder();
       StringBuilder sbErr = new StringBuilder();

       proc.StartInfo.CreateNoWindow = true;
       proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
       proc.StartInfo.UseShellExecute = false;

       proc.StartInfo.RedirectStandardError = true;
       proc.StartInfo.RedirectStandardOutput = true;
       proc.StartInfo.RedirectStandardInput = false;

       proc.StartInfo.Arguments = "";
       proc.StartInfo.FileName = "program1.exe";

       proc.ErrorDataReceived += (s, ee) => { if (ee.Data != null) sbErr.AppendLine(ee.Data); };
       proc.OutputDataReceived += (s, ee) => { if (ee.Data != null) sb.AppendLine(ee.Data); };

       proc.Start();
       proc.BeginOutputReadLine();
       proc.BeginErrorReadLine();
       proc.WaitForExit();
    }

Program1 has a constant that allows to set the size of data to generate, and Program2 launches Program1 and tries to read the data. I should expect the time to grow linearly with size, but it seems much worse than that.

A: I hope i understand your problem. the application hangs on Process.WaitForExit() because this is what Process.WaitForExit(): it waits for the process to exit.
you might want to call it in a new thread:
int your method that create the process:
    Thread trd = new Thread(new ParameterizedThreadStart(Start));
     trd.Start();

and add this method:
  private void Start(object o)
  {
     ((Process)o).WaitForExit();
     // your code after process ended
  }


Q: MySQL doesn't always use a key with the same query I've got a table:
CREATE TABLE `myTable` (
`a` DECIMAL(8,3) NULL DEFAULT NULL,
`b` INT(11) NULL DEFAULT NULL,
`name` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8_general_ci',
`id` INT(11) NOT NULL AUTO_INCREMENT,
`plus` VARCHAR(100) NOT NULL DEFAULT 't' COLLATE 'utf8_unicode_ci',
PRIMARY KEY (`id`),
INDEX `myKey` (`a`, `b`, `name`)
)
COLLATE='utf8_unicode_ci'
ENGINE=InnoDB
AUTO_INCREMENT=2416682;

I run a query:
SELECT * FROM myTable
order by a, b, name
limit 10000, 8 ;

It is very slow, takes about two seconds.
If I run an explain, it gives back this:
        'id' => 1,
        'select_type' => 'SIMPLE',
        'table' => 'myTable ',
        'partitions' => NULL,
        'type' => 'ALL',
        'possible_keys' => NULL,
        'key' => NULL,
        'key_len' => NULL,
        'ref' => NULL,
        'rows' => 1759263,
        'filtered' => 100.00,
        'Extra' => 'Using filesort',

Now if I set the limit to a lower value, it becomes very fast and explain says it uses myKey:
SELECT * FROM myTable
order by a, b, name
limit 1000, 8 ;

explain:
        'id' => 1,
        'select_type' => 'SIMPLE',
        'table' => 'myTable',
        'partitions' => NULL,
        'type' => 'index',
        'possible_keys' => NULL,
        'key' => 'myKey',
        'key_len' => '779',
        'ref' => NULL,
        'rows' => 1008,
        'filtered' => 100.00,
        'Extra' => NULL,

What can I do to make the select faster when the limit has high numbers?
I'm suprised the key is not used all the time, independent of the limit value. Is this normal?
MySQL version is 5.7.20-log.
Thanks

A: You can force the use of the index by doing the following:
FORCE INDEX (myKey)

FORCE INDEX mySQL ...where do I put it?


Q: C# model for nested properties I need to make API request to CloudFlare to purge cache of individual files.
Can someone please guide how to represent the below as C# model class.
files: [
        "http://www.example.com/css/styles.css",
        {
          "url": "http://www.example.com/cat_picture.jpg",
          "headers": {
            "Origin": "cloudflare.com",
            "CF-IPCountry": "US",
            "CF-Device-Type": "desktop"
          }
        }
      ]

A:     var obj = new
    {
        files = new object[]
        {
            "http://www.example.com/css/styles.css",
            new
            {
                url = "http://www.example.com/cat_picture.jpg",
                headers = new Dictionary<string,string>
                {
                    { "Origin", "cloudflare.com" },
                    { "CF-IPCountry","US" },
                    { "CF-Device-Type", "desktop"}
                }
            }
        }
    };

The dictionary is there because of the awkward property names like CF-IPCountry which make it not possible to use an anonymous type.
To show it working:
var json = Newtonsoft.Json.JsonConvert.SerializeObject(obj, Formatting.Indented);

gives:
{
  "files": [
    "http://www.example.com/css/styles.css",
    {
      "url": "http://www.example.com/cat_picture.jpg",
      "headers": {
        "Origin": "cloudflare.com",
        "CF-IPCountry": "US",
        "CF-Device-Type": "desktop"
      }
    }
  ]
}

Edit; that's not quite right - the dictionary didn't work, but I don't have time to fix it.

A: Using classes (maybe you could use better class names then mine :) ):
class Root
{
    [JsonProperty(PropertyName = "files")]
    public List<object> Files { get; set; } = new List<object>();
}

class UrlContainer
{
    [JsonProperty(PropertyName = "url")]
    public string Url { get; set; }

    [JsonProperty(PropertyName = "headers")]
    public Headers Headers { get; set; }
}

class Headers
{
    public string Origin { get; set; }

    [JsonProperty(PropertyName = "CF-IPCountry")]
    public string CfIpCountry { get; set; }

    [JsonProperty(PropertyName = "CF-Device-Type")]
    public string CfDeviceType { get; set; }
}

Usage
var root = new Root();

// Add a simple url string
root.Files.Add("http://www.example.com/css/styles.css");

var urlContainer = new UrlContainer() {
    Url = "http://www.example.com/cat_picture.jpg",
    Headers = new Headers()
    {
        Origin = "cloudflare.com",
        CfIpCountry = "US",
        CfDeviceType = "desktop"
    }
};

// Adding url with headers
root.Files.Add(urlContainer);

string json = JsonConvert.SerializeObject(root);

Note: I'm using Newtonsoft.Json


Q: Pass value from internal to external function - Cannot save password I try to hash passwords with crypto and I cannot save them in the database.
I have node.js 4.2.3 express 4.13.3, and my database is PostgreSQL 9.1. The field is character varying (255) and is named pswrd.
This is my code:
var tobi = new User({
    usrnm:'sp',
    pswrd:'an'
});

module.exports = User;

function User(obj){
    for(var key in obj){
        this[key] = obj[key];
    }
}

User.prototype.save = function (fn){
    var user=this;
     //EDIT, added this :
     var psw ;
    var salt = crypto.randomBytes(50).toString('base64');
    crypto.pbkdf2(user.pswrd, salt, 10000, 150, 'sha512',function(err, derivedKey) {
        //user.pswrd = derivedKey.toString('hex');
         //EDIT, added this:
         var justCrypted = derivedKey.toString('hex');
    });

    var query=client.query('INSERT INTO mytable(usrnm,pswrd)VALUES($1,$2) RETURNING mytable_id',[user.usrnm,user.pswrd], function(err, result) {
        if(err) {console.log(err)}
        else {
          var newlyCreatedId = result.rows[0].mytable_id;
        }
    });
    query.on("end", function (result) {console.log(result);client.end();});
}

tobi.save(function (err){
    if (err)throw error;
    console.log("yo");
})

To run this, I type node lib/user. I get no errors, but the password is not saved properly. The first value gets saved, the an, not the hashed one. What am I missing here?
EDIT
AshleyB answer is good, but, please help me understand how to pass data from an internal function (crypto.pbkdf2) to its external (User.prototype.save = function (fn)) , when the internal has predifined, fixed syntax (crypto.pbkdf2) , so I dont know if or how I can edit it.
How can I leave code as is and still pass the justCrypted back to psw (see edits on code) ? If it was a function that I wrote, I could use apply I guess, but, crypto.pbkdf2 is  predifined and I dont know if can add stuff to it.
Thanks

A: The problem is with the scope, currently the query the altered user.pswrd is outside of the scope of the query so it falls back to the value assigned at the top.
By moving the query inside the 'crypto.pbkdf2'... block the user.pswrd value will work as intended. I've updated your code (and made the salt generation asynchronous, as you have used the async version of pbkdf2 anyway).
var tobi = new User({
  usrnm: 'sp',
  pswrd: 'an'
});

module.exports = User;

function User(obj) {
  for (var key in obj) {
    this[key] = obj[key];
  }
}

User.prototype.save = function(fn) {
  var user = this;

  // Changed salt generation to async version
  // See: https://nodejs.org/api/crypto.html#crypto_crypto_randombytes_size_callback

  crypto.randomBytes(50, function(ex, buf) {
  if (ex) throw ex;
  salt = buf.toString('base64');

    // password, salt, iterations, keylen[, digest], callback
    crypto.pbkdf2(user.pswrd, salt, 10000, 150, 'sha512', function(err, derivedKey) {
    user.pswrd = derivedKey.toString('hex');

        // Moved the query within the hash function
    var query = client.query('INSERT INTO mytable(usrnm,pswrd)VALUES($1,$2) RETURNING mytable_id', [user.usrnm, user.pswrd], function(err, result) {
      if (err) {
        console.log(err)
      } else {
        var newlyCreatedId = result.rows[0].mytable_id;
      }
    });
    query.on("end", function(result) {
      console.log(result);
      client.end();
    });

  });

    });

}

tobi.save(function(err) {
  if (err) throw error;
  console.log("yo");
})

A: To answer your edit, I think you just need to understand that crypto.pbkdf2 is an asynchronous function that has a callback function as it's last parameter.
So within that callback function, you can access the psw variable, and you are able to do something like psw = newlyCreatedId.  However, in your code the query is most likely called before the callback is.  Therefore, you cannot make use of the psw variable in the query as your code stands.
What Ashley B did is to put your query function within the callback, to ensure that the query is not called until after the crypto function.  You could also structure your code to use events, or promises if you didn't wish to nest the functions.

A: This is to answer your EDIT and to help you understand the passing of data between internal and external functions:
I've modified your code slightly to demonstrate the point. What you must understand is the scope and context of the calling function and how your function gets invoked. Crypto.pbkdf2 function accepts a function as a callback. This function is not part of the regular/sequential flow of control. The pbkdf2 function is an 'asynchronous' function - it returns immediately, but the processing can continue on background. This is why the code marked with '//Output 1: This will be undefined' below, will output 'undefined'. The main thread continues while pbkdf2 continues processing in background.
When the system is done processing, it calls the function you specified as the callback to let you know its done processing. Think of it as an SMS you send to someone vs. making a phone call. They reply back with an SMS whenever they get a chance.
However, when the system invokes the callback function, the "scope" or the "context" is different (think of it as an external entity calling your callback function). Therefore, it does not know what the scope/variables/functions in your context. In order to pass data between callback function and your objects, you can use the 'bind' method along with 'this' keyword:
var tobi = new User({
    usrnm:'sp',
    pswrd:'an'
});
module.exports = User;
function User(obj){
    for(var key in obj){
        this[key] = obj[key];
    }
}
User.prototype.save = function (fn){
    var user=this;

     var psw ; //Change this to this.psw to pass changes back.
     var crypto = require('crypto');
    var salt = crypto.randomBytes(50).toString('base64');
    crypto.pbkdf2(user.pswrd, salt, 10000, 150, 'sha512',function(err, derivedKey) {

         var justCrypted = derivedKey.toString('hex');
         console.log('Finished crypting' + justCrypted);
         this.psw = justCrypted;
//When we use 'this' here, we set the value in the right scope/context.
//The var psw does not exist outside the scope of this function, so the
//external function will not know about it. By defining it in the current
// (this) scope, we ensure that external function is also aware of it.
         this.externalFunction(justCrypted);
//Bind the callback function to the current context - it will remember
//this when the callback gets invoked. Think of it as explicitly specifying
//the 'context' of the call/method.
    }.bind(this));
    //Output 1: This will be undefined as callback has not returned yet.
    console.log('New psw' + this.psw);

    /*var query=client.query('INSERT INTO mytable(usrnm,pswrd)VALUES($1,$2) RETURNING mytable_id',[user.usrnm,user.pswrd], function(err, result) {
        if(err) {console.log(err)}
        else {
          var newlyCreatedId = result.rows[0].mytable_id;
        }
    });
    query.on("end", function (result) {console.log(result);client.end();}); */
}
User.prototype.externalFunction = function (someData) {
    console.log('Data passed from an internal function');
    console.log('New PSW: ' + this.psw);
    console.log('Data from external function: ' + someData);
}
tobi.save(function (err){
    if (err)throw error;
    console.log("yo");
})

If I run the above, I see the following output. Note that data is now being passed between internal and external functions via the callback:
C:\temp\node\npm-test>node index.js
New pswundefined
3f9219ae14f9246622973724ace5cb66b190a4b5e86abf482fce5d7889e6aff870741d672ff78e7765fada25f150c70e5e61
c13b5bcdec634d03830668348b5a7d06cf75f426259dcf804241eb2f4362d10f1ebf23a1aecca28072a3f38fb8a39eba88c9
f055e9e7ccabafcd8caed25d8b26f3726022973175545f77e4024bcbcc657081ea5d1f2baf5e9080cbb20696135f2be8834c

Data passed from an internal function
New PSW: 3f9219ae14f9246622973724ace5cb66b190a4b5e86abf482fce5d7889e6aff870741d672ff78e7765fada25f15
0c70e5e61c13b5bcdec634d03830668348b5a7d06cf75f426259dcf804241eb2f4362d10f1ebf23a1aecca28072a3f38fb8a
39eba88c9f055e9e7ccabafcd8caed25d8b26f3726022973175545f77e4024bcbcc657081ea5d1f2baf5e9080cbb20696135
f2be8834c
Data from external function: 3f9219ae14f9246622973724ace5cb66b190a4b5e86abf482fce5d7889e6aff870741d6
72ff78e7765fada25f150c70e5e61c13b5bcdec634d03830668348b5a7d06cf75f426259dcf804241eb2f4362d10f1ebf23a
1aecca28072a3f38fb8a39eba88c9f055e9e7ccabafcd8caed25d8b26f3726022973175545f77e4024bcbcc657081ea5d1f2
baf5e9080cbb20696135f2be8834c

Have a look here to understand how bind works and here to understand the concept of this and context in javascript.
EDIT: In response to your latest, comment.
if you just wanted to pass the justCrypted from pbkdf2 to the var psw of User.prototype.save, you can use the synchronous version of the same method:
crypto.pbkdf2Sync(password, salt, iterations, keylen[, digest])

Your method will then look like:
User.prototype.save = function (fn){
    var user=this;
    var psw ;
    var crypto = require('crypto');
    var salt = crypto.randomBytes(50).toString('base64');
    psw = crypto.pbkdf2Sync(user.pswrd, salt, 10000, 150, 'sha512');
    var justCrypted = psw.toString('hex');
    console.log(justCrypted);
    ....
    ....
}

The important difference here is that this is an synchronous method and the code will wait for crypto.pbkdf2Sync to finish returning a value before it moves to the next line, thus following a sequential flow.
Hope that helps.
EDIT 2: Here is a little diagram of how async works:

Unfortunately, you just can't do it the way you want without using an external function or a synchronous function.


Q: file upload and download with servlet I want to write an simple java servlet which receives file upload from a simple html page, does something with the file and sends back the manipulated file to the user.
The file upload event is handled by doPost method of a servlet on the server side. The requirements is that "Save as..." window needs to be appear immediately after the file is uploaded and processed on the server side and user can save the generated file to his/her computer.
File upload is handled by doPost and file download is handled by doGet method of a servlet. Is it possible to boundle these two events together somehow?
Can I receive byte content as a response of a file upload event? I upload file with ajax post and I can send back content to the browser and I can show this message on the client side.
Is it possible to write a clever java script code which receives binary content as a response of a post request and forces the web browser to pop up the "save as..." window for save the received bytes to a file?
Another solution could be

*

*file upload with ajax

*on the server side the new generated content is saved to a file into a temporary directory

*id or path of the file on the server is sent back to the client as a response

*client (java script) receives the id of the generated file and makes a new GET request immediately to a file download servlet

*file download servlet receives the id/path of the file and reads it from the temp folder and send i bact to the client web browser.

What is the best practice for this scenario?


Q: What is the importance of collections framework in java for the programming of android and how to benefit from it How to use true data structure in android programming I need good learning resources and examples of applying data structure in certain functions

A: The collections framework was designed to meet several goals, such as −

*

*The framework had to be high-performance. The implementations for the fundamental collections (dynamic arrays, linked lists, trees, and hashtables) were to be highly efficient.

*The framework had to allow different types of collections to work in a similar manner and with a high degree of interoperability.

*The framework had to extend and/or adapt a collection easily.

A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain the following −
Interfaces − These are abstract data types that represent collections. Interfaces allow collections to be manipulated independently of the details of their representation. In object-oriented languages, interfaces generally form a hierarchy.
Implementations, i.e., Classes − These are the concrete implementations of the collection interfaces. In essence, they are reusable data structures.
Algorithms − These are the methods that perform useful computations, such as searching and sorting, on objects that implement collection interfaces. The algorithms are said to be polymorphic: that is, the same method can be used on many different implementations of the appropriate collection interface.
For More Details you can find documents on ORACLE's Collection Documents


Q: Numpy array to Pandas data frame formatting Sorry if this has already been answered somewhere!
I am trying to format an array in numpy to a data frame in pandas, which I have done like so:
# array
a = [[' ' '0' 'A' 'T' 'G']
    ['0' 0 0 0 0]
    ['G' 0 -3 -3 5]
    ['G' 0 -3 -6 2]
    ['A' 0 5 0 -3]
    ['A' 0 5 2 -3]
    ['T' 0 0 10 5]
    ['G' 0 -3 5 15]]

# Output data frame using pandas

   0  1   2   3   4
0     0   A   T   G
1  0  0   0   0   0
2  G  0  -3  -3   5
3  G  0  -3  -6   2
4  A  0   5   0  -3
5  A  0   5   2  -3
6  T  0   0  10   5
7  G  0  -3   5  15

# Output I want

   0   A   T   G
0  0   0   0   0
G  0  -3  -3   5
G  0  -3  -6   2
A  0   5   0  -3
A  0   5   2  -3
T  0   0  10   5
G  0  -3   5  15

Any advice on how to do this would be appreciated! :)

A: Declare the first row to be column names and the first column to be row names:
df = pd.DataFrame(data=a[1:], columns=a[0]).set_index(' ')
df.index.name = None
#   0  A  T  G
#0  0  0  0  0
#G  0 -3 -3  5
#G  0 -3 -6  2
#A  0  5  0 -3


Q: Java - Saving StringTokenizer into arrays for further processing in other methods I've been coding Perl and Python a lot and this time I got an assignment to code in Java instead. So I'm not too familiar with handling data in Java.
My task involves having a input file where I need to check dependencies and then output those with transitive dependencies. Clearer ideas below:
Input File:
A: B C
B: C E
C: G
D: A

Output File:
A: B C E G
B: C E G
C: G
D: A B C E G

So far this is what I've got (separating the first and second token):
import java.util.StringTokenizer;
import java.io.*;

public class TestDependency {

    public static void main(String[] args) {

        try{

        FileInputStream fstream = new FileInputStream("input-file");
        BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
            String strLine;

        while ((strLine = br.readLine()) != null)   {
            StringTokenizer items = new StringTokenizer(strLine, ":");

            System.out.println("I: " + items.nextToken().trim());

            StringTokenizer depn = new StringTokenizer(items.nextToken().trim(), " ");

                while(depn.hasMoreTokens()) {
                    System.out.println( "D: " + depn.nextToken().trim() );
            }
        }

        } catch (Exception e){//Catch exception if any
        System.err.println("Error: " + e.getMessage());
        }
    }
}

Any help appreciated. I can imagine Perl or Python to handle this easily. Just need to implement it in Java.

A: This is not very efficient memory-wise and requires good input but should run fine.
public class NodeParser {

    // Map holding references to nodes
    private Map<String, List<String>> nodeReferenceMap;

    /**
     * Parse file and create key/node array pairs
     * @param inputFile
     * @return
     * @throws IOException
     */
    public Map<String, List<String>> parseNodes(String inputFile) throws IOException {

        // Reset list if reusing same object
        nodeReferenceMap = new HashMap<String, List<String>>();

        // Read file
        FileInputStream fstream = new FileInputStream(inputFile);
        BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
        String strLine;

        // Parse nodes into reference mapping
        while((strLine = br.readLine()) != null) {
            // Split key from nodes
            String[] tokens = strLine.split(":");
            String key = tokens[0].trim();
            String[] nodes = tokens[1].trim().split(" ");
            // Set nodes as an array list for key
            nodeReferenceMap.put(key, Arrays.asList(nodes));
        }

        // Recursively build node mapping
        Map<String, Set<String>> parsedNodeMap = new HashMap<String, Set<String>>();
        for(Map.Entry<String, List<String>> entry : nodeReferenceMap.entrySet()) {
            String key = entry.getKey();
            List<String> nodes = entry.getValue();
            // Create initial node set
            Set<String> outSet = new HashSet<String>();
            parsedNodeMap.put(key, outSet);
            // Start recursive call
            addNode(outSet, nodes);
        }

        // Sort keys
        List<String> sortedKeys = new ArrayList<String>(parsedNodeMap.keySet());
        Collections.sort(sortedKeys);

        // Sort nodes
        Map<String, List<String>> sortedParsedNodeMap = new LinkedHashMap<String, List<String>>();
        for(String key : sortedKeys) {
            List<String> sortedNodes = new ArrayList<String>(parsedNodeMap.get(key));
            Collections.sort(sortedNodes);
            sortedParsedNodeMap.put(key, sortedNodes);
        }

        // Return sorted key/node mapping
        return sortedParsedNodeMap;
    }

    /**
     * Recursively add nodes by referencing the previously generated list mapping
     * @param outSet
     * @param nodes
     */
    private void addNode(Set<String> outSet, List<String> nodes) {
        // Add each node to the set mapping
        for(String node : nodes) {
            outSet.add(node);
            // Get referenced nodes
            List<String> nodeList = nodeReferenceMap.get(node);
            if(nodeList != null) {
                // Create array list from abstract list for remove support
                List<String> referencedNodes = new ArrayList<String>(nodeList);
                // Remove already searched nodes to prevent infinite recursion
                referencedNodes.removeAll(outSet);
                // Recursively search more node paths
                if(!referencedNodes.isEmpty()) {
                    addNode(outSet, referencedNodes);
                }
            }
        }
    }
}

Then, you can call this from your program like so:
    public static void main(String[] args) {
        try {
            NodeParser nodeParser = new NodeParser();
            Map<String, List<String>> nodeSet = nodeParser.parseNodes("./res/input.txt");
            for(Map.Entry<String, List<String>> entry : nodeSet.entrySet()) {
                String key = entry.getKey();
                List<String> nodes = entry.getValue();
                System.out.println(key + ": " + nodes);
            }
        } catch (IOException e){
            System.err.println("Error: " + e.getMessage());
        }
    }

Also, the output is not sorted but that should be trivial.

A: String s = "A: B C D";
String i = s.split(":")[0];
String dep[] = s.split(":")[1].trim().split(" ");
System.out.println("i = "+i+", dep = "+Arrays.toString(dep));


Q: change size of kendo window when pressing the "minimize" button I'm trying to change the width of a kendoui window when the "minimize" button is pushed.  I'm new to kendo and coulnd't find the help in their docs.
Here's the code I've got:
HTML:
Some data here

var thewindow = $("#recon_cont_div");
thewindow.kendoWindow({
                          actions: ["Minimize", "Maximize", "Pin", "Close"],
                          width: "100%",
                          height: "100%",
                          draggable: true,
                          resizable: true,
                          minimize: function(e) {
                                debugger;
                                thewindow.data("kendoWindow").setOptions({width: 250});
                          },

                           close: function(e) {
                            alert("window will close now")
                          },

                          title: "The window"
                    }).data("kendoWindow");

When the "minimize function" runs, the window does not resize...it does nothing?
Any help is appreciated. thanks.

A: The window wrapper is the one you need to change:
$("#dialog").kendoWindow({
  actions: ["Minimize"],
  minimize: function(e) {
    $("#dialog").getKendoWindow().wrapper.css({
        width: 200
    });
  }
});

Example: Window size change


Q: Should "/users" and "/users/" point to the same (RESTful) resource? They do in this and probably any other website, but I'm not sure I understand why.
A popular analogy compares RESTful resources to files in the file system and filename users wouldn't point to the same object as filename users/ static web pages and in a static website users would point to users.html and users/ - to a different file - users/index.html.

A:
filename users wouldn't point to the same object as filename users/.

That is not true. In most filesystems, you cannot have a file named users and a directory named users in the same parent directory.
cd users and cd users/ have the same result.

A: Short answer: they may only identify the same resource if one redirects to the other.
URI's identify resources, but they do so differently depending on the response status code to a GET request in HTTP. If one returns a 3xx to the other, then the two URI's identify the same resource. If the two resources each return a 2xx code, then the URI's identify different resources. They may return the same response in reply to a GET request, but they are not therefore the same resource. The two resources may even map to the same handler to produce their reply, but they are not therefore the same resource. To quote Roy Fielding:

The resource is not the storage object. The resource is not a
mechanism that the server uses to handle the storage object. The
resource is a conceptual mapping -- the server receives the identifier
(which identifies the mapping) and applies it to its current mapping
implementation (usually a combination of collection-specific deep tree
traversal and/or hash tables) to find the currently responsible
handler implementation and the handler implementation then selects the
appropriate action+response based on the request content.

So, should /users and /users/ return the same response? No. If one does not redirect to the other, then they should return different responses. However, this is not itself a constraint of REST. It is a constraint, however, which makes networked systems more scalable: information that is duplicated in multiple resources can get out of sync (especially in the presence of caches, which are a constraint of REST) and lead to race conditions. See Pat Helland's Apostate's Opinion for a complete discussion.
Finally, clients may break when attempting to resolve references relative to the given URI. The URI spec makes it clear that resolving the relative reference Jerry/age against /users/ results in /users/Jerry/age, while resolving it against /users (no trailing slash) results in /Jerry/age. It's amazing how much client code has been written to detect and correct the latter to behave like the former (and not always successfully).
For any collection (which /users/ often is), I find it best to always emit /users/ in URI's, redirect /users to /users/ every time, and serve the final response from the /users/ resource: this keeps entities from getting out of sync and makes relative resolution a snap on any client.

A: There are some nuances on this, while "users" represent one resource while "users/" should represent a set of resources, or operations on all resources "users"... But there does not seem to exist a "standard" for this issue.
There is another discussion on this, take a look here: https://softwareengineering.stackexchange.com/questions/186959/trailing-slash-in-restful-api

A: Technically they are not the same. But a request for /users will probably cause a redirect to /users/ which makes them semantically equal.
In terms of JAX-RS @Path, they can both be used for the same path.


Q: Make Cocoa application respond to simple AppleScript command I am trying to add a trivial AppleScript support to a Cocoa application. The application performs a check periodically and I just want to be able to tell it to perform it on demand.
I am trying to follow the SimpleScriptingVerbs Apple example.
I have subclassed NSScriptCommand as follows:
Header:
#import <Cocoa/Cocoa.h>

@interface rdrNotifierUpdateCommand : NSScriptCommand {

}
-(id)performDefaultImplementation;
@end

Implementation:
#import "rdrNotifierUpdateCommand.h"
#import "rdrNotifierAppDelegate.h"

@implementation rdrNotifierUpdateCommand

-(id)performDefaultImplementation {
  NSLog(@"Works at last");
  [((rdrNotifierAppDelegate *)[[NSApplication sharedApplication] delegate])
   checkForNewItems];  // This just fires the timer
  return nil;
}
@end

My .sdef file goes as follows (and the problem seems to be there, but I cannot find it):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd">
<dictionary title="Dictionary" xmlns:xi="http://www.w3.org/2003/XInclude">
    <xi:include href="file:///System/Library/ScriptingDefinitions/CocoaStandard.sdef" xpointer="xpointer(/dictionary/suite)"/>
    <suite name="rdrNotifier Suite" code="rdrN" description="rdrNotifier application specific scripting facilities.">
        <command name="do update" code="rdrNUpdt" description="Check for new items">
            <cocoa class="rdrNotifierUpdateCommand"/>
        </command>
    </suite>
</dictionary>

The Info.plist is set up appropriately.
But, when I try to run the following script in AppleScript editor:
tell application "rdrNotifier"
    do update
end tell

I receive an error about variable "update" not being defined.
I can open the dictionary for my application from AppleScript Editor (i.e. it is successfully registered).
Edit: Found a solution
The problem was indeed in the sdef file: I was not specifying that the application can reply to the command. My final definition goes as follows (Obj-C code unchanged):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd">
<dictionary title="Dictionary" xmlns:xi="http://www.w3.org/2003/XInclude">
  <!-- I have removed the standard suite as the application does not open, print... -->
  <suite name="rdrNotifier Suite" code="rdrN" description="rdrNotifier application specific scripting facilities.">
    <command name="do update" code="rdrNUpdt" description="Check for new items">
      <cocoa class="rdrNotifierUpdateCommand"/>
    </command>
    <class name="application" code="Capp">
      <cocoa class="NSApplication"/>
      <responds-to name="do update">
        <!-- Note you need to specify a method here,
             although it is blank -->
        <cocoa method=""/>
      </responds-to>
    </class>
  </suite>
</dictionary>

Any improvements/tips/criticisms are still welcome.

A: Thanks for making the effort of adding applescript support to your app! Just a quick observation / criticism: When constructing terminology, by all means include spaces, but if that terminology takes the form of 'verb noun' (like 'do update') appleScripters will be irritated if the noun 'update' is not a properly modelled object, and if 'do' is not a proper command.
I would argue that 'do' is a poor choice for a command name, because it is very vague. What's wrong with just using 'update' as the command? (i.e. treat it as a verb).
This issue is treated in some detail by Apple's own technote 2106 (currently here), which you definitely should read and digest if you hope to please your AppleScripting user community.
Apple themselves are not beyond stupid terminology choices such as updatePodcast and updateAllPodcasts (in iTunes). These are stupid choices because (according to tn2106) they contain no spaces, and because the better choice would be to have a proper podcast class and then have an update command which could be used on an individual podcast, all podcasts, or a particular chosen set of podcasts.


Q: Cython print() outputs before C printf(), even when placed afterwards I'm trying to pick up Cython.
import counter

cdef public void increment():
    counter.increment()

cdef public int get():
    return counter.get()

cdef public void say(int times):
    counter.say(times)

This is the "glue code" I'm using to call functions from counter.py, a pure Python source code file. It's laid out like this:
count = 0

def increment():
    global count

    count += 1

def get():
    global count

    return count

def say(times):
    global count

    print(str(count) * times)

I have successfully compiled and run this program. The functions work fine. However, a very strange thing occured when I tested this program:
int main(int argc, char *argv[]) {
    Py_Initialize();

    // The following two lines add the current working directory
    // to the environment variable `PYTHONPATH`. This allows us
    // to import Python modules in this directory.
    PyRun_SimpleString("import sys");
    PyRun_SimpleString("sys.path.append(\".\")");

    PyInit_glue();

    // Tests
    for (int i = 0; i < 10; i++)
    {
        increment();
    }

    int x = get();
    printf("Incremented %d times\n", x);

    printf("The binary representation of the number 42 is");
    say(3);

    Py_Finalize();
    return 0;
}

I would expect the program to produce this output:
Incremented 10 times
The binary representation of the number 42 is
101010

However, it prints this:
Incremented 10 times
101010
The binary representation of the number 42 is

But if I change the line
printf("The binary representation of the number 42 is");

to
printf("The binary representation of the number 42 is\n");

then the output is corrected.
This seems strange to me. I understand that if I want to print the output of a Python function, I might just as well return it to C and store it in a variable, and use C's printf() rather than the native Python print(). But I would be very interested to hear the reason this is happening. After all, the printf() statement is reached before the say() statement (I double checked this in gdb just to make sure). Thanks for reading.


Q: How to minimize amount of requests for Google API (places) autocomplete? I am using google API - places to autocomplete locations in AutoCompleteTextView. I did couple of tests maybe 20 searches and in my profile on google devs, it's written that I have made 200 API requests.
I understand that it is making request every time I type something or edit typed text. But is there any way how to reduce amount of calls to server?
One of ideas is to define threshold to 3 characters.

A: If you did not find a better solutions( I know it is very late, but I am doing something similar and I found my self in here.)
I think you can "cache" previous results, put them in ArrayList. And then in your adapter when performFiltering is called, start by checking the results you have in your Arraylist and if you receive a number of similar items, dont make a request to the API. I am not sure if it is clear but you can at least save a couple requests like that and for the user side, the autocomplete is always refreshing.
I hope it helps.

A: I think a better implementation to reduce the number of calls to the Places API would be to put in a delay of maybe 0.75 seconds after the last key-press.  That would avoid confusion on the part of a user who after seeing autocomplete after 3 letters, sits and waits after typing his 5th.


Q: Import csv in Rapidminer is not loading data properly Importing csv in Rapidminer is not loading data properly in the attributes/ columns and returns errors.
I have set the parameter values correctly in the 'Data Import Wizard'.
Column Separation is set to comma and when I check the "Use Quotes" parameter I see that there are too many "?" appear in the columns even though there is data in the actual csv file.
And when I do not check the “Use Quotes” option then I notice that the content of the columns are distributed across different columns, i.e., data does not appear in the correct column. It also gives error for the date column.
How to resolve this? Any suggestions please? I saw a lot of Rapidminer videos and read about it but did not help.
I am trying to import twitter conversations data which I exported from a 3rd party SaaS tool which extracts Twitter data for us.
Could someone help me soon please? Thanks, Geeta

A: It's virtually impossible to debug this without seeing the data.
The use quotes option requires that each field is surrounded by double quotes. Do not use this if your data does not contain these because the input process will import everything into the first field.
When you use comma as the delimiter, the observed behaviour is likely to be because there are additional commas contained in the data. This seems likely if the data is based on Twitter. This confuses the import because it is just looking for commas.
Generally, if you can get the input data changed, try to get it produced using a delimiter that cannot appear in the raw text data. Good examples would be | or tab. If you can get quotes around the fields, this will help because it allows delimiter characters to appear in the field.
Dates formats can be handled using the data format parameter but my advice is to import the date field as a polynominal and then convert it later to date using the Nominal to Date operator. This gives more control especially when the input data is not clean.


Q: Print what is not in a join table I have a join table created from 2 other tables.
Say one model is called cat, another is called request, and the join table is called catrequest (this table would have cat_id and request_id)
How would I print all of the cats not intersected in the join table i.e. all of the cats NOT requested using rails. I saw some DB based answers, but I am looking for a rails solution using ruby code.
I get how to print a cat that belongs to a request i.e.:
<% @requests.each do |request| %>
  <% request.cats.each do |cat| %>
    <%= cat.name %>
<% end %>

but I don't understand how to do the reverse of this.

A: To get a list of cats that have never been requested you'd go with:
Cat.includes(:cat_requests).where(cat_requests: { id: nil })
# or, if `cat_requests` table does not have primary key (id):
Cat.includes(:cat_requests).where(cat_requests: { cat_id: nil })

The above assumes you have the corresponding association:
class Cat
  has_many :cat_requests
end

A: It sounds like what you need is an outer join, and then to thin out the cats rows that don't have corresponding data for the requests? If that's the case, you might consider using Arel. It supports an outer join and can probably be used to get what you're looking for. Here is a link to a guide that has a lot of helpful information on Arel:
http://jpospisil.com/2014/06/16/the-definitive-guide-to-arel-the-sql-manager-for-ruby.html
Search the page for "The More the Merrier" section which is where joins are discussed.


Q: Can I parallelise a Python function that returns None with Ray? I have a function that takes in a filename and creates a symlink in a target directory if that filename exists in the "source" directory:
def _symlink_photos(photo_name: str, photos_dir: Path, target_dir: Path) -> None:
    """
    Given a string, checks if it exists in `photos_dir`,
    then creates a symlink in `target_dir`
    """

    photo_path: Path = photos_dir / photo_name
    if photo_path.exists():
        (target_dir / photo_name).symlink_to(photo_path)

This function is meant to be used to iterate over a somewhat big list of filenames (some 70k), like so:
def main() -> None:
    """
    Given a list of photos, checks if they exist in the source directory,
    and symlinks them into the target directory.
    """

    for photo_name in tqdm(photo_list):
        _symlink_photos(photo_name, photo_dir, target_dir)

The brute-force/for-loop approach is taking me too long (tqdm estimates over a day), so I was wondering how to parallelise this job. I knew I could use ray, but in order to use it I would need to write my code like this:
futures = [_symlink_photos.remote(photo_name, photo_dir, target_dir) for photo in photo_list]
result = ray.get(futures)

Yet this does not make sense to me, as my function does not return anything. Can I still use ray? Should I switch to something else, like joblib/threading?
Another alternative I was thinking of is to parallelise the "check if photo_name exists in the source dir" part with ray, i.e. to retrieve the list of photos that actually are in the target directory. Only then I would create the symlinks, hopefully taking less time...

A: You can use Ray in the way that you described. The ray.get call will simply return a list of None values, which you can ignore.
You can also look up ray.wait which can be used to wait for certain tasks to finish without actually retrieving the task outputs.


Q: DirectX 2D grid I have been trying to get a 2D grid going. It's for a game map.
Unfortunately, the grid is not as it should be. And I cannot figure out why.
Does anyone have an idea?
Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D

Public Class clsIsometric

'==================================
' SETTINGS
'==================================

Private tile_size As New Point(64, 64) 'Size of one tile in pixels
Private map_size As New Point(25, 25) 'Amount of tiles in total

Private gDevice As Device
Private bufVertex As VertexBuffer
Private bufIndex As IndexBuffer
Private gVertices() As CustomVertex.TransformedColored
Private gIndices() As Integer

'==================================
' CONSTRUCTOR
'==================================

Public Sub New(vDevice As Device)

    gDevice = vDevice

End Sub

Public Sub dispose()

    bufVertex.Dispose()
    bufIndex.Dispose()
    bufVertex = Nothing
    bufIndex = Nothing

End Sub

'==================================
' RENDERING
'==================================

Public Sub buildMap()

    ' Recreate buffers to fit the map size
    ReDim gVertices((map_size.X + 1) * (map_size.Y + 1))    ' x+1 * y+1
    ReDim gIndices(map_size.X * map_size.Y * 6)             ' x * y * 6
    Dim k As Integer

    For cX = 0 To map_size.X - 1 'Rows
        For cY = 0 To map_size.Y - 1 'Columns

            'VERTEX
            k = cX * map_size.X + cY
            gVertices(k) = New CustomVertex.TransformedColored(cX * tile_size.X, cY * tile_size.Y, 0, 1, Color.Blue.ToArgb)

        Next cY
    Next cX

    Dim vertexPerCol As Integer = map_size.Y + 1

    k = 0
    For ccX = 0 To map_size.X - 1
        For ccY = 0 To map_size.Y - 1

            gIndices(k) = ccX * vertexPerCol + ccY                  ' 0
            gIndices(k + 1) = (ccX + 1) * vertexPerCol + (ccY + 1)  ' 1
            gIndices(k + 2) = (ccX + 1) * vertexPerCol + ccY        ' 2

            gIndices(k + 3) = ccX * vertexPerCol + ccY              ' 3
            gIndices(k + 4) = ccX * vertexPerCol + (ccY + 1)        ' 4
            gIndices(k + 5) = (ccX + 1) * vertexPerCol + (ccY + 1)  ' 5

            k += 6 'Each tile has 6 indices. Increase for next tile

        Next
    Next

    bufVertex = New VertexBuffer(GetType(CustomVertex.TransformedColored), gVertices.Length, gDevice, Usage.Dynamic Or Usage.WriteOnly, CustomVertex.TransformedColored.Format, Pool.Default)
    bufIndex = New IndexBuffer(GetType(Integer), gIndices.Length, gDevice, Usage.WriteOnly, Pool.Default)

End Sub

Public Sub render()

    'RENDER THE MAP

    bufVertex.SetData(gVertices, 0, LockFlags.ReadOnly)
    bufIndex.SetData(gIndices, 0, LockFlags.None)

    gDevice.VertexFormat = CustomVertex.TransformedColored.Format
    gDevice.SetStreamSource(0, bufVertex, 0)
    gDevice.Indices = bufIndex
    gDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, gVertices.Length, 0, CInt(gIndices.Length / 3))

End Sub

End Class

This should output a perfect square grid of 25 by 25 tiles. But the wireframe looks like:
http://i43.tinypic.com/2whf51c.jpg

A: Your loop which build the vertices seems to end too early, because you have n+1 vertices each row/column. It fills the array only to the forelast column, which leads to a shift in the vertices.
For cX = 0 To map_size.X 'Rows
    For cY = 0 To map_size.Y 'Columns

        'VERTEX
        k = cX * (map_size.X + 1) + cY
        gVertices(k) = New CustomVertex.TransformedColored(cX * tile_size.X, cY * tile_size.Y, 0, 1, Color.Blue.ToArgb)

    Next cY
Next cX


Q: What do you call an application that does not come with an installer? what you call a software that requires no installation only unzipping like eclipse where it has no installation only we have to unzip and also for uninstalling just delete the Workspace.

A: That's typically called a portable application.


Q: MomentJs giving NaN result I'm trying to count the days from current date to a certain date, and i'm using momentJs to do it, the problem is that it's giving me NaN result, maybe because of the date formatting!
I'm fetching the end date from an api, and it's in this format "DD MM YYYY" "16/12/2023" but in moment I don't know how it's formatted, some help would be appreciated.
The code i'm using
const start = moment();
  const end = moment(post.validTill);
  const diff = end.diff(start, "days");

I just want to get the days from current date till the end date !

A: You can calculate the duration and request the value in days.

const
  diffInDays = (start, end) => moment.duration(end.diff(start)).asDays(),
  dateFormat = 'DD MM YYYY',
  nextWeek   = moment().add(1, 'weeks').format(dateFormat),
  post       = { validTill: nextWeek },
  diff       = diffInDays(moment(), moment(post.validTill, dateFormat));

console.log('Start of next week:', nextWeek);
console.log('Days until:', diff); // 6.xxx (days)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>


Q: Asp Core 3 - How to allow nullable [FromBody] object in controller method Let's say I have a simple controller with one POST method that accepts an object from its body. However, the presence of this object should be optional in the HTTP request body. I tried to implement this behavior with the following code
public class User
{
    public string Name { get; set; }
}

[ApiController]
[Route("[controller]")]
public class GreetingController : ControllerBase
{
    [HttpPost]
    public string SayHello([FromBody] User user = null)
    {
        return "Hello " + user?.Name;
    }
}

If I make a request with an object in the body, everything works fine. But with this configuration, it is unable to make a POST request with empty body. If I create a request without Content-Type header (as there is actually no content), I get the following error:
{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.13",
    "title": "Unsupported Media Type",
    "status": 415,
    "traceId": "|192e45d5-4bc216316f8d3966."
}

if the Content-Type header has value application/json then the response looks like this:
{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "|192e45d6-4bc216316f8d3966.",
    "errors": {
        "": [
            "A non-empty request body is required."
        ]
    }
}

So, how to make the object optional in the request body? It is quite common problem and I am curious if there is a simple solution for that in ASP Core 3. I don't want to read the object from Request stream and deserialize it on my own.

A: Right now, the only way is via a global option, MvcOptions.AllowEmptyInputInBodyModelBinding. It defaults to false, so you just need to do:
services.AddControllers(o =>
{
    o.AllowEmptyInputInBodyModelBinding = true;
});

A: There is now an easier way (since 5.0-preview7)
You can now achieve this per action method, by configuring a FromBodyAttribute property named EmptyBodyBehavior
Demonstration:
public IActionResult Post([FromBody(EmptyBodyBehavior = EmptyBodyBehavior.Allow)] MyModel model)

Thanks to LouraQ's comment, which guided me towards the above answer on github

A: I'm not sure about your final intention, but if you don't want to choose the content-type, you can pass an empty json string.
At this time, the user is not empty, but the content of it's field's value is null, and the final result is the same. Maybe you You can try it.
Here is the debug process with the postman:


Q: String that contains $1 I have a script that scrolls through all the folders of unzipped files and get information of the contents.
The problem is in the name of files such as:
filename="SearchView$10.smali"

Because if I want to get the stats the $1 in the string disappears,and I get the following error:
    subprocess.check_output("stat "+ filename,shell=True)
cannot open `SearchView0.smali' (No such file or directory)

How can I fix it?

A: Avoid shell=True, it leads to security issues. And it is also at the root of your problem, as the $1 is interpreted.
Do this instead:
subprocess.check_output(["stat", filename])


Q: Only some Font Awesome glyphs display in WPF I am attempting to use Font Awesome in a WPF app.  It half works. That is, some glyphs are picked up and show, others show a rectangle.
I'm following guides correctly, I think, but something is going wrong!
Here's a code snippet
<Window.Resources>
    <FontFamily x:Key="FontAwesomeRegular">Fonts/Font Awesome 5 Free-Regular-400.otf#Font Awesome 5 Free Regular</FontFamily>
    <FontFamily x:Key="FontAwesomeBrands">Fonts/Font Awesome 5 Brands-Regular-400.otf#Font Awesome 5 Brands Regular</FontFamily>
    <FontFamily x:Key="FontAwesomeSolid">Fonts/Font Awesome 5 Free-Regular-400.otf#Font Awesome 5 Free Solid</FontFamily>
</Window.Resources>

<Grid>
    <StackPanel Orientation="Vertical">
    <TextBlock FontFamily="{StaticResource FontAwesomeSolid}" Text="" HorizontalAlignment="Left" Foreground="Red" Margin="0" TextWrapping="Wrap" VerticalAlignment="Center" FontSize="100"/>
    <TextBlock FontFamily="{StaticResource FontAwesomeSolid}" Text="" HorizontalAlignment="Left" Foreground="Red" Margin="0" TextWrapping="Wrap" VerticalAlignment="Center" FontSize="100"/>
    </StackPanel>
</Grid>

The fonts are installed in a Fonts directory

And Marked as Resource

And this is what it shows on the screen

You can see that the first icon xf15c; displays as expected but the second one xf15a; does not.  Generally, most do not display.
Why?

A: If you open the .otf file, you can see the name of the font. This is what you need to use to the FontFamily resource, without the file extension.
Try the following code:
<FontFamily x:Key="Icons">pack://application:,,,/{YOUR PROJECT NAME};component/Fonts/#Font Awesome 5 Free Solid</FontFamily>


Q: Backbone: event lost in re-render I have super-View who is in charge of rendering sub-Views. When I re-render the super-View all the events in the sub-Views are lost.
This is an example:
var SubView = Backbone.View.extend({
    events: {
        "click": "click"
    },

    click: function(){
        console.log( "click!" );
    },

    render: function(){
        this.$el.html( "click me" );
        return this;
    }
});

var Composer = Backbone.View.extend({
    initialize: function(){
        this.subView = new SubView();
    },

    render: function(){
        this.$el.html( this.subView.render().el );
    }
});

var composer = new Composer({el: $('#composer')});
composer.render();

When I click in the click me div the event is triggered. If I execute composer.render() again everything looks pretty the same but the click event is not triggered any more.
Check the working jsFiddle.

A: When you do this:
this.$el.html( this.subView.render().el );

You're effectively saying this:
this.$el.empty();
this.$el.append( this.subView.render().el );

and empty kills the events on everything inside this.$el:

To avoid memory leaks, jQuery removes other constructs such as data and event handlers from the child elements before removing the elements themselves.

So you lose the delegate call that binds events on this.subView and the SubView#render won't rebind them.
You need to slip a this.subView.delegateEvents() call into this.$el.html() but you need it to happen after the empty(). You could do it like this:
render: function(){
    console.log( "Composer.render" );
    this.$el.empty();
    this.subView.delegateEvents();
    this.$el.append( this.subView.render().el );
    return this;
}

Demo: http://jsfiddle.net/ambiguous/57maA/1/
Or like this:
render: function(){
    console.log( "Composer.render" );
    this.$el.html( this.subView.render().el );
    this.subView.delegateEvents();
    return this;
}

Demo: http://jsfiddle.net/ambiguous/4qrRa/
Or you could remove and re-create the this.subView when rendering and sidestep the problem that way (but this might cause other problems...).

A: There's a simpler solution here that doesn't blow away the event registrations in the first place: jQuery.detach().
http://jsfiddle.net/ypG8U/1/
this.subView.render().$el.detach().appendTo( this.$el );

This variation is probably preferable for performance reasons though:
http://jsfiddle.net/ypG8U/2/
this.subView.$el.detach();

this.subView.render().$el.appendTo( this.$el );

// or

this.$el.append( this.subView.render().el );

Obviously this is a simplification that matches the example, where the sub view is the only content of the parent. If that was really the case, you could just re-render the sub view. If there were other content you could do something like:
var children = array[];

this.$el.children().detach();

children.push( subView.render().el );

// ...

this.$el.append( children );

or
_( this.subViews ).each( function ( subView ) {

  subView.$el.detach();

} );

// ...

Also, in your original code, and repeated in @mu's answer, a DOM object is passed to jQuery.html(), but that method is only documented as accepting strings of HTML:
this.$el.html( this.subView.render().el );

Documented signature for jQuery.html():
.html( htmlString )

http://api.jquery.com/html/#html2

A: When using $(el).empty() it removes all the child elements in the selected element AND removes ALL the events (and data) that are bound to any (child) elements inside of the selected element (el).
To keep the events bound to the child elements, but still remove the child elements, use:
$(el).children().detach(); instead of $(.el).empty();
This will allow your view to rerender successfully with the events still bound and working.


Q: DocuSign field mandatory/not mandatory per REST request I have a question: is it possible to choose a field to be mandatory or not mandatory per DocuSign REST request when we are forming an envelope?
In some cases I need field to be mandatory, in other - not (or even not usable)

A: Yes, document tabs have parameters required which can be set to true or false.
They also have the parameter readOnly which can also be set to true or false
The API reference has all the parameters in it


Q: PhantomJS driver is not clicking on element in page, but no errors thrown Lately I have faced on issue with latest phantomjs driver and selenium in java development, so issue is this , we have a web site with list of users where every line of user have a delete button which have js action onclick(). What we are trying to do is to simply remove user, but it is not working on phantomjs, but works perfectly on chrome driver. Note that after clicking on delete button js alert is shown with yes/no, so it must be accepted also.
So here is our current code:
FindBy(css = ".glyphicon.glyphicon-trash.text-blue")
private WebElement customerRemoveButton;
.
.
.
.
Actions mouseAction = new Actions(driver);
mouseAction.moveToElement(customerRemoveButton);
mouseAction.click();
mouseAction.build().perform();
acceptAlert();

driver is properly initialised  (cause obviously for chrome driver it is working perfectly), note that we already tried with clicking on button with JS Executor of webdriver and ofc with click().
So, when we execute it via chrome, user successfully removed, when via phantomjs test is passing without any single issue, but user is not removed.

A: Phantom JS something act weirdly. Check if you have a overlay of custom element over html element. if so try to click on custom element and not on actual html element.
if it doesn't work try to click using Javascript, that is you best bet.


Q: How to load a text file and store into data structure I have a 'text' file having content as follows:
012abc3cb7503bddef3ff59e0e52fd79.jpg 160.073318 18.588472 14.246923 8.054444 6.600504 6.261390 5.838249 4.447019 3.639888 3.357715 2.996645 2.910991 2.574769 2.527163 2.343448 2.264113 2.176161 2.088773 1.915582 1.902159 1.836033 1.725432 1.667595 1.633245 1.557424 1.542059 1.434280 1.430181 1.321047 1.302652 1.272890 1.261313 1.188892 1.138115 1.114376 1.070352 1.044311 1.025954 0.993622 0.988920 0.969866 0.933977 0.931669 0.913624 0.882856 0.876036 0.840088 0.822686 0.814072 0.787075 0.781157 0.778171 0.763771 0.748851 0.740975 0.708208 0.691589 0.688566 0.664124 0.659779 0.644820 0.623200 0.614799 0.607180 0.590615 0.578751 0.57...........

Each row represents an image (instance) with the first column being image name, remaining 240 columns being the feature-vector of the image.
How can I load this file in MATLAB and store the image names into 'names' variable and the 240 values into a 'histogram' variable?

A: You might be able to do it with textscan and repmat so you can avoid conversion from strings:
Nfeatures = 240;
fid = fopen('text.txt');
format = ['%s ' repmat('%f', [1 Nfeatures])];
imageFeatureCell = textscan(fid, format, 'CollectOutput', true);
fclose(fid);

A test on a file with 7 rows:
>> fileData
fileData =
    {7x1 cell}    [7x240 double]

Move into your desired variables:
names = fileData{1};      % names{1} contains first file name, etc.
histogram = fileData{2};  % histogram(1,:) contains first file's features

A: str = textread('tmp.txt','%s');
str = reshape(str,241,[]).';
names = str(:,1); %'// cell array of strings with image names
histogram = str2double(str(:,2:end)); %// each row refers to an image


Q: How to display relation table in php? How to display relation table in php using oracle 11g with index format
this is my code:
$stid = oci_parse($conn, 'SELECT * FROM HOTEL');
oci_execute($stid);

while (($row = oci_fetch_array($stid, OCI_BOTH)) != false) {
    $id_hotel = $row['ID_HOTEL'];
    $nama = $row['NAMA_HOTEL'];
    $kamar = $row->KAMAR_ID['NAMA_KAMAR'];
    $harga = $row['HARGA'];
    $gambar = $row['GAMBAR'];
    $lat = $row['LAT'];
    $lng = $row['LNG'];

    echo ("addMarker($lat, $lng, '
$nama - $kamar
 Rp.$harga
Lihat Hotel
');\n");
}

oci_free_statement($stid);
oci_close($conn);


Q: Trouble scraping email information from a website I wish to get persmission to use the images on www.confluence.org for an art project but i need to do that by going to each individual member and asking for thier permission. There is about 10,000 people on the site.
I want to use BeautifulSoup to scrape the site for all the emails, so i can send one mass email instead of 1 email 10,000 times.
The problem however is the email is not written into the the html using text, but what i assume is a php file....located inside an img src.

<img src="visitormailimg.php?id=851" border="0" alt>

and an example of how this appears on the site is
I initially thought of the above email as a simple image that I could run through tesseract to get  the text version of the email, but tesseract doesn't recognize it as an image object. I can click and download the php file as an image manually, but i cant figure out how to do something similar when scraping
Then i saved the webpage onto a folder on my desktop and opened the saved php file visitormailimg.php. Except when opened it i get "the file is not displayed in the editor because it is either binary or uses unsupported text encode" in vscode.
So i am at this stage unsure of how to proceed scraping ww.confluence.org website. Is it an image problem...is it a php problem? Any suggestions?


Q: SQL Server get a JSON of all distinct columns grouped by a different column I have a table that looks like this:

What I want to do is convert it to a JSON that has all distinct years as keys, and an array of Make as value for each key. So based on the image, it would be something like this:
{ "1906": ["Reo", "Studebaker"], "1907": ["Auburn", "Cadillac", "Duryea", "Ford"], ... }
Could someone please help me on how to write a query to achieve this? I've never worked with converting SQL -> JSON before, and all I know is to use FOR JSON to convert a query to JSON.

A: I managed to solve this for whoever needs something similar. However, I think this way is inefficient and I would love for someone to tell me a better method.
I first ran this SQL query:
WITH CTE_Make AS
(
  SELECT [Year], [Make]
  FROM VehicleInformation
  GROUP BY [Year], [Make]
)
SELECT
  ( SELECT
  [Year] AS Year,
  STRING_AGG([Make],',') AS MakeList
  FOR JSON PATH,
  WITHOUT_ARRAY_WRAPPER)
FROM CTE_Make
GROUP BY [Year]

This gave me the JSON in a different format from what I wanted. I stored this in a file "VehicleInformationInit.json", and loaded it in JS as variable vehicleInfoInitJsonFile and ran the following code in JavaScript:
var dict = {};
$.getJSON(vehicleInfoInitJsonFile, function
    (result) {
    result.forEach(x => {
        var makeList = x.MakeList.split(',');
        dict[x.Year] = makeList;
    })

    var newJSON = JSON.stringify(dict);
});

I simply copied the contents of newJSON by attaching a debugger into my required file. However, you can easily use fs and store it in a file if you want to.


Q: Java Array of InnerClass throwing java.lang.NoSuchFieldError I am trying to brushup java after a long time.
Any help is much appreciated.
For demonstration I have Animal Class that has an array of innerclass of Organs.
public class Animal
   {
    String nameOfAnimal;
    Organs [] vitalOrgans = new Organs[3];

    public Animal()
    {
    }

    public String getNameOfAnimal() {
        return nameOfAnimal;
    }

    public void setNameOfAnimal(String nameOfAnimal) {
        this.nameOfAnimal = nameOfAnimal;
    }

    @Override
    public String toString() {
        return "Animal{" + "nameOfAnimal=" + nameOfAnimal + "}";
    }
class Organs{

String nameOfOrgan;
public String getNameOfOrgan() {
    return nameOfOrgan;
}
public void setNameOfOrgan(String nameOfOrgan) {
    this.nameOfOrgan = nameOfOrgan;
}

@Override
public String toString() {
    return "Organs{" + "nameOfOrgan=" + nameOfOrgan + '}';
}

}
}
Now in driver file when I make call there is no syntactical error but I get "Exception in thread "main" java.lang.NoSuchFieldError: vitalOrgans"
    Animal mamal = new Animal();
    mamal.setNameOfAnimal("Chimp");
    mamal.vitalOrgans[0].setNameOfOrgan("Heart");
    System.out.println(mamal.vitalOrgans[0].getNameOfOrgan());

What would be the way to make this (or similar idea) to work.
Thanks.

A: Taking the relevant bit of code:
public class Animal
{
    //...
    Organs [] vitalOrgans = new Organs[3];
    //...
}

Since your declaration of vitalOrgans was never given an access modifier (i.e. one of private, public, protected) it took on default access, which means only other classes in the same package can see it.  Since your other block of code is not in the same package, it cannot see the field.
A minimally viable modification to just make it work would be to set the access to public:
public class Animal
{
    //...
    public Organs [] vitalOrgans = new Organs[3];
    //...
}

While this works, it's not necessarily the best solution, as if you ever change how vitalOrgans is represented, or need to perform any validation, those edits would have to be done throughout the application.  Thus, a better solution (and also, a major stylistic convention in Java for those exact reasons) is to make it (and all your fields, in fact) private and access via methods:
public class Animal {
    private String nameOfAnimal;
    private Organs[] vitalOrgans = new Organs[3];

    //...

    public Organs[] getVitalOrgans() {
        return vitalOrgans;
    }

    //Alternative accessor that fetches only one organ.
    public Organs getVitalOrgan(int index) {
        if(index >= 0 && index < vitalOrgans.length)
            return vitalOrgans[index];
        else
            return null;
    }

    public void setVitalOrgans(Organs[] vitalOrgans) {
        this.vitalOrgans = vitalOrgans
    }

    //...
}

Your caller could then access Organs via either form of the get method (note, you probably want Organs to be public):
Animal.Organs futureMammalHeart = mamal.getVitalOrgan(0); //Animal.Organs due to Organs being an inner class.
if(futureMammalHeart != null) //Demonstration of null check.  Safety first!
    futureMammalHeart.setNameOfOrgan("Heart");
Animal.Organs[] mammalianVitalOrgans = mamal.getVitalOrgans();
if(mammalianVitalOrgans != null) //Just in case...
    System.out.println(mamal.mammalianVitalOrgans[0].getNameOfOrgan());

Also, as Ari mentioned in his answer, don't forget to initialize the organs in your array, otherwise you will get a NullPointerException!

A: You would need to initialize the vitalOrgrans with new Organs(). Like:
public Animal() {
    for (int i = 0; i < vitalOrgans.length; i++) {
        vitalOrgans[i] = new Organs();
    }
}

Because when you say :
Organs[] vitalOrgans = new Organs[3];

You are creating an array of 3 null Organs. Hence the null pointer exception, when accessing "vitalOrgans[i].".


Q: Mahout evaluation - unable to recommend in x cases I am trying to evaluate the recommender algorithms on my data set which probably is sparse, and I have only 341 items for around 20 000 users. I just want to evaluate all of the similarity algorithms. I tried almost all user-based recommendations, and for all of them I am getting this INFO from the evaluator, no matter which one, (AverageAbsoluteDifferenceRecommenderEvaluator or Root mean-square scoring Evaluator) which is Unable to recommend in xXXX cases.  However the final output still have some result. Here is the output of my evaluator:
3/06/17 14:11:35 INFO eval.AbstractDifferenceRecommenderEvaluator: Beginning evaluation using 0.7 of org.apache.mahout.cf.taste.impl.model.jdbc.PostgreSQLJDBCDataModel@44303e7b
13/06/17 14:15:17 INFO model.GenericDataModel: Processed 10000 users
13/06/17 14:15:17 INFO model.GenericDataModel: Processed 20000 users
13/06/17 14:15:17 INFO model.GenericDataModel: Processed 20530 users
13/06/17 14:15:17 INFO eval.AbstractDifferenceRecommenderEvaluator: Beginning evaluation of 11240 users
13/06/17 14:15:17 INFO eval.AbstractDifferenceRecommenderEvaluator: Starting timing of 11240 tasks in 4 threads
13/06/17 14:15:17 INFO eval.AbstractDifferenceRecommenderEvaluator: Average time per recommendation: 4ms
13/06/17 14:15:17 INFO eval.AbstractDifferenceRecommenderEvaluator: Approximate memory used: 57MB / 101MB
13/06/17 14:15:17 INFO eval.AbstractDifferenceRecommenderEvaluator: Unable to recommend in 3 cases
13/06/17 14:15:19 INFO eval.AbstractDifferenceRecommenderEvaluator: Average time per recommendation: 4ms
13/06/17 14:15:19 INFO eval.AbstractDifferenceRecommenderEvaluator: Approximate memory used: 48MB / 99MB
13/06/17 14:15:19 INFO eval.AbstractDifferenceRecommenderEvaluator: Unable to recommend in 882 cases
13/06/17 14:15:20 INFO eval.AbstractDifferenceRecommenderEvaluator: Average time per recommendation: 5ms
13/06/17 14:15:20 INFO eval.AbstractDifferenceRecommenderEvaluator: Approximate memory used: 33MB / 109MB
13/06/17 14:15:20 INFO eval.AbstractDifferenceRecommenderEvaluator: Unable to recommend in 1787 cases
13/06/17 14:15:22 INFO eval.AbstractDifferenceRecommenderEvaluator: Average time per recommendation: 5ms
13/06/17 14:15:22 INFO eval.AbstractDifferenceRecommenderEvaluator: Approximate memory used: 41MB / 86MB
13/06/17 14:15:22 INFO eval.AbstractDifferenceRecommenderEvaluator: Unable to recommend in 2687 cases
13/06/17 14:15:23 INFO eval.AbstractDifferenceRecommenderEvaluator: Average time per recommendation: 5ms
13/06/17 14:15:23 INFO eval.AbstractDifferenceRecommenderEvaluator: Approximate memory used: 38MB / 98MB
13/06/17 14:15:23 INFO eval.AbstractDifferenceRecommenderEvaluator: Unable to recommend in 3569 cases
13/06/17 14:15:24 INFO eval.AbstractDifferenceRecommenderEvaluator: Average time per recommendation: 5ms
13/06/17 14:15:24 INFO eval.AbstractDifferenceRecommenderEvaluator: Approximate memory used: 28MB / 93MB
13/06/17 14:15:24 INFO eval.AbstractDifferenceRecommenderEvaluator: Unable to recommend in 4465 cases
13/06/17 14:15:26 INFO eval.AbstractDifferenceRecommenderEvaluator: Average time per recommendation: 5ms
13/06/17 14:15:26 INFO eval.AbstractDifferenceRecommenderEvaluator: Approximate memory used: 41MB / 88MB
13/06/17 14:15:26 INFO eval.AbstractDifferenceRecommenderEvaluator: Unable to recommend in 5420 cases
13/06/17 14:15:27 INFO eval.AbstractDifferenceRecommenderEvaluator: Average time per recommendation: 5ms
13/06/17 14:15:27 INFO eval.AbstractDifferenceRecommenderEvaluator: Approximate memory used: 45MB / 90MB
13/06/17 14:15:27 INFO eval.AbstractDifferenceRecommenderEvaluator: Unable to recommend in 6317 cases
13/06/17 14:15:28 INFO eval.AbstractDifferenceRecommenderEvaluator: Average time per recommendation: 5ms
13/06/17 14:15:28 INFO eval.AbstractDifferenceRecommenderEvaluator: Approximate memory used: 46MB / 103MB
13/06/17 14:15:28 INFO eval.AbstractDifferenceRecommenderEvaluator: Unable to recommend in 7220 cases
13/06/17 14:15:30 INFO eval.AbstractDifferenceRecommenderEvaluator: Average time per recommendation: 5ms
13/06/17 14:15:30 INFO eval.AbstractDifferenceRecommenderEvaluator: Approximate memory used: 72MB / 102MB
13/06/17 14:15:30 INFO eval.AbstractDifferenceRecommenderEvaluator: Unable to recommend in 8145 cases
13/06/17 14:15:31 INFO eval.AbstractDifferenceRecommenderEvaluator: Average time per recommendation: 5ms
13/06/17 14:15:31 INFO eval.AbstractDifferenceRecommenderEvaluator: Approximate memory used: 67MB / 99MB
13/06/17 14:15:31 INFO eval.AbstractDifferenceRecommenderEvaluator: Unable to recommend in 9084 cases
13/06/17 14:15:33 INFO eval.AbstractDifferenceRecommenderEvaluator: Average time per recommendation: 5ms
13/06/17 14:15:33 INFO eval.AbstractDifferenceRecommenderEvaluator: Approximate memory used: 31MB / 83MB
13/06/17 14:15:33 INFO eval.AbstractDifferenceRecommenderEvaluator: Unable to recommend in 9982 cases
13/06/17 14:15:33 INFO eval.AbstractDifferenceRecommenderEvaluator: Evaluation result: 1.643042326271061

I don't understand the numbers, why they show so many times, and is this unable to recommend in xxx cases bigger then 20% of all my data? Does it mean that for one user it can't recommend in 3 cases, and for other in 9892?


Q: How to open Telegram desktop source in Qt? I'm trying to open the source of Telegram (desktop version) in Qt but I can't find any .pro file.


Q: Alloc - add as accessoryView - release: does it leak? Does this leak memory? This code is executed in cellForRowAtIndexPath: outside the cell creation block (so each time the table cell is updated).
MyView *myView = [[MyView alloc] init];
// ... configuration code
cell.accessoryView = myView;
[myView release];

Or in other words, will the UITableViewCell release the object in its accessoryView when a new object gets assigned to it?
Thanks.

A: Yes, the cell will release the accessory view and you do not have a leak in the example.

A: The property accessoryView of a UITableViewCell is a retain type, in common with many view properties in the kit. Check the Apple documentation for UITableViewCell to convince yourself of this. Therefore there will be no leak in your example - the retain count has been correctly managed. You've also correctly released after setting the accessory view, on account of your alloc call.


Q: How to solve TypeError: The DTypes  and  do not have a common DType? I have a dataframe which I want to plot using stacked chart for all columns
    date              company1           company2           company3
0   2015-09-30  0.1729652326951854  5.10825384154414    5.28662587089132
1   2015-12-31  0.33177760613144625 5.341402694012068   7.757158664745589
2   2016-03-31  0.45978954321258786 0.601947573082123   14.280848001613228
3   2016-06-30  0.08512062667254938 0.26628902842588686 9.342680735839917
4   2016-09-30  0.07052086314882283 10.032170981625246  8.964205526466738
5   2016-12-31  0.06143896111634454 10.06041088786925   8.3778390910586
6   2017-03-31  0.03742645731812014 7.156923204928792   6.793302965780993
7   2017-06-30  0.1032395546373315  5.438273795868809   4.857798532828831

I did the following
df['date'] = pd.to_datetime(df.date)
fig, ax = plt.subplots(figsize=(9,6), dpi= 80)
ax = exchanges_disagg_data.plot.area(legend=None, figsize=(9,6), alpha=0.9)

and i get the following error message.
TypeError: The DTypes <class 'numpy.dtype[uint8]'> and <class 'numpy.dtype[datetime64]'> do not have a common DType. For example they cannot be stored in a single array unless the dtype is `object`.

Then i tried
df.date= df['date'].dt.strftime('%Y-%m-%d')

and i get the plot but instead of date time x ticks, i just get index numbers...
This is very frustrating, would anyone know how to solve this? Many thanks in advance.

A: Note: np.nan is float type and pd.NaT is of datetime null type. Problem with your code is that null values have been filled with np.nan
I got the same error while doing the following thing....
df['date'] = np.where((df['date2'].notnull()) & (df['date3'].notnull()),df['date2']-df['date3'],np.nan)

problem here is date difference of date2 and date3 is of datetime type but the type of "np.nan" is float/int. For saving it to df['date'], datatype should be same. In datetime type the null date is "pd.NaT". So when I replace the above code with below. It worked for me. You can try the same..
df['date'] = np.where((df['date2'].notnull()) & (df['date3'].notnull()),df['date2']-df['date3'],pd.NaT)

So you can replace the nulls present in your data with pd.NaT instead of np.nan
you can use below thing as well...
df['date'].replace(np.NaN, pd.NaT)

or
df['date'].fillna(pd.NaT)

Hope it is helpful for you :)