1

I have an Enum class Station. (I'm using aenum, a library that "extends" python enum, and allows to use enums that have both an integer and a string) This class Station) stores constants, with number and string representation. All fine.

Now, I want to store constant lists of Station objects. What I'm doing works but feels wrong. (I'm kind of new to python / programing.) Does someone have an advice on that ? thank you.

Here is my code :

from typing import List
from aenum import Enum
from abc import ABC


class Station(Enum):
    _init_ = 'value string'

    BASTILLE = 3, 'Bastille'
    LEDRU = 1, 'Ledru Rollin'
    REPU = 10, 'République'
    CHATELET = 2, 'Châtelet les Halles'
    OURCQ = 5, 'Ourcq'
    
    def __str__(self):
        return self.string

    @property
    def nb(self):
        return self.value


class StationEnsemble(ABC):
    name :str
    stations : List[Station]

    @property
    def station_nbs_list(self):
        return [st.nb for st in self.stations]


class RecentStations(StationEnsemble):
    stations = [Station.OURCQ,Station.LEDRU,Station.CHATELET]

class EastStations(StationEnsemble):
    stations=[Station.BASTILLE,Station.OURCQ,Station.CHATELET]
5
  • 2
    If it works, you should probably post it at Code Review. This SE site is intended to provide extensive reviews of working code. Commented Nov 29, 2021 at 8:47
  • Why does it feel wrong? Commented Nov 29, 2021 at 8:47
  • @mkrieger1 : Creating multiple classes that only store one list seems overkill, but I have little experience so perhaps it s normal Commented Nov 29, 2021 at 8:56
  • Why did you create those classes instead of just defining "constant" lists RECENT_STATIONS = [Station.OURCQ, Station.LEDRU, Station.CHATELET] etc.? Commented Nov 29, 2021 at 9:06
  • Because I need to process those list a bit with methods like station_nbs_list Commented Nov 29, 2021 at 9:23

1 Answer 1

1

Using several classes for your lists seems awkward. Why not just write a single class and make the specific lists its instances? This would make the property work (since it needs to be looked up on an instance) also give you a chance to fill in the name attribute you type hinted but never initialized.

class StationEnsemble:
    def __init__(self, name: str, stations: List[Station]):
        self.name = name
        self.stations = stations

    @property
    def station_nbs_list(self):
        return [st.nb for st in self.stations]

RecentStations = StationsEnsemble("recent", [Station.OURCQ, Station.LEDRU, Station.CHATELET])
EastStations = StationsEnsemble("east", [Station.BASTILLE, Station.OURCQ, Station.CHATELET])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.