Skip to main content

A mix and match (swap) library to empower swapping-based projects.

Project description

swap-anything

A mix and match (swap) library to empower swapping-based projects.

Docs Tests codecov PyPI version

NOTE: swapanything is still in its early steps. If you want to contribute or sponsor this project, visit www.founderswap.xyz

Quickstart

Want to develop with us? Check the developer guide

Your first matching round

This library allow you to match subjects (people, things, whatever) depending on their availability slots (calendar slots, timeframe, location, any combination of the abovementioned). Truly, you can use this library as backend for any sort of matching need.

The simplest way to test this library is to use the swapanything python package to make a simple swapping exercise.

from swapanything import prep, select
import pandas as pd

availabilities = pd.DataFrame(
   [
      ["KungFury", "9:00"],
      ["KungFury", "10:00"],
      ["KungFury", "13:00"],
      ["KungFury", "14:00"],
      ["Triceracop", "9:00"],
      ["Triceracop", "11:00"],
      ["Hackerman", "10:00"],
      ["Hackerman", "11:00"],
      ["Katana", "12:00"],
      ["Barbarianna", "12:00"],
      ["Thor", "13:00"],
      ["Thor", "14:00"],
      ["Thor", "15:00"],
      ["T-Rex", "15:00"],
      ["T-Rex", "16:00"],
      ["Hoff 9000", "16:00"],
   ],
   columns=["subject", "availability"]
)

all_possible_matches = prep.get_all_matches(
   availabilities,
   subject_col="subject",
   slot_col="availability",
   subjects_new_col_name="subjects",
   slots_new_col_name="availabilities",
)
#                    subject    availability
# 0    (Barbarianna, Katana)        (12:00,)
# 1    (Hackerman, KungFury)        (10:00,)
# 2  (Hackerman, Triceracop)        (11:00,)
# 3       (Hoff 9000, T-Rex)        (16:00,)
# 4         (KungFury, Thor)  (13:00, 14:00)
# 5   (KungFury, Triceracop)         (9:00,)
# 6            (T-Rex, Thor)        (15:00,)

select.select_matches(
   all_possible_matches,
   subjects_col="subjects",
   slots_col="availabilities",
)
#                   subjects  availabilities
# 0    (Barbarianna, Katana)        (12:00,)
# 1  (Hackerman, Triceracop)        (11:00,)
# 2       (Hoff 9000, T-Rex)        (16:00,)
# 3         (KungFury, Thor)  (13:00, 14:00)

Imagine now that we want to provide a super high importance to the match (KungFury, Triceracop). With select_matches you can use match scores, and the algorithm will try to maximize number of matches and total score!

This way we ensure that high quality matches are selected.

scores = [1, 1, 1, 1, 1, 9001, 1]
# (KungFury, Triceracop)... it's over 9000!
select.select_matches(
   all_possible_matches,
   match_scores=scores,
   subjects_col="subjects",
   slots_col="availabilities",
)
#                   subject availability
# 0   (Barbarianna, Katana)     (12:00,)
# 1  (KungFury, Triceracop)      (9:00,)
# 2           (T-Rex, Thor)     (15:00,)

Advanced Backends

With python, it is possible to integrate swapanything in your application or custom tool. swapanything comes with some pre-configured data backends (e.g. Airtable, Excel Spreadsheets, SQL) that you can easily use to kickstart your swaping-based app!

Airtable

Install airtable dependencies:

pip install swap-anything[airtable]
from swapanything import prep, select
from swapanything_backend import airtable as backend
import os


be = backend.AirTableBackend(
    # subject_id is the record id of the subjects table
    subject_features=["Interests", "Tags", "Score1", "Score2"],
    availability_subject_column="AvailabilitiesSubjectId",
    availabilities_column="Availabilities",
    exclusions_subject_columns=["Subject1", "Subject2"]
    # Tables
    subjects_table_name="Subjects",
    availabilities_table_name="Availabilities",
    exclusions_table_name="Matches",
    # Airtable credentials
    client_id=os.environ["AIRTABLE_BASE_ID"],
    client_secret=os.environ["AIRTABLE_API_KEY"],
)

subjects = be.get_subjects()
availabilities = be.get_availabilities()

all_possible_matches = prep.get_all_matches(
   availabilities,
   subject_col=be.availability_subject_column,
   slot_col=be.availabilities_column,
   subjects_new_col_name="subjects",
   slots_new_col_name="availabilities",
)
select.select_matches(
   all_possible_matches,
   subjects_col="subjects",
   slots_col="availabilities",
)

Using CLI (POC)

This part is in proof of concept stage. Yet to be done!

You can start swapping using spreadsheets as sources/destinations of data. Let's prepare 3 files:

  1. subjects.xlsx - the table of subjects to match with details on features to use to calculate the score for a match. Made as follow:

    SubjectId Interests Tags Score1 Score2
    sub001 i1,i2 t1,t2 0.2 0.5
    sub002 i1 t2 0.2 0.1
    sub003 i3 t1 0.15 0.2
    sub004 i1,i2 t1 0.2 0.5
  2. availabilities.xlsx - a table containing match slots. Subjects can match when they have one or more slots in common. Made as follow:

    AvailabilitiesSubjectId Availabilities
    sub001 2023-01-01 15:30, 2023-01-02 16:30
    sub002 2023-01-01 15:30
    sub003 2023-01-01 15:30
    sub004 2023-01-02 16:30
  3. exclusions.xlsx - a table containing matches to exclude (e.g. subjects that have already matched). Made as follow:

    Subject1 Subject2
    sub001 sub004

Then you can use the command line tool to make the swapping ✨

swapanything --from spreadsheet \
    --subject-id SubjectId \
    --subject-features Interests,Tags,Score1,Score2 \
    --availabilities-subject-col AvailabilitiesSubjectId \
    --availabilities-column Availabilities \
    --exclusions-subject1-id Subject1 \
    --exclusions-subject2-id Subject2 \
    --subjects subjects.xlsx \
    --availabilities availabilities.xlsx \
    --exclusions exclusions.xlsx \
    --to spreadsheet output.xlsx

This will result in the following output.xlsx, containing all new matches:

subject1 subject2 slot
sub001 sub002 2023-01-01 15:30

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

swap-anything-0.2.0.tar.gz (64.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

swap_anything-0.2.0-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file swap-anything-0.2.0.tar.gz.

File metadata

  • Download URL: swap-anything-0.2.0.tar.gz
  • Upload date:
  • Size: 64.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for swap-anything-0.2.0.tar.gz
Algorithm Hash digest
SHA256 c61afcbf93f617ad391bb2b0c6b35a5b1a980277d688b7f458a30a269caef6da
MD5 0b585a1d65baf2bb0c1c74f097c07487
BLAKE2b-256 262a754afa34a171adf1ac92c88824096a9f76e506bf2eb3f3a63f24ecc0c24a

See more details on using hashes here.

File details

Details for the file swap_anything-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: swap_anything-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for swap_anything-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 31b5478553b36851c836c9b654fd537e286aa4af60197a06da4a1f22134ba88b
MD5 a34d7dbbb563d4eeeeef63b8ce51596e
BLAKE2b-256 a18a0ffb7babb5e68de8cb2468e15e7b455d73427963524bed6fe70789b60452

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page