Random sampling of GitHub repositories using multiple methods
Project description
RepoRoulette 🎲: Randomly Sample Repositories from GitHub
Spin the wheel and see which GitHub repositories you get!
🚀 Installation
# Using pip
pip install reporoulette
# From source
git clone https://github.com/gojiplus/reporoulette.git
cd reporoulette
pip install -e .
📖 Sampling Methods
RepoRoulette provides three distinct methods for random GitHub repository sampling:
1. 🎯 ID-Based Sampling
Uses GitHub's sequential repository ID system to generate truly random samples by probing random IDs from the valid ID range. The downside of using the method is that the hit rate can be low (as many IDs are invalid, partly because the repo. is private or abandoned, etc.) And any filtering on repo. characteristics must wait till you have the names.
The function will continue to sample till either max_attempts or till n_samples. You can pass the seed for reproducibility.
from reporoulette import IDSampler
# Initialize the sampler
sampler = IDSampler(token="your_github_token")
# Get 50 random repositories
repos = sampler.sample(n_samples=50)
# Print basic stats
print(f"Success rate: {sampler.success_rate:.2f}%")
print(f"Samples collected: {len(repos)}")
2. ⏱️ Temporal Sampling
Randomly selects days within a specified date range and retrieves repositories updated during those periods using weighted sampling based on repository activity.
from reporoulette import TemporalSampler
from datetime import datetime, timedelta
# Define a date range (last 3 months)
end_date = datetime.now()
start_date = end_date - timedelta(days=90)
# Initialize the sampler
sampler = TemporalSampler(
token="your_github_token",
start_date=start_date,
end_date=end_date
)
# Get 100 random repositories
repos = sampler.sample(n_samples=100)
# Get repositories with specific characteristics
filtered_repos = sampler.sample(
n_samples=50,
min_stars=10,
language="python" # Note: single language, not list
)
3. 🔍 BigQuery Sampling
The BigQuerySampler leverages Google BigQuery's public GitHub dataset to sample repositories with advanced filtering capabilities.
Setup for BigQuery Sampler
-
Create a Google Cloud Platform (GCP) project:
- Go to the GCP Console
- Create a new project
-
Enable the BigQuery API:
- In your project, go to "APIs & Services" > "Library"
- Search for "BigQuery API" and enable it
-
Create a service account:
- Go to "IAM & Admin" > "Service Accounts"
- Create a new service account
- Grant it the "BigQuery User" role
- Create and download a JSON key file
-
Install required dependencies:
pip install google-cloud-bigquery google-auth
-
Using BigQuerySampler:
from reporoulette import BigQuerySampler
# Initialize with service account credentials
sampler = BigQuerySampler(
credentials_path="path/to/your-service-account-key.json",
project_id="your-gcp-project-id",
seed=42
)
# Sample active repositories with commits in the last year
active_repos = sampler.sample(
n_samples=50,
population="active",
languages=["Python", "JavaScript"] # Optional language filter
)
# Sample repositories across random days
random_repos = sampler.sample_by_day(
n_samples=50,
days_to_sample=10,
years_back=5
)
# Get language information for sampled repositories
languages = sampler.get_languages(random_repos)
# Print results
for repo in random_repos:
print(f"Repository: {repo['full_name']}")
repo_languages = languages.get(repo['full_name'], [])
if repo_languages:
print(f"Primary language: {repo_languages[0]['language']}")
print("---")
Advantages:
- Handles large sample sizes efficiently
- Powerful filtering and stratification options
- Not limited by GitHub API rate limits
- Access to historical data
Limitations:
- Could be expensive
- Requires Google Cloud Platform account and billing
- Dataset may have a slight delay (typically 24-48 hours)
4. GH Archive Sampler
The GHArchiveSampler fetches repositories by sampling events from GitHub Archive, a project that records the public GitHub timeline.
from reporoulette import GHArchiveSampler
# Initialize with optional parameters
sampler = GHArchiveSampler(seed=42) # Set seed for reproducibility
# Sample repositories
repos = sampler.sample(
n_samples=100, # Number of repositories to sample
days_to_sample=5, # Number of random days to sample from
repos_per_day=20, # Repositories to sample per day
years_back=2, # How many years to look back
event_types=["PushEvent", "CreateEvent", "PullRequestEvent"] # Event types to consider
)
# Access results
for repo in repos:
print(f"Repository: {repo['full_name']}")
print(f"Event Type: {repo['event_type']}")
print(f"Sampled From: {repo['sampled_from']}")
print("---")
📊 Example Use Cases
- Academic Research: Study coding practices across different languages and communities
- Learning Resources: Discover diverse code examples for education
- Data Science: Build datasets for machine learning models about code patterns
- Trend Analysis: Identify emerging technologies and practices
- Security Research: Find vulnerability patterns across repository types
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
📜 License
This project is licensed under the MIT License - see the LICENSE file for details.
🔗 Related Projects
- GHTorrent - GitHub data archive project
- GitHub Archive - Archive of public GitHub timeline
- PyGithub - Python library for the GitHub API
Built with ❤️ by Gojiplus
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file reporoulette-0.5.0.tar.gz.
File metadata
- Download URL: reporoulette-0.5.0.tar.gz
- Upload date:
- Size: 23.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2db47070d47a64d82108143e1e3e62bb6cede6ce47ae317b20334b20317e0067
|
|
| MD5 |
bc4078ea3f9c97e84fe8864890cbfacf
|
|
| BLAKE2b-256 |
afd626d07084c7995caf4fd7e0ca7447410729f0ee2545afde089648d2141c75
|
Provenance
The following attestation bundles were made for reporoulette-0.5.0.tar.gz:
Publisher:
python-publish.yml on gojiplus/reporoulette
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
reporoulette-0.5.0.tar.gz -
Subject digest:
2db47070d47a64d82108143e1e3e62bb6cede6ce47ae317b20334b20317e0067 - Sigstore transparency entry: 1341604987
- Sigstore integration time:
-
Permalink:
gojiplus/reporoulette@f02191f8e9138c99f590c6b2e89a636f66ef47f1 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/gojiplus
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@f02191f8e9138c99f590c6b2e89a636f66ef47f1 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file reporoulette-0.5.0-py3-none-any.whl.
File metadata
- Download URL: reporoulette-0.5.0-py3-none-any.whl
- Upload date:
- Size: 28.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4fe6c4dc9c052f92d874695a1f8e206f151f219008d1261ecc3323bdf4bcf9c5
|
|
| MD5 |
69c5efc5d4dcd48a7203ab1a588e9442
|
|
| BLAKE2b-256 |
45a9cf64a9147b8afe73eac97c4baf7ea15f9daa718dae63760916e6d93c5d72
|
Provenance
The following attestation bundles were made for reporoulette-0.5.0-py3-none-any.whl:
Publisher:
python-publish.yml on gojiplus/reporoulette
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
reporoulette-0.5.0-py3-none-any.whl -
Subject digest:
4fe6c4dc9c052f92d874695a1f8e206f151f219008d1261ecc3323bdf4bcf9c5 - Sigstore transparency entry: 1341604990
- Sigstore integration time:
-
Permalink:
gojiplus/reporoulette@f02191f8e9138c99f590c6b2e89a636f66ef47f1 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/gojiplus
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@f02191f8e9138c99f590c6b2e89a636f66ef47f1 -
Trigger Event:
workflow_dispatch
-
Statement type: