Tech C**P
14 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
If you are using python 3.5 or greater and want to merge to dictionaries:
>>> a = {'a': 1}
>>> b = {'b': 2}
>>> {**a, **b}
{'a': 1, 'b': 2}

That's it! You have merged 2 dictionaries in the most simplest way ever. :)

If you are using python 2.7 or python 3.4 or less, merge dictionaries as below:
def merge_two_dicts(a, b):
c = a.copy()
c.update(b)
return c

python #python3 #merge #dictionary
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
Extended unpacking (`Python 3 Only`):

>>> a, *b, c = [1, 2, 3, 4, 5]
>>> a
1
>>> b
[2, 3, 4]
>>> c
5

This way first and last list element will be unpacked into a and c and other values will be inside of b variable.

#python #python3 #unpack #extended_unpack
In Django unittests you can use fixtures in order to create dummy data. To create fixtures you can use django dumpdata command to dump a specific data:

python3 manage.py dumpdata --indent 4 YOUR_APP.YOUR_TABLE > output_data.json


This is how you can dump data and use it as your data backend for unittests.

#django #python3 #dumpdata #unittest
How a programmer download from Spotify? (In case you cannot purchase the song and download them from IR like me)

spotdl is one of the great number one python 3 packages that downloads a song or playlist on your system.

In order to download a song copy the music link and:

spotdl --song https://open.spotify.com/track/2DGa7iaidT5s0qnINlwMjJ


Or in case you have a playlist link:

$ spotdl --playlist https://open.spotify.com/user/nocopyrightsounds/playlist/7sZbq8QGyMnhKPcLJvCUFD
INFO: Writing 62 tracks to ncs-releases.txt
$ spotdl --list ncs-releases.txt


To read installation and more command options:

- https://github.com/ritiek/spotify-downloader

#python3 #spotdl #spotify #github