samedi 21 novembre 2020

Algorithm to get suggestions from items related to another

I would like to get some tips to create an efficient algorithm.

This is the scenario. I'm using rails and sqlite.

There're articles with many custom properties (or rows), in a one-to-many relation. Some properties are the same and are in common among articles and often many articles have three or more equal properties.

When creating a new article and inserting the first property, I would the app to suggest the most frequent properties existing in articles related to the first property in the other articles. So, after adding the second property, the app should suggest the most frequent properties existing in articles that contain both the two properties.

Any idea? Some libs already fitting it?

vendredi 20 novembre 2020

How can create a reyclerview like that?

Hi Im a newbie on Android studio and java , I have a bunch of images that I must show to my user on log and I wanted to use a recyclerview to show image with a good UI . I found this one on Driblle that I really love and I would like to implement it on my app . How can I do that ? Here a link of what I want to achieve

To Many Parameters Scala

I have an application that has a single EntryPoint, its a library to automate some data engineers stuffs.

case class DeltaContextConfig(
  primaryKey: List[String],
  columnToOrder: String,
  filesCountFirstBatch: Int,
  destinationPath: String,
  sparkDf: DataFrame,
  sparkContext: SparkSession,
  operationType: String,
  partitionColumn: Option[String] = None,
  tableName: String,
  databaseName: String,
  autoCompaction: Option[Boolean] = Option(true),
  idealFileSize: Option[Int] = Option(128),
  deduplicationColumn: Option[String] = None,
  compactionIntervalTime: Option[Int] = Option(180),
  updateCondition: Option[String] = None,
  setExpression: Option[String] = None
)

This is my case class, my single Entrypoint.

After that all these parameters are pass to other objects, I have objects to write in Datalake, to Compact files and so on. And these objects use some of these parameters, for example, I have a DeltaWriterConfig object:

DeltaWriterConfig(
  sparkDf = deltaContextConfig.sparkDf,
  columnToOrder = deltaContextConfig.columnToOrder,
  destinationPath = deltaContextConfig.destinationPath,
  primaryKey = deltaContextConfig.primaryKey,
  filesCountFirstBatch = deltaContextConfig.filesCountFirstBatch,
  sparkContext = deltaContextConfig.sparkContext,
  operationType = deltaContextConfig.operationType,
  partitionColumn = deltaContextConfig.partitionColumn,
  updateCondition = deltaContextConfig.updateCondition,
  setExpression = deltaContextConfig.setExpression
)

I use the DeltaWriterConfig, to pass these parameters to my class DeltaWriter. I was creating all these configs objects on the MAIN, but I think it is not good, because, I have 3 Config Objects to populate, so I have 3 big constructors on the application main.

Is there any pattern to solve this?

thank you

Update- request user input for application [closed]

I am new to coding and I have created a main method. Im looking for the system to ask the users for input which greeting card do they want. My code works but it runs one after the other. The question would be"Which greeting card do you want?" and they get to pick which one.

I have worked the code so and it shows all of the cards at once. Im trying to get it to be specific to the users input and only show that output. e.g I want a bday card and then show the bday card. Not the other ones

I'd appreciate your help thanks so much.

public class GreetingCardApp {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        //main method
        //declare and create an instance of wedding card using the construction design pattern

        GreetingCard cardWedding = new wedding();
        cardWedding.completeCard();
       
        GreetingCard cardXmasCard = new XmasCard();
        cardXmasCard.completeCard();

    }

}

Does anyone know a repo where I can find implementations of design patterns in real projects (not for educational purposes)?

I'm mostly interested in Python implementations and mainly in the template method pattern. But any language or pattern would be great!

Is it possible to have multiple subjects in Observer pattern?

I've been working on the Observer pattern, with a one-to-many event. In this case, are multiple classes notified, i.e. more than one observer from one subject? Conversely, could there be more than one subject that I want to ask? Does this situation exceed the Observer pattern and create a different pattern?

Imagine a publishing house that prints newspapers, magazines and books. Users can subscribe to whatever they want. For example, a user might subscribe only to books or to books and newspapers at the same time. Each time a new one of these types is released, the relevant users should be notified. Users who are not registered should not be notified. In other words, if the user is only registered for books, a notification will not be sent for newspapers.

In this example, is the subject the publishing house? Or would books, newspapers, and magazines be separate subjects?

Example of design pattern Factory

This is the code I have, is this a factory DP and what does it do i have trouble understanding it

def chain_factories(field_factories, model_name): """ Instanciate a generator by calling all the field factories. """ generator = root_factory() for (fname, field_factory) in field_factories: generator = field_factory(generator, fname, model_name) return generator

def root_factory(): """ Return a generator with empty values dictionaries (except for the flag __complete). """ yield {'__complete': False} while True: yield {'__complete': True}

def randomize(vals, weights=None, seed=False, formatter=format_str, counter_offset=0): """ Return a factory for an iterator of values dicts with pseudo-randomly chosen values (among vals) for a field.

:param list vals: list in which a value will be chosen, depending on `weights`
:param list weights: list of probabilistic weights
:param seed: optional initialization of the random number generator
:param function formatter: (val, counter, values) --> formatted_value
:param int counter_offset:
:returns: function of the form (iterator, field_name, model_name) -> values
:rtype: function (iterator, str, str) -> dict
"""
def generate(iterator, field_name, model_name):
    r = Random('%s+field+%s' % (model_name, seed or field_name))
    for counter, values in enumerate(iterator):
        val = r.choices(vals, weights)[0]
        values[field_name] = formatter(val, counter + counter_offset, values)
        yield values
return generate

def cartesian(vals, weights=None, seed=False, formatter=format_str, then=None): """ Return a factory for an iterator of values dicts that combines all vals for the field with the other field values in input.

:param list vals: list in which a value will be chosen, depending on `weights`
:param list weights: list of probabilistic weights
:param seed: optional initialization of the random number generator
:param function formatter: (val, counter, values) --> formatted_value
:param function then: if defined, factory used when vals has been consumed.
:returns: function of the form (iterator, field_name, model_name) -> values
:rtype: function (iterator, str, str) -> dict
"""
def generate(iterator, field_name, model_name):
    counter = 0
    for values in iterator:
        if values['__complete']:
            break  # will consume and lose an element, (complete so a filling element). If it is a problem, use peekable instead.
        for val in vals:
            yield {**values, field_name: formatter(val, counter, values)}
        counter += 1
    factory = then or randomize(vals, weights, seed, formatter, counter)
    yield from factory(iterator, field_name, model_name)
return generate

def iterate(vals, weights=None, seed=False, formatter=format_str, then=None): """ Return a factory for an iterator of values dicts that picks a value among vals for each input. Once all vals have been used once, resume as then or as a randomize generator.

:param list vals: list in which a value will be chosen, depending on `weights`
:param list weights: list of probabilistic weights
:param seed: optional initialization of the random number generator
:param function formatter: (val, counter, values) --> formatted_value
:param function then: if defined, factory used when vals has been consumed.
:returns: function of the form (iterator, field_name, model_name) -> values
:rtype: function (iterator, str, str) -> dict
"""
def generate(iterator, field_name, model_name):
    counter = 0
    for val in vals: # iteratable order is important, shortest first
        values = next(iterator)
        values[field_name] = formatter(val, counter, values)
        values['__complete'] = False
        yield values
        counter += 1
    factory = then or randomize(vals, weights, seed, formatter, counter)
    yield from factory(iterator, field_name, model_name)
return generate

def constant(val, formatter=format_str): """ Return a factory for an iterator of values dicts that sets the field to the given value in each input dict.

:returns: function of the form (iterator, field_name, model_name) -> values
:rtype: function (iterator, str, str) -> dict
"""
def generate(iterator, field_name, _):
    for counter, values in enumerate(iterator):
        values[field_name] = formatter(val, counter, values)
        yield values
return generate

def compute(function, seed=None): """ Return a factory for an iterator of values dicts that computes the field value as function(values, counter, random), where values is the other field values, counter is an integer, and random is a pseudo-random number generator.

:param function function: (values, counter, random) --> field_values
:param seed: optional initialization of the random number generator
:returns: function of the form (iterator, field_name, model_name) -> values
:rtype: function (iterator, str, str) -> dict
"""
def generate(iterator, field_name, model_name):
    r = Random('%s+field+%s' % (model_name, seed or field_name))
    for counter, values in enumerate(iterator):
        val = function(values=values, counter=counter, random=r)
        values[field_name] = val
        yield values
return generate

def randint(a, b, seed=None): """ Return a factory for an iterator of values dicts that sets the field to the random integer between a and b included in each input dict.

:param int a: minimal random value
:param int b: maximal random value
:returns: function of the form (iterator, field_name, model_name) -> values
:rtype: function (iterator, str, str) -> dict
"""
def get_rand_int(random=None, **kwargs):
    return random.randint(a, b)
return compute(get_rand_int, seed=seed)

return []