1

I'm facing a challenge with maintaining consistency across multiple projects that share a common library. Here's the scenario:

  1. I have a helpers library with a function anonymize_ip(ip).
  2. This function is used in Project A with a specific anonymization algorithm.
  3. I copied the library to Project B, reusing the same function.
  4. Later, I changed the algorithm and expected output of anonymize_ip() for Project B.

Now, the helpers library is out of sync between projects. I want to keep it easily synchronized without breaking existing code. The only solution I've come up with is to create versioned functions:

def anonymize_ip_v1(ip):
    # Original implementation
    pass

def anonymize_ip_v2(ip):
    # New implementation
    pass

Is this the best approach? Are there more elegant solutions for managing function versioning across multiple projects using a shared library? I'm open to suggestions for better code organization, version control strategies, or package management techniques that could help solve this issue.

2 Answers 2

1

If I'm understanding you right, you have 3 different GitHub repositories:

  1. helpers library
  2. Project A
  3. Project B

If this correct, you could use tagging feature in GitHub, so you could push first version of anonymize_ip, and set tag in GitHub for library project e.g. 1.0.0.

After this, you could install this library in your Project A and Project B with specified tag of library (bellow example for poetry):

library = { tag="1.0.0", git = "ssh://[email protected]/your-github/library.git" }

When you need to change some logic in your library you could commit new logic, push it, and build new tag e.g. 1.1.0

After this, you could install new version of your library by switching version in your requirements:

library = { tag="1.1.0", git = "ssh://[email protected]/your-github/library.git" }
2
  • nice idea, but this would only be on a per-library and not per-function basis and would require GitHub, right?
    – Steven O
    Commented Sep 4, 2024 at 11:25
  • If you don't use GitHub, you can achieve this approach by Semantic Versioning. So if you are using setup.py or pyproject.toml, you can change version right it this file: setup(name="library", version="1.0.0", ...). Then you could install library with this version in requirements.txt: library==1.0.0 So if your library grows in a 100+ lines of code, you won’t need to copy and paste it every time across 2 projects. But if you just need to re-use one or couple of functions, your solution with v1 and v2 functions will work well. Commented Sep 5, 2024 at 4:50
0

From what you have described, it seems that you want to come up with a way to not violate The Open Closed Principle (OCP).

This can be achieved by applying the object-oriented approach, in particular The Dependency Inversion Principle (DIP).

enter image description here

With this approach, you can add new IP implementations (into the library) without breaking old code (in the Project A, Project B, etc.), since now all your consumers use the IP interface.

P.S. I tried to outline the essence of The Dependency Inversion Principle (DIP) in my answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.