module filehelper.magic_file

Inheritance diagram of pyensae.filehelper.magic_file

Short summary

module pyensae.filehelper.magic_file

Magic command to handle files

source on GitHub

Classes

class

truncated documentation

MagicFile

Defines magic commands to help with files

Functions

function

truncated documentation

register_file_magics

register magics function, can be called from a notebook

Properties

property

truncated documentation

Context

return the context or None

cross_validation_lock

A contextmanager for running a block with our cross validation lock set to True. At the end of the block, …

Static Methods

staticmethod

truncated documentation

encoding_parser

Defines the way to parse the magic command %encoding.

grep_parser

defines the way to parse the magic command %grep

head_parser

defines the way to parse the magic command %head

hhelp_parser

Defines the way to parse the magic command %hhelp.

lsr_parser

defines the way to parse the magic command %lsr

lsrepo_parser

Defines the way to parse the magic command %lsrepo.

PYTHON_parser

Defines the way to parse the magic command %%PYTHON.

runpy_parser

Defines the way to parse the magic command %%runpy.

tail_parser

defines the way to parse the magic command %tail

Methods

method

truncated documentation

encoding

Defines %encoding which guesses the encoding.

grep

defines %grep which displays the first lines of a file

head

Defines %head which displays the first lines of a file.

hhelp

Define %hhelp, it displays the help for an object in :epkg:`HTML`.

lsr

Defines %lsr which returns the content of a folder, the method stops after around 10000 files –> you …

lsrepo

Defines %lsrepo.

PYTHON

Defines command %%PYTHON.

runpy

Defines command %%runpy.

tail

defines %tail which displays the last lines of a file

Documentation

Magic command to handle files

source on GitHub

class pyensae.filehelper.magic_file.MagicFile(**kwargs: Any)

Bases: MagicClassWithHelpers

Defines magic commands to help with files

Magic command not found

Magic commands are automatically added when importing the module:

import pyensae

This instruction does not raise any exception. To understand what went wrong, the following instruction must be run:

%load_ext pyensae

Usually, one necessary module is missing.

New in version 1.1.

source on GitHub

Create a configurable given a config config.

Parameters

configConfig

If this is empty, default values are used. If config is a Config instance, it will be used to configure the instance.

parentConfigurable instance, optional

The parent Configurable instance of this object.

Notes

Subclasses of Configurable must call the __init__() method of Configurable before doing anything else and using super():

class MyConfigurable(Configurable):
    def __init__(self, config=None):
        super(MyConfigurable, self).__init__(config=config)
        # Then any other code you need to finish initialization.

This ensures that instances will be configured properly.

PYTHON(line, cell=None)

Defines command %%PYTHON.

PYTHON

The magic command %%PYTHON is almost to:

with open(<filename>, "w", encoding="utf8") as f:
    f.write("# -*- coding: utf8 -*-\n")
    f.write(cell.replace("\r", ""))

source on GitHub

static PYTHON_parser()

Defines the way to parse the magic command %%PYTHON.

source on GitHub

_all_trait_default_generators: Dict[str, Any] = {'config': <bound method TraitType.default of <traitlets.traitlets.Instance object>>, 'parent': <bound method TraitType.default of <traitlets.traitlets.Instance object>>}
_cross_validation_lock: bool
_descriptors = [<traitlets.traitlets.ObserveHandler object>, <traitlets.traitlets.Instance object>, <traitlets.traitlets.Instance object>]
_instance_inits = [<bound method ObserveHandler.instance_init of <traitlets.traitlets.ObserveHandler object>>, <bound method Instance.instance_init of <traitlets.traitlets.Instance object>>, <bound method Instance.instance_init of <traitlets.traitlets.Instance object>>]
_static_immutable_initial_values: Dict[str, Any] = {'parent': None}
_trait_default_generators = {}
_trait_notifiers: Dict[str, Any]
_trait_validators: Dict[str, Any]
_trait_values: Dict[str, Any]
_traits: Dict[str, Any] = {'config': <traitlets.traitlets.Instance object>, 'parent': <traitlets.traitlets.Instance object>}
encoding(line)

Defines %encoding which guesses the encoding.

encoding

The magic command %encoding is equivalent to:

from pyensae.file_helper import file_head
file_head(<filename>, <n>, <encoding>)

source on GitHub

static encoding_parser()

Defines the way to parse the magic command %encoding.

source on GitHub

grep(line)

defines %grep which displays the first lines of a file

grep

The magic command %grep is equivalent to:

from pyensae.file_helper import enumerate_grep
list(enumerate_grep(<filename>, <regex>, <encoding>))

source on GitHub

static grep_parser()

defines the way to parse the magic command %grep

source on GitHub

head(line)

Defines %head which displays the first lines of a file.

head

The magic command %head is equivalent to:

from pyensae.file_helper import file_head
file_head(<filename>, <n>, <encoding>)

source on GitHub

static head_parser()

defines the way to parse the magic command %head

source on GitHub

hhelp(line)

Define %hhelp, it displays the help for an object in :epkg:`HTML`.

hhelp

The magic command is equivalent to:

from pyquickhelper import docstring2html
docstring2html(obj, format=format)

New in version 1.1.

source on GitHub

static hhelp_parser()

Defines the way to parse the magic command %hhelp.

New in version 1.1.

source on GitHub

lsr(line)

Defines %lsr which returns the content of a folder, the method stops after around 10000 files –> you should precise the filter.

lsr

The magic command %lsr is almost equivalent to:

from pyquickhelper.file_helper import explore_folder_iterfile
res = explore_folder_iterfile(<filename>, <pattern>)
for f in res:
    print(f)

source on GitHub

static lsr_parser()

defines the way to parse the magic command %lsr

source on GitHub

lsrepo(line)

Defines %lsrepo.

lsrepo

The method returns the files present in a repository (:epkg:`GIT` or :pkg:`SVN`). The code is equivalent to:

from pyquickhelper.filehelper import explore_folder_iterfile_repo
res = explore_folder_iterfile_repo(<filename>, <pattern>)
for f in res:
    print(f)

New in version 1.1.

source on GitHub

static lsrepo_parser()

Defines the way to parse the magic command %lsrepo.

source on GitHub

runpy(line, cell=None)

Defines command %%runpy.

runpy

%%runpy runs a python script which accepts standards input and produces standard outputs, a timeout is set up at 10s. It is almost equivalent to:

from pyquickhelper.loghelper import run_cmd
import sys
cmd = sys.executable.replace(
    "pythonw",
    "python") + " " + filename + " " + args
out, err = run_cmd(
    cmd, wait=True, sin=cell, communicate=True, timeout=10, shell=False)

New in version 1.1.

source on GitHub

static runpy_parser()

Defines the way to parse the magic command %%runpy.

source on GitHub

tail(line)

defines %tail which displays the last lines of a file

tail

The magic command %tail is equivalent to:

from pyensae.file_helper import file_tail
file_tail(<filename>, <n>, <encoding>)

source on GitHub

static tail_parser()

defines the way to parse the magic command %tail

source on GitHub

pyensae.filehelper.magic_file.register_file_magics(ip=None)

register magics function, can be called from a notebook

Parameters:

ip – from get_ipython()

source on GitHub