Shortpath

Shortpath a package which reduces your time to type.

All the time I start terminal and for starting my work have to type long directory paths which were really annoying sometimes. To overcome this problem I just created a package and now I can assign a short name for that path and this really makes me happy.

Brief:

shortpath assign a short name which you passed or it will generate a random string for your path that you entered. If no path you are entering this will take the current working directory as a path. The short name will be user wise. If you will set a short name from a root user this will not work for the guest user and vice versa.

Installation:

$ pip install shortpath

Usage:

$ shortpath –help

$ shortpath –v

$ shortpath –p –s

$ shortpath –p

$ shortpath –s

Note: After setting short name for any path, open a new terminal or a new tab then use your path’s short name.

Advertisements

Command line for your package

In the previous post, I explained how to create a package.
Everyone also wants to use their package with command line so here the steps you can follow. The package we built named shortpath. The structure of our package was

/shortpath

shortpath/

__init__.py
shortpath.py

MANIFEST.in
README.rst
__init__.py
setup.py

Now add new script to it for accessing your package with command line

/shortpath

bin/

shortpath

shortpath script will have some code like

#!/usr/bin/env python

import shortpath
print shortpath.some_method()

Now we have to declare our command script in setup.py

setup(
    ...
    scripts=['bin/funniest-joke'],
    ...
)

When we will be done with our installation of the package, setup tools will copy the script to our PATH and make the command available for general use.

$ shortpath

 

Create a Python package?

We assume that we are creating a package shortpath.

The package should contain these files.
README.rst
MANIFEST.in
setup.py
setup.cfg (If you have any configuration to your package)

Now we have to build our setup.py based on our package details, for creating setup.py follow this.

If you are done with the above steps, your package structure will be something like

/shortpath

shortpath/

__init__.py
shortpath.py

MANIFEST.in
README.rst
__init__.py
setup.py

The shortpath.py will contain your methods, classes. Now, you can test your package locally using

$ pip install -e .

>>> import shortpath
>>> print some_method()

Now for uploading your package to PyPI,

$ pip install twine

Follow the link for registering on PyPI and configuring .pypirc

To generate your package distribution

$ python setup.py sdist

To generate you package info

$ python setup.py egg_info

Now upload your package with command

$ twine upload dist/*

Install the uploaded package

$ pip install shortpath

Note: If any error in installing the package it must be conflicting with the local package so remove the local package and symlink

  • The packages you will find in
    /usr/local/lib/python/dist-packages/
  • The symlink will be in
    /usr/local/lib/python/dist-packages/easy-install.pth

Mosquitto-PHP with Ubuntu

This is just an extension to allow using the Eclipse Mosquitto™ MQTT client library with PHP

# For Installing pecl
sudo apt-get install php-pear

# Install PHP developer packages
sudo apt-get install php5-dev
or
sudo apt-get install php7-dev

# Mosquitto setup
sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
sudo apt-get update
sudo apt-get install libmosquitto-dev
sudo apt-get install mosquitto mosquitto-clients
sudo pecl install Mosquitto-alpha

Add extension=mosquitto.so to php.ini

sudo vim /etc/php5/cli/php.ini

Now test your setup, create new subscriber.php

$client = new Mosquitto\Client();
$client->onConnect(‘connect’);
$client->onDisconnect(‘disconnect’);
$client->onSubscribe(‘subscribe’);
$client->onMessage(‘message’);
$client->connect(“localhost”, 1883, 60);
$client->subscribe(‘/#’, 1);

while (true) {
$client->loop();
sleep(2);
}

$client->disconnect();
unset($client);

function connect($r) {
echo “I got code {$r}\n”;
}

function subscribe() {
echo “Subscribed to a topic\n”;
}

function message($message) {
printf(“\nGot a message on topic %s with payload:%s”,
$message->topic, $message->payload);
}

function disconnect() {
echo “Disconnected cleanly\n”;
}

Execute the subscriber.php

php subscriber.php

On executing the above command, you will get

I got code 0
Subscribed to a topic

Test the subscriber with mosquitto_pub command
mosquitto_pub -h localhost -t “/mqtt” -m “Hello World!”

On executing the above command, you will get

Got a message on topic /mqtt with payload:Hello World

Now create new publisher.php

$client = new Mosquitto\Client();
$client->onConnect(‘connect’);
$client->onDisconnect(‘disconnect’);
$client->onPublish(‘publish’);
$client->connect(“localhost”, 1883, 5);

while (true) {
try{
$client->loop();
$mid = $client->publish(‘/mqtt’, “Hello from PHP!”);
$client->loop();
}catch(Mosquitto\Exception $e){
return;
}
sleep(2);
}

$client->disconnect();
unset($client);

function connect($r) {
echo “I got code {$r}\n”;
}

function publish() {
global $client;
echo “Mesage published\n”;
$client->disconnect();
}

function disconnect() {
echo “Disconnected cleanly\n”;
}

Execute the publisher.php

php publisher.php

On executing the above command, we will get a message as shown below :

I got code 0
Mesage published
Disconnected cleanly

Also, we can see the published message as shown below at the terminal where the subscriber ( subscriber.php ) is running :
I got code 0
Subscribed to a topic
Got a message on topic /mqtt with payload:Hello from PHP!

For now only implemented few steps looking for how much I can dive into.

twiturls

Sometimes we really need something and we don’t want to follow the same fashion what others do. So, here is one of them fetching URLs from tweets and the course of action is using browser manually or from an app you can get the tweets and then from that you can get the URLs.

There is one other way now just get the twiturls, follow the syntax and you will get only the URLs from the tweets.

from twitlinks import twitlinks

twitlinks.get_links(‘#tag’)

twitlinks.get_links(‘#tag’, 5)

twitlinks.get_links(‘#tag1, #tag2’)

twitlinks.get_links(‘#tag1, #tag2’, 5)