问:
Iterator of Strings to Inputstream of bytes
I would like to convert an iterator of Strings to Inputstream of bytes. Usually, I can do this by appending all the strings in a StringBuilder and doing:
InputStream is = new ByteArrayInputStream(sb.toString().getBytes());

But I want to do it lazily because my iterable is provided by Spark and could be very large in length. I found this example to do it in Scala:

```<code>  def rowsToInputStream(rows: Iterator[String], delimiter: String): InputStream = {
  val bytes: Iterator[Byte] = rows.map { row =>
    (row + "\n").getBytes
  }.flatten

  new InputStream {
    override def read(): Int = if (bytes.hasNext) {
      bytes.next & 0xff // bitwise AND - make the signed byte an unsigned int from 0-255
    } else {
      -1
    }
  }
}
</code>```

But I could not find an easy way to convert this into Java. I converted the iterator to stream using Spliterators.spliteratorUnknownSize but then getBytes outputs an array which could not flatten easily. Overall it became pretty messy.

Is there an elegant way to do this in Java?

答:
If you want to have an InputStream supporting fast bulk operations, you should implement the

int read(byte[] b, int off, int len) method, which not only can be called directly by the code reading the InputStream, but is also the backend for the inherited methods

int read(byte b[])

long skip(long n)

byte[] readAllBytes() (JDK 9)

int readNBytes(byte[] b, int off, int len) (JDK 9)

long transferTo(OutputStream out) (JDK 9)

byte[] readNBytes(int len) (JDK 11)

void skipNBytes(long n) (JDK 14)

which will work more efficiently when said method has an efficient implementation.

```<code>public class StringIteratorInputStream extends InputStream {
    private CharsetEncoder encoder;
    private Iterator<String> strings;
    private CharBuffer current;
    private ByteBuffer pending;

    public StringIteratorInputStream(Iterator<String> it) {
        this(it, Charset.defaultCharset());
    }
    public StringIteratorInputStream(Iterator<String> it, Charset cs) {
        encoder = cs.newEncoder();
        strings = Objects.requireNonNull(it);
    }

    @Override
    public int read() throws IOException {
        for(;;) {
            if(pending != null && pending.hasRemaining())
                return pending.get() & 0xff;
            if(!ensureCurrent()) return -1;
            if(pending == null) pending = ByteBuffer.allocate(4096);
            else pending.compact();
            encoder.encode(current, pending, !strings.hasNext());
            pending.flip();
        }
    }

    private boolean ensureCurrent() {
        while(current == null || !current.hasRemaining()) {
            if(!strings.hasNext()) return false;
            current = CharBuffer.wrap(strings.next());
        }
        return true;
    }

    @Override
    public int read(byte[] b, int off, int len) {
        // Objects.checkFromIndexSize(off, len, b.length); // JDK 9
        int transferred = 0;
        if(pending != null && pending.hasRemaining()) {
            boolean serveByBuffer = pending.remaining() >= len;
            pending.get(b, off, transferred = Math.min(pending.remaining(), len));
            if(serveByBuffer) return transferred;
            len -= transferred;
            off += transferred;
        }
        ByteBuffer bb = ByteBuffer.wrap(b, off, len);
        while(bb.hasRemaining() && ensureCurrent()) {
            int r = bb.remaining();
            encoder.encode(current, bb, !strings.hasNext());
            transferred += r - bb.remaining();
        }
        return transferred == 0? -1: transferred;
    }
}
</code>```

A ByteBuffer basically is the combination of the byte buf[];, int pos;, and int count; variables of your solution. However, the pending buffer is only initialized if the caller truly uses the int read() method to read single bytes. Otherwise, the code creates a ByteBuffer that is wrapping the caller provided target buffer, to encode the strings directly into it.

The CharBuffer follows the same concept, just for char sequences. In this code, it will always be a wrapper around one of the strings, rather than a buffer with a storage of its own. So in the best case, this InputStream implementation will encode all iterator provided strings into caller provided buffer(s), without intermediate storage.

This concept does already imply lazy processing, as without intermediate storage, only as much as fitting into the caller provided buffer, in other words, as much as requested by the caller, will be fetched from the iterator.

Body:
I would like to convert an iterator of Strings to Inputstream of bytes. Usually, I can do this by appending all the strings in a StringBuilder and doing:
InputStream is = new ByteArrayInputStream(sb.toString().getBytes());

But I want to do it lazily because my iterable is provided by Spark and could be very large in length. I found this example to do it in Scala:

  def rowsToInputStream(rows: Iterator[String], delimiter: String): InputStream = {
  val bytes: Iterator[Byte] = rows.map { row =>
    (row + "\n").getBytes
  }.flatten

  new InputStream {
    override def read(): Int = if (bytes.hasNext) {
      bytes.next & 0xff // bitwise AND - make the signed byte an unsigned int from 0-255
    } else {
      -1
    }
  }
}

But I could not find an easy way to convert this into Java. I converted the iterator to stream using Spliterators.spliteratorUnknownSize but then getBytes outputs an array which could not flatten easily. Overall it became pretty messy.

Is there an elegant way to do this in Java?

Body:
If you want to have an InputStream supporting fast bulk operations, you should implement the

int read(byte[] b, int off, int len) method, which not only can be called directly by the code reading the InputStream, but is also the backend for the inherited methods

int read(byte b[])

long skip(long n)

byte[] readAllBytes() (JDK 9)

int readNBytes(byte[] b, int off, int len) (JDK 9)

long transferTo(OutputStream out) (JDK 9)

byte[] readNBytes(int len) (JDK 11)

void skipNBytes(long n) (JDK 14)

which will work more efficiently when said method has an efficient implementation.

public class StringIteratorInputStream extends InputStream {
    private CharsetEncoder encoder;
    private Iterator<String> strings;
    private CharBuffer current;
    private ByteBuffer pending;

    public StringIteratorInputStream(Iterator<String> it) {
        this(it, Charset.defaultCharset());
    }
    public StringIteratorInputStream(Iterator<String> it, Charset cs) {
        encoder = cs.newEncoder();
        strings = Objects.requireNonNull(it);
    }

    @Override
    public int read() throws IOException {
        for(;;) {
            if(pending != null && pending.hasRemaining())
                return pending.get() & 0xff;
            if(!ensureCurrent()) return -1;
            if(pending == null) pending = ByteBuffer.allocate(4096);
            else pending.compact();
            encoder.encode(current, pending, !strings.hasNext());
            pending.flip();
        }
    }

    private boolean ensureCurrent() {
        while(current == null || !current.hasRemaining()) {
            if(!strings.hasNext()) return false;
            current = CharBuffer.wrap(strings.next());
        }
        return true;
    }

    @Override
    public int read(byte[] b, int off, int len) {
        // Objects.checkFromIndexSize(off, len, b.length); // JDK 9
        int transferred = 0;
        if(pending != null && pending.hasRemaining()) {
            boolean serveByBuffer = pending.remaining() >= len;
            pending.get(b, off, transferred = Math.min(pending.remaining(), len));
            if(serveByBuffer) return transferred;
            len -= transferred;
            off += transferred;
        }
        ByteBuffer bb = ByteBuffer.wrap(b, off, len);
        while(bb.hasRemaining() && ensureCurrent()) {
            int r = bb.remaining();
            encoder.encode(current, bb, !strings.hasNext());
            transferred += r - bb.remaining();
        }
        return transferred == 0? -1: transferred;
    }
}

A ByteBuffer basically is the combination of the byte buf[];, int pos;, and int count; variables of your solution. However, the pending buffer is only initialized if the caller truly uses the int read() method to read single bytes. Otherwise, the code creates a ByteBuffer that is wrapping the caller provided target buffer, to encode the strings directly into it.

The CharBuffer follows the same concept, just for char sequences. In this code, it will always be a wrapper around one of the strings, rather than a buffer with a storage of its own. So in the best case, this InputStream implementation will encode all iterator provided strings into caller provided buffer(s), without intermediate storage.

This concept does already imply lazy processing, as without intermediate storage, only as much as fitting into the caller provided buffer, in other words, as much as requested by the caller, will be fetched from the iterator.


来源: stackexchange

问:
What does re.compile(r'[[]]') match?
With Python's re module, why do the following act differently:</p>

```<code>>>> r = re.compile(r'[][]')
>>> r.findall(r'[]')
['[', ']']
>>> r = re.compile(r'[[]]')
>>> r.findall(r'[]')
['[]']
>>> r.findall(r'][')
[]
</code>```

答:
Character classes begin with a [ and end with the first ].

So the expression [][] is a character class with the characters ] and [ as character classes must not be empty:  [][]

And the expression [[]] is a character class with just [ and the single character ] after that: [[]]

OwnerDisplayName: gray

ContentLicense: CC BY-SA 2.5

Title: What does re.compile(r'[[]]') match?

Body:
With Python's re module, why do the following act differently:

>>> r = re.compile(r'[][]')
>>> r.findall(r'[]')
['[', ']']
>>> r = re.compile(r'[[]]')
>>> r.findall(r'[]')
['[]']
>>> r.findall(r'][')
[]

LastEditorDisplayName: dbr

OwnerDisplayName: Gumbo

LastEditorDisplayName: Gumbo


问:
SSMS 2008 Add-In - Execute Query
I'm loading a sql script up to an SSMS 2008 add-in like so:

```<code>            ' create a new blank document
        ServiceCache.ScriptFactory.CreateNewBlankScript(Microsoft.SqlServer.Management.UI.VSIntegration.Editors.ScriptType.Sql)

        ' insert SQL statement to the blank document
        Dim doc As EnvDTE.TextDocument = CType(ServiceCache.ExtensibilityModel.Application.ActiveDocument.Object(Nothing), EnvDTE.TextDocument)
        doc.EndPoint.CreateEditPoint().Insert(_Output.ToString())
</code>```

Is there a way to automatically execute the statement as well?

Thanks,

Mark

答:
In SSMS 2008 R2 it would look like this:</p>

```<code>doc.DTE.ExecuteCommand("Query.Execute");
</code>```

Title: SSMS 2008 Add-In - Execute Query

Body:
I'm loading a sql script up to an SSMS 2008 add-in like so:

            ' create a new blank document
        ServiceCache.ScriptFactory.CreateNewBlankScript(Microsoft.SqlServer.Management.UI.VSIntegration.Editors.ScriptType.Sql)

        ' insert SQL statement to the blank document
        Dim doc As EnvDTE.TextDocument = CType(ServiceCache.ExtensibilityModel.Application.ActiveDocument.Object(Nothing), EnvDTE.TextDocument)
        doc.EndPoint.CreateEditPoint().Insert(_Output.ToString())

Is there a way to automatically execute the statement as well?

Thanks,

Mark

Body:
In SSMS 2008 R2 it would look like this:

doc.DTE.ExecuteCommand("Query.Execute");

ContentLicense: CC BY-SA 3.0


问:
I'm trying to auto populate text fields with a calculation of the user input of one field onto another in javascript
I'm looking to make a calculator that auto-fills a text field with a calculation of user input * 12.

The best way I can explain is user input * 12 = text input field result.

Code:

function add_number() {
  var monthlyPayment = parseInt(document.getElementById("monthlyPayment").value);
  var maintFees = parseInt(document.getElementById("maintFees").value);
  var memFees = parseInt(document.getElementById("memFees").value);
  var exchFees = parseInt(document.getElementById("exchFees").value);
  var result = monthlyPayment + maintFees + memFees + exchFees;

  document.getElementById("txtresult").value = result;

  function mpTwelve() {
    var monPay = parseInt(document.getElementById("monthlyPayment").value);
    var monTwe = monPay * 12;

    document.GetElementById("mpTimesTwelve").value = mpTimesTwelve;
  }
}
```

答:
You have some errors. Replace document.GetElementById("mpTimesTwelve").value = mpTimesTwelve; to document.getElementById("mpTimesTwelve").value = monTwe

See example in playground: https://jsfiddle.net/denisstukalov/vywb9mqg/5/#&togetherjs=2ICJ01oHQu

ContentLicense: CC BY-SA 4.0


问:
React: Having a button select something upon click a button
I currently have 4 buttons and 4 circles with 4 numbers each. When someone clicks button 1, circle 1 would be selected. When they click button 2, circle 2 would be selected. I currently have this:

```<code>class App extends Component {
  constructor() {
    super();
    this.state = {
      selButtonIdx: 1,
    };
  }

  handleButtonSelection = (selButtonIdx) => {
    this.setState({ selButtonIdx: selButtonIdx });
    console.log(selButtonIdx);
  };

  render() {
    return (
      <div className="App">
        <header className="App-header">Buttons</header>
        <main>
          <CircleSelector
            selButtonIdx={this.state.selButtonIdx}
            handleButtonSelection={this.handleButtonSelection}
          />
          <Circles />
        </main>
      </div>
    );
  }
}
</code>```

For my CircleSelector I have:

```<code>const CircleSelector = (props) => {
  return (
    <div className="CircleSelector">
      {[1, 2, 3, 4].map((index) => (
        <button
          type="button"
          onClick={() => props.handleButtonSelection(index)}
        >
          Select Button {index} {index === props.selButtonIdx && "(selected)"}
        </button>
      ))}
    </div>
  );
};
</code>```

and for circles:

```<code>const Circle = (props) => {
  return (
    <div className="Circles">
      {[1, 2, 3, 4].map((index) => (
        <div type="text">{index}</div>
      ))}
    </div>
  );
};
</code>```

答:
Yoou should integrate your circle and your button in the same component, this way you can simply use a reference to select the circle. I did the following example with input since i don't know what your circle contains but the idea is here :

```<code>import React, { Component } from "react";
import { render } from "react-dom";

const App = () => {
  return (
    <div>
      {[1, 2, 3, 4].map(el => (
        <MyComponent />
      ))}
    </div>
  );
};

const MyComponent = () => {
  const elRef = React.createRef();

  const selectElement = () => {
    elRef.current.select(); // Select the content of the input
  };
  return (
    <div>
      <button onClick={selectElement}>Select element</button>
      <input ref={elRef} type="text" value="element" />
    </div>
  );
};

render(<App />, document.getElementById("root"));
</code>```

Here is the repro on stackblitz

Body:
I currently have 4 buttons and 4 circles with 4 numbers each. When someone clicks button 1, circle 1 would be selected. When they click button 2, circle 2 would be selected. I currently have this:

class App extends Component {
  constructor() {
    super();
    this.state = {
      selButtonIdx: 1,
    };
  }

  handleButtonSelection = (selButtonIdx) => {
    this.setState({ selButtonIdx: selButtonIdx });
    console.log(selButtonIdx);
  };

  render() {
    return (

        Buttons

          <CircleSelector
            selButtonIdx={this.state.selButtonIdx}
            handleButtonSelection={this.handleButtonSelection}
          />
          <Circles />

    );
  }
}

For my CircleSelector I have:

const CircleSelector = (props) => {
  return (

      {[1, 2, 3, 4].map((index) => (
        <button
          type="button"
          onClick={() => props.handleButtonSelection(index)}
        >
          Select Button {index} {index === props.selButtonIdx && "(selected)"}
        </button>
      ))}

  );
};

and for circles:

const Circle = (props) => {
  return (

      {[1, 2, 3, 4].map((index) => (

{index}

      ))}

  );
};

Body:
Yoou should integrate your circle and your button in the same component, this way you can simply use a reference to select the circle. I did the following example with input since i don't know what your circle contains but the idea is here :

import React, { Component } from "react";
import { render } from "react-dom";

const App = () => {
  return (
    <div>
      {[1, 2, 3, 4].map(el => (
        <MyComponent />
      ))}
    </div>
  );
};

const MyComponent = () => {
  const elRef = React.createRef();

  const selectElement = () => {
    elRef.current.select(); // Select the content of the input
  };
  return (
    <div>
      <button onClick={selectElement}>Select element</button>
      <input ref={elRef} type="text" value="element" />
    </div>
  );
};

render(<App />, document.getElementById("root"));

Here is the repro on stackblitz


问:
Producer failures inside executeInTransaction
In my application
1) we are consuming from a topic (firstTopic) ,
2)  process the request by calling some 3rd party services  and  (We have a retryTemplate here for exponential backoff)
3)  submit to second topic (secondtopic).

We are also manually updating the offset after successful submission. KafkaTransactionManager is used to maintain the transactions. Since the message is posted to firstTopic through a RestController our @Transactional starts there and ends when the offset is updated. We are using executeInTransaction() for this.

KafkaConfiguration

@Bean
public ProducerFactory producerFactory() {
    Map<String, Object> config = new HashMap<>();
    config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
    config.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    config.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);
    config.put(ProducerConfig.RETRIES_CONFIG, 10);
    config.put(ProducerConfig.RETRY_BACKOFF_MS_CONFIG, 1000);
    DefaultKafkaProducerFactory<String, User> defaultKafkaProducerFactory = new DefaultKafkaProducerFactory<>(config);
    defaultKafkaProducerFactory.setTransactionIdPrefix("trans");
    //defaultKafkaProducerFactory.transactionCapable();
    return defaultKafkaProducerFactory;
    //return new DefaultKafkaProducerFactory<>(config);
}
```

<pre class="lang-java prettyprint-override"><code>
/**
 * New configuration for the consumerFactory added
 *
 * @return
 */
@Bean
public ConsumerFactory<String, User> consumerFactory() {
    Map<String, Object> config = new HashMap<>();
    config.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
    config.put(ConsumerConfig.GROUP_ID_CONFIG, "firstTopic-group");
    config.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
    config.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class);
    config.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);
    return new DefaultKafkaConsumerFactory<>(config, new StringDeserializer(), new JsonDeserializer<User>(User.class));
}

@Bean
public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, User>> kafkaListenerContainerFactory() {
    ConcurrentKafkaListenerContainerFactory<String, User> factory = new ConcurrentKafkaListenerContainerFactory<>();
    factory.setConsumerFactory(consumerFactory());
    factory.getContainerProperties().setAckMode(ContainerProperties.AckMode.MANUAL_IMMEDIATE);
    factory.getContainerProperties().setTransactionManager(transactionManager());
    factory.setRetryTemplate(kafkaRetry());
    factory.setStatefulRetry(true);
    factory.setErrorHandler(getErrorHandler());
    factory.setRecoveryCallback(retryContext -> {
        //implement the logic to decide the action after all retries are over.
        ConsumerRecord consumerRecord = (ConsumerRecord) retryContext.getAttribute("record");
        System.out.println("Recovery is called for message  " + consumerRecord.value());
        return Optional.empty();
    });

    return factory;
}

public RetryTemplate kafkaRetry() {
    RetryTemplate retryTemplate = new RetryTemplate();

    ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
    backOffPolicy.setInitialInterval(10 * 1000);
    backOffPolicy.setMultiplier(1);
    backOffPolicy.setMaxInterval(60 * 1000);       // original 25 * 60 * 1000
    retryTemplate.setBackOffPolicy(backOffPolicy);

    SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
    retryPolicy.setMaxAttempts(4);
    retryTemplate.setRetryPolicy(retryPolicy);
    return retryTemplate;
}

public SeekToCurrentErrorHandler getErrorHandler() {
    SeekToCurrentErrorHandler errorHandler = new SeekToCurrentErrorHandler() {

        @Override
        public void handle(Exception thrownException,
                           List<ConsumerRecord<?, ?>> records,
                           Consumer<?, ?> consumer,
                           MessageListenerContainer container) {
            //super.handle(thrownException, records, consumer, container);
            if (!records.isEmpty()) {
                ConsumerRecord<?, ?> record = records.get(0);
                String topic = record.topic();
                long offset = record.offset();
                int partition = record.partition();

                if (thrownException instanceof DeserializationException) {
                    System.out.println("------1111------deserialization exception ");
                } else {
                    System.out.println("------xxxx------Record is empty ");
                    consumer.seek(new TopicPartition(topic, partition), offset);
                }
            } else {
                System.out.println("------4444------Record is empty ");
            }

        }
    };

    return errorHandler;
}
</code>```

Rest Controller

@RestController
@RequestMapping("accounts")
public class UserResource {

    @Autowired
    KafkaTemplate<String, User> kafkaTemplate;

    @PostMapping("/users")
    @Transactional
    public String postComments(@RequestParam("firstName") final String firstName,
                               @RequestParam("lastName") final String lastName,
                               @RequestParam("userName") final String userName) {

        List<String> accountTypes = new ArrayList<String>();
        kafkaTemplate.send("firstTopic", new User(firstName, lastName, userName));

        return "Message sent to the Error queue";
    }

}
```

<p>service</p>

<pre class="lang-java prettyprint-override"><code>@Service
public class KafkaMessageConsumerService {

    @Autowired
    KafkaTemplate<String, User> kafkaTemplate;

    int index = 0;

    @KafkaListener(topics = "firstTopic", groupId = "firstTopic-group")
    //@Transactional
    public void onCustomerMessage(User user, Acknowledgment acknowledgment) throws Exception {

        kafkaTemplate.executeInTransaction(t -> {
            int number = (int) (Math.random() * 10);

            t.send("secondtopic", user);
            if (number % 2 == 0 || number % 2 == 1) {
                System.out.println("about to ack");
                throw new RuntimeException(" Modulus is zero ");
            }
            acknowledgment.acknowledge();

            return true;
        });
    }

}
</code>```

We have also added a producer retry (10) to the secondtopic. In executeInTransaction()  where we are  producing to secondtopic  we are testing producer failures by throwing a manual exception between the send to secondtopic and offset commit.  What we observe is that when the exception is thrown  (inside executeInTransaction()) it retries the  logic outside the executeInTransaction() and after 10 retries  throws this exception

2020-06-02 11:10:34.869 ERROR 685813 --- [ntainer#0-0-C-1] o.s.k.l.DefaultAfterRollbackProcessor    : Backoff FixedBackOff{interval=0, currentAttempts=10, maxAttempts=9} exhausted for ConsumerRecord(topic = firstTopic, partition = 0, leaderEpoch = 0, offset = 4, CreateTime = 1591114230710, serialized key size = -1, serialized value size = 61, headers = RecordHeaders(headers = [], isReadOnly = false), key = null, value = com.barade.sandesh.springKafka.model.User@4d4040ae)

org.springframework.kafka.listener.ListenerExecutionFailedException: Listener method 'public void com.barade.sandesh.springKafka.service.KafkaMessageConsumerService.onCustomerMessage(com.barade.sandesh.springKafka.model.User,org.springframework.kafka.support.Acknowledgment) throws java.lang.Exception' threw exception; nested exception is java.lang.RuntimeException:  Modulus is zero

Is this the correct way of addressing the producer failures ?

Is there a way to   capture the exception after the 10th retry and process the message according to business rule ? Can we keep 2 RetryTemplates and SeekToErrorHandlers ?

答:
You shouldn't be using executeInTransaction there (unless you really want to perform the publishing in a different transaction). The listener container has already started a transaction.

Furthermore, the acknowledge() sends the offsets to the container's transaction, not the one started by the template.

This could be why you are getting strange errors on the acknowledgment; I don't know.

Simply remove the executeInTransaction and the template will participate in the container's transaction.

Body:
In my application
1) we are consuming from a topic (firstTopic) ,
2)  process the request by calling some 3rd party services  and  (We have a retryTemplate here for exponential backoff)
3)  submit to second topic (secondtopic).

We are also manually updating the offset after successful submission. KafkaTransactionManager is used to maintain the transactions. Since the message is posted to firstTopic through a RestController our @Transactional starts there and ends when the offset is updated. We are using executeInTransaction() for this.

KafkaConfiguration

@Bean
public ProducerFactory producerFactory() {
    Map<String, Object> config = new HashMap<>();
    config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
    config.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    config.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);
    config.put(ProducerConfig.RETRIES_CONFIG, 10);
    config.put(ProducerConfig.RETRY_BACKOFF_MS_CONFIG, 1000);
    DefaultKafkaProducerFactory<String, User> defaultKafkaProducerFactory = new DefaultKafkaProducerFactory<>(config);
    defaultKafkaProducerFactory.setTransactionIdPrefix("trans");
    //defaultKafkaProducerFactory.transactionCapable();
    return defaultKafkaProducerFactory;
    //return new DefaultKafkaProducerFactory<>(config);
}

/**
 * New configuration for the consumerFactory added
 *
 * @return
 */
@Bean
public ConsumerFactory<String, User> consumerFactory() {
    Map<String, Object> config = new HashMap<>();
    config.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
    config.put(ConsumerConfig.GROUP_ID_CONFIG, "firstTopic-group");
    config.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
    config.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class);
    config.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);
    return new DefaultKafkaConsumerFactory<>(config, new StringDeserializer(), new JsonDeserializer<User>(User.class));
}

@Bean
public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, User>> kafkaListenerContainerFactory() {
    ConcurrentKafkaListenerContainerFactory<String, User> factory = new ConcurrentKafkaListenerContainerFactory<>();
    factory.setConsumerFactory(consumerFactory());
    factory.getContainerProperties().setAckMode(ContainerProperties.AckMode.MANUAL_IMMEDIATE);
    factory.getContainerProperties().setTransactionManager(transactionManager());
    factory.setRetryTemplate(kafkaRetry());
    factory.setStatefulRetry(true);
    factory.setErrorHandler(getErrorHandler());
    factory.setRecoveryCallback(retryContext -> {
        //implement the logic to decide the action after all retries are over.
        ConsumerRecord consumerRecord = (ConsumerRecord) retryContext.getAttribute("record");
        System.out.println("Recovery is called for message  " + consumerRecord.value());
        return Optional.empty();
    });

    return factory;
}

public RetryTemplate kafkaRetry() {
    RetryTemplate retryTemplate = new RetryTemplate();

    ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
    backOffPolicy.setInitialInterval(10 * 1000);
    backOffPolicy.setMultiplier(1);
    backOffPolicy.setMaxInterval(60 * 1000);       // original 25 * 60 * 1000
    retryTemplate.setBackOffPolicy(backOffPolicy);

    SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
    retryPolicy.setMaxAttempts(4);
    retryTemplate.setRetryPolicy(retryPolicy);
    return retryTemplate;
}

public SeekToCurrentErrorHandler getErrorHandler() {
    SeekToCurrentErrorHandler errorHandler = new SeekToCurrentErrorHandler() {

        @Override
        public void handle(Exception thrownException,
                           List<ConsumerRecord<?, ?>> records,
                           Consumer<?, ?> consumer,
                           MessageListenerContainer container) {
            //super.handle(thrownException, records, consumer, container);
            if (!records.isEmpty()) {
                ConsumerRecord<?, ?> record = records.get(0);
                String topic = record.topic();
                long offset = record.offset();
                int partition = record.partition();

                if (thrownException instanceof DeserializationException) {
                    System.out.println("------1111------deserialization exception ");
                } else {
                    System.out.println("------xxxx------Record is empty ");
                    consumer.seek(new TopicPartition(topic, partition), offset);
                }
            } else {
                System.out.println("------4444------Record is empty ");
            }

        }
    };

    return errorHandler;
}

Rest Controller

@RestController
@RequestMapping("accounts")
public class UserResource {

    @Autowired
    KafkaTemplate<String, User> kafkaTemplate;

    @PostMapping("/users")
    @Transactional
    public String postComments(@RequestParam("firstName") final String firstName,
                               @RequestParam("lastName") final String lastName,
                               @RequestParam("userName") final String userName) {

        List<String> accountTypes = new ArrayList<String>();
        kafkaTemplate.send("firstTopic", new User(firstName, lastName, userName));

        return "Message sent to the Error queue";
    }

}

service

@Service
public class KafkaMessageConsumerService {

    @Autowired
    KafkaTemplate<String, User> kafkaTemplate;

    int index = 0;

    @KafkaListener(topics = "firstTopic", groupId = "firstTopic-group")
    //@Transactional
    public void onCustomerMessage(User user, Acknowledgment acknowledgment) throws Exception {

        kafkaTemplate.executeInTransaction(t -> {
            int number = (int) (Math.random() * 10);

            t.send("secondtopic", user);
            if (number % 2 == 0 || number % 2 == 1) {
                System.out.println("about to ack");
                throw new RuntimeException(" Modulus is zero ");
            }
            acknowledgment.acknowledge();

            return true;
        });
    }

}

We have also added a producer retry (10) to the secondtopic. In executeInTransaction()  where we are  producing to secondtopic  we are testing producer failures by throwing a manual exception between the send to secondtopic and offset commit.  What we observe is that when the exception is thrown  (inside executeInTransaction()) it retries the  logic outside the executeInTransaction() and after 10 retries  throws this exception

2020-06-02 11:10:34.869 ERROR 685813 --- [ntainer#0-0-C-1] o.s.k.l.DefaultAfterRollbackProcessor    : Backoff FixedBackOff{interval=0, currentAttempts=10, maxAttempts=9} exhausted for ConsumerRecord(topic = firstTopic, partition = 0, leaderEpoch = 0, offset = 4, CreateTime = 1591114230710, serialized key size = -1, serialized value size = 61, headers = RecordHeaders(headers = [], isReadOnly = false), key = null, value = com.barade.sandesh.springKafka.model.User@4d4040ae)

org.springframework.kafka.listener.ListenerExecutionFailedException: Listener method 'public void com.barade.sandesh.springKafka.service.KafkaMessageConsumerService.onCustomerMessage(com.barade.sandesh.springKafka.model.User,org.springframework.kafka.support.Acknowledgment) throws java.lang.Exception' threw exception; nested exception is java.lang.RuntimeException:  Modulus is zero

Is this the correct way of addressing the producer failures ?

Is there a way to   capture the exception after the 10th retry and process the message according to business rule ? Can we keep 2 RetryTemplates and SeekToErrorHandlers ?


问:
Renaming multiple columns using their index
How can i rename multiple columns of a dataframe using their index?
For example i want to rename the columns at positions 5,6,7,8 to 'five','six','seven','eight' respectively. I don't want to enter the keys in the dictionary individually.</p>

答:
Building on the premise of your statement I don't want to enter the keys in the dictionary individually.:

You can use a module called num2words.

From the docs:

num2words is a library that converts numbers like 42 to words like forty-two.

Usage:

```<code>from num2words import num2words
num2words(10)
</code>```

Output:

```<code>ten
</code>```

Do pip install num2words

Your final code should be:

```<code>from num2words import num2words

d = {i: num2words(i) for i in range(100)}
df = df.rename(d, axis=1)
</code>```

Body:
Building on the premise of your statement I don't want to enter the keys in the dictionary individually.:

You can use a module called num2words.

From the docs:

num2words is a library that converts numbers like 42 to words like forty-two.

Usage:

from num2words import num2words
num2words(10)

Output:

ten

Do pip install num2words

Your final code should be:

from num2words import num2words

d = {i: num2words(i) for i in range(100)}
df = df.rename(d, axis=1)


问:
How do I put a p tag under two divs?
So I've two divs positioned next to each other via float left for the left div and float right for the right div. I want my p to appear underneath these two divs. However it just refuses to do that. It always appears at the top of the page or between the two divs. I've already tried setting the two divs to display inline block but then they don't stay next to each other and do a line-break instead. I'm relatively new to HTML and CSS so maybe its just a simple beginners fault but I would be gratefull if someone could help me out.

.p {
  background: red;
  width: 100%;
}

.tippsboxright {
  color: rgb(238, 238, 238);
  max-width: 45%;
  margin: 0 0 0 5%;
  float: right;
  font-size: 1.7vw;
  padding: 0;
}

.boxleft {
  max-width: 50%;
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><main>
  <header>
    <h1>My header</h1>
  </header>
  <div class="content">
    <div class="boxleft">
      <img src="../images/questionguy.png" alt="Typ mit fragen" class="imgkauftipps">
    </div>
    <div class="tippsboxright">
      <p>
        This is the right box with text inside of it.
      </p>
    </div>
    <p class="p">This is the p tag that I want to be at the bottom of the page.</p>
  </div>
</main></code>```

答:
You'll want to use CSS flexbox for this instead of float. Put the top divs in a wrapper/container and give the wrapper display: flex. That's basically it, but you can finesse it as I've done in this example:

.wrap {
  display: flex;
  justify-content: space-between;
}

.wrap div {
  width: 100%;
  outline: 1px solid red;
 }```
<pre class="snippet-code-html lang-html prettyprint-override"><code><div class="wrap">
  <div>left</div>
  <div>right</div>
</div>

<p>Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Me non paenitet nullum festiviorem excogitasse ad hoc. Quis aute iure reprehenderit in voluptate velit esse. Quam temere in vitiis, legem sancimus haerentia. Inmensae subtilitatis, obscuris et malesuada fames. Fictum, deserunt mollit anim laborum astutumque!</p></code>```

Or, using the html from your example, except I've moved the
 tag out of the wrapper. Does the
 have to be in the wrapper div?

.content {
  display: flex;
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><main>
  <header>
    <h1>My header</h1>
  </header>
  <div class="content">
    <div class="boxleft">
      <img src="../images/questionguy.png" alt="Typ mit fragen" class="imgkauftipps">
    </div>
    <div class="tippsboxright">
      <p>
        This is the right box with text inside of it.
      </p>
    </div>

  </div>
  <p class="p">This is the p tag that I want to be at the bottom of the page.</p>
</main></code>```

Title: How do I put a p tag under two divs?

Body:
So I've two divs positioned next to each other via float left for the left div and float right for the right div. I want my p to appear underneath these two divs. However it just refuses to do that. It always appears at the top of the page or between the two divs. I've already tried setting the two divs to display inline block but then they don't stay next to each other and do a line-break instead. I'm relatively new to HTML and CSS so maybe its just a simple beginners fault but I would be gratefull if someone could help me out.

.p {
  background: red;
  width: 100%;
}

.tippsboxright {
  color: rgb(238, 238, 238);
  max-width: 45%;
  margin: 0 0 0 5%;
  float: right;
  font-size: 1.7vw;
  padding: 0;
}

.boxleft {
  max-width: 50%;
}

My header

        This is the right box with text inside of it.

This is the p tag that I want to be at the bottom of the page.

Body:
You'll want to use CSS flexbox for this instead of float. Put the top divs in a wrapper/container and give the wrapper display: flex. That's basically it, but you can finesse it as I've done in this example:

.wrap {
  display: flex;
  justify-content: space-between;
}

.wrap div {
  width: 100%;
  outline: 1px solid red;
 }

<div class="wrap">
  <div>left</div>
  <div>right</div>
</div>

<p>Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Me non paenitet nullum festiviorem excogitasse ad hoc. Quis aute iure reprehenderit in voluptate velit esse. Quam temere in vitiis, legem sancimus haerentia. Inmensae subtilitatis, obscuris et malesuada fames. Fictum, deserunt mollit anim laborum astutumque!</p>

Or, using the html from your example, except I've moved the <p> tag out of the wrapper. Does the <p> have to be in the wrapper div?

.content {
  display: flex;
}

<main>
  <header>
    <h1>My header</h1>
  </header>
  <div class="content">
    <div class="boxleft">
      <img src="../images/questionguy.png" alt="Typ mit fragen" class="imgkauftipps">
    </div>
    <div class="tippsboxright">
      <p>
        This is the right box with text inside of it.
      </p>
    </div>

  </div>
  <p class="p">This is the p tag that I want to be at the bottom of the page.</p>
</main>


问:
CSV file to JSON using python
I am currently trying to convert a csv with 4 different fields to a json body for making an api call. The current csv looks like this:

```<code>firstname, lastname, email, login
Jake, Smith, jake.smith@example.com, jake.smith@example.com
John, Appleseed, john.appleseed@example.com, john.appleseed@example.com
</code>```

I would like the json to look like this

```<code>{"profile": {"firstName": "Jake", "lastName": "Smith", "email": "jake.smith@example.com", "login": "jake.smith@example.com"}}
</code>```

```<code>{"profile": {"firstName": "John", "lastName": "Appleseed", "email": "john.appleseed@example.com", "login": "john.appleseed@example.com"}}
</code>```

答:
import csv and import json packages

Create a file path to your CSV file: csvFilePath =
‘csv_file_name.csv’

Create a file path to your JSON file: jsonFilePath =
‘json_file_name.json’

Create an empty dictionary to store your converted JSON data: data =
{}

Open and read the CSV:

Open and read CSV file

Create a JSON file with the file path from step 3 to write converted data on

Create new JSON file and write on it

Final code snippet

Final code example

Reference : https://medium.com/@hannah15198/convert-csv-to-json-with-python-b8899c722f6d

Title: CSV file to JSON using python

Body:
I am currently trying to convert a csv with 4 different fields to a json body for making an api call. The current csv looks like this:

firstname, lastname, email, login
Jake, Smith, jake.smith@example.com, jake.smith@example.com
John, Appleseed, john.appleseed@example.com, john.appleseed@example.com

I would like the json to look like this

{"profile": {"firstName": "Jake", "lastName": "Smith", "email": "jake.smith@example.com", "login": "jake.smith@example.com"}}

{"profile": {"firstName": "John", "lastName": "Appleseed", "email": "john.appleseed@example.com", "login": "john.appleseed@example.com"}}


问:
How to constrain two columns in SQL to have a value in one column only be allowed to appear in combination with the value in the other column?
I have two columns in a table:

- For one row all two columns need to be filled.

- In col1 any ID is allowed.

- In col2 a specific ID once set is only allowed to appear for further rows in combination with the ID in col1 when it was set for the first time.

Example:

```<code> col1 | col2
------|------
   1  |  1        OK
   1  |  1        OK
   1  |  2        OK
   2  |  3        OK
   2  |  1        NOK because 1 in col2 is already associated with 1 in col1.
   1  |  3        NOK because 3 in col2 is already associated with 2 in col1.
</code>```

I want to apply this rule to a table using a constraint. (Without having to use a second table)

Can someone help me? Thanks!

答:
You can use an EXCLUDE constraint to do this.  you would want to exclude cases where col2 are = but col1 is <>.</p>

```<code>create extension btree_gist;

create table foobar (col1 int not null, col2 int not null);

alter table foobar add constraint foobar_ex exclude using gist
   (col2 with =, col1 with <>);
</code>```

Body:
I have two columns in a table:

- For one row all two columns need to be filled.

- In col1 any ID is allowed.

- In col2 a specific ID once set is only allowed to appear for further rows in combination with the ID in col1 when it was set for the first time.

Example:

 col1 | col2
------|------
   1  |  1        OK
   1  |  1        OK
   1  |  2        OK
   2  |  3        OK
   2  |  1        NOK because 1 in col2 is already associated with 1 in col1.
   1  |  3        NOK because 3 in col2 is already associated with 2 in col1.

I want to apply this rule to a table using a constraint. (Without having to use a second table)

Can someone help me? Thanks!

Body:
You can use an EXCLUDE constraint to do this.  you would want to exclude cases where col2 are = but col1 is <>.

create extension btree_gist;

create table foobar (col1 int not null, col2 int not null);

alter table foobar add constraint foobar_ex exclude using gist
   (col2 with =, col1 with <>);


问:
Laravel: Display image from public storage
I am saving an uploaded image like this:

```<code>$profile_picture = $request->file('profile_picture');
$user->profile_picture = $profile_picture->getClientOriginalName();
Storage::disk('public')->put( $user->profile_picture, $request->file('profile_picture') );
</code>```

and then I am trying to display that image in HTML with:

```<code><img class="img-fluid" src="{{ asset('storage/'. Auth::user()->profile_picture) }}">
</code>```

the image is being saved in storage/app/public, but the image is not showing in html, how do i go about this?

thank you

答:
You may use response method on a Laravel filesystem

return Storage::disk('public')->response($user->profile_picture);
```

N.B: In order to do that, you need a Laravel route. The above code should be located in a controller method

Body:
I am saving an uploaded image like this:

$profile_picture = $request->file('profile_picture');
$user->profile_picture = $profile_picture->getClientOriginalName();
Storage::disk('public')->put( $user->profile_picture, $request->file('profile_picture') );

and then I am trying to display that image in HTML with:

profile_picture) }}">

the image is being saved in storage/app/public, but the image is not showing in html, how do i go about this?

thank you

Body:
You may use response method on a Laravel filesystem

return Storage::disk('public')->response($user->profile_picture);

N.B: In order to do that, you need a Laravel route. The above code should be located in a controller method


问:
Tuple into list to find max value and if there is a duplicate
I am creating a function that takes any quantity of numbers, and tell you what is the max value or if there is a tie for largest. I am wondering what I could do to simplify what I have.

```<code>def max_num(*args):
  nums = []
  nums_1 = []
  nums.append(args)
  i = 0
  while i < len(nums[0]):
    nums_1.append(nums[0][i])
    i += 1
  c = max(nums_1)
  nums_1.remove(c)
  if c in nums_1:
    print("It's a tie!")
  else:
    print(c)

max_num(-10, 0, 10, 10)

</code>```

So when I initially make a list with the arguments given, it gives me a tuple inside the list. This is why I create a new list to dissect the tuple into separate values. I have the feeling that wasn't necessary, and that there is a much simpler way to do this. Any advice would be great.

答:
Just get the max, and count how many times it appears in your data:</p>

```<code>def max_num(*args):
    maxi = max(args)
    if args.count(maxi) == 1:
        print(maxi)
    else:
        print('Tie')

max_num(2, 5, 1)
#5
max_num(2, 5, 1, 5)
#Tie
</code>```

Body:
I am creating a function that takes any quantity of numbers, and tell you what is the max value or if there is a tie for largest. I am wondering what I could do to simplify what I have.

def max_num(*args):
  nums = []
  nums_1 = []
  nums.append(args)
  i = 0
  while i < len(nums[0]):
    nums_1.append(nums[0][i])
    i += 1
  c = max(nums_1)
  nums_1.remove(c)
  if c in nums_1:
    print("It's a tie!")
  else:
    print(c)

max_num(-10, 0, 10, 10)

So when I initially make a list with the arguments given, it gives me a tuple inside the list. This is why I create a new list to dissect the tuple into separate values. I have the feeling that wasn't necessary, and that there is a much simpler way to do this. Any advice would be great.

Body:
Just get the max, and count how many times it appears in your data:

def max_num(*args):
    maxi = max(args)
    if args.count(maxi) == 1:
        print(maxi)
    else:
        print('Tie')

max_num(2, 5, 1)
#5
max_num(2, 5, 1, 5)
#Tie


问:
Undefined references to symbols in standard libraries with musl-cross-make
I am using a musl-targeting cross compiler for arm, built with musl-cross-make (gcc 9.2.0, musl 1.2.0). When I compile a simple hello world c program with printf, I get undefined references for symbols that are in the standard library:

```<code>cross-musl/bin/arm-linux-musleabihf-gcc -c hello.c -o hello.o
cross-musl/bin/arm-linux-musleabihf-ld  hello.o -o hello.elf
cross-musl/bin/arm-linux-musleabihf-ld: warning: cannot find entry symbol _start; defaulting to 0000000000010074
cross-musl/bin/arm-linux-musleabihf-ld: hello.o: in function `main':
hello.c:(.text+0x18): undefined reference to `puts'
</code>```

When I add libc.a and crt1.o to the linker command, I get no error:

```<code>cross-musl/bin/arm-linux-musleabihf-gcc -c hello.c -o hello.o
cross-musl/bin/arm-linux-musleabihf-ld -Lcross-musl/arm-linux-musleabihf/lib -lc cross-musl/arm-linux-musleabihf/lib/crt1.o hello.o -o hello.elf
</code>```

I thought that it's not necessary to specify standard libraries and startup files when -nostartfiles, -nostdlib or -nodefaultlibs is not used, or am I wrong ?

答:
@old_timer Thanks, you are right. When I start gcc instead of ld to generate the executable, I have no unresolved externals:</p>

```<code>arm-linux-musleabihf-gcc -c hello.c -o hello.o
arm-linux-musleabihf-gcc -o hello.elf  hello.o
</code>```

Body:
I am using a musl-targeting cross compiler for arm, built with musl-cross-make (gcc 9.2.0, musl 1.2.0). When I compile a simple hello world c program with printf, I get undefined references for symbols that are in the standard library:

cross-musl/bin/arm-linux-musleabihf-gcc -c hello.c -o hello.o
cross-musl/bin/arm-linux-musleabihf-ld  hello.o -o hello.elf
cross-musl/bin/arm-linux-musleabihf-ld: warning: cannot find entry symbol _start; defaulting to 0000000000010074
cross-musl/bin/arm-linux-musleabihf-ld: hello.o: in function `main':
hello.c:(.text+0x18): undefined reference to `puts'

When I add libc.a and crt1.o to the linker command, I get no error:

cross-musl/bin/arm-linux-musleabihf-gcc -c hello.c -o hello.o
cross-musl/bin/arm-linux-musleabihf-ld -Lcross-musl/arm-linux-musleabihf/lib -lc cross-musl/arm-linux-musleabihf/lib/crt1.o hello.o -o hello.elf

I thought that it's not necessary to specify standard libraries and startup files when -nostartfiles, -nostdlib or -nodefaultlibs is not used, or am I wrong ?

Body:
@old_timer Thanks, you are right. When I start gcc instead of ld to generate the executable, I have no unresolved externals:

arm-linux-musleabihf-gcc -c hello.c -o hello.o
arm-linux-musleabihf-gcc -o hello.elf  hello.o


问:
Optimize the code
I am new to python. I would like all the python gurus to suggest some ways to make the following code more pythonic and hence more efficient .
Its a simple code to find the edit distance between two words.

P.S. I would like improvements in code, not in the logic or algorithm optimization.

```<code>class test:
    def __init__(self,a,b,I=1,D=1,R=1):
        self.a = a
        self.b = b
        self.mem = dict()
        self.la = len(a)
        self.lb = len(b)
        self.I = I
        self.D = D
        self.R = R

    def diff(self,i=0,j=0):
        T = self.diff
        memo = self.mem
        if j == self.lb: return self.D * i
        if i == self.la: return self.D * j
        if (i,j) in memo:
            return memo[(i,j)]
        if self.a[i] == self.b[j]:
            memo[(i,j)] = T( i+1,j+1 )
            return memo[ (i,j) ]
        memo[(i,j)] = min(self.R + T(i+1,j+1) , self.D + T(i+1,j) , self.I + T(i,j+1) ,
         self.D + T(i,j+1) , self.I + T(i+1,j) )
        return memo[(i,j)]
</code>```

Variable explanation:

a,b are two string whose edit distance is to be found.
I,D,R Insertion Deletion and Replace cost of a single letter.
mem is dictionary used to memoize the recursive calls.
i and j are the pointers of the string a and b respectively

答:
I think the code is quite pythonic, but as stated in the comments, it is a very bad idea to choose generic single letter variable words, and renders your code very hard to understand, and that is not pythonic.

This applies to every programming language, but in some cases even more so in python and other dynamically typed languages.

Try to be really generous and descriptive with variable names.

See the example (under "What is the meaning of 'Coding Horror'?") in this site creators blog.

Title: Optimize the code

Body:
I am new to python. I would like all the python gurus to suggest some ways to make the following code more pythonic and hence more efficient .
Its a simple code to find the edit distance between two words.

P.S. I would like improvements in code, not in the logic or algorithm optimization.

class test:
    def __init__(self,a,b,I=1,D=1,R=1):
        self.a = a
        self.b = b
        self.mem = dict()
        self.la = len(a)
        self.lb = len(b)
        self.I = I
        self.D = D
        self.R = R

    def diff(self,i=0,j=0):
        T = self.diff
        memo = self.mem
        if j == self.lb: return self.D * i
        if i == self.la: return self.D * j
        if (i,j) in memo:
            return memo[(i,j)]
        if self.a[i] == self.b[j]:
            memo[(i,j)] = T( i+1,j+1 )
            return memo[ (i,j) ]
        memo[(i,j)] = min(self.R + T(i+1,j+1) , self.D + T(i+1,j) , self.I + T(i,j+1) ,
         self.D + T(i,j+1) , self.I + T(i+1,j) )
        return memo[(i,j)]

Variable explanation:

a,b are two string whose edit distance is to be found.
I,D,R Insertion Deletion and Replace cost of a single letter.
mem is dictionary used to memoize the recursive calls.
i and j are the pointers of the string a and b respectively


问:
Extract images in a file and the filename as label
I have a question when learning a machine learning question. I have a file named Real World, and inside it, there are 60 files named like clocks,  Bed, Bike. And all of these 60 files contain images in this category. So I want to get all these images into python(maybe as a NumPy array) to analyze and make the file name like "clock" as the label of images in this file.  Is there any way to do that? </p>

答:
You can do this using functions in the inbuilt os library.
The below code gives you the images as a list of NumPy arrays, and label names in a list.

```<code> import os
dir="/dir"
classes=[clas.name for clas in os.scandir(dir) if clas.is_dir()]

X,y=[],[]
for clas in classes:
    path=os.path.join(dir,clas)
    for img in os.listdir(path):
        X.append(plt.imread(os.path.join(path,img)))
        y.append(str(clas))
</code>```

Body:
You can do this using functions in the inbuilt os library.
The below code gives you the images as a list of NumPy arrays, and label names in a list.

 import os
dir="/dir"
classes=[clas.name for clas in os.scandir(dir) if clas.is_dir()]

X,y=[],[]
for clas in classes:
    path=os.path.join(dir,clas)
    for img in os.listdir(path):
        X.append(plt.imread(os.path.join(path,img)))
        y.append(str(clas))


问:
Currency convertor using two functions
I am trying to make a currency convertor using two functions in one program but for some reason my program either gets this error:

```<code>line 23, in Input1
    print(amount+"Won equals to "+totalamount+" USD")
TypeError: unsupported operand type(s) for +: 'float' and 'str'
</code>```

Or does not print anything after putting the 'Option' and 'Amount'. This is my program:

```<code>print("Please choose which currency you want to convert:")
print("A - Korean Won to US Dollar (Exchange Rate: 0.000905)")
print("B - Korean Won to Euro (Exchange Rate: 0.000807350908)")
print("C - Korean Won to Japanese Yen (Exchange Rate: 0.0919061643)")
print("D - Korean Won to Chinese RMB (Exchange Rate: 0.00603703605)")
print("E - Quit")

usd = 0.000905
eur = 0.000807350908
yen = 0.0919061643
rmb = 0.00603703605

def main():
    option =input("Enter your option: ")
    if option== "E":
        exit()
    amount =float(input("Enter the amoutn in Korean Won: "))
    Input1(option, amount)

def Input1(option,amount):
    if option == "A":
        totalamount = (amount * usd)
        print(amount+"Won equals to "+totalamount+" USD")
    elif option== "B":
        totalamount = (amount * eur)
        print (amount+"Won equals to "+totalamount+" Euro")
    elif option== "C":
        totalamount = (amount * yen)
        print (amount+"Won equals to "+totalamount+" Yen")
    elif option== "D":
        totalamount = (amount * rmb)
        print (amount+"Won equals to "+totalamount+" Chinese RMB")
    else:
            print("You entered an invalid input.")
    return option,amount

main()
</code>```

Can you guys give me tips to fix my program? I am still learning python and any help would be greatly appreciated. Thank you so much!

答:
You have to convert amount and totalamount to a string before using it in print() as string with +. You could use the str() method.

```<code>print(str(amount)+"Won equals to "+str(totalamount)+" USD")
</code>```

Title: Currency convertor using two functions

Body:
I am trying to make a currency convertor using two functions in one program but for some reason my program either gets this error:

line 23, in Input1
    print(amount+"Won equals to "+totalamount+" USD")
TypeError: unsupported operand type(s) for +: 'float' and 'str'

Or does not print anything after putting the 'Option' and 'Amount'. This is my program:

print("Please choose which currency you want to convert:")
print("A - Korean Won to US Dollar (Exchange Rate: 0.000905)")
print("B - Korean Won to Euro (Exchange Rate: 0.000807350908)")
print("C - Korean Won to Japanese Yen (Exchange Rate: 0.0919061643)")
print("D - Korean Won to Chinese RMB (Exchange Rate: 0.00603703605)")
print("E - Quit")

usd = 0.000905
eur = 0.000807350908
yen = 0.0919061643
rmb = 0.00603703605

def main():
    option =input("Enter your option: ")
    if option== "E":
        exit()
    amount =float(input("Enter the amoutn in Korean Won: "))
    Input1(option, amount)

def Input1(option,amount):
    if option == "A":
        totalamount = (amount * usd)
        print(amount+"Won equals to "+totalamount+" USD")
    elif option== "B":
        totalamount = (amount * eur)
        print (amount+"Won equals to "+totalamount+" Euro")
    elif option== "C":
        totalamount = (amount * yen)
        print (amount+"Won equals to "+totalamount+" Yen")
    elif option== "D":
        totalamount = (amount * rmb)
        print (amount+"Won equals to "+totalamount+" Chinese RMB")
    else:
            print("You entered an invalid input.")
    return option,amount

main()

Can you guys give me tips to fix my program? I am still learning python and any help would be greatly appreciated. Thank you so much!

Body:
You have to convert amount and totalamount to a string before using it in print() as string with +. You could use the str() method.

print(str(amount)+"Won equals to "+str(totalamount)+" USD")


答:
Use an f-string instead of string concatenation; it'll handle the conversion for you!

```<code>print(f"{amount} won equals to {totalamount} USD")
</code>```

If you did want to do the conversion yourself, it'd look like this:

```<code>print(str(amount) + "Won equals to " + str(totalamount) + " USD")
</code>```

(edit) to answer the question asked in comments, change your main() function to:

```<code>def main():
    while True:
        option = input("Enter your option: ")
        if option== "E":
            break
        amount = float(input("Enter the amount in Korean Won: "))
        Input1(option, amount)
</code>```

Title: Currency convertor using two functions

Body:
Use an f-string instead of string concatenation; it'll handle the conversion for you!

print(f"{amount} won equals to {totalamount} USD")

If you did want to do the conversion yourself, it'd look like this:

print(str(amount) + "Won equals to " + str(totalamount) + " USD")

(edit) to answer the question asked in comments, change your main() function to:

def main():
    while True:
        option = input("Enter your option: ")
        if option== "E":
            break
        amount = float(input("Enter the amount in Korean Won: "))
        Input1(option, amount)


问:
Need help deleting/removing kivy widgets
The code below makes an app with a big button which makes and appends individual "rows" of widgets (really just 3 labels and a button in a  single gridlayout, representing a single widget)

to the bottom of a scrollview.

I want some way to press the 'delete this' button in a row to delete that particular row.

I imagine I'd need some way to differentiate between these generated rows somehow but I haven't gotten that figured out yet either.
I'd be extatic if someone had an elegant solution for this, as I've been stuck here for quite some time.

screenshot of dummy program

```<code>from kivy.app import App
from kivy.lang import Builder
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.button import Button
from kivy.uix.label import Label

kv = '''
<Label>:
    font_size: 35

<delete_btn>:
    background_color: 1, 0, 0, 1
    size_hint_y: None
    text: 'Delete this'
    font_size: 25

<RootWidget>:
    orientation: 'vertical'

    Button:
        size_hint: 1, .25
        font_size: 50
        pos_hint: {'x': 0, 'top': 1}
        text: 'append row'
        on_press: rootgrid.append_row()

    ScrollviewLayout:
        scroll_type: ['bars', 'content']
        bar_width: 10
        pos_hint: {'x': 0, 'y': .2}

        RootGridLayout:
            id: rootgrid
            cols: 1
            size_hint_y: None
            height: self.minimum_height

'''

class GridRow(GridLayout):
    def __init__(self, **kwargs):
        super(GridRow, self).__init__(**kwargs)
        self.size_hint_y = None

        self.cols = 4
        self.add_widget(delete_btn())
        self.add_widget(Label(text='00:00:00', size_hint_y=None))
        self.add_widget(Label(text='00:00:00', size_hint_y=None))
        self.add_widget(Label(text='00:00:00', size_hint_y=None))

class RootGridLayout(GridLayout):
    def append_row(self):
        self.add_widget(GridRow())

class delete_btn(Button):
    pass

class ScrollviewLayout(ScrollView):
    pass

class RootWidget(BoxLayout):
    pass

class remtimeApp(App):
    def build(self):
        root = RootWidget()
        return root

if __name__ == '__main__':
    Builder.load_string(kv)
    remtimeApp().run()

</code>```

答:
I am not sure whether it is important that the setup of a GridRow happens in Python but if not you can also solve your problem inside the KV language. You only need to change the code for the GridRow class:

```<code><Label>:
    font_size: 35

<GridRow>:
    id: row
    cols: 4
    size_hint_y: None
    Label:
        size_hint_y: None
        text: '00:00:00'
    Label:
        size_hint_y: None
        text: '00:00:00'
    Label:
        size_hint_y: None
        text: '00:00:00'
    Button:
        background_color: 1, 0, 0, 1
        size_hint_y: None
        text: 'Delete this'
        font_size: 25
        on_press: row.parent.remove_widget(row)

<RootWidget>:
    orientation: 'vertical'

    Button:
        size_hint: 1, .25
        font_size: 50
        pos_hint: {'x': 0, 'top': 1}
        text: 'append row'
        on_press: rootgrid.append_row()

    ScrollviewLayout:
        scroll_type: ['bars', 'content']
        bar_width: 10
        pos_hint: {'x': 0, 'y': .2}

        RootGridLayout:
            id: rootgrid
            cols: 1
            size_hint_y: None
            height: self.minimum_height
</code>```

With this the delete_btn class and the GridRow.__init__ function are not needed anymore.

Body:
The code below makes an app with a big button which makes and appends individual "rows" of widgets (really just 3 labels and a button in a  single gridlayout, representing a single widget)

to the bottom of a scrollview.

I want some way to press the 'delete this' button in a row to delete that particular row.

I imagine I'd need some way to differentiate between these generated rows somehow but I haven't gotten that figured out yet either.
I'd be extatic if someone had an elegant solution for this, as I've been stuck here for quite some time.

screenshot of dummy program

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.button import Button
from kivy.uix.label import Label

kv = '''
:
    font_size: 35

<delete_btn>:
    background_color: 1, 0, 0, 1
    size_hint_y: None
    text: 'Delete this'
    font_size: 25

<RootWidget>:
    orientation: 'vertical'

    Button:
        size_hint: 1, .25
        font_size: 50
        pos_hint: {'x': 0, 'top': 1}
        text: 'append row'
        on_press: rootgrid.append_row()

    ScrollviewLayout:
        scroll_type: ['bars', 'content']
        bar_width: 10
        pos_hint: {'x': 0, 'y': .2}

        RootGridLayout:
            id: rootgrid
            cols: 1
            size_hint_y: None
            height: self.minimum_height

'''

class GridRow(GridLayout):
    def __init__(self, **kwargs):
        super(GridRow, self).__init__(**kwargs)
        self.size_hint_y = None

        self.cols = 4
        self.add_widget(delete_btn())
        self.add_widget(Label(text='00:00:00', size_hint_y=None))
        self.add_widget(Label(text='00:00:00', size_hint_y=None))
        self.add_widget(Label(text='00:00:00', size_hint_y=None))

class RootGridLayout(GridLayout):
    def append_row(self):
        self.add_widget(GridRow())

class delete_btn(Button):
    pass

class ScrollviewLayout(ScrollView):
    pass

class RootWidget(BoxLayout):
    pass

class remtimeApp(App):
    def build(self):
        root = RootWidget()
        return root

if __name__ == '__main__':
    Builder.load_string(kv)
    remtimeApp().run()

Body:
I am not sure whether it is important that the setup of a GridRow happens in Python but if not you can also solve your problem inside the KV language. You only need to change the code for the GridRow class:

<Label>:
    font_size: 35

<GridRow>:
    id: row
    cols: 4
    size_hint_y: None
    Label:
        size_hint_y: None
        text: '00:00:00'
    Label:
        size_hint_y: None
        text: '00:00:00'
    Label:
        size_hint_y: None
        text: '00:00:00'
    Button:
        background_color: 1, 0, 0, 1
        size_hint_y: None
        text: 'Delete this'
        font_size: 25
        on_press: row.parent.remove_widget(row)

<RootWidget>:
    orientation: 'vertical'

    Button:
        size_hint: 1, .25
        font_size: 50
        pos_hint: {'x': 0, 'top': 1}
        text: 'append row'
        on_press: rootgrid.append_row()

    ScrollviewLayout:
        scroll_type: ['bars', 'content']
        bar_width: 10
        pos_hint: {'x': 0, 'y': .2}

        RootGridLayout:
            id: rootgrid
            cols: 1
            size_hint_y: None
            height: self.minimum_height

With this the delete_btn class and the GridRow.__init__ function are not needed anymore.


问:
Convert object to byte array and vice-versa in python
I use Python and send and receive packets to/from external c++ server (so pickle can't help) and need to serialize and deserialize python objects to/from byte arrays.

Serialize is easy:

```<code>class MsgBase(Structure):
    def __init__(self, my_type):
        self.m_type = my_type

    def get_buffer(self):
        return bytearray(self)

    _pack_ = 1
    _fields_ = [
        ('m_type', c_uint8)]

message = MsgBase(1)
arr = message.get_buffer()
</code>```

But how to deserialize this buffer back?

need something like this:

```<code>message = MsgBase(0)
message.deserialize_from_bytearray(arr)
</code>```

答:
I found solution:

message = MsgBase.from_buffer(arr)

Body:
I use Python and send and receive packets to/from external c++ server (so pickle can't help) and need to serialize and deserialize python objects to/from byte arrays.

Serialize is easy:

class MsgBase(Structure):
    def __init__(self, my_type):
        self.m_type = my_type

    def get_buffer(self):
        return bytearray(self)

    _pack_ = 1
    _fields_ = [
        ('m_type', c_uint8)]

message = MsgBase(1)
arr = message.get_buffer()

But how to deserialize this buffer back?

need something like this:

message = MsgBase(0)
message.deserialize_from_bytearray(arr)


问:
How can I send large messages with Kafka (over 15MB)?
I send String-messages to Kafka V. 0.8 with the Java Producer API.
If the message size is about 15 MB I get a MessageSizeTooLargeException.
I have tried to set message.max.bytesto 40 MB, but I still get the exception. Small messages worked without problems.

(The exception appear in the producer, I don't have a consumer in this application.)

What can I do to get rid of this exception?

My example producer config

```<code>private ProducerConfig kafkaConfig() {
    Properties props = new Properties();
    props.put("metadata.broker.list", BROKERS);
    props.put("serializer.class", "kafka.serializer.StringEncoder");
    props.put("request.required.acks", "1");
    props.put("message.max.bytes", "" + 1024 * 1024 * 40);
    return new ProducerConfig(props);
}
</code>```

Error-Log:

```<code>4709 [main] WARN  kafka.producer.async.DefaultEventHandler  - Produce request with correlation id 214 failed due to [datasift,0]: kafka.common.MessageSizeTooLargeException
4869 [main] WARN  kafka.producer.async.DefaultEventHandler  - Produce request with    correlation id 217 failed due to [datasift,0]: kafka.common.MessageSizeTooLargeException
5035 [main] WARN  kafka.producer.async.DefaultEventHandler  - Produce request with   correlation id 220 failed due to [datasift,0]: kafka.common.MessageSizeTooLargeException
5198 [main] WARN  kafka.producer.async.DefaultEventHandler  - Produce request with correlation id 223 failed due to [datasift,0]: kafka.common.MessageSizeTooLargeException
5305 [main] ERROR kafka.producer.async.DefaultEventHandler  - Failed to send requests for topics datasift with correlation ids in [213,224]

kafka.common.FailedToSendMessageException: Failed to send messages after 3 tries.
at kafka.producer.async.DefaultEventHandler.handle(Unknown Source)
at kafka.producer.Producer.send(Unknown Source)
at kafka.javaapi.producer.Producer.send(Unknown Source)
</code>```

答:
For people using landoop kafka:
You can pass the config values in the environment variables like:

```<code>docker run -d --rm -p 2181:2181 -p 3030:3030 -p 8081-8083:8081-8083  -p 9581-9585:9581-9585 -p 9092:9092
 -e KAFKA_TOPIC_MAX_MESSAGE_BYTES=15728640 -e KAFKA_REPLICA_FETCH_MAX_BYTES=15728640  landoop/fast-data-dev:latest `
</code>```

This sets topic.max.message.bytes and replica.fetch.max.bytes on the broker.

And if you're using rdkafka then pass the message.max.bytes in the producer config like:

```<code>  const producer = new Kafka.Producer({
        'metadata.broker.list': 'localhost:9092',
        'message.max.bytes': '15728640',
        'dr_cb': true
    });
</code>```

Similarly, for the consumer,

```<code>  const kafkaConf = {
   "group.id": "librd-test",
   "fetch.message.max.bytes":"15728640",
   ... .. }
</code>```

Body:
I send String-messages to Kafka V. 0.8 with the Java Producer API.
If the message size is about 15 MB I get a MessageSizeTooLargeException.
I have tried to set message.max.bytesto 40 MB, but I still get the exception. Small messages worked without problems.

(The exception appear in the producer, I don't have a consumer in this application.)

What can I do to get rid of this exception?

My example producer config

private ProducerConfig kafkaConfig() {
    Properties props = new Properties();
    props.put("metadata.broker.list", BROKERS);
    props.put("serializer.class", "kafka.serializer.StringEncoder");
    props.put("request.required.acks", "1");
    props.put("message.max.bytes", "" + 1024 * 1024 * 40);
    return new ProducerConfig(props);
}

Error-Log:

4709 [main] WARN  kafka.producer.async.DefaultEventHandler  - Produce request with correlation id 214 failed due to [datasift,0]: kafka.common.MessageSizeTooLargeException
4869 [main] WARN  kafka.producer.async.DefaultEventHandler  - Produce request with    correlation id 217 failed due to [datasift,0]: kafka.common.MessageSizeTooLargeException
5035 [main] WARN  kafka.producer.async.DefaultEventHandler  - Produce request with   correlation id 220 failed due to [datasift,0]: kafka.common.MessageSizeTooLargeException
5198 [main] WARN  kafka.producer.async.DefaultEventHandler  - Produce request with correlation id 223 failed due to [datasift,0]: kafka.common.MessageSizeTooLargeException
5305 [main] ERROR kafka.producer.async.DefaultEventHandler  - Failed to send requests for topics datasift with correlation ids in [213,224]

kafka.common.FailedToSendMessageException: Failed to send messages after 3 tries.
at kafka.producer.async.DefaultEventHandler.handle(Unknown Source)
at kafka.producer.Producer.send(Unknown Source)
at kafka.javaapi.producer.Producer.send(Unknown Source)

Body:
For people using landoop kafka:
You can pass the config values in the environment variables like:

docker run -d --rm -p 2181:2181 -p 3030:3030 -p 8081-8083:8081-8083  -p 9581-9585:9581-9585 -p 9092:9092
 -e KAFKA_TOPIC_MAX_MESSAGE_BYTES=15728640 -e KAFKA_REPLICA_FETCH_MAX_BYTES=15728640  landoop/fast-data-dev:latest `

This sets topic.max.message.bytes and replica.fetch.max.bytes on the broker.

And if you're using rdkafka then pass the message.max.bytes in the producer config like:

  const producer = new Kafka.Producer({
        'metadata.broker.list': 'localhost:9092',
        'message.max.bytes': '15728640',
        'dr_cb': true
    });

Similarly, for the consumer,

  const kafkaConf = {
   "group.id": "librd-test",
   "fetch.message.max.bytes":"15728640",
   ... .. }


问:
How to publicly access an AWS EC2 instance that contains an SSL redirect?
I have a Node/Express server running on an EC2 instance that is maintained by Elastic Beanstalk (single instance no load balancer).
I am struggling with implementing a correct SSL redirect that works with EC2.

The SSL redirect is as follows:

```<code>const httpsOptions = {
  key: fs.readFileSync('/local/ssl/key.pem'),
  cert: fs.readFileSync('/local/ssl/cert.pem'),
  passphrase: '*****'
}

http.createServer(function (req, res) {
  let httpsHost = req.headers.host.replace('8081', '8443');
  res.writeHead(301, { "Location": "https://" + httpsHost + req.url });
  res.end();
}).listen(8081);

https.createServer(httpsOptions, app).listen(8443)

</code>```

This works great locally, but when I deploy to my EC2 instance and try to access the site, I get:

Here is a picture of my inbound rules as well:

Do I have the ports configured incorrectly? I cannot use port 80 and port 443 because it requires root permissions, and I won't run my server with root permissions.

答:
This was solved through port forwarding.

Check current IP-tables:

```<code>sudo iptables -t nat -L
</code>```

Remove last entry:

```<code>sudo iptables -t nat -D PREROUTING 1
</code>```

Set up redirection as necessary, in my case:

```<code>sudo iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-ports 8443
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8081
</code>```

This sends all 443 traffic to 8443, and all 80 traffic to 8081.

Body:
I have a Node/Express server running on an EC2 instance that is maintained by Elastic Beanstalk (single instance no load balancer).
I am struggling with implementing a correct SSL redirect that works with EC2.

The SSL redirect is as follows:

const httpsOptions = {
  key: fs.readFileSync('/local/ssl/key.pem'),
  cert: fs.readFileSync('/local/ssl/cert.pem'),
  passphrase: '*****'
}

http.createServer(function (req, res) {
  let httpsHost = req.headers.host.replace('8081', '8443');
  res.writeHead(301, { "Location": "https://" + httpsHost + req.url });
  res.end();
}).listen(8081);

https.createServer(httpsOptions, app).listen(8443)

This works great locally, but when I deploy to my EC2 instance and try to access the site, I get:

Here is a picture of my inbound rules as well:

Do I have the ports configured incorrectly? I cannot use port 80 and port 443 because it requires root permissions, and I won't run my server with root permissions.

Body:
This was solved through port forwarding.

Check current IP-tables:

sudo iptables -t nat -L

Remove last entry:

sudo iptables -t nat -D PREROUTING 1

Set up redirection as necessary, in my case:

sudo iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-ports 8443
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8081

This sends all 443 traffic to 8443, and all 80 traffic to 8081.


问:
Node.js Discord.js UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'calculatedPosition' of undefined
I am using discord.js, I was making a ban command and I got this error: UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'calculatedPosition' of undefined

My code is:

let banUser = message.guild.member(message.mentions.users.first())
let bannerHighRole = message.member.highestRole.calculatedPosition;
let getBannedHighRole = banUser.highestRole.calculatedPosition;
if (bannerHighRole < getBannedHighRole) return message.reply("You cannot ban users that have higher roles than you!")
if (bannerHighRole = getBannedHighRole) return message.reply("You cannot ban users that have same highest role!")
```

How can I fix this error?
I'm running the bot on Discord.js v12

答:
This must be v11 code or before, check docs to update your other code, but regardless:

They have changed a lot of the camelCase functions like fetchMessages into things like messages.fetch, might be useful to know.

// you can use message.mentions.members instead of converting a user
let banUser = message.mentions.members.first();
let bannerHighRole = message.member.roles.highest.position;
let getBannedHighRole = banUser.roles.highest.position;
if (bannerHighRole < getBannedHighRole) return message.reply("You cannot ban users that have higher roles than you!");
//you had x = y, which is an assignment
if (bannerHighRole === getBannedHighRole) return message.reply("You cannot ban users that have same highest role!");
```

Also saw the full code and you should prob check if the user has mod/admin before you calculate all of these.

Body:
I am using discord.js, I was making a ban command and I got this error: UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'calculatedPosition' of undefined

My code is:

let banUser = message.guild.member(message.mentions.users.first())
let bannerHighRole = message.member.highestRole.calculatedPosition;
let getBannedHighRole = banUser.highestRole.calculatedPosition;
if (bannerHighRole < getBannedHighRole) return message.reply("You cannot ban users that have higher roles than you!")
if (bannerHighRole = getBannedHighRole) return message.reply("You cannot ban users that have same highest role!")

How can I fix this error?
I'm running the bot on Discord.js v12

Body:
This must be v11 code or before, check docs to update your other code, but regardless:

They have changed a lot of the camelCase functions like fetchMessages into things like messages.fetch, might be useful to know.

// you can use message.mentions.members instead of converting a user
let banUser = message.mentions.members.first();
let bannerHighRole = message.member.roles.highest.position;
let getBannedHighRole = banUser.roles.highest.position;
if (bannerHighRole < getBannedHighRole) return message.reply("You cannot ban users that have higher roles than you!");
//you had x = y, which is an assignment
if (bannerHighRole === getBannedHighRole) return message.reply("You cannot ban users that have same highest role!");

Also saw the full code and you should prob check if the user has mod/admin before you calculate all of these.


问:
Laravel collection paginate with where clause
I have a news table and i manage the read or unread flag. so I want to paginate both flags are separate without additional database queries.
My requirement is to get all the news and filter read or unread and paginate for a separate table view.
Once I try the following code but getting an error Method Illuminate\Database\Eloquent\Collection::paginate does not exist.

```<code>    $ns=News::all();
    $ns->where('read',1)->paginate(2);
    $ns->where('read',0)->paginate(2);
</code>```

I want to do that using only one DB Query.

答:
Laravel paginate can't make a pagination from collection, you need eloquent query builder instead.

https://laravel.com/docs/7.x/pagination#paginating-query-builder-results

So, if you still want make a pagination from collection, you can create custom pagination for collection with macro. An example is in this gist :

https://gist.github.com/simonhamp/549e8821946e2c40a617c85d2cf5af5e

Body:
I have a news table and i manage the read or unread flag. so I want to paginate both flags are separate without additional database queries.
My requirement is to get all the news and filter read or unread and paginate for a separate table view.
Once I try the following code but getting an error Method Illuminate\Database\Eloquent\Collection::paginate does not exist.

    $ns=News::all();
    $ns->where('read',1)->paginate(2);
    $ns->where('read',0)->paginate(2);

I want to do that using only one DB Query.


问:
IE CSS issue when hitting the browsers back button
I am having an issue with IE (9 in this case) where I have uploaded a new CSS file for a page and it works just fine when I go to the page.  The problem I am having is when I browse to any another page, if I then hit the back button it renders my page using a previous version of the CSS file.  I verified this using the F12 tool to see that the css file only had the entries from the previous version.  In this case, i was styling 'ol' and 'li' elements if that matters.

Is there anything I can do to make IE load the most current version of a css file when the browsers back button is used?

Again, the page loads perfectly if I browse to it in any "forward" manner, I also have my browser set to check for new versions every visit.  Its when I hit the back button it has problems. Also every other browser ive tested has no problem using the back button.

答:
If you are clicking back to return to a page that you have previously visited with the old stylesheet, then yes, it will load the old stylesheet. A simple refresh should fix this.

The reason is that the back button is not meant to reload old pages, but to preserve the pages exact state and return the user back to that page.

If indeed you have loaded the new stylesheet on page A and then move forward to page B, then click back to go to page A and an old version of the style has been loaded, there might be something going seriously wrong with IE's cache. It might be a bug. Maybe changing the filename or adding something to the query string will force it to recognise your stylesheet as a newer version eg:

```<code>style.css?v2
</code>```

will make sure the browser treats your style as a completely new file and disregard any old cached versions.

Body:
If you are clicking back to return to a page that you have previously visited with the old stylesheet, then yes, it will load the old stylesheet. A simple refresh should fix this.

The reason is that the back button is not meant to reload old pages, but to preserve the pages exact state and return the user back to that page.

If indeed you have loaded the new stylesheet on page A and then move forward to page B, then click back to go to page A and an old version of the style has been loaded, there might be something going seriously wrong with IE's cache. It might be a bug. Maybe changing the filename or adding something to the query string will force it to recognise your stylesheet as a newer version eg:

style.css?v2

will make sure the browser treats your style as a completely new file and disregard any old cached versions.


问:
Starting a TCP client with python
I'm learning ethical hacking with Python, and I have been trying to type a simple TCP client from BlackHat python book, but I have problems running the code I have written from the book.

import socket

target_host = "95.127.145.5"
target_post = 80

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((target_host,target_post))

client = send("GET / HTTP/1.1\r\nHost: 95.127.145.5\r\n\r\n")

response = client.recv(4096)
```

I'm  not sure if it's because it's python2 but if it is, I need advice on how to convert this code to python3 because my IDE is python 3.8.3
 the error happens in ("GET / HTTP/1.1\r\nHost: 95.127.145.5\r\n\r\n") and I get the message "inspect type checker options".
Any advice is highly appreciated.

答:
You have to encode the text before sending the request,

This line is also wrong

```<code>client = send("GET / HTTP/1.1\r\nHost: 95.127.145.5\r\n\r\n")
</code>```

It should be :

```<code>client.send("GET / HTTP/1.1\r\nHost: 95.127.145.5\r\n\r\n")
</code>```

See :

```<code>import socket

target_host = "95.127.145.5"
target_post = 80

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((target_host,target_post))

client.send("GET / HTTP/1.1\r\nHost: 95.127.145.5\r\n\r\n".encode())

response = client.recv(4096)
</code>```

Also check whether the host is up or not

Title: Starting a TCP client with python

Body:
I'm learning ethical hacking with Python, and I have been trying to type a simple TCP client from BlackHat python book, but I have problems running the code I have written from the book.

import socket

target_host = "95.127.145.5"
target_post = 80

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((target_host,target_post))

client = send("GET / HTTP/1.1\r\nHost: 95.127.145.5\r\n\r\n")

response = client.recv(4096)

I'm  not sure if it's because it's python2 but if it is, I need advice on how to convert this code to python3 because my IDE is python 3.8.3
 the error happens in ("GET / HTTP/1.1\r\nHost: 95.127.145.5\r\n\r\n") and I get the message "inspect type checker options".
Any advice is highly appreciated.

Body:
You have to encode the text before sending the request,

This line is also wrong

client = send("GET / HTTP/1.1\r\nHost: 95.127.145.5\r\n\r\n")

It should be :

client.send("GET / HTTP/1.1\r\nHost: 95.127.145.5\r\n\r\n")

See :

import socket

target_host = "95.127.145.5"
target_post = 80

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((target_host,target_post))

client.send("GET / HTTP/1.1\r\nHost: 95.127.145.5\r\n\r\n".encode())

response = client.recv(4096)

Also check whether the host is up or not


问:
Python synchronize subclass threads
First of all, I'm new to threading in Python and actually pretty new to Python in general, so please excuse if the question is not well asked.

I have 2 modules that each include one class. These classes are both subclasses of threading.Thread. I start these two threads from a third module, main.py:

from controller import Controller
from server import Server
from ultrasonic import Ultrasonic

if __name__ == "__main__":
    controller = Controller()

    server = Server(controller)
    server.start()

    ultrasonic = Ultrasonic(controller)
    ultrasonic.start()
```

<p>As you can see, both threads access another class in a different module, <code>Controller</code>. To make sure the two threads won't interfere while calling methods of <code>Controller</code>, I want to synchronize these threads. I saw, that there's the option of using <code>threading.Lock</code>, but I'm not sure how to implement this in my specific case, with different subclasses of <code>threading.Thread</code> in different modules.</p>

<p>I hope y'all can help me. Thank you!</p>

<p><strong>EDIT:</strong>
The methods in the <code>Controller</code> class look like this:</p>

<pre class="lang-py prettyprint-override"><code>def stop(self):
    GPIO.output(self.LEFT_PIN1, False)
    GPIO.output(self.LEFT_PIN2, False)
    GPIO.output(self.RIGHT_PIN1, False)
    GPIO.output(self.RIGHT_PIN2, False)
</code>```

I thought it might lead to problems if the GPIO pins recieved contrary signals. If that's not the case, then I don't need to synchronize the threads in the first place.

答:
My guess is that your Controller class could create a Lock:

```<code>from Threading import Lock

class Controller:
    lock = Lock()    # class level instance

    def stop(self):
        Controller.lock.acquire()
        GPIO.output(self.LEFT_PIN1, False)
        GPIO.output(self.LEFT_PIN2, False)
        GPIO.output(self.RIGHT_PIN1, False)
        GPIO.output(self.RIGHT_PIN2, False)
        Controller.lock.release()
</code>```

This way the Controller class creates a single Lock when it is defined.

When any call is make to stop(), the current thread acquires the lock and releases it upon return. Any other thread which makes it to the acquire() call, blocks until the first thread releases it.

Better is to use context management and the with statement:

```<code>    def stop(self):
        with Controller.lock:
            GPIO.output(self.LEFT_PIN1, False)
            GPIO.output(self.LEFT_PIN2, False)
            GPIO.output(self.RIGHT_PIN1, False)
            GPIO.output(self.RIGHT_PIN2, False)
</code>```

Note that I've used a Lock at the class level since you are locking access to the GPIO pins of your processor and there will only be one set of those.

Title: Python synchronize subclass threads

Body:
First of all, I'm new to threading in Python and actually pretty new to Python in general, so please excuse if the question is not well asked.

I have 2 modules that each include one class. These classes are both subclasses of threading.Thread. I start these two threads from a third module, main.py:

from controller import Controller
from server import Server
from ultrasonic import Ultrasonic

if __name__ == "__main__":
    controller = Controller()

    server = Server(controller)
    server.start()

    ultrasonic = Ultrasonic(controller)
    ultrasonic.start()

As you can see, both threads access another class in a different module, Controller. To make sure the two threads won't interfere while calling methods of Controller, I want to synchronize these threads. I saw, that there's the option of using threading.Lock, but I'm not sure how to implement this in my specific case, with different subclasses of threading.Thread in different modules.

I hope y'all can help me. Thank you!

EDIT:
The methods in the Controller class look like this:

def stop(self):
    GPIO.output(self.LEFT_PIN1, False)
    GPIO.output(self.LEFT_PIN2, False)
    GPIO.output(self.RIGHT_PIN1, False)
    GPIO.output(self.RIGHT_PIN2, False)

I thought it might lead to problems if the GPIO pins recieved contrary signals. If that's not the case, then I don't need to synchronize the threads in the first place.

Body:
My guess is that your Controller class could create a Lock:

from Threading import Lock

class Controller:
    lock = Lock()    # class level instance

    def stop(self):
        Controller.lock.acquire()
        GPIO.output(self.LEFT_PIN1, False)
        GPIO.output(self.LEFT_PIN2, False)
        GPIO.output(self.RIGHT_PIN1, False)
        GPIO.output(self.RIGHT_PIN2, False)
        Controller.lock.release()

This way the Controller class creates a single Lock when it is defined.

When any call is make to stop(), the current thread acquires the lock and releases it upon return. Any other thread which makes it to the acquire() call, blocks until the first thread releases it.

Better is to use context management and the with statement:

    def stop(self):
        with Controller.lock:
            GPIO.output(self.LEFT_PIN1, False)
            GPIO.output(self.LEFT_PIN2, False)
            GPIO.output(self.RIGHT_PIN1, False)
            GPIO.output(self.RIGHT_PIN2, False)

Note that I've used a Lock at the class level since you are locking access to the GPIO pins of your processor and there will only be one set of those.


答:
In python2,

```<code>client = send("GET / HTTP/1.1\r\nHost: 95.127.145.5\r\n\r\n")
</code>```

should be

```<code>client.send("GET / HTTP/1.1\r\nHost: 95.127.145.5\r\n\r\n")
</code>```

In python3, it just needs to be

```<code>client.send(b"GET / HTTP/1.1\r\nHost: 95.127.145.5\r\n\r\n")
</code>```

edit: Roshin Raphel's answer does the encoding better than mine. And you're not using target_host when making up the request. So that line is probably better being

```<code>client.send(f"GET / HTTP/1.1\r\nHost: {target_host}\r\n\r\n".encode())
</code>```

Title: Starting a TCP client with python

Body:
In python2,

client = send("GET / HTTP/1.1\r\nHost: 95.127.145.5\r\n\r\n")

should be

client.send("GET / HTTP/1.1\r\nHost: 95.127.145.5\r\n\r\n")

In python3, it just needs to be

client.send(b"GET / HTTP/1.1\r\nHost: 95.127.145.5\r\n\r\n")

edit: Roshin Raphel's answer does the encoding better than mine. And you're not using target_host when making up the request. So that line is probably better being

client.send(f"GET / HTTP/1.1\r\nHost: {target_host}\r\n\r\n".encode())


问:
Looping (iteration) over a pandas dataframe - how to choose desired values?
I have a DataFrame containing informations about airports. Using for loop, I want to extract name of the airport and total seats from the most recent year reported. I am only interested in seeing values higher than 20000.

This code prints out all names and seats:

```<code>for name, seats in zip(sa_airports['Name'], sa_airports['TotalSeats']):
    print(name, ' : ', seats)
</code>```

I tried this:

```<code>for name, seats in zip(sa_airports['Name'], sa_airports['TotalSeats']):
    if sa_airports['TotalSeats'] > 20000:
        print(name, ' : ', seats)
</code>```

But error occured: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Because of that, i tried to put every option into the loop, yet nothing gives me result I want. Example:

```<code>for name, seats in zip(sa_airports['Name'], sa_airports['TotalSeats']):
    if (sa_airports['TotalSeats'] > 20000).any():
        print(name, ' : ', seats)
</code>```

This code checks if any value in totalseats is > 20000 and it is true, so it prints out everything

答:
In general you should (almost) never use loops with Pandas.  Use vectorized operations instead.  They are more performant and easier to understand.</p>

```<code>filtered = sa_airports.loc[sa_airports['TotalSeats'] > 20000][['Name', 'TotalSeats']]
for name, seats in filtered.itertuples(index=False):
    print(name, ' : ', seats)
</code>```

Body:
I have a DataFrame containing informations about airports. Using for loop, I want to extract name of the airport and total seats from the most recent year reported. I am only interested in seeing values higher than 20000.

This code prints out all names and seats:

for name, seats in zip(sa_airports['Name'], sa_airports['TotalSeats']):
    print(name, ' : ', seats)

I tried this:

for name, seats in zip(sa_airports['Name'], sa_airports['TotalSeats']):
    if sa_airports['TotalSeats'] > 20000:
        print(name, ' : ', seats)

But error occured: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Because of that, i tried to put every option into the loop, yet nothing gives me result I want. Example:

for name, seats in zip(sa_airports['Name'], sa_airports['TotalSeats']):
    if (sa_airports['TotalSeats'] > 20000).any():
        print(name, ' : ', seats)

This code checks if any value in totalseats is > 20000 and it is true, so it prints out everything

Body:
In general you should (almost) never use loops with Pandas.  Use vectorized operations instead.  They are more performant and easier to understand.

filtered = sa_airports.loc[sa_airports['TotalSeats'] > 20000][['Name', 'TotalSeats']]
for name, seats in filtered.itertuples(index=False):
    print(name, ' : ', seats)


问:
Memory-mapped files and low-memory scenarios
How does the iOS platform handle memory-mapped files during low-memory scenarios? By low-memory scenarios, I mean when the OS sends the UIApplicationDidReceiveMemoryWarningNotification notification to all observers in the application.

Our files are mapped into memory using +[NSData dataWithContentsOfMappedFile:], the documentation for which states:

A mapped file uses virtual memory techniques to avoid copying pages of the file into memory until they are actually needed.

Does this mean that the OS will also unmap the pages when they're no longer in use? Is it possible to mark pages as being no longer in use? This data is read-only, if that changes the scenario. How about if we were to use mmap() directly? Would this be preferable?

答:
Memory-mapped files copy data from disk into memory a page at a time. Unused pages are free to be swapped out, the same as any other virtual memory, unless they have been wired into physical memory using mlock(2). Memory mapping leaves the determination of what to copy from disk to memory and when to the OS.

Dropping from the Foundation level to the BSD level to use mmap is unlikely to make much difference, beyond making code that has to interface with other Foundation code somewhat more awkward.