Re: [Tutor] What exactly is "state"?
On 03/02/2015 01:42 PM, Sydney Shall wrote: Thank you very much, Joel, Danny, Alan and Dave. Your explanations are all very clear and very enlightening. I shall have to change several of my unittests now. In good time. I am particularly pleased with the examples; they clarify matters considerably for me. Out of subject, I wonder from this exchange whether teaching should not always involve at least several teachers. Your replies are very complimentary! Your reply was very complimentary, our replies were complementary. Notice the e instead of the i. Yes, multiple teachers is frequently useful. When I was in college, many classes had both a professor (lecturer) and assistant professors (or teaching assistants, or ...). One lecture in a big hall, one relatively small class where you could more easily interact. I found the two different viewpoints useful, though sometimes the leaders of the small class were almost afraid to challenge the full prof. I also advocated a dual-manager role in one company. Idea didn't formally go anywhere, but I did manage to escape from management -- went from 50 people to 1, and a promotion at the same time. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What exactly is "state"?
On 02/03/2015 18:03, Dave Angel wrote: On 03/02/2015 11:25 AM, Sydney Shall wrote: I am a beginner and I am now at the strage of learning to write unittests. I have followed the current discussion entitled "How to test a class in pyhton", and I am not clear precisely what is meant by state. In its common meaning I can see some relevance. But is there a technical aspect to the notion. I see it mentioned often and feel rather uncomfortable that I know so little about it. I have deliberately started a new thread. Thanks. When I started composing this, there were no other replies. Sorry for any duplication caused by that. Starting with a dictionary definition: http://www.merriam-webster.com/dictionary/state "the overall physical condition of something : the ability of something to be used, enjoyed, etc." Others: "The particular condition that someone or something is in at a specific time" "In computer science and automata theory, the state of a digital logic circuit or computer program is a technical term for all the stored information, at a given instant in time, to which the circuit or program has access." That last comes the closest to what I'd like to explain. For a given fragment of executing code, the state includes all local variables, all parameters, all closures, all visible globals (ie the ones that *could* be visible to the code. It also includes indirectly the values of all environment variables, lots of system information like the current directory, the time, the network IP address. It also includes the current phase of the moon, the astrological sign of the current president of France, and the number of specs of sand on the eastern shore of a certain Martian lake. Thank you very much, Joel, Danny, Alan and Dave. Your explanations are all very clear and very enlightening. I shall have to change several of my unittests now. In good time. I am particularly pleased with the examples; they clarify matters considerably for me. Out of subject, I wonder from this exchange whether teaching should not always involve at least several teachers. Your replies are very complimentary! -- Sydney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What exactly is "state"?
On 03/02/2015 11:25 AM, Sydney Shall wrote: I am a beginner and I am now at the strage of learning to write unittests. I have followed the current discussion entitled "How to test a class in pyhton", and I am not clear precisely what is meant by state. In its common meaning I can see some relevance. But is there a technical aspect to the notion. I see it mentioned often and feel rather uncomfortable that I know so little about it. I have deliberately started a new thread. Thanks. When I started composing this, there were no other replies. Sorry for any duplication caused by that. Starting with a dictionary definition: http://www.merriam-webster.com/dictionary/state "the overall physical condition of something : the ability of something to be used, enjoyed, etc." Others: "The particular condition that someone or something is in at a specific time" "In computer science and automata theory, the state of a digital logic circuit or computer program is a technical term for all the stored information, at a given instant in time, to which the circuit or program has access." That last comes the closest to what I'd like to explain. For a given fragment of executing code, the state includes all local variables, all parameters, all closures, all visible globals (ie the ones that *could* be visible to the code. It also includes indirectly the values of all environment variables, lots of system information like the current directory, the time, the network IP address. It also includes the current phase of the moon, the astrological sign of the current president of France, and the number of specs of sand on the eastern shore of a certain Martian lake. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What exactly is "state"?
On 02/03/15 16:25, Sydney Shall wrote: I am not clear precisely what is meant by state. In its common meaning I can see some relevance. But is there a technical aspect to the notion. Yes, although it is related to the normal vernacular meaning. There is a whole area of math/engineering dedicated to the study of state and in particular finite state machines and automata. Finite State Machine on Wikipedia will tell you more about that than you want to know :-) Trying to simplify it to the level of ordinary mortals, state simply means a specific set of data attribute values, which affect the outcome of the operations of a system. A very simple state might be a button which can be ON or OFF. That's not very interesting. But suppose we have a more complex circuit model with an upstairs and downstairs switch controlling a stairway light. Now there are two boolean variables and whether the light is on or off depends on the state of both switches Top Bottom Light UP UP OFF UP DOWNON DOWNUP ON DOWNDOWNOFF So the state of the light depends on the state of the two switches. You can't tell what effect moving the Top switch to the UP position will have on the light unless you already know either the current state of the light or the state of the Bottom switch. We can very easily model that as a class: class StairLight: def __init__(self, sw1=True,sw2=True): self.sw1 = sw1 self.sw2 = sw2 self._setLight() def _setLight(self): self.light = self.sw1 != self.sw2 def __str__(self): if self.light: return 'light is ON' else: return 'light is OFF' def toggle1(self): self.sw1 = not self.sw1 self._setLight() def toggle2(self): self.sw2 = not self.sw2 self._setLight() SL = StairLight() print(SL) # -> OFF SL.toggle1() print(SL) # -> ON SL.toggle1() print(SL) # -> OFF SL.toggle2() print(SL) # -> ON SL.toggle2() print(SL) # -> OFF Of course the state values don't need to be boolean, they can be any type. And they can have many valid values. Consider the state of a order with several order lines. It can be initiated, costed, approved, packaged, shipped, delivered, paid, completed, archived. That's the happy path, but then consider the extra states needed for the 'unhappy paths' - awaiting stock, cancelled, rejected, partial delivery, returned, faulty, and so on. To simplify things we usually create an explicit state attribute which is updated after each operation on the order to reflect the current status. There are many ways to address these kinds of problems, and mathematical models that can be applied to simplify them (in many ways similar to the techniques for simplifying boolean algebra). In practice they boil down to either a state model where each state is modelled as a class or a state table where a large table contains a row for each possible state and a column for each possible event. The cells contain an action and the action returns the next state. The huge advantage of using state machines like this is that they are entirely predictable in behaviour and can be modelled and tested exhaustively. This means they are ideal for safety critical systems and the like. Most air/rail/traffic control systems, telecomms network switches, and equipment control software (cameras, iPods, phones etc) are built using state machines. Specialist CASE tools are available (for many !) that will generate the code from a state model (often graphical). They also allow the user to run simulations etc to see the effects, perform performance tests etc all without writing a line of actual code. It's one of the main reasons that aircraft don't often crash due to software faults even though your web browser probably falls over almost every day! One formal language for developing complex designs based on this approach is SDL (not the graphics toolset). There is an opensource CASE tool for SDL written in Python and Qt, called openGeode: http://taste.tuxfamily.org/wiki/index.php?title=Technical_topic:_OpenGEODE,_an_SDL_editor_for_TASTE I spent many years of my life building systems using SDL, C and Motorola 68000 assembler Oh, and finally. Another big win for state machines is that they can oftewn be translated from software into hardware for the ultimate in performance tweaking! :-) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What exactly is "state"?
On Mon, Mar 2, 2015 at 8:25 AM, Sydney Shall wrote: > I am a beginner and I am now at the strage of learning to write unittests. > I have followed the current discussion entitled "How to test a class in > pyhton", and I am not clear precisely what is meant by state. In its common > meaning I can see some relevance. But is there a technical aspect to the > notion. I see it mentioned often and feel rather uncomfortable that I know > so little about it. Hi Sydney, Let's take a concrete example that might help things get started. Let's say that we're writing a playlist generator for a music broadcast system. ## class Playlist(object): # ...? ## What do we want the Playlist to know? When we create such a Playlist, what does the playlist have in hand? Let's say that it has a complete list of songs when it's being built. ## class Playlist(object): def __init__(self, songs): self.songs = songs ## That is, 'songs' is a piece of a playlist's state, something that it owns. So, we can imagine creating a playlist with songs like: ## class Playlist(object): def __init__(self, songs): self.songs = songs ## Here's an example playlist. playlist = Playlist(["Happy", "Beethoven's 9th Symphony", "Power"]) ## Ok, a bit eclectic. But let's go with it. Let's say that we want to add a simple behavior to this class, so that it can do something useful. Let's be able to ask the playlist to give us the *next* song. What would we like next to do here? Let's say that if we call it once on the playlist example above, we'd like to first see "Happy". If we call next() again, we'd like to see "Beethoven", and if we call next() again, we'd like to see "Power". And for the sake of it, once we're out of songs, let's just see "*silence*". We can express the previous paragraph as a unit test! ## import unittest class PlaylistTest(unittest.TestCase): def testSimple(self): playlist = Playlist(["Happy", "Beethoven", "Power"]) self.assertEqual(playlist.next(), "Happy") self.assertEqual(playlist.next(), "Beethoven") self.assertEqual(playlist.next(), "Power") self.assertEqual(playlist.next(), "*silence*") ## In a unit test with a function, all we need to do is call the function on arguments. But in a unit test with a class, we need to set up the background scene. Here, we need to first create a playlist, and *then* test it out. Also, notice the weirdness of calling playlist.next() multple times, and expecting *different* results. If you have mathematical training, this is particularly weird! It's important to notice this because this means that playlist's next() method can't behave as a mathematical function that only pays attention to its immediate parameters. It will also need to take into account something else, something part of the state of the playlist. We can actually run the tests at this point. Of course, they'll fail, but that's ok. Here is a complete program that we can run: ### import unittest class PlaylistTest(unittest.TestCase): def testSimple(self): playlist = Playlist(["Happy", "Beethoven", "Power"]) self.assertEqual(playlist.next(), "Happy") self.assertEqual(playlist.next(), "Beethoven") self.assertEqual(playlist.next(), "Power") self.assertEqual(playlist.next(), "*silence*") class Playlist(object): def __init__(self, songs): self.songs = songs if __name__ == '__main__': unittest.main() ### When we run this, we'll see an error. ### $ python playlist.py E == ERROR: testSimple (__main__.PlaylistGeneratorTest) -- Traceback (most recent call last): File "playlist.py", line 6, in testSimple self.assertEqual(playlist.next(), "Happy") AttributeError: 'Playlist' object has no attribute 'next' -- Ran 1 test in 0.000s FAILED (errors=1) ### And in this case, we are overjoyed to see errors. The tests are telling us that we haven't implemented a next() method. For the sake of keeping this message short, let's write a quick and dirty implementation of one: ### ## Revised implementation of a Playlist: class Playlist(object): def __init__(self, songs): self.songs = songs def next(self): if not self.songs: return '*silence*' nextSong, self.songs = self.songs[0], self
Re: [Tutor] What exactly is "state"?
On Mon, Mar 2, 2015 at 11:25 AM, Sydney Shall wrote: > I am a beginner and I am now at the strage of learning to write unittests. > I have followed the current discussion entitled "How to test a class in > pyhton", and I am not clear precisely what is meant by state. In its common > meaning I can see some relevance. But is there a technical aspect to the > notion. I see it mentioned often and feel rather uncomfortable that I know > so little about it. > I have deliberately started a new thread. > Thanks. > -- > Sydney > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor 'State' is the present value of all variables related to the code that is running. Often code execution is dependent on the present state to determine the path of execution. -- Joel Goldstick http://joelgoldstick.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] What exactly is "state"?
I am a beginner and I am now at the strage of learning to write unittests. I have followed the current discussion entitled "How to test a class in pyhton", and I am not clear precisely what is meant by state. In its common meaning I can see some relevance. But is there a technical aspect to the notion. I see it mentioned often and feel rather uncomfortable that I know so little about it. I have deliberately started a new thread. Thanks. -- Sydney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor