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 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]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 Layer | Minimum | Production recommendation | Purpose in stack |
|---|---|---|---|
| GitHub account | Signed-in browser session | Account that can open public repos | Read python-factorial and enable alerts on your project |
| Example repo | new2code/python-factorial | Leave it public; do not force-push teaching forks | Source of factorial_finder.py and MIT license |
| Editor + Copilot | VS Code (or github.dev) with Copilot Chat | Same prompts GitHub prints | Explain, wrap, and library-discovery chats |
| Python | 3.x with stdlib math | Any current CPython | Run the wrapped function and math.factorial |
| Your project repo | A repo you administer | Public open-source friendly for free Dependabot alerts | Paste 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
-
Open and understand
factorial_finder.py. Onnew2code/python-factorial, openfactorial_finder.py. The teaching loop initializesfactorial = 1, setsnumber = 6, multiplies throughrange(1, number + 1), then prints the result. In the file menu bar, start a Copilot conversation and ask:Explain this program. -
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.pyfile, 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.
-
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:
Wrap the Python code above in a function.Copilot should produce something like:
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 factorialAdd calls and run the program:
print(calculate_factorial(5))print(calculate_factorial(7))print(calculate_factorial(9))print(calculate_factorial(10))
B. Prefer a library, then harden the project
-
Ask Copilot for a library, then check popularity. Open Copilot Chat:
Is there a Python library with a function for calculating a factorial?Copilot should point at the
mathmodule in the Python standard library. Adding a library creates a dependency. Prefer popular, actively maintained code. Ask: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. -
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.
-
Replace the loop with
math.factorial. Ask Copilot:How do I use the factorial function of the math module in my Python project?Replace your implementation with the suggested shape, for example:
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?
| Check | Expected signal | If it fails |
|---|---|---|
| Understanding | Copilot (or your own reading) explains the loop in factorial_finder.py | Re-open the file and re-run the explain prompt |
| License | About shows MIT; license comment sits above your pasted code | Click MIT license in About; paste the notice before code |
| Wrapped snippet | calculate_factorial(5/7/9/10) prints sensible integers | Re-ask Copilot to wrap the function; run the file locally |
| Popularity gate | You can name python/cpython / stdlib math as the source | Re-ask Copilot for the math module repository |
| Dependabot | Security and quality shows Dependabot alerts enabled | Click Enable Dependabot alerts again on that tab |
| Library swap | Program uses import math and math.factorial | Replace 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 tomath.factorialfor this example. - Dependabot never enabled: Cause: treating reuse as editor-only. Fix: Security and quality → Enable Dependabot alerts on a repo you administer.
- Inventing API/
ghlicense 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.