Today's Question:  What does your personal desk look like?        GIVE A SHOUT

 PYTHON


  Python basics summary

Python is recommended by many people as the first language to be learned now. Even in some universities, it is the primary language to be taught to CS/CE students. To master this languages, we should understand some basics of it. Here we summarize some basics about this language.These basics include : string replacement with regular expression, traverse a directory, sort a list, remove duplication, dictionary ordering, dictionary, list, string conversion, date object manipulation, command line parameter parsing(getopt), formatted output, Call system commands or scripts with Python, Python file...

11,112 0       SUMMARY PYTHON


  The tic-tac-toe game with Python

In this tutorial, we will show how to build a tic-tac-toe game with Python. We will use functions,arrays,if statements,while statements, for loops and error handling etc.First, we need to create two functions, the first function will display the borad of the game:def print_board(): for i in range(0,3): for j in range(0,3): print map[2-i][j], if j != 2: print "|", print ""Here we used two for loops to loop through the map, this map is a two dimensional array which contains the location information.The board looks like: | | | | |...

24,927 1       PYTHON TIC-TAC-TOE


  A Python assignment trap

Python has no assignment, it only has reference. Assume, we have following code snippet:>>> values = [0, 1, 2]>>> values[1] = values>>> values[0, [...], 2]Why the result is not [0, [0, 1, 2], 2], instead it goes into an infinite loop? To understand this, we need to understand some basics about Python.Python has no variables, it only has labels. When we run:values = [0, 1, 2]Python will first create a list object [0,1,2], then it labels it as values. If we later run:values = [3, 4, 5]Python will then create a new list object [3,4,5] and it will tear off the values lab...

9,425 0       PYTHON TRAP ASSIGNMENT SHALLOW COPY


  Using Fabric to deploy web app

Many people may use FTP and rsync to synchronize codes to server, this way is fine but it may be troublesome when you need to deploy many times a day, actually there is a simple way if you can spend time on finding the fast way. We introduce Fabric today for deploying web app to remote server.Fabric is a deployment tool written with Python, the biggest feature if it is you no need to login to remote server, you can execute remote commands locally.Here is s simple deployment script written with Python : fabfile.py(Do not change the file name). Put it in your project directory.#!/usr/bin/env pyt...

6,732 0       PYTHON SSH FABRIC WEB DEPLOYMENT


  Build route graph of Hurricane Sandy

Hurricane Sandy swept US east side and landed in New York, it killed 113 persons and incurred 50 billion US dollars economic loss. Sandy is also considered as the most expensive hurricane. We will now use matplotlib and basemap libraries in Python to build a route graph of Sandy.Below is the animated GIF.Shadow is added in the graph to show the time at night, we can see from the graph that Sandy stayed a while in Panama after emerging, then it went through Cuba and became Hurricane-2. Later Sandy moved along US east coast and then turned to west suddenly, headed directly to New York and landed...

14,031 0       PYTHON HURRICANE SANDY ROUTE GRAPH


  Python object creation sequence

[The Python version described in this article is 3.x]This article aims to explore the process of creating new objects in Python. As I explained in a previous article, object creation is just a special case of calling a callable. Consider this Python code:class Joe: passj = Joe()What happens when j = Joe() is executed? Python sees it as a call to the callable Joe, and routes it to the internal function PyObject_Call, with Joe passed as the first argument. PyObject_Call looks at the type of its first argument to extract its tp_call attribute.Now, what is the type of Joe? Whenever we define a ...

2,285 0       PYTHON OBJECT CREATION


  Python internals: how callables work

[The Python version described in this article is 3.x, more specifically - the 3.3 alpha release of CPython.]The concept of a callable is fundamental in Python. When thinking about what can be "called", the immediately obvious answer is functions. Whether it’s user defined functions (written by you), or builtin functions (most probably implemented in C inside the CPython interpreter), functions were meant to be called, right?Well, there are also methods, but they’re not very interesting because they’re just special functions that are bound to objects. What else can be call...

1,901 0       PYTHON RATIONALE CALLABLE WORK


  Currying in Python

What is Currying?Currying is like a kind of incremental binding of function arguments. Let’s define a simple function which takes 5 arguments:1def f(a, b, c, d, e):2    print(a, b, c, d, e)In a language where currying is supported, f is a function which takes one argument (a) and returns a function which takes 4 arguments. This means that f(5) is the following function:1def g(b, c, d, e):2    f(5, b, c, d, e)We could emulate this behavior the following way:1def f(a):2    def g(b, c, d, e):3      &n...

4,283 0       PYTHON CURRING BINDING IMPLEMENT