Code Monkey home page Code Monkey logo

cyutils's Introduction

PyPI version PyPI license PyPI pyversions Downloads

CyUtils

CyUtils is an all purpose package library for python. It currently adds Colored Text, Message Boxes, Vector2s, Extra List Functions, Ciphers. CyUtils will soon have support Vector3s.

Main menu:

Installation

You can install CyUtils with pip! Once you have installed pip, open Command Prompt, Terminal or Powershell and type:

pip install cyutils

Colored Text Module

To print colored text, you first need to import the colored text module:

from cyutils.io import colored_text

You can then use the clrprint() function to print colored text:

from cyutils.io import colored_text

colored_text.clrprint("<Text goes here>", "red")

The colored text module supports a wide range of colors:

  • standard
  • black
  • red
  • green
  • yellow
  • blue
  • magenta
  • cyan
  • white

Message Boxes Module

The Message Boxes module allows you to create a windows message box using VB overloads! It creates a temporary vbscript and executes it to create a message box. To begin, you need to import the message boxes module:

from cyutils.io import message_boxes as mb

This package will allow you to create a message box with a simple message_box() function.

NOTE: THIS FUNCTION WILL ONLY WORK ON WINDOWS OPERATING SYSTEMS

from cyutils.io import message_boxes as mb

mb.message_box("<Window Title>", "<Box Contents>", "< (OPTIONAL) VB Overloads>")

For the VB Overload you can either add text overloads:

e.g. "vbOkOnly+vbCritical"

Or get the sum of the overloads that you want from the table below:

Constant Value Description
vbOKOnly 0 Display OK button only.
vbOKCancel 1 Display OK and Cancel buttons.
vbAbortRetryIgnore 2 Display Abort, Retry, and Ignore buttons.
vbYesNoCancel 3 Display Yes, No, and Cancel buttons.
vbYesNo 4 Display Yes and No buttons.
vbRetryCancel 5 Display Retry and Cancel buttons.
vbCritical 16 Display Critical Message icon.
vbQuestion 32 Display Warning Query icon.
vbExclamation 48 Display Warning Message icon.
vbInformation 64 Display Information Message icon.
vbDefaultButton2 256 Second button is default.
vbDefaultButton3 512 Third button is default.
vbDefaultButton4 768 Fourth button is default.
vbSystemModal 4096 System modal; all applications are suspended until the user responds to the message box.
vbMsgBoxHelpButton 16384 Adds Help button to the message box.
vbMsgBoxSetForeground 65536 Specifies the message box window as the foreground window.
vbMsgBoxRight 524288 Text is right-aligned.
vbMsgBoxRtlReading 1048576 Specifies text should appear as right-to-left reading on Hebrew and Arabic systems.

Custom Error Module

The Custom Error module allows you to create an error without using raise or showing the lines of code that the error occured in. To begin, import the Custom Error module:

from cyutils.io.custom_error import CustomError

The title of an error is the name of the error you want to throw. This is not restricted to default python error names, it can be anything! You can throw a basic error like this:

from cyutils.io.custom_error import CustomError

CustomError("<Title of Error>").throw("<Error Message>")

The custom error can also be stored as a variable and thrown later:

from cyutils.io.custom_error import CustomError

myError = CustomError("<Title of Error>")

#SOME CODE HERE

myError.throw("<Error Message>")

You can also configure the error so that it doesn't exit after sending the message to the console:

from cyutils.io.custom_error import CustomError

myError = CustomError("<Title of Error>", exitOnError=False)
myError.throw("<Error Message>")

print("HELLO WORLD") # HELLO WORLD

Vector2 Module

The Vector2 Module allows the creation and manipulation of 2 dimensional vectors in python. To begin, import the Vector2 module:

from cyutils.tools.math import Vector2

You can now create a vector with a simple command. The following code creates the vector (2, 3) and assigns it to a variable, x:

from cyutils.tools.math import Vector2

x = Vector2(2, 3)

You can now manipulate this with standard operators, such as +, -, *, /, // and check if vectors are equal with ==.

Using a standard operator between two vector will do the operation to their X and Y values, whereas doing a standard operation between a vector and an int or float will apply the operation to both X and Y values with the said int or float. The Vector2 Module also contains some functions. You can see them below:

Vector2 Module: Functions

A menu of all the functions is listed below:

Magnitude

You can use the magnitude function to get the magnitude of a vector. You can see an implementation below:

from cyutils.tools.math import Vector2

myVector = Vector2(2, 3)
print(myVector) # Vector2: (2, 3)

magnitude = myVector.magnitude()
print(magnitude) # 3.605551275463989

You can also get the square magnitude of a vector by using the sqrMagnitude function. This is useful if you want to compare two vectors as this function is faster than the magnitude function. You can see an implementation below:

from cyutils.tools.math import Vector2

myVector = Vector2(2, 3)
print(myVector) # Vector2: (2, 3)

magnitude = myVector.sqrMagnitude()
print(magnitude) # 13

Normalize

You can use the normalize function to get the normalized Vector. That is a Vector where all the values are between 0 and 1. You can see an implementation below:

from cyutils.tools.math import Vector2

myVector = Vector2(2, 3)
print(myVector) # Vector2: (2, 3)

myVector.normalize()
print(myVector) # Vector2: (0.5547001962252291, 0.8320502943378437)

You can also use the normalized function which returns a new vector instead of changing the old one. You can see an implementation below:

from cyutils.tools.math import Vector2

myVector = Vector2(2, 3)
print(myVector) # Vector2: (2, 3)

normalizedVec = myVector.normalized()
print(normalizedVec) # Vector2: (0.5547001962252291, 0.8320502943378437)

Distance

You can use the distance function to calculate the distance between two Vector2s. You can see an implementation below:

from cyutils.tools.math import Vector2

myFirstVector = Vector2(2, 3)
print(myFirstVector) # Vector2: (2, 3)

mySecondVector = Vector2(8, 10)
print(mySecondVector) # Vector2: (8, 10)

distance = Vector2.distance(myFirstVector, mySecondVector)
print(distance) # 9.219544457292887

You can also use the non-static distanceTo function to calculate the distance between itself and another Vector2. You can see an implementation below:

from cyutils.tools.math import Vector2

myFirstVector = Vector2(2, 3)
print(myFirstVector) # Vector2: (2, 3)

mySecondVector = Vector2(8, 10)
print(mySecondVector) # Vector2: (8, 10)

distance = myFirstVector.distanceTo(mySecondVector)
print(distance) # 9.219544457292887

Lerp

You can use the lerp function to lerp on the line between two different vectors. You can see an implementation below:

from cyutils.tools.math import Vector2

myFirstVector = Vector2(2, 3)
print(myFirstVector) # Vector2: (2, 3)

mySecondVector = Vector2(8, 10)
print(mySecondVector) # Vector2: (8, 10)

lerped = Vector2.lerp(myFirstVector, mySecondVector, 0.8)
print(lerped) # Vector2: (3.1999999999999997, 4.4)

Up

The up function returns the Vector (0, 1):

from cyutils.tools.math import Vector2

vector = Vector2.up()
print(vector) # Vector2: (0, 1)

Down

The down function returns the Vector (0, -1):

from cyutils.tools.math import Vector2

vector = Vector2.down()
print(vector) # Vector2: (0, -1)

Left

The left function returns the Vector (-1, 0):

from cyutils.tools.math import Vector2

vector = Vector2.left()
print(vector) # Vector2: (-1, 0)

Right

The right function returns the Vector (1, 0):

from cyutils.tools.math import Vector2

vector = Vector2.right()
print(vector) # Vector2: (1, 0)

One

The one function returns the Vector (1, 1):

from cyutils.tools.math import Vector2

vector = Vector2.one()
print(vector) # Vector2: (1, 1)

Zero

The zero function returns the Vector (0, 0):

from cyutils.tools.math import Vector2

vector = Vector2.zero()
print(vector) # Vector2: (0, 0)

Positive Infinity

The positiveInfinity function returns the Vector (infinity, infinity):

from cyutils.tools.math import Vector2

vector = Vector2.positiveInfinity()
print(vector) # Vector2: (inf, inf)

Negative Infinity

The negativeInfinity function returns the Vector (-infinity, -infinity):

from cyutils.tools.math import Vector2

vector = Vector2.negativeInfinity()
print(vector) # Vector2: (-inf, -inf)

Lists Module

The lists module allows for easier manipulation and creation of lists in python. You can import the module like this:

from cyutils.tools import lists

The lists module comes with a few functions:

Empty List

The empty list function allows you to create a multi-dimensional square list, filled with zeros with a single line of code. You can see an implementation below:

from cyutils.tools import lists

list1 = lists.empty_list(5, dimensions=1)
print(list1) # [0.0, 0.0, 0.0, 0.0, 0.0]

list2 = lists.empty_list(3, dimensions=2)
print(list2) # [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]

list3 = lists.empty_list(2, dimensions=3)
print(list3) # [[[0.0, 0.0], [0.0, 0.0]], [[0.0, 0.0], [0.0, 0.0]]]

Flatten List

The flatten list function allows you to flatten a multidimensional list into a one dimensional list. You can see an implementation below:

from cyutils.tools import lists

multidimensional_list = lists.empty_list(2, dimensions=3)
print(multidimensional_list) # [[[0.0, 0.0], [0.0, 0.0]], [[0.0, 0.0], [0.0, 0.0]]]

onedimensional_list = lists.flatten_list(multidimensional_list)
print(onedimensional_list) # [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

List Total

The list total function allows you to get the sum of all the ints or floats in a multidimensional list. You can see an implementation below:

from cyutils.tools import lists

myList = lists.empty_list(2, dimensions=3)
myList[0][1][1] = 4
myList[1][0][1] = 3
print(myList) # [[[0.0, 0.0], [0.0, 4]], [[0.0, 3], [0.0, 0.0]]]

total = lists.list_total(myList)
print(total) # 7.0

Cipher Module

The cipher module allows you to easily manipulate ciphers in python. CyUtils currently only supports two ciphers:

Ceasar Cipher

You can import the ceasar cipher module like this:

from cyutils.tools.cipher import CeasarCipher

With the module imported, the ceasar cipher can be created like a variable:

from cyutils.tools.cipher import CeasarCipher

myCipher = CeasarCipher(5)
print(myCipher) # Ceasar Cipher with shift: 5

Now, you can use the encode and decode functions to use the ceasar cipher with the shift set above:

from cyutils.tools.cipher import CeasarCipher

myCipher = CeasarCipher(5)
print(myCipher) # Ceasar Cipher with shift: 5

encoded = myCipher.encode("helloworld")
print(encoded) # mjqqtbtwqi

decoded = myCipher.decode(encoded)
print(decoded) # helloworld

You can also use the sysencode function to encode it by using a custom key:

from cyutils.tools.cipher import CeasarCipher

encoded = CeasarCipher.sysencode("helloworld", 5)
print(encoded) # mjqqtbtwqi

Vignere Cipher

You can import the vignere cipher like this:

from cyutils.tools.cipher import VignereCipher

With the module imported, the vignere cipher can be created like a variable:

from cyutils.tools.cipher import VignereCipher

myCipher = VignereCipher("KEY")
print(myCipher) # Vignere Cipher with key: KEY

Now, you can use the encode and decode functions to use the vignere cipher with the key set above:

from cyutils.tools.cipher import VignereCipher

myCipher = VignereCipher("KEY")
print(myCipher) # Vignere Cipher with key: KEY

encoded = myCipher.encode("HELLOWORLD")
print(encoded) # RIJVSUYVJN

decoded = myCipher.decode(encoded)
print(decoded) # HELLOWORLD

cyutils's People

Contributors

cy4shot avatar

Stargazers

kelly avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.