OOP help needed incorporating existing modules in class
I want to incorporate the datetime and other modules into my class. I am new to Python and would really appreciate some help doing this. class FooBar: def getDate(self): return ^^^ how do I do something like this? -- Koncept << "The snake that cannot shed its skin perishes. So do the spirits who are prevented from changing their opinions; they cease to be a spirit." -Nietzsche -- http://mail.python.org/mailman/listinfo/python-list
Threading for a newbie
Hi. I am fairly new to Python programming and am having some trouble wrapping my head around threading. This is a very basic example of what I am trying to do, and would greatly appreciate having this code hacked to pieces so that I can learn from you folks with experience. What I would like to learn from this example is how to use threads to call on other classes and append/modify results in a list outside of scope (basically keep track of variables throught the total threading process and return them somehow afterwards ). I was thinking about using some sort of global, but I am not sure what the best approach to this is. Thanks kindly for any assistance you may be able to offer. -- code -- import time, random, threading order = [] class Foo: def __init__(self, person): print "\nFoo() recieved %s\n" % person class Bar(threading.Thread, Foo): def __init__(self, name): threading.Thread.__init__(self, name = name) self.curName = name def run(self): global order sleepTime = random.randrange(1,6) print "Starting thread for %s in %d seconds" % \ (self.getName(), sleepTime) time.sleep(sleepTime) Foo.__init__(self,self.getName()) print "%s's thread has completed" % self.getName() order.append(self.getName()) def main(): for person in ['Bill','Jane','Steve','Sally','Kim']: thread = Bar(person) thread.start() # How do I print "order" after all the threads are complete? print "\nThreads were processed in the following order:" for i, person in enumerate(order): print "%d. %s" % (i+1,person) if __name__ == "__main__": main() -- Koncept << "The snake that cannot shed its skin perishes. So do the spirits who are prevented from changing their opinions; they cease to be a spirit." -Nietzsche -- http://mail.python.org/mailman/listinfo/python-list
Re: Threading for a newbie
In article <[EMAIL PROTECTED]>, Scott David Daniels <[EMAIL PROTECTED]> wrote: > You generally want to avoid mutation of anything in more than one > thread, and prefer to only share reading between threads. The only > time you can do more than that is when you _know_ the modification > is "atomic" -- there is no chance another thread can "see" a > half-modified value. > ... Scott. Thanks so much for your reply. Admittedly, It's going to take me a bit of time to grok what you posted, but I really do appreciate the help. I was sure that I was going about the the wrong way, and now I have a new path to follow. It's really nice to have people lend a hand when others get stumped. I hope to one day be able to contribute here in a similar manner! Best. -- Koncept << "The snake that cannot shed its skin perishes. So do the spirits who are prevented from changing their opinions; they cease to be a spirit." -Nietzsche -- http://mail.python.org/mailman/listinfo/python-list
REQ: Small Perl to Python conversion needed
Howdie Python folks! I am very new to Python ( 3rd day now ) and it has
already earned its place as my fav. language to work in. I hope to
continue, and I really would appreciate some good resources if anybody
would care to contribute.
My current head-scratcher concerns something I can do in Perl which I
would like to have duplicated for Python. I have noticed that it is not
possible to increment an unset value in Python, so I would like to know
how to duplicate the following bit of code using Python dictionaries.
#!/usr/bin/perl
# Parse comma delimited lines and create a final frequency hash
# Real example would read a file line by line
my %dict = {};
my @lines = ( "1,2,3,4,5", "2,3,4,5", "3,4,5", "4,5", "5" );
foreach(@lines) { map( $dict{ $_ }++, split( "," ) ); }
foreach( sort byKeys keys %dict ) {
print "Key: $_\tFrequency: ", "*" x $dict{ $_ }, "\n"
if $dict{ $_ } =~ /\d+/g;
}
sub byKeys { $dict{$b} <=> $dict{$a} }
__DATA__
Results:
Key: 5 Frequency: *
Key: 4 Frequency:
Key: 3 Frequency: ***
Key: 2 Frequency: **
Key: 1 Frequency: *
--
Koncept <<
"The snake that cannot shed its skin perishes. So do the spirits who are
prevented from changing their opinions; they cease to be a spirit." -Nietzsche
--
http://mail.python.org/mailman/listinfo/python-list
Re: REQ: Small Perl to Python conversion needed
In article <[EMAIL PROTECTED]>, Steven Bethard
<[EMAIL PROTECTED]> wrote:
> I don't speak Perl, but based on your output, I'd probably do something
> like:
>
> py> lines = ["1,2,3,4,5", "2,3,4,5", "3,4,5", "4,5", "5"]
> py> counts = {}
> py> for items in lines:
> ...for item in items.split(','):
> ...counts[item] = counts.get(item, 0) + 1
> ...
> py> for key in sorted(counts, key=counts.__getitem__, reverse=True):
> ... print 'Key: %s Frequency: %s' % (key, '*'*counts[key])
> ...
> Key: 5 Frequency: *
> Key: 4 Frequency:
> Key: 3 Frequency: ***
> Key: 2 Frequency: **
> Key: 1 Frequency: *
>
> I'm probably missing a few subtleties, but hopefully this will get you
> started.
>
> STeVe
Thanks Steven. This helped a lot. Exactly what I was looking for
--
Koncept <<
"The snake that cannot shed its skin perishes. So do the spirits who are
prevented from changing their opinions; they cease to be a spirit." -Nietzsche
--
http://mail.python.org/mailman/listinfo/python-list
