Re: [Tutor] Getting import to use a variable name

2015-05-20 Thread Peter Otten
Jim Mooney Py3.4.3winXP wrote:

> On 19 May 2015 at 17:25, Jim Mooney Py3.4.3winXP
>  wrote:
> 
>>
>> If I can get dir to accept x I can parse the output to get rid of the
>> __xxx stuff and print it out.
>>
> 
> By that I mean dir will give me a list of strings I can then use __doc__
> on to get all useful help items.

If you start with an object dir() gives you a list of attribute names. To 
get the actual attributes use 

attribute = getattr(object, attribute_name)

Then print the attributes' docstring with

print(attribute_name, attribute.__doc__)

The complete example:

$ cat shorthelp.py
import importlib


def first_line(s):
if s is None:
return "(NO HELP AVAILABLE)"
return s.splitlines()[0]


def shorthelp(obj):
if isinstance(obj, str):
# if obj is a string, assume it's a module name
try:
obj = importlib.import_module(obj)
except BaseException as err:
# we really don't want to exit on error
print(err)
return

# documentation for obj
objdoc = first_line(obj.__doc__)
if objdoc:
print(objdoc)
print("-" * len(objdoc))

# documentation for the attributes of obj
names = [name for name in dir(obj) if not name.startswith("_")]
width = max(len(name) for name in names)
for name in names:
print("{:{}}  {}".format(
name, width,
first_line(getattr(obj, name).__doc__)))

$ python3 -i shorthelp.py
>>> shorthelp("whatever")
No module named 'whatever'
>>> shorthelp(42)
int(x=0) -> integer
---
bit_length   int.bit_length() -> int
conjugateReturns self, the complex conjugate of any int.
denominator  int(x=0) -> integer
from_bytes   int.from_bytes(bytes, byteorder, *, signed=False) -> int
imag int(x=0) -> integer
numeratorint(x=0) -> integer
real int(x=0) -> integer
to_bytes int.to_bytes(length, byteorder, *, signed=False) -> bytes
>>> shorthelp("pwd")
This module provides access to the Unix password database.
--
getpwall   getpwall() -> list_of_entries
getpwnam   getpwnam(name) -> (pw_name,pw_passwd,pw_uid,
getpwuid   getpwuid(uid) -> (pw_name,pw_passwd,pw_uid,
struct_passwd  pwd.struct_passwd: Results from getpw*() routines.
>>> shorthelp(grp)
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'grp' is not defined
>>> import grp
>>> shorthelp(grp)
Access to the Unix group database.
--
getgrall  getgrall() -> list of tuples
getgrgid  getgrgid(id) -> tuple
getgrnam  getgrnam(name) -> tuple
struct_group  grp.struct_group: Results from getgr*() routines.


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


[Tutor] Help with writing a bean bag toss game in python

2015-05-20 Thread Grace Anne St Clair-Bates
Hello! I am trying to code a bean-bag toss game in python where the program
generates three random dots inside a turtle graphic. Each different hole
means a different score, but I don't know how to write the function to
calculate the total score with all three random points and display the
score on the score board. It is hard to explain, but if anyone has an idea
please let me know! thanks
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Getting import to use a variable name

2015-05-20 Thread Alan Gauld

On 20/05/15 09:02, Peter Otten wrote:


$ python3 -i shorthelp.py

shorthelp("whatever")

No module named 'whatever'

shorthelp(42)

int(x=0) -> integer
---
bit_length   int.bit_length() -> int
conjugateReturns self, the complex conjugate of any int.
denominator  int(x=0) -> integer

...

shorthelp("pwd")

This module provides access to the Unix password database.
--
getpwall   getpwall() -> list_of_entries
getpwnam   getpwnam(name) -> (pw_name,pw_passwd,pw_uid,
getpwuid   getpwuid(uid) -> (pw_name,pw_passwd,pw_uid,



Thats pretty cool Peter. I can see me using that pretty often.
I think I'll be stealing that for my collection of useful tools. :-)

Kudos to Jim for the concept too.

--
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 with writing a bean bag toss game in python

2015-05-20 Thread Alan Gauld

On 20/05/15 03:53, Grace Anne St Clair-Bates wrote:

Hello! I am trying to code a bean-bag toss game in python where the program
generates three random dots inside a turtle graphic. Each different hole
means a different score, but I don't know how to write the function to
calculate the total score with all three random points and display the
score on the score board. It is hard to explain,


One of the things you learn in programming is that you can't write code 
until you understand the concept. So before you worry about the code you 
need to think more about what you are trying to do. Once you can 
articulate the problem the solution will be easier to see.


And if you can't explain what you are trying to do there is little 
chance of us guessing what it is.


As a start you could try sketching the user interface of your game.
Name the elements. Then describe the interaction between the user
and the game. (FWIW This technique is known as writing a use-case
in software engineering) Include all the alternative outcomes for
a given action.

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


Re: [Tutor] Help with writing a bean bag toss game in python

2015-05-20 Thread Laura Creighton
In a message of Tue, 19 May 2015 19:53:00 -0700, Grace Anne St Clair-Bates writ
es:
>Hello! I am trying to code a bean-bag toss game in python where the program
>generates three random dots inside a turtle graphic. Each different hole
>means a different score, but I don't know how to write the function to
>calculate the total score with all three random points and display the
>score on the score board. It is hard to explain, but if anyone has an idea
>please let me know! thanks

Hello, and welcome.
I am a bit confused here.  Are you trying to make an electronic version
of the game pictured here:
http://media.kohls.com.edgesuite.net/is/image/kohls/1070643?wid=500&hei=500&op_sharpen=1

So by 'turtle graphic' you mean a real picture of a turtle,

or are you using the python turtle module to make something else
called turtle graphics -- graphics made as if you were a turtle
crawling around the screen?   https://docs.python.org/2/library/turtle.html

In either case, can you show us some code?

Thanks very much,
Laura Creighton


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