Re: [Tutor] problem with python3.5 headfirst python 2nd ed chpt 10 test drive example decorator issues

2017-09-29 Thread Rajesh Balel
Hi

I don't see any session initializer , please try with that
session = web.session.Session(app,store,initializer={'login':
0,'privilege': 0,'username':'Guest','logged_in':False})

Regards
Rajesh

On Thu, Sep 28, 2017 at 3:35 PM, peter  wrote:

> I am on chapter 10 of headfirst python second edition. got most of the
> prior codes to work but am stuck on this one. I will add the
> simple_webapp.py which is a decorator enabled  and checker.py which is the
> decorator. when I go into 127.0.0.1:5000 and enter I get the correct
> response. 127.0.0.1:5000/page1 gets me 'you are not logged in' also
> correct.
>
> '127.0.0.1:5000/login' returns 'you are now logged in'  which is correct
> but '127.0.0.1:5000/page1' after that should return 'this is page 1' but
> instead returns 'you are not logged in'.
>
> When I login to page 1 the 'session['logged_in'] = True' should still be
> true but apparently it has not been passed to 'check_logged_in'.
>
> I am stumped and do not know how to proceed to figure this out.
>
> Here are the codes
>
> Also they are attached to this email. I am hoping someone can show me the
> errors of my ways. hopefully it is something simple but I have gone over it
> a lot and think the code is correct.
>
> Thank you for your attention and help.
>
> Peter Risley
>
> checker.py
>
> from flask import session
> from functools import wraps
>
> def check_logged_in(func):
> @wraps(func)
> def wrapper(*args, **kwargs):
> if 'logged _in' in session:
> return func(*args, **kwargs)
> return 'You are not logged in.'
> return wrapper
>
> simple_webapp.py
>
> from flask import Flask, session
> from checker import check_logged_in
>
> """this 'simple_webapp.py' , which pulls all of chp 10 code together. When
> you need to restrict access to specific URLs, base your strategy on this
> webapp's mechanism.
> This uses checker.py check_logged_in and which is a decorator function to
> do the work."""
>
>
>
> app = Flask(__name__)
>
> @app.route('/')
> def hello() -> str:
> return 'Hello from the simple webapp.'
>
>
> @app.route('/page1')
> @check_logged_in
> def page1():
> return 'this is page 1.'
>
> @app.route('/page2')
> @check_logged_in
> def page2():
> return 'this is page 2.'
>
> @app.route('/page3')
> @check_logged_in
> def page3():
> return 'this is page 3.'
>
>
> @app.route('/login')
> def do_login() -> str:
> session['logged_in'] = True
> return 'you are now logged in.'
>
>
> @app.route('/logout')
> def do_logout() -> str:
> session.pop('logged_in')
> return 'you are now logged out.'
>
> app.secret_key = 'yes'
>
> if __name__ == '__main__':
> app.run(debug=True)
>
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sentiment analysis read from a file

2018-03-28 Thread Rajesh Balel
seems you have "tab  separated data

with open('Training.txt') as f:
  my_data = [x.strip().split('\t') for x in f.readlines()]

for x in my_data: print x,

Regards
Rajesh


On Wed, Mar 28, 2018 at 10:14 AM, Peter Otten <__pete...@web.de> wrote:

> Alan Gauld via Tutor wrote:
>
> > On 28/03/18 11:07, theano orf wrote:
> >> I am new in python and I am having problems of how to read a txt file
> and
> >> insert the data in a list,
> >
> > Just a quick response, but your data is more than a text file its a CSV
> > file so the rules change slightly. Especially since you are using the csv
> > module.
> >
> > Your data file is not a CSV file - it is just space separated and the
> > string is not quoted so the CSV default mode of operation won;t
> > work on this data as you seem to expect it to,. You will need to
> > specify the separator (as what? A space wiill split on each word...)
> > CSV might not be the best option here a simple string split combined
> > with slicing might be better.
>
> >>> next(open("training.txt"))
> '1\tThe Da Vinci Code book is just awesome.\n'
>
> So the delimiter would be TAB:
>
> >>> import csv
> >>> next(csv.reader(open("training.txt"), delimiter="\t"))
> ['1', 'The Da Vinci Code book is just awesome.']
>
> >> with open("training.txt", 'r') as file:
> >
> > The CSV module prefers binary files so open it with mode 'rb' not 'r'
>
> That's no longer true for Python 3:
>
> >>> next(csv.reader(open("training.txt", "rb"), delimiter="\t"))
> Traceback (most recent call last):
>   File "", line 1, in 
> _csv.Error: iterator should return strings, not bytes (did you open the
> file
> in text mode?)
>
> However, as csv still does its own newline handling it's a good idea to get
> into the habit of opening the file with newline="" as explained here:
>
> https://docs.python.org/dev/library/csv.html#id3
>
> >> reviews = list(csv.reader(file))
> >
> > Try printing the first 2 lines of reviews to check what you have.
> > I suspect it's not what you think.
> >
> >>positive_review = [r[1] for r in reviews if r[0] == str(1)]
> >
> > str(1) is just '1' so you might as well just use that.
> >
> >> after the print I only take an empty array. Why is this happening? I am
> >> attaching also the training.txt file
> >
> > See the comments above about your data format.
> >
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor