Search

22 August, 2021

PSQL commands handbook

PSQL is its own world. Interacting with it simple, yet the language has to be learned. Here is a list of basic commands which are a must.

Launching PSQL on any terminal:

psql

Create New User (users are roles with rights):

CREATE USER name;

Set a password for the user:

\password user

List all roles (users):

\du

Listing all databases:

\l


Selecting and working on a database:

\c DB_NAME


Creating a database:

CREATE DATABASE name;

Note: The semi-colon at the end is mandatory.

Quitting PSQL (or some command screen):

\q

 Deleting database

DROP DATABASE name;

14 June, 2021

Pytest: Answering all nagging questions.

Pytest has a lot to offer but discovering it's powers are a pain.
I won't be covering those solutions which are already clearly stated in the pytest docs.

Here are a few questions that we all face at some point and with possible answers.

Q. How to find the sequence of fixtures that have been called?


  `python -m pytest --setup-plan test_somefile.py`


This is one way to find out what are the fixtures used.

Q. My imports are not working even after adding __init__ files?

A. With pytest, you have to set the variable PYTHONPATH in order to run test with syntax:

`pytest tests`


If you want still want to run tests with the above step, them use this syntax:

`python -m pytest tests` 


Q. How do I run random tests (just by test name) ?

A. The easiest way to run random test is using 'Keyword Expressions'
    
     `python -m pytest  test/integration -k 'test_password or test_no_configuration' --collect-only`

Pending Questions:


Q. How to pass an argument to a fixture from inside the test. ?
Q. How to ignore a fixture for a test.?
Q. If used a fixture by force using use_fixture, what will be the sequence of fixtures.?
Q. How to Skip a test using fixture?
Q. How to have a setUp, teardown like structure in PyTest like in unittest ?
Q. How to have a SetUpClass to run before class and TearDownClass just like in unittest?
Q. How do I enable 'print' statements.?
Q. How do I control logs by levels.?

   

20 April, 2021

Jupyter notebook and IPython first steps.

In this post, I am going to go through some initial steps for people who are looking to try out Jupyter Notebooks (JN). We will see how to:

    • Use a venv in our Jupyter notebook to work on projects.
    • Shortcuts for working with JN.
    • Basic ways of using new python shortcuts provided in IPython.

Que.  How to install IPython inside a virtualenv and use it?

Ans: These are a few ways to launch the python installed in your venv

- venv/bin/ipython

- python -m IPython

24 February, 2021

Mongo DB first take

Testing new waters. My first NoSQL db was InfluxDB. It is a Time based No SQL type db. 

This time I wanted to taste the all famous MongoDB. I want to keep the basics handy. So I write as I learn.

Basic Commands ☝


Launch MongoDB:

~ mongo



See existing databases

show dbs

03 August, 2020

Grafana with Influxdb and python client

Recently I came across this tool called Grafana. Its a tool that you can use to show graphs and statistics of all kinds. The most popular combination for setup , is Grafana with influxdb as a database. 

I am sharing my setup steps, and supporting python scripts that can be used to generate information and write back to the database. And finally we get to see some nice graphs.

Note: I am using a Mac with Catalina OS 10.15. Python 3.6+.

Installation Plan:


1. Influxdb
2. Grafana
3. influxdb client module for Python.

29 July, 2020

Docker : Most useful commands

Docker has a lot to talk about. But I am going to list a few commands here that come handy while working with it on a daily basis. We will list them in a sequence that looks like a tutorial.


1. List all docker containers


docker container ls

2. List all images


docker image ls

docker images

3. Spawn & run from an image, a new container.


docker run image_name COMMAND

12 June, 2019

Python unittest and exceptions: Writing custom exceptions, and catching them.


Generally in unit test, we match the responses with an expected value . But there are cases , where calling a method or passing an incorrect value (or type of value) may raise an exception . This exception might me a custom exception . Or the message or error code might me specific to the situation. So how to write test for the same.

1. Calling a method with incorrect type , raises an exception.


class MyTest(unittest.TestCase):
    def test_exception(self):
         with self.assertRaises(xmlrpclib.Fault) as cm:
            self.Proxy.setParameter('name', 1234)
            self.fail('Expected exception of type xmlrpc.Fault')