FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Python-2/web_programming/co2_emission.py at master · https-github-com-nzysoft/Python-2 · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
https-github-com-nzysoft
/
Python-2
Public
forked from
TheAlgorithms/Python
Notifications
You must be signed in to change notification settings
Fork
1
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Python-2
/
web_programming
/
co2_emission.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
25 lines (17 loc) · 716 Bytes
Breadcrumbs
Python-2
/
web_programming
/
co2_emission.py
Copy path
File metadata and controls
25 lines (17 loc) · 716 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
"""
Get CO2 emission data from the UK CarbonIntensity API
"""
from
datetime
import
date
import
requests
BASE_URL
=
"https://api.carbonintensity.org.uk/intensity"
# Emission in the last half hour
def
fetch_last_half_hour
()
->
str
:
last_half_hour
=
requests
.
get
(
BASE_URL
).
json
()[
"data"
][
0
]
return
last_half_hour
[
"intensity"
][
"actual"
]
# Emissions in a specific date range
def
fetch_from_to
(
start
,
end
)
->
list
:
return
requests
.
get
(
f"
{
BASE_URL
}
/
{
start
}
/
{
end
}
"
).
json
()[
"data"
]
if
__name__
==
"__main__"
:
for
entry
in
fetch_from_to
(
start
=
date
(
2020
,
10
,
1
),
end
=
date
(
2020
,
10
,
3
)):
print
(
"from {from} to {to}: {intensity[actual]}"
.
format
(
**
entry
))
print
(
f"
{
fetch_last_half_hour
()
=
}
"
)
Back
|
FazBrowse Home
|
New Git URL