Circular Import in Python
Recently, I found a really good example code for Python circular import, and I’d like to record it here.
Here is the code:
|
|
|
|
Guess what will happen if you run python X.py
and python Y.py
?
Recently, I found a really good example code for Python circular import, and I’d like to record it here.
Here is the code:
|
|
|
|
Guess what will happen if you run python X.py
and python Y.py
?
<hash,key,value>
in each slot(The slot content changed after Python 3.6).i = hash(key) & mask
where mask=table_size-1
to calculate which slot it should be placed. If the slot is occupied, CPython using a probing algorithm to find the empty slot to store new item.hash
and key
must be equal.When elements size is below 50000, the table size will increase by a factor of 4 based on used slots. Otherwise, it will increase by a factor of 2. The dictionary size is always \(2^{n}\).
PyTorch is a really powerful framework to build the machine learning models. Although some features is missing when compared with TensorFlow (For example, the early stop function, History to draw plot), its code style is more intuitive.
Torchtext is a NLP package which is also made by pytorch
team. It provide a way to read text, processing and iterate the texts.
Google Colab is a Jupyter notebook environment host by Google, you can use free GPU and TPU to run your modal.
CSRF(Cross-site request forgery) is a way to generate fake user request to target website. For example, on a malicious website A, there is a button, click it will send request to www.B.com/logout. When the user click this button, he will logout from website B unconsciously. Logout is not a big problem, but malicious website can generate more dangerous request like money transfer.
Each web framework has different approach to do CSRF protection. In Django, the validation process is below:
Recently, I’m working on a neo4j project. I use Py2neo
to interact with graph db. Although Py2neo
is a very Pythonic and easy to use, its performance is really poor. Sometimes I have to manually write cypher statement by myself if I can’t bear with the slow execution. Here is a small script which I use to compare the performance of 4 different ways to insert nodes.
import time
from graph_db import graph
from py2neo.data import Node, Subgraph
def delete_label(label):
graph.run('MATCH (n:{}) DETACH DELETE n'.format(label))
def delete_all():
print('delete all')
graph.run('match (n) detach delete n')
def count_label(label):
return len(graph.nodes.match(label))
def bench_create1():
print('Using py2neo one by one')
delete_label('test')
start = time.time()
tx = graph.begin()
for i in range(100000):
n = Node('test', id=i)
tx.create(n)
tx.commit()
print(time.time() - start)
print(count_label('test'))
delete_label('test')
def bench_create2():
print('Using cypher one by one')
delete_label('test')
start = time.time()
tx = graph.begin()
for i in range(100000):
tx.run('create (n:test {id: $id})', id=i)
if i and i % 1000 == 0:
tx.commit()
tx = graph.begin()
tx.commit()
print(time.time() - start)
print(count_label('test'))
delete_label('test')
def bench_create3():
print('Using Subgraph')
delete_label('test')
start = time.time()
tx = graph.begin()
nodes = []
for i in range(100000):
nodes.append(Node('test', id=i))
s = Subgraph(nodes=nodes)
tx.create(s)
tx.commit()
print(time.time() - start)
print(count_label('test'))
delete_label('test')
def bench_create4():
print('Using unwind')
delete_label('test')
start = time.time()
tx = graph.begin()
ids = list(range(100000))
tx.run('unwind $ids as id create (n:test {id: id})', ids=ids)
tx.commit()
print(time.time() - start)
print(count_label('test'))
delete_label('test')
def bench_create():
create_tests = [bench_create1, bench_create2, bench_create3, bench_create4]
print('testing create')
for i in create_tests:
i()
if __name__ == '__main__':
bench_create()
Apparently, using cypher with unwind
keyword is the fastest way to batch insert nodes.
Recently, I enjoy using Spacemacs
, so I decided to switch to org file from Markdown for writing blog. After several attempts, I managed to let Travis convert org file to HTML. Here are the steps.
First you need to install Org Mode plugin on your computer following the official guide: Nikola orgmode plugin.
conf.el
Org Mode
will convert to HTML to display on Nikola. Org Mode plugin will call Emacs to do this job. When I run nikola build
, it shows this message: Please install htmlize from https://github.com/hniksic/emacs-htmlize
. I’m using Spacemacs
, the htmlize
package is already downloaded if the org
layer is enabled. I just need to add htmlize folder to load-path. So here is the code: