CH5 section3 How the function functions?

1. define the class fields

Now we have the database, it's time to creat the table with db.Model.

class Weatherapi(db.Model):
#define the table-weatherapi to the class.model - Weatherapi
    __tablename__ = 'weatherapi'
    id = db.Column(db.Integer, primary_key=True) 
    onlinetime = db.Column(db.String(64),index=True) 
    cityname = db.Column(db.String(64), index=True) 
    weather = db.Column(db.String(64), index=True) 
    temp = db.Column(db.String(64), index=True) 
    wind = db.Column(db.String(64), index=True) 

def __init__(self, id,onlinetime,cityname,weather,temp,wind):

Onlinetime/cityname.../wind are defined as class variables, the type and the number depend on the result that returen by the API. Fields are created as instances of the db.Column class(take your time to understand...)

2. Decide which one is the main code

This is the most important part, you must have strong logical thinking ability and the ability to prepare the code. I almost failed myself and die on the function that use to store the data.

I have so many code:

  1. one for fetch the weather data from openweathermap.com(ch3 - api.py);
  2. one for receive-handing logic data information about the Web request(ch4 - web.py);
  3. one for setting the html page(cha4 - index.html).

Questions:

  1. Which one is the main code in ch5, or I have to write another one?
  2. I have already written the code for creating the database and the table-class, so can I keep writing code in this document or,I need to write code in the new document?
  3. How to write the function which will contain code to process the results of weather information passed back from the server, and then record them to the database?

These quetions are killing me ,since the tutorial only show me how it works on the command, there is no example of function.

The session here is not the Flask session, but the Flask-SQLAlchemy one. It is essentially a beefed up version of a database transaction. This is how it works:

    >>> from yourapp import User
    >>> me = User('admin', '[email protected]')
    >>> db.session.add(me)
    >>> db.session.commit()

3. How the function functions?

I read all the tutorial,but all the answer pointed to NONE.

This is the first time I felt so disappointed in myself, I even creat an issue for it.

The tips from faketooth teacher enlightened me to write half code of this function, he even gave me one more tutorial,but I still don't know how to finish it.

It readlly broke my heart.I doubt the meaning of learning python and wonder what my life was worth.

I have to do something instead of crying,all I need is just a single matching point, that is :tell me the whole process how it works and then I will try my best to write the damn function to receive the data.

Is there ANYBODY out there? to help me..... (Pink Floyd)

Of course.

The one who rescued me is @simpleowen(大猫)!! AGAIN!!

And since he uesed SQL language to finish his task of ch5, so basiclly it seemed inconceivable that the way he taught me how to code is to read and learn with the tutorial that it's totally new/fresh to him(!!) and teach someone immediately!!

AWESOME!!

Well, so that's the story how I met a 'study overlord'.

Here you are, the function you deeply desired:

def insertweatherapi(data):
    #data = fetchAPI(cityname)
    onlinetime = data[0]
    cityname = data[1]
    weather = data[2]
    temp = data[3]
    wind = data[4]
    w = Weatherapi(1,onlinetime,cityname,weather,temp,wind)
    db.session.add(w)
    db.session.commit()
    db.session.close()

and the one update the data:

def updatweatherapi(l):
    cityname = l[0]
    weather = l[1]
    h = Weatherapi.query.filter_by(cityname=l[0]).first()
    h.weather = l[1]
    db.session.add(h)
    db.session.commit()
    db.session.close()

It's pure and simple,right ?

But they ran out of my patience, I almost quit myself on python so thanks to @simpleowen, you are the hero to me.

What I learned that afternoon maybe a small but significant step to me.

results matching ""

    No results matching ""