Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have an class definition:

class ScoreDetail(Document):
    location            = StringField(help_text = "province")
    school              = StringField(help_text = "school")
    spec                = StringField(help_text = "major")
    bz                  = IntField(help_text = "1: Bachelor, 2: Academy, 0: exception")
    wl                  = IntField(help_text = "1: w, 2: l, 0: exception")
    batch               = StringField(help_text = "batch")
    score               = IntField(help_text = "score")
    average_score       = FloatField(help_text = "average score")
    average_score_rank  = FloatField(help_text = "rank of average score")
    low_score           = IntField(help_text = "the lowest score")
    low_score_rank      = IntField(help_text = "rank of the lowest score")
    high_score          = IntField(help_text = "highest score")
    high_score_rank     = IntField(help_text = "rank of the highest score")
    score_number        = IntField(help_text = "people in this score")
    spec_number         = IntField(help_text = "people in the major")
    rank                = IntField(help_text = "rank")
    year                = IntField('year')

Now I have a dict named item, like this:

{
  "school": u"Peking University",
  "average_score": u"255",
  "low_score_rank": u"195466",
  "wl": u"w",
  "batch": u"4",
  "score_number": u"1",
  "average_score_rank": u"178199",
  "low_score": u"150",
  "high_score": u"464",
  "score": u"150",
  "spec_number": u"62",
  "rank": u"195466",
  "year": u"14",
  "high_score_rank": u"60631",
  "spec": u"E-commerce",
  "bz": u"z",
  "location": u"US"
}

I want construct a ScoreDetail object from this dict, so I do this:

wl_map = {'w': 1, 'l': 2}
bz_map = {'b': 1, 'z': 2}
item['wl'] = wl_map[item['wl']]
item['year'] = '20' + item['year']
item['bz'] = bz_map[item['bz']]

s = ScoreDetail(
        school=item['school'],
        average_score=item['average_score'],
        low_score_rank=item['low_score_rank'],
        wl=item['wl'],
        batch=item['batch'],
        score_number=item['score_number'],
        average_score_rank=item['average_score_rank'],
        low_score=item['low_score'],
        high_score=item['high_score'],
        score=item['score'],
        spec_number=item['spec_number'],
        rank=item['rank'],
        year=item['year'],
        high_score_rank=item['high_score_rank'],
        spec=item['spec'],
        bz=item['bz'],
        location=item['location']
        )

Now I want to know these two things:

  1. Can I simplify this code?
  2. As you can see, this is a mongoengine schema. It inherits Document, so it can make '1' to int, '150' to float as default, but if I were not to use MongoEngine, this is just a common class. How can I make some type conversion as default?
share|improve this question

1 Answer 1

If all attributes are in item, then you can create an instance simply with:

s = ScoreDetail(**item)

This is just a guess though. Your code looks like Django, where this would work, but even though this isn't Django, I suspect this to be supported similarly.

Without a friendly framework to convert between objects and json, you could use the serialization/deserialization features of the json package, with custom serializer/deserializer implementations.

share|improve this answer
    
can I implement a simple serialization/deserialization tool? –  roger Jun 9 at 7:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.