Tech C**P
14 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
If you use json.dumps() in order to write a json into a file, it will be wriiten all in one line. You can do more with dumps(). You can pretty print into the file with indentation using indent parameter:

dumped_json = json.dumps(json.loads(content), indent=4)


You can do more and sort json keys! To do that pass sort_keys to dumps function like below:

dumped_json = json.dumps(json.loads(content), indent=4, sort_keys=True)

#python #json #dumps #indent #sort #sort_keys
Get the oldest elasticsearch index:

curl 'http://127.0.0.1:9200/_cat/indices' 2>&1 | awk '{print $3}' | grep "logstash_.*" | sort -t- -k2

DO NOT PANIC! Just enjoy it :)

First of all we use curl to get list of indexes from elasticsearch. By using awk with fetch just the 3rd column of the output, 3rd column refers to your index names (be careful to give your index name as there are internal indexes too and we do not want to purge them). grep command will then filter indexes and outputs those that start by logstash_, if yours are different change it. Finally the sort command sorts the result, but it first gets a delimiter by -t. sort -t- will split the column to TWO columns based on dash (-):

If my index name is logstash_data-20180619, it will exports 2 columns one is logstash_data and the other is 20180619. Now we use -k2 in order to sort based on the second column which is the date of the index.

This is how we can get the oldest elastic search index. I use this for maintenance of ES. In case disk space is almost full, I will delete the oldest elasticsearch index. You can even send a SLACK notification using cURL too.

The possibilities are endless.

Happy bashing :)

#linux #bash #curl #grep #sort #es #elasticsearch #split #awk #script
How to sort data based on a column in Pandas?

You can use sort_values in order to sort data in a dataframe:

df.sort_values(['credit'], ascending=False, inplace=True)

The above sample assumes that you have a data frame called df and sort it based on user credit. The sort order is Descending (ascending=False). and it sorts in place (You don't have to copy the result into a new dataframe).

#python #pandas #dataframe #sort #inplace
How do you sort the below object based on the value in Javascript?

{
"en": "English (English)",
"fa": "French",
"ar": "العربیه",
"zh": "中文 (Chinese)"
}

Well first you need to make an array of arrays:

var sortable_langs = [];
$.each(languages, function(index, language) {
sortable_langs.push([index, language]);
});

sortable_langs.sort(function(a, b) {
if(a[1] < b[1]) { return -1; }
if(a[1] > b[1]) { return 1; }
return 0;
});

NOTE: a[1] and b[1] refers to the language value like: English (English). They are compared with each other. If a[1] is bigger than b[1] then it will be moved to the first to the top.

#javascript #sort #alphabetically