analitics

Pages

Monday, September 9, 2019

Python 3.7.4 : Using the sunpy - part 001.

I wrote about sunpy in the past on this website.
Now this package comes with new features, see the official webpage.
Let's install it.
[mythcat@desk ~]$ pip3 install sunpy --user
Collecting sunpy
...
Successfully installed aioftp-0.13.0 aiohttp-3.6.0 astropy-3.2.1 async-timeout-3.0.1 
multidict-4.5.2 parfive-1.0.0 scipy-1.3.1 sunpy-1.0.3 tqdm-4.35.0 yarl-1.3.0
If you search on web you can find example with spectra , but in the new package is not supported.
ModuleNotFoundError: No module named 'sunpy.spectra'
Let's test it:
[mythcat@desk ~]$ python3 
Python 3.7.4 (default, Jul  9 2019, 16:32:37) 
[GCC 9.1.1 20190503 (Red Hat 9.1.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sunpy
>>> dir(sunpy)
['SunPyTestRunner', 'UnsupportedPythonError', '__all__', '__builtins__', '__cached__', '__doc__',
 '__file__', '__loader__', '__minimum_python_version__', '__name__', '__package__', '__path__',
 '__spec__', '__version__', '_init_log', 'builtins', 'config', 'extern', 'load_config', 'log',
 'logging', 'os', 'print_config', 'self_test', 'sys', 'system_info', 'tests', 'util', 'version']
>>> sunpy.system_info()
==============================
SunPy Installation Information
==============================

#######
General
#######
Time : Monday, 09. September 2019 07:14PM UT
System : Linux
Processor : x86_64
Arch : 64bit
SunPy : 1.0.3
OS: Fedora 30 Thirty (Linux 5.2.11-200.fc30.x86_64 x86_64)


##################
Required Libraries
##################
Python: 3.7.4
NumPy: 1.16.4
SciPy: 1.3.1
matplotlib: 3.0.3
Astropy: 3.2.1
Pandas: 0.25.1
parfive: 1.0.0


#####################
Recommended Libraries
#####################
beautifulsoup: NOT INSTALLED
PyQt4: NOT INSTALLED
PyQt5: 5.12.2
Zeep: NOT INSTALLED
Sqlalchemy: 1.3.7
drms: NOT INSTALLED
>>> help(sunpy)

Help on package sunpy:

NAME
    sunpy

DESCRIPTION
    SunPy
    =====
    
    An open-source Python library for Solar Physics data analysis.
    
    Web Links
    ---------
    Homepage: https://sunpy.org
    Documentation: https://docs.sunpy.org/en/stable/

PACKAGE CONTENTS
    cm (package)
    compiler_version
    conftest
    coordinates (package)
    data (package)
    database (package)
    extern (package)
    image (package)
    instr (package)
    io (package)
    map (package)
    net (package)
    physics (package)
    roi (package)
    sun (package)
    tests (package)
    time (package)
    timeseries (package)
    util (package)
    version
    visualization (package)
... 
Let's install zeep, beautifulsoup4 and drms python packages:
[mythcat@desk ~]$ pip3 install zeep --user
Collecting zeep
...
Successfully installed appdirs-1.4.3 cached-property-1.5.1 isodate-0.6.0 lxml-4.4.1
 requests-toolbelt-0.9.1 zeep-3.4.0
[mythcat@desk ~]$ pip3 install --upgrade beautifulsoup4 --user
Collecting beautifulsoup4
...
Successfully installed beautifulsoup4-4.8.0 soupsieve-1.9.3
[mythcat@desk ~]$ pip3 install --upgrade drms --user
Collecting drms
...
Successfully installed drms-0.5.7
Now, we can see examples with this python package.
First, I will use the AIA_171_IMAGE image:
>>> from sunpy.data.sample import AIA_171_IMAGE 
Files Downloaded: 100%|███████████████████████| 26/26 [00:04<00:00 5.50file="" s="">>> import sunpy.map                                                            
>>> aiamap = sunpy.map.Map(AIA_171_IMAGE)
>>> aiamap.peek()                                                               
Attribute Qt::AA_EnableHighDpiScaling must be set before QCoreApplication is created.                                                                           

** (python3:4152): WARNING **: 22:53:15.329: AT-SPI: Could not obtain desktop path or name


** (python3:4152): WARNING **: 22:53:15.377: atk-bridge: GetRegisteredEvents returned message with unknown signature



Python 3.7.3 : Using the flask - part 018.

In this tutorial, I will show you how to fix auto increment in Flask SQLAlchemy.
The old source code for the user model from the server.py was this:
class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True)
    password = db.Column(db.String(120), unique=True)
    email = db.Column(db.String(120), unique=True)
    gender = db.Column(db.String(5), unique=True)
    work = db.Column(db.String(33), unique=True)
    city = db.Column(db.String(15), unique=True)
The server.sqlite will be this:
[mythcat@desk my_flask]$ sqlite3 server.sqlite 
SQLite version 3.26.0 2018-12-01 12:34:55
Enter ".help" for usage hints.
sqlite> .schema
CREATE TABLE user (
 id INTEGER NOT NULL, 
 username VARCHAR(80), 
 password VARCHAR(120), 
 email VARCHAR(120), 
 gender VARCHAR(5), 
 work VARCHAR(33), 
 city VARCHAR(15), 
 PRIMARY KEY (id), 
 UNIQUE (username), 
 UNIQUE (password), 
 UNIQUE (email), 
 UNIQUE (gender), 
 UNIQUE (work), 
 UNIQUE (city)
);
CREATE TABLE alembic_version (
 version_num VARCHAR(32) NOT NULL, 
 CONSTRAINT alembic_version_pkc PRIMARY KEY (version_num)
);
CREATE TABLE sqlite_stat1(tbl,idx,stat);
sqlite> ^Z 
If you want to change the id into auto increment then you need to follow this steps:
class User(db.Model):
    __tablename__ = 'user'
    __table_args__ = {'sqlite_autoincrement': True}
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True)
    password = db.Column(db.String(120), unique=True)
    email = db.Column(db.String(120), unique=True)
    gender = db.Column(db.String(5), unique=True)
    work = db.Column(db.String(33), unique=True)
    city = db.Column(db.String(15), unique=True)
Delete the server.sqlite file or rename it.
Open python3 and create a new server.sqlite file:
[mythcat@desk my_flask]$ python3
Python 3.7.4 (default, Jul  9 2019, 16:32:37) 
[GCC 9.1.1 20190503 (Red Hat 9.1.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from server import db
>>> db.create_all()
>>> db.engine.table_names()
['sqlite_sequence', 'user']
>>> 
[5]+  Stopat                  python3 
Open the new file to see the changes:
[mythcat@desk my_flask]$ sqlite3 server.sqlite 
SQLite version 3.26.0 2018-12-01 12:34:55
Enter ".help" for usage hints.
sqlite> .schema
CREATE TABLE user (
 id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, 
 username VARCHAR(80), 
 password VARCHAR(120), 
 email VARCHAR(120), 
 gender VARCHAR(5), 
 work VARCHAR(33), 
 city VARCHAR(15), 
 UNIQUE (username), 
 UNIQUE (password), 
 UNIQUE (email), 
 UNIQUE (gender), 
 UNIQUE (work), 
 UNIQUE (city)
);
CREATE TABLE sqlite_sequence(name,seq);
sqlite>  

Wednesday, September 4, 2019

Python 3.7.4 : Create an Stand Alone Executable on Fedora distro.

In this tutorial I will show you how to create an Stand Alone Executable with Python version 3.7.4 and Fedora 30 distro.
First you need to install using the dnf tool the python3 package.
You can test it easy with this command:
[mythcat@desk dist]$ python3
Python 3.7.4 (default, Jul  9 2019, 16:32:37) 
[GCC 9.1.1 20190503 (Red Hat 9.1.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information. 
I created a simple python script named test.py for testing:
import os
import sys
print("Hello!")
You need to install the pyinstaller python module.
Let's see this step with the output:
[mythcat@desk ~]$ pip3 install pyinstaller --user
Now you can use this python module to create Stand Alone Executable.
[mythcat@desk ~]$ pip3 install pyinstaller --user
Requirement already satisfied: pyinstaller in ./.local/lib/python3.7/site-packages (3.5)
Requirement already satisfied: setuptools in /usr/lib/python3.7/site-packages (from pyinstaller) (40.8.0)
Requirement already satisfied: altgraph in ./.local/lib/python3.7/site-packages (from pyinstaller) (0.16.1)
[mythcat@desk ~]$ pyinstaller --onefile test.py
561 INFO: PyInstaller: 3.5
561 INFO: Python: 3.7.4
571 INFO: Platform: Linux-5.2.9-200.fc30.x86_64-x86_64-with-fedora-30-Thirty
573 INFO: wrote /home/mythcat/test.spec
596 INFO: UPX is not available.
598 INFO: Extending PYTHONPATH with paths
['/home/mythcat', '/home/mythcat']
598 INFO: checking Analysis
624 INFO: Building because /home/mythcat/test.py changed
624 INFO: Initializing module dependency graph...
640 INFO: Initializing module graph hooks...
678 INFO: Analyzing base_library.zip ...
5247 INFO: running Analysis Analysis-00.toc
5352 INFO: Caching module hooks...
5376 INFO: Analyzing /home/mythcat/test.py
5396 INFO: Loading module hooks...
5397 INFO: Loading module hook "hook-xml.py"...
5775 INFO: Loading module hook "hook-pydoc.py"...
5800 INFO: Loading module hook "hook-encodings.py"...
5892 INFO: Looking for ctypes DLLs
5893 INFO: Analyzing run-time hooks ...
5901 INFO: Looking for dynamic libraries
6507 INFO: Looking for eggs
6507 INFO: Using Python library /lib64/libpython3.7m.so.1.0
6514 INFO: Warnings written to /home/mythcat/build/test/warn-test.txt
6547 INFO: Graph cross-reference written to /home/mythcat/build/test/xref-test.html
6604 INFO: checking PYZ
6607 INFO: Building because toc changed
6607 INFO: Building PYZ (ZlibArchive) /home/mythcat/build/test/PYZ-00.pyz
7030 INFO: Building PYZ (ZlibArchive) /home/mythcat/build/test/PYZ-00.pyz completed successfully.
7034 INFO: checking PKG
7035 INFO: Building because toc changed
7035 INFO: Building PKG (CArchive) PKG-00.pkg
10033 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
10036 INFO: Bootloader 
/home/mythcat/.local/lib/python3.7/site-packages/PyInstaller/bootloader/Linux-64bit/run
10036 INFO: checking EXE
10123 INFO: Building because toc changed
10124 INFO: Building EXE from EXE-00.toc
10163 INFO: Appending archive to ELF section in EXE /home/mythcat/dist/test
10337 INFO: Building EXE from EXE-00.toc completed successfully.
This command will create some folders: dist, build, ...
The dist folder will have the Stand Alone Executable named test:
[mythcat@desk ~]$ cd dist/
[mythcat@desk dist]$ ls
test
[mythcat@desk dist]$ ./test
Hello!

Tuesday, August 27, 2019

Python 3.7.3 : Using the flask - part 017.

Today I make some changes with my server.py and database and solve some issues, see old version at my old tutorial.
Firt issue was start script.
I create a linux script named start_server.sh to run the flask run command:
[mythcat@desk my_flask]$ ./start_server.sh 
I update the User with new fiels:
class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True)
    password = db.Column(db.String(120), unique=True)
    email = db.Column(db.String(120), unique=True)
    gender = db.Column(db.String(5), unique=True)
    work = db.Column(db.String(33), unique=True)
    city = db.Column(db.String(15), unique=True)
Let's see how I deal with this versus database and migrate process.
[mythcat@desk my_flask]$ rm server.sqlite 
[mythcat@desk my_flask]$ python3
Python 3.7.4 (default, Jul  9 2019, 16:32:37) 
[GCC 9.1.1 20190503 (Red Hat 9.1.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from server import db
>>> db.create_all()
>>> db.engine.table_names()
['user']
>>> exit()
[mythcat@desk my_flask]$ ls server.sqlite 
server.sqlite
[mythcat@desk my_flask]$ sqlite3 server.sqlite 
SQLite version 3.26.0 2018-12-01 12:34:55
Enter ".help" for usage hints.
sqlite> .tables
user
sqlite> .schema user
CREATE TABLE user (
        id INTEGER NOT NULL, 
        username VARCHAR(80), 
        password VARCHAR(120), 
        email VARCHAR(120), 
        gender VARCHAR(5), 
        work VARCHAR(33), 
        city VARCHAR(15), 
        PRIMARY KEY (id), 
        UNIQUE (username), 
        UNIQUE (password), 
        UNIQUE (email), 
        UNIQUE (gender), 
        UNIQUE (work), 
        UNIQUE (city)
);
I got a strange error:
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) duplicate column name: city
I fix it with this , but I'm not sure if the right way:
[mythcat@desk my_flask]$ rm migrations/ -r -f 
[mythcat@desk my_flask]$ python3 server.py db init 
  Creating directory /home/mythcat/project_github/my_flask/migrations ... done
  Creating directory /home/mythcat/project_github/my_flask/migrations/versions ... done
  Generating /home/mythcat/project_github/my_flask/migrations/script.py.mako ... done
  Generating /home/mythcat/project_github/my_flask/migrations/env.py ... done
  Generating /home/mythcat/project_github/my_flask/migrations/alembic.ini ... done
  Generating /home/mythcat/project_github/my_flask/migrations/README ... done
  Please edit configuration/connection/logging settings in '/home/mythcat/project_github/my_flask/migrations/alembic.ini'
  before proceeding.
[mythcat@desk my_flask]$ python3 server.py db migrate
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.env] No changes in schema detected.
[mythcat@desk my_flask]$ python3 server.py db upgrade
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.

Sunday, August 25, 2019

Python 3.7.3 : Using the flask - part 016.

Today I tested a new feature of Flask version 1.1.1.
[mythcat@desk my_flask]$ pip list | grep Flask
Flask                    1.1.1            
Flask-Login              0.4.1            
Flask-Mail               0.9.1            
Flask-Migrate            2.5.2            
Flask-Script             2.0.6            
Flask-SQLAlchemy         2.4.0            
Flask-WTF                0.14.2 
This feature will remove the jsonify python module.
Let's start the blueprint blue_test:
[mythcat@desk my_flask]$ export FLASK_APP=blue_test
[mythcat@desk my_flask]$ flask run 
 * Serving Flask app "blue_test"
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
The output is this:
 {"result":"This is main page!"}
Into my blue_test folder I will remove this jsonify python module:
[
mythcat@desk my_flask]$ cd blue_test/
[mythcat@desk blue_test]$ ll
total 40
-rw-rw-r--. 1 mythcat mythcat  192 Aug 23 11:02 extensions.py
-rw-rw-r--. 1 mythcat mythcat 1826 Aug 23 11:02 forms.py
-rw-rw-r--. 1 mythcat mythcat  701 Aug 23 12:03 __init__.py
-rw-rw-r--. 1 mythcat mythcat 1491 Aug 23 11:02 models.py
drwxrwxr-x. 2 mythcat mythcat  184 Aug 25 14:36 __pycache__
-rw-rw-r--. 1 mythcat mythcat 1050 Aug 23 11:02 routes.py
-rw-rw-r--. 1 mythcat mythcat  142 Aug 23 11:02 settings.py
-rw-rw-r--. 1 mythcat mythcat   34 Aug 23 11:02 start.bat
drwxrwxr-x. 2 mythcat mythcat   81 Aug 23 11:02 templates
-rw-rw-r--. 1 mythcat mythcat 8192 Aug 23 11:02 texts.sqlite
-rw-rw-r--. 1 mythcat mythcat  677 Aug 23 11:02 views.py 
The routes.py will have these changes:
#from flask import Blueprint, jsonify, request
# new flask
from flask import Blueprint, request
...
def home():
    #return jsonify({'result' : 'This is main page!'})
    #this is the new source code:
    return {'result' : 'This is main page!'} 
The next step is to find all jsonify word into my source code using the grep tool and make changes:
[mythcat@desk my_flask]$ grep -nr jsonify *
Binary file blue_test/__pycache__/views.cpython-37.pyc matches
blue_test/views.py:3:from flask import Blueprint, jsonify, request
blue_test/views.py:20:    return jsonify ({'texts':texts})
blue_test/routes.py:1:#from flask import Blueprint, jsonify, request
crud.py:1:from flask import Flask, request, jsonify
crud.py:57:    return jsonify(new_user)
crud.py:64:    return jsonify(result.data)
crud.py:70:    return user_schema.jsonify(user)
crud.py:83:    return user_schema.jsonify(user)
crud.py:92:    return user_schema.jsonify(user)
Binary file __pycache__/crud.cpython-37.pyc matches
Binary file __pycache__/server.cpython-37.pyc matches
server.py:9:from flask import jsonify
server.py:101:    #return jsonify(new_user)
server.py:109:    #return users_schema.jsonify(users)
server.py:111:    return jsonify(all_users.data)
server.py:117:    return jsonify(result.data)
server.py:122:        return jsonify([d['keyword'] for d in result.data])
server.py:132:    return user_schema.jsonify(new_user)
tserv.py:13:from flask import jsonify
tserv.py:56:    #return users_schema.jsonify(users)
tserv.py:58:    return jsonify(all_users.data)
tserv.py:69:    return user_schema.jsonify(user_post)
tserv.py:80:    return user_schema.jsonify(user_put)
tserv.py:88:    return user_schema.jsonify(user_delete)
[mythcat@desk my_flask]$  
After I make changes the result of grep is this:
[mythcat@desk my_flask]$ grep -nr jsonify *
Binary file blue_test/__pycache__/views.cpython-37.pyc matches
blue_test/routes.py:1:#from flask import Blueprint, jsonify, request
blue_test/views.py:3:#from flask import Blueprint, jsonify, request
crud.py:1:#from flask import Flask, request, jsonify
Binary file __pycache__/crud.cpython-37.pyc matches
Binary file __pycache__/server.cpython-37.pyc matches
server.py:9:#from flask import jsonify
server.py:109:    #return users_schema.jsonify(users)
tserv.py:13:#from flask import jsonify
tserv.py:56:    #return users_schema.jsonify(users) 
You can see the last version of my project here.







Saturday, August 24, 2019

Python 3.7.3 : Using the flask - part 015.

In this tutorial, I will show you how to migrate using the Database Migrations in flask project.
Because my laptop is gone I use my old Linux.
First you need to install these python modules with --user argument for Linux:
[mythcat@desk my_flask]$ pip3 install flask-migrate --user
...
[mythcat@desk my_flask]$ pip3 install flask-script --user
Let's test this new issue with server.py file by adding these python modules:
#migrate 
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
...
# create migrate object with db 
migrate = Migrate(app, db)
# create manager 
manager = Manager(app)
# create db command for manager 
manager.add_command('db', MigrateCommand)
...
# add new columns into database 
class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True)
    email = db.Column(db.String(120), unique=True)
    gender = db.Column(db.String(5), unique=True)
    work = db.Column(db.String(33), unique=True)
    city = db.Column(db.String(15), unique=True)
...
# the default name main
if __name__ == '__main__':
    manager.run()
    app.run(debug=True)
Let's fix this migrate issue with the new command:
[mythcat@desk my_flask]$ python3 server.py db init 
  Creating directory /home/mythcat/project_github/my_flask/migrations ... done
  Creating directory /home/mythcat/project_github/my_flask/migrations/versions ... done
  Generating /home/mythcat/project_github/my_flask/migrations/script.py.mako ... done
  Generating /home/mythcat/project_github/my_flask/migrations/env.py ... done
  Generating /home/mythcat/project_github/my_flask/migrations/alembic.ini ... done
  Generating /home/mythcat/project_github/my_flask/migrations/README ... done
  Please edit configuration/connection/logging settings in '/home/mythcat/project_github/my_flask/migrations
/alembic.ini'
  before proceeding.

[mythcat@desk my_flask]$ python3 server.py db migrate
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.autogenerate.compare] Detected added column 'user.city'
INFO  [alembic.autogenerate.compare] Detected added column 'user.gender'
INFO  [alembic.autogenerate.compare] Detected added column 'user.work'
INFO  [alembic.autogenerate.compare] Detected added unique constraint 'None' on '['city']'
INFO  [alembic.autogenerate.compare] Detected added unique constraint 'None' on '['gender']'
INFO  [alembic.autogenerate.compare] Detected added unique constraint 'None' on '['work']'
  Generating /home/mythcat/project_github/my_flask/migrations/versions/ca70c42b5b7a_.py ... done

[mythcat@desk my_flask]$ python3 server.py db upgrade 
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade  -> ca70c42b5b7a, empty message
ERROR [root] Error: No support for ALTER of constraints in SQLite dialect
The database fiels is changed by this command.
Let's see with sqlite3 tool:
[mythcat@desk my_flask]$ sqlite3 server.sqlite 
SQLite version 3.26.0 2018-12-01 12:34:55
Enter ".help" for usage hints.
sqlite> .tables
alembic_version  user           
sqlite> .schema user
CREATE TABLE user (
        id INTEGER NOT NULL, 
        username VARCHAR(80), 
        email VARCHAR(120), city VARCHAR(15), gender VARCHAR(5), work VARCHAR(33), 
        PRIMARY KEY (id), 
        UNIQUE (username), 
        UNIQUE (email)
);
You can see my source code here.

Thursday, August 22, 2019

Python 3.7.3 : Using the inotify.

About this tool you can read here:
inotify functionality is available from the Linux kernel and allows you to register one or more directories for watching, and to simply block and wait for notification events.
[mythcat@desk ~]$ pip3 install inotify --user
...
Successfully installed inotify-0.2.10 nose-1.3.7
Let's test it and see how this can be load it:
[mythcat@desk ~]$ python3
Python 3.7.4 (default, Jul  9 2019, 16:32:37) 
[GCC 9.1.1 20190503 (Red Hat 9.1.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import inotify
>>> from inotify import *
>>> print(dir(inotify))
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', 
'__path__', '__spec__', '__version__']
>>> import inotify.adapters
>>> print(dir(inotify))
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', 
'__path__', '__spec__', '__version__', 'adapters', 'calls', 'constants', 'library']
>>> print(dir(inotify.adapters))
['EINTR', 'EventTimeoutException', 'Inotify', 'InotifyTree', 'InotifyTrees', 'TerminalEventException', 
'_BaseTree', '_DEFAULT_EPOLL_BLOCK_DURATION_S', '_DEFAULT_TERMINAL_EVENTS', '_HEADER_STRUCT_FORMAT', 
'_INOTIFY_EVENT', '_IS_DEBUG', '_LOGGER', '_STRUCT_HEADER_LENGTH', '__builtins__', '__cached__', '__doc__',
 '__file__', '__loader__', '__name__', '__package__', '__spec__', 'collections', 'inotify', 'logging', 'os',
 'select', 'struct', 'time']
>>> import inotify.calls
>>> print(dir(inotify.calls))
['InotifyError', '_LIB', '_LOGGER', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', 
'__name__', '__package__', '__spec__', '_check_nonnegative', '_check_nonzero', '_check_zero', 'ctypes', 
'errno', 'inotify', 'inotify_add_watch', 'inotify_init', 'inotify_rm_watch', 'logging']
>>> import inotify.constants
>>> print(dir(inotify.constants))
['IN_ACCESS', 'IN_ALL_EVENTS', 'IN_ATTRIB', 'IN_CLOEXEC', 'IN_CLOSE', 'IN_CLOSE_NOWRITE', 'IN_CLOSE_WRITE', 
'IN_CREATE', 'IN_DELETE', 'IN_DELETE_SELF', 'IN_DONT_FOLLOW', 'IN_IGNORED', 'IN_ISDIR', 'IN_MASK_ADD', 
'IN_MODIFY', 'IN_MOVE', 'IN_MOVED_FROM', 'IN_MOVED_TO', 'IN_MOVE_SELF', 'IN_NONBLOCK', 'IN_ONESHOT', 
'IN_ONLYDIR', 'IN_OPEN', 'IN_Q_OVERFLOW', 'IN_UNMOUNT', 'MASK_LOOKUP', '__builtins__', '__cached__', 
'__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']
>>> import inotify.library
>>> print(dir(inotify.library))
['_FILEPATH', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', 
'__spec__',
 'ctypes', 'instance']
Let's test it with a simple watch issue for a file named test_file.
First, let create this file:
[mythcat@desk ~]$ touch /tmp/test_file
[mythcat@desk ~]$ ll /tmp/test_file 
-rw-rw-r--. 1 mythcat mythcat 0 Aug 22 13:54 /tmp/test_file
Now, I can use the next script to watch on it:
import inotify.adapters
def notif_tmp():
    adapter_tmp = inotify.adapters.Inotify()

    adapter_tmp.add_watch('/tmp')

    with open('/tmp/test_file', 'w'):
        pass

    events = adapter_tmp.event_gen(yield_nones=False, timeout_s=1)
    events = list(events)

    print(events)
if __name__ == '__main__':
    notif_tmp()
The result is this:
$ python3 notif.py 
[(_INOTIFY_EVENT(wd=1, mask=2, cookie=0, len=16), ['IN_MODIFY'], '/tmp', 'test_file'), 
(_INOTIFY_EVENT(wd=1, mask=32, cookie=0, len=16), ['IN_OPEN'], '/tmp', 'test_file'), 
(_INOTIFY_EVENT(wd=1, mask=8, cookie=0, len=16), ['IN_CLOSE_WRITE'], '/tmp', 'test_file')]
If remove this file the result will be this:
$ python3 notif.py 
[(_INOTIFY_EVENT(wd=1, mask=256, cookie=0, len=16), ['IN_CREATE'], '/tmp', 'test_file'), 
(_INOTIFY_EVENT(wd=1, mask=32, cookie=0, len=16), ['IN_OPEN'], '/tmp', 'test_file'), 
(_INOTIFY_EVENT(wd=1, mask=8, cookie=0, len=16), ['IN_CLOSE_WRITE'], '/tmp', 'test_file')]
Let's test another example:
import inotify.adapters
import logging
_LOGGER = logging.getLogger(__name__)
def test():
    i = inotify.adapters.Inotify()
    i.add_watch('/tmp')

    try:
        for event in i.event_gen():
            if event is not None:
                (header, type_names, path, filename) = event
                _LOGGER.info("WD=(%d) MASK=(%d) COOKIE=(%d) LEN=(%d) MASK->NAMES=%s "
                             "FILENAME=[%s]",
                             header.wd, header.mask, header.cookie, header.len, type_names,
                             filename)
                print(header, type_names, path, filename)
    finally:
        i.remove_watch('/tmp')

if __name__ == '__main__':
    test()
If you run this and will try to edit with vim editor the /tmp/text.txt file the output will be this:
$ python3 notif_002.py
_INOTIFY_EVENT(wd=1, mask=32, cookie=0, len=16) ['IN_OPEN'] /tmp text.txt
_INOTIFY_EVENT(wd=1, mask=256, cookie=0, len=16) ['IN_CREATE'] /tmp .text.txt.swp
_INOTIFY_EVENT(wd=1, mask=32, cookie=0, len=16) ['IN_OPEN'] /tmp .text.txt.swp
...
If you want to use many paths then use this:
    paths = [
        '/tmp',
    ]
    i = Inotify(paths=paths)
This module is not well documented but can be successfully used for certain tasks.

Wednesday, August 21, 2019

Python Qt5 : contextMenu example.

A context menu is a menu in a graphical user interface (GUI) that appears upon user interaction, such as a right-click mouse operation.
I create the default application and I use QMenu to create this context menu with New, Open and Quit.
from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu
import sys


class Window(QMainWindow):
    def __init__(self):
        super().__init__()
        self.title = "PyQt5 Context Menu"
        self.top = 100
        self.left = 100
        self.width = 640
        self.height = 480
        self.InitWindow()


    def InitWindow(self):
        self.setWindowIcon(QtGui.QIcon("icon.png"))
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.show()

    def contextMenuEvent(self, event):
        contextMenu = QMenu(self)
        new_Act = contextMenu.addAction("New")
        open_Act = contextMenu.addAction("Open")
        quit_Act = contextMenu.addAction("Quit")
        action = contextMenu.exec_(self.mapToGlobal(event.pos()))
        if action == quit_Act:
            self.close()


App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())

Tuesday, August 20, 2019

Python Qt5 : the QTimer class.

I haven't written about PyQt5 in a while and today I decided to add a short tutorial on this python module.
The QTimer class is a high-level programming interface for timers and provides repetitive and single-shot timers.
I this example I call a method every second with these lines:
        self.timer = QTimer()
        self.timer.timeout.connect(self.handleTimer)
        self.timer.start(1000)
The timer is stop when the value check by handleTimer has value 100 else the ProgressBar increment the default value with 1.
The full source of code is this:
import sys
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QApplication, QMainWindow, QProgressBar
from PyQt5.QtCore import Qt

class QTimer_ProgressBar(QMainWindow):

    def __init__(self):
        super().__init__()

        self.pbar = QProgressBar(self)
        self.pbar.setGeometry(30, 70,400, 50)
        self.pbar.setValue(0)

        self.setWindowTitle("QTimer Progressbar")
        self.setGeometry(64,64,640,480)
        self.show()

        self.timer = QTimer()
        self.timer.timeout.connect(self.handleTimer)
        self.timer.start(1000)

    def handleTimer(self):
        value = self.pbar.value()
        if value < 100:
            value = value + 1
            self.pbar.setValue(value)
        else:
            self.timer.stop()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = QTimer_ProgressBar()
    sys.exit(app.exec_())
You can also use it like this:
def Qt():
    try:
        # Do things
    finally:
        QTimer.singleShot(5000, Qt)

Qt()

Wednesday, August 14, 2019

Python 3.7.3 : Using the flask - part 014.

Today I worked on YouTube search with flask and Google A.P.I. project.
The source code is simple to understand and you can test any A.P.I. from google using this way.
I created a new Google project with YouTube A.P.I. version 3 and with the A.P.I. key.
I use this key to connect with flask python module.
I used the isodate python module.
You can see the source code on my GitHub repo named flask_yt.
The result is this:

Friday, August 9, 2019

Python 3.7.3 : Using the flask - part 013.

Flask uses Jinga2 template engine.
The Jinga2 template engine uses the following delimiters for escaping from HTML.
We can use this:
  • {% ... %} for Statements
  • {{ ... }} for Expressions to print to the template output
  • {# ... #} for Comments not included in the template output
  • # ... ## for Line Statements
The documentation webpage comes with all information about how can be used.
I create a new HTML5 file named layout.html and I will use this like an example:
{% if current_user.is_authenticated %}
...
{% else %}
...
{% endif %}
This file can be add into another file like this:
{% extends "layout.html" %}
{% block content %}
...
{% endblock content %}
For example, my about.html webpage comes with this source code:
{% extends "layout.html" %}
{% block content %}
    

About Page

{% endblock content %}
The routes.py this webpage will have this call:
@app.route("/about")
def about():
    return render_template('about.html', title='About')
In this way, I will add more HTML5 files to the project.


Thursday, August 8, 2019

Python 3.7.3 : Using the flask - part 012.

The goal of this tutorial step is to understand how the project can use the new features and implementation of the project.
Because in the last tutorial I used the flask_mail python module, now I will add into my project structure.
One good issue is registration issue for users.
First, you need to see the full project and changes at my GitHub project.
I used the itsdangerous python module to use tokens.
Let's install this with the pip tool:
C:\Python373\Scripts>pip install itsdangerous
For registration is need to have login issues and we can use LoginManager, see the extensions.py
#use SQLAlchemy
from flask_sqlalchemy import SQLAlchemy
#use LoginManager
from flask_login import LoginManager
#create login_manager
login_manager = LoginManager()
#create db
db = SQLAlchemy()
Let's see the new User model, see the models.py:
#imports for user model
from datetime import datetime
from .extensions import db
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from blue_test import login_manager
from flask_login import UserMixin

# import db from base folder, see dot
from .extensions import db 

#load the user by id
@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))

# create the Texts mode
class Texts(db.Model):
    # primary key 
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(50))
    txt_content = db.Column(db.String(1000))
    
# create the User model
'''
id = id key for user
username = name
email = user email
password = user password

'''
class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    password = db.Column(db.String(60), nullable=False)

    def get_reset_token(self, expires_sec=1800):
        s = Serializer(app.config['SECRET_KEY'], expires_sec)
        return s.dumps({'user_id': self.id}).decode('utf-8')

    @staticmethod
    def verify_reset_token(token):
        s = Serializer(app.config['SECRET_KEY'])
        try:
            user_id = s.loads(token)['user_id']
        except:
            return None
        return User.query.get(user_id)

    def __repr__(self):
        return f"User('{self.username}', '{self.email}')"
In the blue_test folder project I created the templates folder with two HTML5 files: home.html and register.html.
I created also the forms.py file and I update with my forms: RegistrationForm,RequestResetForm, ResetPasswordForm.
#import python for create forms 
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField
# import python for validate forms field
from wtforms.validators import DataRequired, Length, Email, EqualTo, ValidationError
#import python files project
from .extensions import db, login_manager

class RegistrationForm(FlaskForm):
    username = StringField('Username',
                           validators=[DataRequired(), Length(min=2, max=25)])
    email = StringField('Email',
                        validators=[DataRequired(), Email()])
    password = PasswordField('Password', validators=[DataRequired()])
    confirm_password = PasswordField('Confirm Password',
                                     validators=[DataRequired(), EqualTo('password')])
    submit = SubmitField('Sign Up')

    def validate_username(self, username):
        user = User.query.filter_by(username=username.data).first()
        if user:
            raise ValidationError('That username is taken.')

    def validate_email(self, email):
        user = User.query.filter_by(email=email.data).first()
        if user:
            raise ValidationError('That email is taken.')

class RequestResetForm(FlaskForm):
    email = StringField('Email',
                        validators=[DataRequired(), Email()])
    submit = SubmitField('Password Reset')

    def validate_email(self, email):
        #get user by email
        
        if user is None:
            raise ValidationError('There is no account with that email.')

class ResetPasswordForm(FlaskForm):
    password = PasswordField('Password', validators=[DataRequired()])
    confirm_password = PasswordField('Confirm Password',
                                     validators=[DataRequired(), EqualTo('password')])
    submit = SubmitField('Reset Password')
In the __init__.py file I add this:
#import python files project
from .extensions import db, login_manager
...
    login_manager.init_app(app)
The routers.py will have the new RegistrationForm:
@api.route("/register", methods=['GET', 'POST'])
def register():
    # check if the user is authenticated
    if current_user.is_authenticated:
        return redirect(url_for('home'))
    form = RegistrationForm()
    if form.validate_on_submit():
        flash('Your account has been created!, 'success')
        return redirect(url_for('login'))
    return render_template('register.html', title='Register', form=form)
Depending on your needs, we will send and redirect these forms to the HTML files.
Depending on your needs (users, page access, ...) we will be able to write the source code further.

Wednesday, August 7, 2019

Python 3.7.3 : Using the flask - part 011.

The tutorial for today is focused on the email issue.
I will start with the new python module for flask named flask_mail.
Let's install it:
C:\Python373>cd Scripts

C:\Python373\Scripts>pip3 install flask_mail
Collecting flask_mail
...
Installing collected packages: blinker, flask-mail
Successfully installed blinker-1.4 flask-mail-0.9.1
The next source code let show you how can use this python module.
from flask import Flask 
from flask_mail import Mail, Message

app = Flask(__name__)
app.config['DEBUG'] = True
app.config['TESTING'] = False
app.config['MAIL_SERVER'] = 'smtp...'
app.config['MAIL_PORT'] = 25
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = False
#app.config['MAIL_DEBUG'] = True # debug 
app.config['MAIL_USERNAME'] = None
app.config['MAIL_PASSWORD'] = None
# this will send with the name of another@mail.com
#app.config['MAIL_DEFALT_SENDER'] = ('Another mail', 'another@mail.com')
app.config['MAIL_DEFALT_SENDER'] = None
app.config['MAIL_MAX_EMAILS'] = None # limit the messages send
#app.config['MAIL_SUPRESS_SEND'] = False # testing
app.config['MAIL_ASCII_ATTACHMENTS'] = False

# send an email with the flask application
mail = Mail(app)

@app.route('/mail')
def mail():
    #msg = Message('Hey', sender='another@mail.com')
    #msg = Message('Hey', recipients=['catafest@yahoo.com', 'another@mail.com'])
    #msg.add_recipient = ('another@mail.com')
    msg = Message('Hey', recipients=['catafest@yahoo.com'])

    # you can use body or HTML, using both wills receive the HTML first
    #msg.body = 'This is a body text message!'
    msg.html = 'This is a body text message with HTML5 tags!'

    #add a file attachment to message 
    with app.open_resource('photo.jpg') as add_file_res:
         msg.attach('photo.jpg', 'image/jpeg', add_file_res.read())
    '''
    #create a template message 
    msg = Message(
    subject ='',
    recipients=[],
    body = '',
    html = '',
    sender = '',
    cc = [],
    bcc = [],
    attachments = [],
    reply_to = [],
    date = '',
    charset = [],
    extra_headers = {'':''},
    mail_options = [],
    rcpt_options = []
    )
    '''
    #send mail
    mail.send(msg)
    return 'Message sent!'

@app.route('/bulk')
def bulk():
    users = [{'name':'Me', 'email':'another@mail.com'}]
    # open an connection 
    with mail.connect() as con:
         for user in users:
             msg = Message('Bulk message!', recipients=[user['email']]
             msg.body = 'Body message!'
             con.send(msg)
'''
# another example 
@app.route('/bulk')
def bulk():
    users = [{'name':'Me', 'email':'another@mail.com'}]
    # open an connection 
    with mail.connect() as con:
         for user in users:
             msg = Message('Bulk message!', recipients=[user.email]
             msg.body = 'Body message!'
             con.send(msg)
'''
if __name__ == '__main__':
    app.run()
You can see you need to set the settings for your mail server and then use it into flaks application.
The source code is easy to understand if you follow the commented rows.
I add also put into comments alternative examples for the template message and the bulk function.
This is the first step into sending emails with flask.
We can have a complete implementation on the project but all depends on project structure.