text:
Q: Advice on improving simulation efficiency while reducing variance in Python I use 538's Riddler to practice code. Wrote a simple simulation in Python but had to add another nested loop to get the mean of means in order to reduce output variance. Tried to run it, but after 45 minutes I stopped it, thinking there must be a way to improve the efficiency of the code.
For context the problem is: You have a radio and play 100 songs per day. How big must the playlist be for the probability of playing the same song to be equal to 50%.
My approach is to increment the size of the playlist (starting at 7000) with 1 until the grand mean of the mean probability of a re-play is equal to 50%, using 1000 for both sample sizes and number of samples.
The code is:
import random

playlist = 7000
chance_of_replay = []
sample = 1000
mean_chance_of_replay = 0
replays = 0
temp_sum = 0

while mean_chance_of_replay > 0.5 or mean_chance_of_replay == 0.0:

    playlist += 1

    for j in range(0, sample):

        for i in range(1, sample + 1):

            songs_to_play = 100
            songs_played = []

            while songs_to_play > 0:

                song_pick = random.randint(1, playlist + 1)

                if song_pick not in songs_played:
                    songs_played.append(song_pick)
                    songs_to_play -= 1
                else:
                    replays += 1
                    break

        chance_of_replay.insert(j, (replays / sample))
        replays = 0

    for element in chance_of_replay:
        temp_sum = temp_sum + element

    mean_chance_of_replay = temp_sum/sample

print(playlist)

A: Before looking at performance issues in your code, there is a bigger problem to be solved first: the code is in an infinite loop.
The list chance_of_replay is never cleared and the variable temp_sum is never set to 0.  Because of that, the variable mean_chance_of_replay is always increasing and your code will run forever.
After fixing those two logical errors, you should start worrying about performance optimization.

A: The chance of playing two of the same song decreases as the playlist size goes up.
Have you considered that the chances of the same song being played at a playlist size 7000 is actually below 50%? If that is the case, then checking any higher values will just lead to smaller percentages, therefore you will never find your answer.
If you want to do simulation (as opposed to the pure-mathematical approach), the main optimisation I can find is that the array inserts and appends are quite a performance killer when done many times. What I did was to create an array of Boolean values which store the state of if any of the given songs have been played. It is much easier to check if a given song has been played, and doesn't require any insertions which make new arrays behind the scenes.
Here's the code:
from random import randint

playlist_size = 1
samples = 1000

songs_per_sample = 100

simulation_running = True
while simulation_running:
    replays = 0

    for _ in range(samples):
        songs_played = [False] * playlist_size

        for song_sample in range(songs_per_sample):
            song_to_play_index = randint(0, playlist_size - 1)

            if songs_played[song_to_play_index]:
                replays += 1

            songs_played[song_to_play_index] = True

    replay_chance = replays / (samples * songs_per_sample)

    if replay_chance <= 0.5:
        break

    playlist_size += 1

print(playlist_size)

Running this actually gives a surprising answer, which is way below 7000!


text:
Q: OAuth2 - Create Cllient to call API I'm currently tring to do some calls to Linkedin API (or other API of course).
I'm using OAuth2 and Linkedin strategy gems. The link http://my_app/auth/linkedin works, and I have registered in database the information of access_token.
Now I try to ask the LinkedinApi to get some informations. The Linkedin gem has lines linke this (gems/omniauth-linkedin-oauth2-0.1.5/lib/omniauth/strategies/linkedin.rb) :
access_token.get("/v1/people/~:(#{options.fields.join(',')})?format=json").parsed

with access_token defined by
def access_token
  ::OAuth2::AccessToken.new(client, oauth2_access_token.token, {
    :mode => :query,
    :param_name => 'oauth2_access_token',
    :expires_in => oauth2_access_token.expires_in,
    :expires_at => oauth2_access_token.expires_at
  })
end

My question is : how to create a client object, and a oauth2_access_token ?
A client can be created with OmniAuth::Strategy::Linkedin.new(nil).client, but I'm not sure this is the right method.
There is a lot of functions in OAuth gems, and I'm lost ! How to create simple call to API ?

A: If you're using OAuth, you always need a token (not just a client) to make authorized requests -- the token constitutes your authorization. The difference is whether you need authorization from a user or not. If you do, then you need to involve the user to get the token (using either the Authorization Code or Implicit Grant flows), and after you have the token you can use it to make authorized requests for protected resources that the user has granted you authorization for. If not, then you can get a token using only your application's credentials, using the Client Credentials OAuth flow. After that, you can use that token to access protected resources that your client has access to on its own. But either way, you need a token before you can make API requests.
But to get a Client Credentials token, OmniAuth is the wrong tool. Omniauth describes itself as "a library that standardizes multi-provider authentication for web applications." You're talking about making authorized requests for protected resources using OAuth2. These things are not entirely unrelated, because most Omniauth strategies use OAuth2, since the OpenID Connect standard is built on top of OAuth2. But OAuth is an authorization protocol, not authentication. OmniAuth uses it, but it's not designed for making just any request for protected resources -- it's designed solely for authenticating your application's users.
What you want is the OAuth2 gem. If you use OmniAuth, you've already got this gem -- it's a dependency. But it has the API you're looking for, not OmniAuth.

redpajama_set_name: RedPajamaStackExchange


text:
Q: image not get delete from folder my .cs code
   int ID = 24;
   DAL objdal = new DAL();  //class
   DataTable dt = new DataTable();
   dt = objdal.select_delete_image(ID);  //method to select and delete
   string imagedel = "";
  if (dt.Rows.Count > 0)
 {
     //image delete
     imagedel = dt.Rows[0]["image_url"].ToString();
     File.Delete((Server.MapPath("~/Admin/images/projects/small/" + imagedel.ToString())));       // process to delete
     File.Delete((Server.MapPath("~/Admin/images/projects/large/" + imagedel.ToString())));        // process to delete

  }
     string strScript = "<script language='JavaScript'>alert('Image Deleted Successfully')</script>";
     Page.RegisterStartupScript("PopUp", strScript);

here the database entry is deleted, but the image from the folder is not deleted.
This all works fine in localhost but problem occurs on server side

A: If this code deleted data from Database dt = objdal.select_delete_image(ID); then how  this statement works (dt.Rows.Count > 0), Cross Check whether dt contains any data or not. IF it having data, then cross your file path you are providing to delete image. May be your File Path is not correct well.


text:
Q: Python Persistent number need some assistance to write a Persistent number program in Python to determine the smallest non-negative integer with a persistence
(715 ---> 35 ---> 15 ---> 5) of 3, then 4, then 5, then 6, then 7. With an output with the following format:
The smallest integer with a persistence of 3 is: 39
The smallest integer with a persistence of 4 is: xx
The smallest integer with a persistence of 5 is: xxx
The smallest integer with a persistence of 6 is: xxxx
The smallest integer with a persistence of 7 is: xxxxx

Any code links or leads would be appreciated, thanks.

A: def main():

    low_limit = 3
    high_limit = 7

    for limit in range(low_limit, high_limit+1):
        number = 1
        while persistence(number) < limit:
            number += 1
            print('The smallest integer with a persistence of', limit, 'is:', number)

def persistence(x):
    pcount = 0    # counting the p from 0
    while x>=10:
        y=x       # temp copy of x
        z=1       # z holds the value of y as being broken down and multplied
        while (y!=0):
            z=z*(y%10)     # stripping last number and mult by last z value
            y=y//10       # integer division, dropping off the last digit
        x=z
        pcount +=1
    return pcount

main()


text:
Q: Communication between running Fragment and Activity We are using Retrofit and Activeandroid for my project.
Currently we are facing an issue.
The pattern which we follow in our project is, We get data from server and save it into local database and after data saved we call routine which fetches data from database and populate UI this all happens in single Activity..
Now we have an activity which makes 3 server request, and due to which the amount of code in activity increased.
We are trying to reduce Activity code by creating fragment for the activity and giving responsibility of fetching data and displaying data to Fragment. Rest call will be made by activity. Now once the data is loaded from all the 3 request we need to inform fragment about data is loaded, what is the best way for this.
And is it even possible to send data to fragment once it is loaded.. or the approach we are following is not correct..
Please guide us on this..
Edit1
I read about EventBus. Can event bus solve this problem or it will effect the efficiency.

A: If you are storing those Fragment instances in your Activity then it will be much easier.
1) Create loadNewdata(DataType data) method in your Fragment.
2) Pass the data into the Fragment after getting response from server in your Activity
((YourFragment)fragment).loadNewdata(yourData);

A: I would suggest to use Otto.
By this you do not need to store your data in local db(if you don't want it).Just simply post your responce whenever you received from rest API. As Otto also provides the functionality of sharing custom objects between fragments/Activity. It also helps you to make your code modular.
You will find a working example here & here.

A: You can use Interfaces for this and send data to activity.when response come back pass reference of to show which fragment data needs to be populate the UI.


text:
Q: Query takes too much time for execution I am using Oracle 10g. The following query takes too much time:
  SELECT invno, invdate, ccode
    FROM acct
   WHERE     invno IS NOT NULL
         AND invdate > '01-Feb-2018'
         AND invno LIKE '%' || :ppassingmn.dept || '%'
         AND invno NOT IN (SELECT DISTINCT (vdescript)
                             FROM genldgr
                            WHERE     vchrno LIKE 'IV%'
                                  AND trandate > '01-Feb-2018'
                                  AND vdescript LIKE
                                         '%' || :ppassingmn.dept || '%')
ORDER BY SUBSTR (invno, 12, 15);

Please optimize it.

A: You didn't provide much info. Query itself is pretty much useless (as if you called the mechanic and said "my blue car is slow. Why?").
Anyway, a few hints; maybe they'll help.

*

*'01-Feb-2018' is a string. If INVDATE is a DATE datatype column, you're forcing Oracle to perform implicit conversion between VARCHAR2 and DATE. Use a date literal instead, e.g. and invdate > date '2018-02-01'. The same goes for TRANDATE.

*LIKE is slow. You use it twice. See if you can rewrite it to invno = :passingmn.dept. By the way, what is :passingmn.dept? Looks like a variable, but - what is that dot doing here? The same goes for VDESCRIPT.

*DISTINCT requires you to select all rows, and then remove duplicates. It seems that you don't really care about it, so - remove DISTINCT.

*As INVNO has to look like some parameter value, then it isn't NULL so you can probably remove invno is not null condition.

*ORDER BY also affects performance. Moreover, it selects a substring. Do you need it? If not, remove it

*are columns involved in the WHERE clause indexed? If not, do so

Finally, explain plan would help. Without it, people are partially blind.


text:
Q: Join two list comparing their elements properties public class Person()
    {
    int ID;
    string Name;
    DateTime ChangeDate
    }

var list1 = new List<Person>
    {
        new Person { ID= 1, Name = "Peter", ChangeDate= "2011-10-21" },
        new Person { ID= 2, Name = "John",  ChangeDate= "2011-10-22" },
        new Person { ID= 3, Name = "Mike",  ChangeDate= "2011-10-23" },
        new Person { ID= 4, Name = "Dave",  ChangeDate= "2011-10-24" }
    };

 var list2 = new List<Person>
    {
        new Person { ID= 1, Name = "Pete",  ChangeDate= "2011-10-21" },
        new Person { ID= 2, Name = "Johny", ChangeDate= "2011-10-20" },
        new Person { ID= 3, Name = "Mikey", ChangeDate= "2011-10-24" },
        new Person { ID= 5, Name = "Larry", ChangeDate= "2011-10-27" }
    };

As output I would like to have list1 + list2 =
    Person { ID= 1, Name = "Peter", ChangeDate= "2011-10-21" },
    Person { ID= 2, Name = "John",  ChangeDate= "2011-10-22" },
    Person { ID= 3, Name = "Mikey", ChangeDate= "2011-10-24" },
    Person { ID= 4, Name = "Dave",  ChangeDate= "2011-10-24" }
    Person { ID= 5, Name = "Larry", ChangeDate= "2011-10-27" }

And the Algorithm is like this.
Join two list. If elements of lists have same ID, compare them by ChangeDate and take the ond with bigger date. If ChangeDate are equeal take any of them but not both.
Maybe its easier to concat both lists and than to filter them with lambda. I tried, but always came out with some ugly code :/
Anyone have any idea?

A: LINQ
var q = from person in list1.Concat(list2)
        group person by person.ID into g
        select g.OrderByDescending(p => p.ChangeDate).First();

A: Merge both lists sorting on descending date.  Now you need to take the first occurence of each id in your sorted list.

A: How about something like this?
using System;
using System.Collections.Generic;
using System.Linq;

public class Person
{
    public int ID;
    public string Name;
    public DateTime ChangeDate;
}

public class PersonComparer : IEqualityComparer<Person>
{
    public bool Equals(Person p1, Person p2)
    {
        return p1.ID == p2.ID;
    }

    public int GetHashCode(Person p)
    {
        return p.ID.GetHashCode();
    }
}

class Program
{
    static void Main(string[] args)
    {
        var list1 = new List<Person>
        {
            new Person { ID = 1, Name = "Peter", ChangeDate = DateTime.Parse("2011-10-21") },
            new Person { ID = 2, Name = "John",  ChangeDate = DateTime.Parse("2011-10-22") },
            new Person { ID = 3, Name = "Mike",  ChangeDate = DateTime.Parse("2011-10-23") },
            new Person { ID = 4, Name = "Dave",  ChangeDate = DateTime.Parse("2011-10-24") }
        };

        var list2 = new List<Person>
        {
            new Person { ID = 1, Name = "Pete",  ChangeDate = DateTime.Parse("2011-10-21") },
            new Person { ID = 2, Name = "Johny", ChangeDate = DateTime.Parse("2011-10-20") },
            new Person { ID = 3, Name = "Mikey", ChangeDate = DateTime.Parse("2011-10-24") },
            new Person { ID = 5, Name = "Larry", ChangeDate = DateTime.Parse("2011-10-27") }
        };

        var pc = new PersonComparer();
        var combined = list1.Join(list2, p => p.ID, p => p.ID, (p1,p2) => p2.ChangeDate > p1.ChangeDate ? p2 : p1)
                            .Union(list1.Except(list2, pc))
                            .Union(list2.Except(list1, pc));

        foreach(var p in combined)
        {
            Console.WriteLine(p.ID + " " + p.Name + " " + p.ChangeDate);
        }
    }
}

A: You could concatenate and sort them, then get distinct values:
class PersonIdEqualityComparer : IEqualityComparer<Person>
{
    public bool Equals(Person x, Person y)
    {
        return x.ID == y.ID;
    }

    public int GetHashCode(Person person)
    {
        return person.ID;
    }
}

var result = list1.Concat(list2)
    .OrderByDescending(i => DateTime.Parse(i.ChangeDate)) // Most recent first
    .Distinct(new PersonIdEqualityComparer())
    ;

This assumes that Distinct will take the first item encountered out of each set rather than an arbitrary item.  Considering it probably just inserts them into a HashSet while traversing the collection, this seems reasonable to me.
It also does no error checking.  If any values could be null, or ChangeDate could be invalid, then this code will throw exceptions.  If this could be a problem I suggest you check your data before passing it in, and that you do error checking in the PersonIdEqualityComparer class.


text:
Q: How to find and extract the time from a multiline string in HH:MM format I am trying to find a solution to extract the time given in a dynamic string using regex object using JavaScript.
For example - Assume the string to be "Please provide access to the engineer: last name: XYZ first name: ABC date: 02nd November 2021 time window: 11:00AM - 07:00PM CET". I need to extract the time i.e 11:00AM - 07:00PM CET from this.
I tried using the regex expression for time -
/^(0?[1-9]|1[0-2]):([0-5]\d)\s?((?:A|P)\.?M\.?)$/i

and few more other expressions. However whenever I test the returned value is false.
What is the approach to solve this ?


text:
Q: How do I improve performance of DataSet.ReadXml if I'm using a schema? I'm have a ADO DataSet that I'm loading from its XML file via ReadXml.  The data and the schema are in separate files.
Right now, it takes close to 13 seconds to load this DataSet.  I can cut this to 700 milliseconds if I don't read the DataSet's schema and just let ReadXml infer the schema, but then the resulting DataSet doesn't contain any constraints.
I've tried doing this:
Console.WriteLine("Reading dataset with external schema.");
ds.ReadXmlSchema(xsdPath);
Console.WriteLine("Reading the schema took {0} milliseconds.", sw.ElapsedMilliseconds);
foreach (DataTable dt in ds.Tables)
{
   dt.BeginLoadData();
}
ds.ReadXml(xmlPath);
Console.WriteLine("ReadXml completed after {0} milliseconds.", sw.ElapsedMilliseconds);
foreach (DataTable dt in ds.Tables)
{
   dt.EndLoadData();
}
Console.WriteLine("Process complete at {0} milliseconds.", sw.ElapsedMilliseconds);

When I do this, reading the schema takes 27ms, and reading the DataSet takes 12000+ milliseconds.  And that's the time reported before I call EndLoadData on all the DataTables.
This is not an enormous amount of data - it's about 1.5mb, there are no nested relations, and all of the tables contain two or three columns of 6-30 characters.  The only thing I can figure that's different if I read the schema up front is that the schema includes all of the unique constraints.  But BeginLoadData is supposed to turn constraints off (as well as change notification, etc.).  So that shouldn't apply here.  (And yes, I've tried just setting EnforceConstraints to false.)
I've read many reports of people improving the load time of DataSets by reading the schema first instead of having the object infer the schema.  In my case, inferring the schema makes for a process that's about 20 times faster than having the schema provided explicitly.
This is making me a little crazy.  This DataSet's schema is generated off of metainformation, and I'm tempted to write a method that creates it programatically and just deseralizes it with an XmlReader.  But I'd much prefer not to.
What am I missing?  What else can I do to improve the speed here?

A: I will try to give you a performance comparison between storing data in text plain files and xml files.
The first function creates two files: one file with 1000000 records in plain text and one file with 1000000 (same data) records in xml. First you have to notice the difference in file size: ~64MB(plain text) vs ~102MB (xml file).
void create_files()
    {
        //create text file with data
        StreamWriter sr = new StreamWriter("plain_text.txt");

        for(int i=0;i<1000000;i++)
        {
            sr.WriteLine(i.ToString() + "<SEP>" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbb" + i.ToString());
        }

        sr.Flush();
        sr.Close();

        //create xml file with data
        DataSet ds = new DataSet("DS1");

        DataTable dt = new DataTable("T1");

        DataColumn c1 = new DataColumn("c1", typeof(int));
        DataColumn c2 = new DataColumn("c2", typeof(string));

        dt.Columns.Add(c1);
        dt.Columns.Add(c2);

        ds.Tables.Add(dt);

        DataRow dr;

        for(int j=0; j< 1000000; j++)
        {
            dr = dt.NewRow();
            dr[0]=j;
            dr[1] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbb" + j.ToString();
            dt.Rows.Add(dr);
        }

        ds.WriteXml("xml_text.xml");

    }

The second function reads these two files: first it reads the plain text into a dictionary (just to simulate the real world of using it) and after that it reads the XML file. Both steps are measured in milliseconds (and results are written to console):
Start read Text file into memory
Text file loaded into memory in 7628 milliseconds
Start read XML file into memory
XML file loaded into memory in 21018 milliseconds
void read_files()
    {

        //timers
        Stopwatch stw = new Stopwatch();
        long milliseconds;

        //read text file in a dictionary

        Debug.WriteLine("Start read Text file into memory");

        stw.Start();
        milliseconds = 0;

        StreamReader sr = new StreamReader("plain_text.txt");
        Dictionary<int, string> dict = new Dictionary<int, string>(1000000);
        string line;
        string[] sep = new string[]{"<SEP>"};
        string [] arValues;
        while (sr.EndOfStream!=true)
        {
            line = sr.ReadLine();
            arValues = line.Split(sep,StringSplitOptions.None);
            dict.Add(Convert.ToInt32(arValues[0]),arValues[1]);
        }

        stw.Stop();
        milliseconds = stw.ElapsedMilliseconds;

        Debug.WriteLine("Text file loaded into memory in " + milliseconds.ToString() + " milliseconds" );

        //create xml structure
        DataSet ds = new DataSet("DS1");

        DataTable dt = new DataTable("T1");

        DataColumn c1 = new DataColumn("c1", typeof(int));
        DataColumn c2 = new DataColumn("c2", typeof(string));

        dt.Columns.Add(c1);
        dt.Columns.Add(c2);

        ds.Tables.Add(dt);

        //read xml file

        Debug.WriteLine("Start read XML file into memory");

        stw.Restart();
        milliseconds = 0;

        ds.ReadXml("xml_text.xml");

        stw.Stop();
        milliseconds = stw.ElapsedMilliseconds;

        Debug.WriteLine("XML file loaded into memory in " + milliseconds.ToString() + " milliseconds");

    }

Conclusion: the XML file size is almost double than the text file size and is loaded three times slower than the text file.
XML handling is more convenient (because of the abstraction level) than plain text but it is more CPU/disk consuming.
So, if you have small files and is acceptable from the performance point of view, XML data Sets are more than ok. But, if you need performance, I don't know if XML Data set ( with any kind of method available) is faster that plain text files. And basically, it start from the very first reason: XML file is bigger because it has more tags.

A: It's not an answer, exactly (though it's better than nothing, which is what I've gotten so far), but after a long time struggling with this problem I discovered that it's completely absent when my program's not running inside Visual Studio.
Something I didn't mention before, which makes this even more mystifying, is that when I loaded a different (but comparably large) XML document into the DataSet, the program performed just fine.  I'm now wondering if one of my DataSets has some kind of metainformation attached to it that Visual Studio is checking at runtime while the other one doesn't.  I dunno.

A: Another dimesion to try is to read the dataset without the schema and then Merge it into a typed dataset that has the constraints enabled.  That way it has all of the data on hand as it builds the indexes used to enforce constraints -- maybe it would be more efficient?
From MSDN:

The Merge method is typically called
  at the end of a series of procedures
  that involve validating changes,
  reconciling errors, updating the data
  source with the changes, and finally
  refreshing the existing DataSet

.


text:
Q: How can I solve the output error in flipping a coin? I am writing code about the probability of flipping a coin as follows, but I keep getting errors.
import random

tries = 0
number = random.randint(0, 1)
def coin_probability(hehe):
    for a in range(1, 101):
        heads = 0; tails = 0
        tries += 1
        if tries <= 10:
            if number == 0:
                heads += 1
            cp = heads/n
            print("%4d 번째까지 던지기에서 앞면이 나온 확률 :%5d%%" %(tries, cp))
        else:
            if number == 0:
                heads += 1
            cp = heads/n
            print("%4d 번째까지 던지기에서 앞면이 나온 확률 :%5d%%" %(tries, cp))
n = int(input("동전 던지기 시도 횟수를 입력(1 - 100) : "))
print('*' * 47)
print('총 %d번 동전 던지기에서 앞면이 나올 확률 :  %s' %(n, coin_probability(n))) `

I want the probability to come out one by one from 1 to 10, and display 10 units from 11 to 100. For example:
1 번째까지~: (probability)%
2 번째까지~: (probability)%
3 번째까지~: (probability)%
...
10 번째까지~: (probability)%
20 번째까지~: (probability)%
...

it should be printed like this.
(※I set heads to 0 and tails to 1.)

A: I hope that's what you want:
import random

def coin_probability(attempts):
    heads = 0
    for try_number in range(1, attempts + 1):
        number = random.randint(0, 1)

        if number == 0:
            heads += 1

        cp = (heads/try_number) * 100

        if try_number <= 10 or (try_number % 10) == 0:
            print(f"{try_number} 번째까지 던지기에서 앞면이 나온 확률: {round(cp,5)}")

def main():
    attempts = int(input("동전 던지기 시도 횟수를 입력: "))
    print('*' * 47)
    coin_probability(attempts)

if __name__ == "__main__":
    main()

A: I believe this is what you're looking for.
import random

def coin_probability(n):
    tries = 0
    heads = 0
    for a in range(n):
        number = random.randint(0, 1)
        tries += 1
        if tries <= 10:
            if number == 0:
                heads += 1
            cp = 100*heads/tries
            print("%4d 번째까지 던지기에서 앞면이 나온 확률 :%5f%%" %(tries, cp))
        else:
            if number == 0:
                heads += 1
            cp = 100*heads/tries
            if tries%10 == 0:
               print("%4d 번째까지 던지기에서 앞면이 나온 확률 :%5f%%" %(tries, cp))
    return cp
n = int(input("동전 던지기 시도 횟수를 입력(1 - 100) : "))
print('*' * 47)
p = coin_probability(n)
print('*' * 47)
print('총 %d번 동전 던지기에서 앞면이 나올 확률 :  %s' %(n, p))

A: You could handle the print() after another if with
...
else:
    if tries % 10 == 0:
        print("something", cp, tries)

also you give n to your function as "hehe" but never use it? Other than that you could be a little more specific what kind of error occurs.


text:
Q: Ability to access cookies "cleanly" in test environment How would I go about testing my app for cookies which have been set during a request in a way that's pythonic? The way I currently do it (which works) doesn't feel very convenient, but here it is:
def test_mycookie(app, client):
    def getcookie(name):
        try:
            cookie = client.cookie_jar._cookies['mydomain.dev']['/'][name]
        except KeyError:
            return None
        else:
            return cookie

    with app.test_request_context():
        client.get('/non-existing-path/')
        assert getcookie('mycookie') is None
        client.get('/')
        assert getcookie('mycookie').value == '"xyz"'

Using flask.request.cookies doesn't work for me as it always returns an empty dict. Maybe I'm doing it wrong?
def test_mycookie2(app, client):

    with app.test_request_context():
        client.get('/non-existing-path/')
        assert 'mycookie' not in request.cookies
        client.get('/')
        request.cookies['mycookie']  # Raises KeyError

A: How about this one? use app.test_client will let us keep the context little longer.
with app.test_client() as tc:
    tc.get('/non-existing-path/')
    assert 'mycookie' not in request.cookies
    tc.get('/')
    print request.cookies['mycookie']

also, please give us a minimal example that can works so that we can reproduce this problem.


text:
Q: Timestamp of socket in /proc//fd If I list /proc/<pid>/fd I see a number of entries for sockets.  These entries have timestamps.  At first I thought they were when the socket was created.  But it doesn't always appear to be the case.
What does this timestamp mean?

A: It looks like the entries in /proc/x/fd are instantiated the first time you access them (via a lstat(2) or any system call that has them involved), and that's where the time comes from.


text:
Q: How can I have 2 Widgets in a column, where 1 of them is a scrollable Column? So I have two widgets in a column, one is a Container (top widget) and the other is a Column (bottom widget that should be scrollable), but the Column isn't being viewed as scrollable as shown in the image below:

Here's the Container and Column in code:
<Widget>[
    topBar, // Container
    Container(
      color: Colors.transparent,
      child: SingleChildScrollView(
        child: Column( // Column in container
          children: <Widget>[
            Text(
              "Test",
              style: TextStyle(fontSize: 50),
            ),
            Text(
              "Test",
              style: TextStyle(fontSize: 50),
            ),
            Text(
              "Test",
              style: TextStyle(fontSize: 50),
            ),
            // Insert Other Text Widgets below
          ],
        ),
      ),
    )
  ]

Where topBar is:
Container(
    margin: EdgeInsets.all(0),
    padding: EdgeInsets.fromLTRB(20, 0, 20, 0),
    child: Text(
      'Top Container',
      style: TextStyle(
          color: Colors.white,
          fontWeight: FontWeight.bold,
          fontSize: 25.0
      ),
    ),
);

I've tried using ListView as mentioned in similar questions, but it does the same thing.
I suspect that it's because it's trying to go beyond the parent container, which isn't scrollable but I don't know how to get past that.

UPDATE
Just to clarify, what I'm trying to do is have a ScrollView with a fixed top bar, I don't want the bar to scroll too.
On Android it's just a case of creating two layouts, one with a ScrollView inside. But I'm guessing Flutter does things differently, and I can't seem to wrap my head around it.

A: If you wrap your second Column in an Expanded it will try to occupy as much space as possible and then the overflowing elements in that Column will scroll. This should fix your problem. Example:
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: HomePage());
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    Widget topBar = Container(
      margin: EdgeInsets.all(0),
      padding: EdgeInsets.fromLTRB(20, 0, 20, 0),
      child: Text(
        'Top Container',
        style: TextStyle(
            color: Colors.white, fontWeight: FontWeight.bold, fontSize: 25.0),
      ),
    );
    return Scaffold(
      body: Column(
        children: <Widget>[
          topBar, // Container

          Expanded( // <- Add this

            child: Container(
              color: Colors.transparent,
              child: SingleChildScrollView(
                child: Column(
                  // Column in container
                  children: <Widget>[
                    for (int i = 0; i < 30; i++)
                      Text(
                        "Test",
                        style: TextStyle(fontSize: 50),
                      ),
                  ],
                ),
              ),
            ),
          )
        ],
      ),
    );
  }
}


text:
Q: How to find Projection matrix onto the subspace I need help finding the projection of a matrix onto the subspace $W \subset \mathbb{R}^3$ given by the equation $x+y+z=0$
I am not sure what to start with to answer this question. Do I use projection formula?
Many thanks in advance.

A: HINT
1) Method 1

*

*consider two linearly independent vectors $v_1$ and $v_2$ $\in$ plane

*consider the matrix $A=[v_1\quad v_2]$

*the projection matrix is $P=A(A^TA)^{-1}A^T$

2) Method 2 - more instructive
Ways to find the orthogonal projection matrix

A: Write the defining equation of $W$ in matrix form $$\begin{bmatrix}1&1&1\end{bmatrix}\begin{bmatrix}x\\y\\z\end{bmatrix} = 0,$$ from which you should see that $W$ is the null space of the matrix on the left, that is, the orthogonal complement of the span of $(1,1,1)^T$. The orthogonal projection of a vector $v$ onto $W$ is then whatever's left over after subtracting its projection onto $(1,1,1)^T$—i.e., the orthogonal rejection of $v$ from $(1,1,1)^T$. Therefore, the matrix of orthogonal projection onto $W$ is $I_3-P$, where $P$ is the matrix for projection onto $(1,1,1)^T$, which I'm assuming that you can compute using the projection formula that you mentioned.


text:
Q: Oracle Coherence read-through configuration The part of my Coherence tangosol-coherence-override.xml  configuration doesn't work.
I can start up the server, put and get data, but I can't get data via CacheLoader.
Thanks.
Stub for CacheLoader
package com.example;

import com.tangosol.net.cache.AbstractCacheLoader;
import java.util.HashMap;

public class DataLoader extends AbstractCacheLoader {

    private HashMap<String, String> hashMap;

    public DataLoader() {
        hashMap = new HashMap<String, String>();
        init();
    }

    private void init()  {
        hashMap.put("A", "a");
        hashMap.put("B", "b");
        hashMap.put("C", "c");
        hashMap.put("D", "d");
        hashMap.put("E", "f");
        hashMap.put("F", "g");
    }

    public Object load(Object key) {
        return hashMap.get(key);
    }
}

tangosol-coherence-override.xml
<coherence>
    <cluster-config>
        <member-identity>
            <cluster-name>thecluster</cluster-name>
        </member-identity>

        <multicast-listener>
            <!--<address>224.3.6.0</address>-->
            <port>9100</port>
            <time-to-live>0</time-to-live>
        </multicast-listener>

        <caching-scheme-mapping>
            <cache-mapping>
                <cache-name>ABC</cache-name>
                <scheme-name>distributed-rxwbm</scheme-name>
            </cache-mapping>
        </caching-scheme-mapping>
    </cluster-config>

    <caching-schemes>
        <distributed-scheme>
            <scheme-name>distributed-rwbm</scheme-name>
            <backing-map-scheme>
                <read-write-backing-map-scheme>

                    <internal-cache-scheme>
                        <local-scheme/>
                    </internal-cache-scheme>

                    <cachestore-scheme>
                        <class-scheme>
                            <class-name>com.example.DataLoader</class-name>
                            <init-params>
                                <init-param>
                                    <param-type>java.lang.String</param-type>
                                    <param-value>{cache-name}</param-value>
                                </init-param>
                            </init-params>
                        </class-scheme>
                    </cachestore-scheme>
                </read-write-backing-map-scheme>
            </backing-map-scheme>
        </distributed-scheme>
    </caching-schemes>
</coherence>

Starting server
package com.example;

import com.tangosol.net.DefaultCacheServer;
import com.tangosol.net.DefaultConfigurableCacheFactory;

public class HelloWorld {

    public static void main(String[] args) {

        DefaultConfigurableCacheFactory factory;
        factory = new DefaultConfigurableCacheFactory();

        DefaultCacheServer dcs = new DefaultCacheServer(factory);
        dcs.startAndMonitor(5000);
    }
}

Reading data
package com.example;

import com.tangosol.net.CacheFactory;
import com.tangosol.net.NamedCache;

public class Reader {
    public static void main(String[] args) {
        CacheFactory.ensureCluster();
        NamedCache cache = CacheFactory.getCache("ABC");

        System.out.println("Value in cache is: " + cache.get("A"));
        System.out.println("Value in cache is: " + cache.get("B"));
        System.out.println("Value in cache is: " + cache.get("C"));
        System.out.println("Value in cache is: " + cache.get("D"));
    }
}

A: *

*Your configuration is wrong. You can't merge operational configuration and cache configuration this way (at least not without tricks). You should split it into two files: tangosol-coherence-override.xml and coherence-cache-config.xml like this:
tangosol-coherence-override.xml:
<?xml version="1.0" encoding="UTF-8"?>
<coherence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.oracle.com/coherence/coherence-operational-config"
    xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-operational-config coherence-operational-config.xsd">
    <cluster-config>
        <member-identity>
            <cluster-name>thecluster</cluster-name>
        </member-identity>
        <multicast-listener>
            <!--<address>224.3.6.0</address>-->
            <port>9100</port>
            <time-to-live>0</time-to-live>
        </multicast-listener>
    </cluster-config>
</coherence>

coherence-cache-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<cache-config
    xmlns="http://xmlns.oracle.com/coherence/coherence-cache-config"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-cache-config coherence-cache-config.xsd">
    <caching-scheme-mapping>
        <cache-mapping>
            <cache-name>ABC</cache-name>
            <scheme-name>distributed-rxwbm</scheme-name>
        </cache-mapping>
    </caching-scheme-mapping>
    <caching-schemes>
        <distributed-scheme>
            <scheme-name>distributed-rxwbm</scheme-name>
            <backing-map-scheme>
                <read-write-backing-map-scheme>
                    <internal-cache-scheme>
                        <local-scheme/>
                    </internal-cache-scheme>
                    <cachestore-scheme>
                        <class-scheme>
                            <class-name>com.example.DataLoader</class-name>
                            <init-params>
                                <init-param>
                                    <param-type>java.lang.String</param-type>
                                    <param-value>{cache-name}</param-value>
                                </init-param>
                            </init-params>
                        </class-scheme>
                    </cachestore-scheme>
                </read-write-backing-map-scheme>
            </backing-map-scheme>
            <autostart>true</autostart>
        </distributed-scheme>
    </caching-schemes>
</cache-config>

*Your DataLoader is missing constructor that accepts String parameter (for the cache name),

*If I get you intention right you should consider disabling the storage on the Reader node, i.e. by setting tangosol.coherence.distributed.localstorage=false.


text:
Q: MySQL - Invalid utf8mb4 character string on update My question is almost exactly the same as the one found here
MySQL - 1300 - Invalid utf8 character string on update
No solution was presented and the help from the person (creating a temporary table) didn't seem to help. Here is the select statement I am using:
SELECT
    CONVERT(line_1 USING utf8mb4),
    CONVERT(line_1 USING latin1),
    HEX(line_1)
FROM address
WHERE ((CAST(CONVERT(line_1 USING latin1) AS CHAR)) <> (CAST(line_1 AS CHAR)))
    AND CONVERT(line_1 USING utf8mb4) IS NULL;
+-------------------------------+------------------------------+----------------------------------------------------+
| CONVERT(line_1 USING utf8mb4) | CONVERT(line_1 USING latin1) | hex(line_1)                                        |
+-------------------------------+------------------------------+----------------------------------------------------+
| NULL                          | Högbergsgatan 97             | 48F6676265726773676174616E203937                   |
| NULL                          | Zücherstrasse 161            | 5AFC636865727374726173736520313631                 |
| NULL                          | 2275, Rue de l'Université    | 323237352C20527565206465206C27556E69766572736974E9 |
| NULL                          | Högbergsgatan 97             | 48F6676265726773676174616E203937                   |
+-------------------------------+------------------------------+----------------------------------------------------+

When I try to run the following update command I get:
UPDATE address
SET line_1 = CONVERT(CAST(CONVERT(line_1 USING latin1) AS CHAR) USING utf8mb4)
WHERE (CAST(CONVERT(line_1 USING latin1) AS CHAR) <> CAST(line_1 AS CHAR))
    AND CONVERT(line_1 USING utf8mb4) IS NULL;
ERROR 1300 (HY000): Invalid utf8mb4 character string: 'F66762'

I tried setting the line in the following ways, all producing the same error:
SET line_1 = CAST(CONVERT(line_1 USING latin1) AS CHAR)
SET line_1 = CONVERT(line_1 USING latin1)

I also looked at http://jonisalonen.com/2012/fixing-doubly-utf-8-encoded-text-in-mysql/ to see if maybe it was a double encoding issue, but none of these worked and I kept getting the same character string error.
Additionally, I looked at https://mathiasbynens.be/notes/mysql-utf8mb4 in order to help with the conversion steps, but utf8mb4 and utf8 are causing the exact same issues. (At first I thought it was a utf8 thing so I switched to utf8mb4 and when I still kept getting the same issues, I knew there was a deeper problem)
As you can see, something weird is going on. Looking at my show create address table, I can verify that the charset is set properly:
SHOW CREATE TABLE address;
| address | CREATE TABLE `address` (
  `addressid` bigint(20) NOT NULL AUTO_INCREMENT,
  `addressuuid` char(32) COLLATE utf8mb4_unicode_ci NOT NULL,
  `line_1` blob,
  PRIMARY KEY (`addressid`)
) ENGINE=InnoDB AUTO_INCREMENT=48970 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='All potential addresses' |

Additionally, you can see my character variables are correct in my instance:
mysql> show variables like 'char%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8mb4                    |
| character_set_connection | utf8mb4                    |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8mb4                    |
| character_set_server     | utf8mb4                    |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

How did I get here
So it might be beneficial to give some background information on the issue just in case it is something in the background that caused the issue.
I had a database that originally was set to latin1 encoding everything. I then ran the following code:
SET NAMES 'latin1';

/* We must change things to blob and then back again */
ALTER TABLE `address` CHANGE line_1 line_1 BLOB;
ALTER TABLE `address` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER TABLE `address` CHANGE line_1 line_1 VARCHAR(64);

The reason for switching to blob and then back to varchar is the normal recommended procedure. (www.percona.com/blog/2013/10/16/utf8-data-on-latin1-tables-converting-to-utf8-without-downtime-or-double-encoding/)
Let me know if that helps, and if there is any more information that can be provided. I'm using MySQL 5.6 so theoretically it should handle things better, but who knows. As there are only 4 lines I could just manually update each one, but theoretically there is a greater underlying issue and since I do actually have quite a few more columns to go through, it would be nice to make sure I have a functional way to handle these cases in case I get something with quite a few lines.

A: Since line_1 is a blob, not a text field, MySQL has no control over the "characters" in it, and does not care if it is non-text information (such as a JPG).  In the examples you gave, you have latin1 text in the field (eg, hex F6 for ö).  Hence, CONVERT(line_1 USING latin1) worked 'fine'.
I don't understand your goal.  Are you trying to read the BLOB as TEXT?  If so, and if all the non-ascii characters are encoded latin1, then that CONVERT is the answer.
If your goal is something else, then let's approach it from there.
It was not "doubly encoded", so none of them would work.

ALTER TABLE address CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

Do a SHOW CREATE TABLE address and check the CHARACTER SET of line_1.


text:
Q: Limits of $\frac{\sin^2x}{x^2}$ as $x$ approaches infinity I just want to make sure I'm on the right path with the problem. The problem is as follows:
$$\lim_{x\to\infty}\frac{\sin^2x}{x^2}$$
I rewrote it as follows:
$$\lim_{x\to\infty}\frac{(\sin x)^2}{x^2}$$
Now $\sin(x)^2$ does oscillate as $x$ approaches infinity and therefore a limit does not exist. However it oscillates between the numbers $-1$ and $1$. Since the denominator would increase without bound and the numerator would only move between $-1$ and $1$, part of me wants to say that the limit is zero.
However a smarter part of me wants to say that the limit does not exist due to the numerator. Could someone shed some light on this problem?

A: What we have is as follows: for all $x>0$,
$$
0 \leq \frac{\sin^2 x}{x^2} \leq \frac {1}{x^2}
$$
Now, note that $\lim_{x \to \infty} 0 = \lim_{x \to \infty} \frac 1{x^2} = 0$.  What theorem can we use here to get the answer?

A: To solve the following question, recall the Squeeze Theorem:
So, as we know: $$0 \le \sin^2 x \le 1$$
If we divide by $x^2$
$$0 \le \frac{\sin^2 x}{x^2} \le \frac{1}{x^2}$$
If we evaluate the limit from at either ends: $$\lim_{x \to \infty} 0 = 0$$ $$\lim_{x \to \infty} \frac 1{x^2} = 0$$
Therefore, by the squeeze theorem:
$$\lim_{x \to \infty} \frac{\sin^2 x}{x^2} = 0$$


text:
Q: Libgdx: Cursor position after catching I tried to hide the cursor image by setting the catch-flag to true:
   Gdx.input.setCursorCatched(true);

That works fine. After a while the flag is set back to false.
   Gdx.input.setCursorCatched(false);

The problem is that the cursor position where it reappears is upside down. When the cursor is moved to the top while it is invisible, the cursor reappears at the bottom. Does someone know how to fix this?


text:
Q: Upgrading SQL Azure database size I'm currently in the middle of developing an application loosely similar to StackOverflow (question / answer based)
Initially, I was using AWS (simple db and s3) for storage, however that posed several problems, and i've opted to go with SQL - With a view to eventually hosting on SQL Azure.
I was just wondering, if i opted for the 1gb database initially, could I increase the size to the 5gb / 10gb databases later on? Or would I have to create a separate new database of this size, and port my data over?

A: You can also change the size of the database through the Azure SQL portal. Simply choose the database and go to the scale tab and select the size you want.
Tada

A: Just wanted to expand on Dave's answer since it is very easy in the management tool:

A: See my answer on this other SO question. In a nutshell, you modify db size with T-SQL, such as:
ALTER DATABASE MyDatabase MODIFY (EDITION='WEB', MAXSIZE=5GB)

A: If you're working with SQL Azure Pools than you can execute the follow from the master
ALTER DATABASE database_name
MODIFY (MAXSIZE = x GB)

The key difference being the omission of the 'Edition` parameter. There are certain size restrictions though, which can be found here.


text:
Q: How to optimize performance in xna for windows phone In one of the levels of my game i have quite many big textures to draw. To give you idea what I'm talking about these are the textures sizes and the quantity in the level.
448x420 - 3, 315x400-2, 305x429 -3,  366x167-1, 356x265-4, 401x343-2, 387x251-1 plus about 20 elements with much smaller texture size.
The performance of my original implementation was 20fps. Then I created a single texture map that contained all the textures used in the level, this gave me about 3-4 extra fps. 24fps is not enough for me, what else can I try to optimize my performance?

A: Some things that I could think of in a minute

*

*Does it have a dedicated GPU and Memory or using shared memory?

*Have you tried expanding your textures to a power of two size (although only loading times might improve).

*Are you using mipmaps or other types of texture filtering?

*Howmany texels is it trying to gather?

*Have you tried to use a texture of 1 by 1, just to see if that is actually your bottleneck?

In my experience, texel calls on mobile devices are quite heavy. Be sure to try and decrease the size of your textures as much as possible. Look at your project, if an object only takes about 10% of the screen, does it really need a texture of 128x128 or bigger?
I don't know what kind of project and target device you use, but textures of 448x420(non power of two?!) seem way overkill for an mobile game. In fact, I'd recon you try and stay below a total combined texture usage of 1024x1024 or something?
Do I understand correct that this is your total texture size?
(448 * 420 * 3) + (315 * 400 * 2) + (305 * 429 * 3) + (366 * 167) + (356 * 265 * 4) + (401 * 343 * 2) + (387 * 251) = 2.019.720 pixels (ignoring the ~20 elements you mentioned)
Which means assuming 32 bits textures, in theory, you are using about 64 MB of ram, only for texture storage (and not even assuming mipmaps and XNA not up-scaling to power of two textures) (not sure if XNA internally uses dds/dxt compression(?)). I can imagine this to be quite high for a mobile device.
Hope this might help solving your bottleneck.

A: You should try profiling your application, to check if the issues are actually in the graphics rendering or somewhere else.
There are some tools available for this, although most have limitations when working in WP7. The first recommendations would be Microsoft's Windows Phone profiler and PIX for graphics monitoring, mentioned in the post. There is also the XNA Framework Remote Performance Monitor, but as far as I can tell there is no WP7 compatible version.


text:
Q: Using SunStudio dbxtool on Red Hat Linux. Using sunstudio12.1 dbxtool on Red Hat Linux.  I am able to use dbx command line interface from sunstudio12.1/bin directory to debug a Linux executable.
However when I try to use sunstudio12.1/bin/dbxtool I get "wrong architecture" dbx and dbxtool are located in the same directory and are pathed correctly.
Am I trying to do the impossible?  Or is there a trick to get this to work?


text:
Q: Cannot access an NTFS volume via CreateFile I am attempting to add USB key passthrough functionality to a piece of software that is similar in functionality to Remote Desktop. I have the following code:
public void Open(string path)
{
    System.OperatingSystem ver = System.Environment.OSVersion;
    uint mode;
    uint attr;
    uint share;
    int junk = 0;
    bool success;
    string partitionFileName = @"\\.\" + path;
    uint drvtype;

    // Only determine drvtype if not already defined
    // Default drytype is DRIVE_UNKNOWN
    if (drvtype == Kernel32.DriveType.Unknown)
    {
        drvtype = Kernel32.GetDriveType(path); //0=unknown, 1 = no root drive, 2= removable, 3=fixed, 4=remote, 5=cdrom, 6=ramdisk
    }

    // Set the access modes depending on what we are opening
    if (drvtype == Kernel32.DriveType.CDROM)
    {
        // Depending on the OS version, the flags for opening the cdrom for ioctls are different
        if (ver.Version.Major == 4)    //4 == Windows NT
        {
            mode = Kernel32.GENERIC_READ;
            share = Kernel32.FILE_SHARE_READ;
            attr = Kernel32.FILE_ATTRIBUTE_NORMAL;
        }
        else
        {
            mode = Kernel32.GENERIC_READ | Kernel32.GENERIC_WRITE;
            share = Kernel32.FILE_SHARE_READ | Kernel32.FILE_SHARE_WRITE;
            attr = Kernel32.FILE_ATTRIBUTE_NORMAL;
        }
    }
    else
    {
        mode = Kernel32.GENERIC_READ | Kernel32.GENERIC_WRITE;
        share = Kernel32.FILE_SHARE_READ | Kernel32.FILE_SHARE_WRITE;   // exclusive access to device
        attr = Kernel32.FILE_ATTRIBUTE_NORMAL;
    }

    // Try to open the partition as read/write
    hFile = Kernel32.CreateFile(partitionFileName,
                      mode,
                      share,
                      IntPtr.Zero,
                      Kernel32.OPEN_EXISTING,
                      attr,
                      IntPtr.Zero);

    // Lock the volume
    Kernel32.DeviceIoControl(hFile, Kernel32.EIOControlCode.FsctlLockVolume, IntPtr.Zero, 0, IntPtr.Zero, 0, ref junk, IntPtr.Zero);

    // Dismount from the host OS to get around Vista's security
    Kernel32.DeviceIoControl(hFile, Kernel32.EIOControlCode.FsctlDismountVolume, IntPtr.Zero, 0, IntPtr.Zero, 0, ref junk, IntPtr.Zero);

    // Allow extended access to get at boot sectors
    Kernel32.DeviceIoControl(hFile, Kernel32.EIOControlCode.FsctlAllowExtendedDasdIo, IntPtr.Zero, 0, IntPtr.Zero, 0, ref junk, IntPtr.Zero);

    // If it's a USB disk, see if it is write-protected
    if (drvtype == Kernel32.DriveType.Removable)
    {
        // If it is write-protected, tell the user
        success = Kernel32.DeviceIoControl(hFile, Kernel32.EIOControlCode.DiskIsWritable, IntPtr.Zero, 0, IntPtr.Zero, 0, ref junk, IntPtr.Zero);
        if (success)
        {
            bWriteProtect = false;
        }
        else
        {
            if (Kernel32.GetLastError() == 19) //ERROR_WRITE_PROTECT
            {
                bWriteProtect = true;
                showStatus("Unable to open as read/write. File opened as read-only.");
            }
            else
            {
                bWriteProtect = false;
            }
        }
    }
}

I then read from the disk with ReadFile(). It works great for a FAT-formatted USB key, I can read/write and even boot from it. For some reason however, it does not work with NTFS-formatted keys. Calling ReadFile() throws error 21, ERROR_NOT_READY, which some digging has found is because the volume is unmounted. I cannot leave the volume mounted, because I am accessing (and possibly writing to) the boot sectors on the drive, and from what I understand, Vista/7 will not allow you to do that without first unmounting the drive.
Are there some other DeviceIoControl calls I am supposed to be making for NTFS that I'm missing?


text:
Q: undefined when query realm db async I am trying to query from realm db but it is undefined.
DogService.js
import Realm from 'realm';
import {databaseOptions} from '../models'

    export const DogService  = {
        findAll: async () => {
            Realm.open(databaseOptions).then(
                realm => {
                    const result = realm.objects('Dog')
                    console.log(result) // <=got result
                    return result;
                });
        },
        save: () => {
            Realm.open(databaseOptions).then(
                realm => {
                    realm.write(() => {
                        realm.create('Dog', {name: 'Alex'})
                    });
                });
        }
    }

App.js
async componentDidMount() {
     let realm = await DogService.findAll();
    console.log(realm); // <= undefined
}


text:
Q: Power Platform admin center - missing "Data export (preview)" option I've tried to enable synchronization of logs between my Dataverse environment and Azure Application Insights service. I've tried to setup the thing according to the https://learn.microsoft.com/en-us/power-platform/admin/set-up-export-application-insights article. What I'm missing is the "Data export (preview)" menu option in my admin.powerplatform.com portal :/.

Any idea what is the reason of the described situation?
Am I missing some feature? Or should I enable something in my tenant?
Any help will be appreciated.

A: Is this a production / licensed instance?  Also are you admin on that instance? Is the same account in the azure tenant with access to an app insights account?
I believe it should be available in all regions, so if you don't seen in a licensed instanced I would create a support ticket with MS.


text:
Q: Divide by bins with pandas I have a dataframe in pandas that looks like below. Index is date time object, ordered by day, divided in 5 minute bins. I have a column called 'col1'. So if I do
df['col1']

I get:
DateTime
2008-04-28 09:40:00     300.0
2008-04-28 09:45:00    -800.0
2008-04-28 09:50:00       0.0
2008-04-28 09:55:00    -100.0
2008-04-28 10:00:00       0.0
2008-04-29 09:40:00     500.0
2008-04-29 09:45:00     800.0
2008-04-29 09:50:00     100.0
2008-04-29 09:55:00    -100.0
2008-04-29 10:00:00       0.0

I have another dataframe in pandas obtained using groupby in the original dataframe using
df2 = df([df.index.time])[['col2']].mean()

that outputs:
           col2
09:40:00   4603.585657
09:45:00   5547.011952
09:50:00   8532.007952
09:55:00   6175.298805
10:00:00   4236.055777

What I would like to do is to divide col1 by col2 for each of the 5 minute bins without using a for loop. To explain better, for all the days, for each bin divide col1 by col2. For example, divide all the 9:40:00 values in col1 by 9:40:00 value in col2.
I have no idea how to begin doing this without a for loop, but I have the impression that it should be doable with pandas.
The expected output is:
DateTime
2008-04-28 09:40:00     300.0/4603.585657
2008-04-28 09:45:00    -800.0/5547.011952
2008-04-28 09:50:00       0.0/8532.007952
2008-04-28 09:55:00    -100.0/6175.298805
2008-04-28 10:00:00       0.0/4236.055777
2008-04-29 09:40:00     500.0/4603.585657
2008-04-29 09:45:00     800.0/5547.011952
2008-04-29 09:50:00     100.0/8532.007952
2008-04-29 09:55:00    -100.0/6175.298805
2008-04-29 10:00:00       0.0/4236.055777

A: If need divide by times:
df['new'] = df['col1'].div(df.groupby(df.index.time)['col1'].transform('mean'))
print (df)
                      col1   new
DateTime
2008-04-28 09:40:00  300.0  0.75
2008-04-28 09:45:00 -800.0  -inf
2008-04-28 09:50:00    0.0  0.00
2008-04-28 09:55:00 -100.0  1.00
2008-04-28 10:00:00    0.0   NaN
2008-04-29 09:40:00  500.0  1.25
2008-04-29 09:45:00  800.0   inf
2008-04-29 09:50:00  100.0  2.00
2008-04-29 09:55:00 -100.0  1.00
2008-04-29 10:00:00    0.0   NaN

Or if need divide by days:
df['new'] = df['col1'].div(df.groupby(df.index.date)['col1'].transform('mean'))
print (df)
                      col1       new
DateTime
2008-04-28 09:40:00  300.0 -2.500000
2008-04-28 09:45:00 -800.0  6.666667
2008-04-28 09:50:00    0.0 -0.000000
2008-04-28 09:55:00 -100.0  0.833333
2008-04-28 10:00:00    0.0 -0.000000
2008-04-29 09:40:00  500.0  1.923077
2008-04-29 09:45:00  800.0  3.076923
2008-04-29 09:50:00  100.0  0.384615
2008-04-29 09:55:00 -100.0 -0.384615
2008-04-29 10:00:00    0.0  0.000000


text:
Q: Swift, Protocols and Generics in Genetic Algorithm I am trying to switch from Java to Swift and improve my programming skills in this language.
However, I have some difficulties understanding how generics works in Swift after a study of:
https://docs.swift.org/swift-book/LanguageGuide/Generics.html
I have started to write a genetic algorithm by writing some protocols.
protocol Point : Equatable {
    var identifier: String { get }
    var x: Double { get }
    var y: Double { get }
    func distance<P : Point>(to point: P) -> Double
}

protocol Individual {
    associatedtype P : Point
    var fitness: Double { get }
    var chromosomes: [P] { get }
}

and now I want to make a struct which conforms to the Individual protocol.
The only try that compiles is
struct Route : Individual {
    typealias P = City;
    var fitness: Double { 0.0 }
    var chromosomes: [City]
}

However, I want to make Route as much as generic, therefore I don't want to tell that it uses City as implementation of Point. I want that Route knows that it works on array of objects which conforms to Point protocol.
I'd appreciate your help.
Thank you in advance.

A: First of all, I'd suggest adding a Self requirement on the distance(to:) method. Self just tells the compiler that the paremeter is the same type as the conforming type.
protocol Point : Equatable {
    var identifier: String { get }
    var x: Double { get }
    var y: Double { get }
    func distance(to point: Self) -> Double
}

So, in your City struct point must also be of type City.
struct City: Point {
    var identifier: String
    var x: Double
    var y: Double

    func distance(to point: City) -> Double {
        return .zero
    }
}

You can make your Route struct more flexible by adding a generic parameter that also satisfies the associated type requirement imposed by the Individual protocol.
struct Route<P: Point>: Individual {
    var fitness: Double { 0.0 }
    var chromosomes: [P]
}

To instantiate a Route:
var cityRoute = Route<City>(chromosomes: [])

A: You can create an array of class that implements a specific protocol, so for example in this case you will have:
 struct Route: Individual {
    typealias P = City;
    var fitness: Double { 0.0 }
    var chromosomes: [Point]
}

and you can call all the protocol methods.


text:
Q: Not being able to initialize condition variable in a struct Why am I not able to initialize a condition variable in a struct?
I want each node to have a condition variable so I can wait and signal it, and when I add the initialization code it throws this error:
expected ':', ',', ';', '}' or 'attribute' before '=' token
make: * [trie.o] Error 1
I tried doing the initializition when needed somewhere(not in the struct)and I still get an error, I have included pthreads.h
struct trie_node {

  pthread_cond_t condition=PTHREAD_COND_INITIALIZER;
};

A: You can't use PTHREAD_COND_INITIALIZER when initializing a structure member.
You have to use pthread_cond_init after you create an instance of the structure.
Actually, you can't initialize structure members at all in the structure definition, it's not just this one.

A: To initialize when you define a struct trie_node object, use:
struct trie_node x = { …, PTHREAD_COND_INTIALIZER, … };

In place of the "…", put initializers for other members of the struct, if any. You can omit the trailing ones if you do not care about them.
To assign a value to the condition member in an existing object, use:
x.condition = (pthread_cond_t) PTHREAD_COND_INITIALIZER;

Two things:
The code you used failed because you cannot specify the initialization of a member when you are defining the struct. Initialization goes with definitions of objects, not definitions of types.
The expansion PTHREAD_COND_INITIALIZER has the format of an initializer, something like { values }. It is not an expression, so you cannot use it normally in an assignment statement. However, the (pthread_cond_t) PTHREAD_COND_INITIALIZER shown above creates a "compound literal" that is an expression.

A: To initialize a structure member you have to create an instance of the structure as follows.
struct trie_node my_node;
Then you can initialize it like below.
my_node.condition=PTHREAD_COND_INITIALIZER;
OR
struct trie_node *my_node;
my_node->condition=PTHREAD_COND_INITIALIZER;


text:
Q: Emulate a Bluetooth keyboard on Mac I'm trying to build an application for mac that can emulate mac to an Bluetooth keyboard.
after reading 《HID over GATT Profile 1.0》 and other Bluetooth document, I trying to use Apple's Core Bluetooth framework to do this. But after called
myPeripheralManager.add(hid_service) I got an error message :
Error Domain=CBErrorDomain Code=8 "The specified UUID is not allowed for this operation." UserInfo={NSLocalizedDescription=The specified UUID is not allowed for this operation.}

The UUID of service is:

*

*HID Service : 0x1812

*Device Information Service : 0x180A

*Battery Service : 0x180F

It does appear that Apple prevents the HID service from working on Mac acting as a peripheral.
But some app like Key Pad actually can do this on mac.
What way can circumvent Apple's restriction?

A: KeyPad is emulating a legacy bluetooth HID device, not BLE. Apple does not allow BLE HID on the Mac/iPhone.


text:
Q: Can I disable GNOME legacy tray in 17.04? The legacy tray (see the screenshot below) will be removed in GNOME 3.26. I want to wean myself off it before the change arrives in Ubuntu (17.10 or 18.04).

Can I disable the legacy tray in Ubuntu GNOME 17.04?

A: Several GNOME shell extensions can do that.

*

*Hide Legacy Tray will just hide it.

*TopIcons Plus will move
the items from the tray to the top bar. It's cool and has many
customize options like icons color saturation, padding and size.


text:
Q: Joining two lists of objects in java I have a class of instances which follows:
package com.sample;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement (name = "instance")
public class Instance {

int id;
double startValue;
double endValue;
double duration;
String Code;

public int getId() {
    return id;
}
@XmlElement (name = "ID")
public void setId(int id) {
    this.id = id;
}
public double getStart() {
    return startValue;
}
@XmlElement
public void setStart(double start) {
    this.startValue = start;
}
public double getEnd() {
    return endValue;
}
@XmlElement
public void setEnd(double end) {
    this.endValue = end;
}

public void setDuration() {
    duration = endValue - startValue;
}
public String getCode() {
    return Code;
}
@XmlElement
public void setCode(String code) {
    Code = code;
}

}

And each instance object is being mapped to values in a XML file of events through a jaxb unmarshaller. These instance objects are then being stored in list class called Instances as follows:
package com.sample;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement (name = "ALL_INSTANCES")
@XmlAccessorType(XmlAccessType.FIELD)
public class Instances {

@XmlElement(name = "instance")
private List<Instance> instances = null;

public List<Instance> getInstances() {
    return instances;
}
public void setInstances(List<Instance> instances) {
    this.instances = instances;
}

public int getLength() {
    return instances.size();
}

public static List<Instances> getUnion(List<Instances> one, List<Instances>      two) {
    List<Instances> union = new ArrayList<Instances>(one);
    union.addAll(two);
    return union;
}
}

In the main method, im trying to call the getUnion() method of two lists of instances to join into one after ive called the unmarshalling process.
Instances ins = (Instances) jaxbUnmarshaller.unmarshal(file);
Instances ins1 = (Instances) jaxbUnmarshaller.unmarshal(file1);
Instances.getUnion(ins, ins1);

But saying that I get "The method getUnion(List, List) in the type Instances is not applicable for the arguments (Instances, Instances)". Not sure if im calling this right or designed it right.
Basically, I need a method for my Instances class that joins two Instances together into one List to process.
Thanks.

A: The method takes two arguments of type List<Instance>, not of type Instances, which is a bit confusing and you may be better off changing the getUnion method. But as it currently stands, you can invoke it like this:
Instances ins = (Instances) jaxbUnmarshaller.unmarshal(file);
Instances ins1 = (Instances) jaxbUnmarshaller.unmarshal(file1);
List<Instance> result = Instances.getUnion(ins.getInstances(), ins1.getInstances());


text:
Q: OpenVPN create a connection, run a script and shut it down What would be the best way of going about the following problem?
I have a server that I periodically dump backups to. The only way to connect to this server is via OpenVPN. So far I tested everything manually and it worked fine. Here's what I do:

*

*run openvpn --config my-server-connection-details.ovpn

*in second screen console window I mount NFS share, perform the copy and unmount it

*I go back to screen console 1 and ctrl+c the connection.

How can I best script it so I can just put it in cron? I'm mostly inquiring about the openvpn part, as the rest seems clear to me and I'm an OpenVPN / VPN newbie. The client is a linux machine.
Thanks in advance.

A: What distribution of Linux?   This should be trivial on Debian/Ubuntu.
Setup your config in /etc/openvpn/myvpn.conf, adjust your /etc/default/openvpn file so vpns are not automatically started.
Then your script does something like this.  No need to use screen or anything fancy.
#!/bin/sh
/etc/init.d/openvpn start myvpn
mount /nfs
rsync src dst
/etc/init.d/openvpn stop myvpn


text:
Q: test iptables rules using netcat is not working I have 4 virtual machines using virtual box. One virtual machine work as a router that connects 3 networks. Im trying to drop all communications entering the router machine, excpet for some services. The interfaces ips that I have in each machine are:
Virtual machine 1 that work as router:
eth1: 193.136.200.254
eth2: 10.10.0.254
eth3: 10.254.0.254

Virtual machine 2:
eth1: 10.10.0.2
eth2: 10.10.0.3

Virtual machine 3:
eth1: 193.136.200.2

Virtual machine 4:
eth1: 10.254.0.1

I define the rules below on the router machine:
IPTABLES -P INPUT DROP
iptables -A INPUT -s 193.136.200.3 -d 193.136.200.254 -p udp -m udp --sport 53 -j ACCEPT

And now Im trying to test if this is working using netcat. So in the router machine Im running the command:
nc -u -s 193.136.200.254 -p 53 193.136.200.3 53

And in thevirtual machine 3 Im running:
nc -u 193.136.200.3 53

Then I write a random word in one machine but it is not appearing in the other machine. So it isnt working. Do you know what is wrong?

A: It's because you are connectiong itself, when you type the command:
nc -u 193.136.200.3 53

If you want to connect to your router from machine 3 try this command:

*

*nc -u -s 193.136.200.3 -p 53 193.136.200.254 53 instead of nc -u 193.136.200.3 53


text:
Q: How to test a disk that does not support SMART for errors? I have a Raspberry Pi (running Raspbian) that is booting from a microSD card. Since it's acting as a home server, naturally I want to monitor the microSD card for errors. Unfortunately though, microSD cards don't support SMART like other disks I have, so I am unsure how to monitor the disk for errors.
How can I monitor / check disks that do not support SMART for errors when they are still in use / have partitions mounted?

A: You can replace smartctl -t long selftests with badblocks (no parameters). It performs a simple read-only test. You can run it while filesystems are mounted. (Do NOT use the so-called non-destructive write test).
# badblocks -v /dev/loop0
Checking blocks 0 to 1048575
Checking for bad blocks (read-only test): done
Pass completed, 0 bad blocks found. (0/0/0 errors)

Note you should only use this if you don't already suspect there are bad sectors; if you already know it's going bad, use ddrescue instead. (badblocks throws away all data it reads, ddrescue makes a copy that may come in useful later).
Other than that, you can do things that SMART doesn't do: use a checksumming filesystem, or dm-integrity layer, or backups & compare, to actually verify contents. Lacking those, just run regular filesystem checks.
MicroSD cards also have failure modes that are hard to detect. Some cards may eventually discard writes and keep returning old data on reads. Even simple checksums might not be enough here - if the card happens to return both older data and older checksums, it might still match even if it's the wrong data...
Then there are fake capacity cards that just lose data once you've written too much. Neither return any read or write errors, and it can't be detected with badblocks, not even in its destructive write mode (since the pattern it writes are repetitive).
For this you need a test that uses non-repetitive patterns, e.g. by putting an encryption layer on it (badblocks write on LUKS detects fake capacity cards when badblocks write on raw device does not).


text:
Q: How to create database from codebehind in ASP.net 2.0? I need to create database from codebehind in ASP.net 2.0.
Please help.

A: May be like this:
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand("CREATE DATABASE database_name", connection);
command.Connection.Open();
command.ExecuteReader();
connection.Close();


text:
Q: Creating a File Drop Zone using Angular I'm new to angular, and new to HTML for that matter, and I'm trying to set up a file drag and drop box. But it seems like all the tutorials I see online, just simply do not work for me. So I'll take this tutorial as an example on here:
https://codingdiksha.com/angular-drag-and-drop-multiple-files-upload-to-firebase-storage-using-angularfire2/
Specifically it has the following:

I have come to the conclusion that hovered and dropped are simply not events. Obviously the intent is when I hover, the event (hovered) is supposed to call my toggleHover method in the .TS file. But this doesn't happen. If I use mouseenter instead of hovered, the method is called.
Does the stackOverflow community agree that hovered is not really an event? If it is an event, why does mouseenter work and not hovered?
Further more, every tutorial I have tried to follow, whenever I place a file over the drop zone, my chrome simply downloads the file to my hard drive. It does that anywhere on the screen, including my drop zone. In the tutorial I'm copying, the file is uploaded instead. Most tutorials I have seen for angular, use something along the lines of this:
<input type="file" (change)="onDrop($event)">

Is my code simply not calling the onDrop method, and that's why my browser is simply downloading my file and acting like a drop file zone doesn't even exist?
I know this is a little vague, but I'm hoping someone on here can give me some sort of direction. Perhaps a useful tutorial I haven't come across yet. Thanks for any help you can provide me.

A: First of all: the input with type="file" is needed to show the choose file dialog from your os. Many examples set this input to hidden and simulate a click on it.
Then (in your example you wanna use a directive) the directive needs all the functions as drag and drop need.
export class FileDragNDropDirective {
  //@Input() private allowed_extensions : Array<string> = ['png', 'jpg', 'bmp'];
  @Output() private filesChangeEmiter : EventEmitter<File[]> = new EventEmitter();
  //@Output() private filesInvalidEmiter : EventEmitter<File[]> = new EventEmitter();
  @HostBinding('style.background') private background = '#eee';
  @HostBinding('style.border') private borderStyle = '2px dashed';
  @HostBinding('style.border-color') private borderColor = '#696D7D';
  @HostBinding('style.border-radius') private borderRadius = '5px';

  constructor() { }

  @HostListener('dragover', ['$event']) public onDragOver(evt){
    evt.preventDefault();
    evt.stopPropagation();
    this.background = 'lightgray';
    this.borderColor = 'cadetblue';
    this.borderStyle = '3px solid';
  }

  @HostListener('dragleave', ['$event']) public onDragLeave(evt){
    evt.preventDefault();
    evt.stopPropagation();
    this.background = '#eee';
    this.borderColor = '#696D7D';
    this.borderStyle = '2px dashed';
  }

  @HostListener('drop', ['$event']) public onDrop(evt){
    evt.preventDefault();
    evt.stopPropagation();
    this.background = '#eee';
    this.borderColor = '#696D7D';
    this.borderStyle = '2px dashed';
    debugger;
    let files = evt.dataTransfer.files;
    let valid_files : Array<File> = files;
    this.filesChangeEmiter.emit(valid_files);
  }
}

And here is a sample HTML snippet:

            <input type="file" name="file" id="file" (change)="onFileChange($event.target.files)" multiple>
            Select your file or Drop it here!

All other you can see in this Stackblitz example.


text:
Q: S3 limit to objects in a bucket Does anyone know if there is a limit to the number of objects I can put in an S3 bucket? can I put a million, 10 million etc.. all in a single bucket?

A: While you can store an unlimited number of files/objects in a single bucket, when you go to list a "directory" in a bucket, it will only give you the first 1000 files/objects in that bucket by default. To access all the files in a large "directory" like this, you need to make multiple calls to their API.

A: It looks like the limit has changed. You can store 5TB for a single object.

The total volume of data and number of objects you can store are unlimited. Individual Amazon S3 objects can range in size from a minimum of 0 bytes to a maximum of 5 terabytes. The largest object that can be uploaded in a single PUT is 5 gigabytes. For objects larger than 100 megabytes, customers should consider using the Multipart Upload capability.

http://aws.amazon.com/s3/faqs/#How_much_data_can_I_store

A: There are no limits to the number of objects you can store in your S3 bucket. AWS claims it to have unlimited storage. However, there are some limitations -

*

*By default, customers can provision up to 100 buckets per AWS account. However, you can increase your Amazon S3 bucket limit by visiting AWS Service Limits.

*An object can be 0 bytes to 5TB.

*The largest object that can be uploaded in a single PUT is 5 gigabytes

*For objects larger than 100 megabytes, customers should consider using the Multipart Upload capability.

That being said if you really have a lot of objects to be stored in S3 bucket consider randomizing your object name prefix to improve performance.

When your workload is a mix of request types, introduce some randomness to key names by adding a hash string as a prefix to the key name. By introducing randomness to your key names the I/O load will be distributed across multiple index partitions. For example, you can compute an MD5 hash of the character sequence that you plan to assign as the key and add 3 or 4 characters from the hash as a prefix to the key name.

More details - https://aws.amazon.com/premiumsupport/knowledge-center/s3-bucket-performance-improve/
-- As of June 2018

A: *

*There is no limit on objects per bucket.

*There is a limit of 100 buckets per account  (you need to request amazon if you need more).

*There is no performance drop even if you store millions of objects in a
single bucket.

From docs,

There is no limit to the number of objects that can be stored in a
  bucket and no difference in performance whether you use many buckets
  or just a few. You can store all of your objects in a single bucket,
  or you can organize them across several buckets.

as of Aug 2016

A: According to Amazon:

Write, read, and delete objects containing from 0 bytes to 5 terabytes of data each. The number of objects you can store is unlimited.

Source: http://aws.amazon.com/s3/details/ as of Sep 3, 2015.

A:
"You can store as many objects as you want within a bucket, and write,
  read, and delete objects in your bucket. Objects can be up to 5
  terabytes in size."

from http://aws.amazon.com/s3/details/ (as of Mar 4th 2015)

A: @Acyra- performance of object delivery from a single bucket would depend greatly on the names of the objects in it.
If the file names were distanced by random characters then their physical locations would be spread further on the AWS hardware, but if you named everything 'common-x.jpg', 'common-y.jpg' then those objects will be stored together.
This may slow delivery of the files if you request them simultaneously but not by enough to worry you, the greater risk is from data-loss or an outage, since these objects are stored together they will be lost or unavailable together.


text:
Q: Drawing with sugar I want to create a drawing in sugar thing, much the same as you would throw some sugar on a table and using your fingers to "erase" the sugar particles to form an image.
Does anyone know of a js type tool I can use to make this happen?
I suppose I can take a photo of a desk, and a photo of desk with some sugar on it, and then just erase the top layer, but I'm worried that this won't give a real effect.
I'm currently thinking of having a photo of desk, and then using JS to generate a lot of "sugary" particles, which I can then erase. This sounds incredibly hard to do though. Is it? Can someone point me in a good direction?

A: Sand or is it sugar?
An interesting problem that I had to give a little time.
This works by creating several buffers to hold grains of sand (sugar) and give them life when they need to move.
There is no way that Javascript could do a whole screen of a million plus grains so this demo cheats by only updating a very few and prioritising for new movement rather than allow older moving grains to hog CPU time.
The arrays active, sandStatus, holds the sand gains. active has the pixel address as a 32Bit int and sandStatus has age. The Array sand holds the amount of sand at each pixel and is used to calculate the shadow effect (shadow could be much better using a webGL shader) and to work out which direction sand should slide if disturbed or dropped to the surface.
the var activeMax holds the max number of active sand grains. Increase for a better effect, decrease if the sim runs to slow.
To drop sand use the right mouse button. Hold at one spot to make a pile. Left button pushes the sand about. When you hit a bigger pile the machine may lag (depending on CPU power and browser (best in firefox)).
The push function checks the sand array for any sand. If found it pushes the sand away from the center and piles it up around the edge. Some sand will fall back.
The function sprinkle adds grains of sand (one are a time by pixel coordinate or by index). The function push does the sand drawing FX. update moves the sand grains by checking surrounding pixels heights and moving grains down hill. renderPix handles rendering grains, creating the shadows and deactivating sand grains. The Array shadowChange holds the index of pixels that have had changes so that the shadows can be updated.
Bottom half of the demo is just boilerplate for mouse and canvas setup. All the code in regard to the answer is in the first half.

"use strict";

var activeMax = 2280;  // this is the number of sand grains that are processed at
                   // at time. Increase for better looking effect. decrease
                   // if the machine is not keeping up with the load

var cw;
var ch;
var w;  //
var h;
var canvasBuf = document.createElement("canvas");
var ctxB
var globalTime;  // global to this
var pixels
var sand;
var sandToFall;
var sandToFallCount = 36000;
var shadow; // shadow pixels
var activeMax = 2280;
var active; // index of pixel for active grain
var sandStatus; // status of active grain
var shadowChange; // holds index of pixels that have a shadow change
var pixels;
var buf;
var grain = 0xFFFFFFFF;
var shadowGrain = 0x00000000;
var ready = false;
var sandReady = 0;
var nextActive = 0;
var nextActiveShadow = 0;

var onResize = function(){
cw = canvas.width;
ch = canvas.height;
w = cw;  //
h = ch;
pixels = w*h;
canvasBuf.width = w;
canvasBuf.height = h;
ctxB = canvasBuf.getContext("2d");
sand = new Uint8ClampedArray(pixels);
shadow = new Uint8ClampedArray(pixels); // shadow pixels
sandToFall = new Uint32Array(sandToFallCount);
activeMax = 2280;
active = new Uint32Array(activeMax); // index of pixel for active grain
sandStatus = new Uint16Array(activeMax); // status of active grain
shadowChange= new Uint32Array(activeMax); // holds index of pixels that have a shadow change
sandStatus.fill(0); // clear
active.fill(0);
shadowChange.fill(0);
ctxB.clearRect(0,0,w,h);
ctxB.fillStyle = "white";
ctxB.font = "84px arial";
ctxB.textAlign = "center";
ctxB.globalAlpha = 0.01;
for(var i = 0; i < 12; i ++){
    ctxB.fillText("Sand Doodler!",w/2 + (Math.random()-0.5)*5,h/2 + (Math.random()-0.5)*5);
}
ctxB.globalAlpha = 1;
pixels = ctxB.getImageData(0,0,w,h);
buf = new Uint32Array(pixels.data.buffer);
for(i = 0; i < buf.length; i += 3){
    if(buf[i] !== 0){
        var c = buf[i] >>> 24;
        buf[i] = 0;
        while(c > 0){
            var ind = Math.floor(Math.random()*sandToFallCount);
            if(sandToFall[ind] === 0){
                sandToFall[ind] = i;
            }
            c = c >>> 1;
        }
    }
}
buf.fill(0);
offsets = [1,w-1,w,w+1,-w-1,-w,-w+1,-1];
shadowOffsets = [-w-1,-w,-1];
ready = true;
sandReady=0;
}
function sprinkle(x,y){
var ind;
if(y === undefined){
    ind = x;
}else{
    ind = x + y*w;
}
var alreadyExists = active.indexOf(ind);
var ac = nextActive;
if(alreadyExists > -1){
    sand[ind] += 1;
    shadow[ind] = 0;
    sandStatus[alreadyExists] = 66;
}else{
    active[nextActive] = ind;
    sandStatus[nextActive] = 66;
    shadowChange[nextActiveShadow];
    nextActiveShadow = (nextActiveShadow+1)%activeMax;
    nextActive = (nextActive +1)%activeMax;
    sand[ind] += 1;
    shadow[ind] = 0;
}
return ac;
}

var offsets = [1,w-1,w,w+1,-w-1,-w,-w+1,-1];
var offsetCount = 8;
function update(){
var min,max,minDir,maxDir,dir,start,jj,j,ind,level,i,l1;
for( i = 0; i <activeMax; i ++){
    if(sandStatus[i] !== 0){
        ind = active[i];
        level = sand[ind];
        if(level === 1){
            sandStatus[i] = 1; // deactive is cant move (level ground)
        }else{
            min = level;
            var d;
            minDir = offsets[Math.floor(Math.random()*16)];
            dir = null;
            start = Math.floor(Math.random()*16); // start at a random direction
            for(j=0;j < offsetCount; j++){
                jj = offsets[(j + start)%offsetCount];
                l1 = sand[ind+jj];
                if(l1 < min){
                    min = l1;
                    minDir = jj;
                    d = (j + start)%offsetCount;
                }
            }
            dir = null;
            if(min >= level - 1){ // nowhere to move
                sandStatus[i] = 1;

            }else
            if(min < level-1){ // move to lowest
                dir = minDir
            }
            if(dir !== null){
                var lv = level-min;
                while(lv > 2){
                    active[i] = ind + dir;
                    if(sand[ind] > 1){
                        sand[ind] -= 2;
                        sprinkle(ind)
                    }else{
                        sand[ind] -=1;
                    }
                    ind = ind+dir;
                    sand[active[i]] += 1;
                    if(sand[active[i] + offsets[d]] >=level){
                        d+= Math.random()<0.5? 1 : offsetCount -1;
                        d %=offsetCount;
                    }
                    lv -= 1;
                }
                if(sand[ind]>0){
                    active[i] = ind + dir;
                    sand[ind] -= 1;
                }
                sand[active[i]] += 1;
            }
        }
    }
}
}
var shadowOffsets = [-w-1,-w,-1];
var shadowCols = [0xFFf0f0f0,0xFFd0d0d0,0xFFb0b0b0,0xFF909090];
var shadowDist = [0xf0000000,0xd0000000,0xb0000000,0x90000000]; // shadow col no sand

// renders grains and adds shadows. Deactivates gains when they are done
function renderPix(){
var ac = 0;
for(var i = 0; i < activeMax; i ++){
    if(sandStatus[i] !== 0){
        ac += 1;
        var ind = active[i];
        buf[ind] = grain;
    }
}
for(var i = 0; i < activeMax; i ++){
    if(sandStatus[i] !== 0){
        var ind = active[i];
        var level = sand[ind];
        var col =0;
        if(sand[ind + shadowOffsets[0]] > level ){
            col = 2;
        }else
        if(sand[ind + shadowOffsets[1]] > level ){
            col =1;
        }else
        if(sand[ind + shadowOffsets[2]] > level ){
            col = 1;
        }
        buf[ind] = grain;  // add a sand grain to the image
        shadow[ind] = col;
        shadowChange[nextActiveShadow] = ind;
        nextActiveShadow = (nextActiveShadow + 1)%activeMax;

        var c = 4;
        while(c > 0){
            c-=1;
            ind += w + 1;
            var s = sand[ind];
            var dif = level - s;
            if(dif > 0){
                c-= dif;
            }
            shadow[ind] += 1;
            shadowChange[nextActiveShadow] = ind;
            nextActiveShadow = (nextActiveShadow + 1)%activeMax;
        }
        sandStatus[i] -= 1;
        if(sandStatus[i] === 1){
            sandStatus[i] = 0;
            active[i] = 0;
        }
    }
}
// add calculated shadows
for(var i = 0; i < activeMax; i ++){
    if(shadowChange[i] !== 0){
        var ind = shadowChange[i];
        var s = shadow[ind] <4 ? shadow[ind]-1:3;
        if(sand[ind] > 0){
            buf[ind]=shadowCols[s];
        }else{
            buf[ind]=shadowDist[s];
        }
        shadowChange[i] = 0;
    }
}

}

// push sand about
function push(x,y,radius){
var iyy,iny
var rr = radius * radius ;
x = Math.floor(x);
y = Math.floor(y);
for(var iy = -radius + 1; iy < radius; iy ++){
    iyy = iy * iy;
    iny = (y+iy) * w;
    for(var ix = -radius + 1; ix < radius; ix ++){
        if(ix*ix + iyy <= rr){ // is inside radius
            var ind = (x + ix) + iny;
            if(sand[ind] > 0){
                var dir = Math.random() * Math.PI * 2;
                dir = Math.atan2(iy,ix)
                var r = radius + Math.random() * radius *0.2
                var xx = Math.cos(dir) * r;
                var yy = Math.sin(dir) * r;
                buf[ind] = 0x000000;
                sand[ind] = 0;

               ind = Math.floor(xx + x) + Math.floor(yy + y) * w;
               sprinkle(ind);
            }else{
                buf[ind] = 0;
            }
        }
    }
}
}

function showHeight(){  // for debugging only
for(var i = 0; i < sand.length; i ++){
    buf[i] = 0xff000000;
    var k = sand[i];
    buf[i] +=(k <<16) + (k<<8) + (k);
}
}

// main update function
function display(){
if(!ready){  // only when ready
    return;
}
//ctx.setTransform(1,0,0,1,0,0); // reset transform
ctx.clearRect(0,0,cw,ch);
var mx = Math.floor((mouse.x/cw)*w); // canvas buf mouse pos
var my = Math.floor((mouse.y/ch)*h);

// drop sand
if(mouse.buttonRaw & 4){
    for(var i = 0; i < 120; i ++){
        var dir = Math.random()*Math.PI;
        var dist = ((Math.random()+Math.random()+Math.random())/3-0.5) * 62;
        var x = Math.cos(dir) * dist;
        var y = Math.sin(dir) * dist;
        x += mx;
        y += my;
        x = Math.floor(x); // floor
        y = Math.floor(y); // floor

        sprinkle(x,y);
    }
}else{
    // drop sand for intro FX
    if(sandReady <sandToFallCount){
        for(var i = 0; i < 120; i ++){
            if(sandToFall[sandReady] !== 0){
                sprinkle(sandToFall[sandReady] + offsets[Math.floor(Math.random()*8)%offsets.length]);
            }
            sandReady += 1;
        }
    }
}
   // push sand about.
   if(mouse.buttonRaw & 1){
   push(((mouse.x/cw)*w),((mouse.y/ch)*h),32); // scale mouse to canvasBuf size
}
update();
renderPix();
//showHeight();
ctxB.putImageData(pixels,0,0);
ctx.drawImage(canvasBuf,0,0,cw,ch);

}

//==================================================================================================
// The following code is support code that provides me with a standard interface to various forums.
// It provides a mouse interface, a full screen canvas, and some global often used variable
// like canvas, ctx, mouse, w, h (width and height), globalTime
// This code is not intended to be part of the answer unless specified and has been formated to reduce
// display size. It should not be used as an example of how to write a canvas interface.
// By Blindman67
const U = undefined;const RESIZE_DEBOUNCE_TIME = 100;
var canvas,ctx,mouse,createCanvas,resizeCanvas,setGlobals,globalTime=0,resizeCount = 0;

createCanvas = function () { var c,cs; cs = (c = document.createElement("canvas")).style; cs.position = "absolute"; cs.top = cs.left = "0px"; cs.zIndex = 1000; document.body.appendChild(c); return c;}
resizeCanvas = function () {
if (canvas === U) { canvas = createCanvas(); } canvas.width = window.innerWidth; canvas.height = window.innerHeight; ctx = canvas.getContext("2d");
if (typeof setGlobals === "function") { setGlobals(); } if (typeof onResize === "function"){ resizeCount += 1; setTimeout(debounceResize,RESIZE_DEBOUNCE_TIME);}
}
function debounceResize(){ resizeCount -= 1; if(resizeCount <= 0){ onResize();}}
setGlobals = function(){ cw = w = canvas.width; ch = h = canvas.height; mouse.updateBounds(); }
mouse = (function(){
function preventDefault(e) { e.preventDefault(); }
var mouse = {
    x : 0, y : 0, w : 0, alt : false, shift : false, ctrl : false, buttonRaw : 0, over : false, bm : [1, 2, 4, 6, 5, 3],
    active : false,bounds : null, crashRecover : null, mouseEvents : "mousemove,mousedown,mouseup,mouseout,mouseover,mousewheel,DOMMouseScroll".split(",")
};
var m = mouse;
function mouseMove(e) {
    var t = e.type;
    m.x = e.clientX - m.bounds.left; m.y = e.clientY - m.bounds.top;
    m.alt = e.altKey; m.shift = e.shiftKey; m.ctrl = e.ctrlKey;
    if (t === "mousedown") { m.buttonRaw |= m.bm[e.which-1]; }
    else if (t === "mouseup") { m.buttonRaw &= m.bm[e.which + 2]; }
    else if (t === "mouseout") { m.buttonRaw = 0; m.over = false; }
    else if (t === "mouseover") { m.over = true; }
    else if (t === "mousewheel") { m.w = e.wheelDelta; }
    else if (t === "DOMMouseScroll") { m.w = -e.detail; }
    if (m.callbacks) { m.callbacks.forEach(c => c(e)); }
    if((m.buttonRaw & 2) && m.crashRecover !== null){ if(typeof m.crashRecover === "function"){ setTimeout(m.crashRecover,0);}}
    e.preventDefault();
}
m.updateBounds = function(){
    if(m.active){
        m.bounds = m.element.getBoundingClientRect();
    }

}
m.addCallback = function (callback) {
    if (typeof callback === "function") {
        if (m.callbacks === U) { m.callbacks = [callback]; }
        else { m.callbacks.push(callback); }
    } else { throw new TypeError("mouse.addCallback argument must be a function"); }
}
m.start = function (element, blockContextMenu) {
    if (m.element !== U) { m.removeMouse(); }
    m.element = element === U ? document : element;
    m.blockContextMenu = blockContextMenu === U ? false : blockContextMenu;
    m.mouseEvents.forEach( n => { m.element.addEventListener(n, mouseMove); } );
    if (m.blockContextMenu === true) { m.element.addEventListener("contextmenu", preventDefault, false); }
    m.active = true;
    m.updateBounds();
}
m.remove = function () {
    if (m.element !== U) {
        m.mouseEvents.forEach(n => { m.element.removeEventListener(n, mouseMove); } );
        if (m.contextMenuBlocked === true) { m.element.removeEventListener("contextmenu", preventDefault);}
        m.element = m.callbacks = m.contextMenuBlocked = U;
        m.active = false;
    }
}
return mouse;
})();
function main(timer){ // Main update loop
globalTime = timer;
display();  // call demo code
requestAnimationFrame(main);
}
resizeCanvas();
mouse.start(canvas,true);
window.addEventListener("resize",resizeCanvas);
requestAnimationFrame(main);
body {
  background:#49D;
}
.help {
  text-align : center;
  font-family : Arial,"Helvetica Neue",Helvetica,sans-serif;
  font-size : 18px;
}

Right mouse to drop sand, left button to push it around.


text:
Q: SIGSEGV from Kernel I am having a linked list of nodes modified(added and deleted) dynamically from a user space program. What are the chances that the allocation of nodes are always in user space?
Usual Linked list node,
struct node {
  int x;
  struct node *next;
};

I got this question since when I traverse through the list, the kernel sends a SIGSEGV signal to this user space process.Also I have done the NULL pointer check whenever I create a new node.
Also I know that the kernel sends SIGSEGV signal if the process tries accessing kernel memory.

A: SIGSEGV is a segmentation fault. What this means is that your program is trying to access memory in a region that is not inside of the programs allotted memory range, or segment. Segmentation was an ugly system that people used to have to deal with directly, but not so much anymore. What it means for you today is that your code is most likely deferencing either a null pointer or some un-initialized value.
What you should do is hook your debugger up to your program and see what address is causing the SIGSEGV to be thrown. As soon as you see it, it will most likely be 0x0, or some garbage value such as 0xDEADC0DE or something.
Probably you are not setting all the pointers in your nodes to 0x0. Do this inside the constructor and double check your addNode() removeNode() function to be sure you don't have dangling pointers hanging around.

A: Do not forget to set next=NULL; in node constructor, or just after node creation.
And yes, allocation is always done in user space

A:
What are the chances that the allocation of nodes are always in user space?

100% certain.

when I traverse through the list, the kernel sends a SIGSEGV signal

It means there is a bug in your program trying to access la-la-land.

Also I know that the kernel sends SIGSEGV signal if the process tries accessing kernel memory.

SIGSEGV is a POSIX standard signal sent when the program tries to access forbidden memory. So not necessarily just kernel memory, but rather any memory anywhere outside the space that your process has access to.