2019年6月22日 星期六

Python on Chromebook

Now you can enable Linux on Chromebook:

https://support.google.com/chromebook/answer/9145439?hl=en

    > setting > Linux(測試版)



Python3 is installed in default.


You can also install anaconda if you prefer:

1. download the corresponding file:
     https://www.anaconda.com/distribution/#download-section

2. run the downloaded shell file:
  ~$ sudo bash ./Anaconda3-2019.03-Linux-x86_64.sh

   Please check the following website for details:
https://chromebook.home.blog/2019/01/20/installing-anaconda-on-a-chromebook-no-dev-beta-or-crouton-needed/

Then, install VSCode from the following site:
https://code.visualstudio.com/
(1. download the deb file, 2. double-click the file to install)


* install GCC:
   sudo apt-get install gcc

* how to zip / unzip files:
1. install 7-zip on Linux:
      $ sudo apt-get install p7zip-full
2. zip / unzip commands:
   $ 7z a newfile_name list_of_files
   $ 7z x zipfile_name


2019年4月1日 星期一

Main Function in Python

http://interactivepython.org/courselib/static/thinkcspy/Functions/mainfunction.html

In Python there is nothing special about the name main. We could have called this function anything we wanted. We chose main just to be consistent with some of the other languages.

Before the Python interpreter executes your program, it defines a few special variables. One of those variables is called __name__ and it is automatically set to the string value "__main__" when the program is being executed by itself in a standalone fashion. On the other hand, if the program is being imported by another program, then the __name__ variable is set to the name of that module. This means that we can know whether the program is being run by itself or whether it is being used by another program and based on that observation, we may or may not choose to execute some of the code that we have written.
For example, assume that we have written a collection of functions to do some simple math. We can include a main function to invoke these math functions. It is much more likely, however, that these functions will be imported by another program for some other purpose. In that case, we would not want to execute our main function.

def squareit(n):    
    return n * n

def cubeit(n):    
    return n*n*n

def main():    
    anum = int(input("Please enter a number"))
    print(squareit(anum))
    print(cubeit(anum))

if __name__ == "__main__":
    main()

2019年2月1日 星期五

X509 Certificates in PEM or DER format

How to convert to raw output:

> opalssl x509 -in test.pem -noout -text

in python code,

import OpenSSL.crypto
c = open('test.pem').read()
cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, c)
raw=str(OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_TEXT, cert), 'utf-8')
print(raw)

2019年1月29日 星期二

Random Number


The following code will print 10 pseudo-random integers between 1 and 100

import random
for x in range(10):
  print random.randint(1,101)
For security or cryptographic uses, see the secrets module.
https://docs.python.org/3/library/random.html


random.seed(a=Noneversion=2)

random.randrange(startstop[step])
random.randint(ab)
random.random()

2019年1月14日 星期一

適合所有中學生及初學者的 Online Judge 系統

因為系統會賦予數量不一的測試資料來測驗您的程式是否正確,因此必須先以一個 while 迴圈來讀取所有的測試資料。

Python example:
import sys
for s in sys.stdin:   
   print('hello, '+s)
If two inputs are required:

import sys
for s in sys.stdin:
    n = s.split()
    a = n[0]
    b = n[1]
    ...
  

Binary Data, String, and Integer Conversions in Python

In Python 3, struct  will interpret bytes as packed binary data: This module performs conversions between Python values and C structs rep...