Re: [Tutor] serial device emulator
thanks chirs but i think i don't follow you , can you elaborate more ? Thanks On Mon, Jul 4, 2011 at 8:49 PM, Chris Fuller wrote: > > You don't need to emulate a serial port (since you're writing the code; you'd > have to emulate the port if it was standalone software), only your serial port > library. Write a class that has the same methods as the serial port library > you're using (you only need the methods you're using or think you might use > later), and fill them in with the appropriate code so it behaves like your > real > device. > > Cheers > > On Monday 04 July 2011, Edgar Almonte wrote: >> Hello list need some advice/help with something, i am doing a program >> in python that send some command via serial to a device so far so good >> , the thing is not have the program so i need make another program >> that emulate the behavior of this serial device ( is quiet simple ) to >> test my app >> i read abou that i can use pseudo terminal in linux but not sure how >> attatch the pseudo terminal /dev/pts5 example to a fake_device.py file >> or something like that. >> >> maybe this question is not so python but i will appreciate some help. >> >> >> Thanks >> ___ >> Tutor maillist - Tutor@python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] serial device emulator
What Chris is getting at is that you'll use some module, eg pyserial, to interface with the serial port. So if you write a little module that has the same interface then you can pretend you have a serial port attached device and then switch over to an actual one without changing anything else in your code but the import. ## myserial.py ## class Serial: def __init__(self, port=None): pass def open(self): pass def close(): pass def read(size=1): return 'a' * size def write(data): return len(data) # ## foo.py ## import myserial as serial #import serial ## Use this in production port = serial.Serial() port.open() print port.write("hello, world") print port.read(3) I hope that makes it clearer to you. Adam. P.S. none of that code has been tested On 05/07/11 13:03, Edgar Almonte wrote: thanks chirs but i think i don't follow you , can you elaborate more ? Thanks On Mon, Jul 4, 2011 at 8:49 PM, Chris Fuller wrote: You don't need to emulate a serial port (since you're writing the code; you'd have to emulate the port if it was standalone software), only your serial port library. Write a class that has the same methods as the serial port library you're using (you only need the methods you're using or think you might use later), and fill them in with the appropriate code so it behaves like your real device. Cheers On Monday 04 July 2011, Edgar Almonte wrote: Hello list need some advice/help with something, i am doing a program in python that send some command via serial to a device so far so good , the thing is not have the program so i need make another program that emulate the behavior of this serial device ( is quiet simple ) to test my app i read abou that i can use pseudo terminal in linux but not sure how attatch the pseudo terminal /dev/pts5 example to a fake_device.py file or something like that. maybe this question is not so python but i will appreciate some help. Thanks ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] serial device emulator
got it , thanks a lot On Tue, Jul 5, 2011 at 8:18 AM, Adam Bark wrote: > What Chris is getting at is that you'll use some module, eg pyserial, to > interface with the serial port. So if you write a little module that has the > same interface then you can pretend you have a serial port attached device > and then switch over to an actual one without changing anything else in your > code but the import. > > ## myserial.py ## > class Serial: > def __init__(self, port=None): > pass > def open(self): > pass > def close(): > pass > def read(size=1): > return 'a' * size > def write(data): > return len(data) > # > > ## foo.py ## > import myserial as serial > #import serial ## Use this in production > > port = serial.Serial() > port.open() > print port.write("hello, world") > print port.read(3) > > > I hope that makes it clearer to you. > Adam. > P.S. none of that code has been tested > > > On 05/07/11 13:03, Edgar Almonte wrote: >> >> thanks chirs but i think i don't follow you , can you elaborate more ? >> >> Thanks >> >> On Mon, Jul 4, 2011 at 8:49 PM, Chris Fuller >> wrote: >>> >>> You don't need to emulate a serial port (since you're writing the code; >>> you'd >>> have to emulate the port if it was standalone software), only your serial >>> port >>> library. Write a class that has the same methods as the serial port >>> library >>> you're using (you only need the methods you're using or think you might >>> use >>> later), and fill them in with the appropriate code so it behaves like >>> your real >>> device. >>> >>> Cheers >>> >>> On Monday 04 July 2011, Edgar Almonte wrote: Hello list need some advice/help with something, i am doing a program in python that send some command via serial to a device so far so good , the thing is not have the program so i need make another program that emulate the behavior of this serial device ( is quiet simple ) to test my app i read abou that i can use pseudo terminal in linux but not sure how attatch the pseudo terminal /dev/pts5 example to a fake_device.py file or something like that. maybe this question is not so python but i will appreciate some help. Thanks ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor >>> >>> ___ >>> Tutor maillist - Tutor@python.org >>> To unsubscribe or change subscription options: >>> http://mail.python.org/mailman/listinfo/tutor >>> >> ___ >> Tutor maillist - Tutor@python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Using a dict value in the same dict
Hi, I have a dictionary with the keys 'a' and 'b'. It is not in a class. (I know that everything is in a class, but not explicitly.) May I use the value of 'a' when defining the value of 'b'? If so, what is the syntax? Thx, Péter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using a dict value in the same dict
2011/7/5 Válas Péter : > Hi, > > I have a dictionary with the keys 'a' and 'b'. It is not in a class. (I know > that everything is in a class, but not explicitly.) > May I use the value of 'a' when defining the value of 'b'? If so, what is > the syntax? Yes. The syntax is the same as anything involving a dict - >>> a_dict = dict() >>> a_dict['a'] = 2 >>> a_dict['b'] = a_dict['a'] + 1 >>> a_dict {'a': 2, 'b': 3} ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using a dict value in the same dict
Válas Péter wrote: > I have a dictionary with the keys 'a' and 'b'. It is not in a class. (I > know that everything is in a class, but not explicitly.) > May I use the value of 'a' when defining the value of 'b'? If so, what is > the syntax? >>> d = {} >>> d["a"] = 1 >>> d["b"] = d["a"] + 1 >>> d {'a': 1, 'b': 2} If you want the value of "b" updated whenever d["a"] changes, i. e. that the dict values behave like spreadsheet cells, that is not possible with a standard python dictionary. There is a recipe by Raymond Hettinger with a simple implementation of that behaviour at http://code.activestate.com/recipes/355045-spreadsheet/ As it uses eval() it is OK for private use, but you shouldn't allow a potentially malicious user to run it. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using a dict value in the same dict
"Válas Péter" wrote I have a dictionary with the keys 'a' and 'b'. It is not in a class. May I use the value of 'a' when defining the value of 'b'? If so, what is the syntax? single = {'a': 1, 'b': 2 } double = { 'a': single['a'] *2, 'b' : single['b'] * 2 } single['c'] = 3 double['c'] = single['c'] * 2 Does that help? Is that what you mean? -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using a dict value in the same dict
So the trick is to define the dictionary in separate sessions, not at once. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using a dict value in the same dict
Hi, 7/5 Válas Péter > So the trick is to define the dictionary in separate sessions, not at once. > What do you mean, "seperate sessions, not at once"? W ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using a dict value in the same dict
On Tue, Jul 5, 2011 at 3:40 PM, Walter Prins wrote: >> So the trick is to define the dictionary in separate sessions, not at >> once. > > What do you mean, "seperate sessions, not at once"? He means you can't say: d = {a: "1", b: d["a"]} Which is correct. To set one value based on another they must be set separately. -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] broken script
I am copy-typing the following pre-written program: def break_words(stuff): """This function will break up words for us.""" words=stuff.split(' ') return words def sort_words(words): """Sorts the words.""" return sorted(words) def print_first_word(words): """Prints the first word after popping it off.""" word=words.pop(0) print word I am testing at the end of each section as I type it. As far as line 9 it was fine - but, once I have typed up to line 14, it jibs at line 10 with the following error message: lisi@Tux:~/Python/LearnPythonTheHardWay$ python ex26.py File "ex26.py", line 10 def print_first_word(words) ^ SyntaxError: invalid syntax lisi@Tux:~/Python/LearnPythonTheHardWay$ (The caret should be under the closing bracket at the end of the line in which it is the penultimate character.) I have deleted and re-copy-typed the end of the line repeatedly. In despair, I deleted the end of the line and copied and pasted the end of line 6 into it. I have checked and rechecked for incorrect white-space. I can find nothing wrong. I have copy-pasted it into a WP just to make the non-printing characters visible. I can still see nothing wrong. Why is line 6 fine and line 10 a disaster? I can see no difference in the syntax, though there must clearly be one. :-( I am at a loss as to what to do next. (It is the next exercise that is supposed to have errors in for us to put right!!!) Pointers in the right direction very gratefully received! Thanks, Lisi ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] broken script
Hello, : I am copy-typing the following pre-written program: : : def break_words(stuff): : """This function will break up words for us.""" : words=stuff.split(' ') : return words : : def sort_words(words): : """Sorts the words.""" : return sorted(words) : : def print_first_word(words): : """Prints the first word after popping it off.""" : word=words.pop(0) : print word : : I am testing at the end of each section as I type it. As far as line 9 it was : fine - but, once I have typed up to line 14, it jibs at line 10 with the : following error message: : : lisi@Tux:~/Python/LearnPythonTheHardWay$ python ex26.py : File "ex26.py", line 10 : def print_first_word(words) : ^ : SyntaxError: invalid syntax : lisi@Tux:~/Python/LearnPythonTheHardWay$ Look at the error. Look at the error carefully. Look at the definitions of your other functions Compare. What is different? do you see the colon? | V def silly_function(args): Curiously, the sample that you pasted above has the required colon at the end of the line which starts the function definition. : def print_first_word(words): : """Prints the first word after popping it off.""" : word=words.pop(0) : print word I have heard people express frustration many times about how a program(ming language) or "the computer" did not understand something because the thing was 'missing a damned semicolon'. Unfortunately, these syntactical rules are quite important to our very fast, but not terribly intuitive friends of silicon. : (The caret should be under the closing bracket at the end of the : line in which it is the penultimate character.) I have deleted : and re-copy-typed the end of the line repeatedly. In despair, I : deleted the end of the line and copied and pasted the end of line : 6 into it. I have checked and rechecked for incorrect : white-space. I can find nothing wrong. I have copy-pasted it : into a WP just to make the non-printing characters visible. I : can still see nothing wrong. Why is line 6 fine and line 10 a : disaster? I can see no difference in the syntax, though there : must clearly be one. :-( : : I am at a loss as to what to do next. (It is the next exercise : that is supposed to have errors in for us to put right!!!) : : Pointers in the right direction very gratefully received! Try again, keep with it, and recognize that these beasties are awfully particular in what they accept. And, for good reason. Best of luck, Lisi, -Martin -- Martin A. Brown http://linux-ip.net/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using a dict value in the same dict
Válas Péter wrote: So the trick is to define the dictionary in separate sessions, not at once. No. value = 42 my_dict = {'a': value, 'b': value, 'c': 23, 'd': value, 'e': 97} will work fine too. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] broken script
On Tue, Jul 5, 2011 at 3:23 PM, Martin A. Brown wrote: > I have heard people express frustration many times about how a > program(ming language) or "the computer" did not understand > something because the thing was 'missing a damned semicolon'. > > Unfortunately, these syntactical rules are quite important to our > very fast, but not terribly intuitive friends of silicon. > > I really hate this dumb machine; I wish that they would sell it. It never does just what I want - but only what I tell it. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor