Posts

Showing posts from 2017

Python Defensive Iteration

This is the item 12 explained in the "Effective Python", by Brett Slatkin. Generators are the best save of out of memory problem. Here the problem, class StringToList(object): def __init__(self, text): self.text = text def __iter__(self): for x in self.text: yield x words = StringToList('Hello') # case 1 it = iter(words) it2 =iter(it) print(it, it2) #(<generator object __iter__ at 0x10816d7d0>, <generator object __iter__ at 0x10816d7d0>) next(it) # 'H' next(it2) # 'e' The problem is it and it2 pointing to the same instance and iteration is not as expected ('it2' give a next element as 'e' instead 'H'). To overcome this problem, author has suggested the following solution which is applicable to set and dict as well. In the case 1, same container is used. But in the case 2, different containsers. # case 2 it = iter(words) it2 =iter(words) print(it, it2) # (<generator

PyPI hosting on AWS S3

Create a web hosting in the AWS S3 It is a common task to create a web hosting in the S3 bucket. However, you have to change the permission to access the bucket via http protocol. For that you can right click the dist folder and select the Make public from the drop down menu. Create Distribution package Create a directory for distribution package source for example ojservice . Your folder structure is as follows: ojsevice+ | +index.html | +error.html | +LICENSE.txt | +README.txt | +setup.py | +ojservice+ | +__init__.py | +helloservice.py In the above structure, index.html and error.html are two files given in the AWS S3 host configuration under the bucket’s Static web hosting : respectively index and error documents. Here the helloservice.py: def hello (name) : return 'Hello {}

MSSQL on Mac

I was inspired to write this simple blog after reading the Microsoft Migrate a SQL Server database from Windows to Linux using backup and restore .This article about how to restore the AdventureWork on Linux, but this blog about MSSQL server 2017 on Mac OS Sierra. MSSQL available as a docker image to install on Mac. If you have Docker installed in your Mac, simple to install with two commands as shown in the http://aka.ms/sqldev site: docker pull microsoft/mssql -server -linux docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=yourStrong(!)Password' -p 1433 : 1433 -d microsoft/mssql -server -linux After the docker installation, login to the doker: docker exec -i -t < CONTAINER ID > /bin/bash Create a new folder “backup” in the “/var/opt/mssql”. If you are not admin run the command sudo su to login as super user. cd /var/opt/mssql/ mkdir backup Your work as admin in the docker is finished. Download the AdventureWork from https://msftdbpro

Collatz Conjecture in Python

Please read the wikipedia Collatz Conjecture first. Here the python function for the injecture: def collatz (n) : return n / 2 if n % 2 == 0 else 3 * n + 1 When you sequence , you get the result: n = 10 sequence = [n] while n != 1 : n = collatz(n) sequence.append(n) Generator functions are lazy. Using yield statement you can create generator function as follows: def collatz_iter (n) : while n != 1 : n = collatz(n) yield n Above function is lazy. It doesn’t compute, until iterate over that: s =collatz_iter( 10 ) for i in s: print(i) Reference: The Five Kinds of Python Functions by Steven F. Lott Publisher: O’Reilly Media, Inc.