Skip to main content

Creating new nodes

Nodes can be created using the set or create methods.

The set method invokes create if the flag create_if_not_exist is set to True.

Command Usage:

PYNDatabase.create(*names, val=None)
PYNDatabase.Node.create(*names, val=None)

Although you can create values using the set method, the create method will ultimately be called in order to do so. Additionally, the create method allows you to create multiple new Nodes.

If the val flag is set to None (default), then the new Nodes will have a val of {} (an empty dictionary). The reason that the val flag is set to None by default is due to the mutable default argument dilemma (see 076ad6b, here).

If ANY of the names specified already exists, NONE of them will be created.

Examples:

1. Single/Multiple
2. What doesnt work (already exists (cancel whole operation))

from pyndb import PYNDatabase
db = PYNDatabse({})  # Creates a blank PYNDatabase from a new dict object
db.create('test')  # Creates a single Node named test (Node.val = {})
db.create('test2', val='hello')  # Creates a single Node named test2 with the value 'hello'
db.create('test3', 'testing', 'test4')  # Creates multiple Nodes (Node.val = {})
db.create('test5', 'testing2', 'test6', val='hello')  # Creates multiple Nodes with the value 'hello'

# This will not work!
db.create('test', 'testing3')
# Why?
# We already created <test> above.
# This means that NONE of the Nodes specified will be created.