I am asking a question about file handling within python.
I am capable of creating a text file which can then be read via python. However, i am trying to create a python Recipe storer and therefore i would need to be able to store recipes (entered through python) with details about ingredients and quantities under records which can be opened and viewed with python. For example:
Two different recipes have been entered by the user, stored within a text document.
The user only wants to open one of these recipes.
How would the program be able to display the recipe which the user wants to open?

Any help would be much appreciated!

best to store to recipes in separate files. or consider some database management tool, or even xml.

Edited 2 Years Ago by iamthwee

I would suggest using a user friendly python application such as cherrytree. Each recipe could go in a separate node of the tree, wich could be organised like a cookbook. The other part of it is that cherrytree keeps the node contents in a sqlite database where data can easily be fetched by a python program.

Edited 2 Years Ago by Gribouillis

Can a python program write to the cherrytree database as well as fetch data from it?

Can a python program write to the cherrytree database as well as fetch data from it?

I did not try this. Here is some basic code to fetch node content and even execute python code stored in code nodes

#!/usr/bin/env python
#-*-coding: utf8-*-
from __future__ import unicode_literals, print_function, division, absolute_import


import sqlite3

cflags = (
    absolute_import.compiler_flag
    | division.compiler_flag
    | print_function.compiler_flag
    | unicode_literals.compiler_flag
)

class GetNodeError(Exception): pass

def get_node_content(dbname, nodename):
    conn = sqlite3.connect(dbname)
    c = conn.cursor()
    try:
        c.execute("SELECT txt FROM node WHERE name=?", (nodename,))
        L = c.fetchall()
    finally:
        c.close()
        conn.close()
    try:
        content = L[0][0]
    except IndexError:
        raise GetNodeError(('No node', nodename, 'found in', dbname))
    return content

def execnode(dbname, nodename, namespace):
    content = get_node_content(dbname, nodename)
    filename = "<node '{}' in cherrytree {}>".format(nodename, dbname)
    code = compile(content.encode('utf8'), filename, 'exec', cflags)
    exec(code, namespace)

I think it should not be too difficult to write to the database. Here is the database structure extracted from one of my files

+------------+-----+-----------------+---------+---------+------------+----+
| table_name | cid | name            | type    | notnull | dflt_value | pk |
+------------+-----+-----------------+---------+---------+------------+----+
| node       | 0   | node_id         | INTEGER | 0       | None       | 0  |
| node       | 1   | name            | TEXT    | 0       | None       | 0  |
| node       | 2   | txt             | TEXT    | 0       | None       | 0  |
| node       | 3   | syntax          | TEXT    | 0       | None       | 0  |
| node       | 4   | tags            | TEXT    | 0       | None       | 0  |
| node       | 5   | is_ro           | INTEGER | 0       | None       | 0  |
| node       | 6   | is_richtxt      | INTEGER | 0       | None       | 0  |
| node       | 7   | has_codebox     | INTEGER | 0       | None       | 0  |
| node       | 8   | has_table       | INTEGER | 0       | None       | 0  |
| node       | 9   | has_image       | INTEGER | 0       | None       | 0  |
| node       | 10  | level           | INTEGER | 0       | None       | 0  |
| codebox    | 0   | node_id         | INTEGER | 0       | None       | 0  |
| codebox    | 1   | offset          | INTEGER | 0       | None       | 0  |
| codebox    | 2   | justification   | TEXT    | 0       | None       | 0  |
| codebox    | 3   | txt             | TEXT    | 0       | None       | 0  |
| codebox    | 4   | syntax          | TEXT    | 0       | None       | 0  |
| codebox    | 5   | width           | INTEGER | 0       | None       | 0  |
| codebox    | 6   | height          | INTEGER | 0       | None       | 0  |
| codebox    | 7   | is_width_pix    | INTEGER | 0       | None       | 0  |
| codebox    | 8   | do_highl_bra    | INTEGER | 0       | None       | 0  |
| codebox    | 9   | do_show_linenum | INTEGER | 0       | None       | 0  |
| grid       | 0   | node_id         | INTEGER | 0       | None       | 0  |
| grid       | 1   | offset          | INTEGER | 0       | None       | 0  |
| grid       | 2   | justification   | TEXT    | 0       | None       | 0  |
| grid       | 3   | txt             | TEXT    | 0       | None       | 0  |
| grid       | 4   | col_min         | INTEGER | 0       | None       | 0  |
| grid       | 5   | col_max         | INTEGER | 0       | None       | 0  |
| image      | 0   | node_id         | INTEGER | 0       | None       | 0  |
| image      | 1   | offset          | INTEGER | 0       | None       | 0  |
| image      | 2   | justification   | TEXT    | 0       | None       | 0  |
| image      | 3   | anchor          | TEXT    | 0       | None       | 0  |
| image      | 4   | png             | BLOB    | 0       | None       | 0  |
| children   | 0   | node_id         | INTEGER | 0       | None       | 0  |
| children   | 1   | father_id       | INTEGER | 0       | None       | 0  |
| children   | 2   | sequence        | INTEGER | 0       | None       | 0  |
| bookmark   | 0   | node_id         | INTEGER | 0       | None       | 0  |
| bookmark   | 1   | sequence        | INTEGER | 0       | None       | 0  |
+------------+-----+-----------------+---------+---------+------------+----+

You could also access the database through cherrytree's own python code. There is no documented public api however.

Also notice that cherrytree can use a xml file instead of sqlite.

Edited 2 Years Ago by Gribouillis

Thanks for your help - after discussing with my teacher i do not need to go into too much depth and i only need to create a text file for each recipe. What would be the best approach for this - Ask the user to enter recipe name and then create a new text file with that name and then start asking for ingredients and quantities?

Edited 2 Years Ago by Fighter01

This article has been dead for over six months. Start a new discussion instead.