问:
Does std::vector satisfy the container requirements for Boost.Interprocess allocators?
In boost::interprocess documentation it is said as requirement for containers to be stored in shared memory:

STL containers may not assume that memory allocated with an allocator can be deallocated with other allocators of the same type. All allocators objects must compare equal only if memory allocated with one object can be deallocated with the other one, and this can only tested with operator==() at run-time.

Containers' internal pointers should be of the type allocator::pointer and containers may not assume allocator::pointer is a raw pointer.

All objects must be constructed-destroyed via allocator::construct and allocator::destroy functions.

I am using gcc 4.7.1 with -std=c++11 (and boost 1.53). Is it safe to use the below defined ShmVector type?

```<code>typedef boost::interprocess::allocator<int,
    boost::interprocess::managed_shared_memory::segment_manager>  ShmemAllocator;
typedef std::vector<int, ShmemAllocator> ShmVector;
</code>```

I tried a dummy process which uses this type, and it looks it is working, but I am still not sure that the vector in gcc4.7.1 does satisfy all the requirements. I am especially not sure about the first requirement.

```<code>#include <iostream>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <vector>
#include <cstdlib> //std::system

typedef boost::interprocess::allocator<int,
        boost::interprocess::managed_shared_memory::segment_manager>  ShmemAllocator;
typedef std::vector<int, ShmemAllocator> ShmVector;

int main(int argc, char *argv[])
{
    if(argc == 1){ //Parent process

        struct shm_remove
        {
            shm_remove() { boost::interprocess::shared_memory_object::remove("MySharedMemory"); }
            ~shm_remove(){ boost::interprocess::shared_memory_object::remove("MySharedMemory"); }
        } remover;

        //Create a new segment with given name and size
        boost::interprocess::managed_shared_memory segment(boost::interprocess::create_only,
                "MySharedMemory", 65536);

        //Initialize shared memory STL-compatible allocator
        const ShmemAllocator allocator(segment.get_segment_manager());

        ShmVector* v = segment.construct<ShmVector>("ShmVector")(allocator);
        v->push_back(1); v->push_back(2); v->push_back(3);

        //Launch child process
        std::string s(argv[0]); s += " child ";
        if(0 != std::system(s.c_str()))
            return 1;

    } else { // Child process

        //Open the managed segment
        boost::interprocess::managed_shared_memory segment(
                boost::interprocess::open_only, "MySharedMemory");

        //Find the vector using the c-string name
        ShmVector *v = segment.find<ShmVector>("ShmVector").first;

        for (const auto& i : *v) {
            std::cout << i << " ";
        }
        std::cout << std::endl;

    }
}
</code>```

答:
I think the answer is no. Because in practice (in C++98) and in theory (C++11 standard), std::vector pointer cannot be something else than T*.

That is why boost::interprocess::vector<T> uses boost::container::vector<T, boost::interprocess::allocator<T>> (instead of std::vector<T, boost::interprocess::allocator<T>>).

Body:
In boost::interprocess documentation it is said as requirement for containers to be stored in shared memory:

STL containers may not assume that memory allocated with an allocator can be deallocated with other allocators of the same type. All allocators objects must compare equal only if memory allocated with one object can be deallocated with the other one, and this can only tested with operator==() at run-time.

Containers' internal pointers should be of the type allocator::pointer and containers may not assume allocator::pointer is a raw pointer.

All objects must be constructed-destroyed via allocator::construct and allocator::destroy functions.

I am using gcc 4.7.1 with -std=c++11 (and boost 1.53). Is it safe to use the below defined ShmVector type?

typedef boost::interprocess::allocator<int,
    boost::interprocess::managed_shared_memory::segment_manager>  ShmemAllocator;
typedef std::vector<int, ShmemAllocator> ShmVector;

I tried a dummy process which uses this type, and it looks it is working, but I am still not sure that the vector in gcc4.7.1 does satisfy all the requirements. I am especially not sure about the first requirement.

#include <iostream>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <vector>
#include <cstdlib> //std::system

typedef boost::interprocess::allocator<int,
        boost::interprocess::managed_shared_memory::segment_manager>  ShmemAllocator;
typedef std::vector<int, ShmemAllocator> ShmVector;

int main(int argc, char *argv[])
{
    if(argc == 1){ //Parent process

        struct shm_remove
        {
            shm_remove() { boost::interprocess::shared_memory_object::remove("MySharedMemory"); }
            ~shm_remove(){ boost::interprocess::shared_memory_object::remove("MySharedMemory"); }
        } remover;

        //Create a new segment with given name and size
        boost::interprocess::managed_shared_memory segment(boost::interprocess::create_only,
                "MySharedMemory", 65536);

        //Initialize shared memory STL-compatible allocator
        const ShmemAllocator allocator(segment.get_segment_manager());

        ShmVector* v = segment.construct<ShmVector>("ShmVector")(allocator);
        v->push_back(1); v->push_back(2); v->push_back(3);

        //Launch child process
        std::string s(argv[0]); s += " child ";
        if(0 != std::system(s.c_str()))
            return 1;

    } else { // Child process

        //Open the managed segment
        boost::interprocess::managed_shared_memory segment(
                boost::interprocess::open_only, "MySharedMemory");

        //Find the vector using the c-string name
        ShmVector *v = segment.find<ShmVector>("ShmVector").first;

        for (const auto& i : *v) {
            std::cout << i << " ";
        }
        std::cout << std::endl;

    }
}


来源: stackexchange

问:
Zip two file contents having related timestamp column to create a list in python
I have two files containing timestamp column with 1000+ rows. Row in file f1 is related to the row in file f2. I wanted a Python script to do [f1 nth row,f2 nth row] for all corresponding rows in the best way possible. Thanks!

```<code>f1:
05:43:44
05:59:32

f2:
05:43:51
05:59:39
</code>```

e.g. [05:43:44,05:43:51], [05:59:32,05:59:39] ....

答:
Something like this is probably the most intuitive approach.

```<code>#!/usr/bin/python3
with open("f1.txt") as f1:
  with open("f2.txt") as f2:
    for row1 in f1:
      for row2 in f2:
        print("%s %s" % (row1.strip(), row2.strip()))
</code>```

Some might prefer a list comprehension, but non-pythonistas may not consider it intuitive.

```<code>with open("f1.txt") as f1:
  with open("f2.txt") as f2:
    print("\n".join([
      "%s %s" % (row1.strip(), row2.strip())
      for row1 in f1
      for row2 in f2
    ]))
</code>```

ContentLicense: CC BY-SA 3.0

Body:
I have two files containing timestamp column with 1000+ rows. Row in file f1 is related to the row in file f2. I wanted a Python script to do [f1 nth row,f2 nth row] for all corresponding rows in the best way possible. Thanks!

f1:
05:43:44
05:59:32

f2:
05:43:51
05:59:39

e.g. [05:43:44,05:43:51], [05:59:32,05:59:39] ....

Body:
Something like this is probably the most intuitive approach.

#!/usr/bin/python3
with open("f1.txt") as f1:
  with open("f2.txt") as f2:
    for row1 in f1:
      for row2 in f2:
        print("%s %s" % (row1.strip(), row2.strip()))

Some might prefer a list comprehension, but non-pythonistas may not consider it intuitive.

with open("f1.txt") as f1:
  with open("f2.txt") as f2:
    print("\n".join([
      "%s %s" % (row1.strip(), row2.strip())
      for row1 in f1
      for row2 in f2
    ]))


问:
Setting stdout to non-blocking in python
Prior warning: I'm hacking around here out of curiosity. I have no specific reason to do what I'm doing below!

Below is done on Python 2.7.13 on MacOS 10.12.5

I was hacking around with python and I thought it'd be interesting to see what happened if I made stdout nonblocking

```<code>fcntl.fcntl(sys.stdout.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
</code>```

The call to fcntl is definitely successful. I then try to write a large amount of data (bigger than the max buffer size of a pipe on OSX - which is 65536 bytes). I do it in a variety of ways and get different outcomes, sometimes an exception, sometimes what seems to be a hard fail.

Case 1

```<code>fcntl.fcntl(sys.stdout.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
try:
    sys.stdout.write("A" * 65537)
except Exception as e:
    time.sleep(1)
    print "Caught: {}".format(e)

# Safety sleep to prevent quick exit
time.sleep(1)
</code>```

This always throws the exception Caught: [Errno 35] Resource temporarily unavailable. Makes sense I think. Higher level file object wrapper is telling me the write call failed.

Case 2

```<code>fcntl.fcntl(sys.stdout.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
try:
    sys.stdout.write("A" * 65537)
except Exception as e:
    print "Caught: {}".format(e)

# Safety sleep to prevent quick exit
time.sleep(1)
</code>```

This sometimes throws the exception Caught: [Errno 35] Resource temporarily unavailable or sometimes there is no exception caught and I see the following output:

```<code>close failed in file object destructor:
sys.excepthook is missing
lost sys.stderr
</code>```

Case 3

```<code>fcntl.fcntl(sys.stdout.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
try:
    sys.stdout.write("A" * 65537)
except Exception as e:
    print "Caught: {}".format(e)

# Safety sleep to prevent quick exit
time.sleep(1)

print "Slept"
</code>```

This sometimes throws the exception Caught: [Errno 35] Resource temporarily unavailable or sometimes there is no exception caught and I just see "Slept". It seems that by printing "Slept" I don't get the error message from Case 2.

Case 4

```<code>fcntl.fcntl(sys.stdout.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
try:
    os.write(sys.stdout.fileno(), "A" * 65537)
except Exception as e:
    print "Caught: {}".format(e)

# Safety sleep to prevent quick exit
time.sleep(1)
</code>```

Always okay!

Case 5

```<code>fcntl.fcntl(sys.stdout.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
try:
    print os.write(sys.stdout.fileno(), "A" * 65537)
except Exception as e:
    print "Caught: {}".format(e)

# Safety sleep to prevent quick exit
time.sleep(1)
</code>```

This is sometimes okay or sometimes prints the close failed in file object destructor error message.

My question is, why does this fail like this in python? Am I doing something fundamentally bad here - either with python or at the system level?

It seems like somehow that writing too soon to stdout when the write already failed causes the error message. The error doesn't appear to be an exception. No idea where it's coming from.

N.B. I can write the equivalent program in C and it works okay:

```<code>#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <sys/fcntl.h>
#include <unistd.h>

int main(int argc, const char * argv[])
{
    const size_t NUM_CHARS = 65537;
    char buf[NUM_CHARS];

    // Set stdout non-blocking
    fcntl(fileno(stdout), F_SETFL, O_NONBLOCK);

    // Try to write a large amount of data
    memset(buf, 65, NUM_CHARS);
    size_t written = fwrite(buf, 1, NUM_CHARS, stdout);

    // Wait briefly to give stdout a chance to be read from
    usleep(1000);

    // This will be written correctly
    sprintf(buf, "\nI wrote %zd bytes\n", written);
    fwrite(buf, 1, strlen(buf), stdout);
    return 0;
}
</code>```

答:
This is interesting. There are a few things that I've found so far:

Case 1

This is because sys.stdout.write will either write all of the string or throw an exception, which isn't the desired behavior when using O_NONBLOCK. When the underlying call to  write returns EAGAIN (Errno 35 on OS X), it should be tried again with the data that is remaining. os.write should be used instead, and the return value should be checked to make sure all the data is written.

This code works as expected:

```<code>fcntl.fcntl(sys.stdout.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)

def stdout_write(s):
    written = 0
    while written < len(s):
        try:
            written = written + os.write(sys.stdout.fileno(), s[written:])
        except OSError as e:
            pass

stdout_write("A" * 65537)
</code>```

Case 2

I suspect that this error message is due to
https://bugs.python.org/issue11380:

```<code>close failed in file object destructor:
sys.excepthook is missing
lost sys.stderr
</code>```

I'm not sure why it's sometimes being called. It may be because there is a print in the except statement which is trying to use the same stdout that a write just failed on.

Case 3

This is similar to Case 1. This code always works for me:

```<code>fcntl.fcntl(sys.stdout.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)

def stdout_write(s):
    written = 0
    while written < len(s):
        try:
            written = written + os.write(sys.stdout.fileno(), s[written:])
        except OSError as e:
            pass

stdout_write("A" * 65537)

time.sleep(1)

print "Slept"
</code>```

Case 4

Make sure you check the return value of os.write, I suspect that the full 65537 bytes are not being successfully written.

Case 5

This is similar to Case 2.

Body:
Prior warning: I'm hacking around here out of curiosity. I have no specific reason to do what I'm doing below!

Below is done on Python 2.7.13 on MacOS 10.12.5

I was hacking around with python and I thought it'd be interesting to see what happened if I made stdout nonblocking

fcntl.fcntl(sys.stdout.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)

The call to fcntl is definitely successful. I then try to write a large amount of data (bigger than the max buffer size of a pipe on OSX - which is 65536 bytes). I do it in a variety of ways and get different outcomes, sometimes an exception, sometimes what seems to be a hard fail.

Case 1

fcntl.fcntl(sys.stdout.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
try:
    sys.stdout.write("A" * 65537)
except Exception as e:
    time.sleep(1)
    print "Caught: {}".format(e)

# Safety sleep to prevent quick exit
time.sleep(1)

This always throws the exception Caught: [Errno 35] Resource temporarily unavailable. Makes sense I think. Higher level file object wrapper is telling me the write call failed.

Case 2

fcntl.fcntl(sys.stdout.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
try:
    sys.stdout.write("A" * 65537)
except Exception as e:
    print "Caught: {}".format(e)

# Safety sleep to prevent quick exit
time.sleep(1)

This sometimes throws the exception Caught: [Errno 35] Resource temporarily unavailable or sometimes there is no exception caught and I see the following output:

close failed in file object destructor:
sys.excepthook is missing
lost sys.stderr

Case 3

fcntl.fcntl(sys.stdout.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
try:
    sys.stdout.write("A" * 65537)
except Exception as e:
    print "Caught: {}".format(e)

# Safety sleep to prevent quick exit
time.sleep(1)

print "Slept"

This sometimes throws the exception Caught: [Errno 35] Resource temporarily unavailable or sometimes there is no exception caught and I just see "Slept". It seems that by printing "Slept" I don't get the error message from Case 2.

Case 4

fcntl.fcntl(sys.stdout.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
try:
    os.write(sys.stdout.fileno(), "A" * 65537)
except Exception as e:
    print "Caught: {}".format(e)

# Safety sleep to prevent quick exit
time.sleep(1)

Always okay!

Case 5

fcntl.fcntl(sys.stdout.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
try:
    print os.write(sys.stdout.fileno(), "A" * 65537)
except Exception as e:
    print "Caught: {}".format(e)

# Safety sleep to prevent quick exit
time.sleep(1)

This is sometimes okay or sometimes prints the close failed in file object destructor error message.

My question is, why does this fail like this in python? Am I doing something fundamentally bad here - either with python or at the system level?

It seems like somehow that writing too soon to stdout when the write already failed causes the error message. The error doesn't appear to be an exception. No idea where it's coming from.

N.B. I can write the equivalent program in C and it works okay:

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <sys/fcntl.h>
#include <unistd.h>

int main(int argc, const char * argv[])
{
    const size_t NUM_CHARS = 65537;
    char buf[NUM_CHARS];

    // Set stdout non-blocking
    fcntl(fileno(stdout), F_SETFL, O_NONBLOCK);

    // Try to write a large amount of data
    memset(buf, 65, NUM_CHARS);
    size_t written = fwrite(buf, 1, NUM_CHARS, stdout);

    // Wait briefly to give stdout a chance to be read from
    usleep(1000);

    // This will be written correctly
    sprintf(buf, "\nI wrote %zd bytes\n", written);
    fwrite(buf, 1, strlen(buf), stdout);
    return 0;
}

Body:
This is interesting. There are a few things that I've found so far:

Case 1

This is because sys.stdout.write will either write all of the string or throw an exception, which isn't the desired behavior when using O_NONBLOCK. When the underlying call to  write returns EAGAIN (Errno 35 on OS X), it should be tried again with the data that is remaining. os.write should be used instead, and the return value should be checked to make sure all the data is written.

This code works as expected:

fcntl.fcntl(sys.stdout.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)

def stdout_write(s):
    written = 0
    while written < len(s):
        try:
            written = written + os.write(sys.stdout.fileno(), s[written:])
        except OSError as e:
            pass

stdout_write("A" * 65537)

Case 2

I suspect that this error message is due to
https://bugs.python.org/issue11380:

close failed in file object destructor:
sys.excepthook is missing
lost sys.stderr

I'm not sure why it's sometimes being called. It may be because there is a print in the except statement which is trying to use the same stdout that a write just failed on.

Case 3

This is similar to Case 1. This code always works for me:

fcntl.fcntl(sys.stdout.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)

def stdout_write(s):
    written = 0
    while written < len(s):
        try:
            written = written + os.write(sys.stdout.fileno(), s[written:])
        except OSError as e:
            pass

stdout_write("A" * 65537)

time.sleep(1)

print "Slept"

Case 4

Make sure you check the return value of os.write, I suspect that the full 65537 bytes are not being successfully written.

Case 5

This is similar to Case 2.


问:
what is happening with my code when I try to insert my array ids?
I am trying to do a table for notifications using codeigniter and node.js for push notifications from database ,but however I did an array which contains my product ids from my session cart , but one I send them through my function looks like this on error output

```<code>INSERT INTO storelte_notifications (message,type,product_id,user_id,timestamp) VALUES('low stock', 2, ('4169','4170','4173','4172','4175'), '1', 1499414552)
</code>```

how could I send all together , but not as I am sending I want to send my ids and then store them together with my notifications table  my code looks like  this

notifications function

```<code>public function index()
    {
        $this->session->carrito = $this->sale->checar_existe_carrito();
        $array = $this->sale->get_all_cart($this->session->carrito);
        $product_id = array();
        foreach ($array as $key => $value) {
             $product_id[] = $value['id'];
        }
        $this->json($product_id);
        $this->notification->addNotification('low stock', $product_id, $this->session->log['id'], 'low stock',now());
    }
</code>```

model

insert notifications

```<code>public function addNotification($message, $product_id, $user_id, $type = ''){
    $types = array('new' => 0, 'pending' => 1, 'low stock' => 2);
    if (isset($types[$type]) === false) {
        throw new \InvalidArgumentException('Value for third parameter must be one of new, pending, or low stock.');
    }
    $type = $types[$type];
    $timestamp = time();
    $query = "SELECT COUNT(*) AS notificationCount FROM storelte_notifications WHERE product_id IN ? AND type = ? ";
    $previousNotification = $this->db->query($query, array($product_id, $type))->result_array();
    if ($previousNotification[0]['notificationCount'] == 0) {
        $sql = "INSERT INTO storelte_notifications (message,type,product_id,user_id,timestamp) VALUES(?, ?, ?, ?, ?)";
        try {
            if ($this->db->query($sql, array($message, $type, $product_id, $user_id, $timestamp))) {
                return true;
            }else{
                return false;
            }
        } catch (Exception $e) {

        }
    }else{
        return true;
    }
}
</code>```

答:
maybe you could write sql like this:

```<code>insert into storelte_notifications (message,type,product_id,user_id,timestamp)
values ('low stock', 2, '4169', '1', 1499414552),('low stock', 2, '4170', '1', 1499414552)
</code>```

or you could use foreach

Body:
I am trying to do a table for notifications using codeigniter and node.js for push notifications from database ,but however I did an array which contains my product ids from my session cart , but one I send them through my function looks like this on error output

INSERT INTO storelte_notifications (message,type,product_id,user_id,timestamp) VALUES('low stock', 2, ('4169','4170','4173','4172','4175'), '1', 1499414552)

how could I send all together , but not as I am sending I want to send my ids and then store them together with my notifications table  my code looks like  this

notifications function

public function index()
    {
        $this->session->carrito = $this->sale->checar_existe_carrito();
        $array = $this->sale->get_all_cart($this->session->carrito);
        $product_id = array();
        foreach ($array as $key => $value) {
             $product_id[] = $value['id'];
        }
        $this->json($product_id);
        $this->notification->addNotification('low stock', $product_id, $this->session->log['id'], 'low stock',now());
    }

model

insert notifications

public function addNotification($message, $product_id, $user_id, $type = ''){
    $types = array('new' => 0, 'pending' => 1, 'low stock' => 2);
    if (isset($types[$type]) === false) {
        throw new \InvalidArgumentException('Value for third parameter must be one of new, pending, or low stock.');
    }
    $type = $types[$type];
    $timestamp = time();
    $query = "SELECT COUNT(*) AS notificationCount FROM storelte_notifications WHERE product_id IN ? AND type = ? ";
    $previousNotification = $this->db->query($query, array($product_id, $type))->result_array();
    if ($previousNotification[0]['notificationCount'] == 0) {
        $sql = "INSERT INTO storelte_notifications (message,type,product_id,user_id,timestamp) VALUES(?, ?, ?, ?, ?)";
        try {
            if ($this->db->query($sql, array($message, $type, $product_id, $user_id, $timestamp))) {
                return true;
            }else{
                return false;
            }
        } catch (Exception $e) {

        }
    }else{
        return true;
    }
}

Body:
maybe you could write sql like this:

insert into storelte_notifications (message,type,product_id,user_id,timestamp)
values ('low stock', 2, '4169', '1', 1499414552),('low stock', 2, '4170', '1', 1499414552)

or you could use foreach


问:
Get rid of scientific notation
I need to do some calculations but I'm getting a problem with values which are very low, for example, I need to get the 2.7% of 0.005 and I end up with 1.3500000000000003e-4 which is not what I'm looking for, I just need to know how can I get an accurate percetage of those values, what I'm doing right now is <value> * 2.7 / 100 which works great for integers or floats greater than 0.05.

For example, the one that I need which is 2.7% of 0.005 needs to be shown as 0.000135.

答:
First, understand that the language isn't broken, it's just that computers are just really bad at doing floating point math.

So, in a sense 1.3500000000000003e-4 is accurate, but your issue here is that Elixir prints the (very small and very large) floats in exponent notation. There are a few ways you can print it as 0.000135:

Use Erlang's float_to_binary:

```<code>:erlang.float_to_binary(0.005 * 2.7 / 100, [:compact, {:decimals, 10}])
#=> "0.000135"
</code>```

Use :io.format:

```<code>:io.format("~f~n",[0.005 * 2.7 / 100])
#=> "0.000135"
</code>```

Or use exprintf which is a nice Elixir wrapper around Erlang's :io module

Notice the result is a string and not a number in the above examples - since you're just formatting / printing it in decimal.

Title: Get rid of scientific notation

Body:
First, understand that the language isn't broken, it's just that computers are just really bad at doing floating point math.

So, in a sense 1.3500000000000003e-4 is accurate, but your issue here is that Elixir prints the (very small and very large) floats in exponent notation. There are a few ways you can print it as 0.000135:

Use Erlang's float_to_binary:

:erlang.float_to_binary(0.005 * 2.7 / 100, [:compact, {:decimals, 10}])
#=> "0.000135"

Use :io.format:

:io.format("~f~n",[0.005 * 2.7 / 100])
#=> "0.000135"

Or use exprintf which is a nice Elixir wrapper around Erlang's :io module

Notice the result is a string and not a number in the above examples - since you're just formatting / printing it in decimal.

ContentLicense: CC BY-SA 4.0


问:
kubernetes dial tcp i/o timeout error in version 1.7.0. Also kubeadm join command fails
I am new to Kubernetes, so some of my questions may be basic.

NOTE: REMOVED http::// and https::// URL references in commands and output below, since there is a limit to number of URLs in a question.

My setup:
1 Physical host machine(running Ubuntu 16.04), with bridge networking enabled.

2 Ubuntu 16.04 VMs(Virtual Machines), VM1 is Master Node. VM2 is Slave Node.

I have a router, so behind the router both VMs get local IP address(ie not public IP address).
Since I am on corporate network, I also have proxy settings.
I have browser, apt, curl and wget applications working fine. Able to ping between VM1 and VM2.

Kubernetes Version: 1.7.0 on both Master Node(Virtual Machine-VM1) and Slave Node(Virtual Machine-VM2)

My Steps:
1. On both Master and Slave Nodes, installed the required kubernetes(kubelet kubeadm kubectl kubernetes-cni) packages and docker(docker.io) packages.

On the Master Node:
1. On the Master Node, when I run kubeadmin init, I was getting the following tcp timeout error:

sudo kubeadm init --apiserver-advertise-address=192.168.1.104 --pod-network-cidr=10.244.0.0/16 –skip-preflight- -checks

[kubeadm] WARNING: kubeadm is in beta, please do not use it for production clusters.
unable to get URL "storage.googleapis.com/kubernetes-release/release/stable-1.7.txt": Get storage.googleapis.com/kubernetes-release/release/stable-1.7.txt: dial tcp 172.217.3.208:443: i/o timeout

So tried specifying the kubernetes version, since I read that this prevents fetch from external website, and with that kubeadmin init was successful.

sudo kubeadm init --kubernetes-version v1.7.0 --apiserver-advertise-address=192.168.1.104 --pod-network-cidr=10.244.0.0/16 --skip-preflight-checks

[kubeadm] WARNING: kubeadm is in beta, please do not use it for production clusters.
[init] Using Kubernetes version: v1.7.0

[init] Using Authorization modes: [Node RBAC]

[preflight] Skipping pre-flight checks

[certificates] Using the existing CA certificate and key.

[certificates] Using the existing API Server certificate and key.

[certificates] Using the existing API Server kubelet client certificate and key.

[certificates] Using the existing service account token signing key.

[certificates] Using the existing front-proxy CA certificate and key.

[certificates] Using the existing front-proxy client certificate and key.

[certificates] Valid certificates and keys now exist in "/etc/kubernetes/pki"

[kubeconfig] Using existing up-to-date KubeConfig file: "/etc/kubernetes/admin.conf"

[kubeconfig] Using existing up-to-date KubeConfig file: "/etc/kubernetes/kubelet.conf"

[kubeconfig] Using existing up-to-date KubeConfig file: "/etc/kubernetes/controller-manager.conf"

[kubeconfig] Using existing up-to-date KubeConfig file: "/etc/kubernetes/scheduler.conf"

[apiclient] Created API client, waiting for the control plane to become ready

[apiclient] All control plane components are healthy after 14.009367 seconds

[token] Using token: ec4877.23c06ac2adf9d66c
[apiconfig] Created RBAC rules

[addons] Applied essential addon: kube-proxy

[addons] Applied essential addon: kube-dns

Your Kubernetes master has initialized successfully!

To start using your cluster, you need to run (as a regular user):

mkdir -p $HOME/.kube

sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config

sudo chown $(id -u):$(id -g) $HOME/.kube/config

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  kubernetes.io/docs/admin/addons/

You can now join any number of machines by running the following on each node
as root:

kubeadm join --token ec4877.23c06ac2adf9d66c 192.168.1.104:6443

Ran the below commands and they went through fine.
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

Tried to deploy a pod network to the cluster, but fails with the same tcp timeout error:

kubectl apply -f
docs.projectcalico.org/v2.3/ getting-started/kubernetes/installation/hosted/kubeadm/1.6/calico.yaml

Unable to connect to the server: dial tcp 151.101.0.133:80: i/o timeout

Downloaded the calico.yaml file using browser, and ran the command, it was successful.

skris14@skris14-ubuntu16:~/Downloads$ sudo kubectl apply -f ~/Downloads/calico.yaml

configmap "calico-config" created

daemonset "calico-etcd" created

service "calico-etcd" created

daemonset "calico-node" created

deployment "calico-policy-controller" created

clusterrolebinding "calico-cni-plugin" created

clusterrole "calico-cni-plugin" created

serviceaccount "calico-cni-plugin" created

clusterrolebinding "calico-policy-controller" created

clusterrole "calico-policy-controller" created

serviceaccount "calico-policy-controller" created

On the Slave Node:

Note: I am able to do a basic ping test, and ssh, scp commands between the master node running in VM1 and slave node running in VM2 works fine.

Ran the join command, and it fails trying to get cluster info.

Output of join command in slave node:
skris14@sudha-ubuntu-16:~$ sudo kubeadm join --token ec4877.23c06ac2adf9d66c 192.168.1.104:6443

[sudo] password for skris14:

[kubeadm] WARNING: kubeadm is in beta, please do not use it for production clusters.

[preflight] Running pre-flight checks

[discovery] Trying to connect to API Server "192.168.1.104:6443"

[discovery] Created cluster-info discovery client, requesting info from "192.168.1.104:6443"

[discovery] Failed to request cluster info, will try again: [Get 192.168.1.104:6443/: EOF]
^C

Output of Status Commands On the Master Node:

skris14@skris14-ubuntu16:~/Downloads$
kubectl get nodes

NAME                      STATUS    AGE       VERSION
skris14-ubuntu16.04-vm1   Ready     5d        v1.7.0

skris14@skris14-ubuntu16:~/Downloads$ kubectl cluster-info

Kubernetes master is running at 192.168.1.104:6443

KubeDNS is running at 192.168.1.104:6443/api/v1/namespaces/kube-system/services/kube-dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

skris14@skris14-ubuntu16:~/Downloads$ kubectl get pods --namespace=kube-system -o wide

NAME                                              READY     STATUS     RESTARTS   AGE       IP              NODE

calico-etcd-2lt0c                                 1/1       Running    0          14m       192.168.1.104   skris14-ubuntu16.04-vm1

calico-node-pp1p9                                 2/2       Running    0          14m       192.168.1.104   skris14-ubuntu16.04-vm1

calico-policy-controller-1727037546-m6wqt         1/1       Running    0          14m       192.168.1.104   skris14-ubuntu16.04-vm1

etcd-skris14-ubuntu16.04-vm1                      1/1       Running    1          5d        192.168.1.104   skris14-ubuntu16.04-vm1

kube-apiserver-skris14-ubuntu16.04-vm1            1/1       Running    0          3m        192.168.1.104   skris14-ubuntu16.04-vm1

kube-controller-manager-skris14-ubuntu16.04-vm1   1/1       Running    0          4m        192.168.1.104   skris14-ubuntu16.04-vm1

kube-dns-2425271678-b05v8                         0/3       Pending    0          4m

kube-dns-2425271678-ljsv1                         0/3       OutOfcpu   0          5d                  skris14-ubuntu16.04-vm1

kube-proxy-40zrc                                  1/1       Running    1          5d        192.168.1.104   skris14-ubuntu16.04-vm1

kube-scheduler-skris14-ubuntu16.04-vm1            1/1       Running    5 5d 192.168.1.104   skris14-ubuntu16.04-vm1

Output of ifconfig on Master Node(Virtual Machine1):

skris14@skris14-ubuntu16:~/

docker0   Link encap:Ethernet  HWaddr 02:42:7f:ee:8e:b7
          inet addr:172.17.0.1  Bcast:0.0.0.0  Mask:255.255.0.0
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

ens3      Link encap:Ethernet  HWaddr 52:54:be:36:42:a6
          inet addr:192.168.1.104  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::c60c:647d:1d9d:aca1/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:184500 errors:0 dropped:35 overruns:0 frame:0
          TX packets:92411 errors:0 dropped:0 overruns:0 carrier:0
          collisions:458827 txqueuelen:1000
          RX bytes:242793144 (242.7 MB)  TX bytes:9162254 (9.1 MB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:848277 errors:0 dropped:0 overruns:0 frame:0
          TX packets:848277 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1
          RX bytes:211936528 (211.9 MB)  TX bytes:211936528 (211.9 MB)

tunl0     Link encap:IPIP Tunnel  HWaddr
          inet addr:192.168.112.192  Mask:255.255.255.255
          UP RUNNING NOARP  MTU:1440  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

Output of ifconfig on Slave Node(Virtual Machine2):

skris14@sudha-ubuntu-16:~$ ifconfig
docker0   Link encap:Ethernet  HWaddr 02:42:69:5e:2d:22
          inet addr:172.17.0.1  Bcast:0.0.0.0  Mask:255.255.0.0
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

ens3      Link encap:Ethernet  HWaddr 52:54:be:36:42:b6
          inet addr:192.168.1.105  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::cadb:b714:c679:955/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:72280 errors:0 dropped:0 overruns:0 frame:0
          TX packets:36977 errors:0 dropped:0 overruns:0 carrier:0
          collisions:183622 txqueuelen:1000
          RX bytes:98350159 (98.3 MB)  TX bytes:3431313 (3.4 MB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:1340 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1340 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1
          RX bytes:130985 (130.9 KB)  TX bytes:130985 (130.9 KB)

答:
discovery] Failed to request cluster info, will try again: [Get
  192.168.1.104:6443/: EOF]

your error message shows slave not able to connect to master api server. check these items.

make sure api server running on port 6443

check the routes on both servers.

check the firewall rules on your hosts and router.


问:
How to use linkRadial to draw a link between two points?
I need to manually draw some links between points on a circle and points clustered in the centre of said circle. I have the x,y pairs for the source and target points, but I don't want a simple straight line between them; I want a curved line (similar to the links that are found in tree diagrams). I could use linkHorizontal or linkVertical but their tangents are constant; I want to use linkRadial and have the tangent be the radial line at that particular arc point (the degree of which I also have).

I don't understand the linkRadial API though; it wants an angle and a radius, not an x or y point. How do I convert my two x,y pairs (and the radial line angle) into the angle and radius this is expecting?

答:
Since you have a data array with the x and y positions ("I have the x,y pairs for the source and target points"), you'll have to convert them to angle and radius. Let's see how to do it.

Firstly, let's see an example with fixed coordinates. For instance, suppose you have this data array, with the x and y positions:

```<code>var data = [{
    source: {y: 150,x: 75
    },
    target: {y: 300,x: 0
    }
}, {
    source: {y: 150,x: 75
    },
    target: {y: 0,x: 0
    }
}, {
    source: {y: 150,x: 75
    },
    target: {y: 150,x: 150
    }
}, ];
</code>```

Using this link generator...

```<code>var link = d3.linkHorizontal()
    .x(function(d) {
        return d.y;
    })
    .y(function(d) {
        return d.x;
    });
</code>```

... you'll have a chart like this:

var data = [{
  source: {
    y: 150,
    x: 75
  },
  target: {
    y: 300,
    x: 0
  }
}, {
  source: {
    y: 150,
    x: 75
  },
  target: {
    y: 0,
    x: 0
  }
}, {
  source: {
    y: 150,
    x: 75
  },
  target: {
    y: 150,
    x: 150
  }
}, ];

var svg = d3.select("svg");

var link = d3.linkHorizontal()
  .x(function(d) {
    return d.y;
  })
  .y(function(d) {
    return d.x;
  });

svg.selectAll(null)
  .data(data)
  .enter()
  .append("path")
  .attr("fill", "none")
  .attr("stroke", "blue")
  .attr("d", link);```
<pre class="snippet-code-html lang-html prettyprint-override"><code><script src="https://d3js.org/d3.v4.js"></script>
<svg></svg></code>```

How can we convert this to a data set that can be used with d3.linkRadial()?

One option is iterating with each object, using basic trigonometry to populate the angle and radius properties:

```<code>var radialData = data.map(function(d) {
    return {
        source: {
            x: 0,
            y: 0
        },
        target: {
            x: Math.atan2(d.target.y - d.source.y, d.target.x - d.source.x) - Math.PI,
            y: Math.sqrt((d.target.x - d.source.x) * (d.target.x - d.source.x) + (d.target.y - d.source.y) * (d.target.y - d.source.y))
        }
    };
});
</code>```

Then, using this link generator:

```<code>var linkRadial = d3.linkRadial()
    .angle(function(d) {
        console.log(d)
        return d.x;
    })
    .radius(function(d) {
        return d.y;
    });
</code>```

We'll have this:

var data = [{
  source: {
    y: 150,
    x: 75
  },
  target: {
    y: 300,
    x: 0
  }
}, {
  source: {
    y: 150,
    x: 75
  },
  target: {
    y: 0,
    x: 0
  }
}, {
  source: {
    y: 150,
    x: 75
  },
  target: {
    y: 150,
    x: 150
  }
}, ];

var svg = d3.select("svg");

var radialData = data.map(function(d) {
  return {
    source: {
      x: 0,
      y: 0
    },
    target: {
      x: Math.atan2(d.target.y - d.source.y, d.target.x - d.source.x) - Math.PI,
      y: Math.sqrt((d.target.x - d.source.x) * (d.target.x - d.source.x) + (d.target.y - d.source.y) * (d.target.y - d.source.y))
    }
  };
});

var g = svg.append("g")
  .attr("transform", "translate(150,75)")

var linkRadial = d3.linkRadial()
  .angle(function(d) {
    return d.x;
  })
  .radius(function(d) {
    return d.y;
  });

g.selectAll(null)
  .data(radialData)
  .enter()
  .append("path")
  .attr("fill", "none")
  .attr("stroke", "red")
  .attr("d", linkRadial);```
<pre class="snippet-code-html lang-html prettyprint-override"><code><script src="https://d3js.org/d3.v4.js"></script>
<svg></svg></code>```

Now both generators together, for comparison:

var data = [{
  source: {
    y: 150,
    x: 75
  },
  target: {
    y: 300,
    x: 0
  }
}, {
  source: {
    y: 150,
    x: 75
  },
  target: {
    y: 0,
    x: 0
  }
}, {
  source: {
    y: 150,
    x: 75
  },
  target: {
    y: 150,
    x: 150
  }
}, ];

var svg = d3.select("svg");

var link = d3.linkHorizontal()
  .x(function(d) {
    return d.y;
  })
  .y(function(d) {
    return d.x;
  });

svg.selectAll(null)
  .data(data)
  .enter()
  .append("path")
  .attr("fill", "none")
  .attr("stroke", "blue")
  .attr("d", link);

var radialData = data.map(function(d) {
  return {
    source: {
      x: 0,
      y: 0
    },
    target: {
      x: Math.atan2(d.target.y - d.source.y, d.target.x - d.source.x) - Math.PI,
      y: Math.sqrt((d.target.x - d.source.x) * (d.target.x - d.source.x) + (d.target.y - d.source.y) * (d.target.y - d.source.y))
    }
  };
});

var g = svg.append("g")
  .attr("transform", "translate(150,75)")

var linkRadial = d3.linkRadial()
  .angle(function(d) {
    return d.x;
  })
  .radius(function(d) {
    return d.y;
  });

g.selectAll(null)
  .data(radialData)
  .enter()
  .append("path")
  .attr("fill", "none")
  .attr("stroke", "red")
  .attr("d", linkRadial);```
<pre class="snippet-code-html lang-html prettyprint-override"><code><script src="https://d3js.org/d3.v4.js"></script>
<svg></svg></code>```

Body:
Since you have a data array with the x and y positions ("I have the x,y pairs for the source and target points"), you'll have to convert them to angle and radius. Let's see how to do it.

Firstly, let's see an example with fixed coordinates. For instance, suppose you have this data array, with the x and y positions:

var data = [{
    source: {y: 150,x: 75
    },
    target: {y: 300,x: 0
    }
}, {
    source: {y: 150,x: 75
    },
    target: {y: 0,x: 0
    }
}, {
    source: {y: 150,x: 75
    },
    target: {y: 150,x: 150
    }
}, ];

Using this link generator...

var link = d3.linkHorizontal()
    .x(function(d) {
        return d.y;
    })
    .y(function(d) {
        return d.x;
    });

... you'll have a chart like this:

var data = [{
  source: {
    y: 150,
    x: 75
  },
  target: {
    y: 300,
    x: 0
  }
}, {
  source: {
    y: 150,
    x: 75
  },
  target: {
    y: 0,
    x: 0
  }
}, {
  source: {
    y: 150,
    x: 75
  },
  target: {
    y: 150,
    x: 150
  }
}, ];

var svg = d3.select("svg");

var link = d3.linkHorizontal()
  .x(function(d) {
    return d.y;
  })
  .y(function(d) {
    return d.x;
  });

svg.selectAll(null)
  .data(data)
  .enter()
  .append("path")
  .attr("fill", "none")
  .attr("stroke", "blue")
  .attr("d", link);

<script src="https://d3js.org/d3.v4.js"></script>
<svg></svg>

How can we convert this to a data set that can be used with d3.linkRadial()?

One option is iterating with each object, using basic trigonometry to populate the angle and radius properties:

var radialData = data.map(function(d) {
    return {
        source: {
            x: 0,
            y: 0
        },
        target: {
            x: Math.atan2(d.target.y - d.source.y, d.target.x - d.source.x) - Math.PI,
            y: Math.sqrt((d.target.x - d.source.x) * (d.target.x - d.source.x) + (d.target.y - d.source.y) * (d.target.y - d.source.y))
        }
    };
});

Then, using this link generator:

var linkRadial = d3.linkRadial()
    .angle(function(d) {
        console.log(d)
        return d.x;
    })
    .radius(function(d) {
        return d.y;
    });

We'll have this:

var data = [{
  source: {
    y: 150,
    x: 75
  },
  target: {
    y: 300,
    x: 0
  }
}, {
  source: {
    y: 150,
    x: 75
  },
  target: {
    y: 0,
    x: 0
  }
}, {
  source: {
    y: 150,
    x: 75
  },
  target: {
    y: 150,
    x: 150
  }
}, ];

var svg = d3.select("svg");

var radialData = data.map(function(d) {
  return {
    source: {
      x: 0,
      y: 0
    },
    target: {
      x: Math.atan2(d.target.y - d.source.y, d.target.x - d.source.x) - Math.PI,
      y: Math.sqrt((d.target.x - d.source.x) * (d.target.x - d.source.x) + (d.target.y - d.source.y) * (d.target.y - d.source.y))
    }
  };
});

var g = svg.append("g")
  .attr("transform", "translate(150,75)")

var linkRadial = d3.linkRadial()
  .angle(function(d) {
    return d.x;
  })
  .radius(function(d) {
    return d.y;
  });

g.selectAll(null)
  .data(radialData)
  .enter()
  .append("path")
  .attr("fill", "none")
  .attr("stroke", "red")
  .attr("d", linkRadial);

<script src="https://d3js.org/d3.v4.js"></script>
<svg></svg>

Now both generators together, for comparison:

var data = [{
  source: {
    y: 150,
    x: 75
  },
  target: {
    y: 300,
    x: 0
  }
}, {
  source: {
    y: 150,
    x: 75
  },
  target: {
    y: 0,
    x: 0
  }
}, {
  source: {
    y: 150,
    x: 75
  },
  target: {
    y: 150,
    x: 150
  }
}, ];

var svg = d3.select("svg");

var link = d3.linkHorizontal()
  .x(function(d) {
    return d.y;
  })
  .y(function(d) {
    return d.x;
  });

svg.selectAll(null)
  .data(data)
  .enter()
  .append("path")
  .attr("fill", "none")
  .attr("stroke", "blue")
  .attr("d", link);

var radialData = data.map(function(d) {
  return {
    source: {
      x: 0,
      y: 0
    },
    target: {
      x: Math.atan2(d.target.y - d.source.y, d.target.x - d.source.x) - Math.PI,
      y: Math.sqrt((d.target.x - d.source.x) * (d.target.x - d.source.x) + (d.target.y - d.source.y) * (d.target.y - d.source.y))
    }
  };
});

var g = svg.append("g")
  .attr("transform", "translate(150,75)")

var linkRadial = d3.linkRadial()
  .angle(function(d) {
    return d.x;
  })
  .radius(function(d) {
    return d.y;
  });

g.selectAll(null)
  .data(radialData)
  .enter()
  .append("path")
  .attr("fill", "none")
  .attr("stroke", "red")
  .attr("d", linkRadial);

<script src="https://d3js.org/d3.v4.js"></script>
<svg></svg>


问:
Adding to an array in main via function argument
I'm not sure if I even worded the title correctly, but basically. I want to know if there is a way to add to the buff array from the hey function using the pointers in the arguments and why does it work if it does?

buf[100].
example:

```<code>int main(){
  char buf[100];
  hey("320244",buf);
  printf("%s", buf);
}
  void hey(char* s, char* result){
  /*
  some code that appends to result using pointers
  do some stuff with s and get the result back in buf without using return.
  */
}
</code>```

答:
I have modified your code with some comments :-</p>

```<code>#define LEN 100 //Use a macro instead of error prone digits in code

void hey(char* s, char* result); //Fwd declaration

int main(){
  char buf[LEN] = {0}; //This will initialize the buffer on stack
  hey("320244",buf);
  printf("%s", buf);
  hey("abc", buf); //Possible future invocation
  printf("%s", buf);
}

void hey(char* s, char* result){
  if(strlen(result) + strlen(s) < LEN ) //This will check buffer overflow
    strcat(result, s); //This will concatenate s into result
  else
    //Do some error handling here
}
</code>```

Body:
I'm not sure if I even worded the title correctly, but basically. I want to know if there is a way to add to the buff array from the hey function using the pointers in the arguments and why does it work if it does?

buf[100].
example:

int main(){
  char buf[100];
  hey("320244",buf);
  printf("%s", buf);
}
  void hey(char* s, char* result){
  /*
  some code that appends to result using pointers
  do some stuff with s and get the result back in buf without using return.
  */
}

Body:
I have modified your code with some comments :-

#define LEN 100 //Use a macro instead of error prone digits in code

void hey(char* s, char* result); //Fwd declaration

int main(){
  char buf[LEN] = {0}; //This will initialize the buffer on stack
  hey("320244",buf);
  printf("%s", buf);
  hey("abc", buf); //Possible future invocation
  printf("%s", buf);
}

void hey(char* s, char* result){
  if(strlen(result) + strlen(s) < LEN ) //This will check buffer overflow
    strcat(result, s); //This will concatenate s into result
  else
    //Do some error handling here
}


问:
I need advice about files and categories in asp.net
I am working on a project for files upload and storage.
One of the tasks is to be able to select in which category you want to upload the documents, for example: "work", "private", etc.
As it is now, I have folders for each user, and each user has one folder for all the documents.
I was thinking to create a sql table and have the categories there, but any idea how to reference them.
If anyone else have some better idea, it would be appreciated.
Thanks</p>

答: You can have a table to define categories. Then you need a table to store metadata and path of the uploaded documents. When user uploads a document he/she will select the category in which to upload it and  you can store the path of document, category, size, type, user id etc. in the second table. This way it will be easier to query for the statistics like number of files in each category, total size of files per user or per category.</p>

ContentLicense: CC BY-SA 2.5


问:
How to make an nginx reverse-proxy load balance between two docker containers?
I try to make an nginx reverse proxy load balance between two containers with the same nodejs app.

The directory structure:

```<code>.
+-- docker-compose.yml
+-- nginx
+-- nodejs
|   +-- index.js
|   +-- …
+-- php
</code>```

docker-compose.yml:

```<code>version: "3.1"

services:

  nginx-proxy:
    image: nginx:alpine
    ports:
      - "8000:80"
    volumes:
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    links:
      - php:php-app
      - nodejs:nodejs-app

  nodejs:
    image: node:alpine
    environment:
      NODE_ENV: production
    working_dir: /home/app
    restart: always
    volumes:
      - ./nodejs:/home/app
    command: ["node", "index.js"]

  php:
    image: php:apache
    volumes:
      - ./php:/var/www/html
</code>```

index.js listens at port 8080

nginx conf default.conf:

```<code>upstream nodejs-upstream {
  server nodejs-app:8080;
}

server {
  listen 80;
  root /srv/www;

  location / {
    try_files $uri @nodejs;
  }

  location @nodejs {
    proxy_pass http://nodejs-upstream:8080;
    proxy_set_header Host $host;
  }

  location /api {
    proxy_pass http://php-app:80/api;
    proxy_set_header Host $host;
  }
}
</code>```

Now I start the app with

```<code>docker-compose up  --scale nodejs=2
</code>```

Does it load-balance?

I don't think so because the two instances of the nodejs app listen on the same port 8080.

How is it possible to make the nginx server load-balance between the two instances of the nodejs app?

Is there a better way to do this?

EDIT 1

I am still curious to know how to do that without jwilder/nginx-proxy. Thanks

EDIT 2

I have something which kind of works with:

default.conf:

```<code>upstream nodejs-upstream {
  server nodejs_1:8080;
  server nodejs_2:8080;
}
</code>```

This works while the two nodejs containers are up. When I docker stop nodejs_2, the app is still available (load-balancing seems to work), but the request can be really slow to end up (up to 1min on localhost). If I restart this container, it works fine again…

答:
Q. Is there a better way to do this?

Yes, IMO. Use jwilder/nginx approach. It is automatically updated and discover any new container, adding it to its balancing pool.

https://github.com/jwilder/nginx-proxy

```<code>version: "3.1"

services:

  nginx-proxy:
    image: jwilder/nginx
    ports:
      - "8000:80"
     volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro

  nodejs:
    image: node:alpine
    environment:
      NODE_ENV: production
      VIRTUAL_ENV: localhost
      VIRTUAL_PORT: 8080
    working_dir: /home/app
    restart: always
    volumes:
      - ../nodejs:/home/app
    command: ["node", "index.js"]
</code>```

Nginx wil be automatically updated when scaling. Note the VIRTUAL_ENV env var in the app service. That var will be read from nginx. And you don't need any further config. (None .conf file)

Body:
I try to make an nginx reverse proxy load balance between two containers with the same nodejs app.

The directory structure:

.
+-- docker-compose.yml
+-- nginx
+-- nodejs
|   +-- index.js
|   +-- …
+-- php

docker-compose.yml:

version: "3.1"

services:

  nginx-proxy:
    image: nginx:alpine
    ports:
      - "8000:80"
    volumes:
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    links:
      - php:php-app
      - nodejs:nodejs-app

  nodejs:
    image: node:alpine
    environment:
      NODE_ENV: production
    working_dir: /home/app
    restart: always
    volumes:
      - ./nodejs:/home/app
    command: ["node", "index.js"]

  php:
    image: php:apache
    volumes:
      - ./php:/var/www/html

index.js listens at port 8080

nginx conf default.conf:

upstream nodejs-upstream {
  server nodejs-app:8080;
}

server {
  listen 80;
  root /srv/www;

  location / {
    try_files $uri @nodejs;
  }

  location @nodejs {
    proxy_pass http://nodejs-upstream:8080;
    proxy_set_header Host $host;
  }

  location /api {
    proxy_pass http://php-app:80/api;
    proxy_set_header Host $host;
  }
}

Now I start the app with

docker-compose up  --scale nodejs=2

Does it load-balance?

I don't think so because the two instances of the nodejs app listen on the same port 8080.

How is it possible to make the nginx server load-balance between the two instances of the nodejs app?

Is there a better way to do this?

EDIT 1

I am still curious to know how to do that without jwilder/nginx-proxy. Thanks

EDIT 2

I have something which kind of works with:

default.conf:

upstream nodejs-upstream {
  server nodejs_1:8080;
  server nodejs_2:8080;
}

This works while the two nodejs containers are up. When I docker stop nodejs_2, the app is still available (load-balancing seems to work), but the request can be really slow to end up (up to 1min on localhost). If I restart this container, it works fine again…

Body:
Q. Is there a better way to do this?

Yes, IMO. Use jwilder/nginx approach. It is automatically updated and discover any new container, adding it to its balancing pool.

https://github.com/jwilder/nginx-proxy

version: "3.1"

services:

  nginx-proxy:
    image: jwilder/nginx
    ports:
      - "8000:80"
     volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro

  nodejs:
    image: node:alpine
    environment:
      NODE_ENV: production
      VIRTUAL_ENV: localhost
      VIRTUAL_PORT: 8080
    working_dir: /home/app
    restart: always
    volumes:
      - ../nodejs:/home/app
    command: ["node", "index.js"]

Nginx wil be automatically updated when scaling. Note the VIRTUAL_ENV env var in the app service. That var will be read from nginx. And you don't need any further config. (None .conf file)


问:
How can I manually initialize an angular 2/4 app
I need to initialize an angular app with the component my-app,
the problem is that this component exists on a html template that is not loaded on page start so I have to manually initialize, otherwise it gives me an error showing this message:
Error: The selector "my-app" did not match any elements

Is that possible?

答:
If you're using SystemJS you can create the element dynamically using document.createElement() and append using appendChild() then call System.import() to initialize the app. This assumes that all necessary application assets and SystemJS configuration has been loaded already:

HTML

```<code><body>
  <div id="app"></div>

  <button type="button" id="foo">Click Me</button>

  <script src="app.js"></script>
</body>
</code>```

JS

```<code>(function() {
  var button = document.getElementById('foo');
  var container = document.getElementById('app');

  button.addEventListener('click', function() {
    var myApp = document.createElement('my-app');
    myApp.textContent = 'Loading...';

    container.appendChild(myApp);

    System.import('app')
      .catch(console.error.bind(console));
  });
})();
</code>```

Here is a plunker demonstrating the functionality. The app element is created, appended, and initialized from within the event handler of the button click.

UPDATE If you're using Webpack and depending on your build/bundling process, you can load the main transpiled/bundle JS file dynamically using a pattern in this question to avoid your error. If you're using @angular/cli this may be more difficult as generated files have identifiers such as main.97e5159ded9c783f627a.bundle.js.

Hopefully that helps!

Body:
If you're using SystemJS you can create the element dynamically using document.createElement() and append using appendChild() then call System.import() to initialize the app. This assumes that all necessary application assets and SystemJS configuration has been loaded already:

HTML

<body>
  <div id="app"></div>

  <button type="button" id="foo">Click Me</button>

  <script src="app.js"></script>
</body>

JS

(function() {
  var button = document.getElementById('foo');
  var container = document.getElementById('app');

  button.addEventListener('click', function() {
    var myApp = document.createElement('my-app');
    myApp.textContent = 'Loading...';

    container.appendChild(myApp);

    System.import('app')
      .catch(console.error.bind(console));
  });
})();

Here is a plunker demonstrating the functionality. The app element is created, appended, and initialized from within the event handler of the button click.

UPDATE If you're using Webpack and depending on your build/bundling process, you can load the main transpiled/bundle JS file dynamically using a pattern in this question to avoid your error. If you're using @angular/cli this may be more difficult as generated files have identifiers such as main.97e5159ded9c783f627a.bundle.js.

Hopefully that helps!


问:
folium.GeoJson (style function) not working as i want
```<code>import folium ,pandas ,json

df=pandas.read_csv('Volcanoes_2.txt')

def colors(elev):
    minimum=int(min(df['ELEV']))
    step=int(max((df['ELEV'])-min(df['ELEV']))/3)
    if elev in range (minimum,minimum+step):
        col= "green"
    elif elev in range(minimum+step,minimum+step*2):
        col= "orange"
    else:
        col= "red"
    return col

map_1=folium.Map(location=[df['LAT'].mean(), df['LON'].mean()] ,
zoom_start=6,tiles='mapbox bright')

for name, lon, lat, elev in zip(df['NAME'], df['LON'], df['LAT'],
df['ELEV'] ):
    folium.Marker([lat, lon], popup= name,
     icon = folium.Icon(color =colors(elev))).add_to(map_1)

folium.GeoJson(open('world_geojson.json'),
           name='geojson',
           style_function= lambda x :{'fillcolor':'green' if \
  x['properties']['POP2005']<10000000 \
       else 'orange' if 10000000 <x['properties']['POP2005']>20000000 else 'red'},
       ).add_to(map_1)

folium.LayerControl().add_to(map_1)

map_1.save("map.html")
</code>```

this is the map file https://github.com/xxspider4/new_repo/blob/master/map.html

this is the json file https://github.com/xxspider4/new_repo/blob/master/world_geojson.json

答:
You were very close.  I was able to get it to work by changing fillcolor to fillColor in your style function

lambda x :{'fillColor':'green' if \
  x['properties']['POP2005']<10000000 \
       else 'orange' if 10000000 <x['properties']['POP2005']>20000000 else 'red'}

Body:
import folium ,pandas ,json

df=pandas.read_csv('Volcanoes_2.txt')

def colors(elev):
    minimum=int(min(df['ELEV']))
    step=int(max((df['ELEV'])-min(df['ELEV']))/3)
    if elev in range (minimum,minimum+step):
        col= "green"
    elif elev in range(minimum+step,minimum+step*2):
        col= "orange"
    else:
        col= "red"
    return col

map_1=folium.Map(location=[df['LAT'].mean(), df['LON'].mean()] ,
zoom_start=6,tiles='mapbox bright')

for name, lon, lat, elev in zip(df['NAME'], df['LON'], df['LAT'],
df['ELEV'] ):
    folium.Marker([lat, lon], popup= name,
     icon = folium.Icon(color =colors(elev))).add_to(map_1)

folium.GeoJson(open('world_geojson.json'),
           name='geojson',
           style_function= lambda x :{'fillcolor':'green' if \
  x['properties']['POP2005']<10000000 \
       else 'orange' if 10000000 <x['properties']['POP2005']>20000000 else 'red'},
       ).add_to(map_1)

folium.LayerControl().add_to(map_1)

map_1.save("map.html")

this is the map file https://github.com/xxspider4/new_repo/blob/master/map.html

this is the json file https://github.com/xxspider4/new_repo/blob/master/world_geojson.json


问:
Unable to pull/push in git repository
```<code>$ git pull origin master
fatal: unable to access 'https://xxxxxxxxxxxxxxx':
      error setting certificate verify locations:
CAfile: C:/Users/abc/AppData/Local/Programs/Git/usr/bin/curl-ca-bundle.crt
CApath: none
</code>```

I am getting this error when I pull or push my code.

Please guide me to fix this.

答:
Thanks to https://github.com/npm/npm/issues/1484

git config --global http.sslverify "false" will solve the problem

Title: Unable to pull/push in git repository

Body:
$ git pull origin master
fatal: unable to access 'https://xxxxxxxxxxxxxxx':
      error setting certificate verify locations:
CAfile: C:/Users/abc/AppData/Local/Programs/Git/usr/bin/curl-ca-bundle.crt
CApath: none

I am getting this error when I pull or push my code.

Please guide me to fix this.


问:
How to derive one class from two different classes in Swift based on API level?
I want to use a library with a class SwipeTableViewCell (derives from UITableViewCell), but it only supports iOS 9, so I want to derive from that class if possible, but from the normal UITableViewCell class if the app is running below 9.0.

This is the best I could come up with (not working):

```<code>@available(iOS, obsoleted: 9.0)
class MyTableViewCell : UITableViewCell {
}

@available(iOS 9.0, *)
class MyTableViewCell : SwipeTableViewCell {
}
</code>```

Then I try to derive from that class:

```<code>class SomeOtherTableViewCell : MyTableViewCell {
}
</code>```

Xcode gives me the following error and links to the two class definitions above:

```<code>SomeOtherTableViewCell.swift:34:31: 'MyTableViewCell' is ambiguous for type lookup in this context
Found this candidate
Found this candidate
</code>```

What's the cool way in Swift to conditionally derive from two different classes depending on API level?

答:
Unfortunately (or fortunately depending on how you look at it), exactly what you're trying to do is not possible with Swift or Objective-C from a language perspective. The @available attribute you're using is to tell the compiler that this class is only available if you're compiling a target with a deployment target greater than what you specified.

@MartinR made mention of the #available and the general pattern that you should employ to achieve your goal.

The reason these 2 things are available and necessary to support backward compatibility is because compiled code doesn't necessarily have all of the information about the runtime. The compiler/linker check for the existence of APIs, classes, etc by using the headers and module maps of the SDK at compile time. But, your app is linked with the system frameworks dynamically at runtime. This pattern is why your app doesn't have to be distributed with copies of every part of the SDK that it uses.

In your case, you have SDK9 and are trying to support a runtime of iOS 8 or less. The SwipeTableViewCell doesn't work in iOS 8 likely because it uses APIs that are only available in iOS 9. All of this doesn't prevent SwipeTableViewCell from existing on iOS 8 devices, it just prevents it from working.

You really should evaluate whether you need to support iOS 8 at all. If you change your target's deployment target to iOS 9 and release it to the App Store, people running iOS 8 won't get the update or be able to install it.

Update

After doing some thinking, and some research, I came up with a possible solution for your situation. I only tested it in an environment that relies on a storyboard for the UI, but I think it can gracefully bridge your iOS targets.

```<code>import UIKit

class ViewController: UITableViewController {

    @IBOutlet var staticCell: Any? // In the storyboard, tell the cell to by a "MySwipeableCell"

    override func viewDidLoad() {
        super.viewDidLoad()

        if staticCell is SwipeTableViewCell {
            print("This class was converted magically, which means we're on iOS 9 or later")
        }
    }
}

class SwipeTableViewCell: UITableViewCell {
    // Standin for your library class
}

class MySwipeableCell: UITableViewCell {

    override func awakeAfter(using aDecoder: NSCoder) -> Any? {
        if #available(iOS 9, *) {
            // This will replace the MySwipeableCell with an instance of SwipeTableViewCell instead
            return SwipeTableViewCell.init(coder: aDecoder)
        }

        return self
    }
}
</code>```

Body:
I want to use a library with a class SwipeTableViewCell (derives from UITableViewCell), but it only supports iOS 9, so I want to derive from that class if possible, but from the normal UITableViewCell class if the app is running below 9.0.

This is the best I could come up with (not working):

@available(iOS, obsoleted: 9.0)
class MyTableViewCell : UITableViewCell {
}

@available(iOS 9.0, *)
class MyTableViewCell : SwipeTableViewCell {
}

Then I try to derive from that class:

class SomeOtherTableViewCell : MyTableViewCell {
}

Xcode gives me the following error and links to the two class definitions above:

SomeOtherTableViewCell.swift:34:31: 'MyTableViewCell' is ambiguous for type lookup in this context
Found this candidate
Found this candidate

What's the cool way in Swift to conditionally derive from two different classes depending on API level?

Body:
Unfortunately (or fortunately depending on how you look at it), exactly what you're trying to do is not possible with Swift or Objective-C from a language perspective. The @available attribute you're using is to tell the compiler that this class is only available if you're compiling a target with a deployment target greater than what you specified.

@MartinR made mention of the #available and the general pattern that you should employ to achieve your goal.

The reason these 2 things are available and necessary to support backward compatibility is because compiled code doesn't necessarily have all of the information about the runtime. The compiler/linker check for the existence of APIs, classes, etc by using the headers and module maps of the SDK at compile time. But, your app is linked with the system frameworks dynamically at runtime. This pattern is why your app doesn't have to be distributed with copies of every part of the SDK that it uses.

In your case, you have SDK9 and are trying to support a runtime of iOS 8 or less. The SwipeTableViewCell doesn't work in iOS 8 likely because it uses APIs that are only available in iOS 9. All of this doesn't prevent SwipeTableViewCell from existing on iOS 8 devices, it just prevents it from working.

You really should evaluate whether you need to support iOS 8 at all. If you change your target's deployment target to iOS 9 and release it to the App Store, people running iOS 8 won't get the update or be able to install it.

Update

After doing some thinking, and some research, I came up with a possible solution for your situation. I only tested it in an environment that relies on a storyboard for the UI, but I think it can gracefully bridge your iOS targets.

import UIKit

class ViewController: UITableViewController {

    @IBOutlet var staticCell: Any? // In the storyboard, tell the cell to by a "MySwipeableCell"

    override func viewDidLoad() {
        super.viewDidLoad()

        if staticCell is SwipeTableViewCell {
            print("This class was converted magically, which means we're on iOS 9 or later")
        }
    }
}

class SwipeTableViewCell: UITableViewCell {
    // Standin for your library class
}

class MySwipeableCell: UITableViewCell {

    override func awakeAfter(using aDecoder: NSCoder) -> Any? {
        if #available(iOS 9, *) {
            // This will replace the MySwipeableCell with an instance of SwipeTableViewCell instead
            return SwipeTableViewCell.init(coder: aDecoder)
        }

        return self
    }
}


问:
Getting formatting of empty lines
I am a bit confused about how the QTextBlock::iterator works:

The documentation shows clear examples of how to use it, on normal text:

```<code>QTextBlock::iterator it;
for (it = currentBlock.begin(); !(it.atEnd()); ++it) {
    QTextFragment currentFragment = it.fragment();
    if (currentFragment.isValid())
        processFragment(currentFragment);
}
</code>```

I encounter problems on empty lines of text. On those lines,

```<code>it = currentBlock.begin();
if(it.atEnd())
    // returns true !
</code>```

I still need to be able to read formatting (char and block)

Should I check the block at end ? Is there any other way to test blocks with nothing except the new line ?

My current solution: check the last iterator as well, separate from the "for" loop, and also test if it is the last block in the document (if I try to get the fragment of the last block in the document, the program crashes).

It seems that I am working against the documentation... How should I get the formatting of empty lines ?

Edit:

My old solution:

```<code>QTextBlock currentBlock = document()->findBlock(selStart);
QTextBlock lastBlock = document()->lastBlock();
while (currentBlock.isValid())
{
    QTextBlock::iterator it = currentBlock.begin();
    if(currentBlock != lastBlock && it.atEnd())
    {
        QTextFragment currentFragment = it.fragment();
        if (currentFragment.isValid())
        {
            QTextCharFormat f = currentFragment.charFormat();
            // do something
        }
    }
    else
    {
        for (; !(it.atEnd()); ++it)
        {
            QTextFragment currentFragment = it.fragment();
            if (currentFragment.isValid())
            {
                // do stuff
                QTextCharFormat f = currentFragment.charFormat();
                // do stuff
            }
        }
    }
}
</code>```

New solution based from answer from Tarod eliminates one test (but seems to have less consistent behavior)

```<code>QTextBlock currentBlock = document()->findBlock(selStart);
QTextBlock lastBlock = document()->lastBlock();
while (currentBlock.isValid())
{
    QTextBlock::iterator it = currentBlock.begin();
    if(currentBlock != lastBlock && it.atEnd())
    {
        QTextCharFormat f = currentBlock.charFormat();
        // do something
    }
    else
    {
        for (; !(it.atEnd()); ++it)
        {
            QTextFragment currentFragment = it.fragment();
            if (currentFragment.isValid())
            {
                // do stuff
                QTextCharFormat f = currentFragment.charFormat();
                // do stuff
            }
        }
    }
}
</code>```

I still need to check against last block and avoid using it if empty, sometimes it crashes.

答:
Here is the pseudo code (which works for me). In short, when the method atEnd() returns TRUE, then you append a new line.

```<code>QTextEdit * pwTextEdit = whatever your source;
QTextDocument * poDocument = pwTextEdit->document();

QTextBlock oTextBlock = poDocument->begin();
while (oTextBlock.isValid())
{
    QTextBlock::iterator oTextBlockIterator(oTextBlock.begin());
    while (TRUE)
    {
        if (oTextBlockIterator.atEnd())
        {
            // Append your '\n' here
            break;
        }
        QTextFragment oTextFragment = oTextBlockIterator.fragment();
        QString sText = oTextFragment.text();
        // Process the text from sText
        oTextBlockIterator++;
    }
    oTextBlock = oTextBlock.next();
}
</code>```

Title: Getting formatting of empty lines

Body:
I am a bit confused about how the QTextBlock::iterator works:

The documentation shows clear examples of how to use it, on normal text:

QTextBlock::iterator it;
for (it = currentBlock.begin(); !(it.atEnd()); ++it) {
    QTextFragment currentFragment = it.fragment();
    if (currentFragment.isValid())
        processFragment(currentFragment);
}

I encounter problems on empty lines of text. On those lines,

it = currentBlock.begin();
if(it.atEnd())
    // returns true !

I still need to be able to read formatting (char and block)

Should I check the block at end ? Is there any other way to test blocks with nothing except the new line ?

My current solution: check the last iterator as well, separate from the "for" loop, and also test if it is the last block in the document (if I try to get the fragment of the last block in the document, the program crashes).

It seems that I am working against the documentation... How should I get the formatting of empty lines ?

Edit:

My old solution:

QTextBlock currentBlock = document()->findBlock(selStart);
QTextBlock lastBlock = document()->lastBlock();
while (currentBlock.isValid())
{
    QTextBlock::iterator it = currentBlock.begin();
    if(currentBlock != lastBlock && it.atEnd())
    {
        QTextFragment currentFragment = it.fragment();
        if (currentFragment.isValid())
        {
            QTextCharFormat f = currentFragment.charFormat();
            // do something
        }
    }
    else
    {
        for (; !(it.atEnd()); ++it)
        {
            QTextFragment currentFragment = it.fragment();
            if (currentFragment.isValid())
            {
                // do stuff
                QTextCharFormat f = currentFragment.charFormat();
                // do stuff
            }
        }
    }
}

New solution based from answer from Tarod eliminates one test (but seems to have less consistent behavior)

QTextBlock currentBlock = document()->findBlock(selStart);
QTextBlock lastBlock = document()->lastBlock();
while (currentBlock.isValid())
{
    QTextBlock::iterator it = currentBlock.begin();
    if(currentBlock != lastBlock && it.atEnd())
    {
        QTextCharFormat f = currentBlock.charFormat();
        // do something
    }
    else
    {
        for (; !(it.atEnd()); ++it)
        {
            QTextFragment currentFragment = it.fragment();
            if (currentFragment.isValid())
            {
                // do stuff
                QTextCharFormat f = currentFragment.charFormat();
                // do stuff
            }
        }
    }
}

I still need to check against last block and avoid using it if empty, sometimes it crashes.

Body:
Here is the pseudo code (which works for me). In short, when the method atEnd() returns TRUE, then you append a new line.

QTextEdit * pwTextEdit = whatever your source;
QTextDocument * poDocument = pwTextEdit->document();

QTextBlock oTextBlock = poDocument->begin();
while (oTextBlock.isValid())
{
    QTextBlock::iterator oTextBlockIterator(oTextBlock.begin());
    while (TRUE)
    {
        if (oTextBlockIterator.atEnd())
        {
            // Append your '\n' here
            break;
        }
        QTextFragment oTextFragment = oTextBlockIterator.fragment();
        QString sText = oTextFragment.text();
        // Process the text from sText
        oTextBlockIterator++;
    }
    oTextBlock = oTextBlock.next();
}


问:
sfValidatorFile 'mime_type' validation not working in production server
I've built a backend with a couple of models, one of them, Products, supports image uploading. Locally the file uploading works very fine. When I did the deploy, it stopped working: the form validation fails because of wrong mime type (error is: "Invalid mime type (jpeg image data, exif standard).") for the image field, even when i'm using the same JPG image that i used to test locally.

I'll show you my form class code:

```<code>class ProductForm extends BaseProductForm
{
  public function configure()
  {
    ...

    /* Widgets */
    ...
    $this->widgetSchema['image_1'] = new sfWidgetFormInputFileEditable(array(
      'file_src' => '/'.basename(sfConfig::get('sf_upload_dir')).'/products/normal_'.$this->getObject()->getImage_1(),
      'is_image' => true,
      'edit_mode' => strlen($this->getObject()->getImage_1()) > 0,
      'delete_label' => 'remover el archivo actual',
      'template'  => '<div>%file%<br />%input%</div>'
    ));
    ...

    /* Validators */
    $this->validatorSchema['image_1'] = new sfValidatorFileImage(array(
      'required' => (! $this->getObject()->getImage_1()),
      'max_size' => '5252880',
      'path' => sfConfig::get('sf_upload_dir').'/products/original',
      'mime_types' => 'web_images',
      'min_width' =>  470,
      'max_width' => 99999,
      'min_height' =>  306,
      'max_height' => 99999,
      'validated_file_class' => 'sfResizedFile',   // This class creates the thumbnails
    ), array(
      'required' => 'Tenés que seleccionar una imagen principal.',
      'max_size' => 'El tamaño máximo es 5 MB',
      //'mime_types' => 'Sólo se permiten imágenes para web (jpg, png, gif)',
      'invalid_image' => '%value% no es un archivo de imagen.',
      'min_width' => 'El ancho de "%value%" es muy chico (mínimo %min_width% pixels).',
      'min_height' => 'El alto de "%value%" es muy chico (mínimo %min_height% pixels).',
    ));
    $this->validatorSchema['image_1_delete'] = new sfValidatorPass();
    ...
  }
  ...
</code>```

sfValidatorFileImage is just a custom validator class that extends sfValidatorFile with size checks. I've tried with the regular sfValidatorFile class just in case, but the error persists. Does mime types have something to do with server configuration? And why would it break on a regular web server?

Speed in answer is really appreciated since this was needed deployed today.

答:
Also try to do not use mimes collections (web_images).
use array instead: </p>

```<code>$mime_types = array (
  'image/jpeg',
  'image/pjpeg',
  'image/png',
  'image/x-png',
  'image/gif',
);

$this->setValidator('filename', new sfValidatorFile(array(
  'mime_types' => $mime_types,
...
</code>```

Body:
I've built a backend with a couple of models, one of them, Products, supports image uploading. Locally the file uploading works very fine. When I did the deploy, it stopped working: the form validation fails because of wrong mime type (error is: "Invalid mime type (jpeg image data, exif standard).") for the image field, even when i'm using the same JPG image that i used to test locally.

I'll show you my form class code:

class ProductForm extends BaseProductForm
{
  public function configure()
  {
    ...

    /* Widgets */
    ...
    $this->widgetSchema['image_1'] = new sfWidgetFormInputFileEditable(array(
      'file_src' => '/'.basename(sfConfig::get('sf_upload_dir')).'/products/normal_'.$this->getObject()->getImage_1(),
      'is_image' => true,
      'edit_mode' => strlen($this->getObject()->getImage_1()) > 0,
      'delete_label' => 'remover el archivo actual',
      'template'  => '
%file%
%input%
'
    ));
    ...

    /* Validators */
    $this->validatorSchema['image_1'] = new sfValidatorFileImage(array(
      'required' => (! $this->getObject()->getImage_1()),
      'max_size' => '5252880',
      'path' => sfConfig::get('sf_upload_dir').'/products/original',
      'mime_types' => 'web_images',
      'min_width' =>  470,
      'max_width' => 99999,
      'min_height' =>  306,
      'max_height' => 99999,
      'validated_file_class' => 'sfResizedFile',   // This class creates the thumbnails
    ), array(
      'required' => 'Tenés que seleccionar una imagen principal.',
      'max_size' => 'El tamaño máximo es 5 MB',
      //'mime_types' => 'Sólo se permiten imágenes para web (jpg, png, gif)',
      'invalid_image' => '%value% no es un archivo de imagen.',
      'min_width' => 'El ancho de "%value%" es muy chico (mínimo %min_width% pixels).',
      'min_height' => 'El alto de "%value%" es muy chico (mínimo %min_height% pixels).',
    ));
    $this->validatorSchema['image_1_delete'] = new sfValidatorPass();
    ...
  }
  ...

sfValidatorFileImage is just a custom validator class that extends sfValidatorFile with size checks. I've tried with the regular sfValidatorFile class just in case, but the error persists. Does mime types have something to do with server configuration? And why would it break on a regular web server?

Speed in answer is really appreciated since this was needed deployed today.

Body:
Also try to do not use mimes collections (web_images).
use array instead:

$mime_types = array (
  'image/jpeg',
  'image/pjpeg',
  'image/png',
  'image/x-png',
  'image/gif',
);

$this->setValidator('filename', new sfValidatorFile(array(
  'mime_types' => $mime_types,
...


问:
Query the ids of every entry including a specified child key
Given a database scheme like the following:

```<code>{
   "lessons": {
      "subjectId1": {
           "lessonId2": { ... },
           "lessonId4": { ... },
           "lessonId6": { ... }
      },
      "subjectId2": {
           "lessonId1": { ... },
           "lessonId3": { ... },
           "lessonId5": { ... },
           "lessonId7": { ... }
      }
   }
}
</code>```

How do I retrieve the id(s) of any subject(s) that include a given lessonId?

I came up with a function like this:

```<code>    const refWithParent = database.ref("lessons");
    const snapshot = await refWithParent.orderByChild(lessonId).limitToLast(1).once("value");

    let firebaseId = null;
    if (snapshot.val() !== null) {
        snapshot.forEach(entry => {
            firebaseId = entry.key;
            return true;
        });
    }
    return firebaseId;
</code>```

This does work but I can't create an index in the database as the children are dynamically created.
Is there any easier way to retrieve the id?

答:
As I understood you want to retrieve one index of any subject that contains a lesson with a specific id. If so, you can add a node "contains" to each subject into which you will put a key/value of the form: lessonId: true.

```<code>{
   "lessons":{
      "subjectId1":{
         "contains":{
            "lessonId2":true,
            "lessonId4":true,
            "lessonId6":true
         },
         "lessonId2":{ ... },
         "lessonId4":{ ... },
         "lessonId6":{ ... }
      },
      "subjectId2":{
         "contains":{
            "lessonId1":true,
            "lessonId3":true,
            "lessonId5":true,
            "lessonId7":true
         },
         "lessonId1":{ ... },
         "lessonId3":{ ... },
         "lessonId5":{ ... },
         "lessonId7":{ ... }
      }
   }
}
</code>```

And then make a query (assuming lessonID is a variable in your code):

```<code>refWithParent.orderByChild(`contains/${lessonID)`).equalTo(true).limitToFirst(1).once('value');
</code>```

and then check the obtained snaphost as follows:

```<code>if (snapshot.val() !== null)
{
   let subjectId = snapshot.key;
   //TODO something with subjectId...
}
</code>```

Note, you must always add corresponding pair to "contains" node when you add a lesson to a subject. The same with removing.

P.S. If you want to obtain IDs of all subjects that contain given lesson, then simply remove .limitToFirst(1) from the query. That's all.

Body:
Given a database scheme like the following:

{
   "lessons": {
      "subjectId1": {
           "lessonId2": { ... },
           "lessonId4": { ... },
           "lessonId6": { ... }
      },
      "subjectId2": {
           "lessonId1": { ... },
           "lessonId3": { ... },
           "lessonId5": { ... },
           "lessonId7": { ... }
      }
   }
}

How do I retrieve the id(s) of any subject(s) that include a given lessonId?

I came up with a function like this:

    const refWithParent = database.ref("lessons");
    const snapshot = await refWithParent.orderByChild(lessonId).limitToLast(1).once("value");

    let firebaseId = null;
    if (snapshot.val() !== null) {
        snapshot.forEach(entry => {
            firebaseId = entry.key;
            return true;
        });
    }
    return firebaseId;

This does work but I can't create an index in the database as the children are dynamically created.
Is there any easier way to retrieve the id?

Body:
As I understood you want to retrieve one index of any subject that contains a lesson with a specific id. If so, you can add a node "contains" to each subject into which you will put a key/value of the form: lessonId: true.

{
   "lessons":{
      "subjectId1":{
         "contains":{
            "lessonId2":true,
            "lessonId4":true,
            "lessonId6":true
         },
         "lessonId2":{ ... },
         "lessonId4":{ ... },
         "lessonId6":{ ... }
      },
      "subjectId2":{
         "contains":{
            "lessonId1":true,
            "lessonId3":true,
            "lessonId5":true,
            "lessonId7":true
         },
         "lessonId1":{ ... },
         "lessonId3":{ ... },
         "lessonId5":{ ... },
         "lessonId7":{ ... }
      }
   }
}

And then make a query (assuming lessonID is a variable in your code):

refWithParent.orderByChild(`contains/${lessonID)`).equalTo(true).limitToFirst(1).once('value');

and then check the obtained snaphost as follows:

if (snapshot.val() !== null)
{
   let subjectId = snapshot.key;
   //TODO something with subjectId...
}

Note, you must always add corresponding pair to "contains" node when you add a lesson to a subject. The same with removing.

P.S. If you want to obtain IDs of all subjects that contain given lesson, then simply remove .limitToFirst(1) from the query. That's all.


问:
How can i properly change the assigned secret key in a Django Web Application
So, I'm trying to deploy a Django Web App to production, but I want to change the secret key before doing so.

I've attempted to generate a new key using a randomizing function and insert that new key in place of the old one. When I do so, I get an error that says the following:

AttributeError 'module' object has no attribute 'JSONEncoder' ...
  Exception Location .../django/contrib/messages/storage/cookie.py in
  , line 9

I've deleted the browser cache and restarted the server, but the error persists.
I've also attempted to change the key back, after deleting the browser cache and restarting, the error still persists.

Any idea how to resolve this issue?

Edit: Python version is 2.6.6 and Django version is 1.3.1

答:
I like to use this pattern (where you would put your app's name instead of APP_NAME):

```<code># SECURITY WARNING: keep the secret key used in production secret!
secret_key_env_variable_name = 'APP_NAME_SECRET_KEY'
SECRET_KEY = os.environ.get(secret_key_env_variable_name)
if len(SECRET_KEY) < 25:
    print( 'The value of $%s does not contain enough characters (%s characters)' % (secret_key_env_variable_name, len(SECRET_KEY)))
    raise RuntimeError('SECRET_KEY is not long enough (in environment variable "%s"' % secret_key_env_variable_name)
</code>```

Then in the account that runs Django I export this variable in the .bashrc (use your applicable environment config file):

```<code>export APP_NAME_SECRET_KEY='oysmy1iv=n7ygq%og!b4@(k@40&5d-i&5%^c8$riw%3$r3yi(='
</code>```

Note: that key was randomly generated with the first url for a generator after web searching for: generate a secret_key django.

Body:
I like to use this pattern (where you would put your app's name instead of APP_NAME):

# SECURITY WARNING: keep the secret key used in production secret!
secret_key_env_variable_name = 'APP_NAME_SECRET_KEY'
SECRET_KEY = os.environ.get(secret_key_env_variable_name)
if len(SECRET_KEY) < 25:
    print( 'The value of $%s does not contain enough characters (%s characters)' % (secret_key_env_variable_name, len(SECRET_KEY)))
    raise RuntimeError('SECRET_KEY is not long enough (in environment variable "%s"' % secret_key_env_variable_name)

Then in the account that runs Django I export this variable in the .bashrc (use your applicable environment config file):

export APP_NAME_SECRET_KEY='oysmy1iv=n7ygq%og!b4@(k@40&5d-i&5%^c8$riw%3$r3yi(='

Note: that key was randomly generated with the first url for a generator after web searching for: generate a secret_key django.


问:
Python - "The system cannot find the file specified" due to special character in filename
As part of my python script, I'm testing to see if two files are the same size using:

```<code>os.path.getsize(dir_file) # dir_file = root path + filename joined
</code>```

But when I encounter a file with some special character in its name (e.g Ü) I get the following error: WindowsError: [Error 2] The system cannot find the file specified with something like \xf6 replacing the special character.

I've tried encoding dir_file to utf-8 like so:

```<code>unicode(dir_file, 'utf-8') # method 1
dir_file.encode('utf-8') # method 2
</code>```

But that gives me the following error: UnicodeDecodeError: 'ascii' codec can't decode byte 0xf6 in position 79: ordinal not in range(128).

Not sure how to get around this problem of character encoding.

答:
Try to get the encoding of the filesystem with sys.getfilesystemencoding() to clarify your needs.

Then ensure that the string you pass in parameter uses the same encoding

```<code>if isinstance(dir_file, str):
 print "ascii"
elif isinstance(dir_file, unicode):
 print "unicode"
</code>```

Give your results, I will update the answer.

Body:
As part of my python script, I'm testing to see if two files are the same size using:

os.path.getsize(dir_file) # dir_file = root path + filename joined

But when I encounter a file with some special character in its name (e.g Ü) I get the following error: WindowsError: [Error 2] The system cannot find the file specified with something like \xf6 replacing the special character.

I've tried encoding dir_file to utf-8 like so:

unicode(dir_file, 'utf-8') # method 1
dir_file.encode('utf-8') # method 2

But that gives me the following error: UnicodeDecodeError: 'ascii' codec can't decode byte 0xf6 in position 79: ordinal not in range(128).

Not sure how to get around this problem of character encoding.

Body:
Try to get the encoding of the filesystem with sys.getfilesystemencoding() to clarify your needs.

Then ensure that the string you pass in parameter uses the same encoding

if isinstance(dir_file, str):
 print "ascii"
elif isinstance(dir_file, unicode):
 print "unicode"

Give your results, I will update the answer.