!rWxyQqNqMUDLECdsIf:blad.is

Poetry2nix

321 Members
https://github.com/nix-community/poetry2nix61 Servers

Load older messages


SenderMessageTime
16 Feb 2022
@k900:0upti.meK900 And 2) open works relative to current working directory, not source file location 20:27:35
@k900:0upti.meK900 The actually correct way to do this would be https://docs.python.org/3/library/importlib.html#module-importlib.resources 20:29:08
@worldofgeese:one.ems.hostworldofgeese

This is what I thought! But poetry run lbob actually wants the source directory path. Here's what I wrote in this commit explaining the choice:

Poetry has odd behavior around relative file paths. Python scripts must
import starting from the project (`little_bits_of_buddha`) even if they
occupy the same directory. This commit fixes file paths to operate from
"project in".
20:29:24
@worldofgeese:one.ems.hostworldofgeeseOf course I admit my thinking could be wrong here but I couldn't argue with the results20:29:37
@worldofgeese:one.ems.hostworldofgeese As poetry run lbob and the Nix derivation both run successfully 20:29:49
@k900:0upti.meK900So you fixed issue #1, kind of20:31:13
@k900:0upti.meK900But you still have issue #220:31:19
@k900:0upti.meK900 The easy way out would be to do something like pathlib.Path(__file__) / "../data.json" 20:31:50
@k900:0upti.meK900 The correct way out would be to use importlib.resources 20:32:05
@worldofgeese:one.ems.hostworldofgeeseInteresting. Can you give me an example of how I would use importlib.resources in this case?20:32:40
@k900:0upti.meK900 Basically just from importlib import resources; resources.files("your_package_name").join("data.json").read_text() 20:34:19
@worldofgeese:one.ems.hostworldofgeese From inside my data.py file right? It would replace the open() 20:35:20
@k900:0upti.meK900It would replace the whole thing 20:35:52
@worldofgeese:one.ems.hostworldofgeeseThis is my first real Python project, sorry if my questions are very amateur20:36:03
@k900:0upti.meK900 You probably want to then pass the result to json.loads 20:36:04
@k900:0upti.meK900Also, you might want to move that out of the function 20:36:43
@k900:0upti.meK900Because importlib will be slower than just reading the file off the disk 20:37:00
@k900:0upti.meK900Probably not by enough for it to mattrr20:37:10
@k900:0upti.meK900* Probably not by enough for it to matter20:37:13
@k900:0upti.meK900But it's generally good practice to load resources once and then keep them in memory 20:37:26
@k900:0upti.meK900Because your project could be installed as a wheel, or as an egg, or as whatever other weird format whatever other weird Python implementation uses 20:38:02
@worldofgeese:one.ems.hostworldofgeeseWow, this is really helpful, thank you for taking the time with me! I will try and implement your suggestions and see if I can marshal it into some form of life20:39:54
@worldofgeese:one.ems.hostworldofgeese * Wow, this is really helpful, thank you for taking the time with me! I will try and implement your suggestions and see if I can marshal my application into some form of life20:43:18
@asymmetric:matrix.dapp.org.ukasymmetric left the room.22:08:54
17 Feb 2022
@worldofgeese:one.ems.hostworldofgeese

I've changed the code of data.py to access the package resource data.json like so:

from importlib import resources

with resources.open_text("little_bits_of_buddha", "data.json") as json_file:
    data = json.load(json_file)


def random_sutta():
    attributed_quotes = []
    for collection in data["collection"].values():
        for quote in collection.values():
            attributed_quotes.append(quote)
    return random.choice(attributed_quotes)

which again works with poetry run lbob and nix build && ./result/bin/lbob but continues to give errors accessing the resource from inside dockerTools.streamLayeredImage produced image:

  File "/nix/store/y3i0wgxmwc9l2r5a167isx4bnpsm4fih-python3.9-little-bits-of-buddha-0.1.0/lib/python3.9/site-packages/little_bits_of_buddha/data.py", line 7, in <module>
    with resources.open_text(__package__, "data.json") as json_file:
  File "/nix/store/i6vabb4div9iy6lsl642d86k1q8riasn-python3-3.9.9/lib/python3.9/importlib/resources.py", line 121, in open_text
    open_binary(package, resource), encoding=encoding, errors=errors)
  File "/nix/store/i6vabb4div9iy6lsl642d86k1q8riasn-python3-3.9.9/lib/python3.9/importlib/resources.py", line 111, in open_binary
    raise FileNotFoundError(message)
FileNotFoundError: 'data.json' resource not found in 'little_bits_of_buddha'
07:39:15
@worldofgeese:one.ems.hostworldofgeese *

I've changed the code of data.py to access the package resource data.json like so:

from importlib import resources

with resources.open_text("little_bits_of_buddha", "data.json") as json_file:
    data = json.load(json_file)


def random_sutta():
    attributed_quotes = []
    for collection in data["collection"].values():
        for quote in collection.values():
            attributed_quotes.append(quote)
    return random.choice(attributed_quotes)

which again works with poetry run lbob and nix build && ./result/bin/lbob but continues to give errors accessing the resource from inside a dockerTools.streamLayeredImage produced image:

  File "/nix/store/y3i0wgxmwc9l2r5a167isx4bnpsm4fih-python3.9-little-bits-of-buddha-0.1.0/lib/python3.9/site-packages/little_bits_of_buddha/data.py", line 7, in <module>
    with resources.open_text(__package__, "data.json") as json_file:
  File "/nix/store/i6vabb4div9iy6lsl642d86k1q8riasn-python3-3.9.9/lib/python3.9/importlib/resources.py", line 121, in open_text
    open_binary(package, resource), encoding=encoding, errors=errors)
  File "/nix/store/i6vabb4div9iy6lsl642d86k1q8riasn-python3-3.9.9/lib/python3.9/importlib/resources.py", line 111, in open_binary
    raise FileNotFoundError(message)
FileNotFoundError: 'data.json' resource not found in 'little_bits_of_buddha'
07:39:42
@k900:0upti.meK900Post the actual image config07:40:40
@k900:0upti.meK900And check if the file is there in your container07:40:47
@worldofgeese:one.ems.hostworldofgeese Image config is here from line 23 07:41:43
@worldofgeese:one.ems.hostworldofgeese
bash-5.1# ls /nix/store/i6vabb4div9iy6lsl642d86k1q8riasn-python3.9-little-bits-of-buddha-0.1.0/lib/python3.9/site-packages/little_bits_of_buddha/
__pycache__  app.py  data.json	data.py  run.py
07:44:38

Show newer messages


Back to Room ListRoom Version: 6