Pypack – compact packaging and reusable configuration

10-15-2017

In this post I will talk about how to use pypack to program clean and reusable python code. For programming larger complex applications in python, import statements tend to clutter code readability and isn’t practicable to reuse for different projects. Let’s say if you want to code a data science app you have your “go-to” packages like numpy, matplotlib, math etc. or a web crawler like selenium, beautiful soup, and requests with compact packaging and reusable configuration programming is streamlined.

First specify the packages used in your program in a configuration file named ‘config,’ defining imports and statements in key value declaration spaced by one line.

# config file
imports: 'math','json','collections','itertools','numpy','pandas','matplotlib.pyplot',''

statements: '','','','','np','pd','pp'

This will specify a list of imports pypack will pull into the dev environment necessary for your project.

# new.py
# packages from config file

import math
import json
import collections
import itertools
import numpy as np
import pandas as pd
import matplotlib.pyplot as pp

 

The above code snippet is the result of the configuration file contents listed at the beginning of the post.  pypack is a simple program written in python with less than 37 lines of code that reads the specified packages from the config file and writes those packages to a new python file for specialized coding projects.

import sys
# config file should be in same folder as pypack
# if not, specify
f=open('config','r')
s=f.read()

First pypack opens the config file and reads the contents to memory.

# parse config file
s1=s.split('imports:')
s2=''.join(s1)
s3=s2.split('statements:')
s4=''.join(s3)
arr= s4.split(',')

 

Python syntax is such that assigning elements is as simple as encapsulating a loop with brackets. The first four lines of this snippet comma delimit the the config file and assign imports and statement elements to separate arrays.

# list comprehension of imports and statements
arr=[a for a in arr[:7]]
st=arr[-1].split('\n\n')[0]
arr[-1]=st
arr1= s4.split(',')[7:]
arr1.insert(0,' ')

Next imports and statements lines are split, concatenated and then double space delimited to an array for list comprehension.

.py =open(sys.argv[1],'w')
for i in range(len(arr)):
   if arr1[i]==' ':
       .py.write('import '+arr[i]+'\n')
   if arr1[i]!=' ':
       .py.write('import '+arr[i]+' as '+arr1[i]+'\n')
.py.close()

Finally pypack opens a new writable python file and effectively iterates through the two arrays, writing imports and statements to the new python file.

If you like these blog posts or want to comment and or share something do so below and follow py-guy!

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s