Poppy Cooke

Poppy Cooke

1560956256

How to Submit a Package to PyPI

Writing Python packages and deploying them to PyPI is one way of sharing libraries to the open-source community. For beginners, submitting a package to PyPI is not an easy task. This blog guides you through the complete process including testing and continuous integration and deployment (CI/CD).

Register first!

As a potential open-source software contributor, you need to create the following accounts.

PyPI

PyPi is the main package repository for Python. Register at https://pypi.org/.

Test PyPI

Test PyPI let’s you deploy test packages before deploying them to PyPi. Register at https://test.pypi.org/.

Github

Github is a code repository. You should already be familiar with git. Create an account at https://github.com/. You may choose other git repositories like BitBucket but they will not be covered on this blog.

Travis CI

Travis CI let’s you test software on different platforms. It is a full-pledged continuous integration and deployment (CI/CD) platform. Register at https://travis-ci.org/ using your Github account. You may choose to use other CI/CD platforms but they will not be covered on this blog.

CodeCov

CoveCov provides visualization for code coverage which helps you find untested code. Register at https://codecov.io/gh using your Github account.

Create a Github project

Go to https://github.com/new and create a project. Notice that I have enabled Initialize this repository with a README, used MIT License and integrated Travis CI.

Clone your repository

git clone git@github.com:roniemartinez/amortization.git
git checkout -b staging

Write your package

We will use our example from my previous blog, Calculating Amortization with Python. Below is the improved version (amortization.py). I have added a main() function to use our package from command line after installation using pip.

def calculate_amortization_amount(principal, interest_rate, period):
    """
    Calculates Amortization Amount per period

    :param principal: Principal amount
    :param interest_rate: Interest rate per period
    :param period: Total number of periods
    :return: Amortization amount per period
    """
    x = (1 + interest_rate) ** period
    return principal * (interest_rate * x) / (x - 1)


def amortization_schedule(principal, interest_rate, period):
    """
    Generates amortization schedule

    :param principal: Principal amount
    :param interest_rate: Interest rate per period
    :param period: Total number of periods
    :return: Rows containing period, interest, principal, balance, etc
    """
    amortization_amount = calculate_amortization_amount(principal, interest_rate, period)
    number = 1
    balance = principal
    while number <= period:
        interest = balance * interest_rate
        principal = amortization_amount - interest
        balance -= principal
        yield number, amortization_amount, interest, principal, balance if balance > 0 else 0
        number += 1


def main():  # pragma: no cover
    import argparse
    from tabulate import tabulate

    parser = argparse.ArgumentParser(
        description='Python library for calculating amortizations and generating amortization schedules')
    # required parameters
    required = parser.add_argument_group('required arguments')
    required.add_argument('-P', '--principal', dest='principal', type=float, required=True, help='Principal amount')
    required.add_argument('-n', '--period', dest='period', type=int, required=True, help='Total number of periods')
    required.add_argument('-r', '--interest-rate', dest='interest_rate', type=float, required=True,
                          help='Interest rate per period')
    # optional parameters
    parser.add_argument('-s', '--schedule', dest='schedule', default=False, action='store_true',
                        help='Generate amortization schedule')
    arguments = parser.parse_args()
    if arguments.schedule:
        table = (x for x in amortization_schedule(arguments.principal, arguments.interest_rate, arguments.period))
        print(
            tabulate(
                table,
                headers=["Number", "Amount", "Interest", "Principal", "Balance"],
                floatfmt=",.2f",
                numalign="right"
            )
        )
    else:
        amount = calculate_amortization_amount(arguments.principal, arguments.interest_rate, arguments.period)
        print("Amortization amount: {:,.2f}".format(amount))


if __name__ == '__main__':   # pragma: no cover
    main()

Write a setup.py

setup.py contains all the definitions related to our package.

from setuptools import setup

VERSION = '0.1.0'

setup(
    name='amortization',
    version=VERSION,
    py_modules=['amortization'],
    url='https://github.com/roniemartinez/amortization',
    download_url='https://github.com/roniemartinez/amortization/tarball/{}'.format(VERSION),
    license='MIT',
    author='Ronie Martinez',
    author_email='ronmarti18@gmail.com',
    description='Python library for calculating amortizations and generating amortization schedules',
    long_description=open('README.md').read(),
    long_description_content_type='text/markdown',
    install_requires=['tabulate==0.8.3'],
    entry_points={
        'console_scripts': ['amortization=amortization:main'],
    },
    classifiers=[
        'Development Status :: 4 - Beta',
        'License :: OSI Approved :: MIT License',
        'Topic :: Office/Business :: Financial',
        'Topic :: Scientific/Engineering :: Mathematics',
        'Topic :: Software Development :: Libraries :: Python Modules',
        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: 3.7',
        'Programming Language :: Python :: Implementation :: CPython'
    ]
)

Here are the definitions:

name - name of library

version - version of library

py_modules - list of Python modules (.py) to include in our package. Use packages if we are including directory modules

url - package link, use Github repository link

download_url - link to download package aside from PyPI, use Github releases link

license - license of library

author - name of author

author_email - email of author

description - short description of library

long_description - long description of library, use README

long_description_content_type - tells PyPI the format of long description

install_requires - third party libraries needed

entry_points - used to define scripts

classifiers - this is use to classify our package, see https://pypi.org/pypi?%3Aaction=list_classifiers

Bonus: Write a setup.cfg

This is used to include other files to the resulting package and to define universal wheel.

[metadata]
description-file=README.md
license_files=LICENSE

[bdist_wheel]
universal = 1

Upload using twine

To install <a href="https://github.com/pypa/twine" target="_blank">twine</a>, run the following command:

pip install twine

Save your PyPI credentials

NOTE: Saving your credentials in plain text is not safe. Do not share them anywhere.
Save the following to $HOME/.pypirc:

[distutils]
index-servers=
    pypi
    testpypi

[pypi]
username=<username>
password=<password>

[testpypi]
repository=https://test.pypi.org/legacy/
username=<username>
password=<password>

Build your package

To build your package, run the following command

python setup.py sdist bdist_wheel

The following packages will be created. The first package amortization-<version>.tar.gz is the source distribution. The last package is a wheel distribution.

amortization-<version>.tar.gz
amortization-<version>-py2.py3-none-any.whl

Uploading

To upload, run the following command:

twine upload --repository testpypi dist/*  # uploads to Test PyPI
twine upload --repository pypi dist/*      # uploads to PyPI

Do not deploy untested code!

Writing code is one thing, deploying libaries for everyone is a different thing. You must test first before deployment. Below is the complete guide to automated testing and deployment.

Write your tests

We will use pytest package for testing. To install pytest, run the following command:

pip install pytest

First step is to create an empty conftest.py in the same directory where amortization.py is located. This will help pytest find our module. Create tests directory and write test_amortization.py containing the required tests.

from amortization import calculate_amortization_amount, amortization_schedule


def test_amortization_amount():
    principal = 150000
    period = 36
    interest_rate = 0.1
    amortization = principal * (interest_rate * (1 + interest_rate) ** period)/((1 + interest_rate) ** period - 1)
    assert calculate_amortization_amount(principal, interest_rate, period) == amortization


def test_amortization_schedule():
    principal = 150000
    period = 36
    interest_rate = 0.1

    amortization_amount = calculate_amortization_amount(principal, interest_rate, period)

    number = 1
    balance = principal

    for n, a, i, p, b in amortization_schedule(principal, interest_rate, period):
        interest = balance * interest_rate
        principal = amortization_amount - interest
        balance -= principal

        assert number == n
        assert amortization_amount == a
        assert interest == i
        assert principal == p
        assert balance == b

        number += 1

Running tests locally

To run tests, execute the following command.

pytest

Integrating with Travis CI

To define the jobs for Travis, write .travis.yml file. We will be testing on 4 different Python versions, 2.7, 3.5, 3.6, and 3.7. For managing dependencies, we will be using pipenv. See this Pipfile for the modules to be installed. We cannot deploy to PyPI on all these platforms, we will select 3.6 for deployment.

language: python

cache: pip

python:
  - 2.7
  - 3.5
  - 3.6

matrix:
  include:
  - python: 3.7
    dist: xenial
    sudo: true

branches:
  except:
    - /^[0-9]+\.[0-9]+\.[0-9]+/

install:
  - pip install pipenv
  - pipenv install --dev --skip-lock

script:
  pipenv run pytest --cov=amortization --cov-report=xml -v

after_success:
  pipenv run codecov

deploy:
  provider: pypi
  user: $TEST_PYPI_USER
  password: $TEST_PYPI_PASS
  server: https://test.pypi.org/legacy/
  distributions: "sdist bdist_wheel"
  on:
    branch: staging
    condition: $TRAVIS_PYTHON_VERSION = "3.6"

Notice the variables, $TEST_PYPI_USER and $TEST_PYPI_PASS.

Travis CI supports deployment to PyPI, however, it is not safe to save

the credentials to our code repository. We will need to save the

credentials to Travis Environment Variables. Go

to https://travis-ci.org///settings.

As our .travis.yml was only written for staging branch and to be deployed to Test PyPI, we need to modify it to work with master branch. From master branch, we will deploy to PyPI.

deploy:
  provider: pypi
  user: $PYPI_USER
  password: $PYPI_PASS
  distributions: "sdist bdist_wheel"
  on:
    branch: master
    condition: $TRAVIS_PYTHON_VERSION = "3.6"

Add project to CodeCov

To visualize code coverage, add Github project to CodeCov and obtain an upload token. Add CODECOV_TOKEN to Travis Environment Variables.

Better README

To add visualization about builds and versions and other information. Use shields provided by Travis CI, CodeCov, etc.

<table>
    <tr>
        <td>License</td>
        <td><img src='https://img.shields.io/pypi/l/amortization.svg'></td>
        <td>Version</td>
        <td><img src='https://img.shields.io/pypi/v/amortization.svg'></td>
    </tr>
    <tr>
        <td>Travis CI</td>
        <td><img src='https://travis-ci.org/roniemartinez/amortization.svg?branch=master'></td>
        <td>Coverage</td>
        <td><img src='https://codecov.io/gh/roniemartinez/amortization/branch/master/graph/badge.svg'></td>
    </tr>
    <tr>
        <td>Wheel</td>
        <td><img src='https://img.shields.io/pypi/wheel/amortization.svg'></td>
        <td>Implementation</td>
        <td><img src='https://img.shields.io/pypi/implementation/amortization.svg'></td>
    </tr>
    <tr>
        <td>Status</td>
        <td><img src='https://img.shields.io/pypi/status/amortization.svg'></td>
        <td>Downloads</td>
        <td><img src='https://img.shields.io/pypi/dm/amortization.svg'></td>
    </tr>
    <tr>
        <td>Supported versions</td>
        <td><img src='https://img.shields.io/pypi/pyversions/amortization.svg'></td>
    </tr>
</table>

These shields will result to a nice looking README.

Test and deploy without hassle

After completing all the above steps, deployment will be easier and faster. Just use git, update your code base and push.

git push

Installing our open-source library

We can now install and use our Python library easily.

pip install amortization

Since we added an entry point to our setup.py, we can call the package from command line.

amortization -P 150000 -n 36 -r 0.1 -s

Conclusion

Contributing libraries to PyPI is simple and easy. Open-source tools like Github and Travis CI, reduces time to production.

Source code for this project is available on Github.

#python

What is GEEK

Buddha Community

How to Submit a Package to PyPI
Joseph  Murray

Joseph Murray

1624048020

How to Find All the Classes of a Package in Java

In this article let’s take a look at how to find all classes of a package in Java

To find all classes of a package in Java we can use the ClassHunter of Burningwave Core library. So we start by adding the following dependency to our pom.xml:

XML

1

<dependency>2
    <groupId>org.burningwave</groupId>3
    <artifactId>core</artifactId>4
    <version>8.4.0</version>5
</dependency>

The next steps are the following:

  • retrieving the ClassHunter through the ComponentContainer
  • defining a regular expression that we must pass to the **ClassCriteria **object that will be injected into the **SearchConfig **object
  • calling the **loadInCache **method that loads in the cache all loadable classes of the indicated paths, then applies the criteria filter and then returns the **SearchResult **object which contains the classes that match the criteria

#java #classes #class #packages #package #how to find all the classes of a package in java

Jeewan Garg

Jeewan Garg

1596709222

Boost Your Business With Affordable SEO Packages India

Natural SEO positioning undoubtedly generates long-term revenue. Thus, choose our cost-effective SEO packages India to assist you in creating quality content, analyzing competition, enhancing technical accepts of your website, etc. With JeewanGarg’s affordable SEO Packages, you will achieve the top of the search result in your domain.

Firstly, the best SEO Agency in Delhi talks with you to get to know your business, objectives, and concepts better, and thus they provide you with an ideal and authentic solution. If you have a plan or want to enhance any, JeewanGarg will ask you some questions that will help the agency learn more about your project and proffer you with an outstanding solution that will help you improve and boost your ROI.

JeewanGarg.com is a Digital Marketing Agency, the proffer seo packages india, and the packages is available throughout India and internationally. We are distributors of authentic Google Solutions and an authorized Google Partner, with a handy experience over a decade.

JeewanGarg, apart from Search Engine Optimization (SEO), also has excellency in Google AdWords, Social Marketing, YouTube Marketing, and CMS’s SEO integrations. JeewanGarg has loads of experience in the fields that have helped numerous customers reach and efficiently cover their market.

However, if you want to see your online presence in leading search engines, such as Bing, Yahoo, and Google, JeewanGarg has outstanding knowledge to put your business at its top.

JeewanGarg and their experts work very closely with their clients based on their organizations’ line of services and products, whatever the size of the business. Our priority is the client, and at JeewanGarg, they believe in a personal and friendly relationship in close association with their clients. Our objective is to help our clientele generate more traffic on their websites in an affordable way. The higher the amount of traffic on your website, your business will earn more revenue.
The success of your online business mostly depends on Search Engine Optimization. Thus, it is advisable if you are not an expert in SEO, then you shouldn’t do it yourself. Hire the best SEO Agency in Delhi, choose the Best SEO Packages, and enhance your business online, quickly reaching your ultimate customer.

They are plenty of SEO Packages in India available with JeewanGarg.com, but be careful while selecting the best for yourself and your business. We offer yearly SEO Packages, along with some monthly SEO Packages as well. Apart from these, we also provide you with project-based SEO service, basic SEO Packages, local SEO Packages, E-commerce SEO Packages, Local SEO Packages, and Enterprise SEO Packages.

#best seo packages #seo packages india #cheap seo packages india #seo packages in india

How to Install Pyenv on Ubuntu 18.04

What is Pyenv?
Pyenv is a fantastic tool for installing and managing multiple Python versions. It enables a developer to quickly gain access to newer versions of Python and keeps the system clean and free of unnecessary package bloat. It also offers the ability to quickly switch from one version of Python to another, as well as specify the version of Python a given project uses and can automatically switch to that version. This tutorial covers how to install pyenv on Ubuntu 18.04.

#tutorials #apt #debian #environment #git #github #linux #package #package management #package manager #personal package archive #ppa #pyenv #python #python 3 #python support #python-pip #repository #smb #software #source install #ubuntu #ubuntu 18.04 #venv #virtualenv #web application development

Rohit Kumar

1619001299

iBrandox | Product Packaging Designing Company in Delhi

iBrandox is a genuine Product design company, Product Packaging design is the process of designing product packaging to securely contain, identify, and Product package design is the exterior part of the product. Product package design that creates everything on the outside of a product. If you are looking for the best Product Packaging Designing Company in Delhi so you have to go over the website.

#product packaging designing company in delhi #product packaging designing company #product packaging designing #product packaging design

How to create a python package and publish to PyPI

The Python Package Index (PyPI) is a repository of python packages contributed by open-source python community to help others. What’s the purpose of creating packages? Without them, we would have to rely on sharing the code by copy/paste which is not efficient. A package can have pieces of code that grab data from an API, do some manipulations on inputs etc. Let’s see an example of python package I created to convert text to braille and steps to publish to PyPI.

Step 1: Set up the folder structure

Create a folder structure in the format shown below. The main package folder name should be same as your package name. The folder that contains your code can be named anything. Here I have kept the name same as the main package folder.

.
└── pybraille (main package folder)
    └── pybraille (folder that contains your code)
        └── your code here

Step 2: Add your code

Place all the class or script files in the folder that contains your code and add a init.py file. The init.py file should contain the import statements to expose only the required classes and functions you want the user to access. from pybraille.main import convertText, convertFile

**Also, you can add a list named all in the init.py file containing the module names. This allows the user to import all the module names mentioned in all variable when **from package import * is encountered.

__all__ = [‘convertText’, ‘convertFile’]

#pip #python-packages #python-programming #pypi #python