问:
Azure DevOps run unit test cases in dev environment
I have few unit test cases which run on dev/test/db server. dev/test server are internal servers and behind firewall. I have added these servers in deployment groups in Azure DEVOPS.

I am getting error "remote server is not accessible" when trying to run test cases from Azure Devops. How the azure devops is connecting to remote server which is behind firewall and deploying code in release ? and what i need to do to  execute these test cases to remote server.

答:
Unit tests are not supposed to access the database. Database is considered an external dependency and should be mocked. Best case scenario, if using some tool like Enity Framework you could use the support for an in-memory database for testing purposes. But even the test/in-memory database does not provide full feature support.

Deployment groups is a way to optimize deployment by installing an agent on the server it self and by the tag you assign you limit deployment to specific servers. They are not setting any firewall rules that on the target server

The tests that you try execute are more of integration/regression (I know there is a lot of discussion on test naming). You need a proper test environment to execute these type of tests.

Every time you get a build agent (imagine it like roughly your "vm") and you run your tests it will try to access your database.

If you need (for specific reasons and I do not suggest that at all) to be quick and dirty you could disable the firewall rules in the test db.

Another, slightly better solution would be, if you have Self-hosted agents you can whitelist server's IP


来源: stackexchange

问:
remove duplicates in python
I'm looking to remove the names that are repeated.

My code is connected to retrieve information from SQL server to Python.

```<code>def get_all_artist():
    query="SELECT artist_name FROM Sheet1"
    all_artist = execute_read_query (conn, query)
    for artist_record in all_artist:
        print(str(artist_record[0]))
    return (all_artist)
</code>```

The artist_name that I am retrieving on SQL are:

```<code>BTS
BTS
BTS
TWICE
TWICE
TWICE
TWICE
TWICE
HEIZE
HEIZE
KHALID
KHALID
KHALID
ERIC CHOU
ERIC CHOU
ERIC CHOU
SAM SMITH
SAM SMITH
SAM SMITH
AGUST D
</code>```

However, I'd only like to remove the duplicates on Python without removing any rows in my SQL table:

```<code>BTS
TWICE
HEIZE
KHALID
ERIC CHOU
SAM SMITH
AGUST D
</code>```

答:
Assuming this is a SQL query, the statement for removing repetition is DISTINCT:

```<code>query="SELECT DISTINCT artist_name FROM Sheet1"
</code>```

ContentLicense: CC BY-SA 4.0

Title: remove duplicates in python

Body:
I'm looking to remove the names that are repeated.

My code is connected to retrieve information from SQL server to Python.

def get_all_artist():
    query="SELECT artist_name FROM Sheet1"
    all_artist = execute_read_query (conn, query)
    for artist_record in all_artist:
        print(str(artist_record[0]))
    return (all_artist)

The artist_name that I am retrieving on SQL are:

BTS
BTS
BTS
TWICE
TWICE
TWICE
TWICE
TWICE
HEIZE
HEIZE
KHALID
KHALID
KHALID
ERIC CHOU
ERIC CHOU
ERIC CHOU
SAM SMITH
SAM SMITH
SAM SMITH
AGUST D

However, I'd only like to remove the duplicates on Python without removing any rows in my SQL table:

BTS
TWICE
HEIZE
KHALID
ERIC CHOU
SAM SMITH
AGUST D

Body:
Assuming this is a SQL query, the statement for removing repetition is DISTINCT:

query="SELECT DISTINCT artist_name FROM Sheet1"


问:
Example Thread Java
Why does not work the first example? while the second one with thread works? And I don't understand what is the use of the sleep. It is a counter that start after press the start button . If I press the INC-DEC button it must change the counter trend. So it increases or decreases the counter.

Contatore.java without thread

```<code>public class Contatore extends JFrame {
    private int count;
    private boolean runFlag;
    private JButton onOff;
    private JButton start;
    public Contatore() {
        runFlag = true;
        count = 0;
        setLayout(new FlowLayout());
        onOff = new JButton("INCR-DECR");
        add(onOff);
        onOff.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                runFlag = !runFlag;
            }
        });
        start = new JButton("START");
        add(start);
        start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                start.setEnabled(false);
                while (true) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        System.out.println(e.getMessage());
                    }(runFlag) count++;
                    else count--;
                    System.out.println(count);
                }
            }
        });
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }
    public static void main(String args[]) {
        Contatore f = new Contatore();
    }
}
</code>```

ContatoreRisolto.java with Thread

```<code>public class ContatoreRisolto extends JFrame {
    private boolean runFlag;
    private JButton onOff;
    private JButton start;
    public ContatoreRisolto() {
        runFlag = true;
        setLayout(new FlowLayout());
        onOff = new JButton("INCR-DECR");
        add(onOff);
        onOff.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                runFlag = !runFlag;
            }
        });
        start = new JButton("START");
        add(start);
        start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                start.setEnabled(false);
                Counter c = new Counter();
                c.start();
            }
        });
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }
    public static void main(String args[]) {
        ContatoreRisolto f = new ContatoreRisolto();
    }
    class Counter extends Thread {
        public void run() {
            int count = 0;
            while (true) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException exc) {
                    System.out.println(e.getMessage());
                }
                if (runFlag) count++;
                else count--;
                System.out.println(count);
            }
        }
    }
}
</code>```

答:
The Thread.sleep(500) Method completely stops the main program thread for 500 milliseconds. Because you are doing everything in a single thread in the first example, the program just freezes because you're calling Thread.sleep(500) repeatedly.

In the second example, the main thread keeps running and your Counter thread sleeps. Therefore your program will not freeze and everything works.

Title: Example Thread Java

Body:
Why does not work the first example? while the second one with thread works? And I don't understand what is the use of the sleep. It is a counter that start after press the start button . If I press the INC-DEC button it must change the counter trend. So it increases or decreases the counter.

Contatore.java without thread

public class Contatore extends JFrame {
    private int count;
    private boolean runFlag;
    private JButton onOff;
    private JButton start;
    public Contatore() {
        runFlag = true;
        count = 0;
        setLayout(new FlowLayout());
        onOff = new JButton("INCR-DECR");
        add(onOff);
        onOff.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                runFlag = !runFlag;
            }
        });
        start = new JButton("START");
        add(start);
        start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                start.setEnabled(false);
                while (true) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        System.out.println(e.getMessage());
                    }(runFlag) count++;
                    else count--;
                    System.out.println(count);
                }
            }
        });
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }
    public static void main(String args[]) {
        Contatore f = new Contatore();
    }
}

ContatoreRisolto.java with Thread

public class ContatoreRisolto extends JFrame {
    private boolean runFlag;
    private JButton onOff;
    private JButton start;
    public ContatoreRisolto() {
        runFlag = true;
        setLayout(new FlowLayout());
        onOff = new JButton("INCR-DECR");
        add(onOff);
        onOff.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                runFlag = !runFlag;
            }
        });
        start = new JButton("START");
        add(start);
        start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                start.setEnabled(false);
                Counter c = new Counter();
                c.start();
            }
        });
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }
    public static void main(String args[]) {
        ContatoreRisolto f = new ContatoreRisolto();
    }
    class Counter extends Thread {
        public void run() {
            int count = 0;
            while (true) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException exc) {
                    System.out.println(e.getMessage());
                }
                if (runFlag) count++;
                else count--;
                System.out.println(count);
            }
        }
    }
}


问:
SVG pixelation in chrome
This is very strange. I am using SVG images, because of the low file size, sharp rendering, and scalability ( the objects animate quite a bit ). Its working perfect in FF, ie9, Safari and iPad, but in chrome certain SVG images are rendering very poorly.

Any ideas why this might be happening? The svg files themselves are very small.

Here is some a sample svg

答:
The problem is as the graphic becomes smaller there are less pixels to work with. When rendering the SVG the browser is using equations to determine pixels but the equations result in numbers that fall in between pixels.

It seems the solution is to set the desired size of your SVG in your editing program and then make certain all of your pixels align to the grid. This will still allow your SVG to scale up but will also allow it to render at the smaller size.

Credits to: https://salferrarello.com/svg-looks-pixelated-when-small/#:~:text=Solution,render%20at%20the%20smaller%20size.

Title: SVG pixelation in chrome


问:
Codeigniter create thumb image by custom dimensions, how?
I have Codeigniter custom system, need upload image standard dimensions + create _thumb into same folder without duplicate. This is working code without thumb image creator... Can you help me?</p>
```<code>    public function image($var, $id) {
    $config['upload_path'] = './cdn/' . $this->module;
    $config['allowed_types'] = 'gif|jpg|png|jpeg|webp';
    $this->load->library('upload', $config);
    if (!$this->upload->do_upload('featured_image')) {

    } else {
        $data = $this->upload->data();
        if ($data['file_name'])
            $this->{$this->model}->featured_image = $data['file_name'];
    }
    return true;
}

public function image_upload() {
    $config['upload_path'] = './cdn/' . $this->module;
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    $this->load->library('upload', $config);
    $errors = 0;
    $error_message = '';
    $passed_files = [];
    $var = 'file';
    $files = $this->reArrayFiles(@$_FILES[$var]);
    foreach ($files as $idx => $file) {
        $_FILES[$var] = $files[$idx];
        if ($this->upload->do_upload($var)) {
            $data = $this->upload->data();
            if ($data['file_name']) {
                $ext = pathinfo($data['file_name'], PATHINFO_EXTENSION);
                $file_name = "IRecipes_" . str_replace([".", " "], "", microtime()) . rand(0, 100) . "_orig." . $ext;
                rename($data["full_path"], $data["file_path"] . $file_name);
                $passed_files[] = $file_name;
            }
        } else {
            $errors++;
            $error_message .= strip_tags($this->upload->display_errors());
        }
    }
    if ($passed_files && count($passed_files))
        $this->uploadFile[$var] = ($passed_files);

    if ($errors == count($files))
        die(json_encode(["error" => true, "message" => $error_message]));
    else
        die(json_encode(["error" => false, "files" => $passed_files]));
}
</code>```

答:
You basically need to install ImageMagick on the server/localhost.

You can also check this link for a working example on how to create the thumbnail.

Body:
I have Codeigniter custom system, need upload image standard dimensions + create _thumb into same folder without duplicate. This is working code without thumb image creator... Can you help me?

    public function image($var, $id) {
    $config['upload_path'] = './cdn/' . $this->module;
    $config['allowed_types'] = 'gif|jpg|png|jpeg|webp';
    $this->load->library('upload', $config);
    if (!$this->upload->do_upload('featured_image')) {

    } else {
        $data = $this->upload->data();
        if ($data['file_name'])
            $this->{$this->model}->featured_image = $data['file_name'];
    }
    return true;
}

public function image_upload() {
    $config['upload_path'] = './cdn/' . $this->module;
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    $this->load->library('upload', $config);
    $errors = 0;
    $error_message = '';
    $passed_files = [];
    $var = 'file';
    $files = $this->reArrayFiles(@$_FILES[$var]);
    foreach ($files as $idx => $file) {
        $_FILES[$var] = $files[$idx];
        if ($this->upload->do_upload($var)) {
            $data = $this->upload->data();
            if ($data['file_name']) {
                $ext = pathinfo($data['file_name'], PATHINFO_EXTENSION);
                $file_name = "IRecipes_" . str_replace([".", " "], "", microtime()) . rand(0, 100) . "_orig." . $ext;
                rename($data["full_path"], $data["file_path"] . $file_name);
                $passed_files[] = $file_name;
            }
        } else {
            $errors++;
            $error_message .= strip_tags($this->upload->display_errors());
        }
    }
    if ($passed_files && count($passed_files))
        $this->uploadFile[$var] = ($passed_files);

    if ($errors == count($files))
        die(json_encode(["error" => true, "message" => $error_message]));
    else
        die(json_encode(["error" => false, "files" => $passed_files]));
}


问:
Why is the change from HTTP to HTTPS in View Settings within Google Analytics important?
I have an HTTPS site which runs on SSL since years, also the Google Search Console is configured with HTTPS. If you lookup a page of that site through HTTP, a redirect to HTTPS will take place.

Now I found out, that within Google Analytics > View Settings, the property Website's URL ist still configured with HTTP (in that dropdown menu there). But in Google Analytics > Property Settings, it's correctly configured with HTTPS.

As I don't want to create a negative impact on any data collected within the last 10 years, would the property change have an impact on the data? Why is it important that this property is actually on the currently used protocol? Because data is collected, everything is actually there, so why should I need to change it?

Thanks

答: That setting has no impact on data. It doesn't change automatically, you have to be the one to set it. It is used, for example, to preview the page by clicking on the icon in the page report.</p>


问:
How to checkout and reset using smartgit?
Need help in understanding the below

1). How do i check out to a particular commit and again revert back to the recent commit ?

Tried branch->checkout->same branch and check out to the previous commit, but when i did this the commits made after the one i checked out now got lost !

Its not in the list to checkout to recent commit !

2). How do i reset my head to a particular commit ?

答:
(1) Usually, branch refs won't be changed by a Check Out, so you may use Check Out again to switch back to your branch: simply select the corresponding branch for "Showing Branches" selector.

If the branch ref to your old head is actually lost, you can open the Log and use Query|Load All Commits to display all such heads, then add a temporary branch ref to the corresponding head and check out this head.

(2) Local|Reset should work for you.

ContentLicense: CC BY-SA 3.0


问:
How to fix waitForSyncReply time-out with Meteor/Cordova iOS app?
We have a Meteor/Cordova app. Development works fine, except when deploying to an actual iPhone device. More often than not the app freezes after a couple of actions, resulting in the following logs in XCode:

```<code>2016-12-15 13:12:43.546614 museumexplorer[738:169260] #WK: Connection::waitForSyncReply: Timed-out while waiting for reply, id = 35
2016-12-15 13:12:44.554481 museumexplorer[738:169260] #WK: Connection::waitForSyncReply: Timed-out while waiting for reply, id = 36
2016-12-15 13:12:45.559667 museumexplorer[738:169260] #WK: Connection::waitForSyncReply: Timed-out while waiting for reply, id = 37
2016-12-15 13:13:06.239664 museumexplorer[738:169260] #WK: Connection::waitForSyncReply: Timed-out while waiting for reply, id = 38
2016-12-15 13:13:07.248334 museumexplorer[738:169260] #WK: Connection::waitForSyncReply: Timed-out while waiting for reply, id = 39
2016-12-15 13:13:08.260760 museumexplorer[738:169260] #WK: Connection::waitForSyncReply: Timed-out while waiting for reply, id = 40
</code>```

I found it has something to do with the beacons functionality. If I comment out the following code, the freezes don't occur.

```<code>cordova.plugins.locationManager.startRangingBeaconsInRegion(beaconRegion)
    .fail(console.error)
    .done();
</code>```

I'll keep investigating. But if anyone has any bright ideas, I'd love to hear it!

Regards,
Chris

答: It might be a hidden bug in WebKit, according to this WebKit Bugzilla</p>

Body:
We have a Meteor/Cordova app. Development works fine, except when deploying to an actual iPhone device. More often than not the app freezes after a couple of actions, resulting in the following logs in XCode:

2016-12-15 13:12:43.546614 museumexplorer[738:169260] #WK: Connection::waitForSyncReply: Timed-out while waiting for reply, id = 35
2016-12-15 13:12:44.554481 museumexplorer[738:169260] #WK: Connection::waitForSyncReply: Timed-out while waiting for reply, id = 36
2016-12-15 13:12:45.559667 museumexplorer[738:169260] #WK: Connection::waitForSyncReply: Timed-out while waiting for reply, id = 37
2016-12-15 13:13:06.239664 museumexplorer[738:169260] #WK: Connection::waitForSyncReply: Timed-out while waiting for reply, id = 38
2016-12-15 13:13:07.248334 museumexplorer[738:169260] #WK: Connection::waitForSyncReply: Timed-out while waiting for reply, id = 39
2016-12-15 13:13:08.260760 museumexplorer[738:169260] #WK: Connection::waitForSyncReply: Timed-out while waiting for reply, id = 40

I found it has something to do with the beacons functionality. If I comment out the following code, the freezes don't occur.

cordova.plugins.locationManager.startRangingBeaconsInRegion(beaconRegion)
    .fail(console.error)
    .done();

I'll keep investigating. But if anyone has any bright ideas, I'd love to hear it!

Regards,
Chris


问:
how do i view an active storage uploaded image from my index file, this error comes up
<%= image_tag (complaint.image) %>
This line breeds this error,
Can't resolve image into URL: to_model delegated to attachment, but the attachment is nil
 Without the image_tag, I get an active_storage object; # , how then do I get past this</p>

答:
Just check if its nil before displaying the image.

```<code><%= image_tag(complaint.image) if complaint.image %>
</code>```

Also do NOT put a space between the method name and the parens in Ruby. Since parens are optional in Ruby it will be treated as a single argument and not a list of arguments.

```<code>foo (bar, baz) # causes a syntax error
</code>```

Body:
Just check if its nil before displaying the image.

<%= image_tag(complaint.image) if complaint.image %>

Also do NOT put a space between the method name and the parens in Ruby. Since parens are optional in Ruby it will be treated as a single argument and not a list of arguments.

foo (bar, baz) # causes a syntax error


问:
Getting java.lang.ClassNotFoundException: com.sun.xml.internal.ws.spi.ProviderImpl despite the dependencies are defined
Despite that I have defined the related dependencies as I have added below, getting the java.lang.ClassNotFoundException: com.sun.xml.internal.ws.spi.ProviderImpl exception when my app makes a call to the web service.

```<code><dependency>
  <groupId>javax.xml.ws</groupId>
  <artifactId>jaxws-api</artifactId>
  <version>2.2.10</version>
</dependency>

<dependency>
  <groupId>com.sun.xml.ws</groupId>
  <artifactId>jaxws-rt</artifactId>
  <version>2.2.10</version>
  <type>pom</type>
</dependency>
</code>```

p.s. The servlet container is Apache Tomcat 9.0.4.

p.s. Java version: 9.0.1.

答:
Today in the era of Jakarta, I needed the following two dependencies:

```<code>        <dependency>
            <groupId>jakarta.xml.ws</groupId>
            <artifactId>jakarta.xml.ws-api</artifactId>
            <version>2.3.3</version>
        </dependency>

        <dependency>
            <groupId>com.sun.xml.ws</groupId>
            <artifactId>jaxws-rt</artifactId>
            <version>2.3.3</version>
        </dependency>
</code>```

One thing I find quite weird is that the second dependency is not from Jakarta, I thought all of these implementations were migrated. It works, but I'd appreciate if someone could comment on that.

Body:
Despite that I have defined the related dependencies as I have added below, getting the java.lang.ClassNotFoundException: com.sun.xml.internal.ws.spi.ProviderImpl exception when my app makes a call to the web service.

<dependency>
  <groupId>javax.xml.ws</groupId>
  <artifactId>jaxws-api</artifactId>
  <version>2.2.10</version>
</dependency>

<dependency>
  <groupId>com.sun.xml.ws</groupId>
  <artifactId>jaxws-rt</artifactId>
  <version>2.2.10</version>
  <type>pom</type>
</dependency>

p.s. The servlet container is Apache Tomcat 9.0.4.

p.s. Java version: 9.0.1.

Body:
Today in the era of Jakarta, I needed the following two dependencies:

        <dependency>
            <groupId>jakarta.xml.ws</groupId>
            <artifactId>jakarta.xml.ws-api</artifactId>
            <version>2.3.3</version>
        </dependency>

        <dependency>
            <groupId>com.sun.xml.ws</groupId>
            <artifactId>jaxws-rt</artifactId>
            <version>2.3.3</version>
        </dependency>

One thing I find quite weird is that the second dependency is not from Jakarta, I thought all of these implementations were migrated. It works, but I'd appreciate if someone could comment on that.


问:
When you discard the reference to a SoftReference, will it's clearing still be put on the ReferenceQueue?
I want to understand under which conditions the garbage collector notifies you of clearing a soft reference. The documentation makes it clear it happens when an OOM might occur, but what if you discard the reference completely, GC will come around and collect the SoftReference might notice no other strong/softly references exists and clear the internal referred value.

ReferenceQueue<Object> cleared = new ReferenceQueue<>();
Object X = new Object();
SoftReference<Object> Xs = new SoftReference<>(X, cleared);

Thread.sleep(10);
X = null;
Xs = null;

System.GC();
Thread.sleep(100);
Reference ref;
while ((ref = cleared.poll()) != null) {
   System.err.println(ref);
}
```

So where are the rules around this queue documentated? There must be more corner cases than what I'm asking, so maybe the question has to be broadened a bit.

答:
The relevant point can be found in the package documentation in the Notification section:

The relationship between a registered reference object and its queue is one-sided. That is, a queue does not keep track of the references that are registered with it. If a registered reference becomes unreachable itself, then it will never be enqueued. It is the responsibility of the program using reference objects to ensure that the objects remain reachable for as long as the program is interested in their referents.

So the answer is simple. Since you set the sole variable referencing your SoftReference to null, it will never be enqueued. Whether it will get collected or cleared, doesn’t matter, as you are unable to perceive it. In practice, typical implementations will behave as Eugene wrote, the garbage collector only traverses live references and does not care about what kind of garbage exists within the remaining memory.

Body:
I want to understand under which conditions the garbage collector notifies you of clearing a soft reference. The documentation makes it clear it happens when an OOM might occur, but what if you discard the reference completely, GC will come around and collect the SoftReference might notice no other strong/softly references exists and clear the internal referred value.

ReferenceQueue<Object> cleared = new ReferenceQueue<>();
Object X = new Object();
SoftReference<Object> Xs = new SoftReference<>(X, cleared);

Thread.sleep(10);
X = null;
Xs = null;

System.GC();
Thread.sleep(100);
Reference ref;
while ((ref = cleared.poll()) != null) {
   System.err.println(ref);
}

So where are the rules around this queue documentated? There must be more corner cases than what I'm asking, so maybe the question has to be broadened a bit.


问:
Can I use an Entity's ID in equals/hashCode with fallback to instance equality?
I'm trying to figure out what's wrong with this approach, given my particular usage patterns:

```<code>@Entity
public class DomainObject {
  @Id // + sequence generator
  private Long id;

  @Override
  public boolean equals(Object o) {
    // bunch of other checks omitted for clarity
    if (id != null) {
      return id.equals(o.getId());
     }
     return super.equals(o);
  }

  @Override
  public int hashCode() {
    if (id != null) {
      return id.hashCode();
    }
    return super.hashCode();
}
</code>```

I've read several posts on the subject and it sounds like you don't want to use a DB-generated sequence value in equals/hashCode because they won't be set until the objects are persisted and you don't want disparate transient instances to all be equal, or the persistence layer itself might break.

But is there anything wrong with falling back to the default Object equals/hashCode (instance equality) for transient objects and then using the generated @Id when you have it?

The worst thing I can think of is, a transient object can't ever be equal to a persistent object, which is fine in my use case - the only time I'm ever putting objects in collections and want contains to work, all the objects are already persistent and all have IDs.

However, I feel like there's something else wrong in a really subtle, non-obvious way deep in the persistence layer but I can't quite figure out what.

The other options don't seem that appealing either:

doing nothing and living with instance equality (default Object.equals): works great for most of my entities, but getting tired of workarounds for the handful of cases when I want a collection with a mix of detached entities (e.g., session scope) and "live" ones from current transaction

using a business key: I have clear natural keys but they're mutable, and this would have
some of the same problems as above (hashCode stability if object changes)

using a UUID - I know that will work, but feels wrong to pollute the DB with artifacts to support java.util collections.

See also:

The JPA hashCode() / equals() dilemma

Should the id field of a JPA entity be considered in equals and hashCode?.

答:
If you're sure you don't ever need to add an unpersisted entity to a Set or Map key, you can use the ID to test for equality and as the hash code. But if you do this, you could enforce it by throwing an Exception for an unpersisted object:

```<code>@Entity
public class DomainObject {
  @Id // + sequence generator
  private Long id;

  @Override
  public boolean equals(Object that) {
    // bunch of other checks omitted for clarity
    if (id != null) {
      throw new IllegalStateException("equals() before persisting");
    }
    if (this == that) {
      return true;
    }
    if (that instanceof DomainObject) {
      return id.equals(((DomainObject)that).id);
    }

  }

  @Override
  public int hashCode() {
    if (id != null) {
      throw new IllegalStateException("hashCode() before persisting");
    }
    return id;
  }
}
</code>```

If you do this, you may see surprise exceptions, where you didn't realize you were relying on these methods on unpersisted objects. You may find this helpful in debugging. You might also find it makes your existing code unusable. Either way, you'll be clearer about how your code works.

One thing you should never do is return a constant for the hash code.

```<code>public int hashCode() { return 5; } // Don't ever do this!
</code>```

Technically, it fulfills the contract, but it's a terrible implementation. Just read the javadocs for Object.hashCode(): …producing distinct integer results for unequal objects may improve the performance of hash tables. (The word "may" here is a serious understatement.)

Body:
I'm trying to figure out what's wrong with this approach, given my particular usage patterns:

@Entity
public class DomainObject {
  @Id // + sequence generator
  private Long id;

  @Override
  public boolean equals(Object o) {
    // bunch of other checks omitted for clarity
    if (id != null) {
      return id.equals(o.getId());
     }
     return super.equals(o);
  }

  @Override
  public int hashCode() {
    if (id != null) {
      return id.hashCode();
    }
    return super.hashCode();
}

I've read several posts on the subject and it sounds like you don't want to use a DB-generated sequence value in equals/hashCode because they won't be set until the objects are persisted and you don't want disparate transient instances to all be equal, or the persistence layer itself might break.

But is there anything wrong with falling back to the default Object equals/hashCode (instance equality) for transient objects and then using the generated @Id when you have it?

The worst thing I can think of is, a transient object can't ever be equal to a persistent object, which is fine in my use case - the only time I'm ever putting objects in collections and want contains to work, all the objects are already persistent and all have IDs.

However, I feel like there's something else wrong in a really subtle, non-obvious way deep in the persistence layer but I can't quite figure out what.

The other options don't seem that appealing either:

doing nothing and living with instance equality (default Object.equals): works great for most of my entities, but getting tired of workarounds for the handful of cases when I want a collection with a mix of detached entities (e.g., session scope) and "live" ones from current transaction

using a business key: I have clear natural keys but they're mutable, and this would have
some of the same problems as above (hashCode stability if object changes)

using a UUID - I know that will work, but feels wrong to pollute the DB with artifacts to support java.util collections.

See also:

The JPA hashCode() / equals() dilemma

Should the id field of a JPA entity be considered in equals and hashCode?.

Body:
If you're sure you don't ever need to add an unpersisted entity to a Set or Map key, you can use the ID to test for equality and as the hash code. But if you do this, you could enforce it by throwing an Exception for an unpersisted object:

@Entity
public class DomainObject {
  @Id // + sequence generator
  private Long id;

  @Override
  public boolean equals(Object that) {
    // bunch of other checks omitted for clarity
    if (id != null) {
      throw new IllegalStateException("equals() before persisting");
    }
    if (this == that) {
      return true;
    }
    if (that instanceof DomainObject) {
      return id.equals(((DomainObject)that).id);
    }

  }

  @Override
  public int hashCode() {
    if (id != null) {
      throw new IllegalStateException("hashCode() before persisting");
    }
    return id;
  }
}

If you do this, you may see surprise exceptions, where you didn't realize you were relying on these methods on unpersisted objects. You may find this helpful in debugging. You might also find it makes your existing code unusable. Either way, you'll be clearer about how your code works.

One thing you should never do is return a constant for the hash code.

public int hashCode() { return 5; } // Don't ever do this!

Technically, it fulfills the contract, but it's a terrible implementation. Just read the javadocs for Object.hashCode(): …producing distinct integer results for unequal objects may improve the performance of hash tables. (The word "may" here is a serious understatement.)


问:
How to wait for onPaint to finish painting (C#)
Hi based on the thread here: How to create a jpg image dynamically in memory with .NET?

I have this method:

```<code>        int maxVal = 50;
        int maxXCells = r.Next(maxVal);
        int maxYCells = r.Next(maxVal);
        int cellXPosition = r.Next(maxVal);
        int cellYPosition = r.Next(maxVal);
        int boxSize = 10;

        Graphics fg = this.CreateGraphics();

        using (var bmp = new System.Drawing.Bitmap(maxXCells * boxSize + 1, maxYCells * boxSize + 1))
        {

            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.Clear(Color.Yellow);
                Pen pen = new Pen(Color.Black);
                pen.Width = 1;

                //Draw red rectangle to go behind cross
                Rectangle rect = new Rectangle(boxSize * (cellXPosition - 1), boxSize * (cellYPosition - 1), boxSize, boxSize);
                g.FillRectangle(new SolidBrush(Color.Red), rect);

                //Draw cross
                g.DrawLine(pen, boxSize * (cellXPosition - 1), boxSize * (cellYPosition - 1), boxSize * cellXPosition, boxSize * cellYPosition);
                g.DrawLine(pen, boxSize * (cellXPosition - 1), boxSize * cellYPosition, boxSize * cellXPosition, boxSize * (cellYPosition - 1));

                //Draw horizontal lines
                for (int i = 0; i <= maxXCells; i++)
                {
                    g.DrawLine(pen, (i * boxSize), 0, i * boxSize, boxSize * maxYCells);
                }

                //Draw vertical lines
                for (int i = 0; i <= maxYCells; i++)
                {
                    g.DrawLine(pen, 0, (i * boxSize), boxSize * maxXCells, i * boxSize);
                }
            }
            fg.DrawImage(bmp, 0, 0);
            fg.Dispose();

        }
</code>```

It fires based on the random event (can be many times in a second). What should I do, to  draw a new image only after current drawing has finished? At the moment, I can seen, that if the event to run this method fires faster than drawing has finished, the screen flickers. What are the common solution to avoiding drawing before previous drawing has finished?

This what I included in OnPaint method:

```<code>   protected override void OnPaint(System.Windows.Forms.PaintEventArgs pe)
    {
        finishedInvalidating = false;
        fg = this.CreateGraphics();
        lock (bmp)
        {
            fg.DrawImage(bmp, 0, 0);
        }
        fg.Dispose();
        finishedInvalidating = true;
</code>```

But this does not solve the problem

---------------------- update ------------------

```<code> public partial class LadderFrm : Form
{
    Bitmap bmp;

    int numCols = 3;
    int colWidth = 100;
    int numRows = 30;
    int rowHeight = 20;
    bool finishedInvalidating = false;

    decimal lastprice;

    public LadderFrm()
    {
        bmp = new System.Drawing.Bitmap(numCols * colWidth + 1, numRows * rowHeight + 1);
        prepareLadderGraphics();
    }

    // prepare initial ladder background
    private void prepareLadderGraphics()
    {

        using (Graphics g = Graphics.FromImage(bmp))
        {
            g.Clear(Color.LightGray);
            Pen pen = new Pen(Color.Black);
            pen.Width = 1;

            // drawCells
            for (int i = 0; i < numCols; i++)
            {
                for (int j = 0; j < numRows; j++)
                {
                    Rectangle rect = new Rectangle(i * colWidth, j * rowHeight, colWidth, rowHeight);
                    g.DrawRectangle(pen, rect);
                }
            }
            g.Dispose();
        }

    }

    protected override void OnPaint(System.Windows.Forms.PaintEventArgs pe)
    {
        Graphics fg = this.CreateGraphics();
        lock (fg)
        {
            lock (bmp)
            {
                fg.DrawImage(bmp, 0, 0);
            }
        }
        fg.Dispose();
    }

    public void OrderBookUpdateFn(OrderBookEvent orderBookEvent)
    {

        if (lastprice != orderBookEvent.ValuationAskPrice)
        {
            lastprice = orderBookEvent.ValuationAskPrice;
            lock (bmp)
            {
                using (Graphics g = Graphics.FromImage(bmp))
                {
                    Pen pen = new Pen(Color.Black);
                    pen.Width = 1;
                    for (int i = 0; i < numRows; i++)
                    {
                        Rectangle rect = new Rectangle(colWidth + 1, i*rowHeight + 1, colWidth - 1, rowHeight - 1);
                        g.FillRectangle(new SolidBrush(Color.LightGray), rect);
                        g.DrawString(lastPrice.ToString(), new Font(FontFamily.GenericSansSerif, 10), new SolidBrush(Color.Black), new Point(colWidth + 1, i*rowHeight + 1));
                    }
                    g.Dispose();
                }
                this.Invalidate(new Rectangle(0, 0, 1, 1));
            }
    }
}
</code>```

Above code works, pretty well, however I am not sure if its correct. This is just for test purpose. Method OrderBookUpdateFn(OrderBookEvent orderBookEvent) gets fired very often (sometimes tens of times a second).

答:
to avoid flickering of selfpainted controls you can DoubleBuffer the drawing of the form / control.
Add this to the constructor:

```<code>this.SetStyle(
  ControlStyles.AllPaintingInWmPaint |
  ControlStyles.UserPaint |
  ControlStyles.DoubleBuffer,true);
</code>```

some anti-flicker techniques

Body:
Hi based on the thread here: How to create a jpg image dynamically in memory with .NET?

I have this method:

        int maxVal = 50;
        int maxXCells = r.Next(maxVal);
        int maxYCells = r.Next(maxVal);
        int cellXPosition = r.Next(maxVal);
        int cellYPosition = r.Next(maxVal);
        int boxSize = 10;

        Graphics fg = this.CreateGraphics();

        using (var bmp = new System.Drawing.Bitmap(maxXCells * boxSize + 1, maxYCells * boxSize + 1))
        {

            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.Clear(Color.Yellow);
                Pen pen = new Pen(Color.Black);
                pen.Width = 1;

                //Draw red rectangle to go behind cross
                Rectangle rect = new Rectangle(boxSize * (cellXPosition - 1), boxSize * (cellYPosition - 1), boxSize, boxSize);
                g.FillRectangle(new SolidBrush(Color.Red), rect);

                //Draw cross
                g.DrawLine(pen, boxSize * (cellXPosition - 1), boxSize * (cellYPosition - 1), boxSize * cellXPosition, boxSize * cellYPosition);
                g.DrawLine(pen, boxSize * (cellXPosition - 1), boxSize * cellYPosition, boxSize * cellXPosition, boxSize * (cellYPosition - 1));

                //Draw horizontal lines
                for (int i = 0; i <= maxXCells; i++)
                {
                    g.DrawLine(pen, (i * boxSize), 0, i * boxSize, boxSize * maxYCells);
                }

                //Draw vertical lines
                for (int i = 0; i <= maxYCells; i++)
                {
                    g.DrawLine(pen, 0, (i * boxSize), boxSize * maxXCells, i * boxSize);
                }
            }
            fg.DrawImage(bmp, 0, 0);
            fg.Dispose();

        }

It fires based on the random event (can be many times in a second). What should I do, to  draw a new image only after current drawing has finished? At the moment, I can seen, that if the event to run this method fires faster than drawing has finished, the screen flickers. What are the common solution to avoiding drawing before previous drawing has finished?

This what I included in OnPaint method:

   protected override void OnPaint(System.Windows.Forms.PaintEventArgs pe)
    {
        finishedInvalidating = false;
        fg = this.CreateGraphics();
        lock (bmp)
        {
            fg.DrawImage(bmp, 0, 0);
        }
        fg.Dispose();
        finishedInvalidating = true;

But this does not solve the problem

---------------------- update ------------------

 public partial class LadderFrm : Form
{
    Bitmap bmp;

    int numCols = 3;
    int colWidth = 100;
    int numRows = 30;
    int rowHeight = 20;
    bool finishedInvalidating = false;

    decimal lastprice;

    public LadderFrm()
    {
        bmp = new System.Drawing.Bitmap(numCols * colWidth + 1, numRows * rowHeight + 1);
        prepareLadderGraphics();
    }

    // prepare initial ladder background
    private void prepareLadderGraphics()
    {

        using (Graphics g = Graphics.FromImage(bmp))
        {
            g.Clear(Color.LightGray);
            Pen pen = new Pen(Color.Black);
            pen.Width = 1;

            // drawCells
            for (int i = 0; i < numCols; i++)
            {
                for (int j = 0; j < numRows; j++)
                {
                    Rectangle rect = new Rectangle(i * colWidth, j * rowHeight, colWidth, rowHeight);
                    g.DrawRectangle(pen, rect);
                }
            }
            g.Dispose();
        }

    }

    protected override void OnPaint(System.Windows.Forms.PaintEventArgs pe)
    {
        Graphics fg = this.CreateGraphics();
        lock (fg)
        {
            lock (bmp)
            {
                fg.DrawImage(bmp, 0, 0);
            }
        }
        fg.Dispose();
    }

    public void OrderBookUpdateFn(OrderBookEvent orderBookEvent)
    {

        if (lastprice != orderBookEvent.ValuationAskPrice)
        {
            lastprice = orderBookEvent.ValuationAskPrice;
            lock (bmp)
            {
                using (Graphics g = Graphics.FromImage(bmp))
                {
                    Pen pen = new Pen(Color.Black);
                    pen.Width = 1;
                    for (int i = 0; i < numRows; i++)
                    {
                        Rectangle rect = new Rectangle(colWidth + 1, i*rowHeight + 1, colWidth - 1, rowHeight - 1);
                        g.FillRectangle(new SolidBrush(Color.LightGray), rect);
                        g.DrawString(lastPrice.ToString(), new Font(FontFamily.GenericSansSerif, 10), new SolidBrush(Color.Black), new Point(colWidth + 1, i*rowHeight + 1));
                    }
                    g.Dispose();
                }
                this.Invalidate(new Rectangle(0, 0, 1, 1));
            }
    }
}

Above code works, pretty well, however I am not sure if its correct. This is just for test purpose. Method OrderBookUpdateFn(OrderBookEvent orderBookEvent) gets fired very often (sometimes tens of times a second).

Body:
to avoid flickering of selfpainted controls you can DoubleBuffer the drawing of the form / control.
Add this to the constructor:

this.SetStyle(
  ControlStyles.AllPaintingInWmPaint |
  ControlStyles.UserPaint |
  ControlStyles.DoubleBuffer,true);

some anti-flicker techniques


问:
How to create debian package with CPack command (not from CMake's files)?
I have CMake project built and installed.
Now I want to generate debian package (*.deb) from this. In the Internet there are many instructions how to create debian package with adding something to CMake's files, but the project, which I've built does not belongs to me, so I shouldn't modify its source.
I've found command cpack, which is can also generate deb packages. Unfortunately when I try to use the command:

```<code>cpack -G DEB -C cmake/build/directory -P myPackage.deb -R 1.0.
</code>```

I see:

```<code>CPack Error: Please specify build tree of the project that uses CMake using CPACK_INSTALL_CMAKE_PROJECTS, specify CPACK_INSTALL_COMMANDS, CPACK_INSTALL_SCRIPT, or CPACK_INSTALLED_DIRECTORIES.
</code>```

Unfortunately the options can't be specified in commands in help:

```<code>cpack --help
</code>```

So is it possible to generate debian package with command cpack without any changes to CMake files?

答:
When the CMakeLists.txt includes the CPack module, it produces CPackConfig.cmake in the top build directory. This config file is the default for CPack, but you can override it w/ --config option.

The file consists of a bunch of set() commands to set various CPACK_* variables. To produce a package (the DEB in your question) you ought to write the config file "manually" and set vital variables for CPack, as well as some for DEB generator (i.e. CPACK_DEBIAN_*).

Generally, this config (the variables in it) describes what project(s) and it's components to include to package(s), define some meta-data & so on... In theory, you can pass all that defines via -D options to cpack(1). In practice, IMHO, it'll be easier to write the CPackConfig.cmake %)

Having that config file this command should to what you want:

```<code>$ cpack -G DEB
</code>```

(or just cpack alone if your config describes only Debian package to build).

Body:
I have CMake project built and installed.
Now I want to generate debian package (*.deb) from this. In the Internet there are many instructions how to create debian package with adding something to CMake's files, but the project, which I've built does not belongs to me, so I shouldn't modify its source.
I've found command cpack, which is can also generate deb packages. Unfortunately when I try to use the command:

cpack -G DEB -C cmake/build/directory -P myPackage.deb -R 1.0.

I see:

CPack Error: Please specify build tree of the project that uses CMake using CPACK_INSTALL_CMAKE_PROJECTS, specify CPACK_INSTALL_COMMANDS, CPACK_INSTALL_SCRIPT, or CPACK_INSTALLED_DIRECTORIES.

Unfortunately the options can't be specified in commands in help:

cpack --help

So is it possible to generate debian package with command cpack without any changes to CMake files?

Body:
When the CMakeLists.txt includes the CPack module, it produces CPackConfig.cmake in the top build directory. This config file is the default for CPack, but you can override it w/ --config option.

The file consists of a bunch of set() commands to set various CPACK_* variables. To produce a package (the DEB in your question) you ought to write the config file "manually" and set vital variables for CPack, as well as some for DEB generator (i.e. CPACK_DEBIAN_*).

Generally, this config (the variables in it) describes what project(s) and it's components to include to package(s), define some meta-data & so on... In theory, you can pass all that defines via -D options to cpack(1). In practice, IMHO, it'll be easier to write the CPackConfig.cmake %)

Having that config file this command should to what you want:

$ cpack -G DEB

(or just cpack alone if your config describes only Debian package to build).


问:
Connect to External Oracle database using ksqlDB running in Docker container
I am trying to connect to External Oracle database using ksqlDB running in docker container on my Windows 10 machine. I have gone through the tutorials mentioned on the ksqlDB website and managed to run ksqlDB using below docker-compose file. Now, I need to create JDBC connector to connect to EXTERNAL ORACLE database(not inside container). As most of the available tutorials are on the Postgres database running inside container, I am a little bit confused about connectivity with external databases. I have mentioned one template for connector below. Please suggest how can I modify it based on my Oracle database configuration?

Suppose, Oracle database configuration are as mentioned below on my local machine :

```<code>HOST = dev-db.com
PORT = 1522
SERVICE NAME = dv06.db.com
</code>```

Connector template for Postgres:

```<code>CREATE SOURCE CONNECTOR jdbc_source WITH (
  'connector.class'          = 'io.confluent.connect.jdbc.JdbcSourceConnector',
  'connection.url'           = 'jdbc:postgresql://postgres:5432/postgres',
  'connection.user'          = 'postgres',
  'connection.password'      = 'password',
  'topic.prefix'             = 'jdbc_',
  'table.whitelist'          = 'driver_profiles',
  'mode'                     = 'incrementing',
  'numeric.mapping'          = 'best_fit',
  'incrementing.column.name' = 'driver_id',
  'key'                      = 'driver_id',
  'key.converter'            = 'org.apache.kafka.connect.converters.IntegerConverter');
</code>```

Docker-compose.yml

```<code>---
version: '2'

services:
  zookeeper:
    image: confluentinc/cp-zookeeper:5.4.0
    hostname: zookeeper
    container_name: zookeeper
    ports:
      - "2181:2181"
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000

  broker:
    image: confluentinc/cp-kafka:5.4.0
    hostname: broker
    container_name: broker
    depends_on:
      - zookeeper
    ports:
      - "29092:29092"
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: 'zookeeper:2181'
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://broker:9092,PLAINTEXT_HOST://localhost:29092
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
      KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
      KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
      KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1

  ksqldb-server:
    image: confluentinc/ksqldb-server:0.7.0
    hostname: ksqldb-server
    container_name: ksqldb-server
    depends_on:
      - broker
    ports:
      - "8088:8088"
    environment:
      KSQL_LISTENERS: http://0.0.0.0:8088
      KSQL_BOOTSTRAP_SERVERS: broker:9092
      KSQL_KSQL_LOGGING_PROCESSING_STREAM_AUTO_CREATE: "true"
      KSQL_KSQL_LOGGING_PROCESSING_TOPIC_AUTO_CREATE: "true"
      KSQL_KSQL_CONNECT_WORKER_CONFIG: "/connect/connect.properties"
      KSQL_CONNECT_GROUP_ID: "ksql-connect-cluster"
      KSQL_CONNECT_BOOTSTRAP_SERVERS: "broker:9092"
      KSQL_CONNECT_KEY_CONVERTER: "io.confluent.connect.avro.AvroConverter"
      KSQL_CONNECT_KEY_CONVERTER_SCHEMA_REGISTRY_URL: "http://schema-registry:8081"
      KSQL_CONNECT_VALUE_CONVERTER: "io.confluent.connect.avro.AvroConverter"
      KSQL_CONNECT_VALUE_CONVERTER_SCHEMA_REGISTRY_URL: "http://schema-registry:8081"
      KSQL_CONNECT_CONFIG_STORAGE_TOPIC: "ksql-connect-configs"
      KSQL_CONNECT_OFFSET_STORAGE_TOPIC: "ksql-connect-offsets"
      KSQL_CONNECT_STATUS_STORAGE_TOPIC: "ksql-connect-statuses"
      KSQL_CONNECT_CONFIG_STORAGE_REPLICATION_FACTOR: 1
      KSQL_CONNECT_OFFSET_STORAGE_REPLICATION_FACTOR: 1
      KSQL_CONNECT_STATUS_STORAGE_REPLICATION_FACTOR: 1
      KSQL_CONNECT_PLUGIN_PATH: "/usr/share/kafka/plugins"
    volumes:
      - ./confluentinc-kafka-connect-jdbc-5.4.0:/usr/share/kafka/plugins/jdbc

  ksqldb-cli:
    image: confluentinc/ksqldb-cli:0.7.0
    container_name: ksqldb-cli
    depends_on:
      - broker
      - ksqldb-server
    entrypoint: /bin/sh
    tty: true
</code>```

答:
If what you need is to know the format of the connection.url to use, then the Oracle docs are your friend here: https://docs.oracle.com/database/121/TDPJD/getconn.htm#TDPJD138

For example, the following may work for you:

'connection.url'           = 'jdbc:oracle:thin:@dev-db.com:1522',

Of course, you'll also need to update the username, password and other details about the tables and their keys that you want to import using the connector too.

Body:
I am trying to connect to External Oracle database using ksqlDB running in docker container on my Windows 10 machine. I have gone through the tutorials mentioned on the ksqlDB website and managed to run ksqlDB using below docker-compose file. Now, I need to create JDBC connector to connect to EXTERNAL ORACLE database(not inside container). As most of the available tutorials are on the Postgres database running inside container, I am a little bit confused about connectivity with external databases. I have mentioned one template for connector below. Please suggest how can I modify it based on my Oracle database configuration?

Suppose, Oracle database configuration are as mentioned below on my local machine :

HOST = dev-db.com
PORT = 1522
SERVICE NAME = dv06.db.com

Connector template for Postgres:

CREATE SOURCE CONNECTOR jdbc_source WITH (
  'connector.class'          = 'io.confluent.connect.jdbc.JdbcSourceConnector',
  'connection.url'           = 'jdbc:postgresql://postgres:5432/postgres',
  'connection.user'          = 'postgres',
  'connection.password'      = 'password',
  'topic.prefix'             = 'jdbc_',
  'table.whitelist'          = 'driver_profiles',
  'mode'                     = 'incrementing',
  'numeric.mapping'          = 'best_fit',
  'incrementing.column.name' = 'driver_id',
  'key'                      = 'driver_id',
  'key.converter'            = 'org.apache.kafka.connect.converters.IntegerConverter');

Docker-compose.yml

---
version: '2'

services:
  zookeeper:
    image: confluentinc/cp-zookeeper:5.4.0
    hostname: zookeeper
    container_name: zookeeper
    ports:
      - "2181:2181"
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000

  broker:
    image: confluentinc/cp-kafka:5.4.0
    hostname: broker
    container_name: broker
    depends_on:
      - zookeeper
    ports:
      - "29092:29092"
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: 'zookeeper:2181'
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://broker:9092,PLAINTEXT_HOST://localhost:29092
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
      KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
      KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
      KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1

  ksqldb-server:
    image: confluentinc/ksqldb-server:0.7.0
    hostname: ksqldb-server
    container_name: ksqldb-server
    depends_on:
      - broker
    ports:
      - "8088:8088"
    environment:
      KSQL_LISTENERS: http://0.0.0.0:8088
      KSQL_BOOTSTRAP_SERVERS: broker:9092
      KSQL_KSQL_LOGGING_PROCESSING_STREAM_AUTO_CREATE: "true"
      KSQL_KSQL_LOGGING_PROCESSING_TOPIC_AUTO_CREATE: "true"
      KSQL_KSQL_CONNECT_WORKER_CONFIG: "/connect/connect.properties"
      KSQL_CONNECT_GROUP_ID: "ksql-connect-cluster"
      KSQL_CONNECT_BOOTSTRAP_SERVERS: "broker:9092"
      KSQL_CONNECT_KEY_CONVERTER: "io.confluent.connect.avro.AvroConverter"
      KSQL_CONNECT_KEY_CONVERTER_SCHEMA_REGISTRY_URL: "http://schema-registry:8081"
      KSQL_CONNECT_VALUE_CONVERTER: "io.confluent.connect.avro.AvroConverter"
      KSQL_CONNECT_VALUE_CONVERTER_SCHEMA_REGISTRY_URL: "http://schema-registry:8081"
      KSQL_CONNECT_CONFIG_STORAGE_TOPIC: "ksql-connect-configs"
      KSQL_CONNECT_OFFSET_STORAGE_TOPIC: "ksql-connect-offsets"
      KSQL_CONNECT_STATUS_STORAGE_TOPIC: "ksql-connect-statuses"
      KSQL_CONNECT_CONFIG_STORAGE_REPLICATION_FACTOR: 1
      KSQL_CONNECT_OFFSET_STORAGE_REPLICATION_FACTOR: 1
      KSQL_CONNECT_STATUS_STORAGE_REPLICATION_FACTOR: 1
      KSQL_CONNECT_PLUGIN_PATH: "/usr/share/kafka/plugins"
    volumes:
      - ./confluentinc-kafka-connect-jdbc-5.4.0:/usr/share/kafka/plugins/jdbc

  ksqldb-cli:
    image: confluentinc/ksqldb-cli:0.7.0
    container_name: ksqldb-cli
    depends_on:
      - broker
      - ksqldb-server
    entrypoint: /bin/sh
    tty: true


问:
What causes Composer not to update to a, modified, existing version?
I've created a v1.0.4 tag for my plug-in and deployer executed composer update on the server. After a short amount of time I've deleted this tag and created it again because this version needed a minor modification.
Now when deployer executes composer update on the server the plug-in is not been updated to the new v1.0.4 (that includes the modification) because the version number is still the same.

I've executed composer clear-cache on the server but this does not seem to work. Is this even possible or was it better to create a new version for this modification?

The modified tag v1.0.4 is present in the repository. Any help is appreciated.

答: Please tag your plugin properly as a new version. If v1.0.4 of it is already installed, the commit hash used by that tag is stored in composer.lock, and getting this fixed might lead to more problems popping up.


问:
Comparing sql with c# list in query?
I have a problem with comparing a value in my sql database with a list of object in my code.

The object does not exist in DB

Example of an object in the list:

```<code>{
    public long CompareId
    public bool HasAccess
}
</code>```

I'm using SQLBuilder in c#
And then I want to make a query that says something like this:

In made up code

```<code>SELECT * FROM EntityPermission
WHERE EntityPermission.HasAccess = listOfObjects.Where(obj => obj.CompareId == EntityPermission.CompareId).HasAccess
</code>```

In more made up code but with sql builder

```<code>query.WHERE("(EntityPermission.HasAccess = {0})", listOfObjects.Where(obj => obj.CompareId == EntityPermission.CompareId).HasAccess)
</code>```

I'm fully aware of that I'm mixing c# and sql here, but it was the best way I could explain what I want to accomplish.

In words

I want to find the EntityPermission where the HasAccess column is equal to the HasAccess property of the object where they have the same Id.

Really thankful for all help!

答:
You can first get your sql query in a result table then use LINQ to get your intended values. I know it is not most effective way but it could work.</p>

```<code>        public virtual List<YOUR_DTO> ExampleOperation(YOUR_DTO dto)
        {

            sqlText="SELECT * FROM EntityPermission ";
            dbComm = db.GetSqlStringCommand(sqlText);

            DataTable table = this.Database.ExecuteDataSet(dbComm).Tables[0];

            List<YOUR_DTO> result = new List<YOUR_DTO>();
            foreach (DataRow row in table.Rows)
            {
                result.Add(new YOUR_DTO()
                {
                    ...
                });
            }
            //LINQ
            result = result.Where(obj => obj.CompareId == EntityPermission.CompareId).HasAccess;
            return result;
        }
</code>```

Title: Comparing sql with c# list in query?

Body:
I have a problem with comparing a value in my sql database with a list of object in my code.

The object does not exist in DB

Example of an object in the list:

{
    public long CompareId
    public bool HasAccess
}

I'm using SQLBuilder in c#
And then I want to make a query that says something like this:

In made up code

SELECT * FROM EntityPermission
WHERE EntityPermission.HasAccess = listOfObjects.Where(obj => obj.CompareId == EntityPermission.CompareId).HasAccess

In more made up code but with sql builder

query.WHERE("(EntityPermission.HasAccess = {0})", listOfObjects.Where(obj => obj.CompareId == EntityPermission.CompareId).HasAccess)

I'm fully aware of that I'm mixing c# and sql here, but it was the best way I could explain what I want to accomplish.

In words

I want to find the EntityPermission where the HasAccess column is equal to the HasAccess property of the object where they have the same Id.

Really thankful for all help!

Body:
You can first get your sql query in a result table then use LINQ to get your intended values. I know it is not most effective way but it could work.

        public virtual List<YOUR_DTO> ExampleOperation(YOUR_DTO dto)
        {

            sqlText="SELECT * FROM EntityPermission ";
            dbComm = db.GetSqlStringCommand(sqlText);

            DataTable table = this.Database.ExecuteDataSet(dbComm).Tables[0];

            List<YOUR_DTO> result = new List<YOUR_DTO>();
            foreach (DataRow row in table.Rows)
            {
                result.Add(new YOUR_DTO()
                {
                    ...
                });
            }
            //LINQ
            result = result.Where(obj => obj.CompareId == EntityPermission.CompareId).HasAccess;
            return result;
        }


问:
Interface extending multiple interfaces
i am using spring & have 2 interfaces,

```<code>interface A {
    public void a();
}

interface B {
    public void B();
}
</code>```

and their implementations

```<code>class AImpl implements A {
    public void a(){ // TODO }
}
class BImpl implements B {
    public void b(){ // TODO }
}
</code>```

Now I create an interface C

```<code>    interface C extends A, B {}
</code>```

Is there any way I can get a bean of type C with method definations of AImpl & BImpl

答:
You can't make a CImpl that extends AImpl and BImpl as Java doesn't support multiple inheritance for classes.

But you can do something like this:

```<code>class CImpl implements C {
    private A a;
    private B b;

    public CImpl(A a, B b) {
        this.a = a;
        this.b = b;
    }

    public void a() {
        a.a();
    }

    public void b() {
        b.b();
    }
}
</code>```

And create your CImpl with:

```<code>CImpl c = new CImpl (new AImpl(), new BImpl());
</code>```

If you use Spring (as your tags imply) you can configure AImpl, BImpl and CImpl as Beans and use autowiring for the constructor of CImpl.

Title: Interface extending multiple interfaces

Body:
i am using spring & have 2 interfaces,

interface A {
    public void a();
}

interface B {
    public void B();
}

and their implementations

class AImpl implements A {
    public void a(){ // TODO }
}
class BImpl implements B {
    public void b(){ // TODO }
}

Now I create an interface C

    interface C extends A, B {}

Is there any way I can get a bean of type C with method definations of AImpl & BImpl

Body:
You can't make a CImpl that extends AImpl and BImpl as Java doesn't support multiple inheritance for classes.

But you can do something like this:

class CImpl implements C {
    private A a;
    private B b;

    public CImpl(A a, B b) {
        this.a = a;
        this.b = b;
    }

    public void a() {
        a.a();
    }

    public void b() {
        b.b();
    }
}

And create your CImpl with:

CImpl c = new CImpl (new AImpl(), new BImpl());

If you use Spring (as your tags imply) you can configure AImpl, BImpl and CImpl as Beans and use autowiring for the constructor of CImpl.


问:
Combining Unit Tests from Multiple Projects into One Docker Image for Azure DevOps Pipeline
I have followed the following article on getting my unit tests working in a Docker image and publishing via Azure DevOps pipeline.

Running Your Unit Tests With Visual Studio Team Services and Docker Compose

Each one of my unit tests projects have a very basic Dockerfile:

First Dockerfile for Application Tests

```<code>FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
COPY . /app
WORKDIR /app/Application.Tests
RUN dotnet restore
</code>```

Second DockerFile for Infrastructure tests.

```<code>FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
COPY . /app
WORKDIR /app/Infrastructure.Tests
RUN dotnet restore
</code>```

My docker-compose for both images:

```<code>application.tests:
    image: ${DOCKER_REGISTRY-}applicationtests
    build:
      context: .
      dockerfile: Application.Tests/Dockerfile
    entrypoint: >
        dotnet test --results-directory /testresults --logger trx
        /p:CollectCoverage=true
        /p:CoverletOutputFormat=cobertura
        /p:CoverletOutput=/testresults/Application.Tests.cobertura.xml
        /p:Exclude="[xunit.*]"
    volumes:
        - /opt/vsts/work/_temp:/testresults

infrastructure.tests:
    image: ${DOCKER_REGISTRY-}infrastructuretests
    build:
      context: .
      dockerfile: Infrastructure.Tests/Dockerfile
    entrypoint: >
        dotnet test --results-directory /testresults --logger trx
        /p:CollectCoverage=true
        /p:CoverletOutputFormat=cobertura
        /p:CoverletOutput=/testresults/Infrastructure.Tests.cobertura.xml
        /p:Exclude="[xunit.*]"
    volumes:
        - /opt/vsts/work/_temp:/testresults
</code>```

Is there a way to combine the definition for each image in docker-compose?  I understand that I can combine the tests into a single project, but want to maintain a 1:1 relationship between project and test project.

I should also add that both projects are in the same solution (.NET Core).

答:
Is there a way to run the tests for all projects in a single
  azure-pipeline task?

It's not available with DockerCompose task. According to your scenario, you should avoid combining multiple containers into one image. As the answer in this thread pointed that images should be kept light, and run one service per container.

Docker Compose Task is just the way to manage these services with multiple containers to run in your application. .

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration

If you insist on combing everything together, Docker compose is not fit for your case.

You could have a try with multiple stage dockerfile to combine these dockerfiles into one. This will end up with a single image when your run docker build

Body:
I have followed the following article on getting my unit tests working in a Docker image and publishing via Azure DevOps pipeline.

Running Your Unit Tests With Visual Studio Team Services and Docker Compose

Each one of my unit tests projects have a very basic Dockerfile:

First Dockerfile for Application Tests

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
COPY . /app
WORKDIR /app/Application.Tests
RUN dotnet restore

Second DockerFile for Infrastructure tests.

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
COPY . /app
WORKDIR /app/Infrastructure.Tests
RUN dotnet restore

My docker-compose for both images:

application.tests:
    image: ${DOCKER_REGISTRY-}applicationtests
    build:
      context: .
      dockerfile: Application.Tests/Dockerfile
    entrypoint: >
        dotnet test --results-directory /testresults --logger trx
        /p:CollectCoverage=true
        /p:CoverletOutputFormat=cobertura
        /p:CoverletOutput=/testresults/Application.Tests.cobertura.xml
        /p:Exclude="[xunit.*]"
    volumes:
        - /opt/vsts/work/_temp:/testresults

infrastructure.tests:
    image: ${DOCKER_REGISTRY-}infrastructuretests
    build:
      context: .
      dockerfile: Infrastructure.Tests/Dockerfile
    entrypoint: >
        dotnet test --results-directory /testresults --logger trx
        /p:CollectCoverage=true
        /p:CoverletOutputFormat=cobertura
        /p:CoverletOutput=/testresults/Infrastructure.Tests.cobertura.xml
        /p:Exclude="[xunit.*]"
    volumes:
        - /opt/vsts/work/_temp:/testresults

Is there a way to combine the definition for each image in docker-compose?  I understand that I can combine the tests into a single project, but want to maintain a 1:1 relationship between project and test project.

I should also add that both projects are in the same solution (.NET Core).


问:
IE 11 FILE SELECTION ISSUE - FILES MISSING ON REMOTE CONNECTION
I have updated web page that was using flash to upload files. The updated page works on Edge Chromium, Edge Legacy, Chrome and Firefox. But on IE 11, I have a problem.

I have three systems 2 x Windows 10 and a Server 2019 VM running on one of the Windows 10 systems. All systems have IIS installed.  I have put the updated page / site on all systems.

If I connect to the local host in IE 11, files will upload to the local system, but if I am connected to one of the other systems / remote host then I run into a problem.

In the aspx page I have the following to allow browsing for files:

<input type="button" id="BrowseFiles"  value="Browse Files" onclick="document.getElementById('FileNames').click();" />
<input type="file" id="FileNames" style="display:none;" multiple="multiple" onchange="ListFilesFunction()"/>```
</div>
</div>
</p>

<p>In the associated JavaScript I have:</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>      var sfs = document.getElementById("FileNames");</code>```

When I am connected to the local host “files” appears in “sfs”, but when connected to another system / remote host “files” [ Object FileList ] does not appear, but as previously mentioned only on IE 11.

I hope I am missing something obvious, any thoughts on this?

答:
On further investigation, I discovered that I had left "Display intranet sites in Compatibility View" option enabled in the "Compatibility View Settings".

Unchecking that option resolved the issue.

Body:
I have updated web page that was using flash to upload files. The updated page works on Edge Chromium, Edge Legacy, Chrome and Firefox. But on IE 11, I have a problem.

I have three systems 2 x Windows 10 and a Server 2019 VM running on one of the Windows 10 systems. All systems have IIS installed.  I have put the updated page / site on all systems.

If I connect to the local host in IE 11, files will upload to the local system, but if I am connected to one of the other systems / remote host then I run into a problem.

In the aspx page I have the following to allow browsing for files:

<input type="button" id="BrowseFiles"  value="Browse Files" onclick="document.getElementById('FileNames').click();" />
<input type="file" id="FileNames" style="display:none;" multiple="multiple" onchange="ListFilesFunction()"/>

In the associated JavaScript I have:

      var sfs = document.getElementById("FileNames");

When I am connected to the local host “files” appears in “sfs”, but when connected to another system / remote host “files” [ Object FileList ] does not appear, but as previously mentioned only on IE 11.

I hope I am missing something obvious, any thoughts on this?


问:
How can I redirect traffic from one port to another in the same interface?
I need the traffic that gets into the docker container to a specific port to be redirected to another container, the ip of which I know, to another port using xdp.
For this, I change the checksum of the iphdr structure:

```<code>// Backup old dest address
     unsigned short old_daddr = ntohs(*(unsigned short *)&iph->daddr);
// Override ip header
     iph->tos = 7 << 2;      // DSCP: 7
     iph->daddr = bpf_htonl(2887516418);  // Dest: 172.28.1.2
     iph->check = 0;
     iph->check = checksum((unsigned short *)iph, sizeof(struct iphdr));
...
</code>```

and then the checksum of the tcphdr structure:

```<code>// Update tcp checksum
    unsigned long sum = old_daddr + (~ntohs(*(unsigned short *)&iph->daddr) & 0xffff);
    sum += ntohs(tcp->check);
    sum = (sum & 0xffff) + (sum>>16);
    tcp->check = htons(sum + (sum>>16) - 1);

__u16 new_port = 5555;
    if ((__u16)bpf_ntohs(tcp->dest) == new_port) {
        printt("new check: %d, dest port: %d, source port: %d\n", tcp->check, (__u16)bpf_ntohs(tcp->dest), (__u16)bpf_ntohs(tcp->source));
    } else if ((__u16)bpf_ntohs(tcp->dest) == (__u16)4224) {
        update_tcp_header_port(tcp, &new_port);
        printt("change check: %d, dest port: %d, source port: %d\n", tcp->check, (__u16)bpf_ntohs(tcp->dest), (__u16)bpf_ntohs(tcp->source));
    }
...
return XDP_TX;
</code>```

but it doesn’t work, what am I doing wrong ?

答:
```<code>INTERNAL unsigned short checksum(unsigned short *buf, int bufsz) {
    unsigned long sum = 0;

    while (bufsz > 1) {
        sum += *buf;
        buf++;
        bufsz -= 2;
    }

    if (bufsz == 1) {
        sum += *(unsigned char *)buf;
    }

    sum = (sum & 0xffff) + (sum >> 16);
    sum = (sum & 0xffff) + (sum >> 16);

    return ~sum;
}
</code>```

Body:
I need the traffic that gets into the docker container to a specific port to be redirected to another container, the ip of which I know, to another port using xdp.
For this, I change the checksum of the iphdr structure:

// Backup old dest address
     unsigned short old_daddr = ntohs(*(unsigned short *)&iph->daddr);
// Override ip header
     iph->tos = 7 << 2;      // DSCP: 7
     iph->daddr = bpf_htonl(2887516418);  // Dest: 172.28.1.2
     iph->check = 0;
     iph->check = checksum((unsigned short *)iph, sizeof(struct iphdr));
...

and then the checksum of the tcphdr structure:

// Update tcp checksum
    unsigned long sum = old_daddr + (~ntohs(*(unsigned short *)&iph->daddr) & 0xffff);
    sum += ntohs(tcp->check);
    sum = (sum & 0xffff) + (sum>>16);
    tcp->check = htons(sum + (sum>>16) - 1);

__u16 new_port = 5555;
    if ((__u16)bpf_ntohs(tcp->dest) == new_port) {
        printt("new check: %d, dest port: %d, source port: %d\n", tcp->check, (__u16)bpf_ntohs(tcp->dest), (__u16)bpf_ntohs(tcp->source));
    } else if ((__u16)bpf_ntohs(tcp->dest) == (__u16)4224) {
        update_tcp_header_port(tcp, &new_port);
        printt("change check: %d, dest port: %d, source port: %d\n", tcp->check, (__u16)bpf_ntohs(tcp->dest), (__u16)bpf_ntohs(tcp->source));
    }
...
return XDP_TX;

but it doesn’t work, what am I doing wrong ?


问:
How do I fix an Internal Server Error?- Google Cloud
I am following this tutorial on Google Cloud Platform and am finding that the SIMPLE app launches successfully up on port 8080 but when I head over to the browser to view it externally, I get an internal server error. The tutorial can be found here:
https://cloud.google.com/appengine/docs/standard/nodejs/building-app/deploying-web-service
I am using Win8.1 using the GCP cloud shell terminal.

I have also tried updating my npm packages, moving my yaml, nodejs files to the next higher directory as well as deleting the package.json in the next higher directory. Like I said, port 8080 can come up and logged to the terminal, but does not come up in the browser. My package.json is as follows:

```<code>{
  "name": "express",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "start": "node server.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "description": ""
}
</code>```

My app.yaml file is

```<code>runtime: nodejs10
</code>```

And my server.js file is

```<code>//this is a test by MP2
// date of use : 2020-0601
const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello from App Engine!');
});

// Listen to the App Engine-specified port, or 8080 otherwise
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
  console.log(`Server listening on port ${PORT}...`);
});
</code>```

Any help would be great since I am new to the google cloud. What is also interesting is that the most current version of express is installed at the next higher folder but not in the current one where the desired JSON is. ( and unable to update it) Thank you.
-MP

答:
It seems that this is because there is no express dependency in package.json:

```<code>"dependencies": {
    "express": "^4.17.1"
  }
</code>```

I have replicated all steps and it worked. In the tutorial there is 4.16.3 version, I have different, because I have used npm install express -s and it added version automatically to package.json. However it works with 4.16.3 as well on my side.

I hope it will help!

Body:
I am following this tutorial on Google Cloud Platform and am finding that the SIMPLE app launches successfully up on port 8080 but when I head over to the browser to view it externally, I get an internal server error. The tutorial can be found here:
https://cloud.google.com/appengine/docs/standard/nodejs/building-app/deploying-web-service
I am using Win8.1 using the GCP cloud shell terminal.

I have also tried updating my npm packages, moving my yaml, nodejs files to the next higher directory as well as deleting the package.json in the next higher directory. Like I said, port 8080 can come up and logged to the terminal, but does not come up in the browser. My package.json is as follows:

{
  "name": "express",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "start": "node server.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "description": ""
}

My app.yaml file is

runtime: nodejs10

And my server.js file is

//this is a test by MP2
// date of use : 2020-0601
const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello from App Engine!');
});

// Listen to the App Engine-specified port, or 8080 otherwise
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
  console.log(`Server listening on port ${PORT}...`);
});

Any help would be great since I am new to the google cloud. What is also interesting is that the most current version of express is installed at the next higher folder but not in the current one where the desired JSON is. ( and unable to update it) Thank you.
-MP

Body:
It seems that this is because there is no express dependency in package.json:

"dependencies": {
    "express": "^4.17.1"
  }

I have replicated all steps and it worked. In the tutorial there is 4.16.3 version, I have different, because I have used npm install express -s and it added version automatically to package.json. However it works with 4.16.3 as well on my side.

I hope it will help!


问:
remove duplicates in python
I'm looking to remove the names that are repeated.

My code is connected to retrieve information from SQL server to Python.

```<code>def get_all_artist():
    query="SELECT artist_name FROM Sheet1"
    all_artist = execute_read_query (conn, query)
    for artist_record in all_artist:
        print(str(artist_record[0]))
    return (all_artist)
</code>```

The artist_name that I am retrieving on SQL are:

```<code>BTS
BTS
BTS
TWICE
TWICE
TWICE
TWICE
TWICE
HEIZE
HEIZE
KHALID
KHALID
KHALID
ERIC CHOU
ERIC CHOU
ERIC CHOU
SAM SMITH
SAM SMITH
SAM SMITH
AGUST D
</code>```

However, I'd only like to remove the duplicates on Python without removing any rows in my SQL table:

```<code>BTS
TWICE
HEIZE
KHALID
ERIC CHOU
SAM SMITH
AGUST D
</code>```

答:
Use This Function and It Will Be work:</p>

```<code>def get_all_artist():
    query="SELECT distinct artist_name FROM Sheet1"
    all_artist = execute_read_query (conn, query)
    for artist_record in all_artist:
        print(str(artist_record[0]))
    return (all_artist)
</code>```

Title: remove duplicates in python

Body:
I'm looking to remove the names that are repeated.

My code is connected to retrieve information from SQL server to Python.

def get_all_artist():
    query="SELECT artist_name FROM Sheet1"
    all_artist = execute_read_query (conn, query)
    for artist_record in all_artist:
        print(str(artist_record[0]))
    return (all_artist)

The artist_name that I am retrieving on SQL are:

BTS
BTS
BTS
TWICE
TWICE
TWICE
TWICE
TWICE
HEIZE
HEIZE
KHALID
KHALID
KHALID
ERIC CHOU
ERIC CHOU
ERIC CHOU
SAM SMITH
SAM SMITH
SAM SMITH
AGUST D

However, I'd only like to remove the duplicates on Python without removing any rows in my SQL table:

BTS
TWICE
HEIZE
KHALID
ERIC CHOU
SAM SMITH
AGUST D

Body:
Use This Function and It Will Be work:

def get_all_artist():
    query="SELECT distinct artist_name FROM Sheet1"
    all_artist = execute_read_query (conn, query)
    for artist_record in all_artist:
        print(str(artist_record[0]))
    return (all_artist)


问:
how to parse Turkish charactern in an xml response on iOS
I am getting some Turkish characters in an xml response. That causes some exceptions.

Can anybody tell me how can handle this?

答:
Try this. Convert it into UTF8

```<code>NSData *data = [myString dataUsingEncoding:NSUTF8StringEncoding];
</code>```

Then parse with this data...

```<code>NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
</code>```

Body:
Try this. Convert it into UTF8

NSData *data = [myString dataUsingEncoding:NSUTF8StringEncoding];

Then parse with this data...

NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];


问:
I am unable to extract numeric fields from a PostgreSQL database using Dart and the Postgres package
I have a simple, single row, PostgreSQL database table that includes a numeric(8,2) field, set to 120.00, and a character field set to 'N'.
This is a Windows 10 setup.
When I run the following Dart program the result is unexpected:

```<code>import 'package:postgres/postgres.dart';

void main() async {
  print('Attaching to DB...');
  final conn = PostgreSQLConnection(
    'localhost',
    5432,
    'pdpdb',
    username: 'XXXXXXX',
    password: 'XXXXXXX',
  );

  await conn.open();
  print('Connected to DB');

  var results = await conn.query('''
    SELECT verification, balance FROM contacts
  ''');

  print(results);

  await conn.close();
}
</code>```

My result is this:

```<code>Attaching to DB...
Connected to DB
[[N,  ╔     ╗ 2]]
</code>```

So the 'N' is clear, but the numeric field is gibberish!
Why am I not seeing 120.00 as I do when I run the same command using pgAdmin's query tool:

I have tried to parse the field as if it were a double, but that gives me:

```<code>Unhandled exception:
FormatException: Invalid double
</code>```

I am sure this must be something obvious so any hints will be gratefully received.  Thank you.

If I write a similar process using toMaps():

```<code>final results = await conn.query('''
      SELECT verification, balance FROM contacts
      ''').toMaps();

  print(results);
</code>```

I get the output:

```<code>[{verification: N, balance:
</code>```

Suggesting that something has broken the output.

答:
This isn't really an answer - more of a work around:

It seems that the Postgres package doesn't support NUMERIC fields as Dart doesn't have an equivalent type.
This workaround:
github issue
suggests the use of CASTing the numeric field to a float within the SELECT statement.
I've tried it and it does work.  Granted it is not ideal, but as it works that helps me out a lot at the moment.

OwnerDisplayName: Paul Pritchard

Body:
I have a simple, single row, PostgreSQL database table that includes a numeric(8,2) field, set to 120.00, and a character field set to 'N'.
This is a Windows 10 setup.
When I run the following Dart program the result is unexpected:

import 'package:postgres/postgres.dart';

void main() async {
  print('Attaching to DB...');
  final conn = PostgreSQLConnection(
    'localhost',
    5432,
    'pdpdb',
    username: 'XXXXXXX',
    password: 'XXXXXXX',
  );

  await conn.open();
  print('Connected to DB');

  var results = await conn.query('''
    SELECT verification, balance FROM contacts
  ''');

  print(results);

  await conn.close();
}

My result is this:

Attaching to DB...
Connected to DB
[[N,  ╔     ╗ 2]]

So the 'N' is clear, but the numeric field is gibberish!
Why am I not seeing 120.00 as I do when I run the same command using pgAdmin's query tool:

I have tried to parse the field as if it were a double, but that gives me:

Unhandled exception:
FormatException: Invalid double

I am sure this must be something obvious so any hints will be gratefully received.  Thank you.

If I write a similar process using toMaps():

final results = await conn.query('''
      SELECT verification, balance FROM contacts
      ''').toMaps();

  print(results);

I get the output:

[{verification: N, balance:

Suggesting that something has broken the output.


问:
django model reference, Add more contraint to reference
I have a question about Django model references.

I want to add more constraints to a ForeignKey or ManytoManyField like this:

question_id = models.ManyToManyField(Question(open==True))

Or put another similar constraint:

Actually I don't want to show a question that is not open as a selection( heightened in Answer form like the image above),
of course, I have done it by some query but does Django has any built-in support for it? I have tried some other way but it didn't work. Thanks!

class Answer(models.Model):
    """Give answers"""
    answer = models.TextField()
    question_id = models.ManyToManyField(Question(open==True))
    upVote = models.IntegerField(default=0)
    downVote = models.IntegerField(default=0)

    def __str__(self):
        """return string  """
        return self.answer
```

答:
While you still can, change your model relations. What you have now is that an answer can be linked to multiple questions. However, answer contains up and down vote.

So I can create two questions:

Is 1 + 1 two?

yes

no

Is 1 + 1 three?

yes

no

I can link the answers "yes" and "no" to both questions, which can be convenient with a good interface and is normalized. But they will share the up/down votes. Instead, answer should have a foreign key to question, because an answer can only be linked to one question at a time, even if the answer text is identical to prevent sharing of the up and down votes.

Secondly, we generally don't name fields question_id, but question:
From an object relation perspective, you relate an answer to a question not to a question id. (Under the hood, question_id is created to allow faster lookups and to serve as field name in the underlying database table).

On to your actual problem: you want to limit the available choices, which is what limit_choices_to does for you. So you would end up with this:

class Answer(models.Model):
    """Give answers"""
    answer = models.TextField()
    question = models.ForeignKey(
        Question, on_delete=models.CASCADE, related_name='answers',
        limit_choices_to={'open': True},
    )
    upVote = models.IntegerField(default=0)
    downVote = models.IntegerField(default=0)

    def __str__(self):
        """return string  """
        return self.answer

# Serializer
class AnswerSerializer(serializers.ModelSerializer)
    class Meta:
        model = Answer
        fields = ('answer', 'upVote', 'downVote', 'question_id')
```

As you can see, your serializer can reference the magic field question_id.

Body:
While you still can, change your model relations. What you have now is that an answer can be linked to multiple questions. However, answer contains up and down vote.

So I can create two questions:

Is 1 + 1 two?

yes

no

Is 1 + 1 three?

yes

no

I can link the answers "yes" and "no" to both questions, which can be convenient with a good interface and is normalized. But they will share the up/down votes. Instead, answer should have a foreign key to question, because an answer can only be linked to one question at a time, even if the answer text is identical to prevent sharing of the up and down votes.

Secondly, we generally don't name fields question_id, but question:
From an object relation perspective, you relate an answer to a question not to a question id. (Under the hood, question_id is created to allow faster lookups and to serve as field name in the underlying database table).

On to your actual problem: you want to limit the available choices, which is what limit_choices_to does for you. So you would end up with this:

class Answer(models.Model):
    """Give answers"""
    answer = models.TextField()
    question = models.ForeignKey(
        Question, on_delete=models.CASCADE, related_name='answers',
        limit_choices_to={'open': True},
    )
    upVote = models.IntegerField(default=0)
    downVote = models.IntegerField(default=0)

    def __str__(self):
        """return string  """
        return self.answer

# Serializer
class AnswerSerializer(serializers.ModelSerializer)
    class Meta:
        model = Answer
        fields = ('answer', 'upVote', 'downVote', 'question_id')

As you can see, your serializer can reference the magic field question_id.


问:
Finding Prime numbers in a vector and print them in revers in another vector
here's the problem:
x is a vector with n elements.. build another vector, y, which will contain prime elements from x, but in reverse order.
My code. Don't know what I'm doing wrong.

答:
In the for loop you are using in line 16, you should decrement the value of i, i.e, i--, while you are incrementing it, which might be leading it to be an infinite loop. Also, you have not handled the condition if v[i] = 1. According to your code, it will show 1 as prime number though it shouldn't and also you have declared isPrime as out of the loop which will lead to wrong answers. Let suppose all numbers to be prime and set isPrime to 'true' for every element in the input vector.
Try this:

```<code>for(int i = n-1; i >= 0 ; i--){
    bool isPrime = true;
    if(v[i] == 1){
        continue;
    }
    for(int j = 2; j < v[i]; j++){
        if(v[i] % j == 0){
             isPrime = false;
             break;
        }
   }

   if(isPrime){
       y.push_back(v[i]);
   }
}
</code>```

It should work.

Body:
In the for loop you are using in line 16, you should decrement the value of i, i.e, i--, while you are incrementing it, which might be leading it to be an infinite loop. Also, you have not handled the condition if v[i] = 1. According to your code, it will show 1 as prime number though it shouldn't and also you have declared isPrime as out of the loop which will lead to wrong answers. Let suppose all numbers to be prime and set isPrime to 'true' for every element in the input vector.
Try this:

for(int i = n-1; i >= 0 ; i--){
    bool isPrime = true;
    if(v[i] == 1){
        continue;
    }
    for(int j = 2; j < v[i]; j++){
        if(v[i] % j == 0){
             isPrime = false;
             break;
        }
   }

   if(isPrime){
       y.push_back(v[i]);
   }
}

It should work.


问:
Shell script sorting
I am trying to write a shell script that inserts data in postgres:

First Part:

#!/usr/bin/env bash

for f in /folder/*.sql; do
  psql -U postgres -f $f
done

for f in /folder1/*.sql; do
  psql -U postgres -f $f
done

```

I need to add several conditions on it.

I need to sort file names for folder.
Example my folder has files a.sql, c.sql, b.sql

I want to sort them on basis of name so first a.sql should be inserted then b.sql and then c.sql.

For folder1 there exists some unwanted files, i want to ignore them(which is happening now also) and first i want to insert the files having base fields and then other tables.

Example folder1 has 1.sql, a.txt, 3.sql, base.sql.

I do not want to insert a.txt
And first base.sql should be inserted and then 1.sql and 3.sql

How to do it via shell script?

答:
The asterisk wildcard expands to a sorted list, so you can just use one loop:

for f in /folder/*.sql /folder1/base.sql /folder1/?.sql; do
  psql -U postgres -f $f
done
```

The list is sorted alphabetically, though, and depends on the locale.

The question mark matches just one  character, so ?.sql will match both 1.sql and 3.sql. You can also use [0-9]*.sql to match files whose names start with a digit. Under extended globbing (shopt -s extglob), you can even say +([0-9]).sql, so it also matches files with more than one digit (e.g. 124.sql). Note that asciibetically, 10 < 2.

Title: Shell script sorting

Body:
I am trying to write a shell script that inserts data in postgres:

First Part:

#!/usr/bin/env bash

for f in /folder/*.sql; do
  psql -U postgres -f $f
done

for f in /folder1/*.sql; do
  psql -U postgres -f $f
done

I need to add several conditions on it.

I need to sort file names for folder.
Example my folder has files a.sql, c.sql, b.sql

I want to sort them on basis of name so first a.sql should be inserted then b.sql and then c.sql.

For folder1 there exists some unwanted files, i want to ignore them(which is happening now also) and first i want to insert the files having base fields and then other tables.

Example folder1 has 1.sql, a.txt, 3.sql, base.sql.

I do not want to insert a.txt
And first base.sql should be inserted and then 1.sql and 3.sql

How to do it via shell script?

Body:
The asterisk wildcard expands to a sorted list, so you can just use one loop:

for f in /folder/*.sql /folder1/base.sql /folder1/?.sql; do
  psql -U postgres -f $f
done

The list is sorted alphabetically, though, and depends on the locale.

The question mark matches just one  character, so ?.sql will match both 1.sql and 3.sql. You can also use [0-9]*.sql to match files whose names start with a digit. Under extended globbing (shopt -s extglob), you can even say +([0-9]).sql, so it also matches files with more than one digit (e.g. 124.sql). Note that asciibetically, 10 < 2.


问:
Yup validation access parent.parent
I'm using yup module for validate my form. I would like access to parent for test the value.

My schema :

```<code>enabled: yup.boolean(),
contactDetail: yup.object().shape({
  phoneNumber1: yup.string().nullable(),
  phoneNumber2: yup.string().nullable(),
  email: yup.string().test('email', 'test', async function() {
    // test enabled value
  })
}),
</code>```

The method when can access on same level, not the parent.

Anyone have an idea ?

答:
I came through similar issue. I used this workthrough with success.

Idea: Pass whole form-data as a context to the schema and access any form value using

this.options.context

```<code>  const schema = yup.object().shape({
        enabled: yup.boolean(),
        contactDetail: yup.object().shape({
          phoneNumber1: yup.string().nullable(),
          phoneNumber2: yup.string().nullable(),
          email: yup.string().test('email', 'test', async function () {
            // this.options.context.enabled

          }),
        }),
      });
</code>```

Passing Context

Without Formik

Send your Form data as a context while validating

```<code> schema.validateSync(data, {context: form_data})
</code>```

With Formik

Use validate instead of validationSchema

Pass your Form data as 4th argument in validateYupSchema which represents context and can be accessed later in schema.

Pass your schema as 2nd argument in validateYupSchema.

```<code>    <Formik
      validate={(value) => {
        try {
          validateYupSchema(value, schema, true, value);
        } catch (err) {
          return yupToFormErrors(err); //for rendering validation errors
        }

        return {};
      }}

       onSubmit={} />
</code>```

Following is for the asynchronous validation with Formik.

```<code>    <Formik
      validate={async (value) => {
        try {
          await validateYupSchema(value, schema, false, value);
        } catch (err) {
          return yupToFormErrors(err); //for rendering validation errors
        }

        return {};
      }}

       onSubmit={} />
</code>```

Title: Yup validation access parent.parent

Body:
I'm using yup module for validate my form. I would like access to parent for test the value.

My schema :

enabled: yup.boolean(),
contactDetail: yup.object().shape({
  phoneNumber1: yup.string().nullable(),
  phoneNumber2: yup.string().nullable(),
  email: yup.string().test('email', 'test', async function() {
    // test enabled value
  })
}),

The method when can access on same level, not the parent.

Anyone have an idea ?

Body:
I came through similar issue. I used this workthrough with success.

Idea: Pass whole form-data as a context to the schema and access any form value using

this.options.context

  const schema = yup.object().shape({
        enabled: yup.boolean(),
        contactDetail: yup.object().shape({
          phoneNumber1: yup.string().nullable(),
          phoneNumber2: yup.string().nullable(),
          email: yup.string().test('email', 'test', async function () {
            // this.options.context.enabled

          }),
        }),
      });

Passing Context

Without Formik

Send your Form data as a context while validating

 schema.validateSync(data, {context: form_data})

With Formik

Use validate instead of validationSchema

Pass your Form data as 4th argument in validateYupSchema which represents context and can be accessed later in schema.

Pass your schema as 2nd argument in validateYupSchema.

    <Formik
      validate={(value) => {
        try {
          validateYupSchema(value, schema, true, value);
        } catch (err) {
          return yupToFormErrors(err); //for rendering validation errors
        }

        return {};
      }}

       onSubmit={} />

Following is for the asynchronous validation with Formik.

    <Formik
      validate={async (value) => {
        try {
          await validateYupSchema(value, schema, false, value);
        } catch (err) {
          return yupToFormErrors(err); //for rendering validation errors
        }

        return {};
      }}

       onSubmit={} />