问:
tf operation control_inputs vs inputs
In tf.Operation, what's the difference between control_inputs and inputs? tf.Operation describes them separately, but in the normal course of events I would expect 'every data input is ready' to be exactly the condition for the current operation being able to run. What am I missing?

答:
In tensorflow, one can add extra dependencies via tf.control_dependencies function, which are obeyed by the tensorflow in addition to the ordinary dependencies (which correspond to the inputs). This is exactly what tf.Operation.control_inputs holds.

Here's an example:

a = tf.Variable(name='a', initial_value=1.0, trainable=False)
b = tf.Variable(name='b', initial_value=0.0, trainable=False)
c = tf.add(a, b, name='sum')

op = tf.get_default_graph().get_operation_by_name('sum')
print(list(op.inputs))    # [<tf.Tensor 'a/read:0' shape=() dtype=float32>, <tf.Tensor 'b/read:0' shape=() dtype=float32>]
print(op.control_inputs)  # []
```

<p>It's a standard situation, and unsurprisingly <code>c</code> has only 2 ordinary inputs and no <code>control_inputs</code>. Upon evaluation, <code>c</code> value will be <code>1.0</code>.</p>

<p>But if you add an extra dependency, it'll change the graph and the way the computation goes:</p>

<pre class="lang-py prettyprint-override"><code>a = tf.Variable(name='a', initial_value=1.0, trainable=False)
b = tf.Variable(name='b', initial_value=0.0, trainable=False)

dependent_op = tf.assign(b, a * 3, name='dependent_op')
with tf.control_dependencies([dependent_op]):
  c = tf.add(a, b, name='sum')

op = tf.get_default_graph().get_operation_by_name('sum')
print(list(op.inputs))    # [<tf.Tensor 'a/read:0' shape=() dtype=float32>, <tf.Tensor 'b/read:0' shape=() dtype=float32>]
print(op.control_inputs)  # [<tf.Operation 'dependent_op' type=Assign>]
</code>```

Note that the list of inputs didn't change, but control_inputs now contains the dependent_op. Tensorflow guarantees that dependent_op will be evaluated before sum, as a result, c is going to get the value 4.0. If you define c without tf.control_dependencies block, c will be evaluated to 1.0, because dependent_op, even though it's in the graph, won't be executed.

Title: tf operation control_inputs vs inputs

Body:
In tensorflow, one can add extra dependencies via tf.control_dependencies function, which are obeyed by the tensorflow in addition to the ordinary dependencies (which correspond to the inputs). This is exactly what tf.Operation.control_inputs holds.

Here's an example:

a = tf.Variable(name='a', initial_value=1.0, trainable=False)
b = tf.Variable(name='b', initial_value=0.0, trainable=False)
c = tf.add(a, b, name='sum')

op = tf.get_default_graph().get_operation_by_name('sum')
print(list(op.inputs))    # [<tf.Tensor 'a/read:0' shape=() dtype=float32>, <tf.Tensor 'b/read:0' shape=() dtype=float32>]
print(op.control_inputs)  # []

It's a standard situation, and unsurprisingly c has only 2 ordinary inputs and no control_inputs. Upon evaluation, c value will be 1.0.

But if you add an extra dependency, it'll change the graph and the way the computation goes:

a = tf.Variable(name='a', initial_value=1.0, trainable=False)
b = tf.Variable(name='b', initial_value=0.0, trainable=False)

dependent_op = tf.assign(b, a * 3, name='dependent_op')
with tf.control_dependencies([dependent_op]):
  c = tf.add(a, b, name='sum')

op = tf.get_default_graph().get_operation_by_name('sum')
print(list(op.inputs))    # [<tf.Tensor 'a/read:0' shape=() dtype=float32>, <tf.Tensor 'b/read:0' shape=() dtype=float32>]
print(op.control_inputs)  # [<tf.Operation 'dependent_op' type=Assign>]

Note that the list of inputs didn't change, but control_inputs now contains the dependent_op. Tensorflow guarantees that dependent_op will be evaluated before sum, as a result, c is going to get the value 4.0. If you define c without tf.control_dependencies block, c will be evaluated to 1.0, because dependent_op, even though it's in the graph, won't be executed.


来源: stackexchange

问:
DataTables Loading and Rendering Delay
I am using DataTables with Bootstrap 4 and client side processing. Displaying around 2,000 records.

The loading time is acceptable, however I notice that when I reload the page (F5) I can see an un-formatted DataTable for a split second. It's almost as if DataTables is the last thing to load, and it's quite obvious.

If you look at their 'Zero configuration' example you can see what I mean. When reloading the page you can actually see all the table records for a split second (you have to be quick!).

There is a 'Bootstrap 4' example, also noticeable on that page.

I have a few questions;

Why does this happen?

Is there anything I can do to prevent it?

I have tried re-ordering my JS and CSS files (I only have a few) however it has made no difference. My concern is that as I add more records the loading time will increase and the un-formatted DataTable will be more obvious on each page load.

答:
In my opinion loading time cannot be acceptable for render 2000 rows(or more). Even if loading time is acceptable in PCs and laptops, there is problem for processing in mobiles. Did you test it already?

Datatable is a perfect plugin and has too many options. you can render just 10 rows in first view, and when the user want to see another pages, request from Datatable to draw and render them. So you need to use data property like this:

HTML:

```<code>    <table id="example" class="table table-striped table-bordered" cellspacing="0">
        <thead>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Age</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </thead>
        <tbody>

        </tbody>
    </table>
</code>```

JS:

```<code>var dataSet = new Array();
for(i=0;i<2000;i++){
    var item = new Array(
        "name "+i,
        "position "+i,
        "office "+i,
        "age "+i,
        "start date "+i,
        "salary "+i
    );
    dataSet.push(item);
}

tableMain = $("#example").DataTable({
    data: dataSet,
    columns: [
        {"orderable": true},
        {"orderable": true},
        {"orderable": true},
        {"orderable": true},
        {"orderable": true},
        {"orderable": true}
    ],
    pageLength: 10
});
</code>```

As you see just 10 rows renders in HTML code via this method.

ContentLicense: CC BY-SA 3.0

Title: DataTables Loading and Rendering Delay

Body:
In my opinion loading time cannot be acceptable for render 2000 rows(or more). Even if loading time is acceptable in PCs and laptops, there is problem for processing in mobiles. Did you test it already?

Datatable is a perfect plugin and has too many options. you can render just 10 rows in first view, and when the user want to see another pages, request from Datatable to draw and render them. So you need to use data property like this:

HTML:

    <table id="example" class="table table-striped table-bordered" cellspacing="0">
        <thead>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Age</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </thead>
        <tbody>

        </tbody>
    </table>

JS:

var dataSet = new Array();
for(i=0;i<2000;i++){
    var item = new Array(
        "name "+i,
        "position "+i,
        "office "+i,
        "age "+i,
        "start date "+i,
        "salary "+i
    );
    dataSet.push(item);
}

tableMain = $("#example").DataTable({
    data: dataSet,
    columns: [
        {"orderable": true},
        {"orderable": true},
        {"orderable": true},
        {"orderable": true},
        {"orderable": true},
        {"orderable": true}
    ],
    pageLength: 10
});

As you see just 10 rows renders in HTML code via this method.


问:
Prettify sql output sublime
I would like to find a package which structures and make SQL output more readable.

For example there is a package Pretty JSON for json files.
But I would like to have something that make the spaces between columns the same for rows.

```<code>x   true    SOME_LONG_OUTPUT_WHICH_DESTROYS_THE_ORDER   NULL
y   true     Short_output   NULL
</code>```

What I would like is something like below:

```<code>x   true    SOME_LONG_OUTPUT_WHICH_DESTROYS_THE_ORDER   NULL
y   true    short_output                                NULL
</code>```

Is this possible?

答:
Install the AlignTab plugin.

To prettify your example, I pasted your text into Sublime Text 3, pressed Ctrl-Shift-P, typed align to select AlignTab, and typed \s+ into the regex to align by spaces and it did what you wanted.

Title: Prettify sql output sublime

Body:
I would like to find a package which structures and make SQL output more readable.

For example there is a package Pretty JSON for json files.
But I would like to have something that make the spaces between columns the same for rows.

x   true    SOME_LONG_OUTPUT_WHICH_DESTROYS_THE_ORDER   NULL
y   true     Short_output   NULL

What I would like is something like below:

x   true    SOME_LONG_OUTPUT_WHICH_DESTROYS_THE_ORDER   NULL
y   true    short_output                                NULL

Is this possible?


问:
How to reuse one html head section?
I am using zend and not using AJAX in my site. Therefore I have to include all my CSS and JS files in head section of every phtml page.

```<code><html>
<head>
  <link rel="stylesheet" type="text/css" href="mycssfile.css" />
  <script type="text/javascript" src="myjsfile1.js"></script>
  <script type="text/javascript" src="myjsfile2.js"></script>
  .........
</head>
<body>
  .........
</body>

</html>
</code>```

Whenever I add any new CSS/JS file, I have to include it everywhere. Is there a good way that I can include all my CSS and JS file in a file/place and use that reference everywhere. May be there is a good way in zend.

答:
I usually put my static suff in the bootstrap of the application in the _initViewHelpers method :

```<code>$this->bootstrap(array('frontcontroller', 'view'));
$frontController = $this->getResource('frontcontroller');
$view = $this->getResource('view');

$view->headLink()->appendStylesheet($view->assetUrl('/css/reset.css'))
                 ->appendStylesheet($view->assetUrl('/css/clearfix.css'))
                 ->appendStylesheet($view->assetUrl('/css/screen.css'));

$view->headScript()->appendFile($view->assetUrl('/js/jquery.1.4.3.min.js', 'text/javascript'))
                   ->appendFile($view->assetUrl('/js/jquery.corner.js', 'text/javascript'));
</code>```

And then from your layout file you call them through

```<code>echo $this->headLink()->setIndent();
echo $this->headStyle()->setIndent();
</code>```

This site might also help you with bootstrap and application optimization
http://joegornick.com/2009/11/18/zend-framework-best-practices-part-1-getting-started/

ContentLicense: CC BY-SA 2.5

Title: How to reuse one html head section?

Body:
I am using zend and not using AJAX in my site. Therefore I have to include all my CSS and JS files in head section of every phtml page.

  <link rel="stylesheet" type="text/css" href="mycssfile.css" />

  .........

  .........

Whenever I add any new CSS/JS file, I have to include it everywhere. Is there a good way that I can include all my CSS and JS file in a file/place and use that reference everywhere. May be there is a good way in zend.

Body:
I usually put my static suff in the bootstrap of the application in the _initViewHelpers method :

$this->bootstrap(array('frontcontroller', 'view'));
$frontController = $this->getResource('frontcontroller');
$view = $this->getResource('view');

$view->headLink()->appendStylesheet($view->assetUrl('/css/reset.css'))
                 ->appendStylesheet($view->assetUrl('/css/clearfix.css'))
                 ->appendStylesheet($view->assetUrl('/css/screen.css'));

$view->headScript()->appendFile($view->assetUrl('/js/jquery.1.4.3.min.js', 'text/javascript'))
                   ->appendFile($view->assetUrl('/js/jquery.corner.js', 'text/javascript'));

And then from your layout file you call them through

echo $this->headLink()->setIndent();
echo $this->headStyle()->setIndent();

This site might also help you with bootstrap and application optimization
http://joegornick.com/2009/11/18/zend-framework-best-practices-part-1-getting-started/


问:
Ionic 3 - Polyfill for ES6 in IE11 Not Working
I am aware that Ionic have stated that they will not be supporting IE11 - however I am still currently obliged by work to try and come up with a workaround of some sort if possible!

The issue I am having is the main.js file built by Ionic in the dev environment  is throwing an error in IE11 due to it using template literals.

I have created a polyfills.ts file which includes the following

polyfills.ts

```<code>import 'core-js/es6/string';
import 'core-js/fn/string/raw';
import 'core-js/es6/symbol';
import 'core-js/es6/object';
import 'core-js/es6/function';
import 'core-js/es6/parse-int';
import 'core-js/es6/parse-float';
import 'core-js/es6/number';
import 'core-js/es6/math';
import 'core-js/es6/date';
import 'core-js/es6/array';
import 'core-js/es6/regexp';
import 'core-js/es6/map';
import 'core-js/es6/weak-map';
import 'core-js/es6/set';
import 'core-js/es6/reflect';
import 'core-js/es7/reflect';
import 'classlist.js';
import 'web-animations-js';
import 'hammerjs';
</code>```

and in my main.ts file

```<code>import './polyfills'
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import {enableProdMode} from '@angular/core';
import { AppModule } from './app.module';

enableProdMode();

platformBrowserDynamic().bootstrapModule(AppModule);
</code>```

However it doesn't seem to be resolving the issue. Am I perhaps missing a module from core-js that would solve this issue?

Any advice as always is much appreciated.

Thanks!

答:
The issue was the version of ionic app scripts.

I had

@ionic/app-scripts: 3.1.0

updating to 3.1.2 resolved the issue!

Body:
I am aware that Ionic have stated that they will not be supporting IE11 - however I am still currently obliged by work to try and come up with a workaround of some sort if possible!

The issue I am having is the main.js file built by Ionic in the dev environment  is throwing an error in IE11 due to it using template literals.

I have created a polyfills.ts file which includes the following

polyfills.ts

import 'core-js/es6/string';
import 'core-js/fn/string/raw';
import 'core-js/es6/symbol';
import 'core-js/es6/object';
import 'core-js/es6/function';
import 'core-js/es6/parse-int';
import 'core-js/es6/parse-float';
import 'core-js/es6/number';
import 'core-js/es6/math';
import 'core-js/es6/date';
import 'core-js/es6/array';
import 'core-js/es6/regexp';
import 'core-js/es6/map';
import 'core-js/es6/weak-map';
import 'core-js/es6/set';
import 'core-js/es6/reflect';
import 'core-js/es7/reflect';
import 'classlist.js';
import 'web-animations-js';
import 'hammerjs';

and in my main.ts file

import './polyfills'
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import {enableProdMode} from '@angular/core';
import { AppModule } from './app.module';

enableProdMode();

platformBrowserDynamic().bootstrapModule(AppModule);

However it doesn't seem to be resolving the issue. Am I perhaps missing a module from core-js that would solve this issue?

Any advice as always is much appreciated.

Thanks!


问:
Remove a character from java string using hex code
i would like to remove a character from java string using hex code:

i am trying following code but seems to not be correct as the character isn't replaced: ÿ

```<code>String str ="test  ÿ";
str.replaceAll("\\x{9F}","")
</code>```

is there any thing wrong with the syntax of the hex code? Thanks.

答:
Could you please try this:</p>

```<code>    public class AsciiHexCode {
    public static void main(String[] args) {
        String  str = "test  ÿ";
        String result = str.replaceAll("[^\\x00-\\x7F]", "");
        System.out.println("result : "+ result);
    }
}
</code>```

Body:
i would like to remove a character from java string using hex code:

i am trying following code but seems to not be correct as the character isn't replaced: ÿ

String str ="test  ÿ";
str.replaceAll("\\x{9F}","")

is there any thing wrong with the syntax of the hex code? Thanks.

Body:
Could you please try this:

    public class AsciiHexCode {
    public static void main(String[] args) {
        String  str = "test  ÿ";
        String result = str.replaceAll("[^\\x00-\\x7F]", "");
        System.out.println("result : "+ result);
    }
}


问:
How to multiply a column in an CSV sheet from a batch file in Windows
I need to do a simple multiplication in a CSV sheet from a batch file on a Windows 7 machine.

Assume this is my CSV sheet:

```<code>COL_A, COL_B, COL_C, COL_D
23,3,0.667,6
24,4,0,7
25,2,3.55,7
26,3,,8
</code>```

I need this output:

```<code>COL_A, COL_B, COL_C, COL_D,COL_E
23,3,0.667,6,1.0
24,4,0,7,0
25,2,3.55,7,5.3
26,3,,8,0
</code>```

Formula for Col_E:

```<code>COL_E = COL_C * 1.5
</code>```

答:
```<code>@echo off
setlocal enabledelayedexpansion
FOR /F "tokens=1-18* delims=," %%A IN (mycsv.csv) DO (
set sum1="%%~C"
set /a cole=!sum1! * 2
echo %%~A,%%~B,%%~C,%%~D,!cole!
) >> output.csv
</code>```

EDIT, fixed the pipe to support all rows and edited for the output to work, there is going to be rounding off issue though as batch does not generally support fractions, so will need to relook this.

Body:
I need to do a simple multiplication in a CSV sheet from a batch file on a Windows 7 machine.

Assume this is my CSV sheet:

COL_A, COL_B, COL_C, COL_D
23,3,0.667,6
24,4,0,7
25,2,3.55,7
26,3,,8

I need this output:

COL_A, COL_B, COL_C, COL_D,COL_E
23,3,0.667,6,1.0
24,4,0,7,0
25,2,3.55,7,5.3
26,3,,8,0

Formula for Col_E:

COL_E = COL_C * 1.5

Body:
@echo off
setlocal enabledelayedexpansion
FOR /F "tokens=1-18* delims=," %%A IN (mycsv.csv) DO (
set sum1="%%~C"
set /a cole=!sum1! * 2
echo %%~A,%%~B,%%~C,%%~D,!cole!
) >> output.csv

EDIT, fixed the pipe to support all rows and edited for the output to work, there is going to be rounding off issue though as batch does not generally support fractions, so will need to relook this.


问:
django.db.utils.OperationalError: table "blog_post" already exists
I'm creating a blog using Django and anytime I run manage.py migrate, I get the error below:

```<code>(venv) (C:\Users\KOLAPO\Anaconda3) C:\Programming\Websites\shelteratyourcrossroads\shelteratyourcrossroads>manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, blog, contenttypes, sessions, sites, taggit
Running migrations:
  Applying blog.0001_initial...Traceback (most recent call last):
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\db\backends\utils.py", line 62, in execute
    return self.cursor.execute(sql)
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\db\backends\sqlite3\base.py", line 335, in execute
    return Database.Cursor.execute(self, query)
sqlite3.OperationalError: table "blog_post" already exists

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Programming\Websites\shelteratyourcrossroads\shelteratyourcrossroads\manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\core\management\__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\core\management\base.py", line 294, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\core\management\base.py", line 345, in execute
    output = self.handle(*args, **options)
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\core\management\commands\migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\db\migrations\executor.py", line 115, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\db\migrations\executor.py", line 145, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\db\migrations\migration.py", line 129, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\db\migrations\operations\models.py", line 96, in database_forwards
    schema_editor.create_model(model)
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\db\backends\base\schema.py", line 295, in create_model
    self.execute(sql, params or None)
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\db\backends\base\schema.py", line 112, in execute
    cursor.execute(sql, params)
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\db\backends\utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\db\utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\utils\six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\db\backends\utils.py", line 62, in execute
    return self.cursor.execute(sql)
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\db\backends\sqlite3\base.py", line 335, in execute
    return Database.Cursor.execute(self, query)
django.db.utils.OperationalError: table "blog_post" already exists
</code>```

Here's everything in my models.py file:

```<code>from django.db import models
from django.utils import timezone
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from django.conf import settings
from django.db.models.signals import post_save
from django.dispatch import receiver
from taggit.managers import TaggableManager
from ckeditor_uploader.fields import RichTextUploadingField

class PublishedManager(models.Manager):
    def get_queryset(self):
        return super(PublishedManager,
            self).get_queryset().filter(status='published')

class Post(models.Model):
    STATUS_CHOICES = (
        ('draft', 'Draft'),
        ('published', 'Published'),
    )
    title = models.CharField(max_length=250)
    subtitle = models.TextField()
    slug = models.SlugField(max_length=250, unique_for_date='publish')
    author = models.ForeignKey(User, related_name='blog_posts')
    image = models.ImageField(upload_to='posts/%Y/%m/%d', blank=True)
    # body = models.TextField()
    body = RichTextUploadingField()
    publish = models.DateTimeField(default=timezone.now)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    tags = TaggableManager()
    status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft')

    class Meta:
        ordering = ('-publish',)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('post_detail', args=[
            self.publish.year, self.publish.strftime('%m'),
            self.publish.strftime('%d'), self.slug])

    objects = models.Manager() # default manager
    published = PublishedManager() # custom manager

class Profile(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL)
    image = models.ImageField(upload_to='users/%Y/%m/%d', null=True, blank=True)

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()

    def __str__(self):
        return 'Profile for user {}'.format(self.user.username)

class Feedback(models.Model):
    name = models.CharField(max_length=200, help_text="name of sender")
    email = models.EmailField(max_length=200)
    subject = models.CharField(max_length=200)
    message = models.TextField()
    date = models.DateTimeField(auto_now_add=True)

    class Meta:
        verbose_name_plural = "Feedback"

    def __str__(self):
        return (self.name, "-", self.email)
</code>```

I'm stuck on that error. How do I fix it please?

答:
Maybe faking the initial migration of blog app will help you. Try the following.

./manage.py migrate --fake blog 0001_initial

Body:
I'm creating a blog using Django and anytime I run manage.py migrate, I get the error below:

(venv) (C:\Users\KOLAPO\Anaconda3) C:\Programming\Websites\shelteratyourcrossroads\shelteratyourcrossroads>manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, blog, contenttypes, sessions, sites, taggit
Running migrations:
  Applying blog.0001_initial...Traceback (most recent call last):
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\db\backends\utils.py", line 62, in execute
    return self.cursor.execute(sql)
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\db\backends\sqlite3\base.py", line 335, in execute
    return Database.Cursor.execute(self, query)
sqlite3.OperationalError: table "blog_post" already exists

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Programming\Websites\shelteratyourcrossroads\shelteratyourcrossroads\manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\core\management\__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\core\management\base.py", line 294, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\core\management\base.py", line 345, in execute
    output = self.handle(*args, **options)
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\core\management\commands\migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\db\migrations\executor.py", line 115, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\db\migrations\executor.py", line 145, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\db\migrations\migration.py", line 129, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\db\migrations\operations\models.py", line 96, in database_forwards
    schema_editor.create_model(model)
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\db\backends\base\schema.py", line 295, in create_model
    self.execute(sql, params or None)
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\db\backends\base\schema.py", line 112, in execute
    cursor.execute(sql, params)
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\db\backends\utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\db\utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\utils\six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\db\backends\utils.py", line 62, in execute
    return self.cursor.execute(sql)
  File "C:\Programming\Websites\shelteratyourcrossroads\venv\lib\site-packages\django\db\backends\sqlite3\base.py", line 335, in execute
    return Database.Cursor.execute(self, query)
django.db.utils.OperationalError: table "blog_post" already exists

Here's everything in my models.py file:

from django.db import models
from django.utils import timezone
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from django.conf import settings
from django.db.models.signals import post_save
from django.dispatch import receiver
from taggit.managers import TaggableManager
from ckeditor_uploader.fields import RichTextUploadingField

class PublishedManager(models.Manager):
    def get_queryset(self):
        return super(PublishedManager,
            self).get_queryset().filter(status='published')

class Post(models.Model):
    STATUS_CHOICES = (
        ('draft', 'Draft'),
        ('published', 'Published'),
    )
    title = models.CharField(max_length=250)
    subtitle = models.TextField()
    slug = models.SlugField(max_length=250, unique_for_date='publish')
    author = models.ForeignKey(User, related_name='blog_posts')
    image = models.ImageField(upload_to='posts/%Y/%m/%d', blank=True)
    # body = models.TextField()
    body = RichTextUploadingField()
    publish = models.DateTimeField(default=timezone.now)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    tags = TaggableManager()
    status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft')

    class Meta:
        ordering = ('-publish',)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('post_detail', args=[
            self.publish.year, self.publish.strftime('%m'),
            self.publish.strftime('%d'), self.slug])

    objects = models.Manager() # default manager
    published = PublishedManager() # custom manager

class Profile(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL)
    image = models.ImageField(upload_to='users/%Y/%m/%d', null=True, blank=True)

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()

    def __str__(self):
        return 'Profile for user {}'.format(self.user.username)

class Feedback(models.Model):
    name = models.CharField(max_length=200, help_text="name of sender")
    email = models.EmailField(max_length=200)
    subject = models.CharField(max_length=200)
    message = models.TextField()
    date = models.DateTimeField(auto_now_add=True)

    class Meta:
        verbose_name_plural = "Feedback"

    def __str__(self):
        return (self.name, "-", self.email)

I'm stuck on that error. How do I fix it please?


问:
Changing Ionic Cordova TabRoot Dynamically during runtime
I have created an Ionic Tabs Application.

Tabs.html:

```<code><ion-tabs>
  <ion-tab [root]="tab1Root" tabTitle="Home" tabIcon="md-home"></ion-tab>
  <ion-tab [root]="tab2Root" tabTitle="About" tabIcon="md-information-circle"></ion-tab>
  <ion-tab [root]="tab3Root" tabTitle="My Account" tabIcon="md-log-in"></ion-tab>
</ion-tabs>
</code>```

and Tabs.ts

```<code>@Component({
  templateUrl: 'tabs.html'
})
export class TabsPage {

  tab1Root = HomePage;
  tab2Root = AboutPage;
  tab3Root = LoginPage;

  constructor()
  {}

}
</code>```

What I want to do is once I'm on tab3Root (LoginPage) and I login, I want to change it to the LoggedinPage.
Maintaining the other tabs functioning and normal.

Is that possible? If yes how please?

Thank you

Added Info: check/edit this link it contains my code as example
stackblitz.com/edit/ionic-yff2sx

答:
According to prithivi Raj answer you will be able to navigate to that page but you can not highlight the tabs icon.

inOrder to do that you have you do some thing like this

```<code> this.navCtrl.parrent.select(0);
</code>```

Consider you are in tab3 and after you login you want to navigate to tab1/tab2 just mention the index in the above code parameter like this.navCtrl.parrent.select(0); / this.navCtrl.parrent.select(1);. it will lead you to that tab if you use `this.navCtrl.setRoot("this. loggedInPage"); it will not highlight the tabs which will lead the user confusion in which tab he is active now.

Body:
I have created an Ionic Tabs Application.

Tabs.html:

<ion-tabs>
  <ion-tab [root]="tab1Root" tabTitle="Home" tabIcon="md-home"></ion-tab>
  <ion-tab [root]="tab2Root" tabTitle="About" tabIcon="md-information-circle"></ion-tab>
  <ion-tab [root]="tab3Root" tabTitle="My Account" tabIcon="md-log-in"></ion-tab>
</ion-tabs>

and Tabs.ts

@Component({
  templateUrl: 'tabs.html'
})
export class TabsPage {

  tab1Root = HomePage;
  tab2Root = AboutPage;
  tab3Root = LoginPage;

  constructor()
  {}

}

What I want to do is once I'm on tab3Root (LoginPage) and I login, I want to change it to the LoggedinPage.
Maintaining the other tabs functioning and normal.

Is that possible? If yes how please?

Thank you

Added Info: check/edit this link it contains my code as example
stackblitz.com/edit/ionic-yff2sx

Body:
According to prithivi Raj answer you will be able to navigate to that page but you can not highlight the tabs icon.

inOrder to do that you have you do some thing like this

 this.navCtrl.parrent.select(0);

Consider you are in tab3 and after you login you want to navigate to tab1/tab2 just mention the index in the above code parameter like this.navCtrl.parrent.select(0); / this.navCtrl.parrent.select(1);. it will lead you to that tab if you use `this.navCtrl.setRoot("this. loggedInPage"); it will not highlight the tabs which will lead the user confusion in which tab he is active now.


问:
What is more efficient: List<T>.Add() or System.Array.Resize()?
I'm trying to determine when it's more efficient to List<T>.Add() versus using the Array.Resize() method.

The documentation for Array.Resize says it makes a copy of the entire array, and places it into a new object.  The old object would have to be discarded.  Where does this old object reside? On the stack or the heap?

I don't know how List.Add() works.

Does anyone know how the List.Add method compares to the static Array.Resize method?

I'm interested in memory usage (and cleanup), and what is better for 300 value types, versus 20,000 value types.

For what it's worth, I'm planning on running this code on one of the embedded flavors of .NET.  Potentially the .NET Gadgeteer

答:
The .NET Micro Framework does not support generics, so I will be using an array, copying and destroying it as needed.

I might compare that perfmance to the unrolled linked list mentioned in the powertools library here:
Any implementation of an Unrolled Linked List in C#?


问:
Existing li gets overridden in todolist instead of adding a new li
I am creating a Todolist, when I write something in the input and use the function addNewFunc() it adds a new li, the problem is that it always just overrides that li, it never makes a new li. (example: I write 1 in the input, use the function and 1 will be added as an li, if i write 2 in the input and use the function, that 1 will now become 2, instead of having a 1 and 2)

let input = document.querySelector(".input");
let ul = document.querySelector("ul");
let addNew = document.createElement("li");

let addNewFunc = function () {
  let name = ul.appendChild(addNew);
  name.textContent = input.value
};```
<pre class="snippet-code-html lang-html prettyprint-override"><code><h1>spooky</h1>

<input type="text" class="input">
<ul>
  <li>1</li>
  <li>2</li>
</ul></code>```

答:
A couple of issues.

1) Move let addNew = document.createElement("li"); inside the function. When it's outside the function you're just reusing the same element over and over. If that line is in the function, you create a new element each time the function is called.

2) Might be worth changing that function expression to a function declaration so that it works better as I was picking up the following error in JSFiddle when I was testing the code:

ReferenceError: can't access lexical declaration `addNewFunc' before initialization

```<code>let input = document.querySelector(".input");
let ul = document.querySelector("ul");
let button = document.querySelector('button');
button.addEventListener('click', addNewFunc)

function addNewFunc() {
  let addNew = document.createElement("li");
  addNew.textContent = input.value;
  ul.appendChild(addNew);
};
</code>```

DEMO (I added a button for convenience)

Body:
I am creating a Todolist, when I write something in the input and use the function addNewFunc() it adds a new li, the problem is that it always just overrides that li, it never makes a new li. (example: I write 1 in the input, use the function and 1 will be added as an li, if i write 2 in the input and use the function, that 1 will now become 2, instead of having a 1 and 2)

let input = document.querySelector(".input");
let ul = document.querySelector("ul");
let addNew = document.createElement("li");

let addNewFunc = function () {
  let name = ul.appendChild(addNew);
  name.textContent = input.value
};

spooky

<input type="text" class="input">

1

2

Body:
A couple of issues.

1) Move let addNew = document.createElement("li"); inside the function. When it's outside the function you're just reusing the same element over and over. If that line is in the function, you create a new element each time the function is called.

2) Might be worth changing that function expression to a function declaration so that it works better as I was picking up the following error in JSFiddle when I was testing the code:

ReferenceError: can't access lexical declaration `addNewFunc' before initialization

let input = document.querySelector(".input");
let ul = document.querySelector("ul");
let button = document.querySelector('button');
button.addEventListener('click', addNewFunc)

function addNewFunc() {
  let addNew = document.createElement("li");
  addNew.textContent = input.value;
  ul.appendChild(addNew);
};

DEMO (I added a button for convenience)


问:
Can I create a simple Talend Job using its Java Source Code?
can we generate a Talend Job through its Java code? For the sake of simplicity consider a simple Talend Job without any context variables for different environments and other complications. </p>

答:
Java code for a job is generated by the Studio, but there is no reverse operation consisting in generating a job from its corresponding Java code.

So, the short answer is "No, you can't".

You may consider to generate other files such as job.properties and job.item which contain the configuration for each component used by a job, but I'm afraid it could be very hazardous and at least a very long trip to try to do that.


问:
Ascii Encoding is ditching the £
I'm using the Google Shortener and the only working example I could find (below) encodes the URL string ASCII.  The problem is that when the link contains £ the ASCII replaces it with ?.  This means that when the link is shortened and its clicked, the URL is incorrect.  I've tried lots of different encodings but the this stops the Google shorterner from working.  I either need to come up with an encoding that the shortener the Google will allow or change the code.  I'd rather do the former.  Any help?</p>

```<code>public static string ShortenItBasic(string url, ref string returnedJson, ref bool succeeded, string urlShortenerId = null)
    {
        string finalURL = null;
        string post = "{\"longUrl\": \"" + url + "\"}";
        string urlShortener = null;

        if (!string.IsNullOrEmpty(urlShortenerId))
        {
            urlShortener = urlShortenerId;
        }
        else
        {
            urlShortener = ConfigKeys.GoogleShortenerAPIKey;
        }

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/urlshortener/v1/url?key=" + urlShortener);
        try
        {
            request.ServicePoint.Expect100Continue = false;
            request.Method = "POST";
            request.ContentLength = post.Length;
            request.ContentType = "application/json";
            request.Headers.Add("Cache-Control", "no-cache");

            using (Stream requestStream = request.GetRequestStream())
            {
                byte[] postBuffer = null;

                if (ConfigKeys.IsDebugMode)
                {
                    postBuffer = Encoding.GetEncoding("IBM437").GetBytes(post);

                    //postBuffer = Encoding.ASCII.GetBytes(post);
                }
                else
                {
                    postBuffer = Encoding.ASCII.GetBytes(post);
                }

                requestStream.Write(postBuffer, 0, postBuffer.Length);
            }
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (StreamReader responseReader = new StreamReader(responseStream))
                    {
                        string json = responseReader.ReadToEnd();

                        returnedJson = json;

                        finalURL = Regex.Match(json, @"""id"": ?""(?<id>.+)""").Groups["id"].Value;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            // if Google's URL Shortener is down...
            Logging.LoggingIO.LogHelper("GoogleBitly: Issue with the shortener: <br/><br/>" + ex.Message + " Stack: " + ex.StackTrace);

            succeeded = false;

            return "There is a problem with the Google shortening service: " + ex.Message + ".  Please try another target";
        }

        succeeded = true;
        return finalURL;
    }
</code>```

答: A URL cannot contain the £ character, since it's not an ASCII character. Any character outside of a small subset of ASCII valid in URLs must be percent encoded, e.g. to %C2%A3 for a UTF-8 encoded £, or %A3 for an ISO-8859-1 encoded £ (whichever your server prefers to receive). URL-encode your values when building the URL.

Title: Ascii Encoding is ditching the £

Body:
I'm using the Google Shortener and the only working example I could find (below) encodes the URL string ASCII.  The problem is that when the link contains £ the ASCII replaces it with ?.  This means that when the link is shortened and its clicked, the URL is incorrect.  I've tried lots of different encodings but the this stops the Google shorterner from working.  I either need to come up with an encoding that the shortener the Google will allow or change the code.  I'd rather do the former.  Any help?

public static string ShortenItBasic(string url, ref string returnedJson, ref bool succeeded, string urlShortenerId = null)
    {
        string finalURL = null;
        string post = "{\"longUrl\": \"" + url + "\"}";
        string urlShortener = null;

        if (!string.IsNullOrEmpty(urlShortenerId))
        {
            urlShortener = urlShortenerId;
        }
        else
        {
            urlShortener = ConfigKeys.GoogleShortenerAPIKey;
        }

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/urlshortener/v1/url?key=" + urlShortener);
        try
        {
            request.ServicePoint.Expect100Continue = false;
            request.Method = "POST";
            request.ContentLength = post.Length;
            request.ContentType = "application/json";
            request.Headers.Add("Cache-Control", "no-cache");

            using (Stream requestStream = request.GetRequestStream())
            {
                byte[] postBuffer = null;

                if (ConfigKeys.IsDebugMode)
                {
                    postBuffer = Encoding.GetEncoding("IBM437").GetBytes(post);

                    //postBuffer = Encoding.ASCII.GetBytes(post);
                }
                else
                {
                    postBuffer = Encoding.ASCII.GetBytes(post);
                }

                requestStream.Write(postBuffer, 0, postBuffer.Length);
            }
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (StreamReader responseReader = new StreamReader(responseStream))
                    {
                        string json = responseReader.ReadToEnd();

                        returnedJson = json;

                        finalURL = Regex.Match(json, @"""id"": ?""(?<id>.+)""").Groups["id"].Value;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            // if Google's URL Shortener is down...
            Logging.LoggingIO.LogHelper("GoogleBitly: Issue with the shortener:

" + ex.Message + " Stack: " + ex.StackTrace);

            succeeded = false;

            return "There is a problem with the Google shortening service: " + ex.Message + ".  Please try another target";
        }

        succeeded = true;
        return finalURL;
    }


问:
How to optimize query which has to get last 5 rows with joined table
I have two tables events and user_device. events and user_device has a common field device_id.

user_device table has fields user_id and device_id. Basically, user_device table holding all devices belongs to users.

events table holding all events belongs to devices.

Now I want to get last 5 alerts for a specific user.

So I have made a query by joining both tables like below.

```<code>SELECT *
FROM events
LEFT JOIN user_device ON user_device.deviceid=events.deviceid
WHERE user_device.userid=101
ORDER BY events.id DESC
LIMIT 5
</code>```

events table has more than 4 million records. This query takes 30 seconds to return the results.

If I remove ORDER BY, the query takes only two seconds.

How can I optimize this?

答:
First: don't use SELECT *. Instead, give the names of the columns you want.

Second: You're looking for an equality match on user_device.userid. So you need an index on user_device starting with the userid column. You're then employing the value of deviceid in that same table. So, create this index. It's called a covering index.

```<code> ALTER TABLE user_device ADD INDEX x_user_device (userid, deviceid);
</code>```

Third: You're looking up rows in events by deviceid, then ordering by id. So you need another covering index on those two columns.

```<code> ALTER TABLE events ADD INDEX x_device_id (deviceid, id);
</code>```

Fourth: you mention a column from your LEFT JOINed table in a WHERE clause. That converts the LEFT JOIN to an ordinary inner JOIN. So use JOIN.

Fifth:  SELECT * ... ORDER BY ... LIMIT is a notorious performance antipattern. Why? It has to order a whole mess of records, just to discard all but a few.  Try this instead.  First get the relevant events.id values with a subquery.

```<code>          SELECT events.id
            FROM events
            JOIN user_device ON user_device.deviceid=events.deviceid
           WHERE user_device.userid=101
           ORDER BY events.id DESC
           LIMIT 5
</code>```

Test that subquery. It should give you five relevant event id values, and it should do it very quickly indeed.  Then use this subquery to look up the details you need from your two tables:

```<code>SELECT events.*, user_device.*      /* not optimal. list only the columns you need */
  FROM (
          SELECT events.id
            FROM events
            JOIN user_device ON user_device.deviceid=events.deviceid
           WHERE user_device.userid=101
           ORDER BY events.id DESC
           LIMIT 5
       ) sel
  JOIN events ON sel.id = events.id
  JOIN user_device ON events.deviceid = user_device.deviceid
</code>```

This is called the deferred join query pattern. It does all the ordering on just a pile of id values, then pulls out only a few records.

This should help you keep performance in check as your database grows.

Body:
I have two tables events and user_device. events and user_device has a common field device_id.

user_device table has fields user_id and device_id. Basically, user_device table holding all devices belongs to users.

events table holding all events belongs to devices.

Now I want to get last 5 alerts for a specific user.

So I have made a query by joining both tables like below.

SELECT *
FROM events
LEFT JOIN user_device ON user_device.deviceid=events.deviceid
WHERE user_device.userid=101
ORDER BY events.id DESC
LIMIT 5

events table has more than 4 million records. This query takes 30 seconds to return the results.

If I remove ORDER BY, the query takes only two seconds.

How can I optimize this?

Body:
First: don't use SELECT *. Instead, give the names of the columns you want.

Second: You're looking for an equality match on user_device.userid. So you need an index on user_device starting with the userid column. You're then employing the value of deviceid in that same table. So, create this index. It's called a covering index.

 ALTER TABLE user_device ADD INDEX x_user_device (userid, deviceid);

Third: You're looking up rows in events by deviceid, then ordering by id. So you need another covering index on those two columns.

 ALTER TABLE events ADD INDEX x_device_id (deviceid, id);

Fourth: you mention a column from your LEFT JOINed table in a WHERE clause. That converts the LEFT JOIN to an ordinary inner JOIN. So use JOIN.

Fifth:  SELECT * ... ORDER BY ... LIMIT is a notorious performance antipattern. Why? It has to order a whole mess of records, just to discard all but a few.  Try this instead.  First get the relevant events.id values with a subquery.

          SELECT events.id
            FROM events
            JOIN user_device ON user_device.deviceid=events.deviceid
           WHERE user_device.userid=101
           ORDER BY events.id DESC
           LIMIT 5

Test that subquery. It should give you five relevant event id values, and it should do it very quickly indeed.  Then use this subquery to look up the details you need from your two tables:

SELECT events.*, user_device.*      /* not optimal. list only the columns you need */
  FROM (
          SELECT events.id
            FROM events
            JOIN user_device ON user_device.deviceid=events.deviceid
           WHERE user_device.userid=101
           ORDER BY events.id DESC
           LIMIT 5
       ) sel
  JOIN events ON sel.id = events.id
  JOIN user_device ON events.deviceid = user_device.deviceid

This is called the deferred join query pattern. It does all the ordering on just a pile of id values, then pulls out only a few records.

This should help you keep performance in check as your database grows.


问:
Upload dilemma for new records with GUID as image name
For my CMS I want users to be able to upload pictures which connect to a product. This all goes well until someone wants to upload a picture when creating a new record.

The problem here is that every product has a GUID (next to the ProductID (PK)) and will be created when the product is saved for the first time. I'm uploading my images trough a webservice with Uploadify jquery plugin so it's all in an ajax based way. The image uploader however needs to know the GUID to store it in the right directory.

My temporary fix was to just let the user save the product before uploading so the GUID is known but it's not very user friendly and I'm sure there would be a better way but I cannot find a satisfying one myself.

This is the only option which I could come up with which is kinda ok but maybe I'm missing something completely:

Create a GUID without saving the product, so the image can be uploaded and the product with the GUID can be saved afterwards. But what if the user navigates away after uploading?  I could maybe create a scheduled task to delete those orphant images

Has anyone dealt with similar scenarios? I'm very interested in your opions about his matter since this functionality will be used in multiple sections in my CMS.

Thanks for your time!

Kind regards,
Mark

答:
You mostly answered your question by yourself.

The key are temporary files. Here is a little smarter method: save image in a temporary folder and remember the image name in Session object. If the user navigates away, in the Session_End method of Global.asax, delete those images (better said garbage-collect them).

If the user saves the product, permanently store the images and name them accordingly!!


问:
Spring JMS : Configure Multi Consumer client
I am developing a Spring JMS based client, which connects to an IBM MQ.

This part is working fine !!

Scenario:

The MQ client can get a request from multiple Spring "@Components". Considering, JMSTemplate's "send" and "receive" are completely different methods -

Question:

How do we co-relate the response from component-one to only its request ? e.g.

A sends request-A

B sends request-B

How will SpringListener know to respond the response-A to A, and response-B to B ?

Does spring provide an out of box functionality to handle such a scenario ?

Thanks in advance !!

Sample Listener :

```<code>public class MessageReceiver implements MessageListener {

    @Override
    public void onMessage(Message message) {

        if(message instanceof TextMessage){
            System.out.println(message.toString());
        }
    }
}
</code>```

Sample Sender :

```<code>public class App
{
    public static void main( String[] args )
    {

        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        JmsTemplate jmsTemplate = (JmsTemplate)context.getBean("jmsTemplate");

        jmsTemplate.send(s -> s.createTextMessage("TestingAMQ"));
    }
}
</code>```

答:
based on the JMSReplyTo header you can do it with
JmsTemplate.sendAndReceive for the sender

and in the listener side by

```<code>    public class MessageReceiver implements MessageListener {

        @Override
        public void onMessage(Message message) {
            jmsTemplate.send(message.getJMSReplyTo(), s -> s.createTextMessage("TestingAMQ"));
            if(message instanceof TextMessage){
                System.out.println(message.toString());
            }
        }
   }
</code>```

OR

By setting a Message property in the sender side by Message.setStringProperty and use selectors in the listener or conditional response based on the StringProperty.

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/jms/core/JmsTemplate.html#sendAndReceive-org.springframework.jms.core.MessageCreator-

Body:
I am developing a Spring JMS based client, which connects to an IBM MQ.

This part is working fine !!

Scenario:

The MQ client can get a request from multiple Spring "@Components". Considering, JMSTemplate's "send" and "receive" are completely different methods -

Question:

How do we co-relate the response from component-one to only its request ? e.g.

A sends request-A

B sends request-B

How will SpringListener know to respond the response-A to A, and response-B to B ?

Does spring provide an out of box functionality to handle such a scenario ?

Thanks in advance !!

Sample Listener :

public class MessageReceiver implements MessageListener {

    @Override
    public void onMessage(Message message) {

        if(message instanceof TextMessage){
            System.out.println(message.toString());
        }
    }
}

Sample Sender :

public class App
{
    public static void main( String[] args )
    {

        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        JmsTemplate jmsTemplate = (JmsTemplate)context.getBean("jmsTemplate");

        jmsTemplate.send(s -> s.createTextMessage("TestingAMQ"));
    }
}

Body:
based on the JMSReplyTo header you can do it with
JmsTemplate.sendAndReceive for the sender

and in the listener side by

    public class MessageReceiver implements MessageListener {

        @Override
        public void onMessage(Message message) {
            jmsTemplate.send(message.getJMSReplyTo(), s -> s.createTextMessage("TestingAMQ"));
            if(message instanceof TextMessage){
                System.out.println(message.toString());
            }
        }
   }

OR

By setting a Message property in the sender side by Message.setStringProperty and use selectors in the listener or conditional response based on the StringProperty.

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/jms/core/JmsTemplate.html#sendAndReceive-org.springframework.jms.core.MessageCreator-


问:
Consolidate function in excel- Not getting the desired format
Its my first time using consolidate in excel 2010. Its working as expected, however I am not getting the desired format in the output table.

I have a summary tab for individual Test Plans. I am consolidating this data into a Master Summary but not getting the desired output of the table. What am I missing?

Test Plan 1

Test Plan 2

Summary after consolidation of 2 test plans

Notice that #Tests is towards the left (in a new row) and then the Workbook Name is present. I would like the Workbook Name to be displayed in the left most column and #Test should be after that (same row).

Desired output

答:
I have an idea to solve this problem with a formula instead of using data consolidation, but it has limitations on what the sheet tabs are named. I'll explain all details below.

As I understand it, you have several different sheets in your workbook, each sheet corresponding to a different "test plan", and then you have one "summary" sheet which contains all info about all test plans. So your tabs would look something like this:

In your "summary" sheet, you can paste this formula into cell A2 (line break added for readability):

```<code>= IFERROR(INDIRECT("'Test Plan "&(ROW()-1)&"'!"
  &IF(COLUMN()=1,"A1",ADDRESS(4,COLUMN()-1))),"")
</code>```

Drag this formula over to cell L2 and then drag down as far as necessary. Note if you drag down too far, it isn't an issue because the IFERROR(...,"") part of the formula just returns blank cells if a match isn't found.

This formula will put your data in the desired format and the order of the test plans in the generated table will be in the same order of the tab names. In general I'm not a huge fan of using the volatile INDIRECT function, but in this case, it is the only option that I can think of.

IMPORTANT!! Of course, the major limitation to this scheme is that all test plan sheet tabs must be exactly of the form: Test Plan # because this is the tab name that the formula above is searching for. (Note that you can have more than 9 test plans and it will still work - it isn't limited to one character for the test plan number if you were wondering that.) If your sheets are not (or cannot) be named this, then this scheme will not work for you, but if you can handle changing your sheet tab names to this format, then this will work.

Body:
I have an idea to solve this problem with a formula instead of using data consolidation, but it has limitations on what the sheet tabs are named. I'll explain all details below.

As I understand it, you have several different sheets in your workbook, each sheet corresponding to a different "test plan", and then you have one "summary" sheet which contains all info about all test plans. So your tabs would look something like this:

In your "summary" sheet, you can paste this formula into cell A2 (line break added for readability):

= IFERROR(INDIRECT("'Test Plan "&(ROW()-1)&"'!"
  &IF(COLUMN()=1,"A1",ADDRESS(4,COLUMN()-1))),"")

Drag this formula over to cell L2 and then drag down as far as necessary. Note if you drag down too far, it isn't an issue because the IFERROR(...,"") part of the formula just returns blank cells if a match isn't found.

This formula will put your data in the desired format and the order of the test plans in the generated table will be in the same order of the tab names. In general I'm not a huge fan of using the volatile INDIRECT function, but in this case, it is the only option that I can think of.

IMPORTANT!! Of course, the major limitation to this scheme is that all test plan sheet tabs must be exactly of the form: Test Plan # because this is the tab name that the formula above is searching for. (Note that you can have more than 9 test plans and it will still work - it isn't limited to one character for the test plan number if you were wondering that.) If your sheets are not (or cannot) be named this, then this scheme will not work for you, but if you can handle changing your sheet tab names to this format, then this will work.


问:
Facebook API "conversations" webhook
I'm developing a bot that receives a call from "conversations webhook" to know when a page receives a new message and answer it from my bot.

I've tried "messages" but I can not even make it activate when they send me a message.

I have my conversations webhook active. The "conversations" callback is something like this:

```<code>{"entry":[{"changes":[{"field":"conversations","value":{"thread_id":"t_100006208XXXXXX","page_id":309151156XXXXXX,"thread_key":"t_100006208XXXXXX"}}],"id":"309151156XXXXXX","time":1510581746}],"object":"page"}
</code>```

The problem is that the webhook always sends the same thread_id and thread_key. The page_id does change, but I can not find the way to know which conversation is the one that varies.

Thank you!

答: I solved it! The problem was that I made the call to the api with an access_token from my app, and not from the site itself.</p>

Title: Facebook API "conversations" webhook

Body:
I'm developing a bot that receives a call from "conversations webhook" to know when a page receives a new message and answer it from my bot.

I've tried "messages" but I can not even make it activate when they send me a message.

I have my conversations webhook active. The "conversations" callback is something like this:

{"entry":[{"changes":[{"field":"conversations","value":{"thread_id":"t_100006208XXXXXX","page_id":309151156XXXXXX,"thread_key":"t_100006208XXXXXX"}}],"id":"309151156XXXXXX","time":1510581746}],"object":"page"}

The problem is that the webhook always sends the same thread_id and thread_key. The page_id does change, but I can not find the way to know which conversation is the one that varies.

Thank you!


问:
Which terminal command to get just IP address and nothing else?
I'm trying to use just the IP address (inet) as a parameter in a script I wrote.

Is there an easy way in a unix terminal to get just the IP address, rather than looking through ifconfig?

答:
That would do the trick in a Mac :

```<code>ping $(ifconfig en0 | awk '$1 == "inet" {print $2}')
</code>```

That resolved to ping 192.168.1.2 in my machine.

Pro tip: $(...) means run whatever is inside the parentheses in a subshell and return that as the value.

Body:
That would do the trick in a Mac :

ping $(ifconfig en0 | awk '$1 == "inet" {print $2}')

That resolved to ping 192.168.1.2 in my machine.

Pro tip: $(...) means run whatever is inside the parentheses in a subshell and return that as the value.


问:
XPath issue in Maven replacer plugin
I get the following error when trying to use XPath to replace in web.xml with the Maven replacer plugin

```<code>[ERROR] Failed to execute goal com.google.code.maven-replacer-plugin:replacer:1.5.2:replace (default) on project my-project:
Error during XML replacement: Content is not allowed in prolog. -> [Help 1]
</code>```

And here's the Maven code

```<code><plugin>
    <groupId>com.google.code.maven-replacer-plugin</groupId>
    <artifactId>replacer</artifactId>
    <version>1.5.2</version>
    <executions>
        <execution>
            <phase>prepare-package</phase> <goals>
                <goal>replace</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <file>src/main/webapp/WEB-INF/web.xml</file>
        <outputFile>target/web.xml</outputFile>
        <regexFlags>
            <regexFlag>CASE_INSENSITIVE</regexFlag>
        </regexFlags>
<replacements>
    <replacement>
        <xpath>/web-app/display-name</xpath>
        <token>.*</token>
        <value>${project.name}</value>
    </replacement>
</replacements>
</plugin>
</code>```

web.xml

```<code><?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     id="my-project"
     version="2.5">

    <display-name>My Project</display-name>
    ...
</web-app>
</code>```

Any idea why?

答:
Select the text node you wish to replace entirely instead:

```<code><replacement>
    <xpath>/web-app/display-name/text()</xpath>
    <token>^.*$</token>
    <value>${project.name}</value>
</replacement>
</code>```

The replacer plugin does the following when working with xpaths that select a node:

Select the node (<display-name>My Project</display-name>)

Convert the entire node to a String ("<?xml version="1.0" encoding="UTF-8"?><display-name>My Project</display-name>")

Replace the token (.*) in that String (String is now "${project.name}" - no more xml tags)

Parse the replaced String back into an XML Node: this breaks because your String is not valid XML (because you replaced it entirely.)

Easiest solution to this problem is to select the text node of the node instead.

Bonus: in this case, if your token is .* it will match twice (I'm not sure why) so make sure to use ^.*$ instead.

Title: XPath issue in Maven replacer plugin

Body:
I get the following error when trying to use XPath to replace in web.xml with the Maven replacer plugin

[ERROR] Failed to execute goal com.google.code.maven-replacer-plugin:replacer:1.5.2:replace (default) on project my-project:
Error during XML replacement: Content is not allowed in prolog. -> [Help 1]

And here's the Maven code

<plugin>
    <groupId>com.google.code.maven-replacer-plugin</groupId>
    <artifactId>replacer</artifactId>
    <version>1.5.2</version>
    <executions>
        <execution>
            <phase>prepare-package</phase> <goals>
                <goal>replace</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <file>src/main/webapp/WEB-INF/web.xml</file>
        <outputFile>target/web.xml</outputFile>
        <regexFlags>
            <regexFlag>CASE_INSENSITIVE</regexFlag>
        </regexFlags>
<replacements>
    <replacement>
        <xpath>/web-app/display-name</xpath>
        <token>.*</token>
        <value>${project.name}</value>
    </replacement>
</replacements>
</plugin>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     id="my-project"
     version="2.5">

    <display-name>My Project</display-name>
    ...
</web-app>

Any idea why?

Body:
Select the text node you wish to replace entirely instead:

<replacement>
    <xpath>/web-app/display-name/text()</xpath>
    <token>^.*$</token>
    <value>${project.name}</value>
</replacement>

The replacer plugin does the following when working with xpaths that select a node:

Select the node (<display-name>My Project</display-name>)

Convert the entire node to a String ("<?xml version="1.0" encoding="UTF-8"?><display-name>My Project</display-name>")

Replace the token (.*) in that String (String is now "${project.name}" - no more xml tags)

Parse the replaced String back into an XML Node: this breaks because your String is not valid XML (because you replaced it entirely.)

Easiest solution to this problem is to select the text node of the node instead.

Bonus: in this case, if your token is .* it will match twice (I'm not sure why) so make sure to use ^.*$ instead.


问:
Write python dictionary into textfile
I want to write my python dictionary in a text file where each line contain one key and its value, with comma as separation. I am using the following code:

```<code>with open(compounds, 'w') as f:
    for key, value in dict_NTID_IDs_in_EisDatabase.items():
        f.write('%s:%s\n' % (key, value))
</code>```

But it returns me the following error TypeError: expected str, bytes or os.PathLike object, not list

答:
It should work with this :

```<code>with open(compounds, 'w') as f:
    for key, value in zip(dict_NTID_IDs_in_EisDatabase.keys(), dict_NTID_IDs_in_EisDatabase.values()):
        f.write('%s,%s\n' % (key, value))
</code>```

You can also use dict.items() as you mentionned but dict.items() returns a tuple ((key0, value0), (key1, value1) ...).

To get back to your question, you can use :

```<code>with open(compounds, 'w') as f:
    for item in dict_NTID_IDs_in_EisDatabase.items():
        f.write('%s,%s\n' % (item[0], item[1]))
</code>```

Title: Write python dictionary into textfile

Body:
I want to write my python dictionary in a text file where each line contain one key and its value, with comma as separation. I am using the following code:

with open(compounds, 'w') as f:
    for key, value in dict_NTID_IDs_in_EisDatabase.items():
        f.write('%s:%s\n' % (key, value))

But it returns me the following error TypeError: expected str, bytes or os.PathLike object, not list

Body:
It should work with this :

with open(compounds, 'w') as f:
    for key, value in zip(dict_NTID_IDs_in_EisDatabase.keys(), dict_NTID_IDs_in_EisDatabase.values()):
        f.write('%s,%s\n' % (key, value))

You can also use dict.items() as you mentionned but dict.items() returns a tuple ((key0, value0), (key1, value1) ...).

To get back to your question, you can use :

with open(compounds, 'w') as f:
    for item in dict_NTID_IDs_in_EisDatabase.items():
        f.write('%s,%s\n' % (item[0], item[1]))


问:
jQuery, move ALL elements lower
I have a serious issue: with jQuery, I have to move ALL elements toward down, because an advertisement banner must be put up there so that it wont cover anything. Its like havin a frameset at the top.
I was trying this:

```<code>jQuery('*').each(function() {
    var $this = jQuery(this);
    if ($this.css('position') === 'fixed' || $this.css('position') === 'absolute')
    {
        $this.css('margin-top', 80);
    }
});
</code>```

but not all element are actually pushed.

答:
Try this.</p>

```<code>jQuery(document).ready(function() {
        /*
        // For Down all Element Viewport
        $('html').css('margin-top', 80);
        // or
        // For Down all Element Body

        $('body').css('margin-top', 80);
        */
        // For all fixed or absolute element

       var elems = document.body.getElementsByTagName("*");
       var len = elems.length

       for (var i=0;i<len;i++) {

       if (window.getComputedStyle(elems[i],null).getPropertyValue('position') == 'fixed' || window.getComputedStyle(elems[i],null).getPropertyValue('position') == 'absolute') {
            elems[i].style.marginTop ='80px';
            }
        }
});

// If that content has a specific selector like ID or class, then use
$( your selector ).css("margin-top","80px");
</code>```

Title: jQuery, move ALL elements lower

Body:
I have a serious issue: with jQuery, I have to move ALL elements toward down, because an advertisement banner must be put up there so that it wont cover anything. Its like havin a frameset at the top.
I was trying this:

jQuery('*').each(function() {
    var $this = jQuery(this);
    if ($this.css('position') === 'fixed' || $this.css('position') === 'absolute')
    {
        $this.css('margin-top', 80);
    }
});

but not all element are actually pushed.

Body:
Try this.

jQuery(document).ready(function() {
        /*
        // For Down all Element Viewport
        $('html').css('margin-top', 80);
        // or
        // For Down all Element Body

        $('body').css('margin-top', 80);
        */
        // For all fixed or absolute element

       var elems = document.body.getElementsByTagName("*");
       var len = elems.length

       for (var i=0;i<len;i++) {

       if (window.getComputedStyle(elems[i],null).getPropertyValue('position') == 'fixed' || window.getComputedStyle(elems[i],null).getPropertyValue('position') == 'absolute') {
            elems[i].style.marginTop ='80px';
            }
        }
});

// If that content has a specific selector like ID or class, then use
$( your selector ).css("margin-top","80px");


问:
MySQL connection pooling in separate DB class - howto?
I'm writing an application where I've moved all the MySQL connection setup and teardown to a class, initializing within individual function calls with a With statement.

Now that the development is all done, I'm optimizing and would like to set up connection pooling - but I can't for the life of me figure out how - if I initialize the pool when I set up the object in enter, won't that set up a new pool for each object?

If I put the pool setup in the global of the module, then how do I ensure I set up the pool before I start creating DB objects?

My DB code looks somewhat like this:

```<code># Setting up details for connecting to a local MariaDB/MySQL instance
# replace with suitable code/module when porting to cloud/production
import sys
import mysql.connector
"""Module for abstracting database connectivity
Import this module and then call run_query(), run_query_vals() or run_query_no_return()    """

__all__ = ['UseDatabase', 'CredentialsError', 'ConnectionError', 'SQLError']

class ConnectionError(Exception):
    pass

class CredentialsError(Exception):
    pass

class SQLError(Exception):
    pass

dbconfig = { 'host': '127.0.0.1', 'user' : 'statdev', 'password' : 'statdev', 'database': 'stat',}
# Just so we remember. This also doubles as default server details while doing unit testing.

class UseDatabase:
#    myconfig = {'host': '127.0.0.1', 'user': 'statdev', 'password': 'statdev', 'database': 'stat', }
    config = None

    def __init__(self, config: dict):
        self.config = config

    def __enter__(self) -> 'self':
        try:
            self.conn = mysql.connector.connect(**self.config)
            self.cursor = self.conn.cursor(dictionary=True)
            return self
        except mysql.connector.InterfaceError as err:
            print('Can\'t connect to Database - is it available? \nError: ', str(err))
            raise ConnectionError(err)
        except mysql.connector.ProgrammingError as err:
            print('Invalid credentials - please check ID/Password. \nError: ', str(err))
            raise CredentialsError(err)
        except mysql.connector.IntegrityError as err:
            print("Error: {}".format(err))

        except Exception as err:
            print('Something else went wrong:', str(err))
            return err

    def __exit__(self, exc_type, exc_value, exc_traceback):
        self.conn.commit()
        self.cursor.close()
        self.conn.close()
        if exc_type is mysql.connector.errors.ProgrammingError:
            print('Error in SQL Code - please check the query. \nError: ', str(exc_type))
            raise SQLError(exc_value)
        elif exc_type:
            print('Something else went wrong\n', str(exc_type))
            raise exc_type(exc_value)

    def run_query(self,query_str) -> 'cursor':
        """query function that takes """
        self.cursor.execute(query_str, None)
        return self.cursor

    def run_query_vals(self, query_str, tupleval) -> 'cursor':
#        print("\n\n %s " % query_str)
        self.cursor.execute(query_str, tupleval)
        return self.cursor

    def run_query_no_return(self,query_str) -> 'cursor':
        """query function that takes """
        self.cursor.execute(query_str)
        return self.cursor

def test():
#    dbconfig = {'host': '127.0.0.1', 'user': 'statdev', 'password': 'statdev', 'database': 'stat', }
    with UseDatabase(dbconfig) as db:
#        result = db.run_query("Select NULL from dual")
        result = db.run_query_vals('Select NULL from dual', None)
        res = result.fetchone()
        if res == {'NULL': None}:
            print("DB Module Test was successful! \n"
                  "Queries return values in dictionaries."
                  "\nTest query \'Select NULL from dual\' returned result: %s" % str(res))

if __name__ == '__main__':
    test()
</code>```

答:
This has worked for me but I am not sure it's a perfect solution as, for example, trying to do multiple inserts via a for loop results in a 'Failed getting connection; pool exhausted' error. I did not have this problem when I was using a function-based (non class-based) connection pool. Anyway, to avoid this problem I just simply use 'cursor.executemany' in one go.

Hope this helps someone!

```<code>from mysql.connector.pooling import MySQLConnectionPool
from mysql.connector.errors import ProgrammingError, InterfaceError
from settings import config

# Database connection pool
dbconfig = config.dbconfig
dbconfig_pool = config.dbconfig_pool

#The following is my 'class DBasePool' content:
def __init__(self, dbconfig, dbconfig_pool):
    self.dbconfig = dbconfig
    self.pool_name = dbconfig_pool['pool_name']
    self.pool_size = dbconfig_pool['pool_size']
    try:
        self.cnxpool = self.create_pool(pool_name=self.pool_name, pool_size=self.pool_size)
        self.cnx = self.cnxpool.get_connection()
        self.cursor = self.cnx.cursor(buffered=True)

    except InterfaceError as e:
        logger.error(e)
        raise ConnectionError(e)
    except ProgrammingError as e:
        logger.error(e)
        raise CredentialsError(e)
    except Exception as e:
        logger.error(e)
        raise

def create_pool(self, pool_name, pool_size):
    return MySQLConnectionPool(pool_name=pool_name, pool_size= pool_size, **self.dbconfig)

def close(self, cnx, cursor):
    cursor.close()
    cnx.close()

def execute(self, sql, data=None):
    # Get connection form connection pool instead of creating one
    cnx = self.cnxpool.get_connection()
    cursor = cnx.cursor(buffered=True)

    cursor.execute(sql, data)

    if cursor.rowcount:
        cnx.commit()
        rowcount = cursor.rowcount
        self.close(cnx, cursor)
        return rowcount
    else:
        print('Could not insert record(s): {}, {}'.format(sql, data))
        return 0
</code>```

Body:
I'm writing an application where I've moved all the MySQL connection setup and teardown to a class, initializing within individual function calls with a With statement.

Now that the development is all done, I'm optimizing and would like to set up connection pooling - but I can't for the life of me figure out how - if I initialize the pool when I set up the object in enter, won't that set up a new pool for each object?

If I put the pool setup in the global of the module, then how do I ensure I set up the pool before I start creating DB objects?

My DB code looks somewhat like this:

# Setting up details for connecting to a local MariaDB/MySQL instance
# replace with suitable code/module when porting to cloud/production
import sys
import mysql.connector
"""Module for abstracting database connectivity
Import this module and then call run_query(), run_query_vals() or run_query_no_return()    """

__all__ = ['UseDatabase', 'CredentialsError', 'ConnectionError', 'SQLError']

class ConnectionError(Exception):
    pass

class CredentialsError(Exception):
    pass

class SQLError(Exception):
    pass

dbconfig = { 'host': '127.0.0.1', 'user' : 'statdev', 'password' : 'statdev', 'database': 'stat',}
# Just so we remember. This also doubles as default server details while doing unit testing.

class UseDatabase:
#    myconfig = {'host': '127.0.0.1', 'user': 'statdev', 'password': 'statdev', 'database': 'stat', }
    config = None

    def __init__(self, config: dict):
        self.config = config

    def __enter__(self) -> 'self':
        try:
            self.conn = mysql.connector.connect(**self.config)
            self.cursor = self.conn.cursor(dictionary=True)
            return self
        except mysql.connector.InterfaceError as err:
            print('Can\'t connect to Database - is it available? \nError: ', str(err))
            raise ConnectionError(err)
        except mysql.connector.ProgrammingError as err:
            print('Invalid credentials - please check ID/Password. \nError: ', str(err))
            raise CredentialsError(err)
        except mysql.connector.IntegrityError as err:
            print("Error: {}".format(err))

        except Exception as err:
            print('Something else went wrong:', str(err))
            return err

    def __exit__(self, exc_type, exc_value, exc_traceback):
        self.conn.commit()
        self.cursor.close()
        self.conn.close()
        if exc_type is mysql.connector.errors.ProgrammingError:
            print('Error in SQL Code - please check the query. \nError: ', str(exc_type))
            raise SQLError(exc_value)
        elif exc_type:
            print('Something else went wrong\n', str(exc_type))
            raise exc_type(exc_value)

    def run_query(self,query_str) -> 'cursor':
        """query function that takes """
        self.cursor.execute(query_str, None)
        return self.cursor

    def run_query_vals(self, query_str, tupleval) -> 'cursor':
#        print("\n\n %s " % query_str)
        self.cursor.execute(query_str, tupleval)
        return self.cursor

    def run_query_no_return(self,query_str) -> 'cursor':
        """query function that takes """
        self.cursor.execute(query_str)
        return self.cursor

def test():
#    dbconfig = {'host': '127.0.0.1', 'user': 'statdev', 'password': 'statdev', 'database': 'stat', }
    with UseDatabase(dbconfig) as db:
#        result = db.run_query("Select NULL from dual")
        result = db.run_query_vals('Select NULL from dual', None)
        res = result.fetchone()
        if res == {'NULL': None}:
            print("DB Module Test was successful! \n"
                  "Queries return values in dictionaries."
                  "\nTest query \'Select NULL from dual\' returned result: %s" % str(res))

if __name__ == '__main__':
    test()

Body:
This has worked for me but I am not sure it's a perfect solution as, for example, trying to do multiple inserts via a for loop results in a 'Failed getting connection; pool exhausted' error. I did not have this problem when I was using a function-based (non class-based) connection pool. Anyway, to avoid this problem I just simply use 'cursor.executemany' in one go.

Hope this helps someone!

from mysql.connector.pooling import MySQLConnectionPool
from mysql.connector.errors import ProgrammingError, InterfaceError
from settings import config

# Database connection pool
dbconfig = config.dbconfig
dbconfig_pool = config.dbconfig_pool

#The following is my 'class DBasePool' content:
def __init__(self, dbconfig, dbconfig_pool):
    self.dbconfig = dbconfig
    self.pool_name = dbconfig_pool['pool_name']
    self.pool_size = dbconfig_pool['pool_size']
    try:
        self.cnxpool = self.create_pool(pool_name=self.pool_name, pool_size=self.pool_size)
        self.cnx = self.cnxpool.get_connection()
        self.cursor = self.cnx.cursor(buffered=True)

    except InterfaceError as e:
        logger.error(e)
        raise ConnectionError(e)
    except ProgrammingError as e:
        logger.error(e)
        raise CredentialsError(e)
    except Exception as e:
        logger.error(e)
        raise

def create_pool(self, pool_name, pool_size):
    return MySQLConnectionPool(pool_name=pool_name, pool_size= pool_size, **self.dbconfig)

def close(self, cnx, cursor):
    cursor.close()
    cnx.close()

def execute(self, sql, data=None):
    # Get connection form connection pool instead of creating one
    cnx = self.cnxpool.get_connection()
    cursor = cnx.cursor(buffered=True)

    cursor.execute(sql, data)

    if cursor.rowcount:
        cnx.commit()
        rowcount = cursor.rowcount
        self.close(cnx, cursor)
        return rowcount
    else:
        print('Could not insert record(s): {}, {}'.format(sql, data))
        return 0


问:
How frequently can I send data using Socket.IO?
I'm creating a web application that would require small amounts of data (3 integer values per socket) to be sent from the server to the client very frequently and I wanted to see if there's a maximum frequency for updating the client using Socket.IO.

I was hoping to achieve at least 50 socket connections sending 20 updates per second each. With the ideal number being 200 socket connections sending 50 updates per second.

Question: Is there a limit to how often I can send new data using Socket.IO?

Note: I recognize this also becomes a speed issue of the server-client connection so any information on how fast of a connection I would need is appreciated. I calculated that if each data packet being sent is approximately 500 bytes, then I would be able to send 20 updates a second to 100 connections on a 1 MB/s connection.

答:
That's a very system, network and code dependent question.

Here is a small test harness I've used for similar plain socket.io testing before, I've plugged in some bits to fit your question.

Server

```<code>const io = require('socket.io')(8082)
const connections = []

io.on('connection', function(socket){

  connections.push(socket);
  const slog = (msg, ...args) => console.log('%s %s '+msg, Date.now(), socket.id, ...args)
  slog('Client connected. Total: %s', connections.length)

  socket.on('disconnect', function(data){
    connections.splice(connections.indexOf(socket), 1);
    slog('Client disconnected. Total: %s', connections.length)
  })

  socket.on('single', function(data){
    socket.emit('single',[ 0, now, now, now ])
  })

  socket.on('start', function(data = {}){
    slog('Start stream', data)
    sendBatch(1, data.count, data.delay)
  })

  socket.on('start dump', function(data = {}){
    slog('Start dump', data)
    sendBatch(1, data.count)
  })

  function sendBatch(i, max, delay){
    if ( i > max ) return slog('Done batch %s %s', max, delay)
    socket.emit('batch',[ i, now, now, now ])
    if (delay) {
      setTimeout(()=> sendBatch(i++, max, delay), delay)
    } else {
      setImmediate(()=> sendBatch(i++, max))
    }
  }

})
</code>```

Client

```<code>const io = require('socket.io-client')
const socket = io('http://localhost:8082', {transports: ['websocket']})

socket.on('connect_error', err => console.error('Socket connect error:', err))
socket.on('connect_timeout', err => console.error('Socket connect timeout:', err))
socket.on('reconnect', err => console.error('Socket reconnect:', err))
socket.on('reconnect_attempt', err => console.error('Socket reconnect attempt:', err))
socket.on('reconnecting', err => console.error('Socket reconnecting', err))
socket.on('reconnect_error', err => console.error('Socket reconnect error:', err))
socket.on('reconnect_failed', err => console.error('Socket reconnect failed:', err))

function batch(n){
  socket.on('batch', function(data){
    if ( data[0] >= n ) {
      let end = Date.now()
      let persec = n / (( end - start ) / 1000)
      console.log('Took %s ms for %s at %s/s', end - start, n, persec.toFixed(1))
      return socket.close()
    }
  })
}

function startDump(count = 500000){
  socket.emit('start dump', { count: count })
  console.log('Start dump', count)
  batch(count)
}
function startStream(count = 50, delay = 1000){
  socket.emit('start', { count: count, delay: delay })
  console.log('Start stream', count, delay)
  batch(count)
}

function pingIt(i, max = 50){
  socket.on('single', function(data){
    console.log('Got a single with:', data)
    if (i >= max) {
      let end = Date.now()
      let persec = i / (end - start) * 1000
      console.log('Took %s ms %s/s', end - start, persec.toFixed(2))
      return socket.close()
    }
    socket.emit('single', i+=1)
  })
  socket.emit('single', i)
}

let start = Date.now()

//console.log('args command: %s  count: %s  delay: %s',process.argv[2], process.argv[3], process.argv[4])
switch(process.argv[2]){
  case 'ping':   pingIt(0, process.argv[3]); break
  case 'stream': startStream(process.argv[3], process.argv[4]); break
  case 'dump':   startDump(process.argv[3]); break
  default:       console.log('ping stream dump'); socket.close()
}
</code>```

To test request/response round trips

```<code> node socketio-client.js ping 4
</code>```

To test throughput, dumping messages as fast as the server can.

```<code> node socketio-client.js dump 100000
</code>```

To test a stream of 1000 messages with a 18ms delay between each which is about 50 message per second.

```<code> node socketio-client.js stream 1000 18
</code>```

On my dev machine I can dump about 40000 messages per second to a single localhost client with the 4 integers as a payload (counter + 3 timestamps) on a 2 GHz CPU. Both server and client node processes use 95-100% of a CPU core each. So pure throughput looks ok.

I can emit 100 messages per second to 100 local clients at 55% CPU usage on the server process.

I can't get more than 130-140 messages per second to 100 clients out of a single node process on my dev machine.

A new, high frequency Intel Skylake CPU server might demolish those numbers locally. Add a, possibly flakey, network connection in and it will bring it right back down. Anything other than local network latency is probably going to mess with what you perceive you will gain with such high message rates. Latency jitter on anything slower will play havoc with the "frame rate" of the messages on the client end. Timestamping messages and tracking them on the client would probably be required.

If you do run into problems there are also lower level websocket libraries like ws that will require more implementation from you but will give you more control over socket connections and you can probably eek more performance out of them.

The more connections you have the more contention you will get with the rest of your code and socket code. You will probably end up needing to use multiple nodes to keep thing smooth. Cluster can split the app across multiple Node.js processes. You may need something like Redis, ZeroMQ or Nanomsg to manage IPC. V8 in Node 9 supports SharedArrayBuffer and Atomics but not much has landed in Node yet to use them with workers.

Body:
That's a very system, network and code dependent question.

Here is a small test harness I've used for similar plain socket.io testing before, I've plugged in some bits to fit your question.

Server

const io = require('socket.io')(8082)
const connections = []

io.on('connection', function(socket){

  connections.push(socket);
  const slog = (msg, ...args) => console.log('%s %s '+msg, Date.now(), socket.id, ...args)
  slog('Client connected. Total: %s', connections.length)

  socket.on('disconnect', function(data){
    connections.splice(connections.indexOf(socket), 1);
    slog('Client disconnected. Total: %s', connections.length)
  })

  socket.on('single', function(data){
    socket.emit('single',[ 0, now, now, now ])
  })

  socket.on('start', function(data = {}){
    slog('Start stream', data)
    sendBatch(1, data.count, data.delay)
  })

  socket.on('start dump', function(data = {}){
    slog('Start dump', data)
    sendBatch(1, data.count)
  })

  function sendBatch(i, max, delay){
    if ( i > max ) return slog('Done batch %s %s', max, delay)
    socket.emit('batch',[ i, now, now, now ])
    if (delay) {
      setTimeout(()=> sendBatch(i++, max, delay), delay)
    } else {
      setImmediate(()=> sendBatch(i++, max))
    }
  }

})

Client

const io = require('socket.io-client')
const socket = io('http://localhost:8082', {transports: ['websocket']})

socket.on('connect_error', err => console.error('Socket connect error:', err))
socket.on('connect_timeout', err => console.error('Socket connect timeout:', err))
socket.on('reconnect', err => console.error('Socket reconnect:', err))
socket.on('reconnect_attempt', err => console.error('Socket reconnect attempt:', err))
socket.on('reconnecting', err => console.error('Socket reconnecting', err))
socket.on('reconnect_error', err => console.error('Socket reconnect error:', err))
socket.on('reconnect_failed', err => console.error('Socket reconnect failed:', err))

function batch(n){
  socket.on('batch', function(data){
    if ( data[0] >= n ) {
      let end = Date.now()
      let persec = n / (( end - start ) / 1000)
      console.log('Took %s ms for %s at %s/s', end - start, n, persec.toFixed(1))
      return socket.close()
    }
  })
}

function startDump(count = 500000){
  socket.emit('start dump', { count: count })
  console.log('Start dump', count)
  batch(count)
}
function startStream(count = 50, delay = 1000){
  socket.emit('start', { count: count, delay: delay })
  console.log('Start stream', count, delay)
  batch(count)
}

function pingIt(i, max = 50){
  socket.on('single', function(data){
    console.log('Got a single with:', data)
    if (i >= max) {
      let end = Date.now()
      let persec = i / (end - start) * 1000
      console.log('Took %s ms %s/s', end - start, persec.toFixed(2))
      return socket.close()
    }
    socket.emit('single', i+=1)
  })
  socket.emit('single', i)
}

let start = Date.now()

//console.log('args command: %s  count: %s  delay: %s',process.argv[2], process.argv[3], process.argv[4])
switch(process.argv[2]){
  case 'ping':   pingIt(0, process.argv[3]); break
  case 'stream': startStream(process.argv[3], process.argv[4]); break
  case 'dump':   startDump(process.argv[3]); break
  default:       console.log('ping stream dump'); socket.close()
}

To test request/response round trips

 node socketio-client.js ping 4

To test throughput, dumping messages as fast as the server can.

 node socketio-client.js dump 100000

To test a stream of 1000 messages with a 18ms delay between each which is about 50 message per second.

 node socketio-client.js stream 1000 18

On my dev machine I can dump about 40000 messages per second to a single localhost client with the 4 integers as a payload (counter + 3 timestamps) on a 2 GHz CPU. Both server and client node processes use 95-100% of a CPU core each. So pure throughput looks ok.

I can emit 100 messages per second to 100 local clients at 55% CPU usage on the server process.

I can't get more than 130-140 messages per second to 100 clients out of a single node process on my dev machine.

A new, high frequency Intel Skylake CPU server might demolish those numbers locally. Add a, possibly flakey, network connection in and it will bring it right back down. Anything other than local network latency is probably going to mess with what you perceive you will gain with such high message rates. Latency jitter on anything slower will play havoc with the "frame rate" of the messages on the client end. Timestamping messages and tracking them on the client would probably be required.

If you do run into problems there are also lower level websocket libraries like ws that will require more implementation from you but will give you more control over socket connections and you can probably eek more performance out of them.

The more connections you have the more contention you will get with the rest of your code and socket code. You will probably end up needing to use multiple nodes to keep thing smooth. Cluster can split the app across multiple Node.js processes. You may need something like Redis, ZeroMQ or Nanomsg to manage IPC. V8 in Node 9 supports SharedArrayBuffer and Atomics but not much has landed in Node yet to use them with workers.