This is a Python module which provides the facilities for the polynomial evaluator. It is really nothing more than a bunch of Python code (mostly function definitions) inside a file. The name of the module is the name of the file, plus the .py suffix. The module name is not given in the text of the file, so i can rename module by simply renaming the file.
After a module has been used, Python will create a file with the same name and the extension .pyc. This is the byte code for the module. Python creates or recreates these as needed, so i really don't need to do anything about them.
Problem is: Python does not create .pyc extension. Why?
# This module contains operations to manipulate polynomials.
#
# Need some string services, and some standard system services.
import string, sys
#
# Function to evaluate a polynomial at x. The polynomial is given
# as a list of coefficients, from the greatest to the least. It returns
# the value of the polynomial at x.
def eval(x, poly):
'''Evaluate at x the polynomial with coefficients given in poly.
The value p(x) is returned.'''
sum = 0
while 1:
sum = sum + poly[0] # Add the next coef.
poly = poly[1:] # Done with that one.
if not poly: break # If no more, done entirely.
sum = sum * x # Mult by x (each coef gets x right num times)
return sum
def read(prompt = '', file = sys.stdin):
'''Read a line of integers and return the list of integers.'''
# Read a line
if prompt: print prompt,
line = file.readline()
if not line:
raise EOFError, 'File ended on attempt to read polynomial.'
line = line[:-1]
if line == 'quit':
raise EOFError, 'Input quit on attempt to read polynomial.'
# Go through each item on the line, converting each one and adding it
# to retval.
retval = [ ];
for str in string.split(line):
retval.append(int(str))
return retval
#
# Create a string of the polynomial in sort-of-readable form.
def srep(p):
'''Print the coefficient list as a polynomial.'''
# Get the exponent of first coefficient, plus 1.
exp = len(p)
# Go through the coefs and turn them into terms.
retval = ''
while p:
# Adjust exponent. Done here so continue will run it.
exp = exp - 1
# Strip first coefficient
coef = p[0]
p = p[1:]
# If zero, leave it out.
if coef == 0: continue
# If adding, need a + or -.
if retval:
if coef >= 0:
retval = retval + ' + '
else:
coef = -coef
retval = retval + ' - '
# Add the coefficient, if needed.
if coef != 1 or exp == 0:
retval = retval + str(coef)
if exp != 0: retval = retval + '*'
# Put the x, if we need it.
if exp != 0:
retval = retval + 'x'
if exp != 1: retval = retval + '^' + str(exp)
# For zero, say that.
if not retval: retval = '0'
return retval
See here:
When a script is run by giving its name on the command line, the bytecode for the script is never written to a ".pyc" or ".pyo" file. Thus, the startup time of a script may be reduced by moving most of its code to a module and having a small bootstrap script that imports that module.
Thus, if you want to create a .pyc
file, you have to import the script, not just run it from the command line. For example, you could do
python -c "import myscript"
instead of doing
python myscript.py