Python interlude: stripping (whitespace and newlines)
he wasn't even looking at me and he found me
Xuebing Du
art blog(derogatory)
Aqua Utopia|海の底で記憶を紡ぐ
tumblr dot com

izzy's playlists!
wallacepolsom
DEAR READER
styofa doing anything

PR's Tumblrdome
KIROKAZE
No title available
Cosmic Funnies

祝日 / Permanent Vacation
Today's Document

@theartofmadeline

No title available
Alisa U Zemlji Chuda
TVSTRANGERTHINGS
No title available
seen from United States
seen from United States

seen from Malaysia

seen from Argentina
seen from Türkiye

seen from United States

seen from China

seen from Singapore

seen from United States
seen from United Kingdom

seen from Malaysia
seen from United States
seen from United States

seen from United States

seen from Germany

seen from Germany

seen from Italy
seen from Israel

seen from Malaysia
seen from United States
@babydatajournalism
Python interlude: stripping (whitespace and newlines)
Not the end of Pset 3 - just the first check50 for this tough problem set. Yes, I can find a needle in a haystack. Now, on to the sorting ...
Successful coding has many levels to it. If the code works, that’s a big milestone to many students. But it can also get very poetic with as few lines, characters and symbols as possible. I also want to push back against the idea that there is one type of person or one set of interests that benefit from coding. Think about lawyers, who need to make rock solid legal contracts. Contracts, without loopholes, or without bugs if you will. People who want to be more methodical and better organize information can learn these skills through computer science.
David Malan, senior lecturer on computer science at Harvard SEAS (via harvardseas)
At last -- the final problem in pset2 in CS50. This one really kicked me up and down the block. It was this requirement that made it so hard for me:
if your code is about to apply the jth character of k to the ith character of p, but the latter proves to be a non-alphabetical character, you must wait to apply that jth character of k to the next alphabetical character in p; you must not yet advance to the next character in k.
I'm very glad to have that one solved!
This program encrypts messages using Vigenère’s cipher. Instead of advancing each letter in the plaintext by the same number, Vigenère’s cipher uses a keyword, and each letter in the keyword (converted to a number) advances a letter in the plaintext. So keyword[0] is used to "rotate" plaintext[0] forward in the alphabet that many places. Next, keyword[1] is used to "rotate" plaintext[1] forward in the alphabet that many places. And so on until it is done.
Almost forgot -- the very first assignment in CS50 is to create a new program in Scratch, a beginner's programming environment that's free on an MIT website. Here is my game, built in Scratch.
Continuing my diary/log of CS50 assignments ...
Now that CS50 (Harvard's amazing open Intro to Computer Science course) has rolled over into 2015, some of the assignments have changed. This one actually precedes the Caeser cipher problem in Pset2 (week 2).
I found I had forgotten a lot since my last effort, and I had to review a lot of things! Like how to write a function. Grr.
After many errors and difficulties, I've finished caesar.c in Problem set 2 in CS50. I have learned a lot about working with ASCII. Whew.
Very happy: Successfully completed greedy.c in Problem set 1.
Made it through my first (tiny) C program today.
CS50 at HarvardX
I am looking at the renowned CS50 course. It is a huge intro to computer science course from Harvard. You can take it free online. The video resources are very extensive. It has been translated into many languages.
Week 0: The two 50-minute lectures in this first week are way more lively and fun than the two in Week 1. That is because instructor David J. Malan starts the class with on-stage demos in which students represent binary numbers, and he demonstrates how to use Scratch, a programming language that uses graphical "blocks" to build programs. The first problem set in CS50 requires the use of Scratch.
I spent a couple of days learning to use Scratch. I would recommend it for beginners.
On to Week 1. The first lecture of the week was very dull for an online, not-for-credit learner, because Malan is explaining all kinds of course details. In the second lecture, he uses Scratch again and shows how things you (by now) have used in Scratch can be translated into the language C. This was interesting for me because I've never used C before. Once Malan gets into the syntax for if-statements and loops, I see that many structures are identical to JavaScript.
I have to wonder how beginners survive this course. It must be that the teaching assistants, meeting in numerous small sections with the on-site students, help them a lot.
It was also revealed that students need to download and install a virtual machine configured by the CS50 team. This is pretty awesome, in my opinion, but it is a honkin' huge download (more than a GB). Happily, I learned that Harvard has made this available to all of us faceless virtual students, and supposedly we can use it for 365 days, free of charge.
Next up for me: Problem set 1.
The course covers 13 weeks.
Real innovation doesn’t come from clicking someone else’s button. The masters of the Future of Journalism aren’t waiting around for someone else to build it. Learning to code means being able to build your vision instead of hoping someone else does.
Matt Waite, professor of journalism, University of Nebraska-Lincoln (via beanewsnerd)
"I hate reading other people’s code"
GitHub for Windows can't clone repo, stops at 9%
I installed GitHub for Windows on a university desktop computer, which is on a university's hard-wired Internet/LAN. In many cases, including mine, that means there is a proxy server between my computer and the real Internet. (Download GitHub for Windows, free.)
That turned out to be the cause of my problem: When I tried to clone a repo using GitHub for Windows, or using the GitHub website, it would not work. When trying to clone with GitHub for Windows, the process always stopped at 9% of download and eventually gave me this message:
Failed to clone the repository ... Please check your internet connection.
What's needed: You tell Git the address of the proxy server, and your username and password in the university system.
Here's how:
First, find the URL (address) and port number of the proxy server.
How to find information about your proxy address (on Windows)
Open Control Panel.
Select Internet Options.
Click the Connections tab.
Click the LAN settings button.
Copy and paste these two items into a plain text file: Address Port
Using your own login (username) and password (passw) for that network or computer, fill in the following
http://username:[email protected]:8080
Then open PowerShell (Windows) and paste in the following (with your own info) ALL ON ONE LINE:
git config --global http.proxy http://username:[email protected]:8080
NOTE 1: Make sure you have:
config [space] [hyphen] [hyphen] global
There is NO space between the hyphens and global.
NOTE 2: The stuff after the @ is the two items you copied from LAN settings in Internet Options (above).
NOTE 3: After I had installed GitHub for Windows on this Windows computer, I also installed the Git SCM. Get it here:
msysgit.github.io
Just download the installer .exe file and run it!
Python on Windows 7
I have to use a Windows 7 computer for a couple of months (I know, pity me, right?). I wanted to start using Python on it, and sure enough, Zed's sweet instructions worked just fine.
The hard part was Zed's step 5: "Run your Terminal program. It won't look like much." You are ALREADY IN THE TERMINAL PROGRAM when you open the PowerShell, which he already told you to do. Geez.
Next, I got a big fat error message after I typed python and pressed Enter. That is because Python is not installed. Zed has a link for that (in step 6.1). While installing, I carefully accepted all the defaults, changing NOTHING, because I know Windows is like that.
After installing Python 2.7 (oh yes -- I had to check whether my Windows computer was 64-bit or not. Open the Control Panel, click System, and look for "System type" there), I had to copy and paste the line Zed supplies in step 6.4. This sets the environment variable so the PowerShell can find Python.
Next, I had to exit and restart PowerShell.
And then -- voilà! I typed python at the command prompt, and I was in Python!
Thanks, Zed!
Python statements and syntax
Chapters 10 and 11 in Learning Python, 5th edition are the first in section 3, "Statements and Syntax."
"[Y]ou can make a single statement span across multiple lines. To make this work, you simply have to enclose part of your statement in a bracketed pair—parentheses (()), square brackets ([]), or curly braces ({}). Any code enclosed in these constructs can cross multiple lines ... Parentheses are the catchall device ..." p. 32
"An older rule also allows for continuation lines when the prior line ends in a backslash ... This alternative technique is dated, though, and is frowned on today because it’s difficult to notice and maintain the backslashes." (my bold)
Introducing the try statement:
while True: reply = raw_input('Enter text: ') if reply == 'stop': break try: num = int(reply) except: print 'Bad! ' * 8 else: print num ** 2 print 'Bye'
Chapter 10 is quite short! Just if, while, and try statements.
Assignments, expressions, and print are covered in chapter 11.
"Tuple assignment leads to a common coding trick in Python":
>>> nudge = 'NUDGE' >>> wink = 'WINK' >>> nudge, wink = wink, nudge >>> nudge 'WINK' >>> wink 'NUDGE'
"[W]e must generally have the same number of items on the right as we have variables on the left" -- not surprising, but this is an example of how clearly this book explains most things.
This is funky -- I had to think it out (p. 344):
>>> L = [1, 2, 3, 4] >>> while L: ... front, L = L[0], L[1:] ... print(front, L) ... 1 [2, 3, 4] 2 [3, 4] 3 [4] 4 []
Reserved words: "because module names in import statements become variables in your scripts, variable name constraints extend to your module filenames too."
Note: Names that begin with a single underscore (_X) are NOT imported by a "from module import xyz" statement.
(Python filenames may begin with an underscore.)
Expression statements: Expressions are commonly used as statements in two situations:
calls to functions and methods For functions and methods that do not return a value.
printing values at the interactive prompt i.e. in Terminal, we don't need to write "print."
"[A]lthough expressions can appear as statements in Python, statements cannot be used as expressions."
The rest of chapter 11 is about the print statement, with thorough coverage for both Python 3.x and 2.x (yay!). This is a simple thing:
print x, y # adds space between print x + y # no space
"[W]hereas file write methods write strings to arbitrary files, print writes objects to the stdout stream by default, with some automatic formatting added." p. 358
Learning jQuery, part 2 of 2 (with GitHub repo)
Taking a break from Python to work on jQuery:
Beginning jQuery part 2 from Mindy McAdams
Here is the GitHub repo: jQuery Exercises. Enjoy!
Tuples, Files, and Everything Else - Python
Chapter 9 in Learning Python, 5th edition has this rather unintuitive title, but it's the end of the section about "Object Types and Operations," so it's a kind of catch-all. (Seems to me the tuples could have gone in the previous chapter with lists and dictionaries.)
Tuples: "They work exactly like lists, except that tuples can’t be changed in place (they’re immutable) and are usually written as a series of items in parentheses, not square brackets." (p. 276)
You CAN reassign the variable name of a tuple, however -- like with everything in Python. So you could have T = (1, 2, 3) and then later change it, with T = (4, 5, 6).
You can change a tuple to a list: L = list(T)
Or even T = list(T)
"Tuples can also be used in places that lists cannot — for example, as dictionary keys ...", which seems useful.
Files: Zed gave me a lot of practice with files (writing, reading) in LPTHW. Not much new on that score.
"Python also includes advanced standard library tools for handling generic object storage (the pickle module), for dealing with packed binary data in files (the struct module), and for processing special types of content such as JSON, XML, and CSV text." (p. 284)
JSON - Python standard library module http://docs.python.org/2.7/library/json.html
Python XML and CSV modules: http://docs.python.org/2/library/xml.html http://docs.python.org/2/library/csv.html
Then there's a set of review questions (pp. 313-315). Good review. I keep forgetting how index works:
L.index(4) means "get the index number of the number 4 in the list L."
>>> L = ['apple', 'peach', 'pear', 'strawberry'] >>> L.index('pear') 2 >>> L[2] 'pear'
New files added to my python_notes repo:
string_searches.py
write_read_file.py
I didn't write a new file for demonstrating pickle, because I already did that in useful_modules.py (3 weeks ago).