Pass in a generator / mocking function to a method in a legacy module












0















In Python, I have a legacy module Utils with a legacy method, like this:



@staticmethod
def legacy(users, some_data, other_data):
j=0
for _ in users.keys():
do_something([{'a': some_data[j], 'b': other_data[j]}])
j=j+1


I'm writing a new logic where only users and some_data exists, and other_data does not exist.



I'm thinking of passing in a generator / mocking method so that i can do:



Utils.legacy(users, some_data, magic)


which sets {'b': magic[j]} given a specific j.



Is there a way to implement the magic using generator / some mocking?



Notes:




  • the legacy code cannot be changed

  • the above simplied question can be solved by passing in a dict of size of users, but i'm looking for a more general solution.










share|improve this question

























  • First of all, you can't use a generator because they cannot be indexed

    – ForceBru
    Nov 15 '18 at 20:49


















0















In Python, I have a legacy module Utils with a legacy method, like this:



@staticmethod
def legacy(users, some_data, other_data):
j=0
for _ in users.keys():
do_something([{'a': some_data[j], 'b': other_data[j]}])
j=j+1


I'm writing a new logic where only users and some_data exists, and other_data does not exist.



I'm thinking of passing in a generator / mocking method so that i can do:



Utils.legacy(users, some_data, magic)


which sets {'b': magic[j]} given a specific j.



Is there a way to implement the magic using generator / some mocking?



Notes:




  • the legacy code cannot be changed

  • the above simplied question can be solved by passing in a dict of size of users, but i'm looking for a more general solution.










share|improve this question

























  • First of all, you can't use a generator because they cannot be indexed

    – ForceBru
    Nov 15 '18 at 20:49
















0












0








0








In Python, I have a legacy module Utils with a legacy method, like this:



@staticmethod
def legacy(users, some_data, other_data):
j=0
for _ in users.keys():
do_something([{'a': some_data[j], 'b': other_data[j]}])
j=j+1


I'm writing a new logic where only users and some_data exists, and other_data does not exist.



I'm thinking of passing in a generator / mocking method so that i can do:



Utils.legacy(users, some_data, magic)


which sets {'b': magic[j]} given a specific j.



Is there a way to implement the magic using generator / some mocking?



Notes:




  • the legacy code cannot be changed

  • the above simplied question can be solved by passing in a dict of size of users, but i'm looking for a more general solution.










share|improve this question
















In Python, I have a legacy module Utils with a legacy method, like this:



@staticmethod
def legacy(users, some_data, other_data):
j=0
for _ in users.keys():
do_something([{'a': some_data[j], 'b': other_data[j]}])
j=j+1


I'm writing a new logic where only users and some_data exists, and other_data does not exist.



I'm thinking of passing in a generator / mocking method so that i can do:



Utils.legacy(users, some_data, magic)


which sets {'b': magic[j]} given a specific j.



Is there a way to implement the magic using generator / some mocking?



Notes:




  • the legacy code cannot be changed

  • the above simplied question can be solved by passing in a dict of size of users, but i'm looking for a more general solution.







python generator python-2.x legacy-code






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 20:43







Jeff Xiao

















asked Nov 15 '18 at 20:41









Jeff XiaoJeff Xiao

8641719




8641719













  • First of all, you can't use a generator because they cannot be indexed

    – ForceBru
    Nov 15 '18 at 20:49





















  • First of all, you can't use a generator because they cannot be indexed

    – ForceBru
    Nov 15 '18 at 20:49



















First of all, you can't use a generator because they cannot be indexed

– ForceBru
Nov 15 '18 at 20:49







First of all, you can't use a generator because they cannot be indexed

– ForceBru
Nov 15 '18 at 20:49














1 Answer
1






active

oldest

votes


















1














You just need a class with a __getitem__ method that doesn't put any constraints on its argument.



class Foo(object):
def __getitem__(self, key):
pass


Utils.legacy(users, some_data, Foo())


Replace pass with return foo for whatever placeholder element foo you'd like.



Or, on second thought, use collections.defaultdict.



from collections import defaultdict

Utils.legacy(users, some_data, defaultdict(lambda: None))


(Or use any other zero-argument function to provide a default value in place of lambda: None.)






share|improve this answer


























  • fior yet another layer, e.g. 'c':yet_some_data[0][j], a nested solution works: class NestedFoo(): def __getitem__(self, key): return Foo()

    – Jeff Xiao
    Nov 16 '18 at 12:49








  • 1





    You don't even need a separate class. Just return another instance of the same class: class Foo(): def __getitem__(self, key): return Foo(). Then no matter how deeply you try to index it, the resulting value is still indexible :)

    – chepner
    Nov 16 '18 at 13:17











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53327595%2fpass-in-a-generator-mocking-function-to-a-method-in-a-legacy-module%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You just need a class with a __getitem__ method that doesn't put any constraints on its argument.



class Foo(object):
def __getitem__(self, key):
pass


Utils.legacy(users, some_data, Foo())


Replace pass with return foo for whatever placeholder element foo you'd like.



Or, on second thought, use collections.defaultdict.



from collections import defaultdict

Utils.legacy(users, some_data, defaultdict(lambda: None))


(Or use any other zero-argument function to provide a default value in place of lambda: None.)






share|improve this answer


























  • fior yet another layer, e.g. 'c':yet_some_data[0][j], a nested solution works: class NestedFoo(): def __getitem__(self, key): return Foo()

    – Jeff Xiao
    Nov 16 '18 at 12:49








  • 1





    You don't even need a separate class. Just return another instance of the same class: class Foo(): def __getitem__(self, key): return Foo(). Then no matter how deeply you try to index it, the resulting value is still indexible :)

    – chepner
    Nov 16 '18 at 13:17
















1














You just need a class with a __getitem__ method that doesn't put any constraints on its argument.



class Foo(object):
def __getitem__(self, key):
pass


Utils.legacy(users, some_data, Foo())


Replace pass with return foo for whatever placeholder element foo you'd like.



Or, on second thought, use collections.defaultdict.



from collections import defaultdict

Utils.legacy(users, some_data, defaultdict(lambda: None))


(Or use any other zero-argument function to provide a default value in place of lambda: None.)






share|improve this answer


























  • fior yet another layer, e.g. 'c':yet_some_data[0][j], a nested solution works: class NestedFoo(): def __getitem__(self, key): return Foo()

    – Jeff Xiao
    Nov 16 '18 at 12:49








  • 1





    You don't even need a separate class. Just return another instance of the same class: class Foo(): def __getitem__(self, key): return Foo(). Then no matter how deeply you try to index it, the resulting value is still indexible :)

    – chepner
    Nov 16 '18 at 13:17














1












1








1







You just need a class with a __getitem__ method that doesn't put any constraints on its argument.



class Foo(object):
def __getitem__(self, key):
pass


Utils.legacy(users, some_data, Foo())


Replace pass with return foo for whatever placeholder element foo you'd like.



Or, on second thought, use collections.defaultdict.



from collections import defaultdict

Utils.legacy(users, some_data, defaultdict(lambda: None))


(Or use any other zero-argument function to provide a default value in place of lambda: None.)






share|improve this answer















You just need a class with a __getitem__ method that doesn't put any constraints on its argument.



class Foo(object):
def __getitem__(self, key):
pass


Utils.legacy(users, some_data, Foo())


Replace pass with return foo for whatever placeholder element foo you'd like.



Or, on second thought, use collections.defaultdict.



from collections import defaultdict

Utils.legacy(users, some_data, defaultdict(lambda: None))


(Or use any other zero-argument function to provide a default value in place of lambda: None.)







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 20:56

























answered Nov 15 '18 at 20:51









chepnerchepner

257k34247339




257k34247339













  • fior yet another layer, e.g. 'c':yet_some_data[0][j], a nested solution works: class NestedFoo(): def __getitem__(self, key): return Foo()

    – Jeff Xiao
    Nov 16 '18 at 12:49








  • 1





    You don't even need a separate class. Just return another instance of the same class: class Foo(): def __getitem__(self, key): return Foo(). Then no matter how deeply you try to index it, the resulting value is still indexible :)

    – chepner
    Nov 16 '18 at 13:17



















  • fior yet another layer, e.g. 'c':yet_some_data[0][j], a nested solution works: class NestedFoo(): def __getitem__(self, key): return Foo()

    – Jeff Xiao
    Nov 16 '18 at 12:49








  • 1





    You don't even need a separate class. Just return another instance of the same class: class Foo(): def __getitem__(self, key): return Foo(). Then no matter how deeply you try to index it, the resulting value is still indexible :)

    – chepner
    Nov 16 '18 at 13:17

















fior yet another layer, e.g. 'c':yet_some_data[0][j], a nested solution works: class NestedFoo(): def __getitem__(self, key): return Foo()

– Jeff Xiao
Nov 16 '18 at 12:49







fior yet another layer, e.g. 'c':yet_some_data[0][j], a nested solution works: class NestedFoo(): def __getitem__(self, key): return Foo()

– Jeff Xiao
Nov 16 '18 at 12:49






1




1





You don't even need a separate class. Just return another instance of the same class: class Foo(): def __getitem__(self, key): return Foo(). Then no matter how deeply you try to index it, the resulting value is still indexible :)

– chepner
Nov 16 '18 at 13:17





You don't even need a separate class. Just return another instance of the same class: class Foo(): def __getitem__(self, key): return Foo(). Then no matter how deeply you try to index it, the resulting value is still indexible :)

– chepner
Nov 16 '18 at 13:17




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53327595%2fpass-in-a-generator-mocking-function-to-a-method-in-a-legacy-module%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python