Python io.open() Examples
The following are 30
code examples of io.open().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
io
, or try the search function
.
Example #1
Source File: ipynb2md.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 8 votes |
def main(): parser = argparse.ArgumentParser( description="Jupyter Notebooks to markdown" ) parser.add_argument("notebook", nargs=1, help="The notebook to be converted.") parser.add_argument("-o", "--output", help="output markdown file") args = parser.parse_args() old_ipynb = args.notebook[0] new_ipynb = 'tmp.ipynb' md_file = args.output print(md_file) if not md_file: md_file = os.path.splitext(old_ipynb)[0] + '.md' clear_notebook(old_ipynb, new_ipynb) os.system('jupyter nbconvert ' + new_ipynb + ' --to markdown --output ' + md_file) with open(md_file, 'a') as f: f.write('<!-- INSERT SOURCE DOWNLOAD BUTTONS -->') os.system('rm ' + new_ipynb)
Example #2
Source File: data_io.py From Kaggler with MIT License | 6 votes |
def load_csv(path): """Load data from a CSV file. Args: path (str): A path to the CSV format file containing data. dense (boolean): An optional variable indicating if the return matrix should be dense. By default, it is false. Returns: Data matrix X and target vector y """ with open(path) as f: line = f.readline().strip() X = np.loadtxt(path, delimiter=',', skiprows=0 if is_number(line.split(',')[0]) else 1) y = np.array(X[:, 0]).flatten() X = X[:, 1:] return X, y
Example #3
Source File: text.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def _get_data(self): archive_file_name, archive_hash = self._archive_file data_file_name, data_hash = self._data_file[self._segment] path = os.path.join(self._root, data_file_name) if not os.path.exists(path) or not check_sha1(path, data_hash): namespace = 'gluon/dataset/'+self._namespace downloaded_file_path = download(_get_repo_file_url(namespace, archive_file_name), path=self._root, sha1_hash=archive_hash) with zipfile.ZipFile(downloaded_file_path, 'r') as zf: for member in zf.namelist(): filename = os.path.basename(member) if filename: dest = os.path.join(self._root, filename) with zf.open(member) as source, \ open(dest, "wb") as target: shutil.copyfileobj(source, target) data, label = self._read_batch(path) self._data = nd.array(data, dtype=data.dtype).reshape((-1, self._seq_len)) self._label = nd.array(label, dtype=label.dtype).reshape((-1, self._seq_len))
Example #4
Source File: straight_dope_test_utils.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def _override_relative_paths(notebook): """Overrides the relative path for the data and image directories to point to the right places. This is required as we run the notebooks in a different directory hierarchy more suitable for testing. Args: notebook : string notebook name in folder/notebook format """ notebook_path = os.path.join(*([NOTEBOOKS_DIR] + notebook.split('/'))) + ".ipynb" # Read the notebook and set epochs to num_epochs. with io.open(notebook_path, 'r', encoding='utf-8') as f: notebook = f.read() # Update the location for the data directory. modified_notebook = re.sub(RELATIVE_PATH_REGEX, NOTEBOOKS_DIR, notebook) # Replace the original notebook with the modified one. with io.open(notebook_path, 'w', encoding='utf-8') as f: f.write(modified_notebook)
Example #5
Source File: straight_dope_test_utils.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def _override_epochs(notebook): """Overrides the number of epochs in the notebook to 1 epoch. Note this operation is idempotent. Args: notebook : string notebook name in folder/notebook format """ notebook_path = os.path.join(*([NOTEBOOKS_DIR] + notebook.split('/'))) + ".ipynb" # Read the notebook and set epochs to num_epochs. with io.open(notebook_path, 'r', encoding='utf-8') as f: notebook = f.read() # Set number of epochs to 1. modified_notebook = re.sub(EPOCHS_REGEX, 'epochs = 1', notebook) # Replace the original notebook with the modified one. with io.open(notebook_path, 'w', encoding='utf-8') as f: f.write(modified_notebook)
Example #6
Source File: news_corpus_generator.py From news-corpus-builder with MIT License | 6 votes |
def read_links_file(self,file_path): ''' Read links and associated categories for specified articles in text file seperated by a space Args: file_path (str): The path to text file with news article links and category Returns: articles: Array of tuples that contains article link & cateogory ex. [('IPO','www.cs.columbia.edu')] ''' articles = [] with open(file_path) as f: for line in f: line = line.strip() #Ignore blank lines if len(line) != 0: link,category = line.split(' ') articles.append((category.rstrip(),link.strip())) return articles
Example #7
Source File: driver_utils.py From toolium with Apache License 2.0 | 6 votes |
def save_webdriver_logs_by_type(self, log_type, test_name): """Get webdriver logs of the specified type and write them to a log file :param log_type: browser, client, driver, performance, server, syslog, crashlog or logcat :param test_name: test that has generated these logs """ try: logs = self.driver_wrapper.driver.get_log(log_type) except Exception: return if len(logs) > 0: from toolium.driver_wrappers_pool import DriverWrappersPool log_file_name = '{}_{}.txt'.format(get_valid_filename(test_name), log_type) log_file_name = os.path.join(DriverWrappersPool.logs_directory, log_file_name) with open(log_file_name, 'a+', encoding='utf-8') as log_file: driver_type = self.driver_wrapper.config.get('Driver', 'type') log_file.write( u"\n{} '{}' test logs with driver = {}\n\n".format(datetime.now(), test_name, driver_type)) for entry in logs: timestamp = datetime.fromtimestamp(float(entry['timestamp']) / 1000.).strftime( '%Y-%m-%d %H:%M:%S.%f') log_file.write(u'{}\t{}\t{}\n'.format(timestamp, entry['level'], entry['message'].rstrip()))
Example #8
Source File: cnn_dailymail.py From fine-lm with MIT License | 6 votes |
def write_raw_text_to_files(all_files, urls_path, tmp_dir, is_training): """Write text to files.""" def write_to_file(all_files, urls_path, tmp_dir, filename): with io.open(os.path.join(tmp_dir, filename + ".source"), "w") as fstory: with io.open(os.path.join(tmp_dir, filename + ".target"), "w") as fsummary: for example in example_generator(all_files, urls_path, sum_token=True): story, summary = _story_summary_split(example) fstory.write(story + "\n") fsummary.write(summary + "\n") filename = "cnndm.train" if is_training else "cnndm.dev" tf.logging.info("Writing %s" % filename) write_to_file(all_files, urls_path, tmp_dir, filename) if not is_training: test_urls_path = generator_utils.maybe_download(tmp_dir, "all_test.txt", _TEST_URLS) filename = "cnndm.test" tf.logging.info("Writing %s" % filename) write_to_file(all_files, test_urls_path, tmp_dir, filename)
Example #9
Source File: generator_utils_test.py From fine-lm with MIT License | 6 votes |
def testGunzipFile(self): tmp_dir = self.get_temp_dir() (_, tmp_file_path) = tempfile.mkstemp(dir=tmp_dir) # Create a test zip file and unzip it. with gzip.open(tmp_file_path + ".gz", "wb") as gz_file: gz_file.write(bytes("test line", "utf-8")) generator_utils.gunzip_file(tmp_file_path + ".gz", tmp_file_path + ".txt") # Check that the unzipped result is as expected. lines = [] for line in io.open(tmp_file_path + ".txt", "rb"): lines.append(line.decode("utf-8").strip()) self.assertEqual(len(lines), 1) self.assertEqual(lines[0], "test line") # Clean up. os.remove(tmp_file_path + ".gz") os.remove(tmp_file_path + ".txt") os.remove(tmp_file_path)
Example #10
Source File: helper.py From cherrypy with BSD 3-Clause "New" or "Revised" License | 6 votes |
def write_conf(self, extra=''): if self.ssl: serverpem = os.path.join(thisdir, 'test.pem') ssl = """ server.ssl_certificate: r'%s' server.ssl_private_key: r'%s' """ % (serverpem, serverpem) else: ssl = '' conf = self.config_template % { 'host': self.host, 'port': self.port, 'error_log': self.error_log, 'access_log': self.access_log, 'ssl': ssl, 'extra': extra, } with io.open(self.config_file, 'w', encoding='utf-8') as f: f.write(str(conf))
Example #11
Source File: parser.py From tldr.py with MIT License | 6 votes |
def parse_page(page): """Parse the command man page.""" colors = get_config()['colors'] with io.open(page, encoding='utf-8') as f: lines = f.readlines() output_lines = [] for line in lines[1:]: if is_headline(line): continue elif is_description(line): output_lines.append(click.style(line.replace('>', ' '), fg=colors['description'])) elif is_old_usage(line): output_lines.append(click.style(line, fg=colors['usage'])) elif is_code_example(line): line = ' ' + line if line.startswith('`') else line[2:] output_lines.append(click.style(line.replace('`', ''), fg=colors['command'])) elif is_line_break(line): output_lines.append(click.style(line)) else: output_lines.append(click.style('- ' + line, fg=colors['usage'])) return output_lines
Example #12
Source File: msr_paraphrase.py From lineflow with MIT License | 6 votes |
def get_msr_paraphrase() -> Dict[str, List[Dict[str, str]]]: url = 'https://raw.githubusercontent.com/wasiahmad/paraphrase_identification/master/dataset/msr-paraphrase-corpus/msr_paraphrase_{}.txt' # NOQA root = download.get_cache_directory(os.path.join('datasets', 'msr_paraphrase')) def creator(path): dataset = {} fieldnames = ('quality', 'id1', 'id2', 'string1', 'string2') for split in ('train', 'test'): data_path = gdown.cached_download(url.format(split)) with io.open(data_path, 'r', encoding='utf-8') as f: f.readline() # skip header reader = csv.DictReader(f, delimiter='\t', fieldnames=fieldnames) dataset[split] = [dict(row) for row in reader] with io.open(path, 'wb') as f: pickle.dump(dataset, f) return dataset def loader(path): with io.open(path, 'rb') as f: return pickle.load(f) pkl_path = os.path.join(root, 'msr_paraphrase.pkl') return download.cache_or_load_file(pkl_path, creator, loader)
Example #13
Source File: build_docker_image.py From aegea with Apache License 2.0 | 6 votes |
def get_dockerfile(args): if args.dockerfile: return io.open(args.dockerfile, "rb").read() else: cmd = bash_cmd_preamble + [ "apt-get update -qq", "apt-get install -qqy cloud-init net-tools", "echo $CLOUD_CONFIG_B64 | base64 --decode > /etc/cloud/cloud.cfg.d/99_aegea.cfg", "cloud-init init", "cloud-init modules --mode=config", "cloud-init modules --mode=final" ] return dockerfile.format(base_image=args.base_image, maintainer=ARN.get_iam_username(), label=" ".join(args.tags), cloud_config_b64=base64.b64encode(get_cloud_config(args)).decode(), run=json.dumps(cmd)).encode()
Example #14
Source File: conll2000.py From lineflow with MIT License | 6 votes |
def get_conll2000() -> Dict[str, List[str]]: url = 'https://www.clips.uantwerpen.be/conll2000/chunking/{}.txt.gz' root = download.get_cache_directory(os.path.join('datasets', 'conll2000')) def creator(path): dataset = {} for split in ('train', 'test'): data_path = gdown.cached_download(url.format(split)) with gzip.open(data_path) as f: data = f.read().decode('utf-8').split('\n\n') dataset[split] = data with io.open(path, 'wb') as f: pickle.dump(dataset, f) return dataset def loader(path): with io.open(path, 'rb') as f: return pickle.load(f) pkl_path = os.path.join(root, 'conll2000.pkl') return download.cache_or_load_file(pkl_path, creator, loader)
Example #15
Source File: cli.py From tldr.py with MIT License | 6 votes |
def build_index(): repo_directory = get_config()['repo_directory'] index_path = path.join(repo_directory, 'pages', 'index.json') page_path = path.join(repo_directory, 'pages') tree_generator = os.walk(page_path) folders = next(tree_generator)[1] commands, new_index = {}, {} for folder in folders: pages = next(tree_generator)[2] for page in pages: command_name = path.splitext(page)[0] if command_name not in commands: commands[command_name] = {'name': command_name, 'platform': [folder]} else: commands[command_name]['platform'].append(folder) command_list = [item[1] for item in sorted(commands.items(), key=itemgetter(0))] new_index['commands'] = command_list with open(index_path, mode='w') as f: json.dump(new_index, f)
Example #16
Source File: driver_utils.py From toolium with Apache License 2.0 | 5 votes |
def _download_video(self, video_url, video_name): """Download a video from the remote node :param video_url: video url :param video_name: video name """ from toolium.driver_wrappers_pool import DriverWrappersPool filename = '{0:0=2d}_{1}'.format(DriverWrappersPool.videos_number, video_name) filename = '{}.mp4'.format(get_valid_filename(filename)) filepath = os.path.join(DriverWrappersPool.videos_directory, filename) makedirs_safe(DriverWrappersPool.videos_directory) response = requests.get(video_url) open(filepath, 'wb').write(response.content) self.logger.info("Video saved in '%s'", filepath) DriverWrappersPool.videos_number += 1
Example #17
Source File: setup.py From python-esppy with Apache License 2.0 | 5 votes |
def get_file(fname): with io.open(os.path.join(os.path.dirname(os.path.abspath(__file__)), fname), encoding='utf8') as infile: return infile.read()
Example #18
Source File: main.py From pwnypack with MIT License | 5 votes |
def binary_value_or_stdin(value): """ Return fsencoded value or read raw data from stdin if value is None. """ if value is None: reader = io.open(sys.stdin.fileno(), mode='rb', closefd=False) return reader.read() elif six.PY3: return os.fsencode(value) else: return value
Example #19
Source File: setup.py From nose-htmloutput with BSD 2-Clause "Simplified" License | 5 votes |
def read(*names, **kwargs): return io.open( join(dirname(__file__), *names), encoding=kwargs.get("encoding", "utf8") ).read()
Example #20
Source File: cnn_dailymail.py From lineflow with MIT License | 5 votes |
def get_cnn_dailymail() -> Dict[str, Tuple[arrayfiles.TextFile]]: url = 'https://s3.amazonaws.com/opennmt-models/Summary/cnndm.tar.gz' root = download.get_cache_directory(os.path.join('datasets', 'cnn_dailymail')) def creator(path): archive_path = gdown.cached_download(url) target_path = os.path.join(root, 'raw') with tarfile.open(archive_path, 'r') as archive: print(f'Extracting to {target_path}') archive.extractall(target_path) dataset = {} for split in ('train', 'dev', 'test'): src_path = f'{split if split != "dev" else "val"}.txt.src' tgt_path = f'{split if split != "dev" else "val"}.txt.tgt.tagged' dataset[split] = ( arrayfiles.TextFile(os.path.join(target_path, src_path)), arrayfiles.TextFile(os.path.join(target_path, tgt_path)) ) with io.open(path, 'wb') as f: pickle.dump(dataset, f) return dataset def loader(path): with io.open(path, 'rb') as f: return pickle.load(f) pkl_path = os.path.join(root, 'cnndm.pkl') return download.cache_or_load_file(pkl_path, creator, loader)
Example #21
Source File: setup.py From qtsass with MIT License | 5 votes |
def get_description(): """Get long description.""" with open(os.path.join(HERE, 'README.md'), 'r', encoding='utf-8') as f: data = f.read() return data
Example #22
Source File: tempfile.py From jawfish with MIT License | 5 votes |
def NamedTemporaryFile(mode='w+b', buffering=-1, encoding=None, newline=None, suffix="", prefix=template, dir=None, delete=True): """Create and return a temporary file. Arguments: 'prefix', 'suffix', 'dir' -- as for mkstemp. 'mode' -- the mode argument to io.open (default "w+b"). 'buffering' -- the buffer size argument to io.open (default -1). 'encoding' -- the encoding argument to io.open (default None) 'newline' -- the newline argument to io.open (default None) 'delete' -- whether the file is deleted on close (default True). The file is created as mkstemp() would do it. Returns an object with a file-like interface; the name of the file is accessible as file.name. The file will be automatically deleted when it is closed unless the 'delete' argument is set to False. """ if dir is None: dir = gettempdir() flags = _bin_openflags # Setting O_TEMPORARY in the flags causes the OS to delete # the file when it is closed. This is only supported by Windows. if _os.name == 'nt' and delete: flags |= _os.O_TEMPORARY (fd, name) = _mkstemp_inner(dir, prefix, suffix, flags) file = _io.open(fd, mode, buffering=buffering, newline=newline, encoding=encoding) return _TemporaryFileWrapper(file, name, delete)
Example #23
Source File: tempfile.py From jawfish with MIT License | 5 votes |
def mkstemp(suffix="", prefix=template, dir=None, text=False): """User-callable function to create and return a unique temporary file. The return value is a pair (fd, name) where fd is the file descriptor returned by os.open, and name is the filename. If 'suffix' is specified, the file name will end with that suffix, otherwise there will be no suffix. If 'prefix' is specified, the file name will begin with that prefix, otherwise a default prefix is used. If 'dir' is specified, the file will be created in that directory, otherwise a default directory is used. If 'text' is specified and true, the file is opened in text mode. Else (the default) the file is opened in binary mode. On some operating systems, this makes no difference. The file is readable and writable only by the creating user ID. If the operating system uses permission bits to indicate whether a file is executable, the file is executable by no one. The file descriptor is not inherited by children of this process. Caller is responsible for deleting the file when done with it. """ if dir is None: dir = gettempdir() if text: flags = _text_openflags else: flags = _bin_openflags return _mkstemp_inner(dir, prefix, suffix, flags)
Example #24
Source File: tempfile.py From jawfish with MIT License | 5 votes |
def _mkstemp_inner(dir, pre, suf, flags): """Code common to mkstemp, TemporaryFile, and NamedTemporaryFile.""" names = _get_candidate_names() for seq in range(TMP_MAX): name = next(names) file = _os.path.join(dir, pre + name + suf) try: fd = _os.open(file, flags, 0o600) _set_cloexec(fd) return (fd, _os.path.abspath(file)) except FileExistsError: continue # try again except PermissionError: # This exception is thrown when a directory with the chosen name # already exists on windows. if _os.name == 'nt': continue else: raise raise FileExistsError(_errno.EEXIST, "No usable temporary file name found") # User visible interfaces.
Example #25
Source File: tempfile.py From jawfish with MIT License | 5 votes |
def _get_default_tempdir(): """Calculate the default directory to use for temporary files. This routine should be called exactly once. We determine whether or not a candidate temp dir is usable by trying to create and write to a file in that directory. If this is successful, the test file is deleted. To prevent denial of service, the name of the test file must be randomized.""" namer = _RandomNameSequence() dirlist = _candidate_tempdir_list() for dir in dirlist: if dir != _os.curdir: dir = _os.path.normcase(_os.path.abspath(dir)) # Try only a few names per directory. for seq in range(100): name = next(namer) filename = _os.path.join(dir, name) try: fd = _os.open(filename, _bin_openflags, 0o600) try: try: with _io.open(fd, 'wb', closefd=False) as fp: fp.write(b'blat') finally: _os.close(fd) finally: _os.unlink(filename) return dir except FileExistsError: pass except OSError: break # no point trying more names in this directory raise FileNotFoundError(_errno.ENOENT, "No usable temporary directory found in %s" % dirlist)
Example #26
Source File: tempfile.py From jawfish with MIT License | 5 votes |
def _stat(fn): f = open(fn) f.close()
Example #27
Source File: subprocess.py From jawfish with MIT License | 5 votes |
def _get_devnull(self): if not hasattr(self, '_devnull'): self._devnull = os.open(os.devnull, os.O_RDWR) return self._devnull
Example #28
Source File: config.py From tldr.py with MIT License | 5 votes |
def get_config(): """Get the configurations from .tldrrc and return it as a dict.""" config_path = path.join( (os.environ.get('TLDR_CONFIG_DIR') or path.expanduser('~')), '.tldrrc') config_path = os.path.abspath(os.path.expanduser(config_path)) if not path.exists(config_path): sys.exit("Can't find config file at: {0}. You may use `tldr init` " "to init the config file.".format(config_path)) with io.open(config_path, encoding='utf-8') as f: try: config = yaml.safe_load(f) except yaml.scanner.ScannerError: sys.exit("The config file is not a valid YAML file.") supported_colors = ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'] if not set(config['colors'].values()).issubset(set(supported_colors)): sys.exit("Unsupported colors in config file: {0}.".format( ', '.join(set(config['colors'].values()) - set(supported_colors)))) if not path.exists(config['repo_directory']): sys.exit("Can't find the tldr repo, check the `repo_directory` " "setting in config file.") return config
Example #29
Source File: cli.py From tldr.py with MIT License | 5 votes |
def init(): """Init config file.""" default_config_path = path.join( (os.environ.get('TLDR_CONFIG_DIR') or path.expanduser('~')), '.tldrrc') if path.exists(default_config_path): click.echo("There is already a config file exists, " "skip initializing it.") else: repo_path = click.prompt("Input the tldr repo path") repo_path = os.path.abspath(os.path.expanduser(repo_path)) if not path.exists(repo_path): sys.exit("Repo path not exist, clone it first.") platform = click.prompt("Input your platform(linux, osx or sunos)") if platform not in ['linux', 'osx', 'sunos']: sys.exit("Platform should be linux, osx or sunos.") colors = { "description": "blue", "usage": "green", "command": "cyan" } config = { "repo_directory": repo_path, "colors": colors, "platform": platform } with open(default_config_path, 'w') as f: f.write(yaml.safe_dump(config, default_flow_style=False)) click.echo("Initializing the config file at {0}".format( default_config_path))
Example #30
Source File: cli.py From tldr.py with MIT License | 5 votes |
def get_index(): """Retrieve index in the pages directory.""" repo_directory = get_config()['repo_directory'] with io.open(path.join(repo_directory, 'pages/index.json'), encoding='utf-8') as f: index = json.load(f) return index