Welcome to GDX2py’s documentation!¶
GDX2py is a Python package providing read and write support for GAMS GDX files using built-in data types list and dictionary.
API Reference¶
This page contains auto-generated API reference documentation 1.
gdx2py
¶
Submodules¶
gdx2py.gams
¶
Module contains classes for handling GAMS symbols
Module Contents¶
-
class
gdx2py.gams.
GAMSSet
(keys: Sequence[tuple], domain: Sequence[str] = None, expl_text: str = '', assoc_texts: Sequence[str] = None)¶ Bases:
gdx2py.gams._GAMSNDimSymbol
Class for GAMS Sets
-
elements
¶ Set elements
- Type
list
-
expl_text
¶ Set explanatory text
- Type
str
-
assoc_texts
¶ Set element associated texts
- Type
list
Constructor for GAMSSet
- Parameters
keys – Sequence of tuples of strings for the keys
domain (optional) – Sequence of domain set names, use None for the universal set
expl_text (optional) – Explanatory text
assoc_texts (optional) – Set element associated texts
- Raises
ValueError –
-
property
elements
(self)
-
property
assoc_texts
(self)
-
-
class
gdx2py.gams.
GAMSScalar
(value: float, expl_text: str = '')¶ Bases:
gdx2py.gams._GAMSSymbol
Class for GAMS Scalars (0-dimensional Parameters)
-
expl_text
¶ Symbol explanatory text
- Type
str
Class constructor
- Parameters
name – Symbol name
value – Value
- Raises
ValueError –
-
-
class
gdx2py.gams.
GAMSParameter
(data: Mapping[tuple, float], domain: Sequence[str] = None, expl_text: str = '')¶ Bases:
gdx2py.gams._GAMSNDimSymbol
Class for GAMS Parameters
-
domain
¶ List of set names that make the domain of the symbol
- Type
list
-
expl_text
¶ Symbol explanatory text
- Type
str
-
keys
()¶ Return a view to the symbol’s keys
-
values
()¶ Return a view to the symbol’s values
Constructor for GAMSParameter
- Parameters
data – Dictionay of keys and values
domain (optional) – List of domain set names, use None for the universal set
expl_text (optional) – Explanatory text
- Raises
ValueError –
-
keys
(self)
-
values
(self)
-
to_pandas
(self)¶
-
gdx2py.gdxfile
¶
Module for working with GAMS GDX files
Module Contents¶
-
gdx2py.gdxfile.
GMS_DTYPES
¶
-
gdx2py.gdxfile.
GDX_DTYPE_TEXT
= U254¶
-
gdx2py.gdxfile.
GDX_DTYPE_LABEL
= U63¶
-
gdx2py.gdxfile.
GDX_DTYPE_NUM
= f8¶
-
gdx2py.gdxfile.
EPS_VALUE
¶
-
gdx2py.gdxfile.
SPECIAL_VALUES
¶
-
gdx2py.gdxfile.
GMS_USERINFO_SET_PARAMETER
= 0¶
-
class
gdx2py.gdxfile.
GdxFile
(filename: str, mode: str = 'r', gams_dir: str = None)¶ Bases:
object
Class for working with a gdx file
Use subscripting (gdx_object[‘<symbolname>’]) to get/set a GAMS set or parameter symbol.
-
filename
¶ Absolute filename
- Type
str
Constructor for GdxFile
- Parameters
filename – str
mode – File open mode: ‘r’ for reading, ‘w’ for writing, ‘w+’ for appending (replaces existing symbol)
gams_dir (optional) – Location of GAMS installation directory
- Raises
RuntimeError – Unable to load gdx library, invalid mode
FileNotFoundError – Input file not found
ValueError – Unsupported mode
OSError – Unable to read/write file
Exception – Other errors
-
keys
(self)¶
-
close
(self)¶
-
static
symrange
(sym_count)¶
-
Package Contents¶
-
class
gdx2py.
GdxFile
(filename: str, mode: str = 'r', gams_dir: str = None)¶ Bases:
object
Class for working with a gdx file
Use subscripting (gdx_object[‘<symbolname>’]) to get/set a GAMS set or parameter symbol.
-
filename
¶ Absolute filename
- Type
str
Constructor for GdxFile
- Parameters
filename – str
mode – File open mode: ‘r’ for reading, ‘w’ for writing, ‘w+’ for appending (replaces existing symbol)
gams_dir (optional) – Location of GAMS installation directory
- Raises
RuntimeError – Unable to load gdx library, invalid mode
FileNotFoundError – Input file not found
ValueError – Unsupported mode
OSError – Unable to read/write file
Exception – Other errors
-
keys
(self)¶
-
close
(self)¶
-
static
symrange
(sym_count)¶
-
-
class
gdx2py.
GAMSSet
(keys: Sequence[tuple], domain: Sequence[str] = None, expl_text: str = '', assoc_texts: Sequence[str] = None)¶ Bases:
gdx2py.gams._GAMSNDimSymbol
Class for GAMS Sets
-
elements
¶ Set elements
- Type
list
-
expl_text
¶ Set explanatory text
- Type
str
-
assoc_texts
¶ Set element associated texts
- Type
list
Constructor for GAMSSet
- Parameters
keys – Sequence of tuples of strings for the keys
domain (optional) – Sequence of domain set names, use None for the universal set
expl_text (optional) – Explanatory text
assoc_texts (optional) – Set element associated texts
- Raises
ValueError –
-
property
elements
(self)
-
property
assoc_texts
(self)
-
-
class
gdx2py.
GAMSScalar
(value: float, expl_text: str = '')¶ Bases:
gdx2py.gams._GAMSSymbol
Class for GAMS Scalars (0-dimensional Parameters)
-
expl_text
¶ Symbol explanatory text
- Type
str
Class constructor
- Parameters
name – Symbol name
value – Value
- Raises
ValueError –
-
-
class
gdx2py.
GAMSParameter
(data: Mapping[tuple, float], domain: Sequence[str] = None, expl_text: str = '')¶ Bases:
gdx2py.gams._GAMSNDimSymbol
Class for GAMS Parameters
-
domain
¶ List of set names that make the domain of the symbol
- Type
list
-
expl_text
¶ Symbol explanatory text
- Type
str
-
keys
()¶ Return a view to the symbol’s keys
-
values
()¶ Return a view to the symbol’s values
Constructor for GAMSParameter
- Parameters
data – Dictionay of keys and values
domain (optional) – List of domain set names, use None for the universal set
expl_text (optional) – Explanatory text
- Raises
ValueError –
-
keys
(self)
-
values
(self)
-
to_pandas
(self)¶
-
- 1
Created with sphinx-autoapi
Usage¶
GdxFile
objects behave like dictionaries.
>>> from gdx2py import GdxFile, GAMSSet, GAMSScalar, GAMSParameter
>>> with GdxFile('/path/to/gdx/file.gdx', mode='w') as gdx:
>>> gdx['set1'] = ['a', 'b', 'c'] # Write a simple set
>>> set1 = gdx['set1'] # Read a symbol
>>> # Write a 2-dimensional set
>>> gdx['set2'] = [('a', 'foo'), ('b', 'bar'), ('c', 'baz')]
>>> # Write a scalar with explanatory text
>>> pi = GAMSScalar(3.14, expl_text="Value of pi")
>>> gdx['pi'] = pi
GAMS symbol objects¶
The symbol returned is either a GAMSSet
, GAMSParameter
or
GAMSScalar
. You can convert them to Python built-ins.
>>> float(pi)
3.14
>>> list(set1)
['a', 'b', 'c']
>>> par = GAMSParameter({'a': 1, 'b': 2, 'c': 3, 'd': 4 })
>>> dict(par)
{'a': 1, 'b': 2, 'c': 3, 'd': 4 }
Symbol domains¶
GAMS sets and parameteres can also have a domain. Use the optional domain
parameter to the constructor to define the domain.
>>> cats = GAMSSet(['jaguar', 'lion', 'tiger'], domain=['animals'])
The universal set (‘*’ in GAMS) means there is no specific domain.
Use None
for the universal set.
>>> set2d = GAMSSet([('a', 'foo'), ('b', 'bar'), ('c', 'baz')], domain=['abc', None])