Exploring the Python Packages File System

Introduction

The first thing we learned on this channel was how to create a personal lineup optimizer from scratch. Since then, we have evolved into using a pre-built python package called pydfs-lineup-optimizer that Dima Kudosh put together and made available for hobbyists like ourselves to make use of.

[https://github.com/DimaKudosh/pydfs-lineup-optimizer][0]

There are certainly Pros and Cons to creating your own custom optimizer and using a preconfigured option, mainly boiling down to customization and the limits of your own python knowledge. The natural progression of this skill set is to learn how to manually alter the pydfs-lineup-optimizer to fit our own needs. By learning how to navigate this file system, we can enjoy the best of both worlds. Having the optimizer infrastructure built out for us, while still allowing for some customization.

The example we will be working with today will be adding a new sport into the optimizer that is not currently an option. You will be able to apply the concepts we go over today if you need to add a new site, sport, or speific lineup rules to an existing sport.

Finding the File System

The first thing we will need to do is to identify where the pydfs-lineup-optimizer package is saved on our local machines. This will be dependent upon where python was initially installed at, as the python distribution file system will likely be pretty similar once we can find that location.

In order to find this location, we can simply attempt to pip install a package that is already installed, it will inform us that the requirements are already met, and let us know where it occurs at.

pip install pydfs-lineup-optimizer

Requirement already satisfied: pydfs-lineup-optimizer in c:\users\nfwya\anaconda3\lib\site-packages (3.4.0)
Requirement already satisfied: PuLP==2.4 in c:\users\nfwya\anaconda3\lib\site-packages (from pydfs-lineup-optimizer) (2.4)
Requirement already satisfied: pytz>=2020.5 in c:\users\nfwya\anaconda3\lib\site-packages (from pydfs-lineup-optimizer) (2021.1)
Requirement already satisfied: amply>=0.1.2 in c:\users\nfwya\anaconda3\lib\site-packages (from PuLP==2.4->pydfs-lineup-optimizer) (0.1.4)
Requirement already satisfied: pyparsing in c:\users\nfwya\anaconda3\lib\site-packages (from amply>=0.1.2->PuLP==2.4->pydfs-lineup-optimizer) (2.4.7)
Requirement already satisfied: docutils>=0.3 in c:\users\nfwya\anaconda3\lib\site-packages (from amply>=0.1.2->PuLP==2.4->pydfs-lineup-optimizer) (0.16)
Note: you may need to restart the kernel to use updated packages.

As we can see, my package is installed at this location:

c:\users\nfwya\anaconda3\lib\site-packages

Most of the time, you will need to be able to remember the file path up until 'anaconda3' in this example, as the ..\lib\site-packages should be pretty constant. If we open this file location we will find all of the python packages installed on our machine

filenav1.png

Exploring the pydfs-lineup-optimizer Directory

Now, we simply need to scroll down to where the pydfs_lineup_optimizer directory is located and open it up.

Now, just as some background information for the folks without a python background, most formal packages are put together in a similar organizational structure. The init.py file is what will kick everything off, and the other .py files will be brought in via an import statement. If you open up the init.py file in a text editor or IDE, then you will be able to see all of the import statements referencing these files.

It may surprise you since we have been working in jupyter notebooks mostly, which i think is better for learning purposes since everything is broken into individual cells for testing and such, but this is the entirety of the init.py file above. Most packages are written in such a way so if there is an error, you can pinpoint exactly which file it is occurring in, and where in that file rather than trying to search through one massive .py file and trying to navigate all the different for loops. Additionally, for code that gets reused multiple times, this allows for you to alter it in a single place, and have that update get carried through to every instance it is used in.

Next if we open up the Sites directories we will find directories for each site built into the optimizer, as well as the necessary rules for the game types for each site.

Conclusion

Now that we know how to find the package on our local machine, I encourage you to poke around a bit and get a feel for what's what and how the whole thing works. You can simply work your way backwards from the init.py file and figure out what functions get called and it what order. Next time we will review how to actually make changes to these files to incorporate a new sport!

P.S.

If you accidentally make any changes and break the package and can't figure out how to fix it, simply uninstall and reinstall the package. That will give you a clean install of the package that will erase any changes you had made.

pip uninstall pydfs-lineup-optimizer

pip install pydfs-lineup-optimizer

Happy Tinkering!

Previous
Previous

Adding a New Sport to pydfs_lineup_optimizer

Next
Next

Clarification Around Locking Players into Lineups