问:
Referencing extended variables from the user model after authentication successful
So, I have extended the base django user model using a one-to-one field:

```<code>class UserProfile(models.Model):
    user = models.OneToOneField(User, related_name='user')
    user_avatar = models.ImageField(storage=site_media_upload_location, null=True, blank=True)
</code>```

Upon authentication the following displays correctly other than the user_avatar variable from the user model:

```<code>{% if request.user.is_authenticated %}
  <div class="user">
    <div class="user-image">
      <img class="img-responsive img-circle" src="{{ MEDIA_URL }}{{ request.user.userprofile.user_avatar.url }}" alt="User Avatar">
    </div>
    <div class="dropdown user-dropdown">
    Hello,
    <a href="#" aria-expanded="true">{{ request.user.first_name }}<i class="fa fa-caret-down" aria-hidden="true"></i></a>
    <ul class="sub-menu text-left">
      <li><a href="#">My Profile</a></li>
      <li><a href="#">Settings</a></li>
      <li><a href="#">Log Out</a></li>
    </ul>
  </div>
</div>
{% else %}
Do something else
{% endif %}
</code>```

My MEDIA_URL is defined in the settings.py file as

```<code>MEDIA_URL = "/site_media/"
</code>```

This is purely where every "users" avatar will be stored for development purposes.

I've tried a few combinations, none of which seem to work:

```<code><img class="img-responsive img-circle" src="{{ MEDIA_URL }}{{ request.user.userprofile.user_avatar.url }}" alt="User Avatar">
</code>```

...and this...

```<code><img class="img-responsive img-circle" src="{{ MEDIA_URL }}{{ request.userprofile.user_avatar.url }}" alt="User Avatar">
</code>```

..and this...

```<code><img class="img-responsive img-circle" src="{{ MEDIA_URL }}{{ request.user.user_avatar.url }}" alt="User Avatar">
</code>```

If anyone knows how I can reference the variables of an extended model after authentication success I would be greatly appreciative!

答:
Change this line:

```<code>user = models.OneToOneField(User, related_name='user')
</code>```

to:

```<code>user = models.OneToOneField(User, related_name='userprofile')
</code>```

and this:

```<code>src="{{ MEDIA_URL }}{{ request.user.userprofile.user_avatar.url }}"
</code>```

should work.

When you call request.user, that object tries to get the property userprofile set by the related_name. You have set the related_name to user, so request.user didn't have the property userprofile.

Body:
So, I have extended the base django user model using a one-to-one field:

class UserProfile(models.Model):
    user = models.OneToOneField(User, related_name='user')
    user_avatar = models.ImageField(storage=site_media_upload_location, null=True, blank=True)

Upon authentication the following displays correctly other than the user_avatar variable from the user model:

{% if request.user.is_authenticated %}

    Hello,
    {{ request.user.first_name }}

My Profile

Settings

Log Out

{% else %}
Do something else
{% endif %}

My MEDIA_URL is defined in the settings.py file as

MEDIA_URL = "/site_media/"

This is purely where every "users" avatar will be stored for development purposes.

I've tried a few combinations, none of which seem to work:

...and this...

..and this...

If anyone knows how I can reference the variables of an extended model after authentication success I would be greatly appreciative!

Body:
Change this line:

user = models.OneToOneField(User, related_name='user')

to:

user = models.OneToOneField(User, related_name='userprofile')

and this:

src="{{ MEDIA_URL }}{{ request.user.userprofile.user_avatar.url }}"

should work.

When you call request.user, that object tries to get the property userprofile set by the related_name. You have set the related_name to user, so request.user didn't have the property userprofile.


来源: stackexchange

问:
git: a fast web-interface for huge repos
Some git repositories come in really huge sizes: DragonFly BSD .git directory is 324MB, and FreeBSD's is above 0.5GB in packed size, and above 2GB in unpacked size.

Does Gitweb, cgit or any other web-tools do any kind of pre-caching for these huge repositories?

How can one estimate the optimal amount of resources (e.g. memory and CPU constraints) for a web-interface to a couple of such huge repositories?  What would be the response time for a blame or a log operation of a random file?

答:
(Update June 2017, 4 years later)

Actually, those repos are tiny.

Microsoft Windows code base is a huge repo: 3.5 million files and is over 270 GB in size.

And Git manages it well... with the addition of GVFS (Git Virtual File System), announced last February 2017: that solves most of Git scaling issues (too many pushes, branches, history and files).

And the commands remain reasonably fast (source: "The largest Git repo on the planet")

For context, if we tried this with “vanilla Git”, before we started our work, many of the commands would take 30 minutes up to hours and a few would never complete.

See more with "Beyond GVFS: more details on optimizing Git for large repositories"

This is not yet available in native Git, but the Microsoft team is working to bring patches upstream.

ContentLicense: CC BY-SA 3.0


问:
kubeadm says cni config uninitialized for node using weave
I am trying to setup kubernetes using kubeadm 1.6. The master and nodes are created using Vagrant with Ubuntu 16.04 box. I followed all the instructions and can't figure this out.

vagrant file:

```<code>Vagrant.configure("2") do |config|
  config.vm.box = "bento/ubuntu-16.04"
  config.vm.box_check_update = false

  config.vm.provider "virtualbox" do |vb|
    vb.cpus = 2
    vb.memory = "1024"
  end

  config.vm.provision "shell", path: "provision.sh"

  config.vm.define "master" do |c|
      c.vm.hostname = "master"
      c.vm.network "private_network", ip: "192.168.50.2", netmask: "255.255.255.0", auto_config: true, virtualbox__intnet: "k8s-net"
      c.vm.provision :shell, inline: "sed 's/127\.0\.0\.1.*master.*/192\.168\.50\.2 master/' -i /etc/hosts"

  end

  config.vm.define "node1" do |c|
      c.vm.hostname = "node1"
      c.vm.network "private_network", ip: "192.168.50.3", netmask: "255.255.255.0", auto_config: true, virtualbox__intnet: "k8s-net"
      c.vm.provision :shell, inline: "sed 's/127\.0\.0\.1.*node1.*/192\.168\.50\.3 node1/' -i /etc/hosts"
  end

  config.vm.define "node2" do |c|
      c.vm.hostname = "node2"
      c.vm.network "private_network", ip: "192.168.50.4", netmask: "255.255.255.0", auto_config: true, virtualbox__intnet: "k8s-net"
      c.vm.provision :shell, inline: "sed 's/127\.0\.0\.1.*node2.*/192\.168\.50\.4 node2/' -i /etc/hosts"
  end

end
</code>```

Init kubeadm

sudo kubeadm init --apiserver-advertise-address=192.168.50.2

Run the provided commands

sudo cp /etc/kubernetes/admin.conf $HOME/
sudo chown $(id -u):$(id -g) $HOME/admin.conf
export KUBECONFIG=$HOME/admin.conf

Setup weave

kubectl apply --filename https://git.io/weave-kube-1.6

Validate that master mode is Ready and that dns pod is Running.

Join node with no errors.

kubeadm join --token 2f17fd.c5f6abcccdfa8c7a 192.168.50.2:6443

Node appears on master, but never gets into Ready state. Running kubectl describe node node1 shows this error:

KubeletNotReady runtime network not ready: NetworkReady=false reason:NetworkPluginNotReady message:docker: network plugin is not ready: cni config uninitialized

Why am I getting this error? I tried installing kubeadm 1.5.6 and I get the same error.

Please help.

Update
Found the solution here: How to get kube-dns working in Vagrant cluster using kubeadm and Weave

First, find the public IP by running the following on master.

```<code>kubectl get svc
NAME         CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   10.96.0.1    <none>        443/TCP   1h
</code>```

In each node, make sure that any process using 10.96.0.1 (in my case) is routed to master that is on 10.30.3.41.

So on each Node (you can skip master) use route to set the redirect.

```<code>route add 10.96.0.1 gw 10.30.3.41
</code>```

答:
Your problem is  the default route in the machine.  need to correct default route fist.

un configure current config 'kubeadm reset'

Install kubernets 1.6.4

remove the default route ' ip route delete default via  10.0.2.2'

add the prod network default route 'ip route add default via 192.168.50.1'

configure kubeadm 'kubeadm init --apiserver-advertise-address=192.168.50.2'

install the wave rbac and wave kube

Hope this helps.

Body:
I am trying to setup kubernetes using kubeadm 1.6. The master and nodes are created using Vagrant with Ubuntu 16.04 box. I followed all the instructions and can't figure this out.

vagrant file:

Vagrant.configure("2") do |config|
  config.vm.box = "bento/ubuntu-16.04"
  config.vm.box_check_update = false

  config.vm.provider "virtualbox" do |vb|
    vb.cpus = 2
    vb.memory = "1024"
  end

  config.vm.provision "shell", path: "provision.sh"

  config.vm.define "master" do |c|
      c.vm.hostname = "master"
      c.vm.network "private_network", ip: "192.168.50.2", netmask: "255.255.255.0", auto_config: true, virtualbox__intnet: "k8s-net"
      c.vm.provision :shell, inline: "sed 's/127\.0\.0\.1.*master.*/192\.168\.50\.2 master/' -i /etc/hosts"

  end

  config.vm.define "node1" do |c|
      c.vm.hostname = "node1"
      c.vm.network "private_network", ip: "192.168.50.3", netmask: "255.255.255.0", auto_config: true, virtualbox__intnet: "k8s-net"
      c.vm.provision :shell, inline: "sed 's/127\.0\.0\.1.*node1.*/192\.168\.50\.3 node1/' -i /etc/hosts"
  end

  config.vm.define "node2" do |c|
      c.vm.hostname = "node2"
      c.vm.network "private_network", ip: "192.168.50.4", netmask: "255.255.255.0", auto_config: true, virtualbox__intnet: "k8s-net"
      c.vm.provision :shell, inline: "sed 's/127\.0\.0\.1.*node2.*/192\.168\.50\.4 node2/' -i /etc/hosts"
  end

end

Init kubeadm

sudo kubeadm init --apiserver-advertise-address=192.168.50.2

Run the provided commands

sudo cp /etc/kubernetes/admin.conf $HOME/
sudo chown $(id -u):$(id -g) $HOME/admin.conf
export KUBECONFIG=$HOME/admin.conf

Setup weave

kubectl apply --filename https://git.io/weave-kube-1.6

Validate that master mode is Ready and that dns pod is Running.

Join node with no errors.

kubeadm join --token 2f17fd.c5f6abcccdfa8c7a 192.168.50.2:6443

Node appears on master, but never gets into Ready state. Running kubectl describe node node1 shows this error:

KubeletNotReady runtime network not ready: NetworkReady=false reason:NetworkPluginNotReady message:docker: network plugin is not ready: cni config uninitialized

Why am I getting this error? I tried installing kubeadm 1.5.6 and I get the same error.

Please help.

Update
Found the solution here: How to get kube-dns working in Vagrant cluster using kubeadm and Weave

First, find the public IP by running the following on master.

kubectl get svc
NAME         CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   10.96.0.1    <none>        443/TCP   1h

In each node, make sure that any process using 10.96.0.1 (in my case) is routed to master that is on 10.30.3.41.

So on each Node (you can skip master) use route to set the redirect.

route add 10.96.0.1 gw 10.30.3.41


问:
Am I misunderstanding LINQ to SQL .AsEnumerable()?
Consider this code:

```<code>var query = db.Table
              .Where(t => SomeCondition(t))
              .AsEnumerable();

int recordCount = query.Count();
int totalSomeNumber = query.Sum();
decimal average = query.Average();
</code>```

Assume query takes a very long time to run. I need to get the record count, total SomeNumber's returned, and take an average at the end. I thought based on my reading that .AsEnumerable() would execute the query using LINQ-to-SQL, then use LINQ-to-Objects for the Count, Sum, and Average. Instead, when I do this in LINQPad, I see the same query is run three times. If I replace .AsEnumerable() with .ToList(), it only gets queried once.

Am I missing something about what AsEnumerable is/does?

答:
Justin Niessner's answer is perfect.

I just want to quote a MSDN explanation here: .NET Language-Integrated Query for Relational Data

The AsEnumerable() operator, unlike ToList() and ToArray(), does not cause execution of the query. It is still deferred. The AsEnumerable() operator merely changes the static typing of the query, turning a IQueryable  into an IEnumerable, tricking the compiler into treating the rest of the query as locally executed.

Once it is LINQ to Objects we can apply object's methods (e.g. ToString()). This is the explanation for one of the frequently asked questions about LINQ - Why LINQ to Entities does not recognize the method 'System.String ToString()?

According to ASENUMERABLE - codeblog.jonskeet, AsEnumerable can be handy

Finally, also see this related question: Returning IEnumerable vs. IQueryable

Body:
Consider this code:

var query = db.Table
              .Where(t => SomeCondition(t))
              .AsEnumerable();

int recordCount = query.Count();
int totalSomeNumber = query.Sum();
decimal average = query.Average();

Assume query takes a very long time to run. I need to get the record count, total SomeNumber's returned, and take an average at the end. I thought based on my reading that .AsEnumerable() would execute the query using LINQ-to-SQL, then use LINQ-to-Objects for the Count, Sum, and Average. Instead, when I do this in LINQPad, I see the same query is run three times. If I replace .AsEnumerable() with .ToList(), it only gets queried once.

Am I missing something about what AsEnumerable is/does?

ContentLicense: CC BY-SA 4.0


问:
Java Reflect/AOP override supertype private Method
Is it at all possible to "override" a private method of a super class in Java?

The class whose method I wish to override is a third party class so I cannot modify the source. It would be ideal if there were some way to reflectively set a method on a class.

Alternatively, if it is possible to intercept a private method of a third party class then this would be suitable.

答:
You do not have a legal way to do this. But I can suggest you the following solutions.

Do you really wish to override this method? Try to think about other solution.

Java checks access permissions during compilation only. Are you surprised? I was surprised very much to find out this fact. So you can create skeleton of the third party class (even with empty implementations.). The interesting method should be protected instead of private. Now write your subclass and compile it against your stub. Then package only your subclass and try to run it with the "real" class. It should work. I have not tried this trick with inheritance but I have tried it when I had to access private method or field and it worked fine for me.

Try to use dynamic proxy that wraps your class and changes its implementation. I do not know what are you doing exactly, so I am not sure you can really use this method. But I hope you can. If not go back to #1 or #2.

ContentLicense: CC BY-SA 2.5


问:
Optimize Selenium tests for parallel execution
Currently, I have five instances of phantomjs set up on my Selenium Grid all on different nodes. I am trying to run 5 parallel tests at once, but some of these tests will fail. Each time, it is a different test that will fail on different lines.

I have also put in waits and assertions to wait until the element shows up, but the test cases are still failing in different spots each time. When I run the tests one by one however, all of the tests pass 100% of the time.

Is there any way I can optimize my tests for parallel execution?

My tests are written in Java in a Windows environment. I am using phantomjs 1.9.8 and Selenium 3.54

答:
Anecdotal: When I find my tests run fine on my machine but not so well on a Grid Node, it is typically a timing issue. My workstation is way more powerful than my VMs (Grid Nodes) so simple animations take longer to render. It got to the point where I wrote a .ClickViaJavaScript() to use instead of .Click(). I also threw in a 500ms sleep after the js click.

```<code>// Click element via JavaScript
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click()", element);
// Wait a moment
System.Threading.Thread.Sleep(500);
</code>```

Parallel doesn't really necessitate any code being rewritten so long as you do not change global variables at run-time and each test is atomic.

Body:
Anecdotal: When I find my tests run fine on my machine but not so well on a Grid Node, it is typically a timing issue. My workstation is way more powerful than my VMs (Grid Nodes) so simple animations take longer to render. It got to the point where I wrote a .ClickViaJavaScript() to use instead of .Click(). I also threw in a 500ms sleep after the js click.

// Click element via JavaScript
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click()", element);
// Wait a moment
System.Threading.Thread.Sleep(500);

Parallel doesn't really necessitate any code being rewritten so long as you do not change global variables at run-time and each test is atomic.


问:
tensorflow: what's the difference between tf.nn.dropout and tf.layers.dropout
I'm quite confused about whether to use tf.nn.dropout or tf.layers.dropout.

many MNIST CNN examples seems to use tf.nn.droput, with keep_prop as one of params.

but how is it different with tf.layers.dropout? is the "rate" params in tf.layers.dropout similar to tf.nn.dropout?

Or generally speaking, is the difference between tf.nn.dropout and tf.layers.dropout applies to all other similar situations, like similar functions in tf.nn and tf.layers.

答:
The idea is the same, the parameters are slightly different. In nn.dropout, keep_prob is the probability that each element is kept. In layers.dropout rate=0.1 would drop out 10% of input units.

So keep_prob = 1 - rate. Also layers.dropout allows training parameter.

In general, just read carefully documentation about the functions you care about and you will see the differences.


问:
Using multer with express in docker container
I developed an API with node js, express and multer for fileUploader. Everything work well on local on my computer, but when I try to deploy on a docker container on my server the uploader file not work. I don't have error, in my client side the POST request return success 200 without error but when I attach bach to my container, the files not exist...

Dockerfile:

```<code>FROM node:boron

# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install

# Bundle app source
COPY . /usr/src/app
RUN mkdir public
RUN mkdir public/img

EXPOSE 8085
CMD [ "npm", "start" ]
</code>```

Multer Conf:

```<code>// Upload dir for img
var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, 'public/img')
  },
  filename: function (req, file, cb) {
    cb(null, file.originalname)
  }
});
var upload = multer({ storage: storage }).single('file');
</code>```

I don't understand why that's not work on docker while the same code work fine in localhost.

Thanks for your answers.

答:
It's ok ... i'm very stupid, i just forget to change the file upload address in the client side ...

If some want help about that ^^ ...

Body:
I developed an API with node js, express and multer for fileUploader. Everything work well on local on my computer, but when I try to deploy on a docker container on my server the uploader file not work. I don't have error, in my client side the POST request return success 200 without error but when I attach bach to my container, the files not exist...

Dockerfile:

FROM node:boron

# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install

# Bundle app source
COPY . /usr/src/app
RUN mkdir public
RUN mkdir public/img

EXPOSE 8085
CMD [ "npm", "start" ]

Multer Conf:

// Upload dir for img
var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, 'public/img')
  },
  filename: function (req, file, cb) {
    cb(null, file.originalname)
  }
});
var upload = multer({ storage: storage }).single('file');

I don't understand why that's not work on docker while the same code work fine in localhost.

Thanks for your answers.


问:
Basic Drag and Drop in iOS
I want to have a view in which there are vehicles driving around that the user can also drag and drop. What do you think is the best large-scale strategy for doing this? Is it best to get touch events from the views representing the vehicles, or from the larger view? Is there a simple paradigm you've used for drag and drop that you're satisfied with? What are the drawbacks of different strategies?</p>

答:
Here's the best way to do drag and drop as of iOS 11:

https://developer.apple.com/documentation/uikit/drag_and_drop/adopting_drag_and_drop_in_a_custom_view

Title: Basic Drag and Drop in iOS


问:
Larval left join with Eloquent causing id field to be overwritten
I am building search listings application using Laravel; the app needs to search for Businesses by distance (which I've already built) and include the latest subscription to a business (if any) so I can order by it and then distance.

The two models here are Business and Subscriptions. A Business can have many Subscriptions (although there will only ever be one thats active).

Controller

```<code>$businesses = Business::distance($place['lat'], $place['lng'], $request->distance)
                        ->ofShopType($request->shop_type)
                        ->uptoBudget($request->price)
                        ->leftJoin('subscriptions', function($join) {
                            $join->on('businesses.id', '=', 'subscriptions.business_id')
                                 ->orderBy('subscriptions.created_at', 'desc');
                        });
return $businesses = $businesses->get();
</code>```

Business Model

```<code>public function scopeDistance($query,$from_latitude,$from_longitude,$distance)
{
    $raw = \DB::raw('ROUND ( ( 3959 * acos( cos( radians('.$from_latitude.') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('.$from_longitude.') ) + sin( radians('.$from_latitude.') ) * sin( radians( latitude ) ) ) ), 1 ) AS distance');
    return $query->select('*')->addSelect($raw)
             ->orderBy( 'distance', 'ASC' )
             ->groupBy('businesses.id')
             ->having('distance', '<=', $distance);
}
</code>```

So the difficulties I am getting are that the business.id field is being overwritten by the subscription.id.
I've done some searching and have read that including ->select('businesses.*') before the leftJoin should resolve this, however by doing this I get the following error:

```<code>SQLSTATE[42S22]: Column not found: 1054 Unknown column
      'business.distance' in 'having clause'
      (SQL: select `businesses`.*
          from `businesses`
          left join `subscriptions`
                   on `businesses`.`id` = `subscriptions`.`business_id`
          where `shop_type_id` = 1
            and `min_cost` <= 100
          group by `distance`, `businesses`.`id`
          having `distance` <= 50
          order by `distance` asc)
</code>```

The second issue is that the left join seems to get the first record, I would like to get the most recent record (by created_at). You can see that I already have an orderBy line in my controller but this has no affect as I can see its still returning an older record even though a newer record is available.

答:
You're correct that using select('businesses.*') will prevent the id field from being overwritten.

I would suggest using selectRaw as follows:

```<code>public function scopeDistance($query, $from_latitude, $from_longitude, $distance)
{
    $raw = \DB::raw('ROUND ( ( 3959 * acos( cos( radians('.$from_latitude.') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('.$from_longitude.') ) + sin( radians('.$from_latitude.') ) * sin( radians( latitude ) ) ) ), 1 ) AS distance');
    return $query->selectRaw("businesses.*, $raw")->orderBy( 'distance', 'ASC' )->groupBy('businesses.id')->having('distance', '<=', $distance);
}
</code>```

Body:
I am building search listings application using Laravel; the app needs to search for Businesses by distance (which I've already built) and include the latest subscription to a business (if any) so I can order by it and then distance.

The two models here are Business and Subscriptions. A Business can have many Subscriptions (although there will only ever be one thats active).

Controller

$businesses = Business::distance($place['lat'], $place['lng'], $request->distance)
                        ->ofShopType($request->shop_type)
                        ->uptoBudget($request->price)
                        ->leftJoin('subscriptions', function($join) {
                            $join->on('businesses.id', '=', 'subscriptions.business_id')
                                 ->orderBy('subscriptions.created_at', 'desc');
                        });
return $businesses = $businesses->get();

Business Model

public function scopeDistance($query,$from_latitude,$from_longitude,$distance)
{
    $raw = \DB::raw('ROUND ( ( 3959 * acos( cos( radians('.$from_latitude.') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('.$from_longitude.') ) + sin( radians('.$from_latitude.') ) * sin( radians( latitude ) ) ) ), 1 ) AS distance');
    return $query->select('*')->addSelect($raw)
             ->orderBy( 'distance', 'ASC' )
             ->groupBy('businesses.id')
             ->having('distance', '<=', $distance);
}

So the difficulties I am getting are that the business.id field is being overwritten by the subscription.id.
I've done some searching and have read that including ->select('businesses.*') before the leftJoin should resolve this, however by doing this I get the following error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column
      'business.distance' in 'having clause'
      (SQL: select `businesses`.*
          from `businesses`
          left join `subscriptions`
                   on `businesses`.`id` = `subscriptions`.`business_id`
          where `shop_type_id` = 1
            and `min_cost` <= 100
          group by `distance`, `businesses`.`id`
          having `distance` <= 50
          order by `distance` asc)

The second issue is that the left join seems to get the first record, I would like to get the most recent record (by created_at). You can see that I already have an orderBy line in my controller but this has no affect as I can see its still returning an older record even though a newer record is available.

Body:
You're correct that using select('businesses.*') will prevent the id field from being overwritten.

I would suggest using selectRaw as follows:

public function scopeDistance($query, $from_latitude, $from_longitude, $distance)
{
    $raw = \DB::raw('ROUND ( ( 3959 * acos( cos( radians('.$from_latitude.') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('.$from_longitude.') ) + sin( radians('.$from_latitude.') ) * sin( radians( latitude ) ) ) ), 1 ) AS distance');
    return $query->selectRaw("businesses.*, $raw")->orderBy( 'distance', 'ASC' )->groupBy('businesses.id')->having('distance', '<=', $distance);
}


问:
Are type definitions for npm packages required only on developer machine?
As I understand, the type definition modules (for npm packages) provide intellisense for developers so that they don't have to guess parameter types when calling library methods. For example, these modules are very useful in my typescript project when I consume third party libraries (along with their type definition modules) in my vscode editor.

Is this the only purpose (i.e. provide intellisense) in for type definition modules?

If the answer is yes, then they should not be required on build servers and production machines. Is this true?

答:
If you are working with plain old JavaScript then yes, type packages are only used to provide IntelliSense and better language support in the editor. In many cases, automatic type acquisition should download and manage these typings files for you. You can also list these type packages explicitly in your dev dependencies

If you are working with TypeScript or using the new checkJS feature to typecheck your JavaScript code, the type definitions must be installed on the build machine. Just list them in the dev dependencies. You should generally never need type packages on a production machine


问:
ffmpeg not working from php
I installed the version from http://ffmpeg.org/releases/ffmpeg-2.5.3.tar.bz2
and also tried the yum version but it is not new enough for youtube-dl.

I tried export PATH=$PATH:/usr/local/bin/ffmpeg and PHP/Apache still cant find FFmpeg.

Apache user runs PHP script on CentOS 6.5.

Calling my youtube-dl.php from php command line as root works (with FFmpeg)

Calling my youtube-dl.php from php apache user works (not with FFmpeg)

What I am trying to do: shell exec youtube-dl (youtube dl calls FFmpeg in its binary code which there is now to modifiy it to add the full path). This works but the FFmpeg part
my youtube-dl.php file

```<code>$output = shell_exec('youtube-dl -o "/home/test/%(title)s.%(ext)s" '.$url.'      -f bestvideo+bestaudio 2>&1');
echo "```$output```";

# which ffmpeg
/usr/local/bin/ffmpeg
</code>```

Someone on other pages on this site says local/bin is for customized versions but I downloaded it directly from FFmpeg and did the standard ./configure make install.

Done:

```<code> chmod 755 to /usr/local/bin/ffmpeg
</code>```

Running FFmpeg as a test command directly (sometimes works and doesn't as I upgraded/rebooted/updatedb/etc)

```<code>$output = shell_exec('ffmpeg 2>&1');
echo "```$output```";
</code>```

Result:

```<code>```sh: ffmpeg: command not found```
</code>```

I read all of these similar questions but they didn't help:

FFMPEG PHP does not seem to work

  FFmpeg not working

  Recording audio with FFMPEG works in terminal but not through exec() of PHP

  ffmpeg-php permission denied on localhost

  C++ program from PHP not working properly

  FFMPEG works from command line but not PHP

  Calling a shellscript from php - not working correctly

  PHP Internal Server Error 500 when using ffmpeg to convert files

  PHP code not proceeding to the line after exec ffmpeg

  ffmpeg not working from php script but works from command line

  PHP exec() Not Working With ffmpeg

  ffmpeg not work with PHP exec()

  ffmpeg php exec not working

答:
Solution what worked for me.
I followed the tutorial here to install ffmpeg: http://www.wikihow.com/Install-FFmpeg-on-Windows

Set up the PATH then I checked ffmpeg –version from command prompt it was working.

Then I tried to download a 1080p video with youtube-dl (youtube link) -f bestvideo+bestaudio but I got the same message: "WARNING: You have requested multiple formats but ffmpeg or avconv are not installed. The formats won't be merged." Of course it was installed.

Then I opened the command prompt with Run as Administrator, tried the same and it worked finally.

Title: ffmpeg not working from php

Body:
I installed the version from http://ffmpeg.org/releases/ffmpeg-2.5.3.tar.bz2
and also tried the yum version but it is not new enough for youtube-dl.

I tried export PATH=$PATH:/usr/local/bin/ffmpeg and PHP/Apache still cant find FFmpeg.

Apache user runs PHP script on CentOS 6.5.

Calling my youtube-dl.php from php command line as root works (with FFmpeg)

Calling my youtube-dl.php from php apache user works (not with FFmpeg)

What I am trying to do: shell exec youtube-dl (youtube dl calls FFmpeg in its binary code which there is now to modifiy it to add the full path). This works but the FFmpeg part
my youtube-dl.php file

$output = shell_exec('youtube-dl -o "/home/test/%(title)s.%(ext)s" '.$url.'      -f bestvideo+bestaudio 2>&1');
echo "
$output
";

# which ffmpeg
/usr/local/bin/ffmpeg

Someone on other pages on this site says local/bin is for customized versions but I downloaded it directly from FFmpeg and did the standard ./configure make install.

Done:

 chmod 755 to /usr/local/bin/ffmpeg

Running FFmpeg as a test command directly (sometimes works and doesn't as I upgraded/rebooted/updatedb/etc)

$output = shell_exec('ffmpeg 2>&1');
echo "
$output
";

Result:

sh: ffmpeg: command not found

I read all of these similar questions but they didn't help:

FFMPEG PHP does not seem to work

  FFmpeg not working

  Recording audio with FFMPEG works in terminal but not through exec() of PHP

  ffmpeg-php permission denied on localhost

  C++ program from PHP not working properly

  FFMPEG works from command line but not PHP

  Calling a shellscript from php - not working correctly

  PHP Internal Server Error 500 when using ffmpeg to convert files

  PHP code not proceeding to the line after exec ffmpeg

  ffmpeg not working from php script but works from command line

  PHP exec() Not Working With ffmpeg

  ffmpeg not work with PHP exec()

  ffmpeg php exec not working


问:
UICollectionView resizes cells incorrectly after reloadData
I have a UICollectionView that renders some autosizing cells. When I load the data into the collection view initially, the data is perfectly sized the way it should be.

I have a weird issue where, when I trigger reloadData with a new data set, the auto sizing goes wrong and I end up with incorrectly sized cells.

Images attached.

I have tried invalidating the collectionViewLayout before I call reloadData.

Oddly enough, if I remove data, and trigger a reloadData the cells resize to their correct layout. So I am wondering if this is a view recycling issue?

Thanks in advance.

Below is the layout after the screen was initially loaded for the first time.

Below is the layout after the + opens a modal to add a new post, closes, and refreshes the data - see incorrect sizing of cell "Thanks for all your hard work".

Below is the screen after the screen was reloaded from scratch again.

答:
My answer might be a bit late, but getting the similar behaviour I did manage to solve it.
In my case, the problem was with multiline labels — so changing them to single-line solved the case.
As I didn't need to have multilines — I had no further research on them.

I hope, this answer could be a good starting point to find a fix.


问:
attached email sending in php 5.2.11
I have a problem with sending email with attachment.

It is working on server which has php 5.2.8 installed while it's not working on the other sever which has php 5.2.11 installed.
Following is the function

```<code>function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message)
{
    $file = $path.$filename;
    $file_size = filesize($file);
    $handle = fopen($file, "r");
    $content = fread($handle, $file_size);
    fclose($handle);
    $content = chunk_split(base64_encode($content));
    $uid = md5(uniqid(time()));
    $name = basename($file);
    $header = "From: ".$from_name." <".$from_mail.">\r\n";
    $header .= "Reply-To: ".$replyto."\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= $message."\r\n\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use diff. tyoes here
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $header .= $content."\r\n\r\n";
    $header .= "--".$uid."--";
    if (mail($mailto, $subject, "", $header)) {
       echo  "Picture Uploaded Successfully... OK"; // or use booleans here
  return true;
    } else {
        echo  "mail send ... ERROR!";
return false;
    }
}
</code>```

答: Use a mail library, like Swift Mailer or PHPMailer.</p>

Title: attached email sending in php 5.2.11

Body:
I have a problem with sending email with attachment.

It is working on server which has php 5.2.8 installed while it's not working on the other sever which has php 5.2.11 installed.
Following is the function

function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message)
{
    $file = $path.$filename;
    $file_size = filesize($file);
    $handle = fopen($file, "r");
    $content = fread($handle, $file_size);
    fclose($handle);
    $content = chunk_split(base64_encode($content));
    $uid = md5(uniqid(time()));
    $name = basename($file);
    $header = "From: ".$from_name." <".$from_mail.">\r\n";
    $header .= "Reply-To: ".$replyto."\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= $message."\r\n\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use diff. tyoes here
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $header .= $content."\r\n\r\n";
    $header .= "--".$uid."--";
    if (mail($mailto, $subject, "", $header)) {
       echo  "Picture Uploaded Successfully... OK"; // or use booleans here
  return true;
    } else {
        echo  "mail send ... ERROR!";
return false;
    }
}


问:
Laravel Eloquent Query absence through shared relationship
I have a relationship like this

Requests

```<code>=> id

public function proposals(){
    return $this->hasMany(Proposal::class)
}
</code>```

Proposals

```<code>=> request_id
=> company_id

public function request(){
    return $this->belongsTo(Request::class)
}

public function proposals(){
    return $this->belongsTo(Company::class)
}
</code>```

Companies

```<code>=> id

public function proposals(){
    return $this->hasMany(Proposal::class)
}
</code>```

I've tought in something like this:

```<code>$request->with('proposals')->whereDoesntHave('company', function($query){
    $query->where('company_id', '<>', 1);
})->get()
</code>```

But it didn't work out.

In this scenario how can I retrieve all the requests that one company has not sent a proposal?

EDIT

A short version of my DB Schema

A User can have many Companies, a User can make Requests, a Company respond to this Request through a Proposal

答:
What I am seeing here from your schema is a pivot table relationship.

A Request can belong to many Companies, and a Company can have many Requests. The relationship between the two is the Proposal, which is your pivot table.

So firstly, you want add the correct relationships to your models

class Request
{
    public function companies()
    {
        // You will need to probably set the foreign and local keys as extra parameters here
        return $this->belongsToMany(Company::class, 'proposals')->using(Proposal::class);
    }
}
```

<p>and</p>

<pre class="lang-php prettyprint-override"><code>class Company
{
    public function requests()
    {
        // You will need to probably set the foreign and local keys as extra parameters here
        return $this->belongsToMany(Request::class, 'proposals')->using(Proposal::class);
    }
}
</code>```

Finally, you can get the proposal using the pivot which will be a Proposal object.

To address your original query, you would just do:

$requests = Request::with(['companies' => function($q) {
    $q->where('companyid', '!=', 1);
}]);
```

Body:
I have a relationship like this

Requests

=> id

public function proposals(){
    return $this->hasMany(Proposal::class)
}

Proposals

=> request_id
=> company_id

public function request(){
    return $this->belongsTo(Request::class)
}

public function proposals(){
    return $this->belongsTo(Company::class)
}

Companies

=> id

public function proposals(){
    return $this->hasMany(Proposal::class)
}

I've tought in something like this:

$request->with('proposals')->whereDoesntHave('company', function($query){
    $query->where('company_id', '<>', 1);
})->get()

But it didn't work out.

In this scenario how can I retrieve all the requests that one company has not sent a proposal?

EDIT

A short version of my DB Schema

A User can have many Companies, a User can make Requests, a Company respond to this Request through a Proposal

Body:
What I am seeing here from your schema is a pivot table relationship.

A Request can belong to many Companies, and a Company can have many Requests. The relationship between the two is the Proposal, which is your pivot table.

So firstly, you want add the correct relationships to your models

class Request
{
    public function companies()
    {
        // You will need to probably set the foreign and local keys as extra parameters here
        return $this->belongsToMany(Company::class, 'proposals')->using(Proposal::class);
    }
}

and

class Company
{
    public function requests()
    {
        // You will need to probably set the foreign and local keys as extra parameters here
        return $this->belongsToMany(Request::class, 'proposals')->using(Proposal::class);
    }
}

Finally, you can get the proposal using the pivot which will be a Proposal object.

To address your original query, you would just do:

$requests = Request::with(['companies' => function($q) {
    $q->where('companyid', '!=', 1);
}]);


问:
Could FAIR scheduling mode make Spark Streaming jobs that read from different topics running in parallel?
I use Spark 2.1 and Kafka 0.9.

Under fair sharing, Spark assigns tasks between jobs in a “round robin” fashion, so that all jobs get a roughly equal share of cluster resources. This means that short jobs submitted while a long job is running can start receiving resources right away and still get good response times, without waiting for the long job to finish.

According to this if i have multiple jobs from multiple threads in case of spark streaming(one topic from each thread) is it possible that multiple topics can run simultaneously if i have enough cores in my cluster or would it just do a round robin across pools but run only one job at a time ?

Context:

I have two topics T1 and T2, both with one 1 partition. I have configured a pool with scheduleMode to be FAIR. I have 4 cores registered with spark. Now each topic has two actions(hence two jobs - totally 4 jobs across topics) Let's say J1 and J2 are jobs for T1 and J3 and J4 are jobs for topic T2. What spark is doing in FAIR mode is execute J1 J3 J2 J4, but at any time only one job is executing. Now as each topic has only one partition, only once core is being used and 3 are just free. This is something which i don't want.
Any way i can avoid this ?

答:
I think that fair scheduler alone will not help, as it's the Spark Streaming engine that takes care of submitting the Spark Jobs and normally does so in a sequential mode.

There's a non-documented configuration parameter in Spark Streaming: spark.streaming.concurrentJobs[1], which is set to 1 by default. It controls the parallelism level of jobs submitted to Spark.

By increasing this value, you may see parallel processing of the different spark stages of your streaming job.

I would think that combining this configuration with the fair scheduler in Spark, you will be able to achieve controlled parallel processing of the independent topic consumers. This is mostly uncharted territory.


问:
How to merge projects from two branches into one

Scenario

Assume I have a Visual Studio solution that contains a single project named ProjectA. This solution lives on branchA in git. Then, there was a time when I branched off of branchA into branchB to create a different UI for the same product. The development of both, branchA and branchB continued independently. They still retain the same ProjectA name in both branches; the structure overall remained the same.

```<code>---branchA---x---x------x---------x
              \
               \---branchB---x---x
</code>```

Current structure (on both branches)

```<code>- ProjectA
  ...
- Solution.sln
</code>```

Future structure

```<code>- ProjectA
  ...
- ProjectB
  ...
- Solution.sln
</code>```

Question

How do I merge branchB into branchA without overriding branchA code? In essence, I want to include branchB's contents in branchA without sharing any code. Ideally, I would like to retain the history for branchB until the common ancestor.

Options

Rename branchB's ProjectA to ProjectB and fix the folder structure and lose history for branchB.

Rewrite history as if the files were copied at the common ancestor's point before any of the modifications and additions happened.

What I tried

I tried various merge strategies (no-rename), but all of them move most of the contents.

答: Perhaps stash the contents of ProjectA, merge B, and apply the stashed changes.</p>

Body:
Scenario

Assume I have a Visual Studio solution that contains a single project named ProjectA. This solution lives on branchA in git. Then, there was a time when I branched off of branchA into branchB to create a different UI for the same product. The development of both, branchA and branchB continued independently. They still retain the same ProjectA name in both branches; the structure overall remained the same.

---branchA---x---x------x---------x
              \
               \---branchB---x---x

Current structure (on both branches)

- ProjectA
  ...
- Solution.sln

Future structure

- ProjectA
  ...
- ProjectB
  ...
- Solution.sln

Question

How do I merge branchB into branchA without overriding branchA code? In essence, I want to include branchB's contents in branchA without sharing any code. Ideally, I would like to retain the history for branchB until the common ancestor.

Options

Rename branchB's ProjectA to ProjectB and fix the folder structure and lose history for branchB.

Rewrite history as if the files were copied at the common ancestor's point before any of the modifications and additions happened.

What I tried

I tried various merge strategies (no-rename), but all of them move most of the contents.


问:
Exporting from Google Spreadsheet to Firebase, ERROR
I think most of my errors come from the updates of Firebase. But I'm getting there.

Still loving Firebase!

Ok, I'm trying to export a column demo Spreadsheet to Firebase (code bellow). The error seems to be related to the Firebase reference. I know how to fix it in my Firebase projects, but this is hooked into the Script editor of the spreadsheet.

ERROR: ReferenceError: "FirebaseApp" is not defined. (line 13, file
  "export")

```<code>function writeDataToFirebase() {
  var ss =

SpreadsheetApp.openById("1rV2_S2q5rcakOuHs2E1iLeKR2floRIozSytAt2iRXo8");
  var sheet = ss.getSheets()[0];
  var data = sheet.getDataRange().getValues();
  var dataToImport = {};
  for(var i = 1; i < data.length; i++) {
    var year = data[i][0];
    dataToImport[year] = {
    caption:data[i][1]
  };
}

 var firebaseUrl = "https://test-db-9833d.firebaseio.com/";

 // ERROR HERE ********************************************

 var base = FirebaseApp.getDatabaseByUrl(firebaseUrl);
 base.setData("", dataToImport);

}
</code>```

答:
If you use plain JavaScript, initialize your FirebaseApp class as follows:

```<code>// Load Firebase library
var firebaseScript = document.createElement('script');
firebaseScript.setAttribute('src',
    'https://www.gstatic.com/firebasejs/4.1.1/firebase.js');
document.head.appendChild(firebaseScript);

// Initialize Firebase
// TODO: Replace with your project's customized code snippet
var config = {
    apiKey: "<API_KEY>",
    authDomain: "<PROJECT_ID>.firebaseapp.com",
    databaseURL: "https://<DATABASE_NAME>.firebaseio.com",
    storageBucket: "<BUCKET>.appspot.com",
    messagingSenderId: "<SENDER_ID>",
};

var app = firebase.initializeApp(config);
</code>```

Alternatively, if you use Webpack or Browserify, you can install firebase package and then initialize it as follows:

```<code>var firebase = require("firebase/app");
require("firebase/auth");
require("firebase/database");

var config = {
  // ...
};

var app = firebase.initializeApp(config);
</code>```

Body:
I think most of my errors come from the updates of Firebase. But I'm getting there.

Still loving Firebase!

Ok, I'm trying to export a column demo Spreadsheet to Firebase (code bellow). The error seems to be related to the Firebase reference. I know how to fix it in my Firebase projects, but this is hooked into the Script editor of the spreadsheet.

ERROR: ReferenceError: "FirebaseApp" is not defined. (line 13, file
  "export")

function writeDataToFirebase() {
  var ss =

SpreadsheetApp.openById("1rV2_S2q5rcakOuHs2E1iLeKR2floRIozSytAt2iRXo8");
  var sheet = ss.getSheets()[0];
  var data = sheet.getDataRange().getValues();
  var dataToImport = {};
  for(var i = 1; i < data.length; i++) {
    var year = data[i][0];
    dataToImport[year] = {
    caption:data[i][1]
  };
}

 var firebaseUrl = "https://test-db-9833d.firebaseio.com/";

 // ERROR HERE ********************************************

 var base = FirebaseApp.getDatabaseByUrl(firebaseUrl);
 base.setData("", dataToImport);

}

Body:
If you use plain JavaScript, initialize your FirebaseApp class as follows:

// Load Firebase library
var firebaseScript = document.createElement('script');
firebaseScript.setAttribute('src',
    'https://www.gstatic.com/firebasejs/4.1.1/firebase.js');
document.head.appendChild(firebaseScript);

// Initialize Firebase
// TODO: Replace with your project's customized code snippet
var config = {
    apiKey: "<API_KEY>",
    authDomain: "<PROJECT_ID>.firebaseapp.com",
    databaseURL: "https://<DATABASE_NAME>.firebaseio.com",
    storageBucket: "<BUCKET>.appspot.com",
    messagingSenderId: "<SENDER_ID>",
};

var app = firebase.initializeApp(config);

Alternatively, if you use Webpack or Browserify, you can install firebase package and then initialize it as follows:

var firebase = require("firebase/app");
require("firebase/auth");
require("firebase/database");

var config = {
  // ...
};

var app = firebase.initializeApp(config);


问:
Eclipse - Disabling compiling of XML files while in Java perspective
When compiling android stuff in eclipse, I am forever annoyed when I hit the "Run" button to compile and run, if I am in a .java file everything works great if im in an .xml file (for android layouts) it compiles the xml file and produces an _out.xml file which screws up the project so that I first have to delete the "out" file then clean my project files.

Any quick answer on how to disable the compiling of xml files?

答: There is no way to do this, the reason it happens is because of the fact that when you click "run" it's trying to run the .xml instead of the Project its-self. The easiest thing to do to avoid this, is when you go to hit run do a Ctrl+s, then click on the project folder, then hit run, or do the same thing, but switch tab to a .java file. Sorry to be the bearer or bad news, when I started I had the same problem.. drove me crazy, but when you get into the new habit, you're just fine!</p>


问:
Python functions call by reference
In some languages you can pass a parameter by reference or value by using a special reserved word like ref or val. When you pass a parameter to a Python function it never alters the value of the parameter on leaving the function.The only way to do this is by using the global reserved word (or as i understand it currently).

Example 1:

```<code>k = 2

def foo (n):
     n = n * n     #clarity regarding comment below
     square = n
     return square

j = foo(k)
print j
print k
</code>```

would show

```<code>>>4
>>2
</code>```

showing k to be unchanged.

In this example the variable n is never changed

Example 2:

```<code>n = 0
def foo():
    global n
    n = n * n
    return n
</code>```

In this example the variable n is changed.

Is there any way in Python to call a function and tell Python that the parameter is either a value or reference parameter instead of using global?

答:
Python already call by ref..

let's take example:

```<code>  def foo(var):
      print(hex(id(var)))

  x = 1 # any value
  print(hex(id(x))) # I think the id() give the ref...
  foo(x)
</code>```

OutPut

```<code>  0x50d43700 #with you might give another hex number deppend on your memory
  0x50d43700
</code>```

Title: Python functions call by reference

Body:
In some languages you can pass a parameter by reference or value by using a special reserved word like ref or val. When you pass a parameter to a Python function it never alters the value of the parameter on leaving the function.The only way to do this is by using the global reserved word (or as i understand it currently).

Example 1:

k = 2

def foo (n):
     n = n * n     #clarity regarding comment below
     square = n
     return square

j = foo(k)
print j
print k

would show

>>4
>>2

showing k to be unchanged.

In this example the variable n is never changed

Example 2:

n = 0
def foo():
    global n
    n = n * n
    return n

In this example the variable n is changed.

Is there any way in Python to call a function and tell Python that the parameter is either a value or reference parameter instead of using global?

Body:
Python already call by ref..

let's take example:

  def foo(var):
      print(hex(id(var)))

  x = 1 # any value
  print(hex(id(x))) # I think the id() give the ref...
  foo(x)

OutPut

  0x50d43700 #with you might give another hex number deppend on your memory
  0x50d43700


问:
Too many pythons on my Mac, and pip install is not install package into the right python package folder
I want to use virtualenv on my Mac, and I installed it with pip:

```<code>pip install virtualenv

/usr/local/lib/python2.7/site-packages/pip/commands/install.py:194: UserWarning: Disabling all use of wheels due to the use of --build-options / --global-options / --install-options.
  cmdoptions.check_install_build_global(options)
Collecting virtualenv
  Using cached virtualenv-15.1.0.tar.gz
Skipping bdist_wheel for virtualenv, due to binaries being disabled for it.
Installing collected packages: virtualenv
  Running setup.py install for virtualenv ... done
Successfully installed virtualenv
</code>```

Apparently, virtualenv is not installed into the correct Python packages folder. And here is the error:

```<code>virtualenv -p python3 --system-site-packages py3

Traceback (most recent call last):
  File "/usr/local/bin/virtualenv", line 6, in <module>
    from pkg_resources import load_entry_point
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 3019, in <module>
    @_call_aside
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 3003, in _call_aside
    f(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 3032, in _initialize_master_working_set
    working_set = WorkingSet._build_master()
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 655, in _build_master
    ws.require(__requires__)
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 963, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 849, in resolve
    raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'virtualenv==15.1.0' distribution was not found and is required by the application
</code>```

Then I figured I have too many Pythons on my Mac:

```<code>sudo find / -type d -name "*site-packages*"

find: /dev/fd/user: No such file or directory
find: /dev/fd/user: No such file or directory
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages
/Library/Python/2.6/site-packages
/Library/Python/2.7/site-packages
/Library/Python/2.7/site-packages/flask/testsuite/test_apps/lib/python2.5/site-packages
/opt/X11/lib/python2.6/site-packages
/private/var/folders/17/y6xv651n50j6gplk57ythm8m0000gn/T/pip-Nexe_V-uninstall/Library/Python/2.7/site-packages
/private/var/folders/17/y6xv651n50j6gplk57ythm8m0000gn/T/pip-PpXcxM-uninstall/Library/Python/2.7/site-packages
/private/var/folders/17/y6xv651n50j6gplk57ythm8m0000gn/T/pip-ROUjc9-uninstall/Library/Python/2.7/site-packages
/Users/user/Downloads/virtualenv-15.0.3/py3/lib/python3.6/site-packages
/Users/user/Library/Enthought/Canopy_64bit/factory-reset-backup/System/lib/python2.7/site-packages
/Users/user/Library/Enthought/Canopy_64bit/factory-reset-backup/User/lib/python2.7/site-packages
/Users/user/Library/Enthought/Canopy_64bit/System/lib/python2.7/site-packages
/Users/user/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages
/Users/user/Library/Python/2.7/lib/python/site-packages
/Users/user/Library/Python/3.6/lib/python/site-packages
/Users/user/py3/lib/python3.6/site-packages
/Users/user/venv/lib/python2.7/site-packages
/usr/local/Cellar/numpy/1.11.1/lib/python2.7/site-packages
/usr/local/Cellar/numpy/1.11.1/libexec/nose/lib/python2.7/site-packages
/usr/local/Cellar/numpy/1.12.1/lib/python2.7/site-packages
/usr/local/Cellar/numpy/1.12.1/lib/python3.6/site-packages
/usr/local/Cellar/numpy/1.12.1/libexec/nose/lib/python2.7/site-packages
/usr/local/Cellar/numpy/1.12.1/libexec/nose/lib/python3.6/site-packages
/usr/local/git/lib/python2.7/site-packages
/usr/local/lib/python2.7/site-packages
/usr/local/lib/python3.6/site-packages
</code>```

My current python is pointed to /usr/local/bin/python, and my pip is pointed to /usr/local/bin/pip.  And this what's inside of the default pip:

```<code>cat /usr/local/bin/pip

#!/usr/local/opt/python/bin/python2.7
# EASY-INSTALL-ENTRY-SCRIPT: 'pip==9.0.1','console_scripts','pip'
__requires__ = 'pip==9.0.1'
import re
import sys
from pkg_resources import load_entry_point

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
    sys.exit(
        load_entry_point('pip==9.0.1', 'console_scripts', 'pip')()
    )
</code>```

And my virtualenv executable is something like this:

```<code>cat /usr/local/bin/virtualenv

#!/usr/local/opt/python/bin/python2.7
# EASY-INSTALL-ENTRY-SCRIPT: 'virtualenv==15.1.0','console_scripts','virtualenv'
__requires__ = 'virtualenv==15.1.0'
import re
import sys
from pkg_resources import load_entry_point

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
    sys.exit(
        load_entry_point('virtualenv==15.1.0', 'console_scripts', 'virtualenv')()
    )
</code>```

How to clean my python installation and point pip and virtualenv installation to the current python package folder?

BTW, without pass pip install-option argument, pip install is not installing packages to the current default python package folder.

```<code>pip install --install-option="--install-purelib=/python/packages" virtualenv
</code>```

Even after use pip3, something is still weird with my python:

```<code>$ pip3 install virtualenv
/usr/local/lib/python3.6/site-packages/pip/commands/install.py:194: UserWarning: Disabling all use of wheels due to the use of --build-options / --global-options / --install-options.
  cmdoptions.check_install_build_global(options)
Collecting virtualenv
  Using cached virtualenv-15.1.0.tar.gz
Skipping bdist_wheel for virtualenv, due to binaries being disabled for it.
Installing collected packages: virtualenv
  Running setup.py install for virtualenv ... done
Successfully installed virtualenv

virtualenv -p python3 --system-site-packages py3
Traceback (most recent call last):
  File "/usr/local/bin/virtualenv", line 6, in <module>
    from pkg_resources import load_entry_point
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3019, in <module>
    @_call_aside
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3003, in _call_aside
    f(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3032, in _initialize_master_working_set
    working_set = WorkingSet._build_master()
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 655, in _build_master
    ws.require(__requires__)
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 963, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 849, in resolve
    raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'virtualenv==15.1.0' distribution was not found and is required by the application

pip3 install --install-option="--install-purelib=/python/packages" virtualenv
/usr/local/lib/python3.6/site-packages/pip/commands/install.py:194: UserWarning: Disabling all use of wheels due to the use of --build-options / --global-options / --install-options.
  cmdoptions.check_install_build_global(options)
Collecting virtualenv
  Using cached virtualenv-15.1.0.tar.gz
Skipping bdist_wheel for virtualenv, due to binaries being disabled for it.
Installing collected packages: virtualenv
  Running setup.py install for virtualenv ... error
    Complete output from command /usr/local/opt/python3/bin/python3.6 -u -c "import setuptools, tokenize;__file__='/private/var/folders/17/y6xv651n50j6gplk57ythm8m0000gn/T/pip-build-mvjkq8ur/virtualenv/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /var/folders/17/y6xv651n50j6gplk57ythm8m0000gn/T/pip-y9p6_ro7-record/install-record.txt --single-version-externally-managed --compile --install-purelib=/Library/Python/2.7/site-packages --install-purelib=/python/packages:
    running install
    running build
    running build_py
    creating build
    creating build/lib
    copying virtualenv.py -> build/lib
    creating build/lib/virtualenv_support
    copying virtualenv_support/__init__.py -> build/lib/virtualenv_support
    copying virtualenv_support/argparse-1.4.0-py2.py3-none-any.whl -> build/lib/virtualenv_support
    copying virtualenv_support/pip-9.0.1-py2.py3-none-any.whl -> build/lib/virtualenv_support
    copying virtualenv_support/setuptools-28.8.0-py2.py3-none-any.whl -> build/lib/virtualenv_support
    copying virtualenv_support/wheel-0.29.0-py2.py3-none-any.whl -> build/lib/virtualenv_support
    running install_lib
    byte-compiling /python/packages/virtualenv.py to virtualenv.cpython-36.pyc
    error: [Errno 13] Permission denied: '/python/packages/__pycache__'

    ----------------------------------------
Command "/usr/local/opt/python3/bin/python3.6 -u -c "import setuptools, tokenize;__file__='/private/var/folders/17/y6xv651n50j6gplk57ythm8m0000gn/T/pip-build-mvjkq8ur/virtualenv/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /var/folders/17/y6xv651n50j6gplk57ythm8m0000gn/T/pip-y9p6_ro7-record/install-record.txt --single-version-externally-managed --compile --install-purelib=/Library/Python/2.7/site-packages --install-purelib=/python/packages" failed with error code 1 in /private/var/folders/17/y6xv651n50j6gplk57ythm8m0000gn/T/pip-build-mvjkq8ur/virtualenv/

sudo pip3 install --install-option="--install-purelib=/python/packages" virtualenv
/usr/local/lib/python3.6/site-packages/pip/commands/install.py:194: UserWarning: Disabling all use of wheels due to the use of --build-options / --global-options / --install-options.
  cmdoptions.check_install_build_global(options)
The directory '/Users/yong/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/yong/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting virtualenv
  Downloading virtualenv-15.1.0.tar.gz (1.9MB)
    100% |████████████████████████████████| 1.9MB 824kB/s
Installing collected packages: virtualenv
  Running setup.py install for virtualenv ... done
Successfully installed virtualenv

virtualenv -p python3 --system-site-packages py3
Traceback (most recent call last):
  File "/usr/local/bin/virtualenv", line 6, in <module>
    from pkg_resources import load_entry_point
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3019, in <module>
    @_call_aside
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3003, in _call_aside
    f(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3032, in _initialize_master_working_set
    working_set = WorkingSet._build_master()
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 655, in _build_master
    ws.require(__requires__)
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 963, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 849, in resolve
    raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'virtualenv==15.1.0' distribution was not found and is required by the application

ls /usr/local/lib/python3.6/site-packages/
__pycache__                      pip                              pkg_resources                    setuptools-32.2.0-py3.6.egg-info wheel
easy_install.py                  pip-9.0.1-py3.6.egg-info         setuptools                       sitecustomize.py                 wheel-0.29.0-py3.6.egg-info

ls /Library/Python/2.7/site-packages
Flask_MongoRest-0.1.1-py2.7.egg      colorclass-2.2.0-py2.7.egg-info      idna                                 pyparsing-2.2.0-py2.7.egg-info       terminaltables-3.1.0-py2.7.egg-info
Flask_Views-0.2.1-py2.7.egg          colorclass-2.2.0-py3.6.egg-info      idna-2.5-py2.7.egg-info              pyparsing-2.2.0-py3.6.egg-info       terminaltables-3.1.0-py3.6.egg-info
Flask_WTF-0.9.5-py2.7.egg            coverage-3.7.1-py2.7.egg             idna-2.5-py3.6.egg-info              pyparsing.py                         textblob
IPython                              dateutil                             jinja2                               pyparsing.pyc                        twisted
Pygments-1.6-py2.7.egg               django                               lxml                                 python_dateutil-2.6.0-py2.7.egg-info urllib3
WTForms-1.0.5-py2.7.egg              docopt-0.6.2-py2.7.egg-info          mongoengine                          queuelib                             urllib3-1.21.1-py2.7.egg-info
__pycache__                          docopt-0.6.2-py3.6.egg-info          mysql-0.0.1-py2.7.egg-info           requests                             urllib3-1.21.1-py3.6.egg-info
awscli-1.9.12-py2.7.egg              docopt.py                            nameparser                           requests-2.17.3-py2.7.egg-info       virtualenv-15.1.0-py2.7.egg-info
boto                                 docopt.pyc                           nltk                                 requests-2.17.3-py3.6.egg-info       virtualenv-15.1.0-py3.6.egg-info
bs4                                  docutils                             nose                                 scrapy                               virtualenv.py
cerberus                             eve                                  nose-1.3.1-py2.7.egg                 simplejson                           virtualenv.pyc
certifi                              events                               packaging                            six-1.10.0-py2.7.egg-info            virtualenv_support
certifi-2017.4.17-py2.7.egg-info     flask                                packaging-16.8-py2.7.egg-info        six-1.10.0-py3.6.egg-info            werkzeug
certifi-2017.4.17-py3.6.egg-info     flask_cuddlyrest                     packaging-16.8-py3.6.egg-info        six.py                               wtforms
chardet                              flask_mongoengine                    pandas                               six.pyc                              zmq
chardet-3.0.3-py2.7.egg-info         flask_mongoengine-0.7.0-py2.7.egg    pattern                              sklearn
chardet-3.0.3-py3.6.egg-info         flask_restful                        pip_upgrader                         sphinx
cleancat-0.3-py2.7.egg               flask_views                          pip_upgrader-1.4.0-py2.7.egg-info    tablib
colorclass                           flask_wtf                            pip_upgrader-1.4.0-py3.6.egg-info    terminaltables
</code>```

I had to copy the files to python 3.6 to make it work.

```<code>cp -R /Library/Python/2.7/site-packages/virtualenv-15.1.0-py3.6.egg-info /usr/local/lib/python3.6/site-packages/
cp -R /Library/Python/2.7/site-packages/virtualenv.pyc /usr/local/lib/python3.6/site-packages/
cp -R /Library/Python/2.7/site-packages/virtualenv.py /usr/local/lib/python3.6/site-packages/
</code>```

答:
You are installing your package on python2.7 but then checking for its availability on python3!!

When you install multiple versions of python, usually it will also come with it's own version of pip.
e.g. python3 will also have pip3 installed.
Look for it in directories like below so:
/Library/Frameworks/Python.framework/Versions/3.4/bin/pip3.4

On my machine, I also have a pip3 which points to the latest version of python3 (in my case python3.4) installed on my machine.

Then you simply do:

```<code>pip3 install <yourpackage>
</code>```

Body:
I want to use virtualenv on my Mac, and I installed it with pip:

pip install virtualenv

/usr/local/lib/python2.7/site-packages/pip/commands/install.py:194: UserWarning: Disabling all use of wheels due to the use of --build-options / --global-options / --install-options.
  cmdoptions.check_install_build_global(options)
Collecting virtualenv
  Using cached virtualenv-15.1.0.tar.gz
Skipping bdist_wheel for virtualenv, due to binaries being disabled for it.
Installing collected packages: virtualenv
  Running setup.py install for virtualenv ... done
Successfully installed virtualenv

Apparently, virtualenv is not installed into the correct Python packages folder. And here is the error:

virtualenv -p python3 --system-site-packages py3

Traceback (most recent call last):
  File "/usr/local/bin/virtualenv", line 6, in <module>
    from pkg_resources import load_entry_point
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 3019, in <module>
    @_call_aside
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 3003, in _call_aside
    f(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 3032, in _initialize_master_working_set
    working_set = WorkingSet._build_master()
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 655, in _build_master
    ws.require(__requires__)
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 963, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 849, in resolve
    raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'virtualenv==15.1.0' distribution was not found and is required by the application

Then I figured I have too many Pythons on my Mac:

sudo find / -type d -name "*site-packages*"

find: /dev/fd/user: No such file or directory
find: /dev/fd/user: No such file or directory
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages
/Library/Python/2.6/site-packages
/Library/Python/2.7/site-packages
/Library/Python/2.7/site-packages/flask/testsuite/test_apps/lib/python2.5/site-packages
/opt/X11/lib/python2.6/site-packages
/private/var/folders/17/y6xv651n50j6gplk57ythm8m0000gn/T/pip-Nexe_V-uninstall/Library/Python/2.7/site-packages
/private/var/folders/17/y6xv651n50j6gplk57ythm8m0000gn/T/pip-PpXcxM-uninstall/Library/Python/2.7/site-packages
/private/var/folders/17/y6xv651n50j6gplk57ythm8m0000gn/T/pip-ROUjc9-uninstall/Library/Python/2.7/site-packages
/Users/user/Downloads/virtualenv-15.0.3/py3/lib/python3.6/site-packages
/Users/user/Library/Enthought/Canopy_64bit/factory-reset-backup/System/lib/python2.7/site-packages
/Users/user/Library/Enthought/Canopy_64bit/factory-reset-backup/User/lib/python2.7/site-packages
/Users/user/Library/Enthought/Canopy_64bit/System/lib/python2.7/site-packages
/Users/user/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages
/Users/user/Library/Python/2.7/lib/python/site-packages
/Users/user/Library/Python/3.6/lib/python/site-packages
/Users/user/py3/lib/python3.6/site-packages
/Users/user/venv/lib/python2.7/site-packages
/usr/local/Cellar/numpy/1.11.1/lib/python2.7/site-packages
/usr/local/Cellar/numpy/1.11.1/libexec/nose/lib/python2.7/site-packages
/usr/local/Cellar/numpy/1.12.1/lib/python2.7/site-packages
/usr/local/Cellar/numpy/1.12.1/lib/python3.6/site-packages
/usr/local/Cellar/numpy/1.12.1/libexec/nose/lib/python2.7/site-packages
/usr/local/Cellar/numpy/1.12.1/libexec/nose/lib/python3.6/site-packages
/usr/local/git/lib/python2.7/site-packages
/usr/local/lib/python2.7/site-packages
/usr/local/lib/python3.6/site-packages

My current python is pointed to /usr/local/bin/python, and my pip is pointed to /usr/local/bin/pip.  And this what's inside of the default pip:

cat /usr/local/bin/pip

#!/usr/local/opt/python/bin/python2.7
# EASY-INSTALL-ENTRY-SCRIPT: 'pip==9.0.1','console_scripts','pip'
__requires__ = 'pip==9.0.1'
import re
import sys
from pkg_resources import load_entry_point

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
    sys.exit(
        load_entry_point('pip==9.0.1', 'console_scripts', 'pip')()
    )

And my virtualenv executable is something like this:

cat /usr/local/bin/virtualenv

#!/usr/local/opt/python/bin/python2.7
# EASY-INSTALL-ENTRY-SCRIPT: 'virtualenv==15.1.0','console_scripts','virtualenv'
__requires__ = 'virtualenv==15.1.0'
import re
import sys
from pkg_resources import load_entry_point

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
    sys.exit(
        load_entry_point('virtualenv==15.1.0', 'console_scripts', 'virtualenv')()
    )

How to clean my python installation and point pip and virtualenv installation to the current python package folder?

BTW, without pass pip install-option argument, pip install is not installing packages to the current default python package folder.

pip install --install-option="--install-purelib=/python/packages" virtualenv

Even after use pip3, something is still weird with my python:

$ pip3 install virtualenv
/usr/local/lib/python3.6/site-packages/pip/commands/install.py:194: UserWarning: Disabling all use of wheels due to the use of --build-options / --global-options / --install-options.
  cmdoptions.check_install_build_global(options)
Collecting virtualenv
  Using cached virtualenv-15.1.0.tar.gz
Skipping bdist_wheel for virtualenv, due to binaries being disabled for it.
Installing collected packages: virtualenv
  Running setup.py install for virtualenv ... done
Successfully installed virtualenv

virtualenv -p python3 --system-site-packages py3
Traceback (most recent call last):
  File "/usr/local/bin/virtualenv", line 6, in <module>
    from pkg_resources import load_entry_point
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3019, in <module>
    @_call_aside
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3003, in _call_aside
    f(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3032, in _initialize_master_working_set
    working_set = WorkingSet._build_master()
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 655, in _build_master
    ws.require(__requires__)
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 963, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 849, in resolve
    raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'virtualenv==15.1.0' distribution was not found and is required by the application

pip3 install --install-option="--install-purelib=/python/packages" virtualenv
/usr/local/lib/python3.6/site-packages/pip/commands/install.py:194: UserWarning: Disabling all use of wheels due to the use of --build-options / --global-options / --install-options.
  cmdoptions.check_install_build_global(options)
Collecting virtualenv
  Using cached virtualenv-15.1.0.tar.gz
Skipping bdist_wheel for virtualenv, due to binaries being disabled for it.
Installing collected packages: virtualenv
  Running setup.py install for virtualenv ... error
    Complete output from command /usr/local/opt/python3/bin/python3.6 -u -c "import setuptools, tokenize;__file__='/private/var/folders/17/y6xv651n50j6gplk57ythm8m0000gn/T/pip-build-mvjkq8ur/virtualenv/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /var/folders/17/y6xv651n50j6gplk57ythm8m0000gn/T/pip-y9p6_ro7-record/install-record.txt --single-version-externally-managed --compile --install-purelib=/Library/Python/2.7/site-packages --install-purelib=/python/packages:
    running install
    running build
    running build_py
    creating build
    creating build/lib
    copying virtualenv.py -> build/lib
    creating build/lib/virtualenv_support
    copying virtualenv_support/__init__.py -> build/lib/virtualenv_support
    copying virtualenv_support/argparse-1.4.0-py2.py3-none-any.whl -> build/lib/virtualenv_support
    copying virtualenv_support/pip-9.0.1-py2.py3-none-any.whl -> build/lib/virtualenv_support
    copying virtualenv_support/setuptools-28.8.0-py2.py3-none-any.whl -> build/lib/virtualenv_support
    copying virtualenv_support/wheel-0.29.0-py2.py3-none-any.whl -> build/lib/virtualenv_support
    running install_lib
    byte-compiling /python/packages/virtualenv.py to virtualenv.cpython-36.pyc
    error: [Errno 13] Permission denied: '/python/packages/__pycache__'

    ----------------------------------------
Command "/usr/local/opt/python3/bin/python3.6 -u -c "import setuptools, tokenize;__file__='/private/var/folders/17/y6xv651n50j6gplk57ythm8m0000gn/T/pip-build-mvjkq8ur/virtualenv/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /var/folders/17/y6xv651n50j6gplk57ythm8m0000gn/T/pip-y9p6_ro7-record/install-record.txt --single-version-externally-managed --compile --install-purelib=/Library/Python/2.7/site-packages --install-purelib=/python/packages" failed with error code 1 in /private/var/folders/17/y6xv651n50j6gplk57ythm8m0000gn/T/pip-build-mvjkq8ur/virtualenv/

sudo pip3 install --install-option="--install-purelib=/python/packages" virtualenv
/usr/local/lib/python3.6/site-packages/pip/commands/install.py:194: UserWarning: Disabling all use of wheels due to the use of --build-options / --global-options / --install-options.
  cmdoptions.check_install_build_global(options)
The directory '/Users/yong/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/yong/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting virtualenv
  Downloading virtualenv-15.1.0.tar.gz (1.9MB)
    100% |████████████████████████████████| 1.9MB 824kB/s
Installing collected packages: virtualenv
  Running setup.py install for virtualenv ... done
Successfully installed virtualenv

virtualenv -p python3 --system-site-packages py3
Traceback (most recent call last):
  File "/usr/local/bin/virtualenv", line 6, in <module>
    from pkg_resources import load_entry_point
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3019, in <module>
    @_call_aside
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3003, in _call_aside
    f(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3032, in _initialize_master_working_set
    working_set = WorkingSet._build_master()
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 655, in _build_master
    ws.require(__requires__)
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 963, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 849, in resolve
    raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'virtualenv==15.1.0' distribution was not found and is required by the application

ls /usr/local/lib/python3.6/site-packages/
__pycache__                      pip                              pkg_resources                    setuptools-32.2.0-py3.6.egg-info wheel
easy_install.py                  pip-9.0.1-py3.6.egg-info         setuptools                       sitecustomize.py                 wheel-0.29.0-py3.6.egg-info

ls /Library/Python/2.7/site-packages
Flask_MongoRest-0.1.1-py2.7.egg      colorclass-2.2.0-py2.7.egg-info      idna                                 pyparsing-2.2.0-py2.7.egg-info       terminaltables-3.1.0-py2.7.egg-info
Flask_Views-0.2.1-py2.7.egg          colorclass-2.2.0-py3.6.egg-info      idna-2.5-py2.7.egg-info              pyparsing-2.2.0-py3.6.egg-info       terminaltables-3.1.0-py3.6.egg-info
Flask_WTF-0.9.5-py2.7.egg            coverage-3.7.1-py2.7.egg             idna-2.5-py3.6.egg-info              pyparsing.py                         textblob
IPython                              dateutil                             jinja2                               pyparsing.pyc                        twisted
Pygments-1.6-py2.7.egg               django                               lxml                                 python_dateutil-2.6.0-py2.7.egg-info urllib3
WTForms-1.0.5-py2.7.egg              docopt-0.6.2-py2.7.egg-info          mongoengine                          queuelib                             urllib3-1.21.1-py2.7.egg-info
__pycache__                          docopt-0.6.2-py3.6.egg-info          mysql-0.0.1-py2.7.egg-info           requests                             urllib3-1.21.1-py3.6.egg-info
awscli-1.9.12-py2.7.egg              docopt.py                            nameparser                           requests-2.17.3-py2.7.egg-info       virtualenv-15.1.0-py2.7.egg-info
boto                                 docopt.pyc                           nltk                                 requests-2.17.3-py3.6.egg-info       virtualenv-15.1.0-py3.6.egg-info
bs4                                  docutils                             nose                                 scrapy                               virtualenv.py
cerberus                             eve                                  nose-1.3.1-py2.7.egg                 simplejson                           virtualenv.pyc
certifi                              events                               packaging                            six-1.10.0-py2.7.egg-info            virtualenv_support
certifi-2017.4.17-py2.7.egg-info     flask                                packaging-16.8-py2.7.egg-info        six-1.10.0-py3.6.egg-info            werkzeug
certifi-2017.4.17-py3.6.egg-info     flask_cuddlyrest                     packaging-16.8-py3.6.egg-info        six.py                               wtforms
chardet                              flask_mongoengine                    pandas                               six.pyc                              zmq
chardet-3.0.3-py2.7.egg-info         flask_mongoengine-0.7.0-py2.7.egg    pattern                              sklearn
chardet-3.0.3-py3.6.egg-info         flask_restful                        pip_upgrader                         sphinx
cleancat-0.3-py2.7.egg               flask_views                          pip_upgrader-1.4.0-py2.7.egg-info    tablib
colorclass                           flask_wtf                            pip_upgrader-1.4.0-py3.6.egg-info    terminaltables

I had to copy the files to python 3.6 to make it work.

cp -R /Library/Python/2.7/site-packages/virtualenv-15.1.0-py3.6.egg-info /usr/local/lib/python3.6/site-packages/
cp -R /Library/Python/2.7/site-packages/virtualenv.pyc /usr/local/lib/python3.6/site-packages/
cp -R /Library/Python/2.7/site-packages/virtualenv.py /usr/local/lib/python3.6/site-packages/

Body:
You are installing your package on python2.7 but then checking for its availability on python3!!

When you install multiple versions of python, usually it will also come with it's own version of pip.
e.g. python3 will also have pip3 installed.
Look for it in directories like below so:
/Library/Frameworks/Python.framework/Versions/3.4/bin/pip3.4

On my machine, I also have a pip3 which points to the latest version of python3 (in my case python3.4) installed on my machine.

Then you simply do:

pip3 install <yourpackage>


问:
Access all variables in an array
I'm sure this is a simple question but I'm asking it anyways. Basically I'm trying to do the following in javascript (no jquery) with less code:

```<code>var Ground = [];

function gameLoop()
{
    Gravity += 0.2

    if(Ground[0].isCollided(Player))
    {
        Player.dy = 0;
        Gravity = 0;
    }
    if(Ground[1].isCollided(Player))
    {
        Player.dy = 0;
        Gravity = 0;
    }
    if(Ground[2].isCollided(Player))
    {
        Player.dy = 0;
        Gravity = 0;
    }

    Player.dy = Gravity;
}
</code>```

I've simplified the code extremely from the code in my game I'm making using javascript and the html5 canvas.
What you see is my Ground mechanic as it stands. When the player collides with the Ground Block, the Player's .dy value will no longer change along with the Gravity.

What you saw above is how I would normally do it however this can take up a large amount of space if I have 50 FallingM variables. Is it possible to do something like:

```<code>if(Ground[i].isCollided(Player))
{
    Player.dy = 0;
    Gravity = 0;
}
</code>```

So that the function will still run no matter how many Ground variables I add? Thanks ahead of time!

答:
You should use a loop:</p>

```<code>for(var x of Ground) {
   if(x.isCollided(Player)) {
      Player.dy = 0;
      Gravity = 0;
      break;
   }
}
</code>```

Title: Access all variables in an array

Body:
I'm sure this is a simple question but I'm asking it anyways. Basically I'm trying to do the following in javascript (no jquery) with less code:

var Ground = [];

function gameLoop()
{
    Gravity += 0.2

    if(Ground[0].isCollided(Player))
    {
        Player.dy = 0;
        Gravity = 0;
    }
    if(Ground[1].isCollided(Player))
    {
        Player.dy = 0;
        Gravity = 0;
    }
    if(Ground[2].isCollided(Player))
    {
        Player.dy = 0;
        Gravity = 0;
    }

    Player.dy = Gravity;
}

I've simplified the code extremely from the code in my game I'm making using javascript and the html5 canvas.
What you see is my Ground mechanic as it stands. When the player collides with the Ground Block, the Player's .dy value will no longer change along with the Gravity.

What you saw above is how I would normally do it however this can take up a large amount of space if I have 50 FallingM variables. Is it possible to do something like:

if(Ground[i].isCollided(Player))
{
    Player.dy = 0;
    Gravity = 0;
}

So that the function will still run no matter how many Ground variables I add? Thanks ahead of time!

Body:
You should use a loop:

for(var x of Ground) {
   if(x.isCollided(Player)) {
      Player.dy = 0;
      Gravity = 0;
      break;
   }
}


问:
Ability to programmatically remove a Logger
Is there a way to remove a Logger once it has been added? Say via:

```<code>LogManager.GetLogger("loggerName")
</code>```

In my research, it would appear it is not possible. The closest I have found is the ability to call the Clear hierarchy method. This will indeed clear out the existing loggers but I would like to selectively remove them, not to mention this likely isn't the safest thing to be doing.

For some background, I am logging one file per task where there could be potentially thousands of concurrent tasks and hundreds of thousands of tasks per application lifetime. One approach is to create a Logger for each task and then remove it once the task has completed. Without the ability to selectively remove a Logger though, memory will get chewed up by the retired instances.

Of course, there are alternative designs that would work. The problem could be addressed by adding/removing Appenders and Filters as necessary to a given Logger. Also, a pool of Loggers and Appenders could be created and then configured per task.

It obviously isn't a show stopper if there is no way to remove a specific logger once it is added. I'm just curious if there is a way to delete a Logger that I may have missed?

答:
No, it isn't possible. This is specifically mentioned in their FAQ these days:

Logger instances seem to be create only. Why isn't there a method to remove logger instances?
  It is quite nontrivial to define the semantics of a "removed" logger which is still referenced by the user.

Source:
Ability to programmatically remove a Logger

Body:
Is there a way to remove a Logger once it has been added? Say via:

LogManager.GetLogger("loggerName")

In my research, it would appear it is not possible. The closest I have found is the ability to call the Clear hierarchy method. This will indeed clear out the existing loggers but I would like to selectively remove them, not to mention this likely isn't the safest thing to be doing.

For some background, I am logging one file per task where there could be potentially thousands of concurrent tasks and hundreds of thousands of tasks per application lifetime. One approach is to create a Logger for each task and then remove it once the task has completed. Without the ability to selectively remove a Logger though, memory will get chewed up by the retired instances.

Of course, there are alternative designs that would work. The problem could be addressed by adding/removing Appenders and Filters as necessary to a given Logger. Also, a pool of Loggers and Appenders could be created and then configured per task.

It obviously isn't a show stopper if there is no way to remove a specific logger once it is added. I'm just curious if there is a way to delete a Logger that I may have missed?


问:
Hadoop Resource Manager ip address from namenode
I am trying to find out the cluster info for hadoop management, using http://hadoop-namenode-address:50070/jmx. I managed to get the list of datanodes in the cluster but couldn't get info about secondary namenode, resourcemanager and nodemanager IP addresses.

Is there any way to find it out from the namenode, using REST API?

答:
You cannot get information about the secondary namenode, resource manager and node managers from the namenode. The namenode only knows about datanodes.  While all of these servers are part of Hadoop, the resource manager and node managers are the compute part while the namenode, secondary namenode and datanodes are the data part and both can be run independently of each other.

If you're using either Cloudera Manager, or Ambari you can use their respective APIs to identify the services you need or (as @philantrovert suggested)
scrape the configs from the Hadoop xmls (hdfs-site.xml and yarn-site.xml).


问:
Spring Gemfire Cache implementation
I am trying to implement cache mechanism provide by Spring Data GemFire. Has anyone implemented a solution?  I need to check on performance and ease to implement it.

答:
Sonal-

First, you can find plenty of examples in the Spring User Guides, here, for example...

Accessing Data with GemFire,

Caching Data with GemFire, and ...

Accessing GemFire Data with REST

Additionally, there is a Spring GemFire Examples project here.

I have also started work on building a "Reference Implementation" (RI) for Spring Data GemFire/Geode, here.  I have much work to do with this project yet, like documentation (READMEs) in the Repo, but I do plan to keep it up-to-date with my latest developments since I use the code as a basis for all my conference talks.  Anyway, there is plenty of code examples and tests in this GitHub project to keep you busy for awhile.

Then, the Spring Data GemFire and Spring Data Geode GitHub projects themselves, have plenty of tests to show you how to address different application concerns (Configuration, Data Access, Function Execution, etc, etc).

Of particular interests might be the new Annotation-based configuration model for SDG^2 that I am working on.  This is currently a WIP and I am also working on User Guide documentation for this feature/functionality, but it is established and even inspired by the auto-configuration features and Annotations provided by Spring and Spring Boot (e.g. @EnableXYZ).

Users have even started using the Annotation-based configuration model without significant documentation in place since it builds on concepts already available and familiar in Spring Boot.  In fact combining these SDG specific Annotations with Spring Boot makes for a very powerful combination while preserving simple/easy nature to get started, 1 of my primary goals.

Given the lack of documentation yet, you can find more out in the Spring IO blog, where I first blogged about it here.  Then I expanded on this article in a second blog, talking specifically about security.

And if you are really curious, you can follow the latest developments of the Annotation configuration model in my testing efforts.

Finally, of course, as I have already alluded to, as any good developer knows, getting started is as easy following the examples and reviewing Spring Data GemFire Reference Guide and Javadoc.

Don't forget to familiarize yourself with Pivotal GemFire as well!  Javadoc here.

Hope this helps!

-John

Title: Spring Gemfire Cache implementation


问:
How to get a string from a string, starting and ending with a specific string
I've got a string containing similar text like below

```<code>Name: John\n Surname: Smith\n Address: XXX\n
</code>```

It can appear in different order.

I want to get the name value, the surname value and the adress value.

So the question is: how to get a string starting from string "Name: " and ending before "\n", so I get "John" and the code is very readable?

I tried to use the Substring function but it required modyfing the string so I get the correct index of the "\n" part. And I would prefer not to modify the original string so it's more readable.

答:
I would use Regex in these type of situations for 2 reasons:

It is easier to maintain it in these situations. The Substring, Split, Indexof easily get convoluted when the role of the function increases.

It offers more flexibility for future changes

Below is the code to parse it:

```<code>static string ExtractParam(string input, string arg) {
    var match = Regex.Match(input, $@"\b{arg}:\s*(.*?)\n");
    return match.Success ? match.Groups[1].Value : null;
}

static void Main() {
    var input = "Name: John\n Surname: Smith\n Address: XXX\n";

    var name = ExtractParam(input, "Name");
    var surname = ExtractParam(input, "Surname");
    var address = ExtractParam(input, "Address");

    Console.WriteLine($"Name: {name}\n Surname: {surname}\n Address: {address}\n");
}
</code>```

The regex is very simple to understand.

```<code>\b   : Match a word boundary
\s*  : Eat up any unwanted whitespace
.*?  : Match any string in a non-greedy way
()   : Parenthesis are used to capture what we want to return
</code>```

Body:
I've got a string containing similar text like below

Name: John\n Surname: Smith\n Address: XXX\n

It can appear in different order.

I want to get the name value, the surname value and the adress value.

So the question is: how to get a string starting from string "Name: " and ending before "\n", so I get "John" and the code is very readable?

I tried to use the Substring function but it required modyfing the string so I get the correct index of the "\n" part. And I would prefer not to modify the original string so it's more readable.

Body:
I would use Regex in these type of situations for 2 reasons:

It is easier to maintain it in these situations. The Substring, Split, Indexof easily get convoluted when the role of the function increases.

It offers more flexibility for future changes

Below is the code to parse it:

static string ExtractParam(string input, string arg) {
    var match = Regex.Match(input, $@"\b{arg}:\s*(.*?)\n");
    return match.Success ? match.Groups[1].Value : null;
}

static void Main() {
    var input = "Name: John\n Surname: Smith\n Address: XXX\n";

    var name = ExtractParam(input, "Name");
    var surname = ExtractParam(input, "Surname");
    var address = ExtractParam(input, "Address");

    Console.WriteLine($"Name: {name}\n Surname: {surname}\n Address: {address}\n");
}

The regex is very simple to understand.

\b   : Match a word boundary
\s*  : Eat up any unwanted whitespace
.*?  : Match any string in a non-greedy way
()   : Parenthesis are used to capture what we want to return