2018年7月12日 星期四

[ Python 常見問題 ] Splitting on first occurrence

Source From Here 
Question 
What would be the best way to split a string on the first occurrence of a delimiter? 
For example: 
>>> s = 'John: Hello, my link is http://abc.com' 
>>> s.split() // Split with space 
['John:', 'Hello,', 'my', 'link', 'is', 'http://abc.com'] 
>>> s.split(':') // Split with delimiter ':' 
['John', ' Hello, my link is http', '//abc.com']

How do I split with first ':' to get: ['John', 'Hello, my link is http://abc.com'] ? 

How-To 
From the docs: 
str.split([sep[, maxsplit]]) 
Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements).

So the sample code: 
>>> map(lambda e: e.strip(), s.split(':', 1)) 
['John', 'Hello, my link is http://abc.com']


[ Python 常見問題 ] Running shell command from Python and capturing the output

Source From Here 
Question 
I want to write a function that will execute a shell command and return its output as a string, no matter, is it an error or success message. I just want to get the same result that I would have gotten with the command line. What would be a code example that would do such a thing to capture output in executing shell command from Python? 

How-To 
The answer to this question depends on the version of Python you're using. The simplest approach is to use the subprocess.check_output function: 
>>> import subprocess
>>> subprocess.check_output(['ls', '-l'])
b'total 0\n-rw-r--r-- 1 memyself staff 0 Mar 14 11:04 files\n'

check_output runs a single program that takes only arguments as input.1 It returns the result exactly as printed to stdout. If you need to write input to stdin, skip ahead to the run or Popen sections. If you want to execute complex shell commands, see the note on shell=True at the end of this answer. 

The check_output function works on almost all versions of Python still in wide use (2.7+).2 But for more recent versions, it is no longer the recommended approach. 

Modern versions of Python (3.5 or higher): run 
If you're using Python 3.5 or higher, and do not need backwards compatibility, the new run function is recommended. It provides a very general, high-level API for the subprocess module. To capture the output of a program, pass the subprocess.PIPE flag to the stdout keyword argument. Then access the stdout attribute of the returned CompletedProcess object: 
>>> import subprocess
>>> result = subprocess.run(['ls', '-l'], stdout=subprocess.PIPE)
>>> result.stdout
b'total 0\n-rw-r--r-- 1 memyself staff 0 Mar 14 11:04 files\n'

The return value is a bytes object, so if you want a proper string, you'll need to decode it. Assuming the called process returns a UTF-8-encoded string: 
>>> result.stdout.decode('utf-8')
'total 0\n-rw-r--r-- 1 memyself staff 0 Mar 14 11:04 files\n'

This can all be compressed to a one-liner: 
>>> subprocess.run(['ls', '-l'], stdout=subprocess.PIPE).stdout.decode('utf-8')
'total 0\n-rw-r--r-- 1 memyself staff 0 Mar 14 11:04 files\n'

If you want to pass input to the process's stdin, pass a bytes object to the input keyword argument: 
>>> cmd = ['awk', 'length($0) > 5']
>>> input = 'foo\nfoofoo\n'.encode('utf-8')
>>> result = subprocess.run(cmd, stdout=subprocess.PIPE, input=input)
>>> result.stdout.decode('utf-8')
'foofoo\n'

You can capture errors by passing stderr=subprocess.PIPE (capture to result.stderr) or stderr=subprocess.STDOUT (capture to result.stdout along with regular output). When security is not a concern, you can also run more complex shell commands by passing shell=True as described in the notes below. 

This adds just a bit of complexity, compared to the old way of doing things. But I think it's worth the payoff: now you can do almost anything you need to do with the run function alone.

[ Python 常見問題 ] Find the length of a sentence with English words and Chinese characters

Source From Here 
Question 
The sentence may include non-english characters, e.g. Chinese: 
  1. 你好,hello world  
the expected value for the length is 14 (2 Chinese characters, 10 English words, 1 space and 1 comma) 

How-To 
Decoding is requred: 
>>> s = "你好,hello world" 
>>> len(s) 
18 // 2 chines words will be encoded in UTF-8 which is 3 bytes. So 2 * 2 = 4 bytes to have 14 + 4 = 18 
>>> len(s.decode('utf-8')) // With decoding, we can have correct length this time 
14 
>>> for c in s.decode('utf-8'): 
... print(c) 
... 
你 
好 
, 
h 
e 
l 
l 
o 

w 
o 
r 
l 
d


[Linux 常見問題] How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?

Source From Here 
Question 
I use Ubuntu and installed Curl on it. I want to test my Spring REST application with Curl. I wrote my POST code at Java side. However, I want to test it with Curl. I am trying to post a JSON data. 

Any ideas about the right format of the Curl command? 

How-To 
You need to set your content-type to application/json. But -d sends the Content-Type application/x-www-form-urlencoded, which is not accepted on Spring's side. Looking at the curl man page, I think you can use -H: 
  1. -H "Content-Type: application/json"  
Full example: 
# curl --header "Content-Type: application/json" \
--request POST \
--data '{"username":"xyz","password":"xyz"}' \
http://localhost:3000/api/login

From above, -H is short for --header, -d for --data. Note that -request POST is optional if you use -d, as the -d flag implies a POST request.

2018年7月10日 星期二

[ Python 常見問題 ] How to make inline plots in Jupyter Notebook larger?

Source From Here 
Question 
I have made my plots inline on my Ipython Notebook with "%matplotlib inline."! 
Now, the plot appears. However, it is very small. Is there a way to make it appear larger using either notebook settings or plot settings? 

How-To 
Yes, play with figuresize like so (before you call your subplot): 
  1. fig=plt.figure(figsize=(18, 16), dpi= 80, facecolor='w', edgecolor='k')  
The default figure size (in inches) is controlled by 
  1. matplotlib.rcParams['figure.figsize'] = [width, height]  
For example: 
  1. import matplotlib.pyplot as plt  
  2. plt.rcParams['figure.figsize'] = [10, 5]  
creates a figure with 10 (width) x 5 (height) inches

2018年7月6日 星期五

[ Python 常見問題 ] Create empty file using python

Source From Here 
Question 
touch is a Unix utility that sets the modification and access times of files to the current time of day. If the file doesn't exist, it is created with default permissions. How would you implement it as a Python function to create empty file or touch it if exist? 

How-To 
There is os.mknod("newfile.txt") (but it requires root privileges on OSX). The system call to create a file is actually open() with the O_CREAT flag. So no matter how, you'll always open the file. So the easiest way to simply create a file without truncating it in case it exists is this: 
  1. open(x, 'a').close()  
Actually you could omit the .close() since the refcounting GC of CPython will close it immediately after the open() statement finished - but it's cleaner to do it explicitely and relying on CPython-specific behaviour is not good either. In case you want touch's behaviour (i.e. update the mtime in case the file exists): 
  1. import os  
  2. def touch(path):  
  3.     with open(path, 'a'):  
  4.         os.utime(path, None)  
You could extend this to also create any directories in the path that do not exist: 
  1. basedir = os.path.dirname(path)  
  2. if not os.path.exists(basedir):  
  3.     os.makedirs(basedir)  
Supplement 
* FAQ - Implement touch using Python? 

2018年7月5日 星期四

[ Python 常見問題 ] How to resolve DNS in Python?

Source From Here 
Question 
As title. In Linux shell, we resolve domain this way: 
# dig +short www.google.com
172.217.24.4

# nslookup www.google.com
Server: DNS_IP
Address: DNS_IP#53

Non-authoritative answer:
Name: www.google.com
Address: 172.217.24.4


# host www.google.com
www.google.com has address 172.217.24.4
www.google.com has IPv6 address 2404:6800:4008:800::2004

How to get IP of domain name in Python script? 

How-To 
API:gethostbyname_ex is what you need: 
Translate a host name to IPv4 address format, extended interface. Return a triple (hostname, aliaslist, ipaddrlist) where hostname is the primary host name responding to the given ip_address, aliaslist is a (possibly empty) list of alternative host names for the same address, and ipaddrlist is a list of IPv4 addresses for the same interface on the same host (often but not always a single address).

One example as below: 
>>> socket.gethostbyname_ex('www.google.com')
('www.google.com', [], ['172.217.24.4'])

Supplement 
* Linux command to translate DomainName to IP [closed]

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...