Re: [Tutor] Question About the .format Method.

2016-11-10 Thread Peter Otten
Bryon Adams wrote:

> Hello,
>  Working on a simple function to get an IP address and make it look
> pretty for the PyNet course. I'm wondering if there's way to evenly
> space text with the string.format() method similar to how I'm doing it
> with the % operator. The last two prints keep everything left aligned
> and 20 spaces wide. Is there a way to accomplish this with the .format()
> method that I use in the first print function?

You can left-align, center or right-align with format():

>>> print("| {:<12} | {:^12} | {:>12} |".format("left", "center", "right"))
| left |center|right |

The default is right-align for numbers and left-align for strings:

>>> "{:12}".format(42)
'  42'
>>> "{:12}".format("42")
'42  '

You can find the details here:




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


Re: [Tutor] Python Script

2016-11-10 Thread Alan Gauld via Tutor
On 10/11/16 00:18, Jordan Trudell wrote:
> Hello, I need help running a script, as I am getting an error. 

OK, But we need to know what version of Python and which OS you use.

Also post the script and the full error message you get.
(Don't send attachments because they usually get stripped
by the server, paste the text into your mail and send as
plain text not html.)

Finally, it might help if you tell us exactly how you
are trying to run the script.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
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] Question About the .format Method.

2016-11-10 Thread Alan Gauld via Tutor
On 09/11/16 22:30, Bryon Adams wrote:
> Hello,
>  Working on a simple function to get an IP address and make it look 
> pretty for the PyNet course. I'm wondering if there's way to evenly 
> space text with the string.format() method similar to how I'm doing it 
> with the % operator. 

Yes, read section 6.1.3 of the documentation

https://docs.python.org/3/library/string.html#formatstrings

You can do everything that % formatting can do, and some more.

See the examples in section 6.1.3.2

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
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] MEL to Python Translation of array index in conditional statement

2016-11-10 Thread Brian Schindler
I'm trying to convert the following code from MEL (Maya Embedded Language) to 
Python and having trouble with syntax errors in the conditional statement. 
Could you help me out? 

float $pos[3] = `xform -q -rp $obj`;
if ($pos[0] != 0 || $pos[1] != 0 || $pos[2] != 0) 


Brian Schindler 
Professor of Animation 
School of Digital Media 
Savannah College of Art and Design® 
Office: 912.525.8528 Fax: 912.525.8597 
bschi...@scad.edu 

SCAD: The University for Creative Careers® 

NOTICE: This e-mail message and all attachments transmitted with 
it may contain legally privileged and confidential information 
intended solely for the use of the addressee. If the reader of 
this message is not the intended recipient, you are hereby 
notified that any reading, dissemination, distribution, copying, 
or other use of this message or its attachments is strictly 
prohibited. If you have received this message in error, please 
notify the sender immediately by telephone or by electronic mail 
and then delete this message and all copies and backups thereof. 
Thank you. 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] MEL to Python Translation of array index in conditional statement

2016-11-10 Thread Peter Otten
Brian Schindler wrote:

> I'm trying to convert the following code from MEL (Maya Embedded Language)
> to Python and having trouble with syntax errors in the conditional
> statement. 

What did you try?

> Could you help me out?
> 
> float $pos[3] = `xform -q -rp $obj`;
> if ($pos[0] != 0 || $pos[1] != 0 || $pos[2] != 0)

I don't know MEL. Is xform a commandline tool? In that case you might need 
something along the lines of

output = subprocess.Popen(
["xform", "-q", "-rp", obj], 
stdout=subprocess.PIPE).communicate()[0]
pos = [float(s) for s in output.split()]
if pos[0] or pos[1] or pos[2]:
   ... # whatever


> Brian Schindler
> Professor of Animation

Well, what can I say -- it's never too late to read .
Or  if you prefer the 
classics;)


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


Re: [Tutor] MEL to Python Translation of array index in conditional statement

2016-11-10 Thread Steven D'Aprano
On Thu, Nov 10, 2016 at 08:08:14AM -0500, Brian Schindler wrote:

> I'm trying to convert the following code from MEL (Maya Embedded 
> Language) to Python and having trouble with syntax errors in the 
> conditional statement. Could you help me out?

Only if you help us to help you. I don't know MEL and don't know how to 
read the following:

> float $pos[3] = `xform -q -rp $obj`;
> if ($pos[0] != 0 || $pos[1] != 0 || $pos[2] != 0) 

and chances are nobody else here does either. Can you explain in words 
what it does? If you're getting an error, best to show us the code you 
tried and the FULL error you are getting (copy and paste it please, as 
text, don't post a screenshot).

I'm going to take a wild guess that the code means something like:

pos = an array of four values
if pos[0] != 0 or pos[1] != 0 or pos[2] != 0 then:
call an external program xform with arguments -q -rp obj
convert the result to a floating point value
set pos[3] to that number


which would be something similar to:


import subprocess
if any(pos[:3]):
output = subprocess.check_output(['xform', '-q', '-rp', obj])
pos[3] = float(output)


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


Re: [Tutor] MEL to Python Translation of array index in conditional statement

2016-11-10 Thread Alan Gauld via Tutor
On 10/11/16 13:08, Brian Schindler wrote:
> I'm trying to convert the following code from MEL (Maya Embedded Language)
>  to Python

You'll probably get better support fromthe Maya community than here
since most of us don;'t know anything about Maya's language.

>  and having trouble with syntax errors in the conditional statement. 

> float $pos[3] = `xform -q -rp $obj`;
> if ($pos[0] != 0 || $pos[1] != 0 || $pos[2] != 0) 

That is presumably the Maya code?
Unfortunately I don't know exactly what it does - I can make
a guess but that's all it would be. For example, is the
indentation on line 2 significant? (It would be in python)
I'm guessing the backtick(`) mean you are executing an OS command?
If so you'll need the python subprocess module to do the equivalent.

If the 2nd line does what I think, the equivalent Python code would
be something like:

if pos[0] or pos[1] or pos[2]:
# do something here

Can you show us what you tried and what error message(s) you
got(in full)?


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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