Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
SciProg2024
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
pclark3
SciProg2024
Commits
47a7d596
Commit
47a7d596
authored
1 year ago
by
cprutean
Browse files
Options
Downloads
Patches
Plain Diff
Replace ReadScope.ipynb
parent
6a2222c3
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
CodeExamples/ReadScope.ipynb
+20
-11
20 additions, 11 deletions
CodeExamples/ReadScope.ipynb
with
20 additions
and
11 deletions
CodeExamples/ReadScope.ipynb
+
20
−
11
View file @
47a7d596
...
@@ -5,7 +5,7 @@
...
@@ -5,7 +5,7 @@
"id": "1c15801d",
"id": "1c15801d",
"metadata": {},
"metadata": {},
"source": [
"source": [
"# Read
Text Fil
e"
"# Read
Scop
e"
]
]
},
},
{
{
...
@@ -15,22 +15,31 @@
...
@@ -15,22 +15,31 @@
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
"\"\"\"\"\n",
" Example to show reading a global variable across main and a function.\n",
"\"\"\"\n",
"\"\"\"\n",
"
Read a text file and print out to terminal
\n",
"
import math
\n",
"
\"\"\"
\n",
"
wavelength = 0.65 # Define a global float
\n",
"\n",
"\n",
"def main():\n",
" filename = str(input(\"Input file name : \"))\n",
" filein = open(filename, \"r\") # Open file for reading\n",
"\n",
"\n",
"\n",
"\n",
" input_data = filein.readlines() # Read in data as list of strings\n",
"def planck(temperature):\n",
" c_one = 5.029e6 # 2 h c^2 \n",
" c_two = 1.44e4 # hc/k\n",
" #\n",
" # Can access wavelength inside function\n",
" val = c_one/math.pow(wavelength,5)*(1.0/(math.exp(c_two/(wavelength*temperature)) - 1.0))\n",
" return val\n",
"\n",
"\n",
" for line in input_data: # Scan through writing lines\n",
" \n",
" print(line) # Note extra \"\\n\" problem \n",
"\n",
"\n",
" for line in input_data:\n",
"def main():\n",
" print (line[:-1]) # This removes the last character\n",
" #\n",
" # Can access wavelength inside main\n",
" print(\"Plank intensity at : \" + str(wavelength))\n",
" for t in range(300,340):\n",
" val = planck(t)\n",
" print(\"Value at : \" + str(t) + \" is \" + str(val))\n",
"\n",
"\n",
"main()"
"main()"
]
]
...
...
%% Cell type:markdown id:1c15801d tags:
%% Cell type:markdown id:1c15801d tags:
# Read
Text Fil
e
# Read
Scop
e
%% Cell type:code id:004b1951 tags:
%% Cell type:code id:004b1951 tags:
```
python
```
python
""""
Example to show reading a global variable across main and a function.
"""
"""
Read a text file and print out to terminal
import
math
"""
wavelength
=
0.65
# Define a global float
def
main
():
filename
=
str
(
input
(
"
Input file name :
"
))
filein
=
open
(
filename
,
"
r
"
)
# Open file for reading
input_data
=
filein
.
readlines
()
# Read in data as list of strings
def
planck
(
temperature
):
c_one
=
5.029e6
# 2 h c^2
c_two
=
1.44e4
# hc/k
#
# Can access wavelength inside function
val
=
c_one
/
math
.
pow
(
wavelength
,
5
)
*
(
1.0
/
(
math
.
exp
(
c_two
/
(
wavelength
*
temperature
))
-
1.0
))
return
val
for
line
in
input_data
:
# Scan through writing lines
print
(
line
)
# Note extra "\n" problem
for
line
in
input_data
:
print
(
line
[:
-
1
])
# This removes the last character
def
main
():
#
# Can access wavelength inside main
print
(
"
Plank intensity at :
"
+
str
(
wavelength
))
for
t
in
range
(
300
,
340
):
val
=
planck
(
t
)
print
(
"
Value at :
"
+
str
(
t
)
+
"
is
"
+
str
(
val
))
main
()
main
()
```
```
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment