├── .devcontainer ├── Dockerfile └── devcontainer.json ├── .github ├── CODEOWNERS ├── ISSUE_TEMPLATE.md ├── PULL_REQUEST_TEMPLATE.md └── workflows │ └── main.yml ├── .gitignore ├── .vscode └── settings.json ├── 30DayQuakes.json ├── CONTRIBUTING.md ├── Finished ├── Ch_1 │ ├── challenge_solution.py │ ├── filtering.py │ ├── minmax.py │ ├── sorting.py │ ├── transform.py │ └── utility.py ├── Ch_2 │ ├── challenge_solution.py │ ├── counter.py │ ├── defaultdict.py │ ├── deque.py │ └── namedtuple.py ├── Ch_3 │ ├── challenge_solution.py │ ├── deserialize.py │ ├── serialize_csv.py │ └── serialize_json.py └── Ch_4 │ ├── basiclog_finished.py │ └── customlog_finished.py ├── LICENSE ├── NOTICE ├── README.md └── Start ├── Ch_1 ├── challenge_start.py ├── filtering.py ├── minmax.py ├── sorting.py ├── transform.py └── utility.py ├── Ch_2 ├── challenge_start.py ├── counter.py ├── defaultdict.py ├── deque.py └── namedtuple.py ├── Ch_3 ├── challenge_start.py ├── deserialize.py ├── serialize_csv.py └── serialize_json.py └── Ch_4 ├── basiclog_start.py └── customlog_start.py /.devcontainer/Dockerfile: -------------------------------------------------------------------------------- 1 | # See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.233.0/containers/python-3/.devcontainer/base.Dockerfile 2 | 3 | # [Choice] Python version (use -bullseye variants on local arm64/Apple Silicon): 3, 3.10, 3.9, 3.8, 3.7, 3.6, 3-bullseye, 3.10-bullseye, 3.9-bullseye, 3.8-bullseye, 3.7-bullseye, 3.6-bullseye, 3-buster, 3.10-buster, 3.9-buster, 3.8-buster, 3.7-buster, 3.6-buster 4 | ARG VARIANT="3.10" 5 | FROM mcr.microsoft.com/vscode/devcontainers/python:0-${VARIANT} 6 | 7 | # [Choice] Node.js version: none, lts/*, 16, 14, 12, 10 8 | ARG NODE_VERSION="none" 9 | RUN if [ "${NODE_VERSION}" != "none" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi 10 | 11 | # [Optional] If your pip requirements rarely change, uncomment this section to add them to the image. 12 | # COPY requirements.txt /tmp/pip-tmp/ 13 | # RUN pip3 --disable-pip-version-check --no-cache-dir install -r /tmp/pip-tmp/requirements.txt \ 14 | # && rm -rf /tmp/pip-tmp 15 | 16 | # [Optional] Uncomment this section to install additional OS packages. 17 | # RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ 18 | # && apt-get -y install --no-install-recommends 19 | 20 | # [Optional] Uncomment this line to install global node packages. 21 | # RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g " 2>&1 22 | -------------------------------------------------------------------------------- /.devcontainer/devcontainer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Python 3", 3 | "build": { 4 | "dockerfile": "Dockerfile", 5 | "context": "..", 6 | "args": { 7 | "VARIANT": "3.10", // Set Python version here 8 | "NODE_VERSION": "lts/*" 9 | } 10 | }, 11 | "settings": { 12 | "python.defaultInterpreterPath": "/usr/local/bin/python", 13 | "python.linting.enabled": true, 14 | "python.linting.pylintEnabled": true, 15 | "python.formatting.autopep8Path": "/usr/local/py-utils/bin/autopep8", 16 | "python.formatting.blackPath": "/usr/local/py-utils/bin/black", 17 | "python.formatting.yapfPath": "/usr/local/py-utils/bin/yapf", 18 | "python.linting.banditPath": "/usr/local/py-utils/bin/bandit", 19 | "python.linting.flake8Path": "/usr/local/py-utils/bin/flake8", 20 | "python.linting.mypyPath": "/usr/local/py-utils/bin/mypy", 21 | "python.linting.pycodestylePath": "/usr/local/py-utils/bin/pycodestyle", 22 | "python.linting.pydocstylePath": "/usr/local/py-utils/bin/pydocstyle", 23 | "python.linting.pylintPath": "/usr/local/py-utils/bin/pylint", 24 | "python.linting.pylintArgs": ["--disable=C0111"] 25 | }, 26 | "extensions": [ 27 | "ms-python.python", 28 | "ms-python.vscode-pylance" 29 | ], 30 | "remoteUser": "vscode", 31 | "onCreateCommand": "echo PS1='\"$ \"' >> ~/.bashrc" //Set Terminal Prompt to $ 32 | } 33 | -------------------------------------------------------------------------------- /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | # Codeowners for these exercise files: 2 | # * (asterisk) denotes "all files and folders" 3 | # Example: * @producer @instructor 4 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 7 | 8 | ## Issue Overview 9 | 10 | 11 | ## Describe your environment 12 | 13 | 14 | ## Steps to Reproduce 15 | 16 | 1. 17 | 2. 18 | 3. 19 | 4. 20 | 21 | ## Expected Behavior 22 | 23 | 24 | ## Current Behavior 25 | 26 | 27 | ## Possible Solution 28 | 29 | 30 | ## Screenshots / Video 31 | 32 | 33 | ## Related Issues 34 | 35 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: Copy To Branches 2 | on: 3 | workflow_dispatch: 4 | jobs: 5 | copy-to-branches: 6 | runs-on: ubuntu-latest 7 | steps: 8 | - uses: actions/checkout@v2 9 | with: 10 | fetch-depth: 0 11 | - name: Copy To Branches Action 12 | uses: planetoftheweb/copy-to-branches@v1.2 13 | env: 14 | key: main 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Apple 2 | *.DS_Store 3 | 4 | # Built application files 5 | *.apk 6 | *.ap_ 7 | 8 | # Files for the Dalvik VM 9 | *.dex 10 | 11 | # Java class files 12 | *.class 13 | 14 | # Generated files 15 | bin/ 16 | gen/ 17 | 18 | # Gradle files 19 | .gradle/ 20 | build/ 21 | 22 | # Local configuration file (sdk path, etc) 23 | local.properties 24 | 25 | # Proguard folder generated by Eclipse 26 | proguard/ 27 | 28 | # Log Files 29 | *.log 30 | 31 | # Android Studio Navigation editor temp files 32 | .navigation/ 33 | 34 | # Android Studio captures folder 35 | captures/ 36 | 37 | # Google Doc files 38 | *.gsheet 39 | *.gslides 40 | *.gdoc 41 | 42 | # MS Office files 43 | *.xls_ 44 | *.doc_ 45 | *.ppt_ 46 | 47 | # PDF files 48 | *.pdf 49 | 50 | # ZIP files 51 | *.zip 52 | 53 | # VISUAL STUDIO FILES 54 | 55 | # User-specific files 56 | *.suo 57 | *.user 58 | *.userosscache 59 | *.sln.docstates 60 | 61 | # User-specific files (MonoDevelop/Xamarin Studio) 62 | *.userprefs 63 | 64 | # Build results 65 | [Dd]ebug/ 66 | [Dd]ebugPublic/ 67 | [Rr]elease/ 68 | [Rr]eleases/ 69 | x64/ 70 | x86/ 71 | bld/ 72 | [Bb]in/ 73 | [Oo]bj/ 74 | [Ll]og/ 75 | __pycache__/ 76 | 77 | # Visual Studio 2015 cache/options directory 78 | .vs/ 79 | # Uncomment if you have tasks that create the project's static files in wwwroot 80 | #wwwroot/ 81 | 82 | # MSTest test Results 83 | [Tt]est[Rr]esult*/ 84 | [Bb]uild[Ll]og.* 85 | 86 | # NUNIT 87 | *.VisualState.xml 88 | TestResult.xml 89 | 90 | # Build Results of an ATL Project 91 | [Dd]ebugPS/ 92 | [Rr]eleasePS/ 93 | dlldata.c 94 | 95 | # DNX 96 | project.lock.json 97 | artifacts/ 98 | 99 | *_i.c 100 | *_p.c 101 | *_i.h 102 | *.ilk 103 | *.meta 104 | *.obj 105 | *.pch 106 | *.pdb 107 | *.pgc 108 | *.pgd 109 | *.rsp 110 | *.sbr 111 | *.tlb 112 | *.tli 113 | *.tlh 114 | *.tmp 115 | *.tmp_proj 116 | *.log 117 | *.vspscc 118 | *.vssscc 119 | .builds 120 | *.pidb 121 | *.svclog 122 | *.scc 123 | 124 | # Chutzpah Test files 125 | _Chutzpah* 126 | 127 | # Visual C++ cache files 128 | ipch/ 129 | *.aps 130 | *.ncb 131 | *.opendb 132 | *.opensdf 133 | *.sdf 134 | *.cachefile 135 | *.VC.db 136 | *.VC.VC.opendb 137 | 138 | # Visual Studio profiler 139 | *.psess 140 | *.vsp 141 | *.vspx 142 | *.sap 143 | 144 | # TFS 2012 Local Workspace 145 | $tf/ 146 | 147 | # Guidance Automation Toolkit 148 | *.gpState 149 | 150 | # ReSharper is a .NET coding add-in 151 | _ReSharper*/ 152 | *.[Rr]e[Ss]harper 153 | *.DotSettings.user 154 | 155 | # JustCode is a .NET coding add-in 156 | .JustCode 157 | 158 | # TeamCity is a build add-in 159 | _TeamCity* 160 | 161 | # DotCover is a Code Coverage Tool 162 | *.dotCover 163 | 164 | # NCrunch 165 | _NCrunch_* 166 | .*crunch*.local.xml 167 | nCrunchTemp_* 168 | 169 | # MightyMoose 170 | *.mm.* 171 | AutoTest.Net/ 172 | 173 | # Web workbench (sass) 174 | .sass-cache/ 175 | 176 | # Installshield output folder 177 | [Ee]xpress/ 178 | 179 | # DocProject is a documentation generator add-in 180 | DocProject/buildhelp/ 181 | DocProject/Help/*.HxT 182 | DocProject/Help/*.HxC 183 | DocProject/Help/*.hhc 184 | DocProject/Help/*.hhk 185 | DocProject/Help/*.hhp 186 | DocProject/Help/Html2 187 | DocProject/Help/html 188 | 189 | # Click-Once directory 190 | publish/ 191 | 192 | # Publish Web Output 193 | *.[Pp]ublish.xml 194 | *.azurePubxml 195 | # TODO: Comment the next line if you want to checkin your web deploy settings 196 | # but database connection strings (with potential passwords) will be unencrypted 197 | *.pubxml 198 | *.publishproj 199 | 200 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 201 | # checkin your Azure Web App publish settings, but sensitive information contained 202 | # in these scripts will be unencrypted 203 | PublishScripts/ 204 | 205 | # NuGet Packages 206 | *.nupkg 207 | # The packages folder can be ignored because of Package Restore 208 | **/packages/* 209 | # except build/, which is used as an MSBuild target. 210 | !**/packages/build/ 211 | # Uncomment if necessary however generally it will be regenerated when needed 212 | #!**/packages/repositories.config 213 | # NuGet v3's project.json files produces more ignoreable files 214 | *.nuget.props 215 | *.nuget.targets 216 | 217 | # Microsoft Azure Build Output 218 | csx/ 219 | *.build.csdef 220 | 221 | # Microsoft Azure Emulator 222 | ecf/ 223 | rcf/ 224 | 225 | # Windows Store app package directories and files 226 | AppPackages/ 227 | BundleArtifacts/ 228 | Package.StoreAssociation.xml 229 | _pkginfo.txt 230 | 231 | # Visual Studio cache files 232 | # files ending in .cache can be ignored 233 | *.[Cc]ache 234 | # but keep track of directories ending in .cache 235 | !*.[Cc]ache/ 236 | 237 | # Others 238 | ClientBin/ 239 | ~$* 240 | *~ 241 | *.dbmdl 242 | *.dbproj.schemaview 243 | *.pfx 244 | *.publishsettings 245 | node_modules/ 246 | orleans.codegen.cs 247 | 248 | # Since there are multiple workflows, uncomment next line to ignore bower_components 249 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 250 | #bower_components/ 251 | 252 | # RIA/Silverlight projects 253 | Generated_Code/ 254 | 255 | # Backup & report files from converting an old project file 256 | # to a newer Visual Studio version. Backup files are not needed, 257 | # because we have git ;-) 258 | _UpgradeReport_Files/ 259 | Backup*/ 260 | UpgradeLog*.XML 261 | UpgradeLog*.htm 262 | 263 | # SQL Server files 264 | *.mdf 265 | *.ldf 266 | 267 | # Business Intelligence projects 268 | *.rdl.data 269 | *.bim.layout 270 | *.bim_*.settings 271 | 272 | # Microsoft Fakes 273 | FakesAssemblies/ 274 | 275 | # GhostDoc plugin setting file 276 | *.GhostDoc.xml 277 | 278 | # Node.js Tools for Visual Studio 279 | .ntvs_analysis.dat 280 | 281 | # Visual Studio 6 build log 282 | *.plg 283 | 284 | # Visual Studio 6 workspace options file 285 | *.opt 286 | 287 | # Visual Studio LightSwitch build output 288 | **/*.HTMLClient/GeneratedArtifacts 289 | **/*.DesktopClient/GeneratedArtifacts 290 | **/*.DesktopClient/ModelManifest.xml 291 | **/*.Server/GeneratedArtifacts 292 | **/*.Server/ModelManifest.xml 293 | _Pvt_Extensions 294 | 295 | # Paket dependency manager 296 | .paket/paket.exe 297 | paket-files/ 298 | 299 | # FAKE - F# Make 300 | .fake/ 301 | 302 | # JetBrains Rider 303 | .idea/ 304 | *.sln.iml 305 | 306 | # VS Code folder 307 | .vscode/ -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "editor.bracketPairColorization.enabled": true, 3 | "editor.cursorBlinking": "solid", 4 | "editor.fontFamily": "ui-monospace, Menlo, Monaco, 'Cascadia Mono', 'Segoe UI Mono', 'Roboto Mono', 'Oxygen Mono', 'Ubuntu Monospace', 'Source Code Pro', 'Fira Mono', 'Droid Sans Mono', 'Courier New', monospace", 5 | "editor.fontLigatures": false, 6 | "editor.fontSize": 22, 7 | "editor.formatOnPaste": true, 8 | "editor.formatOnSave": true, 9 | "editor.lineNumbers": "on", 10 | "editor.matchBrackets": "always", 11 | "editor.minimap.enabled": false, 12 | "editor.smoothScrolling": true, 13 | "editor.tabSize": 4, 14 | "editor.useTabStops": true, 15 | "emmet.triggerExpansionOnTab": true, 16 | "explorer.openEditors.visible": 0, 17 | "files.autoSave": "afterDelay", 18 | "screencastMode.onlyKeyboardShortcuts": true, 19 | "terminal.integrated.fontSize": 20, 20 | "workbench.colorTheme": "Visual Studio Dark", 21 | "workbench.fontAliasing": "antialiased", 22 | "workbench.statusBar.visible": true 23 | } 24 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | 2 | Contribution Agreement 3 | ====================== 4 | 5 | This repository does not accept pull requests (PRs). All pull requests will be closed. 6 | 7 | However, if any contributions (through pull requests, issues, feedback or otherwise) are provided, as a contributor, you represent that the code you submit is your original work or that of your employer (in which case you represent you have the right to bind your employer). By submitting code (or otherwise providing feedback), you (and, if applicable, your employer) are licensing the submitted code (and/or feedback) to LinkedIn and the open source community subject to the BSD 2-Clause license. 8 | -------------------------------------------------------------------------------- /Finished/Ch_1/challenge_solution.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Working With Data by Joe Marini 2 | # Programming challenge: summarize the earthquake data 3 | 4 | import json 5 | 6 | 7 | # for this challenge, we're going to summarize the earthquake data as follows: 8 | # 1: How many quakes are there in total? 9 | # 2: How many quakes were felt by at least 100 people? 10 | # 3: Print the name of the place whose quake was felt by the most people, with the # of reports 11 | # 4: Print the top 10 most significant events 12 | 13 | # open the data file and load the JSON 14 | with open("../../30DayQuakes.json", "r", encoding="utf-8") as datafile: 15 | data = json.load(datafile) 16 | 17 | # 1: We can just use the provided data, or we can use len() to get the length of the "features" 18 | print(f"Total quakes: {data['metadata']['count']}") 19 | 20 | 21 | # 2: This is a straightforward filtering process 22 | def feltreport(q): 23 | f = q["properties"]["felt"] 24 | return (f is not None and f >= 100) 25 | 26 | 27 | feltreports = list(filter(feltreport, data["features"])) 28 | print(f"Total quakes felt by at least 100 people: {len(feltreports)}") 29 | 30 | 31 | # 3: We can use the max function here to find the maximum # of felt reports 32 | def getfelt(q): 33 | f = q["properties"]["felt"] 34 | if f is not None: 35 | return f 36 | return 0 37 | 38 | 39 | mostfeltquake = max(data["features"], key=getfelt) 40 | print( 41 | f"Most felt reports: {mostfeltquake['properties']['title']}, reports: {mostfeltquake['properties']['felt']}") 42 | 43 | 44 | # 4: This is a sorting operation 45 | def getsig(q): 46 | s = q["properties"]["sig"] 47 | if s is not None: 48 | return s 49 | return 0 50 | 51 | 52 | sigevents = sorted(data["features"], key=getsig, reverse=True) 53 | print("The 10 most significant events were:") 54 | for i in range(0, 10): 55 | print( 56 | f"Event: {sigevents[i]['properties']['title']}, Significance: {sigevents[i]['properties']['sig']}") 57 | -------------------------------------------------------------------------------- /Finished/Ch_1/filtering.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Working With Data by Joe Marini 2 | # using the filter() function to filter a data set 3 | 4 | import json 5 | 6 | 7 | def filterEvens(x): 8 | # filters out even numbers and keeps odd numbers 9 | if x % 2 == 0: 10 | return False 11 | return True 12 | 13 | 14 | def filterUppers(x): 15 | # filters out upper-case letters and keeps lower case letters 16 | if x.isupper(): 17 | return False 18 | return True 19 | 20 | 21 | # define some sample sequences to operate on 22 | nums = (1, 8, 4, 5, 13, 26, 381, 410, 58, 47) 23 | chars = "abcDeFGHiJklmnoP" 24 | 25 | # use filter to remove items from a list 26 | odds = list(filter(filterEvens, nums)) 27 | print(odds) 28 | 29 | # use filter on non-numeric sequence 30 | lowers = list(filter(filterUppers, chars)) 31 | print(lowers) 32 | 33 | # Use the filter on our data - let's filter out all seismic events that were *not* quakes 34 | # open the data file and load the JSON 35 | with open("../../30DayQuakes.json", "r") as datafile: 36 | data = json.load(datafile) 37 | 38 | 39 | def notAQuake(q): 40 | if q["properties"]["type"] == "earthquake": 41 | return False 42 | return True 43 | 44 | 45 | events = list(filter(notAQuake, data['features'])) 46 | print(f"Total non-quake events: {len(events)}") 47 | for i in range(0, 10): 48 | print(events[i]["properties"]["type"]) 49 | -------------------------------------------------------------------------------- /Finished/Ch_1/minmax.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Working With Data by Joe Marini 2 | # Demonstrates the usage of the min and max functions 3 | import json 4 | 5 | 6 | # Declare an array with some sample data in it 7 | values = [3.0, 2.5, 5.1, 4.1, 1.8, 1.6, 2.2, 5.7, 6.1] 8 | strings = ["one", "three", "five", "seven", "eleven", "eighteen"] 9 | 10 | # The min() function finds the minimum value 11 | print(f"The minimum value is: {min(values)}") 12 | print(f"The minimum value is: {min(strings,key=len)}") 13 | 14 | # The max() function finds the maximum value 15 | print(f"The minimum value is: {max(values)}") 16 | print(f"The minimum value is: {max(strings,key=len)}") 17 | 18 | 19 | # define a custom "key" function to extract a data field 20 | def getmag(dataitem): 21 | magnitude = dataitem["properties"]["mag"] 22 | if (magnitude is None): 23 | magnitude = 0 24 | return float(magnitude) 25 | 26 | 27 | # open the data file and load the JSON 28 | with open("../../30DayQuakes.json", "r") as datafile: 29 | data = json.load(datafile) 30 | 31 | print(data["metadata"]["title"]) 32 | print(len(data["features"])) 33 | print(min(data["features"], key=getmag)) 34 | print(max(data["features"], key=getmag)) 35 | -------------------------------------------------------------------------------- /Finished/Ch_1/sorting.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Working With Data by Joe Marini 2 | # sorting data with the sorted() and sort() functions 3 | 4 | import json 5 | 6 | 7 | numbers = [42, 54, 19, 17, 23, 31, 16, 4] 8 | names = ["Jeff", "Bill", "Addie", "Stephanie", "Zach", "Lukas", "Joe", "Stacy"] 9 | 10 | # the sorted() function can be used to return a new list with sorted data 11 | result1 = sorted(numbers) 12 | print(result1) 13 | # alternately, you can use the list object's sort() method, which sorts the list in-place 14 | names.sort(reverse=True) 15 | print(names) 16 | 17 | # To sort custom objects, we can tell the sort function which property to use 18 | # by specifying a key function 19 | 20 | # open the data file and load the JSON 21 | with open("../../30DayQuakes.json", "r") as datafile: 22 | data = json.load(datafile) 23 | 24 | 25 | def getmag(dataitem): 26 | magnitude = dataitem["properties"]["mag"] 27 | if (magnitude is None): 28 | magnitude = 0 29 | return float(magnitude) 30 | 31 | 32 | data['features'].sort(key=getmag, reverse=True) 33 | for i in range(0, 10): 34 | print(data['features'][i]['properties']['place']) 35 | -------------------------------------------------------------------------------- /Finished/Ch_1/transform.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Working With Data by Joe Marini 2 | # using the map() function to transform data to another form 3 | 4 | import json 5 | import pprint 6 | import datetime 7 | 8 | 9 | def squareFunc(x): 10 | return x**2 11 | 12 | 13 | def toGrade(x): 14 | if (x >= 90): 15 | return "A" 16 | elif (x >= 80 and x < 90): 17 | return "B" 18 | elif (x >= 70 and x < 80): 19 | return "C" 20 | elif (x >= 65 and x < 70): 21 | return "D" 22 | return "F" 23 | 24 | 25 | # define some sample sequences to operate on 26 | nums = (1, 8, 4, 5, 13, 26, 381, 410, 58, 47) 27 | grades = (81, 89, 94, 78, 61, 66, 99, 74) 28 | 29 | # use map to create a new sequence of values 30 | squares = list(map(squareFunc, nums)) 31 | print(squares) 32 | 33 | # use sorted and map to change numbers to grades 34 | grades = sorted(grades) 35 | letters = list(map(toGrade, grades)) 36 | print(letters) 37 | 38 | # Use the filter on our data - let's filter out all seismic events that were *not* quakes 39 | # open the data file and load the JSON 40 | with open("../../30DayQuakes.json", "r") as datafile: 41 | data = json.load(datafile) 42 | 43 | 44 | # filter the data down to the largest events 45 | def bigmag(q): 46 | return q['properties']['mag'] is not None and q['properties']['mag'] >= 6 47 | 48 | 49 | results = list(filter(bigmag, data['features'])) 50 | 51 | 52 | # transform the largest events into a simpler structure 53 | def simplify(q): 54 | return { 55 | "place": q['properties']['place'], 56 | "magnitude": q['properties']['mag'], 57 | "date": str(datetime.date.fromtimestamp(q['properties']["time"]/1000)) 58 | } 59 | 60 | 61 | results = list(map(simplify, results)) 62 | pprint.pp(results, indent=2) 63 | -------------------------------------------------------------------------------- /Finished/Ch_1/utility.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Working With Data by Joe Marini 2 | # demonstrates the use of the any, all, and sum functions 3 | import json 4 | 5 | 6 | values = [0, 1, 2, 3, 4, 5] 7 | 8 | # any() can be used to see if any value in a sequence is True 9 | print(any(values)) 10 | 11 | # all() will detect if all of the values in a sequence are True 12 | print(all(values)) 13 | 14 | # sum() can be use to add all of the values in a sequence 15 | print(sum(values)) 16 | 17 | # these utility functions don't have callbacks like min or max, 18 | # but we can use a generator for more fine control 19 | 20 | # open the data file and load the JSON 21 | with open("../../30DayQuakes.json", "r") as datafile: 22 | data = json.load(datafile) 23 | 24 | # are there any quake reports that were felt by more than 25,000 people? 25 | print(any(quake["properties"]["felt"] is not None and quake["properties"] 26 | ["felt"] > 25000 for quake in data["features"])) 27 | 28 | # how many quakes were felt by more than 500 people? 29 | print(sum(quake["properties"]["felt"] is not None and quake["properties"] 30 | ["felt"] >= 500 for quake in data["features"])) 31 | 32 | # how many quakes had a magnitude of 6 or larger? 33 | print(sum(quake["properties"]["mag"] is not None and quake["properties"] 34 | ["mag"] >= 6 for quake in data["features"])) 35 | -------------------------------------------------------------------------------- /Finished/Ch_2/challenge_solution.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Working With Data by Joe Marini 2 | # Programming challenge: use advanced data collections on the earthquake data 3 | 4 | import json 5 | from collections import defaultdict 6 | 7 | 8 | # open the data file and load the JSON 9 | with open("../../30DayQuakes.json", "r") as datafile: 10 | data = json.load(datafile) 11 | 12 | # use a defaultdict to categorize each event and count each one 13 | totals = defaultdict(int) 14 | for event in data['features']: 15 | totals[event['properties']['type']] += 1 16 | 17 | for k, v in totals.items(): 18 | print(f"{k:15}: {v}") 19 | -------------------------------------------------------------------------------- /Finished/Ch_2/counter.py: -------------------------------------------------------------------------------- 1 | # Demonstrate the usage of Counter objects 2 | 3 | from collections import Counter 4 | 5 | 6 | # list of students in class 1 7 | class1 = ["Bob", "James", "Chad", "Darcy", "Penny", "Hannah", 8 | "Kevin", "James", "Melanie", "Becky", "Steve", "Frank"] 9 | 10 | # list of students in class 2 11 | class2 = ["Bill", "Barry", "Cindy", "Debbie", "Frank", 12 | "Gabby", "Kelly", "James", "Joe", "Sam", "Tara", "Ziggy"] 13 | 14 | # Create a Counter for class1 and class2 15 | c1 = Counter(class1) 16 | c2 = Counter(class2) 17 | 18 | # How many students in class 1 named James? 19 | print(c1["James"]) 20 | 21 | # How many students are in class 1? 22 | print(sum(c1.values()), "students in class 1") 23 | 24 | # Combine the two classes 25 | c1.update(class2) 26 | print(sum(c1.values()), "students in class 1 and 2") 27 | 28 | # What's the most common name in the two classes? 29 | print(c1.most_common(3)) 30 | 31 | # Separate the classes again 32 | c1.subtract(class2) 33 | print(c1.most_common(1)) 34 | 35 | # What's common between the two classes? 36 | print(c1 & c2) 37 | -------------------------------------------------------------------------------- /Finished/Ch_2/defaultdict.py: -------------------------------------------------------------------------------- 1 | # Demonstrate the usage of defaultdict objects 2 | 3 | from collections import defaultdict 4 | 5 | 6 | # define a list of items that we want to count 7 | fruits = ['apple', 'pear', 'orange', 'banana', 8 | 'apple', 'grape', 'banana', 'banana'] 9 | 10 | # use a dictionary to count each element 11 | fruitCounter = defaultdict(int) 12 | 13 | # Count the elements in the list 14 | for fruit in fruits: 15 | fruitCounter[fruit] += 1 16 | 17 | # print the result 18 | for (k, v) in fruitCounter.items(): 19 | print(k + ": " + str(v)) 20 | -------------------------------------------------------------------------------- /Finished/Ch_2/deque.py: -------------------------------------------------------------------------------- 1 | # deque objects are like double-ended queues 2 | 3 | import collections 4 | import string 5 | 6 | 7 | # initialize a deque with lowercase letters 8 | d = collections.deque(string.ascii_lowercase) 9 | 10 | # deques support the len() function 11 | print("Item count: " + str(len(d))) 12 | 13 | # deques can be iterated over 14 | for elem in d: 15 | print(elem.upper()) 16 | 17 | # manipulate items from either end 18 | d.pop() 19 | d.popleft() 20 | d.append(2) 21 | d.appendleft(1) 22 | print(d) 23 | 24 | # use an index to get a particular item 25 | print(d) 26 | d.rotate(1) 27 | print(d) 28 | -------------------------------------------------------------------------------- /Finished/Ch_2/namedtuple.py: -------------------------------------------------------------------------------- 1 | # Demonstrate the usage of namdtuple objects 2 | 3 | import collections 4 | 5 | 6 | # create a Point namedtuple 7 | Point = collections.namedtuple("Point", "x y") 8 | 9 | p1 = Point(10, 20) 10 | p2 = Point(30, 40) 11 | 12 | print(p1, p2) 13 | print(p1.x, p1.y) 14 | 15 | # use _replace to create a new instance 16 | p1 = p1._replace(x=100) 17 | print(p1) 18 | -------------------------------------------------------------------------------- /Finished/Ch_3/challenge_solution.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Working With Data by Joe Marini 2 | # Programming challenge: serialize earthquake data to a file 3 | 4 | import json 5 | import csv 6 | import datetime 7 | 8 | 9 | # open the data file and load the JSON 10 | with open("../../30DayQuakes.json", "r") as datafile: 11 | data = json.load(datafile) 12 | 13 | # Create a CSV file with the following information: 14 | # 40 most significant seismic events, ordered by most recent 15 | # Header row: Magnitude, Place, Felt Reports, Date, and Google Map link 16 | # Date should be in the format of YYYY-MM-DD 17 | 18 | 19 | def getsig(x): 20 | sig = x["properties"]["sig"] 21 | return 0 if sig is None else sig 22 | 23 | 24 | significantevents = sorted(data["features"], key=getsig, reverse=True) 25 | significantevents = significantevents[:40] 26 | significantevents.sort(key=lambda e: e["properties"]["time"], reverse=True) 27 | 28 | header = ["Magnitude", "Place", "Felt Reports", "Date", "Link"] 29 | rows = [] 30 | 31 | for event in significantevents: 32 | thedate = datetime.date.fromtimestamp( 33 | int(event["properties"]["time"]) / 1000) 34 | lat = event["geometry"]["coordinates"][1] 35 | long = event["geometry"]["coordinates"][0] 36 | gmaplink = f"https://maps.google.com/maps/search/?api=1&query={lat}%2C{long}" 37 | 38 | rows.append([event["properties"]["mag"], 39 | event["properties"]["place"], 40 | 0 if event["properties"]["felt"] is None else event["properties"]["felt"], 41 | thedate, 42 | gmaplink]) 43 | 44 | with open("significantevents.csv", "w") as csvfile: 45 | writer = csv.writer(csvfile, delimiter=',') 46 | writer.writerow(header) 47 | writer.writerows(rows) 48 | -------------------------------------------------------------------------------- /Finished/Ch_3/deserialize.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Working With Data by Joe Marini 2 | # read data from a CSV file into an object structure 3 | 4 | import csv 5 | import pprint 6 | 7 | 8 | # read the contents of a CSV file into an object structure 9 | result = [] 10 | 11 | # open the CSV file for reading 12 | with open("largequakes.csv", "r") as csvfile: 13 | # create the reader object 14 | reader = csv.reader(csvfile) 15 | 16 | # does the file contain headers? 17 | sniffer = csv.Sniffer() 18 | sample = csvfile.read(1024) 19 | csvfile.seek(0) 20 | if (sniffer.has_header(sample)): 21 | next(reader) 22 | 23 | # iterate over each row 24 | for row in reader: 25 | # print(row) 26 | 27 | # add the data to our list 28 | result.append({ 29 | "place": row[0], 30 | "magnitude": row[1], 31 | "date": row[2], 32 | "link": row[3] 33 | }) 34 | 35 | pprint.pp(result) 36 | -------------------------------------------------------------------------------- /Finished/Ch_3/serialize_csv.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Working With Data by Joe Marini 2 | # demonstrates how to serialize data to a CSV file 3 | 4 | import csv 5 | import json 6 | import datetime 7 | 8 | # read in the contents of the JSON file 9 | with open("../../30DayQuakes.json", "r") as datafile: 10 | data = json.load(datafile) 11 | 12 | 13 | def isbig(x): 14 | mag = x["properties"]["mag"] 15 | return mag is not None and mag > 5 16 | 17 | 18 | # Filter the data by quakes that are larger than 5 magnitude 19 | largequakes = list(filter(isbig, data["features"])) 20 | 21 | # Create the header and row structures for the data 22 | header = ["Place", "Magnitude", "Date", "Link"] 23 | rows = [] 24 | 25 | # populate the rows with the resulting quake data 26 | for quake in largequakes: 27 | thedate = datetime.date.fromtimestamp( 28 | int(quake["properties"]["time"])/1000) 29 | rows.append([quake["properties"]["place"], quake["properties"] 30 | ["mag"], thedate, quake["properties"]["url"]]) 31 | 32 | # write the results to the CSV file 33 | with open("largequakes.csv", "w", newline='', encoding="utf-8") as csvfile: 34 | writer = csv.writer(csvfile, delimiter=',') 35 | writer.writerow(header) 36 | writer.writerows(rows) 37 | -------------------------------------------------------------------------------- /Finished/Ch_3/serialize_json.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Working With Data by Joe Marini 2 | # demonstrates how to serialize data to a JSON file 3 | 4 | import json 5 | import datetime 6 | 7 | 8 | # read in the contents of the JSON file 9 | with open("../../30DayQuakes.json", "r") as datafile: 10 | data = json.load(datafile) 11 | 12 | 13 | def isbig(x): 14 | mag = x["properties"]["mag"] 15 | return mag is not None and mag > 6 16 | 17 | 18 | # define a function to transform complex JSON to simpler JSON 19 | def simplequake(q): 20 | return { 21 | "place": q["properties"]["place"], 22 | "mag": q["properties"]["mag"], 23 | "link": q["properties"]["url"], 24 | "date": str(datetime.date.fromtimestamp( 25 | int(q["properties"]["time"])/1000)) 26 | } 27 | 28 | 29 | # filter the data to only include large quakes 30 | largequakes = list(filter(isbig, data["features"])) 31 | # transform the data to a JSON format we want to save 32 | largequakes = list(map(simplequake, largequakes)) 33 | 34 | # use the dumps() function to write json to a string 35 | str = json.dumps(largequakes, sort_keys=True, indent=4) 36 | print(str) 37 | 38 | # use the dump() function to write json to a file 39 | with open("largequakes.json", "w", encoding="utf-8") as outfile: 40 | json.dump(largequakes, outfile, sort_keys=True, indent=4) 41 | -------------------------------------------------------------------------------- /Finished/Ch_4/basiclog_finished.py: -------------------------------------------------------------------------------- 1 | # demonstrate the logging api in Python 2 | 3 | # use the built-in logging module 4 | import logging 5 | 6 | 7 | # Use basicConfig to configure logging 8 | # this is only executed once, subsequent calls to 9 | # basicConfig will have no effect 10 | logging.basicConfig(level=logging.DEBUG, 11 | filemode="w", 12 | filename="output.log") 13 | 14 | # Try out each of the log levels 15 | logging.debug("This is a debug-level log message") 16 | logging.info("This is an info-level log message") 17 | logging.warning("This is a warning-level message") 18 | logging.error("This is an error-level message") 19 | logging.critical("This is a critical-level message") 20 | 21 | # Output formatted string to the log 22 | logging.info("Here's a {} variable and an int: {}".format("string", 10)) 23 | -------------------------------------------------------------------------------- /Finished/Ch_4/customlog_finished.py: -------------------------------------------------------------------------------- 1 | # Demonstrate how to customize logging output 2 | 3 | import logging 4 | 5 | extData = {'user': 'joem@example.com'} 6 | 7 | 8 | def anotherFunction(): 9 | logging.debug("This is a debug-level log message", extra=extData) 10 | 11 | 12 | # set the output file and debug level, and 13 | # use a custom formatting specification 14 | fmtStr = "%(asctime)s: %(levelname)s: %(funcName)s Line:%(lineno)d User:%(user)s %(message)s" 15 | dateStr = "%m/%d/%Y %I:%M:%S %p" 16 | logging.basicConfig(filename="output.log", 17 | level=logging.DEBUG, 18 | format=fmtStr, 19 | datefmt=dateStr) 20 | 21 | logging.info("This is an info-level log message", extra=extData) 22 | logging.warning("This is a warning-level message", extra=extData) 23 | anotherFunction() 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | LinkedIn Learning Exercise Files License Agreement 2 | ================================================== 3 | 4 | This License Agreement (the "Agreement") is a binding legal agreement 5 | between you (as an individual or entity, as applicable) and LinkedIn 6 | Corporation (“LinkedIn”). By downloading or using the LinkedIn Learning 7 | exercise files in this repository (“Licensed Materials”), you agree to 8 | be bound by the terms of this Agreement. If you do not agree to these 9 | terms, do not download or use the Licensed Materials. 10 | 11 | 1. License. 12 | - a. Subject to the terms of this Agreement, LinkedIn hereby grants LinkedIn 13 | members during their LinkedIn Learning subscription a non-exclusive, 14 | non-transferable copyright license, for internal use only, to 1) make a 15 | reasonable number of copies of the Licensed Materials, and 2) make 16 | derivative works of the Licensed Materials for the sole purpose of 17 | practicing skills taught in LinkedIn Learning courses. 18 | - b. Distribution. Unless otherwise noted in the Licensed Materials, subject 19 | to the terms of this Agreement, LinkedIn hereby grants LinkedIn members 20 | with a LinkedIn Learning subscription a non-exclusive, non-transferable 21 | copyright license to distribute the Licensed Materials, except the 22 | Licensed Materials may not be included in any product or service (or 23 | otherwise used) to instruct or educate others. 24 | 25 | 2. Restrictions and Intellectual Property. 26 | - a. You may not to use, modify, copy, make derivative works of, publish, 27 | distribute, rent, lease, sell, sublicense, assign or otherwise transfer the 28 | Licensed Materials, except as expressly set forth above in Section 1. 29 | - b. Linkedin (and its licensors) retains its intellectual property rights 30 | in the Licensed Materials. Except as expressly set forth in Section 1, 31 | LinkedIn grants no licenses. 32 | - c. You indemnify LinkedIn and its licensors and affiliates for i) any 33 | alleged infringement or misappropriation of any intellectual property rights 34 | of any third party based on modifications you make to the Licensed Materials, 35 | ii) any claims arising from your use or distribution of all or part of the 36 | Licensed Materials and iii) a breach of this Agreement. You will defend, hold 37 | harmless, and indemnify LinkedIn and its affiliates (and our and their 38 | respective employees, shareholders, and directors) from any claim or action 39 | brought by a third party, including all damages, liabilities, costs and 40 | expenses, including reasonable attorneys’ fees, to the extent resulting from, 41 | alleged to have resulted from, or in connection with: (a) your breach of your 42 | obligations herein; or (b) your use or distribution of any Licensed Materials. 43 | 44 | 3. Open source. This code may include open source software, which may be 45 | subject to other license terms as provided in the files. 46 | 47 | 4. Warranty Disclaimer. LINKEDIN PROVIDES THE LICENSED MATERIALS ON AN “AS IS” 48 | AND “AS AVAILABLE” BASIS. LINKEDIN MAKES NO REPRESENTATION OR WARRANTY, 49 | WHETHER EXPRESS OR IMPLIED, ABOUT THE LICENSED MATERIALS, INCLUDING ANY 50 | REPRESENTATION THAT THE LICENSED MATERIALS WILL BE FREE OF ERRORS, BUGS OR 51 | INTERRUPTIONS, OR THAT THE LICENSED MATERIALS ARE ACCURATE, COMPLETE OR 52 | OTHERWISE VALID. TO THE FULLEST EXTENT PERMITTED BY LAW, LINKEDIN AND ITS 53 | AFFILIATES DISCLAIM ANY IMPLIED OR STATUTORY WARRANTY OR CONDITION, INCLUDING 54 | ANY IMPLIED WARRANTY OR CONDITION OF MERCHANTABILITY OR FITNESS FOR A 55 | PARTICULAR PURPOSE, AVAILABILITY, SECURITY, TITLE AND/OR NON-INFRINGEMENT. 56 | YOUR USE OF THE LICENSED MATERIALS IS AT YOUR OWN DISCRETION AND RISK, AND 57 | YOU WILL BE SOLELY RESPONSIBLE FOR ANY DAMAGE THAT RESULTS FROM USE OF THE 58 | LICENSED MATERIALS TO YOUR COMPUTER SYSTEM OR LOSS OF DATA. NO ADVICE OR 59 | INFORMATION, WHETHER ORAL OR WRITTEN, OBTAINED BY YOU FROM US OR THROUGH OR 60 | FROM THE LICENSED MATERIALS WILL CREATE ANY WARRANTY OR CONDITION NOT 61 | EXPRESSLY STATED IN THESE TERMS. 62 | 63 | 5. Limitation of Liability. LINKEDIN SHALL NOT BE LIABLE FOR ANY INDIRECT, 64 | INCIDENTAL, SPECIAL, PUNITIVE, CONSEQUENTIAL OR EXEMPLARY DAMAGES, INCLUDING 65 | BUT NOT LIMITED TO, DAMAGES FOR LOSS OF PROFITS, GOODWILL, USE, DATA OR OTHER 66 | INTANGIBLE LOSSES . IN NO EVENT WILL LINKEDIN'S AGGREGATE LIABILITY TO YOU 67 | EXCEED $100. THIS LIMITATION OF LIABILITY SHALL: 68 | - i. APPLY REGARDLESS OF WHETHER (A) YOU BASE YOUR CLAIM ON CONTRACT, TORT, 69 | STATUTE, OR ANY OTHER LEGAL THEORY, (B) WE KNEW OR SHOULD HAVE KNOWN ABOUT 70 | THE POSSIBILITY OF SUCH DAMAGES, OR (C) THE LIMITED REMEDIES PROVIDED IN THIS 71 | SECTION FAIL OF THEIR ESSENTIAL PURPOSE; AND 72 | - ii. NOT APPLY TO ANY DAMAGE THAT LINKEDIN MAY CAUSE YOU INTENTIONALLY OR 73 | KNOWINGLY IN VIOLATION OF THESE TERMS OR APPLICABLE LAW, OR AS OTHERWISE 74 | MANDATED BY APPLICABLE LAW THAT CANNOT BE DISCLAIMED IN THESE TERMS. 75 | 76 | 6. Termination. This Agreement automatically terminates upon your breach of 77 | this Agreement or termination of your LinkedIn Learning subscription. On 78 | termination, all licenses granted under this Agreement will terminate 79 | immediately and you will delete the Licensed Materials. Sections 2-7 of this 80 | Agreement survive any termination of this Agreement. LinkedIn may discontinue 81 | the availability of some or all of the Licensed Materials at any time for any 82 | reason. 83 | 84 | 7. Miscellaneous. This Agreement will be governed by and construed in 85 | accordance with the laws of the State of California without regard to conflict 86 | of laws principles. The exclusive forum for any disputes arising out of or 87 | relating to this Agreement shall be an appropriate federal or state court 88 | sitting in the County of Santa Clara, State of California. If LinkedIn does 89 | not act to enforce a breach of this Agreement, that does not mean that 90 | LinkedIn has waived its right to enforce this Agreement. The Agreement does 91 | not create a partnership, agency relationship, or joint venture between the 92 | parties. Neither party has the power or authority to bind the other or to 93 | create any obligation or responsibility on behalf of the other. You may not, 94 | without LinkedIn’s prior written consent, assign or delegate any rights or 95 | obligations under these terms, including in connection with a change of 96 | control. Any purported assignment and delegation shall be ineffective. The 97 | Agreement shall bind and inure to the benefit of the parties, their respective 98 | successors and permitted assigns. If any provision of the Agreement is 99 | unenforceable, that provision will be modified to render it enforceable to the 100 | extent possible to give effect to the parties’ intentions and the remaining 101 | provisions will not be affected. This Agreement is the only agreement between 102 | you and LinkedIn regarding the Licensed Materials, and supersedes all prior 103 | agreements relating to the Licensed Materials. 104 | 105 | Last Updated: March 2019 106 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | Copyright 2023 LinkedIn Corporation 2 | All Rights Reserved. 3 | 4 | Licensed under the LinkedIn Learning Exercise File License (the "License"). 5 | See LICENSE in the project root for license information. 6 | 7 | Please note, this project may automatically load third party code from external 8 | repositories (for example, NPM modules, Composer packages, or other dependencies). 9 | If so, such third party code may be subject to other license terms than as set 10 | forth above. In addition, such third party code may also depend on and load 11 | multiple tiers of dependencies. Please review the applicable licenses of the 12 | additional dependencies. 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Advanced Python: Working With Data 2 | This is the repository for the LinkedIn Learning course Advanced Python: Working With Data. The full course is available from [LinkedIn Learning][lil-course-url]. 3 | 4 | ![Advanced Python: Working With Data][lil-thumbnail-url] 5 | 6 | Data science is one of the fastest-growing areas of technology today. And whether you work with large data sets or just need to process spread sheet files, Python is a great language to use when working with data-intensive applications. In this course, Joe Marini gets you started on working with data in Python, highlighting some of the most useful built-in features of the language. Using a real data set from the United States Geological Survey that tracks earthquake information, Joe shows you how to perform data operations like sorting and filtering, determining basic information like minimum and maximum values, and reading and writing data to and from other formats like CSV and JSON. Check out this course with Joe to see how Python can help you make sense of your data. 7 | 8 | 9 | ### Instructor 10 | 11 | Joe Marini 12 | 13 | Senior Director of Product and Engineering 14 | 15 | 16 | 17 | Check out my other courses on [LinkedIn Learning](https://www.linkedin.com/learning/instructors/joe-marini). 18 | 19 | [lil-course-url]: https://www.linkedin.com/learning/advanced-python-working-with-data?dApp=59033956 20 | [lil-thumbnail-url]: https://media.licdn.com/dms/image/C4E0DAQHFsVUV8qK2oQ/learning-public-crop_675_1200/0/1677626823709?e=2147483647&v=beta&t=iOWz9iUaE863pt-gX47-xkif7849UDZYy8Xrd-jexew 21 | -------------------------------------------------------------------------------- /Start/Ch_1/challenge_start.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Working With Data by Joe Marini 2 | # Programming challenge: summarize the earthquake data 3 | 4 | import json 5 | 6 | 7 | # for this challenge, we're going to summarize the earthquake data as follows: 8 | # 1: How many quakes are there in total? 9 | # 2: How many quakes were felt by at least 100 people? 10 | # 3: Print the name of the place whose quake was felt by the most people, with the # of reports 11 | # 4: Print the top 10 most significant events, with the significance value of each 12 | 13 | # open the data file and load the JSON 14 | with open("../../30DayQuakes.json", "r") as datafile: 15 | data = json.load(datafile) 16 | -------------------------------------------------------------------------------- /Start/Ch_1/filtering.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Working With Data by Joe Marini 2 | # using the filter() function to filter a data set 3 | 4 | import json 5 | 6 | 7 | def filterEvens(x): 8 | # filters out even numbers and keeps odd numbers 9 | if x % 2 == 0: 10 | return False 11 | return True 12 | 13 | 14 | def filterUppers(x): 15 | # filters out upper-case letters and keeps lower case letters 16 | if x.isupper(): 17 | return False 18 | return True 19 | 20 | 21 | # define some sample sequences to operate on 22 | nums = (1, 8, 4, 5, 13, 26, 381, 410, 58, 47) 23 | chars = "abcDeFGHiJklmnoP" 24 | 25 | # TODO: use filter to remove items from a list 26 | 27 | # TODO: use filter on non-numeric sequence 28 | 29 | # Use the filter on our data - let's filter out all seismic events that were *not* quakes 30 | # open the data file and load the JSON 31 | # with open("../../30DayQuakes.json", "r") as datafile: 32 | # data = json.load(datafile) 33 | -------------------------------------------------------------------------------- /Start/Ch_1/minmax.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Working With Data by Joe Marini 2 | # Demonstrates the usage of the min and max functions 3 | import json 4 | 5 | 6 | # Declare an array with some sample data in it 7 | values = [3.0, 2.5, 5.1, 4.1, 1.8, 1.6, 2.2, 5.7, 6.1] 8 | strings = ["one", "three", "five", "seven", "eleven", "eighteen"] 9 | 10 | 11 | # TODO: The min() function finds the minimum value 12 | 13 | 14 | # TODO: The max() function finds the maximum value 15 | 16 | 17 | # TODO: define a custom "key" function to extract a data field 18 | 19 | 20 | # TODO: open the data file and load the JSON 21 | # with open("../../30DayQuakes.json", "r") as datafile: 22 | # data = json.load(datafile) 23 | -------------------------------------------------------------------------------- /Start/Ch_1/sorting.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Working With Data by Joe Marini 2 | # sorting data with the sorted() and sort() functions 3 | 4 | import json 5 | 6 | 7 | numbers = [42, 54, 19, 17, 23, 31, 16, 4] 8 | names = ["Jeff", "Bill", "Addie", "Stephanie", "Zach", "Lukas", "Joe", "Stacy"] 9 | 10 | # TODO: the sorted() function can be used to return a new list with sorted data 11 | 12 | 13 | # TODO: alternately, you can use the list object's sort() method, which sorts the list in-place 14 | 15 | 16 | # TODO: To sort custom objects, we can tell the sort function which property to use 17 | # by specifying a key function 18 | 19 | # open the data file and load the JSON 20 | # with open("../../30DayQuakes.json", "r") as datafile: 21 | # data = json.load(datafile) 22 | 23 | 24 | # def getmag(dataitem): 25 | # magnitude = dataitem["properties"]["mag"] 26 | # if (magnitude is None): 27 | # magnitude = 0 28 | # return float(magnitude) 29 | -------------------------------------------------------------------------------- /Start/Ch_1/transform.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Working With Data by Joe Marini 2 | # using the map() function to transform data to another form 3 | 4 | import json 5 | import pprint 6 | 7 | 8 | def squareFunc(x): 9 | return x**2 10 | 11 | 12 | def toGrade(x): 13 | if (x >= 90): 14 | return "A" 15 | elif (x >= 80 and x < 90): 16 | return "B" 17 | elif (x >= 70 and x < 80): 18 | return "C" 19 | elif (x >= 65 and x < 70): 20 | return "D" 21 | return "F" 22 | 23 | 24 | # define some sample sequences to operate on 25 | nums = (1, 8, 4, 5, 13, 26, 381, 410, 58, 47) 26 | grades = (81, 89, 94, 78, 61, 66, 99, 74) 27 | 28 | # TODO: use map to create a new sequence of values 29 | 30 | # TODO: use sorted and map to change numbers to grades 31 | 32 | # Use the filter on our data - let's filter out all seismic events that were *not* quakes 33 | # open the data file and load the JSON 34 | # with open("../../30DayQuakes.json", "r") as datafile: 35 | # data = json.load(datafile) 36 | 37 | 38 | # filter the data down to the largest events 39 | # def bigmag(q): 40 | # return q['properties']['mag'] is not None and q['properties']['mag'] >= 6 41 | 42 | 43 | # results = list(filter(bigmag, data['features'])) 44 | 45 | # TODO: transform the largest events into a simpler structure 46 | -------------------------------------------------------------------------------- /Start/Ch_1/utility.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Working With Data by Joe Marini 2 | # demonstrates the use of the any, all, and sum functions 3 | import json 4 | 5 | values = [0, 1, 2, 3, 4, 5] 6 | 7 | # TODO: any() can be used to see if any value in a sequence is True 8 | 9 | 10 | # TODO: all() will detect if all of the values in a sequence are True 11 | 12 | 13 | # TODO: sum() can be use to add all of the values in a sequence 14 | 15 | 16 | # these utility functions don't have callbacks like min or max, 17 | # but we can use a generator for more fine control 18 | 19 | # open the data file and load the JSON 20 | # with open("../../30DayQuakes.json", "r") as datafile: 21 | # data = json.load(datafile) 22 | 23 | # TODO: are there any quake reports that were felt by more than 25,000 people? 24 | 25 | 26 | # TODO: how many quakes were felt by more than 500 people? 27 | 28 | 29 | # TODO: how many quakes had a magnitude of 6 or larger? 30 | -------------------------------------------------------------------------------- /Start/Ch_2/challenge_start.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Working With Data by Joe Marini 2 | # Programming challenge: use advanced data collections on the earthquake data 3 | 4 | import json 5 | 6 | 7 | # open the data file and load the JSON 8 | with open("../../30DayQuakes.json", "r") as datafile: 9 | data = json.load(datafile) 10 | -------------------------------------------------------------------------------- /Start/Ch_2/counter.py: -------------------------------------------------------------------------------- 1 | # Demonstrate the usage of Counter objects 2 | 3 | from collections import Counter 4 | 5 | 6 | # list of students in class 1 7 | class1 = ["Bob", "James", "Chad", "Darcy", "Penny", "Hannah", 8 | "Kevin", "James", "Melanie", "Becky", "Steve", "Frank"] 9 | 10 | # list of students in class 2 11 | class2 = ["Bill", "Barry", "Cindy", "Debbie", "Frank", 12 | "Gabby", "Kelly", "James", "Joe", "Sam", "Tara", "Ziggy"] 13 | 14 | # TODO: Create a Counter for class1 and class2 15 | 16 | # TODO: How many students in class 1 named James? 17 | 18 | # TODO: How many students are in class 1? 19 | 20 | # TODO: Combine the two classes 21 | 22 | # TODO: What's the most common name in the two classes? 23 | 24 | # TODO: Separate the classes again 25 | 26 | # TODO: What's common between the two classes? 27 | -------------------------------------------------------------------------------- /Start/Ch_2/defaultdict.py: -------------------------------------------------------------------------------- 1 | # Demonstrate the usage of defaultdict objects 2 | 3 | from collections import defaultdict 4 | 5 | 6 | # define a list of items that we want to count 7 | fruits = ['apple', 'pear', 'orange', 'banana', 8 | 'apple', 'grape', 'banana', 'banana'] 9 | 10 | # TODO: use a dictionary to count each element 11 | fruitCounter = dict() 12 | 13 | # TODO: Count the elements in the list 14 | for fruit in fruits: 15 | fruitCounter[fruit] += 1 16 | 17 | # TODO: print the result 18 | -------------------------------------------------------------------------------- /Start/Ch_2/deque.py: -------------------------------------------------------------------------------- 1 | # deque objects are like double-ended queues 2 | 3 | import collections 4 | import string 5 | 6 | 7 | # TODO: initialize a deque with lowercase letters 8 | 9 | # TODO: deques support the len() function 10 | 11 | # TODO: deques can be iterated over 12 | 13 | # TODO: manipulate items from either end 14 | 15 | # TODO: use an index to get a particular item 16 | -------------------------------------------------------------------------------- /Start/Ch_2/namedtuple.py: -------------------------------------------------------------------------------- 1 | # Demonstrate the usage of namdtuple objects 2 | 3 | import collections 4 | 5 | 6 | # TODO: create a Point namedtuple 7 | 8 | # TODO: use _replace to create a new instance 9 | -------------------------------------------------------------------------------- /Start/Ch_3/challenge_start.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Working With Data by Joe Marini 2 | # Programming challenge: use advanced data collections on the earthquake data 3 | 4 | import json 5 | import csv 6 | 7 | 8 | # open the data file and load the JSON 9 | with open("../../30DayQuakes.json", "r") as datafile: 10 | data = json.load(datafile) 11 | 12 | # Create a CSV file with the following information: 13 | # 40 most significant seismic events, ordered by most recent 14 | # Header row: Magnitude, Place, Felt Reports, Date, and Google Map link 15 | # Date should be in the format of YYYY-MM-DD 16 | -------------------------------------------------------------------------------- /Start/Ch_3/deserialize.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Working With Data by Joe Marini 2 | # read data from a CSV file into an object structure 3 | 4 | import csv 5 | import pprint 6 | 7 | 8 | # read the contents of a CSV file into an object structure 9 | result = [] 10 | 11 | # TODO: open the CSV file for reading 12 | 13 | 14 | pprint.pp(result) 15 | -------------------------------------------------------------------------------- /Start/Ch_3/serialize_csv.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Working With Data by Joe Marini 2 | # demonstrates how to serialize data to a CSV file 3 | 4 | import csv 5 | import json 6 | import datetime 7 | 8 | # read in the contents of the JSON file 9 | with open("../../30DayQuakes.json", "r") as datafile: 10 | data = json.load(datafile) 11 | 12 | 13 | def isbig(x): 14 | mag = x["properties"]["mag"] 15 | return mag is not None and mag > 5 16 | 17 | 18 | # Filter the data by quakes that are larger than 5 magnitude 19 | largequakes = list(filter(isbig, data["features"])) 20 | 21 | # TODO: Create the header and row structures for the data 22 | 23 | # TODO: populate the rows with the resulting quake data 24 | 25 | # TODO: write the results to the CSV file 26 | -------------------------------------------------------------------------------- /Start/Ch_3/serialize_json.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Working With Data by Joe Marini 2 | # demonstrates how to serialize data to a JSON file 3 | 4 | import json 5 | import datetime 6 | 7 | 8 | # read in the contents of the JSON file 9 | with open("../../30DayQuakes.json", "r") as datafile: 10 | data = json.load(datafile) 11 | 12 | 13 | def isbig(x): 14 | mag = x["properties"]["mag"] 15 | return mag is not None and mag > 6 16 | 17 | 18 | # TODO: define a function to transform complex JSON to simpler JSON 19 | 20 | 21 | # filter the data to only include large quakes 22 | largequakes = list(filter(isbig, data["features"])) 23 | # TODO: transform the data to a JSON format we want to save 24 | 25 | # TODO: use the dumps() function to write json to a string 26 | 27 | # TODO: use the dump() function to write json to a file 28 | -------------------------------------------------------------------------------- /Start/Ch_4/basiclog_start.py: -------------------------------------------------------------------------------- 1 | # demonstrate the logging api in Python 2 | 3 | # TODO: use the built-in logging module 4 | 5 | 6 | # TODO: Use basicConfig to configure logging 7 | 8 | # TODO: Try out each of the log levels 9 | 10 | # TODO: Output formatted strings to the log 11 | 12 | -------------------------------------------------------------------------------- /Start/Ch_4/customlog_start.py: -------------------------------------------------------------------------------- 1 | # Demonstrate how to customize logging output 2 | 3 | import logging 4 | 5 | # TODO: add another function to log from 6 | 7 | 8 | # set the output file and debug level, and 9 | # TODO: use a custom formatting specification 10 | logging.basicConfig(filename="output.log", 11 | level=logging.DEBUG) 12 | 13 | logging.info("This is an info-level log message") 14 | logging.warning("This is a warning-level message") 15 | 16 | --------------------------------------------------------------------------------