Keep learning forward
To be updated ...#TIL : View real-time logs using websocketd
I found a handy tool for making a websocket right on your shell.
It is websocketd : http://websocketd.com/Its phisolophy is so simple :
Just read incoming text from stdin and write outgoing text to stdout. Messaging is simple.
Read its docs, I follow the ez tutorial
10 second tutorial
and see how cool does it work.
Let make something cool with this, we got a UNIX tool that print out tostdout
in real-time with changes of end file.Ok cool, change the command to
websocketd --port=8080 tail -f logs/web_access.log
Tada ! Open the webpage that listens the websocket you made and see the magic :D You can tracking access log of website or system in real-time way.
#TIL : Run built-in server via Docker
Docker is the fast and clean way to run Linux programs.
We can run a PHP project via PHP built-in server and Docker.
docker run -it -p 8080:8080 -v `pwd`:/code php:7 php -S 0.0.0.0:8080 -t /code/web /code/web/server.php
With
server.php
content is<?php $filename = __DIR__.preg_replace('#(\?.*)$#', '', $_SERVER['REQUEST_URI']); if (php_sapi_name() === 'cli-server' && is_file($filename)) { return false; } // Run application below $app = new Application(); $app->run();
#TIL : Reference assign object variable
When you have a object
x
and assigny = x
,y
will be a ref ofx
(it looks like pointer of C). So changing property ofy
means changing property ofx
.Ex :
x = {"a": 1, "b": 2} y = x y['a'] = 100 print x['a'] # Result is 100
So if you want clone the value, use
copy
lib :import copy x = {"a": 1, "b": 2} y = copy.deepcopy(x) y['a'] = 100 print x['a'] # Result is 1
#TIL : FTP via curl tool
Can upload an file via FTP by curl tool with handy script :
curl -T file_need_to_upload ftp://hostname --user user:passwd
#TIL : Eloquent Many-to-Many Relationship
To create the n-to-m relationship in Eloquent, we create a table stand between 2 tables.
Eg:We have the db schema :
post post_tag tag id (PK) id (PK) id (PK) title post_id (Index) name (Unique) content tag_id (Index) In the model
Post
andTag
, we define a relation :- App\Model\Post.php
public function tags() { return $this->belongsToMany('App\Model\Tag', 'post_tag', 'post_id', 'tag_id'); }
- App\Model\Tag.php
public function posts() { return $this->belongsToMany('App\Model\Post', 'post_tag', 'tag_id', 'post_id'); }
After that, we can use the relation to fetch the related data, like
$post = Post::firstOrFail(1); $tags = $post->tags; // return the Collections class contains tags
or
$tag = Tag::where('name', '=', 'php')->first(); $posts_of_tag = $tag->posts; // return the Collections class contains posts
When updating or creating post, we have to sync with tags by
$tag_ids = [2, 3 ,4]; // Get tag ids from tag names $post->tags()->sync($tag_ids);
#TIL : String Format Unicode params
unicode_thing = u"Xin chào mọi người" a = '{}'.format(unicode_thing)
will cause the error
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe0' in position 6: ordinal not in range(128)
The solution is add
u
prefix the pattern (it means using unicode pattern) :unicode_thing = u"Xin chào mọi người" a = u'{}'.format(unicode_thing)
#TIL : Index is useless when use function on indexed field
Reality, if using function on indexed field, you will broke indexing by accident.
Eg:WHERE MONTH(`date`) = 11 AND YEAR(`date`) = 2015
Solution is transform the query to comparison query, like this :
WHERE `date` >= '2015-11-01' AND `date` < '2015-12-01'
#TIL : Shortcut keyboard improve productivity
- Open the preference of any Application by
Cmd + ,
. - Press
Cmd + ~
to go previous App when switching App onCmd + Tab
- Copy screenshot to clipboard by
Cmd + Ctl + Shift + 3
- Press
Option
when click to notification center is putting it on “Do no disturb” mode.
- Open the preference of any Application by
#TIL : Debugging Chrome extension
- To open directly the Console Dev Tools, press
Cmd + Opt + J
in MacOSX. - To debug easily the any files of extension, open the url
chrome-extension://<extension-id>/<file-name>
in address bar.
- To open directly the Console Dev Tools, press