Showing posts with label WebDevelopment. Show all posts
Showing posts with label WebDevelopment. Show all posts

Wednesday, 2 August 2017

How to Write, Package and Distribute a Library in Python


Python is a great programming language, but packaging is one of its weakest points. It is a well-known fact in the community. Installing, importing, using and creating packages has improved a lot over the years, but it's still not on par with newer languages like Go and Rust that learned a lot from the struggles of Python and other mature languages. 

In this tutorial, you'll learn everything you need to know about writing, packaging and distributing your own packages. 

How to Write a Python Library

A Python library is a coherent collection of Python modules that is organized as a Python package. In general, that means that all modules live under the same directory and that this directory is on the Python search path. 

Let's quickly write a little Python 3 package and illustrate all these concepts.

The Pathology Package

Python 3 has an excellent Path object, which is a huge improvement over Python 2's awkward os.path module. But it's missing one crucial capability—finding the path of the current script. This is very important when you want to locate access files relative to the current script. 

In many cases, the script can be installed in any location, so you can't use absolute paths, and the working directory can be set to any value, so you can't use a relative path. If you want to access a file in a sub-directory or parent directory, you must be able to figure out the current script directory. 

Here is how you do it in Python:

To access a file called 'file.txt' in a 'data' sub-directory of the current script's directory, you can use the following code: print(open(str(script_dir/'data/file.txt').read())

With the pathology package, you have a built-in script_dir method, and you use it like this:

Yep, it's a mouthful. The pathology package is very simple. It derives its own Path class from pathlib's Path and adds a static script_dir() that always returns the path of the calling script. 

Here is the implementation:

Due to the cross-platform implementation of pathlib.Path, you can derive directly from it and must derive from a specific sub-class (PosixPath or WindowsPath). The script dir resolution uses the inspect module to find the caller and then its filename attribute.

Testing the Pathology Package

Whenever you write something that is more than a throwaway script, you should test it. The pathology module is no exception. Here are the tests using the standard unit test framework: 

The Python Path

Python packages must be installed somewhere on the Python search path to be imported by Python modules. The Python search path is a list of directories and is always available in sys.path. Here is my current sys.path:

Note that the first empty line of the output represents the current directory, so you can import modules from the current working directory, whatever it is. You can directly add or remove directories to/from sys.path. 

You can also define a PYTHONPATH environment variable, and there a few other ways to control it. The standard site-packages is included by default, and this is where packages you install using via pip go. 

How to Package a Python Library

Now that we have our code and tests, let's package it all into a proper library. Python provides an easy way via the setup module. You create a file called setup.py in your package's root directory. Then, to create a source distribution, you run: python setup.py sdist

To create a binary distribution called a wheel, you run: python setup.py bdist_wheel

Here is the setup.py file of the pathology package:

It includes a lot of metadata in addition to the 'packages' item that uses the find_packages() function imported from setuptools to find sub-packages.

Let's build a source distribution:

The warning is because I used a non-standard README.md file. It's safe to ignore. The result is a tar-gzipped file under the dist directory:

And here is a binary distribution:

The pathology package contains only pure Python modules, so a universal package can be built. If your package includes C extensions, you'll have to build a separate wheel for each platform:

For a deeper dive into the topic of packaging Python libraries, check out How to Write Your Own Python Packages.

How to Distribute a Python Package

Python has a central package repository called PyPI (Python Packages Index). When you install a Python package using pip, it will download the package from PyPI (unless you specify a different repository). To distribute our pathology package, we need to upload it to PyPI and provide some extra metadata PyPI requires. The steps are:

  • Create an account on PyPI (just once).
  • Register your package.
  • Upload your package.

Create an Account

You can create an account on the PyPI website. Then create a .pypirc file in your home directory:

For testing purposes, you can add a "pypitest" index server to your .pypirc file:

Register Your Package

If this is the first release of your package, you need to register it with PyPI. Use the register command of setup.py. It will ask you for your password. Note that I point it to the test repository here:

Upload Your Package

Now that the package is registered, we can upload it. I recommend using twine, which is more secure. Install it as usual using pip install twine. Then upload your package using twine and provide your password (redacted below):

For a deeper dive into the topic of distributing your packages, check out How to Share Your Python Packages.

Conclusion

In this tutorial, we went through the fully fledged process of writing a Python library, packaging it, and distributing it through PyPI. At this point, you should have all the tools to write and share your libraries with the world.

Additionally, don’t hesitate to see what we have available for sale and for study in the marketplace, and please ask any questions and provide your valuable feedback using the feed below.



Read the full article here by Nettuts+

Wednesday, 15 February 2017

Falcor: estrarre dati con JavaScript tramite JSON

Flacor è una libreria JavaScript realizzata dagli sviluppatori di Netflix per le procedure di data fetching; sostanzialmente si tratta di una soluzione Open Source per l’estrazione dei dati da sorgenti nelle quali le informazioni da prelevare vengono messe a disposizione tramite notazione JSON (JavaScript Object Notation), un formato ormai standard per l’interscambio in applicazioni client-server.

Uno dei vantaggi derivanti dall’adozione di questa libreria risiede nel fatto che essa permette di fare riferimento ad un unico modello di dati, indipendentemente dalla loro fonte; più precisamente Falcor consente di rappresentare qualsiasi sorgente remota di informazioni sotto forma di …

The post Falcor: estrarre dati con JavaScript tramite JSON appeared first on Edit.



Read the full article here by Edit

Friday, 13 January 2017

Cinder – A Powerful, Intuitive Toolbox

Advertise here via BSA

Cinder is a C++ library for programming with aesthetic intent – the sort of development often called creative coding. This includes domains like graphics, audio, video, and computational geometry. Cinder is cross-platform, with official support for OS X, Windows, iOS, and WinRT. Cinder is production-proven, powerful enough to be the primary tool for professionals, but still suitable for learning and experimentation. Cinder is released under the 2-Clause BSD License.

cinder

Requirements: C++
Demo: https://libcinder.org/
License: BSD License

Sponsors

Professional Web Icons for Your Websites and Applications



Read the full article here by WebAppers

Saturday, 12 November 2016

Winds – Open Source Personalized RSS Reader

Advertise here via BSA

Winds is an open source & beautiful RSS reader built using React/Redux/Sails/Node 7 and Stream. It showcases personalized feeds (using machine learning similar to Facebook, Flipboard, Etsy, and Quora) powered by the http://getstream.io API. This tutorial explains how the personalization API works (http://bit.ly/personalization-winds). You can also check out the hosted demo at http://ift.tt/2eHT9HR.

personalized-rss-reader

Requirements: –
Demo: http://ift.tt/2eHT9HR
License: MIT License

Sponsors

Professional Web Icons for Your Websites and Applications



Read the full article here by WebAppers

Wednesday, 26 October 2016

Stripe Radar – Modern Tools to Prevent Fraud

Advertise here via BSA

Stripe Radar is modern tools to prevent fraud, fully integrated with your payments. Old ways of combating fraud were never designed for modern internet businesses. Stripe Radar is powered by advanced machine learning algorithms that automatically learn from Stripe’s global network of businesses to identify and prevent fraud. Stripe Radar scans every transaction using the most relevant signals to detect and block fraud. And Radar’s algorithms are constantly tweaked by Stripe engineers to adapt to new fraud vectors.

They pinpoint fraud by building behavioral signals from across 100,000+ global companies. (Even if a card is new to your business, there’s an 80% chance it’s been seen before on the Stripe network.) Unlike systems that require weeks to train and only mimic human reviewers, Radar lets you instantly use rich payments data (including info from banks and card networks) to fight fraud.

stripe-radar

Requirements: Stipe
Demo: http://ift.tt/2dAkJbI
License: License Free

Sponsors

Professional Web Icons for Your Websites and Applications



Read the full article here by WebAppers

Wednesday, 12 October 2016

Ubuntu Core 16 Beta su Raspberry Pi

Il progetto Ubuntu Core prosegue il suo sviluppo. La distribuzione di Canonical dedicata ai sistemi embedded si è adesso allineata con i pacchetti presenti nei repository dell’ultima LTS disponibile ovvero Ubuntu 16.04.

Ubuntu Core, essendo un sistema dedicato a dispositivi che di solito non vengono quasi mai aggiornati, fino ad oggi è rimasta ferma ai pacchetti di Ubuntu 15.04 ovvero una versione obsoleta della distribuzione.

Il team del progetto lavora da mesi sul ramo di sviluppo di Ubuntu Core, sopratutto per garantire un supporto più duraturo e meno problematico in mancanza di aggiornamenti frequenti. Ora la nuova beta è pronta …

The post Ubuntu Core 16 Beta su Raspberry Pi appeared first on Edit.



Read the full article here by Edit

Thursday, 30 June 2016

Crosswalk: un Web runtime per sviluppare applicazioni ibride

Crosswalk Project è un runtime che consente di sviluppare applicazioni ibride utilizzando le tecnologie per la creazione di Web application (HTML5, CSS3 e JavaScript); grazie ad esso si disporrà di una WebView o di un browser in-App per il funzionamento di soluzioni destinate a girare su Android, iOS o Windows, mentre la compatibilità con Cordova permette di implementare App multipiattaforma con tecnologie per Internet sfruttando un unico codebase.

Oltre che per massimizzare la produttività, il Crosswalk Project nasce anche per risolvere i problemi di retrocompatibilità cross-device che potrebbero coinvolgere un’applicazione in fase di produzione. Spesso infatti i dispositivi datati si …

The post Crosswalk: un Web runtime per sviluppare applicazioni ibride appeared first on Edit.



Read the full article here by Edit

Saturday, 18 June 2016

nbind: accedere a librerie C++ da JavaScript, NodeJS e browser

nbind è una raccolta di header sviluppati per rendere le librerie C++11 accessibili attraverso codice JavaScript; si tratta di una soluzione facilmente integrabile nei propri progetti, grazie ad un una singola istruzione #include il compilatore di C++ può generare i binding necessari per l’interazione tra le due tecnologie, senza la necessità di ricorrere ad ulteriori strumenti. La libreria coinvolta potrà quindi essere utilizzata come un’estensione di NodeJS o inclusa in una pagina Web senza plugin se compilata con Emscripten in asmJS.

Si tratta di un progetto libero e aperto rilasciato sotto MIT license e basato su template e macro …

The post nbind: accedere a librerie C++ da JavaScript, NodeJS e browser appeared first on Edit.



Read the full article here by Edit