[Tutor] CheckButtons reset to default values

2017-02-10 Thread Pooja Bhalode
Hi,

I have a bunch of checkbuttons in my code and a default settings button
which restores the original setting in the checkbuttons. The code is given
below:


var1 = IntVar()
var2 = IntVar()
var3 = IntVar()
var4 = IntVar()
Checkbutton(frame1, text = "Vertices", variable=var1, onvalue=1,
offvalue=0).grid(row=1, column = 1, sticky=W)
Checkbutton(frame1, text = "Edges", variable=var2).grid(row=2, column =
1, sticky=W)
Checkbutton(frame1, text = "Faces", variable=var3).grid(row=3, column =
1, sticky=W)
check = Checkbutton(frame1, text = "Center", variable=var4)
check.grid(row=4, column = 1, sticky=W)
check.select()

label2 = Label(frame1, text="2. Cut off Improvement %")
label2.grid(row=5, column=0,sticky=W)
Entry2 = Entry(frame1)
Entry2.insert(END, '05')
Entry2.grid(row=5, column = 1, sticky = W)

def default():
print "Inside default"

var1.set(0)
var2.set(0)
var3.set(0)
var4.set(1)
Entry2.delete(0, END)
Entry2.insert(END,'05')

Button(frame1, text = "Select Default value",
command=default).grid(row=0, column = 2, sticky=W)


This resets the Entry2 but it does not reset the checkbuttons selection to
the values specified in default function.
Can some one please tell me where I am going wrong?
Thank you so much.

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


[Tutor] Help

2017-02-10 Thread Sasiliyu Adetunji
Please assist with this assignment

Write a function my_sort which takes in a list of numbers (positive
integers).

The function should return a list of sorted numbers such that odd numbers
come first and even numbers come last.


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


[Tutor] Can I can use subprocess module for connecting to linux from windows

2017-02-10 Thread Basavaraj Lamani
Hi,

down votefavorite


Manual testing: I have installed putty in windows10 machine and by using
putty, I will do SSH connection to Linux machine(VM). I enter username and
password to access Linux machine(VM) then I enter different commands to
install application in Linux machine.

Automation: I want to automate above manual task by using python standard
subprocess module. I don't want to use third party libraries like paramiko,
pexpect etc. I am running script from windows machine. Please help me.

Below is the code( First I am trying to connect linux machine from
windows10).

import subprocess
cmd = "ssh root@10.106.213.235"
p = subprocess.Popen(cmd, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr =subprocess.PIPE, bufsize=1,shell=True)

output: Process finished with exit code 0

code 0 means i believe there is no error in the script. But how i can
confirm that windows machine is connected to linux machine. while running
this script, am i able to see the shell?.

I am new to programming. Please provide your inputs which is really helpful
for me.

Thanks & Regards,

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


Re: [Tutor] CheckButtons reset to default values

2017-02-10 Thread Peter Otten
Pooja Bhalode wrote:

> Hi,
> 
> I have a bunch of checkbuttons in my code and a default settings button
> which restores the original setting in the checkbuttons. The code is given
> below:
> 
> 
> var1 = IntVar()
> var2 = IntVar()
> var3 = IntVar()
> var4 = IntVar()
> Checkbutton(frame1, text = "Vertices", variable=var1, onvalue=1,
> offvalue=0).grid(row=1, column = 1, sticky=W)
> Checkbutton(frame1, text = "Edges", variable=var2).grid(row=2, column
> =
> 1, sticky=W)
> Checkbutton(frame1, text = "Faces", variable=var3).grid(row=3, column
> =
> 1, sticky=W)
> check = Checkbutton(frame1, text = "Center", variable=var4)
> check.grid(row=4, column = 1, sticky=W)
> check.select()
> 
> label2 = Label(frame1, text="2. Cut off Improvement %")
> label2.grid(row=5, column=0,sticky=W)
> Entry2 = Entry(frame1)
> Entry2.insert(END, '05')
> Entry2.grid(row=5, column = 1, sticky = W)
> 
> def default():
> print "Inside default"
> 
> var1.set(0)
> var2.set(0)
> var3.set(0)
> var4.set(1)
> Entry2.delete(0, END)
> Entry2.insert(END,'05')
> 
> Button(frame1, text = "Select Default value",
> command=default).grid(row=0, column = 2, sticky=W)
> 
> 
> This resets the Entry2 but it does not reset the checkbuttons selection to
> the values specified in default function.
> Can some one please tell me where I am going wrong?

The problem has to be elsewhere in your code. If I run a script

from Tkinter import *

root = frame1 = Tk()

if 1:
   # your snippet shown above goes here

root.mainloop()

clicking the button unchecks Vertices, Edges, Faces and checks Center.

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


Re: [Tutor] Help

2017-02-10 Thread Alan Gauld via Tutor
On 10/02/17 02:05, Sasiliyu Adetunji wrote:
> Please assist with this assignment
> 
> Write a function my_sort which takes in a list of numbers (positive
> integers).
> 
> The function should return a list of sorted numbers such that odd numbers
> come first and even numbers come last.

def my_sort(number_list):
   # your code goes here

Does that help?

If not you will need to be more specific about what help you need
and show us your code. We won't write it for you (apart from the
line above...)

-- 
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] Can I can use subprocess module for connecting to linux from windows

2017-02-10 Thread Alan Gauld via Tutor
On 10/02/17 06:48, Basavaraj Lamani wrote:

> putty, I will do SSH connection to Linux machine(VM). I enter username and
> password to access Linux machine(VM) then I enter different commands to
> install application in Linux machine.
> 
> Automation: I want to automate above manual task by using python standard
> subprocess module. 

The way I'd do that is write a Linux shell script to do all the Linux
work then use Python to ftp(*) a copy of the script to the Linux box and
just use ssh to run that script.

Trying to use subprocess to drive an interactive ssh session is an
exercise in frustration which I would avoid if at all possible.
Running a single command is much easier.

(*)Of course that assume you have an ftp server running on Linux...

-- 
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] Help

2017-02-10 Thread Joaquin Alzola
>Please assist with this assignment

>Write a function my_sort which takes in a list of numbers (positive
>integers).

>The function should return a list of sorted numbers such that odd numbers
>come first and even numbers come last.

You can search for even or odd numbers with the modulo (%) operant.

def my_sort(*numbers):


my_sort(X,Y,Z,F)

just trying not to tell you the excersice just to give you hints.
The best way to learn is to struggle a bit doing it yourself.
This email is confidential and may be subject to privilege. If you are not the 
intended recipient, please do not copy or disclose its content but contact the 
sender immediately upon receipt.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] CheckButtons reset to default values

2017-02-10 Thread Pooja Bhalode
Hi Peter,

The code is as shown below.  I have put it in the manner you have told me.

root = Tk()
root.title("Design of Experiments with Parameter Estimation")
root.geometry("1000x1000")

def DesignPoint():
print "Inside Design Point"
rootdesign=Tk()
rootdesign.title("Estimation of Experimental Precision for Data
Var-CoVar")
rootdesign.geometry("600x400")
frame1 = Frame(rootdesign)
frame1.grid(row=0, column=0)

## Inserting Checkboxes:
label1 = Label(frame1, text="1. Design Point Suggestions")
label1.grid(row=0, column=0,sticky=W )
var1 = IntVar()
var2 = IntVar()
var3 = IntVar()
var4 = IntVar()
Checkbutton(frame1, text = "Vertices", variable=var1, onvalue=1,
offvalue=0).grid(row=1, column = 1, sticky=W)
Checkbutton(frame1, text = "Edges", variable=var2).grid(row=2, column =
1, sticky=W)
Checkbutton(frame1, text = "Faces", variable=var3).grid(row=3, column =
1, sticky=W)
check = Checkbutton(frame1, text = "Center", variable=var4)
check.grid(row=4, column = 1, sticky=W)
check.select()


label2 = Label(frame1, text="2. Cut off Improvement %")
label2.grid(row=5, column=0,sticky=W)
Entry2 = Entry(frame1)
Entry2.insert(END, '05')
Entry2.grid(row=5, column = 1, sticky = W)

label3 = Label(frame1, text="3. Simulation of proposed Experiments: ")
label3.grid(row=5, column=0,sticky=W)

label4 = Label(frame1, text="4. Calculate sensitivities")
label4.grid(row=6, column=0,sticky=W)

def default():
print "Inside default"

var1.set(0)
var2.set(0)
var3.set(0)
var4.set(1)
Entry2.delete(0, END)
Entry2.insert(END,'05')
   Button(frame1, text = "Select Default value",
command=default.grid(row=0, column = 2, sticky=W)

rootdesign.mainloop()


## Secondary menu bar:
menusec = Frame(root, bg="white")
butt1 = Button(menusec, text="Part One", command=DesignPoint)
butt1.pack(side=LEFT, padx=1)
menusec.pack(side=TOP, fill=X)


root.mainloop()

It still doesn't work for me for the reason I am not able to figure out.
Please let me know.
Thank you once again in advance.

Pooja

On Fri, Feb 10, 2017 at 4:48 AM, Peter Otten <__pete...@web.de> wrote:

> Pooja Bhalode wrote:
>
> > Hi,
> >
> > I have a bunch of checkbuttons in my code and a default settings button
> > which restores the original setting in the checkbuttons. The code is
> given
> > below:
> >
> >
> > var1 = IntVar()
> > var2 = IntVar()
> > var3 = IntVar()
> > var4 = IntVar()
> > Checkbutton(frame1, text = "Vertices", variable=var1, onvalue=1,
> > offvalue=0).grid(row=1, column = 1, sticky=W)
> > Checkbutton(frame1, text = "Edges", variable=var2).grid(row=2, column
> > =
> > 1, sticky=W)
> > Checkbutton(frame1, text = "Faces", variable=var3).grid(row=3, column
> > =
> > 1, sticky=W)
> > check = Checkbutton(frame1, text = "Center", variable=var4)
> > check.grid(row=4, column = 1, sticky=W)
> > check.select()
> >
> > label2 = Label(frame1, text="2. Cut off Improvement %")
> > label2.grid(row=5, column=0,sticky=W)
> > Entry2 = Entry(frame1)
> > Entry2.insert(END, '05')
> > Entry2.grid(row=5, column = 1, sticky = W)
> >
> > def default():
> > print "Inside default"
> >
> > var1.set(0)
> > var2.set(0)
> > var3.set(0)
> > var4.set(1)
> > Entry2.delete(0, END)
> > Entry2.insert(END,'05')
> >
> > Button(frame1, text = "Select Default value",
> > command=default).grid(row=0, column = 2, sticky=W)
> >
> >
> > This resets the Entry2 but it does not reset the checkbuttons selection
> to
> > the values specified in default function.
> > Can some one please tell me where I am going wrong?
>
> The problem has to be elsewhere in your code. If I run a script
>
> from Tkinter import *
>
> root = frame1 = Tk()
>
> if 1:
># your snippet shown above goes here
>
> root.mainloop()
>
> clicking the button unchecks Vertices, Edges, Faces and checks Center.
>
> ___
> 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] CheckButtons reset to default values

2017-02-10 Thread Alan Gauld via Tutor
On 10/02/17 15:56, Pooja Bhalode wrote:

> root = Tk()
> root.title("Design of Experiments with Parameter Estimation")
> root.geometry("1000x1000")
> 
> def DesignPoint():
> rootdesign=Tk()

You should o0nly have one root. This create a new root
everytime you xcall Designpoint and even if thats only
once you still have a root already at the global level.

It would be better to define the function to have
a root parameter that you can pass in when you call it.

> rootdesign.title("Estimation of Experimental Precision for Data
> Var-CoVar")
> rootdesign.geometry("600x400")

Are you trying to create a second window? If so you
should probably use the TopLevel widget for that but
with the same toplevel root.

Alternatively you could create a Dialog using the Dialog
module but that probably requires a bit more OOP than
you seem comfortable with.


> frame1 = Frame(rootdesign)
> frame1.grid(row=0, column=0)
> 
> ## Inserting Checkboxes:
> label1 = Label(frame1, text="1. Design Point Suggestions")
> label1.grid(row=0, column=0,sticky=W )
> var1 = IntVar()
> var2 = IntVar()
> var3 = IntVar()
> var4 = IntVar()

These vars should probably be declared and initialised
outside the function.

> Checkbutton(frame1, text = "Vertices", variable=var1, onvalue=1,
> offvalue=0).grid(row=1, column = 1, sticky=W)
> Checkbutton(frame1, text = "Edges", variable=var2).grid(row=2, column =
> 1, sticky=W)
> Checkbutton(frame1, text = "Faces", variable=var3).grid(row=3, column =
> 1, sticky=W)
> check = Checkbutton(frame1, text = "Center", variable=var4)
> check.grid(row=4, column = 1, sticky=W)
> check.select()
> 
> 
> label2 = Label(frame1, text="2. Cut off Improvement %")
> label2.grid(row=5, column=0,sticky=W)
> Entry2 = Entry(frame1)
> Entry2.insert(END, '05')
> Entry2.grid(row=5, column = 1, sticky = W)
> 
> label3 = Label(frame1, text="3. Simulation of proposed Experiments: ")
> label3.grid(row=5, column=0,sticky=W)
> 
> label4 = Label(frame1, text="4. Calculate sensitivities")
> label4.grid(row=6, column=0,sticky=W)
> 

The above is ok but...


> def default():
> print "Inside default"
> 
> var1.set(0)
> var2.set(0)
> var3.set(0)
> var4.set(1)
> Entry2.delete(0, END)
> Entry2.insert(END,'05')

You have now defined this function inside the Designpoint
function. It should probably be outside except you would
lose visibility of the vars - which is why they should be global.

>Button(frame1, text = "Select Default value",
> command=default.grid(row=0, column = 2, sticky=W)


But you cannot apply grid to a function. your command
should be default. In suspect you are just missing a
closing parenthesis

Button(frame1,
   text = "Select Default value",
   command=default).grid(row=0, column = 2, sticky=W)

> rootdesign.mainloop()
> 
> 
> ## Secondary menu bar:
> menusec = Frame(root, bg="white")
> butt1 = Button(menusec, text="Part One", command=DesignPoint)
> butt1.pack(side=LEFT, padx=1)
> menusec.pack(side=TOP, fill=X)
> 
> 
> root.mainloop()
> 
> It still doesn't work for me for the reason I am not able to figure out.
> Please let me know.

You have quite a few issues. The main things you should sort out are:

1) only use one root.
2) put the vars and default definitions outside the
   designpoint function.
3) add the closing parenthesis.


-- 
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] Climata package

2017-02-10 Thread fei fan
Hi tutor,

Do you know of any good resources to extract data using the climata package and 
to convert these data in .geojson format?

Thanks,

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


Re: [Tutor] Climata package

2017-02-10 Thread Alan Gauld via Tutor
On 10/02/17 20:07, fei fan wrote:
> Hi tutor,
> 
> Do you know of any good resources to extract data using the climata 
> package and to convert these data in .geojson format?

This forum is for the core language and standard library so this is a
bit off topic, you might get a better response on the main Python list
which has a wider audience.

A quick Google search for "python climata geojson" threw up quite a lot
on geojson but nothing at all on climata.

HTH
-- 
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] Test for type(object) == ???

2017-02-10 Thread boB Stepp
I was playing around with type() tonight.  If I type (pun intended), I get:

py3: type(5)


So I naively thought a test for type int should go like:

py3: type(5) == ""
False

Hmm.  So I tried these other failing tests:

py3: type(5) == 
  File "", line 1
type(5) == 
   ^
SyntaxError: invalid syntax

py3: type(5) == 'int()'
False

I finally stumbled onto the correct form:

py3: type(5) == int
True

So my question is why does "type(5)" result in "", but
the correct Boolean test is "type(5) == int"?  I suspect it has
something to do with the built-in attributes of Python objects that I
currently know so very little about.

As always, many thanks in advance!

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


Re: [Tutor] Test for type(object) == ???

2017-02-10 Thread Zachary Ware
On Fri, Feb 10, 2017 at 7:34 PM, boB Stepp  wrote:
> So my question is why does "type(5)" result in "", but
> the correct Boolean test is "type(5) == int"?  I suspect it has
> something to do with the built-in attributes of Python objects that I
> currently know so very little about.

Try `help(repr)` and `int` on its own at the interactive prompt, and
see if that clarifies anything.  I hope that might be enough to nudge
you in the right direction, but if not, come back and ask :)

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


Re: [Tutor] Test for type(object) == ???

2017-02-10 Thread Alex Kleider

On 2017-02-10 17:34, boB Stepp wrote:

I was playing around with type() tonight.  .


I've also "played around" with this subject-
Here's a source:
http://stackoverflow.com/questions/152580/whats-the-canonical-way-to-check-for-type-in-python

... and a successful experiment:
alex@X301n3:~$ python3
Python 3.4.3 (default, Nov 17 2016, 01:11:57)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.

type(5) is int

True

isinstance(5, int)

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


Re: [Tutor] Test for type(object) == ???

2017-02-10 Thread Ben Finney
boB Stepp  writes:

> I was playing around with type() tonight.  If I type (pun intended), I get:
>
> py3: type(5)
> 

Ceci n'est pas un ‘int’.

> So I naively thought a test for type int should go like:
>
> py3: type(5) == ""
> False
>
> Hmm.

The output from the REPL can only be text. But a type, or any other
object in memory, is not text on the screen; the text only *represents*
an object.

More than that: You can't type objects into your program code. You have
to *reference* them, often by using names.

So the type ‘int’ can be referenced by the name, ‘int’:

>>> type(5) is int
True

And that type, when asked for a text representation, will give you some
text.

>>> type(5)


The object is not its name. The object is not its representation. And
those can (and in this case, are) all different.

https://en.wikipedia.org/wiki/The_Treachery_of_Images>

-- 
 \  “Programs must be written for people to read, and only |
  `\incidentally for machines to execute.” —Abelson & Sussman, |
_o__)  _Structure and Interpretation of Computer Programs_ |
Ben Finney

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


[Tutor] Python-list thread: int vs. float

2017-02-10 Thread boB Stepp
I have been following the thread "int vs. float"
(https://mail.python.org/pipermail/python-list/2017-February/719287.html)
on the main list.  A search for the OP on the Tutor archive came up
negative, so I am hoping he is not following Tutor tonight (Or anytime
prior to the due date for his homework!).  The central part of
adam14711993's question is:

"What I cannot figure out is how to write it so that if my user input
is, for example, 1.5, the program will result with: Sorry, you can
only order whole packages.

"I understand that because I am starting out by assigning my
number_purchases_str to be an int, when the user enters a float that
is a conflict and will crash."

He cannot figure out how to reliably tell if the user's input is an
integer, float or neither.  So I thought I would come up with my
solution, which currently is:

py3: def ck_input():
... value_to_ck = input('Enter a number:')
... try:
... value = int(value_to_ck)
... print('You have entered an integer.')
... except ValueError:
... try:
... value = float(value_to_ck)
... print('You have entered a float.')
... except ValueError:
... print('You have failed to enter a numerical value.')
...

(Yes, I know I am not doing anything with the variable, "value", but
if I were to actually implement this in the future, I am sure I would
find a use for it.  So I left it as is for the moment.)

My quick checks are:

py3: ck_input()
Enter a number:5
You have entered an integer.
py3: ck_input()
Enter a number:5.0
You have entered a float.
py3: ck_input()
Enter a number:'5'
You have failed to enter a numerical value.

This is all well and good.  I am not trying to elicit an "Atta boy,
boB!" here. ~(:>)) Instead, I am wondering if there is something in
Python's wonderful cornucopia of programming stuff that can simplify
this type of check.  As you might guess from my earlier post this
evening, I have been playing around with "type()" and "isinstance()",
but if I try something like:

py3: isinstance(int('5.0'))
Traceback (most recent call last):
  File "", line 1, in 
ValueError: invalid literal for int() with base 10: '5.0'

I get the ValueError I make use of in my function above.

So, is there any easier way to do this check?  If not, can my function
be improved to make it a bit less lengthy, but still eminently
readable?

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


Re: [Tutor] Test for type(object) == ???

2017-02-10 Thread eryk sun
On Sat, Feb 11, 2017 at 1:34 AM, boB Stepp  wrote:
> I was playing around with type() tonight.  If I type (pun intended), I get:
>
> py3: type(5)
> 

`type` is a metaclass that either creates a new class (given 3
arguments: name, bases, and dict) or returns a reference to the class
of an existing object. type(5) is the latter case, and it returns a
reference to the class `int`. What you see printed in the REPL is
repr(int), a string representation of the class object:

 >>> repr(int)
 ""

Speaking of classes and metaclasses, note that you can't call
int.__repr__(int) to get this representation, because the __repr__
special method of int is meant for instances of int such as int(5).
Instead you have to explicitly use __repr__ from the metaclass, i.e.
`type`:

>>> type(int)

>>> type.__repr__(int)
""

But just use built-in repr().
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Test for type(object) == ???

2017-02-10 Thread boB Stepp
On Fri, Feb 10, 2017 at 7:50 PM, Zachary Ware
 wrote:

> Try `help(repr)` and `int` on its own at the interactive prompt, and

py3: help(repr)
Help on built-in function repr in module builtins:

repr(obj, /)
Return the canonical string representation of the object.

For many object types, including most builtins, eval(repr(obj)) == obj.

Question:  What does the forward slash represent in this context?  If I type:

py3: repr(int, /)
  File "", line 1
repr(int, /)
  ^
SyntaxError: invalid syntax

whereas:

py3: repr(int)
""

appears to be correct.

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


Re: [Tutor] Test for type(object) == ???

2017-02-10 Thread boB Stepp
On Fri, Feb 10, 2017 at 8:05 PM, eryk sun  wrote:

> Speaking of classes and metaclasses, note that you can't call
> int.__repr__(int) to get this representation, because the __repr__
> special method of int is meant for instances of int such as int(5).

This bit got me experimenting.  Since the integer "5" is an integer
object instance, I am wondering why I can't do:

py3: 5.__repr__()
  File "", line 1
5.__repr__()
 ^
SyntaxError: invalid syntax

, but I can do:

py3: x = 5
py3: x.__repr__()
'5'

?

More experimentation.  Is this how I would define __repr__() for a custom class?

py3: class boB:
... def __repr__(self):
... return ""
...
py3: x = boB()
py3: repr(x)
""

Seems to work.  But in class examples I've seen posted, I do not
recall __repr__ ever being defined.  So I infer that most of the time
I should not or need not do so, but under what circumstances should I
do so?

Another curiosity question.  If I type:

py3: repr(int)
""

I get what I expect, but if I do the same with my custom class, "boB",
I instead get:

py3: repr(boB)
""

Why the difference between the Python class "int" and my custom class
"boB"?  Did I not define __repr__() correctly?



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


Re: [Tutor] Test for type(object) == ???

2017-02-10 Thread eryk sun
On Sat, Feb 11, 2017 at 4:32 AM, boB Stepp  wrote:
>
> This bit got me experimenting.  Since the integer "5" is an integer
> object instance, I am wondering why I can't do:
>
> py3: 5.__repr__()
>   File "", line 1
> 5.__repr__()
>  ^
> SyntaxError: invalid syntax
>
> , but I can do:
>
> py3: x = 5
> py3: x.__repr__()
> '5'

The parser sees "5." as a floating point number. You can use
parentheses to force it to parse 5 as an integer:

>>> (5).__repr__()
'5'

> But in class examples I've seen posted, I do not recall __repr__ ever
> being defined.  So I infer that most of the time I should not or need not
> do so, but under what circumstances should I do so?

Classes inherit a default __repr__ from `object`. For example:

>>> object.__repr__(5)
''

Define a custom __repr__ when it's useful for debugging to include
additional information. It also gets called for str() if you don't
implement a custom __str__.

> Another curiosity question.  If I type:
>
> py3: repr(int)
> ""
>
> I get what I expect, but if I do the same with my custom class, "boB",
> I instead get:
>
> py3: repr(boB)
> ""
>
> Why the difference between the Python class "int" and my custom class
> "boB"?  Did I not define __repr__() correctly?

The __repr__ defined by your class is used for instances of the class,
not for the class itself. In the above you're seeing the return value
from the metaclass __repr__ that's defined by `type`:

In CPython, type.__repr__ is implemented in C as type_repr in
Objects/typeobject.c. If it can determine the module and qualified
name of a class, then it uses the template "".
Otherwise it uses the template "" with the basic char
*tp_name from the PyTypeObject.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Test for type(object) == ???

2017-02-10 Thread boB Stepp
Thanks for the detailed information.  I have a final really nitpicky question.

On Fri, Feb 10, 2017 at 11:27 PM, eryk sun  wrote:
> On Sat, Feb 11, 2017 at 4:32 AM, boB Stepp  wrote:
>>
>> This bit got me experimenting.  Since the integer "5" is an integer
>> object instance, I am wondering why I can't do:
>>
>> py3: 5.__repr__()
>>   File "", line 1
>> 5.__repr__()
>>  ^
>> SyntaxError: invalid syntax

[snip]

> The parser sees "5." as a floating point number.

I am curious as to why the caret does not point to the first
underscore after the decimal point in the error message?  It is at
that precise character that the discerned syntax error occurs.

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


Re: [Tutor] Test for type(object) == ???

2017-02-10 Thread eryk sun
On Sat, Feb 11, 2017 at 3:22 AM, boB Stepp  wrote:
>
> py3: help(repr)
> Help on built-in function repr in module builtins:
>
> repr(obj, /)
> Return the canonical string representation of the object.
>
> For many object types, including most builtins, eval(repr(obj)) == obj.
>
> Question:  What does the forward slash represent in this context?

It's from the function's __text_signature__.

>>> repr.__text_signature__
'($module, obj, /)'

It means "obj" is a positional-only argument that cannot be passed as a keyword.

>>> s = inspect.signature(repr)
>>> s.parameters['obj'].kind
<_ParameterKind.POSITIONAL_ONLY: 0>

CPython has a lot of built-in functions that don't allow keyword arguments.

>>> repr(obj=42)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: repr() takes no keyword arguments

Look at open() for comparison.

>>> open.__text_signature__
"($module, /, file, mode='r', buffering=-1, encoding=None,\n
errors=None, newline=None, closefd=True, opener=None)"

All of its parameters can be passed as keyword arguments.

>>> s = inspect.signature(open)
>>> s.parameters['file'].kind
<_ParameterKind.POSITIONAL_OR_KEYWORD: 1>
>>> s.parameters['mode'].kind
<_ParameterKind.POSITIONAL_OR_KEYWORD: 1>

For example:

>>> open(file='spam', mode='w')
<_io.TextIOWrapper name='spam' mode='w' encoding='UTF-8'>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Test for type(object) == ???

2017-02-10 Thread boB Stepp
On Fri, Feb 10, 2017 at 11:49 PM, eryk sun  wrote:
> On Sat, Feb 11, 2017 at 3:22 AM, boB Stepp  wrote:
>>
>> py3: help(repr)
>> Help on built-in function repr in module builtins:
>>
>> repr(obj, /)
>> Return the canonical string representation of the object.
>>
>> For many object types, including most builtins, eval(repr(obj)) == obj.
>>
>> Question:  What does the forward slash represent in this context?
>
> It's from the function's __text_signature__.
>
> >>> repr.__text_signature__
> '($module, obj, /)'
>
> It means "obj" is a positional-only argument that cannot be passed as a 
> keyword.

[snip]

> Look at open() for comparison.
>
> >>> open.__text_signature__
> "($module, /, file, mode='r', buffering=-1, encoding=None,\n
> errors=None, newline=None, closefd=True, opener=None)"

In your comparison example, a forward slash appears again, but this
time after "$module" in the location where "obj" previously appeared
in the repr example.  Is it indicating a similar use here?

Can you provide a link to "__text_signature__" in the docs?  I tried
searching for it, but came up empty.


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


Re: [Tutor] Test for type(object) == ???

2017-02-10 Thread eryk sun
On Sat, Feb 11, 2017 at 6:22 AM, boB Stepp  wrote:
> On Fri, Feb 10, 2017 at 11:49 PM, eryk sun  wrote:
>
>> It's from the function's __text_signature__.
>>
>> >>> repr.__text_signature__
>> '($module, obj, /)'
>>
>> It means "obj" is a positional-only argument that cannot be passed as a 
>> keyword.
>
> [snip]
>
>> Look at open() for comparison.
>>
>> >>> open.__text_signature__
>> "($module, /, file, mode='r', buffering=-1, encoding=None,\n
>> errors=None, newline=None, closefd=True, opener=None)"
>
> In your comparison example, a forward slash appears again, but this
> time after "$module" in the location where "obj" previously appeared
> in the repr example.  Is it indicating a similar use here?

All parameters listed before the slash are position-only. Everything
after the slash can be passed as a keyword argument.

> Can you provide a link to "__text_signature__" in the docs?  I tried
> searching for it, but came up empty.

No, I can't. It was added in issue 19674.

http://bugs.python.org/issue19674

The '/' syntax for positional-only arguments is documented in PEP 457.

https://www.python.org/dev/peps/pep-0457

Also see the how-to for CPython's "Argument Clinic" preprocessor,
since (for now) positional-only arguments are only a concern for
built-in functions.

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


Re: [Tutor] Test for type(object) == ???

2017-02-10 Thread boB Stepp
On Sat, Feb 11, 2017 at 12:57 AM, eryk sun  wrote:

>
> The '/' syntax for positional-only arguments is documented in PEP 457.
>
> https://www.python.org/dev/peps/pep-0457

At https://www.python.org/dev/peps/pep-0457/#id14 in PEP 457 it says:



>From the "ten-thousand foot view", and ignoring *args and **kwargs for
now, the grammar for a function definition currently looks like this:

def name(positional_or_keyword_parameters, *, keyword_only_parameters):

Building on that perspective, the new syntax for functions would look like this:

def name(positional_only_parameters, /, positional_or_keyword_parameters,
 *, keyword_only_parameters):

All parameters before the / are positional-only. If / is not specified
in a function signature, that function does not accept any
positional-only parameters.



Has this PEP been implemented yet?  I am running Python 3.5.2 and it
appears not to work.  Also, in "What's New In Python 3.6"
(https://docs.python.org/3/whatsnew/3.6.html) I did not see a mention
of it.


> Also see the how-to for CPython's "Argument Clinic" preprocessor,
> since (for now) positional-only arguments are only a concern for
> built-in functions.

So I am assuming this syntax is only being used for some built-in functions?

You have provided a lot of interesting information.  I'm not sure if I
am comprehending everything, but it has made interesting reading and
Googling tonight.  Enough so that I have stayed up much later than I
meant to!  Thanks!



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