Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
a1c435d
created function to set user config
roshanrajeev Mar 2, 2021
c5b09f2
Merge remote-tracking branch 'upstream/master'
roshanrajeev Mar 2, 2021
7f97db2
Merge branch 'master' into master
roshanrajeev Mar 2, 2021
a4bd675
Merge remote-tracking branch 'upstream/master'
roshanrajeev Mar 7, 2021
491b018
Added file kwarg and config validation
roshanrajeev Mar 7, 2021
98e398b
Added release notes
roshanrajeev Mar 7, 2021
d68baf9
Fixed linting errors
roshanrajeev Mar 8, 2021
e3c7863
Fixed Linting Issues
roshanrajeev Mar 8, 2021
c9f7138
retrigger checks
roshanrajeev Mar 8, 2021
c538796
Update releasenotes/notes/set_config-c96a56d1b860386c.yaml
1ucian0 Mar 8, 2021
dab9c84
Merge remote-tracking branch 'upstream/master'
roshanrajeev Mar 8, 2021
9b81078
Improved try/catch in set_config
roshanrajeev Mar 8, 2021
9604a22
Added tests for set_config
roshanrajeev Mar 9, 2021
021a5dc
Added mock env to tests: set_config
roshanrajeev Mar 9, 2021
bf80f3f
Removed tests for invalid config
roshanrajeev Mar 9, 2021
9a973a6
Linting and Comments
roshanrajeev Mar 9, 2021
4606836
Modified docstring
roshanrajeev Mar 9, 2021
fa4a210
Merge remote-tracking branch 'upstream/master'
roshanrajeev Mar 9, 2021
b41a6f0
Delete exp_value.cpp
roshanrajeev Mar 9, 2021
73cac9c
Merge branch 'master' into master
1ucian0 Mar 15, 2021
b561570
Add validation for all config files
roshanrajeev Mar 16, 2021
df16d94
Merge remote-tracking branch 'upstream/master'
roshanrajeev Mar 16, 2021
94bad76
Modified tests
roshanrajeev Mar 16, 2021
3c44909
Merge branch 'master' of github.com:roshanrajeev/qiskit-terra
roshanrajeev Mar 16, 2021
c86d72d
Merge branch 'master' into master
roshanrajeev Mar 30, 2021
912d60a
Merge branch 'main' into master
1ucian0 May 18, 2021
64cf239
Merge branch 'main' into master
May 21, 2021
46c0d20
Linting
roshanrajeev May 26, 2021
5406a82
Merge branch 'main' of github.com:Qiskit/qiskit-terra
roshanrajeev May 26, 2021
04a840a
Merge branch 'main' into master
May 27, 2021
6e3dbc7
Merge branch 'main' into master
mergify[bot] May 28, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions qiskit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
# Please note these are global instances, not modules.
from qiskit.providers.basicaer import BasicAer

# function to set user configuration
from qiskit.user_config import set_config
Comment thread
1ucian0 marked this conversation as resolved.
Outdated

_config = _user_config.get_config()

# Moved to after IBMQ and Aer imports due to import issues
Expand Down
33 changes: 33 additions & 0 deletions qiskit/user_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,39 @@ def read_config_file(self):
self.settings['num_processes'] = num_processes


def set_config(key, value, section=None):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would also add an optional filename kwarg here too, just in case a user wants to write a new config file outside of what's configured.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does setting QISKIT_SETTINGS cover this use-case?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I was thinking more if you want to create a new file separate the one currently loaded. QISKIT_SETTINGS only controls where the path to the file currently being used. So you could use this to adjust the path, but if you wanted to write to a different path from the file currently loaded this wouldn't work unless you did something like:

path_backup = os.get_environ('QISKIT_SETTINGS')
os.environ['QISKIT_SETTINGS'] = '/new/path'
qiskit.user_config.set_config('circuit_drawer', 'latex')
os.environ['QISKIT_SETTINGS'] = path_backup

which seems a bit clunky.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@mtreinish Is the idea to create the function set_config with filename kwarg to be able to create multiple config files and use QISKIT_SETTINGS as a switch between config files?

"""Adds or modifies a user configuration

It will add the configuration in either the default location
~/.qiskit/settings.conf or if set the value of the `QISKIT_SETTINGS` env var.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should add a note here that any changes to the existing config file will not be reflected in the current session since the config file is parsed at import time.

Args:
key (str): name of the config
value (str): value of the config
section (str, optional): if not specified, adds it to the
`default` section of the config file.
"""
file_name = os.getenv('QISKIT_SETTINGS', DEFAULT_FILENAME)
sec_name = section if section is not None else 'default'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
sec_name = section if section is not None else 'default'
sec_name = 'default' if section is None else section


assert isinstance(key, str), 'Key must be string type'
assert isinstance(value, str), 'Value must be string type'
Comment thread
1ucian0 marked this conversation as resolved.
Outdated

config = configparser.ConfigParser()
config.read(file_name)

if sec_name not in config.sections():
config.add_section(sec_name)

config.set(sec_name, key, value)

with open(file_name, 'w') as cfgfile:
config.write(cfgfile)
Comment thread
1ucian0 marked this conversation as resolved.
Outdated

user_config = UserConfig(file_name)
user_config.read_config_file()


def get_config():
"""Read the config file from the default location or env var

Expand Down