Skip to content

Travis Shen

Hoisting

variable hoisting

function hoisting

How hoisting work?

WRONG the variable or function are moving to the top

WRONG the variable or function are declared first and assign value later

CORRECT execution context was created in two phases:

  1. the create phase: create global object and outer environments, as the parser runs through, it set up the variables and the functions in memory(recognize where they placed, existed in memory), it’s available to access them, BUT variable is not like functions which are placed entirely in memory,  context knows the value only when it end up executing the code, when it access that variable, it got a placeholder undefined.
  2. the execution phase

The Global Environment and Global Object

Global EXECUTION CONTEXT create two things by JavaScript engine:

1. global object, in the browser named “window”

2. variable called “this”, in global execution context, this == window

JavaScript Conceptual Aside

Syntax Parser

A PROGRAM read your code and determines what it does and if it grammar/syntax is valid

Lexical Enviroment

WHERE something sits physically in the code you write

Execution Context

A wrapper to help manage the code that is RUNNING

Name / Value Pair

A NAME which maps to a unique value, the name could defined multiply, but only can have one value in any given CONTEXT; A value can be more multiple name/value pairs

Object

A collection of many name/value pairs

Global

Not inside a function, sitting inside global object

undefined

A special VALUE in JavaScript, means that the variable hasn’t been set, but variable has been set in memory

Single Threaded

One command is executed at a time. JavaScript behaves in a single thread manner

Sync Execution

One line of code executed at a time, in order. JavaScript is sync execution in its behavior

Invocation

Running a function, in JavaScript by using ()

Variable Environment

WHERE variables live and how they relate to each other in memory. Every execution context has its own variable environment

Scope

WHERE a variable is available in your code

Async

More than one at a time

Dynamic typing

DON’T tell JavaScript engine what type of data a variable holds, it figures it out while code is running

<==> static typing

Primitive type

a data type, not an object

Operator

A special FUNCTION that is syntactically(written) differently, generally operator take two parameters and return one result.

prefix notation: +3 4

infix notation: 3 + 4

postfix notation: 3 4+

Operator precedence

Functions are called in order of precedence

Operator associativity ref

What order when functions have same precedence

Coercion

Convert a value from one type to another

Name Space

A container to keep variables and functions with the same name separate

JavaScript Object Notation(json)

inspired from JavaScript object literial

First Class Fuction

Everything you can do with other types you can do with functions, assign them to variables, pass then around create them on the fly

Expression

A unit of code that results in a value and doesn’t have to save to a variable

Statement

A unit of code doesn’t results in a value (e.g. if statement, function statement)

Mutate

to change something; mutable, means one thing can be changed

Argument

the parameters passed to a function

White Space

invisible characters that create literal ‘space’ in your code

Callback Function

A function you give to another function, to be run when the other function is finished

Function Currying

Create a copy of a function but with some preset parameters, very useful in mathematical

Inheritance

classical vs prototypal

To put it another way, “OO” means “object oriented” (which really means class oriented), so to contrast that, I came up with “OLOO”, which means “objects linked to other objects”. At the end of the day, no matter what syntax hoops you jump through in JS to try to convince yourself you’re doing classes, what you end up with is just a bunch of objects that are each linked to another object: behavior delegation.

Reflection

An object can look at itself,  listing and changing its properties and methods

Function Constructor

A normal function that is used to construct objects via “new” operator

Polyfill

Code that adds a feature which the engine may lack

Syntactic Sugar

A different way to type something that doesn’t change how it work under the hood

Method Chaining

Calling one method after another, and each method affects the parent object

Transpile Language

Convert the syntax of one programming language to another.

E.g. TypeScript, Traceur

 

Multiprocessing: Pickle Issue

multiprocessing use pickle module to serialize things among the process, but doesn’t support functions with closures, lambdas, or functions in __main__

Here is the example I try:

solution: dill

dill and multiprocessing: pathos

dill: a utility to serialize all of python
– pox: utilities for filesystem exploration and automated builds
– klepto: persistent caching to memory, disk, or database
– multiprocess: better multiprocessing and multithreading in python
– ppft: distributed and parallel python
– pyina: MPI parallel map and cluster scheduling
– pathos: graph management and execution in heterogenous computing

 

Serialize and Deserialze Sqlalchemy Nested Objects with JSON

At first we build a relationship :

and we want to dump a county instance to json like this:

extending JSONEncoder:

if you want to decode your json file, simply supply a function for object_hood attribute:

object_hook is an optional function that will be called with the result of any object literal decoded (a dict). The return value of object_hook will be used instead of the dict. This feature can be used to implement custom decoders (e.g. JSON-RPC class hinting).

when a json file loads, we can print the parameter “dict” which pass in to object_hood function, the whole process will loads from the most inner object (a dict) to the outer objects look like this:

榮格:潛意識的研究

  • Quote

不一定要用夢作為自由聯想的起點,才能發現病人的情節。這說明誰都可以從周遭的一點直接進入核心。你可以從古斯拉夫字母開始,也可以從沉思水晶球、祈禱輪或現代畫開始。

夢有特殊意義,經常由於情緒波動和內容的習慣性情結所引起。通常,夢有個明確、目的明顯的結構,表示一個基本的觀念或意圖。

榮格理論與佛洛伊德理論的關鍵差異是:榮格認為人類心理由好幾種情結構成,而且許多情結彼此形成二元對立。佛洛伊德認為戀母情結(伊底帕斯情結)是普遍共通的,也就是所有兒童都面對伊底帕斯情結帶來的發展挑戰。而伊底帕斯情結也是絕大部分心理疾病的中心。隨著榮格與佛洛伊德決裂,兩方代表的理論也分歧了。

個人情結有其個別歷史,原型特質的社會情結也有其個別歷史,當個人情結只產生個人偏見時,原型卻製造全民族以特色的神話、宗教和哲學。我們視個人情結為個人意識的補償,同樣宗教神話可以視為人類集體的精神治療。

榮格主張,不要把夢境拿來自由聯想,專注在夢本身(實際形式及內容),排除所有可能引起的聯想。舉例來說,一個人也許會夢到插鑰匙在鎖孔裡,揮動一根粗重的棍子,或用一根撞槌打破一扇門。每個動作都可以視作性的比喻。但事實上,他的潛意識為了本身目的而選擇這些特別意象的其中一種──鑰匙、棍子或撞槌──也含有重大意義。真正的任務是去了解他為什麼夢到鑰匙而不夢到棍子、夢到棍子而不夢到撞槌。有時我們甚至會發現這些意象根本與性行為無關。

許多人錯誤地高估意志的重要性,而且認為如果沒有決定或意圖,他們的心靈就空空如也。但我們必須知道如何區別有企圖和無企圖的心靈內容。前者是源自於自我性格,後者卻是興起自一個與自我不統一的根源。

我們用以處理清醒生活的觀念,並非如我們所認為的那麼正確,也沒有那麼容易了解。相反的,它們的意義會因我們愈深入檢討而愈不正確,原因是我們所聽聞或經驗的任何事都能變成潛在的。

意識愈受到偏見、錯誤、幻想和幼稚的欲望所影響,早已存在的鴻溝也就愈寬闊,成為精神分裂,而且令生活矯揉造作,與正常的本能、性格相去甚遠。

為了精神穩定和生理健康,潛意識和意識必須完整連結,且要齊頭並進。如果這兩者分裂,心理馬上就會產生毛病。

任何夢象徵都不能與個體所夢到的象徵分開,而且沒有那種解釋,可以把夢的意義說得十全十美。夢境是十分個人化的,而且它們的象徵也不能被分類、歸納。

盡你所能學習象徵,然後在分析夢時把象徵全部忘掉。

我的直覺是由突然而出乎意料的洞察力組成,使我認清我的夢意指我自己、我的生活和我的世界,以及我的整個實體,都在和另一個具有理想和追求自己目標的奇異心靈所建立的理論架構對抗。

治療僅限於一個簡單的問題:在分析者和被分析者之間,誰會支配誰?為了這個原因,我放棄了催眠治療,因為我不願意用自己的意志壓迫別人。我希望治療的過程完全發自病人自己的人格,不再為我的提示所影響。我的目的在於保護維持病人的自尊和自由。

個體是唯一的實體,我們愈是輕視個體,一味朝著人類抽象觀念走去,那我們會越走越錯,跌進迷陣。

記號往往比呈現的概念意義少;象徵往往代表某些比表面和直接意義更多的東西。

我很懷疑夢是睡眠的保護人這個理論,因為夢通常會打擾睡眠。夢採取的形式當然是潛意識的,在潛在意識的狀況裡,它們失去明確的定義,因此夢不能產生確定的思想。

為什麼夢境意指一些與內容似乎不相關的東西?原因是夢的內容是象徵的,包含不止一個意義。

宗教是有集體性質和起源的象徵。信教者假定這些意象是神性的起源,懷疑者則說它們是被捏造出來的。然而這兩者都有錯。事實上,它們是集體表象,從初期的夢和有創造力的幻想放射出來,而且這些意象是無意識、自然而然的表明,不是杜撰出來的。

感覺(感官認知)告訴你某物存在;思考告訴你那是什麼東西;感情(理性的)告訴你那東西是否宜人;直覺(非理性)告訴你它從哪裡來和它的動向。

我們一般說本能是生理上的衝動,而且被感官認知,但同時,它們也在幻想中表現自己,往往只以象徵的意象顯現,這些表現就是原型。它們沒有已知的起源,而且隨時隨地複製自己。

就像本能一樣,人類心靈的集體思想模式是天生和遺傳的。關於這類思想模式的感情表現,是全世界一樣的。

在突然改變性情的個案中,我們可以證實原型在潛意識中曾工作過一段時間,巧妙地安排會導致危機的環境。

其實,潛意識看來可以像意識一樣調查事實,並且從中得到結論,潛意識甚至利用一些事實,預期它們可能的結果,因為我們沒意識到這些結果。

因此原型有其本身的本能和特殊能力。它們的作用就像情結,隨自己喜歡任意來去,妨礙或修正我們有意識的企圖。

康德,浮士德,「一開始就是實行。」

現代人可以免除每天向神求助、可以實行自己的目標,也可以順利地把自己的觀念帶進行動中(有志者事竟成)。然而未開化之人似乎還得被恐懼、迷信所阻礙(不知道為什麼要做這個儀式)。其實現代人的神和魔鬼根本還沒消失,只是換了新的名字罷了。

如果我們視人類是一個個體,那我們了解人類就像一個被潛意識力量迷住的人,而我們又喜歡把某些問題藏在分開的抽屜裡。但這就是我們該多考慮我們在做什麼的原因,因為人類現在受到逐漸超乎我們控制的自創和嚴重的危險所威脅。換句話說,我們的世界像神經病患一樣的分裂。就像西方人逐漸警覺到東方侵入的權力意志,了解自己必須採取額外的防禦措施,而同時,又以他本人的道德和好意自豪。
他沒看到的是自己不道德的一面。因為他藉著良好的國際習例掩飾,但共產國家令他當場出醜。
這種事態說明西方社會這麼多人無助的感受。他們開始了解面對他們的困難是道德問題,試圖以武器和經濟政策來解決這個問題,顯然於事無補,兩不討好。現在我們許多人都知道道德和精神的方法比較有效,因為這兩者能提供我們免疫性,以對抗經常增加的傳染病。但所有這種企圖都證明是無效的。只有我們看見自己的陰暗面,才可以對任何道德和精神的傳染病和暗示免疫。
值得注意的是,共產世界有個大神話,它是金色年代、天堂的原型夢。在這裡,每件東西都很足夠,可以大量供給每個人,而且有個偉大、正直且聰明的酋長統治這個人類幼稚園。這有力的原型在其初期的形式就抓緊所有人,我們甚至以自己的幼稚支持它,因為我們西方文化也是在同樣的神話裡。我們潛意識地珍愛同樣的偏見、希望和期待。我們太過相信社會福利國家、世界和平、人類平等、不朽的人權、公正、真理,以及地球上的神國。
令人難過的事實是,人類的實際生活包含一種無情對立的情結——白天與黑夜、生與死、幸福和災難、善與惡。我們甚至不能確定那一樣會壓倒另一樣,即善征服惡,或快樂打敗痛苦。生活是個戰場,它永遠是這樣,不然生存會結束。
一點不錯,這種人類的內心衝突早已令早期的基督徒渴望末世來臨,或是佛教徒排斥所有塵世和雜念。如果他們沒有與特別的精神和道德觀念合作,盡量修正他們否定世界的偏激看法,那很明顯,他們早就自我毀滅了。
我之所以強調這點,完全因為在現今社會裡,有幾百萬人對宗教失去信心,這種人不再了解他們的宗教信仰。當生活順暢如意時沒有宗教信仰,損失是絕對看不出來的,但當逆境或痛苦來臨時,情形又不一樣了,那時,大家就開始尋求方法,反省人生的意義,找尋痛苦經驗的真諦。
有件事情很有意思——去看心理醫生的猶太人和基督徒人數,比天主教徒多,不過這是可以預期的,因為天主教禮拜堂仍然有責任治癒人類的靈魂。人若是只斷然相信有意義的生活方式、神或不朽,一談到死亡就會覺得太刺激。自太古以來,人類就有種「超人」和來世樂土的觀念。只有今天,我們才認為我們可以在沒有這種觀念下過活,有人假設這種觀念是假的。我寧可說它們確實是真的,因為這種概念從史前時代就伴隨人類生活了,而且在任何刺激之下,仍舊會從潛意識突破進意識裡。

宗教象徵的任務是給予人類生活一個意義。

人類早已看出神話的內容似乎不可信,因此他們想使神話成為一種普遍可了解的形式。

因為科學的知識過於成熟,我們的世界變得失去人性,人類感到自身在宇宙間孤立,因為他與自然無涉,而且失去與自然現象的潛意識認同。

我知道佛教徒會說:如果人類遵從八正道的信條和對自我有真切的洞察力,則萬物無一不得宜。基督教告訴我們:只要人類信仰上帝,我們就會有個更完美的世界。而理性主義者堅持:如果人類有才智和理性,我們所有問題都會得到解決。問題是他們沒有一個設法解決自己的問題。

那些有理性的知識份子還不曉得意識並非他全部的心靈。