问:
FreeBSD nginx.pid Permission Denied
Fresh install of nginx on FreeBSD 11.0_RELEASE.  Default nginx.conf with one mod to comment out the pid.  See the conf below.  The error:

```<code>nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: [emerg] open() "/var/run/nginx.pid" failed (13: Permission denied)
nginx: configuration file /usr/local/etc/nginx/nginx.conf test failed
</code>```

Default nginx.conf:

```<code># user  nobody;
worker_processes  1;

# This default error log path is compiled-in to make sure configuration parsing
# errors are logged somewhere, especially during unattended boot when stderr
# isn't normally logged anywhere. This path will be touched on every nginx
# start regardless of error log location configured here. See
# https://trac.nginx.org/nginx/ticket/147 for more info.
#
#error_log  /var/log/nginx/error.log;
#

#pid        logs/nginx.pid;

events {
        worker_connections  1024;
}

http {
        include       mime.types;
        default_type  application/octet-stream;

        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
        #                  '$status $body_bytes_sent "$http_referer" '
        #                  '"$http_user_agent" "$http_x_forwarded_for"';

        #access_log  logs/access.log  main;

        sendfile        on;
        #tcp_nopush     on;

        #keepalive_timeout  0;
        keepalive_timeout  65;

        #gzip  on;

        server {
                listen       80;
                server_name  localhost;

                #charset koi8-r;

                #access_log  logs/host.access.log  main;

                location / {
                        root   /usr/local/www/nginx;
                        index  index.html index.htm;
                }

                #error_page  404              /404.html;

                # redirect server error pages to the static page /50x.html
                #
                error_page   500 502 503 504  /50x.html;
                location = /50x.html {
                        root   /usr/local/www/nginx-dist;
                }

                # proxy the PHP scripts to Apache listening on 127.0.0.1:80
                #
                #location ~ \.php$ {
                #    proxy_pass   http://127.0.0.1;
                #}

                # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
                #
                #location ~ \.php$ {
                #    root           html;
                #    fastcgi_pass   127.0.0.1:9000;
                #    fastcgi_index  index.php;
                #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
                #    include        fastcgi_params;
                #}

                # deny access to .htaccess files, if Apache's document root
                # concurs with nginx's one
                #
                #location ~ /\.ht {
                #    deny  all;
                #}
        }

        # another virtual host using mix of IP-, name-, and port-based configuration
        #
        #server {
        #    listen       8000;
        #    listen       somename:8080;
        #    server_name  somename  alias  another.alias;

        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}

        # HTTPS server
        #
        #server {
        #    listen       443 ssl;
        #    server_name  localhost;

        #    ssl_certificate      cert.pem;
        #    ssl_certificate_key  cert.key;

        #    ssl_session_cache    shared:SSL:1m;
        #    ssl_session_timeout  5m;

        #    ssl_ciphers  HIGH:!aNULL:!MD5;
        #    ssl_prefer_server_ciphers  on;

        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}

}
</code>```

I'm trying to get a basic setup to work, but nginx won't pass tests and won't start.  I am expecting it to start, naturally. My question is "how do I fix this?"  I've never run into this before on FreeBSD, or any other OS for that matter.

The pid is commented out because rc.d already points to the pid, as the error shows.  There is no permanent nginx.pid file in that location.

答:
After installing Nginx do:

```<code>sysrc nginx_enable="YES"
</code>```

Then to start the service

```<code>service nginx start
</code>```

or

```<code>/usr/local/etc/rc.d/nginx start
</code>```

Probably this may also work:

```<code>service nginx configtest
</code>```

By default, the pid is in /var/run/nginx.pid

Later you can just do:

```<code>nginx -t
</code>```

or

```<code>nginx -T
</code>```

And should work with out printing any errors.

Title: FreeBSD nginx.pid Permission Denied

Body:
Fresh install of nginx on FreeBSD 11.0_RELEASE.  Default nginx.conf with one mod to comment out the pid.  See the conf below.  The error:

nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: [emerg] open() "/var/run/nginx.pid" failed (13: Permission denied)
nginx: configuration file /usr/local/etc/nginx/nginx.conf test failed

Default nginx.conf:

# user  nobody;
worker_processes  1;

# This default error log path is compiled-in to make sure configuration parsing
# errors are logged somewhere, especially during unattended boot when stderr
# isn't normally logged anywhere. This path will be touched on every nginx
# start regardless of error log location configured here. See
# https://trac.nginx.org/nginx/ticket/147 for more info.
#
#error_log  /var/log/nginx/error.log;
#

#pid        logs/nginx.pid;

events {
        worker_connections  1024;
}

http {
        include       mime.types;
        default_type  application/octet-stream;

        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
        #                  '$status $body_bytes_sent "$http_referer" '
        #                  '"$http_user_agent" "$http_x_forwarded_for"';

        #access_log  logs/access.log  main;

        sendfile        on;
        #tcp_nopush     on;

        #keepalive_timeout  0;
        keepalive_timeout  65;

        #gzip  on;

        server {
                listen       80;
                server_name  localhost;

                #charset koi8-r;

                #access_log  logs/host.access.log  main;

                location / {
                        root   /usr/local/www/nginx;
                        index  index.html index.htm;
                }

                #error_page  404              /404.html;

                # redirect server error pages to the static page /50x.html
                #
                error_page   500 502 503 504  /50x.html;
                location = /50x.html {
                        root   /usr/local/www/nginx-dist;
                }

                # proxy the PHP scripts to Apache listening on 127.0.0.1:80
                #
                #location ~ \.php$ {
                #    proxy_pass   http://127.0.0.1;
                #}

                # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
                #
                #location ~ \.php$ {
                #    root           html;
                #    fastcgi_pass   127.0.0.1:9000;
                #    fastcgi_index  index.php;
                #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
                #    include        fastcgi_params;
                #}

                # deny access to .htaccess files, if Apache's document root
                # concurs with nginx's one
                #
                #location ~ /\.ht {
                #    deny  all;
                #}
        }

        # another virtual host using mix of IP-, name-, and port-based configuration
        #
        #server {
        #    listen       8000;
        #    listen       somename:8080;
        #    server_name  somename  alias  another.alias;

        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}

        # HTTPS server
        #
        #server {
        #    listen       443 ssl;
        #    server_name  localhost;

        #    ssl_certificate      cert.pem;
        #    ssl_certificate_key  cert.key;

        #    ssl_session_cache    shared:SSL:1m;
        #    ssl_session_timeout  5m;

        #    ssl_ciphers  HIGH:!aNULL:!MD5;
        #    ssl_prefer_server_ciphers  on;

        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}

}

I'm trying to get a basic setup to work, but nginx won't pass tests and won't start.  I am expecting it to start, naturally. My question is "how do I fix this?"  I've never run into this before on FreeBSD, or any other OS for that matter.

The pid is commented out because rc.d already points to the pid, as the error shows.  There is no permanent nginx.pid file in that location.

Body:
After installing Nginx do:

sysrc nginx_enable="YES"

Then to start the service

service nginx start

or

/usr/local/etc/rc.d/nginx start

Probably this may also work:

service nginx configtest

By default, the pid is in /var/run/nginx.pid

Later you can just do:

nginx -t

or

nginx -T

And should work with out printing any errors.


来源: stackexchange

问:
scanf() function doesnt work program in c [GNU/LINUX]
Today i have been coding a came across with this code:

Code

```<code>#include <stdio.h>

main() {

    char letra1;
    char letra2;

    printf("Primera letra: ");
    scanf("%c", &letra1);
    printf("Segunda letra: ");
    scanf("%c", &letra2);

}
</code>```

When I execute the code the first scanf() executes well but the second even didn't execute and close the program and I don't know why.

Execution

```<code>> ej3
Primera letra: A
Segunda letra:
</code>```

Thanks for your time guys.

答: You should use a scanf format string " %c" to skip any pending whitespace characters, including the newline the user typed after the first letter. As posted, the second scanf() reads the \n that is pending in the input stream, so it does not wait for user input.

ContentLicense: CC BY-SA 3.0

Body:
Today i have been coding a came across with this code:

Code

#include <stdio.h>

main() {

    char letra1;
    char letra2;

    printf("Primera letra: ");
    scanf("%c", &letra1);
    printf("Segunda letra: ");
    scanf("%c", &letra2);

}

When I execute the code the first scanf() executes well but the second even didn't execute and close the program and I don't know why.

Execution

> ej3
Primera letra: A
Segunda letra:

Thanks for your time guys.


问:
In intervention/image, how can I change the color of the non-transparent part of a png file?
I want to change my image's color but for it retain transparency. I cannot use the fill() method because it has a regular sharp rule, and my image is irregular. So what should I do with intervention/image?

答:
I found a way to do this with the colorize function in intervention.

```<code>$image = Image::make($path);
$color = '#abcdef';

$r = round((hexdec(substr($color, 1, 2)) * 200) / 255) - 100;
$g = round((hexdec(substr($color, 3, 2)) * 200) / 255) - 100;
$b = round((hexdec(substr($color, 5, 2)) * 200) / 255) - 100;

// Apply or remove color from a neutral #7F7F7F color image
$image->colorize($r,$g,$b);
</code>```

I convert every component of the hex color to dec and then convert it to an scale from -100 to 100 to use it in the colorize function. this has to be done in a fully #7F image which is a totally neutral grey, for it to get the right color.

This is a really old question, but i hope this helps other people who get into the same issue.

Body:
I found a way to do this with the colorize function in intervention.

$image = Image::make($path);
$color = '#abcdef';

$r = round((hexdec(substr($color, 1, 2)) * 200) / 255) - 100;
$g = round((hexdec(substr($color, 3, 2)) * 200) / 255) - 100;
$b = round((hexdec(substr($color, 5, 2)) * 200) / 255) - 100;

// Apply or remove color from a neutral #7F7F7F color image
$image->colorize($r,$g,$b);

I convert every component of the hex color to dec and then convert it to an scale from -100 to 100 to use it in the colorize function. this has to be done in a fully #7F image which is a totally neutral grey, for it to get the right color.

This is a really old question, but i hope this helps other people who get into the same issue.


问:
Custom django foreignfield
Anybody knows how to create a foreignkey field and make it always point to same model, so far I got these.

```<code>class PanMachineTimeUnitField(models.ForeignKey):
    def __init__(self, **kwargs):
        to = 'panbas.PanBasTimeUnit'
        kwargs['verbose_name'] = _('Machine Unit')
        kwargs['related_name'] = 'machine_unit'
        super(PanMachineTimeUnitField, self).__init__(to, **kwargs)
</code>```

But I got errors when on start.
I aim to use it like,

```<code>machine_unit = PanMachineTimeUnitField()
</code>```

No further declarations needed.

Edit:
I want this because, I will have this foreignkey in quiet a few places. If I want to change the verbose_name of field, I want all of my fields to be affected by this change. Verbose name was an example, it may be an another attribute.

I dont want to use settings py to declare the defaults, either.

答:
I recommend that you use only a simple function to create a similarly pre-configured instance of ForeignKey: (not an instance of subclass of ForeignKey)

```<code>def pan_machine_time_unit_field(**kwargs):
    othermodel = 'panbas.PanBasTimeUnit'
    on_delete = models.DO_NOTHING  # or what you need
    kwargs['verbose_name'] = 'Machine Unit'
    kwargs.setdefault('related_name', '+')
    # or: kwargs.setdefault('related_name', "%(app_label)s_%(class)s_related",
    return models.ForeignKey(othermodel, on_delete, **kwargs)

class C(models.Model):
    machine_unit = pan_machine_time_unit_field()
    # or:
    # machine_unit = pan_machine_time_unit_field(related_name='klass_c_children')
</code>```

The related_name attribute is a name used for backward relation from the target object of othermodel to all objects that reference it. That name must be unique on othermodel ('panbas.PanBasTimeUnit', usually something with app and class name that is unique enough) or that name can be '+' if you don't want to create a backward relationship query set. Both variants are implied in the example. Also remember on_delete.

If you would really need to create a subclass (which makes sense if more methods need be customized), you must also define a deconstruct method for migrations. It would be complicated if you need to modify such subclass later. It can be never removed, renamed etc. due to migrations on a custom field. On the other hand, if you create a simple instance of ForeignKey directly by a function, all about migrations can be ignored.

EDIT

Alternatively you can create an abstract base model with that field and create new models by inheritance or multiple inheritance:

```<code>class WithPanBasTimeUnit(models.Model):
    machine_unit = models.ForeignKey(
        'panbas.PanBasTimeUnit',
        models.DO_NOTHING,
        verbose_name=_('Machine Unit'),
        related_name='%(app_label)s_%(class)s_related'
    )

    class Meta:
        abstract = True

class ExampleModel(WithPanBasTimeUnit, ...or more possible base models...):
    ... other fields
</code>```

This solution (inspired by an invalid soution Ykh) useful if you want to add a method to models with that field or to add more fields together, otherwise the original solution is easier.

Title: Custom django foreignfield

Body:
Anybody knows how to create a foreignkey field and make it always point to same model, so far I got these.

class PanMachineTimeUnitField(models.ForeignKey):
    def __init__(self, **kwargs):
        to = 'panbas.PanBasTimeUnit'
        kwargs['verbose_name'] = _('Machine Unit')
        kwargs['related_name'] = 'machine_unit'
        super(PanMachineTimeUnitField, self).__init__(to, **kwargs)

But I got errors when on start.
I aim to use it like,

machine_unit = PanMachineTimeUnitField()

No further declarations needed.

Edit:
I want this because, I will have this foreignkey in quiet a few places. If I want to change the verbose_name of field, I want all of my fields to be affected by this change. Verbose name was an example, it may be an another attribute.

I dont want to use settings py to declare the defaults, either.

Body:
I recommend that you use only a simple function to create a similarly pre-configured instance of ForeignKey: (not an instance of subclass of ForeignKey)

def pan_machine_time_unit_field(**kwargs):
    othermodel = 'panbas.PanBasTimeUnit'
    on_delete = models.DO_NOTHING  # or what you need
    kwargs['verbose_name'] = 'Machine Unit'
    kwargs.setdefault('related_name', '+')
    # or: kwargs.setdefault('related_name', "%(app_label)s_%(class)s_related",
    return models.ForeignKey(othermodel, on_delete, **kwargs)

class C(models.Model):
    machine_unit = pan_machine_time_unit_field()
    # or:
    # machine_unit = pan_machine_time_unit_field(related_name='klass_c_children')

The related_name attribute is a name used for backward relation from the target object of othermodel to all objects that reference it. That name must be unique on othermodel ('panbas.PanBasTimeUnit', usually something with app and class name that is unique enough) or that name can be '+' if you don't want to create a backward relationship query set. Both variants are implied in the example. Also remember on_delete.

If you would really need to create a subclass (which makes sense if more methods need be customized), you must also define a deconstruct method for migrations. It would be complicated if you need to modify such subclass later. It can be never removed, renamed etc. due to migrations on a custom field. On the other hand, if you create a simple instance of ForeignKey directly by a function, all about migrations can be ignored.

EDIT

Alternatively you can create an abstract base model with that field and create new models by inheritance or multiple inheritance:

class WithPanBasTimeUnit(models.Model):
    machine_unit = models.ForeignKey(
        'panbas.PanBasTimeUnit',
        models.DO_NOTHING,
        verbose_name=_('Machine Unit'),
        related_name='%(app_label)s_%(class)s_related'
    )

    class Meta:
        abstract = True

class ExampleModel(WithPanBasTimeUnit, ...or more possible base models...):
    ... other fields

This solution (inspired by an invalid soution Ykh) useful if you want to add a method to models with that field or to add more fields together, otherwise the original solution is easier.


问:
write a jQuery code, where I have to clone a div on button click. New div should have fresh new dropdown list
JQUERY: </p>

```<code> $("#panel_moreInfoDetails_common3").clone().appendTo("#panel_moreInfoDetails_common2");
</code>```

<p>I am able to clone the div, but I need dropdown list in it. Notice that my dropdown options are in foreach loop. HTML is given below
HTML:  </p>

```<code><div class="row" id="panel_moreInfoDetails_common3">
     <div class="delete-moreinfodetails" style="margin-left: 15px;margin-top: 20px;margin-right: 18px;">
         <a data-id="@record.UserFieldIndexNumber" id="button-delete-moreinfodetails" href="javascript:void(0)">
            <i class='icon icon-bin3'></i>
         </a>
     </div>
     <div id="@string.Format("field_{0}_{1}", Model.Page, record.UserFieldIndexNumber)" class="col settings-field @(fields.Contains(string.Format("field_{0}_{1}", Model.Page, record.UserFieldIndexNumber)) ? "hidden" : "")">
        <select class="moreinfo-details" id="@string.Format("field_{0}_{1}", Model.Page, record.UserFieldIndexNumber)" name="@string.Format("field_{0}_{1}", Model.Page, record.UserFieldIndexNumber)" data-field="Info Details">
             <option value="">Select an option</option>
             @foreach (var item in Model.MoreInfoDetailsValues)
             {
                   //selected = record.UserDefinedDropDownField == item.UserDefinedDropDownField ? "selected='selected'" : "";
                   <option data-desc="@item.UserDefinedDropDownField" @selected value="@item.UserDefinedDropDownField">@item.UserDefinedDropDownField</option>
             }
        </select>
     </div>
     <div id="@string.Format("field_{0}_{1}", Model.Page, record.UserFieldIndexNumber)" class="col settings-field @(fields.Contains(string.Format("field_{0}_{1}", Model.Page, record.UserFieldIndexNumber)) ? "hidden" : "")">

          <input data-ufindex="@record.UserFieldIndexNumber" type="text" class="form-control" value="@record.UserFieldValue" />

     </div>
</div>
</code>```

答:
jsfiddle here

jQuery Documentation

.clone( [withDataAndEvents ] [, deepWithDataAndEvents ] )

deepWithDataAndEvents (default: value of withDataAndEvents)

A Boolean indicating whether event handlers and data for all children of the cloned element should be copied. By default its value matches the first argument's value (which defaults to false).

What you are looking for is .clone(true, true)

$("#panel_moreInfoDetails_common3").clone(true, true).appendTo("#panel_moreInfoDetails_common2");

Body:
JQUERY:

 $("#panel_moreInfoDetails_common3").clone().appendTo("#panel_moreInfoDetails_common2");

I am able to clone the div, but I need dropdown list in it. Notice that my dropdown options are in foreach loop. HTML is given below
HTML:

        <select class="moreinfo-details" id="@string.Format("field_{0}_{1}", Model.Page, record.UserFieldIndexNumber)" name="@string.Format("field_{0}_{1}", Model.Page, record.UserFieldIndexNumber)" data-field="Info Details">
             <option value="">Select an option</option>
             @foreach (var item in Model.MoreInfoDetailsValues)
             {
                   //selected = record.UserDefinedDropDownField == item.UserDefinedDropDownField ? "selected='selected'" : "";
                   <option data-desc="@item.UserDefinedDropDownField" @selected value="@item.UserDefinedDropDownField">@item.UserDefinedDropDownField</option>
             }
        </select>

          <input data-ufindex="@record.UserFieldIndexNumber" type="text" class="form-control" value="@record.UserFieldValue" />


问:
Block users in online Android app
I'm developing a mobile online Android game. Some of users violate game rules and I need to block them and restrict using some functions in the app.
What parameters I should to retrieve to identify users devices and then add into DB as blocked? What methods do you use to implement this functionality in your projects?

Update.
Now I already use IP address and ANDROID_ID, but users can change these parameters easily

Update 2.
I already use my own users system for identifying users. User need to create account using email. But the problem is bad users make new accounts and keep violating game rules. So the purpose is to block these users devices to prevent them even to create new account using that blocked device.

答:
ANDROID_ID is not static nor unique, it can change with factory reset, rooted device, if user changes account. Refer to Google documents for more info. And it's not recommended to get Hardware id. If you still want get unique id, you can get wifi or bluetooth adapter's mac address.
For 6+ you can't use The WifiInfo.getMacAddress() method and the BluetoothAdapter.getDefaultAdapter().getAddress() method will both return 02:00:00:00:00:00.. But there are still ways to get them. You can look for it too. I don't remember how it was done but i had done it but it's not recommended by Google to get Hardware ids.

Google Documentation for Unique Ids

Title: Block users in online Android app


问:
What do factory, service and dependency injection exactly mean in angular.js
I am a little confused about the following Angular.js concepts:

factory

service

dependency injection

Can anyone brief me on each one with a simple example or explanation? Any help would be appreciated.

答:
These concepts are part of JavaScript core.

RegExp is a Factory:

console.log(RegExp("[0-9]") );
console.log(RegExp("[a-z]") );
console.log(RegExp("[A-Z]") );
console.log(RegExp("[0-9a-zA-Z]") );```
</div>
</div>
</p>

<p><code>Math</code> is a Service:</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>console.log(Math.PI);
console.log(Math.round(Math.PI));
console.log(Number(Math.random() * 1000).toFixed());
console.log(Number(Math.random() * 10).toPrecision(2));
console.log(Math.floor(Math.random() * 20) + 1);</code>```

call and apply are Dependency Injection:

"use strict";

var foo = {
    min: function min(array) {
        return Math.min.apply(Math, array);
    },

    max: function max(array) {
        return Math.max.apply(Math, array);
    }
};

var bar = foo.min([1,2,3]);
var baz = foo.max([1,2,3]);

console.log("bar: " + bar);
console.log("baz: " + baz);```

References

Meta Programming in ES6 using Symbols | DotNetCurry

John Resig - Fast JavaScript Max/Min

You're Already Using Dependency Injection - Modern Perl Programming

Simple Dependency Injection in Ruby - Sebastian's Blog

SAPUI5: JavaScript Coding Guidelines

Functional JavaScript, Part 3: apply, call, and the arguments object

Body:
These concepts are part of JavaScript core.

RegExp is a Factory:

console.log(RegExp("[0-9]") );
console.log(RegExp("[a-z]") );
console.log(RegExp("[A-Z]") );
console.log(RegExp("[0-9a-zA-Z]") );

Math is a Service:

console.log(Math.PI);
console.log(Math.round(Math.PI));
console.log(Number(Math.random() * 1000).toFixed());
console.log(Number(Math.random() * 10).toPrecision(2));
console.log(Math.floor(Math.random() * 20) + 1);

call and apply are Dependency Injection:

"use strict";

var foo = {
    min: function min(array) {
        return Math.min.apply(Math, array);
    },

    max: function max(array) {
        return Math.max.apply(Math, array);
    }
};

var bar = foo.min([1,2,3]);
var baz = foo.max([1,2,3]);

console.log("bar: " + bar);
console.log("baz: " + baz);

References

Meta Programming in ES6 using Symbols | DotNetCurry

John Resig - Fast JavaScript Max/Min

You're Already Using Dependency Injection - Modern Perl Programming

Simple Dependency Injection in Ruby - Sebastian's Blog

SAPUI5: JavaScript Coding Guidelines

Functional JavaScript, Part 3: apply, call, and the arguments object


问:
Odoo 10 - Get actual row or call function after saving
I'm working on a table that's inside a form. That table is filled automatically with the input of the first field that the user has to do. In the table there is only one column that the user has to enter data in and I made it possible for them to directly enter datas in the table instead of opening a new form.
Those datas need to be checked using values hidden in the table, I just have to check if the value entered by the user respect some tolerances.

Now my problem is, for checking the values I need to know the ids of the values to be able to check with the rights tolerances but I can't get them if the user doesn't save the form.

So I have two questions :
Is it possible to call a function just after saving ? I know I can override the "create" function but that's not what I want, I want a function to be called right after saving, thanks to that I would be able to know the ids and it would be possible to check the values.
If that's not possible, my second question is :
Is it possible to get the row number when the user enters its value in ? It would be useful to get the row number with api.onchange so I could simply loop into the rows until I find the right one and then use the datas that are there.

Thanks for your time

EDIT1 : Here the code I made to loop through the datas :

```<code>class ControlMeasure(models.Model):
    """
        Modèle pour la mesure d'une pièce
    """

# Nom de la table
_name = 'spc.control.measure'

# Champs de la table
name                = fields.Many2one(string='Name', related='control_point_id.measure_type_id', readonly=True)
value               = fields.Float(string='Measured value')
validity            = fields.Boolean(string='Validity', readonly=True)
nominal_value       = fields.Float(string='Nominal value', related='control_point_id.nominal_value', readonly=True)

unit_id             = fields.Many2one('product.uom', string='Unit', related='control_point_id.product_uom_id', readonly=True)
control_part_id     = fields.Many2one('spc.control.part', string='Controled piece')
control_point_id    = fields.Many2one('spc.control.point', string='Control point')

# Champs visuels
control_device_id   = fields.Many2one('spc.control.device', string='Used device', related='control_point_id.control_device_id')

# Vérifie si la valeur mesurée est dans la tolérance
@api.multi
@api.onchange('value')
def is_valid(self):

   # raise Exception('Appel réussi')

    sql= """
            SELECT p.nominal_value, p.inferior_tolerance, p.superior_tolerance FROM spc_control_point p
            WHERE p.control_plan_id = %(ctrlid)s
         """

    if self.control_part_id.control_plan_id == False:
        raise Exception('False ' + str(self.control_part_id.control_plan_id))
    else:
        self.env.cr.execute(sql, {'ctrlid' : self.control_part_id.control_plan_id.id})
        row = self.env.cr.fetchall()

        for i in range(0, len(row)):
            #raise Exception(str(self.value) + ' < ' +  str(row[i][0]) + ' - ' +  str(abs(row[i][1])) +  ' or ' +  str(self.value) + ' > ' +  str(row[i][0]) + ' + ' +  str(abs(row[i][2])))
            if self.value < row[i][0] - abs(row[i][1]) or self.value > row[i][0] + abs(row[i][2]):
                self.validity = False
            else:
                self.validity = True
</code>```

Here the whole code works how I want except the loop that I don't know how to make it properly, I checked if the datas which are tested are the right ones and they are, the test itself works as I want, but as you see everytime I check all the rows and the end, only the last one count.
That's why I came with the idea to get the row number where the user clicked but I don't know if it's possible to do it or even call a function after saving which would be easy because I would know the ids and I could check the datas after saving.

答:
Two possible solutions come up on my mind:

As you mentioned, you can create a method with @api.onchange decorator, to check the entered data each time user passed it in, without actually saving it or any other manipulations.

```<code>@api.multi
@api.onchange('your_field', 'your_field2')
def _onchange_your_fields(self):
    for record in self:
        if record.your_field != record.validation_field:
            raise exceptions.ValidationError(_('Sorry, pal, try again'))
</code>```

The good thing about this, is that it will validate the field's value rapidly: after the user will put a cursor on the next field. Although, it can be irritating from a user's perspective.

You can try @api.constrains decorator. It behaves slightly different from the @api.onchange decorator, but it's a common practice to use it for a validation.

```<code>@api.multi
@api.constrains('your_field', 'your_field2')
def _check_your_fields(self):
    for record in self:
        if record.your_field != record.validation_field:
            raise exceptions.ValidationError(_('Sorry, pal, try again'))
</code>```

The difference is that it will be called after you'll hit "Save" button, but before actually executing create or write functions.

Hope, that will help.

UPDATE 1:

```<code># Vérifie si la valeur mesurée est dans la tolérance
@api.multi
@api.onchange('value')
def validate(self):
    # Because we are using @api.multi decorator, our 'self' is actually a recordset.
    # That means, that we cannot treat it as a regular record, so "self.control_part_id...." would be an odd code
    for record in self:
        # So now we took a one record from recordset, and now we can continue on validating it.
        if not record.control_part_id.control_plan_id:
            raise exceptions.UserError(_('False %s' % str(record.control_part_id.control_plan_id)))

        # You executed here a SQL query, which is actually takes our records from database, not from cache.
        # But we haven't write(or create) our changes yet, and that makes no sense.

        # What we wanna do, is to take values from cache and validate them. You can simply do it just via model's field
        validation_record = record.control_point_id

        if record.value < validation_row.nominal_value - validation_row.inferior_tolerance \
            or record.value > validation_row.nominal_value + validation_row.superior_tolerance:
                record.validity = True

        else:
            # You can get rid of this line by simply defining the default value of record.validity as "False"
            record.validity = False

# I recommend you to check the final validation this way
@api.multi
@api.constrains('validity')
def check_valid(self):
    for record in self:
        if not record.validity:
            raise exceptions.ValidationError(_("Cannot pass a validation rest"))
</code>```

Body:
I'm working on a table that's inside a form. That table is filled automatically with the input of the first field that the user has to do. In the table there is only one column that the user has to enter data in and I made it possible for them to directly enter datas in the table instead of opening a new form.
Those datas need to be checked using values hidden in the table, I just have to check if the value entered by the user respect some tolerances.

Now my problem is, for checking the values I need to know the ids of the values to be able to check with the rights tolerances but I can't get them if the user doesn't save the form.

So I have two questions :
Is it possible to call a function just after saving ? I know I can override the "create" function but that's not what I want, I want a function to be called right after saving, thanks to that I would be able to know the ids and it would be possible to check the values.
If that's not possible, my second question is :
Is it possible to get the row number when the user enters its value in ? It would be useful to get the row number with api.onchange so I could simply loop into the rows until I find the right one and then use the datas that are there.

Thanks for your time

EDIT1 : Here the code I made to loop through the datas :

class ControlMeasure(models.Model):
    """
        Modèle pour la mesure d'une pièce
    """

# Nom de la table
_name = 'spc.control.measure'

# Champs de la table
name                = fields.Many2one(string='Name', related='control_point_id.measure_type_id', readonly=True)
value               = fields.Float(string='Measured value')
validity            = fields.Boolean(string='Validity', readonly=True)
nominal_value       = fields.Float(string='Nominal value', related='control_point_id.nominal_value', readonly=True)

unit_id             = fields.Many2one('product.uom', string='Unit', related='control_point_id.product_uom_id', readonly=True)
control_part_id     = fields.Many2one('spc.control.part', string='Controled piece')
control_point_id    = fields.Many2one('spc.control.point', string='Control point')

# Champs visuels
control_device_id   = fields.Many2one('spc.control.device', string='Used device', related='control_point_id.control_device_id')

# Vérifie si la valeur mesurée est dans la tolérance
@api.multi
@api.onchange('value')
def is_valid(self):

   # raise Exception('Appel réussi')

    sql= """
            SELECT p.nominal_value, p.inferior_tolerance, p.superior_tolerance FROM spc_control_point p
            WHERE p.control_plan_id = %(ctrlid)s
         """

    if self.control_part_id.control_plan_id == False:
        raise Exception('False ' + str(self.control_part_id.control_plan_id))
    else:
        self.env.cr.execute(sql, {'ctrlid' : self.control_part_id.control_plan_id.id})
        row = self.env.cr.fetchall()

        for i in range(0, len(row)):
            #raise Exception(str(self.value) + ' < ' +  str(row[i][0]) + ' - ' +  str(abs(row[i][1])) +  ' or ' +  str(self.value) + ' > ' +  str(row[i][0]) + ' + ' +  str(abs(row[i][2])))
            if self.value < row[i][0] - abs(row[i][1]) or self.value > row[i][0] + abs(row[i][2]):
                self.validity = False
            else:
                self.validity = True

Here the whole code works how I want except the loop that I don't know how to make it properly, I checked if the datas which are tested are the right ones and they are, the test itself works as I want, but as you see everytime I check all the rows and the end, only the last one count.
That's why I came with the idea to get the row number where the user clicked but I don't know if it's possible to do it or even call a function after saving which would be easy because I would know the ids and I could check the datas after saving.

Body:
Two possible solutions come up on my mind:

As you mentioned, you can create a method with @api.onchange decorator, to check the entered data each time user passed it in, without actually saving it or any other manipulations.

@api.multi
@api.onchange('your_field', 'your_field2')
def _onchange_your_fields(self):
    for record in self:
        if record.your_field != record.validation_field:
            raise exceptions.ValidationError(_('Sorry, pal, try again'))

The good thing about this, is that it will validate the field's value rapidly: after the user will put a cursor on the next field. Although, it can be irritating from a user's perspective.

You can try @api.constrains decorator. It behaves slightly different from the @api.onchange decorator, but it's a common practice to use it for a validation.

@api.multi
@api.constrains('your_field', 'your_field2')
def _check_your_fields(self):
    for record in self:
        if record.your_field != record.validation_field:
            raise exceptions.ValidationError(_('Sorry, pal, try again'))

The difference is that it will be called after you'll hit "Save" button, but before actually executing create or write functions.

Hope, that will help.

UPDATE 1:

# Vérifie si la valeur mesurée est dans la tolérance
@api.multi
@api.onchange('value')
def validate(self):
    # Because we are using @api.multi decorator, our 'self' is actually a recordset.
    # That means, that we cannot treat it as a regular record, so "self.control_part_id...." would be an odd code
    for record in self:
        # So now we took a one record from recordset, and now we can continue on validating it.
        if not record.control_part_id.control_plan_id:
            raise exceptions.UserError(_('False %s' % str(record.control_part_id.control_plan_id)))

        # You executed here a SQL query, which is actually takes our records from database, not from cache.
        # But we haven't write(or create) our changes yet, and that makes no sense.

        # What we wanna do, is to take values from cache and validate them. You can simply do it just via model's field
        validation_record = record.control_point_id

        if record.value < validation_row.nominal_value - validation_row.inferior_tolerance \
            or record.value > validation_row.nominal_value + validation_row.superior_tolerance:
                record.validity = True

        else:
            # You can get rid of this line by simply defining the default value of record.validity as "False"
            record.validity = False

# I recommend you to check the final validation this way
@api.multi
@api.constrains('validity')
def check_valid(self):
    for record in self:
        if not record.validity:
            raise exceptions.ValidationError(_("Cannot pass a validation rest"))


问:
In R, how do you subset rows of a dataframe based on values in a vector
I'd like to generate a subset based on factor values contained in a vector. I've included my working code for a simple example. However, what if I have many columns (>10) and I don't wish to list each column using "|" (OR), is there a better way to do this? My example below uses LETTERS but I am dealing with factors (people's names).</p>

```<code>set.seed(37)
df <- data.frame(id1=sample(LETTERS, 20),id2=sample(LETTERS, 20))
L <- c("A","B","E")
subset(df, id1 %in% L | id2 %in% L )
   id1 id2
2    B   V
10   C   B
11   F   A
14   A   F
19   E   S
</code>```

答:
You can use Reduce to construct the OR condition:

```<code>subset(df, Reduce("|", lapply(df, `%in%`, L)))

#   id1 id2
#2    B   V
#10   C   B
#11   F   A
#14   A   F
#19   E   S
</code>```

Or use rowSums to check if there is any letter matching in each row:

```<code>subset(df, rowSums(sapply(df, `%in%`, L)) != 0)

#   id1 id2
#2    B   V
#10   C   B
#11   F   A
#14   A   F
#19   E   S
</code>```

Body:
I'd like to generate a subset based on factor values contained in a vector. I've included my working code for a simple example. However, what if I have many columns (>10) and I don't wish to list each column using "|" (OR), is there a better way to do this? My example below uses LETTERS but I am dealing with factors (people's names).

set.seed(37)
df <- data.frame(id1=sample(LETTERS, 20),id2=sample(LETTERS, 20))
L <- c("A","B","E")
subset(df, id1 %in% L | id2 %in% L )
   id1 id2
2    B   V
10   C   B
11   F   A
14   A   F
19   E   S

Body:
You can use Reduce to construct the OR condition:

subset(df, Reduce("|", lapply(df, `%in%`, L)))

#   id1 id2
#2    B   V
#10   C   B
#11   F   A
#14   A   F
#19   E   S

Or use rowSums to check if there is any letter matching in each row:

subset(df, rowSums(sapply(df, `%in%`, L)) != 0)

#   id1 id2
#2    B   V
#10   C   B
#11   F   A
#14   A   F
#19   E   S


答:
You need to calculate a cumulative sum and then aggregate:</p>

```<code>select date(booking_time), art, max(running_stock)
from (select t.*,
             (@s := if(@a = t.art, @s + weight,
                       if @a := t.art, weight, weight)
                      )
             ) as running_stock
      from t cross join
           (select @s := 0, @a := 0) params
      order by t.art, t.booking_time
     ) t
group by date(booking_time), art;
</code>```

Title: SQL peak value per day

Body:
i have a following table:

   id |  booking_time       | art   | weight
   ---+---------------------+-------+------
    1 | 2017-06-18 10:34:09 | wood  |  1000
    2 | 2017-06-18 11:31:11 | wood  |  2000
    3 | 2017-06-18 14:11:25 | stone |  1000
    4 | 2017-06-18 16:47:37 | wood  |  -300
    5 | 2017-06-19 10:49:21 | wood  |   100
    6 | 2017-06-19 12:41:02 | wood  | -1000
    7 | 2017-06-19 12:49:54 | wood  |   200

what am i trying to get is peak value in stock per day:

   booking_day |  art  | peak
   ------------+-------+-------
    2017-06-18 | wood  | 3000
    2017-06-16 | stone | 1000
    2017-06-19 | wood  | 2800

so for 18.06 highest amount of wood in stock would be 3000 (1000 + 2000); at the end of the day only 2700 (1000 + 2000 - 300) left

for 19.06 we start at 2700 wood from yesterday reaching peak with 2800 (2700 + 100); amount left at end of the day would be 2000 (2700 + 100 - 1000 + 200)

is it even possible with SQL? maybe i need another table to store daily amounts or something like that

Body:
You need to calculate a cumulative sum and then aggregate:

select date(booking_time), art, max(running_stock)
from (select t.*,
             (@s := if(@a = t.art, @s + weight,
                       if @a := t.art, weight, weight)
                      )
             ) as running_stock
      from t cross join
           (select @s := 0, @a := 0) params
      order by t.art, t.booking_time
     ) t
group by date(booking_time), art;


问:
Z Shell "autoload" builtin - what is it good for?
I have been using the Z shell for a while now, and I am starting to be curious. One thing I have stumbled at when writing my own functions is "autoload".

According to the zshbuiltins(1) man page autoload is "equivalent to functions -u" (with an exception), which is "equivalent to typeset -f" (with an exception). However, after looking at the autlooad use of, say functions/Prompts/promptinit, I think I have an idea what it does.

I think of autoload as, well, kind of "import" statement.

But why is "autoload foo" superior to "source bar"? I don't get that.

答:
As stated in the zsh documentation:

A function can be marked as undefined
  using the autoload builtin (or
  functions -u or typeset -fu). Such
  a function has no body. When the
  function is first executed, the shell
  searches for its definition using the
  elements of the fpath variable. [...]

autoload allows for functions to be specified without a body which then get automatically loaded when used ;)

source however takes as argument a script which is then executed in the environment of the current session - i.e. you will retain all changes the script does to the environment, which is not the case when just executing the script.

I think this feature is beneficial when having lots of utilities in functions. It allows for faster startup (all the code for the autoload functions need not be loaded) and may keep the memory footprint of the shell smaller.


问:
Pandas 0.20.2 to_sql() using MySQL
I'm trying to write a dataframe to a MySQL table but am getting a (111 Connection refused) error.

I followed the accepted answer here:
Writing to MySQL database with pandas using SQLAlchemy, to_sql

Answer's code:

```<code>import pandas as pd
import mysql.connector
from sqlalchemy import create_engine

engine = create_engine('mysql+mysqlconnector://[user]:[pass]@[host]:[port]/[schema]', echo=False)
data.to_sql(name='sample_table2', con=engine, if_exists = 'append', index=False)
</code>```

...and the create_engine() line worked without error, but the to_sql() line failed with this error:

```<code>(mysql.connector.errors.InterfaceError) 2003: Can't connect to MySQL server on 'localhost:3306' (111 Connection refused)
</code>```

How I connect to my MySQL database / table is not really relevant, so completely different answers are appreciated, but given the deprecation of the MySQL 'flavor' in pandas 0.20.2, what is the proper way to write a dataframe to MySQL?

答:
Thanks to a tip from @AndyHayden, this answer was the trick.  Basically replacing mysqlconnector with mysqldb was the linchpin.

```<code>engine = create_engine('mysql+mysqldb://[user]:[pass]@[host]:[port]/[schema]', echo = False)
df.to_sql(name = 'my_table', con = engine, if_exists = 'append', index = False)
</code>```

Where [schema] is the database name, and in my particular case, :[port] is omitted with [host] being localhost.

Title: Pandas 0.20.2 to_sql() using MySQL

Body:
I'm trying to write a dataframe to a MySQL table but am getting a (111 Connection refused) error.

I followed the accepted answer here:
Writing to MySQL database with pandas using SQLAlchemy, to_sql

Answer's code:

import pandas as pd
import mysql.connector
from sqlalchemy import create_engine

engine = create_engine('mysql+mysqlconnector://[user]:[pass]@[host]:[port]/[schema]', echo=False)
data.to_sql(name='sample_table2', con=engine, if_exists = 'append', index=False)

...and the create_engine() line worked without error, but the to_sql() line failed with this error:

(mysql.connector.errors.InterfaceError) 2003: Can't connect to MySQL server on 'localhost:3306' (111 Connection refused)

How I connect to my MySQL database / table is not really relevant, so completely different answers are appreciated, but given the deprecation of the MySQL 'flavor' in pandas 0.20.2, what is the proper way to write a dataframe to MySQL?

Body:
Thanks to a tip from @AndyHayden, this answer was the trick.  Basically replacing mysqlconnector with mysqldb was the linchpin.

engine = create_engine('mysql+mysqldb://[user]:[pass]@[host]:[port]/[schema]', echo = False)
df.to_sql(name = 'my_table', con = engine, if_exists = 'append', index = False)

Where [schema] is the database name, and in my particular case, :[port] is omitted with [host] being localhost.


问:
Merge replicate data from identical columns in different csv files
I'm trying to merge replicate data from two columns from different csv files into a single column (of the same name). Each row is a different timepoint. I feel like this should be easy, but I'm new to coding and using the merge or rbind functions haven't gotten me very far.

For example:
data1

A

0 1

2 1

4 1

data2

A

0 2

2 2

4 2

I want:
data_combined

A

0 1 2

2 1 2

4 1 2

Thanks for any help!

答:
If I got your question right, here is your answer:

```<code>data_combined <- data.frame(data1, data2[,2])
</code>```

if it is not what you meant, explain more so that I could help you

Body:
If I got your question right, here is your answer:

data_combined <- data.frame(data1, data2[,2])

if it is not what you meant, explain more so that I could help you


问:
Why isn't it printing (file handling)?
I am not very sure where I have gone wrong.

I am trying to print the contents from the page in files.

Here is the code

```<code>File>>> If you fell down yesterday, stand up today
</code>```

```<code>text_file = open("files.txt", "r")
print(text_file)
</code>```

答:
The way this works in Python is that it takes the file and stores it as just a file in that variable. Thus, you're trying to print a literal file, not the contents. There are a few ways to read things, assuming fvar as the file object for a text doc.

```<code>fulltext = fvar.read()
</code>```

That will put the full text as a string.

```<code>oneline = fvar.readline()
</code>```

Puts one line as the string oneline. Best used in loops. It will read the next line each time it runs - when it hits the end, it will return an empty string.

```<code>linelist = fvar.readlines()
</code>```

This takes every line, as read with readline, and puts it into a list. Performs the same as a loop that reads each line with readline and appends each line to a list.

Hope that helps!

Title: Why isn't it printing (file handling)?

Body:
I am not very sure where I have gone wrong.

I am trying to print the contents from the page in files.

Here is the code

File>>> If you fell down yesterday, stand up today

text_file = open("files.txt", "r")
print(text_file)

Body:
The way this works in Python is that it takes the file and stores it as just a file in that variable. Thus, you're trying to print a literal file, not the contents. There are a few ways to read things, assuming fvar as the file object for a text doc.

fulltext = fvar.read()

That will put the full text as a string.

oneline = fvar.readline()

Puts one line as the string oneline. Best used in loops. It will read the next line each time it runs - when it hits the end, it will return an empty string.

linelist = fvar.readlines()

This takes every line, as read with readline, and puts it into a list. Performs the same as a loop that reads each line with readline and appends each line to a list.

Hope that helps!


问:
MDX Exclude results where a value exists in a hierarchy
Please excuse me if I get some of the terminology wrong, I am relatively new to MDX/OLAP.

I have a dimension for determining ethical walls. The dimension looks like this:

```<code>Ethical Wall (dimension)
   --> Matter ID (hierarchy)
   --> Walled (hierarchy)
   --> White Listed Initials (hierarchy)
   --> Black Listed Initials (hierarchy)
</code>```

The [Walled] hierarchy contains either true or false, depending on if the matter has a wall applied.

The White and Black list hierarchies contain user initials of people who either have or do not have access to a matter, respectively. Note, matters are either white listed or black listed, it is never a combination of the two.

I have been able to account for the no wall scenario and white list scenario relatively easily, but am having lots of issues with the black list scenario. This is the where clause I have come up with so far:

```<code>({
    (
        [Ethical Wall].[Walled].&[True]
        ,[Ethical Wall].[White Listed Initials].&[XXX]
        ,[Ethical Wall].[Black Listed Initials].&[]
    )
    ,(
        [Ethical Wall].[Walled].&[True]
        ,[Ethical Wall].[White Listed Initials].&[]
        ,-{[Ethical Wall].[Black Listed Initials].&[XXX]}
    )
    ,(
        [Ethical Wall].[Walled].&[False]
        ,[Ethical Wall].[White Listed Initials].&[]
        ,[Ethical Wall].[Black Listed Initials].&[]
    )
})
</code>```

Stripping down and representing it in a table the initial dataset, from which I am filtering down, looks something like this:

I want to select only the Ids to which the user with initials XXX has access. Applying the filter from above I get back all 3 Ids. The result set I am looking for is only Id 1 and 2. The filer from above matches like this:

I understand why my filter is retrieving all 3 Ids, but I don't understand how to revise the middle portion of the filter to exclude Ids properly. This is the offending portion of the filter:

```<code>    ,(
        [Ethical Wall].[Walled].&[True]
        ,[Ethical Wall].[White Listed Initials].&[]
        ,-{[Ethical Wall].[Black Listed Initials].&[XXX]}
    )
</code>```

How can I revise my filter to match on that data set like this?

答:
Why can't you simplify to this?</p>

```<code>WHERE
({
    (
        [Ethical Wall].[Walled].&[True]
        ,[Ethical Wall].[White Listed Initials].&[XXX]
    )
    ,(
        [Ethical Wall].[Walled].&[False]
        ,[Ethical Wall].[White Listed Initials].&[]
    )
});
</code>```

Body:
Please excuse me if I get some of the terminology wrong, I am relatively new to MDX/OLAP.

I have a dimension for determining ethical walls. The dimension looks like this:

Ethical Wall (dimension)
   --> Matter ID (hierarchy)
   --> Walled (hierarchy)
   --> White Listed Initials (hierarchy)
   --> Black Listed Initials (hierarchy)

The [Walled] hierarchy contains either true or false, depending on if the matter has a wall applied.

The White and Black list hierarchies contain user initials of people who either have or do not have access to a matter, respectively. Note, matters are either white listed or black listed, it is never a combination of the two.

I have been able to account for the no wall scenario and white list scenario relatively easily, but am having lots of issues with the black list scenario. This is the where clause I have come up with so far:

({
    (
        [Ethical Wall].[Walled].&[True]
        ,[Ethical Wall].[White Listed Initials].&[XXX]
        ,[Ethical Wall].[Black Listed Initials].&[]
    )
    ,(
        [Ethical Wall].[Walled].&[True]
        ,[Ethical Wall].[White Listed Initials].&[]
        ,-{[Ethical Wall].[Black Listed Initials].&[XXX]}
    )
    ,(
        [Ethical Wall].[Walled].&[False]
        ,[Ethical Wall].[White Listed Initials].&[]
        ,[Ethical Wall].[Black Listed Initials].&[]
    )
})

Stripping down and representing it in a table the initial dataset, from which I am filtering down, looks something like this:

I want to select only the Ids to which the user with initials XXX has access. Applying the filter from above I get back all 3 Ids. The result set I am looking for is only Id 1 and 2. The filer from above matches like this:

I understand why my filter is retrieving all 3 Ids, but I don't understand how to revise the middle portion of the filter to exclude Ids properly. This is the offending portion of the filter:

    ,(
        [Ethical Wall].[Walled].&[True]
        ,[Ethical Wall].[White Listed Initials].&[]
        ,-{[Ethical Wall].[Black Listed Initials].&[XXX]}
    )

How can I revise my filter to match on that data set like this?

Body:
Why can't you simplify to this?

WHERE
({
    (
        [Ethical Wall].[Walled].&[True]
        ,[Ethical Wall].[White Listed Initials].&[XXX]
    )
    ,(
        [Ethical Wall].[Walled].&[False]
        ,[Ethical Wall].[White Listed Initials].&[]
    )
});


问:
How to set an icon image for an application?
When I start my application, the icon on the desktop looks like this:

I want to know if there is a way I can change that image to an image of my choosing?

答:
```<code>frame.setIconImage(new ImageIcon(getClass().getResource("logo.png")).getImage()
</code>```

This works when the image is in the same package as the class.
Note: It is better to have images in a resource folder

Body:
frame.setIconImage(new ImageIcon(getClass().getResource("logo.png")).getImage()

This works when the image is in the same package as the class.
Note: It is better to have images in a resource folder


问:
Capturing HTTP traffic using Fiddler from Java applicaiton
I'm having difficulty using Fiddler to diagnose a problem in a Java application to connect to Microsoft Graph's API. Here's my basic environment:

JDK: 1.8.0_131
Apache HttpClient: 4.4
Fiddler: 4.6

The endpoint I'm trying to reach is https://login.microsoftonline.com/common/oauth2/authorize The problem I have is that the traffic is not captured by Fiddler.

I've tried various things as suggested by Googling for this problem, but have not got it to work. In my application, I set the proxy settings as follows:

```<code>httpClientBuilder.setProxy(new HttpHost(proxyServer, proxyPort, proxyScheme));
httpClientBuilder.build();
</code>```

If I use ("127.0.0.1", 8888, "https"): then the error that I see is:
Unrecognized SSL message, plaintext connection?

If I use
("127.0.0.1", 8888, "http"): then the error that I see is: I/O
exception (org.apache.http.conn.UnsupportedSchemeException) caught
when processing request to
{tls}->http://127.0.0.1:8888->https://login.microsoftonline.com:443:
http protocol is not supported

I've tried other things (for example "localhost", "localhost.", and so on as suggested, and setting: -Djsse.enableSNIExtension=false), but so far no luck. Hopefully I am missing something obvious! Thanks for any help.

答:
It turns out that the second configuration for the proxy is the correct one to use (using "http"), but what I had forgotten was that I was setting a ConnectionSocketFactoryRegistry in my HttpClientBuilder. I had not registered a ConnectionSocketFactory for "http" and this caused the second configuration to fail. I added this to the HttpClientBuilder, and now I can see traffic being captured by Fiddler:</p>

```<code>connectionSocketFactoryRegistryBuilder.register("http", new PlainConnectionSocketFactory());
</code>```

Body:
I'm having difficulty using Fiddler to diagnose a problem in a Java application to connect to Microsoft Graph's API. Here's my basic environment:

JDK: 1.8.0_131
Apache HttpClient: 4.4
Fiddler: 4.6

The endpoint I'm trying to reach is https://login.microsoftonline.com/common/oauth2/authorize The problem I have is that the traffic is not captured by Fiddler.

I've tried various things as suggested by Googling for this problem, but have not got it to work. In my application, I set the proxy settings as follows:

httpClientBuilder.setProxy(new HttpHost(proxyServer, proxyPort, proxyScheme));
httpClientBuilder.build();

If I use ("127.0.0.1", 8888, "https"): then the error that I see is:
Unrecognized SSL message, plaintext connection?

If I use
("127.0.0.1", 8888, "http"): then the error that I see is: I/O
exception (org.apache.http.conn.UnsupportedSchemeException) caught
when processing request to
{tls}->http://127.0.0.1:8888->https://login.microsoftonline.com:443:
http protocol is not supported

I've tried other things (for example "localhost", "localhost.", and so on as suggested, and setting: -Djsse.enableSNIExtension=false), but so far no luck. Hopefully I am missing something obvious! Thanks for any help.

Body:
It turns out that the second configuration for the proxy is the correct one to use (using "http"), but what I had forgotten was that I was setting a ConnectionSocketFactoryRegistry in my HttpClientBuilder. I had not registered a ConnectionSocketFactory for "http" and this caused the second configuration to fail. I added this to the HttpClientBuilder, and now I can see traffic being captured by Fiddler:

connectionSocketFactoryRegistryBuilder.register("http", new PlainConnectionSocketFactory());


问:
How to resize the jQuery DatePicker control
I'm using the jQuery DatePicker control for the first time. I've got it working on my form, but it's about twice as big as I would like, and about 1.5 times as big as the demo on the jQuery UI page. Is there some simple setting I'm missing to control the size?

Edit: I found a clue, but it opens up new problems. In the CSS file, it states the component will scale according to the parent element's font size. They recommend setting

```<code>body {font-size: 62.5%;}
</code>```

to make 1em = 10px. Doing this gives me a nicely sized datepicker, but obviously it messes up the rest of my site (I currently have font-size: .9em).

I tried throwing a DIV around my text box and setting its font size, but it seems to ignore that. So there must be some way to shrink the datepicker by changing the font of its parent, but how do I do that without messing up the rest of my site?

答:
Add

```<code>div.ui-datepicker, .ui-datepicker td{
 font-size:10px;
}
</code>```

in a stylesheet loaded after the ui-files.
This will also resize the date items.

OwnerDisplayName: gfrizzle

ContentLicense: CC BY-SA 2.5

Body:
I'm using the jQuery DatePicker control for the first time. I've got it working on my form, but it's about twice as big as I would like, and about 1.5 times as big as the demo on the jQuery UI page. Is there some simple setting I'm missing to control the size?

Edit: I found a clue, but it opens up new problems. In the CSS file, it states the component will scale according to the parent element's font size. They recommend setting

body {font-size: 62.5%;}

to make 1em = 10px. Doing this gives me a nicely sized datepicker, but obviously it messes up the rest of my site (I currently have font-size: .9em).

I tried throwing a DIV around my text box and setting its font size, but it seems to ignore that. So there must be some way to shrink the datepicker by changing the font of its parent, but how do I do that without messing up the rest of my site?

LastEditorDisplayName: gfrizzle

Body:
Add

div.ui-datepicker, .ui-datepicker td{
 font-size:10px;
}

in a stylesheet loaded after the ui-files.
This will also resize the date items.


问:
Compressed representation of polygons
I have a lot of (millions) of polygons from openstreetmap-data with mostly (more than 99%) exactly four coordinates representing houses.

Example

I currently save the four coordinates for each house explicitly as Tuple of floats (Latitude and Longitude), hence taking 32 bytes of memory.

Is there a way to store this information in a compressed way (fewer than 32 byte) since the four coordinates only differ very few in the last decimals?

答:
As @MBo said, you can store one corner of each house and compress the other three corners as relative to the first corner.

Also, if buildings are so similar you can set a "dictionary" of buildings. For each building you store its index in the dictionary and some feature, like its first corner coordinates and rotation.

Title: Compressed representation of polygons


问:
Different plot size for plt.show() and fig.savefig()
I have to create some plots that I want to integrate into a scientific document. Due to space limitations the size of the plot as well as the font size is rather small.
For debugging/convenience reasons it would be nice to interactively show the plot using plt.show() with a larger size for the plot and fonts, before storing it as a PDF file using fig.savefig(...) with smaller sizes.

I achieved to set a different plot size before displaying it. However, I was not able to update the font size. Any ideas how this would be possible?

```<code>#!/usr/bin/env python

import matplotlib as mpl
from pylab import *

params = {
    'text.usetex': True,
    'font.size': 6,
}
mpl.rcParams.update(params)

fig = plt.figure(figsize=(3,2))
ax = fig.add_subplot(111)

x = [1, 2, 3]
y = [2, 2, 2]

ax.plot(x,y, label='label')

plt.tight_layout()
fig.savefig('plot.pdf')

fig.set_size_inches(10, 8, forward=True)
params = {'font.size': 20}
mpl.rcParams.update(params)
plt.show()
</code>```

I am using Matplotlib 2.0.2, but also tried 1.5.3.

答:
Tangential note: you shouldn't from pylab import *, that will clutter your namespace with all sorts of needless things:

```<code>>>> len(globals())
6
>>> from pylab import *
>>> len(globals())
988
</code>```

As you noticed, I don't think that setting any of the rc parameters affects existing plots. You might have to get your hands dirty, although only a bit: there is only a handful of children of your Axes that you need to modify:

```<code>import matplotlib as mpl
import matplotlib.pyplot as plt # <-- all you need here

params = {
    'text.usetex': True,
    'font.size': 6,
}
mpl.rcParams.update(params)

fig = plt.figure(figsize=(3,2))
ax = fig.add_subplot(111)

x = [1, 2, 3]
y = [2, 2, 2]

ax.plot(x,y, label='label')

plt.tight_layout()
fig.savefig('plot.pdf')

fig.set_size_inches(10, 8, forward=True)
# v--- change title and axeslabel font sizes manually
for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] +
             ax.get_xticklabels() + ax.get_yticklabels()):
    item.set_fontsize(20)
plt.show()
</code>```

Saved (left) and shown (right) version:

Arguably, it might be more elegant (or a different kind of messy) to just close your saved plots, modify your rcParams, and redraw the versions you want to show.

Body:
I have to create some plots that I want to integrate into a scientific document. Due to space limitations the size of the plot as well as the font size is rather small.
For debugging/convenience reasons it would be nice to interactively show the plot using plt.show() with a larger size for the plot and fonts, before storing it as a PDF file using fig.savefig(...) with smaller sizes.

I achieved to set a different plot size before displaying it. However, I was not able to update the font size. Any ideas how this would be possible?

#!/usr/bin/env python

import matplotlib as mpl
from pylab import *

params = {
    'text.usetex': True,
    'font.size': 6,
}
mpl.rcParams.update(params)

fig = plt.figure(figsize=(3,2))
ax = fig.add_subplot(111)

x = [1, 2, 3]
y = [2, 2, 2]

ax.plot(x,y, label='label')

plt.tight_layout()
fig.savefig('plot.pdf')

fig.set_size_inches(10, 8, forward=True)
params = {'font.size': 20}
mpl.rcParams.update(params)
plt.show()

I am using Matplotlib 2.0.2, but also tried 1.5.3.

Body:
Tangential note: you shouldn't from pylab import *, that will clutter your namespace with all sorts of needless things:

>>> len(globals())
6
>>> from pylab import *
>>> len(globals())
988

As you noticed, I don't think that setting any of the rc parameters affects existing plots. You might have to get your hands dirty, although only a bit: there is only a handful of children of your Axes that you need to modify:

import matplotlib as mpl
import matplotlib.pyplot as plt # <-- all you need here

params = {
    'text.usetex': True,
    'font.size': 6,
}
mpl.rcParams.update(params)

fig = plt.figure(figsize=(3,2))
ax = fig.add_subplot(111)

x = [1, 2, 3]
y = [2, 2, 2]

ax.plot(x,y, label='label')

plt.tight_layout()
fig.savefig('plot.pdf')

fig.set_size_inches(10, 8, forward=True)
# v--- change title and axeslabel font sizes manually
for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] +
             ax.get_xticklabels() + ax.get_yticklabels()):
    item.set_fontsize(20)
plt.show()

Saved (left) and shown (right) version:

Arguably, it might be more elegant (or a different kind of messy) to just close your saved plots, modify your rcParams, and redraw the versions you want to show.


问:
Java8: HashMap<X, Y> to HashMap<X, Z> using Stream / Map-Reduce / Collector
I know how to "transform" a simple Java List from Y -> Z, i.e.:

```<code>List<String> x;
List<Integer> y = x.stream()
        .map(s -> Integer.parseInt(s))
        .collect(Collectors.toList());
</code>```

Now I'd like to do basically the same with a Map, i.e.:

```<code>INPUT:
{
  "key1" -> "41",    // "41" and "42"
  "key2" -> "42"      // are Strings
}

OUTPUT:
{
  "key1" -> 41,      // 41 and 42
  "key2" -> 42       // are Integers
}
</code>```

The solution should not be limited to String -> Integer. Just like in the List example above, I'd like to call any method (or constructor).

答:
Guava's function Maps.transformValues is what you are looking for, and it works nicely with lambda expressions:

```<code>Maps.transformValues(originalMap, val -> ...)
</code>```

ContentLicense: CC BY-SA 4.0

Body:
I know how to "transform" a simple Java List from Y -> Z, i.e.:

List<String> x;
List<Integer> y = x.stream()
        .map(s -> Integer.parseInt(s))
        .collect(Collectors.toList());

Now I'd like to do basically the same with a Map, i.e.:

INPUT:
{
  "key1" -> "41",    // "41" and "42"
  "key2" -> "42"      // are Strings
}

OUTPUT:
{
  "key1" -> 41,      // 41 and 42
  "key2" -> 42       // are Integers
}

The solution should not be limited to String -> Integer. Just like in the List example above, I'd like to call any method (or constructor).

Body:
Guava's function Maps.transformValues is what you are looking for, and it works nicely with lambda expressions:

Maps.transformValues(originalMap, val -> ...)


问:
When to use Set in JavaScript
I'm just a beginner in javaScript and have a background in python. I was trying this exercise to check if every character of the string2 is contained within the string1.
For example, if string1 is "hello", I'll return true if string2 is "leh" and false if string2 is "low".

What I came up was this:

```<code>function mutation(arr) {
  var set = new Set(string1.split(''));
  for (var i = 0; i < string2.length; i++)
    if (!set.has(string2[i]))
      return false;
  return true;
}
</code>```

I could also go about converting string2 into a Set and then do take the difference i.e. the operation of set(string2) - set(string1) which will fetch me the set of characters that are in String2 but not in String1 but I read creating a set is costly so I didn't go ahead.

I checked other solutions and everyone was using string1.indexOf(letter) method for checking if every letter in string2 is there in string1 or not.

I want to know when should one use a Set difference. Why everyone is using array.indexOf() method which costs O(n) instead of set.has() method which is O(1). Are there any pitfalls if I'm using set.has(). (say browser compatibility)?

Any recommendation is helpful.

答:
In programming, multiple ways lead to the same solution. Using a set with .has() is just as valid as using indexOf(), it all depends on the surrounding code, and the re-use of the objects (eg: sets) you do.

Usually using indexOf() is faster on the CPU and does not care of the special characters (while a traditional simple split implies that words are separated by always the same character).

Building a set cost a little bit more. But it is still pretty fast so it should not be that much of a consideration.

In the end, do what your instinct tells you to do so that your code stays consistent.

Title: When to use Set in JavaScript

Body:
I'm just a beginner in javaScript and have a background in python. I was trying this exercise to check if every character of the string2 is contained within the string1.
For example, if string1 is "hello", I'll return true if string2 is "leh" and false if string2 is "low".

What I came up was this:

function mutation(arr) {
  var set = new Set(string1.split(''));
  for (var i = 0; i < string2.length; i++)
    if (!set.has(string2[i]))
      return false;
  return true;
}

I could also go about converting string2 into a Set and then do take the difference i.e. the operation of set(string2) - set(string1) which will fetch me the set of characters that are in String2 but not in String1 but I read creating a set is costly so I didn't go ahead.

I checked other solutions and everyone was using string1.indexOf(letter) method for checking if every letter in string2 is there in string1 or not.

I want to know when should one use a Set difference. Why everyone is using array.indexOf() method which costs O(n) instead of set.has() method which is O(1). Are there any pitfalls if I'm using set.has(). (say browser compatibility)?

Any recommendation is helpful.


问:
How to hide the first and last object from the array in Excel export in kendo?
I am using kendo grid in my ember app. i have to hide the first and last column in export to Excel and I have to modify the column width in the Excel sheet.

How to hide the object from the array?

Here is my code:

```<code>excelExport : function(e) {
     e.preventDefault();
     var str = e.sender.columns;
     var sheet = e.workbook.sheets[0];
     var tindex = str.map(function(d) { return d['title']; }).indexOf('Action');
     var dindex = str.map(function(d) { return d['field']; }).indexOf('documentKey');

     sheet.columns.removeObject(dindex);sheet.columns.removeObject(tindex);

     var excelSheetColumns = sheet.columns.filter(function(d){
     console.log(">>>>>>"+d);
     //d.removeObject(tindex);
     //d.removeObject(dindex);

     d.width = 50;
     return d;
});

var workbook = new kendo.ooxml.Workbook({
    sheets:e.workbook.sheets
});

kendo.saveAs({
    dataURI: workbook.toDataURL(),
    fileName:"export.xlsx"
});
</code>```

答:
Try this:</p>

```<code>var excelSheetColumns=sheet.columns.slice(2);
excelSheetColumns.forEach(function(col){col.width=50;});
var workbook = new kendo.ooxml.Workbook({
  sheets:excelSheetColumns
});
</code>```

Body:
I am using kendo grid in my ember app. i have to hide the first and last column in export to Excel and I have to modify the column width in the Excel sheet.

How to hide the object from the array?

Here is my code:

excelExport : function(e) {
     e.preventDefault();
     var str = e.sender.columns;
     var sheet = e.workbook.sheets[0];
     var tindex = str.map(function(d) { return d['title']; }).indexOf('Action');
     var dindex = str.map(function(d) { return d['field']; }).indexOf('documentKey');

     sheet.columns.removeObject(dindex);sheet.columns.removeObject(tindex);

     var excelSheetColumns = sheet.columns.filter(function(d){
     console.log(">>>>>>"+d);
     //d.removeObject(tindex);
     //d.removeObject(dindex);

     d.width = 50;
     return d;
});

var workbook = new kendo.ooxml.Workbook({
    sheets:e.workbook.sheets
});

kendo.saveAs({
    dataURI: workbook.toDataURL(),
    fileName:"export.xlsx"
});

Body:
Try this:

var excelSheetColumns=sheet.columns.slice(2);
excelSheetColumns.forEach(function(col){col.width=50;});
var workbook = new kendo.ooxml.Workbook({
  sheets:excelSheetColumns
});


问:
What's the best way to convert a date in the form yyyy-mm-dd to a date using English month names?
What I mean is, let's say I have the date '2018-05-20'. I want it in the form of 'May 20th, 2018'. What is the best way to do this without brute-forcing (below, for example) so that you don't have to create a bunch of dictionary entries and then deal with mapping all the numbers for months and dates to their English representation? </p>

```<code>monthDict = {}
monthDict['01'] = 'January'
monthDict['02'] = 'February'
monthDict['03'] = 'March'
monthDict['04'] = 'April'
monthDict['05'] = 'May'
monthDict['06'] = 'June'
monthDict['07'] = 'July'
monthDict['08'] = 'August'
monthDict['09'] = 'September'
monthDict['10'] = 'October'
monthDict['11'] = 'November'
monthDict['12'] = 'December'
</code>```

答:
You have to parse it in the existing format, and output the new.  There is an output %B that represents the full month.  Example:

```<code>import datetime
s = '2018-05-20'
dt = datetime.datetime.strptime(s, '%Y-%m-%d')
print dt.strftime('%B %d, %Y')
</code>```

Example:

```<code>$ python test.py
May 20, 2018
</code>```

Body:
What I mean is, let's say I have the date '2018-05-20'. I want it in the form of 'May 20th, 2018'. What is the best way to do this without brute-forcing (below, for example) so that you don't have to create a bunch of dictionary entries and then deal with mapping all the numbers for months and dates to their English representation?

monthDict = {}
monthDict['01'] = 'January'
monthDict['02'] = 'February'
monthDict['03'] = 'March'
monthDict['04'] = 'April'
monthDict['05'] = 'May'
monthDict['06'] = 'June'
monthDict['07'] = 'July'
monthDict['08'] = 'August'
monthDict['09'] = 'September'
monthDict['10'] = 'October'
monthDict['11'] = 'November'
monthDict['12'] = 'December'

Body:
You have to parse it in the existing format, and output the new.  There is an output %B that represents the full month.  Example:

import datetime
s = '2018-05-20'
dt = datetime.datetime.strptime(s, '%Y-%m-%d')
print dt.strftime('%B %d, %Y')

Example:

$ python test.py
May 20, 2018


问:
New retrieval argument is not coming into PFC linkage arguments
I have added new retrieve arguments for my data window. When i am trying to launch the data window the pfc linkage arguments are not taking the new argument which i have added.

I get the following error

Retrieve argument 2 does not match expected type

clearly indicating that it's trying to retrieve data window without the 2nd argument.

```<code>ls_dwobj = au_dc.DataObject
au_dc.SetTransObject(SQLCA)

ll_row_count = au_dc.Retrieve(aa_arg_array[1],aa_arg_array[2],aa_arg_array[3],aa_arg_array[4],aa_arg_array[5], aa_arg_array[6], aa_arg_array[7],aa_arg_array[8], aa_arg_array[9],aa_arg_array[10],aa_arg_array[11],aa_arg_array[12],
aa_arg_array[13], aa_arg_array[14], aa_arg_array[15],aa_arg_array[16],
aa_arg_array[17],aa_arg_array[18],aa_arg_array[19],aa_arg_array[20])
</code>```

答:
I get the following error

```<code>Retrieve argument 2 does not match expected type
</code>```

clearly indicating that it's trying to retrieve data window without the 2nd argument.

No. It is clearly indicating that the argument #2 of the retrieve() call (i.e. aa_arg_array[2]) is not of the type declared for the retrieval argument #2.
For example you have declared the argument to be an integer and you pass a string.

Body:
I have added new retrieve arguments for my data window. When i am trying to launch the data window the pfc linkage arguments are not taking the new argument which i have added.

I get the following error

Retrieve argument 2 does not match expected type

clearly indicating that it's trying to retrieve data window without the 2nd argument.

ls_dwobj = au_dc.DataObject
au_dc.SetTransObject(SQLCA)

ll_row_count = au_dc.Retrieve(aa_arg_array[1],aa_arg_array[2],aa_arg_array[3],aa_arg_array[4],aa_arg_array[5], aa_arg_array[6], aa_arg_array[7],aa_arg_array[8], aa_arg_array[9],aa_arg_array[10],aa_arg_array[11],aa_arg_array[12],
aa_arg_array[13], aa_arg_array[14], aa_arg_array[15],aa_arg_array[16],
aa_arg_array[17],aa_arg_array[18],aa_arg_array[19],aa_arg_array[20])

Body:
I get the following error

Retrieve argument 2 does not match expected type

clearly indicating that it's trying to retrieve data window without the 2nd argument.

No. It is clearly indicating that the argument #2 of the retrieve() call (i.e. aa_arg_array[2]) is not of the type declared for the retrieval argument #2.
For example you have declared the argument to be an integer and you pass a string.


问:
Sort file by key with awk or perl like a join without presorting
I want to join two tab separated files, but they are in a different order. I know that it is doable with awk, but I don't know how. Here is the equivalent toy python code (python is too memory inefficient for this task without crazy workarounds):

```<code>import pandas as pd
from random import shuffle

a = ['bar','qux','baz','foo','spam']
df = pd.DataFrame({'nam':a,'asc':[1,2,3,4,5],'desc':[5,4,3,2,1]})

shuffle(a)
print(a)

dex = pd.DataFrame({'dex' : a})
df_b = pd.DataFrame({'VAL1' :[0,1,2,3,4,5,6]})

pd.merge(dex, df,left_on='dex',right_on='nam')[['asc','desc','nam']]
</code>```

I have two files:
For file one, column 2 holds the identifier for each row, there 5 columns I don't need, and then there are about 3 million columns of data.

For file two, There are 12 columns, with the second column containing the same identifiers in a different order, along with additional ids.

I want to sort file one to have the same identifiers and order as file two, with the other columns appropriately rearranged.

File one is potentially multiple gigabytes.

Is this easier with awk and/or other GNU tools, or should I use perl?

答:
If the size of file1 is in the order of GB, and you have 3 million columns of data, you have a tiny number of lines (no more than 200). While you can't load all of the lines themselves into memory, you could easily load all of their locations.

```<code>use feature qw( say );

use Fcntl qw( SEEK_SET );

open(my $fh1, '<', $qfn1) or die("Can't open \"$qfn1\": $!\n");
open(my $fh2, '<', $qfn2) or die("Can't open \"$qfn2\": $!\n");

my %offsets;
while (1) {
   my $offset = tell($fh1);
   my $row1 = <$fh1>;
   last if !defined($row1);

   chomp($row1);
   my @fields1 = split(/\t/, $row1);
   my $key = $fields1[1];
   $offsets{$key} = $offset;
}

while (my $row2 = <$fh2>) {
   chomp($row2);
   my @fields2 = split(/\t/, $row2);
   my $key = $fields2[1];
   my $offset = $offsets{$key};
   if (!defined($offset)) {
      warn("Key $key not found.\n");
      next;
   }

   seek($fh1, $offset, SEEK_SET);
   my $row1 = <$fh1>;
   chomp($row1);
   my @fields1 = split(/\t/, $row1);

   say join "\t", @fields2, @fields1[6..$#fields1];
}
</code>```

This approach can be taken in Python as well.

Note: There exists a much simpler solution if the order is more flexible (i.e. if you're ok with the output being ordered as the records are ordered in file1). This assuming file2 easily fits in memory.

Body:
I want to join two tab separated files, but they are in a different order. I know that it is doable with awk, but I don't know how. Here is the equivalent toy python code (python is too memory inefficient for this task without crazy workarounds):

import pandas as pd
from random import shuffle

a = ['bar','qux','baz','foo','spam']
df = pd.DataFrame({'nam':a,'asc':[1,2,3,4,5],'desc':[5,4,3,2,1]})

shuffle(a)
print(a)

dex = pd.DataFrame({'dex' : a})
df_b = pd.DataFrame({'VAL1' :[0,1,2,3,4,5,6]})

pd.merge(dex, df,left_on='dex',right_on='nam')[['asc','desc','nam']]

I have two files:
For file one, column 2 holds the identifier for each row, there 5 columns I don't need, and then there are about 3 million columns of data.

For file two, There are 12 columns, with the second column containing the same identifiers in a different order, along with additional ids.

I want to sort file one to have the same identifiers and order as file two, with the other columns appropriately rearranged.

File one is potentially multiple gigabytes.

Is this easier with awk and/or other GNU tools, or should I use perl?

Body:
If the size of file1 is in the order of GB, and you have 3 million columns of data, you have a tiny number of lines (no more than 200). While you can't load all of the lines themselves into memory, you could easily load all of their locations.

use feature qw( say );

use Fcntl qw( SEEK_SET );

open(my $fh1, '<', $qfn1) or die("Can't open \"$qfn1\": $!\n");
open(my $fh2, '<', $qfn2) or die("Can't open \"$qfn2\": $!\n");

my %offsets;
while (1) {
   my $offset = tell($fh1);
   my $row1 = <$fh1>;
   last if !defined($row1);

   chomp($row1);
   my @fields1 = split(/\t/, $row1);
   my $key = $fields1[1];
   $offsets{$key} = $offset;
}

while (my $row2 = <$fh2>) {
   chomp($row2);
   my @fields2 = split(/\t/, $row2);
   my $key = $fields2[1];
   my $offset = $offsets{$key};
   if (!defined($offset)) {
      warn("Key $key not found.\n");
      next;
   }

   seek($fh1, $offset, SEEK_SET);
   my $row1 = <$fh1>;
   chomp($row1);
   my @fields1 = split(/\t/, $row1);

   say join "\t", @fields2, @fields1[6..$#fields1];
}

This approach can be taken in Python as well.

Note: There exists a much simpler solution if the order is more flexible (i.e. if you're ok with the output being ordered as the records are ordered in file1). This assuming file2 easily fits in memory.


问:
Java Spring: How to efficiently read and save large amount of data from a CSV file?
I am developing a web application in Java Spring where I want the user to be able to upload a CSV file from the front-end and then see the real-time progress of the importing process and after importing he should be able to search individual entries from the imported data.

The importing process would consist of actually uploading the file (sending it via REST API POST request) and then reading it and saving its contents to a database so the user would be able to search from this data.

What would be the fastest way to save the data to the database? Just looping over the lines and creating a new class object and saving it via JPARepository for each line takes too much time. It took around 90s for 10000 lines. I need to make it a lot faster. I need to add 200k rows in a reasonable amount of time.

Side Notes:

I saw Asynchronous approach, with Reactor. This should be faster as it uses multiple threads and the order of saving the rows basically isn't important (although the data has ID-s in the CSV).

Then I also saw Spring Batch jobs, but all of the examples use SQL. I am using repositories so I'm not sure if I can use it or whether it's the best approach.

答:
This GitHub repo compares 5 different methods of batch inserting data. Acc. to him, using JdbcTemplate is the fastest (he claims 500000 records in 1.79 [+- 0.50] seconds). If you use JdbcTemplate with Spring Data, you'll need to create a custom repository; see this section in the docs for detailed instructions about that.

Spring Data CrudRepository has a save method that takes an Iterable, so you can use that too, although you'll have to time it to see how it performs against the JdbcTemplate. Using Spring Data, the steps are as follows (taken from here with some edit)

Add: rewriteBatchedStatements=true to the end of the connection string.

Make sure you use a generator that supports batching in your entity. E.g.

```<code>@Id
@GeneratedValue(generator = "generator")
@GenericGenerator(name = "generator", strategy = "increment")
</code>```

Use the: save(Iterable<S> entities) method of the CrudRepository to save the data.

Use the: hibernate.jdbc.batch_size configuration.

The code for the solution #2 is here.

As for using multiple threads, remember that writing to the same table in the database from multiple threads may produce table level contentions and produce worse results. You will have to try and time it. How to write multithreaded code using project Reactor is a completely separate topic that's out of the scope here.

HTH.

Body:
This GitHub repo compares 5 different methods of batch inserting data. Acc. to him, using JdbcTemplate is the fastest (he claims 500000 records in 1.79 [+- 0.50] seconds). If you use JdbcTemplate with Spring Data, you'll need to create a custom repository; see this section in the docs for detailed instructions about that.

Spring Data CrudRepository has a save method that takes an Iterable, so you can use that too, although you'll have to time it to see how it performs against the JdbcTemplate. Using Spring Data, the steps are as follows (taken from here with some edit)

Add: rewriteBatchedStatements=true to the end of the connection string.

Make sure you use a generator that supports batching in your entity. E.g.

@Id
@GeneratedValue(generator = "generator")
@GenericGenerator(name = "generator", strategy = "increment")

Use the: save(Iterable<S> entities) method of the CrudRepository to save the data.

Use the: hibernate.jdbc.batch_size configuration.

The code for the solution #2 is here.

As for using multiple threads, remember that writing to the same table in the database from multiple threads may produce table level contentions and produce worse results. You will have to try and time it. How to write multithreaded code using project Reactor is a completely separate topic that's out of the scope here.

HTH.


问:
Problems with Applescript and Automator after macOS update
I had an Automator app that was working perfectly.
I use it to open Safari and fill a form with data taken from a numbers document.

To select the input fields I use something like:

```<code>do JavaScript "document.getElementById('Name').value = '" & name & "';
</code>```

But as soon as the app gets to that line, it encounters an error and stops. I tried several other commands using do JavaScript but nothing seems to work.

I'm not sure if this issue is related to the update. Could the do JavaScript command change?

答:
I found the problem. In Safari, I needed to enable scripts from Apple Events. This option was disabled with the update.

The error didn't display any kind of information, it only said the the application found an error and needed to stop. So it was hard to debug.

Body:
I had an Automator app that was working perfectly.
I use it to open Safari and fill a form with data taken from a numbers document.

To select the input fields I use something like:

do JavaScript "document.getElementById('Name').value = '" & name & "';

But as soon as the app gets to that line, it encounters an error and stops. I tried several other commands using do JavaScript but nothing seems to work.

I'm not sure if this issue is related to the update. Could the do JavaScript command change?


问:
SendGrid (PHP library) with attachments
I have a simple SendGrid API setup but I am wondering how I can include attachments in the email that gets sent. I am passing the attachment URL into the $_POST variable so can return the full path but unsure where to include it in my setup.</p>

```<code>//
$i = 1; foreach ($cart as $download) {

    $file . $i = $download['download'];
    $file_encoded . $i = base64_encode(file_get_contents($file));
    $attachment . $i = new SendGrid\Attachment();
    $attachment . $i->setContent($file_encoded);
    $attachment . $i->setType("application/zip");
    $attachment . $i->setDisposition("attachment");
    $attachment . $i->setFilename("test.zip");
    $mail->addAttachment($attachment . $i);

    i++;

}

// Set email confirmation settings
$email_subject = 'New order from ' . $user_details['first-name-billing'] . ' ' . $user_details['last-name-billing'] . ' via www.***.com';
$email_admin = '******@***.com'; // Client
$email_customer = $user_details['email'];

ob_start();
    require_once './email-confirmation.php';
    $email_body = ob_get_contents();
ob_end_clean();

// Send to confirmation to admin
if ($email_admin) {
    send_email($email_admin, $email_admin, $email_subject, $email_body);
}

// Send confirmation to customer
if ($email_customer) {
    send_email($email_admin, $email_customer, $email_subject, $email_body);
}

// SendGrid init
function send_email($from_email, $to_email, $subject, $body) {
    global $sgAPIKey;
    $from = new SendGrid\Email(null, $from_email);
    $to = new SendGrid\Email(null, $to_email);
    $content = new SendGrid\Content("text/html", $body);
    $mail = new SendGrid\Mail($from, $subject, $to, $content);
    $sg = new \SendGrid($sgAPIKey);
    $response = $sg->client->mail()->send()->post($mail);
}
</code>```

答:
Use the SendGrid\Attachment class. A complete example can be found in the documentation:

```<code>$filename = basename($url);
$file_encoded = base64_encode(file_get_contents($url));
$attachment = new SendGrid\Attachment();
$attachment->setType("application/text");
$attachment->setContent($file_encoded);
$attachment->setDisposition("attachment");
$attachment->setFilename($filename);
$mail->addAttachment($attachment);
</code>```

Title: SendGrid (PHP library) with attachments

Body:
I have a simple SendGrid API setup but I am wondering how I can include attachments in the email that gets sent. I am passing the attachment URL into the $_POST variable so can return the full path but unsure where to include it in my setup.

//
$i = 1; foreach ($cart as $download) {

    $file . $i = $download['download'];
    $file_encoded . $i = base64_encode(file_get_contents($file));
    $attachment . $i = new SendGrid\Attachment();
    $attachment . $i->setContent($file_encoded);
    $attachment . $i->setType("application/zip");
    $attachment . $i->setDisposition("attachment");
    $attachment . $i->setFilename("test.zip");
    $mail->addAttachment($attachment . $i);

    i++;

}

// Set email confirmation settings
$email_subject = 'New order from ' . $user_details['first-name-billing'] . ' ' . $user_details['last-name-billing'] . ' via www.***.com';
$email_admin = '******@***.com'; // Client
$email_customer = $user_details['email'];

ob_start();
    require_once './email-confirmation.php';
    $email_body = ob_get_contents();
ob_end_clean();

// Send to confirmation to admin
if ($email_admin) {
    send_email($email_admin, $email_admin, $email_subject, $email_body);
}

// Send confirmation to customer
if ($email_customer) {
    send_email($email_admin, $email_customer, $email_subject, $email_body);
}

// SendGrid init
function send_email($from_email, $to_email, $subject, $body) {
    global $sgAPIKey;
    $from = new SendGrid\Email(null, $from_email);
    $to = new SendGrid\Email(null, $to_email);
    $content = new SendGrid\Content("text/html", $body);
    $mail = new SendGrid\Mail($from, $subject, $to, $content);
    $sg = new \SendGrid($sgAPIKey);
    $response = $sg->client->mail()->send()->post($mail);
}

Body:
Use the SendGrid\Attachment class. A complete example can be found in the documentation:

$filename = basename($url);
$file_encoded = base64_encode(file_get_contents($url));
$attachment = new SendGrid\Attachment();
$attachment->setType("application/text");
$attachment->setContent($file_encoded);
$attachment->setDisposition("attachment");
$attachment->setFilename($filename);
$mail->addAttachment($attachment);