Storing information on external files


#1

I’m a student working on farmbot as a semester project. The idea was to divide the plot of the farmbot up and have different regions according to plant-growth stages. In this context, I wanted to store information about each of these regions and plant emplacements in an xml file.

However, It seems as if it’s impossible to use etree in the farmbot context. Is that so? And if so, is there a work-around?

I was also planning on using pickler to easily be able to store objects, but I think that doesn’t work either does it?

Thanks in advance for any help,
–Ciarán


#2

Both are available as modules to Farmware, as they are part of the Python standard library:

import pickle
import xml.etree.ElementTree

#3

I do import both of them, but it gives me an error, immediately on the first call of xml.etree.ElementTree in the code. I’ve run it in shell on Ubuntu in debug mode and there it works perfectly.
Is there a specific way to import the modules or to pip them onto the bot?


#4

What is the error, and which version of Python are you using on Ubuntu?


#5

In Ubuntu, there is no error and the WebApp just gives me “Farmware execution failed”.

I am using Python 3.6.7 on Ubuntu 18.04.


#6

Farmware currently only supports Python 2.7. I imagine the code you’ve written uses Python 3 features which are causing errors when run with Python 2.


#7

I’ve now tested with python 2.7 (that was pretty stupid of me actually, using python 3.5). The error seems to have been that I was using the requests module, which doesn’t work in python 2.7. I haven’t tested with the robot yet, but I’m optimistic :smiley: I’ve now changed to urllib2.

I’ll give an update on how it went when I’ve tried on the robot.

Thanks for all the help!


#8

We hope to switch to Python 3 at some point, but are on Python 2 for now.

The requests module is currently available for use:

import requests

#9

Ok, so I changed back because urllib doesn’t work and since you told me that requests does in fact work.

The problem is, that it still gets caught on the xml parse part…

e = xml.etree.ElementTree.parse('./plantTypes.xml').getroot()

#10
def initPlantTypes(self):
    log("Present.", message_type='info')
    e = xml.etree.ElementTree.parse('./plantTypes.xml').getroot()
    log("Accessed plantTypes.xml", message_type='info')
    for plantType in e:
        name = plantType.attrib["name"]
        lightNeeded = int(plantType.attrib["lightNeeded"])
        gt0 = int(plantType.attrib["gt0"])
        gt1 = int(plantType.attrib["gt1"])        
        gt2 = int(plantType.attrib["gt2"])     
           
        self.plantTypeList.append(PlantType(name, lightNeeded, gt0, gt1, gt2))
    log("Loaded plant types.", message_type='info')

This is the function that is giving an error. It logs “Present”, but never logs “Accessed plant types.”
I’m at a loss. Is there a blatantly stupid error somewhere in there?

(Btw there is no path error. I triple checked it. The file does exist and is filled with xml, which on the computer is parsed perfectly.) (With python 2.7 this time)


#11

For debugging purposes you could try catching the error:

try:
    e = xml.etree.ElementTree.parse('')
except Exception as error:
    log(repr(error))

#12

Thx, I will try that tomorrow, as I don’t have access to the robot right now. I will give an update on how it went.