Code Monkey home page Code Monkey logo

Comments (4)

kurtwheeler avatar kurtwheeler commented on August 17, 2024

@mhuyck I've come up with the following function to do handle this issue. I wasn't able to add it as part of the __init__ function like I had originally thought I might because the DownloaderJob has to be saved before it can be saved as a foreign key to the DownloaderJobsToBatches table. Do you think this is about the best that can be done or do you know of any helpful tricks. (Maybe @rzelayafavila or @dongbohu might also have good input?)

    @classmethod
    def create_job_and_relationships(*args, **kwargs):
        """Inits and saves a job, its batches, and the relationships.

        Creates and saves a single DownloaderJob. For each batch
        passed in via the 'batches' keyword, saves that batch, creates
        a DownloaderJobsToBatches record, and saves that record.
        """
        batches = kwargs.pop('batches', None)
        this_job = super().__init__(*args, **kwargs)
        if batches is None:
            raise KeyError("The 'batches' argument must be specified.")
        else:
            this_job.save()
            for batch in batches:
                batch.save()
                downloader_job_to_batch = DownloaderJobsToBatches(batch=batch,
                                                                  downloader_job=this_job)
                downloader_job_to_batch.save()

      return this_job

from refinebio.

mhuyck avatar mhuyck commented on August 17, 2024

👍

I don't know any special tricks for making __init__ do what we thought. It makes sense that you'd need to have the .save done first so there's a bit of an initialization chicken and egg issue there.

My primary observation during that code review was that a one-line object initialization was getting replaced with a template-like block of code in a bunch of places, so if this helper function can streamline that you'll end up with more DRY code and all the bounty that comes with that.

from refinebio.

kurtwheeler avatar kurtwheeler commented on August 17, 2024

Yup I definitely feel that for sure.

from refinebio.

kurtwheeler avatar kurtwheeler commented on August 17, 2024

Addressed by #26

from refinebio.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.