Back to Resources

Reuse other people's code in your projects

Check the MIT license on a source repo, paste and wrap code with proper license attribution, import Python stdlib math, and enable Dependabot alerts.

What are we building and why?

We are following GitHub's reuse tutorial: understand a public Python factorial example, respect its MIT license, paste and wrap the snippet, then replace the hand-rolled loop with the standard library math module while enabling Dependabot alerts on a project you own.

GitHub's Reusing other people's code in your projects page names two styles. Beginners often copy a snippet; developers usually import a library. The worked example is new2code/python-factorial: open factorial_finder.py, ask Copilot to explain it, read MIT license from the About section, paste the file with the license as a top-of-file comment, wrap the loop in a function via Copilot Chat, print factorials for 5/7/9/10, then ask Copilot for a library alternative and switch to math.factorial. Security practice on the same page: prefer popular libraries (stars as a signal) and click Enable Dependabot alerts under Security and quality.

When we walked this UI path at ZeroShot Studio, the About-box license click was the step people skipped most often. After we made "MIT notice in the comment block before any paste" mandatory, silent-copy reviews on teaching repos dropped sharply. Stars on python/cpython (GitHub cites over 64,000) beat inventing a random factorial package. The limitation is honest: Copilot suggests shapes; you still own license compliance and Dependabot enablement.

Flowchart
6 linescompact
flowchart LR
    Source[Examine Source Repo] --> License[Verify MIT License in About]
    License --> Paste[Paste Snippet with License Comment]
    Paste --> Wrap[Wrap Loop in Function]
    Wrap --> Library[Discover Python math Module]
    Library --> Dependabot[Enable Dependabot Alerts]
Rendered from Mermaid source with the native ZeroLabs diagram container.

Related reading: How to Find and Understand Example Code, How to Find and Fix Your First Dependency Vulnerability, and How to Develop Your Project Locally. Authority: Licensing a repository, Choose a License, and Configuring Dependabot alerts.

"Licenses determine how you can use the code in a project, including your ability to copy, modify, and distribute that code."

That line is GitHub's gate. My rule of thumb at ZeroLabs: About box first, notice kept, prefer a well-starred maintained module, then Dependabot alerts on before you call the import "done."

What are the required prerequisites?

This exercise is browser UI plus a Python file you can edit (VS Code with Copilot Chat matches the docs). No GitHub REST or gh api license probe appears on the source page.

Prerequisite LayerMinimumProduction recommendationPurpose in stack
GitHub accountSigned-in browser sessionAccount that can open public reposRead python-factorial and enable alerts on your project
Example reponew2code/python-factorialLeave it public; do not force-push teaching forksSource of factorial_finder.py and MIT license
Editor + CopilotVS Code (or github.dev) with Copilot ChatSame prompts GitHub printsExplain, wrap, and library-discovery chats
Python3.x with stdlib mathAny current CPythonRun the wrapped function and math.factorial
Your project repoA repo you administerPublic open-source friendly for free Dependabot alertsPaste target + Security and quality tab

How do you implement the step-by-step recipe?

Work GitHub's paste path, then the library path, then the Dependabot toggle. Stay on the labeled UI.

A. Paste a snippet with the license intact

  1. Open and understand factorial_finder.py. On new2code/python-factorial, open factorial_finder.py. The teaching loop initializes factorial = 1, sets number = 6, multiplies through range(1, number + 1), then prints the result. In the file menu bar, start a Copilot conversation and ask:

    text
    Explain this program.
  2. Confirm licensing from the About box. On the repository main page, locate About. You should see that the repository is licensed under the MIT license. Click MIT license and read it. Because you are copying the entire factorial_finder.py file, MIT says you should include a copy of the license in your own project. At the top of your Python file, paste the license text as a comment before any code.

    Tip from the docs: use Choose a License to learn what other common licenses allow.

  3. Paste, then wrap the calculator in a function. Paste the snippet into your project under the license comment. To calculate factorials for 5, 7, 9, and 10 without duplicating the whole program, ask Copilot Chat. Paste your current code, then:

    text
    Wrap the Python code above in a function.

    Copilot should produce something like:

    python
    def calculate_factorial(number):    # Initialize the factorial result to 1    factorial = 1    # Loop from 1 to number (inclusive) and multiply factorial by each number    for i in range(1, number + 1):        factorial *= i    return factorial

    Add calls and run the program:

    python
    print(calculate_factorial(5))print(calculate_factorial(7))print(calculate_factorial(9))print(calculate_factorial(10))

B. Prefer a library, then harden the project

  1. Ask Copilot for a library, then check popularity. Open Copilot Chat:

    text
    Is there a Python library with a function for calculating a factorial?

    Copilot should point at the math module in the Python standard library. Adding a library creates a dependency. Prefer popular, actively maintained code. Ask:

    text
    Find the GitHub repository containing the code for the math module in Python.

    Copilot should name python/cpython, which GitHub notes has over 64,000 stars.

  2. Enable Dependabot alerts on your project. On your project's GitHub repository, open the Security and quality tab. Next to Dependabot alerts, click Enable Dependabot alerts. You can later open alerts from the Dependabot item in the sidebar. Dependabot alerts are free on all open source GitHub repositories.

  3. Replace the loop with math.factorial. Ask Copilot:

    text
    How do I use the factorial function of the math module in my Python project?

    Replace your implementation with the suggested shape, for example:

    python
    import math# Calculate the factorial of a numbernumber = 5result = math.factorial(number)print(f"The factorial of {number} is {result}")

    You have now reused both a snippet (with notice) and a library function.

How do you verify the deployment works?

CheckExpected signalIf it fails
UnderstandingCopilot (or your own reading) explains the loop in factorial_finder.pyRe-open the file and re-run the explain prompt
LicenseAbout shows MIT; license comment sits above your pasted codeClick MIT license in About; paste the notice before code
Wrapped snippetcalculate_factorial(5/7/9/10) prints sensible integersRe-ask Copilot to wrap the function; run the file locally
Popularity gateYou can name python/cpython / stdlib math as the sourceRe-ask Copilot for the math module repository
DependabotSecurity and quality shows Dependabot alerts enabledClick Enable Dependabot alerts again on that tab
Library swapProgram uses import math and math.factorialReplace the loop with the Copilot-suggested import usage

What are the common production failure modes?

  • Silent paste with no license comment: Cause: skipping the About box. Fix: open MIT license, paste the notice as a comment, keep a LICENSE copy in the project when you take the whole file.
  • Assuming stars equal permission: Cause: confusing popularity with license rights. Fix: stars help you prefer maintained libraries; the license text decides what you may ship.
  • Leaving a hand-rolled loop after discovering math: Cause: stopping at the wrap step. Fix: complete the library section and switch to math.factorial for this example.
  • Dependabot never enabled: Cause: treating reuse as editor-only. Fix: Security and qualityEnable Dependabot alerts on a repo you administer.
  • Inventing API/gh license probes: Cause: rewriting the About-box step as REST. Fix: stay on the repository About UI the docs show.

FAQ

When should I paste a snippet versus import a library? GitHub calls paste the quickest start for beginners, and libraries the standard, more efficient long-term skill. This tutorial practices both on the same factorial example.

What if the About box shows no license? Do not treat that as free to copy into a product. Default copyright still applies. Pick another example with a clear open-source license, or get explicit permission.

Is Copilot required? The learning page uses Copilot Chat for explain / wrap / library discovery. You can read the file and docs yourself, but keep the same license and Dependabot steps.

Why enable Dependabot for stdlib math? The enable step is the habit GitHub wants whenever dependencies exist. Future third-party packages will surface alerts in the same Dependabot sidebar.

Where do I share what I built? GitHub invites you to share how you repurposed the example in their community discussion, and points to further reading on finding and understanding example code.

Share