Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function to convert address format of cells from one to another
:param addr: address as tuple or label
:param output: -'label' will output label
- 'tuple' will output tuple
- 'flip' will convert to other type
:returns: tuple or label
"""
_MAGIC_NUMBER = 64
if type(addr) == tuple:
if output == 'label' or output == 'flip':
# return self.get_addr_int(*addr)
row = int(addr[0])
col = int(addr[1])
if row < 1 or col < 1:
raise IncorrectCellLabel('(%s, %s)' % (row, col))
div = col
column_label = ''
while div:
(div, mod) = divmod(div, 26)
if mod == 0:
mod = 26
div -= 1
column_label = chr(mod + _MAGIC_NUMBER) + column_label
label = '%s%s' % (column_label, row)
return label
elif output == 'tuple':
return addr
elif type(addr) == str:
if output == 'tuple' or output == 'flip':
if output == 'label' or output == 'flip':
# return self.get_addr_int(*addr)
if addr[0] is None:
row_label = ''
else:
row = int(addr[0])
if row < 1:
raise IncorrectCellLabel(repr(addr))
row_label = str(row)
if addr[1] is None:
column_label = ''
else:
col = int(addr[1])
if col < 1:
raise IncorrectCellLabel(repr(addr))
div = col
column_label = ''
while div:
(div, mod) = divmod(div, 26)
if mod == 0:
mod = 26
div -= 1
column_label = chr(mod + _MAGIC_NUMBER) + column_label
label = '%s%s' % (column_label, row_label)
return label
elif output == 'tuple':
return addr
elif type(addr) == str:
if output == 'tuple' or output == 'flip':
def _label_to_coordinates(self, label):
"""Transforms a label in A1 notation into numeric coordinates and returns them as tuple."""
m = re.match(r'([A-Za-z]*)(\d*)', label)
if m:
column_label = m.group(1).upper()
row, col = m.group(2), 0
if column_label:
for i, c in enumerate(reversed(column_label)):
col += (ord(c) - self._MAGIC_NUMBER) * (26 ** i)
col = int(col)
else:
col = None
row = int(row) if row else None
if not m or (not self.allow_non_single and not (row and col)):
raise IncorrectCellLabel('Not a valid cell label format: {}.'.format(label))
return row, col
return False
addr = [self.row, self.col]
if type(position) == tuple:
addr = (addr[0] + position[0], addr[1] + position[1])
elif type(position) == str:
if "right" in position:
addr[1] += 1
if "left" in position:
addr[1] -= 1
if "top" in position:
addr[0] -= 1
if "bottom" in position:
addr[0] += 1
try:
ncell = self.worksheet.cell(tuple(addr))
except IncorrectCellLabel:
raise CellNotFound
return ncell
return label
elif output == 'tuple':
return addr
elif type(addr) == str:
if output == 'tuple' or output == 'flip':
_cell_addr_re = re.compile(r'([A-Za-z]+)(\d+)')
m = _cell_addr_re.match(addr)
if m:
column_label = m.group(1).upper()
row, col = int(m.group(2)), 0
for i, c in enumerate(reversed(column_label)):
col += (ord(c) - _MAGIC_NUMBER) * (26 ** i)
else:
raise IncorrectCellLabel(addr)
return int(row), int(col)
elif output == 'label':
return addr
else:
raise InvalidArgumentValue
return label
elif output == 'tuple':
return addr
elif type(addr) == str:
if output == 'tuple' or output == 'flip':
_cell_addr_re = re.compile(r'([A-Za-z]+)(\d+)')
m = _cell_addr_re.match(addr)
if m:
column_label = m.group(1).upper()
row, col = int(m.group(2)), 0
for i, c in enumerate(reversed(column_label)):
col += (ord(c) - _MAGIC_NUMBER) * (26 ** i)
else:
raise IncorrectCellLabel(addr)
return int(row), int(col)
elif output == 'label':
return addr
else:
raise InvalidArgumentValue("addr of type " + str(type(addr)))
if isinstance(value, str):
self._value = self._label_to_coordinates(value)
elif isinstance(value, tuple) or isinstance(value, list):
assert len(value) == 2, 'tuple should be of length 2'
assert type(value[0]) is int or value[0] is None, 'address row should be int'
assert type(value[1]) is int or value[1] is None, 'address col should be int'
self._value = tuple(value)
self._validate()
elif not value and self.allow_non_single:
self._value = (None, None)
self._validate()
elif isinstance(value, Address):
self._value = self._label_to_coordinates(value.label)
else:
raise IncorrectCellLabel('Only labels in A1 notation, coordinates as a tuple or '
'pygsheets.Address objects are accepted.')