Giter Site home page Giter Site logo

Comments (12)

iamthebot avatar iamthebot commented on August 17, 2024 1

Yeah, I can put up a PR Friday.

Update: Ended up a little busy today (4/26) so didn't get to this but will add it next week.

from rattler.

baszalmstra avatar baszalmstra commented on August 17, 2024

Do you have an example and how you would change it?

from rattler.

iamthebot avatar iamthebot commented on August 17, 2024

For example, the current PathType class in rattler (eg; here) has getters for hardlink, softlink, etc. that just return True if the PathType is of that value. In other words, to get the path type you have to do a bunch of if/else until you hit a possible value that returns True. That's not really idiomatic and a major hassle if you have a lot of values. It also means downstream code needs to worry about any new enum values not being handled.

So for example, to determine the type of a path in Python I have to do something like:

(currently)

...
if path_type.directory:
  return "directory"
elif path_type.softlink:
  return "softlink"
elif path_type.hardlink:
  return "hardlink"

In the rust code in rattler-conda-types however (eg; here this is an enum. That is, the values are mutually exclusive. This means it would be also safe to expose these types as python enums.

If we did that, now it would be easy in python to:

  1. Get all possible valid values for the enum
  2. Convert from a string to the enum type easily
  3. Convert from the enum type to a string
  4. Check equality across enums of the same type

For example:

>>> print(path_type.value)
PathType.hardlink

>>> PathType["hardlink"] == PathType.hardlink
`True`

>>> assert path_type == PathType.symlink, "some assertion error"

Doing some cursory research, PyO3 does not natively have a way to export rust enums as Python enums. But a few approaches are suggested in this SO post. Another approach I don't see listed there that might be cleaner (and backwards compatible with older versions of Python) is to expose an int member for these types and on the python side have a matching int based enum with those values. That's more memory efficient too.

from rattler.

baszalmstra avatar baszalmstra commented on August 17, 2024

Thanks for the writeup! That makes total sense to me. Would you be able to whip up a Pr?

from rattler.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.