analitics

Pages

Showing posts with label 2.7. Show all posts
Showing posts with label 2.7. Show all posts

Monday, September 18, 2017

YARA another python module - part 002 .

This is another part of YARA python tutorial and the goal of this part is to install the Yara modules.
This is another python module about Yara named yara-python from VirusTotal.
The last tutorial uses the Yara python module.
The YARA modules provide extending features to allow us to define data structures and functions which can be used in your rules to express more complex conditions.
You can also write your own modules.
Some known modules used by YARA are:
  • PE
  • ELF
  • Cuckoo
  • Magic
  • Hash
  • Math
First, you need to install or reinstall YARA to the last version:
>>> yara.__version__
'3.6.3'
The Cuckoo module enables you to create YARA rules based on behavioral information generated by a Cuckoo sandbox.
C:\Python27\Scripts>pip install yara-python
Collecting yara-python
  Downloading yara_python-3.6.3-cp27-cp27m-win32.whl (606kB)
    100% |################################| 614kB 1.3MB/s
Installing collected packages: yara-python
Successfully installed yara-python-3.6.3
pip install cuckoo
Collecting cuckoo
  Downloading Cuckoo-2.0.4.4.tar.gz (3.1MB)
    100% |################################| 3.1MB 255kB/s
...
Successfully installed Mako-1.0.7 alembic-0.8.8 androguard-3.0.1 beautifulsoup4-4.5.3 
capstone-windows-3.0.4 chardet-2.3.0 click-6.6 colorama-0.3.7 cuckoo-2.0.4.4 django-1.8.4 
django-extensions-1.6.7 dpkt-1.8.7 ecdsa-0.13 egghatch-0.2.1 elasticsearch-5.3.0 
flask-sqlalchemy-2.1 httpreplay-0.2.1 jsbeautifier-1.6.2 jsonschema-2.6.0 olefile-0.43 
oletools-0.42 peepdf-0.3.6 pefile2-1.2.11 pillow-3.2.0 pyelftools-0.24 pymisp-2.4.54 
pymongo-3.0.3 python-dateutil-2.4.2 python-editor-1.0.3 python-magic-0.4.12 pythonaes-1.0 
requests-2.13.0 sflock-0.2.16 sqlalchemy-1.0.8 tlslite-ng-0.6.0 unicorn-1.0.1 wakeonlan-0.2.2
Let's test this python module:
>>> import cuckoo
>>> from cuckoo import *
>>> dir(cuckoo)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__version__',
 'auxiliary', 'common', 'compat', 'core', 'machinery', 'misc', 'plugins', 'processing', 
'reporting', 'signatures', 'web']
Let's test some yara modules:
>>> import yara
>>> rule = yara.compile(source='import \"pe\"')
>>> rule = yara.compile(source='import \"elf\"')
>>> rule = yara.compile(source='import \"cuckoo\"')
>>> rule = yara.compile(source='import \"math\"')
I could not use the YARA modules: hash and magic.
I will solve this problem in the future.
You can also write your own modules ( see this webpage ).

Friday, September 1, 2017

The beauty of Python: subprocess module - part 004 .

This series of python tutorials that we started at the beginning of this blog and called "The beauty of Python" is part of the series of tutorials aimed at the simplicity and beauty of the Python programming language.
The main goal for us is how to use this programming language in everyday life with different tasks.
Today I will come up with examples to cover this goal and show you how to use the subprocess python module.
  • using the PowerShell with python :
  • >>> import subprocess
    >>> process=subprocess.Popen(["powershell","Get-Childitem C:\\Windows\\*.log"],stdout=subprocess.PIPE);
    >>> result=process.communicate()[0]
    >>> print result
  • get and print the hostname :
  • >>> print subprocess.check_output("hostname")
  • print the output of ping command :
  • >>> print subprocess.check_output("ping localhost", shell=True)
  • print the output of dir command :
  • >>> cmd = 'dir *'
    >>> supcmd = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    >>> print supcmd.communicate()[0]
    
  • run the python script like python shell :
  • >>> import sys
    >>> import subprocess
    >>> pid = subprocess.Popen([sys.executable, "calc.py"])

Monday, August 21, 2017

Using pip into shell to install and use pymunk.

The tutorial for today will show how to use pip into the python shell to install a python package.
The first step is shown in the next image:

Friday, August 18, 2017

The Google Cloud SDK - part 002 .

The next part of my tutorials about the Google Cloud SDK comes with some info about the project.
As you know I used the default sample app engine hello word standard application.
The goal is to understand how it works by working with Google's documentation and examples.
Into this project folder we have this files:
08/17/2017  11:12 PM                98 app.yaml
08/17/2017  11:12 PM               854 main.py
08/17/2017  11:12 PM               817 main_test.py
Let's see what these files contain:
First is app.yaml and come with:
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /.*
  script: main.app
The next is main.py file:
# Copyright 2016 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import webapp2


class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.write('Hello, World!')


app = webapp2.WSGIApplication([
    ('/', MainPage),
], debug=True)
The last from this folder is main_test.py :
# Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import webtest

import main


def test_get():
    app = webtest.TestApp(main.app)

    response = app.get('/')

    assert response.status_int == 200
    assert response.body == 'Hello, World!'
The app.yaml file is used to configure your App Engine application's settings of the project.
You can have many application-level configuration files (dispatch.yaml, cron.yaml, index.yaml, and queue.yaml).
This all type of configuration files are included in the top level app directory ( in this case: hello_world).
Let's see some common gcloud commands:
  • gcloud app deploy  --project XXXXXX - deploy your project;
  • gcloud app browse - show your project running into your browser;
  • gcloud components list - show all available components;
  • gcloud components update - update all gcloud components;
  • gcloud projects list --limit=10 - show all projects with a limit number;
Let's test some changes:
First, change the text from main.py file with something else:
self.response.write('Hello, World!')
Now use this commands:
C:\Python27\python-docs-samples\appengine\standard\hello_world>gcloud app deploy
C:\Python27\python-docs-samples\appengine\standard\hello_world>gcloud app browse
The result is shown in your browser.
You can read about this files into google documentation page - here.
Also some gcloud commands and reference you can read here.

Thursday, August 17, 2017

The Google Cloud SDK - part 001 .

This tutorial will cover this steps into development with Google Cloud SDK and Python version 2.7:

  • install the Google Cloud SDK on the computer;
  • make settings online for your Google project to use Google Cloud SDK;
  • run the online project of Google Cloud SDK;
  • make setting into your computer to run the local project ;

First, you need to download the Google Cloud SDK and run it.


After GUI install a window command will ask you to set the default project for your work.
Welcome to the Google Cloud SDK! Run "gcloud -h" to get the list of available commands.
---
Welcome! This command will take you through the configuration of gcloud.

Your current configuration has been set to: [default]

You can skip diagnostics next time by using the following flag:
  gcloud init --skip-diagnostics

Network diagnostic detects and fixes local network connection issues.
Checking network connection...done.
Reachability Check passed.
Network diagnostic (1/1 checks) passed.

You must log in to continue. Would you like to log in (Y/n)?  Y
...
The next step is to start online to deploying a Hello World app with: Deploy a Hello World app:

This will start an online tutorial into the right area of the screen with all commands and steps for your Google Cloud SDK online project.
Follow this steps and in the end will see how the online Google Cloud SDK project will show: Hello, World! into your browser.
The next step is to make a local project and run it.
You can use the python docs sample from GoogleCloudPlatform but is not the same with the online example.
To download the GoogleCloudPlatform sample use git command:
C:\Python27>git clone https://github.com/GoogleCloudPlatform/python-docs-samples
Cloning into 'python-docs-samples'...
remote: Counting objects: 12126, done.
remote: Compressing objects: 100% (16/16), done.
remote: Total 12126 (delta 1), reused 10 (delta 1), pack-reused 12106
Receiving objects: 100% (12126/12126), 3.37 MiB | 359.00 KiB/s, done.
Resolving deltas: 100% (6408/6408), done.

C:\Python27>cd python-docs-samples/appengine/standard/hello_world
To start this sample into your google project you need to use this:
C:\Python27\python-docs-samples\appengine\standard\hello_world>gcloud app deploy app.yaml --project encoded-metrics-147522
Services to deploy:

descriptor:      [C:\Python27\python-docs-samples\appengine\standard\hello_world\app.yaml]
source:          [C:\Python27\python-docs-samples\appengine\standard\hello_world]
target project:  [encoded-metrics-147522]
target service:  [default]
target version:  [20170817t234925]
target url:      [https://encoded-metrics-147522.appspot.com]


Do you want to continue (Y/n)?  Y

Beginning deployment of service [default]...
#============================================================#
#= Uploading 5 files to Google Cloud Storage                =#
#============================================================#
File upload done.
Updating service [default]...done.
Waiting for operation [apps/encoded-metrics-147522/operations/XXXXXX] to complete...done.
Updating service [default]...done.
Deployed service [default] to [https://XXXXXX.appspot.com]

You can stream logs from the command line by running:
  $ gcloud app logs tail -s default

To view your application in the web browser run:
  $ gcloud app browse

C:\Python27\python-docs-samples\appengine\standard\hello_world>gcloud app browse
Opening [https://XXXXXX.appspot.com] in a new tab in your default browser.

C:\Python27\python-docs-samples\appengine\standard\hello_world>
This will start your application with the text - Hello, World! into your browser address bar with this web address: XXXXXX.appspot.com.



Wednesday, August 16, 2017

The DreamPie - interactive shell .

The DreamPie was designed to bring you a great interactive shell Python experience.
There are two ways to install the DreamPie:
  • cloning the git repository;
  • downloading a release.
You can read about installation and download here.
To run it just try the dreampie.exe with your python shell, I used with my python 2.7 version:
C:\DreamPie>dreampie.exe --hide-console-window c:\Python27\python.exe
Let's see one screenshot of this running command:

Also, I tested with Python 3.6.2 and works well.
The main window is divided into the history box and the code box.
The history box lets you view previous commands and their output.
The code box for writing your code.
Some keys I used:

  • Ctr+Enter - run the code;
  • Ctr+up / down arrow - adds the previous / next source code;
  • Ctr+Space - show code completions;
  • Ctr+T - open a new tab code;
  • Ctr+W - close the tab code;
  • Ctr+S - save your work history into HTML file.

You can set your font, colors and many features.
I make the installation into C:\DreamPie folder, and comes with all these folders and files:
C:\DreamPie>tree
Folder PATH listing for volume free-tutorials
Volume serial number is 000000FF 0EB1:091D
C:.
├───data
│   ├───language-specs
│   ├───subp-py2
│   │   └───dreampielib
│   │       ├───common
│   │       └───subprocess
│   └───subp-py3
│       └───dreampielib
│           ├───common
│           └───subprocess
├───gtk-2.0
│   ├───cairo
│   ├───gio
│   ├───glib
│   ├───gobject
│   ├───gtk
│   └───runtime
│       ├───bin
│       ├───etc
│       │   ├───bash_completion.d
│       │   ├───fonts
│       │   ├───gtk-2.0
│       │   └───pango
│       ├───lib
│       │   ├───gdk-pixbuf-2.0
│       │   │   └───2.10.0
│       │   │       └───loaders
│       │   ├───glib-2.0
│       │   │   └───include
│       │   └───gtk-2.0
│       │       ├───2.10.0
│       │       │   └───engines
│       │       ├───include
│       │       └───modules
│       └───share
│           ├───aclocal
│           ├───dtds
│           ├───glib-2.0
│           │   ├───gdb
│           │   ├───gettext
│           │   │   └───po
│           │   └───schemas
│           ├───gtk-2.0
│           ├───gtksourceview-2.0
│           │   ├───language-specs
│           │   └───styles
│           ├───icon-naming-utils
│           ├───themes
│           │   ├───Default
│           │   │   └───gtk-2.0-key
│           │   ├───Emacs
│           │   │   └───gtk-2.0-key
│           │   ├───MS-Windows
│           │   │   └───gtk-2.0
│           │   └───Raleigh
│           │       └───gtk-2.0
│           └───xml
│               └───libglade
└───share
    ├───applications
    ├───man
    │   └───man1
    └───pixmaps

Saturday, August 12, 2017

The flickrapi python module .

Flickr is one of the most popular photo hosting websites.
The development team let us test the API and with a plenty of opportunities to use Flickr.
Today I tested the flickrapi python module.
I used the python 2.7 version and pip to install it.
C:\>cd Python27

C:\Python27>cd Scripts

C:\Python27\Scripts>pip install flickrapi
Collecting flickrapi
  Downloading flickrapi-2.3-py2-none-any.whl
...
  Running setup.py install for oauthlib ... done
Successfully installed flickrapi-2.3 oauthlib-2.0.2 requests-oauthlib-0.8.0 requests-toolbelt-0.8.0
You need to have/make an API key into Account Settings, see my API key:
Your API keys You have 1 API key registered to this account.
Then you need to know your user_id.
This can be done by asking api online with your username and the result will be something like this: xxxxxxxx@Nxx.
The example I used is from documentation and I change with my API key to test this python module.


>>>import flickrapi
>>>
>>>api_key = u'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
>>>api_secret = u'YYYYYYYYYYYYYYYYYYYYYYY'
>>>
>>>flickr = flickrapi.FlickrAPI(api_key, api_secret)
>>>photos = flickr.photos.search(user_id='xxxxxxxx@Nxx', per_page='10')
>>>sets = flickr.photosets.getList(user_id='xxxxxxxx@Nxx')
>>> dir(sets)
['__class__', '__contains__', '__copy__', '__deepcopy__', '__delattr__', '__delitem__', '__doc__',
 '__format__', '__getattribute__', '__getitem__', '__hash__', '__init__', '__iter__', '__len__', 
'__new__', '__nonzero__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__setattr__',
 '__setitem__', '__sizeof__', '__str__', '__subclasshook__', '_init', 'addnext', 'addprevious', 
'append', 'attrib', 'base', 'clear', 'cssselect', 'extend', 'find', 'findall', 'findtext', 'get', 
'getchildren', 'getiterator', 'getnext', 'getparent', 'getprevious', 'getroottree', 'index', 'insert',
 'items', 'iter', 'iterancestors', 'iterchildren', 'iterdescendants', 'iterfind', 'itersiblings', 
'itertext', 'keys', 'makeelement', 'nsmap', 'prefix', 'remove', 'replace', 'set', 'sourceline', 
'tag', 'tail', 'text', 'values', 'xpath']
>>> print sets.attrib['stat']
ok
>>> sets.find('photosets').attrib
{'total': '4', 'perpage': '4', 'page': '1', 'pages': '1'}
>>> print "upload a photo via browser - set permisions"
upload a photo via browser - set permisions
>>> flickr.authenticate_via_browser(perms='write')
127.0.0.1 - - [12/Aug/2017 22:05:00] "GET /?oauth_token=xxxxxx-xxxxx&oauth_verifier=xxxxxxxxx HTTP/1.1" 200 -
>>> flickr.upload("0001.gif")

>>> print "try show title of my photos and id"
try show title of my photos and id
for child in photos:
  for i in child:
    photo_set = []
    try:
      title = i.attrib['title']
      print title
    except Exception, e:
      print e
    sets = flickr.photos_getAllContexts(photo_id = i.attrib["id"])
    for j in sets:
      if j.tag == "set":
        photo_set.append(j.attrib["id"])
        print photo_set
The result is my photos title and id:
painting_world
DSC_0112
['72157632077721715']
DSC_0111
['72157632077721715']
DSC_0113
['72157632077721715']
DSC_0110
['72157632077721715']
DSC_0094
['72157632077721715']
DSC_0091
['72157632077721715']
DSC_0108
I tested wit Python 362 version:
C:\Python27>cd ..

C:\>cd Python362

C:\Python362>cd Scripts

C:\Python362\Scripts>pip install flickrapi
Collecting flickrapi
  Downloading flickrapi-2.3-py3-none-any.whl
Collecting requests>=2.2.1 (from flickrapi)
  Downloading requests-2.18.3-py2.py3-none-any.whl (88kB)
    100% |████████████████████████████████| 92kB 1.4MB/s
Collecting six>=1.5.2 (from flickrapi)
  Using cached six-1.10.0-py2.py3-none-any.whl
...
Successfully installed certifi-2017.7.27.1 chardet-3.0.4 flickrapi-2.3 idna-2.5 oauthlib-2.0.2 requests-2.18.3
 requests-oauthlib-0.8.0 requests-toolbelt-0.8.0 six-1.10.0 urllib3-1.22

Thursday, August 10, 2017

The pysolar - python module.

Today I tested another python module named pysolar with python 2.7 and 3.6.2 versions.
You can read more about this python module here:
Pysolar is a collection of Python libraries for simulating the irradiation of any point on earth by the sun. It includes code for extremely precise ephemeris calculations and more.
Using pip under Windows OS to install this python module:
C:\Python27\Scripts>pip install pysolar
...
Successfully built pysolar
Installing collected packages: pysolar
Successfully installed pysolar-0.7
I try also with python 3.6.2 version:
C:\Python362\Scripts>pip install pysolar
Collecting pysolar
  Using cached pysolar-0.7.tar.gz
Installing collected packages: pysolar
  Running setup.py install for pysolar ... done
Successfully installed pysolar-0.7
Let's start with a simple example to calculate the angle between the sun and a plane tangent to the earth where
you are:
C:\Python362>python.exe
Python 3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from pysolar.solar import *
>>> import datetime
>>> my_data = datetime.datetime.now()
>>> print (my_data)
2017-08-10 18:12:01.997373
>>> get_altitude_fast(47.4616,26.3015,datetime.datetime.now())
-6.393190003993212
Let's try the scattering of light by the atmosphere, though it uses an
atmospheric model based on data taken in the United States:
>>> latitude_deg =47.2741
>>> longitude_deg = 26.185
>>> my_data = datetime.datetime.now()
>>> altitude_deg = get_altitude(latitude_deg, longitude_deg,my_data)
C:\Python362\lib\site-packages\pysolar\time.py:105: UserWarning: I don't know about leap seconds after 2015
  (leap_seconds_base_year + len(leap_seconds_adjustments) - 1)
>>> azimuth_deg = get_azimuth(latitude_deg, longitude_deg,  my_data)
>>> radiation.get_radiation_direct(my_data, altitude_deg)
634.4916522219954
About this issue:I don't know about leap seconds after 2015 read here.

Using Flask and Flask-WTF to build websites in Python 2.7 and 3.6.2 - part 002.

This is one update of my old tutorial about Flask and python 2.7 under Linux from here.
The default install of python 2.7 and pip is very simple - read this tutorial.
First, you need to install the Flask python module under Windows 10:
C:\Python27\Scripts>pip install Flask
Collecting Flask
Downloading Flask-0.11.1-py2.py3-none-any.whl (80kB)
100% |################################| 81kB 564kB/s
Collecting click>=2.0 (from Flask)
Downloading click-6.6-py2.py3-none-any.whl (71kB)
100% |################################| 71kB 1.3MB/s
Collecting Werkzeug>=0.7 (from Flask)
Downloading Werkzeug-0.11.11-py2.py3-none-any.whl (306kB)
100% |################################| 307kB 231kB/s
Collecting Jinja2>=2.4 (from Flask)
Downloading Jinja2-2.8-py2.py3-none-any.whl (263kB)
100% |################################| 266kB 890kB/s
Collecting itsdangerous>=0.21 (from Flask)
Downloading itsdangerous-0.24.tar.gz (46kB)
100% |################################| 51kB 1.3MB/s
Collecting MarkupSafe (from Jinja2>=2.4->Flask)
Downloading MarkupSafe-0.23.tar.gz
Installing collected packages: click, Werkzeug, MarkupSafe, Jinja2, itsdangerous
, Flask
Running setup.py install for MarkupSafe ... done
Running setup.py install for itsdangerous ... done
Successfully installed Flask-0.11.1 Jinja2-2.8 MarkupSafe-0.23 Werkzeug-0.11.11
click-6.6 itsdangerous-0.24
The simple example of the running Flask python module is one script named hello.py:
from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run()
Using the command to run the script and show the result into your browser:
C:\Python27>python.exe hello.py
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Now the installation of Flask-WTF python module:
C:\Python27\Scripts>pip install Flask-WTF
Collecting Flask-WTF
  Downloading Flask_WTF-0.14.2-py2.py3-none-any.whl
Collecting WTForms (from Flask-WTF)
  Downloading WTForms-2.1.zip (553kB)
    100% |################################| 563kB 1.7MB/s
Requirement already satisfied: Flask in c:\python27\lib\site-packages (from Flask-WTF)
Requirement already satisfied: click>=2.0 in c:\python27\lib\site-packages (from Flask->Flask-WTF)
Requirement already satisfied: Werkzeug>=0.7 in c:\python27\lib\site-packages (from Flask->Flask-WTF)
Requirement already satisfied: Jinja2>=2.4 in c:\python27\lib\site-packages (from Flask->Flask-WTF)
Requirement already satisfied: itsdangerous>=0.21 in c:\python27\lib\site-packages (from Flask->Flask-WTF)
Requirement already satisfied: MarkupSafe>=0.23 in c:\python27\lib\site-packages (from Jinja2>=2.4->Flask->Flask-WTF)
Installing collected packages: WTForms, Flask-WTF
  Running setup.py install for WTForms ... done
Successfully installed Flask-WTF-0.14.2 WTForms-2.1
Now I try with Python 3.6.2 with both python modules and works great.
C:\Python362\Scripts>pip3.6.exe install flask
...
Successfully installed Jinja2-2.9.6 MarkupSafe-1.0 Werkzeug-0.12.2 click-6.7 flask-0.12.2 itsdangerous-0.24

C:\Python362\Scripts>Flask-WTF
'Flask-WTF' is not recognized as an internal or external command,
operable program or batch file.

C:\Python362\Scripts>pip3.6.exe install Flask-WTF
...
Successfully installed Flask-WTF-0.14.2 WTForms-2.1

Wednesday, July 26, 2017

The gtts python module.

This python module named gtts will create an mp3 file from spoken text via the Google TTS (Text-to-Speech) API.
The installation of the gtts python module under Windows 10.
C:\Python27\Scripts>pip install gtts
Collecting gtts
  Downloading gTTS-1.2.0.tar.gz
Requirement already satisfied: six in c:\python27\lib\site-packages (from gtts)
Requirement already satisfied: requests in c:\python27\lib\site-packages (from gtts)
Collecting gtts_token (from gtts)
  Downloading gTTS-token-1.1.1.zip
Requirement already satisfied: chardet<3 .1.0="">=3.0.2 in c:\python27\lib\site-packages (from requests->gtts)
Requirement already satisfied: certifi>=2017.4.17 in c:\python27\lib\site-packages (from requests->gtts)
Requirement already satisfied: idna<2 .6="">=2.5 in c:\python27\lib\site-packages (from requests->gtts)
Collecting urllib3<1 .22="">=1.21.1 (from requests->gtts)
  Using cached urllib3-1.21.1-py2.py3-none-any.whl
Installing collected packages: gtts-token, gtts, urllib3
  Running setup.py install for gtts-token ... done
  Running setup.py install for gtts ... done
  Found existing installation: urllib3 1.22
    Uninstalling urllib3-1.22:
      Successfully uninstalled urllib3-1.22
Successfully installed gtts-1.2.0 gtts-token-1.1.1 urllib3-1.21.1
Let's see a basic example:
from gtts import gTTS
import os
import pygame.mixer
from time import sleep
 
user_text=input("Type your text: ")
 
translate=gTTS(text=user_text ,lang='en')
translate.save('output.wav')

pygame.mixer.init()
path_name=os.path.realpath('output.wav')
real_path=path_name.replace('\\','\\\\')
pygame.mixer.music.load(open(real_path,"rb"))
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
    sleep(1)
The text will be taken by input into a user_text variable.
You need to type the text into quotes also you will get an error.
The result will be one audio file named output.wav and play it by pygame python module.
This uses the default voices for all languages. I don't find a way to change this voices with python.

Saturday, July 22, 2017

About py-translate python module.

This python module is used for translating text in the terminal.
You can read and see examples with this API on this web page.
Features

  • Fast! Translate an entire book in less than 5 seconds.
  • Made for Python 3 but still works on Python 2
  • Fast and easy to install, easy to use
  • Supports translation from any language
  • Highly composable interface, the power of Unix pipes and filters.
  • Simple API and documentation

Installation 
C:\>cd Python27

C:\Python27>cd Scripts

C:\Python27\Scripts>pip install py-translate
Collecting py-translate
  Downloading py_translate-1.0.3-py2.py3-none-any.whl (61kB)
    100% |################################| 61kB 376kB/s
Installing collected packages: py-translate
Successfully installed py-translate-1.0.3

C:\Python27\Scripts>
Let's test it with a simple example:
>>> import translate
>>> dir(translate)
['TestLanguages', 'TestTranslator', '__author__', '__build__', '__builtins__', '__copyright__', '__doc__', '__file__', '__license__', '__name__', '__package__', '__path__', '__title__', '__version__', 'accumulator', 'coroutine', 'coroutines', 'languages', 'print_table', 'push_url', 'set_task', 'source', 'spool', 'tests', 'translation_table', 'translator', 'write_stream']
>>> from translate import translator
>>> translator('ro', 'en', 'Consider ca dezvoltarea personala este un pas important')
[[[u'I think personal development is an important step', u'Consider ca dezvoltarea personala este un pas important', None, None, 0]], None, u'ro']
>>>

Monday, July 10, 2017

Python tutor - web tool for python programming.

The website comes with this intro about this web tool.
Python Tutor, created by Philip Guo, helps people overcome a fundamental barrier to learning programming: understanding what happens as the computer runs each line of source code.
Using this tool, you can write Python, Java, JavaScript, TypeScript, Ruby, C, and C++ code in your web browser and visualize what the computer is doing step-by-step as it runs your code.
Over 3.5 million people in over 180 countries have used Python Tutor to visualize over 30 million pieces of code, often as a supplement to textbooks, lectures, and online tutorials.

I tested and worked very well.
You can use python programming language 2.7 and 3.6 versions.
No need to import python modules, you will get an error.
Just programming on the fly to test and see the result.
The website comes with some example to see how to deal with this tool.
Let's see some examples:

example with factorial :

# dumb recursive factorial
def fact(n):
    if (n <= 1):
        return 1
    else:
        return n * fact(n - 1)

print(fact(6))

example with for - else:

# find primes using a for-else construct
for n in range(2, 10):
    x_range = range(2, n)
    for x in x_range:
        if n % x == 0:
            break
    else:
        # loop fell through without finding a factor
        print(n)

example with inputs:

prefix = "Hello "

n1 = raw_input("Enter your name")

n2 = raw_input("Enter another name")

res = prefix + n1 + " and " + n2
print(res)

Run your script just press: Visualize Execution or Live Programming Mode buttons and the will run step by step with:
First, Back, Forward and Last.
One good feature of this tool - with a single line of JavaScript code, you can embed a Python Tutor visualization within any webpage.
Another good feature is COLLABORATE to learn together - this allow us to give and get direction with real-time python programming.
Can be a good tool for python chat users.
Let's show you a screenshot to see how this tool working with python scripting.

Sunday, July 9, 2017

The speech python module.

About this python module can be read here.
It's a little undocumented and I have not found tutorials about this python module but I tested with a simple example.
I'm sure he can do more than I tried with my example.
First, the install of this python module:
C:\Python27\Scripts>pip install speech
Collecting speech
  Downloading speech-0.5.2.tar.gz
Installing collected packages: speech
  Running setup.py install for speech ... done
Successfully installed speech-0.5.2
Let's see more about this python module:
>>> dir(speech)
['Listener', '_ListenerBase', '_ListenerCallback', '__builtins__', '__doc__', '__file__', '__name__', '__package__'
, '_constants', '_ensure_event_thread', '_eventthread', '_handlerqueue', '_listeners', '_recognizer', 
'_startlistening', '_voice', 'gencache', 'input', 'islistening', 'listenfor', 'listenforanything', 'pythoncom',
 'say', 'stoplistening', 'thread', 'time', 'win32com']>>> help(speech)
Help on module speech:

NAME
    speech - speech recognition and voice synthesis module.

FILE
    c:\python27\lib\site-packages\speech.py

DESCRIPTION
    Please let me know if you like or use this module -- it would make my day!

    speech.py: Copyright 2008 Michael Gundlach  (gundlach at gmail)
    License: Apache 2.0 (http://www.apache.org/licenses/LICENSE-2.0)

    For this module to work, you'll need pywin32 (http://tinyurl.com/5ezco9
    for Python 2.5 or http://tinyurl.com/5uzpox for Python 2.4) and
    the Microsoft Speech kit (http://tinyurl.com/zflb).


    Classes:
        Listener: represents a command to execute when phrases are heard.

    Functions:
        say(phrase): Say the given phrase out loud.
        input(prompt, phraselist): Block until input heard, then return text.
        stoplistening(): Like calling stoplistening() on all Listeners.
        islistening(): True if any Listener is listening.
        listenforanything(callback): Run a callback when any text is heard.
        listenfor(phraselist, callback): Run a callback when certain text is heard.
Let's make a simple example with one script that tells us something:
>>> speech.say('Hello Catalin George')
The result of this line of code will be heard into your audio device like Hello Catalin George.

Saturday, July 8, 2017

Python Qt4 - part 006.

Today I will deal with QFileDialog widget.
You can read the more about this widget here.
This allows us to open a dialog to load a resource - a file.
The example comes with the base PyQt4 application window with a my_example dialog from fileDialogSample python class.
Into this python class, I have some variable for the file: file_name, data and my_file_open.
The my_text_edit for text area and my_button to open the QFileDialog.
Also, the vbox variable to put all on QVBoxLayout from the application.
Let's see the example:
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore

class fileDialogSample(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)

        #make text area and button
        self.my_text_edit = QtGui.QTextEdit()
        self.my_button = QtGui.QPushButton('Select File', self)

        #open the showDialog
        self.my_button.clicked.connect(self.showDialog)

        #put all into application area
        vbox = QtGui.QVBoxLayout()
        vbox.addWidget(self.my_text_edit)
        vbox.addWidget(self.my_button)
        self.setLayout(vbox)

        #set title and geometry of application
        self.setWindowTitle('File Dialog example')
        self.setGeometry(50, 50, 300, 300)

    #make a function with seeting for my QFileDialog
    def showDialog(self):
        file_name = QtGui.QFileDialog.getOpenFileName(self, 'Open file', 'C://')
        my_file_open = open(file_name)
        data = my_file_open.read()
        self.my_text_edit.setText(data)
        my_file_open.close()

#run the application 
app = QtGui.QApplication(sys.argv)
my_dialog = fileDialogSample()
my_dialog.show()
sys.exit(app.exec_())
Now, just press the Select File button, take a text file and open it.

Friday, July 7, 2017

Python Qt4 - part 005.

Another example with PyQt4 that allow seeing images over the internet.
Here's another simple example with PyQt4 that allows you to view images on the internet.
You can use any image on the internet to display with this python script.
This example is done in two steps:
  • take a single image from the internet - httplib python module;
  • displaying - PyQt4 python module
from PyQt4 import QtGui
import sys
import httplib

def getTempPNG():
   conn = httplib.HTTPConnection("www.meteoromania.ro")
   conn.request("GET", "/sateliti/img/id814/id814_2017070718.png")
   return conn.getresponse().read()

def main():
   app = QtGui.QApplication(sys.argv)
   png = getTempPNG()
   pixmap = QtGui.QPixmap()
   pixmap.loadFromData(png)
   label = QtGui.QLabel()
   label.setPixmap(pixmap)
   label.setWindowTitle('METEOSAT-10 Thermal Infrared Channel 10.8 micrometers Glowing temperature')
   label.show()
   app.exec_()

if __name__ == '__main__':
   main()
The result can be seen in this screenshot:

Python Qt4 - part 004.

Another tutorial about PyQt4 with QLCDNumber widget displays a number with LCD-like digits.
This tutorial will show you how to deal with this widget.
First, you need to know more about QLCDNumber, so take a look here.
The first example is very simple and will show just one digit, see:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Digit(QWidget):

    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.setWindowTitle("One digit")
        lcd = QLCDNumber(self)

app = QApplication(sys.argv)
ls = Digit()
ls.show()
sys.exit(app.exec_())
Now, the next step is to send data to this digit.
One good example is with one slider.
The position of the slider will be send to the QLCDNumber.
How can do that? Will need a vbox to put the QLCDNumber and the slider and then using signal and slot.
Let's see the example:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Digit(QWidget):

    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        #make widgets
        self.setWindowTitle("One digit with slider")
        lcd = QLCDNumber(self)
        slider = QSlider(Qt.Horizontal, self)
        #set layout variable vbox
        vbox = QVBoxLayout()
        #add widgests
        vbox.addWidget(lcd)
        vbox.addWidget(slider)
        #set the vbox to layout
        self.setLayout(vbox)
        #create signal to slot
        self.connect(slider, SIGNAL("valueChanged(int)"),lcd, SLOT("display(int)"))
        self.resize(200, 170)
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ls = Digit()
    ls.show()
    sys.exit(app.exec_())
In the source code in the example, you can see the comments that mark the steps of creating and executing the python script.
Let's try another example with a digital clock:
import sys
from PyQt4 import QtCore, QtGui

class digital_clock(QtGui.QLCDNumber):
    def __init__(self, parent=None):
        super(digital_clock, self).__init__(parent)
        self.setSegmentStyle(QtGui.QLCDNumber.Filled)
        #the defaul is 5 , change to 8 for seconds
        self.setDigitCount(5)
        self.setWindowTitle("Digital Clock")
        self.resize(200, 70)
        timer = QtCore.QTimer(self)
        timer.timeout.connect(self.showTime)
        timer.start(1000)
        self.showTime()

    def showTime(self):
        time = QtCore.QTime.currentTime()
        text = time.toString('hh:mm')
        #if you setDigitsCount to 8
        #uncomment the next line of code
        #text = time.toString('hh:mm:ss')
        if (time.second() % 2) == 0:
            text = text[:2] + ' ' + text[3:]
        self.display(text)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    clock = digital_clock()
    clock.show()
    sys.exit(app.exec_())
If you want to see seconds, then you need to set the digit count of the LCD to 8 ( it's 5 by default) of setDigitCount.
Also you need to uncomment this line of code: text = time.toString('hh:mm:ss') and comment the old one.
You can solve multiple issues with this widget, like: stopwatch, timer, clock down timer ...

Thursday, July 6, 2017

Python Qt4 - part 003.

Today I've taken a simple example with PyQt4 compared to the other tutorials we have done so far.
The main reason was to understand and use PyQt4 to display an important message.
To make this example I have set the following steps for my python program.
  • importing python modules
  • creating the application in PyQt4 as a tray icon class
  • establishing an exit from the application
  • setting up a message to display
  • display the message over a period of time
  • closing the application
  • running the python application
Let's see my source code of my python application:
#! /usr/bin/env python
import sys
from PyQt4 import QtGui, QtCore

class SystemTrayIcon(QtGui.QSystemTrayIcon):
    def __init__(self, parent=None):
        QtGui.QSystemTrayIcon.__init__(self, parent)
        self.setIcon(QtGui.QIcon("mess.svg"))
        menu = QtGui.QMenu(parent)
        exitAction = menu.addAction("Exit")
        self.setContextMenu(menu)
        QtCore.QObject.connect(exitAction,QtCore.SIGNAL('triggered()'), self.exit)

    def click_trap(self, value):
        if value == self.Trigger: #left click!
            self.left_menu.exec_(QtGui.QCursor.pos())

    def welcome(self):
        self.showMessage("Hello user!", "This is a message from PyQT4")

    def show(self):
        QtGui.QSystemTrayIcon.show(self)
        QtCore.QTimer.singleShot(600, self.welcome)

    def exit(self):
        QtCore.QCoreApplication.exit()

def main():
    app = QtGui.QApplication([])
    tray = SystemTrayIcon()
    tray.show()
    app.exec_()

if __name__ == '__main__':
    main()
I used PyQt4 python module to make the application and sys python module for exit from application.
About running application: the main function will run the application.
The python class SystemTrayIcon will work only if we used QApplication to make like any application.
This is the reason I used the variable app.
The tray variable is used to run it like tray icon application.
Into the SystemTrayIcon class, I put some functions to help me with my issue.
Under __init__ I used all settings for my tray icon application: the icon, the exit menu, signal for the exit.
The next functions come with:
  • click_trap - take the click of the user ;
  • welcome - make a message to display;
  • show - display the welcome message;
  • exit - exit from the application
The result of my python script is this message:

About the showMessage then this helps you:

QSystemTrayIcon.showMessage (self, QString title, QString msg, MessageIcon icon = QSystemTrayIcon.Information, int msecs = 10000)

Shows a balloon message for the entry with the given title, message and icon for the time specified in millisecondsTimeoutHint. title and message must be plain text strings.
The message can be clicked by the user; the messageClicked() signal will be emitted when this occurs.
Note that the display of messages is dependent on the system configuration and user preferences and that messages may not appear at all. Hence, it should not be relied upon as the sole means for providing critical information.
On Windows, the millisecondsTimeoutHint is usually ignored by the system when the application has focus.
On Mac OS X, the Growl notification system must be installed for this function to display messages.
This function was introduced in Qt 4.3.

Tuesday, July 4, 2017

The pdb python interactive debugging - part 001.

This is a short intro tutorial on python debugger to summarize this topic related to Python.
According to the development team, this python module called pdb has the following objectives:
The module pdb defines an interactive source code debugger for Python programs. It supports setting (conditional) breakpoints and single stepping at the source line level, inspection of stack frames, source code listing, and evaluation of arbitrary Python code in the context of any stack frame. It also supports post-mortem debugging and can be called under program control.

Let's start it with some example:
C:\Python27>python
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pdb
>>> pdb.pm()
Traceback (most recent call last):
  File "", line 1, in 
  File "C:\Python27\lib\pdb.py", line 1270, in pm
    post_mortem(sys.last_traceback)
AttributeError: 'module' object has no attribute 'last_traceback'
>>> import os
>>> pdb.pm()
> c:\python27\lib\pdb.py(1270)pm()
-> post_mortem(sys.last_traceback)
(Pdb) ?

Documented commands (type help ):
========================================
EOF    bt         cont      enable  jump  pp       run      unt
a      c          continue  exit    l     q        s        until
alias  cl         d         h       list  quit     step     up
args   clear      debug     help    n     r        tbreak   w
b      commands   disable   ignore  next  restart  u        whatis
break  condition  down      j       p     return   unalias  where

Miscellaneous help topics:
==========================
exec  pdb

Undocumented commands:
======================
retval  rv
As you can see, this module will work correctly with just another python module. In the example presented above is the os python module.
With the argument? we can see the commands that we can execute.
Let's see the list command ( l ):
(Pdb) l
1265        p = Pdb()
1266        p.reset()
1267        p.interaction(None, t)
1268
1269    def pm():
1270 ->     post_mortem(sys.last_traceback)
1271
1272
1273    # Main program for testing
1274
1275    TESTCMD = 'import x; x.main()'
You can see the pm function loaded by pdb python module.
Post-mortem debugging is a method that requires an environment that provides dynamic execution of code.
One of the greatest benefits of post-mortem debugging is that you can use it directly after something has gone wrong.
You can move between frames within the current call stack using up and down.
This moves towards older frames on the stack.
The debugger prints the current location with where, see example:
(Pdb) where
  (1)()
> c:\python27\lib\pdb.py(1270)pm()
-> post_mortem(sys.last_traceback)
To execute the current line and then stop at the next execution point use step.
The until command can be used to step past the end of a loop.
Use break command used for setting breakpoints, example: (Pdb) break 4.
Turning off a breakpoint with disabling tells the debugger not to stop when that line is reached, example: (Pdb) disable 1.
Also, we can have the other breakpoints like conditional breakpoints and temporary breakpoint.
Use clear to delete a breakpoint entirely.
Changing execution flow with the jump command lets you alter the flow of your program.
The jump can be ahead and back and moves the point of execution past the location without evaluating any of the statements in between.
We can also have illegal jumps in and out of certain flow control statements prevented by the debugger.
When the debugger reaches the end of your program, it automatically starts it over.
With run command, the program can be restarted.
We can avoid typing complex commands repeatedly by using alias and unalias to define the shortcuts.
The pdb python module lets you save configuration using text files read and interpreted on startup.

Wednesday, June 28, 2017

The Google API Client Library python module.

This python module named Google API Client Library for Python is a client library for accessing the Plus, Moderator, and many other Google APIs, according to the official link.
C:\Python27\Scripts>pip install --upgrade google-api-python-client
Collecting google-api-python-client
  Downloading google_api_python_client-1.6.2-py2.py3-none-any.whl (52kB)
    100% |################################| 61kB 426kB/s
...
Successfully installed google-api-python-client-1.6.2 ...
The example I used is this:
from oauth2client.client import flow_from_clientsecrets
import httplib2
import apiclient
from apiclient.discovery import build
from oauth2client.file import Storage
import webbrowser

def get_credentials():
    scope = 'https://www.googleapis.com/auth/blogger'
    flow = flow_from_clientsecrets(
        'client_id.json', scope,
        redirect_uri='urn:ietf:wg:oauth:2.0:oob')
    storage = Storage('credentials.dat')
    credentials = storage.get()

    if  not credentials or credentials.invalid:
        auth_uri = flow.step1_get_authorize_url()
        webbrowser.open(auth_uri)
        auth_code = raw_input('Enter the auth code: ')
        credentials = flow.step2_exchange(auth_code)
        storage.put(credentials)
    return credentials

def get_service():
    """Returns an authorised blogger api service."""
    credentials = get_credentials()
    http = httplib2.Http()
    http = credentials.authorize(http)
    service = apiclient.discovery.build('blogger', 'v3', http=http)
    return service

if __name__ == '__main__':
    served = get_service()
    print dir(served.blogs)
    users = served.users()

    # Retrieve this user's profile information
    thisuser = users.get(userId='self').execute()
    print('This user\'s display name is: %s' % thisuser['displayName'].encode('utf-8'))

    blogs = served.blogs()

    # Retrieve the list of Blogs this user has write privileges on
    thisusersblogs = blogs.listByUser(userId='self').execute()
    for blog in thisusersblogs['items']:
        print('The blog named \'%s\' is at: %s' % (blog['name'], blog['url']))
The result of this script is this:
C:\Python27>python.exe google_001.py
['__call__', '__class__', '__cmp__', '__delattr__', '__doc__', '__format__', '__func__', 
'__get__', '__getattribute__', '__hash__', '__init__', '__is_resource__', '__new__', 
'__reduce__', '__reduce_ex__', '__repr__', '__self__', '__setattr__', '__sizeof__', 
'__str__', '__subclasshook__', 'im_class', 'im_func', 'im_self']
This user's display name is: Cătălin George Feștilă
The blog named 'python-catalin' is at: http://python-catalin.blogspot.com/
The blog named 'graphics' is at: http://graphic-3d.blogspot.com/
The blog named 'About me and my life ...' is at: http://catalin-festila.blogspot.com/
The blog named 'pygame-catalin' is at: http://pygame-catalin.blogspot.com/
About google settings then you need to have a google account to use Google’s API.
The first step for accessing the Google Developer’s Console.
Then navigate to the Developer Console’s projects page and create a new project for our application by clicking the Create project button and then enable blogger API.
Enter your projects name and hit create.
Click the Go to Credentials button with these settings like in the next image:

Download this credential information in JSON format, in this case, is the client_id.json file.
When you run for the first time this script you will see an open HTML page with your auth code.
The script example named google_001.py will come with this message:
C:\Python27>python.exe google_001.py
C:\Python27\lib\site-packages\oauth2client\_helpers.py:255: UserWarning: Cannot access credentials.dat: No such file or directory
  warnings.warn(_MISSING_FILE_MESSAGE.format(filename))
Enter the auth code:
Put this auth code and allow the script using the open page and your google account using Allow button.
Now you can run the example.


The pyquery python module.

This tutorial is about pyquery python module and python 2.7.13 version.
First I used pip command to install it.
C:\Python27>cd Scripts

C:\Python27\Scripts>pip install pyquery
Collecting pyquery
  Downloading pyquery-1.2.17-py2.py3-none-any.whl
Requirement already satisfied: lxml>=2.1 in c:\python27\lib\site-packages (from pyquery)
Requirement already satisfied: cssselect>0.7.9 in c:\python27\lib\site-packages (from pyquery)
Installing collected packages: pyquery
Successfully installed pyquery-1.2.17
I try to install with pip and python 3.4 version but I got errors.
The development team tells us about this python module:
pyquery allows you to make jquery queries on xml documents. The API is as much as possible the similar to jquery. pyquery uses lxml for fast xml and html manipulation.
Let's try a simple example of this python module.
The base of this example is found links by HTML tag.
from pyquery import PyQuery
 
seeds = [
    'https://twitter.com',
    'http://google.com'
]
 
crawl_frontiers = []
 
def start_crawler():
    crawl_frontiers = crawler_seeds()
 
    print(crawl_frontiers)
 
def crawler_seeds():
    frontiers = []
    for index, seed in enumerate(seeds):
        frontier = {index: read_links(seed)}
        frontiers.append(frontier)
 
    return frontiers
 
def read_links(seed):
    crawler = PyQuery(seed)
    return [crawler(tag_a).attr("href") for tag_a in crawler("a")]
 
start_crawler()
The read_links function takes links from seeds array.
To do that, I need to read the links and put in into another array crawl_frontiers.
The frontiers array is used just for crawler process.
Also, this simple example allows you to understand better the arrays.
You can read more about this python module here.