[Tutor] VNC library for python
Does this exist? I see projects talking about writing viewers in python, and talking about their flaws or potential, but I've not seen a single library.Thanks. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] python decorators
Could somebody explain or point to a good place to learn what are python decorators, and when should they be used? Thanks. __ LLama Gratis a cualquier PC del Mundo. Llamadas a fijos y móviles desde 1 céntimo por minuto. http://es.voice.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] move forward in arbitrary direction
The "something" you're stuck on is the angle the triangle has rotated, measured in radians. If the concept of radians is not familiar, then here's a quick review: There are 2*pi radians in a complete circle (about 6.28) There are 360 degrees in a complete circle 1 radian is approximately 57 degrees Going by radians, 0.0*pi is pointing to the right, 0.5*pi is pointing down, 1.0*pi is pointing to the left, and 1.5*pi is pointing straight up. Keep in mind that python has a math function radians(x), which converts "x" degrees to radians. If you use that, you can use degrees instead and convert on the fly. (i.e. something to the effect of: math.cos(math.radians(degrees)) would give you the change in vector[0]). If you would rather use radians, keep in mind that the python math module also has the pi constant, which is pretty self explanatory... Hope that helps you... Jonathon Michael Shulman wrote: > Hello, I have what should be a basic math question, but I keep messing > it up. > > How do I code an equation so that when I have an object facing an > arbitrary vector, pressing a key will make it move forwards in that > direction? > (I understand all the key-based setup, it's just the equation for moving > in the arbitrary vector direction that's got me stuck) > > right now i can make something move up and down, or left and right, but > if I want to rotate a triangle, then move it so that the 'tip' always > points in the direction it's going to move, I get stuck. > > the thing I have which doesn't work is something like > _ > vector = [0 0 0] > > def specialKey(key,x,y): > if key == 'up': >vector[0] = vector[0] + 1 > ___ > which I assume should be more like > > vector = [0 0 0] > > def specialKey(key,x,y): > if key == 'up': >vector[0] = vector[0] * math.cos(something???)+ 1 >vector[2] = vector[2] * math.sin(something??)+1 > -- > Any help would be greatly appreciated! > > ty, Mike > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] I am terribly confused about "generators" and "iterators".. Help me
My understanding is that an iterator is basically a facade` pattern. If you aren't familiar with patterns, a facade` pattern basically makes something really easy to use or convenient. Yes, you can "do it by hand", and many times that is indeed the preferred method, but sometimes it's easier to use an iterator...particularly if it isn't a special case. Nothing in the manual says you HAVE to use iterators, but they *can* make life easier. Jonathon Asrarahmed Kadri wrote: > > > Hi Folks, > > What are generators and iterators...??And why are they are needed..?? > > I can do my stuff with a 'for' or a 'while' loop.. so why do I need an > ITERATOR..? > > And what is a generator ..? I did try to read about these two things on > the web, but still I AM CONFUSED. > > To be honest, I am used to the nice and lucid style of the wonderful > people on this forum.. :)- > > Regards, > Asrarahmed Kadri > > > -- > To HIM you shall return. > > > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] move forward in arbitrary direction
> right now i can make something move up and down, or left and right, but > if I want to rotate a triangle, then move it so that the 'tip' always > points in the direction it's going to move, I get stuck. http://freespace.virgin.net/hugo.elias/routines/rotate.htm Alan ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] I am terribly confused about "generators" and "iterators".. Help me
> My understanding is that an iterator is basically a facade` pattern. > If you aren't familiar with patterns, a facade` pattern basically makes > something really easy to use or convenient. Yes, you can "do it by > hand", and many times that is indeed the preferred method, but sometimes > it's easier to use an iterator...particularly if it isn't a special > case. Nothing in the manual says you HAVE to use iterators, but they > *can* make life easier. Concretely, we can write a program that will find all the even-length strings in some sequence, and give it back to us as a list: def filterEvenStrings(sequence): results = [] for x in sequence: if len(x) % 2 == 0: results.append(x) return results Now, we can use this function on a list of strings, as one would expect: ### >>> filterEvenStrings('hello world this is a test'.split()) ['this', 'is', 'test'] ### But we can also use this on a file: ## >>> evenWords = filterEvenStrings( ... [x.strip() ... for x in open('/usr/share/dict/words').readlines()]) >>> evenWords[:10] ['aa', 'Aani', 'aardvark', 'aardwolf', 'Aaronite', 'Aaru', 'Ab', 'Ababua', 'abac', 'abacay'] ## Note the nice thing here: we've been able to reuse filterEvenStrings on two entirely different things! We can use the same function on different things because those two things support a common interface: "iteration" support. Most of Python's interesting data structures have built-in iterators. But there are plenty of things that don't by default, but for which we might want to add such iteration support. 'for' loops expect things that iterate: if we try to apply them on things that don't, we'll get an error. For example: ## >>> class Candy: ... def __init__(self, name): ... self.name = name ... >>> class CandyBag: ... def __init__(self): ... self.contents = [] ... def addCandy(self, aCandy): ... if isinstance(aCandy, Candy): ... self.contents.append(aCandy) ... ### Ok, so we can make a CandyBag, and we might like to start adding things to it: ### >>> bag = CandyBag() >>> bag.addCandy(42) >>> bag.addCandy(Candy("snickers")) >>> bag.addCandy(Candy("salt water taffy")) >>> bag.addCandy("spam") ### We'd expect 42 and spam to be ignored, because they're not Candy. Anyway, so let's go over the bag with a loop: ### >>> for c in bag: ... print c ... Traceback (most recent call last): File "", line 1, in ? TypeError: iteration over non-sequence ### This bag is not a sequence. It may CONTAIN a sequence, but it itself isn't one. Now, we can either pull out the bag.contents if we're naughty, but we want to be good: let's have CandyBag support iteration. class CandyBag: def __init__(self): """Creates an empty CandyBag.""" self.contents = [] def addCandy(self, aCandy): """Adds aCandy if it's a Candy. Otherwise, ignore it.""" if isinstance(aCandy, Candy): self.contents.append(aCandy) def __iter__(self): """Returns an iterator to all the candy in us.""" return iter(self.contents) We've added a method __iter__() that produces an iterator when requested. In this case, we'll reuse the built-in iterator for lists. The iter() function takes anything that supports iteration, and gives us a iterator: ## >>> myiterator = iter([3, 1, 4]) >>> myiterator >>> myiterator.next() 3 >>> myiterator.next() 1 >>> myiterator.next() 4 >>> myiterator.next() Traceback (most recent call last): File "", line 1, in ? StopIteration ## And now that CandyBags support iteration, we can satisfy our sweet tooth and loop over a CandyBag using our for loop: ### >>> bag = CandyBag() >>> bag.addCandy(42) >>> bag.addCandy(Candy("snickers")) >>> bag.addCandy(Candy("salt water taffy")) >>> bag.addCandy("spam") >>> for c in bag: ... print c ... <__main__.Candy instance at 0x22893f0> <__main__.Candy instance at 0x22896c0> ### There's our two pieces of candy. We didn't add str() support to our Candy, so that's why they're printing in that peculiar form. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] move forward in arbitrary direction
Going by radians, 0.0*pi is pointing to the right, 0.5*pi is pointingdown, 1.0*pi is pointing to the left, and 1.5*pi is pointing straight up.uh, wouldn't pi/2 be pointing up?Don't you set 0 radians to be the positive-x axis and then go counter-clockwise?Or does it not matter? It seems like it would.Thanks,-Luke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Adding voting to several object in python and db
I m having lots of fun with python it is like all of sudden I munshackled from the days of java Man it is a lot of fun. It is supersimple and easy... Ok now to my question, I m trying to add a rankingfeature to various objects in the system, the users, their posts,their media etc, suffice to say I have to rank a lot of objects. I canthink of implementing this rudimentarily, add python code and copy todo everything.Each object is a postgres db.. egTable user{username, metadata,RANKING int4,}and so on...I cant but woefully look at Ruby_oN_rails guys adding ranking with justone line @act_as_votable and they are set.. no more bugs to fix, Do youguys have any ideas on how to do this, somehting similar to@checkaccess in auth also seems reasonable..thanks a lot for your time and patiencekeep rockin in the web/python world AnilAnil Access over 1 million songs - Yahoo! Music Unlimited Try it today. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Adding voting to several object in python and db
anil maran wrote: > I m having lots of fun with python it is like all of sudden I m > unshackled from the days of java Man it is a lot of fun. It is super > simple and easy... Ok now to my question, I m trying to add a ranking > feature to various objects in the system, the users, their posts, > their media etc, suffice to say I have to rank a lot of objects. I can > think of implementing this rudimentarily, add python code and copy to > do everything. > > Each object is a postgres db.. eg > Table user > { > username, metadata, > RANKING int4, > > } > > and so on... > I cant but woefully look at Ruby_oN_rails guys adding ranking with just > one line @act_as_votable and they are set.. no more bugs to fix, Do you > guys have any ideas on how to do this, somehting similar to > > @checkaccess in auth also seems reasonable.. I for one could not begin to address your question as I do not know what you mean by ranking, nor am I familiar with Ruby or @act_as_votable. Nor do I understand "implementing this rudimentarily, add python code and copy to do everything" Perhaps others have the background to help you. If you don't get any useful replies, please explain the desired behavior. > > thanks a lot for your time and patience > > keep rockin in the web/python world > > > Anil -- Bob Gailer 510-978-4454 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Matching on multiple log lines
I would appreciate any recommendations or assistance on how to read and parse this log. The log is constantly growing and has the potential of being large. For this reason, we are reading in line by line. How do I match on multiple lines, while reading a log line by line. Hopefully the logic example and sample log will make more sense. Thank you, Logic: -- 1) Search for this string 'Writing Message to' 2) Search ahead for this string 'TRANSPORT_STREAM_ID' 3) Followed by a blank line 4) If 1,2, and 3: increase a counter or something (counter +=1) 4) Repeat process Example log 2006/10/24 20:46:05.853 Writing Message to 192.168.1.1:3211 Protocol: 2 Message Type: PROVISION Parameters: Parameter type: CHANNEL_IDParameter value: 0001 Parameter type: SCG_IDParameter value: 0001 Parameter type: TRANSPORT_STREAM_IDParameter value: 0160 Parameter type: PROGRAM_NUMBERParameter value: 0001 Parameter type: GROUPParameter value: 0009 0002 2006/10/24 20:46:05.957 Receiving message from 192.168.1.1:3211 2006/10/24 20:47:05.011 Writing Message to 192.168.1.2:3211 Protocol: 2 Message Type: PROVISION Parameters: Parameter type: CHANNEL_IDParameter value: 0001 Parameter type: SCG_IDParameter value: 0001 Parameter type: TRANSPORT_STREAM_IDParameter value: 0160 2006/10/24 20:47:05.057 Blah Blah..more logging ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Adding voting to several object in python and db
> Ok now to my question, I m trying to add a ranking feature to various > objects in the system, the users, their posts, their media etc, suffice > to say I have to rank a lot of objects. Hi Anil, It sounds like you want to add some sort of feature to a heterogeneous set of classes. Traditionally, you could use a design pattern like "decorator" do to do this. Another way to add some feature to several different classes is to use multiple inheritance. Python's classes give us another tool in our toolbox called "mixins". The idea is that we can systematically build new classes with enhanced behavior in a cookie-cutter way. Here's a concrete example: _counter = 0 def makeAutoincrementingClass(superClass): class NewClass(superClass): def __init__(self, *args, **kwargs): global _counter superClass.__init__(self, *args, **kwargs) self.__id = _counter _counter = _counter + 1 def id(self): return self.__id return NewClass This is something that should look really weird at first! makeAutoincrementingClass() is a function that takes in a superClass, and returns a new class. Let's try using it: ## >>> class Candy: ... def __init__(self, name): ...self.name = name ... def __str__(self): ...return "%s" % self.name ... >>> CountedCandy = makeAutoincrementingClass(Candy) >>> c = CountedCandy("reeses") >>> c.id() 0 >>> print c reeses >>> g = CountedCandy("gumdrops") >>> g.id() 1 So here we've added some auto-incrementing id assignment to Candy. That's sorta cool. But we can add auto-incrementing behavior to any preexisting class: ## >>> from StringIO import StringIO >>> StringIO >>> CountedStringIO = makeAutoincrementingClass(StringIO) >>> s1 = CountedStringIO("hello world, this is a test") >>> s2 = CountedStringIO("happy halloween") >>> s1.id() 2 >>> s2.id() 3 >>> s2.read(5) 'happy' ## Now we've added this id-allocating behavior to a class from the Standard Library. Cool stuff. > I cant but woefully look at Ruby_oN_rails guys adding ranking with just > one line @act_as_votable and they are set.. no more bugs to fix, Do you > guys have any ideas on how to do this, somehting similar to My recommendatio is to really get functions down cold. They are much more powerful than you might guess. What the Ruby folks are doing when they define @act_as_votable is pretty much a mixin technique, just in different syntax. (And Python has this syntax in the form of "decorators".) The idea that drives this is that one can write functions from classes to classes, and it's a neat idea. There was a very old article about this in: http://www.linuxjournal.com/node/4540/print where they implement mixins with some __bases__ hackery. Personally, I don't like their approach of mutating the input class. So the example I wrote above does not mess with the existing class: makeAutoincrementingClass derives a new class that doesn't interfere. Best of wishes! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Matching on multiple log lines
so it's guaranteed that 'Writing Message to' will always be followed by 'TRANSPORT_STREAM_ID' before the next occurrence of 'Writing Message to' and all text between can be ignored, and we increment the counter if and only if there is a newline immediately after 'TRANSPORT_STREAM_ID' yes? I.e. in your example, log, while there are two 'Writing Message to' blah blah 'TRANSPORT_STREAM_ID' sequences, only one has the required newline and proper counting increments the counter just once, yes? I'd keep a line counter to record the total number of lines read. I'd use a boolean flag to record the occurrence of 'Writing Message to' If flag is TRUE and if 'TRANSPORT_STREAM_ID' then test for newline and if TRUE then add the line number to a list of line numbers for the newline then reset flag to FALSE and resume testing down the file. when all done, save the line number of the very last line in the logfile to a stash file. upon next read-through, read the line number of the last line from the stash file and blindly read and discard that many lines before resuming the process. maybe a good idea to save not only the last line number but also the line itself into the stash file and after dropping to the line of the last number , compare text--call police if no match. On Oct 28, 2006, at 5:55 PM, Tom Tucker wrote: > I would appreciate any recommendations or assistance on how to read > and parse this log. The log is constantly growing and has the > potential of being large. For this reason, we are reading in line by > line. How do I match on multiple lines, while reading a log line by > line. Hopefully the logic example and sample log will make more > sense. > Thank you, > > > Logic: > -- > 1) Search for this string 'Writing Message to' > 2) Search ahead for this string 'TRANSPORT_STREAM_ID' > 3) Followed by a blank line > 4) If 1,2, and 3: increase a counter or something (counter +=1) > 4) Repeat process > > > Example log > > 2006/10/24 20:46:05.853 Writing Message to 192.168.1.1:3211 > Protocol: 2 > Message Type: PROVISION > Parameters: > Parameter type: CHANNEL_IDParameter value: 0001 > Parameter type: SCG_IDParameter value: 0001 > Parameter type: TRANSPORT_STREAM_IDParameter value: 0160 > Parameter type: PROGRAM_NUMBERParameter value: 0001 > Parameter type: GROUPParameter value: 0009 0002 > 2006/10/24 20:46:05.957 Receiving message from 192.168.1.1:3211 > 2006/10/24 20:47:05.011 Writing Message to 192.168.1.2:3211 > Protocol: 2 > Message Type: PROVISION > Parameters: > Parameter type: CHANNEL_IDParameter value: 0001 > Parameter type: SCG_IDParameter value: 0001 > Parameter type: TRANSPORT_STREAM_IDParameter value: 0160 > > 2006/10/24 20:47:05.057 Blah Blah..more logging > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] move forward in arbitrary direction
Hrmmm...I do believe you are right, Luke. My bad...it's been a bit since I actually used this stuff (about 12 years ago in college...heh). And yes, it would matter, unless you swapped the key functionality around...but let's keep it simple, yes? Good thing I'm not programming this project...haha. Doh? Jonathon Luke Paireepinart wrote: > > > Going by radians, 0.0*pi is pointing to the right, 0.5*pi is pointing > down, 1.0*pi is pointing to the left, and 1.5*pi is pointing > straight up. > > > uh, wouldn't pi/2 be pointing up? > Don't you set 0 radians to be the positive-x axis and then go > counter-clockwise? > Or does it not matter? > It seems like it would. > Thanks, > -Luke > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor