<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Andrew Fuller</title>
    <description>The latest articles on Forem by Andrew Fuller (@fullhousee).</description>
    <link>https://forem.com/fullhousee</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2164776%2F5b08f722-01b9-45fd-b7ac-394546406819.png</url>
      <title>Forem: Andrew Fuller</title>
      <link>https://forem.com/fullhousee</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/fullhousee"/>
    <language>en</language>
    <item>
      <title>List Comprehension: ORM</title>
      <dc:creator>Andrew Fuller</dc:creator>
      <pubDate>Fri, 25 Oct 2024 01:58:11 +0000</pubDate>
      <link>https://forem.com/fullhousee/list-comprehension-orm-25ja</link>
      <guid>https://forem.com/fullhousee/list-comprehension-orm-25ja</guid>
      <description>&lt;p&gt;Need a swift way to create lists when mapping object relations? List comprehensions are the way to go! You can easily connect all of our data in a programmatic way and manipulate it as you please. The example code below is for a one-to-many relationship between a Author, Book, and Contract.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Book:

    all = []

    def __init__(self, title) -&amp;gt; None:
        self.title = title
        Book.all.append(self)

    def contracts(self):
        return [contract for contract in Contract.all if contract.book == self]

    def authors(self):
        return [contract.author for contract in self.contracts()]


    pass

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will see repeatedly that list comprehensions are taken advantage of you algorithmically connect all of the objects and create usable lists out of them based simple criteria like: if this instance's attribute book is present or author in a list of objects.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Author:

    all = []

    def __init__(self, name) -&amp;gt; None:
        self.name = name
        Author.all.append(self)

    def contracts(self):
        return [contract for contract in Contract.all if contract.author == self]

    def books(self): 
        return [contract.book for contract in self.contracts()]

    def sign_contract(self, book, date, royalties):
        return Contract(self, book, date, royalties)

    def total_royalties(self):
        return sum([contract.royalties for contract in self.contracts()])

    pass
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again and again list comprehensions can be used simply to make accessible data from other classes in ORM. You can even use them in class methods to add more functionality like checking if a passed in date matches that of a book/author contract.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Contract:

    all = []

    def __init__(self, author, book, date, royalties):
        self.author = author
        self.book = book
        self.date = date
        self.royalties = royalties
        Contract.all.append(self)

    @property
    def author(self):
        return self._author

    @author.setter
    def author(self, value):
        if not isinstance(value, Author):
            raise Exception
        self._author = value

    @property
    def book(self):
        return self._book

    @book.setter
    def book(self, value):
        if not isinstance(value, Book):
            raise Exception
        self._book = value

    @property
    def date(self):
        return self._date

    @date.setter
    def date(self, value):
        if not isinstance(value, str):
            raise Exception
        self._date = value

    @property
    def royalties(self):
        return self._royalties

    @royalties.setter
    def royalties(self, value):
        if not isinstance(value, int):
            raise Exception
        self._royalties = value

    @classmethod
    def contracts_by_date(cls, date):
        return [contract for contract in cls.all if contract.date == date]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Overall, it is a very easy way of iterating through your data as opposed to creating 500 lines of code to associate what could be simple relations.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>React rendering lists with .map()</title>
      <dc:creator>Andrew Fuller</dc:creator>
      <pubDate>Fri, 04 Oct 2024 15:59:26 +0000</pubDate>
      <link>https://forem.com/fullhousee/react-rendering-lists-with-map-2l0n</link>
      <guid>https://forem.com/fullhousee/react-rendering-lists-with-map-2l0n</guid>
      <description>&lt;p&gt;&lt;strong&gt;Intro&lt;/strong&gt;&lt;br&gt;
One of the more confusing but simple concepts I had to wrap my mind around when first working with Javascript and React was programmatically rendering "cards" that usually display some image, button and variable information. You can then make lists of these cards from information stored in an API or a local db.json file. &lt;/p&gt;

&lt;p&gt;This is actually quite simple to do when using a powerful tool like React, once you know how to go about it. By the time I built a list of plant cards for a "Plantsy App", I had it mastered. This list had a search feature and could be added to by a controlled form but I won't go into those features today.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;List of Plant Cards&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq5zrqbi6bjyouxvhkjc4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq5zrqbi6bjyouxvhkjc4.png" alt="The list of plant cards rendered" width="800" height="759"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Plant Card JSX Code&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The plant card component took in a prop "plant" that contained info from a db.json file with id, name, image, and price keys from an array of objects. This info was fetched and then passed down the card component give it all it needed to render the way we wanted. It even had condimental rendering with an in/out of stock button. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg67ezv5hsd5nlbifr2bs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg67ezv5hsd5nlbifr2bs.png" alt="Individual plant card code" width="800" height="683"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mapping the Plant Cards&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now for the part you came for - mapping the plant cards. The plant list component was passed a prop of "plants" which could be changed via filtering or adding to our list with a form. It is important to note that you need to create a key attribute for this mapping to work properly and it is bad practice to use an index variable. Our information came with ids but you could also use the plant (not plants) variable for this.&lt;/p&gt;

&lt;p&gt;Put all of this inside a &lt;code&gt;&amp;lt;ul&amp;gt;&lt;/code&gt; or &lt;code&gt;&amp;lt;ol&amp;gt;&lt;/code&gt; element and viola! All of your cards are easily and programmatically rendered with ease. &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4puvnf5ss0ywz1lerryh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4puvnf5ss0ywz1lerryh.png" alt="How to render the list of plant cards with .map()" width="800" height="930"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;React Lists and Keys&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;React has many guides on it features. I recommend you give there website a look if you are still iffy on the technical aspect of mapping lists. &lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://legacy.reactjs.org/docs/lists-and-keys.html" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flegacy.reactjs.org%2Flogo-og.png" height="420" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://legacy.reactjs.org/docs/lists-and-keys.html" rel="noopener noreferrer" class="c-link"&gt;
          Lists and Keys – React
        &lt;/a&gt;
      &lt;/h2&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flegacy.reactjs.org%2Ffavicon.ico" width="800" height="400"&gt;
        legacy.reactjs.org
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


</description>
      <category>react</category>
      <category>beginners</category>
      <category>mapping</category>
    </item>
  </channel>
</rss>
