mirror of
https://github.com/meteoinfo/MeteoInfo.git
synced 2025-12-08 20:36:05 +00:00
update valid variable name function
This commit is contained in:
parent
5cd784ea6c
commit
faf681e417
@ -1,7 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<MeteoInfo File="milconfig.xml" Type="configurefile">
|
||||
<Path OpenPath="D:\Working\MIScript\Jython\mis\io\burf">
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\io\grib"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\io\hdf"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\io\json"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\io\matlab"/>
|
||||
@ -11,24 +10,21 @@
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\io\grads"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\dataframe"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\traj"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\common_math"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\common_math\stats"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\io"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\test"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\io\burf"/>
|
||||
</Path>
|
||||
<File>
|
||||
<OpenedFiles>
|
||||
<OpenedFile File="D:\Working\MIScript\Jython\mis\io\netcdf\sht_coords.py"/>
|
||||
<OpenedFile File="D:\Working\MIScript\Jython\mis\io\burf\add_cma_lookup.py"/>
|
||||
<OpenedFile File="D:\Working\MIScript\Jython\mis\io\burf\bufr_cma_t_upar_1.py"/>
|
||||
<OpenedFile File="D:\Working\MIScript\Jython\mis\io\burf\bufr_gdas_adpsfc.py"/>
|
||||
<OpenedFile File="D:\Working\MIScript\Jython\mis\io\burf\bufr_cma_rsd.py"/>
|
||||
</OpenedFiles>
|
||||
<RecentFiles>
|
||||
<RecentFile File="D:\Working\MIScript\Jython\mis\io\netcdf\sht_coords.py"/>
|
||||
<RecentFile File="D:\Working\MIScript\Jython\mis\io\burf\add_cma_lookup.py"/>
|
||||
<RecentFile File="D:\Working\MIScript\Jython\mis\io\burf\bufr_cma_t_upar_1.py"/>
|
||||
<RecentFile File="D:\Working\MIScript\Jython\mis\io\burf\bufr_gdas_adpsfc.py"/>
|
||||
<RecentFile File="D:\Working\MIScript\Jython\mis\io\burf\bufr_cma_rsd.py"/>
|
||||
</RecentFiles>
|
||||
</File>
|
||||
<Font>
|
||||
@ -36,5 +32,5 @@
|
||||
</Font>
|
||||
<LookFeel DockWindowDecorated="true" LafDecorated="true" Name="FlatDarkLaf"/>
|
||||
<Figure DoubleBuffering="true"/>
|
||||
<Startup MainFormLocation="-6,-6" MainFormSize="1292,764"/>
|
||||
<Startup MainFormLocation="-6,0" MainFormSize="1375,835"/>
|
||||
</MeteoInfo>
|
||||
|
||||
Binary file not shown.
@ -24,6 +24,43 @@ import datetime
|
||||
import numbers
|
||||
import warnings
|
||||
import re
|
||||
import keyword
|
||||
|
||||
|
||||
def to_valid_variable_name(name):
|
||||
"""
|
||||
Rename variable name to a valid python variable name
|
||||
|
||||
Parameters
|
||||
----------
|
||||
name : str
|
||||
Input variable name
|
||||
|
||||
Returns
|
||||
-------
|
||||
str
|
||||
Valid python variable name
|
||||
"""
|
||||
if not name:
|
||||
return "_"
|
||||
|
||||
if bool(re.search(ur'[\u4e00-\u9fff]', name)):
|
||||
return name
|
||||
|
||||
# Replace invalid chars with underscore
|
||||
name = re.sub(r"[^a-zA-Z0-9_]", "_", name)
|
||||
name = re.sub(r"_+", "_", name).strip("_")
|
||||
if not name:
|
||||
return "_"
|
||||
|
||||
if name[0].isdigit():
|
||||
name = "_" + name
|
||||
|
||||
if keyword.iskeyword(name):
|
||||
name += "_"
|
||||
|
||||
return name
|
||||
|
||||
|
||||
# Dimension variable
|
||||
class DimVariable(object):
|
||||
@ -47,7 +84,7 @@ class DimVariable(object):
|
||||
self._coordinate = False
|
||||
if not variable is None:
|
||||
self._real_name = variable.getName()
|
||||
self._name = re.sub('[-_]+', '_', self._real_name)
|
||||
self._name = to_valid_variable_name(self._real_name)
|
||||
self.dtype = np.dtype.fromjava(variable.getDataType())
|
||||
self.dims = variable.getDimensions()
|
||||
self.ndim = variable.getDimNumber()
|
||||
@ -55,7 +92,7 @@ class DimVariable(object):
|
||||
self._coordinate = variable.isDimVar()
|
||||
elif not ncvariable is None:
|
||||
self._real_name = ncvariable.getFullName()
|
||||
self._name = re.sub('[-_]+', '_', self._real_name)
|
||||
self._name = to_valid_variable_name(self._real_name)
|
||||
self.dtype = ncvariable.getDataType()
|
||||
self.dims = ncvariable.getDimensions()
|
||||
self.ndim = len(self.dims)
|
||||
@ -91,7 +128,7 @@ class DimVariable(object):
|
||||
elif self.ncvariable is not None:
|
||||
s_name = self.ncvariable.getShortName()
|
||||
|
||||
s_name = re.sub('[-_]+', '_', s_name)
|
||||
s_name = to_valid_variable_name(s_name)
|
||||
return s_name
|
||||
|
||||
@property
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user