general

Author: Ezio416
Created: 2022-08-18
Updated: 2022-11-21
  • Functions for various things

  • These are all imported to py416 directly, so just call them like so: py416.timestamp()

bytesize(byte: int, si: bool = False) str[source]
  • scales a number of bytes to a prefixed format

Parameters:
  • byte (int) –

    • number of bytes to scale

  • si (bool) –

    • whether to use SI units (1000^x bytes: KB, MB, GB)

    • default: False (1024^x bytes: KiB, MiB, GiB)

Returns:

  • bytes in prefixed format, rounded to 2 decimal places

  • i.e.:
    • 1200000 => ‘1.20MB’

    • 1253656678 => ‘1.17GiB’

Return type:

str

emailsmtp(sender_email: str, recipient: str, subject: str, body: str, smtp_server: str, smtp_port: int, password: str, sender_name: str = '') bool[source]
  • sends an email via SMTP

Parameters:
  • sender_email (str) –

    • email address to send from

  • recipient (str) –

    • email address to send to

  • subject (str) –

    • text to put in the email subject

  • body (str) –

    • test to put in the email body

  • smtp_server (str) –

    • host name for SMTP server

  • smtp_port (int) –

    • port for SMTP server

  • password (str) –

    • password for sender email

  • sender_name (str) –

    • name to show in the ‘from’ field

    • default: none

Returns:

  • whether send was successful

Return type:

bool

gettype(thing) str[source]
  • gets the type of an object

  • wraps type()

Parameters:

thing

  • object of any type

Returns:

  • type of object

Return type:

str

lineno() int[source]
  • gets the line number in the file where the function was called from

Returns:

  • line number in Python script

Return type:

int

month2num(month_word: str) str[source]
  • converts month names to their 2-digit number

Parameters:

month_word (str) –

  • full month name

Returns:

  • zero-padded 2-digit number

Return type:

str

pprint(iterable: object, do_print: bool = True, level: int = 0) str[source]
  • pretty print an iterable object without splitting strings

  • not yet working for dictionaries or special iterable types

Parameters:
  • iterable (object) –

    • thing to pretty print

  • do_print (bool) –

    • whether to print the output

    • default: True

  • level (int) –

    • recursion level

    • not meant to be changed (not useful to the user anyway)

Returns:

  • output

Return type:

str

secmod(seconds: float, sep: str = '') tuple[source]
  • formats a number of seconds nicely, split by days, hours, minutes, and seconds

  • takes the absolute value of the input so the result is always positive

Parameters:
  • seconds (int | float) –

    • number to convert

  • sep (str) –

    • separator between values

    • default: nothing

Returns:

  • string in format and individual values

  • i.e. (‘04d16h47m09s’, 9, 47, 16, 4)

Return type:

tuple [str | int]

secmod_inverse(timestr: str) int[source]
  • does essentially the opposite of :func::secmod

  • converts a string to a number of seconds

Parameters:

timestr (str) –

  • representation of time

  • must be formatted like the base output from :func::secmod, i.e. “3d16h5m47s”

  • can be missing parts, i.e. “3s47s”

  • capitalization is ignored

  • if multiple of the same type of value are passed in the string, i.e. “4h16h”, only the first value is grabbed

Returns:

  • number of seconds

Return type:

int

timestamp(brackets: bool = True, micro: bool = False, offset: bool = True, readable: bool = False, seconds: bool = True, utc: bool = False) str[source]
  • creates a timestamp in ISO format with additional formatting

  • default example: [2022-07-06T13:57:12-06:00]

Parameters:
  • brackets (bool) –

    • whether to surround timestamp in square brackets

    • default: True

  • micro (bool) –

    • whether to include microseconds

    • default: False

  • offset (bool) –

    • whether to include offset from UTC, e.g. timezone

    • default: True

  • readable (bool) –

    • whether to internal whitespace for legibility

    • default: False

  • seconds (bool) –

    • whether to include seconds

    • default: True

  • utc (bool) –

    • current UTC time

    • default: False

Returns:

  • current timestamp with chosen formatting

  • i.e. [2022-08-18 07:15:43.962 +00:00]

Return type:

str

unpack(iterable) tuple[source]
  • recursively retrieves items from some iterable types

Parameters:

iterable (list | tuple) –

  • thing to unpack

Returns:

  • all retrieved items

  • if iterable is not actually iterable, returns a tuple with just the item

Return type:

tuple