顯示具有 basic 標籤的文章。 顯示所有文章
顯示具有 basic 標籤的文章。 顯示所有文章

2015年1月14日 星期三

[AngularJS] Basic Concepts

Here are the basic concepts of Angularjs, these concepts are barely well documented by the officials.

  1. angular.config only accepts providers
  2. Every service and factory are the instances of the providers

    e.g.
    angular.module('myApp')
      .service('FooService', function(){
        //...etc
      })
      .config(function(FooServiceProvider){
        //...etc
      });
    
    
    
    Reference: http://stackoverflow.com/questions/17485900/injecting-dependencies-in-config-modules-angularjs

2014年12月27日 星期六

[Django] Human readable text in admin panel

In Django, when we are dealing with the admin panel, we can specify human readable text for the column through different methods


  1. Make it when building the corresponding model
    e.g.1: question_text = models.CharField('question', max_length=200)
    e.g.2: pub_date = models.DateTimeField('date published')

  2. Django makes the first parameter as the field show in admin UI, in this case "Date published" and "Question" will be shown.

  3. Or, you can make it through the admin.py, Django's build-in admin model

    short_description 
  4. e.g.: was_published_recently.short_description = 'Published recently?'

    The corresponding column will be shown with readable text

  5. Or you let Django do the rest of the job using class option 'list_display'

    list_display = ('first_name', 'last_name')

    Django will set column name as First name and  Last name where they are both 'database fields', underscores will be evaluated as spaces

[Python] printf in Python

We usually make use of the following syntax for printf

print ("I am a %s %s", %('good', 'boy'))

[Python] Basic data types

There are several data structures in Python, the following are some key notes for reference


  1. List
    • Example: a=[1, 2, 3, 4, 5]
    • Mutable (support modification of objects like a.append(6)
    • Support stack operations like "pop", "append"
    • looping (1 expression only) squares = [x**2 for x in range(10)]
    • looping using traditional for
      for x in [1,2,3]:
          x = x**2
  2. Tuple
    • Example: tup = (1, 2, 3, 'wahaha') (Always enclosed with parentheses!)
    • Immutable -> tup[0] = 100 -> Not allowed!!
    • Can contain lists
      tup = (['a', 'b'], [1, 2])
    • Tuple with single element should add trailing comma
      tupSingle = (a,)
  3. Sets
    • Example: a = set([1, 2, 3])
    • No element duplicate
    • Example: 1 in a , Output: True (Fast testing)
    • Mutable
  4. Dictionary
    • Pretty similar to associative arrays in PHP
    • {} -> Empty dict
    • Mutable
    • Example: cord = {'x' : 123.4, 'y' : 278.5}
    • Get keys -> cord.keys() -> return list ['x', 'y']
    • Fast Testing -> 'in'

[Python] __main__ and __name__

A module in python (usually in form of .py) will be executed by Python interpreter, we normally face the following situations when running python code.


  1. Running the .py directly, like python manage.py
  2. Imported from another module, like from admin import manage.py

So how do python know whether the module is directly run as a main program or it is just a part of another module? The special variable __name__ will do the job!


Take a look at this piece of code

if __name__ == '__main__':
    print 'I am run by itself'
else:
    print 'I am imported as module'

So if the .py is being run as a main program, the __name__ will have value __main__ being set, otherwise, the __name__ 's value will become the module name itself. We can therefore deal with different situations depending on the program's executing behaviour.