python from __future__ import absolute_import

from __future__ import absolute_import

1.使用以上语句则启动绝对导入

会从 sys.path 里寻找所有名为 x 的顶层模块

2.而相应的相对导入则采用.

比如一个 package 下有 a.py 和 b.py 两个文件,在 a.py 里 from . import b 即是相对导入 b.py。

区别

PEP 328中:

1
2
3
4
5
6
7
8
9
10
package/
__init__.py
subpackage1/
__init__.py
moduleX.py
moduleY.py
subpackage2/
__init__.py
moduleZ.py
moduleA.py
1
2
3
4
5
6
7
8
from .moduleY import spam
from .moduleY import spam as ham
from . import moduleY
from ..subpackage1 import moduleY
from ..subpackage2.moduleZ import eggs
from ..moduleA import foo
from ...package import bar
from ...sys import path

一个.表示从当前目录寻找

两个.表示从父目录寻找

更多的.则从更高层级的目录寻找

django models mongoengine not in filter

Django: NOT IN -> exclude(a__in=b_list)

NOT IN: nin

1
2
3
4
5
6
7
8
9
10
11
12
ne – not equal to
lt – less than
lte – less than or equal to
gt – greater than
gte – greater than or equal to
not – negate a standard check, may be used before other operators (e.g. Q(age__not__mod=5))
in – value is in list (a list of values should be provided)
nin – value is not in list (a list of values should be provided)
mod – value % x == y, where x and y are two provided values
all – every item in list of values provided is in array
size – the size of the array is
exists – value for field exists

Offical Docs: Querying the database

check class has attribute

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import json
def json_load_byteified(file_handle):
return _byteify(
json.load(file_handle, object_hook=_byteify),
ignore_dicts=True
)
def json_loads_byteified(json_text):
return _byteify(
json.loads(json_text, object_hook=_byteify),
ignore_dicts=True
)
def _byteify(data, ignore_dicts = False):
# if this is a unicode string, return its string representation
if isinstance(data, unicode):
return data.encode('utf-8')
# if this is a list of values, return list of byteified values
if isinstance(data, list):
return [ _byteify(item, ignore_dicts=True) for item in data ]
# if this is a dictionary, return dictionary of byteified keys and values
# but only if we haven't already byteified it
if isinstance(data, dict) and not ignore_dicts:
return {
_byteify(key, ignore_dicts=True): _byteify(value, ignore_dicts=True)
for key, value in data.iteritems()
}
# if it's anything else, return it in its original form
return data

Example usage:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
>>> json_loads_byteified('{"Hello": "World"}')
{'Hello': 'World'}
>>> json_loads_byteified('"I am a top-level string"')
'I am a top-level string'
>>> json_loads_byteified('7')
7
>>> json_loads_byteified('["I am inside a list"]')
['I am inside a list']
>>> json_loads_byteified('[[[[[[[["I am inside a big nest of lists"]]]]]]]]')
[[[[[[[['I am inside a big nest of lists']]]]]]]]
>>> json_loads_byteified('{"foo": "bar", "things": [7, {"qux": "baz", "moo": {"cow": ["milk"]}}]}')
{'things': [7, {'qux': 'baz', 'moo': {'cow': ['milk']}}], 'foo': 'bar'}
>>> json_load_byteified(open('somefile.json'))
{'more json': 'from a file'}

stackoverflow

linux grep reg content

grep -P

awk -F”[123]”

1
cat some.log* | grep -P 'abc.*]' -o | awk -F'|' '{print $2}' | grep -v 0

grep -P切出想要的内容

参考链接

1
awk -F"[@ /t]" '{print $2,$3}' test

以@,空格,Tab键分割test文件的每一行,并输出第二、第三列。

linux sync folder by rsync

rsync

rsync -avz --delete "/home/user/A" "/home/user/B"

Explanation by SonicARG

-a Do the sync preserving all filesystem attributes

-v run verbosely

-z compress the data during the sync (transport the data in compressed mode)

--delete delete the files in target folder that do not exist in the source, /home/user/A: source folder, /home/user/B: target folder

exclude .git

--exclude='.git/'

How to use rsync to backup a directory without git subdirectory

How to sync two folders with command line tools?