Skip to main content

JustBard's Python Utilities

Project description

jblib

Author: Justin Bard

This module was written to minimize the need to write the functions I use often.

INSTALL: python3 -m pip install jblib


The source code can be viewed here: https://github.com/ANamelessDrake/jblib

More of my projects can be found here: http://justbard.com


from jblib import cd

    class cd()

        Example:
            with cd(directory):
                print (os.getcwd())

            print (os.getcwd()) ## Back at the originating directory on exit

Python 3.8 or earlier: from jblib import hilight

Python 3.9 or later: from jblib import hilightV2

    class hilight(string).color(highlight=True, bold=True)

    EXAMPLE:
        print (hilight("Hello World").red(bold=True))

        Or you could make an object:
            text = hilight("Bar")

            print ("Foo "+text.blue())

        To return the original string:
            print (text.string)

    COLORS:
        red
        green
        yellow
        blue
        purple
        teal
        white

    FUN FACTS:
        * This class is loosely based off the very first bit of python code I ever wrote. It was initially created while teaching myself python.
        * This module was intentionally misspelled to shorten the keystrokes needed during use.

from jblib import convert_module

    Module to convert various data

            def convert_time_from_seconds(seconds_given)
                Converts a seconds into minutes, hours and days.

            def IP2Int(ip)
                Converts a IPv4 address to a interger - This is useful to store IP addresses in databases

            def Int2IP(ipnum)
                Converts a interger back to an IPv4 address

            def urlcode(url, encode=False)
                Wrapper for urllib.parse.quote and urllib.parse.unquote.
                From urllib docs - Replace special characters in string using the %xx escape. Letters, digits, and the characters '_.-' are never quoted. By default, this function is intended for quoting the path section of URL.
                - https://docs.python.org/3.1/library/urllib.parse.html?highlight=urllib#urllib.parse.quote

from jblib import HTMLgen

        Basic HTML generator
        02/09/2019

        class HTMLgen(head=False, tail=False, lang="en", docType="html")

        FUNCTIONS:
            title(self, title, scripts=None, css=None)
            body.add(content)
            tag(tag, content=False, close=True, cssclass=None)
            image(src, alt=None, srcset=None, height=None, width=None, style=None, cssclass=None)
            br() <-- Returns a </ br> tag
            div(cssclass) <-- Not yet implemented
            table() -- Class Object
                table.add_row()
                table.produce_table()

        EXAMPLE:
            page = HTMLgen(True, True)
            page.title("This is the page Title", scripts="foo.js bar.js", css="styles.css nav.css")
            page.body.add(page.image("images/frontpage.jpg", width="100%"))
            page.body.add(page.tag("h1", "This is a header line"))
            page.body.add("This is another line")

            ## Creating a table
            test_table = HTMLgen.table()
            test_table.add_row(["column data 1", "column data 2", "column data 3"])
            test_table.add_row(["column data 4", "column data 5", "column data 6"])

            ## And finally we add the table to the rest of the page
            page.body.add(test_table.produce_table())


            page.return_html()

            ```
                <!DOCTYPE html>
                <html lang="en>
                    <head>
                        <title>This is the page Title</title>
                        <link rel="stylesheet" href="styles.css">
                        <link rel="stylesheet" href="nav.css">
                        <script src="foo.js"></script>
                        <script src="bar.js"></script>
                    </head>
                <body>
                    <img src="images/frontpage.jpg" width="100%">
                    <h1>This is a header line</h1>
                    This is another line
                   	<table>
                        <tr>
                            <td>column data 1</td><td>column data 2</td><td>column data 3</td>
                        </tr>
                        <tr>
                            <td>column data 4</td><td>column data 5</td><td>column data 6</td>
                        </tr>

                    </table>

                </body>
                </html>
            ```

from jblib import progress_bar

    Progress Bar
    02/25/2019

    FUNCTIONS:
        progress_bar(progress, barLength=50, text_field="Progress")

    EXAMPLE:
        counter = 0
        for i in range(100):
            counter += 1
            i = counter/100
            progress_bar(i, 50)
            sleep(0.1)

    Progress: [#########################-------------------------] 50.0%

from jblib import colored_progress_bar

    Colored Progress Bar
    10/10/2023

    FUNCTIONS:
        colored_progress_bar(progress, barLength=50, text_field="Progress")

    EXAMPLE:
        counter = 0
        for i in range(100):
            counter += 1
            i = counter/100
            colored_progress_bar(i, 50)
            sleep(0.1)

    Progress: [#########################-------------------------] 50.0%

from jblib import StreamToLogger

    DESCRIPTION:
        Fake file-like stream object that redirects writes to a logger instance.

    CLASS:
        StreamToLogger(object)
    EXAMPLE:
        if log_enabled: ## If true, all standard output and standard error to the console will be disabled
            # create logger
            logging.basicConfig(format='%(asctime)s - %(message)s', datefmt='%Y%m%d %H:%M:%S', filename=log_file)
            logger = logging.getLogger(__name__)
            logger.setLevel(logging.DEBUG)

            logger.propagate = False
            fh = logging.FileHandler(log_file, "a")
            fh.setLevel(logging.DEBUG)
            formatter = logging.Formatter(fmt='%(asctime)s - %(message)s', datefmt='%Y%m%d %H:%M:%S')
            fh.setFormatter(formatter)
            logger.addHandler(fh)
            keep_fds = [fh.stream.fileno()]

            stdout_logger = logging.getLogger('STDOUT')
            sl = StreamToLogger(stdout_logger, logging.INFO)
            sys.stdout = sl

            stderr_logger = logging.getLogger('STDERR')
            sle = StreamToLogger(stderr_logger, logging.ERROR)
            sys.stderr = sle

from jblib import build_date_array

    Build Date Arrays
    02/27/2019

    FUNCTIONS:
        build_date_array(days=1, start_date=str(datetime.date.today()), date_format='%Y-%m-%d', mon=True, tues=True, wed=True, thur=True, fri=True, sat=True, sun=True, weekend=True):

    EXAMPLE:
        dates = build_date_array(days=7, weekend=False)
        dates
            ['2019-02-27', '2019-02-28', '2019-03-01', '2019-03-02', '2019-03-03', '2019-03-05', '2019-03-06']

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

jblib-1.8.6.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

jblib-1.8.6-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

Details for the file jblib-1.8.6.tar.gz.

File metadata

  • Download URL: jblib-1.8.6.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for jblib-1.8.6.tar.gz
Algorithm Hash digest
SHA256 9d99d40e9cb611b86c3c87ebd5218a494b1f0218e445ea9a8355b779ed4ee681
MD5 5fd75b95ed254a592afaf6e26b1b052b
BLAKE2b-256 de221914ebf1aafc1c6c1ee74b5d48a875873d4f3da5daacee2ae50111bf64e7

See more details on using hashes here.

File details

Details for the file jblib-1.8.6-py3-none-any.whl.

File metadata

  • Download URL: jblib-1.8.6-py3-none-any.whl
  • Upload date:
  • Size: 13.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for jblib-1.8.6-py3-none-any.whl
Algorithm Hash digest
SHA256 b7f4cfdbe5dcf1ec4a835bd66896f27ccddcf6fe88bec7a8067aee5f7a268af5
MD5 bc42e9b8b90e6c70c9229e7bf04294c1
BLAKE2b-256 3cd091ea1585f702ad8e17d5d92781a5ffef8c1c7569a815d2833ee8e30db3bc

See more details on using hashes here.

Supported by

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