|
| 1 | +Notes for 10/7/2014 |
| 2 | +================== |
| 3 | + |
| 4 | + |
| 5 | + None is default return value if a function doesn't return something else |
| 6 | + |
| 7 | +print grid notes |
| 8 | + |
| 9 | + Uses NUMBER to make the number of boxes increase without a newline |
| 10 | + |
| 11 | + top = ('+ ' + '- ' * box_size) * number + '+' + '\n' |
| 12 | + |
| 13 | + |
| 14 | +Git - version control system |
| 15 | + |
| 16 | +A history of everything you've ever done to your code |
| 17 | + |
| 18 | +A graph of "states in" which your code has existed |
| 19 | + |
| 20 | +Graph = places/states with paths between them |
| 21 | + |
| 22 | +A set of points of time with a history of where you've been |
| 23 | + |
| 24 | +Each point has a name that uniquely identifies it called a HASH |
| 25 | + |
| 26 | +HEAD = always there, and always points to the point in time you're currently looking at |
| 27 | + |
| 28 | +First branch that exists is called Master |
| 29 | + |
| 30 | +A "branch" is actually just a label that points to a specific point in time |
| 31 | + |
| 32 | +You can CREATE a branch by using the "branch" command |
| 33 | + |
| 34 | + This adds a new label to the current commit |
| 35 | + But it has not "checked it out" |
| 36 | + |
| 37 | +use 'git branch' to see which branch is active |
| 38 | + |
| 39 | +You can use this to switch between branches and make changes in isolation |
| 40 | + |
| 41 | +Git encourages making lots and lots of SMALL COMMITS |
| 42 | + |
| 43 | +Branching allows you to keep related sets of work separate from each other |
| 44 | + |
| 45 | +Simply create a new branch for each session from your repository master branch |
| 46 | + |
| 47 | +Do your homework in a separate branch so you can submit a separate pull request each week |
| 48 | + |
| 49 | + THIS IS HOW IT WORKS IN THE REAL WORLD |
| 50 | + |
| 51 | +(Dozens of branches on a weekly basis) |
| 52 | + |
| 53 | +The final step in the process is merging the work >> Combines work on one branch with your work on another |
| 54 | + |
| 55 | + 'git merge session01' |
| 56 | + |
| 57 | +Sometimes when you merge two branches, you get CONFLICTS |
| 58 | + |
| 59 | +This happens when the same file was changed in about the same place in two different ways |
| 60 | + |
| 61 | +Often, git can figure these things out on its own |
| 62 | + |
| 63 | + You may need to manually edit files to fix the problem |
| 64 | + |
| 65 | + Look for conflict markers |
| 66 | + |
| 67 | +<<<<<<<<<hash1 (stuff from the current branch) |
| 68 | +============ (pivot point between two branches) |
| 69 | +>>>>>>>>>>>>hash2 (stuff from the branch being merged) |
| 70 | + |
| 71 | + REMOVE CONFLICT MARKERS BEFORE RE-ADDING AND COMMITTING |
| 72 | + |
| 73 | +git is a "distributed versioning system" ; there's no central repository that serves as the one main repo |
| 74 | + |
| 75 | +Instead, you work with local repos, and remotes they are connected to |
| 76 | + |
| 77 | +Clone repositories get an origin remote for free: |
| 78 | + |
| 79 | +git remote -v |
| 80 | + |
| 81 | +origin represents where you cloned the repository |
| 82 | + |
| 83 | +Shows that the local repo originated on the github account |
| 84 | + |
| 85 | +Class Repo: |
| 86 | + |
| 87 | + |
| 88 | +You want to keep the fork up-to-date with the original copy as the class goes forward |
| 89 | + |
| 90 | +You can add "remotes" at will, to connect your local repo to other copies of it in different remote locations |
| 91 | + |
| 92 | +This allows you to grab changes made to the repo in these other locations |
| 93 | + |
| 94 | +For our class we will add an upstream remote to our local copy that points to the original copy of the material in the UWPC-PythonCert account |
| 95 | + |
| 96 | +To get updates from new remote, you'll need first to fetch everything |
| 97 | + |
| 98 | +Finally, you can fetch and then merge changes from the upstream master |
| 99 | + |
| 100 | + git checkout master |
| 101 | + |
| 102 | +You can do all in one step with |
| 103 | + |
| 104 | + git pull upstream master |
| 105 | + |
| 106 | +git pull fetches changes, brings them locally, and then merges them in |
| 107 | + |
| 108 | + Just doesn't show what the changes are going to be |
| 109 | + git checkout upstream master to SEE what's about to happen |
| 110 | + |
| 111 | + |
| 112 | +You can incorporate this into your daily workflow |
| 113 | + |
| 114 | + git checkout master |
| 115 | + git pull upstream master |
| 116 | + git push |
| 117 | + [do work] |
| 118 | + git commit -a |
| 119 | + [add a good commit message] |
| 120 | + git push |
| 121 | + [Make a pull request] |
| 122 | + |
| 123 | + |
| 124 | +There are graphical tools for Git (maybe check out?) |
| 125 | + |
| 126 | + |
| 127 | +============================================================================ |
| 128 | + |
| 129 | + |
| 130 | +if and elif <---python's way of letting you make decisions |
| 131 | + |
| 132 | +else is optional, elif are optional |
| 133 | + |
| 134 | +Multiples ifs will cause ALL of the ifs to be distinct (all things will happen instead of just one) |
| 135 | + |
| 136 | +Many languages have a "switch", Python does not |
| 137 | + |
| 138 | +in Python use if, elif, elif, elif, else |
| 139 | + |
| 140 | + Alternatively, use a dictionary or subclassing |
| 141 | + |
| 142 | +Lists -> a way to store a bunch of stuff in an ordered fashion. Just like an array or a vector |
| 143 | + |
| 144 | +a_list = [2,3,4,5] |
| 145 | +a_tuple = (2,3,4,) <---TUPLES USE () INSTEAD OF [] |
| 146 | + |
| 147 | +Biggest difference is that you can change the contents of a list, but not a tuple |
| 148 | + |
| 149 | + |
| 150 | +Loops: |
| 151 | + |
| 152 | +Sometimes called a 'determinate' loop |
| 153 | + |
| 154 | +When you need to do something to everything in a sequence |
| 155 | + |
| 156 | +(you've been using i for item) |
| 157 | + |
| 158 | + for item in a_list: |
| 159 | + print item |
| 160 | + |
| 161 | +range() builds a list of numbers automatically |
| 162 | + |
| 163 | +range(6) |
| 164 | + [0,1,2,3,4,5] |
| 165 | + |
| 166 | +================================ |
| 167 | +Functions: |
| 168 | + |
| 169 | + |
| 170 | +def fun(x,y): |
| 171 | + z = x+y |
| 172 | + return z |
| 173 | + |
| 174 | +xyz are LOCAL |
| 175 | + |
| 176 | +Symbols bound in Python have a SCOPE - determines where it is visible or what value it has in a given block |
| 177 | + |
| 178 | +If a value is rebound INSIDE the function, it does not change the value outside of the function |
| 179 | + |
| 180 | +In general, you should use global bindings mostly for constants (those that are made at the left margin = global) |
| 181 | + |
| 182 | +Convention: global constants are ALL_CAPS |
| 183 | + |
| 184 | + Good convention to follow |
| 185 | + |
| 186 | +We have seen simple parameter lists: |
| 187 | + |
| 188 | +def fun(x, y z): |
| 189 | + |
| 190 | + print x, y, z |
| 191 | + |
| 192 | +These types of params are called "positional" |
| 193 | + |
| 194 | +When you call a function, you must provide arguments for all positional params in the order they are listed |
| 195 | + |
| 196 | +You CAN provide default values for paramters in a function definition (this makes them optional) |
| 197 | + |
| 198 | +def fun(x=1,y=2,z=3) |
| 199 | + |
| 200 | +Or you can use the param as a keyword to indicate what you mean |
| 201 | + |
| 202 | +fun(y=4, x=1) |
| 203 | + |
| 204 | +Once you've provided a keyword argument in this way, you can no longer provide any positional arguments |
| 205 | + |
| 206 | +e.g. fun(x=5, 6) |
| 207 | + |
| 208 | +Documentation: |
| 209 | + |
| 210 | +Leave information in your code about what you were thinking when you wrote it |
| 211 | + |
| 212 | +There are two approaches to this: |
| 213 | + |
| 214 | + Comments (in line with code) |
| 215 | + #like this one |
| 216 | + |
| 217 | + Docstrings |
| 218 | + |
| 219 | + in-line documentation in a number of different places |
| 220 | + def complex_function() |
| 221 | + """THIS IS A DOCSTRING FOR THIS FUNCTION""" |
| 222 | + code code code |
| 223 | + |
| 224 | + complex_function.__doc__ |
| 225 | + |
| 226 | + THIS IS A DOCSTRING FOR THIS FUNCTION |
| 227 | + |
| 228 | + A docstring should be a complete sentence in the form of a command describing what the function does |
| 229 | + Should fit on a single line |
| 230 | + If more description is needed, make the first line a complete sentence and add more lines below for enhancement |
| 231 | + enclose with """ (allows for easy expansion later on) |
| 232 | + Always close on the same line if the doc string is only one line |
| 233 | + |
| 234 | +Recursion: |
| 235 | + |
| 236 | + We've seen functions that call another function |
| 237 | + |
| 238 | + If a function calls ITSELF, that's called recursion |
| 239 | + |
| 240 | + Like with other Functions, a call within a call establishes a call stack |
| 241 | + |
| 242 | + With recursion, if you are not careful, this stack can get VERY deep |
| 243 | + |
| 244 | + Python has a limit for how much it can recurse |
| 245 | + |
| 246 | + Recursion can be useful, especially for a particular set of problems |
| 247 | + |
| 248 | + For example, take the case of the factorial function |
| 249 | + |
| 250 | + 5! == 5*4*3*2*1 |
| 251 | + |
| 252 | + def factorial(x): |
| 253 | + if x ==1: |
| 254 | + return 1 |
| 255 | + else: |
| 256 | + return x * factorial(x - 1) |
| 257 | + |
| 258 | + |
| 259 | +Boolean Expressions!: |
| 260 | + |
| 261 | + What is true or false in Python? |
| 262 | + |
| 263 | + Booleans: True and False (CAPITALIZED) |
| 264 | + |
| 265 | + Something or Nothing |
| 266 | + |
| 267 | + Anything that is SOMETHING is True, anything that is Nothing is False |
| 268 | + |
| 269 | + bool(something) = is something a True or a False value? |
| 270 | + |
| 271 | + Nothing = empty string, tuple, dictionary, zero, or anything that returns 0 |
| 272 | + |
| 273 | + |
| 274 | + EVERYTHING ELSE is True |
| 275 | + |
| 276 | + |
| 277 | +Pythonic Boolean: |
| 278 | + |
| 279 | + Any object, passed to bool() will evaluate to True or False |
| 280 | + |
| 281 | + if xx == True: |
| 282 | + |
| 283 | + do_something() |
| 284 | + |
| 285 | + Bad ^^ |
| 286 | +------------------------ |
| 287 | + if xx: |
| 288 | + |
| 289 | + do_something() |
| 290 | + |
| 291 | + Good ^^ |
| 292 | + (Does not test if it exists, just tests if it's True or False) |
| 293 | + |
| 294 | + Boolean Keywords in Python: and, or, not |
| 295 | + |
| 296 | + 'and' will return the first operand that evaluates to False, or the last operand if none are True |
| 297 | + |
| 298 | + 'or' returns the first operand that evaluates to True or the last operand in line |
| 299 | + |
| 300 | + 'not' is an unary expression and inverts the boolean value of its operand: |
| 301 | + |
| 302 | + not True -> False |
| 303 | + |
| 304 | + not False -> True |
| 305 | + |
| 306 | +Because of the return value of these keywords, you can write concise statements |
| 307 | + |
| 308 | +Ternary Expressions: |
| 309 | + |
| 310 | + if something: |
| 311 | + |
| 312 | + x = a_value |
| 313 | + |
| 314 | + else: |
| 315 | + |
| 316 | + x = another_value |
| 317 | + |
| 318 | + y = 5 if x > 2 else 3 |
| 319 | + |
| 320 | + |
| 321 | +In Python, the boolean types are subclasses of integer: |
| 322 | + |
| 323 | + True == 1 |
| 324 | + True |
| 325 | + |
| 326 | + False == 0 |
| 327 | + True |
| 328 | + |
| 329 | + |
| 330 | + |
| 331 | +Whitespace is great because it encourages good formatting |
| 332 | + |
| 333 | +All statements must be correspondingly indented |
| 334 | + |
| 335 | + |
| 336 | +Other than indenting, space doesn't matter! |
| 337 | + |
| 338 | +Read PEP 8 (Python Style guide) and install a linter in your editor <---- |
| 339 | + |
| 340 | +python is all about namespaces -- the "dots" |
| 341 | + |
| 342 | + name.another_name |
| 343 | + |
| 344 | + The dot indicates that you're looking for a name in the namespace of the given object |
| 345 | + |
| 346 | + Namespace is collection of symbols that are bound to values |
| 347 | + |
| 348 | + You can think of the files that you write that end in .py is a module |
| 349 | + |
| 350 | + A MODULE is simply a namespace |
| 351 | + |
| 352 | + A package is a module with other modules inside it |
| 353 | + |
| 354 | + on a filesystem, this is represented as a directory that contains one or more .py files, one of which must be called __init__.py <---TURNS DIRECTORY FULL OF FILES INTO A PACKAGE |
| 355 | + |
| 356 | + Then you can import from that directory into another program |
| 357 | + |
| 358 | + |
| 359 | +When you import a module, or a symbol from a module, the Python code is compiled to bytecode, the result is a .pyc file |
| 360 | + |
| 361 | + This executes ALL CODE AT THE MODULE SCOPE |
| 362 | + |
| 363 | + For this reason, it is good to avoid module-scope statements that have global side-effect |
| 364 | + |
| 365 | + |
| 366 | +The code in a module is NOT re-run when imported again, it must be explicitly reloaded with |
| 367 | + |
| 368 | + import modulename |
| 369 | + reload(modulename) |
| 370 | + |
| 371 | + python - m <---if module is somewhere in Python Path (set of directories it looks in) it will be executed |
| 372 | + |
| 373 | + You can also use the "run" command from within Ipython |
| 374 | + |
| 375 | +======================================== |
| 376 | + |
| 377 | + |
| 378 | +Implement Ackermann function |
| 379 | + |
| 380 | + create ack.py in session02 |
| 381 | + |
| 382 | + write a good docstring |
| 383 | + |
| 384 | + Ackermann's function is not defined for input values less than 0, so check for that |
| 385 | + |
| 386 | + Use an if __name__ == "__main__" to test your function |
| 387 | + |
| 388 | + |
| 389 | + Create a Fibonacci script |
| 390 | + |
| 391 | + Check class site for homework :( |
0 commit comments