Carleslibs

This is the main page for carleslibs Open Source Python package.

https://pypi.org/project/carleslibs/

In order to install use:

pip3 install carleslibs

In case you want to install to a Virtual Environment, activate it first.

For Ubuntu is done like this:

source /home/carles/Desktop/code/my-awesome-project/venv/bin/activate

If you want to upgrade to the latest version:

/home/carles/Desktop/code/my-awesome-project/venv/bin/pip3 install --upgrade carleslibs

Version 1.0.1 was released on 2021-08-11 wit the addition of:

  • SubProcessUtils

Allows to execute commands and get Exit Code, StdOut and StdErr.

Version 0.99.2 was released on 2021-08-27 with the addition of:

  • StringUtils

Version 0.99.1 was released on 2021-07-12 with:

  • FileUtils
  • DateTimeUtils
  • PythonUtils

The source code is available at:

https://gitlab.com/carles.mateo/carleslibs

I released it under the MIT License.

In order to use them in your project, just install them with pip, or with PyCharm if you’re using a Virtual Environment there.

PyCharm > Settings > Project > Python Interpreter

In order to use in your program import the classes you want like this:

from carleslibs.fileutils import FileUtils
from carleslibs.datetimeutils import DateTimeUtils
from carleslibs.pythonutils import PythonUtils
from carleslibs.stringutils import StringUtils

DateTimeUtils class

And use in your program:

from carleslibs.datetimeutils import DateTimeUtils

o_datetime = DateTimeUtils()
print(o_datetime.get_datetime_as_14_string())
print(o_datetime.get_unix_epoch())
print(o_datetime.get_datetime())
print(o_datetime.get_unix_epoch_as_float())

This will output:

20210713203940
1626205180
2021-07-13 20:39:40
1626205180.05574

PythonUtils class

from carleslibs.pythonutils import PythonUtils

o_python = PythonUtils()

s_version, i_major, i_minor, i_micro, s_releaselevel = o_python.get_python_version()
print("Your Python version is: " + s_version)

if o_python.is_python_2() is True:
    print("You're running on the obsolete Python 2")

if o_python.is_python_3() is True:
    print("Cool, you're running in Python 3")

In my development environment will output:

Your Python version is: 3.8.10
Cool, you're running in Python 3

FileUtils class

from carleslibs.fileutils import FileUtils

o_fileutils = FileUtils()
s_testfile = "/tmp/" + o_datetime.get_datetime(b_milliseconds=False, b_remove_spaces_and_colons=True, b_remove_dashes=False) + "-test.txt"

if o_fileutils.file_exists(s_testfile) is True:
    print("A previous file named " + s_testfile + " exists. Cancelling.")
else:
    b_success = o_fileutils.write(s_testfile, "This is a test\n")
    if b_success is False:
        print("Error writing to: " + s_testfile)

    b_success = o_fileutils.append(s_testfile, "This is another test")
    if b_success is False:
        print("Error appending to: " + s_testfile)

    b_success, s_contents = o_fileutils.read_file_as_string(s_testfile)
    if b_success is False:
        print("Error reading from: " + s_testfile)
    else:
        print("The file " + s_testfile + " contains:")
        print(s_contents)

    b_success, a_lines = o_fileutils.read_file_as_lines(s_testfile)
    if b_success is False:
        print("Error reading from: " + s_testfile)
    else:
        print("The file " + s_testfile + " contains:")
        i_line = 1
        for s_line in a_lines:
            print("Line " + str(i_line) + " contains: " + s_line)
            i_line = i_line + 1

    b_success = o_fileutils.delete(s_file=s_testfile)
    if b_success is True:
        print("File " + s_testfile + " successfully deleted")
    else:
        print("There was a problem deleting file: " + s_testfile)

This will output:

The file /tmp/2021-07-13-210039-test.txt contains:
This is a test
This is another test

The file /tmp/2021-07-13-210039-test.txt contains:
Line 1 contains: This is a test
Line 2 contains: This is another test

File /tmp/2021-07-13-210039-test.txt successfully deleted

StringUtils class

This class provides several methods for unit conversion, string formatting, getting speeds…

It is used in my Open Source CTOP project.

Views: 578 views

Rules for writing a Comment


  1. Comments are moderated
  2. I don't publish Spam
  3. Comments with a fake email are not published
  4. Disrespectful comment are not published, even if they have a valid point
  5. Please try to read all the article before asking, as in many cases questions are already responded