Tech C**P
14 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
In designing API SDKs and documentation it is really annoying to put a whole lot of time to design the documentation and/or designing SDKs in different languages! How do you create SDKs for C#, python, Go, .NET, etc when you are limited in resources? IT world has gone so far, that creation of SDKs for the API or creation of documentation (with great web UI) have been made automatic. No one need to interfere in between.

As well as other programs or languages it needs some specification that all needs to follow. OpenAPI is the thing!

What is OpenAPI?
The OpenAPI Specification (OAS) defines a standard, language-agnostic interface to RESTful APIs which allows both humans and computers to discover and understand the capabilities of the service without access to source code, documentation, or through network traffic inspection. When properly defined, a consumer can understand and interact with the remote service with a minimal amount of implementation logic.

One of the great editors for designing the spec is swagger live editor:
- https://editor.swagger.io/

The full open specification of OpenAPI 3.0:
- https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md

I should again note that this is a wonderful spec and makes your life easier if you master the specification. A sample documentation of OpenAPI is Alopayk:
- https://docs.alopeyk.com/

#openapi #swagger #spec #restful #api #alopayk
Did you know that you can monitor redis commands live from within redis-cli console?

Go to redis client by typing redis-cli in terminal and then type monitor command and enter:

127.0.0.1:6379> monitor
OK
1514301845.678553 [0 127.0.0.1:59388] "COMMAND"
1514301859.676761 [0 127.0.0.1:59388] "HSET" "user" "name" "ali"

It will log everything that happens on your redis server.

#redis #redis_cli #cli #monitor
In case all of a sudden you fell on Outlook for mail templates (yes that sucks!), and you have problem for right to left languages like Farsi read on.

Outlook removes styles from your tags, you need to provide Outlook specific tags for direction named dir. It will solve your RTL problems:
<span dir="rtl"></span>

NOTE: styles like "direction:rtl" wont work on outlook. Beside direction for other mail clients you need to provide dir attribute.

#outlook #email #direction
Nearly all of you have created a python script throughout your career, in case you are a python programmer! Usually scripts take arguments, provide help, optional arguments and more.

Working with modules like optparse and argparse do the job, but they are not powerful enough in case your are designing a complex script like pip CLI script! click python module is at your service.


NOTE: The biggest difference between optparse and argparse is that optparse is deprecated since Python 3.2 and argparse is considered the standard for implementing CLIs in Python.



click do a similar task akin to optparse and argparse but in a nicer way by using decorators.

Take a look at the below github gist and let's discuss about different parts of it:
- https://gist.github.com/alirezastack/cccb70640c5e5881fa71b23966707f8f

The above gist is a sample weather app using python 3. SAMPLE_API_KEY is used to send requests to openweathermap API. current_weather is a regular method like other python methods.

The important part of script starts from @click.command(). This command defines the main method as a cli app. If you just put this command before your main method and run your script as below, it will prints help instruction of your script:
$ python cli.py --help
Usage: cli.py [OPTIONS]

Options:
--help Show this message and exit.

As you can see it prints some default help for your barebone script. 21st line of script is @click.argument('location') that defines a required parameter for you CLI app called location.

If you want to define an optional argument use @click.option. As you can note we have used --api-key, -a in the same argument. It helps users to use the shortcut version or human readable format of the argument. Both refers to the same argument and it will be mapped to api_key method variable (snake case).

help in click.option is used to give a brief guide for the argument when --help is used.

Finally the docstring inside of main method which is inside of triple quote will be shown when --help is used.

NOTE: snake case is a procedure that removes dashes from the begining of an input argument and turns dash into underscore. So something like --format-type will be converted to format_type.

That was easy right? I know, it was super simple compared to argparse and optparse. Rock on!

#python #python3 #cli #click #argparse #optparse #argument
How to kill a process in mongo shell?

If you are unfamiliar with your current database schema and indexes, or run a heavy query by mistake on production database things go nasty! To find the query and kill it run mongo client as below and run currentOp command:
> mongo
> db.currentOp()

currentOp() database method will display the ongoing queries in json format, find the query which is related to you and get its opid. Now you need
to run killOp() database method to kill that process as below:
db.killOp(229)

229 is the operation id we have got from the first command.

NOTE: terminate running operations with extreme caution. Only use db.killOp() to terminate operations initiated by clients and do not terminate internal database operations.

#mongodb #mongo #killOp #currentOp #query
If your in docker swarm and you want to see log data of a specific service you can use --since as below:

docker service logs project_redis --since "1m" -f

It sometimes has unexpected behaviours and does not print logs. Rather than --since you can use tail it is better in case you want to see recent logs:

docker service logs project_redis --tail 1

#docker #swarm #since #tail #log
What is Capped Collections in MongoDB?

Capped collections are fixed-size collections that support high-throughput operations that insert and retrieve documents based on insertion order. Capped collections work in a way similar to circular buffers: once a collection fills its allocated space, it makes room for new documents by overwriting the oldest documents in the collection.

Circular buffers are filled and emptied, so capped collection has similar behaviours.

Some notes:
- Automatic Removal of Oldest Documents
- Capped collections have an _id field and an index on the _id field by default.
- You cannot delete documents from a capped collection. To remove all documents from a collection, use the drop() method to drop the collection and recreate the capped collection.
- You cannot shard a capped collection.
- You must create capped collections explicitly

To create a capped collection:

db.createCollection( "cpu_metrics", { capped: true, size: 100000 } )

The name of the collection in our example is cpu_metrics with the flag of capped=true and size of the collection.

You can also specify maximum number of documents for the collection using max as following example:

db.createCollection("cpu_metrics", { capped : true, size : 5242880, max : 5000 } )

NOTE: the size argument is always required, even when you specify max number of documents.

Check whether collection is capped or not:

db.collection.isCapped()


Convert a non-capped collection to capped:

db.runCommand({"convertToCapped": "mycoll", size: 100000});

#mongodb #mongo #capped #collection #capped_collection
When you see logs in docker you cannot use grep on the output. In case you want to put grep on it you need to send data to standard output (2>&1).

Long story, short:
docker service logs --since "1m" -f app_redis 2>&1 | grep "Your search text"


#docker #log #logs #since #grep
How to get IP address of a docker container?

First you need to issue the following command to get the container id (first column):
docker ps

Use the container ID to run:
docker inspect <container ID>

At the bottom,under NetworkSettings, you can find IPAddress.

#docker #ip_address #container #ps #ip #inspect
Change default editor of crontab in linux from nano to vim:

export EDITOR=vim

You can put this code inside of ~/.bashrc.

NOTE: export is used to set an environment variable in linux.

#linux #crontab #editor #vim #nano