Skip to main content
deleted 441 characters in body
Source Link
SylvainD
  • 29.3k
  • 1
  • 47
  • 93


 

From here, it seems like I am wrong, to be updated asap.

Is this as simple as it can be ? Not quite ? The check for d.get(my_key) is actually to distinguish "test3" from the other values. It could be easier to just do the sameobvious thing for all values and then overrideinstead of trying to retrieve stuff from the "test3" value.dict :

def my_function(my_string, my_key):
    d = {'test' + str(i): {} for i in range(3)}
 
    if my_string:
        d[my_key]d['test3'] = {my_string'a_key': None'a_value'}
    elseif my_string:
        d[my_key] = {'NULL'my_string: None}
    d['test3'] = {'a_key': 'a_value'}
    return d

Now, you have 2 branches looking pretty similar, let's factorize this out.

def my_function(my_string,elif my_key):
    d != {'test' + str(i)'test3': {} 
 for i in range(3)}
    d[my_key] = {my_string if my_string else 'NULL': None}
    d['test3'] = {'a_key': 'a_value'}
    return d

Is this as good as it can be ? I think so but I might be wrong.



 

From here, it seems like I am wrong, to be updated asap.

Is this as simple as it can be ? Not quite ? The check for d.get(my_key) is actually to distinguish "test3" from the other values. It could be easier to just do the same thing for all values and then override the "test3" value.

def my_function(my_string, my_key):
    d = {'test' + str(i): {} for i in range(3)}
 
    if my_string:
        d[my_key] = {my_string: None}
    else:
        d[my_key] = {'NULL': None}
    d['test3'] = {'a_key': 'a_value'}
    return d

Now, you have 2 branches looking pretty similar, let's factorize this out.

def my_function(my_string, my_key):
    d = {'test' + str(i): {} for i in range(3)}
    d[my_key] = {my_string if my_string else 'NULL': None}
    d['test3'] = {'a_key': 'a_value'}
    return d

Is this as good as it can be ? I think so but I might be wrong.

Is this as simple as it can be ? Not quite ? The check for d.get(my_key) is actually to distinguish "test3" from the other values. It could be easier to just do the obvious thing instead of trying to retrieve stuff from the dict :

def my_function(my_string, my_key):
    d = {'test' + str(i): {} for i in range(3)}
    d['test3'] = {'a_key': 'a_value'}
    if my_string:
        d[my_key] = {my_string: None}
    elif my_key != 'test3': 
        d[my_key] = {'NULL': None}
    return d
added 299 characters in body
Source Link
SylvainD
  • 29.3k
  • 1
  • 47
  • 93
# my_string not blank, my_key is new
assert my_function('hello', 'test4') == {'test2': {}, 'test3': {'a_key': 'a_value'}, 'test0': {}, 'test1': {}, 'test4': {'hello': None}}

# my_string not blank, my_key exists
assert my_function('hello', 'test0') == {'test2': {}, 'test3': {'a_key': 'a_value'}, 'test0': {'hello': None}, 'test1': {}}

# my_string blank, my_key exists and its value is not empty
assert my_function('', 'test3') == {'test2': {}, 'test3': {'a_key': 'a_value'}, 'test0': {}, 'test1': {}}

# my_string blank, my_key exists and its value is empty
assert my_function('', 'test0') == {'test2': {}, 'test3': {'a_key': 'a_value'}, 'test0': {'NULL': None}, 'test1': {}}

# my_string blank, my_key is new
assert my_function('', 'test4') == {'test2': {}, 'test3': {'a_key': 'a_value'}, 'test0': {}, 'test1': {}, 'test4': {'NULL': None}} 

# New test from Sjoerd Job Postmus's comment
assert my_function('something not empty', 'test3') == {'test1': {}, 'test0': {}, 'test3': {'something not empty': None}, 'test2': {}}



From here, it seems like I am wrong, to be updated asap.

# my_string not blank, my_key is new
assert my_function('hello', 'test4') == {'test2': {}, 'test3': {'a_key': 'a_value'}, 'test0': {}, 'test1': {}, 'test4': {'hello': None}}

# my_string not blank, my_key exists
assert my_function('hello', 'test0') == {'test2': {}, 'test3': {'a_key': 'a_value'}, 'test0': {'hello': None}, 'test1': {}}

# my_string blank, my_key exists and its value is not empty
assert my_function('', 'test3') == {'test2': {}, 'test3': {'a_key': 'a_value'}, 'test0': {}, 'test1': {}}

# my_string blank, my_key exists and its value is empty
assert my_function('', 'test0') == {'test2': {}, 'test3': {'a_key': 'a_value'}, 'test0': {'NULL': None}, 'test1': {}}

# my_string blank, my_key is new
assert my_function('', 'test4') == {'test2': {}, 'test3': {'a_key': 'a_value'}, 'test0': {}, 'test1': {}, 'test4': {'NULL': None}}
# my_string not blank, my_key is new
assert my_function('hello', 'test4') == {'test2': {}, 'test3': {'a_key': 'a_value'}, 'test0': {}, 'test1': {}, 'test4': {'hello': None}}

# my_string not blank, my_key exists
assert my_function('hello', 'test0') == {'test2': {}, 'test3': {'a_key': 'a_value'}, 'test0': {'hello': None}, 'test1': {}}

# my_string blank, my_key exists and its value is not empty
assert my_function('', 'test3') == {'test2': {}, 'test3': {'a_key': 'a_value'}, 'test0': {}, 'test1': {}}

# my_string blank, my_key exists and its value is empty
assert my_function('', 'test0') == {'test2': {}, 'test3': {'a_key': 'a_value'}, 'test0': {'NULL': None}, 'test1': {}}

# my_string blank, my_key is new
assert my_function('', 'test4') == {'test2': {}, 'test3': {'a_key': 'a_value'}, 'test0': {}, 'test1': {}, 'test4': {'NULL': None}} 

# New test from Sjoerd Job Postmus's comment
assert my_function('something not empty', 'test3') == {'test1': {}, 'test0': {}, 'test3': {'something not empty': None}, 'test2': {}}



From here, it seems like I am wrong, to be updated asap.

added 1 character in body
Source Link
SylvainD
  • 29.3k
  • 1
  • 47
  • 93

Is this as good as it can be. ? I think so but I might be wrong.

Is this as good as it can be. I think so but I might be wrong.

Is this as good as it can be ? I think so but I might be wrong.

added 426 characters in body
Source Link
SylvainD
  • 29.3k
  • 1
  • 47
  • 93
Loading
added 257 characters in body
Source Link
SylvainD
  • 29.3k
  • 1
  • 47
  • 93
Loading
Source Link
SylvainD
  • 29.3k
  • 1
  • 47
  • 93
Loading