commonpy

Collection of common Python utility functions and classes used in other Caltech Library programs.

https://github.com/caltechlibrary/commonpy

Science Score: 62.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
    1 of 2 committers (50.0%) from academic institutions
  • Institutional organization owner
    Organization caltechlibrary has institutional domain (www.library.caltech.edu)
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (18.2%) to scientific vocabulary

Keywords

data-utilities file-utilities file-utils network-utilities python python-functions python3 utility-classes utility-function

Keywords from Contributors

mesh sequences interactive book hacking archival network-simulation
Last synced: 4 months ago · JSON representation ·

Repository

Collection of common Python utility functions and classes used in other Caltech Library programs.

Basic Info
  • Host: GitHub
  • Owner: caltechlibrary
  • License: other
  • Language: Python
  • Default Branch: main
  • Homepage:
  • Size: 410 KB
Statistics
  • Stars: 19
  • Watchers: 4
  • Forks: 1
  • Open Issues: 5
  • Releases: 48
Topics
data-utilities file-utilities file-utils network-utilities python python-functions python3 utility-classes utility-function
Created over 7 years ago · Last pushed over 2 years ago
Metadata Files
Readme Changelog Contributing License Code of conduct Citation Support Codemeta

README.md

CommonPy

This is a collection of common utility functions and classes that we at the Caltech Library have found useful in our other Python projects.

Latest release License Python PyPI

Table of contents

Introduction

This repository does not constitute a single program; instead, it contains a collection of modules with utility functions and classes that we have found ourselves using repeatedly in other Python projects.

Installation

The instructions below assume you have a Python interpreter installed on your computer; if that's not the case, please first install Python version 3 and familiarize yourself with running Python programs on your system.

On Linux, macOS, and Windows operating systems, you should be able to install commonpy with pip. To install commonpy from the Python package repository (PyPI), run the following command: python3 -m pip install commonpy

As an alternative to getting it from PyPI, you can use pip to install commonpy directly from GitHub, like this: sh python3 -m pip install git+https://github.com/caltechlibrary/commonpy.git

Usage

The basic approach to using this package is to import the modules and functions you need. For example:

```python from commonpy.file_utils import readable

if readable('/path/to/some/file'): # do something ```

The following subsections describe the different modules available.

Data structures

The data_structures module provides miscellaneous data classes.

| Class | Purpose | |----------------|---------| | CaseFoldDict | A version of dict that compares keys in a case-insensitive manner | | CaseFoldSet | A version of set that compares keys in a case-insensitive manner |

Data utilities

The data_utils module provides a number of miscellaneous simple functions for some common operations on data of various kinds.

| Function | Purpose | |--------------------|---------| | expanded_range(string) | Given a string of the form "X-Y", returns the list of integers it represents | | flattened(thing) | Takes a list or dictionary and returns a recursively flattened version | | ordinal(integer) | Returns a string with the number followed by "st", "nd, "rd", or "th" | | parsed_datetime(string) | Returns a date object representing the given date string | | pluralized(word, n, include_num) | Returns a plural version of word if n > 1 | | sliced(list, n) | Yields n number of slices from the list | | timestamp() | Returns a string for an easily-readable form of the current time and date | | unique(list) | Takes a list and return a version without duplicates |

File utilities

The file_utils module provides a number of miscellaneous simple functions for some common operations on files and directories.

| Function | Purpose | |--------------------|---------| | alt_extension(file, ext) | Returns file with the extension replaced by ext | | copy_file(src, dst) | Copies file from src to dst | | delete_existing(file) | Deletes the given file | | filename_basename(file) | Returns file without any extensions | | filename_extension(file) | Returns the extension of filename file | | files_in_directory(dir, ext, recursive) | | | filtered_by_extensions(list, endings) | | | nonempty(file) | Returns True if file file is not empty | | open_file(file) | Opens the file by calling the equivalent of "open" on this system | | open_url(url) | Opens the url in the user's default web browser | | readable(dest) | Returns True if file or directory dest is accessible and readable | | relative(file) | Returns a path string for file relative to the current directory | | rename_existing(file) | Renames file to file.bak | | writable(dest) | Returns True if file or directory dest can be written |

Interruptible wait and interruption handling utilities

The interrupt module includes wait(...), a replacement for sleep(...) that is interruptible and works with multiple threads. It also provides methods to cause an interruption (including doing it by issuing a ^C to the program), check whether an interruption occurred, and other related operations.

| Function | Purpose | |--------------------------|---------| | config_interrupt(callback, raise_ex, signal) | Sets up a callback function | | interrupt() | Interrupts any wait in progress | | interrupted() | Returns True if an interruption has been called | | raise_for_interrupts() | Raises an exception if interrupt() has been invoked | | reset_interrupts() | Resets the interruption flag | | wait(duration) | Waits for duration in an interruptible fashion |

Module utilities

The module_utils collection of functions is useful for working with paths related to a running module, for example to find internal data files that might be needed for normal operation.

| Function | Purpose | |--------------------|---------| | config_path(module_name) | Returns the path to local config data directory for the module | | datadir_path(module_name) | Returns the path to the /data subdirectory of the module | | desktop_path() | Returns the path to the user's Desktop directory on this system | | installation_path(module_name) | Returns the path to module's installation directory | | module_path(module_name) | Returns the path to the installed module |

Function config_path(...) is useful to use in conjunction with Python's configparser module. It returns ~/.config/modulename/ on Unix-like systems.

Network utilities

The network_utils module provides several functions that are useful when performing network operations.

| Function | Purpose | | -------------------------------- | ------------------------------------------------------------------- | | download(url, local_dest) | Download a file | | download_file(url, local_dest) | Download a file without raising exceptions | | hostname(url) | Returns the hostname portion of a URL | | net(...) | See below | | netlock(url) | Returns the hostname, port number (if any), and login info (if any) | | network(...) | See below | | network_available() | Returns True if external hosts are reacheable over the network | | on_localhost(url) | Returns True if the address of url points to the local host | | scheme(url) | Returns the protocol portion of the url; e.g., "https" |

network and net

The network and net functions in the network_utils module implements a fairly high-level network operation interface that internally handles timeouts, rate limits, polling, HTTP/2, and more. The function signatures are identical to this:

python network(method, url, client = None, handle_rate = True, polling = False, **kwargs)

The difference between the two functions is their behavior with respect to exceptions. The function network returns only a response object, and raises an exception if any error occurs. The net function returns two values: response, error and does not raise exceptions except in the case of bad arguments; instead, any exceptions are returned as the error value in the list of return values. This allows the caller to inspect the response object even in cases where exceptions are raised.

The response object returned by both net and network is the response from the get or post HTTP call. This object comes from the Python HTTPX module used by

The method parameter value must be a string chosen from the list of known HTTP methods: "get", "post", "head", "options", "put", "delete", or "patch".

The url parameter value must be the URL to which the HTTP method will be applied.

If keyword client is not None, it's assumed to be a HTTPX Client object to use for the network call. Settings such as timeouts should be done by the caller creating appropriately-configured Client objects.

If keyword handle_rate is True, both functions will automatically pause and retry if it receives an HTTP code 429 ("too many requests") from the server. If False, it will return the exception CommonPy.exceptions.RateLimitExceeded instead.

If keyword polling is True, certain statuses like 404 are ignored and the response is returned; otherwise, they are considered errors. The behavior when True is useful in situations where a URL does not exist until something is ready at the server, and the caller is repeatedly checking the URL. It is up to the caller to implement the polling schedule and call this function (with polling = True) as needed.

Additional keyword arguments understood by HTTPX can be passed to both network and net.

Both methods always pass the argument allow_redirects = True to the underlying Python HTTPX library network calls.

download and `downloadfile`_

The functions download(url, local_destination) and download_file(url, local_destination) download a file at the given url, writing it to the file specified by the parameter local_destination. The former version of the function will raise exceptions in case of problems; the latter version simply return True or False depending on the success of the download.

String utilities

| Function | Purpose | |--------------------|---------| | antiformat(s) | Quote instances of { and } in s so it can be passed to format. | | print_boxed(msg) | Print a message with a box around it using pure ASCII characters. |

System utilities

| Function | Purpose | |--------------------|---------| | system_profile() | Returns a string describing the system running this Python program. |

Exceptions

The CommonPy module defines a number of exceptions that it may return. (Most of the exceptions are potentially thrown by net, discussed above.)

| Exception | Meaning | |--------------------------|---------| | CommonPyException | Base class for CommonPy exceptions | | | | | ArgumentError | The function call was given invalid or unexpected arguments | | AuthenticationFailure | Problem obtaining or using authentication credentials | | InternalError | Unrecoverable problem involving CommonPy itself | | Interrupted | The user elected to cancel/quit the program | | NetworkFailure | Unrecoverable problem involving net | | NoContent | No content found at the given location | | RateLimitExceeded | The service flagged reports that its rate limits have been exceeded | | ServiceFailure | Unrecoverable problem involving a remote service |

Getting help

If you find an issue, please submit it in the GitHub issue tracker for this repository.

Contributing

We would be happy to receive your help and participation with enhancing CommonPy! Please visit the guidelines for contributing for some tips on getting started.

If you plan on doing any development on CommonPy, you may want to install the package dependencies listed in requirements-dev.txt, e.g., using a command such as the following. This will install dependencies necessary to run pytest. python3 -m pip install -r requirements-dev.txt

License

Software produced by the Caltech Library is Copyright (C) 2020-2023, Caltech. This software is freely distributed under a BSD/MIT type license. Please see the LICENSE file for more information.

Authors and history

Mike Hucka started this collection of utilities sometime in 2018.

Acknowledgments

This work was funded by the California Institute of Technology Library.

The vector artwork of a toolbox, used as the icon for this repository, was created by priyanka from the Noun Project. It is licensed under the Creative Commons CC-BY 3.0 license.

CommonPy makes use of numerous open-source packages, without which it would have been effectively impossible to develop CommonPy with the resources we had. I want to acknowledge this debt. In alphabetical order, the packages are:

  • boltons – package of miscellaneous Python utilities
  • dateparser – parse dates in almost any string format
  • deprecation – a library to handle deprecation of code in your Python packages
  • h2 – HTTP/2 support library used by HTTPX
  • httpx – Python HTTP client library that supports HTTP/2
  • humanize – make numbers more easily readable by humans
  • ipdb – the IPython debugger
  • pytest – testing framework for Python
  • pytest-mock – wrapper around the mock package for use with pytest
  • python_dateutil – date utilities
  • PyYAML – Python YAML parser
  • pywin32 – Windows APIs for Python
  • sidetrack – simple debug logging/tracing package
  • tldextract – module to parse domains from URLs
  • twine – package for publishing Python packages to PyPI
  • validator-collection – collection of Python functions for validating data

Owner

  • Name: Caltech Library
  • Login: caltechlibrary
  • Kind: organization
  • Email: helpdesk@library.caltech.edu
  • Location: Pasadena, CA 91125

We manage the physical and digital holdings of the California Institute of Technology, provide services and training, and develop open-source software.

Citation (CITATION.cff)

# YAML 1.2
---
authors: 
  -
    affiliation: "Caltech Library"
    given-names: Mike
    family-names: Hucka
    orcid: "0000-0001-9105-5960"
cff-version: "1.1.0"
message: "If you use this software, please cite it using these metadata."
repository-code: "https://github.com/caltechlibrary/commonpy"
title: "CommonPy: assortment of Python helper functions and utility classes"
date-released: 2023-05-02
version: "1.13.0"
keywords:
  - Python
  - utilities
...

GitHub Events

Total
  • Fork event: 1
Last Year
  • Fork event: 1

Committers

Last synced: 5 months ago

All Time
  • Total Commits: 398
  • Total Committers: 2
  • Avg Commits per committer: 199.0
  • Development Distribution Score (DDS): 0.003
Past Year
  • Commits: 0
  • Committers: 0
  • Avg Commits per committer: 0.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
mhucka m****a@c****u 397
dependabot[bot] 4****] 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 5 months ago

All Time
  • Total issues: 7
  • Total pull requests: 1
  • Average time to close issues: about 1 year
  • Average time to close pull requests: 2 days
  • Total issue authors: 2
  • Total pull request authors: 1
  • Average comments per issue: 0.57
  • Average comments per pull request: 0.0
  • Merged pull requests: 1
  • Bot issues: 0
  • Bot pull requests: 1
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • mhucka (6)
  • t4k (1)
Pull Request Authors
  • dependabot[bot] (1)
Top Labels
Issue Labels
Coding: enhancement ✨ (2) Coding: bug 🐛 (2) Task: chore 📥 (1) Coding: compatibility 🔌 (1)
Pull Request Labels
dependencies (1)

Packages

  • Total packages: 1
  • Total downloads:
    • pypi 806 last-month
  • Total docker downloads: 160
  • Total dependent packages: 8
  • Total dependent repositories: 19
  • Total versions: 48
  • Total maintainers: 1
pypi.org: commonpy

Assortment of Python helper functions and utility classes

  • Versions: 48
  • Dependent Packages: 8
  • Dependent Repositories: 19
  • Downloads: 806 Last month
  • Docker Downloads: 160
Rankings
Dependent packages count: 1.1%
Docker downloads count: 2.5%
Dependent repos count: 3.3%
Downloads: 10.2%
Average: 10.4%
Stargazers count: 15.6%
Forks count: 29.8%
Maintainers (1)
Last synced: 5 months ago

Dependencies

requirements-dev.txt pypi
  • flake8 >=4.0.1 development
  • flake8-bugbear >=22.4.25 development
  • flake8-builtins >=1.5.3 development
  • flake8-comprehensions >=3.8.0 development
  • flake8-executable >=2.1.1 development
  • flake8-pie >=0.15.0 development
  • flake8-simplify >=0.19.2 development
  • flake8_implicit_str_concat >=0.3.0 development
  • pytest >=6.2.5 development
  • pytest-cov >=3.0.0 development
  • pytest-mock >=3.7.0 development
requirements.txt pypi
  • PyYAML >=5.3.1
  • boltons ==21.0.0
  • dateparser ==1.0.0
  • deprecation ==2.1.0
  • h2 ==3.2.0
  • httpx ==0.23.0
  • humanize >=3.9.0
  • python_dateutil ==2.8.2
  • pywin32 >=301
  • sidetrack >=2.0.0
  • tldextract ==3.0.2
  • validator-collection ==1.5.0
.github/workflows/codeql-analysis.yml actions
  • actions/checkout v2 composite
  • github/codeql-action/analyze v1 composite
  • github/codeql-action/autobuild v1 composite
  • github/codeql-action/init v1 composite