question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

List comprehensions produce different types

See original GitHub issue

Hi @northwestwitch ! I found a strange thing in scout/server/blueprints/cases/controllers.py.

The function looks like this (cropped)

def case(store, institute_obj, case_obj):
    """Preprocess a single case."""
    case_obj['individual_ids'] = []
    for individual in case_obj['individuals']:
        try:
            sex = int(individual.get('sex', 0))
        except ValueError as err:
            sex = 0
        individual['sex_human'] = SEX_MAP[sex]
        individual['phenotype_human'] = PHENOTYPE_MAP.get(individual['phenotype'])
        case_obj['individual_ids'].append(individual['individual_id'])

    case_obj['assignees'] = [store.user(user_email) for user_email in
                             case_obj.get('assignees', [])]
    suspects = [store.variant(variant_id) or variant_id for variant_id in
                case_obj.get('suspects', [])]
    causatives = [store.variant(variant_id) or variant_id for variant_id in
                  case_obj.get('causatives', [])]

When creating suspects and causatives above it says

store.variant(variant_id) or variant_id

This means that we either get a list with variants (dictionaries) or variant_ids (strings) or a mix of those. The problem arises later in the code when the result is sent to the function variants_filter_by_field in scout/server/blueprints/variants/controllers.py. That one looks like:

def variants_filter_by_field(store, variants, field, case_obj, institute_obj):
    """Given a list of variant objects return only those that have a key specified
      ....
    """
    filtered_variants = []
    # Check if the variants have information if "field"
    for var in variants:
        if var.get(field):
            # Add more details to the variant
            if var['category'] == 'snv':
                var_object = variant(store, institute_obj, case_obj, var['_id'])
            else:
                var_object = sv_variant(store, institute_obj['_id'], case_obj['display_name'], var['_id'])

            filtered_variants.append(var_object['variant'])

    return filtered_variants

It loops over variants and expects them to be dictionaries since it does .get. This fails when the objects are strings.

Could you fix?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
northwestwitchcommented, Jun 13, 2018

I’ll try to fix it today!

0reactions
northwestwitchcommented, Jun 13, 2018

Fixed!

Read more comments on GitHub >

github_iconTop Results From Across the Web

When to Use a List Comprehension in Python
Python list comprehensions make it easy to create lists while performing sophisticated filtering, mapping, and conditional logic on their members.
Read more >
List Comprehensions in Python (With Examples and ... - Datagy
Lists are heterogeneous, meaning they can have a combination of different data types, such as Booleans, strings, integers, etc.
Read more >
List Comprehensions Python | Know 3 Components of List ...
Some of them are lists, sets, dictionaries, etc. There are three different types of components for the list comprehension functions in python, the...
Read more >
List Comprehensions in Python and Generator Expressions
The difference between list comprehensions and generator expressions ... Lists take all possible types of data and combinations of data as their components:....
Read more >
Python List Comprehension (With Examples) - Programiz
List comprehensions aren't the only way to work on lists. Various built-in functions and lambda functions can create and modify lists in less...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found