Skip to main content

Tools for Python scripts or terminal

Project description

invesytoolbox

A set of useful tools, created for my own convenience.

Why "invesy"? Invesy (from German Inhaltsverwaltungssystem == content management system) is a closed source cms I created with Thomas Macher. It's only used in-house, that's why we didn't bother making it open source.

Invesy runs on Zope, so most of the individual website's logic runs in restricted Python. That's one reason for this toolbox: providing a set of useful functions in one single package which can be allowed in our restricted Python environment without having to allow a long list of external packages. Also some packages, while being importable, contain functions or methods not usable in restricted Python (like bs4's prettify).

That's also why all date and time functions also take into account the old DateTime (as opposed to datetime) package, on which Zope is still relying upon heavily.

What's in the box?

  • data_tools
  • date_time_tools
  • email_phone_tools
  • locales_tools
  • restricted_python_tools
  • security_tools
  • terminal_tools
  • text_name_tools
  • www_tools

data_tools

  • dict_from_dict_list: Create a dictionary from a list of dictionaries. You need to provide the key that's to be used.
  • create_vcard: Provide a dictionary, get a vCard.
  • dict_2unicode
    • Converts all keys and values in a dictionary from bytes to unicode.
    • All keys and values are changed from bytes to unicode, if applicable.
    • Other data types are left unchaged, including other compound data types.
  • value_2datatype: Convert a value to a datatype. This function has two modes:
    1. it uses a json file with the metadata (types of variables by name)
    2. it makes educated guesses in converting a string (don't feed bytes to this function!)
  • dict_2datatypes: Converts all data of a dictionary to specific (json metadata) or guessed types using value_2datatype.
  • dictlist_2datatypes: a convenience function using dict_2datatypes
  • sort_dictlist: Sorts a list of dictionaries. A single or multiple keys can provided for sorting.

date_time_tools

  • get_dateformat: Guess the correct format string (like %d.%m.%Y) to correctly convert a string to some date format (datetime.datetime, datetime.date or DateTime.DateTime).
  • str_to_dt: convert a string to datetime.datetime
  • str_to_DT: convert a string to DateTime.DateTime
  • str_to_date: convert a string to datetime.date
  • DT_to_dt: convert DateTime.DateTime to datetime.datetime
  • DT_to_date: convert DateTime.DateTime to datetime.date
  • date_to_dt: convert datetime.date to datetime.datetime
  • date_to_DT: convert datetime.date to DateTime.DateTime
  • convert_datetime: Using the convert-functions above, convert a date, time or string to any other type.
  • get_calendar_week: Get the calendar week from a date, time or string.
  • get_dow_number: Get the day of week number from a string or a date and time format.
  • get_isocalendar: Get the isocalendar tuple (year, week, dow nb) of a date, time or string.
  • get_monday (obsolete): from year and week.
  • daterange_from_week: Get the first and last day of a week.
  • dates_from_week: Get all days of a week.
  • day_from_week: Get a specific day from a week.
  • monday_from_week: self explanatory
  • last_week_of_year: Get the last week of the year (sometimes 52, sometimes 53)

email_phone_tools

  • create_email_message: Create an email message (no sending)
  • process_phonenumber
    • Processes a phonenumber and returns it in international format.
    • Checks if a phonenumber is valid

locales_tools

  • get_locale: Get the current locale, returns a dictionary containing (with examples):
    • locale (tuple): ('de_AT', 'utf-8')
    • locale_string: 'de_AT'
    • language: 'de'
    • country: 'AT'
    • currency: 'EUR'
  • fetch_all_countries: Returns a tuple, a list, a dictionary or a dictionary of dictionaries.
  • format_price: Formats a price according to the locale, ex: 4537 -> € 4.537,-
  • get_country: Returns all the data for a country as a dictionary.
  • get_language_name: Returns the name of a given language code in a language of choice.
  • fetch_holidays: Fetch the official holidays for a country in a specific period of time (given in years or a date range).
  • is_holiday: Check if a date is a holiday in a specific country (or in your current locale).

security_tools

  • create_secure_key: Generate a key (default 256 bit)
  • create_key: Create a key or password
  • check_password_security: Checks if a password (or any string) meets certain criteria

terminal_tools

  • print_: Enhanced print function, specially enhanced for printing in the terminal.
  • wait_for_key: Wait for a pressed key to continue.

text_name_tools

  • and_list: creates a human-readable list, including "and" (or any similar word, i.e. in another language), like

     1, two, Three and 4
    
  • capitalize_text: Capitalize text or names. This is specially handy for names which otherwise would be capitalized wrongly with string's "title" method like:

    • Conan McArthur
    • Susanne Mayr-Grünwald
    • Maria de Angelis
  • could_be_a_name: checks if a string could possibly be a legit name

  • get_gender of a prename.

  • leet: Create a leet from any string. Georg --> 630r9

  • sort_names: Sorts by name, prename: splits only at the last space before sorting. Correctly sorts names

    • with special characters, like umlauts
    • combined names
    • names with prefixes (like de or von)

    examples:

    • Maria de Angelis
    • Susanne Mayr-Grünwald
  • map_special_chars Converts any string to ascii, sometimes converting a single character to two, like for German umlauts (only if sort == False):

     ä --> ae
     ö --> oe
     ü --> ue
     ß --> ss
    
  • sort_word_list: Sort a list of words using map_special_chars.

www_tools

  • change_query_string: change arguments in a query string. A dict of params or an url must be provided.
  • prettify_html: this is the prettify unchanged function from bs4 (BeautifulSoup) for usage in restricted Python.

History

0.0.10

  • check_spam for web forms
  • dictlist_2datatypes: iterates through a list of dictionaries and applies dict_2datatypes
  • prettify_html: provides prettify from BeautifulSoup, because it can't be used directly from restricted Python.
  • could_be_a_name: checks if a string could be a name

0.0.9

  • change_query_string respects Zope parameter converters (like paramname:int)

0.0.8

  • New submodule www
    • change_query_string

0.0.7

  • shorter submodule names (no _tools suffix)
  • is_holiday works without argument
  • create_email_message: new argument encoding
  • process_phonenumbers: cleaned up arguments
  • DT_date: strip a DateTime of its time (get a "naked" date)
  • could_be_a_name: check if a string could possibly be a name

0.0.6

  • renamed capitalize_text to capitalize_name and removed name argument
  • added Sphinx documentation

0.0.5 (2022-06-11)

  • removed terminal_tools (will be included in a separate package)

0.0.4 (2022-06-09)

  • better formatted README

0.0.3 (2022-06-09)

  • updated README (list of functions and a short description)

0.0.2 (2022-06-09)

  • removed VERSION file

0.0.1 (2022-06-09)

  • first version

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

invesytoolbox-0.0.10.tar.gz (27.9 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page