[Tutor] truncated dictionary return

richard kappler richkappler at gmail.com
Mon Dec 2 03:00:06 CET 2013


Would something like

if len(dict) = 8
    return d
else
    continue

work?


On Sun, Dec 1, 2013 at 8:54 PM, richard kappler <richkappler at gmail.com>wrote:

> Now I'm completely lost. While opening the serial port outside the
> function sounds like a good idea, I'm thinking that might not work unless I
> am mistaken. The sensorRead function once it's called would then basically
> own the serial port barring other traffic, yes? That won't work as the same
> serial port that receives sensor data from the arduino sends propulsion and
> nav signals to the arduino which, along with controlling/reading the
> sensors, also controls the motors used for propulsion, hence only opening
> the port when the data is called for. The sensorRead function works, heck
> it's not even mine, it was written by one of the gurus here in response to
> a question I posed months ago (either Alan or Eryksun IIRC) and does
> exactly what it's supposed to do, except for the timing bit.
>
> Perhaps I'm looking for a simple solution where none exists but I rather
> doubt it. I was thinking something along the lines of (psuedo code here)
> check incoming dict for length or number of elements
> if 8, keep
> else retry
>
> While I appreciate the above comments and any help that is offered, I
> neither understand them as presented nor think they will fix the problem
> with the limited understanding I do have. Again, it could be my lack of
> knowledge is preventing me from seeing the light here, but it feels like
> we're reinventing the wheel.
>
> I hope that didn't come across as rude, it truly was not intended to be
> such.
>
> regards, Richard
>
>
> On Sun, Dec 1, 2013 at 3:06 PM, spir <denis.spir at gmail.com> wrote:
>
>> On 12/01/2013 08:28 PM, richard kappler wrote:
>>
>>> I have a script that reads sensor values gathered by an Arduino board
>>> from
>>> serial as a dictionary, said values to later be used in the AI for Nav &
>>> Control. Here's the script:
>>>
>>> #!/usr/bin/python
>>>
>>> def sensorRead():
>>>      import serial
>>>      from time import sleep
>>>
>>>      sensors = {}
>>>      sensors = dict.fromkeys('Sonar1 Sonar2 Sonar3 Sonar4 Dewpoint
>>> Temperature Humidity Light'.split())
>>>
>>>      arduino = serial.Serial('/dev/ttyACM0', 9600)
>>>      sleep(1)
>>>      line = arduino.readline().strip()
>>>      line = line.lstrip('{').rstrip('}').strip()
>>>
>>>      d = {}
>>>      for item in line.split(','):
>>>          item = item.strip()
>>>          key, value = item.split(':')
>>>          key = key.strip()
>>>          value = value.strip()
>>>          d[key]=int(value)
>>>      return d
>>>
>>> I hope that comes through okay, I copied it from the text file so
>>> indentation and such should be fine, if not let me know.
>>>
>>> The script works great with one exception. I understand the problem, I'm
>>> just not sure how to address it. The problem is:
>>>
>>> The Arduino runs on a constant loop, it reads each sensor, sends the key
>>> and the value to the serial bus in format for python to read it as a
>>> dictionary, lather, rinse, repeat.
>>>
>>> Python querries the bus when told. Usually the python script gets the
>>> full
>>> dictionary (all 8 values with keys, brackets etc) but sometimes it
>>> doesn't.
>>> Sometimes it only gets the last few values, sometimes it gets nothing or
>>> misses a bracket and throws an error. This makes sense. They are not in
>>> sync.
>>>
>>> What I need to figure out how to do is have the python script wait until
>>> the next round of values as signified by the opening bracket "{" or check
>>> that it has all 8 values and if not retry or.... something.
>>>
>>> Would this be an if/else? try? exception?
>>>
>>> I've not yet delved into any of these in my quest to learn python except
>>> if/else and that doesn't feel right for this, so I'm at a loss as to how
>>> to
>>> proceed.
>>>
>>
>> * What is the point of the 'sensors' dict? (also, you don't need to
>> initialise it as an enmpty dict)
>> * If you know about regexps or another matching utility, use that to
>> decode the input line into (key,value) pairs. This is even easier if input
>> has a strict format. Would tell us?
>> * About wrong input (incomplete data), avoid try/except, except if ever
>> most cases are ok (it is very costly in case of exception). Anyway, you
>> need to decode input, so use that to check for errors/missing stuff.
>>
>> If you used a matching tool, its absence of result would directly tell
>> about wrong input (provided your pattern is correct! ;-) As of now, just
>> place checks in your decoding sequence. Possible checks places, at first
>> sight, marked below:
>>
>>
>>     line = arduino.readline().strip()
>>     line = line.lstrip('{').rstrip('}').strip()
>>     # check line not empty
>>     # (actually big enough for at least {} + one key:val entry)
>>
>>     d = {}
>>     # first get items in separate var and check how many:
>>
>>     for item in line.split(','):
>>         item = item.strip()          # not needed, for you strip key/val
>> again later
>>         # first get tuple from split() and check its size is 2
>>
>>         key, value = item.split(':')
>>         key = key.strip()
>>         value = value.strip()        # not needed, for int() itself strips
>>         # maybe one try/except here for checking conversion to int:
>>         # (else, you need to check yourself it is an int numeral)
>>         d[key]=int(value)
>>
>> Each check failure is a sign of wrong input. What do you need to do,
>> then? Abandon the whole round?
>>
>> Denis
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
>>
>
>
>
> --
>
> *Mater tua criceta fuit, et pater tuo redoluit bacarum sambucus*
>



-- 

*Mater tua criceta fuit, et pater tuo redoluit bacarum sambucus*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131201/413b7485/attachment.html>


More information about the Tutor mailing list