GitHub Copilot - Patterns & Exercises
GitHub 🌟
en 🇬🇧
en 🇬🇧
  • Introduction
  • Contributing to the Project
  • General
    • Code completion
    • Comment to code
    • Code to comment
    • Quick Q&A
    • Regular expression
    • Language translation
    • Type hinting
    • Code to document
    • Object generation from structured data
    • Showing examples
  • Client Side Tips
    • Copilot snnipet handling
    • GitHub Copilot Shortcuts
    • Go to definition
    • Pin the files you need
  • Design Patterns
    • AI readable naming convention
    • Consistent coding style
    • High-level architecture first
    • Working on small chunks
    • Context-less Architecture
    • Eliminating a tiny OSS dependency
  • Collaboration
    • AI friendly documentation
    • Coaching on prompts
  • Test
    • Creating unit tests
    • Specify how to generate test code
    • Writing failure case first
    • Writing test cases in natural language first
    • Test only what is necessary
  • Refactoring
    • Writing test code before refactoring
    • Making the calculation part independent
    • Asking with open-ended questions
  • Archived
    • GitHub Copilot Patterns & Exercises Guide
    • Translations
      • German 🇩🇪
      • Spanish 🇪🇸
      • French 🇫🇷
      • Italy 🇮🇹
      • Japanese 🇯🇵
      • Portuguese 🇵🇹
      • Chinese 🇨🇳
Powered by GitBook
On this page
  • Description
  • Example
  • Exercise
  • Checklist for Further Learning
Edit on GitHub
  1. General

Object generation from structured data

Generating objects from structured data such as JSON.

Last updated 1 year ago

Description

Working with structured data is an everyday task for developers. Transforming data from formats like JSON into objects within your programming language allows for more robust and maintainable code. Imagine you have a list of users, and you want to convert this data into user objects within your application. GitHub Copilot can help you in this transformation process, turning a tedious task into a seamless exercise.

Example

Here's a Python example of how you might convert the given JSON data into a list of user objects:

import json

json_data = '[{"id": "1", "name": "Yuki Hattori"}, {"id": "2", "name": "George Hattori"}]'
users = json.loads(json_data)

class User:
    def __init__(self, id, name):
        self.id = id
        self.name = name

user_objects = [User(user['id'], user['name']) for user in users]

for user in user_objects:
    print(user.id, user.name)

Exercise

  • Exercise 1: Try generating objects from a different JSON structure, e.g., a JSON that includes address information for the users.

  • Exercise 2: Experiment with handling edge cases, such as missing data within the JSON, and ensure that your code handles them gracefully.

Checklist for Further Learning

  • How would you modify the code to accommodate a more complex data structure?

  • What methods could you use to validate the data before transforming it into objects?

  • How can this pattern be adapted to different programming languages or frameworks?