If you are using
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:
python #python3 #merge #dictionary
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
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
The important part of script starts from
As you can see it prints some default help for your barebone script. 21st line of script is
If you want to define an optional argument use
Finally the docstring inside of main method which is inside of triple quote will be shown when
That was easy right? I know, it was super simple compared to
#python #python3 #cli #click #argparse #optparse #argument
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
Gist
click python module for argument parsing using decorators
Extended unpacking (`Python 3 Only`):
This way first and last list element will be unpacked into
#python #python3 #unpack #extended_unpack
>>> 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
https://spyhce.com/blog/understanding-new-and-init
#python27 #python3 #__init__ #__new__ #old_style #new_style #OOP #class #super
#python27 #python3 #__init__ #__new__ #old_style #new_style #OOP #class #super
Spyhce
__new__ and __init__ | Spyhce blog
The __new__ and __init__ methods behave differently between themselves and between the old-style versus new-style python class definitions.
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:
This is how you can dump data and use it as your data backend for unittests.
#django #python3 #dumpdata #unittest
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)
In order to download a song copy the music link and:
Or in case you have a playlist link:
To read installation and more command options:
- https://github.com/ritiek/spotify-downloader
#python3 #spotdl #spotify #github
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
GitHub
GitHub - ritiek/spotify-downloader: Download your Spotify playlists and songs along with album art and metadata (from YouTube if…
Download your Spotify playlists and songs along with album art and metadata (from YouTube if a match is found). - ritiek/spotify-downloader
A screamingly fast Python 2/3 WSGI server written in C.
https://github.com/jonashaag/bjoern
#wsgi #python2 #python3
https://github.com/jonashaag/bjoern
#wsgi #python2 #python3
GitHub
GitHub - jonashaag/bjoern: A screamingly fast Python 2/3 WSGI server written in C.
A screamingly fast Python 2/3 WSGI server written in C. - jonashaag/bjoern