Python Geek blog - Articles by Andrii Soldatenko

Python Geek blog (Graphs)-[:ARE]->(Everywhere)

Articles by:

Andrii Soldatenko photo of Andrii Soldatenko Python Backend Developer asoldatenko.com Kyiv Ukraine

Eloquent fibonacci sequences in python

If by chance I have omitted anything more or less proper or necessary, I beg forgiveness, since there is no one who is without fault and circumspect in all matters.”

― Leonardo Bonacci - italian mathematician

Leonardo Bonacci

Quick introduction

The idea of this article to collect eloquent python patterns using well-known Fibonacci sequence.

Recursive approach

cat fibonacci1.py
def fibonacci1(n):
    if n < 2:
        return n
    return fibonacci1(n - 2) + fibonacci1(n - 1)

if __name__ == '__main__':
    from timeit import timeit
    print(timeit("fibonacci1(5)", setup="from __main__ import fibonacci1")) 
$ python3 fibonacci1_bench.py # On my MacBook Pro (Mid 2015) 2.5 GHz Intel Core i7, 16 GB 1600 MHz DDR3
2.0335577040023054

Recursive approach using caching

My favorite advice: you should know you language standard library. You can find lot’s of info under functools module docs. Default maxsize is 128 for lru_cache decorator.

$ cat fibonacci2.py
import functools


@functools.lru_cache()
def fibonacci2(n):
    if n < 2:
        return n
    return fibonacci2(n - 2) + fibonacci2(n - 1)


if __name__ == '__main__':
    from timeit import timeit
    print(timeit("fibonacci2(5)", setup="from __main__ import fibonacci2"))
$ python3 fibonacci2_bench.py # On my MacBook Pro (Mid 2015) 2.5 GHz Intel Core i7, 16 GB 1600 MHz DDR3
0.09731649799505249
$ cat fibonacci_generator.py

Generator approach for using yield

def fibonacci_generator():
    a, b = 0, 1
    while True:
        yield a
        a, b = b, a + b

if __name__ == '__main__':
    from timeit import timeit
    print(timeit("list(itertools.islice(fibonacci_generator(), 5))",
                 setup="from __main__ import fibonacci_generator"))
$ python3 fibonacci_generator.py # On my MacBook Pro (Mid 2015) 2.5 GHz Intel Core i7, 16 GB 1600 MHz DDR3
1.1730475709991879

About timeit instead of conclusion.

Also keep in mind:

Note By default, timeit() temporarily turns off garbage collection during the timing. The advantage of this approach is that it makes independent timings more comparable. This disadvantage is that GC may be an important component of the performance of the function being measured. If so, GC can be re-enabled as the first statement in the setup string.

More info about timeit Lib/timeit.py

Can I copy string in Python 3.5? And how?

“Mathematics reveals its secrets only to those who approach it with pure love, for its own beauty.”

― Archimedes

Domenico Fetti Archimedes

Quick introduction

Why do you need to copy a Python strings? It’s interesting question, because Python string is immutable. Also any tries of copy will returns the original string. Python tries to keep just the one copy, as that makes dictionary lookups faster.

May be use slice?

$ python3
Python 3.5.2 (default, Sep 15 2016, 07:38:42)
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 'python'
>>> b = a[:]
>>> b = a[:]
>>> id(a), id(b)
(4400931648, 4400931648)

Add empty string? No!

>>> a = 'python'
>>> id(a)
4400931648
>>> b = a + ''
>>> id(b)
4400931648  

Or use str function?

>>> a = 'python'
>>> id(a)
4400931648
>>> b =str(a)
>>> id(b)
4400931648

Try to use copy?

>>> a = 'python'
>>> id(a)
4400931648
>>> import copy
>>> b = copy.copy(a)
>>> id(b)
4400931648

Let’s do deepcopy!

>>> a = 'python'
>>> id(a)
4400931648
>>> b = copy.deepcopy(a)
>>> id(b)
4400931648
>>> print('No chance!')
No chance!

Another tries with using slice?

>>> a = 'python'
>>> b = (a + '.')[:-1]
>>> id(a)
4400931648
>>> id(b)
4400931760
>>> print('Eureka!')
Eureka!

Last try with encode and decode

>>> a = 'python'
>>> id(a)
4400931648
>>> b = a.encode().decode()
>>> b
'python'
>>> id(b)
4400931984
>>> print('Eureka!')
Eureka!

Conclusion

Try to answer the first question:

Can I copy or clone string in Python?

Answer is: no you can’t.

Every time we create new string, both working examples uses the same idea:

>>> a = 'a' * 1024
>>> b = 'a' * 1024
>>> id(a)
140726834891776
>>> id(b)
140726843315712

P.S. Thanks for ideas from Pavel’s talk from PyCon Russian 2016 and some more info you can find in references.

References

A collection of Python “must reads”

Articles:

Books:

Other python must read lists:

EuroPython 2016

What is the best full text search engine for python?

Introduction

Nowadays we can see lot’s of benchmarks and performance tests of different web frameworks and Python tools. Regarding to search engines, it’s difficult to find useful information especially benchmarks or comparing between different search engines. It’s difficult to manage what search engine you should select for instance, ElasticSearch, Postgres Full Text Search or may be Sphinx or Whoosh. You face a difficult choice, that’s why I am pleased to share with you my acquired experience and benchmarks and focus on how to compare full text search engines for Python.

You can watch my video from EuroPython 2016 in Bilbao.

Building social network with Neo4j and Python

Introduction

Social phenomena is coming. We have lot’s of social applications that we are using every day, let’s say Facebook, twitter, instagram. Lot’s of such kind apps based on social graph and graph theory. I would like to share my knowledge and expertise about how to work with graphs and build large social graph as engine for Social network using python and Graph databases. We’ll compare SQL and NoSQL approaches for friends relationships.

You can find my slides from PyCon Russia 2016.

A collection of sport programming “must reads”

How to Practice

Online Contests:

Books

Materials & articles

PyCon UA 2016 - What is the best full text search engine for Python?

Abstract

Nowadays we can see lot’s of benchmarks and performance tests of different web frameworks and Python tools. Regarding to search engines, it’s difficult to find useful information especially benchmarks or comparing between different search engines. It’s difficult to manage what search engine you should select for instance, ElasticSearch, Postgres Full Text Search or may be Sphinx or Whoosh. You face a difficult choice, that’s why I am pleased to share with you my acquired experience and benchmarks and focus on how to compare full text search engines for Python.

Video

You can watch video from PyCon Ukraine 2016.

Slides

It’s was my second Python Conference in Ukraine in cultural city in Lviv. Slides you can find below from PyCon Ukraine 2016.

Practical continuous quality gates for development process - Selenium Camp 2016

Abstract

There are a lot of books and publications about the continuous integration in the world. But in my experience it’s difficult to find information about how to open quality gates between automated tests and to continuous integration practice to in your current project. After reading several articles and even a couple of books you will understand how to work with it. But what next? I will share with you practical tips and tricks on how to lift iron curtain to your automated tests before a continuous quality practice today. It is for this reason why I am pleased to share with you my acquired experience in my presentation.

Video

You can find my video from Selenium Camp 2016.

Slides

My slides from Selenium Camp 2016.

About Selenim Camp 2016 Conference

Selenium Camp is automated testing conference in Europe, which is focused on usage of Selenium Webdriver.

Погружение в полнотекстовый поиск, используя Python - PyCon Russia 2015

Введение

В наши дни мир тонет в текстовой информации. Все эти годы существующие технологии были сфокусированы на хранении и структурировании данных. Но что же делать, если нам необходимо принимать решение в режиме реального времени, используя эти данные? Я с радостью поделюсь накопленным опытом разработки веб-приложений, использующих полнотекстовый поиск, которые написаны на Python с использованием Django-фреймворка, а также расскажу о результатах исследования разных поисковых движков и интеграции веб-приложений с haystack и elasticsearch. Также поговорим о будущем полнотекстового поиска в контексте Django 1.9 и Python.

Видео

Все видео можно найти на habrahabr

Слайды

Мои слайды с конференции PyCon Russia 2015.

August Python Minsk Meetup

Dive into full text search with Python

  • Getting started with full text search (FTS);
  • Index managment;
  • How to add FTS to typical Django project;
  • FTS based on database PostgreSQL;
  • FTS based on Apache Lucene:
  • Quick overview ElasticSearch;
  • Quick overview Apache Solr;
  • Quick overview;
  • Performance of PostgreSQL FTS and elasticsearch / Apache Solr / Shinx.
  • What is the best approach for django haystack and elasticsearch?
  • Natural text processing with NTLK;
  • Future of Django 1.9, FTS and Python;

Video

You can watch video from Python Minsk Meetup 2015. (1,507 watches on youtube)