Tag: python

Type Hints – Better type at Python

Python is known as a dynamic, strong-typed language. Most developers love it but some feel mad without type checking or type-hinted auto-completion. In Python3.5, Type Hints is introduced to further delight developers who want those features.

Type Hints offers type checking on function parameters, return values and class attributes, as if it’s static-typed. If you pass something does not match the expected type, a warning will be given.

According to The Theory of Type Hints, here’s an example showing how the rules work out in practice:

Say there is an Employee class, and a subclass Manager:

Let’s say variable e is declared with type Employee:

Now it’s OK to assign a Manager instance to e:

It’s not OK to assign an Employee instance to a variable declared with type Manager:

Now, suppose we have a variable whose type is Any :

It’s OK to assign a to Employee e:

Of course it’s also OK to assign Employee e to a:

Continue reading

Dockerizing our Python stack

… and it didn’t benefit that much.

Everyone in the DevOps community should have already heard about Docker.

There are always sysAdmin coming around and telling you how Docker has made his life easier, how well the automation goes or how lightweight the containers are…

So, what is Docker trying to solve?

Basically, Docker wraps up your application and all the dependencies required into a complete filesystem, that becomes a Docker Image. The next step is all about shipping this container to your production infrastructure, let it be AWS, Heroku or other servcies.

Back in the Pre-Docker age, every SysAdmin implements his own solution to package and deploy applications.

A small scale online shop might use git to deploy code and virtualenv to contain applications in an isolated environment. There were also existing solution providers – Heroku, Elastic Beanstalk, Google AppEngine and others services, having their own proprietary way for packaging and deploying applications.

Now, all the configurations and environment settings are standardized in the Docker Container, which actually saves loads of time for developers dealing with the repetitive setup and maintenance.

docker-2

(ref. https://www.docker.com/whatisdocker)

Continue reading

SQLAlchemy in batches: Generating a top playlist

SQLAlchemy is arguably the most powerful and ubiquitous ORM framework for Python.

At Oursky, we have been using SQLAlchemy for quite a period of time and appreciated the flexibility and elegance it provides over the Data Mapper abstraction. No doubt, it works very well for modern web applications but what about long-running background jobs? Would the abstraction get in your ways? (tl;dr: yes, but we still prefer it)

Here are some hands-on experiences from us.

We built a popular iOS application with a song recommendation system at the backend. The system suggests a top list for 20 popular songs.

Previously our editors hand-picked popular songs by download count and gather a new playlist as a recommendation to users. Now, we want to automate this process and generate the playlist weekly.

Continue reading

© 2017 Oursky Code Blog

Theme by Anders NorenUp ↑