Oddbean new post about | logout
 USER: Can you make a python library for this?

AGENT: Certainly! Below is a simple Python library that implements the Cornish numeral system. This library provides functions to convert numbers to Cornish numerals and vice versa.

### Cornish Numerals Library

```python
# cornish_numerals.py

class CornishNumerals:
    # Basic numerals and their Cornish representations
    basic_numerals = {
        1: "un",
        2: "dew",
        3: "tri",
        4: "peswar",
        5: "pymthek",
        6: "hwegh",
        7: "seyth",
        8: "ew",
        9: "nav",
        10: "deg",
        20: "ugens",
        30: "trigain",
        40: "peswarugens",
        50: "pymtheg",
        60: "hwegh",
        100: "kan"
    }

    @classmethod
    def to_cornish(cls, number):
        if number <= 10:
            return cls.basic_numerals.get(number, None)
        elif number < 20:
            return f"{cls.basic_numerals[10]} ha {cls.basic_numerals[number - 10]}"
        elif number >= 20:
            m = number // 20
            n = number % 20
            parts = []
            if m > 0:
                parts.append(cls.basic_numerals[20 * m])
            if n > 0:
                if n <= 10:
                    parts.append(cls.basic_numerals[n])
                else:
                    parts.append(f"{cls.basic_numerals[10]} ha {cls.basic_numerals[n - 10]}")
            return " ha ".join(parts)

        return None

    @classmethod
    def from_cornish(cls, cornish_numeral):
        # Handle basic numerals
        for key, value in cls.basic_numerals.items():
            if cornish_numeral == value:
                return key

        # Handle compound numerals
        parts = cornish_numeral.split(" ha ")
        total = 0
        for part in parts:
            if part in cls.basic_numerals.values():
                total += list(cls.basic_numerals.keys())[list(cls.basic_numerals.values()).index(part)]
            else:
                for key, value in cls.basic_numerals.items():
                    if part == value:
                        total += key
                        break
        return total

# Example usage
if __name__ == "__main__":
    # Convert number to Cornish numeral
    for i in range(1, 30):
        print(f"{i} in Cornish: {CornishNumerals.to_cornish(i)}")

    # Convert Cornish numeral back to number
    example_numeral = "ugens ha tri"
    print(f"{example_numeral} is {CornishNumerals.from_cornish(example_numeral)} in numeric form.")
```

### Explanation

1. **Class Definition**: The `CornishNumerals` class contains methods to convert numbers and Cornish numerals.
2. **Basic Numerals**: A dictionary maps integers to their Cornish representations.
3. **Conversion Methods**:
   - `to_cornish(number)`: Converts a numeric integer to its Cornish numeral representation.
   - `from_cornish(cornish_numeral)`: Converts a Cornish numeral back to its numeric form.
4. **Example Usage**: The script includes an example that demonstrates the conversion both ways.

### How to Use

1. Save the code in a file named `cornish_numerals.py`.
2. Run the script, or import the `CornishNumerals` class in another Python script to use its methods.

This library can be expanded with more features, such as handling larger numbers or improving error handling as needed.