rudiments package

Rudiments – Fundamental elements for any Python project.

This package offers configuration handling and other basic helpers for Python projects.

Copyright © 2015 - 2020 Jürgen Hermann <jh@web.de>

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Submodules

rudiments.humanize module

I/O of common values in forms understood by humans.

rudiments.humanize.bytes2iec(size, compact=False)[source]

Convert a size value in bytes to its equivalent in IEC notation.

See http://physics.nist.gov/cuu/Units/binary.html.

Parameters
  • size (int) – Number of bytes.

  • compact (bool) – If True, the result contains no spaces.

Returns

String representation of size.

Raises

ValueError – Negative or out of bounds value for size.

rudiments.humanize.iec2bytes(size_spec, only_positive=True)[source]

Convert a size specification, optionally containing a scaling unit in IEC notation, to a number of bytes.

Parameters
  • size_spec (str) – Number, optionally followed by a unit.

  • only_positive (bool) – Allow only positive values?

Returns

Numeric bytes size.

Raises

ValueError – Unknown unit specifiers, or bad leading integer.

rudiments.humanize.merge_adjacent(numbers, indicator='..', base=0)[source]

Merge adjacent numbers in an iterable of numbers.

Parameters
  • numbers (list) – List of integers or numeric strings.

  • indicator (str) – Delimiter to indicate generated ranges.

  • base (int) – Passed to the int() conversion when comparing numbers.

Returns

Condensed sequence with either ranges or isolated numbers.

Return type

list of str

rudiments.morph module

Data type conversions.

rudiments.pysupport module

Python helpers + magic.

rudiments.pysupport.import_name(modulename, name=None)[source]

Import identifier name from module modulename.

If name is omitted, modulename must contain the name after the module path, delimited by a colon.

Parameters
  • modulename (str) – Fully qualified module name, e.g. x.y.z.

  • name (str) – Name to import from modulename.

Returns

Requested object.

Return type

object

rudiments.pysupport.load_module(modulename, modulepath)[source]

Load a Python module from a path under a specified name.

Parameters
  • modulename (str) – Fully qualified module name, e.g. x.y.z.

  • modulepath (str) – Filename of the module.

Returns

Loaded module.

rudiments.security module

Security / AuthN / AuthZ helpers.

class rudiments.security.Credentials(target)[source]

Bases: object

Look up and provide authN credentials (username / password) from common sources.

AUTH_MEMOIZE_INPUT = {}
NETRC_FILE = None
URL_RE = re.compile('^(http|https|ftp|ftps)://')
auth_pair(force_console=False)[source]

Return username/password tuple, possibly prompting the user for them.

auth_valid()[source]

Return bool indicating whether full credentials were provided.

rudiments.system module

Operating system related stdlib extensions.

rudiments.www module

WWW access helpers.

You need a dependency on requests in your project if you use this module.

rudiments.www.url_as_file(url, ext=None)[source]

Context manager that GETs a given url and provides it as a local file.

The file is in a closed state upon entering the context, and removed when leaving it, if still there.

To give the file name a specific extension, use ext; the extension can optionally include a separating dot, otherwise it will be added.

Parameters
  • url (str) – URL to retrieve.

  • ext (str, optional) – Extension for the generated filename.

Yields

str – The path to a temporary file with the content of the URL.

Raises

requests.RequestException – Base exception of requests, see its docs for more detailed ones.

Example

>>> import io, re, json
>>> with url_as_file('https://api.github.com/meta', ext='json') as meta:
...     meta, json.load(io.open(meta, encoding='ascii'))['hooks']
(u'/tmp/www-api.github.com-Ba5OhD.json', [u'192.30.252.0/22'])