2017年5月22日 星期一

Python quick lookup - basic

#operator

# not equal
!=

# shift

<< >>

# xor

^

#modules

random

random.random()

# they are equal
random.randint(1,5)
random.choice( [1,2,3,4,5] )

math

math.ceil()

#loop

# foreach
days = ['Monday', 'Tuesday']
for day in days:

# range from 0 to 9
for i in range(10):

# range with step as 2
for i in range(0, 10, 2):

# foreach in dictionary
menu_prices = { '1': 0.5, '2': 0.3, '3': 0.7 }
for key, value in menu_prices.items():
    print( key, ': $', value )

# while
while <condition not matched>:
while (True):
   if <condition>:
      continue
   if <condition>:
      break

#list

# getting length of list
days = ['Monday', 'Tuesday']
length_of_list_days = len( days )

# removing item from list
days.remove('Monday')
del days[0]

# try getting item from dict, return None if not exist
days.get(key) 

#print 

# automatic separated with space
print( item1, item2 )

# specify separator
print( item1, item2, sep='' )

# print with format
print( format(value, '.2f') )


#string

# reverse a string
'hello world'[::-1] # [begin:end:step]
reversed('hello world')

2015年7月30日 星期四

Nodejs

Core

-- Download node.js --

-- Download npm --

-- Download nodejs version manager for windows --


Commands to start

-- Install modules --

npm install _package_
npm install -g _package_ (globally instead of project folder only)

-- Run --

node _js_file
nodemon _js_file_ (require `npm install nodemon`)

Implementation samples


require('_moduel_or_file_name')
- as include

module.exports = function () {
  var service = {
    exposedMember: _privateMember
  }
  return service;
}
- to expose functions when required by other files

async module
- to simulate synchronized flow

2015年7月27日 星期一

Resume-able HTTP download (continuous downloading)

Required headers in the initial HTTP GET request

Range: (leave value empty to get Accept-Range type and ETag in response headers)

Required headers in the subsequent HTTP GET request

If-Range: [ETag]
Range: [type]=[start]-[end]
  • start from 0
  • end can be optional to retrieve to all
  • type can be bytes and all 

Response status

206 - Partial Content
200 - OK for the whole content in response body

Response headers

Content-Range: [type] [start]-[end]/[max]
  • max is the overall size of the file

Reference

http://stackoverflow.com/questions/8293687/sample-http-range-request-session
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.16
http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.12
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35