Node.js:
Regex
var s = 'so qqr rr';
var r = /\w+/;
s.match(r) // ["so", index: 0, input: "so qqr rr"]
// 1st match
r.test(s) // True
- Flag g (global)
- Flag i (case insensitive)
var s = 'so qqr rr'; var r = /\w+/g; s.match(r) // ["so", "qqr", "rr"] // All matches since the flag 'g' in regex r
// Groups? var s = '123-4567 999-3141'; var r = /(\d{3})[-.]\d{4}/; s.match(r); // ["123-4567", "123", index: 0, input: "123-4567 999-3141"] // Global? var s = '123-4567 999-3141'; var r = /(\d{3})[-.]\d{4}/g; s.match(r); // ["123-4567", "999-3141"] // Groups & Global, pls? I mean ... 'exact'ly // Originally I'd like to put TBC here ... var s = '123-4567 999-3141 and 314.1592'; var r = /(\d{3})[-.]\d{4}/g; var res = r.exec(s); while(res) { console.log(res[1]); // Show 1st match res = r.exec(s); }
// Split str by regex-like delimeter? var s = '123-4567, 999-3141 314.1592'; var r = /[,-.\s]/; s.split(r); // ["123", "4567", "", "999", "3141", "314", "1592"] // OMG "" originates from ',' and ' ' // Again, split str by regex-like delimeter? var s = '123-4567, 999-3141 314.1592'; var r = /[,-.\s]+/; // Sequence, perhaps? s.split(r); // ["123", "4567", "999", "3141", "314", "1592"]
// Replace all of 'em? var s = '123-4567, 999-3141 314.1592'; var r = /[,-.\s]+/g; // 'g' is required here -- similar to match s.replace(r, '&'); // "123&4567&999&3141&314&1592" // What about 'doubling' 'em? var s = '123-4567, 999-3141 314.1592'; var r = /([1-9])/g; s.replace(r, '$1$1');
// Match & replace by case? // E.g. 123 -> idc // And 456 -> idk // Scan 'em 2 times and replace? Inefficient & Prone-to-Bugs // What about ... function replacer(match) { if (match === '123') { // Do sth here return 'idc'; } else if (match === '456') { // Do sth here, too return 'idk'; } // Sth went wrong return 'err'; } var s = '123-4567, 999-3141 314.1592'; var r = /(123|456)/g; s.replace(r, replacer); // 'idc-idk7, 999-3141 314.1592'
Python:
One of the decent OO principals from Think Python
Whenever you override a method, the interface of the new method should be the same as the old. It should take the same parameters, return the same type, and obey the same preconditions and postconditions.
If you obey this rule, you will find that any function designed to work with an instance of a superclass, like a Deck, will also work with instances of subclasses like a Hand or PokerHand. If you violate this rule, your code will collapse like (sorry) a house of cards.
Single inheritance vs Multiple inheritance vs MRO vs C3 algorithm vs GFEF algorithm
- Check this post for further information
- Takeaways – inherit the more specific one
Composition VS Inheritance
- Check this post for further information
Dock-typing
Sequences
- List Vs Tuple
Module & Module Search Path
List builtins by
dir(__builtin__)
Basic usage of str.format
# By arg's index print '{1} and {0}'.format('reversed', 'msg') # msg reversed # By kwargs print 'I dont know about {sb}. But I believe {lang} is hard to learn'.format(sb='you', lang='eng') # I dont know about you. But I believe eng is hard to learn' # Add rounding print 'We approximate {val} by 3 decimals as {val:.3f}.'.format(val=0.123456)
- And more of it
f.readlines VS for line in f
pickle & persistent object
# Pickle myobj = {'a': [1, 2, 3], 'b': 12.34, 'c': {'e': 123}} with open('soqqr.pkl', 'w') as f: pickle.dump(myobj, f) # Unpickle with open('soqqr.pkl', 'r') as f: anoth_obj = pickle.load(f)
Profiling
# Easy profiling on yours.py; easy life YOUR_PARAM1=1 YOUR_PARAM2=2 python -m cProfile yours.py args
Quality Control
- U got spaghetti today?
- doctest
- unittest
Batteries Included
Way better logging
GC & reference counting & cyclic garbage collector
EAFP (Easier to ask forgiveness than permission)
Flat VS Nested (try-except)
Array VS List
- few bytes/int VS 16 bytes/int when containing only int
Magics in ipython
- cd
- timeit
- load
Exceptions hierachy
Coding styles for Python
Time complexity table
Conventions
- Underscore prefix to symbolize private vars
React:
Revisits
- I appreciate this design wisdom and quote from the page – ‘When you want to aggregate data from multiple children or to have two child components communicate with each other, move the state upwards so that it lives in the parent component. The parent can then pass the state back down to the children via props, so that the child components are always in sync with each other and with the parent.’
- Conventionally, use on__ and __handle for attributes and handling methods respectively
- Assign proper keys whenever you build dynamic lists – advice from React’s tutorial
- Immutability VS Time Traversal
- Controlled Components
- props
- state
- setState
Vim:
Jump in python blocks?
Highlight regx search
# On & Off
:set hlsearch
:nohlsearch
# toggle by :Hi, get this vv to .vimrc
command Hi set nohlsearch!
# Reload .vimrc when editing
:so %
# Find a 'four-word' word
/\<\w\{4}\>
# Match the brackets and contents
# [Good temporary end](http://ab.cd.io) [你好r]
/\[.\{-}]
# Turn markdown urls to html tags
# e.g. [abc](https://efg.io) to <a href="https://efg.io">abc</a>
:%s/\[\(.\{-}\)](\(https\=:.\{-}\))/<a href=\"\2\">\1<\/a>/g
- Literal char
- Meta char & Quantifier & Position & Char classes & Alternation
Nice referrals for Regex in Vim
- Groups
- Greedy or Not
- Back reference
Murmur:
React Native
My first time to realize that there’s ‘Code of Conduct’ in the open source community OMG!!!
Good article for introducing the very basics of codecs – I’m really so in love with this article: ironical story-telling approach, chronological paragraphs, humorous lines. And I’m stunned for the reason that the author is the co-funder of Trello and the CEO of SO – wt …
And yes, I quote, “Please do not write another line of code until you finish reading this article.”
- especially important when you’re so confused with ASCII, Unicode, Utf-8 etc
- Unicode Byte Order Mark
Best way to catch Regular Exp?
- Good start
- Good temporary end