Ad
  • Custom User Avatar

    Glad you enjoyed it. I will just correct you: DateTimeField should not accept string, no string format in tests. In tests you can find only that it can accpet datetime.datetime instance. Also datetime module is imported for you and it's also in description (note that datetime.datetime is not module, datetime is).

  • Custom User Avatar

    No, test is just allright. I don't want to be spoiler, but model looks like that:

    class User(Model):
        # fields...
        foo = "bar"
    

    And that foo have to be same without any change.

  • Custom User Avatar

    Test looks like that:

    test.expect(getattr(User, 'foo'), 'bar', 'Whooho! Be careful - hide only fields')
    

    Dunno why it still raises AttributeError. Anyway you have to change only fields on class. You have to leave another attributes alone. In metaclass you will get whole dict of class - all attributes and all methods. You should manipulate only with fields. If you change something else, it will fail.

  • Custom User Avatar

    Pardon me, I haven't time visit CodeWars for few days.

    Test #4 for DateTimeField was another implementation detail. I removed it.

    Well, you need to be careful before you do something with original code by metaclass. You should add syntactic sugar for fields, nothing else. I improved tests, so now you shold see better message.

  • Custom User Avatar

    That's very disappointing. Thank you anyway! :-)

  • Custom User Avatar

    What regexp module is available for Haskell? Text.Regex doesn't work for me.

  • Custom User Avatar

    By the way some Field should fail with value None and some not. It depends on "setting" of model. If you look at this model:

    class User(Model):
        first_name = CharField(blank=False)
        last_name = CharField(blank=True)
    

    For first name it shouldn't allow value None, but it should for last name. So this piece of code will go without raising any exception:

    user = User(first_name='Liam')
    user.validate()
    
  • Custom User Avatar

    You are right. I made tests for solutions how it's implemented in Django. But someone can have different solutions, so I edited tests so it tests only final solutions as is described and not implementation details.

  • Custom User Avatar

    I was thinking about 1kyu, but this kata uses really basics of Metaclass (in fact just one thing), so I suppose it's 2kyu.

    Try this help: if I say it simple (not very correctly, but most of people can understand to this) - like classes creates instances, metaclasses creates classes. So by metaclass you can change behavior of creating classes which is needed in this kata. You in fact doing some syntactic sugar for your own framework.

    Those tests are saying you don't want mix some Field definition in you final code. When you use your model like user, you want to have just fields with normal values. Like you would write something like this:

    class User(object):
        def __init__(self, first_name=None, last_name=None, age=None, ...):
            self.first_name = first_name
            self.last_name = last_name
            self.age = age
            ...
        
        def validate(self):
            if not self.first_name:
                raise ValidationError('First name is mandatory.')
            if self.age and self.age < 5:
                raise ValidationError('Age is too small.')
            ...
    

    And your model should help you out with not writing this code again and again for every model.

    Tell me what do you think about this description and I will add this to description. :-)

  • Custom User Avatar

    I thank you for feedback as well. :-)

    I adjusted example test cases, but it doesn't work to reset when you already started... be sure that when someone else start this kata, it will be allright. It wouldn't allow me to publish if it wouldn't pass.

  • Custom User Avatar

    In Django clean method does validation and sometimes it can change values. Therefor it's called clean. I used same name, but in the end I used only validation for simplification. I changed name of method to validate so it is understandable even by someone who don't know Django.

  • Custom User Avatar

    In description is example what it should do with list of what you have to implement. I tried to open some test cases. Hope, it's clearer now. If you still don't know, let me know. But of course, it's not easy one. This kata is intended to play with metaclasses and find out how it is helpful for frameworks like Django.