Skip to content

Instantly share code, notes, and snippets.

@etigui
etigui / slack.py
Last active June 15, 2023 21:18
Python - send file with Slack API using post request
# Send file with Slack API using post request
# Under "Add an OAuth scope" you need to add "files:write:user" permission
# https://api.slack.com/methods/files.upload#arg_title
# https://api.slack.com/types/file#file_types
def get_file_content(file_name):
content = ''
with open(file_name, 'rb') as f:
content = f.read()
@etigui
etigui / sort_dict.py
Last active October 14, 2020 12:08
Python - sort dict
mydict = {"carl": 40,"alan": 2,"bob": 1,"danny": 3}
# Sort a dict by key
for key in sorted(mydict.keys()):
print("%s: %s" % (key, mydict[key]))
# OUTPUT
alan: 2
bob: 1
carl: 40
@etigui
etigui / dico_list.py
Last active January 8, 2020 14:25
Appending to list in Python dictionary
from datetime import datetime
dico = dict()
dico.setdefault('42', []).append(datetime.now().strftime("%d-%m-%Y %H:%M:%S:%f"))
dico.setdefault('42', []).append(datetime.now().strftime("%d-%m-%Y %H:%M:%S:%f"))
dico.setdefault('777', []).append(datetime.now().strftime("%d-%m-%Y %H:%M:%S:%f"))
dico.setdefault('777', []).append(datetime.now().strftime("%d-%m-%Y %H:%M:%S:%f"))
print(dico)
@etigui
etigui / main.py
Created January 8, 2020 14:12
Parse Nginx "access.log" file to python object or json
def main():
ng = nginx.Nginx()
logs_parsed = ng.get_obj_parsed_logs('input.log')
for log in logs_parsed:
print(f'{log.date} {log.ip} {log.url} {log.bytes_sent} {log.referrer} {log.user_agent} {log.status} {log.method}')
ng.save_json_parsed_logs('input.log', 'output.josn')
if __name__ == "__main__":
@etigui
etigui / ws.py
Created November 12, 2019 17:35
Simple python web server (GET, POST, HEAD)
#!/usr/bin/env python
"""
Very simple HTTP server in python (Updated for Python 3.7)
Usage:
./dummy-web-server.py -h
./dummy-web-server.py -l localhost -p 8000
Send a GET request:
curl http://localhost:8000
Send a HEAD request:
curl -I http://localhost:8000
@etigui
etigui / repare.md
Last active October 30, 2019 16:47
Centos7: repare corupted/truncated/deleted file (from packages)

Centos7

If you got corupted/truncated/deleted file while updating/writing a bad commands, and as a result you cannot run service/app. You might be interested in this command to reinstall them:

rpm -Va --nodeps --nodigest --noscripts --nosignature --nofiledigest --nomode --nordev --nouser --nogroup --nomtime | grep '/lib64' | awk '/^S..../{system("rpm -qf "$2)}' | sort | uniq | xargs yum reinstall -y
  1. rpm
  • Verifying a package compare information about the installed files in the package with information about the files taken from the package metadata stored in the rpm database. Among other things, verifying compares the size, type, hash of each file.
  1. grep
  • Take only file located where you want to verify,
@etigui
etigui / commented_file.sh
Last active October 30, 2019 14:01
Bash: comment all python file lines
# One line (console)
while IFS= read -r line; do echo "# $line" >> output_commented.json; done < input.json
# Indented
while IFS= read -r line;
do
echo "# $line" >> output_commented.py
done < input.py
@etigui
etigui / list_dict_json.py
Created October 30, 2019 13:35
Python: add list to dict and save as json
name = dict()
name["data"] = list()
with open('file.json', 'a') as myfile:
for d in ["1", "2"]:
name["data"].append({"a": d[0], "b": d[1]})
json.dump(name, myfile, indent=2)
@etigui
etigui / test.py
Last active October 30, 2019 13:45
List comprehension vs. lambda + filter (json/SimpleNamespace)
# Ref: https://stackoverflow.com/questions/3013449/list-comprehension-vs-lambda-filter/3013686
# Ref json: https://catalog.data.gov/dataset/most-popular-baby-names-by-sex-and-mothers-ethnic-group-new-york-city-8c742
from types import SimpleNamespace
import json
import time
data_object = {}
with open('name.json', 'r') as f:
json_data = f.read()
data_object = json.loads(json_data, object_hook=lambda d: SimpleNamespace(**d))
@etigui
etigui / python_naming_conventions.md
Last active January 25, 2025 20:12
Python naming conventions

Python naming conventions

This document gives coding conventions example for the Python code. This style guide evolves over time as additional conventions are identified and past conventions are rendered obsolete by changes in the language itself.

1. General

  • Avoid using names that are too general or too wordy. Strike a good balance between the two.
    • Bad: data_structure, my_list, info_map, dictionary_for_the_purpose_of_storing_data_representing_word_definitions
    • Good: user_profile, menu_options, word_definitions
  • Never use the characters as single character variable names:
  • “l“ : lowercase letter el