why is the following attribute not found in enum?

  • Last Update :
  • Techknowledgy :

You need to use the enum34 backport:

pip install enum34

you have to install enum34 to get key value pair

yum install python - enum34

Suggestion : 2

This module defines four enumeration classes that can be used to define unique sets of names and values: Enum, IntEnum, Flag, and IntFlag. It also defines one decorator, unique(), and one helper, auto.,Base class for creating enumerated constants that can be combined using the bitwise operators without losing their IntFlag membership. IntFlag members are also subclasses of int.,While Enum, IntEnum, IntFlag, and Flag are expected to cover the majority of use-cases, they cannot cover them all. Here are recipes for some different types of enumerations that can be used directly, or as examples for creating one’s own.,In many use-cases one doesn’t care what the actual value of an enumeration is. There are several ways to define this type of simple enumeration:

>>> from enum
import Enum
   >>>
   class Color(Enum):
   ...RED = 1
   ...GREEN = 2
   ...BLUE = 3
   ...
>>> print(Color.RED)
Color.RED
>>> print(repr(Color.RED))
<Color.RED: 1>
>>> type(Color.RED)
<enum 'Color'>
   >>> isinstance(Color.GREEN, Color)
   True
   >>>
>>> print(Color.RED.name)
RED
>>> class Shake(Enum):
   ...VANILLA = 7
   ...CHOCOLATE = 4
   ...COOKIES = 9
   ...MINT = 3
   ...
   >>>
   for shake in Shake:
   ...print(shake)
   ...
   Shake.VANILLA
Shake.CHOCOLATE
Shake.COOKIES
Shake.MINT

Suggestion : 3

An ENUM is a string object with a value chosen from a list of permitted values that are enumerated explicitly in the column specification at table creation time. , An enumeration value must be a quoted string literal. For example, you can create a table with an ENUM column like this: , Trailing spaces are automatically deleted from ENUM member values in the table definition when a table is created. , Compact data storage in situations where a column has a limited set of possible values. The strings you specify as input values are automatically encoded as numbers. See Section 11.7, “Data Type Storage Requirements” for storage requirements for the ENUM type.

An enumeration value must be a quoted string literal. For example, you can create a table with an ENUM column like this:

CREATE TABLE shirts(
   name VARCHAR(40),
   size ENUM('x-small', 'small', 'medium', 'large', 'x-large')
);
INSERT INTO shirts(name, size) VALUES('dress shirt', 'large'), ('t-shirt', 'medium'),
   ('polo shirt', 'small');
SELECT name, size FROM shirts WHERE size = 'medium'; +
-- -- -- -- - + -- -- -- -- +
|
name | size |
   + -- -- -- -- - + -- -- -- -- +
   |
   t - shirt | medium |
   + -- -- -- -- - + -- -- -- -- +
   UPDATE shirts SET size = 'small'
WHERE size = 'large';
COMMIT;

The index value of the empty string error value is 0. This means that you can use the following SELECT statement to find rows into which invalid ENUM values were assigned:

mysql > SELECT * FROM tbl_name WHERE enum_col = 0;

The index value of the empty string error value is 0. This means that you can use the following SELECT statement to find rows into which invalid ENUM values were assigned:

mysql > SELECT * FROM tbl_name WHERE enum_col = 0;

If you retrieve an ENUM value in a numeric context, the column value's index is returned. For example, you can retrieve numeric values from an ENUM column like this:

mysql > SELECT enum_col + 0 FROM tbl_name;

If you store a number into an ENUM column, the number is treated as the index into the possible values, and the value stored is the enumeration member with that index. (However, this does not work with LOAD DATA, which treats all input as strings.) If the numeric value is quoted, it is still interpreted as an index if there is no matching string in the list of enumeration values. For these reasons, it is not advisable to define an ENUM column with enumeration values that look like numbers, because this can easily become confusing. For example, the following column has enumeration members with string values of '0', '1', and '2', but numeric index values of 1, 2, and 3:

numbers ENUM('0', '1', '2')

If you store 2, it is interpreted as an index value, and becomes '1' (the value with index 2). If you store '2', it matches an enumeration value, so it is stored as '2'. If you store '3', it does not match any enumeration value, so it is treated as an index and becomes '2' (the value with index 3).

mysql > INSERT INTO t(numbers) VALUES(2), ('2'), ('3');
mysql > SELECT * FROM t; +
-- -- -- -- - +
|
numbers |
   + -- -- -- -- - +
   |
   1 |
   |
   2 |
   |
   2 |
   + -- -- -- -- - +

For example, this CREATE TABLE statement does not work because the CONCAT function cannot be used to construct an enumeration value:

CREATE TABLE sizes(
   size ENUM('small', CONCAT('med', 'ium'), 'large')
);

You also cannot employ a user variable as an enumeration value. This pair of statements do not work:

SET @mysize = 'medium';

CREATE TABLE sizes(
   size ENUM('small', @mysize, 'large')
);

Suggestion : 4

The type of value is not an underlying type of enumType.,The type of value is an enumeration, but it is not an enumeration of type enumType.,true if a given integral value, or its name as a string, exists in a specified enumeration; false otherwise.,The following example defines an enumeration named PetType that consists of individual bit fields. It then calls the IsDefined method with possible underlying enumeration values, string names, and composite values that result from setting multiple bit fields.

1._
public:
   static bool IsDefined(Type ^ enumType, System::Object ^ value);
public:
 static bool IsDefined(Type ^ enumType, System::Object ^ value);
public static bool IsDefined(Type enumType, object value);
public static bool IsDefined (Type enumType, object value);
[System.Runtime.InteropServices.ComVisible(true)]
public static bool IsDefined(Type enumType, object value);
static member IsDefined : Type * obj -> bool
[<System.Runtime.InteropServices.ComVisible(true)>]
   static member IsDefined : Type * obj -> bool
[<System.Runtime.InteropServices.ComVisible(true)>]
static member IsDefined : Type * obj -> bool
Public Shared Function IsDefined(enumType As Type, value As Object) As Boolean
1._
public:
generic <typename TEnum>
   where TEnum : value class static bool IsDefined(TEnum value);
public:
generic <typename TEnum>
 where TEnum : value class static bool IsDefined(TEnum value);
public static bool IsDefined<TEnum> (TEnum value) where TEnum : struct;
public static bool IsDefined<TEnum> (TEnum value) where TEnum : struct;
static member IsDefined: 'Enum -> bool (requires '
Enum: struct)

Suggestion : 5

Because they are constants, the names of an enum type's fields are in uppercase letters.,In the Java programming language, you define an enum type by using the enum keyword. For example, you would specify a days-of-the-week enum type as:,In the following example, Planet is an enum type that represents the planets in the solar system. They are defined with constant mass and radius properties.,You should use enum types any time you need to represent a fixed set of constants. That includes natural enum types such as the planets in our solar system and data sets where you know all possible values at compile time—for example, the choices on a menu, command line flags, and so on.

public enum Day {
   SUNDAY,
   MONDAY,
   TUESDAY,
   WEDNESDAY,
   THURSDAY,
   FRIDAY,
   SATURDAY
}
public class EnumTest {
   Day day;

   public EnumTest(Day day) {
      this.day = day;
   }

   public void tellItLikeItIs() {
      switch (day) {
         case MONDAY:
            System.out.println("Mondays are bad.");
            break;

         case FRIDAY:
            System.out.println("Fridays are better.");
            break;

         case SATURDAY:
         case SUNDAY:
            System.out.println("Weekends are best.");
            break;

         default:
            System.out.println("Midweek days are so-so.");
            break;
      }
   }

   public static void main(String[] args) {
      EnumTest firstDay = new EnumTest(Day.MONDAY);
      firstDay.tellItLikeItIs();
      EnumTest thirdDay = new EnumTest(Day.WEDNESDAY);
      thirdDay.tellItLikeItIs();
      EnumTest fifthDay = new EnumTest(Day.FRIDAY);
      fifthDay.tellItLikeItIs();
      EnumTest sixthDay = new EnumTest(Day.SATURDAY);
      sixthDay.tellItLikeItIs();
      EnumTest seventhDay = new EnumTest(Day.SUNDAY);
      seventhDay.tellItLikeItIs();
   }
}
Mondays are bad.
Midweek days are so - so.
Fridays are better.
Weekends are best.
Weekends are best.
for (Planet p: Planet.values()) {
   System.out.printf("Your weight on %s is %f%n",
      p, p.surfaceWeight(mass));
}
public enum Planet {
    MERCURY (3.303e+23, 2.4397e6),
    VENUS   (4.869e+24, 6.0518e6),
    EARTH   (5.976e+24, 6.37814e6),
    MARS    (6.421e+23, 3.3972e6),
    JUPITER (1.9e+27,   7.1492e7),
    SATURN  (5.688e+26, 6.0268e7),
    URANUS  (8.686e+25, 2.5559e7),
    NEPTUNE (1.024e+26, 2.4746e7);

    private final double mass;   // in kilograms
    private final double radius; // in meters
    Planet(double mass, double radius) {
        this.mass = mass;
        this.radius = radius;
    }
    private double mass() { return mass; }
    private double radius() { return radius; }

    // universal gravitational constant  (m3 kg-1 s-2)
    public static final double G = 6.67300E-11;

    double surfaceGravity() {
        return G * mass / (radius * radius);
    }
    double surfaceWeight(double otherMass) {
        return otherMass * surfaceGravity();
    }
    public static void main(String[] args) {
        if (args.length != 1) {
            System.err.println("Usage: java Planet <earth_weight>");
            System.exit(-1);
        }
        double earthWeight = Double.parseDouble(args[0]);
        double mass = earthWeight/EARTH.surfaceGravity();
        for (Planet p : Planet.values())
           System.out.printf("Your weight on %s is %f%n",
                             p, p.surfaceWeight(mass));
    }
}
$ java Planet 175
Your weight on MERCURY is 66.107583
Your weight on VENUS is 158.374842
Your weight on EARTH is 175.000000
Your weight on MARS is 66.279007
Your weight on JUPITER is 442.847567
Your weight on SATURN is 186.552719
Your weight on URANUS is 158.397260
Your weight on NEPTUNE is 199.207413

Suggestion : 6

You can’t use an AttributeConverter on an entity attribute that’s annotated with @Enumerated. So, you need to remove that annotation from your mapping if you want to use the custom mapping.,If you instead want to persist the String representation of your enum value, you need to annotate your entity attribute with @Enumerated(EnumType.STRING).,But before you try to use this mapping, you need to put the right annotations on your entity attribute. ,You can reference the class of your enum type in a @Type annotation on your entity attribute. This is a good approach if you only use the type on one entity attribute.

In this article, I will show you how to use all 3 of these options to map the following Rating enum. It’s used in the Review entity to represent the rating of a book.

public enum Rating {
   ONE,
   TWO,
   THREE,
   FOUR,
   FIVE
}

You don’t need to annotate your entity attribute if you want to store the ordinal value of your enum in the database. Here you can see an example of such a mapping.

@Entity
public class Review {

   @Id
   @GeneratedValue
   private Long id;

   private String message;

   private Rating rating;

   ...
}

If you instead want to persist the String representation of your enum value, you need to annotate your entity attribute with @Enumerated(EnumType.STRING).

@Entity
public class Review {

   @Id
   @GeneratedValue
   private Long id;

   private String message;

   @Enumerated(EnumType.STRING)
   private Rating rating;

   ...
}

If you didn’t annotate your converter with @Converter(autoApply=true), you also need to annotate the entity attribute with @Convert(converter = RatingAttributeConverter.class). This tells Hibernate to use the referenced converter when it reads or writes this entity attribute.

@Entity
public class Review {

   @Id
   @GeneratedValue
   private Long id;

   private String message;

   @Convert(converter = RatingAttributeConverter.class)
   private Rating rating;

   ...
}

Some databases, like PostgreSQL, offer custom data types to store enumerations. These data types are similar to the enum type that we know in Java. They define a set of valid values that can be stored in the database column.

CREATE TYPE rating_enum AS ENUM(
   'ONE', 'TWO', 'THREE', 'FOUR', 'FIVE'
)

Suggestion : 7

Updated date Mar 30, 2022

Go ahead and create an enum as shown in listing 1, This is a enum of flagship phones. 

public enum FlagshipSmartphone {
   [Description("iPhone 13 Pro Max")]
   Apple,
   [Description("Samsung Galaxy Note 20")]
   Samsung,
   [Description("OnePlus 9 Pro")]
   OnePlus,
   [Description("Google Pixel 6 Pro")]
   Google
}

Extension methods are defined as static but are called by using instance method syntax. In the following syntax we have created a static method GetEnumDescription().

public static string GetEnumDescription() {}

The first parameter specifies which type for which we are creating the extension method, in this example that would be "Enum". The parameter is always decorated by "this modifier". 

public static string GetEnumDescription(this Enum enumValue) {}

Now that we are done with all the rules, let's add some logic to this empty body. In following listing all I am doing is fetching the "fieldinfo" of "type enum" in line number 3 and in next line I am searching for the custom attribute which is "type of DescriptionAttribute" if type matched then simply return the description, if "type is not DescriptionAttribute" then simply throw an exception.

public static string GetEnumDescription(this Enum enumValue) {
   var field = enumValue.GetType().GetField(enumValue.ToString());
   if (Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) is DescriptionAttribute attribute) {
      return attribute.Description;
   }
   throw new ArgumentException("Item not found.", nameof(enumValue));
}

All set, let's see the magic. As per our diagram, let's call this extension method in the "class Program", In the following listing 5, we are creating a variable of type "FlagshipSmartphone enum" with value of "Samsung". Then in line number 4 we are actually calling our newly created extension method with the help of (.) dot operator.

using ExtensionMethod;

FlagshipSmartphone Samsung = FlagshipSmartphone.Samsung;
string Description = Samsung.GetEnumDescription();
Console.WriteLine(Description);