Showing posts with label table. Show all posts
Showing posts with label table. Show all posts

Python: How to print a json object in table format

If you have json object and you want print the json object in table format, then you are the right place. Though I didn't wrote any module, I found one which can help. There is a module named pytablewriter. You can use that module to print your json object in many formats, checkout their documenration for more information. Now let's get into business. Lets say you have a json object like below
obj = [{
	'name': 
	'Fix PRB', 
	'project': 
	'PRB', 
	'status': 
	'New', 
	'category': 
	'work', 
	'created_on': 
	'2021-05-13 19:29:39.023231'}, {
	'name': 
	'catalog_broken', 
	'project':  
	'zoom', 
	'status': 
	'New', 
	'category': 
	'work', 
	'created_on': 
	'2021-05-13 19:54:42.109463'}]
We need to build couple of parameters first. Create 3 variables with names as table_name, headers, value_matrix.
table_name = 
	"Todo List"
	# this will not print, its just needed.
headers = []
value_matrix = []
Now lets extract keys and values from given json object.
headers = list(obj[
	0].keys())


	for item 
	in obj:
	value_matrix.append(list(item.values()))
So we built the needed data, now lets print it.
	from pytablewriter 
	import UnicodeTableWriter
writer = UnicodeTableWriter( table_name = table_name, headers = headers, value_matrix = value_matrix )
writer.write_table()
┌──────────────┬───────┬──────┬────────┬──────────────────────────┐
│     name     │project│status│category│        created_on        │
├──────────────┼───────┼──────┼────────┼──────────────────────────┤
│Fix PRB       │PRB    │New   │work    │
	2021
	-05
	-13
	19:
	29:
├──────────────┼───────┼──────┼────────┼──────────────────────────┤
│catalog_broken│zoom   │New   │work    │
	2021
	-05
	-13
	19:
	54:
└──────────────┴───────┴──────┴────────┴──────────────────────────┘
And that's it. It will create a table with give json object. Check the pytablewriter documentation for more options. So I have created a fucntion which you use if you needed in case
	
		def
		pyTablePrinter
		(json_obj, table_name=
			"Todo List")
		:
	

        table_name = table_name
        headers = []
        value_matrix = [] 

        headers = list(json_obj[
	0].keys())
        
	for item 
	in json_obj :
            value_matrix.append(list(item.values()))
        
        writer = UnicodeTableWriter( table_name = table_name, headers = headers, value_matrix = value_matrix )
        writer.write_table()

Hope it helps. Thank you Raja G

MySQL Table Synchronization

MySQL Table Synchronization
=======================

Step: 1 Download the below package and sample script for table sysnchronization.

# cd /opt
# wget  https://static.spiceworks.com/images/how_to_steps/0000/3025/mysql-table-sync-0.9.3.tar.gz
# wget https://static.spiceworks.com/images/how_to_steps/0000/3026/syncTables.sh

Step: 2 Extract above package

# tar xzvf mysql-table-sync-0.9.3.tar.gz
# cd mysql-table-sync-0.9.3

Step: 3 Install perl-mysql modules

# yum install perl-ExtUtils-MakeMaker
# yum install "perl(DBD::mysql)"

Step: 4 Compile and install mysql-table-sync

# perl Makefile.PL
# make install



Step: 4 Then open syncTables.sh read configuration on top of the script. Change the configuration

Basic MySQL Administration

1. How to create a user and along with his privileges automatically ?

GRANT ALL PRIVILEGES ON database.* To 'username'@'hosr or IP ' IDENTIFIED BY 'password';
flush privileges;

2. How to give only specific privileges to users ?

GRANT select,lock tables ON *.* To 'user'@'host or IP ' IDENTIFIED BY 'password';
flush privileges;

3. How to see permissions of a user ?

show grants for 'username'@'hostanme';

4. How to revoke all given privileges ?

revoke all privileges on *.* from 'user'@'host';

5. How to take backup of database ?

mysqldump --user <username> --password=<password> databasename > databasebackup.sql

6. How to take backup of two databases ?

mysqldump --user <username> --password=<password> databasename1 databasename2 > databasebackup.sql

7. How to take backup of all databases in MySQL ?

mysqldump --user <username> --password=<password> -all-databases alldbbackup.sql

8. How to take backup of table in database ?

mysqldump --user <username> --password=<password> database_name database_table \   databasebackup.sql 

9. How to restore one database ?

mysql --user <username> --password=<password> databasename < databasebackup.sql
mysql_upgrade -p -u root --force