Fixed the colab notebook for test models from NVIDIA for waveglow and tacotron2.
Is a blog about python programming language. You can see my work with python programming language, tutorials and news.
Showing posts with label os. Show all posts
Showing posts with label os. Show all posts
Sunday, April 13, 2025
Friday, April 11, 2025
Python 3.13.0rc1 : pig for AI-powered automation.
Pig is an API for AI-powered automation of Windows desktop applications and workflows. It allows AI agents to remotely navigate user interfaces and interact with software on Windows computers, even for applications without public APIs. Organizations might use Pig to automate complex, repetitive tasks involving legacy enterprise software, overcome limitations of traditional robotic process automation tools, and reduce the need for manual data handling by human workers.
Follow the install from the official website.
pip install pig-python
Collecting pig-python
...
Successfully installed aiohttp-retry-2.9.1 click-8.1.8 iso8601-2.1.0 pig-python-0.1.2 simple-term-menu-1.6.6 tabulate-0.9.0
From the API Keys url get the key and run a server:
piglet join --secret the_API_key
Display dimensions: 1280x720
Local server running at http://localhost:3000
Connected to control server
The local machine can be found on this webpage.
I go to machine chat and I ask to create a source script in python for test the artificial intelligence.
It looks like you're encountering an error with the Pig API. The error message indicates that the API key hasn't been set properly.
To resolve this issue, you need to set the PIG_SECRET_KEY environment variable or pass the API key directly to the Client constructor when initializing the Pig client.
Would you like me to help you with setting up the environment variable or modifying your code to include the API key in the Client constructor?
yes , I need a python script example
The remote artificial intelligence machine move the mouse and open notepad, add source code and save to my desktop.
I add my API key and I run it, see the result:

This source script is the result of artificial intelligence processing
"""
Example script showing how to properly initialize the Pig client with an API key
using both environment variable and direct constructor methods.
"""
import os
from pig import Client
# Example API key (replace with your actual API key in production)
API_KEY = "SK-..."
def initialize_with_env_variable():
"""
Method 1: Initialize Pig client using environment variable
This is the recommended approach for production environments
"""
# Set the environment variable
os.environ["PIG_SECRET_KEY"] = API_KEY
# Initialize the client (it will automatically use the environment variable)
client = Client()
print("Client initialized using environment variable")
return client
def initialize_with_direct_key():
"""
Method 2: Initialize Pig client by passing the API key directly
Useful for testing or when environment variables are not preferred
"""
# Initialize the client by passing the API key directly to the constructor
client = Client(api_key=API_KEY)
print("Client initialized by passing API key directly")
return client
if __name__ == "__main__":
# Example 1: Using environment variable
client1 = initialize_with_env_variable()
# Example 2: Passing API key directly
client2 = initialize_with_direct_key()
# Both methods achieve the same result
print("Both initialization methods are complete!")
Posted by
Cătălin George Feștilă
Labels:
2025,
artificial intelligence,
os,
packages,
pig,
python,
python 3,
python packages,
tutorial,
tutorials
Saturday, March 1, 2025
Python 3.13.0rc1 : testing the elevenlabs with artificial intelligence.
Today I teste the elevenlabs python package to use it with artifical inteligence to create sound.
I install this python package with pip tool, I created a python script file and the basic script run well with the api key from the official website.
pip install elevenlabs
Collecting elevenlabs
...
Installing collected packages: websockets, sniffio, pydantic-core, h11, annotated-types, pydantic, httpcore, anyio, httpx,
elevenlabs
Successfully installed annotated-types-0.7.0 anyio-4.8.0 elevenlabs-1.52.0 h11-0.14.0 httpcore-1.0.7 httpx-0.28.1
pydantic-2.10.6the official website pydantic-core-2.27.2 sniffio-1.3.1 websockets-15.0
...
pip install playsound
Collecting playsound
...
Installing collected packages: playsound
Successfully installed playsound-1.3.0
...
python elevenlabs_test_001.py
Fișierul audio a fost salvat la generated_audio.mp3
This is the source code:
import io # Importarea bibliotecii io
from elevenlabs import ElevenLabs
from playsound import playsound
import tempfile
import os
# API Key pentru ElevenLabs
api_key = "API_KEY"
voice_id = "JBFqnCBsd6RMkjVDRZzb"
# Configurarea clientului ElevenLabs
client = ElevenLabs(api_key=api_key )
# Textul pe care vrei să-l convertești în audio
text = 'Hello! This is a test without mpv.'
# Generarea audio
audio_generator = client.generate(text=text, voice=voice_id)
# Colectarea datelor din generator într-un obiect BytesIO
audio_data = io.BytesIO()
for chunk in audio_generator:
audio_data.write(chunk)
audio_data.seek(0) # Resetarea pointerului la începutul streamului
# Specificarea căii de salvare pentru fișierul audio
save_path = 'generated_audio.mp3'
# Salvarea audio într-un fișier temporar
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') as temp_audio:
temp_audio.write(audio_data.read())
temp_audio_path = temp_audio.name
# Redarea fișierului audio utilizând playsound
playsound(temp_audio_path)
# Salvarea fișierului audio generat într-o locație specificată
with open(save_path, 'wb') as f:
audio_data.seek(0) # Resetarea pointerului la începutul streamului pentru a citi din nou datele
f.write(audio_data.read())
print(f'Fișierul audio a fost salvat la {save_path}')
Posted by
Cătălin George Feștilă
Labels:
2025,
artificial intelligence,
elevenlabs,
io,
os,
packages,
playsound,
PyQt6,
python,
python 3,
python packages,
tempfile,
tutorial,
tutorials
Saturday, February 8, 2025
Python 3.13.0rc1 : Testing python with Ollama local install.
I was very busy with development and testing for about two weeks and my laptop was stuck and I was working hard... Today I managed to test local background clipping on my laptop with a local Ollama installation separated by a Python module but with processing from the Python script. I also used Microsoft's Copilot artificial intelligence for python and it works well even though it is not theoretically specialized in development. The source code is quite large but the result is very good and fast:
import subprocess
import os
import json
from PIL import Image, ImageOps
class OllamaProcessor:
def __init__(self, config_file):
self.config_file = config_file
self.model_methods = self.load_config()
def load_config(self):
try:
with open(self.config_file, 'r') as file:
config = json.load(file)
print("Configuration loaded successfully.")
return config
except FileNotFoundError:
print(f"Configuration file {self.config_file} not found.")
raise
except json.JSONDecodeError:
print(f"Error decoding JSON from the configuration file {self.config_file}.")
raise
def check_ollama(self):
try:
result = subprocess.run(["ollama", "--version"], capture_output=True, text=True, check=True)
print("Ollama is installed. Version:", result.stdout)
except subprocess.CalledProcessError as e:
print("Ollama is not installed or not found in PATH. Ensure it's installed and accessible.")
raise
...
Here is the result obtained after finishing running in the command line:
python ollama_test_001.py
Configuration file ollama_config.json created successfully.
Configuration loaded successfully.
Ollama is installed. Version: ollama version is 0.5.7
Available models: ['NAME']
pulling manifest
pulling 170370233dd5... 100% ▕██████████████▏ 4.1 GB
pulling 72d6f08a42f6... 100% ▕██████████████▏ 624 MB
pulling 43070e2d4e53... 100% ▕██████████████▏ 11 KB
pulling c43332387573... 100% ▕██████████████▏ 67 B
pulling ed11eda7790d... 100% ▕██████████████▏ 30 B
pulling 7c658f9561e5... 100% ▕██████████████▏ 564 B
verifying sha256 digest
writing manifest
success
Model llava pulled successfully for method process_images_in_folder.
Some "Command failed ..." but the result is cutting well and it has transparency !
Posted by
Cătălin George Feștilă
Labels:
2025,
artificial intelligence,
copilot,
json,
module,
modules,
ollama,
os,
PIL,
python,
python 3,
python modules,
tutorial,
tutorials,
windows 10
Thursday, January 30, 2025
Blender 3D and python scripting - part 032.
Today I created an addon for Blender version 4.3.2 that allows me to select two folders to render 3D objects from the first folder and add 512px samples with these renderings to the second folder.
This is what the addon installed in Blender 3D looks like:

Here's what the source code of this addon looks like:
bl_info = {
"name": "3D File Renderer by catafest",
"blender": (4, 3, 2),
"category": "Object",
"author": "Catalin George Festila\n"
"nicknames: catafest and mythcat\n"
"country: Romania\n"
"mail: catafest [at] yahoo.com",
"version": (1, 0),
"blender": (2, 80, 0),
"location": "View3D > UI > 3D File Renderer",
"description": "Addon for rendering 3D files",
"warning": "",
"doc_url": "https://github.com/catafest",
"tracker_url": "https://github.com/catafest/issues",
"support": "COMMUNITY",
}
import bpy
import os
class FileRendererProperties(bpy.types.PropertyGroup):
input_directory: bpy.props.StringProperty(
name="Input Directory",
description="Directory containing 3D files",
default="",
maxlen=1024,
subtype='DIR_PATH'
)
output_directory: bpy.props.StringProperty(
name="Output Directory",
description="Directory to save rendered images",
default="",
maxlen=1024,
subtype='DIR_PATH'
)
class RENDER_OT_files(bpy.types.Operator):
bl_idname = "render.files"
bl_label = "Start render 3D files for all files"
def execute(self, context):
input_directory = context.scene.file_renderer_props.input_directory
output_directory = context.scene.file_renderer_props.output_directory
if not input_directory or not output_directory:
self.report({'ERROR'}, "Input and Output directories must be set.")
return {'CANCELLED'}
if not os.path.exists(output_directory):
os.makedirs(output_directory)
def render_file(file_path, output_path):
try:
bpy.ops.wm.read_factory_settings(use_empty=True)
ext = os.path.splitext(file_path)[1].lower()
if ext == ".glb":
bpy.ops.import_scene.gltf(filepath=file_path)
elif ext == ".obj":
bpy.ops.import_scene.obj(filepath=file_path)
elif ext == ".fbx":
bpy.ops.import_scene.fbx(filepath=file_path)
else:
raise ValueError("Unsupported file format")
bpy.ops.object.camera_add(location=(0, -3, 1.5), rotation=(1.1, 0, 0))
camera = bpy.context.scene.objects['Camera']
bpy.context.scene.camera = camera
bpy.ops.object.light_add(type='POINT', location=(0, -3, 3))
light = bpy.context.view_layer.objects.active
light.data.energy = 1000
bpy.context.scene.render.resolution_x = 512
bpy.context.scene.render.resolution_y = 512
bpy.context.scene.render.filepath = output_path
bpy.ops.render.render(write_still=True)
except Exception as e:
# Generate a red image with "BAD FILE" text using Blender
bpy.ops.wm.read_factory_settings(use_empty=True)
bpy.ops.mesh.primitive_plane_add(size=2)
plane = bpy.context.active_object
mat = bpy.data.materials.new(name="BadFileMaterial")
mat.diffuse_color = (1, 0, 0, 1) # Red
plane.data.materials.append(mat)
# Add "BAD FILE" text
bpy.ops.object.text_add(location=(0, 0, 0.1))
text_obj = bpy.context.active_object
text_obj.data.body = "BAD FILE"
text_obj.data.size = 0.5
text_obj.data.align_x = 'CENTER'
text_obj.data.align_y = 'CENTER'
text_obj.rotation_euler = (1.5708, 0, 0)
# Set camera and light
bpy.ops.object.camera_add(location=(0, -3, 1.5), rotation=(1.1, 0, 0))
camera = bpy.context.scene.objects['Camera']
bpy.context.scene.camera = camera
bpy.ops.object.light_add(type='POINT', location=(0, -3, 3))
light = bpy.context.view_layer.objects.active
light.data.energy = 1000
bpy.context.scene.render.resolution_x = 512
bpy.context.scene.render.resolution_y = 512
bpy.context.scene.render.filepath = output_path
bpy.ops.render.render(write_still=True)
for filename in os.listdir(input_directory):
if filename.lower().endswith((".glb", ".obj", ".fbx")):
file_path = os.path.join(input_directory, filename)
output_path = os.path.join(output_directory, os.path.splitext(filename)[0] + ".png")
render_file(file_path, output_path)
self.report({'INFO'}, "Rendering of files is complete.")
return {'FINISHED'}
class ABOUT_OT_dialog(bpy.types.Operator):
bl_idname = "wm.about_dialog"
bl_label = "About this addon"
def execute(self, context):
return context.window_manager.invoke_props_dialog(self)
def draw(self, context):
layout = self.layout
layout.label(text="3D File Renderer by catafest")
layout.label(text="Author: Catalin George Festila")
layout.label(text="Nicknames: catafest and mythcat")
layout.label(text="Country: Romania")
layout.label(text="Email: catafest [at] yahoo.com")
layout.operator("wm.url_open", text="LinkedIn").url = "https://www.linkedin.com/in/c%C4%83t%C4%83lin-george-fe%C8%99til%C4%83-05780a67"
layout.operator("wm.url_open", text="Author Site").url = "https://sites.google.com/view/festila-george-catalin"
layout.operator("wm.url_open", text="catafest GitHub").url = "https://github.com/catafest"
layout.operator("wm.url_open", text="catafest-work GitHub").url = "https://github.com/catafest-work"
class FileRendererPanel(bpy.types.Panel):
bl_label = "3D File Renderer by catafest"
bl_idname = "OBJECT_PT_file_renderer"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'File Renderer'
def draw(self, context):
layout = self.layout
scene = context.scene
file_renderer_props = scene.file_renderer_props
layout.prop(file_renderer_props, "input_directory")
layout.prop(file_renderer_props, "output_directory")
# Styling the render button
render_button = layout.operator("render.files", text="Start render 3D files for all files")
layout.separator()
layout.operator("wm.about_dialog", text="About this addon")
def register():
bpy.utils.register_class(FileRendererProperties)
bpy.utils.register_class(RENDER_OT_files)
bpy.utils.register_class(ABOUT_OT_dialog)
bpy.utils.register_class(FileRendererPanel)
bpy.types.Scene.file_renderer_props = bpy.props.PointerProperty(type=FileRendererProperties)
def unregister():
bpy.utils.unregister_class(FileRendererProperties)
bpy.utils.unregister_class(RENDER_OT_files)
bpy.utils.unregister_class(ABOUT_OT_dialog)
bpy.utils.unregister_class(FileRendererPanel)
del bpy.types.Scene.file_renderer_props
if __name__ == "__main__":
register()
Posted by
Cătălin George Feștilă
Labels:
2025,
3d,
Blender 3D,
bpy,
module,
modules,
os,
packages,
python,
python 3,
python modules,
python packages,
script,
tool
Tuesday, January 7, 2025
Python 3.13.0rc1 : Simple convert all webp files from folder.
Today, a simple script is used to convert WEBP to PNG files from a defined folder.
The script reads a folder path for each WEBP file that is opened and saved as a PNG file.
import os
import sys
from PIL import Image
def convert_webp_to_png(directory):
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith(".webp"):
webp_path = os.path.join(root, file)
png_path = os.path.splitext(webp_path)[0] + ".png"
with Image.open(webp_path) as img:
img.save(png_path, "PNG")
print(f"webp to png file: {webp_path} -> {png_path}")
if __name__ == "__main__":
if len(sys.argv) != 2:
print("How to use: python convert.py path_to_folder_with_webp_files")
sys.exit(1)
directory = sys.argv[1]
if not os.path.isdir(directory):
print(f"{directory} folder is not valid.")
sys.exit(1)
convert_webp_to_png(directory)
print("Finished !")
Posted by
Cătălin George Feștilă
Labels:
2025,
module,
modules,
os,
packages,
PIL,
python,
python 3,
python modules,
python packages,
tutorial,
tutorials,
windows 10
Sunday, November 24, 2024
Python 3.13.0 : emoji symbols with PIL.
Today I want to use emoji symbols and I wrote this python script:
from PIL import Image, ImageDraw, ImageFont
import os
# Font size and image dimensions
font_size = 88
width = 640
height = 480
# Use Symbola.ttf from current directory
font_path = "Symbola.ttf"
# Create image
img = Image.new('RGB', (width, height), color='white')
draw = ImageDraw.Draw(img)
# Get font
font = ImageFont.truetype(font_path, font_size)
# Emoji matrix
emoji_matrix = [
['😀', '😁', '😂', '🤣', '😃'],
['😄', '😅', '😆', '😇', '😈'],
['😉', '😊', '😋', '😌', '😍'],
['😎', '😏', '😐', '😑', '😒']
]
# Calculate spacing
x_spacing = font_size + 10
y_spacing = font_size + 10
# Calculate starting position to center the grid
start_x = (width - (len(emoji_matrix[0]) * x_spacing)) // 2
start_y = (height - (len(emoji_matrix) * y_spacing)) // 2
# Draw emojis
for i, row in enumerate(emoji_matrix):
for j, emoji in enumerate(row):
x = start_x + (j * x_spacing)
y = start_y + (i * y_spacing)
draw.text((x, y), emoji, font=font, fill='black')
# Save the image
img.save('emoji_art.png')
print("Emoji art has been created successfully! Check emoji_art.png")
The result image named emoji_art.png is this:

Wednesday, March 29, 2023
Python : Open3D cannot be used on Windows 10 and Fedora Linux Distro .
Open3D is an open-source library that supports rapid development of software that deals with 3D data. The Open3D frontend exposes a set of carefully selected data structures and algorithms in both C++ and Python. The backend is highly optimized and is set up for parallelization. Open3D was developed from a clean slate with a small and carefully considered set of dependencies. It can be set up on different platforms and compiled from source with minimal effort. The code is clean, consistently styled, and maintained via a clear code review mechanism. Open3D has been used in a number of published research projects and is actively deployed in the cloud. We welcome contributions from the open-source community.
Today I tested this python package with Windows 10 and Fedora Linux Distro with python versions 11 and 10 ...
This package does not work and you will see why ...
C:\PythonProjects\Open3D001>git clone https://github.com/isl-org/Open3D.git
Cloning into 'Open3D'...
remote: Enumerating objects: 67435, done.
remote: Counting objects: 100% (2280/2280), done.
remote: Compressing objects: 100% (1894/1894), done.
remote: Total 67435 (delta 886), reused 599 (delta 385), pack-reused 65155
Receiving objects: 100% (67435/67435), 237.23 MiB | 17.11 MiB/s, done.
Resolving deltas: 100% (50682/50682), done.
Updating files: 100% (2315/2315), done.
C:\PythonProjects\Open3D001>cd Open3D
C:\PythonProjects\Open3D001\Open3D>mkdir build
C:\PythonProjects\Open3D001\Open3D>cd build
C:\PythonProjects\Open3D001\Open3D\build>cmake -DBUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX=C:\open3d_install ..
-- Building for: Visual Studio 17 2022
-- Setting build type to Release as none was specified.
-- CMAKE_BUILD_TYPE is set to Release.
-- Downloading third-party dependencies to C:/PythonProjects/Open3D001/Open3D/3rdparty_downloads
CMake Deprecation Warning at CMakeLists.txt:189 (cmake_policy):
The OLD behavior for policy CMP0072 will be removed from a future version
of CMake.
The cmake-policies(7) manual explains that the OLD behaviors of all
policies are deprecated and that a policy should be set to OLD only under
specific short-term circumstances. Projects should be ported to the NEW
behavior and not rely on setting a policy to OLD.
...
According to this issue number 4796 and my test on Windows 10 with a Python version greater than 10 and on Fedora Linux Distro you cannot use this python package.
You can try an older version of Python and try it, see this example:
C:\PythonProjects\Open3D001>C:\Python310\python.exe -m pip install --user open3d --no-warn-script-location
C:\PythonProjects\Open3D001>C:\Python310\python.exe -c "import open3d as o3d; print(o3d)"
Traceback (most recent call last):
...
from open3d.cpu.pybind import (core, camera, data, geometry, io, pipelines,
ImportError: DLL load failed while importing pybind: A dynamic link library (DLL) initialization routine failed.
...
pip install pybind --user
Collecting pybind
Using cached pybind-0.1.35.tar.gz (15.5 MB)
ERROR: Could not install packages due to an OSError: [WinError 206] The filename or extension is too
long: 'C:\\Users\\catafest\\AppData\\Local\\Temp\\pip-install-7ccpzu3z\\pybind_
...
Basically, this python package cannot be used with an old python version in Windows 10.
Posted by
Cătălin George Feștilă
Labels:
2023,
fedora,
linux,
module,
modules,
Open3D,
os,
packages,
python,
python 3,
python modules,
python packages,
tutorial,
tutorials,
windows,
windows 10,
winodws 10
Sunday, March 26, 2023
Python 3.11.0 : Image generation with OpenAI.
In this tutorial I will show you a python script with PyQt6 and OpenAI that generates an image based on OpenAI token keys and a text that describes the image.
The script is quite simple and requires the installation of python packets: PyQt6,openai.
In the script you can find a python class called MainWindow in which graphic user interface elements are included and openai elements for generating images.
You also need a token key from the official openai page to use for generation.
The script runs with the command python numa_script.py and in the two editboxes is inserted chaie from token API OpenAI and the text that will describe the image to be generated.
This is the python script with the source code:
#create_image.py
import os
import openai
from PyQt6.QtCore import Qt, QSize
from PyQt6.QtGui import QImage, QPixmap
from PyQt6.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QVBoxLayout, QHBoxLayout, QPushButton
import requests
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.resize(500, 500)
self.setWindowTitle("AI Data Input")
# create widgets
self.image_label = QLabel(self)
self.image_label.setFixedSize(QSize(300, 300))
self.url_edit = QLineEdit(self)
self.api_key = QLineEdit(self)
self.send_button = QPushButton("Send data to AI", self)
self.send_button.clicked.connect(self.on_send_button_clicked)
# create layout
layout = QVBoxLayout()
url_layout = QHBoxLayout()
url_layout.addWidget(QLabel("Text request AI: "))
url_layout.addWidget(self.url_edit)
api_layout = QHBoxLayout()
api_layout.addWidget(QLabel("OpenAI API Key: "))
api_layout.addWidget(self.api_key)
layout.addLayout(url_layout)
layout.addLayout(api_layout)
layout.addWidget(self.image_label, alignment=Qt.AlignmentFlag.AlignCenter)
layout.addWidget(self.send_button, alignment=Qt.AlignmentFlag.AlignCenter)
self.setLayout(layout)
def on_send_button_clicked(self):
#openai.api_key = "your api key generated by OpenAI API"
openai.api_key = self.api_key.text()
PROMPT = self.url_edit.text()
url = openai.Image.create(
prompt=PROMPT,
n=1,
size="256x256",
)
# extract the url value
url_value = url['data'][0]['url']
if url_value :
response = requests.get(url_value)
if response.status_code == 200:
image = QImage.fromData(response.content)
pixmap = QPixmap.fromImage(image)
self.image_label.setPixmap(pixmap)
self.image_label.setScaledContents(True)
if __name__ == "__main__":
app = QApplication([])
window = MainWindow()
window.show()
app.exec()
This is the result of the source script:

Posted by
Cătălin George Feștilă
Labels:
2023,
module,
modules,
OpenAI,
os,
packages,
python,
python 3,
python modules,
python packages,
tutorial,
tutorials,
windows,
windows 10
Wednesday, March 22, 2023
Python 3.11.0 : clean from frequent folder and the list of recent files.
This python script that clears all entries in the Windows File Explorer from frequent folder and the list of recent files:
import os
import shutil
# Quick Access folder path on Windows
quick_access_path = os.path.join(os.environ['USERPROFILE'], 'AppData', 'Roaming', 'Microsoft', 'Windows', 'Recent', 'AutomaticDestinations')
# List all files in the Quick Access folder
files = os.listdir(quick_access_path)
print(files)
# Loop through all files in the Quick Access folder
for file in files:
# Check if the file name contains "tmp" or "temp"
if 'tmp' in file.lower() or 'temp' in file.lower():
# Construct the full file path
file_path = os.path.join(quick_access_path, file)
# Delete the file
os.remove(file_path)
# Print a message to the console
print(f"{file_path} deleted successfully.")
# Clear Frequent folder
frequent_folder = os.path.join(os.environ['APPDATA'], 'Microsoft', 'Windows', 'Recent', 'AutomaticDestinations')
os.system('del /f /q "{}\*"'.format(frequent_folder))
# Clear Recent files list
recent_folder = os.path.join(os.environ['APPDATA'], 'Microsoft', 'Windows', 'Recent')
os.system('del /f /q "{}\*"'.format(recent_folder))
Posted by
Cătălin George Feștilă
Labels:
2023,
module,
modules,
os,
packages,
python,
python 3,
python modules,
python packages,
shutil,
tutorial,
tutorials,
windows,
windows 10
Saturday, August 15, 2020
Python 3.8.5 : The hashlib python package - part 001.
The tutorial for today is about hashlib python module.
The official webpage comes for this python package has this intro:
This module implements a common interface to many different secure hash and message digest algorithms. Included are the FIPS secure hash algorithms SHA1, SHA224, SHA256, SHA384, and SHA512 (defined in FIPS 180-2) as well as RSA’s MD5 algorithm (defined in Internet RFC 1321).
The example source code to test a simple hash is this:
I run it first with default source code and then I add a comment to test_hash_file.py file.
You can see the hash is changed from b222523567a8a806382b86578717ddbd00e0f4b4 to 2134660551cc67812413a3a75fd12efb05d591ef.
The official webpage comes for this python package has this intro:
This module implements a common interface to many different secure hash and message digest algorithms. Included are the FIPS secure hash algorithms SHA1, SHA224, SHA256, SHA384, and SHA512 (defined in FIPS 180-2) as well as RSA’s MD5 algorithm (defined in Internet RFC 1321).
The example source code to test a simple hash is this:
import hashlib
import os
def file_sha1(filename):
BUF_SIZE = 65536 # read stuff in 64kb chunks!
get_sha1 = hashlib.sha1()
with open(filename, 'rb') as f:
while True:
data = f.read(BUF_SIZE)
if not data:
break
get_sha1.update(data)
return get_sha1.hexdigest()
# I add this comment after first to see the hash difference.
files = [f for f in os.listdir('.') if os.path.isfile(f)]
for f in files:
h = file_sha1(f)
print(h)
Let's test the source code with the default directory and two files.I run it first with default source code and then I add a comment to test_hash_file.py file.
You can see the hash is changed from b222523567a8a806382b86578717ddbd00e0f4b4 to 2134660551cc67812413a3a75fd12efb05d591ef.
[mythcat@desk Projects_Python]$ ls
test_hash_file.py test_numpy_001.py
[mythcat@desk Projects_Python]$ python test_hash_file.py
98b2833527ad3d9fe263542c6aa06c04182d3dfb
b222523567a8a806382b86578717ddbd00e0f4b4
[mythcat@desk Projects_Python]$ python test_hash_file.py
98b2833527ad3d9fe263542c6aa06c04182d3dfb
2134660551cc67812413a3a75fd12efb05d591ef
Sunday, June 21, 2020
Python 3.8.3 : Using twitter application with python-twitter - part 002.
This is the second part of tutorials series with python-twitter.
Today I will show you how to get pieces of information about friends, users and save into a binary file with pickle known as cPickle.
I will use my old source code from the last tutorial.
import os
import twitter
# for save to file import by python version
try:
import cPickle as pickle
except:
import pickle
consumer_key=' '
consumer_secret=' '
token_key=' '
token_secret=' '
if __name__ == "__main__":
api = twitter.Api(consumer_key=consumer_key,
consumer_secret=consumer_secret,
access_token_key=token_key,
access_token_secret=token_secret)
screen_name = 'catafest'
# print all users of this account authentificated
# you can use GetFriends(screen_name=screen_name)
users = api.GetFriends()
print([u.screen_name for u in users])
# get followers
followers = api.GetFollowers(screen_name=screen_name)
# print followers
print([f.screen_name for f in followers])
# ... and save into a binary file
followers_file = "followers_file.bin"
if not os.path.exists(followers_file):
pickle.dump(followers, open(followers_file, "wb"), protocol=pickle.HIGHEST_PROTOCOL)
# load binary file
if os.path.exists(followers_file):
followers_read = pickle.load(open(followers_file, "rb"))
print(followers_read)
The result is similar with this:
python.exe .\test_webpage_001.py
['SnapChick', 'NASA', 'andor_saga', 'blendermarket', 'Minehut', 'Aternos', 'axnro', 'Flexi23',
...
['PStackoverflow', 'SamLeac86078418', 'Sohanurr559', 'jasonalba', 'avkorablev', 'dotnetfiddle',
...
[User(ID=1260415029855256583, ScreenName=PStackoverflow),
...
Posted by
Cătălin George Feștilă
Labels:
2020,
cPickle,
module,
modules,
os,
packages,
pickle,
python,
python 3,
python modules,
python packages,
python-twitter,
tutorial,
tutorials
Saturday, June 20, 2020
Python 3.8.3 : Using twitter application with python-twitter - part 001.
You need to create a application for your twitter user developer on this webpage.
The next step is to get all keys and tokens from your application.
I used the python-twitter see the official webpage documentation.
Let's install this python module using the pip tool
pip install python-twitter
Collecting python-twitter
...
Installing collected packages: oauthlib, requests-oauthlib, python-twitter
Successfully installed oauthlib-3.1.0 python-twitter-3.5 requests-oauthlib-1.3.0
Let's see a simple source code: import os
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import twitter
import datetime
from datetime import *
consumer_key=' '
consumer_secret=' '
token_key=' '
token_secret=' '
def get_tweets(api=None, screen_name=None):
timeline = api.GetUserTimeline(screen_name=screen_name, count=200)
earliest_tweet = min(timeline, key=lambda x: x.id).id
print("getting tweets before:", earliest_tweet)
while True:
tweets = api.GetUserTimeline(
screen_name=screen_name, max_id=earliest_tweet, count=200
)
new_earliest = min(tweets, key=lambda x: x.id).id
if not tweets or new_earliest == earliest_tweet:
break
else:
earliest_tweet = new_earliest
print("getting tweets before:", earliest_tweet)
timeline += tweets
return timeline
if __name__ == "__main__":
api = twitter.Api(consumer_key=consumer_key,
consumer_secret=consumer_secret,
access_token_key=token_key,
access_token_secret=token_secret)
# print api
#print(dir(api))
# print all users of this account authentificated
#users = api.GetFriends()
#print([u.screen_name for u in users])
# print all tweets of my user catafest
screen_name = "catafest"
timeline = get_tweets(api=api, screen_name=screen_name)
dates = []
for x in timeline:
created = x.created_at
dates.append(created)
print(dates)
dat = [datetime.strptime(d, "%a %b %d %H:%M:%S +0000 %Y") for d in dates]
levels = np.tile([-8, 8, -4, 4, -1, 1],int(np.ceil(len(dat)/3)))[:len(dat)]
print(levels)
fig, ax = plt.subplots(figsize=(7.6, 5), constrained_layout=True)
ax.set(title="Twitter dates")
markerline, stemline, baseline = ax.stem(dat, levels,linefmt="C3-", basefmt="k-",use_line_collection=True)
markerline.set_ydata(np.zeros(len(dat)))
plt.setp(markerline, mec="k", mfc="w", zorder=1)
plt.show()
The result of this script comes with this output: python .\test_webpage_001.py
getting tweets before: 1123237192422367234
['Mon May 18 13:52:09 +0000 2020', 'Sat May 09 11:14:43 +0000 2020', 'Fri May 08 10:42:18 +0000 2020',
'Fri May 08 10:41:37 +0000 2020', 'Sat May 02 17:41:07 +0000 2020', 'Sat May 02 17:39:15 +0000 2020',
'Thu Apr 30 12:53:48 +0000 2020', 'Tue Apr 28 20:00:38 +0000 2020', 'Mon Apr 27 21:12:07 +0000 2020',
'Fri Apr 24 16:39:58 +0000 2020', 'Fri Apr 24 16:09:26 +0000 2020', 'Sat Apr 11 16:56:40 +0000 2020',
'Sun Mar 22 19:11:16 +0000 2020', 'Sat Mar 21 09:03:30 +0000 2020', 'Sat Mar 21 09:02:48 +0000 2020',
'Sat Mar 21 08:59:18 +0000 2020', 'Mon Mar 16 06:29:34 +0000 2020', 'Fri Jan 24 19:59:38 +0000 2020',
'Sat Jan 18 12:14:07 +0000 2020', 'Fri Jan 17 20:58:18 +0000 2020', 'Thu Jan 16 20:50:47 +0000 2020',
'Thu Jan 16 20:49:16 +0000 2020', 'Fri Jan 03 17:57:33 +0000 2020', 'Sat Dec 28 10:14:11 +0000 2019',
'Tue Apr 30 14:46:30 +0000 2019']
[-8 8 -4 4 -1 1 -8 8 -4 4 -1 1 -8 8 -4 4 -1 1 -8 8 -4 4 -1 1 -8]
The image show with matplotlib is this:

Posted by
Cătălin George Feștilă
Labels:
2020,
datetime,
matplotlib,
module,
modules,
numpy,
os,
packages,
python,
python 3,
python modules,
python packages,
python-twitter,
tutorial,
tutorials
Tuesday, November 19, 2019
Python 3.7.5 : Display a file in the hexadecimal and binary output.
This is an example with a few python3 modules that display a file in the hexadecimal and binary output:
import sys
import os.path
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("FILE", help="the file that you wish to dump to hexadecimal", type=str)
parser.add_argument("-b", "--binary", help="display bytes in binary format instead of hexadecimal")
args = parser.parse_args()
try:
with open(args.FILE, "rb") as f:
n = 0
b = f.read(16)
while b:
if not args.binary:
s1 = " ".join([format(i,'02x') for i in b])
s1 = s1[0:23] + " " + s1[23:]
width = 48
else:
s1 = " ".join([format(i,'08b') for i in b])
s1 = s1[0:71] + " " + s1[71:]
width = 144
s2 = "".join([chr(i) if 32 <= i <= 127 else "." for i in b])
print(format(n * 16,'08x'), format(s1), format(s2))
n += 1
b = f.read(16)
print(format(os.path.getsize(args.FILE),'08x'))
except Exception as e:
print(__file__, ": ", type(e).__name__, " - ", e, sep="", file=sys.stderr)
The hexadecimal output of the sec.png file:[mythcat@desk test]$ python3 hex.py sec.png
00000000 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 .PNG........IHDR
00000010 00 00 01 0b 00 00 00 bd 08 03 00 00 00 93 6f a8 ..............o.
00000020 bc 00 00 00 5d 50 4c 54 45 ff ff ff ab ab ab fc ....]PLTE.......
00000030 fc fc a0 a0 a0 f5 f5 f5 a6 a6 a6 f9 f9 f9 9e 9e ................
00000040 9e f1 f1 f1 fa fa fa ea ea ea ad ad ad a4 a4 a4 ................
00000050 d0 d0 d0 f6 f6 f6 e5 e5 e5 bc bc bc c3 c3 c3 97 ................
00000060 97 97 b4 b4 b4 d8 d8 d8 bf bf bf cb cb cb db db ................
00000070 db e1 e1 e1 93 93 93 7f 7f 7f 8d 8d 8d 85 85 85 .............
00000080 79 79 79 70 70 70 d9 a6 b1 29 00 00 08 fa 49 44 yyyppp...)....ID
00000090 41 54 78 9c e5 9d 89 92 a3 2a 14 40 45 45 09 a8 ATx......*.@EE..
000000a0 80 18 34 d3 3d ef ff 3f f3 69 92 4e 67 71 45 90 ..4.=..?.i.NgqE.
000000b0 65 4e 55 4f a5 7b 52 c6 20 dc 9d 4b 14 f9 46 f7 eNUO.{R. ..K..F.
000000c0 87 36 79 8d 6d df 86 75 28 03 e4 4f c4 cb 28 ca .6y.m..u(..O..(.
000000d0 63 db f7 62 97 36 91 8f e9 80 72 ce 6c de 8b 65 c..b.6....r.l..e
000000e0 c8 f9 f9 b7 14 53 68 eb 4e ac 73 a1 ef 7f 91 d2 .....Sh.N.s....
000000f0 c6 7d 6c 04 71 91 e9 be 66 f2 31 14 51 04 dd 97 .}l.q...f.1.Q...
00000100 a0 9c d0 8e e8 be 66 33 f6 d7 5a f3 a7 68 a7 e4 ......f3..Z..h..
00000110 91 f6 f9 8b c1 e8 9f 45 aa f5 53 34 93 45 71 7e .......E..S4.Eq~
00000120 7d 21 4a 9d 97 9d 98 00 ba 67 9f 4e b2 3a 61 a2 }!J......g.N.:a.
00000130 bb bd 96 10 f5 ff e2 b2 c3 d5 ee eb b6 13 b3 4c ...............L
00000140 a0 dd 97 36 06 96 51 c7 7f 7e 41 02 00 c8 da 86 ...6..Q.~A.....
00000150 13 00 bb 7d d7 9d 94 0b f9 88 44 75 03 fc f5 f6 ...}......Du....
00000160 9c d2 d3 fd 45 d2 ff 34 cf 0f 17 89 75 aa 26 46 ....E..4....u.&F
00000170 b4 2d b9 98 fc 7f a0 75 29 ea 43 4e df b2 68 a2 .-....u).CN..h.
...
The binnary output of sec.png file:[mythcat@desk test]$ python3 hex.py -b FILE sec.png
00000000 10001001 01010000 01001110 01000111 00001101 00001010 00011010 00001010
00000000 00000000 00000000 00001101 01001001 01001000 01000100 01010010 .PNG........IHDR
00000010 00000000 00000000 00000001 00001011 00000000 00000000 00000000 10111101
00001000 00000011 00000000 00000000 00000000 10010011 01101111 10101000 ..............o.
00000020 10111100 00000000 00000000 00000000 01011101 01010000 01001100 01010100
01000101 11111111 11111111 11111111 10101011 10101011 10101011 11111100 ....]PLTE.......
00000030 11111100 11111100 10100000 10100000 10100000 11110101 11110101 11110101
10100110 10100110 10100110 11111001 11111001 11111001 10011110 10011110 ................
00000040 10011110 11110001 11110001 11110001 11111010 11111010 11111010 11101010
11101010 11101010 10101101 10101101 10101101 10100100 10100100 10100100 ................
00000050 11010000 11010000 11010000 11110110 11110110 11110110 11100101 11100101
11100101 10111100 10111100 10111100 11000011 11000011 11000011 10010111 ................
00000060 10010111 10010111 10110100 10110100 10110100 11011000 11011000 11011000
10111111 10111111 10111111 11001011 11001011 11001011 11011011 11011011 ................
00000070 11011011 11100001 11100001 11100001 10010011 10010011 10010011 01111111
01111111 01111111 10001101 10001101 10001101 10000101 10000101 10000101 .............
00000080 01111001 01111001 01111001 01110000 01110000 01110000 11011001 10100110
10110001 00101001 00000000 00000000 00001000 11111010 01001001 01000100 yyyppp...)....ID
00000090 01000001 01010100 01111000 10011100 11100101 10011101 10001001 10010010
10100011 00101010 00010100 01000000 01000101 01000101 00001001 10101000 ATx......*.@EE..
...
Saturday, November 2, 2019
Python 3.7.5 : The ani script with ascii.
ASCII, abbreviated from American Standard Code for Information Interchange, is a character encoding standard for electronic communication. ASCII codes represent text in computers, telecommunications equipment, and other devices. see Wikipedia.
This is a simple script named ani.py created by me to show an animation with ASCII ...
The content of these files:
This is a simple script named ani.py created by me to show an animation with ASCII ...
import os, time
os.system('cls')
filenames = ["0.txt","1.txt","2.txt","3.txt"]
frames = []
for name in filenames:
with open (name, "r", encoding="utf8") as f:
frames.append(f.readlines())
"""
for frame in frames:
print("".join(frame))
time.sleep(1)
os.system('clear')
"""
for i in range (4):
os.system('clear')
for frame in frames:
print("".join(frame))
time.sleep(1)
os.system('clear')
You need four text files with an 8X8 character matrix format: 0.txt , 1.txt , 2.txt and 3.txt.The content of these files:
$ cat *.txt
######
######
######
######
####
####
##
##
The end result is a square that shrinks to 4 characters #.
Posted by
Cătălin George Feștilă
Labels:
2019,
os,
python,
python 3,
python modules,
time,
tutorial,
tutorials
Saturday, May 22, 2010
The beauty of Python: Some examples with os and sys modules - part 2
Today i will tell about sys and os modules.
The module os has OS routines for Mac, NT, or Posix depending on what system we're on.
The module sys provides access to some objects used or maintained by the interpreter and to functions that interact strongly with the interpreter.
Some useful functions with sys module.
And now, some useful functions with os module.
These is just a brief tutorial about sys and os modules.
The module os has OS routines for Mac, NT, or Posix depending on what system we're on.
The module sys provides access to some objects used or maintained by the interpreter and to functions that interact strongly with the interpreter.
Some useful functions with sys module.
>>> print sys.version
2.5.2 (r252:60911, Oct 5 2008, 19:24:49)
[GCC 4.3.2]
>>> print sys.version_info
(2, 5, 2, 'final', 0)
>>> print sys.subversion
('CPython', 'tags/r252', '60911')
>>> print sys.platform
linux2
>>> print sys.ps1
>>>
>>> print sys.ps2
...
>>> print sys.prefix
/usr
>>> print sys.path
['', '/usr/lib/python2.5', '/usr/lib/python2.5/plat-linux2', '/usr/lib/python2.5/lib-tk',
'/usr/lib/python2.5/lib-dynload', '/usr/local/lib/python2.5/site-packages',
'/usr/lib/python2.5/site-packages', '/usr/lib/python2.5/site-packages/Numeric',
'/usr/lib/python2.5/site-packages/PIL', '/usr/lib/python2.5/site-packages/gst-0.10',
'/var/lib/python-support/python2.5', '/usr/lib/python2.5/site-packages/gtk-2.0',
'/var/lib/python-support/python2.5/gtk-2.0']
>>> print sys.modules.keys()
['apt.os', 'email.iterators', 'apport.sys', 'random', 'apport.atexit', 'subprocess',
'email.MIMEImage', 'gc', 'apport.pwd', 'os.path', 'encodings.encodings', 'email.mime',
'email.MIMEText', 'xml', 'email.time', 'struct', 'tempfile', 'base64', 'apt.cache',
'pyexpat.errors', 'apt_pkg', 'apport', 'email.binascii', 'email.Parser', 'zipimport',
'apport.xml', 'xml.dom.copy', 'encodings.utf_8', 'apt.apt_pkg', 'email.quoprimime',
'email.mime.text', 'email.urllib', 'email.FeedParser', 'signal', 'email.encoders',
'pyexpat.model', 'apport.packaging_impl', 'apport.cStringIO', 'quopri',
'email.Message', 'cStringIO', 'zlib', 'locale', 'email.charset', 'apport.fileutils',
'xml.parsers.expat', 'atexit', 'email.quopriMIME', 'encodings', 'email.Generator',
'apport.warnings', 'apport.problem_report', 'apt.fcntl', 'email.MIMEAudio', 'urllib',
're', 'apt.select', 'email.quopri', 'apport.report', 'email.mime.base', 'email.errors',
'email', 'math', 'fcntl', 'apport.os', 'apt.progress', 'UserDict', 'exceptions',
'apport.grp', 'apport.shutil', 'codecs', 'xml.dom.domreg', 'email.Header', '_locale',
'email.Iterators', 'socket', 'thread', 'traceback', 'apt.apt', 'e,
'SUDO_COMMAND': '/bin/su', 'SUDO_GID': '999', 'SDL_VIDEO_CENTERED': '1',
'PWD': '/home/mint/Desktop', 'COLORTERM': 'gnome-terminal', 'MAIL': '/var/mail/root'}
mail.Charset', 'xml.dom.xmlbuilder', 'os', 'marshal', 'apport.stat', 'apport.re',
'apt.gettext', 'email.uu', '_sre', 'unittest', '__builtin__', 'apport.apport',
'xml.parsers', 'apport.fnmatch', 'apport.urllib', 'operator', 'xml.parsers.pyexpat',
'email.Errors', 'select', 'apt.string', 'apport.glob', 'apt.warnings', 'email.socket',
'posixpath', 'email.base64MIME', 'errno', '_socket', 'binascii', 'email.Utils',
'sre_constants', 'email.MIMEMessage', 'email._parseaddr', 'email.sys',
'apport.traceback', 'apt.package', 'apt.random', 'xml.dom.NodeFilter',
'email.MIMENonMultipart', '_codecs', 'apport.unittest', 'apport.apt', 'email.os',
'email.utils', 'pwd', 'apport.time', 'copy', '_struct', '_types', 'email.email',
'apt.cdrom', 'uu', 'xml.dom.minidom', 'apport_python_hook', 'apt', 'email.random',
'posix', 'encodings.aliases', 'apt.sys', 'fnmatch', 'sre_parse', 'pickle', 'copy_reg',
'sre_compile', '_random', 'site', 'email.base64', 'apt.errno', '__main__', 'problem_report',
'pyexpat', 'email.MIMEBase', 'email.message', 'string', 'email.mime.nonmultipart',
'apport.subprocess', 'shutil', 'strop', 'grp', 'encodings.codecs', 'gettext',
'email.warnings', 'xml.dom.minicompat', 'email.MIMEMultipart', 'types', 'apport.tempfile',
'stat', '_ssl', 'warnings', 'encodings.types', 'glob', 'email.re', 'sys', 'email.Encoders',
'readline', 'email.cStringIO', 'xml.dom', 'xml.dom.xml', 'apport.signal', 'sitecustomize',
'email.mime.email', 'email.base64mime', 'email.mime.multipart', 'apport.packaging',
'urlparse', 'linecache', 'email.string', 'apt.re', 'time', 'gzip']
And now, some useful functions with os module.
>>> print os.uname()
('Linux', 'mint', '2.6.27-7-generic', '#1 SMP Fri Oct 24 06:42:44 UTC 2008', 'i686')
>>> print os.ttyname(1)
/dev/pts/0
>>> print os.times()
(0.050000000000000003, 0.02, 0.0, 0.0, 17186002.649999999)
>>> print os.environ
{'USERNAME': 'root', 'LANG': 'en_US.UTF-8', 'TERM': 'xterm', 'SHELL': '/bin/bash',
'XDG_SESSION_COOKIE': '842d38513df1a6bb7490c8a14bf69489-1274456064.963733-1686354756',
'SUDO_COMMAND': '/bin/su', 'SHLVL': '1', 'RUNNING_UNDER_GDM': 'yes', 'SUDO_UID': '999',
'SUDO_GID': '999', 'PWD': '/home/mint/Desktop', 'LOGNAME': 'root', 'USER': 'root',
'COLORTERM': 'gnome-terminal',
'PATH': '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games',
'MAIL': '/var/mail/root', 'SUDO_USER': 'mint', 'HOME': '/root', 'DISPLAY': ':0.0',
'_': '/usr/bin/python', 'XAUTHORITY': '/home/mint/.Xauthority'}
>>> print os.mkdir('aaa')
None
>>> print os.mkdir('aaa')
Traceback (most recent call last):
File "", line 1, in
OSError: [Errno 17] File exists: 'aaa'
>>> print os.listdir('/')
['media', 'root', 'sbin', 'usr', 'lib', 'tmp', 'home', 'var', 'cdrom', 'etc',
'rofs', 'bin', 'boot', 'dev', 'initrd.img', 'mnt', 'opt', 'proc', 'srv',
'sys', 'vmlinuz']
These is just a brief tutorial about sys and os modules.
Posted by
Cătălin George Feștilă
Labels:
2.7,
2010,
os,
python,
python modules,
sys,
tutorial,
tutorials
Subscribe to:
Posts (Atom)