Re: [Tutor] what am I not understanding?

2014-10-20 Thread Peter Otten
Clayton Kirkwood wrote:

> raw_table = ('''
> a: Asky: Dividend Yield
> b: Bid d: Dividend per Share
> b2: Ask (Realtime)   r1: Dividend Pay Date
> b3: Bid (Realtime)q: Ex-Dividend Date
> p: Previous Close
> o: Open''')

> o: Open#why aren't both the \t and \n
> being removed? (tried with and without the square brackets)

> I am trying to get to where I can create a dict using the ':' separator

You never state your actual problem with sufficient precision, so I'm going 
to assume:

- Create a Python dict from some text
- Key-value pairs are separated by tabs or newlines
- Keys are separated from values by a colon
- Remaining whitespace surrounding keys and values needs to be stripped

So

>>> raw_table = '''\
... a: Ask\t y: Dividend Yield
... b: Bid\td: Dividend per Share
... b2: Ask (Realtime)\t r1: Dividend Pay Date
... b3: Bid (Realtime)\tq: Ex-Dividend Date
... p: Previous Close
... o: Open'''

I used \t as tabs, but literal tabs will work, too. The backslash in the 
first line is to avoid the empty first line. alternatively remove it and 
other leading/trailing whitespace with raw_table = raw_table.strip()

>>> pairs = [pair for line in raw_table.splitlines() for pair in 
line.split("\t")]
>>> pairs
['a: Ask', ' y: Dividend Yield', 'b: Bid', 'd: Dividend per Share', 'b2: Ask 
(Realtime)', ' r1: Dividend Pay Date', 'b3: Bid (Realtime)', 'q: Ex-Dividend 
Date', 'p: Previous Close', 'o: Open']
>>> pairs = [pair.partition(":")[::2] for pair in pairs]

pair.split(":") instead of partition()[::2] would work, too.

>>> pairs
[('a', ' Ask'), (' y', ' Dividend Yield'), ('b', ' Bid'), ('d', ' Dividend 
per Share'), ('b2', ' Ask (Realtime)'), (' r1', ' Dividend Pay Date'), 
('b3', ' Bid (Realtime)'), ('q', ' Ex-Dividend Date'), ('p', ' Previous 
Close'), ('o', ' Open')]
>>> d = {k.strip(): v.strip() for k, v in pairs}
>>> d
{'b2': 'Ask (Realtime)', 'q': 'Ex-Dividend Date', 'b3': 'Bid (Realtime)', 
'r1': 'Dividend Pay Date', 'd': 'Dividend per Share', 'y': 'Dividend Yield', 
'p': 'Previous Close', 'b': 'Bid', 'a': 'Ask', 'o': 'Open'}


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Python Help

2014-10-20 Thread Taylor Ruzgys
Hi, I was wondering if you could help me with an assignment that I'm doing 
involving Python?  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Help

2014-10-20 Thread Alan Gauld

On 20/10/14 03:56, Taylor Ruzgys wrote:

Hi, I was wondering if you could help me with an assignment that I'm
doing involving Python?


Yes, we can help you do it. We won't do it for you.

You need to tell us what the assignment is, how you have tried
to solve it, including any code you've written. Explain where
and how you are stuck.

Include the full text of any error messages.
Tell us the Python version and OS you use.
Tell us if you are using any non standard libraries/modules


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Insert time into email

2014-10-20 Thread Bo Morris
hello all, hope everyone is doing well.

The below code works, however I am going back and trying to enter the time
and date and I cant quite figure out how to do this without breaking the
code.

#!/usr/bin/python

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
import time

strFrom = "HourlyReport.com"

#strTo = "engineer...@oneconnxt.com"
#strTo = "mmed...@onemediacorpinc.com"
strTo = "b...@onemediacorpinc.com"

l = ['3102EHD-01108.png', '3102DHD-01109.png','3102EHD-01082.png',
'3102DHD-01033.png', '3102EHD-01302.png', '3102DHD-01149.png',
'3102EHD-01125.png', '3102DHD-01144.png', '3102EHD-01105.png']

t = time.strftime("%H:%M:%S")
d = time.strftime("%d/%m/%Y")

msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = 'Test Hourly Report'
msgRoot['From'] = strFrom
msgRoot['To'] = strTo
msgRoot.preamble = 'This is a multi-part message in MIME format.'

msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)

msgText = MIMEText('This is the alternative plain text message.')
msgAlternative.attach(msgText)

msgText = MIMEText('TIMEDATETIMEDATETIMEDATETIMEDATETIMEDATETIMEDATETIMEDATETIMEDATETIMEDATE',
'html')
msgAlternative.attach(msgText)

for image in l:
with open(image, 'rb') as fh:
msgImage = MIMEImage(fh.read())
msgImage.add_header('Content-ID', '<{0}>'.format(image))
msgRoot.attach(msgImage)


try:
   smtpObj = smtplib.SMTP('localhost')
   smtpObj.sendmail(strFrom, strTo, msgRoot.as_string())
   print "Successfully sent email"
except smtplib.SMTPException:
   print "Error: unable to send email"

I need to enter the time and date in the html where "TIME" and "DATE" are.
I imagine I can do this by adding "cid: t" and "cid:d" which just refers
back to t = "time.strftime("%H:%M:%S")" "d = time.strftime("%d/%m/%Y")"?

Thanks in advance for any help.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Insert time into email

2014-10-20 Thread Chris “Kwpolska” Warrick
On Mon, Oct 20, 2014 at 2:34 PM, Bo Morris  wrote:
> hello all, hope everyone is doing well.
>
> The below code works, however I am going back and trying to enter the time
> and date and I cant quite figure out how to do this without breaking the
> code.
>
> #!/usr/bin/python
>
> import smtplib
> from email.MIMEMultipart import MIMEMultipart
> from email.MIMEText import MIMEText
> from email.MIMEImage import MIMEImage
> import time
>
> strFrom = "HourlyReport.com"

PS. You may want to use a real e-mail address here.  Or, at the very
least, something that looks like one.

> #strTo = "engineer...@oneconnxt.com"
> #strTo = "mmed...@onemediacorpinc.com"
> strTo = "b...@onemediacorpinc.com"
>
> l = ['3102EHD-01108.png', '3102DHD-01109.png','3102EHD-01082.png',
> '3102DHD-01033.png', '3102EHD-01302.png', '3102DHD-01149.png',
> '3102EHD-01125.png', '3102DHD-01144.png', '3102EHD-01105.png']
>
> t = time.strftime("%H:%M:%S")
> d = time.strftime("%d/%m/%Y")
>
> msgRoot = MIMEMultipart('related')
> msgRoot['Subject'] = 'Test Hourly Report'
> msgRoot['From'] = strFrom
> msgRoot['To'] = strTo
> msgRoot.preamble = 'This is a multi-part message in MIME format.'
>
> msgAlternative = MIMEMultipart('alternative')
> msgRoot.attach(msgAlternative)
>
> msgText = MIMEText('This is the alternative plain text message.')
> msgAlternative.attach(msgText)
>
> msgText = MIMEText(' src="cid:3102EHD-01108.png" width="400"
> height="300">TIMEDATE src="cid:3102DHD-01109.png" width="400"
> height="300">TIMEDATE cellspacing="15" border="1"> width="400"
> height="300">TIMEDATE src="cid:3102DHD-01033.png" width="400"
> height="300">TIMEDATE cellspacing="15" border="1"> width="400"
> height="300">TIMEDATE src="cid:3102DHD-01149.png" width="400"
> height="300">TIMEDATE cellspacing="15" border="1"> width="400"
> height="300">TIMEDATE src="cid:3102DHD-01144.png" width="400"
> height="300">TIMEDATE cellspacing="15" border="1"> width="400"
> height="300">TIMEDATE',
> 'html')
> msgAlternative.attach(msgText)
>
> for image in l:
> with open(image, 'rb') as fh:
> msgImage = MIMEImage(fh.read())
> msgImage.add_header('Content-ID', '<{0}>'.format(image))
> msgRoot.attach(msgImage)
>
>
> try:
>smtpObj = smtplib.SMTP('localhost')
>smtpObj.sendmail(strFrom, strTo, msgRoot.as_string())
>print "Successfully sent email"
> except smtplib.SMTPException:
>print "Error: unable to send email"
>
> I need to enter the time and date in the html where "TIME" and "DATE" are. I
> imagine I can do this by adding "cid: t" and "cid:d" which just refers back
> to t = "time.strftime("%H:%M:%S")" "d = time.strftime("%d/%m/%Y")"?

No, not really.  cid: is for images.  You want to insert actual text.
Use string formatting.

'…snip…{time}{date}…snip…'.format(time=t, date=d)

This is assuming every date is the same.  If it isn’t, you’d have to
use different identifiers and values.

-- 
Chris “Kwpolska” Warrick 
PGP: 5EAAEA16
stop html mail | always bottom-post | only UTF-8 makes sense
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Python sqlite3 issue

2014-10-20 Thread Juan Christian
I have this code (http://pastebin.com/Q21vQdHZ):

import sqlite3

db = sqlite3.connect('db.sqlite')


def create_db():
db.execute('''
CREATE TABLE TOPICS(
ID INT PRIMARY KEY NOT NULL,
URL VARCHAR NOT NULL,
AUTHOR VARCHAR NOT NULL,
MESSAGE VARCHAR NOT NULL
);
''')


def insert_db(_id, url, author, message):
db.execute("INSERT INTO TOPICS (ID, URL, AUTHOR, MESSAGE) VALUES ({},
{}, {}, {})".format(_id, url, author, message))
db.commit()


def get_db(_id):
cursor = db.execute("SELECT ID, URL, AUTHOR, MESSAGE FROM TOPICS WHERE ID =
{}".format(_id))
return cursor.fetchone()


if __name__ == '__main__':
create_db()
insert_db(12, 'abc.com', 'a', 'b')
get_db(12)
db.close()

And when I try to execute it I get:

First time executing:

Traceback (most recent call last):
  File ".\sql.py", line 29, in 
insert_db(12, 'abc.com', 'a', 'b')
  File ".\sql.py", line 18, in insert_db
db.execute("INSERT INTO TOPICS (ID, URL, AUTHOR, MESSAGE) VALUES ({},
{}, {}, {})".format(_id, url, author, message)
)
sqlite3.OperationalError: no such column: abc.com


Second time executing:

Traceback (most recent call last):
  File ".\sql.py", line 28, in 
create_db()
  File ".\sql.py", line 14, in create_db
''')
sqlite3.OperationalError: table TOPICS already exists


What's the problem? It's just a test script just to learn sqlite3 with
python.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python sqlite3 issue

2014-10-20 Thread Danny Yoo
Hi Juan,



On Mon, Oct 20, 2014 at 10:04 AM, Juan Christian
 wrote:
> I have this code (http://pastebin.com/Q21vQdHZ):
>
> import sqlite3

[code cut]

> def insert_db(_id, url, author, message):
> db.execute("INSERT INTO TOPICS (ID, URL, AUTHOR, MESSAGE) VALUES ({},
> {}, {}, {})".format(_id, url, author, message))

[code cut]


Ah.  Do not use normal string formatting when you're dealing with SQL.
In web programming, we can introduce script injection problems when we
use naive string concatenation.  Unsurprisingly, the same class of
injection attack can be done in SQL when we use naive string
concatenation to build SQL statements.  See:

http://xkcd.com/327/

I'm being serious!  :P

Look for the concept of "prepared statements" or "parameter
substitution" or "placeholder" in the sqllite3 Python bindings: it
should let you safely construct the statement templates.

In the documentation here:

 https://docs.python.org/2/library/sqlite3.html

search for the word "placeholder", and you should see the relevant material.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Insert time into email

2014-10-20 Thread Chris “Kwpolska” Warrick
Forwarding to list — please use Reply All… next time.

On Mon, Oct 20, 2014 at 6:28 PM, Crush  wrote:
> Where does the ."format(time=t,date=d)" go?
>
> I would assume something like
>
> msgTime = MIMEText()
> msgTime.add_header('not sure what to put here?' '' .format(time=t))
> msgRoot.attach(msgTime)
>
> then in the actual html, something like...
> {time}?
>
> and yes, for simplicity sake, the date and time will be the same.

Do not bother with MIME for this.  It WON’T be simpler.  (it’s most
likely impossible, in fact.)

msgText = MIMEText('…rest of your
HTML here, complete with {time} and {date}
tags…'.format(time=t, date=d), 'html')
msgAlternative.attach(msgText)

-- 
Chris “Kwpolska” Warrick 
PGP: 5EAAEA16
stop html mail | always bottom-post | only UTF-8 makes sense
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python sqlite3 issue

2014-10-20 Thread Joel Goldstick
On Mon, Oct 20, 2014 at 1:04 PM, Juan Christian
 wrote:
> I have this code (http://pastebin.com/Q21vQdHZ):
>
> import sqlite3
>
> db = sqlite3.connect('db.sqlite')
>
>
> def create_db():
> db.execute('''
> CREATE TABLE TOPICS(
> ID INT PRIMARY KEY NOT NULL,
> URL VARCHAR NOT NULL,
> AUTHOR VARCHAR NOT NULL,
> MESSAGE VARCHAR NOT NULL
> );
> ''')
>
>
> def insert_db(_id, url, author, message):
> db.execute("INSERT INTO TOPICS (ID, URL, AUTHOR, MESSAGE) VALUES ({},
> {}, {}, {})".format(_id, url, author, message))

The above line looks suspect.  I rewrote like so:
>>> def insert_db(_id, url, author, message):
   print ("INSERT INTO TOPICS (ID, URL, AUTHOR, MESSAGE)
VALUES ({}, {}, {}, {})".format(_id, url, author, message))
...
>>> insert_db(12, "abc.com", "author", "message")
INSERT INTO TOPICS (ID, URL, AUTHOR, MESSAGE) VALUES (12, abc.com,
author, message)
>>>

I've never used format like that.  It looks like you need to quote the
strings.  I don't know if you can tell format to do that or if you
have to escape them.
> db.commit()
>
>
> def get_db(_id):
> cursor = db.execute("SELECT ID, URL, AUTHOR, MESSAGE FROM TOPICS WHERE ID =
> {}".format(_id))
> return cursor.fetchone()
>
>
> if __name__ == '__main__':
> create_db()
> insert_db(12, 'abc.com', 'a', 'b')
> get_db(12)
> db.close()
>
> And when I try to execute it I get:
>
> First time executing:
>
> Traceback (most recent call last):
>   File ".\sql.py", line 29, in 
> insert_db(12, 'abc.com', 'a', 'b')
>   File ".\sql.py", line 18, in insert_db
> db.execute("INSERT INTO TOPICS (ID, URL, AUTHOR, MESSAGE) VALUES ({},
> {}, {}, {})".format(_id, url, author, message)
> )
> sqlite3.OperationalError: no such column: abc.com
>
>
> Second time executing:
>
> Traceback (most recent call last):
>   File ".\sql.py", line 28, in 
> create_db()
>   File ".\sql.py", line 14, in create_db
> ''')
> sqlite3.OperationalError: table TOPICS already exists

This makes sense since the create sql code created TABLE the first time
>
>
> What's the problem? It's just a test script just to learn sqlite3 with
> python.
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 
Joel Goldstick
http://joelgoldstick.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python sqlite3 issue

2014-10-20 Thread Danny Yoo
 insert_db(12, "abc.com", "author", "message")
> INSERT INTO TOPICS (ID, URL, AUTHOR, MESSAGE) VALUES (12, abc.com,
> author, message)

>
> I've never used format like that.  It looks like you need to quote the
> strings.  I don't know if you can tell format to do that or if you
> have to escape them.


In normal situations, this might be good advice.  When the string
being produced is itself to be interpreted as code, though, we want to
see if there's already some library to do the templating and quoting
for us already.  Otherwise, it is extraordinarily easy to leave an
"injection attack" vulnerability.

It doesn't even have to be one with malicious intent.  See the
following from way back in 2005:

https://mail.python.org/pipermail/tutor/2005-June/039213.html

In this case, getting it wrong just means that certain good inputs are
treated incorrectly.  So there's good reason to do this just so that
our programs work.

This is one of those issues that a programmer has to be aware of, to
treat data with the proper respect and wariness.  "Code is data, and
data is code," is one of the mantras that the Lisp programmers use.
When data becomes code, that's when we have to be especially careful.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python sqlite3 issue

2014-10-20 Thread Juan Christian
Ok, new code using ?:

import sqlite3

db = sqlite3.connect('db.sqlite')


def create_db():
db.execute('''
 CREATE TABLE TOPICS(
 ID INT PRIMARY KEY NOT NULL,
 URL VARCHAR NOT NULL,
 AUTHOR VARCHAR NOT NULL,
 MESSAGE VARCHAR NOT NULL
 );
 ''')


def insert_db(_id, url, author, message):
db.execute("INSERT INTO TOPICS (ID, URL, AUTHOR, MESSAGE) VALUES (?, ?,
?, ?)", (_id, url, author, message))
db.commit()


def get_db(_id):
cursor = db.execute("SELECT ID, URL, AUTHOR, MESSAGE FROM TOPICS WHERE ID =
?", (_id))
return cursor.fetchone()


if __name__ == '__main__':
create_db()
insert_db(12, 'abc.com', 'a', 'b')
print(get_db(12))
db.close()

-

But now when I execute I get

Traceback (most recent call last):
  File ".\sql.py", line 30, in 
print(get_db(12))
  File ".\sql.py", line 23, in get_db
cursor = db.execute("SELECT ID, URL, AUTHOR, MESSAGE FROM TOPICS WHERE
ID = ?", (_id))
ValueError: parameters are of unsupported type

-

And the second time, again, I get

Traceback (most recent call last):
  File ".\sql.py", line 28, in 
create_db()
  File ".\sql.py", line 14, in create_db
''')
sqlite3.OperationalError: table TOPICS already exists

On Mon, Oct 20, 2014 at 4:08 PM, Danny Yoo  wrote:

>  insert_db(12, "abc.com", "author", "message")
> > INSERT INTO TOPICS (ID, URL, AUTHOR, MESSAGE) VALUES (12, abc.com,
> > author, message)
> 
> >
> > I've never used format like that.  It looks like you need to quote the
> > strings.  I don't know if you can tell format to do that or if you
> > have to escape them.
>
>
> In normal situations, this might be good advice.  When the string
> being produced is itself to be interpreted as code, though, we want to
> see if there's already some library to do the templating and quoting
> for us already.  Otherwise, it is extraordinarily easy to leave an
> "injection attack" vulnerability.
>
> It doesn't even have to be one with malicious intent.  See the
> following from way back in 2005:
>
> https://mail.python.org/pipermail/tutor/2005-June/039213.html
>
> In this case, getting it wrong just means that certain good inputs are
> treated incorrectly.  So there's good reason to do this just so that
> our programs work.
>
> This is one of those issues that a programmer has to be aware of, to
> treat data with the proper respect and wariness.  "Code is data, and
> data is code," is one of the mantras that the Lisp programmers use.
> When data becomes code, that's when we have to be especially careful.
> ___
> 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] Python sqlite3 issue

2014-10-20 Thread Alan Gauld

On 20/10/14 18:04, Juan Christian wrote:


CREATE TABLE TOPICS(
ID INT PRIMARY KEY NOT NULL,


This means SQLite will automatically create the ID value,
you do not need to provide one.



URL VARCHAR NOT NULL,
AUTHOR VARCHAR NOT NULL,
MESSAGE VARCHAR NOT NULL
);
''')


def insert_db(_id, url, author, message):
 db.execute("INSERT INTO TOPICS (ID, URL, AUTHOR, MESSAGE) VALUES
({}, {}, {}, {})".format(_id, url, author, message))
 db.commit()


So in the insert you should only provide the 3 values
URL, AUTHOR, MESSAGE

The use of format is also a mistake but I see that Danny has
already pointed that out.

Finally when creating tables you can use:

DROP TABLE IF EXISTS TOPICS;

Prior to creating it. That will ensure you don't get an existing
table error.

Some other databases provide other, more conservative, mechanisms
for safely creating tables but SQLite doesn't support them so dropping
the table and then recreating it is your best option.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python sqlite3 issue

2014-10-20 Thread Alan Gauld

On 20/10/14 18:04, Juan Christian wrote:


What's the problem? It's just a test script just to learn sqlite3 with
python.


If you are learning SQLite3 as well as how to access it from Python I 
strongly recommend installing the sqlite3 interpreter and typing the SQL 
commands directly into that. Once you are happy with the SQL you can 
easily transfer that to Python.


In practice I rarely issue CREATE, DROP or ALTER commands from within 
Python. I put those in SQL scripts and execute them from the sqlite3 
interpreter.



--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Insert time into email

2014-10-20 Thread Crush
Hey thanks for the help, it worked like a charm. I will post the complete code 
soon, so others may benefit should they ever need it. 

Thanks again. 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor