lrh9 95 Posting Whiz in Training

Databases might be applicable. Python has the sqlite3 module for simple databases. Do you have some examples of the data you want to work with?

lrh9 95 Posting Whiz in Training

This just doesn't sound like a good idea. If it were me, I'd specify interfaces and let other programmers work out how to fulfill those interfaces.

For instance, the sum function expects an iterable of summable objects. It doesn't care if a programmer stores his or her data in a csv file, json file, shelve, or whatever. It's up to the programmer to get the data he or she wants to use in the correct structure; he or she has to fulfill the interface.

It's better. You don't have to worry about what other programmers are doing. You don't have to write code to fulfill the interface for every situation. You can just specify the interface and let other programmers fulfill it. It's more robust. They can handle situations that you can't foresee. It keeps your code lean.

P.S. When I asked what your case was, I was talking about the project that you are writing this system for. I assume you are writing this to address something in another project? If you aren't, you are "solving" a "problem" that doesn't need to be solved.

lrh9 95 Posting Whiz in Training

Builtins are the way to do it, but in your function you reset s to zero each loop because it is inside the loop. It needs to be set to zero before the loop.

lrh9 95 Posting Whiz in Training

So you are pretty much trying to make functions and methods that behave differently depending on an argument's type or behavior? That's not a very good idea. It's very difficult to ensure that you can appropriately handle all types or behaviors in one function or method. Even if you can, you make it very difficult for yourself or other developers.

If your function or method returns a copy when the object is immutable, what does it return when the object is mutable? The same object? Now imagine you call this function and have to work with the return value. You pretty much have to do the same mutability checking you did in your function or method to ensure you handle the return value properly. This introduces more code, more overhead, and more points where your software can break.

I don't know any standard solutions. What is your specific case? Do you have any code?

I ran into this problem myself when I was working on some trivial neural network code. I wanted to use either a pair of sequences or a pair of mappings to represent a pair of neuron inputs and corresponding neuron weights. If the inputs was a sequence I would use the index of each input to look up the corresponding weight, or if the inputs was a mapping I would use the key of each input to look up the corresponding weight. At first I decided to use the collections abstract base classes to check if …

lrh9 95 Posting Whiz in Training

Why do you need to check an object's mutability? Sounds like you need to solve a different problem or use a different solution. Technically you could try to make whatever changes you wanted to make and catch any AttributeError exceptions, but it still seems funny. So...?

hughesadam_87 commented: . +3
lrh9 95 Posting Whiz in Training

At present the survey requires only a username to participate. Aren't you concerned that someone might vote using other usernames?

lrh9 95 Posting Whiz in Training

If you want to have a save system, you would be better off using an object serialization or data persistence module. You should read the documentation for the pickle and shelve modules.

This little example guess the number game shows a way to keep a record of the total number of correct guesses from one game to another. Try running it and play until you get a couple of guesses right. Then close the game. Then run it again and play a few more times. You should see that your total score is the sum of the correct guesses the first time you played and the correct guesses you made the second time you played.

import random
import shelve

if __name__ == '__main__':
    save = shelve.open('guess.sav')
    score = 0
    while True:
        choice = input('Guess a number from 1 to 10? (Y)es/(N)o: ')
        if choice.lower() in ('yes', 'y'):
            secret = random.randint(1, 10)
            while True:
                try:
                    guess = int(input('Guess the number from 1 to 10! Your guess: '))
                except:
                    continue
                else:
                    if guess == secret:
                        print('Yes!')
                        score += 1
                    else:
                        print('Sorry! The number was {0}.'.format(secret))
                    break
        elif choice.lower() in ('no', 'n'):
            break
    print('Your score this session is {0}!'.format(score))
    try:
        save['score'] += score
    except KeyError:
        save['score'] = score
    print('Your score all time is {0}!'.format(save['score']))
    save.close()
lrh9 95 Posting Whiz in Training

It depends on the structure of your file and what you are trying to do.

To update (read and write) a file you usually open it with mode r+.

From your other posts, I can see you are making a game. Are you trying to develop a save system or game data system? There may be better alternatives than the manipulations you are attempting. You should also read a good tutorial on file steams.

lrh9 95 Posting Whiz in Training

I flagged this as a bad post because the code is wrong. I should have tested it more thoroughly. It fails to send messages appropriately. It will respond to pings correctly, but if I write code to send a notice or a private message, it will take a matter of minutes to send any data, and it is only partially transmitted. Possible problems I have considered are blocking problems, asyncore timeout problems, and buffered sending problems. I haven't been able to resolve this issue on my own, so I am examining other irc bot implementations for a possible solution

lrh9 95 Posting Whiz in Training

You can write a module that can also act as a script by using the idiom:

if __name__ == '__main__':
    pass

Importing a module and then executing the main body of that module is indicative of a design problem, but can be accomplished by wrapping the main body in a main function.

#module.py

def main():
    pass

if __name__ == '__main__':
    main()

#script.py

import module

module.main()
lrh9 95 Posting Whiz in Training

The Python standard library features a csv module for CSV file reading and writing. Read the module documentation to learn how to use it.

I'd use a dictionary or collections.OrderedDict to associate each unique key with a list of values.

lrh9 95 Posting Whiz in Training

I had been wanting to write an IRC bot with asynchronous IO for a while. My bot responds to pings, but can be extended by defining functions and registering them to get called when the bot receives certain commands. My bot uses the RFC 1459 USER command and parameters, but can be made to comply with RFC 2812 by replacing my on_connect function with a compliant one. Here is what I wrote based on the asyncore and asynchat modules:

import asynchat
import asyncore
import collections
import socket

def parse(data):
    data = data.split(b' ')
    if data[0].startswith(b':'): #prefix present
        prefix = data[0][1:]
        data = data[1:]
    else:
        prefix = None
    command = data[0]
    data = b' '.join(data[1:])
    if data.startswith(b':'): #only trailing parameter
        parameters = [data[1:]]
    else:
        data = data.split(b' :')
        if len(data) > 1: #trailing parameter present
            trailing = b' :'.join(data[1:])
        else:
            trailing = None
        parameters = data[0].split(b' ') #regular parameters
        if trailing is not None:
            parameters.append(trailing) #add trailing parameter to regular parameters
    return prefix, command, parameters

class Connection(asynchat.async_chat):

    def __init__(self, nick, user, realname, host, port=6667):
        self.nick = nick
        self.user = user
        self.realname = realname
        self.address = (host, port)
        self.received = list()
        asynchat.async_chat.__init__(self)
        self.set_terminator(b'\n')
        self.handlers = collections.defaultdict(set)

    def collect_incoming_data(self, data):
        self.received.append(data)

    def found_terminator(self):
        data = b''.join(self.received).rstrip(b'\r')
        del self.received[:]
        prefix, command, parameters = parse(data)
        try:
            for handler in self.handlers[command]:
                handler(self, prefix, parameters)
        except Exception as exception:
            for handler in self.handlers[exception]:
                handler(self, exception)

    def message(self, string):
        string = ''.join((string, '\r\n'))
        self.push(string.encode())

    def establish(self):
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.connect(self.address)

    #We use a "plugin" oriented system so we …
lrh9 95 Posting Whiz in Training

I think that if someone takes the time to get an abstract overview of programming languages, he or she finds that programming languages are relatively simple and those featuring the same programming paradigms are quite similar.

It's a fascinating subject. When a person studies a specific programming language, he or she is primarily learning how to code with that language. That person does not necessarily learn transferable knowledge and skills. For instance, comments in Python are slightly different from comments in C or C++.

Look at the Python tutorial and the C++ tutorial side by side.

Both teach the user:
How to begin coding.
About variables and data types.
About control flow.
About functions.
About complex data structures.
About memory.
About input/output.
About classes.
About errors and exceptions.

They diverge on language specific features. Python features a section on modules, and C++ has information on pointers.

If a person can learn one, he or she can learn the other. However, studying one probably won't make a person a better programmer in the other. Knowing one may make the other easier to learn, because the person all ready knows the concepts. For instance, he or she will know what a function is. That person will only need to learn how to implement it in the language.

That is why I recommend that beginning programmers learn Python. In Python they do …

lrh9 95 Posting Whiz in Training

Task 1 is probably something that could be accomplished using system utilities if you were willing to research the options. I bet you could write a command line script or something to do it.

Task 2 can have many approaches. If the program in question supports the right command line options then it would be a similar thing. However, if you want to do complex tasks that require interaction with the program interface... not many options except a macro scripting language like AutoHotkey.

lrh9 95 Posting Whiz in Training

http://en.wikipedia.org/wiki/Pseudorandom_number_generator

There is even a link to an algorithm with a psuedocode implementation.

Personally, I think you should find something else to work on. I think reimplementing established algorithms will provide little educational value.

lrh9 95 Posting Whiz in Training

http://en.wikipedia.org/wiki/There's_more_than_one_way_to_do_it

lrh9 95 Posting Whiz in Training

I am doing some hobby coding regarding neural networks, and I was wondering if this is a correct and good use of abstract base classes or not.

import abc
import collections
import math
import weakref

class AbstractNeuron(metaclass=abc.ABCMeta):

    def __init__(self, weights, f=math.tanh, bias=1, biasWeight=0):
        self.weights = weights
        self.f = f
        self.bias = bias
        self.biasWeight = biasWeight

    def __call__(self, inputs):
        return self.activate(inputs)

    @abc.abstractmethod
    def activate(self, inputs):
        raise NotImplementedError

class MappingNeuron(AbstractNeuron):

    def __init__(self, weights=None, f=math.tanh, bias=1, biasWeight=0):
        if weights is None:
            weights = collections.defaultdict(int)
        super().__init__(weights, f, bias, biasWeight)

    def activate(self, inputs):
        return self.f(math.fsum(inputs[key] * self.weights[key] for key in inputs.keys()) + self.bias * self.biasWeight)

class WeakNeuron(MappingNeuron):

    def __init__(self, weights=None, f=math.tanh, bias=1, biasWeight=0):
        if weights is None:
            weights = weakref.WeakKeyDictionary()
        super().__init__(weights, f, bias, biasWeight)

class SequenceNeuron(AbstractNeuron):

    def __init__(self, weights, f=math.tanh, bias=1, biasWeight=0):
        super().__init__(weights, f, bias, biasWeight)

    def activate(self, inputs):
        return self.f(math.fsum(inputs[i] * weight for i, weight in enumerate(self.weights)) + self.bias * self.biasWeight)
lrh9 95 Posting Whiz in Training

I've never used it, but the subprocess module may be what you need. It seems to feature a function that will start a subprocess and wait for the subprocess exit code. If the exit code is non-zero then the function will raise an exception.

lrh9 95 Posting Whiz in Training

No. Put it after "words=aline.split()".
Also, move "count={}" to above "for aline in infile:".

lrh9 95 Posting Whiz in Training

Each word in words should be a key.

for word in words:
    if word not in count:
        count[word] = 1
    else:
        count[word] += 1
lrh9 95 Posting Whiz in Training

"count" needs to be a dictionary.
"line.split" is a function so it should be "line.split()".
"line.split" returns an iterable of words. You need to use a for loop to iterate over each word and increment its counter.

lrh9 95 Posting Whiz in Training

Not a bad start.

You are counting words, so your keys are going to be words and the values are going to be integers.

If a word isn't present in the dictionary you need to add it. Otherwise you need to increment the counter.

if word not in counter:
    counter[word] = 1
else:
    counter[word] += 1

You have enough of a grasp to do it. The only thing you need to do is learn about string methods so when you decide how you want to handle stuff like whitespace and punctuation you know what to do.

lrh9 95 Posting Whiz in Training

Sounds like an application for collections.Counter.

First do the Python tutorial to learn the Python language.

http://docs.python.org/py3k/tutorial/index.html

Then review the library reference to get an overview of Python's built in functionality.

http://docs.python.org/py3k/library/index.html

Finally, read the documentation for collections and try to write your program.

http://docs.python.org/py3k/library/collections.html

We can't help you if you don't do the work.

vegaseat commented: very nicely said +15
lrh9 95 Posting Whiz in Training

Long time no update. I've been studying finite state machines and it has me thinking that some of this might be better solved using finite state machines. The basic idea is that you have a collection of cards you want to find a hand for. You can iterate over the cards and update the state of the hand as you go. When you get to the end of the collection you have the rank and value.

The upside to this approach is that it is time complexity O(n) where n is the length of the collection of cards you are trying to rank.

One downside is that the size of the state transition table is large. I don't remember how to calculate how large, but in Hold'em you would be evaluating a collection of 7 cards chosen from a pool of 52 cards. Plus you could have intermediate states. Statistics could tell you if you have enough memory to make this approach feasible.

A second downside is that generating the state transition table may be difficult. It's impossible to do by hand, and you'd have to be pretty clever to generate a correct state transition table (including any intermediate states) programmatically.

Just thought I'd post this so someone reading this might solve the problem another way.

lrh9 95 Posting Whiz in Training

You mean you want to write your own implementation?

The source for most standard modules is included with the standard Python distribution. On Windows they can typically be found in "C:\Python32\Lib". The two digits can be different depending on your Python version.

The source for "insort_right":

def insort_right(a, x, lo=0, hi=None):
    """Insert item x in list a, and keep it sorted assuming a is sorted.

    If x is already in a, insert it to the right of the rightmost x.

    Optional args lo (default 0) and hi (default len(a)) bound the
    slice of a to be searched.
    """

    if lo < 0:
        raise ValueError('lo must be non-negative')
    if hi is None:
        hi = len(a)
    while lo < hi:
        mid = (lo+hi)//2
        if x < a[mid]: hi = mid
        else: lo = mid+1
    a.insert(lo, x)
lrh9 95 Posting Whiz in Training

Bisect is one of the better options.

There are performance implications with any kind of sorting. Bisection has logarithmic time complexity. I don't know what the time complexity is for sorting in the standard Python distribution.

If we are adding a lot of items then at some point simply extending the list and sorting it will be faster than insorting each item individually. (Probably.) It's much more elegant too.

import bisect
#making a new list
x = [5, 4, -3, 100, 88.3, -23.9]
#sort it
x.sort()
print(x)
#[-23.9, -3, 4, 5, 88.3, 100]
#adding an item to an existing list
#preserving sorting
bisect.insort(x, 65.5)
print(x)
#[-23.9, -3, 4, 5, 65.5, 88.3, 100]
#adding many items to an existing list
x.extend((75.4, -100, 999))
#sort it
x.sort()
print(x)
#[-100, -23.9, -3, 4, 5, 65.5, 75.4, 88.3, 100, 999]
lrh9 95 Posting Whiz in Training

"Production" is a word that refers to source or code used for practical application. It means you're writing a real program that someone, somewhere is going to use to do something.

"blist" is a third party module. People write third party modules for other people to use to make programs. Third party modules can offer things the standard Python distribution doesn't offer, such as additional functionality or better performance.

I suggested bisect thinking you wanted to write a program using just what is available in a standard Python distribution, but pyTony is right that if you have differing requirements such as a more performant implementation you should research which third party options are available and decide which - if any - to use.

TrustyTony commented: Nice signature links and nice explanation. +13
lrh9 95 Posting Whiz in Training

Is this for production or is it just an exercise?

If it is for production use bisect.insort to insert items into a list in sorted order.

lrh9 95 Posting Whiz in Training

Erm... Didn't notuce until just now, but the line "print(name, 'says hello.')" should be "print(self.name, 'says hello.')".

Sorry.

lrh9 95 Posting Whiz in Training

Let me try to explain object-oriented programming.

Object-oriented programming is programming with objects.

Objects are data structures consisting of fields of data (attributes) and functions that interact with that data (methods).

Objects are instances of classes (types). Classes represent the data structure. When you make a new class, you are "declaring" a class.

In Python, classes possess special methods for certain functions. Their names are usually between double underscores. "__init__" is a special method to initialize a new object and its data. The call to __init__ is usually implicit. Normal methods have an implied first argument (usually called "self") that reflect back to the object. Methods can access an instance's data using "self".

An example:

class Person:

    #Special method. Each person we make has a name.
    def __init__(self, name):
        self.name = name

    #Method accesses the instance's name to print it.
    def introduce(self):
        print(name, 'says hello.')

#__init__ called implicitly
#sets this person's (and only this person's) name
x = Person('John')
#a different person
y = Person('Jane')
#printing their names
x.introduce()
y.introduce()
lrh9 95 Posting Whiz in Training

Seems like the big barrier here is that the poster's primary language must not be English. I tried searching for "moy", but I didn't get any promising results. Perhaps it is an abbreviation or truncation?

lrh9 95 Posting Whiz in Training

As a designer and coder we should be as abstract as usefully possible. Abstract software is compact and reusable. You are treating classes as namespaces and making a specific class or function for every thing. You should treat classes as blueprints that abstractly specify the attributes and capabilities of a type of objects.

We wouldn't make a class for each Tom, Dick, and Harry. We would make a single Person class that specifies the attributes our people have. Our people have a name. So our Person class specifies that people have a name. We create people by creating instances of the Person class. We differentiate each instance with its name.

class Person:

    def __init__(self, name):
        self.name = name

names = ('Tom', 'Dick', 'Harry')
people = [Person(name) for name in names]

Your RPG has character classes with attributes. We can reduce your 25 lines of code for character classes to 16 (or less) and we can reduce the cost of adding a new class from adding 6 lines to adding 1 line. I'd say not bad.

The key to being a good solo software developer is design.

class Class:

    def __init__(self,
                 strength,
                 intelligence,
                 agility,
                 attack,
                 spellpower):
        self.strength = strength
        self.intelligence = intelligence
        self.agility = agility
        self.attack = attack
        self.spellpower = spellpower

ranger = Class(2, 3, 5, 4, 2)
warrior = Class(5, 2, 3, 4, 1)
priest = Class(3, 5, 2, 3, 3)
mage = Class(1, 3, 2, 2, 5)
print(ranger.strength)
print(warrior.strength)
print(priest.strength)
print(mage.strength)
lrh9 95 Posting Whiz in Training

It's not unusual for early implementations of code to be excessive or unrefined.

The important thing is to refactor as necessary.

http://en.wikipedia.org/wiki/Code_refactoring

Your early implementations and final versions will improve over time if you learn more about Python, plan what you want your code to do, decompose data and functions, study other implementations, and ask for feedback.

In terms of code elegance and readability, I don't see the number of lines as a major consideration. In fact, multiple lines are usually preferable to a bunched up one liner.

In terms of comments, there are a few guidelines. You want to avoid useless comments. You don't need to comment every line of code. You don't need to comment obvious code. "#Sets c to a + b." is a useless comment.

How can you tell which code is obvious and which isn't? That is tricky, but I'd ask myself this question: "If I forgot this code, could I look at it and figure out what it does quickly?"

If you think you couldn't, you should add some useful comments explaining what it does.

Useful comments most often give the big picture of what code does. E.g. a useful comment for "c = math.sqrt(a ** 2 + b ** 2)" would be "Calculates the hypotenuse."

Let me offer you my implementation.

##The MIT License
##
##Copyright (c) 2011 Larry Haskins
##
##Permission is hereby granted, free of charge, to any person …
lrh9 95 Posting Whiz in Training

papna, a user in the #python channel on freenode.irc.net, gave me a very clever solution.

papna told me to use a collections.Counter.

http://docs.python.org/py3k/library/collections.html?highlight=collections#collections.Counter

It will satisfy my requirements excepting hashability, but I know how to deal with that.

lrh9 95 Posting Whiz in Training

I'm working on a project where I would like to compare collections of words, but I have additional constraints I need to account for.

The collections are like a set in that order doesn't matter for the comparison.

However, in my problem equal elements are significant.

E.g. the collection of "foo", "bar", "baz" would not be the same as the collection of "baz", "foo", "bar", "baz".

However, the collection of "baz", "foo", "bar", "baz" would be the same as the collection of "bar", "baz", "foo", "baz".

Additionally, the collection itself must be hashable.

Thank you for reading.

lrh9 95 Posting Whiz in Training

I ended up checking out Panda3D's task and event system. Thanks for the replies.

lrh9 95 Posting Whiz in Training

creature.metabolism
creature.metabolsim

Gribouillis commented: spot on -3
vegaseat commented: upvoted for Griboullis (was downvote error) +15
lrh9 95 Posting Whiz in Training

It's also possible to bin items to a collection like a set or list.

This technique can bin items without key collisions.

import collections

data = ((5.639792, 1.36),
        (4.844813, 1.89),
        (4.809105, 2.33),
        (3.954150, 2.69),
        (2.924234, 3.42),
        (1.532669, 4.50),
        (0.000000, 5.63))

bucket = collections.defaultdict(list)
for each in data:
    bucket[int((each[1]))].append(each[0])
print(bucket)
lrh9 95 Posting Whiz in Training

The one I wrote. I'd have to take the time to discern the pattern in the traceback or limit the recursion limit. Otherwise there are just too many lines in the traceback.

lrh9 95 Posting Whiz in Training

I've downloaded it, and I'm attempting to understand the source.

I wrote my own naive event loop, but I'm getting a maximum recursion depth exceeded RuntimeError.

##The MIT License
##
##Copyright (c) 2011 Larry Haskins
##
##Permission is hereby granted, free of charge, to any person obtaining a copy
##of this software and associated documentation files (the "Software"), to deal
##in the Software without restriction, including without limitation the rights
##to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
##copies of the Software, and to permit persons to whom the Software is
##furnished to do so, subject to the following conditions:
##
##The above copyright notice and this permission notice shall be included in
##all copies or substantial portions of the Software.
##
##THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
##IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
##FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
##AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
##LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
##OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
##THE SOFTWARE.

import collections
import heapq
import random
import time

__all__ = ['InstantNotification',
           'Notification',
           'InstantMessage',
           'Message'
           'Dispatcher']

class InstantNotification:

    __slots__ = ('type', 'sender', 'receivers')

    def __init__(self,
                 type,
                 sender=None,
                 receivers=None):
        self.type = type
        self.sender = sender
        self.receivers = receivers

class Notification(InstantNotification):

    __slots__ = ('delay')

    def __lt__(self, other):
        return self.delay < other.delay …
lrh9 95 Posting Whiz in Training

Hm. It seems like bisect assumes ascending order. I hadn't thought of descending order.

You can either write your own function or you can keep your data in ascending order until you need descending order.

It's possible to get a reverse iterator over a sequence using the function reversed.
A slice with a negative step will have reversed elements. Example: L[::-1]

lrh9 95 Posting Whiz in Training

Begin a class repository and write one every so often. They can come in handy more often than you think. Pick a single real world or virtual object and write a class for it.

vegaseat commented: good idea +14
Gribouillis commented: Interesting idea +13
lrh9 95 Posting Whiz in Training

I do think you should use bisect.

It works.

If you can be sure that your value will fall between two indices, you can specify a lower bound and an upper bound to restrict the search. (Thus reducing search times.)

The implementation isn't much different from your own.

def bisect_right(a, x, lo=0, hi=None):
    """Return the index where to insert item x in list a, assuming a is sorted.

    The return value i is such that all e in a[:i] have e <= x, and all e in
    a[i:] have e > x.  So if x already appears in the list, a.insert(x) will
    insert just after the rightmost x already there.

    Optional args lo (default 0) and hi (default len(a)) bound the
    slice of a to be searched.
    """

    if lo < 0:
        raise ValueError('lo must be non-negative')
    if hi is None:
        hi = len(a)
    while lo < hi:
        mid = (lo+hi)//2
        if x < a[mid]: hi = mid
        else: lo = mid+1
    return lo
lrh9 95 Posting Whiz in Training

Create a virtual environment. You can make a game, a simulator, or a virtual reality.

One of the simpler virtual environments you can implement is Conway's Game of Life.

http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life

Most virtual environments have a few things in common.

The environment, obviously. A set of data describing a state and the rules on how it can change.

Agents. These interact with the environment, gathering data, changing it. Agents can poll the environment for data (they request data), or they can be notified with events. (Usually functions that are called when other functions are called.)

A mechanism to update the environment. For instance, in a tick based system time is divided into increments called ticks. Each tick, every agent is given a chance to act and every function to change the environment is called. At the end, conflicting changes can be resolved. The process repeats.

lrh9 95 Posting Whiz in Training

If you can, check out the fractions module.

http://docs.python.org/library/fractions.html#module-fractions

The source is included with Python. If you can locate it, you can see how the author(s) did it.

lrh9 95 Posting Whiz in Training

I can't tell if this is an exercise.

Even if it is, the bisect source can still serve as an inspiration and guideline.

It's strange how people don't realize that a lot of things are in the standard module library and the source is available for most of it.

lrh9 95 Posting Whiz in Training

Use an online resource that makes it easy to retrieve data. Many sites have an API (Application Programming Interface) that allow programs to retrieve data with a few function calls.

For instance, Yahoo lets you retrieve a csv file of stock quotes with a url.
You can use the urllib.retrieve module to retrieve the data, and the csv module to process it.

#Written for and tested on Python 3.1.
import csv
import urllib.request

def yahoo_quotes_csv_url(tickers):
    return ''.join(('http://finance.yahoo.com/d/quotes.csv?s=',
                    '+'.join(tickers),
                    '&f=snl1'))

if __name__ == '__main__':
    tickers = ('GOOG', 'AAPL')
    url = yahoo_quotes_csv_url(tickers)
    print(url)
    urllib.request.urlretrieve(url, 'quotes.csv')
    reader = csv.reader(open('quotes.csv'))
    for row in reader:
        #row[0]=ticker, row[1]=company, float(row[2])=value
        print(row)

Different sites will provide different interfaces and have more data, but if you remember to read the documentation about retrieving data and if you remember to use the right modules you can use it.

Thisisnotanid commented: Very helpful! +2
lrh9 95 Posting Whiz in Training

Isn't this what the bisect module is for?

lrh9 95 Posting Whiz in Training

Have you tried making a client locally and getting it to connect? Maybe the reason select fails to return any sockets is because no one is trying to connect. (Therefore there are no sockets to call accept on.)

Maybe you should try making a client instead of a server. You can make an IRC client along similar lines.

P.S. Unrelated to your problem, but "(sread, swrite, sexc) = ..." works fine without parentheses.

lrh9 95 Posting Whiz in Training

I like to code a simple switch using True or False. When you need to flip the switch then changing state is as easy as:

#Switch in the 'off' state
state = False
#Flip the switch
state = not state
#Switch is now in the 'on' state
#Flip again
state = not state
#Switch is back 'off'
vegaseat commented: agree +13