Tech C**P
15 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
How to convert the below line of string:

data = '15 0 42 50 "some text" "" 4 4 "text"'

into:

[15, 0, 42, 50, 'some text', '', 4, 4, 'text']

As you may have noted you cannot use split as there are texts with spaces in between. For that we can use the below code:

import csv
import io

file = io.StringIO(data) # use io.BytesIO in python 2
reader = csv.reader(file, delimiter=' ')
split_data = next(reader)
parsed_data = [int(x) if x.isdigit() else x for x in split_data]

NOTE: use io.BytesIO instead of io.StringIO in case you are using python 2.

#python #csv #reader #split #stringIO #byteIO
How to disable visual block in VIM?

There is a feature in vim that as you select a text inside of vim, it turns the mode into VISUAL BLOCK. This is annoying for me in case you want to disable it put the below code in ~/.vimrc:

set mouse-=a

#vim #visual_block #mouse #set #vimrc
nethogs is used to monitor network traffic. You can see which processes use the most bandwidth and hogs the network.

Installtion on debian:

apt-get install nethogs

You can give the nethogs a network interface to see what's going on under the hood:

nethogs eth0


The output would something like:

PID USER       PROGRAM                                                       DEV        SENT      RECEIVED
9023 root python eth0 6.083 175.811 KB/sec
20745 root python eth0 2.449 45.715 KB/sec
11934 www-da.. nginx: worker process eth0 131.580 20.238 KB/sec
25925 root /usr/bin/python eth0 3.674 10.090 KB/sec

When nethogs is open, you can press r in order to sort based on RECEIVED or press s to sort based on SENT packets. To change the mode that it is shown for KB/sec press m multiple times and see the output.

#network #sysadmin #linux #nethogs #nethog #network #eth0
How to turn a dictionary into a string?

extra_data = {'iso': 'IR', 'address': 'Iran - Tehran - Azadi'}
' - '.join('{}:{}'.format(key, val) for key, val in extra_data.items())

The output would be:

iso:IR - address:Iran - Tehran - Azadi

NOTE: it could come in handy in case you want to store a variable dictionary structure into NO-SQL database.

#python #dictionary #string