How to get file creation & modification date/times in Python?

Getting some sort of modification date in a cross-platform way is easy – just call os.path.getmtime(path) and you’ll get the Unix timestamp of when the file at path was last modified.

Getting file creation dates, on the other hand, is fiddly and platform-dependent, differing even between the three big OSes:

Putting this all together, cross-platform code should look something like this…

import os
import platform

def creation_date(path_to_file):
    """
    Try to get the date that a file was created, falling back to when it was
    last modified if that isn't possible.
    See http://stackoverflow.com/a/39501288/1709587 for explanation.
    """
    if platform.system() == 'Windows':
        return os.path.getctime(path_to_file)
    else:
        stat = os.stat(path_to_file)
        try:
            return stat.st_birthtime
        except AttributeError:
            # We're probably on Linux. No easy way to get creation dates here,
            # so we'll settle for when its content was last modified.
            return stat.st_mtime

Solution 2:

You have a couple of choices. For one, you can use the os.path.getmtime and os.path.getctime functions:

import os.path, time
print("last modified: %s" % time.ctime(os.path.getmtime(file)))
print("created: %s" % time.ctime(os.path.getctime(file)))

Your other option is to use os.stat:

import os, time
(mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = os.stat(file)
print("last modified: %s" % time.ctime(mtime))

Note: ctime() does not refer to creation time on *nix systems, but rather the last time the inode data changed.

Solution 3:

The best function to use for this is os.path.getmtime(). Internally, this just uses os.stat(filename).st_mtime.

The datetime module is the best manipulating timestamps, so you can get the modification date as a datetime object like this:

import os
import datetime
def modification_date(filename):
    t = os.path.getmtime(filename)
    return datetime.datetime.fromtimestamp(t)

Usage example:

>>> d = modification_date('/var/log/syslog')
>>> print d
2009-10-06 10:50:01
>>> print repr(d)
datetime.datetime(2009, 10, 6, 10, 50, 1)

 

Source : How to get file creation & modification date/times in Python? – Stack Overflow

Running Jenkins in Windows with regular user account

Running Jenkins in Windows with regular user account to see executables in PATH

Went through a very irritating « feature » of Jenkins in Windows, the fact that it runs as the default system user.
From there you have to options:
– Change the system user to have the dependencies you need, or
– Use a regular account

I went with the later.

The symptom is that commands executed in the Build – Execute Windows batch command step will not be able to find executables, despite the fact that they are defined in %PATH%.

A good way to diagnose is to check that you really have the %PATH% properly set and identify who’s running the command. Use these two lines:

echo %PATH%
echo %USERDOMAIN%\%USERNAME%

To solve set the properties in the Jenkins service to use the user you wan and that should marry things just nicely. There are drawbacks in using user accounts like that but it’s much easier and have the expected behaviour.

Edit (aitek.ch): there are drawbacks doing this, like the fact that sometimes the service will give you a login failure, and you will have to go an set the password for the user again, then start the service again, but it works.

Source : Software Antagonistic Pleiotropy: Running Jenkins in Windows with regular user account to see executables in PATH

Python – Try and Except statements

The try and except statements in python helps us to mitigate exception errors with a meaning full way. To understand why we require try and except statements we should know how python handles execution and syntax errors.

There are two types of errors in Python

	1)Syntax errors
	2)Exception errors

Syntax errors occur when there is a wrong syntax in your code and where as exception errors occur when executing the code and found something wrong with file permissions, floating point error, division by zero error, IO errors, Name errors etc though the syntax of the code is right one.

Try and except statements are useful in countering these exception errors. When ever your code come across these exceptions, your try and exception statements help in changing the behavior of your program in a meaning full way.

Let us learn try and except statements with examples
Try and Except statement syntax:

	try:
	    do something here
	except exception-code:
	    do something if found exception-code error
	   

Example 1: Open a file and secure the error with IOError exception if file do not exit’s

	#!/usr/bin/python
	try:
	    f = open('abci.txt', 'r')
	except IOError:
	    print "Can not open the file"

Example 2: Ok, that is fine how about if you try to give illegal mode to open a file like ‘d’ mode? We have to counter this with multiple exceptions which may occur

	​#!/usr/bin/python
	try:
	    f = open('abc.txt', 'dsf')
	    print "Sucessfully opened the file"
	except IOError:
	    print "Can not open the file because due to file open issues "
	except ValueError:
	    print "Can not open the file because some another thing happen"

Example 3: Clubbing multiple exception to form compound statement

	#!/usr/bin/python
	try:
	    f = open('abc.txt', 'd')
	    print "Sucessfully opened the file"
	except IOError and ValueError:
	    print "Can not open the file because due to file open issues "

Example 4: Printing actual exception in output so that user will come to know what kind of error he is getting.

	#!/usr/bin/python
	try:
	    f = open('abc.txt', 'dsf')
	    print "Successfully opened the file"
	except IOError as error1:
	    print "Can not open the file because I got ", error1


Example 5: Printing actual trace back with exception in output so that user will come to know what kind of error he is getting by using raise statement.

	#!/usr/bin/python
	try:
	    f = open('abc.txt', 'r')
	    print "Successfully opened the file"
	except IOError:
	    print "Can not open the file because due to file open issues with below trace back"
	    raise

Example 6: How about executing remaining code if there is no exception occur? Try and except can be combined with else statement so that if exception did not occur else block code is executed

	#!/usr/bin/python
	try:
	    f = open('abc.txt', 'r')
	    print "Sucessfully opened the file"
	except IOError:
	    print "Can not open the file because due to file open issues "
	else:
	    print "I am in else loop"
	    f.close()

Example 7: How about executing some code once this try-except loop is executed and want to execute this code irrespective of exception occur or not. To do this we have to use finally statement to execute this code.

	#!/usr/bin/python
	try:
	    f = open('abc.txt', 'r')
	    print "Successfully opened the file"
	except IOError:
	    print "Can not open the file because due to file open issues "
	else:
	    print "I am in else loop"
	    f.close()
	finally:
	    print "This is the code finally>executed for this try example"

We should know below key words to understand try and exception statements

	try: --Will try to execute code which may contain exception
	except: --Once this exception occur and matches the exception which we are looking, then the code in this exception block is executed.
	raise: --Nothing but actual trace back with complete error message
	finally: --The code which we want to execute finally once try and except/else blocks are executed. This is equal to END block in Perl and AWK.

	else: --If no exception occur execute the code in this block.

Applications for Python

Applications for Python

This one leads to pythons official site.
Yes, some of the best, if not at least what is recommended can be found there.
Its a great place for resources if you actually take the time to look into it.

Many people start off in python, go there just download the latest python version,
and then get teleported to other sites or resources on the web, sure there are great resources out there,
non the less this might be a mistake to beginners as they end up looking into to much info that are irrelevant
at a start point and land on stuff that aren’t really going to help them out.

One should always start there and check out watt they start by recommending.
After all thats maintained by the people behind the python language.

https://www.python.org/about/apps/

How To Package And Distribute Python Applications – Linux

How To Package And Distribute Python Applications

How To Package And Distribute Python Applications

Introduction

All Python libraries (i.e. application packages) that you download using a package manager (e.g. pip) are distributed using a utility dedicated to do the job. These utilities create « Python distributions » which are basically versioned (and compressed) archives. All related elements to what’s being distributed, such as source files and resource files, are contained within it.

In this DigitalOcean article, we are going to talk about the necessary tools for distribution and go over the key steps to allow you to package your own useful libraries, modules, or applications — which should help you when deploying your project on a droplet or sharing on the internet.

Python Distributions and Packages

Even if you have worked only a little with Python, you will be familiar with the concept of using a package manager (e.g. pip, easy_install) to download modules and libraries (e.g. application development frameworks) which are then imported and used to create a new one.

These package management tools, operating locally, connect to a source (i.e. Python Package Index – PyPI) and perform a desired action (e.g. search and install) as they work these resources which are actually called Python distributions.

The way to distribute an application consists of wrapping its directory with some must-have files (along with a few recommended ones), specifying related elements (e.g. resources, dependencies etc.) and releasing it or using it elsewhere…that simple.

Note: You are highly encouraged to work with virtual environments to isolate Python downloads, modules, and applications you are working with.

Python Packages

In Python, a package [technically] is an importable directory (with __init__.py) containing source files (i.e. modules). This shall not be confused with operating-system packages, which are [technically] actual applications (i.e. a Debian package). However, it must be noted that Python distributions are indeed called packages as well.

Example package structure:

package
  |
  |-- __init__.py

Python Applications

Although anything from a single file to one with hundreds scattered across various packages can be considered an application in Python, in most realistic scenarios, an application will consist of multiple modules and a certain amount of external imports (from libraries).

Example application structure:

myapp
  |
  |-- __init__.py
  |-- amodule.py
  |-- anothermod.py
  |__ tests
  |     |
  |     |-- __init__.py
  |     |-- ..
  |     |-- .
  | ..

Python Distribution Tools and Libraries

Given the popular nature of Python and the rich amount of third-party libraries / applications written for it, a simpler and unified way of distributing has always been a necessity. There have been several different tools and libraries used for creating Python distributions.

In order to deal with the tasks of distribution, Python distribution utilities toolset distutils was created.

Python Package Index (PyPI)

Python Package Index, or PyPI, is a central [online] repository for projects (Python distributions). Package managing tools such as pip use this repository in order to host, find and install them.

Getting Started

Let »s begin with creating a simple, general Python flask application [structure] which we then can use to package.

Creating the Application Structure

We aim to create an example that resembles most real-world projects. Therefore, it will be best to imagine a scenario with modularised components.

Example structure:

/MyApplication
    |-- run.py
    |-- config.py
    |__ /app
         |-- __init__.py
         |-- /module_one
             |-- __init__.py
             |-- controllers.py
             |-- models.py                
         |__ /templates
             |-- module_one
                 |-- hello.html
         |__ /static
         |__ ..
         |__ .

Create the folders:

mkdir ~/MyApplication
cd    ~/MyApplication
touch run.py
touch config.py
mkdir app
cd    app
touch __init__.py
mkdir templates
mkdir static
mkdir module_one
cd    module_one
touch __init__.py
touch controllers.py
touch models.py
cd    ../templates
mkdir module_one
cd    module_one
touch hello.html

Edit run.py using nano:

nano ~/MyApplication/run.py

Place the contents:

# Run a test server.
from app import app
app.run(debug=True)

Save and exit using CTRL+X and confirm with with Y.

Edit config.py using nano:

nano ~/MyApplication/config.py

Place the contents:

DEBUG = True

THREADS_PER_PAGE = 4

CSRF_ENABLED     = True
CSRF_SESSION_KEY = "secret"

Save and exit using CTRL+X and confirm with with Y.

Edit app/init.py using nano:

nano ~/MyApplication/app/__init__.py

Place the contents:

from flask import Flask, render_template

app = Flask(__name__)
app.config.from_object("config")

from app.module_one.controllers import module_one

app.register_blueprint(module_one)

Save and exit using CTRL+X and confirm with with Y.

Edit app/module_one/controllers.py using nano:

nano app/module_one/controllers.py

Place the contents:

from flask import Blueprint, request, render_template

module_one = Blueprint("auth", __name__, url_prefix="/auth")

@module_one.route("/hello")
def hello():
    return render_template("module_one/hello.html")

Save and exit using CTRL+X and confirm with with Y.

Place the contents:

Edit app/templates/module_one/hello.html using nano:

nano app/templates/module_one/hello.html

Place the contents:

    &lt!DOCTYPE html>
    &lthtml lang="en">
    &lthead>
        &lttitle>{% block title %}My Site{% endblock %}
        {% block css %}
        {% endblock %}
        &ltmeta name="viewport" content="width=device-width, initial-scale=1.0">
      &lt/head>
    &ltbody>
    Hello, world!
    &lt/body>
    &lt/html>

Save and exit using CTRL+X and confirm with with Y.

Beginning with Application Distribution / Packaging

Having created an exemplary application structure of a web site that uses flask, we can continue with taking the first step into preparing the distribution.

Altering the Folder Structure

In order to package our application well, we need to make some additions to our folder structure.

/MyApplication
    |-- run.py
    |__ /app
         |-- __init__.py
         |-- /module_one
             |-- __init__.py
             |-- controllers.py
             |-- models.py                
         |__ /templates
             |-- module_one
                 |-- hello.html
         |__ /static
         |__ ..
         |__ .
    |-- setup.py    # Distribution setup file
    |-- README.txt  # Read-me file
    |-- MANIFEST.in # Distribution manifest file
    |-- CHANGES.txt # Changes log

Alter the folder structure to create necessary files:

touch ~/MyApplication/setup.py
touch ~/MyApplication/README.py
touch ~/MyApplication/MANIFEST.py
touch ~/MyApplication/CHANGES.py
mv    ~/MyApplication/run.py ~/MyApplication/bin/run

Create the setup.py

nano ~/MyApplication/setup.py

Place the below self explanatory contents:

from distutils.core import setup

setup(
    # Application name:
    name="MyApplication",

    # Version number (initial):
    version="0.1.0",

    # Application author details:
    author="name surname",
    author_email="name@addr.ess",

    # Packages
    packages=["app"],

    # Include additional files into the package
    include_package_data=True,

    # Details
    url="http://pypi.python.org/pypi/MyApplication_v010/",

    #
    # license="LICENSE.txt",
    description="Useful towel-related stuff.",

    # long_description=open("README.txt").read(),

    # Dependent packages (distributions)
    install_requires=[
        "flask",
    ],
)

Save and exit using CTRL+X and confirm with with Y.

Create the MANIFEST.in

If you need to ship extra directories (e.g. static or templates), you need to explicitly state them in the manifest to be packaged. We will do this inside the MANIFEST.in.

nano ~/MyApplication/MANIFEST.in

Place the below self explanatory contents:

recursive-include app/templates *
recursive-include app/static *

Save and exit using CTRL+X and confirm with with Y.

And that’s it! Your Python distribution package is ready to be installed and shipped.

Additional Files

Please remember that in order to have a complete distribution, your file/directory must contain (and linked):

  • README.txt
  • MANIFEST.in
  • LICENSE.txt

Working With the Distribution Ready Application

As we have finalized creation of our application followed by making necessary amendments to the file structure to prepare it for a flawless distribution build, we can begin with going through the packaging operations.

How to Create The Distribution File

In order to generate a distribution file copy, run the following:

cd     ~/MyApplication
python setup.py sdist

This command will go through your setup, print out the operations being performed and generate a tar archive inside the newly created diet directory, similar to:

# root@hostname:~/MyApplication# ls dist
# MyApplication-0.1.0.tar.gz

Note: Since we did not populate all the sub-folders (i.e. static) and worked with additional files (e.g. README.txt), you might see some warnings during the creation process.

How to Install The Application

From now on, your application can be installed and used by others using the setup.py file created.

In order to install the application, run the following:

python setup.py install

If this installation is for development and the requirements are also to be installed, run the following:

python setup.py develop

How to Share Your Application

If you would like to share your code on the Python Packaging Index, you can do so by initiating the « register » procedure as per the following:

python setup.py register

You will need to complete the procedure by following the on-screen instructions.

If you have a registered login, in order to just upload, you can use the following:

python setup.py sdist upload

How to Create Packages of Your Application’s New Versions

  1. Edit the setup.py file with a text editor (e.g. nano) and set the new version number: version="0.1.1"
  2. Edit the CHANGES.txt to reflect the changes
  3. Make the necessary adjustments to the LICENSE.txt and README.txt
  4. Upload your code following the previous step.

Article source: https://www.digitalocean.com/community/tutorials/how-to-package-and-distribute-python-applications

Python: Write Gui Apps

This is an overview of the best tools and the best resources for building desktop applications in Python.

First things first. You can build great desktop applications in Python, and some are widely used (like Dropbox). But you’ll have to find your own way much more than you would using Microsoft’s or Apple’s SDKs. The upside is that, with a bit of legwork to package it appropriately, it’s quite feasible to write a Python application that works on all the major platforms.

GUI toolkits

The first thing you’ll need to choose is a GUI toolkit.

Qt logo

  • For traditional desktop UIs, Qt is a clear winner. It’s powerful, looks native on all the major platforms, and has probably the biggest community. There are two different Python bindings: PyQt is older and more mature, but it’s only free if your application is open source (licensing), while PySide is newer and more permissively licensed (LGPL). I refer to the main Qt docs a lot – the C++ examples mostly translate to Python quite well – but both PyQt’s and PySide’s docs contain some useful information. Qt Designer is a drag and drop interface to design your UI; you can compile its .ui files to Python modules with the pyuic command line tool.
/images/Qt_Designer.png

Qt Designer in action

For attractive, tablet-style interfaces, Kivy is the right choice. It’s a fairly young but promising system. If you want to bring your application to tablets and smartphones, then Kivy is the only option that I’m aware of. More info

  • When you want a basic GUI and don’t care about aesthetics, Tkinter is a simple option. It’s installed as part of Python. Python’s own tkinter documentation is rather minimal, but it links to a bunch of other resources. This site is my favourite – it hasn’t been updated in years, but then neither has Tkinter (except that in Python 3, you import tkinter rather than import Tkinter).
  • pygame is popular for building simple 2D games. There are also frameworks for 3D graphics (pyglet, Panda3d), but I don’t know much about them.
  • An increasingly popular option is to write your application as a local web server, and build the UI in HTML and Javascript. This lets you use Python’s large ecosystem of web frameworks and libraries, but it’s harder to integrate with desktop conventions for things like opening files and window management. CEF Python lets you make a window for your application, based on Google Chrome, but I haven’t tried that.

A couple of alternatives I wouldn’t recommend unless you have a reason to prefer them: GTK is popular on Linux, but it looks ugly on other platforms. The older pygtk bindings have excellent documentation; the newer PyGObject system, which supports recent versions of GTK and Python, doesn’t (though it’s getting better). wx seems to have a good community, but development is slow, and new projects that could have used it now mostly seem to pick Qt.

Packaging and Distribution

This is probably the roughest part of making an application in Python. You can easily distribute tools for developers as Python packages to be installed using pip, but end users don’t generally have Python and pip already set up. Python packages also can’t depend on something like Qt. There are a number of ways to package your application and its dependencies:

  • Pynsist, my own project, makes a Windows installer which installs a version of Python that you specify, and then installs your application. Unlike the other tools listed here, it doesn’t try to ‘freeze’ your application into an exe, but makes shortcuts which launch .py files. This avoids certain kinds of bugs.
  • cx_Freeze is a freeze tool: it makes an executable out of your application. It works on Windows, Mac and Linux, but only produces the executable for the platform you run it on (you can’t make a Windows exe on Linux, for example). It can make simple packages (.msi for Windows, .dmg for Mac, .rpm for Linux), or you can feed its output into NSIS or Inno Setup to have more control over building a Windows installer.
  • PyInstaller is similar to cx_Freeze. It doesn’t yet support Python 3 (update: it does now, since October 2015), but it does have the ability to produce a ‘single file’ executable.
  • py2app is a freeze tool specifically for building Mac .app bundles.
  • py2exe is a Windows-only freeze tool. Development stopped for a long time, but at the time of writing there is some recent activity on it.

Linux packaging

Although some of the freeze tools can build Linux binaries, the preferred way to distribute software is to make a package containing just your application, which has dependencies on Python and the libraries your application uses. So your package doesn’t contain everything it needs, but it tells the package manager what other pieces it needs installed.

Unfortunately, the procedures for preparing these are pretty complex, and Linux distributions still don’t have a common package format. The main ones are deb packages, used by Debian, Ubuntu and Mint, and rpm packages, used by Fedora and Red Hat. I don’t know of a good, simple guide to packaging Python applications for either – if you find one or write one, let me know.

You can get users to download and install your package, but if you want it to receive updates through the package manager, you’ll need to host it in a repository. Submitting your package to the distribution’s main repositories makes it easiest for users to install, but it has to meet the distro’s quality standards, and you generally can’t push new feature releases to people except when they upgrade the whole distribution. Some distributions offer hosting for personal repos: Ubuntu’s PPAs, or Fedora’s Fedorapeople repositories. You can also set up a repository on your own server.

If you don’t want to think about all that, just make a tarball of your application, and explain to Linux users next to the download what it requires.

Miscellaneous

  • Threading: If your application does anything taking longer than about a tenth of a second, you should do it in a background thread, so your UI doesn’t freeze up. Be sure to only interact with GUI elements from the main thread, or you can get segfaults. Python’s GIL isn’t a big issue here: the UI thread shouldn’t need much Python processing time.
  • Updates: Esky is a framework for updating frozen Python applications. I haven’t tried it, but it looks interesting.

Article taken from: http://takluyver.github.io/posts/so-you-want-to-write-a-desktop-app-in-python.html