Iterater and generator in Python

Try to answer the following question?

  1. How do generators save memory?

  2. When is the best time to use a generator?

  3. How can I use itertools to create complex generator workflows?

  4. When is lazy evaluation beneficial, and when is it not?

Tuple and list in Python

Try to answer the following question.

What are lists and tuples good for?

What is the complexity of a lookup in a list/tuple?

How is that complexity achieved?

What are the differences between lists and tuples?

How does appending to a list work?

When should I use lists and tuples?

Exception Control in Python

Pythoner may be familiar with exception control like the demo beblow there:

1
2
3
4
5
6
7
8
9
try:
i = 0
while True:
i += 1
except KeyboardInterrupt:
print "after abort:", i
print "get here"

Architecture of Python Virtual Machine

Python virtual machine is the core part of this language. After compiling the original python code into Opcode(byte code), python VM will take the job left. Python will take every opcode from PyCodeObject.

Web Crawler

Finished my first web scrawler … =_=|

Here is all about the spider. It’s small but funny.

After I finish this web scrawler, I learn that. The most important thing to build a web scrawler is not the code but the inverse process. How to build your program like a web browser.

Static web page is easy to get but how about a dynamic web page. Nowadays, more and more website use ajax to meet their bussiness requirement. If you just send a simple request to the server of that web site, you will find than the returned page is just only part of the total user page that you saw in the web browser.

Brief introduction to opcodes in Python

Python program is compiled down to bytecode which is sort of like assembly for the python virtual machine. The interpreter executes each of these bytecodes one by one.

Let’s look the following code. It’s easy to understand what it is doing without my explanation.

1
2
3
4
5
6
7
[demo.py]
x = 1
y = 2
z = x + y
print "x, y, z", x, y, z

Functional Programming in Python

Functional programming is not only use functions.
Think of “y = f(x)” or “h = f * g”

For comparasion, there are different paradigms in programming.

  • Structured/Procedural – Functions, loops, conditions
  • Object-Oriented Programming (OOP) – Classes, objects, methods
  • Functional Programming – Decorators, comprehensions, and generators
  • Logic Programming

Functions in Python are first class value. It can take functions as arguments and return functions like a value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
"""
function @calc take @f as an argument and use it as a function.
Something like function pointer in C.
"""
def calc(f, x, y):
return f(x, y)
def add(x, y):
return x + y
def sub(x, y):
return x - y
calc(add, 10, 20)

Dict Object in Python

Try to answer these question.

  1. Whatare dictionaries and sets good for?

  2. How are dictionaries and set the same?

  3. What is the overhead when using a dictionary?

  4. How can I optimize the performance of a dictionary?

  5. How does Python use dictionaries to keep track of namespace?

Int Object in Python

Preparation

Here, we are goint to analysis how Python deal with Int Object in itself.

First of all, we should have a environment which help us to explore the detail of the implementation in Python 2.7. At the same time, our operation shouldn’t influence the origianl Python in your workstation, if you have install Python before.

You can get the source code from offical website of Python. And you will find that there have a file configure in the directory of the source.

In the file configure, you can replace all string ‘Python’ with ‘pyLab’. And then move all file prefix with Python into pyLab(mainly in directory Misc/ and Modules)

Don’t forget to run the configuration file – configure with the option --prefix=Location where you want the Python which is for lab to install

Finally, just run make & make install

Repeat myself again, the reason that I move all ‘Python’ into ‘pyLab’ is all for making a difference with the original instance which is official and it shouldn’t be influence by any operation from us.

Rules of Object in Python.

  • Objects are structures allocated on the heap.
  • Objects are never allocated statically or on the stack; they must be accessed through special macros and functions only.

Machine Learning With K-Means

K-Means is a classical unsupervised clustering Learning Algorithm. The detail of the theory about K-Means that you can find it in Wikipedia. Now I introduce to implement this algorithm by myself.

If you are interesting in the implementation and change it into a better version, you could find it in my github repository and give me some advices. I will be appreciated.


So consider about if I want to classify the data into three different cluster. How could I make it?

images

Here is the result:

images

With the mean values:

1
2
3
4
Means =
[[3.5 1 6]
[1.66666 6 6]]