Tech C**P
15 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
If you want to find an element inside of a list in MongoDB document you can use the simple find command. Let's say you have the below documents:
[{
"food": "Ghorme",
"creation_date": "2017-10-12 12:00:00"
"rates": ["bad", "so_so", "not_bad", "mmmm"]
},{
"food": "Kebab",
"creation_date": "2017-10-07 22:10:00"
"rates": ["great", "not_bad", "great", "great"]
}]

Now if you want to find a document that has the value of bad in rates, you just use the below find query:
db.foods.find({rates: "bad" }).pretty()

#mongodb #mongo #find #list #array
Some useful pip (python package manager) command:

show: used to know specific information about a module installed on your system:
$ pip show requests
Name: requests
Version: 2.13.0
Summary: Python HTTP for Humans.
Home-page: http://python-requests.org
Author: Kenneth Reitz
Author-email: me@kennethreitz.com
License: Apache 2.0
Location: /Library/Python/2.7/site-packages
Requires:

search: used to search a python package on pypi.python.org:
$ pip search "suds"
suds-aop (0.6.1) - Lightweight SOAP client (aop's fork)
...
...
...
NOTE: the above list is truncated.

list: list installed packages.
pip list

NOTE: you can grep on pip list to see if a specific package is installed or check its version:
pip list | grep "requests"

freeze: this command does the the same task as list, but its output can be placed inside of the requirements.txt.

uninstall: remove a package from your system:
pip uninstall requests

#python #pip #list #freeze #show #info #search
What is LPUSH in REDIS:

Insert all the specified values at the head of the list stored at key. If key does not exist, it is created as empty list before performing the push operations. When key holds a value that is not a list, an error is returned.

It is possible to push multiple elements using a single command call just specifying multiple arguments at the end of the command. Elements are inserted one after the other to the head of the list, from the leftmost element to the rightmost element. So for instance the command LPUSH mylist a b c will result into a list containing c as first element, b as second element and a as third element.

Return value:
Integer reply: the length of the list after the push operations.

For instance:
redis> LPUSH mylist "world"
(integer) 1
redis> LPUSH mylist "hello"
(integer) 2
redis> LRANGE mylist 0 -1
1) "hello"
2) "world"

NOTE1: time complexity of LPUSH command is O(1). So it is the best from performance point of view.

NOTE2: `LRANGE is used to get list members, if you use 0 to -1 it will return all list elements.

#redis #list #lpush #push
tuples vs list from a different point of view. Tuples of constants can be precomputed by Python's peephole optimizer or AST-optimizer. Lists, on the other hand, get built-up from scratch:

>>> from dis import dis 

>>> dis(compile("(10, 'abc')", '', 'eval'))
1 0 LOAD_CONST 2 ((10, 'abc'))
3 RETURN_VALUE

>>> dis(compile("[10, 'abc']", '', 'eval'))
1 0 LOAD_CONST 0 (10)
3 LOAD_CONST 1 ('abc')
6 BUILD_LIST 2
9 RETURN_VALUE


#python #list #tuple #performance #dis #compile #ast_optimizer
How to add nested documents in Marshmallow python?

books = fields.List(fields.Dict(
keys=fields.String(validate=OneOf(('title', 'author', 'publication_date'))),
values=fields.String(required=True)))


#python #data_class #marshmallow #fields #list #OneOf