问:
Passing the ErrorMessage for clientside validation
Since there is no way to validate a property (with unobtrusive clientside validation) using multiple regex patterns (because validation type has to be unique) i decided to extend FluentValidation so i can do the following.

```<code>RuleFor(x => x.Name).NotEmpty().WithMessage("Name is required")
                    .Length(3, 20).WithMessage("Name must contain between 3 and 20 characters")
                    .Match(@"^[A-Z]").WithMessage("Name has to start with an uppercase letter")
                    .Match(@"^[a-zA-Z0-9_\-\.]*$").WithMessage("Name can only contain: a-z 0-9 _ - .")
                    .Match(@"[a-z0-9]$").WithMessage("Name has to end with a lowercase letter or digit")
                    .NotMatch(@"[_\-\.]{2,}").WithMessage("Name cannot contain consecutive non-alphanumeric characters");
</code>```

The last thing i need to figure out is how to pass the errormessage which is set using WithMessage() via GetClientValidationRules() so it ends up in the "data-val-customregex[SOMEFANCYSTRINGHERETOMAKEITUNIQUE]" attribute on the input element.

```<code>public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) {
    var rule = new ModelClientValidationRule();
    rule.ErrorMessage = [INSERT ERRORMESSAGE HERE];
    rule.ValidationType = "customregex" + StringFunctions.RandomLetters(6);
    rule.ValidationParameters.Add("pattern", pattern);

    yield return rule;
}
</code>```

I've been looking at the FluentValidation sourcecode, but couldn't figure it out. Anyone got any ideas?

答:
I've been discussing how to do this with Jeremy Skinner (the creator of Fluent Validation) at

http://fluentvalidation.codeplex.com/discussions/253505

He was kind enough to write a complete example.

Update

Here is the code we came up with:

First the extensions, for both Match and NotMatch.

```<code>public static class Extensions
{
    public static IRuleBuilderOptions<T, string> Match<T>(this IRuleBuilder<T, string> ruleBuilder, string expression)
    {
        return ruleBuilder.SetValidator(new MatchValidator(expression));
    }

    public static IRuleBuilderOptions<T, string> NotMatch<T>(this IRuleBuilder<T, string> ruleBuilder, string expression) {
        return ruleBuilder.SetValidator(new MatchValidator(expression, false));
    }
}
</code>```

The used interface for the validator

```<code>public interface IMatchValidator : IPropertyValidator
{
    string Expression { get; }
    bool MustMatch { get; }
}
</code>```

The actual validator:

```<code>public class MatchValidator : PropertyValidator, IMatchValidator
{
    string expression;
    bool mustMatch;

    public MatchValidator(string expression, bool mustMatch = true)
        : base(string.Format("The value {0} match with the given expression, while it {1}.", mustMatch ? "did not" : "did", mustMatch ? "should" : "should not"))
    {
        this.expression = expression;
        this.mustMatch = mustMatch;
    }

    protected override bool IsValid(PropertyValidatorContext context)
    {
        return context.PropertyValue == null ||
               context.PropertyValue.ToString() == string.Empty ||
               Regex.IsMatch(context.PropertyValue.ToString(), expression) == mustMatch;
    }

    public string Expression
    {
        get { return expression; }
    }

    public bool MustMatch {
        get { return mustMatch; }
    }
}
</code>```

The adaptor to register the validator:

```<code>public class MatchValidatorAdaptor : FluentValidationPropertyValidator
{
    public MatchValidatorAdaptor(ModelMetadata metadata, ControllerContext controllerContext, PropertyRule rule, IPropertyValidator validator)
        : base(metadata, controllerContext, rule, validator)
    {
    }

    IMatchValidator MatchValidator
    {
        get { return (IMatchValidator)Validator; }
    }

    public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
    {
        var formatter = new MessageFormatter().AppendPropertyName(Rule.PropertyDescription);
        string errorMessage = formatter.BuildMessage(Validator.ErrorMessageSource.GetString());
        yield return new ModelClientValidationMatchRule(MatchValidator.Expression, MatchValidator.MustMatch, errorMessage);
    }
}
</code>```

And finally where the magic happens:

```<code>public class ModelClientValidationMatchRule : ModelClientValidationRule
{
    public ModelClientValidationMatchRule(string expression, bool mustMatch, string errorMessage)
    {
        if (mustMatch)
            base.ValidationType = "match";
        else
            base.ValidationType = "notmatch";

        base.ValidationType += StringFunctions.RandomLetters(6);
        base.ErrorMessage = errorMessage;
        base.ValidationParameters.Add("expression", expression);
    }
}
</code>```

Update 2:

Javascript to wireup jQuery.validator:

```<code>(function ($) {
    function attachMatchValidator(name, mustMatch) {
        $.validator.addMethod(name, function (val, element, expression) {
            var rg = new RegExp(expression, "gi");
            return (rg.test(val) == mustMatch);
        });

        $.validator.unobtrusive.adapters.addSingleVal(name, "expression");
    }

    $("input[type=text]").each(function () {
        $.each(this.attributes, function (i, attribute) {
            if (attribute.name.length == 20 && attribute.name.substring(0, 14) == "data-val-match")
                attachMatchValidator(attribute.name.substring(9, 20), true);

            if (attribute.name.length == 23 && attribute.name.substring(0, 17) == "data-val-notmatch")
                attachMatchValidator(attribute.name.substring(9, 23), false);
        });
    });
} (jQuery));
</code>```

Body:
Since there is no way to validate a property (with unobtrusive clientside validation) using multiple regex patterns (because validation type has to be unique) i decided to extend FluentValidation so i can do the following.

RuleFor(x => x.Name).NotEmpty().WithMessage("Name is required")
                    .Length(3, 20).WithMessage("Name must contain between 3 and 20 characters")
                    .Match(@"^[A-Z]").WithMessage("Name has to start with an uppercase letter")
                    .Match(@"^[a-zA-Z0-9_\-\.]*$").WithMessage("Name can only contain: a-z 0-9 _ - .")
                    .Match(@"[a-z0-9]$").WithMessage("Name has to end with a lowercase letter or digit")
                    .NotMatch(@"[_\-\.]{2,}").WithMessage("Name cannot contain consecutive non-alphanumeric characters");

The last thing i need to figure out is how to pass the errormessage which is set using WithMessage() via GetClientValidationRules() so it ends up in the "data-val-customregex[SOMEFANCYSTRINGHERETOMAKEITUNIQUE]" attribute on the input element.

public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) {
    var rule = new ModelClientValidationRule();
    rule.ErrorMessage = [INSERT ERRORMESSAGE HERE];
    rule.ValidationType = "customregex" + StringFunctions.RandomLetters(6);
    rule.ValidationParameters.Add("pattern", pattern);

    yield return rule;
}

I've been looking at the FluentValidation sourcecode, but couldn't figure it out. Anyone got any ideas?

Body:
I've been discussing how to do this with Jeremy Skinner (the creator of Fluent Validation) at

http://fluentvalidation.codeplex.com/discussions/253505

He was kind enough to write a complete example.

Update

Here is the code we came up with:

First the extensions, for both Match and NotMatch.

public static class Extensions
{
    public static IRuleBuilderOptions<T, string> Match<T>(this IRuleBuilder<T, string> ruleBuilder, string expression)
    {
        return ruleBuilder.SetValidator(new MatchValidator(expression));
    }

    public static IRuleBuilderOptions<T, string> NotMatch<T>(this IRuleBuilder<T, string> ruleBuilder, string expression) {
        return ruleBuilder.SetValidator(new MatchValidator(expression, false));
    }
}

The used interface for the validator

public interface IMatchValidator : IPropertyValidator
{
    string Expression { get; }
    bool MustMatch { get; }
}

The actual validator:

public class MatchValidator : PropertyValidator, IMatchValidator
{
    string expression;
    bool mustMatch;

    public MatchValidator(string expression, bool mustMatch = true)
        : base(string.Format("The value {0} match with the given expression, while it {1}.", mustMatch ? "did not" : "did", mustMatch ? "should" : "should not"))
    {
        this.expression = expression;
        this.mustMatch = mustMatch;
    }

    protected override bool IsValid(PropertyValidatorContext context)
    {
        return context.PropertyValue == null ||
               context.PropertyValue.ToString() == string.Empty ||
               Regex.IsMatch(context.PropertyValue.ToString(), expression) == mustMatch;
    }

    public string Expression
    {
        get { return expression; }
    }

    public bool MustMatch {
        get { return mustMatch; }
    }
}

The adaptor to register the validator:

public class MatchValidatorAdaptor : FluentValidationPropertyValidator
{
    public MatchValidatorAdaptor(ModelMetadata metadata, ControllerContext controllerContext, PropertyRule rule, IPropertyValidator validator)
        : base(metadata, controllerContext, rule, validator)
    {
    }

    IMatchValidator MatchValidator
    {
        get { return (IMatchValidator)Validator; }
    }

    public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
    {
        var formatter = new MessageFormatter().AppendPropertyName(Rule.PropertyDescription);
        string errorMessage = formatter.BuildMessage(Validator.ErrorMessageSource.GetString());
        yield return new ModelClientValidationMatchRule(MatchValidator.Expression, MatchValidator.MustMatch, errorMessage);
    }
}

And finally where the magic happens:

public class ModelClientValidationMatchRule : ModelClientValidationRule
{
    public ModelClientValidationMatchRule(string expression, bool mustMatch, string errorMessage)
    {
        if (mustMatch)
            base.ValidationType = "match";
        else
            base.ValidationType = "notmatch";

        base.ValidationType += StringFunctions.RandomLetters(6);
        base.ErrorMessage = errorMessage;
        base.ValidationParameters.Add("expression", expression);
    }
}

Update 2:

Javascript to wireup jQuery.validator:

(function ($) {
    function attachMatchValidator(name, mustMatch) {
        $.validator.addMethod(name, function (val, element, expression) {
            var rg = new RegExp(expression, "gi");
            return (rg.test(val) == mustMatch);
        });

        $.validator.unobtrusive.adapters.addSingleVal(name, "expression");
    }

    $("input[type=text]").each(function () {
        $.each(this.attributes, function (i, attribute) {
            if (attribute.name.length == 20 && attribute.name.substring(0, 14) == "data-val-match")
                attachMatchValidator(attribute.name.substring(9, 20), true);

            if (attribute.name.length == 23 && attribute.name.substring(0, 17) == "data-val-notmatch")
                attachMatchValidator(attribute.name.substring(9, 23), false);
        });
    });
} (jQuery));


来源: stackexchange

问:
What is a good practive to save hyperparamters for restoring and retraining
I just encounter an issue that I have a good model but forgot what hyperparamters I used. I just have the meta, index and data files. I searched if the hyperparamters are saved in those files. But it seems not the case. So what is the best way to save the hyperparamters together with the checkpoint files such that I can retrain at any times with the same parameters?</p>

答:
I tend to use the current date as a model name, and automatically generate a csv file like:

date, path_to_model, hyper_param1, hyper_param2, ..., test acc, val acc, train acc, loss

That way I can easily load in any model that I've previously trained on a new dataset, by just using the date. This structure was pretty nice for me to use, and allows filtering and is easy to read

ContentLicense: CC BY-SA 4.0


问:
Execute a linux command to copy pdf files in a text file to a directory
I have a list of pdf files in my text file called "pdf.txt".How to copy this to a directory /home/temporary using linux command.

```<code>#!/bin/bash
pdf=cat pdf.txt
cp -R $pdf /home/temporary/b
</code>```

This is not working.

答:
Assuming one file per line, this should do it:</p>

```<code>#!/bin/bash
IFS=$'\n' # split on newlines, not whitespace in case filenames have spaces
for line in $(cat pdf.txt); do
    cp "${line}" /home/temporary/b
done
</code>```

Body:
I have a list of pdf files in my text file called "pdf.txt".How to copy this to a directory /home/temporary using linux command.

#!/bin/bash
pdf=cat pdf.txt
cp -R $pdf /home/temporary/b

This is not working.

Body:
Assuming one file per line, this should do it:

#!/bin/bash
IFS=$'\n' # split on newlines, not whitespace in case filenames have spaces
for line in $(cat pdf.txt); do
    cp "${line}" /home/temporary/b
done


问:
Removing an array of elements causes NullPointerException
I'm a beginner in Java. I have to make shopping cart. But I'm stuck on remove method.

Please help me!

I tried to make remove method. But it doesn't work well..

Customer.java

```<code>public class Customer {
    private String id;
    private String firstName;
    private String lastName;
    private ShoppingCart shopCart;

    public Customer(String id, String firstName, String lastName) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        shopCart = new ShoppingCart();
    }

    public ShoppingCart getShopCart() {
        return this.shopCart;
    }
    @Override
    public String toString() {
        return ("Customer ID is: " + this.id + "\n" + "Customer's name is: "
                 + this.firstName + " " + this.lastName + "\n\n"
                + "Shopping list: \n\n" + shopCart.toString());

    }

</code>```

ShoppingCart.java

```<code>public class ShoppingCart {
    private int itemCount;
    private double totalPrice;
    private static int capacity;
    private Item[] cart;
    public ShoppingCart()
    {
        capacity = 5;
        itemCount = 0;
        totalPrice = 0.0;
        cart = new Item[capacity];
    }

    public void addBooktoCart(String title, String description, double price, int pageCount) {
        if (itemCount < 5) {

        Item item = new Book(title, description, price, pageCount);
        totalPrice += price;
        cart[itemCount] = item;
        itemCount += 1;
        }
        else {
            System.out.println("The maximum number that you can input is 5." +
                     "You cannot add item anymore");
        }

    }
//This remove method may be a problem.
    public void removeItemFromCart(String title) {
        boolean found = false;
        for (int i = 0; i < cart.length; i++) {
            if (cart[i] != null)
               {
                   if (cart[i].getTitle().equals(title)) {
                   totalPrice -= cart[i].getPrice();
                   cart[i] = null;
                   itemCount -= 1;
                   found = true;
                   }
               }
        }
if(found != true) {

            System.out.println("Item is not found in cart. Nothing removed");

        }
    }
    @Override
    public String toString() {
        String contents = "";

        for (int i = 0; i < itemCount; i++)
        {
            contents += cart[i].toString() + "\n\n";
        }

        String strDouble = String.format("%.2f", totalPrice);

        contents += "The total price is: $" + strDouble + "\n\n"
                + "How many items do you have in your shopping cart? \n"
                + "There are " + this.itemCount + " items in my shopping cart. "+ "\n";
        return contents;

    }
}

</code>```

Test.java

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

        Customer c1 = new Customer("12345", "Hyun Min", "Lim");
        c1.getShopCart().addBooktoCart("Harry Potter", "Fantasy Genre", 10.99, 309);
        c1.getShopCart().addCDtoCart("Abbey Road", "Pop Genre", 6.50, 18);
        c1.getShopCart().addMovietoCart("Forrest Gump", "Drama Genre", 13.23, 141);
        c1.getShopCart().removeItemFromCart("Harry Potter");
        System.out.println(c1.toString());
    }
</code>```

I want to remove array of elements. But output is

Exception in thread "main" java.lang.NullPointerException.

What do I do if the compiler works well. I think that the remove method is a problem.

答:
The java.lang.NullPointerException appears because you delete and set the first item in your cart to null, and than when you try to access it in your toString method from Customer.java when you call shopCart.toString().

So, before  c1.getShopCart().removeItemFromCart("Harry Potter"); you have :
["Harry Potter", "Abbey Road", "Forrest Gump"] in your cart,

after c1.getShopCart().removeItemFromCart("Harry Potter"); you have:
[null, "Abbey Road", "Forrest Gump"] and you are accessing the null pointer at cart[0].

Body:
I'm a beginner in Java. I have to make shopping cart. But I'm stuck on remove method.

Please help me!

I tried to make remove method. But it doesn't work well..

Customer.java

public class Customer {
    private String id;
    private String firstName;
    private String lastName;
    private ShoppingCart shopCart;

    public Customer(String id, String firstName, String lastName) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        shopCart = new ShoppingCart();
    }

    public ShoppingCart getShopCart() {
        return this.shopCart;
    }
    @Override
    public String toString() {
        return ("Customer ID is: " + this.id + "\n" + "Customer's name is: "
                 + this.firstName + " " + this.lastName + "\n\n"
                + "Shopping list: \n\n" + shopCart.toString());

    }

ShoppingCart.java

public class ShoppingCart {
    private int itemCount;
    private double totalPrice;
    private static int capacity;
    private Item[] cart;
    public ShoppingCart()
    {
        capacity = 5;
        itemCount = 0;
        totalPrice = 0.0;
        cart = new Item[capacity];
    }

    public void addBooktoCart(String title, String description, double price, int pageCount) {
        if (itemCount < 5) {

        Item item = new Book(title, description, price, pageCount);
        totalPrice += price;
        cart[itemCount] = item;
        itemCount += 1;
        }
        else {
            System.out.println("The maximum number that you can input is 5." +
                     "You cannot add item anymore");
        }

    }
//This remove method may be a problem.
    public void removeItemFromCart(String title) {
        boolean found = false;
        for (int i = 0; i < cart.length; i++) {
            if (cart[i] != null)
               {
                   if (cart[i].getTitle().equals(title)) {
                   totalPrice -= cart[i].getPrice();
                   cart[i] = null;
                   itemCount -= 1;
                   found = true;
                   }
               }
        }
if(found != true) {

            System.out.println("Item is not found in cart. Nothing removed");

        }
    }
    @Override
    public String toString() {
        String contents = "";

        for (int i = 0; i < itemCount; i++)
        {
            contents += cart[i].toString() + "\n\n";
        }

        String strDouble = String.format("%.2f", totalPrice);

        contents += "The total price is: $" + strDouble + "\n\n"
                + "How many items do you have in your shopping cart? \n"
                + "There are " + this.itemCount + " items in my shopping cart. "+ "\n";
        return contents;

    }
}

Test.java

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

        Customer c1 = new Customer("12345", "Hyun Min", "Lim");
        c1.getShopCart().addBooktoCart("Harry Potter", "Fantasy Genre", 10.99, 309);
        c1.getShopCart().addCDtoCart("Abbey Road", "Pop Genre", 6.50, 18);
        c1.getShopCart().addMovietoCart("Forrest Gump", "Drama Genre", 13.23, 141);
        c1.getShopCart().removeItemFromCart("Harry Potter");
        System.out.println(c1.toString());
    }

I want to remove array of elements. But output is

Exception in thread "main" java.lang.NullPointerException.

What do I do if the compiler works well. I think that the remove method is a problem.


问:
Dependency Injection on static method in ASP .Net Core
I have registered the implementation of my logger in ServiceCollection in the start up:

services.AddTransient(typeof(ILogger<>), typeof(GenericLogger<>));
```

<p>Usually, I do this to inject using Constructor:</p>

<pre class="lang-cs prettyprint-override"><code>class DynamoEventProcessor
{
    private readonly IRepository _repository;
    private readonly IDogStatsd _dogStatsd;
    private readonly ILogger<DynamoEventProcessor> _logger;

    public DynamoEventProcessor(IRepository repository, IDogStatsd dogStatsd, ILogger<DynamoEventProcessor> logger)
    {
        _repository = repository;
        _dogStatsd = dogStatsd;
        _logger = logger;
    }
}
</code>```

But I have a class where there is no constructor:

public class ProfileContent
{
    public MemoryStream Content { get; set; }
    public string ContentAlgorithm { get; set; }
    public List<Dictionary<string, AttributeValue>> DataKeys { get; set; }
    public long ExpiresUtc { get; set; }
    public long Version { get; set; }
    public long Deleted { get; set; }

    public static Dictionary<string, EncryptedDataAndKeys> GetEncryptedDataAndKeys(Dictionary<string, Dictionary<string, AttributeValue>> profileContentAttributes)
    {
        _logger.LogInformation("Available Keys: " + KeysAsString(keyList));
        _logger.LogInformation("AccountId missing Coporate Data: " + _converter.GetValueFromAttributeValue(attributes["AccountId"]).ToString());
        var encryptedDataAndKeys = new Dictionary<string, EncryptedDataAndKeys>();

        foreach (var item in profileContentAttributes)
        {
            encryptedDataAndKeys.Add(item.Key, GetEncryptedDataAndKey(item.Value));
        }

        return encryptedDataAndKeys;
    }
}
```

My _logger failed here due to null. I understand the problem, that I didn't inject it properly. How can I inject it when I use it in static method without instantiating an object?

答:
You can't inject into a static constructor. You have a couple of options:

1.) Pass ILogger into the method, hopefully the calling code has it injected.

2.) Have a static property for ILogger on ProfileContent and then in your Startup file, in the Configure method, initialize it i.e.

```<code>ProfileContent.Logger = app.ApplicationServices.GetService<ILogger<ProfileContent>>();
</code>```

then use Logger in your static method. Personally, I would go for option 1.

Body:
I have registered the implementation of my logger in ServiceCollection in the start up:

services.AddTransient(typeof(ILogger<>), typeof(GenericLogger<>));

Usually, I do this to inject using Constructor:

class DynamoEventProcessor
{
    private readonly IRepository _repository;
    private readonly IDogStatsd _dogStatsd;
    private readonly ILogger<DynamoEventProcessor> _logger;

    public DynamoEventProcessor(IRepository repository, IDogStatsd dogStatsd, ILogger<DynamoEventProcessor> logger)
    {
        _repository = repository;
        _dogStatsd = dogStatsd;
        _logger = logger;
    }
}

But I have a class where there is no constructor:

public class ProfileContent
{
    public MemoryStream Content { get; set; }
    public string ContentAlgorithm { get; set; }
    public List<Dictionary<string, AttributeValue>> DataKeys { get; set; }
    public long ExpiresUtc { get; set; }
    public long Version { get; set; }
    public long Deleted { get; set; }

    public static Dictionary<string, EncryptedDataAndKeys> GetEncryptedDataAndKeys(Dictionary<string, Dictionary<string, AttributeValue>> profileContentAttributes)
    {
        _logger.LogInformation("Available Keys: " + KeysAsString(keyList));
        _logger.LogInformation("AccountId missing Coporate Data: " + _converter.GetValueFromAttributeValue(attributes["AccountId"]).ToString());
        var encryptedDataAndKeys = new Dictionary<string, EncryptedDataAndKeys>();

        foreach (var item in profileContentAttributes)
        {
            encryptedDataAndKeys.Add(item.Key, GetEncryptedDataAndKey(item.Value));
        }

        return encryptedDataAndKeys;
    }
}

My _logger failed here due to null. I understand the problem, that I didn't inject it properly. How can I inject it when I use it in static method without instantiating an object?

Body:
You can't inject into a static constructor. You have a couple of options:

1.) Pass ILogger into the method, hopefully the calling code has it injected.

2.) Have a static property for ILogger on ProfileContent and then in your Startup file, in the Configure method, initialize it i.e.

ProfileContent.Logger = app.ApplicationServices.GetService<ILogger<ProfileContent>>();

then use Logger in your static method. Personally, I would go for option 1.


问:
mPDF creates blank page after TOC when using different orientation
I'm trying to create a PDF file via mPDF where the sheets have the following orientation:

```<code>___________
| Page 1   |
|          |
| Portrait |
|          |
___________
___________
| Page 2   |
|  TOC     |
| Portrait |
|          |
___________
__________________
| Page 3          |
| Landscape       |
|                 |
__________________
___________
| Page 4   |
|          |
| Portrait |
|          |
___________
</code>```

However, changing the orientation of page 3 makes mPDF create a blank page between page 2 (TOC) and page 3.

The PDF is created in HTML and then processed to mPDF. The code is as follows:

HTML:

```<code><html>
<head>
    <style>
        @page page-landscape { size: landscape; }
        @page page-portrait { size: portrait; }

        div.landscape {
            page: page-landscape;
        }
        div.portrait {
            page: page-portrait;
        }

    </style>
</head>
<body>
    <div>
        <div>First page - displayed Portrait. The second page should be the TOC (portrait) and the 3rd should be on landscape</div>
    </div>

    <tocpagebreak />

    <div class="landscape">
       <bookmark content="TOC entry" level="0"/>
        <tocentry content="TOC entry" level="0"/>
        <p>TOC entry - Shouldn\'t have a empty page before</p>
    </div>

    <div class="portrait">
        another page
    </div>
</body>
</html>
</code>```

PHP

```<code>$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML($html);
$mpdf->Output();
</code>```

I've tried a number of things to make this work, without success. I'll list some of them:

Using toc-selector="page-portrait" on the <tocpagebreak> tag

Using toc-orientation="P" on the <tocpagebreak> tag

Adding a <pagebreak orientation="L" /> after the <tocpagebreak>

Using class="landscape" in the div after the TOC, using the @page selector, as shown in this example

Setting the variable autoPageBreak to false on mPDF's constructor

Wrapping the pages in div and messing around with the position of <pagebreak>

I was using mPDF v6.0 and now I'm updating to mPDF v8.0.1. This issue occurs on all versions (6, 7 and 8). While on version 6 I used a hack by adding $mpdf->DeletePages(2); after $mpdf->WriteHTML($html); but this has two major problems:

This method is undocumented and seems buggy

With this, the page number doesn't match the correct pages, so I can't add page number on the footer

Is there any way to accomplish this without a blank page? Or a viable workaround?

答:
I figured out how to make this work. The key is to add class="landscape" (or the name you set on CSS) to the container of <tocpagebreak /> and not the actual page you want to display on landscape.

So the HTML should be:

```<code><html>
<head>
    <style>
         @page page-landscape { size: landscape; }
         @page page-portrait { size: portrait; }

        div.landscape { page: page-landscape; }
        div.portrait { page: page-portrait; }
    </style>
</head>

<body>
    <div class="portrait">
        <div>First page - displayed Portrait. The second page should be the TOC (portrait) and the 3rd should be on landscape</div>
    </div>

    <div class="landscape">
        <tocpagebreak />
   </div>

    <div>
     <bookmark content="TOC entry consolidacao" level="0"/>
        <tocentry content="TOC entry consolidacao" level="0"/>
        <p>TOC entry - Shouldn't have a empty page before</p>
    </div>

    <div class="portrait">
        <bookmark content="2nd page" level="0"/>
        <tocentry content="2nd page" level="0"/>
        <p>2nd page</p>
    </div>
</body>
</html>
</code>```

Note the
 wrapping the <tocpagebreak />. This actually doesn't change the orientation of the TOC but rather the next page - I imagine it has something to do with the internals of mPDF.
Then, when you want to change the orientation to portrait, just add the class="portrait" on the page you want it to be applied (as seen on "2nd page").

Other things to note are:

If you have a wrapper for all the content with a padding, it seems to create a blank page at the top or before the TOC (for whatever reason). I had a
 as the child element of  and the class had a padding: 15px; which made mPDF create a new page.

If you're using stylesheets, I advice you to drop them while testing. This sample works as is but if you try it when you have external stylesheets it may not work as expected. When this happens it means some style is forcing mPDF to create a blank page (this was how I found out about 1.). Besides, I was using Bootstrap's row and col-xx classes and remove them all.

Use <pagebreak /> only to create new pages but keeping the orientation. Alternatively use style="page-break-before: always;". This is important because while fiddling around you may find yourself creating a new page with <pagebreak /> while just trying to change the orientation, thus giving you a new blank page.

Finally, if you're using $mpdf->SetFooter() or $mpdf->SetHTMLFooter() (or the corresponding Header methods), once you use the @page selector, the header / footer won't be displayed on the page. You need to set a named header/footer and specify it via CSS on the @page selector. See example 5 on the docs for more information.

Body:
I'm trying to create a PDF file via mPDF where the sheets have the following orientation:

___________
| Page 1   |
|          |
| Portrait |
|          |
___________
___________
| Page 2   |
|  TOC     |
| Portrait |
|          |
___________
__________________
| Page 3          |
| Landscape       |
|                 |
__________________
___________
| Page 4   |
|          |
| Portrait |
|          |
___________

However, changing the orientation of page 3 makes mPDF create a blank page between page 2 (TOC) and page 3.

The PDF is created in HTML and then processed to mPDF. The code is as follows:

HTML:

First page - displayed Portrait. The second page should be the TOC (portrait) and the 3rd should be on landscape

    <tocpagebreak />

       <bookmark content="TOC entry" level="0"/>
        <tocentry content="TOC entry" level="0"/>

TOC entry - Shouldn\'t have a empty page before

        another page

PHP

$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML($html);
$mpdf->Output();

I've tried a number of things to make this work, without success. I'll list some of them:

Using toc-selector="page-portrait" on the <tocpagebreak> tag

Using toc-orientation="P" on the <tocpagebreak> tag

Adding a <pagebreak orientation="L" /> after the <tocpagebreak>

Using class="landscape" in the div after the TOC, using the @page selector, as shown in this example

Setting the variable autoPageBreak to false on mPDF's constructor

Wrapping the pages in div and messing around with the position of <pagebreak>

I was using mPDF v6.0 and now I'm updating to mPDF v8.0.1. This issue occurs on all versions (6, 7 and 8). While on version 6 I used a hack by adding $mpdf->DeletePages(2); after $mpdf->WriteHTML($html); but this has two major problems:

This method is undocumented and seems buggy

With this, the page number doesn't match the correct pages, so I can't add page number on the footer

Is there any way to accomplish this without a blank page? Or a viable workaround?

Body:
I figured out how to make this work. The key is to add class="landscape" (or the name you set on CSS) to the container of <tocpagebreak /> and not the actual page you want to display on landscape.

So the HTML should be:

<html>
<head>
    <style>
         @page page-landscape { size: landscape; }
         @page page-portrait { size: portrait; }

        div.landscape { page: page-landscape; }
        div.portrait { page: page-portrait; }
    </style>
</head>

<body>
    <div class="portrait">
        <div>First page - displayed Portrait. The second page should be the TOC (portrait) and the 3rd should be on landscape</div>
    </div>

    <div class="landscape">
        <tocpagebreak />
   </div>

    <div>
     <bookmark content="TOC entry consolidacao" level="0"/>
        <tocentry content="TOC entry consolidacao" level="0"/>
        <p>TOC entry - Shouldn't have a empty page before</p>
    </div>

    <div class="portrait">
        <bookmark content="2nd page" level="0"/>
        <tocentry content="2nd page" level="0"/>
        <p>2nd page</p>
    </div>
</body>
</html>

Note the <div class="landscape"> wrapping the <tocpagebreak />. This actually doesn't change the orientation of the TOC but rather the next page - I imagine it has something to do with the internals of mPDF.
Then, when you want to change the orientation to portrait, just add the class="portrait" on the page you want it to be applied (as seen on "2nd page").

Other things to note are:

If you have a wrapper for all the content with a padding, it seems to create a blank page at the top or before the TOC (for whatever reason). I had a <div class="page-content"> as the child element of <body> and the class had a padding: 15px; which made mPDF create a new page.

If you're using stylesheets, I advice you to drop them while testing. This sample works as is but if you try it when you have external stylesheets it may not work as expected. When this happens it means some style is forcing mPDF to create a blank page (this was how I found out about 1.). Besides, I was using Bootstrap's row and col-xx classes and remove them all.

Use <pagebreak /> only to create new pages but keeping the orientation. Alternatively use style="page-break-before: always;". This is important because while fiddling around you may find yourself creating a new page with <pagebreak /> while just trying to change the orientation, thus giving you a new blank page.

Finally, if you're using $mpdf->SetFooter() or $mpdf->SetHTMLFooter() (or the corresponding Header methods), once you use the @page selector, the header / footer won't be displayed on the page. You need to set a named header/footer and specify it via CSS on the @page selector. See example 5 on the docs for more information.


问:
What do the parameters mean in scull's proc read implementation?
In LDD3's (Linux Device Drivers 3rd Edition book) scullpipe, what do the parameters in static int scull_read_p_mem(char *buf, char **start, off_t offset, int count, int *eof, void *data) mean? Specifically, I don't understand the difference between start, page, and offset.

There are a number of questions I have regarding the actual implementation itself (which can be found below):

```<code>struct scull_pipe {
        wait_queue_head_t inq, outq;       /* read and write queues */
        char *buffer, *end;                /* begin of buf, end of buf */
        int buffersize;                    /* used in pointer arithmetic */
        char *rp, *wp;                     /* where to read, where to write */
        int nreaders, nwriters;            /* number of openings for r/w */
        struct fasync_struct *async_queue; /* asynchronous readers */
        struct semaphore sem;              /* mutual exclusion semaphore */
        struct cdev cdev;                  /* Char device structure */
};

int scull_p_nr_devs;            /* the number of scull_pipe devices */
scull_pipe *scull_p_devices;    /* scull_pipe devices to be malloc'ed */

/* ...... */

/* our proc read implementation */
static int scull_read_p_mem(char *buf, char **start, off_t offset, int count,
        int *eof, void *data)
{
    int i, len;
    struct scull_pipe *p;

#define LIMIT (PAGE_SIZE-200)   /* don't print any more after this size */
    *start = buf;
    len = sprintf(buf, "Default buffersize is %i\n", scull_p_buffer);
    for(i = 0; i < scull_p_nr_devs && len <= LIMIT; i++) {
        p = &scull_p_devices[i];
        if (down_interruptible(&p->sem))
            return -ERESTARTSYS;
        len += sprintf(buf+len, "\nDevice %i: %p\n", i, p);
        len += sprintf(buf+len, "   Buffer: %p to %p (%i bytes)\n",
                                    p->buffer, p->end, p->buffersize);
        len += sprintf(buf+len, "   rp %p   wp %p\n", p->rp, p->wp);
        len += sprintf(buf+len, "   readers %i   writers %i\n",
                                    p->nreaders, p->nwriters);
        up(&p->sem);
        scullp_proc_offset(buf, start, &offset, &len);
    }
    *eof = (len <= LIMIT);
    return len;
}

static void scullp_proc_offset(char *buf, char **start, off_t *offset, int *len)
{
    /* QUESTION: what does this function do? */
    if (*offset == 0)
        return;
    if (*offset >= *len) {
        *offset -= *len;    /* QUESTION: what is the purpose of this? */
        *len = 0;
    }
    else {
        *start = buf + *offset; /* QUESTION: why do you need to change "start"? */
        *offset = 0;
    }
}

</code>```

答:
The function scull_read_p_mem is used to create a proc entry here using create_proc_read_entry function. A 5 minut google search gave this page which explain the parameters in the function pointer passed to create_proc_read_entry function. With fixed formatting is sais:

Arguments:

  *buf : The kernel allocates a page of memory to any process that attempts to read a proc entry. The page pointer points to that buffer
  of memory into which the data is written.

  **start: This pointer is used when the reading of the proc file should not start from the beginning of the file but from a certain offset. For small reads this is generally set to NULL.

  off : The offset from the beginning of the file where the file pointer currently points to

  count : The number of bytes of data to be read

  data : The data passed from the create_read_proc_entry function call.

  eof: is set to 1 to indicate end of file

But after a while i've also found some docs in kenel fs/proc/generic.c. It's a bit long, but I think it's the only source to sum up the start parameter:

```<code>        /*
         * How to be a proc read function
         * ------------------------------
         * Prototype:
         *    int f(char *buffer, char **start, off_t offset,
         *          int count, int *peof, void *dat)
         *
         * Assume that the buffer is "count" bytes in size.
         *
         * If you know you have supplied all the data you
         * have, set *peof.
         *
         * You have three ways to return data:
         * 0) Leave *start = NULL.  (This is the default.)
         *    Put the data of the requested offset at that
         *    offset within the buffer.  Return the number (n)
         *    of bytes there are from the beginning of the
         *    buffer up to the last byte of data.  If the
         *    number of supplied bytes (= n - offset) is
         *    greater than zero and you didn't signal eof
         *    and the reader is prepared to take more data
         *    you will be called again with the requested
         *    offset advanced by the number of bytes
         *    absorbed.  This interface is useful for files
         *    no larger than the buffer.
         * 1) Set *start = an unsigned long value less than
         *    the buffer address but greater than zero.
         *    Put the data of the requested offset at the
         *    beginning of the buffer.  Return the number of
         *    bytes of data placed there.  If this number is
         *    greater than zero and you didn't signal eof
         *    and the reader is prepared to take more data
         *    you will be called again with the requested
         *    offset advanced by *start.  This interface is
         *    useful when you have a large file consisting
         *    of a series of blocks which you want to count
         *    and return as wholes.
         *    (Hack by Paul.Russell@rustcorp.com.au)
         * 2) Set *start = an address within the buffer.
         *    Put the data of the requested offset at *start.
         *    Return the number of bytes of data placed there.
         *    If this number is greater than zero and you
         *    didn't signal eof and the reader is prepared to
         *    take more data you will be called again with the
         *    requested offset advanced by the number of bytes
         *    absorbed.
         */
</code>```

We can see the start used later inside copy_to_user - the parameter is used to optimize proc entry reads on biiiig files. The user can pass the count variable really small, but you have biig file to read. So you return the size of that file from the proc read function with a *start parameter, that says how many bytes are there to read. That way kernel can even pass count=0, but the proc_read function can return like 5000 with a valid *start address, it will be later used in copy_to_user call to speed up the read.

So:

what do the parameters in static int scull_read_p_mem(char *buf, char **start, off_t offset, int count, int *eof, void *data) mean?

buf - destination buffer to copy the result to

start - the magic pointer explained in the comment above used to speed up proc reading.

offset - offset in the file to read from

count - count of bytes to read

eof - a pointer to int, that need to be set to nonzero, in case the whole file is read

data - user context, passed as the last parameter in the create_proc_entry function.

There are a number of questions I have regarding the actual implementation itself (which can be found below):

The scullp_proc_offset manipulates the len offset inside the buf buffer. If offset != 0, then the scull_read_p_mem needs not to read from the first byte, but some byte offset. Because it is lazily written, then snprintf calls execute anyways, you need "kind of shift" the buffer.

what does this function do? - Actually I see it is a funny way of counting how many bytes were/need to be copied to the user.

what is the purpose of this? - No idea. Looks buggy tho, as *offset will become negative. The comment above the function /* FIXME this should use seq_file */ says there is something left to fix. I think the ides is return exactly information about one scull_p_devices[i] in one call.

why do you need to change "start"? - which comes to this. If *offset is different then 0 and if we have some bytes to read, we should return a pointer to buf + offset, to let kernel know from where to read. Note, that *start = buf was initialized already, so the kernel will do copy_to_user(... *start, len).

Body:
In LDD3's (Linux Device Drivers 3rd Edition book) scullpipe, what do the parameters in static int scull_read_p_mem(char *buf, char **start, off_t offset, int count, int *eof, void *data) mean? Specifically, I don't understand the difference between start, page, and offset.

There are a number of questions I have regarding the actual implementation itself (which can be found below):

struct scull_pipe {
        wait_queue_head_t inq, outq;       /* read and write queues */
        char *buffer, *end;                /* begin of buf, end of buf */
        int buffersize;                    /* used in pointer arithmetic */
        char *rp, *wp;                     /* where to read, where to write */
        int nreaders, nwriters;            /* number of openings for r/w */
        struct fasync_struct *async_queue; /* asynchronous readers */
        struct semaphore sem;              /* mutual exclusion semaphore */
        struct cdev cdev;                  /* Char device structure */
};

int scull_p_nr_devs;            /* the number of scull_pipe devices */
scull_pipe *scull_p_devices;    /* scull_pipe devices to be malloc'ed */

/* ...... */

/* our proc read implementation */
static int scull_read_p_mem(char *buf, char **start, off_t offset, int count,
        int *eof, void *data)
{
    int i, len;
    struct scull_pipe *p;

#define LIMIT (PAGE_SIZE-200)   /* don't print any more after this size */
    *start = buf;
    len = sprintf(buf, "Default buffersize is %i\n", scull_p_buffer);
    for(i = 0; i < scull_p_nr_devs && len <= LIMIT; i++) {
        p = &scull_p_devices[i];
        if (down_interruptible(&p->sem))
            return -ERESTARTSYS;
        len += sprintf(buf+len, "\nDevice %i: %p\n", i, p);
        len += sprintf(buf+len, "   Buffer: %p to %p (%i bytes)\n",
                                    p->buffer, p->end, p->buffersize);
        len += sprintf(buf+len, "   rp %p   wp %p\n", p->rp, p->wp);
        len += sprintf(buf+len, "   readers %i   writers %i\n",
                                    p->nreaders, p->nwriters);
        up(&p->sem);
        scullp_proc_offset(buf, start, &offset, &len);
    }
    *eof = (len <= LIMIT);
    return len;
}

static void scullp_proc_offset(char *buf, char **start, off_t *offset, int *len)
{
    /* QUESTION: what does this function do? */
    if (*offset == 0)
        return;
    if (*offset >= *len) {
        *offset -= *len;    /* QUESTION: what is the purpose of this? */
        *len = 0;
    }
    else {
        *start = buf + *offset; /* QUESTION: why do you need to change "start"? */
        *offset = 0;
    }
}

Body:
The function scull_read_p_mem is used to create a proc entry here using create_proc_read_entry function. A 5 minut google search gave this page which explain the parameters in the function pointer passed to create_proc_read_entry function. With fixed formatting is sais:

Arguments:

  *buf : The kernel allocates a page of memory to any process that attempts to read a proc entry. The page pointer points to that buffer
  of memory into which the data is written.

  **start: This pointer is used when the reading of the proc file should not start from the beginning of the file but from a certain offset. For small reads this is generally set to NULL.

  off : The offset from the beginning of the file where the file pointer currently points to

  count : The number of bytes of data to be read

  data : The data passed from the create_read_proc_entry function call.

  eof: is set to 1 to indicate end of file

But after a while i've also found some docs in kenel fs/proc/generic.c. It's a bit long, but I think it's the only source to sum up the start parameter:

        /*
         * How to be a proc read function
         * ------------------------------
         * Prototype:
         *    int f(char *buffer, char **start, off_t offset,
         *          int count, int *peof, void *dat)
         *
         * Assume that the buffer is "count" bytes in size.
         *
         * If you know you have supplied all the data you
         * have, set *peof.
         *
         * You have three ways to return data:
         * 0) Leave *start = NULL.  (This is the default.)
         *    Put the data of the requested offset at that
         *    offset within the buffer.  Return the number (n)
         *    of bytes there are from the beginning of the
         *    buffer up to the last byte of data.  If the
         *    number of supplied bytes (= n - offset) is
         *    greater than zero and you didn't signal eof
         *    and the reader is prepared to take more data
         *    you will be called again with the requested
         *    offset advanced by the number of bytes
         *    absorbed.  This interface is useful for files
         *    no larger than the buffer.
         * 1) Set *start = an unsigned long value less than
         *    the buffer address but greater than zero.
         *    Put the data of the requested offset at the
         *    beginning of the buffer.  Return the number of
         *    bytes of data placed there.  If this number is
         *    greater than zero and you didn't signal eof
         *    and the reader is prepared to take more data
         *    you will be called again with the requested
         *    offset advanced by *start.  This interface is
         *    useful when you have a large file consisting
         *    of a series of blocks which you want to count
         *    and return as wholes.
         *    (Hack by Paul.Russell@rustcorp.com.au)
         * 2) Set *start = an address within the buffer.
         *    Put the data of the requested offset at *start.
         *    Return the number of bytes of data placed there.
         *    If this number is greater than zero and you
         *    didn't signal eof and the reader is prepared to
         *    take more data you will be called again with the
         *    requested offset advanced by the number of bytes
         *    absorbed.
         */

We can see the start used later inside copy_to_user - the parameter is used to optimize proc entry reads on biiiig files. The user can pass the count variable really small, but you have biig file to read. So you return the size of that file from the proc read function with a *start parameter, that says how many bytes are there to read. That way kernel can even pass count=0, but the proc_read function can return like 5000 with a valid *start address, it will be later used in copy_to_user call to speed up the read.

So:

what do the parameters in static int scull_read_p_mem(char *buf, char **start, off_t offset, int count, int *eof, void *data) mean?

buf - destination buffer to copy the result to

start - the magic pointer explained in the comment above used to speed up proc reading.

offset - offset in the file to read from

count - count of bytes to read

eof - a pointer to int, that need to be set to nonzero, in case the whole file is read

data - user context, passed as the last parameter in the create_proc_entry function.

There are a number of questions I have regarding the actual implementation itself (which can be found below):

The scullp_proc_offset manipulates the len offset inside the buf buffer. If offset != 0, then the scull_read_p_mem needs not to read from the first byte, but some byte offset. Because it is lazily written, then snprintf calls execute anyways, you need "kind of shift" the buffer.

what does this function do? - Actually I see it is a funny way of counting how many bytes were/need to be copied to the user.

what is the purpose of this? - No idea. Looks buggy tho, as *offset will become negative. The comment above the function /* FIXME this should use seq_file */ says there is something left to fix. I think the ides is return exactly information about one scull_p_devices[i] in one call.

why do you need to change "start"? - which comes to this. If *offset is different then 0 and if we have some bytes to read, we should return a pointer to buf + offset, to let kernel know from where to read. Note, that *start = buf was initialized already, so the kernel will do copy_to_user(... *start, len).


答:
Since you kind of extracted the futures already, I'd suggest by just going with some dense layers and convert the "true" and "false" to a 1 and 0 respectively, and just use a sigmoid on the final dense layer.

Try to experiment with something simple first, see how it goes and continue from there on, need more help, just ask

EDIT

```<code>def generator(batch_size=10, nr_features=126):
    feed_data = np.zeros((batch_size, nr_features))
    labels = np.zeros(batch_size)
    i = 0
    for entry in data:
        if entry.pop(-1) == "true":
            labels[i] = 1
        else:
            labels[i] = 0
        feed_data[i, :] = np.array(entry).flatten()
        i += 1
        if not (i % batch_size):
            i = 0
            yield feed_data, labels

model = keras.Sequential()
model.add(keras.layers.Dense(126, input_dim=126))
model.add(keras.layers.Dense(20))
model.add(keras.layers.Dense(1))
model.add(keras.layers.Activation('sigmoid'))
model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])
for d, l in generator():
    model.train_on_batch(d, l)
</code>```

So what happens,

the data in the generator is your full data, I pop out the true/false, convert it to 1/0 and put it into the label array, I concat all features as a feature vecotr of 126. So feed_data.shape = (10, 126) and labels.shape = (10).

I feed that to a simple fully connected network, one that ends up with a sigmoid. Sigmoid is useful for probablilty, so in this case the output will be the probability that a feature vecotr is true. and I just feed the data.

Simple example, is not the full code but should get you started, I tested it, and it runs for me, I did not train anything yet though, that's something for you, good luck!

Oh, and questions, ask away

Title: How to write a classification algorithm in tensorflow using keras in python?

Body:
I have a training set of 2 images which has 64 features and a label attached to them i.e. matched/not matched.

How can I feed this data in a neural network using keras?

My data is as follows:

[
    [
        [
            239,
            1,
            255,
            255,
            255,
            255,
            2,
            0,
            130,
            3,
            1,
            101,
            22,
            154,
            0,
            240,
            30,
            0,
            2,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            128,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            71,
            150,
            212
        ],
        [
            239,
            1,
            255,
            255,
            255,
            255,
            2,
            0,
            130,
            3,
            1,
            101,
            22,
            154,
            0,
            240,
            30,
            0,
            2,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            128,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            71,
            150,
            212
        ],
        "true"
    ],
    [
        [
            239,
            1,
            255,
            255,
            255,
            255,
            2,
            0,
            130,
            3,
            1,
            81,
            28,
            138,
            0,
            241,
            254,
            128,
            6,
            0,
            2,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            128,
            0,
            128,
            2,
            128,
            2,
            192,
            6,
            224,
            6,
            224,
            62,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            13,
            62
        ],
        [
            239,
            1,
            255,
            255,
            255,
            255,
            2,
            0,
            130,
            3,
            1,
            81,
            28,
            138,
            0,
            241,
            254,
            128,
            6,
            0,
            2,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            128,
            0,
            128,
            2,
            128,
            2,
            192,
            6,
            224,
            6,
            224,
            62,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            13,
            62
        ],
        "true"
    ],
    ....
]

I want to train neural network so that after training if I provide it 2 array of 64 features then it should able to tell whether they matched or not?

Body:
Since you kind of extracted the futures already, I'd suggest by just going with some dense layers and convert the "true" and "false" to a 1 and 0 respectively, and just use a sigmoid on the final dense layer.

Try to experiment with something simple first, see how it goes and continue from there on, need more help, just ask

EDIT

def generator(batch_size=10, nr_features=126):
    feed_data = np.zeros((batch_size, nr_features))
    labels = np.zeros(batch_size)
    i = 0
    for entry in data:
        if entry.pop(-1) == "true":
            labels[i] = 1
        else:
            labels[i] = 0
        feed_data[i, :] = np.array(entry).flatten()
        i += 1
        if not (i % batch_size):
            i = 0
            yield feed_data, labels

model = keras.Sequential()
model.add(keras.layers.Dense(126, input_dim=126))
model.add(keras.layers.Dense(20))
model.add(keras.layers.Dense(1))
model.add(keras.layers.Activation('sigmoid'))
model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])
for d, l in generator():
    model.train_on_batch(d, l)

So what happens,

the data in the generator is your full data, I pop out the true/false, convert it to 1/0 and put it into the label array, I concat all features as a feature vecotr of 126. So feed_data.shape = (10, 126) and labels.shape = (10).

I feed that to a simple fully connected network, one that ends up with a sigmoid. Sigmoid is useful for probablilty, so in this case the output will be the probability that a feature vecotr is true. and I just feed the data.

Simple example, is not the full code but should get you started, I tested it, and it runs for me, I did not train anything yet though, that's something for you, good luck!

Oh, and questions, ask away


问:
ggplot2 histogram has solid line along x axis for which there are no values
I'm using ggplot2 to plot the frequency of distance measurements of various roads. Y axis is frequency, x axis is distance. I notice in all the plots (Not just this one) that there is a solid line along the 0 frequency value for all distances - see graph here:

For example, in the image I provided, the maximum road distance is 25, but the line stretches to 30. No matter what I set the xlim to, the line stretches to that maximum distance. I'm not sure what in the code is causing this. Below is the code I'm using to get this:

```<code>ggplot(ln_jan, aes(x=kilo, color=zone_sm)) +
  geom_histogram(fill="black", alpha=.8, position="identity", size =1.15)+
  xlim(0, 30)+
  ylim(0, 4000)+
  ggtitle("Road lengths")+
  ylab("Frequency")+
  xlab("Distance (km)")+
  theme(plot.title = element_text(hjust = 0.5, size = 21, face = "bold"))+
  scale_color_discrete(name = "road types",
  labels=c("highways", "small roads"))+
  theme(axis.text=element_text(size=10, face = "bold"),
        axis.title=element_text(size=14,face="bold"))+
  theme(panel.background = element_rect(fill = 'gray70'))+
  theme(plot.title = element_text(size=26))
</code>```

Here is the head of the dataset for reproducing the problem:

```<code>ID     kilo       zone_sm
185   12.522931      NW
234   12.702159      NW
25315  1.939652      NE
25411  1.938117      NE
25507  1.936778      NE
25603  1.935634      NE
</code>```

As requested here is the dput(hist(ln_jan$kilo)):

```<code>structure(list(breaks = c(0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26), counts = c(6079L, 8898L, 4240L, 2414L, 1677L, 986L, 760L, 609L, 394L, 639L, 338L, 53L, 14L), density = c(0.112154533043061, 0.1641636839969, 0.078225895723405, 0.0445371019519575, 0.0309398177189034, 0.0181912106564333, 0.0140216228183462, 0.0112357477583853, 0.00726910446109, 0.0117892328696358, 0.00623593225342238, 0.000977823696542563, 0.000258293051916903), mids = c(1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25), xname = "ln_jan$kilo", equidist = TRUE), class = "histogram")
</code>```

Here is the length(which(ln_jan$kilo>25)) result: 1. The value of this one is 25.01803... I rounded a little

答:
The issue that you're seeing is that specifying xlim() means that ggplot has to show the frequency of observations in the bins up to 30, which means you get bins with 0 frequency up to that limit - the thickness of your line is making it way more obvious than it would otherwise be. You can use coord_cartesian() instead of xlim() to show the x axis you want, and leave the styling of your graph the same. Here is an example using a recreated data set:

```<code>set.seed(1)
df <- data.frame(x=exp(rnorm(100)))

p <- ggplot(df, aes(x)) +
  geom_histogram(fill="transparent", colour="black", size=2)

p + xlim(0,15)
p + coord_cartesian(xlim=c(0,15)) #<-- this figure shown
</code>```

Updated code for your plot would look like:

```<code>ggplot(ln_jan, aes(x=kilo, color=zone_sm)) +
  geom_histogram(fill="black", alpha=.8, position="identity", size =1.15)+
  coord_cartesian(xlim=c(0, 30), ylim=c(0,4000)) +
  ggtitle("Road lengths")+
  ylab("Frequency")+
  xlab("Distance (km)")+
  theme(plot.title = element_text(hjust = 0.5, size = 21, face = "bold"))+
  scale_color_discrete(name = "road types",
  labels=c("highways", "small roads"))+
  theme(axis.text=element_text(size=10, face = "bold"),
        axis.title=element_text(size=14,face="bold"))+
  theme(panel.background = element_rect(fill = 'gray70'))+
  theme(plot.title = element_text(size=26))
</code>```

Body:
I'm using ggplot2 to plot the frequency of distance measurements of various roads. Y axis is frequency, x axis is distance. I notice in all the plots (Not just this one) that there is a solid line along the 0 frequency value for all distances - see graph here:

For example, in the image I provided, the maximum road distance is 25, but the line stretches to 30. No matter what I set the xlim to, the line stretches to that maximum distance. I'm not sure what in the code is causing this. Below is the code I'm using to get this:

ggplot(ln_jan, aes(x=kilo, color=zone_sm)) +
  geom_histogram(fill="black", alpha=.8, position="identity", size =1.15)+
  xlim(0, 30)+
  ylim(0, 4000)+
  ggtitle("Road lengths")+
  ylab("Frequency")+
  xlab("Distance (km)")+
  theme(plot.title = element_text(hjust = 0.5, size = 21, face = "bold"))+
  scale_color_discrete(name = "road types",
  labels=c("highways", "small roads"))+
  theme(axis.text=element_text(size=10, face = "bold"),
        axis.title=element_text(size=14,face="bold"))+
  theme(panel.background = element_rect(fill = 'gray70'))+
  theme(plot.title = element_text(size=26))

Here is the head of the dataset for reproducing the problem:

ID     kilo       zone_sm
185   12.522931      NW
234   12.702159      NW
25315  1.939652      NE
25411  1.938117      NE
25507  1.936778      NE
25603  1.935634      NE

As requested here is the dput(hist(ln_jan$kilo)):

structure(list(breaks = c(0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26), counts = c(6079L, 8898L, 4240L, 2414L, 1677L, 986L, 760L, 609L, 394L, 639L, 338L, 53L, 14L), density = c(0.112154533043061, 0.1641636839969, 0.078225895723405, 0.0445371019519575, 0.0309398177189034, 0.0181912106564333, 0.0140216228183462, 0.0112357477583853, 0.00726910446109, 0.0117892328696358, 0.00623593225342238, 0.000977823696542563, 0.000258293051916903), mids = c(1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25), xname = "ln_jan$kilo", equidist = TRUE), class = "histogram")

Here is the length(which(ln_jan$kilo>25)) result: 1. The value of this one is 25.01803... I rounded a little

Body:
The issue that you're seeing is that specifying xlim() means that ggplot has to show the frequency of observations in the bins up to 30, which means you get bins with 0 frequency up to that limit - the thickness of your line is making it way more obvious than it would otherwise be. You can use coord_cartesian() instead of xlim() to show the x axis you want, and leave the styling of your graph the same. Here is an example using a recreated data set:

set.seed(1)
df <- data.frame(x=exp(rnorm(100)))

p <- ggplot(df, aes(x)) +
  geom_histogram(fill="transparent", colour="black", size=2)

p + xlim(0,15)
p + coord_cartesian(xlim=c(0,15)) #<-- this figure shown

Updated code for your plot would look like:

ggplot(ln_jan, aes(x=kilo, color=zone_sm)) +
  geom_histogram(fill="black", alpha=.8, position="identity", size =1.15)+
  coord_cartesian(xlim=c(0, 30), ylim=c(0,4000)) +
  ggtitle("Road lengths")+
  ylab("Frequency")+
  xlab("Distance (km)")+
  theme(plot.title = element_text(hjust = 0.5, size = 21, face = "bold"))+
  scale_color_discrete(name = "road types",
  labels=c("highways", "small roads"))+
  theme(axis.text=element_text(size=10, face = "bold"),
        axis.title=element_text(size=14,face="bold"))+
  theme(panel.background = element_rect(fill = 'gray70'))+
  theme(plot.title = element_text(size=26))


问:
How to convert an array?
I need to reformat an array from one type to another using library "Lodash".

I initially have such an array:

```<code>[{
    "language": {
      "displayName": {
        "en": "English"
      },
      "id": 1
    },
    "level": {
      "id": 1,
      "displayName": {
        "en": "Intermediate"
      }
    }
  }, {
    "language": {
      "displayName": {
        "en": "Russian"
      },
      "id": 2
    },
    "level": {
      "id": 1
      "displayName": {
        "en": "Fluent"
      }
    }
  }]
</code>```

How to convert an array in this format:

```<code>[{
    "language": 1, // it is language.id
    "level": 1 // it is level.id
  }, {
    "language": 2, // it is language.id
    "level": 2 // it is level.id
}]
</code>```

答:
You can use a simple map using nested destructuring to extract the properties you want:

const arr = [{"language":{"displayName":{"en":"English"},"id":1},"level":{"id":1,"displayName":{"en":"Intermediate"}}},{"language":{"displayName":{"en":"Russian"},"id":2},"level":{"id":1,"displayName":{"en":"Fluent"}}}];
const res = arr.map(({ language: { id: language }, level: { id: level }}) => ({ language, level }));
console.log(res);```
<pre class="snippet-code-css lang-css prettyprint-override"><code>.as-console-wrapper { max-height: 100% !important; top: auto; }</code>```

Here's a Lodash version using _.map and _.mapValues (to avoid _.map of _.values which is more verbose):

const arr = [{"language":{"displayName":{"en":"English"},"id":1},"level":{"id":1,"displayName":{"en":"Intermediate"}}},{"language":{"displayName":{"en":"Russian"},"id":2},"level":{"id":1,"displayName":{"en":"Fluent"}}}];
const res = _.map(arr, e => _.mapValues(e, ({ id }) => id));
console.log(res);```
<pre class="snippet-code-css lang-css prettyprint-override"><code>.as-console-wrapper { max-height: 100% !important; top: auto; }</code>```

```

Title: How to convert an array?

Body:
I need to reformat an array from one type to another using library "Lodash".

I initially have such an array:

[{
    "language": {
      "displayName": {
        "en": "English"
      },
      "id": 1
    },
    "level": {
      "id": 1,
      "displayName": {
        "en": "Intermediate"
      }
    }
  }, {
    "language": {
      "displayName": {
        "en": "Russian"
      },
      "id": 2
    },
    "level": {
      "id": 1
      "displayName": {
        "en": "Fluent"
      }
    }
  }]

How to convert an array in this format:

[{
    "language": 1, // it is language.id
    "level": 1 // it is level.id
  }, {
    "language": 2, // it is language.id
    "level": 2 // it is level.id
}]

Body:
You can use a simple map using nested destructuring to extract the properties you want:

const arr = [{"language":{"displayName":{"en":"English"},"id":1},"level":{"id":1,"displayName":{"en":"Intermediate"}}},{"language":{"displayName":{"en":"Russian"},"id":2},"level":{"id":1,"displayName":{"en":"Fluent"}}}];
const res = arr.map(({ language: { id: language }, level: { id: level }}) => ({ language, level }));
console.log(res);

.as-console-wrapper { max-height: 100% !important; top: auto; }

Here's a Lodash version using _.map and _.mapValues (to avoid _.map of _.values which is more verbose):

const arr = [{"language":{"displayName":{"en":"English"},"id":1},"level":{"id":1,"displayName":{"en":"Intermediate"}}},{"language":{"displayName":{"en":"Russian"},"id":2},"level":{"id":1,"displayName":{"en":"Fluent"}}}];
const res = _.map(arr, e => _.mapValues(e, ({ id }) => id));
console.log(res);

.as-console-wrapper { max-height: 100% !important; top: auto; }

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>


问:
RSA implementation decryption/ encryption
I'm trying to implement RSA. But I've got a few problems with this. I´m trying to encrypt a String with 2 prime numbers.

```<code>p= 1606938044258990275541962092341162602522202993782792835301611
q= 3213876088517980551083924184682325205044405987565585670603103
</code>```

First I do what has to be done for the RSA algorithm. After I've encrypted the String I tried to decrypt it as well. But the result is something like this: ÜŞϟʐͶz̽ć

```<code>def xgcd(a, b):
    """return (g, x, y) such that a*x + b*y = g = gcd(a, b)"""
    x0, x1, y0, y1 = 0, 1, 1, 0
    while a != 0:
        q, b, a = b // a, a, b % a
        y0, y1 = y1, y0 - q * y1
        x0, x1 = x1, x0 - q * x1
    return b, x0, y0

def genKeypair(p, q):

    n = p * q
    phiN = (p - 1) * (q - 1)
    e = 65537

    d = egcd(phiN, e)
    return n, e, d

# encrypt message and return cipher
def encrypt(m, n, e):
    m1 = ""
    # Turn message string into ascii so it can be used for encryption
    for x in range(len(m)):
        m1 += '{0:03}'.format(ord(m[x]))
    # encrypt
    c = squareAndMultiply(int(m1), e, n)
    print(c)
    return c

# decrypt cipher and return message
def decrypt(c, n, d):
    # decrypt c
    m = squareAndMultiply(c, d, n) #% n
    # put decryption result into ascii format and use ascii to decode it
    m = str(m)
    tmp = ""
    message = ""
    i = 0
    if int(m[0] + m[1] + m[3]) > 255:
        m = "0" + m
    for x in range(len(m)):
        tmp = tmp + m[x]
        i += 1
        if i % 3 == 0:
            message += chr(int(tmp))
            tmp = ""
    return message
</code>```

My square and multiply method looks like this:

```<code>def squareAndMultiply(x, n, m=0):

# turn exponent into binary
bin = '{0:b}'.format(n)
r = x

# loop through the string representing the binary form of the exponent while ignoring the leftmost bit and perform
# the appropriate operations on r
for i in range(1, len(bin)):
    if (bin[i] == "0"):
        r *= r % m
    else:
        r *= r % m
        r *= x % m

# check for m being greater than 0, ignore it otherwise
if (m > 0):
    return r % m
else:
    return r
</code>```

Has anyone an idea what could be wrong and what has to be changed, that the decryption gives the right answer?

答:
In the code, the plaintext is encoded into a string by using the corresponding decimal ASCII-code (formatted to three digits) for each character, e.g.

```<code>ABCDEF -> 065066067068069070
</code>```

Then, the string is converted to an integer m which is used as message and encrypted according to

This concept results in m becoming larger and larger with increasing plaintext length. At some point m violates the condition m < n and the algorithm fails (see RSA, Operation)!

The plaintext length at which the algorithm fails depends on n and can be determined as follows: In the example, n is a 121-digit number. Because 121/3 = 40.33..., a maximum of 40 characters can be encrypted without violating the condition m < n, i.e. from incl. 41 the encryption will generally fail. This can be verified with the following code:

```<code>p = 1606938044258990275541962092341162602522202993782792835301611
q = 3213876088517980551083924184682325205044405987565585670603103

n = p * q
phiN = (p - 1) * (q - 1)
e = 65537
d = egcd(phiN, e)[2]

encrypted = encrypt('0123456789012345678901234567890123456789', n, e);      # will succeed
#encrypted = encrypt('01234567890123456789012345678901234567890', n, e);    # will fail
decrypted = decrypt(encrypted, n, d);
print('>' + decrypted + '<')
</code>```

A possible solution to this problem is to divide the plaintext into blocks with the same number of characters (the last block usually contains fewer characters). This number should correspond to the maximum possible number of characters, so that the condition m < n is not violated (= 40 in the posted example). Then each block is individually encoded and encrypted (in the same way as before).

Body:
I'm trying to implement RSA. But I've got a few problems with this. I´m trying to encrypt a String with 2 prime numbers.

p= 1606938044258990275541962092341162602522202993782792835301611
q= 3213876088517980551083924184682325205044405987565585670603103

First I do what has to be done for the RSA algorithm. After I've encrypted the String I tried to decrypt it as well. But the result is something like this: ÜŞϟʐͶz̽ć

def xgcd(a, b):
    """return (g, x, y) such that a*x + b*y = g = gcd(a, b)"""
    x0, x1, y0, y1 = 0, 1, 1, 0
    while a != 0:
        q, b, a = b // a, a, b % a
        y0, y1 = y1, y0 - q * y1
        x0, x1 = x1, x0 - q * x1
    return b, x0, y0

def genKeypair(p, q):

    n = p * q
    phiN = (p - 1) * (q - 1)
    e = 65537

    d = egcd(phiN, e)
    return n, e, d

# encrypt message and return cipher
def encrypt(m, n, e):
    m1 = ""
    # Turn message string into ascii so it can be used for encryption
    for x in range(len(m)):
        m1 += '{0:03}'.format(ord(m[x]))
    # encrypt
    c = squareAndMultiply(int(m1), e, n)
    print(c)
    return c

# decrypt cipher and return message
def decrypt(c, n, d):
    # decrypt c
    m = squareAndMultiply(c, d, n) #% n
    # put decryption result into ascii format and use ascii to decode it
    m = str(m)
    tmp = ""
    message = ""
    i = 0
    if int(m[0] + m[1] + m[3]) > 255:
        m = "0" + m
    for x in range(len(m)):
        tmp = tmp + m[x]
        i += 1
        if i % 3 == 0:
            message += chr(int(tmp))
            tmp = ""
    return message

My square and multiply method looks like this:

def squareAndMultiply(x, n, m=0):

# turn exponent into binary
bin = '{0:b}'.format(n)
r = x

# loop through the string representing the binary form of the exponent while ignoring the leftmost bit and perform
# the appropriate operations on r
for i in range(1, len(bin)):
    if (bin[i] == "0"):
        r *= r % m
    else:
        r *= r % m
        r *= x % m

# check for m being greater than 0, ignore it otherwise
if (m > 0):
    return r % m
else:
    return r

Has anyone an idea what could be wrong and what has to be changed, that the decryption gives the right answer?

Body:
In the code, the plaintext is encoded into a string by using the corresponding decimal ASCII-code (formatted to three digits) for each character, e.g.

ABCDEF -> 065066067068069070

Then, the string is converted to an integer m which is used as message and encrypted according to

This concept results in m becoming larger and larger with increasing plaintext length. At some point m violates the condition m < n and the algorithm fails (see RSA, Operation)!

The plaintext length at which the algorithm fails depends on n and can be determined as follows: In the example, n is a 121-digit number. Because 121/3 = 40.33..., a maximum of 40 characters can be encrypted without violating the condition m < n, i.e. from incl. 41 the encryption will generally fail. This can be verified with the following code:

p = 1606938044258990275541962092341162602522202993782792835301611
q = 3213876088517980551083924184682325205044405987565585670603103

n = p * q
phiN = (p - 1) * (q - 1)
e = 65537
d = egcd(phiN, e)[2]

encrypted = encrypt('0123456789012345678901234567890123456789', n, e);      # will succeed
#encrypted = encrypt('01234567890123456789012345678901234567890', n, e);    # will fail
decrypted = decrypt(encrypted, n, d);
print('>' + decrypted + '<')

A possible solution to this problem is to divide the plaintext into blocks with the same number of characters (the last block usually contains fewer characters). This number should correspond to the maximum possible number of characters, so that the condition m < n is not violated (= 40 in the posted example). Then each block is individually encoded and encrypted (in the same way as before).


问:
Express api fetch request returns undefined
Edit:  I don't ask anything about cors here

So I have spent hours trying to figure out what's goin on, and well ... I'm losing it...

So I have this simple app that has the server part made in nodejs and the front end in react, they run on diferent ports so I used cors module.

Anyway
So this is my nodejs api using express

```<code>app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
app.get('/', (req, res) => {
  res.send({data:"Hello"});
});
app.listen(port, () => console.log(`Listening on port ${port}`));
</code>```

and then from react the following fetch request

```<code>    fetch("http://www.localhost:5000/",{
        method: 'GET',
        mode: "no-cors",
        cache: "no-cache",
        credentials: "same-origin",
        headers: {"Content-Type": "application/json"}
      })
    .then(res=>{
        console.log("res1",res)
        res.text();
    })
    .then(res=>{
        console.log("res2",res)
    })
    .catch(res=>{
        console.log("Exception : ",res);
    })
</code>```

The thing is this req returns undefined whatever I try.
In res 1 the response is an object ( in this case opaque )

Response {type: "opaque", url: "", redirected: false, status: 0, ok:
  false, …}

And well res2 is undefined.

What am I missing ?

答:
```<code>fetch("http://www.localhost:5000/",{
        method: 'GET',
        mode: "no-cors",
        cache: "no-cache",
        credentials: "same-origin",
        headers: {"Content-Type": "application/json"}
      })
    .then(res=>{
        console.log("res1",res)
        return res.text();
    })
    .then(res=>{
        console.log("res2",res)

    })
    .catch(res=>{
        console.log("Exception : ",res);
    })
</code>```

Body:
Edit:  I don't ask anything about cors here

So I have spent hours trying to figure out what's goin on, and well ... I'm losing it...

So I have this simple app that has the server part made in nodejs and the front end in react, they run on diferent ports so I used cors module.

Anyway
So this is my nodejs api using express

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
app.get('/', (req, res) => {
  res.send({data:"Hello"});
});
app.listen(port, () => console.log(`Listening on port ${port}`));

and then from react the following fetch request

    fetch("http://www.localhost:5000/",{
        method: 'GET',
        mode: "no-cors",
        cache: "no-cache",
        credentials: "same-origin",
        headers: {"Content-Type": "application/json"}
      })
    .then(res=>{
        console.log("res1",res)
        res.text();
    })
    .then(res=>{
        console.log("res2",res)
    })
    .catch(res=>{
        console.log("Exception : ",res);
    })

The thing is this req returns undefined whatever I try.
In res 1 the response is an object ( in this case opaque )

Response {type: "opaque", url: "", redirected: false, status: 0, ok:
  false, …}

And well res2 is undefined.

What am I missing ?


问:
Map Object Array to a Bean
I am making an SQL query via JPA and getting a List of Object Arrays. I wish to map these Object Arrays into a bean.

For example, my query gives me the following output.

```<code>List<Object[]> list = // myWorkingJpaQuery;
// list is of length 2.
// Each Object array always holds a Long in index 0,
// a TimeStamp in index 1 and a String in index 2.
</code>```

Instead of reading these values and performing casting, I wish to map it to a class as follows:

```<code>class ExampleClass{
    //all these variables matches the aliases in myWorkingJpaQuery.
    Long id;
    TimeStamp ts;
    String name;
    // get set
}
</code>```

Tried to use the above class my changing the JPA methods return type and assigning it in the calling class as follows but it doesn't work.

```<code>List<ExampleClass> list = // myWorkingJpaQuery with List<ExampleClass> as return type;
</code>```

Is there a way to do this? It is currently working fine if I stick to Object Array but just trying not to use Objects and castings. For reference, I am using Spring.

答:
Assuming you're using the native query (otherwise, the ORM tool will do this for you automatically):

You might have a code like this:

```<code> EntityManager em = ...
 Query q = em.createNativeQuery("SELECT ...");
 List<Object[]> results = q.getResultList();
</code>```

In this case, you might consider passing an additional parameter to createNativeQuery method:

```<code> Query q = em.createNativeQuery("SELECT...", ExampleClass.class);
 List<ExampleClass> results = q.getResultList();
</code>```

For more complicated/customized Mappings consider using Result Set Mappings feature

Title: Map Object Array to a Bean

Body:
I am making an SQL query via JPA and getting a List of Object Arrays. I wish to map these Object Arrays into a bean.

For example, my query gives me the following output.

List<Object[]> list = // myWorkingJpaQuery;
// list is of length 2.
// Each Object array always holds a Long in index 0,
// a TimeStamp in index 1 and a String in index 2.

Instead of reading these values and performing casting, I wish to map it to a class as follows:

class ExampleClass{
    //all these variables matches the aliases in myWorkingJpaQuery.
    Long id;
    TimeStamp ts;
    String name;
    // get set
}

Tried to use the above class my changing the JPA methods return type and assigning it in the calling class as follows but it doesn't work.

List<ExampleClass> list = // myWorkingJpaQuery with List<ExampleClass> as return type;

Is there a way to do this? It is currently working fine if I stick to Object Array but just trying not to use Objects and castings. For reference, I am using Spring.

Body:
Assuming you're using the native query (otherwise, the ORM tool will do this for you automatically):

You might have a code like this:

 EntityManager em = ...
 Query q = em.createNativeQuery("SELECT ...");
 List<Object[]> results = q.getResultList();

In this case, you might consider passing an additional parameter to createNativeQuery method:

 Query q = em.createNativeQuery("SELECT...", ExampleClass.class);
 List<ExampleClass> results = q.getResultList();

For more complicated/customized Mappings consider using Result Set Mappings feature


问:
Index 3 out of bounds for length 3 in fibonacci iterative error after reducing to three element array
I have a task where I should create fibonacci iterative using loop and array.
How can I check negative values for fibonacciNumberInOrder? My tutor told me that I don't need allocate N elements in array. I can reduce it to 3element array. And I should point this array to last three numbers of fibonacci.
Now I receive an error that: Index 3 out of bounds for length 3.
I guess it's because I've change this:

```<code>long [] fibonacci = new long [fibonacciNumberInOrder];
</code>```

to:

```<code>long [] fibonacci = new long [] {1,1,2};
</code>```

What should I do to make it work and keep my tutor's requirements?

```<code>    public class FibonacciIterative {

    public static void main(String[] args) {
        int fibonacciNumberInOrder = 5;
        fibonacci(fibonacciNumberInOrder);
    }

    public static long[] fibonacci(int fibonacciNumberInOrder) {
        long [] fibonacci = new long [] {1,1,2};
        for (int i = 2; i < fibonacciNumberInOrder; i++) {
            fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2];
            System.out.print(Arrays.toString(fibonacci) + ", ");
        }
        return fibonacci;
    }
}
</code>```

答:
The exception occurred because you are trying to access an element with index that is out of bounds. In your scenario you are trying to access the third element of an array using an index value of 3 when it should be a value of 2 instead. Here is a short excerpt from Java documentation about arrays:

Each item in an array is called an element, and each element is accessed by its numerical index. As shown in the preceding illustration, numbering begins with 0. The 9th element, for example, would therefore be accessed at index 8.

Body:
I have a task where I should create fibonacci iterative using loop and array.
How can I check negative values for fibonacciNumberInOrder? My tutor told me that I don't need allocate N elements in array. I can reduce it to 3element array. And I should point this array to last three numbers of fibonacci.
Now I receive an error that: Index 3 out of bounds for length 3.
I guess it's because I've change this:

long [] fibonacci = new long [fibonacciNumberInOrder];

to:

long [] fibonacci = new long [] {1,1,2};

What should I do to make it work and keep my tutor's requirements?

    public class FibonacciIterative {

    public static void main(String[] args) {
        int fibonacciNumberInOrder = 5;
        fibonacci(fibonacciNumberInOrder);
    }

    public static long[] fibonacci(int fibonacciNumberInOrder) {
        long [] fibonacci = new long [] {1,1,2};
        for (int i = 2; i < fibonacciNumberInOrder; i++) {
            fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2];
            System.out.print(Arrays.toString(fibonacci) + ", ");
        }
        return fibonacci;
    }
}


问:
What is the Best practice for try catch blocks to create clean code?

Possible Duplicate:

  Best practices for exception management in JAVA or C#

I've read a question earlier today on stackoverflow and it made me think about what is the best practice for handling exceptions.

So, my question is what is the best practice to handle exceptions to produce clean and high quality code.

Here is my code, I think it's quiet straight forward but please let me know if I'm wrong or not clear!
I've tried to keep in mind testability and same abstraction level in methods.

Every constructive comment is welcomed. :)

```<code>import java.awt.Point;
import java.io.Closeable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Preconditions;

/**
 * <p>This is a dummy code.</p>
 * The aim is present the best practice on exception separation and handling.
 */
public class ExceptionHandlingDemo {
    // System.out is not a good practice. Using logger is good for testing too (can be checked if the expected error messages are produced).
    private Logger logger = LoggerFactory.getLogger(ExceptionHandlingDemo.class);

    // instance of cannot work with List<Point>
    private interface PointList extends List<Point> {}

    /**
     * The method that loads a list of points from a file.
     * @param path - The path and the name of the file to be loaded.
     * Precondition: path cannot be {@code null}. In such case {@link NullPointerException} will be thrown.
     * Postcondition: if the file don't exist, some IOException occurs or the file doesn't contain the list the returned object is {@code null}.
     * */
    /* (Google throws NullpointerExceptio) since it is not forbidden for the developers to throw it. I know this is arguable but this is out of topic for now. */
    public List<Point> loadPointList(final String path) {
        Preconditions.checkNotNull(path, "The path of the file cannot be null");

        List<Point> pointListToReturn = null;
        ObjectInputStream in = null;

        try {
            in = openObjectInputStream(path);
            pointListToReturn = readPointList(in);
        } catch (final Throwable throwable) {
            handleException(throwable);
        } finally {
            close(in);
        }

        return pointListToReturn;
    }

    /*======== Private helper methods by now ========*/

    private ObjectInputStream openObjectInputStream(final String filename) throws FileNotFoundException, IOException {
        return new ObjectInputStream(new FileInputStream(filename));
    }

    private List<Point> readPointList(final ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
        final Object object = objectInputStream.readObject();

        List<Point> ret = null;

        if (object instanceof PointList) {
            ret = (PointList) object;
        }
        return ret;
    }

    private void handleException(final Throwable throwable) {
        // I don't know the best practice here ...
        logger.error(throwable.toString());
    }

    private void close(final Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException e) {
                logger.error("Failed closing: %s", closeable);
            }
        }
    }

    /*======== Getters and setters by now. ========*/

    // ...

    /**
     * @param args
     */
    public static void main(String[] args) {
        ExceptionHandlingDemo test = new ExceptionHandlingDemo();
        test.loadPointList("test-filename.ext");
    }
}
</code>```

EDITED:

What I want to avoid is writing lot of catch cases after each other...

答:
There some options you can choose.

One of the most important adnvantages of exceptions using is that you can have the only exception handler for all cases. When you write your code you should think about functionality first and only then about exception handling. As a result you may skip in some places try/catch at all for unchecked exceptions and declare your methods as throws SomeCheckedException for checked exceptions. This allows you to have the minimum number of exception handlers.

If you don't know what exactly to do with an exception, just rethrow it.

If you catch checked exception but you don't want clients that use your code have to process exceptions, you may checked exceptions update into unchecked. (Hibernate processes exceptions by this way. They catch checked sql exception and throw unchecked exceptions instead of this)

If the previous recommendation are not convenient for your, then you may also choose some options:

you may just add logging and rethrow the same exception

you may use exception chains by throwing new exception with initCause() method

you may think that code that invokes your code should not know anything about the originial exception, you may archive this either by creating new exception or by using fillInStackTrace() method.

Concerning catching Throwable. Yes in the methods you should not catch it. But as a rule the client that uses your programe need not to see excetpions at all, as a result, you may catch Throwable in the some exception handler that is the top in the chain.

ContentLicense: CC BY-SA 3.0

Body:
Possible Duplicate:

  Best practices for exception management in JAVA or C#

I've read a question earlier today on stackoverflow and it made me think about what is the best practice for handling exceptions.

So, my question is what is the best practice to handle exceptions to produce clean and high quality code.

Here is my code, I think it's quiet straight forward but please let me know if I'm wrong or not clear!
I've tried to keep in mind testability and same abstraction level in methods.

Every constructive comment is welcomed. :)

import java.awt.Point;
import java.io.Closeable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Preconditions;

/**
 *
This is a dummy code.

 * The aim is present the best practice on exception separation and handling.
 */
public class ExceptionHandlingDemo {
    // System.out is not a good practice. Using logger is good for testing too (can be checked if the expected error messages are produced).
    private Logger logger = LoggerFactory.getLogger(ExceptionHandlingDemo.class);

    // instance of cannot work with List<Point>
    private interface PointList extends List<Point> {}

    /**
     * The method that loads a list of points from a file.
     * @param path - The path and the name of the file to be loaded.
     * Precondition: path cannot be {@code null}. In such case {@link NullPointerException} will be thrown.
     * Postcondition: if the file don't exist, some IOException occurs or the file doesn't contain the list the returned object is {@code null}.
     * */
    /* (Google throws NullpointerExceptio) since it is not forbidden for the developers to throw it. I know this is arguable but this is out of topic for now. */
    public List<Point> loadPointList(final String path) {
        Preconditions.checkNotNull(path, "The path of the file cannot be null");

        List<Point> pointListToReturn = null;
        ObjectInputStream in = null;

        try {
            in = openObjectInputStream(path);
            pointListToReturn = readPointList(in);
        } catch (final Throwable throwable) {
            handleException(throwable);
        } finally {
            close(in);
        }

        return pointListToReturn;
    }

    /*======== Private helper methods by now ========*/

    private ObjectInputStream openObjectInputStream(final String filename) throws FileNotFoundException, IOException {
        return new ObjectInputStream(new FileInputStream(filename));
    }

    private List<Point> readPointList(final ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
        final Object object = objectInputStream.readObject();

        List<Point> ret = null;

        if (object instanceof PointList) {
            ret = (PointList) object;
        }
        return ret;
    }

    private void handleException(final Throwable throwable) {
        // I don't know the best practice here ...
        logger.error(throwable.toString());
    }

    private void close(final Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException e) {
                logger.error("Failed closing: %s", closeable);
            }
        }
    }

    /*======== Getters and setters by now. ========*/

    // ...

    /**
     * @param args
     */
    public static void main(String[] args) {
        ExceptionHandlingDemo test = new ExceptionHandlingDemo();
        test.loadPointList("test-filename.ext");
    }
}

EDITED:

What I want to avoid is writing lot of catch cases after each other...