analitics

Pages

Monday, February 27, 2023

Python 3.11.0 : EbookLib python library - part 002.

The last version 3.3 of this file type was published by the EPUB 3 Working Group as a Candidate Recommendation Snapshot on 21 February 2023 ...
I haven't written about the ePub format and python since 2022 because I was busy with all kinds of tasks.
Then I presented a short introduction with some examples on how to create an ePUB file, see that tutorial article.
I like this file type, especially the last format that allows many features, including inserting SVG vector files.
Installing the package is easy on a windows 10 with the pip utility.
pip install ebooklib --user
In the folder with the svg image named cover.svg I created a python file with this content.
from ebooklib import epub

# Create a new EPUB book
book = epub.EpubBook()

# Set the book metadata
book.set_title('My EPUB Book')
book.set_language('en')

# Add a cover image
cover_svg = epub.EpubItem(uid="cover_svg", file_name="cover.svg", media_type="image/svg+xml", content=open("cover.svg").read())
book.add_item(cover_svg)

# Create a new chapter for the cover page
cover_page = epub.EpubHtml(title='Cover Page', file_name='cover.xhtml', lang='en')
cover_page.content = '<img alt="Cover Image" src="cover.svg" />'

# Add the cover page to the book
book.add_item(cover_page)

# Add a chapter to the book
chapter1 = epub.EpubHtml(title='Chapter 1', file_name='chap_1.xhtml', lang='en')
chapter1.content = '<h1>Chapter 1</h1><p>This is the first chapter of my book.</p>'

# Add the chapter to the book
book.add_item(chapter1)

# Create the spine
book.spine = [cover_page, chapter1]

# Generate the EPUB file
epub.write_epub('my_book.epub', book, {})
after running the script this was the result a epub file named my_book.epub.
The result is this simple epub with an SVG file from Wikipedia, see the screenshot take with the Sumatra PDF free software.

Saturday, February 25, 2023

Python 3.8.10 : My colab tutorials - part 031.

I update my source code for the collaboratory google tool. This colab notebook comes with source code for an update of IPython, PyTikTokAPI python module using the cookies browser, and a simple OpenCV test to create an image and show with google.colab.patches.
The cookies for the TikTok package module can be found in any browser by pressing the F12 key.
I don't find a good python package that parse the tiktok content.
The application for development with TikTok looks like Instagram and Facebook ... you can test it at https://developers.tiktok.com/app/.

Sunday, February 19, 2023

Tuesday, February 14, 2023

News : Colab changes ...

Python 2 is no longer supported in Colab and cells runs iron python.
All information you need on migrating your code from Python 2 to Python 3 can be found on Porting Python 2 Code to Python 3.
You can see these changes on the GitHub project.

Thursday, February 9, 2023

Python 3.6.9 : Anvil platform for web with python - part 001.

Anvil is a platform for building and hosting full-stack web apps written entirely in Python. Drag and drop your UI, then write Python on the front-end and back-end to make it all work ...
Yesterday I tested Anvil with python and was amazed at how simple it can be used to develop a web application. Anvil use python version 3.6.9. The tutorial I tested is available on the official website and is a feedback form that sends an email with your name, email address and content to your Anvil account address.
You can see this web application I follow from web tutorials on this webpage.
You can find some tutorials on this webpage.
How it works: Create a free account that can also be paid for via a web interface that you build with drag and drop and populate interface properties, add defined python modules and services, and write source code. I noticed that it uses beginner level python decorators and not very complex source code. The good part is that it is a fast development and the bad part is that it is limited to the free tier. After only three emails received, they came back with messages saying: ... his email quota has been exceeded.
These are features for anvil, I used in my project just the email:

Saturday, February 4, 2023

Python 3.7.0 : CodeSkulptor online compiler and editor.

CodeSkulptor uses Skulpt to provide a browser-based coding environment and can be tested on the official website.
You can see an online default example with simplegui python package on that website with online editor.

Saturday, January 28, 2023

Python : Fix error with pip and pylupdate6.exe .

I try to upgrade the PyQt6 package and I got this error:

pip3 install --upgrade --force-reinstall PyQt6
...
ERROR: Could not install packages due to an OSError: [WinError 2] The system cannot find the file specified:
...pylupdate6.exe
...pylupdate6.exe.deleteme'
I think this solution will work for any python package
Open a command shell run as administrator and run it again:
pip3 install --upgrade --force-reinstall PyQt6
Collecting PyQt6
...
Successfully installed PyQt6-6.4.1 PyQt6-Qt6-6.4.2 PyQt6-sip-13.4.1
If you want to re-download the packages instead of using the files from your pip cache, then use:
pip install --force-reinstall --no-cache-dir

Tuesday, January 24, 2023

Python 3.8.10 : My colab tutorials - part 029.

Colab from google is a great tool when you want to write equations. I show a simple example with LaTeX formula with this colab notebook named catafest_034.

Python : Fix DLL load failed while importing ...

This error DLL load failed while importing ... can have many causes like conflicts with the already installed packages, and also it can break your current environment.
>>> import PyQt6
>>> from PyQt6.QtCore import QUrl
Traceback (most recent call last):
  ...
ImportError: DLL load failed while importing QtCore: The specified module could not be found.
You can see I used to reinstall the PyQt6 python package with this argument --ignore-installed:
pip3 install PyQt6 --user --ignore-installed
Collecting PyQt6
  ...
Installing collected packages: PyQt6-Qt6, PyQt6-sip, PyQt6
  WARNING: The scripts pylupdate6.exe and pyuic6.exe are installed in
  ...
  which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed PyQt6-6.4.0 PyQt6-Qt6-6.4.2 PyQt6-sip-13.4.0
The --ignore-installed option for the pip package manager was first introduced in version 6.0, which was released in April 2014.
The old install give me this error and when I try to use I got this:
>>> from PyQt6 import *
>>> dir(PyQt6)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']
Now after I reinstall with this option the result is good:
>>> import PyQt6
>>> from PyQt6 import QtCore
>>> dir(PyQt6)
['QtCore', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'sip']
>>> from PyQt6.QtCore import QUrl

Python 3.11.0 : The openai python package - part 001.

The OpenAI Python library provides convenient access to the OpenAI API from applications written in the Python language. It includes a pre-defined set of classes for API resources that initialize themselves dynamically from API responses which makes it compatible with a wide range of versions of the OpenAI API.
You can see more about this on the GitHub project.
in order to use this package you need to have a A.P.I. key for OpenAI beta features from this website.
Create a key and copy into file or clipboard beacuse this cannot be accesed after you created.
Use the pip tool to install the openai package:
pip3 install openai --user
I tested with a default example and a simple question: What is the python programmin language?
import os
import openai


openai.api_key = "your_API_OpenAI_key"

response = openai.Completion.create(
    model="text-davinci-003",
    prompt="What is the python programmin language?",
    temperature=0.7,
    max_tokens=100,
    top_p=1,
    frequency_penalty=0,
    presence_penalty=0
)

print(response)
I run this python script code and the result is this:
python openai001.py
{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "logprobs": null,
      "text": "\n\nPython is a high-level, interpreted, general-purpose programming language. 
      It was created by Guido van Rossum and first released in 1991. Python has a wide variety of 
      uses and is a popular language for data science, web development, automation, and artificial 
      intelligence. It is also a popular language for scripting and general-purpose programming."
    }
  ],
  "created": 1674588982,
  "id": "cmpl-6cJNeACMtayBlExV1GJpilde0KcBN",
  "model": "text-davinci-003",
  "object": "text_completion",
  "usage": {
    "completion_tokens": 71,
    "prompt_tokens": 8,
    "total_tokens": 79
  }
}

Monday, January 23, 2023

Python 3.11.0 : About py launcher tool for Windows.

The py launcher tool for Windows is included in the Python for Windows installer starting from version 3.3.
This command-line tool allows you to easily configure and switch between multiple versions of Python installed on a Windows system.
This tool allows you to easily switch between different versions of Python without modifying the system's PATH environment variable, and it also allows you to set default versions of Python for different file extensions.
Let's see some examples
This will argument will give you the current version of your python command:
py --version
Python 3.11.0
This will enumerate all your python versions from Windows O.S.:
py -0
 -V:3.11 *        Python 3.11 (64-bit)
 -V:3.10          Python 3.10 (64-bit)
 -V:3.9           Python 3.9 (64-bit)
 -V:3.7           Python 3.7 (64-bit)
This will set the python command to a specific version in this case 3.10 :
>py -3.10
Python 3.10.7 (tags/v3.10.7:6cc6b13, Sep  5 2022, 14:08:36) [MSC v.1933 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
This will run a python script named scripting001.py with a specific version of python:
py -3.10 scripting001.py

Thursday, January 19, 2023

Python 3.11.0 : The parfive python package.

Today I tested this simple parfive 2.0.2 version python package with the default example from the official website.
A parallel file downloader using asyncio. parfive can handle downloading multiple files in parallel as well as downloading each file in a number of chunks.
The default example works great but when I try to get direct movie files from free web this not work.
Parfive is a lightweight library, I try to download large files but give me error 206.

Thursday, January 12, 2023

Python 3.11.0 : The numpy-quaternion python package - part 001.

I write an article on my website about artificial intelligence and I used python to show a simple example with quaternions.
This Python module adds a quaternion dtype to NumPy and you can read about this on the official website.
I may have mistakenly installed a python packet with a similar name and I had to install it with the command:
python -m pip uninstall quaternion
the next step was to install this command
python -m pip install --upgrade --no-deps --force-reinstall numpy-quaternion
The source code I used defines two quaternions, one with real part a and imaginary parts, and one quaternion using Euler angles.
Then is perform the rotation uses quaternion multiplication.
Let's see the source code
import numpy as np
import quaternion

# define a quaternion with real part a and imaginary parts bi, cj, dk
a = 1
b = 2
c = 3
d = 4
q = np.quaternion(a, b, c, d)

# define a quaternion using euler angles
x = 1.0
y = 2.0
z = 3.0
q2 = quaternion.from_euler_angles(x, y, z)

# define a vector to rotate
v = [1, 0, 0]

# perform the rotation using quaternion multiplication

# quaternion multiplication is not commutative, the order matters
# because this line of source code will not work:  rotated_v = q2 * v * q2.conj()

rotated_v = (q2 * quaternion.quaternion(0, *v)) * q2.conj()

print(rotated_v)
This is the result:
quaternion(0, 0.103846565151668, 0.422918571742548, 0.900197629735517)