Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add queryhelp property to QueryBuilder #3524

Merged
merged 3 commits into from
Nov 9, 2019

Conversation

ltalirz
Copy link
Member

@ltalirz ltalirz commented Nov 7, 2019

fix #3522

  • add queryhelp property to QueryBuilder
    (currently pipes through to get_json_compatible_queryhelp)
  • add deepcopy function to allow direct copies of QueryBuilder
    instances
  • test that QueryBuilder can be re-created from queryhelp
  • remove test_query.py and merge with orm/test_querybuilder.py

@ltalirz
Copy link
Member Author

ltalirz commented Nov 7, 2019

@ltalirz ltalirz force-pushed the issue_3522_copy_qb branch from 65e25ae to 4211067 Compare November 7, 2019 21:27
 * add `queryhelp` property to QueryBuilder
   (currently pipes through to get_json_compatible_queryhelp)
 * add __deepcopy__ function to allow direct copies of QueryBuilder
   instances
 * test that QueryBuilder can be re-created from queryhelp
@ltalirz ltalirz force-pushed the issue_3522_copy_qb branch from 4211067 to 5a1f18b Compare November 7, 2019 23:11
"""
return self.get_json_compatible_queryhelp()

def __deepcopy__(self, memodict={}):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

signature should be def __deepcopy__(self, memo)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed!

return self.get_json_compatible_queryhelp()

def __deepcopy__(self, memodict={}):
qb = type(self)(**self.queryhelp)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for intermediate variable assignment

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had it in here as a bit of a help that what is being returned here is actually a querybuilder.
debatable though whether this is helpful - I'll get rid of it

"""
return self.get_json_compatible_queryhelp()

def __deepcopy__(self, memodict={}):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why only __deepcopy__ implementation. Wouldn't a normal copy also not work? Since the queryhelp won't contain any references to other objects a shallow copy would also just work when implemented like this no?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I chose this because the implementation of the queryhelp uses deepcopy.

return copy.deepcopy({
'path': self._path,
'filters': self._filters,
'project': self._projections,
'order_by': self._order_by,
'limit': self._limit,
'offset': self._offset,
})

And the deepcopy is actually necessary since it contains nested dictionaries:

In [1]: qb=QueryBuilder()

In [2]: qb.append(Data)
Out[2]: <aiida.orm.querybuilder.QueryBuilder at 0x11ea02208>

In [3]: qb.queryhelp
Out[3]:
{'path': [{'entity_type': 'data.Data.',
   'tag': 'Data_1',
   'joining_keyword': None,
   'joining_value': None,
   'outerjoin': False,
   'edge_tag': None}],
 'filters': {'Data_1': {'node_type': {'like': 'data.%'}}},
 'project': {'Data_1': []},
 'order_by': {},
 'limit': None,
 'offset': None}

@sphuber
Copy link
Contributor

sphuber commented Nov 8, 2019

Thanks @ltalirz just some minor comments

@sphuber sphuber merged commit 1882b0b into aiidateam:develop Nov 9, 2019
@sphuber
Copy link
Contributor

sphuber commented Nov 9, 2019

Thanks leo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

how to copy a querybuilder instance?
2 participants