[Tutor] Function help

2014-02-23 Thread Scott W Dunning
I am VERY new to python (programming too).  I had a question regarding 
functions.  Is there a way to call a function multiple times without recalling 
it over and over.  Meaning is there a way I can call a function and then add *5 
or something like that?  I am trying to code an American Flag using turtle for 
class so I’ll post the code I have so far below.  As you can see towards the 
bottom I recall the functions to draw the stars, fill in color and give it 
spacing.  I was wondering if there was a way to cut down on all that some how?  

Thanks for any help!

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


Re: [Tutor] Function help

2014-02-23 Thread Scott W Dunning

On Feb 23, 2014, at 1:12 AM, Scott W Dunning  wrote:

> I am VERY new to python (programming too).  I had a question regarding 
> functions.  Is there a way to call a function multiple times without 
> recalling it over and over.  Meaning is there a way I can call a function and 
> then add *5 or something like that?  I am trying to code an American Flag 
> using turtle for class so I’ll post the code I have so far below.  As you can 
> see towards the bottom I recall the functions to draw the stars, fill in 
> color and give it spacing.  I was wondering if there was a way to cut down on 
> all that some how?  
> 
> Thanks for any help!
> 
> Scott

from turtle import *
from math import sin, sqrt, radians

def star(width):
R = (width)/(2*sin(radians(72)))
A = (2*width)/(3+sqrt(5))
penup()
left(18)
penup()
forward(R)
pendown()
left(162)
forward(A)
right(72)
forward(A)
left(144)
forward(A)
right(72)
forward(A)
left(144)
forward(A)
right(72)
forward(A)
left(144)
forward(A)
right(72)
forward(A)
left(144)
forward(A)
right(72)
forward(A)
penup()
left(162)
forward(R)
left(162)

showturtle()

def fillstar(color):
fillcolor(color)
begin_fill()
star(25)
end_fill()

red = "red"
fillstar(red)

def space(width):
penup()
forward(2*width)
pendown()
space(25)

fillstar(red)
space(25)
fillstar(red)
space(25)
fillstar(red)
space(25)
fillstar(red)
space(25)
fillstar(red)
space(25)

def row(width):
penup()
right(90)
forward(width)
right(90)
forward(11*width)
right(180)
pendown()
row(25)

fillstar(red)
space(25)
fillstar(red)
space(25)
fillstar(red)
space(25)
fillstar(red)
space(25)
fillstar(red)
space(25)

row(25)

fillstar(red)
space(25)
fillstar(red)
space(25)
fillstar(red)
space(25)
fillstar(red)
space(25)
fillstar(red)
space(25)
fillstar(red)
space(25)

row(25)

fillstar(red)
space(25)
fillstar(red)
space(25)
fillstar(red)
space(25)
fillstar(red)
space(25)
fillstar(red)
space(25)

row(25)

fillstar(red)
space(25)
fillstar(red)
space(25)
fillstar(red)
space(25)
fillstar(red)
space(25)
fillstar(red)
space(25)
fillstar(red)
space(25)

row(25)

fillstar(red)
space(25)
fillstar(red)
space(25)
fillstar(red)
space(25)
fillstar(red)
space(25)
fillstar(red)
space(25)

row(25)

fillstar(red)
space(25)
fillstar(red)
space(25)
fillstar(red)
space(25)
fillstar(red)
space(25)
fillstar(red)
space(25)
fillstar(red)
space(25)

row(25)

fillstar(red)
space(25)
fillstar(red)
space(25)
fillstar(red)
space(25)
fillstar(red)
space(25)
fillstar(red)
space(25)

row(25)

fillstar(red)
space(25)
fillstar(red)
space(25)
fillstar(red)
space(25)
fillstar(red)
space(25)
fillstar(red)
space(25)
fillstar(red)
space(25)






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


Re: [Tutor] Function help

2014-02-23 Thread Peter Otten
Scott W Dunning wrote:

> 
> On Feb 23, 2014, at 1:12 AM, Scott W Dunning  wrote:
> 
>> I am VERY new to python (programming too).  I had a question regarding
>> functions.  Is there a way to call a function multiple times without
>> recalling it over and over.  Meaning is there a way I can call a function
>> and then add *5 or something like that?  I am trying to code an American
>> Flag using turtle for class so I’ll post the code I have so far below. 
>> As you can see towards the bottom I recall the functions to draw the
>> stars, fill in color and give it spacing.  I was wondering if there was a
>> way to cut down on all that some how?
>> 
>> Thanks for any help!

The example helps a lot! And of course this process of "cutting down", 
achieving complex tasks by doing simple things repetetively is what 
programmers do all the time. There is even a principle called "DRY" (don't 
repeat yourself) meaning that if you have to write something twice in your 
code you are doing it wrong.

Looking at

> fillstar(red)
> space(25)
> fillstar(red)
> space(25)
> fillstar(red)
> space(25)
> fillstar(red)
> space(25)
> fillstar(red)
> space(25)

a programmer would think "for loop" immediately

for i in range(5):
fillstar(red)
space(25)

and as this sequence occurs more than once in your code you should make it a 
function. For example:

def star_row():
for i in range(5):
fillstar(red)
space(25)

If you want to make rows with more or less stars, or stars in other colors 
you could add parameters:

def star_row(numstars, starcolor):
for i in range(numstars):
fillstar(starcolor)
space(25)

Your code will then become

star_row(6, red)
row(25)
star_row(5, red)
row(25)
...

which still shows a repetetive pattern and thus you can simplify it with 
another loop. You should be able to find a way to write that loop with two
star_row() calls on a single iteration, but can you do it with a single call 
too?


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


Re: [Tutor] Function help

2014-02-23 Thread Ben Finney
Scott W Dunning  writes:

> I am VERY new to python (programming too).

Welcome!

You should establish the fundamentals of Python, by working through the
Python tutorial http://docs.python.org/3/tutorial/>.

Begin at the beginning, and execute all the examples, and experiment
with them to satisfy yourself that you understand what is being
demonstrated; then, move on to the next. Continue until you have a good
grasp of all the basics of Python.

> I had a question regarding functions.  Is there a way to call a
> function multiple times without recalling it over and over.

When you have worked through the tutorial, you will understand how loops
can address problems like this. Enjoy learning the basics of programming!

-- 
 \ “As scarce as truth is, the supply has always been in excess of |
  `\   the demand.” —Josh Billings |
_o__)  |
Ben Finney

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


Re: [Tutor] Function help

2014-02-23 Thread Dave Angel
 Scott W Dunning  Wrote in message:
> I am VERY new to python (programming too).  I had a question regarding 
> functions.  Is there a way to call a function multiple times without 
> recalling it over and over.  Meaning is there a way I can call a function and 
> then add *5 or something like that?  I am trying to code an American Flag 
> using turtle for class so I’ll post the code I have so far below.  As you 
> can see towards the bottom I recall the functions to draw the stars, fill in 
> color and give it spacing.  I was wondering if there was a way to cut down on 
> all that some how?  
> 

Welcome to the tutor forum also, Scott.  You'll find it works very
 similarly to python-list,  and has many of the same people on it.
 I'm not sure how you tried to attach source,  but please be aware
 that this is a text list - anything other than plain text will
 probably be invisible or inconvenient to someone. Just paste
 snippets inline when needed. 

What you're looking for is a loop. for and while are the two
 keywords for looping.  In this case,  since you know how many
 times you want to go round, loop is appropriate. Build a
 collection or iterator of length 5, and loop over it. range is
 designed for the purpose:

for index in range (5):
 dosomething
 moresomething (index)

Everything that's indented will happen 5 times. Later you'll want
 to learn continue and break, which can alter the simple flow. And
 presumably you already know if.

import,  for, while, if, elif, else, break, continue

Incidentally,  your star function could have been much shorter
 with a loop.

-- 
DaveA

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


Re: [Tutor] Responding Tweet: A Twitter Bot

2014-02-23 Thread Alexandre BM
Here is a twitter bot that I created, it evaluates math expression then
it reply to the sender
You can study how it works it's pretty simple :)  [1]

[1] https://github.com/rednaks/Twitter-Math-Bot

On 20/02/2014 09:23, Zaki Akhmad wrote:
> Hello,
>
> I am trying to create a twitter bot which respond mentions as soon as
> possible. Here's the detail.
>
> If @someone mention my account @example then in t seconds I would
> respond @someone tweet. I am hoping to get this t less than 60
> seconds.
>
> I would utilize the Mike Verdone twitter API python[1]. My question is
> how to get the mentions as soon as possible, as @someone mention
> @example? Any python library could help me? Hint please.
>
> Previously, I use cron to search for tweets that mentioned @example account.
>
> [1]https://pypi.python.org/pypi/twitter
>
> Thank you,
>

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


[Tutor] I can't understand where python class methods come from

2014-02-23 Thread voger
I have a really hard time understanding where methods are defined in 
python classes. My first contact with programming was with C++ and Java
and even if I was messing with them in a very amateurish level, I could 
find where each class was defined and what methods were defined inside 
them. Everything was clear and comprehensive. With python everything 
looks like magic. Methods and properties appearing out of nowhere and 
somehow easing the task at hand. I am not complaining for their 
existence but I really can't understand how and what is defined so I can 
use it.


Enough ranting and let me give an example of what I mean.

Let's have a look at the pygeocoder library located here:
http://code.xster.net/pygeocoder/src/c863f907f8a706545fa9b27959595f45f8c5/pygeolib.py?at=default

and a short tutorial on usage by someone unrelated to the author located 
here: 
http://www.pixelite.co.nz/article/validate-and-clean-user-submitted-address-data-pygeocoder?&ei=518KU5mcFeim4ASG34Fo?&usg=AFQjCNFgMx8s7wPP6R6CyLLoSAE-Ohh51Q?&sig2=afBUF9yuxbZYB9fcfK9Lmw?&bvm=bv.61725948,d.bGE


The tutorial basicaly says in code that once we obtain the data we can do:

print "address.valid_address: ", address.valid_address
print "address.street_number: ", address.street_number
print "address.route: ", address.route
print "address.sublocality: ", address.sublocality
print "address.locality: ", address.locality
print "address.administrative_area_level_1: ", 
address.administrative_area_level_1

print "address.country: ", address.country
print "address.postal_code: ", address.postal_code
print "address.coordinates: ", address.coordinates
print "address.formatted_address: ", address.formatted_address

Some properties I can see them defined but some others like sublocality 
or administrative_area_level_1 I don't see them defined anywhere. Also 
in the comments in the source code the author says


#You can also choose a different property to display for each lookup #type.
#Example:
#result.country__short_name

but I can't understand where that __short_name comes from

I hope I didn't bore you with this long text but this is really 
confusing for me and I hope someone can help me understand these things.

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


Re: [Tutor] I can't understand where python class methods come from

2014-02-23 Thread Ben Finney
voger  writes:

> I have a really hard time understanding where methods are defined in
> python classes.

Reading the rest of your message, I think you're saying that you can't
locate *which* class defines a method

> […] With python everything looks like magic.

It can seem that way. Fortunately, most of the magic needs to be
explicitly declared in the code, so it's a matter of knowing what to
look for.

In this message, I'll guide you in when you should expect magic, and
where to look for it.


> Methods and properties appearing out of nowhere and somehow easing the
> task at hand. I am not complaining for their existence but I really
> can't understand how and what is defined so I can use it.

This is a valid complaint, and certainly some libraries can get rather
too clever with the “magic behaviour”, leading to difficulty in
understanding what is happening well enough to debug problems.

> Enough ranting and let me give an example of what I mean.
>
> Let's have a look at the pygeocoder library located here:
> http://code.xster.net/pygeocoder/src/c863f907f8a706545fa9b27959595f45f8c5/pygeolib.py?at=default

The important part of that file is the ‘GeocoderResult’ class
definition. It defines a lot of magic behaviour.

In general, the magic often happens with “dunder”-named attributes; that
is, attributes named with a leading and traling double-underscore.

An attribute named “__foo__” is a strong indication that it will be
treated specially and be invoked by the Python interpreter, *without*
the programmer needing to invoke the attribute by name.

Here is the class definition showing just the dunder-named attributes::

class GeocoderResult(collections.Iterator):

…

def __init__(self, data):
…

def __len__(self):
…

def __iter__(self):
…

def __getitem__(self, key):
…

def __unicode__(self):
…

if sys.version_info[0] >= 3:  # Python 3
def __str__(self):
return self.__unicode__()

def __next__(self):
return self.return_next()
else:  # Python 2
def __str__(self):
return self.__unicode__().encode('utf8')

def next(self):
return self.return_next()

…

def __getattr__(self, name):
…


So already you should be on high alert: there is a large amount of
“magic” in this class! Seemlingly-simple uses of this class and its
instances will result in invocations of those custom methods, even
without seeing the names used explicitly.

> Some properties I can see them defined but some others like
> sublocality or administrative_area_level_1 I don't see them defined
> anywhere.

The specific magic happening here is that, when you access an attribute
on an object, Python will allow interception of that request by a custom
‘__getattr__’ method. As you can see, the ‘GeocoderResult’ class defines
that method for its instances, so you will need to look there for custom
behaviour when accessing attributes on instances of this class.

http://docs.python.org/3/reference/datamodel.html#object.__getattr__>

Similarly, the class defines ‘__getitem__’ to intercept ‘foo[bar]’, it
defines ‘__len__’ to intercept ‘len(foo)’, it defines ‘__iter__’ to
intercept attempts to iterate over an instance; and so on. Much magic!

Personally, I'm suspicious whether this class really needs to be so
awfully clever; it is certainly making the behaviour less intuitive, as
demonstrated in your case. It smells of a poor design.

> I hope I didn't bore you with this long text but this is really
> confusing for me and I hope someone can help me understand these
> things.

One good decision made by the Python core developers early on was a
convention of naming these special, magical-behaviour “hook” attributes
in a way that they are clearly special and signal to the reader that
Python will be using them implicitly.

So the general advice is to understand that any “dunder”-named attribute
needs to be researched, because it can be invoked *without* you
necessarily being aware of that.

Hope that helps!

-- 
 \“Choose mnemonic identifiers. If you can't remember what |
  `\mnemonic means, you've got a problem.” —Larry Wall |
_o__)  |
Ben Finney

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


Re: [Tutor] I can't understand where python class methods come from

2014-02-23 Thread Alan Gauld

On 23/02/14 21:59, voger wrote:

I have a really hard time understanding where methods are defined in
python classes. My first contact with programming was with C++ and Java


Ben has already addressed much of your confusion.
I'd just add that if you look at C++ operator overloading it is very 
similar to Python's except that the C++ operators are more literally 
like their usage.


Thus in C++ to overload the plus sign you write something like
(I'm a bit rusty so the detail might be wrong...):

class C{
public:
   C(int n){
  this.data = n;
   };

   C* operator+(C c){
  return new C(c.data + this.data);
   };
};

myC1 = C(42);
myC2 = C(66);

void main(){
   cout << myC1 + myC2;
}

In Python that is very like

class C:
  def __init__(self,n):
 self.data = n
  def __add__(self, aCobj):
 return self.data + aCobj.data

c1 = C(42)
c2 = C(66)
print (c1 + c2)

In both languages a bit of magic happens to turn __add__ or
operator+ into the infix + operator.


find where each class was defined and what methods were defined inside
them.


Are you sure? That has always been one of the big challenges in OOP, 
finding where operations are defined. Especially in deep inheritance 
hierarchies. That's where explorer type tools can help.

Python provides such help via the help() function.
Often it will show you where a published method is defined.


Everything was clear and comprehensive. With python everything
looks like magic. Methods and properties appearing out of nowhere and
somehow easing the task at hand.


They are always defined but the location and timing can be different. 
That's part of the dynamic nature of Python.


[ Just be thankful it's not lisp which allows you to change all aspects 
of the objects, including the method search algorithm, at runtime...]



Let's have a look at the pygeocoder library located here:
http://code.xster.net/pygeocoder/src/c863f907f8a706545fa9b27959595f45f8c5/pygeolib.py?at=default


There are many "smart" third party modules.
Sometimes for good reasons, sometimes because the author found
a clever solution and wanted to use it. Python is a broad church
and while the standard library is fairly conventional for the
most part third party libraries are the sole responsibility
of their maintainers and may not be so readily understood.

But then again many C libraries are obscure in the extreme,
that's not the language's fault, it's just  open and flexible.
You have the choice to use it without fully understanding,
spend time to understand, find an alternative or write your
own...

hth
--
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] I can't understand where python class methods come from

2014-02-23 Thread James Scholes
voger wrote:
> Some properties I can see them defined but some others like
> sublocality or administrative_area_level_1 I don't see them defined
> anywhere. Also in the comments in the source code the author says
> 
> #You can also choose a different property to display for each lookup
> #type. #Example: #result.country__short_name
> 
> but I can't understand where that __short_name comes from

The relevant code is in the __getattr__ method.  From the Python docs:

> If no class attribute is found, and the object’s class has a
> __getattr__() method, that is called to satisfy the lookup.
>
> -- Source:
> http://docs.python.org/2/reference/datamodel.html

As an aside, your examples are attributes, not methods.  Read the code
below; this is where the magic happens:

def __getattr__(self, name):
lookup = name.split('__')
attribute = lookup[0]

if (attribute in GeocoderResult.attribute_mapping):
attribute = GeocoderResult.attribute_mapping[attribute]

try:
prop = lookup[1]
except IndexError:
prop = 'long_name'

for elem in self.current_data['address_components']:
if attribute in elem['types']:
return elem[prop]
-- 
James Scholes
http://twitter.com/JamesScholes
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor