mardi 22 juin 2021

How do I apply DRY principle?

I want to clean up my class that does web requests to the server. The problem that I ran into is a lot of my code is just repeating it self but each time with a little modification. For example almost the whole method looks the same except one is POST and the other is PATCH or GET request.

            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(req);
            httpWebRequest.Headers["Authorization"] = "Basic " + auth;
            httpWebRequest.Method = "POST";
            httpWebRequest.ContentType = "application/json";
            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = new JavaScriptSerializer().Serialize(new
                {
                    body = GetStringFromFile()
                }) ; 

                streamWriter.Write(json);
            }
            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(req);
            httpWebRequest.Headers["Authorization"] = "Basic " + auth;
            httpWebRequest.Method = "GET";
            httpWebRequest.ContentType = "application/json";

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

This is basically how my whole class looks like and I don't think it should look like that. I don't know if I should be extracting the already simple request to make it smaller and put it in another method, and do that for each type of request(post,get,patch). Or maybe just keep doing this even tho it looks wrong to me because it's repeating code that I don't know how to deal with. What would be the best solution in this case?

Difference between std::optional and boost::optional when its value is a variant

In embedded programming memory allocations are things that we want to avoid. So using design patterns like state design pattern is cumbersome. If we know how many states we will have then we can use placement new operations. But in this article the functional approach avoids all memory manipulations. Only drawback is that it is only for users that have C++17 availability. I tried to use boost::variant and boost::optional with c++11, but a conversion operator is missing like in in this minimal example (Here with MSVC with c++17 it compiles but not with std latest):

#include <string>
#include <boost/variant.hpp>
#include <boost/optional.hpp>


boost::optional<boost::variant<int, std::string>> f()
{
   return 5;
}


int main()
{
    auto ret = *f();
    return *boost::get<int>(&ret);
}

Error message:

error: no viable conversion from returned value of type 'int' to function return type 'boost::optional<boost::variant<int, std::string>>' (aka 'optional<variant<int, basic_string<char>>>')
return 5;

But if I replace boost::optional with std::optional it compiles fine: compiling example Can somebody explain what is the difference between them, and how can I make it work with boost also?

lundi 21 juin 2021

How to design a package that can potentially take any data source and convert it into an extensible, read-only configuration object?

I have a Java package which purpose is to receive configuration parameters from an external config file (XML, JSON, CSV, etc.), convert them into a POJO and return it in a form of an immutable, extensible object to the caller. Caller may only read parameters, but not modify them. If loading, parsing or conversion fail, the package must return a default configuration object with basic number of settings and predefined values. In the future the number of settings might increase and there might be another data source.

enter image description here

Basically, it's a black box with input and output. Input can be any data source, with basic settings, that can potentially increase. Output is a Java object, which contains all settings (that, again, can potentially increase). And it must be read-only. First, I thought about writing an interface with getters for basic settings, which can be extended if the settings list expands? Something like

interface Config { ... }

abstract class BasicConfig implements Config {...}

class ExtendedConfig extends BasicConfig {...}

Then I thought that maybe parsing XML into a HashMap will solve the problem should the settings list increase in the future. In this case I can make HashMap<String, Object> and read anything from it without knowing how many settings it contains.

What would be the best design solution here?

Looking for "find a design pattern for a problem" questions

In a job interview I was asked find a design pattern for the problem. I'm looking for these types of questions with answers. Do you any resource like this?

return/pass object reference from a method for non primitive data types/structures

Apart from the primitive datatypes since most of the Java data types/data structures are objects then what can be the reason for the return statement of a method? Passing object references as an actual parameter shall do an exact task. Then when we do a return from methods and when we shall pass an object references to a method?

Standard way of dealing with standardization for tabular data in PyTorch

This question is all about structuring the code in an elegant way that allows training from scratch, training using a model already trained and evaluating a model + doing the data normalization accordingly.

It is well known that a model works better if the data is standardized. Here is how I deal with this in the context of regression based on tabular data:

df_train, df_test, df_val = get_data(config)
if config['scaler'] is not None:
     scaler = config['scaler'] 
else:
    scaler = StandardScaler().fit(df[config['columns']], df[target]) # I only scale some numerical columns
df_train, df_test, df_val = scaler.transform(df_train), scaler.transform(df_test), scaler.transform(df_val)
best_model, test_predictions = train(df_train, df_test, df_val, config)
target_std, target_mean = get_scaler_coeffs(scaler)
test_predictions['y_hat'] = test_predictions['y_hat'] * target_std + target_mean
store_results(test_predictions, best_model, scaler, config)

In short, I use StandardScaler from sklearn to scale values (target included) and pandas for operating with csv files. After training, I store the best model along with its scaler and its results.

I am trying to write clean, elegant code. However, as you can see, this approach is not elegant at all, but I can’t figure out how I should change the code. What is the “standard” way of dealing with scaling and reverse scaling in pytorch? (preferably for regression purposes).

How best to create many similar formes [closed]

I have 10 pages and on each page I need to create a form. All 10 forms with the same design. Fields in different forms differ from each other, but can also be the same (id, name, phone number, email, etc.). I thought it was impractical to create 10 different forms. I came up with 2 solutions:

  1. Put the form fields into the array of objects, and render the fields depending on the array data in one general form.
  2. Make 10 different forms, but bring common fields (phone, email, etc.) into separate components.

Which of these solutions would be more correct? Or maybe there is an better solution?