mercredi 2 mai 2018

Get data from any framework / project without changing too much code

i did not find anything like this or maybe this has a specific name, which i did not figure out yet. So here is my question.

I have a framework / project which computes and generates data during its process. During this process i like to grab some data into a class which will be later collected and used by my own written project. The data type could be anything of int, float, vectors and so on.

My idea was to use something like a template logger class where you only include the header file, which gives you access to several functions which will save the data for me. For this i found the class / pattern called "Singleton".

I'm now wondering if this is the best solution for this problem. Maybe you have some ideas / pattern / links / code snipptes which shows me how to do this easily without changing to much code in the framework where i want so get the data from.

Thank you

c0op

C# Refactor class that has multiple methods with the same signature

I have a class with multiple methods that have the same signature:

string MethodA(int id);
string MethodB(int id);
string MethodC(int id);

The implementation of these methods differs obviously, but I'm trying to make this more SOLID. I got a long way with Composite pattern, where a Composite class would implement the IDoSomething interface below:

public Interface IDoSomething
{
   string DoSomething(int id);
}

public class CompositeClass : IDoSomething
{
    private readonly IEnumerable<IDoSomething> somethings;
    public CompositeClass(IEnumerable<IDoSomething> somethings)
    {
        this.somethings = somethings;
    }

    public string DoSomething(int id)
    {
        foreach (var s in somethings)
        {
            return s.DoSomething(id);
        }
    }
}

Problem is the result to return. This is different for each call. I could change the signature to string[] but that seems like a hack. Any ideas?

HTML5 input pattern validate letters and numbers

I need to validate password with these requirements:

At least 1 Letter, At least 1 Number, Min 6 chars and Max 12 chars, Special characters not allowed. Here's what I have so far.

<form>
Password: <input type="password" name="pw" pattern="^(?=.*[a-zA-Z])(?=.*[0-9]).{6,12}$" title="Must contain at least one number and one letter, and at least 6 to 12 characters (special characters not allowed)">
<input type="submit">
</form>

mardi 1 mai 2018

what are the architectures use for displaying notifications ?

In snap-chat , linked-In , twitter, Instagram and Google. what are the architectures their are using to build the notification system, what are the different of those architectures?

Organise code design-wise for web scraper

I'm working on a small application that is supposed to scrape/parse a few website and I'm wondering which would be the best way to achieve this (keeping DRY/SOLID in mind).

Here's some pseudocode:

class ScraperScheduler
  def perform
    SraperWorker.perform_async(ParserTypeOne.new)
    SraperWorker.perform_async(ParserTypeTwo.new)
    SraperWorker.perform_async(ParserTypeThree.new)
    SraperWorker.perform_async(ParserTypeFour.new)
  end
end

class ScraperWorker
  def initialize(scraper)
    @scraper = scraper
  end

  def perform
    html = RestClient.get(@scraper.url)
    @scraper.perform_async(html)
  end
end

class ParserTypeOne 
  def perform(html)
    #parse page with nokogiri
    page = Nokogiri::HTML(html)

    parserd_objects.each do |o|
      PersistToDB.perform(o)
    end
  end
end

class PersistToDB
  def perform(o)
    # split o into several ActiveRecord objects
    # check if unique and save to db
  end
end

The ScraperScheduler class is basically just a cronjob that will be called with sidekiq-scheduler once a day. The perform methods are there so I can basically make sidkiq jobs out of everything, but I don't think this is necessary for each one of those classes. Some questions/concerns I have:

  1. ScraperWorker basically only performs the HTTP request. Yet in my example it knows about the url and perform_async properties of the Parser. Any way to do this in a more "loosely coupled" way?
  2. The ParserTypeOne job should be just extracting the data from the HTML with nokogiri. Is it too closely coupled to PersistToDB? How cann I call PersistToDB differently?
  3. Any other suggestions?

I know this would work fine I'm just interested in a few ideas on how to improve this. Suggestions?

Polymorphic map keys in C++

I am working on a computer vision API where I have various algorithms that all work on image data and attempt to classify some objects.

Some of the algorithms are simple which say classify objects simply based on their size ie. the algorithm might return the count of all objects based on their size like small, medium, big, for example

Some other algorithms are more fine tuned and may classify objects based on their true type for example like table, chair, bed.

So I have an interface for all such counting objects as:

class IFurnitureCounter
{
    virtual std::map<ObjectType, size_t> getCount() = 0;
};

The idea is that each of these algorithms will implement this getCount() method so that the caller can use them agnostically.

My issue is that the ObjectType key becomes a mish mash. So, I can define this ObjectType as an enum for example:

enum class ObjectType {SMALL, MEDIUM, BIG, TABLE, CHAIR, BED} 

However this feels bad as each algorithm now sees all the types even though a particular algorithm might only work on a subset of them. I was wondering if there is a better deisgn paradigm to have a better decoupling of these types between algorithms.

{REGEX} Username

I'm trying to create a regex pattern to validate the following username(s) and nothing else:

Usernames must be between 1 and 12 characters long.
Usernames may only contain alphanumeric characters, the space, and hyphens (-). Any other characters are replaced with a space.
Usernames cannot start with or end with a space, but can have any number of spaces between.
Given the above parameters, there are 2,084,454,322,680,818,585,575,401,523,200 possible usernames.

I don't know where to start but I'd love to receive some hints or even better a working pattern.

Thank you in advance.