Gribouillis 1,391 Programming Explorer Team Colleague

A few remarks, because I think the brute force approach is definitely not the good one. First you are not working with base 16 digits, but with base 256 digits because each of your characters has 2 hexadecimal digits. Second, how would you do in base 10 and an incomplete set of digits to find 3 numbers with a given sum ? This is an incomplete addition. Write it on paper, and will see that you won't use a brute force approach.
When you add 3 digits, there is a carry which can only be 0, 1, 2. The same holds in base 256. Since your result has only 4 digits(256), you are left with 3**3 = 27 possibilities for these carry numbers. For each of this 27 possibilities, you only need to find (4 times) 3 digits which sum is a given number, which is a very simple problem.
So you don't need to go through the hundreds of million cases of the brute force approach.

Gribouillis 1,391 Programming Explorer Team Colleague

The list sys.argv contains only strings, not numerical values. So you must convert your argument with offset = int(sys.argv[1]) . Python thought you were using the string modulus operator, which has a completely different meaning.

vegaseat commented: right on +10
Gribouillis 1,391 Programming Explorer Team Colleague

First when you iterate over the lines, there is a newline character at the end of each line. This newline can be removed with line = line.rstrip("\r\n") . You should write this as the first statement in the for loop. Then I suggest that you invoke an output function instead of printing lines. It means that you replace all the print line[4:] by output(line[4:]) . This allows you to change the effect of your code just by defining the output function. For example you can write

import sys
def output(data):
    sys.stdout.write(data)

Later, you can change this function definition to print to a file, or add newlines, etc.

chavanak commented: Helped me a lot and answered my question +1
Gribouillis 1,391 Programming Explorer Team Colleague

You can try this

# python 3
import re
words = {'i':'jeg','read':'leste','book':'boka'}
articles = set(["the", "a"])
while True:
    sentence =input('Write something: ')
    if sentence == 'quit':
        break
    sentence = sentence.split()
    result = []
    art = []
    for word in sentence:
        word_mod = re.sub('[^a-z0-9]', '', word.lower())
        punctuation = word[-1] if word[-1].lower() != word_mod[-1] else ''
        if word_mod in articles:
            art.append(word_mod)
        elif word_mod in words:
            result.append(words[word_mod] + punctuation)
        else:
            result.append(word)

    result = ' '.join(result).split('. ')
    print('. '.join(s.capitalize() for s in result))
    if art:
        print("It seems that there are no articles in Norwegian, so I removed '{0}'.".format(", ".join(art)))
print('See you!')
Gribouillis 1,391 Programming Explorer Team Colleague

There is no memory leak in python. Python uses a generational garbage collector which deletes objects when there is no reference left to these objects. You can have look at the gc module which gives you a few functions to interact with the garbage collector, but normally you don't have to.

Of course, it's the programmer's responsibility to ensure that there is no reference to the object. If a living object A has a member B which is a dictionary which contains a value D one of which member is your bitmap object, then the bitmap will not be freed ! The role of the garbage collector is to solve cyclic references.

You should avoid defining __del__ methods for your objects (see gc.garbage).

Every python object stores an integer which is a count of the existing references to the object. An object with a positive refcount will not be freed by the garbage collector. All the objects you can access from your program have a positive refcount (so that you will never see an object with a refcount of 0). However, there is a method sys.getrefcount() which can help you detect hidden references to your object.

I've heard of a special case of 'memory leak' in python: when you load an extension module by importing a shared library, as far as I know, there is no way to 'unload' the library.

Gribouillis 1,391 Programming Explorer Team Colleague

I think I've found what async_chat did with your unicode string, it encoded it in utf32 before transmitting it. Here are a few tests with python 2.6

>>> s = '2\x00\x00\x000\x00\x00\x00,\x00\x00\x001\x00\x00\x007\x00\x00\x00,\x00\x00\x001\x00\x00\x008\x00\x00\x00,\x00\x00\x001\x00\x00\x009\x00\x00\x00,\x00\x00\x00'
>>> t = s.decode('utf32')
>>> t
u'20,17,18,19,'
>>> t.encode('utf32')
'\xff\xfe\x00\x002\x00\x00\x000\x00\x00\x00,\x00\x00\x001\x00\x00\x007\x00\x00\x00,\x00\x00\x001\x00\x00\x008\x00\x00\x00,\x00\x00\x001\x00\x00\x009\x00\x00\x00,\x00\x00\x00'

When encoding, a header with 4 bytes is added, but this is systematic because if you encode the empty unicode string, you get

>>> empty = u''
>>> empty.encode('utf32')
'\xff\xfe\x00\x00'

so async_chat removed this header.
So an alternative to pushing str(allLists) is to write myString = myString.decode('utf32') in the client. This allows you to pass unicode strings (but in that case, you should always pass unicode strings).

Gribouillis 1,391 Programming Explorer Team Colleague

It's not a string, it's a unicode string. That could be the problem. Try to send str(allLists).

vegaseat commented: neat +10
Gribouillis 1,391 Programming Explorer Team Colleague

This is just a hint

>>> s = "-9999 -9999 -9999 -9999 0.004537001 0.004537001 0.004537001   "
>>> s
'-9999 -9999 -9999 -9999 0.004537001 0.004537001 0.004537001   '
>>> s.strip()  # remove white space at both ends of s
'-9999 -9999 -9999 -9999 0.004537001 0.004537001 0.004537001'
>>> s.strip().split()   # split s on remaining white space
['-9999', '-9999', '-9999', '-9999', '0.004537001', '0.004537001', '0.004537001']
>>> [float(v) for v in s.strip().split()]
[-9999.0, -9999.0, -9999.0, -9999.0, 0.0045370009999999997, 0.0045370009999999997, 0.0045370009999999997]

Also, if you find too many lines, the number 1 debugging tool is to print your results and compare them to whatever you are expecting. If there are too many values, you can also print to a file.

AnnetteM commented: This post is very helpful +1
Gribouillis 1,391 Programming Explorer Team Colleague

You could start filling a dictionary with knowledge, like this

holydays_dict = {
    (2, 18) : "February 18 is Presidents' Day.",
}

Then find how your program could select an entry in the dictionary.

Gribouillis 1,391 Programming Explorer Team Colleague

It seems that your communication protocol adds null characters "\x00" in the string. The first thing you could do is remove these characters with

myString = myString.replace("\x00", "")

The other question, as you said, is to understand why your server sends these null chars. Without the server and client's code, it's difficult to guess.

Gribouillis 1,391 Programming Explorer Team Colleague

Colors in a windows console
This is a little bit off topic, but since the previous post shows you how to print in colors in a linux console, well, I found a link of someone who prints in colors in a windows console using module ctypes http://www.burgaud.com/bring-colors-to-the-windows-console-with-python/ . A nice idea would be to wrap his code in Style objects as in the above snippet and create a cross-platform way to print in colors in a console !

edit: since this post was written, module colorama appeared, which achieves this goal.

Gribouillis 1,391 Programming Explorer Team Colleague

I once wrote a code snippet that parses command lines the way a C program parses argv. You could use it for your problem: save http://www.daniweb.com/code/snippet234768.html as argv.py

>>> from argv import buildargv
>>> print buildargv("10210 'ARDNA 10' 110.00 1 0.00 0.00 1 1 1.0432 -3.954"))
['10210', 'ARDNA 10', '110.00', '1', '0.00', '0.00', '1', '1', '1.0432', '-3.954']

On a command line, the rule is the same: space is preserved in single quoted or double quoted arguments.

Gribouillis 1,391 Programming Explorer Team Colleague

If you want a practical access to the data, the best way is to use a class

class Atom(object):
    def __init__(self, num, elt, neigh=()):
      self.num = num
      self.elt = elt
      self.neigh = set(neigh)

    def __str__(self):
        return "Atom(%s, %s)" % (self.num, self.elt)

    def __lt__(self, other):
        return self.num < other.num

    def neigh_elts(self):
        for atom in sorted(self.neigh):
            yield atom.elt      

def display_atoms(dic):
    for num, atom in sorted(dic.items()):
        print atom, list(atom.neigh_elts())

AllAtoms = {}
AllAtoms[0] = Atom(0, 0)
AllAtoms[1] = Atom(1, 0, [AllAtoms[0]])
AllAtoms[2] = Atom(2, 0, [AllAtoms[0], AllAtoms[1]])

display_atoms(AllAtoms)

AllAtoms[0].elt = 9

print "Atom 0's element set to 9"
display_atoms(AllAtoms)

""" My output --->
Atom(0, 0) []
Atom(1, 0) [0]
Atom(2, 0) [0, 0]
Atom 0's element set to 9
Atom(0, 9) []
Atom(1, 0) [9]
Atom(2, 0) [9, 0]

"""

With a class, you can customize the access to data the way you want. You could also define a class AtomCollection instead of using a dict.

Gribouillis 1,391 Programming Explorer Team Colleague

but I want distinct strings.........and I don't want to loop it over to see if there is a existed string of the same.

You can use a set of strings

s = set(["hello", "world"])
s.add("anotherstring")
s.add("hello")
print s

or a dictionary (dict). Dict is the name of 'hash table' in python.

Gribouillis 1,391 Programming Explorer Team Colleague

I had to modify your code to run it on my system. This is the modified version with the result that both times are the same

import random, sys, os, struct

lib = os.path.dirname(os.__file__)
sourcefilepath = os.path.join(lib, "random%spy" % os.extsep)
testfilepath = sourcefilepath + "c"

istream = open(testfilepath, 'rb')

#Read and discard the first four bytes (the magic pyc identifier).
istream.seek(0)
istream.read(4)

#The last modification time of the source according to the file.
modtime = istream.read(4)
modtime = struct.unpack('i', modtime)[0] # 'L' would require 8 bytes (x86_64 ?)
print("Modtime extracted from random.pyc file")
print(modtime)

istream.close()

#Convert the bytes to an integer.

print('Retrieving source modification time using os.path.getmtime.')
print('According to os.path.getmtime the time the source file was last modified measured in the number of seconds from the last epoch is:')

print(os.path.getmtime(sourcefilepath))

"""
My output --->
Modtime extracted from random.pyc file
1262995199
Retrieving source modification time using os.path.getmtime.
According to os.path.getmtime the time the source file was last modified measured in the number of seconds from the last epoch is:
1262995199.0
"""

I'm running Mandriva Linux 2010 with 64 bits.

lrh9 commented: Sincerely cares about learning and helping others. +1
Gribouillis 1,391 Programming Explorer Team Colleague

Try this

from itertools import permutations
for t in permutations(['e', 'n', 'o', 't', 'r']):
    print t

;)

Gribouillis 1,391 Programming Explorer Team Colleague

The problem is that the print statement is executed before the testit thread had time to establish a connection and receive data. The only thing to do is to wait before the print statement. There are 2 ways to wait here:
First way: you wait until the testit thread is terminated before you make the print statement, for this, you must use the join method like this

current=testit("192.168.0.1")
current.start()
current.join()
print current.ip,current.status

This is probably not what you want to do, so the Second way to wait is to use a threading.Condition instance. You can do it this way

# from __future__ import with_statement # uncomment if python 2.5
from threading import Condition, Thread
from contextlib import contextmanager

@contextmanager
def acquired(condition):
    condition.acquire()
    try:
        yield condition
    finally:
        condition.release()

connection_done = Condition()

class testit(Thread):
    ....
    def run(self):
        try:
            port = 1000
            ...
            self.status = repr(data)
        finally:
            with acquired(connection_done):
                connection_done.notify()
        ...

current=testit("192.168.0.1")
with acquired(connection_done):
    current.start()
    connection_done.wait()
    if current.status == "down":
        print "testit thread could not establish a connection"
print current.ip,current.status
rlogan1 commented: nice response.Thanks +0
Gribouillis 1,391 Programming Explorer Team Colleague

This snippet defines a function restart_program() which restarts your python program from within your python program.

Pupo commented: Nice! +5
Gribouillis 1,391 Programming Explorer Team Colleague

Once you saved the file quantile.py in the same directory, if you want to use the quantile function in program.py, you have to put

from quantile import quantile

before you use quantile in program.py (typically at the top of the program).

Gribouillis 1,391 Programming Explorer Team Colleague
Gribouillis 1,391 Programming Explorer Team Colleague

This tutorial with the ld command seems a little old, and was certainly not written for x86_64. There are probably options to find for gcc and/or ld that would make your module. I'm afraid I can't help you more because I never compiled anything on mac OSX. Good luck !

edit: at the bottom of the tutorial page, there is a link to a zip file containing the example module. Since the author says that it worked on OS X in 2008, you should have a look at the makefile and write something similar ;)

Gribouillis 1,391 Programming Explorer Team Colleague

If you don't know that listtree is already a list, you can write it this way

def mirrored(item):
    if isinstance(item, list):
        return list(mirrored(x) for x in reversed(item))
    else:
        return item

About, the 'list comprehension syntax', when you write

mylist = [ function(x) for x in sequence if condition(x) ] # or list(...)

it has the same effect as

mylist = [] # or list()
for x in sequence:
    if condition(x):
        mylist.append(function(x))

except that the append method is not called.

Gribouillis 1,391 Programming Explorer Team Colleague

You could try something like this

def mirrored(listtree):
    return [(mirrored(x) if isinstance(x, list) else x)
                             for x in reversed(listtree)]

then

>>> a=[[1, 2], ['apple', 'orange']]
>>> mirrored(a)
[['orange', 'apple'], [2, 1]]
>>> a=[[1, 2], [['apple', 'banana', 5], 'orange']]
>>> mirrored(a)
[['orange', [5, 'banana', 'apple']], [2, 1]]
Gribouillis 1,391 Programming Explorer Team Colleague

This snippet defines a class OptCmd which adds line and options parsing capabilities to the python standard lib's cmd.Cmd class. It allows you to quickly write an extensible command line interpreter.

To use this module, you will need this other code snippet, saved as a module named 'argv.py'.

Enjoy the OptCmd class !

TrustyTony commented: neat +12
Gribouillis 1,391 Programming Explorer Team Colleague

Here is how you could start an interpreter for the food program with module cmd

from cmd import Cmd
import os, sys

class FoodCmd(Cmd):
    def __init__(self):
        Cmd.__init__(self)
        self.restart = False

    def do_quit(self, line):
        """exit the interpreter. With option -r, start a new one."""
        if line.strip() == "-r":
            self.restart = True
        return True

    def do_pid(self, line):
        print("My pid is %d" % os.getpid())

    def do_showline(self, line):
        print(repr(line))

if __name__ == "__main__":
    interpreter = FoodCmd()
    interpreter.prompt = "--food-- "
    interpreter.cmdloop("""
        ----------------------------------
        |  Welcome to the food program ! |
        ----------------------------------
""")
    if interpreter.restart:
        os.execl(sys.executable, sys.executable, __file__)

Then you can add do_* methods to add interactive commands. Here I defined 3 commands: 'pid' which displays the program's process id, 'quit' which exits the interpreter, with an option 'quit -r' which exits and restarts the program (this is useful when you're editing the program), and 'showline' which shows you how llines of input are received by the interpreter.

Gribouillis 1,391 Programming Explorer Team Colleague

Don't waste time writing and editor or an IDE! Don't reinvent the wheel! Go to the wikipedia list of algorithms, which points to virtually hundreds or thousands of clever algorithms, and implement a few of them in python.

Gribouillis 1,391 Programming Explorer Team Colleague

Assuming the name of your service is service , you could try to run this program

# runservice.py
import subprocess as sp
import sys
SERVICE_COMMAND = "service"

def run_service(command):
    with open("output.txt", "w") as output_file:
        process = sp.Popen(command, shell=True, stdout=sp.PIPE)
        for line in process.stdout:
            for f in (output_file, sys.stdout):
                f.write(line)
        process.wait()

if __name__ == "__main__":
   run_service(SERVICE_COMMAND)
Gribouillis 1,391 Programming Explorer Team Colleague

Sometimes, you just want to run a system command from python and capture its output. Currently, the standard way to do this is to call the subprocess.Popen with convenient arguments and use the communicate method. After writing such code a few times, I wrote this little Command class to ease the process. Its functionalities are limited, but in many cases, it does just what you need :)

Gribouillis 1,391 Programming Explorer Team Colleague

You can try a statement like this one

print(''.join(s[len("SimonSays: "):] for s in open("simonfile.txt") if s.startswith("SimonSays: ")))
Gribouillis 1,391 Programming Explorer Team Colleague

Well, x + y is transformed into x.__add__(y) if x has a __add__ method. If it doesn't, but y has a __radd__ method, it's transformed into y.__radd__(x) . So all you need to do is to define __add__ and possibly __radd__ methods in your classes.

Gribouillis 1,391 Programming Explorer Team Colleague

Use the 'key' argument of the 'sort' method like this

def score(item): # define a score function for your list items
    return item[1]

r =[(300, 4), (5, 6), (100, 2)]
r.sort(key = score) # sort by ascending score
print r
"""
my output -->
[(100, 2), (300, 4), (5, 6)]
"""
Gribouillis 1,391 Programming Explorer Team Colleague

The explanation is that the code in a class body is executed when a module is loaded, like code at module level, while code in a function or method is only executed when the function is called. So in your last example, the print statement is executed when you create a B instance, and not when class B is defined.
Of course, if a class definition is included in a function's body, its code is executed each time the function is called and not when the module is loaded.

HiHe commented: Thanks for the help +2
Gribouillis 1,391 Programming Explorer Team Colleague

This snippet provides 2 functions to create in memory a gziped archive of a sequence of files and directories, and to extract files from this archive. These functions are similar in effect to the linux commands tar czf and tar xzf but instead of writing an archive file on disk, a python string is used.

Gribouillis 1,391 Programming Explorer Team Colleague

No, you must write

for kind in ('ant', 'ant', 'bee', 'car'):
    etc

Also note that ('ant') is not a tuple with a single element but the string 'ant'. If you want a tuple with a single element, you must write ('ant',) .

Gribouillis 1,391 Programming Explorer Team Colleague

Note that the theoretical probabilities are 13/32, 13/32, 6/32 for ant, bee and car. This is very close from the program's output

Chance of getting ant: 40.160000% # instead of 40.625
Chance of getting bee: 40.470000% # instead of 40.625
Chance of getting car: 19.370000% # instead of 18.75
Gribouillis 1,391 Programming Explorer Team Colleague

The problem is that you are mistaking function parameters with return values. Here is how you could write your program

#!/usr/bin/env python
import sys
from random import randrange, shuffle, choice, sample

DOORS = ['ant','ant','ant','bee','bee','bee','car','car']

def reveal(what, availabledoors):
    """return an index chosen from 'availabledoors' which as value 'what' in DOORS.
   'what' can be 'ant', 'bee' or 'car'"""
    # the parameters what, availabledoors and the variable doors defined below
    # exist only in this function body. They are unreachable from outside code.
    doors = [i for i in availabledoors if DOORS[i] == what]
    return choice(doors)

# When we are here, the function 'reveal' was defined, but was never executed.

def pickdoor(doors):
    """Returns a number representing the original door chosen.
    The parameter 'doors' is a list containing indexes."""
    return choice(doors)

# again, here , pickdoor was defined but not yet called

def main(iterations=10000):
    shuffle(DOORS)
    obtainedant = 0
    obtainedbee = 0
    obtainedcar = 0

    for dummy in xrange(iterations):
        """calculates the chance of the final switched door containing an ant/bee/car"""
        doors = range(len(DOORS))
        picked = pickdoor(doors) # Here we call pickdoor and get a return value
        doors.remove(picked)
        for kind in ('ant', 'bee', 'car'):
            revealed = reveal(kind, doors) # This is where we call reveal
            doors.remove(revealed)
        picked = pickdoor(doors)
        switch = DOORS[picked]
        if switch == 'ant':
            obtainedant += 1
        elif switch == 'bee':
            obtainedbee += 1
        elif switch == 'car':
            obtainedcar += 1

    chanceofgettingant = (float(obtainedant) / iterations) * 100
    chanceofgettingbee = (float(obtainedbee) / iterations) * 100
    chanceofgettingcar = (float(obtainedcar) / iterations) * 100

    print …
pith commented: Thank you very much +0
Gribouillis 1,391 Programming Explorer Team Colleague

The value of the variable switch in the main loop is <function switch at 0x7f17ffb10c08> , so it's never equal to 'ant', 'bee' or 'car'. There are many errors in your code. You are using undefined variables in different places. First concentrate on the content of the main loop: what should it do ?

Gribouillis 1,391 Programming Explorer Team Colleague

Again, you can use a threading.Condition instance. Here is a program that works with python 2.6 and 3.1. I divided the time.sleep by a factor 10 :)

#!/usr/bin/env python

from threading import Thread, Condition
from contextlib import contextmanager
from random import randint
from time import sleep
import sys

@contextmanager
def acquired(condition):
    condition.acquire()
    try:
        yield condition
    finally:
        condition.release()

num = 0
bound = 1 << 31
numcond = Condition()
pexited = False

class P(Thread):
    def __init__(self):
        Thread.__init__(self)

    def run(self):
        global num, pexited
        n = randint(0, 99)
        while n >= 0:
            x = randint(0, 99)
            with acquired(numcond):
                num += x
                print ("P: +%d --> %d" % (x, num))
                if num >= bound:
                    numcond.notify()
            n -= 1
            sleep(0.030)
        with acquired(numcond):
            pexited = True
            print("P exiting")
            numcond.notify()

class C(Thread):
    def __init__(self):
        Thread.__init__(self)

    def run(self):
        global num, bound
        m = randint(0, 99)
        while m >= 0:
            with acquired(numcond):
                bound = randint(0, 99)
                numcond.wait()
                if num > bound:
                    num -= bound
                    print ("C: -%d --> %d" % (bound, num))
                if pexited:
                    print ("C aborting")
                    return
            m -= 1
            sleep(0.015)
        print ("C exiting")

def main():
    p = P()
    c = C()
    p.start()
    c.start()

if __name__ == "__main__":
    main()
Gribouillis 1,391 Programming Explorer Team Colleague

The road to writing "good programs" is paved with writing hundreds of not so good programs. The most important step is that your programs run :)

thekilon commented: How so true... Programs are not a demonstration of intelegence but automation of usability...even a beginner can make a highly useful programme from very basic concepts +0
Gribouillis 1,391 Programming Explorer Team Colleague

Consider this

>>> import datetime
>>> value = datetime.date.today()
>>> print(value)
2009-12-27
>>> value
datetime.date(2009, 12, 27)

When you call print(value) , the string printed is the string resulting from the call str(value) , or equivalently, the string returned by the method __str__() of the class. On the other hand, when the interpreter displays the value (without print), the string actually printed is the string resulting from the call repr(value) , or equivalently the string returned by the method __repr__ . The rule in python, is that when this is possible, the __repr__ method should return an expression which, if evaluated, would allow to reconstruct a new copy of the object. The datetime.date class follows this rule. Many classes don't because they contain too complex data or because their author neglected this feature.

mahela007 commented: Informative as usual +1
Gribouillis 1,391 Programming Explorer Team Colleague

You could use

def find_dupe(L):
    if len(L) == 0:
        return False
    elif L[0] in L[1:]:
        return True
    else:
        return find_dupe(L[1:])
Gribouillis 1,391 Programming Explorer Team Colleague

An additional method to plot the function using pylab (matplotlib)

def plot_pylab (self ):
    """func.plot_pylab() -> plots func using pylab.
    The function returns when the users closes the pylab window."""
    import pylab 
    pylab .plot (self ._x ,self ._y ,"b")
    pylab .show ()
Gribouillis 1,391 Programming Explorer Team Colleague

There are 2 small bugs in the previous post: remove the line location = sys.argv[-1] in function print_disk_info . Also in hardpath() , the call os.readlink(p) should be replaced by _readlink(p) where _readlink is the function

def _readlink(p):
    ln = os.readlink(p)
    if os.path.isabs(ln):
        return ln
    else:
        p, name = os.path.split(p)
        return os.path.join(p, ln)
Gribouillis 1,391 Programming Explorer Team Colleague

I think it maintains the traditional distinction between allocation and initialization. When you use structures in C for example, you must first use malloc to allocate a structure and then you initialize the structure's content. These are 2 different kinds of operations. For example you can decide to change your allocation strategy (implementing a garbage collector or a memory pool or whatever) an this doesn't interfere with the fields intitialization. In C++, you can override the new operator for such things. Other OO languages like objective C separate allocation and initialization. So, in python, __new__ is for allocation and __init__ for initialization.
You could say that allocation deals with memory and initialization deals with your program's logic.

Gribouillis 1,391 Programming Explorer Team Colleague

You can get a drive letter from a path like this

import os
drive = os.path.splitdrive(mypath)[0]
print(drive)

Now for mypath, you could use either the current working dir os.getcwd() or your module's file path __file__ . Then you can call the dos command using subprocess.Popen (I normally work on linux, so I don't know your dos command for free space).

lrh9 commented: Smart. +0
Gribouillis 1,391 Programming Explorer Team Colleague

I ended up figuring it out by breaking down sin into a function of series rather than using Taylor expansions. It was much simpler. This was my code for sin:

def sine(x):
    sum = 0.0
    n = 0.0
    term = 1.0
    while (term > .0000000001):
        #loops until the iterations grow so large that 'term' becomes negligibly small
        term = ((x ** (2 * n + 1.0))) / (factorial(2 * n + 1.0))
        if n % 2 == 0:
            sum += term
        else:
            sum -= term
        n += 1
    return sum

I also had made a function to calculate factorials, fyi.

You can speed this up:

def sine(x):
    term = float(x)
    result = term # avoid 'sum' which is the name of a builtin function
    u = - term * term # this is minus (x squared)
    n = 0 # use an integer for integer data
    while abs(term) > 1.0e-10:
        n += 2
        term *= u / (n * (n+1))
        result += term
    return result

The remaining question is to give an estimate for the error on the result. With this series, I think I can prove that with an n having the same order of magnitude as x (say n > 2|x|) for example, then the error on the result is smaller than 2 |term| for example. This is bad when x is large, but you can speed up things because sine is 2 pi -periodic. So a good idea would be to replace x by y = x …

Gribouillis 1,391 Programming Explorer Team Colleague

You probably still have the wrong indentation for the line
status = staticmethod(status).

Gribouillis 1,391 Programming Explorer Team Colleague

I see. Note that handling errors by the means of a return status is not very pythonic. It's a C like way of thinking. A more pythonic approach would be to define a

class LibraryError(Exception):
    pass

and in your methods, you could raise a LibraryError instead of returning False. Then client code could catch LibraryError. This gives client code the freedom to handle the error or not. If it doesn't handle it, the exception propagates.

vegaseat commented: nice idea +9
Gribouillis 1,391 Programming Explorer Team Colleague

The indentation is bad. The code should read

# Classy Critter
# Demonstrates class attributes and static methods

class Critter(object):
    """A virtual pet"""
    total = 0
    
    
    def status():
        print "\nThe total number of critters is", Critter.total
        
    status = staticmethod(status)
        
    def __init__(self, name):
        print "A critter has been born!"
            
        self.name = name
            
        Critter.total += 1
            
            
# main

print "Accessing the class attribute Critter.total:",
print Critter.total

print "\nCreating critters."

crit1 = Critter("critter 1")
crit2 = Critter("critter 2")
crit3 = Critter("critter 3")

Critter.status()

print "\nAccessing the class attribute through an object:",
print crit1.total

raw_input("\n\nPress the enter key to exit.")

About the error message: with your bad indentation, there was no __init__ method in class Critter. So the call crit1 = Critter("critter 1") passed the string argument "critter 1" to the object class, and finally to object.__new__ which creates a new object. Since python 3.0, this method does not accept any parameters but the calling class. Note that if you are using python >= 3, you must modify the print statements.

Gribouillis 1,391 Programming Explorer Team Colleague

Thank you all very much for your help in the decision. I have gone with design one and have implemented the features that I want so far. I am going to create a two front ends to implement the module. One command line and the other GUI. You can view the current module at coder profile and I will be posting the finished applications with downloads very soon to coder profile.

Nice. Just one criticism, I don't like your

except:
    return False

because you're just hiding errors, so that if something goes wrong, your program will never know. You should think about another way to handle the error (if you don't want to write error recovery code, you could send the error to a log file).