本文整理汇总了Python中matplotlib.cbook.mkdirs函数的典型用法代码示例。如果您正苦于以下问题:Python mkdirs函数的具体用法?Python mkdirs怎么用?Python mkdirs使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了mkdirs函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: fetch_historical_yahoo
def fetch_historical_yahoo(ticker, date1, date2, cachename=None,dividends=False):
"""
Fetch historical data for ticker between date1 and date2. date1 and
date2 are date or datetime instances, or (year, month, day) sequences.
Ex:
fh = fetch_historical_yahoo('^GSPC', (2000, 1, 1), (2001, 12, 31))
cachename is the name of the local file cache. If None, will
default to the md5 hash or the url (which incorporates the ticker
and date range)
set dividends=True to return dividends instead of price data. With
this option set, parse functions will not work
a file handle is returned
"""
ticker = ticker.upper()
if iterable(date1):
d1 = (date1[1]-1, date1[2], date1[0])
else:
d1 = (date1.month-1, date1.day, date1.year)
if iterable(date2):
d2 = (date2[1]-1, date2[2], date2[0])
else:
d2 = (date2.month-1, date2.day, date2.year)
if dividends:
g='v'
verbose.report('Retrieving dividends instead of prices')
else:
g='d'
urlFmt = 'http://table.finance.yahoo.com/table.csv?a=%d&b=%d&c=%d&d=%d&e=%d&f=%d&s=%s&y=0&g=%s&ignore=.csv'
url = urlFmt % (d1[0], d1[1], d1[2],
d2[0], d2[1], d2[2], ticker, g)
if cachename is None:
cachename = os.path.join(cachedir, md5(url).hexdigest())
if os.path.exists(cachename):
fh = open(cachename)
verbose.report('Using cachefile %s for %s'%(cachename, ticker))
else:
mkdirs(cachedir)
urlfh = urlopen(url)
fh = open(cachename, 'wb')
fh.write(urlfh.read())
fh.close()
verbose.report('Saved %s data to cache file %s'%(ticker, cachename))
fh = open(cachename, 'r')
return fh
示例2: _get_configdir
def _get_configdir():
"""
Return the string representing the configuration directory.
Default is HOME/.matplotlib. You can override this with the
MPLCONFIGDIR environment variable. If the default is not
writable, and MPLCONFIGDIR is not set, then
tempfile.gettempdir() is used to provide a directory in
which a matplotlib subdirectory is created as the configuration
directory.
"""
configdir = os.environ.get('MPLCONFIGDIR')
if configdir is not None:
if not os.path.exists(configdir):
os.makedirs(configdir)
if not _is_writable_dir(configdir):
return _create_tmp_config_dir()
return configdir
h = get_home()
p = os.path.join(get_home(), '.matplotlib')
if os.path.exists(p):
if not _is_writable_dir(p):
return _create_tmp_config_dir()
else:
if not _is_writable_dir(h):
return _create_tmp_config_dir()
from matplotlib.cbook import mkdirs
mkdirs(p)
return p
示例3: _get_configdir
def _get_configdir():
"""
Return the string representing the configuration dir.
default is HOME/.matplotlib. you can override this with the
MPLCONFIGDIR environment variable
"""
configdir = os.environ.get('MPLCONFIGDIR')
if configdir is not None:
if not os.path.exists(configdir):
os.makedirs(configdir)
if not _is_writable_dir(configdir):
raise RuntimeError('Could not write to MPLCONFIGDIR="%s"'%configdir)
return configdir
h = get_home()
p = os.path.join(get_home(), '.matplotlib')
if os.path.exists(p):
if not _is_writable_dir(p):
raise RuntimeError("'%s' is not a writable dir; you must set %s/.matplotlib to be a writable dir. You can also set environment variable MPLCONFIGDIR to any writable directory where you want matplotlib data stored "% (h, h))
else:
if not _is_writable_dir(h):
raise RuntimeError("Failed to create %s/.matplotlib; consider setting MPLCONFIGDIR to a writable directory for matplotlib configuration data"%h)
from matplotlib.cbook import mkdirs
mkdirs(p)
return p
示例4: _get_config_or_cache_dir
def _get_config_or_cache_dir(xdg_base):
from matplotlib.cbook import mkdirs
configdir = os.environ.get('MPLCONFIGDIR')
if configdir is not None:
if not os.path.exists(configdir):
from matplotlib.cbook import mkdirs
mkdirs(configdir)
if not _is_writable_dir(configdir):
return _create_tmp_config_dir()
return configdir
p = None
h = get_home()
if h is not None:
p = os.path.join(h, '.matplotlib')
if (sys.platform.startswith('linux') and
xdg_base is not None):
p = os.path.join(xdg_base, 'matplotlib')
if p is not None:
if os.path.exists(p):
if _is_writable_dir(p):
return p
else:
try:
mkdirs(p)
except OSError:
pass
else:
return p
return _create_tmp_config_dir()
示例5: _get_config_or_cache_dir
def _get_config_or_cache_dir(xdg_base):
from matplotlib.cbook import mkdirs
configdir = os.environ.get("MPLCONFIGDIR")
if configdir is not None:
if not os.path.exists(configdir):
from matplotlib.cbook import mkdirs
mkdirs(configdir)
if not _is_writable_dir(configdir):
return _create_tmp_config_dir()
return configdir
h = get_home()
p = os.path.join(h, ".matplotlib")
if sys.platform.startswith("linux") and not os.path.exists(p):
p = os.path.join(xdg_base, "matplotlib")
if os.path.exists(p):
if not _is_writable_dir(p):
return _create_tmp_config_dir()
else:
try:
mkdirs(p)
except OSError:
return _create_tmp_config_dir()
return p
示例6: __init__
def __init__(self):
if self.texcache is None:
raise RuntimeError(
('Cannot create TexManager, as there is no cache directory '
'available'))
mkdirs(self.texcache)
ff = rcParams['font.family']
if len(ff) == 1 and ff[0].lower() in self.font_families:
self.font_family = ff[0].lower()
elif isinstance(ff, six.string_types) and ff.lower() in self.font_families:
self.font_family = ff.lower()
else:
mpl.verbose.report(
'font.family must be one of (%s) when text.usetex is True. '
'serif will be used by default.' %
', '.join(self.font_families),
'helpful')
self.font_family = 'serif'
fontconfig = [self.font_family]
for font_family, font_family_attr in [(ff, ff.replace('-', '_'))
for ff in self.font_families]:
for font in rcParams['font.' + font_family]:
if font.lower() in self.font_info:
setattr(self, font_family_attr,
self.font_info[font.lower()])
if DEBUG:
print('family: %s, font: %s, info: %s' %
(font_family, font,
self.font_info[font.lower()]))
break
else:
if DEBUG:
print('$s font is not compatible with usetex')
else:
mpl.verbose.report('No LaTeX-compatible font found for the '
'%s font family in rcParams. Using '
'default.' % font_family, 'helpful')
setattr(self, font_family_attr, self.font_info[font_family])
fontconfig.append(getattr(self, font_family_attr)[0])
# Add a hash of the latex preamble to self._fontconfig so that the
# correct png is selected for strings rendered with same font and dpi
# even if the latex preamble changes within the session
preamble_bytes = six.text_type(self.get_custom_preamble()).encode('utf-8')
fontconfig.append(md5(preamble_bytes).hexdigest())
self._fontconfig = ''.join(fontconfig)
# The following packages and commands need to be included in the latex
# file's preamble:
cmd = [self.serif[1], self.sans_serif[1], self.monospace[1]]
if self.font_family == 'cursive':
cmd.append(self.cursive[1])
while '\\usepackage{type1cm}' in cmd:
cmd.remove('\\usepackage{type1cm}')
cmd = '\n'.join(cmd)
self._font_preamble = '\n'.join(['\\usepackage{type1cm}', cmd,
'\\usepackage{textcomp}'])
示例7: run
def run(arguments, options, state_machine, lineno):
reference = directives.uri(arguments[0])
basedir, fname = os.path.split(reference)
basename, ext = os.path.splitext(fname)
if ext != '.py':
basename = fname
sourcename = fname
#print 'plotdir', reference, basename, ext
# get the directory of the rst file
rstdir, rstfile = os.path.split(state_machine.document.attributes['source'])
reldir = rstdir[len(setup.confdir)+1:]
relparts = [p for p in os.path.split(reldir) if p.strip()]
nparts = len(relparts)
#print ' rstdir=%s, reldir=%s, relparts=%s, nparts=%d'%(rstdir, reldir, relparts, nparts)
#print 'RUN', rstdir, reldir
outdir = os.path.join(setup.confdir, setup.config.plot_output_dir, basedir)
if not os.path.exists(outdir):
cbook.mkdirs(outdir)
linkdir = ('../' * nparts) + setup.config.plot_output_dir.replace(os.path.sep, '/') + '/' + basedir
#linkdir = os.path.join('..', outdir)
num_figs = makefig(reference, outdir,
is_doctest=('doctest-format' in options))
#print ' reference="%s", basedir="%s", linkdir="%s", outdir="%s"'%(reference, basedir, linkdir, outdir)
if options.has_key('include-source'):
contents = open(reference, 'r').read()
if 'doctest-format' in options:
lines = ['']
else:
lines = ['.. code-block:: python', '']
lines += [' %s'%row.rstrip() for row in contents.split('\n')]
del options['include-source']
else:
lines = []
if 'doctest-format' in options:
del options['doctest-format']
if num_figs > 0:
options = [' :%s: %s' % (key, val) for key, val in
options.items()]
options = "\n".join(options)
for i in range(num_figs):
if num_figs == 1:
outname = basename
else:
outname = "%s_%02d" % (basename, i)
lines.extend((template % locals()).split('\n'))
else:
lines.extend((exception_template % locals()).split('\n'))
if len(lines):
state_machine.insert_input(
lines, state_machine.input_lines.source(0))
return []
示例8: _image_directories
def _image_directories(func):
"""
Compute the baseline and result image directories for testing *func*.
Create the result directory if it doesn't exist.
"""
module_name = func.__module__
if module_name == '__main__':
# FIXME: this won't work for nested packages in matplotlib.tests
warnings.warn('test module run as script. guessing baseline image locations')
script_name = sys.argv[0]
basedir = os.path.abspath(os.path.dirname(script_name))
subdir = os.path.splitext(os.path.split(script_name)[1])[0]
else:
mods = module_name.split('.')
if len(mods) >= 3:
mods.pop(0)
# mods[0] will be the name of the package being tested (in
# most cases "matplotlib") However if this is a
# namespace package pip installed and run via the nose
# multiprocess plugin or as a specific test this may be
# missing. See https://github.com/matplotlib/matplotlib/issues/3314
if mods.pop(0) != 'tests':
warnings.warn(("Module '%s' does not live in a parent module "
"named 'tests'. This is probably ok, but we may not be able "
"to guess the correct subdirectory containing the baseline "
"images. If things go wrong please make sure that there is "
"a parent directory named 'tests' and that it contains a "
"__init__.py file (can be empty).") % module_name)
subdir = os.path.join(*mods)
import imp
def find_dotted_module(module_name, path=None):
"""A version of imp which can handle dots in the module name.
As for imp.find_module(), the return value is a 3-element
tuple (file, pathname, description)."""
res = None
for sub_mod in module_name.split('.'):
try:
res = file, path, _ = imp.find_module(sub_mod, path)
path = [path]
if file is not None:
file.close()
except ImportError:
# assume namespace package
path = sys.modules[sub_mod].__path__
res = None, path, None
return res
mod_file = find_dotted_module(func.__module__)[1]
basedir = os.path.dirname(mod_file)
baseline_dir = os.path.join(basedir, 'baseline_images', subdir)
result_dir = os.path.abspath(os.path.join('result_images', subdir))
if not os.path.exists(result_dir):
cbook.mkdirs(result_dir)
return baseline_dir, result_dir
示例9: __init__
def __init__(self):
if self.texcache is None:
raise RuntimeError(
('Cannot create TexManager, as there is no cache directory '
'available'))
mkdirs(self.texcache)
ff = rcParams['font.family']
if len(ff) == 1 and ff[0].lower() in self.font_families:
self.font_family = ff[0].lower()
elif isinstance(ff, str) and ff.lower() in self.font_families:
self.font_family = ff.lower()
else:
mpl.verbose.report(
'font.family must be one of (%s) when text.usetex is True. '
'serif will be used by default.' %
', '.join(self.font_families),
'helpful')
self.font_family = 'serif'
fontconfig = [self.font_family]
for font_family, font_family_attr in [(ff, ff.replace('-', '_'))
for ff in self.font_families]:
for font in rcParams['font.' + font_family]:
if font.lower() in self.font_info:
setattr(self, font_family_attr,
self.font_info[font.lower()])
if DEBUG:
print('family: %s, font: %s, info: %s' %
(font_family, font,
self.font_info[font.lower()]))
break
else:
if DEBUG:
print('$s font is not compatible with usetex')
else:
mpl.verbose.report('No LaTeX-compatible font found for the '
'%s font family in rcParams. Using '
'default.' % ff, 'helpful')
setattr(self, font_family_attr, self.font_info[font_family])
fontconfig.append(getattr(self, font_family_attr)[0])
self._fontconfig = ''.join(fontconfig)
# The following packages and commands need to be included in the latex
# file's preamble:
cmd = [self.serif[1], self.sans_serif[1], self.monospace[1]]
if self.font_family == 'cursive':
cmd.append(self.cursive[1])
while r'\usepackage{type1cm}' in cmd:
cmd.remove(r'\usepackage{type1cm}')
cmd = '\n'.join(cmd)
self._font_preamble = '\n'.join([r'\usepackage{type1cm}', cmd,
r'\usepackage{textcomp}'])
示例10: __init__
def __init__(self):
if self.texcache is None:
raise RuntimeError(("Cannot create TexManager, as there is no cache directory " "available"))
mkdirs(self.texcache)
ff = rcParams["font.family"]
if len(ff) == 1 and ff[0].lower() in self.font_families:
self.font_family = ff[0].lower()
elif isinstance(ff, six.string_types) and ff.lower() in self.font_families:
self.font_family = ff.lower()
else:
mpl.verbose.report(
"font.family must be one of (%s) when text.usetex is True. "
"serif will be used by default." % ", ".join(self.font_families),
"helpful",
)
self.font_family = "serif"
fontconfig = [self.font_family]
for font_family, font_family_attr in [(ff, ff.replace("-", "_")) for ff in self.font_families]:
for font in rcParams["font." + font_family]:
if font.lower() in self.font_info:
setattr(self, font_family_attr, self.font_info[font.lower()])
if DEBUG:
print("family: %s, font: %s, info: %s" % (font_family, font, self.font_info[font.lower()]))
break
else:
if DEBUG:
print("$s font is not compatible with usetex")
else:
mpl.verbose.report(
"No LaTeX-compatible font found for the " "%s font family in rcParams. Using " "default." % ff,
"helpful",
)
setattr(self, font_family_attr, self.font_info[font_family])
fontconfig.append(getattr(self, font_family_attr)[0])
# Add a hash of the latex preamble to self._fontconfig so that the
# correct png is selected for strings rendered with same font and dpi
# even if the latex preamble changes within the session
preamble_bytes = six.text_type(self.get_custom_preamble()).encode("utf-8")
fontconfig.append(md5(preamble_bytes).hexdigest())
self._fontconfig = "".join(fontconfig)
# The following packages and commands need to be included in the latex
# file's preamble:
cmd = [self.serif[1], self.sans_serif[1], self.monospace[1]]
if self.font_family == "cursive":
cmd.append(self.cursive[1])
while "\\usepackage{type1cm}" in cmd:
cmd.remove("\\usepackage{type1cm}")
cmd = "\n".join(cmd)
self._font_preamble = "\n".join(["\\usepackage{type1cm}", cmd, "\\usepackage{textcomp}"])
示例11: _image_directories
def _image_directories():
"""
Compute the baseline and result image directories for testing *func*.
Create the result directory if it doesn't exist.
"""
# module_name = __init__.__module__
# mods = module_name.split('.')
# basedir = os.path.join(*mods)
result_dir = os.path.join(basedir, "testresult", ".")
baseline_dir = os.path.join(basedir, "baseline", ".")
if not os.path.exists(result_dir):
cbook.mkdirs(result_dir)
return baseline_dir, result_dir
示例12: get_cache_dir
def get_cache_dir():
cachedir = _get_cachedir()
if cachedir is None:
raise RuntimeError('Could not find a suitable configuration directory')
cache_dir = os.path.join(cachedir, 'test_cache')
if not os.path.exists(cache_dir):
try:
cbook.mkdirs(cache_dir)
except IOError:
return None
if not os.access(cache_dir, os.W_OK):
return None
return cache_dir
示例13: __init__
def __init__(self):
if self.texcache is None:
raise RuntimeError('Cannot create TexManager, as there is no '
'cache directory available')
mkdirs(self.texcache)
ff = rcParams['font.family']
if len(ff) == 1 and ff[0].lower() in self.font_families:
self.font_family = ff[0].lower()
elif (isinstance(ff, six.string_types)
and ff.lower() in self.font_families):
self.font_family = ff.lower()
else:
_log.info('font.family must be one of (%s) when text.usetex is '
'True. serif will be used by default.',
', '.join(self.font_families))
self.font_family = 'serif'
fontconfig = [self.font_family]
for font_family in self.font_families:
font_family_attr = font_family.replace('-', '_')
for font in rcParams['font.' + font_family]:
if font.lower() in self.font_info:
setattr(self, font_family_attr,
self.font_info[font.lower()])
_log.debug('family: %s, font: %s, info: %s',
font_family, font, self.font_info[font.lower()])
break
else:
_log.debug('%s font is not compatible with usetex.',
font_family)
else:
_log.info('No LaTeX-compatible font found for the %s font '
'family in rcParams. Using default.', font_family)
setattr(self, font_family_attr, self.font_info[font_family])
fontconfig.append(getattr(self, font_family_attr)[0])
# Add a hash of the latex preamble to self._fontconfig so that the
# correct png is selected for strings rendered with same font and dpi
# even if the latex preamble changes within the session
preamble_bytes = self.get_custom_preamble().encode('utf-8')
fontconfig.append(md5(preamble_bytes).hexdigest())
self._fontconfig = ''.join(fontconfig)
# The following packages and commands need to be included in the latex
# file's preamble:
cmd = [self.serif[1], self.sans_serif[1], self.monospace[1]]
if self.font_family == 'cursive':
cmd.append(self.cursive[1])
self._font_preamble = '\n'.join(
[r'\usepackage{type1cm}'] + cmd + [r'\usepackage{textcomp}'])
示例14: _image_directories
def _image_directories(func):
"""
Compute the baseline and result image directories for testing *func*.
Create the result directory if it doesn't exist.
"""
module_name = func.__module__
if module_name == '__main__':
# FIXME: this won't work for nested packages in matplotlib.tests
warnings.warn('test module run as script. guessing baseline image locations')
script_name = sys.argv[0]
basedir = os.path.abspath(os.path.dirname(script_name))
subdir = os.path.splitext(os.path.split(script_name)[1])[0]
else:
mods = module_name.split('.')
if len(mods) >= 3:
mods.pop(0)
# mods[0] will be the name of the package being tested (in
# most cases "matplotlib") However if this is a
# namespace package pip installed and run via the nose
# multiprocess plugin or as a specific test this may be
# missing. See https://github.com/matplotlib/matplotlib/issues/3314
assert mods.pop(0) == 'tests'
subdir = os.path.join(*mods)
import imp
def find_dotted_module(module_name, path=None):
"""A version of imp which can handle dots in the module name"""
res = None
for sub_mod in module_name.split('.'):
try:
res = file, path, _ = imp.find_module(sub_mod, path)
path = [path]
if file is not None:
file.close()
except ImportError:
# assume namespace package
path = sys.modules[sub_mod].__path__
res = None, path, None
return res
mod_file = find_dotted_module(func.__module__)[1]
basedir = os.path.dirname(mod_file)
baseline_dir = os.path.join(basedir, 'baseline_images', subdir)
result_dir = os.path.abspath(os.path.join('result_images', subdir))
if not os.path.exists(result_dir):
cbook.mkdirs(result_dir)
return baseline_dir, result_dir
示例15: make_test_image_filenames
def make_test_image_filenames(name, test_file):
"""
Create filenames for testing
Parameters
----------
name : str
An identifier for the specific test. This will make-up
part of the filenames.
test_file : str
Full path of the test file. This will determine the
directory structure
Returns
-------
out : Bunch
Object with 3 attributes to store the generated filenames
- result
- baseline
- expected
`result`, is the filename for the image generated by the test.
`baseline`, is the filename for the baseline image to which
the result will be compared.
`expected`, is the filename to the copy of the baseline that
will be stored in the same directory as the result image.
Creating a copy make comparison easier.
"""
if '.png' not in name:
name = name + '.png'
basedir = os.path.abspath(os.path.dirname(test_file))
basename = os.path.basename(test_file)
subdir = os.path.splitext(basename)[0]
baseline_dir = os.path.join(basedir, 'baseline_images', subdir)
result_dir = os.path.abspath(os.path.join('result_images', subdir))
if not os.path.exists(result_dir):
cbook.mkdirs(result_dir)
base, ext = os.path.splitext(name)
expected_name = '{}-{}{}'.format(base, 'expected', ext)
filenames = cbook.Bunch(
baseline=os.path.join(baseline_dir, name),
result=os.path.join(result_dir, name),
expected=os.path.join(result_dir, expected_name))
return filenames