Playing with Turtle XYZ

3D Rendering has always interested me, but I’ve never taken the time to mess around with it much.

When I was a kid I used to do stuff like this in Basic.  But I didn’t know trig or other similar functions so I was left to basic math and the random functions.

Here is a little turtle program (python) that will make a wrinkled fabric type display.

2015-12-18 - B163539.png

Programming

Hi, this is Eli.  One of our school assignments is to completely learn our math tables by heart.   We wrote a python program to help us practice with this.  This is a program for multiplication tables 1-12.

9-10-2014 9-31-13 PM

Ezra and I are learning how to program in python.  Dad helped us get the structure of this program right, but we entered into IDLE and debugged it ourselves.

Here is an example of the program in action:

9-10-2014 9-36-40 PM

When you activate the program it  asks you how many problems you want – you can go from 1 to 144 problems.

When you are done with your problems it tells you how long you took and how many problems you got right and how many problems you got right and how long it took you to answer each problem.

That’s all for now!

Python: ‘tuple’ object is not callable

This can be a bit of an obscure error, if you run into it…  It looks like this:

File ".../CCRM/Content.py", line 202, in Page_Update
    ('Nav1'       , Data.Nav1),
TypeError: 'tuple' object is not callable

In reality, it’s typically caused by accidentally forgetting a comma from the line before:

    Page_MNID = App.DB.Value('''
      UPDATE
        "Dashboard"."Page"
      SET
        [Field=Value]
      WHERE True
        AND "Page_MNID" = $Page_MNID
      ''',
      ('ScriptPath' , Data.ScriptPath)
      ('Nav1'       , Data.Nav1),
      ('Nav2_Icon'  , Data.Nav2_Icon),
      ('Nav2_Label' , Data.Nav2_Label),
      ('Title'      , Data.Title),
      ('Active'     , Data.Active),
      Page_MNID     = Data.Page_MNID,
      )

Notice that line 9 is missing a comma at the end?  That causes python to see this:

tuple_object = ("ScriptPath", Data.ScriptPath")
tuple_object("Nav1" , Data.Nav1)  #eg, next tuple looks like params

Solution?  Just add the comma :)

TypeError: list indices must be integers

You start your day, happily working with dictionaries…Life is good.

>>> mydict = {'key-a': 'value-a', 'key-b': 'value-b'}
>>> mydict['key-a']
'value-a'

All of a sudden, storm clouds appear. Your dictionary variable accidentally gets assigned a list, and life is no longer good!

>>> mydict = {'key-a': 'value-a', 'key-b': 'value-b'}
>>> mydict['key-a']
'value-a'
>>> mydict = []
>>> mydict['key-a']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers

The error message "TypeError: list indices must be integers" is generated when you attempt to use a non-int for a index inside the [] operator.

By the way, almost the same message is used if you do this with a tuple:

TypeError: tuple indices must be integers

Great article on Python super, __mro__, and attributes

I highly recommend reading this excellent writeup on Python super(), python __mro__, python attributes, and more.

It is Copyright © 2005-2009 Shalabh Chaturvedi

http://www.cafepy.com/article/python_attributes_and_methods/python_attributes_and_methods.html#method-resolution-order

Python 3 bytes indexing returns integers!

While converting a bunch of werkzeug code to Python 3.1, I ran into an issue with one of the bytes objects that was previously a string.

The line of code in question was originally this:

if value and value[0] == value[-1] == b'"':
   ...

If value is something, and the first and last characters are a quote character, then…

However, as it turns out, the bytes object is actually a list of integers in the range of 0-255.

So when you use an indexing operation on a byte, you actually get an integer back. For example:

>>> x = b'Hello World'
>>> x[0]
72

>>> x = 'Hello World'
>>> x[0]
'H'

Big difference, eh?

Python has keyword only parameters!

Maybe I should have known this about Python by now, but…

Little did I know (was wishing for it today), but there IS a way to specify that arguments MUST be keyword only.

>>> def foo(a,b,*,c,d):
...     print(a,b,c,d)
...
>>> foo(1,2,3,4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: foo() takes exactly 2 positional arguments (4 given)
In this case, everything after the * is required to be keyword only. This means that you can safely amend the function definition to re-order or even add keyword arguments.
Likewise, the following requires ALL keyword arguments.
>>> def foo(*,a,b):
...     print(a,b)
...
>>> foo(1,2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: foo() takes exactly 0 positional arguments (2 given)

>>> foo(a=1, b=2)
1 2


Read more about it at:

http://www.python.org/dev/peps/pep-3102/