[Tutor] pythonic

2018-03-29 Thread Pat Martin
Hello all,

I have written the following program. It generates a template for Pelican
web site static generator. It works just fine, it generates the template
and then I put the info in it to customize. But I was wondering, is this
the "right" way to do it in python?

#!/usr/bin/env python3
"""Generate a Pelican markdown base page."""

import argparse
import datetime


def Main():
"""Run if run as a program."""
parser = argparse.ArgumentParser()
parser.add_argument("-T", "--title", type=str, required=True,
help='Title for site, also generates the slug',
metavar="")
parser.add_argument("-c", "--category", required=True,
help='Category or categories of post', metavar="")
parser.add_argument("-t", "--tags", type=str, required=True,
help="Tags for post", metavar="")
parser.add_argument("-a", "--author", type=str, default="Pat Martin",
help="Author of post", metavar="")
args = parser.parse_args()

now = datetime.datetime.now()
slug = args.title.replace(" ", "-").lower()

with open("{}.md".format(slug), 'w') as f:
f.write("Title: {}\n".format(args.title))
f.write("Date: {}-{}-{} {}:{}\n".format(now.year,
now.month,
now.day,
now.hour,
now.minute))
f.write("Modified: {}-{}-{} {}:{}\n".format(now.year,
now.month,
now.day,
now.hour,
now.minute))
f.write("Category: {}\n".format(args.category))
f.write("Slug: {}\n".format(slug))
f.write("Authors: {}\n".format(args.author))
f.write("Summary: \n")


if __name__ == "__main__":
Main()



Thanks for any input.

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


Re: [Tutor] Problem with testing - container

2018-03-29 Thread Albert-Jan Roskam

Op 29 mrt. 2018 21:06 schreef "Shall, Sydney" :
>
> I have a problem with a specific test, I think.
>
> I use a Mac with OS X 10.13.3
> I use Anaconda with Python 3.5
>
> I have been writing tests for a Class that I have written. I am not
> finished yet, but there are already nearly 400 tests. The tests run and
> return OK. [After cleaning up many silly mistakes that I made.]
>
> However, now, when I run the tests (Unittest) I get the following warning:
> 
> lib/python3.5/unittest/case.py:1092: FutureWarning: elementwise
> comparison failed; returning scalar instead, but in the future will
> perform elementwise comparison
>if member in container:
> ...
>
> No other information is given.
>
> My problem is, how do I find out where exactly the problem originates.
> --

Try running Python with the option '-W error', or set PYTHONWARNINGS to 
'error'. https://docs.python.org/2/using/cmdline.html#envvar-PYTHONWARNINGS
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Turtle color question

2018-03-29 Thread Peter Otten
Roger Lea Scherer wrote:

> I've tried everything I can think of but cannot get the pencolor to
> change. (Although the arrow drawing the boxes changes color.) I've placed
> it in places I didn't even think it would work. The arrow draws what looks
> like the boxes I expect, but the color looks the same as the background.
> So I assigned a variable and printed that variable which says None. See:
> 
>  RESTART: C:/Users/Roger/AppData/Local/Programs/Python/Python36/turtle
> square draw.py
> None

> 
> Removing " color = " doesn't change the outcome. I hope this adequately
> explains things.Thanks, again.
> 
> ​import turtle
> 
> def drawSquare(t, sz):
> """Get turtle t to draw a square of sz side"""
> t.pu()

I recommend that you use the long name here, penup() instead of pu().
I should then become glaringly obvious that you lift the pen but never put 
it down again...

> t.forward(sz*2)
> 
> for i in range(4):
> t.forward(sz)
> t.left(90)
> 
> 
> wn = turtle.Screen()
> wn.bgcolor("blue")
> 
> alex = turtle.Turtle()
> color = alex.pencolor("darkgreen")
> print(color)
> 
> def main():
> for i in range(4):
> drawSquare(alex,20)
> 
> main()
> 
> wn.exitonclick()
> ​
> 


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


Re: [Tutor] XML parsing

2018-03-29 Thread Peter Otten
Asif Iqbal wrote:

> On Thu, Mar 29, 2018 at 3:56 AM, Peter Otten <__pete...@web.de> wrote:
> 
>> Asif Iqbal wrote:
>>
>> > I am trying to extract all the *template-name*s, but no success yet
>> >
>> > Here is a sample xml file
>> >
>> > http://tail-f.com/ns/rest;>
>> >   http://networks.com/nms;>
>> > ALLFLEX-BLOOMINGTON
>> > post-staging
>> > full-mesh
>> > ALLFLEX
>> > http://networks.com/nms;>
>> >   advanced-plus
>> >   1000
>> >   true
>> >   true
>> > 
>> > 
>> > 
>> >
>> > with open('/tmp/template-metadata') as f:
>> > import xml.etree.ElementTree as ET
>> > root = ET.fromstring(f.read())
>> >
>> > print len(root)
>> > print root[0][0].text
>> > for l in root.findall('template-metadata'):
>> > print l
>> >
>> >
>> > 392
>> > ALLFLEX-BLOOMINGTON
>> >
>> >
>> > It prints the length of the tree and the first element of the first
>> child,
>> > but when I try to loop through to find all the 'template-name's
>> > it does not print anything.
>> >
>> > What am I doing wrong?
>>
>> You have to include the namespace:
>>
>> for l in root.findall('{http://networks.com/nms}template-metadata'):
>>
> 
> How do I extract the 'template-name' ?

I hoped you'd get the idea. 

> This is what I tried
> 
>  for l in root.findall('{http://networks.com/nms}template-metadata'):

Rinse and repeat:

> print l.find('template-name').text

should be

print l.find('{http://networks.com/nms}template-name').text

> 
> I am following the doc
> https://docs.python.org/2/library/xml.etree.elementtree.html section
> 19.7.1.3 findall example
> 
> I get this error attribute error 'NoneType' object has no attribute text.
> I do not understand why l.find('template-name') is NoneType.

Take the time to read

https://docs.python.org/2/library/xml.etree.elementtree.html#parsing-xml-with-namespaces


> Here is complete code with output.
> 
> 
> import xml.etree.ElementTree as ET
> 
> xmlfile='''
> http://tail-f.com/ns/rest;>
>   http://networks.com/nms;>
> ALLFLEX-BLOOMINGTON
> post-staging
> full-mesh
> ALLFLEX
> http://networks.com/nms;>
>   advanced-plus
>   1000
>   true
>   true
> '''
> 
> root = ET.fromstring(xmlfile)
> print root.tag
> print root[0][0].text
> for l in root.findall('{http://networks.com/nms}template-metadata'):
> print l.find('template-name').text
> 
> collection
> ALLFLEX-BLOOMINGTON
> 
> 
---
AttributeError
>Traceback (most recent call
> last) in () 19 print
> root[0][0].text 20 for l in
> root.findall('{http://networks.com/nms}template-metadata'):---> 21
> print l.find('template-name').text
> AttributeError: 'NoneType' object has no attribute 'text'


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


Re: [Tutor] XML parsing

2018-03-29 Thread Asif Iqbal
On Thu, Mar 29, 2018 at 3:56 AM, Peter Otten <__pete...@web.de> wrote:

> Asif Iqbal wrote:
>
> > I am trying to extract all the *template-name*s, but no success yet
> >
> > Here is a sample xml file
> >
> > http://tail-f.com/ns/rest;>
> >   http://networks.com/nms;>
> > ALLFLEX-BLOOMINGTON
> > post-staging
> > full-mesh
> > ALLFLEX
> > http://networks.com/nms;>
> >   advanced-plus
> >   1000
> >   true
> >   true
> > 
> > 
> > 
> >
> > with open('/tmp/template-metadata') as f:
> > import xml.etree.ElementTree as ET
> > root = ET.fromstring(f.read())
> >
> > print len(root)
> > print root[0][0].text
> > for l in root.findall('template-metadata'):
> > print l
> >
> >
> > 392
> > ALLFLEX-BLOOMINGTON
> >
> >
> > It prints the length of the tree and the first element of the first
> child,
> > but when I try to loop through to find all the 'template-name's
> > it does not print anything.
> >
> > What am I doing wrong?
>
> You have to include the namespace:
>
> for l in root.findall('{http://networks.com/nms}template-metadata'):
>

How do I extract the 'template-name' ?

This is what I tried

 for l in root.findall('{http://networks.com/nms}template-metadata'):
print l.find('template-name').text

I am following the doc
https://docs.python.org/2/library/xml.etree.elementtree.html section
19.7.1.3 findall example

I get this error attribute error 'NoneType' object has no attribute text. I
do not understand why l.find('template-name') is NoneType.

Here is complete code with output.


import xml.etree.ElementTree as ET

xmlfile='''
http://tail-f.com/ns/rest;>
  http://networks.com/nms;>
ALLFLEX-BLOOMINGTON
post-staging
full-mesh
ALLFLEX
http://networks.com/nms;>
  advanced-plus
  1000
  true
  true
'''

root = ET.fromstring(xmlfile)
print root.tag
print root[0][0].text
for l in root.findall('{http://networks.com/nms}template-metadata'):
print l.find('template-name').text

collection
ALLFLEX-BLOOMINGTON

---AttributeError
   Traceback (most recent call
last) in () 19 print
root[0][0].text 20 for l in
root.findall('{http://networks.com/nms}template-metadata'):---> 21
print l.find('template-name').text
AttributeError: 'NoneType' object has no attribute 'text'





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



-- 
Asif Iqbal
PGP Key: 0xE62693C5 KeyServer: pgp.mit.edu
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Problem with testing - container

2018-03-29 Thread Shall, Sydney

I have a problem with a specific test, I think.

I use a Mac with OS X 10.13.3
I use Anaconda with Python 3.5

I have been writing tests for a Class that I have written. I am not 
finished yet, but there are already nearly 400 tests. The tests run and 
return OK. [After cleaning up many silly mistakes that I made.]


However, now, when I run the tests (Unittest) I get the following warning:

lib/python3.5/unittest/case.py:1092: FutureWarning: elementwise 
comparison failed; returning scalar instead, but in the future will 
perform elementwise comparison

  if member in container:
...

No other information is given.

My problem is, how do I find out where exactly the problem originates.
--
Sydney
--

_

Professor Sydney Shall
Department of Haematology/Oncology
Phone: +(0)2078489200
E-Mail: sydney.shall
[Correspondents outside the College should add @kcl.ac.uk]
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Turtle color question

2018-03-29 Thread Roger Lea Scherer
I've tried everything I can think of but cannot get the pencolor to change.
(Although the arrow drawing the boxes changes color.) I've placed it in
places I didn't even think it would work. The arrow draws what looks like
the boxes I expect, but the color looks the same as the background. So I
assigned a variable and printed that variable which says None. See:

 RESTART: C:/Users/Roger/AppData/Local/Programs/Python/Python36/turtle
square draw.py
None
>>>

Removing " color = " doesn't change the outcome. I hope this adequately
explains things.Thanks, again.

​import turtle

def drawSquare(t, sz):
"""Get turtle t to draw a square of sz side"""
t.pu()
t.forward(sz*2)

for i in range(4):
t.forward(sz)
t.left(90)


wn = turtle.Screen()
wn.bgcolor("blue")

alex = turtle.Turtle()
color = alex.pencolor("darkgreen")
print(color)

def main():
for i in range(4):
drawSquare(alex,20)

main()

wn.exitonclick()
​

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


Re: [Tutor] XML parsing

2018-03-29 Thread Peter Otten
Asif Iqbal wrote:

> I am trying to extract all the *template-name*s, but no success yet
> 
> Here is a sample xml file
> 
> http://tail-f.com/ns/rest;>
>   http://networks.com/nms;>
> ALLFLEX-BLOOMINGTON
> post-staging
> full-mesh
> ALLFLEX
> http://networks.com/nms;>
>   advanced-plus
>   1000
>   true
>   true
> 
> 
> 
> 
> with open('/tmp/template-metadata') as f:
> import xml.etree.ElementTree as ET
> root = ET.fromstring(f.read())
> 
> print len(root)
> print root[0][0].text
> for l in root.findall('template-metadata'):
> print l
> 
> 
> 392
> ALLFLEX-BLOOMINGTON
> 
> 
> It prints the length of the tree and the first element of the first child,
> but when I try to loop through to find all the 'template-name's
> it does not print anything.
> 
> What am I doing wrong?

You have to include the namespace:

for l in root.findall('{http://networks.com/nms}template-metadata'):
...


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


[Tutor] XML parsing

2018-03-29 Thread Asif Iqbal
I am trying to extract all the *template-name*s, but no success yet

Here is a sample xml file

http://tail-f.com/ns/rest;>
  http://networks.com/nms;>
ALLFLEX-BLOOMINGTON
post-staging
full-mesh
ALLFLEX
http://networks.com/nms;>
  advanced-plus
  1000
  true
  true




with open('/tmp/template-metadata') as f:
import xml.etree.ElementTree as ET
root = ET.fromstring(f.read())

print len(root)
print root[0][0].text
for l in root.findall('template-metadata'):
print l


392
ALLFLEX-BLOOMINGTON


It prints the length of the tree and the first element of the first child,
but when I try to loop through to find all the 'template-name's
it does not print anything.

What am I doing wrong?


-- 
Asif Iqbal
PGP Key: 0xE62693C5 KeyServer: pgp.mit.edu
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor