-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblog.atom.xml
2529 lines (2414 loc) · 277 KB
/
blog.atom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>The L Blog</title><link href="http://lmontopo.github.io/" rel="alternate"></link><link href="http://lmontopo.github.io/feeds/blog.atom.xml" rel="self"></link><id>http://lmontopo.github.io/</id><updated>2016-11-06T05:00:00-05:00</updated><entry><title>What's frontend, what's backend?</title><link href="http://lmontopo.github.io/frontend-backend.html" rel="alternate"></link><updated>2016-11-06T05:00:00-05:00</updated><author><name>Leta Montopoli</name></author><id>tag:lmontopo.github.io,2016-11-06:frontend-backend.html</id><summary type="html"><p>Last week a co-worker of mine asked me to clarify for him what the difference was between backend and frontend web development. I had been asked this quesiton before, but this was the first time I felt I provided a clear explanation and was able to instill knowledge.</p>
<p>So, here, I'd like jot down a lof of the key points which I think helped to clarify the meaning of and the difference between 'frontend' and 'backend' development:</p>
<ul>
<li><strong>Backend code is run on the server computer while frontend code is run on the client computer.</strong> </li>
<li>Being a <em>fullstack developer</em> means that you write (or can write) both backend and frontend code. </li>
<li>The backend takes care of any logic that the server needs to run before a response can be sent to the client. This includes things like retreiving data from a database, authenticating a user or making some calculations.</li>
<li>The frontend code takes care of any logic that needs to run locally - on the clients computer - in response to user interactions. This includes any subsequent network calls for updated data.</li>
<li>Frontend code is usually written in JavaScript, which your browser can interpret.</li>
</ul>
<h4>Why can't I write frontend in Python (or can I)?</h4>
<ul>
<li>Just like all browsers know how to turn HTML and CSS into pretty pages, all browsers know how to
interpret JavaScript; <strong>All browsers have built in JavaScript interpreters.</strong> <ul>
<li>Check it out! In Chrome right click on a page and click on the 'inspect'. Then click on the 'console' tab. You are now looking at a JavaScript Interpreter! For fun, write the following: <code>alert('Leta is cool')</code>.</li>
</ul>
</li>
<li>Python interpreters aren't currently built into browsers. It's not that they <em>couldn't be</em> it's just that they <em>aren't</em>. If I wanted to write frontend logic in Python I'd have two choices: <ol>
<li>Write a browser plugin to be able to interpret Python, and prompt users to install it to view my web app. <em>(This type of thing has been done in order to run Java on the frontend! Remember those annoying Java plugin installation promps?)</em></li>
<li>Compile my Python code down to JavaScript before sending it to the client. This is a bit of a hack, though, because technically javaScript is still what would be running on the client side.</li>
</ol>
</li>
</ul>
<h4>Where do HTML and CSS fit in?</h4>
<ul>
<li>Often HTML and CSS are considered frontend. The reason being is that, like JavaScript, they are interpreted by your browser.</li>
<li>I consider HTML to be backend only in the context of a template that will be compiled by a template enginge. In this case one is writting an HTML page with added template language logic which will render more HTML depending on the data that is fed into it. </li>
<li>To me, this falls under the backend umbrella because the templatign engine will turn this template into a finished HTML file on the surver side before sending the final HTML file to the client.</li>
<li>Some people agree with me on this distinction, others disagree (and that's OK!)</li>
</ul>
<h4>Further readings</h4>
<p>If you're curious to know a little more, specifically about why / how JavaScript became the only language built into browsers, I recommend reading <a href="https://www.reddit.com/r/learnprogramming/comments/3frml2/why_is_javascript_the_only_frontend_programming/?st=iv70e8ah&amp;sh=d65bf9be">this</a> thread on reddit. I found it to be well explained and pretty thorough! </p></summary><category term="Web"></category></entry><entry><title>How the Internet Works: Domain Names and Routing</title><link href="http://lmontopo.github.io/how-the-internet-works-2.html" rel="alternate"></link><updated>2015-09-14T05:00:00-04:00</updated><author><name>Leta Montopoli</name></author><id>tag:lmontopo.github.io,2015-09-14:how-the-internet-works-2.html</id><summary type="html"><p>This blog post is a continuation of my previous post <a href="http://lmontopo.github.io/how-the-internet-works.html">How the Internet Works</a>. Here, I hope to answer the following questions: </p>
<ul>
<li>
<p><strong>Since we don't usually specify the IP address of the computer we wish to connect with, how does our computer successfully connect with it?</strong> </p>
</li>
<li>
<p><strong>Assuming the IP address of the destination computer is known, How does a request from my computer actually make it to the computer with this IP? How does it know where that computer is?</strong></p>
</li>
</ul>
<h2>The Domain Name System</h2>
<p>A <strong>domain name</strong> is a human readable address of a computer on the internet. Domain names are used all the time for activities requiring the internet, including sending emails and browsing web pages. The part of a URL<sup id="fnref:1"><a class="footnote-ref" href="#fn:1" rel="footnote">1</a></sup> that appears after the protocol but before the file path is the domain name. For example, <code>lmontopo.github.io</code> is the domain name in the URL of this blog post: <code>https://lmontopo.github.io/how-the-internet-works-2.html</code>. </p>
<p>The term <strong>Domain Name System (DNS)</strong> refers to the internet service of translating domain names into IP addresses. It consists of a distributed<sup id="fnref:2"><a class="footnote-ref" href="#fn:2" rel="footnote">2</a></sup> database of IP address and domain name pairs. Hundreds of servers known as <strong>DNS servers</strong> are dedicated to working together to translate domain names into IP addresses. There are two kinds of DNS servers: caching servers and authoritative name servers: </p>
<ul>
<li>
<p><strong>Authoritative Servers</strong> are responsible for maintaining a database of the IP addresses of domains within its 'authority'. <em>What does this mean?</em> Well, these servers are organized in a heiarchy that matches the structure of the domain names themselves. Indeed, domain names consist of one or more domains separated by dots. The rightmost domain is the <em>top-level domain</em>. Examples of top-level domains include <code>com</code>, <code>org</code> and <code>edu</code>. Directly left of the top-level domain is the second-level domain, preceeded by more sub-domains until the leftmost word, the <em>host</em>. An authoritative DNS server handles a specific level in this heiarchy and knows the IP addresses of all hosts and subdomains under it. So, for example, an authoritative DNS server with authority over the <code>github.io</code> domain would know the IP address of host <code>lmontopo.github.io</code>.</p>
</li>
<li>
<p><strong>Caching Servers</strong> are your computer's first point of contact with the DNS. Your computer is automatically<sup id="fnref:3"><a class="footnote-ref" href="#fn:3" rel="footnote">3</a></sup> configured with the IP address of a caching DNS server (usually provided by your Internet Service Provider) so that your computer knows who to contact for DNS resolution. These caching servers communicate back and forth with authoritative name servers on your behalf until they find one that can resolve the supplied domain name into an IP address. </p>
</li>
</ul>
<p><strong>OK, I know this stuff can be confusing, but if you've made it this far, bear with me!</strong></p>
<p><em>Let's go through an example to better understand how this works: Suppose we request the IP address of the domain name 'www.wikipedia.org'</em>:</p>
<p>As previously explained, our computer will first send a request to the DNS Server (a cache server) specified in its network configuration settings. Lets call this cache server Jane, since we'll be talking about it lots. Our computer will ask Jane something like “Can you tell me the IP address of www.wikipedia.org?”. Usually the DNS server provided by our ISP will have a cache<sup id="fnref:4"><a class="footnote-ref" href="#fn:4" rel="footnote">4</a></sup> of frequently resolved domain names. Assuming Jane has one, she'll search her cache for this domain name. If Jane finds it she'll get back to us straight away with the desired IP address. Otherwise Jane will strip the host from the domain name and search her cache for a server with authority over the remaining name: <code>wikipedia.com</code>. If Jane find this server's IP in her cache she'll send our original request-“Can you tell me the IP address of www.wikipedia.org?”- to this server. For now, lets assume Jane's cache doesn't contain 'wikipedia.com'. Then Jane will again search her cache for the server with authority over the <code>com</code> domain. (Notice that each time the authoritative DNS server strips of the leftmost word off the domain name.) Finally, if Jane doesn't find this server's IP, she'll resort to contacting the <strong>DNS Root Servers</strong>. Root servers are the Authoritative DNS Servers which reside at the top of the heiarchy, right above above top level DNS servers. <em>(Cache servers, like Jane, are pre-configured with a list of root servers so that they always have someone to contact.)</em> In this case the root server would respond with a list of server IPs of the <code>com</code> domain. Jane would then contact one of the servers from this list, and ask it to resolve <code>www.wikipedia.com</code>. The <code>com</code> DNS server would give Jane some IPs of DNS servers with authority over the <code>wikipedia.com</code> domain. Jane could then contact one of these servers, any of which could resolve <code>www.wikipedia.com</code>. When Jane finally gets the translation she sends us back our desired IP address. Jane will also keep (at least temporarily) a cache of the IP addresses that were just discovered: IPs of <code>com</code> domain servers, <code>wikipedia.com</code> domain servers and the IP address of <code>www.wikipedia.com</code>. The following picture demontstrates this process:<sup id="fnref:5"><a class="footnote-ref" href="#fn:5" rel="footnote">5</a></sup></p>
<p><img alt="Pelican" src="../images/example_lookup.png" /></p>
<p><em>Cool Trick: you can actually bypass the whole Domain Name Resolution Process by typing in the IP address of the computer you wish to communicate with.</em></p>
<h2>Routing</h2>
<p>Once we know the IP address of the computer we want to talk to, how does our request make its way to that computer? </p>
<p>The answer, is <strong>routers</strong>. Routers connect</p>
<p>Routers are connected between networks to control the flow of messages between them. The main responsibilitis of a router are to ensure messages get sent where they are intended, and to ensure messages don't get sent into networks where they weren't intended. But how does it do this? </p>
<p>Well, each router usually knows about the IP addresses within its sub-networks but not about the IP addresses in networks above it (similar to the DNS system!). At a high level, we can imagine that a request arrives at its destination IP address by first travelling up this hierarchy of routers until a router that knows about the destination's IP address is reached. At this point the request would begin to travel back down the chain in the direction of the desired computer. </p>
<p><strong>Although this gives us a nice idea of how routing works, in reality there's a bit more to it.</strong> The picture I painted seems to imply that the computer at the ‘top’ of this chain is all knowing -i.e. it knows all the IP addresses of every computer below it- and that is not the case. Each router has a configuration table which lists patterns of IP addresses, and rules to follow based on those patterns. The router will scan the destination address and match that IP address against the patterns in its table. When the router finds the category that the IP fits into, it follows the related instructions to go in a specific direction. Before actually sending out information in this direction, the router will check to see if that direction is satisfactory (i.e. flow is OK there, and nothing seems broken). If all is well it will go ahead. Otherwise it will check the configuration table for an alternate route. Because many different routes exist between two computers on the internet, a request made to google.com one minute might take a completely different route than the next minute. And that's part of the beauty of routing, is that its adaptable and can act accordingly.</p>
<p>In order for routers to do their job, they are also performing <strong>Network Address Translations</strong> (NAT). Remember last time how I mentioned that your personal computer's public IP is your router's IP? Well, your router has to translate all of the traffic coming from your local network, re-addressing it with its own IP address. Then the router has to keep track of which incoming messages (all of which are addressed to <em>its</em> IP address!) are meant for which computer in its local Network. Even more complicated, routers may have to deal with situations where a private IP in your local network overlaps with a public IP address somewhere in the internet. In some cases this might cause issues, but other times your router may successfully create a 'lookup table' to keep track of which computer is which, and translate IP addresses to compensate.</p>
<h3>Thanks Again</h3>
<p>Well, thats it (for now!) on how the internet works. I hope that if you've made it this far you've enjoyed the journey. As always, feel free to reach out to me if you have any questions! I am no expert, but if there's something I can help you understand I'd be happy to do so!</p>
<div class="footnote">
<hr />
<ol>
<li id="fn:1">
<p>URL stands for Uniform Resource Locator and refers to the web address used to locate a web page on the World Wide Web.&#160;<a class="footnote-backref" href="#fnref:1" rev="footnote" title="Jump back to footnote 1 in the text">&#8617;</a></p>
</li>
<li id="fn:2">
<p>The word <em>distributed</em> in this context just means that the database is stored over many computers, as opposed to a single database on a single computer. &#160;<a class="footnote-backref" href="#fnref:2" rev="footnote" title="Jump back to footnote 2 in the text">&#8617;</a></p>
</li>
<li id="fn:3">
<p>Of course, you can override this setting if you'd like to use some other Domain Name System Server.&#160;<a class="footnote-backref" href="#fnref:3" rev="footnote" title="Jump back to footnote 3 in the text">&#8617;</a></p>
</li>
<li id="fn:4">
<p>A <strong>cache</strong> is just a fancy word for data that is stored on a machine. Usually this data is the result of a computation or process that it previously performed, so that if it needs the result again it can provide it more quickly. &#160;<a class="footnote-backref" href="#fnref:4" rev="footnote" title="Jump back to footnote 4 in the text">&#8617;</a></p>
</li>
<li id="fn:5">
<p>I got this great image from the wikipedia page on the Domain Name System. You can find it <a href="https://en.wikipedia.org/wiki/Domain_Name_System#/media/File:An_example_of_theoretical_DNS_recursion.svg">here</a>.&#160;<a class="footnote-backref" href="#fnref:5" rev="footnote" title="Jump back to footnote 5 in the text">&#8617;</a></p>
</li>
</ol>
</div></summary><category term="Web"></category></entry><entry><title>How the Internet Works</title><link href="http://lmontopo.github.io/how-the-internet-works.html" rel="alternate"></link><updated>2015-09-10T05:00:00-04:00</updated><author><name>Leta Montopoli</name></author><id>tag:lmontopo.github.io,2015-09-10:how-the-internet-works.html</id><summary type="html"><h2>What is the internet?</h2>
<p>One thing I’d like to clarify right off the bat is that when we connect to the internet we are connecting to other physical computers. Any service accessed through the internet- email, Skype, reading the news online- is the result of obtaining data from one or more physical computers. The internet isn’t some magical entity where resources appear out of thin air. Indeed, files saved on an online storage system are physically stored on one or more physical computer somewhere in the world. Those of you who already knew this might find it silly of me to point this out, but I’m sure that there are people out there who have this misconception - I used to be one of them.</p>
<p>In order to understand the structure of the internet, we'll begin by talking about computer networks in general.</p>
<h3>Types of Networks</h3>
<p>Different types of computer networks can be classified by size. Lets start with the smallest and work our way up to the largest - the internet: </p>
<ul>
<li>The smallest type of network is a <strong>Personal Area Network</strong> (PAN). A good example of this type of network is the use of bluetooth. In a personal area network computers are interacting with each other over a very short distance. </li>
<li>The second smallest type of network is a <strong>Local Area Network</strong>, or LAN. These networks span the area of a house, or a few floors of an office building. As an example: consider how the computers in an office building are often all connected to the same printer. Another example of the use of a LAN would be you pinging <sup id="fnref:1"><a class="footnote-ref" href="#fn:1" rel="footnote">1</a></sup> your co-workers computer when you’re both in the office. The 'ping' would travel from your computer to your office's router<sup id="fnref:2"><a class="footnote-ref" href="#fn:2" rel="footnote">2</a></sup> and then to your coworker's computer. </li>
<li>At a larger scale we have <strong>Metropolitan Area Network</strong> (MAN) which can span the distance of a few blocks or the entire city. An example of a MAN would be the network of computers that are serviced by a particular Internet Service Provider (ISP) within the city of Toronto. Indeed all of the modems which provide the LANs with internet, are connected to each other through Internet Exchange Points (IXPs) within the city.</li>
<li>Anything larger than a MAN is considered to be a <strong>Wide Area Network</strong>, or WAN. The largest WAN is the internet, connecting computers all over the earth. </li>
</ul>
<p>The internet can be thought of as a compilation of some of these smaller networks. One might call it a 'network of networks'. </p>
<p><strong>Let's walk through some of the networks I'm connected to when I access the internet from my apartment in Toronto.</strong> At the smallest level I'm connected to my LAN through my router. This router communicates with my modem which allows me access to the city-wide network of computers serviced by my internet provider, a MAN. Then, all of the internet providers within Toronto connect to each other at IXPs to create an even larger MAN. And all of these large MANs accross the globe connect to one another through various IXPs, creating what we call the internet! The following image <sup id="fnref:3"><a class="footnote-ref" href="#fn:3" rel="footnote">3</a></sup> serves as a good visual depiction of the internet as a network of networks:</p>
<p><img alt="Pelican" src="../images/lan_man_wan.jpg" /></p>
<h3>Physically how are computers connected?</h3>
<p>Sometimes transmission happens wirelessly, other times it happens through physical wires.
Bluetooth and wifi are examples of a wireless network connections. In these situations radio signals are used to carry data from one computer to the next. Most of these wireless networks happen over very short distances. Between cities and countries and continents, computers are usually connected through wires. Yes, <em>wires are literally in the ocean, connecting continents so that we can chat to people across the globe.</em></p>
<p>Cool. Now we have an idea of what the internet <em>is</em>. It is a network of computer networks connecting computers accross the globe. These connections can be wireless or through wires. But <em>how</em> does it work?</p>
<h2>How is data transmitted?</h2>
<p>How do computers understand each other? How does a message get to the appropriate computer? Introducing... the protocol stack!</p>
<h3>The Protocol Stack</h3>
<p>A <strong>protocol</strong> is just a set of rules that computers obey when they exchange data. Every type of communication that occurs over the internet uses a set of protocols specified by its corresponding protocol stack. Different applications (skype, email, etc) may use different protocol stacks. Here is a diagram depicting the primary layers in the stack: </p>
<p><img alt="Pelican" src="../images/protocol_stack.png" /></p>
<p>As you can see from the diagram, when data gets sent from Computer 1 to Computer 2, Computer 1 first has to prepare the data for its transmission by going through each of the layers of the protocol stack to add information, package it up, possibly encode or translate it. When the data is ready, it is passed through the internet from Computer 1 to Computer 2, where the data is unpacked and transformed into its original form. Lets start from the bottom and work our way up, describing briefly each of these layers. </p>
<h4>Hardware Layer:</h4>
<p>This layer is responsible for translating messages from your computer's native language (in 0s and 1s) to signals that can be transmitted either through wire or radio signals. Most laptops today have hardware specific for both the ethernet cable and for wifi. Note that different hardware is required in your computer for each kind of physical connection. Often the protocol associated with this hardware requires that the unique address on the associated hardware be specified when going from one computer to the next. This address is called the <strong>Media Access Control address</strong>, or MAC address. </p>
<p><em>If you are using a Mac OS X (for Linux and Windows see footnote<sup id="fnref:4"><a class="footnote-ref" href="#fn:4" rel="footnote">4</a></sup>), you can find your computer's MAC addresses, by typing the following command into your terminal:</em> <code>$ networksetup -listallhardwareports</code>. <em>You should see several different types of hardware appear, including wifi and ethernet, each with their own 'ethernet address'. This 'ethernet address' is that hardware's MAC address! A couple of my entries look like this:</em> </p>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5
6
7</pre></div></td><td class="code"><div class="highlight"><pre>Hardware Port: Ethernet
Device: en0
Ethernet Address: 9f:68:90:ce:82:62
Hardware Port: Wi-Fi
Device: en1
Ethernet Address: 70:27:92:c6:db:63
</pre></div>
</td></tr></table>
<h4>Internet Protocol Layer:</h4>
<p>Every computer on the internet is marked with a ‘unique’ address called an <strong>Internet Protocol address</strong>, or IP address. In order for data to move from one computer to the next, it must be labeled with the target IP address and the sender’s IP address. IP addresses can be split into two categories, <em>public</em> and <em>private</em>. Public IPs are given to every computer ‘visible’ on the internet and are guaranteed to be unique. Private IPs are given to computers within the same Local Area Network and need only be unique with respect to the other IPs on that network. When you connect to your wifi at home, your router is acting as your gateway to the internet and your private IP allows your router to distinguish your computer from others on the local network. </p>
<p>IP addresses can also be <em>static</em> or <em>dynamic</em>. Static IP addresses are permanent; they don't change. A computer with a static IP address will have the exact same IP address for the duration of its contract with an Internet Service Provider. This requires the ISP to effectively <em>reserve</em> an IP address for a single computer. In contrast, dynamic IP addresses are temporary in nature. A computer is re-assigned a dynamic IP address each time it connects to the internet (or, sometimes, even mid-session). Consequently, ISPs can <em>re-use</em> dynamic IP addresses. For example, consider the dynamic IP address that is assigned to my computer when I connect to the wifi at a local coffee shop. This IP address is not reserved for my computer alone, and may be re-assigned to another computer when I leave. Because they are re-usable, dynamic IP addresses are less costly than static IPs. Most personal devices will make use of dynamic IPs, while servers usually rely on static IP addresses.</p>
<h4>Transport Protocol Layer:</h4>
<p>Among other things, the transport protocol is in charge of labelling which port number on a receiving computer a message is sent to. You can think of the port as being analogous to specifying a person’s name on a letter to distinguish which person in a house the letter is being sent to. Since your computer has multiple applications accessing the internet, the port number specifies which application incoming data is meant for. This allows multiple processes to share the same hardware resources without interfering with one another. Without port numbers, a computer's operating system would have a hard time knowing which application (a web browser, email service) an incoming message was meant for. </p>
<p>Other responsibilities of the transport layer may include segmentation and desegmentation of the data, ensuring sent data is received, and ensuring it is received in the desired order.</p>
<h4>Application Level Protocol:</h4>
<p>Specific to the application, be it email, skype, or the world wide web. In the following example we will discuss the Hyper Text Transfer Protocol (HTTP), which is a popular Application Level Protocol.</p>
<p><strong>Classic Example: Suppose I’m connecting to the internet through my apartment’s wifi and I type www.wikipedia.com into my browser and press enter.</strong></p>
<p><em>Here the application in use is the world wide web. The request will be formatted according to the application level protocols specifications (in this case HTTP): <code>HTTP 1.1/ GET wiki/main_page</code>. This tells the application on the receiving end that we are using HTTP protocol and that we wish to GET (meaning retrieve) the resource specified. This message will get passed from our browser (the application) to the computer’s operating system where the transport protocol will do its work. In this example TCP, Transmission Control Protocol will be used. When necessary, TCP will split the message into smaller (more manageable) chunks called packets before sending. In this case, each packet will also be labeled so that the receiver knows in which order the packets should be read. TCP will also attach the port number (in this case 80) to each packet. Next, the Internet Protocol further packages each packet with the sender and receiver's IP addresses, before handing over control to the hardware/link layer. This layer wraps the packages with the wifi MAC address on your computer and translates messages into radio signals to be sent to the router. This message will go from computer to computer, continually unpacked and repackaged by the hardware layers as it travels, until it reaches its destination, where the wiki/main_page resource lives. The destination computer will translate the message into its native language through the hardware layer. Then the TCP layer will ensure that each package has arrived (remember that they are labelled) and if one is missing, the computer will send out a request for the missing packet. If all have arrived, it will place them in order, and send them to the appropriate computer application. This main wiki page will then be retrieved from the application, and the process of packaging it up for sending will commence. The resource will be wrapped with the message <code>HTTP 1.1 200 OK</code> before being split up into manageable chunks, labeled, then repackaged with the IP address of the destination and sender, and further translated and prepared by the hardware layer before being sent out. When the message finds its way back to our computer, it will be unpacked by each layer until the contents of the message, the wiki/main_page, is provided to our browser to display.</em> </p>
<h3>Stay Tuned... Domain Names and Routing!</h3>
<p>At this point we’ve answered a lot of questions in regards to how the internet works. But, there’s still a big missing piece of the puzzle, namely, how does my computer know the IP address of the computer we want a resource from, and how does my router know how to navigate to that computer? </p>
<h4>Thanks</h4>
<p>Thanks for reading along on this journey to better understand the internet. There is, of course, still lots of details that I have left out of this discussion, and much that I still have to learn, but I think I've provided enough details to staisfy some itching curiousities. Don't hesitate to reach out if anything is unclear or if you have any questions! I can't guarantee I have all the answers, but if I don't, I might be curious as well! </p>
<p>Big thanks to <a href="http://individual.utoronto.ca/jnlantos/">Jasmin Lantos</a> for studying this topic with me during our 10% time at <a href="https://securitycompass.com/">Security Compass</a>! </p>
<div class="footnote">
<hr />
<ol>
<li id="fn:1">
<p>For anyone unfamiliar with the PING command, try it out! Simply type <code>ping ip_address</code> to effectively 'ping' the computer with the corresponding IP address. You can also ping a server by their hostname. &#160;<a class="footnote-backref" href="#fnref:1" rev="footnote" title="Jump back to footnote 1 in the text">&#8617;</a></p>
</li>
<li id="fn:2">
<p>The difference between a router and a modem confsed me at first, so I'm going to take a moment to describe their purposes in your home. Your router is what allows several computer to connect to the internet simultaneously. The router is connected to the motem, and the motem is what is needed to gain access to the internet through your Internet Service Provider. This means that if you only want one computer to have access to the internet at a time in your househole, you only need a motem, not a router. Similarily, if you just wanted to communicate with other computers in your home, and not to other computers in the world, you could just use a router and do away with the motem and the internet service provider. Sometimes you can buy a box that is both a router and a motem all in one. &#160;<a class="footnote-backref" href="#fnref:2" rev="footnote" title="Jump back to footnote 2 in the text">&#8617;</a></p>
</li>
<li id="fn:3">
<p>This image is adapted from <a href="http://www.gta.ufrj.br/ensino/eel879/trabalhos_vf_2008_2/igorcamp/LAN_MAN_WAN.JPG">http://www.gta.ufrj.br/ensino/eel879/trabalhos_vf_2008_2/igorcamp/LAN_MAN_WAN.JPG</a>&#160;<a class="footnote-backref" href="#fnref:3" rev="footnote" title="Jump back to footnote 3 in the text">&#8617;</a></p>
</li>
<li id="fn:4">
<p>On a Linux, I have been told that the appropriate command would be <code>ifconfig -a</code> and that the number next to <code>HWaddress</code> corresponds to the MAC address. On a Windows operating system, I beleive one needs to type <code>getmac /v</code> into the prompt. Unfortunately I haven't been able to try these commandsn myself, so please let me know if they do not work! &#160;<a class="footnote-backref" href="#fnref:4" rev="footnote" title="Jump back to footnote 4 in the text">&#8617;</a></p>
</li>
</ol>
</div></summary><category term="Web"></category></entry><entry><title>News, Updates and Getting Momentum Again!</title><link href="http://lmontopo.github.io/news-and-updates.html" rel="alternate"></link><updated>2015-07-12T00:00:00-04:00</updated><author><name>Leta Montopoli</name></author><id>tag:lmontopo.github.io,2015-07-12:news-and-updates.html</id><summary type="html"><p>Hello again, Internet People! Lets start with some news and updates!</p>
<h3>Hacker School is now The Recurse Center</h3>
<p>This is kind of 'old' news, but since I went AWOL for a while, I'm only now making the announcement to the world. For now, I am going to leave my old blog posts that mention Hacker School as is, but in the future I hope to go back and change them. Going forward, I will use 'The Recurse Center' exclusively.</p>
<p>For anyone who would like more information about The Recurse Center or about the name change, check out these links: </p>
<ul>
<li><a href="https://www.recurse.com/">www.recurse.com</a></li>
<li><a href="https://www.recurse.com/blog/77-hacker-school-is-now-the-recurse-center">www.recurse.com/blog/77-hacker-school-is-now-the-recurse-center</a> </li>
</ul>
<h3>I'm working at Security Compass</h3>
<p>After a few months of job searching this winter, I landed a position as a paid intern at a <a href="http://securitycompass.com/">Security Compass</a> in Toronto. I am a little over 3 months into the position, and I have signed a contract to become a permanent Junior Dev starting in August! I am excited to be staying with them, and I look forward to the challenges to come! </p>
<p>I want to take a quick moment here to thank The Recurse Center for all of the support it offered me during the job search process. I found much support and advice from the facilitators as well as from other recurse center participants, and for that I am really grateful. Thank you. </p>
<h3>Building Momentum</h3>
<p>The move and the internship have been the primary reasons for my blogging leave of absence. Initially, with all these changes in my life, I needed more rest than usual and so I took a break from blogging. But then as time passed I found it was increasingly harder to get back into writing. Despite the <em>plethora</em> of articles I had buzzing in my head demanding to be written, procrastination always seemed to win. And in the rare moments when I actually managed to write something, I often couldn't find the time to 'perfect' my work and so perfectionism silenced me from publishing it.</p>
<p>This blog post is my attempt to battle perfectionism and procrastination. I hope that with this imperfect blog post, I can get the ball rolling again, and build the momentum to start writting on a somewhat more regular basis again. </p>
<p>Ciao!</p></summary><category term="Life"></category><category term="The Recurse Center"></category></entry><entry><title>Interviews are oportunities to learn</title><link href="http://lmontopo.github.io/interviews.html" rel="alternate"></link><updated>2015-01-29T05:00:00-05:00</updated><author><name>Leta Montopoli</name></author><id>tag:lmontopo.github.io,2015-01-29:interviews.html</id><summary type="html"><p>Over the past month I've had my first experience interviewing for development positions. Its been a somewhat nerve-racking experience but also, at times, enjoyable. I've come to realize that the best way to approach an interview is with an attitude of excitement and an openness to learn.</p>
<p>A couple days before my first 'technical' interview I was feeling pretty overwhelemed. Tom, a Hacker School facilitator, reminded me that <strong>its public knowledge that I haven't been programming forever</strong>. He continued to explain that its OK I don't know everything, and that I'm not expected to. This was exactly the reminder I needed to hear. Of <em>course</em> I don't know everything. I'm just starting out in this field! Sonali suggested I have fun with my interviews and that I view them as opportunities to learn. </p>
<p>Their words of wisdom have really helped me make the most of my interviews. When I stopped putting pressure on myself to be something I'm not (an experienced programmer who knows everything) I could relax, be open to learning, and be genuinely engaged in conversations about programming. Regardless of whether or not they lead to a job, I am counting my interviews thus far to be a success because of the way I handled them. I was genuine, I had fun, and I learned cool things. Win.</p>
<p>On that note, here's a neat Python thing I learned during a recent technical interview<sup id="fnref:*"><a class="footnote-ref" href="#fn:*" rel="footnote">1</a></sup>: </p>
<h4>A Python Function's Default Arguments Are Mutable:</h4>
<p>Without running the python code, guess what the following lines would return in an interactive Python interpreter. (Actually try to guess them before reading my answer! I've added a blank line where you should be guessing):</p>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
2
3
4
5
6
7
8
9
10
11</pre></div></td><td class="code"><div class="highlight"><pre><span class="o">&gt;&gt;&gt;</span> <span class="k">def</span> <span class="nf">test</span><span class="p">(</span><span class="n">a</span> <span class="o">=</span> <span class="p">[]):</span>
<span class="o">...</span> <span class="n">a</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="o">...</span> <span class="k">return</span> <span class="n">a</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">b</span> <span class="o">=</span> <span class="n">test</span><span class="p">()</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">b</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">c</span> <span class="o">=</span> <span class="n">test</span><span class="p">()</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">c</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">d</span> <span class="o">=</span> <span class="n">test</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">d</span>
</pre></div>
</td></tr></table>
<p>Have you guessed yet?!
... </p>
<p>My guess was <code>[1]</code>, <code>[1]</code>, <code>[1,1]</code> but the correct answer is <code>[1]</code>, <code>[1,1]</code>, <code>[1,1,1]</code>.</p>
<p><strong>What does all of this mean and what does it tell us about the way Python handles default arguments?</strong></p>
<p>It means that the default argument binding<sup id="fnref:%"><a class="footnote-ref" href="#fn:%" rel="footnote">2</a></sup> happens at the function's <em>definition</em> not at the function's <em>execution</em>. In other words, the statement <code>a = []</code> is evaluated when the function definition statement is executed, but not when the function is called and its body is executed.</p>
<p>Lets dig a little deeper. Recall that in Python everything, including functions, is an object. When a function definition is executed a new function object is created. This function object will have an attribute called <code>func_defaults</code> that contains the values of the default arguments. </p>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2</pre></div></td><td class="code"><div class="highlight"><pre><span class="o">&gt;&gt;&gt;</span> <span class="n">test</span><span class="o">.</span><span class="n">func_defaults</span>
<span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">],)</span>
</pre></div>
</td></tr></table>
<p>Like usual object attributes, func_defaults can be mutated. In our example, the default argument is mutated when the body of the function is executed. Since the line of code that sets func_defaults to <code>[]</code> is run only when the object is instantiated, these mutations affect the default value for subsequent calls on the function. </p>
<p>I hope this makes sense! Feel free to ask questions if it doesn't. :) Also, some useful resources can be found <a href="http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument">here</a> and <a href="http://effbot.org/zone/default-values.htm">here</a>. </p>
<p>And to anyone out there currently undergoing interviews: Try to have fun. Above all else, be yourself. You are already impressive.</p>
<div class="footnote">
<hr />
<ol>
<li id="fn:*">
<p>The interviewer agreed that I could write about his question! :)&#160;<a class="footnote-backref" href="#fnref:*" rev="footnote" title="Jump back to footnote 1 in the text">&#8617;</a></p>
</li>
<li id="fn:%">
<p>Arugment 'binding' is just a fancy way of specifying that a variables name (ex. <code>a</code>) gets assigned to a peice of data (ex. <code>[]</code>).&#160;<a class="footnote-backref" href="#fnref:%" rev="footnote" title="Jump back to footnote 2 in the text">&#8617;</a></p>
</li>
</ol>
</div></summary><category term="Python"></category></entry><entry><title>Some things I learned when my computer crashed.</title><link href="http://lmontopo.github.io/some-things-i-learned-when-my-computer-crashed.html" rel="alternate"></link><updated>2014-12-31T00:00:00-05:00</updated><author><name>Leta Montopoli</name></author><id>tag:lmontopo.github.io,2014-12-31:some-things-i-learned-when-my-computer-crashed.html</id><summary type="html"><p>During my last two weeks at Hacker School I encountered some serious troubles with my computer. Despite becoming overly emotional about the state of my computer, I learned some valuable things through this experience. I learned some cool bash commands when <a href="http://rodarmor.com/">Casey</a> helped me try to diagnose the cause of my computer's distress. Then, when my computer was unusable, I spent some time reading a bit about bash and I used Casey's to set up my first ssh account! Finally, when I got my computer back in good health, I learned some lessons about good documentation practices and backing things up. In this blog post I'll write about some of these things I've learned.</p>
<h4>Bash Commands:</h4>
<p>When Casey and I first realized my computer was abnormally slow we set out to do some investigating. We used some bash commands to see what processes were running and what system calls were being made. Our hope was that we might detect a particular call that my computer was stuck on. We didn't have much success with our investigation, but it was still super cool peaking behind the scenes! Our experimentation also make me eager to learn more about the command line. So, while my computer was away being fixed, I spent some time reading more about bash. Here are some of the bash commands I've learned:</p>
<ul>
<li>
<p><code>$ ps aux</code> : shows all current processes being run. This is one of the commands Casey taught me while we were playing detective! </p>
</li>
<li>
<p><code>$ dtruss -p PID</code> : Another one that Casey taught me. When dtruss is called on a particular process (here i'm specifying the process by its PID, or "process ID"), your computer will show you all of the system calls that are happening for that process. If you want, you can also look up the man pages for these system calls to figure out what is happening. Very cool stuff!</p>
</li>
<li>
<p><code>$ cal</code> : This one is kind of just for fun. If you aren't familiar with this command, try it out! </p>
</li>
<li>
<p><code>$ say something</code> : This one is also just for fun. The word 'something' can be replaced with whatever you want. <a href="http://todayincode.tumblr.com/">Cerek</a> taught me this command when he made his computer sing rap lyrics. </p>
</li>
<li>
<p><code>$ chmod</code> : changes the reading, writing and executing permissions of a file. I read about this from a book, and then two days later I needed to use this command! I'll talk more in detail about this command in the next seciton. </p>
</li>
<li>
<p><code>$ sudo</code> : This is a command that I've been using for a while without fully understanding it. What I understood was that to call certain commands we must first call <code>sudo</code> and provide our password. While this is true, it misses the big picture of what sudo <em>does</em>. Sudo's man page provides a rather good explanation of the command: "<strong>sudo</strong> allows a permitted user to execute a command as the superuser<sup id="fnref:1"><a class="footnote-ref" href="#fn:1" rel="footnote">1</a></sup> or as another user". In fact, <code>sudo another_user</code> can be called to carry out a command as if you were 'another_user'. The superuser is the default user when no other user is specified. So, calling <code>sudo</code> with no other argument will allow me to call commands as if I were the superuser, thus giving me extra privileges. </p>
</li>
<li>
<p><code>$ alias alias_name="command"</code> : creates an alias for the command! An <strong>alias</strong> is a shortcut for other bash commands. A useful tutorial (with examples!) on this topic can be found <a href="https://www.digitalocean.com/community/tutorials/an-introduction-to-useful-bash-aliases-and-functions">here</a>. With <a href="http://www.zephyrizing.net/">Geoff</a>'s help I made my very first alias for a really ugly looking git command. Here it is: </p>
</li>
</ul>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2</pre></div></td><td class="code"><div class="highlight"><pre><span class="nv">$ </span><span class="nb">alias </span><span class="nv">gr</span><span class="o">=</span><span class="s2">&quot;log --graph --full-history --all --color --pretty=tformat:%x1b</span>
<span class="s2">[31m%h%x09%x1b[32m%d%x1b[0m%x20%s%x20%x1b[33m(%an)%x1b[0m -n15&quot;</span>
</pre></div>
</td></tr></table>
<p>Now I can just type <code>git gr</code> into my command line to produce a pretty graphical representation of my git log. Great! </p>
<h4>What is SSH?</h4>
<p>Throughout hacker school I had heard the term "ssh" come up in conversation a few times, but I never paused to ask about it. That is, until <a href="http://www.flowerhack.com/">Julia</a> set up an ssh server on "www.hackers.cool" and invited us to all get accounts! So, I learned some stuff about ssh, I set up a hackers.cool account on Casey's computer, and, once I got my computer back from the repair shop, I transfered my account from Casey's computer to my own. Here's are some things I learned:</p>
<p><strong>SSH</strong> stands for secure shell. As far as I understand this specifies a protocol by which 'clients' can log onto a server computer remotely through a command line interface. For me, setting up my own hackers.cool account helped me understand what this really meant. I got to see how typing <code>ssh leta@hackers.cool</code> into my terminal would remotely connect me to the harckers.cool server. Here are some of the cool things I observed:</p>
<ul>
<li>
<p>Other users could write messages in the terminal like <code>banner Hello</code> or <code>echo Hey!</code>, and they would appear on my terminal too! This makes sense because we were all remotely connected to the <em>same</em> server.</p>
</li>
<li>
<p>I made changes to my hackers.cool user home page. This homepage is specified by a file on the <em>remote</em> computer and I was changing this file from <em>my</em> computer. I could even check out my homepage on hackers.cool for proof that the changes actually occured. This was an exciting experience for me. </p>
</li>
<li>
<p>Another cool thing I could do was type 'irc', which stands for internet relay chat. This brings me to a an old school style chat room with other hackers.cool clients! </p>
</li>
</ul>
<p><strong>How I set up my hackers.cool account</strong>:<br />
On www.hackers.cool you can click on "sign up here" for instructions. It will ask you for a username and will ask you to provide a public ssh key. I like <a href="https://help.github.com/articles/generating-ssh-keys/">Github's</a> simple description of an SSH key as "a way to identify trusted computers without involving passwords". I'm not exactly sure how they work, but some sort of cryptogrophy is involved for security. I'd like to find out more about them. </p>
<p><strong>How did I transfer my account from Casey's computer to my own?</strong><br />
At some point I had to figure out how to transfer my hackers.cool account from Casey's computer to my own. Since the ssh key associated with my account was on Casey's computer and not my own, something needed to be done before I could log in from my own computer. So, I grabbed a USB stick and moved the .ssh directory onto my computer. I still wasn't able to log into hackers.cool. When I tried to I got an error message. I don't remember exactly what the error message was, but it said something about the permissions of files in my .ssh directory not being secure enough. It seemed to me that the permissions of these files must have changed during the transfer from Casey's computer to my own. So, I used <strong>chmod</strong> to change them! Here are the details: </p>
<p><strong>How I used chmod</strong>: <br />
I used the command <code>chmod 700 file</code> for both files within my .ssh directory. The number 7 signifies permission to read, write ane execute. The number 0 specifies no permissions at all. The first digit refers to the user's permissions, the second digit to the 'groups' permissions, and the third to the world. So by specifying 700, I gave the user reading, writing and executing permissions but gave no permissions to anyone else. The next time I tried to log in with "ssh leta@hackers.cool" I was successful! (The <a href="http://en.wikipedia.org/wiki/Chmod">wikipedia page</a> gives a more thorough description of how chmod can be used.)</p>
<h4>Tips about Pip:</h4>
<p>It can actually be super helpful to keep track of any dependencies for a particular project. I realized this when my computer crashed and none of my git repos included any meta-data about their dependencies. In the future I'm going to use this very easy way to keep track of my python dependencies for each project: </p>
<ol>
<li>First, setup a virtual environment for each project, with all of the dependencies installed in this virtual environment. If this is my setup, then <code>pip freeze</code> will produce a list of all the pip packages installed for this project. (Note: without a virtual environment for each project, <code>pip freeze</code> would simply produce a list of ALL dependencies for ALL projects.) </li>
<li>Make a requirements page by typing: <code>pip freeze &gt;&gt; requirements.txt</code>.</li>
<li>Include the requirements page in my remote repo!</li>
<li>When I want to run a project on another computer I can run <code>pip install -r requirements.txt</code> and all of the documented packages will be install by pip! Easy! </li>
</ol>
<h4>Documenting Your Workflow and Backing Things Up</h4>
<p><strong>Include source files in your github repo!</strong>
When my computer crashed and I wanted to write a blog post from Casey's computer, it occured to me how much it sucked that I only saved my html output, and not my source files of my blog. With the help of some awesome hackerschoolers, I was able to retreive some old files and get things back up and running. But, the whole process would have been a lot smoother had I simply included my markdown files in my github repo! </p>
<p><strong>Add a README with your workflow!</strong>
Once I had Casey's computer all set up for me to work on my blog, I added a README file to my github repo, speciying my workflow. This ended up helping me dramatically once I got my own computer back. Sometimes in the moment I think "oh, I'll remember my workflow". But maybe I won't! It's worth taking a minute to include a readme with your workflow, in case you don't remmeber it. :) </p>
<p>And generally.... <strong>Back things up.</strong></p>
<div class="footnote">
<hr />
<ol>
<li id="fn:1">
<p>The superuser is a fairly new concept to me, but this is what I understand so far: A superuser is a user on a computer that has all the special privileges used for system administration. Different operating system's have different names for the superuser. This is not a user that you create yourself on your computer, it is there without you doing anything.&#160;<a class="footnote-backref" href="#fnref:1" rev="footnote" title="Jump back to footnote 1 in the text">&#8617;</a></p>
</li>
</ol>
</div></summary><category term="Bash"></category><category term="Documentation"></category></entry><entry><title>Scopes got me again!</title><link href="http://lmontopo.github.io/scopes-got-me-again.html" rel="alternate"></link><updated>2014-12-15T00:00:00-05:00</updated><author><name>Leta Montopoli</name></author><id>tag:lmontopo.github.io,2014-12-15:scopes-got-me-again.html</id><summary type="html"><p>In a previous blog post I wrote about some of the struggles I encountered with scoping when Margo and I worked on our web framework Chapeau. This week, scopes got me again! This time, while I was working on my Template Engine. </p>
<p>I think that, as a new programmer, its not totally surprising that I've been a bit naive and carefree about scopes and global variables. However, I think that making mistakes was a good way for me to learn. Having been tricked by scoping issues twice now, I'll be much more careful with these matters in the future. </p>
<p>In this blog post I'm going to present a peice of code I wrote as an example. It illustrates in a straightforward manner the issues I was facing with my Template Engine. After presenting the code, I'll explain what is problematic about it. I'll present some examples tests I wrote for it, I'll show the weird things that the tests were doing, and I'll explain what I initially thought was happening and what I learned was <em>actually</em> happening. </p>
<p>Here's the example file which I've named string_maker.py</p>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
2
3
4
5
6
7
8
9
10
11</pre></div></td><td class="code"><div class="highlight"><pre><span class="k">class</span> <span class="nc">HTMLString</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span> <span class="o">=</span> <span class="s">&quot;&quot;</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">value</span>
<span class="k">def</span> <span class="nf">update</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">html_text</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">+</span> <span class="n">html_text</span>
<span class="n">my_HTML</span> <span class="o">=</span> <span class="n">HTMLString</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">add_to_string</span><span class="p">(</span><span class="n">text</span><span class="p">):</span>
<span class="n">my_HTML</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">text</span><span class="p">))</span>
</pre></div>
</td></tr></table>
<p>First the class HTMLString is defined with attribute 'value', initially set to be the empty string. This class also contains a method, update, which adds an inputed string to the end of value. An instance of this class called my_HTML is created and a function, 'add_to_string()', is defined. This function basically turns some input into a string format and calls my_HTML's update method on that string. </p>
<p>This code is pretty simple and it does a satisfactory job of concatinating more characters to the end of an existing string. But notice that only one string is ever being added to. Indeed, we only have one instance of the StringHTML class. If we wanted this program to produce two different strings, well it can't (or at least I don't think it can in its current state). This isn't really an issue unless, say, this code was part of a larger program intended to work as a template engine... So consider for a moment that the strings we are creating in this program are HTML pages. Perhaps these webpages will be sent back to a web-app which wants multiple web-pages. If more than one HTML page is being created, then our program would have to terminate, and then start up again, in between HTML pages. That's not really ideal. </p>
<p>Since my Template Engine has code that is similar to string_maker.py, we can already can see that I wasn't going about things in the smartest way. I did realize this at some point but I wanted to get some working tests up and running before improving my code.mSo lets write some tests for string_maker.py! I started with this:</p>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5
6
7
8</pre></div></td><td class="code"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">string_maker</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">import</span> <span class="nn">unittest</span>
<span class="k">class</span> <span class="nc">TestMain</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">test_simple</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">add_to_string</span><span class="p">(</span><span class="s">&#39;&lt;html&gt;Hey!&lt;/html&gt;&#39;</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">my_HTML</span><span class="o">.</span><span class="n">value</span><span class="p">,</span> <span class="s">&#39;&lt;html&gt;Hey!&lt;/html&gt;&#39;</span><span class="p">)</span>
</pre></div>
</td></tr></table>
<p>And received this lovely little message back:</p>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3</pre></div></td><td class="code"><div class="highlight"><pre>Ran 1 test in 0.000s
OK
</pre></div>
</td></tr></table>
<p>Gotta love that!</p>
<p>Now lets add another test function to this class, and we'll call add_to_string() multiple times in this test to make sure that it will keep updating as we expect. Here's what we'll add:</p>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5</pre></div></td><td class="code"><div class="highlight"><pre> <span class="k">def</span> <span class="nf">test_call_multiple</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">add_to_string</span><span class="p">(</span><span class="s">&#39;&lt;html&gt;One&#39;</span><span class="p">)</span>
<span class="n">add_to_string</span><span class="p">(</span><span class="s">&#39;two&#39;</span><span class="p">)</span>
<span class="n">add_to_string</span><span class="p">(</span><span class="s">&#39;three&lt;/html&gt;&#39;</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">my_HTML</span><span class="o">.</span><span class="n">value</span><span class="p">,</span> <span class="s">&#39;&lt;html&gt;Onetwothree&lt;/html&gt;&#39;</span><span class="p">)</span>
</pre></div>
</td></tr></table>
<p>At this point I expect an error. Afterall, I know that string_maker.py can only create one html page at a time. So lets run the test and see. </p>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
2
3
4
5
6
7
8
9
10
11
12
13
14</pre></div></td><td class="code"><div class="highlight"><pre>$ python test.py
.F
======================================================================
FAIL: test_class (__main__.TestMain)
----------------------------------------------------------------------
Traceback (most recent call last):
File &quot;test.py&quot;, line 8, in test_class
self.assertEqual(my_HTML.value, &#39;<span class="nt">&lt;html&gt;</span>Hey!<span class="nt">&lt;/html&gt;</span>&#39;)
AssertionError: &#39;<span class="nt">&lt;html&gt;</span>Onetwothree<span class="nt">&lt;/html&gt;&lt;html&gt;</span>Hey!<span class="nt">&lt;/html&gt;</span>&#39; != &#39;<span class="nt">&lt;html&gt;</span>Hey!<span class="nt">&lt;/html&gt;</span>&#39;
----------------------------------------------------------------------
Ran 2 tests in 0.000s
FAILED (failures=1)
</pre></div>
</td></tr></table>
<p>A single error, as expected. What I didn't expect was for the first test, test_simple, to be the failing test! This lead to a nice lesson about Python Unittests: <strong>Tests do not run in the order that you declare them</strong>. Cool! </p>
<p>At this point I get all naive and think "OK, I can fix this, here's how":</p>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16</pre></div></td><td class="code"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">string_maker</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">import</span> <span class="nn">unittest</span>
<span class="k">class</span> <span class="nc">TestMain</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">test_simple</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">my_HTML</span> <span class="o">=</span> <span class="n">HTMLString</span><span class="p">()</span>
<span class="n">add_to_string</span><span class="p">(</span><span class="s">&#39;&lt;html&gt;Hey!&lt;/html&gt;&#39;</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">my_HTML</span><span class="o">.</span><span class="n">value</span><span class="p">,</span> <span class="s">&#39;&lt;html&gt;Hey!&lt;/html&gt;&#39;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">test_call_multiple</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">my_HTML</span> <span class="o">=</span> <span class="n">HTMLString</span><span class="p">()</span>
<span class="n">add_to_string</span><span class="p">(</span><span class="s">&#39;&lt;html&gt;One&#39;</span><span class="p">)</span>
<span class="n">add_to_string</span><span class="p">(</span><span class="s">&#39;two&#39;</span><span class="p">)</span>
<span class="n">add_to_string</span><span class="p">(</span><span class="s">&#39;three&lt;/html&gt;&#39;</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">my_HTML</span><span class="o">.</span><span class="n">value</span><span class="p">,</span> <span class="s">&#39;&lt;html&gt;Onetwothree&lt;/html&gt;&#39;</span><span class="p">)</span>
</pre></div>
</td></tr></table>
<p>I figured, if I just re-instantiate my class inside each function, then my_HTML.value will be whiped clean at the begining of each test. Problem solved, right? Wrong....</p>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22</pre></div></td><td class="code"><div class="highlight"><pre><span class="err">$</span> <span class="n">python</span> <span class="n">test</span><span class="o">.</span><span class="n">py</span>
<span class="n">FF</span>
<span class="o">======================================================================</span>
<span class="n">FAIL</span><span class="p">:</span> <span class="n">test_call_multiple</span> <span class="p">(</span><span class="n">__main__</span><span class="o">.</span><span class="n">TestMain</span><span class="p">)</span>
<span class="o">----------------------------------------------------------------------</span>
<span class="n">Traceback</span> <span class="p">(</span><span class="n">most</span> <span class="n">recent</span> <span class="n">call</span> <span class="n">last</span><span class="p">):</span>
<span class="n">File</span> <span class="s">&quot;test.py&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">16</span><span class="p">,</span> <span class="ow">in</span> <span class="n">test_call_multiple</span>
<span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">my_HTML</span><span class="o">.</span><span class="n">value</span><span class="p">,</span> <span class="s">&#39;&lt;html&gt;Onetwothree&lt;/html&gt;&#39;</span><span class="p">)</span>
<span class="ne">AssertionError</span><span class="p">:</span> <span class="s">&#39;&#39;</span> <span class="o">!=</span> <span class="s">&#39;&lt;html&gt;Onetwothree&lt;/html&gt;&#39;</span>
<span class="o">======================================================================</span>
<span class="n">FAIL</span><span class="p">:</span> <span class="n">test_class</span> <span class="p">(</span><span class="n">__main__</span><span class="o">.</span><span class="n">TestMain</span><span class="p">)</span>
<span class="o">----------------------------------------------------------------------</span>
<span class="n">Traceback</span> <span class="p">(</span><span class="n">most</span> <span class="n">recent</span> <span class="n">call</span> <span class="n">last</span><span class="p">):</span>
<span class="n">File</span> <span class="s">&quot;test.py&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">9</span><span class="p">,</span> <span class="ow">in</span> <span class="n">test_class</span>
<span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">my_HTML</span><span class="o">.</span><span class="n">value</span><span class="p">,</span> <span class="s">&#39;&lt;html&gt;Hey!&lt;/html&gt;&#39;</span><span class="p">)</span>
<span class="ne">AssertionError</span><span class="p">:</span> <span class="s">&#39;&#39;</span> <span class="o">!=</span> <span class="s">&#39;&lt;html&gt;Hey!&lt;/html&gt;&#39;</span>
<span class="o">----------------------------------------------------------------------</span>
<span class="n">Ran</span> <span class="mi">2</span> <span class="n">tests</span> <span class="ow">in</span> <span class="mf">0.000</span><span class="n">s</span>
<span class="n">FAILED</span> <span class="p">(</span><span class="n">failures</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
</pre></div>
</td></tr></table>
<p>AHHH!! WHAT IS HAPPENING!? My instinct was to think that UnitTests just work in reallly wierd ways that I don't understand. So I figured I experimented. </p>
<p>The next thing I tried was this: Instead of re-instantiating HTMLString inside each test function I defined one of UnitTest's magic setUp functions. When running unit tests, if you define a function called <code>setUp()</code>, then Python will know to run this function before every other test funciton. So I made a setUp function to perform <code>my_HTML = HTMLString()</code>. And then I got the same errors as I did when I never re-instantiated at all. Its like my program didn't even run my setUp function at all! I tried doing the same thing but with a tearDown() function, and again had no luck. </p>
<p>I was so confused. I got my awesome friend <a href="http://programmingforwitches.tumblr.com">Amanda</a>'s help, and she suggested that, instead of writing <code>from string_maker import *</code>, that I just import the functions I needed to run my code. This was a good idea, but it didn't work either. Amanda was awesome though, because she asked me all sorts of questions, and those questions helped me realize what I was doing wrong! Here's what I learned:</p>
<p>When I re-instantiate the <code>my_HTML = HTMLString()</code> inside each of my test functions, I am creating a local instance of this class. Local, meaning, in the scope of the function. Then I call <code>add_to_string</code> which updates the <em>global</em> instance of my_HTML (ie. the instance of the class that is in my code string_maker.py)
. <strong>These are not the same object!</strong> So I update the global instance, and then assertEqual on the local instance, which of course still have an empty value string. </p>
<p>If this isn't making sense, remember that when you create a function you create a scope for that function. When a function encounters a name its not familiar with, it will first check within its own scope, ie within its own definition, to see if there is anything by that name defined there. If there isn't, then it will look for things outside of its own scope. As soon as it finds something, it uses it. </p>
<p>Take Away: <strong>You can have a local variable and a global variable that have the same name, but they are not the same thing!!!!</strong> </p></summary><category term="Python"></category></entry><entry><title>Regular Expressions in Python - with LOTS of Examples!</title><link href="http://lmontopo.github.io/regular-expressions-in-python-with-lots-of-examples.html" rel="alternate"></link><updated>2014-12-04T05:00:00-05:00</updated><author><name>Leta Montopoli</name></author><id>tag:lmontopo.github.io,2014-12-04:regular-expressions-in-python-with-lots-of-examples.html</id><summary type="html"><p>Over the past several days I've learned a great deal about regular
expressions. I struggled initially with the subject, but I think things
have finally clicked. So, I'm going to write what I've learned! In this
blog post I am going to:</p>
<ul>
<li>Explain what a regular expression is.</li>
<li>Introduce <code>re.split()</code> and use it to provide examples.</li>
<li>Explain some of the ways that the star, dot, and question mark can
be used in a regular expression.</li>
<li>Explain, in detail, the plague of the backslash.</li>
</ul>
<p>As I learned about regular expressions, I found examples to be
enourmously helpful. For this reason, there will be a TON of examples
throughout this blogpost.</p>
<h4>What is a regular expression?</h4>
<p>A regular expression is sequence of characters that represent a pattern
we would like to search for within a larger chunk of text. There are two
types of characters within a regular expression: ones that represent
themselves, called <strong>ordinary characters</strong>, and one's that don't
represent themselves, called <strong>special characters</strong>. Most common
characters, like the letter 'a', match to themselves in a python regular
expression.</p>
<h4>Introducing the split function:</h4>
<p>To use regular expression in Python you need to <code>import re</code>. This module
gives you access to many different methods. The ones I use the most are
<code>re.split(pattern, string)</code> and <code>re.match(patter, string)</code>. I find
<code>re.split()</code> easiest to understand, so we'll start there. We'll visit
<code>re.search()</code> later.</p>
<p>The <code>re.split(pattern, string)</code> is very similar to python's built in
split method for strings. It splits the inputed string over the pattern
expressed by the regular expression. By default, <code>re.split()</code> will
return a list of all the parts of the string, excluding the parts that
matched the pattern. If you would like the resulting list to include the
sections which match to the pattern then parenthesis are needed around
the regular expression:</p>
<h5>Example 1: Introductiory Example:</h5>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5
6</pre></div></td><td class="code"><div class="highlight"><pre><span class="o">&gt;&gt;&gt;</span> <span class="kn">import</span> <span class="nn">re</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">text</span> <span class="o">=</span> <span class="s">&#39;Hackerschool is cool.&#39;</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;a&quot;</span><span class="p">,</span> <span class="n">text</span><span class="p">)</span>
<span class="p">[</span><span class="s">&#39;H&#39;</span><span class="p">,</span> <span class="s">&#39;ckerschool is cool.&#39;</span><span class="p">]</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;(a)&quot;</span><span class="p">,</span> <span class="n">text</span><span class="p">)</span>
<span class="p">[</span><span class="s">&#39;H&#39;</span><span class="p">,</span> <span class="s">&#39;a&#39;</span><span class="p">,</span> <span class="s">&#39;ckerschool is cool.&#39;</span><span class="p">]</span>
</pre></div>
</td></tr></table>
<p>As you can see, when we put parentheses around 'a', then 'a' is included
in the resulting list. But perhaps we have a pattern that is more than
one character long, and we want only part of the pattern to be kept in
the array. To do this brackets are put around the part of the regular
expression we want to keep. Check it out:</p>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2</pre></div></td><td class="code"><div class="highlight"><pre><span class="o">&gt;&gt;&gt;</span> <span class="n">re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;a(ck)&quot;</span><span class="p">,</span> <span class="n">text</span><span class="p">)</span>
<span class="p">[</span><span class="s">&#39;H&#39;</span><span class="p">,</span><span class="s">&#39;ck&#39;</span><span class="p">,</span> <span class="s">&#39;erschool is cool.&#39;</span><span class="p">]</span>
</pre></div>
</td></tr></table>
<p>Here the 'a' is still left out because it wasn't included in the
parentheses!</p>
<h4>Special Characters Star, Period and Question Mark:</h4>
<p>We're going to use <code>re.split()</code> to explore what these special symbols
match to in a regular expression.</p>
<h5>Example 2: The period:</h5>
<p>According to Python's regular expression documentation, the period
matches any character except a newline.</p>
<p>so as you'd expect it is going to split on every character...</p>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5
6
7
8
9</pre></div></td><td class="code"><div class="highlight"><pre><span class="o">&gt;&gt;&gt;</span> <span class="n">text</span> <span class="o">=</span> <span class="s">&quot;Hackerschool is cool!&quot;</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;(.)&quot;</span><span class="p">,</span> <span class="n">text</span><span class="p">)</span>
<span class="p">[</span><span class="s">&#39;&#39;</span><span class="p">,</span> <span class="s">&#39;H&#39;</span><span class="p">,</span>
<span class="s">&#39;&#39;</span><span class="p">,</span> <span class="s">&#39;a&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">,</span> <span class="s">&#39;c&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">,</span> <span class="s">&#39;k&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">,</span> <span class="s">&#39;e&#39;</span><span class="p">,</span>
<span class="s">&#39;&#39;</span><span class="p">,</span> <span class="s">&#39;r&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">,</span> <span class="s">&#39;s&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">,</span> <span class="s">&#39;c&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">,</span> <span class="s">&#39;h&#39;</span><span class="p">,</span>
<span class="s">&#39;&#39;</span><span class="p">,</span> <span class="s">&#39;o&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">,</span> <span class="s">&#39;o&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">,</span> <span class="s">&#39;l&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">,</span> <span class="s">&#39; &#39;</span><span class="p">,</span>
<span class="s">&#39;&#39;</span><span class="p">,</span> <span class="s">&#39;i&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">,</span> <span class="s">&#39;s&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">,</span> <span class="s">&#39; &#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">,</span> <span class="s">&#39;c&#39;</span><span class="p">,</span>
<span class="s">&#39;&#39;</span><span class="p">,</span> <span class="s">&#39;o&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">,</span> <span class="s">&#39;o&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">,</span> <span class="s">&#39;l&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">,</span> <span class="s">&#39;!&#39;</span><span class="p">,</span>
<span class="s">&#39;&#39;</span><span class="p">]</span>
</pre></div>
</td></tr></table>
<p>It might be surprising to you that there are empty string elements
between each character. I was surprised! I don't really have a good
explanation for why this happens except to say that this is just the way
the split function works. It finds the pattern we are searching for, and
then says "OK, on the left side of this pattern instance we have (fill
in the blank) and on the right side we have (fill in the blank)". In our
example, since every character is an instance of the pattern, on either
side of each pattern instance there is only the empty string. We'll see
another occurence of this behaviour in a later example. If it doesn't
make sense to you now, hopefully it will then.</p>
<p>Now lets combine the dot with other characters:</p>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2</pre></div></td><td class="code"><div class="highlight"><pre><span class="o">&gt;&gt;&gt;</span> <span class="n">re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;(.ool)&quot;</span><span class="p">,</span> <span class="n">text</span><span class="p">)</span>
<span class="p">[</span><span class="s">&#39;Hackersc&#39;</span><span class="p">,</span> <span class="s">&#39;hool&#39;</span><span class="p">,</span> <span class="s">&#39; is &#39;</span><span class="p">,</span> <span class="s">&#39;cool&#39;</span><span class="p">,</span> <span class="s">&#39;!&#39;</span><span class="p">]</span>
</pre></div>
</td></tr></table>
<p>This makes sense - there were two occurences where there appeared "some
character followed by 'ool'".</p>
<p>Lets try a couple more examples just to make sure we get the hang of it:</p>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4</pre></div></td><td class="code"><div class="highlight"><pre><span class="o">&gt;&gt;&gt;</span> <span class="n">re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;(c.o)&quot;</span><span class="p">,</span> <span class="n">text</span><span class="p">)</span>
<span class="p">[</span><span class="s">&#39;Hackers&#39;</span><span class="p">,</span> <span class="s">&#39;cho&#39;</span><span class="p">,</span> <span class="s">&#39;ol is &#39;</span><span class="p">,</span> <span class="s">&#39;coo&#39;</span><span class="p">,</span> <span class="s">&#39;l!&#39;</span><span class="p">]</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;(c.h)&quot;</span><span class="p">,</span> <span class="n">text</span><span class="p">)</span>
<span class="p">[</span><span class="s">&#39;Hackerschool is cool!&#39;</span><span class="p">]</span>
</pre></div>
</td></tr></table>
<p>The first time we split over 'cho' and 'coo' since both are instances of
the pattern "'c' followed by some character followed by 'o'". The second
time we don't split the expression at all. Even though 'ch' is in
school, there is no character between the 'c' and the 'h'. So 'ch'
doesn't match to the regular expression 'c.h'. The lesson here is that
<strong>'.' on its own matches to exactly one instance of any character</strong>. Not
zero, not two, one.</p>
<h5>Example 3: The period and the star.</h5>
<p>The star represents 0 or more repititions of the previous character
expressed in the regular expression. Whenever possible, the star will
'suck up' as much as it can. Because of this behaviour, star is called a
<strong>greedy character</strong>.</p>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5
6</pre></div></td><td class="code"><div class="highlight"><pre><span class="o">&gt;&gt;&gt;</span> <span class="n">text</span> <span class="o">=</span> <span class="s">&quot;I like reading scify books.&quot;</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;(s.i)&quot;</span><span class="p">,</span> <span class="n">text</span><span class="p">)</span>
<span class="p">[</span><span class="s">&#39;I like reading &#39;</span><span class="p">,</span> <span class="s">&#39;sci&#39;</span><span class="p">,</span> <span class="s">&#39;fi books.&#39;</span><span class="p">]</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;(s.*i)&quot;</span><span class="p">,</span> <span class="n">text</span><span class="p">)</span>
<span class="p">[</span><span class="s">&#39;I like reading &#39;</span><span class="p">,</span> <span class="s">&#39;scifi&#39;</span><span class="p">,</span> <span class="n">books</span><span class="o">.</span><span class="s">&#39;]</span>
<span class="o">&gt;&gt;&gt;</span>
</pre></div>
</td></tr></table>
<p>Without the star, the regular expression 's.i' will match to 'sci'. When
the star is added the regular expression matches to more stuff! The
expression 's.*i' matches to 'scifi' because the star indicates that
the dot can be repeated 0 or more times. Because the star is greedy it
soaks up as many repetitions as possible.</p>
<p>Lets see what happens when we change the text to "I like science
books.".</p>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3</pre></div></td><td class="code"><div class="highlight"><pre><span class="o">&gt;&gt;&gt;</span> <span class="n">text</span> <span class="o">=</span> <span class="s">&quot;I like science books.&quot;</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;(s.*i)&quot;</span><span class="p">,</span> <span class="n">text</span><span class="p">)</span>
<span class="p">[</span><span class="s">&#39;I like &#39;</span><span class="p">,</span> <span class="s">&#39;sci&#39;</span><span class="p">,</span> <span class="s">&#39;ence books.&#39;</span><span class="p">]</span>
</pre></div>
</td></tr></table>
<p>Now that there is only one 'i' in the text, there are no more characters
that the star can suck up, so, "s.*i" matches to 'sci'.</p>
<h5>Example 4: The Question Mark</h5>
<p>The question matches either 0 or 1 repetitions of the preceeding regular
expression. Here are some examples:</p>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5
6</pre></div></td><td class="code"><div class="highlight"><pre><span class="o">&gt;&gt;&gt;</span> <span class="n">text</span> <span class="o">=</span> <span class="s">&quot;There are num sections in this document.&quot;</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;(e.*)&quot;</span><span class="p">,</span> <span class="n">text</span><span class="p">)</span>
<span class="p">[</span><span class="s">&#39;Th&#39;</span><span class="p">,</span> <span class="s">&#39;ere are num sections in this document.&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">]</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;(e.?)&quot;</span><span class="p">,</span> <span class="n">text</span><span class="p">)</span>
<span class="p">[</span><span class="s">&#39;Th&#39;</span><span class="p">,</span> <span class="s">&#39;er&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">,</span><span class="s">&#39;e &#39;</span><span class="p">,</span> <span class="s">&#39;ar&#39;</span><span class="p">,</span> <span class="s">&#39;e &#39;</span><span class="p">,</span> <span class="s">&#39;num s&#39;</span><span class="p">,</span> <span class="s">&#39;ec&#39;</span><span class="p">,</span> <span class="s">&#39;tions in this docum&#39;</span><span class="p">,</span> <span class="s">&#39;en&#39;</span><span class="p">,</span> <span class="s">&#39;t.&#39;</span><span class="p">]</span>
<span class="o">&gt;&gt;&gt;</span>
</pre></div>
</td></tr></table>
<p>When we use the question mark instead of the star there are many more
matches to the regular expression! The question mark is basically the
oposite of a greedy character.</p>
<p>Also, notice the occurence of an empty string between the 'er' and the
'e'. Since we are splitting over the 'er' and the 'e' the split
functions wants to put something on either side of these that isn't
something else we're splitting over. The only thing between the 'er' and
the 'e' is the empty string. Hopefully this examples helps to understand
the whole empty string phenomenon.</p>
<h5>Example 5: Combining the dot, star, and question mark:</h5>
<p>Lets see what happens when we combine all three: the dot, the star, and
the question mark.</p>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5</pre></div></td><td class="code"><div class="highlight"><pre><span class="o">&gt;&gt;&gt;</span> <span class="n">text</span> <span class="o">=</span> <span class="s">&quot;There are num sections in this document.&quot;</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;(e.*?)&quot;</span><span class="p">,</span> <span class="n">text</span><span class="p">)</span>
<span class="p">[</span><span class="s">&#39;Th&#39;</span><span class="p">,</span> <span class="s">&#39;e&#39;</span><span class="p">,</span> <span class="s">&#39;r&#39;</span><span class="p">,</span> <span class="s">&#39;e&#39;</span><span class="p">,</span> <span class="s">&#39;ar&#39;</span><span class="p">,</span> <span class="s">&#39;e&#39;</span><span class="p">,</span> <span class="s">&#39; num s&#39;</span><span class="p">,</span> <span class="s">&#39;e&#39;</span><span class="p">,</span>
<span class="s">&#39;ctions in this docum&#39;</span><span class="p">,</span> <span class="s">&#39;e&#39;</span><span class="p">,</span> <span class="s">&#39;nt.&#39;</span><span class="p">]</span>
<span class="o">&gt;&gt;&gt;</span>
</pre></div>
</td></tr></table>
<p>Placing the question mark after a the star makes the regular expression
match to the minimal number of characters possible. We'll see this more
in the next example...</p>
<h5>Example #4: Application to my Template Engine:</h5>
<p>I have been working on writing my own template engine. In my language,
variables will be contained within double curly braces. I used regular
expressions to parse the html template and find all of the variables.
Lets see an example:</p>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3</pre></div></td><td class="code"><div class="highlight"><pre><span class="o">&gt;&gt;&gt;</span> <span class="n">html_text</span> <span class="o">=</span> <span class="s">&quot;&lt;html&gt;&lt;title&gt; Hello {{name}}.&lt;/title&gt; Today is {{day}}.&lt;/html&gt;&quot;</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;({{.*?}})&quot;</span><span class="p">,</span> <span class="n">html_text</span><span class="p">)</span>
<span class="p">[</span><span class="s">&#39;&lt;html&gt;&lt;title&gt; Hello &#39;</span><span class="p">,</span><span class="s">&#39;{{name}}&#39;</span><span class="p">,</span> <span class="s">&#39;.&lt;/title&gt; Today is &#39;</span><span class="p">,</span><span class="s">&#39;{{day}}&#39;</span><span class="p">,</span> <span class="s">&#39;.&lt;/html&gt;&#39;</span><span class="p">]</span>
</pre></div>
</td></tr></table>
<p>Just to make sure we understand what the star and question mark are
doing, lets see what happens when either one is removed:</p>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4</pre></div></td><td class="code"><div class="highlight"><pre><span class="o">&gt;&gt;&gt;</span> <span class="n">re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;({{.*}})&quot;</span><span class="p">,</span> <span class="n">html_text</span><span class="p">)</span>
<span class="p">[</span><span class="s">&#39;&lt;html&gt;&lt;title&gt; Hello &#39;</span><span class="p">,</span> <span class="s">&#39;{{name}}.&lt;/title&gt; Today is {{day}}&#39;</span><span class="p">,</span> <span class="s">&#39;.&lt;/html&gt;&#39;</span><span class="p">]</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;({{.?}})&quot;</span><span class="p">,</span> <span class="n">html_text</span><span class="p">)</span>
<span class="p">[</span><span class="s">&#39;&lt;html&gt;&lt;title&gt; Hello {{name}}.&lt;/title&gt; Today is {{day}}.&lt;/html&gt;&#39;</span><span class="p">]</span>
</pre></div>
</td></tr></table>
<p>When the question mark is removed, the star acts greedily and soaks
everything up until the last occurence of '}}'. When the star is
removed, we have no match to our pattern at all. This is because the
variable names inside the curly braces are more than 1 character long.</p>
<h4>The plague of the backslash:</h4>
<p>Now we have a handle on those special functions, lets explore the ...
dun dun dun... THE BACKSLASH!</p>
<p>Beleive it or not, the regular expression for a backslash is FOUR
backslashes. Woah. Lets walk through why this is the case:</p>
<p>In python, a backslash is a special character, and so to represent a
backslash we actually need to use two backslashes. So in python two
backslashes represents one. I'm not even talking about regular
expressions yet, I'm just talking about python strings. Lets verify
this:</p>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5</pre></div></td><td class="code"><div class="highlight"><pre><span class="o">&gt;&gt;&gt;</span> <span class="n">backslash</span> <span class="o">=</span> <span class="s">&quot;</span><span class="se">\\</span><span class="s">&quot;</span>
<span class="o">&gt;&gt;&gt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">backslash</span><span class="p">)</span>
<span class="mi">1</span>
<span class="o">&gt;&gt;&gt;</span> <span class="k">print</span> <span class="n">backslash</span>
\
</pre></div>
</td></tr></table>
<p>Neat!</p>
<p>Ok, so this means that we actually have to write a regular expression
which will match to two backslashes instead of to a single backslash.
Now, in regular expression land, backlash is one of those special
characters we talked about previously. They don't match to themselves.
The regular expression for a backslash is also two backslashes. So,
since we want to match to the python string <code>\\</code> the regular expression
to do so becomes <code>\\\\</code>.</p>
<p>I hope you've enjoyed this introduction to regular expression in Python.
If anything is confusing, don't hesitate to contact me!</p></summary><category term="Regular Expressions"></category><category term="Python"></category></entry><entry><title>I made my first cookie!</title><link href="http://lmontopo.github.io/i-made-my-first-cookie.html" rel="alternate"></link><updated>2014-11-27T05:00:00-05:00</updated><author><name>Leta Montopoli</name></author><id>tag:lmontopo.github.io,2014-11-27:i-made-my-first-cookie.html</id><summary type="html"><p>Margo and I have been working on our web-framework, Chapeau. To test out
its functionality and user-friendliness, we've been using Chapeau to
build various web-apps. This week we implimented a buzz-feed-style quiz.
To challenge ourselves (and Chapeau) we structured the app to have a
separate webpage for each question, and then a final webpage displaying
the result. This forced us to address the issues: 'Does Chapeau allow
data to be transfered between non-adjacent webpages?' and 'Does Chapeau
allow for the data to be accessed directly and used in a calculation'?
As we tried to answer these questions and broaden the functionality of
Chapeau, the framework took on several variations. In this blog post
I'll begin by describing how Chapeau works for very simple web-apps, and
then I'll continue by explaining some of the changes we made to Chapeau
as we worked towards our buzz-feed-style quiz.</p>
<h4>Simple Web Apps</h4>
<p>Chapeau's design makes very simple web-apps just that: very simple. To
make such an app, a developer needs only to define a dicitonary matching
URL's to their corresponding html pages. Then calling Chapeau's 'go'
function on the dictionary gets everything going. Pretty simple right?
Beleive it or not, with Chapeau, an app this simple can even pass
variables from an html form to the webpage that follows. The app itself
(by 'app' I mean the python code) does not need to be changed to include
this feature. Adding very basic variable passing to a Chapeau app is
just a matter of formatting your html pages accordingly. Here's how to
do it:</p>
<ul>
<li>The form must specify either 'method = get' or 'method = post',
since Chapeau deals only with post and get requests.</li>
<li>The form's action needs to specify the URL that this form redirects
to.</li>
<li>The html page corresponding to this URL should have '%(key)s'
wherever this key's value is meant to appear.</li>
</ul>
<p>How is this all working? In the background Chapeau parses the request
made from the form's submission. Chapeau finds the user's input in this
request and stores it in a dictionary of key-value pairs called params.
The specified html file is then read in as a string with '%params'
tacked on to the end. Python's built in string formatting takes care of
the rest, and the page renders with the desired arguments! Easy-peasy.</p>
<p>But what if we wanted to mutate the user input in some way? Or what if
we wanted to <em>use</em> the input to perform some sort of analysis or
calculation? Perhaps we want to create some results for a quiz? Our
initial version of Chapeau could not handle such situations.</p>
<h4>Adding Functions</h4>
<p>To extend Chapeau's usability Margo and I decided that web developers
would have the choice to route a URL to a path or to a function. If a
URL is routed to a function then this function would have a few
structural constraints. Initially, these were the constraints we
implimented:</p>
<ul>
<li>Chapeau will be passing a dictionary into the function, so the
function must be defined to accept it. This dictionary will contain
some of the request information and will look something like:
'{'type': 'get', 'query': 'query_string', 'path': 'url' , 'body':
'user_input_from_post_form'}'. (Basically this dictionary stores
all of the request information except for the headers.)</li>
<li>The function must return two objects: a path and a dictionary. The
path will specify the next html page to be rendered and the
dictionary will be the same one that the function receives, but
possibly with some mutated data.</li>
</ul>
<p>The dictionary that the function takes in gives the developer access to
the user input that was just aquired. If the developer wanted to mutate
this user input in anyway, they could do so. Alternatively, if the
developper wants user input to be saved so that it can be obtained
later, Margo and I figured this could be done as well. The developper
could create a global dictionary that they can add to when user input is
submitted, and that they could read from later. Problem solved. Except
not really.</p>
<p>More experienced developpers might already see the problem we've
created. Consider the buzz-feed-style quiz that Margo and I wanted to
create. Suppose that we implimented this app by storing the user input
in a global dictionary. This dictionary is updated after every question
is answered. When the results page needs to be rendered all of the user
input is obtained from the global dictionary. But suppose two clients,
client1 and client2, are taking the quiz simultaneously. Client1 inputs
their answer to question 1 first, and chooses 'grapes'. Before client1
finishes the quiz client2 submits their answer to question 1 as
'oranges'. When our app goes to fetch the results of client1, their
'grapes' answer will have been overwritten by 'oranges'. Afterall, we've
only implimented one global dictionary for our entire client base. Oups.</p>
<h4>Finally, adding Cookies!</h4>
<p>At this point Margo and I weren't really sure how to solve this issue.
How do we keep track of which answer corresponds to which client? For
advice, we turned to our trusted friend
<a href="http://www.greghendershott.com">Greg</a> who suggested we use cookies!</p>
<p><strong>What is a cookie, and how do I make one?</strong></p>
<p>A cookie is a very small peice of data that will be sent from a server
and stored temporarily on a client's computer. To create a cookie, the
server needs to include a 'Set-Cookie' header in their HTTP response to
the client. This will prompt the client's browser to create a 'cookie'
to store the specified data. When a browser has a cookie from our server
any HTTP request it sends to us will include a 'Cookie' header
containing the data we asked it to store.</p>
<p><strong>How does this help our situation?</strong></p>
<p>Margo and I used cookies to store every quiz answer submited from a
client. Then, when the client's browser sends our server an HTTP request
asking for their results page, this request will include all of that
client's quiz answers - stored in the form of cookies! Great!</p>
<p>Now lets talk about how we implimented this. To create cookies Margo and
I changed the constraints of our functions (the ones that URL's are
routed to) as follows:</p>
<ul>
<li>Chapeau will now pass to these functions both the dictionary
containing the request information and a client object. So these
functions must now accept two parameters! The dicitonary accepted
contains everything the previous one does, but also includes
'{header: {all of the headers and their values}}'.</li>
<li>The functions must call Chapeau's render function: render(client,
path/to/html/file, args, header) before returning None.</li>
</ul>
<p>Now that <em>all</em> of the request information is passed to the developper,
cookies can be read. Since the developer is now also in charge of
calling the render function, they have control over any headers they
want to include in their response. The header parameter in the render
function is optional, and when unspecified it will be set to None. The
args parameter is a dictionary of variables that we want to pass to the
html file. So, in our buzz-feed-style-quiz, every client's answer will
be saved in a cookie. When we want to render the results page all of the
incoming cookies can be read from the inputed dictionary of request
information. We can create another dictionary out of the received
cookies and can pass this dictionary into our render function to be
passed to our html file. Yay!</p>
<p>Thanks Margo for working on this project with me, its been an adventure!</p></summary><category term="Web"></category></entry><entry><title>Lisp Interpreter: Episode 3 (The Finally!) ... Continued.</title><link href="http://lmontopo.github.io/lisp-interpreter-episode-3-the-finally-continued.html" rel="alternate"></link><updated>2014-11-24T05:00:00-05:00</updated><author><name>Leta Montopoli</name></author><id>tag:lmontopo.github.io,2014-11-24:lisp-interpreter-episode-3-the-finally-continued.html</id><summary type="html"><p>In last week's episode we inspected how my program tokenizes and parses
the user's input. In the spirit of finishing this series of blog posts,
I have decided today to present an overview of how the rest of my
program works. We'll walk through the basic algorithm that my program
follows, pausing at some of the more exciting and important parts. I'll
do my best to skip over the less exciting bits of code, while still
providing enough information to convey the general ideas. If anyone
would like to see the code in its entirety, feel free to check it out on
my github account: <a href="https://github.com/lmontopo/Lispeter">https://github.com/lmontopo/Lispeter</a>. As always, I
welcome any questions or comments about either this blog post or the
code i'm describing. Lets begin!</p>
<p>After the user's Scheme input is parsed and tokenzied, it is passed to a
function which 'unwraps' it. This funciton is called 'outer_evaluate'.
Remember how, in the tokenzier, the input gets wrapped in an extra set
of parenthesis? My 'outer_evaluate' function is how I dealt with this.
It evaluates from left to right each of the internal expressions within
the parsed expression and then it spit out the result of the last
expression. Here's the function:</p>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5</pre></div></td><td class="code"><div class="highlight"><pre><span class="k">def</span> <span class="nf">outter_evaluate</span><span class="p">(</span><span class="n">list_input</span><span class="p">,</span><span class="n">env</span><span class="p">):</span>
<span class="n">evaluated_list</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">expression</span> <span class="ow">in</span> <span class="n">list_input</span><span class="p">:</span>
<span class="n">evaluated_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">evaluate</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">env</span><span class="p">))</span>
<span class="k">return</span> <span class="n">evaluated_list</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
</pre></div>
</td></tr></table>
<p>Here, 'evaluate' is the main function that interprets the parsed input.
Inside 'evaluate' the input is classified as either a list or an atom
and is passed to the 'is_cons' and 'is_atom' functions respectively.
The 'is_atom' function is pretty straightforward since atoms are
self-evaluating, but I'll take a bit of time to describe how the
'is_cons' function works.</p>
<p>When 'is_cons' is called it inspects the first element of the list that
is inputed. Assuming that our input is a valid Scheme expression, we
expect the first item in this list to be a function. 'is_cons' will
check to see if this function is in our pre-defined list of special
functions. (Recall that special functions are the ones that change the
flow of the interpretation.) Once the program has decided if the
function is special or not, input is directed to either the
'is_special' or 'is_regular' function, whichever is appropriate. Lets
talk a bit about both of these functions.</p>
<p>The 'is_special' function consists of a case by case evaluation of what
to do for each special operator. I'll just present a subset of this
function because the whole thing is a little long and overwhelming.
Here's some of it:</p>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35</pre></div></td><td class="code"><div class="highlight"><pre><span class="k">def</span> <span class="nf">call_special</span><span class="p">(</span><span class="n">list_input</span><span class="p">,</span> <span class="n">env</span><span class="p">):</span>
<span class="n">head</span><span class="p">,</span> <span class="n">rest</span> <span class="o">=</span> <span class="n">list_input</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">list_input</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
<span class="k">if</span> <span class="n">head</span> <span class="o">==</span> <span class="s">&quot;map&quot;</span><span class="p">:</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">rest</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
<span class="n">new_head</span><span class="p">,</span> <span class="n">list_to_act_on</span> <span class="o">=</span> <span class="n">rest</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">evaluate</span><span class="p">(</span><span class="n">rest</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">env</span><span class="p">)</span>
<span class="n">new_list</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">list_to_act_on</span><span class="p">:</span>
<span class="n">new_item</span> <span class="o">=</span> <span class="n">interpret</span><span class="p">(</span><span class="s">&quot;(</span><span class="si">%s</span><span class="s"> </span><span class="si">%s</span><span class="s">)&quot;</span> <span class="o">%</span><span class="p">(</span><span class="n">new_head</span><span class="p">,</span> <span class="n">item</span><span class="p">),</span> <span class="n">env</span><span class="p">)</span>
<span class="n">new_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">new_item</span><span class="p">)</span>
<span class="k">return</span> <span class="n">new_list</span>
<span class="k">if</span> <span class="n">head</span> <span class="o">==</span> <span class="s">&#39;define&#39;</span><span class="p">:</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">rest</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
<span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">rest</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="ow">is</span> <span class="nb">str</span><span class="p">:</span>
<span class="n">env</span><span class="o">.</span><span class="n">add_values</span><span class="p">(</span><span class="n">rest</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">rest</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">name</span> <span class="o">=</span> <span class="n">rest</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span>
<span class="n">expression</span> <span class="o">=</span> <span class="n">MakeLambda</span><span class="p">(</span><span class="n">rest</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">1</span><span class="p">:],</span> <span class="n">rest</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span>
<span class="n">env</span><span class="o">.</span><span class="n">add_values</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">expression</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">raise</span> <span class="n">too_many</span>
<span class="k">if</span> <span class="n">head</span> <span class="o">==</span> <span class="s">&#39;lambda&#39;</span><span class="p">:</span>
<span class="n">func</span> <span class="o">=</span> <span class="n">MakeLambda</span><span class="p">(</span><span class="n">rest</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span><span class="n">rest</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span>
<span class="n">env</span><span class="o">.</span><span class="n">add_values</span><span class="p">(</span><span class="s">&#39;lam&#39;</span><span class="p">,</span> <span class="n">func</span><span class="p">)</span>
<span class="k">return</span> <span class="s">&#39;lam&#39;</span>
<span class="k">if</span> <span class="n">head</span> <span class="o">==</span> <span class="s">&#39;quote&#39;</span><span class="p">:</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">list_input</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
<span class="k">return</span> <span class="n">rest</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">raise</span> <span class="n">quote_error</span>