defload(name):""" This method creates and loads a new journal. :param name: The base name of the journal to load. :return: A new journal data structure populated with the file data. """
importrequestsimportbs4url='https://www.wunderground.com/weather-forecast/72202'# get pageresponse=requests.get(url)# parse html (reponse.text) into DOMsoup=bs4.BeautifulSoup(response.text,'html.parser')# get location, condition, temp, scale (F vs C)loc=soup.find(class_='region-content-header').find('h1').get_text()condition=soup.find(class_='condition-icon').get_text()temp=soup.find(class_='wu-unit-temperature').find(class_='wu-value').get_text()scale=soup.find(class_='wu-unit-temperature').find(class_='wu-label').get_text()
importplatformifplatform.system()=='Darwin':subprocess.call(['open',folder])elifplatform.system()=='Windows':subprocess.call(['explorer',folder])elifplatform.system()=='Linux':subprocess.call(['xdg-open',folder])else:print("We don't support your os: "+platform.system())
classDragon(Creature):def__init__(self,name,level,scale_thickness):super().__init__(name,level)# initializer for Creatureself.scale_thickness=scale_thicknessdefbreath_fire():...
# all types derive from Creaturecreatures=[SmallAnimal('Toad',1),Creature('Tiger',12),SmallAnimal('Bat',3),Dragon('Dragon',50,75),Wizard('Evil Wizard',1000)]wiard=Wizard('Gandolf',22)forcincreatures:wizard.attack(c)# Wizard knows how to battle any type of Creature# Duck Typing# "If it looks/acts like a Duck -- it's a duck"# wizard.attack(c) in most languages would require 'c'# to have inherited from Creature, given that .attack()# takes a creature. Python's Duck Typing takes# 'things that look like creatures'
# yield makes fibonacci a generator methoddeffibonacci(limit):current=0next=1# after item is returned and processed,# execution returns and resumeswhilecurrent<limit:current,next=next,next+currentyieldcurrent# yield keyword returns one element of a sequence
info=dict()info['age']=42info['loc']='Italy'info=dict(age=42,loc='Italy')info={'age':42,'loc':'Italy'}location=info['loc']if'age'ininfo:# test for key# use info['age']
try:importstatistics# only Python 3.4.3+except:importstatistics_2_stand_inasstatisticsnumbers=[1,6,99,...,5]the_avg=statistics.mean(numbers)# statistics_2_stand_in.pydefmean(lst):# your implementation of mean here...returnavg
try:search=input("Movie search text (x to exit): ")ifsearch!='x':results=movie_svc.find_movies(search)print("Found {} results.".format(len(results)))forrinresults:print("{} -- {}".format(r.year,r.title))print()# Order from more specfic to more generalexceptValueError:print("Error: Search text is required.")exceptrequests.exceptions.ConnectionError:print("Error: Your network is down.")exceptExceptionasx:print("Unexpected error. Details: {}".format(x))
deffind_movies(search_text):ifnotsearch_textornotsearch_text.strip():raiseValueError("Search text is required.")# raise will immediately return from function with error