Monday, May 20, 2013
Python module learning notes
This afternoon when I tried to add Guzunty Pi to the HC137 decoder circuit, I found the decoder seems overloaded and could not output a low bit. I removed the Guzunty Pi, and the whole things works again. But then even 2 EEPROMs seem to overload the circuit. I tied only 1 EEPROM and everything works fine. But there seemed an intermittant short circuit somewhere and loaded down the 3V3 to about 2V0. I tidied up the messy wiring and the short circuit disappeared. But then even with no EEPROM the decoder still could not decode.
I gave up after trying my luck for an hour, doing various tests. But still found things behaving strangely. So I gave up and went for supper. On my way to eat, I suddenly reminded myself that there should be an oscillation, because the problem happened when I tried to add the Guzunty Pi LED module which uses a high frequency clock, perhaps from 5MHz down to 2K. One other thing is that I am using long wring of about 1m for the SPI connection. So it was rather sure that there was an oscillation.
One thing I must do now is forget Guzunty Pi, for the time being. I also remember that some weeks earlier I found GPi could not start or get hanged, perhaps it was also related to oscillation.
The other problem is that when I tried to find functions to do troubleshooting, I found it more and more difficult to find the functions I want, because my program is getting to a laughably 4,000 lines long! It is like a big wot of chop suet and fried noddle. It is hard to find which function is buried somewhere.
I started learning Python last October, so it is about 7 months now. I have been too lazy to learn new things like module and objects. But I think it is time to take a break from hardware and try to organize my messy code into modules. Then hopefully my troubleshooting life would be easier.
I googled the following Python Module tutorial.
Python » Documentation » The Python Tutorial » Python Modules
http://docs.python.org/2/tutorial/modules.html
6. Modules
If you quit from the Python interpreter and enter it again, the definitions you have made (functions and variables) are lost. Therefore, if you want to write a somewhat longer program, you are better off using a text editor to prepare the input for the interpreter and running it with that file as input instead. This is known as creating a script. As your program gets longer, you may want to split it into several files for easier maintenance. You may also want to use a handy function that you’ve written in several programs without copying its definition into each program.
To support this, Python has a way to put definitions in a file and use them in a script or in an interactive instance of the interpreter. Such a file is called a module; definitions from a module can be imported into other modules or into the main module (the collection of variables that you have access to in a script executed at the top level and in calculator mode).
A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended. Within a module, the module’s name (as a string) is available as the value of the global variable __name__. For instance, use your favorite text editor to create a file called fibo.py in the current directory with the following contents:
# Fibonacci numbers module
def fib(n): # write Fibonacci series up to n
a, b = 0, 1
while b < n:
print b,
a, b = b, a+b
def fib2(n): # return Fibonacci series up to n
result = []
a, b = 0, 1
while b < n:
result.append(b)
a, b = b, a+b
return result
Now enter the Python interpreter and import this module with the following command:
>>>
>>> import fibo
This does not enter the names of the functions defined in fibo directly in the current symbol table; it only enters the module name fibo there. Using the module name you can access the functions:
>>>
>>> fibo.fib(1000)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
>>> fibo.fib2(100)
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
>>> fibo.__name__
'fibo'
If you intend to use a function often you can assign it to a local name:
>>>
>>> fib = fibo.fib
>>> fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377
...
.END
No comments:
Post a Comment