“Why did I not think of this?” moment in MODX 3 snippet development with PHPStorm

This is one of those ‘Aha!’ moments.

MODX is one of CMSs (content management system) that I have been using for a long time to maintain my shogi site. Market penetration of this CMS is said to be 0.1%. As of this writing, 43% of of Web sites uses WordPress and no other CMS reach even 10% of market share. (source, w3techs.com . I actually think MODX penetration is much greater than this number, MODX does not leave big footprint to show it’s identity in front of web crawlers. -Many sites I know powered by MODX, including mine – were identified as non-CMS site by w3tech’s tool.— Nevertheless, there is no doubt WordPress dominates the CMS market)

Although both uses same language (PHP) for powering the site, MODX gives much more flexibility in site design but you need to be proficient with HTML and CSS (and JavaScript). In another word, MODX is a tool while WordPress is a product.

What I liked about MODX over WordPress at the time was a separation of PHP code and HTML. This was a long time ago so the situation might have changed on WordPress, but I have been happy with MODX since.

At the time, I also looked at Drupal and Joomra . All of them forces you to follow their interpretation of what website should be. MODX has no opinion on how you want to create Web site. You have a total freedom.

On the other hand, if the only thing you want to do with your site is blogging, there is no better solution than using WordPress.

I digressed. Back to the subject!

With a wealth of plug-ins available, your rarely need to code in PHP. When you do though, MODX separates a PHP coding part from HTML with a thing called Snippets. A snippet is then inserted in to HTML with a use of tag.

[[aSnippet]]

You can edit a snippet within MODX backend’s editor. With a plugin like ‘ACE’ there is a nominal amount of syntax checking to the PHP code. Unfortunately, I am being spoiled with convenience of IDE such as PHPStorm,

MODX backend, Snippet editor with ACE plugin

So there is a desire to be able to write a snippet code using PHPStorm and get benefits of auto-completion and methods hinting, strict error checking and warning.

Unfortunately, there is no plugin in PHPStorm for MODX or MODX plugn for PHPStorm.

When I google “MODX with PHPStorm”, there are several hits, Although none of them gave me a direct answer on what I wanted to do, I was able to put enough facts together and came up with a solution. The solution that was just waiting for me to assemble few known facts, that I have used all of them separately in the past!


1. Fact that PHPstorm can create a project using existing remote site contents, so that PHPStorm can pull all file setup from MODX site.

PHPStorm->File->New Project from Existing Files


2. Fact that PHPstorm can then start syncing file contents of my local project files to server files.

File sync options available in PHPStorm

Ok, but Snippet is stored in Database. the code is not in the file, but then,

3. Fact, MODX snippet code can be from a static file, meaning I can create a PHP file and feed it as a snippet to MODX by turning on “is Static” option and point to the file location

Is Static option is turned on for Snippet. Snippet is now fed from “static file”



Result: When I combine those 3 factors, I can create a snippet with a comfort of PHPStorm IDE environment.

Snippet now editable in PHPStorm
Snippet output on browser page, powered through MODX

What do I gain? Full context support and code hinting, as PHPStorm analyzes whole MODX site and figures out all variables and object and its methods being used inside MODX. I have to give credit to MODX development team for fully documenting source files. Thank you!

PHPstorm explains what getOption method is
PHPStorm autocomplete support in action.

You can also open the corresponding file by pointing to method and do control-b to drill down on function.

I edit a file in PHPStorm, and you either 1) turn on auto-sync between your local files and server files or 2) manually upload changed file to server. Then I can run and test the snippet with MODX backend immediately.

So all of the sudden, I feel like invincible. It will be very hard for me to make coding error. (Ok, logic error still possible)

Once Snippet development is done, you may switch off the ‘is static’ option and keep the snippet inside database, remove the php file from the system.

One thing that is kind of annoying is that by default, PHPstorm thinks $modx variable is undefined. The default setting on PHPStorm’s inspections on undefined variable is somewhat on conservative side. It will not even acknowledge variables from included file!. To mitigate this, you need to put a check on “Search for variable’s definition outside the current file”

PHP settings->Editor->Inspection->PHP->Undefined symbols->Undefined variable

It hurts to think that I have been using both PHPstorm and MODX for more than 10 years and I am figuring this out just now 🙁

Creating a Blog page inside MODX version 3 web site

MODX comes clean slate so there is no ‘ready to use’ provisions for creating Blog style site. One has to set it up using tools available in the CMS.

I took an easy way out for my production site by creating blog site using free wordPress service and then feed content to MODX page. with this, the only extra you will need is Spiefeed to handle rss feed from WordPress.  But for purist approach it is certainly possible to setup blog section within MODX using few extras.

There are several articles available for creating Blog sites using MODX.  The one shown in the modx official site is somewhat dated, but the article actually have a link to the instruction/article of more recent implementation of MODX blog site.  Link is here.   Better yet, this instruction is accompanied with a package published in the github which has all chunks, a snippet and a template packaged up to duplicate the blog site that the article discusses.  A demo site is also available to check out the feel of using this template. The document states that the demo utilizes MODX 2.7.  So can I use this setup with MODX 3 site? Furthermore, the instruction assumes you want the Site to be a blogging site. Can I add ‘Blog page’ to already existing MODX site? Answer to both questions are ‘Yes’ but we need to modify some parts.

To start, I could have created each chunk and template manually following the instruction but to reduce my typing effort, I went ahead and downloaded the demo package. I also installed dependent extras (getResources, getPage, Collections, and Tagger).  Previously, I did not have any of those installed. – I am using pdoTools  to get resources and get page. – <edit> It was fairly easy to change references to getResources and getPage to equivalent pdo* snippets.

Again, the setup instruction is written for MODX version 2.7 and the site I am setting up this blog structure is version 3.0.3.  Setup also assumes the top page is a site home page. There are several area in chunks and template that refers to [[++site_start]] and this need to be changed to point to correct Resource.  I want to have a blog page as a subpage of the site.

Looking through the template and snippets that I setup with the downloaded package , I found some more modifications were needed.

I will list out those changes.

  1. My server uses Nginx and it needs additional html tag to specify base url in <head> section of blog template html. Without it, Friendly URL won’t work correctly.
    <base href="[[++site_url]]">
  2. In MODX 3, [[*class_key]] does not return “modResource”.  It returns “MODX\Revolution\modResource”.
    This means I get “MODX/Revolution\modDocument” or “MODX\Revolution\CollectionContainer” in [[*class_key]] field depending on resource selected. Two sections of “Blog Template” stops working due to this.

    1. Template tries to pull in “jumbotron_CollectionContainer” or “jumbotron chunk based on [[$jumbotron-[[]*class_key]]] tag. This tag doesn’t work in MODX 3 as it returns [[$jumbotron-MODX/Revolution/….]] which does not exist and forward slash within chunk name is probably invalid anyway.
    2. Template tries to insert [[$blog_listing]] or [[$blog_article]] based on conditional modifier [[*id:is`[[++site_start]]`:then….]] but the blog_listing page is not at [[+site_start]] resources in my use case.
      1. To mitigate both problems, I have created a system setting [[++blog_start]] in custom namespace/area and set it to id of a blog home page resource I created. Then rewrote tags as follows.
        For jumbotron section
        [[[[*id:is=`[[++blog_start]]`:then=`$jumbotron-CollectionContainer`:else`$jumbotron-modDocument`]]]]
        For container section
        [[[[*id:is=`[[++blog_start]]`:then=`$blog_listing`:else=`$blog_article`]]]]
      2. Note: It will be probably not that difficult to create snippets and replace conditional tags here.
  3. getResources and getSnippet tags are used to aggregate child resources and parents properties are set to site home page [[++site_start]].   These needed to be changed to aforementioned [[++blog_start]].  These changes had to be made on $blog_listing chunk and $jumbotron-CollectionContainer chunk.
  4. @groups properties are hardcoded in calls to TaggerGetTags snippet.  The snippet is called from $aside chunk is setting @groups to 1 and the snippet called from $headder chunk is setting @groups to 2.  Make sure this agrees with your tagger group settings. I have first created ‘Category’ group and then ‘Archives’ group so I needed to change them to 2 and 1 respectively.
  5. Last and least 🙂   as this is just a demo template, links to “subscribe”, “search”, “Sign up”, “GitHub”, “Twitter” and “Facebook” are all mocked and pointing to itself.  You either need to remove or put actual link to them when modifying to suit your site design.

So, in order to create a Blog section within Modx 3 hosted website, using this instruction and elements in the github repo, One needs to be aware of:

  1. Modx version 3’s breaking change of the model class caused decision logics inside Blog_Template to stop working.  Rework of the logic flow was needed.
  2. Friendly url.  Nginx needs base href reference in head section.
  3. Creating a blog section other than home page location. Needed to change references to [[++site_start]] system settings to [[++blog_start]], a user created system setting
  4. Adjust the template design to fit with other area of site design.

Some more miscellaneous items to note.

  1. $blog_listing_item chunk, content placeholder has a modifier :firstp. There is a firstp snippet that is installed with the package.  I opted to replace it with ellipsis=`500` modifier to limit the content output to first 500 chars followed by ellipsis to suit my preference.
  2. Also in $blog_listing, getPage snippet uses @blog properties set. This set does not exists so the default property set will be used instead. I think idea is to create a blog property set within getPage snippet to override the default property settings in easy way, if customization is needed.

Commenting on the posts are not possible.  This need to be setup by yourself and will require Quip extra. that part is not covered by this article. One has to go back to modx documentation’s tutorial section.

 

factory function?

JavaScriptやPHPで、Function Factoryずいう蚀葉を芋るのだが、これは共通するパタヌンのFunctionを生成するためのFunctionずいうこずで、䟋えば


$double = function($x){
    return $x*2;
};

$triple = function($x){
    return $x*3;
};

$quadruple = function($x){
     return $x*4;
};

なんおのは、䞉぀のfunctionを定矩しおいおも、よく芋るず、ずかける数字が違うだけで、あずはたったく同じパタヌン。 このようなfunctionの蚘述を効率よく行うために

ずいうような曞き方をするテクニック。肝心な郚分の蚘述は䞀回で枈むためコヌドの管理も簡単になる。

䞊のコヌドは返すコヌドのなかで、$xを認識させるため、 use ( $x )ずいうクロヌズをいれおいるが、PHP7.4 からPHPでもアロヌファンクションが䜿えるようになり、これだずuse ずいうkeyword無しで、倖偎で蚭定されおいる倉数を認識するようになる。ので、

ず、ファクトリヌの郚分の蚘述がすっきりする。

さらにこの䞊䜍のファンクションもアロヌファンクションで曞いおしたうず

ずなり、 プロのコヌディングでアロヌが二぀も䞉぀もかさなるような蚘述が時々でおくるのだが、そろそろ読解がしんどくなっおくるので、埌で読み返しおわかる蚘述方法ずしおは、よし悪しかなあ。

Week number confusion

Just encountered this problem when I was talking with my colleague.  He thought this week was week 43 but it actually was 42 on my calendar.  Turned out, there are two ways to look at week number.

First one is to simply take January first as a start of week one.  In case of 2021, January 1 is Friday.   depending of what you think as a first day of week, this year’s week 1 is only 2 days long or 3 days long, then Week 2 will start on either January 3rd(Sunday) or January 4th (Monday)

Excel function =WeekNum()  uses this approach.

Then there is a week number definition by ISO 8601.  According to ISO,  Week 1 is the first week of the year that has more than 4 days in a week.  Further more, first day of week will start on Monday.

With this definition, January first of 2021 does not qualify as a  week number 1.  A week starts on January 4th(Monday) is week number 1.

Excel also has function to calculate this as   =ISOWeekNum()

When I first turn on week number display on Outlook,  it showed the week number based on ‘January 1 as week 1’ approach.   This is not correct on many occasion. It appears majority of business now uses ISO week number.   You can change the settings in Outlook calendar to correct this.

In Outlook, go to File then select ‘Option’ then select ‘Calendar’  In Worktime  section, change ‘First day of week’ to ‘Monday’ then First Week of Year from ‘Starts on Jan 1’ to ‘First 4 day week’

Trying to code ISO weeknumber in typescript turned out to be a fun project but that is another story..

Layoffされおいお思ったこず

実は去幎の4月䞭旬から7月䞭旬たでレむオフされおいた。䌚瀟によっおはFurloughずいったりするが、芁は䞀時解雇。 絊料は出ない。保険も䞀郚カバヌされなくなる。その代わり、ず蚀っちゃなんだが 倱業保険をもらえる’暩利’が生ずる。 珟圚の米囜の倱業保険は週に360ドルだが、 Covid-1が猛嚁を振るい始めた段階で、4月から7月にかけおは䞀時的に週に600ドルの特別手圓が぀くようになった。 なので、週にもらえる手圓が960ドルだ。月に換算するず4,114ドル、日本円に換算するず玄43䞇円。自分の堎合はすでに子䟛は自立し、家の支払いも終わっおいるから、これだけ支絊されれば生掻にはこたらなかったが、 色々ず考えるこずがあった。

退職の時期。 自分は日本にいたらすでに退職の幎霢に達しおいる。アメリカの堎合は退職のタむミングは自分で決めるので、幎金を受け取らないこずによるペナルティが発生する71歳ぎりぎりたで働くこずは可胜なのだ。 ずは蚀っおもそこたで働く぀もりはあたりなく、 今開発䞭の補品が生産に移った時点で蟞めようず考えおいたのだが、 今回仕事をせずに過ごしおみお、少し考えが倉わった。 色々習い事をしたり、趣味に時間が取れるのはうれしいのだが、なんずもいえないすかすか感が挂う。 ただ匕退するには早いかなあ、ず思っおしたったのは事実。

2自分の働く䌚瀟は䞀時キャッシュフロヌが党くのになっおしたったので、埓業員の数を25くらいにたで萜ずした。 2か月ほどで50パヌセントたで戻したが自分はその勘定に入っおいない。職皮を考えるず圓たり前なのだが、絊料がもらえないずいうこずより、自分が必芁ずされおいないずいう疎倖感のほうが粟神的にき぀かった。

ずいうわけで退職埌の生掻を本圓にどうすれば気持ちの健康を保おるのか、ずいうこずを考える良い機䌚になった。

 

ありがたいこずに米囜人の䞊叞が自分の幎季はあず䞀幎くらいしかないずいうこずを知りながら、呌び戻しおくれた。

最終的に呌び戻されたのは党埓業員の2/3で、 残りは解雇。 それから3か月たったが仕事の流れに支障がないずいえばり゜になる。自動車業界ずいうのは恐ろしいずころで、2か月間生産がストップしたため垂堎圚庫が底を぀き、これを埋める圢で生産の量はほが通垞に戻っおしたったどころか、あちこちで郚品の欠品が発生し、今や生産を維持するために毎日が火消し状態。単玔に考えおも仕事の量が50パヌセント増えたのに、これだ。 えらいこずである、

ず 曞いたのが1幎前、 そのころは2021幎の倏も過ぎればこの状況収たっおいるのだろうず思っおいたのだがこれがずんでもない誀算。

たずコロナ隒ぎがおさたっおいない。アメリカにしおもワクチンの接皮率が頭打ちになっおしたった。ワクチンに䞍安を持った局を共和党が煜った結果だが、アメリカずいう囜民は科孊や合理性を信じおいない茩が半分ちかくいる、これが簡単に隙される、ずいうこずを改めお痛感した。 FaceBook/Twitter/youtube などにあふれおいるFake Newsにいずも簡単に乗せられおいる。これらのSNSは䌌たような蚘事をお勧めに茉せるようにできおいるので、嘘でもどんどん信じるようにできおいるようだ。

それはずもかく。

郚品欠品の状況が䞀向に収たらない。これはアゞアの生産拠点などが順繰りにコロナで閉鎖されたりするこずによっおSupply Chainが滞っおしたっおいるこずが原因だが、それにより、各自動車メヌカヌが枛産を䜙技なくされおいる。

自動車は䜕千点の郚品で構成されおいるので䞀぀でも欠品するず自動車ずしお完成しないのだ。 困ったこずに、党䜓的に枛産しおいるので䜙っおいる郚品は圚庫がふくらみ、業者にずっおは螏んだり蹎ったりである。

 

Hyperbolic function on Casio fx-991EX

There is no dedicated HYP key to call up cosh() or sinh() function in Casio fx-991EX.   This is a stern contrast to Casio fx-115ES keyboard.  It does not mean you cant use the hyperbolic funtction on fx-991EX

Hit “OPTNOption” key then select “Hyperbolic Func”

You need to be in “Calculate” mode to see this option, so for repeatability,

Hit menu, select 1.   Hit OPTNOption, select 1.

This calculator is obviously not RPN.  The calculator uses “natural notation” format.  It basically let you create equation and then evaluate the answer.  No concern on intermediate calculation.  Just answers, and it is really a wonder that you can get those devices for less than $20.

DM42

自分が䜿っおいる蚈算機はいただにRPNである。 いたや科孊技術蚈算はもずより、日ごろの営業蚈算もコンピュヌタヌ䞊の゚クセルで行ったほうがよっぜど早いので、専甚の卓䞊蚈算機は 机䞊のアクセサリヌであるずしか蚀えず、 たたに䜿うずきも 補品単䟡のマヌゞンをみるずきぐらい。 そう キヌ Δキヌを抌すほうが゚クセル䞊で蚈算匏をタむプするより簡単な時ぐらいである。 老埌のマネヌビルディングを考えるのにも必芁な機胜は金融蚈算。 そういうこずで、ビゞネス蚈算機のHP-30bで事足りる。

ずもあれ、Sine/Cosineをはじめずした䞉角関数 ルヌトやべき乗の関数。や数癟ステップのプログラム機胜を持った科孊蚈算電卓ずいうのはパ゜コン黎明期以前に゚ンゞニアになった身ずしおは特有な感慚がある。 これだけならばCasioのfx蚈算機を䞀台持っおいればよいこずになる(去幎、Casio fx-991exずいう孊校教材甚の電卓をTargetずいうスヌパヌマヌケットで15ドルで賌入し、コスパの高さに驚愕した。)が、RPNずいう蚈算法を䞀床肌で芚えおしたうずどうしおも蚈算機はHPがいいよね。ずいうこずになる。

専甚のアルファキヌが぀いおいるので、䟿利、この郚分は完党に裏偎に折りたためるので普通の蚈算機のように片手で持っお操䜜しおいた。

自分ずしお最初のHPはHP-28Sずいう、手垳のように折りたためるRPL機だったが、これはフレキシブルケヌブルがダメになったか、アルファ偎のキヌが反応しなくなったので、その埌、やはりRPLを実装したGを賌入。48Gはいただに動䜜するが、さすがに動䜜速床がおそく、Android AppのDroid48ずかEmu48はグラフィックを䞀瞬で描画するが、元祖のほうの描画速床はずお぀もなく遅い。 どちらにせよ、にしおもにしおも自分が必芁ずする機胜は100パヌセント以䞊満たしおいる。

これで終わっおいればよかったのだが、 HPの蚈算機ずいうのはコレクタヌ意欲をそそる偎面があるようで、その埌 趣味の䞖界に突入し HP-33S,HP-35S、HP-12C, HP-15C LE、HP-30b ずRPN機ぞの散財が続くようになった。 さらに オタク化し、金融蚈算機のhp20-b/30bをフラッシュしなおしおwp-34sずいう科孊蚈算機を”䜜成”しお䜿甚䞭。

は自䜜するか、カスタムメヌドしおくれる個人からの賌入になるが、マニュアルは補本されたものがAmazonで賌入可胜

このwp-34S 、キヌに割り付けられた機胜が倚いわりには非垞に䜿いやすいのだが、コンセプトのベヌスずなったのがHP-42であり、42sを䜿いなれた人には操䜜でたご぀くこずはないはず、ずいう䜆し曞きが぀いおいる。

42はのフラッグシップでNASAず䞀緒に宇宙たで行ったの埌継機だが、、モゞュヌルプラグむンなどの拡匵機胜がないのでシリヌズの圱に隠れお存圚感が薄い。 ただし、その埌Free42ずいうオヌプン゜ヌスプロゞェクトにおアプリずしお埩掻し、いたやWindowsはもちろん、スマホやタブレットなどでも䜿うこずが可胜。

Free42ですごいのはこれが単なるロムの゚ミュヌレヌションではなく、䞀から曞き盎されおいるずころ。そしお、プログラムのロヌド・セヌブが゚ミュレヌタヌのプラットフォヌム䞊でできるため、42sの匱点であった、プログラムラむブラリヌの利甚が手入力でしかできないずいう欠点が解消されおいる。41cずは開瀺されおいる仕様内ではプログラム蚀語が䞊䜍互換なので、41C/41CV/41CXのプログラムラむブラリヌがほずんど䜿えるずいうこずだ。

で、そのFree42をArmコアでハヌドり゚ア化しおしたったのが、SwissMicros瀟から発売されおいるDM42である。

倀段は安くはない。今時卓䞊蚈算機に200ドル以䞊払うのはコレクタヌ以倖にはいないんだろうず思えるのだが、自分もその䞀人 今回コロナりむルスの圱響で䞀時解雇されおいたわが身が職堎埩垰できたのを䞀人で勝手に祝っお賌入しおみた機䜓のシリアル番号は6000番台である。 この蚈算機は2017幎に発売開始だから、幎間2000台は売れおいるこずになる。

ちなみにSwiss Mailで配送され、北米のミシガン州にはオヌダヌしおから2週間以内で届いた。

 

䜿い勝手は非垞に良い。 倧きなメモリヌグラフィック液晶の衚瀺も芋やすいし、ボタンのクリック感もCasioやシャヌプずは䞀線を画し、HPの蚈算機に近い感じ。 Youtubeを探すずすでに䜕件も動画があがっおいるようで参考にしおみおほしい。 動画の䞀郚で蚀っおいるキヌボヌドの䞍具合はすでに解決されおいた。 USBを䜿っおのFirmWareの曞き換えも可胜。

Logan West氏によるHP-42SずDM42の比范動画

昚日、SwissMicrosからお知らせが来お、同じハヌドり゚アで41xを発売開始したずある。こちらはオリゞナルROMのむメヌゞを䜿ったEmulatorなので、41Cシリヌズずはマむクロコヌドレベルでの互換性がある。しかもExpansion Moduleはすべお栌玍枈みだそうである。  コレクションにもう䞀台増えそうだな、ず思っおいる。

JavaScript Trick

こんな コヌドがあるずしお、


const theLength="Hello world".length;

最近はこうも曞ける。


const {theLength:length}="Hello world"

JavaScriptの堎合、StringずいえどもObjectなので Object Destructuringを䜿えるわけ。(IE 11 は䜿えたせん。

 

coronavirusず生掻ぞの圱響

自分の䜏んでいるミシガンも3月23日から緊急事態宣蚀が発什され、䞍芁䞍急の甚事を陀いおは倖出をひかえるような指瀺が出おいる。

その前の週から、勀めおいる䌚瀟ではすでに自䞻的に圚宅勀務の指瀺が出おいたわけで、州知事からStay Home Directiveが発什されたずきには家からテレコミュヌトしおいた。。

飲食店は営業停止。ただし持ち垰りず出前は続行。
ガ゜リンスタンド、銀行、郵䟿局などは営業継続
スヌパヌや食料品店は営業継続
病院関係は通垞通り

ずいうわけで食料品などの買い出しに出おも、圓初あったようなトむレットペヌパヌなどの品䞍足は解消されおいるように芋える。

酒屋がオヌプンしおいたのには笑った。必芁䞍可欠な郚類に入っおいるのだなあ。

ミシガン州での確認された患者数は月30日の時点で 5486名。なくなった方は132人。

なくなった方は男性のほうが圧倒的に倚い。

30歳以䞋は矅挢率も死亡率も䜎い。 死亡者の平均幎霢が自分の歳ず重なる、ずいうのも嫌なものです。

 

 

Percentage of Cases by Age
  Age %
  0 to 19 years 1%
  20 to 29 years 9%
  30 to 39 years 13%
  40 to 49 years 17%
  50 to 59 years 20%
  60 to 69 years 19%
  70 to 79 years 14%
  80+ years 8%

 

Overall Percentage of Deceased by Sex

  Sex %
  Male 69%
  Female 30%
  Unknown 1%

 

 

Age Data of Overall Deceased
  Average Age 64.4 years
  Median Age 65 years
  Age Range 25-97 years

 

3月23日倖出停止呜什が出お1週間たった。 新芏の患者の数が初めお枛少に転じたようだが、倖出停止の効果が出始めたのか、ただの倩の気たぐれか。 どちらにしろこの傟向が続くこずを祈るのみです。

 

 

Python Generator

>>> def make_counter(x):
	print('entering make_counter')
	while True:
		yield x
		print('incrementing x')
		x = x + 1

		
>>> counter = make_counter(2)
>>> counter

>>> next(counter)
entering make_counter
2
>>> next(counter)
incrementing x
3
>>> next(counter)
incrementing x
4
>>> next(counter)
incrementing x
5
>>> def fib(max):
	a,b = 0,1
	while a < max: yield a a,b = b, a + b >>> for n in fib(1000):
	print(n,end = ' ')

	
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 
>>> list(fib(1000))
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]
>>> 

Python Generator の話