-->

Wednesday, July 10, 2019

Python - Creating Module Packages


Intent

Beyond importing someone else's packages, sometimes it helps to make your own. This will assist in the understanding of how to do so.

I. It helps to have the pep8 compliance package to test.

py -3 -m pip install pytest
py -3 -m pip install pytest-pep8

Next step, make sure your module is pip complaint


  1. cd to your module and run…
  2. py.test --pep8 vsearch.py

Common Errors Found

  1. Not having two blank lines between each function
  2. Not having a white space following a colon
  3. Using tabs to indent. It prefers 4     blank   manual    lines

Note the ^ will mark where in the code it believes things are out of compliance.




II. Next a setup.py and a README.txt file are required

 

Creating setup.py

 

Textbook example:
from setuptools import setup

setup(name='vsearch', version='1.01', description='The Head First Python Search Tools', author='HF Python 2e', author_email='hfpy2e@gmail.com', url='headfirstlabs.com', py_modules=['vsearch'],)

Creating README.txt

Just put in whatever you want. So long as the README exists.

III. Run setup.py as the setup distribution

In the command line run as follows: 

py -3 setup.py sdist

IV. Install your new module from within it's distribution



Example:

py -3 -m pip install [moduleName]-1.0.zip

V. Now you can run your module. Don't forget to run it as something, or you'll have to call it by what it is. Have fun! :)

Python Basic Data Structures and Common Commands Cheatsheet


Lists
[]
list
append, extend, pop, index
Tuples (immutable)
() or {},
tuple

Sets
{'',}


Dictionary
{}
dict


List | Common Commands:

Append | For adding a single element. If you give it a list, it'll add a list of a list
Extend | for merging a lot of elements
Clear | for clearing the element
Index | for searching the position of an item, first occurrence.
Remove | for removing a specific item, first occurrence
pop | for removing a single item from at a INDEX position (AND returning it if need be). 
Copy | For creating a new object. Anyway else will just reference the old object
Reverse | Flips things over
Count | Built in Frequency Counter


Examples




Iterating in Python Sampler


for i in [1, 2, 3]:
print (i)

for ch in "Yay!":
print(ch)

for num in range(5):
print('David is good at this!')

help(range)


phrase="Don't Panic!"

All the Letters

phrase

Every Third Letter up to index location 10

phrase[0:10:3]

All letters up to but not including the 10th
phrase[:10]

Only the first three letters
phrase[3:]

Every Second Letter
phrase[::2]

How to count backwards 1
backwords = phrase[::-1}
''.join(backwords)

>>> phrase[::-1]
"!cinap t'noD"

How to count backwards 2
>>> phrase = "The Sky is Blue"
>>> words = phrase.split(' ')
>>> words
['The', 'Sky', 'is', 'Blue']
>>> words[::-1]
['Blue', 'is', 'Sky', 'The']