← index #3086Issue #8058
Related · high · value 1.247
QUERY · ISSUE

open: No support for creating file if doesn't exist, while opening in r/w mode

openby pfalconopened 2017-05-13updated 2017-06-05
enhancement

There doesn't seem to be a mode for CPython3 open() to do subj. The closest one is "a+", but that additionally seeks to the end of file. Now the question is whether "a+" has a relation to POSIX open() mode O_APPEND which stipulates that any write to file opened with it happen at EOF (lseek() for writing is ignored).

Usecase: e.g., opening a file for btree.open().

CANDIDATE · ISSUE

mpremote: btree database won't work

closedby chrisdecker1201opened 2021-12-02updated 2021-12-10
tools

Hello,

I really like micropython and really like mpremote :).

I'm using micropython 1.17.

I started now using btree to store some date in combination with mpremote. But I'm having problems, due to a wrong btree-database.

Steps to reproduce

  1. create btree.py (that's just the example from the documentation)
import btree

# First, we need to open a stream which holds a database
# This is usually a file, but can be in-memory database
# using io.BytesIO, a raw flash partition, etc.
# Oftentimes, you want to create a database file if it doesn't
# exist and open if it exists. Idiom below takes care of this.
# DO NOT open database with "a+b" access mode.
try:
    f = open("mydb", "r+b")
except OSError:
    f = open("mydb", "w+b")

# Now open a database itself
db = btree.open(f)

# The keys you add will be sorted internally in the database
db[b"3"] = b"three"
db[b"1"] = b"one"
db[b"2"] = b"two"

# Assume that any changes are cached in memory unless
# explicitly flushed (or database closed). Flush database
# at the end of each "transaction".
db.flush()

# Prints b'two'
print(db[b"2"])

# Iterate over sorted keys in the database, starting from b"2"
# until the end of the database, returning only values.
# Mind that arguments passed to values() method are *key* values.
# Prints:
#   b'two'
#   b'three'
for word in db.values(b"2"):
    print(word)

del db[b"2"]

# No longer true, prints False
print(b"2" in db)

# Prints:
#  b"1"
#  b"3"
for key in db:
    print(key)

db.close()

# Don't forget to close the underlying stream!
f.close()
  1. install mpremote
pip3 install mpremote==0.0.6
  1. execute btree.py with mounting
mpremote u0 mount . run btree.py

output:

b'two'
b'two'
b'three'
False
b'1'
b'3'
  1. execute btree.py a second time
mpremote u0 mount . run btree.py

output:

Local directory . is mounted at /remote
Traceback (most recent call last):
  File "<stdin>", line 15, in <module>
OSError: 79

Expected behaviour:

Same behaviour like when I'm copying the btree.py file to the board and executing it:

 mpremote u0 cp btree.py :
 mpremote u0 run btree.py
 mpremote u0 run btree.py
 mpremote u0 cp :mydb mydb

output:

# first run
b'two'
b'two'
b'three'
False
b'1'
b'3'
# second run
b'two'
b'two'
b'three'
False
b'1'
b'3'

Some notes

  • I found out, when I execute the steps from the expected behaviour (copy the mydb from the board on my computer) and then execute mpremote u0 mount . run btree.py it works.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied