2015年10月26日星期一

PYTHON编程技巧总结【2】

Commands in this section:
   assert Expression[, Arguments]
  type('a') == types.StringType
   subprocess.call / os.system
   profile cprofile pstats
   Python decorators



quite similar with "if condition: Do()"
assert 2 + 2 == 5, "Houston we've got a problem"
types.StringType can be used to check whether a variation is a STR.

Run C commands or other shell based commands using subprocess.call/os.system. Get the return values with subprocess.stdout
subprocess.call("exit 1", shell=True)

profile, cProfile, and pstats – Performance analysis of Python programs (website)


Decorators (website )
class safe:
  def __init__(self, function):
    self.function = function

  def __call__(self, *args):
    try:
      return self.function(*args)
    except Exception, e:
      print "Error: %s" % (e)
@safe
def unsafe(x):
  return 1 / x

print "unsafe(1): ", unsafe(1)
print "unsafe(0): ", unsafe(0)













2015年9月6日星期日

【table】dealing with tables in Astronomy

Here I gathered some softwares I used in daily works  related to tables. Hope it helps.


(1) Topcat.
You should try it anyway. It is graph interactive software. You can learn different formats for tables. Different tasks can be completed here including to match catalogs!

(2) STILTS
 It is a command line software. This actually is also the background command used in TOPCAT. So you can complete all the tasks of TOPCAT with this package.

My example to use this package. I transpose the ascii table which means to change the rows to columns and columns to rows by the following command:
   stilts tpipe ifmt=ascii ofmt=ascii in='m0717p_v1.tex' out='m0717p_v1.lis' cmd='transpose'

(3) asciidata  asciitable
Python package used to dealing with ascii table. Easy to use when you are familiar with Python. However, I think it takes time to learn about the details of each commands.

(4) Using readcols.py
A small program to read a table (Can be easily found from google like here.). I usually use this commands to read data. So so easy to use and you do not have to learn to much about how the program works.

2015年7月1日星期三

【PYTHON】Become Pythonic

I have been using PYTHON for more than two year. But when I checked the programs in my first few years. I feel it is so hard to read. So I was thinking if there is a standard style or a method to make beautiful python program. Just like we write scientific papers. Fortunately, there are many discussions about "Be pythonic". Many useful links are included bellow.

(1)  PPT for 

Be pythonic @StudyArea 台南 2012 10月份

byHsinYi Chen




Some tips to use python

(1) While using interactive model in command line, you can use "_" instead of copying the number calculated in the last command line.
(2) exchange the values in a and b, simply type: b,a = a,b 
(3) String formation
     str.format = format(...)
     "{0} {1} {2}".format(1, 2, 3)
     "{a} {b} {c}".format(a=1, b=2, c=3)
     "%" is not recommend
(4) Useful operators:  lambda, filter, reduce, map
f = lambda x, y : x + y
r = map(func, seq)
result = filter(lambda x: x % 2, fib)
reduce(lambda x,y: x+y, [47,11,42,13])
(5) Learn to use Decorator.
       @decorator
       def function(a, b):
             return(a)
(6) get the path of ***
       the path of a module : os.path.realpath(module.__file__)
       the path of the current script : os.path.realpath(__file__)
(7)  exec、 eval、 execfile and compile  (a link)
      exec 'print "Hello World"'

To be continue